bnx2x_cmn.c 121 KB

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