source: mainline/uspace/srv/hw/netif/ne2000/dp8390.c@ 8436590

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8436590 was 8f17503, checked in by Jakub Jermar <jakub@…>, 15 years ago

When dropping a packet, drop also the sq_mutex, otherwise the next
invocation of ne2k_send() will self-deadlock.

  • Property mode set to 100644
File size: 16.8 KB
RevLine 
[e0854e3]1/*
2 * Copyright (c) 2009 Lukas Mejdrech
3 * Copyright (c) 2011 Martin Decky
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/*
31 * This code is based upon the NE2000 driver for MINIX,
32 * distributed according to a BSD-style license.
33 *
34 * Copyright (c) 1987, 1997, 2006 Vrije Universiteit
35 * Copyright (c) 1992, 1994 Philip Homburg
36 * Copyright (c) 1996 G. Falzoni
37 *
38 */
39
[3c106e88]40/** @addtogroup ne2000
[21580dd]41 * @{
42 */
43
44/** @file
[3c106e88]45 *
46 * NE2000 (based on DP8390) network interface core implementation.
47 * Only the basic NE2000 PIO (ISA) interface is supported, remote
48 * DMA is completely absent from this code for simplicity.
49 *
[21580dd]50 */
51
52#include <assert.h>
[2687bdb]53#include <byteorder.h>
[21580dd]54#include <errno.h>
[3c106e88]55#include <libarch/ddi.h>
[c69d327]56#include <net/packet.h>
[0a866eeb]57#include <packet_client.h>
[21580dd]58#include "dp8390.h"
59
[3c106e88]60/** Page size */
61#define DP_PAGE 256
62
63/** 6 * DP_PAGE >= 1514 bytes */
64#define SQ_PAGES 6
65
66/* NE2000 implementation. */
67
68/** NE2000 Data Register */
69#define NE2K_DATA 0x0010
70
71/** NE2000 Reset register */
72#define NE2K_RESET 0x001f
73
74/** NE2000 data start */
75#define NE2K_START 0x4000
76
77/** NE2000 data size */
78#define NE2K_SIZE 0x4000
79
80/** NE2000 retry count */
[43b4314]81#define NE2K_RETRY 0x1000
[3c106e88]82
83/** NE2000 error messages rate limiting */
84#define NE2K_ERL 10
85
86/** Minimum Ethernet packet size in bytes */
87#define ETH_MIN_PACK_SIZE 60
88
89/** Maximum Ethernet packet size in bytes */
90#define ETH_MAX_PACK_SIZE_TAGGED 1518
91
92/** Type definition of the receive header
[39d70ec]93 *
[21580dd]94 */
[3c106e88]95typedef struct {
96 /** Copy of RSR */
97 uint8_t status;
98
99 /** Pointer to next packet */
100 uint8_t next;
101
102 /** Receive Byte Count Low */
103 uint8_t rbcl;
104
105 /** Receive Byte Count High */
106 uint8_t rbch;
107} recv_header_t;
[21580dd]108
[39d70ec]109/** Read a memory block word by word.
110 *
[3c106e88]111 * @param[in] port Source address.
112 * @param[out] buf Destination buffer.
113 * @param[in] size Memory block size in bytes.
[39d70ec]114 *
[21580dd]115 */
[3c106e88]116static void pio_read_buf_16(void *port, void *buf, size_t size)
[95ff5c4]117{
[3c106e88]118 size_t i;
[95ff5c4]119
[3c106e88]120 for (i = 0; (i << 1) < size; i++)
121 *((uint16_t *) buf + i) = pio_read_16((ioport16_t *) (port));
[21580dd]122}
123
[3c106e88]124/** Write a memory block word by word.
[66b628a]125 *
[3c106e88]126 * @param[in] port Destination address.
127 * @param[in] buf Source buffer.
128 * @param[in] size Memory block size in bytes.
[66b628a]129 *
130 */
[3c106e88]131static void pio_write_buf_16(void *port, void *buf, size_t size)
[95ff5c4]132{
[3c106e88]133 size_t i;
[95ff5c4]134
[3c106e88]135 for (i = 0; (i << 1) < size; i++)
136 pio_write_16((ioport16_t *) port, *((uint16_t *) buf + i));
[21580dd]137}
138
[3c106e88]139static void ne2k_download(ne2k_t *ne2k, void *buf, size_t addr, size_t size)
[95ff5c4]140{
[3c106e88]141 size_t esize = size & ~1;
142
143 pio_write_8(ne2k->port + DP_RBCR0, esize & 0xff);
144 pio_write_8(ne2k->port + DP_RBCR1, (esize >> 8) & 0xff);
145 pio_write_8(ne2k->port + DP_RSAR0, addr & 0xff);
146 pio_write_8(ne2k->port + DP_RSAR1, (addr >> 8) & 0xff);
147 pio_write_8(ne2k->port + DP_CR, CR_DM_RR | CR_PS_P0 | CR_STA);
148
149 if (esize != 0) {
150 pio_read_buf_16(ne2k->data_port, buf, esize);
151 size -= esize;
152 buf += esize;
153 }
154
155 if (size) {
156 assert(size == 1);
[7922dea]157
[3c106e88]158 uint16_t word = pio_read_16(ne2k->data_port);
159 memcpy(buf, &word, 1);
[21580dd]160 }
161}
162
[3c106e88]163static void ne2k_upload(ne2k_t *ne2k, void *buf, size_t addr, size_t size)
[7922dea]164{
[3c106e88]165 size_t esize = size & ~1;
166
167 pio_write_8(ne2k->port + DP_RBCR0, esize & 0xff);
168 pio_write_8(ne2k->port + DP_RBCR1, (esize >> 8) & 0xff);
169 pio_write_8(ne2k->port + DP_RSAR0, addr & 0xff);
170 pio_write_8(ne2k->port + DP_RSAR1, (addr >> 8) & 0xff);
171 pio_write_8(ne2k->port + DP_CR, CR_DM_RW | CR_PS_P0 | CR_STA);
172
173 if (esize != 0) {
174 pio_write_buf_16(ne2k->data_port, buf, esize);
175 size -= esize;
176 buf += esize;
[7922dea]177 }
178
[3c106e88]179 if (size) {
180 assert(size == 1);
[7922dea]181
[3c106e88]182 uint16_t word = 0;
[7922dea]183
[3c106e88]184 memcpy(&word, buf, 1);
185 pio_write_16(ne2k->data_port, word);
[7922dea]186 }
187}
188
[43b4314]189static void ne2k_init(ne2k_t *ne2k)
190{
191 unsigned int i;
192
193 /* Reset the ethernet card */
194 uint8_t val = pio_read_8(ne2k->port + NE2K_RESET);
195 usleep(2000);
196 pio_write_8(ne2k->port + NE2K_RESET, val);
197 usleep(2000);
198
199 /* Reset the DP8390 */
200 pio_write_8(ne2k->port + DP_CR, CR_STP | CR_DM_ABORT);
201 for (i = 0; i < NE2K_RETRY; i++) {
202 if (pio_read_8(ne2k->port + DP_ISR) != 0)
203 break;
204 }
205}
206
[3c106e88]207/** Probe and initialize the network interface.
208 *
209 * @param[in,out] ne2k Network interface structure.
210 * @param[in] port Device address.
211 * @param[in] irq Device interrupt vector.
212 *
213 * @return EOK on success.
214 * @return EXDEV if the network interface was not recognized.
215 *
216 */
217int ne2k_probe(ne2k_t *ne2k, void *port, int irq)
[21580dd]218{
[3c106e88]219 unsigned int i;
[95ff5c4]220
[3c106e88]221 /* General initialization */
222 ne2k->port = port;
223 ne2k->data_port = ne2k->port + NE2K_DATA;
224 ne2k->irq = irq;
225 ne2k->probed = false;
226 ne2k->up = false;
227
[43b4314]228 ne2k_init(ne2k);
[95ff5c4]229
[3c106e88]230 /* Check if the DP8390 is really there */
[43b4314]231 uint8_t val = pio_read_8(ne2k->port + DP_CR);
[3c106e88]232 if ((val & (CR_STP | CR_DM_ABORT)) != (CR_STP | CR_DM_ABORT))
233 return EXDEV;
[39d70ec]234
[3c106e88]235 /* Disable the receiver and init TCR and DCR */
236 pio_write_8(ne2k->port + DP_RCR, RCR_MON);
237 pio_write_8(ne2k->port + DP_TCR, TCR_NORMAL);
238 pio_write_8(ne2k->port + DP_DCR, DCR_WORDWIDE | DCR_8BYTES | DCR_BMS);
[39d70ec]239
[3c106e88]240 /* Setup a transfer to get the MAC address */
241 pio_write_8(ne2k->port + DP_RBCR0, ETH_ADDR << 1);
242 pio_write_8(ne2k->port + DP_RBCR1, 0);
243 pio_write_8(ne2k->port + DP_RSAR0, 0);
244 pio_write_8(ne2k->port + DP_RSAR1, 0);
245 pio_write_8(ne2k->port + DP_CR, CR_DM_RR | CR_PS_P0 | CR_STA);
[95ff5c4]246
[3c106e88]247 for (i = 0; i < ETH_ADDR; i++)
248 ne2k->mac[i] = pio_read_16(ne2k->data_port);
[95ff5c4]249
[43b4314]250 ne2k->probed = true;
251 return EOK;
252}
253
254/** Start the network interface.
255 *
256 * @param[in,out] ne2k Network interface structure.
257 *
258 * @return EOK on success.
259 * @return EXDEV if the network interface is disabled.
260 *
261 */
262int ne2k_up(ne2k_t *ne2k)
263{
264 if (!ne2k->probed)
265 return EXDEV;
266
267 ne2k_init(ne2k);
268
[3c106e88]269 /*
270 * Setup send queue. Use the first
271 * SQ_PAGES of NE2000 memory for the send
272 * buffer.
273 */
274 ne2k->sq.dirty = false;
275 ne2k->sq.page = NE2K_START / DP_PAGE;
276 fibril_mutex_initialize(&ne2k->sq_mutex);
277 fibril_condvar_initialize(&ne2k->sq_cv);
[95ff5c4]278
[3c106e88]279 /*
280 * Setup receive ring buffer. Use all the rest
281 * of the NE2000 memory (except the first SQ_PAGES
282 * reserved for the send buffer) for the receive
283 * ring buffer.
284 */
285 ne2k->start_page = ne2k->sq.page + SQ_PAGES;
286 ne2k->stop_page = ne2k->sq.page + NE2K_SIZE / DP_PAGE;
[d3c9b60]287
288 /*
[3c106e88]289 * Initialization of the DP8390 following the mandatory procedure
[21580dd]290 * in reference manual ("DP8390D/NS32490D NIC Network Interface
291 * Controller", National Semiconductor, July 1995, Page 29).
292 */
[d3c9b60]293
[21580dd]294 /* Step 1: */
[3c106e88]295 pio_write_8(ne2k->port + DP_CR, CR_PS_P0 | CR_STP | CR_DM_ABORT);
[d3c9b60]296
[21580dd]297 /* Step 2: */
[3c106e88]298 pio_write_8(ne2k->port + DP_DCR, DCR_WORDWIDE | DCR_8BYTES | DCR_BMS);
[d3c9b60]299
[21580dd]300 /* Step 3: */
[3c106e88]301 pio_write_8(ne2k->port + DP_RBCR0, 0);
302 pio_write_8(ne2k->port + DP_RBCR1, 0);
[d3c9b60]303
[21580dd]304 /* Step 4: */
[3c106e88]305 pio_write_8(ne2k->port + DP_RCR, RCR_AB);
[d3c9b60]306
[21580dd]307 /* Step 5: */
[3c106e88]308 pio_write_8(ne2k->port + DP_TCR, TCR_INTERNAL);
[d3c9b60]309
[21580dd]310 /* Step 6: */
[3c106e88]311 pio_write_8(ne2k->port + DP_BNRY, ne2k->start_page);
312 pio_write_8(ne2k->port + DP_PSTART, ne2k->start_page);
313 pio_write_8(ne2k->port + DP_PSTOP, ne2k->stop_page);
[d3c9b60]314
[21580dd]315 /* Step 7: */
[3c106e88]316 pio_write_8(ne2k->port + DP_ISR, 0xff);
[d3c9b60]317
[21580dd]318 /* Step 8: */
[3c106e88]319 pio_write_8(ne2k->port + DP_IMR,
320 IMR_PRXE | IMR_PTXE | IMR_RXEE | IMR_TXEE | IMR_OVWE | IMR_CNTE);
[d3c9b60]321
[21580dd]322 /* Step 9: */
[3c106e88]323 pio_write_8(ne2k->port + DP_CR, CR_PS_P1 | CR_DM_ABORT | CR_STP);
324
325 pio_write_8(ne2k->port + DP_PAR0, ne2k->mac[0]);
326 pio_write_8(ne2k->port + DP_PAR1, ne2k->mac[1]);
327 pio_write_8(ne2k->port + DP_PAR2, ne2k->mac[2]);
328 pio_write_8(ne2k->port + DP_PAR3, ne2k->mac[3]);
329 pio_write_8(ne2k->port + DP_PAR4, ne2k->mac[4]);
330 pio_write_8(ne2k->port + DP_PAR5, ne2k->mac[5]);
331
332 pio_write_8(ne2k->port + DP_MAR0, 0xff);
333 pio_write_8(ne2k->port + DP_MAR1, 0xff);
334 pio_write_8(ne2k->port + DP_MAR2, 0xff);
335 pio_write_8(ne2k->port + DP_MAR3, 0xff);
336 pio_write_8(ne2k->port + DP_MAR4, 0xff);
337 pio_write_8(ne2k->port + DP_MAR5, 0xff);
338 pio_write_8(ne2k->port + DP_MAR6, 0xff);
339 pio_write_8(ne2k->port + DP_MAR7, 0xff);
340
341 pio_write_8(ne2k->port + DP_CURR, ne2k->start_page + 1);
[d3c9b60]342
[21580dd]343 /* Step 10: */
[7300c37]344 pio_write_8(ne2k->port + DP_CR, CR_PS_P0 | CR_DM_ABORT | CR_STA);
[d3c9b60]345
[21580dd]346 /* Step 11: */
[3c106e88]347 pio_write_8(ne2k->port + DP_TCR, TCR_NORMAL);
[d3c9b60]348
[3c106e88]349 /* Reset counters by reading */
350 pio_read_8(ne2k->port + DP_CNTR0);
351 pio_read_8(ne2k->port + DP_CNTR1);
352 pio_read_8(ne2k->port + DP_CNTR2);
353
354 /* Finish the initialization */
355 ne2k->up = true;
356 return EOK;
357}
358
359/** Stop the network interface.
360 *
361 * @param[in,out] ne2k Network interface structure.
362 *
363 */
364void ne2k_down(ne2k_t *ne2k)
365{
366 if ((ne2k->probed) && (ne2k->up)) {
367 pio_write_8(ne2k->port + DP_CR, CR_STP | CR_DM_ABORT);
[43b4314]368 ne2k_init(ne2k);
[3c106e88]369 ne2k->up = false;
370 }
[21580dd]371}
372
[3c106e88]373/** Send a frame.
374 *
375 * @param[in,out] ne2k Network interface structure.
376 * @param[in] packet Frame to be sent.
377 *
378 */
379void ne2k_send(ne2k_t *ne2k, packet_t *packet)
[21580dd]380{
[3c106e88]381 assert(ne2k->probed);
382 assert(ne2k->up);
383
384 fibril_mutex_lock(&ne2k->sq_mutex);
[b590c21]385
[3c106e88]386 while (ne2k->sq.dirty)
387 fibril_condvar_wait(&ne2k->sq_cv, &ne2k->sq_mutex);
[b590c21]388
[3c106e88]389 void *buf = packet_get_data(packet);
390 size_t size = packet_get_data_length(packet);
391
392 if ((size < ETH_MIN_PACK_SIZE) || (size > ETH_MAX_PACK_SIZE_TAGGED)) {
[8f17503]393 fibril_mutex_unlock(&ne2k->sq_mutex);
[3c106e88]394 fprintf(stderr, "%s: Frame dropped (invalid size %zu bytes)\n",
395 NAME, size);
396 return;
397 }
[b590c21]398
[3c106e88]399 /* Upload the frame to the ethernet card */
400 ne2k_upload(ne2k, buf, ne2k->sq.page * DP_PAGE, size);
401 ne2k->sq.dirty = true;
402 ne2k->sq.size = size;
403
404 /* Initialize the transfer */
405 pio_write_8(ne2k->port + DP_TPSR, ne2k->sq.page);
406 pio_write_8(ne2k->port + DP_TBCR0, size & 0xff);
407 pio_write_8(ne2k->port + DP_TBCR1, (size >> 8) & 0xff);
408 pio_write_8(ne2k->port + DP_CR, CR_TXP | CR_STA);
409
410 fibril_mutex_unlock(&ne2k->sq_mutex);
[21580dd]411}
412
[3c106e88]413static void ne2k_reset(ne2k_t *ne2k)
[21580dd]414{
[3c106e88]415 unsigned int i;
[b590c21]416
[3c106e88]417 /* Stop the chip */
418 pio_write_8(ne2k->port + DP_CR, CR_STP | CR_DM_ABORT);
419 pio_write_8(ne2k->port + DP_RBCR0, 0);
420 pio_write_8(ne2k->port + DP_RBCR1, 0);
[b590c21]421
[43b4314]422 for (i = 0; i < NE2K_RETRY; i++) {
[3c106e88]423 if ((pio_read_8(ne2k->port + DP_ISR) & ISR_RST) != 0)
424 break;
425 }
[b590c21]426
[3c106e88]427 pio_write_8(ne2k->port + DP_TCR, TCR_1EXTERNAL | TCR_OFST);
428 pio_write_8(ne2k->port + DP_CR, CR_STA | CR_DM_ABORT);
429 pio_write_8(ne2k->port + DP_TCR, TCR_NORMAL);
[b590c21]430
[3c106e88]431 /* Acknowledge the ISR_RDC (remote DMA) interrupt */
[43b4314]432 for (i = 0; i < NE2K_RETRY; i++) {
[3c106e88]433 if ((pio_read_8(ne2k->port + DP_ISR) & ISR_RDC) != 0)
434 break;
435 }
[b590c21]436
[3c106e88]437 uint8_t val = pio_read_8(ne2k->port + DP_ISR);
438 pio_write_8(ne2k->port + DP_ISR, val & ~ISR_RDC);
[b590c21]439
440 /*
[3c106e88]441 * Reset the transmit ring. If we were transmitting a frame,
442 * we pretend that the packet is processed. Higher layers will
[21580dd]443 * retransmit if the packet wasn't actually sent.
444 */
[3c106e88]445 fibril_mutex_lock(&ne2k->sq_mutex);
446 ne2k->sq.dirty = false;
447 fibril_mutex_unlock(&ne2k->sq_mutex);
[21580dd]448}
449
[7300c37]450static frame_t *ne2k_receive_frame(ne2k_t *ne2k, uint8_t page, size_t length)
[3c106e88]451{
[7300c37]452 frame_t *frame = (frame_t *) malloc(sizeof(frame_t));
453 if (frame == NULL)
454 return NULL;
455
456 link_initialize(&frame->link);
457
458 frame->packet = netif_packet_get_1(length);
459 if (frame->packet == NULL) {
460 free(frame);
461 return NULL;
462 }
[3c106e88]463
[7300c37]464 void *buf = packet_suffix(frame->packet, length);
[3c106e88]465 bzero(buf, length);
466 uint8_t last = page + length / DP_PAGE;
467
468 if (last >= ne2k->stop_page) {
469 size_t left = (ne2k->stop_page - page) * DP_PAGE
470 - sizeof(recv_header_t);
471
472 ne2k_download(ne2k, buf, page * DP_PAGE + sizeof(recv_header_t),
473 left);
474 ne2k_download(ne2k, buf + left, ne2k->start_page * DP_PAGE,
475 length - left);
476 } else
477 ne2k_download(ne2k, buf, page * DP_PAGE + sizeof(recv_header_t),
478 length);
479
480 ne2k->stats.receive_packets++;
[7300c37]481 return frame;
[3c106e88]482}
483
[7300c37]484static link_t *ne2k_receive(ne2k_t *ne2k)
[21580dd]485{
[7300c37]486 /*
487 * Allocate memory for the list of received frames.
488 * If the allocation fails here we still receive the
489 * frames from the network, but they will be lost.
490 */
491 link_t *frames = (link_t *) malloc(sizeof(link_t));
492 if (frames != NULL)
493 list_initialize(frames);
494
[3c106e88]495 while (true) {
496 uint8_t boundary = pio_read_8(ne2k->port + DP_BNRY) + 1;
497
498 if (boundary == ne2k->stop_page)
499 boundary = ne2k->start_page;
500
501 pio_write_8(ne2k->port + DP_CR, CR_PS_P1 | CR_STA);
502 uint8_t current = pio_read_8(ne2k->port + DP_CURR);
503 pio_write_8(ne2k->port + DP_CR, CR_PS_P0 | CR_STA);
504
505 if (current == boundary)
506 /* No more frames to process */
507 break;
508
509 recv_header_t header;
510 size_t size = sizeof(header);
511 size_t offset = boundary * DP_PAGE;
512
513 /* Get the frame header */
514 pio_write_8(ne2k->port + DP_RBCR0, size & 0xff);
515 pio_write_8(ne2k->port + DP_RBCR1, (size >> 8) & 0xff);
516 pio_write_8(ne2k->port + DP_RSAR0, offset & 0xff);
517 pio_write_8(ne2k->port + DP_RSAR1, (offset >> 8) & 0xff);
518 pio_write_8(ne2k->port + DP_CR, CR_DM_RR | CR_PS_P0 | CR_STA);
519
520 pio_read_buf_16(ne2k->data_port, (void *) &header, size);
521
522 size_t length =
523 (((size_t) header.rbcl) | (((size_t) header.rbch) << 8)) - size;
524 uint8_t next = header.next;
525
526 if ((length < ETH_MIN_PACK_SIZE)
527 || (length > ETH_MAX_PACK_SIZE_TAGGED)) {
528 fprintf(stderr, "%s: Rant frame (%zu bytes)\n", NAME, length);
529 next = current;
530 } else if ((header.next < ne2k->start_page)
531 || (header.next > ne2k->stop_page)) {
532 fprintf(stderr, "%s: Malformed next frame %u\n", NAME,
533 header.next);
534 next = current;
535 } else if (header.status & RSR_FO) {
536 /*
537 * This is very serious, so we issue a warning and
538 * reset the buffers.
539 */
540 fprintf(stderr, "%s: FIFO overrun\n", NAME);
541 ne2k->overruns++;
542 next = current;
[7300c37]543 } else if ((header.status & RSR_PRX) && (ne2k->up)) {
[bddec0c]544 if (frames != NULL) {
545 frame_t *frame = ne2k_receive_frame(ne2k, boundary, length);
546 if (frame != NULL)
547 list_append(&frame->link, frames);
548 }
[7300c37]549 }
[3c106e88]550
551 /*
552 * Update the boundary pointer
553 * to the value of the page
554 * prior to the next packet to
555 * be processed.
556 */
557 if (next == ne2k->start_page)
558 next = ne2k->stop_page - 1;
559 else
560 next--;
561
562 pio_write_8(ne2k->port + DP_BNRY, next);
563 }
[7300c37]564
565 return frames;
[3c106e88]566}
567
[7300c37]568link_t *ne2k_interrupt(ne2k_t *ne2k, uint8_t isr, uint8_t tsr)
[3c106e88]569{
[7300c37]570 /* List of received frames */
571 link_t *frames = NULL;
572
[abe95c9]573 if (isr & (ISR_PTX | ISR_TXE)) {
574 if (isr & ISR_TXE)
575 ne2k->stats.send_errors++;
576 else {
577 if (tsr & TSR_PTX)
578 ne2k->stats.send_packets++;
[0777f4c5]579
[abe95c9]580 if (tsr & TSR_COL)
581 ne2k->stats.collisions++;
[0777f4c5]582
[abe95c9]583 if (tsr & TSR_ABT)
584 ne2k->stats.send_aborted_errors++;
585
586 if (tsr & TSR_CRS)
587 ne2k->stats.send_carrier_errors++;
588
589 if (tsr & TSR_FU) {
590 ne2k->underruns++;
591 if (ne2k->underruns < NE2K_ERL)
592 fprintf(stderr, "%s: FIFO underrun\n", NAME);
[21580dd]593 }
[0777f4c5]594
[abe95c9]595 if (tsr & TSR_CDH) {
596 ne2k->stats.send_heartbeat_errors++;
597 if (ne2k->stats.send_heartbeat_errors < NE2K_ERL)
598 fprintf(stderr, "%s: CD heartbeat failure\n", NAME);
599 }
600
601 if (tsr & TSR_OWC)
602 ne2k->stats.send_window_errors++;
[21580dd]603 }
[0777f4c5]604
[abe95c9]605 fibril_mutex_lock(&ne2k->sq_mutex);
[0777f4c5]606
[abe95c9]607 if (ne2k->sq.dirty) {
608 /* Prepare the buffer for next packet */
609 ne2k->sq.dirty = false;
610 ne2k->sq.size = 0;
611
612 /* Signal a next frame to be sent */
613 fibril_condvar_broadcast(&ne2k->sq_cv);
614 } else {
615 ne2k->misses++;
616 if (ne2k->misses < NE2K_ERL)
617 fprintf(stderr, "%s: Spurious PTX interrupt\n", NAME);
[21580dd]618 }
[0777f4c5]619
[abe95c9]620 fibril_mutex_unlock(&ne2k->sq_mutex);
621 }
622
623 if (isr & ISR_RXE)
624 ne2k->stats.receive_errors++;
625
626 if (isr & ISR_CNT) {
627 ne2k->stats.receive_crc_errors +=
628 pio_read_8(ne2k->port + DP_CNTR0);
629 ne2k->stats.receive_frame_errors +=
630 pio_read_8(ne2k->port + DP_CNTR1);
631 ne2k->stats.receive_missed_errors +=
632 pio_read_8(ne2k->port + DP_CNTR2);
[21580dd]633 }
[0777f4c5]634
[7300c37]635 if (isr & ISR_PRX)
636 frames = ne2k_receive(ne2k);
637
[abe95c9]638 if (isr & ISR_RST) {
[0777f4c5]639 /*
[66b628a]640 * The chip is stopped, and all arrived
641 * frames are delivered.
[21580dd]642 */
[3c106e88]643 ne2k_reset(ne2k);
[7922dea]644 }
[b590c21]645
[abe95c9]646 /* Unmask interrupts to be processed in the next round */
647 pio_write_8(ne2k->port + DP_IMR,
648 IMR_PRXE | IMR_PTXE | IMR_RXEE | IMR_TXEE | IMR_OVWE | IMR_CNTE);
[7300c37]649
650 return frames;
[21580dd]651}
652
653/** @}
654 */
Note: See TracBrowser for help on using the repository browser.