cxgb4_main.c 139 KB

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