skge.c 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227
  1. /*
  2. * New driver for Marvell Yukon chipset and SysKonnect Gigabit
  3. * Ethernet adapters. Based on earlier sk98lin, e100 and
  4. * FreeBSD if_sk drivers.
  5. *
  6. * This driver intentionally does not support all the features
  7. * of the original driver such as link fail-over and link management because
  8. * those should be done at higher levels.
  9. *
  10. * Copyright (C) 2004, 2005 Stephen Hemminger <shemminger@osdl.org>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  26. #include <linux/in.h>
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/etherdevice.h>
  32. #include <linux/ethtool.h>
  33. #include <linux/pci.h>
  34. #include <linux/if_vlan.h>
  35. #include <linux/ip.h>
  36. #include <linux/delay.h>
  37. #include <linux/crc32.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/debugfs.h>
  40. #include <linux/sched.h>
  41. #include <linux/seq_file.h>
  42. #include <linux/mii.h>
  43. #include <linux/slab.h>
  44. #include <linux/dmi.h>
  45. #include <linux/prefetch.h>
  46. #include <asm/irq.h>
  47. #include "skge.h"
  48. #define DRV_NAME "skge"
  49. #define DRV_VERSION "1.14"
  50. #define DEFAULT_TX_RING_SIZE 128
  51. #define DEFAULT_RX_RING_SIZE 512
  52. #define MAX_TX_RING_SIZE 1024
  53. #define TX_LOW_WATER (MAX_SKB_FRAGS + 1)
  54. #define MAX_RX_RING_SIZE 4096
  55. #define RX_COPY_THRESHOLD 128
  56. #define RX_BUF_SIZE 1536
  57. #define PHY_RETRIES 1000
  58. #define ETH_JUMBO_MTU 9000
  59. #define TX_WATCHDOG (5 * HZ)
  60. #define NAPI_WEIGHT 64
  61. #define BLINK_MS 250
  62. #define LINK_HZ HZ
  63. #define SKGE_EEPROM_MAGIC 0x9933aabb
  64. MODULE_DESCRIPTION("SysKonnect Gigabit Ethernet driver");
  65. MODULE_AUTHOR("Stephen Hemminger <shemminger@linux-foundation.org>");
  66. MODULE_LICENSE("GPL");
  67. MODULE_VERSION(DRV_VERSION);
  68. static const u32 default_msg = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
  69. NETIF_MSG_LINK | NETIF_MSG_IFUP |
  70. NETIF_MSG_IFDOWN);
  71. static int debug = -1; /* defaults above */
  72. module_param(debug, int, 0);
  73. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
  74. static const struct pci_device_id skge_id_table[] = {
  75. { PCI_DEVICE(PCI_VENDOR_ID_3COM, 0x1700) }, /* 3Com 3C940 */
  76. { PCI_DEVICE(PCI_VENDOR_ID_3COM, 0x80EB) }, /* 3Com 3C940B */
  77. #ifdef CONFIG_SKGE_GENESIS
  78. { PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, 0x4300) }, /* SK-9xx */
  79. #endif
  80. { PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, 0x4320) }, /* SK-98xx V2.0 */
  81. { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4b01) }, /* D-Link DGE-530T (rev.B) */
  82. { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4c00) }, /* D-Link DGE-530T */
  83. { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302) }, /* D-Link DGE-530T Rev C1 */
  84. { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4320) }, /* Marvell Yukon 88E8001/8003/8010 */
  85. { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5005) }, /* Belkin */
  86. { PCI_DEVICE(PCI_VENDOR_ID_CNET, 0x434E) }, /* CNet PowerG-2000 */
  87. { PCI_DEVICE(PCI_VENDOR_ID_LINKSYS, 0x1064) }, /* Linksys EG1064 v2 */
  88. { PCI_VENDOR_ID_LINKSYS, 0x1032, PCI_ANY_ID, 0x0015 }, /* Linksys EG1032 v2 */
  89. { 0 }
  90. };
  91. MODULE_DEVICE_TABLE(pci, skge_id_table);
  92. static int skge_up(struct net_device *dev);
  93. static int skge_down(struct net_device *dev);
  94. static void skge_phy_reset(struct skge_port *skge);
  95. static void skge_tx_clean(struct net_device *dev);
  96. static int xm_phy_write(struct skge_hw *hw, int port, u16 reg, u16 val);
  97. static int gm_phy_write(struct skge_hw *hw, int port, u16 reg, u16 val);
  98. static void genesis_get_stats(struct skge_port *skge, u64 *data);
  99. static void yukon_get_stats(struct skge_port *skge, u64 *data);
  100. static void yukon_init(struct skge_hw *hw, int port);
  101. static void genesis_mac_init(struct skge_hw *hw, int port);
  102. static void genesis_link_up(struct skge_port *skge);
  103. static void skge_set_multicast(struct net_device *dev);
  104. static irqreturn_t skge_intr(int irq, void *dev_id);
  105. /* Avoid conditionals by using array */
  106. static const int txqaddr[] = { Q_XA1, Q_XA2 };
  107. static const int rxqaddr[] = { Q_R1, Q_R2 };
  108. static const u32 rxirqmask[] = { IS_R1_F, IS_R2_F };
  109. static const u32 txirqmask[] = { IS_XA1_F, IS_XA2_F };
  110. static const u32 napimask[] = { IS_R1_F|IS_XA1_F, IS_R2_F|IS_XA2_F };
  111. static const u32 portmask[] = { IS_PORT_1, IS_PORT_2 };
  112. static inline bool is_genesis(const struct skge_hw *hw)
  113. {
  114. #ifdef CONFIG_SKGE_GENESIS
  115. return hw->chip_id == CHIP_ID_GENESIS;
  116. #else
  117. return false;
  118. #endif
  119. }
  120. static int skge_get_regs_len(struct net_device *dev)
  121. {
  122. return 0x4000;
  123. }
  124. /*
  125. * Returns copy of whole control register region
  126. * Note: skip RAM address register because accessing it will
  127. * cause bus hangs!
  128. */
  129. static void skge_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  130. void *p)
  131. {
  132. const struct skge_port *skge = netdev_priv(dev);
  133. const void __iomem *io = skge->hw->regs;
  134. regs->version = 1;
  135. memset(p, 0, regs->len);
  136. memcpy_fromio(p, io, B3_RAM_ADDR);
  137. memcpy_fromio(p + B3_RI_WTO_R1, io + B3_RI_WTO_R1,
  138. regs->len - B3_RI_WTO_R1);
  139. }
  140. /* Wake on Lan only supported on Yukon chips with rev 1 or above */
  141. static u32 wol_supported(const struct skge_hw *hw)
  142. {
  143. if (is_genesis(hw))
  144. return 0;
  145. if (hw->chip_id == CHIP_ID_YUKON && hw->chip_rev == 0)
  146. return 0;
  147. return WAKE_MAGIC | WAKE_PHY;
  148. }
  149. static void skge_wol_init(struct skge_port *skge)
  150. {
  151. struct skge_hw *hw = skge->hw;
  152. int port = skge->port;
  153. u16 ctrl;
  154. skge_write16(hw, B0_CTST, CS_RST_CLR);
  155. skge_write16(hw, SK_REG(port, GMAC_LINK_CTRL), GMLC_RST_CLR);
  156. /* Turn on Vaux */
  157. skge_write8(hw, B0_POWER_CTRL,
  158. PC_VAUX_ENA | PC_VCC_ENA | PC_VAUX_ON | PC_VCC_OFF);
  159. /* WA code for COMA mode -- clear PHY reset */
  160. if (hw->chip_id == CHIP_ID_YUKON_LITE &&
  161. hw->chip_rev >= CHIP_REV_YU_LITE_A3) {
  162. u32 reg = skge_read32(hw, B2_GP_IO);
  163. reg |= GP_DIR_9;
  164. reg &= ~GP_IO_9;
  165. skge_write32(hw, B2_GP_IO, reg);
  166. }
  167. skge_write32(hw, SK_REG(port, GPHY_CTRL),
  168. GPC_DIS_SLEEP |
  169. GPC_HWCFG_M_3 | GPC_HWCFG_M_2 | GPC_HWCFG_M_1 | GPC_HWCFG_M_0 |
  170. GPC_ANEG_1 | GPC_RST_SET);
  171. skge_write32(hw, SK_REG(port, GPHY_CTRL),
  172. GPC_DIS_SLEEP |
  173. GPC_HWCFG_M_3 | GPC_HWCFG_M_2 | GPC_HWCFG_M_1 | GPC_HWCFG_M_0 |
  174. GPC_ANEG_1 | GPC_RST_CLR);
  175. skge_write32(hw, SK_REG(port, GMAC_CTRL), GMC_RST_CLR);
  176. /* Force to 10/100 skge_reset will re-enable on resume */
  177. gm_phy_write(hw, port, PHY_MARV_AUNE_ADV,
  178. (PHY_AN_100FULL | PHY_AN_100HALF |
  179. PHY_AN_10FULL | PHY_AN_10HALF | PHY_AN_CSMA));
  180. /* no 1000 HD/FD */
  181. gm_phy_write(hw, port, PHY_MARV_1000T_CTRL, 0);
  182. gm_phy_write(hw, port, PHY_MARV_CTRL,
  183. PHY_CT_RESET | PHY_CT_SPS_LSB | PHY_CT_ANE |
  184. PHY_CT_RE_CFG | PHY_CT_DUP_MD);
  185. /* Set GMAC to no flow control and auto update for speed/duplex */
  186. gma_write16(hw, port, GM_GP_CTRL,
  187. GM_GPCR_FC_TX_DIS|GM_GPCR_TX_ENA|GM_GPCR_RX_ENA|
  188. GM_GPCR_DUP_FULL|GM_GPCR_FC_RX_DIS|GM_GPCR_AU_FCT_DIS);
  189. /* Set WOL address */
  190. memcpy_toio(hw->regs + WOL_REGS(port, WOL_MAC_ADDR),
  191. skge->netdev->dev_addr, ETH_ALEN);
  192. /* Turn on appropriate WOL control bits */
  193. skge_write16(hw, WOL_REGS(port, WOL_CTRL_STAT), WOL_CTL_CLEAR_RESULT);
  194. ctrl = 0;
  195. if (skge->wol & WAKE_PHY)
  196. ctrl |= WOL_CTL_ENA_PME_ON_LINK_CHG|WOL_CTL_ENA_LINK_CHG_UNIT;
  197. else
  198. ctrl |= WOL_CTL_DIS_PME_ON_LINK_CHG|WOL_CTL_DIS_LINK_CHG_UNIT;
  199. if (skge->wol & WAKE_MAGIC)
  200. ctrl |= WOL_CTL_ENA_PME_ON_MAGIC_PKT|WOL_CTL_ENA_MAGIC_PKT_UNIT;
  201. else
  202. ctrl |= WOL_CTL_DIS_PME_ON_MAGIC_PKT|WOL_CTL_DIS_MAGIC_PKT_UNIT;
  203. ctrl |= WOL_CTL_DIS_PME_ON_PATTERN|WOL_CTL_DIS_PATTERN_UNIT;
  204. skge_write16(hw, WOL_REGS(port, WOL_CTRL_STAT), ctrl);
  205. /* block receiver */
  206. skge_write8(hw, SK_REG(port, RX_GMF_CTRL_T), GMF_RST_SET);
  207. }
  208. static void skge_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  209. {
  210. struct skge_port *skge = netdev_priv(dev);
  211. wol->supported = wol_supported(skge->hw);
  212. wol->wolopts = skge->wol;
  213. }
  214. static int skge_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  215. {
  216. struct skge_port *skge = netdev_priv(dev);
  217. struct skge_hw *hw = skge->hw;
  218. if ((wol->wolopts & ~wol_supported(hw)) ||
  219. !device_can_wakeup(&hw->pdev->dev))
  220. return -EOPNOTSUPP;
  221. skge->wol = wol->wolopts;
  222. device_set_wakeup_enable(&hw->pdev->dev, skge->wol);
  223. return 0;
  224. }
  225. /* Determine supported/advertised modes based on hardware.
  226. * Note: ethtool ADVERTISED_xxx == SUPPORTED_xxx
  227. */
  228. static u32 skge_supported_modes(const struct skge_hw *hw)
  229. {
  230. u32 supported;
  231. if (hw->copper) {
  232. supported = (SUPPORTED_10baseT_Half |
  233. SUPPORTED_10baseT_Full |
  234. SUPPORTED_100baseT_Half |
  235. SUPPORTED_100baseT_Full |
  236. SUPPORTED_1000baseT_Half |
  237. SUPPORTED_1000baseT_Full |
  238. SUPPORTED_Autoneg |
  239. SUPPORTED_TP);
  240. if (is_genesis(hw))
  241. supported &= ~(SUPPORTED_10baseT_Half |
  242. SUPPORTED_10baseT_Full |
  243. SUPPORTED_100baseT_Half |
  244. SUPPORTED_100baseT_Full);
  245. else if (hw->chip_id == CHIP_ID_YUKON)
  246. supported &= ~SUPPORTED_1000baseT_Half;
  247. } else
  248. supported = (SUPPORTED_1000baseT_Full |
  249. SUPPORTED_1000baseT_Half |
  250. SUPPORTED_FIBRE |
  251. SUPPORTED_Autoneg);
  252. return supported;
  253. }
  254. static int skge_get_settings(struct net_device *dev,
  255. struct ethtool_cmd *ecmd)
  256. {
  257. struct skge_port *skge = netdev_priv(dev);
  258. struct skge_hw *hw = skge->hw;
  259. ecmd->transceiver = XCVR_INTERNAL;
  260. ecmd->supported = skge_supported_modes(hw);
  261. if (hw->copper) {
  262. ecmd->port = PORT_TP;
  263. ecmd->phy_address = hw->phy_addr;
  264. } else
  265. ecmd->port = PORT_FIBRE;
  266. ecmd->advertising = skge->advertising;
  267. ecmd->autoneg = skge->autoneg;
  268. ethtool_cmd_speed_set(ecmd, skge->speed);
  269. ecmd->duplex = skge->duplex;
  270. return 0;
  271. }
  272. static int skge_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
  273. {
  274. struct skge_port *skge = netdev_priv(dev);
  275. const struct skge_hw *hw = skge->hw;
  276. u32 supported = skge_supported_modes(hw);
  277. int err = 0;
  278. if (ecmd->autoneg == AUTONEG_ENABLE) {
  279. ecmd->advertising = supported;
  280. skge->duplex = -1;
  281. skge->speed = -1;
  282. } else {
  283. u32 setting;
  284. u32 speed = ethtool_cmd_speed(ecmd);
  285. switch (speed) {
  286. case SPEED_1000:
  287. if (ecmd->duplex == DUPLEX_FULL)
  288. setting = SUPPORTED_1000baseT_Full;
  289. else if (ecmd->duplex == DUPLEX_HALF)
  290. setting = SUPPORTED_1000baseT_Half;
  291. else
  292. return -EINVAL;
  293. break;
  294. case SPEED_100:
  295. if (ecmd->duplex == DUPLEX_FULL)
  296. setting = SUPPORTED_100baseT_Full;
  297. else if (ecmd->duplex == DUPLEX_HALF)
  298. setting = SUPPORTED_100baseT_Half;
  299. else
  300. return -EINVAL;
  301. break;
  302. case SPEED_10:
  303. if (ecmd->duplex == DUPLEX_FULL)
  304. setting = SUPPORTED_10baseT_Full;
  305. else if (ecmd->duplex == DUPLEX_HALF)
  306. setting = SUPPORTED_10baseT_Half;
  307. else
  308. return -EINVAL;
  309. break;
  310. default:
  311. return -EINVAL;
  312. }
  313. if ((setting & supported) == 0)
  314. return -EINVAL;
  315. skge->speed = speed;
  316. skge->duplex = ecmd->duplex;
  317. }
  318. skge->autoneg = ecmd->autoneg;
  319. skge->advertising = ecmd->advertising;
  320. if (netif_running(dev)) {
  321. skge_down(dev);
  322. err = skge_up(dev);
  323. if (err) {
  324. dev_close(dev);
  325. return err;
  326. }
  327. }
  328. return 0;
  329. }
  330. static void skge_get_drvinfo(struct net_device *dev,
  331. struct ethtool_drvinfo *info)
  332. {
  333. struct skge_port *skge = netdev_priv(dev);
  334. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  335. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  336. strlcpy(info->bus_info, pci_name(skge->hw->pdev),
  337. sizeof(info->bus_info));
  338. }
  339. static const struct skge_stat {
  340. char name[ETH_GSTRING_LEN];
  341. u16 xmac_offset;
  342. u16 gma_offset;
  343. } skge_stats[] = {
  344. { "tx_bytes", XM_TXO_OK_HI, GM_TXO_OK_HI },
  345. { "rx_bytes", XM_RXO_OK_HI, GM_RXO_OK_HI },
  346. { "tx_broadcast", XM_TXF_BC_OK, GM_TXF_BC_OK },
  347. { "rx_broadcast", XM_RXF_BC_OK, GM_RXF_BC_OK },
  348. { "tx_multicast", XM_TXF_MC_OK, GM_TXF_MC_OK },
  349. { "rx_multicast", XM_RXF_MC_OK, GM_RXF_MC_OK },
  350. { "tx_unicast", XM_TXF_UC_OK, GM_TXF_UC_OK },
  351. { "rx_unicast", XM_RXF_UC_OK, GM_RXF_UC_OK },
  352. { "tx_mac_pause", XM_TXF_MPAUSE, GM_TXF_MPAUSE },
  353. { "rx_mac_pause", XM_RXF_MPAUSE, GM_RXF_MPAUSE },
  354. { "collisions", XM_TXF_SNG_COL, GM_TXF_SNG_COL },
  355. { "multi_collisions", XM_TXF_MUL_COL, GM_TXF_MUL_COL },
  356. { "aborted", XM_TXF_ABO_COL, GM_TXF_ABO_COL },
  357. { "late_collision", XM_TXF_LAT_COL, GM_TXF_LAT_COL },
  358. { "fifo_underrun", XM_TXE_FIFO_UR, GM_TXE_FIFO_UR },
  359. { "fifo_overflow", XM_RXE_FIFO_OV, GM_RXE_FIFO_OV },
  360. { "rx_toolong", XM_RXF_LNG_ERR, GM_RXF_LNG_ERR },
  361. { "rx_jabber", XM_RXF_JAB_PKT, GM_RXF_JAB_PKT },
  362. { "rx_runt", XM_RXE_RUNT, GM_RXE_FRAG },
  363. { "rx_too_long", XM_RXF_LNG_ERR, GM_RXF_LNG_ERR },
  364. { "rx_fcs_error", XM_RXF_FCS_ERR, GM_RXF_FCS_ERR },
  365. };
  366. static int skge_get_sset_count(struct net_device *dev, int sset)
  367. {
  368. switch (sset) {
  369. case ETH_SS_STATS:
  370. return ARRAY_SIZE(skge_stats);
  371. default:
  372. return -EOPNOTSUPP;
  373. }
  374. }
  375. static void skge_get_ethtool_stats(struct net_device *dev,
  376. struct ethtool_stats *stats, u64 *data)
  377. {
  378. struct skge_port *skge = netdev_priv(dev);
  379. if (is_genesis(skge->hw))
  380. genesis_get_stats(skge, data);
  381. else
  382. yukon_get_stats(skge, data);
  383. }
  384. /* Use hardware MIB variables for critical path statistics and
  385. * transmit feedback not reported at interrupt.
  386. * Other errors are accounted for in interrupt handler.
  387. */
  388. static struct net_device_stats *skge_get_stats(struct net_device *dev)
  389. {
  390. struct skge_port *skge = netdev_priv(dev);
  391. u64 data[ARRAY_SIZE(skge_stats)];
  392. if (is_genesis(skge->hw))
  393. genesis_get_stats(skge, data);
  394. else
  395. yukon_get_stats(skge, data);
  396. dev->stats.tx_bytes = data[0];
  397. dev->stats.rx_bytes = data[1];
  398. dev->stats.tx_packets = data[2] + data[4] + data[6];
  399. dev->stats.rx_packets = data[3] + data[5] + data[7];
  400. dev->stats.multicast = data[3] + data[5];
  401. dev->stats.collisions = data[10];
  402. dev->stats.tx_aborted_errors = data[12];
  403. return &dev->stats;
  404. }
  405. static void skge_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  406. {
  407. int i;
  408. switch (stringset) {
  409. case ETH_SS_STATS:
  410. for (i = 0; i < ARRAY_SIZE(skge_stats); i++)
  411. memcpy(data + i * ETH_GSTRING_LEN,
  412. skge_stats[i].name, ETH_GSTRING_LEN);
  413. break;
  414. }
  415. }
  416. static void skge_get_ring_param(struct net_device *dev,
  417. struct ethtool_ringparam *p)
  418. {
  419. struct skge_port *skge = netdev_priv(dev);
  420. p->rx_max_pending = MAX_RX_RING_SIZE;
  421. p->tx_max_pending = MAX_TX_RING_SIZE;
  422. p->rx_pending = skge->rx_ring.count;
  423. p->tx_pending = skge->tx_ring.count;
  424. }
  425. static int skge_set_ring_param(struct net_device *dev,
  426. struct ethtool_ringparam *p)
  427. {
  428. struct skge_port *skge = netdev_priv(dev);
  429. int err = 0;
  430. if (p->rx_pending == 0 || p->rx_pending > MAX_RX_RING_SIZE ||
  431. p->tx_pending < TX_LOW_WATER || p->tx_pending > MAX_TX_RING_SIZE)
  432. return -EINVAL;
  433. skge->rx_ring.count = p->rx_pending;
  434. skge->tx_ring.count = p->tx_pending;
  435. if (netif_running(dev)) {
  436. skge_down(dev);
  437. err = skge_up(dev);
  438. if (err)
  439. dev_close(dev);
  440. }
  441. return err;
  442. }
  443. static u32 skge_get_msglevel(struct net_device *netdev)
  444. {
  445. struct skge_port *skge = netdev_priv(netdev);
  446. return skge->msg_enable;
  447. }
  448. static void skge_set_msglevel(struct net_device *netdev, u32 value)
  449. {
  450. struct skge_port *skge = netdev_priv(netdev);
  451. skge->msg_enable = value;
  452. }
  453. static int skge_nway_reset(struct net_device *dev)
  454. {
  455. struct skge_port *skge = netdev_priv(dev);
  456. if (skge->autoneg != AUTONEG_ENABLE || !netif_running(dev))
  457. return -EINVAL;
  458. skge_phy_reset(skge);
  459. return 0;
  460. }
  461. static void skge_get_pauseparam(struct net_device *dev,
  462. struct ethtool_pauseparam *ecmd)
  463. {
  464. struct skge_port *skge = netdev_priv(dev);
  465. ecmd->rx_pause = ((skge->flow_control == FLOW_MODE_SYMMETRIC) ||
  466. (skge->flow_control == FLOW_MODE_SYM_OR_REM));
  467. ecmd->tx_pause = (ecmd->rx_pause ||
  468. (skge->flow_control == FLOW_MODE_LOC_SEND));
  469. ecmd->autoneg = ecmd->rx_pause || ecmd->tx_pause;
  470. }
  471. static int skge_set_pauseparam(struct net_device *dev,
  472. struct ethtool_pauseparam *ecmd)
  473. {
  474. struct skge_port *skge = netdev_priv(dev);
  475. struct ethtool_pauseparam old;
  476. int err = 0;
  477. skge_get_pauseparam(dev, &old);
  478. if (ecmd->autoneg != old.autoneg)
  479. skge->flow_control = ecmd->autoneg ? FLOW_MODE_NONE : FLOW_MODE_SYMMETRIC;
  480. else {
  481. if (ecmd->rx_pause && ecmd->tx_pause)
  482. skge->flow_control = FLOW_MODE_SYMMETRIC;
  483. else if (ecmd->rx_pause && !ecmd->tx_pause)
  484. skge->flow_control = FLOW_MODE_SYM_OR_REM;
  485. else if (!ecmd->rx_pause && ecmd->tx_pause)
  486. skge->flow_control = FLOW_MODE_LOC_SEND;
  487. else
  488. skge->flow_control = FLOW_MODE_NONE;
  489. }
  490. if (netif_running(dev)) {
  491. skge_down(dev);
  492. err = skge_up(dev);
  493. if (err) {
  494. dev_close(dev);
  495. return err;
  496. }
  497. }
  498. return 0;
  499. }
  500. /* Chip internal frequency for clock calculations */
  501. static inline u32 hwkhz(const struct skge_hw *hw)
  502. {
  503. return is_genesis(hw) ? 53125 : 78125;
  504. }
  505. /* Chip HZ to microseconds */
  506. static inline u32 skge_clk2usec(const struct skge_hw *hw, u32 ticks)
  507. {
  508. return (ticks * 1000) / hwkhz(hw);
  509. }
  510. /* Microseconds to chip HZ */
  511. static inline u32 skge_usecs2clk(const struct skge_hw *hw, u32 usec)
  512. {
  513. return hwkhz(hw) * usec / 1000;
  514. }
  515. static int skge_get_coalesce(struct net_device *dev,
  516. struct ethtool_coalesce *ecmd)
  517. {
  518. struct skge_port *skge = netdev_priv(dev);
  519. struct skge_hw *hw = skge->hw;
  520. int port = skge->port;
  521. ecmd->rx_coalesce_usecs = 0;
  522. ecmd->tx_coalesce_usecs = 0;
  523. if (skge_read32(hw, B2_IRQM_CTRL) & TIM_START) {
  524. u32 delay = skge_clk2usec(hw, skge_read32(hw, B2_IRQM_INI));
  525. u32 msk = skge_read32(hw, B2_IRQM_MSK);
  526. if (msk & rxirqmask[port])
  527. ecmd->rx_coalesce_usecs = delay;
  528. if (msk & txirqmask[port])
  529. ecmd->tx_coalesce_usecs = delay;
  530. }
  531. return 0;
  532. }
  533. /* Note: interrupt timer is per board, but can turn on/off per port */
  534. static int skge_set_coalesce(struct net_device *dev,
  535. struct ethtool_coalesce *ecmd)
  536. {
  537. struct skge_port *skge = netdev_priv(dev);
  538. struct skge_hw *hw = skge->hw;
  539. int port = skge->port;
  540. u32 msk = skge_read32(hw, B2_IRQM_MSK);
  541. u32 delay = 25;
  542. if (ecmd->rx_coalesce_usecs == 0)
  543. msk &= ~rxirqmask[port];
  544. else if (ecmd->rx_coalesce_usecs < 25 ||
  545. ecmd->rx_coalesce_usecs > 33333)
  546. return -EINVAL;
  547. else {
  548. msk |= rxirqmask[port];
  549. delay = ecmd->rx_coalesce_usecs;
  550. }
  551. if (ecmd->tx_coalesce_usecs == 0)
  552. msk &= ~txirqmask[port];
  553. else if (ecmd->tx_coalesce_usecs < 25 ||
  554. ecmd->tx_coalesce_usecs > 33333)
  555. return -EINVAL;
  556. else {
  557. msk |= txirqmask[port];
  558. delay = min(delay, ecmd->rx_coalesce_usecs);
  559. }
  560. skge_write32(hw, B2_IRQM_MSK, msk);
  561. if (msk == 0)
  562. skge_write32(hw, B2_IRQM_CTRL, TIM_STOP);
  563. else {
  564. skge_write32(hw, B2_IRQM_INI, skge_usecs2clk(hw, delay));
  565. skge_write32(hw, B2_IRQM_CTRL, TIM_START);
  566. }
  567. return 0;
  568. }
  569. enum led_mode { LED_MODE_OFF, LED_MODE_ON, LED_MODE_TST };
  570. static void skge_led(struct skge_port *skge, enum led_mode mode)
  571. {
  572. struct skge_hw *hw = skge->hw;
  573. int port = skge->port;
  574. spin_lock_bh(&hw->phy_lock);
  575. if (is_genesis(hw)) {
  576. switch (mode) {
  577. case LED_MODE_OFF:
  578. if (hw->phy_type == SK_PHY_BCOM)
  579. xm_phy_write(hw, port, PHY_BCOM_P_EXT_CTRL, PHY_B_PEC_LED_OFF);
  580. else {
  581. skge_write32(hw, SK_REG(port, TX_LED_VAL), 0);
  582. skge_write8(hw, SK_REG(port, TX_LED_CTRL), LED_T_OFF);
  583. }
  584. skge_write8(hw, SK_REG(port, LNK_LED_REG), LINKLED_OFF);
  585. skge_write32(hw, SK_REG(port, RX_LED_VAL), 0);
  586. skge_write8(hw, SK_REG(port, RX_LED_CTRL), LED_T_OFF);
  587. break;
  588. case LED_MODE_ON:
  589. skge_write8(hw, SK_REG(port, LNK_LED_REG), LINKLED_ON);
  590. skge_write8(hw, SK_REG(port, LNK_LED_REG), LINKLED_LINKSYNC_ON);
  591. skge_write8(hw, SK_REG(port, RX_LED_CTRL), LED_START);
  592. skge_write8(hw, SK_REG(port, TX_LED_CTRL), LED_START);
  593. break;
  594. case LED_MODE_TST:
  595. skge_write8(hw, SK_REG(port, RX_LED_TST), LED_T_ON);
  596. skge_write32(hw, SK_REG(port, RX_LED_VAL), 100);
  597. skge_write8(hw, SK_REG(port, RX_LED_CTRL), LED_START);
  598. if (hw->phy_type == SK_PHY_BCOM)
  599. xm_phy_write(hw, port, PHY_BCOM_P_EXT_CTRL, PHY_B_PEC_LED_ON);
  600. else {
  601. skge_write8(hw, SK_REG(port, TX_LED_TST), LED_T_ON);
  602. skge_write32(hw, SK_REG(port, TX_LED_VAL), 100);
  603. skge_write8(hw, SK_REG(port, TX_LED_CTRL), LED_START);
  604. }
  605. }
  606. } else {
  607. switch (mode) {
  608. case LED_MODE_OFF:
  609. gm_phy_write(hw, port, PHY_MARV_LED_CTRL, 0);
  610. gm_phy_write(hw, port, PHY_MARV_LED_OVER,
  611. PHY_M_LED_MO_DUP(MO_LED_OFF) |
  612. PHY_M_LED_MO_10(MO_LED_OFF) |
  613. PHY_M_LED_MO_100(MO_LED_OFF) |
  614. PHY_M_LED_MO_1000(MO_LED_OFF) |
  615. PHY_M_LED_MO_RX(MO_LED_OFF));
  616. break;
  617. case LED_MODE_ON:
  618. gm_phy_write(hw, port, PHY_MARV_LED_CTRL,
  619. PHY_M_LED_PULS_DUR(PULS_170MS) |
  620. PHY_M_LED_BLINK_RT(BLINK_84MS) |
  621. PHY_M_LEDC_TX_CTRL |
  622. PHY_M_LEDC_DP_CTRL);
  623. gm_phy_write(hw, port, PHY_MARV_LED_OVER,
  624. PHY_M_LED_MO_RX(MO_LED_OFF) |
  625. (skge->speed == SPEED_100 ?
  626. PHY_M_LED_MO_100(MO_LED_ON) : 0));
  627. break;
  628. case LED_MODE_TST:
  629. gm_phy_write(hw, port, PHY_MARV_LED_CTRL, 0);
  630. gm_phy_write(hw, port, PHY_MARV_LED_OVER,
  631. PHY_M_LED_MO_DUP(MO_LED_ON) |
  632. PHY_M_LED_MO_10(MO_LED_ON) |
  633. PHY_M_LED_MO_100(MO_LED_ON) |
  634. PHY_M_LED_MO_1000(MO_LED_ON) |
  635. PHY_M_LED_MO_RX(MO_LED_ON));
  636. }
  637. }
  638. spin_unlock_bh(&hw->phy_lock);
  639. }
  640. /* blink LED's for finding board */
  641. static int skge_set_phys_id(struct net_device *dev,
  642. enum ethtool_phys_id_state state)
  643. {
  644. struct skge_port *skge = netdev_priv(dev);
  645. switch (state) {
  646. case ETHTOOL_ID_ACTIVE:
  647. return 2; /* cycle on/off twice per second */
  648. case ETHTOOL_ID_ON:
  649. skge_led(skge, LED_MODE_TST);
  650. break;
  651. case ETHTOOL_ID_OFF:
  652. skge_led(skge, LED_MODE_OFF);
  653. break;
  654. case ETHTOOL_ID_INACTIVE:
  655. /* back to regular LED state */
  656. skge_led(skge, netif_running(dev) ? LED_MODE_ON : LED_MODE_OFF);
  657. }
  658. return 0;
  659. }
  660. static int skge_get_eeprom_len(struct net_device *dev)
  661. {
  662. struct skge_port *skge = netdev_priv(dev);
  663. u32 reg2;
  664. pci_read_config_dword(skge->hw->pdev, PCI_DEV_REG2, &reg2);
  665. return 1 << (((reg2 & PCI_VPD_ROM_SZ) >> 14) + 8);
  666. }
  667. static u32 skge_vpd_read(struct pci_dev *pdev, int cap, u16 offset)
  668. {
  669. u32 val;
  670. pci_write_config_word(pdev, cap + PCI_VPD_ADDR, offset);
  671. do {
  672. pci_read_config_word(pdev, cap + PCI_VPD_ADDR, &offset);
  673. } while (!(offset & PCI_VPD_ADDR_F));
  674. pci_read_config_dword(pdev, cap + PCI_VPD_DATA, &val);
  675. return val;
  676. }
  677. static void skge_vpd_write(struct pci_dev *pdev, int cap, u16 offset, u32 val)
  678. {
  679. pci_write_config_dword(pdev, cap + PCI_VPD_DATA, val);
  680. pci_write_config_word(pdev, cap + PCI_VPD_ADDR,
  681. offset | PCI_VPD_ADDR_F);
  682. do {
  683. pci_read_config_word(pdev, cap + PCI_VPD_ADDR, &offset);
  684. } while (offset & PCI_VPD_ADDR_F);
  685. }
  686. static int skge_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom,
  687. u8 *data)
  688. {
  689. struct skge_port *skge = netdev_priv(dev);
  690. struct pci_dev *pdev = skge->hw->pdev;
  691. int cap = pci_find_capability(pdev, PCI_CAP_ID_VPD);
  692. int length = eeprom->len;
  693. u16 offset = eeprom->offset;
  694. if (!cap)
  695. return -EINVAL;
  696. eeprom->magic = SKGE_EEPROM_MAGIC;
  697. while (length > 0) {
  698. u32 val = skge_vpd_read(pdev, cap, offset);
  699. int n = min_t(int, length, sizeof(val));
  700. memcpy(data, &val, n);
  701. length -= n;
  702. data += n;
  703. offset += n;
  704. }
  705. return 0;
  706. }
  707. static int skge_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom,
  708. u8 *data)
  709. {
  710. struct skge_port *skge = netdev_priv(dev);
  711. struct pci_dev *pdev = skge->hw->pdev;
  712. int cap = pci_find_capability(pdev, PCI_CAP_ID_VPD);
  713. int length = eeprom->len;
  714. u16 offset = eeprom->offset;
  715. if (!cap)
  716. return -EINVAL;
  717. if (eeprom->magic != SKGE_EEPROM_MAGIC)
  718. return -EINVAL;
  719. while (length > 0) {
  720. u32 val;
  721. int n = min_t(int, length, sizeof(val));
  722. if (n < sizeof(val))
  723. val = skge_vpd_read(pdev, cap, offset);
  724. memcpy(&val, data, n);
  725. skge_vpd_write(pdev, cap, offset, val);
  726. length -= n;
  727. data += n;
  728. offset += n;
  729. }
  730. return 0;
  731. }
  732. static const struct ethtool_ops skge_ethtool_ops = {
  733. .get_settings = skge_get_settings,
  734. .set_settings = skge_set_settings,
  735. .get_drvinfo = skge_get_drvinfo,
  736. .get_regs_len = skge_get_regs_len,
  737. .get_regs = skge_get_regs,
  738. .get_wol = skge_get_wol,
  739. .set_wol = skge_set_wol,
  740. .get_msglevel = skge_get_msglevel,
  741. .set_msglevel = skge_set_msglevel,
  742. .nway_reset = skge_nway_reset,
  743. .get_link = ethtool_op_get_link,
  744. .get_eeprom_len = skge_get_eeprom_len,
  745. .get_eeprom = skge_get_eeprom,
  746. .set_eeprom = skge_set_eeprom,
  747. .get_ringparam = skge_get_ring_param,
  748. .set_ringparam = skge_set_ring_param,
  749. .get_pauseparam = skge_get_pauseparam,
  750. .set_pauseparam = skge_set_pauseparam,
  751. .get_coalesce = skge_get_coalesce,
  752. .set_coalesce = skge_set_coalesce,
  753. .get_strings = skge_get_strings,
  754. .set_phys_id = skge_set_phys_id,
  755. .get_sset_count = skge_get_sset_count,
  756. .get_ethtool_stats = skge_get_ethtool_stats,
  757. };
  758. /*
  759. * Allocate ring elements and chain them together
  760. * One-to-one association of board descriptors with ring elements
  761. */
  762. static int skge_ring_alloc(struct skge_ring *ring, void *vaddr, u32 base)
  763. {
  764. struct skge_tx_desc *d;
  765. struct skge_element *e;
  766. int i;
  767. ring->start = kcalloc(ring->count, sizeof(*e), GFP_KERNEL);
  768. if (!ring->start)
  769. return -ENOMEM;
  770. for (i = 0, e = ring->start, d = vaddr; i < ring->count; i++, e++, d++) {
  771. e->desc = d;
  772. if (i == ring->count - 1) {
  773. e->next = ring->start;
  774. d->next_offset = base;
  775. } else {
  776. e->next = e + 1;
  777. d->next_offset = base + (i+1) * sizeof(*d);
  778. }
  779. }
  780. ring->to_use = ring->to_clean = ring->start;
  781. return 0;
  782. }
  783. /* Allocate and setup a new buffer for receiving */
  784. static int skge_rx_setup(struct skge_port *skge, struct skge_element *e,
  785. struct sk_buff *skb, unsigned int bufsize)
  786. {
  787. struct skge_rx_desc *rd = e->desc;
  788. dma_addr_t map;
  789. map = pci_map_single(skge->hw->pdev, skb->data, bufsize,
  790. PCI_DMA_FROMDEVICE);
  791. if (pci_dma_mapping_error(skge->hw->pdev, map))
  792. return -1;
  793. rd->dma_lo = lower_32_bits(map);
  794. rd->dma_hi = upper_32_bits(map);
  795. e->skb = skb;
  796. rd->csum1_start = ETH_HLEN;
  797. rd->csum2_start = ETH_HLEN;
  798. rd->csum1 = 0;
  799. rd->csum2 = 0;
  800. wmb();
  801. rd->control = BMU_OWN | BMU_STF | BMU_IRQ_EOF | BMU_TCP_CHECK | bufsize;
  802. dma_unmap_addr_set(e, mapaddr, map);
  803. dma_unmap_len_set(e, maplen, bufsize);
  804. return 0;
  805. }
  806. /* Resume receiving using existing skb,
  807. * Note: DMA address is not changed by chip.
  808. * MTU not changed while receiver active.
  809. */
  810. static inline void skge_rx_reuse(struct skge_element *e, unsigned int size)
  811. {
  812. struct skge_rx_desc *rd = e->desc;
  813. rd->csum2 = 0;
  814. rd->csum2_start = ETH_HLEN;
  815. wmb();
  816. rd->control = BMU_OWN | BMU_STF | BMU_IRQ_EOF | BMU_TCP_CHECK | size;
  817. }
  818. /* Free all buffers in receive ring, assumes receiver stopped */
  819. static void skge_rx_clean(struct skge_port *skge)
  820. {
  821. struct skge_hw *hw = skge->hw;
  822. struct skge_ring *ring = &skge->rx_ring;
  823. struct skge_element *e;
  824. e = ring->start;
  825. do {
  826. struct skge_rx_desc *rd = e->desc;
  827. rd->control = 0;
  828. if (e->skb) {
  829. pci_unmap_single(hw->pdev,
  830. dma_unmap_addr(e, mapaddr),
  831. dma_unmap_len(e, maplen),
  832. PCI_DMA_FROMDEVICE);
  833. dev_kfree_skb(e->skb);
  834. e->skb = NULL;
  835. }
  836. } while ((e = e->next) != ring->start);
  837. }
  838. /* Allocate buffers for receive ring
  839. * For receive: to_clean is next received frame.
  840. */
  841. static int skge_rx_fill(struct net_device *dev)
  842. {
  843. struct skge_port *skge = netdev_priv(dev);
  844. struct skge_ring *ring = &skge->rx_ring;
  845. struct skge_element *e;
  846. e = ring->start;
  847. do {
  848. struct sk_buff *skb;
  849. skb = __netdev_alloc_skb(dev, skge->rx_buf_size + NET_IP_ALIGN,
  850. GFP_KERNEL);
  851. if (!skb)
  852. return -ENOMEM;
  853. skb_reserve(skb, NET_IP_ALIGN);
  854. if (skge_rx_setup(skge, e, skb, skge->rx_buf_size) < 0) {
  855. dev_kfree_skb(skb);
  856. return -EIO;
  857. }
  858. } while ((e = e->next) != ring->start);
  859. ring->to_clean = ring->start;
  860. return 0;
  861. }
  862. static const char *skge_pause(enum pause_status status)
  863. {
  864. switch (status) {
  865. case FLOW_STAT_NONE:
  866. return "none";
  867. case FLOW_STAT_REM_SEND:
  868. return "rx only";
  869. case FLOW_STAT_LOC_SEND:
  870. return "tx_only";
  871. case FLOW_STAT_SYMMETRIC: /* Both station may send PAUSE */
  872. return "both";
  873. default:
  874. return "indeterminated";
  875. }
  876. }
  877. static void skge_link_up(struct skge_port *skge)
  878. {
  879. skge_write8(skge->hw, SK_REG(skge->port, LNK_LED_REG),
  880. LED_BLK_OFF|LED_SYNC_OFF|LED_ON);
  881. netif_carrier_on(skge->netdev);
  882. netif_wake_queue(skge->netdev);
  883. netif_info(skge, link, skge->netdev,
  884. "Link is up at %d Mbps, %s duplex, flow control %s\n",
  885. skge->speed,
  886. skge->duplex == DUPLEX_FULL ? "full" : "half",
  887. skge_pause(skge->flow_status));
  888. }
  889. static void skge_link_down(struct skge_port *skge)
  890. {
  891. skge_write8(skge->hw, SK_REG(skge->port, LNK_LED_REG), LED_OFF);
  892. netif_carrier_off(skge->netdev);
  893. netif_stop_queue(skge->netdev);
  894. netif_info(skge, link, skge->netdev, "Link is down\n");
  895. }
  896. static void xm_link_down(struct skge_hw *hw, int port)
  897. {
  898. struct net_device *dev = hw->dev[port];
  899. struct skge_port *skge = netdev_priv(dev);
  900. xm_write16(hw, port, XM_IMSK, XM_IMSK_DISABLE);
  901. if (netif_carrier_ok(dev))
  902. skge_link_down(skge);
  903. }
  904. static int __xm_phy_read(struct skge_hw *hw, int port, u16 reg, u16 *val)
  905. {
  906. int i;
  907. xm_write16(hw, port, XM_PHY_ADDR, reg | hw->phy_addr);
  908. *val = xm_read16(hw, port, XM_PHY_DATA);
  909. if (hw->phy_type == SK_PHY_XMAC)
  910. goto ready;
  911. for (i = 0; i < PHY_RETRIES; i++) {
  912. if (xm_read16(hw, port, XM_MMU_CMD) & XM_MMU_PHY_RDY)
  913. goto ready;
  914. udelay(1);
  915. }
  916. return -ETIMEDOUT;
  917. ready:
  918. *val = xm_read16(hw, port, XM_PHY_DATA);
  919. return 0;
  920. }
  921. static u16 xm_phy_read(struct skge_hw *hw, int port, u16 reg)
  922. {
  923. u16 v = 0;
  924. if (__xm_phy_read(hw, port, reg, &v))
  925. pr_warn("%s: phy read timed out\n", hw->dev[port]->name);
  926. return v;
  927. }
  928. static int xm_phy_write(struct skge_hw *hw, int port, u16 reg, u16 val)
  929. {
  930. int i;
  931. xm_write16(hw, port, XM_PHY_ADDR, reg | hw->phy_addr);
  932. for (i = 0; i < PHY_RETRIES; i++) {
  933. if (!(xm_read16(hw, port, XM_MMU_CMD) & XM_MMU_PHY_BUSY))
  934. goto ready;
  935. udelay(1);
  936. }
  937. return -EIO;
  938. ready:
  939. xm_write16(hw, port, XM_PHY_DATA, val);
  940. for (i = 0; i < PHY_RETRIES; i++) {
  941. if (!(xm_read16(hw, port, XM_MMU_CMD) & XM_MMU_PHY_BUSY))
  942. return 0;
  943. udelay(1);
  944. }
  945. return -ETIMEDOUT;
  946. }
  947. static void genesis_init(struct skge_hw *hw)
  948. {
  949. /* set blink source counter */
  950. skge_write32(hw, B2_BSC_INI, (SK_BLK_DUR * SK_FACT_53) / 100);
  951. skge_write8(hw, B2_BSC_CTRL, BSC_START);
  952. /* configure mac arbiter */
  953. skge_write16(hw, B3_MA_TO_CTRL, MA_RST_CLR);
  954. /* configure mac arbiter timeout values */
  955. skge_write8(hw, B3_MA_TOINI_RX1, SK_MAC_TO_53);
  956. skge_write8(hw, B3_MA_TOINI_RX2, SK_MAC_TO_53);
  957. skge_write8(hw, B3_MA_TOINI_TX1, SK_MAC_TO_53);
  958. skge_write8(hw, B3_MA_TOINI_TX2, SK_MAC_TO_53);
  959. skge_write8(hw, B3_MA_RCINI_RX1, 0);
  960. skge_write8(hw, B3_MA_RCINI_RX2, 0);
  961. skge_write8(hw, B3_MA_RCINI_TX1, 0);
  962. skge_write8(hw, B3_MA_RCINI_TX2, 0);
  963. /* configure packet arbiter timeout */
  964. skge_write16(hw, B3_PA_CTRL, PA_RST_CLR);
  965. skge_write16(hw, B3_PA_TOINI_RX1, SK_PKT_TO_MAX);
  966. skge_write16(hw, B3_PA_TOINI_TX1, SK_PKT_TO_MAX);
  967. skge_write16(hw, B3_PA_TOINI_RX2, SK_PKT_TO_MAX);
  968. skge_write16(hw, B3_PA_TOINI_TX2, SK_PKT_TO_MAX);
  969. }
  970. static void genesis_reset(struct skge_hw *hw, int port)
  971. {
  972. static const u8 zero[8] = { 0 };
  973. u32 reg;
  974. skge_write8(hw, SK_REG(port, GMAC_IRQ_MSK), 0);
  975. /* reset the statistics module */
  976. xm_write32(hw, port, XM_GP_PORT, XM_GP_RES_STAT);
  977. xm_write16(hw, port, XM_IMSK, XM_IMSK_DISABLE);
  978. xm_write32(hw, port, XM_MODE, 0); /* clear Mode Reg */
  979. xm_write16(hw, port, XM_TX_CMD, 0); /* reset TX CMD Reg */
  980. xm_write16(hw, port, XM_RX_CMD, 0); /* reset RX CMD Reg */
  981. /* disable Broadcom PHY IRQ */
  982. if (hw->phy_type == SK_PHY_BCOM)
  983. xm_write16(hw, port, PHY_BCOM_INT_MASK, 0xffff);
  984. xm_outhash(hw, port, XM_HSM, zero);
  985. /* Flush TX and RX fifo */
  986. reg = xm_read32(hw, port, XM_MODE);
  987. xm_write32(hw, port, XM_MODE, reg | XM_MD_FTF);
  988. xm_write32(hw, port, XM_MODE, reg | XM_MD_FRF);
  989. }
  990. /* Convert mode to MII values */
  991. static const u16 phy_pause_map[] = {
  992. [FLOW_MODE_NONE] = 0,
  993. [FLOW_MODE_LOC_SEND] = PHY_AN_PAUSE_ASYM,
  994. [FLOW_MODE_SYMMETRIC] = PHY_AN_PAUSE_CAP,
  995. [FLOW_MODE_SYM_OR_REM] = PHY_AN_PAUSE_CAP | PHY_AN_PAUSE_ASYM,
  996. };
  997. /* special defines for FIBER (88E1011S only) */
  998. static const u16 fiber_pause_map[] = {
  999. [FLOW_MODE_NONE] = PHY_X_P_NO_PAUSE,
  1000. [FLOW_MODE_LOC_SEND] = PHY_X_P_ASYM_MD,
  1001. [FLOW_MODE_SYMMETRIC] = PHY_X_P_SYM_MD,
  1002. [FLOW_MODE_SYM_OR_REM] = PHY_X_P_BOTH_MD,
  1003. };
  1004. /* Check status of Broadcom phy link */
  1005. static void bcom_check_link(struct skge_hw *hw, int port)
  1006. {
  1007. struct net_device *dev = hw->dev[port];
  1008. struct skge_port *skge = netdev_priv(dev);
  1009. u16 status;
  1010. /* read twice because of latch */
  1011. xm_phy_read(hw, port, PHY_BCOM_STAT);
  1012. status = xm_phy_read(hw, port, PHY_BCOM_STAT);
  1013. if ((status & PHY_ST_LSYNC) == 0) {
  1014. xm_link_down(hw, port);
  1015. return;
  1016. }
  1017. if (skge->autoneg == AUTONEG_ENABLE) {
  1018. u16 lpa, aux;
  1019. if (!(status & PHY_ST_AN_OVER))
  1020. return;
  1021. lpa = xm_phy_read(hw, port, PHY_XMAC_AUNE_LP);
  1022. if (lpa & PHY_B_AN_RF) {
  1023. netdev_notice(dev, "remote fault\n");
  1024. return;
  1025. }
  1026. aux = xm_phy_read(hw, port, PHY_BCOM_AUX_STAT);
  1027. /* Check Duplex mismatch */
  1028. switch (aux & PHY_B_AS_AN_RES_MSK) {
  1029. case PHY_B_RES_1000FD:
  1030. skge->duplex = DUPLEX_FULL;
  1031. break;
  1032. case PHY_B_RES_1000HD:
  1033. skge->duplex = DUPLEX_HALF;
  1034. break;
  1035. default:
  1036. netdev_notice(dev, "duplex mismatch\n");
  1037. return;
  1038. }
  1039. /* We are using IEEE 802.3z/D5.0 Table 37-4 */
  1040. switch (aux & PHY_B_AS_PAUSE_MSK) {
  1041. case PHY_B_AS_PAUSE_MSK:
  1042. skge->flow_status = FLOW_STAT_SYMMETRIC;
  1043. break;
  1044. case PHY_B_AS_PRR:
  1045. skge->flow_status = FLOW_STAT_REM_SEND;
  1046. break;
  1047. case PHY_B_AS_PRT:
  1048. skge->flow_status = FLOW_STAT_LOC_SEND;
  1049. break;
  1050. default:
  1051. skge->flow_status = FLOW_STAT_NONE;
  1052. }
  1053. skge->speed = SPEED_1000;
  1054. }
  1055. if (!netif_carrier_ok(dev))
  1056. genesis_link_up(skge);
  1057. }
  1058. /* Broadcom 5400 only supports giagabit! SysKonnect did not put an additional
  1059. * Phy on for 100 or 10Mbit operation
  1060. */
  1061. static void bcom_phy_init(struct skge_port *skge)
  1062. {
  1063. struct skge_hw *hw = skge->hw;
  1064. int port = skge->port;
  1065. int i;
  1066. u16 id1, r, ext, ctl;
  1067. /* magic workaround patterns for Broadcom */
  1068. static const struct {
  1069. u16 reg;
  1070. u16 val;
  1071. } A1hack[] = {
  1072. { 0x18, 0x0c20 }, { 0x17, 0x0012 }, { 0x15, 0x1104 },
  1073. { 0x17, 0x0013 }, { 0x15, 0x0404 }, { 0x17, 0x8006 },
  1074. { 0x15, 0x0132 }, { 0x17, 0x8006 }, { 0x15, 0x0232 },
  1075. { 0x17, 0x800D }, { 0x15, 0x000F }, { 0x18, 0x0420 },
  1076. }, C0hack[] = {
  1077. { 0x18, 0x0c20 }, { 0x17, 0x0012 }, { 0x15, 0x1204 },
  1078. { 0x17, 0x0013 }, { 0x15, 0x0A04 }, { 0x18, 0x0420 },
  1079. };
  1080. /* read Id from external PHY (all have the same address) */
  1081. id1 = xm_phy_read(hw, port, PHY_XMAC_ID1);
  1082. /* Optimize MDIO transfer by suppressing preamble. */
  1083. r = xm_read16(hw, port, XM_MMU_CMD);
  1084. r |= XM_MMU_NO_PRE;
  1085. xm_write16(hw, port, XM_MMU_CMD, r);
  1086. switch (id1) {
  1087. case PHY_BCOM_ID1_C0:
  1088. /*
  1089. * Workaround BCOM Errata for the C0 type.
  1090. * Write magic patterns to reserved registers.
  1091. */
  1092. for (i = 0; i < ARRAY_SIZE(C0hack); i++)
  1093. xm_phy_write(hw, port,
  1094. C0hack[i].reg, C0hack[i].val);
  1095. break;
  1096. case PHY_BCOM_ID1_A1:
  1097. /*
  1098. * Workaround BCOM Errata for the A1 type.
  1099. * Write magic patterns to reserved registers.
  1100. */
  1101. for (i = 0; i < ARRAY_SIZE(A1hack); i++)
  1102. xm_phy_write(hw, port,
  1103. A1hack[i].reg, A1hack[i].val);
  1104. break;
  1105. }
  1106. /*
  1107. * Workaround BCOM Errata (#10523) for all BCom PHYs.
  1108. * Disable Power Management after reset.
  1109. */
  1110. r = xm_phy_read(hw, port, PHY_BCOM_AUX_CTRL);
  1111. r |= PHY_B_AC_DIS_PM;
  1112. xm_phy_write(hw, port, PHY_BCOM_AUX_CTRL, r);
  1113. /* Dummy read */
  1114. xm_read16(hw, port, XM_ISRC);
  1115. ext = PHY_B_PEC_EN_LTR; /* enable tx led */
  1116. ctl = PHY_CT_SP1000; /* always 1000mbit */
  1117. if (skge->autoneg == AUTONEG_ENABLE) {
  1118. /*
  1119. * Workaround BCOM Errata #1 for the C5 type.
  1120. * 1000Base-T Link Acquisition Failure in Slave Mode
  1121. * Set Repeater/DTE bit 10 of the 1000Base-T Control Register
  1122. */
  1123. u16 adv = PHY_B_1000C_RD;
  1124. if (skge->advertising & ADVERTISED_1000baseT_Half)
  1125. adv |= PHY_B_1000C_AHD;
  1126. if (skge->advertising & ADVERTISED_1000baseT_Full)
  1127. adv |= PHY_B_1000C_AFD;
  1128. xm_phy_write(hw, port, PHY_BCOM_1000T_CTRL, adv);
  1129. ctl |= PHY_CT_ANE | PHY_CT_RE_CFG;
  1130. } else {
  1131. if (skge->duplex == DUPLEX_FULL)
  1132. ctl |= PHY_CT_DUP_MD;
  1133. /* Force to slave */
  1134. xm_phy_write(hw, port, PHY_BCOM_1000T_CTRL, PHY_B_1000C_MSE);
  1135. }
  1136. /* Set autonegotiation pause parameters */
  1137. xm_phy_write(hw, port, PHY_BCOM_AUNE_ADV,
  1138. phy_pause_map[skge->flow_control] | PHY_AN_CSMA);
  1139. /* Handle Jumbo frames */
  1140. if (hw->dev[port]->mtu > ETH_DATA_LEN) {
  1141. xm_phy_write(hw, port, PHY_BCOM_AUX_CTRL,
  1142. PHY_B_AC_TX_TST | PHY_B_AC_LONG_PACK);
  1143. ext |= PHY_B_PEC_HIGH_LA;
  1144. }
  1145. xm_phy_write(hw, port, PHY_BCOM_P_EXT_CTRL, ext);
  1146. xm_phy_write(hw, port, PHY_BCOM_CTRL, ctl);
  1147. /* Use link status change interrupt */
  1148. xm_phy_write(hw, port, PHY_BCOM_INT_MASK, PHY_B_DEF_MSK);
  1149. }
  1150. static void xm_phy_init(struct skge_port *skge)
  1151. {
  1152. struct skge_hw *hw = skge->hw;
  1153. int port = skge->port;
  1154. u16 ctrl = 0;
  1155. if (skge->autoneg == AUTONEG_ENABLE) {
  1156. if (skge->advertising & ADVERTISED_1000baseT_Half)
  1157. ctrl |= PHY_X_AN_HD;
  1158. if (skge->advertising & ADVERTISED_1000baseT_Full)
  1159. ctrl |= PHY_X_AN_FD;
  1160. ctrl |= fiber_pause_map[skge->flow_control];
  1161. xm_phy_write(hw, port, PHY_XMAC_AUNE_ADV, ctrl);
  1162. /* Restart Auto-negotiation */
  1163. ctrl = PHY_CT_ANE | PHY_CT_RE_CFG;
  1164. } else {
  1165. /* Set DuplexMode in Config register */
  1166. if (skge->duplex == DUPLEX_FULL)
  1167. ctrl |= PHY_CT_DUP_MD;
  1168. /*
  1169. * Do NOT enable Auto-negotiation here. This would hold
  1170. * the link down because no IDLEs are transmitted
  1171. */
  1172. }
  1173. xm_phy_write(hw, port, PHY_XMAC_CTRL, ctrl);
  1174. /* Poll PHY for status changes */
  1175. mod_timer(&skge->link_timer, jiffies + LINK_HZ);
  1176. }
  1177. static int xm_check_link(struct net_device *dev)
  1178. {
  1179. struct skge_port *skge = netdev_priv(dev);
  1180. struct skge_hw *hw = skge->hw;
  1181. int port = skge->port;
  1182. u16 status;
  1183. /* read twice because of latch */
  1184. xm_phy_read(hw, port, PHY_XMAC_STAT);
  1185. status = xm_phy_read(hw, port, PHY_XMAC_STAT);
  1186. if ((status & PHY_ST_LSYNC) == 0) {
  1187. xm_link_down(hw, port);
  1188. return 0;
  1189. }
  1190. if (skge->autoneg == AUTONEG_ENABLE) {
  1191. u16 lpa, res;
  1192. if (!(status & PHY_ST_AN_OVER))
  1193. return 0;
  1194. lpa = xm_phy_read(hw, port, PHY_XMAC_AUNE_LP);
  1195. if (lpa & PHY_B_AN_RF) {
  1196. netdev_notice(dev, "remote fault\n");
  1197. return 0;
  1198. }
  1199. res = xm_phy_read(hw, port, PHY_XMAC_RES_ABI);
  1200. /* Check Duplex mismatch */
  1201. switch (res & (PHY_X_RS_HD | PHY_X_RS_FD)) {
  1202. case PHY_X_RS_FD:
  1203. skge->duplex = DUPLEX_FULL;
  1204. break;
  1205. case PHY_X_RS_HD:
  1206. skge->duplex = DUPLEX_HALF;
  1207. break;
  1208. default:
  1209. netdev_notice(dev, "duplex mismatch\n");
  1210. return 0;
  1211. }
  1212. /* We are using IEEE 802.3z/D5.0 Table 37-4 */
  1213. if ((skge->flow_control == FLOW_MODE_SYMMETRIC ||
  1214. skge->flow_control == FLOW_MODE_SYM_OR_REM) &&
  1215. (lpa & PHY_X_P_SYM_MD))
  1216. skge->flow_status = FLOW_STAT_SYMMETRIC;
  1217. else if (skge->flow_control == FLOW_MODE_SYM_OR_REM &&
  1218. (lpa & PHY_X_RS_PAUSE) == PHY_X_P_ASYM_MD)
  1219. /* Enable PAUSE receive, disable PAUSE transmit */
  1220. skge->flow_status = FLOW_STAT_REM_SEND;
  1221. else if (skge->flow_control == FLOW_MODE_LOC_SEND &&
  1222. (lpa & PHY_X_RS_PAUSE) == PHY_X_P_BOTH_MD)
  1223. /* Disable PAUSE receive, enable PAUSE transmit */
  1224. skge->flow_status = FLOW_STAT_LOC_SEND;
  1225. else
  1226. skge->flow_status = FLOW_STAT_NONE;
  1227. skge->speed = SPEED_1000;
  1228. }
  1229. if (!netif_carrier_ok(dev))
  1230. genesis_link_up(skge);
  1231. return 1;
  1232. }
  1233. /* Poll to check for link coming up.
  1234. *
  1235. * Since internal PHY is wired to a level triggered pin, can't
  1236. * get an interrupt when carrier is detected, need to poll for
  1237. * link coming up.
  1238. */
  1239. static void xm_link_timer(unsigned long arg)
  1240. {
  1241. struct skge_port *skge = (struct skge_port *) arg;
  1242. struct net_device *dev = skge->netdev;
  1243. struct skge_hw *hw = skge->hw;
  1244. int port = skge->port;
  1245. int i;
  1246. unsigned long flags;
  1247. if (!netif_running(dev))
  1248. return;
  1249. spin_lock_irqsave(&hw->phy_lock, flags);
  1250. /*
  1251. * Verify that the link by checking GPIO register three times.
  1252. * This pin has the signal from the link_sync pin connected to it.
  1253. */
  1254. for (i = 0; i < 3; i++) {
  1255. if (xm_read16(hw, port, XM_GP_PORT) & XM_GP_INP_ASS)
  1256. goto link_down;
  1257. }
  1258. /* Re-enable interrupt to detect link down */
  1259. if (xm_check_link(dev)) {
  1260. u16 msk = xm_read16(hw, port, XM_IMSK);
  1261. msk &= ~XM_IS_INP_ASS;
  1262. xm_write16(hw, port, XM_IMSK, msk);
  1263. xm_read16(hw, port, XM_ISRC);
  1264. } else {
  1265. link_down:
  1266. mod_timer(&skge->link_timer,
  1267. round_jiffies(jiffies + LINK_HZ));
  1268. }
  1269. spin_unlock_irqrestore(&hw->phy_lock, flags);
  1270. }
  1271. static void genesis_mac_init(struct skge_hw *hw, int port)
  1272. {
  1273. struct net_device *dev = hw->dev[port];
  1274. struct skge_port *skge = netdev_priv(dev);
  1275. int jumbo = hw->dev[port]->mtu > ETH_DATA_LEN;
  1276. int i;
  1277. u32 r;
  1278. static const u8 zero[6] = { 0 };
  1279. for (i = 0; i < 10; i++) {
  1280. skge_write16(hw, SK_REG(port, TX_MFF_CTRL1),
  1281. MFF_SET_MAC_RST);
  1282. if (skge_read16(hw, SK_REG(port, TX_MFF_CTRL1)) & MFF_SET_MAC_RST)
  1283. goto reset_ok;
  1284. udelay(1);
  1285. }
  1286. netdev_warn(dev, "genesis reset failed\n");
  1287. reset_ok:
  1288. /* Unreset the XMAC. */
  1289. skge_write16(hw, SK_REG(port, TX_MFF_CTRL1), MFF_CLR_MAC_RST);
  1290. /*
  1291. * Perform additional initialization for external PHYs,
  1292. * namely for the 1000baseTX cards that use the XMAC's
  1293. * GMII mode.
  1294. */
  1295. if (hw->phy_type != SK_PHY_XMAC) {
  1296. /* Take external Phy out of reset */
  1297. r = skge_read32(hw, B2_GP_IO);
  1298. if (port == 0)
  1299. r |= GP_DIR_0|GP_IO_0;
  1300. else
  1301. r |= GP_DIR_2|GP_IO_2;
  1302. skge_write32(hw, B2_GP_IO, r);
  1303. /* Enable GMII interface */
  1304. xm_write16(hw, port, XM_HW_CFG, XM_HW_GMII_MD);
  1305. }
  1306. switch (hw->phy_type) {
  1307. case SK_PHY_XMAC:
  1308. xm_phy_init(skge);
  1309. break;
  1310. case SK_PHY_BCOM:
  1311. bcom_phy_init(skge);
  1312. bcom_check_link(hw, port);
  1313. }
  1314. /* Set Station Address */
  1315. xm_outaddr(hw, port, XM_SA, dev->dev_addr);
  1316. /* We don't use match addresses so clear */
  1317. for (i = 1; i < 16; i++)
  1318. xm_outaddr(hw, port, XM_EXM(i), zero);
  1319. /* Clear MIB counters */
  1320. xm_write16(hw, port, XM_STAT_CMD,
  1321. XM_SC_CLR_RXC | XM_SC_CLR_TXC);
  1322. /* Clear two times according to Errata #3 */
  1323. xm_write16(hw, port, XM_STAT_CMD,
  1324. XM_SC_CLR_RXC | XM_SC_CLR_TXC);
  1325. /* configure Rx High Water Mark (XM_RX_HI_WM) */
  1326. xm_write16(hw, port, XM_RX_HI_WM, 1450);
  1327. /* We don't need the FCS appended to the packet. */
  1328. r = XM_RX_LENERR_OK | XM_RX_STRIP_FCS;
  1329. if (jumbo)
  1330. r |= XM_RX_BIG_PK_OK;
  1331. if (skge->duplex == DUPLEX_HALF) {
  1332. /*
  1333. * If in manual half duplex mode the other side might be in
  1334. * full duplex mode, so ignore if a carrier extension is not seen
  1335. * on frames received
  1336. */
  1337. r |= XM_RX_DIS_CEXT;
  1338. }
  1339. xm_write16(hw, port, XM_RX_CMD, r);
  1340. /* We want short frames padded to 60 bytes. */
  1341. xm_write16(hw, port, XM_TX_CMD, XM_TX_AUTO_PAD);
  1342. /* Increase threshold for jumbo frames on dual port */
  1343. if (hw->ports > 1 && jumbo)
  1344. xm_write16(hw, port, XM_TX_THR, 1020);
  1345. else
  1346. xm_write16(hw, port, XM_TX_THR, 512);
  1347. /*
  1348. * Enable the reception of all error frames. This is is
  1349. * a necessary evil due to the design of the XMAC. The
  1350. * XMAC's receive FIFO is only 8K in size, however jumbo
  1351. * frames can be up to 9000 bytes in length. When bad
  1352. * frame filtering is enabled, the XMAC's RX FIFO operates
  1353. * in 'store and forward' mode. For this to work, the
  1354. * entire frame has to fit into the FIFO, but that means
  1355. * that jumbo frames larger than 8192 bytes will be
  1356. * truncated. Disabling all bad frame filtering causes
  1357. * the RX FIFO to operate in streaming mode, in which
  1358. * case the XMAC will start transferring frames out of the
  1359. * RX FIFO as soon as the FIFO threshold is reached.
  1360. */
  1361. xm_write32(hw, port, XM_MODE, XM_DEF_MODE);
  1362. /*
  1363. * Initialize the Receive Counter Event Mask (XM_RX_EV_MSK)
  1364. * - Enable all bits excepting 'Octets Rx OK Low CntOv'
  1365. * and 'Octets Rx OK Hi Cnt Ov'.
  1366. */
  1367. xm_write32(hw, port, XM_RX_EV_MSK, XMR_DEF_MSK);
  1368. /*
  1369. * Initialize the Transmit Counter Event Mask (XM_TX_EV_MSK)
  1370. * - Enable all bits excepting 'Octets Tx OK Low CntOv'
  1371. * and 'Octets Tx OK Hi Cnt Ov'.
  1372. */
  1373. xm_write32(hw, port, XM_TX_EV_MSK, XMT_DEF_MSK);
  1374. /* Configure MAC arbiter */
  1375. skge_write16(hw, B3_MA_TO_CTRL, MA_RST_CLR);
  1376. /* configure timeout values */
  1377. skge_write8(hw, B3_MA_TOINI_RX1, 72);
  1378. skge_write8(hw, B3_MA_TOINI_RX2, 72);
  1379. skge_write8(hw, B3_MA_TOINI_TX1, 72);
  1380. skge_write8(hw, B3_MA_TOINI_TX2, 72);
  1381. skge_write8(hw, B3_MA_RCINI_RX1, 0);
  1382. skge_write8(hw, B3_MA_RCINI_RX2, 0);
  1383. skge_write8(hw, B3_MA_RCINI_TX1, 0);
  1384. skge_write8(hw, B3_MA_RCINI_TX2, 0);
  1385. /* Configure Rx MAC FIFO */
  1386. skge_write8(hw, SK_REG(port, RX_MFF_CTRL2), MFF_RST_CLR);
  1387. skge_write16(hw, SK_REG(port, RX_MFF_CTRL1), MFF_ENA_TIM_PAT);
  1388. skge_write8(hw, SK_REG(port, RX_MFF_CTRL2), MFF_ENA_OP_MD);
  1389. /* Configure Tx MAC FIFO */
  1390. skge_write8(hw, SK_REG(port, TX_MFF_CTRL2), MFF_RST_CLR);
  1391. skge_write16(hw, SK_REG(port, TX_MFF_CTRL1), MFF_TX_CTRL_DEF);
  1392. skge_write8(hw, SK_REG(port, TX_MFF_CTRL2), MFF_ENA_OP_MD);
  1393. if (jumbo) {
  1394. /* Enable frame flushing if jumbo frames used */
  1395. skge_write16(hw, SK_REG(port, RX_MFF_CTRL1), MFF_ENA_FLUSH);
  1396. } else {
  1397. /* enable timeout timers if normal frames */
  1398. skge_write16(hw, B3_PA_CTRL,
  1399. (port == 0) ? PA_ENA_TO_TX1 : PA_ENA_TO_TX2);
  1400. }
  1401. }
  1402. static void genesis_stop(struct skge_port *skge)
  1403. {
  1404. struct skge_hw *hw = skge->hw;
  1405. int port = skge->port;
  1406. unsigned retries = 1000;
  1407. u16 cmd;
  1408. /* Disable Tx and Rx */
  1409. cmd = xm_read16(hw, port, XM_MMU_CMD);
  1410. cmd &= ~(XM_MMU_ENA_RX | XM_MMU_ENA_TX);
  1411. xm_write16(hw, port, XM_MMU_CMD, cmd);
  1412. genesis_reset(hw, port);
  1413. /* Clear Tx packet arbiter timeout IRQ */
  1414. skge_write16(hw, B3_PA_CTRL,
  1415. port == 0 ? PA_CLR_TO_TX1 : PA_CLR_TO_TX2);
  1416. /* Reset the MAC */
  1417. skge_write16(hw, SK_REG(port, TX_MFF_CTRL1), MFF_CLR_MAC_RST);
  1418. do {
  1419. skge_write16(hw, SK_REG(port, TX_MFF_CTRL1), MFF_SET_MAC_RST);
  1420. if (!(skge_read16(hw, SK_REG(port, TX_MFF_CTRL1)) & MFF_SET_MAC_RST))
  1421. break;
  1422. } while (--retries > 0);
  1423. /* For external PHYs there must be special handling */
  1424. if (hw->phy_type != SK_PHY_XMAC) {
  1425. u32 reg = skge_read32(hw, B2_GP_IO);
  1426. if (port == 0) {
  1427. reg |= GP_DIR_0;
  1428. reg &= ~GP_IO_0;
  1429. } else {
  1430. reg |= GP_DIR_2;
  1431. reg &= ~GP_IO_2;
  1432. }
  1433. skge_write32(hw, B2_GP_IO, reg);
  1434. skge_read32(hw, B2_GP_IO);
  1435. }
  1436. xm_write16(hw, port, XM_MMU_CMD,
  1437. xm_read16(hw, port, XM_MMU_CMD)
  1438. & ~(XM_MMU_ENA_RX | XM_MMU_ENA_TX));
  1439. xm_read16(hw, port, XM_MMU_CMD);
  1440. }
  1441. static void genesis_get_stats(struct skge_port *skge, u64 *data)
  1442. {
  1443. struct skge_hw *hw = skge->hw;
  1444. int port = skge->port;
  1445. int i;
  1446. unsigned long timeout = jiffies + HZ;
  1447. xm_write16(hw, port,
  1448. XM_STAT_CMD, XM_SC_SNP_TXC | XM_SC_SNP_RXC);
  1449. /* wait for update to complete */
  1450. while (xm_read16(hw, port, XM_STAT_CMD)
  1451. & (XM_SC_SNP_TXC | XM_SC_SNP_RXC)) {
  1452. if (time_after(jiffies, timeout))
  1453. break;
  1454. udelay(10);
  1455. }
  1456. /* special case for 64 bit octet counter */
  1457. data[0] = (u64) xm_read32(hw, port, XM_TXO_OK_HI) << 32
  1458. | xm_read32(hw, port, XM_TXO_OK_LO);
  1459. data[1] = (u64) xm_read32(hw, port, XM_RXO_OK_HI) << 32
  1460. | xm_read32(hw, port, XM_RXO_OK_LO);
  1461. for (i = 2; i < ARRAY_SIZE(skge_stats); i++)
  1462. data[i] = xm_read32(hw, port, skge_stats[i].xmac_offset);
  1463. }
  1464. static void genesis_mac_intr(struct skge_hw *hw, int port)
  1465. {
  1466. struct net_device *dev = hw->dev[port];
  1467. struct skge_port *skge = netdev_priv(dev);
  1468. u16 status = xm_read16(hw, port, XM_ISRC);
  1469. netif_printk(skge, intr, KERN_DEBUG, skge->netdev,
  1470. "mac interrupt status 0x%x\n", status);
  1471. if (hw->phy_type == SK_PHY_XMAC && (status & XM_IS_INP_ASS)) {
  1472. xm_link_down(hw, port);
  1473. mod_timer(&skge->link_timer, jiffies + 1);
  1474. }
  1475. if (status & XM_IS_TXF_UR) {
  1476. xm_write32(hw, port, XM_MODE, XM_MD_FTF);
  1477. ++dev->stats.tx_fifo_errors;
  1478. }
  1479. }
  1480. static void genesis_link_up(struct skge_port *skge)
  1481. {
  1482. struct skge_hw *hw = skge->hw;
  1483. int port = skge->port;
  1484. u16 cmd, msk;
  1485. u32 mode;
  1486. cmd = xm_read16(hw, port, XM_MMU_CMD);
  1487. /*
  1488. * enabling pause frame reception is required for 1000BT
  1489. * because the XMAC is not reset if the link is going down
  1490. */
  1491. if (skge->flow_status == FLOW_STAT_NONE ||
  1492. skge->flow_status == FLOW_STAT_LOC_SEND)
  1493. /* Disable Pause Frame Reception */
  1494. cmd |= XM_MMU_IGN_PF;
  1495. else
  1496. /* Enable Pause Frame Reception */
  1497. cmd &= ~XM_MMU_IGN_PF;
  1498. xm_write16(hw, port, XM_MMU_CMD, cmd);
  1499. mode = xm_read32(hw, port, XM_MODE);
  1500. if (skge->flow_status == FLOW_STAT_SYMMETRIC ||
  1501. skge->flow_status == FLOW_STAT_LOC_SEND) {
  1502. /*
  1503. * Configure Pause Frame Generation
  1504. * Use internal and external Pause Frame Generation.
  1505. * Sending pause frames is edge triggered.
  1506. * Send a Pause frame with the maximum pause time if
  1507. * internal oder external FIFO full condition occurs.
  1508. * Send a zero pause time frame to re-start transmission.
  1509. */
  1510. /* XM_PAUSE_DA = '010000C28001' (default) */
  1511. /* XM_MAC_PTIME = 0xffff (maximum) */
  1512. /* remember this value is defined in big endian (!) */
  1513. xm_write16(hw, port, XM_MAC_PTIME, 0xffff);
  1514. mode |= XM_PAUSE_MODE;
  1515. skge_write16(hw, SK_REG(port, RX_MFF_CTRL1), MFF_ENA_PAUSE);
  1516. } else {
  1517. /*
  1518. * disable pause frame generation is required for 1000BT
  1519. * because the XMAC is not reset if the link is going down
  1520. */
  1521. /* Disable Pause Mode in Mode Register */
  1522. mode &= ~XM_PAUSE_MODE;
  1523. skge_write16(hw, SK_REG(port, RX_MFF_CTRL1), MFF_DIS_PAUSE);
  1524. }
  1525. xm_write32(hw, port, XM_MODE, mode);
  1526. /* Turn on detection of Tx underrun */
  1527. msk = xm_read16(hw, port, XM_IMSK);
  1528. msk &= ~XM_IS_TXF_UR;
  1529. xm_write16(hw, port, XM_IMSK, msk);
  1530. xm_read16(hw, port, XM_ISRC);
  1531. /* get MMU Command Reg. */
  1532. cmd = xm_read16(hw, port, XM_MMU_CMD);
  1533. if (hw->phy_type != SK_PHY_XMAC && skge->duplex == DUPLEX_FULL)
  1534. cmd |= XM_MMU_GMII_FD;
  1535. /*
  1536. * Workaround BCOM Errata (#10523) for all BCom Phys
  1537. * Enable Power Management after link up
  1538. */
  1539. if (hw->phy_type == SK_PHY_BCOM) {
  1540. xm_phy_write(hw, port, PHY_BCOM_AUX_CTRL,
  1541. xm_phy_read(hw, port, PHY_BCOM_AUX_CTRL)
  1542. & ~PHY_B_AC_DIS_PM);
  1543. xm_phy_write(hw, port, PHY_BCOM_INT_MASK, PHY_B_DEF_MSK);
  1544. }
  1545. /* enable Rx/Tx */
  1546. xm_write16(hw, port, XM_MMU_CMD,
  1547. cmd | XM_MMU_ENA_RX | XM_MMU_ENA_TX);
  1548. skge_link_up(skge);
  1549. }
  1550. static inline void bcom_phy_intr(struct skge_port *skge)
  1551. {
  1552. struct skge_hw *hw = skge->hw;
  1553. int port = skge->port;
  1554. u16 isrc;
  1555. isrc = xm_phy_read(hw, port, PHY_BCOM_INT_STAT);
  1556. netif_printk(skge, intr, KERN_DEBUG, skge->netdev,
  1557. "phy interrupt status 0x%x\n", isrc);
  1558. if (isrc & PHY_B_IS_PSE)
  1559. pr_err("%s: uncorrectable pair swap error\n",
  1560. hw->dev[port]->name);
  1561. /* Workaround BCom Errata:
  1562. * enable and disable loopback mode if "NO HCD" occurs.
  1563. */
  1564. if (isrc & PHY_B_IS_NO_HDCL) {
  1565. u16 ctrl = xm_phy_read(hw, port, PHY_BCOM_CTRL);
  1566. xm_phy_write(hw, port, PHY_BCOM_CTRL,
  1567. ctrl | PHY_CT_LOOP);
  1568. xm_phy_write(hw, port, PHY_BCOM_CTRL,
  1569. ctrl & ~PHY_CT_LOOP);
  1570. }
  1571. if (isrc & (PHY_B_IS_AN_PR | PHY_B_IS_LST_CHANGE))
  1572. bcom_check_link(hw, port);
  1573. }
  1574. static int gm_phy_write(struct skge_hw *hw, int port, u16 reg, u16 val)
  1575. {
  1576. int i;
  1577. gma_write16(hw, port, GM_SMI_DATA, val);
  1578. gma_write16(hw, port, GM_SMI_CTRL,
  1579. GM_SMI_CT_PHY_AD(hw->phy_addr) | GM_SMI_CT_REG_AD(reg));
  1580. for (i = 0; i < PHY_RETRIES; i++) {
  1581. udelay(1);
  1582. if (!(gma_read16(hw, port, GM_SMI_CTRL) & GM_SMI_CT_BUSY))
  1583. return 0;
  1584. }
  1585. pr_warn("%s: phy write timeout\n", hw->dev[port]->name);
  1586. return -EIO;
  1587. }
  1588. static int __gm_phy_read(struct skge_hw *hw, int port, u16 reg, u16 *val)
  1589. {
  1590. int i;
  1591. gma_write16(hw, port, GM_SMI_CTRL,
  1592. GM_SMI_CT_PHY_AD(hw->phy_addr)
  1593. | GM_SMI_CT_REG_AD(reg) | GM_SMI_CT_OP_RD);
  1594. for (i = 0; i < PHY_RETRIES; i++) {
  1595. udelay(1);
  1596. if (gma_read16(hw, port, GM_SMI_CTRL) & GM_SMI_CT_RD_VAL)
  1597. goto ready;
  1598. }
  1599. return -ETIMEDOUT;
  1600. ready:
  1601. *val = gma_read16(hw, port, GM_SMI_DATA);
  1602. return 0;
  1603. }
  1604. static u16 gm_phy_read(struct skge_hw *hw, int port, u16 reg)
  1605. {
  1606. u16 v = 0;
  1607. if (__gm_phy_read(hw, port, reg, &v))
  1608. pr_warn("%s: phy read timeout\n", hw->dev[port]->name);
  1609. return v;
  1610. }
  1611. /* Marvell Phy Initialization */
  1612. static void yukon_init(struct skge_hw *hw, int port)
  1613. {
  1614. struct skge_port *skge = netdev_priv(hw->dev[port]);
  1615. u16 ctrl, ct1000, adv;
  1616. if (skge->autoneg == AUTONEG_ENABLE) {
  1617. u16 ectrl = gm_phy_read(hw, port, PHY_MARV_EXT_CTRL);
  1618. ectrl &= ~(PHY_M_EC_M_DSC_MSK | PHY_M_EC_S_DSC_MSK |
  1619. PHY_M_EC_MAC_S_MSK);
  1620. ectrl |= PHY_M_EC_MAC_S(MAC_TX_CLK_25_MHZ);
  1621. ectrl |= PHY_M_EC_M_DSC(0) | PHY_M_EC_S_DSC(1);
  1622. gm_phy_write(hw, port, PHY_MARV_EXT_CTRL, ectrl);
  1623. }
  1624. ctrl = gm_phy_read(hw, port, PHY_MARV_CTRL);
  1625. if (skge->autoneg == AUTONEG_DISABLE)
  1626. ctrl &= ~PHY_CT_ANE;
  1627. ctrl |= PHY_CT_RESET;
  1628. gm_phy_write(hw, port, PHY_MARV_CTRL, ctrl);
  1629. ctrl = 0;
  1630. ct1000 = 0;
  1631. adv = PHY_AN_CSMA;
  1632. if (skge->autoneg == AUTONEG_ENABLE) {
  1633. if (hw->copper) {
  1634. if (skge->advertising & ADVERTISED_1000baseT_Full)
  1635. ct1000 |= PHY_M_1000C_AFD;
  1636. if (skge->advertising & ADVERTISED_1000baseT_Half)
  1637. ct1000 |= PHY_M_1000C_AHD;
  1638. if (skge->advertising & ADVERTISED_100baseT_Full)
  1639. adv |= PHY_M_AN_100_FD;
  1640. if (skge->advertising & ADVERTISED_100baseT_Half)
  1641. adv |= PHY_M_AN_100_HD;
  1642. if (skge->advertising & ADVERTISED_10baseT_Full)
  1643. adv |= PHY_M_AN_10_FD;
  1644. if (skge->advertising & ADVERTISED_10baseT_Half)
  1645. adv |= PHY_M_AN_10_HD;
  1646. /* Set Flow-control capabilities */
  1647. adv |= phy_pause_map[skge->flow_control];
  1648. } else {
  1649. if (skge->advertising & ADVERTISED_1000baseT_Full)
  1650. adv |= PHY_M_AN_1000X_AFD;
  1651. if (skge->advertising & ADVERTISED_1000baseT_Half)
  1652. adv |= PHY_M_AN_1000X_AHD;
  1653. adv |= fiber_pause_map[skge->flow_control];
  1654. }
  1655. /* Restart Auto-negotiation */
  1656. ctrl |= PHY_CT_ANE | PHY_CT_RE_CFG;
  1657. } else {
  1658. /* forced speed/duplex settings */
  1659. ct1000 = PHY_M_1000C_MSE;
  1660. if (skge->duplex == DUPLEX_FULL)
  1661. ctrl |= PHY_CT_DUP_MD;
  1662. switch (skge->speed) {
  1663. case SPEED_1000:
  1664. ctrl |= PHY_CT_SP1000;
  1665. break;
  1666. case SPEED_100:
  1667. ctrl |= PHY_CT_SP100;
  1668. break;
  1669. }
  1670. ctrl |= PHY_CT_RESET;
  1671. }
  1672. gm_phy_write(hw, port, PHY_MARV_1000T_CTRL, ct1000);
  1673. gm_phy_write(hw, port, PHY_MARV_AUNE_ADV, adv);
  1674. gm_phy_write(hw, port, PHY_MARV_CTRL, ctrl);
  1675. /* Enable phy interrupt on autonegotiation complete (or link up) */
  1676. if (skge->autoneg == AUTONEG_ENABLE)
  1677. gm_phy_write(hw, port, PHY_MARV_INT_MASK, PHY_M_IS_AN_MSK);
  1678. else
  1679. gm_phy_write(hw, port, PHY_MARV_INT_MASK, PHY_M_IS_DEF_MSK);
  1680. }
  1681. static void yukon_reset(struct skge_hw *hw, int port)
  1682. {
  1683. gm_phy_write(hw, port, PHY_MARV_INT_MASK, 0);/* disable PHY IRQs */
  1684. gma_write16(hw, port, GM_MC_ADDR_H1, 0); /* clear MC hash */
  1685. gma_write16(hw, port, GM_MC_ADDR_H2, 0);
  1686. gma_write16(hw, port, GM_MC_ADDR_H3, 0);
  1687. gma_write16(hw, port, GM_MC_ADDR_H4, 0);
  1688. gma_write16(hw, port, GM_RX_CTRL,
  1689. gma_read16(hw, port, GM_RX_CTRL)
  1690. | GM_RXCR_UCF_ENA | GM_RXCR_MCF_ENA);
  1691. }
  1692. /* Apparently, early versions of Yukon-Lite had wrong chip_id? */
  1693. static int is_yukon_lite_a0(struct skge_hw *hw)
  1694. {
  1695. u32 reg;
  1696. int ret;
  1697. if (hw->chip_id != CHIP_ID_YUKON)
  1698. return 0;
  1699. reg = skge_read32(hw, B2_FAR);
  1700. skge_write8(hw, B2_FAR + 3, 0xff);
  1701. ret = (skge_read8(hw, B2_FAR + 3) != 0);
  1702. skge_write32(hw, B2_FAR, reg);
  1703. return ret;
  1704. }
  1705. static void yukon_mac_init(struct skge_hw *hw, int port)
  1706. {
  1707. struct skge_port *skge = netdev_priv(hw->dev[port]);
  1708. int i;
  1709. u32 reg;
  1710. const u8 *addr = hw->dev[port]->dev_addr;
  1711. /* WA code for COMA mode -- set PHY reset */
  1712. if (hw->chip_id == CHIP_ID_YUKON_LITE &&
  1713. hw->chip_rev >= CHIP_REV_YU_LITE_A3) {
  1714. reg = skge_read32(hw, B2_GP_IO);
  1715. reg |= GP_DIR_9 | GP_IO_9;
  1716. skge_write32(hw, B2_GP_IO, reg);
  1717. }
  1718. /* hard reset */
  1719. skge_write32(hw, SK_REG(port, GPHY_CTRL), GPC_RST_SET);
  1720. skge_write32(hw, SK_REG(port, GMAC_CTRL), GMC_RST_SET);
  1721. /* WA code for COMA mode -- clear PHY reset */
  1722. if (hw->chip_id == CHIP_ID_YUKON_LITE &&
  1723. hw->chip_rev >= CHIP_REV_YU_LITE_A3) {
  1724. reg = skge_read32(hw, B2_GP_IO);
  1725. reg |= GP_DIR_9;
  1726. reg &= ~GP_IO_9;
  1727. skge_write32(hw, B2_GP_IO, reg);
  1728. }
  1729. /* Set hardware config mode */
  1730. reg = GPC_INT_POL_HI | GPC_DIS_FC | GPC_DIS_SLEEP |
  1731. GPC_ENA_XC | GPC_ANEG_ADV_ALL_M | GPC_ENA_PAUSE;
  1732. reg |= hw->copper ? GPC_HWCFG_GMII_COP : GPC_HWCFG_GMII_FIB;
  1733. /* Clear GMC reset */
  1734. skge_write32(hw, SK_REG(port, GPHY_CTRL), reg | GPC_RST_SET);
  1735. skge_write32(hw, SK_REG(port, GPHY_CTRL), reg | GPC_RST_CLR);
  1736. skge_write32(hw, SK_REG(port, GMAC_CTRL), GMC_PAUSE_ON | GMC_RST_CLR);
  1737. if (skge->autoneg == AUTONEG_DISABLE) {
  1738. reg = GM_GPCR_AU_ALL_DIS;
  1739. gma_write16(hw, port, GM_GP_CTRL,
  1740. gma_read16(hw, port, GM_GP_CTRL) | reg);
  1741. switch (skge->speed) {
  1742. case SPEED_1000:
  1743. reg &= ~GM_GPCR_SPEED_100;
  1744. reg |= GM_GPCR_SPEED_1000;
  1745. break;
  1746. case SPEED_100:
  1747. reg &= ~GM_GPCR_SPEED_1000;
  1748. reg |= GM_GPCR_SPEED_100;
  1749. break;
  1750. case SPEED_10:
  1751. reg &= ~(GM_GPCR_SPEED_1000 | GM_GPCR_SPEED_100);
  1752. break;
  1753. }
  1754. if (skge->duplex == DUPLEX_FULL)
  1755. reg |= GM_GPCR_DUP_FULL;
  1756. } else
  1757. reg = GM_GPCR_SPEED_1000 | GM_GPCR_SPEED_100 | GM_GPCR_DUP_FULL;
  1758. switch (skge->flow_control) {
  1759. case FLOW_MODE_NONE:
  1760. skge_write32(hw, SK_REG(port, GMAC_CTRL), GMC_PAUSE_OFF);
  1761. reg |= GM_GPCR_FC_TX_DIS | GM_GPCR_FC_RX_DIS | GM_GPCR_AU_FCT_DIS;
  1762. break;
  1763. case FLOW_MODE_LOC_SEND:
  1764. /* disable Rx flow-control */
  1765. reg |= GM_GPCR_FC_RX_DIS | GM_GPCR_AU_FCT_DIS;
  1766. break;
  1767. case FLOW_MODE_SYMMETRIC:
  1768. case FLOW_MODE_SYM_OR_REM:
  1769. /* enable Tx & Rx flow-control */
  1770. break;
  1771. }
  1772. gma_write16(hw, port, GM_GP_CTRL, reg);
  1773. skge_read16(hw, SK_REG(port, GMAC_IRQ_SRC));
  1774. yukon_init(hw, port);
  1775. /* MIB clear */
  1776. reg = gma_read16(hw, port, GM_PHY_ADDR);
  1777. gma_write16(hw, port, GM_PHY_ADDR, reg | GM_PAR_MIB_CLR);
  1778. for (i = 0; i < GM_MIB_CNT_SIZE; i++)
  1779. gma_read16(hw, port, GM_MIB_CNT_BASE + 8*i);
  1780. gma_write16(hw, port, GM_PHY_ADDR, reg);
  1781. /* transmit control */
  1782. gma_write16(hw, port, GM_TX_CTRL, TX_COL_THR(TX_COL_DEF));
  1783. /* receive control reg: unicast + multicast + no FCS */
  1784. gma_write16(hw, port, GM_RX_CTRL,
  1785. GM_RXCR_UCF_ENA | GM_RXCR_CRC_DIS | GM_RXCR_MCF_ENA);
  1786. /* transmit flow control */
  1787. gma_write16(hw, port, GM_TX_FLOW_CTRL, 0xffff);
  1788. /* transmit parameter */
  1789. gma_write16(hw, port, GM_TX_PARAM,
  1790. TX_JAM_LEN_VAL(TX_JAM_LEN_DEF) |
  1791. TX_JAM_IPG_VAL(TX_JAM_IPG_DEF) |
  1792. TX_IPG_JAM_DATA(TX_IPG_JAM_DEF));
  1793. /* configure the Serial Mode Register */
  1794. reg = DATA_BLIND_VAL(DATA_BLIND_DEF)
  1795. | GM_SMOD_VLAN_ENA
  1796. | IPG_DATA_VAL(IPG_DATA_DEF);
  1797. if (hw->dev[port]->mtu > ETH_DATA_LEN)
  1798. reg |= GM_SMOD_JUMBO_ENA;
  1799. gma_write16(hw, port, GM_SERIAL_MODE, reg);
  1800. /* physical address: used for pause frames */
  1801. gma_set_addr(hw, port, GM_SRC_ADDR_1L, addr);
  1802. /* virtual address for data */
  1803. gma_set_addr(hw, port, GM_SRC_ADDR_2L, addr);
  1804. /* enable interrupt mask for counter overflows */
  1805. gma_write16(hw, port, GM_TX_IRQ_MSK, 0);
  1806. gma_write16(hw, port, GM_RX_IRQ_MSK, 0);
  1807. gma_write16(hw, port, GM_TR_IRQ_MSK, 0);
  1808. /* Initialize Mac Fifo */
  1809. /* Configure Rx MAC FIFO */
  1810. skge_write16(hw, SK_REG(port, RX_GMF_FL_MSK), RX_FF_FL_DEF_MSK);
  1811. reg = GMF_OPER_ON | GMF_RX_F_FL_ON;
  1812. /* disable Rx GMAC FIFO Flush for YUKON-Lite Rev. A0 only */
  1813. if (is_yukon_lite_a0(hw))
  1814. reg &= ~GMF_RX_F_FL_ON;
  1815. skge_write8(hw, SK_REG(port, RX_GMF_CTRL_T), GMF_RST_CLR);
  1816. skge_write16(hw, SK_REG(port, RX_GMF_CTRL_T), reg);
  1817. /*
  1818. * because Pause Packet Truncation in GMAC is not working
  1819. * we have to increase the Flush Threshold to 64 bytes
  1820. * in order to flush pause packets in Rx FIFO on Yukon-1
  1821. */
  1822. skge_write16(hw, SK_REG(port, RX_GMF_FL_THR), RX_GMF_FL_THR_DEF+1);
  1823. /* Configure Tx MAC FIFO */
  1824. skge_write8(hw, SK_REG(port, TX_GMF_CTRL_T), GMF_RST_CLR);
  1825. skge_write16(hw, SK_REG(port, TX_GMF_CTRL_T), GMF_OPER_ON);
  1826. }
  1827. /* Go into power down mode */
  1828. static void yukon_suspend(struct skge_hw *hw, int port)
  1829. {
  1830. u16 ctrl;
  1831. ctrl = gm_phy_read(hw, port, PHY_MARV_PHY_CTRL);
  1832. ctrl |= PHY_M_PC_POL_R_DIS;
  1833. gm_phy_write(hw, port, PHY_MARV_PHY_CTRL, ctrl);
  1834. ctrl = gm_phy_read(hw, port, PHY_MARV_CTRL);
  1835. ctrl |= PHY_CT_RESET;
  1836. gm_phy_write(hw, port, PHY_MARV_CTRL, ctrl);
  1837. /* switch IEEE compatible power down mode on */
  1838. ctrl = gm_phy_read(hw, port, PHY_MARV_CTRL);
  1839. ctrl |= PHY_CT_PDOWN;
  1840. gm_phy_write(hw, port, PHY_MARV_CTRL, ctrl);
  1841. }
  1842. static void yukon_stop(struct skge_port *skge)
  1843. {
  1844. struct skge_hw *hw = skge->hw;
  1845. int port = skge->port;
  1846. skge_write8(hw, SK_REG(port, GMAC_IRQ_MSK), 0);
  1847. yukon_reset(hw, port);
  1848. gma_write16(hw, port, GM_GP_CTRL,
  1849. gma_read16(hw, port, GM_GP_CTRL)
  1850. & ~(GM_GPCR_TX_ENA|GM_GPCR_RX_ENA));
  1851. gma_read16(hw, port, GM_GP_CTRL);
  1852. yukon_suspend(hw, port);
  1853. /* set GPHY Control reset */
  1854. skge_write8(hw, SK_REG(port, GPHY_CTRL), GPC_RST_SET);
  1855. skge_write8(hw, SK_REG(port, GMAC_CTRL), GMC_RST_SET);
  1856. }
  1857. static void yukon_get_stats(struct skge_port *skge, u64 *data)
  1858. {
  1859. struct skge_hw *hw = skge->hw;
  1860. int port = skge->port;
  1861. int i;
  1862. data[0] = (u64) gma_read32(hw, port, GM_TXO_OK_HI) << 32
  1863. | gma_read32(hw, port, GM_TXO_OK_LO);
  1864. data[1] = (u64) gma_read32(hw, port, GM_RXO_OK_HI) << 32
  1865. | gma_read32(hw, port, GM_RXO_OK_LO);
  1866. for (i = 2; i < ARRAY_SIZE(skge_stats); i++)
  1867. data[i] = gma_read32(hw, port,
  1868. skge_stats[i].gma_offset);
  1869. }
  1870. static void yukon_mac_intr(struct skge_hw *hw, int port)
  1871. {
  1872. struct net_device *dev = hw->dev[port];
  1873. struct skge_port *skge = netdev_priv(dev);
  1874. u8 status = skge_read8(hw, SK_REG(port, GMAC_IRQ_SRC));
  1875. netif_printk(skge, intr, KERN_DEBUG, skge->netdev,
  1876. "mac interrupt status 0x%x\n", status);
  1877. if (status & GM_IS_RX_FF_OR) {
  1878. ++dev->stats.rx_fifo_errors;
  1879. skge_write8(hw, SK_REG(port, RX_GMF_CTRL_T), GMF_CLI_RX_FO);
  1880. }
  1881. if (status & GM_IS_TX_FF_UR) {
  1882. ++dev->stats.tx_fifo_errors;
  1883. skge_write8(hw, SK_REG(port, TX_GMF_CTRL_T), GMF_CLI_TX_FU);
  1884. }
  1885. }
  1886. static u16 yukon_speed(const struct skge_hw *hw, u16 aux)
  1887. {
  1888. switch (aux & PHY_M_PS_SPEED_MSK) {
  1889. case PHY_M_PS_SPEED_1000:
  1890. return SPEED_1000;
  1891. case PHY_M_PS_SPEED_100:
  1892. return SPEED_100;
  1893. default:
  1894. return SPEED_10;
  1895. }
  1896. }
  1897. static void yukon_link_up(struct skge_port *skge)
  1898. {
  1899. struct skge_hw *hw = skge->hw;
  1900. int port = skge->port;
  1901. u16 reg;
  1902. /* Enable Transmit FIFO Underrun */
  1903. skge_write8(hw, SK_REG(port, GMAC_IRQ_MSK), GMAC_DEF_MSK);
  1904. reg = gma_read16(hw, port, GM_GP_CTRL);
  1905. if (skge->duplex == DUPLEX_FULL || skge->autoneg == AUTONEG_ENABLE)
  1906. reg |= GM_GPCR_DUP_FULL;
  1907. /* enable Rx/Tx */
  1908. reg |= GM_GPCR_RX_ENA | GM_GPCR_TX_ENA;
  1909. gma_write16(hw, port, GM_GP_CTRL, reg);
  1910. gm_phy_write(hw, port, PHY_MARV_INT_MASK, PHY_M_IS_DEF_MSK);
  1911. skge_link_up(skge);
  1912. }
  1913. static void yukon_link_down(struct skge_port *skge)
  1914. {
  1915. struct skge_hw *hw = skge->hw;
  1916. int port = skge->port;
  1917. u16 ctrl;
  1918. ctrl = gma_read16(hw, port, GM_GP_CTRL);
  1919. ctrl &= ~(GM_GPCR_RX_ENA | GM_GPCR_TX_ENA);
  1920. gma_write16(hw, port, GM_GP_CTRL, ctrl);
  1921. if (skge->flow_status == FLOW_STAT_REM_SEND) {
  1922. ctrl = gm_phy_read(hw, port, PHY_MARV_AUNE_ADV);
  1923. ctrl |= PHY_M_AN_ASP;
  1924. /* restore Asymmetric Pause bit */
  1925. gm_phy_write(hw, port, PHY_MARV_AUNE_ADV, ctrl);
  1926. }
  1927. skge_link_down(skge);
  1928. yukon_init(hw, port);
  1929. }
  1930. static void yukon_phy_intr(struct skge_port *skge)
  1931. {
  1932. struct skge_hw *hw = skge->hw;
  1933. int port = skge->port;
  1934. const char *reason = NULL;
  1935. u16 istatus, phystat;
  1936. istatus = gm_phy_read(hw, port, PHY_MARV_INT_STAT);
  1937. phystat = gm_phy_read(hw, port, PHY_MARV_PHY_STAT);
  1938. netif_printk(skge, intr, KERN_DEBUG, skge->netdev,
  1939. "phy interrupt status 0x%x 0x%x\n", istatus, phystat);
  1940. if (istatus & PHY_M_IS_AN_COMPL) {
  1941. if (gm_phy_read(hw, port, PHY_MARV_AUNE_LP)
  1942. & PHY_M_AN_RF) {
  1943. reason = "remote fault";
  1944. goto failed;
  1945. }
  1946. if (gm_phy_read(hw, port, PHY_MARV_1000T_STAT) & PHY_B_1000S_MSF) {
  1947. reason = "master/slave fault";
  1948. goto failed;
  1949. }
  1950. if (!(phystat & PHY_M_PS_SPDUP_RES)) {
  1951. reason = "speed/duplex";
  1952. goto failed;
  1953. }
  1954. skge->duplex = (phystat & PHY_M_PS_FULL_DUP)
  1955. ? DUPLEX_FULL : DUPLEX_HALF;
  1956. skge->speed = yukon_speed(hw, phystat);
  1957. /* We are using IEEE 802.3z/D5.0 Table 37-4 */
  1958. switch (phystat & PHY_M_PS_PAUSE_MSK) {
  1959. case PHY_M_PS_PAUSE_MSK:
  1960. skge->flow_status = FLOW_STAT_SYMMETRIC;
  1961. break;
  1962. case PHY_M_PS_RX_P_EN:
  1963. skge->flow_status = FLOW_STAT_REM_SEND;
  1964. break;
  1965. case PHY_M_PS_TX_P_EN:
  1966. skge->flow_status = FLOW_STAT_LOC_SEND;
  1967. break;
  1968. default:
  1969. skge->flow_status = FLOW_STAT_NONE;
  1970. }
  1971. if (skge->flow_status == FLOW_STAT_NONE ||
  1972. (skge->speed < SPEED_1000 && skge->duplex == DUPLEX_HALF))
  1973. skge_write8(hw, SK_REG(port, GMAC_CTRL), GMC_PAUSE_OFF);
  1974. else
  1975. skge_write8(hw, SK_REG(port, GMAC_CTRL), GMC_PAUSE_ON);
  1976. yukon_link_up(skge);
  1977. return;
  1978. }
  1979. if (istatus & PHY_M_IS_LSP_CHANGE)
  1980. skge->speed = yukon_speed(hw, phystat);
  1981. if (istatus & PHY_M_IS_DUP_CHANGE)
  1982. skge->duplex = (phystat & PHY_M_PS_FULL_DUP) ? DUPLEX_FULL : DUPLEX_HALF;
  1983. if (istatus & PHY_M_IS_LST_CHANGE) {
  1984. if (phystat & PHY_M_PS_LINK_UP)
  1985. yukon_link_up(skge);
  1986. else
  1987. yukon_link_down(skge);
  1988. }
  1989. return;
  1990. failed:
  1991. pr_err("%s: autonegotiation failed (%s)\n", skge->netdev->name, reason);
  1992. /* XXX restart autonegotiation? */
  1993. }
  1994. static void skge_phy_reset(struct skge_port *skge)
  1995. {
  1996. struct skge_hw *hw = skge->hw;
  1997. int port = skge->port;
  1998. struct net_device *dev = hw->dev[port];
  1999. netif_stop_queue(skge->netdev);
  2000. netif_carrier_off(skge->netdev);
  2001. spin_lock_bh(&hw->phy_lock);
  2002. if (is_genesis(hw)) {
  2003. genesis_reset(hw, port);
  2004. genesis_mac_init(hw, port);
  2005. } else {
  2006. yukon_reset(hw, port);
  2007. yukon_init(hw, port);
  2008. }
  2009. spin_unlock_bh(&hw->phy_lock);
  2010. skge_set_multicast(dev);
  2011. }
  2012. /* Basic MII support */
  2013. static int skge_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  2014. {
  2015. struct mii_ioctl_data *data = if_mii(ifr);
  2016. struct skge_port *skge = netdev_priv(dev);
  2017. struct skge_hw *hw = skge->hw;
  2018. int err = -EOPNOTSUPP;
  2019. if (!netif_running(dev))
  2020. return -ENODEV; /* Phy still in reset */
  2021. switch (cmd) {
  2022. case SIOCGMIIPHY:
  2023. data->phy_id = hw->phy_addr;
  2024. /* fallthru */
  2025. case SIOCGMIIREG: {
  2026. u16 val = 0;
  2027. spin_lock_bh(&hw->phy_lock);
  2028. if (is_genesis(hw))
  2029. err = __xm_phy_read(hw, skge->port, data->reg_num & 0x1f, &val);
  2030. else
  2031. err = __gm_phy_read(hw, skge->port, data->reg_num & 0x1f, &val);
  2032. spin_unlock_bh(&hw->phy_lock);
  2033. data->val_out = val;
  2034. break;
  2035. }
  2036. case SIOCSMIIREG:
  2037. spin_lock_bh(&hw->phy_lock);
  2038. if (is_genesis(hw))
  2039. err = xm_phy_write(hw, skge->port, data->reg_num & 0x1f,
  2040. data->val_in);
  2041. else
  2042. err = gm_phy_write(hw, skge->port, data->reg_num & 0x1f,
  2043. data->val_in);
  2044. spin_unlock_bh(&hw->phy_lock);
  2045. break;
  2046. }
  2047. return err;
  2048. }
  2049. static void skge_ramset(struct skge_hw *hw, u16 q, u32 start, size_t len)
  2050. {
  2051. u32 end;
  2052. start /= 8;
  2053. len /= 8;
  2054. end = start + len - 1;
  2055. skge_write8(hw, RB_ADDR(q, RB_CTRL), RB_RST_CLR);
  2056. skge_write32(hw, RB_ADDR(q, RB_START), start);
  2057. skge_write32(hw, RB_ADDR(q, RB_WP), start);
  2058. skge_write32(hw, RB_ADDR(q, RB_RP), start);
  2059. skge_write32(hw, RB_ADDR(q, RB_END), end);
  2060. if (q == Q_R1 || q == Q_R2) {
  2061. /* Set thresholds on receive queue's */
  2062. skge_write32(hw, RB_ADDR(q, RB_RX_UTPP),
  2063. start + (2*len)/3);
  2064. skge_write32(hw, RB_ADDR(q, RB_RX_LTPP),
  2065. start + (len/3));
  2066. } else {
  2067. /* Enable store & forward on Tx queue's because
  2068. * Tx FIFO is only 4K on Genesis and 1K on Yukon
  2069. */
  2070. skge_write8(hw, RB_ADDR(q, RB_CTRL), RB_ENA_STFWD);
  2071. }
  2072. skge_write8(hw, RB_ADDR(q, RB_CTRL), RB_ENA_OP_MD);
  2073. }
  2074. /* Setup Bus Memory Interface */
  2075. static void skge_qset(struct skge_port *skge, u16 q,
  2076. const struct skge_element *e)
  2077. {
  2078. struct skge_hw *hw = skge->hw;
  2079. u32 watermark = 0x600;
  2080. u64 base = skge->dma + (e->desc - skge->mem);
  2081. /* optimization to reduce window on 32bit/33mhz */
  2082. if ((skge_read16(hw, B0_CTST) & (CS_BUS_CLOCK | CS_BUS_SLOT_SZ)) == 0)
  2083. watermark /= 2;
  2084. skge_write32(hw, Q_ADDR(q, Q_CSR), CSR_CLR_RESET);
  2085. skge_write32(hw, Q_ADDR(q, Q_F), watermark);
  2086. skge_write32(hw, Q_ADDR(q, Q_DA_H), (u32)(base >> 32));
  2087. skge_write32(hw, Q_ADDR(q, Q_DA_L), (u32)base);
  2088. }
  2089. static int skge_up(struct net_device *dev)
  2090. {
  2091. struct skge_port *skge = netdev_priv(dev);
  2092. struct skge_hw *hw = skge->hw;
  2093. int port = skge->port;
  2094. u32 chunk, ram_addr;
  2095. size_t rx_size, tx_size;
  2096. int err;
  2097. if (!is_valid_ether_addr(dev->dev_addr))
  2098. return -EINVAL;
  2099. netif_info(skge, ifup, skge->netdev, "enabling interface\n");
  2100. if (dev->mtu > RX_BUF_SIZE)
  2101. skge->rx_buf_size = dev->mtu + ETH_HLEN;
  2102. else
  2103. skge->rx_buf_size = RX_BUF_SIZE;
  2104. rx_size = skge->rx_ring.count * sizeof(struct skge_rx_desc);
  2105. tx_size = skge->tx_ring.count * sizeof(struct skge_tx_desc);
  2106. skge->mem_size = tx_size + rx_size;
  2107. skge->mem = pci_alloc_consistent(hw->pdev, skge->mem_size, &skge->dma);
  2108. if (!skge->mem)
  2109. return -ENOMEM;
  2110. BUG_ON(skge->dma & 7);
  2111. if (upper_32_bits(skge->dma) != upper_32_bits(skge->dma + skge->mem_size)) {
  2112. dev_err(&hw->pdev->dev, "pci_alloc_consistent region crosses 4G boundary\n");
  2113. err = -EINVAL;
  2114. goto free_pci_mem;
  2115. }
  2116. memset(skge->mem, 0, skge->mem_size);
  2117. err = skge_ring_alloc(&skge->rx_ring, skge->mem, skge->dma);
  2118. if (err)
  2119. goto free_pci_mem;
  2120. err = skge_rx_fill(dev);
  2121. if (err)
  2122. goto free_rx_ring;
  2123. err = skge_ring_alloc(&skge->tx_ring, skge->mem + rx_size,
  2124. skge->dma + rx_size);
  2125. if (err)
  2126. goto free_rx_ring;
  2127. if (hw->ports == 1) {
  2128. err = request_irq(hw->pdev->irq, skge_intr, IRQF_SHARED,
  2129. dev->name, hw);
  2130. if (err) {
  2131. netdev_err(dev, "Unable to allocate interrupt %d error: %d\n",
  2132. hw->pdev->irq, err);
  2133. goto free_tx_ring;
  2134. }
  2135. }
  2136. /* Initialize MAC */
  2137. netif_carrier_off(dev);
  2138. spin_lock_bh(&hw->phy_lock);
  2139. if (is_genesis(hw))
  2140. genesis_mac_init(hw, port);
  2141. else
  2142. yukon_mac_init(hw, port);
  2143. spin_unlock_bh(&hw->phy_lock);
  2144. /* Configure RAMbuffers - equally between ports and tx/rx */
  2145. chunk = (hw->ram_size - hw->ram_offset) / (hw->ports * 2);
  2146. ram_addr = hw->ram_offset + 2 * chunk * port;
  2147. skge_ramset(hw, rxqaddr[port], ram_addr, chunk);
  2148. skge_qset(skge, rxqaddr[port], skge->rx_ring.to_clean);
  2149. BUG_ON(skge->tx_ring.to_use != skge->tx_ring.to_clean);
  2150. skge_ramset(hw, txqaddr[port], ram_addr+chunk, chunk);
  2151. skge_qset(skge, txqaddr[port], skge->tx_ring.to_use);
  2152. /* Start receiver BMU */
  2153. wmb();
  2154. skge_write8(hw, Q_ADDR(rxqaddr[port], Q_CSR), CSR_START | CSR_IRQ_CL_F);
  2155. skge_led(skge, LED_MODE_ON);
  2156. spin_lock_irq(&hw->hw_lock);
  2157. hw->intr_mask |= portmask[port];
  2158. skge_write32(hw, B0_IMSK, hw->intr_mask);
  2159. skge_read32(hw, B0_IMSK);
  2160. spin_unlock_irq(&hw->hw_lock);
  2161. napi_enable(&skge->napi);
  2162. skge_set_multicast(dev);
  2163. return 0;
  2164. free_tx_ring:
  2165. kfree(skge->tx_ring.start);
  2166. free_rx_ring:
  2167. skge_rx_clean(skge);
  2168. kfree(skge->rx_ring.start);
  2169. free_pci_mem:
  2170. pci_free_consistent(hw->pdev, skge->mem_size, skge->mem, skge->dma);
  2171. skge->mem = NULL;
  2172. return err;
  2173. }
  2174. /* stop receiver */
  2175. static void skge_rx_stop(struct skge_hw *hw, int port)
  2176. {
  2177. skge_write8(hw, Q_ADDR(rxqaddr[port], Q_CSR), CSR_STOP);
  2178. skge_write32(hw, RB_ADDR(port ? Q_R2 : Q_R1, RB_CTRL),
  2179. RB_RST_SET|RB_DIS_OP_MD);
  2180. skge_write32(hw, Q_ADDR(rxqaddr[port], Q_CSR), CSR_SET_RESET);
  2181. }
  2182. static int skge_down(struct net_device *dev)
  2183. {
  2184. struct skge_port *skge = netdev_priv(dev);
  2185. struct skge_hw *hw = skge->hw;
  2186. int port = skge->port;
  2187. if (skge->mem == NULL)
  2188. return 0;
  2189. netif_info(skge, ifdown, skge->netdev, "disabling interface\n");
  2190. netif_tx_disable(dev);
  2191. if (is_genesis(hw) && hw->phy_type == SK_PHY_XMAC)
  2192. del_timer_sync(&skge->link_timer);
  2193. napi_disable(&skge->napi);
  2194. netif_carrier_off(dev);
  2195. spin_lock_irq(&hw->hw_lock);
  2196. hw->intr_mask &= ~portmask[port];
  2197. skge_write32(hw, B0_IMSK, (hw->ports == 1) ? 0 : hw->intr_mask);
  2198. skge_read32(hw, B0_IMSK);
  2199. spin_unlock_irq(&hw->hw_lock);
  2200. if (hw->ports == 1)
  2201. free_irq(hw->pdev->irq, hw);
  2202. skge_write8(skge->hw, SK_REG(skge->port, LNK_LED_REG), LED_OFF);
  2203. if (is_genesis(hw))
  2204. genesis_stop(skge);
  2205. else
  2206. yukon_stop(skge);
  2207. /* Stop transmitter */
  2208. skge_write8(hw, Q_ADDR(txqaddr[port], Q_CSR), CSR_STOP);
  2209. skge_write32(hw, RB_ADDR(txqaddr[port], RB_CTRL),
  2210. RB_RST_SET|RB_DIS_OP_MD);
  2211. /* Disable Force Sync bit and Enable Alloc bit */
  2212. skge_write8(hw, SK_REG(port, TXA_CTRL),
  2213. TXA_DIS_FSYNC | TXA_DIS_ALLOC | TXA_STOP_RC);
  2214. /* Stop Interval Timer and Limit Counter of Tx Arbiter */
  2215. skge_write32(hw, SK_REG(port, TXA_ITI_INI), 0L);
  2216. skge_write32(hw, SK_REG(port, TXA_LIM_INI), 0L);
  2217. /* Reset PCI FIFO */
  2218. skge_write32(hw, Q_ADDR(txqaddr[port], Q_CSR), CSR_SET_RESET);
  2219. skge_write32(hw, RB_ADDR(txqaddr[port], RB_CTRL), RB_RST_SET);
  2220. /* Reset the RAM Buffer async Tx queue */
  2221. skge_write8(hw, RB_ADDR(port == 0 ? Q_XA1 : Q_XA2, RB_CTRL), RB_RST_SET);
  2222. skge_rx_stop(hw, port);
  2223. if (is_genesis(hw)) {
  2224. skge_write8(hw, SK_REG(port, TX_MFF_CTRL2), MFF_RST_SET);
  2225. skge_write8(hw, SK_REG(port, RX_MFF_CTRL2), MFF_RST_SET);
  2226. } else {
  2227. skge_write8(hw, SK_REG(port, RX_GMF_CTRL_T), GMF_RST_SET);
  2228. skge_write8(hw, SK_REG(port, TX_GMF_CTRL_T), GMF_RST_SET);
  2229. }
  2230. skge_led(skge, LED_MODE_OFF);
  2231. netif_tx_lock_bh(dev);
  2232. skge_tx_clean(dev);
  2233. netif_tx_unlock_bh(dev);
  2234. skge_rx_clean(skge);
  2235. kfree(skge->rx_ring.start);
  2236. kfree(skge->tx_ring.start);
  2237. pci_free_consistent(hw->pdev, skge->mem_size, skge->mem, skge->dma);
  2238. skge->mem = NULL;
  2239. return 0;
  2240. }
  2241. static inline int skge_avail(const struct skge_ring *ring)
  2242. {
  2243. smp_mb();
  2244. return ((ring->to_clean > ring->to_use) ? 0 : ring->count)
  2245. + (ring->to_clean - ring->to_use) - 1;
  2246. }
  2247. static netdev_tx_t skge_xmit_frame(struct sk_buff *skb,
  2248. struct net_device *dev)
  2249. {
  2250. struct skge_port *skge = netdev_priv(dev);
  2251. struct skge_hw *hw = skge->hw;
  2252. struct skge_element *e;
  2253. struct skge_tx_desc *td;
  2254. int i;
  2255. u32 control, len;
  2256. dma_addr_t map;
  2257. if (skb_padto(skb, ETH_ZLEN))
  2258. return NETDEV_TX_OK;
  2259. if (unlikely(skge_avail(&skge->tx_ring) < skb_shinfo(skb)->nr_frags + 1))
  2260. return NETDEV_TX_BUSY;
  2261. e = skge->tx_ring.to_use;
  2262. td = e->desc;
  2263. BUG_ON(td->control & BMU_OWN);
  2264. e->skb = skb;
  2265. len = skb_headlen(skb);
  2266. map = pci_map_single(hw->pdev, skb->data, len, PCI_DMA_TODEVICE);
  2267. if (pci_dma_mapping_error(hw->pdev, map))
  2268. goto mapping_error;
  2269. dma_unmap_addr_set(e, mapaddr, map);
  2270. dma_unmap_len_set(e, maplen, len);
  2271. td->dma_lo = lower_32_bits(map);
  2272. td->dma_hi = upper_32_bits(map);
  2273. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  2274. const int offset = skb_checksum_start_offset(skb);
  2275. /* This seems backwards, but it is what the sk98lin
  2276. * does. Looks like hardware is wrong?
  2277. */
  2278. if (ipip_hdr(skb)->protocol == IPPROTO_UDP &&
  2279. hw->chip_rev == 0 && hw->chip_id == CHIP_ID_YUKON)
  2280. control = BMU_TCP_CHECK;
  2281. else
  2282. control = BMU_UDP_CHECK;
  2283. td->csum_offs = 0;
  2284. td->csum_start = offset;
  2285. td->csum_write = offset + skb->csum_offset;
  2286. } else
  2287. control = BMU_CHECK;
  2288. if (!skb_shinfo(skb)->nr_frags) /* single buffer i.e. no fragments */
  2289. control |= BMU_EOF | BMU_IRQ_EOF;
  2290. else {
  2291. struct skge_tx_desc *tf = td;
  2292. control |= BMU_STFWD;
  2293. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  2294. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2295. map = skb_frag_dma_map(&hw->pdev->dev, frag, 0,
  2296. skb_frag_size(frag), DMA_TO_DEVICE);
  2297. if (dma_mapping_error(&hw->pdev->dev, map))
  2298. goto mapping_unwind;
  2299. e = e->next;
  2300. e->skb = skb;
  2301. tf = e->desc;
  2302. BUG_ON(tf->control & BMU_OWN);
  2303. tf->dma_lo = lower_32_bits(map);
  2304. tf->dma_hi = upper_32_bits(map);
  2305. dma_unmap_addr_set(e, mapaddr, map);
  2306. dma_unmap_len_set(e, maplen, skb_frag_size(frag));
  2307. tf->control = BMU_OWN | BMU_SW | control | skb_frag_size(frag);
  2308. }
  2309. tf->control |= BMU_EOF | BMU_IRQ_EOF;
  2310. }
  2311. /* Make sure all the descriptors written */
  2312. wmb();
  2313. td->control = BMU_OWN | BMU_SW | BMU_STF | control | len;
  2314. wmb();
  2315. netdev_sent_queue(dev, skb->len);
  2316. skge_write8(hw, Q_ADDR(txqaddr[skge->port], Q_CSR), CSR_START);
  2317. netif_printk(skge, tx_queued, KERN_DEBUG, skge->netdev,
  2318. "tx queued, slot %td, len %d\n",
  2319. e - skge->tx_ring.start, skb->len);
  2320. skge->tx_ring.to_use = e->next;
  2321. smp_wmb();
  2322. if (skge_avail(&skge->tx_ring) <= TX_LOW_WATER) {
  2323. netdev_dbg(dev, "transmit queue full\n");
  2324. netif_stop_queue(dev);
  2325. }
  2326. return NETDEV_TX_OK;
  2327. mapping_unwind:
  2328. e = skge->tx_ring.to_use;
  2329. pci_unmap_single(hw->pdev,
  2330. dma_unmap_addr(e, mapaddr),
  2331. dma_unmap_len(e, maplen),
  2332. PCI_DMA_TODEVICE);
  2333. while (i-- > 0) {
  2334. e = e->next;
  2335. pci_unmap_page(hw->pdev,
  2336. dma_unmap_addr(e, mapaddr),
  2337. dma_unmap_len(e, maplen),
  2338. PCI_DMA_TODEVICE);
  2339. }
  2340. mapping_error:
  2341. if (net_ratelimit())
  2342. dev_warn(&hw->pdev->dev, "%s: tx mapping error\n", dev->name);
  2343. dev_kfree_skb_any(skb);
  2344. return NETDEV_TX_OK;
  2345. }
  2346. /* Free resources associated with this reing element */
  2347. static inline void skge_tx_unmap(struct pci_dev *pdev, struct skge_element *e,
  2348. u32 control)
  2349. {
  2350. /* skb header vs. fragment */
  2351. if (control & BMU_STF)
  2352. pci_unmap_single(pdev, dma_unmap_addr(e, mapaddr),
  2353. dma_unmap_len(e, maplen),
  2354. PCI_DMA_TODEVICE);
  2355. else
  2356. pci_unmap_page(pdev, dma_unmap_addr(e, mapaddr),
  2357. dma_unmap_len(e, maplen),
  2358. PCI_DMA_TODEVICE);
  2359. }
  2360. /* Free all buffers in transmit ring */
  2361. static void skge_tx_clean(struct net_device *dev)
  2362. {
  2363. struct skge_port *skge = netdev_priv(dev);
  2364. struct skge_element *e;
  2365. for (e = skge->tx_ring.to_clean; e != skge->tx_ring.to_use; e = e->next) {
  2366. struct skge_tx_desc *td = e->desc;
  2367. skge_tx_unmap(skge->hw->pdev, e, td->control);
  2368. if (td->control & BMU_EOF)
  2369. dev_kfree_skb(e->skb);
  2370. td->control = 0;
  2371. }
  2372. netdev_reset_queue(dev);
  2373. skge->tx_ring.to_clean = e;
  2374. }
  2375. static void skge_tx_timeout(struct net_device *dev)
  2376. {
  2377. struct skge_port *skge = netdev_priv(dev);
  2378. netif_printk(skge, timer, KERN_DEBUG, skge->netdev, "tx timeout\n");
  2379. skge_write8(skge->hw, Q_ADDR(txqaddr[skge->port], Q_CSR), CSR_STOP);
  2380. skge_tx_clean(dev);
  2381. netif_wake_queue(dev);
  2382. }
  2383. static int skge_change_mtu(struct net_device *dev, int new_mtu)
  2384. {
  2385. int err;
  2386. if (new_mtu < ETH_ZLEN || new_mtu > ETH_JUMBO_MTU)
  2387. return -EINVAL;
  2388. if (!netif_running(dev)) {
  2389. dev->mtu = new_mtu;
  2390. return 0;
  2391. }
  2392. skge_down(dev);
  2393. dev->mtu = new_mtu;
  2394. err = skge_up(dev);
  2395. if (err)
  2396. dev_close(dev);
  2397. return err;
  2398. }
  2399. static const u8 pause_mc_addr[ETH_ALEN] = { 0x1, 0x80, 0xc2, 0x0, 0x0, 0x1 };
  2400. static void genesis_add_filter(u8 filter[8], const u8 *addr)
  2401. {
  2402. u32 crc, bit;
  2403. crc = ether_crc_le(ETH_ALEN, addr);
  2404. bit = ~crc & 0x3f;
  2405. filter[bit/8] |= 1 << (bit%8);
  2406. }
  2407. static void genesis_set_multicast(struct net_device *dev)
  2408. {
  2409. struct skge_port *skge = netdev_priv(dev);
  2410. struct skge_hw *hw = skge->hw;
  2411. int port = skge->port;
  2412. struct netdev_hw_addr *ha;
  2413. u32 mode;
  2414. u8 filter[8];
  2415. mode = xm_read32(hw, port, XM_MODE);
  2416. mode |= XM_MD_ENA_HASH;
  2417. if (dev->flags & IFF_PROMISC)
  2418. mode |= XM_MD_ENA_PROM;
  2419. else
  2420. mode &= ~XM_MD_ENA_PROM;
  2421. if (dev->flags & IFF_ALLMULTI)
  2422. memset(filter, 0xff, sizeof(filter));
  2423. else {
  2424. memset(filter, 0, sizeof(filter));
  2425. if (skge->flow_status == FLOW_STAT_REM_SEND ||
  2426. skge->flow_status == FLOW_STAT_SYMMETRIC)
  2427. genesis_add_filter(filter, pause_mc_addr);
  2428. netdev_for_each_mc_addr(ha, dev)
  2429. genesis_add_filter(filter, ha->addr);
  2430. }
  2431. xm_write32(hw, port, XM_MODE, mode);
  2432. xm_outhash(hw, port, XM_HSM, filter);
  2433. }
  2434. static void yukon_add_filter(u8 filter[8], const u8 *addr)
  2435. {
  2436. u32 bit = ether_crc(ETH_ALEN, addr) & 0x3f;
  2437. filter[bit/8] |= 1 << (bit%8);
  2438. }
  2439. static void yukon_set_multicast(struct net_device *dev)
  2440. {
  2441. struct skge_port *skge = netdev_priv(dev);
  2442. struct skge_hw *hw = skge->hw;
  2443. int port = skge->port;
  2444. struct netdev_hw_addr *ha;
  2445. int rx_pause = (skge->flow_status == FLOW_STAT_REM_SEND ||
  2446. skge->flow_status == FLOW_STAT_SYMMETRIC);
  2447. u16 reg;
  2448. u8 filter[8];
  2449. memset(filter, 0, sizeof(filter));
  2450. reg = gma_read16(hw, port, GM_RX_CTRL);
  2451. reg |= GM_RXCR_UCF_ENA;
  2452. if (dev->flags & IFF_PROMISC) /* promiscuous */
  2453. reg &= ~(GM_RXCR_UCF_ENA | GM_RXCR_MCF_ENA);
  2454. else if (dev->flags & IFF_ALLMULTI) /* all multicast */
  2455. memset(filter, 0xff, sizeof(filter));
  2456. else if (netdev_mc_empty(dev) && !rx_pause)/* no multicast */
  2457. reg &= ~GM_RXCR_MCF_ENA;
  2458. else {
  2459. reg |= GM_RXCR_MCF_ENA;
  2460. if (rx_pause)
  2461. yukon_add_filter(filter, pause_mc_addr);
  2462. netdev_for_each_mc_addr(ha, dev)
  2463. yukon_add_filter(filter, ha->addr);
  2464. }
  2465. gma_write16(hw, port, GM_MC_ADDR_H1,
  2466. (u16)filter[0] | ((u16)filter[1] << 8));
  2467. gma_write16(hw, port, GM_MC_ADDR_H2,
  2468. (u16)filter[2] | ((u16)filter[3] << 8));
  2469. gma_write16(hw, port, GM_MC_ADDR_H3,
  2470. (u16)filter[4] | ((u16)filter[5] << 8));
  2471. gma_write16(hw, port, GM_MC_ADDR_H4,
  2472. (u16)filter[6] | ((u16)filter[7] << 8));
  2473. gma_write16(hw, port, GM_RX_CTRL, reg);
  2474. }
  2475. static inline u16 phy_length(const struct skge_hw *hw, u32 status)
  2476. {
  2477. if (is_genesis(hw))
  2478. return status >> XMR_FS_LEN_SHIFT;
  2479. else
  2480. return status >> GMR_FS_LEN_SHIFT;
  2481. }
  2482. static inline int bad_phy_status(const struct skge_hw *hw, u32 status)
  2483. {
  2484. if (is_genesis(hw))
  2485. return (status & (XMR_FS_ERR | XMR_FS_2L_VLAN)) != 0;
  2486. else
  2487. return (status & GMR_FS_ANY_ERR) ||
  2488. (status & GMR_FS_RX_OK) == 0;
  2489. }
  2490. static void skge_set_multicast(struct net_device *dev)
  2491. {
  2492. struct skge_port *skge = netdev_priv(dev);
  2493. if (is_genesis(skge->hw))
  2494. genesis_set_multicast(dev);
  2495. else
  2496. yukon_set_multicast(dev);
  2497. }
  2498. /* Get receive buffer from descriptor.
  2499. * Handles copy of small buffers and reallocation failures
  2500. */
  2501. static struct sk_buff *skge_rx_get(struct net_device *dev,
  2502. struct skge_element *e,
  2503. u32 control, u32 status, u16 csum)
  2504. {
  2505. struct skge_port *skge = netdev_priv(dev);
  2506. struct sk_buff *skb;
  2507. u16 len = control & BMU_BBC;
  2508. netif_printk(skge, rx_status, KERN_DEBUG, skge->netdev,
  2509. "rx slot %td status 0x%x len %d\n",
  2510. e - skge->rx_ring.start, status, len);
  2511. if (len > skge->rx_buf_size)
  2512. goto error;
  2513. if ((control & (BMU_EOF|BMU_STF)) != (BMU_STF|BMU_EOF))
  2514. goto error;
  2515. if (bad_phy_status(skge->hw, status))
  2516. goto error;
  2517. if (phy_length(skge->hw, status) != len)
  2518. goto error;
  2519. if (len < RX_COPY_THRESHOLD) {
  2520. skb = netdev_alloc_skb_ip_align(dev, len);
  2521. if (!skb)
  2522. goto resubmit;
  2523. pci_dma_sync_single_for_cpu(skge->hw->pdev,
  2524. dma_unmap_addr(e, mapaddr),
  2525. dma_unmap_len(e, maplen),
  2526. PCI_DMA_FROMDEVICE);
  2527. skb_copy_from_linear_data(e->skb, skb->data, len);
  2528. pci_dma_sync_single_for_device(skge->hw->pdev,
  2529. dma_unmap_addr(e, mapaddr),
  2530. dma_unmap_len(e, maplen),
  2531. PCI_DMA_FROMDEVICE);
  2532. skge_rx_reuse(e, skge->rx_buf_size);
  2533. } else {
  2534. struct skge_element ee;
  2535. struct sk_buff *nskb;
  2536. nskb = netdev_alloc_skb_ip_align(dev, skge->rx_buf_size);
  2537. if (!nskb)
  2538. goto resubmit;
  2539. ee = *e;
  2540. skb = ee.skb;
  2541. prefetch(skb->data);
  2542. if (skge_rx_setup(skge, e, nskb, skge->rx_buf_size) < 0) {
  2543. dev_kfree_skb(nskb);
  2544. goto resubmit;
  2545. }
  2546. pci_unmap_single(skge->hw->pdev,
  2547. dma_unmap_addr(&ee, mapaddr),
  2548. dma_unmap_len(&ee, maplen),
  2549. PCI_DMA_FROMDEVICE);
  2550. }
  2551. skb_put(skb, len);
  2552. if (dev->features & NETIF_F_RXCSUM) {
  2553. skb->csum = csum;
  2554. skb->ip_summed = CHECKSUM_COMPLETE;
  2555. }
  2556. skb->protocol = eth_type_trans(skb, dev);
  2557. return skb;
  2558. error:
  2559. netif_printk(skge, rx_err, KERN_DEBUG, skge->netdev,
  2560. "rx err, slot %td control 0x%x status 0x%x\n",
  2561. e - skge->rx_ring.start, control, status);
  2562. if (is_genesis(skge->hw)) {
  2563. if (status & (XMR_FS_RUNT|XMR_FS_LNG_ERR))
  2564. dev->stats.rx_length_errors++;
  2565. if (status & XMR_FS_FRA_ERR)
  2566. dev->stats.rx_frame_errors++;
  2567. if (status & XMR_FS_FCS_ERR)
  2568. dev->stats.rx_crc_errors++;
  2569. } else {
  2570. if (status & (GMR_FS_LONG_ERR|GMR_FS_UN_SIZE))
  2571. dev->stats.rx_length_errors++;
  2572. if (status & GMR_FS_FRAGMENT)
  2573. dev->stats.rx_frame_errors++;
  2574. if (status & GMR_FS_CRC_ERR)
  2575. dev->stats.rx_crc_errors++;
  2576. }
  2577. resubmit:
  2578. skge_rx_reuse(e, skge->rx_buf_size);
  2579. return NULL;
  2580. }
  2581. /* Free all buffers in Tx ring which are no longer owned by device */
  2582. static void skge_tx_done(struct net_device *dev)
  2583. {
  2584. struct skge_port *skge = netdev_priv(dev);
  2585. struct skge_ring *ring = &skge->tx_ring;
  2586. struct skge_element *e;
  2587. unsigned int bytes_compl = 0, pkts_compl = 0;
  2588. skge_write8(skge->hw, Q_ADDR(txqaddr[skge->port], Q_CSR), CSR_IRQ_CL_F);
  2589. for (e = ring->to_clean; e != ring->to_use; e = e->next) {
  2590. u32 control = ((const struct skge_tx_desc *) e->desc)->control;
  2591. if (control & BMU_OWN)
  2592. break;
  2593. skge_tx_unmap(skge->hw->pdev, e, control);
  2594. if (control & BMU_EOF) {
  2595. netif_printk(skge, tx_done, KERN_DEBUG, skge->netdev,
  2596. "tx done slot %td\n",
  2597. e - skge->tx_ring.start);
  2598. pkts_compl++;
  2599. bytes_compl += e->skb->len;
  2600. dev_consume_skb_any(e->skb);
  2601. }
  2602. }
  2603. netdev_completed_queue(dev, pkts_compl, bytes_compl);
  2604. skge->tx_ring.to_clean = e;
  2605. /* Can run lockless until we need to synchronize to restart queue. */
  2606. smp_mb();
  2607. if (unlikely(netif_queue_stopped(dev) &&
  2608. skge_avail(&skge->tx_ring) > TX_LOW_WATER)) {
  2609. netif_tx_lock(dev);
  2610. if (unlikely(netif_queue_stopped(dev) &&
  2611. skge_avail(&skge->tx_ring) > TX_LOW_WATER)) {
  2612. netif_wake_queue(dev);
  2613. }
  2614. netif_tx_unlock(dev);
  2615. }
  2616. }
  2617. static int skge_poll(struct napi_struct *napi, int to_do)
  2618. {
  2619. struct skge_port *skge = container_of(napi, struct skge_port, napi);
  2620. struct net_device *dev = skge->netdev;
  2621. struct skge_hw *hw = skge->hw;
  2622. struct skge_ring *ring = &skge->rx_ring;
  2623. struct skge_element *e;
  2624. int work_done = 0;
  2625. skge_tx_done(dev);
  2626. skge_write8(hw, Q_ADDR(rxqaddr[skge->port], Q_CSR), CSR_IRQ_CL_F);
  2627. for (e = ring->to_clean; prefetch(e->next), work_done < to_do; e = e->next) {
  2628. struct skge_rx_desc *rd = e->desc;
  2629. struct sk_buff *skb;
  2630. u32 control;
  2631. rmb();
  2632. control = rd->control;
  2633. if (control & BMU_OWN)
  2634. break;
  2635. skb = skge_rx_get(dev, e, control, rd->status, rd->csum2);
  2636. if (likely(skb)) {
  2637. napi_gro_receive(napi, skb);
  2638. ++work_done;
  2639. }
  2640. }
  2641. ring->to_clean = e;
  2642. /* restart receiver */
  2643. wmb();
  2644. skge_write8(hw, Q_ADDR(rxqaddr[skge->port], Q_CSR), CSR_START);
  2645. if (work_done < to_do) {
  2646. unsigned long flags;
  2647. napi_gro_flush(napi, false);
  2648. spin_lock_irqsave(&hw->hw_lock, flags);
  2649. __napi_complete(napi);
  2650. hw->intr_mask |= napimask[skge->port];
  2651. skge_write32(hw, B0_IMSK, hw->intr_mask);
  2652. skge_read32(hw, B0_IMSK);
  2653. spin_unlock_irqrestore(&hw->hw_lock, flags);
  2654. }
  2655. return work_done;
  2656. }
  2657. /* Parity errors seem to happen when Genesis is connected to a switch
  2658. * with no other ports present. Heartbeat error??
  2659. */
  2660. static void skge_mac_parity(struct skge_hw *hw, int port)
  2661. {
  2662. struct net_device *dev = hw->dev[port];
  2663. ++dev->stats.tx_heartbeat_errors;
  2664. if (is_genesis(hw))
  2665. skge_write16(hw, SK_REG(port, TX_MFF_CTRL1),
  2666. MFF_CLR_PERR);
  2667. else
  2668. /* HW-Bug #8: cleared by GMF_CLI_TX_FC instead of GMF_CLI_TX_PE */
  2669. skge_write8(hw, SK_REG(port, TX_GMF_CTRL_T),
  2670. (hw->chip_id == CHIP_ID_YUKON && hw->chip_rev == 0)
  2671. ? GMF_CLI_TX_FC : GMF_CLI_TX_PE);
  2672. }
  2673. static void skge_mac_intr(struct skge_hw *hw, int port)
  2674. {
  2675. if (is_genesis(hw))
  2676. genesis_mac_intr(hw, port);
  2677. else
  2678. yukon_mac_intr(hw, port);
  2679. }
  2680. /* Handle device specific framing and timeout interrupts */
  2681. static void skge_error_irq(struct skge_hw *hw)
  2682. {
  2683. struct pci_dev *pdev = hw->pdev;
  2684. u32 hwstatus = skge_read32(hw, B0_HWE_ISRC);
  2685. if (is_genesis(hw)) {
  2686. /* clear xmac errors */
  2687. if (hwstatus & (IS_NO_STAT_M1|IS_NO_TIST_M1))
  2688. skge_write16(hw, RX_MFF_CTRL1, MFF_CLR_INSTAT);
  2689. if (hwstatus & (IS_NO_STAT_M2|IS_NO_TIST_M2))
  2690. skge_write16(hw, RX_MFF_CTRL2, MFF_CLR_INSTAT);
  2691. } else {
  2692. /* Timestamp (unused) overflow */
  2693. if (hwstatus & IS_IRQ_TIST_OV)
  2694. skge_write8(hw, GMAC_TI_ST_CTRL, GMT_ST_CLR_IRQ);
  2695. }
  2696. if (hwstatus & IS_RAM_RD_PAR) {
  2697. dev_err(&pdev->dev, "Ram read data parity error\n");
  2698. skge_write16(hw, B3_RI_CTRL, RI_CLR_RD_PERR);
  2699. }
  2700. if (hwstatus & IS_RAM_WR_PAR) {
  2701. dev_err(&pdev->dev, "Ram write data parity error\n");
  2702. skge_write16(hw, B3_RI_CTRL, RI_CLR_WR_PERR);
  2703. }
  2704. if (hwstatus & IS_M1_PAR_ERR)
  2705. skge_mac_parity(hw, 0);
  2706. if (hwstatus & IS_M2_PAR_ERR)
  2707. skge_mac_parity(hw, 1);
  2708. if (hwstatus & IS_R1_PAR_ERR) {
  2709. dev_err(&pdev->dev, "%s: receive queue parity error\n",
  2710. hw->dev[0]->name);
  2711. skge_write32(hw, B0_R1_CSR, CSR_IRQ_CL_P);
  2712. }
  2713. if (hwstatus & IS_R2_PAR_ERR) {
  2714. dev_err(&pdev->dev, "%s: receive queue parity error\n",
  2715. hw->dev[1]->name);
  2716. skge_write32(hw, B0_R2_CSR, CSR_IRQ_CL_P);
  2717. }
  2718. if (hwstatus & (IS_IRQ_MST_ERR|IS_IRQ_STAT)) {
  2719. u16 pci_status, pci_cmd;
  2720. pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
  2721. pci_read_config_word(pdev, PCI_STATUS, &pci_status);
  2722. dev_err(&pdev->dev, "PCI error cmd=%#x status=%#x\n",
  2723. pci_cmd, pci_status);
  2724. /* Write the error bits back to clear them. */
  2725. pci_status &= PCI_STATUS_ERROR_BITS;
  2726. skge_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_ON);
  2727. pci_write_config_word(pdev, PCI_COMMAND,
  2728. pci_cmd | PCI_COMMAND_SERR | PCI_COMMAND_PARITY);
  2729. pci_write_config_word(pdev, PCI_STATUS, pci_status);
  2730. skge_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_OFF);
  2731. /* if error still set then just ignore it */
  2732. hwstatus = skge_read32(hw, B0_HWE_ISRC);
  2733. if (hwstatus & IS_IRQ_STAT) {
  2734. dev_warn(&hw->pdev->dev, "unable to clear error (so ignoring them)\n");
  2735. hw->intr_mask &= ~IS_HW_ERR;
  2736. }
  2737. }
  2738. }
  2739. /*
  2740. * Interrupt from PHY are handled in tasklet (softirq)
  2741. * because accessing phy registers requires spin wait which might
  2742. * cause excess interrupt latency.
  2743. */
  2744. static void skge_extirq(unsigned long arg)
  2745. {
  2746. struct skge_hw *hw = (struct skge_hw *) arg;
  2747. int port;
  2748. for (port = 0; port < hw->ports; port++) {
  2749. struct net_device *dev = hw->dev[port];
  2750. if (netif_running(dev)) {
  2751. struct skge_port *skge = netdev_priv(dev);
  2752. spin_lock(&hw->phy_lock);
  2753. if (!is_genesis(hw))
  2754. yukon_phy_intr(skge);
  2755. else if (hw->phy_type == SK_PHY_BCOM)
  2756. bcom_phy_intr(skge);
  2757. spin_unlock(&hw->phy_lock);
  2758. }
  2759. }
  2760. spin_lock_irq(&hw->hw_lock);
  2761. hw->intr_mask |= IS_EXT_REG;
  2762. skge_write32(hw, B0_IMSK, hw->intr_mask);
  2763. skge_read32(hw, B0_IMSK);
  2764. spin_unlock_irq(&hw->hw_lock);
  2765. }
  2766. static irqreturn_t skge_intr(int irq, void *dev_id)
  2767. {
  2768. struct skge_hw *hw = dev_id;
  2769. u32 status;
  2770. int handled = 0;
  2771. spin_lock(&hw->hw_lock);
  2772. /* Reading this register masks IRQ */
  2773. status = skge_read32(hw, B0_SP_ISRC);
  2774. if (status == 0 || status == ~0)
  2775. goto out;
  2776. handled = 1;
  2777. status &= hw->intr_mask;
  2778. if (status & IS_EXT_REG) {
  2779. hw->intr_mask &= ~IS_EXT_REG;
  2780. tasklet_schedule(&hw->phy_task);
  2781. }
  2782. if (status & (IS_XA1_F|IS_R1_F)) {
  2783. struct skge_port *skge = netdev_priv(hw->dev[0]);
  2784. hw->intr_mask &= ~(IS_XA1_F|IS_R1_F);
  2785. napi_schedule(&skge->napi);
  2786. }
  2787. if (status & IS_PA_TO_TX1)
  2788. skge_write16(hw, B3_PA_CTRL, PA_CLR_TO_TX1);
  2789. if (status & IS_PA_TO_RX1) {
  2790. ++hw->dev[0]->stats.rx_over_errors;
  2791. skge_write16(hw, B3_PA_CTRL, PA_CLR_TO_RX1);
  2792. }
  2793. if (status & IS_MAC1)
  2794. skge_mac_intr(hw, 0);
  2795. if (hw->dev[1]) {
  2796. struct skge_port *skge = netdev_priv(hw->dev[1]);
  2797. if (status & (IS_XA2_F|IS_R2_F)) {
  2798. hw->intr_mask &= ~(IS_XA2_F|IS_R2_F);
  2799. napi_schedule(&skge->napi);
  2800. }
  2801. if (status & IS_PA_TO_RX2) {
  2802. ++hw->dev[1]->stats.rx_over_errors;
  2803. skge_write16(hw, B3_PA_CTRL, PA_CLR_TO_RX2);
  2804. }
  2805. if (status & IS_PA_TO_TX2)
  2806. skge_write16(hw, B3_PA_CTRL, PA_CLR_TO_TX2);
  2807. if (status & IS_MAC2)
  2808. skge_mac_intr(hw, 1);
  2809. }
  2810. if (status & IS_HW_ERR)
  2811. skge_error_irq(hw);
  2812. out:
  2813. skge_write32(hw, B0_IMSK, hw->intr_mask);
  2814. skge_read32(hw, B0_IMSK);
  2815. spin_unlock(&hw->hw_lock);
  2816. return IRQ_RETVAL(handled);
  2817. }
  2818. #ifdef CONFIG_NET_POLL_CONTROLLER
  2819. static void skge_netpoll(struct net_device *dev)
  2820. {
  2821. struct skge_port *skge = netdev_priv(dev);
  2822. disable_irq(dev->irq);
  2823. skge_intr(dev->irq, skge->hw);
  2824. enable_irq(dev->irq);
  2825. }
  2826. #endif
  2827. static int skge_set_mac_address(struct net_device *dev, void *p)
  2828. {
  2829. struct skge_port *skge = netdev_priv(dev);
  2830. struct skge_hw *hw = skge->hw;
  2831. unsigned port = skge->port;
  2832. const struct sockaddr *addr = p;
  2833. u16 ctrl;
  2834. if (!is_valid_ether_addr(addr->sa_data))
  2835. return -EADDRNOTAVAIL;
  2836. memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
  2837. if (!netif_running(dev)) {
  2838. memcpy_toio(hw->regs + B2_MAC_1 + port*8, dev->dev_addr, ETH_ALEN);
  2839. memcpy_toio(hw->regs + B2_MAC_2 + port*8, dev->dev_addr, ETH_ALEN);
  2840. } else {
  2841. /* disable Rx */
  2842. spin_lock_bh(&hw->phy_lock);
  2843. ctrl = gma_read16(hw, port, GM_GP_CTRL);
  2844. gma_write16(hw, port, GM_GP_CTRL, ctrl & ~GM_GPCR_RX_ENA);
  2845. memcpy_toio(hw->regs + B2_MAC_1 + port*8, dev->dev_addr, ETH_ALEN);
  2846. memcpy_toio(hw->regs + B2_MAC_2 + port*8, dev->dev_addr, ETH_ALEN);
  2847. if (is_genesis(hw))
  2848. xm_outaddr(hw, port, XM_SA, dev->dev_addr);
  2849. else {
  2850. gma_set_addr(hw, port, GM_SRC_ADDR_1L, dev->dev_addr);
  2851. gma_set_addr(hw, port, GM_SRC_ADDR_2L, dev->dev_addr);
  2852. }
  2853. gma_write16(hw, port, GM_GP_CTRL, ctrl);
  2854. spin_unlock_bh(&hw->phy_lock);
  2855. }
  2856. return 0;
  2857. }
  2858. static const struct {
  2859. u8 id;
  2860. const char *name;
  2861. } skge_chips[] = {
  2862. { CHIP_ID_GENESIS, "Genesis" },
  2863. { CHIP_ID_YUKON, "Yukon" },
  2864. { CHIP_ID_YUKON_LITE, "Yukon-Lite"},
  2865. { CHIP_ID_YUKON_LP, "Yukon-LP"},
  2866. };
  2867. static const char *skge_board_name(const struct skge_hw *hw)
  2868. {
  2869. int i;
  2870. static char buf[16];
  2871. for (i = 0; i < ARRAY_SIZE(skge_chips); i++)
  2872. if (skge_chips[i].id == hw->chip_id)
  2873. return skge_chips[i].name;
  2874. snprintf(buf, sizeof buf, "chipid 0x%x", hw->chip_id);
  2875. return buf;
  2876. }
  2877. /*
  2878. * Setup the board data structure, but don't bring up
  2879. * the port(s)
  2880. */
  2881. static int skge_reset(struct skge_hw *hw)
  2882. {
  2883. u32 reg;
  2884. u16 ctst, pci_status;
  2885. u8 t8, mac_cfg, pmd_type;
  2886. int i;
  2887. ctst = skge_read16(hw, B0_CTST);
  2888. /* do a SW reset */
  2889. skge_write8(hw, B0_CTST, CS_RST_SET);
  2890. skge_write8(hw, B0_CTST, CS_RST_CLR);
  2891. /* clear PCI errors, if any */
  2892. skge_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_ON);
  2893. skge_write8(hw, B2_TST_CTRL2, 0);
  2894. pci_read_config_word(hw->pdev, PCI_STATUS, &pci_status);
  2895. pci_write_config_word(hw->pdev, PCI_STATUS,
  2896. pci_status | PCI_STATUS_ERROR_BITS);
  2897. skge_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_OFF);
  2898. skge_write8(hw, B0_CTST, CS_MRST_CLR);
  2899. /* restore CLK_RUN bits (for Yukon-Lite) */
  2900. skge_write16(hw, B0_CTST,
  2901. ctst & (CS_CLK_RUN_HOT|CS_CLK_RUN_RST|CS_CLK_RUN_ENA));
  2902. hw->chip_id = skge_read8(hw, B2_CHIP_ID);
  2903. hw->phy_type = skge_read8(hw, B2_E_1) & 0xf;
  2904. pmd_type = skge_read8(hw, B2_PMD_TYP);
  2905. hw->copper = (pmd_type == 'T' || pmd_type == '1');
  2906. switch (hw->chip_id) {
  2907. case CHIP_ID_GENESIS:
  2908. #ifdef CONFIG_SKGE_GENESIS
  2909. switch (hw->phy_type) {
  2910. case SK_PHY_XMAC:
  2911. hw->phy_addr = PHY_ADDR_XMAC;
  2912. break;
  2913. case SK_PHY_BCOM:
  2914. hw->phy_addr = PHY_ADDR_BCOM;
  2915. break;
  2916. default:
  2917. dev_err(&hw->pdev->dev, "unsupported phy type 0x%x\n",
  2918. hw->phy_type);
  2919. return -EOPNOTSUPP;
  2920. }
  2921. break;
  2922. #else
  2923. dev_err(&hw->pdev->dev, "Genesis chip detected but not configured\n");
  2924. return -EOPNOTSUPP;
  2925. #endif
  2926. case CHIP_ID_YUKON:
  2927. case CHIP_ID_YUKON_LITE:
  2928. case CHIP_ID_YUKON_LP:
  2929. if (hw->phy_type < SK_PHY_MARV_COPPER && pmd_type != 'S')
  2930. hw->copper = 1;
  2931. hw->phy_addr = PHY_ADDR_MARV;
  2932. break;
  2933. default:
  2934. dev_err(&hw->pdev->dev, "unsupported chip type 0x%x\n",
  2935. hw->chip_id);
  2936. return -EOPNOTSUPP;
  2937. }
  2938. mac_cfg = skge_read8(hw, B2_MAC_CFG);
  2939. hw->ports = (mac_cfg & CFG_SNG_MAC) ? 1 : 2;
  2940. hw->chip_rev = (mac_cfg & CFG_CHIP_R_MSK) >> 4;
  2941. /* read the adapters RAM size */
  2942. t8 = skge_read8(hw, B2_E_0);
  2943. if (is_genesis(hw)) {
  2944. if (t8 == 3) {
  2945. /* special case: 4 x 64k x 36, offset = 0x80000 */
  2946. hw->ram_size = 0x100000;
  2947. hw->ram_offset = 0x80000;
  2948. } else
  2949. hw->ram_size = t8 * 512;
  2950. } else if (t8 == 0)
  2951. hw->ram_size = 0x20000;
  2952. else
  2953. hw->ram_size = t8 * 4096;
  2954. hw->intr_mask = IS_HW_ERR;
  2955. /* Use PHY IRQ for all but fiber based Genesis board */
  2956. if (!(is_genesis(hw) && hw->phy_type == SK_PHY_XMAC))
  2957. hw->intr_mask |= IS_EXT_REG;
  2958. if (is_genesis(hw))
  2959. genesis_init(hw);
  2960. else {
  2961. /* switch power to VCC (WA for VAUX problem) */
  2962. skge_write8(hw, B0_POWER_CTRL,
  2963. PC_VAUX_ENA | PC_VCC_ENA | PC_VAUX_OFF | PC_VCC_ON);
  2964. /* avoid boards with stuck Hardware error bits */
  2965. if ((skge_read32(hw, B0_ISRC) & IS_HW_ERR) &&
  2966. (skge_read32(hw, B0_HWE_ISRC) & IS_IRQ_SENSOR)) {
  2967. dev_warn(&hw->pdev->dev, "stuck hardware sensor bit\n");
  2968. hw->intr_mask &= ~IS_HW_ERR;
  2969. }
  2970. /* Clear PHY COMA */
  2971. skge_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_ON);
  2972. pci_read_config_dword(hw->pdev, PCI_DEV_REG1, &reg);
  2973. reg &= ~PCI_PHY_COMA;
  2974. pci_write_config_dword(hw->pdev, PCI_DEV_REG1, reg);
  2975. skge_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_OFF);
  2976. for (i = 0; i < hw->ports; i++) {
  2977. skge_write16(hw, SK_REG(i, GMAC_LINK_CTRL), GMLC_RST_SET);
  2978. skge_write16(hw, SK_REG(i, GMAC_LINK_CTRL), GMLC_RST_CLR);
  2979. }
  2980. }
  2981. /* turn off hardware timer (unused) */
  2982. skge_write8(hw, B2_TI_CTRL, TIM_STOP);
  2983. skge_write8(hw, B2_TI_CTRL, TIM_CLR_IRQ);
  2984. skge_write8(hw, B0_LED, LED_STAT_ON);
  2985. /* enable the Tx Arbiters */
  2986. for (i = 0; i < hw->ports; i++)
  2987. skge_write8(hw, SK_REG(i, TXA_CTRL), TXA_ENA_ARB);
  2988. /* Initialize ram interface */
  2989. skge_write16(hw, B3_RI_CTRL, RI_RST_CLR);
  2990. skge_write8(hw, B3_RI_WTO_R1, SK_RI_TO_53);
  2991. skge_write8(hw, B3_RI_WTO_XA1, SK_RI_TO_53);
  2992. skge_write8(hw, B3_RI_WTO_XS1, SK_RI_TO_53);
  2993. skge_write8(hw, B3_RI_RTO_R1, SK_RI_TO_53);
  2994. skge_write8(hw, B3_RI_RTO_XA1, SK_RI_TO_53);
  2995. skge_write8(hw, B3_RI_RTO_XS1, SK_RI_TO_53);
  2996. skge_write8(hw, B3_RI_WTO_R2, SK_RI_TO_53);
  2997. skge_write8(hw, B3_RI_WTO_XA2, SK_RI_TO_53);
  2998. skge_write8(hw, B3_RI_WTO_XS2, SK_RI_TO_53);
  2999. skge_write8(hw, B3_RI_RTO_R2, SK_RI_TO_53);
  3000. skge_write8(hw, B3_RI_RTO_XA2, SK_RI_TO_53);
  3001. skge_write8(hw, B3_RI_RTO_XS2, SK_RI_TO_53);
  3002. skge_write32(hw, B0_HWE_IMSK, IS_ERR_MSK);
  3003. /* Set interrupt moderation for Transmit only
  3004. * Receive interrupts avoided by NAPI
  3005. */
  3006. skge_write32(hw, B2_IRQM_MSK, IS_XA1_F|IS_XA2_F);
  3007. skge_write32(hw, B2_IRQM_INI, skge_usecs2clk(hw, 100));
  3008. skge_write32(hw, B2_IRQM_CTRL, TIM_START);
  3009. /* Leave irq disabled until first port is brought up. */
  3010. skge_write32(hw, B0_IMSK, 0);
  3011. for (i = 0; i < hw->ports; i++) {
  3012. if (is_genesis(hw))
  3013. genesis_reset(hw, i);
  3014. else
  3015. yukon_reset(hw, i);
  3016. }
  3017. return 0;
  3018. }
  3019. #ifdef CONFIG_SKGE_DEBUG
  3020. static struct dentry *skge_debug;
  3021. static int skge_debug_show(struct seq_file *seq, void *v)
  3022. {
  3023. struct net_device *dev = seq->private;
  3024. const struct skge_port *skge = netdev_priv(dev);
  3025. const struct skge_hw *hw = skge->hw;
  3026. const struct skge_element *e;
  3027. if (!netif_running(dev))
  3028. return -ENETDOWN;
  3029. seq_printf(seq, "IRQ src=%x mask=%x\n", skge_read32(hw, B0_ISRC),
  3030. skge_read32(hw, B0_IMSK));
  3031. seq_printf(seq, "Tx Ring: (%d)\n", skge_avail(&skge->tx_ring));
  3032. for (e = skge->tx_ring.to_clean; e != skge->tx_ring.to_use; e = e->next) {
  3033. const struct skge_tx_desc *t = e->desc;
  3034. seq_printf(seq, "%#x dma=%#x%08x %#x csum=%#x/%x/%x\n",
  3035. t->control, t->dma_hi, t->dma_lo, t->status,
  3036. t->csum_offs, t->csum_write, t->csum_start);
  3037. }
  3038. seq_printf(seq, "\nRx Ring:\n");
  3039. for (e = skge->rx_ring.to_clean; ; e = e->next) {
  3040. const struct skge_rx_desc *r = e->desc;
  3041. if (r->control & BMU_OWN)
  3042. break;
  3043. seq_printf(seq, "%#x dma=%#x%08x %#x %#x csum=%#x/%x\n",
  3044. r->control, r->dma_hi, r->dma_lo, r->status,
  3045. r->timestamp, r->csum1, r->csum1_start);
  3046. }
  3047. return 0;
  3048. }
  3049. static int skge_debug_open(struct inode *inode, struct file *file)
  3050. {
  3051. return single_open(file, skge_debug_show, inode->i_private);
  3052. }
  3053. static const struct file_operations skge_debug_fops = {
  3054. .owner = THIS_MODULE,
  3055. .open = skge_debug_open,
  3056. .read = seq_read,
  3057. .llseek = seq_lseek,
  3058. .release = single_release,
  3059. };
  3060. /*
  3061. * Use network device events to create/remove/rename
  3062. * debugfs file entries
  3063. */
  3064. static int skge_device_event(struct notifier_block *unused,
  3065. unsigned long event, void *ptr)
  3066. {
  3067. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  3068. struct skge_port *skge;
  3069. struct dentry *d;
  3070. if (dev->netdev_ops->ndo_open != &skge_up || !skge_debug)
  3071. goto done;
  3072. skge = netdev_priv(dev);
  3073. switch (event) {
  3074. case NETDEV_CHANGENAME:
  3075. if (skge->debugfs) {
  3076. d = debugfs_rename(skge_debug, skge->debugfs,
  3077. skge_debug, dev->name);
  3078. if (d)
  3079. skge->debugfs = d;
  3080. else {
  3081. netdev_info(dev, "rename failed\n");
  3082. debugfs_remove(skge->debugfs);
  3083. }
  3084. }
  3085. break;
  3086. case NETDEV_GOING_DOWN:
  3087. if (skge->debugfs) {
  3088. debugfs_remove(skge->debugfs);
  3089. skge->debugfs = NULL;
  3090. }
  3091. break;
  3092. case NETDEV_UP:
  3093. d = debugfs_create_file(dev->name, S_IRUGO,
  3094. skge_debug, dev,
  3095. &skge_debug_fops);
  3096. if (!d || IS_ERR(d))
  3097. netdev_info(dev, "debugfs create failed\n");
  3098. else
  3099. skge->debugfs = d;
  3100. break;
  3101. }
  3102. done:
  3103. return NOTIFY_DONE;
  3104. }
  3105. static struct notifier_block skge_notifier = {
  3106. .notifier_call = skge_device_event,
  3107. };
  3108. static __init void skge_debug_init(void)
  3109. {
  3110. struct dentry *ent;
  3111. ent = debugfs_create_dir("skge", NULL);
  3112. if (!ent || IS_ERR(ent)) {
  3113. pr_info("debugfs create directory failed\n");
  3114. return;
  3115. }
  3116. skge_debug = ent;
  3117. register_netdevice_notifier(&skge_notifier);
  3118. }
  3119. static __exit void skge_debug_cleanup(void)
  3120. {
  3121. if (skge_debug) {
  3122. unregister_netdevice_notifier(&skge_notifier);
  3123. debugfs_remove(skge_debug);
  3124. skge_debug = NULL;
  3125. }
  3126. }
  3127. #else
  3128. #define skge_debug_init()
  3129. #define skge_debug_cleanup()
  3130. #endif
  3131. static const struct net_device_ops skge_netdev_ops = {
  3132. .ndo_open = skge_up,
  3133. .ndo_stop = skge_down,
  3134. .ndo_start_xmit = skge_xmit_frame,
  3135. .ndo_do_ioctl = skge_ioctl,
  3136. .ndo_get_stats = skge_get_stats,
  3137. .ndo_tx_timeout = skge_tx_timeout,
  3138. .ndo_change_mtu = skge_change_mtu,
  3139. .ndo_validate_addr = eth_validate_addr,
  3140. .ndo_set_rx_mode = skge_set_multicast,
  3141. .ndo_set_mac_address = skge_set_mac_address,
  3142. #ifdef CONFIG_NET_POLL_CONTROLLER
  3143. .ndo_poll_controller = skge_netpoll,
  3144. #endif
  3145. };
  3146. /* Initialize network device */
  3147. static struct net_device *skge_devinit(struct skge_hw *hw, int port,
  3148. int highmem)
  3149. {
  3150. struct skge_port *skge;
  3151. struct net_device *dev = alloc_etherdev(sizeof(*skge));
  3152. if (!dev)
  3153. return NULL;
  3154. SET_NETDEV_DEV(dev, &hw->pdev->dev);
  3155. dev->netdev_ops = &skge_netdev_ops;
  3156. dev->ethtool_ops = &skge_ethtool_ops;
  3157. dev->watchdog_timeo = TX_WATCHDOG;
  3158. dev->irq = hw->pdev->irq;
  3159. if (highmem)
  3160. dev->features |= NETIF_F_HIGHDMA;
  3161. skge = netdev_priv(dev);
  3162. netif_napi_add(dev, &skge->napi, skge_poll, NAPI_WEIGHT);
  3163. skge->netdev = dev;
  3164. skge->hw = hw;
  3165. skge->msg_enable = netif_msg_init(debug, default_msg);
  3166. skge->tx_ring.count = DEFAULT_TX_RING_SIZE;
  3167. skge->rx_ring.count = DEFAULT_RX_RING_SIZE;
  3168. /* Auto speed and flow control */
  3169. skge->autoneg = AUTONEG_ENABLE;
  3170. skge->flow_control = FLOW_MODE_SYM_OR_REM;
  3171. skge->duplex = -1;
  3172. skge->speed = -1;
  3173. skge->advertising = skge_supported_modes(hw);
  3174. if (device_can_wakeup(&hw->pdev->dev)) {
  3175. skge->wol = wol_supported(hw) & WAKE_MAGIC;
  3176. device_set_wakeup_enable(&hw->pdev->dev, skge->wol);
  3177. }
  3178. hw->dev[port] = dev;
  3179. skge->port = port;
  3180. /* Only used for Genesis XMAC */
  3181. if (is_genesis(hw))
  3182. setup_timer(&skge->link_timer, xm_link_timer, (unsigned long) skge);
  3183. else {
  3184. dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG |
  3185. NETIF_F_RXCSUM;
  3186. dev->features |= dev->hw_features;
  3187. }
  3188. /* read the mac address */
  3189. memcpy_fromio(dev->dev_addr, hw->regs + B2_MAC_1 + port*8, ETH_ALEN);
  3190. return dev;
  3191. }
  3192. static void skge_show_addr(struct net_device *dev)
  3193. {
  3194. const struct skge_port *skge = netdev_priv(dev);
  3195. netif_info(skge, probe, skge->netdev, "addr %pM\n", dev->dev_addr);
  3196. }
  3197. static int only_32bit_dma;
  3198. static int skge_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  3199. {
  3200. struct net_device *dev, *dev1;
  3201. struct skge_hw *hw;
  3202. int err, using_dac = 0;
  3203. err = pci_enable_device(pdev);
  3204. if (err) {
  3205. dev_err(&pdev->dev, "cannot enable PCI device\n");
  3206. goto err_out;
  3207. }
  3208. err = pci_request_regions(pdev, DRV_NAME);
  3209. if (err) {
  3210. dev_err(&pdev->dev, "cannot obtain PCI resources\n");
  3211. goto err_out_disable_pdev;
  3212. }
  3213. pci_set_master(pdev);
  3214. if (!only_32bit_dma && !pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
  3215. using_dac = 1;
  3216. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  3217. } else if (!(err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)))) {
  3218. using_dac = 0;
  3219. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  3220. }
  3221. if (err) {
  3222. dev_err(&pdev->dev, "no usable DMA configuration\n");
  3223. goto err_out_free_regions;
  3224. }
  3225. #ifdef __BIG_ENDIAN
  3226. /* byte swap descriptors in hardware */
  3227. {
  3228. u32 reg;
  3229. pci_read_config_dword(pdev, PCI_DEV_REG2, &reg);
  3230. reg |= PCI_REV_DESC;
  3231. pci_write_config_dword(pdev, PCI_DEV_REG2, reg);
  3232. }
  3233. #endif
  3234. err = -ENOMEM;
  3235. /* space for skge@pci:0000:04:00.0 */
  3236. hw = kzalloc(sizeof(*hw) + strlen(DRV_NAME "@pci:")
  3237. + strlen(pci_name(pdev)) + 1, GFP_KERNEL);
  3238. if (!hw)
  3239. goto err_out_free_regions;
  3240. sprintf(hw->irq_name, DRV_NAME "@pci:%s", pci_name(pdev));
  3241. hw->pdev = pdev;
  3242. spin_lock_init(&hw->hw_lock);
  3243. spin_lock_init(&hw->phy_lock);
  3244. tasklet_init(&hw->phy_task, skge_extirq, (unsigned long) hw);
  3245. hw->regs = ioremap_nocache(pci_resource_start(pdev, 0), 0x4000);
  3246. if (!hw->regs) {
  3247. dev_err(&pdev->dev, "cannot map device registers\n");
  3248. goto err_out_free_hw;
  3249. }
  3250. err = skge_reset(hw);
  3251. if (err)
  3252. goto err_out_iounmap;
  3253. pr_info("%s addr 0x%llx irq %d chip %s rev %d\n",
  3254. DRV_VERSION,
  3255. (unsigned long long)pci_resource_start(pdev, 0), pdev->irq,
  3256. skge_board_name(hw), hw->chip_rev);
  3257. dev = skge_devinit(hw, 0, using_dac);
  3258. if (!dev) {
  3259. err = -ENOMEM;
  3260. goto err_out_led_off;
  3261. }
  3262. /* Some motherboards are broken and has zero in ROM. */
  3263. if (!is_valid_ether_addr(dev->dev_addr))
  3264. dev_warn(&pdev->dev, "bad (zero?) ethernet address in rom\n");
  3265. err = register_netdev(dev);
  3266. if (err) {
  3267. dev_err(&pdev->dev, "cannot register net device\n");
  3268. goto err_out_free_netdev;
  3269. }
  3270. skge_show_addr(dev);
  3271. if (hw->ports > 1) {
  3272. dev1 = skge_devinit(hw, 1, using_dac);
  3273. if (!dev1) {
  3274. err = -ENOMEM;
  3275. goto err_out_unregister;
  3276. }
  3277. err = register_netdev(dev1);
  3278. if (err) {
  3279. dev_err(&pdev->dev, "cannot register second net device\n");
  3280. goto err_out_free_dev1;
  3281. }
  3282. err = request_irq(pdev->irq, skge_intr, IRQF_SHARED,
  3283. hw->irq_name, hw);
  3284. if (err) {
  3285. dev_err(&pdev->dev, "cannot assign irq %d\n",
  3286. pdev->irq);
  3287. goto err_out_unregister_dev1;
  3288. }
  3289. skge_show_addr(dev1);
  3290. }
  3291. pci_set_drvdata(pdev, hw);
  3292. return 0;
  3293. err_out_unregister_dev1:
  3294. unregister_netdev(dev1);
  3295. err_out_free_dev1:
  3296. free_netdev(dev1);
  3297. err_out_unregister:
  3298. unregister_netdev(dev);
  3299. err_out_free_netdev:
  3300. free_netdev(dev);
  3301. err_out_led_off:
  3302. skge_write16(hw, B0_LED, LED_STAT_OFF);
  3303. err_out_iounmap:
  3304. iounmap(hw->regs);
  3305. err_out_free_hw:
  3306. kfree(hw);
  3307. err_out_free_regions:
  3308. pci_release_regions(pdev);
  3309. err_out_disable_pdev:
  3310. pci_disable_device(pdev);
  3311. err_out:
  3312. return err;
  3313. }
  3314. static void skge_remove(struct pci_dev *pdev)
  3315. {
  3316. struct skge_hw *hw = pci_get_drvdata(pdev);
  3317. struct net_device *dev0, *dev1;
  3318. if (!hw)
  3319. return;
  3320. dev1 = hw->dev[1];
  3321. if (dev1)
  3322. unregister_netdev(dev1);
  3323. dev0 = hw->dev[0];
  3324. unregister_netdev(dev0);
  3325. tasklet_kill(&hw->phy_task);
  3326. spin_lock_irq(&hw->hw_lock);
  3327. hw->intr_mask = 0;
  3328. if (hw->ports > 1) {
  3329. skge_write32(hw, B0_IMSK, 0);
  3330. skge_read32(hw, B0_IMSK);
  3331. free_irq(pdev->irq, hw);
  3332. }
  3333. spin_unlock_irq(&hw->hw_lock);
  3334. skge_write16(hw, B0_LED, LED_STAT_OFF);
  3335. skge_write8(hw, B0_CTST, CS_RST_SET);
  3336. if (hw->ports > 1)
  3337. free_irq(pdev->irq, hw);
  3338. pci_release_regions(pdev);
  3339. pci_disable_device(pdev);
  3340. if (dev1)
  3341. free_netdev(dev1);
  3342. free_netdev(dev0);
  3343. iounmap(hw->regs);
  3344. kfree(hw);
  3345. }
  3346. #ifdef CONFIG_PM_SLEEP
  3347. static int skge_suspend(struct device *dev)
  3348. {
  3349. struct pci_dev *pdev = to_pci_dev(dev);
  3350. struct skge_hw *hw = pci_get_drvdata(pdev);
  3351. int i;
  3352. if (!hw)
  3353. return 0;
  3354. for (i = 0; i < hw->ports; i++) {
  3355. struct net_device *dev = hw->dev[i];
  3356. struct skge_port *skge = netdev_priv(dev);
  3357. if (netif_running(dev))
  3358. skge_down(dev);
  3359. if (skge->wol)
  3360. skge_wol_init(skge);
  3361. }
  3362. skge_write32(hw, B0_IMSK, 0);
  3363. return 0;
  3364. }
  3365. static int skge_resume(struct device *dev)
  3366. {
  3367. struct pci_dev *pdev = to_pci_dev(dev);
  3368. struct skge_hw *hw = pci_get_drvdata(pdev);
  3369. int i, err;
  3370. if (!hw)
  3371. return 0;
  3372. err = skge_reset(hw);
  3373. if (err)
  3374. goto out;
  3375. for (i = 0; i < hw->ports; i++) {
  3376. struct net_device *dev = hw->dev[i];
  3377. if (netif_running(dev)) {
  3378. err = skge_up(dev);
  3379. if (err) {
  3380. netdev_err(dev, "could not up: %d\n", err);
  3381. dev_close(dev);
  3382. goto out;
  3383. }
  3384. }
  3385. }
  3386. out:
  3387. return err;
  3388. }
  3389. static SIMPLE_DEV_PM_OPS(skge_pm_ops, skge_suspend, skge_resume);
  3390. #define SKGE_PM_OPS (&skge_pm_ops)
  3391. #else
  3392. #define SKGE_PM_OPS NULL
  3393. #endif /* CONFIG_PM_SLEEP */
  3394. static void skge_shutdown(struct pci_dev *pdev)
  3395. {
  3396. struct skge_hw *hw = pci_get_drvdata(pdev);
  3397. int i;
  3398. if (!hw)
  3399. return;
  3400. for (i = 0; i < hw->ports; i++) {
  3401. struct net_device *dev = hw->dev[i];
  3402. struct skge_port *skge = netdev_priv(dev);
  3403. if (skge->wol)
  3404. skge_wol_init(skge);
  3405. }
  3406. pci_wake_from_d3(pdev, device_may_wakeup(&pdev->dev));
  3407. pci_set_power_state(pdev, PCI_D3hot);
  3408. }
  3409. static struct pci_driver skge_driver = {
  3410. .name = DRV_NAME,
  3411. .id_table = skge_id_table,
  3412. .probe = skge_probe,
  3413. .remove = skge_remove,
  3414. .shutdown = skge_shutdown,
  3415. .driver.pm = SKGE_PM_OPS,
  3416. };
  3417. static struct dmi_system_id skge_32bit_dma_boards[] = {
  3418. {
  3419. .ident = "Gigabyte nForce boards",
  3420. .matches = {
  3421. DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co"),
  3422. DMI_MATCH(DMI_BOARD_NAME, "nForce"),
  3423. },
  3424. },
  3425. {
  3426. .ident = "ASUS P5NSLI",
  3427. .matches = {
  3428. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  3429. DMI_MATCH(DMI_BOARD_NAME, "P5NSLI")
  3430. },
  3431. },
  3432. {
  3433. .ident = "FUJITSU SIEMENS A8NE-FM",
  3434. .matches = {
  3435. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTek Computer INC."),
  3436. DMI_MATCH(DMI_BOARD_NAME, "A8NE-FM")
  3437. },
  3438. },
  3439. {}
  3440. };
  3441. static int __init skge_init_module(void)
  3442. {
  3443. if (dmi_check_system(skge_32bit_dma_boards))
  3444. only_32bit_dma = 1;
  3445. skge_debug_init();
  3446. return pci_register_driver(&skge_driver);
  3447. }
  3448. static void __exit skge_cleanup_module(void)
  3449. {
  3450. pci_unregister_driver(&skge_driver);
  3451. skge_debug_cleanup();
  3452. }
  3453. module_init(skge_init_module);
  3454. module_exit(skge_cleanup_module);