Home | History | Annotate | Line # | Download | only in altboot
nif.c revision 1.4
      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 
     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 	{ "stge", stg_match, stg_init, stg_send, stg_recv },
     62 };
     63 static int nnifdv = sizeof(lnifdv)/sizeof(lnifdv[0]);
     64 
     65 int
     66 netif_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 	return 0;
     84   found:
     85 	pci->drv = dv->priv = (*dv->init)(tag, enaddr);
     86 	s = &netdesc;
     87 	s->io_netif = dv;
     88 	memcpy(s->myea, enaddr, sizeof(s->myea));
     89 	/* build btinfo to identify NIF device */
     90 	snprintf(bi_net.devname, sizeof(bi_net.devname), dv->name);
     91 	memcpy(bi_net.mac_address, enaddr, sizeof(bi_net.mac_address));
     92 	bi_net.cookie = tag;
     93 	snprintf(bi_rdev.devname, sizeof(bi_rdev.devname), dv->name);
     94 	bi_rdev.cookie = tag;
     95 	return 1;
     96 }
     97 
     98 int
     99 netif_open(void *cookie)
    100 {
    101 	/* single action */
    102 	return 0;
    103 }
    104 
    105 int
    106 netif_close(int sock)
    107 {
    108 	/* nothing to do for the HW */
    109 	return 0;
    110 }
    111 
    112 /*
    113  * Send a packet.  The ether header is already there.
    114  * Return the length sent (or -1 on error).
    115  */
    116 ssize_t
    117 netif_put(struct iodesc *desc, void *pkt, size_t len)
    118 {
    119 	struct nifdv *dv = desc->io_netif;
    120 
    121 	return dv ? (*dv->send)(dv->priv, pkt, len) : -1;
    122 }
    123 
    124 /*
    125  * Receive a packet, including the ether header.
    126  * Return the total length received (or -1 on error).
    127  */
    128 ssize_t
    129 netif_get(struct iodesc *desc, void *pkt, size_t maxlen, saseconds_t timo)
    130 {
    131 	struct nifdv *dv = desc->io_netif;
    132 	int len;
    133 
    134 	len = dv ? (*dv->recv)(dv->priv, pkt, maxlen, timo) : -1;
    135 	if (len == -1)
    136 		printf("timeout\n");
    137 	return len;
    138 }
    139 
    140 struct iodesc *
    141 socktodesc(int num)
    142 {
    143 
    144 	return (num == 0) ? &netdesc : NULL;
    145 }
    146