]>
Commit | Line | Data |
---|---|---|
1 | #include <pci.h> | |
2 | #include <smram.h> | |
3 | #include <pci-bother.h> | |
4 | #include <output.h> | |
5 | #include <minilib.h> | |
6 | #include <lwip/init.h> | |
7 | #include "net.h" | |
8 | ||
9 | #include "lwip/opt.h" | |
10 | #include "lwip/def.h" | |
11 | #include "lwip/mem.h" | |
12 | #include "lwip/pbuf.h" | |
13 | #include "lwip/sys.h" | |
14 | #include <lwip/stats.h> | |
15 | #include <lwip/snmp.h> | |
16 | #include <lwip/dhcp.h> | |
17 | #include <lwip/tcp.h> | |
18 | #include "netif/etharp.h" | |
19 | #include "netif/ppp_oe.h" | |
20 | ||
21 | static struct nic *_nic = 0x0; | |
22 | static struct netif _netif; | |
23 | ||
24 | extern struct pci_driver a3c90x_driver; | |
25 | ||
26 | void eth_poll() | |
27 | { | |
28 | unsigned char pkt[1600]; | |
29 | int len; | |
30 | struct pbuf *p, *q; | |
31 | struct eth_hdr *ethhdr; | |
32 | int pos = 0; | |
33 | static int ticks = 0; | |
34 | ||
35 | if (!_nic) | |
36 | return; | |
37 | ||
38 | smram_tseg_set_state(SMRAM_TSEG_OPEN); | |
39 | ||
40 | if ((ticks % 1000) == 0) /* About a minute */ | |
41 | dhcp_coarse_tmr(); | |
42 | if ((ticks % 8) == 0) /* About 500msec*/ | |
43 | dhcp_fine_tmr(); | |
44 | if ((ticks % 4) == 0) /* About 250msec*/ | |
45 | tcp_tmr(); | |
46 | ticks++; | |
47 | ||
48 | if (!_nic->poll(_nic, 0)) | |
49 | return; | |
50 | ||
51 | _nic->packet = pkt; | |
52 | _nic->poll(_nic, 1); | |
53 | ||
54 | len = _nic->packetlen; | |
55 | ||
56 | outputf("NIC: Packet: %d bytes", len); | |
57 | ||
58 | p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL); | |
59 | if (p == NULL) | |
60 | { | |
61 | outputf("NIC: out of memory for packet?"); | |
62 | LINK_STATS_INC(link.memerr); | |
63 | LINK_STATS_INC(link.drop); | |
64 | return; | |
65 | } | |
66 | ||
67 | for(q = p; q != NULL; q = q->next) | |
68 | { | |
69 | memcpy(q->payload, pkt+pos, q->len); | |
70 | pos += q->len; | |
71 | } | |
72 | ||
73 | LINK_STATS_INC(link.recv); | |
74 | ||
75 | ethhdr = p->payload; | |
76 | ||
77 | switch (htons(ethhdr->type)) { | |
78 | case ETHTYPE_IP: | |
79 | case ETHTYPE_ARP: | |
80 | if (_netif.input(p, &_netif) != ERR_OK) | |
81 | { | |
82 | LWIP_DEBUGF(NETIF_DEBUG, ("netdev_input: IP input error\n")); | |
83 | pbuf_free(p); | |
84 | } | |
85 | break; | |
86 | ||
87 | default: | |
88 | outputf("Unhandled packet type %04x input", ethhdr->type); | |
89 | pbuf_free(p); | |
90 | break; | |
91 | } | |
92 | } | |
93 | ||
94 | static err_t _transmit(struct netif *netif, struct pbuf *p) | |
95 | { | |
96 | struct nic *nic = netif->state; | |
97 | struct pbuf *q; | |
98 | unsigned char pkt[1600]; | |
99 | unsigned int len = 0; | |
100 | ||
101 | for(q = p; q != NULL; q = q->next) | |
102 | { | |
103 | memcpy(pkt + len, q->payload, q->len); | |
104 | len += q->len; | |
105 | } | |
106 | ||
107 | outputf("NIC: Transmit packet: %d bytes", len); | |
108 | ||
109 | nic->transmit(len, pkt); | |
110 | ||
111 | LINK_STATS_INC(link.xmit); | |
112 | ||
113 | return ERR_OK; | |
114 | } | |
115 | ||
116 | static err_t _init(struct netif *netif) | |
117 | { | |
118 | struct nic *nic = netif->state; | |
119 | ||
120 | LWIP_ASSERT("netif != NULL", (netif != NULL)); | |
121 | ||
122 | #if LWIP_NETIF_HOSTNAME | |
123 | netif->hostname = "netwatch"; | |
124 | #endif | |
125 | ||
126 | NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 100000000); | |
127 | ||
128 | netif->name[0] = 'e'; | |
129 | netif->name[1] = 'n'; | |
130 | netif->output = etharp_output; | |
131 | netif->linkoutput = _transmit; | |
132 | ||
133 | memcpy(netif->hwaddr, nic->hwaddr, 6); | |
134 | netif->mtu = 1500; | |
135 | netif->hwaddr_len = ETHARP_HWADDR_LEN; | |
136 | netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP; | |
137 | ||
138 | return ERR_OK; | |
139 | } | |
140 | ||
141 | int eth_register(struct nic *nic) | |
142 | { | |
143 | static struct ip_addr ipa = { 0 } , netmask = { 0 } , gw = { 0 }; | |
144 | ||
145 | if (_nic) | |
146 | return -1; | |
147 | netif_add(&_netif, &ipa, &netmask, &gw, (void*)nic, _init, ethernet_input); | |
148 | netif_set_default(&_netif); | |
149 | netif_set_up(&_netif); | |
150 | dhcp_start(&_netif); | |
151 | _nic = nic; | |
152 | return 0; | |
153 | } | |
154 | ||
155 | void eth_init() | |
156 | { | |
157 | extern void httpd_init(); | |
158 | ||
159 | /* Required for DMA to work. :( */ | |
160 | smram_tseg_set_state(SMRAM_TSEG_OPEN); | |
161 | lwip_init(); | |
162 | pci_probe_driver(a3c90x_driver); | |
163 | httpd_init(); | |
164 | } |