]> Joshua Wise's Git repositories - netwatch.git/blame - net/net.c
More ICH2-specific code diked out.
[netwatch.git] / net / net.c
CommitLineData
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>
4e3ef36b 16#include <tables.h>
9552cf46 17#include <lwip/init.h>
4d898e0b 18#include "net.h"
47c41031
JW
19
20#include "lwip/opt.h"
21#include "lwip/def.h"
22#include "lwip/mem.h"
23#include "lwip/pbuf.h"
24#include "lwip/sys.h"
25#include <lwip/stats.h>
26#include <lwip/snmp.h>
98d29191
JW
27#include <lwip/dhcp.h>
28#include <lwip/tcp.h>
47c41031
JW
29#include "netif/etharp.h"
30#include "netif/ppp_oe.h"
3b3161a1 31
7a914840 32static struct nic *_nic = 0x0;
47c41031 33static struct netif _netif;
3e6d6106 34
7a914840
JW
35extern struct pci_driver a3c90x_driver;
36
bec09bd1 37void eth_recv(struct nic *nic, struct pbuf *p)
6c744a5a 38{
47c41031 39 struct eth_hdr *ethhdr;
bec09bd1
JW
40
41 LINK_STATS_INC(link.recv);
42
43 ethhdr = p->payload;
44
45 switch (htons(ethhdr->type)) {
46 case ETHTYPE_IP:
47 case ETHTYPE_ARP:
48 if (_netif.input(p, &_netif) != ERR_OK)
49 {
50 LWIP_DEBUGF(NETIF_DEBUG, ("netdev_input: IP input error\n"));
51 pbuf_free(p);
52 }
53 break;
54
55 default:
56 outputf("Unhandled packet type %04x input", ethhdr->type);
57 pbuf_free(p);
58 break;
59 }
60}
61
62void eth_poll()
63{
98d29191 64 static int ticks = 0;
03bcc4db 65 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
66
67 if (!_nic)
68 return;
69
70 smram_tseg_set_state(SMRAM_TSEG_OPEN);
98d29191
JW
71
72 if ((ticks % 1000) == 0) /* About a minute */
73 dhcp_coarse_tmr();
74 if ((ticks % 8) == 0) /* About 500msec*/
75 dhcp_fine_tmr();
76 if ((ticks % 4) == 0) /* About 250msec*/
77 tcp_tmr();
78 ticks++;
6c744a5a 79
bec09bd1 80 while (i > 0)
47c41031 81 {
bec09bd1
JW
82 int n = _nic->recv(_nic);
83 i -= n;
84 if (n == 0)
4d886842 85 break;
14e7cde4
JP
86 }
87}
88
47c41031 89static err_t _transmit(struct netif *netif, struct pbuf *p)
3b3161a1 90{
47c41031 91 struct nic *nic = netif->state;
99182958 92
bc9e1044 93// outputf("NIC: Transmit packet");
50d89a31 94
6d6494e4 95 nic->transmit(nic, p);
d3411e0d 96
47c41031
JW
97 LINK_STATS_INC(link.xmit);
98
99 return ERR_OK;
100}
101
102static err_t _init(struct netif *netif)
103{
104 struct nic *nic = netif->state;
105
106 LWIP_ASSERT("netif != NULL", (netif != NULL));
107
108#if LWIP_NETIF_HOSTNAME
109 netif->hostname = "netwatch";
110#endif
6c744a5a 111
47c41031 112 NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 100000000);
6c744a5a 113
47c41031
JW
114 netif->name[0] = 'e';
115 netif->name[1] = 'n';
116 netif->output = etharp_output;
117 netif->linkoutput = _transmit;
99182958 118
47c41031
JW
119 memcpy(netif->hwaddr, nic->hwaddr, 6);
120 netif->mtu = 1500;
121 netif->hwaddr_len = ETHARP_HWADDR_LEN;
122 netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
123
124 return ERR_OK;
3b3161a1
JW
125}
126
7a914840 127int eth_register(struct nic *nic)
3b3161a1 128{
98d29191 129 static struct ip_addr ipa = { 0 } , netmask = { 0 } , gw = { 0 };
47c41031 130
7a914840
JW
131 if (_nic)
132 return -1;
47c41031
JW
133 netif_add(&_netif, &ipa, &netmask, &gw, (void*)nic, _init, ethernet_input);
134 netif_set_default(&_netif);
135 netif_set_up(&_netif);
98d29191 136 dhcp_start(&_netif);
7a914840
JW
137 _nic = nic;
138 return 0;
3b3161a1
JW
139}
140
4e3ef36b
JW
141typedef void(*thunk_t)();
142
143TABLE(thunk_t, protocols);
144
3b3161a1
JW
145void eth_init()
146{
4e3ef36b 147 int i;
6f9272bd 148
c25f3f39
JW
149 /* Required for DMA to work. :( */
150 smram_tseg_set_state(SMRAM_TSEG_OPEN);
80726808 151
4e3ef36b 152 lwip_init();
80726808 153
4e3ef36b
JW
154 for (i = 0; i < TABLE_LENGTH(protocols); i++)
155 protocols_table[i]();
3b3161a1 156}
This page took 0.044901 seconds and 5 git commands to generate.