summaryrefslogtreecommitdiff
path: root/sys/arch/mvmeppc/stand/libsa/if_bug.c
blob: 3db1794da16ee74671687a7553c449aefc5afe69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*	$NetBSD: if_bug.c,v 1.4 2009/03/14 15:36:11 dsl Exp $	*/

/*-
 * Copyright (c) 2002 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Steve C. Woodford.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/param.h>
#include <sys/types.h>
#include <sys/socket.h>

#include <netinet/in.h>
#include <netinet/in_systm.h>

#include <net/if.h>
#include <net/if_ether.h>

#include <lib/libkern/libkern.h>
#include <lib/libsa/stand.h>
#include <lib/libsa/net.h>
#include <lib/libsa/netif.h>

#include "libsa.h"
#include "bugsyscalls.h"

static int	bug_match(struct netif *, void *);
static int	bug_probe(struct netif *, void *);
static void	bug_init(struct iodesc *, void *);
static int	bug_get(struct iodesc *, void *, size_t, saseconds_t);
static int	bug_put(struct iodesc *, void *, size_t);
static void	bug_end(struct netif *);

static struct netif_stats bug_stats;

static struct netif_dif bug0_dif = {
	0, 1, &bug_stats, 0, 0
};

struct netif_driver bug_driver = {
	"net",			/* netif_bname */
	bug_match,		/* match */
	bug_probe,		/* probe */
	bug_init,		/* init */
	bug_get,		/* get */
	bug_put,		/* put */
	bug_end,		/* end */
	&bug0_dif,		/* netif_ifs */
	1,			/* netif_nifs */
};

struct bug_softc {
	u_int32_t	sc_pad1;
	u_int8_t	sc_rxbuf[ETHER_MAX_LEN];
	u_int32_t	sc_pad2;
	u_int8_t	sc_txbuf[ETHER_MAX_LEN];
};

static struct bug_softc bug_softc;

int
bug_match(struct netif *nif, void *machdep_hint)
{

	if (machdep_hint &&
	    memcmp(bug_driver.netif_bname, machdep_hint,
	           strlen(bug_driver.netif_bname)) == 0)
		return (1);

	return (0);
}

int
bug_probe(struct netif *nif, void *machdep_hint)
{

	return (0);
}

void
bug_init(struct iodesc *desc, void *machdep_hint)
{
	struct netif *nif = desc->io_netif;
	struct bug_netio nio;

	nio.nc_clun = 0;
	nio.nc_dlun = 0;
	nio.nc_status = 0;
	nio.nc_command = BUG_NETIO_CMD_GET_MAC;
	nio.nc_buffer = desc->myea;
	nio.nc_length = 6;
	nio.nc_csr = 0;

	if (bugsys_netio(&nio) != 0 || nio.nc_status != 0)
		panic("bug_init: Failed to get MAC address! (code 0x%x)",
		    nio.nc_status);

	nio.nc_clun = 0;
	nio.nc_dlun = 0;
	nio.nc_status = 0;
	nio.nc_command = BUG_NETIO_CMD_FLUSH;
	nio.nc_buffer = NULL;
	nio.nc_length = 0;
	nio.nc_csr = 0;

	if (bugsys_netio(&nio) != 0 || nio.nc_status != 0)
		panic("bug_init: Failed to flush netio device (code 0x%x)",
		    nio.nc_status);

	printf("network: %s%d attached to %s\n", nif->nif_driver->netif_bname,
	    nif->nif_unit, ether_sprintf(desc->myea));

	nif->nif_devdata = &bug_softc;
}

int
bug_get(struct iodesc *desc, void *pkt, size_t len, saseconds_t timeout)
{
	struct netif *nif = desc->io_netif;
	struct bug_softc *sc = nif->nif_devdata;
	struct bug_netio nio;

	nio.nc_clun = 0;
	nio.nc_dlun = 0;
	nio.nc_status = 0;
	nio.nc_command = BUG_NETIO_CMD_RECEIVE;
	nio.nc_buffer = sc->sc_rxbuf;
	nio.nc_length = ETHER_MAX_LEN;
	nio.nc_csr = 0;

	if (bugsys_netio(&nio) != 0 || nio.nc_status != 0) {
		printf("bug_get: Receive packet failed (code: 0x%x)\n",
		    nio.nc_status);
		return (0);
	}

	if (nio.nc_length) {
		memcpy(pkt, sc->sc_rxbuf, MIN(len, nio.nc_length));
		return (MIN(len, nio.nc_length));
	}

	return (0);
}

int
bug_put(struct iodesc *desc, void *pkt, size_t len)
{
	struct netif *nif = desc->io_netif;
	struct bug_softc *sc = nif->nif_devdata;
	struct bug_netio nio;

	memcpy(&sc->sc_txbuf, pkt, len);

	nio.nc_clun = 0;
	nio.nc_dlun = 0;
	nio.nc_status = 0;
	nio.nc_command = BUG_NETIO_CMD_TRANSMIT;
	nio.nc_buffer = sc->sc_txbuf;
	nio.nc_length = MAX(len, ETHER_MIN_LEN);
	nio.nc_csr = 0;

	if (bugsys_netio(&nio) != 0 || nio.nc_status != 0) {
		printf("bug_put: Send packet failed (code: 0x%x)\n",
		    nio.nc_status);
		return (0);
	}

	return (len);
}

void
bug_end(struct netif *nif)
{
	struct bug_netio nio;

	nio.nc_clun = 0;
	nio.nc_dlun = 0;
	nio.nc_status = 0;
	nio.nc_command = BUG_NETIO_CMD_FLUSH;
	nio.nc_buffer = NULL;
	nio.nc_length = 0;
	nio.nc_csr = 0;

	if (bugsys_netio(&nio) != 0 || nio.nc_status != 0)
		printf("bug_end: netio failed (code: 0x%x)\n", nio.nc_status);
}