cassini.c 139 KB

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