cassini.c 138 KB

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