home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lxapi32.zip / Linux / PCI / pci.c < prev    next >
C/C++ Source or Header  |  2002-04-26  |  57KB  |  2,034 lines

  1. /*
  2.  *    $Id: pci.c,v 1.2 2002/04/26 23:09:31 smilcke Exp $
  3.  *
  4.  *    PCI Bus Services, see include/linux/pci.h for further explanation.
  5.  *
  6.  *    Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter,
  7.  *    David Mosberger-Tang
  8.  *
  9.  *    Copyright 1997 -- 2000 Martin Mares <mj@ucw.cz>
  10.  */
  11.  
  12. #include <linux/config.h>
  13. #include <linux/module.h>
  14. #include <linux/types.h>
  15. #include <linux/kernel.h>
  16. #include <linux/pci.h>
  17. #include <linux/string.h>
  18. #include <linux/init.h>
  19. #include <linux/slab.h>
  20. #include <linux/ioport.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/pm.h>
  23. #include <linux/kmod.h>        /* for hotplug_path */
  24. #include <linux/bitops.h>
  25. #include <linux/delay.h>
  26.  
  27. #include <asm/page.h>
  28. #include <asm/dma.h>    /* isa_dma_bridge_buggy */
  29.  
  30. #undef DEBUG
  31.  
  32. #ifdef DEBUG
  33. #define DBG(x...) printk(x)
  34. #else
  35. #define DBG(x...)
  36. #endif
  37.  
  38. LIST_HEAD(pci_root_buses);
  39. LIST_HEAD(pci_devices);
  40.  
  41. /**
  42.  * pci_find_slot - locate PCI device from a given PCI slot
  43.  * @bus: number of PCI bus on which desired PCI device resides
  44.  * @devfn: encodes number of PCI slot in which the desired PCI
  45.  * device resides and the logical device number within that slot
  46.  * in case of multi-function devices.
  47.  *
  48.  * Given a PCI bus and slot/function number, the desired PCI device
  49.  * is located in system global list of PCI devices.  If the device
  50.  * is found, a pointer to its data structure is returned.  If no
  51.  * device is found, %NULL is returned.
  52.  */
  53. struct pci_dev *
  54. pci_find_slot(unsigned int bus, unsigned int devfn)
  55. {
  56.     struct pci_dev *dev;
  57.  
  58.     pci_for_each_dev(dev) {
  59.         if (dev->bus->number == bus && dev->devfn == devfn)
  60.             return dev;
  61.     }
  62.     return NULL;
  63. }
  64.  
  65. /**
  66.  * pci_find_subsys - begin or continue searching for a PCI device by vendor/subvendor/device/subdevice id
  67.  * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
  68.  * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
  69.  * @ss_vendor: PCI subsystem vendor id to match, or %PCI_ANY_ID to match all vendor ids
  70.  * @ss_device: PCI subsystem device id to match, or %PCI_ANY_ID to match all device ids
  71.  * @from: Previous PCI device found in search, or %NULL for new search.
  72.  *
  73.  * Iterates through the list of known PCI devices.  If a PCI device is
  74.  * found with a matching @vendor, @device, @ss_vendor and @ss_device, a pointer to its
  75.  * device structure is returned.  Otherwise, %NULL is returned.
  76.  * A new search is initiated by passing %NULL to the @from argument.
  77.  * Otherwise if @from is not %NULL, searches continue from next device on the global list.
  78.  */
  79. struct pci_dev *
  80. pci_find_subsys(unsigned int vendor, unsigned int device,
  81.         unsigned int ss_vendor, unsigned int ss_device,
  82.         const struct pci_dev *from)
  83. {
  84.     struct list_head *n = from ? from->global_list.next : pci_devices.next;
  85.  
  86.     while (n != &pci_devices) {
  87.         struct pci_dev *dev = pci_dev_g(n);
  88.         if ((vendor == PCI_ANY_ID || dev->vendor == vendor) &&
  89.             (device == PCI_ANY_ID || dev->device == device) &&
  90.             (ss_vendor == PCI_ANY_ID || dev->subsystem_vendor == ss_vendor) &&
  91.             (ss_device == PCI_ANY_ID || dev->subsystem_device == ss_device))
  92.             return dev;
  93.         n = n->next;
  94.     }
  95.     return NULL;
  96. }
  97.  
  98.  
  99. /**
  100.  * pci_find_device - begin or continue searching for a PCI device by vendor/device id
  101.  * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
  102.  * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
  103.  * @from: Previous PCI device found in search, or %NULL for new search.
  104.  *
  105.  * Iterates through the list of known PCI devices.  If a PCI device is
  106.  * found with a matching @vendor and @device, a pointer to its device structure is
  107.  * returned.  Otherwise, %NULL is returned.
  108.  * A new search is initiated by passing %NULL to the @from argument.
  109.  * Otherwise if @from is not %NULL, searches continue from next device on the global list.
  110.  */
  111. struct pci_dev *
  112. pci_find_device(unsigned int vendor, unsigned int device, const struct pci_dev *from)
  113. {
  114.     return pci_find_subsys(vendor, device, PCI_ANY_ID, PCI_ANY_ID, from);
  115. }
  116.  
  117.  
  118. /**
  119.  * pci_find_class - begin or continue searching for a PCI device by class
  120.  * @class: search for a PCI device with this class designation
  121.  * @from: Previous PCI device found in search, or %NULL for new search.
  122.  *
  123.  * Iterates through the list of known PCI devices.  If a PCI device is
  124.  * found with a matching @class, a pointer to its device structure is
  125.  * returned.  Otherwise, %NULL is returned.
  126.  * A new search is initiated by passing %NULL to the @from argument.
  127.  * Otherwise if @from is not %NULL, searches continue from next device
  128.  * on the global list.
  129.  */
  130. struct pci_dev *
  131. pci_find_class(unsigned int class, const struct pci_dev *from)
  132. {
  133.     struct list_head *n = from ? from->global_list.next : pci_devices.next;
  134.  
  135.     while (n != &pci_devices) {
  136.         struct pci_dev *dev = pci_dev_g(n);
  137.         if (dev->class == class)
  138.             return dev;
  139.         n = n->next;
  140.     }
  141.     return NULL;
  142. }
  143.  
  144. /**
  145.  * pci_find_capability - query for devices' capabilities
  146.  * @dev: PCI device to query
  147.  * @cap: capability code
  148.  *
  149.  * Tell if a device supports a given PCI capability.
  150.  * Returns the address of the requested capability structure within the
  151.  * device's PCI configuration space or 0 in case the device does not
  152.  * support it.  Possible values for @cap:
  153.  *
  154.  *  %PCI_CAP_ID_PM           Power Management
  155.  *
  156.  *  %PCI_CAP_ID_AGP          Accelerated Graphics Port
  157.  *
  158.  *  %PCI_CAP_ID_VPD          Vital Product Data
  159.  *
  160.  *  %PCI_CAP_ID_SLOTID       Slot Identification
  161.  *
  162.  *  %PCI_CAP_ID_MSI          Message Signalled Interrupts
  163.  *
  164.  *  %PCI_CAP_ID_CHSWP        CompactPCI HotSwap
  165.  */
  166. int
  167. pci_find_capability(struct pci_dev *dev, int cap)
  168. {
  169.     u16 status;
  170.     u8 pos, id;
  171.     int ttl = 48;
  172.  
  173.     pci_read_config_word(dev, PCI_STATUS, &status);
  174.     if (!(status & PCI_STATUS_CAP_LIST))
  175.         return 0;
  176.     switch (dev->hdr_type) {
  177.     case PCI_HEADER_TYPE_NORMAL:
  178.     case PCI_HEADER_TYPE_BRIDGE:
  179.         pci_read_config_byte(dev, PCI_CAPABILITY_LIST, &pos);
  180.         break;
  181.     case PCI_HEADER_TYPE_CARDBUS:
  182.         pci_read_config_byte(dev, PCI_CB_CAPABILITY_LIST, &pos);
  183.         break;
  184.     default:
  185.         return 0;
  186.     }
  187.     while (ttl-- && pos >= 0x40) {
  188.         pos &= ~3;
  189.         pci_read_config_byte(dev, pos + PCI_CAP_LIST_ID, &id);
  190.         if (id == 0xff)
  191.             break;
  192.         if (id == cap)
  193.             return pos;
  194.         pci_read_config_byte(dev, pos + PCI_CAP_LIST_NEXT, &pos);
  195.     }
  196.     return 0;
  197. }
  198.  
  199.  
  200. /**
  201.  * pci_find_parent_resource - return resource region of parent bus of given region
  202.  * @dev: PCI device structure contains resources to be searched
  203.  * @res: child resource record for which parent is sought
  204.  *
  205.  *  For given resource region of given device, return the resource
  206.  *  region of parent bus the given region is contained in or where
  207.  *  it should be allocated from.
  208.  */
  209. struct resource *
  210. pci_find_parent_resource(const struct pci_dev *dev, struct resource *res)
  211. {
  212.     const struct pci_bus *bus = dev->bus;
  213.     int i;
  214.     struct resource *best = NULL;
  215.  
  216.     for(i=0; i<4; i++) {
  217.         struct resource *r = bus->resource[i];
  218.         if (!r)
  219.             continue;
  220.         if (res->start && !(res->start >= r->start && res->end <= r->end))
  221.             continue;    /* Not contained */
  222.         if ((res->flags ^ r->flags) & (IORESOURCE_IO | IORESOURCE_MEM))
  223.             continue;    /* Wrong type */
  224.         if (!((res->flags ^ r->flags) & IORESOURCE_PREFETCH))
  225.             return r;    /* Exact match */
  226.         if ((res->flags & IORESOURCE_PREFETCH) && !(r->flags & IORESOURCE_PREFETCH))
  227.             best = r;    /* Approximating prefetchable by non-prefetchable */
  228.     }
  229.     return best;
  230. }
  231.  
  232. /**
  233.  * pci_set_power_state - Set the power state of a PCI device
  234.  * @dev: PCI device to be suspended
  235.  * @state: Power state we're entering
  236.  *
  237.  * Transition a device to a new power state, using the Power Management
  238.  * Capabilities in the device's config space.
  239.  *
  240.  * RETURN VALUE:
  241.  * -EINVAL if trying to enter a lower state than we're already in.
  242.  * 0 if we're already in the requested state.
  243.  * -EIO if device does not support PCI PM.
  244.  * 0 if we can successfully change the power state.
  245.  */
  246.  
  247. int
  248. pci_set_power_state(struct pci_dev *dev, int state)
  249. {
  250.     int pm;
  251.     u16 pmcsr;
  252.  
  253.     /* bound the state we're entering */
  254.     if (state > 3) state = 3;
  255.  
  256.     /* Validate current state:
  257.      * Can enter D0 from any state, but if we can only go deeper
  258.      * to sleep if we're already in a low power state
  259.      */
  260.     if (state > 0 && dev->current_state > state)
  261.         return -EINVAL;
  262.     else if (dev->current_state == state)
  263.         return 0;        /* we're already there */
  264.  
  265.     /* find PCI PM capability in list */
  266.     pm = pci_find_capability(dev, PCI_CAP_ID_PM);
  267.     
  268.     /* abort if the device doesn't support PM capabilities */
  269.     if (!pm) return -EIO;
  270.  
  271.     /* check if this device supports the desired state */
  272.     if (state == 1 || state == 2) {
  273.         u16 pmc;
  274.         pci_read_config_word(dev,pm + PCI_PM_PMC,&pmc);
  275.         if (state == 1 && !(pmc & PCI_PM_CAP_D1)) return -EIO;
  276.         else if (state == 2 && !(pmc & PCI_PM_CAP_D2)) return -EIO;
  277.     }
  278.  
  279.     /* If we're in D3, force entire word to 0.
  280.      * This doesn't affect PME_Status, disables PME_En, and
  281.      * sets PowerState to 0.
  282.      */
  283.     if (dev->current_state >= 3)
  284.         pmcsr = 0;
  285.     else {
  286.         pci_read_config_word(dev, pm + PCI_PM_CTRL, &pmcsr);
  287.         pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  288.         pmcsr |= state;
  289.     }
  290.  
  291.     /* enter specified state */
  292.     pci_write_config_word(dev, pm + PCI_PM_CTRL, pmcsr);
  293.  
  294.     /* Mandatory power management transition delays */
  295.     /* see PCI PM 1.1 5.6.1 table 18 */
  296.     if(state == 3 || dev->current_state == 3)
  297.     {
  298.         set_current_state(TASK_UNINTERRUPTIBLE);
  299.         schedule_timeout(HZ/100);
  300.     }
  301.     else if(state == 2 || dev->current_state == 2)
  302.         udelay(200);
  303.     dev->current_state = state;
  304.  
  305.     return 0;
  306. }
  307.  
  308. /**
  309.  * pci_save_state - save the PCI configuration space of a device before suspending
  310.  * @dev: - PCI device that we're dealing with
  311.  * @buffer: - buffer to hold config space context
  312.  *
  313.  * @buffer must be large enough to hold the entire PCI 2.2 config space
  314.  * (>= 64 bytes).
  315.  */
  316. int
  317. pci_save_state(struct pci_dev *dev, u32 *buffer)
  318. {
  319.     int i;
  320.     if (buffer) {
  321.         /* XXX: 100% dword access ok here? */
  322.         for (i = 0; i < 16; i++)
  323.             pci_read_config_dword(dev, i * 4,&buffer[i]);
  324.     }
  325.     return 0;
  326. }
  327.  
  328. /**
  329.  * pci_restore_state - Restore the saved state of a PCI device
  330.  * @dev: - PCI device that we're dealing with
  331.  * @buffer: - saved PCI config space
  332.  *
  333.  */
  334. int
  335. pci_restore_state(struct pci_dev *dev, u32 *buffer)
  336. {
  337.     int i;
  338.  
  339.     if (buffer) {
  340.         for (i = 0; i < 16; i++)
  341.             pci_write_config_dword(dev,i * 4, buffer[i]);
  342.     }
  343.     /*
  344.      * otherwise, write the context information we know from bootup.
  345.      * This works around a problem where warm-booting from Windows
  346.      * combined with a D3(hot)->D0 transition causes PCI config
  347.      * header data to be forgotten.
  348.      */    
  349.     else {
  350.         for (i = 0; i < 6; i ++)
  351.             pci_write_config_dword(dev,
  352.                            PCI_BASE_ADDRESS_0 + (i * 4),
  353.                            dev->resource[i].start);
  354.         pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
  355.     }
  356.     return 0;
  357. }
  358.  
  359. /**
  360.  * pci_enable_device - Initialize device before it's used by a driver.
  361.  * @dev: PCI device to be initialized
  362.  *
  363.  *  Initialize device before it's used by a driver. Ask low-level code
  364.  *  to enable I/O and memory. Wake up the device if it was suspended.
  365.  *  Beware, this function can fail.
  366.  */
  367. int
  368. pci_enable_device(struct pci_dev *dev)
  369. {
  370.     int err;
  371.  
  372.     pci_set_power_state(dev, 0);
  373.     if ((err = pcibios_enable_device(dev)) < 0)
  374.         return err;
  375.     return 0;
  376. }
  377.  
  378. /**
  379.  * pci_disable_device - Disable PCI device after use
  380.  * @dev: PCI device to be disabled
  381.  *
  382.  * Signal to the system that the PCI device is not in use by the system
  383.  * anymore.  This only involves disabling PCI bus-mastering, if active.
  384.  */
  385. void
  386. pci_disable_device(struct pci_dev *dev)
  387. {
  388.     u16 pci_command;
  389.  
  390.     pci_read_config_word(dev, PCI_COMMAND, &pci_command);
  391.     if (pci_command & PCI_COMMAND_MASTER) {
  392.         pci_command &= ~PCI_COMMAND_MASTER;
  393.         pci_write_config_word(dev, PCI_COMMAND, pci_command);
  394.     }
  395. }
  396.  
  397. /**
  398.  * pci_enable_wake - enable device to generate PME# when suspended
  399.  * @dev: - PCI device to operate on
  400.  * @state: - Current state of device.
  401.  * @enable: - Flag to enable or disable generation
  402.  *
  403.  * Set the bits in the device's PM Capabilities to generate PME# when
  404.  * the system is suspended.
  405.  *
  406.  * -EIO is returned if device doesn't have PM Capabilities.
  407.  * -EINVAL is returned if device supports it, but can't generate wake events.
  408.  * 0 if operation is successful.
  409.  *
  410.  */
  411. int pci_enable_wake(struct pci_dev *dev, u32 state, int enable)
  412. {
  413.     int pm;
  414.     u16 value;
  415.  
  416.     /* find PCI PM capability in list */
  417.     pm = pci_find_capability(dev, PCI_CAP_ID_PM);
  418.  
  419.     /* If device doesn't support PM Capabilities, but request is to disable
  420.      * wake events, it's a nop; otherwise fail */
  421.     if (!pm)
  422.         return enable ? -EIO : 0;
  423.  
  424.     /* Check device's ability to generate PME# */
  425.     pci_read_config_word(dev,pm+PCI_PM_PMC,&value);
  426.  
  427.     value &= PCI_PM_CAP_PME_MASK;
  428.     value >>= ffs(value);   /* First bit of mask */
  429.  
  430.     /* Check if it can generate PME# from requested state. */
  431.     if (!value || !(value & (1 << state)))
  432.         return enable ? -EINVAL : 0;
  433.  
  434.     pci_read_config_word(dev, pm + PCI_PM_CTRL, &value);
  435.  
  436.     /* Clear PME_Status by writing 1 to it and enable PME# */
  437.     value |= PCI_PM_CTRL_PME_STATUS | PCI_PM_CTRL_PME_ENABLE;
  438.  
  439.     if (!enable)
  440.         value &= ~PCI_PM_CTRL_PME_ENABLE;
  441.  
  442.     pci_write_config_word(dev, pm + PCI_PM_CTRL, value);
  443.     
  444.     return 0;
  445. }
  446.  
  447. int
  448. pci_get_interrupt_pin(struct pci_dev *dev, struct pci_dev **bridge)
  449. {
  450.     u8 pin;
  451.  
  452.     pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
  453.     if (!pin)
  454.         return -1;
  455.     pin--;
  456.     while (dev->bus->self) {
  457.         pin = (pin + PCI_SLOT(dev->devfn)) % 4;
  458.         dev = dev->bus->self;
  459.     }
  460.     *bridge = dev;
  461.     return pin;
  462. }
  463.  
  464. /**
  465.  *    pci_release_regions - Release reserved PCI I/O and memory resources
  466.  *    @pdev: PCI device whose resources were previously reserved by pci_request_regions
  467.  *
  468.  *    Releases all PCI I/O and memory resources previously reserved by a
  469.  *    successful call to pci_request_regions.  Call this function only
  470.  *    after all use of the PCI regions has ceased.
  471.  */
  472. void pci_release_regions(struct pci_dev *pdev)
  473. {
  474.     int i;
  475.     
  476.     for (i = 0; i < 6; i++) {
  477.         if (pci_resource_len(pdev, i) == 0)
  478.             continue;
  479.  
  480.         if (pci_resource_flags(pdev, i) & IORESOURCE_IO)
  481.             release_region(pci_resource_start(pdev, i),
  482.                        pci_resource_len(pdev, i));
  483.  
  484.         else if (pci_resource_flags(pdev, i) & IORESOURCE_MEM)
  485.             release_mem_region(pci_resource_start(pdev, i),
  486.                        pci_resource_len(pdev, i));
  487.     }
  488. }
  489.  
  490. /**
  491.  *    pci_request_regions - Reserved PCI I/O and memory resources
  492.  *    @pdev: PCI device whose resources are to be reserved
  493.  *    @res_name: Name to be associated with resource.
  494.  *
  495.  *    Mark all PCI regions associated with PCI device @pdev as
  496.  *    being reserved by owner @res_name.  Do not access any
  497.  *    address inside the PCI regions unless this call returns
  498.  *    successfully.
  499.  *
  500.  *    Returns 0 on success, or %EBUSY on error.  A warning
  501.  *    message is also printed on failure.
  502.  */
  503. int pci_request_regions(struct pci_dev *pdev, char *res_name)
  504. {
  505.     int i;
  506.     
  507.     for (i = 0; i < 6; i++) {
  508.         if (pci_resource_len(pdev, i) == 0)
  509.             continue;
  510.  
  511.         if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
  512.             if (!request_region(pci_resource_start(pdev, i),
  513.                         pci_resource_len(pdev, i), res_name))
  514.                 goto err_out;
  515.         }
  516.         
  517.         else if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
  518.             if (!request_mem_region(pci_resource_start(pdev, i),
  519.                             pci_resource_len(pdev, i), res_name))
  520.                 goto err_out;
  521.         }
  522.     }
  523.     
  524.     return 0;
  525.  
  526. err_out:
  527.     printk (KERN_WARNING "PCI: Unable to reserve %s region #%d:%lx@%lx for device %s\n",
  528.         pci_resource_flags(pdev, i) & IORESOURCE_IO ? "I/O" : "mem",
  529.         i + 1, /* PCI BAR # */
  530.         pci_resource_len(pdev, i), pci_resource_start(pdev, i),
  531.         pdev->slot_name);
  532.     pci_release_regions(pdev);
  533.     return -EBUSY;
  534. }
  535.  
  536.  
  537. /*
  538.  *  Registration of PCI drivers and handling of hot-pluggable devices.
  539.  */
  540.  
  541. static LIST_HEAD(pci_drivers);
  542.  
  543. /**
  544.  * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
  545.  * @ids: array of PCI device id structures to search in
  546.  * @dev: the PCI device structure to match against
  547.  *
  548.  * Used by a driver to check whether a PCI device present in the
  549.  * system is in its list of supported devices.Returns the matching
  550.  * pci_device_id structure or %NULL if there is no match.
  551.  */
  552. const struct pci_device_id *
  553. pci_match_device(const struct pci_device_id *ids, const struct pci_dev *dev)
  554. {
  555.     while (ids->vendor || ids->subvendor || ids->class_mask) {
  556.         if ((ids->vendor == PCI_ANY_ID || ids->vendor == dev->vendor) &&
  557.             (ids->device == PCI_ANY_ID || ids->device == dev->device) &&
  558.             (ids->subvendor == PCI_ANY_ID || ids->subvendor == dev->subsystem_vendor) &&
  559.             (ids->subdevice == PCI_ANY_ID || ids->subdevice == dev->subsystem_device) &&
  560.             !((ids->class ^ dev->class) & ids->class_mask))
  561.             return ids;
  562.         ids++;
  563.     }
  564.     return NULL;
  565. }
  566.  
  567. static int
  568. pci_announce_device(struct pci_driver *drv, struct pci_dev *dev)
  569. {
  570.     const struct pci_device_id *id;
  571.     int ret = 0;
  572.  
  573.     if (drv->id_table) {
  574.         id = pci_match_device(drv->id_table, dev);
  575.         if (!id) {
  576.             ret = 0;
  577.             goto out;
  578.         }
  579.     } else
  580.         id = NULL;
  581.  
  582.     dev_probe_lock();
  583.     if (drv->probe(dev, id) >= 0) {
  584.         dev->driver = drv;
  585.         ret = 1;
  586.     }
  587.     dev_probe_unlock();
  588. out:
  589.     return ret;
  590. }
  591.  
  592. /**
  593.  * pci_register_driver - register a new pci driver
  594.  * @drv: the driver structure to register
  595.  *
  596.  * Adds the driver structure to the list of registered drivers
  597.  * Returns the number of pci devices which were claimed by the driver
  598.  * during registration.  The driver remains registered even if the
  599.  * return value is zero.
  600.  */
  601. int
  602. pci_register_driver(struct pci_driver *drv)
  603. {
  604.     struct pci_dev *dev;
  605.     int count = 0;
  606.  
  607.     list_add_tail(&drv->node, &pci_drivers);
  608.     pci_for_each_dev(dev) {
  609.         if (!pci_dev_driver(dev))
  610.             count += pci_announce_device(drv, dev);
  611.     }
  612.     return count;
  613. }
  614.  
  615. /**
  616.  * pci_unregister_driver - unregister a pci driver
  617.  * @drv: the driver structure to unregister
  618.  *
  619.  * Deletes the driver structure from the list of registered PCI drivers,
  620.  * gives it a chance to clean up by calling its remove() function for
  621.  * each device it was responsible for, and marks those devices as
  622.  * driverless.
  623.  */
  624.  
  625. void
  626. pci_unregister_driver(struct pci_driver *drv)
  627. {
  628.     struct pci_dev *dev;
  629.  
  630.     list_del(&drv->node);
  631.     pci_for_each_dev(dev) {
  632.         if (dev->driver == drv) {
  633.             if (drv->remove)
  634.                 drv->remove(dev);
  635.             dev->driver = NULL;
  636.         }
  637.     }
  638. }
  639.  
  640. #ifdef CONFIG_HOTPLUG
  641.  
  642. #ifndef FALSE
  643. #define FALSE    (0)
  644. #define TRUE    (!FALSE)
  645. #endif
  646.  
  647. static void
  648. run_sbin_hotplug(struct pci_dev *pdev, int insert)
  649. {
  650.     int i;
  651.     char *argv[3], *envp[8];
  652.     char id[20], sub_id[24], bus_id[24], class_id[20];
  653.  
  654.     if (!hotplug_path[0])
  655.         return;
  656.  
  657.     sprintf(class_id, "PCI_CLASS=%04X", pdev->class);
  658.     sprintf(id, "PCI_ID=%04X:%04X", pdev->vendor, pdev->device);
  659.     sprintf(sub_id, "PCI_SUBSYS_ID=%04X:%04X", pdev->subsystem_vendor, pdev->subsystem_device);
  660.     sprintf(bus_id, "PCI_SLOT_NAME=%s", pdev->slot_name);
  661.  
  662.     i = 0;
  663.     argv[i++] = hotplug_path;
  664.     argv[i++] = "pci";
  665.     argv[i] = 0;
  666.  
  667.     i = 0;
  668.     /* minimal command environment */
  669.     envp[i++] = "HOME=/";
  670.     envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  671.     
  672.     /* other stuff we want to pass to /sbin/hotplug */
  673.     envp[i++] = class_id;
  674.     envp[i++] = id;
  675.     envp[i++] = sub_id;
  676.     envp[i++] = bus_id;
  677.     if (insert)
  678.         envp[i++] = "ACTION=add";
  679.     else
  680.         envp[i++] = "ACTION=remove";
  681.     envp[i] = 0;
  682.  
  683.     call_usermodehelper (argv [0], argv, envp);
  684. }
  685.  
  686. /**
  687.  * pci_announce_device_to_drivers - tell the drivers a new device has appeared
  688.  * @dev: the device that has shown up
  689.  *
  690.  * Notifys the drivers that a new device has appeared, and also notifys
  691.  * userspace through /sbin/hotplug.
  692.  */
  693. void
  694. pci_announce_device_to_drivers(struct pci_dev *dev)
  695. {
  696.     struct list_head *ln;
  697.  
  698.     for(ln=pci_drivers.next; ln != &pci_drivers; ln=ln->next) {
  699.         struct pci_driver *drv = list_entry(ln, struct pci_driver, node);
  700.         if (drv->remove && pci_announce_device(drv, dev))
  701.             break;
  702.     }
  703.  
  704.     /* notify userspace of new hotplug device */
  705.     run_sbin_hotplug(dev, TRUE);
  706. }
  707.  
  708. /**
  709.  * pci_insert_device - insert a hotplug device
  710.  * @dev: the device to insert
  711.  * @bus: where to insert it
  712.  *
  713.  * Add a new device to the device lists and notify userspace (/sbin/hotplug).
  714.  */
  715. void
  716. pci_insert_device(struct pci_dev *dev, struct pci_bus *bus)
  717. {
  718.     list_add_tail(&dev->bus_list, &bus->devices);
  719.     list_add_tail(&dev->global_list, &pci_devices);
  720. #ifdef CONFIG_PROC_FS
  721.     pci_proc_attach_device(dev);
  722. #endif
  723.     pci_announce_device_to_drivers(dev);
  724. }
  725.  
  726. static void
  727. pci_free_resources(struct pci_dev *dev)
  728. {
  729.     int i;
  730.  
  731.     for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  732.         struct resource *res = dev->resource + i;
  733.         if (res->parent)
  734.             release_resource(res);
  735.     }
  736. }
  737.  
  738. /**
  739.  * pci_remove_device - remove a hotplug device
  740.  * @dev: the device to remove
  741.  *
  742.  * Delete the device structure from the device lists and
  743.  * notify userspace (/sbin/hotplug).
  744.  */
  745. void
  746. pci_remove_device(struct pci_dev *dev)
  747. {
  748.     if (dev->driver) {
  749.         if (dev->driver->remove)
  750.             dev->driver->remove(dev);
  751.         dev->driver = NULL;
  752.     }
  753.     list_del(&dev->bus_list);
  754.     list_del(&dev->global_list);
  755.     pci_free_resources(dev);
  756. #ifdef CONFIG_PROC_FS
  757.     pci_proc_detach_device(dev);
  758. #endif
  759.  
  760.     /* notify userspace of hotplug device removal */
  761.     run_sbin_hotplug(dev, FALSE);
  762. }
  763.  
  764. #endif
  765.  
  766. static struct pci_driver pci_compat_driver = {
  767.     name: "compat"
  768. };
  769.  
  770. /**
  771.  * pci_dev_driver - get the pci_driver of a device
  772.  * @dev: the device to query
  773.  *
  774.  * Returns the appropriate pci_driver structure or %NULL if there is no
  775.  * registered driver for the device.
  776.  */
  777. struct pci_driver *
  778. pci_dev_driver(const struct pci_dev *dev)
  779. {
  780.     if (dev->driver)
  781.         return dev->driver;
  782.     else {
  783.         int i;
  784.         for(i=0; i<=PCI_ROM_RESOURCE; i++)
  785.             if (dev->resource[i].flags & IORESOURCE_BUSY)
  786.                 return &pci_compat_driver;
  787.     }
  788.     return NULL;
  789. }
  790.  
  791.  
  792. /*
  793.  * This interrupt-safe spinlock protects all accesses to PCI
  794.  * configuration space.
  795.  */
  796.  
  797. static spinlock_t pci_lock = SPIN_LOCK_UNLOCKED;
  798.  
  799. /*
  800.  *  Wrappers for all PCI configuration access functions.  They just check
  801.  *  alignment, do locking and call the low-level functions pointed to
  802.  *  by pci_dev->ops.
  803.  */
  804.  
  805. #define PCI_byte_BAD 0
  806. #define PCI_word_BAD (pos & 1)
  807. #define PCI_dword_BAD (pos & 3)
  808.  
  809. #define PCI_OP(rw,size,type) \
  810. int pci_##rw##_config_##size (struct pci_dev *dev, int pos, type value) \
  811. {                                    \
  812.     int res;                            \
  813.     unsigned long flags;                        \
  814.     if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER;    \
  815.     spin_lock_irqsave(&pci_lock, flags);                \
  816.     res = dev->bus->ops->rw##_##size(dev, pos, value);        \
  817.     spin_unlock_irqrestore(&pci_lock, flags);            \
  818.     return res;                            \
  819. }
  820.  
  821. PCI_OP(read, byte, u8 *)
  822. PCI_OP(read, word, u16 *)
  823. PCI_OP(read, dword, u32 *)
  824. PCI_OP(write, byte, u8)
  825. PCI_OP(write, word, u16)
  826. PCI_OP(write, dword, u32)
  827.  
  828. /**
  829.  * pci_set_master - enables bus-mastering for device dev
  830.  * @dev: the PCI device to enable
  831.  *
  832.  * Enables bus-mastering on the device and calls pcibios_set_master()
  833.  * to do the needed arch specific settings.
  834.  */
  835. void
  836. pci_set_master(struct pci_dev *dev)
  837. {
  838.     u16 cmd;
  839.  
  840.     pci_read_config_word(dev, PCI_COMMAND, &cmd);
  841.     if (! (cmd & PCI_COMMAND_MASTER)) {
  842.         DBG("PCI: Enabling bus mastering for device %s\n", dev->slot_name);
  843.         cmd |= PCI_COMMAND_MASTER;
  844.         pci_write_config_word(dev, PCI_COMMAND, cmd);
  845.     }
  846.     pcibios_set_master(dev);
  847. }
  848.  
  849. int
  850. pci_set_dma_mask(struct pci_dev *dev, u64 mask)
  851. {
  852.     if (!pci_dma_supported(dev, mask))
  853.         return -EIO;
  854.  
  855.     dev->dma_mask = mask;
  856.  
  857.     return 0;
  858. }
  859.  
  860. int
  861. pci_dac_set_dma_mask(struct pci_dev *dev, u64 mask)
  862. {
  863.     if (!pci_dac_dma_supported(dev, mask))
  864.         return -EIO;
  865.  
  866.     dev->dma_mask = mask;
  867.  
  868.     return 0;
  869. }
  870.  
  871. /*
  872.  * Translate the low bits of the PCI base
  873.  * to the resource type
  874.  */
  875. static inline unsigned int pci_calc_resource_flags(unsigned int flags)
  876. {
  877.     if (flags & PCI_BASE_ADDRESS_SPACE_IO)
  878.         return IORESOURCE_IO;
  879.  
  880.     if (flags & PCI_BASE_ADDRESS_MEM_PREFETCH)
  881.         return IORESOURCE_MEM | IORESOURCE_PREFETCH;
  882.  
  883.     return IORESOURCE_MEM;
  884. }
  885.  
  886. /*
  887.  * Find the extent of a PCI decode..
  888.  */
  889. static u32 pci_size(u32 base, unsigned long mask)
  890. {
  891.     u32 size = mask & base;        /* Find the significant bits */
  892.     size = size & ~(size-1);    /* Get the lowest of them to find the decode size */
  893.     return size-1;            /* extent = size - 1 */
  894. }
  895.  
  896. static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
  897. {
  898.     unsigned int pos, reg, next;
  899.     u32 l, sz;
  900.     struct resource *res;
  901.  
  902.     for(pos=0; pos<howmany; pos = next) {
  903.         next = pos+1;
  904.         res = &dev->resource[pos];
  905.         res->name = dev->name;
  906.         reg = PCI_BASE_ADDRESS_0 + (pos << 2);
  907.         pci_read_config_dword(dev, reg, &l);
  908.         pci_write_config_dword(dev, reg, ~0);
  909.         pci_read_config_dword(dev, reg, &sz);
  910.         pci_write_config_dword(dev, reg, l);
  911.         if (!sz || sz == 0xffffffff)
  912.             continue;
  913.         if (l == 0xffffffff)
  914.             l = 0;
  915.         if ((l & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_MEMORY) {
  916.             res->start = l & PCI_BASE_ADDRESS_MEM_MASK;
  917.             sz = pci_size(sz, PCI_BASE_ADDRESS_MEM_MASK);
  918.         } else {
  919.             res->start = l & PCI_BASE_ADDRESS_IO_MASK;
  920.             sz = pci_size(sz, PCI_BASE_ADDRESS_IO_MASK & 0xffff);
  921.         }
  922.         res->end = res->start + (unsigned long) sz;
  923.         res->flags |= (l & 0xf) | pci_calc_resource_flags(l);
  924.         if ((l & (PCI_BASE_ADDRESS_SPACE | PCI_BASE_ADDRESS_MEM_TYPE_MASK))
  925.             == (PCI_BASE_ADDRESS_SPACE_MEMORY | PCI_BASE_ADDRESS_MEM_TYPE_64)) {
  926.             pci_read_config_dword(dev, reg+4, &l);
  927.             next++;
  928. #if BITS_PER_LONG == 64
  929.             res->start |= ((unsigned long) l) << 32;
  930.             res->end = res->start + sz;
  931.             pci_write_config_dword(dev, reg+4, ~0);
  932.             pci_read_config_dword(dev, reg+4, &sz);
  933.             pci_write_config_dword(dev, reg+4, l);
  934.             if (~sz)
  935.                 res->end = res->start + 0xffffffff +
  936.                         (((unsigned long) ~sz) << 32);
  937. #else
  938.             if (l) {
  939.                 printk(KERN_ERR "PCI: Unable to handle 64-bit address for device %s\n", dev->slot_name);
  940.                 res->start = 0;
  941.                 res->flags = 0;
  942.                 continue;
  943.             }
  944. #endif
  945.         }
  946.     }
  947.     if (rom) {
  948.         dev->rom_base_reg = rom;
  949.         res = &dev->resource[PCI_ROM_RESOURCE];
  950.         pci_read_config_dword(dev, rom, &l);
  951.         pci_write_config_dword(dev, rom, ~PCI_ROM_ADDRESS_ENABLE);
  952.         pci_read_config_dword(dev, rom, &sz);
  953.         pci_write_config_dword(dev, rom, l);
  954.         if (l == 0xffffffff)
  955.             l = 0;
  956.         if (sz && sz != 0xffffffff) {
  957.             res->flags = (l & PCI_ROM_ADDRESS_ENABLE) |
  958.               IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
  959.             res->start = l & PCI_ROM_ADDRESS_MASK;
  960.             sz = pci_size(sz, PCI_ROM_ADDRESS_MASK);
  961.             res->end = res->start + (unsigned long) sz;
  962.         }
  963.         res->name = dev->name;
  964.     }
  965. }
  966.  
  967. void __devinit  pci_read_bridge_bases(struct pci_bus *child)
  968. {
  969.     struct pci_dev *dev = child->self;
  970.     u8 io_base_lo, io_limit_lo;
  971.     u16 mem_base_lo, mem_limit_lo;
  972.     unsigned long base, limit;
  973.     struct resource *res;
  974.     int i;
  975.  
  976.     if (!dev)        /* It's a host bus, nothing to read */
  977.         return;
  978.  
  979.     for(i=0; i<3; i++)
  980.         child->resource[i] = &dev->resource[PCI_BRIDGE_RESOURCES+i];
  981.  
  982.     res = child->resource[0];
  983.     pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo);
  984.     pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo);
  985.     base = (io_base_lo & PCI_IO_RANGE_MASK) << 8;
  986.     limit = (io_limit_lo & PCI_IO_RANGE_MASK) << 8;
  987.  
  988.     if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
  989.         u16 io_base_hi, io_limit_hi;
  990.         pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi);
  991.         pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi);
  992.         base |= (io_base_hi << 16);
  993.         limit |= (io_limit_hi << 16);
  994.     }
  995.  
  996.     if (base && base <= limit) {
  997.         res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;
  998.         res->start = base;
  999.         res->end = limit + 0xfff;
  1000.         res->name = child->name;
  1001.     } else {
  1002.         /*
  1003.          * Ugh. We don't know enough about this bridge. Just assume
  1004.          * that it's entirely transparent.
  1005.          */
  1006.         printk(KERN_ERR "Unknown bridge resource %d: assuming transparent\n", 0);
  1007.         child->resource[0] = child->parent->resource[0];
  1008.     }
  1009.  
  1010.     res = child->resource[1];
  1011.     pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo);
  1012.     pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo);
  1013.     base = (mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16;
  1014.     limit = (mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16;
  1015.     if (base && base <= limit) {
  1016.         res->flags = (mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM;
  1017.         res->start = base;
  1018.         res->end = limit + 0xfffff;
  1019.         res->name = child->name;
  1020.     } else {
  1021.         /* See comment above. Same thing */
  1022.         printk(KERN_ERR "Unknown bridge resource %d: assuming transparent\n", 1);
  1023.         child->resource[1] = child->parent->resource[1];
  1024.     }
  1025.  
  1026.     res = child->resource[2];
  1027.     pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo);
  1028.     pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo);
  1029.     base = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16;
  1030.     limit = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16;
  1031.  
  1032.     if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
  1033.         u32 mem_base_hi, mem_limit_hi;
  1034.         pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi);
  1035.         pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi);
  1036. #if BITS_PER_LONG == 64
  1037.         base |= ((long) mem_base_hi) << 32;
  1038.         limit |= ((long) mem_limit_hi) << 32;
  1039. #else
  1040.         if (mem_base_hi || mem_limit_hi) {
  1041.             printk(KERN_ERR "PCI: Unable to handle 64-bit address space for %s\n", child->name);
  1042.             return;
  1043.         }
  1044. #endif
  1045.     }
  1046.     if (base && base <= limit) {
  1047.         res->flags = (mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM | IORESOURCE_PREFETCH;
  1048.         res->start = base;
  1049.         res->end = limit + 0xfffff;
  1050.         res->name = child->name;
  1051.     } else {
  1052.         /* See comments above */
  1053.         printk(KERN_ERR "Unknown bridge resource %d: assuming transparent\n", 2);
  1054.         child->resource[2] = child->parent->resource[2];
  1055.     }
  1056. }
  1057.  
  1058. static struct pci_bus * __devinit  pci_alloc_bus(void)
  1059. {
  1060.     struct pci_bus *b;
  1061.  
  1062.     b = kmalloc(sizeof(*b), GFP_KERNEL);
  1063.     if (b) {
  1064.         memset(b, 0, sizeof(*b));
  1065.         INIT_LIST_HEAD(&b->children);
  1066.         INIT_LIST_HEAD(&b->devices);
  1067.     }
  1068.     return b;
  1069. }
  1070.  
  1071. struct pci_bus * __devinit pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev, int busnr)
  1072. {
  1073.     struct pci_bus *child;
  1074.     int i;
  1075.  
  1076.     /*
  1077.      * Allocate a new bus, and inherit stuff from the parent..
  1078.      */
  1079.     child = pci_alloc_bus();
  1080.  
  1081.     list_add_tail(&child->node, &parent->children);
  1082.     child->self = dev;
  1083.     dev->subordinate = child;
  1084.     child->parent = parent;
  1085.     child->ops = parent->ops;
  1086.     child->sysdata = parent->sysdata;
  1087.  
  1088.     /*
  1089.      * Set up the primary, secondary and subordinate
  1090.      * bus numbers.
  1091.      */
  1092.     child->number = child->secondary = busnr;
  1093.     child->primary = parent->secondary;
  1094.     child->subordinate = 0xff;
  1095.  
  1096.     /* Set up default resource pointers.. */
  1097.     for (i = 0; i < 4; i++)
  1098.         child->resource[i] = &dev->resource[PCI_BRIDGE_RESOURCES+i];
  1099.  
  1100.     return child;
  1101. }
  1102.  
  1103. unsigned int __devinit pci_do_scan_bus(struct pci_bus *bus);
  1104.  
  1105. /*
  1106.  * If it's a bridge, configure it and scan the bus behind it.
  1107.  * For CardBus bridges, we don't scan behind as the devices will
  1108.  * be handled by the bridge driver itself.
  1109.  *
  1110.  * We need to process bridges in two passes -- first we scan those
  1111.  * already configured by the BIOS and after we are done with all of
  1112.  * them, we proceed to assigning numbers to the remaining buses in
  1113.  * order to avoid overlaps between old and new bus numbers.
  1114.  */
  1115. static int __devinit pci_scan_bridge(struct pci_bus *bus, struct pci_dev * dev, int max, int pass)
  1116. {
  1117.     unsigned int buses;
  1118.     unsigned short cr;
  1119.     struct pci_bus *child;
  1120.     int is_cardbus = (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS);
  1121.  
  1122.     pci_read_config_dword(dev, PCI_PRIMARY_BUS, &buses);
  1123.     DBG("Scanning behind PCI bridge %s, config %06x, pass %d\n", dev->slot_name, buses & 0xffffff, pass);
  1124.     if ((buses & 0xffff00) && !pcibios_assign_all_busses()) {
  1125.         /*
  1126.          * Bus already configured by firmware, process it in the first
  1127.          * pass and just note the configuration.
  1128.          */
  1129.         if (pass)
  1130.             return max;
  1131.         child = pci_add_new_bus(bus, dev, 0);
  1132.         child->primary = buses & 0xFF;
  1133.         child->secondary = (buses >> 8) & 0xFF;
  1134.         child->subordinate = (buses >> 16) & 0xFF;
  1135.         child->number = child->secondary;
  1136.         if (!is_cardbus) {
  1137.             unsigned int cmax = pci_do_scan_bus(child);
  1138.             if (cmax > max) max = cmax;
  1139.         } else {
  1140.             unsigned int cmax = child->subordinate;
  1141.             if (cmax > max) max = cmax;
  1142.         }
  1143.     } else {
  1144.         /*
  1145.          * We need to assign a number to this bus which we always
  1146.          * do in the second pass. We also keep all address decoders
  1147.          * on the bridge disabled during scanning.  FIXME: Why?
  1148.          */
  1149.         if (!pass)
  1150.             return max;
  1151.         pci_read_config_word(dev, PCI_COMMAND, &cr);
  1152.         pci_write_config_word(dev, PCI_COMMAND, 0x0000);
  1153.         pci_write_config_word(dev, PCI_STATUS, 0xffff);
  1154.  
  1155.         child = pci_add_new_bus(bus, dev, ++max);
  1156.         buses = (buses & 0xff000000)
  1157.               | ((unsigned int)(child->primary)     <<  0)
  1158.               | ((unsigned int)(child->secondary)   <<  8)
  1159.               | ((unsigned int)(child->subordinate) << 16);
  1160.         /*
  1161.          * We need to blast all three values with a single write.
  1162.          */
  1163.         pci_write_config_dword(dev, PCI_PRIMARY_BUS, buses);
  1164.         if (!is_cardbus) {
  1165.             /* Now we can scan all subordinate buses... */
  1166.             max = pci_do_scan_bus(child);
  1167.         } else {
  1168.             /*
  1169.              * For CardBus bridges, we leave 4 bus numbers
  1170.              * as cards with a PCI-to-PCI bridge can be
  1171.              * inserted later.
  1172.              */
  1173.             max += 3;
  1174.         }
  1175.         /*
  1176.          * Set the subordinate bus number to its real value.
  1177.          */
  1178.         child->subordinate = max;
  1179.         pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, max);
  1180.         pci_write_config_word(dev, PCI_COMMAND, cr);
  1181.     }
  1182.     sprintf(child->name, (is_cardbus ? "PCI CardBus #%02x" : "PCI Bus #%02x"), child->number);
  1183.     return max;
  1184. }
  1185.  
  1186. /*
  1187.  * Read interrupt line and base address registers.
  1188.  * The architecture-dependent code can tweak these, of course.
  1189.  */
  1190. static void pci_read_irq(struct pci_dev *dev)
  1191. {
  1192.     unsigned char irq;
  1193.  
  1194.     pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq);
  1195.     if (irq)
  1196.         pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq);
  1197.     dev->irq = irq;
  1198. }
  1199.  
  1200. /**
  1201.  * pci_setup_device - fill in class and map information of a device
  1202.  * @dev: the device structure to fill
  1203.  *
  1204.  * Initialize the device structure with information about the device's
  1205.  * vendor,class,memory and IO-space addresses,IRQ lines etc.
  1206.  * Called at initialisation of the PCI subsystem and by CardBus services.
  1207.  * Returns 0 on success and -1 if unknown type of device (not normal, bridge
  1208.  * or CardBus).
  1209.  */
  1210. int pci_setup_device(struct pci_dev * dev)
  1211. {
  1212.     u32 class;
  1213.  
  1214.     sprintf(dev->slot_name, "%02x:%02x.%d", dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
  1215.     sprintf(dev->name, "PCI device %04x:%04x", dev->vendor, dev->device);
  1216.     
  1217.     pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
  1218.     class >>= 8;                    /* upper 3 bytes */
  1219.     dev->class = class;
  1220.     class >>= 8;
  1221.  
  1222.     DBG("Found %02x:%02x [%04x/%04x] %06x %02x\n", dev->bus->number, dev->devfn, dev->vendor, dev->device, class, dev->hdr_type);
  1223.  
  1224.     /* "Unknown power state" */
  1225.     dev->current_state = 4;
  1226.  
  1227.     switch (dev->hdr_type) {            /* header type */
  1228.     case PCI_HEADER_TYPE_NORMAL:            /* standard header */
  1229.         if (class == PCI_CLASS_BRIDGE_PCI)
  1230.             goto bad;
  1231.         pci_read_irq(dev);
  1232.         pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
  1233.         pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
  1234.         pci_read_config_word(dev, PCI_SUBSYSTEM_ID, &dev->subsystem_device);
  1235.         break;
  1236.  
  1237.     case PCI_HEADER_TYPE_BRIDGE:            /* bridge header */
  1238.         if (class != PCI_CLASS_BRIDGE_PCI)
  1239.             goto bad;
  1240.         pci_read_bases(dev, 2, PCI_ROM_ADDRESS1);
  1241.         break;
  1242.  
  1243.     case PCI_HEADER_TYPE_CARDBUS:            /* CardBus bridge header */
  1244.         if (class != PCI_CLASS_BRIDGE_CARDBUS)
  1245.             goto bad;
  1246.         pci_read_irq(dev);
  1247.         pci_read_bases(dev, 1, 0);
  1248.         pci_read_config_word(dev, PCI_CB_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
  1249.         pci_read_config_word(dev, PCI_CB_SUBSYSTEM_ID, &dev->subsystem_device);
  1250.         break;
  1251.  
  1252.     default:                    /* unknown header */
  1253.         printk(KERN_ERR "PCI: device %s has unknown header type %02x, ignoring.\n",
  1254.             dev->slot_name, dev->hdr_type);
  1255.         return -1;
  1256.  
  1257.     bad:
  1258.         printk(KERN_ERR "PCI: %s: class %x doesn't match header type %02x. Ignoring class.\n",
  1259.                dev->slot_name, class, dev->hdr_type);
  1260.         dev->class = PCI_CLASS_NOT_DEFINED;
  1261.     }
  1262.  
  1263.     /* We found a fine healthy device, go go go... */
  1264.     return 0;
  1265. }
  1266.  
  1267. /*
  1268.  * Read the config data for a PCI device, sanity-check it
  1269.  * and fill in the dev structure...
  1270.  */
  1271. struct pci_dev * __devinit pci_scan_device(struct pci_dev *temp)
  1272. {
  1273.     struct pci_dev *dev;
  1274.     u32 l;
  1275.  
  1276.     if (pci_read_config_dword(temp, PCI_VENDOR_ID, &l))
  1277.         return NULL;
  1278.  
  1279.     /* some broken boards return 0 or ~0 if a slot is empty: */
  1280.     if (l == 0xffffffff || l == 0x00000000 || l == 0x0000ffff || l == 0xffff0000)
  1281.         return NULL;
  1282.  
  1283.     dev = kmalloc(sizeof(*dev), GFP_KERNEL);
  1284.     if (!dev)
  1285.         return NULL;
  1286.  
  1287.     memcpy(dev, temp, sizeof(*dev));
  1288.     dev->vendor = l & 0xffff;
  1289.     dev->device = (l >> 16) & 0xffff;
  1290.  
  1291.     /* Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer)
  1292.        set this higher, assuming the system even supports it.  */
  1293.     dev->dma_mask = 0xffffffff;
  1294.     if (pci_setup_device(dev) < 0) {
  1295.         kfree(dev);
  1296.         dev = NULL;
  1297.     }
  1298.     return dev;
  1299. }
  1300.  
  1301. struct pci_dev * __devinit pci_scan_slot(struct pci_dev *temp)
  1302. {
  1303.     struct pci_bus *bus = temp->bus;
  1304.     struct pci_dev *dev;
  1305.     struct pci_dev *first_dev = NULL;
  1306.     int func = 0;
  1307.     int is_multi = 0;
  1308.     u8 hdr_type;
  1309.  
  1310.     for (func = 0; func < 8; func++, temp->devfn++) {
  1311.         if (func && !is_multi)        /* not a multi-function device */
  1312.             continue;
  1313.         if (pci_read_config_byte(temp, PCI_HEADER_TYPE, &hdr_type))
  1314.             continue;
  1315.         temp->hdr_type = hdr_type & 0x7f;
  1316.  
  1317.         dev = pci_scan_device(temp);
  1318.         if (!dev)
  1319.             continue;
  1320.         pci_name_device(dev);
  1321.         if (!func) {
  1322.             is_multi = hdr_type & 0x80;
  1323.             first_dev = dev;
  1324.         }
  1325.  
  1326.         /*
  1327.          * Link the device to both the global PCI device chain and
  1328.          * the per-bus list of devices.
  1329.          */
  1330.         list_add_tail(&dev->global_list, &pci_devices);
  1331.         list_add_tail(&dev->bus_list, &bus->devices);
  1332.  
  1333.         /* Fix up broken headers */
  1334.         pci_fixup_device(PCI_FIXUP_HEADER, dev);
  1335.     }
  1336.     return first_dev;
  1337. }
  1338.  
  1339. unsigned int __devinit pci_do_scan_bus(struct pci_bus *bus)
  1340. {
  1341.     unsigned int devfn, max, pass;
  1342.     struct list_head *ln;
  1343.     struct pci_dev *dev, dev0;
  1344.  
  1345.     DBG("Scanning bus %02x\n", bus->number);
  1346.     max = bus->secondary;
  1347.  
  1348.     /* Create a device template */
  1349.     memset(&dev0, 0, sizeof(dev0));
  1350.     dev0.bus = bus;
  1351.     dev0.sysdata = bus->sysdata;
  1352.  
  1353.     /* Go find them, Rover! */
  1354.     for (devfn = 0; devfn < 0x100; devfn += 8) {
  1355.         dev0.devfn = devfn;
  1356.         pci_scan_slot(&dev0);
  1357.     }
  1358.  
  1359.     /*
  1360.      * After performing arch-dependent fixup of the bus, look behind
  1361.      * all PCI-to-PCI bridges on this bus.
  1362.      */
  1363.     DBG("Fixups for bus %02x\n", bus->number);
  1364.     pcibios_fixup_bus(bus);
  1365.     for (pass=0; pass < 2; pass++)
  1366.         for (ln=bus->devices.next; ln != &bus->devices; ln=ln->next) {
  1367.             dev = pci_dev_b(ln);
  1368.             if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE || dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
  1369.                 max = pci_scan_bridge(bus, dev, max, pass);
  1370.         }
  1371.  
  1372.     /*
  1373.      * We've scanned the bus and so we know all about what's on
  1374.      * the other side of any bridges that may be on this bus plus
  1375.      * any devices.
  1376.      *
  1377.      * Return how far we've got finding sub-buses.
  1378.      */
  1379.     DBG("Bus scan for %02x returning with max=%02x\n", bus->number, max);
  1380.     return max;
  1381. }
  1382.  
  1383. int __devinit  pci_bus_exists(const struct list_head *list, int nr)
  1384. {
  1385.     const struct list_head *l;
  1386.  
  1387.     for(l=list->next; l != list; l = l->next) {
  1388.         const struct pci_bus *b = pci_bus_b(l);
  1389.         if (b->number == nr || pci_bus_exists(&b->children, nr))
  1390.             return 1;
  1391.     }
  1392.     return 0;
  1393. }
  1394.  
  1395. struct pci_bus * __devinit  pci_alloc_primary_bus(int bus)
  1396. {
  1397.     struct pci_bus *b;
  1398.  
  1399.     if (pci_bus_exists(&pci_root_buses, bus)) {
  1400.         /* If we already got to this bus through a different bridge, ignore it */
  1401.         DBG("PCI: Bus %02x already known\n", bus);
  1402.         return NULL;
  1403.     }
  1404.  
  1405.     b = pci_alloc_bus();
  1406.     list_add_tail(&b->node, &pci_root_buses);
  1407.  
  1408.     b->number = b->secondary = bus;
  1409.     b->resource[0] = &ioport_resource;
  1410.     b->resource[1] = &iomem_resource;
  1411.     return b;
  1412. }
  1413.  
  1414. struct pci_bus * __devinit  pci_scan_bus(int bus, struct pci_ops *ops, void *sysdata)
  1415. {
  1416.     struct pci_bus *b = pci_alloc_primary_bus(bus);
  1417.     if (b) {
  1418.         b->sysdata = sysdata;
  1419.         b->ops = ops;
  1420.         b->subordinate = pci_do_scan_bus(b);
  1421.     }
  1422.     return b;
  1423. }
  1424.  
  1425. #ifdef CONFIG_PM
  1426.  
  1427. /*
  1428.  * PCI Power management..
  1429.  *
  1430.  * This needs to be done centralized, so that we power manage PCI
  1431.  * devices in the right order: we should not shut down PCI bridges
  1432.  * before we've shut down the devices behind them, and we should
  1433.  * not wake up devices before we've woken up the bridge to the
  1434.  * device.. Eh?
  1435.  *
  1436.  * We do not touch devices that don't have a driver that exports
  1437.  * a suspend/resume function. That is just too dangerous. If the default
  1438.  * PCI suspend/resume functions work for a device, the driver can
  1439.  * easily implement them (ie just have a suspend function that calls
  1440.  * the pci_set_power_state() function).
  1441.  */
  1442.  
  1443. static int pci_pm_save_state_device(struct pci_dev *dev, u32 state)
  1444. {
  1445.     int error = 0;
  1446.     if (dev) {
  1447.         struct pci_driver *driver = dev->driver;
  1448.         if (driver && driver->save_state)
  1449.             error = driver->save_state(dev,state);
  1450.     }
  1451.     return error;
  1452. }
  1453.  
  1454. static int pci_pm_suspend_device(struct pci_dev *dev, u32 state)
  1455. {
  1456.     int error = 0;
  1457.     if (dev) {
  1458.         struct pci_driver *driver = dev->driver;
  1459.         if (driver && driver->suspend)
  1460.             error = driver->suspend(dev,state);
  1461.     }
  1462.     return error;
  1463. }
  1464.  
  1465. static int pci_pm_resume_device(struct pci_dev *dev)
  1466. {
  1467.     int error = 0;
  1468.     if (dev) {
  1469.         struct pci_driver *driver = dev->driver;
  1470.         if (driver && driver->resume)
  1471.             error = driver->resume(dev);
  1472.     }
  1473.     return error;
  1474. }
  1475.  
  1476. static int pci_pm_save_state_bus(struct pci_bus *bus, u32 state)
  1477. {
  1478.     struct list_head *list;
  1479.     int error = 0;
  1480.  
  1481.     list_for_each(list, &bus->children) {
  1482.         error = pci_pm_save_state_bus(pci_bus_b(list),state);
  1483.         if (error) return error;
  1484.     }
  1485.     list_for_each(list, &bus->devices) {
  1486.         error = pci_pm_save_state_device(pci_dev_b(list),state);
  1487.         if (error) return error;
  1488.     }
  1489.     return 0;
  1490. }
  1491.  
  1492. static int pci_pm_suspend_bus(struct pci_bus *bus, u32 state)
  1493. {
  1494.     struct list_head *list;
  1495.  
  1496.     /* Walk the bus children list */
  1497.     list_for_each(list, &bus->children)
  1498.         pci_pm_suspend_bus(pci_bus_b(list),state);
  1499.  
  1500.     /* Walk the device children list */
  1501.     list_for_each(list, &bus->devices)
  1502.         pci_pm_suspend_device(pci_dev_b(list),state);
  1503.     return 0;
  1504. }
  1505.  
  1506. static int pci_pm_resume_bus(struct pci_bus *bus)
  1507. {
  1508.     struct list_head *list;
  1509.  
  1510.     /* Walk the device children list */
  1511.     list_for_each(list, &bus->devices)
  1512.         pci_pm_resume_device(pci_dev_b(list));
  1513.  
  1514.     /* And then walk the bus children */
  1515.     list_for_each(list, &bus->children)
  1516.         pci_pm_resume_bus(pci_bus_b(list));
  1517.     return 0;
  1518. }
  1519.  
  1520. static int pci_pm_save_state(u32 state)
  1521. {
  1522.     struct list_head *list;
  1523.     struct pci_bus *bus;
  1524.     int error = 0;
  1525.  
  1526.     list_for_each(list, &pci_root_buses) {
  1527.         bus = pci_bus_b(list);
  1528.         error = pci_pm_save_state_bus(bus,state);
  1529.         if (!error)
  1530.             error = pci_pm_save_state_device(bus->self,state);
  1531.     }
  1532.     return error;
  1533. }
  1534.  
  1535. static int pci_pm_suspend(u32 state)
  1536. {
  1537.     struct list_head *list;
  1538.     struct pci_bus *bus;
  1539.  
  1540.     list_for_each(list, &pci_root_buses) {
  1541.         bus = pci_bus_b(list);
  1542.         pci_pm_suspend_bus(bus,state);
  1543.         pci_pm_suspend_device(bus->self,state);
  1544.     }
  1545.     return 0;
  1546. }
  1547.  
  1548. static int pci_pm_resume(void)
  1549. {
  1550.     struct list_head *list;
  1551.     struct pci_bus *bus;
  1552.  
  1553.     list_for_each(list, &pci_root_buses) {
  1554.         bus = pci_bus_b(list);
  1555.         pci_pm_resume_device(bus->self);
  1556.         pci_pm_resume_bus(bus);
  1557.     }
  1558.     return 0;
  1559. }
  1560.  
  1561. static int
  1562. pci_pm_callback(struct pm_dev *pm_device, pm_request_t rqst, void *data)
  1563. {
  1564.     int error = 0;
  1565.  
  1566.     switch (rqst) {
  1567.     case PM_SAVE_STATE:
  1568.         error = pci_pm_save_state((u32)data);
  1569.         break;
  1570.     case PM_SUSPEND:
  1571.         error = pci_pm_suspend((u32)data);
  1572.         break;
  1573.     case PM_RESUME:
  1574.         error = pci_pm_resume();
  1575.         break;
  1576.     default: break;
  1577.     }
  1578.     return error;
  1579. }
  1580.  
  1581. #endif
  1582.  
  1583. /*
  1584.  * Pool allocator ... wraps the pci_alloc_consistent page allocator, so
  1585.  * small blocks are easily used by drivers for bus mastering controllers.
  1586.  * This should probably be sharing the guts of the slab allocator.
  1587.  */
  1588.  
  1589. struct pci_pool {    /* the pool */
  1590.     struct list_head    page_list;
  1591.     spinlock_t        lock;
  1592.     size_t            blocks_per_page;
  1593.     size_t            size;
  1594.     int            flags;
  1595.     struct pci_dev        *dev;
  1596.     size_t            allocation;
  1597.     char            name [32];
  1598.     wait_queue_head_t    waitq;
  1599. };
  1600.  
  1601. struct pci_page {    /* cacheable header for 'allocation' bytes */
  1602.     struct list_head    page_list;
  1603.     void            *vaddr;
  1604.     dma_addr_t        dma;
  1605.     unsigned long        bitmap [0];
  1606. };
  1607.  
  1608. #define    POOL_TIMEOUT_JIFFIES    ((100 /* msec */ * HZ) / 1000)
  1609. #define    POOL_POISON_BYTE    0xa7
  1610.  
  1611. // #define CONFIG_PCIPOOL_DEBUG
  1612.  
  1613.  
  1614. /**
  1615.  * pci_pool_create - Creates a pool of pci consistent memory blocks, for dma.
  1616.  * @name: name of pool, for diagnostics
  1617.  * @pdev: pci device that will be doing the DMA
  1618.  * @size: size of the blocks in this pool.
  1619.  * @align: alignment requirement for blocks; must be a power of two
  1620.  * @allocation: returned blocks won't cross this boundary (or zero)
  1621.  * @flags: SLAB_* flags (not all are supported).
  1622.  *
  1623.  * Returns a pci allocation pool with the requested characteristics, or
  1624.  * null if one can't be created.  Given one of these pools, pci_pool_alloc()
  1625.  * may be used to allocate memory.  Such memory will all have "consistent"
  1626.  * DMA mappings, accessible by the device and its driver without using
  1627.  * cache flushing primitives.  The actual size of blocks allocated may be
  1628.  * larger than requested because of alignment.
  1629.  *
  1630.  * If allocation is nonzero, objects returned from pci_pool_alloc() won't
  1631.  * cross that size boundary.  This is useful for devices which have
  1632.  * addressing restrictions on individual DMA transfers, such as not crossing
  1633.  * boundaries of 4KBytes.
  1634.  */
  1635. struct pci_pool *
  1636. pci_pool_create (const char *name, struct pci_dev *pdev,
  1637.     size_t size, size_t align, size_t allocation, int flags)
  1638. {
  1639.     struct pci_pool        *retval;
  1640.  
  1641.     if (align == 0)
  1642.         align = 1;
  1643.     if (size == 0)
  1644.         return 0;
  1645.     else if (size < align)
  1646.         size = align;
  1647.     else if ((size % align) != 0) {
  1648.         size += align + 1;
  1649.         size &= ~(align - 1);
  1650.     }
  1651.  
  1652.     if (allocation == 0) {
  1653.         if (PAGE_SIZE < size)
  1654.             allocation = size;
  1655.         else
  1656.             allocation = PAGE_SIZE;
  1657.         // FIXME: round up for less fragmentation
  1658.     } else if (allocation < size)
  1659.         return 0;
  1660.  
  1661.     if (!(retval = kmalloc (sizeof *retval, flags)))
  1662.         return retval;
  1663.  
  1664. #ifdef    CONFIG_PCIPOOL_DEBUG
  1665.     flags |= SLAB_POISON;
  1666. #endif
  1667.  
  1668.     strncpy (retval->name, name, sizeof retval->name);
  1669.     retval->name [sizeof retval->name - 1] = 0;
  1670.  
  1671.     retval->dev = pdev;
  1672.     INIT_LIST_HEAD (&retval->page_list);
  1673.     spin_lock_init (&retval->lock);
  1674.     retval->size = size;
  1675.     retval->flags = flags;
  1676.     retval->allocation = allocation;
  1677.     retval->blocks_per_page = allocation / size;
  1678.     init_waitqueue_head (&retval->waitq);
  1679.  
  1680. #ifdef CONFIG_PCIPOOL_DEBUG
  1681.     printk (KERN_DEBUG "pcipool create %s/%s size %d, %d/page (%d alloc)\n",
  1682.         pdev ? pdev->slot_name : NULL, retval->name, size,
  1683.         retval->blocks_per_page, allocation);
  1684. #endif
  1685.  
  1686.     return retval;
  1687. }
  1688.  
  1689.  
  1690. static struct pci_page *
  1691. pool_alloc_page (struct pci_pool *pool, int mem_flags)
  1692. {
  1693.     struct pci_page    *page;
  1694.     int        mapsize;
  1695.  
  1696.     mapsize = pool->blocks_per_page;
  1697.     mapsize = (mapsize + BITS_PER_LONG - 1) / BITS_PER_LONG;
  1698.     mapsize *= sizeof (long);
  1699.  
  1700.     page = (struct pci_page *) kmalloc (mapsize + sizeof *page, mem_flags);
  1701.     if (!page)
  1702.         return 0;
  1703.     page->vaddr = pci_alloc_consistent (pool->dev,
  1704.                         pool->allocation,
  1705.                         &page->dma);
  1706.     if (page->vaddr) {
  1707.         memset (page->bitmap, 0xff, mapsize);    // bit set == free
  1708.         if (pool->flags & SLAB_POISON)
  1709.             memset (page->vaddr, POOL_POISON_BYTE, pool->allocation);
  1710.         list_add (&page->page_list, &pool->page_list);
  1711.     } else {
  1712.         kfree (page);
  1713.         page = 0;
  1714.     }
  1715.     return page;
  1716. }
  1717.  
  1718.  
  1719. static inline int
  1720. is_page_busy (int blocks, unsigned long *bitmap)
  1721. {
  1722.     while (blocks > 0) {
  1723.         if (*bitmap++ != ~0UL)
  1724.             return 1;
  1725.         blocks -= BITS_PER_LONG;
  1726.     }
  1727.     return 0;
  1728. }
  1729.  
  1730. static void
  1731. pool_free_page (struct pci_pool *pool, struct pci_page *page)
  1732. {
  1733.     dma_addr_t    dma = page->dma;
  1734.  
  1735.     if (pool->flags & SLAB_POISON)
  1736.         memset (page->vaddr, POOL_POISON_BYTE, pool->allocation);
  1737.     pci_free_consistent (pool->dev, pool->allocation, page->vaddr, dma);
  1738.     list_del (&page->page_list);
  1739.     kfree (page);
  1740. }
  1741.  
  1742.  
  1743. /**
  1744.  * pci_pool_destroy - destroys a pool of pci memory blocks.
  1745.  * @pool: pci pool that will be destroyed
  1746.  *
  1747.  * Caller guarantees that no more memory from the pool is in use,
  1748.  * and that nothing will try to use the pool after this call.
  1749.  */
  1750. void
  1751. pci_pool_destroy (struct pci_pool *pool)
  1752. {
  1753.     unsigned long        flags;
  1754.  
  1755. #ifdef CONFIG_PCIPOOL_DEBUG
  1756.     printk (KERN_DEBUG "pcipool destroy %s/%s\n",
  1757.         pool->dev ? pool->dev->slot_name : NULL,
  1758.         pool->name);
  1759. #endif
  1760.  
  1761.     spin_lock_irqsave (&pool->lock, flags);
  1762.     while (!list_empty (&pool->page_list)) {
  1763.         struct pci_page        *page;
  1764.         page = list_entry (pool->page_list.next,
  1765.                 struct pci_page, page_list);
  1766.         if (is_page_busy (pool->blocks_per_page, page->bitmap)) {
  1767.             printk (KERN_ERR "pci_pool_destroy %s/%s, %p busy\n",
  1768.                 pool->dev ? pool->dev->slot_name : NULL,
  1769.                 pool->name, page->vaddr);
  1770.             /* leak the still-in-use consistent memory */
  1771.             list_del (&page->page_list);
  1772.             kfree (page);
  1773.         } else
  1774.             pool_free_page (pool, page);
  1775.     }
  1776.     spin_unlock_irqrestore (&pool->lock, flags);
  1777.     kfree (pool);
  1778. }
  1779.  
  1780.  
  1781. /**
  1782.  * pci_pool_alloc - get a block of consistent memory
  1783.  * @pool: pci pool that will produce the block
  1784.  * @mem_flags: SLAB_KERNEL or SLAB_ATOMIC
  1785.  * @handle: pointer to dma address of block
  1786.  *
  1787.  * This returns the kernel virtual address of a currently unused block,
  1788.  * and reports its dma address through the handle.
  1789.  * If such a memory block can't be allocated, null is returned.
  1790.  */
  1791. void *
  1792. pci_pool_alloc (struct pci_pool *pool, int mem_flags, dma_addr_t *handle)
  1793. {
  1794.     unsigned long        flags;
  1795.     struct list_head    *entry;
  1796.     struct pci_page        *page;
  1797.     int            map, block;
  1798.     size_t            offset;
  1799.     void            *retval;
  1800.  
  1801. restart:
  1802.     spin_lock_irqsave (&pool->lock, flags);
  1803.     list_for_each (entry, &pool->page_list) {
  1804.         int        i;
  1805.         page = list_entry (entry, struct pci_page, page_list);
  1806.         /* only cachable accesses here ... */
  1807.         for (map = 0, i = 0;
  1808.                 i < pool->blocks_per_page;
  1809.                 i += BITS_PER_LONG, map++) {
  1810.             if (page->bitmap [map] == 0)
  1811.                 continue;
  1812.             block = ffz (~ page->bitmap [map]);
  1813.             if ((i + block) < pool->blocks_per_page) {
  1814.                 clear_bit (block, &page->bitmap [map]);
  1815.                 offset = (BITS_PER_LONG * map) + block;
  1816.                 offset *= pool->size;
  1817.                 goto ready;
  1818.             }
  1819.         }
  1820.     }
  1821.     if (!(page = pool_alloc_page (pool, mem_flags))) {
  1822.         if (mem_flags == SLAB_KERNEL) {
  1823.             DECLARE_WAITQUEUE (wait, current);
  1824.  
  1825.             current->state = TASK_INTERRUPTIBLE;
  1826.             add_wait_queue (&pool->waitq, &wait);
  1827.             spin_unlock_irqrestore (&pool->lock, flags);
  1828.  
  1829.             schedule_timeout (POOL_TIMEOUT_JIFFIES);
  1830.  
  1831.             current->state = TASK_RUNNING;
  1832.             remove_wait_queue (&pool->waitq, &wait);
  1833.             goto restart;
  1834.         }
  1835.         retval = 0;
  1836.         goto done;
  1837.     }
  1838.  
  1839.     clear_bit (0, &page->bitmap [0]);
  1840.     offset = 0;
  1841. ready:
  1842.     retval = offset + page->vaddr;
  1843.     *handle = offset + page->dma;
  1844. done:
  1845.     spin_unlock_irqrestore (&pool->lock, flags);
  1846.     return retval;
  1847. }
  1848.  
  1849.  
  1850. static struct pci_page *
  1851. pool_find_page (struct pci_pool *pool, dma_addr_t dma)
  1852. {
  1853.     unsigned long        flags;
  1854.     struct list_head    *entry;
  1855.     struct pci_page        *page;
  1856.  
  1857.     spin_lock_irqsave (&pool->lock, flags);
  1858.     list_for_each (entry, &pool->page_list) {
  1859.         page = list_entry (entry, struct pci_page, page_list);
  1860.         if (dma < page->dma)
  1861.             continue;
  1862.         if (dma < (page->dma + pool->allocation))
  1863.             goto done;
  1864.     }
  1865.     page = 0;
  1866. done:
  1867.     spin_unlock_irqrestore (&pool->lock, flags);
  1868.     return page;
  1869. }
  1870.  
  1871.  
  1872. /**
  1873.  * pci_pool_free - put block back into pci pool
  1874.  * @pool: the pci pool holding the block
  1875.  * @vaddr: virtual address of block
  1876.  * @dma: dma address of block
  1877.  *
  1878.  * Caller promises neither device nor driver will again touch this block
  1879.  * unless it is first re-allocated.
  1880.  */
  1881. void
  1882. pci_pool_free (struct pci_pool *pool, void *vaddr, dma_addr_t dma)
  1883. {
  1884.     struct pci_page        *page;
  1885.     unsigned long        flags;
  1886.     int            map, block;
  1887.  
  1888.     if ((page = pool_find_page (pool, dma)) == 0) {
  1889.         printk (KERN_ERR "pci_pool_free %s/%s, %p/%x (bad dma)\n",
  1890.             pool->dev ? pool->dev->slot_name : NULL,
  1891.             pool->name, vaddr, (int) (dma & 0xffffffff));
  1892.         return;
  1893.     }
  1894. #ifdef    CONFIG_PCIPOOL_DEBUG
  1895.     if (((dma - page->dma) + (void *)page->vaddr) != vaddr) {
  1896.         printk (KERN_ERR "pci_pool_free %s/%s, %p (bad vaddr)/%x\n",
  1897.             pool->dev ? pool->dev->slot_name : NULL,
  1898.             pool->name, vaddr, (int) (dma & 0xffffffff));
  1899.         return;
  1900.     }
  1901. #endif
  1902.  
  1903.     block = dma - page->dma;
  1904.     block /= pool->size;
  1905.     map = block / BITS_PER_LONG;
  1906.     block %= BITS_PER_LONG;
  1907.  
  1908. #ifdef    CONFIG_PCIPOOL_DEBUG
  1909.     if (page->bitmap [map] & (1UL << block)) {
  1910.         printk (KERN_ERR "pci_pool_free %s/%s, dma %x already free\n",
  1911.             pool->dev ? pool->dev->slot_name : NULL,
  1912.             pool->name, dma);
  1913.         return;
  1914.     }
  1915. #endif
  1916.     if (pool->flags & SLAB_POISON)
  1917.         memset (vaddr, POOL_POISON_BYTE, pool->size);
  1918.  
  1919.     spin_lock_irqsave (&pool->lock, flags);
  1920.     set_bit (block, &page->bitmap [map]);
  1921.     if (waitqueue_active (&pool->waitq))
  1922.         wake_up (&pool->waitq);
  1923.     /*
  1924.      * Resist a temptation to do
  1925.      *    if (!is_page_busy(bpp, page->bitmap)) pool_free_page(pool, page);
  1926.      * it is not interrupt safe. Better have empty pages hang around.
  1927.      */
  1928.     spin_unlock_irqrestore (&pool->lock, flags);
  1929. }
  1930.  
  1931.  
  1932. void __devinit  pci_init(void)
  1933. {
  1934.     struct pci_dev *dev;
  1935.  
  1936.     pcibios_init();
  1937.  
  1938.     pci_for_each_dev(dev) {
  1939.         pci_fixup_device(PCI_FIXUP_FINAL, dev);
  1940.     }
  1941.  
  1942. #ifdef CONFIG_PM
  1943.     pm_register(PM_PCI_DEV, 0, pci_pm_callback);
  1944. #endif
  1945. }
  1946.  
  1947. static int __devinit  pci_setup(char *str)
  1948. {
  1949.     while (str) {
  1950.         char *k = strchr(str, ',');
  1951.         if (k)
  1952.             *k++ = 0;
  1953.         if (*str && (str = pcibios_setup(str)) && *str) {
  1954.             /* PCI layer options should be handled here */
  1955.             printk(KERN_ERR "PCI: Unknown option `%s'\n", str);
  1956.         }
  1957.         str = k;
  1958.     }
  1959.     return 1;
  1960. }
  1961.  
  1962. __setup("pci=", pci_setup);
  1963.  
  1964. EXPORT_SYMBOL(pci_read_config_byte);
  1965. EXPORT_SYMBOL(pci_read_config_word);
  1966. EXPORT_SYMBOL(pci_read_config_dword);
  1967. EXPORT_SYMBOL(pci_write_config_byte);
  1968. EXPORT_SYMBOL(pci_write_config_word);
  1969. EXPORT_SYMBOL(pci_write_config_dword);
  1970. EXPORT_SYMBOL(pci_devices);
  1971. EXPORT_SYMBOL(pci_root_buses);
  1972. EXPORT_SYMBOL(pci_enable_device);
  1973. EXPORT_SYMBOL(pci_disable_device);
  1974. EXPORT_SYMBOL(pci_find_capability);
  1975. EXPORT_SYMBOL(pci_release_regions);
  1976. EXPORT_SYMBOL(pci_request_regions);
  1977. EXPORT_SYMBOL(pci_find_class);
  1978. EXPORT_SYMBOL(pci_find_device);
  1979. EXPORT_SYMBOL(pci_find_slot);
  1980. EXPORT_SYMBOL(pci_find_subsys);
  1981. EXPORT_SYMBOL(pci_set_master);
  1982. EXPORT_SYMBOL(pci_set_dma_mask);
  1983. EXPORT_SYMBOL(pci_dac_set_dma_mask);
  1984. EXPORT_SYMBOL(pci_assign_resource);
  1985. EXPORT_SYMBOL(pci_register_driver);
  1986. EXPORT_SYMBOL(pci_unregister_driver);
  1987. EXPORT_SYMBOL(pci_dev_driver);
  1988. EXPORT_SYMBOL(pci_match_device);
  1989. EXPORT_SYMBOL(pci_find_parent_resource);
  1990.  
  1991. #ifdef CONFIG_HOTPLUG
  1992. EXPORT_SYMBOL(pci_setup_device);
  1993. EXPORT_SYMBOL(pci_insert_device);
  1994. EXPORT_SYMBOL(pci_remove_device);
  1995. EXPORT_SYMBOL(pci_announce_device_to_drivers);
  1996. EXPORT_SYMBOL(pci_add_new_bus);
  1997. EXPORT_SYMBOL(pci_do_scan_bus);
  1998. EXPORT_SYMBOL(pci_scan_slot);
  1999. EXPORT_SYMBOL(pci_proc_attach_device);
  2000. EXPORT_SYMBOL(pci_proc_detach_device);
  2001. EXPORT_SYMBOL(pci_proc_attach_bus);
  2002. EXPORT_SYMBOL(pci_proc_detach_bus);
  2003. #endif
  2004.  
  2005. EXPORT_SYMBOL(pci_set_power_state);
  2006. EXPORT_SYMBOL(pci_save_state);
  2007. EXPORT_SYMBOL(pci_restore_state);
  2008. EXPORT_SYMBOL(pci_enable_wake);
  2009.  
  2010. /* Obsolete functions */
  2011.  
  2012. EXPORT_SYMBOL(pcibios_present);
  2013. EXPORT_SYMBOL(pcibios_read_config_byte);
  2014. EXPORT_SYMBOL(pcibios_read_config_word);
  2015. EXPORT_SYMBOL(pcibios_read_config_dword);
  2016. EXPORT_SYMBOL(pcibios_write_config_byte);
  2017. EXPORT_SYMBOL(pcibios_write_config_word);
  2018. EXPORT_SYMBOL(pcibios_write_config_dword);
  2019. EXPORT_SYMBOL(pcibios_find_class);
  2020. EXPORT_SYMBOL(pcibios_find_device);
  2021.  
  2022. /* Quirk info */
  2023.  
  2024. EXPORT_SYMBOL(isa_dma_bridge_buggy);
  2025. EXPORT_SYMBOL(pci_pci_problems);
  2026.  
  2027. /* Pool allocator */
  2028.  
  2029. EXPORT_SYMBOL (pci_pool_create);
  2030. EXPORT_SYMBOL (pci_pool_destroy);
  2031. EXPORT_SYMBOL (pci_pool_alloc);
  2032. EXPORT_SYMBOL (pci_pool_free);
  2033.  
  2034.