blob: cb30d9c1153d5394c08db57f6b64486bfcd1592a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 void ahc_linux_pci_dev_remove(struct pci_dev *pdev);
53
54/* Define the macro locally since it's different for different class of chips.
55*/
56#define ID(x) ID_C(x, PCI_CLASS_STORAGE_SCSI)
57
58static struct pci_device_id ahc_linux_pci_id_table[] = {
59 /* aic7850 based controllers */
60 ID(ID_AHA_2902_04_10_15_20C_30C),
61 /* aic7860 based controllers */
62 ID(ID_AHA_2930CU),
63 ID(ID_AHA_1480A & ID_DEV_VENDOR_MASK),
64 ID(ID_AHA_2940AU_0 & ID_DEV_VENDOR_MASK),
65 ID(ID_AHA_2940AU_CN & ID_DEV_VENDOR_MASK),
66 ID(ID_AHA_2930C_VAR & ID_DEV_VENDOR_MASK),
67 /* aic7870 based controllers */
68 ID(ID_AHA_2940),
69 ID(ID_AHA_3940),
70 ID(ID_AHA_398X),
71 ID(ID_AHA_2944),
72 ID(ID_AHA_3944),
73 ID(ID_AHA_4944),
74 /* aic7880 based controllers */
75 ID(ID_AHA_2940U & ID_DEV_VENDOR_MASK),
76 ID(ID_AHA_3940U & ID_DEV_VENDOR_MASK),
77 ID(ID_AHA_2944U & ID_DEV_VENDOR_MASK),
78 ID(ID_AHA_3944U & ID_DEV_VENDOR_MASK),
79 ID(ID_AHA_398XU & ID_DEV_VENDOR_MASK),
80 ID(ID_AHA_4944U & ID_DEV_VENDOR_MASK),
81 ID(ID_AHA_2930U & ID_DEV_VENDOR_MASK),
82 ID(ID_AHA_2940U_PRO & ID_DEV_VENDOR_MASK),
83 ID(ID_AHA_2940U_CN & ID_DEV_VENDOR_MASK),
84 /* aic7890 based controllers */
85 ID(ID_AHA_2930U2),
86 ID(ID_AHA_2940U2B),
87 ID(ID_AHA_2940U2_OEM),
88 ID(ID_AHA_2940U2),
89 ID(ID_AHA_2950U2B),
90 ID16(ID_AIC7890_ARO & ID_AIC7895_ARO_MASK),
91 ID(ID_AAA_131U2),
92 /* aic7890 based controllers */
93 ID(ID_AHA_29160),
94 ID(ID_AHA_29160_CPQ),
95 ID(ID_AHA_29160N),
96 ID(ID_AHA_29160C),
97 ID(ID_AHA_29160B),
98 ID(ID_AHA_19160B),
99 ID(ID_AIC7892_ARO),
100 /* aic7892 based controllers */
101 ID(ID_AHA_2940U_DUAL),
102 ID(ID_AHA_3940AU),
103 ID(ID_AHA_3944AU),
104 ID(ID_AIC7895_ARO),
105 ID(ID_AHA_3950U2B_0),
106 ID(ID_AHA_3950U2B_1),
107 ID(ID_AHA_3950U2D_0),
108 ID(ID_AHA_3950U2D_1),
109 ID(ID_AIC7896_ARO),
110 /* aic7899 based controllers */
111 ID(ID_AHA_3960D),
112 ID(ID_AHA_3960D_CPQ),
113 ID(ID_AIC7899_ARO),
114 /* Generic chip probes for devices we don't know exactly. */
115 ID(ID_AIC7850 & ID_DEV_VENDOR_MASK),
116 ID(ID_AIC7855 & ID_DEV_VENDOR_MASK),
117 ID(ID_AIC7859 & ID_DEV_VENDOR_MASK),
118 ID(ID_AIC7860 & ID_DEV_VENDOR_MASK),
119 ID(ID_AIC7870 & ID_DEV_VENDOR_MASK),
120 ID(ID_AIC7880 & ID_DEV_VENDOR_MASK),
121 ID16(ID_AIC7890 & ID_9005_GENERIC_MASK),
122 ID16(ID_AIC7892 & ID_9005_GENERIC_MASK),
123 ID(ID_AIC7895 & ID_DEV_VENDOR_MASK),
124 ID16(ID_AIC7896 & ID_9005_GENERIC_MASK),
125 ID16(ID_AIC7899 & ID_9005_GENERIC_MASK),
126 ID(ID_AIC7810 & ID_DEV_VENDOR_MASK),
127 ID(ID_AIC7815 & ID_DEV_VENDOR_MASK),
128 { 0 }
129};
130
131MODULE_DEVICE_TABLE(pci, ahc_linux_pci_id_table);
132
133struct pci_driver aic7xxx_pci_driver = {
134 .name = "aic7xxx",
135 .probe = ahc_linux_pci_dev_probe,
136 .remove = ahc_linux_pci_dev_remove,
137 .id_table = ahc_linux_pci_id_table
138};
139
140static void
141ahc_linux_pci_dev_remove(struct pci_dev *pdev)
142{
Christoph Hellwig3d656922005-06-11 00:14:30 +0200143 struct ahc_softc *ahc = pci_get_drvdata(pdev);
144 u_long s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
James Bottomley97af50f2005-10-02 15:22:35 -0500146 if (ahc->platform_data && ahc->platform_data->host)
147 scsi_remove_host(ahc->platform_data->host);
148
Christoph Hellwig3d656922005-06-11 00:14:30 +0200149 ahc_lock(ahc, &s);
150 ahc_intr_enable(ahc, FALSE);
151 ahc_unlock(ahc, &s);
152 ahc_free(ahc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
James Bottomley3a4f5c62005-08-13 09:42:45 -0500155static void
156ahc_linux_pci_inherit_flags(struct ahc_softc *ahc)
157{
158 struct pci_dev *pdev = ahc->dev_softc, *master_pdev;
159 unsigned int master_devfn = PCI_DEVFN(PCI_SLOT(pdev->devfn), 0);
160
161 master_pdev = pci_get_slot(pdev->bus, master_devfn);
162 if (master_pdev) {
163 struct ahc_softc *master = pci_get_drvdata(master_pdev);
164 if (master) {
165 ahc->flags &= ~AHC_BIOS_ENABLED;
166 ahc->flags |= master->flags & AHC_BIOS_ENABLED;
167
168 ahc->flags &= ~AHC_PRIMARY_CHANNEL;
169 ahc->flags |= master->flags & AHC_PRIMARY_CHANNEL;
170 } else
171 printk(KERN_ERR "aic7xxx: no multichannel peer found!\n");
172 pci_dev_put(master_pdev);
173 }
174}
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176static int
177ahc_linux_pci_dev_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
178{
179 char buf[80];
180 const uint64_t mask_39bit = 0x7FFFFFFFFFULL;
181 struct ahc_softc *ahc;
182 ahc_dev_softc_t pci;
183 struct ahc_pci_identity *entry;
184 char *name;
185 int error;
James Bottomleya89f29f62005-08-30 11:48:07 -0500186 struct device *dev = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 pci = pdev;
189 entry = ahc_find_pci_device(pci);
190 if (entry == NULL)
191 return (-ENODEV);
192
193 /*
194 * Allocate a softc for this card and
195 * set it up for attachment by our
196 * common detect routine.
197 */
198 sprintf(buf, "ahc_pci:%d:%d:%d",
199 ahc_get_pci_bus(pci),
200 ahc_get_pci_slot(pci),
201 ahc_get_pci_function(pci));
202 name = malloc(strlen(buf) + 1, M_DEVBUF, M_NOWAIT);
203 if (name == NULL)
204 return (-ENOMEM);
205 strcpy(name, buf);
206 ahc = ahc_alloc(NULL, name);
207 if (ahc == NULL)
208 return (-ENOMEM);
209 if (pci_enable_device(pdev)) {
210 ahc_free(ahc);
211 return (-ENODEV);
212 }
213 pci_set_master(pdev);
214
215 if (sizeof(dma_addr_t) > 4
James Bottomleya89f29f62005-08-30 11:48:07 -0500216 && ahc->features & AHC_LARGE_SCBS
217 && dma_set_mask(dev, mask_39bit) == 0
218 && dma_get_required_mask(dev) > DMA_32BIT_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 ahc->flags |= AHC_39BIT_ADDRESSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 } else {
James Bottomleya89f29f62005-08-30 11:48:07 -0500221 if (dma_set_mask(dev, DMA_32BIT_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 printk(KERN_WARNING "aic7xxx: No suitable DMA available.\n");
223 return (-ENODEV);
224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
226 ahc->dev_softc = pci;
227 error = ahc_pci_config(ahc, entry);
228 if (error != 0) {
229 ahc_free(ahc);
230 return (-error);
231 }
James Bottomley3a4f5c62005-08-13 09:42:45 -0500232
233 /*
234 * Second Function PCI devices need to inherit some
235 * settings from function 0.
236 */
237 if ((ahc->features & AHC_MULTI_FUNC) && PCI_FUNC(pdev->devfn) != 0)
238 ahc_linux_pci_inherit_flags(ahc);
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 pci_set_drvdata(pdev, ahc);
Christoph Hellwigdfd287f2005-06-28 16:49:44 +0200241 ahc_linux_register_host(ahc, &aic7xxx_driver_template);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return (0);
243}
244
245int
246ahc_linux_pci_init(void)
247{
248 /* Translate error or zero return into zero or one */
249 return pci_module_init(&aic7xxx_pci_driver) ? 0 : 1;
250}
251
252void
253ahc_linux_pci_exit(void)
254{
255 pci_unregister_driver(&aic7xxx_pci_driver);
256}
257
258static int
259ahc_linux_pci_reserve_io_region(struct ahc_softc *ahc, u_long *base)
260{
261 if (aic7xxx_allow_memio == 0)
262 return (ENOMEM);
263
264 *base = pci_resource_start(ahc->dev_softc, 0);
265 if (*base == 0)
266 return (ENOMEM);
267 if (request_region(*base, 256, "aic7xxx") == 0)
268 return (ENOMEM);
269 return (0);
270}
271
272static int
273ahc_linux_pci_reserve_mem_region(struct ahc_softc *ahc,
274 u_long *bus_addr,
275 uint8_t __iomem **maddr)
276{
277 u_long start;
278 int error;
279
280 error = 0;
281 start = pci_resource_start(ahc->dev_softc, 1);
282 if (start != 0) {
283 *bus_addr = start;
284 if (request_mem_region(start, 0x1000, "aic7xxx") == 0)
285 error = ENOMEM;
286 if (error == 0) {
287 *maddr = ioremap_nocache(start, 256);
288 if (*maddr == NULL) {
289 error = ENOMEM;
290 release_mem_region(start, 0x1000);
291 }
292 }
293 } else
294 error = ENOMEM;
295 return (error);
296}
297
298int
299ahc_pci_map_registers(struct ahc_softc *ahc)
300{
301 uint32_t command;
302 u_long base;
303 uint8_t __iomem *maddr;
304 int error;
305
306 /*
307 * If its allowed, we prefer memory mapped access.
308 */
309 command = ahc_pci_read_config(ahc->dev_softc, PCIR_COMMAND, 4);
310 command &= ~(PCIM_CMD_PORTEN|PCIM_CMD_MEMEN);
311 base = 0;
312 maddr = NULL;
313 error = ahc_linux_pci_reserve_mem_region(ahc, &base, &maddr);
314 if (error == 0) {
315 ahc->platform_data->mem_busaddr = base;
316 ahc->tag = BUS_SPACE_MEMIO;
317 ahc->bsh.maddr = maddr;
318 ahc_pci_write_config(ahc->dev_softc, PCIR_COMMAND,
319 command | PCIM_CMD_MEMEN, 4);
320
321 /*
322 * Do a quick test to see if memory mapped
323 * I/O is functioning correctly.
324 */
325 if (ahc_pci_test_register_access(ahc) != 0) {
326
327 printf("aic7xxx: PCI Device %d:%d:%d "
328 "failed memory mapped test. Using PIO.\n",
329 ahc_get_pci_bus(ahc->dev_softc),
330 ahc_get_pci_slot(ahc->dev_softc),
331 ahc_get_pci_function(ahc->dev_softc));
332 iounmap(maddr);
333 release_mem_region(ahc->platform_data->mem_busaddr,
334 0x1000);
335 ahc->bsh.maddr = NULL;
336 maddr = NULL;
337 } else
338 command |= PCIM_CMD_MEMEN;
339 } else {
340 printf("aic7xxx: PCI%d:%d:%d MEM region 0x%lx "
341 "unavailable. Cannot memory map device.\n",
342 ahc_get_pci_bus(ahc->dev_softc),
343 ahc_get_pci_slot(ahc->dev_softc),
344 ahc_get_pci_function(ahc->dev_softc),
345 base);
346 }
347
348 /*
349 * We always prefer memory mapped access.
350 */
351 if (maddr == NULL) {
352
353 error = ahc_linux_pci_reserve_io_region(ahc, &base);
354 if (error == 0) {
355 ahc->tag = BUS_SPACE_PIO;
356 ahc->bsh.ioport = base;
357 command |= PCIM_CMD_PORTEN;
358 } else {
359 printf("aic7xxx: PCI%d:%d:%d IO region 0x%lx[0..255] "
360 "unavailable. Cannot map device.\n",
361 ahc_get_pci_bus(ahc->dev_softc),
362 ahc_get_pci_slot(ahc->dev_softc),
363 ahc_get_pci_function(ahc->dev_softc),
364 base);
365 }
366 }
367 ahc_pci_write_config(ahc->dev_softc, PCIR_COMMAND, command, 4);
368 return (error);
369}
370
371int
372ahc_pci_map_int(struct ahc_softc *ahc)
373{
374 int error;
375
376 error = request_irq(ahc->dev_softc->irq, ahc_linux_isr,
377 SA_SHIRQ, "aic7xxx", ahc);
378 if (error == 0)
379 ahc->platform_data->irq = ahc->dev_softc->irq;
380
381 return (-error);
382}
383