cassini.c 139 KB

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