cassini.c 140 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305
  1. /* cassini.c: Sun Microsystems Cassini(+) ethernet driver.
  2. *
  3. * Copyright (C) 2004 Sun Microsystems Inc.
  4. * Copyright (C) 2003 Adrian Sun (asun@darksunrising.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. * 02111-1307, USA.
  20. *
  21. * This driver uses the sungem driver (c) David Miller
  22. * (davem@redhat.com) as its basis.
  23. *
  24. * The cassini chip has a number of features that distinguish it from
  25. * the gem chip:
  26. * 4 transmit descriptor rings that are used for either QoS (VLAN) or
  27. * load balancing (non-VLAN mode)
  28. * batching of multiple packets
  29. * multiple CPU dispatching
  30. * page-based RX descriptor engine with separate completion rings
  31. * Gigabit support (GMII and PCS interface)
  32. * MIF link up/down detection works
  33. *
  34. * RX is handled by page sized buffers that are attached as fragments to
  35. * the skb. here's what's done:
  36. * -- driver allocates pages at a time and keeps reference counts
  37. * on them.
  38. * -- the upper protocol layers assume that the header is in the skb
  39. * itself. as a result, cassini will copy a small amount (64 bytes)
  40. * to make them happy.
  41. * -- driver appends the rest of the data pages as frags to skbuffs
  42. * and increments the reference count
  43. * -- on page reclamation, the driver swaps the page with a spare page.
  44. * if that page is still in use, it frees its reference to that page,
  45. * and allocates a new page for use. otherwise, it just recycles the
  46. * the page.
  47. *
  48. * NOTE: cassini can parse the header. however, it's not worth it
  49. * as long as the network stack requires a header copy.
  50. *
  51. * TX has 4 queues. currently these queues are used in a round-robin
  52. * fashion for load balancing. They can also be used for QoS. for that
  53. * to work, however, QoS information needs to be exposed down to the driver
  54. * level so that subqueues get targeted to particular transmit rings.
  55. * alternatively, the queues can be configured via use of the all-purpose
  56. * ioctl.
  57. *
  58. * RX DATA: the rx completion ring has all the info, but the rx desc
  59. * ring has all of the data. RX can conceivably come in under multiple
  60. * interrupts, but the INT# assignment needs to be set up properly by
  61. * the BIOS and conveyed to the driver. PCI BIOSes don't know how to do
  62. * that. also, the two descriptor rings are designed to distinguish between
  63. * encrypted and non-encrypted packets, but we use them for buffering
  64. * instead.
  65. *
  66. * by default, the selective clear mask is set up to process rx packets.
  67. */
  68. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  69. #include <linux/module.h>
  70. #include <linux/kernel.h>
  71. #include <linux/types.h>
  72. #include <linux/compiler.h>
  73. #include <linux/slab.h>
  74. #include <linux/delay.h>
  75. #include <linux/init.h>
  76. #include <linux/interrupt.h>
  77. #include <linux/vmalloc.h>
  78. #include <linux/ioport.h>
  79. #include <linux/pci.h>
  80. #include <linux/mm.h>
  81. #include <linux/highmem.h>
  82. #include <linux/list.h>
  83. #include <linux/dma-mapping.h>
  84. #include <linux/netdevice.h>
  85. #include <linux/etherdevice.h>
  86. #include <linux/skbuff.h>
  87. #include <linux/ethtool.h>
  88. #include <linux/crc32.h>
  89. #include <linux/random.h>
  90. #include <linux/mii.h>
  91. #include <linux/ip.h>
  92. #include <linux/tcp.h>
  93. #include <linux/mutex.h>
  94. #include <linux/firmware.h>
  95. #include <net/checksum.h>
  96. #include <linux/atomic.h>
  97. #include <asm/system.h>
  98. #include <asm/io.h>
  99. #include <asm/byteorder.h>
  100. #include <asm/uaccess.h>
  101. #define cas_page_map(x) kmap_atomic((x), KM_SKB_DATA_SOFTIRQ)
  102. #define cas_page_unmap(x) kunmap_atomic((x), KM_SKB_DATA_SOFTIRQ)
  103. #define CAS_NCPUS num_online_cpus()
  104. #define cas_skb_release(x) netif_rx(x)
  105. /* select which firmware to use */
  106. #define USE_HP_WORKAROUND
  107. #define HP_WORKAROUND_DEFAULT /* select which firmware to use as default */
  108. #define CAS_HP_ALT_FIRMWARE cas_prog_null /* alternate firmware */
  109. #include "cassini.h"
  110. #define USE_TX_COMPWB /* use completion writeback registers */
  111. #define USE_CSMA_CD_PROTO /* standard CSMA/CD */
  112. #define USE_RX_BLANK /* hw interrupt mitigation */
  113. #undef USE_ENTROPY_DEV /* don't test for entropy device */
  114. /* NOTE: these aren't useable unless PCI interrupts can be assigned.
  115. * also, we need to make cp->lock finer-grained.
  116. */
  117. #undef USE_PCI_INTB
  118. #undef USE_PCI_INTC
  119. #undef USE_PCI_INTD
  120. #undef USE_QOS
  121. #undef USE_VPD_DEBUG /* debug vpd information if defined */
  122. /* rx processing options */
  123. #define USE_PAGE_ORDER /* specify to allocate large rx pages */
  124. #define RX_DONT_BATCH 0 /* if 1, don't batch flows */
  125. #define RX_COPY_ALWAYS 0 /* if 0, use frags */
  126. #define RX_COPY_MIN 64 /* copy a little to make upper layers happy */
  127. #undef RX_COUNT_BUFFERS /* define to calculate RX buffer stats */
  128. #define DRV_MODULE_NAME "cassini"
  129. #define DRV_MODULE_VERSION "1.6"
  130. #define DRV_MODULE_RELDATE "21 May 2008"
  131. #define CAS_DEF_MSG_ENABLE \
  132. (NETIF_MSG_DRV | \
  133. NETIF_MSG_PROBE | \
  134. NETIF_MSG_LINK | \
  135. NETIF_MSG_TIMER | \
  136. NETIF_MSG_IFDOWN | \
  137. NETIF_MSG_IFUP | \
  138. NETIF_MSG_RX_ERR | \
  139. NETIF_MSG_TX_ERR)
  140. /* length of time before we decide the hardware is borked,
  141. * and dev->tx_timeout() should be called to fix the problem
  142. */
  143. #define CAS_TX_TIMEOUT (HZ)
  144. #define CAS_LINK_TIMEOUT (22*HZ/10)
  145. #define CAS_LINK_FAST_TIMEOUT (1)
  146. /* timeout values for state changing. these specify the number
  147. * of 10us delays to be used before giving up.
  148. */
  149. #define STOP_TRIES_PHY 1000
  150. #define STOP_TRIES 5000
  151. /* specify a minimum frame size to deal with some fifo issues
  152. * max mtu == 2 * page size - ethernet header - 64 - swivel =
  153. * 2 * page_size - 0x50
  154. */
  155. #define CAS_MIN_FRAME 97
  156. #define CAS_1000MB_MIN_FRAME 255
  157. #define CAS_MIN_MTU 60
  158. #define CAS_MAX_MTU min(((cp->page_size << 1) - 0x50), 9000)
  159. #if 1
  160. /*
  161. * Eliminate these and use separate atomic counters for each, to
  162. * avoid a race condition.
  163. */
  164. #else
  165. #define CAS_RESET_MTU 1
  166. #define CAS_RESET_ALL 2
  167. #define CAS_RESET_SPARE 3
  168. #endif
  169. static char version[] __devinitdata =
  170. DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
  171. static int cassini_debug = -1; /* -1 == use CAS_DEF_MSG_ENABLE as value */
  172. static int link_mode;
  173. MODULE_AUTHOR("Adrian Sun (asun@darksunrising.com)");
  174. MODULE_DESCRIPTION("Sun Cassini(+) ethernet driver");
  175. MODULE_LICENSE("GPL");
  176. MODULE_FIRMWARE("sun/cassini.bin");
  177. module_param(cassini_debug, int, 0);
  178. MODULE_PARM_DESC(cassini_debug, "Cassini bitmapped debugging message enable value");
  179. module_param(link_mode, int, 0);
  180. MODULE_PARM_DESC(link_mode, "default link mode");
  181. /*
  182. * Work around for a PCS bug in which the link goes down due to the chip
  183. * being confused and never showing a link status of "up."
  184. */
  185. #define DEFAULT_LINKDOWN_TIMEOUT 5
  186. /*
  187. * Value in seconds, for user input.
  188. */
  189. static int linkdown_timeout = DEFAULT_LINKDOWN_TIMEOUT;
  190. module_param(linkdown_timeout, int, 0);
  191. MODULE_PARM_DESC(linkdown_timeout,
  192. "min reset interval in sec. for PCS linkdown issue; disabled if not positive");
  193. /*
  194. * value in 'ticks' (units used by jiffies). Set when we init the
  195. * module because 'HZ' in actually a function call on some flavors of
  196. * Linux. This will default to DEFAULT_LINKDOWN_TIMEOUT * HZ.
  197. */
  198. static int link_transition_timeout;
  199. static u16 link_modes[] __devinitdata = {
  200. BMCR_ANENABLE, /* 0 : autoneg */
  201. 0, /* 1 : 10bt half duplex */
  202. BMCR_SPEED100, /* 2 : 100bt half duplex */
  203. BMCR_FULLDPLX, /* 3 : 10bt full duplex */
  204. BMCR_SPEED100|BMCR_FULLDPLX, /* 4 : 100bt full duplex */
  205. CAS_BMCR_SPEED1000|BMCR_FULLDPLX /* 5 : 1000bt full duplex */
  206. };
  207. static DEFINE_PCI_DEVICE_TABLE(cas_pci_tbl) = {
  208. { PCI_VENDOR_ID_SUN, PCI_DEVICE_ID_SUN_CASSINI,
  209. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  210. { PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SATURN,
  211. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  212. { 0, }
  213. };
  214. MODULE_DEVICE_TABLE(pci, cas_pci_tbl);
  215. static void cas_set_link_modes(struct cas *cp);
  216. static inline void cas_lock_tx(struct cas *cp)
  217. {
  218. int i;
  219. for (i = 0; i < N_TX_RINGS; i++)
  220. spin_lock(&cp->tx_lock[i]);
  221. }
  222. static inline void cas_lock_all(struct cas *cp)
  223. {
  224. spin_lock_irq(&cp->lock);
  225. cas_lock_tx(cp);
  226. }
  227. /* WTZ: QA was finding deadlock problems with the previous
  228. * versions after long test runs with multiple cards per machine.
  229. * See if replacing cas_lock_all with safer versions helps. The
  230. * symptoms QA is reporting match those we'd expect if interrupts
  231. * aren't being properly restored, and we fixed a previous deadlock
  232. * with similar symptoms by using save/restore versions in other
  233. * places.
  234. */
  235. #define cas_lock_all_save(cp, flags) \
  236. do { \
  237. struct cas *xxxcp = (cp); \
  238. spin_lock_irqsave(&xxxcp->lock, flags); \
  239. cas_lock_tx(xxxcp); \
  240. } while (0)
  241. static inline void cas_unlock_tx(struct cas *cp)
  242. {
  243. int i;
  244. for (i = N_TX_RINGS; i > 0; i--)
  245. spin_unlock(&cp->tx_lock[i - 1]);
  246. }
  247. static inline void cas_unlock_all(struct cas *cp)
  248. {
  249. cas_unlock_tx(cp);
  250. spin_unlock_irq(&cp->lock);
  251. }
  252. #define cas_unlock_all_restore(cp, flags) \
  253. do { \
  254. struct cas *xxxcp = (cp); \
  255. cas_unlock_tx(xxxcp); \
  256. spin_unlock_irqrestore(&xxxcp->lock, flags); \
  257. } while (0)
  258. static void cas_disable_irq(struct cas *cp, const int ring)
  259. {
  260. /* Make sure we won't get any more interrupts */
  261. if (ring == 0) {
  262. writel(0xFFFFFFFF, cp->regs + REG_INTR_MASK);
  263. return;
  264. }
  265. /* disable completion interrupts and selectively mask */
  266. if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
  267. switch (ring) {
  268. #if defined (USE_PCI_INTB) || defined(USE_PCI_INTC) || defined(USE_PCI_INTD)
  269. #ifdef USE_PCI_INTB
  270. case 1:
  271. #endif
  272. #ifdef USE_PCI_INTC
  273. case 2:
  274. #endif
  275. #ifdef USE_PCI_INTD
  276. case 3:
  277. #endif
  278. writel(INTRN_MASK_CLEAR_ALL | INTRN_MASK_RX_EN,
  279. cp->regs + REG_PLUS_INTRN_MASK(ring));
  280. break;
  281. #endif
  282. default:
  283. writel(INTRN_MASK_CLEAR_ALL, cp->regs +
  284. REG_PLUS_INTRN_MASK(ring));
  285. break;
  286. }
  287. }
  288. }
  289. static inline void cas_mask_intr(struct cas *cp)
  290. {
  291. int i;
  292. for (i = 0; i < N_RX_COMP_RINGS; i++)
  293. cas_disable_irq(cp, i);
  294. }
  295. static void cas_enable_irq(struct cas *cp, const int ring)
  296. {
  297. if (ring == 0) { /* all but TX_DONE */
  298. writel(INTR_TX_DONE, cp->regs + REG_INTR_MASK);
  299. return;
  300. }
  301. if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
  302. switch (ring) {
  303. #if defined (USE_PCI_INTB) || defined(USE_PCI_INTC) || defined(USE_PCI_INTD)
  304. #ifdef USE_PCI_INTB
  305. case 1:
  306. #endif
  307. #ifdef USE_PCI_INTC
  308. case 2:
  309. #endif
  310. #ifdef USE_PCI_INTD
  311. case 3:
  312. #endif
  313. writel(INTRN_MASK_RX_EN, cp->regs +
  314. REG_PLUS_INTRN_MASK(ring));
  315. break;
  316. #endif
  317. default:
  318. break;
  319. }
  320. }
  321. }
  322. static inline void cas_unmask_intr(struct cas *cp)
  323. {
  324. int i;
  325. for (i = 0; i < N_RX_COMP_RINGS; i++)
  326. cas_enable_irq(cp, i);
  327. }
  328. static inline void cas_entropy_gather(struct cas *cp)
  329. {
  330. #ifdef USE_ENTROPY_DEV
  331. if ((cp->cas_flags & CAS_FLAG_ENTROPY_DEV) == 0)
  332. return;
  333. batch_entropy_store(readl(cp->regs + REG_ENTROPY_IV),
  334. readl(cp->regs + REG_ENTROPY_IV),
  335. sizeof(uint64_t)*8);
  336. #endif
  337. }
  338. static inline void cas_entropy_reset(struct cas *cp)
  339. {
  340. #ifdef USE_ENTROPY_DEV
  341. if ((cp->cas_flags & CAS_FLAG_ENTROPY_DEV) == 0)
  342. return;
  343. writel(BIM_LOCAL_DEV_PAD | BIM_LOCAL_DEV_PROM | BIM_LOCAL_DEV_EXT,
  344. cp->regs + REG_BIM_LOCAL_DEV_EN);
  345. writeb(ENTROPY_RESET_STC_MODE, cp->regs + REG_ENTROPY_RESET);
  346. writeb(0x55, cp->regs + REG_ENTROPY_RAND_REG);
  347. /* if we read back 0x0, we don't have an entropy device */
  348. if (readb(cp->regs + REG_ENTROPY_RAND_REG) == 0)
  349. cp->cas_flags &= ~CAS_FLAG_ENTROPY_DEV;
  350. #endif
  351. }
  352. /* access to the phy. the following assumes that we've initialized the MIF to
  353. * be in frame rather than bit-bang mode
  354. */
  355. static u16 cas_phy_read(struct cas *cp, int reg)
  356. {
  357. u32 cmd;
  358. int limit = STOP_TRIES_PHY;
  359. cmd = MIF_FRAME_ST | MIF_FRAME_OP_READ;
  360. cmd |= CAS_BASE(MIF_FRAME_PHY_ADDR, cp->phy_addr);
  361. cmd |= CAS_BASE(MIF_FRAME_REG_ADDR, reg);
  362. cmd |= MIF_FRAME_TURN_AROUND_MSB;
  363. writel(cmd, cp->regs + REG_MIF_FRAME);
  364. /* poll for completion */
  365. while (limit-- > 0) {
  366. udelay(10);
  367. cmd = readl(cp->regs + REG_MIF_FRAME);
  368. if (cmd & MIF_FRAME_TURN_AROUND_LSB)
  369. return cmd & MIF_FRAME_DATA_MASK;
  370. }
  371. return 0xFFFF; /* -1 */
  372. }
  373. static int cas_phy_write(struct cas *cp, int reg, u16 val)
  374. {
  375. int limit = STOP_TRIES_PHY;
  376. u32 cmd;
  377. cmd = MIF_FRAME_ST | MIF_FRAME_OP_WRITE;
  378. cmd |= CAS_BASE(MIF_FRAME_PHY_ADDR, cp->phy_addr);
  379. cmd |= CAS_BASE(MIF_FRAME_REG_ADDR, reg);
  380. cmd |= MIF_FRAME_TURN_AROUND_MSB;
  381. cmd |= val & MIF_FRAME_DATA_MASK;
  382. writel(cmd, cp->regs + REG_MIF_FRAME);
  383. /* poll for completion */
  384. while (limit-- > 0) {
  385. udelay(10);
  386. cmd = readl(cp->regs + REG_MIF_FRAME);
  387. if (cmd & MIF_FRAME_TURN_AROUND_LSB)
  388. return 0;
  389. }
  390. return -1;
  391. }
  392. static void cas_phy_powerup(struct cas *cp)
  393. {
  394. u16 ctl = cas_phy_read(cp, MII_BMCR);
  395. if ((ctl & BMCR_PDOWN) == 0)
  396. return;
  397. ctl &= ~BMCR_PDOWN;
  398. cas_phy_write(cp, MII_BMCR, ctl);
  399. }
  400. static void cas_phy_powerdown(struct cas *cp)
  401. {
  402. u16 ctl = cas_phy_read(cp, MII_BMCR);
  403. if (ctl & BMCR_PDOWN)
  404. return;
  405. ctl |= BMCR_PDOWN;
  406. cas_phy_write(cp, MII_BMCR, ctl);
  407. }
  408. /* cp->lock held. note: the last put_page will free the buffer */
  409. static int cas_page_free(struct cas *cp, cas_page_t *page)
  410. {
  411. pci_unmap_page(cp->pdev, page->dma_addr, cp->page_size,
  412. PCI_DMA_FROMDEVICE);
  413. __free_pages(page->buffer, cp->page_order);
  414. kfree(page);
  415. return 0;
  416. }
  417. #ifdef RX_COUNT_BUFFERS
  418. #define RX_USED_ADD(x, y) ((x)->used += (y))
  419. #define RX_USED_SET(x, y) ((x)->used = (y))
  420. #else
  421. #define RX_USED_ADD(x, y)
  422. #define RX_USED_SET(x, y)
  423. #endif
  424. /* local page allocation routines for the receive buffers. jumbo pages
  425. * require at least 8K contiguous and 8K aligned buffers.
  426. */
  427. static cas_page_t *cas_page_alloc(struct cas *cp, const gfp_t flags)
  428. {
  429. cas_page_t *page;
  430. page = kmalloc(sizeof(cas_page_t), flags);
  431. if (!page)
  432. return NULL;
  433. INIT_LIST_HEAD(&page->list);
  434. RX_USED_SET(page, 0);
  435. page->buffer = alloc_pages(flags, cp->page_order);
  436. if (!page->buffer)
  437. goto page_err;
  438. page->dma_addr = pci_map_page(cp->pdev, page->buffer, 0,
  439. cp->page_size, PCI_DMA_FROMDEVICE);
  440. return page;
  441. page_err:
  442. kfree(page);
  443. return NULL;
  444. }
  445. /* initialize spare pool of rx buffers, but allocate during the open */
  446. static void cas_spare_init(struct cas *cp)
  447. {
  448. spin_lock(&cp->rx_inuse_lock);
  449. INIT_LIST_HEAD(&cp->rx_inuse_list);
  450. spin_unlock(&cp->rx_inuse_lock);
  451. spin_lock(&cp->rx_spare_lock);
  452. INIT_LIST_HEAD(&cp->rx_spare_list);
  453. cp->rx_spares_needed = RX_SPARE_COUNT;
  454. spin_unlock(&cp->rx_spare_lock);
  455. }
  456. /* used on close. free all the spare buffers. */
  457. static void cas_spare_free(struct cas *cp)
  458. {
  459. struct list_head list, *elem, *tmp;
  460. /* free spare buffers */
  461. INIT_LIST_HEAD(&list);
  462. spin_lock(&cp->rx_spare_lock);
  463. list_splice_init(&cp->rx_spare_list, &list);
  464. spin_unlock(&cp->rx_spare_lock);
  465. list_for_each_safe(elem, tmp, &list) {
  466. cas_page_free(cp, list_entry(elem, cas_page_t, list));
  467. }
  468. INIT_LIST_HEAD(&list);
  469. #if 1
  470. /*
  471. * Looks like Adrian had protected this with a different
  472. * lock than used everywhere else to manipulate this list.
  473. */
  474. spin_lock(&cp->rx_inuse_lock);
  475. list_splice_init(&cp->rx_inuse_list, &list);
  476. spin_unlock(&cp->rx_inuse_lock);
  477. #else
  478. spin_lock(&cp->rx_spare_lock);
  479. list_splice_init(&cp->rx_inuse_list, &list);
  480. spin_unlock(&cp->rx_spare_lock);
  481. #endif
  482. list_for_each_safe(elem, tmp, &list) {
  483. cas_page_free(cp, list_entry(elem, cas_page_t, list));
  484. }
  485. }
  486. /* replenish spares if needed */
  487. static void cas_spare_recover(struct cas *cp, const gfp_t flags)
  488. {
  489. struct list_head list, *elem, *tmp;
  490. int needed, i;
  491. /* check inuse list. if we don't need any more free buffers,
  492. * just free it
  493. */
  494. /* make a local copy of the list */
  495. INIT_LIST_HEAD(&list);
  496. spin_lock(&cp->rx_inuse_lock);
  497. list_splice_init(&cp->rx_inuse_list, &list);
  498. spin_unlock(&cp->rx_inuse_lock);
  499. list_for_each_safe(elem, tmp, &list) {
  500. cas_page_t *page = list_entry(elem, cas_page_t, list);
  501. /*
  502. * With the lockless pagecache, cassini buffering scheme gets
  503. * slightly less accurate: we might find that a page has an
  504. * elevated reference count here, due to a speculative ref,
  505. * and skip it as in-use. Ideally we would be able to reclaim
  506. * it. However this would be such a rare case, it doesn't
  507. * matter too much as we should pick it up the next time round.
  508. *
  509. * Importantly, if we find that the page has a refcount of 1
  510. * here (our refcount), then we know it is definitely not inuse
  511. * so we can reuse it.
  512. */
  513. if (page_count(page->buffer) > 1)
  514. continue;
  515. list_del(elem);
  516. spin_lock(&cp->rx_spare_lock);
  517. if (cp->rx_spares_needed > 0) {
  518. list_add(elem, &cp->rx_spare_list);
  519. cp->rx_spares_needed--;
  520. spin_unlock(&cp->rx_spare_lock);
  521. } else {
  522. spin_unlock(&cp->rx_spare_lock);
  523. cas_page_free(cp, page);
  524. }
  525. }
  526. /* put any inuse buffers back on the list */
  527. if (!list_empty(&list)) {
  528. spin_lock(&cp->rx_inuse_lock);
  529. list_splice(&list, &cp->rx_inuse_list);
  530. spin_unlock(&cp->rx_inuse_lock);
  531. }
  532. spin_lock(&cp->rx_spare_lock);
  533. needed = cp->rx_spares_needed;
  534. spin_unlock(&cp->rx_spare_lock);
  535. if (!needed)
  536. return;
  537. /* we still need spares, so try to allocate some */
  538. INIT_LIST_HEAD(&list);
  539. i = 0;
  540. while (i < needed) {
  541. cas_page_t *spare = cas_page_alloc(cp, flags);
  542. if (!spare)
  543. break;
  544. list_add(&spare->list, &list);
  545. i++;
  546. }
  547. spin_lock(&cp->rx_spare_lock);
  548. list_splice(&list, &cp->rx_spare_list);
  549. cp->rx_spares_needed -= i;
  550. spin_unlock(&cp->rx_spare_lock);
  551. }
  552. /* pull a page from the list. */
  553. static cas_page_t *cas_page_dequeue(struct cas *cp)
  554. {
  555. struct list_head *entry;
  556. int recover;
  557. spin_lock(&cp->rx_spare_lock);
  558. if (list_empty(&cp->rx_spare_list)) {
  559. /* try to do a quick recovery */
  560. spin_unlock(&cp->rx_spare_lock);
  561. cas_spare_recover(cp, GFP_ATOMIC);
  562. spin_lock(&cp->rx_spare_lock);
  563. if (list_empty(&cp->rx_spare_list)) {
  564. netif_err(cp, rx_err, cp->dev,
  565. "no spare buffers available\n");
  566. spin_unlock(&cp->rx_spare_lock);
  567. return NULL;
  568. }
  569. }
  570. entry = cp->rx_spare_list.next;
  571. list_del(entry);
  572. recover = ++cp->rx_spares_needed;
  573. spin_unlock(&cp->rx_spare_lock);
  574. /* trigger the timer to do the recovery */
  575. if ((recover & (RX_SPARE_RECOVER_VAL - 1)) == 0) {
  576. #if 1
  577. atomic_inc(&cp->reset_task_pending);
  578. atomic_inc(&cp->reset_task_pending_spare);
  579. schedule_work(&cp->reset_task);
  580. #else
  581. atomic_set(&cp->reset_task_pending, CAS_RESET_SPARE);
  582. schedule_work(&cp->reset_task);
  583. #endif
  584. }
  585. return list_entry(entry, cas_page_t, list);
  586. }
  587. static void cas_mif_poll(struct cas *cp, const int enable)
  588. {
  589. u32 cfg;
  590. cfg = readl(cp->regs + REG_MIF_CFG);
  591. cfg &= (MIF_CFG_MDIO_0 | MIF_CFG_MDIO_1);
  592. if (cp->phy_type & CAS_PHY_MII_MDIO1)
  593. cfg |= MIF_CFG_PHY_SELECT;
  594. /* poll and interrupt on link status change. */
  595. if (enable) {
  596. cfg |= MIF_CFG_POLL_EN;
  597. cfg |= CAS_BASE(MIF_CFG_POLL_REG, MII_BMSR);
  598. cfg |= CAS_BASE(MIF_CFG_POLL_PHY, cp->phy_addr);
  599. }
  600. writel((enable) ? ~(BMSR_LSTATUS | BMSR_ANEGCOMPLETE) : 0xFFFF,
  601. cp->regs + REG_MIF_MASK);
  602. writel(cfg, cp->regs + REG_MIF_CFG);
  603. }
  604. /* Must be invoked under cp->lock */
  605. static void cas_begin_auto_negotiation(struct cas *cp, struct ethtool_cmd *ep)
  606. {
  607. u16 ctl;
  608. #if 1
  609. int lcntl;
  610. int changed = 0;
  611. int oldstate = cp->lstate;
  612. int link_was_not_down = !(oldstate == link_down);
  613. #endif
  614. /* Setup link parameters */
  615. if (!ep)
  616. goto start_aneg;
  617. lcntl = cp->link_cntl;
  618. if (ep->autoneg == AUTONEG_ENABLE)
  619. cp->link_cntl = BMCR_ANENABLE;
  620. else {
  621. u32 speed = ethtool_cmd_speed(ep);
  622. cp->link_cntl = 0;
  623. if (speed == SPEED_100)
  624. cp->link_cntl |= BMCR_SPEED100;
  625. else if (speed == SPEED_1000)
  626. cp->link_cntl |= CAS_BMCR_SPEED1000;
  627. if (ep->duplex == DUPLEX_FULL)
  628. cp->link_cntl |= BMCR_FULLDPLX;
  629. }
  630. #if 1
  631. changed = (lcntl != cp->link_cntl);
  632. #endif
  633. start_aneg:
  634. if (cp->lstate == link_up) {
  635. netdev_info(cp->dev, "PCS link down\n");
  636. } else {
  637. if (changed) {
  638. netdev_info(cp->dev, "link configuration changed\n");
  639. }
  640. }
  641. cp->lstate = link_down;
  642. cp->link_transition = LINK_TRANSITION_LINK_DOWN;
  643. if (!cp->hw_running)
  644. return;
  645. #if 1
  646. /*
  647. * WTZ: If the old state was link_up, we turn off the carrier
  648. * to replicate everything we do elsewhere on a link-down
  649. * event when we were already in a link-up state..
  650. */
  651. if (oldstate == link_up)
  652. netif_carrier_off(cp->dev);
  653. if (changed && link_was_not_down) {
  654. /*
  655. * WTZ: This branch will simply schedule a full reset after
  656. * we explicitly changed link modes in an ioctl. See if this
  657. * fixes the link-problems we were having for forced mode.
  658. */
  659. atomic_inc(&cp->reset_task_pending);
  660. atomic_inc(&cp->reset_task_pending_all);
  661. schedule_work(&cp->reset_task);
  662. cp->timer_ticks = 0;
  663. mod_timer(&cp->link_timer, jiffies + CAS_LINK_TIMEOUT);
  664. return;
  665. }
  666. #endif
  667. if (cp->phy_type & CAS_PHY_SERDES) {
  668. u32 val = readl(cp->regs + REG_PCS_MII_CTRL);
  669. if (cp->link_cntl & BMCR_ANENABLE) {
  670. val |= (PCS_MII_RESTART_AUTONEG | PCS_MII_AUTONEG_EN);
  671. cp->lstate = link_aneg;
  672. } else {
  673. if (cp->link_cntl & BMCR_FULLDPLX)
  674. val |= PCS_MII_CTRL_DUPLEX;
  675. val &= ~PCS_MII_AUTONEG_EN;
  676. cp->lstate = link_force_ok;
  677. }
  678. cp->link_transition = LINK_TRANSITION_LINK_CONFIG;
  679. writel(val, cp->regs + REG_PCS_MII_CTRL);
  680. } else {
  681. cas_mif_poll(cp, 0);
  682. ctl = cas_phy_read(cp, MII_BMCR);
  683. ctl &= ~(BMCR_FULLDPLX | BMCR_SPEED100 |
  684. CAS_BMCR_SPEED1000 | BMCR_ANENABLE);
  685. ctl |= cp->link_cntl;
  686. if (ctl & BMCR_ANENABLE) {
  687. ctl |= BMCR_ANRESTART;
  688. cp->lstate = link_aneg;
  689. } else {
  690. cp->lstate = link_force_ok;
  691. }
  692. cp->link_transition = LINK_TRANSITION_LINK_CONFIG;
  693. cas_phy_write(cp, MII_BMCR, ctl);
  694. cas_mif_poll(cp, 1);
  695. }
  696. cp->timer_ticks = 0;
  697. mod_timer(&cp->link_timer, jiffies + CAS_LINK_TIMEOUT);
  698. }
  699. /* Must be invoked under cp->lock. */
  700. static int cas_reset_mii_phy(struct cas *cp)
  701. {
  702. int limit = STOP_TRIES_PHY;
  703. u16 val;
  704. cas_phy_write(cp, MII_BMCR, BMCR_RESET);
  705. udelay(100);
  706. while (--limit) {
  707. val = cas_phy_read(cp, MII_BMCR);
  708. if ((val & BMCR_RESET) == 0)
  709. break;
  710. udelay(10);
  711. }
  712. return limit <= 0;
  713. }
  714. static int cas_saturn_firmware_init(struct cas *cp)
  715. {
  716. const struct firmware *fw;
  717. const char fw_name[] = "sun/cassini.bin";
  718. int err;
  719. if (PHY_NS_DP83065 != cp->phy_id)
  720. return 0;
  721. err = request_firmware(&fw, fw_name, &cp->pdev->dev);
  722. if (err) {
  723. pr_err("Failed to load firmware \"%s\"\n",
  724. fw_name);
  725. return err;
  726. }
  727. if (fw->size < 2) {
  728. pr_err("bogus length %zu in \"%s\"\n",
  729. fw->size, fw_name);
  730. err = -EINVAL;
  731. goto out;
  732. }
  733. cp->fw_load_addr= fw->data[1] << 8 | fw->data[0];
  734. cp->fw_size = fw->size - 2;
  735. cp->fw_data = vmalloc(cp->fw_size);
  736. if (!cp->fw_data) {
  737. err = -ENOMEM;
  738. pr_err("\"%s\" Failed %d\n", fw_name, err);
  739. goto out;
  740. }
  741. memcpy(cp->fw_data, &fw->data[2], cp->fw_size);
  742. out:
  743. release_firmware(fw);
  744. return err;
  745. }
  746. static void cas_saturn_firmware_load(struct cas *cp)
  747. {
  748. int i;
  749. cas_phy_powerdown(cp);
  750. /* expanded memory access mode */
  751. cas_phy_write(cp, DP83065_MII_MEM, 0x0);
  752. /* pointer configuration for new firmware */
  753. cas_phy_write(cp, DP83065_MII_REGE, 0x8ff9);
  754. cas_phy_write(cp, DP83065_MII_REGD, 0xbd);
  755. cas_phy_write(cp, DP83065_MII_REGE, 0x8ffa);
  756. cas_phy_write(cp, DP83065_MII_REGD, 0x82);
  757. cas_phy_write(cp, DP83065_MII_REGE, 0x8ffb);
  758. cas_phy_write(cp, DP83065_MII_REGD, 0x0);
  759. cas_phy_write(cp, DP83065_MII_REGE, 0x8ffc);
  760. cas_phy_write(cp, DP83065_MII_REGD, 0x39);
  761. /* download new firmware */
  762. cas_phy_write(cp, DP83065_MII_MEM, 0x1);
  763. cas_phy_write(cp, DP83065_MII_REGE, cp->fw_load_addr);
  764. for (i = 0; i < cp->fw_size; i++)
  765. cas_phy_write(cp, DP83065_MII_REGD, cp->fw_data[i]);
  766. /* enable firmware */
  767. cas_phy_write(cp, DP83065_MII_REGE, 0x8ff8);
  768. cas_phy_write(cp, DP83065_MII_REGD, 0x1);
  769. }
  770. /* phy initialization */
  771. static void cas_phy_init(struct cas *cp)
  772. {
  773. u16 val;
  774. /* if we're in MII/GMII mode, set up phy */
  775. if (CAS_PHY_MII(cp->phy_type)) {
  776. writel(PCS_DATAPATH_MODE_MII,
  777. cp->regs + REG_PCS_DATAPATH_MODE);
  778. cas_mif_poll(cp, 0);
  779. cas_reset_mii_phy(cp); /* take out of isolate mode */
  780. if (PHY_LUCENT_B0 == cp->phy_id) {
  781. /* workaround link up/down issue with lucent */
  782. cas_phy_write(cp, LUCENT_MII_REG, 0x8000);
  783. cas_phy_write(cp, MII_BMCR, 0x00f1);
  784. cas_phy_write(cp, LUCENT_MII_REG, 0x0);
  785. } else if (PHY_BROADCOM_B0 == (cp->phy_id & 0xFFFFFFFC)) {
  786. /* workarounds for broadcom phy */
  787. cas_phy_write(cp, BROADCOM_MII_REG8, 0x0C20);
  788. cas_phy_write(cp, BROADCOM_MII_REG7, 0x0012);
  789. cas_phy_write(cp, BROADCOM_MII_REG5, 0x1804);
  790. cas_phy_write(cp, BROADCOM_MII_REG7, 0x0013);
  791. cas_phy_write(cp, BROADCOM_MII_REG5, 0x1204);
  792. cas_phy_write(cp, BROADCOM_MII_REG7, 0x8006);
  793. cas_phy_write(cp, BROADCOM_MII_REG5, 0x0132);
  794. cas_phy_write(cp, BROADCOM_MII_REG7, 0x8006);
  795. cas_phy_write(cp, BROADCOM_MII_REG5, 0x0232);
  796. cas_phy_write(cp, BROADCOM_MII_REG7, 0x201F);
  797. cas_phy_write(cp, BROADCOM_MII_REG5, 0x0A20);
  798. } else if (PHY_BROADCOM_5411 == cp->phy_id) {
  799. val = cas_phy_read(cp, BROADCOM_MII_REG4);
  800. val = cas_phy_read(cp, BROADCOM_MII_REG4);
  801. if (val & 0x0080) {
  802. /* link workaround */
  803. cas_phy_write(cp, BROADCOM_MII_REG4,
  804. val & ~0x0080);
  805. }
  806. } else if (cp->cas_flags & CAS_FLAG_SATURN) {
  807. writel((cp->phy_type & CAS_PHY_MII_MDIO0) ?
  808. SATURN_PCFG_FSI : 0x0,
  809. cp->regs + REG_SATURN_PCFG);
  810. /* load firmware to address 10Mbps auto-negotiation
  811. * issue. NOTE: this will need to be changed if the
  812. * default firmware gets fixed.
  813. */
  814. if (PHY_NS_DP83065 == cp->phy_id) {
  815. cas_saturn_firmware_load(cp);
  816. }
  817. cas_phy_powerup(cp);
  818. }
  819. /* advertise capabilities */
  820. val = cas_phy_read(cp, MII_BMCR);
  821. val &= ~BMCR_ANENABLE;
  822. cas_phy_write(cp, MII_BMCR, val);
  823. udelay(10);
  824. cas_phy_write(cp, MII_ADVERTISE,
  825. cas_phy_read(cp, MII_ADVERTISE) |
  826. (ADVERTISE_10HALF | ADVERTISE_10FULL |
  827. ADVERTISE_100HALF | ADVERTISE_100FULL |
  828. CAS_ADVERTISE_PAUSE |
  829. CAS_ADVERTISE_ASYM_PAUSE));
  830. if (cp->cas_flags & CAS_FLAG_1000MB_CAP) {
  831. /* make sure that we don't advertise half
  832. * duplex to avoid a chip issue
  833. */
  834. val = cas_phy_read(cp, CAS_MII_1000_CTRL);
  835. val &= ~CAS_ADVERTISE_1000HALF;
  836. val |= CAS_ADVERTISE_1000FULL;
  837. cas_phy_write(cp, CAS_MII_1000_CTRL, val);
  838. }
  839. } else {
  840. /* reset pcs for serdes */
  841. u32 val;
  842. int limit;
  843. writel(PCS_DATAPATH_MODE_SERDES,
  844. cp->regs + REG_PCS_DATAPATH_MODE);
  845. /* enable serdes pins on saturn */
  846. if (cp->cas_flags & CAS_FLAG_SATURN)
  847. writel(0, cp->regs + REG_SATURN_PCFG);
  848. /* Reset PCS unit. */
  849. val = readl(cp->regs + REG_PCS_MII_CTRL);
  850. val |= PCS_MII_RESET;
  851. writel(val, cp->regs + REG_PCS_MII_CTRL);
  852. limit = STOP_TRIES;
  853. while (--limit > 0) {
  854. udelay(10);
  855. if ((readl(cp->regs + REG_PCS_MII_CTRL) &
  856. PCS_MII_RESET) == 0)
  857. break;
  858. }
  859. if (limit <= 0)
  860. netdev_warn(cp->dev, "PCS reset bit would not clear [%08x]\n",
  861. readl(cp->regs + REG_PCS_STATE_MACHINE));
  862. /* Make sure PCS is disabled while changing advertisement
  863. * configuration.
  864. */
  865. writel(0x0, cp->regs + REG_PCS_CFG);
  866. /* Advertise all capabilities except half-duplex. */
  867. val = readl(cp->regs + REG_PCS_MII_ADVERT);
  868. val &= ~PCS_MII_ADVERT_HD;
  869. val |= (PCS_MII_ADVERT_FD | PCS_MII_ADVERT_SYM_PAUSE |
  870. PCS_MII_ADVERT_ASYM_PAUSE);
  871. writel(val, cp->regs + REG_PCS_MII_ADVERT);
  872. /* enable PCS */
  873. writel(PCS_CFG_EN, cp->regs + REG_PCS_CFG);
  874. /* pcs workaround: enable sync detect */
  875. writel(PCS_SERDES_CTRL_SYNCD_EN,
  876. cp->regs + REG_PCS_SERDES_CTRL);
  877. }
  878. }
  879. static int cas_pcs_link_check(struct cas *cp)
  880. {
  881. u32 stat, state_machine;
  882. int retval = 0;
  883. /* The link status bit latches on zero, so you must
  884. * read it twice in such a case to see a transition
  885. * to the link being up.
  886. */
  887. stat = readl(cp->regs + REG_PCS_MII_STATUS);
  888. if ((stat & PCS_MII_STATUS_LINK_STATUS) == 0)
  889. stat = readl(cp->regs + REG_PCS_MII_STATUS);
  890. /* The remote-fault indication is only valid
  891. * when autoneg has completed.
  892. */
  893. if ((stat & (PCS_MII_STATUS_AUTONEG_COMP |
  894. PCS_MII_STATUS_REMOTE_FAULT)) ==
  895. (PCS_MII_STATUS_AUTONEG_COMP | PCS_MII_STATUS_REMOTE_FAULT))
  896. netif_info(cp, link, cp->dev, "PCS RemoteFault\n");
  897. /* work around link detection issue by querying the PCS state
  898. * machine directly.
  899. */
  900. state_machine = readl(cp->regs + REG_PCS_STATE_MACHINE);
  901. if ((state_machine & PCS_SM_LINK_STATE_MASK) != SM_LINK_STATE_UP) {
  902. stat &= ~PCS_MII_STATUS_LINK_STATUS;
  903. } else if (state_machine & PCS_SM_WORD_SYNC_STATE_MASK) {
  904. stat |= PCS_MII_STATUS_LINK_STATUS;
  905. }
  906. if (stat & PCS_MII_STATUS_LINK_STATUS) {
  907. if (cp->lstate != link_up) {
  908. if (cp->opened) {
  909. cp->lstate = link_up;
  910. cp->link_transition = LINK_TRANSITION_LINK_UP;
  911. cas_set_link_modes(cp);
  912. netif_carrier_on(cp->dev);
  913. }
  914. }
  915. } else if (cp->lstate == link_up) {
  916. cp->lstate = link_down;
  917. if (link_transition_timeout != 0 &&
  918. cp->link_transition != LINK_TRANSITION_REQUESTED_RESET &&
  919. !cp->link_transition_jiffies_valid) {
  920. /*
  921. * force a reset, as a workaround for the
  922. * link-failure problem. May want to move this to a
  923. * point a bit earlier in the sequence. If we had
  924. * generated a reset a short time ago, we'll wait for
  925. * the link timer to check the status until a
  926. * timer expires (link_transistion_jiffies_valid is
  927. * true when the timer is running.) Instead of using
  928. * a system timer, we just do a check whenever the
  929. * link timer is running - this clears the flag after
  930. * a suitable delay.
  931. */
  932. retval = 1;
  933. cp->link_transition = LINK_TRANSITION_REQUESTED_RESET;
  934. cp->link_transition_jiffies = jiffies;
  935. cp->link_transition_jiffies_valid = 1;
  936. } else {
  937. cp->link_transition = LINK_TRANSITION_ON_FAILURE;
  938. }
  939. netif_carrier_off(cp->dev);
  940. if (cp->opened)
  941. netif_info(cp, link, cp->dev, "PCS link down\n");
  942. /* Cassini only: if you force a mode, there can be
  943. * sync problems on link down. to fix that, the following
  944. * things need to be checked:
  945. * 1) read serialink state register
  946. * 2) read pcs status register to verify link down.
  947. * 3) if link down and serial link == 0x03, then you need
  948. * to global reset the chip.
  949. */
  950. if ((cp->cas_flags & CAS_FLAG_REG_PLUS) == 0) {
  951. /* should check to see if we're in a forced mode */
  952. stat = readl(cp->regs + REG_PCS_SERDES_STATE);
  953. if (stat == 0x03)
  954. return 1;
  955. }
  956. } else if (cp->lstate == link_down) {
  957. if (link_transition_timeout != 0 &&
  958. cp->link_transition != LINK_TRANSITION_REQUESTED_RESET &&
  959. !cp->link_transition_jiffies_valid) {
  960. /* force a reset, as a workaround for the
  961. * link-failure problem. May want to move
  962. * this to a point a bit earlier in the
  963. * sequence.
  964. */
  965. retval = 1;
  966. cp->link_transition = LINK_TRANSITION_REQUESTED_RESET;
  967. cp->link_transition_jiffies = jiffies;
  968. cp->link_transition_jiffies_valid = 1;
  969. } else {
  970. cp->link_transition = LINK_TRANSITION_STILL_FAILED;
  971. }
  972. }
  973. return retval;
  974. }
  975. static int cas_pcs_interrupt(struct net_device *dev,
  976. struct cas *cp, u32 status)
  977. {
  978. u32 stat = readl(cp->regs + REG_PCS_INTR_STATUS);
  979. if ((stat & PCS_INTR_STATUS_LINK_CHANGE) == 0)
  980. return 0;
  981. return cas_pcs_link_check(cp);
  982. }
  983. static int cas_txmac_interrupt(struct net_device *dev,
  984. struct cas *cp, u32 status)
  985. {
  986. u32 txmac_stat = readl(cp->regs + REG_MAC_TX_STATUS);
  987. if (!txmac_stat)
  988. return 0;
  989. netif_printk(cp, intr, KERN_DEBUG, cp->dev,
  990. "txmac interrupt, txmac_stat: 0x%x\n", txmac_stat);
  991. /* Defer timer expiration is quite normal,
  992. * don't even log the event.
  993. */
  994. if ((txmac_stat & MAC_TX_DEFER_TIMER) &&
  995. !(txmac_stat & ~MAC_TX_DEFER_TIMER))
  996. return 0;
  997. spin_lock(&cp->stat_lock[0]);
  998. if (txmac_stat & MAC_TX_UNDERRUN) {
  999. netdev_err(dev, "TX MAC xmit underrun\n");
  1000. cp->net_stats[0].tx_fifo_errors++;
  1001. }
  1002. if (txmac_stat & MAC_TX_MAX_PACKET_ERR) {
  1003. netdev_err(dev, "TX MAC max packet size error\n");
  1004. cp->net_stats[0].tx_errors++;
  1005. }
  1006. /* The rest are all cases of one of the 16-bit TX
  1007. * counters expiring.
  1008. */
  1009. if (txmac_stat & MAC_TX_COLL_NORMAL)
  1010. cp->net_stats[0].collisions += 0x10000;
  1011. if (txmac_stat & MAC_TX_COLL_EXCESS) {
  1012. cp->net_stats[0].tx_aborted_errors += 0x10000;
  1013. cp->net_stats[0].collisions += 0x10000;
  1014. }
  1015. if (txmac_stat & MAC_TX_COLL_LATE) {
  1016. cp->net_stats[0].tx_aborted_errors += 0x10000;
  1017. cp->net_stats[0].collisions += 0x10000;
  1018. }
  1019. spin_unlock(&cp->stat_lock[0]);
  1020. /* We do not keep track of MAC_TX_COLL_FIRST and
  1021. * MAC_TX_PEAK_ATTEMPTS events.
  1022. */
  1023. return 0;
  1024. }
  1025. static void cas_load_firmware(struct cas *cp, cas_hp_inst_t *firmware)
  1026. {
  1027. cas_hp_inst_t *inst;
  1028. u32 val;
  1029. int i;
  1030. i = 0;
  1031. while ((inst = firmware) && inst->note) {
  1032. writel(i, cp->regs + REG_HP_INSTR_RAM_ADDR);
  1033. val = CAS_BASE(HP_INSTR_RAM_HI_VAL, inst->val);
  1034. val |= CAS_BASE(HP_INSTR_RAM_HI_MASK, inst->mask);
  1035. writel(val, cp->regs + REG_HP_INSTR_RAM_DATA_HI);
  1036. val = CAS_BASE(HP_INSTR_RAM_MID_OUTARG, inst->outarg >> 10);
  1037. val |= CAS_BASE(HP_INSTR_RAM_MID_OUTOP, inst->outop);
  1038. val |= CAS_BASE(HP_INSTR_RAM_MID_FNEXT, inst->fnext);
  1039. val |= CAS_BASE(HP_INSTR_RAM_MID_FOFF, inst->foff);
  1040. val |= CAS_BASE(HP_INSTR_RAM_MID_SNEXT, inst->snext);
  1041. val |= CAS_BASE(HP_INSTR_RAM_MID_SOFF, inst->soff);
  1042. val |= CAS_BASE(HP_INSTR_RAM_MID_OP, inst->op);
  1043. writel(val, cp->regs + REG_HP_INSTR_RAM_DATA_MID);
  1044. val = CAS_BASE(HP_INSTR_RAM_LOW_OUTMASK, inst->outmask);
  1045. val |= CAS_BASE(HP_INSTR_RAM_LOW_OUTSHIFT, inst->outshift);
  1046. val |= CAS_BASE(HP_INSTR_RAM_LOW_OUTEN, inst->outenab);
  1047. val |= CAS_BASE(HP_INSTR_RAM_LOW_OUTARG, inst->outarg);
  1048. writel(val, cp->regs + REG_HP_INSTR_RAM_DATA_LOW);
  1049. ++firmware;
  1050. ++i;
  1051. }
  1052. }
  1053. static void cas_init_rx_dma(struct cas *cp)
  1054. {
  1055. u64 desc_dma = cp->block_dvma;
  1056. u32 val;
  1057. int i, size;
  1058. /* rx free descriptors */
  1059. val = CAS_BASE(RX_CFG_SWIVEL, RX_SWIVEL_OFF_VAL);
  1060. val |= CAS_BASE(RX_CFG_DESC_RING, RX_DESC_RINGN_INDEX(0));
  1061. val |= CAS_BASE(RX_CFG_COMP_RING, RX_COMP_RINGN_INDEX(0));
  1062. if ((N_RX_DESC_RINGS > 1) &&
  1063. (cp->cas_flags & CAS_FLAG_REG_PLUS)) /* do desc 2 */
  1064. val |= CAS_BASE(RX_CFG_DESC_RING1, RX_DESC_RINGN_INDEX(1));
  1065. writel(val, cp->regs + REG_RX_CFG);
  1066. val = (unsigned long) cp->init_rxds[0] -
  1067. (unsigned long) cp->init_block;
  1068. writel((desc_dma + val) >> 32, cp->regs + REG_RX_DB_HI);
  1069. writel((desc_dma + val) & 0xffffffff, cp->regs + REG_RX_DB_LOW);
  1070. writel(RX_DESC_RINGN_SIZE(0) - 4, cp->regs + REG_RX_KICK);
  1071. if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
  1072. /* rx desc 2 is for IPSEC packets. however,
  1073. * we don't it that for that purpose.
  1074. */
  1075. val = (unsigned long) cp->init_rxds[1] -
  1076. (unsigned long) cp->init_block;
  1077. writel((desc_dma + val) >> 32, cp->regs + REG_PLUS_RX_DB1_HI);
  1078. writel((desc_dma + val) & 0xffffffff, cp->regs +
  1079. REG_PLUS_RX_DB1_LOW);
  1080. writel(RX_DESC_RINGN_SIZE(1) - 4, cp->regs +
  1081. REG_PLUS_RX_KICK1);
  1082. }
  1083. /* rx completion registers */
  1084. val = (unsigned long) cp->init_rxcs[0] -
  1085. (unsigned long) cp->init_block;
  1086. writel((desc_dma + val) >> 32, cp->regs + REG_RX_CB_HI);
  1087. writel((desc_dma + val) & 0xffffffff, cp->regs + REG_RX_CB_LOW);
  1088. if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
  1089. /* rx comp 2-4 */
  1090. for (i = 1; i < MAX_RX_COMP_RINGS; i++) {
  1091. val = (unsigned long) cp->init_rxcs[i] -
  1092. (unsigned long) cp->init_block;
  1093. writel((desc_dma + val) >> 32, cp->regs +
  1094. REG_PLUS_RX_CBN_HI(i));
  1095. writel((desc_dma + val) & 0xffffffff, cp->regs +
  1096. REG_PLUS_RX_CBN_LOW(i));
  1097. }
  1098. }
  1099. /* read selective clear regs to prevent spurious interrupts
  1100. * on reset because complete == kick.
  1101. * selective clear set up to prevent interrupts on resets
  1102. */
  1103. readl(cp->regs + REG_INTR_STATUS_ALIAS);
  1104. writel(INTR_RX_DONE | INTR_RX_BUF_UNAVAIL, cp->regs + REG_ALIAS_CLEAR);
  1105. if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
  1106. for (i = 1; i < N_RX_COMP_RINGS; i++)
  1107. readl(cp->regs + REG_PLUS_INTRN_STATUS_ALIAS(i));
  1108. /* 2 is different from 3 and 4 */
  1109. if (N_RX_COMP_RINGS > 1)
  1110. writel(INTR_RX_DONE_ALT | INTR_RX_BUF_UNAVAIL_1,
  1111. cp->regs + REG_PLUS_ALIASN_CLEAR(1));
  1112. for (i = 2; i < N_RX_COMP_RINGS; i++)
  1113. writel(INTR_RX_DONE_ALT,
  1114. cp->regs + REG_PLUS_ALIASN_CLEAR(i));
  1115. }
  1116. /* set up pause thresholds */
  1117. val = CAS_BASE(RX_PAUSE_THRESH_OFF,
  1118. cp->rx_pause_off / RX_PAUSE_THRESH_QUANTUM);
  1119. val |= CAS_BASE(RX_PAUSE_THRESH_ON,
  1120. cp->rx_pause_on / RX_PAUSE_THRESH_QUANTUM);
  1121. writel(val, cp->regs + REG_RX_PAUSE_THRESH);
  1122. /* zero out dma reassembly buffers */
  1123. for (i = 0; i < 64; i++) {
  1124. writel(i, cp->regs + REG_RX_TABLE_ADDR);
  1125. writel(0x0, cp->regs + REG_RX_TABLE_DATA_LOW);
  1126. writel(0x0, cp->regs + REG_RX_TABLE_DATA_MID);
  1127. writel(0x0, cp->regs + REG_RX_TABLE_DATA_HI);
  1128. }
  1129. /* make sure address register is 0 for normal operation */
  1130. writel(0x0, cp->regs + REG_RX_CTRL_FIFO_ADDR);
  1131. writel(0x0, cp->regs + REG_RX_IPP_FIFO_ADDR);
  1132. /* interrupt mitigation */
  1133. #ifdef USE_RX_BLANK
  1134. val = CAS_BASE(RX_BLANK_INTR_TIME, RX_BLANK_INTR_TIME_VAL);
  1135. val |= CAS_BASE(RX_BLANK_INTR_PKT, RX_BLANK_INTR_PKT_VAL);
  1136. writel(val, cp->regs + REG_RX_BLANK);
  1137. #else
  1138. writel(0x0, cp->regs + REG_RX_BLANK);
  1139. #endif
  1140. /* interrupt generation as a function of low water marks for
  1141. * free desc and completion entries. these are used to trigger
  1142. * housekeeping for rx descs. we don't use the free interrupt
  1143. * as it's not very useful
  1144. */
  1145. /* val = CAS_BASE(RX_AE_THRESH_FREE, RX_AE_FREEN_VAL(0)); */
  1146. val = CAS_BASE(RX_AE_THRESH_COMP, RX_AE_COMP_VAL);
  1147. writel(val, cp->regs + REG_RX_AE_THRESH);
  1148. if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
  1149. val = CAS_BASE(RX_AE1_THRESH_FREE, RX_AE_FREEN_VAL(1));
  1150. writel(val, cp->regs + REG_PLUS_RX_AE1_THRESH);
  1151. }
  1152. /* Random early detect registers. useful for congestion avoidance.
  1153. * this should be tunable.
  1154. */
  1155. writel(0x0, cp->regs + REG_RX_RED);
  1156. /* receive page sizes. default == 2K (0x800) */
  1157. val = 0;
  1158. if (cp->page_size == 0x1000)
  1159. val = 0x1;
  1160. else if (cp->page_size == 0x2000)
  1161. val = 0x2;
  1162. else if (cp->page_size == 0x4000)
  1163. val = 0x3;
  1164. /* round mtu + offset. constrain to page size. */
  1165. size = cp->dev->mtu + 64;
  1166. if (size > cp->page_size)
  1167. size = cp->page_size;
  1168. if (size <= 0x400)
  1169. i = 0x0;
  1170. else if (size <= 0x800)
  1171. i = 0x1;
  1172. else if (size <= 0x1000)
  1173. i = 0x2;
  1174. else
  1175. i = 0x3;
  1176. cp->mtu_stride = 1 << (i + 10);
  1177. val = CAS_BASE(RX_PAGE_SIZE, val);
  1178. val |= CAS_BASE(RX_PAGE_SIZE_MTU_STRIDE, i);
  1179. val |= CAS_BASE(RX_PAGE_SIZE_MTU_COUNT, cp->page_size >> (i + 10));
  1180. val |= CAS_BASE(RX_PAGE_SIZE_MTU_OFF, 0x1);
  1181. writel(val, cp->regs + REG_RX_PAGE_SIZE);
  1182. /* enable the header parser if desired */
  1183. if (CAS_HP_FIRMWARE == cas_prog_null)
  1184. return;
  1185. val = CAS_BASE(HP_CFG_NUM_CPU, CAS_NCPUS > 63 ? 0 : CAS_NCPUS);
  1186. val |= HP_CFG_PARSE_EN | HP_CFG_SYN_INC_MASK;
  1187. val |= CAS_BASE(HP_CFG_TCP_THRESH, HP_TCP_THRESH_VAL);
  1188. writel(val, cp->regs + REG_HP_CFG);
  1189. }
  1190. static inline void cas_rxc_init(struct cas_rx_comp *rxc)
  1191. {
  1192. memset(rxc, 0, sizeof(*rxc));
  1193. rxc->word4 = cpu_to_le64(RX_COMP4_ZERO);
  1194. }
  1195. /* NOTE: we use the ENC RX DESC ring for spares. the rx_page[0,1]
  1196. * flipping is protected by the fact that the chip will not
  1197. * hand back the same page index while it's being processed.
  1198. */
  1199. static inline cas_page_t *cas_page_spare(struct cas *cp, const int index)
  1200. {
  1201. cas_page_t *page = cp->rx_pages[1][index];
  1202. cas_page_t *new;
  1203. if (page_count(page->buffer) == 1)
  1204. return page;
  1205. new = cas_page_dequeue(cp);
  1206. if (new) {
  1207. spin_lock(&cp->rx_inuse_lock);
  1208. list_add(&page->list, &cp->rx_inuse_list);
  1209. spin_unlock(&cp->rx_inuse_lock);
  1210. }
  1211. return new;
  1212. }
  1213. /* this needs to be changed if we actually use the ENC RX DESC ring */
  1214. static cas_page_t *cas_page_swap(struct cas *cp, const int ring,
  1215. const int index)
  1216. {
  1217. cas_page_t **page0 = cp->rx_pages[0];
  1218. cas_page_t **page1 = cp->rx_pages[1];
  1219. /* swap if buffer is in use */
  1220. if (page_count(page0[index]->buffer) > 1) {
  1221. cas_page_t *new = cas_page_spare(cp, index);
  1222. if (new) {
  1223. page1[index] = page0[index];
  1224. page0[index] = new;
  1225. }
  1226. }
  1227. RX_USED_SET(page0[index], 0);
  1228. return page0[index];
  1229. }
  1230. static void cas_clean_rxds(struct cas *cp)
  1231. {
  1232. /* only clean ring 0 as ring 1 is used for spare buffers */
  1233. struct cas_rx_desc *rxd = cp->init_rxds[0];
  1234. int i, size;
  1235. /* release all rx flows */
  1236. for (i = 0; i < N_RX_FLOWS; i++) {
  1237. struct sk_buff *skb;
  1238. while ((skb = __skb_dequeue(&cp->rx_flows[i]))) {
  1239. cas_skb_release(skb);
  1240. }
  1241. }
  1242. /* initialize descriptors */
  1243. size = RX_DESC_RINGN_SIZE(0);
  1244. for (i = 0; i < size; i++) {
  1245. cas_page_t *page = cas_page_swap(cp, 0, i);
  1246. rxd[i].buffer = cpu_to_le64(page->dma_addr);
  1247. rxd[i].index = cpu_to_le64(CAS_BASE(RX_INDEX_NUM, i) |
  1248. CAS_BASE(RX_INDEX_RING, 0));
  1249. }
  1250. cp->rx_old[0] = RX_DESC_RINGN_SIZE(0) - 4;
  1251. cp->rx_last[0] = 0;
  1252. cp->cas_flags &= ~CAS_FLAG_RXD_POST(0);
  1253. }
  1254. static void cas_clean_rxcs(struct cas *cp)
  1255. {
  1256. int i, j;
  1257. /* take ownership of rx comp descriptors */
  1258. memset(cp->rx_cur, 0, sizeof(*cp->rx_cur)*N_RX_COMP_RINGS);
  1259. memset(cp->rx_new, 0, sizeof(*cp->rx_new)*N_RX_COMP_RINGS);
  1260. for (i = 0; i < N_RX_COMP_RINGS; i++) {
  1261. struct cas_rx_comp *rxc = cp->init_rxcs[i];
  1262. for (j = 0; j < RX_COMP_RINGN_SIZE(i); j++) {
  1263. cas_rxc_init(rxc + j);
  1264. }
  1265. }
  1266. }
  1267. #if 0
  1268. /* When we get a RX fifo overflow, the RX unit is probably hung
  1269. * so we do the following.
  1270. *
  1271. * If any part of the reset goes wrong, we return 1 and that causes the
  1272. * whole chip to be reset.
  1273. */
  1274. static int cas_rxmac_reset(struct cas *cp)
  1275. {
  1276. struct net_device *dev = cp->dev;
  1277. int limit;
  1278. u32 val;
  1279. /* First, reset MAC RX. */
  1280. writel(cp->mac_rx_cfg & ~MAC_RX_CFG_EN, cp->regs + REG_MAC_RX_CFG);
  1281. for (limit = 0; limit < STOP_TRIES; limit++) {
  1282. if (!(readl(cp->regs + REG_MAC_RX_CFG) & MAC_RX_CFG_EN))
  1283. break;
  1284. udelay(10);
  1285. }
  1286. if (limit == STOP_TRIES) {
  1287. netdev_err(dev, "RX MAC will not disable, resetting whole chip\n");
  1288. return 1;
  1289. }
  1290. /* Second, disable RX DMA. */
  1291. writel(0, cp->regs + REG_RX_CFG);
  1292. for (limit = 0; limit < STOP_TRIES; limit++) {
  1293. if (!(readl(cp->regs + REG_RX_CFG) & RX_CFG_DMA_EN))
  1294. break;
  1295. udelay(10);
  1296. }
  1297. if (limit == STOP_TRIES) {
  1298. netdev_err(dev, "RX DMA will not disable, resetting whole chip\n");
  1299. return 1;
  1300. }
  1301. mdelay(5);
  1302. /* Execute RX reset command. */
  1303. writel(SW_RESET_RX, cp->regs + REG_SW_RESET);
  1304. for (limit = 0; limit < STOP_TRIES; limit++) {
  1305. if (!(readl(cp->regs + REG_SW_RESET) & SW_RESET_RX))
  1306. break;
  1307. udelay(10);
  1308. }
  1309. if (limit == STOP_TRIES) {
  1310. netdev_err(dev, "RX reset command will not execute, resetting whole chip\n");
  1311. return 1;
  1312. }
  1313. /* reset driver rx state */
  1314. cas_clean_rxds(cp);
  1315. cas_clean_rxcs(cp);
  1316. /* Now, reprogram the rest of RX unit. */
  1317. cas_init_rx_dma(cp);
  1318. /* re-enable */
  1319. val = readl(cp->regs + REG_RX_CFG);
  1320. writel(val | RX_CFG_DMA_EN, cp->regs + REG_RX_CFG);
  1321. writel(MAC_RX_FRAME_RECV, cp->regs + REG_MAC_RX_MASK);
  1322. val = readl(cp->regs + REG_MAC_RX_CFG);
  1323. writel(val | MAC_RX_CFG_EN, cp->regs + REG_MAC_RX_CFG);
  1324. return 0;
  1325. }
  1326. #endif
  1327. static int cas_rxmac_interrupt(struct net_device *dev, struct cas *cp,
  1328. u32 status)
  1329. {
  1330. u32 stat = readl(cp->regs + REG_MAC_RX_STATUS);
  1331. if (!stat)
  1332. return 0;
  1333. netif_dbg(cp, intr, cp->dev, "rxmac interrupt, stat: 0x%x\n", stat);
  1334. /* these are all rollovers */
  1335. spin_lock(&cp->stat_lock[0]);
  1336. if (stat & MAC_RX_ALIGN_ERR)
  1337. cp->net_stats[0].rx_frame_errors += 0x10000;
  1338. if (stat & MAC_RX_CRC_ERR)
  1339. cp->net_stats[0].rx_crc_errors += 0x10000;
  1340. if (stat & MAC_RX_LEN_ERR)
  1341. cp->net_stats[0].rx_length_errors += 0x10000;
  1342. if (stat & MAC_RX_OVERFLOW) {
  1343. cp->net_stats[0].rx_over_errors++;
  1344. cp->net_stats[0].rx_fifo_errors++;
  1345. }
  1346. /* We do not track MAC_RX_FRAME_COUNT and MAC_RX_VIOL_ERR
  1347. * events.
  1348. */
  1349. spin_unlock(&cp->stat_lock[0]);
  1350. return 0;
  1351. }
  1352. static int cas_mac_interrupt(struct net_device *dev, struct cas *cp,
  1353. u32 status)
  1354. {
  1355. u32 stat = readl(cp->regs + REG_MAC_CTRL_STATUS);
  1356. if (!stat)
  1357. return 0;
  1358. netif_printk(cp, intr, KERN_DEBUG, cp->dev,
  1359. "mac interrupt, stat: 0x%x\n", stat);
  1360. /* This interrupt is just for pause frame and pause
  1361. * tracking. It is useful for diagnostics and debug
  1362. * but probably by default we will mask these events.
  1363. */
  1364. if (stat & MAC_CTRL_PAUSE_STATE)
  1365. cp->pause_entered++;
  1366. if (stat & MAC_CTRL_PAUSE_RECEIVED)
  1367. cp->pause_last_time_recvd = (stat >> 16);
  1368. return 0;
  1369. }
  1370. /* Must be invoked under cp->lock. */
  1371. static inline int cas_mdio_link_not_up(struct cas *cp)
  1372. {
  1373. u16 val;
  1374. switch (cp->lstate) {
  1375. case link_force_ret:
  1376. netif_info(cp, link, cp->dev, "Autoneg failed again, keeping forced mode\n");
  1377. cas_phy_write(cp, MII_BMCR, cp->link_fcntl);
  1378. cp->timer_ticks = 5;
  1379. cp->lstate = link_force_ok;
  1380. cp->link_transition = LINK_TRANSITION_LINK_CONFIG;
  1381. break;
  1382. case link_aneg:
  1383. val = cas_phy_read(cp, MII_BMCR);
  1384. /* Try forced modes. we try things in the following order:
  1385. * 1000 full -> 100 full/half -> 10 half
  1386. */
  1387. val &= ~(BMCR_ANRESTART | BMCR_ANENABLE);
  1388. val |= BMCR_FULLDPLX;
  1389. val |= (cp->cas_flags & CAS_FLAG_1000MB_CAP) ?
  1390. CAS_BMCR_SPEED1000 : BMCR_SPEED100;
  1391. cas_phy_write(cp, MII_BMCR, val);
  1392. cp->timer_ticks = 5;
  1393. cp->lstate = link_force_try;
  1394. cp->link_transition = LINK_TRANSITION_LINK_CONFIG;
  1395. break;
  1396. case link_force_try:
  1397. /* Downgrade from 1000 to 100 to 10 Mbps if necessary. */
  1398. val = cas_phy_read(cp, MII_BMCR);
  1399. cp->timer_ticks = 5;
  1400. if (val & CAS_BMCR_SPEED1000) { /* gigabit */
  1401. val &= ~CAS_BMCR_SPEED1000;
  1402. val |= (BMCR_SPEED100 | BMCR_FULLDPLX);
  1403. cas_phy_write(cp, MII_BMCR, val);
  1404. break;
  1405. }
  1406. if (val & BMCR_SPEED100) {
  1407. if (val & BMCR_FULLDPLX) /* fd failed */
  1408. val &= ~BMCR_FULLDPLX;
  1409. else { /* 100Mbps failed */
  1410. val &= ~BMCR_SPEED100;
  1411. }
  1412. cas_phy_write(cp, MII_BMCR, val);
  1413. break;
  1414. }
  1415. default:
  1416. break;
  1417. }
  1418. return 0;
  1419. }
  1420. /* must be invoked with cp->lock held */
  1421. static int cas_mii_link_check(struct cas *cp, const u16 bmsr)
  1422. {
  1423. int restart;
  1424. if (bmsr & BMSR_LSTATUS) {
  1425. /* Ok, here we got a link. If we had it due to a forced
  1426. * fallback, and we were configured for autoneg, we
  1427. * retry a short autoneg pass. If you know your hub is
  1428. * broken, use ethtool ;)
  1429. */
  1430. if ((cp->lstate == link_force_try) &&
  1431. (cp->link_cntl & BMCR_ANENABLE)) {
  1432. cp->lstate = link_force_ret;
  1433. cp->link_transition = LINK_TRANSITION_LINK_CONFIG;
  1434. cas_mif_poll(cp, 0);
  1435. cp->link_fcntl = cas_phy_read(cp, MII_BMCR);
  1436. cp->timer_ticks = 5;
  1437. if (cp->opened)
  1438. netif_info(cp, link, cp->dev,
  1439. "Got link after fallback, retrying autoneg once...\n");
  1440. cas_phy_write(cp, MII_BMCR,
  1441. cp->link_fcntl | BMCR_ANENABLE |
  1442. BMCR_ANRESTART);
  1443. cas_mif_poll(cp, 1);
  1444. } else if (cp->lstate != link_up) {
  1445. cp->lstate = link_up;
  1446. cp->link_transition = LINK_TRANSITION_LINK_UP;
  1447. if (cp->opened) {
  1448. cas_set_link_modes(cp);
  1449. netif_carrier_on(cp->dev);
  1450. }
  1451. }
  1452. return 0;
  1453. }
  1454. /* link not up. if the link was previously up, we restart the
  1455. * whole process
  1456. */
  1457. restart = 0;
  1458. if (cp->lstate == link_up) {
  1459. cp->lstate = link_down;
  1460. cp->link_transition = LINK_TRANSITION_LINK_DOWN;
  1461. netif_carrier_off(cp->dev);
  1462. if (cp->opened)
  1463. netif_info(cp, link, cp->dev, "Link down\n");
  1464. restart = 1;
  1465. } else if (++cp->timer_ticks > 10)
  1466. cas_mdio_link_not_up(cp);
  1467. return restart;
  1468. }
  1469. static int cas_mif_interrupt(struct net_device *dev, struct cas *cp,
  1470. u32 status)
  1471. {
  1472. u32 stat = readl(cp->regs + REG_MIF_STATUS);
  1473. u16 bmsr;
  1474. /* check for a link change */
  1475. if (CAS_VAL(MIF_STATUS_POLL_STATUS, stat) == 0)
  1476. return 0;
  1477. bmsr = CAS_VAL(MIF_STATUS_POLL_DATA, stat);
  1478. return cas_mii_link_check(cp, bmsr);
  1479. }
  1480. static int cas_pci_interrupt(struct net_device *dev, struct cas *cp,
  1481. u32 status)
  1482. {
  1483. u32 stat = readl(cp->regs + REG_PCI_ERR_STATUS);
  1484. if (!stat)
  1485. return 0;
  1486. netdev_err(dev, "PCI error [%04x:%04x]",
  1487. stat, readl(cp->regs + REG_BIM_DIAG));
  1488. /* cassini+ has this reserved */
  1489. if ((stat & PCI_ERR_BADACK) &&
  1490. ((cp->cas_flags & CAS_FLAG_REG_PLUS) == 0))
  1491. pr_cont(" <No ACK64# during ABS64 cycle>");
  1492. if (stat & PCI_ERR_DTRTO)
  1493. pr_cont(" <Delayed transaction timeout>");
  1494. if (stat & PCI_ERR_OTHER)
  1495. pr_cont(" <other>");
  1496. if (stat & PCI_ERR_BIM_DMA_WRITE)
  1497. pr_cont(" <BIM DMA 0 write req>");
  1498. if (stat & PCI_ERR_BIM_DMA_READ)
  1499. pr_cont(" <BIM DMA 0 read req>");
  1500. pr_cont("\n");
  1501. if (stat & PCI_ERR_OTHER) {
  1502. u16 cfg;
  1503. /* Interrogate PCI config space for the
  1504. * true cause.
  1505. */
  1506. pci_read_config_word(cp->pdev, PCI_STATUS, &cfg);
  1507. netdev_err(dev, "Read PCI cfg space status [%04x]\n", cfg);
  1508. if (cfg & PCI_STATUS_PARITY)
  1509. netdev_err(dev, "PCI parity error detected\n");
  1510. if (cfg & PCI_STATUS_SIG_TARGET_ABORT)
  1511. netdev_err(dev, "PCI target abort\n");
  1512. if (cfg & PCI_STATUS_REC_TARGET_ABORT)
  1513. netdev_err(dev, "PCI master acks target abort\n");
  1514. if (cfg & PCI_STATUS_REC_MASTER_ABORT)
  1515. netdev_err(dev, "PCI master abort\n");
  1516. if (cfg & PCI_STATUS_SIG_SYSTEM_ERROR)
  1517. netdev_err(dev, "PCI system error SERR#\n");
  1518. if (cfg & PCI_STATUS_DETECTED_PARITY)
  1519. netdev_err(dev, "PCI parity error\n");
  1520. /* Write the error bits back to clear them. */
  1521. cfg &= (PCI_STATUS_PARITY |
  1522. PCI_STATUS_SIG_TARGET_ABORT |
  1523. PCI_STATUS_REC_TARGET_ABORT |
  1524. PCI_STATUS_REC_MASTER_ABORT |
  1525. PCI_STATUS_SIG_SYSTEM_ERROR |
  1526. PCI_STATUS_DETECTED_PARITY);
  1527. pci_write_config_word(cp->pdev, PCI_STATUS, cfg);
  1528. }
  1529. /* For all PCI errors, we should reset the chip. */
  1530. return 1;
  1531. }
  1532. /* All non-normal interrupt conditions get serviced here.
  1533. * Returns non-zero if we should just exit the interrupt
  1534. * handler right now (ie. if we reset the card which invalidates
  1535. * all of the other original irq status bits).
  1536. */
  1537. static int cas_abnormal_irq(struct net_device *dev, struct cas *cp,
  1538. u32 status)
  1539. {
  1540. if (status & INTR_RX_TAG_ERROR) {
  1541. /* corrupt RX tag framing */
  1542. netif_printk(cp, rx_err, KERN_DEBUG, cp->dev,
  1543. "corrupt rx tag framing\n");
  1544. spin_lock(&cp->stat_lock[0]);
  1545. cp->net_stats[0].rx_errors++;
  1546. spin_unlock(&cp->stat_lock[0]);
  1547. goto do_reset;
  1548. }
  1549. if (status & INTR_RX_LEN_MISMATCH) {
  1550. /* length mismatch. */
  1551. netif_printk(cp, rx_err, KERN_DEBUG, cp->dev,
  1552. "length mismatch for rx frame\n");
  1553. spin_lock(&cp->stat_lock[0]);
  1554. cp->net_stats[0].rx_errors++;
  1555. spin_unlock(&cp->stat_lock[0]);
  1556. goto do_reset;
  1557. }
  1558. if (status & INTR_PCS_STATUS) {
  1559. if (cas_pcs_interrupt(dev, cp, status))
  1560. goto do_reset;
  1561. }
  1562. if (status & INTR_TX_MAC_STATUS) {
  1563. if (cas_txmac_interrupt(dev, cp, status))
  1564. goto do_reset;
  1565. }
  1566. if (status & INTR_RX_MAC_STATUS) {
  1567. if (cas_rxmac_interrupt(dev, cp, status))
  1568. goto do_reset;
  1569. }
  1570. if (status & INTR_MAC_CTRL_STATUS) {
  1571. if (cas_mac_interrupt(dev, cp, status))
  1572. goto do_reset;
  1573. }
  1574. if (status & INTR_MIF_STATUS) {
  1575. if (cas_mif_interrupt(dev, cp, status))
  1576. goto do_reset;
  1577. }
  1578. if (status & INTR_PCI_ERROR_STATUS) {
  1579. if (cas_pci_interrupt(dev, cp, status))
  1580. goto do_reset;
  1581. }
  1582. return 0;
  1583. do_reset:
  1584. #if 1
  1585. atomic_inc(&cp->reset_task_pending);
  1586. atomic_inc(&cp->reset_task_pending_all);
  1587. netdev_err(dev, "reset called in cas_abnormal_irq [0x%x]\n", status);
  1588. schedule_work(&cp->reset_task);
  1589. #else
  1590. atomic_set(&cp->reset_task_pending, CAS_RESET_ALL);
  1591. netdev_err(dev, "reset called in cas_abnormal_irq\n");
  1592. schedule_work(&cp->reset_task);
  1593. #endif
  1594. return 1;
  1595. }
  1596. /* NOTE: CAS_TABORT returns 1 or 2 so that it can be used when
  1597. * determining whether to do a netif_stop/wakeup
  1598. */
  1599. #define CAS_TABORT(x) (((x)->cas_flags & CAS_FLAG_TARGET_ABORT) ? 2 : 1)
  1600. #define CAS_ROUND_PAGE(x) (((x) + PAGE_SIZE - 1) & PAGE_MASK)
  1601. static inline int cas_calc_tabort(struct cas *cp, const unsigned long addr,
  1602. const int len)
  1603. {
  1604. unsigned long off = addr + len;
  1605. if (CAS_TABORT(cp) == 1)
  1606. return 0;
  1607. if ((CAS_ROUND_PAGE(off) - off) > TX_TARGET_ABORT_LEN)
  1608. return 0;
  1609. return TX_TARGET_ABORT_LEN;
  1610. }
  1611. static inline void cas_tx_ringN(struct cas *cp, int ring, int limit)
  1612. {
  1613. struct cas_tx_desc *txds;
  1614. struct sk_buff **skbs;
  1615. struct net_device *dev = cp->dev;
  1616. int entry, count;
  1617. spin_lock(&cp->tx_lock[ring]);
  1618. txds = cp->init_txds[ring];
  1619. skbs = cp->tx_skbs[ring];
  1620. entry = cp->tx_old[ring];
  1621. count = TX_BUFF_COUNT(ring, entry, limit);
  1622. while (entry != limit) {
  1623. struct sk_buff *skb = skbs[entry];
  1624. dma_addr_t daddr;
  1625. u32 dlen;
  1626. int frag;
  1627. if (!skb) {
  1628. /* this should never occur */
  1629. entry = TX_DESC_NEXT(ring, entry);
  1630. continue;
  1631. }
  1632. /* however, we might get only a partial skb release. */
  1633. count -= skb_shinfo(skb)->nr_frags +
  1634. + cp->tx_tiny_use[ring][entry].nbufs + 1;
  1635. if (count < 0)
  1636. break;
  1637. netif_printk(cp, tx_done, KERN_DEBUG, cp->dev,
  1638. "tx[%d] done, slot %d\n", ring, entry);
  1639. skbs[entry] = NULL;
  1640. cp->tx_tiny_use[ring][entry].nbufs = 0;
  1641. for (frag = 0; frag <= skb_shinfo(skb)->nr_frags; frag++) {
  1642. struct cas_tx_desc *txd = txds + entry;
  1643. daddr = le64_to_cpu(txd->buffer);
  1644. dlen = CAS_VAL(TX_DESC_BUFLEN,
  1645. le64_to_cpu(txd->control));
  1646. pci_unmap_page(cp->pdev, daddr, dlen,
  1647. PCI_DMA_TODEVICE);
  1648. entry = TX_DESC_NEXT(ring, entry);
  1649. /* tiny buffer may follow */
  1650. if (cp->tx_tiny_use[ring][entry].used) {
  1651. cp->tx_tiny_use[ring][entry].used = 0;
  1652. entry = TX_DESC_NEXT(ring, entry);
  1653. }
  1654. }
  1655. spin_lock(&cp->stat_lock[ring]);
  1656. cp->net_stats[ring].tx_packets++;
  1657. cp->net_stats[ring].tx_bytes += skb->len;
  1658. spin_unlock(&cp->stat_lock[ring]);
  1659. dev_kfree_skb_irq(skb);
  1660. }
  1661. cp->tx_old[ring] = entry;
  1662. /* this is wrong for multiple tx rings. the net device needs
  1663. * multiple queues for this to do the right thing. we wait
  1664. * for 2*packets to be available when using tiny buffers
  1665. */
  1666. if (netif_queue_stopped(dev) &&
  1667. (TX_BUFFS_AVAIL(cp, ring) > CAS_TABORT(cp)*(MAX_SKB_FRAGS + 1)))
  1668. netif_wake_queue(dev);
  1669. spin_unlock(&cp->tx_lock[ring]);
  1670. }
  1671. static void cas_tx(struct net_device *dev, struct cas *cp,
  1672. u32 status)
  1673. {
  1674. int limit, ring;
  1675. #ifdef USE_TX_COMPWB
  1676. u64 compwb = le64_to_cpu(cp->init_block->tx_compwb);
  1677. #endif
  1678. netif_printk(cp, intr, KERN_DEBUG, cp->dev,
  1679. "tx interrupt, status: 0x%x, %llx\n",
  1680. status, (unsigned long long)compwb);
  1681. /* process all the rings */
  1682. for (ring = 0; ring < N_TX_RINGS; ring++) {
  1683. #ifdef USE_TX_COMPWB
  1684. /* use the completion writeback registers */
  1685. limit = (CAS_VAL(TX_COMPWB_MSB, compwb) << 8) |
  1686. CAS_VAL(TX_COMPWB_LSB, compwb);
  1687. compwb = TX_COMPWB_NEXT(compwb);
  1688. #else
  1689. limit = readl(cp->regs + REG_TX_COMPN(ring));
  1690. #endif
  1691. if (cp->tx_old[ring] != limit)
  1692. cas_tx_ringN(cp, ring, limit);
  1693. }
  1694. }
  1695. static int cas_rx_process_pkt(struct cas *cp, struct cas_rx_comp *rxc,
  1696. int entry, const u64 *words,
  1697. struct sk_buff **skbref)
  1698. {
  1699. int dlen, hlen, len, i, alloclen;
  1700. int off, swivel = RX_SWIVEL_OFF_VAL;
  1701. struct cas_page *page;
  1702. struct sk_buff *skb;
  1703. void *addr, *crcaddr;
  1704. __sum16 csum;
  1705. char *p;
  1706. hlen = CAS_VAL(RX_COMP2_HDR_SIZE, words[1]);
  1707. dlen = CAS_VAL(RX_COMP1_DATA_SIZE, words[0]);
  1708. len = hlen + dlen;
  1709. if (RX_COPY_ALWAYS || (words[2] & RX_COMP3_SMALL_PKT))
  1710. alloclen = len;
  1711. else
  1712. alloclen = max(hlen, RX_COPY_MIN);
  1713. skb = dev_alloc_skb(alloclen + swivel + cp->crc_size);
  1714. if (skb == NULL)
  1715. return -1;
  1716. *skbref = skb;
  1717. skb_reserve(skb, swivel);
  1718. p = skb->data;
  1719. addr = crcaddr = NULL;
  1720. if (hlen) { /* always copy header pages */
  1721. i = CAS_VAL(RX_COMP2_HDR_INDEX, words[1]);
  1722. page = cp->rx_pages[CAS_VAL(RX_INDEX_RING, i)][CAS_VAL(RX_INDEX_NUM, i)];
  1723. off = CAS_VAL(RX_COMP2_HDR_OFF, words[1]) * 0x100 +
  1724. swivel;
  1725. i = hlen;
  1726. if (!dlen) /* attach FCS */
  1727. i += cp->crc_size;
  1728. pci_dma_sync_single_for_cpu(cp->pdev, page->dma_addr + off, i,
  1729. PCI_DMA_FROMDEVICE);
  1730. addr = cas_page_map(page->buffer);
  1731. memcpy(p, addr + off, i);
  1732. pci_dma_sync_single_for_device(cp->pdev, page->dma_addr + off, i,
  1733. PCI_DMA_FROMDEVICE);
  1734. cas_page_unmap(addr);
  1735. RX_USED_ADD(page, 0x100);
  1736. p += hlen;
  1737. swivel = 0;
  1738. }
  1739. if (alloclen < (hlen + dlen)) {
  1740. skb_frag_t *frag = skb_shinfo(skb)->frags;
  1741. /* normal or jumbo packets. we use frags */
  1742. i = CAS_VAL(RX_COMP1_DATA_INDEX, words[0]);
  1743. page = cp->rx_pages[CAS_VAL(RX_INDEX_RING, i)][CAS_VAL(RX_INDEX_NUM, i)];
  1744. off = CAS_VAL(RX_COMP1_DATA_OFF, words[0]) + swivel;
  1745. hlen = min(cp->page_size - off, dlen);
  1746. if (hlen < 0) {
  1747. netif_printk(cp, rx_err, KERN_DEBUG, cp->dev,
  1748. "rx page overflow: %d\n", hlen);
  1749. dev_kfree_skb_irq(skb);
  1750. return -1;
  1751. }
  1752. i = hlen;
  1753. if (i == dlen) /* attach FCS */
  1754. i += cp->crc_size;
  1755. pci_dma_sync_single_for_cpu(cp->pdev, page->dma_addr + off, i,
  1756. PCI_DMA_FROMDEVICE);
  1757. /* make sure we always copy a header */
  1758. swivel = 0;
  1759. if (p == (char *) skb->data) { /* not split */
  1760. addr = cas_page_map(page->buffer);
  1761. memcpy(p, addr + off, RX_COPY_MIN);
  1762. pci_dma_sync_single_for_device(cp->pdev, page->dma_addr + off, i,
  1763. PCI_DMA_FROMDEVICE);
  1764. cas_page_unmap(addr);
  1765. off += RX_COPY_MIN;
  1766. swivel = RX_COPY_MIN;
  1767. RX_USED_ADD(page, cp->mtu_stride);
  1768. } else {
  1769. RX_USED_ADD(page, hlen);
  1770. }
  1771. skb_put(skb, alloclen);
  1772. skb_shinfo(skb)->nr_frags++;
  1773. skb->data_len += hlen - swivel;
  1774. skb->truesize += hlen - swivel;
  1775. skb->len += hlen - swivel;
  1776. get_page(page->buffer);
  1777. frag->page = page->buffer;
  1778. frag->page_offset = off;
  1779. frag->size = hlen - swivel;
  1780. /* any more data? */
  1781. if ((words[0] & RX_COMP1_SPLIT_PKT) && ((dlen -= hlen) > 0)) {
  1782. hlen = dlen;
  1783. off = 0;
  1784. i = CAS_VAL(RX_COMP2_NEXT_INDEX, words[1]);
  1785. page = cp->rx_pages[CAS_VAL(RX_INDEX_RING, i)][CAS_VAL(RX_INDEX_NUM, i)];
  1786. pci_dma_sync_single_for_cpu(cp->pdev, page->dma_addr,
  1787. hlen + cp->crc_size,
  1788. PCI_DMA_FROMDEVICE);
  1789. pci_dma_sync_single_for_device(cp->pdev, page->dma_addr,
  1790. hlen + cp->crc_size,
  1791. PCI_DMA_FROMDEVICE);
  1792. skb_shinfo(skb)->nr_frags++;
  1793. skb->data_len += hlen;
  1794. skb->len += hlen;
  1795. frag++;
  1796. get_page(page->buffer);
  1797. frag->page = page->buffer;
  1798. frag->page_offset = 0;
  1799. frag->size = hlen;
  1800. RX_USED_ADD(page, hlen + cp->crc_size);
  1801. }
  1802. if (cp->crc_size) {
  1803. addr = cas_page_map(page->buffer);
  1804. crcaddr = addr + off + hlen;
  1805. }
  1806. } else {
  1807. /* copying packet */
  1808. if (!dlen)
  1809. goto end_copy_pkt;
  1810. i = CAS_VAL(RX_COMP1_DATA_INDEX, words[0]);
  1811. page = cp->rx_pages[CAS_VAL(RX_INDEX_RING, i)][CAS_VAL(RX_INDEX_NUM, i)];
  1812. off = CAS_VAL(RX_COMP1_DATA_OFF, words[0]) + swivel;
  1813. hlen = min(cp->page_size - off, dlen);
  1814. if (hlen < 0) {
  1815. netif_printk(cp, rx_err, KERN_DEBUG, cp->dev,
  1816. "rx page overflow: %d\n", hlen);
  1817. dev_kfree_skb_irq(skb);
  1818. return -1;
  1819. }
  1820. i = hlen;
  1821. if (i == dlen) /* attach FCS */
  1822. i += cp->crc_size;
  1823. pci_dma_sync_single_for_cpu(cp->pdev, page->dma_addr + off, i,
  1824. PCI_DMA_FROMDEVICE);
  1825. addr = cas_page_map(page->buffer);
  1826. memcpy(p, addr + off, i);
  1827. pci_dma_sync_single_for_device(cp->pdev, page->dma_addr + off, i,
  1828. PCI_DMA_FROMDEVICE);
  1829. cas_page_unmap(addr);
  1830. if (p == (char *) skb->data) /* not split */
  1831. RX_USED_ADD(page, cp->mtu_stride);
  1832. else
  1833. RX_USED_ADD(page, i);
  1834. /* any more data? */
  1835. if ((words[0] & RX_COMP1_SPLIT_PKT) && ((dlen -= hlen) > 0)) {
  1836. p += hlen;
  1837. i = CAS_VAL(RX_COMP2_NEXT_INDEX, words[1]);
  1838. page = cp->rx_pages[CAS_VAL(RX_INDEX_RING, i)][CAS_VAL(RX_INDEX_NUM, i)];
  1839. pci_dma_sync_single_for_cpu(cp->pdev, page->dma_addr,
  1840. dlen + cp->crc_size,
  1841. PCI_DMA_FROMDEVICE);
  1842. addr = cas_page_map(page->buffer);
  1843. memcpy(p, addr, dlen + cp->crc_size);
  1844. pci_dma_sync_single_for_device(cp->pdev, page->dma_addr,
  1845. dlen + cp->crc_size,
  1846. PCI_DMA_FROMDEVICE);
  1847. cas_page_unmap(addr);
  1848. RX_USED_ADD(page, dlen + cp->crc_size);
  1849. }
  1850. end_copy_pkt:
  1851. if (cp->crc_size) {
  1852. addr = NULL;
  1853. crcaddr = skb->data + alloclen;
  1854. }
  1855. skb_put(skb, alloclen);
  1856. }
  1857. csum = (__force __sum16)htons(CAS_VAL(RX_COMP4_TCP_CSUM, words[3]));
  1858. if (cp->crc_size) {
  1859. /* checksum includes FCS. strip it out. */
  1860. csum = csum_fold(csum_partial(crcaddr, cp->crc_size,
  1861. csum_unfold(csum)));
  1862. if (addr)
  1863. cas_page_unmap(addr);
  1864. }
  1865. skb->protocol = eth_type_trans(skb, cp->dev);
  1866. if (skb->protocol == htons(ETH_P_IP)) {
  1867. skb->csum = csum_unfold(~csum);
  1868. skb->ip_summed = CHECKSUM_COMPLETE;
  1869. } else
  1870. skb_checksum_none_assert(skb);
  1871. return len;
  1872. }
  1873. /* we can handle up to 64 rx flows at a time. we do the same thing
  1874. * as nonreassm except that we batch up the buffers.
  1875. * NOTE: we currently just treat each flow as a bunch of packets that
  1876. * we pass up. a better way would be to coalesce the packets
  1877. * into a jumbo packet. to do that, we need to do the following:
  1878. * 1) the first packet will have a clean split between header and
  1879. * data. save both.
  1880. * 2) each time the next flow packet comes in, extend the
  1881. * data length and merge the checksums.
  1882. * 3) on flow release, fix up the header.
  1883. * 4) make sure the higher layer doesn't care.
  1884. * because packets get coalesced, we shouldn't run into fragment count
  1885. * issues.
  1886. */
  1887. static inline void cas_rx_flow_pkt(struct cas *cp, const u64 *words,
  1888. struct sk_buff *skb)
  1889. {
  1890. int flowid = CAS_VAL(RX_COMP3_FLOWID, words[2]) & (N_RX_FLOWS - 1);
  1891. struct sk_buff_head *flow = &cp->rx_flows[flowid];
  1892. /* this is protected at a higher layer, so no need to
  1893. * do any additional locking here. stick the buffer
  1894. * at the end.
  1895. */
  1896. __skb_queue_tail(flow, skb);
  1897. if (words[0] & RX_COMP1_RELEASE_FLOW) {
  1898. while ((skb = __skb_dequeue(flow))) {
  1899. cas_skb_release(skb);
  1900. }
  1901. }
  1902. }
  1903. /* put rx descriptor back on ring. if a buffer is in use by a higher
  1904. * layer, this will need to put in a replacement.
  1905. */
  1906. static void cas_post_page(struct cas *cp, const int ring, const int index)
  1907. {
  1908. cas_page_t *new;
  1909. int entry;
  1910. entry = cp->rx_old[ring];
  1911. new = cas_page_swap(cp, ring, index);
  1912. cp->init_rxds[ring][entry].buffer = cpu_to_le64(new->dma_addr);
  1913. cp->init_rxds[ring][entry].index =
  1914. cpu_to_le64(CAS_BASE(RX_INDEX_NUM, index) |
  1915. CAS_BASE(RX_INDEX_RING, ring));
  1916. entry = RX_DESC_ENTRY(ring, entry + 1);
  1917. cp->rx_old[ring] = entry;
  1918. if (entry % 4)
  1919. return;
  1920. if (ring == 0)
  1921. writel(entry, cp->regs + REG_RX_KICK);
  1922. else if ((N_RX_DESC_RINGS > 1) &&
  1923. (cp->cas_flags & CAS_FLAG_REG_PLUS))
  1924. writel(entry, cp->regs + REG_PLUS_RX_KICK1);
  1925. }
  1926. /* only when things are bad */
  1927. static int cas_post_rxds_ringN(struct cas *cp, int ring, int num)
  1928. {
  1929. unsigned int entry, last, count, released;
  1930. int cluster;
  1931. cas_page_t **page = cp->rx_pages[ring];
  1932. entry = cp->rx_old[ring];
  1933. netif_printk(cp, intr, KERN_DEBUG, cp->dev,
  1934. "rxd[%d] interrupt, done: %d\n", ring, entry);
  1935. cluster = -1;
  1936. count = entry & 0x3;
  1937. last = RX_DESC_ENTRY(ring, num ? entry + num - 4: entry - 4);
  1938. released = 0;
  1939. while (entry != last) {
  1940. /* make a new buffer if it's still in use */
  1941. if (page_count(page[entry]->buffer) > 1) {
  1942. cas_page_t *new = cas_page_dequeue(cp);
  1943. if (!new) {
  1944. /* let the timer know that we need to
  1945. * do this again
  1946. */
  1947. cp->cas_flags |= CAS_FLAG_RXD_POST(ring);
  1948. if (!timer_pending(&cp->link_timer))
  1949. mod_timer(&cp->link_timer, jiffies +
  1950. CAS_LINK_FAST_TIMEOUT);
  1951. cp->rx_old[ring] = entry;
  1952. cp->rx_last[ring] = num ? num - released : 0;
  1953. return -ENOMEM;
  1954. }
  1955. spin_lock(&cp->rx_inuse_lock);
  1956. list_add(&page[entry]->list, &cp->rx_inuse_list);
  1957. spin_unlock(&cp->rx_inuse_lock);
  1958. cp->init_rxds[ring][entry].buffer =
  1959. cpu_to_le64(new->dma_addr);
  1960. page[entry] = new;
  1961. }
  1962. if (++count == 4) {
  1963. cluster = entry;
  1964. count = 0;
  1965. }
  1966. released++;
  1967. entry = RX_DESC_ENTRY(ring, entry + 1);
  1968. }
  1969. cp->rx_old[ring] = entry;
  1970. if (cluster < 0)
  1971. return 0;
  1972. if (ring == 0)
  1973. writel(cluster, cp->regs + REG_RX_KICK);
  1974. else if ((N_RX_DESC_RINGS > 1) &&
  1975. (cp->cas_flags & CAS_FLAG_REG_PLUS))
  1976. writel(cluster, cp->regs + REG_PLUS_RX_KICK1);
  1977. return 0;
  1978. }
  1979. /* process a completion ring. packets are set up in three basic ways:
  1980. * small packets: should be copied header + data in single buffer.
  1981. * large packets: header and data in a single buffer.
  1982. * split packets: header in a separate buffer from data.
  1983. * data may be in multiple pages. data may be > 256
  1984. * bytes but in a single page.
  1985. *
  1986. * NOTE: RX page posting is done in this routine as well. while there's
  1987. * the capability of using multiple RX completion rings, it isn't
  1988. * really worthwhile due to the fact that the page posting will
  1989. * force serialization on the single descriptor ring.
  1990. */
  1991. static int cas_rx_ringN(struct cas *cp, int ring, int budget)
  1992. {
  1993. struct cas_rx_comp *rxcs = cp->init_rxcs[ring];
  1994. int entry, drops;
  1995. int npackets = 0;
  1996. netif_printk(cp, intr, KERN_DEBUG, cp->dev,
  1997. "rx[%d] interrupt, done: %d/%d\n",
  1998. ring,
  1999. readl(cp->regs + REG_RX_COMP_HEAD), cp->rx_new[ring]);
  2000. entry = cp->rx_new[ring];
  2001. drops = 0;
  2002. while (1) {
  2003. struct cas_rx_comp *rxc = rxcs + entry;
  2004. struct sk_buff *uninitialized_var(skb);
  2005. int type, len;
  2006. u64 words[4];
  2007. int i, dring;
  2008. words[0] = le64_to_cpu(rxc->word1);
  2009. words[1] = le64_to_cpu(rxc->word2);
  2010. words[2] = le64_to_cpu(rxc->word3);
  2011. words[3] = le64_to_cpu(rxc->word4);
  2012. /* don't touch if still owned by hw */
  2013. type = CAS_VAL(RX_COMP1_TYPE, words[0]);
  2014. if (type == 0)
  2015. break;
  2016. /* hw hasn't cleared the zero bit yet */
  2017. if (words[3] & RX_COMP4_ZERO) {
  2018. break;
  2019. }
  2020. /* get info on the packet */
  2021. if (words[3] & (RX_COMP4_LEN_MISMATCH | RX_COMP4_BAD)) {
  2022. spin_lock(&cp->stat_lock[ring]);
  2023. cp->net_stats[ring].rx_errors++;
  2024. if (words[3] & RX_COMP4_LEN_MISMATCH)
  2025. cp->net_stats[ring].rx_length_errors++;
  2026. if (words[3] & RX_COMP4_BAD)
  2027. cp->net_stats[ring].rx_crc_errors++;
  2028. spin_unlock(&cp->stat_lock[ring]);
  2029. /* We'll just return it to Cassini. */
  2030. drop_it:
  2031. spin_lock(&cp->stat_lock[ring]);
  2032. ++cp->net_stats[ring].rx_dropped;
  2033. spin_unlock(&cp->stat_lock[ring]);
  2034. goto next;
  2035. }
  2036. len = cas_rx_process_pkt(cp, rxc, entry, words, &skb);
  2037. if (len < 0) {
  2038. ++drops;
  2039. goto drop_it;
  2040. }
  2041. /* see if it's a flow re-assembly or not. the driver
  2042. * itself handles release back up.
  2043. */
  2044. if (RX_DONT_BATCH || (type == 0x2)) {
  2045. /* non-reassm: these always get released */
  2046. cas_skb_release(skb);
  2047. } else {
  2048. cas_rx_flow_pkt(cp, words, skb);
  2049. }
  2050. spin_lock(&cp->stat_lock[ring]);
  2051. cp->net_stats[ring].rx_packets++;
  2052. cp->net_stats[ring].rx_bytes += len;
  2053. spin_unlock(&cp->stat_lock[ring]);
  2054. next:
  2055. npackets++;
  2056. /* should it be released? */
  2057. if (words[0] & RX_COMP1_RELEASE_HDR) {
  2058. i = CAS_VAL(RX_COMP2_HDR_INDEX, words[1]);
  2059. dring = CAS_VAL(RX_INDEX_RING, i);
  2060. i = CAS_VAL(RX_INDEX_NUM, i);
  2061. cas_post_page(cp, dring, i);
  2062. }
  2063. if (words[0] & RX_COMP1_RELEASE_DATA) {
  2064. i = CAS_VAL(RX_COMP1_DATA_INDEX, words[0]);
  2065. dring = CAS_VAL(RX_INDEX_RING, i);
  2066. i = CAS_VAL(RX_INDEX_NUM, i);
  2067. cas_post_page(cp, dring, i);
  2068. }
  2069. if (words[0] & RX_COMP1_RELEASE_NEXT) {
  2070. i = CAS_VAL(RX_COMP2_NEXT_INDEX, words[1]);
  2071. dring = CAS_VAL(RX_INDEX_RING, i);
  2072. i = CAS_VAL(RX_INDEX_NUM, i);
  2073. cas_post_page(cp, dring, i);
  2074. }
  2075. /* skip to the next entry */
  2076. entry = RX_COMP_ENTRY(ring, entry + 1 +
  2077. CAS_VAL(RX_COMP1_SKIP, words[0]));
  2078. #ifdef USE_NAPI
  2079. if (budget && (npackets >= budget))
  2080. break;
  2081. #endif
  2082. }
  2083. cp->rx_new[ring] = entry;
  2084. if (drops)
  2085. netdev_info(cp->dev, "Memory squeeze, deferring packet\n");
  2086. return npackets;
  2087. }
  2088. /* put completion entries back on the ring */
  2089. static void cas_post_rxcs_ringN(struct net_device *dev,
  2090. struct cas *cp, int ring)
  2091. {
  2092. struct cas_rx_comp *rxc = cp->init_rxcs[ring];
  2093. int last, entry;
  2094. last = cp->rx_cur[ring];
  2095. entry = cp->rx_new[ring];
  2096. netif_printk(cp, intr, KERN_DEBUG, dev,
  2097. "rxc[%d] interrupt, done: %d/%d\n",
  2098. ring, readl(cp->regs + REG_RX_COMP_HEAD), entry);
  2099. /* zero and re-mark descriptors */
  2100. while (last != entry) {
  2101. cas_rxc_init(rxc + last);
  2102. last = RX_COMP_ENTRY(ring, last + 1);
  2103. }
  2104. cp->rx_cur[ring] = last;
  2105. if (ring == 0)
  2106. writel(last, cp->regs + REG_RX_COMP_TAIL);
  2107. else if (cp->cas_flags & CAS_FLAG_REG_PLUS)
  2108. writel(last, cp->regs + REG_PLUS_RX_COMPN_TAIL(ring));
  2109. }
  2110. /* cassini can use all four PCI interrupts for the completion ring.
  2111. * rings 3 and 4 are identical
  2112. */
  2113. #if defined(USE_PCI_INTC) || defined(USE_PCI_INTD)
  2114. static inline void cas_handle_irqN(struct net_device *dev,
  2115. struct cas *cp, const u32 status,
  2116. const int ring)
  2117. {
  2118. if (status & (INTR_RX_COMP_FULL_ALT | INTR_RX_COMP_AF_ALT))
  2119. cas_post_rxcs_ringN(dev, cp, ring);
  2120. }
  2121. static irqreturn_t cas_interruptN(int irq, void *dev_id)
  2122. {
  2123. struct net_device *dev = dev_id;
  2124. struct cas *cp = netdev_priv(dev);
  2125. unsigned long flags;
  2126. int ring;
  2127. u32 status = readl(cp->regs + REG_PLUS_INTRN_STATUS(ring));
  2128. /* check for shared irq */
  2129. if (status == 0)
  2130. return IRQ_NONE;
  2131. ring = (irq == cp->pci_irq_INTC) ? 2 : 3;
  2132. spin_lock_irqsave(&cp->lock, flags);
  2133. if (status & INTR_RX_DONE_ALT) { /* handle rx separately */
  2134. #ifdef USE_NAPI
  2135. cas_mask_intr(cp);
  2136. napi_schedule(&cp->napi);
  2137. #else
  2138. cas_rx_ringN(cp, ring, 0);
  2139. #endif
  2140. status &= ~INTR_RX_DONE_ALT;
  2141. }
  2142. if (status)
  2143. cas_handle_irqN(dev, cp, status, ring);
  2144. spin_unlock_irqrestore(&cp->lock, flags);
  2145. return IRQ_HANDLED;
  2146. }
  2147. #endif
  2148. #ifdef USE_PCI_INTB
  2149. /* everything but rx packets */
  2150. static inline void cas_handle_irq1(struct cas *cp, const u32 status)
  2151. {
  2152. if (status & INTR_RX_BUF_UNAVAIL_1) {
  2153. /* Frame arrived, no free RX buffers available.
  2154. * NOTE: we can get this on a link transition. */
  2155. cas_post_rxds_ringN(cp, 1, 0);
  2156. spin_lock(&cp->stat_lock[1]);
  2157. cp->net_stats[1].rx_dropped++;
  2158. spin_unlock(&cp->stat_lock[1]);
  2159. }
  2160. if (status & INTR_RX_BUF_AE_1)
  2161. cas_post_rxds_ringN(cp, 1, RX_DESC_RINGN_SIZE(1) -
  2162. RX_AE_FREEN_VAL(1));
  2163. if (status & (INTR_RX_COMP_AF | INTR_RX_COMP_FULL))
  2164. cas_post_rxcs_ringN(cp, 1);
  2165. }
  2166. /* ring 2 handles a few more events than 3 and 4 */
  2167. static irqreturn_t cas_interrupt1(int irq, void *dev_id)
  2168. {
  2169. struct net_device *dev = dev_id;
  2170. struct cas *cp = netdev_priv(dev);
  2171. unsigned long flags;
  2172. u32 status = readl(cp->regs + REG_PLUS_INTRN_STATUS(1));
  2173. /* check for shared interrupt */
  2174. if (status == 0)
  2175. return IRQ_NONE;
  2176. spin_lock_irqsave(&cp->lock, flags);
  2177. if (status & INTR_RX_DONE_ALT) { /* handle rx separately */
  2178. #ifdef USE_NAPI
  2179. cas_mask_intr(cp);
  2180. napi_schedule(&cp->napi);
  2181. #else
  2182. cas_rx_ringN(cp, 1, 0);
  2183. #endif
  2184. status &= ~INTR_RX_DONE_ALT;
  2185. }
  2186. if (status)
  2187. cas_handle_irq1(cp, status);
  2188. spin_unlock_irqrestore(&cp->lock, flags);
  2189. return IRQ_HANDLED;
  2190. }
  2191. #endif
  2192. static inline void cas_handle_irq(struct net_device *dev,
  2193. struct cas *cp, const u32 status)
  2194. {
  2195. /* housekeeping interrupts */
  2196. if (status & INTR_ERROR_MASK)
  2197. cas_abnormal_irq(dev, cp, status);
  2198. if (status & INTR_RX_BUF_UNAVAIL) {
  2199. /* Frame arrived, no free RX buffers available.
  2200. * NOTE: we can get this on a link transition.
  2201. */
  2202. cas_post_rxds_ringN(cp, 0, 0);
  2203. spin_lock(&cp->stat_lock[0]);
  2204. cp->net_stats[0].rx_dropped++;
  2205. spin_unlock(&cp->stat_lock[0]);
  2206. } else if (status & INTR_RX_BUF_AE) {
  2207. cas_post_rxds_ringN(cp, 0, RX_DESC_RINGN_SIZE(0) -
  2208. RX_AE_FREEN_VAL(0));
  2209. }
  2210. if (status & (INTR_RX_COMP_AF | INTR_RX_COMP_FULL))
  2211. cas_post_rxcs_ringN(dev, cp, 0);
  2212. }
  2213. static irqreturn_t cas_interrupt(int irq, void *dev_id)
  2214. {
  2215. struct net_device *dev = dev_id;
  2216. struct cas *cp = netdev_priv(dev);
  2217. unsigned long flags;
  2218. u32 status = readl(cp->regs + REG_INTR_STATUS);
  2219. if (status == 0)
  2220. return IRQ_NONE;
  2221. spin_lock_irqsave(&cp->lock, flags);
  2222. if (status & (INTR_TX_ALL | INTR_TX_INTME)) {
  2223. cas_tx(dev, cp, status);
  2224. status &= ~(INTR_TX_ALL | INTR_TX_INTME);
  2225. }
  2226. if (status & INTR_RX_DONE) {
  2227. #ifdef USE_NAPI
  2228. cas_mask_intr(cp);
  2229. napi_schedule(&cp->napi);
  2230. #else
  2231. cas_rx_ringN(cp, 0, 0);
  2232. #endif
  2233. status &= ~INTR_RX_DONE;
  2234. }
  2235. if (status)
  2236. cas_handle_irq(dev, cp, status);
  2237. spin_unlock_irqrestore(&cp->lock, flags);
  2238. return IRQ_HANDLED;
  2239. }
  2240. #ifdef USE_NAPI
  2241. static int cas_poll(struct napi_struct *napi, int budget)
  2242. {
  2243. struct cas *cp = container_of(napi, struct cas, napi);
  2244. struct net_device *dev = cp->dev;
  2245. int i, enable_intr, credits;
  2246. u32 status = readl(cp->regs + REG_INTR_STATUS);
  2247. unsigned long flags;
  2248. spin_lock_irqsave(&cp->lock, flags);
  2249. cas_tx(dev, cp, status);
  2250. spin_unlock_irqrestore(&cp->lock, flags);
  2251. /* NAPI rx packets. we spread the credits across all of the
  2252. * rxc rings
  2253. *
  2254. * to make sure we're fair with the work we loop through each
  2255. * ring N_RX_COMP_RING times with a request of
  2256. * budget / N_RX_COMP_RINGS
  2257. */
  2258. enable_intr = 1;
  2259. credits = 0;
  2260. for (i = 0; i < N_RX_COMP_RINGS; i++) {
  2261. int j;
  2262. for (j = 0; j < N_RX_COMP_RINGS; j++) {
  2263. credits += cas_rx_ringN(cp, j, budget / N_RX_COMP_RINGS);
  2264. if (credits >= budget) {
  2265. enable_intr = 0;
  2266. goto rx_comp;
  2267. }
  2268. }
  2269. }
  2270. rx_comp:
  2271. /* final rx completion */
  2272. spin_lock_irqsave(&cp->lock, flags);
  2273. if (status)
  2274. cas_handle_irq(dev, cp, status);
  2275. #ifdef USE_PCI_INTB
  2276. if (N_RX_COMP_RINGS > 1) {
  2277. status = readl(cp->regs + REG_PLUS_INTRN_STATUS(1));
  2278. if (status)
  2279. cas_handle_irq1(dev, cp, status);
  2280. }
  2281. #endif
  2282. #ifdef USE_PCI_INTC
  2283. if (N_RX_COMP_RINGS > 2) {
  2284. status = readl(cp->regs + REG_PLUS_INTRN_STATUS(2));
  2285. if (status)
  2286. cas_handle_irqN(dev, cp, status, 2);
  2287. }
  2288. #endif
  2289. #ifdef USE_PCI_INTD
  2290. if (N_RX_COMP_RINGS > 3) {
  2291. status = readl(cp->regs + REG_PLUS_INTRN_STATUS(3));
  2292. if (status)
  2293. cas_handle_irqN(dev, cp, status, 3);
  2294. }
  2295. #endif
  2296. spin_unlock_irqrestore(&cp->lock, flags);
  2297. if (enable_intr) {
  2298. napi_complete(napi);
  2299. cas_unmask_intr(cp);
  2300. }
  2301. return credits;
  2302. }
  2303. #endif
  2304. #ifdef CONFIG_NET_POLL_CONTROLLER
  2305. static void cas_netpoll(struct net_device *dev)
  2306. {
  2307. struct cas *cp = netdev_priv(dev);
  2308. cas_disable_irq(cp, 0);
  2309. cas_interrupt(cp->pdev->irq, dev);
  2310. cas_enable_irq(cp, 0);
  2311. #ifdef USE_PCI_INTB
  2312. if (N_RX_COMP_RINGS > 1) {
  2313. /* cas_interrupt1(); */
  2314. }
  2315. #endif
  2316. #ifdef USE_PCI_INTC
  2317. if (N_RX_COMP_RINGS > 2) {
  2318. /* cas_interruptN(); */
  2319. }
  2320. #endif
  2321. #ifdef USE_PCI_INTD
  2322. if (N_RX_COMP_RINGS > 3) {
  2323. /* cas_interruptN(); */
  2324. }
  2325. #endif
  2326. }
  2327. #endif
  2328. static void cas_tx_timeout(struct net_device *dev)
  2329. {
  2330. struct cas *cp = netdev_priv(dev);
  2331. netdev_err(dev, "transmit timed out, resetting\n");
  2332. if (!cp->hw_running) {
  2333. netdev_err(dev, "hrm.. hw not running!\n");
  2334. return;
  2335. }
  2336. netdev_err(dev, "MIF_STATE[%08x]\n",
  2337. readl(cp->regs + REG_MIF_STATE_MACHINE));
  2338. netdev_err(dev, "MAC_STATE[%08x]\n",
  2339. readl(cp->regs + REG_MAC_STATE_MACHINE));
  2340. netdev_err(dev, "TX_STATE[%08x:%08x:%08x] FIFO[%08x:%08x:%08x] SM1[%08x] SM2[%08x]\n",
  2341. readl(cp->regs + REG_TX_CFG),
  2342. readl(cp->regs + REG_MAC_TX_STATUS),
  2343. readl(cp->regs + REG_MAC_TX_CFG),
  2344. readl(cp->regs + REG_TX_FIFO_PKT_CNT),
  2345. readl(cp->regs + REG_TX_FIFO_WRITE_PTR),
  2346. readl(cp->regs + REG_TX_FIFO_READ_PTR),
  2347. readl(cp->regs + REG_TX_SM_1),
  2348. readl(cp->regs + REG_TX_SM_2));
  2349. netdev_err(dev, "RX_STATE[%08x:%08x:%08x]\n",
  2350. readl(cp->regs + REG_RX_CFG),
  2351. readl(cp->regs + REG_MAC_RX_STATUS),
  2352. readl(cp->regs + REG_MAC_RX_CFG));
  2353. netdev_err(dev, "HP_STATE[%08x:%08x:%08x:%08x]\n",
  2354. readl(cp->regs + REG_HP_STATE_MACHINE),
  2355. readl(cp->regs + REG_HP_STATUS0),
  2356. readl(cp->regs + REG_HP_STATUS1),
  2357. readl(cp->regs + REG_HP_STATUS2));
  2358. #if 1
  2359. atomic_inc(&cp->reset_task_pending);
  2360. atomic_inc(&cp->reset_task_pending_all);
  2361. schedule_work(&cp->reset_task);
  2362. #else
  2363. atomic_set(&cp->reset_task_pending, CAS_RESET_ALL);
  2364. schedule_work(&cp->reset_task);
  2365. #endif
  2366. }
  2367. static inline int cas_intme(int ring, int entry)
  2368. {
  2369. /* Algorithm: IRQ every 1/2 of descriptors. */
  2370. if (!(entry & ((TX_DESC_RINGN_SIZE(ring) >> 1) - 1)))
  2371. return 1;
  2372. return 0;
  2373. }
  2374. static void cas_write_txd(struct cas *cp, int ring, int entry,
  2375. dma_addr_t mapping, int len, u64 ctrl, int last)
  2376. {
  2377. struct cas_tx_desc *txd = cp->init_txds[ring] + entry;
  2378. ctrl |= CAS_BASE(TX_DESC_BUFLEN, len);
  2379. if (cas_intme(ring, entry))
  2380. ctrl |= TX_DESC_INTME;
  2381. if (last)
  2382. ctrl |= TX_DESC_EOF;
  2383. txd->control = cpu_to_le64(ctrl);
  2384. txd->buffer = cpu_to_le64(mapping);
  2385. }
  2386. static inline void *tx_tiny_buf(struct cas *cp, const int ring,
  2387. const int entry)
  2388. {
  2389. return cp->tx_tiny_bufs[ring] + TX_TINY_BUF_LEN*entry;
  2390. }
  2391. static inline dma_addr_t tx_tiny_map(struct cas *cp, const int ring,
  2392. const int entry, const int tentry)
  2393. {
  2394. cp->tx_tiny_use[ring][tentry].nbufs++;
  2395. cp->tx_tiny_use[ring][entry].used = 1;
  2396. return cp->tx_tiny_dvma[ring] + TX_TINY_BUF_LEN*entry;
  2397. }
  2398. static inline int cas_xmit_tx_ringN(struct cas *cp, int ring,
  2399. struct sk_buff *skb)
  2400. {
  2401. struct net_device *dev = cp->dev;
  2402. int entry, nr_frags, frag, tabort, tentry;
  2403. dma_addr_t mapping;
  2404. unsigned long flags;
  2405. u64 ctrl;
  2406. u32 len;
  2407. spin_lock_irqsave(&cp->tx_lock[ring], flags);
  2408. /* This is a hard error, log it. */
  2409. if (TX_BUFFS_AVAIL(cp, ring) <=
  2410. CAS_TABORT(cp)*(skb_shinfo(skb)->nr_frags + 1)) {
  2411. netif_stop_queue(dev);
  2412. spin_unlock_irqrestore(&cp->tx_lock[ring], flags);
  2413. netdev_err(dev, "BUG! Tx Ring full when queue awake!\n");
  2414. return 1;
  2415. }
  2416. ctrl = 0;
  2417. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  2418. const u64 csum_start_off = skb_checksum_start_offset(skb);
  2419. const u64 csum_stuff_off = csum_start_off + skb->csum_offset;
  2420. ctrl = TX_DESC_CSUM_EN |
  2421. CAS_BASE(TX_DESC_CSUM_START, csum_start_off) |
  2422. CAS_BASE(TX_DESC_CSUM_STUFF, csum_stuff_off);
  2423. }
  2424. entry = cp->tx_new[ring];
  2425. cp->tx_skbs[ring][entry] = skb;
  2426. nr_frags = skb_shinfo(skb)->nr_frags;
  2427. len = skb_headlen(skb);
  2428. mapping = pci_map_page(cp->pdev, virt_to_page(skb->data),
  2429. offset_in_page(skb->data), len,
  2430. PCI_DMA_TODEVICE);
  2431. tentry = entry;
  2432. tabort = cas_calc_tabort(cp, (unsigned long) skb->data, len);
  2433. if (unlikely(tabort)) {
  2434. /* NOTE: len is always > tabort */
  2435. cas_write_txd(cp, ring, entry, mapping, len - tabort,
  2436. ctrl | TX_DESC_SOF, 0);
  2437. entry = TX_DESC_NEXT(ring, entry);
  2438. skb_copy_from_linear_data_offset(skb, len - tabort,
  2439. tx_tiny_buf(cp, ring, entry), tabort);
  2440. mapping = tx_tiny_map(cp, ring, entry, tentry);
  2441. cas_write_txd(cp, ring, entry, mapping, tabort, ctrl,
  2442. (nr_frags == 0));
  2443. } else {
  2444. cas_write_txd(cp, ring, entry, mapping, len, ctrl |
  2445. TX_DESC_SOF, (nr_frags == 0));
  2446. }
  2447. entry = TX_DESC_NEXT(ring, entry);
  2448. for (frag = 0; frag < nr_frags; frag++) {
  2449. skb_frag_t *fragp = &skb_shinfo(skb)->frags[frag];
  2450. len = fragp->size;
  2451. mapping = pci_map_page(cp->pdev, fragp->page,
  2452. fragp->page_offset, len,
  2453. PCI_DMA_TODEVICE);
  2454. tabort = cas_calc_tabort(cp, fragp->page_offset, len);
  2455. if (unlikely(tabort)) {
  2456. void *addr;
  2457. /* NOTE: len is always > tabort */
  2458. cas_write_txd(cp, ring, entry, mapping, len - tabort,
  2459. ctrl, 0);
  2460. entry = TX_DESC_NEXT(ring, entry);
  2461. addr = cas_page_map(fragp->page);
  2462. memcpy(tx_tiny_buf(cp, ring, entry),
  2463. addr + fragp->page_offset + len - tabort,
  2464. tabort);
  2465. cas_page_unmap(addr);
  2466. mapping = tx_tiny_map(cp, ring, entry, tentry);
  2467. len = tabort;
  2468. }
  2469. cas_write_txd(cp, ring, entry, mapping, len, ctrl,
  2470. (frag + 1 == nr_frags));
  2471. entry = TX_DESC_NEXT(ring, entry);
  2472. }
  2473. cp->tx_new[ring] = entry;
  2474. if (TX_BUFFS_AVAIL(cp, ring) <= CAS_TABORT(cp)*(MAX_SKB_FRAGS + 1))
  2475. netif_stop_queue(dev);
  2476. netif_printk(cp, tx_queued, KERN_DEBUG, dev,
  2477. "tx[%d] queued, slot %d, skblen %d, avail %d\n",
  2478. ring, entry, skb->len, TX_BUFFS_AVAIL(cp, ring));
  2479. writel(entry, cp->regs + REG_TX_KICKN(ring));
  2480. spin_unlock_irqrestore(&cp->tx_lock[ring], flags);
  2481. return 0;
  2482. }
  2483. static netdev_tx_t cas_start_xmit(struct sk_buff *skb, struct net_device *dev)
  2484. {
  2485. struct cas *cp = netdev_priv(dev);
  2486. /* this is only used as a load-balancing hint, so it doesn't
  2487. * need to be SMP safe
  2488. */
  2489. static int ring;
  2490. if (skb_padto(skb, cp->min_frame_size))
  2491. return NETDEV_TX_OK;
  2492. /* XXX: we need some higher-level QoS hooks to steer packets to
  2493. * individual queues.
  2494. */
  2495. if (cas_xmit_tx_ringN(cp, ring++ & N_TX_RINGS_MASK, skb))
  2496. return NETDEV_TX_BUSY;
  2497. return NETDEV_TX_OK;
  2498. }
  2499. static void cas_init_tx_dma(struct cas *cp)
  2500. {
  2501. u64 desc_dma = cp->block_dvma;
  2502. unsigned long off;
  2503. u32 val;
  2504. int i;
  2505. /* set up tx completion writeback registers. must be 8-byte aligned */
  2506. #ifdef USE_TX_COMPWB
  2507. off = offsetof(struct cas_init_block, tx_compwb);
  2508. writel((desc_dma + off) >> 32, cp->regs + REG_TX_COMPWB_DB_HI);
  2509. writel((desc_dma + off) & 0xffffffff, cp->regs + REG_TX_COMPWB_DB_LOW);
  2510. #endif
  2511. /* enable completion writebacks, enable paced mode,
  2512. * disable read pipe, and disable pre-interrupt compwbs
  2513. */
  2514. val = TX_CFG_COMPWB_Q1 | TX_CFG_COMPWB_Q2 |
  2515. TX_CFG_COMPWB_Q3 | TX_CFG_COMPWB_Q4 |
  2516. TX_CFG_DMA_RDPIPE_DIS | TX_CFG_PACED_MODE |
  2517. TX_CFG_INTR_COMPWB_DIS;
  2518. /* write out tx ring info and tx desc bases */
  2519. for (i = 0; i < MAX_TX_RINGS; i++) {
  2520. off = (unsigned long) cp->init_txds[i] -
  2521. (unsigned long) cp->init_block;
  2522. val |= CAS_TX_RINGN_BASE(i);
  2523. writel((desc_dma + off) >> 32, cp->regs + REG_TX_DBN_HI(i));
  2524. writel((desc_dma + off) & 0xffffffff, cp->regs +
  2525. REG_TX_DBN_LOW(i));
  2526. /* don't zero out the kick register here as the system
  2527. * will wedge
  2528. */
  2529. }
  2530. writel(val, cp->regs + REG_TX_CFG);
  2531. /* program max burst sizes. these numbers should be different
  2532. * if doing QoS.
  2533. */
  2534. #ifdef USE_QOS
  2535. writel(0x800, cp->regs + REG_TX_MAXBURST_0);
  2536. writel(0x1600, cp->regs + REG_TX_MAXBURST_1);
  2537. writel(0x2400, cp->regs + REG_TX_MAXBURST_2);
  2538. writel(0x4800, cp->regs + REG_TX_MAXBURST_3);
  2539. #else
  2540. writel(0x800, cp->regs + REG_TX_MAXBURST_0);
  2541. writel(0x800, cp->regs + REG_TX_MAXBURST_1);
  2542. writel(0x800, cp->regs + REG_TX_MAXBURST_2);
  2543. writel(0x800, cp->regs + REG_TX_MAXBURST_3);
  2544. #endif
  2545. }
  2546. /* Must be invoked under cp->lock. */
  2547. static inline void cas_init_dma(struct cas *cp)
  2548. {
  2549. cas_init_tx_dma(cp);
  2550. cas_init_rx_dma(cp);
  2551. }
  2552. static void cas_process_mc_list(struct cas *cp)
  2553. {
  2554. u16 hash_table[16];
  2555. u32 crc;
  2556. struct netdev_hw_addr *ha;
  2557. int i = 1;
  2558. memset(hash_table, 0, sizeof(hash_table));
  2559. netdev_for_each_mc_addr(ha, cp->dev) {
  2560. if (i <= CAS_MC_EXACT_MATCH_SIZE) {
  2561. /* use the alternate mac address registers for the
  2562. * first 15 multicast addresses
  2563. */
  2564. writel((ha->addr[4] << 8) | ha->addr[5],
  2565. cp->regs + REG_MAC_ADDRN(i*3 + 0));
  2566. writel((ha->addr[2] << 8) | ha->addr[3],
  2567. cp->regs + REG_MAC_ADDRN(i*3 + 1));
  2568. writel((ha->addr[0] << 8) | ha->addr[1],
  2569. cp->regs + REG_MAC_ADDRN(i*3 + 2));
  2570. i++;
  2571. }
  2572. else {
  2573. /* use hw hash table for the next series of
  2574. * multicast addresses
  2575. */
  2576. crc = ether_crc_le(ETH_ALEN, ha->addr);
  2577. crc >>= 24;
  2578. hash_table[crc >> 4] |= 1 << (15 - (crc & 0xf));
  2579. }
  2580. }
  2581. for (i = 0; i < 16; i++)
  2582. writel(hash_table[i], cp->regs + REG_MAC_HASH_TABLEN(i));
  2583. }
  2584. /* Must be invoked under cp->lock. */
  2585. static u32 cas_setup_multicast(struct cas *cp)
  2586. {
  2587. u32 rxcfg = 0;
  2588. int i;
  2589. if (cp->dev->flags & IFF_PROMISC) {
  2590. rxcfg |= MAC_RX_CFG_PROMISC_EN;
  2591. } else if (cp->dev->flags & IFF_ALLMULTI) {
  2592. for (i=0; i < 16; i++)
  2593. writel(0xFFFF, cp->regs + REG_MAC_HASH_TABLEN(i));
  2594. rxcfg |= MAC_RX_CFG_HASH_FILTER_EN;
  2595. } else {
  2596. cas_process_mc_list(cp);
  2597. rxcfg |= MAC_RX_CFG_HASH_FILTER_EN;
  2598. }
  2599. return rxcfg;
  2600. }
  2601. /* must be invoked under cp->stat_lock[N_TX_RINGS] */
  2602. static void cas_clear_mac_err(struct cas *cp)
  2603. {
  2604. writel(0, cp->regs + REG_MAC_COLL_NORMAL);
  2605. writel(0, cp->regs + REG_MAC_COLL_FIRST);
  2606. writel(0, cp->regs + REG_MAC_COLL_EXCESS);
  2607. writel(0, cp->regs + REG_MAC_COLL_LATE);
  2608. writel(0, cp->regs + REG_MAC_TIMER_DEFER);
  2609. writel(0, cp->regs + REG_MAC_ATTEMPTS_PEAK);
  2610. writel(0, cp->regs + REG_MAC_RECV_FRAME);
  2611. writel(0, cp->regs + REG_MAC_LEN_ERR);
  2612. writel(0, cp->regs + REG_MAC_ALIGN_ERR);
  2613. writel(0, cp->regs + REG_MAC_FCS_ERR);
  2614. writel(0, cp->regs + REG_MAC_RX_CODE_ERR);
  2615. }
  2616. static void cas_mac_reset(struct cas *cp)
  2617. {
  2618. int i;
  2619. /* do both TX and RX reset */
  2620. writel(0x1, cp->regs + REG_MAC_TX_RESET);
  2621. writel(0x1, cp->regs + REG_MAC_RX_RESET);
  2622. /* wait for TX */
  2623. i = STOP_TRIES;
  2624. while (i-- > 0) {
  2625. if (readl(cp->regs + REG_MAC_TX_RESET) == 0)
  2626. break;
  2627. udelay(10);
  2628. }
  2629. /* wait for RX */
  2630. i = STOP_TRIES;
  2631. while (i-- > 0) {
  2632. if (readl(cp->regs + REG_MAC_RX_RESET) == 0)
  2633. break;
  2634. udelay(10);
  2635. }
  2636. if (readl(cp->regs + REG_MAC_TX_RESET) |
  2637. readl(cp->regs + REG_MAC_RX_RESET))
  2638. netdev_err(cp->dev, "mac tx[%d]/rx[%d] reset failed [%08x]\n",
  2639. readl(cp->regs + REG_MAC_TX_RESET),
  2640. readl(cp->regs + REG_MAC_RX_RESET),
  2641. readl(cp->regs + REG_MAC_STATE_MACHINE));
  2642. }
  2643. /* Must be invoked under cp->lock. */
  2644. static void cas_init_mac(struct cas *cp)
  2645. {
  2646. unsigned char *e = &cp->dev->dev_addr[0];
  2647. int i;
  2648. cas_mac_reset(cp);
  2649. /* setup core arbitration weight register */
  2650. writel(CAWR_RR_DIS, cp->regs + REG_CAWR);
  2651. /* XXX Use pci_dma_burst_advice() */
  2652. #if !defined(CONFIG_SPARC64) && !defined(CONFIG_ALPHA)
  2653. /* set the infinite burst register for chips that don't have
  2654. * pci issues.
  2655. */
  2656. if ((cp->cas_flags & CAS_FLAG_TARGET_ABORT) == 0)
  2657. writel(INF_BURST_EN, cp->regs + REG_INF_BURST);
  2658. #endif
  2659. writel(0x1BF0, cp->regs + REG_MAC_SEND_PAUSE);
  2660. writel(0x00, cp->regs + REG_MAC_IPG0);
  2661. writel(0x08, cp->regs + REG_MAC_IPG1);
  2662. writel(0x04, cp->regs + REG_MAC_IPG2);
  2663. /* change later for 802.3z */
  2664. writel(0x40, cp->regs + REG_MAC_SLOT_TIME);
  2665. /* min frame + FCS */
  2666. writel(ETH_ZLEN + 4, cp->regs + REG_MAC_FRAMESIZE_MIN);
  2667. /* Ethernet payload + header + FCS + optional VLAN tag. NOTE: we
  2668. * specify the maximum frame size to prevent RX tag errors on
  2669. * oversized frames.
  2670. */
  2671. writel(CAS_BASE(MAC_FRAMESIZE_MAX_BURST, 0x2000) |
  2672. CAS_BASE(MAC_FRAMESIZE_MAX_FRAME,
  2673. (CAS_MAX_MTU + ETH_HLEN + 4 + 4)),
  2674. cp->regs + REG_MAC_FRAMESIZE_MAX);
  2675. /* NOTE: crc_size is used as a surrogate for half-duplex.
  2676. * workaround saturn half-duplex issue by increasing preamble
  2677. * size to 65 bytes.
  2678. */
  2679. if ((cp->cas_flags & CAS_FLAG_SATURN) && cp->crc_size)
  2680. writel(0x41, cp->regs + REG_MAC_PA_SIZE);
  2681. else
  2682. writel(0x07, cp->regs + REG_MAC_PA_SIZE);
  2683. writel(0x04, cp->regs + REG_MAC_JAM_SIZE);
  2684. writel(0x10, cp->regs + REG_MAC_ATTEMPT_LIMIT);
  2685. writel(0x8808, cp->regs + REG_MAC_CTRL_TYPE);
  2686. writel((e[5] | (e[4] << 8)) & 0x3ff, cp->regs + REG_MAC_RANDOM_SEED);
  2687. writel(0, cp->regs + REG_MAC_ADDR_FILTER0);
  2688. writel(0, cp->regs + REG_MAC_ADDR_FILTER1);
  2689. writel(0, cp->regs + REG_MAC_ADDR_FILTER2);
  2690. writel(0, cp->regs + REG_MAC_ADDR_FILTER2_1_MASK);
  2691. writel(0, cp->regs + REG_MAC_ADDR_FILTER0_MASK);
  2692. /* setup mac address in perfect filter array */
  2693. for (i = 0; i < 45; i++)
  2694. writel(0x0, cp->regs + REG_MAC_ADDRN(i));
  2695. writel((e[4] << 8) | e[5], cp->regs + REG_MAC_ADDRN(0));
  2696. writel((e[2] << 8) | e[3], cp->regs + REG_MAC_ADDRN(1));
  2697. writel((e[0] << 8) | e[1], cp->regs + REG_MAC_ADDRN(2));
  2698. writel(0x0001, cp->regs + REG_MAC_ADDRN(42));
  2699. writel(0xc200, cp->regs + REG_MAC_ADDRN(43));
  2700. writel(0x0180, cp->regs + REG_MAC_ADDRN(44));
  2701. cp->mac_rx_cfg = cas_setup_multicast(cp);
  2702. spin_lock(&cp->stat_lock[N_TX_RINGS]);
  2703. cas_clear_mac_err(cp);
  2704. spin_unlock(&cp->stat_lock[N_TX_RINGS]);
  2705. /* Setup MAC interrupts. We want to get all of the interesting
  2706. * counter expiration events, but we do not want to hear about
  2707. * normal rx/tx as the DMA engine tells us that.
  2708. */
  2709. writel(MAC_TX_FRAME_XMIT, cp->regs + REG_MAC_TX_MASK);
  2710. writel(MAC_RX_FRAME_RECV, cp->regs + REG_MAC_RX_MASK);
  2711. /* Don't enable even the PAUSE interrupts for now, we
  2712. * make no use of those events other than to record them.
  2713. */
  2714. writel(0xffffffff, cp->regs + REG_MAC_CTRL_MASK);
  2715. }
  2716. /* Must be invoked under cp->lock. */
  2717. static void cas_init_pause_thresholds(struct cas *cp)
  2718. {
  2719. /* Calculate pause thresholds. Setting the OFF threshold to the
  2720. * full RX fifo size effectively disables PAUSE generation
  2721. */
  2722. if (cp->rx_fifo_size <= (2 * 1024)) {
  2723. cp->rx_pause_off = cp->rx_pause_on = cp->rx_fifo_size;
  2724. } else {
  2725. int max_frame = (cp->dev->mtu + ETH_HLEN + 4 + 4 + 64) & ~63;
  2726. if (max_frame * 3 > cp->rx_fifo_size) {
  2727. cp->rx_pause_off = 7104;
  2728. cp->rx_pause_on = 960;
  2729. } else {
  2730. int off = (cp->rx_fifo_size - (max_frame * 2));
  2731. int on = off - max_frame;
  2732. cp->rx_pause_off = off;
  2733. cp->rx_pause_on = on;
  2734. }
  2735. }
  2736. }
  2737. static int cas_vpd_match(const void __iomem *p, const char *str)
  2738. {
  2739. int len = strlen(str) + 1;
  2740. int i;
  2741. for (i = 0; i < len; i++) {
  2742. if (readb(p + i) != str[i])
  2743. return 0;
  2744. }
  2745. return 1;
  2746. }
  2747. /* get the mac address by reading the vpd information in the rom.
  2748. * also get the phy type and determine if there's an entropy generator.
  2749. * NOTE: this is a bit convoluted for the following reasons:
  2750. * 1) vpd info has order-dependent mac addresses for multinic cards
  2751. * 2) the only way to determine the nic order is to use the slot
  2752. * number.
  2753. * 3) fiber cards don't have bridges, so their slot numbers don't
  2754. * mean anything.
  2755. * 4) we don't actually know we have a fiber card until after
  2756. * the mac addresses are parsed.
  2757. */
  2758. static int cas_get_vpd_info(struct cas *cp, unsigned char *dev_addr,
  2759. const int offset)
  2760. {
  2761. void __iomem *p = cp->regs + REG_EXPANSION_ROM_RUN_START;
  2762. void __iomem *base, *kstart;
  2763. int i, len;
  2764. int found = 0;
  2765. #define VPD_FOUND_MAC 0x01
  2766. #define VPD_FOUND_PHY 0x02
  2767. int phy_type = CAS_PHY_MII_MDIO0; /* default phy type */
  2768. int mac_off = 0;
  2769. #if defined(CONFIG_SPARC)
  2770. const unsigned char *addr;
  2771. #endif
  2772. /* give us access to the PROM */
  2773. writel(BIM_LOCAL_DEV_PROM | BIM_LOCAL_DEV_PAD,
  2774. cp->regs + REG_BIM_LOCAL_DEV_EN);
  2775. /* check for an expansion rom */
  2776. if (readb(p) != 0x55 || readb(p + 1) != 0xaa)
  2777. goto use_random_mac_addr;
  2778. /* search for beginning of vpd */
  2779. base = NULL;
  2780. for (i = 2; i < EXPANSION_ROM_SIZE; i++) {
  2781. /* check for PCIR */
  2782. if ((readb(p + i + 0) == 0x50) &&
  2783. (readb(p + i + 1) == 0x43) &&
  2784. (readb(p + i + 2) == 0x49) &&
  2785. (readb(p + i + 3) == 0x52)) {
  2786. base = p + (readb(p + i + 8) |
  2787. (readb(p + i + 9) << 8));
  2788. break;
  2789. }
  2790. }
  2791. if (!base || (readb(base) != 0x82))
  2792. goto use_random_mac_addr;
  2793. i = (readb(base + 1) | (readb(base + 2) << 8)) + 3;
  2794. while (i < EXPANSION_ROM_SIZE) {
  2795. if (readb(base + i) != 0x90) /* no vpd found */
  2796. goto use_random_mac_addr;
  2797. /* found a vpd field */
  2798. len = readb(base + i + 1) | (readb(base + i + 2) << 8);
  2799. /* extract keywords */
  2800. kstart = base + i + 3;
  2801. p = kstart;
  2802. while ((p - kstart) < len) {
  2803. int klen = readb(p + 2);
  2804. int j;
  2805. char type;
  2806. p += 3;
  2807. /* look for the following things:
  2808. * -- correct length == 29
  2809. * 3 (type) + 2 (size) +
  2810. * 18 (strlen("local-mac-address") + 1) +
  2811. * 6 (mac addr)
  2812. * -- VPD Instance 'I'
  2813. * -- VPD Type Bytes 'B'
  2814. * -- VPD data length == 6
  2815. * -- property string == local-mac-address
  2816. *
  2817. * -- correct length == 24
  2818. * 3 (type) + 2 (size) +
  2819. * 12 (strlen("entropy-dev") + 1) +
  2820. * 7 (strlen("vms110") + 1)
  2821. * -- VPD Instance 'I'
  2822. * -- VPD Type String 'B'
  2823. * -- VPD data length == 7
  2824. * -- property string == entropy-dev
  2825. *
  2826. * -- correct length == 18
  2827. * 3 (type) + 2 (size) +
  2828. * 9 (strlen("phy-type") + 1) +
  2829. * 4 (strlen("pcs") + 1)
  2830. * -- VPD Instance 'I'
  2831. * -- VPD Type String 'S'
  2832. * -- VPD data length == 4
  2833. * -- property string == phy-type
  2834. *
  2835. * -- correct length == 23
  2836. * 3 (type) + 2 (size) +
  2837. * 14 (strlen("phy-interface") + 1) +
  2838. * 4 (strlen("pcs") + 1)
  2839. * -- VPD Instance 'I'
  2840. * -- VPD Type String 'S'
  2841. * -- VPD data length == 4
  2842. * -- property string == phy-interface
  2843. */
  2844. if (readb(p) != 'I')
  2845. goto next;
  2846. /* finally, check string and length */
  2847. type = readb(p + 3);
  2848. if (type == 'B') {
  2849. if ((klen == 29) && readb(p + 4) == 6 &&
  2850. cas_vpd_match(p + 5,
  2851. "local-mac-address")) {
  2852. if (mac_off++ > offset)
  2853. goto next;
  2854. /* set mac address */
  2855. for (j = 0; j < 6; j++)
  2856. dev_addr[j] =
  2857. readb(p + 23 + j);
  2858. goto found_mac;
  2859. }
  2860. }
  2861. if (type != 'S')
  2862. goto next;
  2863. #ifdef USE_ENTROPY_DEV
  2864. if ((klen == 24) &&
  2865. cas_vpd_match(p + 5, "entropy-dev") &&
  2866. cas_vpd_match(p + 17, "vms110")) {
  2867. cp->cas_flags |= CAS_FLAG_ENTROPY_DEV;
  2868. goto next;
  2869. }
  2870. #endif
  2871. if (found & VPD_FOUND_PHY)
  2872. goto next;
  2873. if ((klen == 18) && readb(p + 4) == 4 &&
  2874. cas_vpd_match(p + 5, "phy-type")) {
  2875. if (cas_vpd_match(p + 14, "pcs")) {
  2876. phy_type = CAS_PHY_SERDES;
  2877. goto found_phy;
  2878. }
  2879. }
  2880. if ((klen == 23) && readb(p + 4) == 4 &&
  2881. cas_vpd_match(p + 5, "phy-interface")) {
  2882. if (cas_vpd_match(p + 19, "pcs")) {
  2883. phy_type = CAS_PHY_SERDES;
  2884. goto found_phy;
  2885. }
  2886. }
  2887. found_mac:
  2888. found |= VPD_FOUND_MAC;
  2889. goto next;
  2890. found_phy:
  2891. found |= VPD_FOUND_PHY;
  2892. next:
  2893. p += klen;
  2894. }
  2895. i += len + 3;
  2896. }
  2897. use_random_mac_addr:
  2898. if (found & VPD_FOUND_MAC)
  2899. goto done;
  2900. #if defined(CONFIG_SPARC)
  2901. addr = of_get_property(cp->of_node, "local-mac-address", NULL);
  2902. if (addr != NULL) {
  2903. memcpy(dev_addr, addr, 6);
  2904. goto done;
  2905. }
  2906. #endif
  2907. /* Sun MAC prefix then 3 random bytes. */
  2908. pr_info("MAC address not found in ROM VPD\n");
  2909. dev_addr[0] = 0x08;
  2910. dev_addr[1] = 0x00;
  2911. dev_addr[2] = 0x20;
  2912. get_random_bytes(dev_addr + 3, 3);
  2913. done:
  2914. writel(0, cp->regs + REG_BIM_LOCAL_DEV_EN);
  2915. return phy_type;
  2916. }
  2917. /* check pci invariants */
  2918. static void cas_check_pci_invariants(struct cas *cp)
  2919. {
  2920. struct pci_dev *pdev = cp->pdev;
  2921. cp->cas_flags = 0;
  2922. if ((pdev->vendor == PCI_VENDOR_ID_SUN) &&
  2923. (pdev->device == PCI_DEVICE_ID_SUN_CASSINI)) {
  2924. if (pdev->revision >= CAS_ID_REVPLUS)
  2925. cp->cas_flags |= CAS_FLAG_REG_PLUS;
  2926. if (pdev->revision < CAS_ID_REVPLUS02u)
  2927. cp->cas_flags |= CAS_FLAG_TARGET_ABORT;
  2928. /* Original Cassini supports HW CSUM, but it's not
  2929. * enabled by default as it can trigger TX hangs.
  2930. */
  2931. if (pdev->revision < CAS_ID_REV2)
  2932. cp->cas_flags |= CAS_FLAG_NO_HW_CSUM;
  2933. } else {
  2934. /* Only sun has original cassini chips. */
  2935. cp->cas_flags |= CAS_FLAG_REG_PLUS;
  2936. /* We use a flag because the same phy might be externally
  2937. * connected.
  2938. */
  2939. if ((pdev->vendor == PCI_VENDOR_ID_NS) &&
  2940. (pdev->device == PCI_DEVICE_ID_NS_SATURN))
  2941. cp->cas_flags |= CAS_FLAG_SATURN;
  2942. }
  2943. }
  2944. static int cas_check_invariants(struct cas *cp)
  2945. {
  2946. struct pci_dev *pdev = cp->pdev;
  2947. u32 cfg;
  2948. int i;
  2949. /* get page size for rx buffers. */
  2950. cp->page_order = 0;
  2951. #ifdef USE_PAGE_ORDER
  2952. if (PAGE_SHIFT < CAS_JUMBO_PAGE_SHIFT) {
  2953. /* see if we can allocate larger pages */
  2954. struct page *page = alloc_pages(GFP_ATOMIC,
  2955. CAS_JUMBO_PAGE_SHIFT -
  2956. PAGE_SHIFT);
  2957. if (page) {
  2958. __free_pages(page, CAS_JUMBO_PAGE_SHIFT - PAGE_SHIFT);
  2959. cp->page_order = CAS_JUMBO_PAGE_SHIFT - PAGE_SHIFT;
  2960. } else {
  2961. printk("MTU limited to %d bytes\n", CAS_MAX_MTU);
  2962. }
  2963. }
  2964. #endif
  2965. cp->page_size = (PAGE_SIZE << cp->page_order);
  2966. /* Fetch the FIFO configurations. */
  2967. cp->tx_fifo_size = readl(cp->regs + REG_TX_FIFO_SIZE) * 64;
  2968. cp->rx_fifo_size = RX_FIFO_SIZE;
  2969. /* finish phy determination. MDIO1 takes precedence over MDIO0 if
  2970. * they're both connected.
  2971. */
  2972. cp->phy_type = cas_get_vpd_info(cp, cp->dev->dev_addr,
  2973. PCI_SLOT(pdev->devfn));
  2974. if (cp->phy_type & CAS_PHY_SERDES) {
  2975. cp->cas_flags |= CAS_FLAG_1000MB_CAP;
  2976. return 0; /* no more checking needed */
  2977. }
  2978. /* MII */
  2979. cfg = readl(cp->regs + REG_MIF_CFG);
  2980. if (cfg & MIF_CFG_MDIO_1) {
  2981. cp->phy_type = CAS_PHY_MII_MDIO1;
  2982. } else if (cfg & MIF_CFG_MDIO_0) {
  2983. cp->phy_type = CAS_PHY_MII_MDIO0;
  2984. }
  2985. cas_mif_poll(cp, 0);
  2986. writel(PCS_DATAPATH_MODE_MII, cp->regs + REG_PCS_DATAPATH_MODE);
  2987. for (i = 0; i < 32; i++) {
  2988. u32 phy_id;
  2989. int j;
  2990. for (j = 0; j < 3; j++) {
  2991. cp->phy_addr = i;
  2992. phy_id = cas_phy_read(cp, MII_PHYSID1) << 16;
  2993. phy_id |= cas_phy_read(cp, MII_PHYSID2);
  2994. if (phy_id && (phy_id != 0xFFFFFFFF)) {
  2995. cp->phy_id = phy_id;
  2996. goto done;
  2997. }
  2998. }
  2999. }
  3000. pr_err("MII phy did not respond [%08x]\n",
  3001. readl(cp->regs + REG_MIF_STATE_MACHINE));
  3002. return -1;
  3003. done:
  3004. /* see if we can do gigabit */
  3005. cfg = cas_phy_read(cp, MII_BMSR);
  3006. if ((cfg & CAS_BMSR_1000_EXTEND) &&
  3007. cas_phy_read(cp, CAS_MII_1000_EXTEND))
  3008. cp->cas_flags |= CAS_FLAG_1000MB_CAP;
  3009. return 0;
  3010. }
  3011. /* Must be invoked under cp->lock. */
  3012. static inline void cas_start_dma(struct cas *cp)
  3013. {
  3014. int i;
  3015. u32 val;
  3016. int txfailed = 0;
  3017. /* enable dma */
  3018. val = readl(cp->regs + REG_TX_CFG) | TX_CFG_DMA_EN;
  3019. writel(val, cp->regs + REG_TX_CFG);
  3020. val = readl(cp->regs + REG_RX_CFG) | RX_CFG_DMA_EN;
  3021. writel(val, cp->regs + REG_RX_CFG);
  3022. /* enable the mac */
  3023. val = readl(cp->regs + REG_MAC_TX_CFG) | MAC_TX_CFG_EN;
  3024. writel(val, cp->regs + REG_MAC_TX_CFG);
  3025. val = readl(cp->regs + REG_MAC_RX_CFG) | MAC_RX_CFG_EN;
  3026. writel(val, cp->regs + REG_MAC_RX_CFG);
  3027. i = STOP_TRIES;
  3028. while (i-- > 0) {
  3029. val = readl(cp->regs + REG_MAC_TX_CFG);
  3030. if ((val & MAC_TX_CFG_EN))
  3031. break;
  3032. udelay(10);
  3033. }
  3034. if (i < 0) txfailed = 1;
  3035. i = STOP_TRIES;
  3036. while (i-- > 0) {
  3037. val = readl(cp->regs + REG_MAC_RX_CFG);
  3038. if ((val & MAC_RX_CFG_EN)) {
  3039. if (txfailed) {
  3040. netdev_err(cp->dev,
  3041. "enabling mac failed [tx:%08x:%08x]\n",
  3042. readl(cp->regs + REG_MIF_STATE_MACHINE),
  3043. readl(cp->regs + REG_MAC_STATE_MACHINE));
  3044. }
  3045. goto enable_rx_done;
  3046. }
  3047. udelay(10);
  3048. }
  3049. netdev_err(cp->dev, "enabling mac failed [%s:%08x:%08x]\n",
  3050. (txfailed ? "tx,rx" : "rx"),
  3051. readl(cp->regs + REG_MIF_STATE_MACHINE),
  3052. readl(cp->regs + REG_MAC_STATE_MACHINE));
  3053. enable_rx_done:
  3054. cas_unmask_intr(cp); /* enable interrupts */
  3055. writel(RX_DESC_RINGN_SIZE(0) - 4, cp->regs + REG_RX_KICK);
  3056. writel(0, cp->regs + REG_RX_COMP_TAIL);
  3057. if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
  3058. if (N_RX_DESC_RINGS > 1)
  3059. writel(RX_DESC_RINGN_SIZE(1) - 4,
  3060. cp->regs + REG_PLUS_RX_KICK1);
  3061. for (i = 1; i < N_RX_COMP_RINGS; i++)
  3062. writel(0, cp->regs + REG_PLUS_RX_COMPN_TAIL(i));
  3063. }
  3064. }
  3065. /* Must be invoked under cp->lock. */
  3066. static void cas_read_pcs_link_mode(struct cas *cp, int *fd, int *spd,
  3067. int *pause)
  3068. {
  3069. u32 val = readl(cp->regs + REG_PCS_MII_LPA);
  3070. *fd = (val & PCS_MII_LPA_FD) ? 1 : 0;
  3071. *pause = (val & PCS_MII_LPA_SYM_PAUSE) ? 0x01 : 0x00;
  3072. if (val & PCS_MII_LPA_ASYM_PAUSE)
  3073. *pause |= 0x10;
  3074. *spd = 1000;
  3075. }
  3076. /* Must be invoked under cp->lock. */
  3077. static void cas_read_mii_link_mode(struct cas *cp, int *fd, int *spd,
  3078. int *pause)
  3079. {
  3080. u32 val;
  3081. *fd = 0;
  3082. *spd = 10;
  3083. *pause = 0;
  3084. /* use GMII registers */
  3085. val = cas_phy_read(cp, MII_LPA);
  3086. if (val & CAS_LPA_PAUSE)
  3087. *pause = 0x01;
  3088. if (val & CAS_LPA_ASYM_PAUSE)
  3089. *pause |= 0x10;
  3090. if (val & LPA_DUPLEX)
  3091. *fd = 1;
  3092. if (val & LPA_100)
  3093. *spd = 100;
  3094. if (cp->cas_flags & CAS_FLAG_1000MB_CAP) {
  3095. val = cas_phy_read(cp, CAS_MII_1000_STATUS);
  3096. if (val & (CAS_LPA_1000FULL | CAS_LPA_1000HALF))
  3097. *spd = 1000;
  3098. if (val & CAS_LPA_1000FULL)
  3099. *fd = 1;
  3100. }
  3101. }
  3102. /* A link-up condition has occurred, initialize and enable the
  3103. * rest of the chip.
  3104. *
  3105. * Must be invoked under cp->lock.
  3106. */
  3107. static void cas_set_link_modes(struct cas *cp)
  3108. {
  3109. u32 val;
  3110. int full_duplex, speed, pause;
  3111. full_duplex = 0;
  3112. speed = 10;
  3113. pause = 0;
  3114. if (CAS_PHY_MII(cp->phy_type)) {
  3115. cas_mif_poll(cp, 0);
  3116. val = cas_phy_read(cp, MII_BMCR);
  3117. if (val & BMCR_ANENABLE) {
  3118. cas_read_mii_link_mode(cp, &full_duplex, &speed,
  3119. &pause);
  3120. } else {
  3121. if (val & BMCR_FULLDPLX)
  3122. full_duplex = 1;
  3123. if (val & BMCR_SPEED100)
  3124. speed = 100;
  3125. else if (val & CAS_BMCR_SPEED1000)
  3126. speed = (cp->cas_flags & CAS_FLAG_1000MB_CAP) ?
  3127. 1000 : 100;
  3128. }
  3129. cas_mif_poll(cp, 1);
  3130. } else {
  3131. val = readl(cp->regs + REG_PCS_MII_CTRL);
  3132. cas_read_pcs_link_mode(cp, &full_duplex, &speed, &pause);
  3133. if ((val & PCS_MII_AUTONEG_EN) == 0) {
  3134. if (val & PCS_MII_CTRL_DUPLEX)
  3135. full_duplex = 1;
  3136. }
  3137. }
  3138. netif_info(cp, link, cp->dev, "Link up at %d Mbps, %s-duplex\n",
  3139. speed, full_duplex ? "full" : "half");
  3140. val = MAC_XIF_TX_MII_OUTPUT_EN | MAC_XIF_LINK_LED;
  3141. if (CAS_PHY_MII(cp->phy_type)) {
  3142. val |= MAC_XIF_MII_BUFFER_OUTPUT_EN;
  3143. if (!full_duplex)
  3144. val |= MAC_XIF_DISABLE_ECHO;
  3145. }
  3146. if (full_duplex)
  3147. val |= MAC_XIF_FDPLX_LED;
  3148. if (speed == 1000)
  3149. val |= MAC_XIF_GMII_MODE;
  3150. writel(val, cp->regs + REG_MAC_XIF_CFG);
  3151. /* deal with carrier and collision detect. */
  3152. val = MAC_TX_CFG_IPG_EN;
  3153. if (full_duplex) {
  3154. val |= MAC_TX_CFG_IGNORE_CARRIER;
  3155. val |= MAC_TX_CFG_IGNORE_COLL;
  3156. } else {
  3157. #ifndef USE_CSMA_CD_PROTO
  3158. val |= MAC_TX_CFG_NEVER_GIVE_UP_EN;
  3159. val |= MAC_TX_CFG_NEVER_GIVE_UP_LIM;
  3160. #endif
  3161. }
  3162. /* val now set up for REG_MAC_TX_CFG */
  3163. /* If gigabit and half-duplex, enable carrier extension
  3164. * mode. increase slot time to 512 bytes as well.
  3165. * else, disable it and make sure slot time is 64 bytes.
  3166. * also activate checksum bug workaround
  3167. */
  3168. if ((speed == 1000) && !full_duplex) {
  3169. writel(val | MAC_TX_CFG_CARRIER_EXTEND,
  3170. cp->regs + REG_MAC_TX_CFG);
  3171. val = readl(cp->regs + REG_MAC_RX_CFG);
  3172. val &= ~MAC_RX_CFG_STRIP_FCS; /* checksum workaround */
  3173. writel(val | MAC_RX_CFG_CARRIER_EXTEND,
  3174. cp->regs + REG_MAC_RX_CFG);
  3175. writel(0x200, cp->regs + REG_MAC_SLOT_TIME);
  3176. cp->crc_size = 4;
  3177. /* minimum size gigabit frame at half duplex */
  3178. cp->min_frame_size = CAS_1000MB_MIN_FRAME;
  3179. } else {
  3180. writel(val, cp->regs + REG_MAC_TX_CFG);
  3181. /* checksum bug workaround. don't strip FCS when in
  3182. * half-duplex mode
  3183. */
  3184. val = readl(cp->regs + REG_MAC_RX_CFG);
  3185. if (full_duplex) {
  3186. val |= MAC_RX_CFG_STRIP_FCS;
  3187. cp->crc_size = 0;
  3188. cp->min_frame_size = CAS_MIN_MTU;
  3189. } else {
  3190. val &= ~MAC_RX_CFG_STRIP_FCS;
  3191. cp->crc_size = 4;
  3192. cp->min_frame_size = CAS_MIN_FRAME;
  3193. }
  3194. writel(val & ~MAC_RX_CFG_CARRIER_EXTEND,
  3195. cp->regs + REG_MAC_RX_CFG);
  3196. writel(0x40, cp->regs + REG_MAC_SLOT_TIME);
  3197. }
  3198. if (netif_msg_link(cp)) {
  3199. if (pause & 0x01) {
  3200. netdev_info(cp->dev, "Pause is enabled (rxfifo: %d off: %d on: %d)\n",
  3201. cp->rx_fifo_size,
  3202. cp->rx_pause_off,
  3203. cp->rx_pause_on);
  3204. } else if (pause & 0x10) {
  3205. netdev_info(cp->dev, "TX pause enabled\n");
  3206. } else {
  3207. netdev_info(cp->dev, "Pause is disabled\n");
  3208. }
  3209. }
  3210. val = readl(cp->regs + REG_MAC_CTRL_CFG);
  3211. val &= ~(MAC_CTRL_CFG_SEND_PAUSE_EN | MAC_CTRL_CFG_RECV_PAUSE_EN);
  3212. if (pause) { /* symmetric or asymmetric pause */
  3213. val |= MAC_CTRL_CFG_SEND_PAUSE_EN;
  3214. if (pause & 0x01) { /* symmetric pause */
  3215. val |= MAC_CTRL_CFG_RECV_PAUSE_EN;
  3216. }
  3217. }
  3218. writel(val, cp->regs + REG_MAC_CTRL_CFG);
  3219. cas_start_dma(cp);
  3220. }
  3221. /* Must be invoked under cp->lock. */
  3222. static void cas_init_hw(struct cas *cp, int restart_link)
  3223. {
  3224. if (restart_link)
  3225. cas_phy_init(cp);
  3226. cas_init_pause_thresholds(cp);
  3227. cas_init_mac(cp);
  3228. cas_init_dma(cp);
  3229. if (restart_link) {
  3230. /* Default aneg parameters */
  3231. cp->timer_ticks = 0;
  3232. cas_begin_auto_negotiation(cp, NULL);
  3233. } else if (cp->lstate == link_up) {
  3234. cas_set_link_modes(cp);
  3235. netif_carrier_on(cp->dev);
  3236. }
  3237. }
  3238. /* Must be invoked under cp->lock. on earlier cassini boards,
  3239. * SOFT_0 is tied to PCI reset. we use this to force a pci reset,
  3240. * let it settle out, and then restore pci state.
  3241. */
  3242. static void cas_hard_reset(struct cas *cp)
  3243. {
  3244. writel(BIM_LOCAL_DEV_SOFT_0, cp->regs + REG_BIM_LOCAL_DEV_EN);
  3245. udelay(20);
  3246. pci_restore_state(cp->pdev);
  3247. }
  3248. static void cas_global_reset(struct cas *cp, int blkflag)
  3249. {
  3250. int limit;
  3251. /* issue a global reset. don't use RSTOUT. */
  3252. if (blkflag && !CAS_PHY_MII(cp->phy_type)) {
  3253. /* For PCS, when the blkflag is set, we should set the
  3254. * SW_REST_BLOCK_PCS_SLINK bit to prevent the results of
  3255. * the last autonegotiation from being cleared. We'll
  3256. * need some special handling if the chip is set into a
  3257. * loopback mode.
  3258. */
  3259. writel((SW_RESET_TX | SW_RESET_RX | SW_RESET_BLOCK_PCS_SLINK),
  3260. cp->regs + REG_SW_RESET);
  3261. } else {
  3262. writel(SW_RESET_TX | SW_RESET_RX, cp->regs + REG_SW_RESET);
  3263. }
  3264. /* need to wait at least 3ms before polling register */
  3265. mdelay(3);
  3266. limit = STOP_TRIES;
  3267. while (limit-- > 0) {
  3268. u32 val = readl(cp->regs + REG_SW_RESET);
  3269. if ((val & (SW_RESET_TX | SW_RESET_RX)) == 0)
  3270. goto done;
  3271. udelay(10);
  3272. }
  3273. netdev_err(cp->dev, "sw reset failed\n");
  3274. done:
  3275. /* enable various BIM interrupts */
  3276. writel(BIM_CFG_DPAR_INTR_ENABLE | BIM_CFG_RMA_INTR_ENABLE |
  3277. BIM_CFG_RTA_INTR_ENABLE, cp->regs + REG_BIM_CFG);
  3278. /* clear out pci error status mask for handled errors.
  3279. * we don't deal with DMA counter overflows as they happen
  3280. * all the time.
  3281. */
  3282. writel(0xFFFFFFFFU & ~(PCI_ERR_BADACK | PCI_ERR_DTRTO |
  3283. PCI_ERR_OTHER | PCI_ERR_BIM_DMA_WRITE |
  3284. PCI_ERR_BIM_DMA_READ), cp->regs +
  3285. REG_PCI_ERR_STATUS_MASK);
  3286. /* set up for MII by default to address mac rx reset timeout
  3287. * issue
  3288. */
  3289. writel(PCS_DATAPATH_MODE_MII, cp->regs + REG_PCS_DATAPATH_MODE);
  3290. }
  3291. static void cas_reset(struct cas *cp, int blkflag)
  3292. {
  3293. u32 val;
  3294. cas_mask_intr(cp);
  3295. cas_global_reset(cp, blkflag);
  3296. cas_mac_reset(cp);
  3297. cas_entropy_reset(cp);
  3298. /* disable dma engines. */
  3299. val = readl(cp->regs + REG_TX_CFG);
  3300. val &= ~TX_CFG_DMA_EN;
  3301. writel(val, cp->regs + REG_TX_CFG);
  3302. val = readl(cp->regs + REG_RX_CFG);
  3303. val &= ~RX_CFG_DMA_EN;
  3304. writel(val, cp->regs + REG_RX_CFG);
  3305. /* program header parser */
  3306. if ((cp->cas_flags & CAS_FLAG_TARGET_ABORT) ||
  3307. (CAS_HP_ALT_FIRMWARE == cas_prog_null)) {
  3308. cas_load_firmware(cp, CAS_HP_FIRMWARE);
  3309. } else {
  3310. cas_load_firmware(cp, CAS_HP_ALT_FIRMWARE);
  3311. }
  3312. /* clear out error registers */
  3313. spin_lock(&cp->stat_lock[N_TX_RINGS]);
  3314. cas_clear_mac_err(cp);
  3315. spin_unlock(&cp->stat_lock[N_TX_RINGS]);
  3316. }
  3317. /* Shut down the chip, must be called with pm_mutex held. */
  3318. static void cas_shutdown(struct cas *cp)
  3319. {
  3320. unsigned long flags;
  3321. /* Make us not-running to avoid timers respawning */
  3322. cp->hw_running = 0;
  3323. del_timer_sync(&cp->link_timer);
  3324. /* Stop the reset task */
  3325. #if 0
  3326. while (atomic_read(&cp->reset_task_pending_mtu) ||
  3327. atomic_read(&cp->reset_task_pending_spare) ||
  3328. atomic_read(&cp->reset_task_pending_all))
  3329. schedule();
  3330. #else
  3331. while (atomic_read(&cp->reset_task_pending))
  3332. schedule();
  3333. #endif
  3334. /* Actually stop the chip */
  3335. cas_lock_all_save(cp, flags);
  3336. cas_reset(cp, 0);
  3337. if (cp->cas_flags & CAS_FLAG_SATURN)
  3338. cas_phy_powerdown(cp);
  3339. cas_unlock_all_restore(cp, flags);
  3340. }
  3341. static int cas_change_mtu(struct net_device *dev, int new_mtu)
  3342. {
  3343. struct cas *cp = netdev_priv(dev);
  3344. if (new_mtu < CAS_MIN_MTU || new_mtu > CAS_MAX_MTU)
  3345. return -EINVAL;
  3346. dev->mtu = new_mtu;
  3347. if (!netif_running(dev) || !netif_device_present(dev))
  3348. return 0;
  3349. /* let the reset task handle it */
  3350. #if 1
  3351. atomic_inc(&cp->reset_task_pending);
  3352. if ((cp->phy_type & CAS_PHY_SERDES)) {
  3353. atomic_inc(&cp->reset_task_pending_all);
  3354. } else {
  3355. atomic_inc(&cp->reset_task_pending_mtu);
  3356. }
  3357. schedule_work(&cp->reset_task);
  3358. #else
  3359. atomic_set(&cp->reset_task_pending, (cp->phy_type & CAS_PHY_SERDES) ?
  3360. CAS_RESET_ALL : CAS_RESET_MTU);
  3361. pr_err("reset called in cas_change_mtu\n");
  3362. schedule_work(&cp->reset_task);
  3363. #endif
  3364. flush_work_sync(&cp->reset_task);
  3365. return 0;
  3366. }
  3367. static void cas_clean_txd(struct cas *cp, int ring)
  3368. {
  3369. struct cas_tx_desc *txd = cp->init_txds[ring];
  3370. struct sk_buff *skb, **skbs = cp->tx_skbs[ring];
  3371. u64 daddr, dlen;
  3372. int i, size;
  3373. size = TX_DESC_RINGN_SIZE(ring);
  3374. for (i = 0; i < size; i++) {
  3375. int frag;
  3376. if (skbs[i] == NULL)
  3377. continue;
  3378. skb = skbs[i];
  3379. skbs[i] = NULL;
  3380. for (frag = 0; frag <= skb_shinfo(skb)->nr_frags; frag++) {
  3381. int ent = i & (size - 1);
  3382. /* first buffer is never a tiny buffer and so
  3383. * needs to be unmapped.
  3384. */
  3385. daddr = le64_to_cpu(txd[ent].buffer);
  3386. dlen = CAS_VAL(TX_DESC_BUFLEN,
  3387. le64_to_cpu(txd[ent].control));
  3388. pci_unmap_page(cp->pdev, daddr, dlen,
  3389. PCI_DMA_TODEVICE);
  3390. if (frag != skb_shinfo(skb)->nr_frags) {
  3391. i++;
  3392. /* next buffer might by a tiny buffer.
  3393. * skip past it.
  3394. */
  3395. ent = i & (size - 1);
  3396. if (cp->tx_tiny_use[ring][ent].used)
  3397. i++;
  3398. }
  3399. }
  3400. dev_kfree_skb_any(skb);
  3401. }
  3402. /* zero out tiny buf usage */
  3403. memset(cp->tx_tiny_use[ring], 0, size*sizeof(*cp->tx_tiny_use[ring]));
  3404. }
  3405. /* freed on close */
  3406. static inline void cas_free_rx_desc(struct cas *cp, int ring)
  3407. {
  3408. cas_page_t **page = cp->rx_pages[ring];
  3409. int i, size;
  3410. size = RX_DESC_RINGN_SIZE(ring);
  3411. for (i = 0; i < size; i++) {
  3412. if (page[i]) {
  3413. cas_page_free(cp, page[i]);
  3414. page[i] = NULL;
  3415. }
  3416. }
  3417. }
  3418. static void cas_free_rxds(struct cas *cp)
  3419. {
  3420. int i;
  3421. for (i = 0; i < N_RX_DESC_RINGS; i++)
  3422. cas_free_rx_desc(cp, i);
  3423. }
  3424. /* Must be invoked under cp->lock. */
  3425. static void cas_clean_rings(struct cas *cp)
  3426. {
  3427. int i;
  3428. /* need to clean all tx rings */
  3429. memset(cp->tx_old, 0, sizeof(*cp->tx_old)*N_TX_RINGS);
  3430. memset(cp->tx_new, 0, sizeof(*cp->tx_new)*N_TX_RINGS);
  3431. for (i = 0; i < N_TX_RINGS; i++)
  3432. cas_clean_txd(cp, i);
  3433. /* zero out init block */
  3434. memset(cp->init_block, 0, sizeof(struct cas_init_block));
  3435. cas_clean_rxds(cp);
  3436. cas_clean_rxcs(cp);
  3437. }
  3438. /* allocated on open */
  3439. static inline int cas_alloc_rx_desc(struct cas *cp, int ring)
  3440. {
  3441. cas_page_t **page = cp->rx_pages[ring];
  3442. int size, i = 0;
  3443. size = RX_DESC_RINGN_SIZE(ring);
  3444. for (i = 0; i < size; i++) {
  3445. if ((page[i] = cas_page_alloc(cp, GFP_KERNEL)) == NULL)
  3446. return -1;
  3447. }
  3448. return 0;
  3449. }
  3450. static int cas_alloc_rxds(struct cas *cp)
  3451. {
  3452. int i;
  3453. for (i = 0; i < N_RX_DESC_RINGS; i++) {
  3454. if (cas_alloc_rx_desc(cp, i) < 0) {
  3455. cas_free_rxds(cp);
  3456. return -1;
  3457. }
  3458. }
  3459. return 0;
  3460. }
  3461. static void cas_reset_task(struct work_struct *work)
  3462. {
  3463. struct cas *cp = container_of(work, struct cas, reset_task);
  3464. #if 0
  3465. int pending = atomic_read(&cp->reset_task_pending);
  3466. #else
  3467. int pending_all = atomic_read(&cp->reset_task_pending_all);
  3468. int pending_spare = atomic_read(&cp->reset_task_pending_spare);
  3469. int pending_mtu = atomic_read(&cp->reset_task_pending_mtu);
  3470. if (pending_all == 0 && pending_spare == 0 && pending_mtu == 0) {
  3471. /* We can have more tasks scheduled than actually
  3472. * needed.
  3473. */
  3474. atomic_dec(&cp->reset_task_pending);
  3475. return;
  3476. }
  3477. #endif
  3478. /* The link went down, we reset the ring, but keep
  3479. * DMA stopped. Use this function for reset
  3480. * on error as well.
  3481. */
  3482. if (cp->hw_running) {
  3483. unsigned long flags;
  3484. /* Make sure we don't get interrupts or tx packets */
  3485. netif_device_detach(cp->dev);
  3486. cas_lock_all_save(cp, flags);
  3487. if (cp->opened) {
  3488. /* We call cas_spare_recover when we call cas_open.
  3489. * but we do not initialize the lists cas_spare_recover
  3490. * uses until cas_open is called.
  3491. */
  3492. cas_spare_recover(cp, GFP_ATOMIC);
  3493. }
  3494. #if 1
  3495. /* test => only pending_spare set */
  3496. if (!pending_all && !pending_mtu)
  3497. goto done;
  3498. #else
  3499. if (pending == CAS_RESET_SPARE)
  3500. goto done;
  3501. #endif
  3502. /* when pending == CAS_RESET_ALL, the following
  3503. * call to cas_init_hw will restart auto negotiation.
  3504. * Setting the second argument of cas_reset to
  3505. * !(pending == CAS_RESET_ALL) will set this argument
  3506. * to 1 (avoiding reinitializing the PHY for the normal
  3507. * PCS case) when auto negotiation is not restarted.
  3508. */
  3509. #if 1
  3510. cas_reset(cp, !(pending_all > 0));
  3511. if (cp->opened)
  3512. cas_clean_rings(cp);
  3513. cas_init_hw(cp, (pending_all > 0));
  3514. #else
  3515. cas_reset(cp, !(pending == CAS_RESET_ALL));
  3516. if (cp->opened)
  3517. cas_clean_rings(cp);
  3518. cas_init_hw(cp, pending == CAS_RESET_ALL);
  3519. #endif
  3520. done:
  3521. cas_unlock_all_restore(cp, flags);
  3522. netif_device_attach(cp->dev);
  3523. }
  3524. #if 1
  3525. atomic_sub(pending_all, &cp->reset_task_pending_all);
  3526. atomic_sub(pending_spare, &cp->reset_task_pending_spare);
  3527. atomic_sub(pending_mtu, &cp->reset_task_pending_mtu);
  3528. atomic_dec(&cp->reset_task_pending);
  3529. #else
  3530. atomic_set(&cp->reset_task_pending, 0);
  3531. #endif
  3532. }
  3533. static void cas_link_timer(unsigned long data)
  3534. {
  3535. struct cas *cp = (struct cas *) data;
  3536. int mask, pending = 0, reset = 0;
  3537. unsigned long flags;
  3538. if (link_transition_timeout != 0 &&
  3539. cp->link_transition_jiffies_valid &&
  3540. ((jiffies - cp->link_transition_jiffies) >
  3541. (link_transition_timeout))) {
  3542. /* One-second counter so link-down workaround doesn't
  3543. * cause resets to occur so fast as to fool the switch
  3544. * into thinking the link is down.
  3545. */
  3546. cp->link_transition_jiffies_valid = 0;
  3547. }
  3548. if (!cp->hw_running)
  3549. return;
  3550. spin_lock_irqsave(&cp->lock, flags);
  3551. cas_lock_tx(cp);
  3552. cas_entropy_gather(cp);
  3553. /* If the link task is still pending, we just
  3554. * reschedule the link timer
  3555. */
  3556. #if 1
  3557. if (atomic_read(&cp->reset_task_pending_all) ||
  3558. atomic_read(&cp->reset_task_pending_spare) ||
  3559. atomic_read(&cp->reset_task_pending_mtu))
  3560. goto done;
  3561. #else
  3562. if (atomic_read(&cp->reset_task_pending))
  3563. goto done;
  3564. #endif
  3565. /* check for rx cleaning */
  3566. if ((mask = (cp->cas_flags & CAS_FLAG_RXD_POST_MASK))) {
  3567. int i, rmask;
  3568. for (i = 0; i < MAX_RX_DESC_RINGS; i++) {
  3569. rmask = CAS_FLAG_RXD_POST(i);
  3570. if ((mask & rmask) == 0)
  3571. continue;
  3572. /* post_rxds will do a mod_timer */
  3573. if (cas_post_rxds_ringN(cp, i, cp->rx_last[i]) < 0) {
  3574. pending = 1;
  3575. continue;
  3576. }
  3577. cp->cas_flags &= ~rmask;
  3578. }
  3579. }
  3580. if (CAS_PHY_MII(cp->phy_type)) {
  3581. u16 bmsr;
  3582. cas_mif_poll(cp, 0);
  3583. bmsr = cas_phy_read(cp, MII_BMSR);
  3584. /* WTZ: Solaris driver reads this twice, but that
  3585. * may be due to the PCS case and the use of a
  3586. * common implementation. Read it twice here to be
  3587. * safe.
  3588. */
  3589. bmsr = cas_phy_read(cp, MII_BMSR);
  3590. cas_mif_poll(cp, 1);
  3591. readl(cp->regs + REG_MIF_STATUS); /* avoid dups */
  3592. reset = cas_mii_link_check(cp, bmsr);
  3593. } else {
  3594. reset = cas_pcs_link_check(cp);
  3595. }
  3596. if (reset)
  3597. goto done;
  3598. /* check for tx state machine confusion */
  3599. if ((readl(cp->regs + REG_MAC_TX_STATUS) & MAC_TX_FRAME_XMIT) == 0) {
  3600. u32 val = readl(cp->regs + REG_MAC_STATE_MACHINE);
  3601. u32 wptr, rptr;
  3602. int tlm = CAS_VAL(MAC_SM_TLM, val);
  3603. if (((tlm == 0x5) || (tlm == 0x3)) &&
  3604. (CAS_VAL(MAC_SM_ENCAP_SM, val) == 0)) {
  3605. netif_printk(cp, tx_err, KERN_DEBUG, cp->dev,
  3606. "tx err: MAC_STATE[%08x]\n", val);
  3607. reset = 1;
  3608. goto done;
  3609. }
  3610. val = readl(cp->regs + REG_TX_FIFO_PKT_CNT);
  3611. wptr = readl(cp->regs + REG_TX_FIFO_WRITE_PTR);
  3612. rptr = readl(cp->regs + REG_TX_FIFO_READ_PTR);
  3613. if ((val == 0) && (wptr != rptr)) {
  3614. netif_printk(cp, tx_err, KERN_DEBUG, cp->dev,
  3615. "tx err: TX_FIFO[%08x:%08x:%08x]\n",
  3616. val, wptr, rptr);
  3617. reset = 1;
  3618. }
  3619. if (reset)
  3620. cas_hard_reset(cp);
  3621. }
  3622. done:
  3623. if (reset) {
  3624. #if 1
  3625. atomic_inc(&cp->reset_task_pending);
  3626. atomic_inc(&cp->reset_task_pending_all);
  3627. schedule_work(&cp->reset_task);
  3628. #else
  3629. atomic_set(&cp->reset_task_pending, CAS_RESET_ALL);
  3630. pr_err("reset called in cas_link_timer\n");
  3631. schedule_work(&cp->reset_task);
  3632. #endif
  3633. }
  3634. if (!pending)
  3635. mod_timer(&cp->link_timer, jiffies + CAS_LINK_TIMEOUT);
  3636. cas_unlock_tx(cp);
  3637. spin_unlock_irqrestore(&cp->lock, flags);
  3638. }
  3639. /* tiny buffers are used to avoid target abort issues with
  3640. * older cassini's
  3641. */
  3642. static void cas_tx_tiny_free(struct cas *cp)
  3643. {
  3644. struct pci_dev *pdev = cp->pdev;
  3645. int i;
  3646. for (i = 0; i < N_TX_RINGS; i++) {
  3647. if (!cp->tx_tiny_bufs[i])
  3648. continue;
  3649. pci_free_consistent(pdev, TX_TINY_BUF_BLOCK,
  3650. cp->tx_tiny_bufs[i],
  3651. cp->tx_tiny_dvma[i]);
  3652. cp->tx_tiny_bufs[i] = NULL;
  3653. }
  3654. }
  3655. static int cas_tx_tiny_alloc(struct cas *cp)
  3656. {
  3657. struct pci_dev *pdev = cp->pdev;
  3658. int i;
  3659. for (i = 0; i < N_TX_RINGS; i++) {
  3660. cp->tx_tiny_bufs[i] =
  3661. pci_alloc_consistent(pdev, TX_TINY_BUF_BLOCK,
  3662. &cp->tx_tiny_dvma[i]);
  3663. if (!cp->tx_tiny_bufs[i]) {
  3664. cas_tx_tiny_free(cp);
  3665. return -1;
  3666. }
  3667. }
  3668. return 0;
  3669. }
  3670. static int cas_open(struct net_device *dev)
  3671. {
  3672. struct cas *cp = netdev_priv(dev);
  3673. int hw_was_up, err;
  3674. unsigned long flags;
  3675. mutex_lock(&cp->pm_mutex);
  3676. hw_was_up = cp->hw_running;
  3677. /* The power-management mutex protects the hw_running
  3678. * etc. state so it is safe to do this bit without cp->lock
  3679. */
  3680. if (!cp->hw_running) {
  3681. /* Reset the chip */
  3682. cas_lock_all_save(cp, flags);
  3683. /* We set the second arg to cas_reset to zero
  3684. * because cas_init_hw below will have its second
  3685. * argument set to non-zero, which will force
  3686. * autonegotiation to start.
  3687. */
  3688. cas_reset(cp, 0);
  3689. cp->hw_running = 1;
  3690. cas_unlock_all_restore(cp, flags);
  3691. }
  3692. err = -ENOMEM;
  3693. if (cas_tx_tiny_alloc(cp) < 0)
  3694. goto err_unlock;
  3695. /* alloc rx descriptors */
  3696. if (cas_alloc_rxds(cp) < 0)
  3697. goto err_tx_tiny;
  3698. /* allocate spares */
  3699. cas_spare_init(cp);
  3700. cas_spare_recover(cp, GFP_KERNEL);
  3701. /* We can now request the interrupt as we know it's masked
  3702. * on the controller. cassini+ has up to 4 interrupts
  3703. * that can be used, but you need to do explicit pci interrupt
  3704. * mapping to expose them
  3705. */
  3706. if (request_irq(cp->pdev->irq, cas_interrupt,
  3707. IRQF_SHARED, dev->name, (void *) dev)) {
  3708. netdev_err(cp->dev, "failed to request irq !\n");
  3709. err = -EAGAIN;
  3710. goto err_spare;
  3711. }
  3712. #ifdef USE_NAPI
  3713. napi_enable(&cp->napi);
  3714. #endif
  3715. /* init hw */
  3716. cas_lock_all_save(cp, flags);
  3717. cas_clean_rings(cp);
  3718. cas_init_hw(cp, !hw_was_up);
  3719. cp->opened = 1;
  3720. cas_unlock_all_restore(cp, flags);
  3721. netif_start_queue(dev);
  3722. mutex_unlock(&cp->pm_mutex);
  3723. return 0;
  3724. err_spare:
  3725. cas_spare_free(cp);
  3726. cas_free_rxds(cp);
  3727. err_tx_tiny:
  3728. cas_tx_tiny_free(cp);
  3729. err_unlock:
  3730. mutex_unlock(&cp->pm_mutex);
  3731. return err;
  3732. }
  3733. static int cas_close(struct net_device *dev)
  3734. {
  3735. unsigned long flags;
  3736. struct cas *cp = netdev_priv(dev);
  3737. #ifdef USE_NAPI
  3738. napi_disable(&cp->napi);
  3739. #endif
  3740. /* Make sure we don't get distracted by suspend/resume */
  3741. mutex_lock(&cp->pm_mutex);
  3742. netif_stop_queue(dev);
  3743. /* Stop traffic, mark us closed */
  3744. cas_lock_all_save(cp, flags);
  3745. cp->opened = 0;
  3746. cas_reset(cp, 0);
  3747. cas_phy_init(cp);
  3748. cas_begin_auto_negotiation(cp, NULL);
  3749. cas_clean_rings(cp);
  3750. cas_unlock_all_restore(cp, flags);
  3751. free_irq(cp->pdev->irq, (void *) dev);
  3752. cas_spare_free(cp);
  3753. cas_free_rxds(cp);
  3754. cas_tx_tiny_free(cp);
  3755. mutex_unlock(&cp->pm_mutex);
  3756. return 0;
  3757. }
  3758. static struct {
  3759. const char name[ETH_GSTRING_LEN];
  3760. } ethtool_cassini_statnames[] = {
  3761. {"collisions"},
  3762. {"rx_bytes"},
  3763. {"rx_crc_errors"},
  3764. {"rx_dropped"},
  3765. {"rx_errors"},
  3766. {"rx_fifo_errors"},
  3767. {"rx_frame_errors"},
  3768. {"rx_length_errors"},
  3769. {"rx_over_errors"},
  3770. {"rx_packets"},
  3771. {"tx_aborted_errors"},
  3772. {"tx_bytes"},
  3773. {"tx_dropped"},
  3774. {"tx_errors"},
  3775. {"tx_fifo_errors"},
  3776. {"tx_packets"}
  3777. };
  3778. #define CAS_NUM_STAT_KEYS ARRAY_SIZE(ethtool_cassini_statnames)
  3779. static struct {
  3780. const int offsets; /* neg. values for 2nd arg to cas_read_phy */
  3781. } ethtool_register_table[] = {
  3782. {-MII_BMSR},
  3783. {-MII_BMCR},
  3784. {REG_CAWR},
  3785. {REG_INF_BURST},
  3786. {REG_BIM_CFG},
  3787. {REG_RX_CFG},
  3788. {REG_HP_CFG},
  3789. {REG_MAC_TX_CFG},
  3790. {REG_MAC_RX_CFG},
  3791. {REG_MAC_CTRL_CFG},
  3792. {REG_MAC_XIF_CFG},
  3793. {REG_MIF_CFG},
  3794. {REG_PCS_CFG},
  3795. {REG_SATURN_PCFG},
  3796. {REG_PCS_MII_STATUS},
  3797. {REG_PCS_STATE_MACHINE},
  3798. {REG_MAC_COLL_EXCESS},
  3799. {REG_MAC_COLL_LATE}
  3800. };
  3801. #define CAS_REG_LEN ARRAY_SIZE(ethtool_register_table)
  3802. #define CAS_MAX_REGS (sizeof (u32)*CAS_REG_LEN)
  3803. static void cas_read_regs(struct cas *cp, u8 *ptr, int len)
  3804. {
  3805. u8 *p;
  3806. int i;
  3807. unsigned long flags;
  3808. spin_lock_irqsave(&cp->lock, flags);
  3809. for (i = 0, p = ptr; i < len ; i ++, p += sizeof(u32)) {
  3810. u16 hval;
  3811. u32 val;
  3812. if (ethtool_register_table[i].offsets < 0) {
  3813. hval = cas_phy_read(cp,
  3814. -ethtool_register_table[i].offsets);
  3815. val = hval;
  3816. } else {
  3817. val= readl(cp->regs+ethtool_register_table[i].offsets);
  3818. }
  3819. memcpy(p, (u8 *)&val, sizeof(u32));
  3820. }
  3821. spin_unlock_irqrestore(&cp->lock, flags);
  3822. }
  3823. static struct net_device_stats *cas_get_stats(struct net_device *dev)
  3824. {
  3825. struct cas *cp = netdev_priv(dev);
  3826. struct net_device_stats *stats = cp->net_stats;
  3827. unsigned long flags;
  3828. int i;
  3829. unsigned long tmp;
  3830. /* we collate all of the stats into net_stats[N_TX_RING] */
  3831. if (!cp->hw_running)
  3832. return stats + N_TX_RINGS;
  3833. /* collect outstanding stats */
  3834. /* WTZ: the Cassini spec gives these as 16 bit counters but
  3835. * stored in 32-bit words. Added a mask of 0xffff to be safe,
  3836. * in case the chip somehow puts any garbage in the other bits.
  3837. * Also, counter usage didn't seem to mach what Adrian did
  3838. * in the parts of the code that set these quantities. Made
  3839. * that consistent.
  3840. */
  3841. spin_lock_irqsave(&cp->stat_lock[N_TX_RINGS], flags);
  3842. stats[N_TX_RINGS].rx_crc_errors +=
  3843. readl(cp->regs + REG_MAC_FCS_ERR) & 0xffff;
  3844. stats[N_TX_RINGS].rx_frame_errors +=
  3845. readl(cp->regs + REG_MAC_ALIGN_ERR) &0xffff;
  3846. stats[N_TX_RINGS].rx_length_errors +=
  3847. readl(cp->regs + REG_MAC_LEN_ERR) & 0xffff;
  3848. #if 1
  3849. tmp = (readl(cp->regs + REG_MAC_COLL_EXCESS) & 0xffff) +
  3850. (readl(cp->regs + REG_MAC_COLL_LATE) & 0xffff);
  3851. stats[N_TX_RINGS].tx_aborted_errors += tmp;
  3852. stats[N_TX_RINGS].collisions +=
  3853. tmp + (readl(cp->regs + REG_MAC_COLL_NORMAL) & 0xffff);
  3854. #else
  3855. stats[N_TX_RINGS].tx_aborted_errors +=
  3856. readl(cp->regs + REG_MAC_COLL_EXCESS);
  3857. stats[N_TX_RINGS].collisions += readl(cp->regs + REG_MAC_COLL_EXCESS) +
  3858. readl(cp->regs + REG_MAC_COLL_LATE);
  3859. #endif
  3860. cas_clear_mac_err(cp);
  3861. /* saved bits that are unique to ring 0 */
  3862. spin_lock(&cp->stat_lock[0]);
  3863. stats[N_TX_RINGS].collisions += stats[0].collisions;
  3864. stats[N_TX_RINGS].rx_over_errors += stats[0].rx_over_errors;
  3865. stats[N_TX_RINGS].rx_frame_errors += stats[0].rx_frame_errors;
  3866. stats[N_TX_RINGS].rx_fifo_errors += stats[0].rx_fifo_errors;
  3867. stats[N_TX_RINGS].tx_aborted_errors += stats[0].tx_aborted_errors;
  3868. stats[N_TX_RINGS].tx_fifo_errors += stats[0].tx_fifo_errors;
  3869. spin_unlock(&cp->stat_lock[0]);
  3870. for (i = 0; i < N_TX_RINGS; i++) {
  3871. spin_lock(&cp->stat_lock[i]);
  3872. stats[N_TX_RINGS].rx_length_errors +=
  3873. stats[i].rx_length_errors;
  3874. stats[N_TX_RINGS].rx_crc_errors += stats[i].rx_crc_errors;
  3875. stats[N_TX_RINGS].rx_packets += stats[i].rx_packets;
  3876. stats[N_TX_RINGS].tx_packets += stats[i].tx_packets;
  3877. stats[N_TX_RINGS].rx_bytes += stats[i].rx_bytes;
  3878. stats[N_TX_RINGS].tx_bytes += stats[i].tx_bytes;
  3879. stats[N_TX_RINGS].rx_errors += stats[i].rx_errors;
  3880. stats[N_TX_RINGS].tx_errors += stats[i].tx_errors;
  3881. stats[N_TX_RINGS].rx_dropped += stats[i].rx_dropped;
  3882. stats[N_TX_RINGS].tx_dropped += stats[i].tx_dropped;
  3883. memset(stats + i, 0, sizeof(struct net_device_stats));
  3884. spin_unlock(&cp->stat_lock[i]);
  3885. }
  3886. spin_unlock_irqrestore(&cp->stat_lock[N_TX_RINGS], flags);
  3887. return stats + N_TX_RINGS;
  3888. }
  3889. static void cas_set_multicast(struct net_device *dev)
  3890. {
  3891. struct cas *cp = netdev_priv(dev);
  3892. u32 rxcfg, rxcfg_new;
  3893. unsigned long flags;
  3894. int limit = STOP_TRIES;
  3895. if (!cp->hw_running)
  3896. return;
  3897. spin_lock_irqsave(&cp->lock, flags);
  3898. rxcfg = readl(cp->regs + REG_MAC_RX_CFG);
  3899. /* disable RX MAC and wait for completion */
  3900. writel(rxcfg & ~MAC_RX_CFG_EN, cp->regs + REG_MAC_RX_CFG);
  3901. while (readl(cp->regs + REG_MAC_RX_CFG) & MAC_RX_CFG_EN) {
  3902. if (!limit--)
  3903. break;
  3904. udelay(10);
  3905. }
  3906. /* disable hash filter and wait for completion */
  3907. limit = STOP_TRIES;
  3908. rxcfg &= ~(MAC_RX_CFG_PROMISC_EN | MAC_RX_CFG_HASH_FILTER_EN);
  3909. writel(rxcfg & ~MAC_RX_CFG_EN, cp->regs + REG_MAC_RX_CFG);
  3910. while (readl(cp->regs + REG_MAC_RX_CFG) & MAC_RX_CFG_HASH_FILTER_EN) {
  3911. if (!limit--)
  3912. break;
  3913. udelay(10);
  3914. }
  3915. /* program hash filters */
  3916. cp->mac_rx_cfg = rxcfg_new = cas_setup_multicast(cp);
  3917. rxcfg |= rxcfg_new;
  3918. writel(rxcfg, cp->regs + REG_MAC_RX_CFG);
  3919. spin_unlock_irqrestore(&cp->lock, flags);
  3920. }
  3921. static void cas_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  3922. {
  3923. struct cas *cp = netdev_priv(dev);
  3924. strncpy(info->driver, DRV_MODULE_NAME, ETHTOOL_BUSINFO_LEN);
  3925. strncpy(info->version, DRV_MODULE_VERSION, ETHTOOL_BUSINFO_LEN);
  3926. info->fw_version[0] = '\0';
  3927. strncpy(info->bus_info, pci_name(cp->pdev), ETHTOOL_BUSINFO_LEN);
  3928. info->regdump_len = cp->casreg_len < CAS_MAX_REGS ?
  3929. cp->casreg_len : CAS_MAX_REGS;
  3930. info->n_stats = CAS_NUM_STAT_KEYS;
  3931. }
  3932. static int cas_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  3933. {
  3934. struct cas *cp = netdev_priv(dev);
  3935. u16 bmcr;
  3936. int full_duplex, speed, pause;
  3937. unsigned long flags;
  3938. enum link_state linkstate = link_up;
  3939. cmd->advertising = 0;
  3940. cmd->supported = SUPPORTED_Autoneg;
  3941. if (cp->cas_flags & CAS_FLAG_1000MB_CAP) {
  3942. cmd->supported |= SUPPORTED_1000baseT_Full;
  3943. cmd->advertising |= ADVERTISED_1000baseT_Full;
  3944. }
  3945. /* Record PHY settings if HW is on. */
  3946. spin_lock_irqsave(&cp->lock, flags);
  3947. bmcr = 0;
  3948. linkstate = cp->lstate;
  3949. if (CAS_PHY_MII(cp->phy_type)) {
  3950. cmd->port = PORT_MII;
  3951. cmd->transceiver = (cp->cas_flags & CAS_FLAG_SATURN) ?
  3952. XCVR_INTERNAL : XCVR_EXTERNAL;
  3953. cmd->phy_address = cp->phy_addr;
  3954. cmd->advertising |= ADVERTISED_TP | ADVERTISED_MII |
  3955. ADVERTISED_10baseT_Half |
  3956. ADVERTISED_10baseT_Full |
  3957. ADVERTISED_100baseT_Half |
  3958. ADVERTISED_100baseT_Full;
  3959. cmd->supported |=
  3960. (SUPPORTED_10baseT_Half |
  3961. SUPPORTED_10baseT_Full |
  3962. SUPPORTED_100baseT_Half |
  3963. SUPPORTED_100baseT_Full |
  3964. SUPPORTED_TP | SUPPORTED_MII);
  3965. if (cp->hw_running) {
  3966. cas_mif_poll(cp, 0);
  3967. bmcr = cas_phy_read(cp, MII_BMCR);
  3968. cas_read_mii_link_mode(cp, &full_duplex,
  3969. &speed, &pause);
  3970. cas_mif_poll(cp, 1);
  3971. }
  3972. } else {
  3973. cmd->port = PORT_FIBRE;
  3974. cmd->transceiver = XCVR_INTERNAL;
  3975. cmd->phy_address = 0;
  3976. cmd->supported |= SUPPORTED_FIBRE;
  3977. cmd->advertising |= ADVERTISED_FIBRE;
  3978. if (cp->hw_running) {
  3979. /* pcs uses the same bits as mii */
  3980. bmcr = readl(cp->regs + REG_PCS_MII_CTRL);
  3981. cas_read_pcs_link_mode(cp, &full_duplex,
  3982. &speed, &pause);
  3983. }
  3984. }
  3985. spin_unlock_irqrestore(&cp->lock, flags);
  3986. if (bmcr & BMCR_ANENABLE) {
  3987. cmd->advertising |= ADVERTISED_Autoneg;
  3988. cmd->autoneg = AUTONEG_ENABLE;
  3989. ethtool_cmd_speed_set(cmd, ((speed == 10) ?
  3990. SPEED_10 :
  3991. ((speed == 1000) ?
  3992. SPEED_1000 : SPEED_100)));
  3993. cmd->duplex = full_duplex ? DUPLEX_FULL : DUPLEX_HALF;
  3994. } else {
  3995. cmd->autoneg = AUTONEG_DISABLE;
  3996. ethtool_cmd_speed_set(cmd, ((bmcr & CAS_BMCR_SPEED1000) ?
  3997. SPEED_1000 :
  3998. ((bmcr & BMCR_SPEED100) ?
  3999. SPEED_100 : SPEED_10)));
  4000. cmd->duplex =
  4001. (bmcr & BMCR_FULLDPLX) ?
  4002. DUPLEX_FULL : DUPLEX_HALF;
  4003. }
  4004. if (linkstate != link_up) {
  4005. /* Force these to "unknown" if the link is not up and
  4006. * autonogotiation in enabled. We can set the link
  4007. * speed to 0, but not cmd->duplex,
  4008. * because its legal values are 0 and 1. Ethtool will
  4009. * print the value reported in parentheses after the
  4010. * word "Unknown" for unrecognized values.
  4011. *
  4012. * If in forced mode, we report the speed and duplex
  4013. * settings that we configured.
  4014. */
  4015. if (cp->link_cntl & BMCR_ANENABLE) {
  4016. ethtool_cmd_speed_set(cmd, 0);
  4017. cmd->duplex = 0xff;
  4018. } else {
  4019. ethtool_cmd_speed_set(cmd, SPEED_10);
  4020. if (cp->link_cntl & BMCR_SPEED100) {
  4021. ethtool_cmd_speed_set(cmd, SPEED_100);
  4022. } else if (cp->link_cntl & CAS_BMCR_SPEED1000) {
  4023. ethtool_cmd_speed_set(cmd, SPEED_1000);
  4024. }
  4025. cmd->duplex = (cp->link_cntl & BMCR_FULLDPLX)?
  4026. DUPLEX_FULL : DUPLEX_HALF;
  4027. }
  4028. }
  4029. return 0;
  4030. }
  4031. static int cas_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  4032. {
  4033. struct cas *cp = netdev_priv(dev);
  4034. unsigned long flags;
  4035. u32 speed = ethtool_cmd_speed(cmd);
  4036. /* Verify the settings we care about. */
  4037. if (cmd->autoneg != AUTONEG_ENABLE &&
  4038. cmd->autoneg != AUTONEG_DISABLE)
  4039. return -EINVAL;
  4040. if (cmd->autoneg == AUTONEG_DISABLE &&
  4041. ((speed != SPEED_1000 &&
  4042. speed != SPEED_100 &&
  4043. speed != SPEED_10) ||
  4044. (cmd->duplex != DUPLEX_HALF &&
  4045. cmd->duplex != DUPLEX_FULL)))
  4046. return -EINVAL;
  4047. /* Apply settings and restart link process. */
  4048. spin_lock_irqsave(&cp->lock, flags);
  4049. cas_begin_auto_negotiation(cp, cmd);
  4050. spin_unlock_irqrestore(&cp->lock, flags);
  4051. return 0;
  4052. }
  4053. static int cas_nway_reset(struct net_device *dev)
  4054. {
  4055. struct cas *cp = netdev_priv(dev);
  4056. unsigned long flags;
  4057. if ((cp->link_cntl & BMCR_ANENABLE) == 0)
  4058. return -EINVAL;
  4059. /* Restart link process. */
  4060. spin_lock_irqsave(&cp->lock, flags);
  4061. cas_begin_auto_negotiation(cp, NULL);
  4062. spin_unlock_irqrestore(&cp->lock, flags);
  4063. return 0;
  4064. }
  4065. static u32 cas_get_link(struct net_device *dev)
  4066. {
  4067. struct cas *cp = netdev_priv(dev);
  4068. return cp->lstate == link_up;
  4069. }
  4070. static u32 cas_get_msglevel(struct net_device *dev)
  4071. {
  4072. struct cas *cp = netdev_priv(dev);
  4073. return cp->msg_enable;
  4074. }
  4075. static void cas_set_msglevel(struct net_device *dev, u32 value)
  4076. {
  4077. struct cas *cp = netdev_priv(dev);
  4078. cp->msg_enable = value;
  4079. }
  4080. static int cas_get_regs_len(struct net_device *dev)
  4081. {
  4082. struct cas *cp = netdev_priv(dev);
  4083. return cp->casreg_len < CAS_MAX_REGS ? cp->casreg_len: CAS_MAX_REGS;
  4084. }
  4085. static void cas_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  4086. void *p)
  4087. {
  4088. struct cas *cp = netdev_priv(dev);
  4089. regs->version = 0;
  4090. /* cas_read_regs handles locks (cp->lock). */
  4091. cas_read_regs(cp, p, regs->len / sizeof(u32));
  4092. }
  4093. static int cas_get_sset_count(struct net_device *dev, int sset)
  4094. {
  4095. switch (sset) {
  4096. case ETH_SS_STATS:
  4097. return CAS_NUM_STAT_KEYS;
  4098. default:
  4099. return -EOPNOTSUPP;
  4100. }
  4101. }
  4102. static void cas_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  4103. {
  4104. memcpy(data, &ethtool_cassini_statnames,
  4105. CAS_NUM_STAT_KEYS * ETH_GSTRING_LEN);
  4106. }
  4107. static void cas_get_ethtool_stats(struct net_device *dev,
  4108. struct ethtool_stats *estats, u64 *data)
  4109. {
  4110. struct cas *cp = netdev_priv(dev);
  4111. struct net_device_stats *stats = cas_get_stats(cp->dev);
  4112. int i = 0;
  4113. data[i++] = stats->collisions;
  4114. data[i++] = stats->rx_bytes;
  4115. data[i++] = stats->rx_crc_errors;
  4116. data[i++] = stats->rx_dropped;
  4117. data[i++] = stats->rx_errors;
  4118. data[i++] = stats->rx_fifo_errors;
  4119. data[i++] = stats->rx_frame_errors;
  4120. data[i++] = stats->rx_length_errors;
  4121. data[i++] = stats->rx_over_errors;
  4122. data[i++] = stats->rx_packets;
  4123. data[i++] = stats->tx_aborted_errors;
  4124. data[i++] = stats->tx_bytes;
  4125. data[i++] = stats->tx_dropped;
  4126. data[i++] = stats->tx_errors;
  4127. data[i++] = stats->tx_fifo_errors;
  4128. data[i++] = stats->tx_packets;
  4129. BUG_ON(i != CAS_NUM_STAT_KEYS);
  4130. }
  4131. static const struct ethtool_ops cas_ethtool_ops = {
  4132. .get_drvinfo = cas_get_drvinfo,
  4133. .get_settings = cas_get_settings,
  4134. .set_settings = cas_set_settings,
  4135. .nway_reset = cas_nway_reset,
  4136. .get_link = cas_get_link,
  4137. .get_msglevel = cas_get_msglevel,
  4138. .set_msglevel = cas_set_msglevel,
  4139. .get_regs_len = cas_get_regs_len,
  4140. .get_regs = cas_get_regs,
  4141. .get_sset_count = cas_get_sset_count,
  4142. .get_strings = cas_get_strings,
  4143. .get_ethtool_stats = cas_get_ethtool_stats,
  4144. };
  4145. static int cas_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  4146. {
  4147. struct cas *cp = netdev_priv(dev);
  4148. struct mii_ioctl_data *data = if_mii(ifr);
  4149. unsigned long flags;
  4150. int rc = -EOPNOTSUPP;
  4151. /* Hold the PM mutex while doing ioctl's or we may collide
  4152. * with open/close and power management and oops.
  4153. */
  4154. mutex_lock(&cp->pm_mutex);
  4155. switch (cmd) {
  4156. case SIOCGMIIPHY: /* Get address of MII PHY in use. */
  4157. data->phy_id = cp->phy_addr;
  4158. /* Fallthrough... */
  4159. case SIOCGMIIREG: /* Read MII PHY register. */
  4160. spin_lock_irqsave(&cp->lock, flags);
  4161. cas_mif_poll(cp, 0);
  4162. data->val_out = cas_phy_read(cp, data->reg_num & 0x1f);
  4163. cas_mif_poll(cp, 1);
  4164. spin_unlock_irqrestore(&cp->lock, flags);
  4165. rc = 0;
  4166. break;
  4167. case SIOCSMIIREG: /* Write MII PHY register. */
  4168. spin_lock_irqsave(&cp->lock, flags);
  4169. cas_mif_poll(cp, 0);
  4170. rc = cas_phy_write(cp, data->reg_num & 0x1f, data->val_in);
  4171. cas_mif_poll(cp, 1);
  4172. spin_unlock_irqrestore(&cp->lock, flags);
  4173. break;
  4174. default:
  4175. break;
  4176. }
  4177. mutex_unlock(&cp->pm_mutex);
  4178. return rc;
  4179. }
  4180. /* When this chip sits underneath an Intel 31154 bridge, it is the
  4181. * only subordinate device and we can tweak the bridge settings to
  4182. * reflect that fact.
  4183. */
  4184. static void __devinit cas_program_bridge(struct pci_dev *cas_pdev)
  4185. {
  4186. struct pci_dev *pdev = cas_pdev->bus->self;
  4187. u32 val;
  4188. if (!pdev)
  4189. return;
  4190. if (pdev->vendor != 0x8086 || pdev->device != 0x537c)
  4191. return;
  4192. /* Clear bit 10 (Bus Parking Control) in the Secondary
  4193. * Arbiter Control/Status Register which lives at offset
  4194. * 0x41. Using a 32-bit word read/modify/write at 0x40
  4195. * is much simpler so that's how we do this.
  4196. */
  4197. pci_read_config_dword(pdev, 0x40, &val);
  4198. val &= ~0x00040000;
  4199. pci_write_config_dword(pdev, 0x40, val);
  4200. /* Max out the Multi-Transaction Timer settings since
  4201. * Cassini is the only device present.
  4202. *
  4203. * The register is 16-bit and lives at 0x50. When the
  4204. * settings are enabled, it extends the GRANT# signal
  4205. * for a requestor after a transaction is complete. This
  4206. * allows the next request to run without first needing
  4207. * to negotiate the GRANT# signal back.
  4208. *
  4209. * Bits 12:10 define the grant duration:
  4210. *
  4211. * 1 -- 16 clocks
  4212. * 2 -- 32 clocks
  4213. * 3 -- 64 clocks
  4214. * 4 -- 128 clocks
  4215. * 5 -- 256 clocks
  4216. *
  4217. * All other values are illegal.
  4218. *
  4219. * Bits 09:00 define which REQ/GNT signal pairs get the
  4220. * GRANT# signal treatment. We set them all.
  4221. */
  4222. pci_write_config_word(pdev, 0x50, (5 << 10) | 0x3ff);
  4223. /* The Read Prefecth Policy register is 16-bit and sits at
  4224. * offset 0x52. It enables a "smart" pre-fetch policy. We
  4225. * enable it and max out all of the settings since only one
  4226. * device is sitting underneath and thus bandwidth sharing is
  4227. * not an issue.
  4228. *
  4229. * The register has several 3 bit fields, which indicates a
  4230. * multiplier applied to the base amount of prefetching the
  4231. * chip would do. These fields are at:
  4232. *
  4233. * 15:13 --- ReRead Primary Bus
  4234. * 12:10 --- FirstRead Primary Bus
  4235. * 09:07 --- ReRead Secondary Bus
  4236. * 06:04 --- FirstRead Secondary Bus
  4237. *
  4238. * Bits 03:00 control which REQ/GNT pairs the prefetch settings
  4239. * get enabled on. Bit 3 is a grouped enabler which controls
  4240. * all of the REQ/GNT pairs from [8:3]. Bits 2 to 0 control
  4241. * the individual REQ/GNT pairs [2:0].
  4242. */
  4243. pci_write_config_word(pdev, 0x52,
  4244. (0x7 << 13) |
  4245. (0x7 << 10) |
  4246. (0x7 << 7) |
  4247. (0x7 << 4) |
  4248. (0xf << 0));
  4249. /* Force cacheline size to 0x8 */
  4250. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
  4251. /* Force latency timer to maximum setting so Cassini can
  4252. * sit on the bus as long as it likes.
  4253. */
  4254. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xff);
  4255. }
  4256. static const struct net_device_ops cas_netdev_ops = {
  4257. .ndo_open = cas_open,
  4258. .ndo_stop = cas_close,
  4259. .ndo_start_xmit = cas_start_xmit,
  4260. .ndo_get_stats = cas_get_stats,
  4261. .ndo_set_multicast_list = cas_set_multicast,
  4262. .ndo_do_ioctl = cas_ioctl,
  4263. .ndo_tx_timeout = cas_tx_timeout,
  4264. .ndo_change_mtu = cas_change_mtu,
  4265. .ndo_set_mac_address = eth_mac_addr,
  4266. .ndo_validate_addr = eth_validate_addr,
  4267. #ifdef CONFIG_NET_POLL_CONTROLLER
  4268. .ndo_poll_controller = cas_netpoll,
  4269. #endif
  4270. };
  4271. static int __devinit cas_init_one(struct pci_dev *pdev,
  4272. const struct pci_device_id *ent)
  4273. {
  4274. static int cas_version_printed = 0;
  4275. unsigned long casreg_len;
  4276. struct net_device *dev;
  4277. struct cas *cp;
  4278. int i, err, pci_using_dac;
  4279. u16 pci_cmd;
  4280. u8 orig_cacheline_size = 0, cas_cacheline_size = 0;
  4281. if (cas_version_printed++ == 0)
  4282. pr_info("%s", version);
  4283. err = pci_enable_device(pdev);
  4284. if (err) {
  4285. dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
  4286. return err;
  4287. }
  4288. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  4289. dev_err(&pdev->dev, "Cannot find proper PCI device "
  4290. "base address, aborting\n");
  4291. err = -ENODEV;
  4292. goto err_out_disable_pdev;
  4293. }
  4294. dev = alloc_etherdev(sizeof(*cp));
  4295. if (!dev) {
  4296. dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
  4297. err = -ENOMEM;
  4298. goto err_out_disable_pdev;
  4299. }
  4300. SET_NETDEV_DEV(dev, &pdev->dev);
  4301. err = pci_request_regions(pdev, dev->name);
  4302. if (err) {
  4303. dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
  4304. goto err_out_free_netdev;
  4305. }
  4306. pci_set_master(pdev);
  4307. /* we must always turn on parity response or else parity
  4308. * doesn't get generated properly. disable SERR/PERR as well.
  4309. * in addition, we want to turn MWI on.
  4310. */
  4311. pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
  4312. pci_cmd &= ~PCI_COMMAND_SERR;
  4313. pci_cmd |= PCI_COMMAND_PARITY;
  4314. pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
  4315. if (pci_try_set_mwi(pdev))
  4316. pr_warning("Could not enable MWI for %s\n", pci_name(pdev));
  4317. cas_program_bridge(pdev);
  4318. /*
  4319. * On some architectures, the default cache line size set
  4320. * by pci_try_set_mwi reduces perforamnce. We have to increase
  4321. * it for this case. To start, we'll print some configuration
  4322. * data.
  4323. */
  4324. #if 1
  4325. pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE,
  4326. &orig_cacheline_size);
  4327. if (orig_cacheline_size < CAS_PREF_CACHELINE_SIZE) {
  4328. cas_cacheline_size =
  4329. (CAS_PREF_CACHELINE_SIZE < SMP_CACHE_BYTES) ?
  4330. CAS_PREF_CACHELINE_SIZE : SMP_CACHE_BYTES;
  4331. if (pci_write_config_byte(pdev,
  4332. PCI_CACHE_LINE_SIZE,
  4333. cas_cacheline_size)) {
  4334. dev_err(&pdev->dev, "Could not set PCI cache "
  4335. "line size\n");
  4336. goto err_write_cacheline;
  4337. }
  4338. }
  4339. #endif
  4340. /* Configure DMA attributes. */
  4341. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
  4342. pci_using_dac = 1;
  4343. err = pci_set_consistent_dma_mask(pdev,
  4344. DMA_BIT_MASK(64));
  4345. if (err < 0) {
  4346. dev_err(&pdev->dev, "Unable to obtain 64-bit DMA "
  4347. "for consistent allocations\n");
  4348. goto err_out_free_res;
  4349. }
  4350. } else {
  4351. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  4352. if (err) {
  4353. dev_err(&pdev->dev, "No usable DMA configuration, "
  4354. "aborting\n");
  4355. goto err_out_free_res;
  4356. }
  4357. pci_using_dac = 0;
  4358. }
  4359. casreg_len = pci_resource_len(pdev, 0);
  4360. cp = netdev_priv(dev);
  4361. cp->pdev = pdev;
  4362. #if 1
  4363. /* A value of 0 indicates we never explicitly set it */
  4364. cp->orig_cacheline_size = cas_cacheline_size ? orig_cacheline_size: 0;
  4365. #endif
  4366. cp->dev = dev;
  4367. cp->msg_enable = (cassini_debug < 0) ? CAS_DEF_MSG_ENABLE :
  4368. cassini_debug;
  4369. #if defined(CONFIG_SPARC)
  4370. cp->of_node = pci_device_to_OF_node(pdev);
  4371. #endif
  4372. cp->link_transition = LINK_TRANSITION_UNKNOWN;
  4373. cp->link_transition_jiffies_valid = 0;
  4374. spin_lock_init(&cp->lock);
  4375. spin_lock_init(&cp->rx_inuse_lock);
  4376. spin_lock_init(&cp->rx_spare_lock);
  4377. for (i = 0; i < N_TX_RINGS; i++) {
  4378. spin_lock_init(&cp->stat_lock[i]);
  4379. spin_lock_init(&cp->tx_lock[i]);
  4380. }
  4381. spin_lock_init(&cp->stat_lock[N_TX_RINGS]);
  4382. mutex_init(&cp->pm_mutex);
  4383. init_timer(&cp->link_timer);
  4384. cp->link_timer.function = cas_link_timer;
  4385. cp->link_timer.data = (unsigned long) cp;
  4386. #if 1
  4387. /* Just in case the implementation of atomic operations
  4388. * change so that an explicit initialization is necessary.
  4389. */
  4390. atomic_set(&cp->reset_task_pending, 0);
  4391. atomic_set(&cp->reset_task_pending_all, 0);
  4392. atomic_set(&cp->reset_task_pending_spare, 0);
  4393. atomic_set(&cp->reset_task_pending_mtu, 0);
  4394. #endif
  4395. INIT_WORK(&cp->reset_task, cas_reset_task);
  4396. /* Default link parameters */
  4397. if (link_mode >= 0 && link_mode < 6)
  4398. cp->link_cntl = link_modes[link_mode];
  4399. else
  4400. cp->link_cntl = BMCR_ANENABLE;
  4401. cp->lstate = link_down;
  4402. cp->link_transition = LINK_TRANSITION_LINK_DOWN;
  4403. netif_carrier_off(cp->dev);
  4404. cp->timer_ticks = 0;
  4405. /* give us access to cassini registers */
  4406. cp->regs = pci_iomap(pdev, 0, casreg_len);
  4407. if (!cp->regs) {
  4408. dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
  4409. goto err_out_free_res;
  4410. }
  4411. cp->casreg_len = casreg_len;
  4412. pci_save_state(pdev);
  4413. cas_check_pci_invariants(cp);
  4414. cas_hard_reset(cp);
  4415. cas_reset(cp, 0);
  4416. if (cas_check_invariants(cp))
  4417. goto err_out_iounmap;
  4418. if (cp->cas_flags & CAS_FLAG_SATURN)
  4419. if (cas_saturn_firmware_init(cp))
  4420. goto err_out_iounmap;
  4421. cp->init_block = (struct cas_init_block *)
  4422. pci_alloc_consistent(pdev, sizeof(struct cas_init_block),
  4423. &cp->block_dvma);
  4424. if (!cp->init_block) {
  4425. dev_err(&pdev->dev, "Cannot allocate init block, aborting\n");
  4426. goto err_out_iounmap;
  4427. }
  4428. for (i = 0; i < N_TX_RINGS; i++)
  4429. cp->init_txds[i] = cp->init_block->txds[i];
  4430. for (i = 0; i < N_RX_DESC_RINGS; i++)
  4431. cp->init_rxds[i] = cp->init_block->rxds[i];
  4432. for (i = 0; i < N_RX_COMP_RINGS; i++)
  4433. cp->init_rxcs[i] = cp->init_block->rxcs[i];
  4434. for (i = 0; i < N_RX_FLOWS; i++)
  4435. skb_queue_head_init(&cp->rx_flows[i]);
  4436. dev->netdev_ops = &cas_netdev_ops;
  4437. dev->ethtool_ops = &cas_ethtool_ops;
  4438. dev->watchdog_timeo = CAS_TX_TIMEOUT;
  4439. #ifdef USE_NAPI
  4440. netif_napi_add(dev, &cp->napi, cas_poll, 64);
  4441. #endif
  4442. dev->irq = pdev->irq;
  4443. dev->dma = 0;
  4444. /* Cassini features. */
  4445. if ((cp->cas_flags & CAS_FLAG_NO_HW_CSUM) == 0)
  4446. dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
  4447. if (pci_using_dac)
  4448. dev->features |= NETIF_F_HIGHDMA;
  4449. if (register_netdev(dev)) {
  4450. dev_err(&pdev->dev, "Cannot register net device, aborting\n");
  4451. goto err_out_free_consistent;
  4452. }
  4453. i = readl(cp->regs + REG_BIM_CFG);
  4454. netdev_info(dev, "Sun Cassini%s (%sbit/%sMHz PCI/%s) Ethernet[%d] %pM\n",
  4455. (cp->cas_flags & CAS_FLAG_REG_PLUS) ? "+" : "",
  4456. (i & BIM_CFG_32BIT) ? "32" : "64",
  4457. (i & BIM_CFG_66MHZ) ? "66" : "33",
  4458. (cp->phy_type == CAS_PHY_SERDES) ? "Fi" : "Cu", pdev->irq,
  4459. dev->dev_addr);
  4460. pci_set_drvdata(pdev, dev);
  4461. cp->hw_running = 1;
  4462. cas_entropy_reset(cp);
  4463. cas_phy_init(cp);
  4464. cas_begin_auto_negotiation(cp, NULL);
  4465. return 0;
  4466. err_out_free_consistent:
  4467. pci_free_consistent(pdev, sizeof(struct cas_init_block),
  4468. cp->init_block, cp->block_dvma);
  4469. err_out_iounmap:
  4470. mutex_lock(&cp->pm_mutex);
  4471. if (cp->hw_running)
  4472. cas_shutdown(cp);
  4473. mutex_unlock(&cp->pm_mutex);
  4474. pci_iounmap(pdev, cp->regs);
  4475. err_out_free_res:
  4476. pci_release_regions(pdev);
  4477. err_write_cacheline:
  4478. /* Try to restore it in case the error occurred after we
  4479. * set it.
  4480. */
  4481. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, orig_cacheline_size);
  4482. err_out_free_netdev:
  4483. free_netdev(dev);
  4484. err_out_disable_pdev:
  4485. pci_disable_device(pdev);
  4486. pci_set_drvdata(pdev, NULL);
  4487. return -ENODEV;
  4488. }
  4489. static void __devexit cas_remove_one(struct pci_dev *pdev)
  4490. {
  4491. struct net_device *dev = pci_get_drvdata(pdev);
  4492. struct cas *cp;
  4493. if (!dev)
  4494. return;
  4495. cp = netdev_priv(dev);
  4496. unregister_netdev(dev);
  4497. if (cp->fw_data)
  4498. vfree(cp->fw_data);
  4499. mutex_lock(&cp->pm_mutex);
  4500. cancel_work_sync(&cp->reset_task);
  4501. if (cp->hw_running)
  4502. cas_shutdown(cp);
  4503. mutex_unlock(&cp->pm_mutex);
  4504. #if 1
  4505. if (cp->orig_cacheline_size) {
  4506. /* Restore the cache line size if we had modified
  4507. * it.
  4508. */
  4509. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
  4510. cp->orig_cacheline_size);
  4511. }
  4512. #endif
  4513. pci_free_consistent(pdev, sizeof(struct cas_init_block),
  4514. cp->init_block, cp->block_dvma);
  4515. pci_iounmap(pdev, cp->regs);
  4516. free_netdev(dev);
  4517. pci_release_regions(pdev);
  4518. pci_disable_device(pdev);
  4519. pci_set_drvdata(pdev, NULL);
  4520. }
  4521. #ifdef CONFIG_PM
  4522. static int cas_suspend(struct pci_dev *pdev, pm_message_t state)
  4523. {
  4524. struct net_device *dev = pci_get_drvdata(pdev);
  4525. struct cas *cp = netdev_priv(dev);
  4526. unsigned long flags;
  4527. mutex_lock(&cp->pm_mutex);
  4528. /* If the driver is opened, we stop the DMA */
  4529. if (cp->opened) {
  4530. netif_device_detach(dev);
  4531. cas_lock_all_save(cp, flags);
  4532. /* We can set the second arg of cas_reset to 0
  4533. * because on resume, we'll call cas_init_hw with
  4534. * its second arg set so that autonegotiation is
  4535. * restarted.
  4536. */
  4537. cas_reset(cp, 0);
  4538. cas_clean_rings(cp);
  4539. cas_unlock_all_restore(cp, flags);
  4540. }
  4541. if (cp->hw_running)
  4542. cas_shutdown(cp);
  4543. mutex_unlock(&cp->pm_mutex);
  4544. return 0;
  4545. }
  4546. static int cas_resume(struct pci_dev *pdev)
  4547. {
  4548. struct net_device *dev = pci_get_drvdata(pdev);
  4549. struct cas *cp = netdev_priv(dev);
  4550. netdev_info(dev, "resuming\n");
  4551. mutex_lock(&cp->pm_mutex);
  4552. cas_hard_reset(cp);
  4553. if (cp->opened) {
  4554. unsigned long flags;
  4555. cas_lock_all_save(cp, flags);
  4556. cas_reset(cp, 0);
  4557. cp->hw_running = 1;
  4558. cas_clean_rings(cp);
  4559. cas_init_hw(cp, 1);
  4560. cas_unlock_all_restore(cp, flags);
  4561. netif_device_attach(dev);
  4562. }
  4563. mutex_unlock(&cp->pm_mutex);
  4564. return 0;
  4565. }
  4566. #endif /* CONFIG_PM */
  4567. static struct pci_driver cas_driver = {
  4568. .name = DRV_MODULE_NAME,
  4569. .id_table = cas_pci_tbl,
  4570. .probe = cas_init_one,
  4571. .remove = __devexit_p(cas_remove_one),
  4572. #ifdef CONFIG_PM
  4573. .suspend = cas_suspend,
  4574. .resume = cas_resume
  4575. #endif
  4576. };
  4577. static int __init cas_init(void)
  4578. {
  4579. if (linkdown_timeout > 0)
  4580. link_transition_timeout = linkdown_timeout * HZ;
  4581. else
  4582. link_transition_timeout = 0;
  4583. return pci_register_driver(&cas_driver);
  4584. }
  4585. static void __exit cas_cleanup(void)
  4586. {
  4587. pci_unregister_driver(&cas_driver);
  4588. }
  4589. module_init(cas_init);
  4590. module_exit(cas_cleanup);