linux/drivers/scsi/aic7xxx/aic7xxx_osm_pci.c
<<
>>
Prefs
   1/*
   2 * Linux driver attachment glue for PCI based 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/aic7xxx_osm_pci.c#47 $
  40 */
  41
  42#include "aic7xxx_osm.h"
  43#include "aic7xxx_pci.h"
  44
  45static int      ahc_linux_pci_dev_probe(struct pci_dev *pdev,
  46                                        const struct pci_device_id *ent);
  47static int      ahc_linux_pci_reserve_io_region(struct ahc_softc *ahc,
  48                                                u_long *base);
  49static int      ahc_linux_pci_reserve_mem_region(struct ahc_softc *ahc,
  50                                                 u_long *bus_addr,
  51                                                 uint8_t __iomem **maddr);
  52static int      ahc_linux_pci_dev_suspend(struct pci_dev *pdev, pm_message_t mesg);
  53static int      ahc_linux_pci_dev_resume(struct pci_dev *pdev);
  54static void     ahc_linux_pci_dev_remove(struct pci_dev *pdev);
  55
  56/* Define the macro locally since it's different for different class of chips.
  57*/
  58#define ID(x)   ID_C(x, PCI_CLASS_STORAGE_SCSI)
  59
  60static struct pci_device_id ahc_linux_pci_id_table[] = {
  61        /* aic7850 based controllers */
  62        ID(ID_AHA_2902_04_10_15_20C_30C),
  63        /* aic7860 based controllers */
  64        ID(ID_AHA_2930CU),
  65        ID(ID_AHA_1480A & ID_DEV_VENDOR_MASK),
  66        ID(ID_AHA_2940AU_0 & ID_DEV_VENDOR_MASK),
  67        ID(ID_AHA_2940AU_CN & ID_DEV_VENDOR_MASK),
  68        ID(ID_AHA_2930C_VAR & ID_DEV_VENDOR_MASK),
  69        /* aic7870 based controllers */
  70        ID(ID_AHA_2940),
  71        ID(ID_AHA_3940),
  72        ID(ID_AHA_398X),
  73        ID(ID_AHA_2944),
  74        ID(ID_AHA_3944),
  75        ID(ID_AHA_4944),
  76        /* aic7880 based controllers */
  77        ID(ID_AHA_2940U & ID_DEV_VENDOR_MASK),
  78        ID(ID_AHA_3940U & ID_DEV_VENDOR_MASK),
  79        ID(ID_AHA_2944U & ID_DEV_VENDOR_MASK),
  80        ID(ID_AHA_3944U & ID_DEV_VENDOR_MASK),
  81        ID(ID_AHA_398XU & ID_DEV_VENDOR_MASK),
  82        ID(ID_AHA_4944U & ID_DEV_VENDOR_MASK),
  83        ID(ID_AHA_2930U & ID_DEV_VENDOR_MASK),
  84        ID(ID_AHA_2940U_PRO & ID_DEV_VENDOR_MASK),
  85        ID(ID_AHA_2940U_CN & ID_DEV_VENDOR_MASK),
  86        /* aic7890 based controllers */
  87        ID(ID_AHA_2930U2),
  88        ID(ID_AHA_2940U2B),
  89        ID(ID_AHA_2940U2_OEM),
  90        ID(ID_AHA_2940U2),
  91        ID(ID_AHA_2950U2B),
  92        ID16(ID_AIC7890_ARO & ID_AIC7895_ARO_MASK),
  93        ID(ID_AAA_131U2),
  94        /* aic7890 based controllers */
  95        ID(ID_AHA_29160),
  96        ID(ID_AHA_29160_CPQ),
  97        ID(ID_AHA_29160N),
  98        ID(ID_AHA_29160C),
  99        ID(ID_AHA_29160B),
 100        ID(ID_AHA_19160B),
 101        ID(ID_AIC7892_ARO),
 102        /* aic7892 based controllers */
 103        ID(ID_AHA_2940U_DUAL),
 104        ID(ID_AHA_3940AU),
 105        ID(ID_AHA_3944AU),
 106        ID(ID_AIC7895_ARO),
 107        ID(ID_AHA_3950U2B_0),
 108        ID(ID_AHA_3950U2B_1),
 109        ID(ID_AHA_3950U2D_0),
 110        ID(ID_AHA_3950U2D_1),
 111        ID(ID_AIC7896_ARO),
 112        /* aic7899 based controllers */
 113        ID(ID_AHA_3960D),
 114        ID(ID_AHA_3960D_CPQ),
 115        ID(ID_AIC7899_ARO),
 116        /* Generic chip probes for devices we don't know exactly. */
 117        ID(ID_AIC7850 & ID_DEV_VENDOR_MASK),
 118        ID(ID_AIC7855 & ID_DEV_VENDOR_MASK),
 119        ID(ID_AIC7859 & ID_DEV_VENDOR_MASK),
 120        ID(ID_AIC7860 & ID_DEV_VENDOR_MASK),
 121        ID(ID_AIC7870 & ID_DEV_VENDOR_MASK),
 122        ID(ID_AIC7880 & ID_DEV_VENDOR_MASK),
 123        ID16(ID_AIC7890 & ID_9005_GENERIC_MASK),
 124        ID16(ID_AIC7892 & ID_9005_GENERIC_MASK),
 125        ID(ID_AIC7895 & ID_DEV_VENDOR_MASK),
 126        ID16(ID_AIC7896 & ID_9005_GENERIC_MASK),
 127        ID16(ID_AIC7899 & ID_9005_GENERIC_MASK),
 128        ID(ID_AIC7810 & ID_DEV_VENDOR_MASK),
 129        ID(ID_AIC7815 & ID_DEV_VENDOR_MASK),
 130        { 0 }
 131};
 132
 133MODULE_DEVICE_TABLE(pci, ahc_linux_pci_id_table);
 134
 135static struct pci_driver aic7xxx_pci_driver = {
 136        .name           = "aic7xxx",
 137        .probe          = ahc_linux_pci_dev_probe,
 138#ifdef CONFIG_PM
 139        .suspend        = ahc_linux_pci_dev_suspend,
 140        .resume         = ahc_linux_pci_dev_resume,
 141#endif
 142        .remove         = ahc_linux_pci_dev_remove,
 143        .id_table       = ahc_linux_pci_id_table
 144};
 145
 146static int
 147ahc_linux_pci_dev_suspend(struct pci_dev *pdev, pm_message_t mesg)
 148{
 149        struct ahc_softc *ahc = pci_get_drvdata(pdev);
 150        int rc;
 151
 152        if ((rc = ahc_suspend(ahc)))
 153                return rc;
 154
 155        pci_save_state(pdev);
 156        pci_disable_device(pdev);
 157
 158        if (mesg.event == PM_EVENT_SUSPEND)
 159                pci_set_power_state(pdev, PCI_D3hot);
 160
 161        return rc;
 162}
 163
 164static int
 165ahc_linux_pci_dev_resume(struct pci_dev *pdev)
 166{
 167        struct ahc_softc *ahc = pci_get_drvdata(pdev);
 168        int rc;
 169
 170        pci_set_power_state(pdev, PCI_D0);
 171        pci_restore_state(pdev);
 172
 173        if ((rc = pci_enable_device(pdev))) {
 174                dev_printk(KERN_ERR, &pdev->dev,
 175                           "failed to enable device after resume (%d)\n", rc);
 176                return rc;
 177        }
 178
 179        pci_set_master(pdev);
 180
 181        ahc_pci_resume(ahc);
 182
 183        return (ahc_resume(ahc));
 184}
 185
 186static void
 187ahc_linux_pci_dev_remove(struct pci_dev *pdev)
 188{
 189        struct ahc_softc *ahc = pci_get_drvdata(pdev);
 190        u_long s;
 191
 192        if (ahc->platform_data && ahc->platform_data->host)
 193                        scsi_remove_host(ahc->platform_data->host);
 194
 195        ahc_lock(ahc, &s);
 196        ahc_intr_enable(ahc, FALSE);
 197        ahc_unlock(ahc, &s);
 198        ahc_free(ahc);
 199}
 200
 201static void
 202ahc_linux_pci_inherit_flags(struct ahc_softc *ahc)
 203{
 204        struct pci_dev *pdev = ahc->dev_softc, *master_pdev;
 205        unsigned int master_devfn = PCI_DEVFN(PCI_SLOT(pdev->devfn), 0);
 206
 207        master_pdev = pci_get_slot(pdev->bus, master_devfn);
 208        if (master_pdev) {
 209                struct ahc_softc *master = pci_get_drvdata(master_pdev);
 210                if (master) {
 211                        ahc->flags &= ~AHC_BIOS_ENABLED; 
 212                        ahc->flags |= master->flags & AHC_BIOS_ENABLED;
 213
 214                        ahc->flags &= ~AHC_PRIMARY_CHANNEL; 
 215                        ahc->flags |= master->flags & AHC_PRIMARY_CHANNEL;
 216                } else
 217                        printk(KERN_ERR "aic7xxx: no multichannel peer found!\n");
 218                pci_dev_put(master_pdev);
 219        } 
 220}
 221
 222static int
 223ahc_linux_pci_dev_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 224{
 225        char             buf[80];
 226        const uint64_t   mask_39bit = 0x7FFFFFFFFFULL;
 227        struct           ahc_softc *ahc;
 228        ahc_dev_softc_t  pci;
 229        struct           ahc_pci_identity *entry;
 230        char            *name;
 231        int              error;
 232        struct device   *dev = &pdev->dev;
 233
 234        pci = pdev;
 235        entry = ahc_find_pci_device(pci);
 236        if (entry == NULL)
 237                return (-ENODEV);
 238
 239        /*
 240         * Allocate a softc for this card and
 241         * set it up for attachment by our
 242         * common detect routine.
 243         */
 244        sprintf(buf, "ahc_pci:%d:%d:%d",
 245                ahc_get_pci_bus(pci),
 246                ahc_get_pci_slot(pci),
 247                ahc_get_pci_function(pci));
 248        name = malloc(strlen(buf) + 1, M_DEVBUF, M_NOWAIT);
 249        if (name == NULL)
 250                return (-ENOMEM);
 251        strcpy(name, buf);
 252        ahc = ahc_alloc(NULL, name);
 253        if (ahc == NULL)
 254                return (-ENOMEM);
 255        if (pci_enable_device(pdev)) {
 256                ahc_free(ahc);
 257                return (-ENODEV);
 258        }
 259        pci_set_master(pdev);
 260
 261        if (sizeof(dma_addr_t) > 4
 262            && ahc->features & AHC_LARGE_SCBS
 263            && dma_set_mask(dev, mask_39bit) == 0
 264            && dma_get_required_mask(dev) > DMA_32BIT_MASK) {
 265                ahc->flags |= AHC_39BIT_ADDRESSING;
 266        } else {
 267                if (dma_set_mask(dev, DMA_32BIT_MASK)) {
 268                        ahc_free(ahc);
 269                        printk(KERN_WARNING "aic7xxx: No suitable DMA available.\n");
 270                        return (-ENODEV);
 271                }
 272        }
 273        ahc->dev_softc = pci;
 274        error = ahc_pci_config(ahc, entry);
 275        if (error != 0) {
 276                ahc_free(ahc);
 277                return (-error);
 278        }
 279
 280        /*
 281         * Second Function PCI devices need to inherit some
 282         * settings from function 0.
 283         */
 284        if ((ahc->features & AHC_MULTI_FUNC) && PCI_FUNC(pdev->devfn) != 0)
 285                ahc_linux_pci_inherit_flags(ahc);
 286
 287        pci_set_drvdata(pdev, ahc);
 288        ahc_linux_register_host(ahc, &aic7xxx_driver_template);
 289        return (0);
 290}
 291
 292int
 293ahc_linux_pci_init(void)
 294{
 295        return pci_register_driver(&aic7xxx_pci_driver);
 296}
 297
 298void
 299ahc_linux_pci_exit(void)
 300{
 301        pci_unregister_driver(&aic7xxx_pci_driver);
 302}
 303
 304static int
 305ahc_linux_pci_reserve_io_region(struct ahc_softc *ahc, u_long *base)
 306{
 307        if (aic7xxx_allow_memio == 0)
 308                return (ENOMEM);
 309
 310        *base = pci_resource_start(ahc->dev_softc, 0);
 311        if (*base == 0)
 312                return (ENOMEM);
 313        if (request_region(*base, 256, "aic7xxx") == 0)
 314                return (ENOMEM);
 315        return (0);
 316}
 317
 318static int
 319ahc_linux_pci_reserve_mem_region(struct ahc_softc *ahc,
 320                                 u_long *bus_addr,
 321                                 uint8_t __iomem **maddr)
 322{
 323        u_long  start;
 324        int     error;
 325
 326        error = 0;
 327        start = pci_resource_start(ahc->dev_softc, 1);
 328        if (start != 0) {
 329                *bus_addr = start;
 330                if (request_mem_region(start, 0x1000, "aic7xxx") == 0)
 331                        error = ENOMEM;
 332                if (error == 0) {
 333                        *maddr = ioremap_nocache(start, 256);
 334                        if (*maddr == NULL) {
 335                                error = ENOMEM;
 336                                release_mem_region(start, 0x1000);
 337                        }
 338                }
 339        } else
 340                error = ENOMEM;
 341        return (error);
 342}
 343
 344int
 345ahc_pci_map_registers(struct ahc_softc *ahc)
 346{
 347        uint32_t command;
 348        u_long   base;
 349        uint8_t __iomem *maddr;
 350        int      error;
 351
 352        /*
 353         * If its allowed, we prefer memory mapped access.
 354         */
 355        command = ahc_pci_read_config(ahc->dev_softc, PCIR_COMMAND, 4);
 356        command &= ~(PCIM_CMD_PORTEN|PCIM_CMD_MEMEN);
 357        base = 0;
 358        maddr = NULL;
 359        error = ahc_linux_pci_reserve_mem_region(ahc, &base, &maddr);
 360        if (error == 0) {
 361                ahc->platform_data->mem_busaddr = base;
 362                ahc->tag = BUS_SPACE_MEMIO;
 363                ahc->bsh.maddr = maddr;
 364                ahc_pci_write_config(ahc->dev_softc, PCIR_COMMAND,
 365                                     command | PCIM_CMD_MEMEN, 4);
 366
 367                /*
 368                 * Do a quick test to see if memory mapped
 369                 * I/O is functioning correctly.
 370                 */
 371                if (ahc_pci_test_register_access(ahc) != 0) {
 372
 373                        printf("aic7xxx: PCI Device %d:%d:%d "
 374                               "failed memory mapped test.  Using PIO.\n",
 375                               ahc_get_pci_bus(ahc->dev_softc),
 376                               ahc_get_pci_slot(ahc->dev_softc),
 377                               ahc_get_pci_function(ahc->dev_softc));
 378                        iounmap(maddr);
 379                        release_mem_region(ahc->platform_data->mem_busaddr,
 380                                           0x1000);
 381                        ahc->bsh.maddr = NULL;
 382                        maddr = NULL;
 383                } else
 384                        command |= PCIM_CMD_MEMEN;
 385        } else {
 386                printf("aic7xxx: PCI%d:%d:%d MEM region 0x%lx "
 387                       "unavailable. Cannot memory map device.\n",
 388                       ahc_get_pci_bus(ahc->dev_softc),
 389                       ahc_get_pci_slot(ahc->dev_softc),
 390                       ahc_get_pci_function(ahc->dev_softc),
 391                       base);
 392        }
 393
 394        /*
 395         * We always prefer memory mapped access.
 396         */
 397        if (maddr == NULL) {
 398
 399                error = ahc_linux_pci_reserve_io_region(ahc, &base);
 400                if (error == 0) {
 401                        ahc->tag = BUS_SPACE_PIO;
 402                        ahc->bsh.ioport = base;
 403                        command |= PCIM_CMD_PORTEN;
 404                } else {
 405                        printf("aic7xxx: PCI%d:%d:%d IO region 0x%lx[0..255] "
 406                               "unavailable. Cannot map device.\n",
 407                               ahc_get_pci_bus(ahc->dev_softc),
 408                               ahc_get_pci_slot(ahc->dev_softc),
 409                               ahc_get_pci_function(ahc->dev_softc),
 410                               base);
 411                }
 412        }
 413        ahc_pci_write_config(ahc->dev_softc, PCIR_COMMAND, command, 4);
 414        return (error);
 415}
 416
 417int
 418ahc_pci_map_int(struct ahc_softc *ahc)
 419{
 420        int error;
 421
 422        error = request_irq(ahc->dev_softc->irq, ahc_linux_isr,
 423                            IRQF_SHARED, "aic7xxx", ahc);
 424        if (error == 0)
 425                ahc->platform_data->irq = ahc->dev_softc->irq;
 426        
 427        return (-error);
 428}
 429
 430