sb1250-mac.c 65 KB

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