sb1250-mac.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652
  1. /*
  2. * Copyright (C) 2001,2002,2003,2004 Broadcom Corporation
  3. * Copyright (c) 2006, 2007 Maciej W. Rozycki
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  17. *
  18. *
  19. * This driver is designed for the Broadcom SiByte SOC built-in
  20. * Ethernet controllers. Written by Mitch Lichtenberg at Broadcom Corp.
  21. *
  22. * Updated to the driver model and the PHY abstraction layer
  23. * by Maciej W. Rozycki.
  24. */
  25. #include <linux/bug.h>
  26. #include <linux/module.h>
  27. #include <linux/kernel.h>
  28. #include <linux/string.h>
  29. #include <linux/timer.h>
  30. #include <linux/errno.h>
  31. #include <linux/ioport.h>
  32. #include <linux/slab.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/netdevice.h>
  35. #include <linux/etherdevice.h>
  36. #include <linux/skbuff.h>
  37. #include <linux/bitops.h>
  38. #include <linux/err.h>
  39. #include <linux/ethtool.h>
  40. #include <linux/mii.h>
  41. #include <linux/phy.h>
  42. #include <linux/platform_device.h>
  43. #include <linux/prefetch.h>
  44. #include <asm/cache.h>
  45. #include <asm/io.h>
  46. #include <asm/processor.h> /* Processor type for cache alignment. */
  47. /* Operational parameters that usually are not changed. */
  48. #define CONFIG_SBMAC_COALESCE
  49. /* Time in jiffies before concluding the transmitter is hung. */
  50. #define TX_TIMEOUT (2*HZ)
  51. MODULE_AUTHOR("Mitch Lichtenberg (Broadcom Corp.)");
  52. MODULE_DESCRIPTION("Broadcom SiByte SOC GB Ethernet driver");
  53. /* A few user-configurable values which may be modified when a driver
  54. module is loaded. */
  55. /* 1 normal messages, 0 quiet .. 7 verbose. */
  56. static int debug = 1;
  57. module_param(debug, int, S_IRUGO);
  58. MODULE_PARM_DESC(debug, "Debug messages");
  59. #ifdef CONFIG_SBMAC_COALESCE
  60. static int int_pktcnt_tx = 255;
  61. module_param(int_pktcnt_tx, int, S_IRUGO);
  62. MODULE_PARM_DESC(int_pktcnt_tx, "TX packet count");
  63. static int int_timeout_tx = 255;
  64. module_param(int_timeout_tx, int, S_IRUGO);
  65. MODULE_PARM_DESC(int_timeout_tx, "TX timeout value");
  66. static int int_pktcnt_rx = 64;
  67. module_param(int_pktcnt_rx, int, S_IRUGO);
  68. MODULE_PARM_DESC(int_pktcnt_rx, "RX packet count");
  69. static int int_timeout_rx = 64;
  70. module_param(int_timeout_rx, int, S_IRUGO);
  71. MODULE_PARM_DESC(int_timeout_rx, "RX timeout value");
  72. #endif
  73. #include <asm/sibyte/board.h>
  74. #include <asm/sibyte/sb1250.h>
  75. #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
  76. #include <asm/sibyte/bcm1480_regs.h>
  77. #include <asm/sibyte/bcm1480_int.h>
  78. #define R_MAC_DMA_OODPKTLOST_RX R_MAC_DMA_OODPKTLOST
  79. #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
  80. #include <asm/sibyte/sb1250_regs.h>
  81. #include <asm/sibyte/sb1250_int.h>
  82. #else
  83. #error invalid SiByte MAC configuration
  84. #endif
  85. #include <asm/sibyte/sb1250_scd.h>
  86. #include <asm/sibyte/sb1250_mac.h>
  87. #include <asm/sibyte/sb1250_dma.h>
  88. #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
  89. #define UNIT_INT(n) (K_BCM1480_INT_MAC_0 + ((n) * 2))
  90. #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
  91. #define UNIT_INT(n) (K_INT_MAC_0 + (n))
  92. #else
  93. #error invalid SiByte MAC configuration
  94. #endif
  95. #ifdef K_INT_PHY
  96. #define SBMAC_PHY_INT K_INT_PHY
  97. #else
  98. #define SBMAC_PHY_INT PHY_POLL
  99. #endif
  100. /**********************************************************************
  101. * Simple types
  102. ********************************************************************* */
  103. enum sbmac_speed {
  104. sbmac_speed_none = 0,
  105. sbmac_speed_10 = SPEED_10,
  106. sbmac_speed_100 = SPEED_100,
  107. sbmac_speed_1000 = SPEED_1000,
  108. };
  109. enum sbmac_duplex {
  110. sbmac_duplex_none = -1,
  111. sbmac_duplex_half = DUPLEX_HALF,
  112. sbmac_duplex_full = DUPLEX_FULL,
  113. };
  114. enum sbmac_fc {
  115. sbmac_fc_none,
  116. sbmac_fc_disabled,
  117. sbmac_fc_frame,
  118. sbmac_fc_collision,
  119. sbmac_fc_carrier,
  120. };
  121. enum sbmac_state {
  122. sbmac_state_uninit,
  123. sbmac_state_off,
  124. sbmac_state_on,
  125. sbmac_state_broken,
  126. };
  127. /**********************************************************************
  128. * Macros
  129. ********************************************************************* */
  130. #define SBDMA_NEXTBUF(d,f) ((((d)->f+1) == (d)->sbdma_dscrtable_end) ? \
  131. (d)->sbdma_dscrtable : (d)->f+1)
  132. #define NUMCACHEBLKS(x) (((x)+SMP_CACHE_BYTES-1)/SMP_CACHE_BYTES)
  133. #define SBMAC_MAX_TXDESCR 256
  134. #define SBMAC_MAX_RXDESCR 256
  135. #define ENET_PACKET_SIZE 1518
  136. /*#define ENET_PACKET_SIZE 9216 */
  137. /**********************************************************************
  138. * DMA Descriptor structure
  139. ********************************************************************* */
  140. struct sbdmadscr {
  141. uint64_t dscr_a;
  142. uint64_t dscr_b;
  143. };
  144. /**********************************************************************
  145. * DMA Controller structure
  146. ********************************************************************* */
  147. struct sbmacdma {
  148. /*
  149. * This stuff is used to identify the channel and the registers
  150. * associated with it.
  151. */
  152. struct sbmac_softc *sbdma_eth; /* back pointer to associated
  153. MAC */
  154. int sbdma_channel; /* channel number */
  155. int sbdma_txdir; /* direction (1=transmit) */
  156. int sbdma_maxdescr; /* total # of descriptors
  157. in ring */
  158. #ifdef CONFIG_SBMAC_COALESCE
  159. int sbdma_int_pktcnt;
  160. /* # descriptors rx/tx
  161. before interrupt */
  162. int sbdma_int_timeout;
  163. /* # usec rx/tx interrupt */
  164. #endif
  165. void __iomem *sbdma_config0; /* DMA config register 0 */
  166. void __iomem *sbdma_config1; /* DMA config register 1 */
  167. void __iomem *sbdma_dscrbase;
  168. /* descriptor base address */
  169. void __iomem *sbdma_dscrcnt; /* descriptor count register */
  170. void __iomem *sbdma_curdscr; /* current descriptor
  171. address */
  172. void __iomem *sbdma_oodpktlost;
  173. /* pkt drop (rx only) */
  174. /*
  175. * This stuff is for maintenance of the ring
  176. */
  177. void *sbdma_dscrtable_unaligned;
  178. struct sbdmadscr *sbdma_dscrtable;
  179. /* base of descriptor table */
  180. struct sbdmadscr *sbdma_dscrtable_end;
  181. /* end of descriptor table */
  182. struct sk_buff **sbdma_ctxtable;
  183. /* context table, one
  184. per descr */
  185. dma_addr_t sbdma_dscrtable_phys;
  186. /* and also the phys addr */
  187. struct sbdmadscr *sbdma_addptr; /* next dscr for sw to add */
  188. struct sbdmadscr *sbdma_remptr; /* next dscr for sw
  189. to remove */
  190. };
  191. /**********************************************************************
  192. * Ethernet softc structure
  193. ********************************************************************* */
  194. struct sbmac_softc {
  195. /*
  196. * Linux-specific things
  197. */
  198. struct net_device *sbm_dev; /* pointer to linux device */
  199. struct napi_struct napi;
  200. struct phy_device *phy_dev; /* the associated PHY device */
  201. struct mii_bus *mii_bus; /* the MII bus */
  202. spinlock_t sbm_lock; /* spin lock */
  203. int sbm_devflags; /* current device flags */
  204. /*
  205. * Controller-specific things
  206. */
  207. void __iomem *sbm_base; /* MAC's base address */
  208. enum sbmac_state sbm_state; /* current state */
  209. void __iomem *sbm_macenable; /* MAC Enable Register */
  210. void __iomem *sbm_maccfg; /* MAC Config Register */
  211. void __iomem *sbm_fifocfg; /* FIFO Config Register */
  212. void __iomem *sbm_framecfg; /* Frame Config Register */
  213. void __iomem *sbm_rxfilter; /* Receive Filter Register */
  214. void __iomem *sbm_isr; /* Interrupt Status Register */
  215. void __iomem *sbm_imr; /* Interrupt Mask Register */
  216. void __iomem *sbm_mdio; /* MDIO Register */
  217. enum sbmac_speed sbm_speed; /* current speed */
  218. enum sbmac_duplex sbm_duplex; /* current duplex */
  219. enum sbmac_fc sbm_fc; /* cur. flow control setting */
  220. int sbm_pause; /* current pause setting */
  221. int sbm_link; /* current link state */
  222. unsigned char sbm_hwaddr[ETH_ALEN];
  223. struct sbmacdma sbm_txdma; /* only channel 0 for now */
  224. struct sbmacdma sbm_rxdma;
  225. int rx_hw_checksum;
  226. int sbe_idx;
  227. };
  228. /**********************************************************************
  229. * Externs
  230. ********************************************************************* */
  231. /**********************************************************************
  232. * Prototypes
  233. ********************************************************************* */
  234. static void sbdma_initctx(struct sbmacdma *d, struct sbmac_softc *s, int chan,
  235. int txrx, int maxdescr);
  236. static void sbdma_channel_start(struct sbmacdma *d, int rxtx);
  237. static int sbdma_add_rcvbuffer(struct sbmac_softc *sc, struct sbmacdma *d,
  238. struct sk_buff *m);
  239. static int sbdma_add_txbuffer(struct sbmacdma *d, struct sk_buff *m);
  240. static void sbdma_emptyring(struct sbmacdma *d);
  241. static void sbdma_fillring(struct sbmac_softc *sc, struct sbmacdma *d);
  242. static int sbdma_rx_process(struct sbmac_softc *sc, struct sbmacdma *d,
  243. int work_to_do, int poll);
  244. static void sbdma_tx_process(struct sbmac_softc *sc, struct sbmacdma *d,
  245. int poll);
  246. static int sbmac_initctx(struct sbmac_softc *s);
  247. static void sbmac_channel_start(struct sbmac_softc *s);
  248. static void sbmac_channel_stop(struct sbmac_softc *s);
  249. static enum sbmac_state sbmac_set_channel_state(struct sbmac_softc *,
  250. enum sbmac_state);
  251. static void sbmac_promiscuous_mode(struct sbmac_softc *sc, int onoff);
  252. static uint64_t sbmac_addr2reg(unsigned char *ptr);
  253. static irqreturn_t sbmac_intr(int irq, void *dev_instance);
  254. static int sbmac_start_tx(struct sk_buff *skb, struct net_device *dev);
  255. static void sbmac_setmulti(struct sbmac_softc *sc);
  256. static int sbmac_init(struct platform_device *pldev, long long base);
  257. static int sbmac_set_speed(struct sbmac_softc *s, enum sbmac_speed speed);
  258. static int sbmac_set_duplex(struct sbmac_softc *s, enum sbmac_duplex duplex,
  259. enum sbmac_fc fc);
  260. static int sbmac_open(struct net_device *dev);
  261. static void sbmac_tx_timeout (struct net_device *dev);
  262. static void sbmac_set_rx_mode(struct net_device *dev);
  263. static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  264. static int sbmac_close(struct net_device *dev);
  265. static int sbmac_poll(struct napi_struct *napi, int budget);
  266. static void sbmac_mii_poll(struct net_device *dev);
  267. static int sbmac_mii_probe(struct net_device *dev);
  268. static void sbmac_mii_sync(void __iomem *sbm_mdio);
  269. static void sbmac_mii_senddata(void __iomem *sbm_mdio, unsigned int data,
  270. int bitcnt);
  271. static int sbmac_mii_read(struct mii_bus *bus, int phyaddr, int regidx);
  272. static int sbmac_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
  273. u16 val);
  274. /**********************************************************************
  275. * Globals
  276. ********************************************************************* */
  277. static char sbmac_string[] = "sb1250-mac";
  278. static char sbmac_mdio_string[] = "sb1250-mac-mdio";
  279. /**********************************************************************
  280. * MDIO constants
  281. ********************************************************************* */
  282. #define MII_COMMAND_START 0x01
  283. #define MII_COMMAND_READ 0x02
  284. #define MII_COMMAND_WRITE 0x01
  285. #define MII_COMMAND_ACK 0x02
  286. #define M_MAC_MDIO_DIR_OUTPUT 0 /* for clarity */
  287. #define ENABLE 1
  288. #define DISABLE 0
  289. /**********************************************************************
  290. * SBMAC_MII_SYNC(sbm_mdio)
  291. *
  292. * Synchronize with the MII - send a pattern of bits to the MII
  293. * that will guarantee that it is ready to accept a command.
  294. *
  295. * Input parameters:
  296. * sbm_mdio - address of the MAC's MDIO register
  297. *
  298. * Return value:
  299. * nothing
  300. ********************************************************************* */
  301. static void sbmac_mii_sync(void __iomem *sbm_mdio)
  302. {
  303. int cnt;
  304. uint64_t bits;
  305. int mac_mdio_genc;
  306. mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
  307. bits = M_MAC_MDIO_DIR_OUTPUT | M_MAC_MDIO_OUT;
  308. __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
  309. for (cnt = 0; cnt < 32; cnt++) {
  310. __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, sbm_mdio);
  311. __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
  312. }
  313. }
  314. /**********************************************************************
  315. * SBMAC_MII_SENDDATA(sbm_mdio, data, bitcnt)
  316. *
  317. * Send some bits to the MII. The bits to be sent are right-
  318. * justified in the 'data' parameter.
  319. *
  320. * Input parameters:
  321. * sbm_mdio - address of the MAC's MDIO register
  322. * data - data to send
  323. * bitcnt - number of bits to send
  324. ********************************************************************* */
  325. static void sbmac_mii_senddata(void __iomem *sbm_mdio, unsigned int data,
  326. int bitcnt)
  327. {
  328. int i;
  329. uint64_t bits;
  330. unsigned int curmask;
  331. int mac_mdio_genc;
  332. mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
  333. bits = M_MAC_MDIO_DIR_OUTPUT;
  334. __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
  335. curmask = 1 << (bitcnt - 1);
  336. for (i = 0; i < bitcnt; i++) {
  337. if (data & curmask)
  338. bits |= M_MAC_MDIO_OUT;
  339. else bits &= ~M_MAC_MDIO_OUT;
  340. __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
  341. __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, sbm_mdio);
  342. __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
  343. curmask >>= 1;
  344. }
  345. }
  346. /**********************************************************************
  347. * SBMAC_MII_READ(bus, phyaddr, regidx)
  348. * Read a PHY register.
  349. *
  350. * Input parameters:
  351. * bus - MDIO bus handle
  352. * phyaddr - PHY's address
  353. * regnum - index of register to read
  354. *
  355. * Return value:
  356. * value read, or 0xffff if an error occurred.
  357. ********************************************************************* */
  358. static int sbmac_mii_read(struct mii_bus *bus, int phyaddr, int regidx)
  359. {
  360. struct sbmac_softc *sc = (struct sbmac_softc *)bus->priv;
  361. void __iomem *sbm_mdio = sc->sbm_mdio;
  362. int idx;
  363. int error;
  364. int regval;
  365. int mac_mdio_genc;
  366. /*
  367. * Synchronize ourselves so that the PHY knows the next
  368. * thing coming down is a command
  369. */
  370. sbmac_mii_sync(sbm_mdio);
  371. /*
  372. * Send the data to the PHY. The sequence is
  373. * a "start" command (2 bits)
  374. * a "read" command (2 bits)
  375. * the PHY addr (5 bits)
  376. * the register index (5 bits)
  377. */
  378. sbmac_mii_senddata(sbm_mdio, MII_COMMAND_START, 2);
  379. sbmac_mii_senddata(sbm_mdio, MII_COMMAND_READ, 2);
  380. sbmac_mii_senddata(sbm_mdio, phyaddr, 5);
  381. sbmac_mii_senddata(sbm_mdio, regidx, 5);
  382. mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
  383. /*
  384. * Switch the port around without a clock transition.
  385. */
  386. __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
  387. /*
  388. * Send out a clock pulse to signal we want the status
  389. */
  390. __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
  391. sbm_mdio);
  392. __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
  393. /*
  394. * If an error occurred, the PHY will signal '1' back
  395. */
  396. error = __raw_readq(sbm_mdio) & M_MAC_MDIO_IN;
  397. /*
  398. * Issue an 'idle' clock pulse, but keep the direction
  399. * the same.
  400. */
  401. __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
  402. sbm_mdio);
  403. __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
  404. regval = 0;
  405. for (idx = 0; idx < 16; idx++) {
  406. regval <<= 1;
  407. if (error == 0) {
  408. if (__raw_readq(sbm_mdio) & M_MAC_MDIO_IN)
  409. regval |= 1;
  410. }
  411. __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
  412. sbm_mdio);
  413. __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
  414. }
  415. /* Switch back to output */
  416. __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, sbm_mdio);
  417. if (error == 0)
  418. return regval;
  419. return 0xffff;
  420. }
  421. /**********************************************************************
  422. * SBMAC_MII_WRITE(bus, phyaddr, regidx, regval)
  423. *
  424. * Write a value to a PHY register.
  425. *
  426. * Input parameters:
  427. * bus - MDIO bus handle
  428. * phyaddr - PHY to use
  429. * regidx - register within the PHY
  430. * regval - data to write to register
  431. *
  432. * Return value:
  433. * 0 for success
  434. ********************************************************************* */
  435. static int sbmac_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
  436. u16 regval)
  437. {
  438. struct sbmac_softc *sc = (struct sbmac_softc *)bus->priv;
  439. void __iomem *sbm_mdio = sc->sbm_mdio;
  440. int mac_mdio_genc;
  441. sbmac_mii_sync(sbm_mdio);
  442. sbmac_mii_senddata(sbm_mdio, MII_COMMAND_START, 2);
  443. sbmac_mii_senddata(sbm_mdio, MII_COMMAND_WRITE, 2);
  444. sbmac_mii_senddata(sbm_mdio, phyaddr, 5);
  445. sbmac_mii_senddata(sbm_mdio, regidx, 5);
  446. sbmac_mii_senddata(sbm_mdio, MII_COMMAND_ACK, 2);
  447. sbmac_mii_senddata(sbm_mdio, regval, 16);
  448. mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
  449. __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, sbm_mdio);
  450. return 0;
  451. }
  452. /**********************************************************************
  453. * SBDMA_INITCTX(d,s,chan,txrx,maxdescr)
  454. *
  455. * Initialize a DMA channel context. Since there are potentially
  456. * eight DMA channels per MAC, it's nice to do this in a standard
  457. * way.
  458. *
  459. * Input parameters:
  460. * d - struct sbmacdma (DMA channel context)
  461. * s - struct sbmac_softc (pointer to a MAC)
  462. * chan - channel number (0..1 right now)
  463. * txrx - Identifies DMA_TX or DMA_RX for channel direction
  464. * maxdescr - number of descriptors
  465. *
  466. * Return value:
  467. * nothing
  468. ********************************************************************* */
  469. static void sbdma_initctx(struct sbmacdma *d, struct sbmac_softc *s, int chan,
  470. int txrx, int maxdescr)
  471. {
  472. #ifdef CONFIG_SBMAC_COALESCE
  473. int int_pktcnt, int_timeout;
  474. #endif
  475. /*
  476. * Save away interesting stuff in the structure
  477. */
  478. d->sbdma_eth = s;
  479. d->sbdma_channel = chan;
  480. d->sbdma_txdir = txrx;
  481. #if 0
  482. /* RMON clearing */
  483. s->sbe_idx =(s->sbm_base - A_MAC_BASE_0)/MAC_SPACING;
  484. #endif
  485. __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_BYTES);
  486. __raw_writeq(0, s->sbm_base + R_MAC_RMON_COLLISIONS);
  487. __raw_writeq(0, s->sbm_base + R_MAC_RMON_LATE_COL);
  488. __raw_writeq(0, s->sbm_base + R_MAC_RMON_EX_COL);
  489. __raw_writeq(0, s->sbm_base + R_MAC_RMON_FCS_ERROR);
  490. __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_ABORT);
  491. __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_BAD);
  492. __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_GOOD);
  493. __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_RUNT);
  494. __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_OVERSIZE);
  495. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BYTES);
  496. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_MCAST);
  497. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BCAST);
  498. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BAD);
  499. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_GOOD);
  500. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_RUNT);
  501. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_OVERSIZE);
  502. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_FCS_ERROR);
  503. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_LENGTH_ERROR);
  504. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_CODE_ERROR);
  505. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_ALIGN_ERROR);
  506. /*
  507. * initialize register pointers
  508. */
  509. d->sbdma_config0 =
  510. s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CONFIG0);
  511. d->sbdma_config1 =
  512. s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CONFIG1);
  513. d->sbdma_dscrbase =
  514. s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_DSCR_BASE);
  515. d->sbdma_dscrcnt =
  516. s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_DSCR_CNT);
  517. d->sbdma_curdscr =
  518. s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CUR_DSCRADDR);
  519. if (d->sbdma_txdir)
  520. d->sbdma_oodpktlost = NULL;
  521. else
  522. d->sbdma_oodpktlost =
  523. s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_OODPKTLOST_RX);
  524. /*
  525. * Allocate memory for the ring
  526. */
  527. d->sbdma_maxdescr = maxdescr;
  528. d->sbdma_dscrtable_unaligned = kcalloc(d->sbdma_maxdescr + 1,
  529. sizeof(*d->sbdma_dscrtable),
  530. GFP_KERNEL);
  531. /*
  532. * The descriptor table must be aligned to at least 16 bytes or the
  533. * MAC will corrupt it.
  534. */
  535. d->sbdma_dscrtable = (struct sbdmadscr *)
  536. ALIGN((unsigned long)d->sbdma_dscrtable_unaligned,
  537. sizeof(*d->sbdma_dscrtable));
  538. d->sbdma_dscrtable_end = d->sbdma_dscrtable + d->sbdma_maxdescr;
  539. d->sbdma_dscrtable_phys = virt_to_phys(d->sbdma_dscrtable);
  540. /*
  541. * And context table
  542. */
  543. d->sbdma_ctxtable = kcalloc(d->sbdma_maxdescr,
  544. sizeof(*d->sbdma_ctxtable), GFP_KERNEL);
  545. #ifdef CONFIG_SBMAC_COALESCE
  546. /*
  547. * Setup Rx/Tx DMA coalescing defaults
  548. */
  549. int_pktcnt = (txrx == DMA_TX) ? int_pktcnt_tx : int_pktcnt_rx;
  550. if ( int_pktcnt ) {
  551. d->sbdma_int_pktcnt = int_pktcnt;
  552. } else {
  553. d->sbdma_int_pktcnt = 1;
  554. }
  555. int_timeout = (txrx == DMA_TX) ? int_timeout_tx : int_timeout_rx;
  556. if ( int_timeout ) {
  557. d->sbdma_int_timeout = int_timeout;
  558. } else {
  559. d->sbdma_int_timeout = 0;
  560. }
  561. #endif
  562. }
  563. /**********************************************************************
  564. * SBDMA_CHANNEL_START(d)
  565. *
  566. * Initialize the hardware registers for a DMA channel.
  567. *
  568. * Input parameters:
  569. * d - DMA channel to init (context must be previously init'd
  570. * rxtx - DMA_RX or DMA_TX depending on what type of channel
  571. *
  572. * Return value:
  573. * nothing
  574. ********************************************************************* */
  575. static void sbdma_channel_start(struct sbmacdma *d, int rxtx)
  576. {
  577. /*
  578. * Turn on the DMA channel
  579. */
  580. #ifdef CONFIG_SBMAC_COALESCE
  581. __raw_writeq(V_DMA_INT_TIMEOUT(d->sbdma_int_timeout) |
  582. 0, d->sbdma_config1);
  583. __raw_writeq(M_DMA_EOP_INT_EN |
  584. V_DMA_RINGSZ(d->sbdma_maxdescr) |
  585. V_DMA_INT_PKTCNT(d->sbdma_int_pktcnt) |
  586. 0, d->sbdma_config0);
  587. #else
  588. __raw_writeq(0, d->sbdma_config1);
  589. __raw_writeq(V_DMA_RINGSZ(d->sbdma_maxdescr) |
  590. 0, d->sbdma_config0);
  591. #endif
  592. __raw_writeq(d->sbdma_dscrtable_phys, d->sbdma_dscrbase);
  593. /*
  594. * Initialize ring pointers
  595. */
  596. d->sbdma_addptr = d->sbdma_dscrtable;
  597. d->sbdma_remptr = d->sbdma_dscrtable;
  598. }
  599. /**********************************************************************
  600. * SBDMA_CHANNEL_STOP(d)
  601. *
  602. * Initialize the hardware registers for a DMA channel.
  603. *
  604. * Input parameters:
  605. * d - DMA channel to init (context must be previously init'd
  606. *
  607. * Return value:
  608. * nothing
  609. ********************************************************************* */
  610. static void sbdma_channel_stop(struct sbmacdma *d)
  611. {
  612. /*
  613. * Turn off the DMA channel
  614. */
  615. __raw_writeq(0, d->sbdma_config1);
  616. __raw_writeq(0, d->sbdma_dscrbase);
  617. __raw_writeq(0, d->sbdma_config0);
  618. /*
  619. * Zero ring pointers
  620. */
  621. d->sbdma_addptr = NULL;
  622. d->sbdma_remptr = NULL;
  623. }
  624. static inline void sbdma_align_skb(struct sk_buff *skb,
  625. unsigned int power2, unsigned int offset)
  626. {
  627. unsigned char *addr = skb->data;
  628. unsigned char *newaddr = PTR_ALIGN(addr, power2);
  629. skb_reserve(skb, newaddr - addr + offset);
  630. }
  631. /**********************************************************************
  632. * SBDMA_ADD_RCVBUFFER(d,sb)
  633. *
  634. * Add a buffer to the specified DMA channel. For receive channels,
  635. * this queues a buffer for inbound packets.
  636. *
  637. * Input parameters:
  638. * sc - softc structure
  639. * d - DMA channel descriptor
  640. * sb - sk_buff to add, or NULL if we should allocate one
  641. *
  642. * Return value:
  643. * 0 if buffer could not be added (ring is full)
  644. * 1 if buffer added successfully
  645. ********************************************************************* */
  646. static int sbdma_add_rcvbuffer(struct sbmac_softc *sc, struct sbmacdma *d,
  647. struct sk_buff *sb)
  648. {
  649. struct net_device *dev = sc->sbm_dev;
  650. struct sbdmadscr *dsc;
  651. struct sbdmadscr *nextdsc;
  652. struct sk_buff *sb_new = NULL;
  653. int pktsize = ENET_PACKET_SIZE;
  654. /* get pointer to our current place in the ring */
  655. dsc = d->sbdma_addptr;
  656. nextdsc = SBDMA_NEXTBUF(d,sbdma_addptr);
  657. /*
  658. * figure out if the ring is full - if the next descriptor
  659. * is the same as the one that we're going to remove from
  660. * the ring, the ring is full
  661. */
  662. if (nextdsc == d->sbdma_remptr) {
  663. return -ENOSPC;
  664. }
  665. /*
  666. * Allocate a sk_buff if we don't already have one.
  667. * If we do have an sk_buff, reset it so that it's empty.
  668. *
  669. * Note: sk_buffs don't seem to be guaranteed to have any sort
  670. * of alignment when they are allocated. Therefore, allocate enough
  671. * extra space to make sure that:
  672. *
  673. * 1. the data does not start in the middle of a cache line.
  674. * 2. The data does not end in the middle of a cache line
  675. * 3. The buffer can be aligned such that the IP addresses are
  676. * naturally aligned.
  677. *
  678. * Remember, the SOCs MAC writes whole cache lines at a time,
  679. * without reading the old contents first. So, if the sk_buff's
  680. * data portion starts in the middle of a cache line, the SOC
  681. * DMA will trash the beginning (and ending) portions.
  682. */
  683. if (sb == NULL) {
  684. sb_new = netdev_alloc_skb(dev, ENET_PACKET_SIZE +
  685. SMP_CACHE_BYTES * 2 +
  686. NET_IP_ALIGN);
  687. if (sb_new == NULL)
  688. return -ENOBUFS;
  689. sbdma_align_skb(sb_new, SMP_CACHE_BYTES, NET_IP_ALIGN);
  690. }
  691. else {
  692. sb_new = sb;
  693. /*
  694. * nothing special to reinit buffer, it's already aligned
  695. * and sb->data already points to a good place.
  696. */
  697. }
  698. /*
  699. * fill in the descriptor
  700. */
  701. #ifdef CONFIG_SBMAC_COALESCE
  702. /*
  703. * Do not interrupt per DMA transfer.
  704. */
  705. dsc->dscr_a = virt_to_phys(sb_new->data) |
  706. V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize + NET_IP_ALIGN)) | 0;
  707. #else
  708. dsc->dscr_a = virt_to_phys(sb_new->data) |
  709. V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize + NET_IP_ALIGN)) |
  710. M_DMA_DSCRA_INTERRUPT;
  711. #endif
  712. /* receiving: no options */
  713. dsc->dscr_b = 0;
  714. /*
  715. * fill in the context
  716. */
  717. d->sbdma_ctxtable[dsc-d->sbdma_dscrtable] = sb_new;
  718. /*
  719. * point at next packet
  720. */
  721. d->sbdma_addptr = nextdsc;
  722. /*
  723. * Give the buffer to the DMA engine.
  724. */
  725. __raw_writeq(1, d->sbdma_dscrcnt);
  726. return 0; /* we did it */
  727. }
  728. /**********************************************************************
  729. * SBDMA_ADD_TXBUFFER(d,sb)
  730. *
  731. * Add a transmit buffer to the specified DMA channel, causing a
  732. * transmit to start.
  733. *
  734. * Input parameters:
  735. * d - DMA channel descriptor
  736. * sb - sk_buff to add
  737. *
  738. * Return value:
  739. * 0 transmit queued successfully
  740. * otherwise error code
  741. ********************************************************************* */
  742. static int sbdma_add_txbuffer(struct sbmacdma *d, struct sk_buff *sb)
  743. {
  744. struct sbdmadscr *dsc;
  745. struct sbdmadscr *nextdsc;
  746. uint64_t phys;
  747. uint64_t ncb;
  748. int length;
  749. /* get pointer to our current place in the ring */
  750. dsc = d->sbdma_addptr;
  751. nextdsc = SBDMA_NEXTBUF(d,sbdma_addptr);
  752. /*
  753. * figure out if the ring is full - if the next descriptor
  754. * is the same as the one that we're going to remove from
  755. * the ring, the ring is full
  756. */
  757. if (nextdsc == d->sbdma_remptr) {
  758. return -ENOSPC;
  759. }
  760. /*
  761. * Under Linux, it's not necessary to copy/coalesce buffers
  762. * like it is on NetBSD. We think they're all contiguous,
  763. * but that may not be true for GBE.
  764. */
  765. length = sb->len;
  766. /*
  767. * fill in the descriptor. Note that the number of cache
  768. * blocks in the descriptor is the number of blocks
  769. * *spanned*, so we need to add in the offset (if any)
  770. * while doing the calculation.
  771. */
  772. phys = virt_to_phys(sb->data);
  773. ncb = NUMCACHEBLKS(length+(phys & (SMP_CACHE_BYTES - 1)));
  774. dsc->dscr_a = phys |
  775. V_DMA_DSCRA_A_SIZE(ncb) |
  776. #ifndef CONFIG_SBMAC_COALESCE
  777. M_DMA_DSCRA_INTERRUPT |
  778. #endif
  779. M_DMA_ETHTX_SOP;
  780. /* transmitting: set outbound options and length */
  781. dsc->dscr_b = V_DMA_DSCRB_OPTIONS(K_DMA_ETHTX_APPENDCRC_APPENDPAD) |
  782. V_DMA_DSCRB_PKT_SIZE(length);
  783. /*
  784. * fill in the context
  785. */
  786. d->sbdma_ctxtable[dsc-d->sbdma_dscrtable] = sb;
  787. /*
  788. * point at next packet
  789. */
  790. d->sbdma_addptr = nextdsc;
  791. /*
  792. * Give the buffer to the DMA engine.
  793. */
  794. __raw_writeq(1, d->sbdma_dscrcnt);
  795. return 0; /* we did it */
  796. }
  797. /**********************************************************************
  798. * SBDMA_EMPTYRING(d)
  799. *
  800. * Free all allocated sk_buffs on the specified DMA channel;
  801. *
  802. * Input parameters:
  803. * d - DMA channel
  804. *
  805. * Return value:
  806. * nothing
  807. ********************************************************************* */
  808. static void sbdma_emptyring(struct sbmacdma *d)
  809. {
  810. int idx;
  811. struct sk_buff *sb;
  812. for (idx = 0; idx < d->sbdma_maxdescr; idx++) {
  813. sb = d->sbdma_ctxtable[idx];
  814. if (sb) {
  815. dev_kfree_skb(sb);
  816. d->sbdma_ctxtable[idx] = NULL;
  817. }
  818. }
  819. }
  820. /**********************************************************************
  821. * SBDMA_FILLRING(d)
  822. *
  823. * Fill the specified DMA channel (must be receive channel)
  824. * with sk_buffs
  825. *
  826. * Input parameters:
  827. * sc - softc structure
  828. * d - DMA channel
  829. *
  830. * Return value:
  831. * nothing
  832. ********************************************************************* */
  833. static void sbdma_fillring(struct sbmac_softc *sc, struct sbmacdma *d)
  834. {
  835. int idx;
  836. for (idx = 0; idx < SBMAC_MAX_RXDESCR - 1; idx++) {
  837. if (sbdma_add_rcvbuffer(sc, d, NULL) != 0)
  838. break;
  839. }
  840. }
  841. #ifdef CONFIG_NET_POLL_CONTROLLER
  842. static void sbmac_netpoll(struct net_device *netdev)
  843. {
  844. struct sbmac_softc *sc = netdev_priv(netdev);
  845. int irq = sc->sbm_dev->irq;
  846. __raw_writeq(0, sc->sbm_imr);
  847. sbmac_intr(irq, netdev);
  848. #ifdef CONFIG_SBMAC_COALESCE
  849. __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
  850. ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0),
  851. sc->sbm_imr);
  852. #else
  853. __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
  854. (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), sc->sbm_imr);
  855. #endif
  856. }
  857. #endif
  858. /**********************************************************************
  859. * SBDMA_RX_PROCESS(sc,d,work_to_do,poll)
  860. *
  861. * Process "completed" receive buffers on the specified DMA channel.
  862. *
  863. * Input parameters:
  864. * sc - softc structure
  865. * d - DMA channel context
  866. * work_to_do - no. of packets to process before enabling interrupt
  867. * again (for NAPI)
  868. * poll - 1: using polling (for NAPI)
  869. *
  870. * Return value:
  871. * nothing
  872. ********************************************************************* */
  873. static int sbdma_rx_process(struct sbmac_softc *sc, struct sbmacdma *d,
  874. int work_to_do, int poll)
  875. {
  876. struct net_device *dev = sc->sbm_dev;
  877. int curidx;
  878. int hwidx;
  879. struct sbdmadscr *dsc;
  880. struct sk_buff *sb;
  881. int len;
  882. int work_done = 0;
  883. int dropped = 0;
  884. prefetch(d);
  885. again:
  886. /* Check if the HW dropped any frames */
  887. dev->stats.rx_fifo_errors
  888. += __raw_readq(sc->sbm_rxdma.sbdma_oodpktlost) & 0xffff;
  889. __raw_writeq(0, sc->sbm_rxdma.sbdma_oodpktlost);
  890. while (work_to_do-- > 0) {
  891. /*
  892. * figure out where we are (as an index) and where
  893. * the hardware is (also as an index)
  894. *
  895. * This could be done faster if (for example) the
  896. * descriptor table was page-aligned and contiguous in
  897. * both virtual and physical memory -- you could then
  898. * just compare the low-order bits of the virtual address
  899. * (sbdma_remptr) and the physical address (sbdma_curdscr CSR)
  900. */
  901. dsc = d->sbdma_remptr;
  902. curidx = dsc - d->sbdma_dscrtable;
  903. prefetch(dsc);
  904. prefetch(&d->sbdma_ctxtable[curidx]);
  905. hwidx = ((__raw_readq(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
  906. d->sbdma_dscrtable_phys) /
  907. sizeof(*d->sbdma_dscrtable);
  908. /*
  909. * If they're the same, that means we've processed all
  910. * of the descriptors up to (but not including) the one that
  911. * the hardware is working on right now.
  912. */
  913. if (curidx == hwidx)
  914. goto done;
  915. /*
  916. * Otherwise, get the packet's sk_buff ptr back
  917. */
  918. sb = d->sbdma_ctxtable[curidx];
  919. d->sbdma_ctxtable[curidx] = NULL;
  920. len = (int)G_DMA_DSCRB_PKT_SIZE(dsc->dscr_b) - 4;
  921. /*
  922. * Check packet status. If good, process it.
  923. * If not, silently drop it and put it back on the
  924. * receive ring.
  925. */
  926. if (likely (!(dsc->dscr_a & M_DMA_ETHRX_BAD))) {
  927. /*
  928. * Add a new buffer to replace the old one. If we fail
  929. * to allocate a buffer, we're going to drop this
  930. * packet and put it right back on the receive ring.
  931. */
  932. if (unlikely(sbdma_add_rcvbuffer(sc, d, NULL) ==
  933. -ENOBUFS)) {
  934. dev->stats.rx_dropped++;
  935. /* Re-add old buffer */
  936. sbdma_add_rcvbuffer(sc, d, sb);
  937. /* No point in continuing at the moment */
  938. printk(KERN_ERR "dropped packet (1)\n");
  939. d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
  940. goto done;
  941. } else {
  942. /*
  943. * Set length into the packet
  944. */
  945. skb_put(sb,len);
  946. /*
  947. * Buffer has been replaced on the
  948. * receive ring. Pass the buffer to
  949. * the kernel
  950. */
  951. sb->protocol = eth_type_trans(sb,d->sbdma_eth->sbm_dev);
  952. /* Check hw IPv4/TCP checksum if supported */
  953. if (sc->rx_hw_checksum == ENABLE) {
  954. if (!((dsc->dscr_a) & M_DMA_ETHRX_BADIP4CS) &&
  955. !((dsc->dscr_a) & M_DMA_ETHRX_BADTCPCS)) {
  956. sb->ip_summed = CHECKSUM_UNNECESSARY;
  957. /* don't need to set sb->csum */
  958. } else {
  959. skb_checksum_none_assert(sb);
  960. }
  961. }
  962. prefetch(sb->data);
  963. prefetch((const void *)(((char *)sb->data)+32));
  964. if (poll)
  965. dropped = netif_receive_skb(sb);
  966. else
  967. dropped = netif_rx(sb);
  968. if (dropped == NET_RX_DROP) {
  969. dev->stats.rx_dropped++;
  970. d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
  971. goto done;
  972. }
  973. else {
  974. dev->stats.rx_bytes += len;
  975. dev->stats.rx_packets++;
  976. }
  977. }
  978. } else {
  979. /*
  980. * Packet was mangled somehow. Just drop it and
  981. * put it back on the receive ring.
  982. */
  983. dev->stats.rx_errors++;
  984. sbdma_add_rcvbuffer(sc, d, sb);
  985. }
  986. /*
  987. * .. and advance to the next buffer.
  988. */
  989. d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
  990. work_done++;
  991. }
  992. if (!poll) {
  993. work_to_do = 32;
  994. goto again; /* collect fifo drop statistics again */
  995. }
  996. done:
  997. return work_done;
  998. }
  999. /**********************************************************************
  1000. * SBDMA_TX_PROCESS(sc,d)
  1001. *
  1002. * Process "completed" transmit buffers on the specified DMA channel.
  1003. * This is normally called within the interrupt service routine.
  1004. * Note that this isn't really ideal for priority channels, since
  1005. * it processes all of the packets on a given channel before
  1006. * returning.
  1007. *
  1008. * Input parameters:
  1009. * sc - softc structure
  1010. * d - DMA channel context
  1011. * poll - 1: using polling (for NAPI)
  1012. *
  1013. * Return value:
  1014. * nothing
  1015. ********************************************************************* */
  1016. static void sbdma_tx_process(struct sbmac_softc *sc, struct sbmacdma *d,
  1017. int poll)
  1018. {
  1019. struct net_device *dev = sc->sbm_dev;
  1020. int curidx;
  1021. int hwidx;
  1022. struct sbdmadscr *dsc;
  1023. struct sk_buff *sb;
  1024. unsigned long flags;
  1025. int packets_handled = 0;
  1026. spin_lock_irqsave(&(sc->sbm_lock), flags);
  1027. if (d->sbdma_remptr == d->sbdma_addptr)
  1028. goto end_unlock;
  1029. hwidx = ((__raw_readq(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
  1030. d->sbdma_dscrtable_phys) / sizeof(*d->sbdma_dscrtable);
  1031. for (;;) {
  1032. /*
  1033. * figure out where we are (as an index) and where
  1034. * the hardware is (also as an index)
  1035. *
  1036. * This could be done faster if (for example) the
  1037. * descriptor table was page-aligned and contiguous in
  1038. * both virtual and physical memory -- you could then
  1039. * just compare the low-order bits of the virtual address
  1040. * (sbdma_remptr) and the physical address (sbdma_curdscr CSR)
  1041. */
  1042. curidx = d->sbdma_remptr - d->sbdma_dscrtable;
  1043. /*
  1044. * If they're the same, that means we've processed all
  1045. * of the descriptors up to (but not including) the one that
  1046. * the hardware is working on right now.
  1047. */
  1048. if (curidx == hwidx)
  1049. break;
  1050. /*
  1051. * Otherwise, get the packet's sk_buff ptr back
  1052. */
  1053. dsc = &(d->sbdma_dscrtable[curidx]);
  1054. sb = d->sbdma_ctxtable[curidx];
  1055. d->sbdma_ctxtable[curidx] = NULL;
  1056. /*
  1057. * Stats
  1058. */
  1059. dev->stats.tx_bytes += sb->len;
  1060. dev->stats.tx_packets++;
  1061. /*
  1062. * for transmits, we just free buffers.
  1063. */
  1064. dev_kfree_skb_irq(sb);
  1065. /*
  1066. * .. and advance to the next buffer.
  1067. */
  1068. d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
  1069. packets_handled++;
  1070. }
  1071. /*
  1072. * Decide if we should wake up the protocol or not.
  1073. * Other drivers seem to do this when we reach a low
  1074. * watermark on the transmit queue.
  1075. */
  1076. if (packets_handled)
  1077. netif_wake_queue(d->sbdma_eth->sbm_dev);
  1078. end_unlock:
  1079. spin_unlock_irqrestore(&(sc->sbm_lock), flags);
  1080. }
  1081. /**********************************************************************
  1082. * SBMAC_INITCTX(s)
  1083. *
  1084. * Initialize an Ethernet context structure - this is called
  1085. * once per MAC on the 1250. Memory is allocated here, so don't
  1086. * call it again from inside the ioctl routines that bring the
  1087. * interface up/down
  1088. *
  1089. * Input parameters:
  1090. * s - sbmac context structure
  1091. *
  1092. * Return value:
  1093. * 0
  1094. ********************************************************************* */
  1095. static int sbmac_initctx(struct sbmac_softc *s)
  1096. {
  1097. /*
  1098. * figure out the addresses of some ports
  1099. */
  1100. s->sbm_macenable = s->sbm_base + R_MAC_ENABLE;
  1101. s->sbm_maccfg = s->sbm_base + R_MAC_CFG;
  1102. s->sbm_fifocfg = s->sbm_base + R_MAC_THRSH_CFG;
  1103. s->sbm_framecfg = s->sbm_base + R_MAC_FRAMECFG;
  1104. s->sbm_rxfilter = s->sbm_base + R_MAC_ADFILTER_CFG;
  1105. s->sbm_isr = s->sbm_base + R_MAC_STATUS;
  1106. s->sbm_imr = s->sbm_base + R_MAC_INT_MASK;
  1107. s->sbm_mdio = s->sbm_base + R_MAC_MDIO;
  1108. /*
  1109. * Initialize the DMA channels. Right now, only one per MAC is used
  1110. * Note: Only do this _once_, as it allocates memory from the kernel!
  1111. */
  1112. sbdma_initctx(&(s->sbm_txdma),s,0,DMA_TX,SBMAC_MAX_TXDESCR);
  1113. sbdma_initctx(&(s->sbm_rxdma),s,0,DMA_RX,SBMAC_MAX_RXDESCR);
  1114. /*
  1115. * initial state is OFF
  1116. */
  1117. s->sbm_state = sbmac_state_off;
  1118. return 0;
  1119. }
  1120. static void sbdma_uninitctx(struct sbmacdma *d)
  1121. {
  1122. if (d->sbdma_dscrtable_unaligned) {
  1123. kfree(d->sbdma_dscrtable_unaligned);
  1124. d->sbdma_dscrtable_unaligned = d->sbdma_dscrtable = NULL;
  1125. }
  1126. if (d->sbdma_ctxtable) {
  1127. kfree(d->sbdma_ctxtable);
  1128. d->sbdma_ctxtable = NULL;
  1129. }
  1130. }
  1131. static void sbmac_uninitctx(struct sbmac_softc *sc)
  1132. {
  1133. sbdma_uninitctx(&(sc->sbm_txdma));
  1134. sbdma_uninitctx(&(sc->sbm_rxdma));
  1135. }
  1136. /**********************************************************************
  1137. * SBMAC_CHANNEL_START(s)
  1138. *
  1139. * Start packet processing on this MAC.
  1140. *
  1141. * Input parameters:
  1142. * s - sbmac structure
  1143. *
  1144. * Return value:
  1145. * nothing
  1146. ********************************************************************* */
  1147. static void sbmac_channel_start(struct sbmac_softc *s)
  1148. {
  1149. uint64_t reg;
  1150. void __iomem *port;
  1151. uint64_t cfg,fifo,framecfg;
  1152. int idx, th_value;
  1153. /*
  1154. * Don't do this if running
  1155. */
  1156. if (s->sbm_state == sbmac_state_on)
  1157. return;
  1158. /*
  1159. * Bring the controller out of reset, but leave it off.
  1160. */
  1161. __raw_writeq(0, s->sbm_macenable);
  1162. /*
  1163. * Ignore all received packets
  1164. */
  1165. __raw_writeq(0, s->sbm_rxfilter);
  1166. /*
  1167. * Calculate values for various control registers.
  1168. */
  1169. cfg = M_MAC_RETRY_EN |
  1170. M_MAC_TX_HOLD_SOP_EN |
  1171. V_MAC_TX_PAUSE_CNT_16K |
  1172. M_MAC_AP_STAT_EN |
  1173. M_MAC_FAST_SYNC |
  1174. M_MAC_SS_EN |
  1175. 0;
  1176. /*
  1177. * Be sure that RD_THRSH+WR_THRSH <= 32 for pass1 pars
  1178. * and make sure that RD_THRSH + WR_THRSH <=128 for pass2 and above
  1179. * Use a larger RD_THRSH for gigabit
  1180. */
  1181. if (soc_type == K_SYS_SOC_TYPE_BCM1250 && periph_rev < 2)
  1182. th_value = 28;
  1183. else
  1184. th_value = 64;
  1185. fifo = V_MAC_TX_WR_THRSH(4) | /* Must be '4' or '8' */
  1186. ((s->sbm_speed == sbmac_speed_1000)
  1187. ? V_MAC_TX_RD_THRSH(th_value) : V_MAC_TX_RD_THRSH(4)) |
  1188. V_MAC_TX_RL_THRSH(4) |
  1189. V_MAC_RX_PL_THRSH(4) |
  1190. V_MAC_RX_RD_THRSH(4) | /* Must be '4' */
  1191. V_MAC_RX_RL_THRSH(8) |
  1192. 0;
  1193. framecfg = V_MAC_MIN_FRAMESZ_DEFAULT |
  1194. V_MAC_MAX_FRAMESZ_DEFAULT |
  1195. V_MAC_BACKOFF_SEL(1);
  1196. /*
  1197. * Clear out the hash address map
  1198. */
  1199. port = s->sbm_base + R_MAC_HASH_BASE;
  1200. for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
  1201. __raw_writeq(0, port);
  1202. port += sizeof(uint64_t);
  1203. }
  1204. /*
  1205. * Clear out the exact-match table
  1206. */
  1207. port = s->sbm_base + R_MAC_ADDR_BASE;
  1208. for (idx = 0; idx < MAC_ADDR_COUNT; idx++) {
  1209. __raw_writeq(0, port);
  1210. port += sizeof(uint64_t);
  1211. }
  1212. /*
  1213. * Clear out the DMA Channel mapping table registers
  1214. */
  1215. port = s->sbm_base + R_MAC_CHUP0_BASE;
  1216. for (idx = 0; idx < MAC_CHMAP_COUNT; idx++) {
  1217. __raw_writeq(0, port);
  1218. port += sizeof(uint64_t);
  1219. }
  1220. port = s->sbm_base + R_MAC_CHLO0_BASE;
  1221. for (idx = 0; idx < MAC_CHMAP_COUNT; idx++) {
  1222. __raw_writeq(0, port);
  1223. port += sizeof(uint64_t);
  1224. }
  1225. /*
  1226. * Program the hardware address. It goes into the hardware-address
  1227. * register as well as the first filter register.
  1228. */
  1229. reg = sbmac_addr2reg(s->sbm_hwaddr);
  1230. port = s->sbm_base + R_MAC_ADDR_BASE;
  1231. __raw_writeq(reg, port);
  1232. port = s->sbm_base + R_MAC_ETHERNET_ADDR;
  1233. __raw_writeq(reg, port);
  1234. /*
  1235. * Set the receive filter for no packets, and write values
  1236. * to the various config registers
  1237. */
  1238. __raw_writeq(0, s->sbm_rxfilter);
  1239. __raw_writeq(0, s->sbm_imr);
  1240. __raw_writeq(framecfg, s->sbm_framecfg);
  1241. __raw_writeq(fifo, s->sbm_fifocfg);
  1242. __raw_writeq(cfg, s->sbm_maccfg);
  1243. /*
  1244. * Initialize DMA channels (rings should be ok now)
  1245. */
  1246. sbdma_channel_start(&(s->sbm_rxdma), DMA_RX);
  1247. sbdma_channel_start(&(s->sbm_txdma), DMA_TX);
  1248. /*
  1249. * Configure the speed, duplex, and flow control
  1250. */
  1251. sbmac_set_speed(s,s->sbm_speed);
  1252. sbmac_set_duplex(s,s->sbm_duplex,s->sbm_fc);
  1253. /*
  1254. * Fill the receive ring
  1255. */
  1256. sbdma_fillring(s, &(s->sbm_rxdma));
  1257. /*
  1258. * Turn on the rest of the bits in the enable register
  1259. */
  1260. #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
  1261. __raw_writeq(M_MAC_RXDMA_EN0 |
  1262. M_MAC_TXDMA_EN0, s->sbm_macenable);
  1263. #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
  1264. __raw_writeq(M_MAC_RXDMA_EN0 |
  1265. M_MAC_TXDMA_EN0 |
  1266. M_MAC_RX_ENABLE |
  1267. M_MAC_TX_ENABLE, s->sbm_macenable);
  1268. #else
  1269. #error invalid SiByte MAC configuration
  1270. #endif
  1271. #ifdef CONFIG_SBMAC_COALESCE
  1272. __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
  1273. ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0), s->sbm_imr);
  1274. #else
  1275. __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
  1276. (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), s->sbm_imr);
  1277. #endif
  1278. /*
  1279. * Enable receiving unicasts and broadcasts
  1280. */
  1281. __raw_writeq(M_MAC_UCAST_EN | M_MAC_BCAST_EN, s->sbm_rxfilter);
  1282. /*
  1283. * we're running now.
  1284. */
  1285. s->sbm_state = sbmac_state_on;
  1286. /*
  1287. * Program multicast addresses
  1288. */
  1289. sbmac_setmulti(s);
  1290. /*
  1291. * If channel was in promiscuous mode before, turn that on
  1292. */
  1293. if (s->sbm_devflags & IFF_PROMISC) {
  1294. sbmac_promiscuous_mode(s,1);
  1295. }
  1296. }
  1297. /**********************************************************************
  1298. * SBMAC_CHANNEL_STOP(s)
  1299. *
  1300. * Stop packet processing on this MAC.
  1301. *
  1302. * Input parameters:
  1303. * s - sbmac structure
  1304. *
  1305. * Return value:
  1306. * nothing
  1307. ********************************************************************* */
  1308. static void sbmac_channel_stop(struct sbmac_softc *s)
  1309. {
  1310. /* don't do this if already stopped */
  1311. if (s->sbm_state == sbmac_state_off)
  1312. return;
  1313. /* don't accept any packets, disable all interrupts */
  1314. __raw_writeq(0, s->sbm_rxfilter);
  1315. __raw_writeq(0, s->sbm_imr);
  1316. /* Turn off ticker */
  1317. /* XXX */
  1318. /* turn off receiver and transmitter */
  1319. __raw_writeq(0, s->sbm_macenable);
  1320. /* We're stopped now. */
  1321. s->sbm_state = sbmac_state_off;
  1322. /*
  1323. * Stop DMA channels (rings should be ok now)
  1324. */
  1325. sbdma_channel_stop(&(s->sbm_rxdma));
  1326. sbdma_channel_stop(&(s->sbm_txdma));
  1327. /* Empty the receive and transmit rings */
  1328. sbdma_emptyring(&(s->sbm_rxdma));
  1329. sbdma_emptyring(&(s->sbm_txdma));
  1330. }
  1331. /**********************************************************************
  1332. * SBMAC_SET_CHANNEL_STATE(state)
  1333. *
  1334. * Set the channel's state ON or OFF
  1335. *
  1336. * Input parameters:
  1337. * state - new state
  1338. *
  1339. * Return value:
  1340. * old state
  1341. ********************************************************************* */
  1342. static enum sbmac_state sbmac_set_channel_state(struct sbmac_softc *sc,
  1343. enum sbmac_state state)
  1344. {
  1345. enum sbmac_state oldstate = sc->sbm_state;
  1346. /*
  1347. * If same as previous state, return
  1348. */
  1349. if (state == oldstate) {
  1350. return oldstate;
  1351. }
  1352. /*
  1353. * If new state is ON, turn channel on
  1354. */
  1355. if (state == sbmac_state_on) {
  1356. sbmac_channel_start(sc);
  1357. }
  1358. else {
  1359. sbmac_channel_stop(sc);
  1360. }
  1361. /*
  1362. * Return previous state
  1363. */
  1364. return oldstate;
  1365. }
  1366. /**********************************************************************
  1367. * SBMAC_PROMISCUOUS_MODE(sc,onoff)
  1368. *
  1369. * Turn on or off promiscuous mode
  1370. *
  1371. * Input parameters:
  1372. * sc - softc
  1373. * onoff - 1 to turn on, 0 to turn off
  1374. *
  1375. * Return value:
  1376. * nothing
  1377. ********************************************************************* */
  1378. static void sbmac_promiscuous_mode(struct sbmac_softc *sc,int onoff)
  1379. {
  1380. uint64_t reg;
  1381. if (sc->sbm_state != sbmac_state_on)
  1382. return;
  1383. if (onoff) {
  1384. reg = __raw_readq(sc->sbm_rxfilter);
  1385. reg |= M_MAC_ALLPKT_EN;
  1386. __raw_writeq(reg, sc->sbm_rxfilter);
  1387. }
  1388. else {
  1389. reg = __raw_readq(sc->sbm_rxfilter);
  1390. reg &= ~M_MAC_ALLPKT_EN;
  1391. __raw_writeq(reg, sc->sbm_rxfilter);
  1392. }
  1393. }
  1394. /**********************************************************************
  1395. * SBMAC_SETIPHDR_OFFSET(sc,onoff)
  1396. *
  1397. * Set the iphdr offset as 15 assuming ethernet encapsulation
  1398. *
  1399. * Input parameters:
  1400. * sc - softc
  1401. *
  1402. * Return value:
  1403. * nothing
  1404. ********************************************************************* */
  1405. static void sbmac_set_iphdr_offset(struct sbmac_softc *sc)
  1406. {
  1407. uint64_t reg;
  1408. /* Hard code the off set to 15 for now */
  1409. reg = __raw_readq(sc->sbm_rxfilter);
  1410. reg &= ~M_MAC_IPHDR_OFFSET | V_MAC_IPHDR_OFFSET(15);
  1411. __raw_writeq(reg, sc->sbm_rxfilter);
  1412. /* BCM1250 pass1 didn't have hardware checksum. Everything
  1413. later does. */
  1414. if (soc_type == K_SYS_SOC_TYPE_BCM1250 && periph_rev < 2) {
  1415. sc->rx_hw_checksum = DISABLE;
  1416. } else {
  1417. sc->rx_hw_checksum = ENABLE;
  1418. }
  1419. }
  1420. /**********************************************************************
  1421. * SBMAC_ADDR2REG(ptr)
  1422. *
  1423. * Convert six bytes into the 64-bit register value that
  1424. * we typically write into the SBMAC's address/mcast registers
  1425. *
  1426. * Input parameters:
  1427. * ptr - pointer to 6 bytes
  1428. *
  1429. * Return value:
  1430. * register value
  1431. ********************************************************************* */
  1432. static uint64_t sbmac_addr2reg(unsigned char *ptr)
  1433. {
  1434. uint64_t reg = 0;
  1435. ptr += 6;
  1436. reg |= (uint64_t) *(--ptr);
  1437. reg <<= 8;
  1438. reg |= (uint64_t) *(--ptr);
  1439. reg <<= 8;
  1440. reg |= (uint64_t) *(--ptr);
  1441. reg <<= 8;
  1442. reg |= (uint64_t) *(--ptr);
  1443. reg <<= 8;
  1444. reg |= (uint64_t) *(--ptr);
  1445. reg <<= 8;
  1446. reg |= (uint64_t) *(--ptr);
  1447. return reg;
  1448. }
  1449. /**********************************************************************
  1450. * SBMAC_SET_SPEED(s,speed)
  1451. *
  1452. * Configure LAN speed for the specified MAC.
  1453. * Warning: must be called when MAC is off!
  1454. *
  1455. * Input parameters:
  1456. * s - sbmac structure
  1457. * speed - speed to set MAC to (see enum sbmac_speed)
  1458. *
  1459. * Return value:
  1460. * 1 if successful
  1461. * 0 indicates invalid parameters
  1462. ********************************************************************* */
  1463. static int sbmac_set_speed(struct sbmac_softc *s, enum sbmac_speed speed)
  1464. {
  1465. uint64_t cfg;
  1466. uint64_t framecfg;
  1467. /*
  1468. * Save new current values
  1469. */
  1470. s->sbm_speed = speed;
  1471. if (s->sbm_state == sbmac_state_on)
  1472. return 0; /* save for next restart */
  1473. /*
  1474. * Read current register values
  1475. */
  1476. cfg = __raw_readq(s->sbm_maccfg);
  1477. framecfg = __raw_readq(s->sbm_framecfg);
  1478. /*
  1479. * Mask out the stuff we want to change
  1480. */
  1481. cfg &= ~(M_MAC_BURST_EN | M_MAC_SPEED_SEL);
  1482. framecfg &= ~(M_MAC_IFG_RX | M_MAC_IFG_TX | M_MAC_IFG_THRSH |
  1483. M_MAC_SLOT_SIZE);
  1484. /*
  1485. * Now add in the new bits
  1486. */
  1487. switch (speed) {
  1488. case sbmac_speed_10:
  1489. framecfg |= V_MAC_IFG_RX_10 |
  1490. V_MAC_IFG_TX_10 |
  1491. K_MAC_IFG_THRSH_10 |
  1492. V_MAC_SLOT_SIZE_10;
  1493. cfg |= V_MAC_SPEED_SEL_10MBPS;
  1494. break;
  1495. case sbmac_speed_100:
  1496. framecfg |= V_MAC_IFG_RX_100 |
  1497. V_MAC_IFG_TX_100 |
  1498. V_MAC_IFG_THRSH_100 |
  1499. V_MAC_SLOT_SIZE_100;
  1500. cfg |= V_MAC_SPEED_SEL_100MBPS ;
  1501. break;
  1502. case sbmac_speed_1000:
  1503. framecfg |= V_MAC_IFG_RX_1000 |
  1504. V_MAC_IFG_TX_1000 |
  1505. V_MAC_IFG_THRSH_1000 |
  1506. V_MAC_SLOT_SIZE_1000;
  1507. cfg |= V_MAC_SPEED_SEL_1000MBPS | M_MAC_BURST_EN;
  1508. break;
  1509. default:
  1510. return 0;
  1511. }
  1512. /*
  1513. * Send the bits back to the hardware
  1514. */
  1515. __raw_writeq(framecfg, s->sbm_framecfg);
  1516. __raw_writeq(cfg, s->sbm_maccfg);
  1517. return 1;
  1518. }
  1519. /**********************************************************************
  1520. * SBMAC_SET_DUPLEX(s,duplex,fc)
  1521. *
  1522. * Set Ethernet duplex and flow control options for this MAC
  1523. * Warning: must be called when MAC is off!
  1524. *
  1525. * Input parameters:
  1526. * s - sbmac structure
  1527. * duplex - duplex setting (see enum sbmac_duplex)
  1528. * fc - flow control setting (see enum sbmac_fc)
  1529. *
  1530. * Return value:
  1531. * 1 if ok
  1532. * 0 if an invalid parameter combination was specified
  1533. ********************************************************************* */
  1534. static int sbmac_set_duplex(struct sbmac_softc *s, enum sbmac_duplex duplex,
  1535. enum sbmac_fc fc)
  1536. {
  1537. uint64_t cfg;
  1538. /*
  1539. * Save new current values
  1540. */
  1541. s->sbm_duplex = duplex;
  1542. s->sbm_fc = fc;
  1543. if (s->sbm_state == sbmac_state_on)
  1544. return 0; /* save for next restart */
  1545. /*
  1546. * Read current register values
  1547. */
  1548. cfg = __raw_readq(s->sbm_maccfg);
  1549. /*
  1550. * Mask off the stuff we're about to change
  1551. */
  1552. cfg &= ~(M_MAC_FC_SEL | M_MAC_FC_CMD | M_MAC_HDX_EN);
  1553. switch (duplex) {
  1554. case sbmac_duplex_half:
  1555. switch (fc) {
  1556. case sbmac_fc_disabled:
  1557. cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_DISABLED;
  1558. break;
  1559. case sbmac_fc_collision:
  1560. cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENABLED;
  1561. break;
  1562. case sbmac_fc_carrier:
  1563. cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENAB_FALSECARR;
  1564. break;
  1565. case sbmac_fc_frame: /* not valid in half duplex */
  1566. default: /* invalid selection */
  1567. return 0;
  1568. }
  1569. break;
  1570. case sbmac_duplex_full:
  1571. switch (fc) {
  1572. case sbmac_fc_disabled:
  1573. cfg |= V_MAC_FC_CMD_DISABLED;
  1574. break;
  1575. case sbmac_fc_frame:
  1576. cfg |= V_MAC_FC_CMD_ENABLED;
  1577. break;
  1578. case sbmac_fc_collision: /* not valid in full duplex */
  1579. case sbmac_fc_carrier: /* not valid in full duplex */
  1580. default:
  1581. return 0;
  1582. }
  1583. break;
  1584. default:
  1585. return 0;
  1586. }
  1587. /*
  1588. * Send the bits back to the hardware
  1589. */
  1590. __raw_writeq(cfg, s->sbm_maccfg);
  1591. return 1;
  1592. }
  1593. /**********************************************************************
  1594. * SBMAC_INTR()
  1595. *
  1596. * Interrupt handler for MAC interrupts
  1597. *
  1598. * Input parameters:
  1599. * MAC structure
  1600. *
  1601. * Return value:
  1602. * nothing
  1603. ********************************************************************* */
  1604. static irqreturn_t sbmac_intr(int irq,void *dev_instance)
  1605. {
  1606. struct net_device *dev = (struct net_device *) dev_instance;
  1607. struct sbmac_softc *sc = netdev_priv(dev);
  1608. uint64_t isr;
  1609. int handled = 0;
  1610. /*
  1611. * Read the ISR (this clears the bits in the real
  1612. * register, except for counter addr)
  1613. */
  1614. isr = __raw_readq(sc->sbm_isr) & ~M_MAC_COUNTER_ADDR;
  1615. if (isr == 0)
  1616. return IRQ_RETVAL(0);
  1617. handled = 1;
  1618. /*
  1619. * Transmits on channel 0
  1620. */
  1621. if (isr & (M_MAC_INT_CHANNEL << S_MAC_TX_CH0))
  1622. sbdma_tx_process(sc,&(sc->sbm_txdma), 0);
  1623. if (isr & (M_MAC_INT_CHANNEL << S_MAC_RX_CH0)) {
  1624. if (napi_schedule_prep(&sc->napi)) {
  1625. __raw_writeq(0, sc->sbm_imr);
  1626. __napi_schedule(&sc->napi);
  1627. /* Depend on the exit from poll to reenable intr */
  1628. }
  1629. else {
  1630. /* may leave some packets behind */
  1631. sbdma_rx_process(sc,&(sc->sbm_rxdma),
  1632. SBMAC_MAX_RXDESCR * 2, 0);
  1633. }
  1634. }
  1635. return IRQ_RETVAL(handled);
  1636. }
  1637. /**********************************************************************
  1638. * SBMAC_START_TX(skb,dev)
  1639. *
  1640. * Start output on the specified interface. Basically, we
  1641. * queue as many buffers as we can until the ring fills up, or
  1642. * we run off the end of the queue, whichever comes first.
  1643. *
  1644. * Input parameters:
  1645. *
  1646. *
  1647. * Return value:
  1648. * nothing
  1649. ********************************************************************* */
  1650. static int sbmac_start_tx(struct sk_buff *skb, struct net_device *dev)
  1651. {
  1652. struct sbmac_softc *sc = netdev_priv(dev);
  1653. unsigned long flags;
  1654. /* lock eth irq */
  1655. spin_lock_irqsave(&sc->sbm_lock, flags);
  1656. /*
  1657. * Put the buffer on the transmit ring. If we
  1658. * don't have room, stop the queue.
  1659. */
  1660. if (sbdma_add_txbuffer(&(sc->sbm_txdma),skb)) {
  1661. /* XXX save skb that we could not send */
  1662. netif_stop_queue(dev);
  1663. spin_unlock_irqrestore(&sc->sbm_lock, flags);
  1664. return NETDEV_TX_BUSY;
  1665. }
  1666. spin_unlock_irqrestore(&sc->sbm_lock, flags);
  1667. return NETDEV_TX_OK;
  1668. }
  1669. /**********************************************************************
  1670. * SBMAC_SETMULTI(sc)
  1671. *
  1672. * Reprogram the multicast table into the hardware, given
  1673. * the list of multicasts associated with the interface
  1674. * structure.
  1675. *
  1676. * Input parameters:
  1677. * sc - softc
  1678. *
  1679. * Return value:
  1680. * nothing
  1681. ********************************************************************* */
  1682. static void sbmac_setmulti(struct sbmac_softc *sc)
  1683. {
  1684. uint64_t reg;
  1685. void __iomem *port;
  1686. int idx;
  1687. struct netdev_hw_addr *ha;
  1688. struct net_device *dev = sc->sbm_dev;
  1689. /*
  1690. * Clear out entire multicast table. We do this by nuking
  1691. * the entire hash table and all the direct matches except
  1692. * the first one, which is used for our station address
  1693. */
  1694. for (idx = 1; idx < MAC_ADDR_COUNT; idx++) {
  1695. port = sc->sbm_base + R_MAC_ADDR_BASE+(idx*sizeof(uint64_t));
  1696. __raw_writeq(0, port);
  1697. }
  1698. for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
  1699. port = sc->sbm_base + R_MAC_HASH_BASE+(idx*sizeof(uint64_t));
  1700. __raw_writeq(0, port);
  1701. }
  1702. /*
  1703. * Clear the filter to say we don't want any multicasts.
  1704. */
  1705. reg = __raw_readq(sc->sbm_rxfilter);
  1706. reg &= ~(M_MAC_MCAST_INV | M_MAC_MCAST_EN);
  1707. __raw_writeq(reg, sc->sbm_rxfilter);
  1708. if (dev->flags & IFF_ALLMULTI) {
  1709. /*
  1710. * Enable ALL multicasts. Do this by inverting the
  1711. * multicast enable bit.
  1712. */
  1713. reg = __raw_readq(sc->sbm_rxfilter);
  1714. reg |= (M_MAC_MCAST_INV | M_MAC_MCAST_EN);
  1715. __raw_writeq(reg, sc->sbm_rxfilter);
  1716. return;
  1717. }
  1718. /*
  1719. * Progam new multicast entries. For now, only use the
  1720. * perfect filter. In the future we'll need to use the
  1721. * hash filter if the perfect filter overflows
  1722. */
  1723. /* XXX only using perfect filter for now, need to use hash
  1724. * XXX if the table overflows */
  1725. idx = 1; /* skip station address */
  1726. netdev_for_each_mc_addr(ha, dev) {
  1727. if (idx == MAC_ADDR_COUNT)
  1728. break;
  1729. reg = sbmac_addr2reg(ha->addr);
  1730. port = sc->sbm_base + R_MAC_ADDR_BASE+(idx * sizeof(uint64_t));
  1731. __raw_writeq(reg, port);
  1732. idx++;
  1733. }
  1734. /*
  1735. * Enable the "accept multicast bits" if we programmed at least one
  1736. * multicast.
  1737. */
  1738. if (idx > 1) {
  1739. reg = __raw_readq(sc->sbm_rxfilter);
  1740. reg |= M_MAC_MCAST_EN;
  1741. __raw_writeq(reg, sc->sbm_rxfilter);
  1742. }
  1743. }
  1744. static int sb1250_change_mtu(struct net_device *_dev, int new_mtu)
  1745. {
  1746. if (new_mtu > ENET_PACKET_SIZE)
  1747. return -EINVAL;
  1748. _dev->mtu = new_mtu;
  1749. pr_info("changing the mtu to %d\n", new_mtu);
  1750. return 0;
  1751. }
  1752. static const struct net_device_ops sbmac_netdev_ops = {
  1753. .ndo_open = sbmac_open,
  1754. .ndo_stop = sbmac_close,
  1755. .ndo_start_xmit = sbmac_start_tx,
  1756. .ndo_set_rx_mode = sbmac_set_rx_mode,
  1757. .ndo_tx_timeout = sbmac_tx_timeout,
  1758. .ndo_do_ioctl = sbmac_mii_ioctl,
  1759. .ndo_change_mtu = sb1250_change_mtu,
  1760. .ndo_validate_addr = eth_validate_addr,
  1761. .ndo_set_mac_address = eth_mac_addr,
  1762. #ifdef CONFIG_NET_POLL_CONTROLLER
  1763. .ndo_poll_controller = sbmac_netpoll,
  1764. #endif
  1765. };
  1766. /**********************************************************************
  1767. * SBMAC_INIT(dev)
  1768. *
  1769. * Attach routine - init hardware and hook ourselves into linux
  1770. *
  1771. * Input parameters:
  1772. * dev - net_device structure
  1773. *
  1774. * Return value:
  1775. * status
  1776. ********************************************************************* */
  1777. static int sbmac_init(struct platform_device *pldev, long long base)
  1778. {
  1779. struct net_device *dev = platform_get_drvdata(pldev);
  1780. int idx = pldev->id;
  1781. struct sbmac_softc *sc = netdev_priv(dev);
  1782. unsigned char *eaddr;
  1783. uint64_t ea_reg;
  1784. int i;
  1785. int err;
  1786. sc->sbm_dev = dev;
  1787. sc->sbe_idx = idx;
  1788. eaddr = sc->sbm_hwaddr;
  1789. /*
  1790. * Read the ethernet address. The firmware left this programmed
  1791. * for us in the ethernet address register for each mac.
  1792. */
  1793. ea_reg = __raw_readq(sc->sbm_base + R_MAC_ETHERNET_ADDR);
  1794. __raw_writeq(0, sc->sbm_base + R_MAC_ETHERNET_ADDR);
  1795. for (i = 0; i < 6; i++) {
  1796. eaddr[i] = (uint8_t) (ea_reg & 0xFF);
  1797. ea_reg >>= 8;
  1798. }
  1799. for (i = 0; i < 6; i++) {
  1800. dev->dev_addr[i] = eaddr[i];
  1801. }
  1802. /*
  1803. * Initialize context (get pointers to registers and stuff), then
  1804. * allocate the memory for the descriptor tables.
  1805. */
  1806. sbmac_initctx(sc);
  1807. /*
  1808. * Set up Linux device callins
  1809. */
  1810. spin_lock_init(&(sc->sbm_lock));
  1811. dev->netdev_ops = &sbmac_netdev_ops;
  1812. dev->watchdog_timeo = TX_TIMEOUT;
  1813. netif_napi_add(dev, &sc->napi, sbmac_poll, 16);
  1814. dev->irq = UNIT_INT(idx);
  1815. /* This is needed for PASS2 for Rx H/W checksum feature */
  1816. sbmac_set_iphdr_offset(sc);
  1817. sc->mii_bus = mdiobus_alloc();
  1818. if (sc->mii_bus == NULL) {
  1819. err = -ENOMEM;
  1820. goto uninit_ctx;
  1821. }
  1822. sc->mii_bus->name = sbmac_mdio_string;
  1823. snprintf(sc->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  1824. pldev->name, idx);
  1825. sc->mii_bus->priv = sc;
  1826. sc->mii_bus->read = sbmac_mii_read;
  1827. sc->mii_bus->write = sbmac_mii_write;
  1828. sc->mii_bus->parent = &pldev->dev;
  1829. /*
  1830. * Probe PHY address
  1831. */
  1832. err = mdiobus_register(sc->mii_bus);
  1833. if (err) {
  1834. printk(KERN_ERR "%s: unable to register MDIO bus\n",
  1835. dev->name);
  1836. goto free_mdio;
  1837. }
  1838. platform_set_drvdata(pldev, sc->mii_bus);
  1839. err = register_netdev(dev);
  1840. if (err) {
  1841. printk(KERN_ERR "%s.%d: unable to register netdev\n",
  1842. sbmac_string, idx);
  1843. goto unreg_mdio;
  1844. }
  1845. pr_info("%s.%d: registered as %s\n", sbmac_string, idx, dev->name);
  1846. if (sc->rx_hw_checksum == ENABLE)
  1847. pr_info("%s: enabling TCP rcv checksum\n", dev->name);
  1848. /*
  1849. * Display Ethernet address (this is called during the config
  1850. * process so we need to finish off the config message that
  1851. * was being displayed)
  1852. */
  1853. pr_info("%s: SiByte Ethernet at 0x%08Lx, address: %pM\n",
  1854. dev->name, base, eaddr);
  1855. return 0;
  1856. unreg_mdio:
  1857. mdiobus_unregister(sc->mii_bus);
  1858. free_mdio:
  1859. mdiobus_free(sc->mii_bus);
  1860. uninit_ctx:
  1861. sbmac_uninitctx(sc);
  1862. return err;
  1863. }
  1864. static int sbmac_open(struct net_device *dev)
  1865. {
  1866. struct sbmac_softc *sc = netdev_priv(dev);
  1867. int err;
  1868. if (debug > 1)
  1869. pr_debug("%s: sbmac_open() irq %d.\n", dev->name, dev->irq);
  1870. /*
  1871. * map/route interrupt (clear status first, in case something
  1872. * weird is pending; we haven't initialized the mac registers
  1873. * yet)
  1874. */
  1875. __raw_readq(sc->sbm_isr);
  1876. err = request_irq(dev->irq, sbmac_intr, IRQF_SHARED, dev->name, dev);
  1877. if (err) {
  1878. printk(KERN_ERR "%s: unable to get IRQ %d\n", dev->name,
  1879. dev->irq);
  1880. goto out_err;
  1881. }
  1882. sc->sbm_speed = sbmac_speed_none;
  1883. sc->sbm_duplex = sbmac_duplex_none;
  1884. sc->sbm_fc = sbmac_fc_none;
  1885. sc->sbm_pause = -1;
  1886. sc->sbm_link = 0;
  1887. /*
  1888. * Attach to the PHY
  1889. */
  1890. err = sbmac_mii_probe(dev);
  1891. if (err)
  1892. goto out_unregister;
  1893. /*
  1894. * Turn on the channel
  1895. */
  1896. sbmac_set_channel_state(sc,sbmac_state_on);
  1897. netif_start_queue(dev);
  1898. sbmac_set_rx_mode(dev);
  1899. phy_start(sc->phy_dev);
  1900. napi_enable(&sc->napi);
  1901. return 0;
  1902. out_unregister:
  1903. free_irq(dev->irq, dev);
  1904. out_err:
  1905. return err;
  1906. }
  1907. static int sbmac_mii_probe(struct net_device *dev)
  1908. {
  1909. struct sbmac_softc *sc = netdev_priv(dev);
  1910. struct phy_device *phy_dev;
  1911. phy_dev = phy_find_first(sc->mii_bus);
  1912. if (!phy_dev) {
  1913. printk(KERN_ERR "%s: no PHY found\n", dev->name);
  1914. return -ENXIO;
  1915. }
  1916. phy_dev = phy_connect(dev, dev_name(&phy_dev->mdio.dev),
  1917. &sbmac_mii_poll, PHY_INTERFACE_MODE_GMII);
  1918. if (IS_ERR(phy_dev)) {
  1919. printk(KERN_ERR "%s: could not attach to PHY\n", dev->name);
  1920. return PTR_ERR(phy_dev);
  1921. }
  1922. /* Remove any features not supported by the controller */
  1923. phy_dev->supported &= SUPPORTED_10baseT_Half |
  1924. SUPPORTED_10baseT_Full |
  1925. SUPPORTED_100baseT_Half |
  1926. SUPPORTED_100baseT_Full |
  1927. SUPPORTED_1000baseT_Half |
  1928. SUPPORTED_1000baseT_Full |
  1929. SUPPORTED_Autoneg |
  1930. SUPPORTED_MII |
  1931. SUPPORTED_Pause |
  1932. SUPPORTED_Asym_Pause;
  1933. phy_attached_info(phy_dev);
  1934. phy_dev->advertising = phy_dev->supported;
  1935. sc->phy_dev = phy_dev;
  1936. return 0;
  1937. }
  1938. static void sbmac_mii_poll(struct net_device *dev)
  1939. {
  1940. struct sbmac_softc *sc = netdev_priv(dev);
  1941. struct phy_device *phy_dev = sc->phy_dev;
  1942. unsigned long flags;
  1943. enum sbmac_fc fc;
  1944. int link_chg, speed_chg, duplex_chg, pause_chg, fc_chg;
  1945. link_chg = (sc->sbm_link != phy_dev->link);
  1946. speed_chg = (sc->sbm_speed != phy_dev->speed);
  1947. duplex_chg = (sc->sbm_duplex != phy_dev->duplex);
  1948. pause_chg = (sc->sbm_pause != phy_dev->pause);
  1949. if (!link_chg && !speed_chg && !duplex_chg && !pause_chg)
  1950. return; /* Hmmm... */
  1951. if (!phy_dev->link) {
  1952. if (link_chg) {
  1953. sc->sbm_link = phy_dev->link;
  1954. sc->sbm_speed = sbmac_speed_none;
  1955. sc->sbm_duplex = sbmac_duplex_none;
  1956. sc->sbm_fc = sbmac_fc_disabled;
  1957. sc->sbm_pause = -1;
  1958. pr_info("%s: link unavailable\n", dev->name);
  1959. }
  1960. return;
  1961. }
  1962. if (phy_dev->duplex == DUPLEX_FULL) {
  1963. if (phy_dev->pause)
  1964. fc = sbmac_fc_frame;
  1965. else
  1966. fc = sbmac_fc_disabled;
  1967. } else
  1968. fc = sbmac_fc_collision;
  1969. fc_chg = (sc->sbm_fc != fc);
  1970. pr_info("%s: link available: %dbase-%cD\n", dev->name, phy_dev->speed,
  1971. phy_dev->duplex == DUPLEX_FULL ? 'F' : 'H');
  1972. spin_lock_irqsave(&sc->sbm_lock, flags);
  1973. sc->sbm_speed = phy_dev->speed;
  1974. sc->sbm_duplex = phy_dev->duplex;
  1975. sc->sbm_fc = fc;
  1976. sc->sbm_pause = phy_dev->pause;
  1977. sc->sbm_link = phy_dev->link;
  1978. if ((speed_chg || duplex_chg || fc_chg) &&
  1979. sc->sbm_state != sbmac_state_off) {
  1980. /*
  1981. * something changed, restart the channel
  1982. */
  1983. if (debug > 1)
  1984. pr_debug("%s: restarting channel "
  1985. "because PHY state changed\n", dev->name);
  1986. sbmac_channel_stop(sc);
  1987. sbmac_channel_start(sc);
  1988. }
  1989. spin_unlock_irqrestore(&sc->sbm_lock, flags);
  1990. }
  1991. static void sbmac_tx_timeout (struct net_device *dev)
  1992. {
  1993. struct sbmac_softc *sc = netdev_priv(dev);
  1994. unsigned long flags;
  1995. spin_lock_irqsave(&sc->sbm_lock, flags);
  1996. netif_trans_update(dev); /* prevent tx timeout */
  1997. dev->stats.tx_errors++;
  1998. spin_unlock_irqrestore(&sc->sbm_lock, flags);
  1999. printk (KERN_WARNING "%s: Transmit timed out\n",dev->name);
  2000. }
  2001. static void sbmac_set_rx_mode(struct net_device *dev)
  2002. {
  2003. unsigned long flags;
  2004. struct sbmac_softc *sc = netdev_priv(dev);
  2005. spin_lock_irqsave(&sc->sbm_lock, flags);
  2006. if ((dev->flags ^ sc->sbm_devflags) & IFF_PROMISC) {
  2007. /*
  2008. * Promiscuous changed.
  2009. */
  2010. if (dev->flags & IFF_PROMISC) {
  2011. sbmac_promiscuous_mode(sc,1);
  2012. }
  2013. else {
  2014. sbmac_promiscuous_mode(sc,0);
  2015. }
  2016. }
  2017. spin_unlock_irqrestore(&sc->sbm_lock, flags);
  2018. /*
  2019. * Program the multicasts. Do this every time.
  2020. */
  2021. sbmac_setmulti(sc);
  2022. }
  2023. static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  2024. {
  2025. struct sbmac_softc *sc = netdev_priv(dev);
  2026. if (!netif_running(dev) || !sc->phy_dev)
  2027. return -EINVAL;
  2028. return phy_mii_ioctl(sc->phy_dev, rq, cmd);
  2029. }
  2030. static int sbmac_close(struct net_device *dev)
  2031. {
  2032. struct sbmac_softc *sc = netdev_priv(dev);
  2033. napi_disable(&sc->napi);
  2034. phy_stop(sc->phy_dev);
  2035. sbmac_set_channel_state(sc, sbmac_state_off);
  2036. netif_stop_queue(dev);
  2037. if (debug > 1)
  2038. pr_debug("%s: Shutting down ethercard\n", dev->name);
  2039. phy_disconnect(sc->phy_dev);
  2040. sc->phy_dev = NULL;
  2041. free_irq(dev->irq, dev);
  2042. sbdma_emptyring(&(sc->sbm_txdma));
  2043. sbdma_emptyring(&(sc->sbm_rxdma));
  2044. return 0;
  2045. }
  2046. static int sbmac_poll(struct napi_struct *napi, int budget)
  2047. {
  2048. struct sbmac_softc *sc = container_of(napi, struct sbmac_softc, napi);
  2049. int work_done;
  2050. work_done = sbdma_rx_process(sc, &(sc->sbm_rxdma), budget, 1);
  2051. sbdma_tx_process(sc, &(sc->sbm_txdma), 1);
  2052. if (work_done < budget) {
  2053. napi_complete(napi);
  2054. #ifdef CONFIG_SBMAC_COALESCE
  2055. __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
  2056. ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0),
  2057. sc->sbm_imr);
  2058. #else
  2059. __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
  2060. (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), sc->sbm_imr);
  2061. #endif
  2062. }
  2063. return work_done;
  2064. }
  2065. static int sbmac_probe(struct platform_device *pldev)
  2066. {
  2067. struct net_device *dev;
  2068. struct sbmac_softc *sc;
  2069. void __iomem *sbm_base;
  2070. struct resource *res;
  2071. u64 sbmac_orig_hwaddr;
  2072. int err;
  2073. res = platform_get_resource(pldev, IORESOURCE_MEM, 0);
  2074. BUG_ON(!res);
  2075. sbm_base = ioremap_nocache(res->start, resource_size(res));
  2076. if (!sbm_base) {
  2077. printk(KERN_ERR "%s: unable to map device registers\n",
  2078. dev_name(&pldev->dev));
  2079. err = -ENOMEM;
  2080. goto out_out;
  2081. }
  2082. /*
  2083. * The R_MAC_ETHERNET_ADDR register will be set to some nonzero
  2084. * value for us by the firmware if we're going to use this MAC.
  2085. * If we find a zero, skip this MAC.
  2086. */
  2087. sbmac_orig_hwaddr = __raw_readq(sbm_base + R_MAC_ETHERNET_ADDR);
  2088. pr_debug("%s: %sconfiguring MAC at 0x%08Lx\n", dev_name(&pldev->dev),
  2089. sbmac_orig_hwaddr ? "" : "not ", (long long)res->start);
  2090. if (sbmac_orig_hwaddr == 0) {
  2091. err = 0;
  2092. goto out_unmap;
  2093. }
  2094. /*
  2095. * Okay, cool. Initialize this MAC.
  2096. */
  2097. dev = alloc_etherdev(sizeof(struct sbmac_softc));
  2098. if (!dev) {
  2099. err = -ENOMEM;
  2100. goto out_unmap;
  2101. }
  2102. platform_set_drvdata(pldev, dev);
  2103. SET_NETDEV_DEV(dev, &pldev->dev);
  2104. sc = netdev_priv(dev);
  2105. sc->sbm_base = sbm_base;
  2106. err = sbmac_init(pldev, res->start);
  2107. if (err)
  2108. goto out_kfree;
  2109. return 0;
  2110. out_kfree:
  2111. free_netdev(dev);
  2112. __raw_writeq(sbmac_orig_hwaddr, sbm_base + R_MAC_ETHERNET_ADDR);
  2113. out_unmap:
  2114. iounmap(sbm_base);
  2115. out_out:
  2116. return err;
  2117. }
  2118. static int __exit sbmac_remove(struct platform_device *pldev)
  2119. {
  2120. struct net_device *dev = platform_get_drvdata(pldev);
  2121. struct sbmac_softc *sc = netdev_priv(dev);
  2122. unregister_netdev(dev);
  2123. sbmac_uninitctx(sc);
  2124. mdiobus_unregister(sc->mii_bus);
  2125. mdiobus_free(sc->mii_bus);
  2126. iounmap(sc->sbm_base);
  2127. free_netdev(dev);
  2128. return 0;
  2129. }
  2130. static struct platform_driver sbmac_driver = {
  2131. .probe = sbmac_probe,
  2132. .remove = __exit_p(sbmac_remove),
  2133. .driver = {
  2134. .name = sbmac_string,
  2135. },
  2136. };
  2137. module_platform_driver(sbmac_driver);