sb1250-mac.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:72k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * Copyright (C) 2001 Broadcom Corporation
  3.  *
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either version 2
  7.  * of the License, or (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  * 
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17.  */
  18. /*
  19.   This driver is designed for the Broadcom BCM12500 SOC chip's built-in
  20.   Ethernet controllers.
  21.   
  22.   The author may be reached as mpl@broadcom.com
  23. */
  24. #define CONFIG_SBMAC_COALESCE
  25. /* A few user-configurable values.
  26.    These may be modified when a driver module is loaded. */
  27. static int debug = 1; /* 1 normal messages, 0 quiet .. 7 verbose. */
  28. /* Used to pass the media type, etc.
  29.    Both 'options[]' and 'full_duplex[]' should exist for driver
  30.    interoperability.
  31.    The media type is usually passed in 'options[]'.
  32. */
  33. #define MAX_UNITS 3 /* More are supported, limit only on options */
  34. #ifdef MODULE
  35. static int options[MAX_UNITS] = {-1, -1, -1};
  36. static int full_duplex[MAX_UNITS] = {-1, -1, -1};
  37. #endif
  38. static int int_pktcnt = 0;
  39. static int int_timeout = 0;
  40. /* Operational parameters that usually are not changed. */
  41. /* Time in jiffies before concluding the transmitter is hung. */
  42. #define TX_TIMEOUT  (2*HZ)
  43. #if !defined(__OPTIMIZE__)  ||  !defined(__KERNEL__)
  44. #warning  You must compile this file with the correct options!
  45. #warning  See the last lines of the source file.
  46. #error  You must compile this driver with "-O".
  47. #endif
  48. #include <linux/module.h>
  49. #include <linux/kernel.h>
  50. #include <linux/string.h>
  51. #include <linux/timer.h>
  52. #include <linux/errno.h>
  53. #include <linux/ioport.h>
  54. #include <linux/slab.h>
  55. #include <linux/interrupt.h>
  56. #include <linux/netdevice.h>
  57. #include <linux/etherdevice.h>
  58. #include <linux/skbuff.h>
  59. #include <linux/init.h>
  60. #include <linux/config.h>
  61. #include <asm/processor.h> /* Processor type for cache alignment. */
  62. #include <asm/bitops.h>
  63. #include <asm/io.h>
  64. #include <asm/sibyte/sb1250.h>
  65. #include <asm/sibyte/64bit.h>
  66. /* This is only here until the firmware is ready.  In that case,
  67.    the firmware leaves the ethernet address in the register for us. */
  68. #ifdef CONFIG_SIBYTE_STANDALONE
  69. #define SBMAC_ETH0_HWADDR "40:00:00:00:01:00"
  70. #define SBMAC_ETH1_HWADDR "40:00:00:00:01:01"
  71. #define SBMAC_ETH2_HWADDR "40:00:00:00:01:02"
  72. #endif
  73. /* These identify the driver base version and may not be removed. */
  74. #if 0
  75. static char version1[] __devinitdata =
  76. "sb1250-mac.c:1.00 1/11/2001 Written by Mitch Lichtenberg (mpl@broadcom.com)n";
  77. #endif
  78. MODULE_AUTHOR("Mitch Lichtenberg (mpl@broadcom.com)");
  79. MODULE_DESCRIPTION("Broadcom BCM12500 SOC GB Ethernet driver");
  80. MODULE_PARM(debug, "i");
  81. MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i");
  82. MODULE_PARM(full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i");
  83. MODULE_PARM(int_pktcnt, "i");
  84. MODULE_PARM(int_timeout, "i");
  85. #include <asm/sibyte/sb1250_defs.h>
  86. #include <asm/sibyte/sb1250_regs.h>
  87. #include <asm/sibyte/sb1250_mac.h>
  88. #include <asm/sibyte/sb1250_dma.h>
  89. #include <asm/sibyte/sb1250_int.h>
  90. #include <asm/sibyte/sb1250_scd.h> /* Only to check SOC part number. */
  91. /**********************************************************************
  92.  *  Simple types
  93.  ********************************************************************* */
  94. typedef unsigned long sbmac_port_t;
  95. typedef uint64_t sbmac_physaddr_t;
  96. typedef uint64_t sbmac_enetaddr_t;
  97. typedef enum { sbmac_speed_auto, sbmac_speed_10,
  98.        sbmac_speed_100, sbmac_speed_1000 } sbmac_speed_t;
  99. typedef enum { sbmac_duplex_auto, sbmac_duplex_half,
  100.        sbmac_duplex_full } sbmac_duplex_t;
  101. typedef enum { sbmac_fc_auto, sbmac_fc_disabled, sbmac_fc_frame,
  102.        sbmac_fc_collision, sbmac_fc_carrier } sbmac_fc_t;
  103. typedef enum { sbmac_state_uninit, sbmac_state_off, sbmac_state_on, 
  104.        sbmac_state_broken } sbmac_state_t;
  105. /**********************************************************************
  106.  *  Macros
  107.  ********************************************************************* */
  108. #define SBDMA_NEXTBUF(d,f) ((((d)->f+1) == (d)->sbdma_dscrtable_end) ? 
  109.   (d)->sbdma_dscrtable : (d)->f+1)
  110. #define CACHELINESIZE 32
  111. #define NUMCACHEBLKS(x) (((x)+CACHELINESIZE-1)/CACHELINESIZE)
  112. #define KMALLOC(x) kmalloc((x),GFP_KERNEL)
  113. #define KFREE(x) kfree(x)
  114. #define KVTOPHYS(x) virt_to_bus((void *)(x))
  115.  
  116. #define SBMAC_READCSR(t)    (in64((unsigned long)(t)))
  117. #define SBMAC_WRITECSR(t,v) (out64(v, (unsigned long)(t)))
  118. #define PKSEG1(x) ((sbmac_port_t) KSEG1ADDR(x))
  119. #define SBMAC_MAX_TXDESCR 32
  120. #define SBMAC_MAX_RXDESCR 32
  121. #define ETHER_ALIGN 2
  122. #define ETHER_ADDR_LEN 6
  123. #define ENET_PACKET_SIZE 1518 
  124. /*#define ENET_PACKET_SIZE 9216 */ 
  125. /**********************************************************************
  126.  *  DMA Descriptor structure
  127.  ********************************************************************* */
  128. typedef struct sbdmadscr_s {
  129. uint64_t  dscr_a;
  130. uint64_t  dscr_b;
  131. } sbdmadscr_t;
  132. typedef unsigned long paddr_t;
  133. typedef unsigned long vaddr_t;
  134. /**********************************************************************
  135.  *  DMA Controller structure
  136.  ********************************************************************* */
  137. typedef struct sbmacdma_s {
  138. /* 
  139.  * This stuff is used to identify the channel and the registers
  140.  * associated with it.
  141.  */
  142. struct sbmac_softc *sbdma_eth;         /* back pointer to associated MAC */
  143. int              sbdma_channel; /* channel number */
  144. int  sbdma_txdir;       /* direction (1=transmit) */
  145. int  sbdma_maxdescr; /* total # of descriptors in ring */
  146. #ifdef CONFIG_SBMAC_COALESCE
  147.         int              sbdma_int_pktcnt;  /* # descriptors rx before interrupt*/
  148.         int              sbdma_int_timeout; /* # usec rx interrupt */
  149. #endif
  150. sbmac_port_t     sbdma_config0; /* DMA config register 0 */
  151. sbmac_port_t     sbdma_config1; /* DMA config register 1 */
  152. sbmac_port_t     sbdma_dscrbase; /* Descriptor base address */
  153. sbmac_port_t     sbdma_dscrcnt;     /* Descriptor count register */
  154. sbmac_port_t     sbdma_curdscr; /* current descriptor address */
  155. /*
  156.  * This stuff is for maintenance of the ring
  157.  */
  158. sbdmadscr_t     *sbdma_dscrtable; /* base of descriptor table */
  159. sbdmadscr_t     *sbdma_dscrtable_end; /* end of descriptor table */
  160. struct sk_buff **sbdma_ctxtable;    /* context table, one per descr */
  161. paddr_t          sbdma_dscrtable_phys; /* and also the phys addr */
  162. sbdmadscr_t     *sbdma_addptr; /* next dscr for sw to add */
  163. sbdmadscr_t     *sbdma_remptr; /* next dscr for sw to remove */
  164. } sbmacdma_t;
  165. /**********************************************************************
  166.  *  Ethernet softc structure
  167.  ********************************************************************* */
  168. struct sbmac_softc {
  169. /*
  170.  * Linux-specific things
  171.  */
  172. struct net_device *sbm_dev; /* pointer to linux device */
  173. spinlock_t sbm_lock; /* spin lock */
  174. struct timer_list sbm_timer;      /* for monitoring MII */
  175. struct net_device_stats sbm_stats; 
  176. int sbm_devflags; /* current device flags */
  177.         int      sbm_phy_oldbmsr;
  178.         int      sbm_phy_oldanlpar;
  179.         int      sbm_phy_oldk1stsr;
  180.         int          sbm_phy_oldlinkstat;
  181. int sbm_buffersize;
  182. unsigned char sbm_phys[2];
  183. /*
  184.  * Controller-specific things
  185.  */
  186. sbmac_port_t     sbm_base;          /* MAC's base address */
  187. sbmac_state_t    sbm_state;         /* current state */
  188. sbmac_port_t     sbm_macenable; /* MAC Enable Register */
  189. sbmac_port_t     sbm_maccfg; /* MAC Configuration Register */
  190. sbmac_port_t     sbm_fifocfg; /* FIFO configuration register */
  191. sbmac_port_t     sbm_framecfg; /* Frame configuration register */
  192. sbmac_port_t     sbm_rxfilter; /* receive filter register */
  193. sbmac_port_t     sbm_isr; /* Interrupt status register */
  194. sbmac_port_t     sbm_imr; /* Interrupt mask register */
  195. sbmac_port_t     sbm_mdio; /* MDIO register */
  196. sbmac_speed_t    sbm_speed; /* current speed */
  197. sbmac_duplex_t   sbm_duplex; /* current duplex */
  198. sbmac_fc_t       sbm_fc; /* current flow control setting */
  199. u_char           sbm_hwaddr[ETHER_ADDR_LEN];
  200. sbmacdma_t       sbm_txdma; /* for now, only use channel 0 */
  201. sbmacdma_t       sbm_rxdma;
  202. int              rx_hw_checksum;
  203. int   sbe_idx;
  204. };
  205. /**********************************************************************
  206.  *  Externs
  207.  ********************************************************************* */
  208. /**********************************************************************
  209.  *  Prototypes
  210.  ********************************************************************* */
  211. static void sbdma_initctx(sbmacdma_t *d,
  212.   struct sbmac_softc *s,
  213.   int chan,
  214.   int txrx,
  215.   int maxdescr);
  216. static void sbdma_channel_start(sbmacdma_t *d, int rxtx);
  217. static int sbdma_add_rcvbuffer(sbmacdma_t *d,struct sk_buff *m);
  218. static int sbdma_add_txbuffer(sbmacdma_t *d,struct sk_buff *m);
  219. static void sbdma_emptyring(sbmacdma_t *d);
  220. static void sbdma_fillring(sbmacdma_t *d);
  221. static void sbdma_rx_process(struct sbmac_softc *sc,sbmacdma_t *d);
  222. static void sbdma_tx_process(struct sbmac_softc *sc,sbmacdma_t *d);
  223. static int sbmac_initctx(struct sbmac_softc *s);
  224. static void sbmac_channel_start(struct sbmac_softc *s);
  225. static void sbmac_channel_stop(struct sbmac_softc *s);
  226. static sbmac_state_t sbmac_set_channel_state(struct sbmac_softc *,sbmac_state_t);
  227. static void sbmac_promiscuous_mode(struct sbmac_softc *sc,int onoff);
  228. /*static void sbmac_init_and_start(struct sbmac_softc *sc);*/
  229. static uint64_t sbmac_addr2reg(unsigned char *ptr);
  230. static void sbmac_intr(int irq,void *dev_instance,struct pt_regs *rgs);
  231. static int sbmac_start_tx(struct sk_buff *skb, struct net_device *dev);
  232. static void sbmac_setmulti(struct sbmac_softc *sc);
  233. static int sbmac_init(struct net_device *dev);
  234. static int sbmac_set_speed(struct sbmac_softc *s,sbmac_speed_t speed);
  235. static int sbmac_set_duplex(struct sbmac_softc *s,sbmac_duplex_t duplex,sbmac_fc_t fc);
  236. static int sbmac_open(struct net_device *dev);
  237. static void sbmac_timer(unsigned long data);
  238. static void sbmac_tx_timeout (struct net_device *dev);
  239. static struct net_device_stats *sbmac_get_stats(struct net_device *dev);
  240. static void sbmac_set_rx_mode(struct net_device *dev);
  241. static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  242. static int sbmac_close(struct net_device *dev);
  243. static int sbmac_mii_poll(struct sbmac_softc *s,int noisy);
  244. static void sbmac_mii_sync(struct sbmac_softc *s);
  245. static void sbmac_mii_senddata(struct sbmac_softc *s,unsigned int data, int bitcnt);
  246. static unsigned int sbmac_mii_read(struct sbmac_softc *s,int phyaddr,int regidx);
  247. static void sbmac_mii_write(struct sbmac_softc *s,int phyaddr,int regidx,
  248.     unsigned int regval);
  249. /**********************************************************************
  250.  *  Globals
  251.  ********************************************************************* */
  252. static uint64_t sbmac_orig_hwaddr[MAX_UNITS];
  253. static uint64_t chip_revision;
  254. /**********************************************************************
  255.  *  MDIO constants
  256.  ********************************************************************* */
  257. #define MII_COMMAND_START 0x01
  258. #define MII_COMMAND_READ 0x02
  259. #define MII_COMMAND_WRITE 0x01
  260. #define MII_COMMAND_ACK 0x02
  261. #define BMCR_RESET     0x8000
  262. #define BMCR_LOOPBACK  0x4000
  263. #define BMCR_SPEED0    0x2000
  264. #define BMCR_ANENABLE  0x1000
  265. #define BMCR_POWERDOWN 0x0800
  266. #define BMCR_ISOLATE   0x0400
  267. #define BMCR_RESTARTAN 0x0200
  268. #define BMCR_DUPLEX    0x0100
  269. #define BMCR_COLTEST   0x0080
  270. #define BMCR_SPEED1    0x0040
  271. #define BMCR_SPEED1000 (BMCR_SPEED1)
  272. #define BMCR_SPEED100  (BMCR_SPEED0)
  273. #define BMCR_SPEED10  0
  274. #define BMSR_100BT4 0x8000
  275. #define BMSR_100BT_FDX 0x4000
  276. #define BMSR_100BT_HDX  0x2000
  277. #define BMSR_10BT_FDX   0x1000
  278. #define BMSR_10BT_HDX   0x0800
  279. #define BMSR_100BT2_FDX 0x0400
  280. #define BMSR_100BT2_HDX 0x0200
  281. #define BMSR_1000BT_XSR 0x0100
  282. #define BMSR_PRESUP 0x0040
  283. #define BMSR_ANCOMPLT 0x0020
  284. #define BMSR_REMFAULT 0x0010
  285. #define BMSR_AUTONEG 0x0008
  286. #define BMSR_LINKSTAT 0x0004
  287. #define BMSR_JABDETECT 0x0002
  288. #define BMSR_EXTCAPAB 0x0001
  289. #define PHYIDR1  0x2000
  290. #define PHYIDR2 0x5C60
  291. #define ANAR_NP 0x8000
  292. #define ANAR_RF 0x2000
  293. #define ANAR_ASYPAUSE 0x0800
  294. #define ANAR_PAUSE 0x0400
  295. #define ANAR_T4 0x0200
  296. #define ANAR_TXFD 0x0100
  297. #define ANAR_TXHD 0x0080
  298. #define ANAR_10FD 0x0040
  299. #define ANAR_10HD 0x0020
  300. #define ANAR_PSB 0x0001
  301. #define ANLPAR_NP 0x8000
  302. #define ANLPAR_ACK 0x4000
  303. #define ANLPAR_RF 0x2000
  304. #define ANLPAR_ASYPAUSE 0x0800
  305. #define ANLPAR_PAUSE 0x0400
  306. #define ANLPAR_T4 0x0200
  307. #define ANLPAR_TXFD 0x0100
  308. #define ANLPAR_TXHD 0x0080
  309. #define ANLPAR_10FD 0x0040
  310. #define ANLPAR_10HD 0x0020
  311. #define ANLPAR_PSB 0x0001 /* 802.3 */
  312. #define ANER_PDF 0x0010
  313. #define ANER_LPNPABLE 0x0008
  314. #define ANER_NPABLE 0x0004
  315. #define ANER_PAGERX 0x0002
  316. #define ANER_LPANABLE 0x0001
  317. #define ANNPTR_NP 0x8000
  318. #define ANNPTR_MP 0x2000
  319. #define ANNPTR_ACK2 0x1000
  320. #define ANNPTR_TOGTX 0x0800
  321. #define ANNPTR_CODE 0x0008
  322. #define ANNPRR_NP 0x8000
  323. #define ANNPRR_MP 0x2000
  324. #define ANNPRR_ACK3 0x1000
  325. #define ANNPRR_TOGTX 0x0800
  326. #define ANNPRR_CODE 0x0008
  327. #define K1TCR_TESTMODE 0x0000
  328. #define K1TCR_MSMCE 0x1000
  329. #define K1TCR_MSCV 0x0800
  330. #define K1TCR_RPTR 0x0400
  331. #define K1TCR_1000BT_FDX 0x200
  332. #define K1TCR_1000BT_HDX 0x100
  333. #define K1STSR_MSMCFLT 0x8000
  334. #define K1STSR_MSCFGRES 0x4000
  335. #define K1STSR_LRSTAT 0x2000
  336. #define K1STSR_RRSTAT 0x1000
  337. #define K1STSR_LP1KFD 0x0800
  338. #define K1STSR_LP1KHD   0x0400
  339. #define K1STSR_LPASMDIR 0x0200
  340. #define K1SCR_1KX_FDX 0x8000
  341. #define K1SCR_1KX_HDX 0x4000
  342. #define K1SCR_1KT_FDX 0x2000
  343. #define K1SCR_1KT_HDX 0x1000
  344. #define STRAP_PHY1 0x0800
  345. #define STRAP_NCMODE 0x0400
  346. #define STRAP_MANMSCFG 0x0200
  347. #define STRAP_ANENABLE 0x0100
  348. #define STRAP_MSVAL 0x0080
  349. #define STRAP_1KHDXADV 0x0010
  350. #define STRAP_1KFDXADV 0x0008
  351. #define STRAP_100ADV 0x0004
  352. #define STRAP_SPEEDSEL 0x0000
  353. #define STRAP_SPEED100 0x0001
  354. #define PHYSUP_SPEED1000 0x10
  355. #define PHYSUP_SPEED100  0x08
  356. #define PHYSUP_SPEED10   0x00
  357. #define PHYSUP_LINKUP  0x04
  358. #define PHYSUP_FDX       0x02
  359. #define MII_BMCR 0x00  /* Basic mode control register (rw) */
  360. #define MII_BMSR 0x01 /* Basic mode status register (ro) */
  361. #define MII_K1STSR 0x0A /* 1K Status Register (ro) */
  362. #define MII_ANLPAR 0x05 /* Autonegotiation lnk partner abilities (rw) */
  363. #define M_MAC_MDIO_DIR_OUTPUT 0 /* for clarity */
  364. #define ENABLE  1
  365. #define DISABLE 0
  366. /**********************************************************************
  367.  *  SBMAC_MII_SYNC(s)
  368.  *  
  369.  *  Synchronize with the MII - send a pattern of bits to the MII
  370.  *  that will guarantee that it is ready to accept a command.
  371.  *  
  372.  *  Input parameters: 
  373.  *      s - sbmac structure
  374.  *      
  375.  *  Return value:
  376.  *      nothing
  377.  ********************************************************************* */
  378. static void sbmac_mii_sync(struct sbmac_softc *s)
  379. {
  380. int cnt;
  381. uint64_t bits;
  382. bits = M_MAC_MDIO_DIR_OUTPUT | M_MAC_MDIO_OUT;
  383. SBMAC_WRITECSR(s->sbm_mdio,bits);
  384. for (cnt = 0; cnt < 32; cnt++) {
  385. SBMAC_WRITECSR(s->sbm_mdio,bits | M_MAC_MDC);
  386. SBMAC_WRITECSR(s->sbm_mdio,bits);
  387. }
  388. }
  389. /**********************************************************************
  390.  *  SBMAC_MII_SENDDATA(s,data,bitcnt)
  391.  *  
  392.  *  Send some bits to the MII.  The bits to be sent are right-
  393.  *  justified in the 'data' parameter.
  394.  *  
  395.  *  Input parameters: 
  396.  *      s - sbmac structure
  397.  *      data - data to send
  398.  *      bitcnt - number of bits to send
  399.  ********************************************************************* */
  400. static void sbmac_mii_senddata(struct sbmac_softc *s,unsigned int data, int bitcnt)
  401. {
  402. int i;
  403. uint64_t bits;
  404. unsigned int curmask;
  405. bits = M_MAC_MDIO_DIR_OUTPUT;
  406. SBMAC_WRITECSR(s->sbm_mdio,bits);
  407. curmask = 1 << (bitcnt - 1);
  408. for (i = 0; i < bitcnt; i++) {
  409. if (data & curmask) bits |= M_MAC_MDIO_OUT;
  410. else bits &= ~M_MAC_MDIO_OUT;
  411. SBMAC_WRITECSR(s->sbm_mdio,bits);
  412. SBMAC_WRITECSR(s->sbm_mdio,bits | M_MAC_MDC);
  413. SBMAC_WRITECSR(s->sbm_mdio,bits);
  414. curmask >>= 1;
  415. }
  416. }
  417. /**********************************************************************
  418.  *  SBMAC_MII_READ(s,phyaddr,regidx)
  419.  *  
  420.  *  Read a PHY register.
  421.  *  
  422.  *  Input parameters: 
  423.  *      s - sbmac structure
  424.  *      phyaddr - PHY's address
  425.  *      regidx = index of register to read
  426.  *      
  427.  *  Return value:
  428.  *      value read, or 0 if an error occured.
  429.  ********************************************************************* */
  430. static unsigned int sbmac_mii_read(struct sbmac_softc *s,int phyaddr,int regidx)
  431. {
  432. int idx;
  433. int error;
  434. int regval;
  435. /*
  436.  * Synchronize ourselves so that the PHY knows the next
  437.  * thing coming down is a command
  438.  */
  439. sbmac_mii_sync(s);
  440. /*
  441.  * Send the data to the PHY.  The sequence is
  442.  * a "start" command (2 bits)
  443.  * a "read" command (2 bits)
  444.  * the PHY addr (5 bits)
  445.  * the register index (5 bits)
  446.  */
  447. sbmac_mii_senddata(s,MII_COMMAND_START, 2);
  448. sbmac_mii_senddata(s,MII_COMMAND_READ, 2);
  449. sbmac_mii_senddata(s,phyaddr, 5);
  450. sbmac_mii_senddata(s,regidx, 5);
  451. /* 
  452.  * Switch the port around without a clock transition.
  453.  */
  454. SBMAC_WRITECSR(s->sbm_mdio,M_MAC_MDIO_DIR_INPUT);
  455. /*
  456.  * Send out a clock pulse to signal we want the status
  457.  */
  458. SBMAC_WRITECSR(s->sbm_mdio,M_MAC_MDIO_DIR_INPUT | M_MAC_MDC);
  459. SBMAC_WRITECSR(s->sbm_mdio,M_MAC_MDIO_DIR_INPUT);
  460. /* 
  461.  * If an error occured, the PHY will signal '1' back
  462.  */
  463. error = SBMAC_READCSR(s->sbm_mdio) & M_MAC_MDIO_IN;
  464. /* 
  465.  * Issue an 'idle' clock pulse, but keep the direction
  466.  * the same.
  467.  */
  468. SBMAC_WRITECSR(s->sbm_mdio,M_MAC_MDIO_DIR_INPUT | M_MAC_MDC);
  469. SBMAC_WRITECSR(s->sbm_mdio,M_MAC_MDIO_DIR_INPUT);
  470. regval = 0;
  471. for (idx = 0; idx < 16; idx++) {
  472. regval <<= 1;
  473. if (error == 0) {
  474. if (SBMAC_READCSR(s->sbm_mdio) & M_MAC_MDIO_IN) regval |= 1;
  475. }
  476. SBMAC_WRITECSR(s->sbm_mdio,M_MAC_MDIO_DIR_INPUT | M_MAC_MDC);
  477. SBMAC_WRITECSR(s->sbm_mdio,M_MAC_MDIO_DIR_INPUT);
  478. }
  479. /* Switch back to output */
  480. SBMAC_WRITECSR(s->sbm_mdio,M_MAC_MDIO_DIR_OUTPUT);
  481. if (error == 0) return regval;
  482. return 0;
  483. }
  484. /**********************************************************************
  485.  *  SBMAC_MII_WRITE(s,phyaddr,regidx,regval)
  486.  *  
  487.  *  Write a value to a PHY register.
  488.  *  
  489.  *  Input parameters: 
  490.  *      s - sbmac structure
  491.  *      phyaddr - PHY to use
  492.  *      regidx - register within the PHY
  493.  *      regval - data to write to register
  494.  *      
  495.  *  Return value:
  496.  *      nothing
  497.  ********************************************************************* */
  498. static void sbmac_mii_write(struct sbmac_softc *s,int phyaddr,int regidx,
  499.     unsigned int regval)
  500. {
  501. sbmac_mii_sync(s);
  502. sbmac_mii_senddata(s,MII_COMMAND_START,2);
  503. sbmac_mii_senddata(s,MII_COMMAND_WRITE,2);
  504. sbmac_mii_senddata(s,phyaddr, 5);
  505. sbmac_mii_senddata(s,regidx, 5);
  506. sbmac_mii_senddata(s,MII_COMMAND_ACK,2);
  507. sbmac_mii_senddata(s,regval,16);
  508. SBMAC_WRITECSR(s->sbm_mdio,M_MAC_MDIO_DIR_OUTPUT);
  509. }
  510. /**********************************************************************
  511.  *  SBDMA_INITCTX(d,s,chan,txrx,maxdescr)
  512.  *  
  513.  *  Initialize a DMA channel context.  Since there are potentially
  514.  *  eight DMA channels per MAC, it's nice to do this in a standard
  515.  *  way.  
  516.  *  
  517.  *  Input parameters: 
  518.  *      d - sbmacdma_t structure (DMA channel context)
  519.  *      s - sbmac_softc structure (pointer to a MAC)
  520.  *      chan - channel number (0..1 right now)
  521.  *      txrx - Identifies DMA_TX or DMA_RX for channel direction
  522.  *      maxdescr - number of descriptors
  523.  *      
  524.  *  Return value:
  525.  *      nothing
  526.  ********************************************************************* */
  527. static void sbdma_initctx(sbmacdma_t *d,
  528.   struct sbmac_softc *s,
  529.   int chan,
  530.   int txrx,
  531.   int maxdescr)
  532. {
  533. /* 
  534.  * Save away interesting stuff in the structure 
  535.  */
  536. d->sbdma_eth       = s;
  537. d->sbdma_channel   = chan;
  538. d->sbdma_txdir     = txrx;
  539. /* RMON clearing */
  540. s->sbe_idx =(s->sbm_base - A_MAC_BASE_0)/MAC_SPACING;
  541. SBMAC_WRITECSR(PKSEG1(
  542.         A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_TX_BYTES)), 0);
  543. SBMAC_WRITECSR(PKSEG1(
  544.         A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_COLLISIONS)), 0);
  545. SBMAC_WRITECSR(PKSEG1(
  546.         A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_LATE_COL)), 0);
  547. SBMAC_WRITECSR(PKSEG1(
  548.         A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_EX_COL)), 0);
  549. SBMAC_WRITECSR(PKSEG1(
  550.         A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_FCS_ERROR)), 0);
  551. SBMAC_WRITECSR(PKSEG1(
  552.         A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_TX_ABORT)), 0);
  553. SBMAC_WRITECSR(PKSEG1(
  554.         A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_TX_BAD)), 0);
  555. SBMAC_WRITECSR(PKSEG1(
  556.         A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_TX_GOOD)), 0);
  557. SBMAC_WRITECSR(PKSEG1(
  558.         A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_TX_RUNT)), 0);
  559. SBMAC_WRITECSR(PKSEG1(
  560.         A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_TX_OVERSIZE)), 0);
  561. SBMAC_WRITECSR(PKSEG1(
  562.         A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_BYTES)), 0);
  563. SBMAC_WRITECSR(PKSEG1(
  564.         A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_MCAST)), 0);
  565. SBMAC_WRITECSR(PKSEG1(
  566.         A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_BCAST)), 0);
  567. SBMAC_WRITECSR(PKSEG1(
  568.         A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_BAD)), 0);
  569. SBMAC_WRITECSR(PKSEG1(
  570.         A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_GOOD)), 0);
  571. SBMAC_WRITECSR(PKSEG1(
  572.         A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_RUNT)), 0);
  573. SBMAC_WRITECSR(PKSEG1(
  574.         A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_OVERSIZE)), 0);
  575. SBMAC_WRITECSR(PKSEG1(
  576.         A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_FCS_ERROR)), 0);
  577. SBMAC_WRITECSR(PKSEG1(
  578.         A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_LENGTH_ERROR)), 0);
  579. SBMAC_WRITECSR(PKSEG1(
  580.         A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_CODE_ERROR)), 0);
  581. SBMAC_WRITECSR(PKSEG1(
  582.         A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_ALIGN_ERROR)), 0);
  583. /* 
  584.  * initialize register pointers 
  585.  */
  586. d->sbdma_config0 = 
  587. PKSEG1(s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CONFIG0));
  588. d->sbdma_config1 = 
  589. PKSEG1(s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CONFIG1));
  590. d->sbdma_dscrbase = 
  591. PKSEG1(s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_DSCR_BASE));
  592. d->sbdma_dscrcnt = 
  593. PKSEG1(s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_DSCR_CNT));
  594. d->sbdma_curdscr = 
  595. PKSEG1(s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CUR_DSCRADDR));
  596. /*
  597.  * Allocate memory for the ring
  598.  */
  599. d->sbdma_maxdescr = maxdescr;
  600. d->sbdma_dscrtable = (sbdmadscr_t *) 
  601. KMALLOC(d->sbdma_maxdescr*sizeof(sbdmadscr_t));
  602. memset(d->sbdma_dscrtable,0,d->sbdma_maxdescr*sizeof(sbdmadscr_t));
  603. d->sbdma_dscrtable_end = d->sbdma_dscrtable + d->sbdma_maxdescr;
  604. d->sbdma_dscrtable_phys = KVTOPHYS(d->sbdma_dscrtable);
  605. /*
  606.  * And context table
  607.  */
  608. d->sbdma_ctxtable = (struct sk_buff **) 
  609. KMALLOC(d->sbdma_maxdescr*sizeof(struct sk_buff *));
  610. memset(d->sbdma_ctxtable,0,d->sbdma_maxdescr*sizeof(struct sk_buff *));
  611. #ifdef CONFIG_SBMAC_COALESCE
  612.         /*
  613.          * Setup Rx DMA coalescing defaults
  614.          */
  615.         if ( txrx == DMA_RX ) {
  616. if ( int_pktcnt ) {
  617.                  d->sbdma_int_pktcnt = int_pktcnt;
  618.         }
  619. else {
  620.                  d->sbdma_int_pktcnt = 1;
  621.         }
  622. if ( int_timeout ) {
  623.         d->sbdma_int_timeout = int_timeout;
  624.         }
  625. else {
  626.         d->sbdma_int_timeout = 0;
  627.     }
  628.         }
  629.         else {
  630.                 d->sbdma_int_pktcnt = 0;
  631.                 d->sbdma_int_timeout = 0;
  632.         }
  633. #endif
  634. }
  635. /**********************************************************************
  636.  *  SBDMA_CHANNEL_START(d)
  637.  *  
  638.  *  Initialize the hardware registers for a DMA channel.
  639.  *  
  640.  *  Input parameters: 
  641.  *      d - DMA channel to init (context must be previously init'd
  642.  *         rxtx - DMA_RX or DMA_TX depending on what type of channel
  643.  *      
  644.  *  Return value:
  645.  *      nothing
  646.  ********************************************************************* */
  647. static void sbdma_channel_start(sbmacdma_t *d, int rxtx )
  648. {
  649.     /*
  650.      * Turn on the DMA channel
  651.      */
  652. #ifdef CONFIG_SBMAC_COALESCE
  653.     if (rxtx == DMA_RX) {
  654.         SBMAC_WRITECSR(d->sbdma_config1,
  655.        V_DMA_INT_TIMEOUT(d->sbdma_int_timeout) |
  656.        0);
  657.         SBMAC_WRITECSR(d->sbdma_config0,
  658.                        M_DMA_EOP_INT_EN |
  659.                        V_DMA_RINGSZ(d->sbdma_maxdescr) |
  660.                        V_DMA_INT_PKTCNT(d->sbdma_int_pktcnt) |
  661.                        0);
  662. }
  663.     else {
  664. SBMAC_WRITECSR(d->sbdma_config1,0);
  665. SBMAC_WRITECSR(d->sbdma_config0,
  666.        V_DMA_RINGSZ(d->sbdma_maxdescr) |
  667.        0);
  668. }
  669. #else
  670.     SBMAC_WRITECSR(d->sbdma_config1,0);
  671.     SBMAC_WRITECSR(d->sbdma_config0,
  672.    V_DMA_RINGSZ(d->sbdma_maxdescr) |
  673.    0);
  674. #endif
  675.     SBMAC_WRITECSR(d->sbdma_dscrbase,d->sbdma_dscrtable_phys);
  676.     /*
  677.      * Initialize ring pointers
  678.      */
  679.     d->sbdma_addptr = d->sbdma_dscrtable;
  680.     d->sbdma_remptr = d->sbdma_dscrtable;
  681. }
  682. /**********************************************************************
  683.  *  SBDMA_CHANNEL_STOP(d)
  684.  *  
  685.  *  Initialize the hardware registers for a DMA channel.
  686.  *  
  687.  *  Input parameters: 
  688.  *      d - DMA channel to init (context must be previously init'd
  689.  *      
  690.  *  Return value:
  691.  *      nothing
  692.  ********************************************************************* */
  693. static void sbdma_channel_stop(sbmacdma_t *d)
  694. {
  695. /*
  696.  * Turn off the DMA channel
  697.  */
  698. SBMAC_WRITECSR(d->sbdma_config1,0);
  699. SBMAC_WRITECSR(d->sbdma_dscrbase,0);
  700. SBMAC_WRITECSR(d->sbdma_config0,0);
  701. /*
  702.  * Zero ring pointers
  703.  */
  704. d->sbdma_addptr = 0;
  705. d->sbdma_remptr = 0;
  706. }
  707. static void sbdma_align_skb(struct sk_buff *skb,int power2,int offset)
  708. {
  709. unsigned long addr;
  710. unsigned long newaddr;
  711. addr = (unsigned long) skb->data;
  712. newaddr = (addr + power2 - 1) & ~(power2 - 1);
  713. skb_reserve(skb,newaddr-addr+offset);
  714. }
  715. /**********************************************************************
  716.  *  SBDMA_ADD_RCVBUFFER(d,sb)
  717.  *  
  718.  *  Add a buffer to the specified DMA channel.   For receive channels,
  719.  *  this queues a buffer for inbound packets.
  720.  *  
  721.  *  Input parameters: 
  722.  *      d - DMA channel descriptor
  723.  *     sb - sk_buff to add, or NULL if we should allocate one
  724.  *      
  725.  *  Return value:
  726.  *      0 if buffer could not be added (ring is full)
  727.  *      1 if buffer added successfully
  728.  ********************************************************************* */
  729. static int sbdma_add_rcvbuffer(sbmacdma_t *d,struct sk_buff *sb)
  730. {
  731. sbdmadscr_t *dsc;
  732. sbdmadscr_t *nextdsc;
  733. struct sk_buff *sb_new = NULL;
  734. int pktsize = ENET_PACKET_SIZE;
  735. /* get pointer to our current place in the ring */
  736. dsc = d->sbdma_addptr;
  737. nextdsc = SBDMA_NEXTBUF(d,sbdma_addptr);
  738. /*
  739.  * figure out if the ring is full - if the next descriptor
  740.  * is the same as the one that we're going to remove from
  741.  * the ring, the ring is full
  742.  */
  743. if (nextdsc == d->sbdma_remptr) {
  744. return -ENOSPC;
  745. }
  746. /* 
  747.  * Allocate a sk_buff if we don't already have one.  
  748.  * If we do have an sk_buff, reset it so that it's empty.
  749.  *
  750.  * Note: sk_buffs don't seem to be guaranteed to have any sort
  751.  * of alignment when they are allocated.  Therefore, allocate enough
  752.  * extra space to make sure that:
  753.  *
  754.  *    1. the data does not start in the middle of a cache line.
  755.  *    2. The data does not end in the middle of a cache line
  756.  *    3. The buffer can be aligned such that the IP addresses are 
  757.  *       naturally aligned.
  758.  *
  759.  *  Remember, the SB1250's MAC writes whole cache lines at a time,
  760.  *  without reading the old contents first.  So, if the sk_buff's
  761.  *  data portion starts in the middle of a cache line, the SB1250
  762.  *  DMA will trash the beginning (and ending) portions.
  763.  */
  764. if (sb == NULL) {
  765. sb_new = dev_alloc_skb(ENET_PACKET_SIZE + CACHELINESIZE*2 + ETHER_ALIGN);
  766. if (sb_new == NULL) {
  767. printk(KERN_INFO "%s: sk_buff allocation failedn",
  768.        d->sbdma_eth->sbm_dev->name);
  769. return -ENOBUFS;
  770. }
  771. sbdma_align_skb(sb_new,CACHELINESIZE,ETHER_ALIGN);
  772. /* mark skbuff owned by our device */
  773. sb_new->dev = d->sbdma_eth->sbm_dev;
  774. }
  775. else {
  776. sb_new = sb;
  777. /* 
  778.  * nothing special to reinit buffer, it's already aligned
  779.  * and sb->tail already points to a good place.
  780.  */
  781. }
  782. /*
  783.  * fill in the descriptor 
  784.  */
  785. #ifdef CONFIG_SBMAC_COALESCE
  786.         /*
  787.          * Do not interrupt per DMA transfer.
  788.          */
  789.         dsc->dscr_a = KVTOPHYS(sb_new->tail) |
  790.                 V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize+ETHER_ALIGN)) |
  791.                 0;
  792. #else
  793. dsc->dscr_a = KVTOPHYS(sb_new->tail) |
  794. V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize+ETHER_ALIGN)) |
  795. M_DMA_DSCRA_INTERRUPT;
  796. #endif
  797. /* receiving: no options */
  798. dsc->dscr_b = 0;
  799. /*
  800.  * fill in the context 
  801.  */
  802. d->sbdma_ctxtable[dsc-d->sbdma_dscrtable] = sb_new;
  803. /* 
  804.  * point at next packet 
  805.  */
  806. d->sbdma_addptr = nextdsc;
  807. /* 
  808.  * Give the buffer to the DMA engine.
  809.  */
  810. SBMAC_WRITECSR(d->sbdma_dscrcnt,1);
  811. return 0; /* we did it */
  812. }
  813. /**********************************************************************
  814.  *  SBDMA_ADD_TXBUFFER(d,sb)
  815.  *  
  816.  *  Add a transmit buffer to the specified DMA channel, causing a
  817.  *  transmit to start.
  818.  *  
  819.  *  Input parameters: 
  820.  *      d - DMA channel descriptor
  821.  *     sb - sk_buff to add
  822.  *      
  823.  *  Return value:
  824.  *      0 transmit queued successfully
  825.  *      otherwise error code
  826.  ********************************************************************* */
  827. static int sbdma_add_txbuffer(sbmacdma_t *d,struct sk_buff *sb)
  828. {
  829. sbdmadscr_t *dsc;
  830. sbdmadscr_t *nextdsc;
  831. uint64_t phys;
  832. uint64_t ncb;
  833. int length;
  834. /* get pointer to our current place in the ring */
  835. dsc = d->sbdma_addptr;
  836. nextdsc = SBDMA_NEXTBUF(d,sbdma_addptr);
  837. /*
  838.  * figure out if the ring is full - if the next descriptor
  839.  * is the same as the one that we're going to remove from
  840.  * the ring, the ring is full
  841.  */
  842. if (nextdsc == d->sbdma_remptr) {
  843. return -ENOSPC;
  844. }
  845. /*
  846.  * Under Linux, it's not necessary to copy/coalesce buffers
  847.  * like it is on NetBSD.  We think they're all contiguous,
  848.  * but that may not be true for GBE.
  849.  */
  850. length = sb->len;
  851. /*
  852.  * fill in the descriptor.  Note that the number of cache
  853.  * blocks in the descriptor is the number of blocks
  854.  * *spanned*, so we need to add in the offset (if any)
  855.  * while doing the calculation.
  856.  */
  857. phys = KVTOPHYS(sb->data);
  858. ncb = NUMCACHEBLKS(length+(phys & (CACHELINESIZE-1)));
  859. dsc->dscr_a = phys | 
  860. V_DMA_DSCRA_A_SIZE(ncb) |
  861. M_DMA_DSCRA_INTERRUPT |
  862. M_DMA_ETHTX_SOP;
  863. /* transmitting: set outbound options and length */
  864. dsc->dscr_b = V_DMA_DSCRB_OPTIONS(K_DMA_ETHTX_APPENDCRC_APPENDPAD) |
  865. V_DMA_DSCRB_PKT_SIZE(length);
  866. /*
  867.  * fill in the context 
  868.  */
  869. d->sbdma_ctxtable[dsc-d->sbdma_dscrtable] = sb;
  870. /* 
  871.  * point at next packet 
  872.  */
  873. d->sbdma_addptr = nextdsc;
  874. /* 
  875.  * Give the buffer to the DMA engine.
  876.  */
  877. SBMAC_WRITECSR(d->sbdma_dscrcnt,1);
  878. return 0; /* we did it */
  879. }
  880. /**********************************************************************
  881.  *  SBDMA_EMPTYRING(d)
  882.  *  
  883.  *  Free all allocated sk_buffs on the specified DMA channel;
  884.  *  
  885.  *  Input parameters: 
  886.  *      d  - DMA channel
  887.  *      
  888.  *  Return value:
  889.  *      nothing
  890.  ********************************************************************* */
  891. static void sbdma_emptyring(sbmacdma_t *d)
  892. {
  893. int idx;
  894. struct sk_buff *sb;
  895. for (idx = 0; idx < d->sbdma_maxdescr; idx++) {
  896. sb = d->sbdma_ctxtable[idx];
  897. if (sb) {
  898. dev_kfree_skb(sb);
  899. d->sbdma_ctxtable[idx] = NULL;
  900. }
  901. }
  902. }
  903. /**********************************************************************
  904.  *  SBDMA_FILLRING(d)
  905.  *  
  906.  *  Fill the specified DMA channel (must be receive channel)
  907.  *  with sk_buffs
  908.  *  
  909.  *  Input parameters: 
  910.  *      d - DMA channel
  911.  *      
  912.  *  Return value:
  913.  *      nothing
  914.  ********************************************************************* */
  915. static void sbdma_fillring(sbmacdma_t *d)
  916. {
  917. int idx;
  918. for (idx = 0; idx < SBMAC_MAX_RXDESCR-1; idx++) {
  919. if (sbdma_add_rcvbuffer(d,NULL) != 0) break;
  920. }
  921. }
  922. /**********************************************************************
  923.  *  SBDMA_RX_PROCESS(sc,d)
  924.  *  
  925.  *  Process "completed" receive buffers on the specified DMA channel.  
  926.  *  Note that this isn't really ideal for priority channels, since
  927.  *  it processes all of the packets on a given channel before 
  928.  *  returning. 
  929.  *
  930.  *  Input parameters: 
  931.  *    sc - softc structure
  932.  *      d - DMA channel context
  933.  *      
  934.  *  Return value:
  935.  *      nothing
  936.  ********************************************************************* */
  937. static void sbdma_rx_process(struct sbmac_softc *sc,sbmacdma_t *d)
  938. {
  939. int curidx;
  940. int hwidx;
  941. sbdmadscr_t *dsc;
  942. struct sk_buff *sb;
  943. int len;
  944. for (;;) {
  945. /* 
  946.  * figure out where we are (as an index) and where
  947.  * the hardware is (also as an index)
  948.  *
  949.  * This could be done faster if (for example) the 
  950.  * descriptor table was page-aligned and contiguous in
  951.  * both virtual and physical memory -- you could then
  952.  * just compare the low-order bits of the virtual address
  953.  * (sbdma_remptr) and the physical address (sbdma_curdscr CSR)
  954.  */
  955. curidx = d->sbdma_remptr - d->sbdma_dscrtable;
  956. hwidx = (int) (((SBMAC_READCSR(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
  957. d->sbdma_dscrtable_phys) / sizeof(sbdmadscr_t));
  958. /*
  959.  * If they're the same, that means we've processed all
  960.  * of the descriptors up to (but not including) the one that
  961.  * the hardware is working on right now.
  962.  */
  963. if (curidx == hwidx) break;
  964. /*
  965.  * Otherwise, get the packet's sk_buff ptr back
  966.  */
  967. dsc = &(d->sbdma_dscrtable[curidx]);
  968. sb = d->sbdma_ctxtable[curidx];
  969. d->sbdma_ctxtable[curidx] = NULL;
  970. len = (int)G_DMA_DSCRB_PKT_SIZE(dsc->dscr_b) - 4;
  971. /*
  972.  * Check packet status.  If good, process it.
  973.  * If not, silently drop it and put it back on the
  974.  * receive ring.
  975.  */
  976. if (!(dsc->dscr_a & M_DMA_ETHRX_BAD)) {
  977. /*
  978.  * Set length into the packet
  979.  */
  980. skb_put(sb,len);
  981.         /*
  982.  * Add a new buffer to replace the old one.  If we fail
  983.  * to allocate a buffer, we're going to drop this
  984.  * packet and put it right back on the receive ring.
  985.  */
  986. if (sbdma_add_rcvbuffer(d,NULL) == -ENOBUFS) {
  987.     sbdma_add_rcvbuffer(d,sb); /* re-add old buffer */
  988.     }
  989. else {
  990.     /*
  991.      * Buffer has been replaced on the receive ring.
  992.      * Pass the buffer to the kernel
  993.      */
  994.     sc->sbm_stats.rx_bytes += len;
  995.     sc->sbm_stats.rx_packets++;
  996.     sb->protocol = eth_type_trans(sb,d->sbdma_eth->sbm_dev);
  997.                             if (sc->rx_hw_checksum == ENABLE) {
  998.     /* if the ip checksum is good indicate in skb.
  999.                 else set CHECKSUM_NONE as device failed to
  1000. checksum the packet */
  1001.        if (((dsc->dscr_b) |M_DMA_ETHRX_BADTCPCS) ||
  1002.         ((dsc->dscr_a)| M_DMA_ETHRX_BADIP4CS)){
  1003.   sb->ip_summed = CHECKSUM_NONE;
  1004.        } else {
  1005.  printk(KERN_DEBUG "hw checksum fail .n");
  1006.  sb->ip_summed = CHECKSUM_UNNECESSARY;
  1007.        }
  1008.     } /*rx_hw_checksum */
  1009.     netif_rx(sb);
  1010.     }
  1011. }
  1012. else {
  1013. /*
  1014.  * Packet was mangled somehow.  Just drop it and
  1015.  * put it back on the receive ring.
  1016.  */
  1017. sbdma_add_rcvbuffer(d,sb);
  1018. }
  1019. /* 
  1020.  * .. and advance to the next buffer.
  1021.  */
  1022. d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
  1023. }
  1024. }
  1025. /**********************************************************************
  1026.  *  SBDMA_TX_PROCESS(sc,d)
  1027.  *  
  1028.  *  Process "completed" transmit buffers on the specified DMA channel.  
  1029.  *  This is normally called within the interrupt service routine.
  1030.  *  Note that this isn't really ideal for priority channels, since
  1031.  *  it processes all of the packets on a given channel before 
  1032.  *  returning. 
  1033.  *
  1034.  *  Input parameters: 
  1035.  *      sc - softc structure
  1036.  *      d - DMA channel context
  1037.  *      
  1038.  *  Return value:
  1039.  *      nothing
  1040.  ********************************************************************* */
  1041. static void sbdma_tx_process(struct sbmac_softc *sc,sbmacdma_t *d)
  1042. {
  1043. int curidx;
  1044. int hwidx;
  1045. sbdmadscr_t *dsc;
  1046. struct sk_buff *sb;
  1047. unsigned long flags;
  1048. spin_lock_irqsave(&(sc->sbm_lock), flags);
  1049. for (;;) {
  1050. /* 
  1051.  * figure out where we are (as an index) and where
  1052.  * the hardware is (also as an index)
  1053.  *
  1054.  * This could be done faster if (for example) the 
  1055.  * descriptor table was page-aligned and contiguous in
  1056.  * both virtual and physical memory -- you could then
  1057.  * just compare the low-order bits of the virtual address
  1058.  * (sbdma_remptr) and the physical address (sbdma_curdscr CSR)
  1059.  */
  1060. curidx = d->sbdma_remptr - d->sbdma_dscrtable;
  1061. {
  1062. /* XXX This is gross, ugly, and only here because justin hacked it
  1063.    in to fix a problem without really understanding it. 
  1064.    
  1065.    It seems that, for whatever reason, this routine is invoked immediately upon the enabling of interrupts.
  1066.    So then the Read below returns zero, making hwidx a negative number, and anti-hilarity
  1067.    ensues.
  1068.    
  1069.    I'm guessing there's a proper fix involving clearing out interrupt state from old packets
  1070.    before enabling interrupts, but I'm not sure.  
  1071.    Anyways, this hack seems to work, and is Good Enough for 11 PM.  :)
  1072.    
  1073.    -Justin
  1074. */
  1075.   
  1076. uint64_t tmp = SBMAC_READCSR(d->sbdma_curdscr);
  1077. if (!tmp) {
  1078. break;
  1079. }
  1080. hwidx = (int) (((tmp & M_DMA_CURDSCR_ADDR) -
  1081. d->sbdma_dscrtable_phys) / sizeof(sbdmadscr_t));
  1082. }
  1083. /*
  1084.  * If they're the same, that means we've processed all
  1085.  * of the descriptors up to (but not including) the one that
  1086.  * the hardware is working on right now.
  1087.  */
  1088. if (curidx == hwidx) break;
  1089. /*
  1090.  * Otherwise, get the packet's sk_buff ptr back
  1091.  */
  1092. dsc = &(d->sbdma_dscrtable[curidx]);
  1093. sb = d->sbdma_ctxtable[curidx];
  1094. d->sbdma_ctxtable[curidx] = NULL;
  1095. /*
  1096.  * Stats
  1097.  */
  1098. sc->sbm_stats.tx_bytes += sb->len;
  1099. sc->sbm_stats.tx_packets++;
  1100. /*
  1101.  * for transmits, we just free buffers.
  1102.  */
  1103. dev_kfree_skb_irq(sb);
  1104. /* 
  1105.  * .. and advance to the next buffer.
  1106.  */
  1107. d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
  1108. }
  1109. /*
  1110.  * Decide if we should wake up the protocol or not.
  1111.  * Other drivers seem to do this when we reach a low
  1112.  * watermark on the transmit queue.
  1113.  */
  1114. netif_wake_queue(d->sbdma_eth->sbm_dev);
  1115. spin_unlock_irqrestore(&(sc->sbm_lock), flags);
  1116. }
  1117. /**********************************************************************
  1118.  *  SBMAC_INITCTX(s)
  1119.  *  
  1120.  *  Initialize an Ethernet context structure - this is called
  1121.  *  once per MAC on the 1250.  Memory is allocated here, so don't
  1122.  *  call it again from inside the ioctl routines that bring the
  1123.  *  interface up/down
  1124.  *  
  1125.  *  Input parameters: 
  1126.  *      s - sbmac context structure
  1127.  *      
  1128.  *  Return value:
  1129.  *      0
  1130.  ********************************************************************* */
  1131. static int sbmac_initctx(struct sbmac_softc *s)
  1132. {
  1133. /* 
  1134.  * figure out the addresses of some ports 
  1135.  */
  1136. s->sbm_macenable = PKSEG1(s->sbm_base + R_MAC_ENABLE);
  1137. s->sbm_maccfg    = PKSEG1(s->sbm_base + R_MAC_CFG);
  1138. s->sbm_fifocfg   = PKSEG1(s->sbm_base + R_MAC_THRSH_CFG);
  1139. s->sbm_framecfg  = PKSEG1(s->sbm_base + R_MAC_FRAMECFG);
  1140. s->sbm_rxfilter  = PKSEG1(s->sbm_base + R_MAC_ADFILTER_CFG);
  1141. s->sbm_isr       = PKSEG1(s->sbm_base + R_MAC_STATUS);
  1142. s->sbm_imr       = PKSEG1(s->sbm_base + R_MAC_INT_MASK);
  1143. s->sbm_mdio      = PKSEG1(s->sbm_base + R_MAC_MDIO);
  1144. s->sbm_phys[0]   = 1;
  1145. s->sbm_phys[1]   = 0;
  1146. s->sbm_phy_oldbmsr = 0;
  1147. s->sbm_phy_oldanlpar = 0;
  1148. s->sbm_phy_oldk1stsr = 0;
  1149. s->sbm_phy_oldlinkstat = 0;
  1150. /*
  1151.  * Initialize the DMA channels.  Right now, only one per MAC is used
  1152.  * Note: Only do this _once_, as it allocates memory from the kernel!
  1153.  */
  1154. sbdma_initctx(&(s->sbm_txdma),s,0,DMA_TX,SBMAC_MAX_TXDESCR);
  1155. sbdma_initctx(&(s->sbm_rxdma),s,0,DMA_RX,SBMAC_MAX_RXDESCR);
  1156. /*
  1157.  * initial state is OFF
  1158.  */
  1159. s->sbm_state = sbmac_state_off;
  1160. /*
  1161.  * Initial speed is (XXX TEMP) 10MBit/s HDX no FC
  1162.  */
  1163. s->sbm_speed = sbmac_speed_10;
  1164. s->sbm_duplex = sbmac_duplex_half;
  1165. s->sbm_fc = sbmac_fc_disabled;
  1166. return 0;
  1167. }
  1168. static void sbdma_uninitctx(struct sbmacdma_s *d)
  1169. {
  1170. if (d->sbdma_dscrtable) {
  1171. KFREE(d->sbdma_dscrtable);
  1172. d->sbdma_dscrtable = NULL;
  1173. }
  1174. if (d->sbdma_ctxtable) {
  1175. KFREE(d->sbdma_ctxtable);
  1176. d->sbdma_ctxtable = NULL;
  1177. }
  1178. }
  1179. static void sbmac_uninitctx(struct sbmac_softc *sc)
  1180. {
  1181. sbdma_uninitctx(&(sc->sbm_txdma));
  1182. sbdma_uninitctx(&(sc->sbm_rxdma));
  1183. }
  1184. /**********************************************************************
  1185.  *  SBMAC_CHANNEL_START(s)
  1186.  *  
  1187.  *  Start packet processing on this MAC.
  1188.  *  
  1189.  *  Input parameters: 
  1190.  *      s - sbmac structure
  1191.  *      
  1192.  *  Return value:
  1193.  *      nothing
  1194.  ********************************************************************* */
  1195. static void sbmac_channel_start(struct sbmac_softc *s)
  1196. {
  1197. uint64_t reg;
  1198. sbmac_port_t port;
  1199. uint64_t cfg,fifo,framecfg;
  1200. int idx;
  1201. /*
  1202.  * Don't do this if running
  1203.  */
  1204. if (s->sbm_state == sbmac_state_on) return;
  1205. /*
  1206.  * Bring the controller out of reset, but leave it off.
  1207.  */
  1208. SBMAC_WRITECSR(s->sbm_macenable,0);
  1209. /*
  1210.  * Ignore all received packets
  1211.  */
  1212. SBMAC_WRITECSR(s->sbm_rxfilter,0);
  1213. /* 
  1214.  * Calculate values for various control registers.
  1215.  */
  1216. cfg = M_MAC_RETRY_EN |
  1217. M_MAC_TX_HOLD_SOP_EN | 
  1218. V_MAC_TX_PAUSE_CNT_16K |
  1219. M_MAC_AP_STAT_EN |
  1220. M_MAC_FAST_SYNC |
  1221. M_MAC_SS_EN |
  1222. 0;
  1223. /* 
  1224.  * Be sure that RD_THRSH+WR_THRSH <= 32
  1225.  * Use a larger RD_THRSH for gigabit
  1226.  */
  1227. fifo = V_MAC_TX_WR_THRSH(4) | /* Must be '4' or '8' */
  1228. ((s->sbm_speed == sbmac_speed_1000)
  1229.  ? V_MAC_TX_RD_THRSH(28) : V_MAC_TX_RD_THRSH(4)) |
  1230. V_MAC_TX_RL_THRSH(4) |
  1231. V_MAC_RX_PL_THRSH(4) |
  1232. V_MAC_RX_RD_THRSH(4) | /* Must be '4' */
  1233. V_MAC_RX_PL_THRSH(4) |
  1234. V_MAC_RX_RL_THRSH(8) |
  1235. 0;
  1236. framecfg = V_MAC_MIN_FRAMESZ_DEFAULT |
  1237. V_MAC_MAX_FRAMESZ_DEFAULT |
  1238. V_MAC_BACKOFF_SEL(1);
  1239. /*
  1240.  * Clear out the hash address map 
  1241.  */
  1242. port = PKSEG1(s->sbm_base + R_MAC_HASH_BASE);
  1243.         for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
  1244. SBMAC_WRITECSR(port,0);
  1245. port += sizeof(uint64_t);
  1246. }
  1247. /*
  1248.  * Clear out the exact-match table
  1249.  */
  1250. port = PKSEG1(s->sbm_base + R_MAC_ADDR_BASE);
  1251. for (idx = 0; idx < MAC_ADDR_COUNT; idx++) {
  1252. SBMAC_WRITECSR(port,0);
  1253. port += sizeof(uint64_t);
  1254. }
  1255. /*
  1256.  * Clear out the DMA Channel mapping table registers
  1257.  */
  1258. port = PKSEG1(s->sbm_base + R_MAC_CHUP0_BASE);
  1259. for (idx = 0; idx < MAC_CHMAP_COUNT; idx++) {
  1260. SBMAC_WRITECSR(port,0);
  1261. port += sizeof(uint64_t);
  1262. }
  1263. port = PKSEG1(s->sbm_base + R_MAC_CHLO0_BASE);
  1264. for (idx = 0; idx < MAC_CHMAP_COUNT; idx++) {
  1265. SBMAC_WRITECSR(port,0);
  1266. port += sizeof(uint64_t);
  1267. }
  1268. /*
  1269.  * Program the hardware address.  It goes into the hardware-address
  1270.  * register as well as the first filter register.
  1271.  */
  1272. reg = sbmac_addr2reg(s->sbm_hwaddr);
  1273. port = PKSEG1(s->sbm_base + R_MAC_ADDR_BASE);
  1274. SBMAC_WRITECSR(port,reg);
  1275. port = PKSEG1(s->sbm_base + R_MAC_ETHERNET_ADDR);
  1276. #ifdef CONFIG_SB1_PASS_1_WORKAROUNDS
  1277. /*
  1278.  * Pass1 SB1250s do not receive packets addressed to the
  1279.  * destination address in the R_MAC_ETHERNET_ADDR register.
  1280.  * Set the value to zero.
  1281.  */
  1282. SBMAC_WRITECSR(port,0);
  1283. #else
  1284. SBMAC_WRITECSR(port,reg);
  1285. #endif
  1286. /*
  1287.  * Set the receive filter for no packets, and write values
  1288.  * to the various config registers
  1289.  */
  1290. SBMAC_WRITECSR(s->sbm_rxfilter,0);
  1291. SBMAC_WRITECSR(s->sbm_imr,0);
  1292. SBMAC_WRITECSR(s->sbm_framecfg,framecfg);
  1293. SBMAC_WRITECSR(s->sbm_fifocfg,fifo);
  1294. SBMAC_WRITECSR(s->sbm_maccfg,cfg);
  1295. /*
  1296.  * Initialize DMA channels (rings should be ok now)
  1297.  */
  1298. sbdma_channel_start(&(s->sbm_rxdma), DMA_RX);
  1299. sbdma_channel_start(&(s->sbm_txdma), DMA_TX);
  1300. /*
  1301.  * Configure the speed, duplex, and flow control
  1302.  */
  1303. sbmac_set_speed(s,s->sbm_speed);
  1304. sbmac_set_duplex(s,s->sbm_duplex,s->sbm_fc);
  1305. /*
  1306.  * Fill the receive ring
  1307.  */
  1308. sbdma_fillring(&(s->sbm_rxdma));
  1309. /* 
  1310.  * Turn on the rest of the bits in the enable register
  1311.  */      
  1312. SBMAC_WRITECSR(s->sbm_macenable,
  1313.        M_MAC_RXDMA_EN0 |
  1314.        M_MAC_TXDMA_EN0 |
  1315.        M_MAC_RX_ENABLE |
  1316.        M_MAC_TX_ENABLE);
  1317. #ifdef CONFIG_SBMAC_COALESCE
  1318. /*
  1319.  * Accept any TX interrupt and EOP count/timer RX interrupts on ch 0
  1320.  */
  1321. SBMAC_WRITECSR(s->sbm_imr,
  1322.        (M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
  1323.        ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0));
  1324. #else
  1325. /*
  1326.  * Accept any kind of interrupt on TX and RX DMA channel 0
  1327.  */
  1328. SBMAC_WRITECSR(s->sbm_imr,
  1329.        (M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
  1330.        (M_MAC_INT_CHANNEL << S_MAC_RX_CH0));
  1331. #endif
  1332. /* 
  1333.  * Enable receiving unicasts and broadcasts 
  1334.  */
  1335. SBMAC_WRITECSR(s->sbm_rxfilter,M_MAC_UCAST_EN | M_MAC_BCAST_EN);
  1336. /*
  1337.  * we're running now. 
  1338.  */
  1339. s->sbm_state = sbmac_state_on;
  1340. /* 
  1341.  * Program multicast addresses 
  1342.  */
  1343. sbmac_setmulti(s);
  1344. /* 
  1345.  * If channel was in promiscuous mode before, turn that on 
  1346.  */
  1347. if (s->sbm_devflags & IFF_PROMISC) {
  1348. sbmac_promiscuous_mode(s,1);
  1349. }
  1350. }
  1351. /**********************************************************************
  1352.  *  SBMAC_CHANNEL_STOP(s)
  1353.  *  
  1354.  *  Stop packet processing on this MAC.
  1355.  *  
  1356.  *  Input parameters: 
  1357.  *      s - sbmac structure
  1358.  *      
  1359.  *  Return value:
  1360.  *      nothing
  1361.  ********************************************************************* */
  1362. static void sbmac_channel_stop(struct sbmac_softc *s)
  1363. {
  1364. /* don't do this if already stopped */
  1365. if (s->sbm_state == sbmac_state_off) return;
  1366. /* don't accept any packets, disable all interrupts */
  1367. SBMAC_WRITECSR(s->sbm_rxfilter,0);
  1368. SBMAC_WRITECSR(s->sbm_imr,0);
  1369. /* Turn off ticker */
  1370. /* XXX */
  1371. /* turn off receiver and transmitter */
  1372. SBMAC_WRITECSR(s->sbm_macenable,0);
  1373. /* We're stopped now. */
  1374. s->sbm_state = sbmac_state_off;
  1375. /*
  1376.  * Stop DMA channels (rings should be ok now)
  1377.  */
  1378. sbdma_channel_stop(&(s->sbm_rxdma));
  1379. sbdma_channel_stop(&(s->sbm_txdma));
  1380. /* Empty the receive and transmit rings */
  1381. sbdma_emptyring(&(s->sbm_rxdma));
  1382. sbdma_emptyring(&(s->sbm_txdma));
  1383. }
  1384. /**********************************************************************
  1385.  *  SBMAC_SET_CHANNEL_STATE(state)
  1386.  *  
  1387.  *  Set the channel's state ON or OFF
  1388.  *  
  1389.  *  Input parameters: 
  1390.  *      state - new state
  1391.  *      
  1392.  *  Return value:
  1393.  *      old state
  1394.  ********************************************************************* */
  1395. static sbmac_state_t sbmac_set_channel_state(struct sbmac_softc *sc,
  1396.      sbmac_state_t state)
  1397. {
  1398. sbmac_state_t oldstate = sc->sbm_state;
  1399. /*
  1400.  * If same as previous state, return
  1401.  */
  1402. if (state == oldstate) {
  1403. return oldstate;
  1404. }
  1405. /*
  1406.  * If new state is ON, turn channel on 
  1407.  */
  1408. if (state == sbmac_state_on) {
  1409. sbmac_channel_start(sc);
  1410. }
  1411. else {
  1412. sbmac_channel_stop(sc);
  1413. }
  1414. /*
  1415.  * Return previous state
  1416.  */
  1417. return oldstate;
  1418. }
  1419. /**********************************************************************
  1420.  *  SBMAC_PROMISCUOUS_MODE(sc,onoff)
  1421.  *  
  1422.  *  Turn on or off promiscuous mode
  1423.  *  
  1424.  *  Input parameters: 
  1425.  *      sc - softc
  1426.  *      onoff - 1 to turn on, 0 to turn off
  1427.  *      
  1428.  *  Return value:
  1429.  *      nothing
  1430.  ********************************************************************* */
  1431. static void sbmac_promiscuous_mode(struct sbmac_softc *sc,int onoff)
  1432. {
  1433. uint64_t reg;
  1434. if (sc->sbm_state != sbmac_state_on) return;
  1435. if (onoff) {
  1436. reg = SBMAC_READCSR(sc->sbm_rxfilter);
  1437. reg |= M_MAC_ALLPKT_EN;
  1438. SBMAC_WRITECSR(sc->sbm_rxfilter,reg);
  1439. }
  1440. else {
  1441. reg = SBMAC_READCSR(sc->sbm_rxfilter);
  1442. reg &= ~M_MAC_ALLPKT_EN;
  1443. SBMAC_WRITECSR(sc->sbm_rxfilter,reg);
  1444. }
  1445. }
  1446. /**********************************************************************
  1447.  *  SBMAC_SETIPHDR_OFFSET(sc,onoff)
  1448.  *  
  1449.  *  Set the iphdr offset as 15 assuming ethernet encapsulation
  1450.  *  
  1451.  *  Input parameters: 
  1452.  *      sc - softc
  1453.  *      
  1454.  *  Return value:
  1455.  *      nothing
  1456.  ********************************************************************* */
  1457. static void sbmac_set_iphdr_offset(struct sbmac_softc *sc)
  1458. {
  1459. uint64_t reg;
  1460. reg = SBMAC_READCSR(sc->sbm_rxfilter);
  1461. reg &= ~M_MAC_IPHDR_OFFSET;
  1462. /* Hard code the off set to 15 for now */
  1463. reg |= 15 << S_MAC_IPHDR_OFFSET;
  1464. SBMAC_WRITECSR(sc->sbm_rxfilter,reg);
  1465. /* read system identification to determine revision */
  1466. if (sb1250_pass >= K_SYS_REVISION_PASS2) {
  1467. printk(KERN_INFO "pass2 - enabling Rx rcv tcp checksumn");
  1468. sc->rx_hw_checksum = ENABLE;
  1469. } else {
  1470. sc->rx_hw_checksum = DISABLE;
  1471. }
  1472. }
  1473. #if 0
  1474. /**********************************************************************
  1475.  *  SBMAC_INIT_AND_START(sc)
  1476.  *  
  1477.  *  Stop the channel and restart it.  This is generally used
  1478.  *  when we have to do something to the channel that requires
  1479.  *  a swift kick.
  1480.  *  
  1481.  *  Input parameters: 
  1482.  *      sc - softc
  1483.  ********************************************************************* */
  1484. static void sbmac_init_and_start(struct sbmac_softc *sc)
  1485. {
  1486. unsigned long flags;
  1487. spin_lock_irqsave(&(sc->sbm_lock),flags);
  1488. sbmac_set_channel_state(sc,sbmac_state_on);
  1489. spin_unlock_irqrestore(&(sc->sbm_lock),flags);
  1490. }
  1491. #endif
  1492. /**********************************************************************
  1493.  *  SBMAC_ADDR2REG(ptr)
  1494.  *  
  1495.  *  Convert six bytes into the 64-bit register value that
  1496.  *  we typically write into the SBMAC's address/mcast registers
  1497.  *  
  1498.  *  Input parameters: 
  1499.  *      ptr - pointer to 6 bytes
  1500.  *      
  1501.  *  Return value:
  1502.  *      register value
  1503.  ********************************************************************* */
  1504. static uint64_t sbmac_addr2reg(unsigned char *ptr)
  1505. {
  1506. uint64_t reg = 0;
  1507. ptr += 6;
  1508. reg |= (uint64_t) *(--ptr); 
  1509. reg <<= 8;
  1510. reg |= (uint64_t) *(--ptr); 
  1511. reg <<= 8;
  1512. reg |= (uint64_t) *(--ptr); 
  1513. reg <<= 8;
  1514. reg |= (uint64_t) *(--ptr); 
  1515. reg <<= 8;
  1516. reg |= (uint64_t) *(--ptr); 
  1517. reg <<= 8;
  1518. reg |= (uint64_t) *(--ptr); 
  1519. return reg;
  1520. }
  1521. /**********************************************************************
  1522.  *  SBMAC_SET_SPEED(s,speed)
  1523.  *  
  1524.  *  Configure LAN speed for the specified MAC.
  1525.  *  Warning: must be called when MAC is off!
  1526.  *  
  1527.  *  Input parameters: 
  1528.  *      s - sbmac structure
  1529.  *      speed - speed to set MAC to (see sbmac_speed_t enum)
  1530.  *      
  1531.  *  Return value:
  1532.  *      1 if successful
  1533.  *      0 indicates invalid parameters
  1534.  ********************************************************************* */
  1535. static int sbmac_set_speed(struct sbmac_softc *s,sbmac_speed_t speed)
  1536. {
  1537. uint64_t cfg;
  1538. uint64_t framecfg;
  1539. /*
  1540.  * Save new current values
  1541.  */
  1542. s->sbm_speed = speed;
  1543. if (s->sbm_state == sbmac_state_on) return 0; /* save for next restart */
  1544. /*
  1545.  * Read current register values 
  1546.  */
  1547. cfg = SBMAC_READCSR(s->sbm_maccfg);
  1548. framecfg = SBMAC_READCSR(s->sbm_framecfg);
  1549. /*
  1550.  * Mask out the stuff we want to change
  1551.  */
  1552. cfg &= ~(M_MAC_BURST_EN | M_MAC_SPEED_SEL);
  1553. framecfg &= ~(M_MAC_IFG_RX | M_MAC_IFG_TX | M_MAC_IFG_THRSH |
  1554.       M_MAC_SLOT_SIZE);
  1555. /*
  1556.  * Now add in the new bits
  1557.  */
  1558. switch (speed) {
  1559. case sbmac_speed_10:
  1560. framecfg |= V_MAC_IFG_RX_10 |
  1561. V_MAC_IFG_TX_10 |
  1562. K_MAC_IFG_THRSH_10 |
  1563. V_MAC_SLOT_SIZE_10;
  1564. cfg |= V_MAC_SPEED_SEL_10MBPS;
  1565. break;
  1566. case sbmac_speed_100:
  1567. framecfg |= V_MAC_IFG_RX_100 |
  1568. V_MAC_IFG_TX_100 |
  1569. V_MAC_IFG_THRSH_100 |
  1570. V_MAC_SLOT_SIZE_100;
  1571. cfg |= V_MAC_SPEED_SEL_100MBPS ;
  1572. break;
  1573. case sbmac_speed_1000:
  1574. framecfg |= V_MAC_IFG_RX_1000 |
  1575. V_MAC_IFG_TX_1000 |
  1576. V_MAC_IFG_THRSH_1000 |
  1577. V_MAC_SLOT_SIZE_1000;
  1578. cfg |= V_MAC_SPEED_SEL_1000MBPS | M_MAC_BURST_EN;
  1579. break;
  1580. case sbmac_speed_auto: /* XXX not implemented */
  1581. /* fall through */
  1582. default:
  1583. return 0;
  1584. }
  1585. /*
  1586.  * Send the bits back to the hardware 
  1587.  */
  1588. SBMAC_WRITECSR(s->sbm_framecfg,framecfg);
  1589. SBMAC_WRITECSR(s->sbm_maccfg,cfg);
  1590. return 1;
  1591. }
  1592. /**********************************************************************
  1593.  *  SBMAC_SET_DUPLEX(s,duplex,fc)
  1594.  *  
  1595.  *  Set Ethernet duplex and flow control options for this MAC
  1596.  *  Warning: must be called when MAC is off!
  1597.  *  
  1598.  *  Input parameters: 
  1599.  *      s - sbmac structure
  1600.  *      duplex - duplex setting (see sbmac_duplex_t)
  1601.  *      fc - flow control setting (see sbmac_fc_t)
  1602.  *      
  1603.  *  Return value:
  1604.  *      1 if ok
  1605.  *      0 if an invalid parameter combination was specified
  1606.  ********************************************************************* */
  1607. static int sbmac_set_duplex(struct sbmac_softc *s,sbmac_duplex_t duplex,sbmac_fc_t fc)
  1608. {
  1609. uint64_t cfg;
  1610. /*
  1611.  * Save new current values
  1612.  */
  1613. s->sbm_duplex = duplex;
  1614. s->sbm_fc = fc;
  1615. if (s->sbm_state == sbmac_state_on) return 0; /* save for next restart */
  1616. /*
  1617.  * Read current register values 
  1618.  */
  1619. cfg = SBMAC_READCSR(s->sbm_maccfg);
  1620. /*
  1621.  * Mask off the stuff we're about to change
  1622.  */
  1623. cfg &= ~(M_MAC_FC_SEL | M_MAC_FC_CMD | M_MAC_HDX_EN);
  1624. switch (duplex) {
  1625. case sbmac_duplex_half:
  1626. switch (fc) {
  1627. case sbmac_fc_disabled:
  1628. cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_DISABLED;
  1629. break;
  1630. case sbmac_fc_collision:
  1631. cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENABLED;
  1632. break;
  1633. case sbmac_fc_carrier:
  1634. cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENAB_FALSECARR;
  1635. break;
  1636. case sbmac_fc_auto: /* XXX not implemented */
  1637. /* fall through */    
  1638. case sbmac_fc_frame: /* not valid in half duplex */
  1639. default: /* invalid selection */
  1640. return 0;
  1641. }
  1642. break;
  1643. case sbmac_duplex_full:
  1644. switch (fc) {
  1645. case sbmac_fc_disabled:
  1646. cfg |= V_MAC_FC_CMD_DISABLED;
  1647. break;
  1648. case sbmac_fc_frame:
  1649. cfg |= V_MAC_FC_CMD_ENABLED;
  1650. break;
  1651. case sbmac_fc_collision: /* not valid in full duplex */
  1652. case sbmac_fc_carrier: /* not valid in full duplex */
  1653. case sbmac_fc_auto: /* XXX not implemented */
  1654. /* fall through */    
  1655. default:
  1656. return 0;
  1657. }
  1658. break;
  1659. case sbmac_duplex_auto:
  1660. /* XXX not implemented */
  1661. break;
  1662. }
  1663. /*
  1664.  * Send the bits back to the hardware 
  1665.  */
  1666. SBMAC_WRITECSR(s->sbm_maccfg,cfg);
  1667. return 1;
  1668. }
  1669. /**********************************************************************
  1670.  *  SBMAC_INTR()
  1671.  *  
  1672.  *  Interrupt handler for MAC interrupts
  1673.  *  
  1674.  *  Input parameters: 
  1675.  *      MAC structure
  1676.  *      
  1677.  *  Return value:
  1678.  *      nothing
  1679.  ********************************************************************* */
  1680. static void sbmac_intr(int irq,void *dev_instance,struct pt_regs *rgs)
  1681. {
  1682. struct net_device *dev = (struct net_device *) dev_instance;
  1683. struct sbmac_softc *sc = (struct sbmac_softc *) (dev->priv);
  1684. uint64_t isr;
  1685. for (;;) {
  1686. /*
  1687.  * Read the ISR (this clears the bits in the real
  1688.  * register, except for counter addr)
  1689.  */
  1690. isr = SBMAC_READCSR(sc->sbm_isr) & ~M_MAC_COUNTER_ADDR;
  1691. if (isr == 0) break;
  1692. /*
  1693.  * Transmits on channel 0
  1694.  */
  1695. if (isr & (M_MAC_INT_CHANNEL << S_MAC_TX_CH0)) {
  1696. sbdma_tx_process(sc,&(sc->sbm_txdma));
  1697. }
  1698. /*
  1699.  * Receives on channel 0
  1700.  */
  1701. /*
  1702.  * It's important to test all the bits (or at least the
  1703.  * EOP_SEEN bit) when deciding to do the RX process
  1704.  * particularly when coalescing, to make sure we
  1705.  * take care of the following:
  1706.  *
  1707.  * If you have some packets waiting (have been received
  1708.  * but no interrupt) and get a TX interrupt before
  1709.  * the RX timer or counter expires, reading the ISR
  1710.  * above will clear the timer and counter, and you
  1711.  * won't get another interrupt until a packet shows
  1712.  * up to start the timer again.  Testing
  1713.  * EOP_SEEN here takes care of this case.
  1714.  * (EOP_SEEN is part of M_MAC_INT_CHANNEL << S_MAC_RX_CH0)
  1715.  */
  1716.  
  1717. if (isr & (M_MAC_INT_CHANNEL << S_MAC_RX_CH0)) {
  1718. sbdma_rx_process(sc,&(sc->sbm_rxdma));
  1719. }
  1720. }
  1721. }
  1722. /**********************************************************************
  1723.  *  SBMAC_START_TX(skb,dev)
  1724.  *  
  1725.  *  Start output on the specified interface.  Basically, we 
  1726.  *  queue as many buffers as we can until the ring fills up, or
  1727.  *  we run off the end of the queue, whichever comes first.
  1728.  *  
  1729.  *  Input parameters: 
  1730.  *      
  1731.  *      
  1732.  *  Return value:
  1733.  *      nothing
  1734.  ********************************************************************* */
  1735. static int sbmac_start_tx(struct sk_buff *skb, struct net_device *dev)
  1736. {
  1737. struct sbmac_softc *sc = (struct sbmac_softc *)dev->priv;
  1738. /* lock eth irq */
  1739. spin_lock_irq (&sc->sbm_lock);
  1740. /*
  1741.  * Put the buffer on the transmit ring.  If we 
  1742.  * don't have room, stop the queue.
  1743.  */
  1744. if (sbdma_add_txbuffer(&(sc->sbm_txdma),skb)) {
  1745. /* XXX save skb that we could not send */
  1746. netif_stop_queue(dev);
  1747. spin_unlock_irq(&sc->sbm_lock);
  1748. return 1;
  1749. }
  1750. dev->trans_start = jiffies;
  1751. spin_unlock_irq (&sc->sbm_lock);
  1752. return 0;
  1753. }
  1754. /**********************************************************************
  1755.  *  SBMAC_SETMULTI(sc)
  1756.  *  
  1757.  *  Reprogram the multicast table into the hardware, given
  1758.  *  the list of multicasts associated with the interface
  1759.  *  structure.
  1760.  *  
  1761.  *  Input parameters: 
  1762.  *      sc - softc
  1763.  *      
  1764.  *  Return value:
  1765.  *      nothing
  1766.  ********************************************************************* */
  1767. static void sbmac_setmulti(struct sbmac_softc *sc)
  1768. {
  1769. uint64_t reg;
  1770. sbmac_port_t port;
  1771. int idx;
  1772. struct dev_mc_list *mclist;
  1773. struct net_device *dev = sc->sbm_dev;
  1774. /* 
  1775.  * Clear out entire multicast table.  We do this by nuking
  1776.  * the entire hash table and all the direct matches except
  1777.  * the first one, which is used for our station address 
  1778.  */
  1779. for (idx = 1; idx < MAC_ADDR_COUNT; idx++) {
  1780. port = PKSEG1(sc->sbm_base + R_MAC_ADDR_BASE+(idx*sizeof(uint64_t)));
  1781. SBMAC_WRITECSR(port,0);
  1782. }
  1783. for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
  1784. port = PKSEG1(sc->sbm_base + R_MAC_HASH_BASE+(idx*sizeof(uint64_t)));
  1785. SBMAC_WRITECSR(port,0);
  1786. }
  1787. /*
  1788.  * Clear the filter to say we don't want any multicasts.
  1789.  */
  1790. reg = SBMAC_READCSR(sc->sbm_rxfilter);
  1791. reg &= ~(M_MAC_MCAST_INV | M_MAC_MCAST_EN);
  1792. SBMAC_WRITECSR(sc->sbm_rxfilter,reg);
  1793. if (dev->flags & IFF_ALLMULTI) {
  1794. /* 
  1795.  * Enable ALL multicasts.  Do this by inverting the 
  1796.  * multicast enable bit. 
  1797.  */
  1798. reg = SBMAC_READCSR(sc->sbm_rxfilter);
  1799. reg |= (M_MAC_MCAST_INV | M_MAC_MCAST_EN);
  1800. SBMAC_WRITECSR(sc->sbm_rxfilter,reg);
  1801. return;
  1802. }
  1803. /* 
  1804.  * Progam new multicast entries.  For now, only use the
  1805.  * perfect filter.  In the future we'll need to use the
  1806.  * hash filter if the perfect filter overflows
  1807.  */
  1808. /* XXX only using perfect filter for now, need to use hash
  1809.  * XXX if the table overflows */
  1810. idx = 1; /* skip station address */
  1811. mclist = dev->mc_list;
  1812. while (mclist && (idx < MAC_ADDR_COUNT)) {
  1813. reg = sbmac_addr2reg(mclist->dmi_addr);
  1814. port = PKSEG1(sc->sbm_base + 
  1815.       R_MAC_ADDR_BASE+(idx*sizeof(uint64_t)));
  1816. SBMAC_WRITECSR(port,reg);
  1817. idx++;
  1818. mclist = mclist->next;
  1819. }
  1820. /*
  1821.  * Enable the "accept multicast bits" if we programmed at least one
  1822.  * multicast. 
  1823.  */
  1824. if (idx > 1) {
  1825. reg = SBMAC_READCSR(sc->sbm_rxfilter);
  1826. reg |= M_MAC_MCAST_EN;
  1827. SBMAC_WRITECSR(sc->sbm_rxfilter,reg);
  1828. }
  1829. }
  1830. #if defined(SBMAC_ETH0_HWADDR) || defined(SBMAC_ETH1_HWADDR) || defined(SBMAC_ETH2_HWADDR)
  1831. /**********************************************************************
  1832.  *  SBMAC_PARSE_XDIGIT(str)
  1833.  *  
  1834.  *  Parse a hex digit, returning its value
  1835.  *  
  1836.  *  Input parameters: 
  1837.  *      str - character
  1838.  *      
  1839.  *  Return value:
  1840.  *      hex value, or -1 if invalid
  1841.  ********************************************************************* */
  1842. static int sbmac_parse_xdigit(char str)
  1843. {
  1844. int digit;
  1845. if ((str >= '0') && (str <= '9')) digit = str - '0';
  1846. else if ((str >= 'a') && (str <= 'f')) digit = str - 'a' + 10;
  1847. else if ((str >= 'A') && (str <= 'F')) digit = str - 'A' + 10;
  1848. else return -1;
  1849. return digit;
  1850. }
  1851. /**********************************************************************
  1852.  *  SBMAC_PARSE_HWADDR(str,hwaddr)
  1853.  *  
  1854.  *  Convert a string in the form xx:xx:xx:xx:xx:xx into a 6-byte
  1855.  *  Ethernet address.
  1856.  *  
  1857.  *  Input parameters: 
  1858.  *      str - string
  1859.  *      hwaddr - pointer to hardware address
  1860.  *      
  1861.  *  Return value:
  1862.  *      0 if ok, else -1
  1863.  ********************************************************************* */
  1864. static int sbmac_parse_hwaddr(char *str,u_char *hwaddr)
  1865. {
  1866. int digit1,digit2;
  1867. int idx = 6;
  1868. while (*str && (idx > 0)) {
  1869. digit1 = sbmac_parse_xdigit(*str);
  1870. if (digit1 < 0) return -1;
  1871. str++;
  1872. if (!*str) return -1;
  1873. if ((*str == ':') || (*str == '-')) {
  1874. digit2 = digit1;
  1875. digit1 = 0;
  1876. }
  1877. else {
  1878. digit2 = sbmac_parse_xdigit(*str);
  1879. if (digit2 < 0) return -1;
  1880. str++;
  1881. }
  1882. *hwaddr++ = (digit1 << 4) | digit2;
  1883. idx--;
  1884. if (*str == '-') str++;
  1885. if (*str == ':') str++;
  1886. }
  1887. return 0;
  1888. }
  1889. #endif
  1890. static int sb1250_change_mtu(struct net_device *_dev, int new_mtu)
  1891. {
  1892.         if (new_mtu >  ENET_PACKET_SIZE)
  1893.                 return -EINVAL;
  1894.         _dev->mtu = new_mtu;
  1895. printk(KERN_INFO "changing the mtu to %dn", new_mtu);
  1896.         return 0;
  1897. }
  1898. /**********************************************************************
  1899.  *  SBMAC_INIT(dev)
  1900.  *  
  1901.  *  Attach routine - init hardware and hook ourselves into linux
  1902.  *  
  1903.  *  Input parameters: 
  1904.  *      dev - net_device structure
  1905.  *      
  1906.  *  Return value:
  1907.  *      status
  1908.  ********************************************************************* */
  1909. static int sbmac_init(struct net_device *dev)
  1910. {
  1911. struct sbmac_softc *sc;
  1912. u_char *eaddr;
  1913. uint64_t ea_reg;
  1914. int idx;
  1915. sc = (struct sbmac_softc *)dev->priv;
  1916. /* Determine controller base address */
  1917. sc->sbm_base = (sbmac_port_t) dev->base_addr;
  1918. sc->sbm_dev = dev;
  1919. eaddr = sc->sbm_hwaddr;
  1920. /* 
  1921.  * Read the ethernet address.  The firwmare left this programmed
  1922.  * for us in the ethernet address register for each mac.
  1923.  */
  1924. ea_reg = SBMAC_READCSR(PKSEG1(sc->sbm_base + R_MAC_ETHERNET_ADDR));
  1925. SBMAC_WRITECSR(PKSEG1(sc->sbm_base + R_MAC_ETHERNET_ADDR), 0);
  1926. for (idx = 0; idx < 6; idx++) {
  1927. eaddr[idx] = (uint8_t) (ea_reg & 0xFF);
  1928. ea_reg >>= 8;
  1929. }
  1930. for (idx = 0; idx < 6; idx++) {
  1931. dev->dev_addr[idx] = eaddr[idx];
  1932. }
  1933. /*
  1934.  * Init packet size 
  1935.  */
  1936. sc->sbm_buffersize = ENET_PACKET_SIZE + CACHELINESIZE*2 + ETHER_ALIGN;
  1937. /* 
  1938.  * Initialize context (get pointers to registers and stuff), then
  1939.  * allocate the memory for the descriptor tables.
  1940.  */
  1941. sbmac_initctx(sc);
  1942. /*
  1943.  * Display Ethernet address (this is called during the config process
  1944.  * so we need to finish off the config message that was being displayed)
  1945.  */
  1946. printk(KERN_INFO
  1947.        "%s: SB1250 Ethernet at 0x%08lX, address: %02X-%02X-%02X-%02X-%02X-%02Xn", 
  1948.        dev->name,
  1949.        (unsigned long) sc->sbm_base,
  1950.        eaddr[0],eaddr[1],eaddr[2],eaddr[3],eaddr[4],eaddr[5]);
  1951. /*
  1952.  * Set up Linux device callins
  1953.  */
  1954. spin_lock_init(&(sc->sbm_lock));
  1955. ether_setup(dev);
  1956. dev->open               = sbmac_open;
  1957. dev->hard_start_xmit    = sbmac_start_tx;
  1958. dev->stop               = sbmac_close;
  1959. dev->get_stats          = sbmac_get_stats;
  1960. dev->set_multicast_list = sbmac_set_rx_mode;
  1961. dev->do_ioctl           = sbmac_mii_ioctl;
  1962. dev->tx_timeout         = sbmac_tx_timeout;
  1963. dev->watchdog_timeo     = TX_TIMEOUT;
  1964. dev->change_mtu         = sb1250_change_mtu;
  1965. if (sb1250_pass >= K_SYS_REVISION_PASS3) {
  1966. /* In pass3 we do dumb checksum in TX */
  1967. dev->features |= NETIF_F_IP_CSUM;
  1968. }
  1969.         /* This is needed for PASS2 for Rx H/W checksum feature */
  1970. sbmac_set_iphdr_offset( sc);
  1971. return 0;
  1972. }
  1973. static int sbmac_open(struct net_device *dev)
  1974. {
  1975. struct sbmac_softc *sc = (struct sbmac_softc *)dev->priv;
  1976. MOD_INC_USE_COUNT;
  1977. if (debug > 1) {
  1978. printk(KERN_DEBUG "%s: sbmac_open() irq %d.n", dev->name, dev->irq);
  1979. }
  1980. /* 
  1981.  * map/route interrupt 
  1982.  */
  1983. if (request_irq(dev->irq, &sbmac_intr, SA_SHIRQ, dev->name, dev)) {
  1984. MOD_DEC_USE_COUNT;
  1985. return -EBUSY;
  1986. }
  1987. /*
  1988.  * Configure default speed 
  1989.  */
  1990. sbmac_mii_poll(sc,1);
  1991. /*
  1992.  * Turn on the channel
  1993.  */
  1994. sbmac_set_channel_state(sc,sbmac_state_on);
  1995. /*
  1996.  * XXX Station address is in dev->dev_addr
  1997.  */
  1998. if (dev->if_port == 0)
  1999. dev->if_port = 0; 
  2000. netif_start_queue(dev);
  2001. sbmac_set_rx_mode(dev);
  2002. /* Set the timer to check for link beat. */
  2003. init_timer(&sc->sbm_timer);
  2004. sc->sbm_timer.expires = jiffies + 2;
  2005. sc->sbm_timer.data = (unsigned long)dev;
  2006. sc->sbm_timer.function = &sbmac_timer;
  2007. add_timer(&sc->sbm_timer);
  2008. return 0;
  2009. }
  2010. static int sbmac_mii_poll(struct sbmac_softc *s,int noisy)
  2011. {
  2012.     int bmsr,bmcr,k1stsr,anlpar;
  2013.     int chg;
  2014.     char buffer[100];
  2015.     char *p = buffer;
  2016.     /* Read the mode status and mode control registers. */
  2017.     bmsr = sbmac_mii_read(s,s->sbm_phys[0],MII_BMSR);
  2018.     bmcr = sbmac_mii_read(s,s->sbm_phys[0],MII_BMCR);
  2019.     /* get the link partner status */
  2020.     anlpar = sbmac_mii_read(s,s->sbm_phys[0],MII_ANLPAR);
  2021.     /* if supported, read the 1000baseT register */
  2022.     if (bmsr & BMSR_1000BT_XSR) {
  2023. k1stsr = sbmac_mii_read(s,s->sbm_phys[0],MII_K1STSR);
  2024. }
  2025.     else {
  2026. k1stsr = 0;
  2027. }
  2028.     chg = 0;
  2029.     if ((bmsr & BMSR_LINKSTAT) == 0) {
  2030. /*
  2031.  * If link status is down, clear out old info so that when
  2032.  * it comes back up it will force us to reconfigure speed
  2033.  */
  2034. s->sbm_phy_oldbmsr = 0;
  2035. s->sbm_phy_oldanlpar = 0;
  2036. s->sbm_phy_oldk1stsr = 0;
  2037. return 0;
  2038. }
  2039.     if ((s->sbm_phy_oldbmsr != bmsr) ||
  2040. (s->sbm_phy_oldanlpar != anlpar) ||
  2041. (s->sbm_phy_oldk1stsr != k1stsr)) {
  2042. if (debug > 1) {
  2043.     printk(KERN_DEBUG "%s: bmsr:%x/%x anlpar:%x/%x  k1stsr:%x/%xn",
  2044.        s->sbm_dev->name,
  2045.        s->sbm_phy_oldbmsr,bmsr,
  2046.        s->sbm_phy_oldanlpar,anlpar,
  2047.        s->sbm_phy_oldk1stsr,k1stsr);
  2048.     }
  2049. s->sbm_phy_oldbmsr = bmsr;
  2050. s->sbm_phy_oldanlpar = anlpar;
  2051. s->sbm_phy_oldk1stsr = k1stsr;
  2052. chg = 1;
  2053. }
  2054.     if (chg == 0) return 0;
  2055.     p += sprintf(p,"Link speed: ");
  2056.     if (k1stsr & K1STSR_LP1KFD) {
  2057. s->sbm_speed = sbmac_speed_1000;
  2058. s->sbm_duplex = sbmac_duplex_full;
  2059. s->sbm_fc = sbmac_fc_frame;
  2060. p += sprintf(p,"1000BaseT FDX");
  2061. }
  2062.     else if (k1stsr & K1STSR_LP1KHD) {
  2063. s->sbm_speed = sbmac_speed_1000;
  2064. s->sbm_duplex = sbmac_duplex_half;
  2065. s->sbm_fc = sbmac_fc_disabled;
  2066. p += sprintf(p,"1000BaseT HDX");
  2067. }
  2068.     else if (anlpar & ANLPAR_TXFD) {
  2069. s->sbm_speed = sbmac_speed_100;
  2070. s->sbm_duplex = sbmac_duplex_full;
  2071. s->sbm_fc = (anlpar & ANLPAR_PAUSE) ? sbmac_fc_frame : sbmac_fc_disabled;
  2072. p += sprintf(p,"100BaseT FDX");
  2073. }
  2074.     else if (anlpar & ANLPAR_TXHD) {
  2075. s->sbm_speed = sbmac_speed_100;
  2076. s->sbm_duplex = sbmac_duplex_half;
  2077. s->sbm_fc = sbmac_fc_disabled;
  2078. p += sprintf(p,"100BaseT HDX");
  2079. }
  2080.     else if (anlpar & ANLPAR_10FD) {
  2081. s->sbm_speed = sbmac_speed_10;
  2082. s->sbm_duplex = sbmac_duplex_full;
  2083. s->sbm_fc = sbmac_fc_frame;
  2084. p += sprintf(p,"10BaseT FDX");
  2085. }
  2086.     else if (anlpar & ANLPAR_10HD) {
  2087. s->sbm_speed = sbmac_speed_10;
  2088. s->sbm_duplex = sbmac_duplex_half;
  2089. s->sbm_fc = sbmac_fc_collision;
  2090. p += sprintf(p,"10BaseT HDX");
  2091. }
  2092.     else {
  2093. p += sprintf(p,"Unknown");
  2094. }
  2095. #ifdef CONFIG_NET_SB1250_MAC_QUIET
  2096.     noisy = 0;
  2097. #endif
  2098.     if (noisy) {
  2099.     printk(KERN_INFO "%s: %sn",s->sbm_dev->name,buffer);
  2100.     }
  2101.     return 1;
  2102. }
  2103. static void sbmac_timer(unsigned long data)
  2104. {
  2105. struct net_device *dev = (struct net_device *)data;
  2106. struct sbmac_softc *sc = (struct sbmac_softc *)dev->priv;
  2107. int next_tick = HZ;
  2108. int mii_status;
  2109. spin_lock_irq (&sc->sbm_lock);
  2110. /* make IFF_RUNNING follow the MII status bit "Link established" */
  2111. mii_status = sbmac_mii_read(sc, sc->sbm_phys[0], MII_BMSR);
  2112. if ( (mii_status & BMSR_LINKSTAT) != (sc->sbm_phy_oldlinkstat) ) {
  2113.              sc->sbm_phy_oldlinkstat = mii_status & BMSR_LINKSTAT;
  2114. if (mii_status & BMSR_LINKSTAT) {
  2115. netif_carrier_on(dev);
  2116. }
  2117. else {
  2118. netif_carrier_off(dev);
  2119. }
  2120. }
  2121. /*
  2122.  * Poll the PHY to see what speed we should be running at
  2123.  */
  2124. if (sbmac_mii_poll(sc,1)) {
  2125.     if (sc->sbm_state != sbmac_state_off) {
  2126. /*
  2127.  * something changed, restart the channel
  2128.  */
  2129. if (debug > 1) {
  2130.     printk("%s: restarting channel because speed changedn",
  2131.    sc->sbm_dev->name);
  2132.     }
  2133. sbmac_channel_stop(sc);
  2134. sbmac_channel_start(sc);
  2135. }
  2136.     }
  2137. spin_unlock_irq (&sc->sbm_lock);
  2138. sc->sbm_timer.expires = jiffies + next_tick;
  2139. add_timer(&sc->sbm_timer);
  2140. }
  2141. static void sbmac_tx_timeout (struct net_device *dev)
  2142. {
  2143. struct sbmac_softc *sc = (struct sbmac_softc *) dev->priv;
  2144. spin_lock_irq (&sc->sbm_lock);
  2145. dev->trans_start = jiffies;
  2146. sc->sbm_stats.tx_errors++;
  2147. spin_unlock_irq (&sc->sbm_lock);
  2148. printk (KERN_WARNING "%s: Transmit timed outn",dev->name);
  2149. }
  2150. static struct net_device_stats *sbmac_get_stats(struct net_device *dev)
  2151. {
  2152. struct sbmac_softc *sc = (struct sbmac_softc *)dev->priv;
  2153. unsigned long flags;
  2154. spin_lock_irqsave(&sc->sbm_lock, flags);
  2155. /* XXX update other stats here */
  2156. spin_unlock_irqrestore(&sc->sbm_lock, flags);
  2157. return &sc->sbm_stats;
  2158. }
  2159. static void sbmac_set_rx_mode(struct net_device *dev)
  2160. {
  2161. unsigned long flags;
  2162. int msg_flag = 0;
  2163. struct sbmac_softc *sc = (struct sbmac_softc *)dev->priv;
  2164. spin_lock_irqsave(&sc->sbm_lock, flags);
  2165. if ((dev->flags ^ sc->sbm_devflags) & IFF_PROMISC) {
  2166. /*
  2167.  * Promiscuous changed.
  2168.  */
  2169. if (dev->flags & IFF_PROMISC) {
  2170. /* Unconditionally log net taps. */
  2171. msg_flag = 1;
  2172. sbmac_promiscuous_mode(sc,1);
  2173. }
  2174. else {
  2175. msg_flag = 2;
  2176. sbmac_promiscuous_mode(sc,0);
  2177. }
  2178. }
  2179. spin_unlock_irqrestore(&sc->sbm_lock, flags);
  2180. if (msg_flag) {
  2181. printk(KERN_NOTICE "%s: Promiscuous mode %sabled.n", dev->name,(msg_flag==1)?"en":"dis");
  2182. }
  2183. /*
  2184.  * Program the multicasts.  Do this every time.
  2185.  */
  2186. sbmac_setmulti(sc);
  2187. }
  2188. static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  2189. {
  2190. struct sbmac_softc *sc = (struct sbmac_softc *)dev->priv;
  2191. u16 *data = (u16 *)&rq->ifr_data;
  2192. unsigned long flags;
  2193. int retval;
  2194. spin_lock_irqsave(&sc->sbm_lock, flags);
  2195. retval = 0;
  2196. switch(cmd) {
  2197. case SIOCDEVPRIVATE: /* Get the address of the PHY in use. */
  2198. data[0] = sc->sbm_phys[0] & 0x1f;
  2199. /* Fall Through */
  2200. case SIOCDEVPRIVATE+1: /* Read the specified MII register. */
  2201. data[3] = sbmac_mii_read(sc, data[0] & 0x1f, data[1] & 0x1f);
  2202. break;
  2203. case SIOCDEVPRIVATE+2: /* Write the specified MII register */
  2204. if (!capable(CAP_NET_ADMIN)) {
  2205. retval = -EPERM;
  2206. break;
  2207. }
  2208. if (debug > 1) {
  2209.     printk(KERN_DEBUG "%s: sbmac_mii_ioctl: write %02X %02X %02Xn",dev->name,
  2210.        data[0],data[1],data[2]);
  2211.     }
  2212. sbmac_mii_write(sc, data[0] & 0x1f, data[1] & 0x1f, data[2]);
  2213. break;
  2214. default:
  2215. retval = -EOPNOTSUPP;
  2216. }
  2217. spin_unlock_irqrestore(&sc->sbm_lock, flags);
  2218. return retval;
  2219. }
  2220. static int sbmac_close(struct net_device *dev)
  2221. {
  2222. struct sbmac_softc *sc = (struct sbmac_softc *)dev->priv;
  2223. unsigned long flags;
  2224. sbmac_set_channel_state(sc,sbmac_state_off);
  2225. del_timer_sync(&sc->sbm_timer);
  2226. spin_lock_irqsave(&sc->sbm_lock, flags);
  2227. netif_stop_queue(dev);
  2228. if (debug > 1) {
  2229. printk(KERN_DEBUG "%s: Shutting down ethercardn",dev->name);
  2230. }
  2231. spin_unlock_irqrestore(&sc->sbm_lock, flags);
  2232. /* Make sure there is no irq-handler running on a different CPU. */
  2233. synchronize_irq();
  2234. free_irq(dev->irq, dev);
  2235. sbdma_emptyring(&(sc->sbm_txdma));
  2236. sbdma_emptyring(&(sc->sbm_rxdma));
  2237. MOD_DEC_USE_COUNT;
  2238. return 0;
  2239. }
  2240. #if defined(SBMAC_ETH0_HWADDR) || defined(SBMAC_ETH1_HWADDR) || defined(SBMAC_ETH2_HWADDR)
  2241. static void
  2242. sbmac_setup_hwaddr(int chan,char *addr)
  2243. {
  2244. uint8_t eaddr[6];
  2245. uint64_t val;
  2246. sbmac_port_t port;
  2247. port = A_MAC_CHANNEL_BASE(chan);
  2248. sbmac_parse_hwaddr(addr,eaddr);
  2249. val = sbmac_addr2reg(eaddr);
  2250. SBMAC_WRITECSR(PKSEG1(port+R_MAC_ETHERNET_ADDR),val);
  2251. val = SBMAC_READCSR(PKSEG1(port+R_MAC_ETHERNET_ADDR));
  2252. }
  2253. #endif
  2254. static struct net_device *dev_sbmac[MAX_UNITS] = {0,0,0};
  2255. static int __init
  2256. sbmac_init_module(void)
  2257. {
  2258. int idx;
  2259. int macidx = 0;
  2260. struct net_device *dev;
  2261. sbmac_port_t port;
  2262. int chip_max_units;
  2263. /*
  2264.  * For bringup when not using the firmware, we can pre-fill
  2265.  * the MAC addresses using the environment variables
  2266.  * specified in this file (or maybe from the config file?)
  2267.  */
  2268. #ifdef SBMAC_ETH0_HWADDR
  2269. sbmac_setup_hwaddr(0,SBMAC_ETH0_HWADDR);
  2270. #endif
  2271. #ifdef SBMAC_ETH1_HWADDR
  2272. sbmac_setup_hwaddr(1,SBMAC_ETH1_HWADDR);
  2273. #endif
  2274. #ifdef SBMAC_ETH2_HWADDR
  2275. sbmac_setup_hwaddr(2,SBMAC_ETH2_HWADDR);
  2276. #endif
  2277. /*
  2278.  * Walk through the Ethernet controllers and find
  2279.  * those who have their MAC addresses set.
  2280.  */
  2281. chip_revision = SBMAC_READCSR(PKSEG1(A_SCD_SYSTEM_REVISION));
  2282. switch ((int)G_SYS_PART(chip_revision)) {
  2283. case 0x1150:
  2284. case 0x1250:
  2285. chip_max_units = 3;
  2286. break;
  2287. case 0x1120:
  2288. case 0x1125:
  2289. case 0x1126:
  2290. chip_max_units = 2;
  2291. break;
  2292. default:
  2293. chip_max_units = 0;
  2294. break;
  2295. }
  2296. if (chip_max_units > MAX_UNITS)
  2297. chip_max_units = MAX_UNITS;
  2298. for (idx = 0; idx < chip_max_units; idx++) {
  2299.         /*
  2300.          * This is the base address of the MAC.
  2301.  */
  2302.         port = A_MAC_CHANNEL_BASE(idx);
  2303. /*
  2304.  * The R_MAC_ETHERNET_ADDR register will be set to some nonzero
  2305.  * value for us by the firmware if we're going to use this MAC.
  2306.  * If we find a zero, skip this MAC.
  2307.  */
  2308. sbmac_orig_hwaddr[idx] = SBMAC_READCSR(PKSEG1(port+R_MAC_ETHERNET_ADDR));
  2309. if (sbmac_orig_hwaddr[idx] == 0) {
  2310.     printk( KERN_DEBUG "sbmac: not configuring MAC at %xn",(uint32_t)port);
  2311.     continue;
  2312. }
  2313. /*
  2314.  * Okay, cool.  Initialize this MAC.
  2315.  */
  2316. dev = init_etherdev(NULL,sizeof(struct sbmac_softc));
  2317. if (!dev) 
  2318.   return -ENOMEM; /* return ENOMEM */
  2319. dev->irq = K_INT_MAC_0 + idx;
  2320. dev->base_addr = port;
  2321. dev->mem_end = 0;
  2322. /*dev->init = sbmac_init;*/
  2323. sbmac_init(dev);
  2324. dev_sbmac[macidx] = dev;
  2325. macidx++;
  2326. }
  2327. /*
  2328.  * Should we care, 'macidx' is the total number of enabled MACs.
  2329.  */
  2330. return 0;
  2331. }
  2332. static void __exit
  2333. sbmac_cleanup_module(void)
  2334. {
  2335. int idx;
  2336. struct net_device *dev;
  2337. sbmac_port_t port;
  2338. for (idx = 0; idx < MAX_UNITS; idx++) {
  2339. dev = dev_sbmac[idx];
  2340. if (dev == NULL) continue;
  2341. if (dev->priv != NULL) {
  2342. struct sbmac_softc *sc = (struct sbmac_softc *) dev->priv;
  2343. unregister_netdev(dev);
  2344. sbmac_uninitctx(sc);
  2345. }
  2346.         port = A_MAC_CHANNEL_BASE(idx);
  2347. SBMAC_WRITECSR(PKSEG1(port+R_MAC_ETHERNET_ADDR), sbmac_orig_hwaddr[idx] );
  2348. KFREE(dev);
  2349. dev_sbmac[idx] = NULL;
  2350. }
  2351. }
  2352. module_init(sbmac_init_module);
  2353. module_exit(sbmac_cleanup_module);