sb1250-mac.c 65 KB

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