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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * Copyright 2000, 2001 MontaVista Software Inc.
  3.  * Author: MontaVista Software, Inc.
  4.  *          stevel@mvista.com or source@mvista.com
  5.  *
  6.  * ########################################################################
  7.  *
  8.  *  This program is free software; you can distribute it and/or modify it
  9.  *  under the terms of the GNU General Public License (Version 2) as
  10.  *  published by the Free Software Foundation.
  11.  *
  12.  *  This program is distributed in the hope it will be useful, but WITHOUT
  13.  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14.  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15.  *  for more details.
  16.  *
  17.  *  You should have received a copy of the GNU General Public License along
  18.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  19.  *  59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  20.  *
  21.  * ########################################################################
  22.  *
  23.  * Ethernet driver for the MIPS GT96100 Advanced Communication Controller.
  24.  * 
  25.  *  Revision history
  26.  *    
  27.  *    11.11.2001  Moved to 2.4.14, ppopov@mvista.com.  Modified driver to add
  28.  *                proper gt96100A support.
  29.  *    12.05.2001  Moved eth port 0 to irq 3 (mapped to GT_SERINT0 on EV96100A)
  30.  *                in order for both ports to work. Also cleaned up boot
  31.  *                option support (mac address string parsing), fleshed out
  32.  *                gt96100_cleanup_module(), and other general code cleanups
  33.  *                <stevel@mvista.com>.
  34.  */
  35. #include <linux/config.h>
  36. #include <linux/module.h>
  37. #include <linux/kernel.h>
  38. #include <linux/sched.h>
  39. #include <linux/string.h>
  40. #include <linux/timer.h>
  41. #include <linux/errno.h>
  42. #include <linux/in.h>
  43. #include <linux/ioport.h>
  44. #include <linux/slab.h>
  45. #include <linux/interrupt.h>
  46. #include <linux/pci.h>
  47. #include <linux/init.h>
  48. #include <linux/netdevice.h>
  49. #include <linux/etherdevice.h>
  50. #include <linux/skbuff.h>
  51. #include <linux/delay.h>
  52. #include <linux/ctype.h>
  53. #include <asm/irq.h>
  54. #include <asm/bitops.h>
  55. #include <asm/io.h>
  56. #define DESC_BE 1
  57. #define DESC_DATA_BE 1
  58. #define GT96100_DEBUG 2
  59. #include "gt96100eth.h"
  60. // prototypes
  61. static void* dmaalloc(size_t size, dma_addr_t *dma_handle);
  62. static void dmafree(size_t size, void *vaddr);
  63. static void gt96100_delay(int msec);
  64. static int gt96100_add_hash_entry(struct net_device *dev,
  65.   unsigned char* addr);
  66. static void read_mib_counters(struct gt96100_private *gp);
  67. static int read_MII(int phy_addr, u32 reg);
  68. static int write_MII(int phy_addr, u32 reg, u16 data);
  69. #if 0
  70. static void dump_tx_ring(struct net_device *dev);
  71. static void dump_rx_ring(struct net_device *dev);
  72. #endif
  73. static int gt96100_init_module(void);
  74. static void gt96100_cleanup_module(void);
  75. static void dump_MII(int dbg_lvl, struct net_device *dev);
  76. static void dump_tx_desc(int dbg_lvl, struct net_device *dev, int i);
  77. static void dump_rx_desc(int dbg_lvl, struct net_device *dev, int i);
  78. static void dump_skb(int dbg_lvl, struct net_device *dev,
  79.      struct sk_buff *skb);
  80. static void dump_hw_addr(int dbg_lvl, struct net_device *dev,
  81.  const char* pfx, unsigned char* addr_str);
  82. static void update_stats(struct gt96100_private *gp);
  83. static void abort(struct net_device *dev, u32 abort_bits);
  84. static void hard_stop(struct net_device *dev);
  85. static void enable_ether_irq(struct net_device *dev);
  86. static void disable_ether_irq(struct net_device *dev);
  87. static int gt96100_probe1(int port_num);
  88. static void reset_tx(struct net_device *dev);
  89. static void reset_rx(struct net_device *dev);
  90. static int gt96100_check_tx_consistent(struct gt96100_private *gp);
  91. static int gt96100_init(struct net_device *dev);
  92. static int gt96100_open(struct net_device *dev);
  93. static int gt96100_close(struct net_device *dev);
  94. static int gt96100_tx(struct sk_buff *skb, struct net_device *dev);
  95. static int gt96100_rx(struct net_device *dev, u32 status);
  96. static void gt96100_interrupt(int irq, void *dev_id, struct pt_regs *regs);
  97. static void gt96100_tx_timeout(struct net_device *dev);
  98. static void gt96100_set_rx_mode(struct net_device *dev);
  99. static struct net_device_stats* gt96100_get_stats(struct net_device *dev);
  100. extern char * __init prom_getcmdline(void);
  101. static int max_interrupt_work = 32;
  102. #define nibswap(x) ((((x) >> 4) & 0x0f) | (((x) << 4) & 0xf0))
  103. #define RUN_AT(x) (jiffies + (x))
  104. // For reading/writing 32-bit words and half-words from/to DMA memory
  105. #ifdef DESC_BE
  106. #define cpu_to_dma32 cpu_to_be32
  107. #define dma32_to_cpu be32_to_cpu
  108. #define cpu_to_dma16 cpu_to_be16
  109. #define dma16_to_cpu be16_to_cpu
  110. #else
  111. #define cpu_to_dma32 cpu_to_le32
  112. #define dma32_to_cpu le32_to_cpu
  113. #define cpu_to_dma16 cpu_to_le16
  114. #define dma16_to_cpu le16_to_cpu
  115. #endif
  116. static char mac0[18] = "00.02.03.04.05.06";
  117. static char mac1[18] = "00.01.02.03.04.05";
  118. MODULE_PARM(mac0, "c18");
  119. MODULE_PARM(mac1, "c18");
  120. MODULE_PARM_DESC(mac0, "MAC address for GT96100 ethernet port 0");
  121. MODULE_PARM_DESC(mac1, "MAC address for GT96100 ethernet port 1");
  122. /*
  123.  * Info for the GT96100 ethernet controller's ports.
  124.  */
  125. static struct gt96100_if_t {
  126. struct net_device *dev;
  127. unsigned int  iobase;   // IO Base address of this port
  128. int           irq;      // IRQ number of this port
  129. char         *mac_str;
  130. } gt96100_iflist[NUM_INTERFACES] = {
  131. {
  132. NULL,
  133. GT96100_ETH0_BASE, GT96100_ETHER0_IRQ,
  134. mac0
  135. },
  136. {
  137. NULL,
  138. GT96100_ETH1_BASE, GT96100_ETHER1_IRQ,
  139. mac1
  140. }
  141. };
  142. static inline const char*
  143. chip_name(int chip_rev)
  144. {
  145. switch (chip_rev) {
  146. case REV_GT96100:
  147. return "GT96100";
  148. case REV_GT96100A_1:
  149. case REV_GT96100A:
  150. return "GT96100A";
  151. default:
  152. return "Unknown GT96100";
  153. }
  154. }
  155. /*
  156.   DMA memory allocation, derived from pci_alloc_consistent.
  157. */
  158. static void *
  159. dmaalloc(size_t size, dma_addr_t *dma_handle)
  160. {
  161. void *ret;
  162. ret = (void *)__get_free_pages(GFP_ATOMIC | GFP_DMA,
  163.        get_order(size));
  164. if (ret != NULL) {
  165. dma_cache_inv((unsigned long)ret, size);
  166. if (dma_handle != NULL)
  167. *dma_handle = virt_to_phys(ret);
  168. /* bump virtual address up to non-cached area */
  169. ret = (void*)KSEG1ADDR(ret);
  170. }
  171. return ret;
  172. }
  173. static void
  174. dmafree(size_t size, void *vaddr)
  175. {
  176. vaddr = (void*)KSEG0ADDR(vaddr);
  177. free_pages((unsigned long)vaddr, get_order(size));
  178. }
  179. static void
  180. gt96100_delay(int ms)
  181. {
  182. if (in_interrupt())
  183. return;
  184. else {
  185. current->state = TASK_INTERRUPTIBLE;
  186. schedule_timeout(ms*HZ/1000);
  187. }
  188. }
  189. static int
  190. parse_mac_addr(struct net_device *dev, char* macstr)
  191. {
  192. int i, j;
  193. unsigned char result, value;
  194. for (i=0; i<6; i++) {
  195. result = 0;
  196. if (i != 5 && *(macstr+2) != '.') {
  197. err(__FILE__ "invalid mac address format: %d %cn",
  198.     i, *(macstr+2));
  199. return -EINVAL;
  200. }
  201. for (j=0; j<2; j++) {
  202. if (isxdigit(*macstr) &&
  203.     (value = isdigit(*macstr) ? *macstr-'0' : 
  204.      toupper(*macstr)-'A'+10) < 16) {
  205. result = result*16 + value;
  206. macstr++;
  207. } else {
  208. err(__FILE__ "invalid mac address "
  209.     "character: %cn", *macstr);
  210. return -EINVAL;
  211. }
  212. }
  213. macstr++; // step over '.'
  214. dev->dev_addr[i] = result;
  215. }
  216. return 0;
  217. }
  218. static int
  219. read_MII(int phy_addr, u32 reg)
  220. {
  221. int timedout = 20;
  222. u32 smir = smirOpCode | (phy_addr << smirPhyAdBit) |
  223. (reg << smirRegAdBit);
  224. // wait for last operation to complete
  225. while (GT96100_READ(GT96100_ETH_SMI_REG) & smirBusy) {
  226. // snooze for 1 msec and check again
  227. gt96100_delay(1);
  228. if (--timedout == 0) {
  229. printk(KERN_ERR "%s: busy timeout!!n", __FUNCTION__);
  230. return -ENODEV;
  231. }
  232. }
  233.     
  234. GT96100_WRITE(GT96100_ETH_SMI_REG, smir);
  235. timedout = 20;
  236. // wait for read to complete
  237. while (!((smir = GT96100_READ(GT96100_ETH_SMI_REG)) & smirReadValid)) {
  238. // snooze for 1 msec and check again
  239. gt96100_delay(1);
  240. if (--timedout == 0) {
  241. printk(KERN_ERR "%s: timeout!!n", __FUNCTION__);
  242. return -ENODEV;
  243. }
  244. }
  245. return (int)(smir & smirDataMask);
  246. }
  247. static void
  248. dump_tx_desc(int dbg_lvl, struct net_device *dev, int i)
  249. {
  250. struct gt96100_private *gp = (struct gt96100_private *)dev->priv;
  251. gt96100_td_t *td = &gp->tx_ring[i];
  252. dbg(dbg_lvl, "Tx descriptor at 0x%08lx:n", virt_to_phys(td));
  253. dbg(dbg_lvl,
  254.     "    cmdstat=%04x, byte_cnt=%04x, buff_ptr=%04x, next=%04xn",
  255.     dma32_to_cpu(td->cmdstat),
  256.     dma16_to_cpu(td->byte_cnt),
  257.     dma32_to_cpu(td->buff_ptr),
  258.     dma32_to_cpu(td->next));
  259. }
  260. static void
  261. dump_rx_desc(int dbg_lvl, struct net_device *dev, int i)
  262. {
  263. struct gt96100_private *gp = (struct gt96100_private *)dev->priv;
  264. gt96100_rd_t *rd = &gp->rx_ring[i];
  265. dbg(dbg_lvl, "Rx descriptor at 0x%08lx:n", virt_to_phys(rd));
  266. dbg(dbg_lvl, "    cmdstat=%04x, buff_sz=%04x, byte_cnt=%04x, "
  267.     "buff_ptr=%04x, next=%04xn",
  268.     dma32_to_cpu(rd->cmdstat),
  269.     dma16_to_cpu(rd->buff_sz),
  270.     dma16_to_cpu(rd->byte_cnt),
  271.     dma32_to_cpu(rd->buff_ptr),
  272.     dma32_to_cpu(rd->next));
  273. }
  274. static int
  275. write_MII(int phy_addr, u32 reg, u16 data)
  276. {
  277. int timedout = 20;
  278. u32 smir = (phy_addr << smirPhyAdBit) |
  279. (reg << smirRegAdBit) | data;
  280. // wait for last operation to complete
  281. while (GT96100_READ(GT96100_ETH_SMI_REG) & smirBusy) {
  282. // snooze for 1 msec and check again
  283. gt96100_delay(1);
  284. if (--timedout == 0) {
  285. printk(KERN_ERR "%s: busy timeout!!n", __FUNCTION__);
  286. return -1;
  287. }
  288. }
  289. GT96100_WRITE(GT96100_ETH_SMI_REG, smir);
  290. return 0;
  291. }
  292. #if 0
  293. // These routines work, just disabled to avoid compile warnings
  294. static void
  295. dump_tx_ring(struct net_device *dev)
  296. {
  297. struct gt96100_private *gp = (struct gt96100_private *)dev->priv;
  298. int i;
  299. dbg(0, "%s: txno/txni/cnt=%d/%d/%dn", __FUNCTION__,
  300.     gp->tx_next_out, gp->tx_next_in, gp->tx_count);
  301. for (i=0; i<TX_RING_SIZE; i++)
  302. dump_tx_desc(0, dev, i);
  303. }
  304. static void
  305. dump_rx_ring(struct net_device *dev)
  306. {
  307. struct gt96100_private *gp = (struct gt96100_private *)dev->priv;
  308. int i;
  309. dbg(0, "%s: rxno=%dn", __FUNCTION__, gp->rx_next_out);
  310. for (i=0; i<RX_RING_SIZE; i++)
  311. dump_rx_desc(0, dev, i);
  312. }
  313. #endif
  314. static void
  315. dump_MII(int dbg_lvl, struct net_device *dev)
  316. {
  317. int i, val;
  318. struct gt96100_private *gp = (struct gt96100_private *)dev->priv;
  319.     
  320. if (dbg_lvl <= GT96100_DEBUG) {
  321. for (i=0; i<7; i++) {
  322. if ((val = read_MII(gp->phy_addr, i)) >= 0)
  323. printk("MII Reg %d=%xn", i, val);
  324. }
  325. for (i=16; i<21; i++) {
  326. if ((val = read_MII(gp->phy_addr, i)) >= 0)
  327. printk("MII Reg %d=%xn", i, val);
  328. }
  329. }
  330. }
  331. static void
  332. dump_hw_addr(int dbg_lvl, struct net_device *dev, const char* pfx,
  333.      unsigned char* addr_str)
  334. {
  335. int i;
  336. char buf[100], octet[5];
  337.     
  338. if (dbg_lvl <= GT96100_DEBUG) {
  339. strcpy(buf, pfx);
  340. for (i = 0; i < 6; i++) {
  341. sprintf(octet, "%2.2x%s",
  342. addr_str[i], i<5 ? ":" : "n");
  343. strcat(buf, octet);
  344. }
  345. info("%s", buf);
  346. }
  347. }
  348. static void
  349. dump_skb(int dbg_lvl, struct net_device *dev, struct sk_buff *skb)
  350. {
  351. int i;
  352. unsigned char* skbdata;
  353.     
  354. if (dbg_lvl <= GT96100_DEBUG) {
  355. dbg(dbg_lvl, "%s: skb=%p, skb->data=%p, skb->len=%dn",
  356.     __FUNCTION__, skb, skb->data, skb->len);
  357. skbdata = (unsigned char*)KSEG1ADDR(skb->data);
  358.     
  359. for (i=0; i<skb->len; i++) {
  360. if (!(i % 16))
  361. printk(KERN_DEBUG "n   %3.3x: %2.2x,",
  362.        i, skbdata[i]);
  363. else
  364. printk(KERN_DEBUG "%2.2x,", skbdata[i]);
  365. }
  366. printk(KERN_DEBUG "n");
  367. }
  368. }
  369. static int
  370. gt96100_add_hash_entry(struct net_device *dev, unsigned char* addr)
  371. {
  372. struct gt96100_private *gp = (struct gt96100_private *)dev->priv;
  373. //u16 hashResult, stmp;
  374. //unsigned char ctmp, hash_ea[6];
  375. u32 tblEntry1, tblEntry0, *tblEntryAddr;
  376. int i;
  377. tblEntry1 = hteValid | hteRD;
  378. tblEntry1 |= (u32)addr[5] << 3;
  379. tblEntry1 |= (u32)addr[4] << 11;
  380. tblEntry1 |= (u32)addr[3] << 19;
  381. tblEntry1 |= ((u32)addr[2] & 0x1f) << 27;
  382. dbg(3, "%s: tblEntry1=%xn", __FUNCTION__, tblEntry1);
  383. tblEntry0 = ((u32)addr[2] >> 5) & 0x07;
  384. tblEntry0 |= (u32)addr[1] << 3;
  385. tblEntry0 |= (u32)addr[0] << 11;
  386. dbg(3, "%s: tblEntry0=%xn", __FUNCTION__, tblEntry0);
  387. #if 0
  388. for (i=0; i<6; i++) {
  389. // nibble swap
  390. ctmp = nibswap(addr[i]);
  391. // invert every nibble
  392. hash_ea[i] = ((ctmp&1)<<3) | ((ctmp&8)>>3) |
  393. ((ctmp&2)<<1) | ((ctmp&4)>>1);
  394. hash_ea[i] |= ((ctmp&0x10)<<3) | ((ctmp&0x80)>>3) |
  395. ((ctmp&0x20)<<1) | ((ctmp&0x40)>>1);
  396. }
  397. dump_hw_addr(3, dev, "%s: nib swap/invt addr=", __FUNCTION__, hash_ea);
  398.     
  399. if (gp->hash_mode == 0) {
  400. hashResult = ((u16)hash_ea[0] & 0xfc) << 7;
  401. stmp = ((u16)hash_ea[0] & 0x03) |
  402. (((u16)hash_ea[1] & 0x7f) << 2);
  403. stmp ^= (((u16)hash_ea[1] >> 7) & 0x01) |
  404. ((u16)hash_ea[2] << 1);
  405. stmp ^= (u16)hash_ea[3] | (((u16)hash_ea[4] & 1) << 8);
  406. hashResult |= stmp;
  407. } else {
  408. return -1; // don't support hash mode 1
  409. }
  410. dbg(3, "%s: hashResult=%xn", __FUNCTION__, hashResult);
  411. tblEntryAddr =
  412. (u32 *)(&gp->hash_table[((u32)hashResult & 0x7ff) << 3]);
  413.     
  414. dbg(3, "%s: tblEntryAddr=%pn", tblEntryAddr, __FUNCTION__);
  415. for (i=0; i<HASH_HOP_NUMBER; i++) {
  416. if ((*tblEntryAddr & hteValid) &&
  417.     !(*tblEntryAddr & hteSkip)) {
  418. // This entry is already occupied, go to next entry
  419. tblEntryAddr += 2;
  420. dbg(3, "%s: skipping to %pn", __FUNCTION__, 
  421.     tblEntryAddr);
  422. } else {
  423. memset(tblEntryAddr, 0, 8);
  424. tblEntryAddr[1] = cpu_to_dma32(tblEntry1);
  425. tblEntryAddr[0] = cpu_to_dma32(tblEntry0);
  426. break;
  427. }
  428. }
  429. if (i >= HASH_HOP_NUMBER) {
  430. err("%s: expired!n", __FUNCTION__);
  431. return -1; // Couldn't find an unused entry
  432. }
  433. #else
  434. tblEntryAddr = (u32 *)gp->hash_table;
  435. for (i=0; i<RX_HASH_TABLE_SIZE/4; i+=2) {
  436. tblEntryAddr[i+1] = cpu_to_dma32(tblEntry1);
  437. tblEntryAddr[i] = cpu_to_dma32(tblEntry0);
  438. }
  439. #endif
  440.     
  441. return 0;
  442. }
  443. static void
  444. read_mib_counters(struct gt96100_private *gp)
  445. {
  446. u32* mib_regs = (u32*)&gp->mib;
  447. int i;
  448.     
  449. for (i=0; i<sizeof(mib_counters_t)/sizeof(u32); i++)
  450. mib_regs[i] = GT96100ETH_READ(gp, GT96100_ETH_MIB_COUNT_BASE +
  451.       i*sizeof(u32));
  452. }
  453. static void
  454. update_stats(struct gt96100_private *gp)
  455. {
  456. mib_counters_t *mib = &gp->mib;
  457. struct net_device_stats *stats = &gp->stats;
  458.     
  459. read_mib_counters(gp);
  460.     
  461. stats->rx_packets = mib->totalFramesReceived;
  462. stats->tx_packets = mib->framesSent;
  463. stats->rx_bytes = mib->totalByteReceived;
  464. stats->tx_bytes = mib->byteSent;
  465. stats->rx_errors = mib->totalFramesReceived - mib->framesReceived;
  466. //the tx error counters are incremented by the ISR
  467. //rx_dropped incremented by gt96100_rx
  468. //tx_dropped incremented by gt96100_tx
  469. stats->multicast = mib->multicastFramesReceived;
  470. // collisions incremented by gt96100_tx_complete
  471. stats->rx_length_errors = mib->oversizeFrames + mib->fragments;
  472. // The RxError condition means the Rx DMA encountered a
  473. // CPU owned descriptor, which, if things are working as
  474. // they should, means the Rx ring has overflowed.
  475. stats->rx_over_errors = mib->macRxError;
  476. stats->rx_crc_errors = mib->cRCError;
  477. }
  478. static void
  479. abort(struct net_device *dev, u32 abort_bits)
  480. {
  481. struct gt96100_private *gp = (struct gt96100_private *)dev->priv;
  482. int timedout = 100; // wait up to 100 msec for hard stop to complete
  483. dbg(3, "%sn", __FUNCTION__);
  484. // Return if neither Rx or Tx abort bits are set
  485. if (!(abort_bits & (sdcmrAR | sdcmrAT)))
  486. return;
  487. // make sure only the Rx/Tx abort bits are set
  488. abort_bits &= (sdcmrAR | sdcmrAT);
  489.     
  490. spin_lock(&gp->lock);
  491. // abort any Rx/Tx DMA immediately
  492. GT96100ETH_WRITE(gp, GT96100_ETH_SDMA_COMM, abort_bits);
  493. dbg(3, "%s: SDMA comm = %xn", __FUNCTION__,
  494.     GT96100ETH_READ(gp, GT96100_ETH_SDMA_COMM));
  495. // wait for abort to complete
  496. while (GT96100ETH_READ(gp, GT96100_ETH_SDMA_COMM) & abort_bits) {
  497. // snooze for 20 msec and check again
  498. gt96100_delay(1);
  499. if (--timedout == 0) {
  500. err("%s: timeout!!n", __FUNCTION__);
  501. break;
  502. }
  503. }
  504. spin_unlock(&gp->lock);
  505. }
  506. static void
  507. hard_stop(struct net_device *dev)
  508. {
  509. struct gt96100_private *gp = (struct gt96100_private *)dev->priv;
  510. dbg(3, "%sn", __FUNCTION__);
  511. disable_ether_irq(dev);
  512. abort(dev, sdcmrAR | sdcmrAT);
  513. // disable port
  514. GT96100ETH_WRITE(gp, GT96100_ETH_PORT_CONFIG, 0);
  515. }
  516. static void
  517. enable_ether_irq(struct net_device *dev)
  518. {
  519. struct gt96100_private *gp = (struct gt96100_private *)dev->priv;
  520. u32 intMask;
  521. /*
  522.  * route ethernet interrupt to GT_SERINT0 for port 0,
  523.  * GT_INT0 for port 1.
  524.  */
  525. int intr_mask_reg = (gp->port_num == 0) ?
  526. GT96100_SERINT0_MASK : GT96100_INT0_HIGH_MASK;
  527. if (gp->chip_rev >= REV_GT96100A_1) {
  528. intMask = icrTxBufferLow | icrTxEndLow |
  529. icrTxErrorLow  | icrRxOVR | icrTxUdr |
  530. icrRxBufferQ0 | icrRxErrorQ0 |
  531. icrMIIPhySTC | icrEtherIntSum;
  532. }
  533. else {
  534. intMask = icrTxBufferLow | icrTxEndLow |
  535. icrTxErrorLow  | icrRxOVR | icrTxUdr |
  536. icrRxBuffer | icrRxError |
  537. icrMIIPhySTC | icrEtherIntSum;
  538. }
  539. // unmask interrupts
  540. GT96100ETH_WRITE(gp, GT96100_ETH_INT_MASK, intMask);
  541.     
  542. intMask = GT96100_READ(intr_mask_reg);
  543. intMask |= 1<<gp->port_num;
  544. GT96100_WRITE(intr_mask_reg, intMask);
  545. }
  546. static void
  547. disable_ether_irq(struct net_device *dev)
  548. {
  549. struct gt96100_private *gp = (struct gt96100_private *)dev->priv;
  550. u32 intMask;
  551. int intr_mask_reg = (gp->port_num == 0) ?
  552. GT96100_SERINT0_MASK : GT96100_INT0_HIGH_MASK;
  553. intMask = GT96100_READ(intr_mask_reg);
  554. intMask &= ~(1<<gp->port_num);
  555. GT96100_WRITE(intr_mask_reg, intMask);
  556.     
  557. GT96100ETH_WRITE(gp, GT96100_ETH_INT_MASK, 0);
  558. }
  559. /*
  560.  * Init GT96100 ethernet controller driver
  561.  */
  562. int gt96100_init_module(void)
  563. {
  564. int i, retval=0;
  565. u16 vendor_id, device_id;
  566. u32 cpuConfig;
  567. #ifndef CONFIG_MIPS_GT96100ETH
  568. return -ENODEV;
  569. #endif
  570. // probe for GT96100 by reading PCI0 vendor/device ID register
  571. pcibios_read_config_word(0, 0, PCI_VENDOR_ID, &vendor_id);
  572. pcibios_read_config_word(0, 0, PCI_DEVICE_ID, &device_id);
  573.     
  574. if (vendor_id != PCI_VENDOR_ID_GALILEO ||
  575.     (device_id != PCI_DEVICE_ID_GALILEO_GT96100 &&
  576.      device_id != PCI_DEVICE_ID_GALILEO_GT96100A)) {
  577. printk(KERN_ERR __FILE__ ": GT96100 not found!n");
  578. return -ENODEV;
  579. }
  580. cpuConfig = GT96100_READ(GT96100_CPU_INTERF_CONFIG);
  581. if (cpuConfig & (1<<12)) {
  582. printk(KERN_ERR __FILE__
  583.        ": must be in Big Endian mode!n");
  584. return -ENODEV;
  585. }
  586. for (i=0; i < NUM_INTERFACES; i++) {
  587. retval |= gt96100_probe1(i);
  588. }
  589. return retval;
  590. }
  591. static int __init
  592. gt96100_probe1(int port_num)
  593. {
  594. struct gt96100_private *gp = NULL;
  595. struct gt96100_if_t *gtif = &gt96100_iflist[port_num];
  596. int phy_addr, phy_id1, phy_id2;
  597. u32 phyAD;
  598. int retval;
  599. unsigned char chip_rev;
  600. struct net_device *dev = NULL;
  601.     
  602. if (gtif->irq < 0) {
  603. printk(KERN_ERR "%s: irq unknown - probing not supportedn", __FUNCTION_);
  604. return -ENODEV;
  605. }
  606.     
  607. pcibios_read_config_byte(0, 0, PCI_REVISION_ID, &chip_rev);
  608. if (chip_rev >= REV_GT96100A_1) {
  609. phyAD = GT96100_READ(GT96100_ETH_PHY_ADDR_REG);
  610. phy_addr = (phyAD >> (5*port_num)) & 0x1f;
  611. } else {
  612. /*
  613.  * not sure what's this about -- probably 
  614.  * a gt bug
  615.  */
  616. phy_addr = port_num;
  617. phyAD = GT96100_READ(GT96100_ETH_PHY_ADDR_REG);
  618. phyAD &= ~(0x1f << (port_num*5));
  619. phyAD |= phy_addr << (port_num*5);
  620. GT96100_WRITE(GT96100_ETH_PHY_ADDR_REG, phyAD);
  621. }
  622. // probe for the external PHY
  623. if ((phy_id1 = read_MII(phy_addr, 2)) <= 0 ||
  624.     (phy_id2 = read_MII(phy_addr, 3)) <= 0) {
  625. printk(KERN_ERR "%s: no PHY found on MII%dn", __FUNCTION__, port_num);
  626. return -ENODEV;
  627. }
  628. if (!request_region(gtif->iobase, GT96100_ETH_IO_SIZE, "GT96100ETH")) {
  629. printk(KERN_ERR "%s: request_region failedn", __FUNCTION__);
  630. return -EBUSY;
  631. }
  632. dev = init_etherdev(0, sizeof(struct gt96100_private));
  633. gtif->dev = dev;
  634. /* private struct aligned and zeroed by init_etherdev */
  635. /* Fill in the 'dev' fields. */
  636. dev->base_addr = gtif->iobase;
  637. dev->irq = gtif->irq;
  638. if ((retval = parse_mac_addr(dev, gtif->mac_str))) {
  639. err("%s: MAC address parse failedn", __FUNCTION__);
  640. retval = -EINVAL;
  641. goto free_region;
  642. }
  643. /* Initialize our private structure. */
  644. if (dev->priv == NULL) {
  645. gp = (struct gt96100_private *)kmalloc(sizeof(*gp),
  646.        GFP_KERNEL);
  647. if (gp == NULL) {
  648. retval = -ENOMEM;
  649. goto free_region;
  650. }
  651. dev->priv = gp;
  652. }
  653. gp = dev->priv;
  654. memset(gp, 0, sizeof(*gp)); // clear it
  655. gp->port_num = port_num;
  656. gp->io_size = GT96100_ETH_IO_SIZE;
  657. gp->port_offset = port_num * GT96100_ETH_IO_SIZE;
  658. gp->phy_addr = phy_addr;
  659. gp->chip_rev = chip_rev;
  660. info("%s found at 0x%x, irq %dn",
  661.      chip_name(gp->chip_rev), gtif->iobase, gtif->irq);
  662. dump_hw_addr(0, dev, "HW Address ", dev->dev_addr);
  663. info("%s chip revision=%dn", chip_name(gp->chip_rev), gp->chip_rev);
  664. info("%s ethernet port %dn", chip_name(gp->chip_rev), gp->port_num);
  665. info("external PHY ID1=0x%04x, ID2=0x%04xn", phy_id1, phy_id2);
  666. // Allocate Rx and Tx descriptor rings
  667. if (gp->rx_ring == NULL) {
  668. // All descriptors in ring must be 16-byte aligned
  669. gp->rx_ring = dmaalloc(sizeof(gt96100_rd_t) * RX_RING_SIZE
  670.        + sizeof(gt96100_td_t) * TX_RING_SIZE,
  671.        &gp->rx_ring_dma);
  672. if (gp->rx_ring == NULL) {
  673. retval = -ENOMEM;
  674. goto free_region;
  675. }
  676. gp->tx_ring = (gt96100_td_t *)(gp->rx_ring + RX_RING_SIZE);
  677. gp->tx_ring_dma =
  678. gp->rx_ring_dma + sizeof(gt96100_rd_t) * RX_RING_SIZE;
  679. }
  680.     
  681. // Allocate the Rx Data Buffers
  682. if (gp->rx_buff == NULL) {
  683. gp->rx_buff = dmaalloc(PKT_BUF_SZ*RX_RING_SIZE,
  684.        &gp->rx_buff_dma);
  685. if (gp->rx_buff == NULL) {
  686. dmafree(sizeof(gt96100_rd_t) * RX_RING_SIZE
  687. + sizeof(gt96100_td_t) * TX_RING_SIZE,
  688. gp->rx_ring);
  689. retval = -ENOMEM;
  690. goto free_region;
  691. }
  692. }
  693.     
  694. dbg(3, "%s: rx_ring=%p, tx_ring=%pn", __FUNCTION__,
  695.     gp->rx_ring, gp->tx_ring);
  696. // Allocate Rx Hash Table
  697. if (gp->hash_table == NULL) {
  698. gp->hash_table = (char*)dmaalloc(RX_HASH_TABLE_SIZE,
  699.  &gp->hash_table_dma);
  700. if (gp->hash_table == NULL) {
  701. dmafree(sizeof(gt96100_rd_t) * RX_RING_SIZE
  702. + sizeof(gt96100_td_t) * TX_RING_SIZE,
  703. gp->rx_ring);
  704. dmafree(PKT_BUF_SZ*RX_RING_SIZE, gp->rx_buff);
  705. retval = -ENOMEM;
  706. goto free_region;
  707. }
  708. }
  709.     
  710. dbg(3, "%s: hash=%pn", __FUNCTION__, gp->hash_table);
  711. spin_lock_init(&gp->lock);
  712.     
  713. dev->open = gt96100_open;
  714. dev->hard_start_xmit = gt96100_tx;
  715. dev->stop = gt96100_close;
  716. dev->get_stats = gt96100_get_stats;
  717. //dev->do_ioctl = gt96100_ioctl;
  718. dev->set_multicast_list = gt96100_set_rx_mode;
  719. dev->tx_timeout = gt96100_tx_timeout;
  720. dev->watchdog_timeo = GT96100ETH_TX_TIMEOUT;
  721. /* Fill in the fields of the device structure with ethernet values. */
  722. ether_setup(dev);
  723. return 0;
  724.  free_region:
  725. release_region(gtif->iobase, GT96100_ETH_IO_SIZE);
  726. unregister_netdev(dev);
  727. if (dev->priv != NULL)
  728. kfree (dev->priv);
  729. kfree (dev);
  730. err("%s failed.  Returns %dn", __FUNCTION__, retval);
  731. return retval;
  732. }
  733. static void
  734. reset_tx(struct net_device *dev)
  735. {
  736. struct gt96100_private *gp = (struct gt96100_private *)dev->priv;
  737. int i;
  738. abort(dev, sdcmrAT);
  739. for (i=0; i<TX_RING_SIZE; i++) {
  740. if (gp->tx_skbuff[i]) {
  741. if (in_interrupt())
  742. dev_kfree_skb_irq(gp->tx_skbuff[i]);
  743. else
  744. dev_kfree_skb(gp->tx_skbuff[i]);
  745. gp->tx_skbuff[i] = NULL;
  746. }
  747. gp->tx_ring[i].cmdstat = 0; // CPU owns
  748. gp->tx_ring[i].byte_cnt = 0;
  749. gp->tx_ring[i].buff_ptr = 0;
  750. gp->tx_ring[i].next =
  751. cpu_to_dma32(gp->tx_ring_dma +
  752.      sizeof(gt96100_td_t) * (i+1));
  753. dump_tx_desc(4, dev, i);
  754. }
  755. /* Wrap the ring. */
  756. gp->tx_ring[i-1].next = cpu_to_dma32(gp->tx_ring_dma);
  757.     
  758. // setup only the lowest priority TxCDP reg
  759. GT96100ETH_WRITE(gp, GT96100_ETH_CURR_TX_DESC_PTR0, gp->tx_ring_dma);
  760. GT96100ETH_WRITE(gp, GT96100_ETH_CURR_TX_DESC_PTR1, 0);
  761. // init Tx indeces and pkt counter
  762. gp->tx_next_in = gp->tx_next_out = 0;
  763. gp->tx_count = 0;
  764. }
  765. static void
  766. reset_rx(struct net_device *dev)
  767. {
  768. struct gt96100_private *gp = (struct gt96100_private *)dev->priv;
  769. int i;
  770. abort(dev, sdcmrAR);
  771.     
  772. for (i=0; i<RX_RING_SIZE; i++) {
  773. gp->rx_ring[i].next =
  774. cpu_to_dma32(gp->rx_ring_dma +
  775.      sizeof(gt96100_rd_t) * (i+1));
  776. gp->rx_ring[i].buff_ptr =
  777. cpu_to_dma32(gp->rx_buff_dma + i*PKT_BUF_SZ);
  778. gp->rx_ring[i].buff_sz = cpu_to_dma16(PKT_BUF_SZ);
  779. // Give ownership to device, set first and last, enable intr
  780. gp->rx_ring[i].cmdstat =
  781. cpu_to_dma32((u32)(rxFirst | rxLast | rxOwn | rxEI));
  782. dump_rx_desc(4, dev, i);
  783. }
  784. /* Wrap the ring. */
  785. gp->rx_ring[i-1].next = cpu_to_dma32(gp->rx_ring_dma);
  786. // Setup only the lowest priority RxFDP and RxCDP regs
  787. for (i=0; i<4; i++) {
  788. if (i == 0) {
  789. GT96100ETH_WRITE(gp, GT96100_ETH_1ST_RX_DESC_PTR0,
  790.  gp->rx_ring_dma);
  791. GT96100ETH_WRITE(gp, GT96100_ETH_CURR_RX_DESC_PTR0,
  792.  gp->rx_ring_dma);
  793. } else {
  794. GT96100ETH_WRITE(gp,
  795.  GT96100_ETH_1ST_RX_DESC_PTR0 + i*4,
  796.  0);
  797. GT96100ETH_WRITE(gp,
  798.  GT96100_ETH_CURR_RX_DESC_PTR0 + i*4,
  799.  0);
  800. }
  801. }
  802. // init Rx NextOut index
  803. gp->rx_next_out = 0;
  804. }
  805. // Returns 1 if the Tx counter and indeces don't gel
  806. static int
  807. gt96100_check_tx_consistent(struct gt96100_private *gp)
  808. {
  809. int diff = gp->tx_next_in - gp->tx_next_out;
  810. diff = diff<0 ? TX_RING_SIZE + diff : diff;
  811. diff = gp->tx_count == TX_RING_SIZE ? diff + TX_RING_SIZE : diff;
  812.     
  813. return (diff != gp->tx_count);
  814. }
  815. static int
  816. gt96100_init(struct net_device *dev)
  817. {
  818. struct gt96100_private *gp = (struct gt96100_private *)dev->priv;
  819. u32 tmp;
  820. u16 mii_reg;
  821.     
  822. dbg(3, "%s: dev=%pn", __FUNCTION__, dev);
  823. dbg(3, "%s: scs10_lo=%4x, scs10_hi=%4xn", __FUNCTION__, 
  824.     GT96100_READ(0x8), GT96100_READ(0x10));
  825. dbg(3, "%s: scs32_lo=%4x, scs32_hi=%4xn", __FUNCTION__,
  826.     GT96100_READ(0x18), GT96100_READ(0x20));
  827.     
  828. // Stop and disable Port
  829. hard_stop(dev);
  830.     
  831. // Setup CIU Arbiter
  832. tmp = GT96100_READ(GT96100_CIU_ARBITER_CONFIG);
  833. tmp |= (0x0c << (gp->port_num*2)); // set Ether DMA req priority to hi
  834. #ifndef DESC_BE
  835. tmp &= ~(1<<31);                   // set desc endianess to little
  836. #else
  837. tmp |= (1<<31);
  838. #endif
  839. GT96100_WRITE(GT96100_CIU_ARBITER_CONFIG, tmp);
  840. dbg(3, "%s: CIU Config=%x/%xn", __FUNCTION__, 
  841.     tmp, GT96100_READ(GT96100_CIU_ARBITER_CONFIG));
  842. // Set routing.
  843. tmp = GT96100_READ(GT96100_ROUTE_MAIN) & (0x3f << 18);
  844. tmp |= (0x07 << (18 + gp->port_num*3));
  845. GT96100_WRITE(GT96100_ROUTE_MAIN, tmp);
  846. /* set MII as peripheral func */
  847. tmp = GT96100_READ(GT96100_GPP_CONFIG2);
  848. tmp |= 0x7fff << (gp->port_num*16);
  849. GT96100_WRITE(GT96100_GPP_CONFIG2, tmp);
  850. /* Set up MII port pin directions */
  851. tmp = GT96100_READ(GT96100_GPP_IO2);
  852. tmp |= 0x003d << (gp->port_num*16);
  853. GT96100_WRITE(GT96100_GPP_IO2, tmp);
  854. // Set-up hash table
  855. memset(gp->hash_table, 0, RX_HASH_TABLE_SIZE); // clear it
  856. gp->hash_mode = 0;
  857. // Add a single entry to hash table - our ethernet address
  858. gt96100_add_hash_entry(dev, dev->dev_addr);
  859. // Set-up DMA ptr to hash table
  860. GT96100ETH_WRITE(gp, GT96100_ETH_HASH_TBL_PTR, gp->hash_table_dma);
  861. dbg(3, "%s: Hash Tbl Ptr=%xn", __FUNCTION__,
  862.     GT96100ETH_READ(gp, GT96100_ETH_HASH_TBL_PTR));
  863. // Setup Tx
  864. reset_tx(dev);
  865. dbg(3, "%s: Curr Tx Desc Ptr0=%xn", __FUNCTION__,
  866.     GT96100ETH_READ(gp, GT96100_ETH_CURR_TX_DESC_PTR0));
  867. // Setup Rx
  868. reset_rx(dev);
  869. dbg(3, "%s: 1st/Curr Rx Desc Ptr0=%x/%xn", __FUNCTION__,
  870.     GT96100ETH_READ(gp, GT96100_ETH_1ST_RX_DESC_PTR0),
  871.     GT96100ETH_READ(gp, GT96100_ETH_CURR_RX_DESC_PTR0));
  872. // eth port config register
  873. GT96100ETH_WRITE(gp, GT96100_ETH_PORT_CONFIG_EXT,
  874.  pcxrFCTL | pcxrFCTLen | pcxrFLP | pcxrDPLXen);
  875. mii_reg = read_MII(gp->phy_addr, 0x11); /* int enable register */
  876. mii_reg |= 2;  /* enable mii interrupt */
  877. write_MII(gp->phy_addr, 0x11, mii_reg);
  878. dbg(3, "%s: PhyAD=%xn", __FUNCTION__,
  879.     GT96100_READ(GT96100_ETH_PHY_ADDR_REG));
  880. // setup DMA
  881. // We want the Rx/Tx DMA to write/read data to/from memory in
  882. // Big Endian mode. Also set DMA Burst Size to 8 64Bit words.
  883. #ifdef DESC_DATA_BE
  884. GT96100ETH_WRITE(gp, GT96100_ETH_SDMA_CONFIG,
  885.  (0xf<<sdcrRCBit) | sdcrRIFB | (3<<sdcrBSZBit));
  886. #else
  887. GT96100ETH_WRITE(gp, GT96100_ETH_SDMA_CONFIG,
  888.  sdcrBLMR | sdcrBLMT |
  889.  (0xf<<sdcrRCBit) | sdcrRIFB | (3<<sdcrBSZBit));
  890. #endif
  891. dbg(3, "%s: SDMA Config=%xn", __FUNCTION__,
  892.     GT96100ETH_READ(gp, GT96100_ETH_SDMA_CONFIG));
  893. // start Rx DMA
  894. GT96100ETH_WRITE(gp, GT96100_ETH_SDMA_COMM, sdcmrERD);
  895. dbg(3, "%s: SDMA Comm=%xn", __FUNCTION__,
  896.     GT96100ETH_READ(gp, GT96100_ETH_SDMA_COMM));
  897.     
  898. // enable this port (set hash size to 1/2K)
  899. GT96100ETH_WRITE(gp, GT96100_ETH_PORT_CONFIG, pcrEN | pcrHS);
  900. dbg(3, "%s: Port Config=%xn", __FUNCTION__,
  901.     GT96100ETH_READ(gp, GT96100_ETH_PORT_CONFIG));
  902.     
  903. /*
  904.  * Disable all Type-of-Service queueing. All Rx packets will be
  905.  * treated normally and will be sent to the lowest priority
  906.  * queue.
  907.  *
  908.  * Disable flow-control for now. FIXME: support flow control?
  909.  */
  910. // clear all the MIB ctr regs
  911. GT96100ETH_WRITE(gp, GT96100_ETH_PORT_CONFIG_EXT,
  912.  pcxrFCTL | pcxrFCTLen | pcxrFLP |
  913.  pcxrPRIOrxOverride);
  914. read_mib_counters(gp);
  915. GT96100ETH_WRITE(gp, GT96100_ETH_PORT_CONFIG_EXT,
  916.  pcxrFCTL | pcxrFCTLen | pcxrFLP |
  917.  pcxrPRIOrxOverride | pcxrMIBclrMode);
  918.     
  919. dbg(3, "%s: Port Config Ext=%xn", __FUNCTION__,
  920.     GT96100ETH_READ(gp, GT96100_ETH_PORT_CONFIG_EXT));
  921. netif_start_queue(dev);
  922. dump_MII(4, dev);
  923. // enable interrupts
  924. enable_ether_irq(dev);
  925. // we should now be receiving frames
  926. return 0;
  927. }
  928. static int
  929. gt96100_open(struct net_device *dev)
  930. {
  931. int retval;
  932.     
  933. MOD_INC_USE_COUNT;
  934. dbg(2, "%s: dev=%pn", __FUNCTION__, dev);
  935. // Initialize and startup the GT-96100 ethernet port
  936. if ((retval = gt96100_init(dev))) {
  937. err("error in gt96100_initn");
  938. free_irq(dev->irq, dev);
  939. MOD_DEC_USE_COUNT;
  940. return retval;
  941. }
  942. if ((retval = request_irq(dev->irq, &gt96100_interrupt,
  943.   SA_SHIRQ, dev->name, dev))) {
  944. err("unable to get IRQ %dn", dev->irq);
  945. MOD_DEC_USE_COUNT;
  946. return retval;
  947. }
  948. dbg(2, "%s: Initialization done.n", __FUNCTION__);
  949. return 0;
  950. }
  951. static int
  952. gt96100_close(struct net_device *dev)
  953. {
  954. dbg(3, "%s: dev=%pn", __FUNCTION__, dev);
  955. // stop the device
  956. if (netif_device_present(dev)) {
  957. netif_stop_queue(dev);
  958. hard_stop(dev);
  959. }
  960. free_irq(dev->irq, dev);
  961.     
  962. MOD_DEC_USE_COUNT;
  963. return 0;
  964. }
  965. static int
  966. gt96100_tx(struct sk_buff *skb, struct net_device *dev)
  967. {
  968. struct gt96100_private *gp = (struct gt96100_private *)dev->priv;
  969. unsigned long flags;
  970. int nextIn;
  971. spin_lock_irqsave(&gp->lock, flags);
  972. nextIn = gp->tx_next_in;
  973. dbg(3, "%s: nextIn=%dn", __FUNCTION__, nextIn);
  974.     
  975. if (gp->tx_count >= TX_RING_SIZE) {
  976. warn("Tx Ring full, pkt dropped.n");
  977. gp->stats.tx_dropped++;
  978. spin_unlock_irqrestore(&gp->lock, flags);
  979. return 1;
  980. }
  981.     
  982. if (!(gp->last_psr & psrLink)) {
  983. err("%s: Link down, pkt dropped.n", __FUNCTION__);
  984. gp->stats.tx_dropped++;
  985. spin_unlock_irqrestore(&gp->lock, flags);
  986. return 1;
  987. }
  988.     
  989. if (dma32_to_cpu(gp->tx_ring[nextIn].cmdstat) & txOwn) {
  990. err("%s: device owns descriptor, pkt dropped.n", __FUNCTION__);
  991. gp->stats.tx_dropped++;
  992. // stop the queue, so Tx timeout can fix it
  993. netif_stop_queue(dev);
  994. spin_unlock_irqrestore(&gp->lock, flags);
  995. return 1;
  996. }
  997.     
  998. // Prepare the Descriptor at tx_next_in
  999. gp->tx_skbuff[nextIn] = skb;
  1000. gp->tx_ring[nextIn].byte_cnt = cpu_to_dma16(skb->len);
  1001. gp->tx_ring[nextIn].buff_ptr = cpu_to_dma32(virt_to_phys(skb->data));
  1002. // make sure packet gets written back to memory
  1003. dma_cache_wback_inv((unsigned long)(skb->data), skb->len);
  1004. // Give ownership to device, set first and last desc, enable interrupt
  1005. // Setting of ownership bit must be *last*!
  1006. gp->tx_ring[nextIn].cmdstat =
  1007. cpu_to_dma32((u32)(txOwn | txGenCRC | txEI |
  1008.    txPad | txFirst | txLast));
  1009.     
  1010. dump_tx_desc(4, dev, nextIn);
  1011. dump_skb(4, dev, skb);
  1012. // increment tx_next_in with wrap
  1013. gp->tx_next_in = (nextIn + 1) % TX_RING_SIZE;
  1014. // If DMA is stopped, restart
  1015. if (!(GT96100ETH_READ(gp, GT96100_ETH_PORT_STATUS) & psrTxLow))
  1016. GT96100ETH_WRITE(gp, GT96100_ETH_SDMA_COMM,
  1017.  sdcmrERD | sdcmrTXDL);
  1018. // increment count and stop queue if full
  1019. if (++gp->tx_count == TX_RING_SIZE) {
  1020. gp->tx_full = 1;
  1021. netif_stop_queue(dev);
  1022. dbg(2, "Tx Ring now full, queue stopped.n");
  1023. }
  1024.     
  1025. dev->trans_start = jiffies;
  1026. spin_unlock_irqrestore(&gp->lock, flags);
  1027. return 0;
  1028. }
  1029. static int
  1030. gt96100_rx(struct net_device *dev, u32 status)
  1031. {
  1032. struct gt96100_private *gp = (struct gt96100_private *)dev->priv;
  1033. struct sk_buff *skb;
  1034. int pkt_len, nextOut, cdp;
  1035. gt96100_rd_t *rd;
  1036. u32 cmdstat;
  1037.     
  1038. dbg(3, "%s: dev=%p, status=%xn", __FUNCTION__, dev, status);
  1039. cdp = (GT96100ETH_READ(gp, GT96100_ETH_1ST_RX_DESC_PTR0)
  1040.        - gp->rx_ring_dma) / sizeof(gt96100_rd_t);
  1041. // Continue until we reach 1st descriptor pointer
  1042. for (nextOut = gp->rx_next_out; nextOut != cdp;
  1043.      nextOut = (nextOut + 1) % RX_RING_SIZE) {
  1044. if (--gp->intr_work_done == 0)
  1045. break;
  1046. rd = &gp->rx_ring[nextOut];
  1047. cmdstat = dma32_to_cpu(rd->cmdstat);
  1048. dbg(4, "%s: Rx desc cmdstat=%x, nextOut=%dn", __FUNCTION__,
  1049.     cmdstat, nextOut);
  1050. if (cmdstat & (u32)rxOwn) {
  1051. //err(__FUNCTION__ ": device owns descriptor!n");
  1052. // DMA is not finished updating descriptor???
  1053. // Leave and come back later to pick-up where
  1054. // we left off.
  1055. break;
  1056. }
  1057. // Drop this received pkt if there were any errors
  1058. if (((cmdstat & (u32)(rxErrorSummary)) &&
  1059.      (cmdstat & (u32)(rxFirst))) || (status & icrRxError)) {
  1060. // update the detailed rx error counters that
  1061. // are not covered by the MIB counters.
  1062. if (cmdstat & (u32)rxOverrun)
  1063. gp->stats.rx_fifo_errors++;
  1064. cmdstat |= (u32)rxOwn;
  1065. rd->cmdstat = cpu_to_dma32(cmdstat);
  1066. continue;
  1067. }
  1068. /*
  1069.  * Must be first and last (ie only) descriptor of packet. We
  1070.  * ignore (drop) any packets that do not fit in one descriptor.
  1071.  * Every descriptor's receive buffer is large enough to hold
  1072.  * the maximum 802.3 frame size, so a multi-descriptor packet
  1073.  * indicates an error. Most if not all corrupted packets will
  1074.  * have already been dropped by the above check for the
  1075.  * rxErrorSummary status bit.
  1076.  */
  1077. if (!(cmdstat & (u32)rxFirst) || !(cmdstat & (u32)rxLast)) {
  1078. if (cmdstat & (u32)rxFirst) {
  1079. /*
  1080.  * This is the first descriptor of a
  1081.  * multi-descriptor packet. It isn't corrupted
  1082.  * because the above check for rxErrorSummary
  1083.  * would have dropped it already, so what's
  1084.  * the deal with this packet? Good question,
  1085.  * let's dump it out.
  1086.  */
  1087. err("%s: desc not first and last!n", __FUNCTION__);
  1088. dump_rx_desc(0, dev, nextOut);
  1089. }
  1090. cmdstat |= (u32)rxOwn;
  1091. rd->cmdstat = cpu_to_dma32(cmdstat);
  1092. // continue to drop every descriptor of this packet
  1093. continue;
  1094. }
  1095. pkt_len = dma16_to_cpu(rd->byte_cnt);
  1096. /* Create new skb. */
  1097. skb = dev_alloc_skb(pkt_len+2);
  1098. if (skb == NULL) {
  1099. err("%s: Memory squeeze, dropping packet.n", __FUNCTION__);
  1100. gp->stats.rx_dropped++;
  1101. cmdstat |= (u32)rxOwn;
  1102. rd->cmdstat = cpu_to_dma32(cmdstat);
  1103. continue;
  1104. }
  1105. skb->dev = dev;
  1106. skb_reserve(skb, 2);   /* 16 byte IP header align */
  1107. memcpy(skb_put(skb, pkt_len),
  1108.        &gp->rx_buff[nextOut*PKT_BUF_SZ], pkt_len);
  1109. skb->protocol = eth_type_trans(skb, dev);
  1110. dump_skb(4, dev, skb);
  1111. netif_rx(skb);        /* pass the packet to upper layers */
  1112. dev->last_rx = jiffies;
  1113. // now we can release ownership of this desc back to device
  1114. cmdstat |= (u32)rxOwn;
  1115. rd->cmdstat = cpu_to_dma32(cmdstat);
  1116. }
  1117.     
  1118. if (nextOut == gp->rx_next_out)
  1119. dbg(3, "%s: RxCDP did not increment?n", __FUNCTION__);
  1120. gp->rx_next_out = nextOut;
  1121. return 0;
  1122. }
  1123. static void
  1124. gt96100_tx_complete(struct net_device *dev, u32 status)
  1125. {
  1126. struct gt96100_private *gp = (struct gt96100_private *)dev->priv;
  1127. int nextOut, cdp;
  1128. gt96100_td_t *td;
  1129. u32 cmdstat;
  1130. cdp = (GT96100ETH_READ(gp, GT96100_ETH_CURR_TX_DESC_PTR0)
  1131.        - gp->tx_ring_dma) / sizeof(gt96100_td_t);
  1132.     
  1133. // Continue until we reach the current descriptor pointer
  1134. for (nextOut = gp->tx_next_out; nextOut != cdp;
  1135.      nextOut = (nextOut + 1) % TX_RING_SIZE) {
  1136. if (--gp->intr_work_done == 0)
  1137. break;
  1138. td = &gp->tx_ring[nextOut];
  1139. cmdstat = dma32_to_cpu(td->cmdstat);
  1140. dbg(3, "%s: Tx desc cmdstat=%x, nextOut=%dn", __FUNCTION__,
  1141.     cmdstat, nextOut);
  1142. if (cmdstat & (u32)txOwn) {
  1143. //dump_tx_ring(dev);
  1144. // DMA is not finished writing descriptor???
  1145. // Leave and come back later to pick-up where
  1146. // we left off.
  1147. break;
  1148. }
  1149. // increment Tx error stats
  1150. if (cmdstat & (u32)txErrorSummary) {
  1151. dbg(2, "%s: Tx error, cmdstat = %xn", __FUNCTION__,
  1152.     cmdstat);
  1153. gp->stats.tx_errors++;
  1154. if (cmdstat & (u32)txReTxLimit)
  1155. gp->stats.tx_aborted_errors++;
  1156. if (cmdstat & (u32)txUnderrun)
  1157. gp->stats.tx_fifo_errors++;
  1158. if (cmdstat & (u32)txLateCollision)
  1159. gp->stats.tx_window_errors++;
  1160. }
  1161. if (cmdstat & (u32)txCollision)
  1162. gp->stats.collisions +=
  1163. (u32)((cmdstat & txReTxCntMask) >>
  1164.       txReTxCntBit);
  1165. // Wake the queue if the ring was full
  1166. if (gp->tx_full) {
  1167. gp->tx_full = 0;
  1168. if (gp->last_psr & psrLink) {
  1169. netif_wake_queue(dev);
  1170. dbg(2, "%s: Tx Ring was full, queue wakedn", __FUNCTION_);
  1171. }
  1172. }
  1173. // decrement tx ring buffer count
  1174. if (gp->tx_count) gp->tx_count--;
  1175. // free the skb
  1176. if (gp->tx_skbuff[nextOut]) {
  1177. dbg(3, "%s: good Tx, skb=%pn", __FUNCTION__,
  1178.     gp->tx_skbuff[nextOut]);
  1179. dev_kfree_skb_irq(gp->tx_skbuff[nextOut]);
  1180. gp->tx_skbuff[nextOut] = NULL;
  1181. } else {
  1182. err("%s: no skb!n", __FUNCTION__);
  1183. }
  1184. }
  1185. gp->tx_next_out = nextOut;
  1186. if (gt96100_check_tx_consistent(gp)) {
  1187. err("%s: Tx queue inconsistent!n", __FUNCTION__);
  1188. }
  1189.     
  1190. if ((status & icrTxEndLow) && gp->tx_count != 0) {
  1191. // we must restart the DMA
  1192. dbg(3, "%s: Restarting Tx DMAn", __FUNCTION__);
  1193. GT96100ETH_WRITE(gp, GT96100_ETH_SDMA_COMM,
  1194.  sdcmrERD | sdcmrTXDL);
  1195. }
  1196. }
  1197. static void
  1198. gt96100_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  1199. {
  1200. struct net_device *dev = (struct net_device *)dev_id;
  1201. struct gt96100_private *gp = (struct gt96100_private *)dev->priv;
  1202. u32 status;
  1203.     
  1204. if (dev == NULL) {
  1205. err("%s: null dev ptrn", __FUNCTION__);
  1206. return;
  1207. }
  1208. dbg(3, "%s: entry, icr=%xn", __FUNCTION__,
  1209.     GT96100ETH_READ(gp, GT96100_ETH_INT_CAUSE));
  1210. spin_lock(&gp->lock);
  1211. gp->intr_work_done = max_interrupt_work;
  1212. while (gp->intr_work_done > 0) {
  1213. status = GT96100ETH_READ(gp, GT96100_ETH_INT_CAUSE);
  1214. // ACK interrupts
  1215. GT96100ETH_WRITE(gp, GT96100_ETH_INT_CAUSE, ~status);
  1216. if ((status & icrEtherIntSum) == 0 &&
  1217.     !(status & (icrTxBufferLow|icrTxBufferHigh|icrRxBuffer)))
  1218. break;
  1219. if (status & icrMIIPhySTC) {
  1220. u32 psr = GT96100ETH_READ(gp, GT96100_ETH_PORT_STATUS);
  1221. if (gp->last_psr != psr) {
  1222. dbg(0, "port status:n");
  1223. dbg(0, "    %s MBit/s, %s-duplex, "
  1224.     "flow-control %s, link is %s,n",
  1225.     psr & psrSpeed ? "100":"10",
  1226.     psr & psrDuplex ? "full":"half",
  1227.     psr & psrFctl ? "disabled":"enabled",
  1228.     psr & psrLink ? "up":"down");
  1229. dbg(0, "    TxLowQ is %s, TxHighQ is %s, "
  1230.     "Transmitter is %sn",
  1231.     psr & psrTxLow ? "running":"stopped",
  1232.     psr & psrTxHigh ? "running":"stopped",
  1233.     psr & psrTxInProg ? "on":"off");
  1234. if ((psr & psrLink) && !gp->tx_full &&
  1235.     netif_queue_stopped(dev)) {
  1236. dbg(0, ": Link up, waking queue.n",
  1237.     __FUNCTION_);
  1238. netif_wake_queue(dev);
  1239. } else if (!(psr & psrLink) &&
  1240.    !netif_queue_stopped(dev)) {
  1241. dbg(0, "Link down, stopping queue.n",
  1242.     __FUNCTION__);
  1243. netif_stop_queue(dev);
  1244. }
  1245. gp->last_psr = psr;
  1246. }
  1247. if (--gp->intr_work_done == 0)
  1248. break;
  1249. }
  1250. if (status & (icrTxBufferLow | icrTxEndLow))
  1251. gt96100_tx_complete(dev, status);
  1252. if (status & (icrRxBuffer | icrRxError)) {
  1253. gt96100_rx(dev, status);
  1254. }
  1255. // Now check TX errors (RX errors were handled in gt96100_rx)
  1256. if (status & icrTxErrorLow) {
  1257. err("%s: Tx resource errorn", __FUNCTION__);
  1258. if (--gp->intr_work_done == 0)
  1259. break;
  1260. }
  1261. if (status & icrTxUdr) {
  1262. err("%s: Tx underrun errorn", __FUNCTION__);
  1263. if (--gp->intr_work_done == 0)
  1264. break;
  1265. }
  1266. }
  1267. if (gp->intr_work_done == 0) {
  1268. // ACK any remaining pending interrupts
  1269. GT96100ETH_WRITE(gp, GT96100_ETH_INT_CAUSE, 0);
  1270. dbg(3, "%s: hit max workn", __FUNCTION__);
  1271. }
  1272.     
  1273. dbg(3, "%s: exit, icr=%xn", __FUNCTION__,
  1274.     GT96100ETH_READ(gp, GT96100_ETH_INT_CAUSE));
  1275. spin_unlock(&gp->lock);
  1276. }
  1277. static void
  1278. gt96100_tx_timeout(struct net_device *dev)
  1279. {
  1280. struct gt96100_private *gp = (struct gt96100_private *)dev->priv;
  1281. unsigned long flags;
  1282.     
  1283. spin_lock_irqsave(&gp->lock, flags);
  1284.     
  1285. if (!(gp->last_psr & psrLink)) {
  1286. err("tx_timeout: link down.n");
  1287. spin_unlock_irqrestore(&gp->lock, flags);
  1288. } else {
  1289. if (gt96100_check_tx_consistent(gp))
  1290. err("tx_timeout: Tx ring error.n");
  1291. disable_ether_irq(dev);
  1292. spin_unlock_irqrestore(&gp->lock, flags);
  1293. reset_tx(dev);
  1294. enable_ether_irq(dev);
  1295. netif_wake_queue(dev);
  1296. }
  1297. }
  1298. static void
  1299. gt96100_set_rx_mode(struct net_device *dev)
  1300. {
  1301. struct gt96100_private *gp = (struct gt96100_private *)dev->priv;
  1302. unsigned long flags;
  1303. //struct dev_mc_list *mcptr;
  1304.     
  1305. dbg(3, "%s: dev=%p, flags=%xn", __FUNCTION__, dev, dev->flags);
  1306. // stop the Receiver DMA
  1307. abort(dev, sdcmrAR);
  1308. spin_lock_irqsave(&gp->lock, flags);
  1309. if (dev->flags & IFF_PROMISC) {
  1310. GT96100ETH_WRITE(gp, GT96100_ETH_PORT_CONFIG,
  1311.  pcrEN | pcrHS | pcrPM);
  1312. }
  1313. #if 0
  1314. /*
  1315.   FIXME: currently multicast doesn't work - need to get hash table
  1316.   working first.
  1317. */
  1318. if (dev->mc_count) {
  1319. // clear hash table
  1320. memset(gp->hash_table, 0, RX_HASH_TABLE_SIZE);
  1321. // Add our ethernet address
  1322. gt96100_add_hash_entry(dev, dev->dev_addr);
  1323. for (mcptr = dev->mc_list; mcptr; mcptr = mcptr->next) {
  1324. dump_hw_addr(2, dev, __FUNCTION__ ": addr=",
  1325.      mcptr->dmi_addr);
  1326. gt96100_add_hash_entry(dev, mcptr->dmi_addr);
  1327. }
  1328. }
  1329. #endif
  1330.     
  1331. // restart Rx DMA
  1332. GT96100ETH_WRITE(gp, GT96100_ETH_SDMA_COMM, sdcmrERD);
  1333. spin_unlock_irqrestore(&gp->lock, flags);
  1334. }
  1335. static struct net_device_stats *
  1336. gt96100_get_stats(struct net_device *dev)
  1337. {
  1338. struct gt96100_private *gp = (struct gt96100_private *)dev->priv;
  1339. unsigned long flags;
  1340. dbg(3, "%s: dev=%pn", __FUNCTION__, dev);
  1341. if (netif_device_present(dev)) {
  1342. spin_lock_irqsave (&gp->lock, flags);
  1343. update_stats(gp);
  1344. spin_unlock_irqrestore (&gp->lock, flags);
  1345. }
  1346. return &gp->stats;
  1347. }
  1348. static void gt96100_cleanup_module(void)
  1349. {
  1350. int i;
  1351. for (i=0; i<NUM_INTERFACES; i++) {
  1352. struct gt96100_if_t *gtif = &gt96100_iflist[i];
  1353. if (gtif->dev != NULL) {
  1354. struct gt96100_private *gp =
  1355. (struct gt96100_private *)gtif->dev->priv;
  1356. release_region(gtif->iobase, gp->io_size);
  1357. unregister_netdev(gtif->dev);
  1358. if (gtif->dev->priv != NULL)
  1359. kfree (gtif->dev->priv);
  1360. kfree (gtif->dev);
  1361. }
  1362. }
  1363. }
  1364. #ifndef MODULE
  1365. static int __init gt96100_setup(char *options)
  1366. {
  1367. char *this_opt;
  1368. if (!options || !*options)
  1369. return 0;
  1370.         for(this_opt=strtok(options, ",");
  1371.     this_opt; this_opt=strtok(NULL, ",")) {
  1372. if (!strncmp(this_opt, "mac0:", 5)) {
  1373. memcpy(mac0, this_opt+5, 17);
  1374. mac0[17]= '';
  1375. } else if (!strncmp(this_opt, "mac1:", 5)) {
  1376. memcpy(mac1, this_opt+5, 17);
  1377. mac1[17]= '';
  1378. }
  1379. }
  1380. return 1;
  1381. }
  1382. __setup("gt96100eth=", gt96100_setup);
  1383. #endif /* !MODULE */
  1384. module_init(gt96100_init_module);
  1385. module_exit(gt96100_cleanup_module);
  1386. MODULE_AUTHOR("Steve Longerbeam <stevel@mvista.com>");
  1387. MODULE_DESCRIPTION("GT96100 Ethernet driver");