/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

Large files are truncated click here to view the full 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)