cxgb4_main.c 138 KB

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