static int tulip_init_one()

in ethernet/dec/tulip/tulip_core.c [1289:1789]


static int tulip_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
{
	struct tulip_private *tp;
	/* See note below on the multiport cards. */
	static unsigned char last_phys_addr[ETH_ALEN] = {
		0x00, 'L', 'i', 'n', 'u', 'x'
	};
#if defined(__i386__) || defined(__x86_64__)	/* Patch up x86 BIOS bug. */
	static int last_irq;
#endif
	int i, irq;
	unsigned short sum;
	unsigned char *ee_data;
	struct net_device *dev;
	void __iomem *ioaddr;
	static int board_idx = -1;
	int chip_idx = ent->driver_data;
	const char *chip_name = tulip_tbl[chip_idx].chip_name;
	unsigned int eeprom_missing = 0;
	u8 addr[ETH_ALEN] __aligned(2);
	unsigned int force_csr0 = 0;

	board_idx++;

	/*
	 *	Lan media wire a tulip chip to a wan interface. Needs a very
	 *	different driver (lmc driver)
	 */

        if (pdev->subsystem_vendor == PCI_VENDOR_ID_LMC) {
		pr_err("skipping LMC card\n");
		return -ENODEV;
	} else if (pdev->subsystem_vendor == PCI_VENDOR_ID_SBE &&
		   (pdev->subsystem_device == PCI_SUBDEVICE_ID_SBE_T3E3 ||
		    pdev->subsystem_device == PCI_SUBDEVICE_ID_SBE_2T3E3_P0 ||
		    pdev->subsystem_device == PCI_SUBDEVICE_ID_SBE_2T3E3_P1)) {
		pr_err("skipping SBE T3E3 port\n");
		return -ENODEV;
	}

	/*
	 *	DM910x chips should be handled by the dmfe driver, except
	 *	on-board chips on SPARC systems.  Also, early DM9100s need
	 *	software CRC which only the dmfe driver supports.
	 */

#ifdef CONFIG_TULIP_DM910X
	if (chip_idx == DM910X) {
		struct device_node *dp;

		if (pdev->vendor == 0x1282 && pdev->device == 0x9100 &&
		    pdev->revision < 0x30) {
			pr_info("skipping early DM9100 with Crc bug (use dmfe)\n");
			return -ENODEV;
		}

		dp = pci_device_to_OF_node(pdev);
		if (!(dp && of_get_property(dp, "local-mac-address", NULL))) {
			pr_info("skipping DM910x expansion card (use dmfe)\n");
			return -ENODEV;
		}
	}
#endif

	/*
	 *	Looks for early PCI chipsets where people report hangs
	 *	without the workarounds being on.
	 */

	/* 1. Intel Saturn. Switch to 8 long words burst, 8 long word cache
	      aligned.  Aries might need this too. The Saturn errata are not
	      pretty reading but thankfully it's an old 486 chipset.

	   2. The dreaded SiS496 486 chipset. Same workaround as Intel
	      Saturn.
	*/

	if (pci_dev_present(early_486_chipsets)) {
		csr0 = MRL | MRM | (8 << BurstLenShift) | (1 << CALShift);
		force_csr0 = 1;
	}

	/* bugfix: the ASIX must have a burst limit or horrible things happen. */
	if (chip_idx == AX88140) {
		if ((csr0 & 0x3f00) == 0)
			csr0 |= 0x2000;
	}

	/* PNIC doesn't have MWI/MRL/MRM... */
	if (chip_idx == LC82C168)
		csr0 &= ~0xfff10000; /* zero reserved bits 31:20, 16 */

	/* DM9102A has troubles with MRM & clear reserved bits 24:22, 20, 16, 7:1 */
	if (tulip_uli_dm_quirk(pdev)) {
		csr0 &= ~0x01f100ff;
#if defined(CONFIG_SPARC)
                csr0 = (csr0 & ~0xff00) | 0xe000;
#endif
	}
	/*
	 *	And back to business
	 */

	i = pci_enable_device(pdev);
	if (i) {
		pr_err("Cannot enable tulip board #%d, aborting\n", board_idx);
		return i;
	}

	irq = pdev->irq;

	/* alloc_etherdev ensures aligned and zeroed private structures */
	dev = alloc_etherdev (sizeof (*tp));
	if (!dev)
		return -ENOMEM;

	SET_NETDEV_DEV(dev, &pdev->dev);
	if (pci_resource_len (pdev, 0) < tulip_tbl[chip_idx].io_size) {
		pr_err("%s: I/O region (0x%llx@0x%llx) too small, aborting\n",
		       pci_name(pdev),
		       (unsigned long long)pci_resource_len (pdev, 0),
		       (unsigned long long)pci_resource_start (pdev, 0));
		goto err_out_free_netdev;
	}

	/* grab all resources from both PIO and MMIO regions, as we
	 * don't want anyone else messing around with our hardware */
	if (pci_request_regions (pdev, DRV_NAME))
		goto err_out_free_netdev;

	ioaddr =  pci_iomap(pdev, TULIP_BAR, tulip_tbl[chip_idx].io_size);

	if (!ioaddr)
		goto err_out_free_res;

	/*
	 * initialize private data structure 'tp'
	 * it is zeroed and aligned in alloc_etherdev
	 */
	tp = netdev_priv(dev);
	tp->dev = dev;

	tp->rx_ring = dma_alloc_coherent(&pdev->dev,
					 sizeof(struct tulip_rx_desc) * RX_RING_SIZE +
					 sizeof(struct tulip_tx_desc) * TX_RING_SIZE,
					 &tp->rx_ring_dma, GFP_KERNEL);
	if (!tp->rx_ring)
		goto err_out_mtable;
	tp->tx_ring = (struct tulip_tx_desc *)(tp->rx_ring + RX_RING_SIZE);
	tp->tx_ring_dma = tp->rx_ring_dma + sizeof(struct tulip_rx_desc) * RX_RING_SIZE;

	tp->chip_id = chip_idx;
	tp->flags = tulip_tbl[chip_idx].flags;

	tp->wolinfo.supported = 0;
	tp->wolinfo.wolopts = 0;
	/* COMET: Enable power management only for AN983B */
	if (chip_idx == COMET ) {
		u32 sig;
		pci_read_config_dword (pdev, 0x80, &sig);
		if (sig == 0x09811317) {
			tp->flags |= COMET_PM;
			tp->wolinfo.supported = WAKE_PHY | WAKE_MAGIC;
			pr_info("%s: Enabled WOL support for AN983B\n",
				__func__);
		}
	}
	tp->pdev = pdev;
	tp->base_addr = ioaddr;
	tp->revision = pdev->revision;
	tp->csr0 = csr0;
	spin_lock_init(&tp->lock);
	spin_lock_init(&tp->mii_lock);
	timer_setup(&tp->timer, tulip_tbl[tp->chip_id].media_timer, 0);

	INIT_WORK(&tp->media_work, tulip_tbl[tp->chip_id].media_task);

#ifdef CONFIG_TULIP_MWI
	if (!force_csr0 && (tp->flags & HAS_PCI_MWI))
		tulip_mwi_config (pdev, dev);
#endif

	/* Stop the chip's Tx and Rx processes. */
	tulip_stop_rxtx(tp);

	pci_set_master(pdev);

#ifdef CONFIG_GSC
	if (pdev->subsystem_vendor == PCI_VENDOR_ID_HP) {
		switch (pdev->subsystem_device) {
		default:
			break;
		case 0x1061:
		case 0x1062:
		case 0x1063:
		case 0x1098:
		case 0x1099:
		case 0x10EE:
			tp->flags |= HAS_SWAPPED_SEEPROM | NEEDS_FAKE_MEDIA_TABLE;
			chip_name = "GSC DS21140 Tulip";
		}
	}
#endif

	/* Clear the missed-packet counter. */
	ioread32(ioaddr + CSR8);

	/* The station address ROM is read byte serially.  The register must
	   be polled, waiting for the value to be read bit serially from the
	   EEPROM.
	   */
	ee_data = tp->eeprom;
	memset(ee_data, 0, sizeof(tp->eeprom));
	sum = 0;
	if (chip_idx == LC82C168) {
		for (i = 0; i < 3; i++) {
			int value, boguscnt = 100000;
			iowrite32(0x600 | i, ioaddr + 0x98);
			do {
				value = ioread32(ioaddr + CSR9);
			} while (value < 0  && --boguscnt > 0);
			put_unaligned_le16(value, ((__le16 *)addr) + i);
			sum += value & 0xffff;
		}
		eth_hw_addr_set(dev, addr);
	} else if (chip_idx == COMET) {
		/* No need to read the EEPROM. */
		put_unaligned_le32(ioread32(ioaddr + 0xA4), addr);
		put_unaligned_le16(ioread32(ioaddr + 0xA8), addr + 4);
		eth_hw_addr_set(dev, addr);
		for (i = 0; i < 6; i ++)
			sum += dev->dev_addr[i];
	} else {
		/* A serial EEPROM interface, we read now and sort it out later. */
		int sa_offset = 0;
		int ee_addr_size = tulip_read_eeprom(dev, 0xff, 8) & 0x40000 ? 8 : 6;
		int ee_max_addr = ((1 << ee_addr_size) - 1) * sizeof(u16);

		if (ee_max_addr > sizeof(tp->eeprom))
			ee_max_addr = sizeof(tp->eeprom);

		for (i = 0; i < ee_max_addr ; i += sizeof(u16)) {
			u16 data = tulip_read_eeprom(dev, i/2, ee_addr_size);
			ee_data[i] = data & 0xff;
			ee_data[i + 1] = data >> 8;
		}

		/* DEC now has a specification (see Notes) but early board makers
		   just put the address in the first EEPROM locations. */
		/* This does  memcmp(ee_data, ee_data+16, 8) */
		for (i = 0; i < 8; i ++)
			if (ee_data[i] != ee_data[16+i])
				sa_offset = 20;
		if (chip_idx == CONEXANT) {
			/* Check that the tuple type and length is correct. */
			if (ee_data[0x198] == 0x04  &&  ee_data[0x199] == 6)
				sa_offset = 0x19A;
		} else if (ee_data[0] == 0xff  &&  ee_data[1] == 0xff &&
				   ee_data[2] == 0) {
			sa_offset = 2;		/* Grrr, damn Matrox boards. */
		}
#ifdef CONFIG_MIPS_COBALT
               if ((pdev->bus->number == 0) &&
                   ((PCI_SLOT(pdev->devfn) == 7) ||
                    (PCI_SLOT(pdev->devfn) == 12))) {
                       /* Cobalt MAC address in first EEPROM locations. */
                       sa_offset = 0;
		       /* Ensure our media table fixup get's applied */
		       memcpy(ee_data + 16, ee_data, 8);
               }
#endif
#ifdef CONFIG_GSC
		/* Check to see if we have a broken srom */
		if (ee_data[0] == 0x61 && ee_data[1] == 0x10) {
			/* pci_vendor_id and subsystem_id are swapped */
			ee_data[0] = ee_data[2];
			ee_data[1] = ee_data[3];
			ee_data[2] = 0x61;
			ee_data[3] = 0x10;

			/* HSC-PCI boards need to be byte-swaped and shifted
			 * up 1 word.  This shift needs to happen at the end
			 * of the MAC first because of the 2 byte overlap.
			 */
			for (i = 4; i >= 0; i -= 2) {
				ee_data[17 + i + 3] = ee_data[17 + i];
				ee_data[16 + i + 5] = ee_data[16 + i];
			}
		}
#endif

		for (i = 0; i < 6; i ++) {
			addr[i] = ee_data[i + sa_offset];
			sum += ee_data[i + sa_offset];
		}
		eth_hw_addr_set(dev, addr);
	}
	/* Lite-On boards have the address byte-swapped. */
	if ((dev->dev_addr[0] == 0xA0 ||
	     dev->dev_addr[0] == 0xC0 ||
	     dev->dev_addr[0] == 0x02) &&
	    dev->dev_addr[1] == 0x00) {
		for (i = 0; i < 6; i+=2) {
			addr[i] = dev->dev_addr[i+1];
			addr[i+1] = dev->dev_addr[i];
		}
		eth_hw_addr_set(dev, addr);
	}

	/* On the Zynx 315 Etherarray and other multiport boards only the
	   first Tulip has an EEPROM.
	   On Sparc systems the mac address is held in the OBP property
	   "local-mac-address".
	   The addresses of the subsequent ports are derived from the first.
	   Many PCI BIOSes also incorrectly report the IRQ line, so we correct
	   that here as well. */
	if (sum == 0  || sum == 6*0xff) {
#if defined(CONFIG_SPARC)
		struct device_node *dp = pci_device_to_OF_node(pdev);
		const unsigned char *addr2;
		int len;
#endif
		eeprom_missing = 1;
		for (i = 0; i < 5; i++)
			addr[i] = last_phys_addr[i];
		addr[i] = last_phys_addr[i] + 1;
		eth_hw_addr_set(dev, addr);
#if defined(CONFIG_SPARC)
		addr2 = of_get_property(dp, "local-mac-address", &len);
		if (addr2 && len == ETH_ALEN)
			eth_hw_addr_set(dev, addr2);
#endif
#if defined(__i386__) || defined(__x86_64__)	/* Patch up x86 BIOS bug. */
		if (last_irq)
			irq = last_irq;
#endif
	}

	for (i = 0; i < 6; i++)
		last_phys_addr[i] = dev->dev_addr[i];
#if defined(__i386__) || defined(__x86_64__)	/* Patch up x86 BIOS bug. */
	last_irq = irq;
#endif

	/* The lower four bits are the media type. */
	if (board_idx >= 0  &&  board_idx < MAX_UNITS) {
		if (options[board_idx] & MEDIA_MASK)
			tp->default_port = options[board_idx] & MEDIA_MASK;
		if ((options[board_idx] & FullDuplex) || full_duplex[board_idx] > 0)
			tp->full_duplex = 1;
		if (mtu[board_idx] > 0)
			dev->mtu = mtu[board_idx];
	}
	if (dev->mem_start & MEDIA_MASK)
		tp->default_port = dev->mem_start & MEDIA_MASK;
	if (tp->default_port) {
		pr_info(DRV_NAME "%d: Transceiver selection forced to %s\n",
			board_idx, medianame[tp->default_port & MEDIA_MASK]);
		tp->medialock = 1;
		if (tulip_media_cap[tp->default_port] & MediaAlwaysFD)
			tp->full_duplex = 1;
	}
	if (tp->full_duplex)
		tp->full_duplex_lock = 1;

	if (tulip_media_cap[tp->default_port] & MediaIsMII) {
		static const u16 media2advert[] = {
			0x20, 0x40, 0x03e0, 0x60, 0x80, 0x100, 0x200
		};
		tp->mii_advertise = media2advert[tp->default_port - 9];
		tp->mii_advertise |= (tp->flags & HAS_8023X); /* Matching bits! */
	}

	if (tp->flags & HAS_MEDIA_TABLE) {
		sprintf(dev->name, DRV_NAME "%d", board_idx);	/* hack */
		tulip_parse_eeprom(dev);
		strcpy(dev->name, "eth%d");			/* un-hack */
	}

	if ((tp->flags & ALWAYS_CHECK_MII) ||
		(tp->mtable  &&  tp->mtable->has_mii) ||
		( ! tp->mtable  &&  (tp->flags & HAS_MII))) {
		if (tp->mtable  &&  tp->mtable->has_mii) {
			for (i = 0; i < tp->mtable->leafcount; i++)
				if (tp->mtable->mleaf[i].media == 11) {
					tp->cur_index = i;
					tp->saved_if_port = dev->if_port;
					tulip_select_media(dev, 2);
					dev->if_port = tp->saved_if_port;
					break;
				}
		}

		/* Find the connected MII xcvrs.
		   Doing this in open() would allow detecting external xcvrs
		   later, but takes much time. */
		tulip_find_mii (dev, board_idx);
	}

	/* The Tulip-specific entries in the device structure. */
	dev->netdev_ops = &tulip_netdev_ops;
	dev->watchdog_timeo = TX_TIMEOUT;
#ifdef CONFIG_TULIP_NAPI
	netif_napi_add(dev, &tp->napi, tulip_poll, 16);
#endif
	dev->ethtool_ops = &ops;

	if (register_netdev(dev))
		goto err_out_free_ring;

	pci_set_drvdata(pdev, dev);

	dev_info(&dev->dev,
#ifdef CONFIG_TULIP_MMIO
		 "%s rev %d at MMIO %#llx,%s %pM, IRQ %d\n",
#else
		 "%s rev %d at Port %#llx,%s %pM, IRQ %d\n",
#endif
		 chip_name, pdev->revision,
		 (unsigned long long)pci_resource_start(pdev, TULIP_BAR),
		 eeprom_missing ? " EEPROM not present," : "",
		 dev->dev_addr, irq);

        if (tp->chip_id == PNIC2)
		tp->link_change = pnic2_lnk_change;
	else if (tp->flags & HAS_NWAY)
		tp->link_change = t21142_lnk_change;
	else if (tp->flags & HAS_PNICNWAY)
		tp->link_change = pnic_lnk_change;

	/* Reset the xcvr interface and turn on heartbeat. */
	switch (chip_idx) {
	case DC21140:
	case DM910X:
	default:
		if (tp->mtable)
			iowrite32(tp->mtable->csr12dir | 0x100, ioaddr + CSR12);
		break;
	case DC21142:
		if (tp->mii_cnt  ||  tulip_media_cap[dev->if_port] & MediaIsMII) {
			iowrite32(csr6_mask_defstate, ioaddr + CSR6);
			iowrite32(0x0000, ioaddr + CSR13);
			iowrite32(0x0000, ioaddr + CSR14);
			iowrite32(csr6_mask_hdcap, ioaddr + CSR6);
		} else
			t21142_start_nway(dev);
		break;
	case PNIC2:
	        /* just do a reset for sanity sake */
		iowrite32(0x0000, ioaddr + CSR13);
		iowrite32(0x0000, ioaddr + CSR14);
		break;
	case LC82C168:
		if ( ! tp->mii_cnt) {
			tp->nway = 1;
			tp->nwayset = 0;
			iowrite32(csr6_ttm | csr6_ca, ioaddr + CSR6);
			iowrite32(0x30, ioaddr + CSR12);
			iowrite32(0x0001F078, ioaddr + CSR6);
			iowrite32(0x0201F078, ioaddr + CSR6); /* Turn on autonegotiation. */
		}
		break;
	case MX98713:
	case COMPEX9881:
		iowrite32(0x00000000, ioaddr + CSR6);
		iowrite32(0x000711C0, ioaddr + CSR14); /* Turn on NWay. */
		iowrite32(0x00000001, ioaddr + CSR13);
		break;
	case MX98715:
	case MX98725:
		iowrite32(0x01a80000, ioaddr + CSR6);
		iowrite32(0xFFFFFFFF, ioaddr + CSR14);
		iowrite32(0x00001000, ioaddr + CSR12);
		break;
	case COMET:
		/* No initialization necessary. */
		break;
	}

	/* put the chip in snooze mode until opened */
	tulip_set_power_state (tp, 0, 1);

	return 0;

err_out_free_ring:
	dma_free_coherent(&pdev->dev,
			  sizeof(struct tulip_rx_desc) * RX_RING_SIZE +
			  sizeof(struct tulip_tx_desc) * TX_RING_SIZE,
			  tp->rx_ring, tp->rx_ring_dma);

err_out_mtable:
	kfree (tp->mtable);
	pci_iounmap(pdev, ioaddr);

err_out_free_res:
	pci_release_regions (pdev);

err_out_free_netdev:
	free_netdev (dev);
	return -ENODEV;
}