linux/drivers/scsi/aic7xxx/aic79xx_osm_pci.c
<<
>>
Prefs
   1/*
   2 * Linux driver attachment glue for PCI based U320 controllers.
   3 *
   4 * Copyright (c) 2000-2001 Adaptec Inc.
   5 * All rights reserved.
   6 *
   7 * Redistribution and use in source and binary forms, with or without
   8 * modification, are permitted provided that the following conditions
   9 * are met:
  10 * 1. Redistributions of source code must retain the above copyright
  11 *    notice, this list of conditions, and the following disclaimer,
  12 *    without modification.
  13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  14 *    substantially similar to the "NO WARRANTY" disclaimer below
  15 *    ("Disclaimer") and any redistribution must be conditioned upon
  16 *    including a substantially similar Disclaimer requirement for further
  17 *    binary redistribution.
  18 * 3. Neither the names of the above-listed copyright holders nor the names
  19 *    of any contributors may be used to endorse or promote products derived
  20 *    from this software without specific prior written permission.
  21 *
  22 * Alternatively, this software may be distributed under the terms of the
  23 * GNU General Public License ("GPL") version 2 as published by the Free
  24 * Software Foundation.
  25 *
  26 * NO WARRANTY
  27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  31 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  35 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  36 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37 * POSSIBILITY OF SUCH DAMAGES.
  38 *
  39 * $Id: //depot/aic7xxx/linux/drivers/scsi/aic7xxx/aic79xx_osm_pci.c#25 $
  40 */
  41
  42#include "aic79xx_osm.h"
  43#include "aic79xx_inline.h"
  44#include "aic79xx_pci.h"
  45
  46static int      ahd_linux_pci_dev_probe(struct pci_dev *pdev,
  47                                        const struct pci_device_id *ent);
  48static int      ahd_linux_pci_reserve_io_regions(struct ahd_softc *ahd,
  49                                                 u_long *base, u_long *base2);
  50static int      ahd_linux_pci_reserve_mem_region(struct ahd_softc *ahd,
  51                                                 u_long *bus_addr,
  52                                                 uint8_t __iomem **maddr);
  53static int      ahd_linux_pci_dev_suspend(struct pci_dev *pdev, pm_message_t mesg);
  54static int      ahd_linux_pci_dev_resume(struct pci_dev *pdev);
  55static void     ahd_linux_pci_dev_remove(struct pci_dev *pdev);
  56
  57/* Define the macro locally since it's different for different class of chips.
  58 */
  59#define ID(x)            \
  60        ID2C(x),         \
  61        ID2C(IDIROC(x))
  62
  63static struct pci_device_id ahd_linux_pci_id_table[] = {
  64        /* aic7901 based controllers */
  65        ID(ID_AHA_29320A),
  66        ID(ID_AHA_29320ALP),
  67        ID(ID_AHA_29320LPE),
  68        /* aic7902 based controllers */
  69        ID(ID_AHA_29320),
  70        ID(ID_AHA_29320B),
  71        ID(ID_AHA_29320LP),
  72        ID(ID_AHA_39320),
  73        ID(ID_AHA_39320_B),
  74        ID(ID_AHA_39320A),
  75        ID(ID_AHA_39320D),
  76        ID(ID_AHA_39320D_HP),
  77        ID(ID_AHA_39320D_B),
  78        ID(ID_AHA_39320D_B_HP),
  79        /* Generic chip probes for devices we don't know exactly. */
  80        ID16(ID_AIC7901 & ID_9005_GENERIC_MASK),
  81        ID(ID_AIC7901A & ID_DEV_VENDOR_MASK),
  82        ID16(ID_AIC7902 & ID_9005_GENERIC_MASK),
  83        { 0 }
  84};
  85
  86MODULE_DEVICE_TABLE(pci, ahd_linux_pci_id_table);
  87
  88static struct pci_driver aic79xx_pci_driver = {
  89        .name           = "aic79xx",
  90        .probe          = ahd_linux_pci_dev_probe,
  91#ifdef CONFIG_PM
  92        .suspend        = ahd_linux_pci_dev_suspend,
  93        .resume         = ahd_linux_pci_dev_resume,
  94#endif
  95        .remove         = ahd_linux_pci_dev_remove,
  96        .id_table       = ahd_linux_pci_id_table
  97};
  98
  99static int
 100ahd_linux_pci_dev_suspend(struct pci_dev *pdev, pm_message_t mesg)
 101{
 102        struct ahd_softc *ahd = pci_get_drvdata(pdev);
 103        int rc;
 104
 105        if ((rc = ahd_suspend(ahd)))
 106                return rc;
 107
 108        ahd_pci_suspend(ahd);
 109
 110        pci_save_state(pdev);
 111        pci_disable_device(pdev);
 112
 113        if (mesg.event == PM_EVENT_SUSPEND)
 114                pci_set_power_state(pdev, PCI_D3hot);
 115
 116        return rc;
 117}
 118
 119static int
 120ahd_linux_pci_dev_resume(struct pci_dev *pdev)
 121{
 122        struct ahd_softc *ahd = pci_get_drvdata(pdev);
 123        int rc;
 124
 125        pci_set_power_state(pdev, PCI_D0);
 126        pci_restore_state(pdev);
 127
 128        if ((rc = pci_enable_device(pdev))) {
 129                dev_printk(KERN_ERR, &pdev->dev,
 130                           "failed to enable device after resume (%d)\n", rc);
 131                return rc;
 132        }
 133
 134        pci_set_master(pdev);
 135
 136        ahd_pci_resume(ahd);
 137
 138        ahd_resume(ahd);
 139
 140        return rc;
 141}
 142
 143static void
 144ahd_linux_pci_dev_remove(struct pci_dev *pdev)
 145{
 146        struct ahd_softc *ahd = pci_get_drvdata(pdev);
 147        u_long s;
 148
 149        if (ahd->platform_data && ahd->platform_data->host)
 150                        scsi_remove_host(ahd->platform_data->host);
 151
 152        ahd_lock(ahd, &s);
 153        ahd_intr_enable(ahd, FALSE);
 154        ahd_unlock(ahd, &s);
 155        ahd_free(ahd);
 156}
 157
 158static void
 159ahd_linux_pci_inherit_flags(struct ahd_softc *ahd)
 160{
 161        struct pci_dev *pdev = ahd->dev_softc, *master_pdev;
 162        unsigned int master_devfn = PCI_DEVFN(PCI_SLOT(pdev->devfn), 0);
 163
 164        master_pdev = pci_get_slot(pdev->bus, master_devfn);
 165        if (master_pdev) {
 166                struct ahd_softc *master = pci_get_drvdata(master_pdev);
 167                if (master) {
 168                        ahd->flags &= ~AHD_BIOS_ENABLED;
 169                        ahd->flags |= master->flags & AHD_BIOS_ENABLED;
 170                } else
 171                        printk(KERN_ERR "aic79xx: no multichannel peer found!\n");
 172                pci_dev_put(master_pdev);
 173        }
 174}
 175
 176static int
 177ahd_linux_pci_dev_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 178{
 179        char             buf[80];
 180        struct           ahd_softc *ahd;
 181        ahd_dev_softc_t  pci;
 182        struct           ahd_pci_identity *entry;
 183        char            *name;
 184        int              error;
 185        struct device   *dev = &pdev->dev;
 186
 187        pci = pdev;
 188        entry = ahd_find_pci_device(pci);
 189        if (entry == NULL)
 190                return (-ENODEV);
 191
 192        /*
 193         * Allocate a softc for this card and
 194         * set it up for attachment by our
 195         * common detect routine.
 196         */
 197        sprintf(buf, "ahd_pci:%d:%d:%d",
 198                ahd_get_pci_bus(pci),
 199                ahd_get_pci_slot(pci),
 200                ahd_get_pci_function(pci));
 201        name = malloc(strlen(buf) + 1, M_DEVBUF, M_NOWAIT);
 202        if (name == NULL)
 203                return (-ENOMEM);
 204        strcpy(name, buf);
 205        ahd = ahd_alloc(NULL, name);
 206        if (ahd == NULL)
 207                return (-ENOMEM);
 208        if (pci_enable_device(pdev)) {
 209                ahd_free(ahd);
 210                return (-ENODEV);
 211        }
 212        pci_set_master(pdev);
 213
 214        if (sizeof(dma_addr_t) > 4) {
 215                const u64 required_mask = dma_get_required_mask(dev);
 216
 217                if (required_mask > DMA_39BIT_MASK &&
 218                    dma_set_mask(dev, DMA_64BIT_MASK) == 0)
 219                        ahd->flags |= AHD_64BIT_ADDRESSING;
 220                else if (required_mask > DMA_32BIT_MASK &&
 221                         dma_set_mask(dev, DMA_39BIT_MASK) == 0)
 222                        ahd->flags |= AHD_39BIT_ADDRESSING;
 223                else
 224                        dma_set_mask(dev, DMA_32BIT_MASK);
 225        } else {
 226                dma_set_mask(dev, DMA_32BIT_MASK);
 227        }
 228        ahd->dev_softc = pci;
 229        error = ahd_pci_config(ahd, entry);
 230        if (error != 0) {
 231                ahd_free(ahd);
 232                return (-error);
 233        }
 234
 235        /*
 236         * Second Function PCI devices need to inherit some
 237         * * settings from function 0.
 238         */
 239        if ((ahd->features & AHD_MULTI_FUNC) && PCI_FUNC(pdev->devfn) != 0)
 240                ahd_linux_pci_inherit_flags(ahd);
 241
 242        pci_set_drvdata(pdev, ahd);
 243
 244        ahd_linux_register_host(ahd, &aic79xx_driver_template);
 245        return (0);
 246}
 247
 248int
 249ahd_linux_pci_init(void)
 250{
 251        return pci_register_driver(&aic79xx_pci_driver);
 252}
 253
 254void
 255ahd_linux_pci_exit(void)
 256{
 257        pci_unregister_driver(&aic79xx_pci_driver);
 258}
 259
 260static int
 261ahd_linux_pci_reserve_io_regions(struct ahd_softc *ahd, u_long *base,
 262                                 u_long *base2)
 263{
 264        *base = pci_resource_start(ahd->dev_softc, 0);
 265        /*
 266         * This is really the 3rd bar and should be at index 2,
 267         * but the Linux PCI code doesn't know how to "count" 64bit
 268         * bars.
 269         */
 270        *base2 = pci_resource_start(ahd->dev_softc, 3);
 271        if (*base == 0 || *base2 == 0)
 272                return (ENOMEM);
 273        if (!request_region(*base, 256, "aic79xx"))
 274                return (ENOMEM);
 275        if (!request_region(*base2, 256, "aic79xx")) {
 276                release_region(*base, 256);
 277                return (ENOMEM);
 278        }
 279        return (0);
 280}
 281
 282static int
 283ahd_linux_pci_reserve_mem_region(struct ahd_softc *ahd,
 284                                 u_long *bus_addr,
 285                                 uint8_t __iomem **maddr)
 286{
 287        u_long  start;
 288        u_long  base_page;
 289        u_long  base_offset;
 290        int     error = 0;
 291
 292        if (aic79xx_allow_memio == 0)
 293                return (ENOMEM);
 294
 295        if ((ahd->bugs & AHD_PCIX_MMAPIO_BUG) != 0)
 296                return (ENOMEM);
 297
 298        start = pci_resource_start(ahd->dev_softc, 1);
 299        base_page = start & PAGE_MASK;
 300        base_offset = start - base_page;
 301        if (start != 0) {
 302                *bus_addr = start;
 303                if (!request_mem_region(start, 0x1000, "aic79xx"))
 304                        error = ENOMEM;
 305                if (!error) {
 306                        *maddr = ioremap_nocache(base_page, base_offset + 512);
 307                        if (*maddr == NULL) {
 308                                error = ENOMEM;
 309                                release_mem_region(start, 0x1000);
 310                        } else
 311                                *maddr += base_offset;
 312                }
 313        } else
 314                error = ENOMEM;
 315        return (error);
 316}
 317
 318int
 319ahd_pci_map_registers(struct ahd_softc *ahd)
 320{
 321        uint32_t command;
 322        u_long   base;
 323        uint8_t __iomem *maddr;
 324        int      error;
 325
 326        /*
 327         * If its allowed, we prefer memory mapped access.
 328         */
 329        command = ahd_pci_read_config(ahd->dev_softc, PCIR_COMMAND, 4);
 330        command &= ~(PCIM_CMD_PORTEN|PCIM_CMD_MEMEN);
 331        base = 0;
 332        maddr = NULL;
 333        error = ahd_linux_pci_reserve_mem_region(ahd, &base, &maddr);
 334        if (error == 0) {
 335                ahd->platform_data->mem_busaddr = base;
 336                ahd->tags[0] = BUS_SPACE_MEMIO;
 337                ahd->bshs[0].maddr = maddr;
 338                ahd->tags[1] = BUS_SPACE_MEMIO;
 339                ahd->bshs[1].maddr = maddr + 0x100;
 340                ahd_pci_write_config(ahd->dev_softc, PCIR_COMMAND,
 341                                     command | PCIM_CMD_MEMEN, 4);
 342
 343                if (ahd_pci_test_register_access(ahd) != 0) {
 344
 345                        printf("aic79xx: PCI Device %d:%d:%d "
 346                               "failed memory mapped test.  Using PIO.\n",
 347                               ahd_get_pci_bus(ahd->dev_softc),
 348                               ahd_get_pci_slot(ahd->dev_softc),
 349                               ahd_get_pci_function(ahd->dev_softc));
 350                        iounmap(maddr);
 351                        release_mem_region(ahd->platform_data->mem_busaddr,
 352                                           0x1000);
 353                        ahd->bshs[0].maddr = NULL;
 354                        maddr = NULL;
 355                } else
 356                        command |= PCIM_CMD_MEMEN;
 357        } else if (bootverbose) {
 358                printf("aic79xx: PCI%d:%d:%d MEM region 0x%lx "
 359                       "unavailable. Cannot memory map device.\n",
 360                       ahd_get_pci_bus(ahd->dev_softc),
 361                       ahd_get_pci_slot(ahd->dev_softc),
 362                       ahd_get_pci_function(ahd->dev_softc),
 363                       base);
 364        }
 365
 366        if (maddr == NULL) {
 367                u_long   base2;
 368
 369                error = ahd_linux_pci_reserve_io_regions(ahd, &base, &base2);
 370                if (error == 0) {
 371                        ahd->tags[0] = BUS_SPACE_PIO;
 372                        ahd->tags[1] = BUS_SPACE_PIO;
 373                        ahd->bshs[0].ioport = base;
 374                        ahd->bshs[1].ioport = base2;
 375                        command |= PCIM_CMD_PORTEN;
 376                } else {
 377                        printf("aic79xx: PCI%d:%d:%d IO regions 0x%lx and 0x%lx"
 378                               "unavailable. Cannot map device.\n",
 379                               ahd_get_pci_bus(ahd->dev_softc),
 380                               ahd_get_pci_slot(ahd->dev_softc),
 381                               ahd_get_pci_function(ahd->dev_softc),
 382                               base, base2);
 383                }
 384        }
 385        ahd_pci_write_config(ahd->dev_softc, PCIR_COMMAND, command, 4);
 386        return (error);
 387}
 388
 389int
 390ahd_pci_map_int(struct ahd_softc *ahd)
 391{
 392        int error;
 393
 394        error = request_irq(ahd->dev_softc->irq, ahd_linux_isr,
 395                            IRQF_SHARED, "aic79xx", ahd);
 396        if (!error)
 397                ahd->platform_data->irq = ahd->dev_softc->irq;
 398        
 399        return (-error);
 400}
 401
 402void
 403ahd_power_state_change(struct ahd_softc *ahd, ahd_power_state new_state)
 404{
 405        pci_set_power_state(ahd->dev_softc, new_state);
 406}
 407