nif.c revision 1.3.2.2 1 1.3.2.2 bouyer /* $NetBSD: nif.c,v 1.3.2.2 2011/03/06 15:07:55 bouyer Exp $ */
2 1.3.2.2 bouyer
3 1.3.2.2 bouyer /*-
4 1.3.2.2 bouyer * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 1.3.2.2 bouyer * All rights reserved.
6 1.3.2.2 bouyer *
7 1.3.2.2 bouyer * This code is derived from software contributed to The NetBSD Foundation
8 1.3.2.2 bouyer * by Tohru Nishimura.
9 1.3.2.2 bouyer *
10 1.3.2.2 bouyer * Redistribution and use in source and binary forms, with or without
11 1.3.2.2 bouyer * modification, are permitted provided that the following conditions
12 1.3.2.2 bouyer * are met:
13 1.3.2.2 bouyer * 1. Redistributions of source code must retain the above copyright
14 1.3.2.2 bouyer * notice, this list of conditions and the following disclaimer.
15 1.3.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
16 1.3.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
17 1.3.2.2 bouyer * documentation and/or other materials provided with the distribution.
18 1.3.2.2 bouyer *
19 1.3.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.3.2.2 bouyer * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.3.2.2 bouyer * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.3.2.2 bouyer * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.3.2.2 bouyer * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.3.2.2 bouyer * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.3.2.2 bouyer * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.3.2.2 bouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.3.2.2 bouyer * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.3.2.2 bouyer * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.3.2.2 bouyer * POSSIBILITY OF SUCH DAMAGE.
30 1.3.2.2 bouyer */
31 1.3.2.2 bouyer
32 1.3.2.2 bouyer #include <sys/param.h>
33 1.3.2.2 bouyer
34 1.3.2.2 bouyer #include <netinet/in.h>
35 1.3.2.2 bouyer #include <netinet/in_systm.h>
36 1.3.2.2 bouyer
37 1.3.2.2 bouyer #include <lib/libsa/stand.h>
38 1.3.2.2 bouyer #include <lib/libsa/net.h>
39 1.3.2.2 bouyer
40 1.3.2.2 bouyer #include <machine/bootinfo.h>
41 1.3.2.2 bouyer
42 1.3.2.2 bouyer #include "globals.h"
43 1.3.2.2 bouyer
44 1.3.2.2 bouyer struct nifdv {
45 1.3.2.2 bouyer char *name;
46 1.3.2.2 bouyer int (*match)(unsigned, void *);
47 1.3.2.2 bouyer void *(*init)(unsigned, void *);
48 1.3.2.2 bouyer int (*send)(void *, char *, unsigned);
49 1.3.2.2 bouyer int (*recv)(void *, char *, unsigned, unsigned);
50 1.3.2.2 bouyer int (*halt)(void *, int);
51 1.3.2.2 bouyer void *priv;
52 1.3.2.2 bouyer };
53 1.3.2.2 bouyer
54 1.3.2.2 bouyer static struct iodesc netdesc;
55 1.3.2.2 bouyer
56 1.3.2.2 bouyer static struct nifdv lnifdv[] = {
57 1.3.2.2 bouyer { "fxp", fxp_match, fxp_init, fxp_send, fxp_recv },
58 1.3.2.2 bouyer { "tlp", tlp_match, tlp_init, tlp_send, tlp_recv },
59 1.3.2.2 bouyer { "re", rge_match, rge_init, rge_send, rge_recv },
60 1.3.2.2 bouyer { "sk", skg_match, skg_init, skg_send, skg_recv },
61 1.3.2.2 bouyer };
62 1.3.2.2 bouyer static int nnifdv = sizeof(lnifdv)/sizeof(lnifdv[0]);
63 1.3.2.2 bouyer
64 1.3.2.2 bouyer int
65 1.3.2.2 bouyer netif_init(void *self)
66 1.3.2.2 bouyer {
67 1.3.2.2 bouyer struct pcidev *pci = self;
68 1.3.2.2 bouyer struct iodesc *s;
69 1.3.2.2 bouyer struct nifdv *dv;
70 1.3.2.2 bouyer unsigned tag;
71 1.3.2.2 bouyer int n;
72 1.3.2.2 bouyer uint8_t enaddr[6];
73 1.3.2.2 bouyer extern struct btinfo_net bi_net;
74 1.3.2.2 bouyer extern struct btinfo_rootdevice bi_rdev;
75 1.3.2.2 bouyer
76 1.3.2.2 bouyer tag = pci->bdf;
77 1.3.2.2 bouyer for (n = 0; n < nnifdv; n++) {
78 1.3.2.2 bouyer dv = &lnifdv[n];
79 1.3.2.2 bouyer if ((*dv->match)(tag, NULL) > 0)
80 1.3.2.2 bouyer goto found;
81 1.3.2.2 bouyer }
82 1.3.2.2 bouyer return 0;
83 1.3.2.2 bouyer found:
84 1.3.2.2 bouyer pci->drv = dv->priv = (*dv->init)(tag, enaddr);
85 1.3.2.2 bouyer s = &netdesc;
86 1.3.2.2 bouyer s->io_netif = dv;
87 1.3.2.2 bouyer memcpy(s->myea, enaddr, sizeof(s->myea));
88 1.3.2.2 bouyer /* build btinfo to identify NIF device */
89 1.3.2.2 bouyer snprintf(bi_net.devname, sizeof(bi_net.devname), dv->name);
90 1.3.2.2 bouyer memcpy(bi_net.mac_address, enaddr, sizeof(bi_net.mac_address));
91 1.3.2.2 bouyer bi_net.cookie = tag;
92 1.3.2.2 bouyer snprintf(bi_rdev.devname, sizeof(bi_rdev.devname), dv->name);
93 1.3.2.2 bouyer bi_rdev.cookie = tag;
94 1.3.2.2 bouyer return 1;
95 1.3.2.2 bouyer }
96 1.3.2.2 bouyer
97 1.3.2.2 bouyer int
98 1.3.2.2 bouyer netif_open(void *cookie)
99 1.3.2.2 bouyer {
100 1.3.2.2 bouyer /* single action */
101 1.3.2.2 bouyer return 0;
102 1.3.2.2 bouyer }
103 1.3.2.2 bouyer
104 1.3.2.2 bouyer int
105 1.3.2.2 bouyer netif_close(int sock)
106 1.3.2.2 bouyer {
107 1.3.2.2 bouyer /* nothing to do for the HW */
108 1.3.2.2 bouyer return 0;
109 1.3.2.2 bouyer }
110 1.3.2.2 bouyer
111 1.3.2.2 bouyer /*
112 1.3.2.2 bouyer * Send a packet. The ether header is already there.
113 1.3.2.2 bouyer * Return the length sent (or -1 on error).
114 1.3.2.2 bouyer */
115 1.3.2.2 bouyer ssize_t
116 1.3.2.2 bouyer netif_put(struct iodesc *desc, void *pkt, size_t len)
117 1.3.2.2 bouyer {
118 1.3.2.2 bouyer struct nifdv *dv = desc->io_netif;
119 1.3.2.2 bouyer
120 1.3.2.2 bouyer return dv ? (*dv->send)(dv->priv, pkt, len) : -1;
121 1.3.2.2 bouyer }
122 1.3.2.2 bouyer
123 1.3.2.2 bouyer /*
124 1.3.2.2 bouyer * Receive a packet, including the ether header.
125 1.3.2.2 bouyer * Return the total length received (or -1 on error).
126 1.3.2.2 bouyer */
127 1.3.2.2 bouyer ssize_t
128 1.3.2.2 bouyer netif_get(struct iodesc *desc, void *pkt, size_t maxlen, saseconds_t timo)
129 1.3.2.2 bouyer {
130 1.3.2.2 bouyer struct nifdv *dv = desc->io_netif;
131 1.3.2.2 bouyer int len;
132 1.3.2.2 bouyer
133 1.3.2.2 bouyer len = dv ? (*dv->recv)(dv->priv, pkt, maxlen, timo) : -1;
134 1.3.2.2 bouyer if (len == -1)
135 1.3.2.2 bouyer printf("timeout\n");
136 1.3.2.2 bouyer return len;
137 1.3.2.2 bouyer }
138 1.3.2.2 bouyer
139 1.3.2.2 bouyer struct iodesc *
140 1.3.2.2 bouyer socktodesc(int num)
141 1.3.2.2 bouyer {
142 1.3.2.2 bouyer
143 1.3.2.2 bouyer return (num == 0) ? &netdesc : NULL;
144 1.3.2.2 bouyer }
145