cassini.c 139 KB

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