if_prom.c revision 1.4.8.2       1  1.4.8.2     skrll /*      $NetBSD: if_prom.c,v 1.4.8.2 2009/04/28 07:34:33 skrll Exp $ */
      2      1.1  gmcgarry 
      3      1.1  gmcgarry /* Copyright (c) 1999 The NetBSD Foundation, Inc.
      4      1.1  gmcgarry  * All rights reserved.
      5      1.1  gmcgarry  *
      6      1.1  gmcgarry  * This code is derived from software contributed to The NetBSD Foundation
      7      1.1  gmcgarry  * by Gregory McGarry.
      8      1.1  gmcgarry  *
      9      1.1  gmcgarry  * Redistribution and use in source and binary forms, with or without
     10      1.1  gmcgarry  * modification, are permitted provided that the following conditions
     11      1.1  gmcgarry  * are met:
     12      1.1  gmcgarry  * 1. Redistributions of source code must retain the above copyright
     13      1.1  gmcgarry  *    notice, this list of conditions and the following disclaimer.
     14      1.1  gmcgarry  * 2. Redistributions in binary form must reproduce the above copyright
     15      1.1  gmcgarry  *    notice, this list of conditions and the following disclaimer in the
     16      1.1  gmcgarry  *    documentation and/or other materials provided with the distribution.
     17      1.1  gmcgarry  *
     18      1.1  gmcgarry  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19      1.1  gmcgarry  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20      1.1  gmcgarry  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21      1.1  gmcgarry  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22      1.1  gmcgarry  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23      1.1  gmcgarry  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24      1.1  gmcgarry  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25      1.1  gmcgarry  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26      1.1  gmcgarry  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27      1.1  gmcgarry  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28      1.1  gmcgarry  * POSSIBILITY OF SUCH DAMAGE.
     29      1.1  gmcgarry  */
     30      1.1  gmcgarry 
     31      1.1  gmcgarry #include <sys/param.h>
     32      1.1  gmcgarry #include <sys/types.h>
     33      1.1  gmcgarry 
     34      1.1  gmcgarry #include <net/if_ether.h>
     35      1.1  gmcgarry #include <netinet/in.h>
     36      1.1  gmcgarry #include <netinet/in_systm.h>
     37      1.1  gmcgarry #include <netinet/ip.h>
     38      1.1  gmcgarry 
     39      1.1  gmcgarry #include <lib/libsa/stand.h>
     40      1.1  gmcgarry #include <lib/libsa/net.h>
     41      1.1  gmcgarry #include <lib/libsa/netif.h>
     42      1.3   tsutsui #include <lib/libsa/dev_net.h>
     43      1.1  gmcgarry #include <lib/libkern/libkern.h>
     44      1.1  gmcgarry 
     45      1.1  gmcgarry #include <machine/dec_prom.h>
     46      1.1  gmcgarry #include <stand/common/common.h>
     47      1.1  gmcgarry 
     48      1.1  gmcgarry #ifdef NET_DEBUG
     49  1.4.8.2     skrll void dump_packet_info(void *, int);
     50      1.1  gmcgarry #endif
     51      1.1  gmcgarry 
     52      1.1  gmcgarry /*
     53      1.1  gmcgarry  *  For some reason the proms won't pass arp responses back to us.  I
     54      1.1  gmcgarry  *  have checked if the first parameter to bootread/bootwrite do anything
     55      1.1  gmcgarry  *  but it doesn't appear so.  Therefore, we stop the upper layers from
     56      1.1  gmcgarry  *  sending arp requests in the first place, by monitoring packets which
     57      1.1  gmcgarry  *  come in and filling the arp cache ourselves.  - gmcgarry
     58      1.1  gmcgarry  */
     59      1.1  gmcgarry #ifdef FILL_ARPCACHE
     60      1.1  gmcgarry struct arp_list {
     61      1.1  gmcgarry 	struct in_addr	addr;
     62      1.1  gmcgarry 	u_char		ea[6];
     63      1.1  gmcgarry };
     64      1.1  gmcgarry extern struct arp_list arp_list[8];
     65      1.1  gmcgarry extern int arp_num;
     66  1.4.8.2     skrll void fill_arpcache(void *, int);
     67      1.1  gmcgarry #endif
     68      1.1  gmcgarry 
     69      1.1  gmcgarry /* forward declarations */
     70  1.4.8.2     skrll int prom_probe(struct netif *, void *);
     71  1.4.8.2     skrll int prom_match(struct netif *, void *);
     72  1.4.8.2     skrll void prom_init(struct iodesc *, void *);
     73  1.4.8.2     skrll int prom_get(struct iodesc *, void *, size_t, saseconds_t);
     74  1.4.8.2     skrll int prom_put(struct iodesc *, void *, size_t);
     75  1.4.8.2     skrll void prom_end(struct netif *);
     76      1.1  gmcgarry 
     77      1.1  gmcgarry extern struct netif_stats       prom_stats[];
     78      1.1  gmcgarry struct netif_dif prom_ifs[] = {
     79      1.1  gmcgarry /*	dif_unit	dif_nsel	dif_stats	dif_private	*/
     80      1.1  gmcgarry {	0,		1,		&prom_stats[0],	0,		},
     81      1.1  gmcgarry };
     82      1.2  drochner #define NPROM_IFS (sizeof(prom_ifs) / sizeof(prom_ifs[0]))
     83      1.2  drochner struct netif_stats prom_stats[NPROM_IFS];
     84      1.1  gmcgarry 
     85      1.1  gmcgarry struct netif_driver prom_netif_driver = {
     86      1.1  gmcgarry 	"prom",				/* netif_bname */
     87      1.1  gmcgarry 	prom_match,			/* netif_match */
     88      1.1  gmcgarry 	prom_probe,			/* netif_probe */
     89      1.1  gmcgarry 	prom_init,			/* netif_init */
     90      1.1  gmcgarry 	prom_get,			/* netif_get */
     91      1.1  gmcgarry 	prom_put,			/* netif_put */
     92      1.1  gmcgarry 	prom_end,			/* netif_end */
     93      1.1  gmcgarry 	prom_ifs,			/* netif_ifs */
     94      1.2  drochner 	NPROM_IFS			/* netif_nifs */
     95      1.1  gmcgarry };
     96      1.1  gmcgarry 
     97      1.1  gmcgarry static int sc_fd;				/* PROM file id */
     98      1.1  gmcgarry 
     99      1.1  gmcgarry int
    100  1.4.8.2     skrll prom_match(struct netif *nif, void *machdep_hint)
    101      1.1  gmcgarry {
    102      1.1  gmcgarry 
    103      1.1  gmcgarry #ifdef NET_DEBUG
    104      1.1  gmcgarry 	printf("prom_match: called\n");
    105      1.1  gmcgarry #endif
    106      1.1  gmcgarry 	return (1);
    107      1.1  gmcgarry }
    108      1.1  gmcgarry 
    109      1.1  gmcgarry 
    110      1.1  gmcgarry int
    111  1.4.8.2     skrll prom_probe(struct netif *nif, void *machdep_hint)
    112      1.1  gmcgarry {
    113      1.1  gmcgarry 
    114      1.1  gmcgarry #ifdef NET_DEBUG
    115      1.1  gmcgarry 	printf("prom_probe: called\n");
    116      1.1  gmcgarry #endif
    117      1.1  gmcgarry 	return 0;
    118      1.1  gmcgarry }
    119      1.1  gmcgarry 
    120      1.1  gmcgarry 
    121      1.1  gmcgarry void
    122  1.4.8.2     skrll prom_init(struct iodesc *desc, void *machdep_hint)
    123      1.1  gmcgarry {
    124      1.2  drochner 	char *device =
    125      1.2  drochner 		((struct netif *)desc->io_netif)->nif_driver->netif_bname;
    126      1.1  gmcgarry 	char *c, *enet;
    127      1.1  gmcgarry 	int i, j, num;
    128      1.1  gmcgarry 
    129      1.1  gmcgarry #ifdef NET_DEBUG
    130      1.1  gmcgarry 	printf("prom_init: called\n");
    131      1.1  gmcgarry #endif
    132      1.1  gmcgarry 
    133      1.1  gmcgarry 	try_bootp = 1;
    134      1.1  gmcgarry 
    135      1.1  gmcgarry 	/*
    136      1.1  gmcgarry 	 * Get our hardware address (this prom call is one of the rare ones
    137      1.1  gmcgarry          * which is the same for new and old proms)
    138      1.1  gmcgarry          */
    139      1.1  gmcgarry 	enet = (*callv->_getenv)("enet");
    140      1.1  gmcgarry 
    141      1.1  gmcgarry #ifdef NET_DEBUG
    142      1.1  gmcgarry 	if (debug)
    143      1.1  gmcgarry 		printf("enet=%s\n", enet);
    144      1.1  gmcgarry #endif
    145      1.1  gmcgarry 
    146      1.1  gmcgarry 	i=0;
    147      1.1  gmcgarry 	c = enet;
    148      1.1  gmcgarry 	for (i=0; i<6; i++) {
    149      1.1  gmcgarry 		j = *c - '0';
    150      1.1  gmcgarry 		num = (j<10?j:j-39);
    151      1.1  gmcgarry 		num <<= 4;
    152      1.1  gmcgarry 		c++;
    153      1.1  gmcgarry 		j = *c - '0';
    154      1.1  gmcgarry 		num += (j<10?j:j-39);
    155      1.1  gmcgarry 		desc->myea[i] = num;
    156      1.1  gmcgarry 		c++;
    157      1.1  gmcgarry 		c++; /* skip '-' */
    158      1.1  gmcgarry 	}
    159      1.1  gmcgarry 
    160      1.1  gmcgarry 	desc->xid = 0x66d30000;
    161      1.1  gmcgarry 
    162      1.1  gmcgarry 	if (callv == &callvec)
    163      1.1  gmcgarry 		sc_fd = prom_open(device, 0);
    164      1.1  gmcgarry 	else
    165      1.1  gmcgarry 		sc_fd = (*callv->_bootinit)(device);
    166      1.1  gmcgarry 
    167      1.1  gmcgarry 	if (sc_fd < 0)
    168      1.1  gmcgarry 		printf("problem initialising device\n");
    169      1.1  gmcgarry }
    170      1.1  gmcgarry 
    171      1.1  gmcgarry 
    172      1.1  gmcgarry int
    173  1.4.8.2     skrll prom_put(struct iodesc *desc, void *pkt, size_t len)
    174      1.1  gmcgarry {
    175      1.1  gmcgarry 	int s;
    176      1.1  gmcgarry 
    177      1.1  gmcgarry #ifdef NET_DEBUG
    178      1.1  gmcgarry 	printf("prom_put: called\n");
    179      1.1  gmcgarry #endif
    180      1.1  gmcgarry 
    181      1.1  gmcgarry #ifdef NET_DEBUG
    182      1.1  gmcgarry 	if (debug)
    183      1.1  gmcgarry 		dump_packet_info(pkt,len);
    184      1.1  gmcgarry #endif
    185      1.1  gmcgarry 
    186      1.1  gmcgarry 	if (callv == &callvec)
    187      1.1  gmcgarry 		s = prom_write(sc_fd, pkt, len);
    188      1.1  gmcgarry 	else {
    189      1.1  gmcgarry 		s = (*callv->_bootwrite)(0, pkt, len);
    190      1.1  gmcgarry 		(*callv->_wbflush)(); /* didn't really make a difference */
    191      1.1  gmcgarry 	}
    192      1.1  gmcgarry 	if (s < 0)
    193      1.1  gmcgarry 		return (EIO);
    194      1.1  gmcgarry 	return s;
    195      1.1  gmcgarry }
    196      1.1  gmcgarry 
    197      1.1  gmcgarry 
    198      1.1  gmcgarry int
    199  1.4.8.2     skrll prom_get(struct iodesc *desc, void *pkt, size_t len, saseconds_t timeout)
    200      1.1  gmcgarry {
    201      1.1  gmcgarry 	int s;
    202  1.4.8.1     skrll 	satime_t t;
    203      1.1  gmcgarry 
    204      1.1  gmcgarry #ifdef NET_DEBUG
    205      1.1  gmcgarry 	printf("prom_get: called\n");
    206      1.1  gmcgarry #endif
    207      1.1  gmcgarry 
    208      1.1  gmcgarry 	t = getsecs();
    209      1.1  gmcgarry 	s = 0;
    210      1.1  gmcgarry 	while (((getsecs() - t) < timeout) && !s) {
    211      1.1  gmcgarry 		if (callv == &callvec)
    212      1.1  gmcgarry 			s = prom_read(sc_fd, pkt, len);
    213      1.1  gmcgarry 		else
    214      1.1  gmcgarry 			s = (*callv->_bootread)(0, pkt, len);
    215      1.1  gmcgarry 	}
    216      1.1  gmcgarry 
    217      1.1  gmcgarry #ifdef FILL_ARPCACHE
    218      1.1  gmcgarry 	if (s > 0)
    219      1.1  gmcgarry 		fill_arpcache(pkt,s);
    220      1.1  gmcgarry #endif
    221      1.1  gmcgarry 
    222      1.1  gmcgarry 	return s;
    223      1.1  gmcgarry 
    224      1.1  gmcgarry }
    225      1.1  gmcgarry 
    226      1.1  gmcgarry 
    227      1.1  gmcgarry void
    228  1.4.8.2     skrll prom_end(struct netif *nif)
    229      1.1  gmcgarry {
    230      1.1  gmcgarry 
    231      1.1  gmcgarry #ifdef NET_DEBUG
    232      1.1  gmcgarry 	printf("prom_end: called\n");
    233      1.1  gmcgarry #endif
    234      1.1  gmcgarry 
    235      1.1  gmcgarry 	if (callv == &callvec)
    236      1.1  gmcgarry 		prom_close(sc_fd);
    237      1.1  gmcgarry }
    238      1.1  gmcgarry 
    239      1.1  gmcgarry 
    240      1.1  gmcgarry #ifdef FILL_ARPCACHE
    241      1.1  gmcgarry void fill_arpcache (pkt, len)
    242      1.1  gmcgarry 	void *pkt;
    243      1.1  gmcgarry 	int len;
    244      1.1  gmcgarry {
    245      1.1  gmcgarry 	int i;
    246      1.1  gmcgarry 	struct arp_list *al;
    247      1.1  gmcgarry 	struct ether_header *eh = (struct ether_header *)pkt;
    248      1.1  gmcgarry 	struct ip *ih = (struct ip *)(eh + 1);
    249      1.1  gmcgarry 
    250      1.1  gmcgarry #ifdef NET_DEBUG
    251      1.1  gmcgarry 	if (debug)
    252      1.1  gmcgarry 		dump_packet_info(pkt, len);
    253      1.1  gmcgarry #endif
    254      1.1  gmcgarry 
    255      1.1  gmcgarry 	if (ntohs(eh->ether_type) == 0x0800) {
    256      1.1  gmcgarry 
    257      1.1  gmcgarry 		/* check arp cache */
    258      1.1  gmcgarry 		for (i=0, al=arp_list; i<arp_num; ++i, ++al) {
    259      1.1  gmcgarry 			if (al->addr.s_addr == ih->ip_src.s_addr) {
    260      1.1  gmcgarry 				/* already in cache */
    261      1.1  gmcgarry 				return;
    262      1.1  gmcgarry 			}
    263      1.1  gmcgarry 		}
    264      1.1  gmcgarry         	if (arp_num > 7)
    265      1.1  gmcgarry                		arp_num = 1;    /* recycle */
    266      1.1  gmcgarry 		al->addr.s_addr = ih->ip_src.s_addr;
    267      1.1  gmcgarry 		for (i=0; i<6; i++)
    268      1.1  gmcgarry 			al->ea[i] = eh->ether_shost[i];
    269      1.1  gmcgarry 		++arp_num;
    270      1.1  gmcgarry 	}
    271      1.1  gmcgarry 
    272      1.1  gmcgarry }
    273      1.1  gmcgarry #endif
    274      1.1  gmcgarry 
    275      1.1  gmcgarry #ifdef NET_DEBUG
    276      1.1  gmcgarry void dump_packet_info(pkt, len)
    277      1.1  gmcgarry 	void *pkt;
    278      1.1  gmcgarry 	int len;
    279      1.1  gmcgarry {
    280      1.1  gmcgarry 	struct ether_header *eh = (struct ether_header *)pkt;
    281      1.1  gmcgarry 	struct ip *ih = (struct ip *)(eh + 1);
    282      1.1  gmcgarry 
    283      1.1  gmcgarry 	printf("ether_dhost = %s\n", ether_sprintf(eh->ether_dhost));
    284      1.1  gmcgarry 	printf("ether_shost = %s\n", ether_sprintf(eh->ether_shost));
    285      1.1  gmcgarry 	printf("ether_type = 0x%x\n", ntohs(eh->ether_type));
    286      1.1  gmcgarry 
    287      1.1  gmcgarry 	if (ntohs(eh->ether_type) == 0x0800) {
    288      1.1  gmcgarry 		printf("ip packet version %d\n", ih->ip_v);
    289      1.1  gmcgarry 		printf("source ip: 0x%x\n", ih->ip_src.s_addr);
    290      1.1  gmcgarry 		printf("dest ip: 0x%x\n", ih->ip_dst.s_addr);
    291      1.1  gmcgarry 
    292      1.1  gmcgarry 	}
    293      1.1  gmcgarry 
    294      1.1  gmcgarry }
    295      1.1  gmcgarry #endif
    296