sb1250-mac.c 65 KB

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