Subject: fixes for fxp cardbus driver
To: None <gnats-bugs@gnats.netbsd.org>
From: Johan Danielsson <joda@pdc.kth.se>
List: netbsd-bugs
Date: 10/15/1999 18:00:18
>Number:         8631
>Category:       kern
>Synopsis:       fixes for fxp cardbus driver
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    kern-bug-people (Kernel Bug People)
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Fri Oct 15 23:54:00 1999
>Last-Modified:
>Originator:     Johan Danielsson
>Organization:
>Release:        1999-10-15
>Environment:
	<machine, os, target, libraries (multiple lines)>
System: NetBSD 1.4L (BLUBB) #9: Fri Oct 15 17:22:11 CEST 1999


>Description:

These patches are needed to get the fxp to work. It includes the
changes in kern/7677 (detects the size of the eeprom).

>How-To-Repeat:
>Fix:

Index: cardbus/if_fxp_cardbus.c
===================================================================
RCS file: /cvsroot/syssrc/sys/dev/cardbus/if_fxp_cardbus.c,v
retrieving revision 1.2
diff -w -u -r1.2 if_fxp_cardbus.c
--- if_fxp_cardbus.c	1999/10/15 06:42:22	1.2
+++ if_fxp_cardbus.c	1999/10/15 15:55:30
@@ -76,8 +76,8 @@
 
 #include <dev/mii/miivar.h>
 
-#include <dev/pci/if_fxpreg.h>
-#include <dev/pci/if_fxpvar.h>
+#include <dev/ic/i82557reg.h>
+#include <dev/ic/i82557var.h>
 
 #include <dev/pci/pcivar.h>
 #include <dev/pci/pcireg.h>
@@ -91,8 +91,10 @@
 
 struct fxp_cardbus_softc {
     struct fxp_softc sc;
-    cardbustag_t tag;
+    cardbus_devfunc_t ct;
+    pcireg_t base0_reg;
     pcireg_t base1_reg;
+    bus_size_t size;
 };
 
 struct cfattach fxp_cardbus_ca = {
@@ -147,66 +149,42 @@
     struct fxp_softc *sc = (struct fxp_softc*)self;
     struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc*)self;
     struct cardbus_attach_args *ca = aux;
-    cardbus_function_tag_t cf = ca->ca_cf;
+    bus_space_tag_t iot, memt;
     bus_space_handle_t ioh, memh;
 	
     bus_addr_t adr;
+    bus_size_t size;
 
-    /*
-     * Map control/status registers.
-     */
-#if rbus
-    cardbus_function_tag_t cf = ct->ct_cf;
-
-    if (cardbus_mapreg_map(ct, CARDBUS_BASE1_REG, CARDBUS_MAPREG_TYPE_IO, 0,
-			   &(sc->sc_iot), &ioh, &adr, NULL)) {
-      panic("io alloc in ex_attach_cardbus\n");
-    }
-    csc->base1_reg = adr | 1;
-    sc->sc_sh = ioh;
+    csc->ct = ca->ca_ct;
 
-#elif unibus
-    printf("fxp_cardbus_attach: uni %p\n", ca->ca_uni);
-    if ((*cf->cf_ub.unibus_space_alloc)(ca->ca_uni, ca->bar_info[1].tag, 0, 
-					ca->bar_info[1].size,
-					ca->bar_info[1].size - 1,
-					ca->bar_info[1].size,
-					0, 0, &adr, &ioh))
-	panic("io alloc");
-
-
-    csc->base1_reg = adr | 1;
-    sc->sc_st = ca->bar_info[1].tag;
-    sc->sc_sh = ioh;
-
-    if (0) {
 	/*
 	 * Map control/status registers.
 	 */
-	if ((*cf->cf_ub.unibus_space_alloc)(ca->ca_uni, ca->bar_info[0].tag, 0, 
-					    ca->bar_info[0].size,
-					    ca->bar_info[0].size - 1,
-					    ca->bar_info[0].size,
-					    0, 0, &adr, &memh))
-	    panic("mem alloc");
-
-
-	sc->sc_st = ca->bar_info[0].tag;
+    if(cardbus_mapreg_map(csc->ct, CARDBUS_BASE0_REG, 
+			  PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 
+			  0, &memt, &memh, &adr, &size) == 0) {
+	csc->base0_reg = adr;
+	sc->sc_st = memt;
 	sc->sc_sh = memh;
-    }
-#endif
+	csc->size = size;
+    } else if(cardbus_mapreg_map(csc->ct, CARDBUS_BASE1_REG, 
+				 PCI_MAPREG_TYPE_IO, 
+				 0, &iot, &ioh, &adr, &size) == 0) {
+	csc->base1_reg = adr | 1;
+	sc->sc_st = iot;
+	sc->sc_sh = ioh;
+	csc->size = size;
+    } else
+	panic("%s: failed to allocate mem and io space", __FUNCTION__);
 
-    csc->tag = ca->ca_tag;
 
     sc->sc_dmat = ca->ca_dmat;
-    sc->enable = fxp_cardbus_enable;
-    sc->disable = fxp_cardbus_disable;
-    sc->enabled = 0;
-    
+    sc->sc_enable = fxp_cardbus_enable;
+    sc->sc_disable = fxp_cardbus_disable;
+    sc->sc_enabled = 0;
     fxp_cardbus_setup(sc);
-    printf(": Intel EtherExpress Pro 10+/100B Ethernet\n");
     
-    fxp_attach_common(sc);
+    fxp_attach(sc);
 }
 
 
@@ -218,28 +196,26 @@
     cardbus_chipset_tag_t cc = psc->sc_cc;
     cardbus_function_tag_t cf = psc->sc_cf;
     pcireg_t command;
-
-    printf("fxp_cardbus_setup\n");
 
-    cardbus_conf_write(cc, cf, csc->tag, CARDBUS_BASE1_REG, csc->base1_reg);
+    cardbustag_t tag = cardbus_make_tag(cc, cf, csc->ct->ct_bus, 
+					csc->ct->ct_dev, csc->ct->ct_func);
 
+    command = Cardbus_conf_read(csc->ct, tag, CARDBUS_COMMAND_STATUS_REG);
+    if(csc->base0_reg) {
+	Cardbus_conf_write(csc->ct, tag,
+			   CARDBUS_BASE0_REG, csc->base0_reg);
+	(cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE);
+	command |= CARDBUS_COMMAND_MEM_ENABLE | CARDBUS_COMMAND_MASTER_ENABLE;
+	
+    } else if(csc->base1_reg) {
+	Cardbus_conf_write(csc->ct, tag,
+			   CARDBUS_BASE1_REG, csc->base1_reg);
     (cf->cardbus_ctrl)(cc, CARDBUS_IO_ENABLE);
-
-    /* enable the card */
-    command = cardbus_conf_read(cc, cf, csc->tag, CARDBUS_COMMAND_STATUS_REG);
     command |= (CARDBUS_COMMAND_IO_ENABLE | CARDBUS_COMMAND_MASTER_ENABLE);
-    cardbus_conf_write(cc, cf, csc->tag, CARDBUS_COMMAND_STATUS_REG, command);
-#if 0
-    cardbus_conf_write(cc, cf, ca->tag, CARDBUS_BASE0_REG, adr);
-
-    (ca->ca_cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE);
-
-    /* enable card mem */
-    command = cardbus_conf_read(cc, cf, ca->tag, CARDBUS_COMMAND_STATUS_REG);
-    command |= CARDBUS_COMMAND_MEM_ENABLE;
-    cardbus_conf_write(cc, cf, ca->tag, CARDBUS_COMMAND_STATUS_REG, command);
-#endif
 }
+    /* enable the card */
+    Cardbus_conf_write(csc->ct, tag, CARDBUS_COMMAND_STATUS_REG, command);
+}
 
 static int
 fxp_cardbus_enable(struct fxp_softc *sc)
@@ -249,7 +225,7 @@
     cardbus_chipset_tag_t cc = psc->sc_cc;
     cardbus_function_tag_t cf = psc->sc_cf;
 
-    cardbus_function_enable(psc, csc->tag);
+    cardbus_function_enable(csc->ct);
 
     fxp_cardbus_setup(sc);
 
@@ -267,6 +243,8 @@
     return 0;
 }
 
+void fxp_stop(struct fxp_softc*);
+
 static void
 fxp_cardbus_disable(struct fxp_softc *sc)
 {
@@ -277,5 +255,5 @@
     fxp_stop(sc);
     cardbus_intr_disestablish(cc, cf, sc->sc_ih); /* remove intr handler */
     
-    cardbus_function_disable(psc);
+    cardbus_function_disable(((struct fxp_cardbus_softc*)sc)->ct);
 }
Index: ic/i82557.c
===================================================================
RCS file: /cvsroot/syssrc/sys/dev/ic/i82557.c,v
retrieving revision 1.8
diff -w -u -r1.8 i82557.c
--- i82557.c	1999/08/05 01:35:40	1.8
+++ i82557.c	1999/10/15 15:55:34
@@ -190,6 +190,9 @@
 
 int	fxp_copy_small = 0;
 
+static int fxp_enable __P((struct fxp_softc*));
+static void fxp_disable __P((struct fxp_softc*));
+
 struct fxp_phytype {
 	int	fp_phy;		/* type of PHY, -1 for MII at the end. */
 	void	(*fp_init) __P((struct fxp_softc *));
@@ -443,6 +446,28 @@
 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
 	DELAY(10);
 
+	/* figure out EEPROM size */
+	/* XXX this is probably not the best way to do this; the linux
+           driver does a checksum of the eeprom, but there is (AFAIK)
+           no on-line documentation on what this checksum should look
+           like (you could just steal the code from linux, but that's
+           cheating); for now we just use the fact that the upper two
+           bits of word 10 should be 01 */
+	for(sc->sc_eeprom_size = 6; 
+	    sc->sc_eeprom_size <= 8; 
+	    sc->sc_eeprom_size++) {
+		fxp_read_eeprom(sc, &data, 10, 1);
+		if((data & 0xc000) == 0x4000)
+			break;
+	}
+	if(sc->sc_eeprom_size > 8)
+		panic("%s: failed to get EEPROM size", sc->sc_dev.dv_xname);
+#ifdef DEBUG
+	printf("%s: assuming %d word EEPROM\n", 
+	       sc->sc_dev.dv_xname, 
+	       1 << sc->sc_eeprom_size);
+#endif
+
 	/*
 	 * Get info about the primary PHY
 	 */
@@ -496,7 +521,7 @@
 		/*
 		 * Shift in address.
 		 */
-		for (x = 6; x > 0; x--) {
+		for (x = sc->sc_eeprom_size; x > 0; x--) {
 			if ((i + offset) & (1 << (x - 1))) {
 				reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
 			} else {
@@ -729,6 +777,9 @@
 	u_int16_t len;
 	u_int8_t statack;
 
+	if (sc->sc_enabled == 0)
+		return 0;
+	
 	while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
 		claimed = 1;
 
@@ -1325,6 +1376,12 @@
 {
 	struct fxp_softc *sc = ifp->if_softc;
 
+	if(sc->sc_enabled == 0) {
+		ifmr->ifm_active = IFM_ETHER | IFM_NONE;
+		ifmr->ifm_status = 0;
+		return;
+	}
+	
 	mii_pollstat(&sc->sc_mii);
 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
@@ -1466,6 +1523,8 @@
 
 	switch (command) {
 	case SIOCSIFADDR:
+		if ((error = fxp_enable(sc)) != 0)
+			break;
 		ifp->if_flags |= IFF_UP;
 
 		switch (ifa->ifa_addr->sa_family) {
@@ -1513,24 +1572,33 @@
 			 * stop it.
 			 */
 			fxp_stop(sc, 1);
+			fxp_disable(sc);
 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
 			   (ifp->if_flags & IFF_RUNNING) == 0) {
 			/*
 			 * If interface is marked up and it is stopped, then
 			 * start it.
 			 */
+			if((error = fxp_enable(sc)) != 0)
+				break;
 			error = fxp_init(sc);
 		} else if ((ifp->if_flags & IFF_UP) != 0) {
 			/*
 			 * Reset the interface to pick up change in any other
 			 * flags that affect the hardware state.
 			 */
+			if((error = fxp_enable(sc)) != 0)
+				break;
 			error = fxp_init(sc);
 		}
 		break;
 
 	case SIOCADDMULTI:
 	case SIOCDELMULTI:
+		if(sc->sc_enabled == 0) {
+			error = EIO;
+			break;
+		}
 		error = (command == SIOCADDMULTI) ?
 		    ether_addmulti(ifr, &sc->sc_ethercom) :
 		    ether_delmulti(ifr, &sc->sc_ethercom);
@@ -1644,4 +1712,28 @@
 		FXP_CDMCSSYNC(sc,
 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
 	} while ((mcsp->cb_status & FXP_CB_STATUS_C) == 0);
+}
+
+static int
+fxp_enable(struct fxp_softc *sc)
+{
+	if (sc->sc_enabled == 0 && sc->sc_enable != NULL) {
+		if ((*sc->sc_enable)(sc) != 0) {
+			printf("%s: device enable failed\n",
+			       sc->sc_dev.dv_xname);
+			return (EIO);
+		}
+	}
+	
+	sc->sc_enabled = 1;
+	return (0);
+}
+
+static void
+fxp_disable(struct fxp_softc *sc)
+{
+	if (sc->sc_enabled != 0 && sc->sc_disable != NULL) {
+		(*sc->sc_disable)(sc);
+		sc->sc_enabled = 0;
+	}
 }
Index: ic/i82557var.h
===================================================================
RCS file: /cvsroot/syssrc/sys/dev/ic/i82557var.h,v
retrieving revision 1.6
diff -w -u -r1.6 i82557var.h
--- i82557var.h	1999/08/05 01:35:41	1.6
+++ i82557var.h	1999/10/15 15:55:39
@@ -198,9 +198,16 @@
 	int phy_primary_addr;		/* address of primary PHY */
 	int phy_primary_device;		/* device type of primary PHY */
 	int phy_10Mbps_only;		/* PHY is 10Mbps-only device */
+
+	int	sc_enabled;	/* boolean; power enabled on interface */
+	int	(*sc_enable) __P((struct fxp_softc *));
+	void	(*sc_disable) __P((struct fxp_softc *));
+
+	int sc_eeprom_size;		/* log2 size of EEPROM */
 #if NRND > 0
 	rndsource_element_t rnd_source;	/* random source */
 #endif
+	
 };
 
 #define	FXP_RXMAP_GET(sc)	((sc)->sc_rxmaps[(sc)->sc_rxfree++])
Index: cardbus/files.cardbus
===================================================================
RCS file: /cvsroot/syssrc/sys/dev/cardbus/files.cardbus,v
retrieving revision 1.2
diff -w -u -r1.2 files.cardbus
--- files.cardbus       1999/10/15 06:42:22     1.2
+++ files.cardbus       1999/10/15 15:57:43
@@ -25,9 +25,9 @@
 #
 #
 #
-#attach        fxp at cardbus with fxp_cardbus
-#file  dev/cardbus/if_fxp_cardbus.c            fxp_cardbus
+attach fxp at cardbus with fxp_cardbus
+file   dev/cardbus/if_fxp_cardbus.c            fxp_cardbus
 
 
#attach        com at cardbus with com_cardbus
#file  dev/cardbus/com_cardbus.c               com_cardbus
>Audit-Trail:
>Unformatted: