ice_main.c 138 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2018, Intel Corporation. */
  3. /* Intel(R) Ethernet Connection E800 Series Linux Driver */
  4. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  5. #include "ice.h"
  6. #define DRV_VERSION "ice-0.7.0-k"
  7. #define DRV_SUMMARY "Intel(R) Ethernet Connection E800 Series Linux Driver"
  8. const char ice_drv_ver[] = DRV_VERSION;
  9. static const char ice_driver_string[] = DRV_SUMMARY;
  10. static const char ice_copyright[] = "Copyright (c) 2018, Intel Corporation.";
  11. MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
  12. MODULE_DESCRIPTION(DRV_SUMMARY);
  13. MODULE_LICENSE("GPL");
  14. MODULE_VERSION(DRV_VERSION);
  15. static int debug = -1;
  16. module_param(debug, int, 0644);
  17. #ifndef CONFIG_DYNAMIC_DEBUG
  18. MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all), hw debug_mask (0x8XXXXXXX)");
  19. #else
  20. MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all)");
  21. #endif /* !CONFIG_DYNAMIC_DEBUG */
  22. static struct workqueue_struct *ice_wq;
  23. static const struct net_device_ops ice_netdev_ops;
  24. static void ice_pf_dis_all_vsi(struct ice_pf *pf);
  25. static void ice_rebuild(struct ice_pf *pf);
  26. static int ice_vsi_release(struct ice_vsi *vsi);
  27. static void ice_update_vsi_stats(struct ice_vsi *vsi);
  28. static void ice_update_pf_stats(struct ice_pf *pf);
  29. /**
  30. * ice_get_free_slot - get the next non-NULL location index in array
  31. * @array: array to search
  32. * @size: size of the array
  33. * @curr: last known occupied index to be used as a search hint
  34. *
  35. * void * is being used to keep the functionality generic. This lets us use this
  36. * function on any array of pointers.
  37. */
  38. static int ice_get_free_slot(void *array, int size, int curr)
  39. {
  40. int **tmp_array = (int **)array;
  41. int next;
  42. if (curr < (size - 1) && !tmp_array[curr + 1]) {
  43. next = curr + 1;
  44. } else {
  45. int i = 0;
  46. while ((i < size) && (tmp_array[i]))
  47. i++;
  48. if (i == size)
  49. next = ICE_NO_VSI;
  50. else
  51. next = i;
  52. }
  53. return next;
  54. }
  55. /**
  56. * ice_search_res - Search the tracker for a block of resources
  57. * @res: pointer to the resource
  58. * @needed: size of the block needed
  59. * @id: identifier to track owner
  60. * Returns the base item index of the block, or -ENOMEM for error
  61. */
  62. static int ice_search_res(struct ice_res_tracker *res, u16 needed, u16 id)
  63. {
  64. int start = res->search_hint;
  65. int end = start;
  66. id |= ICE_RES_VALID_BIT;
  67. do {
  68. /* skip already allocated entries */
  69. if (res->list[end++] & ICE_RES_VALID_BIT) {
  70. start = end;
  71. if ((start + needed) > res->num_entries)
  72. break;
  73. }
  74. if (end == (start + needed)) {
  75. int i = start;
  76. /* there was enough, so assign it to the requestor */
  77. while (i != end)
  78. res->list[i++] = id;
  79. if (end == res->num_entries)
  80. end = 0;
  81. res->search_hint = end;
  82. return start;
  83. }
  84. } while (1);
  85. return -ENOMEM;
  86. }
  87. /**
  88. * ice_get_res - get a block of resources
  89. * @pf: board private structure
  90. * @res: pointer to the resource
  91. * @needed: size of the block needed
  92. * @id: identifier to track owner
  93. *
  94. * Returns the base item index of the block, or -ENOMEM for error
  95. * The search_hint trick and lack of advanced fit-finding only works
  96. * because we're highly likely to have all the same sized requests.
  97. * Linear search time and any fragmentation should be minimal.
  98. */
  99. static int
  100. ice_get_res(struct ice_pf *pf, struct ice_res_tracker *res, u16 needed, u16 id)
  101. {
  102. int ret;
  103. if (!res || !pf)
  104. return -EINVAL;
  105. if (!needed || needed > res->num_entries || id >= ICE_RES_VALID_BIT) {
  106. dev_err(&pf->pdev->dev,
  107. "param err: needed=%d, num_entries = %d id=0x%04x\n",
  108. needed, res->num_entries, id);
  109. return -EINVAL;
  110. }
  111. /* search based on search_hint */
  112. ret = ice_search_res(res, needed, id);
  113. if (ret < 0) {
  114. /* previous search failed. Reset search hint and try again */
  115. res->search_hint = 0;
  116. ret = ice_search_res(res, needed, id);
  117. }
  118. return ret;
  119. }
  120. /**
  121. * ice_free_res - free a block of resources
  122. * @res: pointer to the resource
  123. * @index: starting index previously returned by ice_get_res
  124. * @id: identifier to track owner
  125. * Returns number of resources freed
  126. */
  127. static int ice_free_res(struct ice_res_tracker *res, u16 index, u16 id)
  128. {
  129. int count = 0;
  130. int i;
  131. if (!res || index >= res->num_entries)
  132. return -EINVAL;
  133. id |= ICE_RES_VALID_BIT;
  134. for (i = index; i < res->num_entries && res->list[i] == id; i++) {
  135. res->list[i] = 0;
  136. count++;
  137. }
  138. return count;
  139. }
  140. /**
  141. * ice_add_mac_to_list - Add a mac address filter entry to the list
  142. * @vsi: the VSI to be forwarded to
  143. * @add_list: pointer to the list which contains MAC filter entries
  144. * @macaddr: the MAC address to be added.
  145. *
  146. * Adds mac address filter entry to the temp list
  147. *
  148. * Returns 0 on success or ENOMEM on failure.
  149. */
  150. static int ice_add_mac_to_list(struct ice_vsi *vsi, struct list_head *add_list,
  151. const u8 *macaddr)
  152. {
  153. struct ice_fltr_list_entry *tmp;
  154. struct ice_pf *pf = vsi->back;
  155. tmp = devm_kzalloc(&pf->pdev->dev, sizeof(*tmp), GFP_ATOMIC);
  156. if (!tmp)
  157. return -ENOMEM;
  158. tmp->fltr_info.flag = ICE_FLTR_TX;
  159. tmp->fltr_info.src = vsi->vsi_num;
  160. tmp->fltr_info.lkup_type = ICE_SW_LKUP_MAC;
  161. tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
  162. tmp->fltr_info.fwd_id.vsi_id = vsi->vsi_num;
  163. ether_addr_copy(tmp->fltr_info.l_data.mac.mac_addr, macaddr);
  164. INIT_LIST_HEAD(&tmp->list_entry);
  165. list_add(&tmp->list_entry, add_list);
  166. return 0;
  167. }
  168. /**
  169. * ice_add_mac_to_sync_list - creates list of mac addresses to be synced
  170. * @netdev: the net device on which the sync is happening
  171. * @addr: mac address to sync
  172. *
  173. * This is a callback function which is called by the in kernel device sync
  174. * functions (like __dev_uc_sync, __dev_mc_sync, etc). This function only
  175. * populates the tmp_sync_list, which is later used by ice_add_mac to add the
  176. * mac filters from the hardware.
  177. */
  178. static int ice_add_mac_to_sync_list(struct net_device *netdev, const u8 *addr)
  179. {
  180. struct ice_netdev_priv *np = netdev_priv(netdev);
  181. struct ice_vsi *vsi = np->vsi;
  182. if (ice_add_mac_to_list(vsi, &vsi->tmp_sync_list, addr))
  183. return -EINVAL;
  184. return 0;
  185. }
  186. /**
  187. * ice_add_mac_to_unsync_list - creates list of mac addresses to be unsynced
  188. * @netdev: the net device on which the unsync is happening
  189. * @addr: mac address to unsync
  190. *
  191. * This is a callback function which is called by the in kernel device unsync
  192. * functions (like __dev_uc_unsync, __dev_mc_unsync, etc). This function only
  193. * populates the tmp_unsync_list, which is later used by ice_remove_mac to
  194. * delete the mac filters from the hardware.
  195. */
  196. static int ice_add_mac_to_unsync_list(struct net_device *netdev, const u8 *addr)
  197. {
  198. struct ice_netdev_priv *np = netdev_priv(netdev);
  199. struct ice_vsi *vsi = np->vsi;
  200. if (ice_add_mac_to_list(vsi, &vsi->tmp_unsync_list, addr))
  201. return -EINVAL;
  202. return 0;
  203. }
  204. /**
  205. * ice_free_fltr_list - free filter lists helper
  206. * @dev: pointer to the device struct
  207. * @h: pointer to the list head to be freed
  208. *
  209. * Helper function to free filter lists previously created using
  210. * ice_add_mac_to_list
  211. */
  212. static void ice_free_fltr_list(struct device *dev, struct list_head *h)
  213. {
  214. struct ice_fltr_list_entry *e, *tmp;
  215. list_for_each_entry_safe(e, tmp, h, list_entry) {
  216. list_del(&e->list_entry);
  217. devm_kfree(dev, e);
  218. }
  219. }
  220. /**
  221. * ice_vsi_fltr_changed - check if filter state changed
  222. * @vsi: VSI to be checked
  223. *
  224. * returns true if filter state has changed, false otherwise.
  225. */
  226. static bool ice_vsi_fltr_changed(struct ice_vsi *vsi)
  227. {
  228. return test_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags) ||
  229. test_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags) ||
  230. test_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
  231. }
  232. /**
  233. * ice_vsi_sync_fltr - Update the VSI filter list to the HW
  234. * @vsi: ptr to the VSI
  235. *
  236. * Push any outstanding VSI filter changes through the AdminQ.
  237. */
  238. static int ice_vsi_sync_fltr(struct ice_vsi *vsi)
  239. {
  240. struct device *dev = &vsi->back->pdev->dev;
  241. struct net_device *netdev = vsi->netdev;
  242. bool promisc_forced_on = false;
  243. struct ice_pf *pf = vsi->back;
  244. struct ice_hw *hw = &pf->hw;
  245. enum ice_status status = 0;
  246. u32 changed_flags = 0;
  247. int err = 0;
  248. if (!vsi->netdev)
  249. return -EINVAL;
  250. while (test_and_set_bit(__ICE_CFG_BUSY, vsi->state))
  251. usleep_range(1000, 2000);
  252. changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
  253. vsi->current_netdev_flags = vsi->netdev->flags;
  254. INIT_LIST_HEAD(&vsi->tmp_sync_list);
  255. INIT_LIST_HEAD(&vsi->tmp_unsync_list);
  256. if (ice_vsi_fltr_changed(vsi)) {
  257. clear_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
  258. clear_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
  259. clear_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
  260. /* grab the netdev's addr_list_lock */
  261. netif_addr_lock_bh(netdev);
  262. __dev_uc_sync(netdev, ice_add_mac_to_sync_list,
  263. ice_add_mac_to_unsync_list);
  264. __dev_mc_sync(netdev, ice_add_mac_to_sync_list,
  265. ice_add_mac_to_unsync_list);
  266. /* our temp lists are populated. release lock */
  267. netif_addr_unlock_bh(netdev);
  268. }
  269. /* Remove mac addresses in the unsync list */
  270. status = ice_remove_mac(hw, &vsi->tmp_unsync_list);
  271. ice_free_fltr_list(dev, &vsi->tmp_unsync_list);
  272. if (status) {
  273. netdev_err(netdev, "Failed to delete MAC filters\n");
  274. /* if we failed because of alloc failures, just bail */
  275. if (status == ICE_ERR_NO_MEMORY) {
  276. err = -ENOMEM;
  277. goto out;
  278. }
  279. }
  280. /* Add mac addresses in the sync list */
  281. status = ice_add_mac(hw, &vsi->tmp_sync_list);
  282. ice_free_fltr_list(dev, &vsi->tmp_sync_list);
  283. if (status) {
  284. netdev_err(netdev, "Failed to add MAC filters\n");
  285. /* If there is no more space for new umac filters, vsi
  286. * should go into promiscuous mode. There should be some
  287. * space reserved for promiscuous filters.
  288. */
  289. if (hw->adminq.sq_last_status == ICE_AQ_RC_ENOSPC &&
  290. !test_and_set_bit(__ICE_FLTR_OVERFLOW_PROMISC,
  291. vsi->state)) {
  292. promisc_forced_on = true;
  293. netdev_warn(netdev,
  294. "Reached MAC filter limit, forcing promisc mode on VSI %d\n",
  295. vsi->vsi_num);
  296. } else {
  297. err = -EIO;
  298. goto out;
  299. }
  300. }
  301. /* check for changes in promiscuous modes */
  302. if (changed_flags & IFF_ALLMULTI)
  303. netdev_warn(netdev, "Unsupported configuration\n");
  304. if (((changed_flags & IFF_PROMISC) || promisc_forced_on) ||
  305. test_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags)) {
  306. clear_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
  307. if (vsi->current_netdev_flags & IFF_PROMISC) {
  308. /* Apply TX filter rule to get traffic from VMs */
  309. status = ice_cfg_dflt_vsi(hw, vsi->vsi_num, true,
  310. ICE_FLTR_TX);
  311. if (status) {
  312. netdev_err(netdev, "Error setting default VSI %i tx rule\n",
  313. vsi->vsi_num);
  314. vsi->current_netdev_flags &= ~IFF_PROMISC;
  315. err = -EIO;
  316. goto out_promisc;
  317. }
  318. /* Apply RX filter rule to get traffic from wire */
  319. status = ice_cfg_dflt_vsi(hw, vsi->vsi_num, true,
  320. ICE_FLTR_RX);
  321. if (status) {
  322. netdev_err(netdev, "Error setting default VSI %i rx rule\n",
  323. vsi->vsi_num);
  324. vsi->current_netdev_flags &= ~IFF_PROMISC;
  325. err = -EIO;
  326. goto out_promisc;
  327. }
  328. } else {
  329. /* Clear TX filter rule to stop traffic from VMs */
  330. status = ice_cfg_dflt_vsi(hw, vsi->vsi_num, false,
  331. ICE_FLTR_TX);
  332. if (status) {
  333. netdev_err(netdev, "Error clearing default VSI %i tx rule\n",
  334. vsi->vsi_num);
  335. vsi->current_netdev_flags |= IFF_PROMISC;
  336. err = -EIO;
  337. goto out_promisc;
  338. }
  339. /* Clear filter RX to remove traffic from wire */
  340. status = ice_cfg_dflt_vsi(hw, vsi->vsi_num, false,
  341. ICE_FLTR_RX);
  342. if (status) {
  343. netdev_err(netdev, "Error clearing default VSI %i rx rule\n",
  344. vsi->vsi_num);
  345. vsi->current_netdev_flags |= IFF_PROMISC;
  346. err = -EIO;
  347. goto out_promisc;
  348. }
  349. }
  350. }
  351. goto exit;
  352. out_promisc:
  353. set_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
  354. goto exit;
  355. out:
  356. /* if something went wrong then set the changed flag so we try again */
  357. set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
  358. set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
  359. exit:
  360. clear_bit(__ICE_CFG_BUSY, vsi->state);
  361. return err;
  362. }
  363. /**
  364. * ice_sync_fltr_subtask - Sync the VSI filter list with HW
  365. * @pf: board private structure
  366. */
  367. static void ice_sync_fltr_subtask(struct ice_pf *pf)
  368. {
  369. int v;
  370. if (!pf || !(test_bit(ICE_FLAG_FLTR_SYNC, pf->flags)))
  371. return;
  372. clear_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
  373. for (v = 0; v < pf->num_alloc_vsi; v++)
  374. if (pf->vsi[v] && ice_vsi_fltr_changed(pf->vsi[v]) &&
  375. ice_vsi_sync_fltr(pf->vsi[v])) {
  376. /* come back and try again later */
  377. set_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
  378. break;
  379. }
  380. }
  381. /**
  382. * ice_is_reset_recovery_pending - schedule a reset
  383. * @state: pf state field
  384. */
  385. static bool ice_is_reset_recovery_pending(unsigned long int *state)
  386. {
  387. return test_bit(__ICE_RESET_RECOVERY_PENDING, state);
  388. }
  389. /**
  390. * ice_prepare_for_reset - prep for the core to reset
  391. * @pf: board private structure
  392. *
  393. * Inform or close all dependent features in prep for reset.
  394. */
  395. static void
  396. ice_prepare_for_reset(struct ice_pf *pf)
  397. {
  398. struct ice_hw *hw = &pf->hw;
  399. u32 v;
  400. ice_for_each_vsi(pf, v)
  401. if (pf->vsi[v])
  402. ice_remove_vsi_fltr(hw, pf->vsi[v]->vsi_num);
  403. dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
  404. /* disable the VSIs and their queues that are not already DOWN */
  405. /* pf_dis_all_vsi modifies netdev structures -rtnl_lock needed */
  406. ice_pf_dis_all_vsi(pf);
  407. ice_for_each_vsi(pf, v)
  408. if (pf->vsi[v])
  409. pf->vsi[v]->vsi_num = 0;
  410. ice_shutdown_all_ctrlq(hw);
  411. }
  412. /**
  413. * ice_do_reset - Initiate one of many types of resets
  414. * @pf: board private structure
  415. * @reset_type: reset type requested
  416. * before this function was called.
  417. */
  418. static void ice_do_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
  419. {
  420. struct device *dev = &pf->pdev->dev;
  421. struct ice_hw *hw = &pf->hw;
  422. dev_dbg(dev, "reset_type 0x%x requested\n", reset_type);
  423. WARN_ON(in_interrupt());
  424. /* PFR is a bit of a special case because it doesn't result in an OICR
  425. * interrupt. So for PFR, we prepare for reset, issue the reset and
  426. * rebuild sequentially.
  427. */
  428. if (reset_type == ICE_RESET_PFR) {
  429. set_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
  430. ice_prepare_for_reset(pf);
  431. }
  432. /* trigger the reset */
  433. if (ice_reset(hw, reset_type)) {
  434. dev_err(dev, "reset %d failed\n", reset_type);
  435. set_bit(__ICE_RESET_FAILED, pf->state);
  436. clear_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
  437. return;
  438. }
  439. if (reset_type == ICE_RESET_PFR) {
  440. pf->pfr_count++;
  441. ice_rebuild(pf);
  442. clear_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
  443. }
  444. }
  445. /**
  446. * ice_reset_subtask - Set up for resetting the device and driver
  447. * @pf: board private structure
  448. */
  449. static void ice_reset_subtask(struct ice_pf *pf)
  450. {
  451. enum ice_reset_req reset_type;
  452. rtnl_lock();
  453. /* When a CORER/GLOBR/EMPR is about to happen, the hardware triggers an
  454. * OICR interrupt. The OICR handler (ice_misc_intr) determines what
  455. * type of reset happened and sets __ICE_RESET_RECOVERY_PENDING bit in
  456. * pf->state. So if reset/recovery is pending (as indicated by this bit)
  457. * we do a rebuild and return.
  458. */
  459. if (ice_is_reset_recovery_pending(pf->state)) {
  460. clear_bit(__ICE_GLOBR_RECV, pf->state);
  461. clear_bit(__ICE_CORER_RECV, pf->state);
  462. ice_prepare_for_reset(pf);
  463. /* make sure we are ready to rebuild */
  464. if (ice_check_reset(&pf->hw))
  465. set_bit(__ICE_RESET_FAILED, pf->state);
  466. else
  467. ice_rebuild(pf);
  468. clear_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
  469. goto unlock;
  470. }
  471. /* No pending resets to finish processing. Check for new resets */
  472. if (test_and_clear_bit(__ICE_GLOBR_REQ, pf->state))
  473. reset_type = ICE_RESET_GLOBR;
  474. else if (test_and_clear_bit(__ICE_CORER_REQ, pf->state))
  475. reset_type = ICE_RESET_CORER;
  476. else if (test_and_clear_bit(__ICE_PFR_REQ, pf->state))
  477. reset_type = ICE_RESET_PFR;
  478. else
  479. goto unlock;
  480. /* reset if not already down or resetting */
  481. if (!test_bit(__ICE_DOWN, pf->state) &&
  482. !test_bit(__ICE_CFG_BUSY, pf->state)) {
  483. ice_do_reset(pf, reset_type);
  484. }
  485. unlock:
  486. rtnl_unlock();
  487. }
  488. /**
  489. * ice_watchdog_subtask - periodic tasks not using event driven scheduling
  490. * @pf: board private structure
  491. */
  492. static void ice_watchdog_subtask(struct ice_pf *pf)
  493. {
  494. int i;
  495. /* if interface is down do nothing */
  496. if (test_bit(__ICE_DOWN, pf->state) ||
  497. test_bit(__ICE_CFG_BUSY, pf->state))
  498. return;
  499. /* make sure we don't do these things too often */
  500. if (time_before(jiffies,
  501. pf->serv_tmr_prev + pf->serv_tmr_period))
  502. return;
  503. pf->serv_tmr_prev = jiffies;
  504. /* Update the stats for active netdevs so the network stack
  505. * can look at updated numbers whenever it cares to
  506. */
  507. ice_update_pf_stats(pf);
  508. for (i = 0; i < pf->num_alloc_vsi; i++)
  509. if (pf->vsi[i] && pf->vsi[i]->netdev)
  510. ice_update_vsi_stats(pf->vsi[i]);
  511. }
  512. /**
  513. * ice_print_link_msg - print link up or down message
  514. * @vsi: the VSI whose link status is being queried
  515. * @isup: boolean for if the link is now up or down
  516. */
  517. void ice_print_link_msg(struct ice_vsi *vsi, bool isup)
  518. {
  519. const char *speed;
  520. const char *fc;
  521. if (vsi->current_isup == isup)
  522. return;
  523. vsi->current_isup = isup;
  524. if (!isup) {
  525. netdev_info(vsi->netdev, "NIC Link is Down\n");
  526. return;
  527. }
  528. switch (vsi->port_info->phy.link_info.link_speed) {
  529. case ICE_AQ_LINK_SPEED_40GB:
  530. speed = "40 G";
  531. break;
  532. case ICE_AQ_LINK_SPEED_25GB:
  533. speed = "25 G";
  534. break;
  535. case ICE_AQ_LINK_SPEED_20GB:
  536. speed = "20 G";
  537. break;
  538. case ICE_AQ_LINK_SPEED_10GB:
  539. speed = "10 G";
  540. break;
  541. case ICE_AQ_LINK_SPEED_5GB:
  542. speed = "5 G";
  543. break;
  544. case ICE_AQ_LINK_SPEED_2500MB:
  545. speed = "2.5 G";
  546. break;
  547. case ICE_AQ_LINK_SPEED_1000MB:
  548. speed = "1 G";
  549. break;
  550. case ICE_AQ_LINK_SPEED_100MB:
  551. speed = "100 M";
  552. break;
  553. default:
  554. speed = "Unknown";
  555. break;
  556. }
  557. switch (vsi->port_info->fc.current_mode) {
  558. case ICE_FC_FULL:
  559. fc = "RX/TX";
  560. break;
  561. case ICE_FC_TX_PAUSE:
  562. fc = "TX";
  563. break;
  564. case ICE_FC_RX_PAUSE:
  565. fc = "RX";
  566. break;
  567. default:
  568. fc = "Unknown";
  569. break;
  570. }
  571. netdev_info(vsi->netdev, "NIC Link is up %sbps, Flow Control: %s\n",
  572. speed, fc);
  573. }
  574. /**
  575. * ice_init_link_events - enable/initialize link events
  576. * @pi: pointer to the port_info instance
  577. *
  578. * Returns -EIO on failure, 0 on success
  579. */
  580. static int ice_init_link_events(struct ice_port_info *pi)
  581. {
  582. u16 mask;
  583. mask = ~((u16)(ICE_AQ_LINK_EVENT_UPDOWN | ICE_AQ_LINK_EVENT_MEDIA_NA |
  584. ICE_AQ_LINK_EVENT_MODULE_QUAL_FAIL));
  585. if (ice_aq_set_event_mask(pi->hw, pi->lport, mask, NULL)) {
  586. dev_dbg(ice_hw_to_dev(pi->hw),
  587. "Failed to set link event mask for port %d\n",
  588. pi->lport);
  589. return -EIO;
  590. }
  591. if (ice_aq_get_link_info(pi, true, NULL, NULL)) {
  592. dev_dbg(ice_hw_to_dev(pi->hw),
  593. "Failed to enable link events for port %d\n",
  594. pi->lport);
  595. return -EIO;
  596. }
  597. return 0;
  598. }
  599. /**
  600. * ice_vsi_link_event - update the vsi's netdev
  601. * @vsi: the vsi on which the link event occurred
  602. * @link_up: whether or not the vsi needs to be set up or down
  603. */
  604. static void ice_vsi_link_event(struct ice_vsi *vsi, bool link_up)
  605. {
  606. if (!vsi || test_bit(__ICE_DOWN, vsi->state))
  607. return;
  608. if (vsi->type == ICE_VSI_PF) {
  609. if (!vsi->netdev) {
  610. dev_dbg(&vsi->back->pdev->dev,
  611. "vsi->netdev is not initialized!\n");
  612. return;
  613. }
  614. if (link_up) {
  615. netif_carrier_on(vsi->netdev);
  616. netif_tx_wake_all_queues(vsi->netdev);
  617. } else {
  618. netif_carrier_off(vsi->netdev);
  619. netif_tx_stop_all_queues(vsi->netdev);
  620. }
  621. }
  622. }
  623. /**
  624. * ice_link_event - process the link event
  625. * @pf: pf that the link event is associated with
  626. * @pi: port_info for the port that the link event is associated with
  627. *
  628. * Returns -EIO if ice_get_link_status() fails
  629. * Returns 0 on success
  630. */
  631. static int
  632. ice_link_event(struct ice_pf *pf, struct ice_port_info *pi)
  633. {
  634. u8 new_link_speed, old_link_speed;
  635. struct ice_phy_info *phy_info;
  636. bool new_link_same_as_old;
  637. bool new_link, old_link;
  638. u8 lport;
  639. u16 v;
  640. phy_info = &pi->phy;
  641. phy_info->link_info_old = phy_info->link_info;
  642. /* Force ice_get_link_status() to update link info */
  643. phy_info->get_link_info = true;
  644. old_link = (phy_info->link_info_old.link_info & ICE_AQ_LINK_UP);
  645. old_link_speed = phy_info->link_info_old.link_speed;
  646. lport = pi->lport;
  647. if (ice_get_link_status(pi, &new_link)) {
  648. dev_dbg(&pf->pdev->dev,
  649. "Could not get link status for port %d\n", lport);
  650. return -EIO;
  651. }
  652. new_link_speed = phy_info->link_info.link_speed;
  653. new_link_same_as_old = (new_link == old_link &&
  654. new_link_speed == old_link_speed);
  655. ice_for_each_vsi(pf, v) {
  656. struct ice_vsi *vsi = pf->vsi[v];
  657. if (!vsi || !vsi->port_info)
  658. continue;
  659. if (new_link_same_as_old &&
  660. (test_bit(__ICE_DOWN, vsi->state) ||
  661. new_link == netif_carrier_ok(vsi->netdev)))
  662. continue;
  663. if (vsi->port_info->lport == lport) {
  664. ice_print_link_msg(vsi, new_link);
  665. ice_vsi_link_event(vsi, new_link);
  666. }
  667. }
  668. return 0;
  669. }
  670. /**
  671. * ice_handle_link_event - handle link event via ARQ
  672. * @pf: pf that the link event is associated with
  673. *
  674. * Return -EINVAL if port_info is null
  675. * Return status on succes
  676. */
  677. static int ice_handle_link_event(struct ice_pf *pf)
  678. {
  679. struct ice_port_info *port_info;
  680. int status;
  681. port_info = pf->hw.port_info;
  682. if (!port_info)
  683. return -EINVAL;
  684. status = ice_link_event(pf, port_info);
  685. if (status)
  686. dev_dbg(&pf->pdev->dev,
  687. "Could not process link event, error %d\n", status);
  688. return status;
  689. }
  690. /**
  691. * __ice_clean_ctrlq - helper function to clean controlq rings
  692. * @pf: ptr to struct ice_pf
  693. * @q_type: specific Control queue type
  694. */
  695. static int __ice_clean_ctrlq(struct ice_pf *pf, enum ice_ctl_q q_type)
  696. {
  697. struct ice_rq_event_info event;
  698. struct ice_hw *hw = &pf->hw;
  699. struct ice_ctl_q_info *cq;
  700. u16 pending, i = 0;
  701. const char *qtype;
  702. u32 oldval, val;
  703. /* Do not clean control queue if/when PF reset fails */
  704. if (test_bit(__ICE_RESET_FAILED, pf->state))
  705. return 0;
  706. switch (q_type) {
  707. case ICE_CTL_Q_ADMIN:
  708. cq = &hw->adminq;
  709. qtype = "Admin";
  710. break;
  711. default:
  712. dev_warn(&pf->pdev->dev, "Unknown control queue type 0x%x\n",
  713. q_type);
  714. return 0;
  715. }
  716. /* check for error indications - PF_xx_AxQLEN register layout for
  717. * FW/MBX/SB are identical so just use defines for PF_FW_AxQLEN.
  718. */
  719. val = rd32(hw, cq->rq.len);
  720. if (val & (PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
  721. PF_FW_ARQLEN_ARQCRIT_M)) {
  722. oldval = val;
  723. if (val & PF_FW_ARQLEN_ARQVFE_M)
  724. dev_dbg(&pf->pdev->dev,
  725. "%s Receive Queue VF Error detected\n", qtype);
  726. if (val & PF_FW_ARQLEN_ARQOVFL_M) {
  727. dev_dbg(&pf->pdev->dev,
  728. "%s Receive Queue Overflow Error detected\n",
  729. qtype);
  730. }
  731. if (val & PF_FW_ARQLEN_ARQCRIT_M)
  732. dev_dbg(&pf->pdev->dev,
  733. "%s Receive Queue Critical Error detected\n",
  734. qtype);
  735. val &= ~(PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
  736. PF_FW_ARQLEN_ARQCRIT_M);
  737. if (oldval != val)
  738. wr32(hw, cq->rq.len, val);
  739. }
  740. val = rd32(hw, cq->sq.len);
  741. if (val & (PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
  742. PF_FW_ATQLEN_ATQCRIT_M)) {
  743. oldval = val;
  744. if (val & PF_FW_ATQLEN_ATQVFE_M)
  745. dev_dbg(&pf->pdev->dev,
  746. "%s Send Queue VF Error detected\n", qtype);
  747. if (val & PF_FW_ATQLEN_ATQOVFL_M) {
  748. dev_dbg(&pf->pdev->dev,
  749. "%s Send Queue Overflow Error detected\n",
  750. qtype);
  751. }
  752. if (val & PF_FW_ATQLEN_ATQCRIT_M)
  753. dev_dbg(&pf->pdev->dev,
  754. "%s Send Queue Critical Error detected\n",
  755. qtype);
  756. val &= ~(PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
  757. PF_FW_ATQLEN_ATQCRIT_M);
  758. if (oldval != val)
  759. wr32(hw, cq->sq.len, val);
  760. }
  761. event.buf_len = cq->rq_buf_size;
  762. event.msg_buf = devm_kzalloc(&pf->pdev->dev, event.buf_len,
  763. GFP_KERNEL);
  764. if (!event.msg_buf)
  765. return 0;
  766. do {
  767. enum ice_status ret;
  768. u16 opcode;
  769. ret = ice_clean_rq_elem(hw, cq, &event, &pending);
  770. if (ret == ICE_ERR_AQ_NO_WORK)
  771. break;
  772. if (ret) {
  773. dev_err(&pf->pdev->dev,
  774. "%s Receive Queue event error %d\n", qtype,
  775. ret);
  776. break;
  777. }
  778. opcode = le16_to_cpu(event.desc.opcode);
  779. switch (opcode) {
  780. case ice_aqc_opc_get_link_status:
  781. if (ice_handle_link_event(pf))
  782. dev_err(&pf->pdev->dev,
  783. "Could not handle link event");
  784. break;
  785. default:
  786. dev_dbg(&pf->pdev->dev,
  787. "%s Receive Queue unknown event 0x%04x ignored\n",
  788. qtype, opcode);
  789. break;
  790. }
  791. } while (pending && (i++ < ICE_DFLT_IRQ_WORK));
  792. devm_kfree(&pf->pdev->dev, event.msg_buf);
  793. return pending && (i == ICE_DFLT_IRQ_WORK);
  794. }
  795. /**
  796. * ice_clean_adminq_subtask - clean the AdminQ rings
  797. * @pf: board private structure
  798. */
  799. static void ice_clean_adminq_subtask(struct ice_pf *pf)
  800. {
  801. struct ice_hw *hw = &pf->hw;
  802. u32 val;
  803. if (!test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
  804. return;
  805. if (__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN))
  806. return;
  807. clear_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
  808. /* re-enable Admin queue interrupt causes */
  809. val = rd32(hw, PFINT_FW_CTL);
  810. wr32(hw, PFINT_FW_CTL, (val | PFINT_FW_CTL_CAUSE_ENA_M));
  811. ice_flush(hw);
  812. }
  813. /**
  814. * ice_service_task_schedule - schedule the service task to wake up
  815. * @pf: board private structure
  816. *
  817. * If not already scheduled, this puts the task into the work queue.
  818. */
  819. static void ice_service_task_schedule(struct ice_pf *pf)
  820. {
  821. if (!test_bit(__ICE_DOWN, pf->state) &&
  822. !test_and_set_bit(__ICE_SERVICE_SCHED, pf->state))
  823. queue_work(ice_wq, &pf->serv_task);
  824. }
  825. /**
  826. * ice_service_task_complete - finish up the service task
  827. * @pf: board private structure
  828. */
  829. static void ice_service_task_complete(struct ice_pf *pf)
  830. {
  831. WARN_ON(!test_bit(__ICE_SERVICE_SCHED, pf->state));
  832. /* force memory (pf->state) to sync before next service task */
  833. smp_mb__before_atomic();
  834. clear_bit(__ICE_SERVICE_SCHED, pf->state);
  835. }
  836. /**
  837. * ice_service_timer - timer callback to schedule service task
  838. * @t: pointer to timer_list
  839. */
  840. static void ice_service_timer(struct timer_list *t)
  841. {
  842. struct ice_pf *pf = from_timer(pf, t, serv_tmr);
  843. mod_timer(&pf->serv_tmr, round_jiffies(pf->serv_tmr_period + jiffies));
  844. ice_service_task_schedule(pf);
  845. }
  846. /**
  847. * ice_service_task - manage and run subtasks
  848. * @work: pointer to work_struct contained by the PF struct
  849. */
  850. static void ice_service_task(struct work_struct *work)
  851. {
  852. struct ice_pf *pf = container_of(work, struct ice_pf, serv_task);
  853. unsigned long start_time = jiffies;
  854. /* subtasks */
  855. /* process reset requests first */
  856. ice_reset_subtask(pf);
  857. /* bail if a reset/recovery cycle is pending */
  858. if (ice_is_reset_recovery_pending(pf->state) ||
  859. test_bit(__ICE_SUSPENDED, pf->state)) {
  860. ice_service_task_complete(pf);
  861. return;
  862. }
  863. ice_sync_fltr_subtask(pf);
  864. ice_watchdog_subtask(pf);
  865. ice_clean_adminq_subtask(pf);
  866. /* Clear __ICE_SERVICE_SCHED flag to allow scheduling next event */
  867. ice_service_task_complete(pf);
  868. /* If the tasks have taken longer than one service timer period
  869. * or there is more work to be done, reset the service timer to
  870. * schedule the service task now.
  871. */
  872. if (time_after(jiffies, (start_time + pf->serv_tmr_period)) ||
  873. test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
  874. mod_timer(&pf->serv_tmr, jiffies);
  875. }
  876. /**
  877. * ice_set_ctrlq_len - helper function to set controlq length
  878. * @hw: pointer to the hw instance
  879. */
  880. static void ice_set_ctrlq_len(struct ice_hw *hw)
  881. {
  882. hw->adminq.num_rq_entries = ICE_AQ_LEN;
  883. hw->adminq.num_sq_entries = ICE_AQ_LEN;
  884. hw->adminq.rq_buf_size = ICE_AQ_MAX_BUF_LEN;
  885. hw->adminq.sq_buf_size = ICE_AQ_MAX_BUF_LEN;
  886. }
  887. /**
  888. * ice_irq_affinity_notify - Callback for affinity changes
  889. * @notify: context as to what irq was changed
  890. * @mask: the new affinity mask
  891. *
  892. * This is a callback function used by the irq_set_affinity_notifier function
  893. * so that we may register to receive changes to the irq affinity masks.
  894. */
  895. static void ice_irq_affinity_notify(struct irq_affinity_notify *notify,
  896. const cpumask_t *mask)
  897. {
  898. struct ice_q_vector *q_vector =
  899. container_of(notify, struct ice_q_vector, affinity_notify);
  900. cpumask_copy(&q_vector->affinity_mask, mask);
  901. }
  902. /**
  903. * ice_irq_affinity_release - Callback for affinity notifier release
  904. * @ref: internal core kernel usage
  905. *
  906. * This is a callback function used by the irq_set_affinity_notifier function
  907. * to inform the current notification subscriber that they will no longer
  908. * receive notifications.
  909. */
  910. static void ice_irq_affinity_release(struct kref __always_unused *ref) {}
  911. /**
  912. * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
  913. * @vsi: the VSI being un-configured
  914. */
  915. static void ice_vsi_dis_irq(struct ice_vsi *vsi)
  916. {
  917. struct ice_pf *pf = vsi->back;
  918. struct ice_hw *hw = &pf->hw;
  919. int base = vsi->base_vector;
  920. u32 val;
  921. int i;
  922. /* disable interrupt causation from each queue */
  923. if (vsi->tx_rings) {
  924. ice_for_each_txq(vsi, i) {
  925. if (vsi->tx_rings[i]) {
  926. u16 reg;
  927. reg = vsi->tx_rings[i]->reg_idx;
  928. val = rd32(hw, QINT_TQCTL(reg));
  929. val &= ~QINT_TQCTL_CAUSE_ENA_M;
  930. wr32(hw, QINT_TQCTL(reg), val);
  931. }
  932. }
  933. }
  934. if (vsi->rx_rings) {
  935. ice_for_each_rxq(vsi, i) {
  936. if (vsi->rx_rings[i]) {
  937. u16 reg;
  938. reg = vsi->rx_rings[i]->reg_idx;
  939. val = rd32(hw, QINT_RQCTL(reg));
  940. val &= ~QINT_RQCTL_CAUSE_ENA_M;
  941. wr32(hw, QINT_RQCTL(reg), val);
  942. }
  943. }
  944. }
  945. /* disable each interrupt */
  946. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
  947. for (i = vsi->base_vector;
  948. i < (vsi->num_q_vectors + vsi->base_vector); i++)
  949. wr32(hw, GLINT_DYN_CTL(i), 0);
  950. ice_flush(hw);
  951. for (i = 0; i < vsi->num_q_vectors; i++)
  952. synchronize_irq(pf->msix_entries[i + base].vector);
  953. }
  954. }
  955. /**
  956. * ice_vsi_ena_irq - Enable IRQ for the given VSI
  957. * @vsi: the VSI being configured
  958. */
  959. static int ice_vsi_ena_irq(struct ice_vsi *vsi)
  960. {
  961. struct ice_pf *pf = vsi->back;
  962. struct ice_hw *hw = &pf->hw;
  963. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
  964. int i;
  965. for (i = 0; i < vsi->num_q_vectors; i++)
  966. ice_irq_dynamic_ena(hw, vsi, vsi->q_vectors[i]);
  967. }
  968. ice_flush(hw);
  969. return 0;
  970. }
  971. /**
  972. * ice_vsi_delete - delete a VSI from the switch
  973. * @vsi: pointer to VSI being removed
  974. */
  975. static void ice_vsi_delete(struct ice_vsi *vsi)
  976. {
  977. struct ice_pf *pf = vsi->back;
  978. struct ice_vsi_ctx ctxt;
  979. enum ice_status status;
  980. ctxt.vsi_num = vsi->vsi_num;
  981. memcpy(&ctxt.info, &vsi->info, sizeof(struct ice_aqc_vsi_props));
  982. status = ice_aq_free_vsi(&pf->hw, &ctxt, false, NULL);
  983. if (status)
  984. dev_err(&pf->pdev->dev, "Failed to delete VSI %i in FW\n",
  985. vsi->vsi_num);
  986. }
  987. /**
  988. * ice_vsi_req_irq_msix - get MSI-X vectors from the OS for the VSI
  989. * @vsi: the VSI being configured
  990. * @basename: name for the vector
  991. */
  992. static int ice_vsi_req_irq_msix(struct ice_vsi *vsi, char *basename)
  993. {
  994. int q_vectors = vsi->num_q_vectors;
  995. struct ice_pf *pf = vsi->back;
  996. int base = vsi->base_vector;
  997. int rx_int_idx = 0;
  998. int tx_int_idx = 0;
  999. int vector, err;
  1000. int irq_num;
  1001. for (vector = 0; vector < q_vectors; vector++) {
  1002. struct ice_q_vector *q_vector = vsi->q_vectors[vector];
  1003. irq_num = pf->msix_entries[base + vector].vector;
  1004. if (q_vector->tx.ring && q_vector->rx.ring) {
  1005. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  1006. "%s-%s-%d", basename, "TxRx", rx_int_idx++);
  1007. tx_int_idx++;
  1008. } else if (q_vector->rx.ring) {
  1009. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  1010. "%s-%s-%d", basename, "rx", rx_int_idx++);
  1011. } else if (q_vector->tx.ring) {
  1012. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  1013. "%s-%s-%d", basename, "tx", tx_int_idx++);
  1014. } else {
  1015. /* skip this unused q_vector */
  1016. continue;
  1017. }
  1018. err = devm_request_irq(&pf->pdev->dev,
  1019. pf->msix_entries[base + vector].vector,
  1020. vsi->irq_handler, 0, q_vector->name,
  1021. q_vector);
  1022. if (err) {
  1023. netdev_err(vsi->netdev,
  1024. "MSIX request_irq failed, error: %d\n", err);
  1025. goto free_q_irqs;
  1026. }
  1027. /* register for affinity change notifications */
  1028. q_vector->affinity_notify.notify = ice_irq_affinity_notify;
  1029. q_vector->affinity_notify.release = ice_irq_affinity_release;
  1030. irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
  1031. /* assign the mask for this irq */
  1032. irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
  1033. }
  1034. vsi->irqs_ready = true;
  1035. return 0;
  1036. free_q_irqs:
  1037. while (vector) {
  1038. vector--;
  1039. irq_num = pf->msix_entries[base + vector].vector,
  1040. irq_set_affinity_notifier(irq_num, NULL);
  1041. irq_set_affinity_hint(irq_num, NULL);
  1042. devm_free_irq(&pf->pdev->dev, irq_num, &vsi->q_vectors[vector]);
  1043. }
  1044. return err;
  1045. }
  1046. /**
  1047. * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
  1048. * @vsi: the VSI being configured
  1049. */
  1050. static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
  1051. {
  1052. struct ice_hw_common_caps *cap;
  1053. struct ice_pf *pf = vsi->back;
  1054. if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
  1055. vsi->rss_size = 1;
  1056. return;
  1057. }
  1058. cap = &pf->hw.func_caps.common_cap;
  1059. switch (vsi->type) {
  1060. case ICE_VSI_PF:
  1061. /* PF VSI will inherit RSS instance of PF */
  1062. vsi->rss_table_size = cap->rss_table_size;
  1063. vsi->rss_size = min_t(int, num_online_cpus(),
  1064. BIT(cap->rss_table_entry_width));
  1065. vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF;
  1066. break;
  1067. default:
  1068. dev_warn(&pf->pdev->dev, "Unknown VSI type %d\n", vsi->type);
  1069. break;
  1070. }
  1071. }
  1072. /**
  1073. * ice_vsi_setup_q_map - Setup a VSI queue map
  1074. * @vsi: the VSI being configured
  1075. * @ctxt: VSI context structure
  1076. */
  1077. static void ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
  1078. {
  1079. u16 offset = 0, qmap = 0, numq_tc;
  1080. u16 pow = 0, max_rss = 0, qcount;
  1081. u16 qcount_tx = vsi->alloc_txq;
  1082. u16 qcount_rx = vsi->alloc_rxq;
  1083. bool ena_tc0 = false;
  1084. int i;
  1085. /* at least TC0 should be enabled by default */
  1086. if (vsi->tc_cfg.numtc) {
  1087. if (!(vsi->tc_cfg.ena_tc & BIT(0)))
  1088. ena_tc0 = true;
  1089. } else {
  1090. ena_tc0 = true;
  1091. }
  1092. if (ena_tc0) {
  1093. vsi->tc_cfg.numtc++;
  1094. vsi->tc_cfg.ena_tc |= 1;
  1095. }
  1096. numq_tc = qcount_rx / vsi->tc_cfg.numtc;
  1097. /* TC mapping is a function of the number of Rx queues assigned to the
  1098. * VSI for each traffic class and the offset of these queues.
  1099. * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
  1100. * queues allocated to TC0. No:of queues is a power-of-2.
  1101. *
  1102. * If TC is not enabled, the queue offset is set to 0, and allocate one
  1103. * queue, this way, traffic for the given TC will be sent to the default
  1104. * queue.
  1105. *
  1106. * Setup number and offset of Rx queues for all TCs for the VSI
  1107. */
  1108. /* qcount will change if RSS is enabled */
  1109. if (test_bit(ICE_FLAG_RSS_ENA, vsi->back->flags)) {
  1110. if (vsi->type == ICE_VSI_PF)
  1111. max_rss = ICE_MAX_LG_RSS_QS;
  1112. else
  1113. max_rss = ICE_MAX_SMALL_RSS_QS;
  1114. qcount = min_t(int, numq_tc, max_rss);
  1115. qcount = min_t(int, qcount, vsi->rss_size);
  1116. } else {
  1117. qcount = numq_tc;
  1118. }
  1119. /* find higher power-of-2 of qcount */
  1120. pow = ilog2(qcount);
  1121. if (!is_power_of_2(qcount))
  1122. pow++;
  1123. for (i = 0; i < ICE_MAX_TRAFFIC_CLASS; i++) {
  1124. if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
  1125. /* TC is not enabled */
  1126. vsi->tc_cfg.tc_info[i].qoffset = 0;
  1127. vsi->tc_cfg.tc_info[i].qcount = 1;
  1128. ctxt->info.tc_mapping[i] = 0;
  1129. continue;
  1130. }
  1131. /* TC is enabled */
  1132. vsi->tc_cfg.tc_info[i].qoffset = offset;
  1133. vsi->tc_cfg.tc_info[i].qcount = qcount;
  1134. qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
  1135. ICE_AQ_VSI_TC_Q_OFFSET_M) |
  1136. ((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
  1137. ICE_AQ_VSI_TC_Q_NUM_M);
  1138. offset += qcount;
  1139. ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
  1140. }
  1141. vsi->num_txq = qcount_tx;
  1142. vsi->num_rxq = offset;
  1143. /* Rx queue mapping */
  1144. ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
  1145. /* q_mapping buffer holds the info for the first queue allocated for
  1146. * this VSI in the PF space and also the number of queues associated
  1147. * with this VSI.
  1148. */
  1149. ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
  1150. ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
  1151. }
  1152. /**
  1153. * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
  1154. * @ctxt: the VSI context being set
  1155. *
  1156. * This initializes a default VSI context for all sections except the Queues.
  1157. */
  1158. static void ice_set_dflt_vsi_ctx(struct ice_vsi_ctx *ctxt)
  1159. {
  1160. u32 table = 0;
  1161. memset(&ctxt->info, 0, sizeof(ctxt->info));
  1162. /* VSI's should be allocated from shared pool */
  1163. ctxt->alloc_from_pool = true;
  1164. /* Src pruning enabled by default */
  1165. ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
  1166. /* Traffic from VSI can be sent to LAN */
  1167. ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
  1168. /* Allow all packets untagged/tagged */
  1169. ctxt->info.port_vlan_flags = ((ICE_AQ_VSI_PVLAN_MODE_ALL &
  1170. ICE_AQ_VSI_PVLAN_MODE_M) >>
  1171. ICE_AQ_VSI_PVLAN_MODE_S);
  1172. /* Show VLAN/UP from packets in Rx descriptors */
  1173. ctxt->info.port_vlan_flags |= ((ICE_AQ_VSI_PVLAN_EMOD_STR_BOTH &
  1174. ICE_AQ_VSI_PVLAN_EMOD_M) >>
  1175. ICE_AQ_VSI_PVLAN_EMOD_S);
  1176. /* Have 1:1 UP mapping for both ingress/egress tables */
  1177. table |= ICE_UP_TABLE_TRANSLATE(0, 0);
  1178. table |= ICE_UP_TABLE_TRANSLATE(1, 1);
  1179. table |= ICE_UP_TABLE_TRANSLATE(2, 2);
  1180. table |= ICE_UP_TABLE_TRANSLATE(3, 3);
  1181. table |= ICE_UP_TABLE_TRANSLATE(4, 4);
  1182. table |= ICE_UP_TABLE_TRANSLATE(5, 5);
  1183. table |= ICE_UP_TABLE_TRANSLATE(6, 6);
  1184. table |= ICE_UP_TABLE_TRANSLATE(7, 7);
  1185. ctxt->info.ingress_table = cpu_to_le32(table);
  1186. ctxt->info.egress_table = cpu_to_le32(table);
  1187. /* Have 1:1 UP mapping for outer to inner UP table */
  1188. ctxt->info.outer_up_table = cpu_to_le32(table);
  1189. /* No Outer tag support outer_tag_flags remains to zero */
  1190. }
  1191. /**
  1192. * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
  1193. * @ctxt: the VSI context being set
  1194. * @vsi: the VSI being configured
  1195. */
  1196. static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
  1197. {
  1198. u8 lut_type, hash_type;
  1199. switch (vsi->type) {
  1200. case ICE_VSI_PF:
  1201. /* PF VSI will inherit RSS instance of PF */
  1202. lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
  1203. hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
  1204. break;
  1205. default:
  1206. dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
  1207. vsi->type);
  1208. return;
  1209. }
  1210. ctxt->info.q_opt_rss = ((lut_type << ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
  1211. ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
  1212. ((hash_type << ICE_AQ_VSI_Q_OPT_RSS_HASH_S) &
  1213. ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
  1214. }
  1215. /**
  1216. * ice_vsi_add - Create a new VSI or fetch preallocated VSI
  1217. * @vsi: the VSI being configured
  1218. *
  1219. * This initializes a VSI context depending on the VSI type to be added and
  1220. * passes it down to the add_vsi aq command to create a new VSI.
  1221. */
  1222. static int ice_vsi_add(struct ice_vsi *vsi)
  1223. {
  1224. struct ice_vsi_ctx ctxt = { 0 };
  1225. struct ice_pf *pf = vsi->back;
  1226. struct ice_hw *hw = &pf->hw;
  1227. int ret = 0;
  1228. switch (vsi->type) {
  1229. case ICE_VSI_PF:
  1230. ctxt.flags = ICE_AQ_VSI_TYPE_PF;
  1231. break;
  1232. default:
  1233. return -ENODEV;
  1234. }
  1235. ice_set_dflt_vsi_ctx(&ctxt);
  1236. /* if the switch is in VEB mode, allow VSI loopback */
  1237. if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
  1238. ctxt.info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
  1239. /* Set LUT type and HASH type if RSS is enabled */
  1240. if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
  1241. ice_set_rss_vsi_ctx(&ctxt, vsi);
  1242. ctxt.info.sw_id = vsi->port_info->sw_id;
  1243. ice_vsi_setup_q_map(vsi, &ctxt);
  1244. ret = ice_aq_add_vsi(hw, &ctxt, NULL);
  1245. if (ret) {
  1246. dev_err(&vsi->back->pdev->dev,
  1247. "Add VSI AQ call failed, err %d\n", ret);
  1248. return -EIO;
  1249. }
  1250. vsi->info = ctxt.info;
  1251. vsi->vsi_num = ctxt.vsi_num;
  1252. return ret;
  1253. }
  1254. /**
  1255. * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
  1256. * @vsi: the VSI being cleaned up
  1257. */
  1258. static void ice_vsi_release_msix(struct ice_vsi *vsi)
  1259. {
  1260. struct ice_pf *pf = vsi->back;
  1261. u16 vector = vsi->base_vector;
  1262. struct ice_hw *hw = &pf->hw;
  1263. u32 txq = 0;
  1264. u32 rxq = 0;
  1265. int i, q;
  1266. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  1267. struct ice_q_vector *q_vector = vsi->q_vectors[i];
  1268. wr32(hw, GLINT_ITR(ICE_RX_ITR, vector), 0);
  1269. wr32(hw, GLINT_ITR(ICE_TX_ITR, vector), 0);
  1270. for (q = 0; q < q_vector->num_ring_tx; q++) {
  1271. wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
  1272. txq++;
  1273. }
  1274. for (q = 0; q < q_vector->num_ring_rx; q++) {
  1275. wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
  1276. rxq++;
  1277. }
  1278. }
  1279. ice_flush(hw);
  1280. }
  1281. /**
  1282. * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
  1283. * @vsi: the VSI having rings deallocated
  1284. */
  1285. static void ice_vsi_clear_rings(struct ice_vsi *vsi)
  1286. {
  1287. int i;
  1288. if (vsi->tx_rings) {
  1289. for (i = 0; i < vsi->alloc_txq; i++) {
  1290. if (vsi->tx_rings[i]) {
  1291. kfree_rcu(vsi->tx_rings[i], rcu);
  1292. vsi->tx_rings[i] = NULL;
  1293. }
  1294. }
  1295. }
  1296. if (vsi->rx_rings) {
  1297. for (i = 0; i < vsi->alloc_rxq; i++) {
  1298. if (vsi->rx_rings[i]) {
  1299. kfree_rcu(vsi->rx_rings[i], rcu);
  1300. vsi->rx_rings[i] = NULL;
  1301. }
  1302. }
  1303. }
  1304. }
  1305. /**
  1306. * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
  1307. * @vsi: VSI which is having rings allocated
  1308. */
  1309. static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
  1310. {
  1311. struct ice_pf *pf = vsi->back;
  1312. int i;
  1313. /* Allocate tx_rings */
  1314. for (i = 0; i < vsi->alloc_txq; i++) {
  1315. struct ice_ring *ring;
  1316. /* allocate with kzalloc(), free with kfree_rcu() */
  1317. ring = kzalloc(sizeof(*ring), GFP_KERNEL);
  1318. if (!ring)
  1319. goto err_out;
  1320. ring->q_index = i;
  1321. ring->reg_idx = vsi->txq_map[i];
  1322. ring->ring_active = false;
  1323. ring->vsi = vsi;
  1324. ring->netdev = vsi->netdev;
  1325. ring->dev = &pf->pdev->dev;
  1326. ring->count = vsi->num_desc;
  1327. vsi->tx_rings[i] = ring;
  1328. }
  1329. /* Allocate rx_rings */
  1330. for (i = 0; i < vsi->alloc_rxq; i++) {
  1331. struct ice_ring *ring;
  1332. /* allocate with kzalloc(), free with kfree_rcu() */
  1333. ring = kzalloc(sizeof(*ring), GFP_KERNEL);
  1334. if (!ring)
  1335. goto err_out;
  1336. ring->q_index = i;
  1337. ring->reg_idx = vsi->rxq_map[i];
  1338. ring->ring_active = false;
  1339. ring->vsi = vsi;
  1340. ring->netdev = vsi->netdev;
  1341. ring->dev = &pf->pdev->dev;
  1342. ring->count = vsi->num_desc;
  1343. vsi->rx_rings[i] = ring;
  1344. }
  1345. return 0;
  1346. err_out:
  1347. ice_vsi_clear_rings(vsi);
  1348. return -ENOMEM;
  1349. }
  1350. /**
  1351. * ice_vsi_free_irq - Free the irq association with the OS
  1352. * @vsi: the VSI being configured
  1353. */
  1354. static void ice_vsi_free_irq(struct ice_vsi *vsi)
  1355. {
  1356. struct ice_pf *pf = vsi->back;
  1357. int base = vsi->base_vector;
  1358. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
  1359. int i;
  1360. if (!vsi->q_vectors || !vsi->irqs_ready)
  1361. return;
  1362. vsi->irqs_ready = false;
  1363. for (i = 0; i < vsi->num_q_vectors; i++) {
  1364. u16 vector = i + base;
  1365. int irq_num;
  1366. irq_num = pf->msix_entries[vector].vector;
  1367. /* free only the irqs that were actually requested */
  1368. if (!vsi->q_vectors[i] ||
  1369. !(vsi->q_vectors[i]->num_ring_tx ||
  1370. vsi->q_vectors[i]->num_ring_rx))
  1371. continue;
  1372. /* clear the affinity notifier in the IRQ descriptor */
  1373. irq_set_affinity_notifier(irq_num, NULL);
  1374. /* clear the affinity_mask in the IRQ descriptor */
  1375. irq_set_affinity_hint(irq_num, NULL);
  1376. synchronize_irq(irq_num);
  1377. devm_free_irq(&pf->pdev->dev, irq_num,
  1378. vsi->q_vectors[i]);
  1379. }
  1380. ice_vsi_release_msix(vsi);
  1381. }
  1382. }
  1383. /**
  1384. * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
  1385. * @vsi: the VSI being configured
  1386. */
  1387. static void ice_vsi_cfg_msix(struct ice_vsi *vsi)
  1388. {
  1389. struct ice_pf *pf = vsi->back;
  1390. u16 vector = vsi->base_vector;
  1391. struct ice_hw *hw = &pf->hw;
  1392. u32 txq = 0, rxq = 0;
  1393. int i, q, itr;
  1394. u8 itr_gran;
  1395. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  1396. struct ice_q_vector *q_vector = vsi->q_vectors[i];
  1397. itr_gran = hw->itr_gran_200;
  1398. if (q_vector->num_ring_rx) {
  1399. q_vector->rx.itr =
  1400. ITR_TO_REG(vsi->rx_rings[rxq]->rx_itr_setting,
  1401. itr_gran);
  1402. q_vector->rx.latency_range = ICE_LOW_LATENCY;
  1403. }
  1404. if (q_vector->num_ring_tx) {
  1405. q_vector->tx.itr =
  1406. ITR_TO_REG(vsi->tx_rings[txq]->tx_itr_setting,
  1407. itr_gran);
  1408. q_vector->tx.latency_range = ICE_LOW_LATENCY;
  1409. }
  1410. wr32(hw, GLINT_ITR(ICE_RX_ITR, vector), q_vector->rx.itr);
  1411. wr32(hw, GLINT_ITR(ICE_TX_ITR, vector), q_vector->tx.itr);
  1412. /* Both Transmit Queue Interrupt Cause Control register
  1413. * and Receive Queue Interrupt Cause control register
  1414. * expects MSIX_INDX field to be the vector index
  1415. * within the function space and not the absolute
  1416. * vector index across PF or across device.
  1417. * For SR-IOV VF VSIs queue vector index always starts
  1418. * with 1 since first vector index(0) is used for OICR
  1419. * in VF space. Since VMDq and other PF VSIs are withtin
  1420. * the PF function space, use the vector index thats
  1421. * tracked for this PF.
  1422. */
  1423. for (q = 0; q < q_vector->num_ring_tx; q++) {
  1424. u32 val;
  1425. itr = ICE_TX_ITR;
  1426. val = QINT_TQCTL_CAUSE_ENA_M |
  1427. (itr << QINT_TQCTL_ITR_INDX_S) |
  1428. (vector << QINT_TQCTL_MSIX_INDX_S);
  1429. wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), val);
  1430. txq++;
  1431. }
  1432. for (q = 0; q < q_vector->num_ring_rx; q++) {
  1433. u32 val;
  1434. itr = ICE_RX_ITR;
  1435. val = QINT_RQCTL_CAUSE_ENA_M |
  1436. (itr << QINT_RQCTL_ITR_INDX_S) |
  1437. (vector << QINT_RQCTL_MSIX_INDX_S);
  1438. wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), val);
  1439. rxq++;
  1440. }
  1441. }
  1442. ice_flush(hw);
  1443. }
  1444. /**
  1445. * ice_ena_misc_vector - enable the non-queue interrupts
  1446. * @pf: board private structure
  1447. */
  1448. static void ice_ena_misc_vector(struct ice_pf *pf)
  1449. {
  1450. struct ice_hw *hw = &pf->hw;
  1451. u32 val;
  1452. /* clear things first */
  1453. wr32(hw, PFINT_OICR_ENA, 0); /* disable all */
  1454. rd32(hw, PFINT_OICR); /* read to clear */
  1455. val = (PFINT_OICR_HLP_RDY_M |
  1456. PFINT_OICR_CPM_RDY_M |
  1457. PFINT_OICR_ECC_ERR_M |
  1458. PFINT_OICR_MAL_DETECT_M |
  1459. PFINT_OICR_GRST_M |
  1460. PFINT_OICR_PCI_EXCEPTION_M |
  1461. PFINT_OICR_GPIO_M |
  1462. PFINT_OICR_STORM_DETECT_M |
  1463. PFINT_OICR_HMC_ERR_M);
  1464. wr32(hw, PFINT_OICR_ENA, val);
  1465. /* SW_ITR_IDX = 0, but don't change INTENA */
  1466. wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
  1467. GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
  1468. }
  1469. /**
  1470. * ice_misc_intr - misc interrupt handler
  1471. * @irq: interrupt number
  1472. * @data: pointer to a q_vector
  1473. */
  1474. static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
  1475. {
  1476. struct ice_pf *pf = (struct ice_pf *)data;
  1477. struct ice_hw *hw = &pf->hw;
  1478. irqreturn_t ret = IRQ_NONE;
  1479. u32 oicr, ena_mask;
  1480. set_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
  1481. oicr = rd32(hw, PFINT_OICR);
  1482. ena_mask = rd32(hw, PFINT_OICR_ENA);
  1483. if (oicr & PFINT_OICR_GRST_M) {
  1484. u32 reset;
  1485. /* we have a reset warning */
  1486. ena_mask &= ~PFINT_OICR_GRST_M;
  1487. reset = (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
  1488. GLGEN_RSTAT_RESET_TYPE_S;
  1489. if (reset == ICE_RESET_CORER)
  1490. pf->corer_count++;
  1491. else if (reset == ICE_RESET_GLOBR)
  1492. pf->globr_count++;
  1493. else
  1494. pf->empr_count++;
  1495. /* If a reset cycle isn't already in progress, we set a bit in
  1496. * pf->state so that the service task can start a reset/rebuild.
  1497. * We also make note of which reset happened so that peer
  1498. * devices/drivers can be informed.
  1499. */
  1500. if (!test_bit(__ICE_RESET_RECOVERY_PENDING, pf->state)) {
  1501. if (reset == ICE_RESET_CORER)
  1502. set_bit(__ICE_CORER_RECV, pf->state);
  1503. else if (reset == ICE_RESET_GLOBR)
  1504. set_bit(__ICE_GLOBR_RECV, pf->state);
  1505. else
  1506. set_bit(__ICE_EMPR_RECV, pf->state);
  1507. set_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
  1508. }
  1509. }
  1510. if (oicr & PFINT_OICR_HMC_ERR_M) {
  1511. ena_mask &= ~PFINT_OICR_HMC_ERR_M;
  1512. dev_dbg(&pf->pdev->dev,
  1513. "HMC Error interrupt - info 0x%x, data 0x%x\n",
  1514. rd32(hw, PFHMC_ERRORINFO),
  1515. rd32(hw, PFHMC_ERRORDATA));
  1516. }
  1517. /* Report and mask off any remaining unexpected interrupts */
  1518. oicr &= ena_mask;
  1519. if (oicr) {
  1520. dev_dbg(&pf->pdev->dev, "unhandled interrupt oicr=0x%08x\n",
  1521. oicr);
  1522. /* If a critical error is pending there is no choice but to
  1523. * reset the device.
  1524. */
  1525. if (oicr & (PFINT_OICR_PE_CRITERR_M |
  1526. PFINT_OICR_PCI_EXCEPTION_M |
  1527. PFINT_OICR_ECC_ERR_M)) {
  1528. set_bit(__ICE_PFR_REQ, pf->state);
  1529. ice_service_task_schedule(pf);
  1530. }
  1531. ena_mask &= ~oicr;
  1532. }
  1533. ret = IRQ_HANDLED;
  1534. /* re-enable interrupt causes that are not handled during this pass */
  1535. wr32(hw, PFINT_OICR_ENA, ena_mask);
  1536. if (!test_bit(__ICE_DOWN, pf->state)) {
  1537. ice_service_task_schedule(pf);
  1538. ice_irq_dynamic_ena(hw, NULL, NULL);
  1539. }
  1540. return ret;
  1541. }
  1542. /**
  1543. * ice_vsi_map_rings_to_vectors - Map VSI rings to interrupt vectors
  1544. * @vsi: the VSI being configured
  1545. *
  1546. * This function maps descriptor rings to the queue-specific vectors allotted
  1547. * through the MSI-X enabling code. On a constrained vector budget, we map Tx
  1548. * and Rx rings to the vector as "efficiently" as possible.
  1549. */
  1550. static void ice_vsi_map_rings_to_vectors(struct ice_vsi *vsi)
  1551. {
  1552. int q_vectors = vsi->num_q_vectors;
  1553. int tx_rings_rem, rx_rings_rem;
  1554. int v_id;
  1555. /* initially assigning remaining rings count to VSIs num queue value */
  1556. tx_rings_rem = vsi->num_txq;
  1557. rx_rings_rem = vsi->num_rxq;
  1558. for (v_id = 0; v_id < q_vectors; v_id++) {
  1559. struct ice_q_vector *q_vector = vsi->q_vectors[v_id];
  1560. int tx_rings_per_v, rx_rings_per_v, q_id, q_base;
  1561. /* Tx rings mapping to vector */
  1562. tx_rings_per_v = DIV_ROUND_UP(tx_rings_rem, q_vectors - v_id);
  1563. q_vector->num_ring_tx = tx_rings_per_v;
  1564. q_vector->tx.ring = NULL;
  1565. q_base = vsi->num_txq - tx_rings_rem;
  1566. for (q_id = q_base; q_id < (q_base + tx_rings_per_v); q_id++) {
  1567. struct ice_ring *tx_ring = vsi->tx_rings[q_id];
  1568. tx_ring->q_vector = q_vector;
  1569. tx_ring->next = q_vector->tx.ring;
  1570. q_vector->tx.ring = tx_ring;
  1571. }
  1572. tx_rings_rem -= tx_rings_per_v;
  1573. /* Rx rings mapping to vector */
  1574. rx_rings_per_v = DIV_ROUND_UP(rx_rings_rem, q_vectors - v_id);
  1575. q_vector->num_ring_rx = rx_rings_per_v;
  1576. q_vector->rx.ring = NULL;
  1577. q_base = vsi->num_rxq - rx_rings_rem;
  1578. for (q_id = q_base; q_id < (q_base + rx_rings_per_v); q_id++) {
  1579. struct ice_ring *rx_ring = vsi->rx_rings[q_id];
  1580. rx_ring->q_vector = q_vector;
  1581. rx_ring->next = q_vector->rx.ring;
  1582. q_vector->rx.ring = rx_ring;
  1583. }
  1584. rx_rings_rem -= rx_rings_per_v;
  1585. }
  1586. }
  1587. /**
  1588. * ice_vsi_set_num_qs - Set num queues, descriptors and vectors for a VSI
  1589. * @vsi: the VSI being configured
  1590. *
  1591. * Return 0 on success and a negative value on error
  1592. */
  1593. static void ice_vsi_set_num_qs(struct ice_vsi *vsi)
  1594. {
  1595. struct ice_pf *pf = vsi->back;
  1596. switch (vsi->type) {
  1597. case ICE_VSI_PF:
  1598. vsi->alloc_txq = pf->num_lan_tx;
  1599. vsi->alloc_rxq = pf->num_lan_rx;
  1600. vsi->num_desc = ALIGN(ICE_DFLT_NUM_DESC, ICE_REQ_DESC_MULTIPLE);
  1601. vsi->num_q_vectors = max_t(int, pf->num_lan_rx, pf->num_lan_tx);
  1602. break;
  1603. default:
  1604. dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
  1605. vsi->type);
  1606. break;
  1607. }
  1608. }
  1609. /**
  1610. * ice_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
  1611. * @vsi: VSI pointer
  1612. * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
  1613. *
  1614. * On error: returns error code (negative)
  1615. * On success: returns 0
  1616. */
  1617. static int ice_vsi_alloc_arrays(struct ice_vsi *vsi, bool alloc_qvectors)
  1618. {
  1619. struct ice_pf *pf = vsi->back;
  1620. /* allocate memory for both Tx and Rx ring pointers */
  1621. vsi->tx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_txq,
  1622. sizeof(struct ice_ring *), GFP_KERNEL);
  1623. if (!vsi->tx_rings)
  1624. goto err_txrings;
  1625. vsi->rx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_rxq,
  1626. sizeof(struct ice_ring *), GFP_KERNEL);
  1627. if (!vsi->rx_rings)
  1628. goto err_rxrings;
  1629. if (alloc_qvectors) {
  1630. /* allocate memory for q_vector pointers */
  1631. vsi->q_vectors = devm_kcalloc(&pf->pdev->dev,
  1632. vsi->num_q_vectors,
  1633. sizeof(struct ice_q_vector *),
  1634. GFP_KERNEL);
  1635. if (!vsi->q_vectors)
  1636. goto err_vectors;
  1637. }
  1638. return 0;
  1639. err_vectors:
  1640. devm_kfree(&pf->pdev->dev, vsi->rx_rings);
  1641. err_rxrings:
  1642. devm_kfree(&pf->pdev->dev, vsi->tx_rings);
  1643. err_txrings:
  1644. return -ENOMEM;
  1645. }
  1646. /**
  1647. * ice_msix_clean_rings - MSIX mode Interrupt Handler
  1648. * @irq: interrupt number
  1649. * @data: pointer to a q_vector
  1650. */
  1651. static irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
  1652. {
  1653. struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
  1654. if (!q_vector->tx.ring && !q_vector->rx.ring)
  1655. return IRQ_HANDLED;
  1656. napi_schedule(&q_vector->napi);
  1657. return IRQ_HANDLED;
  1658. }
  1659. /**
  1660. * ice_vsi_alloc - Allocates the next available struct vsi in the PF
  1661. * @pf: board private structure
  1662. * @type: type of VSI
  1663. *
  1664. * returns a pointer to a VSI on success, NULL on failure.
  1665. */
  1666. static struct ice_vsi *ice_vsi_alloc(struct ice_pf *pf, enum ice_vsi_type type)
  1667. {
  1668. struct ice_vsi *vsi = NULL;
  1669. /* Need to protect the allocation of the VSIs at the PF level */
  1670. mutex_lock(&pf->sw_mutex);
  1671. /* If we have already allocated our maximum number of VSIs,
  1672. * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index
  1673. * is available to be populated
  1674. */
  1675. if (pf->next_vsi == ICE_NO_VSI) {
  1676. dev_dbg(&pf->pdev->dev, "out of VSI slots!\n");
  1677. goto unlock_pf;
  1678. }
  1679. vsi = devm_kzalloc(&pf->pdev->dev, sizeof(*vsi), GFP_KERNEL);
  1680. if (!vsi)
  1681. goto unlock_pf;
  1682. vsi->type = type;
  1683. vsi->back = pf;
  1684. set_bit(__ICE_DOWN, vsi->state);
  1685. vsi->idx = pf->next_vsi;
  1686. vsi->work_lmt = ICE_DFLT_IRQ_WORK;
  1687. ice_vsi_set_num_qs(vsi);
  1688. switch (vsi->type) {
  1689. case ICE_VSI_PF:
  1690. if (ice_vsi_alloc_arrays(vsi, true))
  1691. goto err_rings;
  1692. /* Setup default MSIX irq handler for VSI */
  1693. vsi->irq_handler = ice_msix_clean_rings;
  1694. break;
  1695. default:
  1696. dev_warn(&pf->pdev->dev, "Unknown VSI type %d\n", vsi->type);
  1697. goto unlock_pf;
  1698. }
  1699. /* fill VSI slot in the PF struct */
  1700. pf->vsi[pf->next_vsi] = vsi;
  1701. /* prepare pf->next_vsi for next use */
  1702. pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
  1703. pf->next_vsi);
  1704. goto unlock_pf;
  1705. err_rings:
  1706. devm_kfree(&pf->pdev->dev, vsi);
  1707. vsi = NULL;
  1708. unlock_pf:
  1709. mutex_unlock(&pf->sw_mutex);
  1710. return vsi;
  1711. }
  1712. /**
  1713. * ice_free_irq_msix_misc - Unroll misc vector setup
  1714. * @pf: board private structure
  1715. */
  1716. static void ice_free_irq_msix_misc(struct ice_pf *pf)
  1717. {
  1718. /* disable OICR interrupt */
  1719. wr32(&pf->hw, PFINT_OICR_ENA, 0);
  1720. ice_flush(&pf->hw);
  1721. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags) && pf->msix_entries) {
  1722. synchronize_irq(pf->msix_entries[pf->oicr_idx].vector);
  1723. devm_free_irq(&pf->pdev->dev,
  1724. pf->msix_entries[pf->oicr_idx].vector, pf);
  1725. }
  1726. ice_free_res(pf->irq_tracker, pf->oicr_idx, ICE_RES_MISC_VEC_ID);
  1727. }
  1728. /**
  1729. * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
  1730. * @pf: board private structure
  1731. *
  1732. * This sets up the handler for MSIX 0, which is used to manage the
  1733. * non-queue interrupts, e.g. AdminQ and errors. This is not used
  1734. * when in MSI or Legacy interrupt mode.
  1735. */
  1736. static int ice_req_irq_msix_misc(struct ice_pf *pf)
  1737. {
  1738. struct ice_hw *hw = &pf->hw;
  1739. int oicr_idx, err = 0;
  1740. u8 itr_gran;
  1741. u32 val;
  1742. if (!pf->int_name[0])
  1743. snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
  1744. dev_driver_string(&pf->pdev->dev),
  1745. dev_name(&pf->pdev->dev));
  1746. /* Do not request IRQ but do enable OICR interrupt since settings are
  1747. * lost during reset. Note that this function is called only during
  1748. * rebuild path and not while reset is in progress.
  1749. */
  1750. if (ice_is_reset_recovery_pending(pf->state))
  1751. goto skip_req_irq;
  1752. /* reserve one vector in irq_tracker for misc interrupts */
  1753. oicr_idx = ice_get_res(pf, pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
  1754. if (oicr_idx < 0)
  1755. return oicr_idx;
  1756. pf->oicr_idx = oicr_idx;
  1757. err = devm_request_irq(&pf->pdev->dev,
  1758. pf->msix_entries[pf->oicr_idx].vector,
  1759. ice_misc_intr, 0, pf->int_name, pf);
  1760. if (err) {
  1761. dev_err(&pf->pdev->dev,
  1762. "devm_request_irq for %s failed: %d\n",
  1763. pf->int_name, err);
  1764. ice_free_res(pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
  1765. return err;
  1766. }
  1767. skip_req_irq:
  1768. ice_ena_misc_vector(pf);
  1769. val = (pf->oicr_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
  1770. (ICE_RX_ITR & PFINT_OICR_CTL_ITR_INDX_M) |
  1771. PFINT_OICR_CTL_CAUSE_ENA_M;
  1772. wr32(hw, PFINT_OICR_CTL, val);
  1773. /* This enables Admin queue Interrupt causes */
  1774. val = (pf->oicr_idx & PFINT_FW_CTL_MSIX_INDX_M) |
  1775. (ICE_RX_ITR & PFINT_FW_CTL_ITR_INDX_M) |
  1776. PFINT_FW_CTL_CAUSE_ENA_M;
  1777. wr32(hw, PFINT_FW_CTL, val);
  1778. itr_gran = hw->itr_gran_200;
  1779. wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->oicr_idx),
  1780. ITR_TO_REG(ICE_ITR_8K, itr_gran));
  1781. ice_flush(hw);
  1782. ice_irq_dynamic_ena(hw, NULL, NULL);
  1783. return 0;
  1784. }
  1785. /**
  1786. * ice_vsi_get_qs_contig - Assign a contiguous chunk of queues to VSI
  1787. * @vsi: the VSI getting queues
  1788. *
  1789. * Return 0 on success and a negative value on error
  1790. */
  1791. static int ice_vsi_get_qs_contig(struct ice_vsi *vsi)
  1792. {
  1793. struct ice_pf *pf = vsi->back;
  1794. int offset, ret = 0;
  1795. mutex_lock(&pf->avail_q_mutex);
  1796. /* look for contiguous block of queues for tx */
  1797. offset = bitmap_find_next_zero_area(pf->avail_txqs, ICE_MAX_TXQS,
  1798. 0, vsi->alloc_txq, 0);
  1799. if (offset < ICE_MAX_TXQS) {
  1800. int i;
  1801. bitmap_set(pf->avail_txqs, offset, vsi->alloc_txq);
  1802. for (i = 0; i < vsi->alloc_txq; i++)
  1803. vsi->txq_map[i] = i + offset;
  1804. } else {
  1805. ret = -ENOMEM;
  1806. vsi->tx_mapping_mode = ICE_VSI_MAP_SCATTER;
  1807. }
  1808. /* look for contiguous block of queues for rx */
  1809. offset = bitmap_find_next_zero_area(pf->avail_rxqs, ICE_MAX_RXQS,
  1810. 0, vsi->alloc_rxq, 0);
  1811. if (offset < ICE_MAX_RXQS) {
  1812. int i;
  1813. bitmap_set(pf->avail_rxqs, offset, vsi->alloc_rxq);
  1814. for (i = 0; i < vsi->alloc_rxq; i++)
  1815. vsi->rxq_map[i] = i + offset;
  1816. } else {
  1817. ret = -ENOMEM;
  1818. vsi->rx_mapping_mode = ICE_VSI_MAP_SCATTER;
  1819. }
  1820. mutex_unlock(&pf->avail_q_mutex);
  1821. return ret;
  1822. }
  1823. /**
  1824. * ice_vsi_get_qs_scatter - Assign a scattered queues to VSI
  1825. * @vsi: the VSI getting queues
  1826. *
  1827. * Return 0 on success and a negative value on error
  1828. */
  1829. static int ice_vsi_get_qs_scatter(struct ice_vsi *vsi)
  1830. {
  1831. struct ice_pf *pf = vsi->back;
  1832. int i, index = 0;
  1833. mutex_lock(&pf->avail_q_mutex);
  1834. if (vsi->tx_mapping_mode == ICE_VSI_MAP_SCATTER) {
  1835. for (i = 0; i < vsi->alloc_txq; i++) {
  1836. index = find_next_zero_bit(pf->avail_txqs,
  1837. ICE_MAX_TXQS, index);
  1838. if (index < ICE_MAX_TXQS) {
  1839. set_bit(index, pf->avail_txqs);
  1840. vsi->txq_map[i] = index;
  1841. } else {
  1842. goto err_scatter_tx;
  1843. }
  1844. }
  1845. }
  1846. if (vsi->rx_mapping_mode == ICE_VSI_MAP_SCATTER) {
  1847. for (i = 0; i < vsi->alloc_rxq; i++) {
  1848. index = find_next_zero_bit(pf->avail_rxqs,
  1849. ICE_MAX_RXQS, index);
  1850. if (index < ICE_MAX_RXQS) {
  1851. set_bit(index, pf->avail_rxqs);
  1852. vsi->rxq_map[i] = index;
  1853. } else {
  1854. goto err_scatter_rx;
  1855. }
  1856. }
  1857. }
  1858. mutex_unlock(&pf->avail_q_mutex);
  1859. return 0;
  1860. err_scatter_rx:
  1861. /* unflag any queues we have grabbed (i is failed position) */
  1862. for (index = 0; index < i; index++) {
  1863. clear_bit(vsi->rxq_map[index], pf->avail_rxqs);
  1864. vsi->rxq_map[index] = 0;
  1865. }
  1866. i = vsi->alloc_txq;
  1867. err_scatter_tx:
  1868. /* i is either position of failed attempt or vsi->alloc_txq */
  1869. for (index = 0; index < i; index++) {
  1870. clear_bit(vsi->txq_map[index], pf->avail_txqs);
  1871. vsi->txq_map[index] = 0;
  1872. }
  1873. mutex_unlock(&pf->avail_q_mutex);
  1874. return -ENOMEM;
  1875. }
  1876. /**
  1877. * ice_vsi_get_qs - Assign queues from PF to VSI
  1878. * @vsi: the VSI to assign queues to
  1879. *
  1880. * Returns 0 on success and a negative value on error
  1881. */
  1882. static int ice_vsi_get_qs(struct ice_vsi *vsi)
  1883. {
  1884. int ret = 0;
  1885. vsi->tx_mapping_mode = ICE_VSI_MAP_CONTIG;
  1886. vsi->rx_mapping_mode = ICE_VSI_MAP_CONTIG;
  1887. /* NOTE: ice_vsi_get_qs_contig() will set the rx/tx mapping
  1888. * modes individually to scatter if assigning contiguous queues
  1889. * to rx or tx fails
  1890. */
  1891. ret = ice_vsi_get_qs_contig(vsi);
  1892. if (ret < 0) {
  1893. if (vsi->tx_mapping_mode == ICE_VSI_MAP_SCATTER)
  1894. vsi->alloc_txq = max_t(u16, vsi->alloc_txq,
  1895. ICE_MAX_SCATTER_TXQS);
  1896. if (vsi->rx_mapping_mode == ICE_VSI_MAP_SCATTER)
  1897. vsi->alloc_rxq = max_t(u16, vsi->alloc_rxq,
  1898. ICE_MAX_SCATTER_RXQS);
  1899. ret = ice_vsi_get_qs_scatter(vsi);
  1900. }
  1901. return ret;
  1902. }
  1903. /**
  1904. * ice_vsi_put_qs - Release queues from VSI to PF
  1905. * @vsi: the VSI thats going to release queues
  1906. */
  1907. static void ice_vsi_put_qs(struct ice_vsi *vsi)
  1908. {
  1909. struct ice_pf *pf = vsi->back;
  1910. int i;
  1911. mutex_lock(&pf->avail_q_mutex);
  1912. for (i = 0; i < vsi->alloc_txq; i++) {
  1913. clear_bit(vsi->txq_map[i], pf->avail_txqs);
  1914. vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
  1915. }
  1916. for (i = 0; i < vsi->alloc_rxq; i++) {
  1917. clear_bit(vsi->rxq_map[i], pf->avail_rxqs);
  1918. vsi->rxq_map[i] = ICE_INVAL_Q_INDEX;
  1919. }
  1920. mutex_unlock(&pf->avail_q_mutex);
  1921. }
  1922. /**
  1923. * ice_free_q_vector - Free memory allocated for a specific interrupt vector
  1924. * @vsi: VSI having the memory freed
  1925. * @v_idx: index of the vector to be freed
  1926. */
  1927. static void ice_free_q_vector(struct ice_vsi *vsi, int v_idx)
  1928. {
  1929. struct ice_q_vector *q_vector;
  1930. struct ice_ring *ring;
  1931. if (!vsi->q_vectors[v_idx]) {
  1932. dev_dbg(&vsi->back->pdev->dev, "Queue vector at index %d not found\n",
  1933. v_idx);
  1934. return;
  1935. }
  1936. q_vector = vsi->q_vectors[v_idx];
  1937. ice_for_each_ring(ring, q_vector->tx)
  1938. ring->q_vector = NULL;
  1939. ice_for_each_ring(ring, q_vector->rx)
  1940. ring->q_vector = NULL;
  1941. /* only VSI with an associated netdev is set up with NAPI */
  1942. if (vsi->netdev)
  1943. netif_napi_del(&q_vector->napi);
  1944. devm_kfree(&vsi->back->pdev->dev, q_vector);
  1945. vsi->q_vectors[v_idx] = NULL;
  1946. }
  1947. /**
  1948. * ice_vsi_free_q_vectors - Free memory allocated for interrupt vectors
  1949. * @vsi: the VSI having memory freed
  1950. */
  1951. static void ice_vsi_free_q_vectors(struct ice_vsi *vsi)
  1952. {
  1953. int v_idx;
  1954. for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
  1955. ice_free_q_vector(vsi, v_idx);
  1956. }
  1957. /**
  1958. * ice_cfg_netdev - Setup the netdev flags
  1959. * @vsi: the VSI being configured
  1960. *
  1961. * Returns 0 on success, negative value on failure
  1962. */
  1963. static int ice_cfg_netdev(struct ice_vsi *vsi)
  1964. {
  1965. netdev_features_t csumo_features;
  1966. netdev_features_t vlano_features;
  1967. netdev_features_t dflt_features;
  1968. netdev_features_t tso_features;
  1969. struct ice_netdev_priv *np;
  1970. struct net_device *netdev;
  1971. u8 mac_addr[ETH_ALEN];
  1972. netdev = alloc_etherdev_mqs(sizeof(struct ice_netdev_priv),
  1973. vsi->alloc_txq, vsi->alloc_rxq);
  1974. if (!netdev)
  1975. return -ENOMEM;
  1976. vsi->netdev = netdev;
  1977. np = netdev_priv(netdev);
  1978. np->vsi = vsi;
  1979. dflt_features = NETIF_F_SG |
  1980. NETIF_F_HIGHDMA |
  1981. NETIF_F_RXHASH;
  1982. csumo_features = NETIF_F_RXCSUM |
  1983. NETIF_F_IP_CSUM |
  1984. NETIF_F_IPV6_CSUM;
  1985. vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER |
  1986. NETIF_F_HW_VLAN_CTAG_TX |
  1987. NETIF_F_HW_VLAN_CTAG_RX;
  1988. tso_features = NETIF_F_TSO;
  1989. /* set features that user can change */
  1990. netdev->hw_features = dflt_features | csumo_features |
  1991. vlano_features | tso_features;
  1992. /* enable features */
  1993. netdev->features |= netdev->hw_features;
  1994. /* encap and VLAN devices inherit default, csumo and tso features */
  1995. netdev->hw_enc_features |= dflt_features | csumo_features |
  1996. tso_features;
  1997. netdev->vlan_features |= dflt_features | csumo_features |
  1998. tso_features;
  1999. if (vsi->type == ICE_VSI_PF) {
  2000. SET_NETDEV_DEV(netdev, &vsi->back->pdev->dev);
  2001. ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
  2002. ether_addr_copy(netdev->dev_addr, mac_addr);
  2003. ether_addr_copy(netdev->perm_addr, mac_addr);
  2004. }
  2005. netdev->priv_flags |= IFF_UNICAST_FLT;
  2006. /* assign netdev_ops */
  2007. netdev->netdev_ops = &ice_netdev_ops;
  2008. /* setup watchdog timeout value to be 5 second */
  2009. netdev->watchdog_timeo = 5 * HZ;
  2010. ice_set_ethtool_ops(netdev);
  2011. netdev->min_mtu = ETH_MIN_MTU;
  2012. netdev->max_mtu = ICE_MAX_MTU;
  2013. return 0;
  2014. }
  2015. /**
  2016. * ice_vsi_free_arrays - clean up vsi resources
  2017. * @vsi: pointer to VSI being cleared
  2018. * @free_qvectors: bool to specify if q_vectors should be deallocated
  2019. */
  2020. static void ice_vsi_free_arrays(struct ice_vsi *vsi, bool free_qvectors)
  2021. {
  2022. struct ice_pf *pf = vsi->back;
  2023. /* free the ring and vector containers */
  2024. if (free_qvectors && vsi->q_vectors) {
  2025. devm_kfree(&pf->pdev->dev, vsi->q_vectors);
  2026. vsi->q_vectors = NULL;
  2027. }
  2028. if (vsi->tx_rings) {
  2029. devm_kfree(&pf->pdev->dev, vsi->tx_rings);
  2030. vsi->tx_rings = NULL;
  2031. }
  2032. if (vsi->rx_rings) {
  2033. devm_kfree(&pf->pdev->dev, vsi->rx_rings);
  2034. vsi->rx_rings = NULL;
  2035. }
  2036. }
  2037. /**
  2038. * ice_vsi_clear - clean up and deallocate the provided vsi
  2039. * @vsi: pointer to VSI being cleared
  2040. *
  2041. * This deallocates the vsi's queue resources, removes it from the PF's
  2042. * VSI array if necessary, and deallocates the VSI
  2043. *
  2044. * Returns 0 on success, negative on failure
  2045. */
  2046. static int ice_vsi_clear(struct ice_vsi *vsi)
  2047. {
  2048. struct ice_pf *pf = NULL;
  2049. if (!vsi)
  2050. return 0;
  2051. if (!vsi->back)
  2052. return -EINVAL;
  2053. pf = vsi->back;
  2054. if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
  2055. dev_dbg(&pf->pdev->dev, "vsi does not exist at pf->vsi[%d]\n",
  2056. vsi->idx);
  2057. return -EINVAL;
  2058. }
  2059. mutex_lock(&pf->sw_mutex);
  2060. /* updates the PF for this cleared vsi */
  2061. pf->vsi[vsi->idx] = NULL;
  2062. if (vsi->idx < pf->next_vsi)
  2063. pf->next_vsi = vsi->idx;
  2064. ice_vsi_free_arrays(vsi, true);
  2065. mutex_unlock(&pf->sw_mutex);
  2066. devm_kfree(&pf->pdev->dev, vsi);
  2067. return 0;
  2068. }
  2069. /**
  2070. * ice_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
  2071. * @vsi: the VSI being configured
  2072. * @v_idx: index of the vector in the vsi struct
  2073. *
  2074. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  2075. */
  2076. static int ice_vsi_alloc_q_vector(struct ice_vsi *vsi, int v_idx)
  2077. {
  2078. struct ice_pf *pf = vsi->back;
  2079. struct ice_q_vector *q_vector;
  2080. /* allocate q_vector */
  2081. q_vector = devm_kzalloc(&pf->pdev->dev, sizeof(*q_vector), GFP_KERNEL);
  2082. if (!q_vector)
  2083. return -ENOMEM;
  2084. q_vector->vsi = vsi;
  2085. q_vector->v_idx = v_idx;
  2086. /* only set affinity_mask if the CPU is online */
  2087. if (cpu_online(v_idx))
  2088. cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
  2089. if (vsi->netdev)
  2090. netif_napi_add(vsi->netdev, &q_vector->napi, ice_napi_poll,
  2091. NAPI_POLL_WEIGHT);
  2092. /* tie q_vector and vsi together */
  2093. vsi->q_vectors[v_idx] = q_vector;
  2094. return 0;
  2095. }
  2096. /**
  2097. * ice_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
  2098. * @vsi: the VSI being configured
  2099. *
  2100. * We allocate one q_vector per queue interrupt. If allocation fails we
  2101. * return -ENOMEM.
  2102. */
  2103. static int ice_vsi_alloc_q_vectors(struct ice_vsi *vsi)
  2104. {
  2105. struct ice_pf *pf = vsi->back;
  2106. int v_idx = 0, num_q_vectors;
  2107. int err;
  2108. if (vsi->q_vectors[0]) {
  2109. dev_dbg(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
  2110. vsi->vsi_num);
  2111. return -EEXIST;
  2112. }
  2113. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
  2114. num_q_vectors = vsi->num_q_vectors;
  2115. } else {
  2116. err = -EINVAL;
  2117. goto err_out;
  2118. }
  2119. for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
  2120. err = ice_vsi_alloc_q_vector(vsi, v_idx);
  2121. if (err)
  2122. goto err_out;
  2123. }
  2124. return 0;
  2125. err_out:
  2126. while (v_idx--)
  2127. ice_free_q_vector(vsi, v_idx);
  2128. dev_err(&pf->pdev->dev,
  2129. "Failed to allocate %d q_vector for VSI %d, ret=%d\n",
  2130. vsi->num_q_vectors, vsi->vsi_num, err);
  2131. vsi->num_q_vectors = 0;
  2132. return err;
  2133. }
  2134. /**
  2135. * ice_vsi_setup_vector_base - Set up the base vector for the given VSI
  2136. * @vsi: ptr to the VSI
  2137. *
  2138. * This should only be called after ice_vsi_alloc() which allocates the
  2139. * corresponding SW VSI structure and initializes num_queue_pairs for the
  2140. * newly allocated VSI.
  2141. *
  2142. * Returns 0 on success or negative on failure
  2143. */
  2144. static int ice_vsi_setup_vector_base(struct ice_vsi *vsi)
  2145. {
  2146. struct ice_pf *pf = vsi->back;
  2147. int num_q_vectors = 0;
  2148. if (vsi->base_vector) {
  2149. dev_dbg(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
  2150. vsi->vsi_num, vsi->base_vector);
  2151. return -EEXIST;
  2152. }
  2153. if (!test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
  2154. return -ENOENT;
  2155. switch (vsi->type) {
  2156. case ICE_VSI_PF:
  2157. num_q_vectors = vsi->num_q_vectors;
  2158. break;
  2159. default:
  2160. dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
  2161. vsi->type);
  2162. break;
  2163. }
  2164. if (num_q_vectors)
  2165. vsi->base_vector = ice_get_res(pf, pf->irq_tracker,
  2166. num_q_vectors, vsi->idx);
  2167. if (vsi->base_vector < 0) {
  2168. dev_err(&pf->pdev->dev,
  2169. "Failed to get tracking for %d vectors for VSI %d, err=%d\n",
  2170. num_q_vectors, vsi->vsi_num, vsi->base_vector);
  2171. return -ENOENT;
  2172. }
  2173. return 0;
  2174. }
  2175. /**
  2176. * ice_fill_rss_lut - Fill the RSS lookup table with default values
  2177. * @lut: Lookup table
  2178. * @rss_table_size: Lookup table size
  2179. * @rss_size: Range of queue number for hashing
  2180. */
  2181. void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
  2182. {
  2183. u16 i;
  2184. for (i = 0; i < rss_table_size; i++)
  2185. lut[i] = i % rss_size;
  2186. }
  2187. /**
  2188. * ice_vsi_cfg_rss - Configure RSS params for a VSI
  2189. * @vsi: VSI to be configured
  2190. */
  2191. static int ice_vsi_cfg_rss(struct ice_vsi *vsi)
  2192. {
  2193. u8 seed[ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE];
  2194. struct ice_aqc_get_set_rss_keys *key;
  2195. struct ice_pf *pf = vsi->back;
  2196. enum ice_status status;
  2197. int err = 0;
  2198. u8 *lut;
  2199. vsi->rss_size = min_t(int, vsi->rss_size, vsi->num_rxq);
  2200. lut = devm_kzalloc(&pf->pdev->dev, vsi->rss_table_size, GFP_KERNEL);
  2201. if (!lut)
  2202. return -ENOMEM;
  2203. if (vsi->rss_lut_user)
  2204. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  2205. else
  2206. ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
  2207. status = ice_aq_set_rss_lut(&pf->hw, vsi->vsi_num, vsi->rss_lut_type,
  2208. lut, vsi->rss_table_size);
  2209. if (status) {
  2210. dev_err(&vsi->back->pdev->dev,
  2211. "set_rss_lut failed, error %d\n", status);
  2212. err = -EIO;
  2213. goto ice_vsi_cfg_rss_exit;
  2214. }
  2215. key = devm_kzalloc(&vsi->back->pdev->dev, sizeof(*key), GFP_KERNEL);
  2216. if (!key) {
  2217. err = -ENOMEM;
  2218. goto ice_vsi_cfg_rss_exit;
  2219. }
  2220. if (vsi->rss_hkey_user)
  2221. memcpy(seed, vsi->rss_hkey_user,
  2222. ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
  2223. else
  2224. netdev_rss_key_fill((void *)seed,
  2225. ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
  2226. memcpy(&key->standard_rss_key, seed,
  2227. ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
  2228. status = ice_aq_set_rss_key(&pf->hw, vsi->vsi_num, key);
  2229. if (status) {
  2230. dev_err(&vsi->back->pdev->dev, "set_rss_key failed, error %d\n",
  2231. status);
  2232. err = -EIO;
  2233. }
  2234. devm_kfree(&pf->pdev->dev, key);
  2235. ice_vsi_cfg_rss_exit:
  2236. devm_kfree(&pf->pdev->dev, lut);
  2237. return err;
  2238. }
  2239. /**
  2240. * ice_vsi_reinit_setup - return resource and reallocate resource for a VSI
  2241. * @vsi: pointer to the ice_vsi
  2242. *
  2243. * This reallocates the VSIs queue resources
  2244. *
  2245. * Returns 0 on success and negative value on failure
  2246. */
  2247. static int ice_vsi_reinit_setup(struct ice_vsi *vsi)
  2248. {
  2249. u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
  2250. int ret, i;
  2251. if (!vsi)
  2252. return -EINVAL;
  2253. ice_vsi_free_q_vectors(vsi);
  2254. ice_free_res(vsi->back->irq_tracker, vsi->base_vector, vsi->idx);
  2255. vsi->base_vector = 0;
  2256. ice_vsi_clear_rings(vsi);
  2257. ice_vsi_free_arrays(vsi, false);
  2258. ice_vsi_set_num_qs(vsi);
  2259. /* Initialize VSI struct elements and create VSI in FW */
  2260. ret = ice_vsi_add(vsi);
  2261. if (ret < 0)
  2262. goto err_vsi;
  2263. ret = ice_vsi_alloc_arrays(vsi, false);
  2264. if (ret < 0)
  2265. goto err_vsi;
  2266. switch (vsi->type) {
  2267. case ICE_VSI_PF:
  2268. if (!vsi->netdev) {
  2269. ret = ice_cfg_netdev(vsi);
  2270. if (ret)
  2271. goto err_rings;
  2272. ret = register_netdev(vsi->netdev);
  2273. if (ret)
  2274. goto err_rings;
  2275. netif_carrier_off(vsi->netdev);
  2276. netif_tx_stop_all_queues(vsi->netdev);
  2277. }
  2278. ret = ice_vsi_alloc_q_vectors(vsi);
  2279. if (ret)
  2280. goto err_rings;
  2281. ret = ice_vsi_setup_vector_base(vsi);
  2282. if (ret)
  2283. goto err_vectors;
  2284. ret = ice_vsi_alloc_rings(vsi);
  2285. if (ret)
  2286. goto err_vectors;
  2287. ice_vsi_map_rings_to_vectors(vsi);
  2288. break;
  2289. default:
  2290. break;
  2291. }
  2292. ice_vsi_set_tc_cfg(vsi);
  2293. /* configure VSI nodes based on number of queues and TC's */
  2294. for (i = 0; i < vsi->tc_cfg.numtc; i++)
  2295. max_txqs[i] = vsi->num_txq;
  2296. ret = ice_cfg_vsi_lan(vsi->port_info, vsi->vsi_num,
  2297. vsi->tc_cfg.ena_tc, max_txqs);
  2298. if (ret) {
  2299. dev_info(&vsi->back->pdev->dev,
  2300. "Failed VSI lan queue config\n");
  2301. goto err_vectors;
  2302. }
  2303. return 0;
  2304. err_vectors:
  2305. ice_vsi_free_q_vectors(vsi);
  2306. err_rings:
  2307. if (vsi->netdev) {
  2308. vsi->current_netdev_flags = 0;
  2309. unregister_netdev(vsi->netdev);
  2310. free_netdev(vsi->netdev);
  2311. vsi->netdev = NULL;
  2312. }
  2313. err_vsi:
  2314. ice_vsi_clear(vsi);
  2315. set_bit(__ICE_RESET_FAILED, vsi->back->state);
  2316. return ret;
  2317. }
  2318. /**
  2319. * ice_vsi_setup - Set up a VSI by a given type
  2320. * @pf: board private structure
  2321. * @type: VSI type
  2322. * @pi: pointer to the port_info instance
  2323. *
  2324. * This allocates the sw VSI structure and its queue resources.
  2325. *
  2326. * Returns pointer to the successfully allocated and configure VSI sw struct on
  2327. * success, otherwise returns NULL on failure.
  2328. */
  2329. static struct ice_vsi *
  2330. ice_vsi_setup(struct ice_pf *pf, enum ice_vsi_type type,
  2331. struct ice_port_info *pi)
  2332. {
  2333. u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
  2334. struct device *dev = &pf->pdev->dev;
  2335. struct ice_vsi_ctx ctxt = { 0 };
  2336. struct ice_vsi *vsi;
  2337. int ret, i;
  2338. vsi = ice_vsi_alloc(pf, type);
  2339. if (!vsi) {
  2340. dev_err(dev, "could not allocate VSI\n");
  2341. return NULL;
  2342. }
  2343. vsi->port_info = pi;
  2344. vsi->vsw = pf->first_sw;
  2345. if (ice_vsi_get_qs(vsi)) {
  2346. dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
  2347. vsi->idx);
  2348. goto err_get_qs;
  2349. }
  2350. /* set RSS capabilities */
  2351. ice_vsi_set_rss_params(vsi);
  2352. /* create the VSI */
  2353. ret = ice_vsi_add(vsi);
  2354. if (ret)
  2355. goto err_vsi;
  2356. ctxt.vsi_num = vsi->vsi_num;
  2357. switch (vsi->type) {
  2358. case ICE_VSI_PF:
  2359. ret = ice_cfg_netdev(vsi);
  2360. if (ret)
  2361. goto err_cfg_netdev;
  2362. ret = register_netdev(vsi->netdev);
  2363. if (ret)
  2364. goto err_register_netdev;
  2365. netif_carrier_off(vsi->netdev);
  2366. /* make sure transmit queues start off as stopped */
  2367. netif_tx_stop_all_queues(vsi->netdev);
  2368. ret = ice_vsi_alloc_q_vectors(vsi);
  2369. if (ret)
  2370. goto err_msix;
  2371. ret = ice_vsi_setup_vector_base(vsi);
  2372. if (ret)
  2373. goto err_rings;
  2374. ret = ice_vsi_alloc_rings(vsi);
  2375. if (ret)
  2376. goto err_rings;
  2377. ice_vsi_map_rings_to_vectors(vsi);
  2378. /* Do not exit if configuring RSS had an issue, at least
  2379. * receive traffic on first queue. Hence no need to capture
  2380. * return value
  2381. */
  2382. if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
  2383. ice_vsi_cfg_rss(vsi);
  2384. break;
  2385. default:
  2386. /* if vsi type is not recognized, clean up the resources and
  2387. * exit
  2388. */
  2389. goto err_rings;
  2390. }
  2391. ice_vsi_set_tc_cfg(vsi);
  2392. /* configure VSI nodes based on number of queues and TC's */
  2393. for (i = 0; i < vsi->tc_cfg.numtc; i++)
  2394. max_txqs[i] = vsi->num_txq;
  2395. ret = ice_cfg_vsi_lan(vsi->port_info, vsi->vsi_num,
  2396. vsi->tc_cfg.ena_tc, max_txqs);
  2397. if (ret) {
  2398. dev_info(&pf->pdev->dev, "Failed VSI lan queue config\n");
  2399. goto err_rings;
  2400. }
  2401. return vsi;
  2402. err_rings:
  2403. ice_vsi_free_q_vectors(vsi);
  2404. err_msix:
  2405. if (vsi->netdev && vsi->netdev->reg_state == NETREG_REGISTERED)
  2406. unregister_netdev(vsi->netdev);
  2407. err_register_netdev:
  2408. if (vsi->netdev) {
  2409. free_netdev(vsi->netdev);
  2410. vsi->netdev = NULL;
  2411. }
  2412. err_cfg_netdev:
  2413. ret = ice_aq_free_vsi(&pf->hw, &ctxt, false, NULL);
  2414. if (ret)
  2415. dev_err(&vsi->back->pdev->dev,
  2416. "Free VSI AQ call failed, err %d\n", ret);
  2417. err_vsi:
  2418. ice_vsi_put_qs(vsi);
  2419. err_get_qs:
  2420. pf->q_left_tx += vsi->alloc_txq;
  2421. pf->q_left_rx += vsi->alloc_rxq;
  2422. ice_vsi_clear(vsi);
  2423. return NULL;
  2424. }
  2425. /**
  2426. * ice_vsi_add_vlan - Add vsi membership for given vlan
  2427. * @vsi: the vsi being configured
  2428. * @vid: vlan id to be added
  2429. */
  2430. static int ice_vsi_add_vlan(struct ice_vsi *vsi, u16 vid)
  2431. {
  2432. struct ice_fltr_list_entry *tmp;
  2433. struct ice_pf *pf = vsi->back;
  2434. LIST_HEAD(tmp_add_list);
  2435. enum ice_status status;
  2436. int err = 0;
  2437. tmp = devm_kzalloc(&pf->pdev->dev, sizeof(*tmp), GFP_KERNEL);
  2438. if (!tmp)
  2439. return -ENOMEM;
  2440. tmp->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
  2441. tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
  2442. tmp->fltr_info.flag = ICE_FLTR_TX;
  2443. tmp->fltr_info.src = vsi->vsi_num;
  2444. tmp->fltr_info.fwd_id.vsi_id = vsi->vsi_num;
  2445. tmp->fltr_info.l_data.vlan.vlan_id = vid;
  2446. INIT_LIST_HEAD(&tmp->list_entry);
  2447. list_add(&tmp->list_entry, &tmp_add_list);
  2448. status = ice_add_vlan(&pf->hw, &tmp_add_list);
  2449. if (status) {
  2450. err = -ENODEV;
  2451. dev_err(&pf->pdev->dev, "Failure Adding VLAN %d on VSI %i\n",
  2452. vid, vsi->vsi_num);
  2453. }
  2454. ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
  2455. return err;
  2456. }
  2457. /**
  2458. * ice_vlan_rx_add_vid - Add a vlan id filter to HW offload
  2459. * @netdev: network interface to be adjusted
  2460. * @proto: unused protocol
  2461. * @vid: vlan id to be added
  2462. *
  2463. * net_device_ops implementation for adding vlan ids
  2464. */
  2465. static int ice_vlan_rx_add_vid(struct net_device *netdev,
  2466. __always_unused __be16 proto, u16 vid)
  2467. {
  2468. struct ice_netdev_priv *np = netdev_priv(netdev);
  2469. struct ice_vsi *vsi = np->vsi;
  2470. int ret = 0;
  2471. if (vid >= VLAN_N_VID) {
  2472. netdev_err(netdev, "VLAN id requested %d is out of range %d\n",
  2473. vid, VLAN_N_VID);
  2474. return -EINVAL;
  2475. }
  2476. if (vsi->info.pvid)
  2477. return -EINVAL;
  2478. /* Add all VLAN ids including 0 to the switch filter. VLAN id 0 is
  2479. * needed to continue allowing all untagged packets since VLAN prune
  2480. * list is applied to all packets by the switch
  2481. */
  2482. ret = ice_vsi_add_vlan(vsi, vid);
  2483. if (!ret)
  2484. set_bit(vid, vsi->active_vlans);
  2485. return ret;
  2486. }
  2487. /**
  2488. * ice_vsi_kill_vlan - Remove VSI membership for a given VLAN
  2489. * @vsi: the VSI being configured
  2490. * @vid: VLAN id to be removed
  2491. */
  2492. static void ice_vsi_kill_vlan(struct ice_vsi *vsi, u16 vid)
  2493. {
  2494. struct ice_fltr_list_entry *list;
  2495. struct ice_pf *pf = vsi->back;
  2496. LIST_HEAD(tmp_add_list);
  2497. list = devm_kzalloc(&pf->pdev->dev, sizeof(*list), GFP_KERNEL);
  2498. if (!list)
  2499. return;
  2500. list->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
  2501. list->fltr_info.fwd_id.vsi_id = vsi->vsi_num;
  2502. list->fltr_info.fltr_act = ICE_FWD_TO_VSI;
  2503. list->fltr_info.l_data.vlan.vlan_id = vid;
  2504. list->fltr_info.flag = ICE_FLTR_TX;
  2505. list->fltr_info.src = vsi->vsi_num;
  2506. INIT_LIST_HEAD(&list->list_entry);
  2507. list_add(&list->list_entry, &tmp_add_list);
  2508. if (ice_remove_vlan(&pf->hw, &tmp_add_list))
  2509. dev_err(&pf->pdev->dev, "Error removing VLAN %d on vsi %i\n",
  2510. vid, vsi->vsi_num);
  2511. ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
  2512. }
  2513. /**
  2514. * ice_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
  2515. * @netdev: network interface to be adjusted
  2516. * @proto: unused protocol
  2517. * @vid: vlan id to be removed
  2518. *
  2519. * net_device_ops implementation for removing vlan ids
  2520. */
  2521. static int ice_vlan_rx_kill_vid(struct net_device *netdev,
  2522. __always_unused __be16 proto, u16 vid)
  2523. {
  2524. struct ice_netdev_priv *np = netdev_priv(netdev);
  2525. struct ice_vsi *vsi = np->vsi;
  2526. if (vsi->info.pvid)
  2527. return -EINVAL;
  2528. /* return code is ignored as there is nothing a user
  2529. * can do about failure to remove and a log message was
  2530. * already printed from the other function
  2531. */
  2532. ice_vsi_kill_vlan(vsi, vid);
  2533. clear_bit(vid, vsi->active_vlans);
  2534. return 0;
  2535. }
  2536. /**
  2537. * ice_setup_pf_sw - Setup the HW switch on startup or after reset
  2538. * @pf: board private structure
  2539. *
  2540. * Returns 0 on success, negative value on failure
  2541. */
  2542. static int ice_setup_pf_sw(struct ice_pf *pf)
  2543. {
  2544. LIST_HEAD(tmp_add_list);
  2545. u8 broadcast[ETH_ALEN];
  2546. struct ice_vsi *vsi;
  2547. int status = 0;
  2548. if (!ice_is_reset_recovery_pending(pf->state)) {
  2549. vsi = ice_vsi_setup(pf, ICE_VSI_PF, pf->hw.port_info);
  2550. if (!vsi) {
  2551. status = -ENOMEM;
  2552. goto error_exit;
  2553. }
  2554. } else {
  2555. vsi = pf->vsi[0];
  2556. status = ice_vsi_reinit_setup(vsi);
  2557. if (status < 0)
  2558. return -EIO;
  2559. }
  2560. /* tmp_add_list contains a list of MAC addresses for which MAC
  2561. * filters need to be programmed. Add the VSI's unicast MAC to
  2562. * this list
  2563. */
  2564. status = ice_add_mac_to_list(vsi, &tmp_add_list,
  2565. vsi->port_info->mac.perm_addr);
  2566. if (status)
  2567. goto error_exit;
  2568. /* VSI needs to receive broadcast traffic, so add the broadcast
  2569. * MAC address to the list.
  2570. */
  2571. eth_broadcast_addr(broadcast);
  2572. status = ice_add_mac_to_list(vsi, &tmp_add_list, broadcast);
  2573. if (status)
  2574. goto error_exit;
  2575. /* program MAC filters for entries in tmp_add_list */
  2576. status = ice_add_mac(&pf->hw, &tmp_add_list);
  2577. if (status) {
  2578. dev_err(&pf->pdev->dev, "Could not add MAC filters\n");
  2579. status = -ENOMEM;
  2580. goto error_exit;
  2581. }
  2582. ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
  2583. return status;
  2584. error_exit:
  2585. ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
  2586. if (vsi) {
  2587. ice_vsi_free_q_vectors(vsi);
  2588. if (vsi->netdev && vsi->netdev->reg_state == NETREG_REGISTERED)
  2589. unregister_netdev(vsi->netdev);
  2590. if (vsi->netdev) {
  2591. free_netdev(vsi->netdev);
  2592. vsi->netdev = NULL;
  2593. }
  2594. ice_vsi_delete(vsi);
  2595. ice_vsi_put_qs(vsi);
  2596. pf->q_left_tx += vsi->alloc_txq;
  2597. pf->q_left_rx += vsi->alloc_rxq;
  2598. ice_vsi_clear(vsi);
  2599. }
  2600. return status;
  2601. }
  2602. /**
  2603. * ice_determine_q_usage - Calculate queue distribution
  2604. * @pf: board private structure
  2605. *
  2606. * Return -ENOMEM if we don't get enough queues for all ports
  2607. */
  2608. static void ice_determine_q_usage(struct ice_pf *pf)
  2609. {
  2610. u16 q_left_tx, q_left_rx;
  2611. q_left_tx = pf->hw.func_caps.common_cap.num_txq;
  2612. q_left_rx = pf->hw.func_caps.common_cap.num_rxq;
  2613. pf->num_lan_tx = min_t(int, q_left_tx, num_online_cpus());
  2614. /* only 1 rx queue unless RSS is enabled */
  2615. if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags))
  2616. pf->num_lan_rx = 1;
  2617. else
  2618. pf->num_lan_rx = min_t(int, q_left_rx, num_online_cpus());
  2619. pf->q_left_tx = q_left_tx - pf->num_lan_tx;
  2620. pf->q_left_rx = q_left_rx - pf->num_lan_rx;
  2621. }
  2622. /**
  2623. * ice_deinit_pf - Unrolls initialziations done by ice_init_pf
  2624. * @pf: board private structure to initialize
  2625. */
  2626. static void ice_deinit_pf(struct ice_pf *pf)
  2627. {
  2628. if (pf->serv_tmr.function)
  2629. del_timer_sync(&pf->serv_tmr);
  2630. if (pf->serv_task.func)
  2631. cancel_work_sync(&pf->serv_task);
  2632. mutex_destroy(&pf->sw_mutex);
  2633. mutex_destroy(&pf->avail_q_mutex);
  2634. }
  2635. /**
  2636. * ice_init_pf - Initialize general software structures (struct ice_pf)
  2637. * @pf: board private structure to initialize
  2638. */
  2639. static void ice_init_pf(struct ice_pf *pf)
  2640. {
  2641. bitmap_zero(pf->flags, ICE_PF_FLAGS_NBITS);
  2642. set_bit(ICE_FLAG_MSIX_ENA, pf->flags);
  2643. mutex_init(&pf->sw_mutex);
  2644. mutex_init(&pf->avail_q_mutex);
  2645. /* Clear avail_[t|r]x_qs bitmaps (set all to avail) */
  2646. mutex_lock(&pf->avail_q_mutex);
  2647. bitmap_zero(pf->avail_txqs, ICE_MAX_TXQS);
  2648. bitmap_zero(pf->avail_rxqs, ICE_MAX_RXQS);
  2649. mutex_unlock(&pf->avail_q_mutex);
  2650. if (pf->hw.func_caps.common_cap.rss_table_size)
  2651. set_bit(ICE_FLAG_RSS_ENA, pf->flags);
  2652. /* setup service timer and periodic service task */
  2653. timer_setup(&pf->serv_tmr, ice_service_timer, 0);
  2654. pf->serv_tmr_period = HZ;
  2655. INIT_WORK(&pf->serv_task, ice_service_task);
  2656. clear_bit(__ICE_SERVICE_SCHED, pf->state);
  2657. }
  2658. /**
  2659. * ice_ena_msix_range - Request a range of MSIX vectors from the OS
  2660. * @pf: board private structure
  2661. *
  2662. * compute the number of MSIX vectors required (v_budget) and request from
  2663. * the OS. Return the number of vectors reserved or negative on failure
  2664. */
  2665. static int ice_ena_msix_range(struct ice_pf *pf)
  2666. {
  2667. int v_left, v_actual, v_budget = 0;
  2668. int needed, err, i;
  2669. v_left = pf->hw.func_caps.common_cap.num_msix_vectors;
  2670. /* reserve one vector for miscellaneous handler */
  2671. needed = 1;
  2672. v_budget += needed;
  2673. v_left -= needed;
  2674. /* reserve vectors for LAN traffic */
  2675. pf->num_lan_msix = min_t(int, num_online_cpus(), v_left);
  2676. v_budget += pf->num_lan_msix;
  2677. pf->msix_entries = devm_kcalloc(&pf->pdev->dev, v_budget,
  2678. sizeof(struct msix_entry), GFP_KERNEL);
  2679. if (!pf->msix_entries) {
  2680. err = -ENOMEM;
  2681. goto exit_err;
  2682. }
  2683. for (i = 0; i < v_budget; i++)
  2684. pf->msix_entries[i].entry = i;
  2685. /* actually reserve the vectors */
  2686. v_actual = pci_enable_msix_range(pf->pdev, pf->msix_entries,
  2687. ICE_MIN_MSIX, v_budget);
  2688. if (v_actual < 0) {
  2689. dev_err(&pf->pdev->dev, "unable to reserve MSI-X vectors\n");
  2690. err = v_actual;
  2691. goto msix_err;
  2692. }
  2693. if (v_actual < v_budget) {
  2694. dev_warn(&pf->pdev->dev,
  2695. "not enough vectors. requested = %d, obtained = %d\n",
  2696. v_budget, v_actual);
  2697. if (v_actual >= (pf->num_lan_msix + 1)) {
  2698. pf->num_avail_msix = v_actual - (pf->num_lan_msix + 1);
  2699. } else if (v_actual >= 2) {
  2700. pf->num_lan_msix = 1;
  2701. pf->num_avail_msix = v_actual - 2;
  2702. } else {
  2703. pci_disable_msix(pf->pdev);
  2704. err = -ERANGE;
  2705. goto msix_err;
  2706. }
  2707. }
  2708. return v_actual;
  2709. msix_err:
  2710. devm_kfree(&pf->pdev->dev, pf->msix_entries);
  2711. goto exit_err;
  2712. exit_err:
  2713. pf->num_lan_msix = 0;
  2714. clear_bit(ICE_FLAG_MSIX_ENA, pf->flags);
  2715. return err;
  2716. }
  2717. /**
  2718. * ice_dis_msix - Disable MSI-X interrupt setup in OS
  2719. * @pf: board private structure
  2720. */
  2721. static void ice_dis_msix(struct ice_pf *pf)
  2722. {
  2723. pci_disable_msix(pf->pdev);
  2724. devm_kfree(&pf->pdev->dev, pf->msix_entries);
  2725. pf->msix_entries = NULL;
  2726. clear_bit(ICE_FLAG_MSIX_ENA, pf->flags);
  2727. }
  2728. /**
  2729. * ice_init_interrupt_scheme - Determine proper interrupt scheme
  2730. * @pf: board private structure to initialize
  2731. */
  2732. static int ice_init_interrupt_scheme(struct ice_pf *pf)
  2733. {
  2734. int vectors = 0;
  2735. ssize_t size;
  2736. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
  2737. vectors = ice_ena_msix_range(pf);
  2738. else
  2739. return -ENODEV;
  2740. if (vectors < 0)
  2741. return vectors;
  2742. /* set up vector assignment tracking */
  2743. size = sizeof(struct ice_res_tracker) + (sizeof(u16) * vectors);
  2744. pf->irq_tracker = devm_kzalloc(&pf->pdev->dev, size, GFP_KERNEL);
  2745. if (!pf->irq_tracker) {
  2746. ice_dis_msix(pf);
  2747. return -ENOMEM;
  2748. }
  2749. pf->irq_tracker->num_entries = vectors;
  2750. return 0;
  2751. }
  2752. /**
  2753. * ice_clear_interrupt_scheme - Undo things done by ice_init_interrupt_scheme
  2754. * @pf: board private structure
  2755. */
  2756. static void ice_clear_interrupt_scheme(struct ice_pf *pf)
  2757. {
  2758. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
  2759. ice_dis_msix(pf);
  2760. devm_kfree(&pf->pdev->dev, pf->irq_tracker);
  2761. pf->irq_tracker = NULL;
  2762. }
  2763. /**
  2764. * ice_probe - Device initialization routine
  2765. * @pdev: PCI device information struct
  2766. * @ent: entry in ice_pci_tbl
  2767. *
  2768. * Returns 0 on success, negative on failure
  2769. */
  2770. static int ice_probe(struct pci_dev *pdev,
  2771. const struct pci_device_id __always_unused *ent)
  2772. {
  2773. struct ice_pf *pf;
  2774. struct ice_hw *hw;
  2775. int err;
  2776. /* this driver uses devres, see Documentation/driver-model/devres.txt */
  2777. err = pcim_enable_device(pdev);
  2778. if (err)
  2779. return err;
  2780. err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), pci_name(pdev));
  2781. if (err) {
  2782. dev_err(&pdev->dev, "I/O map error %d\n", err);
  2783. return err;
  2784. }
  2785. pf = devm_kzalloc(&pdev->dev, sizeof(*pf), GFP_KERNEL);
  2786. if (!pf)
  2787. return -ENOMEM;
  2788. /* set up for high or low dma */
  2789. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  2790. if (err)
  2791. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  2792. if (err) {
  2793. dev_err(&pdev->dev, "DMA configuration failed: 0x%x\n", err);
  2794. return err;
  2795. }
  2796. pci_enable_pcie_error_reporting(pdev);
  2797. pci_set_master(pdev);
  2798. pf->pdev = pdev;
  2799. pci_set_drvdata(pdev, pf);
  2800. set_bit(__ICE_DOWN, pf->state);
  2801. hw = &pf->hw;
  2802. hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
  2803. hw->back = pf;
  2804. hw->vendor_id = pdev->vendor;
  2805. hw->device_id = pdev->device;
  2806. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  2807. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  2808. hw->subsystem_device_id = pdev->subsystem_device;
  2809. hw->bus.device = PCI_SLOT(pdev->devfn);
  2810. hw->bus.func = PCI_FUNC(pdev->devfn);
  2811. ice_set_ctrlq_len(hw);
  2812. pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
  2813. #ifndef CONFIG_DYNAMIC_DEBUG
  2814. if (debug < -1)
  2815. hw->debug_mask = debug;
  2816. #endif
  2817. err = ice_init_hw(hw);
  2818. if (err) {
  2819. dev_err(&pdev->dev, "ice_init_hw failed: %d\n", err);
  2820. err = -EIO;
  2821. goto err_exit_unroll;
  2822. }
  2823. dev_info(&pdev->dev, "firmware %d.%d.%05d api %d.%d\n",
  2824. hw->fw_maj_ver, hw->fw_min_ver, hw->fw_build,
  2825. hw->api_maj_ver, hw->api_min_ver);
  2826. ice_init_pf(pf);
  2827. ice_determine_q_usage(pf);
  2828. pf->num_alloc_vsi = min_t(u16, ICE_MAX_VSI_ALLOC,
  2829. hw->func_caps.guaranteed_num_vsi);
  2830. if (!pf->num_alloc_vsi) {
  2831. err = -EIO;
  2832. goto err_init_pf_unroll;
  2833. }
  2834. pf->vsi = devm_kcalloc(&pdev->dev, pf->num_alloc_vsi,
  2835. sizeof(struct ice_vsi *), GFP_KERNEL);
  2836. if (!pf->vsi) {
  2837. err = -ENOMEM;
  2838. goto err_init_pf_unroll;
  2839. }
  2840. err = ice_init_interrupt_scheme(pf);
  2841. if (err) {
  2842. dev_err(&pdev->dev,
  2843. "ice_init_interrupt_scheme failed: %d\n", err);
  2844. err = -EIO;
  2845. goto err_init_interrupt_unroll;
  2846. }
  2847. /* In case of MSIX we are going to setup the misc vector right here
  2848. * to handle admin queue events etc. In case of legacy and MSI
  2849. * the misc functionality and queue processing is combined in
  2850. * the same vector and that gets setup at open.
  2851. */
  2852. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
  2853. err = ice_req_irq_msix_misc(pf);
  2854. if (err) {
  2855. dev_err(&pdev->dev,
  2856. "setup of misc vector failed: %d\n", err);
  2857. goto err_init_interrupt_unroll;
  2858. }
  2859. }
  2860. /* create switch struct for the switch element created by FW on boot */
  2861. pf->first_sw = devm_kzalloc(&pdev->dev, sizeof(struct ice_sw),
  2862. GFP_KERNEL);
  2863. if (!pf->first_sw) {
  2864. err = -ENOMEM;
  2865. goto err_msix_misc_unroll;
  2866. }
  2867. pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
  2868. pf->first_sw->pf = pf;
  2869. /* record the sw_id available for later use */
  2870. pf->first_sw->sw_id = hw->port_info->sw_id;
  2871. err = ice_setup_pf_sw(pf);
  2872. if (err) {
  2873. dev_err(&pdev->dev,
  2874. "probe failed due to setup pf switch:%d\n", err);
  2875. goto err_alloc_sw_unroll;
  2876. }
  2877. /* Driver is mostly up */
  2878. clear_bit(__ICE_DOWN, pf->state);
  2879. /* since everything is good, start the service timer */
  2880. mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
  2881. err = ice_init_link_events(pf->hw.port_info);
  2882. if (err) {
  2883. dev_err(&pdev->dev, "ice_init_link_events failed: %d\n", err);
  2884. goto err_alloc_sw_unroll;
  2885. }
  2886. return 0;
  2887. err_alloc_sw_unroll:
  2888. set_bit(__ICE_DOWN, pf->state);
  2889. devm_kfree(&pf->pdev->dev, pf->first_sw);
  2890. err_msix_misc_unroll:
  2891. ice_free_irq_msix_misc(pf);
  2892. err_init_interrupt_unroll:
  2893. ice_clear_interrupt_scheme(pf);
  2894. devm_kfree(&pdev->dev, pf->vsi);
  2895. err_init_pf_unroll:
  2896. ice_deinit_pf(pf);
  2897. ice_deinit_hw(hw);
  2898. err_exit_unroll:
  2899. pci_disable_pcie_error_reporting(pdev);
  2900. return err;
  2901. }
  2902. /**
  2903. * ice_remove - Device removal routine
  2904. * @pdev: PCI device information struct
  2905. */
  2906. static void ice_remove(struct pci_dev *pdev)
  2907. {
  2908. struct ice_pf *pf = pci_get_drvdata(pdev);
  2909. int i = 0;
  2910. int err;
  2911. if (!pf)
  2912. return;
  2913. set_bit(__ICE_DOWN, pf->state);
  2914. for (i = 0; i < pf->num_alloc_vsi; i++) {
  2915. if (!pf->vsi[i])
  2916. continue;
  2917. err = ice_vsi_release(pf->vsi[i]);
  2918. if (err)
  2919. dev_dbg(&pf->pdev->dev, "Failed to release VSI index %d (err %d)\n",
  2920. i, err);
  2921. }
  2922. ice_free_irq_msix_misc(pf);
  2923. ice_clear_interrupt_scheme(pf);
  2924. ice_deinit_pf(pf);
  2925. ice_deinit_hw(&pf->hw);
  2926. pci_disable_pcie_error_reporting(pdev);
  2927. }
  2928. /* ice_pci_tbl - PCI Device ID Table
  2929. *
  2930. * Wildcard entries (PCI_ANY_ID) should come last
  2931. * Last entry must be all 0s
  2932. *
  2933. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  2934. * Class, Class Mask, private data (not used) }
  2935. */
  2936. static const struct pci_device_id ice_pci_tbl[] = {
  2937. { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_BACKPLANE), 0 },
  2938. { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_QSFP), 0 },
  2939. { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_SFP), 0 },
  2940. { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_10G_BASE_T), 0 },
  2941. { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_SGMII), 0 },
  2942. /* required last entry */
  2943. { 0, }
  2944. };
  2945. MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
  2946. static struct pci_driver ice_driver = {
  2947. .name = KBUILD_MODNAME,
  2948. .id_table = ice_pci_tbl,
  2949. .probe = ice_probe,
  2950. .remove = ice_remove,
  2951. };
  2952. /**
  2953. * ice_module_init - Driver registration routine
  2954. *
  2955. * ice_module_init is the first routine called when the driver is
  2956. * loaded. All it does is register with the PCI subsystem.
  2957. */
  2958. static int __init ice_module_init(void)
  2959. {
  2960. int status;
  2961. pr_info("%s - version %s\n", ice_driver_string, ice_drv_ver);
  2962. pr_info("%s\n", ice_copyright);
  2963. ice_wq = alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM, KBUILD_MODNAME);
  2964. if (!ice_wq) {
  2965. pr_err("Failed to create workqueue\n");
  2966. return -ENOMEM;
  2967. }
  2968. status = pci_register_driver(&ice_driver);
  2969. if (status) {
  2970. pr_err("failed to register pci driver, err %d\n", status);
  2971. destroy_workqueue(ice_wq);
  2972. }
  2973. return status;
  2974. }
  2975. module_init(ice_module_init);
  2976. /**
  2977. * ice_module_exit - Driver exit cleanup routine
  2978. *
  2979. * ice_module_exit is called just before the driver is removed
  2980. * from memory.
  2981. */
  2982. static void __exit ice_module_exit(void)
  2983. {
  2984. pci_unregister_driver(&ice_driver);
  2985. destroy_workqueue(ice_wq);
  2986. pr_info("module unloaded\n");
  2987. }
  2988. module_exit(ice_module_exit);
  2989. /**
  2990. * ice_set_mac_address - NDO callback to set mac address
  2991. * @netdev: network interface device structure
  2992. * @pi: pointer to an address structure
  2993. *
  2994. * Returns 0 on success, negative on failure
  2995. */
  2996. static int ice_set_mac_address(struct net_device *netdev, void *pi)
  2997. {
  2998. struct ice_netdev_priv *np = netdev_priv(netdev);
  2999. struct ice_vsi *vsi = np->vsi;
  3000. struct ice_pf *pf = vsi->back;
  3001. struct ice_hw *hw = &pf->hw;
  3002. struct sockaddr *addr = pi;
  3003. enum ice_status status;
  3004. LIST_HEAD(a_mac_list);
  3005. LIST_HEAD(r_mac_list);
  3006. u8 flags = 0;
  3007. int err;
  3008. u8 *mac;
  3009. mac = (u8 *)addr->sa_data;
  3010. if (!is_valid_ether_addr(mac))
  3011. return -EADDRNOTAVAIL;
  3012. if (ether_addr_equal(netdev->dev_addr, mac)) {
  3013. netdev_warn(netdev, "already using mac %pM\n", mac);
  3014. return 0;
  3015. }
  3016. if (test_bit(__ICE_DOWN, pf->state) ||
  3017. ice_is_reset_recovery_pending(pf->state)) {
  3018. netdev_err(netdev, "can't set mac %pM. device not ready\n",
  3019. mac);
  3020. return -EBUSY;
  3021. }
  3022. /* When we change the mac address we also have to change the mac address
  3023. * based filter rules that were created previously for the old mac
  3024. * address. So first, we remove the old filter rule using ice_remove_mac
  3025. * and then create a new filter rule using ice_add_mac. Note that for
  3026. * both these operations, we first need to form a "list" of mac
  3027. * addresses (even though in this case, we have only 1 mac address to be
  3028. * added/removed) and this done using ice_add_mac_to_list. Depending on
  3029. * the ensuing operation this "list" of mac addresses is either to be
  3030. * added or removed from the filter.
  3031. */
  3032. err = ice_add_mac_to_list(vsi, &r_mac_list, netdev->dev_addr);
  3033. if (err) {
  3034. err = -EADDRNOTAVAIL;
  3035. goto free_lists;
  3036. }
  3037. status = ice_remove_mac(hw, &r_mac_list);
  3038. if (status) {
  3039. err = -EADDRNOTAVAIL;
  3040. goto free_lists;
  3041. }
  3042. err = ice_add_mac_to_list(vsi, &a_mac_list, mac);
  3043. if (err) {
  3044. err = -EADDRNOTAVAIL;
  3045. goto free_lists;
  3046. }
  3047. status = ice_add_mac(hw, &a_mac_list);
  3048. if (status) {
  3049. err = -EADDRNOTAVAIL;
  3050. goto free_lists;
  3051. }
  3052. free_lists:
  3053. /* free list entries */
  3054. ice_free_fltr_list(&pf->pdev->dev, &r_mac_list);
  3055. ice_free_fltr_list(&pf->pdev->dev, &a_mac_list);
  3056. if (err) {
  3057. netdev_err(netdev, "can't set mac %pM. filter update failed\n",
  3058. mac);
  3059. return err;
  3060. }
  3061. /* change the netdev's mac address */
  3062. memcpy(netdev->dev_addr, mac, netdev->addr_len);
  3063. netdev_dbg(vsi->netdev, "updated mac address to %pM\n",
  3064. netdev->dev_addr);
  3065. /* write new mac address to the firmware */
  3066. flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
  3067. status = ice_aq_manage_mac_write(hw, mac, flags, NULL);
  3068. if (status) {
  3069. netdev_err(netdev, "can't set mac %pM. write to firmware failed.\n",
  3070. mac);
  3071. }
  3072. return 0;
  3073. }
  3074. /**
  3075. * ice_set_rx_mode - NDO callback to set the netdev filters
  3076. * @netdev: network interface device structure
  3077. */
  3078. static void ice_set_rx_mode(struct net_device *netdev)
  3079. {
  3080. struct ice_netdev_priv *np = netdev_priv(netdev);
  3081. struct ice_vsi *vsi = np->vsi;
  3082. if (!vsi)
  3083. return;
  3084. /* Set the flags to synchronize filters
  3085. * ndo_set_rx_mode may be triggered even without a change in netdev
  3086. * flags
  3087. */
  3088. set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
  3089. set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
  3090. set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
  3091. /* schedule our worker thread which will take care of
  3092. * applying the new filter changes
  3093. */
  3094. ice_service_task_schedule(vsi->back);
  3095. }
  3096. /**
  3097. * ice_fdb_add - add an entry to the hardware database
  3098. * @ndm: the input from the stack
  3099. * @tb: pointer to array of nladdr (unused)
  3100. * @dev: the net device pointer
  3101. * @addr: the MAC address entry being added
  3102. * @vid: VLAN id
  3103. * @flags: instructions from stack about fdb operation
  3104. */
  3105. static int ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
  3106. struct net_device *dev, const unsigned char *addr,
  3107. u16 vid, u16 flags)
  3108. {
  3109. int err;
  3110. if (vid) {
  3111. netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
  3112. return -EINVAL;
  3113. }
  3114. if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
  3115. netdev_err(dev, "FDB only supports static addresses\n");
  3116. return -EINVAL;
  3117. }
  3118. if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
  3119. err = dev_uc_add_excl(dev, addr);
  3120. else if (is_multicast_ether_addr(addr))
  3121. err = dev_mc_add_excl(dev, addr);
  3122. else
  3123. err = -EINVAL;
  3124. /* Only return duplicate errors if NLM_F_EXCL is set */
  3125. if (err == -EEXIST && !(flags & NLM_F_EXCL))
  3126. err = 0;
  3127. return err;
  3128. }
  3129. /**
  3130. * ice_fdb_del - delete an entry from the hardware database
  3131. * @ndm: the input from the stack
  3132. * @tb: pointer to array of nladdr (unused)
  3133. * @dev: the net device pointer
  3134. * @addr: the MAC address entry being added
  3135. * @vid: VLAN id
  3136. */
  3137. static int ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
  3138. struct net_device *dev, const unsigned char *addr,
  3139. __always_unused u16 vid)
  3140. {
  3141. int err;
  3142. if (ndm->ndm_state & NUD_PERMANENT) {
  3143. netdev_err(dev, "FDB only supports static addresses\n");
  3144. return -EINVAL;
  3145. }
  3146. if (is_unicast_ether_addr(addr))
  3147. err = dev_uc_del(dev, addr);
  3148. else if (is_multicast_ether_addr(addr))
  3149. err = dev_mc_del(dev, addr);
  3150. else
  3151. err = -EINVAL;
  3152. return err;
  3153. }
  3154. /**
  3155. * ice_vsi_manage_vlan_insertion - Manage VLAN insertion for the VSI for Tx
  3156. * @vsi: the vsi being changed
  3157. */
  3158. static int ice_vsi_manage_vlan_insertion(struct ice_vsi *vsi)
  3159. {
  3160. struct device *dev = &vsi->back->pdev->dev;
  3161. struct ice_hw *hw = &vsi->back->hw;
  3162. struct ice_vsi_ctx ctxt = { 0 };
  3163. enum ice_status status;
  3164. /* Here we are configuring the VSI to let the driver add VLAN tags by
  3165. * setting port_vlan_flags to ICE_AQ_VSI_PVLAN_MODE_ALL. The actual VLAN
  3166. * tag insertion happens in the Tx hot path, in ice_tx_map.
  3167. */
  3168. ctxt.info.port_vlan_flags = ICE_AQ_VSI_PVLAN_MODE_ALL;
  3169. ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
  3170. ctxt.vsi_num = vsi->vsi_num;
  3171. status = ice_aq_update_vsi(hw, &ctxt, NULL);
  3172. if (status) {
  3173. dev_err(dev, "update VSI for VLAN insert failed, err %d aq_err %d\n",
  3174. status, hw->adminq.sq_last_status);
  3175. return -EIO;
  3176. }
  3177. vsi->info.port_vlan_flags = ctxt.info.port_vlan_flags;
  3178. return 0;
  3179. }
  3180. /**
  3181. * ice_vsi_manage_vlan_stripping - Manage VLAN stripping for the VSI for Rx
  3182. * @vsi: the vsi being changed
  3183. * @ena: boolean value indicating if this is a enable or disable request
  3184. */
  3185. static int ice_vsi_manage_vlan_stripping(struct ice_vsi *vsi, bool ena)
  3186. {
  3187. struct device *dev = &vsi->back->pdev->dev;
  3188. struct ice_hw *hw = &vsi->back->hw;
  3189. struct ice_vsi_ctx ctxt = { 0 };
  3190. enum ice_status status;
  3191. /* Here we are configuring what the VSI should do with the VLAN tag in
  3192. * the Rx packet. We can either leave the tag in the packet or put it in
  3193. * the Rx descriptor.
  3194. */
  3195. if (ena) {
  3196. /* Strip VLAN tag from Rx packet and put it in the desc */
  3197. ctxt.info.port_vlan_flags = ICE_AQ_VSI_PVLAN_EMOD_STR_BOTH;
  3198. } else {
  3199. /* Disable stripping. Leave tag in packet */
  3200. ctxt.info.port_vlan_flags = ICE_AQ_VSI_PVLAN_EMOD_NOTHING;
  3201. }
  3202. ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
  3203. ctxt.vsi_num = vsi->vsi_num;
  3204. status = ice_aq_update_vsi(hw, &ctxt, NULL);
  3205. if (status) {
  3206. dev_err(dev, "update VSI for VALN strip failed, ena = %d err %d aq_err %d\n",
  3207. ena, status, hw->adminq.sq_last_status);
  3208. return -EIO;
  3209. }
  3210. vsi->info.port_vlan_flags = ctxt.info.port_vlan_flags;
  3211. return 0;
  3212. }
  3213. /**
  3214. * ice_set_features - set the netdev feature flags
  3215. * @netdev: ptr to the netdev being adjusted
  3216. * @features: the feature set that the stack is suggesting
  3217. */
  3218. static int ice_set_features(struct net_device *netdev,
  3219. netdev_features_t features)
  3220. {
  3221. struct ice_netdev_priv *np = netdev_priv(netdev);
  3222. struct ice_vsi *vsi = np->vsi;
  3223. int ret = 0;
  3224. if ((features & NETIF_F_HW_VLAN_CTAG_RX) &&
  3225. !(netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
  3226. ret = ice_vsi_manage_vlan_stripping(vsi, true);
  3227. else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) &&
  3228. (netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
  3229. ret = ice_vsi_manage_vlan_stripping(vsi, false);
  3230. else if ((features & NETIF_F_HW_VLAN_CTAG_TX) &&
  3231. !(netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
  3232. ret = ice_vsi_manage_vlan_insertion(vsi);
  3233. else if (!(features & NETIF_F_HW_VLAN_CTAG_TX) &&
  3234. (netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
  3235. ret = ice_vsi_manage_vlan_insertion(vsi);
  3236. return ret;
  3237. }
  3238. /**
  3239. * ice_vsi_vlan_setup - Setup vlan offload properties on a VSI
  3240. * @vsi: VSI to setup vlan properties for
  3241. */
  3242. static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
  3243. {
  3244. int ret = 0;
  3245. if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
  3246. ret = ice_vsi_manage_vlan_stripping(vsi, true);
  3247. if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)
  3248. ret = ice_vsi_manage_vlan_insertion(vsi);
  3249. return ret;
  3250. }
  3251. /**
  3252. * ice_restore_vlan - Reinstate VLANs when vsi/netdev comes back up
  3253. * @vsi: the VSI being brought back up
  3254. */
  3255. static int ice_restore_vlan(struct ice_vsi *vsi)
  3256. {
  3257. int err;
  3258. u16 vid;
  3259. if (!vsi->netdev)
  3260. return -EINVAL;
  3261. err = ice_vsi_vlan_setup(vsi);
  3262. if (err)
  3263. return err;
  3264. for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID) {
  3265. err = ice_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q), vid);
  3266. if (err)
  3267. break;
  3268. }
  3269. return err;
  3270. }
  3271. /**
  3272. * ice_setup_tx_ctx - setup a struct ice_tlan_ctx instance
  3273. * @ring: The Tx ring to configure
  3274. * @tlan_ctx: Pointer to the Tx LAN queue context structure to be initialized
  3275. * @pf_q: queue index in the PF space
  3276. *
  3277. * Configure the Tx descriptor ring in TLAN context.
  3278. */
  3279. static void
  3280. ice_setup_tx_ctx(struct ice_ring *ring, struct ice_tlan_ctx *tlan_ctx, u16 pf_q)
  3281. {
  3282. struct ice_vsi *vsi = ring->vsi;
  3283. struct ice_hw *hw = &vsi->back->hw;
  3284. tlan_ctx->base = ring->dma >> ICE_TLAN_CTX_BASE_S;
  3285. tlan_ctx->port_num = vsi->port_info->lport;
  3286. /* Transmit Queue Length */
  3287. tlan_ctx->qlen = ring->count;
  3288. /* PF number */
  3289. tlan_ctx->pf_num = hw->pf_id;
  3290. /* queue belongs to a specific VSI type
  3291. * VF / VM index should be programmed per vmvf_type setting:
  3292. * for vmvf_type = VF, it is VF number between 0-256
  3293. * for vmvf_type = VM, it is VM number between 0-767
  3294. * for PF or EMP this field should be set to zero
  3295. */
  3296. switch (vsi->type) {
  3297. case ICE_VSI_PF:
  3298. tlan_ctx->vmvf_type = ICE_TLAN_CTX_VMVF_TYPE_PF;
  3299. break;
  3300. default:
  3301. return;
  3302. }
  3303. /* make sure the context is associated with the right VSI */
  3304. tlan_ctx->src_vsi = vsi->vsi_num;
  3305. tlan_ctx->tso_ena = ICE_TX_LEGACY;
  3306. tlan_ctx->tso_qnum = pf_q;
  3307. /* Legacy or Advanced Host Interface:
  3308. * 0: Advanced Host Interface
  3309. * 1: Legacy Host Interface
  3310. */
  3311. tlan_ctx->legacy_int = ICE_TX_LEGACY;
  3312. }
  3313. /**
  3314. * ice_vsi_cfg_txqs - Configure the VSI for Tx
  3315. * @vsi: the VSI being configured
  3316. *
  3317. * Return 0 on success and a negative value on error
  3318. * Configure the Tx VSI for operation.
  3319. */
  3320. static int ice_vsi_cfg_txqs(struct ice_vsi *vsi)
  3321. {
  3322. struct ice_aqc_add_tx_qgrp *qg_buf;
  3323. struct ice_aqc_add_txqs_perq *txq;
  3324. struct ice_pf *pf = vsi->back;
  3325. enum ice_status status;
  3326. u16 buf_len, i, pf_q;
  3327. int err = 0, tc = 0;
  3328. u8 num_q_grps;
  3329. buf_len = sizeof(struct ice_aqc_add_tx_qgrp);
  3330. qg_buf = devm_kzalloc(&pf->pdev->dev, buf_len, GFP_KERNEL);
  3331. if (!qg_buf)
  3332. return -ENOMEM;
  3333. if (vsi->num_txq > ICE_MAX_TXQ_PER_TXQG) {
  3334. err = -EINVAL;
  3335. goto err_cfg_txqs;
  3336. }
  3337. qg_buf->num_txqs = 1;
  3338. num_q_grps = 1;
  3339. /* set up and configure the tx queues */
  3340. ice_for_each_txq(vsi, i) {
  3341. struct ice_tlan_ctx tlan_ctx = { 0 };
  3342. pf_q = vsi->txq_map[i];
  3343. ice_setup_tx_ctx(vsi->tx_rings[i], &tlan_ctx, pf_q);
  3344. /* copy context contents into the qg_buf */
  3345. qg_buf->txqs[0].txq_id = cpu_to_le16(pf_q);
  3346. ice_set_ctx((u8 *)&tlan_ctx, qg_buf->txqs[0].txq_ctx,
  3347. ice_tlan_ctx_info);
  3348. /* init queue specific tail reg. It is referred as transmit
  3349. * comm scheduler queue doorbell.
  3350. */
  3351. vsi->tx_rings[i]->tail = pf->hw.hw_addr + QTX_COMM_DBELL(pf_q);
  3352. status = ice_ena_vsi_txq(vsi->port_info, vsi->vsi_num, tc,
  3353. num_q_grps, qg_buf, buf_len, NULL);
  3354. if (status) {
  3355. dev_err(&vsi->back->pdev->dev,
  3356. "Failed to set LAN Tx queue context, error: %d\n",
  3357. status);
  3358. err = -ENODEV;
  3359. goto err_cfg_txqs;
  3360. }
  3361. /* Add Tx Queue TEID into the VSI tx ring from the response
  3362. * This will complete configuring and enabling the queue.
  3363. */
  3364. txq = &qg_buf->txqs[0];
  3365. if (pf_q == le16_to_cpu(txq->txq_id))
  3366. vsi->tx_rings[i]->txq_teid =
  3367. le32_to_cpu(txq->q_teid);
  3368. }
  3369. err_cfg_txqs:
  3370. devm_kfree(&pf->pdev->dev, qg_buf);
  3371. return err;
  3372. }
  3373. /**
  3374. * ice_setup_rx_ctx - Configure a receive ring context
  3375. * @ring: The Rx ring to configure
  3376. *
  3377. * Configure the Rx descriptor ring in RLAN context.
  3378. */
  3379. static int ice_setup_rx_ctx(struct ice_ring *ring)
  3380. {
  3381. struct ice_vsi *vsi = ring->vsi;
  3382. struct ice_hw *hw = &vsi->back->hw;
  3383. u32 rxdid = ICE_RXDID_FLEX_NIC;
  3384. struct ice_rlan_ctx rlan_ctx;
  3385. u32 regval;
  3386. u16 pf_q;
  3387. int err;
  3388. /* what is RX queue number in global space of 2K rx queues */
  3389. pf_q = vsi->rxq_map[ring->q_index];
  3390. /* clear the context structure first */
  3391. memset(&rlan_ctx, 0, sizeof(rlan_ctx));
  3392. rlan_ctx.base = ring->dma >> 7;
  3393. rlan_ctx.qlen = ring->count;
  3394. /* Receive Packet Data Buffer Size.
  3395. * The Packet Data Buffer Size is defined in 128 byte units.
  3396. */
  3397. rlan_ctx.dbuf = vsi->rx_buf_len >> ICE_RLAN_CTX_DBUF_S;
  3398. /* use 32 byte descriptors */
  3399. rlan_ctx.dsize = 1;
  3400. /* Strip the Ethernet CRC bytes before the packet is posted to host
  3401. * memory.
  3402. */
  3403. rlan_ctx.crcstrip = 1;
  3404. /* L2TSEL flag defines the reported L2 Tags in the receive descriptor */
  3405. rlan_ctx.l2tsel = 1;
  3406. rlan_ctx.dtype = ICE_RX_DTYPE_NO_SPLIT;
  3407. rlan_ctx.hsplit_0 = ICE_RLAN_RX_HSPLIT_0_NO_SPLIT;
  3408. rlan_ctx.hsplit_1 = ICE_RLAN_RX_HSPLIT_1_NO_SPLIT;
  3409. /* This controls whether VLAN is stripped from inner headers
  3410. * The VLAN in the inner L2 header is stripped to the receive
  3411. * descriptor if enabled by this flag.
  3412. */
  3413. rlan_ctx.showiv = 0;
  3414. /* Max packet size for this queue - must not be set to a larger value
  3415. * than 5 x DBUF
  3416. */
  3417. rlan_ctx.rxmax = min_t(u16, vsi->max_frame,
  3418. ICE_MAX_CHAINED_RX_BUFS * vsi->rx_buf_len);
  3419. /* Rx queue threshold in units of 64 */
  3420. rlan_ctx.lrxqthresh = 1;
  3421. /* Enable Flexible Descriptors in the queue context which
  3422. * allows this driver to select a specific receive descriptor format
  3423. */
  3424. regval = rd32(hw, QRXFLXP_CNTXT(pf_q));
  3425. regval |= (rxdid << QRXFLXP_CNTXT_RXDID_IDX_S) &
  3426. QRXFLXP_CNTXT_RXDID_IDX_M;
  3427. /* increasing context priority to pick up profile id;
  3428. * default is 0x01; setting to 0x03 to ensure profile
  3429. * is programming if prev context is of same priority
  3430. */
  3431. regval |= (0x03 << QRXFLXP_CNTXT_RXDID_PRIO_S) &
  3432. QRXFLXP_CNTXT_RXDID_PRIO_M;
  3433. wr32(hw, QRXFLXP_CNTXT(pf_q), regval);
  3434. /* Absolute queue number out of 2K needs to be passed */
  3435. err = ice_write_rxq_ctx(hw, &rlan_ctx, pf_q);
  3436. if (err) {
  3437. dev_err(&vsi->back->pdev->dev,
  3438. "Failed to set LAN Rx queue context for absolute Rx queue %d error: %d\n",
  3439. pf_q, err);
  3440. return -EIO;
  3441. }
  3442. /* init queue specific tail register */
  3443. ring->tail = hw->hw_addr + QRX_TAIL(pf_q);
  3444. writel(0, ring->tail);
  3445. ice_alloc_rx_bufs(ring, ICE_DESC_UNUSED(ring));
  3446. return 0;
  3447. }
  3448. /**
  3449. * ice_vsi_cfg_rxqs - Configure the VSI for Rx
  3450. * @vsi: the VSI being configured
  3451. *
  3452. * Return 0 on success and a negative value on error
  3453. * Configure the Rx VSI for operation.
  3454. */
  3455. static int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
  3456. {
  3457. int err = 0;
  3458. u16 i;
  3459. if (vsi->netdev && vsi->netdev->mtu > ETH_DATA_LEN)
  3460. vsi->max_frame = vsi->netdev->mtu +
  3461. ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  3462. else
  3463. vsi->max_frame = ICE_RXBUF_2048;
  3464. vsi->rx_buf_len = ICE_RXBUF_2048;
  3465. /* set up individual rings */
  3466. for (i = 0; i < vsi->num_rxq && !err; i++)
  3467. err = ice_setup_rx_ctx(vsi->rx_rings[i]);
  3468. if (err) {
  3469. dev_err(&vsi->back->pdev->dev, "ice_setup_rx_ctx failed\n");
  3470. return -EIO;
  3471. }
  3472. return err;
  3473. }
  3474. /**
  3475. * ice_vsi_cfg - Setup the VSI
  3476. * @vsi: the VSI being configured
  3477. *
  3478. * Return 0 on success and negative value on error
  3479. */
  3480. static int ice_vsi_cfg(struct ice_vsi *vsi)
  3481. {
  3482. int err;
  3483. ice_set_rx_mode(vsi->netdev);
  3484. err = ice_restore_vlan(vsi);
  3485. if (err)
  3486. return err;
  3487. err = ice_vsi_cfg_txqs(vsi);
  3488. if (!err)
  3489. err = ice_vsi_cfg_rxqs(vsi);
  3490. return err;
  3491. }
  3492. /**
  3493. * ice_vsi_stop_tx_rings - Disable Tx rings
  3494. * @vsi: the VSI being configured
  3495. */
  3496. static int ice_vsi_stop_tx_rings(struct ice_vsi *vsi)
  3497. {
  3498. struct ice_pf *pf = vsi->back;
  3499. struct ice_hw *hw = &pf->hw;
  3500. enum ice_status status;
  3501. u32 *q_teids, val;
  3502. u16 *q_ids, i;
  3503. int err = 0;
  3504. if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
  3505. return -EINVAL;
  3506. q_teids = devm_kcalloc(&pf->pdev->dev, vsi->num_txq, sizeof(*q_teids),
  3507. GFP_KERNEL);
  3508. if (!q_teids)
  3509. return -ENOMEM;
  3510. q_ids = devm_kcalloc(&pf->pdev->dev, vsi->num_txq, sizeof(*q_ids),
  3511. GFP_KERNEL);
  3512. if (!q_ids) {
  3513. err = -ENOMEM;
  3514. goto err_alloc_q_ids;
  3515. }
  3516. /* set up the tx queue list to be disabled */
  3517. ice_for_each_txq(vsi, i) {
  3518. u16 v_idx;
  3519. if (!vsi->tx_rings || !vsi->tx_rings[i]) {
  3520. err = -EINVAL;
  3521. goto err_out;
  3522. }
  3523. q_ids[i] = vsi->txq_map[i];
  3524. q_teids[i] = vsi->tx_rings[i]->txq_teid;
  3525. /* clear cause_ena bit for disabled queues */
  3526. val = rd32(hw, QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
  3527. val &= ~QINT_TQCTL_CAUSE_ENA_M;
  3528. wr32(hw, QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
  3529. /* software is expected to wait for 100 ns */
  3530. ndelay(100);
  3531. /* trigger a software interrupt for the vector associated to
  3532. * the queue to schedule napi handler
  3533. */
  3534. v_idx = vsi->tx_rings[i]->q_vector->v_idx;
  3535. wr32(hw, GLINT_DYN_CTL(vsi->base_vector + v_idx),
  3536. GLINT_DYN_CTL_SWINT_TRIG_M | GLINT_DYN_CTL_INTENA_MSK_M);
  3537. }
  3538. status = ice_dis_vsi_txq(vsi->port_info, vsi->num_txq, q_ids, q_teids,
  3539. NULL);
  3540. if (status) {
  3541. dev_err(&pf->pdev->dev,
  3542. "Failed to disable LAN Tx queues, error: %d\n",
  3543. status);
  3544. err = -ENODEV;
  3545. }
  3546. err_out:
  3547. devm_kfree(&pf->pdev->dev, q_ids);
  3548. err_alloc_q_ids:
  3549. devm_kfree(&pf->pdev->dev, q_teids);
  3550. return err;
  3551. }
  3552. /**
  3553. * ice_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
  3554. * @pf: the PF being configured
  3555. * @pf_q: the PF queue
  3556. * @ena: enable or disable state of the queue
  3557. *
  3558. * This routine will wait for the given Rx queue of the PF to reach the
  3559. * enabled or disabled state.
  3560. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3561. * multiple retries; else will return 0 in case of success.
  3562. */
  3563. static int ice_pf_rxq_wait(struct ice_pf *pf, int pf_q, bool ena)
  3564. {
  3565. int i;
  3566. for (i = 0; i < ICE_Q_WAIT_RETRY_LIMIT; i++) {
  3567. u32 rx_reg = rd32(&pf->hw, QRX_CTRL(pf_q));
  3568. if (ena == !!(rx_reg & QRX_CTRL_QENA_STAT_M))
  3569. break;
  3570. usleep_range(10, 20);
  3571. }
  3572. if (i >= ICE_Q_WAIT_RETRY_LIMIT)
  3573. return -ETIMEDOUT;
  3574. return 0;
  3575. }
  3576. /**
  3577. * ice_vsi_ctrl_rx_rings - Start or stop a VSI's rx rings
  3578. * @vsi: the VSI being configured
  3579. * @ena: start or stop the rx rings
  3580. */
  3581. static int ice_vsi_ctrl_rx_rings(struct ice_vsi *vsi, bool ena)
  3582. {
  3583. struct ice_pf *pf = vsi->back;
  3584. struct ice_hw *hw = &pf->hw;
  3585. int i, j, ret = 0;
  3586. for (i = 0; i < vsi->num_rxq; i++) {
  3587. int pf_q = vsi->rxq_map[i];
  3588. u32 rx_reg;
  3589. for (j = 0; j < ICE_Q_WAIT_MAX_RETRY; j++) {
  3590. rx_reg = rd32(hw, QRX_CTRL(pf_q));
  3591. if (((rx_reg >> QRX_CTRL_QENA_REQ_S) & 1) ==
  3592. ((rx_reg >> QRX_CTRL_QENA_STAT_S) & 1))
  3593. break;
  3594. usleep_range(1000, 2000);
  3595. }
  3596. /* Skip if the queue is already in the requested state */
  3597. if (ena == !!(rx_reg & QRX_CTRL_QENA_STAT_M))
  3598. continue;
  3599. /* turn on/off the queue */
  3600. if (ena)
  3601. rx_reg |= QRX_CTRL_QENA_REQ_M;
  3602. else
  3603. rx_reg &= ~QRX_CTRL_QENA_REQ_M;
  3604. wr32(hw, QRX_CTRL(pf_q), rx_reg);
  3605. /* wait for the change to finish */
  3606. ret = ice_pf_rxq_wait(pf, pf_q, ena);
  3607. if (ret) {
  3608. dev_err(&pf->pdev->dev,
  3609. "VSI idx %d Rx ring %d %sable timeout\n",
  3610. vsi->idx, pf_q, (ena ? "en" : "dis"));
  3611. break;
  3612. }
  3613. }
  3614. return ret;
  3615. }
  3616. /**
  3617. * ice_vsi_start_rx_rings - start VSI's rx rings
  3618. * @vsi: the VSI whose rings are to be started
  3619. *
  3620. * Returns 0 on success and a negative value on error
  3621. */
  3622. static int ice_vsi_start_rx_rings(struct ice_vsi *vsi)
  3623. {
  3624. return ice_vsi_ctrl_rx_rings(vsi, true);
  3625. }
  3626. /**
  3627. * ice_vsi_stop_rx_rings - stop VSI's rx rings
  3628. * @vsi: the VSI
  3629. *
  3630. * Returns 0 on success and a negative value on error
  3631. */
  3632. static int ice_vsi_stop_rx_rings(struct ice_vsi *vsi)
  3633. {
  3634. return ice_vsi_ctrl_rx_rings(vsi, false);
  3635. }
  3636. /**
  3637. * ice_vsi_stop_tx_rx_rings - stop VSI's tx and rx rings
  3638. * @vsi: the VSI
  3639. * Returns 0 on success and a negative value on error
  3640. */
  3641. static int ice_vsi_stop_tx_rx_rings(struct ice_vsi *vsi)
  3642. {
  3643. int err_tx, err_rx;
  3644. err_tx = ice_vsi_stop_tx_rings(vsi);
  3645. if (err_tx)
  3646. dev_dbg(&vsi->back->pdev->dev, "Failed to disable Tx rings\n");
  3647. err_rx = ice_vsi_stop_rx_rings(vsi);
  3648. if (err_rx)
  3649. dev_dbg(&vsi->back->pdev->dev, "Failed to disable Rx rings\n");
  3650. if (err_tx || err_rx)
  3651. return -EIO;
  3652. return 0;
  3653. }
  3654. /**
  3655. * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
  3656. * @vsi: the VSI being configured
  3657. */
  3658. static void ice_napi_enable_all(struct ice_vsi *vsi)
  3659. {
  3660. int q_idx;
  3661. if (!vsi->netdev)
  3662. return;
  3663. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
  3664. napi_enable(&vsi->q_vectors[q_idx]->napi);
  3665. }
  3666. /**
  3667. * ice_up_complete - Finish the last steps of bringing up a connection
  3668. * @vsi: The VSI being configured
  3669. *
  3670. * Return 0 on success and negative value on error
  3671. */
  3672. static int ice_up_complete(struct ice_vsi *vsi)
  3673. {
  3674. struct ice_pf *pf = vsi->back;
  3675. int err;
  3676. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
  3677. ice_vsi_cfg_msix(vsi);
  3678. else
  3679. return -ENOTSUPP;
  3680. /* Enable only Rx rings, Tx rings were enabled by the FW when the
  3681. * Tx queue group list was configured and the context bits were
  3682. * programmed using ice_vsi_cfg_txqs
  3683. */
  3684. err = ice_vsi_start_rx_rings(vsi);
  3685. if (err)
  3686. return err;
  3687. clear_bit(__ICE_DOWN, vsi->state);
  3688. ice_napi_enable_all(vsi);
  3689. ice_vsi_ena_irq(vsi);
  3690. if (vsi->port_info &&
  3691. (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
  3692. vsi->netdev) {
  3693. ice_print_link_msg(vsi, true);
  3694. netif_tx_start_all_queues(vsi->netdev);
  3695. netif_carrier_on(vsi->netdev);
  3696. }
  3697. ice_service_task_schedule(pf);
  3698. return err;
  3699. }
  3700. /**
  3701. * ice_up - Bring the connection back up after being down
  3702. * @vsi: VSI being configured
  3703. */
  3704. int ice_up(struct ice_vsi *vsi)
  3705. {
  3706. int err;
  3707. err = ice_vsi_cfg(vsi);
  3708. if (!err)
  3709. err = ice_up_complete(vsi);
  3710. return err;
  3711. }
  3712. /**
  3713. * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
  3714. * @ring: Tx or Rx ring to read stats from
  3715. * @pkts: packets stats counter
  3716. * @bytes: bytes stats counter
  3717. *
  3718. * This function fetches stats from the ring considering the atomic operations
  3719. * that needs to be performed to read u64 values in 32 bit machine.
  3720. */
  3721. static void ice_fetch_u64_stats_per_ring(struct ice_ring *ring, u64 *pkts,
  3722. u64 *bytes)
  3723. {
  3724. unsigned int start;
  3725. *pkts = 0;
  3726. *bytes = 0;
  3727. if (!ring)
  3728. return;
  3729. do {
  3730. start = u64_stats_fetch_begin_irq(&ring->syncp);
  3731. *pkts = ring->stats.pkts;
  3732. *bytes = ring->stats.bytes;
  3733. } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
  3734. }
  3735. /**
  3736. * ice_stat_update40 - read 40 bit stat from the chip and update stat values
  3737. * @hw: ptr to the hardware info
  3738. * @hireg: high 32 bit HW register to read from
  3739. * @loreg: low 32 bit HW register to read from
  3740. * @prev_stat_loaded: bool to specify if previous stats are loaded
  3741. * @prev_stat: ptr to previous loaded stat value
  3742. * @cur_stat: ptr to current stat value
  3743. */
  3744. static void ice_stat_update40(struct ice_hw *hw, u32 hireg, u32 loreg,
  3745. bool prev_stat_loaded, u64 *prev_stat,
  3746. u64 *cur_stat)
  3747. {
  3748. u64 new_data;
  3749. new_data = rd32(hw, loreg);
  3750. new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
  3751. /* device stats are not reset at PFR, they likely will not be zeroed
  3752. * when the driver starts. So save the first values read and use them as
  3753. * offsets to be subtracted from the raw values in order to report stats
  3754. * that count from zero.
  3755. */
  3756. if (!prev_stat_loaded)
  3757. *prev_stat = new_data;
  3758. if (likely(new_data >= *prev_stat))
  3759. *cur_stat = new_data - *prev_stat;
  3760. else
  3761. /* to manage the potential roll-over */
  3762. *cur_stat = (new_data + BIT_ULL(40)) - *prev_stat;
  3763. *cur_stat &= 0xFFFFFFFFFFULL;
  3764. }
  3765. /**
  3766. * ice_stat_update32 - read 32 bit stat from the chip and update stat values
  3767. * @hw: ptr to the hardware info
  3768. * @reg: HW register to read from
  3769. * @prev_stat_loaded: bool to specify if previous stats are loaded
  3770. * @prev_stat: ptr to previous loaded stat value
  3771. * @cur_stat: ptr to current stat value
  3772. */
  3773. static void ice_stat_update32(struct ice_hw *hw, u32 reg, bool prev_stat_loaded,
  3774. u64 *prev_stat, u64 *cur_stat)
  3775. {
  3776. u32 new_data;
  3777. new_data = rd32(hw, reg);
  3778. /* device stats are not reset at PFR, they likely will not be zeroed
  3779. * when the driver starts. So save the first values read and use them as
  3780. * offsets to be subtracted from the raw values in order to report stats
  3781. * that count from zero.
  3782. */
  3783. if (!prev_stat_loaded)
  3784. *prev_stat = new_data;
  3785. if (likely(new_data >= *prev_stat))
  3786. *cur_stat = new_data - *prev_stat;
  3787. else
  3788. /* to manage the potential roll-over */
  3789. *cur_stat = (new_data + BIT_ULL(32)) - *prev_stat;
  3790. }
  3791. /**
  3792. * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
  3793. * @vsi: the VSI to be updated
  3794. */
  3795. static void ice_update_eth_stats(struct ice_vsi *vsi)
  3796. {
  3797. struct ice_eth_stats *prev_es, *cur_es;
  3798. struct ice_hw *hw = &vsi->back->hw;
  3799. u16 vsi_num = vsi->vsi_num; /* HW absolute index of a VSI */
  3800. prev_es = &vsi->eth_stats_prev;
  3801. cur_es = &vsi->eth_stats;
  3802. ice_stat_update40(hw, GLV_GORCH(vsi_num), GLV_GORCL(vsi_num),
  3803. vsi->stat_offsets_loaded, &prev_es->rx_bytes,
  3804. &cur_es->rx_bytes);
  3805. ice_stat_update40(hw, GLV_UPRCH(vsi_num), GLV_UPRCL(vsi_num),
  3806. vsi->stat_offsets_loaded, &prev_es->rx_unicast,
  3807. &cur_es->rx_unicast);
  3808. ice_stat_update40(hw, GLV_MPRCH(vsi_num), GLV_MPRCL(vsi_num),
  3809. vsi->stat_offsets_loaded, &prev_es->rx_multicast,
  3810. &cur_es->rx_multicast);
  3811. ice_stat_update40(hw, GLV_BPRCH(vsi_num), GLV_BPRCL(vsi_num),
  3812. vsi->stat_offsets_loaded, &prev_es->rx_broadcast,
  3813. &cur_es->rx_broadcast);
  3814. ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
  3815. &prev_es->rx_discards, &cur_es->rx_discards);
  3816. ice_stat_update40(hw, GLV_GOTCH(vsi_num), GLV_GOTCL(vsi_num),
  3817. vsi->stat_offsets_loaded, &prev_es->tx_bytes,
  3818. &cur_es->tx_bytes);
  3819. ice_stat_update40(hw, GLV_UPTCH(vsi_num), GLV_UPTCL(vsi_num),
  3820. vsi->stat_offsets_loaded, &prev_es->tx_unicast,
  3821. &cur_es->tx_unicast);
  3822. ice_stat_update40(hw, GLV_MPTCH(vsi_num), GLV_MPTCL(vsi_num),
  3823. vsi->stat_offsets_loaded, &prev_es->tx_multicast,
  3824. &cur_es->tx_multicast);
  3825. ice_stat_update40(hw, GLV_BPTCH(vsi_num), GLV_BPTCL(vsi_num),
  3826. vsi->stat_offsets_loaded, &prev_es->tx_broadcast,
  3827. &cur_es->tx_broadcast);
  3828. ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
  3829. &prev_es->tx_errors, &cur_es->tx_errors);
  3830. vsi->stat_offsets_loaded = true;
  3831. }
  3832. /**
  3833. * ice_update_vsi_ring_stats - Update VSI stats counters
  3834. * @vsi: the VSI to be updated
  3835. */
  3836. static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
  3837. {
  3838. struct rtnl_link_stats64 *vsi_stats = &vsi->net_stats;
  3839. struct ice_ring *ring;
  3840. u64 pkts, bytes;
  3841. int i;
  3842. /* reset netdev stats */
  3843. vsi_stats->tx_packets = 0;
  3844. vsi_stats->tx_bytes = 0;
  3845. vsi_stats->rx_packets = 0;
  3846. vsi_stats->rx_bytes = 0;
  3847. /* reset non-netdev (extended) stats */
  3848. vsi->tx_restart = 0;
  3849. vsi->tx_busy = 0;
  3850. vsi->tx_linearize = 0;
  3851. vsi->rx_buf_failed = 0;
  3852. vsi->rx_page_failed = 0;
  3853. rcu_read_lock();
  3854. /* update Tx rings counters */
  3855. ice_for_each_txq(vsi, i) {
  3856. ring = READ_ONCE(vsi->tx_rings[i]);
  3857. ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
  3858. vsi_stats->tx_packets += pkts;
  3859. vsi_stats->tx_bytes += bytes;
  3860. vsi->tx_restart += ring->tx_stats.restart_q;
  3861. vsi->tx_busy += ring->tx_stats.tx_busy;
  3862. vsi->tx_linearize += ring->tx_stats.tx_linearize;
  3863. }
  3864. /* update Rx rings counters */
  3865. ice_for_each_rxq(vsi, i) {
  3866. ring = READ_ONCE(vsi->rx_rings[i]);
  3867. ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
  3868. vsi_stats->rx_packets += pkts;
  3869. vsi_stats->rx_bytes += bytes;
  3870. vsi->rx_buf_failed += ring->rx_stats.alloc_buf_failed;
  3871. vsi->rx_page_failed += ring->rx_stats.alloc_page_failed;
  3872. }
  3873. rcu_read_unlock();
  3874. }
  3875. /**
  3876. * ice_update_vsi_stats - Update VSI stats counters
  3877. * @vsi: the VSI to be updated
  3878. */
  3879. static void ice_update_vsi_stats(struct ice_vsi *vsi)
  3880. {
  3881. struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
  3882. struct ice_eth_stats *cur_es = &vsi->eth_stats;
  3883. struct ice_pf *pf = vsi->back;
  3884. if (test_bit(__ICE_DOWN, vsi->state) ||
  3885. test_bit(__ICE_CFG_BUSY, pf->state))
  3886. return;
  3887. /* get stats as recorded by Tx/Rx rings */
  3888. ice_update_vsi_ring_stats(vsi);
  3889. /* get VSI stats as recorded by the hardware */
  3890. ice_update_eth_stats(vsi);
  3891. cur_ns->tx_errors = cur_es->tx_errors;
  3892. cur_ns->rx_dropped = cur_es->rx_discards;
  3893. cur_ns->tx_dropped = cur_es->tx_discards;
  3894. cur_ns->multicast = cur_es->rx_multicast;
  3895. /* update some more netdev stats if this is main VSI */
  3896. if (vsi->type == ICE_VSI_PF) {
  3897. cur_ns->rx_crc_errors = pf->stats.crc_errors;
  3898. cur_ns->rx_errors = pf->stats.crc_errors +
  3899. pf->stats.illegal_bytes;
  3900. cur_ns->rx_length_errors = pf->stats.rx_len_errors;
  3901. }
  3902. }
  3903. /**
  3904. * ice_update_pf_stats - Update PF port stats counters
  3905. * @pf: PF whose stats needs to be updated
  3906. */
  3907. static void ice_update_pf_stats(struct ice_pf *pf)
  3908. {
  3909. struct ice_hw_port_stats *prev_ps, *cur_ps;
  3910. struct ice_hw *hw = &pf->hw;
  3911. u8 pf_id;
  3912. prev_ps = &pf->stats_prev;
  3913. cur_ps = &pf->stats;
  3914. pf_id = hw->pf_id;
  3915. ice_stat_update40(hw, GLPRT_GORCH(pf_id), GLPRT_GORCL(pf_id),
  3916. pf->stat_prev_loaded, &prev_ps->eth.rx_bytes,
  3917. &cur_ps->eth.rx_bytes);
  3918. ice_stat_update40(hw, GLPRT_UPRCH(pf_id), GLPRT_UPRCL(pf_id),
  3919. pf->stat_prev_loaded, &prev_ps->eth.rx_unicast,
  3920. &cur_ps->eth.rx_unicast);
  3921. ice_stat_update40(hw, GLPRT_MPRCH(pf_id), GLPRT_MPRCL(pf_id),
  3922. pf->stat_prev_loaded, &prev_ps->eth.rx_multicast,
  3923. &cur_ps->eth.rx_multicast);
  3924. ice_stat_update40(hw, GLPRT_BPRCH(pf_id), GLPRT_BPRCL(pf_id),
  3925. pf->stat_prev_loaded, &prev_ps->eth.rx_broadcast,
  3926. &cur_ps->eth.rx_broadcast);
  3927. ice_stat_update40(hw, GLPRT_GOTCH(pf_id), GLPRT_GOTCL(pf_id),
  3928. pf->stat_prev_loaded, &prev_ps->eth.tx_bytes,
  3929. &cur_ps->eth.tx_bytes);
  3930. ice_stat_update40(hw, GLPRT_UPTCH(pf_id), GLPRT_UPTCL(pf_id),
  3931. pf->stat_prev_loaded, &prev_ps->eth.tx_unicast,
  3932. &cur_ps->eth.tx_unicast);
  3933. ice_stat_update40(hw, GLPRT_MPTCH(pf_id), GLPRT_MPTCL(pf_id),
  3934. pf->stat_prev_loaded, &prev_ps->eth.tx_multicast,
  3935. &cur_ps->eth.tx_multicast);
  3936. ice_stat_update40(hw, GLPRT_BPTCH(pf_id), GLPRT_BPTCL(pf_id),
  3937. pf->stat_prev_loaded, &prev_ps->eth.tx_broadcast,
  3938. &cur_ps->eth.tx_broadcast);
  3939. ice_stat_update32(hw, GLPRT_TDOLD(pf_id), pf->stat_prev_loaded,
  3940. &prev_ps->tx_dropped_link_down,
  3941. &cur_ps->tx_dropped_link_down);
  3942. ice_stat_update40(hw, GLPRT_PRC64H(pf_id), GLPRT_PRC64L(pf_id),
  3943. pf->stat_prev_loaded, &prev_ps->rx_size_64,
  3944. &cur_ps->rx_size_64);
  3945. ice_stat_update40(hw, GLPRT_PRC127H(pf_id), GLPRT_PRC127L(pf_id),
  3946. pf->stat_prev_loaded, &prev_ps->rx_size_127,
  3947. &cur_ps->rx_size_127);
  3948. ice_stat_update40(hw, GLPRT_PRC255H(pf_id), GLPRT_PRC255L(pf_id),
  3949. pf->stat_prev_loaded, &prev_ps->rx_size_255,
  3950. &cur_ps->rx_size_255);
  3951. ice_stat_update40(hw, GLPRT_PRC511H(pf_id), GLPRT_PRC511L(pf_id),
  3952. pf->stat_prev_loaded, &prev_ps->rx_size_511,
  3953. &cur_ps->rx_size_511);
  3954. ice_stat_update40(hw, GLPRT_PRC1023H(pf_id),
  3955. GLPRT_PRC1023L(pf_id), pf->stat_prev_loaded,
  3956. &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
  3957. ice_stat_update40(hw, GLPRT_PRC1522H(pf_id),
  3958. GLPRT_PRC1522L(pf_id), pf->stat_prev_loaded,
  3959. &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
  3960. ice_stat_update40(hw, GLPRT_PRC9522H(pf_id),
  3961. GLPRT_PRC9522L(pf_id), pf->stat_prev_loaded,
  3962. &prev_ps->rx_size_big, &cur_ps->rx_size_big);
  3963. ice_stat_update40(hw, GLPRT_PTC64H(pf_id), GLPRT_PTC64L(pf_id),
  3964. pf->stat_prev_loaded, &prev_ps->tx_size_64,
  3965. &cur_ps->tx_size_64);
  3966. ice_stat_update40(hw, GLPRT_PTC127H(pf_id), GLPRT_PTC127L(pf_id),
  3967. pf->stat_prev_loaded, &prev_ps->tx_size_127,
  3968. &cur_ps->tx_size_127);
  3969. ice_stat_update40(hw, GLPRT_PTC255H(pf_id), GLPRT_PTC255L(pf_id),
  3970. pf->stat_prev_loaded, &prev_ps->tx_size_255,
  3971. &cur_ps->tx_size_255);
  3972. ice_stat_update40(hw, GLPRT_PTC511H(pf_id), GLPRT_PTC511L(pf_id),
  3973. pf->stat_prev_loaded, &prev_ps->tx_size_511,
  3974. &cur_ps->tx_size_511);
  3975. ice_stat_update40(hw, GLPRT_PTC1023H(pf_id),
  3976. GLPRT_PTC1023L(pf_id), pf->stat_prev_loaded,
  3977. &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
  3978. ice_stat_update40(hw, GLPRT_PTC1522H(pf_id),
  3979. GLPRT_PTC1522L(pf_id), pf->stat_prev_loaded,
  3980. &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
  3981. ice_stat_update40(hw, GLPRT_PTC9522H(pf_id),
  3982. GLPRT_PTC9522L(pf_id), pf->stat_prev_loaded,
  3983. &prev_ps->tx_size_big, &cur_ps->tx_size_big);
  3984. ice_stat_update32(hw, GLPRT_LXONRXC(pf_id), pf->stat_prev_loaded,
  3985. &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
  3986. ice_stat_update32(hw, GLPRT_LXOFFRXC(pf_id), pf->stat_prev_loaded,
  3987. &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
  3988. ice_stat_update32(hw, GLPRT_LXONTXC(pf_id), pf->stat_prev_loaded,
  3989. &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
  3990. ice_stat_update32(hw, GLPRT_LXOFFTXC(pf_id), pf->stat_prev_loaded,
  3991. &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
  3992. ice_stat_update32(hw, GLPRT_CRCERRS(pf_id), pf->stat_prev_loaded,
  3993. &prev_ps->crc_errors, &cur_ps->crc_errors);
  3994. ice_stat_update32(hw, GLPRT_ILLERRC(pf_id), pf->stat_prev_loaded,
  3995. &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
  3996. ice_stat_update32(hw, GLPRT_MLFC(pf_id), pf->stat_prev_loaded,
  3997. &prev_ps->mac_local_faults,
  3998. &cur_ps->mac_local_faults);
  3999. ice_stat_update32(hw, GLPRT_MRFC(pf_id), pf->stat_prev_loaded,
  4000. &prev_ps->mac_remote_faults,
  4001. &cur_ps->mac_remote_faults);
  4002. ice_stat_update32(hw, GLPRT_RLEC(pf_id), pf->stat_prev_loaded,
  4003. &prev_ps->rx_len_errors, &cur_ps->rx_len_errors);
  4004. ice_stat_update32(hw, GLPRT_RUC(pf_id), pf->stat_prev_loaded,
  4005. &prev_ps->rx_undersize, &cur_ps->rx_undersize);
  4006. ice_stat_update32(hw, GLPRT_RFC(pf_id), pf->stat_prev_loaded,
  4007. &prev_ps->rx_fragments, &cur_ps->rx_fragments);
  4008. ice_stat_update32(hw, GLPRT_ROC(pf_id), pf->stat_prev_loaded,
  4009. &prev_ps->rx_oversize, &cur_ps->rx_oversize);
  4010. ice_stat_update32(hw, GLPRT_RJC(pf_id), pf->stat_prev_loaded,
  4011. &prev_ps->rx_jabber, &cur_ps->rx_jabber);
  4012. pf->stat_prev_loaded = true;
  4013. }
  4014. /**
  4015. * ice_get_stats64 - get statistics for network device structure
  4016. * @netdev: network interface device structure
  4017. * @stats: main device statistics structure
  4018. */
  4019. static
  4020. void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
  4021. {
  4022. struct ice_netdev_priv *np = netdev_priv(netdev);
  4023. struct rtnl_link_stats64 *vsi_stats;
  4024. struct ice_vsi *vsi = np->vsi;
  4025. vsi_stats = &vsi->net_stats;
  4026. if (test_bit(__ICE_DOWN, vsi->state) || !vsi->num_txq || !vsi->num_rxq)
  4027. return;
  4028. /* netdev packet/byte stats come from ring counter. These are obtained
  4029. * by summing up ring counters (done by ice_update_vsi_ring_stats).
  4030. */
  4031. ice_update_vsi_ring_stats(vsi);
  4032. stats->tx_packets = vsi_stats->tx_packets;
  4033. stats->tx_bytes = vsi_stats->tx_bytes;
  4034. stats->rx_packets = vsi_stats->rx_packets;
  4035. stats->rx_bytes = vsi_stats->rx_bytes;
  4036. /* The rest of the stats can be read from the hardware but instead we
  4037. * just return values that the watchdog task has already obtained from
  4038. * the hardware.
  4039. */
  4040. stats->multicast = vsi_stats->multicast;
  4041. stats->tx_errors = vsi_stats->tx_errors;
  4042. stats->tx_dropped = vsi_stats->tx_dropped;
  4043. stats->rx_errors = vsi_stats->rx_errors;
  4044. stats->rx_dropped = vsi_stats->rx_dropped;
  4045. stats->rx_crc_errors = vsi_stats->rx_crc_errors;
  4046. stats->rx_length_errors = vsi_stats->rx_length_errors;
  4047. }
  4048. #ifdef CONFIG_NET_POLL_CONTROLLER
  4049. /**
  4050. * ice_netpoll - polling "interrupt" handler
  4051. * @netdev: network interface device structure
  4052. *
  4053. * Used by netconsole to send skbs without having to re-enable interrupts.
  4054. * This is not called in the normal interrupt path.
  4055. */
  4056. static void ice_netpoll(struct net_device *netdev)
  4057. {
  4058. struct ice_netdev_priv *np = netdev_priv(netdev);
  4059. struct ice_vsi *vsi = np->vsi;
  4060. struct ice_pf *pf = vsi->back;
  4061. int i;
  4062. if (test_bit(__ICE_DOWN, vsi->state) ||
  4063. !test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
  4064. return;
  4065. for (i = 0; i < vsi->num_q_vectors; i++)
  4066. ice_msix_clean_rings(0, vsi->q_vectors[i]);
  4067. }
  4068. #endif /* CONFIG_NET_POLL_CONTROLLER */
  4069. /**
  4070. * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
  4071. * @vsi: VSI having NAPI disabled
  4072. */
  4073. static void ice_napi_disable_all(struct ice_vsi *vsi)
  4074. {
  4075. int q_idx;
  4076. if (!vsi->netdev)
  4077. return;
  4078. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
  4079. napi_disable(&vsi->q_vectors[q_idx]->napi);
  4080. }
  4081. /**
  4082. * ice_down - Shutdown the connection
  4083. * @vsi: The VSI being stopped
  4084. */
  4085. int ice_down(struct ice_vsi *vsi)
  4086. {
  4087. int i, err;
  4088. /* Caller of this function is expected to set the
  4089. * vsi->state __ICE_DOWN bit
  4090. */
  4091. if (vsi->netdev) {
  4092. netif_carrier_off(vsi->netdev);
  4093. netif_tx_disable(vsi->netdev);
  4094. }
  4095. ice_vsi_dis_irq(vsi);
  4096. err = ice_vsi_stop_tx_rx_rings(vsi);
  4097. ice_napi_disable_all(vsi);
  4098. ice_for_each_txq(vsi, i)
  4099. ice_clean_tx_ring(vsi->tx_rings[i]);
  4100. ice_for_each_rxq(vsi, i)
  4101. ice_clean_rx_ring(vsi->rx_rings[i]);
  4102. if (err)
  4103. netdev_err(vsi->netdev, "Failed to close VSI 0x%04X on switch 0x%04X\n",
  4104. vsi->vsi_num, vsi->vsw->sw_id);
  4105. return err;
  4106. }
  4107. /**
  4108. * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
  4109. * @vsi: VSI having resources allocated
  4110. *
  4111. * Return 0 on success, negative on failure
  4112. */
  4113. static int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
  4114. {
  4115. int i, err;
  4116. if (!vsi->num_txq) {
  4117. dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Tx queues\n",
  4118. vsi->vsi_num);
  4119. return -EINVAL;
  4120. }
  4121. ice_for_each_txq(vsi, i) {
  4122. err = ice_setup_tx_ring(vsi->tx_rings[i]);
  4123. if (err)
  4124. break;
  4125. }
  4126. return err;
  4127. }
  4128. /**
  4129. * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
  4130. * @vsi: VSI having resources allocated
  4131. *
  4132. * Return 0 on success, negative on failure
  4133. */
  4134. static int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
  4135. {
  4136. int i, err;
  4137. if (!vsi->num_rxq) {
  4138. dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Rx queues\n",
  4139. vsi->vsi_num);
  4140. return -EINVAL;
  4141. }
  4142. ice_for_each_rxq(vsi, i) {
  4143. err = ice_setup_rx_ring(vsi->rx_rings[i]);
  4144. if (err)
  4145. break;
  4146. }
  4147. return err;
  4148. }
  4149. /**
  4150. * ice_vsi_req_irq - Request IRQ from the OS
  4151. * @vsi: The VSI IRQ is being requested for
  4152. * @basename: name for the vector
  4153. *
  4154. * Return 0 on success and a negative value on error
  4155. */
  4156. static int ice_vsi_req_irq(struct ice_vsi *vsi, char *basename)
  4157. {
  4158. struct ice_pf *pf = vsi->back;
  4159. int err = -EINVAL;
  4160. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
  4161. err = ice_vsi_req_irq_msix(vsi, basename);
  4162. return err;
  4163. }
  4164. /**
  4165. * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
  4166. * @vsi: the VSI having resources freed
  4167. */
  4168. static void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
  4169. {
  4170. int i;
  4171. if (!vsi->tx_rings)
  4172. return;
  4173. ice_for_each_txq(vsi, i)
  4174. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
  4175. ice_free_tx_ring(vsi->tx_rings[i]);
  4176. }
  4177. /**
  4178. * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
  4179. * @vsi: the VSI having resources freed
  4180. */
  4181. static void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
  4182. {
  4183. int i;
  4184. if (!vsi->rx_rings)
  4185. return;
  4186. ice_for_each_rxq(vsi, i)
  4187. if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
  4188. ice_free_rx_ring(vsi->rx_rings[i]);
  4189. }
  4190. /**
  4191. * ice_vsi_open - Called when a network interface is made active
  4192. * @vsi: the VSI to open
  4193. *
  4194. * Initialization of the VSI
  4195. *
  4196. * Returns 0 on success, negative value on error
  4197. */
  4198. static int ice_vsi_open(struct ice_vsi *vsi)
  4199. {
  4200. char int_name[ICE_INT_NAME_STR_LEN];
  4201. struct ice_pf *pf = vsi->back;
  4202. int err;
  4203. /* allocate descriptors */
  4204. err = ice_vsi_setup_tx_rings(vsi);
  4205. if (err)
  4206. goto err_setup_tx;
  4207. err = ice_vsi_setup_rx_rings(vsi);
  4208. if (err)
  4209. goto err_setup_rx;
  4210. err = ice_vsi_cfg(vsi);
  4211. if (err)
  4212. goto err_setup_rx;
  4213. snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
  4214. dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
  4215. err = ice_vsi_req_irq(vsi, int_name);
  4216. if (err)
  4217. goto err_setup_rx;
  4218. /* Notify the stack of the actual queue counts. */
  4219. err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
  4220. if (err)
  4221. goto err_set_qs;
  4222. err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
  4223. if (err)
  4224. goto err_set_qs;
  4225. err = ice_up_complete(vsi);
  4226. if (err)
  4227. goto err_up_complete;
  4228. return 0;
  4229. err_up_complete:
  4230. ice_down(vsi);
  4231. err_set_qs:
  4232. ice_vsi_free_irq(vsi);
  4233. err_setup_rx:
  4234. ice_vsi_free_rx_rings(vsi);
  4235. err_setup_tx:
  4236. ice_vsi_free_tx_rings(vsi);
  4237. return err;
  4238. }
  4239. /**
  4240. * ice_vsi_close - Shut down a VSI
  4241. * @vsi: the VSI being shut down
  4242. */
  4243. static void ice_vsi_close(struct ice_vsi *vsi)
  4244. {
  4245. if (!test_and_set_bit(__ICE_DOWN, vsi->state))
  4246. ice_down(vsi);
  4247. ice_vsi_free_irq(vsi);
  4248. ice_vsi_free_tx_rings(vsi);
  4249. ice_vsi_free_rx_rings(vsi);
  4250. }
  4251. /**
  4252. * ice_rss_clean - Delete RSS related VSI structures that hold user inputs
  4253. * @vsi: the VSI being removed
  4254. */
  4255. static void ice_rss_clean(struct ice_vsi *vsi)
  4256. {
  4257. struct ice_pf *pf;
  4258. pf = vsi->back;
  4259. if (vsi->rss_hkey_user)
  4260. devm_kfree(&pf->pdev->dev, vsi->rss_hkey_user);
  4261. if (vsi->rss_lut_user)
  4262. devm_kfree(&pf->pdev->dev, vsi->rss_lut_user);
  4263. }
  4264. /**
  4265. * ice_vsi_release - Delete a VSI and free its resources
  4266. * @vsi: the VSI being removed
  4267. *
  4268. * Returns 0 on success or < 0 on error
  4269. */
  4270. static int ice_vsi_release(struct ice_vsi *vsi)
  4271. {
  4272. struct ice_pf *pf;
  4273. if (!vsi->back)
  4274. return -ENODEV;
  4275. pf = vsi->back;
  4276. if (vsi->netdev) {
  4277. unregister_netdev(vsi->netdev);
  4278. free_netdev(vsi->netdev);
  4279. vsi->netdev = NULL;
  4280. }
  4281. if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
  4282. ice_rss_clean(vsi);
  4283. /* Disable VSI and free resources */
  4284. ice_vsi_dis_irq(vsi);
  4285. ice_vsi_close(vsi);
  4286. /* reclaim interrupt vectors back to PF */
  4287. ice_free_res(vsi->back->irq_tracker, vsi->base_vector, vsi->idx);
  4288. pf->num_avail_msix += vsi->num_q_vectors;
  4289. ice_remove_vsi_fltr(&pf->hw, vsi->vsi_num);
  4290. ice_vsi_delete(vsi);
  4291. ice_vsi_free_q_vectors(vsi);
  4292. ice_vsi_clear_rings(vsi);
  4293. ice_vsi_put_qs(vsi);
  4294. pf->q_left_tx += vsi->alloc_txq;
  4295. pf->q_left_rx += vsi->alloc_rxq;
  4296. ice_vsi_clear(vsi);
  4297. return 0;
  4298. }
  4299. /**
  4300. * ice_dis_vsi - pause a VSI
  4301. * @vsi: the VSI being paused
  4302. */
  4303. static void ice_dis_vsi(struct ice_vsi *vsi)
  4304. {
  4305. if (test_bit(__ICE_DOWN, vsi->state))
  4306. return;
  4307. set_bit(__ICE_NEEDS_RESTART, vsi->state);
  4308. if (vsi->netdev && netif_running(vsi->netdev) &&
  4309. vsi->type == ICE_VSI_PF)
  4310. vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
  4311. ice_vsi_close(vsi);
  4312. }
  4313. /**
  4314. * ice_ena_vsi - resume a VSI
  4315. * @vsi: the VSI being resume
  4316. */
  4317. static void ice_ena_vsi(struct ice_vsi *vsi)
  4318. {
  4319. if (!test_and_clear_bit(__ICE_NEEDS_RESTART, vsi->state))
  4320. return;
  4321. if (vsi->netdev && netif_running(vsi->netdev))
  4322. vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
  4323. else if (ice_vsi_open(vsi))
  4324. /* this clears the DOWN bit */
  4325. dev_dbg(&vsi->back->pdev->dev, "Failed open VSI 0x%04X on switch 0x%04X\n",
  4326. vsi->vsi_num, vsi->vsw->sw_id);
  4327. }
  4328. /**
  4329. * ice_pf_dis_all_vsi - Pause all VSIs on a PF
  4330. * @pf: the PF
  4331. */
  4332. static void ice_pf_dis_all_vsi(struct ice_pf *pf)
  4333. {
  4334. int v;
  4335. ice_for_each_vsi(pf, v)
  4336. if (pf->vsi[v])
  4337. ice_dis_vsi(pf->vsi[v]);
  4338. }
  4339. /**
  4340. * ice_pf_ena_all_vsi - Resume all VSIs on a PF
  4341. * @pf: the PF
  4342. */
  4343. static void ice_pf_ena_all_vsi(struct ice_pf *pf)
  4344. {
  4345. int v;
  4346. ice_for_each_vsi(pf, v)
  4347. if (pf->vsi[v])
  4348. ice_ena_vsi(pf->vsi[v]);
  4349. }
  4350. /**
  4351. * ice_rebuild - rebuild after reset
  4352. * @pf: pf to rebuild
  4353. */
  4354. static void ice_rebuild(struct ice_pf *pf)
  4355. {
  4356. struct device *dev = &pf->pdev->dev;
  4357. struct ice_hw *hw = &pf->hw;
  4358. enum ice_status ret;
  4359. int err;
  4360. if (test_bit(__ICE_DOWN, pf->state))
  4361. goto clear_recovery;
  4362. dev_dbg(dev, "rebuilding pf\n");
  4363. ret = ice_init_all_ctrlq(hw);
  4364. if (ret) {
  4365. dev_err(dev, "control queues init failed %d\n", ret);
  4366. goto fail_reset;
  4367. }
  4368. ret = ice_clear_pf_cfg(hw);
  4369. if (ret) {
  4370. dev_err(dev, "clear PF configuration failed %d\n", ret);
  4371. goto fail_reset;
  4372. }
  4373. ice_clear_pxe_mode(hw);
  4374. ret = ice_get_caps(hw);
  4375. if (ret) {
  4376. dev_err(dev, "ice_get_caps failed %d\n", ret);
  4377. goto fail_reset;
  4378. }
  4379. /* basic nic switch setup */
  4380. err = ice_setup_pf_sw(pf);
  4381. if (err) {
  4382. dev_err(dev, "ice_setup_pf_sw failed\n");
  4383. goto fail_reset;
  4384. }
  4385. /* start misc vector */
  4386. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
  4387. err = ice_req_irq_msix_misc(pf);
  4388. if (err) {
  4389. dev_err(dev, "misc vector setup failed: %d\n", err);
  4390. goto fail_reset;
  4391. }
  4392. }
  4393. /* restart the VSIs that were rebuilt and running before the reset */
  4394. ice_pf_ena_all_vsi(pf);
  4395. return;
  4396. fail_reset:
  4397. ice_shutdown_all_ctrlq(hw);
  4398. set_bit(__ICE_RESET_FAILED, pf->state);
  4399. clear_recovery:
  4400. set_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
  4401. }
  4402. /**
  4403. * ice_change_mtu - NDO callback to change the MTU
  4404. * @netdev: network interface device structure
  4405. * @new_mtu: new value for maximum frame size
  4406. *
  4407. * Returns 0 on success, negative on failure
  4408. */
  4409. static int ice_change_mtu(struct net_device *netdev, int new_mtu)
  4410. {
  4411. struct ice_netdev_priv *np = netdev_priv(netdev);
  4412. struct ice_vsi *vsi = np->vsi;
  4413. struct ice_pf *pf = vsi->back;
  4414. u8 count = 0;
  4415. if (new_mtu == netdev->mtu) {
  4416. netdev_warn(netdev, "mtu is already %d\n", netdev->mtu);
  4417. return 0;
  4418. }
  4419. if (new_mtu < netdev->min_mtu) {
  4420. netdev_err(netdev, "new mtu invalid. min_mtu is %d\n",
  4421. netdev->min_mtu);
  4422. return -EINVAL;
  4423. } else if (new_mtu > netdev->max_mtu) {
  4424. netdev_err(netdev, "new mtu invalid. max_mtu is %d\n",
  4425. netdev->min_mtu);
  4426. return -EINVAL;
  4427. }
  4428. /* if a reset is in progress, wait for some time for it to complete */
  4429. do {
  4430. if (ice_is_reset_recovery_pending(pf->state)) {
  4431. count++;
  4432. usleep_range(1000, 2000);
  4433. } else {
  4434. break;
  4435. }
  4436. } while (count < 100);
  4437. if (count == 100) {
  4438. netdev_err(netdev, "can't change mtu. Device is busy\n");
  4439. return -EBUSY;
  4440. }
  4441. netdev->mtu = new_mtu;
  4442. /* if VSI is up, bring it down and then back up */
  4443. if (!test_and_set_bit(__ICE_DOWN, vsi->state)) {
  4444. int err;
  4445. err = ice_down(vsi);
  4446. if (err) {
  4447. netdev_err(netdev, "change mtu if_up err %d\n", err);
  4448. return err;
  4449. }
  4450. err = ice_up(vsi);
  4451. if (err) {
  4452. netdev_err(netdev, "change mtu if_up err %d\n", err);
  4453. return err;
  4454. }
  4455. }
  4456. netdev_dbg(netdev, "changed mtu to %d\n", new_mtu);
  4457. return 0;
  4458. }
  4459. /**
  4460. * ice_set_rss - Set RSS keys and lut
  4461. * @vsi: Pointer to VSI structure
  4462. * @seed: RSS hash seed
  4463. * @lut: Lookup table
  4464. * @lut_size: Lookup table size
  4465. *
  4466. * Returns 0 on success, negative on failure
  4467. */
  4468. int ice_set_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  4469. {
  4470. struct ice_pf *pf = vsi->back;
  4471. struct ice_hw *hw = &pf->hw;
  4472. enum ice_status status;
  4473. if (seed) {
  4474. struct ice_aqc_get_set_rss_keys *buf =
  4475. (struct ice_aqc_get_set_rss_keys *)seed;
  4476. status = ice_aq_set_rss_key(hw, vsi->vsi_num, buf);
  4477. if (status) {
  4478. dev_err(&pf->pdev->dev,
  4479. "Cannot set RSS key, err %d aq_err %d\n",
  4480. status, hw->adminq.rq_last_status);
  4481. return -EIO;
  4482. }
  4483. }
  4484. if (lut) {
  4485. status = ice_aq_set_rss_lut(hw, vsi->vsi_num,
  4486. vsi->rss_lut_type, lut, lut_size);
  4487. if (status) {
  4488. dev_err(&pf->pdev->dev,
  4489. "Cannot set RSS lut, err %d aq_err %d\n",
  4490. status, hw->adminq.rq_last_status);
  4491. return -EIO;
  4492. }
  4493. }
  4494. return 0;
  4495. }
  4496. /**
  4497. * ice_get_rss - Get RSS keys and lut
  4498. * @vsi: Pointer to VSI structure
  4499. * @seed: Buffer to store the keys
  4500. * @lut: Buffer to store the lookup table entries
  4501. * @lut_size: Size of buffer to store the lookup table entries
  4502. *
  4503. * Returns 0 on success, negative on failure
  4504. */
  4505. int ice_get_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  4506. {
  4507. struct ice_pf *pf = vsi->back;
  4508. struct ice_hw *hw = &pf->hw;
  4509. enum ice_status status;
  4510. if (seed) {
  4511. struct ice_aqc_get_set_rss_keys *buf =
  4512. (struct ice_aqc_get_set_rss_keys *)seed;
  4513. status = ice_aq_get_rss_key(hw, vsi->vsi_num, buf);
  4514. if (status) {
  4515. dev_err(&pf->pdev->dev,
  4516. "Cannot get RSS key, err %d aq_err %d\n",
  4517. status, hw->adminq.rq_last_status);
  4518. return -EIO;
  4519. }
  4520. }
  4521. if (lut) {
  4522. status = ice_aq_get_rss_lut(hw, vsi->vsi_num,
  4523. vsi->rss_lut_type, lut, lut_size);
  4524. if (status) {
  4525. dev_err(&pf->pdev->dev,
  4526. "Cannot get RSS lut, err %d aq_err %d\n",
  4527. status, hw->adminq.rq_last_status);
  4528. return -EIO;
  4529. }
  4530. }
  4531. return 0;
  4532. }
  4533. /**
  4534. * ice_open - Called when a network interface becomes active
  4535. * @netdev: network interface device structure
  4536. *
  4537. * The open entry point is called when a network interface is made
  4538. * active by the system (IFF_UP). At this point all resources needed
  4539. * for transmit and receive operations are allocated, the interrupt
  4540. * handler is registered with the OS, the netdev watchdog is enabled,
  4541. * and the stack is notified that the interface is ready.
  4542. *
  4543. * Returns 0 on success, negative value on failure
  4544. */
  4545. static int ice_open(struct net_device *netdev)
  4546. {
  4547. struct ice_netdev_priv *np = netdev_priv(netdev);
  4548. struct ice_vsi *vsi = np->vsi;
  4549. int err;
  4550. netif_carrier_off(netdev);
  4551. err = ice_vsi_open(vsi);
  4552. if (err)
  4553. netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
  4554. vsi->vsi_num, vsi->vsw->sw_id);
  4555. return err;
  4556. }
  4557. /**
  4558. * ice_stop - Disables a network interface
  4559. * @netdev: network interface device structure
  4560. *
  4561. * The stop entry point is called when an interface is de-activated by the OS,
  4562. * and the netdevice enters the DOWN state. The hardware is still under the
  4563. * driver's control, but the netdev interface is disabled.
  4564. *
  4565. * Returns success only - not allowed to fail
  4566. */
  4567. static int ice_stop(struct net_device *netdev)
  4568. {
  4569. struct ice_netdev_priv *np = netdev_priv(netdev);
  4570. struct ice_vsi *vsi = np->vsi;
  4571. ice_vsi_close(vsi);
  4572. return 0;
  4573. }
  4574. /**
  4575. * ice_features_check - Validate encapsulated packet conforms to limits
  4576. * @skb: skb buffer
  4577. * @netdev: This port's netdev
  4578. * @features: Offload features that the stack believes apply
  4579. */
  4580. static netdev_features_t
  4581. ice_features_check(struct sk_buff *skb,
  4582. struct net_device __always_unused *netdev,
  4583. netdev_features_t features)
  4584. {
  4585. size_t len;
  4586. /* No point in doing any of this if neither checksum nor GSO are
  4587. * being requested for this frame. We can rule out both by just
  4588. * checking for CHECKSUM_PARTIAL
  4589. */
  4590. if (skb->ip_summed != CHECKSUM_PARTIAL)
  4591. return features;
  4592. /* We cannot support GSO if the MSS is going to be less than
  4593. * 64 bytes. If it is then we need to drop support for GSO.
  4594. */
  4595. if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
  4596. features &= ~NETIF_F_GSO_MASK;
  4597. len = skb_network_header(skb) - skb->data;
  4598. if (len & ~(ICE_TXD_MACLEN_MAX))
  4599. goto out_rm_features;
  4600. len = skb_transport_header(skb) - skb_network_header(skb);
  4601. if (len & ~(ICE_TXD_IPLEN_MAX))
  4602. goto out_rm_features;
  4603. if (skb->encapsulation) {
  4604. len = skb_inner_network_header(skb) - skb_transport_header(skb);
  4605. if (len & ~(ICE_TXD_L4LEN_MAX))
  4606. goto out_rm_features;
  4607. len = skb_inner_transport_header(skb) -
  4608. skb_inner_network_header(skb);
  4609. if (len & ~(ICE_TXD_IPLEN_MAX))
  4610. goto out_rm_features;
  4611. }
  4612. return features;
  4613. out_rm_features:
  4614. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  4615. }
  4616. static const struct net_device_ops ice_netdev_ops = {
  4617. .ndo_open = ice_open,
  4618. .ndo_stop = ice_stop,
  4619. .ndo_start_xmit = ice_start_xmit,
  4620. .ndo_features_check = ice_features_check,
  4621. .ndo_set_rx_mode = ice_set_rx_mode,
  4622. .ndo_set_mac_address = ice_set_mac_address,
  4623. .ndo_validate_addr = eth_validate_addr,
  4624. .ndo_change_mtu = ice_change_mtu,
  4625. .ndo_get_stats64 = ice_get_stats64,
  4626. #ifdef CONFIG_NET_POLL_CONTROLLER
  4627. .ndo_poll_controller = ice_netpoll,
  4628. #endif /* CONFIG_NET_POLL_CONTROLLER */
  4629. .ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
  4630. .ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
  4631. .ndo_set_features = ice_set_features,
  4632. .ndo_fdb_add = ice_fdb_add,
  4633. .ndo_fdb_del = ice_fdb_del,
  4634. };