cassini.c 141 KB

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