cxgb4_main.c 139 KB

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