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>
8 #include <uip/uip_arp.h>
11 #define TX_START_BUFF 0x40
12 #define RX_START_BUFF 0x4c
13 #define RX_END_BUFF 0x80
15 static uint_t next = (RX_START_BUFF << 8);
16 static uint_t received = 0;
17 static uint_t send_done = 1;
19 struct NE2K_REGS* regs;
22 int (*packet_received)(struct NE2K_Packet_Info *info, uchar_t *packet);
26 static void Dump_Registers()
29 PrintBoth("Dumping NIC registers for page %x...\n", (In_Byte(NE2K_CR) & 0xc0) >> 6);
31 for(i = 0; i <= 0x0f; i += 0x01) {
32 data = In_Byte(NE2K_BASE_ADDR+i);
33 PrintBoth("\t%x: %x\n", NE2K_BASE_ADDR + i, data);
38 static void NE2K_Interrupt_Handler(struct Interrupt_State * state)
42 PrintBoth("NIC Interrupt Occured!\n");
43 uchar_t isr_content = In_Byte(NE2K_ISR);
45 PrintBoth("Contents of ISR: %x\n", isr_content);
47 if(isr_content & 0x01) /* A packet has been received. */
50 Out_Byte(NE2K_CR, 0x4a); /* Page 1 */
51 current = In_Byte(NE2K_CURR);
52 Out_Byte(NE2K_CR, 0x0a); /* Page 0 */
55 /* When CURR equals BNRY, all packets in the receive ring buffer have been read, and
56 the packet received bit in the interrupt status register can be cleared. */
57 if(current == In_Byte(NE2K_BNRY))
58 Out_Byte(NE2K_ISR, 0x01);
62 if(isr_content & 0x02) /* A packet has been successfully transmitted. */
65 Out_Byte(NE2K_ISR, 0x02);
68 //Out_Byte(NE2K_ISR, 0xff); /* Clear all interrupts. */
71 int Init_Ne2k(int (*rcvd_fn)(struct NE2K_Packet_Info *info, uchar_t *packet))
73 callbacks.packet_received = rcvd_fn;
75 PrintBoth("Initializing network card...\n");
76 Out_Byte(NE2K_CR+0x1f, In_Byte(NE2K_CR+0x1f)); /* Reset */
78 regs = Malloc(sizeof(struct NE2K_REGS));
79 struct _CR * cr = (struct _CR *)&(regs->cr);
80 struct _RCR * rcr = (struct _RCR*)&(regs->rcr);
81 struct _IMR * imr = (struct _IMR *)&(regs->imr);
84 regs->dcr = 0x49; /* Word-wide DMA transfer. */
85 regs->isr = 0xff; /* Clear all interrupts. */
86 regs->rcr = 0x20; /* Accept packets shorter than 64 bytes. */
87 regs->tcr = 0x02; /* Internal loopback mode. */
89 Out_Byte(NE2K_CR, regs->cr);
90 Out_Byte(NE2K_DCR, regs->dcr);
91 Out_Byte(NE2K_ISR, regs->isr);
92 Out_Byte(NE2K_RCR, regs->rcr);
93 Out_Byte(NE2K_TCR, regs->tcr);
94 Out_Byte(NE2K_IMR, regs->imr);
96 /* Remote byte count registers. */
97 Out_Byte(NE2K_RBCR0, 0x00);
98 Out_Byte(NE2K_RBCR1, 0x00);
99 /* Remote start address registers. */
100 Out_Byte(NE2K_RSAR0, 0x00);
101 Out_Byte(NE2K_RSAR1, 0x00);
103 Out_Byte(NE2K_TPSR, TX_START_BUFF); /* Transmit page start register */
104 Out_Byte(NE2K_PSTART, RX_START_BUFF); /* Page start register */
105 Out_Byte(NE2K_PSTOP, RX_END_BUFF); /* Page stop register */
106 Out_Byte(NE2K_BNRY, RX_START_BUFF); /* Boundary register */
108 cr->ps = 0x01; /* Switch to reg page 1. */
109 Out_Byte(NE2K_CR, regs->cr);
110 /* Current page register: points to first free page that can be used for packet reception. */
111 Out_Byte(NE2K_CURR, RX_START_BUFF);
112 cr->ps = 0x00; /* Switch to page 0 */
114 Out_Byte(NE2K_CR, regs->cr);
115 Out_Byte(NE2K_ISR, regs->isr);
117 /* Interrupt mask register: setting a bit to 1 enables the
118 corresponding interrupt in ISR. */
125 Out_Byte(NE2K_IMR, regs->imr);
127 cr->ps = 0x01; /* Switch to reg page 1 */
128 Out_Byte(NE2K_CR, regs->cr);
130 /* Set the physical address of the card to 52:54:00:12:34:58 */
131 Out_Byte(NE2K_CR+0x01, 0x52);
132 Out_Byte(NE2K_CR+0x02, 0x54);
133 Out_Byte(NE2K_CR+0x03, 0x00);
134 Out_Byte(NE2K_CR+0x04, 0x12);
135 Out_Byte(NE2K_CR+0x05, 0x34);
136 Out_Byte(NE2K_CR+0x06, 0x58);
138 /* Set the multicast address register to all 1s; accepts all multicast packets */
140 for(i = 0x08; i <= 0x0f; i++) {
141 Out_Byte(NE2K_CR+i, 0xff);
144 regs->cr = 0x21; //set CR to start value
145 Out_Byte(NE2K_CR, regs->cr);
148 Out_Byte(NE2K_TCR, regs->tcr);
154 rcr->pro = 0x1; /* Promiscuous mode: accept all packets. */
156 Out_Byte(NE2K_RCR, regs->rcr);
158 cr->sta = 0x1; // toggle start bit
160 Out_Byte(NE2K_CR, regs->cr);
166 Out_Byte(NE2K_CR, regs->cr);
170 Out_Byte(NE2K_CR, regs->cr);
174 Out_Byte(NE2K_CR, regs->cr);
177 Install_IRQ(NE2K_IRQ, NE2K_Interrupt_Handler);
178 Enable_IRQ(NE2K_IRQ);
180 for(i = 0; i < 0; i++)
183 PrintBoth("Transmitting a packet\n");
187 uchar_t src_addr[6] = { 0x52, 0x54, 0x00, 0x12, 0x34, 0x58 };
188 uchar_t dest_addr[6] = { 0x52, 0x54, 0x00, 0x12, 0x34, 0x56 };
191 uchar_t *data = Malloc(size);
192 data = "This is a 64-byte string that will be used to test transmission.";
194 for(i = 0; i < 0; i++) {
195 NE2K_Send(regs, src_addr, dest_addr, 0x01, data, size);
198 //Free(data); // Why does this crash?
204 * This function is called when there is data in uip_buf that's ready to be sent.
205 * uip_arp_out() is used to translate the destination IP address to a MAC address.
206 * If the corresponding MAC address isn't in the cache, the packet is replaced with
207 * an ARP packet, which is sent out instead. The original packet will need to be
208 * retransmitted at some point in the future.
210 int NE2K_Transmit(uint_t size)
216 /* Based on example code from the uIP documentation... */
217 if(size <= UIP_LLH_LEN + UIP_TCPIP_HLEN) {
218 memcpy(data, &uip_buf[0], size);
220 memcpy(data, &uip_buf[0], UIP_LLH_LEN + UIP_TCPIP_HLEN);
221 memcpy(data + UIP_LLH_LEN + UIP_TCPIP_HLEN, uip_appdata, size - UIP_TCPIP_HLEN - UIP_LLH_LEN);
224 /* Manually copy in the source MAC address for now. */
225 uchar_t src_addr[6] = { 0x52, 0x54, 0x00, 0x12, 0x34, 0x58 };
226 memcpy(data + 6, src_addr, 6);
227 if(*(data+12) != 0x08 || *(data+13) != 0x06)
229 /* This is not an ARP packet. Fill in te size of the packet manually. */
230 *(data+12) = size & 0xff;
231 *(data+13) = (size >> 8) & 0xff;
234 NE2K_Send_Packet(data, size);
239 int NE2K_Send_Packet(uchar_t *packet, uint_t size)
241 struct _CR * cr = (struct _CR*)&(regs->cr);
242 regs->cr = 0x21; /* Turn off remote DMA, stop command */
243 cr->stp = 0x0; /* toggle start on */
245 Out_Byte(NE2K_CR, regs->cr);
247 // Read-before-write bug fix?
248 Out_Byte(NE2K_RBCR0, 0x42);
249 Out_Byte(NE2K_RBCR1, 0x00);
250 Out_Byte(NE2K_RSAR0, 0x42);
251 Out_Byte(NE2K_RSAR1, 0x00);
253 cr->rd = 0x01; /* set remote DMA to 'remote read' */
254 Out_Byte(NE2K_CR, regs->cr);
256 regs->isr = 0x40; /* clear 'remote DMA complete' interrupt */
257 Out_Byte(NE2K_ISR, regs->isr);
259 /* Set remote byte count registers */
260 Out_Byte(NE2K_RBCR0, size & 0xff);
261 Out_Byte(NE2K_RBCR1, (size >> 8) & 0xff);
263 /* Set transmit byte count registers. */
264 Out_Byte(NE2K_TBCR0, size & 0xff);
265 Out_Byte(NE2K_TBCR1, (size >> 8) & 0xff);
267 /* Set remote start address registers to the first page of the transmit ring buffer. */
268 Out_Byte(NE2K_RSAR0, 0x00);
269 Out_Byte(NE2K_RSAR1, TX_START_BUFF);
271 cr->rd = 0x02; /* Set remote DMA to 'remote write' */
272 Out_Byte(NE2K_CR, regs->cr);
274 /* Push the packet data to into the dataport */
276 for(i = 0; i < size; i += 2) {
277 Out_Word(NE2K_CR + 0x10, (*(packet + i + 1) << 8) | *(packet + i));
280 cr->txp = 0x1; /* Start transmission */
281 Out_Byte(NE2K_CR, regs->cr);
287 /* Assumes src and dest are arrays of 6 characters. */
288 int NE2K_Send(uchar_t src[], uchar_t dest[], uint_t type, uchar_t *data, uint_t size)
290 struct _CR * cr = (struct _CR*)&(regs->cr);
291 uint_t packet_size = size + 16;
292 regs->cr = 0x21; /* Turn off remote DMA, stop command */
293 cr->stp = 0x0; /* toggle start on */
295 Out_Byte(NE2K_CR, regs->cr);
297 // Read-before-write bug fix?
298 Out_Byte(NE2K_RBCR0, 0x42);
299 Out_Byte(NE2K_RBCR1, 0x00);
300 Out_Byte(NE2K_RSAR0, 0x42);
301 Out_Byte(NE2K_RSAR1, 0x00);
303 cr->rd = 0x01; /* set remote DMA to 'remote read' */
304 Out_Byte(NE2K_CR, regs->cr);
306 regs->isr = 0x40; /* clear 'remote DMA complete' interrupt */
307 Out_Byte(NE2K_ISR, regs->isr);
309 /* Set remote byte count registers */
310 Out_Byte(NE2K_RBCR0, packet_size & 0xff);
311 Out_Byte(NE2K_RBCR1, (packet_size >> 8) & 0xff);
313 /* Set transmit byte count registers. */
314 Out_Byte(NE2K_TBCR0, packet_size & 0xff);
315 Out_Byte(NE2K_TBCR1, (packet_size >> 8) & 0xff);
317 /* Set remote start address registers to the first page of the transmit ring buffer. */
318 Out_Byte(NE2K_RSAR0, 0x00);
319 Out_Byte(NE2K_RSAR1, TX_START_BUFF);
321 cr->rd = 0x02; /* Set remote DMA to 'remote write' */
322 Out_Byte(NE2K_CR, regs->cr);
324 /* Begin pushing the packet into the dataport (located at 0x10 from the base address). */
325 /* Destination Address */
326 Out_Word(NE2K_CR + 0x10, (dest[1] << 8) | dest[0]);
327 Out_Word(NE2K_CR + 0x10, (dest[3] << 8) | dest[2]);
328 Out_Word(NE2K_CR + 0x10, (dest[5] << 8) | dest[4]);
331 Out_Word(NE2K_CR + 0x10, (src[1] << 8) | src[0]);
332 Out_Word(NE2K_CR + 0x10, (src[3] << 8) | src[2]);
333 Out_Word(NE2K_CR + 0x10, (src[5] << 8) | src[4]);
336 Out_Word(NE2K_CR + 0x10, packet_size);
340 for(i = 0; i < size; i += 2) {
341 Out_Word(NE2K_CR + 0x10, (*(data + i + 1) << 8) | *(data + i));
344 cr->txp = 0x1; /* Start transmission */
345 Out_Byte(NE2K_CR, regs->cr);
353 PrintBoth("Packet Received\n");
355 Out_Byte(NE2K_CR, 0x22);
356 /* Set RSAR to the start address of the received packet. */
357 Out_Byte(NE2K_RSAR0, next & 0xff);
358 Out_Byte(NE2K_RSAR1, next >> 8);
359 Out_Byte(NE2K_CR, 0x0a);
362 * A four byte header is added to the beginning of each received packet by the NIC.
363 * The first byte is the location of the next packet in the ring buffer.
364 * The second byte is the receive status code.
365 * The third and fourth bytes are the size of the packet.
370 data = In_Word(NE2K_CR + 0x10);
373 PrintBoth("\nPacket data:\n\t");
374 PrintBoth("%x ", data);
377 /* Get the location of the next packet */
378 next = data & 0xff00;
380 /* Retrieve the packet size from the header, and store it in RBCR. */
381 uint_t packet_size = In_Word(NE2K_CR + 0x10) - 4;
382 uchar_t *packet = Malloc(packet_size);
383 Out_Byte(NE2K_RBCR0, packet_size & 0xff);
384 Out_Byte(NE2K_RBCR1, (packet_size>>8) & 0xff);
387 PrintBoth("packetsize = %x\n\t", packet_size);
390 /* Copy the received packet over from the ring buffer. */
391 for(i = 0; i < packet_size; i+=2) {
392 data = In_Word(NE2K_CR + 0x10);
393 *(packet + i) = data & 0x00ff;
394 *(packet + i + 1) = (data & 0xff00) >> 8;
396 PrintBoth("BNRY = %x\n", In_Byte(NE2K_BNRY));
397 Out_Byte(NE2K_CR, 0x4a);
398 PrintBoth("CURR = %x\n", In_Byte(NE2K_CURR));
399 Out_Byte(NE2K_CR, 0x0a);
403 //Out_Byte(NE2K_RBCR0, (In_Byte(NE2K_RBCR0))-2);
404 //Out_Byte(NE2K_RSAR0, (In_Byte(NE2K_RSAR0))+2);
406 PrintBoth("\n%d packets have been received", ++received);
409 Out_Byte(NE2K_ISR, 0x40); /* Clear the remote DMA complete interrupt. */
411 /* The BNRY register stores the location of the first packet that hasn't been read yet */
412 Out_Byte(NE2K_BNRY, next >> 8);
414 struct NE2K_Packet_Info *info = Malloc(sizeof(struct NE2K_Packet_Info));
415 info->size = packet_size;
417 memcpy(info->dest, packet, 6);
418 memcpy(info->src, packet + 6, 6);
419 callbacks.packet_received(info, packet);