cxgb4_main.c 131 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827
  1. /*
  2. * This file is part of the Chelsio T4 Ethernet driver for Linux.
  3. *
  4. * Copyright (c) 2003-2014 Chelsio Communications, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  35. #include <linux/bitmap.h>
  36. #include <linux/crc32.h>
  37. #include <linux/ctype.h>
  38. #include <linux/debugfs.h>
  39. #include <linux/err.h>
  40. #include <linux/etherdevice.h>
  41. #include <linux/firmware.h>
  42. #include <linux/if.h>
  43. #include <linux/if_vlan.h>
  44. #include <linux/init.h>
  45. #include <linux/log2.h>
  46. #include <linux/mdio.h>
  47. #include <linux/module.h>
  48. #include <linux/moduleparam.h>
  49. #include <linux/mutex.h>
  50. #include <linux/netdevice.h>
  51. #include <linux/pci.h>
  52. #include <linux/aer.h>
  53. #include <linux/rtnetlink.h>
  54. #include <linux/sched.h>
  55. #include <linux/seq_file.h>
  56. #include <linux/sockios.h>
  57. #include <linux/vmalloc.h>
  58. #include <linux/workqueue.h>
  59. #include <net/neighbour.h>
  60. #include <net/netevent.h>
  61. #include <net/addrconf.h>
  62. #include <net/bonding.h>
  63. #include <net/addrconf.h>
  64. #include <asm/uaccess.h>
  65. #include "cxgb4.h"
  66. #include "t4_regs.h"
  67. #include "t4_values.h"
  68. #include "t4_msg.h"
  69. #include "t4fw_api.h"
  70. #include "t4fw_version.h"
  71. #include "cxgb4_dcb.h"
  72. #include "cxgb4_debugfs.h"
  73. #include "clip_tbl.h"
  74. #include "l2t.h"
  75. char cxgb4_driver_name[] = KBUILD_MODNAME;
  76. #ifdef DRV_VERSION
  77. #undef DRV_VERSION
  78. #endif
  79. #define DRV_VERSION "2.0.0-ko"
  80. const char cxgb4_driver_version[] = DRV_VERSION;
  81. #define DRV_DESC "Chelsio T4/T5 Network Driver"
  82. /* Host shadow copy of ingress filter entry. This is in host native format
  83. * and doesn't match the ordering or bit order, etc. of the hardware of the
  84. * firmware command. The use of bit-field structure elements is purely to
  85. * remind ourselves of the field size limitations and save memory in the case
  86. * where the filter table is large.
  87. */
  88. struct filter_entry {
  89. /* Administrative fields for filter.
  90. */
  91. u32 valid:1; /* filter allocated and valid */
  92. u32 locked:1; /* filter is administratively locked */
  93. u32 pending:1; /* filter action is pending firmware reply */
  94. u32 smtidx:8; /* Source MAC Table index for smac */
  95. struct l2t_entry *l2t; /* Layer Two Table entry for dmac */
  96. /* The filter itself. Most of this is a straight copy of information
  97. * provided by the extended ioctl(). Some fields are translated to
  98. * internal forms -- for instance the Ingress Queue ID passed in from
  99. * the ioctl() is translated into the Absolute Ingress Queue ID.
  100. */
  101. struct ch_filter_specification fs;
  102. };
  103. #define DFLT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK | \
  104. NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP |\
  105. NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR)
  106. /* Macros needed to support the PCI Device ID Table ...
  107. */
  108. #define CH_PCI_DEVICE_ID_TABLE_DEFINE_BEGIN \
  109. static const struct pci_device_id cxgb4_pci_tbl[] = {
  110. #define CH_PCI_DEVICE_ID_FUNCTION 0x4
  111. /* Include PCI Device IDs for both PF4 and PF0-3 so our PCI probe() routine is
  112. * called for both.
  113. */
  114. #define CH_PCI_DEVICE_ID_FUNCTION2 0x0
  115. #define CH_PCI_ID_TABLE_ENTRY(devid) \
  116. {PCI_VDEVICE(CHELSIO, (devid)), 4}
  117. #define CH_PCI_DEVICE_ID_TABLE_DEFINE_END \
  118. { 0, } \
  119. }
  120. #include "t4_pci_id_tbl.h"
  121. #define FW4_FNAME "cxgb4/t4fw.bin"
  122. #define FW5_FNAME "cxgb4/t5fw.bin"
  123. #define FW4_CFNAME "cxgb4/t4-config.txt"
  124. #define FW5_CFNAME "cxgb4/t5-config.txt"
  125. MODULE_DESCRIPTION(DRV_DESC);
  126. MODULE_AUTHOR("Chelsio Communications");
  127. MODULE_LICENSE("Dual BSD/GPL");
  128. MODULE_VERSION(DRV_VERSION);
  129. MODULE_DEVICE_TABLE(pci, cxgb4_pci_tbl);
  130. MODULE_FIRMWARE(FW4_FNAME);
  131. MODULE_FIRMWARE(FW5_FNAME);
  132. /*
  133. * Normally we're willing to become the firmware's Master PF but will be happy
  134. * if another PF has already become the Master and initialized the adapter.
  135. * Setting "force_init" will cause this driver to forcibly establish itself as
  136. * the Master PF and initialize the adapter.
  137. */
  138. static uint force_init;
  139. module_param(force_init, uint, 0644);
  140. MODULE_PARM_DESC(force_init, "Forcibly become Master PF and initialize adapter");
  141. /*
  142. * Normally if the firmware we connect to has Configuration File support, we
  143. * use that and only fall back to the old Driver-based initialization if the
  144. * Configuration File fails for some reason. If force_old_init is set, then
  145. * we'll always use the old Driver-based initialization sequence.
  146. */
  147. static uint force_old_init;
  148. module_param(force_old_init, uint, 0644);
  149. MODULE_PARM_DESC(force_old_init, "Force old initialization sequence, deprecated"
  150. " parameter");
  151. static int dflt_msg_enable = DFLT_MSG_ENABLE;
  152. module_param(dflt_msg_enable, int, 0644);
  153. MODULE_PARM_DESC(dflt_msg_enable, "Chelsio T4 default message enable bitmap");
  154. /*
  155. * The driver uses the best interrupt scheme available on a platform in the
  156. * order MSI-X, MSI, legacy INTx interrupts. This parameter determines which
  157. * of these schemes the driver may consider as follows:
  158. *
  159. * msi = 2: choose from among all three options
  160. * msi = 1: only consider MSI and INTx interrupts
  161. * msi = 0: force INTx interrupts
  162. */
  163. static int msi = 2;
  164. module_param(msi, int, 0644);
  165. MODULE_PARM_DESC(msi, "whether to use INTx (0), MSI (1) or MSI-X (2)");
  166. /*
  167. * Queue interrupt hold-off timer values. Queues default to the first of these
  168. * upon creation.
  169. */
  170. static unsigned int intr_holdoff[SGE_NTIMERS - 1] = { 5, 10, 20, 50, 100 };
  171. module_param_array(intr_holdoff, uint, NULL, 0644);
  172. MODULE_PARM_DESC(intr_holdoff, "values for queue interrupt hold-off timers "
  173. "0..4 in microseconds, deprecated parameter");
  174. static unsigned int intr_cnt[SGE_NCOUNTERS - 1] = { 4, 8, 16 };
  175. module_param_array(intr_cnt, uint, NULL, 0644);
  176. MODULE_PARM_DESC(intr_cnt,
  177. "thresholds 1..3 for queue interrupt packet counters, "
  178. "deprecated parameter");
  179. /*
  180. * Normally we tell the chip to deliver Ingress Packets into our DMA buffers
  181. * offset by 2 bytes in order to have the IP headers line up on 4-byte
  182. * boundaries. This is a requirement for many architectures which will throw
  183. * a machine check fault if an attempt is made to access one of the 4-byte IP
  184. * header fields on a non-4-byte boundary. And it's a major performance issue
  185. * even on some architectures which allow it like some implementations of the
  186. * x86 ISA. However, some architectures don't mind this and for some very
  187. * edge-case performance sensitive applications (like forwarding large volumes
  188. * of small packets), setting this DMA offset to 0 will decrease the number of
  189. * PCI-E Bus transfers enough to measurably affect performance.
  190. */
  191. static int rx_dma_offset = 2;
  192. static bool vf_acls;
  193. #ifdef CONFIG_PCI_IOV
  194. module_param(vf_acls, bool, 0644);
  195. MODULE_PARM_DESC(vf_acls, "if set enable virtualization L2 ACL enforcement, "
  196. "deprecated parameter");
  197. /* Configure the number of PCI-E Virtual Function which are to be instantiated
  198. * on SR-IOV Capable Physical Functions.
  199. */
  200. static unsigned int num_vf[NUM_OF_PF_WITH_SRIOV];
  201. module_param_array(num_vf, uint, NULL, 0644);
  202. MODULE_PARM_DESC(num_vf, "number of VFs for each of PFs 0-3");
  203. #endif
  204. /* TX Queue select used to determine what algorithm to use for selecting TX
  205. * queue. Select between the kernel provided function (select_queue=0) or user
  206. * cxgb_select_queue function (select_queue=1)
  207. *
  208. * Default: select_queue=0
  209. */
  210. static int select_queue;
  211. module_param(select_queue, int, 0644);
  212. MODULE_PARM_DESC(select_queue,
  213. "Select between kernel provided method of selecting or driver method of selecting TX queue. Default is kernel method.");
  214. static unsigned int tp_vlan_pri_map = HW_TPL_FR_MT_PR_IV_P_FC;
  215. module_param(tp_vlan_pri_map, uint, 0644);
  216. MODULE_PARM_DESC(tp_vlan_pri_map, "global compressed filter configuration, "
  217. "deprecated parameter");
  218. static struct dentry *cxgb4_debugfs_root;
  219. static LIST_HEAD(adapter_list);
  220. static DEFINE_MUTEX(uld_mutex);
  221. /* Adapter list to be accessed from atomic context */
  222. static LIST_HEAD(adap_rcu_list);
  223. static DEFINE_SPINLOCK(adap_rcu_lock);
  224. static struct cxgb4_uld_info ulds[CXGB4_ULD_MAX];
  225. static const char *uld_str[] = { "RDMA", "iSCSI" };
  226. static void link_report(struct net_device *dev)
  227. {
  228. if (!netif_carrier_ok(dev))
  229. netdev_info(dev, "link down\n");
  230. else {
  231. static const char *fc[] = { "no", "Rx", "Tx", "Tx/Rx" };
  232. const char *s = "10Mbps";
  233. const struct port_info *p = netdev_priv(dev);
  234. switch (p->link_cfg.speed) {
  235. case 10000:
  236. s = "10Gbps";
  237. break;
  238. case 1000:
  239. s = "1000Mbps";
  240. break;
  241. case 100:
  242. s = "100Mbps";
  243. break;
  244. case 40000:
  245. s = "40Gbps";
  246. break;
  247. }
  248. netdev_info(dev, "link up, %s, full-duplex, %s PAUSE\n", s,
  249. fc[p->link_cfg.fc]);
  250. }
  251. }
  252. #ifdef CONFIG_CHELSIO_T4_DCB
  253. /* Set up/tear down Data Center Bridging Priority mapping for a net device. */
  254. static void dcb_tx_queue_prio_enable(struct net_device *dev, int enable)
  255. {
  256. struct port_info *pi = netdev_priv(dev);
  257. struct adapter *adap = pi->adapter;
  258. struct sge_eth_txq *txq = &adap->sge.ethtxq[pi->first_qset];
  259. int i;
  260. /* We use a simple mapping of Port TX Queue Index to DCB
  261. * Priority when we're enabling DCB.
  262. */
  263. for (i = 0; i < pi->nqsets; i++, txq++) {
  264. u32 name, value;
  265. int err;
  266. name = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
  267. FW_PARAMS_PARAM_X_V(
  268. FW_PARAMS_PARAM_DMAQ_EQ_DCBPRIO_ETH) |
  269. FW_PARAMS_PARAM_YZ_V(txq->q.cntxt_id));
  270. value = enable ? i : 0xffffffff;
  271. /* Since we can be called while atomic (from "interrupt
  272. * level") we need to issue the Set Parameters Commannd
  273. * without sleeping (timeout < 0).
  274. */
  275. err = t4_set_params_nosleep(adap, adap->mbox, adap->fn, 0, 1,
  276. &name, &value);
  277. if (err)
  278. dev_err(adap->pdev_dev,
  279. "Can't %s DCB Priority on port %d, TX Queue %d: err=%d\n",
  280. enable ? "set" : "unset", pi->port_id, i, -err);
  281. else
  282. txq->dcb_prio = value;
  283. }
  284. }
  285. #endif /* CONFIG_CHELSIO_T4_DCB */
  286. void t4_os_link_changed(struct adapter *adapter, int port_id, int link_stat)
  287. {
  288. struct net_device *dev = adapter->port[port_id];
  289. /* Skip changes from disabled ports. */
  290. if (netif_running(dev) && link_stat != netif_carrier_ok(dev)) {
  291. if (link_stat)
  292. netif_carrier_on(dev);
  293. else {
  294. #ifdef CONFIG_CHELSIO_T4_DCB
  295. cxgb4_dcb_state_init(dev);
  296. dcb_tx_queue_prio_enable(dev, false);
  297. #endif /* CONFIG_CHELSIO_T4_DCB */
  298. netif_carrier_off(dev);
  299. }
  300. link_report(dev);
  301. }
  302. }
  303. void t4_os_portmod_changed(const struct adapter *adap, int port_id)
  304. {
  305. static const char *mod_str[] = {
  306. NULL, "LR", "SR", "ER", "passive DA", "active DA", "LRM"
  307. };
  308. const struct net_device *dev = adap->port[port_id];
  309. const struct port_info *pi = netdev_priv(dev);
  310. if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
  311. netdev_info(dev, "port module unplugged\n");
  312. else if (pi->mod_type < ARRAY_SIZE(mod_str))
  313. netdev_info(dev, "%s module inserted\n", mod_str[pi->mod_type]);
  314. }
  315. /*
  316. * Configure the exact and hash address filters to handle a port's multicast
  317. * and secondary unicast MAC addresses.
  318. */
  319. static int set_addr_filters(const struct net_device *dev, bool sleep)
  320. {
  321. u64 mhash = 0;
  322. u64 uhash = 0;
  323. bool free = true;
  324. u16 filt_idx[7];
  325. const u8 *addr[7];
  326. int ret, naddr = 0;
  327. const struct netdev_hw_addr *ha;
  328. int uc_cnt = netdev_uc_count(dev);
  329. int mc_cnt = netdev_mc_count(dev);
  330. const struct port_info *pi = netdev_priv(dev);
  331. unsigned int mb = pi->adapter->fn;
  332. /* first do the secondary unicast addresses */
  333. netdev_for_each_uc_addr(ha, dev) {
  334. addr[naddr++] = ha->addr;
  335. if (--uc_cnt == 0 || naddr >= ARRAY_SIZE(addr)) {
  336. ret = t4_alloc_mac_filt(pi->adapter, mb, pi->viid, free,
  337. naddr, addr, filt_idx, &uhash, sleep);
  338. if (ret < 0)
  339. return ret;
  340. free = false;
  341. naddr = 0;
  342. }
  343. }
  344. /* next set up the multicast addresses */
  345. netdev_for_each_mc_addr(ha, dev) {
  346. addr[naddr++] = ha->addr;
  347. if (--mc_cnt == 0 || naddr >= ARRAY_SIZE(addr)) {
  348. ret = t4_alloc_mac_filt(pi->adapter, mb, pi->viid, free,
  349. naddr, addr, filt_idx, &mhash, sleep);
  350. if (ret < 0)
  351. return ret;
  352. free = false;
  353. naddr = 0;
  354. }
  355. }
  356. return t4_set_addr_hash(pi->adapter, mb, pi->viid, uhash != 0,
  357. uhash | mhash, sleep);
  358. }
  359. int dbfifo_int_thresh = 10; /* 10 == 640 entry threshold */
  360. module_param(dbfifo_int_thresh, int, 0644);
  361. MODULE_PARM_DESC(dbfifo_int_thresh, "doorbell fifo interrupt threshold");
  362. /*
  363. * usecs to sleep while draining the dbfifo
  364. */
  365. static int dbfifo_drain_delay = 1000;
  366. module_param(dbfifo_drain_delay, int, 0644);
  367. MODULE_PARM_DESC(dbfifo_drain_delay,
  368. "usecs to sleep while draining the dbfifo");
  369. /*
  370. * Set Rx properties of a port, such as promiscruity, address filters, and MTU.
  371. * If @mtu is -1 it is left unchanged.
  372. */
  373. static int set_rxmode(struct net_device *dev, int mtu, bool sleep_ok)
  374. {
  375. int ret;
  376. struct port_info *pi = netdev_priv(dev);
  377. ret = set_addr_filters(dev, sleep_ok);
  378. if (ret == 0)
  379. ret = t4_set_rxmode(pi->adapter, pi->adapter->fn, pi->viid, mtu,
  380. (dev->flags & IFF_PROMISC) ? 1 : 0,
  381. (dev->flags & IFF_ALLMULTI) ? 1 : 0, 1, -1,
  382. sleep_ok);
  383. return ret;
  384. }
  385. /**
  386. * link_start - enable a port
  387. * @dev: the port to enable
  388. *
  389. * Performs the MAC and PHY actions needed to enable a port.
  390. */
  391. static int link_start(struct net_device *dev)
  392. {
  393. int ret;
  394. struct port_info *pi = netdev_priv(dev);
  395. unsigned int mb = pi->adapter->fn;
  396. /*
  397. * We do not set address filters and promiscuity here, the stack does
  398. * that step explicitly.
  399. */
  400. ret = t4_set_rxmode(pi->adapter, mb, pi->viid, dev->mtu, -1, -1, -1,
  401. !!(dev->features & NETIF_F_HW_VLAN_CTAG_RX), true);
  402. if (ret == 0) {
  403. ret = t4_change_mac(pi->adapter, mb, pi->viid,
  404. pi->xact_addr_filt, dev->dev_addr, true,
  405. true);
  406. if (ret >= 0) {
  407. pi->xact_addr_filt = ret;
  408. ret = 0;
  409. }
  410. }
  411. if (ret == 0)
  412. ret = t4_link_start(pi->adapter, mb, pi->tx_chan,
  413. &pi->link_cfg);
  414. if (ret == 0) {
  415. local_bh_disable();
  416. ret = t4_enable_vi_params(pi->adapter, mb, pi->viid, true,
  417. true, CXGB4_DCB_ENABLED);
  418. local_bh_enable();
  419. }
  420. return ret;
  421. }
  422. int cxgb4_dcb_enabled(const struct net_device *dev)
  423. {
  424. #ifdef CONFIG_CHELSIO_T4_DCB
  425. struct port_info *pi = netdev_priv(dev);
  426. if (!pi->dcb.enabled)
  427. return 0;
  428. return ((pi->dcb.state == CXGB4_DCB_STATE_FW_ALLSYNCED) ||
  429. (pi->dcb.state == CXGB4_DCB_STATE_HOST));
  430. #else
  431. return 0;
  432. #endif
  433. }
  434. EXPORT_SYMBOL(cxgb4_dcb_enabled);
  435. #ifdef CONFIG_CHELSIO_T4_DCB
  436. /* Handle a Data Center Bridging update message from the firmware. */
  437. static void dcb_rpl(struct adapter *adap, const struct fw_port_cmd *pcmd)
  438. {
  439. int port = FW_PORT_CMD_PORTID_G(ntohl(pcmd->op_to_portid));
  440. struct net_device *dev = adap->port[port];
  441. int old_dcb_enabled = cxgb4_dcb_enabled(dev);
  442. int new_dcb_enabled;
  443. cxgb4_dcb_handle_fw_update(adap, pcmd);
  444. new_dcb_enabled = cxgb4_dcb_enabled(dev);
  445. /* If the DCB has become enabled or disabled on the port then we're
  446. * going to need to set up/tear down DCB Priority parameters for the
  447. * TX Queues associated with the port.
  448. */
  449. if (new_dcb_enabled != old_dcb_enabled)
  450. dcb_tx_queue_prio_enable(dev, new_dcb_enabled);
  451. }
  452. #endif /* CONFIG_CHELSIO_T4_DCB */
  453. /* Clear a filter and release any of its resources that we own. This also
  454. * clears the filter's "pending" status.
  455. */
  456. static void clear_filter(struct adapter *adap, struct filter_entry *f)
  457. {
  458. /* If the new or old filter have loopback rewriteing rules then we'll
  459. * need to free any existing Layer Two Table (L2T) entries of the old
  460. * filter rule. The firmware will handle freeing up any Source MAC
  461. * Table (SMT) entries used for rewriting Source MAC Addresses in
  462. * loopback rules.
  463. */
  464. if (f->l2t)
  465. cxgb4_l2t_release(f->l2t);
  466. /* The zeroing of the filter rule below clears the filter valid,
  467. * pending, locked flags, l2t pointer, etc. so it's all we need for
  468. * this operation.
  469. */
  470. memset(f, 0, sizeof(*f));
  471. }
  472. /* Handle a filter write/deletion reply.
  473. */
  474. static void filter_rpl(struct adapter *adap, const struct cpl_set_tcb_rpl *rpl)
  475. {
  476. unsigned int idx = GET_TID(rpl);
  477. unsigned int nidx = idx - adap->tids.ftid_base;
  478. unsigned int ret;
  479. struct filter_entry *f;
  480. if (idx >= adap->tids.ftid_base && nidx <
  481. (adap->tids.nftids + adap->tids.nsftids)) {
  482. idx = nidx;
  483. ret = TCB_COOKIE_G(rpl->cookie);
  484. f = &adap->tids.ftid_tab[idx];
  485. if (ret == FW_FILTER_WR_FLT_DELETED) {
  486. /* Clear the filter when we get confirmation from the
  487. * hardware that the filter has been deleted.
  488. */
  489. clear_filter(adap, f);
  490. } else if (ret == FW_FILTER_WR_SMT_TBL_FULL) {
  491. dev_err(adap->pdev_dev, "filter %u setup failed due to full SMT\n",
  492. idx);
  493. clear_filter(adap, f);
  494. } else if (ret == FW_FILTER_WR_FLT_ADDED) {
  495. f->smtidx = (be64_to_cpu(rpl->oldval) >> 24) & 0xff;
  496. f->pending = 0; /* asynchronous setup completed */
  497. f->valid = 1;
  498. } else {
  499. /* Something went wrong. Issue a warning about the
  500. * problem and clear everything out.
  501. */
  502. dev_err(adap->pdev_dev, "filter %u setup failed with error %u\n",
  503. idx, ret);
  504. clear_filter(adap, f);
  505. }
  506. }
  507. }
  508. /* Response queue handler for the FW event queue.
  509. */
  510. static int fwevtq_handler(struct sge_rspq *q, const __be64 *rsp,
  511. const struct pkt_gl *gl)
  512. {
  513. u8 opcode = ((const struct rss_header *)rsp)->opcode;
  514. rsp++; /* skip RSS header */
  515. /* FW can send EGR_UPDATEs encapsulated in a CPL_FW4_MSG.
  516. */
  517. if (unlikely(opcode == CPL_FW4_MSG &&
  518. ((const struct cpl_fw4_msg *)rsp)->type == FW_TYPE_RSSCPL)) {
  519. rsp++;
  520. opcode = ((const struct rss_header *)rsp)->opcode;
  521. rsp++;
  522. if (opcode != CPL_SGE_EGR_UPDATE) {
  523. dev_err(q->adap->pdev_dev, "unexpected FW4/CPL %#x on FW event queue\n"
  524. , opcode);
  525. goto out;
  526. }
  527. }
  528. if (likely(opcode == CPL_SGE_EGR_UPDATE)) {
  529. const struct cpl_sge_egr_update *p = (void *)rsp;
  530. unsigned int qid = EGR_QID_G(ntohl(p->opcode_qid));
  531. struct sge_txq *txq;
  532. txq = q->adap->sge.egr_map[qid - q->adap->sge.egr_start];
  533. txq->restarts++;
  534. if ((u8 *)txq < (u8 *)q->adap->sge.ofldtxq) {
  535. struct sge_eth_txq *eq;
  536. eq = container_of(txq, struct sge_eth_txq, q);
  537. netif_tx_wake_queue(eq->txq);
  538. } else {
  539. struct sge_ofld_txq *oq;
  540. oq = container_of(txq, struct sge_ofld_txq, q);
  541. tasklet_schedule(&oq->qresume_tsk);
  542. }
  543. } else if (opcode == CPL_FW6_MSG || opcode == CPL_FW4_MSG) {
  544. const struct cpl_fw6_msg *p = (void *)rsp;
  545. #ifdef CONFIG_CHELSIO_T4_DCB
  546. const struct fw_port_cmd *pcmd = (const void *)p->data;
  547. unsigned int cmd = FW_CMD_OP_G(ntohl(pcmd->op_to_portid));
  548. unsigned int action =
  549. FW_PORT_CMD_ACTION_G(ntohl(pcmd->action_to_len16));
  550. if (cmd == FW_PORT_CMD &&
  551. action == FW_PORT_ACTION_GET_PORT_INFO) {
  552. int port = FW_PORT_CMD_PORTID_G(
  553. be32_to_cpu(pcmd->op_to_portid));
  554. struct net_device *dev = q->adap->port[port];
  555. int state_input = ((pcmd->u.info.dcbxdis_pkd &
  556. FW_PORT_CMD_DCBXDIS_F)
  557. ? CXGB4_DCB_INPUT_FW_DISABLED
  558. : CXGB4_DCB_INPUT_FW_ENABLED);
  559. cxgb4_dcb_state_fsm(dev, state_input);
  560. }
  561. if (cmd == FW_PORT_CMD &&
  562. action == FW_PORT_ACTION_L2_DCB_CFG)
  563. dcb_rpl(q->adap, pcmd);
  564. else
  565. #endif
  566. if (p->type == 0)
  567. t4_handle_fw_rpl(q->adap, p->data);
  568. } else if (opcode == CPL_L2T_WRITE_RPL) {
  569. const struct cpl_l2t_write_rpl *p = (void *)rsp;
  570. do_l2t_write_rpl(q->adap, p);
  571. } else if (opcode == CPL_SET_TCB_RPL) {
  572. const struct cpl_set_tcb_rpl *p = (void *)rsp;
  573. filter_rpl(q->adap, p);
  574. } else
  575. dev_err(q->adap->pdev_dev,
  576. "unexpected CPL %#x on FW event queue\n", opcode);
  577. out:
  578. return 0;
  579. }
  580. /**
  581. * uldrx_handler - response queue handler for ULD queues
  582. * @q: the response queue that received the packet
  583. * @rsp: the response queue descriptor holding the offload message
  584. * @gl: the gather list of packet fragments
  585. *
  586. * Deliver an ingress offload packet to a ULD. All processing is done by
  587. * the ULD, we just maintain statistics.
  588. */
  589. static int uldrx_handler(struct sge_rspq *q, const __be64 *rsp,
  590. const struct pkt_gl *gl)
  591. {
  592. struct sge_ofld_rxq *rxq = container_of(q, struct sge_ofld_rxq, rspq);
  593. /* FW can send CPLs encapsulated in a CPL_FW4_MSG.
  594. */
  595. if (((const struct rss_header *)rsp)->opcode == CPL_FW4_MSG &&
  596. ((const struct cpl_fw4_msg *)(rsp + 1))->type == FW_TYPE_RSSCPL)
  597. rsp += 2;
  598. if (ulds[q->uld].rx_handler(q->adap->uld_handle[q->uld], rsp, gl)) {
  599. rxq->stats.nomem++;
  600. return -1;
  601. }
  602. if (gl == NULL)
  603. rxq->stats.imm++;
  604. else if (gl == CXGB4_MSG_AN)
  605. rxq->stats.an++;
  606. else
  607. rxq->stats.pkts++;
  608. return 0;
  609. }
  610. static void disable_msi(struct adapter *adapter)
  611. {
  612. if (adapter->flags & USING_MSIX) {
  613. pci_disable_msix(adapter->pdev);
  614. adapter->flags &= ~USING_MSIX;
  615. } else if (adapter->flags & USING_MSI) {
  616. pci_disable_msi(adapter->pdev);
  617. adapter->flags &= ~USING_MSI;
  618. }
  619. }
  620. /*
  621. * Interrupt handler for non-data events used with MSI-X.
  622. */
  623. static irqreturn_t t4_nondata_intr(int irq, void *cookie)
  624. {
  625. struct adapter *adap = cookie;
  626. u32 v = t4_read_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A));
  627. if (v & PFSW_F) {
  628. adap->swintr = 1;
  629. t4_write_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A), v);
  630. }
  631. if (adap->flags & MASTER_PF)
  632. t4_slow_intr_handler(adap);
  633. return IRQ_HANDLED;
  634. }
  635. /*
  636. * Name the MSI-X interrupts.
  637. */
  638. static void name_msix_vecs(struct adapter *adap)
  639. {
  640. int i, j, msi_idx = 2, n = sizeof(adap->msix_info[0].desc);
  641. /* non-data interrupts */
  642. snprintf(adap->msix_info[0].desc, n, "%s", adap->port[0]->name);
  643. /* FW events */
  644. snprintf(adap->msix_info[1].desc, n, "%s-FWeventq",
  645. adap->port[0]->name);
  646. /* Ethernet queues */
  647. for_each_port(adap, j) {
  648. struct net_device *d = adap->port[j];
  649. const struct port_info *pi = netdev_priv(d);
  650. for (i = 0; i < pi->nqsets; i++, msi_idx++)
  651. snprintf(adap->msix_info[msi_idx].desc, n, "%s-Rx%d",
  652. d->name, i);
  653. }
  654. /* offload queues */
  655. for_each_ofldrxq(&adap->sge, i)
  656. snprintf(adap->msix_info[msi_idx++].desc, n, "%s-ofld%d",
  657. adap->port[0]->name, i);
  658. for_each_rdmarxq(&adap->sge, i)
  659. snprintf(adap->msix_info[msi_idx++].desc, n, "%s-rdma%d",
  660. adap->port[0]->name, i);
  661. for_each_rdmaciq(&adap->sge, i)
  662. snprintf(adap->msix_info[msi_idx++].desc, n, "%s-rdma-ciq%d",
  663. adap->port[0]->name, i);
  664. }
  665. static int request_msix_queue_irqs(struct adapter *adap)
  666. {
  667. struct sge *s = &adap->sge;
  668. int err, ethqidx, ofldqidx = 0, rdmaqidx = 0, rdmaciqqidx = 0;
  669. int msi_index = 2;
  670. err = request_irq(adap->msix_info[1].vec, t4_sge_intr_msix, 0,
  671. adap->msix_info[1].desc, &s->fw_evtq);
  672. if (err)
  673. return err;
  674. for_each_ethrxq(s, ethqidx) {
  675. err = request_irq(adap->msix_info[msi_index].vec,
  676. t4_sge_intr_msix, 0,
  677. adap->msix_info[msi_index].desc,
  678. &s->ethrxq[ethqidx].rspq);
  679. if (err)
  680. goto unwind;
  681. msi_index++;
  682. }
  683. for_each_ofldrxq(s, ofldqidx) {
  684. err = request_irq(adap->msix_info[msi_index].vec,
  685. t4_sge_intr_msix, 0,
  686. adap->msix_info[msi_index].desc,
  687. &s->ofldrxq[ofldqidx].rspq);
  688. if (err)
  689. goto unwind;
  690. msi_index++;
  691. }
  692. for_each_rdmarxq(s, rdmaqidx) {
  693. err = request_irq(adap->msix_info[msi_index].vec,
  694. t4_sge_intr_msix, 0,
  695. adap->msix_info[msi_index].desc,
  696. &s->rdmarxq[rdmaqidx].rspq);
  697. if (err)
  698. goto unwind;
  699. msi_index++;
  700. }
  701. for_each_rdmaciq(s, rdmaciqqidx) {
  702. err = request_irq(adap->msix_info[msi_index].vec,
  703. t4_sge_intr_msix, 0,
  704. adap->msix_info[msi_index].desc,
  705. &s->rdmaciq[rdmaciqqidx].rspq);
  706. if (err)
  707. goto unwind;
  708. msi_index++;
  709. }
  710. return 0;
  711. unwind:
  712. while (--rdmaciqqidx >= 0)
  713. free_irq(adap->msix_info[--msi_index].vec,
  714. &s->rdmaciq[rdmaciqqidx].rspq);
  715. while (--rdmaqidx >= 0)
  716. free_irq(adap->msix_info[--msi_index].vec,
  717. &s->rdmarxq[rdmaqidx].rspq);
  718. while (--ofldqidx >= 0)
  719. free_irq(adap->msix_info[--msi_index].vec,
  720. &s->ofldrxq[ofldqidx].rspq);
  721. while (--ethqidx >= 0)
  722. free_irq(adap->msix_info[--msi_index].vec,
  723. &s->ethrxq[ethqidx].rspq);
  724. free_irq(adap->msix_info[1].vec, &s->fw_evtq);
  725. return err;
  726. }
  727. static void free_msix_queue_irqs(struct adapter *adap)
  728. {
  729. int i, msi_index = 2;
  730. struct sge *s = &adap->sge;
  731. free_irq(adap->msix_info[1].vec, &s->fw_evtq);
  732. for_each_ethrxq(s, i)
  733. free_irq(adap->msix_info[msi_index++].vec, &s->ethrxq[i].rspq);
  734. for_each_ofldrxq(s, i)
  735. free_irq(adap->msix_info[msi_index++].vec, &s->ofldrxq[i].rspq);
  736. for_each_rdmarxq(s, i)
  737. free_irq(adap->msix_info[msi_index++].vec, &s->rdmarxq[i].rspq);
  738. for_each_rdmaciq(s, i)
  739. free_irq(adap->msix_info[msi_index++].vec, &s->rdmaciq[i].rspq);
  740. }
  741. /**
  742. * cxgb4_write_rss - write the RSS table for a given port
  743. * @pi: the port
  744. * @queues: array of queue indices for RSS
  745. *
  746. * Sets up the portion of the HW RSS table for the port's VI to distribute
  747. * packets to the Rx queues in @queues.
  748. */
  749. int cxgb4_write_rss(const struct port_info *pi, const u16 *queues)
  750. {
  751. u16 *rss;
  752. int i, err;
  753. const struct sge_eth_rxq *q = &pi->adapter->sge.ethrxq[pi->first_qset];
  754. rss = kmalloc(pi->rss_size * sizeof(u16), GFP_KERNEL);
  755. if (!rss)
  756. return -ENOMEM;
  757. /* map the queue indices to queue ids */
  758. for (i = 0; i < pi->rss_size; i++, queues++)
  759. rss[i] = q[*queues].rspq.abs_id;
  760. err = t4_config_rss_range(pi->adapter, pi->adapter->fn, pi->viid, 0,
  761. pi->rss_size, rss, pi->rss_size);
  762. kfree(rss);
  763. return err;
  764. }
  765. /**
  766. * setup_rss - configure RSS
  767. * @adap: the adapter
  768. *
  769. * Sets up RSS for each port.
  770. */
  771. static int setup_rss(struct adapter *adap)
  772. {
  773. int i, err;
  774. for_each_port(adap, i) {
  775. const struct port_info *pi = adap2pinfo(adap, i);
  776. err = cxgb4_write_rss(pi, pi->rss);
  777. if (err)
  778. return err;
  779. }
  780. return 0;
  781. }
  782. /*
  783. * Return the channel of the ingress queue with the given qid.
  784. */
  785. static unsigned int rxq_to_chan(const struct sge *p, unsigned int qid)
  786. {
  787. qid -= p->ingr_start;
  788. return netdev2pinfo(p->ingr_map[qid]->netdev)->tx_chan;
  789. }
  790. /*
  791. * Wait until all NAPI handlers are descheduled.
  792. */
  793. static void quiesce_rx(struct adapter *adap)
  794. {
  795. int i;
  796. for (i = 0; i < adap->sge.ingr_sz; i++) {
  797. struct sge_rspq *q = adap->sge.ingr_map[i];
  798. if (q && q->handler) {
  799. napi_disable(&q->napi);
  800. local_bh_disable();
  801. while (!cxgb_poll_lock_napi(q))
  802. mdelay(1);
  803. local_bh_enable();
  804. }
  805. }
  806. }
  807. /* Disable interrupt and napi handler */
  808. static void disable_interrupts(struct adapter *adap)
  809. {
  810. if (adap->flags & FULL_INIT_DONE) {
  811. t4_intr_disable(adap);
  812. if (adap->flags & USING_MSIX) {
  813. free_msix_queue_irqs(adap);
  814. free_irq(adap->msix_info[0].vec, adap);
  815. } else {
  816. free_irq(adap->pdev->irq, adap);
  817. }
  818. quiesce_rx(adap);
  819. }
  820. }
  821. /*
  822. * Enable NAPI scheduling and interrupt generation for all Rx queues.
  823. */
  824. static void enable_rx(struct adapter *adap)
  825. {
  826. int i;
  827. for (i = 0; i < adap->sge.ingr_sz; i++) {
  828. struct sge_rspq *q = adap->sge.ingr_map[i];
  829. if (!q)
  830. continue;
  831. if (q->handler) {
  832. cxgb_busy_poll_init_lock(q);
  833. napi_enable(&q->napi);
  834. }
  835. /* 0-increment GTS to start the timer and enable interrupts */
  836. t4_write_reg(adap, MYPF_REG(SGE_PF_GTS_A),
  837. SEINTARM_V(q->intr_params) |
  838. INGRESSQID_V(q->cntxt_id));
  839. }
  840. }
  841. static int alloc_ofld_rxqs(struct adapter *adap, struct sge_ofld_rxq *q,
  842. unsigned int nq, unsigned int per_chan, int msi_idx,
  843. u16 *ids)
  844. {
  845. int i, err;
  846. for (i = 0; i < nq; i++, q++) {
  847. if (msi_idx > 0)
  848. msi_idx++;
  849. err = t4_sge_alloc_rxq(adap, &q->rspq, false,
  850. adap->port[i / per_chan],
  851. msi_idx, q->fl.size ? &q->fl : NULL,
  852. uldrx_handler);
  853. if (err)
  854. return err;
  855. memset(&q->stats, 0, sizeof(q->stats));
  856. if (ids)
  857. ids[i] = q->rspq.abs_id;
  858. }
  859. return 0;
  860. }
  861. /**
  862. * setup_sge_queues - configure SGE Tx/Rx/response queues
  863. * @adap: the adapter
  864. *
  865. * Determines how many sets of SGE queues to use and initializes them.
  866. * We support multiple queue sets per port if we have MSI-X, otherwise
  867. * just one queue set per port.
  868. */
  869. static int setup_sge_queues(struct adapter *adap)
  870. {
  871. int err, msi_idx, i, j;
  872. struct sge *s = &adap->sge;
  873. bitmap_zero(s->starving_fl, s->egr_sz);
  874. bitmap_zero(s->txq_maperr, s->egr_sz);
  875. if (adap->flags & USING_MSIX)
  876. msi_idx = 1; /* vector 0 is for non-queue interrupts */
  877. else {
  878. err = t4_sge_alloc_rxq(adap, &s->intrq, false, adap->port[0], 0,
  879. NULL, NULL);
  880. if (err)
  881. return err;
  882. msi_idx = -((int)s->intrq.abs_id + 1);
  883. }
  884. /* NOTE: If you add/delete any Ingress/Egress Queue allocations in here,
  885. * don't forget to update the following which need to be
  886. * synchronized to and changes here.
  887. *
  888. * 1. The calculations of MAX_INGQ in cxgb4.h.
  889. *
  890. * 2. Update enable_msix/name_msix_vecs/request_msix_queue_irqs
  891. * to accommodate any new/deleted Ingress Queues
  892. * which need MSI-X Vectors.
  893. *
  894. * 3. Update sge_qinfo_show() to include information on the
  895. * new/deleted queues.
  896. */
  897. err = t4_sge_alloc_rxq(adap, &s->fw_evtq, true, adap->port[0],
  898. msi_idx, NULL, fwevtq_handler);
  899. if (err) {
  900. freeout: t4_free_sge_resources(adap);
  901. return err;
  902. }
  903. for_each_port(adap, i) {
  904. struct net_device *dev = adap->port[i];
  905. struct port_info *pi = netdev_priv(dev);
  906. struct sge_eth_rxq *q = &s->ethrxq[pi->first_qset];
  907. struct sge_eth_txq *t = &s->ethtxq[pi->first_qset];
  908. for (j = 0; j < pi->nqsets; j++, q++) {
  909. if (msi_idx > 0)
  910. msi_idx++;
  911. err = t4_sge_alloc_rxq(adap, &q->rspq, false, dev,
  912. msi_idx, &q->fl,
  913. t4_ethrx_handler);
  914. if (err)
  915. goto freeout;
  916. q->rspq.idx = j;
  917. memset(&q->stats, 0, sizeof(q->stats));
  918. }
  919. for (j = 0; j < pi->nqsets; j++, t++) {
  920. err = t4_sge_alloc_eth_txq(adap, t, dev,
  921. netdev_get_tx_queue(dev, j),
  922. s->fw_evtq.cntxt_id);
  923. if (err)
  924. goto freeout;
  925. }
  926. }
  927. j = s->ofldqsets / adap->params.nports; /* ofld queues per channel */
  928. for_each_ofldrxq(s, i) {
  929. err = t4_sge_alloc_ofld_txq(adap, &s->ofldtxq[i],
  930. adap->port[i / j],
  931. s->fw_evtq.cntxt_id);
  932. if (err)
  933. goto freeout;
  934. }
  935. #define ALLOC_OFLD_RXQS(firstq, nq, per_chan, ids) do { \
  936. err = alloc_ofld_rxqs(adap, firstq, nq, per_chan, msi_idx, ids); \
  937. if (err) \
  938. goto freeout; \
  939. if (msi_idx > 0) \
  940. msi_idx += nq; \
  941. } while (0)
  942. ALLOC_OFLD_RXQS(s->ofldrxq, s->ofldqsets, j, s->ofld_rxq);
  943. ALLOC_OFLD_RXQS(s->rdmarxq, s->rdmaqs, 1, s->rdma_rxq);
  944. j = s->rdmaciqs / adap->params.nports; /* rdmaq queues per channel */
  945. ALLOC_OFLD_RXQS(s->rdmaciq, s->rdmaciqs, j, s->rdma_ciq);
  946. #undef ALLOC_OFLD_RXQS
  947. for_each_port(adap, i) {
  948. /*
  949. * Note that ->rdmarxq[i].rspq.cntxt_id below is 0 if we don't
  950. * have RDMA queues, and that's the right value.
  951. */
  952. err = t4_sge_alloc_ctrl_txq(adap, &s->ctrlq[i], adap->port[i],
  953. s->fw_evtq.cntxt_id,
  954. s->rdmarxq[i].rspq.cntxt_id);
  955. if (err)
  956. goto freeout;
  957. }
  958. t4_write_reg(adap, is_t4(adap->params.chip) ?
  959. MPS_TRC_RSS_CONTROL_A :
  960. MPS_T5_TRC_RSS_CONTROL_A,
  961. RSSCONTROL_V(netdev2pinfo(adap->port[0])->tx_chan) |
  962. QUEUENUMBER_V(s->ethrxq[0].rspq.abs_id));
  963. return 0;
  964. }
  965. /*
  966. * Allocate a chunk of memory using kmalloc or, if that fails, vmalloc.
  967. * The allocated memory is cleared.
  968. */
  969. void *t4_alloc_mem(size_t size)
  970. {
  971. void *p = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
  972. if (!p)
  973. p = vzalloc(size);
  974. return p;
  975. }
  976. /*
  977. * Free memory allocated through alloc_mem().
  978. */
  979. void t4_free_mem(void *addr)
  980. {
  981. if (is_vmalloc_addr(addr))
  982. vfree(addr);
  983. else
  984. kfree(addr);
  985. }
  986. /* Send a Work Request to write the filter at a specified index. We construct
  987. * a Firmware Filter Work Request to have the work done and put the indicated
  988. * filter into "pending" mode which will prevent any further actions against
  989. * it till we get a reply from the firmware on the completion status of the
  990. * request.
  991. */
  992. static int set_filter_wr(struct adapter *adapter, int fidx)
  993. {
  994. struct filter_entry *f = &adapter->tids.ftid_tab[fidx];
  995. struct sk_buff *skb;
  996. struct fw_filter_wr *fwr;
  997. unsigned int ftid;
  998. skb = alloc_skb(sizeof(*fwr), GFP_KERNEL);
  999. if (!skb)
  1000. return -ENOMEM;
  1001. /* If the new filter requires loopback Destination MAC and/or VLAN
  1002. * rewriting then we need to allocate a Layer 2 Table (L2T) entry for
  1003. * the filter.
  1004. */
  1005. if (f->fs.newdmac || f->fs.newvlan) {
  1006. /* allocate L2T entry for new filter */
  1007. f->l2t = t4_l2t_alloc_switching(adapter->l2t);
  1008. if (f->l2t == NULL) {
  1009. kfree_skb(skb);
  1010. return -EAGAIN;
  1011. }
  1012. if (t4_l2t_set_switching(adapter, f->l2t, f->fs.vlan,
  1013. f->fs.eport, f->fs.dmac)) {
  1014. cxgb4_l2t_release(f->l2t);
  1015. f->l2t = NULL;
  1016. kfree_skb(skb);
  1017. return -ENOMEM;
  1018. }
  1019. }
  1020. ftid = adapter->tids.ftid_base + fidx;
  1021. fwr = (struct fw_filter_wr *)__skb_put(skb, sizeof(*fwr));
  1022. memset(fwr, 0, sizeof(*fwr));
  1023. /* It would be nice to put most of the following in t4_hw.c but most
  1024. * of the work is translating the cxgbtool ch_filter_specification
  1025. * into the Work Request and the definition of that structure is
  1026. * currently in cxgbtool.h which isn't appropriate to pull into the
  1027. * common code. We may eventually try to come up with a more neutral
  1028. * filter specification structure but for now it's easiest to simply
  1029. * put this fairly direct code in line ...
  1030. */
  1031. fwr->op_pkd = htonl(FW_WR_OP_V(FW_FILTER_WR));
  1032. fwr->len16_pkd = htonl(FW_WR_LEN16_V(sizeof(*fwr)/16));
  1033. fwr->tid_to_iq =
  1034. htonl(FW_FILTER_WR_TID_V(ftid) |
  1035. FW_FILTER_WR_RQTYPE_V(f->fs.type) |
  1036. FW_FILTER_WR_NOREPLY_V(0) |
  1037. FW_FILTER_WR_IQ_V(f->fs.iq));
  1038. fwr->del_filter_to_l2tix =
  1039. htonl(FW_FILTER_WR_RPTTID_V(f->fs.rpttid) |
  1040. FW_FILTER_WR_DROP_V(f->fs.action == FILTER_DROP) |
  1041. FW_FILTER_WR_DIRSTEER_V(f->fs.dirsteer) |
  1042. FW_FILTER_WR_MASKHASH_V(f->fs.maskhash) |
  1043. FW_FILTER_WR_DIRSTEERHASH_V(f->fs.dirsteerhash) |
  1044. FW_FILTER_WR_LPBK_V(f->fs.action == FILTER_SWITCH) |
  1045. FW_FILTER_WR_DMAC_V(f->fs.newdmac) |
  1046. FW_FILTER_WR_SMAC_V(f->fs.newsmac) |
  1047. FW_FILTER_WR_INSVLAN_V(f->fs.newvlan == VLAN_INSERT ||
  1048. f->fs.newvlan == VLAN_REWRITE) |
  1049. FW_FILTER_WR_RMVLAN_V(f->fs.newvlan == VLAN_REMOVE ||
  1050. f->fs.newvlan == VLAN_REWRITE) |
  1051. FW_FILTER_WR_HITCNTS_V(f->fs.hitcnts) |
  1052. FW_FILTER_WR_TXCHAN_V(f->fs.eport) |
  1053. FW_FILTER_WR_PRIO_V(f->fs.prio) |
  1054. FW_FILTER_WR_L2TIX_V(f->l2t ? f->l2t->idx : 0));
  1055. fwr->ethtype = htons(f->fs.val.ethtype);
  1056. fwr->ethtypem = htons(f->fs.mask.ethtype);
  1057. fwr->frag_to_ovlan_vldm =
  1058. (FW_FILTER_WR_FRAG_V(f->fs.val.frag) |
  1059. FW_FILTER_WR_FRAGM_V(f->fs.mask.frag) |
  1060. FW_FILTER_WR_IVLAN_VLD_V(f->fs.val.ivlan_vld) |
  1061. FW_FILTER_WR_OVLAN_VLD_V(f->fs.val.ovlan_vld) |
  1062. FW_FILTER_WR_IVLAN_VLDM_V(f->fs.mask.ivlan_vld) |
  1063. FW_FILTER_WR_OVLAN_VLDM_V(f->fs.mask.ovlan_vld));
  1064. fwr->smac_sel = 0;
  1065. fwr->rx_chan_rx_rpl_iq =
  1066. htons(FW_FILTER_WR_RX_CHAN_V(0) |
  1067. FW_FILTER_WR_RX_RPL_IQ_V(adapter->sge.fw_evtq.abs_id));
  1068. fwr->maci_to_matchtypem =
  1069. htonl(FW_FILTER_WR_MACI_V(f->fs.val.macidx) |
  1070. FW_FILTER_WR_MACIM_V(f->fs.mask.macidx) |
  1071. FW_FILTER_WR_FCOE_V(f->fs.val.fcoe) |
  1072. FW_FILTER_WR_FCOEM_V(f->fs.mask.fcoe) |
  1073. FW_FILTER_WR_PORT_V(f->fs.val.iport) |
  1074. FW_FILTER_WR_PORTM_V(f->fs.mask.iport) |
  1075. FW_FILTER_WR_MATCHTYPE_V(f->fs.val.matchtype) |
  1076. FW_FILTER_WR_MATCHTYPEM_V(f->fs.mask.matchtype));
  1077. fwr->ptcl = f->fs.val.proto;
  1078. fwr->ptclm = f->fs.mask.proto;
  1079. fwr->ttyp = f->fs.val.tos;
  1080. fwr->ttypm = f->fs.mask.tos;
  1081. fwr->ivlan = htons(f->fs.val.ivlan);
  1082. fwr->ivlanm = htons(f->fs.mask.ivlan);
  1083. fwr->ovlan = htons(f->fs.val.ovlan);
  1084. fwr->ovlanm = htons(f->fs.mask.ovlan);
  1085. memcpy(fwr->lip, f->fs.val.lip, sizeof(fwr->lip));
  1086. memcpy(fwr->lipm, f->fs.mask.lip, sizeof(fwr->lipm));
  1087. memcpy(fwr->fip, f->fs.val.fip, sizeof(fwr->fip));
  1088. memcpy(fwr->fipm, f->fs.mask.fip, sizeof(fwr->fipm));
  1089. fwr->lp = htons(f->fs.val.lport);
  1090. fwr->lpm = htons(f->fs.mask.lport);
  1091. fwr->fp = htons(f->fs.val.fport);
  1092. fwr->fpm = htons(f->fs.mask.fport);
  1093. if (f->fs.newsmac)
  1094. memcpy(fwr->sma, f->fs.smac, sizeof(fwr->sma));
  1095. /* Mark the filter as "pending" and ship off the Filter Work Request.
  1096. * When we get the Work Request Reply we'll clear the pending status.
  1097. */
  1098. f->pending = 1;
  1099. set_wr_txq(skb, CPL_PRIORITY_CONTROL, f->fs.val.iport & 0x3);
  1100. t4_ofld_send(adapter, skb);
  1101. return 0;
  1102. }
  1103. /* Delete the filter at a specified index.
  1104. */
  1105. static int del_filter_wr(struct adapter *adapter, int fidx)
  1106. {
  1107. struct filter_entry *f = &adapter->tids.ftid_tab[fidx];
  1108. struct sk_buff *skb;
  1109. struct fw_filter_wr *fwr;
  1110. unsigned int len, ftid;
  1111. len = sizeof(*fwr);
  1112. ftid = adapter->tids.ftid_base + fidx;
  1113. skb = alloc_skb(len, GFP_KERNEL);
  1114. if (!skb)
  1115. return -ENOMEM;
  1116. fwr = (struct fw_filter_wr *)__skb_put(skb, len);
  1117. t4_mk_filtdelwr(ftid, fwr, adapter->sge.fw_evtq.abs_id);
  1118. /* Mark the filter as "pending" and ship off the Filter Work Request.
  1119. * When we get the Work Request Reply we'll clear the pending status.
  1120. */
  1121. f->pending = 1;
  1122. t4_mgmt_tx(adapter, skb);
  1123. return 0;
  1124. }
  1125. static u16 cxgb_select_queue(struct net_device *dev, struct sk_buff *skb,
  1126. void *accel_priv, select_queue_fallback_t fallback)
  1127. {
  1128. int txq;
  1129. #ifdef CONFIG_CHELSIO_T4_DCB
  1130. /* If a Data Center Bridging has been successfully negotiated on this
  1131. * link then we'll use the skb's priority to map it to a TX Queue.
  1132. * The skb's priority is determined via the VLAN Tag Priority Code
  1133. * Point field.
  1134. */
  1135. if (cxgb4_dcb_enabled(dev)) {
  1136. u16 vlan_tci;
  1137. int err;
  1138. err = vlan_get_tag(skb, &vlan_tci);
  1139. if (unlikely(err)) {
  1140. if (net_ratelimit())
  1141. netdev_warn(dev,
  1142. "TX Packet without VLAN Tag on DCB Link\n");
  1143. txq = 0;
  1144. } else {
  1145. txq = (vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
  1146. #ifdef CONFIG_CHELSIO_T4_FCOE
  1147. if (skb->protocol == htons(ETH_P_FCOE))
  1148. txq = skb->priority & 0x7;
  1149. #endif /* CONFIG_CHELSIO_T4_FCOE */
  1150. }
  1151. return txq;
  1152. }
  1153. #endif /* CONFIG_CHELSIO_T4_DCB */
  1154. if (select_queue) {
  1155. txq = (skb_rx_queue_recorded(skb)
  1156. ? skb_get_rx_queue(skb)
  1157. : smp_processor_id());
  1158. while (unlikely(txq >= dev->real_num_tx_queues))
  1159. txq -= dev->real_num_tx_queues;
  1160. return txq;
  1161. }
  1162. return fallback(dev, skb) % dev->real_num_tx_queues;
  1163. }
  1164. static inline int is_offload(const struct adapter *adap)
  1165. {
  1166. return adap->params.offload;
  1167. }
  1168. static int closest_timer(const struct sge *s, int time)
  1169. {
  1170. int i, delta, match = 0, min_delta = INT_MAX;
  1171. for (i = 0; i < ARRAY_SIZE(s->timer_val); i++) {
  1172. delta = time - s->timer_val[i];
  1173. if (delta < 0)
  1174. delta = -delta;
  1175. if (delta < min_delta) {
  1176. min_delta = delta;
  1177. match = i;
  1178. }
  1179. }
  1180. return match;
  1181. }
  1182. static int closest_thres(const struct sge *s, int thres)
  1183. {
  1184. int i, delta, match = 0, min_delta = INT_MAX;
  1185. for (i = 0; i < ARRAY_SIZE(s->counter_val); i++) {
  1186. delta = thres - s->counter_val[i];
  1187. if (delta < 0)
  1188. delta = -delta;
  1189. if (delta < min_delta) {
  1190. min_delta = delta;
  1191. match = i;
  1192. }
  1193. }
  1194. return match;
  1195. }
  1196. /**
  1197. * cxgb4_set_rspq_intr_params - set a queue's interrupt holdoff parameters
  1198. * @q: the Rx queue
  1199. * @us: the hold-off time in us, or 0 to disable timer
  1200. * @cnt: the hold-off packet count, or 0 to disable counter
  1201. *
  1202. * Sets an Rx queue's interrupt hold-off time and packet count. At least
  1203. * one of the two needs to be enabled for the queue to generate interrupts.
  1204. */
  1205. int cxgb4_set_rspq_intr_params(struct sge_rspq *q,
  1206. unsigned int us, unsigned int cnt)
  1207. {
  1208. struct adapter *adap = q->adap;
  1209. if ((us | cnt) == 0)
  1210. cnt = 1;
  1211. if (cnt) {
  1212. int err;
  1213. u32 v, new_idx;
  1214. new_idx = closest_thres(&adap->sge, cnt);
  1215. if (q->desc && q->pktcnt_idx != new_idx) {
  1216. /* the queue has already been created, update it */
  1217. v = FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
  1218. FW_PARAMS_PARAM_X_V(
  1219. FW_PARAMS_PARAM_DMAQ_IQ_INTCNTTHRESH) |
  1220. FW_PARAMS_PARAM_YZ_V(q->cntxt_id);
  1221. err = t4_set_params(adap, adap->fn, adap->fn, 0, 1, &v,
  1222. &new_idx);
  1223. if (err)
  1224. return err;
  1225. }
  1226. q->pktcnt_idx = new_idx;
  1227. }
  1228. us = us == 0 ? 6 : closest_timer(&adap->sge, us);
  1229. q->intr_params = QINTR_TIMER_IDX(us) | (cnt > 0 ? QINTR_CNT_EN : 0);
  1230. return 0;
  1231. }
  1232. static int cxgb_set_features(struct net_device *dev, netdev_features_t features)
  1233. {
  1234. const struct port_info *pi = netdev_priv(dev);
  1235. netdev_features_t changed = dev->features ^ features;
  1236. int err;
  1237. if (!(changed & NETIF_F_HW_VLAN_CTAG_RX))
  1238. return 0;
  1239. err = t4_set_rxmode(pi->adapter, pi->adapter->fn, pi->viid, -1,
  1240. -1, -1, -1,
  1241. !!(features & NETIF_F_HW_VLAN_CTAG_RX), true);
  1242. if (unlikely(err))
  1243. dev->features = features ^ NETIF_F_HW_VLAN_CTAG_RX;
  1244. return err;
  1245. }
  1246. static int setup_debugfs(struct adapter *adap)
  1247. {
  1248. if (IS_ERR_OR_NULL(adap->debugfs_root))
  1249. return -1;
  1250. #ifdef CONFIG_DEBUG_FS
  1251. t4_setup_debugfs(adap);
  1252. #endif
  1253. return 0;
  1254. }
  1255. /*
  1256. * upper-layer driver support
  1257. */
  1258. /*
  1259. * Allocate an active-open TID and set it to the supplied value.
  1260. */
  1261. int cxgb4_alloc_atid(struct tid_info *t, void *data)
  1262. {
  1263. int atid = -1;
  1264. spin_lock_bh(&t->atid_lock);
  1265. if (t->afree) {
  1266. union aopen_entry *p = t->afree;
  1267. atid = (p - t->atid_tab) + t->atid_base;
  1268. t->afree = p->next;
  1269. p->data = data;
  1270. t->atids_in_use++;
  1271. }
  1272. spin_unlock_bh(&t->atid_lock);
  1273. return atid;
  1274. }
  1275. EXPORT_SYMBOL(cxgb4_alloc_atid);
  1276. /*
  1277. * Release an active-open TID.
  1278. */
  1279. void cxgb4_free_atid(struct tid_info *t, unsigned int atid)
  1280. {
  1281. union aopen_entry *p = &t->atid_tab[atid - t->atid_base];
  1282. spin_lock_bh(&t->atid_lock);
  1283. p->next = t->afree;
  1284. t->afree = p;
  1285. t->atids_in_use--;
  1286. spin_unlock_bh(&t->atid_lock);
  1287. }
  1288. EXPORT_SYMBOL(cxgb4_free_atid);
  1289. /*
  1290. * Allocate a server TID and set it to the supplied value.
  1291. */
  1292. int cxgb4_alloc_stid(struct tid_info *t, int family, void *data)
  1293. {
  1294. int stid;
  1295. spin_lock_bh(&t->stid_lock);
  1296. if (family == PF_INET) {
  1297. stid = find_first_zero_bit(t->stid_bmap, t->nstids);
  1298. if (stid < t->nstids)
  1299. __set_bit(stid, t->stid_bmap);
  1300. else
  1301. stid = -1;
  1302. } else {
  1303. stid = bitmap_find_free_region(t->stid_bmap, t->nstids, 2);
  1304. if (stid < 0)
  1305. stid = -1;
  1306. }
  1307. if (stid >= 0) {
  1308. t->stid_tab[stid].data = data;
  1309. stid += t->stid_base;
  1310. /* IPv6 requires max of 520 bits or 16 cells in TCAM
  1311. * This is equivalent to 4 TIDs. With CLIP enabled it
  1312. * needs 2 TIDs.
  1313. */
  1314. if (family == PF_INET)
  1315. t->stids_in_use++;
  1316. else
  1317. t->stids_in_use += 4;
  1318. }
  1319. spin_unlock_bh(&t->stid_lock);
  1320. return stid;
  1321. }
  1322. EXPORT_SYMBOL(cxgb4_alloc_stid);
  1323. /* Allocate a server filter TID and set it to the supplied value.
  1324. */
  1325. int cxgb4_alloc_sftid(struct tid_info *t, int family, void *data)
  1326. {
  1327. int stid;
  1328. spin_lock_bh(&t->stid_lock);
  1329. if (family == PF_INET) {
  1330. stid = find_next_zero_bit(t->stid_bmap,
  1331. t->nstids + t->nsftids, t->nstids);
  1332. if (stid < (t->nstids + t->nsftids))
  1333. __set_bit(stid, t->stid_bmap);
  1334. else
  1335. stid = -1;
  1336. } else {
  1337. stid = -1;
  1338. }
  1339. if (stid >= 0) {
  1340. t->stid_tab[stid].data = data;
  1341. stid -= t->nstids;
  1342. stid += t->sftid_base;
  1343. t->stids_in_use++;
  1344. }
  1345. spin_unlock_bh(&t->stid_lock);
  1346. return stid;
  1347. }
  1348. EXPORT_SYMBOL(cxgb4_alloc_sftid);
  1349. /* Release a server TID.
  1350. */
  1351. void cxgb4_free_stid(struct tid_info *t, unsigned int stid, int family)
  1352. {
  1353. /* Is it a server filter TID? */
  1354. if (t->nsftids && (stid >= t->sftid_base)) {
  1355. stid -= t->sftid_base;
  1356. stid += t->nstids;
  1357. } else {
  1358. stid -= t->stid_base;
  1359. }
  1360. spin_lock_bh(&t->stid_lock);
  1361. if (family == PF_INET)
  1362. __clear_bit(stid, t->stid_bmap);
  1363. else
  1364. bitmap_release_region(t->stid_bmap, stid, 2);
  1365. t->stid_tab[stid].data = NULL;
  1366. if (family == PF_INET)
  1367. t->stids_in_use--;
  1368. else
  1369. t->stids_in_use -= 4;
  1370. spin_unlock_bh(&t->stid_lock);
  1371. }
  1372. EXPORT_SYMBOL(cxgb4_free_stid);
  1373. /*
  1374. * Populate a TID_RELEASE WR. Caller must properly size the skb.
  1375. */
  1376. static void mk_tid_release(struct sk_buff *skb, unsigned int chan,
  1377. unsigned int tid)
  1378. {
  1379. struct cpl_tid_release *req;
  1380. set_wr_txq(skb, CPL_PRIORITY_SETUP, chan);
  1381. req = (struct cpl_tid_release *)__skb_put(skb, sizeof(*req));
  1382. INIT_TP_WR(req, tid);
  1383. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, tid));
  1384. }
  1385. /*
  1386. * Queue a TID release request and if necessary schedule a work queue to
  1387. * process it.
  1388. */
  1389. static void cxgb4_queue_tid_release(struct tid_info *t, unsigned int chan,
  1390. unsigned int tid)
  1391. {
  1392. void **p = &t->tid_tab[tid];
  1393. struct adapter *adap = container_of(t, struct adapter, tids);
  1394. spin_lock_bh(&adap->tid_release_lock);
  1395. *p = adap->tid_release_head;
  1396. /* Low 2 bits encode the Tx channel number */
  1397. adap->tid_release_head = (void **)((uintptr_t)p | chan);
  1398. if (!adap->tid_release_task_busy) {
  1399. adap->tid_release_task_busy = true;
  1400. queue_work(adap->workq, &adap->tid_release_task);
  1401. }
  1402. spin_unlock_bh(&adap->tid_release_lock);
  1403. }
  1404. /*
  1405. * Process the list of pending TID release requests.
  1406. */
  1407. static void process_tid_release_list(struct work_struct *work)
  1408. {
  1409. struct sk_buff *skb;
  1410. struct adapter *adap;
  1411. adap = container_of(work, struct adapter, tid_release_task);
  1412. spin_lock_bh(&adap->tid_release_lock);
  1413. while (adap->tid_release_head) {
  1414. void **p = adap->tid_release_head;
  1415. unsigned int chan = (uintptr_t)p & 3;
  1416. p = (void *)p - chan;
  1417. adap->tid_release_head = *p;
  1418. *p = NULL;
  1419. spin_unlock_bh(&adap->tid_release_lock);
  1420. while (!(skb = alloc_skb(sizeof(struct cpl_tid_release),
  1421. GFP_KERNEL)))
  1422. schedule_timeout_uninterruptible(1);
  1423. mk_tid_release(skb, chan, p - adap->tids.tid_tab);
  1424. t4_ofld_send(adap, skb);
  1425. spin_lock_bh(&adap->tid_release_lock);
  1426. }
  1427. adap->tid_release_task_busy = false;
  1428. spin_unlock_bh(&adap->tid_release_lock);
  1429. }
  1430. /*
  1431. * Release a TID and inform HW. If we are unable to allocate the release
  1432. * message we defer to a work queue.
  1433. */
  1434. void cxgb4_remove_tid(struct tid_info *t, unsigned int chan, unsigned int tid)
  1435. {
  1436. void *old;
  1437. struct sk_buff *skb;
  1438. struct adapter *adap = container_of(t, struct adapter, tids);
  1439. old = t->tid_tab[tid];
  1440. skb = alloc_skb(sizeof(struct cpl_tid_release), GFP_ATOMIC);
  1441. if (likely(skb)) {
  1442. t->tid_tab[tid] = NULL;
  1443. mk_tid_release(skb, chan, tid);
  1444. t4_ofld_send(adap, skb);
  1445. } else
  1446. cxgb4_queue_tid_release(t, chan, tid);
  1447. if (old)
  1448. atomic_dec(&t->tids_in_use);
  1449. }
  1450. EXPORT_SYMBOL(cxgb4_remove_tid);
  1451. /*
  1452. * Allocate and initialize the TID tables. Returns 0 on success.
  1453. */
  1454. static int tid_init(struct tid_info *t)
  1455. {
  1456. size_t size;
  1457. unsigned int stid_bmap_size;
  1458. unsigned int natids = t->natids;
  1459. struct adapter *adap = container_of(t, struct adapter, tids);
  1460. stid_bmap_size = BITS_TO_LONGS(t->nstids + t->nsftids);
  1461. size = t->ntids * sizeof(*t->tid_tab) +
  1462. natids * sizeof(*t->atid_tab) +
  1463. t->nstids * sizeof(*t->stid_tab) +
  1464. t->nsftids * sizeof(*t->stid_tab) +
  1465. stid_bmap_size * sizeof(long) +
  1466. t->nftids * sizeof(*t->ftid_tab) +
  1467. t->nsftids * sizeof(*t->ftid_tab);
  1468. t->tid_tab = t4_alloc_mem(size);
  1469. if (!t->tid_tab)
  1470. return -ENOMEM;
  1471. t->atid_tab = (union aopen_entry *)&t->tid_tab[t->ntids];
  1472. t->stid_tab = (struct serv_entry *)&t->atid_tab[natids];
  1473. t->stid_bmap = (unsigned long *)&t->stid_tab[t->nstids + t->nsftids];
  1474. t->ftid_tab = (struct filter_entry *)&t->stid_bmap[stid_bmap_size];
  1475. spin_lock_init(&t->stid_lock);
  1476. spin_lock_init(&t->atid_lock);
  1477. t->stids_in_use = 0;
  1478. t->afree = NULL;
  1479. t->atids_in_use = 0;
  1480. atomic_set(&t->tids_in_use, 0);
  1481. /* Setup the free list for atid_tab and clear the stid bitmap. */
  1482. if (natids) {
  1483. while (--natids)
  1484. t->atid_tab[natids - 1].next = &t->atid_tab[natids];
  1485. t->afree = t->atid_tab;
  1486. }
  1487. bitmap_zero(t->stid_bmap, t->nstids + t->nsftids);
  1488. /* Reserve stid 0 for T4/T5 adapters */
  1489. if (!t->stid_base &&
  1490. (is_t4(adap->params.chip) || is_t5(adap->params.chip)))
  1491. __set_bit(0, t->stid_bmap);
  1492. return 0;
  1493. }
  1494. /**
  1495. * cxgb4_create_server - create an IP server
  1496. * @dev: the device
  1497. * @stid: the server TID
  1498. * @sip: local IP address to bind server to
  1499. * @sport: the server's TCP port
  1500. * @queue: queue to direct messages from this server to
  1501. *
  1502. * Create an IP server for the given port and address.
  1503. * Returns <0 on error and one of the %NET_XMIT_* values on success.
  1504. */
  1505. int cxgb4_create_server(const struct net_device *dev, unsigned int stid,
  1506. __be32 sip, __be16 sport, __be16 vlan,
  1507. unsigned int queue)
  1508. {
  1509. unsigned int chan;
  1510. struct sk_buff *skb;
  1511. struct adapter *adap;
  1512. struct cpl_pass_open_req *req;
  1513. int ret;
  1514. skb = alloc_skb(sizeof(*req), GFP_KERNEL);
  1515. if (!skb)
  1516. return -ENOMEM;
  1517. adap = netdev2adap(dev);
  1518. req = (struct cpl_pass_open_req *)__skb_put(skb, sizeof(*req));
  1519. INIT_TP_WR(req, 0);
  1520. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ, stid));
  1521. req->local_port = sport;
  1522. req->peer_port = htons(0);
  1523. req->local_ip = sip;
  1524. req->peer_ip = htonl(0);
  1525. chan = rxq_to_chan(&adap->sge, queue);
  1526. req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
  1527. req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
  1528. SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
  1529. ret = t4_mgmt_tx(adap, skb);
  1530. return net_xmit_eval(ret);
  1531. }
  1532. EXPORT_SYMBOL(cxgb4_create_server);
  1533. /* cxgb4_create_server6 - create an IPv6 server
  1534. * @dev: the device
  1535. * @stid: the server TID
  1536. * @sip: local IPv6 address to bind server to
  1537. * @sport: the server's TCP port
  1538. * @queue: queue to direct messages from this server to
  1539. *
  1540. * Create an IPv6 server for the given port and address.
  1541. * Returns <0 on error and one of the %NET_XMIT_* values on success.
  1542. */
  1543. int cxgb4_create_server6(const struct net_device *dev, unsigned int stid,
  1544. const struct in6_addr *sip, __be16 sport,
  1545. unsigned int queue)
  1546. {
  1547. unsigned int chan;
  1548. struct sk_buff *skb;
  1549. struct adapter *adap;
  1550. struct cpl_pass_open_req6 *req;
  1551. int ret;
  1552. skb = alloc_skb(sizeof(*req), GFP_KERNEL);
  1553. if (!skb)
  1554. return -ENOMEM;
  1555. adap = netdev2adap(dev);
  1556. req = (struct cpl_pass_open_req6 *)__skb_put(skb, sizeof(*req));
  1557. INIT_TP_WR(req, 0);
  1558. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ6, stid));
  1559. req->local_port = sport;
  1560. req->peer_port = htons(0);
  1561. req->local_ip_hi = *(__be64 *)(sip->s6_addr);
  1562. req->local_ip_lo = *(__be64 *)(sip->s6_addr + 8);
  1563. req->peer_ip_hi = cpu_to_be64(0);
  1564. req->peer_ip_lo = cpu_to_be64(0);
  1565. chan = rxq_to_chan(&adap->sge, queue);
  1566. req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
  1567. req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
  1568. SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
  1569. ret = t4_mgmt_tx(adap, skb);
  1570. return net_xmit_eval(ret);
  1571. }
  1572. EXPORT_SYMBOL(cxgb4_create_server6);
  1573. int cxgb4_remove_server(const struct net_device *dev, unsigned int stid,
  1574. unsigned int queue, bool ipv6)
  1575. {
  1576. struct sk_buff *skb;
  1577. struct adapter *adap;
  1578. struct cpl_close_listsvr_req *req;
  1579. int ret;
  1580. adap = netdev2adap(dev);
  1581. skb = alloc_skb(sizeof(*req), GFP_KERNEL);
  1582. if (!skb)
  1583. return -ENOMEM;
  1584. req = (struct cpl_close_listsvr_req *)__skb_put(skb, sizeof(*req));
  1585. INIT_TP_WR(req, 0);
  1586. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ, stid));
  1587. req->reply_ctrl = htons(NO_REPLY_V(0) | (ipv6 ? LISTSVR_IPV6_V(1) :
  1588. LISTSVR_IPV6_V(0)) | QUEUENO_V(queue));
  1589. ret = t4_mgmt_tx(adap, skb);
  1590. return net_xmit_eval(ret);
  1591. }
  1592. EXPORT_SYMBOL(cxgb4_remove_server);
  1593. /**
  1594. * cxgb4_best_mtu - find the entry in the MTU table closest to an MTU
  1595. * @mtus: the HW MTU table
  1596. * @mtu: the target MTU
  1597. * @idx: index of selected entry in the MTU table
  1598. *
  1599. * Returns the index and the value in the HW MTU table that is closest to
  1600. * but does not exceed @mtu, unless @mtu is smaller than any value in the
  1601. * table, in which case that smallest available value is selected.
  1602. */
  1603. unsigned int cxgb4_best_mtu(const unsigned short *mtus, unsigned short mtu,
  1604. unsigned int *idx)
  1605. {
  1606. unsigned int i = 0;
  1607. while (i < NMTUS - 1 && mtus[i + 1] <= mtu)
  1608. ++i;
  1609. if (idx)
  1610. *idx = i;
  1611. return mtus[i];
  1612. }
  1613. EXPORT_SYMBOL(cxgb4_best_mtu);
  1614. /**
  1615. * cxgb4_best_aligned_mtu - find best MTU, [hopefully] data size aligned
  1616. * @mtus: the HW MTU table
  1617. * @header_size: Header Size
  1618. * @data_size_max: maximum Data Segment Size
  1619. * @data_size_align: desired Data Segment Size Alignment (2^N)
  1620. * @mtu_idxp: HW MTU Table Index return value pointer (possibly NULL)
  1621. *
  1622. * Similar to cxgb4_best_mtu() but instead of searching the Hardware
  1623. * MTU Table based solely on a Maximum MTU parameter, we break that
  1624. * parameter up into a Header Size and Maximum Data Segment Size, and
  1625. * provide a desired Data Segment Size Alignment. If we find an MTU in
  1626. * the Hardware MTU Table which will result in a Data Segment Size with
  1627. * the requested alignment _and_ that MTU isn't "too far" from the
  1628. * closest MTU, then we'll return that rather than the closest MTU.
  1629. */
  1630. unsigned int cxgb4_best_aligned_mtu(const unsigned short *mtus,
  1631. unsigned short header_size,
  1632. unsigned short data_size_max,
  1633. unsigned short data_size_align,
  1634. unsigned int *mtu_idxp)
  1635. {
  1636. unsigned short max_mtu = header_size + data_size_max;
  1637. unsigned short data_size_align_mask = data_size_align - 1;
  1638. int mtu_idx, aligned_mtu_idx;
  1639. /* Scan the MTU Table till we find an MTU which is larger than our
  1640. * Maximum MTU or we reach the end of the table. Along the way,
  1641. * record the last MTU found, if any, which will result in a Data
  1642. * Segment Length matching the requested alignment.
  1643. */
  1644. for (mtu_idx = 0, aligned_mtu_idx = -1; mtu_idx < NMTUS; mtu_idx++) {
  1645. unsigned short data_size = mtus[mtu_idx] - header_size;
  1646. /* If this MTU minus the Header Size would result in a
  1647. * Data Segment Size of the desired alignment, remember it.
  1648. */
  1649. if ((data_size & data_size_align_mask) == 0)
  1650. aligned_mtu_idx = mtu_idx;
  1651. /* If we're not at the end of the Hardware MTU Table and the
  1652. * next element is larger than our Maximum MTU, drop out of
  1653. * the loop.
  1654. */
  1655. if (mtu_idx+1 < NMTUS && mtus[mtu_idx+1] > max_mtu)
  1656. break;
  1657. }
  1658. /* If we fell out of the loop because we ran to the end of the table,
  1659. * then we just have to use the last [largest] entry.
  1660. */
  1661. if (mtu_idx == NMTUS)
  1662. mtu_idx--;
  1663. /* If we found an MTU which resulted in the requested Data Segment
  1664. * Length alignment and that's "not far" from the largest MTU which is
  1665. * less than or equal to the maximum MTU, then use that.
  1666. */
  1667. if (aligned_mtu_idx >= 0 &&
  1668. mtu_idx - aligned_mtu_idx <= 1)
  1669. mtu_idx = aligned_mtu_idx;
  1670. /* If the caller has passed in an MTU Index pointer, pass the
  1671. * MTU Index back. Return the MTU value.
  1672. */
  1673. if (mtu_idxp)
  1674. *mtu_idxp = mtu_idx;
  1675. return mtus[mtu_idx];
  1676. }
  1677. EXPORT_SYMBOL(cxgb4_best_aligned_mtu);
  1678. /**
  1679. * cxgb4_port_chan - get the HW channel of a port
  1680. * @dev: the net device for the port
  1681. *
  1682. * Return the HW Tx channel of the given port.
  1683. */
  1684. unsigned int cxgb4_port_chan(const struct net_device *dev)
  1685. {
  1686. return netdev2pinfo(dev)->tx_chan;
  1687. }
  1688. EXPORT_SYMBOL(cxgb4_port_chan);
  1689. unsigned int cxgb4_dbfifo_count(const struct net_device *dev, int lpfifo)
  1690. {
  1691. struct adapter *adap = netdev2adap(dev);
  1692. u32 v1, v2, lp_count, hp_count;
  1693. v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
  1694. v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
  1695. if (is_t4(adap->params.chip)) {
  1696. lp_count = LP_COUNT_G(v1);
  1697. hp_count = HP_COUNT_G(v1);
  1698. } else {
  1699. lp_count = LP_COUNT_T5_G(v1);
  1700. hp_count = HP_COUNT_T5_G(v2);
  1701. }
  1702. return lpfifo ? lp_count : hp_count;
  1703. }
  1704. EXPORT_SYMBOL(cxgb4_dbfifo_count);
  1705. /**
  1706. * cxgb4_port_viid - get the VI id of a port
  1707. * @dev: the net device for the port
  1708. *
  1709. * Return the VI id of the given port.
  1710. */
  1711. unsigned int cxgb4_port_viid(const struct net_device *dev)
  1712. {
  1713. return netdev2pinfo(dev)->viid;
  1714. }
  1715. EXPORT_SYMBOL(cxgb4_port_viid);
  1716. /**
  1717. * cxgb4_port_idx - get the index of a port
  1718. * @dev: the net device for the port
  1719. *
  1720. * Return the index of the given port.
  1721. */
  1722. unsigned int cxgb4_port_idx(const struct net_device *dev)
  1723. {
  1724. return netdev2pinfo(dev)->port_id;
  1725. }
  1726. EXPORT_SYMBOL(cxgb4_port_idx);
  1727. void cxgb4_get_tcp_stats(struct pci_dev *pdev, struct tp_tcp_stats *v4,
  1728. struct tp_tcp_stats *v6)
  1729. {
  1730. struct adapter *adap = pci_get_drvdata(pdev);
  1731. spin_lock(&adap->stats_lock);
  1732. t4_tp_get_tcp_stats(adap, v4, v6);
  1733. spin_unlock(&adap->stats_lock);
  1734. }
  1735. EXPORT_SYMBOL(cxgb4_get_tcp_stats);
  1736. void cxgb4_iscsi_init(struct net_device *dev, unsigned int tag_mask,
  1737. const unsigned int *pgsz_order)
  1738. {
  1739. struct adapter *adap = netdev2adap(dev);
  1740. t4_write_reg(adap, ULP_RX_ISCSI_TAGMASK_A, tag_mask);
  1741. t4_write_reg(adap, ULP_RX_ISCSI_PSZ_A, HPZ0_V(pgsz_order[0]) |
  1742. HPZ1_V(pgsz_order[1]) | HPZ2_V(pgsz_order[2]) |
  1743. HPZ3_V(pgsz_order[3]));
  1744. }
  1745. EXPORT_SYMBOL(cxgb4_iscsi_init);
  1746. int cxgb4_flush_eq_cache(struct net_device *dev)
  1747. {
  1748. struct adapter *adap = netdev2adap(dev);
  1749. int ret;
  1750. ret = t4_fwaddrspace_write(adap, adap->mbox,
  1751. 0xe1000000 + SGE_CTXT_CMD_A, 0x20000000);
  1752. return ret;
  1753. }
  1754. EXPORT_SYMBOL(cxgb4_flush_eq_cache);
  1755. static int read_eq_indices(struct adapter *adap, u16 qid, u16 *pidx, u16 *cidx)
  1756. {
  1757. u32 addr = t4_read_reg(adap, SGE_DBQ_CTXT_BADDR_A) + 24 * qid + 8;
  1758. __be64 indices;
  1759. int ret;
  1760. spin_lock(&adap->win0_lock);
  1761. ret = t4_memory_rw(adap, 0, MEM_EDC0, addr,
  1762. sizeof(indices), (__be32 *)&indices,
  1763. T4_MEMORY_READ);
  1764. spin_unlock(&adap->win0_lock);
  1765. if (!ret) {
  1766. *cidx = (be64_to_cpu(indices) >> 25) & 0xffff;
  1767. *pidx = (be64_to_cpu(indices) >> 9) & 0xffff;
  1768. }
  1769. return ret;
  1770. }
  1771. int cxgb4_sync_txq_pidx(struct net_device *dev, u16 qid, u16 pidx,
  1772. u16 size)
  1773. {
  1774. struct adapter *adap = netdev2adap(dev);
  1775. u16 hw_pidx, hw_cidx;
  1776. int ret;
  1777. ret = read_eq_indices(adap, qid, &hw_pidx, &hw_cidx);
  1778. if (ret)
  1779. goto out;
  1780. if (pidx != hw_pidx) {
  1781. u16 delta;
  1782. u32 val;
  1783. if (pidx >= hw_pidx)
  1784. delta = pidx - hw_pidx;
  1785. else
  1786. delta = size - hw_pidx + pidx;
  1787. if (is_t4(adap->params.chip))
  1788. val = PIDX_V(delta);
  1789. else
  1790. val = PIDX_T5_V(delta);
  1791. wmb();
  1792. t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
  1793. QID_V(qid) | val);
  1794. }
  1795. out:
  1796. return ret;
  1797. }
  1798. EXPORT_SYMBOL(cxgb4_sync_txq_pidx);
  1799. void cxgb4_disable_db_coalescing(struct net_device *dev)
  1800. {
  1801. struct adapter *adap;
  1802. adap = netdev2adap(dev);
  1803. t4_set_reg_field(adap, SGE_DOORBELL_CONTROL_A, NOCOALESCE_F,
  1804. NOCOALESCE_F);
  1805. }
  1806. EXPORT_SYMBOL(cxgb4_disable_db_coalescing);
  1807. void cxgb4_enable_db_coalescing(struct net_device *dev)
  1808. {
  1809. struct adapter *adap;
  1810. adap = netdev2adap(dev);
  1811. t4_set_reg_field(adap, SGE_DOORBELL_CONTROL_A, NOCOALESCE_F, 0);
  1812. }
  1813. EXPORT_SYMBOL(cxgb4_enable_db_coalescing);
  1814. int cxgb4_read_tpte(struct net_device *dev, u32 stag, __be32 *tpte)
  1815. {
  1816. struct adapter *adap;
  1817. u32 offset, memtype, memaddr;
  1818. u32 edc0_size, edc1_size, mc0_size, mc1_size, size;
  1819. u32 edc0_end, edc1_end, mc0_end, mc1_end;
  1820. int ret;
  1821. adap = netdev2adap(dev);
  1822. offset = ((stag >> 8) * 32) + adap->vres.stag.start;
  1823. /* Figure out where the offset lands in the Memory Type/Address scheme.
  1824. * This code assumes that the memory is laid out starting at offset 0
  1825. * with no breaks as: EDC0, EDC1, MC0, MC1. All cards have both EDC0
  1826. * and EDC1. Some cards will have neither MC0 nor MC1, most cards have
  1827. * MC0, and some have both MC0 and MC1.
  1828. */
  1829. size = t4_read_reg(adap, MA_EDRAM0_BAR_A);
  1830. edc0_size = EDRAM0_SIZE_G(size) << 20;
  1831. size = t4_read_reg(adap, MA_EDRAM1_BAR_A);
  1832. edc1_size = EDRAM1_SIZE_G(size) << 20;
  1833. size = t4_read_reg(adap, MA_EXT_MEMORY0_BAR_A);
  1834. mc0_size = EXT_MEM0_SIZE_G(size) << 20;
  1835. edc0_end = edc0_size;
  1836. edc1_end = edc0_end + edc1_size;
  1837. mc0_end = edc1_end + mc0_size;
  1838. if (offset < edc0_end) {
  1839. memtype = MEM_EDC0;
  1840. memaddr = offset;
  1841. } else if (offset < edc1_end) {
  1842. memtype = MEM_EDC1;
  1843. memaddr = offset - edc0_end;
  1844. } else {
  1845. if (offset < mc0_end) {
  1846. memtype = MEM_MC0;
  1847. memaddr = offset - edc1_end;
  1848. } else if (is_t4(adap->params.chip)) {
  1849. /* T4 only has a single memory channel */
  1850. goto err;
  1851. } else {
  1852. size = t4_read_reg(adap, MA_EXT_MEMORY1_BAR_A);
  1853. mc1_size = EXT_MEM1_SIZE_G(size) << 20;
  1854. mc1_end = mc0_end + mc1_size;
  1855. if (offset < mc1_end) {
  1856. memtype = MEM_MC1;
  1857. memaddr = offset - mc0_end;
  1858. } else {
  1859. /* offset beyond the end of any memory */
  1860. goto err;
  1861. }
  1862. }
  1863. }
  1864. spin_lock(&adap->win0_lock);
  1865. ret = t4_memory_rw(adap, 0, memtype, memaddr, 32, tpte, T4_MEMORY_READ);
  1866. spin_unlock(&adap->win0_lock);
  1867. return ret;
  1868. err:
  1869. dev_err(adap->pdev_dev, "stag %#x, offset %#x out of range\n",
  1870. stag, offset);
  1871. return -EINVAL;
  1872. }
  1873. EXPORT_SYMBOL(cxgb4_read_tpte);
  1874. u64 cxgb4_read_sge_timestamp(struct net_device *dev)
  1875. {
  1876. u32 hi, lo;
  1877. struct adapter *adap;
  1878. adap = netdev2adap(dev);
  1879. lo = t4_read_reg(adap, SGE_TIMESTAMP_LO_A);
  1880. hi = TSVAL_G(t4_read_reg(adap, SGE_TIMESTAMP_HI_A));
  1881. return ((u64)hi << 32) | (u64)lo;
  1882. }
  1883. EXPORT_SYMBOL(cxgb4_read_sge_timestamp);
  1884. int cxgb4_bar2_sge_qregs(struct net_device *dev,
  1885. unsigned int qid,
  1886. enum cxgb4_bar2_qtype qtype,
  1887. u64 *pbar2_qoffset,
  1888. unsigned int *pbar2_qid)
  1889. {
  1890. return cxgb4_t4_bar2_sge_qregs(netdev2adap(dev),
  1891. qid,
  1892. (qtype == CXGB4_BAR2_QTYPE_EGRESS
  1893. ? T4_BAR2_QTYPE_EGRESS
  1894. : T4_BAR2_QTYPE_INGRESS),
  1895. pbar2_qoffset,
  1896. pbar2_qid);
  1897. }
  1898. EXPORT_SYMBOL(cxgb4_bar2_sge_qregs);
  1899. static struct pci_driver cxgb4_driver;
  1900. static void check_neigh_update(struct neighbour *neigh)
  1901. {
  1902. const struct device *parent;
  1903. const struct net_device *netdev = neigh->dev;
  1904. if (netdev->priv_flags & IFF_802_1Q_VLAN)
  1905. netdev = vlan_dev_real_dev(netdev);
  1906. parent = netdev->dev.parent;
  1907. if (parent && parent->driver == &cxgb4_driver.driver)
  1908. t4_l2t_update(dev_get_drvdata(parent), neigh);
  1909. }
  1910. static int netevent_cb(struct notifier_block *nb, unsigned long event,
  1911. void *data)
  1912. {
  1913. switch (event) {
  1914. case NETEVENT_NEIGH_UPDATE:
  1915. check_neigh_update(data);
  1916. break;
  1917. case NETEVENT_REDIRECT:
  1918. default:
  1919. break;
  1920. }
  1921. return 0;
  1922. }
  1923. static bool netevent_registered;
  1924. static struct notifier_block cxgb4_netevent_nb = {
  1925. .notifier_call = netevent_cb
  1926. };
  1927. static void drain_db_fifo(struct adapter *adap, int usecs)
  1928. {
  1929. u32 v1, v2, lp_count, hp_count;
  1930. do {
  1931. v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
  1932. v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
  1933. if (is_t4(adap->params.chip)) {
  1934. lp_count = LP_COUNT_G(v1);
  1935. hp_count = HP_COUNT_G(v1);
  1936. } else {
  1937. lp_count = LP_COUNT_T5_G(v1);
  1938. hp_count = HP_COUNT_T5_G(v2);
  1939. }
  1940. if (lp_count == 0 && hp_count == 0)
  1941. break;
  1942. set_current_state(TASK_UNINTERRUPTIBLE);
  1943. schedule_timeout(usecs_to_jiffies(usecs));
  1944. } while (1);
  1945. }
  1946. static void disable_txq_db(struct sge_txq *q)
  1947. {
  1948. unsigned long flags;
  1949. spin_lock_irqsave(&q->db_lock, flags);
  1950. q->db_disabled = 1;
  1951. spin_unlock_irqrestore(&q->db_lock, flags);
  1952. }
  1953. static void enable_txq_db(struct adapter *adap, struct sge_txq *q)
  1954. {
  1955. spin_lock_irq(&q->db_lock);
  1956. if (q->db_pidx_inc) {
  1957. /* Make sure that all writes to the TX descriptors
  1958. * are committed before we tell HW about them.
  1959. */
  1960. wmb();
  1961. t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
  1962. QID_V(q->cntxt_id) | PIDX_V(q->db_pidx_inc));
  1963. q->db_pidx_inc = 0;
  1964. }
  1965. q->db_disabled = 0;
  1966. spin_unlock_irq(&q->db_lock);
  1967. }
  1968. static void disable_dbs(struct adapter *adap)
  1969. {
  1970. int i;
  1971. for_each_ethrxq(&adap->sge, i)
  1972. disable_txq_db(&adap->sge.ethtxq[i].q);
  1973. for_each_ofldrxq(&adap->sge, i)
  1974. disable_txq_db(&adap->sge.ofldtxq[i].q);
  1975. for_each_port(adap, i)
  1976. disable_txq_db(&adap->sge.ctrlq[i].q);
  1977. }
  1978. static void enable_dbs(struct adapter *adap)
  1979. {
  1980. int i;
  1981. for_each_ethrxq(&adap->sge, i)
  1982. enable_txq_db(adap, &adap->sge.ethtxq[i].q);
  1983. for_each_ofldrxq(&adap->sge, i)
  1984. enable_txq_db(adap, &adap->sge.ofldtxq[i].q);
  1985. for_each_port(adap, i)
  1986. enable_txq_db(adap, &adap->sge.ctrlq[i].q);
  1987. }
  1988. static void notify_rdma_uld(struct adapter *adap, enum cxgb4_control cmd)
  1989. {
  1990. if (adap->uld_handle[CXGB4_ULD_RDMA])
  1991. ulds[CXGB4_ULD_RDMA].control(adap->uld_handle[CXGB4_ULD_RDMA],
  1992. cmd);
  1993. }
  1994. static void process_db_full(struct work_struct *work)
  1995. {
  1996. struct adapter *adap;
  1997. adap = container_of(work, struct adapter, db_full_task);
  1998. drain_db_fifo(adap, dbfifo_drain_delay);
  1999. enable_dbs(adap);
  2000. notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
  2001. t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
  2002. DBFIFO_HP_INT_F | DBFIFO_LP_INT_F,
  2003. DBFIFO_HP_INT_F | DBFIFO_LP_INT_F);
  2004. }
  2005. static void sync_txq_pidx(struct adapter *adap, struct sge_txq *q)
  2006. {
  2007. u16 hw_pidx, hw_cidx;
  2008. int ret;
  2009. spin_lock_irq(&q->db_lock);
  2010. ret = read_eq_indices(adap, (u16)q->cntxt_id, &hw_pidx, &hw_cidx);
  2011. if (ret)
  2012. goto out;
  2013. if (q->db_pidx != hw_pidx) {
  2014. u16 delta;
  2015. u32 val;
  2016. if (q->db_pidx >= hw_pidx)
  2017. delta = q->db_pidx - hw_pidx;
  2018. else
  2019. delta = q->size - hw_pidx + q->db_pidx;
  2020. if (is_t4(adap->params.chip))
  2021. val = PIDX_V(delta);
  2022. else
  2023. val = PIDX_T5_V(delta);
  2024. wmb();
  2025. t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
  2026. QID_V(q->cntxt_id) | val);
  2027. }
  2028. out:
  2029. q->db_disabled = 0;
  2030. q->db_pidx_inc = 0;
  2031. spin_unlock_irq(&q->db_lock);
  2032. if (ret)
  2033. CH_WARN(adap, "DB drop recovery failed.\n");
  2034. }
  2035. static void recover_all_queues(struct adapter *adap)
  2036. {
  2037. int i;
  2038. for_each_ethrxq(&adap->sge, i)
  2039. sync_txq_pidx(adap, &adap->sge.ethtxq[i].q);
  2040. for_each_ofldrxq(&adap->sge, i)
  2041. sync_txq_pidx(adap, &adap->sge.ofldtxq[i].q);
  2042. for_each_port(adap, i)
  2043. sync_txq_pidx(adap, &adap->sge.ctrlq[i].q);
  2044. }
  2045. static void process_db_drop(struct work_struct *work)
  2046. {
  2047. struct adapter *adap;
  2048. adap = container_of(work, struct adapter, db_drop_task);
  2049. if (is_t4(adap->params.chip)) {
  2050. drain_db_fifo(adap, dbfifo_drain_delay);
  2051. notify_rdma_uld(adap, CXGB4_CONTROL_DB_DROP);
  2052. drain_db_fifo(adap, dbfifo_drain_delay);
  2053. recover_all_queues(adap);
  2054. drain_db_fifo(adap, dbfifo_drain_delay);
  2055. enable_dbs(adap);
  2056. notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
  2057. } else {
  2058. u32 dropped_db = t4_read_reg(adap, 0x010ac);
  2059. u16 qid = (dropped_db >> 15) & 0x1ffff;
  2060. u16 pidx_inc = dropped_db & 0x1fff;
  2061. u64 bar2_qoffset;
  2062. unsigned int bar2_qid;
  2063. int ret;
  2064. ret = cxgb4_t4_bar2_sge_qregs(adap, qid, T4_BAR2_QTYPE_EGRESS,
  2065. &bar2_qoffset, &bar2_qid);
  2066. if (ret)
  2067. dev_err(adap->pdev_dev, "doorbell drop recovery: "
  2068. "qid=%d, pidx_inc=%d\n", qid, pidx_inc);
  2069. else
  2070. writel(PIDX_T5_V(pidx_inc) | QID_V(bar2_qid),
  2071. adap->bar2 + bar2_qoffset + SGE_UDB_KDOORBELL);
  2072. /* Re-enable BAR2 WC */
  2073. t4_set_reg_field(adap, 0x10b0, 1<<15, 1<<15);
  2074. }
  2075. t4_set_reg_field(adap, SGE_DOORBELL_CONTROL_A, DROPPED_DB_F, 0);
  2076. }
  2077. void t4_db_full(struct adapter *adap)
  2078. {
  2079. if (is_t4(adap->params.chip)) {
  2080. disable_dbs(adap);
  2081. notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
  2082. t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
  2083. DBFIFO_HP_INT_F | DBFIFO_LP_INT_F, 0);
  2084. queue_work(adap->workq, &adap->db_full_task);
  2085. }
  2086. }
  2087. void t4_db_dropped(struct adapter *adap)
  2088. {
  2089. if (is_t4(adap->params.chip)) {
  2090. disable_dbs(adap);
  2091. notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
  2092. }
  2093. queue_work(adap->workq, &adap->db_drop_task);
  2094. }
  2095. static void uld_attach(struct adapter *adap, unsigned int uld)
  2096. {
  2097. void *handle;
  2098. struct cxgb4_lld_info lli;
  2099. unsigned short i;
  2100. lli.pdev = adap->pdev;
  2101. lli.pf = adap->fn;
  2102. lli.l2t = adap->l2t;
  2103. lli.tids = &adap->tids;
  2104. lli.ports = adap->port;
  2105. lli.vr = &adap->vres;
  2106. lli.mtus = adap->params.mtus;
  2107. if (uld == CXGB4_ULD_RDMA) {
  2108. lli.rxq_ids = adap->sge.rdma_rxq;
  2109. lli.ciq_ids = adap->sge.rdma_ciq;
  2110. lli.nrxq = adap->sge.rdmaqs;
  2111. lli.nciq = adap->sge.rdmaciqs;
  2112. } else if (uld == CXGB4_ULD_ISCSI) {
  2113. lli.rxq_ids = adap->sge.ofld_rxq;
  2114. lli.nrxq = adap->sge.ofldqsets;
  2115. }
  2116. lli.ntxq = adap->sge.ofldqsets;
  2117. lli.nchan = adap->params.nports;
  2118. lli.nports = adap->params.nports;
  2119. lli.wr_cred = adap->params.ofldq_wr_cred;
  2120. lli.adapter_type = adap->params.chip;
  2121. lli.iscsi_iolen = MAXRXDATA_G(t4_read_reg(adap, TP_PARA_REG2_A));
  2122. lli.cclk_ps = 1000000000 / adap->params.vpd.cclk;
  2123. lli.udb_density = 1 << adap->params.sge.eq_qpp;
  2124. lli.ucq_density = 1 << adap->params.sge.iq_qpp;
  2125. lli.filt_mode = adap->params.tp.vlan_pri_map;
  2126. /* MODQ_REQ_MAP sets queues 0-3 to chan 0-3 */
  2127. for (i = 0; i < NCHAN; i++)
  2128. lli.tx_modq[i] = i;
  2129. lli.gts_reg = adap->regs + MYPF_REG(SGE_PF_GTS_A);
  2130. lli.db_reg = adap->regs + MYPF_REG(SGE_PF_KDOORBELL_A);
  2131. lli.fw_vers = adap->params.fw_vers;
  2132. lli.dbfifo_int_thresh = dbfifo_int_thresh;
  2133. lli.sge_ingpadboundary = adap->sge.fl_align;
  2134. lli.sge_egrstatuspagesize = adap->sge.stat_len;
  2135. lli.sge_pktshift = adap->sge.pktshift;
  2136. lli.enable_fw_ofld_conn = adap->flags & FW_OFLD_CONN;
  2137. lli.max_ordird_qp = adap->params.max_ordird_qp;
  2138. lli.max_ird_adapter = adap->params.max_ird_adapter;
  2139. lli.ulptx_memwrite_dsgl = adap->params.ulptx_memwrite_dsgl;
  2140. handle = ulds[uld].add(&lli);
  2141. if (IS_ERR(handle)) {
  2142. dev_warn(adap->pdev_dev,
  2143. "could not attach to the %s driver, error %ld\n",
  2144. uld_str[uld], PTR_ERR(handle));
  2145. return;
  2146. }
  2147. adap->uld_handle[uld] = handle;
  2148. if (!netevent_registered) {
  2149. register_netevent_notifier(&cxgb4_netevent_nb);
  2150. netevent_registered = true;
  2151. }
  2152. if (adap->flags & FULL_INIT_DONE)
  2153. ulds[uld].state_change(handle, CXGB4_STATE_UP);
  2154. }
  2155. static void attach_ulds(struct adapter *adap)
  2156. {
  2157. unsigned int i;
  2158. spin_lock(&adap_rcu_lock);
  2159. list_add_tail_rcu(&adap->rcu_node, &adap_rcu_list);
  2160. spin_unlock(&adap_rcu_lock);
  2161. mutex_lock(&uld_mutex);
  2162. list_add_tail(&adap->list_node, &adapter_list);
  2163. for (i = 0; i < CXGB4_ULD_MAX; i++)
  2164. if (ulds[i].add)
  2165. uld_attach(adap, i);
  2166. mutex_unlock(&uld_mutex);
  2167. }
  2168. static void detach_ulds(struct adapter *adap)
  2169. {
  2170. unsigned int i;
  2171. mutex_lock(&uld_mutex);
  2172. list_del(&adap->list_node);
  2173. for (i = 0; i < CXGB4_ULD_MAX; i++)
  2174. if (adap->uld_handle[i]) {
  2175. ulds[i].state_change(adap->uld_handle[i],
  2176. CXGB4_STATE_DETACH);
  2177. adap->uld_handle[i] = NULL;
  2178. }
  2179. if (netevent_registered && list_empty(&adapter_list)) {
  2180. unregister_netevent_notifier(&cxgb4_netevent_nb);
  2181. netevent_registered = false;
  2182. }
  2183. mutex_unlock(&uld_mutex);
  2184. spin_lock(&adap_rcu_lock);
  2185. list_del_rcu(&adap->rcu_node);
  2186. spin_unlock(&adap_rcu_lock);
  2187. }
  2188. static void notify_ulds(struct adapter *adap, enum cxgb4_state new_state)
  2189. {
  2190. unsigned int i;
  2191. mutex_lock(&uld_mutex);
  2192. for (i = 0; i < CXGB4_ULD_MAX; i++)
  2193. if (adap->uld_handle[i])
  2194. ulds[i].state_change(adap->uld_handle[i], new_state);
  2195. mutex_unlock(&uld_mutex);
  2196. }
  2197. /**
  2198. * cxgb4_register_uld - register an upper-layer driver
  2199. * @type: the ULD type
  2200. * @p: the ULD methods
  2201. *
  2202. * Registers an upper-layer driver with this driver and notifies the ULD
  2203. * about any presently available devices that support its type. Returns
  2204. * %-EBUSY if a ULD of the same type is already registered.
  2205. */
  2206. int cxgb4_register_uld(enum cxgb4_uld type, const struct cxgb4_uld_info *p)
  2207. {
  2208. int ret = 0;
  2209. struct adapter *adap;
  2210. if (type >= CXGB4_ULD_MAX)
  2211. return -EINVAL;
  2212. mutex_lock(&uld_mutex);
  2213. if (ulds[type].add) {
  2214. ret = -EBUSY;
  2215. goto out;
  2216. }
  2217. ulds[type] = *p;
  2218. list_for_each_entry(adap, &adapter_list, list_node)
  2219. uld_attach(adap, type);
  2220. out: mutex_unlock(&uld_mutex);
  2221. return ret;
  2222. }
  2223. EXPORT_SYMBOL(cxgb4_register_uld);
  2224. /**
  2225. * cxgb4_unregister_uld - unregister an upper-layer driver
  2226. * @type: the ULD type
  2227. *
  2228. * Unregisters an existing upper-layer driver.
  2229. */
  2230. int cxgb4_unregister_uld(enum cxgb4_uld type)
  2231. {
  2232. struct adapter *adap;
  2233. if (type >= CXGB4_ULD_MAX)
  2234. return -EINVAL;
  2235. mutex_lock(&uld_mutex);
  2236. list_for_each_entry(adap, &adapter_list, list_node)
  2237. adap->uld_handle[type] = NULL;
  2238. ulds[type].add = NULL;
  2239. mutex_unlock(&uld_mutex);
  2240. return 0;
  2241. }
  2242. EXPORT_SYMBOL(cxgb4_unregister_uld);
  2243. #if IS_ENABLED(CONFIG_IPV6)
  2244. static int cxgb4_inet6addr_handler(struct notifier_block *this,
  2245. unsigned long event, void *data)
  2246. {
  2247. struct inet6_ifaddr *ifa = data;
  2248. struct net_device *event_dev = ifa->idev->dev;
  2249. const struct device *parent = NULL;
  2250. #if IS_ENABLED(CONFIG_BONDING)
  2251. struct adapter *adap;
  2252. #endif
  2253. if (event_dev->priv_flags & IFF_802_1Q_VLAN)
  2254. event_dev = vlan_dev_real_dev(event_dev);
  2255. #if IS_ENABLED(CONFIG_BONDING)
  2256. if (event_dev->flags & IFF_MASTER) {
  2257. list_for_each_entry(adap, &adapter_list, list_node) {
  2258. switch (event) {
  2259. case NETDEV_UP:
  2260. cxgb4_clip_get(adap->port[0],
  2261. (const u32 *)ifa, 1);
  2262. break;
  2263. case NETDEV_DOWN:
  2264. cxgb4_clip_release(adap->port[0],
  2265. (const u32 *)ifa, 1);
  2266. break;
  2267. default:
  2268. break;
  2269. }
  2270. }
  2271. return NOTIFY_OK;
  2272. }
  2273. #endif
  2274. if (event_dev)
  2275. parent = event_dev->dev.parent;
  2276. if (parent && parent->driver == &cxgb4_driver.driver) {
  2277. switch (event) {
  2278. case NETDEV_UP:
  2279. cxgb4_clip_get(event_dev, (const u32 *)ifa, 1);
  2280. break;
  2281. case NETDEV_DOWN:
  2282. cxgb4_clip_release(event_dev, (const u32 *)ifa, 1);
  2283. break;
  2284. default:
  2285. break;
  2286. }
  2287. }
  2288. return NOTIFY_OK;
  2289. }
  2290. static bool inet6addr_registered;
  2291. static struct notifier_block cxgb4_inet6addr_notifier = {
  2292. .notifier_call = cxgb4_inet6addr_handler
  2293. };
  2294. static void update_clip(const struct adapter *adap)
  2295. {
  2296. int i;
  2297. struct net_device *dev;
  2298. int ret;
  2299. rcu_read_lock();
  2300. for (i = 0; i < MAX_NPORTS; i++) {
  2301. dev = adap->port[i];
  2302. ret = 0;
  2303. if (dev)
  2304. ret = cxgb4_update_root_dev_clip(dev);
  2305. if (ret < 0)
  2306. break;
  2307. }
  2308. rcu_read_unlock();
  2309. }
  2310. #endif /* IS_ENABLED(CONFIG_IPV6) */
  2311. /**
  2312. * cxgb_up - enable the adapter
  2313. * @adap: adapter being enabled
  2314. *
  2315. * Called when the first port is enabled, this function performs the
  2316. * actions necessary to make an adapter operational, such as completing
  2317. * the initialization of HW modules, and enabling interrupts.
  2318. *
  2319. * Must be called with the rtnl lock held.
  2320. */
  2321. static int cxgb_up(struct adapter *adap)
  2322. {
  2323. int err;
  2324. err = setup_sge_queues(adap);
  2325. if (err)
  2326. goto out;
  2327. err = setup_rss(adap);
  2328. if (err)
  2329. goto freeq;
  2330. if (adap->flags & USING_MSIX) {
  2331. name_msix_vecs(adap);
  2332. err = request_irq(adap->msix_info[0].vec, t4_nondata_intr, 0,
  2333. adap->msix_info[0].desc, adap);
  2334. if (err)
  2335. goto irq_err;
  2336. err = request_msix_queue_irqs(adap);
  2337. if (err) {
  2338. free_irq(adap->msix_info[0].vec, adap);
  2339. goto irq_err;
  2340. }
  2341. } else {
  2342. err = request_irq(adap->pdev->irq, t4_intr_handler(adap),
  2343. (adap->flags & USING_MSI) ? 0 : IRQF_SHARED,
  2344. adap->port[0]->name, adap);
  2345. if (err)
  2346. goto irq_err;
  2347. }
  2348. enable_rx(adap);
  2349. t4_sge_start(adap);
  2350. t4_intr_enable(adap);
  2351. adap->flags |= FULL_INIT_DONE;
  2352. notify_ulds(adap, CXGB4_STATE_UP);
  2353. #if IS_ENABLED(CONFIG_IPV6)
  2354. update_clip(adap);
  2355. #endif
  2356. out:
  2357. return err;
  2358. irq_err:
  2359. dev_err(adap->pdev_dev, "request_irq failed, err %d\n", err);
  2360. freeq:
  2361. t4_free_sge_resources(adap);
  2362. goto out;
  2363. }
  2364. static void cxgb_down(struct adapter *adapter)
  2365. {
  2366. cancel_work_sync(&adapter->tid_release_task);
  2367. cancel_work_sync(&adapter->db_full_task);
  2368. cancel_work_sync(&adapter->db_drop_task);
  2369. adapter->tid_release_task_busy = false;
  2370. adapter->tid_release_head = NULL;
  2371. t4_sge_stop(adapter);
  2372. t4_free_sge_resources(adapter);
  2373. adapter->flags &= ~FULL_INIT_DONE;
  2374. }
  2375. /*
  2376. * net_device operations
  2377. */
  2378. static int cxgb_open(struct net_device *dev)
  2379. {
  2380. int err;
  2381. struct port_info *pi = netdev_priv(dev);
  2382. struct adapter *adapter = pi->adapter;
  2383. netif_carrier_off(dev);
  2384. if (!(adapter->flags & FULL_INIT_DONE)) {
  2385. err = cxgb_up(adapter);
  2386. if (err < 0)
  2387. return err;
  2388. }
  2389. err = link_start(dev);
  2390. if (!err)
  2391. netif_tx_start_all_queues(dev);
  2392. return err;
  2393. }
  2394. static int cxgb_close(struct net_device *dev)
  2395. {
  2396. struct port_info *pi = netdev_priv(dev);
  2397. struct adapter *adapter = pi->adapter;
  2398. netif_tx_stop_all_queues(dev);
  2399. netif_carrier_off(dev);
  2400. return t4_enable_vi(adapter, adapter->fn, pi->viid, false, false);
  2401. }
  2402. /* Return an error number if the indicated filter isn't writable ...
  2403. */
  2404. static int writable_filter(struct filter_entry *f)
  2405. {
  2406. if (f->locked)
  2407. return -EPERM;
  2408. if (f->pending)
  2409. return -EBUSY;
  2410. return 0;
  2411. }
  2412. /* Delete the filter at the specified index (if valid). The checks for all
  2413. * the common problems with doing this like the filter being locked, currently
  2414. * pending in another operation, etc.
  2415. */
  2416. static int delete_filter(struct adapter *adapter, unsigned int fidx)
  2417. {
  2418. struct filter_entry *f;
  2419. int ret;
  2420. if (fidx >= adapter->tids.nftids + adapter->tids.nsftids)
  2421. return -EINVAL;
  2422. f = &adapter->tids.ftid_tab[fidx];
  2423. ret = writable_filter(f);
  2424. if (ret)
  2425. return ret;
  2426. if (f->valid)
  2427. return del_filter_wr(adapter, fidx);
  2428. return 0;
  2429. }
  2430. int cxgb4_create_server_filter(const struct net_device *dev, unsigned int stid,
  2431. __be32 sip, __be16 sport, __be16 vlan,
  2432. unsigned int queue, unsigned char port, unsigned char mask)
  2433. {
  2434. int ret;
  2435. struct filter_entry *f;
  2436. struct adapter *adap;
  2437. int i;
  2438. u8 *val;
  2439. adap = netdev2adap(dev);
  2440. /* Adjust stid to correct filter index */
  2441. stid -= adap->tids.sftid_base;
  2442. stid += adap->tids.nftids;
  2443. /* Check to make sure the filter requested is writable ...
  2444. */
  2445. f = &adap->tids.ftid_tab[stid];
  2446. ret = writable_filter(f);
  2447. if (ret)
  2448. return ret;
  2449. /* Clear out any old resources being used by the filter before
  2450. * we start constructing the new filter.
  2451. */
  2452. if (f->valid)
  2453. clear_filter(adap, f);
  2454. /* Clear out filter specifications */
  2455. memset(&f->fs, 0, sizeof(struct ch_filter_specification));
  2456. f->fs.val.lport = cpu_to_be16(sport);
  2457. f->fs.mask.lport = ~0;
  2458. val = (u8 *)&sip;
  2459. if ((val[0] | val[1] | val[2] | val[3]) != 0) {
  2460. for (i = 0; i < 4; i++) {
  2461. f->fs.val.lip[i] = val[i];
  2462. f->fs.mask.lip[i] = ~0;
  2463. }
  2464. if (adap->params.tp.vlan_pri_map & PORT_F) {
  2465. f->fs.val.iport = port;
  2466. f->fs.mask.iport = mask;
  2467. }
  2468. }
  2469. if (adap->params.tp.vlan_pri_map & PROTOCOL_F) {
  2470. f->fs.val.proto = IPPROTO_TCP;
  2471. f->fs.mask.proto = ~0;
  2472. }
  2473. f->fs.dirsteer = 1;
  2474. f->fs.iq = queue;
  2475. /* Mark filter as locked */
  2476. f->locked = 1;
  2477. f->fs.rpttid = 1;
  2478. ret = set_filter_wr(adap, stid);
  2479. if (ret) {
  2480. clear_filter(adap, f);
  2481. return ret;
  2482. }
  2483. return 0;
  2484. }
  2485. EXPORT_SYMBOL(cxgb4_create_server_filter);
  2486. int cxgb4_remove_server_filter(const struct net_device *dev, unsigned int stid,
  2487. unsigned int queue, bool ipv6)
  2488. {
  2489. int ret;
  2490. struct filter_entry *f;
  2491. struct adapter *adap;
  2492. adap = netdev2adap(dev);
  2493. /* Adjust stid to correct filter index */
  2494. stid -= adap->tids.sftid_base;
  2495. stid += adap->tids.nftids;
  2496. f = &adap->tids.ftid_tab[stid];
  2497. /* Unlock the filter */
  2498. f->locked = 0;
  2499. ret = delete_filter(adap, stid);
  2500. if (ret)
  2501. return ret;
  2502. return 0;
  2503. }
  2504. EXPORT_SYMBOL(cxgb4_remove_server_filter);
  2505. static struct rtnl_link_stats64 *cxgb_get_stats(struct net_device *dev,
  2506. struct rtnl_link_stats64 *ns)
  2507. {
  2508. struct port_stats stats;
  2509. struct port_info *p = netdev_priv(dev);
  2510. struct adapter *adapter = p->adapter;
  2511. /* Block retrieving statistics during EEH error
  2512. * recovery. Otherwise, the recovery might fail
  2513. * and the PCI device will be removed permanently
  2514. */
  2515. spin_lock(&adapter->stats_lock);
  2516. if (!netif_device_present(dev)) {
  2517. spin_unlock(&adapter->stats_lock);
  2518. return ns;
  2519. }
  2520. t4_get_port_stats(adapter, p->tx_chan, &stats);
  2521. spin_unlock(&adapter->stats_lock);
  2522. ns->tx_bytes = stats.tx_octets;
  2523. ns->tx_packets = stats.tx_frames;
  2524. ns->rx_bytes = stats.rx_octets;
  2525. ns->rx_packets = stats.rx_frames;
  2526. ns->multicast = stats.rx_mcast_frames;
  2527. /* detailed rx_errors */
  2528. ns->rx_length_errors = stats.rx_jabber + stats.rx_too_long +
  2529. stats.rx_runt;
  2530. ns->rx_over_errors = 0;
  2531. ns->rx_crc_errors = stats.rx_fcs_err;
  2532. ns->rx_frame_errors = stats.rx_symbol_err;
  2533. ns->rx_fifo_errors = stats.rx_ovflow0 + stats.rx_ovflow1 +
  2534. stats.rx_ovflow2 + stats.rx_ovflow3 +
  2535. stats.rx_trunc0 + stats.rx_trunc1 +
  2536. stats.rx_trunc2 + stats.rx_trunc3;
  2537. ns->rx_missed_errors = 0;
  2538. /* detailed tx_errors */
  2539. ns->tx_aborted_errors = 0;
  2540. ns->tx_carrier_errors = 0;
  2541. ns->tx_fifo_errors = 0;
  2542. ns->tx_heartbeat_errors = 0;
  2543. ns->tx_window_errors = 0;
  2544. ns->tx_errors = stats.tx_error_frames;
  2545. ns->rx_errors = stats.rx_symbol_err + stats.rx_fcs_err +
  2546. ns->rx_length_errors + stats.rx_len_err + ns->rx_fifo_errors;
  2547. return ns;
  2548. }
  2549. static int cxgb_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
  2550. {
  2551. unsigned int mbox;
  2552. int ret = 0, prtad, devad;
  2553. struct port_info *pi = netdev_priv(dev);
  2554. struct mii_ioctl_data *data = (struct mii_ioctl_data *)&req->ifr_data;
  2555. switch (cmd) {
  2556. case SIOCGMIIPHY:
  2557. if (pi->mdio_addr < 0)
  2558. return -EOPNOTSUPP;
  2559. data->phy_id = pi->mdio_addr;
  2560. break;
  2561. case SIOCGMIIREG:
  2562. case SIOCSMIIREG:
  2563. if (mdio_phy_id_is_c45(data->phy_id)) {
  2564. prtad = mdio_phy_id_prtad(data->phy_id);
  2565. devad = mdio_phy_id_devad(data->phy_id);
  2566. } else if (data->phy_id < 32) {
  2567. prtad = data->phy_id;
  2568. devad = 0;
  2569. data->reg_num &= 0x1f;
  2570. } else
  2571. return -EINVAL;
  2572. mbox = pi->adapter->fn;
  2573. if (cmd == SIOCGMIIREG)
  2574. ret = t4_mdio_rd(pi->adapter, mbox, prtad, devad,
  2575. data->reg_num, &data->val_out);
  2576. else
  2577. ret = t4_mdio_wr(pi->adapter, mbox, prtad, devad,
  2578. data->reg_num, data->val_in);
  2579. break;
  2580. default:
  2581. return -EOPNOTSUPP;
  2582. }
  2583. return ret;
  2584. }
  2585. static void cxgb_set_rxmode(struct net_device *dev)
  2586. {
  2587. /* unfortunately we can't return errors to the stack */
  2588. set_rxmode(dev, -1, false);
  2589. }
  2590. static int cxgb_change_mtu(struct net_device *dev, int new_mtu)
  2591. {
  2592. int ret;
  2593. struct port_info *pi = netdev_priv(dev);
  2594. if (new_mtu < 81 || new_mtu > MAX_MTU) /* accommodate SACK */
  2595. return -EINVAL;
  2596. ret = t4_set_rxmode(pi->adapter, pi->adapter->fn, pi->viid, new_mtu, -1,
  2597. -1, -1, -1, true);
  2598. if (!ret)
  2599. dev->mtu = new_mtu;
  2600. return ret;
  2601. }
  2602. static int cxgb_set_mac_addr(struct net_device *dev, void *p)
  2603. {
  2604. int ret;
  2605. struct sockaddr *addr = p;
  2606. struct port_info *pi = netdev_priv(dev);
  2607. if (!is_valid_ether_addr(addr->sa_data))
  2608. return -EADDRNOTAVAIL;
  2609. ret = t4_change_mac(pi->adapter, pi->adapter->fn, pi->viid,
  2610. pi->xact_addr_filt, addr->sa_data, true, true);
  2611. if (ret < 0)
  2612. return ret;
  2613. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  2614. pi->xact_addr_filt = ret;
  2615. return 0;
  2616. }
  2617. #ifdef CONFIG_NET_POLL_CONTROLLER
  2618. static void cxgb_netpoll(struct net_device *dev)
  2619. {
  2620. struct port_info *pi = netdev_priv(dev);
  2621. struct adapter *adap = pi->adapter;
  2622. if (adap->flags & USING_MSIX) {
  2623. int i;
  2624. struct sge_eth_rxq *rx = &adap->sge.ethrxq[pi->first_qset];
  2625. for (i = pi->nqsets; i; i--, rx++)
  2626. t4_sge_intr_msix(0, &rx->rspq);
  2627. } else
  2628. t4_intr_handler(adap)(0, adap);
  2629. }
  2630. #endif
  2631. static const struct net_device_ops cxgb4_netdev_ops = {
  2632. .ndo_open = cxgb_open,
  2633. .ndo_stop = cxgb_close,
  2634. .ndo_start_xmit = t4_eth_xmit,
  2635. .ndo_select_queue = cxgb_select_queue,
  2636. .ndo_get_stats64 = cxgb_get_stats,
  2637. .ndo_set_rx_mode = cxgb_set_rxmode,
  2638. .ndo_set_mac_address = cxgb_set_mac_addr,
  2639. .ndo_set_features = cxgb_set_features,
  2640. .ndo_validate_addr = eth_validate_addr,
  2641. .ndo_do_ioctl = cxgb_ioctl,
  2642. .ndo_change_mtu = cxgb_change_mtu,
  2643. #ifdef CONFIG_NET_POLL_CONTROLLER
  2644. .ndo_poll_controller = cxgb_netpoll,
  2645. #endif
  2646. #ifdef CONFIG_CHELSIO_T4_FCOE
  2647. .ndo_fcoe_enable = cxgb_fcoe_enable,
  2648. .ndo_fcoe_disable = cxgb_fcoe_disable,
  2649. #endif /* CONFIG_CHELSIO_T4_FCOE */
  2650. #ifdef CONFIG_NET_RX_BUSY_POLL
  2651. .ndo_busy_poll = cxgb_busy_poll,
  2652. #endif
  2653. };
  2654. void t4_fatal_err(struct adapter *adap)
  2655. {
  2656. t4_set_reg_field(adap, SGE_CONTROL_A, GLOBALENABLE_F, 0);
  2657. t4_intr_disable(adap);
  2658. dev_alert(adap->pdev_dev, "encountered fatal error, adapter stopped\n");
  2659. }
  2660. /* Return the specified PCI-E Configuration Space register from our Physical
  2661. * Function. We try first via a Firmware LDST Command since we prefer to let
  2662. * the firmware own all of these registers, but if that fails we go for it
  2663. * directly ourselves.
  2664. */
  2665. static u32 t4_read_pcie_cfg4(struct adapter *adap, int reg)
  2666. {
  2667. struct fw_ldst_cmd ldst_cmd;
  2668. u32 val;
  2669. int ret;
  2670. /* Construct and send the Firmware LDST Command to retrieve the
  2671. * specified PCI-E Configuration Space register.
  2672. */
  2673. memset(&ldst_cmd, 0, sizeof(ldst_cmd));
  2674. ldst_cmd.op_to_addrspace =
  2675. htonl(FW_CMD_OP_V(FW_LDST_CMD) |
  2676. FW_CMD_REQUEST_F |
  2677. FW_CMD_READ_F |
  2678. FW_LDST_CMD_ADDRSPACE_V(FW_LDST_ADDRSPC_FUNC_PCIE));
  2679. ldst_cmd.cycles_to_len16 = htonl(FW_LEN16(ldst_cmd));
  2680. ldst_cmd.u.pcie.select_naccess = FW_LDST_CMD_NACCESS_V(1);
  2681. ldst_cmd.u.pcie.ctrl_to_fn =
  2682. (FW_LDST_CMD_LC_F | FW_LDST_CMD_FN_V(adap->fn));
  2683. ldst_cmd.u.pcie.r = reg;
  2684. ret = t4_wr_mbox(adap, adap->mbox, &ldst_cmd, sizeof(ldst_cmd),
  2685. &ldst_cmd);
  2686. /* If the LDST Command suucceeded, exctract the returned register
  2687. * value. Otherwise read it directly ourself.
  2688. */
  2689. if (ret == 0)
  2690. val = ntohl(ldst_cmd.u.pcie.data[0]);
  2691. else
  2692. t4_hw_pci_read_cfg4(adap, reg, &val);
  2693. return val;
  2694. }
  2695. static void setup_memwin(struct adapter *adap)
  2696. {
  2697. u32 mem_win0_base, mem_win1_base, mem_win2_base, mem_win2_aperture;
  2698. if (is_t4(adap->params.chip)) {
  2699. u32 bar0;
  2700. /* Truncation intentional: we only read the bottom 32-bits of
  2701. * the 64-bit BAR0/BAR1 ... We use the hardware backdoor
  2702. * mechanism to read BAR0 instead of using
  2703. * pci_resource_start() because we could be operating from
  2704. * within a Virtual Machine which is trapping our accesses to
  2705. * our Configuration Space and we need to set up the PCI-E
  2706. * Memory Window decoders with the actual addresses which will
  2707. * be coming across the PCI-E link.
  2708. */
  2709. bar0 = t4_read_pcie_cfg4(adap, PCI_BASE_ADDRESS_0);
  2710. bar0 &= PCI_BASE_ADDRESS_MEM_MASK;
  2711. adap->t4_bar0 = bar0;
  2712. mem_win0_base = bar0 + MEMWIN0_BASE;
  2713. mem_win1_base = bar0 + MEMWIN1_BASE;
  2714. mem_win2_base = bar0 + MEMWIN2_BASE;
  2715. mem_win2_aperture = MEMWIN2_APERTURE;
  2716. } else {
  2717. /* For T5, only relative offset inside the PCIe BAR is passed */
  2718. mem_win0_base = MEMWIN0_BASE;
  2719. mem_win1_base = MEMWIN1_BASE;
  2720. mem_win2_base = MEMWIN2_BASE_T5;
  2721. mem_win2_aperture = MEMWIN2_APERTURE_T5;
  2722. }
  2723. t4_write_reg(adap, PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN_A, 0),
  2724. mem_win0_base | BIR_V(0) |
  2725. WINDOW_V(ilog2(MEMWIN0_APERTURE) - 10));
  2726. t4_write_reg(adap, PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN_A, 1),
  2727. mem_win1_base | BIR_V(0) |
  2728. WINDOW_V(ilog2(MEMWIN1_APERTURE) - 10));
  2729. t4_write_reg(adap, PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN_A, 2),
  2730. mem_win2_base | BIR_V(0) |
  2731. WINDOW_V(ilog2(mem_win2_aperture) - 10));
  2732. t4_read_reg(adap, PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN_A, 2));
  2733. }
  2734. static void setup_memwin_rdma(struct adapter *adap)
  2735. {
  2736. if (adap->vres.ocq.size) {
  2737. u32 start;
  2738. unsigned int sz_kb;
  2739. start = t4_read_pcie_cfg4(adap, PCI_BASE_ADDRESS_2);
  2740. start &= PCI_BASE_ADDRESS_MEM_MASK;
  2741. start += OCQ_WIN_OFFSET(adap->pdev, &adap->vres);
  2742. sz_kb = roundup_pow_of_two(adap->vres.ocq.size) >> 10;
  2743. t4_write_reg(adap,
  2744. PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN_A, 3),
  2745. start | BIR_V(1) | WINDOW_V(ilog2(sz_kb)));
  2746. t4_write_reg(adap,
  2747. PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3),
  2748. adap->vres.ocq.start);
  2749. t4_read_reg(adap,
  2750. PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3));
  2751. }
  2752. }
  2753. static int adap_init1(struct adapter *adap, struct fw_caps_config_cmd *c)
  2754. {
  2755. u32 v;
  2756. int ret;
  2757. /* get device capabilities */
  2758. memset(c, 0, sizeof(*c));
  2759. c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
  2760. FW_CMD_REQUEST_F | FW_CMD_READ_F);
  2761. c->cfvalid_to_len16 = htonl(FW_LEN16(*c));
  2762. ret = t4_wr_mbox(adap, adap->fn, c, sizeof(*c), c);
  2763. if (ret < 0)
  2764. return ret;
  2765. /* select capabilities we'll be using */
  2766. if (c->niccaps & htons(FW_CAPS_CONFIG_NIC_VM)) {
  2767. if (!vf_acls)
  2768. c->niccaps ^= htons(FW_CAPS_CONFIG_NIC_VM);
  2769. else
  2770. c->niccaps = htons(FW_CAPS_CONFIG_NIC_VM);
  2771. } else if (vf_acls) {
  2772. dev_err(adap->pdev_dev, "virtualization ACLs not supported");
  2773. return ret;
  2774. }
  2775. c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
  2776. FW_CMD_REQUEST_F | FW_CMD_WRITE_F);
  2777. ret = t4_wr_mbox(adap, adap->fn, c, sizeof(*c), NULL);
  2778. if (ret < 0)
  2779. return ret;
  2780. ret = t4_config_glbl_rss(adap, adap->fn,
  2781. FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL,
  2782. FW_RSS_GLB_CONFIG_CMD_TNLMAPEN_F |
  2783. FW_RSS_GLB_CONFIG_CMD_TNLALLLKP_F);
  2784. if (ret < 0)
  2785. return ret;
  2786. ret = t4_cfg_pfvf(adap, adap->fn, adap->fn, 0, adap->sge.egr_sz, 64,
  2787. MAX_INGQ, 0, 0, 4, 0xf, 0xf, 16, FW_CMD_CAP_PF,
  2788. FW_CMD_CAP_PF);
  2789. if (ret < 0)
  2790. return ret;
  2791. t4_sge_init(adap);
  2792. /* tweak some settings */
  2793. t4_write_reg(adap, TP_SHIFT_CNT_A, 0x64f8849);
  2794. t4_write_reg(adap, ULP_RX_TDDP_PSZ_A, HPZ0_V(PAGE_SHIFT - 12));
  2795. t4_write_reg(adap, TP_PIO_ADDR_A, TP_INGRESS_CONFIG_A);
  2796. v = t4_read_reg(adap, TP_PIO_DATA_A);
  2797. t4_write_reg(adap, TP_PIO_DATA_A, v & ~CSUM_HAS_PSEUDO_HDR_F);
  2798. /* first 4 Tx modulation queues point to consecutive Tx channels */
  2799. adap->params.tp.tx_modq_map = 0xE4;
  2800. t4_write_reg(adap, TP_TX_MOD_QUEUE_REQ_MAP_A,
  2801. TX_MOD_QUEUE_REQ_MAP_V(adap->params.tp.tx_modq_map));
  2802. /* associate each Tx modulation queue with consecutive Tx channels */
  2803. v = 0x84218421;
  2804. t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
  2805. &v, 1, TP_TX_SCHED_HDR_A);
  2806. t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
  2807. &v, 1, TP_TX_SCHED_FIFO_A);
  2808. t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
  2809. &v, 1, TP_TX_SCHED_PCMD_A);
  2810. #define T4_TX_MODQ_10G_WEIGHT_DEFAULT 16 /* in KB units */
  2811. if (is_offload(adap)) {
  2812. t4_write_reg(adap, TP_TX_MOD_QUEUE_WEIGHT0_A,
  2813. TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
  2814. TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
  2815. TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
  2816. TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
  2817. t4_write_reg(adap, TP_TX_MOD_CHANNEL_WEIGHT_A,
  2818. TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
  2819. TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
  2820. TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
  2821. TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
  2822. }
  2823. /* get basic stuff going */
  2824. return t4_early_init(adap, adap->fn);
  2825. }
  2826. /*
  2827. * Max # of ATIDs. The absolute HW max is 16K but we keep it lower.
  2828. */
  2829. #define MAX_ATIDS 8192U
  2830. /*
  2831. * Phase 0 of initialization: contact FW, obtain config, perform basic init.
  2832. *
  2833. * If the firmware we're dealing with has Configuration File support, then
  2834. * we use that to perform all configuration
  2835. */
  2836. /*
  2837. * Tweak configuration based on module parameters, etc. Most of these have
  2838. * defaults assigned to them by Firmware Configuration Files (if we're using
  2839. * them) but need to be explicitly set if we're using hard-coded
  2840. * initialization. But even in the case of using Firmware Configuration
  2841. * Files, we'd like to expose the ability to change these via module
  2842. * parameters so these are essentially common tweaks/settings for
  2843. * Configuration Files and hard-coded initialization ...
  2844. */
  2845. static int adap_init0_tweaks(struct adapter *adapter)
  2846. {
  2847. /*
  2848. * Fix up various Host-Dependent Parameters like Page Size, Cache
  2849. * Line Size, etc. The firmware default is for a 4KB Page Size and
  2850. * 64B Cache Line Size ...
  2851. */
  2852. t4_fixup_host_params(adapter, PAGE_SIZE, L1_CACHE_BYTES);
  2853. /*
  2854. * Process module parameters which affect early initialization.
  2855. */
  2856. if (rx_dma_offset != 2 && rx_dma_offset != 0) {
  2857. dev_err(&adapter->pdev->dev,
  2858. "Ignoring illegal rx_dma_offset=%d, using 2\n",
  2859. rx_dma_offset);
  2860. rx_dma_offset = 2;
  2861. }
  2862. t4_set_reg_field(adapter, SGE_CONTROL_A,
  2863. PKTSHIFT_V(PKTSHIFT_M),
  2864. PKTSHIFT_V(rx_dma_offset));
  2865. /*
  2866. * Don't include the "IP Pseudo Header" in CPL_RX_PKT checksums: Linux
  2867. * adds the pseudo header itself.
  2868. */
  2869. t4_tp_wr_bits_indirect(adapter, TP_INGRESS_CONFIG_A,
  2870. CSUM_HAS_PSEUDO_HDR_F, 0);
  2871. return 0;
  2872. }
  2873. /*
  2874. * Attempt to initialize the adapter via a Firmware Configuration File.
  2875. */
  2876. static int adap_init0_config(struct adapter *adapter, int reset)
  2877. {
  2878. struct fw_caps_config_cmd caps_cmd;
  2879. const struct firmware *cf;
  2880. unsigned long mtype = 0, maddr = 0;
  2881. u32 finiver, finicsum, cfcsum;
  2882. int ret;
  2883. int config_issued = 0;
  2884. char *fw_config_file, fw_config_file_path[256];
  2885. char *config_name = NULL;
  2886. /*
  2887. * Reset device if necessary.
  2888. */
  2889. if (reset) {
  2890. ret = t4_fw_reset(adapter, adapter->mbox,
  2891. PIORSTMODE_F | PIORST_F);
  2892. if (ret < 0)
  2893. goto bye;
  2894. }
  2895. /*
  2896. * If we have a T4 configuration file under /lib/firmware/cxgb4/,
  2897. * then use that. Otherwise, use the configuration file stored
  2898. * in the adapter flash ...
  2899. */
  2900. switch (CHELSIO_CHIP_VERSION(adapter->params.chip)) {
  2901. case CHELSIO_T4:
  2902. fw_config_file = FW4_CFNAME;
  2903. break;
  2904. case CHELSIO_T5:
  2905. fw_config_file = FW5_CFNAME;
  2906. break;
  2907. default:
  2908. dev_err(adapter->pdev_dev, "Device %d is not supported\n",
  2909. adapter->pdev->device);
  2910. ret = -EINVAL;
  2911. goto bye;
  2912. }
  2913. ret = request_firmware(&cf, fw_config_file, adapter->pdev_dev);
  2914. if (ret < 0) {
  2915. config_name = "On FLASH";
  2916. mtype = FW_MEMTYPE_CF_FLASH;
  2917. maddr = t4_flash_cfg_addr(adapter);
  2918. } else {
  2919. u32 params[7], val[7];
  2920. sprintf(fw_config_file_path,
  2921. "/lib/firmware/%s", fw_config_file);
  2922. config_name = fw_config_file_path;
  2923. if (cf->size >= FLASH_CFG_MAX_SIZE)
  2924. ret = -ENOMEM;
  2925. else {
  2926. params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
  2927. FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
  2928. ret = t4_query_params(adapter, adapter->mbox,
  2929. adapter->fn, 0, 1, params, val);
  2930. if (ret == 0) {
  2931. /*
  2932. * For t4_memory_rw() below addresses and
  2933. * sizes have to be in terms of multiples of 4
  2934. * bytes. So, if the Configuration File isn't
  2935. * a multiple of 4 bytes in length we'll have
  2936. * to write that out separately since we can't
  2937. * guarantee that the bytes following the
  2938. * residual byte in the buffer returned by
  2939. * request_firmware() are zeroed out ...
  2940. */
  2941. size_t resid = cf->size & 0x3;
  2942. size_t size = cf->size & ~0x3;
  2943. __be32 *data = (__be32 *)cf->data;
  2944. mtype = FW_PARAMS_PARAM_Y_G(val[0]);
  2945. maddr = FW_PARAMS_PARAM_Z_G(val[0]) << 16;
  2946. spin_lock(&adapter->win0_lock);
  2947. ret = t4_memory_rw(adapter, 0, mtype, maddr,
  2948. size, data, T4_MEMORY_WRITE);
  2949. if (ret == 0 && resid != 0) {
  2950. union {
  2951. __be32 word;
  2952. char buf[4];
  2953. } last;
  2954. int i;
  2955. last.word = data[size >> 2];
  2956. for (i = resid; i < 4; i++)
  2957. last.buf[i] = 0;
  2958. ret = t4_memory_rw(adapter, 0, mtype,
  2959. maddr + size,
  2960. 4, &last.word,
  2961. T4_MEMORY_WRITE);
  2962. }
  2963. spin_unlock(&adapter->win0_lock);
  2964. }
  2965. }
  2966. release_firmware(cf);
  2967. if (ret)
  2968. goto bye;
  2969. }
  2970. /*
  2971. * Issue a Capability Configuration command to the firmware to get it
  2972. * to parse the Configuration File. We don't use t4_fw_config_file()
  2973. * because we want the ability to modify various features after we've
  2974. * processed the configuration file ...
  2975. */
  2976. memset(&caps_cmd, 0, sizeof(caps_cmd));
  2977. caps_cmd.op_to_write =
  2978. htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
  2979. FW_CMD_REQUEST_F |
  2980. FW_CMD_READ_F);
  2981. caps_cmd.cfvalid_to_len16 =
  2982. htonl(FW_CAPS_CONFIG_CMD_CFVALID_F |
  2983. FW_CAPS_CONFIG_CMD_MEMTYPE_CF_V(mtype) |
  2984. FW_CAPS_CONFIG_CMD_MEMADDR64K_CF_V(maddr >> 16) |
  2985. FW_LEN16(caps_cmd));
  2986. ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
  2987. &caps_cmd);
  2988. /* If the CAPS_CONFIG failed with an ENOENT (for a Firmware
  2989. * Configuration File in FLASH), our last gasp effort is to use the
  2990. * Firmware Configuration File which is embedded in the firmware. A
  2991. * very few early versions of the firmware didn't have one embedded
  2992. * but we can ignore those.
  2993. */
  2994. if (ret == -ENOENT) {
  2995. memset(&caps_cmd, 0, sizeof(caps_cmd));
  2996. caps_cmd.op_to_write =
  2997. htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
  2998. FW_CMD_REQUEST_F |
  2999. FW_CMD_READ_F);
  3000. caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
  3001. ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd,
  3002. sizeof(caps_cmd), &caps_cmd);
  3003. config_name = "Firmware Default";
  3004. }
  3005. config_issued = 1;
  3006. if (ret < 0)
  3007. goto bye;
  3008. finiver = ntohl(caps_cmd.finiver);
  3009. finicsum = ntohl(caps_cmd.finicsum);
  3010. cfcsum = ntohl(caps_cmd.cfcsum);
  3011. if (finicsum != cfcsum)
  3012. dev_warn(adapter->pdev_dev, "Configuration File checksum "\
  3013. "mismatch: [fini] csum=%#x, computed csum=%#x\n",
  3014. finicsum, cfcsum);
  3015. /*
  3016. * And now tell the firmware to use the configuration we just loaded.
  3017. */
  3018. caps_cmd.op_to_write =
  3019. htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
  3020. FW_CMD_REQUEST_F |
  3021. FW_CMD_WRITE_F);
  3022. caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
  3023. ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
  3024. NULL);
  3025. if (ret < 0)
  3026. goto bye;
  3027. /*
  3028. * Tweak configuration based on system architecture, module
  3029. * parameters, etc.
  3030. */
  3031. ret = adap_init0_tweaks(adapter);
  3032. if (ret < 0)
  3033. goto bye;
  3034. /*
  3035. * And finally tell the firmware to initialize itself using the
  3036. * parameters from the Configuration File.
  3037. */
  3038. ret = t4_fw_initialize(adapter, adapter->mbox);
  3039. if (ret < 0)
  3040. goto bye;
  3041. /* Emit Firmware Configuration File information and return
  3042. * successfully.
  3043. */
  3044. dev_info(adapter->pdev_dev, "Successfully configured using Firmware "\
  3045. "Configuration File \"%s\", version %#x, computed checksum %#x\n",
  3046. config_name, finiver, cfcsum);
  3047. return 0;
  3048. /*
  3049. * Something bad happened. Return the error ... (If the "error"
  3050. * is that there's no Configuration File on the adapter we don't
  3051. * want to issue a warning since this is fairly common.)
  3052. */
  3053. bye:
  3054. if (config_issued && ret != -ENOENT)
  3055. dev_warn(adapter->pdev_dev, "\"%s\" configuration file error %d\n",
  3056. config_name, -ret);
  3057. return ret;
  3058. }
  3059. static struct fw_info fw_info_array[] = {
  3060. {
  3061. .chip = CHELSIO_T4,
  3062. .fs_name = FW4_CFNAME,
  3063. .fw_mod_name = FW4_FNAME,
  3064. .fw_hdr = {
  3065. .chip = FW_HDR_CHIP_T4,
  3066. .fw_ver = __cpu_to_be32(FW_VERSION(T4)),
  3067. .intfver_nic = FW_INTFVER(T4, NIC),
  3068. .intfver_vnic = FW_INTFVER(T4, VNIC),
  3069. .intfver_ri = FW_INTFVER(T4, RI),
  3070. .intfver_iscsi = FW_INTFVER(T4, ISCSI),
  3071. .intfver_fcoe = FW_INTFVER(T4, FCOE),
  3072. },
  3073. }, {
  3074. .chip = CHELSIO_T5,
  3075. .fs_name = FW5_CFNAME,
  3076. .fw_mod_name = FW5_FNAME,
  3077. .fw_hdr = {
  3078. .chip = FW_HDR_CHIP_T5,
  3079. .fw_ver = __cpu_to_be32(FW_VERSION(T5)),
  3080. .intfver_nic = FW_INTFVER(T5, NIC),
  3081. .intfver_vnic = FW_INTFVER(T5, VNIC),
  3082. .intfver_ri = FW_INTFVER(T5, RI),
  3083. .intfver_iscsi = FW_INTFVER(T5, ISCSI),
  3084. .intfver_fcoe = FW_INTFVER(T5, FCOE),
  3085. },
  3086. }
  3087. };
  3088. static struct fw_info *find_fw_info(int chip)
  3089. {
  3090. int i;
  3091. for (i = 0; i < ARRAY_SIZE(fw_info_array); i++) {
  3092. if (fw_info_array[i].chip == chip)
  3093. return &fw_info_array[i];
  3094. }
  3095. return NULL;
  3096. }
  3097. /*
  3098. * Phase 0 of initialization: contact FW, obtain config, perform basic init.
  3099. */
  3100. static int adap_init0(struct adapter *adap)
  3101. {
  3102. int ret;
  3103. u32 v, port_vec;
  3104. enum dev_state state;
  3105. u32 params[7], val[7];
  3106. struct fw_caps_config_cmd caps_cmd;
  3107. int reset = 1;
  3108. /* Grab Firmware Device Log parameters as early as possible so we have
  3109. * access to it for debugging, etc.
  3110. */
  3111. ret = t4_init_devlog_params(adap);
  3112. if (ret < 0)
  3113. return ret;
  3114. /* Contact FW, advertising Master capability */
  3115. ret = t4_fw_hello(adap, adap->mbox, adap->mbox, MASTER_MAY, &state);
  3116. if (ret < 0) {
  3117. dev_err(adap->pdev_dev, "could not connect to FW, error %d\n",
  3118. ret);
  3119. return ret;
  3120. }
  3121. if (ret == adap->mbox)
  3122. adap->flags |= MASTER_PF;
  3123. /*
  3124. * If we're the Master PF Driver and the device is uninitialized,
  3125. * then let's consider upgrading the firmware ... (We always want
  3126. * to check the firmware version number in order to A. get it for
  3127. * later reporting and B. to warn if the currently loaded firmware
  3128. * is excessively mismatched relative to the driver.)
  3129. */
  3130. t4_get_fw_version(adap, &adap->params.fw_vers);
  3131. t4_get_tp_version(adap, &adap->params.tp_vers);
  3132. if ((adap->flags & MASTER_PF) && state != DEV_STATE_INIT) {
  3133. struct fw_info *fw_info;
  3134. struct fw_hdr *card_fw;
  3135. const struct firmware *fw;
  3136. const u8 *fw_data = NULL;
  3137. unsigned int fw_size = 0;
  3138. /* This is the firmware whose headers the driver was compiled
  3139. * against
  3140. */
  3141. fw_info = find_fw_info(CHELSIO_CHIP_VERSION(adap->params.chip));
  3142. if (fw_info == NULL) {
  3143. dev_err(adap->pdev_dev,
  3144. "unable to get firmware info for chip %d.\n",
  3145. CHELSIO_CHIP_VERSION(adap->params.chip));
  3146. return -EINVAL;
  3147. }
  3148. /* allocate memory to read the header of the firmware on the
  3149. * card
  3150. */
  3151. card_fw = t4_alloc_mem(sizeof(*card_fw));
  3152. /* Get FW from from /lib/firmware/ */
  3153. ret = request_firmware(&fw, fw_info->fw_mod_name,
  3154. adap->pdev_dev);
  3155. if (ret < 0) {
  3156. dev_err(adap->pdev_dev,
  3157. "unable to load firmware image %s, error %d\n",
  3158. fw_info->fw_mod_name, ret);
  3159. } else {
  3160. fw_data = fw->data;
  3161. fw_size = fw->size;
  3162. }
  3163. /* upgrade FW logic */
  3164. ret = t4_prep_fw(adap, fw_info, fw_data, fw_size, card_fw,
  3165. state, &reset);
  3166. /* Cleaning up */
  3167. release_firmware(fw);
  3168. t4_free_mem(card_fw);
  3169. if (ret < 0)
  3170. goto bye;
  3171. }
  3172. /*
  3173. * Grab VPD parameters. This should be done after we establish a
  3174. * connection to the firmware since some of the VPD parameters
  3175. * (notably the Core Clock frequency) are retrieved via requests to
  3176. * the firmware. On the other hand, we need these fairly early on
  3177. * so we do this right after getting ahold of the firmware.
  3178. */
  3179. ret = get_vpd_params(adap, &adap->params.vpd);
  3180. if (ret < 0)
  3181. goto bye;
  3182. /*
  3183. * Find out what ports are available to us. Note that we need to do
  3184. * this before calling adap_init0_no_config() since it needs nports
  3185. * and portvec ...
  3186. */
  3187. v =
  3188. FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
  3189. FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_PORTVEC);
  3190. ret = t4_query_params(adap, adap->mbox, adap->fn, 0, 1, &v, &port_vec);
  3191. if (ret < 0)
  3192. goto bye;
  3193. adap->params.nports = hweight32(port_vec);
  3194. adap->params.portvec = port_vec;
  3195. /* If the firmware is initialized already, emit a simply note to that
  3196. * effect. Otherwise, it's time to try initializing the adapter.
  3197. */
  3198. if (state == DEV_STATE_INIT) {
  3199. dev_info(adap->pdev_dev, "Coming up as %s: "\
  3200. "Adapter already initialized\n",
  3201. adap->flags & MASTER_PF ? "MASTER" : "SLAVE");
  3202. } else {
  3203. dev_info(adap->pdev_dev, "Coming up as MASTER: "\
  3204. "Initializing adapter\n");
  3205. /* Find out whether we're dealing with a version of the
  3206. * firmware which has configuration file support.
  3207. */
  3208. params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
  3209. FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
  3210. ret = t4_query_params(adap, adap->mbox, adap->fn, 0, 1,
  3211. params, val);
  3212. /* If the firmware doesn't support Configuration Files,
  3213. * return an error.
  3214. */
  3215. if (ret < 0) {
  3216. dev_err(adap->pdev_dev, "firmware doesn't support "
  3217. "Firmware Configuration Files\n");
  3218. goto bye;
  3219. }
  3220. /* The firmware provides us with a memory buffer where we can
  3221. * load a Configuration File from the host if we want to
  3222. * override the Configuration File in flash.
  3223. */
  3224. ret = adap_init0_config(adap, reset);
  3225. if (ret == -ENOENT) {
  3226. dev_err(adap->pdev_dev, "no Configuration File "
  3227. "present on adapter.\n");
  3228. goto bye;
  3229. }
  3230. if (ret < 0) {
  3231. dev_err(adap->pdev_dev, "could not initialize "
  3232. "adapter, error %d\n", -ret);
  3233. goto bye;
  3234. }
  3235. }
  3236. /* Give the SGE code a chance to pull in anything that it needs ...
  3237. * Note that this must be called after we retrieve our VPD parameters
  3238. * in order to know how to convert core ticks to seconds, etc.
  3239. */
  3240. ret = t4_sge_init(adap);
  3241. if (ret < 0)
  3242. goto bye;
  3243. if (is_bypass_device(adap->pdev->device))
  3244. adap->params.bypass = 1;
  3245. /*
  3246. * Grab some of our basic fundamental operating parameters.
  3247. */
  3248. #define FW_PARAM_DEV(param) \
  3249. (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) | \
  3250. FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_##param))
  3251. #define FW_PARAM_PFVF(param) \
  3252. FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) | \
  3253. FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_##param)| \
  3254. FW_PARAMS_PARAM_Y_V(0) | \
  3255. FW_PARAMS_PARAM_Z_V(0)
  3256. params[0] = FW_PARAM_PFVF(EQ_START);
  3257. params[1] = FW_PARAM_PFVF(L2T_START);
  3258. params[2] = FW_PARAM_PFVF(L2T_END);
  3259. params[3] = FW_PARAM_PFVF(FILTER_START);
  3260. params[4] = FW_PARAM_PFVF(FILTER_END);
  3261. params[5] = FW_PARAM_PFVF(IQFLINT_START);
  3262. ret = t4_query_params(adap, adap->mbox, adap->fn, 0, 6, params, val);
  3263. if (ret < 0)
  3264. goto bye;
  3265. adap->sge.egr_start = val[0];
  3266. adap->l2t_start = val[1];
  3267. adap->l2t_end = val[2];
  3268. adap->tids.ftid_base = val[3];
  3269. adap->tids.nftids = val[4] - val[3] + 1;
  3270. adap->sge.ingr_start = val[5];
  3271. /* qids (ingress/egress) returned from firmware can be anywhere
  3272. * in the range from EQ(IQFLINT)_START to EQ(IQFLINT)_END.
  3273. * Hence driver needs to allocate memory for this range to
  3274. * store the queue info. Get the highest IQFLINT/EQ index returned
  3275. * in FW_EQ_*_CMD.alloc command.
  3276. */
  3277. params[0] = FW_PARAM_PFVF(EQ_END);
  3278. params[1] = FW_PARAM_PFVF(IQFLINT_END);
  3279. ret = t4_query_params(adap, adap->mbox, adap->fn, 0, 2, params, val);
  3280. if (ret < 0)
  3281. goto bye;
  3282. adap->sge.egr_sz = val[0] - adap->sge.egr_start + 1;
  3283. adap->sge.ingr_sz = val[1] - adap->sge.ingr_start + 1;
  3284. adap->sge.egr_map = kcalloc(adap->sge.egr_sz,
  3285. sizeof(*adap->sge.egr_map), GFP_KERNEL);
  3286. if (!adap->sge.egr_map) {
  3287. ret = -ENOMEM;
  3288. goto bye;
  3289. }
  3290. adap->sge.ingr_map = kcalloc(adap->sge.ingr_sz,
  3291. sizeof(*adap->sge.ingr_map), GFP_KERNEL);
  3292. if (!adap->sge.ingr_map) {
  3293. ret = -ENOMEM;
  3294. goto bye;
  3295. }
  3296. /* Allocate the memory for the vaious egress queue bitmaps
  3297. * ie starving_fl and txq_maperr.
  3298. */
  3299. adap->sge.starving_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
  3300. sizeof(long), GFP_KERNEL);
  3301. if (!adap->sge.starving_fl) {
  3302. ret = -ENOMEM;
  3303. goto bye;
  3304. }
  3305. adap->sge.txq_maperr = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
  3306. sizeof(long), GFP_KERNEL);
  3307. if (!adap->sge.txq_maperr) {
  3308. ret = -ENOMEM;
  3309. goto bye;
  3310. }
  3311. params[0] = FW_PARAM_PFVF(CLIP_START);
  3312. params[1] = FW_PARAM_PFVF(CLIP_END);
  3313. ret = t4_query_params(adap, adap->mbox, adap->fn, 0, 2, params, val);
  3314. if (ret < 0)
  3315. goto bye;
  3316. adap->clipt_start = val[0];
  3317. adap->clipt_end = val[1];
  3318. /* query params related to active filter region */
  3319. params[0] = FW_PARAM_PFVF(ACTIVE_FILTER_START);
  3320. params[1] = FW_PARAM_PFVF(ACTIVE_FILTER_END);
  3321. ret = t4_query_params(adap, adap->mbox, adap->fn, 0, 2, params, val);
  3322. /* If Active filter size is set we enable establishing
  3323. * offload connection through firmware work request
  3324. */
  3325. if ((val[0] != val[1]) && (ret >= 0)) {
  3326. adap->flags |= FW_OFLD_CONN;
  3327. adap->tids.aftid_base = val[0];
  3328. adap->tids.aftid_end = val[1];
  3329. }
  3330. /* If we're running on newer firmware, let it know that we're
  3331. * prepared to deal with encapsulated CPL messages. Older
  3332. * firmware won't understand this and we'll just get
  3333. * unencapsulated messages ...
  3334. */
  3335. params[0] = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
  3336. val[0] = 1;
  3337. (void) t4_set_params(adap, adap->mbox, adap->fn, 0, 1, params, val);
  3338. /*
  3339. * Find out whether we're allowed to use the T5+ ULPTX MEMWRITE DSGL
  3340. * capability. Earlier versions of the firmware didn't have the
  3341. * ULPTX_MEMWRITE_DSGL so we'll interpret a query failure as no
  3342. * permission to use ULPTX MEMWRITE DSGL.
  3343. */
  3344. if (is_t4(adap->params.chip)) {
  3345. adap->params.ulptx_memwrite_dsgl = false;
  3346. } else {
  3347. params[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
  3348. ret = t4_query_params(adap, adap->mbox, adap->fn, 0,
  3349. 1, params, val);
  3350. adap->params.ulptx_memwrite_dsgl = (ret == 0 && val[0] != 0);
  3351. }
  3352. /*
  3353. * Get device capabilities so we can determine what resources we need
  3354. * to manage.
  3355. */
  3356. memset(&caps_cmd, 0, sizeof(caps_cmd));
  3357. caps_cmd.op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
  3358. FW_CMD_REQUEST_F | FW_CMD_READ_F);
  3359. caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
  3360. ret = t4_wr_mbox(adap, adap->mbox, &caps_cmd, sizeof(caps_cmd),
  3361. &caps_cmd);
  3362. if (ret < 0)
  3363. goto bye;
  3364. if (caps_cmd.ofldcaps) {
  3365. /* query offload-related parameters */
  3366. params[0] = FW_PARAM_DEV(NTID);
  3367. params[1] = FW_PARAM_PFVF(SERVER_START);
  3368. params[2] = FW_PARAM_PFVF(SERVER_END);
  3369. params[3] = FW_PARAM_PFVF(TDDP_START);
  3370. params[4] = FW_PARAM_PFVF(TDDP_END);
  3371. params[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
  3372. ret = t4_query_params(adap, adap->mbox, adap->fn, 0, 6,
  3373. params, val);
  3374. if (ret < 0)
  3375. goto bye;
  3376. adap->tids.ntids = val[0];
  3377. adap->tids.natids = min(adap->tids.ntids / 2, MAX_ATIDS);
  3378. adap->tids.stid_base = val[1];
  3379. adap->tids.nstids = val[2] - val[1] + 1;
  3380. /*
  3381. * Setup server filter region. Divide the available filter
  3382. * region into two parts. Regular filters get 1/3rd and server
  3383. * filters get 2/3rd part. This is only enabled if workarond
  3384. * path is enabled.
  3385. * 1. For regular filters.
  3386. * 2. Server filter: This are special filters which are used
  3387. * to redirect SYN packets to offload queue.
  3388. */
  3389. if (adap->flags & FW_OFLD_CONN && !is_bypass(adap)) {
  3390. adap->tids.sftid_base = adap->tids.ftid_base +
  3391. DIV_ROUND_UP(adap->tids.nftids, 3);
  3392. adap->tids.nsftids = adap->tids.nftids -
  3393. DIV_ROUND_UP(adap->tids.nftids, 3);
  3394. adap->tids.nftids = adap->tids.sftid_base -
  3395. adap->tids.ftid_base;
  3396. }
  3397. adap->vres.ddp.start = val[3];
  3398. adap->vres.ddp.size = val[4] - val[3] + 1;
  3399. adap->params.ofldq_wr_cred = val[5];
  3400. adap->params.offload = 1;
  3401. }
  3402. if (caps_cmd.rdmacaps) {
  3403. params[0] = FW_PARAM_PFVF(STAG_START);
  3404. params[1] = FW_PARAM_PFVF(STAG_END);
  3405. params[2] = FW_PARAM_PFVF(RQ_START);
  3406. params[3] = FW_PARAM_PFVF(RQ_END);
  3407. params[4] = FW_PARAM_PFVF(PBL_START);
  3408. params[5] = FW_PARAM_PFVF(PBL_END);
  3409. ret = t4_query_params(adap, adap->mbox, adap->fn, 0, 6,
  3410. params, val);
  3411. if (ret < 0)
  3412. goto bye;
  3413. adap->vres.stag.start = val[0];
  3414. adap->vres.stag.size = val[1] - val[0] + 1;
  3415. adap->vres.rq.start = val[2];
  3416. adap->vres.rq.size = val[3] - val[2] + 1;
  3417. adap->vres.pbl.start = val[4];
  3418. adap->vres.pbl.size = val[5] - val[4] + 1;
  3419. params[0] = FW_PARAM_PFVF(SQRQ_START);
  3420. params[1] = FW_PARAM_PFVF(SQRQ_END);
  3421. params[2] = FW_PARAM_PFVF(CQ_START);
  3422. params[3] = FW_PARAM_PFVF(CQ_END);
  3423. params[4] = FW_PARAM_PFVF(OCQ_START);
  3424. params[5] = FW_PARAM_PFVF(OCQ_END);
  3425. ret = t4_query_params(adap, adap->mbox, adap->fn, 0, 6, params,
  3426. val);
  3427. if (ret < 0)
  3428. goto bye;
  3429. adap->vres.qp.start = val[0];
  3430. adap->vres.qp.size = val[1] - val[0] + 1;
  3431. adap->vres.cq.start = val[2];
  3432. adap->vres.cq.size = val[3] - val[2] + 1;
  3433. adap->vres.ocq.start = val[4];
  3434. adap->vres.ocq.size = val[5] - val[4] + 1;
  3435. params[0] = FW_PARAM_DEV(MAXORDIRD_QP);
  3436. params[1] = FW_PARAM_DEV(MAXIRD_ADAPTER);
  3437. ret = t4_query_params(adap, adap->mbox, adap->fn, 0, 2, params,
  3438. val);
  3439. if (ret < 0) {
  3440. adap->params.max_ordird_qp = 8;
  3441. adap->params.max_ird_adapter = 32 * adap->tids.ntids;
  3442. ret = 0;
  3443. } else {
  3444. adap->params.max_ordird_qp = val[0];
  3445. adap->params.max_ird_adapter = val[1];
  3446. }
  3447. dev_info(adap->pdev_dev,
  3448. "max_ordird_qp %d max_ird_adapter %d\n",
  3449. adap->params.max_ordird_qp,
  3450. adap->params.max_ird_adapter);
  3451. }
  3452. if (caps_cmd.iscsicaps) {
  3453. params[0] = FW_PARAM_PFVF(ISCSI_START);
  3454. params[1] = FW_PARAM_PFVF(ISCSI_END);
  3455. ret = t4_query_params(adap, adap->mbox, adap->fn, 0, 2,
  3456. params, val);
  3457. if (ret < 0)
  3458. goto bye;
  3459. adap->vres.iscsi.start = val[0];
  3460. adap->vres.iscsi.size = val[1] - val[0] + 1;
  3461. }
  3462. #undef FW_PARAM_PFVF
  3463. #undef FW_PARAM_DEV
  3464. /* The MTU/MSS Table is initialized by now, so load their values. If
  3465. * we're initializing the adapter, then we'll make any modifications
  3466. * we want to the MTU/MSS Table and also initialize the congestion
  3467. * parameters.
  3468. */
  3469. t4_read_mtu_tbl(adap, adap->params.mtus, NULL);
  3470. if (state != DEV_STATE_INIT) {
  3471. int i;
  3472. /* The default MTU Table contains values 1492 and 1500.
  3473. * However, for TCP, it's better to have two values which are
  3474. * a multiple of 8 +/- 4 bytes apart near this popular MTU.
  3475. * This allows us to have a TCP Data Payload which is a
  3476. * multiple of 8 regardless of what combination of TCP Options
  3477. * are in use (always a multiple of 4 bytes) which is
  3478. * important for performance reasons. For instance, if no
  3479. * options are in use, then we have a 20-byte IP header and a
  3480. * 20-byte TCP header. In this case, a 1500-byte MSS would
  3481. * result in a TCP Data Payload of 1500 - 40 == 1460 bytes
  3482. * which is not a multiple of 8. So using an MSS of 1488 in
  3483. * this case results in a TCP Data Payload of 1448 bytes which
  3484. * is a multiple of 8. On the other hand, if 12-byte TCP Time
  3485. * Stamps have been negotiated, then an MTU of 1500 bytes
  3486. * results in a TCP Data Payload of 1448 bytes which, as
  3487. * above, is a multiple of 8 bytes ...
  3488. */
  3489. for (i = 0; i < NMTUS; i++)
  3490. if (adap->params.mtus[i] == 1492) {
  3491. adap->params.mtus[i] = 1488;
  3492. break;
  3493. }
  3494. t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
  3495. adap->params.b_wnd);
  3496. }
  3497. t4_init_sge_params(adap);
  3498. t4_init_tp_params(adap);
  3499. adap->flags |= FW_OK;
  3500. return 0;
  3501. /*
  3502. * Something bad happened. If a command timed out or failed with EIO
  3503. * FW does not operate within its spec or something catastrophic
  3504. * happened to HW/FW, stop issuing commands.
  3505. */
  3506. bye:
  3507. kfree(adap->sge.egr_map);
  3508. kfree(adap->sge.ingr_map);
  3509. kfree(adap->sge.starving_fl);
  3510. kfree(adap->sge.txq_maperr);
  3511. if (ret != -ETIMEDOUT && ret != -EIO)
  3512. t4_fw_bye(adap, adap->mbox);
  3513. return ret;
  3514. }
  3515. /* EEH callbacks */
  3516. static pci_ers_result_t eeh_err_detected(struct pci_dev *pdev,
  3517. pci_channel_state_t state)
  3518. {
  3519. int i;
  3520. struct adapter *adap = pci_get_drvdata(pdev);
  3521. if (!adap)
  3522. goto out;
  3523. rtnl_lock();
  3524. adap->flags &= ~FW_OK;
  3525. notify_ulds(adap, CXGB4_STATE_START_RECOVERY);
  3526. spin_lock(&adap->stats_lock);
  3527. for_each_port(adap, i) {
  3528. struct net_device *dev = adap->port[i];
  3529. netif_device_detach(dev);
  3530. netif_carrier_off(dev);
  3531. }
  3532. spin_unlock(&adap->stats_lock);
  3533. disable_interrupts(adap);
  3534. if (adap->flags & FULL_INIT_DONE)
  3535. cxgb_down(adap);
  3536. rtnl_unlock();
  3537. if ((adap->flags & DEV_ENABLED)) {
  3538. pci_disable_device(pdev);
  3539. adap->flags &= ~DEV_ENABLED;
  3540. }
  3541. out: return state == pci_channel_io_perm_failure ?
  3542. PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
  3543. }
  3544. static pci_ers_result_t eeh_slot_reset(struct pci_dev *pdev)
  3545. {
  3546. int i, ret;
  3547. struct fw_caps_config_cmd c;
  3548. struct adapter *adap = pci_get_drvdata(pdev);
  3549. if (!adap) {
  3550. pci_restore_state(pdev);
  3551. pci_save_state(pdev);
  3552. return PCI_ERS_RESULT_RECOVERED;
  3553. }
  3554. if (!(adap->flags & DEV_ENABLED)) {
  3555. if (pci_enable_device(pdev)) {
  3556. dev_err(&pdev->dev, "Cannot reenable PCI "
  3557. "device after reset\n");
  3558. return PCI_ERS_RESULT_DISCONNECT;
  3559. }
  3560. adap->flags |= DEV_ENABLED;
  3561. }
  3562. pci_set_master(pdev);
  3563. pci_restore_state(pdev);
  3564. pci_save_state(pdev);
  3565. pci_cleanup_aer_uncorrect_error_status(pdev);
  3566. if (t4_wait_dev_ready(adap->regs) < 0)
  3567. return PCI_ERS_RESULT_DISCONNECT;
  3568. if (t4_fw_hello(adap, adap->fn, adap->fn, MASTER_MUST, NULL) < 0)
  3569. return PCI_ERS_RESULT_DISCONNECT;
  3570. adap->flags |= FW_OK;
  3571. if (adap_init1(adap, &c))
  3572. return PCI_ERS_RESULT_DISCONNECT;
  3573. for_each_port(adap, i) {
  3574. struct port_info *p = adap2pinfo(adap, i);
  3575. ret = t4_alloc_vi(adap, adap->fn, p->tx_chan, adap->fn, 0, 1,
  3576. NULL, NULL);
  3577. if (ret < 0)
  3578. return PCI_ERS_RESULT_DISCONNECT;
  3579. p->viid = ret;
  3580. p->xact_addr_filt = -1;
  3581. }
  3582. t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
  3583. adap->params.b_wnd);
  3584. setup_memwin(adap);
  3585. if (cxgb_up(adap))
  3586. return PCI_ERS_RESULT_DISCONNECT;
  3587. return PCI_ERS_RESULT_RECOVERED;
  3588. }
  3589. static void eeh_resume(struct pci_dev *pdev)
  3590. {
  3591. int i;
  3592. struct adapter *adap = pci_get_drvdata(pdev);
  3593. if (!adap)
  3594. return;
  3595. rtnl_lock();
  3596. for_each_port(adap, i) {
  3597. struct net_device *dev = adap->port[i];
  3598. if (netif_running(dev)) {
  3599. link_start(dev);
  3600. cxgb_set_rxmode(dev);
  3601. }
  3602. netif_device_attach(dev);
  3603. }
  3604. rtnl_unlock();
  3605. }
  3606. static const struct pci_error_handlers cxgb4_eeh = {
  3607. .error_detected = eeh_err_detected,
  3608. .slot_reset = eeh_slot_reset,
  3609. .resume = eeh_resume,
  3610. };
  3611. static inline bool is_x_10g_port(const struct link_config *lc)
  3612. {
  3613. return (lc->supported & FW_PORT_CAP_SPEED_10G) != 0 ||
  3614. (lc->supported & FW_PORT_CAP_SPEED_40G) != 0;
  3615. }
  3616. static inline void init_rspq(struct adapter *adap, struct sge_rspq *q,
  3617. unsigned int us, unsigned int cnt,
  3618. unsigned int size, unsigned int iqe_size)
  3619. {
  3620. q->adap = adap;
  3621. cxgb4_set_rspq_intr_params(q, us, cnt);
  3622. q->iqe_len = iqe_size;
  3623. q->size = size;
  3624. }
  3625. /*
  3626. * Perform default configuration of DMA queues depending on the number and type
  3627. * of ports we found and the number of available CPUs. Most settings can be
  3628. * modified by the admin prior to actual use.
  3629. */
  3630. static void cfg_queues(struct adapter *adap)
  3631. {
  3632. struct sge *s = &adap->sge;
  3633. int i, n10g = 0, qidx = 0;
  3634. #ifndef CONFIG_CHELSIO_T4_DCB
  3635. int q10g = 0;
  3636. #endif
  3637. int ciq_size;
  3638. for_each_port(adap, i)
  3639. n10g += is_x_10g_port(&adap2pinfo(adap, i)->link_cfg);
  3640. #ifdef CONFIG_CHELSIO_T4_DCB
  3641. /* For Data Center Bridging support we need to be able to support up
  3642. * to 8 Traffic Priorities; each of which will be assigned to its
  3643. * own TX Queue in order to prevent Head-Of-Line Blocking.
  3644. */
  3645. if (adap->params.nports * 8 > MAX_ETH_QSETS) {
  3646. dev_err(adap->pdev_dev, "MAX_ETH_QSETS=%d < %d!\n",
  3647. MAX_ETH_QSETS, adap->params.nports * 8);
  3648. BUG_ON(1);
  3649. }
  3650. for_each_port(adap, i) {
  3651. struct port_info *pi = adap2pinfo(adap, i);
  3652. pi->first_qset = qidx;
  3653. pi->nqsets = 8;
  3654. qidx += pi->nqsets;
  3655. }
  3656. #else /* !CONFIG_CHELSIO_T4_DCB */
  3657. /*
  3658. * We default to 1 queue per non-10G port and up to # of cores queues
  3659. * per 10G port.
  3660. */
  3661. if (n10g)
  3662. q10g = (MAX_ETH_QSETS - (adap->params.nports - n10g)) / n10g;
  3663. if (q10g > netif_get_num_default_rss_queues())
  3664. q10g = netif_get_num_default_rss_queues();
  3665. for_each_port(adap, i) {
  3666. struct port_info *pi = adap2pinfo(adap, i);
  3667. pi->first_qset = qidx;
  3668. pi->nqsets = is_x_10g_port(&pi->link_cfg) ? q10g : 1;
  3669. qidx += pi->nqsets;
  3670. }
  3671. #endif /* !CONFIG_CHELSIO_T4_DCB */
  3672. s->ethqsets = qidx;
  3673. s->max_ethqsets = qidx; /* MSI-X may lower it later */
  3674. if (is_offload(adap)) {
  3675. /*
  3676. * For offload we use 1 queue/channel if all ports are up to 1G,
  3677. * otherwise we divide all available queues amongst the channels
  3678. * capped by the number of available cores.
  3679. */
  3680. if (n10g) {
  3681. i = min_t(int, ARRAY_SIZE(s->ofldrxq),
  3682. num_online_cpus());
  3683. s->ofldqsets = roundup(i, adap->params.nports);
  3684. } else
  3685. s->ofldqsets = adap->params.nports;
  3686. /* For RDMA one Rx queue per channel suffices */
  3687. s->rdmaqs = adap->params.nports;
  3688. /* Try and allow at least 1 CIQ per cpu rounding down
  3689. * to the number of ports, with a minimum of 1 per port.
  3690. * A 2 port card in a 6 cpu system: 6 CIQs, 3 / port.
  3691. * A 4 port card in a 6 cpu system: 4 CIQs, 1 / port.
  3692. * A 4 port card in a 2 cpu system: 4 CIQs, 1 / port.
  3693. */
  3694. s->rdmaciqs = min_t(int, MAX_RDMA_CIQS, num_online_cpus());
  3695. s->rdmaciqs = (s->rdmaciqs / adap->params.nports) *
  3696. adap->params.nports;
  3697. s->rdmaciqs = max_t(int, s->rdmaciqs, adap->params.nports);
  3698. }
  3699. for (i = 0; i < ARRAY_SIZE(s->ethrxq); i++) {
  3700. struct sge_eth_rxq *r = &s->ethrxq[i];
  3701. init_rspq(adap, &r->rspq, 5, 10, 1024, 64);
  3702. r->fl.size = 72;
  3703. }
  3704. for (i = 0; i < ARRAY_SIZE(s->ethtxq); i++)
  3705. s->ethtxq[i].q.size = 1024;
  3706. for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++)
  3707. s->ctrlq[i].q.size = 512;
  3708. for (i = 0; i < ARRAY_SIZE(s->ofldtxq); i++)
  3709. s->ofldtxq[i].q.size = 1024;
  3710. for (i = 0; i < ARRAY_SIZE(s->ofldrxq); i++) {
  3711. struct sge_ofld_rxq *r = &s->ofldrxq[i];
  3712. init_rspq(adap, &r->rspq, 5, 1, 1024, 64);
  3713. r->rspq.uld = CXGB4_ULD_ISCSI;
  3714. r->fl.size = 72;
  3715. }
  3716. for (i = 0; i < ARRAY_SIZE(s->rdmarxq); i++) {
  3717. struct sge_ofld_rxq *r = &s->rdmarxq[i];
  3718. init_rspq(adap, &r->rspq, 5, 1, 511, 64);
  3719. r->rspq.uld = CXGB4_ULD_RDMA;
  3720. r->fl.size = 72;
  3721. }
  3722. ciq_size = 64 + adap->vres.cq.size + adap->tids.nftids;
  3723. if (ciq_size > SGE_MAX_IQ_SIZE) {
  3724. CH_WARN(adap, "CIQ size too small for available IQs\n");
  3725. ciq_size = SGE_MAX_IQ_SIZE;
  3726. }
  3727. for (i = 0; i < ARRAY_SIZE(s->rdmaciq); i++) {
  3728. struct sge_ofld_rxq *r = &s->rdmaciq[i];
  3729. init_rspq(adap, &r->rspq, 5, 1, ciq_size, 64);
  3730. r->rspq.uld = CXGB4_ULD_RDMA;
  3731. }
  3732. init_rspq(adap, &s->fw_evtq, 0, 1, 1024, 64);
  3733. init_rspq(adap, &s->intrq, 0, 1, 2 * MAX_INGQ, 64);
  3734. }
  3735. /*
  3736. * Reduce the number of Ethernet queues across all ports to at most n.
  3737. * n provides at least one queue per port.
  3738. */
  3739. static void reduce_ethqs(struct adapter *adap, int n)
  3740. {
  3741. int i;
  3742. struct port_info *pi;
  3743. while (n < adap->sge.ethqsets)
  3744. for_each_port(adap, i) {
  3745. pi = adap2pinfo(adap, i);
  3746. if (pi->nqsets > 1) {
  3747. pi->nqsets--;
  3748. adap->sge.ethqsets--;
  3749. if (adap->sge.ethqsets <= n)
  3750. break;
  3751. }
  3752. }
  3753. n = 0;
  3754. for_each_port(adap, i) {
  3755. pi = adap2pinfo(adap, i);
  3756. pi->first_qset = n;
  3757. n += pi->nqsets;
  3758. }
  3759. }
  3760. /* 2 MSI-X vectors needed for the FW queue and non-data interrupts */
  3761. #define EXTRA_VECS 2
  3762. static int enable_msix(struct adapter *adap)
  3763. {
  3764. int ofld_need = 0;
  3765. int i, want, need, allocated;
  3766. struct sge *s = &adap->sge;
  3767. unsigned int nchan = adap->params.nports;
  3768. struct msix_entry *entries;
  3769. entries = kmalloc(sizeof(*entries) * (MAX_INGQ + 1),
  3770. GFP_KERNEL);
  3771. if (!entries)
  3772. return -ENOMEM;
  3773. for (i = 0; i < MAX_INGQ + 1; ++i)
  3774. entries[i].entry = i;
  3775. want = s->max_ethqsets + EXTRA_VECS;
  3776. if (is_offload(adap)) {
  3777. want += s->rdmaqs + s->rdmaciqs + s->ofldqsets;
  3778. /* need nchan for each possible ULD */
  3779. ofld_need = 3 * nchan;
  3780. }
  3781. #ifdef CONFIG_CHELSIO_T4_DCB
  3782. /* For Data Center Bridging we need 8 Ethernet TX Priority Queues for
  3783. * each port.
  3784. */
  3785. need = 8 * adap->params.nports + EXTRA_VECS + ofld_need;
  3786. #else
  3787. need = adap->params.nports + EXTRA_VECS + ofld_need;
  3788. #endif
  3789. allocated = pci_enable_msix_range(adap->pdev, entries, need, want);
  3790. if (allocated < 0) {
  3791. dev_info(adap->pdev_dev, "not enough MSI-X vectors left,"
  3792. " not using MSI-X\n");
  3793. kfree(entries);
  3794. return allocated;
  3795. }
  3796. /* Distribute available vectors to the various queue groups.
  3797. * Every group gets its minimum requirement and NIC gets top
  3798. * priority for leftovers.
  3799. */
  3800. i = allocated - EXTRA_VECS - ofld_need;
  3801. if (i < s->max_ethqsets) {
  3802. s->max_ethqsets = i;
  3803. if (i < s->ethqsets)
  3804. reduce_ethqs(adap, i);
  3805. }
  3806. if (is_offload(adap)) {
  3807. if (allocated < want) {
  3808. s->rdmaqs = nchan;
  3809. s->rdmaciqs = nchan;
  3810. }
  3811. /* leftovers go to OFLD */
  3812. i = allocated - EXTRA_VECS - s->max_ethqsets -
  3813. s->rdmaqs - s->rdmaciqs;
  3814. s->ofldqsets = (i / nchan) * nchan; /* round down */
  3815. }
  3816. for (i = 0; i < allocated; ++i)
  3817. adap->msix_info[i].vec = entries[i].vector;
  3818. kfree(entries);
  3819. return 0;
  3820. }
  3821. #undef EXTRA_VECS
  3822. static int init_rss(struct adapter *adap)
  3823. {
  3824. unsigned int i, j;
  3825. for_each_port(adap, i) {
  3826. struct port_info *pi = adap2pinfo(adap, i);
  3827. pi->rss = kcalloc(pi->rss_size, sizeof(u16), GFP_KERNEL);
  3828. if (!pi->rss)
  3829. return -ENOMEM;
  3830. for (j = 0; j < pi->rss_size; j++)
  3831. pi->rss[j] = ethtool_rxfh_indir_default(j, pi->nqsets);
  3832. }
  3833. return 0;
  3834. }
  3835. static void print_port_info(const struct net_device *dev)
  3836. {
  3837. char buf[80];
  3838. char *bufp = buf;
  3839. const char *spd = "";
  3840. const struct port_info *pi = netdev_priv(dev);
  3841. const struct adapter *adap = pi->adapter;
  3842. if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_2_5GB)
  3843. spd = " 2.5 GT/s";
  3844. else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_5_0GB)
  3845. spd = " 5 GT/s";
  3846. else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_8_0GB)
  3847. spd = " 8 GT/s";
  3848. if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_100M)
  3849. bufp += sprintf(bufp, "100/");
  3850. if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_1G)
  3851. bufp += sprintf(bufp, "1000/");
  3852. if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_10G)
  3853. bufp += sprintf(bufp, "10G/");
  3854. if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_40G)
  3855. bufp += sprintf(bufp, "40G/");
  3856. if (bufp != buf)
  3857. --bufp;
  3858. sprintf(bufp, "BASE-%s", t4_get_port_type_description(pi->port_type));
  3859. netdev_info(dev, "Chelsio %s rev %d %s %sNIC PCIe x%d%s%s\n",
  3860. adap->params.vpd.id,
  3861. CHELSIO_CHIP_RELEASE(adap->params.chip), buf,
  3862. is_offload(adap) ? "R" : "", adap->params.pci.width, spd,
  3863. (adap->flags & USING_MSIX) ? " MSI-X" :
  3864. (adap->flags & USING_MSI) ? " MSI" : "");
  3865. netdev_info(dev, "S/N: %s, P/N: %s\n",
  3866. adap->params.vpd.sn, adap->params.vpd.pn);
  3867. }
  3868. static void enable_pcie_relaxed_ordering(struct pci_dev *dev)
  3869. {
  3870. pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
  3871. }
  3872. /*
  3873. * Free the following resources:
  3874. * - memory used for tables
  3875. * - MSI/MSI-X
  3876. * - net devices
  3877. * - resources FW is holding for us
  3878. */
  3879. static void free_some_resources(struct adapter *adapter)
  3880. {
  3881. unsigned int i;
  3882. t4_free_mem(adapter->l2t);
  3883. t4_free_mem(adapter->tids.tid_tab);
  3884. kfree(adapter->sge.egr_map);
  3885. kfree(adapter->sge.ingr_map);
  3886. kfree(adapter->sge.starving_fl);
  3887. kfree(adapter->sge.txq_maperr);
  3888. disable_msi(adapter);
  3889. for_each_port(adapter, i)
  3890. if (adapter->port[i]) {
  3891. kfree(adap2pinfo(adapter, i)->rss);
  3892. free_netdev(adapter->port[i]);
  3893. }
  3894. if (adapter->flags & FW_OK)
  3895. t4_fw_bye(adapter, adapter->fn);
  3896. }
  3897. #define TSO_FLAGS (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN)
  3898. #define VLAN_FEAT (NETIF_F_SG | NETIF_F_IP_CSUM | TSO_FLAGS | \
  3899. NETIF_F_IPV6_CSUM | NETIF_F_HIGHDMA)
  3900. #define SEGMENT_SIZE 128
  3901. static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  3902. {
  3903. int func, i, err, s_qpp, qpp, num_seg;
  3904. struct port_info *pi;
  3905. bool highdma = false;
  3906. struct adapter *adapter = NULL;
  3907. void __iomem *regs;
  3908. printk_once(KERN_INFO "%s - version %s\n", DRV_DESC, DRV_VERSION);
  3909. err = pci_request_regions(pdev, KBUILD_MODNAME);
  3910. if (err) {
  3911. /* Just info, some other driver may have claimed the device. */
  3912. dev_info(&pdev->dev, "cannot obtain PCI resources\n");
  3913. return err;
  3914. }
  3915. err = pci_enable_device(pdev);
  3916. if (err) {
  3917. dev_err(&pdev->dev, "cannot enable PCI device\n");
  3918. goto out_release_regions;
  3919. }
  3920. regs = pci_ioremap_bar(pdev, 0);
  3921. if (!regs) {
  3922. dev_err(&pdev->dev, "cannot map device registers\n");
  3923. err = -ENOMEM;
  3924. goto out_disable_device;
  3925. }
  3926. err = t4_wait_dev_ready(regs);
  3927. if (err < 0)
  3928. goto out_unmap_bar0;
  3929. /* We control everything through one PF */
  3930. func = SOURCEPF_G(readl(regs + PL_WHOAMI_A));
  3931. if (func != ent->driver_data) {
  3932. iounmap(regs);
  3933. pci_disable_device(pdev);
  3934. pci_save_state(pdev); /* to restore SR-IOV later */
  3935. goto sriov;
  3936. }
  3937. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
  3938. highdma = true;
  3939. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  3940. if (err) {
  3941. dev_err(&pdev->dev, "unable to obtain 64-bit DMA for "
  3942. "coherent allocations\n");
  3943. goto out_unmap_bar0;
  3944. }
  3945. } else {
  3946. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  3947. if (err) {
  3948. dev_err(&pdev->dev, "no usable DMA configuration\n");
  3949. goto out_unmap_bar0;
  3950. }
  3951. }
  3952. pci_enable_pcie_error_reporting(pdev);
  3953. enable_pcie_relaxed_ordering(pdev);
  3954. pci_set_master(pdev);
  3955. pci_save_state(pdev);
  3956. adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
  3957. if (!adapter) {
  3958. err = -ENOMEM;
  3959. goto out_unmap_bar0;
  3960. }
  3961. adapter->workq = create_singlethread_workqueue("cxgb4");
  3962. if (!adapter->workq) {
  3963. err = -ENOMEM;
  3964. goto out_free_adapter;
  3965. }
  3966. /* PCI device has been enabled */
  3967. adapter->flags |= DEV_ENABLED;
  3968. adapter->regs = regs;
  3969. adapter->pdev = pdev;
  3970. adapter->pdev_dev = &pdev->dev;
  3971. adapter->mbox = func;
  3972. adapter->fn = func;
  3973. adapter->msg_enable = dflt_msg_enable;
  3974. memset(adapter->chan_map, 0xff, sizeof(adapter->chan_map));
  3975. spin_lock_init(&adapter->stats_lock);
  3976. spin_lock_init(&adapter->tid_release_lock);
  3977. spin_lock_init(&adapter->win0_lock);
  3978. INIT_WORK(&adapter->tid_release_task, process_tid_release_list);
  3979. INIT_WORK(&adapter->db_full_task, process_db_full);
  3980. INIT_WORK(&adapter->db_drop_task, process_db_drop);
  3981. err = t4_prep_adapter(adapter);
  3982. if (err)
  3983. goto out_free_adapter;
  3984. if (!is_t4(adapter->params.chip)) {
  3985. s_qpp = (QUEUESPERPAGEPF0_S +
  3986. (QUEUESPERPAGEPF1_S - QUEUESPERPAGEPF0_S) *
  3987. adapter->fn);
  3988. qpp = 1 << QUEUESPERPAGEPF0_G(t4_read_reg(adapter,
  3989. SGE_EGRESS_QUEUES_PER_PAGE_PF_A) >> s_qpp);
  3990. num_seg = PAGE_SIZE / SEGMENT_SIZE;
  3991. /* Each segment size is 128B. Write coalescing is enabled only
  3992. * when SGE_EGRESS_QUEUES_PER_PAGE_PF reg value for the
  3993. * queue is less no of segments that can be accommodated in
  3994. * a page size.
  3995. */
  3996. if (qpp > num_seg) {
  3997. dev_err(&pdev->dev,
  3998. "Incorrect number of egress queues per page\n");
  3999. err = -EINVAL;
  4000. goto out_free_adapter;
  4001. }
  4002. adapter->bar2 = ioremap_wc(pci_resource_start(pdev, 2),
  4003. pci_resource_len(pdev, 2));
  4004. if (!adapter->bar2) {
  4005. dev_err(&pdev->dev, "cannot map device bar2 region\n");
  4006. err = -ENOMEM;
  4007. goto out_free_adapter;
  4008. }
  4009. }
  4010. setup_memwin(adapter);
  4011. err = adap_init0(adapter);
  4012. setup_memwin_rdma(adapter);
  4013. if (err)
  4014. goto out_unmap_bar;
  4015. for_each_port(adapter, i) {
  4016. struct net_device *netdev;
  4017. netdev = alloc_etherdev_mq(sizeof(struct port_info),
  4018. MAX_ETH_QSETS);
  4019. if (!netdev) {
  4020. err = -ENOMEM;
  4021. goto out_free_dev;
  4022. }
  4023. SET_NETDEV_DEV(netdev, &pdev->dev);
  4024. adapter->port[i] = netdev;
  4025. pi = netdev_priv(netdev);
  4026. pi->adapter = adapter;
  4027. pi->xact_addr_filt = -1;
  4028. pi->port_id = i;
  4029. netdev->irq = pdev->irq;
  4030. netdev->hw_features = NETIF_F_SG | TSO_FLAGS |
  4031. NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  4032. NETIF_F_RXCSUM | NETIF_F_RXHASH |
  4033. NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
  4034. if (highdma)
  4035. netdev->hw_features |= NETIF_F_HIGHDMA;
  4036. netdev->features |= netdev->hw_features;
  4037. netdev->vlan_features = netdev->features & VLAN_FEAT;
  4038. netdev->priv_flags |= IFF_UNICAST_FLT;
  4039. netdev->netdev_ops = &cxgb4_netdev_ops;
  4040. #ifdef CONFIG_CHELSIO_T4_DCB
  4041. netdev->dcbnl_ops = &cxgb4_dcb_ops;
  4042. cxgb4_dcb_state_init(netdev);
  4043. #endif
  4044. cxgb4_set_ethtool_ops(netdev);
  4045. }
  4046. pci_set_drvdata(pdev, adapter);
  4047. if (adapter->flags & FW_OK) {
  4048. err = t4_port_init(adapter, func, func, 0);
  4049. if (err)
  4050. goto out_free_dev;
  4051. }
  4052. /*
  4053. * Configure queues and allocate tables now, they can be needed as
  4054. * soon as the first register_netdev completes.
  4055. */
  4056. cfg_queues(adapter);
  4057. adapter->l2t = t4_init_l2t();
  4058. if (!adapter->l2t) {
  4059. /* We tolerate a lack of L2T, giving up some functionality */
  4060. dev_warn(&pdev->dev, "could not allocate L2T, continuing\n");
  4061. adapter->params.offload = 0;
  4062. }
  4063. #if IS_ENABLED(CONFIG_IPV6)
  4064. adapter->clipt = t4_init_clip_tbl(adapter->clipt_start,
  4065. adapter->clipt_end);
  4066. if (!adapter->clipt) {
  4067. /* We tolerate a lack of clip_table, giving up
  4068. * some functionality
  4069. */
  4070. dev_warn(&pdev->dev,
  4071. "could not allocate Clip table, continuing\n");
  4072. adapter->params.offload = 0;
  4073. }
  4074. #endif
  4075. if (is_offload(adapter) && tid_init(&adapter->tids) < 0) {
  4076. dev_warn(&pdev->dev, "could not allocate TID table, "
  4077. "continuing\n");
  4078. adapter->params.offload = 0;
  4079. }
  4080. /* See what interrupts we'll be using */
  4081. if (msi > 1 && enable_msix(adapter) == 0)
  4082. adapter->flags |= USING_MSIX;
  4083. else if (msi > 0 && pci_enable_msi(pdev) == 0)
  4084. adapter->flags |= USING_MSI;
  4085. err = init_rss(adapter);
  4086. if (err)
  4087. goto out_free_dev;
  4088. /*
  4089. * The card is now ready to go. If any errors occur during device
  4090. * registration we do not fail the whole card but rather proceed only
  4091. * with the ports we manage to register successfully. However we must
  4092. * register at least one net device.
  4093. */
  4094. for_each_port(adapter, i) {
  4095. pi = adap2pinfo(adapter, i);
  4096. netif_set_real_num_tx_queues(adapter->port[i], pi->nqsets);
  4097. netif_set_real_num_rx_queues(adapter->port[i], pi->nqsets);
  4098. err = register_netdev(adapter->port[i]);
  4099. if (err)
  4100. break;
  4101. adapter->chan_map[pi->tx_chan] = i;
  4102. print_port_info(adapter->port[i]);
  4103. }
  4104. if (i == 0) {
  4105. dev_err(&pdev->dev, "could not register any net devices\n");
  4106. goto out_free_dev;
  4107. }
  4108. if (err) {
  4109. dev_warn(&pdev->dev, "only %d net devices registered\n", i);
  4110. err = 0;
  4111. }
  4112. if (cxgb4_debugfs_root) {
  4113. adapter->debugfs_root = debugfs_create_dir(pci_name(pdev),
  4114. cxgb4_debugfs_root);
  4115. setup_debugfs(adapter);
  4116. }
  4117. /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
  4118. pdev->needs_freset = 1;
  4119. if (is_offload(adapter))
  4120. attach_ulds(adapter);
  4121. sriov:
  4122. #ifdef CONFIG_PCI_IOV
  4123. if (func < ARRAY_SIZE(num_vf) && num_vf[func] > 0)
  4124. if (pci_enable_sriov(pdev, num_vf[func]) == 0)
  4125. dev_info(&pdev->dev,
  4126. "instantiated %u virtual functions\n",
  4127. num_vf[func]);
  4128. #endif
  4129. return 0;
  4130. out_free_dev:
  4131. free_some_resources(adapter);
  4132. out_unmap_bar:
  4133. if (!is_t4(adapter->params.chip))
  4134. iounmap(adapter->bar2);
  4135. out_free_adapter:
  4136. if (adapter->workq)
  4137. destroy_workqueue(adapter->workq);
  4138. kfree(adapter);
  4139. out_unmap_bar0:
  4140. iounmap(regs);
  4141. out_disable_device:
  4142. pci_disable_pcie_error_reporting(pdev);
  4143. pci_disable_device(pdev);
  4144. out_release_regions:
  4145. pci_release_regions(pdev);
  4146. return err;
  4147. }
  4148. static void remove_one(struct pci_dev *pdev)
  4149. {
  4150. struct adapter *adapter = pci_get_drvdata(pdev);
  4151. #ifdef CONFIG_PCI_IOV
  4152. pci_disable_sriov(pdev);
  4153. #endif
  4154. if (adapter) {
  4155. int i;
  4156. /* Tear down per-adapter Work Queue first since it can contain
  4157. * references to our adapter data structure.
  4158. */
  4159. destroy_workqueue(adapter->workq);
  4160. if (is_offload(adapter))
  4161. detach_ulds(adapter);
  4162. disable_interrupts(adapter);
  4163. for_each_port(adapter, i)
  4164. if (adapter->port[i]->reg_state == NETREG_REGISTERED)
  4165. unregister_netdev(adapter->port[i]);
  4166. debugfs_remove_recursive(adapter->debugfs_root);
  4167. /* If we allocated filters, free up state associated with any
  4168. * valid filters ...
  4169. */
  4170. if (adapter->tids.ftid_tab) {
  4171. struct filter_entry *f = &adapter->tids.ftid_tab[0];
  4172. for (i = 0; i < (adapter->tids.nftids +
  4173. adapter->tids.nsftids); i++, f++)
  4174. if (f->valid)
  4175. clear_filter(adapter, f);
  4176. }
  4177. if (adapter->flags & FULL_INIT_DONE)
  4178. cxgb_down(adapter);
  4179. free_some_resources(adapter);
  4180. #if IS_ENABLED(CONFIG_IPV6)
  4181. t4_cleanup_clip_tbl(adapter);
  4182. #endif
  4183. iounmap(adapter->regs);
  4184. if (!is_t4(adapter->params.chip))
  4185. iounmap(adapter->bar2);
  4186. pci_disable_pcie_error_reporting(pdev);
  4187. if ((adapter->flags & DEV_ENABLED)) {
  4188. pci_disable_device(pdev);
  4189. adapter->flags &= ~DEV_ENABLED;
  4190. }
  4191. pci_release_regions(pdev);
  4192. synchronize_rcu();
  4193. kfree(adapter);
  4194. } else
  4195. pci_release_regions(pdev);
  4196. }
  4197. static struct pci_driver cxgb4_driver = {
  4198. .name = KBUILD_MODNAME,
  4199. .id_table = cxgb4_pci_tbl,
  4200. .probe = init_one,
  4201. .remove = remove_one,
  4202. .shutdown = remove_one,
  4203. .err_handler = &cxgb4_eeh,
  4204. };
  4205. static int __init cxgb4_init_module(void)
  4206. {
  4207. int ret;
  4208. /* Debugfs support is optional, just warn if this fails */
  4209. cxgb4_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
  4210. if (!cxgb4_debugfs_root)
  4211. pr_warn("could not create debugfs entry, continuing\n");
  4212. ret = pci_register_driver(&cxgb4_driver);
  4213. if (ret < 0)
  4214. debugfs_remove(cxgb4_debugfs_root);
  4215. #if IS_ENABLED(CONFIG_IPV6)
  4216. if (!inet6addr_registered) {
  4217. register_inet6addr_notifier(&cxgb4_inet6addr_notifier);
  4218. inet6addr_registered = true;
  4219. }
  4220. #endif
  4221. return ret;
  4222. }
  4223. static void __exit cxgb4_cleanup_module(void)
  4224. {
  4225. #if IS_ENABLED(CONFIG_IPV6)
  4226. if (inet6addr_registered) {
  4227. unregister_inet6addr_notifier(&cxgb4_inet6addr_notifier);
  4228. inet6addr_registered = false;
  4229. }
  4230. #endif
  4231. pci_unregister_driver(&cxgb4_driver);
  4232. debugfs_remove(cxgb4_debugfs_root); /* NULL ok */
  4233. }
  4234. module_init(cxgb4_init_module);
  4235. module_exit(cxgb4_cleanup_module);