]>
Commit | Line | Data |
---|---|---|
3c4e084d JP |
1 | /* net.c |
2 | * Top-level network glue code | |
3 | * NetWatch system management mode administration console | |
4 | * | |
5 | * Copyright (c) 2008 Jacob Potter and Joshua Wise. All rights reserved. | |
6 | * This program is free software; you can redistribute and/or modify it under | |
7 | * the terms found in the file LICENSE in the root of this source tree. | |
8 | * | |
9 | */ | |
10 | ||
3b3161a1 | 11 | #include <pci.h> |
6c744a5a | 12 | #include <smram.h> |
4d898e0b | 13 | #include <pci-bother.h> |
3b3161a1 | 14 | #include <output.h> |
14e7cde4 | 15 | #include <minilib.h> |
9552cf46 | 16 | #include <lwip/init.h> |
4d898e0b | 17 | #include "net.h" |
47c41031 JW |
18 | |
19 | #include "lwip/opt.h" | |
20 | #include "lwip/def.h" | |
21 | #include "lwip/mem.h" | |
22 | #include "lwip/pbuf.h" | |
23 | #include "lwip/sys.h" | |
24 | #include <lwip/stats.h> | |
25 | #include <lwip/snmp.h> | |
98d29191 JW |
26 | #include <lwip/dhcp.h> |
27 | #include <lwip/tcp.h> | |
47c41031 JW |
28 | #include "netif/etharp.h" |
29 | #include "netif/ppp_oe.h" | |
3b3161a1 | 30 | |
7da36fbd JP |
31 | #include "rfb.h" |
32 | ||
7a914840 | 33 | static struct nic *_nic = 0x0; |
47c41031 | 34 | static struct netif _netif; |
3e6d6106 | 35 | |
7a914840 JW |
36 | extern struct pci_driver a3c90x_driver; |
37 | ||
bec09bd1 | 38 | void eth_recv(struct nic *nic, struct pbuf *p) |
6c744a5a | 39 | { |
47c41031 | 40 | struct eth_hdr *ethhdr; |
bec09bd1 JW |
41 | |
42 | LINK_STATS_INC(link.recv); | |
43 | ||
44 | ethhdr = p->payload; | |
45 | ||
46 | switch (htons(ethhdr->type)) { | |
47 | case ETHTYPE_IP: | |
48 | case ETHTYPE_ARP: | |
49 | if (_netif.input(p, &_netif) != ERR_OK) | |
50 | { | |
51 | LWIP_DEBUGF(NETIF_DEBUG, ("netdev_input: IP input error\n")); | |
52 | pbuf_free(p); | |
53 | } | |
54 | break; | |
55 | ||
56 | default: | |
57 | outputf("Unhandled packet type %04x input", ethhdr->type); | |
58 | pbuf_free(p); | |
59 | break; | |
60 | } | |
61 | } | |
62 | ||
63 | void eth_poll() | |
64 | { | |
98d29191 | 65 | static int ticks = 0; |
03bcc4db | 66 | int i = 15; /* Don't process more than 15 packets at a time; we don't want the host to get TOO badly slowed down... */ |
47c41031 JW |
67 | |
68 | if (!_nic) | |
69 | return; | |
70 | ||
71 | smram_tseg_set_state(SMRAM_TSEG_OPEN); | |
98d29191 JW |
72 | |
73 | if ((ticks % 1000) == 0) /* About a minute */ | |
74 | dhcp_coarse_tmr(); | |
75 | if ((ticks % 8) == 0) /* About 500msec*/ | |
76 | dhcp_fine_tmr(); | |
77 | if ((ticks % 4) == 0) /* About 250msec*/ | |
78 | tcp_tmr(); | |
79 | ticks++; | |
6c744a5a | 80 | |
bec09bd1 | 81 | while (i > 0) |
47c41031 | 82 | { |
bec09bd1 JW |
83 | int n = _nic->recv(_nic); |
84 | i -= n; | |
85 | if (n == 0) | |
4d886842 | 86 | break; |
14e7cde4 JP |
87 | } |
88 | } | |
89 | ||
47c41031 | 90 | static err_t _transmit(struct netif *netif, struct pbuf *p) |
3b3161a1 | 91 | { |
47c41031 | 92 | struct nic *nic = netif->state; |
99182958 | 93 | |
bc9e1044 | 94 | // outputf("NIC: Transmit packet"); |
50d89a31 | 95 | |
6d6494e4 | 96 | nic->transmit(nic, p); |
d3411e0d | 97 | |
47c41031 JW |
98 | LINK_STATS_INC(link.xmit); |
99 | ||
100 | return ERR_OK; | |
101 | } | |
102 | ||
103 | static err_t _init(struct netif *netif) | |
104 | { | |
105 | struct nic *nic = netif->state; | |
106 | ||
107 | LWIP_ASSERT("netif != NULL", (netif != NULL)); | |
108 | ||
109 | #if LWIP_NETIF_HOSTNAME | |
110 | netif->hostname = "netwatch"; | |
111 | #endif | |
6c744a5a | 112 | |
47c41031 | 113 | NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 100000000); |
6c744a5a | 114 | |
47c41031 JW |
115 | netif->name[0] = 'e'; |
116 | netif->name[1] = 'n'; | |
117 | netif->output = etharp_output; | |
118 | netif->linkoutput = _transmit; | |
99182958 | 119 | |
47c41031 JW |
120 | memcpy(netif->hwaddr, nic->hwaddr, 6); |
121 | netif->mtu = 1500; | |
122 | netif->hwaddr_len = ETHARP_HWADDR_LEN; | |
123 | netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP; | |
124 | ||
125 | return ERR_OK; | |
3b3161a1 JW |
126 | } |
127 | ||
7a914840 | 128 | int eth_register(struct nic *nic) |
3b3161a1 | 129 | { |
98d29191 | 130 | static struct ip_addr ipa = { 0 } , netmask = { 0 } , gw = { 0 }; |
47c41031 | 131 | |
7a914840 JW |
132 | if (_nic) |
133 | return -1; | |
47c41031 JW |
134 | netif_add(&_netif, &ipa, &netmask, &gw, (void*)nic, _init, ethernet_input); |
135 | netif_set_default(&_netif); | |
136 | netif_set_up(&_netif); | |
98d29191 | 137 | dhcp_start(&_netif); |
7a914840 JW |
138 | _nic = nic; |
139 | return 0; | |
3b3161a1 JW |
140 | } |
141 | ||
142 | void eth_init() | |
143 | { | |
6f9272bd JW |
144 | extern void httpd_init(); |
145 | ||
c25f3f39 JW |
146 | /* Required for DMA to work. :( */ |
147 | smram_tseg_set_state(SMRAM_TSEG_OPEN); | |
9552cf46 | 148 | lwip_init(); |
6f9272bd | 149 | httpd_init(); |
80726808 | 150 | |
7da36fbd | 151 | rfb_init(); |
80726808 | 152 | |
3b3161a1 | 153 | } |