bnx2x_cmn.c 136 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112
  1. /* bnx2x_cmn.c: QLogic Everest network driver.
  2. *
  3. * Copyright (c) 2007-2013 Broadcom Corporation
  4. * Copyright (c) 2014 QLogic Corporation
  5. * All rights reserved
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation.
  10. *
  11. * Maintained by: Ariel Elior <ariel.elior@qlogic.com>
  12. * Written by: Eliezer Tamir
  13. * Based on code from Michael Chan's bnx2 driver
  14. * UDP CSUM errata workaround by Arik Gendelman
  15. * Slowpath and fastpath rework by Vladislav Zolotarov
  16. * Statistics and Link management by Yitchak Gertner
  17. *
  18. */
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/etherdevice.h>
  21. #include <linux/if_vlan.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/ip.h>
  24. #include <linux/crash_dump.h>
  25. #include <net/tcp.h>
  26. #include <net/ipv6.h>
  27. #include <net/ip6_checksum.h>
  28. #include <net/busy_poll.h>
  29. #include <linux/prefetch.h>
  30. #include "bnx2x_cmn.h"
  31. #include "bnx2x_init.h"
  32. #include "bnx2x_sp.h"
  33. static void bnx2x_free_fp_mem_cnic(struct bnx2x *bp);
  34. static int bnx2x_alloc_fp_mem_cnic(struct bnx2x *bp);
  35. static int bnx2x_alloc_fp_mem(struct bnx2x *bp);
  36. static int bnx2x_poll(struct napi_struct *napi, int budget);
  37. static void bnx2x_add_all_napi_cnic(struct bnx2x *bp)
  38. {
  39. int i;
  40. /* Add NAPI objects */
  41. for_each_rx_queue_cnic(bp, i) {
  42. netif_napi_add(bp->dev, &bnx2x_fp(bp, i, napi),
  43. bnx2x_poll, NAPI_POLL_WEIGHT);
  44. }
  45. }
  46. static void bnx2x_add_all_napi(struct bnx2x *bp)
  47. {
  48. int i;
  49. /* Add NAPI objects */
  50. for_each_eth_queue(bp, i) {
  51. netif_napi_add(bp->dev, &bnx2x_fp(bp, i, napi),
  52. bnx2x_poll, NAPI_POLL_WEIGHT);
  53. }
  54. }
  55. static int bnx2x_calc_num_queues(struct bnx2x *bp)
  56. {
  57. int nq = bnx2x_num_queues ? : netif_get_num_default_rss_queues();
  58. /* Reduce memory usage in kdump environment by using only one queue */
  59. if (is_kdump_kernel())
  60. nq = 1;
  61. nq = clamp(nq, 1, BNX2X_MAX_QUEUES(bp));
  62. return nq;
  63. }
  64. /**
  65. * bnx2x_move_fp - move content of the fastpath structure.
  66. *
  67. * @bp: driver handle
  68. * @from: source FP index
  69. * @to: destination FP index
  70. *
  71. * Makes sure the contents of the bp->fp[to].napi is kept
  72. * intact. This is done by first copying the napi struct from
  73. * the target to the source, and then mem copying the entire
  74. * source onto the target. Update txdata pointers and related
  75. * content.
  76. */
  77. static inline void bnx2x_move_fp(struct bnx2x *bp, int from, int to)
  78. {
  79. struct bnx2x_fastpath *from_fp = &bp->fp[from];
  80. struct bnx2x_fastpath *to_fp = &bp->fp[to];
  81. struct bnx2x_sp_objs *from_sp_objs = &bp->sp_objs[from];
  82. struct bnx2x_sp_objs *to_sp_objs = &bp->sp_objs[to];
  83. struct bnx2x_fp_stats *from_fp_stats = &bp->fp_stats[from];
  84. struct bnx2x_fp_stats *to_fp_stats = &bp->fp_stats[to];
  85. int old_max_eth_txqs, new_max_eth_txqs;
  86. int old_txdata_index = 0, new_txdata_index = 0;
  87. struct bnx2x_agg_info *old_tpa_info = to_fp->tpa_info;
  88. /* Copy the NAPI object as it has been already initialized */
  89. from_fp->napi = to_fp->napi;
  90. /* Move bnx2x_fastpath contents */
  91. memcpy(to_fp, from_fp, sizeof(*to_fp));
  92. to_fp->index = to;
  93. /* Retain the tpa_info of the original `to' version as we don't want
  94. * 2 FPs to contain the same tpa_info pointer.
  95. */
  96. to_fp->tpa_info = old_tpa_info;
  97. /* move sp_objs contents as well, as their indices match fp ones */
  98. memcpy(to_sp_objs, from_sp_objs, sizeof(*to_sp_objs));
  99. /* move fp_stats contents as well, as their indices match fp ones */
  100. memcpy(to_fp_stats, from_fp_stats, sizeof(*to_fp_stats));
  101. /* Update txdata pointers in fp and move txdata content accordingly:
  102. * Each fp consumes 'max_cos' txdata structures, so the index should be
  103. * decremented by max_cos x delta.
  104. */
  105. old_max_eth_txqs = BNX2X_NUM_ETH_QUEUES(bp) * (bp)->max_cos;
  106. new_max_eth_txqs = (BNX2X_NUM_ETH_QUEUES(bp) - from + to) *
  107. (bp)->max_cos;
  108. if (from == FCOE_IDX(bp)) {
  109. old_txdata_index = old_max_eth_txqs + FCOE_TXQ_IDX_OFFSET;
  110. new_txdata_index = new_max_eth_txqs + FCOE_TXQ_IDX_OFFSET;
  111. }
  112. memcpy(&bp->bnx2x_txq[new_txdata_index],
  113. &bp->bnx2x_txq[old_txdata_index],
  114. sizeof(struct bnx2x_fp_txdata));
  115. to_fp->txdata_ptr[0] = &bp->bnx2x_txq[new_txdata_index];
  116. }
  117. /**
  118. * bnx2x_fill_fw_str - Fill buffer with FW version string.
  119. *
  120. * @bp: driver handle
  121. * @buf: character buffer to fill with the fw name
  122. * @buf_len: length of the above buffer
  123. *
  124. */
  125. void bnx2x_fill_fw_str(struct bnx2x *bp, char *buf, size_t buf_len)
  126. {
  127. if (IS_PF(bp)) {
  128. u8 phy_fw_ver[PHY_FW_VER_LEN];
  129. phy_fw_ver[0] = '\0';
  130. bnx2x_get_ext_phy_fw_version(&bp->link_params,
  131. phy_fw_ver, PHY_FW_VER_LEN);
  132. strlcpy(buf, bp->fw_ver, buf_len);
  133. snprintf(buf + strlen(bp->fw_ver), 32 - strlen(bp->fw_ver),
  134. "bc %d.%d.%d%s%s",
  135. (bp->common.bc_ver & 0xff0000) >> 16,
  136. (bp->common.bc_ver & 0xff00) >> 8,
  137. (bp->common.bc_ver & 0xff),
  138. ((phy_fw_ver[0] != '\0') ? " phy " : ""), phy_fw_ver);
  139. } else {
  140. bnx2x_vf_fill_fw_str(bp, buf, buf_len);
  141. }
  142. }
  143. /**
  144. * bnx2x_shrink_eth_fp - guarantees fastpath structures stay intact
  145. *
  146. * @bp: driver handle
  147. * @delta: number of eth queues which were not allocated
  148. */
  149. static void bnx2x_shrink_eth_fp(struct bnx2x *bp, int delta)
  150. {
  151. int i, cos, old_eth_num = BNX2X_NUM_ETH_QUEUES(bp);
  152. /* Queue pointer cannot be re-set on an fp-basis, as moving pointer
  153. * backward along the array could cause memory to be overridden
  154. */
  155. for (cos = 1; cos < bp->max_cos; cos++) {
  156. for (i = 0; i < old_eth_num - delta; i++) {
  157. struct bnx2x_fastpath *fp = &bp->fp[i];
  158. int new_idx = cos * (old_eth_num - delta) + i;
  159. memcpy(&bp->bnx2x_txq[new_idx], fp->txdata_ptr[cos],
  160. sizeof(struct bnx2x_fp_txdata));
  161. fp->txdata_ptr[cos] = &bp->bnx2x_txq[new_idx];
  162. }
  163. }
  164. }
  165. int bnx2x_load_count[2][3] = { {0} }; /* per-path: 0-common, 1-port0, 2-port1 */
  166. /* free skb in the packet ring at pos idx
  167. * return idx of last bd freed
  168. */
  169. static u16 bnx2x_free_tx_pkt(struct bnx2x *bp, struct bnx2x_fp_txdata *txdata,
  170. u16 idx, unsigned int *pkts_compl,
  171. unsigned int *bytes_compl)
  172. {
  173. struct sw_tx_bd *tx_buf = &txdata->tx_buf_ring[idx];
  174. struct eth_tx_start_bd *tx_start_bd;
  175. struct eth_tx_bd *tx_data_bd;
  176. struct sk_buff *skb = tx_buf->skb;
  177. u16 bd_idx = TX_BD(tx_buf->first_bd), new_cons;
  178. int nbd;
  179. u16 split_bd_len = 0;
  180. /* prefetch skb end pointer to speedup dev_kfree_skb() */
  181. prefetch(&skb->end);
  182. DP(NETIF_MSG_TX_DONE, "fp[%d]: pkt_idx %d buff @(%p)->skb %p\n",
  183. txdata->txq_index, idx, tx_buf, skb);
  184. tx_start_bd = &txdata->tx_desc_ring[bd_idx].start_bd;
  185. nbd = le16_to_cpu(tx_start_bd->nbd) - 1;
  186. #ifdef BNX2X_STOP_ON_ERROR
  187. if ((nbd - 1) > (MAX_SKB_FRAGS + 2)) {
  188. BNX2X_ERR("BAD nbd!\n");
  189. bnx2x_panic();
  190. }
  191. #endif
  192. new_cons = nbd + tx_buf->first_bd;
  193. /* Get the next bd */
  194. bd_idx = TX_BD(NEXT_TX_IDX(bd_idx));
  195. /* Skip a parse bd... */
  196. --nbd;
  197. bd_idx = TX_BD(NEXT_TX_IDX(bd_idx));
  198. if (tx_buf->flags & BNX2X_HAS_SECOND_PBD) {
  199. /* Skip second parse bd... */
  200. --nbd;
  201. bd_idx = TX_BD(NEXT_TX_IDX(bd_idx));
  202. }
  203. /* TSO headers+data bds share a common mapping. See bnx2x_tx_split() */
  204. if (tx_buf->flags & BNX2X_TSO_SPLIT_BD) {
  205. tx_data_bd = &txdata->tx_desc_ring[bd_idx].reg_bd;
  206. split_bd_len = BD_UNMAP_LEN(tx_data_bd);
  207. --nbd;
  208. bd_idx = TX_BD(NEXT_TX_IDX(bd_idx));
  209. }
  210. /* unmap first bd */
  211. dma_unmap_single(&bp->pdev->dev, BD_UNMAP_ADDR(tx_start_bd),
  212. BD_UNMAP_LEN(tx_start_bd) + split_bd_len,
  213. DMA_TO_DEVICE);
  214. /* now free frags */
  215. while (nbd > 0) {
  216. tx_data_bd = &txdata->tx_desc_ring[bd_idx].reg_bd;
  217. dma_unmap_page(&bp->pdev->dev, BD_UNMAP_ADDR(tx_data_bd),
  218. BD_UNMAP_LEN(tx_data_bd), DMA_TO_DEVICE);
  219. if (--nbd)
  220. bd_idx = TX_BD(NEXT_TX_IDX(bd_idx));
  221. }
  222. /* release skb */
  223. WARN_ON(!skb);
  224. if (likely(skb)) {
  225. (*pkts_compl)++;
  226. (*bytes_compl) += skb->len;
  227. dev_kfree_skb_any(skb);
  228. }
  229. tx_buf->first_bd = 0;
  230. tx_buf->skb = NULL;
  231. return new_cons;
  232. }
  233. int bnx2x_tx_int(struct bnx2x *bp, struct bnx2x_fp_txdata *txdata)
  234. {
  235. struct netdev_queue *txq;
  236. u16 hw_cons, sw_cons, bd_cons = txdata->tx_bd_cons;
  237. unsigned int pkts_compl = 0, bytes_compl = 0;
  238. #ifdef BNX2X_STOP_ON_ERROR
  239. if (unlikely(bp->panic))
  240. return -1;
  241. #endif
  242. txq = netdev_get_tx_queue(bp->dev, txdata->txq_index);
  243. hw_cons = le16_to_cpu(*txdata->tx_cons_sb);
  244. sw_cons = txdata->tx_pkt_cons;
  245. while (sw_cons != hw_cons) {
  246. u16 pkt_cons;
  247. pkt_cons = TX_BD(sw_cons);
  248. DP(NETIF_MSG_TX_DONE,
  249. "queue[%d]: hw_cons %u sw_cons %u pkt_cons %u\n",
  250. txdata->txq_index, hw_cons, sw_cons, pkt_cons);
  251. bd_cons = bnx2x_free_tx_pkt(bp, txdata, pkt_cons,
  252. &pkts_compl, &bytes_compl);
  253. sw_cons++;
  254. }
  255. netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
  256. txdata->tx_pkt_cons = sw_cons;
  257. txdata->tx_bd_cons = bd_cons;
  258. /* Need to make the tx_bd_cons update visible to start_xmit()
  259. * before checking for netif_tx_queue_stopped(). Without the
  260. * memory barrier, there is a small possibility that
  261. * start_xmit() will miss it and cause the queue to be stopped
  262. * forever.
  263. * On the other hand we need an rmb() here to ensure the proper
  264. * ordering of bit testing in the following
  265. * netif_tx_queue_stopped(txq) call.
  266. */
  267. smp_mb();
  268. if (unlikely(netif_tx_queue_stopped(txq))) {
  269. /* Taking tx_lock() is needed to prevent re-enabling the queue
  270. * while it's empty. This could have happen if rx_action() gets
  271. * suspended in bnx2x_tx_int() after the condition before
  272. * netif_tx_wake_queue(), while tx_action (bnx2x_start_xmit()):
  273. *
  274. * stops the queue->sees fresh tx_bd_cons->releases the queue->
  275. * sends some packets consuming the whole queue again->
  276. * stops the queue
  277. */
  278. __netif_tx_lock(txq, smp_processor_id());
  279. if ((netif_tx_queue_stopped(txq)) &&
  280. (bp->state == BNX2X_STATE_OPEN) &&
  281. (bnx2x_tx_avail(bp, txdata) >= MAX_DESC_PER_TX_PKT))
  282. netif_tx_wake_queue(txq);
  283. __netif_tx_unlock(txq);
  284. }
  285. return 0;
  286. }
  287. static inline void bnx2x_update_last_max_sge(struct bnx2x_fastpath *fp,
  288. u16 idx)
  289. {
  290. u16 last_max = fp->last_max_sge;
  291. if (SUB_S16(idx, last_max) > 0)
  292. fp->last_max_sge = idx;
  293. }
  294. static inline void bnx2x_update_sge_prod(struct bnx2x_fastpath *fp,
  295. u16 sge_len,
  296. struct eth_end_agg_rx_cqe *cqe)
  297. {
  298. struct bnx2x *bp = fp->bp;
  299. u16 last_max, last_elem, first_elem;
  300. u16 delta = 0;
  301. u16 i;
  302. if (!sge_len)
  303. return;
  304. /* First mark all used pages */
  305. for (i = 0; i < sge_len; i++)
  306. BIT_VEC64_CLEAR_BIT(fp->sge_mask,
  307. RX_SGE(le16_to_cpu(cqe->sgl_or_raw_data.sgl[i])));
  308. DP(NETIF_MSG_RX_STATUS, "fp_cqe->sgl[%d] = %d\n",
  309. sge_len - 1, le16_to_cpu(cqe->sgl_or_raw_data.sgl[sge_len - 1]));
  310. /* Here we assume that the last SGE index is the biggest */
  311. prefetch((void *)(fp->sge_mask));
  312. bnx2x_update_last_max_sge(fp,
  313. le16_to_cpu(cqe->sgl_or_raw_data.sgl[sge_len - 1]));
  314. last_max = RX_SGE(fp->last_max_sge);
  315. last_elem = last_max >> BIT_VEC64_ELEM_SHIFT;
  316. first_elem = RX_SGE(fp->rx_sge_prod) >> BIT_VEC64_ELEM_SHIFT;
  317. /* If ring is not full */
  318. if (last_elem + 1 != first_elem)
  319. last_elem++;
  320. /* Now update the prod */
  321. for (i = first_elem; i != last_elem; i = NEXT_SGE_MASK_ELEM(i)) {
  322. if (likely(fp->sge_mask[i]))
  323. break;
  324. fp->sge_mask[i] = BIT_VEC64_ELEM_ONE_MASK;
  325. delta += BIT_VEC64_ELEM_SZ;
  326. }
  327. if (delta > 0) {
  328. fp->rx_sge_prod += delta;
  329. /* clear page-end entries */
  330. bnx2x_clear_sge_mask_next_elems(fp);
  331. }
  332. DP(NETIF_MSG_RX_STATUS,
  333. "fp->last_max_sge = %d fp->rx_sge_prod = %d\n",
  334. fp->last_max_sge, fp->rx_sge_prod);
  335. }
  336. /* Get Toeplitz hash value in the skb using the value from the
  337. * CQE (calculated by HW).
  338. */
  339. static u32 bnx2x_get_rxhash(const struct bnx2x *bp,
  340. const struct eth_fast_path_rx_cqe *cqe,
  341. enum pkt_hash_types *rxhash_type)
  342. {
  343. /* Get Toeplitz hash from CQE */
  344. if ((bp->dev->features & NETIF_F_RXHASH) &&
  345. (cqe->status_flags & ETH_FAST_PATH_RX_CQE_RSS_HASH_FLG)) {
  346. enum eth_rss_hash_type htype;
  347. htype = cqe->status_flags & ETH_FAST_PATH_RX_CQE_RSS_HASH_TYPE;
  348. *rxhash_type = ((htype == TCP_IPV4_HASH_TYPE) ||
  349. (htype == TCP_IPV6_HASH_TYPE)) ?
  350. PKT_HASH_TYPE_L4 : PKT_HASH_TYPE_L3;
  351. return le32_to_cpu(cqe->rss_hash_result);
  352. }
  353. *rxhash_type = PKT_HASH_TYPE_NONE;
  354. return 0;
  355. }
  356. static void bnx2x_tpa_start(struct bnx2x_fastpath *fp, u16 queue,
  357. u16 cons, u16 prod,
  358. struct eth_fast_path_rx_cqe *cqe)
  359. {
  360. struct bnx2x *bp = fp->bp;
  361. struct sw_rx_bd *cons_rx_buf = &fp->rx_buf_ring[cons];
  362. struct sw_rx_bd *prod_rx_buf = &fp->rx_buf_ring[prod];
  363. struct eth_rx_bd *prod_bd = &fp->rx_desc_ring[prod];
  364. dma_addr_t mapping;
  365. struct bnx2x_agg_info *tpa_info = &fp->tpa_info[queue];
  366. struct sw_rx_bd *first_buf = &tpa_info->first_buf;
  367. /* print error if current state != stop */
  368. if (tpa_info->tpa_state != BNX2X_TPA_STOP)
  369. BNX2X_ERR("start of bin not in stop [%d]\n", queue);
  370. /* Try to map an empty data buffer from the aggregation info */
  371. mapping = dma_map_single(&bp->pdev->dev,
  372. first_buf->data + NET_SKB_PAD,
  373. fp->rx_buf_size, DMA_FROM_DEVICE);
  374. /*
  375. * ...if it fails - move the skb from the consumer to the producer
  376. * and set the current aggregation state as ERROR to drop it
  377. * when TPA_STOP arrives.
  378. */
  379. if (unlikely(dma_mapping_error(&bp->pdev->dev, mapping))) {
  380. /* Move the BD from the consumer to the producer */
  381. bnx2x_reuse_rx_data(fp, cons, prod);
  382. tpa_info->tpa_state = BNX2X_TPA_ERROR;
  383. return;
  384. }
  385. /* move empty data from pool to prod */
  386. prod_rx_buf->data = first_buf->data;
  387. dma_unmap_addr_set(prod_rx_buf, mapping, mapping);
  388. /* point prod_bd to new data */
  389. prod_bd->addr_hi = cpu_to_le32(U64_HI(mapping));
  390. prod_bd->addr_lo = cpu_to_le32(U64_LO(mapping));
  391. /* move partial skb from cons to pool (don't unmap yet) */
  392. *first_buf = *cons_rx_buf;
  393. /* mark bin state as START */
  394. tpa_info->parsing_flags =
  395. le16_to_cpu(cqe->pars_flags.flags);
  396. tpa_info->vlan_tag = le16_to_cpu(cqe->vlan_tag);
  397. tpa_info->tpa_state = BNX2X_TPA_START;
  398. tpa_info->len_on_bd = le16_to_cpu(cqe->len_on_bd);
  399. tpa_info->placement_offset = cqe->placement_offset;
  400. tpa_info->rxhash = bnx2x_get_rxhash(bp, cqe, &tpa_info->rxhash_type);
  401. if (fp->mode == TPA_MODE_GRO) {
  402. u16 gro_size = le16_to_cpu(cqe->pkt_len_or_gro_seg_len);
  403. tpa_info->full_page = SGE_PAGES / gro_size * gro_size;
  404. tpa_info->gro_size = gro_size;
  405. }
  406. #ifdef BNX2X_STOP_ON_ERROR
  407. fp->tpa_queue_used |= (1 << queue);
  408. DP(NETIF_MSG_RX_STATUS, "fp->tpa_queue_used = 0x%llx\n",
  409. fp->tpa_queue_used);
  410. #endif
  411. }
  412. /* Timestamp option length allowed for TPA aggregation:
  413. *
  414. * nop nop kind length echo val
  415. */
  416. #define TPA_TSTAMP_OPT_LEN 12
  417. /**
  418. * bnx2x_set_gro_params - compute GRO values
  419. *
  420. * @skb: packet skb
  421. * @parsing_flags: parsing flags from the START CQE
  422. * @len_on_bd: total length of the first packet for the
  423. * aggregation.
  424. * @pkt_len: length of all segments
  425. *
  426. * Approximate value of the MSS for this aggregation calculated using
  427. * the first packet of it.
  428. * Compute number of aggregated segments, and gso_type.
  429. */
  430. static void bnx2x_set_gro_params(struct sk_buff *skb, u16 parsing_flags,
  431. u16 len_on_bd, unsigned int pkt_len,
  432. u16 num_of_coalesced_segs)
  433. {
  434. /* TPA aggregation won't have either IP options or TCP options
  435. * other than timestamp or IPv6 extension headers.
  436. */
  437. u16 hdrs_len = ETH_HLEN + sizeof(struct tcphdr);
  438. if (GET_FLAG(parsing_flags, PARSING_FLAGS_OVER_ETHERNET_PROTOCOL) ==
  439. PRS_FLAG_OVERETH_IPV6) {
  440. hdrs_len += sizeof(struct ipv6hdr);
  441. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  442. } else {
  443. hdrs_len += sizeof(struct iphdr);
  444. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  445. }
  446. /* Check if there was a TCP timestamp, if there is it's will
  447. * always be 12 bytes length: nop nop kind length echo val.
  448. *
  449. * Otherwise FW would close the aggregation.
  450. */
  451. if (parsing_flags & PARSING_FLAGS_TIME_STAMP_EXIST_FLAG)
  452. hdrs_len += TPA_TSTAMP_OPT_LEN;
  453. skb_shinfo(skb)->gso_size = len_on_bd - hdrs_len;
  454. /* tcp_gro_complete() will copy NAPI_GRO_CB(skb)->count
  455. * to skb_shinfo(skb)->gso_segs
  456. */
  457. NAPI_GRO_CB(skb)->count = num_of_coalesced_segs;
  458. }
  459. static int bnx2x_alloc_rx_sge(struct bnx2x *bp, struct bnx2x_fastpath *fp,
  460. u16 index, gfp_t gfp_mask)
  461. {
  462. struct sw_rx_page *sw_buf = &fp->rx_page_ring[index];
  463. struct eth_rx_sge *sge = &fp->rx_sge_ring[index];
  464. struct bnx2x_alloc_pool *pool = &fp->page_pool;
  465. dma_addr_t mapping;
  466. if (!pool->page || (PAGE_SIZE - pool->offset) < SGE_PAGE_SIZE) {
  467. /* put page reference used by the memory pool, since we
  468. * won't be using this page as the mempool anymore.
  469. */
  470. if (pool->page)
  471. put_page(pool->page);
  472. pool->page = alloc_pages(gfp_mask, PAGES_PER_SGE_SHIFT);
  473. if (unlikely(!pool->page))
  474. return -ENOMEM;
  475. pool->offset = 0;
  476. }
  477. mapping = dma_map_page(&bp->pdev->dev, pool->page,
  478. pool->offset, SGE_PAGE_SIZE, DMA_FROM_DEVICE);
  479. if (unlikely(dma_mapping_error(&bp->pdev->dev, mapping))) {
  480. BNX2X_ERR("Can't map sge\n");
  481. return -ENOMEM;
  482. }
  483. get_page(pool->page);
  484. sw_buf->page = pool->page;
  485. sw_buf->offset = pool->offset;
  486. dma_unmap_addr_set(sw_buf, mapping, mapping);
  487. sge->addr_hi = cpu_to_le32(U64_HI(mapping));
  488. sge->addr_lo = cpu_to_le32(U64_LO(mapping));
  489. pool->offset += SGE_PAGE_SIZE;
  490. return 0;
  491. }
  492. static int bnx2x_fill_frag_skb(struct bnx2x *bp, struct bnx2x_fastpath *fp,
  493. struct bnx2x_agg_info *tpa_info,
  494. u16 pages,
  495. struct sk_buff *skb,
  496. struct eth_end_agg_rx_cqe *cqe,
  497. u16 cqe_idx)
  498. {
  499. struct sw_rx_page *rx_pg, old_rx_pg;
  500. u32 i, frag_len, frag_size;
  501. int err, j, frag_id = 0;
  502. u16 len_on_bd = tpa_info->len_on_bd;
  503. u16 full_page = 0, gro_size = 0;
  504. frag_size = le16_to_cpu(cqe->pkt_len) - len_on_bd;
  505. if (fp->mode == TPA_MODE_GRO) {
  506. gro_size = tpa_info->gro_size;
  507. full_page = tpa_info->full_page;
  508. }
  509. /* This is needed in order to enable forwarding support */
  510. if (frag_size)
  511. bnx2x_set_gro_params(skb, tpa_info->parsing_flags, len_on_bd,
  512. le16_to_cpu(cqe->pkt_len),
  513. le16_to_cpu(cqe->num_of_coalesced_segs));
  514. #ifdef BNX2X_STOP_ON_ERROR
  515. if (pages > min_t(u32, 8, MAX_SKB_FRAGS) * SGE_PAGES) {
  516. BNX2X_ERR("SGL length is too long: %d. CQE index is %d\n",
  517. pages, cqe_idx);
  518. BNX2X_ERR("cqe->pkt_len = %d\n", cqe->pkt_len);
  519. bnx2x_panic();
  520. return -EINVAL;
  521. }
  522. #endif
  523. /* Run through the SGL and compose the fragmented skb */
  524. for (i = 0, j = 0; i < pages; i += PAGES_PER_SGE, j++) {
  525. u16 sge_idx = RX_SGE(le16_to_cpu(cqe->sgl_or_raw_data.sgl[j]));
  526. /* FW gives the indices of the SGE as if the ring is an array
  527. (meaning that "next" element will consume 2 indices) */
  528. if (fp->mode == TPA_MODE_GRO)
  529. frag_len = min_t(u32, frag_size, (u32)full_page);
  530. else /* LRO */
  531. frag_len = min_t(u32, frag_size, (u32)SGE_PAGES);
  532. rx_pg = &fp->rx_page_ring[sge_idx];
  533. old_rx_pg = *rx_pg;
  534. /* If we fail to allocate a substitute page, we simply stop
  535. where we are and drop the whole packet */
  536. err = bnx2x_alloc_rx_sge(bp, fp, sge_idx, GFP_ATOMIC);
  537. if (unlikely(err)) {
  538. bnx2x_fp_qstats(bp, fp)->rx_skb_alloc_failed++;
  539. return err;
  540. }
  541. dma_unmap_page(&bp->pdev->dev,
  542. dma_unmap_addr(&old_rx_pg, mapping),
  543. SGE_PAGE_SIZE, DMA_FROM_DEVICE);
  544. /* Add one frag and update the appropriate fields in the skb */
  545. if (fp->mode == TPA_MODE_LRO)
  546. skb_fill_page_desc(skb, j, old_rx_pg.page,
  547. old_rx_pg.offset, frag_len);
  548. else { /* GRO */
  549. int rem;
  550. int offset = 0;
  551. for (rem = frag_len; rem > 0; rem -= gro_size) {
  552. int len = rem > gro_size ? gro_size : rem;
  553. skb_fill_page_desc(skb, frag_id++,
  554. old_rx_pg.page,
  555. old_rx_pg.offset + offset,
  556. len);
  557. if (offset)
  558. get_page(old_rx_pg.page);
  559. offset += len;
  560. }
  561. }
  562. skb->data_len += frag_len;
  563. skb->truesize += SGE_PAGES;
  564. skb->len += frag_len;
  565. frag_size -= frag_len;
  566. }
  567. return 0;
  568. }
  569. static void bnx2x_frag_free(const struct bnx2x_fastpath *fp, void *data)
  570. {
  571. if (fp->rx_frag_size)
  572. skb_free_frag(data);
  573. else
  574. kfree(data);
  575. }
  576. static void *bnx2x_frag_alloc(const struct bnx2x_fastpath *fp, gfp_t gfp_mask)
  577. {
  578. if (fp->rx_frag_size) {
  579. /* GFP_KERNEL allocations are used only during initialization */
  580. if (unlikely(gfpflags_allow_blocking(gfp_mask)))
  581. return (void *)__get_free_page(gfp_mask);
  582. return netdev_alloc_frag(fp->rx_frag_size);
  583. }
  584. return kmalloc(fp->rx_buf_size + NET_SKB_PAD, gfp_mask);
  585. }
  586. #ifdef CONFIG_INET
  587. static void bnx2x_gro_ip_csum(struct bnx2x *bp, struct sk_buff *skb)
  588. {
  589. const struct iphdr *iph = ip_hdr(skb);
  590. struct tcphdr *th;
  591. skb_set_transport_header(skb, sizeof(struct iphdr));
  592. th = tcp_hdr(skb);
  593. th->check = ~tcp_v4_check(skb->len - skb_transport_offset(skb),
  594. iph->saddr, iph->daddr, 0);
  595. }
  596. static void bnx2x_gro_ipv6_csum(struct bnx2x *bp, struct sk_buff *skb)
  597. {
  598. struct ipv6hdr *iph = ipv6_hdr(skb);
  599. struct tcphdr *th;
  600. skb_set_transport_header(skb, sizeof(struct ipv6hdr));
  601. th = tcp_hdr(skb);
  602. th->check = ~tcp_v6_check(skb->len - skb_transport_offset(skb),
  603. &iph->saddr, &iph->daddr, 0);
  604. }
  605. static void bnx2x_gro_csum(struct bnx2x *bp, struct sk_buff *skb,
  606. void (*gro_func)(struct bnx2x*, struct sk_buff*))
  607. {
  608. skb_set_network_header(skb, 0);
  609. gro_func(bp, skb);
  610. tcp_gro_complete(skb);
  611. }
  612. #endif
  613. static void bnx2x_gro_receive(struct bnx2x *bp, struct bnx2x_fastpath *fp,
  614. struct sk_buff *skb)
  615. {
  616. #ifdef CONFIG_INET
  617. if (skb_shinfo(skb)->gso_size) {
  618. switch (be16_to_cpu(skb->protocol)) {
  619. case ETH_P_IP:
  620. bnx2x_gro_csum(bp, skb, bnx2x_gro_ip_csum);
  621. break;
  622. case ETH_P_IPV6:
  623. bnx2x_gro_csum(bp, skb, bnx2x_gro_ipv6_csum);
  624. break;
  625. default:
  626. WARN_ONCE(1, "Error: FW GRO supports only IPv4/IPv6, not 0x%04x\n",
  627. be16_to_cpu(skb->protocol));
  628. }
  629. }
  630. #endif
  631. skb_record_rx_queue(skb, fp->rx_queue);
  632. napi_gro_receive(&fp->napi, skb);
  633. }
  634. static void bnx2x_tpa_stop(struct bnx2x *bp, struct bnx2x_fastpath *fp,
  635. struct bnx2x_agg_info *tpa_info,
  636. u16 pages,
  637. struct eth_end_agg_rx_cqe *cqe,
  638. u16 cqe_idx)
  639. {
  640. struct sw_rx_bd *rx_buf = &tpa_info->first_buf;
  641. u8 pad = tpa_info->placement_offset;
  642. u16 len = tpa_info->len_on_bd;
  643. struct sk_buff *skb = NULL;
  644. u8 *new_data, *data = rx_buf->data;
  645. u8 old_tpa_state = tpa_info->tpa_state;
  646. tpa_info->tpa_state = BNX2X_TPA_STOP;
  647. /* If we there was an error during the handling of the TPA_START -
  648. * drop this aggregation.
  649. */
  650. if (old_tpa_state == BNX2X_TPA_ERROR)
  651. goto drop;
  652. /* Try to allocate the new data */
  653. new_data = bnx2x_frag_alloc(fp, GFP_ATOMIC);
  654. /* Unmap skb in the pool anyway, as we are going to change
  655. pool entry status to BNX2X_TPA_STOP even if new skb allocation
  656. fails. */
  657. dma_unmap_single(&bp->pdev->dev, dma_unmap_addr(rx_buf, mapping),
  658. fp->rx_buf_size, DMA_FROM_DEVICE);
  659. if (likely(new_data))
  660. skb = build_skb(data, fp->rx_frag_size);
  661. if (likely(skb)) {
  662. #ifdef BNX2X_STOP_ON_ERROR
  663. if (pad + len > fp->rx_buf_size) {
  664. BNX2X_ERR("skb_put is about to fail... pad %d len %d rx_buf_size %d\n",
  665. pad, len, fp->rx_buf_size);
  666. bnx2x_panic();
  667. return;
  668. }
  669. #endif
  670. skb_reserve(skb, pad + NET_SKB_PAD);
  671. skb_put(skb, len);
  672. skb_set_hash(skb, tpa_info->rxhash, tpa_info->rxhash_type);
  673. skb->protocol = eth_type_trans(skb, bp->dev);
  674. skb->ip_summed = CHECKSUM_UNNECESSARY;
  675. if (!bnx2x_fill_frag_skb(bp, fp, tpa_info, pages,
  676. skb, cqe, cqe_idx)) {
  677. if (tpa_info->parsing_flags & PARSING_FLAGS_VLAN)
  678. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), tpa_info->vlan_tag);
  679. bnx2x_gro_receive(bp, fp, skb);
  680. } else {
  681. DP(NETIF_MSG_RX_STATUS,
  682. "Failed to allocate new pages - dropping packet!\n");
  683. dev_kfree_skb_any(skb);
  684. }
  685. /* put new data in bin */
  686. rx_buf->data = new_data;
  687. return;
  688. }
  689. if (new_data)
  690. bnx2x_frag_free(fp, new_data);
  691. drop:
  692. /* drop the packet and keep the buffer in the bin */
  693. DP(NETIF_MSG_RX_STATUS,
  694. "Failed to allocate or map a new skb - dropping packet!\n");
  695. bnx2x_fp_stats(bp, fp)->eth_q_stats.rx_skb_alloc_failed++;
  696. }
  697. static int bnx2x_alloc_rx_data(struct bnx2x *bp, struct bnx2x_fastpath *fp,
  698. u16 index, gfp_t gfp_mask)
  699. {
  700. u8 *data;
  701. struct sw_rx_bd *rx_buf = &fp->rx_buf_ring[index];
  702. struct eth_rx_bd *rx_bd = &fp->rx_desc_ring[index];
  703. dma_addr_t mapping;
  704. data = bnx2x_frag_alloc(fp, gfp_mask);
  705. if (unlikely(data == NULL))
  706. return -ENOMEM;
  707. mapping = dma_map_single(&bp->pdev->dev, data + NET_SKB_PAD,
  708. fp->rx_buf_size,
  709. DMA_FROM_DEVICE);
  710. if (unlikely(dma_mapping_error(&bp->pdev->dev, mapping))) {
  711. bnx2x_frag_free(fp, data);
  712. BNX2X_ERR("Can't map rx data\n");
  713. return -ENOMEM;
  714. }
  715. rx_buf->data = data;
  716. dma_unmap_addr_set(rx_buf, mapping, mapping);
  717. rx_bd->addr_hi = cpu_to_le32(U64_HI(mapping));
  718. rx_bd->addr_lo = cpu_to_le32(U64_LO(mapping));
  719. return 0;
  720. }
  721. static
  722. void bnx2x_csum_validate(struct sk_buff *skb, union eth_rx_cqe *cqe,
  723. struct bnx2x_fastpath *fp,
  724. struct bnx2x_eth_q_stats *qstats)
  725. {
  726. /* Do nothing if no L4 csum validation was done.
  727. * We do not check whether IP csum was validated. For IPv4 we assume
  728. * that if the card got as far as validating the L4 csum, it also
  729. * validated the IP csum. IPv6 has no IP csum.
  730. */
  731. if (cqe->fast_path_cqe.status_flags &
  732. ETH_FAST_PATH_RX_CQE_L4_XSUM_NO_VALIDATION_FLG)
  733. return;
  734. /* If L4 validation was done, check if an error was found. */
  735. if (cqe->fast_path_cqe.type_error_flags &
  736. (ETH_FAST_PATH_RX_CQE_IP_BAD_XSUM_FLG |
  737. ETH_FAST_PATH_RX_CQE_L4_BAD_XSUM_FLG))
  738. qstats->hw_csum_err++;
  739. else
  740. skb->ip_summed = CHECKSUM_UNNECESSARY;
  741. }
  742. static int bnx2x_rx_int(struct bnx2x_fastpath *fp, int budget)
  743. {
  744. struct bnx2x *bp = fp->bp;
  745. u16 bd_cons, bd_prod, bd_prod_fw, comp_ring_cons;
  746. u16 sw_comp_cons, sw_comp_prod;
  747. int rx_pkt = 0;
  748. union eth_rx_cqe *cqe;
  749. struct eth_fast_path_rx_cqe *cqe_fp;
  750. #ifdef BNX2X_STOP_ON_ERROR
  751. if (unlikely(bp->panic))
  752. return 0;
  753. #endif
  754. if (budget <= 0)
  755. return rx_pkt;
  756. bd_cons = fp->rx_bd_cons;
  757. bd_prod = fp->rx_bd_prod;
  758. bd_prod_fw = bd_prod;
  759. sw_comp_cons = fp->rx_comp_cons;
  760. sw_comp_prod = fp->rx_comp_prod;
  761. comp_ring_cons = RCQ_BD(sw_comp_cons);
  762. cqe = &fp->rx_comp_ring[comp_ring_cons];
  763. cqe_fp = &cqe->fast_path_cqe;
  764. DP(NETIF_MSG_RX_STATUS,
  765. "queue[%d]: sw_comp_cons %u\n", fp->index, sw_comp_cons);
  766. while (BNX2X_IS_CQE_COMPLETED(cqe_fp)) {
  767. struct sw_rx_bd *rx_buf = NULL;
  768. struct sk_buff *skb;
  769. u8 cqe_fp_flags;
  770. enum eth_rx_cqe_type cqe_fp_type;
  771. u16 len, pad, queue;
  772. u8 *data;
  773. u32 rxhash;
  774. enum pkt_hash_types rxhash_type;
  775. #ifdef BNX2X_STOP_ON_ERROR
  776. if (unlikely(bp->panic))
  777. return 0;
  778. #endif
  779. bd_prod = RX_BD(bd_prod);
  780. bd_cons = RX_BD(bd_cons);
  781. /* A rmb() is required to ensure that the CQE is not read
  782. * before it is written by the adapter DMA. PCI ordering
  783. * rules will make sure the other fields are written before
  784. * the marker at the end of struct eth_fast_path_rx_cqe
  785. * but without rmb() a weakly ordered processor can process
  786. * stale data. Without the barrier TPA state-machine might
  787. * enter inconsistent state and kernel stack might be
  788. * provided with incorrect packet description - these lead
  789. * to various kernel crashed.
  790. */
  791. rmb();
  792. cqe_fp_flags = cqe_fp->type_error_flags;
  793. cqe_fp_type = cqe_fp_flags & ETH_FAST_PATH_RX_CQE_TYPE;
  794. DP(NETIF_MSG_RX_STATUS,
  795. "CQE type %x err %x status %x queue %x vlan %x len %u\n",
  796. CQE_TYPE(cqe_fp_flags),
  797. cqe_fp_flags, cqe_fp->status_flags,
  798. le32_to_cpu(cqe_fp->rss_hash_result),
  799. le16_to_cpu(cqe_fp->vlan_tag),
  800. le16_to_cpu(cqe_fp->pkt_len_or_gro_seg_len));
  801. /* is this a slowpath msg? */
  802. if (unlikely(CQE_TYPE_SLOW(cqe_fp_type))) {
  803. bnx2x_sp_event(fp, cqe);
  804. goto next_cqe;
  805. }
  806. rx_buf = &fp->rx_buf_ring[bd_cons];
  807. data = rx_buf->data;
  808. if (!CQE_TYPE_FAST(cqe_fp_type)) {
  809. struct bnx2x_agg_info *tpa_info;
  810. u16 frag_size, pages;
  811. #ifdef BNX2X_STOP_ON_ERROR
  812. /* sanity check */
  813. if (fp->mode == TPA_MODE_DISABLED &&
  814. (CQE_TYPE_START(cqe_fp_type) ||
  815. CQE_TYPE_STOP(cqe_fp_type)))
  816. BNX2X_ERR("START/STOP packet while TPA disabled, type %x\n",
  817. CQE_TYPE(cqe_fp_type));
  818. #endif
  819. if (CQE_TYPE_START(cqe_fp_type)) {
  820. u16 queue = cqe_fp->queue_index;
  821. DP(NETIF_MSG_RX_STATUS,
  822. "calling tpa_start on queue %d\n",
  823. queue);
  824. bnx2x_tpa_start(fp, queue,
  825. bd_cons, bd_prod,
  826. cqe_fp);
  827. goto next_rx;
  828. }
  829. queue = cqe->end_agg_cqe.queue_index;
  830. tpa_info = &fp->tpa_info[queue];
  831. DP(NETIF_MSG_RX_STATUS,
  832. "calling tpa_stop on queue %d\n",
  833. queue);
  834. frag_size = le16_to_cpu(cqe->end_agg_cqe.pkt_len) -
  835. tpa_info->len_on_bd;
  836. if (fp->mode == TPA_MODE_GRO)
  837. pages = (frag_size + tpa_info->full_page - 1) /
  838. tpa_info->full_page;
  839. else
  840. pages = SGE_PAGE_ALIGN(frag_size) >>
  841. SGE_PAGE_SHIFT;
  842. bnx2x_tpa_stop(bp, fp, tpa_info, pages,
  843. &cqe->end_agg_cqe, comp_ring_cons);
  844. #ifdef BNX2X_STOP_ON_ERROR
  845. if (bp->panic)
  846. return 0;
  847. #endif
  848. bnx2x_update_sge_prod(fp, pages, &cqe->end_agg_cqe);
  849. goto next_cqe;
  850. }
  851. /* non TPA */
  852. len = le16_to_cpu(cqe_fp->pkt_len_or_gro_seg_len);
  853. pad = cqe_fp->placement_offset;
  854. dma_sync_single_for_cpu(&bp->pdev->dev,
  855. dma_unmap_addr(rx_buf, mapping),
  856. pad + RX_COPY_THRESH,
  857. DMA_FROM_DEVICE);
  858. pad += NET_SKB_PAD;
  859. prefetch(data + pad); /* speedup eth_type_trans() */
  860. /* is this an error packet? */
  861. if (unlikely(cqe_fp_flags & ETH_RX_ERROR_FALGS)) {
  862. DP(NETIF_MSG_RX_ERR | NETIF_MSG_RX_STATUS,
  863. "ERROR flags %x rx packet %u\n",
  864. cqe_fp_flags, sw_comp_cons);
  865. bnx2x_fp_qstats(bp, fp)->rx_err_discard_pkt++;
  866. goto reuse_rx;
  867. }
  868. /* Since we don't have a jumbo ring
  869. * copy small packets if mtu > 1500
  870. */
  871. if ((bp->dev->mtu > ETH_MAX_PACKET_SIZE) &&
  872. (len <= RX_COPY_THRESH)) {
  873. skb = napi_alloc_skb(&fp->napi, len);
  874. if (skb == NULL) {
  875. DP(NETIF_MSG_RX_ERR | NETIF_MSG_RX_STATUS,
  876. "ERROR packet dropped because of alloc failure\n");
  877. bnx2x_fp_qstats(bp, fp)->rx_skb_alloc_failed++;
  878. goto reuse_rx;
  879. }
  880. memcpy(skb->data, data + pad, len);
  881. bnx2x_reuse_rx_data(fp, bd_cons, bd_prod);
  882. } else {
  883. if (likely(bnx2x_alloc_rx_data(bp, fp, bd_prod,
  884. GFP_ATOMIC) == 0)) {
  885. dma_unmap_single(&bp->pdev->dev,
  886. dma_unmap_addr(rx_buf, mapping),
  887. fp->rx_buf_size,
  888. DMA_FROM_DEVICE);
  889. skb = build_skb(data, fp->rx_frag_size);
  890. if (unlikely(!skb)) {
  891. bnx2x_frag_free(fp, data);
  892. bnx2x_fp_qstats(bp, fp)->
  893. rx_skb_alloc_failed++;
  894. goto next_rx;
  895. }
  896. skb_reserve(skb, pad);
  897. } else {
  898. DP(NETIF_MSG_RX_ERR | NETIF_MSG_RX_STATUS,
  899. "ERROR packet dropped because of alloc failure\n");
  900. bnx2x_fp_qstats(bp, fp)->rx_skb_alloc_failed++;
  901. reuse_rx:
  902. bnx2x_reuse_rx_data(fp, bd_cons, bd_prod);
  903. goto next_rx;
  904. }
  905. }
  906. skb_put(skb, len);
  907. skb->protocol = eth_type_trans(skb, bp->dev);
  908. /* Set Toeplitz hash for a none-LRO skb */
  909. rxhash = bnx2x_get_rxhash(bp, cqe_fp, &rxhash_type);
  910. skb_set_hash(skb, rxhash, rxhash_type);
  911. skb_checksum_none_assert(skb);
  912. if (bp->dev->features & NETIF_F_RXCSUM)
  913. bnx2x_csum_validate(skb, cqe, fp,
  914. bnx2x_fp_qstats(bp, fp));
  915. skb_record_rx_queue(skb, fp->rx_queue);
  916. /* Check if this packet was timestamped */
  917. if (unlikely(cqe->fast_path_cqe.type_error_flags &
  918. (1 << ETH_FAST_PATH_RX_CQE_PTP_PKT_SHIFT)))
  919. bnx2x_set_rx_ts(bp, skb);
  920. if (le16_to_cpu(cqe_fp->pars_flags.flags) &
  921. PARSING_FLAGS_VLAN)
  922. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  923. le16_to_cpu(cqe_fp->vlan_tag));
  924. napi_gro_receive(&fp->napi, skb);
  925. next_rx:
  926. rx_buf->data = NULL;
  927. bd_cons = NEXT_RX_IDX(bd_cons);
  928. bd_prod = NEXT_RX_IDX(bd_prod);
  929. bd_prod_fw = NEXT_RX_IDX(bd_prod_fw);
  930. rx_pkt++;
  931. next_cqe:
  932. sw_comp_prod = NEXT_RCQ_IDX(sw_comp_prod);
  933. sw_comp_cons = NEXT_RCQ_IDX(sw_comp_cons);
  934. /* mark CQE as free */
  935. BNX2X_SEED_CQE(cqe_fp);
  936. if (rx_pkt == budget)
  937. break;
  938. comp_ring_cons = RCQ_BD(sw_comp_cons);
  939. cqe = &fp->rx_comp_ring[comp_ring_cons];
  940. cqe_fp = &cqe->fast_path_cqe;
  941. } /* while */
  942. fp->rx_bd_cons = bd_cons;
  943. fp->rx_bd_prod = bd_prod_fw;
  944. fp->rx_comp_cons = sw_comp_cons;
  945. fp->rx_comp_prod = sw_comp_prod;
  946. /* Update producers */
  947. bnx2x_update_rx_prod(bp, fp, bd_prod_fw, sw_comp_prod,
  948. fp->rx_sge_prod);
  949. return rx_pkt;
  950. }
  951. static irqreturn_t bnx2x_msix_fp_int(int irq, void *fp_cookie)
  952. {
  953. struct bnx2x_fastpath *fp = fp_cookie;
  954. struct bnx2x *bp = fp->bp;
  955. u8 cos;
  956. DP(NETIF_MSG_INTR,
  957. "got an MSI-X interrupt on IDX:SB [fp %d fw_sd %d igusb %d]\n",
  958. fp->index, fp->fw_sb_id, fp->igu_sb_id);
  959. bnx2x_ack_sb(bp, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
  960. #ifdef BNX2X_STOP_ON_ERROR
  961. if (unlikely(bp->panic))
  962. return IRQ_HANDLED;
  963. #endif
  964. /* Handle Rx and Tx according to MSI-X vector */
  965. for_each_cos_in_tx_queue(fp, cos)
  966. prefetch(fp->txdata_ptr[cos]->tx_cons_sb);
  967. prefetch(&fp->sb_running_index[SM_RX_ID]);
  968. napi_schedule_irqoff(&bnx2x_fp(bp, fp->index, napi));
  969. return IRQ_HANDLED;
  970. }
  971. /* HW Lock for shared dual port PHYs */
  972. void bnx2x_acquire_phy_lock(struct bnx2x *bp)
  973. {
  974. mutex_lock(&bp->port.phy_mutex);
  975. bnx2x_acquire_hw_lock(bp, HW_LOCK_RESOURCE_MDIO);
  976. }
  977. void bnx2x_release_phy_lock(struct bnx2x *bp)
  978. {
  979. bnx2x_release_hw_lock(bp, HW_LOCK_RESOURCE_MDIO);
  980. mutex_unlock(&bp->port.phy_mutex);
  981. }
  982. /* calculates MF speed according to current linespeed and MF configuration */
  983. u16 bnx2x_get_mf_speed(struct bnx2x *bp)
  984. {
  985. u16 line_speed = bp->link_vars.line_speed;
  986. if (IS_MF(bp)) {
  987. u16 maxCfg = bnx2x_extract_max_cfg(bp,
  988. bp->mf_config[BP_VN(bp)]);
  989. /* Calculate the current MAX line speed limit for the MF
  990. * devices
  991. */
  992. if (IS_MF_PERCENT_BW(bp))
  993. line_speed = (line_speed * maxCfg) / 100;
  994. else { /* SD mode */
  995. u16 vn_max_rate = maxCfg * 100;
  996. if (vn_max_rate < line_speed)
  997. line_speed = vn_max_rate;
  998. }
  999. }
  1000. return line_speed;
  1001. }
  1002. /**
  1003. * bnx2x_fill_report_data - fill link report data to report
  1004. *
  1005. * @bp: driver handle
  1006. * @data: link state to update
  1007. *
  1008. * It uses a none-atomic bit operations because is called under the mutex.
  1009. */
  1010. static void bnx2x_fill_report_data(struct bnx2x *bp,
  1011. struct bnx2x_link_report_data *data)
  1012. {
  1013. memset(data, 0, sizeof(*data));
  1014. if (IS_PF(bp)) {
  1015. /* Fill the report data: effective line speed */
  1016. data->line_speed = bnx2x_get_mf_speed(bp);
  1017. /* Link is down */
  1018. if (!bp->link_vars.link_up || (bp->flags & MF_FUNC_DIS))
  1019. __set_bit(BNX2X_LINK_REPORT_LINK_DOWN,
  1020. &data->link_report_flags);
  1021. if (!BNX2X_NUM_ETH_QUEUES(bp))
  1022. __set_bit(BNX2X_LINK_REPORT_LINK_DOWN,
  1023. &data->link_report_flags);
  1024. /* Full DUPLEX */
  1025. if (bp->link_vars.duplex == DUPLEX_FULL)
  1026. __set_bit(BNX2X_LINK_REPORT_FD,
  1027. &data->link_report_flags);
  1028. /* Rx Flow Control is ON */
  1029. if (bp->link_vars.flow_ctrl & BNX2X_FLOW_CTRL_RX)
  1030. __set_bit(BNX2X_LINK_REPORT_RX_FC_ON,
  1031. &data->link_report_flags);
  1032. /* Tx Flow Control is ON */
  1033. if (bp->link_vars.flow_ctrl & BNX2X_FLOW_CTRL_TX)
  1034. __set_bit(BNX2X_LINK_REPORT_TX_FC_ON,
  1035. &data->link_report_flags);
  1036. } else { /* VF */
  1037. *data = bp->vf_link_vars;
  1038. }
  1039. }
  1040. /**
  1041. * bnx2x_link_report - report link status to OS.
  1042. *
  1043. * @bp: driver handle
  1044. *
  1045. * Calls the __bnx2x_link_report() under the same locking scheme
  1046. * as a link/PHY state managing code to ensure a consistent link
  1047. * reporting.
  1048. */
  1049. void bnx2x_link_report(struct bnx2x *bp)
  1050. {
  1051. bnx2x_acquire_phy_lock(bp);
  1052. __bnx2x_link_report(bp);
  1053. bnx2x_release_phy_lock(bp);
  1054. }
  1055. /**
  1056. * __bnx2x_link_report - report link status to OS.
  1057. *
  1058. * @bp: driver handle
  1059. *
  1060. * None atomic implementation.
  1061. * Should be called under the phy_lock.
  1062. */
  1063. void __bnx2x_link_report(struct bnx2x *bp)
  1064. {
  1065. struct bnx2x_link_report_data cur_data;
  1066. /* reread mf_cfg */
  1067. if (IS_PF(bp) && !CHIP_IS_E1(bp))
  1068. bnx2x_read_mf_cfg(bp);
  1069. /* Read the current link report info */
  1070. bnx2x_fill_report_data(bp, &cur_data);
  1071. /* Don't report link down or exactly the same link status twice */
  1072. if (!memcmp(&cur_data, &bp->last_reported_link, sizeof(cur_data)) ||
  1073. (test_bit(BNX2X_LINK_REPORT_LINK_DOWN,
  1074. &bp->last_reported_link.link_report_flags) &&
  1075. test_bit(BNX2X_LINK_REPORT_LINK_DOWN,
  1076. &cur_data.link_report_flags)))
  1077. return;
  1078. bp->link_cnt++;
  1079. /* We are going to report a new link parameters now -
  1080. * remember the current data for the next time.
  1081. */
  1082. memcpy(&bp->last_reported_link, &cur_data, sizeof(cur_data));
  1083. /* propagate status to VFs */
  1084. if (IS_PF(bp))
  1085. bnx2x_iov_link_update(bp);
  1086. if (test_bit(BNX2X_LINK_REPORT_LINK_DOWN,
  1087. &cur_data.link_report_flags)) {
  1088. netif_carrier_off(bp->dev);
  1089. netdev_err(bp->dev, "NIC Link is Down\n");
  1090. return;
  1091. } else {
  1092. const char *duplex;
  1093. const char *flow;
  1094. netif_carrier_on(bp->dev);
  1095. if (test_and_clear_bit(BNX2X_LINK_REPORT_FD,
  1096. &cur_data.link_report_flags))
  1097. duplex = "full";
  1098. else
  1099. duplex = "half";
  1100. /* Handle the FC at the end so that only these flags would be
  1101. * possibly set. This way we may easily check if there is no FC
  1102. * enabled.
  1103. */
  1104. if (cur_data.link_report_flags) {
  1105. if (test_bit(BNX2X_LINK_REPORT_RX_FC_ON,
  1106. &cur_data.link_report_flags)) {
  1107. if (test_bit(BNX2X_LINK_REPORT_TX_FC_ON,
  1108. &cur_data.link_report_flags))
  1109. flow = "ON - receive & transmit";
  1110. else
  1111. flow = "ON - receive";
  1112. } else {
  1113. flow = "ON - transmit";
  1114. }
  1115. } else {
  1116. flow = "none";
  1117. }
  1118. netdev_info(bp->dev, "NIC Link is Up, %d Mbps %s duplex, Flow control: %s\n",
  1119. cur_data.line_speed, duplex, flow);
  1120. }
  1121. }
  1122. static void bnx2x_set_next_page_sgl(struct bnx2x_fastpath *fp)
  1123. {
  1124. int i;
  1125. for (i = 1; i <= NUM_RX_SGE_PAGES; i++) {
  1126. struct eth_rx_sge *sge;
  1127. sge = &fp->rx_sge_ring[RX_SGE_CNT * i - 2];
  1128. sge->addr_hi =
  1129. cpu_to_le32(U64_HI(fp->rx_sge_mapping +
  1130. BCM_PAGE_SIZE*(i % NUM_RX_SGE_PAGES)));
  1131. sge->addr_lo =
  1132. cpu_to_le32(U64_LO(fp->rx_sge_mapping +
  1133. BCM_PAGE_SIZE*(i % NUM_RX_SGE_PAGES)));
  1134. }
  1135. }
  1136. static void bnx2x_free_tpa_pool(struct bnx2x *bp,
  1137. struct bnx2x_fastpath *fp, int last)
  1138. {
  1139. int i;
  1140. for (i = 0; i < last; i++) {
  1141. struct bnx2x_agg_info *tpa_info = &fp->tpa_info[i];
  1142. struct sw_rx_bd *first_buf = &tpa_info->first_buf;
  1143. u8 *data = first_buf->data;
  1144. if (data == NULL) {
  1145. DP(NETIF_MSG_IFDOWN, "tpa bin %d empty on free\n", i);
  1146. continue;
  1147. }
  1148. if (tpa_info->tpa_state == BNX2X_TPA_START)
  1149. dma_unmap_single(&bp->pdev->dev,
  1150. dma_unmap_addr(first_buf, mapping),
  1151. fp->rx_buf_size, DMA_FROM_DEVICE);
  1152. bnx2x_frag_free(fp, data);
  1153. first_buf->data = NULL;
  1154. }
  1155. }
  1156. void bnx2x_init_rx_rings_cnic(struct bnx2x *bp)
  1157. {
  1158. int j;
  1159. for_each_rx_queue_cnic(bp, j) {
  1160. struct bnx2x_fastpath *fp = &bp->fp[j];
  1161. fp->rx_bd_cons = 0;
  1162. /* Activate BD ring */
  1163. /* Warning!
  1164. * this will generate an interrupt (to the TSTORM)
  1165. * must only be done after chip is initialized
  1166. */
  1167. bnx2x_update_rx_prod(bp, fp, fp->rx_bd_prod, fp->rx_comp_prod,
  1168. fp->rx_sge_prod);
  1169. }
  1170. }
  1171. void bnx2x_init_rx_rings(struct bnx2x *bp)
  1172. {
  1173. int func = BP_FUNC(bp);
  1174. u16 ring_prod;
  1175. int i, j;
  1176. /* Allocate TPA resources */
  1177. for_each_eth_queue(bp, j) {
  1178. struct bnx2x_fastpath *fp = &bp->fp[j];
  1179. DP(NETIF_MSG_IFUP,
  1180. "mtu %d rx_buf_size %d\n", bp->dev->mtu, fp->rx_buf_size);
  1181. if (fp->mode != TPA_MODE_DISABLED) {
  1182. /* Fill the per-aggregation pool */
  1183. for (i = 0; i < MAX_AGG_QS(bp); i++) {
  1184. struct bnx2x_agg_info *tpa_info =
  1185. &fp->tpa_info[i];
  1186. struct sw_rx_bd *first_buf =
  1187. &tpa_info->first_buf;
  1188. first_buf->data =
  1189. bnx2x_frag_alloc(fp, GFP_KERNEL);
  1190. if (!first_buf->data) {
  1191. BNX2X_ERR("Failed to allocate TPA skb pool for queue[%d] - disabling TPA on this queue!\n",
  1192. j);
  1193. bnx2x_free_tpa_pool(bp, fp, i);
  1194. fp->mode = TPA_MODE_DISABLED;
  1195. break;
  1196. }
  1197. dma_unmap_addr_set(first_buf, mapping, 0);
  1198. tpa_info->tpa_state = BNX2X_TPA_STOP;
  1199. }
  1200. /* "next page" elements initialization */
  1201. bnx2x_set_next_page_sgl(fp);
  1202. /* set SGEs bit mask */
  1203. bnx2x_init_sge_ring_bit_mask(fp);
  1204. /* Allocate SGEs and initialize the ring elements */
  1205. for (i = 0, ring_prod = 0;
  1206. i < MAX_RX_SGE_CNT*NUM_RX_SGE_PAGES; i++) {
  1207. if (bnx2x_alloc_rx_sge(bp, fp, ring_prod,
  1208. GFP_KERNEL) < 0) {
  1209. BNX2X_ERR("was only able to allocate %d rx sges\n",
  1210. i);
  1211. BNX2X_ERR("disabling TPA for queue[%d]\n",
  1212. j);
  1213. /* Cleanup already allocated elements */
  1214. bnx2x_free_rx_sge_range(bp, fp,
  1215. ring_prod);
  1216. bnx2x_free_tpa_pool(bp, fp,
  1217. MAX_AGG_QS(bp));
  1218. fp->mode = TPA_MODE_DISABLED;
  1219. ring_prod = 0;
  1220. break;
  1221. }
  1222. ring_prod = NEXT_SGE_IDX(ring_prod);
  1223. }
  1224. fp->rx_sge_prod = ring_prod;
  1225. }
  1226. }
  1227. for_each_eth_queue(bp, j) {
  1228. struct bnx2x_fastpath *fp = &bp->fp[j];
  1229. fp->rx_bd_cons = 0;
  1230. /* Activate BD ring */
  1231. /* Warning!
  1232. * this will generate an interrupt (to the TSTORM)
  1233. * must only be done after chip is initialized
  1234. */
  1235. bnx2x_update_rx_prod(bp, fp, fp->rx_bd_prod, fp->rx_comp_prod,
  1236. fp->rx_sge_prod);
  1237. if (j != 0)
  1238. continue;
  1239. if (CHIP_IS_E1(bp)) {
  1240. REG_WR(bp, BAR_USTRORM_INTMEM +
  1241. USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(func),
  1242. U64_LO(fp->rx_comp_mapping));
  1243. REG_WR(bp, BAR_USTRORM_INTMEM +
  1244. USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(func) + 4,
  1245. U64_HI(fp->rx_comp_mapping));
  1246. }
  1247. }
  1248. }
  1249. static void bnx2x_free_tx_skbs_queue(struct bnx2x_fastpath *fp)
  1250. {
  1251. u8 cos;
  1252. struct bnx2x *bp = fp->bp;
  1253. for_each_cos_in_tx_queue(fp, cos) {
  1254. struct bnx2x_fp_txdata *txdata = fp->txdata_ptr[cos];
  1255. unsigned pkts_compl = 0, bytes_compl = 0;
  1256. u16 sw_prod = txdata->tx_pkt_prod;
  1257. u16 sw_cons = txdata->tx_pkt_cons;
  1258. while (sw_cons != sw_prod) {
  1259. bnx2x_free_tx_pkt(bp, txdata, TX_BD(sw_cons),
  1260. &pkts_compl, &bytes_compl);
  1261. sw_cons++;
  1262. }
  1263. netdev_tx_reset_queue(
  1264. netdev_get_tx_queue(bp->dev,
  1265. txdata->txq_index));
  1266. }
  1267. }
  1268. static void bnx2x_free_tx_skbs_cnic(struct bnx2x *bp)
  1269. {
  1270. int i;
  1271. for_each_tx_queue_cnic(bp, i) {
  1272. bnx2x_free_tx_skbs_queue(&bp->fp[i]);
  1273. }
  1274. }
  1275. static void bnx2x_free_tx_skbs(struct bnx2x *bp)
  1276. {
  1277. int i;
  1278. for_each_eth_queue(bp, i) {
  1279. bnx2x_free_tx_skbs_queue(&bp->fp[i]);
  1280. }
  1281. }
  1282. static void bnx2x_free_rx_bds(struct bnx2x_fastpath *fp)
  1283. {
  1284. struct bnx2x *bp = fp->bp;
  1285. int i;
  1286. /* ring wasn't allocated */
  1287. if (fp->rx_buf_ring == NULL)
  1288. return;
  1289. for (i = 0; i < NUM_RX_BD; i++) {
  1290. struct sw_rx_bd *rx_buf = &fp->rx_buf_ring[i];
  1291. u8 *data = rx_buf->data;
  1292. if (data == NULL)
  1293. continue;
  1294. dma_unmap_single(&bp->pdev->dev,
  1295. dma_unmap_addr(rx_buf, mapping),
  1296. fp->rx_buf_size, DMA_FROM_DEVICE);
  1297. rx_buf->data = NULL;
  1298. bnx2x_frag_free(fp, data);
  1299. }
  1300. }
  1301. static void bnx2x_free_rx_skbs_cnic(struct bnx2x *bp)
  1302. {
  1303. int j;
  1304. for_each_rx_queue_cnic(bp, j) {
  1305. bnx2x_free_rx_bds(&bp->fp[j]);
  1306. }
  1307. }
  1308. static void bnx2x_free_rx_skbs(struct bnx2x *bp)
  1309. {
  1310. int j;
  1311. for_each_eth_queue(bp, j) {
  1312. struct bnx2x_fastpath *fp = &bp->fp[j];
  1313. bnx2x_free_rx_bds(fp);
  1314. if (fp->mode != TPA_MODE_DISABLED)
  1315. bnx2x_free_tpa_pool(bp, fp, MAX_AGG_QS(bp));
  1316. }
  1317. }
  1318. static void bnx2x_free_skbs_cnic(struct bnx2x *bp)
  1319. {
  1320. bnx2x_free_tx_skbs_cnic(bp);
  1321. bnx2x_free_rx_skbs_cnic(bp);
  1322. }
  1323. void bnx2x_free_skbs(struct bnx2x *bp)
  1324. {
  1325. bnx2x_free_tx_skbs(bp);
  1326. bnx2x_free_rx_skbs(bp);
  1327. }
  1328. void bnx2x_update_max_mf_config(struct bnx2x *bp, u32 value)
  1329. {
  1330. /* load old values */
  1331. u32 mf_cfg = bp->mf_config[BP_VN(bp)];
  1332. if (value != bnx2x_extract_max_cfg(bp, mf_cfg)) {
  1333. /* leave all but MAX value */
  1334. mf_cfg &= ~FUNC_MF_CFG_MAX_BW_MASK;
  1335. /* set new MAX value */
  1336. mf_cfg |= (value << FUNC_MF_CFG_MAX_BW_SHIFT)
  1337. & FUNC_MF_CFG_MAX_BW_MASK;
  1338. bnx2x_fw_command(bp, DRV_MSG_CODE_SET_MF_BW, mf_cfg);
  1339. }
  1340. }
  1341. /**
  1342. * bnx2x_free_msix_irqs - free previously requested MSI-X IRQ vectors
  1343. *
  1344. * @bp: driver handle
  1345. * @nvecs: number of vectors to be released
  1346. */
  1347. static void bnx2x_free_msix_irqs(struct bnx2x *bp, int nvecs)
  1348. {
  1349. int i, offset = 0;
  1350. if (nvecs == offset)
  1351. return;
  1352. /* VFs don't have a default SB */
  1353. if (IS_PF(bp)) {
  1354. free_irq(bp->msix_table[offset].vector, bp->dev);
  1355. DP(NETIF_MSG_IFDOWN, "released sp irq (%d)\n",
  1356. bp->msix_table[offset].vector);
  1357. offset++;
  1358. }
  1359. if (CNIC_SUPPORT(bp)) {
  1360. if (nvecs == offset)
  1361. return;
  1362. offset++;
  1363. }
  1364. for_each_eth_queue(bp, i) {
  1365. if (nvecs == offset)
  1366. return;
  1367. DP(NETIF_MSG_IFDOWN, "about to release fp #%d->%d irq\n",
  1368. i, bp->msix_table[offset].vector);
  1369. free_irq(bp->msix_table[offset++].vector, &bp->fp[i]);
  1370. }
  1371. }
  1372. void bnx2x_free_irq(struct bnx2x *bp)
  1373. {
  1374. if (bp->flags & USING_MSIX_FLAG &&
  1375. !(bp->flags & USING_SINGLE_MSIX_FLAG)) {
  1376. int nvecs = BNX2X_NUM_ETH_QUEUES(bp) + CNIC_SUPPORT(bp);
  1377. /* vfs don't have a default status block */
  1378. if (IS_PF(bp))
  1379. nvecs++;
  1380. bnx2x_free_msix_irqs(bp, nvecs);
  1381. } else {
  1382. free_irq(bp->dev->irq, bp->dev);
  1383. }
  1384. }
  1385. int bnx2x_enable_msix(struct bnx2x *bp)
  1386. {
  1387. int msix_vec = 0, i, rc;
  1388. /* VFs don't have a default status block */
  1389. if (IS_PF(bp)) {
  1390. bp->msix_table[msix_vec].entry = msix_vec;
  1391. BNX2X_DEV_INFO("msix_table[0].entry = %d (slowpath)\n",
  1392. bp->msix_table[0].entry);
  1393. msix_vec++;
  1394. }
  1395. /* Cnic requires an msix vector for itself */
  1396. if (CNIC_SUPPORT(bp)) {
  1397. bp->msix_table[msix_vec].entry = msix_vec;
  1398. BNX2X_DEV_INFO("msix_table[%d].entry = %d (CNIC)\n",
  1399. msix_vec, bp->msix_table[msix_vec].entry);
  1400. msix_vec++;
  1401. }
  1402. /* We need separate vectors for ETH queues only (not FCoE) */
  1403. for_each_eth_queue(bp, i) {
  1404. bp->msix_table[msix_vec].entry = msix_vec;
  1405. BNX2X_DEV_INFO("msix_table[%d].entry = %d (fastpath #%u)\n",
  1406. msix_vec, msix_vec, i);
  1407. msix_vec++;
  1408. }
  1409. DP(BNX2X_MSG_SP, "about to request enable msix with %d vectors\n",
  1410. msix_vec);
  1411. rc = pci_enable_msix_range(bp->pdev, &bp->msix_table[0],
  1412. BNX2X_MIN_MSIX_VEC_CNT(bp), msix_vec);
  1413. /*
  1414. * reconfigure number of tx/rx queues according to available
  1415. * MSI-X vectors
  1416. */
  1417. if (rc == -ENOSPC) {
  1418. /* Get by with single vector */
  1419. rc = pci_enable_msix_range(bp->pdev, &bp->msix_table[0], 1, 1);
  1420. if (rc < 0) {
  1421. BNX2X_DEV_INFO("Single MSI-X is not attainable rc %d\n",
  1422. rc);
  1423. goto no_msix;
  1424. }
  1425. BNX2X_DEV_INFO("Using single MSI-X vector\n");
  1426. bp->flags |= USING_SINGLE_MSIX_FLAG;
  1427. BNX2X_DEV_INFO("set number of queues to 1\n");
  1428. bp->num_ethernet_queues = 1;
  1429. bp->num_queues = bp->num_ethernet_queues + bp->num_cnic_queues;
  1430. } else if (rc < 0) {
  1431. BNX2X_DEV_INFO("MSI-X is not attainable rc %d\n", rc);
  1432. goto no_msix;
  1433. } else if (rc < msix_vec) {
  1434. /* how less vectors we will have? */
  1435. int diff = msix_vec - rc;
  1436. BNX2X_DEV_INFO("Trying to use less MSI-X vectors: %d\n", rc);
  1437. /*
  1438. * decrease number of queues by number of unallocated entries
  1439. */
  1440. bp->num_ethernet_queues -= diff;
  1441. bp->num_queues = bp->num_ethernet_queues + bp->num_cnic_queues;
  1442. BNX2X_DEV_INFO("New queue configuration set: %d\n",
  1443. bp->num_queues);
  1444. }
  1445. bp->flags |= USING_MSIX_FLAG;
  1446. return 0;
  1447. no_msix:
  1448. /* fall to INTx if not enough memory */
  1449. if (rc == -ENOMEM)
  1450. bp->flags |= DISABLE_MSI_FLAG;
  1451. return rc;
  1452. }
  1453. static int bnx2x_req_msix_irqs(struct bnx2x *bp)
  1454. {
  1455. int i, rc, offset = 0;
  1456. /* no default status block for vf */
  1457. if (IS_PF(bp)) {
  1458. rc = request_irq(bp->msix_table[offset++].vector,
  1459. bnx2x_msix_sp_int, 0,
  1460. bp->dev->name, bp->dev);
  1461. if (rc) {
  1462. BNX2X_ERR("request sp irq failed\n");
  1463. return -EBUSY;
  1464. }
  1465. }
  1466. if (CNIC_SUPPORT(bp))
  1467. offset++;
  1468. for_each_eth_queue(bp, i) {
  1469. struct bnx2x_fastpath *fp = &bp->fp[i];
  1470. snprintf(fp->name, sizeof(fp->name), "%s-fp-%d",
  1471. bp->dev->name, i);
  1472. rc = request_irq(bp->msix_table[offset].vector,
  1473. bnx2x_msix_fp_int, 0, fp->name, fp);
  1474. if (rc) {
  1475. BNX2X_ERR("request fp #%d irq (%d) failed rc %d\n", i,
  1476. bp->msix_table[offset].vector, rc);
  1477. bnx2x_free_msix_irqs(bp, offset);
  1478. return -EBUSY;
  1479. }
  1480. offset++;
  1481. }
  1482. i = BNX2X_NUM_ETH_QUEUES(bp);
  1483. if (IS_PF(bp)) {
  1484. offset = 1 + CNIC_SUPPORT(bp);
  1485. netdev_info(bp->dev,
  1486. "using MSI-X IRQs: sp %d fp[%d] %d ... fp[%d] %d\n",
  1487. bp->msix_table[0].vector,
  1488. 0, bp->msix_table[offset].vector,
  1489. i - 1, bp->msix_table[offset + i - 1].vector);
  1490. } else {
  1491. offset = CNIC_SUPPORT(bp);
  1492. netdev_info(bp->dev,
  1493. "using MSI-X IRQs: fp[%d] %d ... fp[%d] %d\n",
  1494. 0, bp->msix_table[offset].vector,
  1495. i - 1, bp->msix_table[offset + i - 1].vector);
  1496. }
  1497. return 0;
  1498. }
  1499. int bnx2x_enable_msi(struct bnx2x *bp)
  1500. {
  1501. int rc;
  1502. rc = pci_enable_msi(bp->pdev);
  1503. if (rc) {
  1504. BNX2X_DEV_INFO("MSI is not attainable\n");
  1505. return -1;
  1506. }
  1507. bp->flags |= USING_MSI_FLAG;
  1508. return 0;
  1509. }
  1510. static int bnx2x_req_irq(struct bnx2x *bp)
  1511. {
  1512. unsigned long flags;
  1513. unsigned int irq;
  1514. if (bp->flags & (USING_MSI_FLAG | USING_MSIX_FLAG))
  1515. flags = 0;
  1516. else
  1517. flags = IRQF_SHARED;
  1518. if (bp->flags & USING_MSIX_FLAG)
  1519. irq = bp->msix_table[0].vector;
  1520. else
  1521. irq = bp->pdev->irq;
  1522. return request_irq(irq, bnx2x_interrupt, flags, bp->dev->name, bp->dev);
  1523. }
  1524. static int bnx2x_setup_irqs(struct bnx2x *bp)
  1525. {
  1526. int rc = 0;
  1527. if (bp->flags & USING_MSIX_FLAG &&
  1528. !(bp->flags & USING_SINGLE_MSIX_FLAG)) {
  1529. rc = bnx2x_req_msix_irqs(bp);
  1530. if (rc)
  1531. return rc;
  1532. } else {
  1533. rc = bnx2x_req_irq(bp);
  1534. if (rc) {
  1535. BNX2X_ERR("IRQ request failed rc %d, aborting\n", rc);
  1536. return rc;
  1537. }
  1538. if (bp->flags & USING_MSI_FLAG) {
  1539. bp->dev->irq = bp->pdev->irq;
  1540. netdev_info(bp->dev, "using MSI IRQ %d\n",
  1541. bp->dev->irq);
  1542. }
  1543. if (bp->flags & USING_MSIX_FLAG) {
  1544. bp->dev->irq = bp->msix_table[0].vector;
  1545. netdev_info(bp->dev, "using MSIX IRQ %d\n",
  1546. bp->dev->irq);
  1547. }
  1548. }
  1549. return 0;
  1550. }
  1551. static void bnx2x_napi_enable_cnic(struct bnx2x *bp)
  1552. {
  1553. int i;
  1554. for_each_rx_queue_cnic(bp, i) {
  1555. napi_enable(&bnx2x_fp(bp, i, napi));
  1556. }
  1557. }
  1558. static void bnx2x_napi_enable(struct bnx2x *bp)
  1559. {
  1560. int i;
  1561. for_each_eth_queue(bp, i) {
  1562. napi_enable(&bnx2x_fp(bp, i, napi));
  1563. }
  1564. }
  1565. static void bnx2x_napi_disable_cnic(struct bnx2x *bp)
  1566. {
  1567. int i;
  1568. for_each_rx_queue_cnic(bp, i) {
  1569. napi_disable(&bnx2x_fp(bp, i, napi));
  1570. }
  1571. }
  1572. static void bnx2x_napi_disable(struct bnx2x *bp)
  1573. {
  1574. int i;
  1575. for_each_eth_queue(bp, i) {
  1576. napi_disable(&bnx2x_fp(bp, i, napi));
  1577. }
  1578. }
  1579. void bnx2x_netif_start(struct bnx2x *bp)
  1580. {
  1581. if (netif_running(bp->dev)) {
  1582. bnx2x_napi_enable(bp);
  1583. if (CNIC_LOADED(bp))
  1584. bnx2x_napi_enable_cnic(bp);
  1585. bnx2x_int_enable(bp);
  1586. if (bp->state == BNX2X_STATE_OPEN)
  1587. netif_tx_wake_all_queues(bp->dev);
  1588. }
  1589. }
  1590. void bnx2x_netif_stop(struct bnx2x *bp, int disable_hw)
  1591. {
  1592. bnx2x_int_disable_sync(bp, disable_hw);
  1593. bnx2x_napi_disable(bp);
  1594. if (CNIC_LOADED(bp))
  1595. bnx2x_napi_disable_cnic(bp);
  1596. }
  1597. u16 bnx2x_select_queue(struct net_device *dev, struct sk_buff *skb,
  1598. void *accel_priv, select_queue_fallback_t fallback)
  1599. {
  1600. struct bnx2x *bp = netdev_priv(dev);
  1601. if (CNIC_LOADED(bp) && !NO_FCOE(bp)) {
  1602. struct ethhdr *hdr = (struct ethhdr *)skb->data;
  1603. u16 ether_type = ntohs(hdr->h_proto);
  1604. /* Skip VLAN tag if present */
  1605. if (ether_type == ETH_P_8021Q) {
  1606. struct vlan_ethhdr *vhdr =
  1607. (struct vlan_ethhdr *)skb->data;
  1608. ether_type = ntohs(vhdr->h_vlan_encapsulated_proto);
  1609. }
  1610. /* If ethertype is FCoE or FIP - use FCoE ring */
  1611. if ((ether_type == ETH_P_FCOE) || (ether_type == ETH_P_FIP))
  1612. return bnx2x_fcoe_tx(bp, txq_index);
  1613. }
  1614. /* select a non-FCoE queue */
  1615. return fallback(dev, skb) % (BNX2X_NUM_ETH_QUEUES(bp) * bp->max_cos);
  1616. }
  1617. void bnx2x_set_num_queues(struct bnx2x *bp)
  1618. {
  1619. /* RSS queues */
  1620. bp->num_ethernet_queues = bnx2x_calc_num_queues(bp);
  1621. /* override in STORAGE SD modes */
  1622. if (IS_MF_STORAGE_ONLY(bp))
  1623. bp->num_ethernet_queues = 1;
  1624. /* Add special queues */
  1625. bp->num_cnic_queues = CNIC_SUPPORT(bp); /* For FCOE */
  1626. bp->num_queues = bp->num_ethernet_queues + bp->num_cnic_queues;
  1627. BNX2X_DEV_INFO("set number of queues to %d\n", bp->num_queues);
  1628. }
  1629. /**
  1630. * bnx2x_set_real_num_queues - configure netdev->real_num_[tx,rx]_queues
  1631. *
  1632. * @bp: Driver handle
  1633. *
  1634. * We currently support for at most 16 Tx queues for each CoS thus we will
  1635. * allocate a multiple of 16 for ETH L2 rings according to the value of the
  1636. * bp->max_cos.
  1637. *
  1638. * If there is an FCoE L2 queue the appropriate Tx queue will have the next
  1639. * index after all ETH L2 indices.
  1640. *
  1641. * If the actual number of Tx queues (for each CoS) is less than 16 then there
  1642. * will be the holes at the end of each group of 16 ETh L2 indices (0..15,
  1643. * 16..31,...) with indices that are not coupled with any real Tx queue.
  1644. *
  1645. * The proper configuration of skb->queue_mapping is handled by
  1646. * bnx2x_select_queue() and __skb_tx_hash().
  1647. *
  1648. * bnx2x_setup_tc() takes care of the proper TC mappings so that __skb_tx_hash()
  1649. * will return a proper Tx index if TC is enabled (netdev->num_tc > 0).
  1650. */
  1651. static int bnx2x_set_real_num_queues(struct bnx2x *bp, int include_cnic)
  1652. {
  1653. int rc, tx, rx;
  1654. tx = BNX2X_NUM_ETH_QUEUES(bp) * bp->max_cos;
  1655. rx = BNX2X_NUM_ETH_QUEUES(bp);
  1656. /* account for fcoe queue */
  1657. if (include_cnic && !NO_FCOE(bp)) {
  1658. rx++;
  1659. tx++;
  1660. }
  1661. rc = netif_set_real_num_tx_queues(bp->dev, tx);
  1662. if (rc) {
  1663. BNX2X_ERR("Failed to set real number of Tx queues: %d\n", rc);
  1664. return rc;
  1665. }
  1666. rc = netif_set_real_num_rx_queues(bp->dev, rx);
  1667. if (rc) {
  1668. BNX2X_ERR("Failed to set real number of Rx queues: %d\n", rc);
  1669. return rc;
  1670. }
  1671. DP(NETIF_MSG_IFUP, "Setting real num queues to (tx, rx) (%d, %d)\n",
  1672. tx, rx);
  1673. return rc;
  1674. }
  1675. static void bnx2x_set_rx_buf_size(struct bnx2x *bp)
  1676. {
  1677. int i;
  1678. for_each_queue(bp, i) {
  1679. struct bnx2x_fastpath *fp = &bp->fp[i];
  1680. u32 mtu;
  1681. /* Always use a mini-jumbo MTU for the FCoE L2 ring */
  1682. if (IS_FCOE_IDX(i))
  1683. /*
  1684. * Although there are no IP frames expected to arrive to
  1685. * this ring we still want to add an
  1686. * IP_HEADER_ALIGNMENT_PADDING to prevent a buffer
  1687. * overrun attack.
  1688. */
  1689. mtu = BNX2X_FCOE_MINI_JUMBO_MTU;
  1690. else
  1691. mtu = bp->dev->mtu;
  1692. fp->rx_buf_size = BNX2X_FW_RX_ALIGN_START +
  1693. IP_HEADER_ALIGNMENT_PADDING +
  1694. ETH_OVREHEAD +
  1695. mtu +
  1696. BNX2X_FW_RX_ALIGN_END;
  1697. fp->rx_buf_size = SKB_DATA_ALIGN(fp->rx_buf_size);
  1698. /* Note : rx_buf_size doesn't take into account NET_SKB_PAD */
  1699. if (fp->rx_buf_size + NET_SKB_PAD <= PAGE_SIZE)
  1700. fp->rx_frag_size = fp->rx_buf_size + NET_SKB_PAD;
  1701. else
  1702. fp->rx_frag_size = 0;
  1703. }
  1704. }
  1705. static int bnx2x_init_rss(struct bnx2x *bp)
  1706. {
  1707. int i;
  1708. u8 num_eth_queues = BNX2X_NUM_ETH_QUEUES(bp);
  1709. /* Prepare the initial contents for the indirection table if RSS is
  1710. * enabled
  1711. */
  1712. for (i = 0; i < sizeof(bp->rss_conf_obj.ind_table); i++)
  1713. bp->rss_conf_obj.ind_table[i] =
  1714. bp->fp->cl_id +
  1715. ethtool_rxfh_indir_default(i, num_eth_queues);
  1716. /*
  1717. * For 57710 and 57711 SEARCHER configuration (rss_keys) is
  1718. * per-port, so if explicit configuration is needed , do it only
  1719. * for a PMF.
  1720. *
  1721. * For 57712 and newer on the other hand it's a per-function
  1722. * configuration.
  1723. */
  1724. return bnx2x_config_rss_eth(bp, bp->port.pmf || !CHIP_IS_E1x(bp));
  1725. }
  1726. int bnx2x_rss(struct bnx2x *bp, struct bnx2x_rss_config_obj *rss_obj,
  1727. bool config_hash, bool enable)
  1728. {
  1729. struct bnx2x_config_rss_params params = {NULL};
  1730. /* Although RSS is meaningless when there is a single HW queue we
  1731. * still need it enabled in order to have HW Rx hash generated.
  1732. *
  1733. * if (!is_eth_multi(bp))
  1734. * bp->multi_mode = ETH_RSS_MODE_DISABLED;
  1735. */
  1736. params.rss_obj = rss_obj;
  1737. __set_bit(RAMROD_COMP_WAIT, &params.ramrod_flags);
  1738. if (enable) {
  1739. __set_bit(BNX2X_RSS_MODE_REGULAR, &params.rss_flags);
  1740. /* RSS configuration */
  1741. __set_bit(BNX2X_RSS_IPV4, &params.rss_flags);
  1742. __set_bit(BNX2X_RSS_IPV4_TCP, &params.rss_flags);
  1743. __set_bit(BNX2X_RSS_IPV6, &params.rss_flags);
  1744. __set_bit(BNX2X_RSS_IPV6_TCP, &params.rss_flags);
  1745. if (rss_obj->udp_rss_v4)
  1746. __set_bit(BNX2X_RSS_IPV4_UDP, &params.rss_flags);
  1747. if (rss_obj->udp_rss_v6)
  1748. __set_bit(BNX2X_RSS_IPV6_UDP, &params.rss_flags);
  1749. if (!CHIP_IS_E1x(bp)) {
  1750. /* valid only for TUNN_MODE_VXLAN tunnel mode */
  1751. __set_bit(BNX2X_RSS_IPV4_VXLAN, &params.rss_flags);
  1752. __set_bit(BNX2X_RSS_IPV6_VXLAN, &params.rss_flags);
  1753. /* valid only for TUNN_MODE_GRE tunnel mode */
  1754. __set_bit(BNX2X_RSS_TUNN_INNER_HDRS, &params.rss_flags);
  1755. }
  1756. } else {
  1757. __set_bit(BNX2X_RSS_MODE_DISABLED, &params.rss_flags);
  1758. }
  1759. /* Hash bits */
  1760. params.rss_result_mask = MULTI_MASK;
  1761. memcpy(params.ind_table, rss_obj->ind_table, sizeof(params.ind_table));
  1762. if (config_hash) {
  1763. /* RSS keys */
  1764. netdev_rss_key_fill(params.rss_key, T_ETH_RSS_KEY * 4);
  1765. __set_bit(BNX2X_RSS_SET_SRCH, &params.rss_flags);
  1766. }
  1767. if (IS_PF(bp))
  1768. return bnx2x_config_rss(bp, &params);
  1769. else
  1770. return bnx2x_vfpf_config_rss(bp, &params);
  1771. }
  1772. static int bnx2x_init_hw(struct bnx2x *bp, u32 load_code)
  1773. {
  1774. struct bnx2x_func_state_params func_params = {NULL};
  1775. /* Prepare parameters for function state transitions */
  1776. __set_bit(RAMROD_COMP_WAIT, &func_params.ramrod_flags);
  1777. func_params.f_obj = &bp->func_obj;
  1778. func_params.cmd = BNX2X_F_CMD_HW_INIT;
  1779. func_params.params.hw_init.load_phase = load_code;
  1780. return bnx2x_func_state_change(bp, &func_params);
  1781. }
  1782. /*
  1783. * Cleans the object that have internal lists without sending
  1784. * ramrods. Should be run when interrupts are disabled.
  1785. */
  1786. void bnx2x_squeeze_objects(struct bnx2x *bp)
  1787. {
  1788. int rc;
  1789. unsigned long ramrod_flags = 0, vlan_mac_flags = 0;
  1790. struct bnx2x_mcast_ramrod_params rparam = {NULL};
  1791. struct bnx2x_vlan_mac_obj *mac_obj = &bp->sp_objs->mac_obj;
  1792. /***************** Cleanup MACs' object first *************************/
  1793. /* Wait for completion of requested */
  1794. __set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
  1795. /* Perform a dry cleanup */
  1796. __set_bit(RAMROD_DRV_CLR_ONLY, &ramrod_flags);
  1797. /* Clean ETH primary MAC */
  1798. __set_bit(BNX2X_ETH_MAC, &vlan_mac_flags);
  1799. rc = mac_obj->delete_all(bp, &bp->sp_objs->mac_obj, &vlan_mac_flags,
  1800. &ramrod_flags);
  1801. if (rc != 0)
  1802. BNX2X_ERR("Failed to clean ETH MACs: %d\n", rc);
  1803. /* Cleanup UC list */
  1804. vlan_mac_flags = 0;
  1805. __set_bit(BNX2X_UC_LIST_MAC, &vlan_mac_flags);
  1806. rc = mac_obj->delete_all(bp, mac_obj, &vlan_mac_flags,
  1807. &ramrod_flags);
  1808. if (rc != 0)
  1809. BNX2X_ERR("Failed to clean UC list MACs: %d\n", rc);
  1810. /***************** Now clean mcast object *****************************/
  1811. rparam.mcast_obj = &bp->mcast_obj;
  1812. __set_bit(RAMROD_DRV_CLR_ONLY, &rparam.ramrod_flags);
  1813. /* Add a DEL command... - Since we're doing a driver cleanup only,
  1814. * we take a lock surrounding both the initial send and the CONTs,
  1815. * as we don't want a true completion to disrupt us in the middle.
  1816. */
  1817. netif_addr_lock_bh(bp->dev);
  1818. rc = bnx2x_config_mcast(bp, &rparam, BNX2X_MCAST_CMD_DEL);
  1819. if (rc < 0)
  1820. BNX2X_ERR("Failed to add a new DEL command to a multi-cast object: %d\n",
  1821. rc);
  1822. /* ...and wait until all pending commands are cleared */
  1823. rc = bnx2x_config_mcast(bp, &rparam, BNX2X_MCAST_CMD_CONT);
  1824. while (rc != 0) {
  1825. if (rc < 0) {
  1826. BNX2X_ERR("Failed to clean multi-cast object: %d\n",
  1827. rc);
  1828. netif_addr_unlock_bh(bp->dev);
  1829. return;
  1830. }
  1831. rc = bnx2x_config_mcast(bp, &rparam, BNX2X_MCAST_CMD_CONT);
  1832. }
  1833. netif_addr_unlock_bh(bp->dev);
  1834. }
  1835. #ifndef BNX2X_STOP_ON_ERROR
  1836. #define LOAD_ERROR_EXIT(bp, label) \
  1837. do { \
  1838. (bp)->state = BNX2X_STATE_ERROR; \
  1839. goto label; \
  1840. } while (0)
  1841. #define LOAD_ERROR_EXIT_CNIC(bp, label) \
  1842. do { \
  1843. bp->cnic_loaded = false; \
  1844. goto label; \
  1845. } while (0)
  1846. #else /*BNX2X_STOP_ON_ERROR*/
  1847. #define LOAD_ERROR_EXIT(bp, label) \
  1848. do { \
  1849. (bp)->state = BNX2X_STATE_ERROR; \
  1850. (bp)->panic = 1; \
  1851. return -EBUSY; \
  1852. } while (0)
  1853. #define LOAD_ERROR_EXIT_CNIC(bp, label) \
  1854. do { \
  1855. bp->cnic_loaded = false; \
  1856. (bp)->panic = 1; \
  1857. return -EBUSY; \
  1858. } while (0)
  1859. #endif /*BNX2X_STOP_ON_ERROR*/
  1860. static void bnx2x_free_fw_stats_mem(struct bnx2x *bp)
  1861. {
  1862. BNX2X_PCI_FREE(bp->fw_stats, bp->fw_stats_mapping,
  1863. bp->fw_stats_data_sz + bp->fw_stats_req_sz);
  1864. return;
  1865. }
  1866. static int bnx2x_alloc_fw_stats_mem(struct bnx2x *bp)
  1867. {
  1868. int num_groups, vf_headroom = 0;
  1869. int is_fcoe_stats = NO_FCOE(bp) ? 0 : 1;
  1870. /* number of queues for statistics is number of eth queues + FCoE */
  1871. u8 num_queue_stats = BNX2X_NUM_ETH_QUEUES(bp) + is_fcoe_stats;
  1872. /* Total number of FW statistics requests =
  1873. * 1 for port stats + 1 for PF stats + potential 2 for FCoE (fcoe proper
  1874. * and fcoe l2 queue) stats + num of queues (which includes another 1
  1875. * for fcoe l2 queue if applicable)
  1876. */
  1877. bp->fw_stats_num = 2 + is_fcoe_stats + num_queue_stats;
  1878. /* vf stats appear in the request list, but their data is allocated by
  1879. * the VFs themselves. We don't include them in the bp->fw_stats_num as
  1880. * it is used to determine where to place the vf stats queries in the
  1881. * request struct
  1882. */
  1883. if (IS_SRIOV(bp))
  1884. vf_headroom = bnx2x_vf_headroom(bp);
  1885. /* Request is built from stats_query_header and an array of
  1886. * stats_query_cmd_group each of which contains
  1887. * STATS_QUERY_CMD_COUNT rules. The real number or requests is
  1888. * configured in the stats_query_header.
  1889. */
  1890. num_groups =
  1891. (((bp->fw_stats_num + vf_headroom) / STATS_QUERY_CMD_COUNT) +
  1892. (((bp->fw_stats_num + vf_headroom) % STATS_QUERY_CMD_COUNT) ?
  1893. 1 : 0));
  1894. DP(BNX2X_MSG_SP, "stats fw_stats_num %d, vf headroom %d, num_groups %d\n",
  1895. bp->fw_stats_num, vf_headroom, num_groups);
  1896. bp->fw_stats_req_sz = sizeof(struct stats_query_header) +
  1897. num_groups * sizeof(struct stats_query_cmd_group);
  1898. /* Data for statistics requests + stats_counter
  1899. * stats_counter holds per-STORM counters that are incremented
  1900. * when STORM has finished with the current request.
  1901. * memory for FCoE offloaded statistics are counted anyway,
  1902. * even if they will not be sent.
  1903. * VF stats are not accounted for here as the data of VF stats is stored
  1904. * in memory allocated by the VF, not here.
  1905. */
  1906. bp->fw_stats_data_sz = sizeof(struct per_port_stats) +
  1907. sizeof(struct per_pf_stats) +
  1908. sizeof(struct fcoe_statistics_params) +
  1909. sizeof(struct per_queue_stats) * num_queue_stats +
  1910. sizeof(struct stats_counter);
  1911. bp->fw_stats = BNX2X_PCI_ALLOC(&bp->fw_stats_mapping,
  1912. bp->fw_stats_data_sz + bp->fw_stats_req_sz);
  1913. if (!bp->fw_stats)
  1914. goto alloc_mem_err;
  1915. /* Set shortcuts */
  1916. bp->fw_stats_req = (struct bnx2x_fw_stats_req *)bp->fw_stats;
  1917. bp->fw_stats_req_mapping = bp->fw_stats_mapping;
  1918. bp->fw_stats_data = (struct bnx2x_fw_stats_data *)
  1919. ((u8 *)bp->fw_stats + bp->fw_stats_req_sz);
  1920. bp->fw_stats_data_mapping = bp->fw_stats_mapping +
  1921. bp->fw_stats_req_sz;
  1922. DP(BNX2X_MSG_SP, "statistics request base address set to %x %x\n",
  1923. U64_HI(bp->fw_stats_req_mapping),
  1924. U64_LO(bp->fw_stats_req_mapping));
  1925. DP(BNX2X_MSG_SP, "statistics data base address set to %x %x\n",
  1926. U64_HI(bp->fw_stats_data_mapping),
  1927. U64_LO(bp->fw_stats_data_mapping));
  1928. return 0;
  1929. alloc_mem_err:
  1930. bnx2x_free_fw_stats_mem(bp);
  1931. BNX2X_ERR("Can't allocate FW stats memory\n");
  1932. return -ENOMEM;
  1933. }
  1934. /* send load request to mcp and analyze response */
  1935. static int bnx2x_nic_load_request(struct bnx2x *bp, u32 *load_code)
  1936. {
  1937. u32 param;
  1938. /* init fw_seq */
  1939. bp->fw_seq =
  1940. (SHMEM_RD(bp, func_mb[BP_FW_MB_IDX(bp)].drv_mb_header) &
  1941. DRV_MSG_SEQ_NUMBER_MASK);
  1942. BNX2X_DEV_INFO("fw_seq 0x%08x\n", bp->fw_seq);
  1943. /* Get current FW pulse sequence */
  1944. bp->fw_drv_pulse_wr_seq =
  1945. (SHMEM_RD(bp, func_mb[BP_FW_MB_IDX(bp)].drv_pulse_mb) &
  1946. DRV_PULSE_SEQ_MASK);
  1947. BNX2X_DEV_INFO("drv_pulse 0x%x\n", bp->fw_drv_pulse_wr_seq);
  1948. param = DRV_MSG_CODE_LOAD_REQ_WITH_LFA;
  1949. if (IS_MF_SD(bp) && bnx2x_port_after_undi(bp))
  1950. param |= DRV_MSG_CODE_LOAD_REQ_FORCE_LFA;
  1951. /* load request */
  1952. (*load_code) = bnx2x_fw_command(bp, DRV_MSG_CODE_LOAD_REQ, param);
  1953. /* if mcp fails to respond we must abort */
  1954. if (!(*load_code)) {
  1955. BNX2X_ERR("MCP response failure, aborting\n");
  1956. return -EBUSY;
  1957. }
  1958. /* If mcp refused (e.g. other port is in diagnostic mode) we
  1959. * must abort
  1960. */
  1961. if ((*load_code) == FW_MSG_CODE_DRV_LOAD_REFUSED) {
  1962. BNX2X_ERR("MCP refused load request, aborting\n");
  1963. return -EBUSY;
  1964. }
  1965. return 0;
  1966. }
  1967. /* check whether another PF has already loaded FW to chip. In
  1968. * virtualized environments a pf from another VM may have already
  1969. * initialized the device including loading FW
  1970. */
  1971. int bnx2x_compare_fw_ver(struct bnx2x *bp, u32 load_code, bool print_err)
  1972. {
  1973. /* is another pf loaded on this engine? */
  1974. if (load_code != FW_MSG_CODE_DRV_LOAD_COMMON_CHIP &&
  1975. load_code != FW_MSG_CODE_DRV_LOAD_COMMON) {
  1976. /* build my FW version dword */
  1977. /*(DEBLOBBED)*/
  1978. /* read loaded FW from chip */
  1979. u32 loaded_fw = REG_RD(bp, XSEM_REG_PRAM);
  1980. u32 my_fw = ~loaded_fw;
  1981. DP(BNX2X_MSG_SP, "loaded fw %x, my fw %x\n",
  1982. loaded_fw, my_fw);
  1983. /* abort nic load if version mismatch */
  1984. if (my_fw != loaded_fw) {
  1985. if (print_err)
  1986. BNX2X_ERR("bnx2x with FW %x was already loaded which mismatches my %x FW. Aborting\n",
  1987. loaded_fw, my_fw);
  1988. else
  1989. BNX2X_DEV_INFO("bnx2x with FW %x was already loaded which mismatches my %x FW, possibly due to MF UNDI\n",
  1990. loaded_fw, my_fw);
  1991. return -EBUSY;
  1992. }
  1993. }
  1994. return 0;
  1995. }
  1996. /* returns the "mcp load_code" according to global load_count array */
  1997. static int bnx2x_nic_load_no_mcp(struct bnx2x *bp, int port)
  1998. {
  1999. int path = BP_PATH(bp);
  2000. DP(NETIF_MSG_IFUP, "NO MCP - load counts[%d] %d, %d, %d\n",
  2001. path, bnx2x_load_count[path][0], bnx2x_load_count[path][1],
  2002. bnx2x_load_count[path][2]);
  2003. bnx2x_load_count[path][0]++;
  2004. bnx2x_load_count[path][1 + port]++;
  2005. DP(NETIF_MSG_IFUP, "NO MCP - new load counts[%d] %d, %d, %d\n",
  2006. path, bnx2x_load_count[path][0], bnx2x_load_count[path][1],
  2007. bnx2x_load_count[path][2]);
  2008. if (bnx2x_load_count[path][0] == 1)
  2009. return FW_MSG_CODE_DRV_LOAD_COMMON;
  2010. else if (bnx2x_load_count[path][1 + port] == 1)
  2011. return FW_MSG_CODE_DRV_LOAD_PORT;
  2012. else
  2013. return FW_MSG_CODE_DRV_LOAD_FUNCTION;
  2014. }
  2015. /* mark PMF if applicable */
  2016. static void bnx2x_nic_load_pmf(struct bnx2x *bp, u32 load_code)
  2017. {
  2018. if ((load_code == FW_MSG_CODE_DRV_LOAD_COMMON) ||
  2019. (load_code == FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) ||
  2020. (load_code == FW_MSG_CODE_DRV_LOAD_PORT)) {
  2021. bp->port.pmf = 1;
  2022. /* We need the barrier to ensure the ordering between the
  2023. * writing to bp->port.pmf here and reading it from the
  2024. * bnx2x_periodic_task().
  2025. */
  2026. smp_mb();
  2027. } else {
  2028. bp->port.pmf = 0;
  2029. }
  2030. DP(NETIF_MSG_LINK, "pmf %d\n", bp->port.pmf);
  2031. }
  2032. static void bnx2x_nic_load_afex_dcc(struct bnx2x *bp, int load_code)
  2033. {
  2034. if (((load_code == FW_MSG_CODE_DRV_LOAD_COMMON) ||
  2035. (load_code == FW_MSG_CODE_DRV_LOAD_COMMON_CHIP)) &&
  2036. (bp->common.shmem2_base)) {
  2037. if (SHMEM2_HAS(bp, dcc_support))
  2038. SHMEM2_WR(bp, dcc_support,
  2039. (SHMEM_DCC_SUPPORT_DISABLE_ENABLE_PF_TLV |
  2040. SHMEM_DCC_SUPPORT_BANDWIDTH_ALLOCATION_TLV));
  2041. if (SHMEM2_HAS(bp, afex_driver_support))
  2042. SHMEM2_WR(bp, afex_driver_support,
  2043. SHMEM_AFEX_SUPPORTED_VERSION_ONE);
  2044. }
  2045. /* Set AFEX default VLAN tag to an invalid value */
  2046. bp->afex_def_vlan_tag = -1;
  2047. }
  2048. /**
  2049. * bnx2x_bz_fp - zero content of the fastpath structure.
  2050. *
  2051. * @bp: driver handle
  2052. * @index: fastpath index to be zeroed
  2053. *
  2054. * Makes sure the contents of the bp->fp[index].napi is kept
  2055. * intact.
  2056. */
  2057. static void bnx2x_bz_fp(struct bnx2x *bp, int index)
  2058. {
  2059. struct bnx2x_fastpath *fp = &bp->fp[index];
  2060. int cos;
  2061. struct napi_struct orig_napi = fp->napi;
  2062. struct bnx2x_agg_info *orig_tpa_info = fp->tpa_info;
  2063. /* bzero bnx2x_fastpath contents */
  2064. if (fp->tpa_info)
  2065. memset(fp->tpa_info, 0, ETH_MAX_AGGREGATION_QUEUES_E1H_E2 *
  2066. sizeof(struct bnx2x_agg_info));
  2067. memset(fp, 0, sizeof(*fp));
  2068. /* Restore the NAPI object as it has been already initialized */
  2069. fp->napi = orig_napi;
  2070. fp->tpa_info = orig_tpa_info;
  2071. fp->bp = bp;
  2072. fp->index = index;
  2073. if (IS_ETH_FP(fp))
  2074. fp->max_cos = bp->max_cos;
  2075. else
  2076. /* Special queues support only one CoS */
  2077. fp->max_cos = 1;
  2078. /* Init txdata pointers */
  2079. if (IS_FCOE_FP(fp))
  2080. fp->txdata_ptr[0] = &bp->bnx2x_txq[FCOE_TXQ_IDX(bp)];
  2081. if (IS_ETH_FP(fp))
  2082. for_each_cos_in_tx_queue(fp, cos)
  2083. fp->txdata_ptr[cos] = &bp->bnx2x_txq[cos *
  2084. BNX2X_NUM_ETH_QUEUES(bp) + index];
  2085. /* set the tpa flag for each queue. The tpa flag determines the queue
  2086. * minimal size so it must be set prior to queue memory allocation
  2087. */
  2088. if (bp->dev->features & NETIF_F_LRO)
  2089. fp->mode = TPA_MODE_LRO;
  2090. else if (bp->dev->features & NETIF_F_GRO &&
  2091. bnx2x_mtu_allows_gro(bp->dev->mtu))
  2092. fp->mode = TPA_MODE_GRO;
  2093. else
  2094. fp->mode = TPA_MODE_DISABLED;
  2095. /* We don't want TPA if it's disabled in bp
  2096. * or if this is an FCoE L2 ring.
  2097. */
  2098. if (bp->disable_tpa || IS_FCOE_FP(fp))
  2099. fp->mode = TPA_MODE_DISABLED;
  2100. }
  2101. void bnx2x_set_os_driver_state(struct bnx2x *bp, u32 state)
  2102. {
  2103. u32 cur;
  2104. if (!IS_MF_BD(bp) || !SHMEM2_HAS(bp, os_driver_state) || IS_VF(bp))
  2105. return;
  2106. cur = SHMEM2_RD(bp, os_driver_state[BP_FW_MB_IDX(bp)]);
  2107. DP(NETIF_MSG_IFUP, "Driver state %08x-->%08x\n",
  2108. cur, state);
  2109. SHMEM2_WR(bp, os_driver_state[BP_FW_MB_IDX(bp)], state);
  2110. }
  2111. int bnx2x_load_cnic(struct bnx2x *bp)
  2112. {
  2113. int i, rc, port = BP_PORT(bp);
  2114. DP(NETIF_MSG_IFUP, "Starting CNIC-related load\n");
  2115. mutex_init(&bp->cnic_mutex);
  2116. if (IS_PF(bp)) {
  2117. rc = bnx2x_alloc_mem_cnic(bp);
  2118. if (rc) {
  2119. BNX2X_ERR("Unable to allocate bp memory for cnic\n");
  2120. LOAD_ERROR_EXIT_CNIC(bp, load_error_cnic0);
  2121. }
  2122. }
  2123. rc = bnx2x_alloc_fp_mem_cnic(bp);
  2124. if (rc) {
  2125. BNX2X_ERR("Unable to allocate memory for cnic fps\n");
  2126. LOAD_ERROR_EXIT_CNIC(bp, load_error_cnic0);
  2127. }
  2128. /* Update the number of queues with the cnic queues */
  2129. rc = bnx2x_set_real_num_queues(bp, 1);
  2130. if (rc) {
  2131. BNX2X_ERR("Unable to set real_num_queues including cnic\n");
  2132. LOAD_ERROR_EXIT_CNIC(bp, load_error_cnic0);
  2133. }
  2134. /* Add all CNIC NAPI objects */
  2135. bnx2x_add_all_napi_cnic(bp);
  2136. DP(NETIF_MSG_IFUP, "cnic napi added\n");
  2137. bnx2x_napi_enable_cnic(bp);
  2138. rc = bnx2x_init_hw_func_cnic(bp);
  2139. if (rc)
  2140. LOAD_ERROR_EXIT_CNIC(bp, load_error_cnic1);
  2141. bnx2x_nic_init_cnic(bp);
  2142. if (IS_PF(bp)) {
  2143. /* Enable Timer scan */
  2144. REG_WR(bp, TM_REG_EN_LINEAR0_TIMER + port*4, 1);
  2145. /* setup cnic queues */
  2146. for_each_cnic_queue(bp, i) {
  2147. rc = bnx2x_setup_queue(bp, &bp->fp[i], 0);
  2148. if (rc) {
  2149. BNX2X_ERR("Queue setup failed\n");
  2150. LOAD_ERROR_EXIT(bp, load_error_cnic2);
  2151. }
  2152. }
  2153. }
  2154. /* Initialize Rx filter. */
  2155. bnx2x_set_rx_mode_inner(bp);
  2156. /* re-read iscsi info */
  2157. bnx2x_get_iscsi_info(bp);
  2158. bnx2x_setup_cnic_irq_info(bp);
  2159. bnx2x_setup_cnic_info(bp);
  2160. bp->cnic_loaded = true;
  2161. if (bp->state == BNX2X_STATE_OPEN)
  2162. bnx2x_cnic_notify(bp, CNIC_CTL_START_CMD);
  2163. DP(NETIF_MSG_IFUP, "Ending successfully CNIC-related load\n");
  2164. return 0;
  2165. #ifndef BNX2X_STOP_ON_ERROR
  2166. load_error_cnic2:
  2167. /* Disable Timer scan */
  2168. REG_WR(bp, TM_REG_EN_LINEAR0_TIMER + port*4, 0);
  2169. load_error_cnic1:
  2170. bnx2x_napi_disable_cnic(bp);
  2171. /* Update the number of queues without the cnic queues */
  2172. if (bnx2x_set_real_num_queues(bp, 0))
  2173. BNX2X_ERR("Unable to set real_num_queues not including cnic\n");
  2174. load_error_cnic0:
  2175. BNX2X_ERR("CNIC-related load failed\n");
  2176. bnx2x_free_fp_mem_cnic(bp);
  2177. bnx2x_free_mem_cnic(bp);
  2178. return rc;
  2179. #endif /* ! BNX2X_STOP_ON_ERROR */
  2180. }
  2181. /* must be called with rtnl_lock */
  2182. int bnx2x_nic_load(struct bnx2x *bp, int load_mode)
  2183. {
  2184. int port = BP_PORT(bp);
  2185. int i, rc = 0, load_code = 0;
  2186. DP(NETIF_MSG_IFUP, "Starting NIC load\n");
  2187. DP(NETIF_MSG_IFUP,
  2188. "CNIC is %s\n", CNIC_ENABLED(bp) ? "enabled" : "disabled");
  2189. #ifdef BNX2X_STOP_ON_ERROR
  2190. if (unlikely(bp->panic)) {
  2191. BNX2X_ERR("Can't load NIC when there is panic\n");
  2192. return -EPERM;
  2193. }
  2194. #endif
  2195. bp->state = BNX2X_STATE_OPENING_WAIT4_LOAD;
  2196. /* zero the structure w/o any lock, before SP handler is initialized */
  2197. memset(&bp->last_reported_link, 0, sizeof(bp->last_reported_link));
  2198. __set_bit(BNX2X_LINK_REPORT_LINK_DOWN,
  2199. &bp->last_reported_link.link_report_flags);
  2200. if (IS_PF(bp))
  2201. /* must be called before memory allocation and HW init */
  2202. bnx2x_ilt_set_info(bp);
  2203. /*
  2204. * Zero fastpath structures preserving invariants like napi, which are
  2205. * allocated only once, fp index, max_cos, bp pointer.
  2206. * Also set fp->mode and txdata_ptr.
  2207. */
  2208. DP(NETIF_MSG_IFUP, "num queues: %d", bp->num_queues);
  2209. for_each_queue(bp, i)
  2210. bnx2x_bz_fp(bp, i);
  2211. memset(bp->bnx2x_txq, 0, (BNX2X_MAX_RSS_COUNT(bp) * BNX2X_MULTI_TX_COS +
  2212. bp->num_cnic_queues) *
  2213. sizeof(struct bnx2x_fp_txdata));
  2214. bp->fcoe_init = false;
  2215. /* Set the receive queues buffer size */
  2216. bnx2x_set_rx_buf_size(bp);
  2217. if (IS_PF(bp)) {
  2218. rc = bnx2x_alloc_mem(bp);
  2219. if (rc) {
  2220. BNX2X_ERR("Unable to allocate bp memory\n");
  2221. return rc;
  2222. }
  2223. }
  2224. /* need to be done after alloc mem, since it's self adjusting to amount
  2225. * of memory available for RSS queues
  2226. */
  2227. rc = bnx2x_alloc_fp_mem(bp);
  2228. if (rc) {
  2229. BNX2X_ERR("Unable to allocate memory for fps\n");
  2230. LOAD_ERROR_EXIT(bp, load_error0);
  2231. }
  2232. /* Allocated memory for FW statistics */
  2233. if (bnx2x_alloc_fw_stats_mem(bp))
  2234. LOAD_ERROR_EXIT(bp, load_error0);
  2235. /* request pf to initialize status blocks */
  2236. if (IS_VF(bp)) {
  2237. rc = bnx2x_vfpf_init(bp);
  2238. if (rc)
  2239. LOAD_ERROR_EXIT(bp, load_error0);
  2240. }
  2241. /* As long as bnx2x_alloc_mem() may possibly update
  2242. * bp->num_queues, bnx2x_set_real_num_queues() should always
  2243. * come after it. At this stage cnic queues are not counted.
  2244. */
  2245. rc = bnx2x_set_real_num_queues(bp, 0);
  2246. if (rc) {
  2247. BNX2X_ERR("Unable to set real_num_queues\n");
  2248. LOAD_ERROR_EXIT(bp, load_error0);
  2249. }
  2250. /* configure multi cos mappings in kernel.
  2251. * this configuration may be overridden by a multi class queue
  2252. * discipline or by a dcbx negotiation result.
  2253. */
  2254. bnx2x_setup_tc(bp->dev, bp->max_cos);
  2255. /* Add all NAPI objects */
  2256. bnx2x_add_all_napi(bp);
  2257. DP(NETIF_MSG_IFUP, "napi added\n");
  2258. bnx2x_napi_enable(bp);
  2259. if (IS_PF(bp)) {
  2260. /* set pf load just before approaching the MCP */
  2261. bnx2x_set_pf_load(bp);
  2262. /* if mcp exists send load request and analyze response */
  2263. if (!BP_NOMCP(bp)) {
  2264. /* attempt to load pf */
  2265. rc = bnx2x_nic_load_request(bp, &load_code);
  2266. if (rc)
  2267. LOAD_ERROR_EXIT(bp, load_error1);
  2268. /* what did mcp say? */
  2269. rc = bnx2x_compare_fw_ver(bp, load_code, true);
  2270. if (rc) {
  2271. bnx2x_fw_command(bp, DRV_MSG_CODE_LOAD_DONE, 0);
  2272. LOAD_ERROR_EXIT(bp, load_error2);
  2273. }
  2274. } else {
  2275. load_code = bnx2x_nic_load_no_mcp(bp, port);
  2276. }
  2277. /* mark pmf if applicable */
  2278. bnx2x_nic_load_pmf(bp, load_code);
  2279. /* Init Function state controlling object */
  2280. bnx2x__init_func_obj(bp);
  2281. /* Initialize HW */
  2282. rc = bnx2x_init_hw(bp, load_code);
  2283. if (rc) {
  2284. BNX2X_ERR("HW init failed, aborting\n");
  2285. bnx2x_fw_command(bp, DRV_MSG_CODE_LOAD_DONE, 0);
  2286. LOAD_ERROR_EXIT(bp, load_error2);
  2287. }
  2288. }
  2289. bnx2x_pre_irq_nic_init(bp);
  2290. /* Connect to IRQs */
  2291. rc = bnx2x_setup_irqs(bp);
  2292. if (rc) {
  2293. BNX2X_ERR("setup irqs failed\n");
  2294. if (IS_PF(bp))
  2295. bnx2x_fw_command(bp, DRV_MSG_CODE_LOAD_DONE, 0);
  2296. LOAD_ERROR_EXIT(bp, load_error2);
  2297. }
  2298. /* Init per-function objects */
  2299. if (IS_PF(bp)) {
  2300. /* Setup NIC internals and enable interrupts */
  2301. bnx2x_post_irq_nic_init(bp, load_code);
  2302. bnx2x_init_bp_objs(bp);
  2303. bnx2x_iov_nic_init(bp);
  2304. /* Set AFEX default VLAN tag to an invalid value */
  2305. bp->afex_def_vlan_tag = -1;
  2306. bnx2x_nic_load_afex_dcc(bp, load_code);
  2307. bp->state = BNX2X_STATE_OPENING_WAIT4_PORT;
  2308. rc = bnx2x_func_start(bp);
  2309. if (rc) {
  2310. BNX2X_ERR("Function start failed!\n");
  2311. bnx2x_fw_command(bp, DRV_MSG_CODE_LOAD_DONE, 0);
  2312. LOAD_ERROR_EXIT(bp, load_error3);
  2313. }
  2314. /* Send LOAD_DONE command to MCP */
  2315. if (!BP_NOMCP(bp)) {
  2316. load_code = bnx2x_fw_command(bp,
  2317. DRV_MSG_CODE_LOAD_DONE, 0);
  2318. if (!load_code) {
  2319. BNX2X_ERR("MCP response failure, aborting\n");
  2320. rc = -EBUSY;
  2321. LOAD_ERROR_EXIT(bp, load_error3);
  2322. }
  2323. }
  2324. /* initialize FW coalescing state machines in RAM */
  2325. bnx2x_update_coalesce(bp);
  2326. }
  2327. /* setup the leading queue */
  2328. rc = bnx2x_setup_leading(bp);
  2329. if (rc) {
  2330. BNX2X_ERR("Setup leading failed!\n");
  2331. LOAD_ERROR_EXIT(bp, load_error3);
  2332. }
  2333. /* set up the rest of the queues */
  2334. for_each_nondefault_eth_queue(bp, i) {
  2335. if (IS_PF(bp))
  2336. rc = bnx2x_setup_queue(bp, &bp->fp[i], false);
  2337. else /* VF */
  2338. rc = bnx2x_vfpf_setup_q(bp, &bp->fp[i], false);
  2339. if (rc) {
  2340. BNX2X_ERR("Queue %d setup failed\n", i);
  2341. LOAD_ERROR_EXIT(bp, load_error3);
  2342. }
  2343. }
  2344. /* setup rss */
  2345. rc = bnx2x_init_rss(bp);
  2346. if (rc) {
  2347. BNX2X_ERR("PF RSS init failed\n");
  2348. LOAD_ERROR_EXIT(bp, load_error3);
  2349. }
  2350. /* Now when Clients are configured we are ready to work */
  2351. bp->state = BNX2X_STATE_OPEN;
  2352. /* Configure a ucast MAC */
  2353. if (IS_PF(bp))
  2354. rc = bnx2x_set_eth_mac(bp, true);
  2355. else /* vf */
  2356. rc = bnx2x_vfpf_config_mac(bp, bp->dev->dev_addr, bp->fp->index,
  2357. true);
  2358. if (rc) {
  2359. BNX2X_ERR("Setting Ethernet MAC failed\n");
  2360. LOAD_ERROR_EXIT(bp, load_error3);
  2361. }
  2362. if (IS_PF(bp) && bp->pending_max) {
  2363. bnx2x_update_max_mf_config(bp, bp->pending_max);
  2364. bp->pending_max = 0;
  2365. }
  2366. if (bp->port.pmf) {
  2367. rc = bnx2x_initial_phy_init(bp, load_mode);
  2368. if (rc)
  2369. LOAD_ERROR_EXIT(bp, load_error3);
  2370. }
  2371. bp->link_params.feature_config_flags &= ~FEATURE_CONFIG_BOOT_FROM_SAN;
  2372. /* Start fast path */
  2373. /* Re-configure vlan filters */
  2374. rc = bnx2x_vlan_reconfigure_vid(bp);
  2375. if (rc)
  2376. LOAD_ERROR_EXIT(bp, load_error3);
  2377. /* Initialize Rx filter. */
  2378. bnx2x_set_rx_mode_inner(bp);
  2379. if (bp->flags & PTP_SUPPORTED) {
  2380. bnx2x_init_ptp(bp);
  2381. bnx2x_configure_ptp_filters(bp);
  2382. }
  2383. /* Start Tx */
  2384. switch (load_mode) {
  2385. case LOAD_NORMAL:
  2386. /* Tx queue should be only re-enabled */
  2387. netif_tx_wake_all_queues(bp->dev);
  2388. break;
  2389. case LOAD_OPEN:
  2390. netif_tx_start_all_queues(bp->dev);
  2391. smp_mb__after_atomic();
  2392. break;
  2393. case LOAD_DIAG:
  2394. case LOAD_LOOPBACK_EXT:
  2395. bp->state = BNX2X_STATE_DIAG;
  2396. break;
  2397. default:
  2398. break;
  2399. }
  2400. if (bp->port.pmf)
  2401. bnx2x_update_drv_flags(bp, 1 << DRV_FLAGS_PORT_MASK, 0);
  2402. else
  2403. bnx2x__link_status_update(bp);
  2404. /* start the timer */
  2405. mod_timer(&bp->timer, jiffies + bp->current_interval);
  2406. if (CNIC_ENABLED(bp))
  2407. bnx2x_load_cnic(bp);
  2408. if (IS_PF(bp))
  2409. bnx2x_schedule_sp_rtnl(bp, BNX2X_SP_RTNL_GET_DRV_VERSION, 0);
  2410. if (IS_PF(bp) && SHMEM2_HAS(bp, drv_capabilities_flag)) {
  2411. /* mark driver is loaded in shmem2 */
  2412. u32 val;
  2413. val = SHMEM2_RD(bp, drv_capabilities_flag[BP_FW_MB_IDX(bp)]);
  2414. val &= ~DRV_FLAGS_MTU_MASK;
  2415. val |= (bp->dev->mtu << DRV_FLAGS_MTU_SHIFT);
  2416. SHMEM2_WR(bp, drv_capabilities_flag[BP_FW_MB_IDX(bp)],
  2417. val | DRV_FLAGS_CAPABILITIES_LOADED_SUPPORTED |
  2418. DRV_FLAGS_CAPABILITIES_LOADED_L2);
  2419. }
  2420. /* Wait for all pending SP commands to complete */
  2421. if (IS_PF(bp) && !bnx2x_wait_sp_comp(bp, ~0x0UL)) {
  2422. BNX2X_ERR("Timeout waiting for SP elements to complete\n");
  2423. bnx2x_nic_unload(bp, UNLOAD_CLOSE, false);
  2424. return -EBUSY;
  2425. }
  2426. /* Update driver data for On-Chip MFW dump. */
  2427. if (IS_PF(bp))
  2428. bnx2x_update_mfw_dump(bp);
  2429. /* If PMF - send ADMIN DCBX msg to MFW to initiate DCBX FSM */
  2430. if (bp->port.pmf && (bp->state != BNX2X_STATE_DIAG))
  2431. bnx2x_dcbx_init(bp, false);
  2432. if (!IS_MF_SD_STORAGE_PERSONALITY_ONLY(bp))
  2433. bnx2x_set_os_driver_state(bp, OS_DRIVER_STATE_ACTIVE);
  2434. DP(NETIF_MSG_IFUP, "Ending successfully NIC load\n");
  2435. return 0;
  2436. #ifndef BNX2X_STOP_ON_ERROR
  2437. load_error3:
  2438. if (IS_PF(bp)) {
  2439. bnx2x_int_disable_sync(bp, 1);
  2440. /* Clean queueable objects */
  2441. bnx2x_squeeze_objects(bp);
  2442. }
  2443. /* Free SKBs, SGEs, TPA pool and driver internals */
  2444. bnx2x_free_skbs(bp);
  2445. for_each_rx_queue(bp, i)
  2446. bnx2x_free_rx_sge_range(bp, bp->fp + i, NUM_RX_SGE);
  2447. /* Release IRQs */
  2448. bnx2x_free_irq(bp);
  2449. load_error2:
  2450. if (IS_PF(bp) && !BP_NOMCP(bp)) {
  2451. bnx2x_fw_command(bp, DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP, 0);
  2452. bnx2x_fw_command(bp, DRV_MSG_CODE_UNLOAD_DONE, 0);
  2453. }
  2454. bp->port.pmf = 0;
  2455. load_error1:
  2456. bnx2x_napi_disable(bp);
  2457. bnx2x_del_all_napi(bp);
  2458. /* clear pf_load status, as it was already set */
  2459. if (IS_PF(bp))
  2460. bnx2x_clear_pf_load(bp);
  2461. load_error0:
  2462. bnx2x_free_fw_stats_mem(bp);
  2463. bnx2x_free_fp_mem(bp);
  2464. bnx2x_free_mem(bp);
  2465. return rc;
  2466. #endif /* ! BNX2X_STOP_ON_ERROR */
  2467. }
  2468. int bnx2x_drain_tx_queues(struct bnx2x *bp)
  2469. {
  2470. u8 rc = 0, cos, i;
  2471. /* Wait until tx fastpath tasks complete */
  2472. for_each_tx_queue(bp, i) {
  2473. struct bnx2x_fastpath *fp = &bp->fp[i];
  2474. for_each_cos_in_tx_queue(fp, cos)
  2475. rc = bnx2x_clean_tx_queue(bp, fp->txdata_ptr[cos]);
  2476. if (rc)
  2477. return rc;
  2478. }
  2479. return 0;
  2480. }
  2481. /* must be called with rtnl_lock */
  2482. int bnx2x_nic_unload(struct bnx2x *bp, int unload_mode, bool keep_link)
  2483. {
  2484. int i;
  2485. bool global = false;
  2486. DP(NETIF_MSG_IFUP, "Starting NIC unload\n");
  2487. if (!IS_MF_SD_STORAGE_PERSONALITY_ONLY(bp))
  2488. bnx2x_set_os_driver_state(bp, OS_DRIVER_STATE_DISABLED);
  2489. /* mark driver is unloaded in shmem2 */
  2490. if (IS_PF(bp) && SHMEM2_HAS(bp, drv_capabilities_flag)) {
  2491. u32 val;
  2492. val = SHMEM2_RD(bp, drv_capabilities_flag[BP_FW_MB_IDX(bp)]);
  2493. SHMEM2_WR(bp, drv_capabilities_flag[BP_FW_MB_IDX(bp)],
  2494. val & ~DRV_FLAGS_CAPABILITIES_LOADED_L2);
  2495. }
  2496. if (IS_PF(bp) && bp->recovery_state != BNX2X_RECOVERY_DONE &&
  2497. (bp->state == BNX2X_STATE_CLOSED ||
  2498. bp->state == BNX2X_STATE_ERROR)) {
  2499. /* We can get here if the driver has been unloaded
  2500. * during parity error recovery and is either waiting for a
  2501. * leader to complete or for other functions to unload and
  2502. * then ifdown has been issued. In this case we want to
  2503. * unload and let other functions to complete a recovery
  2504. * process.
  2505. */
  2506. bp->recovery_state = BNX2X_RECOVERY_DONE;
  2507. bp->is_leader = 0;
  2508. bnx2x_release_leader_lock(bp);
  2509. smp_mb();
  2510. DP(NETIF_MSG_IFDOWN, "Releasing a leadership...\n");
  2511. BNX2X_ERR("Can't unload in closed or error state\n");
  2512. return -EINVAL;
  2513. }
  2514. /* Nothing to do during unload if previous bnx2x_nic_load()
  2515. * have not completed successfully - all resources are released.
  2516. *
  2517. * we can get here only after unsuccessful ndo_* callback, during which
  2518. * dev->IFF_UP flag is still on.
  2519. */
  2520. if (bp->state == BNX2X_STATE_CLOSED || bp->state == BNX2X_STATE_ERROR)
  2521. return 0;
  2522. /* It's important to set the bp->state to the value different from
  2523. * BNX2X_STATE_OPEN and only then stop the Tx. Otherwise bnx2x_tx_int()
  2524. * may restart the Tx from the NAPI context (see bnx2x_tx_int()).
  2525. */
  2526. bp->state = BNX2X_STATE_CLOSING_WAIT4_HALT;
  2527. smp_mb();
  2528. /* indicate to VFs that the PF is going down */
  2529. bnx2x_iov_channel_down(bp);
  2530. if (CNIC_LOADED(bp))
  2531. bnx2x_cnic_notify(bp, CNIC_CTL_STOP_CMD);
  2532. /* Stop Tx */
  2533. bnx2x_tx_disable(bp);
  2534. netdev_reset_tc(bp->dev);
  2535. bp->rx_mode = BNX2X_RX_MODE_NONE;
  2536. del_timer_sync(&bp->timer);
  2537. if (IS_PF(bp) && !BP_NOMCP(bp)) {
  2538. /* Set ALWAYS_ALIVE bit in shmem */
  2539. bp->fw_drv_pulse_wr_seq |= DRV_PULSE_ALWAYS_ALIVE;
  2540. bnx2x_drv_pulse(bp);
  2541. bnx2x_stats_handle(bp, STATS_EVENT_STOP);
  2542. bnx2x_save_statistics(bp);
  2543. }
  2544. /* wait till consumers catch up with producers in all queues.
  2545. * If we're recovering, FW can't write to host so no reason
  2546. * to wait for the queues to complete all Tx.
  2547. */
  2548. if (unload_mode != UNLOAD_RECOVERY)
  2549. bnx2x_drain_tx_queues(bp);
  2550. /* if VF indicate to PF this function is going down (PF will delete sp
  2551. * elements and clear initializations
  2552. */
  2553. if (IS_VF(bp))
  2554. bnx2x_vfpf_close_vf(bp);
  2555. else if (unload_mode != UNLOAD_RECOVERY)
  2556. /* if this is a normal/close unload need to clean up chip*/
  2557. bnx2x_chip_cleanup(bp, unload_mode, keep_link);
  2558. else {
  2559. /* Send the UNLOAD_REQUEST to the MCP */
  2560. bnx2x_send_unload_req(bp, unload_mode);
  2561. /* Prevent transactions to host from the functions on the
  2562. * engine that doesn't reset global blocks in case of global
  2563. * attention once global blocks are reset and gates are opened
  2564. * (the engine which leader will perform the recovery
  2565. * last).
  2566. */
  2567. if (!CHIP_IS_E1x(bp))
  2568. bnx2x_pf_disable(bp);
  2569. /* Disable HW interrupts, NAPI */
  2570. bnx2x_netif_stop(bp, 1);
  2571. /* Delete all NAPI objects */
  2572. bnx2x_del_all_napi(bp);
  2573. if (CNIC_LOADED(bp))
  2574. bnx2x_del_all_napi_cnic(bp);
  2575. /* Release IRQs */
  2576. bnx2x_free_irq(bp);
  2577. /* Report UNLOAD_DONE to MCP */
  2578. bnx2x_send_unload_done(bp, false);
  2579. }
  2580. /*
  2581. * At this stage no more interrupts will arrive so we may safely clean
  2582. * the queueable objects here in case they failed to get cleaned so far.
  2583. */
  2584. if (IS_PF(bp))
  2585. bnx2x_squeeze_objects(bp);
  2586. /* There should be no more pending SP commands at this stage */
  2587. bp->sp_state = 0;
  2588. bp->port.pmf = 0;
  2589. /* clear pending work in rtnl task */
  2590. bp->sp_rtnl_state = 0;
  2591. smp_mb();
  2592. /* Free SKBs, SGEs, TPA pool and driver internals */
  2593. bnx2x_free_skbs(bp);
  2594. if (CNIC_LOADED(bp))
  2595. bnx2x_free_skbs_cnic(bp);
  2596. for_each_rx_queue(bp, i)
  2597. bnx2x_free_rx_sge_range(bp, bp->fp + i, NUM_RX_SGE);
  2598. bnx2x_free_fp_mem(bp);
  2599. if (CNIC_LOADED(bp))
  2600. bnx2x_free_fp_mem_cnic(bp);
  2601. if (IS_PF(bp)) {
  2602. if (CNIC_LOADED(bp))
  2603. bnx2x_free_mem_cnic(bp);
  2604. }
  2605. bnx2x_free_mem(bp);
  2606. bp->state = BNX2X_STATE_CLOSED;
  2607. bp->cnic_loaded = false;
  2608. /* Clear driver version indication in shmem */
  2609. if (IS_PF(bp) && !BP_NOMCP(bp))
  2610. bnx2x_update_mng_version(bp);
  2611. /* Check if there are pending parity attentions. If there are - set
  2612. * RECOVERY_IN_PROGRESS.
  2613. */
  2614. if (IS_PF(bp) && bnx2x_chk_parity_attn(bp, &global, false)) {
  2615. bnx2x_set_reset_in_progress(bp);
  2616. /* Set RESET_IS_GLOBAL if needed */
  2617. if (global)
  2618. bnx2x_set_reset_global(bp);
  2619. }
  2620. /* The last driver must disable a "close the gate" if there is no
  2621. * parity attention or "process kill" pending.
  2622. */
  2623. if (IS_PF(bp) &&
  2624. !bnx2x_clear_pf_load(bp) &&
  2625. bnx2x_reset_is_done(bp, BP_PATH(bp)))
  2626. bnx2x_disable_close_the_gate(bp);
  2627. DP(NETIF_MSG_IFUP, "Ending NIC unload\n");
  2628. return 0;
  2629. }
  2630. int bnx2x_set_power_state(struct bnx2x *bp, pci_power_t state)
  2631. {
  2632. u16 pmcsr;
  2633. /* If there is no power capability, silently succeed */
  2634. if (!bp->pdev->pm_cap) {
  2635. BNX2X_DEV_INFO("No power capability. Breaking.\n");
  2636. return 0;
  2637. }
  2638. pci_read_config_word(bp->pdev, bp->pdev->pm_cap + PCI_PM_CTRL, &pmcsr);
  2639. switch (state) {
  2640. case PCI_D0:
  2641. pci_write_config_word(bp->pdev, bp->pdev->pm_cap + PCI_PM_CTRL,
  2642. ((pmcsr & ~PCI_PM_CTRL_STATE_MASK) |
  2643. PCI_PM_CTRL_PME_STATUS));
  2644. if (pmcsr & PCI_PM_CTRL_STATE_MASK)
  2645. /* delay required during transition out of D3hot */
  2646. msleep(20);
  2647. break;
  2648. case PCI_D3hot:
  2649. /* If there are other clients above don't
  2650. shut down the power */
  2651. if (atomic_read(&bp->pdev->enable_cnt) != 1)
  2652. return 0;
  2653. /* Don't shut down the power for emulation and FPGA */
  2654. if (CHIP_REV_IS_SLOW(bp))
  2655. return 0;
  2656. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  2657. pmcsr |= 3;
  2658. if (bp->wol)
  2659. pmcsr |= PCI_PM_CTRL_PME_ENABLE;
  2660. pci_write_config_word(bp->pdev, bp->pdev->pm_cap + PCI_PM_CTRL,
  2661. pmcsr);
  2662. /* No more memory access after this point until
  2663. * device is brought back to D0.
  2664. */
  2665. break;
  2666. default:
  2667. dev_err(&bp->pdev->dev, "Can't support state = %d\n", state);
  2668. return -EINVAL;
  2669. }
  2670. return 0;
  2671. }
  2672. /*
  2673. * net_device service functions
  2674. */
  2675. static int bnx2x_poll(struct napi_struct *napi, int budget)
  2676. {
  2677. struct bnx2x_fastpath *fp = container_of(napi, struct bnx2x_fastpath,
  2678. napi);
  2679. struct bnx2x *bp = fp->bp;
  2680. int rx_work_done;
  2681. u8 cos;
  2682. #ifdef BNX2X_STOP_ON_ERROR
  2683. if (unlikely(bp->panic)) {
  2684. napi_complete(napi);
  2685. return 0;
  2686. }
  2687. #endif
  2688. for_each_cos_in_tx_queue(fp, cos)
  2689. if (bnx2x_tx_queue_has_work(fp->txdata_ptr[cos]))
  2690. bnx2x_tx_int(bp, fp->txdata_ptr[cos]);
  2691. rx_work_done = (bnx2x_has_rx_work(fp)) ? bnx2x_rx_int(fp, budget) : 0;
  2692. if (rx_work_done < budget) {
  2693. /* No need to update SB for FCoE L2 ring as long as
  2694. * it's connected to the default SB and the SB
  2695. * has been updated when NAPI was scheduled.
  2696. */
  2697. if (IS_FCOE_FP(fp)) {
  2698. napi_complete(napi);
  2699. } else {
  2700. bnx2x_update_fpsb_idx(fp);
  2701. /* bnx2x_has_rx_work() reads the status block,
  2702. * thus we need to ensure that status block indices
  2703. * have been actually read (bnx2x_update_fpsb_idx)
  2704. * prior to this check (bnx2x_has_rx_work) so that
  2705. * we won't write the "newer" value of the status block
  2706. * to IGU (if there was a DMA right after
  2707. * bnx2x_has_rx_work and if there is no rmb, the memory
  2708. * reading (bnx2x_update_fpsb_idx) may be postponed
  2709. * to right before bnx2x_ack_sb). In this case there
  2710. * will never be another interrupt until there is
  2711. * another update of the status block, while there
  2712. * is still unhandled work.
  2713. */
  2714. rmb();
  2715. if (!(bnx2x_has_rx_work(fp) || bnx2x_has_tx_work(fp))) {
  2716. napi_complete(napi);
  2717. /* Re-enable interrupts */
  2718. DP(NETIF_MSG_RX_STATUS,
  2719. "Update index to %d\n", fp->fp_hc_idx);
  2720. bnx2x_ack_sb(bp, fp->igu_sb_id, USTORM_ID,
  2721. le16_to_cpu(fp->fp_hc_idx),
  2722. IGU_INT_ENABLE, 1);
  2723. } else {
  2724. rx_work_done = budget;
  2725. }
  2726. }
  2727. }
  2728. return rx_work_done;
  2729. }
  2730. /* we split the first BD into headers and data BDs
  2731. * to ease the pain of our fellow microcode engineers
  2732. * we use one mapping for both BDs
  2733. */
  2734. static u16 bnx2x_tx_split(struct bnx2x *bp,
  2735. struct bnx2x_fp_txdata *txdata,
  2736. struct sw_tx_bd *tx_buf,
  2737. struct eth_tx_start_bd **tx_bd, u16 hlen,
  2738. u16 bd_prod)
  2739. {
  2740. struct eth_tx_start_bd *h_tx_bd = *tx_bd;
  2741. struct eth_tx_bd *d_tx_bd;
  2742. dma_addr_t mapping;
  2743. int old_len = le16_to_cpu(h_tx_bd->nbytes);
  2744. /* first fix first BD */
  2745. h_tx_bd->nbytes = cpu_to_le16(hlen);
  2746. DP(NETIF_MSG_TX_QUEUED, "TSO split header size is %d (%x:%x)\n",
  2747. h_tx_bd->nbytes, h_tx_bd->addr_hi, h_tx_bd->addr_lo);
  2748. /* now get a new data BD
  2749. * (after the pbd) and fill it */
  2750. bd_prod = TX_BD(NEXT_TX_IDX(bd_prod));
  2751. d_tx_bd = &txdata->tx_desc_ring[bd_prod].reg_bd;
  2752. mapping = HILO_U64(le32_to_cpu(h_tx_bd->addr_hi),
  2753. le32_to_cpu(h_tx_bd->addr_lo)) + hlen;
  2754. d_tx_bd->addr_hi = cpu_to_le32(U64_HI(mapping));
  2755. d_tx_bd->addr_lo = cpu_to_le32(U64_LO(mapping));
  2756. d_tx_bd->nbytes = cpu_to_le16(old_len - hlen);
  2757. /* this marks the BD as one that has no individual mapping */
  2758. tx_buf->flags |= BNX2X_TSO_SPLIT_BD;
  2759. DP(NETIF_MSG_TX_QUEUED,
  2760. "TSO split data size is %d (%x:%x)\n",
  2761. d_tx_bd->nbytes, d_tx_bd->addr_hi, d_tx_bd->addr_lo);
  2762. /* update tx_bd */
  2763. *tx_bd = (struct eth_tx_start_bd *)d_tx_bd;
  2764. return bd_prod;
  2765. }
  2766. #define bswab32(b32) ((__force __le32) swab32((__force __u32) (b32)))
  2767. #define bswab16(b16) ((__force __le16) swab16((__force __u16) (b16)))
  2768. static __le16 bnx2x_csum_fix(unsigned char *t_header, u16 csum, s8 fix)
  2769. {
  2770. __sum16 tsum = (__force __sum16) csum;
  2771. if (fix > 0)
  2772. tsum = ~csum_fold(csum_sub((__force __wsum) csum,
  2773. csum_partial(t_header - fix, fix, 0)));
  2774. else if (fix < 0)
  2775. tsum = ~csum_fold(csum_add((__force __wsum) csum,
  2776. csum_partial(t_header, -fix, 0)));
  2777. return bswab16(tsum);
  2778. }
  2779. static u32 bnx2x_xmit_type(struct bnx2x *bp, struct sk_buff *skb)
  2780. {
  2781. u32 rc;
  2782. __u8 prot = 0;
  2783. __be16 protocol;
  2784. if (skb->ip_summed != CHECKSUM_PARTIAL)
  2785. return XMIT_PLAIN;
  2786. protocol = vlan_get_protocol(skb);
  2787. if (protocol == htons(ETH_P_IPV6)) {
  2788. rc = XMIT_CSUM_V6;
  2789. prot = ipv6_hdr(skb)->nexthdr;
  2790. } else {
  2791. rc = XMIT_CSUM_V4;
  2792. prot = ip_hdr(skb)->protocol;
  2793. }
  2794. if (!CHIP_IS_E1x(bp) && skb->encapsulation) {
  2795. if (inner_ip_hdr(skb)->version == 6) {
  2796. rc |= XMIT_CSUM_ENC_V6;
  2797. if (inner_ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
  2798. rc |= XMIT_CSUM_TCP;
  2799. } else {
  2800. rc |= XMIT_CSUM_ENC_V4;
  2801. if (inner_ip_hdr(skb)->protocol == IPPROTO_TCP)
  2802. rc |= XMIT_CSUM_TCP;
  2803. }
  2804. }
  2805. if (prot == IPPROTO_TCP)
  2806. rc |= XMIT_CSUM_TCP;
  2807. if (skb_is_gso(skb)) {
  2808. if (skb_is_gso_v6(skb)) {
  2809. rc |= (XMIT_GSO_V6 | XMIT_CSUM_TCP);
  2810. if (rc & XMIT_CSUM_ENC)
  2811. rc |= XMIT_GSO_ENC_V6;
  2812. } else {
  2813. rc |= (XMIT_GSO_V4 | XMIT_CSUM_TCP);
  2814. if (rc & XMIT_CSUM_ENC)
  2815. rc |= XMIT_GSO_ENC_V4;
  2816. }
  2817. }
  2818. return rc;
  2819. }
  2820. /* VXLAN: 4 = 1 (for linear data BD) + 3 (2 for PBD and last BD) */
  2821. #define BNX2X_NUM_VXLAN_TSO_WIN_SUB_BDS 4
  2822. /* Regular: 3 = 1 (for linear data BD) + 2 (for PBD and last BD) */
  2823. #define BNX2X_NUM_TSO_WIN_SUB_BDS 3
  2824. #if (MAX_SKB_FRAGS >= MAX_FETCH_BD - BDS_PER_TX_PKT)
  2825. /* check if packet requires linearization (packet is too fragmented)
  2826. no need to check fragmentation if page size > 8K (there will be no
  2827. violation to FW restrictions) */
  2828. static int bnx2x_pkt_req_lin(struct bnx2x *bp, struct sk_buff *skb,
  2829. u32 xmit_type)
  2830. {
  2831. int first_bd_sz = 0, num_tso_win_sub = BNX2X_NUM_TSO_WIN_SUB_BDS;
  2832. int to_copy = 0, hlen = 0;
  2833. if (xmit_type & XMIT_GSO_ENC)
  2834. num_tso_win_sub = BNX2X_NUM_VXLAN_TSO_WIN_SUB_BDS;
  2835. if (skb_shinfo(skb)->nr_frags >= (MAX_FETCH_BD - num_tso_win_sub)) {
  2836. if (xmit_type & XMIT_GSO) {
  2837. unsigned short lso_mss = skb_shinfo(skb)->gso_size;
  2838. int wnd_size = MAX_FETCH_BD - num_tso_win_sub;
  2839. /* Number of windows to check */
  2840. int num_wnds = skb_shinfo(skb)->nr_frags - wnd_size;
  2841. int wnd_idx = 0;
  2842. int frag_idx = 0;
  2843. u32 wnd_sum = 0;
  2844. /* Headers length */
  2845. if (xmit_type & XMIT_GSO_ENC)
  2846. hlen = (int)(skb_inner_transport_header(skb) -
  2847. skb->data) +
  2848. inner_tcp_hdrlen(skb);
  2849. else
  2850. hlen = (int)(skb_transport_header(skb) -
  2851. skb->data) + tcp_hdrlen(skb);
  2852. /* Amount of data (w/o headers) on linear part of SKB*/
  2853. first_bd_sz = skb_headlen(skb) - hlen;
  2854. wnd_sum = first_bd_sz;
  2855. /* Calculate the first sum - it's special */
  2856. for (frag_idx = 0; frag_idx < wnd_size - 1; frag_idx++)
  2857. wnd_sum +=
  2858. skb_frag_size(&skb_shinfo(skb)->frags[frag_idx]);
  2859. /* If there was data on linear skb data - check it */
  2860. if (first_bd_sz > 0) {
  2861. if (unlikely(wnd_sum < lso_mss)) {
  2862. to_copy = 1;
  2863. goto exit_lbl;
  2864. }
  2865. wnd_sum -= first_bd_sz;
  2866. }
  2867. /* Others are easier: run through the frag list and
  2868. check all windows */
  2869. for (wnd_idx = 0; wnd_idx <= num_wnds; wnd_idx++) {
  2870. wnd_sum +=
  2871. skb_frag_size(&skb_shinfo(skb)->frags[wnd_idx + wnd_size - 1]);
  2872. if (unlikely(wnd_sum < lso_mss)) {
  2873. to_copy = 1;
  2874. break;
  2875. }
  2876. wnd_sum -=
  2877. skb_frag_size(&skb_shinfo(skb)->frags[wnd_idx]);
  2878. }
  2879. } else {
  2880. /* in non-LSO too fragmented packet should always
  2881. be linearized */
  2882. to_copy = 1;
  2883. }
  2884. }
  2885. exit_lbl:
  2886. if (unlikely(to_copy))
  2887. DP(NETIF_MSG_TX_QUEUED,
  2888. "Linearization IS REQUIRED for %s packet. num_frags %d hlen %d first_bd_sz %d\n",
  2889. (xmit_type & XMIT_GSO) ? "LSO" : "non-LSO",
  2890. skb_shinfo(skb)->nr_frags, hlen, first_bd_sz);
  2891. return to_copy;
  2892. }
  2893. #endif
  2894. /**
  2895. * bnx2x_set_pbd_gso - update PBD in GSO case.
  2896. *
  2897. * @skb: packet skb
  2898. * @pbd: parse BD
  2899. * @xmit_type: xmit flags
  2900. */
  2901. static void bnx2x_set_pbd_gso(struct sk_buff *skb,
  2902. struct eth_tx_parse_bd_e1x *pbd,
  2903. u32 xmit_type)
  2904. {
  2905. pbd->lso_mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
  2906. pbd->tcp_send_seq = bswab32(tcp_hdr(skb)->seq);
  2907. pbd->tcp_flags = pbd_tcp_flags(tcp_hdr(skb));
  2908. if (xmit_type & XMIT_GSO_V4) {
  2909. pbd->ip_id = bswab16(ip_hdr(skb)->id);
  2910. pbd->tcp_pseudo_csum =
  2911. bswab16(~csum_tcpudp_magic(ip_hdr(skb)->saddr,
  2912. ip_hdr(skb)->daddr,
  2913. 0, IPPROTO_TCP, 0));
  2914. } else {
  2915. pbd->tcp_pseudo_csum =
  2916. bswab16(~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  2917. &ipv6_hdr(skb)->daddr,
  2918. 0, IPPROTO_TCP, 0));
  2919. }
  2920. pbd->global_data |=
  2921. cpu_to_le16(ETH_TX_PARSE_BD_E1X_PSEUDO_CS_WITHOUT_LEN);
  2922. }
  2923. /**
  2924. * bnx2x_set_pbd_csum_enc - update PBD with checksum and return header length
  2925. *
  2926. * @bp: driver handle
  2927. * @skb: packet skb
  2928. * @parsing_data: data to be updated
  2929. * @xmit_type: xmit flags
  2930. *
  2931. * 57712/578xx related, when skb has encapsulation
  2932. */
  2933. static u8 bnx2x_set_pbd_csum_enc(struct bnx2x *bp, struct sk_buff *skb,
  2934. u32 *parsing_data, u32 xmit_type)
  2935. {
  2936. *parsing_data |=
  2937. ((((u8 *)skb_inner_transport_header(skb) - skb->data) >> 1) <<
  2938. ETH_TX_PARSE_BD_E2_L4_HDR_START_OFFSET_W_SHIFT) &
  2939. ETH_TX_PARSE_BD_E2_L4_HDR_START_OFFSET_W;
  2940. if (xmit_type & XMIT_CSUM_TCP) {
  2941. *parsing_data |= ((inner_tcp_hdrlen(skb) / 4) <<
  2942. ETH_TX_PARSE_BD_E2_TCP_HDR_LENGTH_DW_SHIFT) &
  2943. ETH_TX_PARSE_BD_E2_TCP_HDR_LENGTH_DW;
  2944. return skb_inner_transport_header(skb) +
  2945. inner_tcp_hdrlen(skb) - skb->data;
  2946. }
  2947. /* We support checksum offload for TCP and UDP only.
  2948. * No need to pass the UDP header length - it's a constant.
  2949. */
  2950. return skb_inner_transport_header(skb) +
  2951. sizeof(struct udphdr) - skb->data;
  2952. }
  2953. /**
  2954. * bnx2x_set_pbd_csum_e2 - update PBD with checksum and return header length
  2955. *
  2956. * @bp: driver handle
  2957. * @skb: packet skb
  2958. * @parsing_data: data to be updated
  2959. * @xmit_type: xmit flags
  2960. *
  2961. * 57712/578xx related
  2962. */
  2963. static u8 bnx2x_set_pbd_csum_e2(struct bnx2x *bp, struct sk_buff *skb,
  2964. u32 *parsing_data, u32 xmit_type)
  2965. {
  2966. *parsing_data |=
  2967. ((((u8 *)skb_transport_header(skb) - skb->data) >> 1) <<
  2968. ETH_TX_PARSE_BD_E2_L4_HDR_START_OFFSET_W_SHIFT) &
  2969. ETH_TX_PARSE_BD_E2_L4_HDR_START_OFFSET_W;
  2970. if (xmit_type & XMIT_CSUM_TCP) {
  2971. *parsing_data |= ((tcp_hdrlen(skb) / 4) <<
  2972. ETH_TX_PARSE_BD_E2_TCP_HDR_LENGTH_DW_SHIFT) &
  2973. ETH_TX_PARSE_BD_E2_TCP_HDR_LENGTH_DW;
  2974. return skb_transport_header(skb) + tcp_hdrlen(skb) - skb->data;
  2975. }
  2976. /* We support checksum offload for TCP and UDP only.
  2977. * No need to pass the UDP header length - it's a constant.
  2978. */
  2979. return skb_transport_header(skb) + sizeof(struct udphdr) - skb->data;
  2980. }
  2981. /* set FW indication according to inner or outer protocols if tunneled */
  2982. static void bnx2x_set_sbd_csum(struct bnx2x *bp, struct sk_buff *skb,
  2983. struct eth_tx_start_bd *tx_start_bd,
  2984. u32 xmit_type)
  2985. {
  2986. tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_L4_CSUM;
  2987. if (xmit_type & (XMIT_CSUM_ENC_V6 | XMIT_CSUM_V6))
  2988. tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_IPV6;
  2989. if (!(xmit_type & XMIT_CSUM_TCP))
  2990. tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_IS_UDP;
  2991. }
  2992. /**
  2993. * bnx2x_set_pbd_csum - update PBD with checksum and return header length
  2994. *
  2995. * @bp: driver handle
  2996. * @skb: packet skb
  2997. * @pbd: parse BD to be updated
  2998. * @xmit_type: xmit flags
  2999. */
  3000. static u8 bnx2x_set_pbd_csum(struct bnx2x *bp, struct sk_buff *skb,
  3001. struct eth_tx_parse_bd_e1x *pbd,
  3002. u32 xmit_type)
  3003. {
  3004. u8 hlen = (skb_network_header(skb) - skb->data) >> 1;
  3005. /* for now NS flag is not used in Linux */
  3006. pbd->global_data =
  3007. cpu_to_le16(hlen |
  3008. ((skb->protocol == cpu_to_be16(ETH_P_8021Q)) <<
  3009. ETH_TX_PARSE_BD_E1X_LLC_SNAP_EN_SHIFT));
  3010. pbd->ip_hlen_w = (skb_transport_header(skb) -
  3011. skb_network_header(skb)) >> 1;
  3012. hlen += pbd->ip_hlen_w;
  3013. /* We support checksum offload for TCP and UDP only */
  3014. if (xmit_type & XMIT_CSUM_TCP)
  3015. hlen += tcp_hdrlen(skb) / 2;
  3016. else
  3017. hlen += sizeof(struct udphdr) / 2;
  3018. pbd->total_hlen_w = cpu_to_le16(hlen);
  3019. hlen = hlen*2;
  3020. if (xmit_type & XMIT_CSUM_TCP) {
  3021. pbd->tcp_pseudo_csum = bswab16(tcp_hdr(skb)->check);
  3022. } else {
  3023. s8 fix = SKB_CS_OFF(skb); /* signed! */
  3024. DP(NETIF_MSG_TX_QUEUED,
  3025. "hlen %d fix %d csum before fix %x\n",
  3026. le16_to_cpu(pbd->total_hlen_w), fix, SKB_CS(skb));
  3027. /* HW bug: fixup the CSUM */
  3028. pbd->tcp_pseudo_csum =
  3029. bnx2x_csum_fix(skb_transport_header(skb),
  3030. SKB_CS(skb), fix);
  3031. DP(NETIF_MSG_TX_QUEUED, "csum after fix %x\n",
  3032. pbd->tcp_pseudo_csum);
  3033. }
  3034. return hlen;
  3035. }
  3036. static void bnx2x_update_pbds_gso_enc(struct sk_buff *skb,
  3037. struct eth_tx_parse_bd_e2 *pbd_e2,
  3038. struct eth_tx_parse_2nd_bd *pbd2,
  3039. u16 *global_data,
  3040. u32 xmit_type)
  3041. {
  3042. u16 hlen_w = 0;
  3043. u8 outerip_off, outerip_len = 0;
  3044. /* from outer IP to transport */
  3045. hlen_w = (skb_inner_transport_header(skb) -
  3046. skb_network_header(skb)) >> 1;
  3047. /* transport len */
  3048. hlen_w += inner_tcp_hdrlen(skb) >> 1;
  3049. pbd2->fw_ip_hdr_to_payload_w = hlen_w;
  3050. /* outer IP header info */
  3051. if (xmit_type & XMIT_CSUM_V4) {
  3052. struct iphdr *iph = ip_hdr(skb);
  3053. u32 csum = (__force u32)(~iph->check) -
  3054. (__force u32)iph->tot_len -
  3055. (__force u32)iph->frag_off;
  3056. outerip_len = iph->ihl << 1;
  3057. pbd2->fw_ip_csum_wo_len_flags_frag =
  3058. bswab16(csum_fold((__force __wsum)csum));
  3059. } else {
  3060. pbd2->fw_ip_hdr_to_payload_w =
  3061. hlen_w - ((sizeof(struct ipv6hdr)) >> 1);
  3062. pbd_e2->data.tunnel_data.flags |=
  3063. ETH_TUNNEL_DATA_IPV6_OUTER;
  3064. }
  3065. pbd2->tcp_send_seq = bswab32(inner_tcp_hdr(skb)->seq);
  3066. pbd2->tcp_flags = pbd_tcp_flags(inner_tcp_hdr(skb));
  3067. /* inner IP header info */
  3068. if (xmit_type & XMIT_CSUM_ENC_V4) {
  3069. pbd2->hw_ip_id = bswab16(inner_ip_hdr(skb)->id);
  3070. pbd_e2->data.tunnel_data.pseudo_csum =
  3071. bswab16(~csum_tcpudp_magic(
  3072. inner_ip_hdr(skb)->saddr,
  3073. inner_ip_hdr(skb)->daddr,
  3074. 0, IPPROTO_TCP, 0));
  3075. } else {
  3076. pbd_e2->data.tunnel_data.pseudo_csum =
  3077. bswab16(~csum_ipv6_magic(
  3078. &inner_ipv6_hdr(skb)->saddr,
  3079. &inner_ipv6_hdr(skb)->daddr,
  3080. 0, IPPROTO_TCP, 0));
  3081. }
  3082. outerip_off = (skb_network_header(skb) - skb->data) >> 1;
  3083. *global_data |=
  3084. outerip_off |
  3085. (outerip_len <<
  3086. ETH_TX_PARSE_2ND_BD_IP_HDR_LEN_OUTER_W_SHIFT) |
  3087. ((skb->protocol == cpu_to_be16(ETH_P_8021Q)) <<
  3088. ETH_TX_PARSE_2ND_BD_LLC_SNAP_EN_SHIFT);
  3089. if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
  3090. SET_FLAG(*global_data, ETH_TX_PARSE_2ND_BD_TUNNEL_UDP_EXIST, 1);
  3091. pbd2->tunnel_udp_hdr_start_w = skb_transport_offset(skb) >> 1;
  3092. }
  3093. }
  3094. static inline void bnx2x_set_ipv6_ext_e2(struct sk_buff *skb, u32 *parsing_data,
  3095. u32 xmit_type)
  3096. {
  3097. struct ipv6hdr *ipv6;
  3098. if (!(xmit_type & (XMIT_GSO_ENC_V6 | XMIT_GSO_V6)))
  3099. return;
  3100. if (xmit_type & XMIT_GSO_ENC_V6)
  3101. ipv6 = inner_ipv6_hdr(skb);
  3102. else /* XMIT_GSO_V6 */
  3103. ipv6 = ipv6_hdr(skb);
  3104. if (ipv6->nexthdr == NEXTHDR_IPV6)
  3105. *parsing_data |= ETH_TX_PARSE_BD_E2_IPV6_WITH_EXT_HDR;
  3106. }
  3107. /* called with netif_tx_lock
  3108. * bnx2x_tx_int() runs without netif_tx_lock unless it needs to call
  3109. * netif_wake_queue()
  3110. */
  3111. netdev_tx_t bnx2x_start_xmit(struct sk_buff *skb, struct net_device *dev)
  3112. {
  3113. struct bnx2x *bp = netdev_priv(dev);
  3114. struct netdev_queue *txq;
  3115. struct bnx2x_fp_txdata *txdata;
  3116. struct sw_tx_bd *tx_buf;
  3117. struct eth_tx_start_bd *tx_start_bd, *first_bd;
  3118. struct eth_tx_bd *tx_data_bd, *total_pkt_bd = NULL;
  3119. struct eth_tx_parse_bd_e1x *pbd_e1x = NULL;
  3120. struct eth_tx_parse_bd_e2 *pbd_e2 = NULL;
  3121. struct eth_tx_parse_2nd_bd *pbd2 = NULL;
  3122. u32 pbd_e2_parsing_data = 0;
  3123. u16 pkt_prod, bd_prod;
  3124. int nbd, txq_index;
  3125. dma_addr_t mapping;
  3126. u32 xmit_type = bnx2x_xmit_type(bp, skb);
  3127. int i;
  3128. u8 hlen = 0;
  3129. __le16 pkt_size = 0;
  3130. struct ethhdr *eth;
  3131. u8 mac_type = UNICAST_ADDRESS;
  3132. #ifdef BNX2X_STOP_ON_ERROR
  3133. if (unlikely(bp->panic))
  3134. return NETDEV_TX_BUSY;
  3135. #endif
  3136. txq_index = skb_get_queue_mapping(skb);
  3137. txq = netdev_get_tx_queue(dev, txq_index);
  3138. BUG_ON(txq_index >= MAX_ETH_TXQ_IDX(bp) + (CNIC_LOADED(bp) ? 1 : 0));
  3139. txdata = &bp->bnx2x_txq[txq_index];
  3140. /* enable this debug print to view the transmission queue being used
  3141. DP(NETIF_MSG_TX_QUEUED, "indices: txq %d, fp %d, txdata %d\n",
  3142. txq_index, fp_index, txdata_index); */
  3143. /* enable this debug print to view the transmission details
  3144. DP(NETIF_MSG_TX_QUEUED,
  3145. "transmitting packet cid %d fp index %d txdata_index %d tx_data ptr %p fp pointer %p\n",
  3146. txdata->cid, fp_index, txdata_index, txdata, fp); */
  3147. if (unlikely(bnx2x_tx_avail(bp, txdata) <
  3148. skb_shinfo(skb)->nr_frags +
  3149. BDS_PER_TX_PKT +
  3150. NEXT_CNT_PER_TX_PKT(MAX_BDS_PER_TX_PKT))) {
  3151. /* Handle special storage cases separately */
  3152. if (txdata->tx_ring_size == 0) {
  3153. struct bnx2x_eth_q_stats *q_stats =
  3154. bnx2x_fp_qstats(bp, txdata->parent_fp);
  3155. q_stats->driver_filtered_tx_pkt++;
  3156. dev_kfree_skb(skb);
  3157. return NETDEV_TX_OK;
  3158. }
  3159. bnx2x_fp_qstats(bp, txdata->parent_fp)->driver_xoff++;
  3160. netif_tx_stop_queue(txq);
  3161. BNX2X_ERR("BUG! Tx ring full when queue awake!\n");
  3162. return NETDEV_TX_BUSY;
  3163. }
  3164. DP(NETIF_MSG_TX_QUEUED,
  3165. "queue[%d]: SKB: summed %x protocol %x protocol(%x,%x) gso type %x xmit_type %x len %d\n",
  3166. txq_index, skb->ip_summed, skb->protocol, ipv6_hdr(skb)->nexthdr,
  3167. ip_hdr(skb)->protocol, skb_shinfo(skb)->gso_type, xmit_type,
  3168. skb->len);
  3169. eth = (struct ethhdr *)skb->data;
  3170. /* set flag according to packet type (UNICAST_ADDRESS is default)*/
  3171. if (unlikely(is_multicast_ether_addr(eth->h_dest))) {
  3172. if (is_broadcast_ether_addr(eth->h_dest))
  3173. mac_type = BROADCAST_ADDRESS;
  3174. else
  3175. mac_type = MULTICAST_ADDRESS;
  3176. }
  3177. #if (MAX_SKB_FRAGS >= MAX_FETCH_BD - BDS_PER_TX_PKT)
  3178. /* First, check if we need to linearize the skb (due to FW
  3179. restrictions). No need to check fragmentation if page size > 8K
  3180. (there will be no violation to FW restrictions) */
  3181. if (bnx2x_pkt_req_lin(bp, skb, xmit_type)) {
  3182. /* Statistics of linearization */
  3183. bp->lin_cnt++;
  3184. if (skb_linearize(skb) != 0) {
  3185. DP(NETIF_MSG_TX_QUEUED,
  3186. "SKB linearization failed - silently dropping this SKB\n");
  3187. dev_kfree_skb_any(skb);
  3188. return NETDEV_TX_OK;
  3189. }
  3190. }
  3191. #endif
  3192. /* Map skb linear data for DMA */
  3193. mapping = dma_map_single(&bp->pdev->dev, skb->data,
  3194. skb_headlen(skb), DMA_TO_DEVICE);
  3195. if (unlikely(dma_mapping_error(&bp->pdev->dev, mapping))) {
  3196. DP(NETIF_MSG_TX_QUEUED,
  3197. "SKB mapping failed - silently dropping this SKB\n");
  3198. dev_kfree_skb_any(skb);
  3199. return NETDEV_TX_OK;
  3200. }
  3201. /*
  3202. Please read carefully. First we use one BD which we mark as start,
  3203. then we have a parsing info BD (used for TSO or xsum),
  3204. and only then we have the rest of the TSO BDs.
  3205. (don't forget to mark the last one as last,
  3206. and to unmap only AFTER you write to the BD ...)
  3207. And above all, all pdb sizes are in words - NOT DWORDS!
  3208. */
  3209. /* get current pkt produced now - advance it just before sending packet
  3210. * since mapping of pages may fail and cause packet to be dropped
  3211. */
  3212. pkt_prod = txdata->tx_pkt_prod;
  3213. bd_prod = TX_BD(txdata->tx_bd_prod);
  3214. /* get a tx_buf and first BD
  3215. * tx_start_bd may be changed during SPLIT,
  3216. * but first_bd will always stay first
  3217. */
  3218. tx_buf = &txdata->tx_buf_ring[TX_BD(pkt_prod)];
  3219. tx_start_bd = &txdata->tx_desc_ring[bd_prod].start_bd;
  3220. first_bd = tx_start_bd;
  3221. tx_start_bd->bd_flags.as_bitfield = ETH_TX_BD_FLAGS_START_BD;
  3222. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
  3223. if (!(bp->flags & TX_TIMESTAMPING_EN)) {
  3224. BNX2X_ERR("Tx timestamping was not enabled, this packet will not be timestamped\n");
  3225. } else if (bp->ptp_tx_skb) {
  3226. BNX2X_ERR("The device supports only a single outstanding packet to timestamp, this packet will not be timestamped\n");
  3227. } else {
  3228. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  3229. /* schedule check for Tx timestamp */
  3230. bp->ptp_tx_skb = skb_get(skb);
  3231. bp->ptp_tx_start = jiffies;
  3232. schedule_work(&bp->ptp_task);
  3233. }
  3234. }
  3235. /* header nbd: indirectly zero other flags! */
  3236. tx_start_bd->general_data = 1 << ETH_TX_START_BD_HDR_NBDS_SHIFT;
  3237. /* remember the first BD of the packet */
  3238. tx_buf->first_bd = txdata->tx_bd_prod;
  3239. tx_buf->skb = skb;
  3240. tx_buf->flags = 0;
  3241. DP(NETIF_MSG_TX_QUEUED,
  3242. "sending pkt %u @%p next_idx %u bd %u @%p\n",
  3243. pkt_prod, tx_buf, txdata->tx_pkt_prod, bd_prod, tx_start_bd);
  3244. if (skb_vlan_tag_present(skb)) {
  3245. tx_start_bd->vlan_or_ethertype =
  3246. cpu_to_le16(skb_vlan_tag_get(skb));
  3247. tx_start_bd->bd_flags.as_bitfield |=
  3248. (X_ETH_OUTBAND_VLAN << ETH_TX_BD_FLAGS_VLAN_MODE_SHIFT);
  3249. } else {
  3250. /* when transmitting in a vf, start bd must hold the ethertype
  3251. * for fw to enforce it
  3252. */
  3253. u16 vlan_tci = 0;
  3254. #ifndef BNX2X_STOP_ON_ERROR
  3255. if (IS_VF(bp)) {
  3256. #endif
  3257. /* Still need to consider inband vlan for enforced */
  3258. if (__vlan_get_tag(skb, &vlan_tci)) {
  3259. tx_start_bd->vlan_or_ethertype =
  3260. cpu_to_le16(ntohs(eth->h_proto));
  3261. } else {
  3262. tx_start_bd->bd_flags.as_bitfield |=
  3263. (X_ETH_INBAND_VLAN <<
  3264. ETH_TX_BD_FLAGS_VLAN_MODE_SHIFT);
  3265. tx_start_bd->vlan_or_ethertype =
  3266. cpu_to_le16(vlan_tci);
  3267. }
  3268. #ifndef BNX2X_STOP_ON_ERROR
  3269. } else {
  3270. /* used by FW for packet accounting */
  3271. tx_start_bd->vlan_or_ethertype = cpu_to_le16(pkt_prod);
  3272. }
  3273. #endif
  3274. }
  3275. nbd = 2; /* start_bd + pbd + frags (updated when pages are mapped) */
  3276. /* turn on parsing and get a BD */
  3277. bd_prod = TX_BD(NEXT_TX_IDX(bd_prod));
  3278. if (xmit_type & XMIT_CSUM)
  3279. bnx2x_set_sbd_csum(bp, skb, tx_start_bd, xmit_type);
  3280. if (!CHIP_IS_E1x(bp)) {
  3281. pbd_e2 = &txdata->tx_desc_ring[bd_prod].parse_bd_e2;
  3282. memset(pbd_e2, 0, sizeof(struct eth_tx_parse_bd_e2));
  3283. if (xmit_type & XMIT_CSUM_ENC) {
  3284. u16 global_data = 0;
  3285. /* Set PBD in enc checksum offload case */
  3286. hlen = bnx2x_set_pbd_csum_enc(bp, skb,
  3287. &pbd_e2_parsing_data,
  3288. xmit_type);
  3289. /* turn on 2nd parsing and get a BD */
  3290. bd_prod = TX_BD(NEXT_TX_IDX(bd_prod));
  3291. pbd2 = &txdata->tx_desc_ring[bd_prod].parse_2nd_bd;
  3292. memset(pbd2, 0, sizeof(*pbd2));
  3293. pbd_e2->data.tunnel_data.ip_hdr_start_inner_w =
  3294. (skb_inner_network_header(skb) -
  3295. skb->data) >> 1;
  3296. if (xmit_type & XMIT_GSO_ENC)
  3297. bnx2x_update_pbds_gso_enc(skb, pbd_e2, pbd2,
  3298. &global_data,
  3299. xmit_type);
  3300. pbd2->global_data = cpu_to_le16(global_data);
  3301. /* add addition parse BD indication to start BD */
  3302. SET_FLAG(tx_start_bd->general_data,
  3303. ETH_TX_START_BD_PARSE_NBDS, 1);
  3304. /* set encapsulation flag in start BD */
  3305. SET_FLAG(tx_start_bd->general_data,
  3306. ETH_TX_START_BD_TUNNEL_EXIST, 1);
  3307. tx_buf->flags |= BNX2X_HAS_SECOND_PBD;
  3308. nbd++;
  3309. } else if (xmit_type & XMIT_CSUM) {
  3310. /* Set PBD in checksum offload case w/o encapsulation */
  3311. hlen = bnx2x_set_pbd_csum_e2(bp, skb,
  3312. &pbd_e2_parsing_data,
  3313. xmit_type);
  3314. }
  3315. bnx2x_set_ipv6_ext_e2(skb, &pbd_e2_parsing_data, xmit_type);
  3316. /* Add the macs to the parsing BD if this is a vf or if
  3317. * Tx Switching is enabled.
  3318. */
  3319. if (IS_VF(bp)) {
  3320. /* override GRE parameters in BD */
  3321. bnx2x_set_fw_mac_addr(&pbd_e2->data.mac_addr.src_hi,
  3322. &pbd_e2->data.mac_addr.src_mid,
  3323. &pbd_e2->data.mac_addr.src_lo,
  3324. eth->h_source);
  3325. bnx2x_set_fw_mac_addr(&pbd_e2->data.mac_addr.dst_hi,
  3326. &pbd_e2->data.mac_addr.dst_mid,
  3327. &pbd_e2->data.mac_addr.dst_lo,
  3328. eth->h_dest);
  3329. } else {
  3330. if (bp->flags & TX_SWITCHING)
  3331. bnx2x_set_fw_mac_addr(
  3332. &pbd_e2->data.mac_addr.dst_hi,
  3333. &pbd_e2->data.mac_addr.dst_mid,
  3334. &pbd_e2->data.mac_addr.dst_lo,
  3335. eth->h_dest);
  3336. #ifdef BNX2X_STOP_ON_ERROR
  3337. /* Enforce security is always set in Stop on Error -
  3338. * source mac should be present in the parsing BD
  3339. */
  3340. bnx2x_set_fw_mac_addr(&pbd_e2->data.mac_addr.src_hi,
  3341. &pbd_e2->data.mac_addr.src_mid,
  3342. &pbd_e2->data.mac_addr.src_lo,
  3343. eth->h_source);
  3344. #endif
  3345. }
  3346. SET_FLAG(pbd_e2_parsing_data,
  3347. ETH_TX_PARSE_BD_E2_ETH_ADDR_TYPE, mac_type);
  3348. } else {
  3349. u16 global_data = 0;
  3350. pbd_e1x = &txdata->tx_desc_ring[bd_prod].parse_bd_e1x;
  3351. memset(pbd_e1x, 0, sizeof(struct eth_tx_parse_bd_e1x));
  3352. /* Set PBD in checksum offload case */
  3353. if (xmit_type & XMIT_CSUM)
  3354. hlen = bnx2x_set_pbd_csum(bp, skb, pbd_e1x, xmit_type);
  3355. SET_FLAG(global_data,
  3356. ETH_TX_PARSE_BD_E1X_ETH_ADDR_TYPE, mac_type);
  3357. pbd_e1x->global_data |= cpu_to_le16(global_data);
  3358. }
  3359. /* Setup the data pointer of the first BD of the packet */
  3360. tx_start_bd->addr_hi = cpu_to_le32(U64_HI(mapping));
  3361. tx_start_bd->addr_lo = cpu_to_le32(U64_LO(mapping));
  3362. tx_start_bd->nbytes = cpu_to_le16(skb_headlen(skb));
  3363. pkt_size = tx_start_bd->nbytes;
  3364. DP(NETIF_MSG_TX_QUEUED,
  3365. "first bd @%p addr (%x:%x) nbytes %d flags %x vlan %x\n",
  3366. tx_start_bd, tx_start_bd->addr_hi, tx_start_bd->addr_lo,
  3367. le16_to_cpu(tx_start_bd->nbytes),
  3368. tx_start_bd->bd_flags.as_bitfield,
  3369. le16_to_cpu(tx_start_bd->vlan_or_ethertype));
  3370. if (xmit_type & XMIT_GSO) {
  3371. DP(NETIF_MSG_TX_QUEUED,
  3372. "TSO packet len %d hlen %d total len %d tso size %d\n",
  3373. skb->len, hlen, skb_headlen(skb),
  3374. skb_shinfo(skb)->gso_size);
  3375. tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_SW_LSO;
  3376. if (unlikely(skb_headlen(skb) > hlen)) {
  3377. nbd++;
  3378. bd_prod = bnx2x_tx_split(bp, txdata, tx_buf,
  3379. &tx_start_bd, hlen,
  3380. bd_prod);
  3381. }
  3382. if (!CHIP_IS_E1x(bp))
  3383. pbd_e2_parsing_data |=
  3384. (skb_shinfo(skb)->gso_size <<
  3385. ETH_TX_PARSE_BD_E2_LSO_MSS_SHIFT) &
  3386. ETH_TX_PARSE_BD_E2_LSO_MSS;
  3387. else
  3388. bnx2x_set_pbd_gso(skb, pbd_e1x, xmit_type);
  3389. }
  3390. /* Set the PBD's parsing_data field if not zero
  3391. * (for the chips newer than 57711).
  3392. */
  3393. if (pbd_e2_parsing_data)
  3394. pbd_e2->parsing_data = cpu_to_le32(pbd_e2_parsing_data);
  3395. tx_data_bd = (struct eth_tx_bd *)tx_start_bd;
  3396. /* Handle fragmented skb */
  3397. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  3398. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  3399. mapping = skb_frag_dma_map(&bp->pdev->dev, frag, 0,
  3400. skb_frag_size(frag), DMA_TO_DEVICE);
  3401. if (unlikely(dma_mapping_error(&bp->pdev->dev, mapping))) {
  3402. unsigned int pkts_compl = 0, bytes_compl = 0;
  3403. DP(NETIF_MSG_TX_QUEUED,
  3404. "Unable to map page - dropping packet...\n");
  3405. /* we need unmap all buffers already mapped
  3406. * for this SKB;
  3407. * first_bd->nbd need to be properly updated
  3408. * before call to bnx2x_free_tx_pkt
  3409. */
  3410. first_bd->nbd = cpu_to_le16(nbd);
  3411. bnx2x_free_tx_pkt(bp, txdata,
  3412. TX_BD(txdata->tx_pkt_prod),
  3413. &pkts_compl, &bytes_compl);
  3414. return NETDEV_TX_OK;
  3415. }
  3416. bd_prod = TX_BD(NEXT_TX_IDX(bd_prod));
  3417. tx_data_bd = &txdata->tx_desc_ring[bd_prod].reg_bd;
  3418. if (total_pkt_bd == NULL)
  3419. total_pkt_bd = &txdata->tx_desc_ring[bd_prod].reg_bd;
  3420. tx_data_bd->addr_hi = cpu_to_le32(U64_HI(mapping));
  3421. tx_data_bd->addr_lo = cpu_to_le32(U64_LO(mapping));
  3422. tx_data_bd->nbytes = cpu_to_le16(skb_frag_size(frag));
  3423. le16_add_cpu(&pkt_size, skb_frag_size(frag));
  3424. nbd++;
  3425. DP(NETIF_MSG_TX_QUEUED,
  3426. "frag %d bd @%p addr (%x:%x) nbytes %d\n",
  3427. i, tx_data_bd, tx_data_bd->addr_hi, tx_data_bd->addr_lo,
  3428. le16_to_cpu(tx_data_bd->nbytes));
  3429. }
  3430. DP(NETIF_MSG_TX_QUEUED, "last bd @%p\n", tx_data_bd);
  3431. /* update with actual num BDs */
  3432. first_bd->nbd = cpu_to_le16(nbd);
  3433. bd_prod = TX_BD(NEXT_TX_IDX(bd_prod));
  3434. /* now send a tx doorbell, counting the next BD
  3435. * if the packet contains or ends with it
  3436. */
  3437. if (TX_BD_POFF(bd_prod) < nbd)
  3438. nbd++;
  3439. /* total_pkt_bytes should be set on the first data BD if
  3440. * it's not an LSO packet and there is more than one
  3441. * data BD. In this case pkt_size is limited by an MTU value.
  3442. * However we prefer to set it for an LSO packet (while we don't
  3443. * have to) in order to save some CPU cycles in a none-LSO
  3444. * case, when we much more care about them.
  3445. */
  3446. if (total_pkt_bd != NULL)
  3447. total_pkt_bd->total_pkt_bytes = pkt_size;
  3448. if (pbd_e1x)
  3449. DP(NETIF_MSG_TX_QUEUED,
  3450. "PBD (E1X) @%p ip_data %x ip_hlen %u ip_id %u lso_mss %u tcp_flags %x xsum %x seq %u hlen %u\n",
  3451. pbd_e1x, pbd_e1x->global_data, pbd_e1x->ip_hlen_w,
  3452. pbd_e1x->ip_id, pbd_e1x->lso_mss, pbd_e1x->tcp_flags,
  3453. pbd_e1x->tcp_pseudo_csum, pbd_e1x->tcp_send_seq,
  3454. le16_to_cpu(pbd_e1x->total_hlen_w));
  3455. if (pbd_e2)
  3456. DP(NETIF_MSG_TX_QUEUED,
  3457. "PBD (E2) @%p dst %x %x %x src %x %x %x parsing_data %x\n",
  3458. pbd_e2,
  3459. pbd_e2->data.mac_addr.dst_hi,
  3460. pbd_e2->data.mac_addr.dst_mid,
  3461. pbd_e2->data.mac_addr.dst_lo,
  3462. pbd_e2->data.mac_addr.src_hi,
  3463. pbd_e2->data.mac_addr.src_mid,
  3464. pbd_e2->data.mac_addr.src_lo,
  3465. pbd_e2->parsing_data);
  3466. DP(NETIF_MSG_TX_QUEUED, "doorbell: nbd %d bd %u\n", nbd, bd_prod);
  3467. netdev_tx_sent_queue(txq, skb->len);
  3468. skb_tx_timestamp(skb);
  3469. txdata->tx_pkt_prod++;
  3470. /*
  3471. * Make sure that the BD data is updated before updating the producer
  3472. * since FW might read the BD right after the producer is updated.
  3473. * This is only applicable for weak-ordered memory model archs such
  3474. * as IA-64. The following barrier is also mandatory since FW will
  3475. * assumes packets must have BDs.
  3476. */
  3477. wmb();
  3478. txdata->tx_db.data.prod += nbd;
  3479. barrier();
  3480. DOORBELL(bp, txdata->cid, txdata->tx_db.raw);
  3481. mmiowb();
  3482. txdata->tx_bd_prod += nbd;
  3483. if (unlikely(bnx2x_tx_avail(bp, txdata) < MAX_DESC_PER_TX_PKT)) {
  3484. netif_tx_stop_queue(txq);
  3485. /* paired memory barrier is in bnx2x_tx_int(), we have to keep
  3486. * ordering of set_bit() in netif_tx_stop_queue() and read of
  3487. * fp->bd_tx_cons */
  3488. smp_mb();
  3489. bnx2x_fp_qstats(bp, txdata->parent_fp)->driver_xoff++;
  3490. if (bnx2x_tx_avail(bp, txdata) >= MAX_DESC_PER_TX_PKT)
  3491. netif_tx_wake_queue(txq);
  3492. }
  3493. txdata->tx_pkt++;
  3494. return NETDEV_TX_OK;
  3495. }
  3496. void bnx2x_get_c2s_mapping(struct bnx2x *bp, u8 *c2s_map, u8 *c2s_default)
  3497. {
  3498. int mfw_vn = BP_FW_MB_IDX(bp);
  3499. u32 tmp;
  3500. /* If the shmem shouldn't affect configuration, reflect */
  3501. if (!IS_MF_BD(bp)) {
  3502. int i;
  3503. for (i = 0; i < BNX2X_MAX_PRIORITY; i++)
  3504. c2s_map[i] = i;
  3505. *c2s_default = 0;
  3506. return;
  3507. }
  3508. tmp = SHMEM2_RD(bp, c2s_pcp_map_lower[mfw_vn]);
  3509. tmp = (__force u32)be32_to_cpu((__force __be32)tmp);
  3510. c2s_map[0] = tmp & 0xff;
  3511. c2s_map[1] = (tmp >> 8) & 0xff;
  3512. c2s_map[2] = (tmp >> 16) & 0xff;
  3513. c2s_map[3] = (tmp >> 24) & 0xff;
  3514. tmp = SHMEM2_RD(bp, c2s_pcp_map_upper[mfw_vn]);
  3515. tmp = (__force u32)be32_to_cpu((__force __be32)tmp);
  3516. c2s_map[4] = tmp & 0xff;
  3517. c2s_map[5] = (tmp >> 8) & 0xff;
  3518. c2s_map[6] = (tmp >> 16) & 0xff;
  3519. c2s_map[7] = (tmp >> 24) & 0xff;
  3520. tmp = SHMEM2_RD(bp, c2s_pcp_map_default[mfw_vn]);
  3521. tmp = (__force u32)be32_to_cpu((__force __be32)tmp);
  3522. *c2s_default = (tmp >> (8 * mfw_vn)) & 0xff;
  3523. }
  3524. /**
  3525. * bnx2x_setup_tc - routine to configure net_device for multi tc
  3526. *
  3527. * @netdev: net device to configure
  3528. * @tc: number of traffic classes to enable
  3529. *
  3530. * callback connected to the ndo_setup_tc function pointer
  3531. */
  3532. int bnx2x_setup_tc(struct net_device *dev, u8 num_tc)
  3533. {
  3534. struct bnx2x *bp = netdev_priv(dev);
  3535. u8 c2s_map[BNX2X_MAX_PRIORITY], c2s_def;
  3536. int cos, prio, count, offset;
  3537. /* setup tc must be called under rtnl lock */
  3538. ASSERT_RTNL();
  3539. /* no traffic classes requested. Aborting */
  3540. if (!num_tc) {
  3541. netdev_reset_tc(dev);
  3542. return 0;
  3543. }
  3544. /* requested to support too many traffic classes */
  3545. if (num_tc > bp->max_cos) {
  3546. BNX2X_ERR("support for too many traffic classes requested: %d. Max supported is %d\n",
  3547. num_tc, bp->max_cos);
  3548. return -EINVAL;
  3549. }
  3550. /* declare amount of supported traffic classes */
  3551. if (netdev_set_num_tc(dev, num_tc)) {
  3552. BNX2X_ERR("failed to declare %d traffic classes\n", num_tc);
  3553. return -EINVAL;
  3554. }
  3555. bnx2x_get_c2s_mapping(bp, c2s_map, &c2s_def);
  3556. /* configure priority to traffic class mapping */
  3557. for (prio = 0; prio < BNX2X_MAX_PRIORITY; prio++) {
  3558. int outer_prio = c2s_map[prio];
  3559. netdev_set_prio_tc_map(dev, prio, bp->prio_to_cos[outer_prio]);
  3560. DP(BNX2X_MSG_SP | NETIF_MSG_IFUP,
  3561. "mapping priority %d to tc %d\n",
  3562. outer_prio, bp->prio_to_cos[outer_prio]);
  3563. }
  3564. /* Use this configuration to differentiate tc0 from other COSes
  3565. This can be used for ets or pfc, and save the effort of setting
  3566. up a multio class queue disc or negotiating DCBX with a switch
  3567. netdev_set_prio_tc_map(dev, 0, 0);
  3568. DP(BNX2X_MSG_SP, "mapping priority %d to tc %d\n", 0, 0);
  3569. for (prio = 1; prio < 16; prio++) {
  3570. netdev_set_prio_tc_map(dev, prio, 1);
  3571. DP(BNX2X_MSG_SP, "mapping priority %d to tc %d\n", prio, 1);
  3572. } */
  3573. /* configure traffic class to transmission queue mapping */
  3574. for (cos = 0; cos < bp->max_cos; cos++) {
  3575. count = BNX2X_NUM_ETH_QUEUES(bp);
  3576. offset = cos * BNX2X_NUM_NON_CNIC_QUEUES(bp);
  3577. netdev_set_tc_queue(dev, cos, count, offset);
  3578. DP(BNX2X_MSG_SP | NETIF_MSG_IFUP,
  3579. "mapping tc %d to offset %d count %d\n",
  3580. cos, offset, count);
  3581. }
  3582. return 0;
  3583. }
  3584. int __bnx2x_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
  3585. struct tc_to_netdev *tc)
  3586. {
  3587. if (tc->type != TC_SETUP_MQPRIO)
  3588. return -EINVAL;
  3589. return bnx2x_setup_tc(dev, tc->tc);
  3590. }
  3591. /* called with rtnl_lock */
  3592. int bnx2x_change_mac_addr(struct net_device *dev, void *p)
  3593. {
  3594. struct sockaddr *addr = p;
  3595. struct bnx2x *bp = netdev_priv(dev);
  3596. int rc = 0;
  3597. if (!is_valid_ether_addr(addr->sa_data)) {
  3598. BNX2X_ERR("Requested MAC address is not valid\n");
  3599. return -EINVAL;
  3600. }
  3601. if (IS_MF_STORAGE_ONLY(bp)) {
  3602. BNX2X_ERR("Can't change address on STORAGE ONLY function\n");
  3603. return -EINVAL;
  3604. }
  3605. if (netif_running(dev)) {
  3606. rc = bnx2x_set_eth_mac(bp, false);
  3607. if (rc)
  3608. return rc;
  3609. }
  3610. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  3611. if (netif_running(dev))
  3612. rc = bnx2x_set_eth_mac(bp, true);
  3613. if (IS_PF(bp) && SHMEM2_HAS(bp, curr_cfg))
  3614. SHMEM2_WR(bp, curr_cfg, CURR_CFG_MET_OS);
  3615. return rc;
  3616. }
  3617. static void bnx2x_free_fp_mem_at(struct bnx2x *bp, int fp_index)
  3618. {
  3619. union host_hc_status_block *sb = &bnx2x_fp(bp, fp_index, status_blk);
  3620. struct bnx2x_fastpath *fp = &bp->fp[fp_index];
  3621. u8 cos;
  3622. /* Common */
  3623. if (IS_FCOE_IDX(fp_index)) {
  3624. memset(sb, 0, sizeof(union host_hc_status_block));
  3625. fp->status_blk_mapping = 0;
  3626. } else {
  3627. /* status blocks */
  3628. if (!CHIP_IS_E1x(bp))
  3629. BNX2X_PCI_FREE(sb->e2_sb,
  3630. bnx2x_fp(bp, fp_index,
  3631. status_blk_mapping),
  3632. sizeof(struct host_hc_status_block_e2));
  3633. else
  3634. BNX2X_PCI_FREE(sb->e1x_sb,
  3635. bnx2x_fp(bp, fp_index,
  3636. status_blk_mapping),
  3637. sizeof(struct host_hc_status_block_e1x));
  3638. }
  3639. /* Rx */
  3640. if (!skip_rx_queue(bp, fp_index)) {
  3641. bnx2x_free_rx_bds(fp);
  3642. /* fastpath rx rings: rx_buf rx_desc rx_comp */
  3643. BNX2X_FREE(bnx2x_fp(bp, fp_index, rx_buf_ring));
  3644. BNX2X_PCI_FREE(bnx2x_fp(bp, fp_index, rx_desc_ring),
  3645. bnx2x_fp(bp, fp_index, rx_desc_mapping),
  3646. sizeof(struct eth_rx_bd) * NUM_RX_BD);
  3647. BNX2X_PCI_FREE(bnx2x_fp(bp, fp_index, rx_comp_ring),
  3648. bnx2x_fp(bp, fp_index, rx_comp_mapping),
  3649. sizeof(struct eth_fast_path_rx_cqe) *
  3650. NUM_RCQ_BD);
  3651. /* SGE ring */
  3652. BNX2X_FREE(bnx2x_fp(bp, fp_index, rx_page_ring));
  3653. BNX2X_PCI_FREE(bnx2x_fp(bp, fp_index, rx_sge_ring),
  3654. bnx2x_fp(bp, fp_index, rx_sge_mapping),
  3655. BCM_PAGE_SIZE * NUM_RX_SGE_PAGES);
  3656. }
  3657. /* Tx */
  3658. if (!skip_tx_queue(bp, fp_index)) {
  3659. /* fastpath tx rings: tx_buf tx_desc */
  3660. for_each_cos_in_tx_queue(fp, cos) {
  3661. struct bnx2x_fp_txdata *txdata = fp->txdata_ptr[cos];
  3662. DP(NETIF_MSG_IFDOWN,
  3663. "freeing tx memory of fp %d cos %d cid %d\n",
  3664. fp_index, cos, txdata->cid);
  3665. BNX2X_FREE(txdata->tx_buf_ring);
  3666. BNX2X_PCI_FREE(txdata->tx_desc_ring,
  3667. txdata->tx_desc_mapping,
  3668. sizeof(union eth_tx_bd_types) * NUM_TX_BD);
  3669. }
  3670. }
  3671. /* end of fastpath */
  3672. }
  3673. static void bnx2x_free_fp_mem_cnic(struct bnx2x *bp)
  3674. {
  3675. int i;
  3676. for_each_cnic_queue(bp, i)
  3677. bnx2x_free_fp_mem_at(bp, i);
  3678. }
  3679. void bnx2x_free_fp_mem(struct bnx2x *bp)
  3680. {
  3681. int i;
  3682. for_each_eth_queue(bp, i)
  3683. bnx2x_free_fp_mem_at(bp, i);
  3684. }
  3685. static void set_sb_shortcuts(struct bnx2x *bp, int index)
  3686. {
  3687. union host_hc_status_block status_blk = bnx2x_fp(bp, index, status_blk);
  3688. if (!CHIP_IS_E1x(bp)) {
  3689. bnx2x_fp(bp, index, sb_index_values) =
  3690. (__le16 *)status_blk.e2_sb->sb.index_values;
  3691. bnx2x_fp(bp, index, sb_running_index) =
  3692. (__le16 *)status_blk.e2_sb->sb.running_index;
  3693. } else {
  3694. bnx2x_fp(bp, index, sb_index_values) =
  3695. (__le16 *)status_blk.e1x_sb->sb.index_values;
  3696. bnx2x_fp(bp, index, sb_running_index) =
  3697. (__le16 *)status_blk.e1x_sb->sb.running_index;
  3698. }
  3699. }
  3700. /* Returns the number of actually allocated BDs */
  3701. static int bnx2x_alloc_rx_bds(struct bnx2x_fastpath *fp,
  3702. int rx_ring_size)
  3703. {
  3704. struct bnx2x *bp = fp->bp;
  3705. u16 ring_prod, cqe_ring_prod;
  3706. int i, failure_cnt = 0;
  3707. fp->rx_comp_cons = 0;
  3708. cqe_ring_prod = ring_prod = 0;
  3709. /* This routine is called only during fo init so
  3710. * fp->eth_q_stats.rx_skb_alloc_failed = 0
  3711. */
  3712. for (i = 0; i < rx_ring_size; i++) {
  3713. if (bnx2x_alloc_rx_data(bp, fp, ring_prod, GFP_KERNEL) < 0) {
  3714. failure_cnt++;
  3715. continue;
  3716. }
  3717. ring_prod = NEXT_RX_IDX(ring_prod);
  3718. cqe_ring_prod = NEXT_RCQ_IDX(cqe_ring_prod);
  3719. WARN_ON(ring_prod <= (i - failure_cnt));
  3720. }
  3721. if (failure_cnt)
  3722. BNX2X_ERR("was only able to allocate %d rx skbs on queue[%d]\n",
  3723. i - failure_cnt, fp->index);
  3724. fp->rx_bd_prod = ring_prod;
  3725. /* Limit the CQE producer by the CQE ring size */
  3726. fp->rx_comp_prod = min_t(u16, NUM_RCQ_RINGS*RCQ_DESC_CNT,
  3727. cqe_ring_prod);
  3728. bnx2x_fp_stats(bp, fp)->eth_q_stats.rx_skb_alloc_failed += failure_cnt;
  3729. return i - failure_cnt;
  3730. }
  3731. static void bnx2x_set_next_page_rx_cq(struct bnx2x_fastpath *fp)
  3732. {
  3733. int i;
  3734. for (i = 1; i <= NUM_RCQ_RINGS; i++) {
  3735. struct eth_rx_cqe_next_page *nextpg;
  3736. nextpg = (struct eth_rx_cqe_next_page *)
  3737. &fp->rx_comp_ring[RCQ_DESC_CNT * i - 1];
  3738. nextpg->addr_hi =
  3739. cpu_to_le32(U64_HI(fp->rx_comp_mapping +
  3740. BCM_PAGE_SIZE*(i % NUM_RCQ_RINGS)));
  3741. nextpg->addr_lo =
  3742. cpu_to_le32(U64_LO(fp->rx_comp_mapping +
  3743. BCM_PAGE_SIZE*(i % NUM_RCQ_RINGS)));
  3744. }
  3745. }
  3746. static int bnx2x_alloc_fp_mem_at(struct bnx2x *bp, int index)
  3747. {
  3748. union host_hc_status_block *sb;
  3749. struct bnx2x_fastpath *fp = &bp->fp[index];
  3750. int ring_size = 0;
  3751. u8 cos;
  3752. int rx_ring_size = 0;
  3753. if (!bp->rx_ring_size && IS_MF_STORAGE_ONLY(bp)) {
  3754. rx_ring_size = MIN_RX_SIZE_NONTPA;
  3755. bp->rx_ring_size = rx_ring_size;
  3756. } else if (!bp->rx_ring_size) {
  3757. rx_ring_size = MAX_RX_AVAIL/BNX2X_NUM_RX_QUEUES(bp);
  3758. if (CHIP_IS_E3(bp)) {
  3759. u32 cfg = SHMEM_RD(bp,
  3760. dev_info.port_hw_config[BP_PORT(bp)].
  3761. default_cfg);
  3762. /* Decrease ring size for 1G functions */
  3763. if ((cfg & PORT_HW_CFG_NET_SERDES_IF_MASK) ==
  3764. PORT_HW_CFG_NET_SERDES_IF_SGMII)
  3765. rx_ring_size /= 10;
  3766. }
  3767. /* allocate at least number of buffers required by FW */
  3768. rx_ring_size = max_t(int, bp->disable_tpa ? MIN_RX_SIZE_NONTPA :
  3769. MIN_RX_SIZE_TPA, rx_ring_size);
  3770. bp->rx_ring_size = rx_ring_size;
  3771. } else /* if rx_ring_size specified - use it */
  3772. rx_ring_size = bp->rx_ring_size;
  3773. DP(BNX2X_MSG_SP, "calculated rx_ring_size %d\n", rx_ring_size);
  3774. /* Common */
  3775. sb = &bnx2x_fp(bp, index, status_blk);
  3776. if (!IS_FCOE_IDX(index)) {
  3777. /* status blocks */
  3778. if (!CHIP_IS_E1x(bp)) {
  3779. sb->e2_sb = BNX2X_PCI_ALLOC(&bnx2x_fp(bp, index, status_blk_mapping),
  3780. sizeof(struct host_hc_status_block_e2));
  3781. if (!sb->e2_sb)
  3782. goto alloc_mem_err;
  3783. } else {
  3784. sb->e1x_sb = BNX2X_PCI_ALLOC(&bnx2x_fp(bp, index, status_blk_mapping),
  3785. sizeof(struct host_hc_status_block_e1x));
  3786. if (!sb->e1x_sb)
  3787. goto alloc_mem_err;
  3788. }
  3789. }
  3790. /* FCoE Queue uses Default SB and doesn't ACK the SB, thus no need to
  3791. * set shortcuts for it.
  3792. */
  3793. if (!IS_FCOE_IDX(index))
  3794. set_sb_shortcuts(bp, index);
  3795. /* Tx */
  3796. if (!skip_tx_queue(bp, index)) {
  3797. /* fastpath tx rings: tx_buf tx_desc */
  3798. for_each_cos_in_tx_queue(fp, cos) {
  3799. struct bnx2x_fp_txdata *txdata = fp->txdata_ptr[cos];
  3800. DP(NETIF_MSG_IFUP,
  3801. "allocating tx memory of fp %d cos %d\n",
  3802. index, cos);
  3803. txdata->tx_buf_ring = kcalloc(NUM_TX_BD,
  3804. sizeof(struct sw_tx_bd),
  3805. GFP_KERNEL);
  3806. if (!txdata->tx_buf_ring)
  3807. goto alloc_mem_err;
  3808. txdata->tx_desc_ring = BNX2X_PCI_ALLOC(&txdata->tx_desc_mapping,
  3809. sizeof(union eth_tx_bd_types) * NUM_TX_BD);
  3810. if (!txdata->tx_desc_ring)
  3811. goto alloc_mem_err;
  3812. }
  3813. }
  3814. /* Rx */
  3815. if (!skip_rx_queue(bp, index)) {
  3816. /* fastpath rx rings: rx_buf rx_desc rx_comp */
  3817. bnx2x_fp(bp, index, rx_buf_ring) =
  3818. kcalloc(NUM_RX_BD, sizeof(struct sw_rx_bd), GFP_KERNEL);
  3819. if (!bnx2x_fp(bp, index, rx_buf_ring))
  3820. goto alloc_mem_err;
  3821. bnx2x_fp(bp, index, rx_desc_ring) =
  3822. BNX2X_PCI_ALLOC(&bnx2x_fp(bp, index, rx_desc_mapping),
  3823. sizeof(struct eth_rx_bd) * NUM_RX_BD);
  3824. if (!bnx2x_fp(bp, index, rx_desc_ring))
  3825. goto alloc_mem_err;
  3826. /* Seed all CQEs by 1s */
  3827. bnx2x_fp(bp, index, rx_comp_ring) =
  3828. BNX2X_PCI_FALLOC(&bnx2x_fp(bp, index, rx_comp_mapping),
  3829. sizeof(struct eth_fast_path_rx_cqe) * NUM_RCQ_BD);
  3830. if (!bnx2x_fp(bp, index, rx_comp_ring))
  3831. goto alloc_mem_err;
  3832. /* SGE ring */
  3833. bnx2x_fp(bp, index, rx_page_ring) =
  3834. kcalloc(NUM_RX_SGE, sizeof(struct sw_rx_page),
  3835. GFP_KERNEL);
  3836. if (!bnx2x_fp(bp, index, rx_page_ring))
  3837. goto alloc_mem_err;
  3838. bnx2x_fp(bp, index, rx_sge_ring) =
  3839. BNX2X_PCI_ALLOC(&bnx2x_fp(bp, index, rx_sge_mapping),
  3840. BCM_PAGE_SIZE * NUM_RX_SGE_PAGES);
  3841. if (!bnx2x_fp(bp, index, rx_sge_ring))
  3842. goto alloc_mem_err;
  3843. /* RX BD ring */
  3844. bnx2x_set_next_page_rx_bd(fp);
  3845. /* CQ ring */
  3846. bnx2x_set_next_page_rx_cq(fp);
  3847. /* BDs */
  3848. ring_size = bnx2x_alloc_rx_bds(fp, rx_ring_size);
  3849. if (ring_size < rx_ring_size)
  3850. goto alloc_mem_err;
  3851. }
  3852. return 0;
  3853. /* handles low memory cases */
  3854. alloc_mem_err:
  3855. BNX2X_ERR("Unable to allocate full memory for queue %d (size %d)\n",
  3856. index, ring_size);
  3857. /* FW will drop all packets if queue is not big enough,
  3858. * In these cases we disable the queue
  3859. * Min size is different for OOO, TPA and non-TPA queues
  3860. */
  3861. if (ring_size < (fp->mode == TPA_MODE_DISABLED ?
  3862. MIN_RX_SIZE_NONTPA : MIN_RX_SIZE_TPA)) {
  3863. /* release memory allocated for this queue */
  3864. bnx2x_free_fp_mem_at(bp, index);
  3865. return -ENOMEM;
  3866. }
  3867. return 0;
  3868. }
  3869. static int bnx2x_alloc_fp_mem_cnic(struct bnx2x *bp)
  3870. {
  3871. if (!NO_FCOE(bp))
  3872. /* FCoE */
  3873. if (bnx2x_alloc_fp_mem_at(bp, FCOE_IDX(bp)))
  3874. /* we will fail load process instead of mark
  3875. * NO_FCOE_FLAG
  3876. */
  3877. return -ENOMEM;
  3878. return 0;
  3879. }
  3880. static int bnx2x_alloc_fp_mem(struct bnx2x *bp)
  3881. {
  3882. int i;
  3883. /* 1. Allocate FP for leading - fatal if error
  3884. * 2. Allocate RSS - fix number of queues if error
  3885. */
  3886. /* leading */
  3887. if (bnx2x_alloc_fp_mem_at(bp, 0))
  3888. return -ENOMEM;
  3889. /* RSS */
  3890. for_each_nondefault_eth_queue(bp, i)
  3891. if (bnx2x_alloc_fp_mem_at(bp, i))
  3892. break;
  3893. /* handle memory failures */
  3894. if (i != BNX2X_NUM_ETH_QUEUES(bp)) {
  3895. int delta = BNX2X_NUM_ETH_QUEUES(bp) - i;
  3896. WARN_ON(delta < 0);
  3897. bnx2x_shrink_eth_fp(bp, delta);
  3898. if (CNIC_SUPPORT(bp))
  3899. /* move non eth FPs next to last eth FP
  3900. * must be done in that order
  3901. * FCOE_IDX < FWD_IDX < OOO_IDX
  3902. */
  3903. /* move FCoE fp even NO_FCOE_FLAG is on */
  3904. bnx2x_move_fp(bp, FCOE_IDX(bp), FCOE_IDX(bp) - delta);
  3905. bp->num_ethernet_queues -= delta;
  3906. bp->num_queues = bp->num_ethernet_queues +
  3907. bp->num_cnic_queues;
  3908. BNX2X_ERR("Adjusted num of queues from %d to %d\n",
  3909. bp->num_queues + delta, bp->num_queues);
  3910. }
  3911. return 0;
  3912. }
  3913. void bnx2x_free_mem_bp(struct bnx2x *bp)
  3914. {
  3915. int i;
  3916. for (i = 0; i < bp->fp_array_size; i++)
  3917. kfree(bp->fp[i].tpa_info);
  3918. kfree(bp->fp);
  3919. kfree(bp->sp_objs);
  3920. kfree(bp->fp_stats);
  3921. kfree(bp->bnx2x_txq);
  3922. kfree(bp->msix_table);
  3923. kfree(bp->ilt);
  3924. }
  3925. int bnx2x_alloc_mem_bp(struct bnx2x *bp)
  3926. {
  3927. struct bnx2x_fastpath *fp;
  3928. struct msix_entry *tbl;
  3929. struct bnx2x_ilt *ilt;
  3930. int msix_table_size = 0;
  3931. int fp_array_size, txq_array_size;
  3932. int i;
  3933. /*
  3934. * The biggest MSI-X table we might need is as a maximum number of fast
  3935. * path IGU SBs plus default SB (for PF only).
  3936. */
  3937. msix_table_size = bp->igu_sb_cnt;
  3938. if (IS_PF(bp))
  3939. msix_table_size++;
  3940. BNX2X_DEV_INFO("msix_table_size %d\n", msix_table_size);
  3941. /* fp array: RSS plus CNIC related L2 queues */
  3942. fp_array_size = BNX2X_MAX_RSS_COUNT(bp) + CNIC_SUPPORT(bp);
  3943. bp->fp_array_size = fp_array_size;
  3944. BNX2X_DEV_INFO("fp_array_size %d\n", bp->fp_array_size);
  3945. fp = kcalloc(bp->fp_array_size, sizeof(*fp), GFP_KERNEL);
  3946. if (!fp)
  3947. goto alloc_err;
  3948. for (i = 0; i < bp->fp_array_size; i++) {
  3949. fp[i].tpa_info =
  3950. kcalloc(ETH_MAX_AGGREGATION_QUEUES_E1H_E2,
  3951. sizeof(struct bnx2x_agg_info), GFP_KERNEL);
  3952. if (!(fp[i].tpa_info))
  3953. goto alloc_err;
  3954. }
  3955. bp->fp = fp;
  3956. /* allocate sp objs */
  3957. bp->sp_objs = kcalloc(bp->fp_array_size, sizeof(struct bnx2x_sp_objs),
  3958. GFP_KERNEL);
  3959. if (!bp->sp_objs)
  3960. goto alloc_err;
  3961. /* allocate fp_stats */
  3962. bp->fp_stats = kcalloc(bp->fp_array_size, sizeof(struct bnx2x_fp_stats),
  3963. GFP_KERNEL);
  3964. if (!bp->fp_stats)
  3965. goto alloc_err;
  3966. /* Allocate memory for the transmission queues array */
  3967. txq_array_size =
  3968. BNX2X_MAX_RSS_COUNT(bp) * BNX2X_MULTI_TX_COS + CNIC_SUPPORT(bp);
  3969. BNX2X_DEV_INFO("txq_array_size %d", txq_array_size);
  3970. bp->bnx2x_txq = kcalloc(txq_array_size, sizeof(struct bnx2x_fp_txdata),
  3971. GFP_KERNEL);
  3972. if (!bp->bnx2x_txq)
  3973. goto alloc_err;
  3974. /* msix table */
  3975. tbl = kcalloc(msix_table_size, sizeof(*tbl), GFP_KERNEL);
  3976. if (!tbl)
  3977. goto alloc_err;
  3978. bp->msix_table = tbl;
  3979. /* ilt */
  3980. ilt = kzalloc(sizeof(*ilt), GFP_KERNEL);
  3981. if (!ilt)
  3982. goto alloc_err;
  3983. bp->ilt = ilt;
  3984. return 0;
  3985. alloc_err:
  3986. bnx2x_free_mem_bp(bp);
  3987. return -ENOMEM;
  3988. }
  3989. int bnx2x_reload_if_running(struct net_device *dev)
  3990. {
  3991. struct bnx2x *bp = netdev_priv(dev);
  3992. if (unlikely(!netif_running(dev)))
  3993. return 0;
  3994. bnx2x_nic_unload(bp, UNLOAD_NORMAL, true);
  3995. return bnx2x_nic_load(bp, LOAD_NORMAL);
  3996. }
  3997. int bnx2x_get_cur_phy_idx(struct bnx2x *bp)
  3998. {
  3999. u32 sel_phy_idx = 0;
  4000. if (bp->link_params.num_phys <= 1)
  4001. return INT_PHY;
  4002. if (bp->link_vars.link_up) {
  4003. sel_phy_idx = EXT_PHY1;
  4004. /* In case link is SERDES, check if the EXT_PHY2 is the one */
  4005. if ((bp->link_vars.link_status & LINK_STATUS_SERDES_LINK) &&
  4006. (bp->link_params.phy[EXT_PHY2].supported & SUPPORTED_FIBRE))
  4007. sel_phy_idx = EXT_PHY2;
  4008. } else {
  4009. switch (bnx2x_phy_selection(&bp->link_params)) {
  4010. case PORT_HW_CFG_PHY_SELECTION_HARDWARE_DEFAULT:
  4011. case PORT_HW_CFG_PHY_SELECTION_FIRST_PHY:
  4012. case PORT_HW_CFG_PHY_SELECTION_FIRST_PHY_PRIORITY:
  4013. sel_phy_idx = EXT_PHY1;
  4014. break;
  4015. case PORT_HW_CFG_PHY_SELECTION_SECOND_PHY:
  4016. case PORT_HW_CFG_PHY_SELECTION_SECOND_PHY_PRIORITY:
  4017. sel_phy_idx = EXT_PHY2;
  4018. break;
  4019. }
  4020. }
  4021. return sel_phy_idx;
  4022. }
  4023. int bnx2x_get_link_cfg_idx(struct bnx2x *bp)
  4024. {
  4025. u32 sel_phy_idx = bnx2x_get_cur_phy_idx(bp);
  4026. /*
  4027. * The selected activated PHY is always after swapping (in case PHY
  4028. * swapping is enabled). So when swapping is enabled, we need to reverse
  4029. * the configuration
  4030. */
  4031. if (bp->link_params.multi_phy_config &
  4032. PORT_HW_CFG_PHY_SWAPPED_ENABLED) {
  4033. if (sel_phy_idx == EXT_PHY1)
  4034. sel_phy_idx = EXT_PHY2;
  4035. else if (sel_phy_idx == EXT_PHY2)
  4036. sel_phy_idx = EXT_PHY1;
  4037. }
  4038. return LINK_CONFIG_IDX(sel_phy_idx);
  4039. }
  4040. #ifdef NETDEV_FCOE_WWNN
  4041. int bnx2x_fcoe_get_wwn(struct net_device *dev, u64 *wwn, int type)
  4042. {
  4043. struct bnx2x *bp = netdev_priv(dev);
  4044. struct cnic_eth_dev *cp = &bp->cnic_eth_dev;
  4045. switch (type) {
  4046. case NETDEV_FCOE_WWNN:
  4047. *wwn = HILO_U64(cp->fcoe_wwn_node_name_hi,
  4048. cp->fcoe_wwn_node_name_lo);
  4049. break;
  4050. case NETDEV_FCOE_WWPN:
  4051. *wwn = HILO_U64(cp->fcoe_wwn_port_name_hi,
  4052. cp->fcoe_wwn_port_name_lo);
  4053. break;
  4054. default:
  4055. BNX2X_ERR("Wrong WWN type requested - %d\n", type);
  4056. return -EINVAL;
  4057. }
  4058. return 0;
  4059. }
  4060. #endif
  4061. /* called with rtnl_lock */
  4062. int bnx2x_change_mtu(struct net_device *dev, int new_mtu)
  4063. {
  4064. struct bnx2x *bp = netdev_priv(dev);
  4065. if (pci_num_vf(bp->pdev)) {
  4066. DP(BNX2X_MSG_IOV, "VFs are enabled, can not change MTU\n");
  4067. return -EPERM;
  4068. }
  4069. if (bp->recovery_state != BNX2X_RECOVERY_DONE) {
  4070. BNX2X_ERR("Can't perform change MTU during parity recovery\n");
  4071. return -EAGAIN;
  4072. }
  4073. if ((new_mtu > ETH_MAX_JUMBO_PACKET_SIZE) ||
  4074. ((new_mtu + ETH_HLEN) < ETH_MIN_PACKET_SIZE)) {
  4075. BNX2X_ERR("Can't support requested MTU size\n");
  4076. return -EINVAL;
  4077. }
  4078. /* This does not race with packet allocation
  4079. * because the actual alloc size is
  4080. * only updated as part of load
  4081. */
  4082. dev->mtu = new_mtu;
  4083. if (IS_PF(bp) && SHMEM2_HAS(bp, curr_cfg))
  4084. SHMEM2_WR(bp, curr_cfg, CURR_CFG_MET_OS);
  4085. return bnx2x_reload_if_running(dev);
  4086. }
  4087. netdev_features_t bnx2x_fix_features(struct net_device *dev,
  4088. netdev_features_t features)
  4089. {
  4090. struct bnx2x *bp = netdev_priv(dev);
  4091. if (pci_num_vf(bp->pdev)) {
  4092. netdev_features_t changed = dev->features ^ features;
  4093. /* Revert the requested changes in features if they
  4094. * would require internal reload of PF in bnx2x_set_features().
  4095. */
  4096. if (!(features & NETIF_F_RXCSUM) && !bp->disable_tpa) {
  4097. features &= ~NETIF_F_RXCSUM;
  4098. features |= dev->features & NETIF_F_RXCSUM;
  4099. }
  4100. if (changed & NETIF_F_LOOPBACK) {
  4101. features &= ~NETIF_F_LOOPBACK;
  4102. features |= dev->features & NETIF_F_LOOPBACK;
  4103. }
  4104. }
  4105. /* TPA requires Rx CSUM offloading */
  4106. if (!(features & NETIF_F_RXCSUM)) {
  4107. features &= ~NETIF_F_LRO;
  4108. features &= ~NETIF_F_GRO;
  4109. }
  4110. return features;
  4111. }
  4112. int bnx2x_set_features(struct net_device *dev, netdev_features_t features)
  4113. {
  4114. struct bnx2x *bp = netdev_priv(dev);
  4115. netdev_features_t changes = features ^ dev->features;
  4116. bool bnx2x_reload = false;
  4117. int rc;
  4118. /* VFs or non SRIOV PFs should be able to change loopback feature */
  4119. if (!pci_num_vf(bp->pdev)) {
  4120. if (features & NETIF_F_LOOPBACK) {
  4121. if (bp->link_params.loopback_mode != LOOPBACK_BMAC) {
  4122. bp->link_params.loopback_mode = LOOPBACK_BMAC;
  4123. bnx2x_reload = true;
  4124. }
  4125. } else {
  4126. if (bp->link_params.loopback_mode != LOOPBACK_NONE) {
  4127. bp->link_params.loopback_mode = LOOPBACK_NONE;
  4128. bnx2x_reload = true;
  4129. }
  4130. }
  4131. }
  4132. /* if GRO is changed while LRO is enabled, don't force a reload */
  4133. if ((changes & NETIF_F_GRO) && (features & NETIF_F_LRO))
  4134. changes &= ~NETIF_F_GRO;
  4135. /* if GRO is changed while HW TPA is off, don't force a reload */
  4136. if ((changes & NETIF_F_GRO) && bp->disable_tpa)
  4137. changes &= ~NETIF_F_GRO;
  4138. if (changes)
  4139. bnx2x_reload = true;
  4140. if (bnx2x_reload) {
  4141. if (bp->recovery_state == BNX2X_RECOVERY_DONE) {
  4142. dev->features = features;
  4143. rc = bnx2x_reload_if_running(dev);
  4144. return rc ? rc : 1;
  4145. }
  4146. /* else: bnx2x_nic_load() will be called at end of recovery */
  4147. }
  4148. return 0;
  4149. }
  4150. void bnx2x_tx_timeout(struct net_device *dev)
  4151. {
  4152. struct bnx2x *bp = netdev_priv(dev);
  4153. #ifdef BNX2X_STOP_ON_ERROR
  4154. if (!bp->panic)
  4155. bnx2x_panic();
  4156. #endif
  4157. /* This allows the netif to be shutdown gracefully before resetting */
  4158. bnx2x_schedule_sp_rtnl(bp, BNX2X_SP_RTNL_TX_TIMEOUT, 0);
  4159. }
  4160. int bnx2x_suspend(struct pci_dev *pdev, pm_message_t state)
  4161. {
  4162. struct net_device *dev = pci_get_drvdata(pdev);
  4163. struct bnx2x *bp;
  4164. if (!dev) {
  4165. dev_err(&pdev->dev, "BAD net device from bnx2x_init_one\n");
  4166. return -ENODEV;
  4167. }
  4168. bp = netdev_priv(dev);
  4169. rtnl_lock();
  4170. pci_save_state(pdev);
  4171. if (!netif_running(dev)) {
  4172. rtnl_unlock();
  4173. return 0;
  4174. }
  4175. netif_device_detach(dev);
  4176. bnx2x_nic_unload(bp, UNLOAD_CLOSE, false);
  4177. bnx2x_set_power_state(bp, pci_choose_state(pdev, state));
  4178. rtnl_unlock();
  4179. return 0;
  4180. }
  4181. int bnx2x_resume(struct pci_dev *pdev)
  4182. {
  4183. struct net_device *dev = pci_get_drvdata(pdev);
  4184. struct bnx2x *bp;
  4185. int rc;
  4186. if (!dev) {
  4187. dev_err(&pdev->dev, "BAD net device from bnx2x_init_one\n");
  4188. return -ENODEV;
  4189. }
  4190. bp = netdev_priv(dev);
  4191. if (bp->recovery_state != BNX2X_RECOVERY_DONE) {
  4192. BNX2X_ERR("Handling parity error recovery. Try again later\n");
  4193. return -EAGAIN;
  4194. }
  4195. rtnl_lock();
  4196. pci_restore_state(pdev);
  4197. if (!netif_running(dev)) {
  4198. rtnl_unlock();
  4199. return 0;
  4200. }
  4201. bnx2x_set_power_state(bp, PCI_D0);
  4202. netif_device_attach(dev);
  4203. rc = bnx2x_nic_load(bp, LOAD_OPEN);
  4204. rtnl_unlock();
  4205. return rc;
  4206. }
  4207. void bnx2x_set_ctx_validation(struct bnx2x *bp, struct eth_context *cxt,
  4208. u32 cid)
  4209. {
  4210. if (!cxt) {
  4211. BNX2X_ERR("bad context pointer %p\n", cxt);
  4212. return;
  4213. }
  4214. /* ustorm cxt validation */
  4215. cxt->ustorm_ag_context.cdu_usage =
  4216. CDU_RSRVD_VALUE_TYPE_A(HW_CID(bp, cid),
  4217. CDU_REGION_NUMBER_UCM_AG, ETH_CONNECTION_TYPE);
  4218. /* xcontext validation */
  4219. cxt->xstorm_ag_context.cdu_reserved =
  4220. CDU_RSRVD_VALUE_TYPE_A(HW_CID(bp, cid),
  4221. CDU_REGION_NUMBER_XCM_AG, ETH_CONNECTION_TYPE);
  4222. }
  4223. static void storm_memset_hc_timeout(struct bnx2x *bp, u8 port,
  4224. u8 fw_sb_id, u8 sb_index,
  4225. u8 ticks)
  4226. {
  4227. u32 addr = BAR_CSTRORM_INTMEM +
  4228. CSTORM_STATUS_BLOCK_DATA_TIMEOUT_OFFSET(fw_sb_id, sb_index);
  4229. REG_WR8(bp, addr, ticks);
  4230. DP(NETIF_MSG_IFUP,
  4231. "port %x fw_sb_id %d sb_index %d ticks %d\n",
  4232. port, fw_sb_id, sb_index, ticks);
  4233. }
  4234. static void storm_memset_hc_disable(struct bnx2x *bp, u8 port,
  4235. u16 fw_sb_id, u8 sb_index,
  4236. u8 disable)
  4237. {
  4238. u32 enable_flag = disable ? 0 : (1 << HC_INDEX_DATA_HC_ENABLED_SHIFT);
  4239. u32 addr = BAR_CSTRORM_INTMEM +
  4240. CSTORM_STATUS_BLOCK_DATA_FLAGS_OFFSET(fw_sb_id, sb_index);
  4241. u8 flags = REG_RD8(bp, addr);
  4242. /* clear and set */
  4243. flags &= ~HC_INDEX_DATA_HC_ENABLED;
  4244. flags |= enable_flag;
  4245. REG_WR8(bp, addr, flags);
  4246. DP(NETIF_MSG_IFUP,
  4247. "port %x fw_sb_id %d sb_index %d disable %d\n",
  4248. port, fw_sb_id, sb_index, disable);
  4249. }
  4250. void bnx2x_update_coalesce_sb_index(struct bnx2x *bp, u8 fw_sb_id,
  4251. u8 sb_index, u8 disable, u16 usec)
  4252. {
  4253. int port = BP_PORT(bp);
  4254. u8 ticks = usec / BNX2X_BTR;
  4255. storm_memset_hc_timeout(bp, port, fw_sb_id, sb_index, ticks);
  4256. disable = disable ? 1 : (usec ? 0 : 1);
  4257. storm_memset_hc_disable(bp, port, fw_sb_id, sb_index, disable);
  4258. }
  4259. void bnx2x_schedule_sp_rtnl(struct bnx2x *bp, enum sp_rtnl_flag flag,
  4260. u32 verbose)
  4261. {
  4262. smp_mb__before_atomic();
  4263. set_bit(flag, &bp->sp_rtnl_state);
  4264. smp_mb__after_atomic();
  4265. DP((BNX2X_MSG_SP | verbose), "Scheduling sp_rtnl task [Flag: %d]\n",
  4266. flag);
  4267. schedule_delayed_work(&bp->sp_rtnl_task, 0);
  4268. }