probe.c

来自「linux 内核源代码」· C语言 代码 · 共 1,257 行 · 第 1/3 页

C
1,257
字号
	/*	 * Set up the primary, secondary and subordinate	 * bus numbers.	 */	child->number = child->secondary = busnr;	child->primary = parent->secondary;	child->subordinate = 0xff;	/* Set up default resource pointers and names.. */	for (i = 0; i < 4; i++) {		child->resource[i] = &bridge->resource[PCI_BRIDGE_RESOURCES+i];		child->resource[i]->name = child->name;	}	bridge->subordinate = child;	return child;error_file_create:	class_device_unregister(&child->class_dev);error_register:	kfree(child);	return NULL;}struct pci_bus *pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev, int busnr){	struct pci_bus *child;	child = pci_alloc_child_bus(parent, dev, busnr);	if (child) {		down_write(&pci_bus_sem);		list_add_tail(&child->node, &parent->children);		up_write(&pci_bus_sem);	}	return child;}static void pci_fixup_parent_subordinate_busnr(struct pci_bus *child, int max){	struct pci_bus *parent = child->parent;	/* Attempts to fix that up are really dangerous unless	   we're going to re-assign all bus numbers. */	if (!pcibios_assign_all_busses())		return;	while (parent->parent && parent->subordinate < max) {		parent->subordinate = max;		pci_write_config_byte(parent->self, PCI_SUBORDINATE_BUS, max);		parent = parent->parent;	}}unsigned int pci_scan_child_bus(struct pci_bus *bus);/* * If it's a bridge, configure it and scan the bus behind it. * For CardBus bridges, we don't scan behind as the devices will * be handled by the bridge driver itself. * * We need to process bridges in two passes -- first we scan those * already configured by the BIOS and after we are done with all of * them, we proceed to assigning numbers to the remaining buses in * order to avoid overlaps between old and new bus numbers. */int pci_scan_bridge(struct pci_bus *bus, struct pci_dev * dev, int max, int pass){	struct pci_bus *child;	int is_cardbus = (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS);	u32 buses, i, j = 0;	u16 bctl;	pci_read_config_dword(dev, PCI_PRIMARY_BUS, &buses);	pr_debug("PCI: Scanning behind PCI bridge %s, config %06x, pass %d\n",		 pci_name(dev), buses & 0xffffff, pass);	/* Disable MasterAbortMode during probing to avoid reporting	   of bus errors (in some architectures) */ 	pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &bctl);	pci_write_config_word(dev, PCI_BRIDGE_CONTROL,			      bctl & ~PCI_BRIDGE_CTL_MASTER_ABORT);	if ((buses & 0xffff00) && !pcibios_assign_all_busses() && !is_cardbus) {		unsigned int cmax, busnr;		/*		 * Bus already configured by firmware, process it in the first		 * pass and just note the configuration.		 */		if (pass)			goto out;		busnr = (buses >> 8) & 0xFF;		/*		 * If we already got to this bus through a different bridge,		 * ignore it.  This can happen with the i450NX chipset.		 */		if (pci_find_bus(pci_domain_nr(bus), busnr)) {			printk(KERN_INFO "PCI: Bus %04x:%02x already known\n",					pci_domain_nr(bus), busnr);			goto out;		}		child = pci_add_new_bus(bus, dev, busnr);		if (!child)			goto out;		child->primary = buses & 0xFF;		child->subordinate = (buses >> 16) & 0xFF;		child->bridge_ctl = bctl;		cmax = pci_scan_child_bus(child);		if (cmax > max)			max = cmax;		if (child->subordinate > max)			max = child->subordinate;	} else {		/*		 * We need to assign a number to this bus which we always		 * do in the second pass.		 */		if (!pass) {			if (pcibios_assign_all_busses())				/* Temporarily disable forwarding of the				   configuration cycles on all bridges in				   this bus segment to avoid possible				   conflicts in the second pass between two				   bridges programmed with overlapping				   bus ranges. */				pci_write_config_dword(dev, PCI_PRIMARY_BUS,						       buses & ~0xffffff);			goto out;		}		/* Clear errors */		pci_write_config_word(dev, PCI_STATUS, 0xffff);		/* Prevent assigning a bus number that already exists.		 * This can happen when a bridge is hot-plugged */		if (pci_find_bus(pci_domain_nr(bus), max+1))			goto out;		child = pci_add_new_bus(bus, dev, ++max);		buses = (buses & 0xff000000)		      | ((unsigned int)(child->primary)     <<  0)		      | ((unsigned int)(child->secondary)   <<  8)		      | ((unsigned int)(child->subordinate) << 16);		/*		 * yenta.c forces a secondary latency timer of 176.		 * Copy that behaviour here.		 */		if (is_cardbus) {			buses &= ~0xff000000;			buses |= CARDBUS_LATENCY_TIMER << 24;		}					/*		 * We need to blast all three values with a single write.		 */		pci_write_config_dword(dev, PCI_PRIMARY_BUS, buses);		if (!is_cardbus) {			child->bridge_ctl = bctl;			/*			 * Adjust subordinate busnr in parent buses.			 * We do this before scanning for children because			 * some devices may not be detected if the bios			 * was lazy.			 */			pci_fixup_parent_subordinate_busnr(child, max);			/* Now we can scan all subordinate buses... */			max = pci_scan_child_bus(child);			/*			 * now fix it up again since we have found			 * the real value of max.			 */			pci_fixup_parent_subordinate_busnr(child, max);		} else {			/*			 * For CardBus bridges, we leave 4 bus numbers			 * as cards with a PCI-to-PCI bridge can be			 * inserted later.			 */			for (i=0; i<CARDBUS_RESERVE_BUSNR; i++) {				struct pci_bus *parent = bus;				if (pci_find_bus(pci_domain_nr(bus),							max+i+1))					break;				while (parent->parent) {					if ((!pcibios_assign_all_busses()) &&					    (parent->subordinate > max) &&					    (parent->subordinate <= max+i)) {						j = 1;					}					parent = parent->parent;				}				if (j) {					/*					 * Often, there are two cardbus bridges					 * -- try to leave one valid bus number					 * for each one.					 */					i /= 2;					break;				}			}			max += i;			pci_fixup_parent_subordinate_busnr(child, max);		}		/*		 * Set the subordinate bus number to its real value.		 */		child->subordinate = max;		pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, max);	}	sprintf(child->name, (is_cardbus ? "PCI CardBus #%02x" : "PCI Bus #%02x"), child->number);	/* Has only triggered on CardBus, fixup is in yenta_socket */	while (bus->parent) {		if ((child->subordinate > bus->subordinate) ||		    (child->number > bus->subordinate) ||		    (child->number < bus->number) ||		    (child->subordinate < bus->number)) {			pr_debug("PCI: Bus #%02x (-#%02x) is %s"				"hidden behind%s bridge #%02x (-#%02x)\n",				child->number, child->subordinate,				(bus->number > child->subordinate &&				 bus->subordinate < child->number) ?					"wholly " : " partially",				bus->self->transparent ? " transparent" : " ",				bus->number, bus->subordinate);		}		bus = bus->parent;	}out:	pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bctl);	return max;}/* * Read interrupt line and base address registers. * The architecture-dependent code can tweak these, of course. */static void pci_read_irq(struct pci_dev *dev){	unsigned char irq;	pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq);	dev->pin = irq;	if (irq)		pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq);	dev->irq = irq;}#define LEGACY_IO_RESOURCE	(IORESOURCE_IO | IORESOURCE_PCI_FIXED)/** * pci_setup_device - fill in class and map information of a device * @dev: the device structure to fill * * Initialize the device structure with information about the device's  * vendor,class,memory and IO-space addresses,IRQ lines etc. * Called at initialisation of the PCI subsystem and by CardBus services. * Returns 0 on success and -1 if unknown type of device (not normal, bridge * or CardBus). */static int pci_setup_device(struct pci_dev * dev){	u32 class;	sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(dev->bus),		dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));	pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);	dev->revision = class & 0xff;	class >>= 8;				    /* upper 3 bytes */	dev->class = class;	class >>= 8;	pr_debug("PCI: Found %s [%04x/%04x] %06x %02x\n", pci_name(dev),		 dev->vendor, dev->device, class, dev->hdr_type);	/* "Unknown power state" */	dev->current_state = PCI_UNKNOWN;	/* Early fixups, before probing the BARs */	pci_fixup_device(pci_fixup_early, dev);	class = dev->class >> 8;	switch (dev->hdr_type) {		    /* header type */	case PCI_HEADER_TYPE_NORMAL:		    /* standard header */		if (class == PCI_CLASS_BRIDGE_PCI)			goto bad;		pci_read_irq(dev);		pci_read_bases(dev, 6, PCI_ROM_ADDRESS);		pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);		pci_read_config_word(dev, PCI_SUBSYSTEM_ID, &dev->subsystem_device);		/*		 *	Do the ugly legacy mode stuff here rather than broken chip		 *	quirk code. Legacy mode ATA controllers have fixed		 *	addresses. These are not always echoed in BAR0-3, and		 *	BAR0-3 in a few cases contain junk!		 */		if (class == PCI_CLASS_STORAGE_IDE) {			u8 progif;			pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);			if ((progif & 1) == 0) {				dev->resource[0].start = 0x1F0;				dev->resource[0].end = 0x1F7;				dev->resource[0].flags = LEGACY_IO_RESOURCE;				dev->resource[1].start = 0x3F6;				dev->resource[1].end = 0x3F6;				dev->resource[1].flags = LEGACY_IO_RESOURCE;			}			if ((progif & 4) == 0) {				dev->resource[2].start = 0x170;				dev->resource[2].end = 0x177;				dev->resource[2].flags = LEGACY_IO_RESOURCE;				dev->resource[3].start = 0x376;				dev->resource[3].end = 0x376;				dev->resource[3].flags = LEGACY_IO_RESOURCE;			}		}		break;	case PCI_HEADER_TYPE_BRIDGE:		    /* bridge header */		if (class != PCI_CLASS_BRIDGE_PCI)			goto bad;		/* The PCI-to-PCI bridge spec requires that subtractive		   decoding (i.e. transparent) bridge must have programming		   interface code of 0x01. */ 		pci_read_irq(dev);		dev->transparent = ((dev->class & 0xff) == 1);		pci_read_bases(dev, 2, PCI_ROM_ADDRESS1);		break;	case PCI_HEADER_TYPE_CARDBUS:		    /* CardBus bridge header */		if (class != PCI_CLASS_BRIDGE_CARDBUS)			goto bad;		pci_read_irq(dev);		pci_read_bases(dev, 1, 0);		pci_read_config_word(dev, PCI_CB_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);		pci_read_config_word(dev, PCI_CB_SUBSYSTEM_ID, &dev->subsystem_device);		break;	default:				    /* unknown header */		printk(KERN_ERR "PCI: device %s has unknown header type %02x, ignoring.\n",			pci_name(dev), dev->hdr_type);		return -1;	bad:		printk(KERN_ERR "PCI: %s: class %x doesn't match header type %02x. Ignoring class.\n",		       pci_name(dev), class, dev->hdr_type);		dev->class = PCI_CLASS_NOT_DEFINED;	}	/* We found a fine healthy device, go go go... */	return 0;}/** * pci_release_dev - free a pci device structure when all users of it are finished. * @dev: device that's been disconnected * * Will be called only by the device core when all users of this pci device are * done. */static void pci_release_dev(struct device *dev){	struct pci_dev *pci_dev;	pci_dev = to_pci_dev(dev);	kfree(pci_dev);}static void set_pcie_port_type(struct pci_dev *pdev){	int pos;	u16 reg16;	pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);	if (!pos)		return;	pdev->is_pcie = 1;	pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, &reg16);	pdev->pcie_type = (reg16 & PCI_EXP_FLAGS_TYPE) >> 4;}/** * pci_cfg_space_size - get the configuration space size of the PCI device. * @dev: PCI device * * Regular PCI devices have 256 bytes, but PCI-X 2 and PCI Express devices * have 4096 bytes.  Even if the device is capable, that doesn't mean we can * access it.  Maybe we don't have a way to generate extended config space * accesses, or the device is behind a reverse Express bridge.  So we try * reading the dword at 0x100 which must either be 0 or a valid extended * capability header. */int pci_cfg_space_size(struct pci_dev *dev){	int pos;	u32 status;	pos = pci_find_capability(dev, PCI_CAP_ID_EXP);	if (!pos) {		pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);		if (!pos)			goto fail;		pci_read_config_dword(dev, pos + PCI_X_STATUS, &status);		if (!(status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ)))			goto fail;	}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?