1 #include <geekos/ne2k.h>
2 #include <geekos/debug.h>
4 #include <geekos/irq.h>
5 #include <geekos/malloc.h>
6 #include <geekos/string.h>
9 #define TX_START_BUFF 0x40
10 #define RX_START_BUFF 0x4c
11 #define RX_END_BUFF 0x80
13 uint_t next = (RX_START_BUFF << 8);
14 static uint_t received = 0;
15 static uint_t send_done = 1;
18 int (*packet_received)(struct NE2K_Packet_Info *info, uchar_t *packet);
21 static void Dump_Registers()
24 PrintBoth("Dumping NIC registers for page %x...\n", (In_Byte(NE2K_CR) & 0xc0) >> 6);
26 for(i = 0; i <= 0x0f; i += 0x01) {
27 data = In_Byte(NE2K_BASE_ADDR+i);
28 PrintBoth("\t%x: %x\n", NE2K_BASE_ADDR + i, data);
32 static int NE2K_Transmit(struct NE2K_REGS *regs)
37 struct _CR * cr = (struct _CR*)&(regs->cr);
38 uint_t packet_size = 80;
40 cr->stp = 0x0; //toggle start on
42 Out_Byte(NE2K_CR, regs->cr);
44 // Read-before-write bug fix?
45 Out_Byte(NE2K_RBCR0, 0x42);
46 Out_Byte(NE2K_RBCR1, 0x00);
47 Out_Byte(NE2K_RSAR0, 0x42);
48 Out_Byte(NE2K_RSAR1, 0x00);
50 cr->rd = 0x01; // set remote DMA to 'remote read'
51 Out_Byte(NE2K_CR, regs->cr);
53 regs->isr = 0x40; // clear and set Remote DMA high
54 Out_Byte(NE2K_ISR, regs->isr);
56 Out_Byte(NE2K_RBCR0, packet_size);
57 Out_Byte(NE2K_RBCR1, 0x00);
59 Out_Byte(NE2K_TBCR0, packet_size);
60 Out_Byte(NE2K_TBCR1, 0x00);
62 Out_Byte(NE2K_RSAR0, 0x00);
63 Out_Byte(NE2K_RSAR1, 0x40);
66 Out_Byte(NE2K_CR, regs->cr);
68 /* Begin pushing the packet into the dataport (located at 0x10 from the base address) */
69 /* Destination address = 52:54:00:12:34:56 */
70 Out_Word(NE2K_CR+0x10, 0x5452);
71 Out_Word(NE2K_CR+0x10, 0x1200);
72 Out_Word(NE2K_CR+0x10, 0x5634);
74 /* Source address = 52:54:00:12:34:58 */
75 Out_Word(NE2K_CR+0x10, 0x5452);
76 Out_Word(NE2K_CR+0x10, 0x1200);
77 Out_Word(NE2K_CR+0x10, 0x5834);
79 /* Type length and data; currently random data */
82 for(i = 1; i <= packet_size/2-12; i++, n+=2) {
83 //Out_Word(NE2K_CR+0x10, 0x0f0b);
84 Out_Word(NE2K_CR+0x10, (n<<8) | (n+1));
88 //Out_Byte(NE2K_ISR, regs->isr); /* Do we need this here? */
93 static void NE2K_Interrupt_Handler(struct Interrupt_State * state)
96 PrintBoth("NIC Interrupt Occured!\n");
97 uchar_t isr_content = In_Byte(NE2K_ISR);
99 PrintBoth("Contents of ISR: %x\n", isr_content);
101 if(isr_content & 0x01) /* A packet has been received. */
105 Out_Byte(NE2K_CR, 0x4a);
106 current = In_Byte(NE2K_CURR);
107 Out_Byte(NE2K_CR, 0x0a);
109 Out_Byte(NE2K_ISR, 0x01);
110 /* If BNRY and CURR aren't equal, more than one packet has been received. */
111 }while (current > In_Byte(NE2K_BNRY));
115 if(isr_content & 0x02) /* A packet has been successfully transmitted. */
118 Out_Byte(NE2K_ISR, 0x02);
120 Out_Byte(NE2K_ISR, 0xff); /* Clear all interrupts. */
123 int Init_Ne2k(int (*rcvd_fn)(struct NE2K_Packet_Info *info, uchar_t *packet))
125 callbacks.packet_received = rcvd_fn;
127 PrintBoth("Initializing network card...\n");
128 Out_Byte(NE2K_CR+0x1f, In_Byte(NE2K_CR+0x1f)); /* Reset? */
130 struct NE2K_REGS* regs = Malloc(sizeof(struct NE2K_REGS));
131 struct _CR * cr = (struct _CR *)&(regs->cr);
132 struct _RCR * rcr = (struct _RCR*)&(regs->rcr);
133 struct _IMR * imr = (struct _IMR *)&(regs->imr);
136 regs->dcr = 0x49; /* Word-wide DMA transfer. */
137 regs->isr = 0xff; /* Clear all interrupts. */
138 regs->rcr = 0x20; /* Accept packets shorter than 64 bytes. */
139 regs->tcr = 0x02; /* Internal loopback mode. */
141 Out_Byte(NE2K_CR, regs->cr);
142 Out_Byte(NE2K_DCR, regs->dcr);
143 Out_Byte(NE2K_ISR, regs->isr);
144 Out_Byte(NE2K_RCR, regs->rcr);
145 Out_Byte(NE2K_TCR, regs->tcr);
146 Out_Byte(NE2K_IMR, regs->imr);
148 /* Remote byte count registers. */
149 Out_Byte(NE2K_RBCR0, 0x00);
150 Out_Byte(NE2K_RBCR1, 0x00);
151 /* Remote start address registers. */
152 Out_Byte(NE2K_RSAR0, 0x00);
153 Out_Byte(NE2K_RSAR1, 0x00);
155 Out_Byte(NE2K_TPSR, TX_START_BUFF); /* Transmit page start register */
156 Out_Byte(NE2K_PSTART, RX_START_BUFF); /* Page start register */
157 Out_Byte(NE2K_PSTOP, RX_END_BUFF); /* Page stop register */
158 Out_Byte(NE2K_BNRY, RX_START_BUFF); /* Boundary register */
160 cr->ps = 0x01; /* Switch to reg page 1. */
161 Out_Byte(NE2K_CR, regs->cr);
162 /* Current page register: points to first free page that can be used for packet reception. */
163 Out_Byte(NE2K_CURR, RX_START_BUFF);
164 cr->ps = 0x00; /* Switch to page 0 */
166 Out_Byte(NE2K_CR, regs->cr);
167 Out_Byte(NE2K_ISR, regs->isr);
169 /* Interrupt mask register: setting a bit to 1 enables the
170 corresponding interrupt in ISR. */
177 Out_Byte(NE2K_IMR, regs->imr);
179 cr->ps = 0x01; /* Switch to reg page 1 */
180 Out_Byte(NE2K_CR, regs->cr);
182 /* Set the physical address of the card to 52:54:00:12:34:58 */
183 Out_Byte(NE2K_CR+0x01, 0x52);
184 Out_Byte(NE2K_CR+0x02, 0x54);
185 Out_Byte(NE2K_CR+0x03, 0x00);
186 Out_Byte(NE2K_CR+0x04, 0x12);
187 Out_Byte(NE2K_CR+0x05, 0x34);
188 Out_Byte(NE2K_CR+0x06, 0x58);
190 /* Set the multicast address register to all 1s; accepts all multicast packets */
192 for(i = 0x08; i <= 0x0f; i++) {
193 Out_Byte(NE2K_CR+i, 0xff);
196 regs->cr = 0x21; //set CR to start value
197 Out_Byte(NE2K_CR, regs->cr);
200 Out_Byte(NE2K_TCR, regs->tcr);
206 rcr->pro = 0x1; /* Promiscuous mode: accept all packets. */
208 Out_Byte(NE2K_RCR, regs->rcr);
210 cr->sta = 0x1; // toggle start bit
212 Out_Byte(NE2K_CR, regs->cr);
217 Out_Byte(NE2K_CR, regs->cr);
221 Out_Byte(NE2K_CR, regs->cr);
225 Out_Byte(NE2K_CR, regs->cr);
227 Install_IRQ(NE2K_IRQ, NE2K_Interrupt_Handler);
228 Enable_IRQ(NE2K_IRQ);
230 for(i = 0; i < 0; i++)
233 PrintBoth("Transmitting a packet\n");
236 uchar_t src_addr[6] = { 0x52, 0x54, 0x00, 0x12, 0x34, 0x58 };
237 uchar_t dest_addr[6] = { 0x52, 0x54, 0x00, 0x12, 0x34, 0x56 };
240 uchar_t *data = Malloc(size);
241 data = "This is a 64-byte string that will be used to test transmission.";
243 for(i = 0; i < 0; i++) {
244 NE2K_Send(regs, src_addr, dest_addr, 0x01, data, size);
247 //Free(data); // Why does this crash?
252 /* Assumes src and dest are arrays of 6 characters. */
253 int NE2K_Send(struct NE2K_REGS *regs, uchar_t src[], uchar_t dest[], uint_t type, uchar_t *data, uint_t size)
255 struct _CR * cr = (struct _CR*)&(regs->cr);
256 uint_t packet_size = size + 16;
258 cr->stp = 0x0; //toggle start on
260 Out_Byte(NE2K_CR, regs->cr);
262 // Read-before-write bug fix?
263 Out_Byte(NE2K_RBCR0, 0x42);
264 Out_Byte(NE2K_RBCR1, 0x00);
265 Out_Byte(NE2K_RSAR0, 0x42);
266 Out_Byte(NE2K_RSAR1, 0x00);
268 cr->rd = 0x01; // set remote DMA to 'remote read'
269 Out_Byte(NE2K_CR, regs->cr);
271 regs->isr = 0x40; // clear and set Remote DMA high
272 Out_Byte(NE2K_ISR, regs->isr);
274 /* Set remote byte count registers */
275 Out_Byte(NE2K_RBCR0, packet_size & 0xff);
276 Out_Byte(NE2K_RBCR1, (packet_size >> 8) & 0xff);
278 /* Set transmit byte count registers. */
279 Out_Byte(NE2K_TBCR0, packet_size & 0xff);
280 Out_Byte(NE2K_TBCR1, (packet_size >> 8) & 0xff);
282 Out_Byte(NE2K_RSAR0, 0x00);
283 Out_Byte(NE2K_RSAR1, TX_START_BUFF);
286 Out_Byte(NE2K_CR, regs->cr);
288 /* Destination Address */
289 Out_Word(NE2K_CR + 0x10, (dest[1] << 8) | dest[0]);
290 Out_Word(NE2K_CR + 0x10, (dest[3] << 8) | dest[2]);
291 Out_Word(NE2K_CR + 0x10, (dest[5] << 8) | dest[4]);
294 Out_Word(NE2K_CR + 0x10, (src[1] << 8) | src[0]);
295 Out_Word(NE2K_CR + 0x10, (src[3] << 8) | src[2]);
296 Out_Word(NE2K_CR + 0x10, (src[5] << 8) | src[4]);
299 Out_Word(NE2K_CR + 0x10, packet_size);
302 for(i = 0; i < size; i += 2) {
303 Out_Word(NE2K_CR + 0x10, (*(data + i + 1) << 8) | *(data + i));
311 PrintBoth("Packet Received\n");
313 Out_Byte(NE2K_CR, 0x22);
314 /* Set RSAR to the start address of the received packet. */
315 Out_Byte(NE2K_RSAR0, next & 0xff);
316 Out_Byte(NE2K_RSAR1, next >> 8);
317 Out_Byte(NE2K_CR, 0x0a);
320 * A four byte header is added to the beginning of each received packet by the NIC.
321 * The first byte is the location of the next packet in the ring buffer.
322 * The second byte is the receive status code.
323 * The third and fourth bytes are the size of the packet.
328 PrintBoth("\nPacket data:\n\t");
330 data = In_Word(NE2K_CR + 0x10);
331 PrintBoth("%x ", data);
333 /* Get the location of the next packet */
334 next = data & 0xff00;
336 /* Retrieve the packet size from the header, and store it in RBCR. */
337 uint_t packet_size = In_Word(NE2K_CR + 0x10) - 4;
338 uchar_t *packet = Malloc(packet_size);
339 Out_Byte(NE2K_RBCR0, packet_size & 0xff);
340 Out_Byte(NE2K_RBCR1, (packet_size>>8) & 0xff);
343 PrintBoth("packetsize = %x\n\t", packet_size);
346 /* Copy the received packet over from the ring buffer. */
347 for(i = 0; i < packet_size; i+=2) {
348 data = In_Word(NE2K_CR + 0x10);
349 //PrintBoth("%x ", data);
350 *(packet + i) = data & 0x00ff;
351 *(packet + i + 1) = (data & 0xff00) >> 8;
353 PrintBoth("BNRY = %x\n", In_Byte(NE2K_BNRY));
354 Out_Byte(NE2K_CR, 0x4a);
355 PrintBoth("CURR = %x\n", In_Byte(NE2K_CURR));
356 Out_Byte(NE2K_CR, 0x0a);
363 //Out_Byte(NE2K_RBCR0, (In_Byte(NE2K_RBCR0))-2);
364 //Out_Byte(NE2K_RSAR0, (In_Byte(NE2K_RSAR0))+2);
366 PrintBoth("\n%d packets have been received", ++received);
369 Out_Byte(NE2K_ISR, 0x40); /* Clear the remote DMA complete interrupt. */
371 /* The BNRY register stores the location of the first packet that hasn't been read yet */
372 Out_Byte(NE2K_BNRY, next >> 8);
374 struct NE2K_Packet_Info *info = Malloc(sizeof(struct NE2K_Packet_Info));
375 info->size = packet_size;
377 memcpy(info->dest, packet, 6);
378 memcpy(info->src, packet + 6, 6);
379 callbacks.packet_received(info, packet);