Home | History | Annotate | Line # | Download | only in altboot
nif.c revision 1.1
      1 /* $NetBSD: nif.c,v 1.1 2011/01/23 01:05:30 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(unsigned tag)
     66 {
     67 	struct iodesc *s;
     68 	struct nifdv *dv;
     69 	int n;
     70 	uint8_t enaddr[6];
     71 	extern struct btinfo_net bi_net;
     72 	extern struct btinfo_rootdevice bi_rdev;
     73 
     74 	for (n = 0; n < nnifdv; n++) {
     75 		dv = &lnifdv[n];
     76 		if ((*dv->match)(tag, NULL) > 0)
     77 			goto found;
     78 	}
     79 	return 0;
     80   found:
     81 	dv->priv = (*dv->init)(tag, enaddr);
     82 	s = &netdesc;
     83 	s->io_netif = dv;
     84 	memcpy(s->myea, enaddr, sizeof(s->myea));
     85 
     86 	/* build btinfo to identify NIF device */
     87 	snprintf(bi_net.devname, sizeof(bi_net.devname), dv->name);
     88 	memcpy(bi_net.mac_address, enaddr, sizeof(bi_net.mac_address));
     89 	bi_net.cookie = tag;
     90 	snprintf(bi_rdev.devname, sizeof(bi_rdev.devname), dv->name);
     91 	bi_rdev.cookie = tag;
     92 	return 1;
     93 }
     94 
     95 int
     96 netif_open(void *cookie)
     97 {
     98 	/* single action */
     99 	return 0;
    100 }
    101 
    102 int
    103 netif_close(int sock)
    104 {
    105 	/* nothing to do for the HW */
    106 	return 0;
    107 }
    108 
    109 /*
    110  * Send a packet.  The ether header is already there.
    111  * Return the length sent (or -1 on error).
    112  */
    113 ssize_t
    114 netif_put(struct iodesc *desc, void *pkt, size_t len)
    115 {
    116 	struct nifdv *dv = desc->io_netif;
    117 
    118 	return dv ? (*dv->send)(dv->priv, pkt, len) : -1;
    119 }
    120 
    121 /*
    122  * Receive a packet, including the ether header.
    123  * Return the total length received (or -1 on error).
    124  */
    125 ssize_t
    126 netif_get(struct iodesc *desc, void *pkt, size_t maxlen, saseconds_t timo)
    127 {
    128 	struct nifdv *dv = desc->io_netif;
    129 	int len;
    130 
    131 	len = dv ? (*dv->recv)(dv->priv, pkt, maxlen, timo) : -1;
    132 	if (len == -1)
    133 		printf("timeout\n");
    134 	return len;
    135 }
    136 
    137 struct iodesc *
    138 socktodesc(int num)
    139 {
    140 
    141 	return (num == 0) ? &netdesc : NULL;
    142 }
    143