/kern_oII/drivers/net/sb1250-mac.c

http://omnia2droid.googlecode.com/ · C · 2955 lines · 1516 code · 564 blank · 875 comment · 197 complexity · 0e286b3200a43435037b2dfcfa474133 MD5 · raw file

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