cassini.c 140 KB

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