sb1250-mac.c 65 KB

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