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