sb1250-mac.c 64 KB

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