skge.c 107 KB

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