cxgb4_main.c 142 KB

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