1/* $NetBSD: nif.c,v 1.4 2011/03/06 20:36:29 phx 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
44struct 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	void (*shutdown)(void *);
51	void *priv;
52};
53
54static struct iodesc netdesc;
55
56static 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	{ "stge", stg_match, stg_init, stg_send, stg_recv, stg_shutdown },
62};
63static int nnifdv = sizeof(lnifdv)/sizeof(lnifdv[0]);
64
65int
66netif_init(void *self)
67{
68	struct pcidev *pci = self;
69	struct iodesc *s;
70	struct nifdv *dv;
71	unsigned tag;
72	int n;
73	uint8_t enaddr[6];
74	extern struct btinfo_net bi_net;
75	extern struct btinfo_rootdevice bi_rdev;
76
77	tag = pci->bdf;
78	for (n = 0; n < nnifdv; n++) {
79		dv = &lnifdv[n];
80		if ((*dv->match)(tag, NULL) > 0)
81			goto found;
82	}
83	pci->drv = NULL;
84	return 0;
85  found:
86	pci->drv = dv->priv = (*dv->init)(tag, enaddr);
87	s = &netdesc;
88	s->io_netif = dv;
89	memcpy(s->myea, enaddr, sizeof(s->myea));
90	/* build btinfo to identify NIF device */
91	snprintf(bi_net.devname, sizeof(bi_net.devname), dv->name);
92	memcpy(bi_net.mac_address, enaddr, sizeof(bi_net.mac_address));
93	bi_net.cookie = tag;
94	snprintf(bi_rdev.devname, sizeof(bi_rdev.devname), dv->name);
95	bi_rdev.cookie = tag;
96	return 1;
97}
98
99void
100netif_shutdown_all(void)
101{
102	struct nifdv *dv;
103
104	/* currently there can be only one NIF */
105	dv = netdesc.io_netif;
106	if (dv != NULL)
107		if (dv->shutdown != NULL)
108			(*dv->shutdown)(dv->priv);
109	memset(&netdesc, 0, sizeof(netdesc));
110}
111
112int
113netif_open(void *cookie)
114{
115
116	/* single action */
117	return 0;
118}
119
120int
121netif_close(int sock)
122{
123
124	/* nothing to do for the HW */
125	return 0;
126}
127
128/*
129 * Send a packet.  The ether header is already there.
130 * Return the length sent (or -1 on error).
131 */
132ssize_t
133netif_put(struct iodesc *desc, void *pkt, size_t len)
134{
135	struct nifdv *dv = desc->io_netif;
136
137	return dv ? (*dv->send)(dv->priv, pkt, len) : -1;
138}
139
140/*
141 * Receive a packet, including the ether header.
142 * Return the total length received (or -1 on error).
143 */
144ssize_t
145netif_get(struct iodesc *desc, void *pkt, size_t maxlen, saseconds_t timo)
146{
147	struct nifdv *dv = desc->io_netif;
148	int len;
149
150	len = dv ? (*dv->recv)(dv->priv, pkt, maxlen, timo) : -1;
151	if (len == -1)
152		printf("timeout\n");
153	return len;
154}
155
156struct iodesc *
157socktodesc(int num)
158{
159
160	return (num == 0) ? &netdesc : NULL;
161}
162