hns3_enet.c 117 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615
  1. // SPDX-License-Identifier: GPL-2.0+
  2. // Copyright (c) 2016-2017 Hisilicon Limited.
  3. #include <linux/dma-mapping.h>
  4. #include <linux/etherdevice.h>
  5. #include <linux/interrupt.h>
  6. #ifdef CONFIG_RFS_ACCEL
  7. #include <linux/cpu_rmap.h>
  8. #endif
  9. #include <linux/if_vlan.h>
  10. #include <linux/ip.h>
  11. #include <linux/ipv6.h>
  12. #include <linux/module.h>
  13. #include <linux/pci.h>
  14. #include <linux/aer.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/sctp.h>
  17. #include <linux/vermagic.h>
  18. #include <net/gre.h>
  19. #include <net/ip6_checksum.h>
  20. #include <net/pkt_cls.h>
  21. #include <net/tcp.h>
  22. #include <net/vxlan.h>
  23. #include <net/geneve.h>
  24. #include "hnae3.h"
  25. #include "hns3_enet.h"
  26. #define hns3_set_field(origin, shift, val) ((origin) |= ((val) << (shift)))
  27. #define hns3_tx_bd_count(S) DIV_ROUND_UP(S, HNS3_MAX_BD_SIZE)
  28. #define hns3_rl_err(fmt, ...) \
  29. do { \
  30. if (net_ratelimit()) \
  31. netdev_err(fmt, ##__VA_ARGS__); \
  32. } while (0)
  33. static void hns3_clear_all_ring(struct hnae3_handle *h, bool force);
  34. static void hns3_remove_hw_addr(struct net_device *netdev);
  35. static const char hns3_driver_name[] = "hns3";
  36. const char hns3_driver_version[] = VERMAGIC_STRING;
  37. static const char hns3_driver_string[] =
  38. "Hisilicon Ethernet Network Driver for Hip08 Family";
  39. static const char hns3_copyright[] = "Copyright (c) 2017 Huawei Corporation.";
  40. static struct hnae3_client client;
  41. static int debug = -1;
  42. module_param(debug, int, 0);
  43. MODULE_PARM_DESC(debug, " Network interface message level setting");
  44. #define DEFAULT_MSG_LEVEL (NETIF_MSG_PROBE | NETIF_MSG_LINK | \
  45. NETIF_MSG_IFDOWN | NETIF_MSG_IFUP)
  46. #define HNS3_INNER_VLAN_TAG 1
  47. #define HNS3_OUTER_VLAN_TAG 2
  48. #define HNS3_MIN_TX_LEN 33U
  49. #define HNS3_MIN_TUN_PKT_LEN 65U
  50. /* hns3_pci_tbl - PCI Device ID Table
  51. *
  52. * Last entry must be all 0s
  53. *
  54. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  55. * Class, Class Mask, private data (not used) }
  56. */
  57. static const struct pci_device_id hns3_pci_tbl[] = {
  58. {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_GE), 0},
  59. {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_25GE), 0},
  60. {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_25GE_RDMA),
  61. HNAE3_DEV_SUPPORT_ROCE_DCB_BITS},
  62. {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_25GE_RDMA_MACSEC),
  63. HNAE3_DEV_SUPPORT_ROCE_DCB_BITS},
  64. {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_50GE_RDMA),
  65. HNAE3_DEV_SUPPORT_ROCE_DCB_BITS},
  66. {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_50GE_RDMA_MACSEC),
  67. HNAE3_DEV_SUPPORT_ROCE_DCB_BITS},
  68. {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_100G_RDMA_MACSEC),
  69. HNAE3_DEV_SUPPORT_ROCE_DCB_BITS},
  70. {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_100G_VF), 0},
  71. {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_100G_RDMA_DCB_PFC_VF),
  72. HNAE3_DEV_SUPPORT_ROCE_DCB_BITS},
  73. /* required last entry */
  74. {0, }
  75. };
  76. MODULE_DEVICE_TABLE(pci, hns3_pci_tbl);
  77. static irqreturn_t hns3_irq_handle(int irq, void *vector)
  78. {
  79. struct hns3_enet_tqp_vector *tqp_vector = vector;
  80. napi_schedule_irqoff(&tqp_vector->napi);
  81. return IRQ_HANDLED;
  82. }
  83. static void hns3_nic_uninit_irq(struct hns3_nic_priv *priv)
  84. {
  85. struct hns3_enet_tqp_vector *tqp_vectors;
  86. unsigned int i;
  87. for (i = 0; i < priv->vector_num; i++) {
  88. tqp_vectors = &priv->tqp_vector[i];
  89. if (tqp_vectors->irq_init_flag != HNS3_VECTOR_INITED)
  90. continue;
  91. /* clear the affinity mask */
  92. irq_set_affinity_hint(tqp_vectors->vector_irq, NULL);
  93. /* release the irq resource */
  94. free_irq(tqp_vectors->vector_irq, tqp_vectors);
  95. tqp_vectors->irq_init_flag = HNS3_VECTOR_NOT_INITED;
  96. }
  97. }
  98. static int hns3_nic_init_irq(struct hns3_nic_priv *priv)
  99. {
  100. struct hns3_enet_tqp_vector *tqp_vectors;
  101. int txrx_int_idx = 0;
  102. int rx_int_idx = 0;
  103. int tx_int_idx = 0;
  104. unsigned int i;
  105. int ret;
  106. for (i = 0; i < priv->vector_num; i++) {
  107. tqp_vectors = &priv->tqp_vector[i];
  108. if (tqp_vectors->irq_init_flag == HNS3_VECTOR_INITED)
  109. continue;
  110. if (tqp_vectors->tx_group.ring && tqp_vectors->rx_group.ring) {
  111. snprintf(tqp_vectors->name, HNAE3_INT_NAME_LEN - 1,
  112. "%s-%s-%d", priv->netdev->name, "TxRx",
  113. txrx_int_idx++);
  114. txrx_int_idx++;
  115. } else if (tqp_vectors->rx_group.ring) {
  116. snprintf(tqp_vectors->name, HNAE3_INT_NAME_LEN - 1,
  117. "%s-%s-%d", priv->netdev->name, "Rx",
  118. rx_int_idx++);
  119. } else if (tqp_vectors->tx_group.ring) {
  120. snprintf(tqp_vectors->name, HNAE3_INT_NAME_LEN - 1,
  121. "%s-%s-%d", priv->netdev->name, "Tx",
  122. tx_int_idx++);
  123. } else {
  124. /* Skip this unused q_vector */
  125. continue;
  126. }
  127. tqp_vectors->name[HNAE3_INT_NAME_LEN - 1] = '\0';
  128. ret = request_irq(tqp_vectors->vector_irq, hns3_irq_handle, 0,
  129. tqp_vectors->name, tqp_vectors);
  130. if (ret) {
  131. netdev_err(priv->netdev, "request irq(%d) fail\n",
  132. tqp_vectors->vector_irq);
  133. hns3_nic_uninit_irq(priv);
  134. return ret;
  135. }
  136. irq_set_affinity_hint(tqp_vectors->vector_irq,
  137. &tqp_vectors->affinity_mask);
  138. tqp_vectors->irq_init_flag = HNS3_VECTOR_INITED;
  139. }
  140. return 0;
  141. }
  142. static void hns3_mask_vector_irq(struct hns3_enet_tqp_vector *tqp_vector,
  143. u32 mask_en)
  144. {
  145. writel(mask_en, tqp_vector->mask_addr);
  146. }
  147. static void hns3_vector_enable(struct hns3_enet_tqp_vector *tqp_vector)
  148. {
  149. napi_enable(&tqp_vector->napi);
  150. /* enable vector */
  151. hns3_mask_vector_irq(tqp_vector, 1);
  152. }
  153. static void hns3_vector_disable(struct hns3_enet_tqp_vector *tqp_vector)
  154. {
  155. /* disable vector */
  156. hns3_mask_vector_irq(tqp_vector, 0);
  157. disable_irq(tqp_vector->vector_irq);
  158. napi_disable(&tqp_vector->napi);
  159. }
  160. void hns3_set_vector_coalesce_rl(struct hns3_enet_tqp_vector *tqp_vector,
  161. u32 rl_value)
  162. {
  163. u32 rl_reg = hns3_rl_usec_to_reg(rl_value);
  164. /* this defines the configuration for RL (Interrupt Rate Limiter).
  165. * Rl defines rate of interrupts i.e. number of interrupts-per-second
  166. * GL and RL(Rate Limiter) are 2 ways to acheive interrupt coalescing
  167. */
  168. if (rl_reg > 0 && !tqp_vector->tx_group.coal.gl_adapt_enable &&
  169. !tqp_vector->rx_group.coal.gl_adapt_enable)
  170. /* According to the hardware, the range of rl_reg is
  171. * 0-59 and the unit is 4.
  172. */
  173. rl_reg |= HNS3_INT_RL_ENABLE_MASK;
  174. writel(rl_reg, tqp_vector->mask_addr + HNS3_VECTOR_RL_OFFSET);
  175. }
  176. void hns3_set_vector_coalesce_rx_gl(struct hns3_enet_tqp_vector *tqp_vector,
  177. u32 gl_value)
  178. {
  179. u32 rx_gl_reg = hns3_gl_usec_to_reg(gl_value);
  180. writel(rx_gl_reg, tqp_vector->mask_addr + HNS3_VECTOR_GL0_OFFSET);
  181. }
  182. void hns3_set_vector_coalesce_tx_gl(struct hns3_enet_tqp_vector *tqp_vector,
  183. u32 gl_value)
  184. {
  185. u32 tx_gl_reg = hns3_gl_usec_to_reg(gl_value);
  186. writel(tx_gl_reg, tqp_vector->mask_addr + HNS3_VECTOR_GL1_OFFSET);
  187. }
  188. static void hns3_vector_gl_rl_init(struct hns3_enet_tqp_vector *tqp_vector,
  189. struct hns3_nic_priv *priv)
  190. {
  191. /* initialize the configuration for interrupt coalescing.
  192. * 1. GL (Interrupt Gap Limiter)
  193. * 2. RL (Interrupt Rate Limiter)
  194. *
  195. * Default: enable interrupt coalescing self-adaptive and GL
  196. */
  197. tqp_vector->tx_group.coal.gl_adapt_enable = 1;
  198. tqp_vector->rx_group.coal.gl_adapt_enable = 1;
  199. tqp_vector->tx_group.coal.int_gl = HNS3_INT_GL_50K;
  200. tqp_vector->rx_group.coal.int_gl = HNS3_INT_GL_50K;
  201. tqp_vector->rx_group.coal.flow_level = HNS3_FLOW_LOW;
  202. tqp_vector->tx_group.coal.flow_level = HNS3_FLOW_LOW;
  203. }
  204. static void hns3_vector_gl_rl_init_hw(struct hns3_enet_tqp_vector *tqp_vector,
  205. struct hns3_nic_priv *priv)
  206. {
  207. struct hnae3_handle *h = priv->ae_handle;
  208. hns3_set_vector_coalesce_tx_gl(tqp_vector,
  209. tqp_vector->tx_group.coal.int_gl);
  210. hns3_set_vector_coalesce_rx_gl(tqp_vector,
  211. tqp_vector->rx_group.coal.int_gl);
  212. hns3_set_vector_coalesce_rl(tqp_vector, h->kinfo.int_rl_setting);
  213. }
  214. static int hns3_nic_set_real_num_queue(struct net_device *netdev)
  215. {
  216. struct hnae3_handle *h = hns3_get_handle(netdev);
  217. struct hnae3_knic_private_info *kinfo = &h->kinfo;
  218. unsigned int queue_size = kinfo->rss_size * kinfo->num_tc;
  219. int i, ret;
  220. if (kinfo->num_tc <= 1) {
  221. netdev_reset_tc(netdev);
  222. } else {
  223. ret = netdev_set_num_tc(netdev, kinfo->num_tc);
  224. if (ret) {
  225. netdev_err(netdev,
  226. "netdev_set_num_tc fail, ret=%d!\n", ret);
  227. return ret;
  228. }
  229. for (i = 0; i < HNAE3_MAX_TC; i++) {
  230. if (!kinfo->tc_info[i].enable)
  231. continue;
  232. netdev_set_tc_queue(netdev,
  233. kinfo->tc_info[i].tc,
  234. kinfo->tc_info[i].tqp_count,
  235. kinfo->tc_info[i].tqp_offset);
  236. }
  237. }
  238. ret = netif_set_real_num_tx_queues(netdev, queue_size);
  239. if (ret) {
  240. netdev_err(netdev,
  241. "netif_set_real_num_tx_queues fail, ret=%d!\n", ret);
  242. return ret;
  243. }
  244. ret = netif_set_real_num_rx_queues(netdev, queue_size);
  245. if (ret) {
  246. netdev_err(netdev,
  247. "netif_set_real_num_rx_queues fail, ret=%d!\n", ret);
  248. return ret;
  249. }
  250. return 0;
  251. }
  252. static u16 hns3_get_max_available_channels(struct hnae3_handle *h)
  253. {
  254. u16 alloc_tqps, max_rss_size, rss_size;
  255. h->ae_algo->ops->get_tqps_and_rss_info(h, &alloc_tqps, &max_rss_size);
  256. rss_size = alloc_tqps / h->kinfo.num_tc;
  257. return min_t(u16, rss_size, max_rss_size);
  258. }
  259. static void hns3_tqp_enable(struct hnae3_queue *tqp)
  260. {
  261. u32 rcb_reg;
  262. rcb_reg = hns3_read_dev(tqp, HNS3_RING_EN_REG);
  263. rcb_reg |= BIT(HNS3_RING_EN_B);
  264. hns3_write_dev(tqp, HNS3_RING_EN_REG, rcb_reg);
  265. }
  266. static void hns3_tqp_disable(struct hnae3_queue *tqp)
  267. {
  268. u32 rcb_reg;
  269. rcb_reg = hns3_read_dev(tqp, HNS3_RING_EN_REG);
  270. rcb_reg &= ~BIT(HNS3_RING_EN_B);
  271. hns3_write_dev(tqp, HNS3_RING_EN_REG, rcb_reg);
  272. }
  273. static void hns3_free_rx_cpu_rmap(struct net_device *netdev)
  274. {
  275. #ifdef CONFIG_RFS_ACCEL
  276. free_irq_cpu_rmap(netdev->rx_cpu_rmap);
  277. netdev->rx_cpu_rmap = NULL;
  278. #endif
  279. }
  280. static int hns3_set_rx_cpu_rmap(struct net_device *netdev)
  281. {
  282. #ifdef CONFIG_RFS_ACCEL
  283. struct hns3_nic_priv *priv = netdev_priv(netdev);
  284. struct hns3_enet_tqp_vector *tqp_vector;
  285. int i, ret;
  286. if (!netdev->rx_cpu_rmap) {
  287. netdev->rx_cpu_rmap = alloc_irq_cpu_rmap(priv->vector_num);
  288. if (!netdev->rx_cpu_rmap)
  289. return -ENOMEM;
  290. }
  291. for (i = 0; i < priv->vector_num; i++) {
  292. tqp_vector = &priv->tqp_vector[i];
  293. ret = irq_cpu_rmap_add(netdev->rx_cpu_rmap,
  294. tqp_vector->vector_irq);
  295. if (ret) {
  296. hns3_free_rx_cpu_rmap(netdev);
  297. return ret;
  298. }
  299. }
  300. #endif
  301. return 0;
  302. }
  303. static int hns3_nic_net_up(struct net_device *netdev)
  304. {
  305. struct hns3_nic_priv *priv = netdev_priv(netdev);
  306. struct hnae3_handle *h = priv->ae_handle;
  307. int i, j;
  308. int ret;
  309. ret = hns3_nic_reset_all_ring(h);
  310. if (ret)
  311. return ret;
  312. /* the device can work without cpu rmap, only aRFS needs it */
  313. ret = hns3_set_rx_cpu_rmap(netdev);
  314. if (ret)
  315. netdev_warn(netdev, "set rx cpu rmap fail, ret=%d!\n", ret);
  316. /* get irq resource for all vectors */
  317. ret = hns3_nic_init_irq(priv);
  318. if (ret) {
  319. netdev_err(netdev, "init irq failed! ret=%d\n", ret);
  320. goto free_rmap;
  321. }
  322. clear_bit(HNS3_NIC_STATE_DOWN, &priv->state);
  323. /* enable the vectors */
  324. for (i = 0; i < priv->vector_num; i++)
  325. hns3_vector_enable(&priv->tqp_vector[i]);
  326. /* enable rcb */
  327. for (j = 0; j < h->kinfo.num_tqps; j++)
  328. hns3_tqp_enable(h->kinfo.tqp[j]);
  329. /* start the ae_dev */
  330. ret = h->ae_algo->ops->start ? h->ae_algo->ops->start(h) : 0;
  331. if (ret)
  332. goto out_start_err;
  333. return 0;
  334. out_start_err:
  335. set_bit(HNS3_NIC_STATE_DOWN, &priv->state);
  336. while (j--)
  337. hns3_tqp_disable(h->kinfo.tqp[j]);
  338. for (j = i - 1; j >= 0; j--)
  339. hns3_vector_disable(&priv->tqp_vector[j]);
  340. hns3_nic_uninit_irq(priv);
  341. free_rmap:
  342. hns3_free_rx_cpu_rmap(netdev);
  343. return ret;
  344. }
  345. static void hns3_config_xps(struct hns3_nic_priv *priv)
  346. {
  347. int i;
  348. for (i = 0; i < priv->vector_num; i++) {
  349. struct hns3_enet_tqp_vector *tqp_vector = &priv->tqp_vector[i];
  350. struct hns3_enet_ring *ring = tqp_vector->tx_group.ring;
  351. while (ring) {
  352. int ret;
  353. ret = netif_set_xps_queue(priv->netdev,
  354. &tqp_vector->affinity_mask,
  355. ring->tqp->tqp_index);
  356. if (ret)
  357. netdev_warn(priv->netdev,
  358. "set xps queue failed: %d", ret);
  359. ring = ring->next;
  360. }
  361. }
  362. }
  363. static int hns3_nic_net_open(struct net_device *netdev)
  364. {
  365. struct hns3_nic_priv *priv = netdev_priv(netdev);
  366. struct hnae3_handle *h = hns3_get_handle(netdev);
  367. struct hnae3_knic_private_info *kinfo;
  368. int i, ret;
  369. if (hns3_nic_resetting(netdev))
  370. return -EBUSY;
  371. if (!test_bit(HNS3_NIC_STATE_DOWN, &priv->state)) {
  372. netdev_warn(netdev, "net open repeatedly!\n");
  373. return 0;
  374. }
  375. netif_carrier_off(netdev);
  376. ret = hns3_nic_set_real_num_queue(netdev);
  377. if (ret)
  378. return ret;
  379. ret = hns3_nic_net_up(netdev);
  380. if (ret) {
  381. netdev_err(netdev, "net up fail, ret=%d!\n", ret);
  382. return ret;
  383. }
  384. kinfo = &h->kinfo;
  385. for (i = 0; i < HNAE3_MAX_USER_PRIO; i++)
  386. netdev_set_prio_tc_map(netdev, i, kinfo->prio_tc[i]);
  387. if (h->ae_algo->ops->set_timer_task)
  388. h->ae_algo->ops->set_timer_task(priv->ae_handle, true);
  389. hns3_config_xps(priv);
  390. netif_dbg(h, drv, netdev, "net open\n");
  391. return 0;
  392. }
  393. static void hns3_reset_tx_queue(struct hnae3_handle *h)
  394. {
  395. struct net_device *ndev = h->kinfo.netdev;
  396. struct hns3_nic_priv *priv = netdev_priv(ndev);
  397. struct netdev_queue *dev_queue;
  398. u32 i;
  399. for (i = 0; i < h->kinfo.num_tqps; i++) {
  400. dev_queue = netdev_get_tx_queue(ndev,
  401. priv->ring_data[i].queue_index);
  402. netdev_tx_reset_queue(dev_queue);
  403. }
  404. }
  405. static void hns3_nic_net_down(struct net_device *netdev)
  406. {
  407. struct hns3_nic_priv *priv = netdev_priv(netdev);
  408. struct hnae3_handle *h = hns3_get_handle(netdev);
  409. const struct hnae3_ae_ops *ops;
  410. int i;
  411. /* disable vectors */
  412. for (i = 0; i < priv->vector_num; i++)
  413. hns3_vector_disable(&priv->tqp_vector[i]);
  414. /* disable rcb */
  415. for (i = 0; i < h->kinfo.num_tqps; i++)
  416. hns3_tqp_disable(h->kinfo.tqp[i]);
  417. /* stop ae_dev */
  418. ops = priv->ae_handle->ae_algo->ops;
  419. if (ops->stop)
  420. ops->stop(priv->ae_handle);
  421. hns3_free_rx_cpu_rmap(netdev);
  422. /* free irq resources */
  423. hns3_nic_uninit_irq(priv);
  424. /* delay ring buffer clearing to hns3_reset_notify_uninit_enet
  425. * during reset process, because driver may not be able
  426. * to disable the ring through firmware when downing the netdev.
  427. */
  428. if (!hns3_nic_resetting(netdev))
  429. hns3_clear_all_ring(priv->ae_handle, false);
  430. hns3_reset_tx_queue(priv->ae_handle);
  431. }
  432. static int hns3_nic_net_stop(struct net_device *netdev)
  433. {
  434. struct hns3_nic_priv *priv = netdev_priv(netdev);
  435. struct hnae3_handle *h = hns3_get_handle(netdev);
  436. if (test_and_set_bit(HNS3_NIC_STATE_DOWN, &priv->state))
  437. return 0;
  438. netif_dbg(h, drv, netdev, "net stop\n");
  439. if (h->ae_algo->ops->set_timer_task)
  440. h->ae_algo->ops->set_timer_task(priv->ae_handle, false);
  441. netif_carrier_off(netdev);
  442. netif_tx_disable(netdev);
  443. hns3_nic_net_down(netdev);
  444. return 0;
  445. }
  446. static int hns3_nic_uc_sync(struct net_device *netdev,
  447. const unsigned char *addr)
  448. {
  449. struct hnae3_handle *h = hns3_get_handle(netdev);
  450. if (h->ae_algo->ops->add_uc_addr)
  451. return h->ae_algo->ops->add_uc_addr(h, addr);
  452. return 0;
  453. }
  454. static int hns3_nic_uc_unsync(struct net_device *netdev,
  455. const unsigned char *addr)
  456. {
  457. struct hnae3_handle *h = hns3_get_handle(netdev);
  458. if (h->ae_algo->ops->rm_uc_addr)
  459. return h->ae_algo->ops->rm_uc_addr(h, addr);
  460. return 0;
  461. }
  462. static int hns3_nic_mc_sync(struct net_device *netdev,
  463. const unsigned char *addr)
  464. {
  465. struct hnae3_handle *h = hns3_get_handle(netdev);
  466. if (h->ae_algo->ops->add_mc_addr)
  467. return h->ae_algo->ops->add_mc_addr(h, addr);
  468. return 0;
  469. }
  470. static int hns3_nic_mc_unsync(struct net_device *netdev,
  471. const unsigned char *addr)
  472. {
  473. struct hnae3_handle *h = hns3_get_handle(netdev);
  474. if (h->ae_algo->ops->rm_mc_addr)
  475. return h->ae_algo->ops->rm_mc_addr(h, addr);
  476. return 0;
  477. }
  478. static u8 hns3_get_netdev_flags(struct net_device *netdev)
  479. {
  480. u8 flags = 0;
  481. if (netdev->flags & IFF_PROMISC) {
  482. flags = HNAE3_USER_UPE | HNAE3_USER_MPE | HNAE3_BPE;
  483. } else {
  484. flags |= HNAE3_VLAN_FLTR;
  485. if (netdev->flags & IFF_ALLMULTI)
  486. flags |= HNAE3_USER_MPE;
  487. }
  488. return flags;
  489. }
  490. static void hns3_nic_set_rx_mode(struct net_device *netdev)
  491. {
  492. struct hnae3_handle *h = hns3_get_handle(netdev);
  493. u8 new_flags;
  494. int ret;
  495. new_flags = hns3_get_netdev_flags(netdev);
  496. ret = __dev_uc_sync(netdev, hns3_nic_uc_sync, hns3_nic_uc_unsync);
  497. if (ret) {
  498. netdev_err(netdev, "sync uc address fail\n");
  499. if (ret == -ENOSPC)
  500. new_flags |= HNAE3_OVERFLOW_UPE;
  501. }
  502. if (netdev->flags & IFF_MULTICAST) {
  503. ret = __dev_mc_sync(netdev, hns3_nic_mc_sync,
  504. hns3_nic_mc_unsync);
  505. if (ret) {
  506. netdev_err(netdev, "sync mc address fail\n");
  507. if (ret == -ENOSPC)
  508. new_flags |= HNAE3_OVERFLOW_MPE;
  509. }
  510. }
  511. /* User mode Promisc mode enable and vlan filtering is disabled to
  512. * let all packets in. MAC-VLAN Table overflow Promisc enabled and
  513. * vlan fitering is enabled
  514. */
  515. hns3_enable_vlan_filter(netdev, new_flags & HNAE3_VLAN_FLTR);
  516. h->netdev_flags = new_flags;
  517. hns3_update_promisc_mode(netdev, new_flags);
  518. }
  519. int hns3_update_promisc_mode(struct net_device *netdev, u8 promisc_flags)
  520. {
  521. struct hns3_nic_priv *priv = netdev_priv(netdev);
  522. struct hnae3_handle *h = priv->ae_handle;
  523. if (h->ae_algo->ops->set_promisc_mode) {
  524. return h->ae_algo->ops->set_promisc_mode(h,
  525. promisc_flags & HNAE3_UPE,
  526. promisc_flags & HNAE3_MPE);
  527. }
  528. return 0;
  529. }
  530. void hns3_enable_vlan_filter(struct net_device *netdev, bool enable)
  531. {
  532. struct hns3_nic_priv *priv = netdev_priv(netdev);
  533. struct hnae3_handle *h = priv->ae_handle;
  534. bool last_state;
  535. if (h->pdev->revision >= 0x21 && h->ae_algo->ops->enable_vlan_filter) {
  536. last_state = h->netdev_flags & HNAE3_VLAN_FLTR ? true : false;
  537. if (enable != last_state) {
  538. netdev_info(netdev,
  539. "%s vlan filter\n",
  540. enable ? "enable" : "disable");
  541. h->ae_algo->ops->enable_vlan_filter(h, enable);
  542. }
  543. }
  544. }
  545. static int hns3_set_tso(struct sk_buff *skb, u32 *paylen,
  546. u16 *mss, u32 *type_cs_vlan_tso)
  547. {
  548. u32 l4_offset, hdr_len;
  549. union l3_hdr_info l3;
  550. union l4_hdr_info l4;
  551. u32 l4_paylen;
  552. int ret;
  553. if (!skb_is_gso(skb))
  554. return 0;
  555. ret = skb_cow_head(skb, 0);
  556. if (unlikely(ret))
  557. return ret;
  558. l3.hdr = skb_network_header(skb);
  559. l4.hdr = skb_transport_header(skb);
  560. /* Software should clear the IPv4's checksum field when tso is
  561. * needed.
  562. */
  563. if (l3.v4->version == 4)
  564. l3.v4->check = 0;
  565. /* tunnel packet */
  566. if (skb_shinfo(skb)->gso_type & (SKB_GSO_GRE |
  567. SKB_GSO_GRE_CSUM |
  568. SKB_GSO_UDP_TUNNEL |
  569. SKB_GSO_UDP_TUNNEL_CSUM)) {
  570. if ((!(skb_shinfo(skb)->gso_type &
  571. SKB_GSO_PARTIAL)) &&
  572. (skb_shinfo(skb)->gso_type &
  573. SKB_GSO_UDP_TUNNEL_CSUM)) {
  574. /* Software should clear the udp's checksum
  575. * field when tso is needed.
  576. */
  577. l4.udp->check = 0;
  578. }
  579. /* reset l3&l4 pointers from outer to inner headers */
  580. l3.hdr = skb_inner_network_header(skb);
  581. l4.hdr = skb_inner_transport_header(skb);
  582. /* Software should clear the IPv4's checksum field when
  583. * tso is needed.
  584. */
  585. if (l3.v4->version == 4)
  586. l3.v4->check = 0;
  587. }
  588. /* normal or tunnel packet */
  589. l4_offset = l4.hdr - skb->data;
  590. hdr_len = (l4.tcp->doff << 2) + l4_offset;
  591. /* remove payload length from inner pseudo checksum when tso */
  592. l4_paylen = skb->len - l4_offset;
  593. csum_replace_by_diff(&l4.tcp->check,
  594. (__force __wsum)htonl(l4_paylen));
  595. /* find the txbd field values */
  596. *paylen = skb->len - hdr_len;
  597. hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_TSO_B, 1);
  598. /* get MSS for TSO */
  599. *mss = skb_shinfo(skb)->gso_size;
  600. return 0;
  601. }
  602. static int hns3_get_l4_protocol(struct sk_buff *skb, u8 *ol4_proto,
  603. u8 *il4_proto)
  604. {
  605. union l3_hdr_info l3;
  606. unsigned char *l4_hdr;
  607. unsigned char *exthdr;
  608. u8 l4_proto_tmp;
  609. __be16 frag_off;
  610. /* find outer header point */
  611. l3.hdr = skb_network_header(skb);
  612. l4_hdr = skb_transport_header(skb);
  613. if (skb->protocol == htons(ETH_P_IPV6)) {
  614. exthdr = l3.hdr + sizeof(*l3.v6);
  615. l4_proto_tmp = l3.v6->nexthdr;
  616. if (l4_hdr != exthdr)
  617. ipv6_skip_exthdr(skb, exthdr - skb->data,
  618. &l4_proto_tmp, &frag_off);
  619. } else if (skb->protocol == htons(ETH_P_IP)) {
  620. l4_proto_tmp = l3.v4->protocol;
  621. } else {
  622. return -EINVAL;
  623. }
  624. *ol4_proto = l4_proto_tmp;
  625. /* tunnel packet */
  626. if (!skb->encapsulation) {
  627. *il4_proto = 0;
  628. return 0;
  629. }
  630. /* find inner header point */
  631. l3.hdr = skb_inner_network_header(skb);
  632. l4_hdr = skb_inner_transport_header(skb);
  633. if (l3.v6->version == 6) {
  634. exthdr = l3.hdr + sizeof(*l3.v6);
  635. l4_proto_tmp = l3.v6->nexthdr;
  636. if (l4_hdr != exthdr)
  637. ipv6_skip_exthdr(skb, exthdr - skb->data,
  638. &l4_proto_tmp, &frag_off);
  639. } else if (l3.v4->version == 4) {
  640. l4_proto_tmp = l3.v4->protocol;
  641. }
  642. *il4_proto = l4_proto_tmp;
  643. return 0;
  644. }
  645. /* when skb->encapsulation is 0, skb->ip_summed is CHECKSUM_PARTIAL
  646. * and it is udp packet, which has a dest port as the IANA assigned.
  647. * the hardware is expected to do the checksum offload, but the
  648. * hardware will not do the checksum offload when udp dest port is
  649. * 4789, 4790 or 6081.
  650. */
  651. static bool hns3_tunnel_csum_bug(struct sk_buff *skb)
  652. {
  653. union l4_hdr_info l4;
  654. l4.hdr = skb_transport_header(skb);
  655. if (!(!skb->encapsulation &&
  656. (l4.udp->dest == htons(IANA_VXLAN_UDP_PORT) ||
  657. l4.udp->dest == htons(GENEVE_UDP_PORT) ||
  658. l4.udp->dest == htons(4790))))
  659. return false;
  660. return true;
  661. }
  662. static void hns3_set_outer_l2l3l4(struct sk_buff *skb, u8 ol4_proto,
  663. u32 *ol_type_vlan_len_msec)
  664. {
  665. u32 l2_len, l3_len, l4_len;
  666. unsigned char *il2_hdr;
  667. union l3_hdr_info l3;
  668. union l4_hdr_info l4;
  669. l3.hdr = skb_network_header(skb);
  670. l4.hdr = skb_transport_header(skb);
  671. /* compute OL2 header size, defined in 2 Bytes */
  672. l2_len = l3.hdr - skb->data;
  673. hns3_set_field(*ol_type_vlan_len_msec, HNS3_TXD_L2LEN_S, l2_len >> 1);
  674. /* compute OL3 header size, defined in 4 Bytes */
  675. l3_len = l4.hdr - l3.hdr;
  676. hns3_set_field(*ol_type_vlan_len_msec, HNS3_TXD_L3LEN_S, l3_len >> 2);
  677. il2_hdr = skb_inner_mac_header(skb);
  678. /* compute OL4 header size, defined in 4 Bytes */
  679. l4_len = il2_hdr - l4.hdr;
  680. hns3_set_field(*ol_type_vlan_len_msec, HNS3_TXD_L4LEN_S, l4_len >> 2);
  681. /* define outer network header type */
  682. if (skb->protocol == htons(ETH_P_IP)) {
  683. if (skb_is_gso(skb))
  684. hns3_set_field(*ol_type_vlan_len_msec,
  685. HNS3_TXD_OL3T_S,
  686. HNS3_OL3T_IPV4_CSUM);
  687. else
  688. hns3_set_field(*ol_type_vlan_len_msec,
  689. HNS3_TXD_OL3T_S,
  690. HNS3_OL3T_IPV4_NO_CSUM);
  691. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  692. hns3_set_field(*ol_type_vlan_len_msec, HNS3_TXD_OL3T_S,
  693. HNS3_OL3T_IPV6);
  694. }
  695. if (ol4_proto == IPPROTO_UDP)
  696. hns3_set_field(*ol_type_vlan_len_msec, HNS3_TXD_TUNTYPE_S,
  697. HNS3_TUN_MAC_IN_UDP);
  698. else if (ol4_proto == IPPROTO_GRE)
  699. hns3_set_field(*ol_type_vlan_len_msec, HNS3_TXD_TUNTYPE_S,
  700. HNS3_TUN_NVGRE);
  701. }
  702. static int hns3_set_l2l3l4(struct sk_buff *skb, u8 ol4_proto,
  703. u8 il4_proto, u32 *type_cs_vlan_tso,
  704. u32 *ol_type_vlan_len_msec)
  705. {
  706. unsigned char *l2_hdr = skb->data;
  707. u32 l4_proto = ol4_proto;
  708. union l4_hdr_info l4;
  709. union l3_hdr_info l3;
  710. u32 l2_len, l3_len;
  711. l4.hdr = skb_transport_header(skb);
  712. l3.hdr = skb_network_header(skb);
  713. /* handle encapsulation skb */
  714. if (skb->encapsulation) {
  715. /* If this is a not UDP/GRE encapsulation skb */
  716. if (!(ol4_proto == IPPROTO_UDP || ol4_proto == IPPROTO_GRE)) {
  717. /* drop the skb tunnel packet if hardware don't support,
  718. * because hardware can't calculate csum when TSO.
  719. */
  720. if (skb_is_gso(skb))
  721. return -EDOM;
  722. /* the stack computes the IP header already,
  723. * driver calculate l4 checksum when not TSO.
  724. */
  725. return skb_checksum_help(skb);
  726. }
  727. hns3_set_outer_l2l3l4(skb, ol4_proto, ol_type_vlan_len_msec);
  728. /* switch to inner header */
  729. l2_hdr = skb_inner_mac_header(skb);
  730. l3.hdr = skb_inner_network_header(skb);
  731. l4.hdr = skb_inner_transport_header(skb);
  732. l4_proto = il4_proto;
  733. }
  734. if (l3.v4->version == 4) {
  735. hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L3T_S,
  736. HNS3_L3T_IPV4);
  737. /* the stack computes the IP header already, the only time we
  738. * need the hardware to recompute it is in the case of TSO.
  739. */
  740. if (skb_is_gso(skb))
  741. hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L3CS_B, 1);
  742. } else if (l3.v6->version == 6) {
  743. hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L3T_S,
  744. HNS3_L3T_IPV6);
  745. }
  746. /* compute inner(/normal) L2 header size, defined in 2 Bytes */
  747. l2_len = l3.hdr - l2_hdr;
  748. hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L2LEN_S, l2_len >> 1);
  749. /* compute inner(/normal) L3 header size, defined in 4 Bytes */
  750. l3_len = l4.hdr - l3.hdr;
  751. hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L3LEN_S, l3_len >> 2);
  752. /* compute inner(/normal) L4 header size, defined in 4 Bytes */
  753. switch (l4_proto) {
  754. case IPPROTO_TCP:
  755. hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L4CS_B, 1);
  756. hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L4T_S,
  757. HNS3_L4T_TCP);
  758. hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L4LEN_S,
  759. l4.tcp->doff);
  760. break;
  761. case IPPROTO_UDP:
  762. if (hns3_tunnel_csum_bug(skb)) {
  763. int ret = skb_put_padto(skb, HNS3_MIN_TUN_PKT_LEN);
  764. return ret ? ret : skb_checksum_help(skb);
  765. }
  766. hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L4CS_B, 1);
  767. hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L4T_S,
  768. HNS3_L4T_UDP);
  769. hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L4LEN_S,
  770. (sizeof(struct udphdr) >> 2));
  771. break;
  772. case IPPROTO_SCTP:
  773. hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L4CS_B, 1);
  774. hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L4T_S,
  775. HNS3_L4T_SCTP);
  776. hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L4LEN_S,
  777. (sizeof(struct sctphdr) >> 2));
  778. break;
  779. default:
  780. /* drop the skb tunnel packet if hardware don't support,
  781. * because hardware can't calculate csum when TSO.
  782. */
  783. if (skb_is_gso(skb))
  784. return -EDOM;
  785. /* the stack computes the IP header already,
  786. * driver calculate l4 checksum when not TSO.
  787. */
  788. return skb_checksum_help(skb);
  789. }
  790. return 0;
  791. }
  792. static void hns3_set_txbd_baseinfo(u16 *bdtp_fe_sc_vld_ra_ri, int frag_end)
  793. {
  794. /* Config bd buffer end */
  795. if (!!frag_end)
  796. hns3_set_field(*bdtp_fe_sc_vld_ra_ri, HNS3_TXD_FE_B, 1U);
  797. hns3_set_field(*bdtp_fe_sc_vld_ra_ri, HNS3_TXD_VLD_B, 1U);
  798. }
  799. static int hns3_handle_vtags(struct hns3_enet_ring *tx_ring,
  800. struct sk_buff *skb)
  801. {
  802. struct hnae3_handle *handle = tx_ring->tqp->handle;
  803. struct vlan_ethhdr *vhdr;
  804. int rc;
  805. if (!(skb->protocol == htons(ETH_P_8021Q) ||
  806. skb_vlan_tag_present(skb)))
  807. return 0;
  808. /* Since HW limitation, if port based insert VLAN enabled, only one VLAN
  809. * header is allowed in skb, otherwise it will cause RAS error.
  810. */
  811. if (unlikely(skb_vlan_tagged_multi(skb) &&
  812. handle->port_base_vlan_state ==
  813. HNAE3_PORT_BASE_VLAN_ENABLE))
  814. return -EINVAL;
  815. if (skb->protocol == htons(ETH_P_8021Q) &&
  816. !(handle->kinfo.netdev->features & NETIF_F_HW_VLAN_CTAG_TX)) {
  817. /* When HW VLAN acceleration is turned off, and the stack
  818. * sets the protocol to 802.1q, the driver just need to
  819. * set the protocol to the encapsulated ethertype.
  820. */
  821. skb->protocol = vlan_get_protocol(skb);
  822. return 0;
  823. }
  824. if (skb_vlan_tag_present(skb)) {
  825. /* Based on hw strategy, use out_vtag in two layer tag case,
  826. * and use inner_vtag in one tag case.
  827. */
  828. if (skb->protocol == htons(ETH_P_8021Q) &&
  829. handle->port_base_vlan_state ==
  830. HNAE3_PORT_BASE_VLAN_DISABLE)
  831. rc = HNS3_OUTER_VLAN_TAG;
  832. else
  833. rc = HNS3_INNER_VLAN_TAG;
  834. skb->protocol = vlan_get_protocol(skb);
  835. return rc;
  836. }
  837. rc = skb_cow_head(skb, 0);
  838. if (unlikely(rc < 0))
  839. return rc;
  840. vhdr = (struct vlan_ethhdr *)skb->data;
  841. vhdr->h_vlan_TCI |= cpu_to_be16((skb->priority << VLAN_PRIO_SHIFT)
  842. & VLAN_PRIO_MASK);
  843. skb->protocol = vlan_get_protocol(skb);
  844. return 0;
  845. }
  846. static int hns3_fill_skb_desc(struct hns3_enet_ring *ring,
  847. struct sk_buff *skb, struct hns3_desc *desc)
  848. {
  849. u32 ol_type_vlan_len_msec = 0;
  850. u32 type_cs_vlan_tso = 0;
  851. u32 paylen = skb->len;
  852. u16 inner_vtag = 0;
  853. u16 out_vtag = 0;
  854. u16 mss = 0;
  855. int ret;
  856. ret = hns3_handle_vtags(ring, skb);
  857. if (unlikely(ret < 0)) {
  858. u64_stats_update_begin(&ring->syncp);
  859. ring->stats.tx_vlan_err++;
  860. u64_stats_update_end(&ring->syncp);
  861. return ret;
  862. } else if (ret == HNS3_INNER_VLAN_TAG) {
  863. inner_vtag = skb_vlan_tag_get(skb);
  864. inner_vtag |= (skb->priority << VLAN_PRIO_SHIFT) &
  865. VLAN_PRIO_MASK;
  866. hns3_set_field(type_cs_vlan_tso, HNS3_TXD_VLAN_B, 1);
  867. } else if (ret == HNS3_OUTER_VLAN_TAG) {
  868. out_vtag = skb_vlan_tag_get(skb);
  869. out_vtag |= (skb->priority << VLAN_PRIO_SHIFT) &
  870. VLAN_PRIO_MASK;
  871. hns3_set_field(ol_type_vlan_len_msec, HNS3_TXD_OVLAN_B,
  872. 1);
  873. }
  874. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  875. u8 ol4_proto, il4_proto;
  876. skb_reset_mac_len(skb);
  877. ret = hns3_get_l4_protocol(skb, &ol4_proto, &il4_proto);
  878. if (unlikely(ret)) {
  879. u64_stats_update_begin(&ring->syncp);
  880. ring->stats.tx_l4_proto_err++;
  881. u64_stats_update_end(&ring->syncp);
  882. return ret;
  883. }
  884. ret = hns3_set_l2l3l4(skb, ol4_proto, il4_proto,
  885. &type_cs_vlan_tso,
  886. &ol_type_vlan_len_msec);
  887. if (unlikely(ret)) {
  888. u64_stats_update_begin(&ring->syncp);
  889. ring->stats.tx_l2l3l4_err++;
  890. u64_stats_update_end(&ring->syncp);
  891. return ret;
  892. }
  893. ret = hns3_set_tso(skb, &paylen, &mss,
  894. &type_cs_vlan_tso);
  895. if (unlikely(ret)) {
  896. u64_stats_update_begin(&ring->syncp);
  897. ring->stats.tx_tso_err++;
  898. u64_stats_update_end(&ring->syncp);
  899. return ret;
  900. }
  901. }
  902. /* Set txbd */
  903. desc->tx.ol_type_vlan_len_msec =
  904. cpu_to_le32(ol_type_vlan_len_msec);
  905. desc->tx.type_cs_vlan_tso_len = cpu_to_le32(type_cs_vlan_tso);
  906. desc->tx.paylen = cpu_to_le32(paylen);
  907. desc->tx.mss = cpu_to_le16(mss);
  908. desc->tx.vlan_tag = cpu_to_le16(inner_vtag);
  909. desc->tx.outer_vlan_tag = cpu_to_le16(out_vtag);
  910. return 0;
  911. }
  912. static int hns3_fill_desc(struct hns3_enet_ring *ring, void *priv,
  913. unsigned int size, int frag_end,
  914. enum hns_desc_type type)
  915. {
  916. struct hns3_desc_cb *desc_cb = &ring->desc_cb[ring->next_to_use];
  917. struct hns3_desc *desc = &ring->desc[ring->next_to_use];
  918. struct device *dev = ring_to_dev(ring);
  919. skb_frag_t *frag;
  920. unsigned int frag_buf_num;
  921. int k, sizeoflast;
  922. dma_addr_t dma;
  923. if (type == DESC_TYPE_SKB) {
  924. struct sk_buff *skb = (struct sk_buff *)priv;
  925. int ret;
  926. ret = hns3_fill_skb_desc(ring, skb, desc);
  927. if (unlikely(ret))
  928. return ret;
  929. dma = dma_map_single(dev, skb->data, size, DMA_TO_DEVICE);
  930. } else {
  931. frag = (skb_frag_t *)priv;
  932. dma = skb_frag_dma_map(dev, frag, 0, size, DMA_TO_DEVICE);
  933. }
  934. if (unlikely(dma_mapping_error(dev, dma))) {
  935. u64_stats_update_begin(&ring->syncp);
  936. ring->stats.sw_err_cnt++;
  937. u64_stats_update_end(&ring->syncp);
  938. return -ENOMEM;
  939. }
  940. desc_cb->length = size;
  941. if (likely(size <= HNS3_MAX_BD_SIZE)) {
  942. u16 bdtp_fe_sc_vld_ra_ri = 0;
  943. desc_cb->priv = priv;
  944. desc_cb->dma = dma;
  945. desc_cb->type = type;
  946. desc->addr = cpu_to_le64(dma);
  947. desc->tx.send_size = cpu_to_le16(size);
  948. hns3_set_txbd_baseinfo(&bdtp_fe_sc_vld_ra_ri, frag_end);
  949. desc->tx.bdtp_fe_sc_vld_ra_ri =
  950. cpu_to_le16(bdtp_fe_sc_vld_ra_ri);
  951. ring_ptr_move_fw(ring, next_to_use);
  952. return 0;
  953. }
  954. frag_buf_num = hns3_tx_bd_count(size);
  955. sizeoflast = size & HNS3_TX_LAST_SIZE_M;
  956. sizeoflast = sizeoflast ? sizeoflast : HNS3_MAX_BD_SIZE;
  957. /* When frag size is bigger than hardware limit, split this frag */
  958. for (k = 0; k < frag_buf_num; k++) {
  959. u16 bdtp_fe_sc_vld_ra_ri = 0;
  960. /* The txbd's baseinfo of DESC_TYPE_PAGE & DESC_TYPE_SKB */
  961. desc_cb->priv = priv;
  962. desc_cb->dma = dma + HNS3_MAX_BD_SIZE * k;
  963. desc_cb->type = (type == DESC_TYPE_SKB && !k) ?
  964. DESC_TYPE_SKB : DESC_TYPE_PAGE;
  965. /* now, fill the descriptor */
  966. desc->addr = cpu_to_le64(dma + HNS3_MAX_BD_SIZE * k);
  967. desc->tx.send_size = cpu_to_le16((k == frag_buf_num - 1) ?
  968. (u16)sizeoflast : (u16)HNS3_MAX_BD_SIZE);
  969. hns3_set_txbd_baseinfo(&bdtp_fe_sc_vld_ra_ri,
  970. frag_end && (k == frag_buf_num - 1) ?
  971. 1 : 0);
  972. desc->tx.bdtp_fe_sc_vld_ra_ri =
  973. cpu_to_le16(bdtp_fe_sc_vld_ra_ri);
  974. /* move ring pointer to next */
  975. ring_ptr_move_fw(ring, next_to_use);
  976. desc_cb = &ring->desc_cb[ring->next_to_use];
  977. desc = &ring->desc[ring->next_to_use];
  978. }
  979. return 0;
  980. }
  981. static unsigned int hns3_nic_bd_num(struct sk_buff *skb)
  982. {
  983. unsigned int bd_num;
  984. int i;
  985. /* if the total len is within the max bd limit */
  986. if (likely(skb->len <= HNS3_MAX_BD_SIZE))
  987. return skb_shinfo(skb)->nr_frags + 1;
  988. bd_num = hns3_tx_bd_count(skb_headlen(skb));
  989. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  990. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  991. bd_num += hns3_tx_bd_count(skb_frag_size(frag));
  992. }
  993. return bd_num;
  994. }
  995. static unsigned int hns3_gso_hdr_len(struct sk_buff *skb)
  996. {
  997. if (!skb->encapsulation)
  998. return skb_transport_offset(skb) + tcp_hdrlen(skb);
  999. return skb_inner_transport_offset(skb) + inner_tcp_hdrlen(skb);
  1000. }
  1001. /* HW need every continuous 8 buffer data to be larger than MSS,
  1002. * we simplify it by ensuring skb_headlen + the first continuous
  1003. * 7 frags to to be larger than gso header len + mss, and the remaining
  1004. * continuous 7 frags to be larger than MSS except the last 7 frags.
  1005. */
  1006. static bool hns3_skb_need_linearized(struct sk_buff *skb)
  1007. {
  1008. int bd_limit = HNS3_MAX_BD_NUM_NORMAL - 1;
  1009. unsigned int tot_len = 0;
  1010. int i;
  1011. for (i = 0; i < bd_limit; i++)
  1012. tot_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1013. /* ensure headlen + the first 7 frags is greater than mss + header
  1014. * and the first 7 frags is greater than mss.
  1015. */
  1016. if (((tot_len + skb_headlen(skb)) < (skb_shinfo(skb)->gso_size +
  1017. hns3_gso_hdr_len(skb))) || (tot_len < skb_shinfo(skb)->gso_size))
  1018. return true;
  1019. /* ensure the remaining continuous 7 buffer is greater than mss */
  1020. for (i = 0; i < (skb_shinfo(skb)->nr_frags - bd_limit - 1); i++) {
  1021. tot_len -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1022. tot_len += skb_frag_size(&skb_shinfo(skb)->frags[i + bd_limit]);
  1023. if (tot_len < skb_shinfo(skb)->gso_size)
  1024. return true;
  1025. }
  1026. return false;
  1027. }
  1028. static int hns3_nic_maybe_stop_tx(struct hns3_enet_ring *ring,
  1029. struct sk_buff **out_skb)
  1030. {
  1031. struct sk_buff *skb = *out_skb;
  1032. unsigned int bd_num;
  1033. bd_num = hns3_nic_bd_num(skb);
  1034. if (unlikely(bd_num > HNS3_MAX_BD_NUM_NORMAL)) {
  1035. struct sk_buff *new_skb;
  1036. if (skb_is_gso(skb) && bd_num <= HNS3_MAX_BD_NUM_TSO &&
  1037. !hns3_skb_need_linearized(skb))
  1038. goto out;
  1039. /* manual split the send packet */
  1040. new_skb = skb_copy(skb, GFP_ATOMIC);
  1041. if (!new_skb)
  1042. return -ENOMEM;
  1043. dev_kfree_skb_any(skb);
  1044. *out_skb = new_skb;
  1045. bd_num = hns3_nic_bd_num(new_skb);
  1046. if ((skb_is_gso(new_skb) && bd_num > HNS3_MAX_BD_NUM_TSO) ||
  1047. (!skb_is_gso(new_skb) && bd_num > HNS3_MAX_BD_NUM_NORMAL))
  1048. return -ENOMEM;
  1049. u64_stats_update_begin(&ring->syncp);
  1050. ring->stats.tx_copy++;
  1051. u64_stats_update_end(&ring->syncp);
  1052. }
  1053. out:
  1054. if (unlikely(ring_space(ring) < bd_num))
  1055. return -EBUSY;
  1056. return bd_num;
  1057. }
  1058. static void hns3_clear_desc(struct hns3_enet_ring *ring, int next_to_use_orig)
  1059. {
  1060. struct device *dev = ring_to_dev(ring);
  1061. unsigned int i;
  1062. for (i = 0; i < ring->desc_num; i++) {
  1063. struct hns3_desc *desc = &ring->desc[ring->next_to_use];
  1064. memset(desc, 0, sizeof(*desc));
  1065. /* check if this is where we started */
  1066. if (ring->next_to_use == next_to_use_orig)
  1067. break;
  1068. /* rollback one */
  1069. ring_ptr_move_bw(ring, next_to_use);
  1070. if (!ring->desc_cb[ring->next_to_use].dma)
  1071. continue;
  1072. /* unmap the descriptor dma address */
  1073. if (ring->desc_cb[ring->next_to_use].type == DESC_TYPE_SKB)
  1074. dma_unmap_single(dev,
  1075. ring->desc_cb[ring->next_to_use].dma,
  1076. ring->desc_cb[ring->next_to_use].length,
  1077. DMA_TO_DEVICE);
  1078. else if (ring->desc_cb[ring->next_to_use].length)
  1079. dma_unmap_page(dev,
  1080. ring->desc_cb[ring->next_to_use].dma,
  1081. ring->desc_cb[ring->next_to_use].length,
  1082. DMA_TO_DEVICE);
  1083. ring->desc_cb[ring->next_to_use].length = 0;
  1084. ring->desc_cb[ring->next_to_use].dma = 0;
  1085. ring->desc_cb[ring->next_to_use].type = DESC_TYPE_UNKNOWN;
  1086. }
  1087. }
  1088. netdev_tx_t hns3_nic_net_xmit(struct sk_buff *skb, struct net_device *netdev)
  1089. {
  1090. struct hns3_nic_priv *priv = netdev_priv(netdev);
  1091. struct hns3_nic_ring_data *ring_data =
  1092. &tx_ring_data(priv, skb->queue_mapping);
  1093. struct hns3_enet_ring *ring = ring_data->ring;
  1094. struct netdev_queue *dev_queue;
  1095. skb_frag_t *frag;
  1096. int next_to_use_head;
  1097. int buf_num;
  1098. int seg_num;
  1099. int size;
  1100. int ret;
  1101. int i;
  1102. /* Hardware can only handle short frames above 32 bytes */
  1103. if (skb_put_padto(skb, HNS3_MIN_TX_LEN))
  1104. return NETDEV_TX_OK;
  1105. /* Prefetch the data used later */
  1106. prefetch(skb->data);
  1107. buf_num = hns3_nic_maybe_stop_tx(ring, &skb);
  1108. if (unlikely(buf_num <= 0)) {
  1109. if (buf_num == -EBUSY) {
  1110. u64_stats_update_begin(&ring->syncp);
  1111. ring->stats.tx_busy++;
  1112. u64_stats_update_end(&ring->syncp);
  1113. goto out_net_tx_busy;
  1114. } else if (buf_num == -ENOMEM) {
  1115. u64_stats_update_begin(&ring->syncp);
  1116. ring->stats.sw_err_cnt++;
  1117. u64_stats_update_end(&ring->syncp);
  1118. }
  1119. hns3_rl_err(netdev, "xmit error: %d!\n", buf_num);
  1120. goto out_err_tx_ok;
  1121. }
  1122. /* No. of segments (plus a header) */
  1123. seg_num = skb_shinfo(skb)->nr_frags + 1;
  1124. /* Fill the first part */
  1125. size = skb_headlen(skb);
  1126. next_to_use_head = ring->next_to_use;
  1127. ret = hns3_fill_desc(ring, skb, size, seg_num == 1 ? 1 : 0,
  1128. DESC_TYPE_SKB);
  1129. if (unlikely(ret))
  1130. goto fill_err;
  1131. /* Fill the fragments */
  1132. for (i = 1; i < seg_num; i++) {
  1133. frag = &skb_shinfo(skb)->frags[i - 1];
  1134. size = skb_frag_size(frag);
  1135. ret = hns3_fill_desc(ring, frag, size,
  1136. seg_num - 1 == i ? 1 : 0,
  1137. DESC_TYPE_PAGE);
  1138. if (unlikely(ret))
  1139. goto fill_err;
  1140. }
  1141. /* Complete translate all packets */
  1142. dev_queue = netdev_get_tx_queue(netdev, ring_data->queue_index);
  1143. netdev_tx_sent_queue(dev_queue, skb->len);
  1144. wmb(); /* Commit all data before submit */
  1145. hnae3_queue_xmit(ring->tqp, buf_num);
  1146. return NETDEV_TX_OK;
  1147. fill_err:
  1148. hns3_clear_desc(ring, next_to_use_head);
  1149. out_err_tx_ok:
  1150. dev_kfree_skb_any(skb);
  1151. return NETDEV_TX_OK;
  1152. out_net_tx_busy:
  1153. netif_stop_subqueue(netdev, ring_data->queue_index);
  1154. smp_mb(); /* Commit all data before submit */
  1155. return NETDEV_TX_BUSY;
  1156. }
  1157. static int hns3_nic_net_set_mac_address(struct net_device *netdev, void *p)
  1158. {
  1159. struct hnae3_handle *h = hns3_get_handle(netdev);
  1160. struct sockaddr *mac_addr = p;
  1161. int ret;
  1162. if (!mac_addr || !is_valid_ether_addr((const u8 *)mac_addr->sa_data))
  1163. return -EADDRNOTAVAIL;
  1164. if (ether_addr_equal(netdev->dev_addr, mac_addr->sa_data)) {
  1165. netdev_info(netdev, "already using mac address %pM\n",
  1166. mac_addr->sa_data);
  1167. return 0;
  1168. }
  1169. ret = h->ae_algo->ops->set_mac_addr(h, mac_addr->sa_data, false);
  1170. if (ret) {
  1171. netdev_err(netdev, "set_mac_address fail, ret=%d!\n", ret);
  1172. return ret;
  1173. }
  1174. ether_addr_copy(netdev->dev_addr, mac_addr->sa_data);
  1175. return 0;
  1176. }
  1177. static int hns3_nic_do_ioctl(struct net_device *netdev,
  1178. struct ifreq *ifr, int cmd)
  1179. {
  1180. struct hnae3_handle *h = hns3_get_handle(netdev);
  1181. if (!netif_running(netdev))
  1182. return -EINVAL;
  1183. if (!h->ae_algo->ops->do_ioctl)
  1184. return -EOPNOTSUPP;
  1185. return h->ae_algo->ops->do_ioctl(h, ifr, cmd);
  1186. }
  1187. static int hns3_nic_set_features(struct net_device *netdev,
  1188. netdev_features_t features)
  1189. {
  1190. netdev_features_t changed = netdev->features ^ features;
  1191. struct hns3_nic_priv *priv = netdev_priv(netdev);
  1192. struct hnae3_handle *h = priv->ae_handle;
  1193. bool enable;
  1194. int ret;
  1195. if (changed & (NETIF_F_GRO_HW) && h->ae_algo->ops->set_gro_en) {
  1196. enable = !!(features & NETIF_F_GRO_HW);
  1197. ret = h->ae_algo->ops->set_gro_en(h, enable);
  1198. if (ret)
  1199. return ret;
  1200. }
  1201. if ((changed & NETIF_F_HW_VLAN_CTAG_FILTER) &&
  1202. h->ae_algo->ops->enable_vlan_filter) {
  1203. enable = !!(features & NETIF_F_HW_VLAN_CTAG_FILTER);
  1204. h->ae_algo->ops->enable_vlan_filter(h, enable);
  1205. }
  1206. if ((changed & NETIF_F_HW_VLAN_CTAG_RX) &&
  1207. h->ae_algo->ops->enable_hw_strip_rxvtag) {
  1208. enable = !!(features & NETIF_F_HW_VLAN_CTAG_RX);
  1209. ret = h->ae_algo->ops->enable_hw_strip_rxvtag(h, enable);
  1210. if (ret)
  1211. return ret;
  1212. }
  1213. if ((changed & NETIF_F_NTUPLE) && h->ae_algo->ops->enable_fd) {
  1214. enable = !!(features & NETIF_F_NTUPLE);
  1215. h->ae_algo->ops->enable_fd(h, enable);
  1216. }
  1217. netdev->features = features;
  1218. return 0;
  1219. }
  1220. static void hns3_nic_get_stats64(struct net_device *netdev,
  1221. struct rtnl_link_stats64 *stats)
  1222. {
  1223. struct hns3_nic_priv *priv = netdev_priv(netdev);
  1224. int queue_num = priv->ae_handle->kinfo.num_tqps;
  1225. struct hnae3_handle *handle = priv->ae_handle;
  1226. struct hns3_enet_ring *ring;
  1227. u64 rx_length_errors = 0;
  1228. u64 rx_crc_errors = 0;
  1229. u64 rx_multicast = 0;
  1230. unsigned int start;
  1231. u64 tx_errors = 0;
  1232. u64 rx_errors = 0;
  1233. unsigned int idx;
  1234. u64 tx_bytes = 0;
  1235. u64 rx_bytes = 0;
  1236. u64 tx_pkts = 0;
  1237. u64 rx_pkts = 0;
  1238. u64 tx_drop = 0;
  1239. u64 rx_drop = 0;
  1240. if (test_bit(HNS3_NIC_STATE_DOWN, &priv->state))
  1241. return;
  1242. handle->ae_algo->ops->update_stats(handle, &netdev->stats);
  1243. for (idx = 0; idx < queue_num; idx++) {
  1244. /* fetch the tx stats */
  1245. ring = priv->ring_data[idx].ring;
  1246. do {
  1247. start = u64_stats_fetch_begin_irq(&ring->syncp);
  1248. tx_bytes += ring->stats.tx_bytes;
  1249. tx_pkts += ring->stats.tx_pkts;
  1250. tx_drop += ring->stats.sw_err_cnt;
  1251. tx_drop += ring->stats.tx_vlan_err;
  1252. tx_drop += ring->stats.tx_l4_proto_err;
  1253. tx_drop += ring->stats.tx_l2l3l4_err;
  1254. tx_drop += ring->stats.tx_tso_err;
  1255. tx_errors += ring->stats.sw_err_cnt;
  1256. tx_errors += ring->stats.tx_vlan_err;
  1257. tx_errors += ring->stats.tx_l4_proto_err;
  1258. tx_errors += ring->stats.tx_l2l3l4_err;
  1259. tx_errors += ring->stats.tx_tso_err;
  1260. } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
  1261. /* fetch the rx stats */
  1262. ring = priv->ring_data[idx + queue_num].ring;
  1263. do {
  1264. start = u64_stats_fetch_begin_irq(&ring->syncp);
  1265. rx_bytes += ring->stats.rx_bytes;
  1266. rx_pkts += ring->stats.rx_pkts;
  1267. rx_drop += ring->stats.l2_err;
  1268. rx_errors += ring->stats.l2_err;
  1269. rx_errors += ring->stats.l3l4_csum_err;
  1270. rx_crc_errors += ring->stats.l2_err;
  1271. rx_multicast += ring->stats.rx_multicast;
  1272. rx_length_errors += ring->stats.err_pkt_len;
  1273. } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
  1274. }
  1275. stats->tx_bytes = tx_bytes;
  1276. stats->tx_packets = tx_pkts;
  1277. stats->rx_bytes = rx_bytes;
  1278. stats->rx_packets = rx_pkts;
  1279. stats->rx_errors = rx_errors;
  1280. stats->multicast = rx_multicast;
  1281. stats->rx_length_errors = rx_length_errors;
  1282. stats->rx_crc_errors = rx_crc_errors;
  1283. stats->rx_missed_errors = netdev->stats.rx_missed_errors;
  1284. stats->tx_errors = tx_errors;
  1285. stats->rx_dropped = rx_drop;
  1286. stats->tx_dropped = tx_drop;
  1287. stats->collisions = netdev->stats.collisions;
  1288. stats->rx_over_errors = netdev->stats.rx_over_errors;
  1289. stats->rx_frame_errors = netdev->stats.rx_frame_errors;
  1290. stats->rx_fifo_errors = netdev->stats.rx_fifo_errors;
  1291. stats->tx_aborted_errors = netdev->stats.tx_aborted_errors;
  1292. stats->tx_carrier_errors = netdev->stats.tx_carrier_errors;
  1293. stats->tx_fifo_errors = netdev->stats.tx_fifo_errors;
  1294. stats->tx_heartbeat_errors = netdev->stats.tx_heartbeat_errors;
  1295. stats->tx_window_errors = netdev->stats.tx_window_errors;
  1296. stats->rx_compressed = netdev->stats.rx_compressed;
  1297. stats->tx_compressed = netdev->stats.tx_compressed;
  1298. }
  1299. static int hns3_setup_tc(struct net_device *netdev, void *type_data)
  1300. {
  1301. struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
  1302. u8 *prio_tc = mqprio_qopt->qopt.prio_tc_map;
  1303. struct hnae3_knic_private_info *kinfo;
  1304. u8 tc = mqprio_qopt->qopt.num_tc;
  1305. u16 mode = mqprio_qopt->mode;
  1306. u8 hw = mqprio_qopt->qopt.hw;
  1307. struct hnae3_handle *h;
  1308. if (!((hw == TC_MQPRIO_HW_OFFLOAD_TCS &&
  1309. mode == TC_MQPRIO_MODE_CHANNEL) || (!hw && tc == 0)))
  1310. return -EOPNOTSUPP;
  1311. if (tc > HNAE3_MAX_TC)
  1312. return -EINVAL;
  1313. if (!netdev)
  1314. return -EINVAL;
  1315. h = hns3_get_handle(netdev);
  1316. kinfo = &h->kinfo;
  1317. netif_dbg(h, drv, netdev, "setup tc: num_tc=%u\n", tc);
  1318. return (kinfo->dcb_ops && kinfo->dcb_ops->setup_tc) ?
  1319. kinfo->dcb_ops->setup_tc(h, tc ? tc : 1, prio_tc) : -EOPNOTSUPP;
  1320. }
  1321. static int hns3_nic_setup_tc(struct net_device *dev, enum tc_setup_type type,
  1322. void *type_data)
  1323. {
  1324. if (type != TC_SETUP_QDISC_MQPRIO)
  1325. return -EOPNOTSUPP;
  1326. return hns3_setup_tc(dev, type_data);
  1327. }
  1328. static int hns3_vlan_rx_add_vid(struct net_device *netdev,
  1329. __be16 proto, u16 vid)
  1330. {
  1331. struct hnae3_handle *h = hns3_get_handle(netdev);
  1332. int ret = -EIO;
  1333. if (h->ae_algo->ops->set_vlan_filter)
  1334. ret = h->ae_algo->ops->set_vlan_filter(h, proto, vid, false);
  1335. return ret;
  1336. }
  1337. static int hns3_vlan_rx_kill_vid(struct net_device *netdev,
  1338. __be16 proto, u16 vid)
  1339. {
  1340. struct hnae3_handle *h = hns3_get_handle(netdev);
  1341. int ret = -EIO;
  1342. if (h->ae_algo->ops->set_vlan_filter)
  1343. ret = h->ae_algo->ops->set_vlan_filter(h, proto, vid, true);
  1344. return ret;
  1345. }
  1346. static int hns3_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan,
  1347. u8 qos, __be16 vlan_proto)
  1348. {
  1349. struct hnae3_handle *h = hns3_get_handle(netdev);
  1350. int ret = -EIO;
  1351. netif_dbg(h, drv, netdev,
  1352. "set vf vlan: vf=%d, vlan=%u, qos=%u, vlan_proto=%u\n",
  1353. vf, vlan, qos, vlan_proto);
  1354. if (h->ae_algo->ops->set_vf_vlan_filter)
  1355. ret = h->ae_algo->ops->set_vf_vlan_filter(h, vf, vlan,
  1356. qos, vlan_proto);
  1357. return ret;
  1358. }
  1359. static int hns3_nic_change_mtu(struct net_device *netdev, int new_mtu)
  1360. {
  1361. struct hnae3_handle *h = hns3_get_handle(netdev);
  1362. int ret;
  1363. if (hns3_nic_resetting(netdev))
  1364. return -EBUSY;
  1365. if (!h->ae_algo->ops->set_mtu)
  1366. return -EOPNOTSUPP;
  1367. netif_dbg(h, drv, netdev,
  1368. "change mtu from %u to %d\n", netdev->mtu, new_mtu);
  1369. ret = h->ae_algo->ops->set_mtu(h, new_mtu);
  1370. if (ret)
  1371. netdev_err(netdev, "failed to change MTU in hardware %d\n",
  1372. ret);
  1373. else
  1374. netdev->mtu = new_mtu;
  1375. return ret;
  1376. }
  1377. static bool hns3_get_tx_timeo_queue_info(struct net_device *ndev)
  1378. {
  1379. struct hns3_nic_priv *priv = netdev_priv(ndev);
  1380. struct hnae3_handle *h = hns3_get_handle(ndev);
  1381. struct hns3_enet_ring *tx_ring = NULL;
  1382. struct napi_struct *napi;
  1383. int timeout_queue = 0;
  1384. int hw_head, hw_tail;
  1385. int fbd_num, fbd_oft;
  1386. int ebd_num, ebd_oft;
  1387. int bd_num, bd_err;
  1388. int ring_en, tc;
  1389. int i;
  1390. /* Find the stopped queue the same way the stack does */
  1391. for (i = 0; i < ndev->num_tx_queues; i++) {
  1392. struct netdev_queue *q;
  1393. unsigned long trans_start;
  1394. q = netdev_get_tx_queue(ndev, i);
  1395. trans_start = q->trans_start;
  1396. if (netif_xmit_stopped(q) &&
  1397. time_after(jiffies,
  1398. (trans_start + ndev->watchdog_timeo))) {
  1399. timeout_queue = i;
  1400. netdev_info(ndev, "queue state: 0x%lx, delta msecs: %u\n",
  1401. q->state,
  1402. jiffies_to_msecs(jiffies - trans_start));
  1403. break;
  1404. }
  1405. }
  1406. if (i == ndev->num_tx_queues) {
  1407. netdev_info(ndev,
  1408. "no netdev TX timeout queue found, timeout count: %llu\n",
  1409. priv->tx_timeout_count);
  1410. return false;
  1411. }
  1412. priv->tx_timeout_count++;
  1413. tx_ring = priv->ring_data[timeout_queue].ring;
  1414. napi = &tx_ring->tqp_vector->napi;
  1415. netdev_info(ndev,
  1416. "tx_timeout count: %llu, queue id: %d, SW_NTU: 0x%x, SW_NTC: 0x%x, napi state: %lu\n",
  1417. priv->tx_timeout_count, timeout_queue, tx_ring->next_to_use,
  1418. tx_ring->next_to_clean, napi->state);
  1419. netdev_info(ndev,
  1420. "tx_pkts: %llu, tx_bytes: %llu, io_err_cnt: %llu, sw_err_cnt: %llu\n",
  1421. tx_ring->stats.tx_pkts, tx_ring->stats.tx_bytes,
  1422. tx_ring->stats.io_err_cnt, tx_ring->stats.sw_err_cnt);
  1423. netdev_info(ndev,
  1424. "seg_pkt_cnt: %llu, tx_err_cnt: %llu, restart_queue: %llu, tx_busy: %llu\n",
  1425. tx_ring->stats.seg_pkt_cnt, tx_ring->stats.tx_err_cnt,
  1426. tx_ring->stats.restart_queue, tx_ring->stats.tx_busy);
  1427. /* When mac received many pause frames continuous, it's unable to send
  1428. * packets, which may cause tx timeout
  1429. */
  1430. if (h->ae_algo->ops->get_mac_stats) {
  1431. struct hns3_mac_stats mac_stats;
  1432. h->ae_algo->ops->get_mac_stats(h, &mac_stats);
  1433. netdev_info(ndev, "tx_pause_cnt: %llu, rx_pause_cnt: %llu\n",
  1434. mac_stats.tx_pause_cnt, mac_stats.rx_pause_cnt);
  1435. }
  1436. hw_head = readl_relaxed(tx_ring->tqp->io_base +
  1437. HNS3_RING_TX_RING_HEAD_REG);
  1438. hw_tail = readl_relaxed(tx_ring->tqp->io_base +
  1439. HNS3_RING_TX_RING_TAIL_REG);
  1440. fbd_num = readl_relaxed(tx_ring->tqp->io_base +
  1441. HNS3_RING_TX_RING_FBDNUM_REG);
  1442. fbd_oft = readl_relaxed(tx_ring->tqp->io_base +
  1443. HNS3_RING_TX_RING_OFFSET_REG);
  1444. ebd_num = readl_relaxed(tx_ring->tqp->io_base +
  1445. HNS3_RING_TX_RING_EBDNUM_REG);
  1446. ebd_oft = readl_relaxed(tx_ring->tqp->io_base +
  1447. HNS3_RING_TX_RING_EBD_OFFSET_REG);
  1448. bd_num = readl_relaxed(tx_ring->tqp->io_base +
  1449. HNS3_RING_TX_RING_BD_NUM_REG);
  1450. bd_err = readl_relaxed(tx_ring->tqp->io_base +
  1451. HNS3_RING_TX_RING_BD_ERR_REG);
  1452. ring_en = readl_relaxed(tx_ring->tqp->io_base + HNS3_RING_EN_REG);
  1453. tc = readl_relaxed(tx_ring->tqp->io_base + HNS3_RING_TX_RING_TC_REG);
  1454. netdev_info(ndev,
  1455. "BD_NUM: 0x%x HW_HEAD: 0x%x, HW_TAIL: 0x%x, BD_ERR: 0x%x, INT: 0x%x\n",
  1456. bd_num, hw_head, hw_tail, bd_err,
  1457. readl(tx_ring->tqp_vector->mask_addr));
  1458. netdev_info(ndev,
  1459. "RING_EN: 0x%x, TC: 0x%x, FBD_NUM: 0x%x FBD_OFT: 0x%x, EBD_NUM: 0x%x, EBD_OFT: 0x%x\n",
  1460. ring_en, tc, fbd_num, fbd_oft, ebd_num, ebd_oft);
  1461. return true;
  1462. }
  1463. static void hns3_nic_net_timeout(struct net_device *ndev)
  1464. {
  1465. struct hns3_nic_priv *priv = netdev_priv(ndev);
  1466. struct hnae3_handle *h = priv->ae_handle;
  1467. if (!hns3_get_tx_timeo_queue_info(ndev))
  1468. return;
  1469. /* request the reset, and let the hclge to determine
  1470. * which reset level should be done
  1471. */
  1472. if (h->ae_algo->ops->reset_event)
  1473. h->ae_algo->ops->reset_event(h->pdev, h);
  1474. }
  1475. #ifdef CONFIG_RFS_ACCEL
  1476. static int hns3_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
  1477. u16 rxq_index, u32 flow_id)
  1478. {
  1479. struct hnae3_handle *h = hns3_get_handle(dev);
  1480. struct flow_keys fkeys;
  1481. if (!h->ae_algo->ops->add_arfs_entry)
  1482. return -EOPNOTSUPP;
  1483. if (skb->encapsulation)
  1484. return -EPROTONOSUPPORT;
  1485. if (!skb_flow_dissect_flow_keys(skb, &fkeys, 0))
  1486. return -EPROTONOSUPPORT;
  1487. if ((fkeys.basic.n_proto != htons(ETH_P_IP) &&
  1488. fkeys.basic.n_proto != htons(ETH_P_IPV6)) ||
  1489. (fkeys.basic.ip_proto != IPPROTO_TCP &&
  1490. fkeys.basic.ip_proto != IPPROTO_UDP))
  1491. return -EPROTONOSUPPORT;
  1492. return h->ae_algo->ops->add_arfs_entry(h, rxq_index, flow_id, &fkeys);
  1493. }
  1494. #endif
  1495. static const struct net_device_ops hns3_nic_netdev_ops = {
  1496. .ndo_open = hns3_nic_net_open,
  1497. .ndo_stop = hns3_nic_net_stop,
  1498. .ndo_start_xmit = hns3_nic_net_xmit,
  1499. .ndo_tx_timeout = hns3_nic_net_timeout,
  1500. .ndo_set_mac_address = hns3_nic_net_set_mac_address,
  1501. .ndo_do_ioctl = hns3_nic_do_ioctl,
  1502. .ndo_change_mtu = hns3_nic_change_mtu,
  1503. .ndo_set_features = hns3_nic_set_features,
  1504. .ndo_get_stats64 = hns3_nic_get_stats64,
  1505. .ndo_setup_tc = hns3_nic_setup_tc,
  1506. .ndo_set_rx_mode = hns3_nic_set_rx_mode,
  1507. .ndo_vlan_rx_add_vid = hns3_vlan_rx_add_vid,
  1508. .ndo_vlan_rx_kill_vid = hns3_vlan_rx_kill_vid,
  1509. .ndo_set_vf_vlan = hns3_ndo_set_vf_vlan,
  1510. #ifdef CONFIG_RFS_ACCEL
  1511. .ndo_rx_flow_steer = hns3_rx_flow_steer,
  1512. #endif
  1513. };
  1514. bool hns3_is_phys_func(struct pci_dev *pdev)
  1515. {
  1516. u32 dev_id = pdev->device;
  1517. switch (dev_id) {
  1518. case HNAE3_DEV_ID_GE:
  1519. case HNAE3_DEV_ID_25GE:
  1520. case HNAE3_DEV_ID_25GE_RDMA:
  1521. case HNAE3_DEV_ID_25GE_RDMA_MACSEC:
  1522. case HNAE3_DEV_ID_50GE_RDMA:
  1523. case HNAE3_DEV_ID_50GE_RDMA_MACSEC:
  1524. case HNAE3_DEV_ID_100G_RDMA_MACSEC:
  1525. return true;
  1526. case HNAE3_DEV_ID_100G_VF:
  1527. case HNAE3_DEV_ID_100G_RDMA_DCB_PFC_VF:
  1528. return false;
  1529. default:
  1530. dev_warn(&pdev->dev, "un-recognized pci device-id %d",
  1531. dev_id);
  1532. }
  1533. return false;
  1534. }
  1535. static void hns3_disable_sriov(struct pci_dev *pdev)
  1536. {
  1537. /* If our VFs are assigned we cannot shut down SR-IOV
  1538. * without causing issues, so just leave the hardware
  1539. * available but disabled
  1540. */
  1541. if (pci_vfs_assigned(pdev)) {
  1542. dev_warn(&pdev->dev,
  1543. "disabling driver while VFs are assigned\n");
  1544. return;
  1545. }
  1546. pci_disable_sriov(pdev);
  1547. }
  1548. static void hns3_get_dev_capability(struct pci_dev *pdev,
  1549. struct hnae3_ae_dev *ae_dev)
  1550. {
  1551. if (pdev->revision >= 0x21) {
  1552. hnae3_set_bit(ae_dev->flag, HNAE3_DEV_SUPPORT_FD_B, 1);
  1553. hnae3_set_bit(ae_dev->flag, HNAE3_DEV_SUPPORT_GRO_B, 1);
  1554. }
  1555. }
  1556. /* hns3_probe - Device initialization routine
  1557. * @pdev: PCI device information struct
  1558. * @ent: entry in hns3_pci_tbl
  1559. *
  1560. * hns3_probe initializes a PF identified by a pci_dev structure.
  1561. * The OS initialization, configuring of the PF private structure,
  1562. * and a hardware reset occur.
  1563. *
  1564. * Returns 0 on success, negative on failure
  1565. */
  1566. static int hns3_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  1567. {
  1568. struct hnae3_ae_dev *ae_dev;
  1569. int ret;
  1570. ae_dev = devm_kzalloc(&pdev->dev, sizeof(*ae_dev), GFP_KERNEL);
  1571. if (!ae_dev) {
  1572. ret = -ENOMEM;
  1573. return ret;
  1574. }
  1575. ae_dev->pdev = pdev;
  1576. ae_dev->flag = ent->driver_data;
  1577. ae_dev->reset_type = HNAE3_NONE_RESET;
  1578. hns3_get_dev_capability(pdev, ae_dev);
  1579. pci_set_drvdata(pdev, ae_dev);
  1580. ret = hnae3_register_ae_dev(ae_dev);
  1581. if (ret) {
  1582. devm_kfree(&pdev->dev, ae_dev);
  1583. pci_set_drvdata(pdev, NULL);
  1584. }
  1585. return ret;
  1586. }
  1587. /* hns3_remove - Device removal routine
  1588. * @pdev: PCI device information struct
  1589. */
  1590. static void hns3_remove(struct pci_dev *pdev)
  1591. {
  1592. struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev);
  1593. if (hns3_is_phys_func(pdev) && IS_ENABLED(CONFIG_PCI_IOV))
  1594. hns3_disable_sriov(pdev);
  1595. hnae3_unregister_ae_dev(ae_dev);
  1596. pci_set_drvdata(pdev, NULL);
  1597. }
  1598. /**
  1599. * hns3_pci_sriov_configure
  1600. * @pdev: pointer to a pci_dev structure
  1601. * @num_vfs: number of VFs to allocate
  1602. *
  1603. * Enable or change the number of VFs. Called when the user updates the number
  1604. * of VFs in sysfs.
  1605. **/
  1606. static int hns3_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
  1607. {
  1608. int ret;
  1609. if (!(hns3_is_phys_func(pdev) && IS_ENABLED(CONFIG_PCI_IOV))) {
  1610. dev_warn(&pdev->dev, "Can not config SRIOV\n");
  1611. return -EINVAL;
  1612. }
  1613. if (num_vfs) {
  1614. ret = pci_enable_sriov(pdev, num_vfs);
  1615. if (ret)
  1616. dev_err(&pdev->dev, "SRIOV enable failed %d\n", ret);
  1617. else
  1618. return num_vfs;
  1619. } else if (!pci_vfs_assigned(pdev)) {
  1620. pci_disable_sriov(pdev);
  1621. } else {
  1622. dev_warn(&pdev->dev,
  1623. "Unable to free VFs because some are assigned to VMs.\n");
  1624. }
  1625. return 0;
  1626. }
  1627. static void hns3_shutdown(struct pci_dev *pdev)
  1628. {
  1629. struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev);
  1630. hnae3_unregister_ae_dev(ae_dev);
  1631. devm_kfree(&pdev->dev, ae_dev);
  1632. pci_set_drvdata(pdev, NULL);
  1633. if (system_state == SYSTEM_POWER_OFF)
  1634. pci_set_power_state(pdev, PCI_D3hot);
  1635. }
  1636. static pci_ers_result_t hns3_error_detected(struct pci_dev *pdev,
  1637. pci_channel_state_t state)
  1638. {
  1639. struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev);
  1640. pci_ers_result_t ret;
  1641. dev_info(&pdev->dev, "PCI error detected, state(=%d)!!\n", state);
  1642. if (state == pci_channel_io_perm_failure)
  1643. return PCI_ERS_RESULT_DISCONNECT;
  1644. if (!ae_dev || !ae_dev->ops) {
  1645. dev_err(&pdev->dev,
  1646. "Can't recover - error happened before device initialized\n");
  1647. return PCI_ERS_RESULT_NONE;
  1648. }
  1649. if (ae_dev->ops->handle_hw_ras_error)
  1650. ret = ae_dev->ops->handle_hw_ras_error(ae_dev);
  1651. else
  1652. return PCI_ERS_RESULT_NONE;
  1653. return ret;
  1654. }
  1655. static pci_ers_result_t hns3_slot_reset(struct pci_dev *pdev)
  1656. {
  1657. struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev);
  1658. const struct hnae3_ae_ops *ops;
  1659. enum hnae3_reset_type reset_type;
  1660. struct device *dev = &pdev->dev;
  1661. if (!ae_dev || !ae_dev->ops)
  1662. return PCI_ERS_RESULT_NONE;
  1663. ops = ae_dev->ops;
  1664. /* request the reset */
  1665. if (ops->reset_event && ops->get_reset_level &&
  1666. ops->set_default_reset_request) {
  1667. if (ae_dev->hw_err_reset_req) {
  1668. reset_type = ops->get_reset_level(ae_dev,
  1669. &ae_dev->hw_err_reset_req);
  1670. ops->set_default_reset_request(ae_dev, reset_type);
  1671. dev_info(dev, "requesting reset due to PCI error\n");
  1672. ops->reset_event(pdev, NULL);
  1673. }
  1674. return PCI_ERS_RESULT_RECOVERED;
  1675. }
  1676. return PCI_ERS_RESULT_DISCONNECT;
  1677. }
  1678. static void hns3_reset_prepare(struct pci_dev *pdev)
  1679. {
  1680. struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev);
  1681. dev_info(&pdev->dev, "hns3 flr prepare\n");
  1682. if (ae_dev && ae_dev->ops && ae_dev->ops->flr_prepare)
  1683. ae_dev->ops->flr_prepare(ae_dev);
  1684. }
  1685. static void hns3_reset_done(struct pci_dev *pdev)
  1686. {
  1687. struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev);
  1688. dev_info(&pdev->dev, "hns3 flr done\n");
  1689. if (ae_dev && ae_dev->ops && ae_dev->ops->flr_done)
  1690. ae_dev->ops->flr_done(ae_dev);
  1691. }
  1692. static const struct pci_error_handlers hns3_err_handler = {
  1693. .error_detected = hns3_error_detected,
  1694. .slot_reset = hns3_slot_reset,
  1695. .reset_prepare = hns3_reset_prepare,
  1696. .reset_done = hns3_reset_done,
  1697. };
  1698. static struct pci_driver hns3_driver = {
  1699. .name = hns3_driver_name,
  1700. .id_table = hns3_pci_tbl,
  1701. .probe = hns3_probe,
  1702. .remove = hns3_remove,
  1703. .shutdown = hns3_shutdown,
  1704. .sriov_configure = hns3_pci_sriov_configure,
  1705. .err_handler = &hns3_err_handler,
  1706. };
  1707. /* set default feature to hns3 */
  1708. static void hns3_set_default_feature(struct net_device *netdev)
  1709. {
  1710. struct hnae3_handle *h = hns3_get_handle(netdev);
  1711. struct pci_dev *pdev = h->pdev;
  1712. netdev->priv_flags |= IFF_UNICAST_FLT;
  1713. netdev->hw_enc_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  1714. NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_GSO |
  1715. NETIF_F_GRO | NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_GSO_GRE |
  1716. NETIF_F_GSO_GRE_CSUM | NETIF_F_GSO_UDP_TUNNEL |
  1717. NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_SCTP_CRC;
  1718. netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
  1719. netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
  1720. netdev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  1721. NETIF_F_HW_VLAN_CTAG_FILTER |
  1722. NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
  1723. NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_GSO |
  1724. NETIF_F_GRO | NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_GSO_GRE |
  1725. NETIF_F_GSO_GRE_CSUM | NETIF_F_GSO_UDP_TUNNEL |
  1726. NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_SCTP_CRC;
  1727. netdev->vlan_features |=
  1728. NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM |
  1729. NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO |
  1730. NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_GSO_GRE |
  1731. NETIF_F_GSO_GRE_CSUM | NETIF_F_GSO_UDP_TUNNEL |
  1732. NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_SCTP_CRC;
  1733. netdev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  1734. NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
  1735. NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_GSO |
  1736. NETIF_F_GRO | NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_GSO_GRE |
  1737. NETIF_F_GSO_GRE_CSUM | NETIF_F_GSO_UDP_TUNNEL |
  1738. NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_SCTP_CRC;
  1739. if (pdev->revision >= 0x21) {
  1740. netdev->hw_features |= NETIF_F_GRO_HW;
  1741. netdev->features |= NETIF_F_GRO_HW;
  1742. if (!(h->flags & HNAE3_SUPPORT_VF)) {
  1743. netdev->hw_features |= NETIF_F_NTUPLE;
  1744. netdev->features |= NETIF_F_NTUPLE;
  1745. }
  1746. }
  1747. }
  1748. static int hns3_alloc_buffer(struct hns3_enet_ring *ring,
  1749. struct hns3_desc_cb *cb)
  1750. {
  1751. unsigned int order = hns3_page_order(ring);
  1752. struct page *p;
  1753. p = dev_alloc_pages(order);
  1754. if (!p)
  1755. return -ENOMEM;
  1756. cb->priv = p;
  1757. cb->page_offset = 0;
  1758. cb->reuse_flag = 0;
  1759. cb->buf = page_address(p);
  1760. cb->length = hns3_page_size(ring);
  1761. cb->type = DESC_TYPE_PAGE;
  1762. return 0;
  1763. }
  1764. static void hns3_free_buffer(struct hns3_enet_ring *ring,
  1765. struct hns3_desc_cb *cb)
  1766. {
  1767. if (cb->type == DESC_TYPE_SKB)
  1768. dev_kfree_skb_any((struct sk_buff *)cb->priv);
  1769. else if (!HNAE3_IS_TX_RING(ring))
  1770. put_page((struct page *)cb->priv);
  1771. memset(cb, 0, sizeof(*cb));
  1772. }
  1773. static int hns3_map_buffer(struct hns3_enet_ring *ring, struct hns3_desc_cb *cb)
  1774. {
  1775. cb->dma = dma_map_page(ring_to_dev(ring), cb->priv, 0,
  1776. cb->length, ring_to_dma_dir(ring));
  1777. if (unlikely(dma_mapping_error(ring_to_dev(ring), cb->dma)))
  1778. return -EIO;
  1779. return 0;
  1780. }
  1781. static void hns3_unmap_buffer(struct hns3_enet_ring *ring,
  1782. struct hns3_desc_cb *cb)
  1783. {
  1784. if (cb->type == DESC_TYPE_SKB)
  1785. dma_unmap_single(ring_to_dev(ring), cb->dma, cb->length,
  1786. ring_to_dma_dir(ring));
  1787. else if (cb->length)
  1788. dma_unmap_page(ring_to_dev(ring), cb->dma, cb->length,
  1789. ring_to_dma_dir(ring));
  1790. }
  1791. static void hns3_buffer_detach(struct hns3_enet_ring *ring, int i)
  1792. {
  1793. hns3_unmap_buffer(ring, &ring->desc_cb[i]);
  1794. ring->desc[i].addr = 0;
  1795. }
  1796. static void hns3_free_buffer_detach(struct hns3_enet_ring *ring, int i)
  1797. {
  1798. struct hns3_desc_cb *cb = &ring->desc_cb[i];
  1799. if (!ring->desc_cb[i].dma)
  1800. return;
  1801. hns3_buffer_detach(ring, i);
  1802. hns3_free_buffer(ring, cb);
  1803. }
  1804. static void hns3_free_buffers(struct hns3_enet_ring *ring)
  1805. {
  1806. int i;
  1807. for (i = 0; i < ring->desc_num; i++)
  1808. hns3_free_buffer_detach(ring, i);
  1809. }
  1810. /* free desc along with its attached buffer */
  1811. static void hns3_free_desc(struct hns3_enet_ring *ring)
  1812. {
  1813. int size = ring->desc_num * sizeof(ring->desc[0]);
  1814. hns3_free_buffers(ring);
  1815. if (ring->desc) {
  1816. dma_free_coherent(ring_to_dev(ring), size,
  1817. ring->desc, ring->desc_dma_addr);
  1818. ring->desc = NULL;
  1819. }
  1820. }
  1821. static int hns3_alloc_desc(struct hns3_enet_ring *ring)
  1822. {
  1823. int size = ring->desc_num * sizeof(ring->desc[0]);
  1824. ring->desc = dma_alloc_coherent(ring_to_dev(ring), size,
  1825. &ring->desc_dma_addr, GFP_KERNEL);
  1826. if (!ring->desc)
  1827. return -ENOMEM;
  1828. return 0;
  1829. }
  1830. static int hns3_reserve_buffer_map(struct hns3_enet_ring *ring,
  1831. struct hns3_desc_cb *cb)
  1832. {
  1833. int ret;
  1834. ret = hns3_alloc_buffer(ring, cb);
  1835. if (ret)
  1836. goto out;
  1837. ret = hns3_map_buffer(ring, cb);
  1838. if (ret)
  1839. goto out_with_buf;
  1840. return 0;
  1841. out_with_buf:
  1842. hns3_free_buffer(ring, cb);
  1843. out:
  1844. return ret;
  1845. }
  1846. static int hns3_alloc_buffer_attach(struct hns3_enet_ring *ring, int i)
  1847. {
  1848. int ret = hns3_reserve_buffer_map(ring, &ring->desc_cb[i]);
  1849. if (ret)
  1850. return ret;
  1851. ring->desc[i].addr = cpu_to_le64(ring->desc_cb[i].dma);
  1852. return 0;
  1853. }
  1854. /* Allocate memory for raw pkg, and map with dma */
  1855. static int hns3_alloc_ring_buffers(struct hns3_enet_ring *ring)
  1856. {
  1857. int i, j, ret;
  1858. for (i = 0; i < ring->desc_num; i++) {
  1859. ret = hns3_alloc_buffer_attach(ring, i);
  1860. if (ret)
  1861. goto out_buffer_fail;
  1862. }
  1863. return 0;
  1864. out_buffer_fail:
  1865. for (j = i - 1; j >= 0; j--)
  1866. hns3_free_buffer_detach(ring, j);
  1867. return ret;
  1868. }
  1869. /* detach a in-used buffer and replace with a reserved one */
  1870. static void hns3_replace_buffer(struct hns3_enet_ring *ring, int i,
  1871. struct hns3_desc_cb *res_cb)
  1872. {
  1873. hns3_unmap_buffer(ring, &ring->desc_cb[i]);
  1874. ring->desc_cb[i] = *res_cb;
  1875. ring->desc[i].addr = cpu_to_le64(ring->desc_cb[i].dma);
  1876. ring->desc[i].rx.bd_base_info = 0;
  1877. }
  1878. static void hns3_reuse_buffer(struct hns3_enet_ring *ring, int i)
  1879. {
  1880. ring->desc_cb[i].reuse_flag = 0;
  1881. ring->desc[i].addr = cpu_to_le64(ring->desc_cb[i].dma +
  1882. ring->desc_cb[i].page_offset);
  1883. ring->desc[i].rx.bd_base_info = 0;
  1884. }
  1885. static void hns3_nic_reclaim_desc(struct hns3_enet_ring *ring, int head,
  1886. int *bytes, int *pkts)
  1887. {
  1888. int ntc = ring->next_to_clean;
  1889. struct hns3_desc_cb *desc_cb;
  1890. while (head != ntc) {
  1891. desc_cb = &ring->desc_cb[ntc];
  1892. (*pkts) += (desc_cb->type == DESC_TYPE_SKB);
  1893. (*bytes) += desc_cb->length;
  1894. /* desc_cb will be cleaned, after hnae3_free_buffer_detach */
  1895. hns3_free_buffer_detach(ring, ntc);
  1896. if (++ntc == ring->desc_num)
  1897. ntc = 0;
  1898. /* Issue prefetch for next Tx descriptor */
  1899. prefetch(&ring->desc_cb[ntc]);
  1900. }
  1901. /* This smp_store_release() pairs with smp_load_acquire() in
  1902. * ring_space called by hns3_nic_net_xmit.
  1903. */
  1904. smp_store_release(&ring->next_to_clean, ntc);
  1905. }
  1906. static int is_valid_clean_head(struct hns3_enet_ring *ring, int h)
  1907. {
  1908. int u = ring->next_to_use;
  1909. int c = ring->next_to_clean;
  1910. if (unlikely(h > ring->desc_num))
  1911. return 0;
  1912. return u > c ? (h > c && h <= u) : (h > c || h <= u);
  1913. }
  1914. void hns3_clean_tx_ring(struct hns3_enet_ring *ring)
  1915. {
  1916. struct net_device *netdev = ring->tqp->handle->kinfo.netdev;
  1917. struct hns3_nic_priv *priv = netdev_priv(netdev);
  1918. struct netdev_queue *dev_queue;
  1919. int bytes, pkts;
  1920. int head;
  1921. head = readl_relaxed(ring->tqp->io_base + HNS3_RING_TX_RING_HEAD_REG);
  1922. rmb(); /* Make sure head is ready before touch any data */
  1923. if (is_ring_empty(ring) || head == ring->next_to_clean)
  1924. return; /* no data to poll */
  1925. if (unlikely(!is_valid_clean_head(ring, head))) {
  1926. netdev_err(netdev, "wrong head (%d, %d-%d)\n", head,
  1927. ring->next_to_use, ring->next_to_clean);
  1928. u64_stats_update_begin(&ring->syncp);
  1929. ring->stats.io_err_cnt++;
  1930. u64_stats_update_end(&ring->syncp);
  1931. return;
  1932. }
  1933. bytes = 0;
  1934. pkts = 0;
  1935. hns3_nic_reclaim_desc(ring, head, &bytes, &pkts);
  1936. ring->tqp_vector->tx_group.total_bytes += bytes;
  1937. ring->tqp_vector->tx_group.total_packets += pkts;
  1938. u64_stats_update_begin(&ring->syncp);
  1939. ring->stats.tx_bytes += bytes;
  1940. ring->stats.tx_pkts += pkts;
  1941. u64_stats_update_end(&ring->syncp);
  1942. dev_queue = netdev_get_tx_queue(netdev, ring->tqp->tqp_index);
  1943. netdev_tx_completed_queue(dev_queue, pkts, bytes);
  1944. if (unlikely(pkts && netif_carrier_ok(netdev) &&
  1945. (ring_space(ring) > HNS3_MAX_BD_PER_PKT))) {
  1946. /* Make sure that anybody stopping the queue after this
  1947. * sees the new next_to_clean.
  1948. */
  1949. smp_mb();
  1950. if (netif_tx_queue_stopped(dev_queue) &&
  1951. !test_bit(HNS3_NIC_STATE_DOWN, &priv->state)) {
  1952. netif_tx_wake_queue(dev_queue);
  1953. ring->stats.restart_queue++;
  1954. }
  1955. }
  1956. }
  1957. static int hns3_desc_unused(struct hns3_enet_ring *ring)
  1958. {
  1959. int ntc = ring->next_to_clean;
  1960. int ntu = ring->next_to_use;
  1961. return ((ntc >= ntu) ? 0 : ring->desc_num) + ntc - ntu;
  1962. }
  1963. static void hns3_nic_alloc_rx_buffers(struct hns3_enet_ring *ring,
  1964. int cleand_count)
  1965. {
  1966. struct hns3_desc_cb *desc_cb;
  1967. struct hns3_desc_cb res_cbs;
  1968. int i, ret;
  1969. for (i = 0; i < cleand_count; i++) {
  1970. desc_cb = &ring->desc_cb[ring->next_to_use];
  1971. if (desc_cb->reuse_flag) {
  1972. u64_stats_update_begin(&ring->syncp);
  1973. ring->stats.reuse_pg_cnt++;
  1974. u64_stats_update_end(&ring->syncp);
  1975. hns3_reuse_buffer(ring, ring->next_to_use);
  1976. } else {
  1977. ret = hns3_reserve_buffer_map(ring, &res_cbs);
  1978. if (ret) {
  1979. u64_stats_update_begin(&ring->syncp);
  1980. ring->stats.sw_err_cnt++;
  1981. u64_stats_update_end(&ring->syncp);
  1982. hns3_rl_err(ring->tqp_vector->napi.dev,
  1983. "alloc rx buffer failed: %d\n",
  1984. ret);
  1985. break;
  1986. }
  1987. hns3_replace_buffer(ring, ring->next_to_use, &res_cbs);
  1988. u64_stats_update_begin(&ring->syncp);
  1989. ring->stats.non_reuse_pg++;
  1990. u64_stats_update_end(&ring->syncp);
  1991. }
  1992. ring_ptr_move_fw(ring, next_to_use);
  1993. }
  1994. wmb(); /* Make all data has been write before submit */
  1995. writel_relaxed(i, ring->tqp->io_base + HNS3_RING_RX_RING_HEAD_REG);
  1996. }
  1997. static void hns3_nic_reuse_page(struct sk_buff *skb, int i,
  1998. struct hns3_enet_ring *ring, int pull_len,
  1999. struct hns3_desc_cb *desc_cb)
  2000. {
  2001. struct hns3_desc *desc = &ring->desc[ring->next_to_clean];
  2002. int size = le16_to_cpu(desc->rx.size);
  2003. u32 truesize = hns3_buf_size(ring);
  2004. skb_add_rx_frag(skb, i, desc_cb->priv, desc_cb->page_offset + pull_len,
  2005. size - pull_len, truesize);
  2006. /* Avoid re-using remote pages, or the stack is still using the page
  2007. * when page_offset rollback to zero, flag default unreuse
  2008. */
  2009. if (unlikely(page_to_nid(desc_cb->priv) != numa_mem_id()) ||
  2010. (!desc_cb->page_offset && page_count(desc_cb->priv) > 1))
  2011. return;
  2012. /* Move offset up to the next cache line */
  2013. desc_cb->page_offset += truesize;
  2014. if (desc_cb->page_offset + truesize <= hns3_page_size(ring)) {
  2015. desc_cb->reuse_flag = 1;
  2016. /* Bump ref count on page before it is given */
  2017. get_page(desc_cb->priv);
  2018. } else if (page_count(desc_cb->priv) == 1) {
  2019. desc_cb->reuse_flag = 1;
  2020. desc_cb->page_offset = 0;
  2021. get_page(desc_cb->priv);
  2022. }
  2023. }
  2024. static int hns3_gro_complete(struct sk_buff *skb, u32 l234info)
  2025. {
  2026. __be16 type = skb->protocol;
  2027. struct tcphdr *th;
  2028. int depth = 0;
  2029. while (eth_type_vlan(type)) {
  2030. struct vlan_hdr *vh;
  2031. if ((depth + VLAN_HLEN) > skb_headlen(skb))
  2032. return -EFAULT;
  2033. vh = (struct vlan_hdr *)(skb->data + depth);
  2034. type = vh->h_vlan_encapsulated_proto;
  2035. depth += VLAN_HLEN;
  2036. }
  2037. skb_set_network_header(skb, depth);
  2038. if (type == htons(ETH_P_IP)) {
  2039. const struct iphdr *iph = ip_hdr(skb);
  2040. depth += sizeof(struct iphdr);
  2041. skb_set_transport_header(skb, depth);
  2042. th = tcp_hdr(skb);
  2043. th->check = ~tcp_v4_check(skb->len - depth, iph->saddr,
  2044. iph->daddr, 0);
  2045. } else if (type == htons(ETH_P_IPV6)) {
  2046. const struct ipv6hdr *iph = ipv6_hdr(skb);
  2047. depth += sizeof(struct ipv6hdr);
  2048. skb_set_transport_header(skb, depth);
  2049. th = tcp_hdr(skb);
  2050. th->check = ~tcp_v6_check(skb->len - depth, &iph->saddr,
  2051. &iph->daddr, 0);
  2052. } else {
  2053. hns3_rl_err(skb->dev,
  2054. "Error: FW GRO supports only IPv4/IPv6, not 0x%04x, depth: %d\n",
  2055. be16_to_cpu(type), depth);
  2056. return -EFAULT;
  2057. }
  2058. skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
  2059. if (th->cwr)
  2060. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  2061. if (l234info & BIT(HNS3_RXD_GRO_FIXID_B))
  2062. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_FIXEDID;
  2063. skb->csum_start = (unsigned char *)th - skb->head;
  2064. skb->csum_offset = offsetof(struct tcphdr, check);
  2065. skb->ip_summed = CHECKSUM_PARTIAL;
  2066. return 0;
  2067. }
  2068. static void hns3_rx_checksum(struct hns3_enet_ring *ring, struct sk_buff *skb,
  2069. u32 l234info, u32 bd_base_info, u32 ol_info)
  2070. {
  2071. struct net_device *netdev = ring->tqp->handle->kinfo.netdev;
  2072. int l3_type, l4_type;
  2073. int ol4_type;
  2074. skb->ip_summed = CHECKSUM_NONE;
  2075. skb_checksum_none_assert(skb);
  2076. if (!(netdev->features & NETIF_F_RXCSUM))
  2077. return;
  2078. /* check if hardware has done checksum */
  2079. if (!(bd_base_info & BIT(HNS3_RXD_L3L4P_B)))
  2080. return;
  2081. if (unlikely(l234info & (BIT(HNS3_RXD_L3E_B) | BIT(HNS3_RXD_L4E_B) |
  2082. BIT(HNS3_RXD_OL3E_B) |
  2083. BIT(HNS3_RXD_OL4E_B)))) {
  2084. u64_stats_update_begin(&ring->syncp);
  2085. ring->stats.l3l4_csum_err++;
  2086. u64_stats_update_end(&ring->syncp);
  2087. return;
  2088. }
  2089. ol4_type = hnae3_get_field(ol_info, HNS3_RXD_OL4ID_M,
  2090. HNS3_RXD_OL4ID_S);
  2091. switch (ol4_type) {
  2092. case HNS3_OL4_TYPE_MAC_IN_UDP:
  2093. case HNS3_OL4_TYPE_NVGRE:
  2094. skb->csum_level = 1;
  2095. /* fall through */
  2096. case HNS3_OL4_TYPE_NO_TUN:
  2097. l3_type = hnae3_get_field(l234info, HNS3_RXD_L3ID_M,
  2098. HNS3_RXD_L3ID_S);
  2099. l4_type = hnae3_get_field(l234info, HNS3_RXD_L4ID_M,
  2100. HNS3_RXD_L4ID_S);
  2101. /* Can checksum ipv4 or ipv6 + UDP/TCP/SCTP packets */
  2102. if ((l3_type == HNS3_L3_TYPE_IPV4 ||
  2103. l3_type == HNS3_L3_TYPE_IPV6) &&
  2104. (l4_type == HNS3_L4_TYPE_UDP ||
  2105. l4_type == HNS3_L4_TYPE_TCP ||
  2106. l4_type == HNS3_L4_TYPE_SCTP))
  2107. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2108. break;
  2109. default:
  2110. break;
  2111. }
  2112. }
  2113. static void hns3_rx_skb(struct hns3_enet_ring *ring, struct sk_buff *skb)
  2114. {
  2115. if (skb_has_frag_list(skb))
  2116. napi_gro_flush(&ring->tqp_vector->napi, false);
  2117. napi_gro_receive(&ring->tqp_vector->napi, skb);
  2118. }
  2119. static bool hns3_parse_vlan_tag(struct hns3_enet_ring *ring,
  2120. struct hns3_desc *desc, u32 l234info,
  2121. u16 *vlan_tag)
  2122. {
  2123. struct hnae3_handle *handle = ring->tqp->handle;
  2124. struct pci_dev *pdev = ring->tqp->handle->pdev;
  2125. if (pdev->revision == 0x20) {
  2126. *vlan_tag = le16_to_cpu(desc->rx.ot_vlan_tag);
  2127. if (!(*vlan_tag & VLAN_VID_MASK))
  2128. *vlan_tag = le16_to_cpu(desc->rx.vlan_tag);
  2129. return (*vlan_tag != 0);
  2130. }
  2131. #define HNS3_STRP_OUTER_VLAN 0x1
  2132. #define HNS3_STRP_INNER_VLAN 0x2
  2133. #define HNS3_STRP_BOTH 0x3
  2134. /* Hardware always insert VLAN tag into RX descriptor when
  2135. * remove the tag from packet, driver needs to determine
  2136. * reporting which tag to stack.
  2137. */
  2138. switch (hnae3_get_field(l234info, HNS3_RXD_STRP_TAGP_M,
  2139. HNS3_RXD_STRP_TAGP_S)) {
  2140. case HNS3_STRP_OUTER_VLAN:
  2141. if (handle->port_base_vlan_state !=
  2142. HNAE3_PORT_BASE_VLAN_DISABLE)
  2143. return false;
  2144. *vlan_tag = le16_to_cpu(desc->rx.ot_vlan_tag);
  2145. return true;
  2146. case HNS3_STRP_INNER_VLAN:
  2147. if (handle->port_base_vlan_state !=
  2148. HNAE3_PORT_BASE_VLAN_DISABLE)
  2149. return false;
  2150. *vlan_tag = le16_to_cpu(desc->rx.vlan_tag);
  2151. return true;
  2152. case HNS3_STRP_BOTH:
  2153. if (handle->port_base_vlan_state ==
  2154. HNAE3_PORT_BASE_VLAN_DISABLE)
  2155. *vlan_tag = le16_to_cpu(desc->rx.ot_vlan_tag);
  2156. else
  2157. *vlan_tag = le16_to_cpu(desc->rx.vlan_tag);
  2158. return true;
  2159. default:
  2160. return false;
  2161. }
  2162. }
  2163. static int hns3_alloc_skb(struct hns3_enet_ring *ring, unsigned int length,
  2164. unsigned char *va)
  2165. {
  2166. #define HNS3_NEED_ADD_FRAG 1
  2167. struct hns3_desc_cb *desc_cb = &ring->desc_cb[ring->next_to_clean];
  2168. struct net_device *netdev = ring->tqp->handle->kinfo.netdev;
  2169. struct sk_buff *skb;
  2170. ring->skb = napi_alloc_skb(&ring->tqp_vector->napi, HNS3_RX_HEAD_SIZE);
  2171. skb = ring->skb;
  2172. if (unlikely(!skb)) {
  2173. hns3_rl_err(netdev, "alloc rx skb fail\n");
  2174. u64_stats_update_begin(&ring->syncp);
  2175. ring->stats.sw_err_cnt++;
  2176. u64_stats_update_end(&ring->syncp);
  2177. return -ENOMEM;
  2178. }
  2179. prefetchw(skb->data);
  2180. ring->pending_buf = 1;
  2181. ring->frag_num = 0;
  2182. ring->tail_skb = NULL;
  2183. if (length <= HNS3_RX_HEAD_SIZE) {
  2184. memcpy(__skb_put(skb, length), va, ALIGN(length, sizeof(long)));
  2185. /* We can reuse buffer as-is, just make sure it is local */
  2186. if (likely(page_to_nid(desc_cb->priv) == numa_mem_id()))
  2187. desc_cb->reuse_flag = 1;
  2188. else /* This page cannot be reused so discard it */
  2189. put_page(desc_cb->priv);
  2190. ring_ptr_move_fw(ring, next_to_clean);
  2191. return 0;
  2192. }
  2193. u64_stats_update_begin(&ring->syncp);
  2194. ring->stats.seg_pkt_cnt++;
  2195. u64_stats_update_end(&ring->syncp);
  2196. ring->pull_len = eth_get_headlen(netdev, va, HNS3_RX_HEAD_SIZE);
  2197. __skb_put(skb, ring->pull_len);
  2198. hns3_nic_reuse_page(skb, ring->frag_num++, ring, ring->pull_len,
  2199. desc_cb);
  2200. ring_ptr_move_fw(ring, next_to_clean);
  2201. return HNS3_NEED_ADD_FRAG;
  2202. }
  2203. static int hns3_add_frag(struct hns3_enet_ring *ring, struct hns3_desc *desc,
  2204. struct sk_buff **out_skb, bool pending)
  2205. {
  2206. struct sk_buff *skb = *out_skb;
  2207. struct sk_buff *head_skb = *out_skb;
  2208. struct sk_buff *new_skb;
  2209. struct hns3_desc_cb *desc_cb;
  2210. struct hns3_desc *pre_desc;
  2211. u32 bd_base_info;
  2212. int pre_bd;
  2213. /* if there is pending bd, the SW param next_to_clean has moved
  2214. * to next and the next is NULL
  2215. */
  2216. if (pending) {
  2217. pre_bd = (ring->next_to_clean - 1 + ring->desc_num) %
  2218. ring->desc_num;
  2219. pre_desc = &ring->desc[pre_bd];
  2220. bd_base_info = le32_to_cpu(pre_desc->rx.bd_base_info);
  2221. } else {
  2222. bd_base_info = le32_to_cpu(desc->rx.bd_base_info);
  2223. }
  2224. while (!(bd_base_info & BIT(HNS3_RXD_FE_B))) {
  2225. desc = &ring->desc[ring->next_to_clean];
  2226. desc_cb = &ring->desc_cb[ring->next_to_clean];
  2227. bd_base_info = le32_to_cpu(desc->rx.bd_base_info);
  2228. /* make sure HW write desc complete */
  2229. dma_rmb();
  2230. if (!(bd_base_info & BIT(HNS3_RXD_VLD_B)))
  2231. return -ENXIO;
  2232. if (unlikely(ring->frag_num >= MAX_SKB_FRAGS)) {
  2233. new_skb = napi_alloc_skb(&ring->tqp_vector->napi,
  2234. HNS3_RX_HEAD_SIZE);
  2235. if (unlikely(!new_skb)) {
  2236. hns3_rl_err(ring->tqp_vector->napi.dev,
  2237. "alloc rx fraglist skb fail\n");
  2238. return -ENXIO;
  2239. }
  2240. ring->frag_num = 0;
  2241. if (ring->tail_skb) {
  2242. ring->tail_skb->next = new_skb;
  2243. ring->tail_skb = new_skb;
  2244. } else {
  2245. skb_shinfo(skb)->frag_list = new_skb;
  2246. ring->tail_skb = new_skb;
  2247. }
  2248. }
  2249. if (ring->tail_skb) {
  2250. head_skb->truesize += hns3_buf_size(ring);
  2251. head_skb->data_len += le16_to_cpu(desc->rx.size);
  2252. head_skb->len += le16_to_cpu(desc->rx.size);
  2253. skb = ring->tail_skb;
  2254. }
  2255. hns3_nic_reuse_page(skb, ring->frag_num++, ring, 0, desc_cb);
  2256. ring_ptr_move_fw(ring, next_to_clean);
  2257. ring->pending_buf++;
  2258. }
  2259. return 0;
  2260. }
  2261. static int hns3_set_gro_and_checksum(struct hns3_enet_ring *ring,
  2262. struct sk_buff *skb, u32 l234info,
  2263. u32 bd_base_info, u32 ol_info)
  2264. {
  2265. u32 l3_type;
  2266. skb_shinfo(skb)->gso_size = hnae3_get_field(bd_base_info,
  2267. HNS3_RXD_GRO_SIZE_M,
  2268. HNS3_RXD_GRO_SIZE_S);
  2269. /* if there is no HW GRO, do not set gro params */
  2270. if (!skb_shinfo(skb)->gso_size) {
  2271. hns3_rx_checksum(ring, skb, l234info, bd_base_info, ol_info);
  2272. return 0;
  2273. }
  2274. NAPI_GRO_CB(skb)->count = hnae3_get_field(l234info,
  2275. HNS3_RXD_GRO_COUNT_M,
  2276. HNS3_RXD_GRO_COUNT_S);
  2277. l3_type = hnae3_get_field(l234info, HNS3_RXD_L3ID_M, HNS3_RXD_L3ID_S);
  2278. if (l3_type == HNS3_L3_TYPE_IPV4)
  2279. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  2280. else if (l3_type == HNS3_L3_TYPE_IPV6)
  2281. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  2282. else
  2283. return -EFAULT;
  2284. return hns3_gro_complete(skb, l234info);
  2285. }
  2286. static void hns3_set_rx_skb_rss_type(struct hns3_enet_ring *ring,
  2287. struct sk_buff *skb, u32 rss_hash)
  2288. {
  2289. struct hnae3_handle *handle = ring->tqp->handle;
  2290. enum pkt_hash_types rss_type;
  2291. if (rss_hash)
  2292. rss_type = handle->kinfo.rss_type;
  2293. else
  2294. rss_type = PKT_HASH_TYPE_NONE;
  2295. skb_set_hash(skb, rss_hash, rss_type);
  2296. }
  2297. static int hns3_handle_bdinfo(struct hns3_enet_ring *ring, struct sk_buff *skb)
  2298. {
  2299. struct net_device *netdev = ring->tqp->handle->kinfo.netdev;
  2300. enum hns3_pkt_l2t_type l2_frame_type;
  2301. u32 bd_base_info, l234info, ol_info;
  2302. struct hns3_desc *desc;
  2303. unsigned int len;
  2304. int pre_ntc, ret;
  2305. /* bdinfo handled below is only valid on the last BD of the
  2306. * current packet, and ring->next_to_clean indicates the first
  2307. * descriptor of next packet, so need - 1 below.
  2308. */
  2309. pre_ntc = ring->next_to_clean ? (ring->next_to_clean - 1) :
  2310. (ring->desc_num - 1);
  2311. desc = &ring->desc[pre_ntc];
  2312. bd_base_info = le32_to_cpu(desc->rx.bd_base_info);
  2313. l234info = le32_to_cpu(desc->rx.l234_info);
  2314. ol_info = le32_to_cpu(desc->rx.ol_info);
  2315. /* Based on hw strategy, the tag offloaded will be stored at
  2316. * ot_vlan_tag in two layer tag case, and stored at vlan_tag
  2317. * in one layer tag case.
  2318. */
  2319. if (netdev->features & NETIF_F_HW_VLAN_CTAG_RX) {
  2320. u16 vlan_tag;
  2321. if (hns3_parse_vlan_tag(ring, desc, l234info, &vlan_tag))
  2322. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  2323. vlan_tag);
  2324. }
  2325. if (unlikely(!desc->rx.pkt_len || (l234info & (BIT(HNS3_RXD_TRUNCAT_B) |
  2326. BIT(HNS3_RXD_L2E_B))))) {
  2327. u64_stats_update_begin(&ring->syncp);
  2328. if (l234info & BIT(HNS3_RXD_L2E_B))
  2329. ring->stats.l2_err++;
  2330. else
  2331. ring->stats.err_pkt_len++;
  2332. u64_stats_update_end(&ring->syncp);
  2333. return -EFAULT;
  2334. }
  2335. len = skb->len;
  2336. /* Do update ip stack process */
  2337. skb->protocol = eth_type_trans(skb, netdev);
  2338. /* This is needed in order to enable forwarding support */
  2339. ret = hns3_set_gro_and_checksum(ring, skb, l234info,
  2340. bd_base_info, ol_info);
  2341. if (unlikely(ret)) {
  2342. u64_stats_update_begin(&ring->syncp);
  2343. ring->stats.rx_err_cnt++;
  2344. u64_stats_update_end(&ring->syncp);
  2345. return ret;
  2346. }
  2347. l2_frame_type = hnae3_get_field(l234info, HNS3_RXD_DMAC_M,
  2348. HNS3_RXD_DMAC_S);
  2349. u64_stats_update_begin(&ring->syncp);
  2350. ring->stats.rx_pkts++;
  2351. ring->stats.rx_bytes += len;
  2352. if (l2_frame_type == HNS3_L2_TYPE_MULTICAST)
  2353. ring->stats.rx_multicast++;
  2354. u64_stats_update_end(&ring->syncp);
  2355. ring->tqp_vector->rx_group.total_bytes += len;
  2356. hns3_set_rx_skb_rss_type(ring, skb, le32_to_cpu(desc->rx.rss_hash));
  2357. return 0;
  2358. }
  2359. static int hns3_handle_rx_bd(struct hns3_enet_ring *ring,
  2360. struct sk_buff **out_skb)
  2361. {
  2362. struct sk_buff *skb = ring->skb;
  2363. struct hns3_desc_cb *desc_cb;
  2364. struct hns3_desc *desc;
  2365. unsigned int length;
  2366. u32 bd_base_info;
  2367. int ret;
  2368. desc = &ring->desc[ring->next_to_clean];
  2369. desc_cb = &ring->desc_cb[ring->next_to_clean];
  2370. prefetch(desc);
  2371. length = le16_to_cpu(desc->rx.size);
  2372. bd_base_info = le32_to_cpu(desc->rx.bd_base_info);
  2373. /* Check valid BD */
  2374. if (unlikely(!(bd_base_info & BIT(HNS3_RXD_VLD_B))))
  2375. return -ENXIO;
  2376. if (!skb)
  2377. ring->va = (unsigned char *)desc_cb->buf + desc_cb->page_offset;
  2378. /* Prefetch first cache line of first page
  2379. * Idea is to cache few bytes of the header of the packet. Our L1 Cache
  2380. * line size is 64B so need to prefetch twice to make it 128B. But in
  2381. * actual we can have greater size of caches with 128B Level 1 cache
  2382. * lines. In such a case, single fetch would suffice to cache in the
  2383. * relevant part of the header.
  2384. */
  2385. prefetch(ring->va);
  2386. #if L1_CACHE_BYTES < 128
  2387. prefetch(ring->va + L1_CACHE_BYTES);
  2388. #endif
  2389. if (!skb) {
  2390. ret = hns3_alloc_skb(ring, length, ring->va);
  2391. *out_skb = skb = ring->skb;
  2392. if (ret < 0) /* alloc buffer fail */
  2393. return ret;
  2394. if (ret > 0) { /* need add frag */
  2395. ret = hns3_add_frag(ring, desc, &skb, false);
  2396. if (ret)
  2397. return ret;
  2398. /* As the head data may be changed when GRO enable, copy
  2399. * the head data in after other data rx completed
  2400. */
  2401. memcpy(skb->data, ring->va,
  2402. ALIGN(ring->pull_len, sizeof(long)));
  2403. }
  2404. } else {
  2405. ret = hns3_add_frag(ring, desc, &skb, true);
  2406. if (ret)
  2407. return ret;
  2408. /* As the head data may be changed when GRO enable, copy
  2409. * the head data in after other data rx completed
  2410. */
  2411. memcpy(skb->data, ring->va,
  2412. ALIGN(ring->pull_len, sizeof(long)));
  2413. }
  2414. ret = hns3_handle_bdinfo(ring, skb);
  2415. if (unlikely(ret)) {
  2416. dev_kfree_skb_any(skb);
  2417. return ret;
  2418. }
  2419. skb_record_rx_queue(skb, ring->tqp->tqp_index);
  2420. *out_skb = skb;
  2421. return 0;
  2422. }
  2423. int hns3_clean_rx_ring(struct hns3_enet_ring *ring, int budget,
  2424. void (*rx_fn)(struct hns3_enet_ring *, struct sk_buff *))
  2425. {
  2426. #define RCB_NOF_ALLOC_RX_BUFF_ONCE 16
  2427. int unused_count = hns3_desc_unused(ring);
  2428. struct sk_buff *skb = ring->skb;
  2429. int recv_pkts = 0;
  2430. int recv_bds = 0;
  2431. int err, num;
  2432. num = readl_relaxed(ring->tqp->io_base + HNS3_RING_RX_RING_FBDNUM_REG);
  2433. rmb(); /* Make sure num taken effect before the other data is touched */
  2434. num -= unused_count;
  2435. unused_count -= ring->pending_buf;
  2436. while (recv_pkts < budget && recv_bds < num) {
  2437. /* Reuse or realloc buffers */
  2438. if (unused_count >= RCB_NOF_ALLOC_RX_BUFF_ONCE) {
  2439. hns3_nic_alloc_rx_buffers(ring, unused_count);
  2440. unused_count = hns3_desc_unused(ring) -
  2441. ring->pending_buf;
  2442. }
  2443. /* Poll one pkt */
  2444. err = hns3_handle_rx_bd(ring, &skb);
  2445. if (unlikely(!skb)) /* This fault cannot be repaired */
  2446. goto out;
  2447. if (err == -ENXIO) { /* Do not get FE for the packet */
  2448. goto out;
  2449. } else if (unlikely(err)) { /* Do jump the err */
  2450. recv_bds += ring->pending_buf;
  2451. unused_count += ring->pending_buf;
  2452. ring->skb = NULL;
  2453. ring->pending_buf = 0;
  2454. continue;
  2455. }
  2456. rx_fn(ring, skb);
  2457. recv_bds += ring->pending_buf;
  2458. unused_count += ring->pending_buf;
  2459. ring->skb = NULL;
  2460. ring->pending_buf = 0;
  2461. recv_pkts++;
  2462. }
  2463. out:
  2464. /* Make all data has been write before submit */
  2465. if (unused_count > 0)
  2466. hns3_nic_alloc_rx_buffers(ring, unused_count);
  2467. return recv_pkts;
  2468. }
  2469. static bool hns3_get_new_flow_lvl(struct hns3_enet_ring_group *ring_group)
  2470. {
  2471. #define HNS3_RX_LOW_BYTE_RATE 10000
  2472. #define HNS3_RX_MID_BYTE_RATE 20000
  2473. #define HNS3_RX_ULTRA_PACKET_RATE 40
  2474. enum hns3_flow_level_range new_flow_level;
  2475. struct hns3_enet_tqp_vector *tqp_vector;
  2476. int packets_per_msecs, bytes_per_msecs;
  2477. u32 time_passed_ms;
  2478. tqp_vector = ring_group->ring->tqp_vector;
  2479. time_passed_ms =
  2480. jiffies_to_msecs(jiffies - tqp_vector->last_jiffies);
  2481. if (!time_passed_ms)
  2482. return false;
  2483. do_div(ring_group->total_packets, time_passed_ms);
  2484. packets_per_msecs = ring_group->total_packets;
  2485. do_div(ring_group->total_bytes, time_passed_ms);
  2486. bytes_per_msecs = ring_group->total_bytes;
  2487. new_flow_level = ring_group->coal.flow_level;
  2488. /* Simple throttlerate management
  2489. * 0-10MB/s lower (50000 ints/s)
  2490. * 10-20MB/s middle (20000 ints/s)
  2491. * 20-1249MB/s high (18000 ints/s)
  2492. * > 40000pps ultra (8000 ints/s)
  2493. */
  2494. switch (new_flow_level) {
  2495. case HNS3_FLOW_LOW:
  2496. if (bytes_per_msecs > HNS3_RX_LOW_BYTE_RATE)
  2497. new_flow_level = HNS3_FLOW_MID;
  2498. break;
  2499. case HNS3_FLOW_MID:
  2500. if (bytes_per_msecs > HNS3_RX_MID_BYTE_RATE)
  2501. new_flow_level = HNS3_FLOW_HIGH;
  2502. else if (bytes_per_msecs <= HNS3_RX_LOW_BYTE_RATE)
  2503. new_flow_level = HNS3_FLOW_LOW;
  2504. break;
  2505. case HNS3_FLOW_HIGH:
  2506. case HNS3_FLOW_ULTRA:
  2507. default:
  2508. if (bytes_per_msecs <= HNS3_RX_MID_BYTE_RATE)
  2509. new_flow_level = HNS3_FLOW_MID;
  2510. break;
  2511. }
  2512. if (packets_per_msecs > HNS3_RX_ULTRA_PACKET_RATE &&
  2513. &tqp_vector->rx_group == ring_group)
  2514. new_flow_level = HNS3_FLOW_ULTRA;
  2515. ring_group->total_bytes = 0;
  2516. ring_group->total_packets = 0;
  2517. ring_group->coal.flow_level = new_flow_level;
  2518. return true;
  2519. }
  2520. static bool hns3_get_new_int_gl(struct hns3_enet_ring_group *ring_group)
  2521. {
  2522. struct hns3_enet_tqp_vector *tqp_vector;
  2523. u16 new_int_gl;
  2524. if (!ring_group->ring)
  2525. return false;
  2526. tqp_vector = ring_group->ring->tqp_vector;
  2527. if (!tqp_vector->last_jiffies)
  2528. return false;
  2529. if (ring_group->total_packets == 0) {
  2530. ring_group->coal.int_gl = HNS3_INT_GL_50K;
  2531. ring_group->coal.flow_level = HNS3_FLOW_LOW;
  2532. return true;
  2533. }
  2534. if (!hns3_get_new_flow_lvl(ring_group))
  2535. return false;
  2536. new_int_gl = ring_group->coal.int_gl;
  2537. switch (ring_group->coal.flow_level) {
  2538. case HNS3_FLOW_LOW:
  2539. new_int_gl = HNS3_INT_GL_50K;
  2540. break;
  2541. case HNS3_FLOW_MID:
  2542. new_int_gl = HNS3_INT_GL_20K;
  2543. break;
  2544. case HNS3_FLOW_HIGH:
  2545. new_int_gl = HNS3_INT_GL_18K;
  2546. break;
  2547. case HNS3_FLOW_ULTRA:
  2548. new_int_gl = HNS3_INT_GL_8K;
  2549. break;
  2550. default:
  2551. break;
  2552. }
  2553. if (new_int_gl != ring_group->coal.int_gl) {
  2554. ring_group->coal.int_gl = new_int_gl;
  2555. return true;
  2556. }
  2557. return false;
  2558. }
  2559. static void hns3_update_new_int_gl(struct hns3_enet_tqp_vector *tqp_vector)
  2560. {
  2561. struct hns3_enet_ring_group *rx_group = &tqp_vector->rx_group;
  2562. struct hns3_enet_ring_group *tx_group = &tqp_vector->tx_group;
  2563. bool rx_update, tx_update;
  2564. /* update param every 1000ms */
  2565. if (time_before(jiffies,
  2566. tqp_vector->last_jiffies + msecs_to_jiffies(1000)))
  2567. return;
  2568. if (rx_group->coal.gl_adapt_enable) {
  2569. rx_update = hns3_get_new_int_gl(rx_group);
  2570. if (rx_update)
  2571. hns3_set_vector_coalesce_rx_gl(tqp_vector,
  2572. rx_group->coal.int_gl);
  2573. }
  2574. if (tx_group->coal.gl_adapt_enable) {
  2575. tx_update = hns3_get_new_int_gl(tx_group);
  2576. if (tx_update)
  2577. hns3_set_vector_coalesce_tx_gl(tqp_vector,
  2578. tx_group->coal.int_gl);
  2579. }
  2580. tqp_vector->last_jiffies = jiffies;
  2581. }
  2582. static int hns3_nic_common_poll(struct napi_struct *napi, int budget)
  2583. {
  2584. struct hns3_nic_priv *priv = netdev_priv(napi->dev);
  2585. struct hns3_enet_ring *ring;
  2586. int rx_pkt_total = 0;
  2587. struct hns3_enet_tqp_vector *tqp_vector =
  2588. container_of(napi, struct hns3_enet_tqp_vector, napi);
  2589. bool clean_complete = true;
  2590. int rx_budget = budget;
  2591. if (unlikely(test_bit(HNS3_NIC_STATE_DOWN, &priv->state))) {
  2592. napi_complete(napi);
  2593. return 0;
  2594. }
  2595. /* Since the actual Tx work is minimal, we can give the Tx a larger
  2596. * budget and be more aggressive about cleaning up the Tx descriptors.
  2597. */
  2598. hns3_for_each_ring(ring, tqp_vector->tx_group)
  2599. hns3_clean_tx_ring(ring);
  2600. /* make sure rx ring budget not smaller than 1 */
  2601. if (tqp_vector->num_tqps > 1)
  2602. rx_budget = max(budget / tqp_vector->num_tqps, 1);
  2603. hns3_for_each_ring(ring, tqp_vector->rx_group) {
  2604. int rx_cleaned = hns3_clean_rx_ring(ring, rx_budget,
  2605. hns3_rx_skb);
  2606. if (rx_cleaned >= rx_budget)
  2607. clean_complete = false;
  2608. rx_pkt_total += rx_cleaned;
  2609. }
  2610. tqp_vector->rx_group.total_packets += rx_pkt_total;
  2611. if (!clean_complete)
  2612. return budget;
  2613. if (napi_complete(napi) &&
  2614. likely(!test_bit(HNS3_NIC_STATE_DOWN, &priv->state))) {
  2615. hns3_update_new_int_gl(tqp_vector);
  2616. hns3_mask_vector_irq(tqp_vector, 1);
  2617. }
  2618. return rx_pkt_total;
  2619. }
  2620. static int hns3_get_vector_ring_chain(struct hns3_enet_tqp_vector *tqp_vector,
  2621. struct hnae3_ring_chain_node *head)
  2622. {
  2623. struct pci_dev *pdev = tqp_vector->handle->pdev;
  2624. struct hnae3_ring_chain_node *cur_chain = head;
  2625. struct hnae3_ring_chain_node *chain;
  2626. struct hns3_enet_ring *tx_ring;
  2627. struct hns3_enet_ring *rx_ring;
  2628. tx_ring = tqp_vector->tx_group.ring;
  2629. if (tx_ring) {
  2630. cur_chain->tqp_index = tx_ring->tqp->tqp_index;
  2631. hnae3_set_bit(cur_chain->flag, HNAE3_RING_TYPE_B,
  2632. HNAE3_RING_TYPE_TX);
  2633. hnae3_set_field(cur_chain->int_gl_idx, HNAE3_RING_GL_IDX_M,
  2634. HNAE3_RING_GL_IDX_S, HNAE3_RING_GL_TX);
  2635. cur_chain->next = NULL;
  2636. while (tx_ring->next) {
  2637. tx_ring = tx_ring->next;
  2638. chain = devm_kzalloc(&pdev->dev, sizeof(*chain),
  2639. GFP_KERNEL);
  2640. if (!chain)
  2641. goto err_free_chain;
  2642. cur_chain->next = chain;
  2643. chain->tqp_index = tx_ring->tqp->tqp_index;
  2644. hnae3_set_bit(chain->flag, HNAE3_RING_TYPE_B,
  2645. HNAE3_RING_TYPE_TX);
  2646. hnae3_set_field(chain->int_gl_idx,
  2647. HNAE3_RING_GL_IDX_M,
  2648. HNAE3_RING_GL_IDX_S,
  2649. HNAE3_RING_GL_TX);
  2650. cur_chain = chain;
  2651. }
  2652. }
  2653. rx_ring = tqp_vector->rx_group.ring;
  2654. if (!tx_ring && rx_ring) {
  2655. cur_chain->next = NULL;
  2656. cur_chain->tqp_index = rx_ring->tqp->tqp_index;
  2657. hnae3_set_bit(cur_chain->flag, HNAE3_RING_TYPE_B,
  2658. HNAE3_RING_TYPE_RX);
  2659. hnae3_set_field(cur_chain->int_gl_idx, HNAE3_RING_GL_IDX_M,
  2660. HNAE3_RING_GL_IDX_S, HNAE3_RING_GL_RX);
  2661. rx_ring = rx_ring->next;
  2662. }
  2663. while (rx_ring) {
  2664. chain = devm_kzalloc(&pdev->dev, sizeof(*chain), GFP_KERNEL);
  2665. if (!chain)
  2666. goto err_free_chain;
  2667. cur_chain->next = chain;
  2668. chain->tqp_index = rx_ring->tqp->tqp_index;
  2669. hnae3_set_bit(chain->flag, HNAE3_RING_TYPE_B,
  2670. HNAE3_RING_TYPE_RX);
  2671. hnae3_set_field(chain->int_gl_idx, HNAE3_RING_GL_IDX_M,
  2672. HNAE3_RING_GL_IDX_S, HNAE3_RING_GL_RX);
  2673. cur_chain = chain;
  2674. rx_ring = rx_ring->next;
  2675. }
  2676. return 0;
  2677. err_free_chain:
  2678. cur_chain = head->next;
  2679. while (cur_chain) {
  2680. chain = cur_chain->next;
  2681. devm_kfree(&pdev->dev, cur_chain);
  2682. cur_chain = chain;
  2683. }
  2684. head->next = NULL;
  2685. return -ENOMEM;
  2686. }
  2687. static void hns3_free_vector_ring_chain(struct hns3_enet_tqp_vector *tqp_vector,
  2688. struct hnae3_ring_chain_node *head)
  2689. {
  2690. struct pci_dev *pdev = tqp_vector->handle->pdev;
  2691. struct hnae3_ring_chain_node *chain_tmp, *chain;
  2692. chain = head->next;
  2693. while (chain) {
  2694. chain_tmp = chain->next;
  2695. devm_kfree(&pdev->dev, chain);
  2696. chain = chain_tmp;
  2697. }
  2698. }
  2699. static void hns3_add_ring_to_group(struct hns3_enet_ring_group *group,
  2700. struct hns3_enet_ring *ring)
  2701. {
  2702. ring->next = group->ring;
  2703. group->ring = ring;
  2704. group->count++;
  2705. }
  2706. static void hns3_nic_set_cpumask(struct hns3_nic_priv *priv)
  2707. {
  2708. struct pci_dev *pdev = priv->ae_handle->pdev;
  2709. struct hns3_enet_tqp_vector *tqp_vector;
  2710. int num_vectors = priv->vector_num;
  2711. int numa_node;
  2712. int vector_i;
  2713. numa_node = dev_to_node(&pdev->dev);
  2714. for (vector_i = 0; vector_i < num_vectors; vector_i++) {
  2715. tqp_vector = &priv->tqp_vector[vector_i];
  2716. cpumask_set_cpu(cpumask_local_spread(vector_i, numa_node),
  2717. &tqp_vector->affinity_mask);
  2718. }
  2719. }
  2720. static int hns3_nic_init_vector_data(struct hns3_nic_priv *priv)
  2721. {
  2722. struct hnae3_handle *h = priv->ae_handle;
  2723. struct hns3_enet_tqp_vector *tqp_vector;
  2724. int ret = 0;
  2725. int i;
  2726. hns3_nic_set_cpumask(priv);
  2727. for (i = 0; i < priv->vector_num; i++) {
  2728. tqp_vector = &priv->tqp_vector[i];
  2729. hns3_vector_gl_rl_init_hw(tqp_vector, priv);
  2730. tqp_vector->num_tqps = 0;
  2731. }
  2732. for (i = 0; i < h->kinfo.num_tqps; i++) {
  2733. u16 vector_i = i % priv->vector_num;
  2734. u16 tqp_num = h->kinfo.num_tqps;
  2735. tqp_vector = &priv->tqp_vector[vector_i];
  2736. hns3_add_ring_to_group(&tqp_vector->tx_group,
  2737. priv->ring_data[i].ring);
  2738. hns3_add_ring_to_group(&tqp_vector->rx_group,
  2739. priv->ring_data[i + tqp_num].ring);
  2740. priv->ring_data[i].ring->tqp_vector = tqp_vector;
  2741. priv->ring_data[i + tqp_num].ring->tqp_vector = tqp_vector;
  2742. tqp_vector->num_tqps++;
  2743. }
  2744. for (i = 0; i < priv->vector_num; i++) {
  2745. struct hnae3_ring_chain_node vector_ring_chain;
  2746. tqp_vector = &priv->tqp_vector[i];
  2747. tqp_vector->rx_group.total_bytes = 0;
  2748. tqp_vector->rx_group.total_packets = 0;
  2749. tqp_vector->tx_group.total_bytes = 0;
  2750. tqp_vector->tx_group.total_packets = 0;
  2751. tqp_vector->handle = h;
  2752. ret = hns3_get_vector_ring_chain(tqp_vector,
  2753. &vector_ring_chain);
  2754. if (ret)
  2755. goto map_ring_fail;
  2756. ret = h->ae_algo->ops->map_ring_to_vector(h,
  2757. tqp_vector->vector_irq, &vector_ring_chain);
  2758. hns3_free_vector_ring_chain(tqp_vector, &vector_ring_chain);
  2759. if (ret)
  2760. goto map_ring_fail;
  2761. netif_napi_add(priv->netdev, &tqp_vector->napi,
  2762. hns3_nic_common_poll, NAPI_POLL_WEIGHT);
  2763. }
  2764. return 0;
  2765. map_ring_fail:
  2766. while (i--)
  2767. netif_napi_del(&priv->tqp_vector[i].napi);
  2768. return ret;
  2769. }
  2770. static int hns3_nic_alloc_vector_data(struct hns3_nic_priv *priv)
  2771. {
  2772. #define HNS3_VECTOR_PF_MAX_NUM 64
  2773. struct hnae3_handle *h = priv->ae_handle;
  2774. struct hns3_enet_tqp_vector *tqp_vector;
  2775. struct hnae3_vector_info *vector;
  2776. struct pci_dev *pdev = h->pdev;
  2777. u16 tqp_num = h->kinfo.num_tqps;
  2778. u16 vector_num;
  2779. int ret = 0;
  2780. u16 i;
  2781. /* RSS size, cpu online and vector_num should be the same */
  2782. /* Should consider 2p/4p later */
  2783. vector_num = min_t(u16, num_online_cpus(), tqp_num);
  2784. vector_num = min_t(u16, vector_num, HNS3_VECTOR_PF_MAX_NUM);
  2785. vector = devm_kcalloc(&pdev->dev, vector_num, sizeof(*vector),
  2786. GFP_KERNEL);
  2787. if (!vector)
  2788. return -ENOMEM;
  2789. /* save the actual available vector number */
  2790. vector_num = h->ae_algo->ops->get_vector(h, vector_num, vector);
  2791. priv->vector_num = vector_num;
  2792. priv->tqp_vector = (struct hns3_enet_tqp_vector *)
  2793. devm_kcalloc(&pdev->dev, vector_num, sizeof(*priv->tqp_vector),
  2794. GFP_KERNEL);
  2795. if (!priv->tqp_vector) {
  2796. ret = -ENOMEM;
  2797. goto out;
  2798. }
  2799. for (i = 0; i < priv->vector_num; i++) {
  2800. tqp_vector = &priv->tqp_vector[i];
  2801. tqp_vector->idx = i;
  2802. tqp_vector->mask_addr = vector[i].io_addr;
  2803. tqp_vector->vector_irq = vector[i].vector;
  2804. hns3_vector_gl_rl_init(tqp_vector, priv);
  2805. }
  2806. out:
  2807. devm_kfree(&pdev->dev, vector);
  2808. return ret;
  2809. }
  2810. static void hns3_clear_ring_group(struct hns3_enet_ring_group *group)
  2811. {
  2812. group->ring = NULL;
  2813. group->count = 0;
  2814. }
  2815. static void hns3_nic_uninit_vector_data(struct hns3_nic_priv *priv)
  2816. {
  2817. struct hnae3_ring_chain_node vector_ring_chain;
  2818. struct hnae3_handle *h = priv->ae_handle;
  2819. struct hns3_enet_tqp_vector *tqp_vector;
  2820. int i;
  2821. for (i = 0; i < priv->vector_num; i++) {
  2822. tqp_vector = &priv->tqp_vector[i];
  2823. if (!tqp_vector->rx_group.ring && !tqp_vector->tx_group.ring)
  2824. continue;
  2825. hns3_get_vector_ring_chain(tqp_vector, &vector_ring_chain);
  2826. h->ae_algo->ops->unmap_ring_from_vector(h,
  2827. tqp_vector->vector_irq, &vector_ring_chain);
  2828. hns3_free_vector_ring_chain(tqp_vector, &vector_ring_chain);
  2829. if (tqp_vector->irq_init_flag == HNS3_VECTOR_INITED) {
  2830. irq_set_affinity_hint(tqp_vector->vector_irq, NULL);
  2831. free_irq(tqp_vector->vector_irq, tqp_vector);
  2832. tqp_vector->irq_init_flag = HNS3_VECTOR_NOT_INITED;
  2833. }
  2834. hns3_clear_ring_group(&tqp_vector->rx_group);
  2835. hns3_clear_ring_group(&tqp_vector->tx_group);
  2836. netif_napi_del(&priv->tqp_vector[i].napi);
  2837. }
  2838. }
  2839. static int hns3_nic_dealloc_vector_data(struct hns3_nic_priv *priv)
  2840. {
  2841. struct hnae3_handle *h = priv->ae_handle;
  2842. struct pci_dev *pdev = h->pdev;
  2843. int i, ret;
  2844. for (i = 0; i < priv->vector_num; i++) {
  2845. struct hns3_enet_tqp_vector *tqp_vector;
  2846. tqp_vector = &priv->tqp_vector[i];
  2847. ret = h->ae_algo->ops->put_vector(h, tqp_vector->vector_irq);
  2848. if (ret)
  2849. return ret;
  2850. }
  2851. devm_kfree(&pdev->dev, priv->tqp_vector);
  2852. return 0;
  2853. }
  2854. static int hns3_ring_get_cfg(struct hnae3_queue *q, struct hns3_nic_priv *priv,
  2855. unsigned int ring_type)
  2856. {
  2857. struct hns3_nic_ring_data *ring_data = priv->ring_data;
  2858. int queue_num = priv->ae_handle->kinfo.num_tqps;
  2859. struct pci_dev *pdev = priv->ae_handle->pdev;
  2860. struct hns3_enet_ring *ring;
  2861. int desc_num;
  2862. ring = devm_kzalloc(&pdev->dev, sizeof(*ring), GFP_KERNEL);
  2863. if (!ring)
  2864. return -ENOMEM;
  2865. if (ring_type == HNAE3_RING_TYPE_TX) {
  2866. desc_num = priv->ae_handle->kinfo.num_tx_desc;
  2867. ring_data[q->tqp_index].ring = ring;
  2868. ring_data[q->tqp_index].queue_index = q->tqp_index;
  2869. ring->io_base = (u8 __iomem *)q->io_base + HNS3_TX_REG_OFFSET;
  2870. } else {
  2871. desc_num = priv->ae_handle->kinfo.num_rx_desc;
  2872. ring_data[q->tqp_index + queue_num].ring = ring;
  2873. ring_data[q->tqp_index + queue_num].queue_index = q->tqp_index;
  2874. ring->io_base = q->io_base;
  2875. }
  2876. hnae3_set_bit(ring->flag, HNAE3_RING_TYPE_B, ring_type);
  2877. ring->tqp = q;
  2878. ring->desc = NULL;
  2879. ring->desc_cb = NULL;
  2880. ring->dev = priv->dev;
  2881. ring->desc_dma_addr = 0;
  2882. ring->buf_size = q->buf_size;
  2883. ring->desc_num = desc_num;
  2884. ring->next_to_use = 0;
  2885. ring->next_to_clean = 0;
  2886. return 0;
  2887. }
  2888. static int hns3_queue_to_ring(struct hnae3_queue *tqp,
  2889. struct hns3_nic_priv *priv)
  2890. {
  2891. int ret;
  2892. ret = hns3_ring_get_cfg(tqp, priv, HNAE3_RING_TYPE_TX);
  2893. if (ret)
  2894. return ret;
  2895. ret = hns3_ring_get_cfg(tqp, priv, HNAE3_RING_TYPE_RX);
  2896. if (ret) {
  2897. devm_kfree(priv->dev, priv->ring_data[tqp->tqp_index].ring);
  2898. return ret;
  2899. }
  2900. return 0;
  2901. }
  2902. static int hns3_get_ring_config(struct hns3_nic_priv *priv)
  2903. {
  2904. struct hnae3_handle *h = priv->ae_handle;
  2905. struct pci_dev *pdev = h->pdev;
  2906. int i, ret;
  2907. priv->ring_data = devm_kzalloc(&pdev->dev,
  2908. array3_size(h->kinfo.num_tqps,
  2909. sizeof(*priv->ring_data),
  2910. 2),
  2911. GFP_KERNEL);
  2912. if (!priv->ring_data)
  2913. return -ENOMEM;
  2914. for (i = 0; i < h->kinfo.num_tqps; i++) {
  2915. ret = hns3_queue_to_ring(h->kinfo.tqp[i], priv);
  2916. if (ret)
  2917. goto err;
  2918. }
  2919. return 0;
  2920. err:
  2921. while (i--) {
  2922. devm_kfree(priv->dev, priv->ring_data[i].ring);
  2923. devm_kfree(priv->dev,
  2924. priv->ring_data[i + h->kinfo.num_tqps].ring);
  2925. }
  2926. devm_kfree(&pdev->dev, priv->ring_data);
  2927. priv->ring_data = NULL;
  2928. return ret;
  2929. }
  2930. static void hns3_put_ring_config(struct hns3_nic_priv *priv)
  2931. {
  2932. struct hnae3_handle *h = priv->ae_handle;
  2933. int i;
  2934. if (!priv->ring_data)
  2935. return;
  2936. for (i = 0; i < h->kinfo.num_tqps; i++) {
  2937. devm_kfree(priv->dev, priv->ring_data[i].ring);
  2938. devm_kfree(priv->dev,
  2939. priv->ring_data[i + h->kinfo.num_tqps].ring);
  2940. }
  2941. devm_kfree(priv->dev, priv->ring_data);
  2942. priv->ring_data = NULL;
  2943. }
  2944. static int hns3_alloc_ring_memory(struct hns3_enet_ring *ring)
  2945. {
  2946. int ret;
  2947. if (ring->desc_num <= 0 || ring->buf_size <= 0)
  2948. return -EINVAL;
  2949. ring->desc_cb = devm_kcalloc(ring_to_dev(ring), ring->desc_num,
  2950. sizeof(ring->desc_cb[0]), GFP_KERNEL);
  2951. if (!ring->desc_cb) {
  2952. ret = -ENOMEM;
  2953. goto out;
  2954. }
  2955. ret = hns3_alloc_desc(ring);
  2956. if (ret)
  2957. goto out_with_desc_cb;
  2958. if (!HNAE3_IS_TX_RING(ring)) {
  2959. ret = hns3_alloc_ring_buffers(ring);
  2960. if (ret)
  2961. goto out_with_desc;
  2962. }
  2963. return 0;
  2964. out_with_desc:
  2965. hns3_free_desc(ring);
  2966. out_with_desc_cb:
  2967. devm_kfree(ring_to_dev(ring), ring->desc_cb);
  2968. ring->desc_cb = NULL;
  2969. out:
  2970. return ret;
  2971. }
  2972. void hns3_fini_ring(struct hns3_enet_ring *ring)
  2973. {
  2974. hns3_free_desc(ring);
  2975. devm_kfree(ring_to_dev(ring), ring->desc_cb);
  2976. ring->desc_cb = NULL;
  2977. ring->next_to_clean = 0;
  2978. ring->next_to_use = 0;
  2979. ring->pending_buf = 0;
  2980. if (ring->skb) {
  2981. dev_kfree_skb_any(ring->skb);
  2982. ring->skb = NULL;
  2983. }
  2984. }
  2985. static int hns3_buf_size2type(u32 buf_size)
  2986. {
  2987. int bd_size_type;
  2988. switch (buf_size) {
  2989. case 512:
  2990. bd_size_type = HNS3_BD_SIZE_512_TYPE;
  2991. break;
  2992. case 1024:
  2993. bd_size_type = HNS3_BD_SIZE_1024_TYPE;
  2994. break;
  2995. case 2048:
  2996. bd_size_type = HNS3_BD_SIZE_2048_TYPE;
  2997. break;
  2998. case 4096:
  2999. bd_size_type = HNS3_BD_SIZE_4096_TYPE;
  3000. break;
  3001. default:
  3002. bd_size_type = HNS3_BD_SIZE_2048_TYPE;
  3003. }
  3004. return bd_size_type;
  3005. }
  3006. static void hns3_init_ring_hw(struct hns3_enet_ring *ring)
  3007. {
  3008. dma_addr_t dma = ring->desc_dma_addr;
  3009. struct hnae3_queue *q = ring->tqp;
  3010. if (!HNAE3_IS_TX_RING(ring)) {
  3011. hns3_write_dev(q, HNS3_RING_RX_RING_BASEADDR_L_REG, (u32)dma);
  3012. hns3_write_dev(q, HNS3_RING_RX_RING_BASEADDR_H_REG,
  3013. (u32)((dma >> 31) >> 1));
  3014. hns3_write_dev(q, HNS3_RING_RX_RING_BD_LEN_REG,
  3015. hns3_buf_size2type(ring->buf_size));
  3016. hns3_write_dev(q, HNS3_RING_RX_RING_BD_NUM_REG,
  3017. ring->desc_num / 8 - 1);
  3018. } else {
  3019. hns3_write_dev(q, HNS3_RING_TX_RING_BASEADDR_L_REG,
  3020. (u32)dma);
  3021. hns3_write_dev(q, HNS3_RING_TX_RING_BASEADDR_H_REG,
  3022. (u32)((dma >> 31) >> 1));
  3023. hns3_write_dev(q, HNS3_RING_TX_RING_BD_NUM_REG,
  3024. ring->desc_num / 8 - 1);
  3025. }
  3026. }
  3027. static void hns3_init_tx_ring_tc(struct hns3_nic_priv *priv)
  3028. {
  3029. struct hnae3_knic_private_info *kinfo = &priv->ae_handle->kinfo;
  3030. int i;
  3031. for (i = 0; i < HNAE3_MAX_TC; i++) {
  3032. struct hnae3_tc_info *tc_info = &kinfo->tc_info[i];
  3033. int j;
  3034. if (!tc_info->enable)
  3035. continue;
  3036. for (j = 0; j < tc_info->tqp_count; j++) {
  3037. struct hnae3_queue *q;
  3038. q = priv->ring_data[tc_info->tqp_offset + j].ring->tqp;
  3039. hns3_write_dev(q, HNS3_RING_TX_RING_TC_REG,
  3040. tc_info->tc);
  3041. }
  3042. }
  3043. }
  3044. int hns3_init_all_ring(struct hns3_nic_priv *priv)
  3045. {
  3046. struct hnae3_handle *h = priv->ae_handle;
  3047. int ring_num = h->kinfo.num_tqps * 2;
  3048. int i, j;
  3049. int ret;
  3050. for (i = 0; i < ring_num; i++) {
  3051. ret = hns3_alloc_ring_memory(priv->ring_data[i].ring);
  3052. if (ret) {
  3053. dev_err(priv->dev,
  3054. "Alloc ring memory fail! ret=%d\n", ret);
  3055. goto out_when_alloc_ring_memory;
  3056. }
  3057. u64_stats_init(&priv->ring_data[i].ring->syncp);
  3058. }
  3059. return 0;
  3060. out_when_alloc_ring_memory:
  3061. for (j = i - 1; j >= 0; j--)
  3062. hns3_fini_ring(priv->ring_data[j].ring);
  3063. return -ENOMEM;
  3064. }
  3065. int hns3_uninit_all_ring(struct hns3_nic_priv *priv)
  3066. {
  3067. struct hnae3_handle *h = priv->ae_handle;
  3068. int i;
  3069. for (i = 0; i < h->kinfo.num_tqps; i++) {
  3070. hns3_fini_ring(priv->ring_data[i].ring);
  3071. hns3_fini_ring(priv->ring_data[i + h->kinfo.num_tqps].ring);
  3072. }
  3073. return 0;
  3074. }
  3075. /* Set mac addr if it is configured. or leave it to the AE driver */
  3076. static int hns3_init_mac_addr(struct net_device *netdev, bool init)
  3077. {
  3078. struct hns3_nic_priv *priv = netdev_priv(netdev);
  3079. struct hnae3_handle *h = priv->ae_handle;
  3080. u8 mac_addr_temp[ETH_ALEN];
  3081. int ret = 0;
  3082. if (h->ae_algo->ops->get_mac_addr && init) {
  3083. h->ae_algo->ops->get_mac_addr(h, mac_addr_temp);
  3084. ether_addr_copy(netdev->dev_addr, mac_addr_temp);
  3085. }
  3086. /* Check if the MAC address is valid, if not get a random one */
  3087. if (!is_valid_ether_addr(netdev->dev_addr)) {
  3088. eth_hw_addr_random(netdev);
  3089. dev_warn(priv->dev, "using random MAC address %pM\n",
  3090. netdev->dev_addr);
  3091. }
  3092. if (h->ae_algo->ops->set_mac_addr)
  3093. ret = h->ae_algo->ops->set_mac_addr(h, netdev->dev_addr, true);
  3094. return ret;
  3095. }
  3096. static int hns3_init_phy(struct net_device *netdev)
  3097. {
  3098. struct hnae3_handle *h = hns3_get_handle(netdev);
  3099. int ret = 0;
  3100. if (h->ae_algo->ops->mac_connect_phy)
  3101. ret = h->ae_algo->ops->mac_connect_phy(h);
  3102. return ret;
  3103. }
  3104. static void hns3_uninit_phy(struct net_device *netdev)
  3105. {
  3106. struct hnae3_handle *h = hns3_get_handle(netdev);
  3107. if (h->ae_algo->ops->mac_disconnect_phy)
  3108. h->ae_algo->ops->mac_disconnect_phy(h);
  3109. }
  3110. static int hns3_restore_fd_rules(struct net_device *netdev)
  3111. {
  3112. struct hnae3_handle *h = hns3_get_handle(netdev);
  3113. int ret = 0;
  3114. if (h->ae_algo->ops->restore_fd_rules)
  3115. ret = h->ae_algo->ops->restore_fd_rules(h);
  3116. return ret;
  3117. }
  3118. static void hns3_del_all_fd_rules(struct net_device *netdev, bool clear_list)
  3119. {
  3120. struct hnae3_handle *h = hns3_get_handle(netdev);
  3121. if (h->ae_algo->ops->del_all_fd_entries)
  3122. h->ae_algo->ops->del_all_fd_entries(h, clear_list);
  3123. }
  3124. static int hns3_client_start(struct hnae3_handle *handle)
  3125. {
  3126. if (!handle->ae_algo->ops->client_start)
  3127. return 0;
  3128. return handle->ae_algo->ops->client_start(handle);
  3129. }
  3130. static void hns3_client_stop(struct hnae3_handle *handle)
  3131. {
  3132. if (!handle->ae_algo->ops->client_stop)
  3133. return;
  3134. handle->ae_algo->ops->client_stop(handle);
  3135. }
  3136. static void hns3_info_show(struct hns3_nic_priv *priv)
  3137. {
  3138. struct hnae3_knic_private_info *kinfo = &priv->ae_handle->kinfo;
  3139. dev_info(priv->dev, "MAC address: %pM\n", priv->netdev->dev_addr);
  3140. dev_info(priv->dev, "Task queue pairs numbers: %d\n", kinfo->num_tqps);
  3141. dev_info(priv->dev, "RSS size: %d\n", kinfo->rss_size);
  3142. dev_info(priv->dev, "Allocated RSS size: %d\n", kinfo->req_rss_size);
  3143. dev_info(priv->dev, "RX buffer length: %d\n", kinfo->rx_buf_len);
  3144. dev_info(priv->dev, "Desc num per TX queue: %d\n", kinfo->num_tx_desc);
  3145. dev_info(priv->dev, "Desc num per RX queue: %d\n", kinfo->num_rx_desc);
  3146. dev_info(priv->dev, "Total number of enabled TCs: %d\n", kinfo->num_tc);
  3147. dev_info(priv->dev, "Max mtu size: %d\n", priv->netdev->max_mtu);
  3148. }
  3149. static int hns3_client_init(struct hnae3_handle *handle)
  3150. {
  3151. struct pci_dev *pdev = handle->pdev;
  3152. u16 alloc_tqps, max_rss_size;
  3153. struct hns3_nic_priv *priv;
  3154. struct net_device *netdev;
  3155. int ret;
  3156. handle->ae_algo->ops->get_tqps_and_rss_info(handle, &alloc_tqps,
  3157. &max_rss_size);
  3158. netdev = alloc_etherdev_mq(sizeof(struct hns3_nic_priv), alloc_tqps);
  3159. if (!netdev)
  3160. return -ENOMEM;
  3161. priv = netdev_priv(netdev);
  3162. priv->dev = &pdev->dev;
  3163. priv->netdev = netdev;
  3164. priv->ae_handle = handle;
  3165. priv->tx_timeout_count = 0;
  3166. set_bit(HNS3_NIC_STATE_DOWN, &priv->state);
  3167. handle->msg_enable = netif_msg_init(debug, DEFAULT_MSG_LEVEL);
  3168. handle->kinfo.netdev = netdev;
  3169. handle->priv = (void *)priv;
  3170. hns3_init_mac_addr(netdev, true);
  3171. hns3_set_default_feature(netdev);
  3172. netdev->watchdog_timeo = HNS3_TX_TIMEOUT;
  3173. netdev->priv_flags |= IFF_UNICAST_FLT;
  3174. netdev->netdev_ops = &hns3_nic_netdev_ops;
  3175. SET_NETDEV_DEV(netdev, &pdev->dev);
  3176. hns3_ethtool_set_ops(netdev);
  3177. /* Carrier off reporting is important to ethtool even BEFORE open */
  3178. netif_carrier_off(netdev);
  3179. ret = hns3_get_ring_config(priv);
  3180. if (ret) {
  3181. ret = -ENOMEM;
  3182. goto out_get_ring_cfg;
  3183. }
  3184. ret = hns3_nic_alloc_vector_data(priv);
  3185. if (ret) {
  3186. ret = -ENOMEM;
  3187. goto out_alloc_vector_data;
  3188. }
  3189. ret = hns3_nic_init_vector_data(priv);
  3190. if (ret) {
  3191. ret = -ENOMEM;
  3192. goto out_init_vector_data;
  3193. }
  3194. ret = hns3_init_all_ring(priv);
  3195. if (ret) {
  3196. ret = -ENOMEM;
  3197. goto out_init_ring_data;
  3198. }
  3199. ret = hns3_init_phy(netdev);
  3200. if (ret)
  3201. goto out_init_phy;
  3202. ret = register_netdev(netdev);
  3203. if (ret) {
  3204. dev_err(priv->dev, "probe register netdev fail!\n");
  3205. goto out_reg_netdev_fail;
  3206. }
  3207. ret = hns3_client_start(handle);
  3208. if (ret) {
  3209. dev_err(priv->dev, "hns3_client_start fail! ret=%d\n", ret);
  3210. goto out_client_start;
  3211. }
  3212. hns3_dcbnl_setup(handle);
  3213. hns3_dbg_init(handle);
  3214. /* MTU range: (ETH_MIN_MTU(kernel default) - 9702) */
  3215. netdev->max_mtu = HNS3_MAX_MTU;
  3216. set_bit(HNS3_NIC_STATE_INITED, &priv->state);
  3217. if (netif_msg_drv(handle))
  3218. hns3_info_show(priv);
  3219. return ret;
  3220. out_client_start:
  3221. unregister_netdev(netdev);
  3222. out_reg_netdev_fail:
  3223. hns3_uninit_phy(netdev);
  3224. out_init_phy:
  3225. hns3_uninit_all_ring(priv);
  3226. out_init_ring_data:
  3227. hns3_nic_uninit_vector_data(priv);
  3228. out_init_vector_data:
  3229. hns3_nic_dealloc_vector_data(priv);
  3230. out_alloc_vector_data:
  3231. priv->ring_data = NULL;
  3232. out_get_ring_cfg:
  3233. priv->ae_handle = NULL;
  3234. free_netdev(netdev);
  3235. return ret;
  3236. }
  3237. static void hns3_client_uninit(struct hnae3_handle *handle, bool reset)
  3238. {
  3239. struct net_device *netdev = handle->kinfo.netdev;
  3240. struct hns3_nic_priv *priv = netdev_priv(netdev);
  3241. int ret;
  3242. hns3_remove_hw_addr(netdev);
  3243. if (netdev->reg_state != NETREG_UNINITIALIZED)
  3244. unregister_netdev(netdev);
  3245. hns3_client_stop(handle);
  3246. hns3_uninit_phy(netdev);
  3247. if (!test_and_clear_bit(HNS3_NIC_STATE_INITED, &priv->state)) {
  3248. netdev_warn(netdev, "already uninitialized\n");
  3249. goto out_netdev_free;
  3250. }
  3251. hns3_del_all_fd_rules(netdev, true);
  3252. hns3_clear_all_ring(handle, true);
  3253. hns3_nic_uninit_vector_data(priv);
  3254. ret = hns3_nic_dealloc_vector_data(priv);
  3255. if (ret)
  3256. netdev_err(netdev, "dealloc vector error\n");
  3257. ret = hns3_uninit_all_ring(priv);
  3258. if (ret)
  3259. netdev_err(netdev, "uninit ring error\n");
  3260. hns3_put_ring_config(priv);
  3261. out_netdev_free:
  3262. hns3_dbg_uninit(handle);
  3263. free_netdev(netdev);
  3264. }
  3265. static void hns3_link_status_change(struct hnae3_handle *handle, bool linkup)
  3266. {
  3267. struct net_device *netdev = handle->kinfo.netdev;
  3268. if (!netdev)
  3269. return;
  3270. if (linkup) {
  3271. netif_tx_wake_all_queues(netdev);
  3272. netif_carrier_on(netdev);
  3273. if (netif_msg_link(handle))
  3274. netdev_info(netdev, "link up\n");
  3275. } else {
  3276. netif_carrier_off(netdev);
  3277. netif_tx_stop_all_queues(netdev);
  3278. if (netif_msg_link(handle))
  3279. netdev_info(netdev, "link down\n");
  3280. }
  3281. }
  3282. static int hns3_client_setup_tc(struct hnae3_handle *handle, u8 tc)
  3283. {
  3284. struct hnae3_knic_private_info *kinfo = &handle->kinfo;
  3285. struct net_device *ndev = kinfo->netdev;
  3286. if (tc > HNAE3_MAX_TC)
  3287. return -EINVAL;
  3288. if (!ndev)
  3289. return -ENODEV;
  3290. return hns3_nic_set_real_num_queue(ndev);
  3291. }
  3292. static int hns3_recover_hw_addr(struct net_device *ndev)
  3293. {
  3294. struct netdev_hw_addr_list *list;
  3295. struct netdev_hw_addr *ha, *tmp;
  3296. int ret = 0;
  3297. netif_addr_lock_bh(ndev);
  3298. /* go through and sync uc_addr entries to the device */
  3299. list = &ndev->uc;
  3300. list_for_each_entry_safe(ha, tmp, &list->list, list) {
  3301. ret = hns3_nic_uc_sync(ndev, ha->addr);
  3302. if (ret)
  3303. goto out;
  3304. }
  3305. /* go through and sync mc_addr entries to the device */
  3306. list = &ndev->mc;
  3307. list_for_each_entry_safe(ha, tmp, &list->list, list) {
  3308. ret = hns3_nic_mc_sync(ndev, ha->addr);
  3309. if (ret)
  3310. goto out;
  3311. }
  3312. out:
  3313. netif_addr_unlock_bh(ndev);
  3314. return ret;
  3315. }
  3316. static void hns3_remove_hw_addr(struct net_device *netdev)
  3317. {
  3318. struct netdev_hw_addr_list *list;
  3319. struct netdev_hw_addr *ha, *tmp;
  3320. hns3_nic_uc_unsync(netdev, netdev->dev_addr);
  3321. netif_addr_lock_bh(netdev);
  3322. /* go through and unsync uc_addr entries to the device */
  3323. list = &netdev->uc;
  3324. list_for_each_entry_safe(ha, tmp, &list->list, list)
  3325. hns3_nic_uc_unsync(netdev, ha->addr);
  3326. /* go through and unsync mc_addr entries to the device */
  3327. list = &netdev->mc;
  3328. list_for_each_entry_safe(ha, tmp, &list->list, list)
  3329. if (ha->refcount > 1)
  3330. hns3_nic_mc_unsync(netdev, ha->addr);
  3331. netif_addr_unlock_bh(netdev);
  3332. }
  3333. static void hns3_clear_tx_ring(struct hns3_enet_ring *ring)
  3334. {
  3335. while (ring->next_to_clean != ring->next_to_use) {
  3336. ring->desc[ring->next_to_clean].tx.bdtp_fe_sc_vld_ra_ri = 0;
  3337. hns3_free_buffer_detach(ring, ring->next_to_clean);
  3338. ring_ptr_move_fw(ring, next_to_clean);
  3339. }
  3340. }
  3341. static int hns3_clear_rx_ring(struct hns3_enet_ring *ring)
  3342. {
  3343. struct hns3_desc_cb res_cbs;
  3344. int ret;
  3345. while (ring->next_to_use != ring->next_to_clean) {
  3346. /* When a buffer is not reused, it's memory has been
  3347. * freed in hns3_handle_rx_bd or will be freed by
  3348. * stack, so we need to replace the buffer here.
  3349. */
  3350. if (!ring->desc_cb[ring->next_to_use].reuse_flag) {
  3351. ret = hns3_reserve_buffer_map(ring, &res_cbs);
  3352. if (ret) {
  3353. u64_stats_update_begin(&ring->syncp);
  3354. ring->stats.sw_err_cnt++;
  3355. u64_stats_update_end(&ring->syncp);
  3356. /* if alloc new buffer fail, exit directly
  3357. * and reclear in up flow.
  3358. */
  3359. netdev_warn(ring->tqp->handle->kinfo.netdev,
  3360. "reserve buffer map failed, ret = %d\n",
  3361. ret);
  3362. return ret;
  3363. }
  3364. hns3_replace_buffer(ring, ring->next_to_use, &res_cbs);
  3365. }
  3366. ring_ptr_move_fw(ring, next_to_use);
  3367. }
  3368. /* Free the pending skb in rx ring */
  3369. if (ring->skb) {
  3370. dev_kfree_skb_any(ring->skb);
  3371. ring->skb = NULL;
  3372. ring->pending_buf = 0;
  3373. }
  3374. return 0;
  3375. }
  3376. static void hns3_force_clear_rx_ring(struct hns3_enet_ring *ring)
  3377. {
  3378. while (ring->next_to_use != ring->next_to_clean) {
  3379. /* When a buffer is not reused, it's memory has been
  3380. * freed in hns3_handle_rx_bd or will be freed by
  3381. * stack, so only need to unmap the buffer here.
  3382. */
  3383. if (!ring->desc_cb[ring->next_to_use].reuse_flag) {
  3384. hns3_unmap_buffer(ring,
  3385. &ring->desc_cb[ring->next_to_use]);
  3386. ring->desc_cb[ring->next_to_use].dma = 0;
  3387. }
  3388. ring_ptr_move_fw(ring, next_to_use);
  3389. }
  3390. }
  3391. static void hns3_clear_all_ring(struct hnae3_handle *h, bool force)
  3392. {
  3393. struct net_device *ndev = h->kinfo.netdev;
  3394. struct hns3_nic_priv *priv = netdev_priv(ndev);
  3395. u32 i;
  3396. for (i = 0; i < h->kinfo.num_tqps; i++) {
  3397. struct hns3_enet_ring *ring;
  3398. ring = priv->ring_data[i].ring;
  3399. hns3_clear_tx_ring(ring);
  3400. ring = priv->ring_data[i + h->kinfo.num_tqps].ring;
  3401. /* Continue to clear other rings even if clearing some
  3402. * rings failed.
  3403. */
  3404. if (force)
  3405. hns3_force_clear_rx_ring(ring);
  3406. else
  3407. hns3_clear_rx_ring(ring);
  3408. }
  3409. }
  3410. int hns3_nic_reset_all_ring(struct hnae3_handle *h)
  3411. {
  3412. struct net_device *ndev = h->kinfo.netdev;
  3413. struct hns3_nic_priv *priv = netdev_priv(ndev);
  3414. struct hns3_enet_ring *rx_ring;
  3415. int i, j;
  3416. int ret;
  3417. for (i = 0; i < h->kinfo.num_tqps; i++) {
  3418. ret = h->ae_algo->ops->reset_queue(h, i);
  3419. if (ret)
  3420. return ret;
  3421. hns3_init_ring_hw(priv->ring_data[i].ring);
  3422. /* We need to clear tx ring here because self test will
  3423. * use the ring and will not run down before up
  3424. */
  3425. hns3_clear_tx_ring(priv->ring_data[i].ring);
  3426. priv->ring_data[i].ring->next_to_clean = 0;
  3427. priv->ring_data[i].ring->next_to_use = 0;
  3428. rx_ring = priv->ring_data[i + h->kinfo.num_tqps].ring;
  3429. hns3_init_ring_hw(rx_ring);
  3430. ret = hns3_clear_rx_ring(rx_ring);
  3431. if (ret)
  3432. return ret;
  3433. /* We can not know the hardware head and tail when this
  3434. * function is called in reset flow, so we reuse all desc.
  3435. */
  3436. for (j = 0; j < rx_ring->desc_num; j++)
  3437. hns3_reuse_buffer(rx_ring, j);
  3438. rx_ring->next_to_clean = 0;
  3439. rx_ring->next_to_use = 0;
  3440. }
  3441. hns3_init_tx_ring_tc(priv);
  3442. return 0;
  3443. }
  3444. static void hns3_store_coal(struct hns3_nic_priv *priv)
  3445. {
  3446. /* ethtool only support setting and querying one coal
  3447. * configuration for now, so save the vector 0' coal
  3448. * configuration here in order to restore it.
  3449. */
  3450. memcpy(&priv->tx_coal, &priv->tqp_vector[0].tx_group.coal,
  3451. sizeof(struct hns3_enet_coalesce));
  3452. memcpy(&priv->rx_coal, &priv->tqp_vector[0].rx_group.coal,
  3453. sizeof(struct hns3_enet_coalesce));
  3454. }
  3455. static void hns3_restore_coal(struct hns3_nic_priv *priv)
  3456. {
  3457. u16 vector_num = priv->vector_num;
  3458. int i;
  3459. for (i = 0; i < vector_num; i++) {
  3460. memcpy(&priv->tqp_vector[i].tx_group.coal, &priv->tx_coal,
  3461. sizeof(struct hns3_enet_coalesce));
  3462. memcpy(&priv->tqp_vector[i].rx_group.coal, &priv->rx_coal,
  3463. sizeof(struct hns3_enet_coalesce));
  3464. }
  3465. }
  3466. static int hns3_reset_notify_down_enet(struct hnae3_handle *handle)
  3467. {
  3468. struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
  3469. struct hnae3_knic_private_info *kinfo = &handle->kinfo;
  3470. struct net_device *ndev = kinfo->netdev;
  3471. struct hns3_nic_priv *priv = netdev_priv(ndev);
  3472. if (test_and_set_bit(HNS3_NIC_STATE_RESETTING, &priv->state))
  3473. return 0;
  3474. /* it is cumbersome for hardware to pick-and-choose entries for deletion
  3475. * from table space. Hence, for function reset software intervention is
  3476. * required to delete the entries
  3477. */
  3478. if (hns3_dev_ongoing_func_reset(ae_dev)) {
  3479. hns3_remove_hw_addr(ndev);
  3480. hns3_del_all_fd_rules(ndev, false);
  3481. }
  3482. if (!netif_running(ndev))
  3483. return 0;
  3484. return hns3_nic_net_stop(ndev);
  3485. }
  3486. static int hns3_reset_notify_up_enet(struct hnae3_handle *handle)
  3487. {
  3488. struct hnae3_knic_private_info *kinfo = &handle->kinfo;
  3489. struct hns3_nic_priv *priv = netdev_priv(kinfo->netdev);
  3490. int ret = 0;
  3491. if (!test_bit(HNS3_NIC_STATE_INITED, &priv->state)) {
  3492. netdev_err(kinfo->netdev, "device is not initialized yet\n");
  3493. return -EFAULT;
  3494. }
  3495. clear_bit(HNS3_NIC_STATE_RESETTING, &priv->state);
  3496. if (netif_running(kinfo->netdev)) {
  3497. ret = hns3_nic_net_open(kinfo->netdev);
  3498. if (ret) {
  3499. set_bit(HNS3_NIC_STATE_RESETTING, &priv->state);
  3500. netdev_err(kinfo->netdev,
  3501. "net up fail, ret=%d!\n", ret);
  3502. return ret;
  3503. }
  3504. }
  3505. return ret;
  3506. }
  3507. static int hns3_reset_notify_init_enet(struct hnae3_handle *handle)
  3508. {
  3509. struct net_device *netdev = handle->kinfo.netdev;
  3510. struct hns3_nic_priv *priv = netdev_priv(netdev);
  3511. int ret;
  3512. /* Carrier off reporting is important to ethtool even BEFORE open */
  3513. netif_carrier_off(netdev);
  3514. ret = hns3_get_ring_config(priv);
  3515. if (ret)
  3516. return ret;
  3517. ret = hns3_nic_alloc_vector_data(priv);
  3518. if (ret)
  3519. goto err_put_ring;
  3520. hns3_restore_coal(priv);
  3521. ret = hns3_nic_init_vector_data(priv);
  3522. if (ret)
  3523. goto err_dealloc_vector;
  3524. ret = hns3_init_all_ring(priv);
  3525. if (ret)
  3526. goto err_uninit_vector;
  3527. ret = hns3_client_start(handle);
  3528. if (ret) {
  3529. dev_err(priv->dev, "hns3_client_start fail! ret=%d\n", ret);
  3530. goto err_uninit_ring;
  3531. }
  3532. set_bit(HNS3_NIC_STATE_INITED, &priv->state);
  3533. return ret;
  3534. err_uninit_ring:
  3535. hns3_uninit_all_ring(priv);
  3536. err_uninit_vector:
  3537. hns3_nic_uninit_vector_data(priv);
  3538. err_dealloc_vector:
  3539. hns3_nic_dealloc_vector_data(priv);
  3540. err_put_ring:
  3541. hns3_put_ring_config(priv);
  3542. return ret;
  3543. }
  3544. static int hns3_reset_notify_restore_enet(struct hnae3_handle *handle)
  3545. {
  3546. struct net_device *netdev = handle->kinfo.netdev;
  3547. bool vlan_filter_enable;
  3548. int ret;
  3549. ret = hns3_init_mac_addr(netdev, false);
  3550. if (ret)
  3551. return ret;
  3552. ret = hns3_recover_hw_addr(netdev);
  3553. if (ret)
  3554. return ret;
  3555. ret = hns3_update_promisc_mode(netdev, handle->netdev_flags);
  3556. if (ret)
  3557. return ret;
  3558. vlan_filter_enable = netdev->flags & IFF_PROMISC ? false : true;
  3559. hns3_enable_vlan_filter(netdev, vlan_filter_enable);
  3560. if (handle->ae_algo->ops->restore_vlan_table)
  3561. handle->ae_algo->ops->restore_vlan_table(handle);
  3562. return hns3_restore_fd_rules(netdev);
  3563. }
  3564. static int hns3_reset_notify_uninit_enet(struct hnae3_handle *handle)
  3565. {
  3566. struct net_device *netdev = handle->kinfo.netdev;
  3567. struct hns3_nic_priv *priv = netdev_priv(netdev);
  3568. int ret;
  3569. if (!test_and_clear_bit(HNS3_NIC_STATE_INITED, &priv->state)) {
  3570. netdev_warn(netdev, "already uninitialized\n");
  3571. return 0;
  3572. }
  3573. hns3_clear_all_ring(handle, true);
  3574. hns3_reset_tx_queue(priv->ae_handle);
  3575. hns3_nic_uninit_vector_data(priv);
  3576. hns3_store_coal(priv);
  3577. ret = hns3_nic_dealloc_vector_data(priv);
  3578. if (ret)
  3579. netdev_err(netdev, "dealloc vector error\n");
  3580. ret = hns3_uninit_all_ring(priv);
  3581. if (ret)
  3582. netdev_err(netdev, "uninit ring error\n");
  3583. hns3_put_ring_config(priv);
  3584. return ret;
  3585. }
  3586. static int hns3_reset_notify(struct hnae3_handle *handle,
  3587. enum hnae3_reset_notify_type type)
  3588. {
  3589. int ret = 0;
  3590. switch (type) {
  3591. case HNAE3_UP_CLIENT:
  3592. ret = hns3_reset_notify_up_enet(handle);
  3593. break;
  3594. case HNAE3_DOWN_CLIENT:
  3595. ret = hns3_reset_notify_down_enet(handle);
  3596. break;
  3597. case HNAE3_INIT_CLIENT:
  3598. ret = hns3_reset_notify_init_enet(handle);
  3599. break;
  3600. case HNAE3_UNINIT_CLIENT:
  3601. ret = hns3_reset_notify_uninit_enet(handle);
  3602. break;
  3603. case HNAE3_RESTORE_CLIENT:
  3604. ret = hns3_reset_notify_restore_enet(handle);
  3605. break;
  3606. default:
  3607. break;
  3608. }
  3609. return ret;
  3610. }
  3611. static int hns3_change_channels(struct hnae3_handle *handle, u32 new_tqp_num,
  3612. bool rxfh_configured)
  3613. {
  3614. int ret;
  3615. ret = handle->ae_algo->ops->set_channels(handle, new_tqp_num,
  3616. rxfh_configured);
  3617. if (ret) {
  3618. dev_err(&handle->pdev->dev,
  3619. "Change tqp num(%u) fail.\n", new_tqp_num);
  3620. return ret;
  3621. }
  3622. ret = hns3_reset_notify(handle, HNAE3_INIT_CLIENT);
  3623. if (ret)
  3624. return ret;
  3625. ret = hns3_reset_notify(handle, HNAE3_UP_CLIENT);
  3626. if (ret)
  3627. hns3_reset_notify(handle, HNAE3_UNINIT_CLIENT);
  3628. return ret;
  3629. }
  3630. int hns3_set_channels(struct net_device *netdev,
  3631. struct ethtool_channels *ch)
  3632. {
  3633. struct hnae3_handle *h = hns3_get_handle(netdev);
  3634. struct hnae3_knic_private_info *kinfo = &h->kinfo;
  3635. bool rxfh_configured = netif_is_rxfh_configured(netdev);
  3636. u32 new_tqp_num = ch->combined_count;
  3637. u16 org_tqp_num;
  3638. int ret;
  3639. if (hns3_nic_resetting(netdev))
  3640. return -EBUSY;
  3641. if (ch->rx_count || ch->tx_count)
  3642. return -EINVAL;
  3643. if (new_tqp_num > hns3_get_max_available_channels(h) ||
  3644. new_tqp_num < 1) {
  3645. dev_err(&netdev->dev,
  3646. "Change tqps fail, the tqp range is from 1 to %d",
  3647. hns3_get_max_available_channels(h));
  3648. return -EINVAL;
  3649. }
  3650. if (kinfo->rss_size == new_tqp_num)
  3651. return 0;
  3652. netif_dbg(h, drv, netdev,
  3653. "set channels: tqp_num=%u, rxfh=%d\n",
  3654. new_tqp_num, rxfh_configured);
  3655. ret = hns3_reset_notify(h, HNAE3_DOWN_CLIENT);
  3656. if (ret)
  3657. return ret;
  3658. ret = hns3_reset_notify(h, HNAE3_UNINIT_CLIENT);
  3659. if (ret)
  3660. return ret;
  3661. org_tqp_num = h->kinfo.num_tqps;
  3662. ret = hns3_change_channels(h, new_tqp_num, rxfh_configured);
  3663. if (ret) {
  3664. int ret1;
  3665. netdev_warn(netdev,
  3666. "Change channels fail, revert to old value\n");
  3667. ret1 = hns3_change_channels(h, org_tqp_num, rxfh_configured);
  3668. if (ret1) {
  3669. netdev_err(netdev,
  3670. "revert to old channel fail\n");
  3671. return ret1;
  3672. }
  3673. return ret;
  3674. }
  3675. return 0;
  3676. }
  3677. static const struct hns3_hw_error_info hns3_hw_err[] = {
  3678. { .type = HNAE3_PPU_POISON_ERROR,
  3679. .msg = "PPU poison" },
  3680. { .type = HNAE3_CMDQ_ECC_ERROR,
  3681. .msg = "IMP CMDQ error" },
  3682. { .type = HNAE3_IMP_RD_POISON_ERROR,
  3683. .msg = "IMP RD poison" },
  3684. };
  3685. static void hns3_process_hw_error(struct hnae3_handle *handle,
  3686. enum hnae3_hw_error_type type)
  3687. {
  3688. int i;
  3689. for (i = 0; i < ARRAY_SIZE(hns3_hw_err); i++) {
  3690. if (hns3_hw_err[i].type == type) {
  3691. dev_err(&handle->pdev->dev, "Detected %s!\n",
  3692. hns3_hw_err[i].msg);
  3693. break;
  3694. }
  3695. }
  3696. }
  3697. static const struct hnae3_client_ops client_ops = {
  3698. .init_instance = hns3_client_init,
  3699. .uninit_instance = hns3_client_uninit,
  3700. .link_status_change = hns3_link_status_change,
  3701. .setup_tc = hns3_client_setup_tc,
  3702. .reset_notify = hns3_reset_notify,
  3703. .process_hw_error = hns3_process_hw_error,
  3704. };
  3705. /* hns3_init_module - Driver registration routine
  3706. * hns3_init_module is the first routine called when the driver is
  3707. * loaded. All it does is register with the PCI subsystem.
  3708. */
  3709. static int __init hns3_init_module(void)
  3710. {
  3711. int ret;
  3712. pr_info("%s: %s - version\n", hns3_driver_name, hns3_driver_string);
  3713. pr_info("%s: %s\n", hns3_driver_name, hns3_copyright);
  3714. client.type = HNAE3_CLIENT_KNIC;
  3715. snprintf(client.name, HNAE3_CLIENT_NAME_LENGTH - 1, "%s",
  3716. hns3_driver_name);
  3717. client.ops = &client_ops;
  3718. INIT_LIST_HEAD(&client.node);
  3719. hns3_dbg_register_debugfs(hns3_driver_name);
  3720. ret = hnae3_register_client(&client);
  3721. if (ret)
  3722. goto err_reg_client;
  3723. ret = pci_register_driver(&hns3_driver);
  3724. if (ret)
  3725. goto err_reg_driver;
  3726. return ret;
  3727. err_reg_driver:
  3728. hnae3_unregister_client(&client);
  3729. err_reg_client:
  3730. hns3_dbg_unregister_debugfs();
  3731. return ret;
  3732. }
  3733. module_init(hns3_init_module);
  3734. /* hns3_exit_module - Driver exit cleanup routine
  3735. * hns3_exit_module is called just before the driver is removed
  3736. * from memory.
  3737. */
  3738. static void __exit hns3_exit_module(void)
  3739. {
  3740. pci_unregister_driver(&hns3_driver);
  3741. hnae3_unregister_client(&client);
  3742. hns3_dbg_unregister_debugfs();
  3743. }
  3744. module_exit(hns3_exit_module);
  3745. MODULE_DESCRIPTION("HNS3: Hisilicon Ethernet Driver");
  3746. MODULE_AUTHOR("Huawei Tech. Co., Ltd.");
  3747. MODULE_LICENSE("GPL");
  3748. MODULE_ALIAS("pci:hns-nic");
  3749. MODULE_VERSION(HNS3_MOD_VERSION);