This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / drivers / scsi / aic7xxx / aic79xx_osm_pci.c
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
45 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
46 struct pci_device_id
47 {
48 };
49 #endif
50
51 static int      ahd_linux_pci_dev_probe(struct pci_dev *pdev,
52                                         const struct pci_device_id *ent);
53 static int      ahd_linux_pci_reserve_io_regions(struct ahd_softc *ahd,
54                                                  u_long *base, u_long *base2);
55 static int      ahd_linux_pci_reserve_mem_region(struct ahd_softc *ahd,
56                                                  u_long *bus_addr,
57                                                  uint8_t **maddr);
58 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
59 static void     ahd_linux_pci_dev_remove(struct pci_dev *pdev);
60
61 /* We do our own ID filtering.  So, grab all SCSI storage class devices. */
62 static struct pci_device_id ahd_linux_pci_id_table[] = {
63         {
64                 0x9005, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
65                 PCI_CLASS_STORAGE_SCSI << 8, 0xFFFF00, 0
66         },
67         {
68                 0x9005, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
69                 PCI_CLASS_STORAGE_RAID << 8, 0xFFFF00, 0
70         },
71         { 0 }
72 };
73
74 MODULE_DEVICE_TABLE(pci, ahd_linux_pci_id_table);
75
76 struct pci_driver aic79xx_pci_driver = {
77         .name           = "aic79xx",
78         .probe          = ahd_linux_pci_dev_probe,
79         .remove         = ahd_linux_pci_dev_remove,
80         .id_table       = ahd_linux_pci_id_table
81 };
82
83 static void
84 ahd_linux_pci_dev_remove(struct pci_dev *pdev)
85 {
86         struct ahd_softc *ahd;
87         u_long l;
88
89         /*
90          * We should be able to just perform
91          * the free directly, but check our
92          * list for extra sanity.
93          */
94         ahd_list_lock(&l);
95         ahd = ahd_find_softc((struct ahd_softc *)pci_get_drvdata(pdev));
96         if (ahd != NULL) {
97                 u_long s;
98
99                 TAILQ_REMOVE(&ahd_tailq, ahd, links);
100                 ahd_list_unlock(&l);
101                 ahd_lock(ahd, &s);
102                 ahd_intr_enable(ahd, FALSE);
103                 ahd_unlock(ahd, &s);
104                 ahd_free(ahd);
105         } else
106                 ahd_list_unlock(&l);
107 }
108 #endif /* !LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0) */
109
110 static int
111 ahd_linux_pci_dev_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
112 {
113         char             buf[80];
114         struct           ahd_softc *ahd;
115         ahd_dev_softc_t  pci;
116         struct           ahd_pci_identity *entry;
117         char            *name;
118         int              error;
119
120         /*
121          * Some BIOSen report the same device multiple times.
122          */
123         TAILQ_FOREACH(ahd, &ahd_tailq, links) {
124                 struct pci_dev *probed_pdev;
125
126                 probed_pdev = ahd->dev_softc;
127                 if (probed_pdev->bus->number == pdev->bus->number
128                  && probed_pdev->devfn == pdev->devfn)
129                         break;
130         }
131         if (ahd != NULL) {
132                 /* Skip duplicate. */
133                 return (-ENODEV);
134         }
135
136         pci = pdev;
137         entry = ahd_find_pci_device(pci);
138         if (entry == NULL)
139                 return (-ENODEV);
140
141         /*
142          * Allocate a softc for this card and
143          * set it up for attachment by our
144          * common detect routine.
145          */
146         sprintf(buf, "ahd_pci:%d:%d:%d",
147                 ahd_get_pci_bus(pci),
148                 ahd_get_pci_slot(pci),
149                 ahd_get_pci_function(pci));
150         name = malloc(strlen(buf) + 1, M_DEVBUF, M_NOWAIT);
151         if (name == NULL)
152                 return (-ENOMEM);
153         strcpy(name, buf);
154         ahd = ahd_alloc(NULL, name);
155         if (ahd == NULL)
156                 return (-ENOMEM);
157 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
158         if (pci_enable_device(pdev)) {
159                 ahd_free(ahd);
160                 return (-ENODEV);
161         }
162         pci_set_master(pdev);
163
164         if (sizeof(bus_addr_t) > 4) {
165                 uint64_t   memsize;
166                 bus_addr_t mask_64bit;
167                 bus_addr_t mask_39bit;
168
169                 memsize = ahd_linux_get_memsize();
170                 mask_64bit = (bus_addr_t)0xFFFFFFFFFFFFFFFFULL;
171                 mask_39bit = (bus_addr_t)0x7FFFFFFFFFULL;
172                 if (memsize >= 0x8000000000ULL
173                  && ahd_pci_set_dma_mask(pdev, mask_64bit) == 0) {
174                         ahd->flags |= AHD_64BIT_ADDRESSING;
175                         ahd->platform_data->hw_dma_mask = mask_64bit;
176                 } else if (memsize > 0x80000000
177                         && ahd_pci_set_dma_mask(pdev, mask_39bit) == 0) {
178                         ahd->flags |= AHD_39BIT_ADDRESSING;
179                         ahd->platform_data->hw_dma_mask = mask_39bit;
180                 }
181         } else {
182                 ahd_pci_set_dma_mask(pdev, 0xFFFFFFFF);
183                 ahd->platform_data->hw_dma_mask = 0xFFFFFFFF;
184         }
185 #endif
186         ahd->dev_softc = pci;
187         error = ahd_pci_config(ahd, entry);
188         if (error != 0) {
189                 ahd_free(ahd);
190                 return (-error);
191         }
192 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
193         pci_set_drvdata(pdev, ahd);
194         if (aic79xx_detect_complete) {
195 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
196                 ahd_linux_register_host(ahd, &aic79xx_driver_template);
197 #else
198                 printf("aic79xx: ignoring PCI device found after "
199                        "initialization\n");
200                 return (-ENODEV);
201 #endif
202         }
203 #endif
204         return (0);
205 }
206
207 int
208 ahd_linux_pci_init(void)
209 {
210 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
211         return (pci_module_init(&aic79xx_pci_driver));
212 #else
213         struct pci_dev *pdev;
214         u_int class;
215         int found;
216
217         /* If we don't have a PCI bus, we can't find any adapters. */
218         if (pci_present() == 0)
219                 return (0);
220
221         found = 0;
222         pdev = NULL;
223         class = PCI_CLASS_STORAGE_SCSI << 8;
224         while ((pdev = pci_find_class(class, pdev)) != NULL) {
225                 ahd_dev_softc_t pci;
226                 int error;
227
228                 pci = pdev;
229                 error = ahd_linux_pci_dev_probe(pdev, /*pci_devid*/NULL);
230                 if (error == 0)
231                         found++;
232         }
233         return (found);
234 #endif
235 }
236
237 void
238 ahd_linux_pci_exit(void)
239 {
240         pci_unregister_driver(&aic79xx_pci_driver);
241 }
242
243 static int
244 ahd_linux_pci_reserve_io_regions(struct ahd_softc *ahd, u_long *base,
245                                  u_long *base2)
246 {
247 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
248         *base = pci_resource_start(ahd->dev_softc, 0);
249         /*
250          * This is really the 3rd bar and should be at index 2,
251          * but the Linux PCI code doesn't know how to "count" 64bit
252          * bars.
253          */
254         *base2 = pci_resource_start(ahd->dev_softc, 3);
255 #else
256         *base = ahd_pci_read_config(ahd->dev_softc, AHD_PCI_IOADDR0, 4);
257         *base2 = ahd_pci_read_config(ahd->dev_softc, AHD_PCI_IOADDR1, 4);
258         *base &= PCI_BASE_ADDRESS_IO_MASK;
259         *base2 &= PCI_BASE_ADDRESS_IO_MASK;
260 #endif
261         if (*base == 0 || *base2 == 0)
262                 return (ENOMEM);
263 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
264         if (check_region(*base, 256) != 0
265          || check_region(*base2, 256) != 0)
266                 return (ENOMEM);
267         request_region(*base, 256, "aic79xx");
268         request_region(*base2, 256, "aic79xx");
269 #else
270         if (request_region(*base, 256, "aic79xx") == 0)
271                 return (ENOMEM);
272         if (request_region(*base2, 256, "aic79xx") == 0) {
273                 release_region(*base2, 256);
274                 return (ENOMEM);
275         }
276 #endif
277         return (0);
278 }
279
280 static int
281 ahd_linux_pci_reserve_mem_region(struct ahd_softc *ahd,
282                                  u_long *bus_addr,
283                                  uint8_t **maddr)
284 {
285         u_long  start;
286         u_long  base_page;
287         u_long  base_offset;
288         int     error;
289
290         if (aic79xx_allow_memio == 0)
291                 return (ENOMEM);
292
293         if ((ahd->bugs & AHD_PCIX_MMAPIO_BUG) != 0)
294                 return (ENOMEM);
295
296         error = 0;
297 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
298         start = pci_resource_start(ahd->dev_softc, 1);
299         base_page = start & PAGE_MASK;
300         base_offset = start - base_page;
301 #else
302         start = ahd_pci_read_config(ahd->dev_softc, PCIR_MAPS+4, 4);
303         base_offset = start & PCI_BASE_ADDRESS_MEM_MASK;
304         base_page = base_offset & PAGE_MASK;
305         base_offset -= base_page;
306 #endif
307         if (start != 0) {
308                 *bus_addr = start;
309 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
310                 if (request_mem_region(start, 0x1000, "aic79xx") == 0)
311                         error = ENOMEM;
312 #endif
313                 if (error == 0) {
314                         *maddr = ioremap_nocache(base_page, base_offset + 256);
315                         if (*maddr == NULL) {
316                                 error = ENOMEM;
317 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
318                                 release_mem_region(start, 0x1000);
319 #endif
320                         } else
321                                 *maddr += base_offset;
322                 }
323         } else
324                 error = ENOMEM;
325         return (error);
326 }
327
328 int
329 ahd_pci_map_registers(struct ahd_softc *ahd)
330 {
331         uint32_t command;
332         u_long   base;
333         uint8_t *maddr;
334         int      error;
335
336         /*
337          * If its allowed, we prefer memory mapped access.
338          */
339         command = ahd_pci_read_config(ahd->dev_softc, PCIR_COMMAND, 4);
340         command &= ~(PCIM_CMD_PORTEN|PCIM_CMD_MEMEN);
341         base = 0;
342         maddr = NULL;
343         error = ahd_linux_pci_reserve_mem_region(ahd, &base, &maddr);
344         if (error == 0) {
345                 ahd->platform_data->mem_busaddr = base;
346                 ahd->tags[0] = BUS_SPACE_MEMIO;
347                 ahd->bshs[0].maddr = maddr;
348                 ahd->tags[1] = BUS_SPACE_MEMIO;
349                 ahd->bshs[1].maddr = maddr + 0x100;
350                 ahd_pci_write_config(ahd->dev_softc, PCIR_COMMAND,
351                                      command | PCIM_CMD_MEMEN, 4);
352
353                 if (ahd_pci_test_register_access(ahd) != 0) {
354
355                         printf("aic79xx: PCI Device %d:%d:%d "
356                                "failed memory mapped test.  Using PIO.\n",
357                                ahd_get_pci_bus(ahd->dev_softc),
358                                ahd_get_pci_slot(ahd->dev_softc),
359                                ahd_get_pci_function(ahd->dev_softc));
360                         iounmap((void *)((u_long)maddr & PAGE_MASK));
361 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
362                         release_mem_region(ahd->platform_data->mem_busaddr,
363                                            0x1000);
364 #endif
365                         ahd->bshs[0].maddr = NULL;
366                         maddr = NULL;
367                 } else
368                         command |= PCIM_CMD_MEMEN;
369         } else if (bootverbose) {
370                 printf("aic79xx: PCI%d:%d:%d MEM region 0x%lx "
371                        "unavailable. Cannot memory map device.\n",
372                        ahd_get_pci_bus(ahd->dev_softc),
373                        ahd_get_pci_slot(ahd->dev_softc),
374                        ahd_get_pci_function(ahd->dev_softc),
375                        base);
376         }
377
378         if (maddr == NULL) {
379                 u_long   base2;
380
381                 error = ahd_linux_pci_reserve_io_regions(ahd, &base, &base2);
382                 if (error == 0) {
383                         ahd->tags[0] = BUS_SPACE_PIO;
384                         ahd->tags[1] = BUS_SPACE_PIO;
385                         ahd->bshs[0].ioport = base;
386                         ahd->bshs[1].ioport = base2;
387                         command |= PCIM_CMD_PORTEN;
388                 } else {
389                         printf("aic79xx: PCI%d:%d:%d IO regions 0x%lx and 0x%lx"
390                                "unavailable. Cannot map device.\n",
391                                ahd_get_pci_bus(ahd->dev_softc),
392                                ahd_get_pci_slot(ahd->dev_softc),
393                                ahd_get_pci_function(ahd->dev_softc),
394                                base, base2);
395                 }
396         }
397         ahd_pci_write_config(ahd->dev_softc, PCIR_COMMAND, command, 4);
398         return (error);
399 }
400
401 int
402 ahd_pci_map_int(struct ahd_softc *ahd)
403 {
404         int error;
405
406         error = request_irq(ahd->dev_softc->irq, ahd_linux_isr,
407                             SA_SHIRQ, "aic79xx", ahd);
408         if (error == 0)
409                 ahd->platform_data->irq = ahd->dev_softc->irq;
410         
411         return (-error);
412 }
413
414 void
415 ahd_power_state_change(struct ahd_softc *ahd, ahd_power_state new_state)
416 {
417 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
418         pci_set_power_state(ahd->dev_softc, new_state);
419 #else
420         uint32_t cap;
421         u_int cap_offset;
422
423         /*
424          * Traverse the capability list looking for
425          * the power management capability.
426          */
427         cap = 0;
428         cap_offset = ahd_pci_read_config(ahd->dev_softc,
429                                          PCIR_CAP_PTR, /*bytes*/1);
430         while (cap_offset != 0) {
431
432                 cap = ahd_pci_read_config(ahd->dev_softc,
433                                           cap_offset, /*bytes*/4);
434                 if ((cap & 0xFF) == 1
435                  && ((cap >> 16) & 0x3) > 0) {
436                         uint32_t pm_control;
437
438                         pm_control = ahd_pci_read_config(ahd->dev_softc,
439                                                          cap_offset + 4,
440                                                          /*bytes*/4);
441                         pm_control &= ~0x3;
442                         pm_control |= new_state;
443                         ahd_pci_write_config(ahd->dev_softc,
444                                              cap_offset + 4,
445                                              pm_control, /*bytes*/2);
446                         break;
447                 }
448                 cap_offset = (cap >> 8) & 0xFF;
449         }
450 #endif 
451 }