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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* $Id: ethernet.c,v 1.30 2002/05/07 18:50:08 johana Exp $
  2.  *
  3.  * e100net.c: A network driver for the ETRAX 100LX network controller.
  4.  *
  5.  * Copyright (c) 1998-2002 Axis Communications AB.
  6.  *
  7.  * The outline of this driver comes from skeleton.c.
  8.  *
  9.  * $Log: ethernet.c,v $
  10.  * Revision 1.30  2002/05/07 18:50:08  johana
  11.  * Correct spelling in comments.
  12.  *
  13.  * Revision 1.29  2002/05/06 05:38:49  starvik
  14.  * Performance improvements:
  15.  *    Large packets are not copied (breakpoint set to 256 bytes)
  16.  *    The cache bug workaround is delayed until half of the receive list
  17.  *      has been used
  18.  *    Added transmit list
  19.  *    Transmit interrupts are only enabled when transmit queue is full
  20.  *
  21.  * Revision 1.28.2.1  2002/04/30 08:15:51  starvik
  22.  * Performance improvements:
  23.  *   Large packets are not copied (breakpoint set to 256 bytes)
  24.  *   The cache bug workaround is delayed until half of the receive list
  25.  *     has been used.
  26.  *   Added transmit list
  27.  *   Transmit interrupts are only enabled when transmit queue is full
  28.  *
  29.  * Revision 1.28  2002/04/22 11:47:21  johana
  30.  * Fix according to 2.4.19-pre7. time_after/time_before and
  31.  * missing end of comment.
  32.  * The patch has a typo for ethernet.c in e100_clear_network_leds(),
  33.  *  that is fixed here.
  34.  *
  35.  * Revision 1.27  2002/04/12 11:55:11  bjornw
  36.  * Added TODO
  37.  *
  38.  * Revision 1.26  2002/03/15 17:11:02  bjornw
  39.  * Use prepare_rx_descriptor after the CPU has touched the receiving descs
  40.  *
  41.  * Revision 1.25  2002/03/08 13:07:53  bjornw
  42.  * Unnecessary spinlock removed
  43.  *
  44.  * Revision 1.24  2002/02/20 12:57:43  fredriks
  45.  * Replaced MIN() with min().
  46.  *
  47.  * Revision 1.23  2002/02/20 10:58:14  fredriks
  48.  * Strip the Ethernet checksum (4 bytes) before forwarding a frame to upper layers.
  49.  *
  50.  * Revision 1.22  2002/01/30 07:48:22  matsfg
  51.  * Initiate R_NETWORK_TR_CTRL
  52.  *
  53.  * Revision 1.21  2001/11/23 11:54:49  starvik
  54.  * Added IFF_PROMISC and IFF_ALLMULTI handling in set_multicast_list
  55.  * Removed compiler warnings
  56.  *
  57.  * Revision 1.20  2001/11/12 19:26:00  pkj
  58.  * * Corrected e100_negotiate() to not assign half to current_duplex when
  59.  *   it was supposed to compare them...
  60.  * * Cleaned up failure handling in e100_open().
  61.  * * Fixed compiler warnings.
  62.  *
  63.  * Revision 1.19  2001/11/09 07:43:09  starvik
  64.  * Added full duplex support
  65.  * Added ioctl to set speed and duplex
  66.  * Clear LED timer only runs when LED is lit
  67.  *
  68.  * Revision 1.18  2001/10/03 14:40:43  jonashg
  69.  * Update rx_bytes counter.
  70.  *
  71.  * Revision 1.17  2001/06/11 12:43:46  olof
  72.  * Modified defines for network LED behavior
  73.  *
  74.  * Revision 1.16  2001/05/30 06:12:46  markusl
  75.  * TxDesc.next should not be set to NULL
  76.  *
  77.  * Revision 1.15  2001/05/29 10:27:04  markusl
  78.  * Updated after review remarks:
  79.  * +Use IO_EXTRACT
  80.  * +Handle underrun
  81.  *
  82.  * Revision 1.14  2001/05/29 09:20:14  jonashg
  83.  * Use driver name on printk output so one can tell which driver that complains.
  84.  *
  85.  * Revision 1.13  2001/05/09 12:35:59  johana
  86.  * Use DMA_NBR and IRQ_NBR defines from dma.h and irq.h
  87.  *
  88.  * Revision 1.12  2001/04/05 11:43:11  tobiasa
  89.  * Check dev before panic.
  90.  *
  91.  * Revision 1.11  2001/04/04 11:21:05  markusl
  92.  * Updated according to review remarks
  93.  *
  94.  * Revision 1.10  2001/03/26 16:03:06  bjornw
  95.  * Needs linux/config.h
  96.  *
  97.  * Revision 1.9  2001/03/19 14:47:48  pkj
  98.  * * Make sure there is always a pause after the network LEDs are
  99.  *   changed so they will not look constantly lit during heavy traffic.
  100.  * * Always use HZ when setting times relative to jiffies.
  101.  * * Use LED_NETWORK_SET() when setting the network LEDs.
  102.  *
  103.  * Revision 1.8  2001/02/27 13:52:48  bjornw
  104.  * malloc.h -> slab.h
  105.  *
  106.  * Revision 1.7  2001/02/23 13:46:38  bjornw
  107.  * Spellling check
  108.  *
  109.  * Revision 1.6  2001/01/26 15:21:04  starvik
  110.  * Don't disable interrupts while reading MDIO registers (MDIO is slow)
  111.  * Corrected promiscuous mode
  112.  * Improved deallocation of IRQs ("ifconfig eth0 down" now works)
  113.  *
  114.  * Revision 1.5  2000/11/29 17:22:22  bjornw
  115.  * Get rid of the udword types legacy stuff
  116.  *
  117.  * Revision 1.4  2000/11/22 16:36:09  bjornw
  118.  * Please marketing by using the correct case when spelling Etrax.
  119.  *
  120.  * Revision 1.3  2000/11/21 16:43:04  bjornw
  121.  * Minor short->int change
  122.  *
  123.  * Revision 1.2  2000/11/08 14:27:57  bjornw
  124.  * 2.4 port
  125.  *
  126.  * Revision 1.1  2000/11/06 13:56:00  bjornw
  127.  * Verbatim copy of the 1.24 version of e100net.c from elinux
  128.  *
  129.  * Revision 1.24  2000/10/04 15:55:23  bjornw
  130.  * * Use virt_to_phys etc. for DMA addresses
  131.  * * Removed bogus CHECKSUM_UNNECESSARY
  132.  *
  133.  *
  134.  */
  135. #include <linux/config.h>
  136. #include <linux/module.h>
  137. #include <linux/kernel.h>
  138. #include <linux/sched.h>
  139. #include <linux/delay.h>
  140. #include <linux/types.h>
  141. #include <linux/fcntl.h>
  142. #include <linux/interrupt.h>
  143. #include <linux/ptrace.h>
  144. #include <linux/ioport.h>
  145. #include <linux/in.h>
  146. #include <linux/slab.h>
  147. #include <linux/string.h>
  148. #include <linux/spinlock.h>
  149. #include <linux/errno.h>
  150. #include <linux/init.h>
  151. #include <linux/netdevice.h>
  152. #include <linux/etherdevice.h>
  153. #include <linux/skbuff.h>
  154. #include <asm/svinto.h>     /* DMA and register descriptions */
  155. #include <asm/io.h>         /* LED_* I/O functions */
  156. #include <asm/irq.h>
  157. #include <asm/dma.h>
  158. #include <asm/system.h>
  159. #include <asm/bitops.h>
  160. #include <asm/ethernet.h>
  161. //#define ETHDEBUG
  162. #define D(x)
  163. /*
  164.  * The name of the card. Is used for messages and in the requests for
  165.  * io regions, irqs and dma channels
  166.  */
  167. static const char* cardname = "ETRAX 100LX built-in ethernet controller";
  168. /* A default ethernet address. Highlevel SW will set the real one later */
  169. static struct sockaddr default_mac = {
  170. 0,
  171. { 0x00, 0x40, 0x8C, 0xCD, 0x00, 0x00 }
  172. };
  173. /* Information that need to be kept for each board. */
  174. struct net_local {
  175. struct net_device_stats stats;
  176. /* Tx control lock.  This protects the transmit buffer ring
  177.  * state along with the "tx full" state of the driver.  This
  178.  * means all netif_queue flow control actions are protected
  179.  * by this lock as well.
  180.  */
  181. spinlock_t lock;
  182. };
  183. typedef struct etrax_eth_descr
  184. {
  185. etrax_dma_descr descr;
  186. struct sk_buff* skb;
  187. } etrax_eth_descr;
  188. /* Duplex settings */
  189. enum duplex
  190. {
  191. half,
  192. full,
  193. autoneg
  194. };
  195. /* Dma descriptors etc. */
  196. #define MAX_MEDIA_DATA_SIZE 1518
  197. #define MIN_PACKET_LEN      46
  198. #define ETHER_HEAD_LEN      14
  199. /* 
  200. ** MDIO constants.
  201. */
  202. #define MDIO_BASE_STATUS_REG                0x1
  203. #define MDIO_BASE_CONTROL_REG               0x0
  204. #define MDIO_BC_NEGOTIATE                0x0200
  205. #define MDIO_BC_FULL_DUPLEX_MASK         0x0100
  206. #define MDIO_BC_AUTO_NEG_MASK            0x1000
  207. #define MDIO_BC_SPEED_SELECT_MASK        0x2000
  208. #define MDIO_ADVERTISMENT_REG               0x4
  209. #define MDIO_ADVERT_100_FD                0x100
  210. #define MDIO_ADVERT_100_HD                0x080
  211. #define MDIO_ADVERT_10_FD                 0x040
  212. #define MDIO_ADVERT_10_HD                 0x020
  213. #define MDIO_LINK_UP_MASK                   0x4
  214. #define MDIO_START                          0x1
  215. #define MDIO_READ                           0x2
  216. #define MDIO_WRITE                          0x1
  217. #define MDIO_PREAMBLE              0xfffffffful
  218. /* Broadcom specific */
  219. #define MDIO_AUX_CTRL_STATUS_REG           0x18
  220. #define MDIO_FULL_DUPLEX_IND                0x1
  221. #define MDIO_SPEED                          0x2
  222. #define MDIO_PHYS_ADDR                      0x0
  223. /* Network flash constants */
  224. #define NET_FLASH_TIME                  (HZ/50) /* 20 ms */
  225. #define NET_FLASH_PAUSE                (HZ/100) /* 10 ms */
  226. #define NET_LINK_UP_CHECK_INTERVAL       (2*HZ) /* 2 s   */
  227. #define NET_DUPLEX_CHECK_INTERVAL        (2*HZ) /* 2 s   */
  228. #define NO_NETWORK_ACTIVITY 0
  229. #define NETWORK_ACTIVITY    1
  230. #define NBR_OF_RX_DESC     64
  231. #define NBR_OF_TX_DESC     256
  232. /* Large packets are sent directly to upper layers while small packets are */
  233. /* copied (to reduce memory waste). The following constant decides the breakpoint */
  234. #define RX_COPYBREAK 256
  235. /* Due to a chip bug we need to flush the cache when descriptors are returned */
  236. /* to the DMA. To decrease performance impact we return descriptors in chunks. */
  237. /* The following constant determines the number of descriptors to return. */
  238. #define RX_QUEUE_THRESHOLD  NBR_OF_RX_DESC/2
  239. #define GET_BIT(bit,val)   (((val) >> (bit)) & 0x01)
  240. /* Define some macros to access ETRAX 100 registers */
  241. #define SETF(var, reg, field, val) var = (var & ~IO_MASK(##reg##, field)) | 
  242.   IO_FIELD(##reg##, field, val)
  243. #define SETS(var, reg, field, val) var = (var & ~IO_MASK(##reg##, field)) | 
  244.   IO_STATE(##reg##, field, val)
  245. static etrax_eth_descr *myNextRxDesc;  /* Points to the next descriptor to
  246.                                           to be processed */
  247. static etrax_eth_descr *myLastRxDesc;  /* The last processed descriptor */
  248. static etrax_eth_descr *myPrevRxDesc;  /* The descriptor right before myNextRxDesc */
  249. static etrax_eth_descr RxDescList[NBR_OF_RX_DESC] __attribute__ ((aligned(32)));
  250. static etrax_eth_descr* myFirstTxDesc; /* First packet not yet sent */
  251. static etrax_eth_descr* myLastTxDesc;  /* End of send queue */
  252. static etrax_eth_descr* myNextTxDesc;  /* Next descriptor to use */
  253. static etrax_eth_descr TxDescList[NBR_OF_TX_DESC] __attribute__ ((aligned(32)));
  254. static unsigned int network_rec_config_shadow = 0;
  255. /* Network speed indication. */
  256. static struct timer_list speed_timer;
  257. static struct timer_list clear_led_timer;
  258. static int current_speed; /* Speed read from tranceiver */
  259. static int current_speed_selection; /* Speed selected by user */
  260. static int led_next_time;
  261. static int led_active;
  262. static int rx_queue_len;
  263. /* Duplex */
  264. static struct timer_list duplex_timer;
  265. static int full_duplex;
  266. static enum duplex current_duplex;
  267. /* Index to functions, as function prototypes. */
  268. static int etrax_ethernet_init(struct net_device *dev);
  269. static int e100_open(struct net_device *dev);
  270. static int e100_set_mac_address(struct net_device *dev, void *addr);
  271. static int e100_send_packet(struct sk_buff *skb, struct net_device *dev);
  272. static void e100rx_interrupt(int irq, void *dev_id, struct pt_regs *regs);
  273. static void e100tx_interrupt(int irq, void *dev_id, struct pt_regs *regs);
  274. static void e100nw_interrupt(int irq, void *dev_id, struct pt_regs *regs);
  275. static void e100_rx(struct net_device *dev);
  276. static int e100_close(struct net_device *dev);
  277. static int e100_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
  278. static void e100_tx_timeout(struct net_device *dev);
  279. static struct net_device_stats *e100_get_stats(struct net_device *dev);
  280. static void set_multicast_list(struct net_device *dev);
  281. static void e100_hardware_send_packet(char *buf, int length);
  282. static void update_rx_stats(struct net_device_stats *);
  283. static void update_tx_stats(struct net_device_stats *);
  284. static void e100_check_speed(unsigned long dummy);
  285. static void e100_set_speed(unsigned long speed);
  286. static void e100_check_duplex(unsigned long dummy);
  287. static void e100_set_duplex(enum duplex);
  288. static void e100_negotiate(void);
  289. static unsigned short e100_get_mdio_reg(unsigned char reg_num);
  290. static void e100_send_mdio_cmd(unsigned short cmd, int write_cmd);
  291. static void e100_send_mdio_bit(unsigned char bit);
  292. static unsigned char e100_receive_mdio_bit(void);
  293. static void e100_reset_tranceiver(void);
  294. static void e100_clear_network_leds(unsigned long dummy);
  295. static void e100_set_network_leds(int active);
  296. #define tx_done(dev) (*R_DMA_CH0_CMD == 0)
  297. /*
  298.  * Check for a network adaptor of this type, and return '0' if one exists.
  299.  * If dev->base_addr == 0, probe all likely locations.
  300.  * If dev->base_addr == 1, always return failure.
  301.  * If dev->base_addr == 2, allocate space for the device and return success
  302.  * (detachable devices only).
  303.  */
  304. static int __init
  305. etrax_ethernet_init(struct net_device *dev)
  306. {
  307. int i;
  308. printk("ETRAX 100LX 10/100MBit ethernet v2.0 (c) 2000-2001 Axis Communications ABn");
  309. dev->base_addr = (unsigned int)R_NETWORK_SA_0; /* just to have something to show */
  310. printk("%s initializedn", dev->name);
  311. /* make Linux aware of the new hardware  */
  312. if (!dev) {
  313. printk(KERN_WARNING "%s: dev == NULL. Should this happen?n",
  314.        cardname);
  315. dev = init_etherdev(dev, sizeof(struct net_local));
  316. if (!dev)
  317. panic("init_etherdev failedn");
  318. }
  319. /* setup generic handlers and stuff in the dev struct */
  320. ether_setup(dev);
  321. /* make room for the local structure containing stats etc */
  322. dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
  323. if (dev->priv == NULL)
  324. return -ENOMEM;
  325. memset(dev->priv, 0, sizeof(struct net_local));
  326. /* now setup our etrax specific stuff */
  327. dev->irq = NETWORK_DMA_RX_IRQ_NBR; /* we really use DMATX as well... */
  328. dev->dma = NETWORK_RX_DMA_NBR;
  329. /* fill in our handlers so the network layer can talk to us in the future */
  330. dev->open               = e100_open;
  331. dev->hard_start_xmit    = e100_send_packet;
  332. dev->stop               = e100_close;
  333. dev->get_stats          = e100_get_stats;
  334. dev->set_multicast_list = set_multicast_list;
  335. dev->set_mac_address    = e100_set_mac_address;
  336. dev->do_ioctl           = e100_ioctl;
  337. dev->tx_timeout         = e100_tx_timeout;
  338. /* set the default MAC address */
  339. e100_set_mac_address(dev, &default_mac);
  340. /* Initialise the list of Etrax DMA-descriptors */
  341. /* Initialise receive descriptors */
  342. for (i = 0; i < NBR_OF_RX_DESC; i++) {
  343. RxDescList[i].skb = dev_alloc_skb(MAX_MEDIA_DATA_SIZE);
  344. RxDescList[i].descr.ctrl   = 0;
  345. RxDescList[i].descr.sw_len = MAX_MEDIA_DATA_SIZE;
  346. RxDescList[i].descr.next   = virt_to_phys(&RxDescList[i + 1]);
  347. RxDescList[i].descr.buf    = virt_to_phys(RxDescList[i].skb->data);
  348. RxDescList[i].descr.status = 0;
  349. RxDescList[i].descr.hw_len = 0;
  350.              
  351. prepare_rx_descriptor(&RxDescList[i].descr);
  352. }
  353. RxDescList[NBR_OF_RX_DESC - 1].descr.ctrl   = d_eol;
  354. RxDescList[NBR_OF_RX_DESC - 1].descr.next   = virt_to_phys(&RxDescList[0]);
  355. rx_queue_len = 0;
  356. /* Initialize transmit descriptors */
  357. for (i = 0; i < NBR_OF_TX_DESC; i++) {
  358. TxDescList[i].descr.ctrl   = 0;
  359. TxDescList[i].descr.sw_len = 0;
  360. TxDescList[i].descr.next   = virt_to_phys(&TxDescList[i + 1].descr);
  361. TxDescList[i].descr.buf    = 0;
  362. TxDescList[i].descr.status = 0;
  363. TxDescList[i].descr.hw_len = 0;
  364. TxDescList[i].skb = 0;
  365. }
  366. TxDescList[NBR_OF_TX_DESC - 1].descr.ctrl   = d_eol;
  367. TxDescList[NBR_OF_TX_DESC - 1].descr.next   = virt_to_phys(&TxDescList[0].descr);
  368.         
  369. /* Initialise initial pointers */
  370. myNextRxDesc  = &RxDescList[0];
  371. myLastRxDesc  = &RxDescList[NBR_OF_RX_DESC - 1];
  372. myPrevRxDesc  = &RxDescList[NBR_OF_RX_DESC - 1];
  373. myFirstTxDesc = &TxDescList[0];
  374. myNextTxDesc  = &TxDescList[0];
  375. myLastTxDesc  = &TxDescList[NBR_OF_TX_DESC - 1];
  376. /* Initialize speed indicator stuff. */
  377. current_speed = 10;
  378. current_speed_selection = 0; /* Auto */
  379. speed_timer.expires = jiffies + NET_LINK_UP_CHECK_INTERVAL;
  380. speed_timer.function = e100_check_speed;
  381. add_timer(&speed_timer);
  382.         
  383. clear_led_timer.function = e100_clear_network_leds;
  384.         
  385. full_duplex = 0;
  386. current_duplex = autoneg;
  387. duplex_timer.expires = jiffies + NET_DUPLEX_CHECK_INTERVAL;
  388. duplex_timer.function = e100_check_duplex;
  389. add_timer(&duplex_timer);
  390. return 0;
  391. }
  392. /* set MAC address of the interface. called from the core after a
  393.  * SIOCSIFADDR ioctl, and from the bootup above.
  394.  */
  395. static int
  396. e100_set_mac_address(struct net_device *dev, void *p)
  397. {
  398. struct sockaddr *addr = p;
  399. int i;
  400. /* remember it */
  401. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  402. /* Write it to the hardware.
  403.  * Note the way the address is wrapped:
  404.  * *R_NETWORK_SA_0 = a0_0 | (a0_1 << 8) | (a0_2 << 16) | (a0_3 << 24);
  405.  * *R_NETWORK_SA_1 = a0_4 | (a0_5 << 8);
  406.  */
  407. *R_NETWORK_SA_0 = dev->dev_addr[0] | (dev->dev_addr[1] << 8) |
  408. (dev->dev_addr[2] << 16) | (dev->dev_addr[3] << 24);
  409. *R_NETWORK_SA_1 = dev->dev_addr[4] | (dev->dev_addr[5] << 8);
  410. *R_NETWORK_SA_2 = 0;
  411. /* show it in the log as well */
  412. printk("%s: changed MAC to ", dev->name);
  413. for (i = 0; i < 5; i++)
  414. printk("%02X:", dev->dev_addr[i]);
  415. printk("%02Xn", dev->dev_addr[i]);
  416. return 0;
  417. }
  418. /*
  419.  * Open/initialize the board. This is called (in the current kernel)
  420.  * sometime after booting when the 'ifconfig' program is run.
  421.  *
  422.  * This routine should set everything up anew at each open, even
  423.  * registers that "should" only need to be set once at boot, so that
  424.  * there is non-reboot way to recover if something goes wrong.
  425.  */
  426. static int
  427. e100_open(struct net_device *dev)
  428. {
  429. unsigned long flags;
  430. /* disable the ethernet interface while we configure it */
  431. *R_NETWORK_GEN_CONFIG =
  432. IO_STATE(R_NETWORK_GEN_CONFIG, phy,    mii_clk) |
  433. IO_STATE(R_NETWORK_GEN_CONFIG, enable, off);
  434. /* enable the MDIO output pin */
  435. *R_NETWORK_MGM_CTRL = IO_STATE(R_NETWORK_MGM_CTRL, mdoe, enable);
  436. *R_IRQ_MASK0_CLR =
  437. IO_STATE(R_IRQ_MASK0_CLR, overrun, clr) |
  438. IO_STATE(R_IRQ_MASK0_CLR, underrun, clr) |
  439. IO_STATE(R_IRQ_MASK0_CLR, excessive_col, clr);
  440. /* clear dma0 and 1 eop and descr irq masks */
  441. *R_IRQ_MASK2_CLR =
  442. IO_STATE(R_IRQ_MASK2_CLR, dma0_descr, clr) |
  443. IO_STATE(R_IRQ_MASK2_CLR, dma0_eop, clr) |
  444. IO_STATE(R_IRQ_MASK2_CLR, dma1_descr, clr) |
  445. IO_STATE(R_IRQ_MASK2_CLR, dma1_eop, clr);
  446. /* Reset and wait for the DMA channels */
  447. RESET_DMA(NETWORK_TX_DMA_NBR);
  448. RESET_DMA(NETWORK_RX_DMA_NBR);
  449. WAIT_DMA(NETWORK_TX_DMA_NBR);
  450. WAIT_DMA(NETWORK_RX_DMA_NBR);
  451. /* Initialise the etrax network controller */
  452. /* allocate the irq corresponding to the receiving DMA */
  453. if (request_irq(NETWORK_DMA_RX_IRQ_NBR, e100rx_interrupt, 0,
  454. cardname, (void *)dev)) {
  455. goto grace_exit0;
  456. }
  457. /* allocate the irq corresponding to the transmitting DMA */
  458. if (request_irq(NETWORK_DMA_TX_IRQ_NBR, e100tx_interrupt, 0,
  459. cardname, (void *)dev)) {
  460. goto grace_exit1;
  461. }
  462. /* allocate the irq corresponding to the network errors etc */
  463. if (request_irq(NETWORK_STATUS_IRQ_NBR, e100nw_interrupt, 0,
  464. cardname, (void *)dev)) {
  465. goto grace_exit2;
  466. }
  467. /*
  468.  * Always allocate the DMA channels after the IRQ,
  469.  * and clean up on failure.
  470.  */
  471. if (request_dma(NETWORK_TX_DMA_NBR, cardname)) {
  472. goto grace_exit3;
  473. }
  474. if (request_dma(NETWORK_RX_DMA_NBR, cardname)) {
  475. goto grace_exit4;
  476. }
  477. /* give the HW an idea of what MAC address we want */
  478. *R_NETWORK_SA_0 = dev->dev_addr[0] | (dev->dev_addr[1] << 8) |
  479. (dev->dev_addr[2] << 16) | (dev->dev_addr[3] << 24);
  480. *R_NETWORK_SA_1 = dev->dev_addr[4] | (dev->dev_addr[5] << 8);
  481. *R_NETWORK_SA_2 = 0;
  482. #if 0
  483. /* use promiscuous mode for testing */
  484. *R_NETWORK_GA_0 = 0xffffffff;
  485. *R_NETWORK_GA_1 = 0xffffffff;
  486. *R_NETWORK_REC_CONFIG = 0xd; /* broadcast rec, individ. rec, ma0 enabled */
  487. #else
  488. SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, broadcast, receive);
  489. SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, ma0, enable);
  490. SETF(network_rec_config_shadow, R_NETWORK_REC_CONFIG, duplex, full_duplex);
  491. *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
  492. #endif
  493. *R_NETWORK_GEN_CONFIG =
  494. IO_STATE(R_NETWORK_GEN_CONFIG, phy,    mii_clk) |
  495. IO_STATE(R_NETWORK_GEN_CONFIG, enable, on);
  496. *R_NETWORK_TR_CTRL = 
  497. IO_STATE(R_NETWORK_TR_CTRL, clr_error, clr) |
  498. IO_STATE(R_NETWORK_TR_CTRL, delay, none) |
  499. IO_STATE(R_NETWORK_TR_CTRL, cancel, dont) |
  500. IO_STATE(R_NETWORK_TR_CTRL, cd, enable) |
  501. IO_STATE(R_NETWORK_TR_CTRL, retry, enable) |
  502. IO_STATE(R_NETWORK_TR_CTRL, pad, enable) |
  503. IO_STATE(R_NETWORK_TR_CTRL, crc, enable);
  504. save_flags(flags);
  505. cli();
  506. /* enable the irq's for ethernet DMA */
  507. *R_IRQ_MASK2_SET =
  508. IO_STATE(R_IRQ_MASK2_SET, dma1_eop, set); 
  509. *R_IRQ_MASK0_SET =
  510. IO_STATE(R_IRQ_MASK0_SET, overrun,       set) |
  511. IO_STATE(R_IRQ_MASK0_SET, underrun,      set) |
  512. IO_STATE(R_IRQ_MASK0_SET, excessive_col, set);
  513. /* make sure the irqs are cleared */
  514. *R_DMA_CH0_CLR_INTR = IO_STATE(R_DMA_CH0_CLR_INTR, clr_eop, do);
  515. *R_DMA_CH1_CLR_INTR = IO_STATE(R_DMA_CH1_CLR_INTR, clr_eop, do);
  516. /* make sure the rec and transmit error counters are cleared */
  517. (void)*R_REC_COUNTERS;  /* dummy read */
  518. (void)*R_TR_COUNTERS;   /* dummy read */
  519. /* start the receiving DMA channel so we can receive packets from now on */
  520. *R_DMA_CH1_FIRST = virt_to_phys(myNextRxDesc);
  521. *R_DMA_CH1_CMD = IO_STATE(R_DMA_CH1_CMD, cmd, start);
  522. /* Set up transmit DMA channel so it can be restarted later */
  523.         
  524. *R_DMA_CH0_FIRST = 0;
  525. *R_DMA_CH0_DESCR = virt_to_phys(myLastTxDesc);
  526. restore_flags(flags);
  527. /* We are now ready to accept transmit requeusts from
  528.  * the queueing layer of the networking.
  529.  */
  530. netif_start_queue(dev);
  531. return 0;
  532. grace_exit4:
  533. free_dma(NETWORK_TX_DMA_NBR);
  534. grace_exit3:
  535. free_irq(NETWORK_STATUS_IRQ_NBR, (void *)dev);
  536. grace_exit2:
  537. free_irq(NETWORK_DMA_TX_IRQ_NBR, (void *)dev);
  538. grace_exit1:
  539. free_irq(NETWORK_DMA_RX_IRQ_NBR, (void *)dev);
  540. grace_exit0:
  541. return -EAGAIN;
  542. }
  543. static void
  544. e100_check_speed(unsigned long dummy)
  545. {
  546. unsigned long data;
  547. int old_speed = current_speed;
  548. data = e100_get_mdio_reg(MDIO_BASE_STATUS_REG);
  549. if (!(data & MDIO_LINK_UP_MASK)) {
  550. current_speed = 0;
  551. } else {
  552. data = e100_get_mdio_reg(MDIO_AUX_CTRL_STATUS_REG);
  553. current_speed = (data & MDIO_SPEED ? 100 : 10);
  554. }
  555. if (old_speed != current_speed)
  556. e100_set_network_leds(NO_NETWORK_ACTIVITY);
  557. /* Reinitialize the timer. */
  558. speed_timer.expires = jiffies + NET_LINK_UP_CHECK_INTERVAL;
  559. add_timer(&speed_timer);
  560. }
  561. static void
  562. e100_negotiate(void)
  563. {
  564. unsigned short cmd;
  565. unsigned short data = e100_get_mdio_reg(MDIO_ADVERTISMENT_REG);
  566. int bitCounter;
  567. /* Discard old speed and duplex settings */
  568. data &= ~(MDIO_ADVERT_100_HD | MDIO_ADVERT_100_FD | 
  569.           MDIO_ADVERT_10_FD | MDIO_ADVERT_10_HD);
  570.   
  571. switch (current_speed_selection) {
  572. case 10 :
  573. if (current_duplex == full)
  574. data |= MDIO_ADVERT_10_FD;
  575. else if (current_duplex == half)
  576. data |= MDIO_ADVERT_10_HD;
  577. else
  578. data |= MDIO_ADVERT_10_HD |  MDIO_ADVERT_10_FD;
  579. break;
  580. case 100 :
  581.  if (current_duplex == full)
  582. data |= MDIO_ADVERT_100_FD;
  583. else if (current_duplex == half)
  584. data |= MDIO_ADVERT_100_HD;
  585. else
  586. data |= MDIO_ADVERT_100_HD |  MDIO_ADVERT_100_FD;
  587. break;
  588. case 0 : /* Auto */
  589.  if (current_duplex == full)
  590. data |= MDIO_ADVERT_100_FD | MDIO_ADVERT_10_FD;
  591. else if (current_duplex == half)
  592. data |= MDIO_ADVERT_100_HD | MDIO_ADVERT_10_HD;
  593. else
  594. data |= MDIO_ADVERT_100_HD | MDIO_ADVERT_100_FD | MDIO_ADVERT_10_FD | MDIO_ADVERT_10_HD;
  595. break;
  596. default : /* assume autoneg speed and duplex */
  597. data |= MDIO_ADVERT_100_HD | MDIO_ADVERT_100_FD | 
  598.         MDIO_ADVERT_10_FD | MDIO_ADVERT_10_HD;
  599. }
  600. cmd = (MDIO_START << 14) | (MDIO_WRITE << 12) | (MDIO_PHYS_ADDR << 7) |
  601.       (MDIO_ADVERTISMENT_REG<< 2);
  602. e100_send_mdio_cmd(cmd, 1);
  603. /* Data... */
  604. for (bitCounter=15; bitCounter>=0 ; bitCounter--) {
  605. e100_send_mdio_bit(GET_BIT(bitCounter, data));
  606. }
  607. /* Renegotiate with link partner */
  608. data = e100_get_mdio_reg(MDIO_BASE_CONTROL_REG);
  609. data |= MDIO_BC_NEGOTIATE;
  610. cmd = (MDIO_START << 14) | (MDIO_WRITE << 12) | (MDIO_PHYS_ADDR << 7) |
  611.       (MDIO_BASE_CONTROL_REG<< 2);
  612. e100_send_mdio_cmd(cmd, 1);
  613. /* Data... */
  614. for (bitCounter=15; bitCounter>=0 ; bitCounter--) {
  615. e100_send_mdio_bit(GET_BIT(bitCounter, data));
  616. }  
  617. }
  618. static void
  619. e100_set_speed(unsigned long speed)
  620. {
  621. current_speed_selection = speed;
  622. e100_negotiate();
  623. }
  624. static void
  625. e100_check_duplex(unsigned long dummy)
  626. {
  627. unsigned long data;
  628. data = e100_get_mdio_reg(MDIO_AUX_CTRL_STATUS_REG);
  629.         
  630. if (data & MDIO_FULL_DUPLEX_IND) {
  631. if (!full_duplex) { /* Duplex changed to full? */
  632. full_duplex = 1;
  633. SETF(network_rec_config_shadow, R_NETWORK_REC_CONFIG, duplex, full_duplex);
  634. *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
  635. }
  636. } else { /* half */
  637. if (full_duplex) { /* Duplex changed to half? */
  638. full_duplex = 0;
  639. SETF(network_rec_config_shadow, R_NETWORK_REC_CONFIG, duplex, full_duplex);
  640. *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
  641. }
  642. }
  643. /* Reinitialize the timer. */
  644. duplex_timer.expires = jiffies + NET_DUPLEX_CHECK_INTERVAL;
  645. add_timer(&duplex_timer);
  646. }
  647. static void 
  648. e100_set_duplex(enum duplex new_duplex)
  649. {
  650. current_duplex = new_duplex;
  651. e100_negotiate();
  652. }
  653. static unsigned short
  654. e100_get_mdio_reg(unsigned char reg_num)
  655. {
  656. unsigned short cmd;    /* Data to be sent on MDIO port */
  657. unsigned short data;   /* Data read from MDIO */
  658. int bitCounter;
  659. /* Start of frame, OP Code, Physical Address, Register Address */
  660. cmd = (MDIO_START << 14) | (MDIO_READ << 12) | (MDIO_PHYS_ADDR << 7) |
  661. (reg_num << 2);
  662. e100_send_mdio_cmd(cmd, 0);
  663. data = 0;
  664. /* Data... */
  665. for (bitCounter=15; bitCounter>=0 ; bitCounter--) {
  666. data |= (e100_receive_mdio_bit() << bitCounter);
  667. }
  668. return data;
  669. }
  670. static void
  671. e100_send_mdio_cmd(unsigned short cmd, int write_cmd)
  672. {
  673. int bitCounter;
  674. unsigned char data = 0x2;
  675. /* Preamble */
  676. for (bitCounter = 31; bitCounter>= 0; bitCounter--)
  677. e100_send_mdio_bit(GET_BIT(bitCounter, MDIO_PREAMBLE));
  678. for (bitCounter = 15; bitCounter >= 2; bitCounter--)
  679. e100_send_mdio_bit(GET_BIT(bitCounter, cmd));
  680. /* Turnaround */
  681. for (bitCounter = 1; bitCounter >= 0 ; bitCounter--)
  682. if (write_cmd)
  683. e100_send_mdio_bit(GET_BIT(bitCounter, data));
  684. else
  685. e100_receive_mdio_bit();
  686. }
  687. static void
  688. e100_send_mdio_bit(unsigned char bit)
  689. {
  690. *R_NETWORK_MGM_CTRL =
  691. IO_STATE(R_NETWORK_MGM_CTRL, mdoe, enable) |
  692. IO_FIELD(R_NETWORK_MGM_CTRL, mdio, bit);
  693. udelay(1);
  694. *R_NETWORK_MGM_CTRL =
  695. IO_STATE(R_NETWORK_MGM_CTRL, mdoe, enable) |
  696. IO_MASK(R_NETWORK_MGM_CTRL, mdck) |
  697. IO_FIELD(R_NETWORK_MGM_CTRL, mdio, bit);
  698. udelay(1);
  699. }
  700. static unsigned char
  701. e100_receive_mdio_bit()
  702. {
  703. unsigned char bit;
  704. *R_NETWORK_MGM_CTRL = 0;
  705. bit = IO_EXTRACT(R_NETWORK_STAT, mdio, *R_NETWORK_STAT);
  706. udelay(1);
  707. *R_NETWORK_MGM_CTRL = IO_MASK(R_NETWORK_MGM_CTRL, mdck);
  708. udelay(1);
  709. return bit;
  710. }
  711. static void 
  712. e100_reset_tranceiver(void)
  713. {
  714. unsigned short cmd;
  715. unsigned short data;
  716. int bitCounter;
  717. data = e100_get_mdio_reg(MDIO_BASE_CONTROL_REG);
  718. cmd = (MDIO_START << 14) | (MDIO_WRITE << 12) | (MDIO_PHYS_ADDR << 7) | (MDIO_BASE_CONTROL_REG << 2);
  719. e100_send_mdio_cmd(cmd, 1);
  720. data |= 0x8000;
  721. for (bitCounter = 15; bitCounter >= 0 ; bitCounter--) {
  722. e100_send_mdio_bit(GET_BIT(bitCounter, data));
  723. }
  724. }
  725. /* Called by upper layers if they decide it took too long to complete
  726.  * sending a packet - we need to reset and stuff.
  727.  */
  728. static void
  729. e100_tx_timeout(struct net_device *dev)
  730. {
  731. struct net_local *np = (struct net_local *)dev->priv;
  732. printk(KERN_WARNING "%s: transmit timed out, %s?n", dev->name,
  733.        tx_done(dev) ? "IRQ problem" : "network cable problem");
  734. /* remember we got an error */
  735. np->stats.tx_errors++; 
  736. /* reset the TX DMA in case it has hung on something */
  737. RESET_DMA(NETWORK_TX_DMA_NBR);
  738. WAIT_DMA(NETWORK_TX_DMA_NBR);
  739. /* Reset the tranceiver. */
  740. e100_reset_tranceiver();
  741. /* and get rid of the packets that never got an interrupt */
  742. while (myFirstTxDesc != myNextTxDesc)
  743. {
  744. dev_kfree_skb(myFirstTxDesc->skb);
  745. myFirstTxDesc->skb = 0;
  746. myFirstTxDesc = phys_to_virt(myFirstTxDesc->descr.next);
  747. }
  748. /* Set up transmit DMA channel so it can be restarted later */
  749. *R_DMA_CH0_FIRST = 0;
  750. *R_DMA_CH0_DESCR = virt_to_phys(myLastTxDesc);
  751. /* tell the upper layers we're ok again */
  752. netif_wake_queue(dev);
  753. }
  754. /* This will only be invoked if the driver is _not_ in XOFF state.
  755.  * What this means is that we need not check it, and that this
  756.  * invariant will hold if we make sure that the netif_*_queue()
  757.  * calls are done at the proper times.
  758.  */
  759. static int
  760. e100_send_packet(struct sk_buff *skb, struct net_device *dev)
  761. {
  762. struct net_local *np = (struct net_local *)dev->priv;
  763. int length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
  764. unsigned char *buf = skb->data;
  765. #ifdef ETHDEBUG
  766. printk("send packet len %dn", length);
  767. #endif
  768. spin_lock_irq(&np->lock);  /* protect from tx_interrupt and ourself */
  769. myNextTxDesc->skb = skb;
  770. dev->trans_start = jiffies;
  771. e100_hardware_send_packet(buf, length);
  772. myNextTxDesc = phys_to_virt(myNextTxDesc->descr.next);
  773. /* Stop queue if full */
  774. if (myNextTxDesc == myFirstTxDesc) {
  775. /* Enable transmit interrupt to wake up queue */
  776. *R_DMA_CH0_CLR_INTR = IO_STATE(R_DMA_CH0_CLR_INTR, clr_eop, do);
  777. *R_IRQ_MASK2_SET = IO_STATE(R_IRQ_MASK2_SET, dma0_eop, set);
  778. netif_stop_queue(dev);
  779. }
  780. else {
  781.   /* Report any packets that have been sent */
  782. while (myFirstTxDesc != phys_to_virt(*R_DMA_CH0_FIRST) &&
  783.        myFirstTxDesc != myNextTxDesc)
  784. {
  785. np->stats.tx_bytes += myFirstTxDesc->skb->len;
  786. np->stats.tx_packets++;
  787. /* dma is ready with the transmission of the data in tx_skb, so now
  788.    we can release the skb memory */
  789. dev_kfree_skb(myFirstTxDesc->skb);
  790. myFirstTxDesc->skb = 0;
  791. myFirstTxDesc = phys_to_virt(myFirstTxDesc->descr.next);
  792. }
  793. }
  794. spin_unlock_irq(&np->lock);
  795. return 0;
  796. }
  797. /*
  798.  * The typical workload of the driver:
  799.  *   Handle the network interface interrupts.
  800.  */
  801. static void
  802. e100rx_interrupt(int irq, void *dev_id, struct pt_regs * regs)
  803. {
  804. struct net_device *dev = (struct net_device *)dev_id;
  805. unsigned long irqbits = *R_IRQ_MASK2_RD;
  806.  
  807. if (irqbits & IO_STATE(R_IRQ_MASK2_RD, dma1_eop, active)) {
  808. /* acknowledge the eop interrupt */
  809. *R_DMA_CH1_CLR_INTR = IO_STATE(R_DMA_CH1_CLR_INTR, clr_eop, do);
  810. /* check if one or more complete packets were indeed received */
  811. while (*R_DMA_CH1_FIRST != virt_to_phys(myNextRxDesc)) {
  812. /* Take out the buffer and give it to the OS, then
  813.  * allocate a new buffer to put a packet in.
  814.  */
  815. e100_rx(dev);
  816. ((struct net_local *)dev->priv)->stats.rx_packets++;
  817. /* restart/continue on the channel, for safety */
  818. *R_DMA_CH1_CMD = IO_STATE(R_DMA_CH1_CMD, cmd, restart);
  819. /* clear dma channel 1 eop/descr irq bits */
  820. *R_DMA_CH1_CLR_INTR =
  821. IO_STATE(R_DMA_CH1_CLR_INTR, clr_eop, do) |
  822. IO_STATE(R_DMA_CH1_CLR_INTR, clr_descr, do);
  823. /* now, we might have gotten another packet
  824.    so we have to loop back and check if so */
  825. }
  826. }
  827. }
  828. /* the transmit dma channel interrupt
  829.  *
  830.  * this is supposed to free the skbuff which was pending during transmission,
  831.  * and inform the kernel that we can send one more buffer
  832.  */
  833. static void
  834. e100tx_interrupt(int irq, void *dev_id, struct pt_regs * regs)
  835. {
  836. struct net_device *dev = (struct net_device *)dev_id;
  837. unsigned long irqbits = *R_IRQ_MASK2_RD;
  838. struct net_local *np = (struct net_local *)dev->priv;
  839. /* check for a dma0_eop interrupt */
  840. if (irqbits & IO_STATE(R_IRQ_MASK2_RD, dma0_eop, active)) {
  841. /* Report all sent packets */
  842. do {
  843. /* acknowledge the eop interrupt */
  844. *R_DMA_CH0_CLR_INTR = IO_STATE(R_DMA_CH0_CLR_INTR, clr_eop, do);
  845. np->stats.tx_bytes += myFirstTxDesc->skb->len;
  846. np->stats.tx_packets++;
  847. /* dma is ready with the transmission of the data in tx_skb, so now
  848.    we can release the skb memory */
  849. dev_kfree_skb_irq(myFirstTxDesc->skb);
  850. myFirstTxDesc->skb = 0;
  851. if (netif_queue_stopped(dev)) {
  852.    /* Queue is running, disable tx IRQ */
  853.    *R_IRQ_MASK2_CLR = IO_STATE(R_IRQ_MASK2_CLR, dma0_eop, clr);
  854. netif_wake_queue(dev);
  855. }
  856. myFirstTxDesc = phys_to_virt(myFirstTxDesc->descr.next);
  857. } while (myFirstTxDesc != phys_to_virt(*R_DMA_CH0_FIRST) &&
  858.          myFirstTxDesc != myNextTxDesc);
  859. }
  860. }
  861. static void
  862. e100nw_interrupt(int irq, void *dev_id, struct pt_regs * regs)
  863. {
  864. struct net_device *dev = (struct net_device *)dev_id;
  865. struct net_local *np = (struct net_local *)dev->priv;
  866. unsigned long irqbits = *R_IRQ_MASK0_RD;
  867. /* check for underrun irq */
  868. if (irqbits & IO_STATE(R_IRQ_MASK0_RD, underrun, active)) { 
  869. *R_NETWORK_TR_CTRL = IO_STATE(R_NETWORK_TR_CTRL, clr_error, clr);
  870. np->stats.tx_errors++;
  871. D(printk("ethernet receiver underrun!n"));
  872. }
  873. /* check for overrun irq */
  874. if (irqbits & IO_STATE(R_IRQ_MASK0_RD, overrun, active)) { 
  875. update_rx_stats(&np->stats); /* this will ack the irq */
  876. D(printk("ethernet receiver overrun!n"));
  877. }
  878. /* check for excessive collision irq */
  879. if (irqbits & IO_STATE(R_IRQ_MASK0_RD, excessive_col, active)) { 
  880. *R_NETWORK_TR_CTRL = IO_STATE(R_NETWORK_TR_CTRL, clr_error, clr);
  881. np->stats.tx_errors++;
  882. D(printk("ethernet excessive collisions!n"));
  883. }
  884. }
  885. /* We have a good packet(s), get it/them out of the buffers. */
  886. static void
  887. e100_rx(struct net_device *dev)
  888. {
  889. struct sk_buff *skb;
  890. int length = 0;
  891. struct net_local *np = (struct net_local *)dev->priv;
  892. unsigned char *skb_data_ptr;
  893. #ifdef ETHDEBUG
  894. int i;
  895. #endif
  896. if (!led_active && time_after(jiffies, led_next_time)) {
  897. /* light the network leds depending on the current speed. */
  898. e100_set_network_leds(NETWORK_ACTIVITY);
  899. /* Set the earliest time we may clear the LED */
  900. led_next_time = jiffies + NET_FLASH_TIME;
  901. led_active = 1;
  902. mod_timer(&clear_led_timer, jiffies + HZ/10);
  903. }
  904. length = myNextRxDesc->descr.hw_len - 4;
  905. ((struct net_local *)dev->priv)->stats.rx_bytes += length;
  906. #ifdef ETHDEBUG
  907. printk("Got a packet of length %d:n", length);
  908. /* dump the first bytes in the packet */
  909. skb_data_ptr = (unsigned char *)phys_to_virt(myNextRxDesc->descr.buf);
  910. for (i = 0; i < 8; i++) {
  911. printk("%d: %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2xn", i * 8,
  912.        skb_data_ptr[0],skb_data_ptr[1],skb_data_ptr[2],skb_data_ptr[3],
  913.        skb_data_ptr[4],skb_data_ptr[5],skb_data_ptr[6],skb_data_ptr[7]);
  914. skb_data_ptr += 8;
  915. }
  916. #endif
  917. if (length < RX_COPYBREAK) {
  918. /* Small packet, copy data */
  919. skb = dev_alloc_skb(length - ETHER_HEAD_LEN);
  920. if (!skb) {
  921. np->stats.rx_errors++;
  922. printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.n", dev->name);
  923. return;
  924. }
  925. skb_put(skb, length - ETHER_HEAD_LEN);        /* allocate room for the packet body */
  926. skb_data_ptr = skb_push(skb, ETHER_HEAD_LEN); /* allocate room for the header */
  927. #ifdef ETHDEBUG
  928. printk("head = 0x%x, data = 0x%x, tail = 0x%x, end = 0x%xn",
  929.   skb->head, skb->data, skb->tail, skb->end);
  930. printk("copying packet to 0x%x.n", skb_data_ptr);
  931. #endif
  932.           
  933. memcpy(skb_data_ptr, phys_to_virt(myNextRxDesc->descr.buf), length);
  934. }
  935. else {
  936. /* Large packet, send directly to upper layers and allocate new memory */  
  937. skb = myNextRxDesc->skb;
  938. skb_put(skb, length);
  939. myNextRxDesc->skb = dev_alloc_skb(MAX_MEDIA_DATA_SIZE);
  940. myNextRxDesc->descr.buf = virt_to_phys(myNextRxDesc->skb->data);
  941. }
  942. skb->dev = dev;
  943. skb->protocol = eth_type_trans(skb, dev);
  944. /* Send the packet to the upper layers */
  945. netif_rx(skb);
  946. /* Prepare for next packet */
  947. myNextRxDesc->descr.status = 0;
  948. myPrevRxDesc = myNextRxDesc;
  949. myNextRxDesc = phys_to_virt(myNextRxDesc->descr.next);
  950. rx_queue_len++;
  951. /* Check if descriptors should be returned */
  952. if (rx_queue_len == RX_QUEUE_THRESHOLD) {
  953. flush_etrax_cache();
  954. myPrevRxDesc->descr.ctrl |= d_eol;
  955. myLastRxDesc->descr.ctrl &= ~d_eol;
  956. myLastRxDesc = myPrevRxDesc;
  957. rx_queue_len = 0;
  958. }
  959. }
  960. /* The inverse routine to net_open(). */
  961. static int
  962. e100_close(struct net_device *dev)
  963. {
  964. struct net_local *np = (struct net_local *)dev->priv;
  965. printk("Closing %s.n", dev->name);
  966. netif_stop_queue(dev);
  967. *R_NETWORK_GEN_CONFIG =
  968. IO_STATE(R_NETWORK_GEN_CONFIG, phy,    mii_clk) |
  969. IO_STATE(R_NETWORK_GEN_CONFIG, enable, off);
  970. *R_IRQ_MASK0_CLR =
  971. IO_STATE(R_IRQ_MASK0_CLR, overrun, clr) |
  972. IO_STATE(R_IRQ_MASK0_CLR, underrun, clr) |
  973. IO_STATE(R_IRQ_MASK0_CLR, excessive_col, clr);
  974. *R_IRQ_MASK2_CLR =
  975. IO_STATE(R_IRQ_MASK2_CLR, dma0_descr, clr) |
  976. IO_STATE(R_IRQ_MASK2_CLR, dma0_eop, clr) |
  977. IO_STATE(R_IRQ_MASK2_CLR, dma1_descr, clr) |
  978. IO_STATE(R_IRQ_MASK2_CLR, dma1_eop, clr);
  979. /* Stop the receiver and the transmitter */
  980. RESET_DMA(NETWORK_TX_DMA_NBR);
  981. RESET_DMA(NETWORK_RX_DMA_NBR);
  982. /* Flush the Tx and disable Rx here. */
  983. free_irq(NETWORK_DMA_RX_IRQ_NBR, (void *)dev);
  984. free_irq(NETWORK_DMA_TX_IRQ_NBR, (void *)dev);
  985. free_irq(NETWORK_STATUS_IRQ_NBR, (void *)dev);
  986. free_dma(NETWORK_TX_DMA_NBR);
  987. free_dma(NETWORK_RX_DMA_NBR);
  988. /* Update the statistics here. */
  989. update_rx_stats(&np->stats);
  990. update_tx_stats(&np->stats);
  991. return 0;
  992. }
  993. static int
  994. e100_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  995. {
  996. /* Maybe default should return -EINVAL instead? */
  997. switch (cmd) {
  998. case SET_ETH_SPEED_10:                  /* 10 Mbps */
  999. e100_set_speed(10);
  1000. break;
  1001. case SET_ETH_SPEED_100:                /* 100 Mbps */
  1002. e100_set_speed(100);
  1003. break;
  1004. case SET_ETH_SPEED_AUTO:              /* Auto negotiate speed */
  1005. e100_set_speed(0);
  1006. break;
  1007. case SET_ETH_DUPLEX_HALF:              /* Hhalf duplex. */
  1008. e100_set_duplex(half);
  1009. break;
  1010. case SET_ETH_DUPLEX_FULL:              /* Full duplex. */
  1011. e100_set_duplex(full);
  1012. break;
  1013. case SET_ETH_DUPLEX_AUTO:             /* Autonegotiate duplex*/
  1014. e100_set_duplex(autoneg);
  1015. break;
  1016. default: /* Auto neg */
  1017. e100_set_speed(0);
  1018. e100_set_duplex(autoneg);
  1019. break;
  1020. }
  1021. return 0;
  1022. }
  1023. static void
  1024. update_rx_stats(struct net_device_stats *es)
  1025. {
  1026. unsigned long r = *R_REC_COUNTERS;
  1027. /* update stats relevant to reception errors */
  1028. es->rx_fifo_errors += IO_EXTRACT(R_REC_COUNTERS, congestion, r);
  1029. es->rx_crc_errors += IO_EXTRACT(R_REC_COUNTERS, crc_error, r);
  1030. es->rx_frame_errors += IO_EXTRACT(R_REC_COUNTERS, alignment_error, r);
  1031. es->rx_length_errors += IO_EXTRACT(R_REC_COUNTERS, oversize, r);
  1032. }
  1033. static void
  1034. update_tx_stats(struct net_device_stats *es)
  1035. {
  1036. unsigned long r = *R_TR_COUNTERS;
  1037. /* update stats relevant to transmission errors */
  1038. es->collisions +=
  1039. IO_EXTRACT(R_TR_COUNTERS, single_col, r) +
  1040. IO_EXTRACT(R_TR_COUNTERS, multiple_col, r);
  1041. es->tx_errors += IO_EXTRACT(R_TR_COUNTERS, deferred, r);
  1042. }
  1043. /*
  1044.  * Get the current statistics.
  1045.  * This may be called with the card open or closed.
  1046.  */
  1047. static struct net_device_stats *
  1048. e100_get_stats(struct net_device *dev)
  1049. {
  1050. struct net_local *lp = (struct net_local *)dev->priv;
  1051. update_rx_stats(&lp->stats);
  1052. update_tx_stats(&lp->stats);
  1053. return &lp->stats;
  1054. }
  1055. /*
  1056.  * Set or clear the multicast filter for this adaptor.
  1057.  * num_addrs == -1 Promiscuous mode, receive all packets
  1058.  * num_addrs == 0 Normal mode, clear multicast list
  1059.  * num_addrs > 0 Multicast mode, receive normal and MC packets,
  1060.  * and do best-effort filtering.
  1061.  */
  1062. static void
  1063. set_multicast_list(struct net_device *dev)
  1064. {
  1065. int num_addr = dev->mc_count;
  1066. unsigned long int lo_bits;
  1067. unsigned long int hi_bits;
  1068. if (dev->flags & IFF_PROMISC)
  1069. {
  1070. /* promiscuous mode */
  1071. lo_bits = 0xfffffffful;
  1072. hi_bits = 0xfffffffful;
  1073. /* Enable individual receive */
  1074. SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, receive);
  1075. *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
  1076. } else if (dev->flags & IFF_ALLMULTI) {
  1077. /* enable all multicasts */
  1078. lo_bits = 0xfffffffful;
  1079. hi_bits = 0xfffffffful;
  1080. /* Disable individual receive */
  1081. SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, discard);
  1082. *R_NETWORK_REC_CONFIG =  network_rec_config_shadow;
  1083. } else if (num_addr == 0) {
  1084. /* Normal, clear the mc list */
  1085. lo_bits = 0x00000000ul;
  1086. hi_bits = 0x00000000ul;
  1087. /* Disable individual receive */
  1088. SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, discard);
  1089. *R_NETWORK_REC_CONFIG =  network_rec_config_shadow;
  1090. } else {
  1091. /* MC mode, receive normal and MC packets */
  1092. char hash_ix;
  1093. struct dev_mc_list *dmi = dev->mc_list;
  1094. int i;
  1095. char *baddr;
  1096. lo_bits = 0x00000000ul;
  1097. hi_bits = 0x00000000ul;
  1098. for (i=0; i<num_addr; i++) {
  1099. /* Calculate the hash index for the GA registers */
  1100. hash_ix = 0;
  1101. baddr = dmi->dmi_addr;
  1102. hash_ix ^= (*baddr) & 0x3f;
  1103. hash_ix ^= ((*baddr) >> 6) & 0x03;
  1104. ++baddr;
  1105. hash_ix ^= ((*baddr) << 2) & 0x03c;
  1106. hash_ix ^= ((*baddr) >> 4) & 0xf;
  1107. ++baddr;
  1108. hash_ix ^= ((*baddr) << 4) & 0x30;
  1109. hash_ix ^= ((*baddr) >> 2) & 0x3f;
  1110. ++baddr;
  1111. hash_ix ^= (*baddr) & 0x3f;
  1112. hash_ix ^= ((*baddr) >> 6) & 0x03;
  1113. ++baddr;
  1114. hash_ix ^= ((*baddr) << 2) & 0x03c;
  1115. hash_ix ^= ((*baddr) >> 4) & 0xf;
  1116. ++baddr;
  1117. hash_ix ^= ((*baddr) << 4) & 0x30;
  1118. hash_ix ^= ((*baddr) >> 2) & 0x3f;
  1119. hash_ix &= 0x3f;
  1120. if (hash_ix > 32) {
  1121. hi_bits |= (1 << (hash_ix-32));
  1122. }
  1123. else {
  1124. lo_bits |= (1 << hash_ix);
  1125. }
  1126. dmi = dmi->next;
  1127. }
  1128. /* Disable individual receive */
  1129. SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, discard);
  1130. *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
  1131. }
  1132. *R_NETWORK_GA_0 = lo_bits;
  1133. *R_NETWORK_GA_1 = hi_bits;
  1134. }
  1135. void
  1136. e100_hardware_send_packet(char *buf, int length)
  1137. {
  1138. D(printk("e100 send pack, buf 0x%x len %dn", buf, length));
  1139. if (!led_active && time_after(jiffies, led_next_time)) {
  1140. /* light the network leds depending on the current speed. */
  1141. e100_set_network_leds(NETWORK_ACTIVITY);
  1142. /* Set the earliest time we may clear the LED */
  1143. led_next_time = jiffies + NET_FLASH_TIME;
  1144. led_active = 1;
  1145. mod_timer(&clear_led_timer, jiffies + HZ/10);
  1146. }
  1147. /* configure the tx dma descriptor */
  1148. myNextTxDesc->descr.sw_len = length;
  1149. myNextTxDesc->descr.ctrl = d_eop | d_eol | d_wait;
  1150. myNextTxDesc->descr.buf = virt_to_phys(buf);
  1151.         /* Move end of list */
  1152.         myLastTxDesc->descr.ctrl &= ~d_eol;
  1153.         myLastTxDesc = myNextTxDesc;
  1154. /* Restart DMA channel */
  1155. *R_DMA_CH0_CMD = IO_STATE(R_DMA_CH0_CMD, cmd, restart);
  1156. }
  1157. static void
  1158. e100_clear_network_leds(unsigned long dummy)
  1159. {
  1160. if (led_active && time_after(jiffies, led_next_time)) {
  1161. e100_set_network_leds(NO_NETWORK_ACTIVITY);
  1162. /* Set the earliest time we may set the LED */
  1163. led_next_time = jiffies + NET_FLASH_PAUSE;
  1164. led_active = 0;
  1165. }
  1166. }
  1167. static void
  1168. e100_set_network_leds(int active)
  1169. {
  1170. #if defined(CONFIG_ETRAX_NETWORK_LED_ON_WHEN_LINK)
  1171. int light_leds = (active == NO_NETWORK_ACTIVITY);
  1172. #elif defined(CONFIG_ETRAX_NETWORK_LED_ON_WHEN_ACTIVITY)
  1173. int light_leds = (active == NETWORK_ACTIVITY);
  1174. #else
  1175. #error "Define either CONFIG_ETRAX_NETWORK_LED_ON_WHEN_LINK or CONFIG_ETRAX_NETWORK_LED_ON_WHEN_ACTIVITY"
  1176. #endif
  1177. if (!current_speed) {
  1178. /* Make LED red, link is down */
  1179. LED_NETWORK_SET(LED_RED);
  1180. }
  1181. else if (light_leds) {
  1182. if (current_speed == 10) {
  1183. LED_NETWORK_SET(LED_ORANGE);
  1184. } else {
  1185. LED_NETWORK_SET(LED_GREEN);
  1186. }
  1187. }
  1188. else {
  1189. LED_NETWORK_SET(LED_OFF);
  1190. }
  1191. }
  1192. static struct net_device dev_etrax_ethernet;  /* only got one */
  1193. static int
  1194. etrax_init_module(void)
  1195. {
  1196. struct net_device *d = &dev_etrax_ethernet;
  1197. d->init = etrax_ethernet_init;
  1198. if (register_netdev(d) == 0)
  1199. return 0;
  1200. else
  1201. return -ENODEV;
  1202. }
  1203. module_init(etrax_init_module);