cxgb4_main.c 138 KB

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