]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | ||
46 | static int ahd_linux_pci_dev_probe(struct pci_dev *pdev, | |
47 | const struct pci_device_id *ent); | |
48 | static int ahd_linux_pci_reserve_io_regions(struct ahd_softc *ahd, | |
49 | u_long *base, u_long *base2); | |
50 | static int ahd_linux_pci_reserve_mem_region(struct ahd_softc *ahd, | |
51 | u_long *bus_addr, | |
52 | uint8_t __iomem **maddr); | |
53 | static void ahd_linux_pci_dev_remove(struct pci_dev *pdev); | |
54 | ||
55 | /* Define the macro locally since it's different for different class of chips. | |
56 | */ | |
57 | #define ID(x) \ | |
58 | ID2C(x), \ | |
59 | ID2C(IDIROC(x)) | |
60 | ||
61 | static struct pci_device_id ahd_linux_pci_id_table[] = { | |
62 | /* aic7901 based controllers */ | |
63 | ID(ID_AHA_29320A), | |
64 | ID(ID_AHA_29320ALP), | |
65 | /* aic7902 based controllers */ | |
66 | ID(ID_AHA_29320), | |
67 | ID(ID_AHA_29320B), | |
68 | ID(ID_AHA_29320LP), | |
69 | ID(ID_AHA_39320), | |
70 | ID(ID_AHA_39320_B), | |
71 | ID(ID_AHA_39320A), | |
72 | ID(ID_AHA_39320D), | |
73 | ID(ID_AHA_39320D_HP), | |
74 | ID(ID_AHA_39320D_B), | |
75 | ID(ID_AHA_39320D_B_HP), | |
76 | /* Generic chip probes for devices we don't know exactly. */ | |
77 | ID16(ID_AIC7901 & ID_9005_GENERIC_MASK), | |
78 | ID(ID_AIC7901A & ID_DEV_VENDOR_MASK), | |
79 | ID16(ID_AIC7902 & ID_9005_GENERIC_MASK), | |
80 | { 0 } | |
81 | }; | |
82 | ||
83 | MODULE_DEVICE_TABLE(pci, ahd_linux_pci_id_table); | |
84 | ||
85 | struct pci_driver aic79xx_pci_driver = { | |
86 | .name = "aic79xx", | |
87 | .probe = ahd_linux_pci_dev_probe, | |
88 | .remove = ahd_linux_pci_dev_remove, | |
89 | .id_table = ahd_linux_pci_id_table | |
90 | }; | |
91 | ||
92 | static void | |
93 | ahd_linux_pci_dev_remove(struct pci_dev *pdev) | |
94 | { | |
95 | struct ahd_softc *ahd; | |
96 | u_long l; | |
97 | ||
98 | /* | |
99 | * We should be able to just perform | |
100 | * the free directly, but check our | |
101 | * list for extra sanity. | |
102 | */ | |
103 | ahd_list_lock(&l); | |
104 | ahd = ahd_find_softc((struct ahd_softc *)pci_get_drvdata(pdev)); | |
105 | if (ahd != NULL) { | |
106 | u_long s; | |
107 | ||
108 | TAILQ_REMOVE(&ahd_tailq, ahd, links); | |
109 | ahd_list_unlock(&l); | |
110 | ahd_lock(ahd, &s); | |
111 | ahd_intr_enable(ahd, FALSE); | |
112 | ahd_unlock(ahd, &s); | |
113 | ahd_free(ahd); | |
114 | } else | |
115 | ahd_list_unlock(&l); | |
116 | } | |
117 | ||
118 | static int | |
119 | ahd_linux_pci_dev_probe(struct pci_dev *pdev, const struct pci_device_id *ent) | |
120 | { | |
121 | char buf[80]; | |
122 | struct ahd_softc *ahd; | |
123 | ahd_dev_softc_t pci; | |
124 | struct ahd_pci_identity *entry; | |
125 | char *name; | |
126 | int error; | |
127 | ||
128 | /* | |
129 | * Some BIOSen report the same device multiple times. | |
130 | */ | |
131 | TAILQ_FOREACH(ahd, &ahd_tailq, links) { | |
132 | struct pci_dev *probed_pdev; | |
133 | ||
134 | probed_pdev = ahd->dev_softc; | |
135 | if (probed_pdev->bus->number == pdev->bus->number | |
136 | && probed_pdev->devfn == pdev->devfn) | |
137 | break; | |
138 | } | |
139 | if (ahd != NULL) { | |
140 | /* Skip duplicate. */ | |
141 | return (-ENODEV); | |
142 | } | |
143 | ||
144 | pci = pdev; | |
145 | entry = ahd_find_pci_device(pci); | |
146 | if (entry == NULL) | |
147 | return (-ENODEV); | |
148 | ||
149 | /* | |
150 | * Allocate a softc for this card and | |
151 | * set it up for attachment by our | |
152 | * common detect routine. | |
153 | */ | |
154 | sprintf(buf, "ahd_pci:%d:%d:%d", | |
155 | ahd_get_pci_bus(pci), | |
156 | ahd_get_pci_slot(pci), | |
157 | ahd_get_pci_function(pci)); | |
158 | name = malloc(strlen(buf) + 1, M_DEVBUF, M_NOWAIT); | |
159 | if (name == NULL) | |
160 | return (-ENOMEM); | |
161 | strcpy(name, buf); | |
162 | ahd = ahd_alloc(NULL, name); | |
163 | if (ahd == NULL) | |
164 | return (-ENOMEM); | |
165 | if (pci_enable_device(pdev)) { | |
166 | ahd_free(ahd); | |
167 | return (-ENODEV); | |
168 | } | |
169 | pci_set_master(pdev); | |
170 | ||
171 | if (sizeof(dma_addr_t) > 4) { | |
172 | uint64_t memsize; | |
173 | const uint64_t mask_39bit = 0x7FFFFFFFFFULL; | |
174 | ||
175 | memsize = ahd_linux_get_memsize(); | |
176 | ||
177 | if (memsize >= 0x8000000000ULL | |
178 | && pci_set_dma_mask(pdev, DMA_64BIT_MASK) == 0) { | |
179 | ahd->flags |= AHD_64BIT_ADDRESSING; | |
1da177e4 LT |
180 | } else if (memsize > 0x80000000 |
181 | && pci_set_dma_mask(pdev, mask_39bit) == 0) { | |
182 | ahd->flags |= AHD_39BIT_ADDRESSING; | |
1da177e4 LT |
183 | } |
184 | } else { | |
185 | pci_set_dma_mask(pdev, DMA_32BIT_MASK); | |
1da177e4 LT |
186 | } |
187 | ahd->dev_softc = pci; | |
188 | error = ahd_pci_config(ahd, entry); | |
189 | if (error != 0) { | |
190 | ahd_free(ahd); | |
191 | return (-error); | |
192 | } | |
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 | return (0); | |
204 | } | |
205 | ||
206 | int | |
207 | ahd_linux_pci_init(void) | |
208 | { | |
209 | return (pci_module_init(&aic79xx_pci_driver)); | |
210 | } | |
211 | ||
212 | void | |
213 | ahd_linux_pci_exit(void) | |
214 | { | |
215 | pci_unregister_driver(&aic79xx_pci_driver); | |
216 | } | |
217 | ||
218 | static int | |
219 | ahd_linux_pci_reserve_io_regions(struct ahd_softc *ahd, u_long *base, | |
220 | u_long *base2) | |
221 | { | |
222 | *base = pci_resource_start(ahd->dev_softc, 0); | |
223 | /* | |
224 | * This is really the 3rd bar and should be at index 2, | |
225 | * but the Linux PCI code doesn't know how to "count" 64bit | |
226 | * bars. | |
227 | */ | |
228 | *base2 = pci_resource_start(ahd->dev_softc, 3); | |
229 | if (*base == 0 || *base2 == 0) | |
230 | return (ENOMEM); | |
231 | if (request_region(*base, 256, "aic79xx") == 0) | |
232 | return (ENOMEM); | |
233 | if (request_region(*base2, 256, "aic79xx") == 0) { | |
234 | release_region(*base2, 256); | |
235 | return (ENOMEM); | |
236 | } | |
237 | return (0); | |
238 | } | |
239 | ||
240 | static int | |
241 | ahd_linux_pci_reserve_mem_region(struct ahd_softc *ahd, | |
242 | u_long *bus_addr, | |
243 | uint8_t __iomem **maddr) | |
244 | { | |
245 | u_long start; | |
246 | u_long base_page; | |
247 | u_long base_offset; | |
248 | int error; | |
249 | ||
250 | if (aic79xx_allow_memio == 0) | |
251 | return (ENOMEM); | |
252 | ||
253 | if ((ahd->bugs & AHD_PCIX_MMAPIO_BUG) != 0) | |
254 | return (ENOMEM); | |
255 | ||
256 | error = 0; | |
257 | start = pci_resource_start(ahd->dev_softc, 1); | |
258 | base_page = start & PAGE_MASK; | |
259 | base_offset = start - base_page; | |
260 | if (start != 0) { | |
261 | *bus_addr = start; | |
262 | if (request_mem_region(start, 0x1000, "aic79xx") == 0) | |
263 | error = ENOMEM; | |
264 | if (error == 0) { | |
265 | *maddr = ioremap_nocache(base_page, base_offset + 256); | |
266 | if (*maddr == NULL) { | |
267 | error = ENOMEM; | |
268 | release_mem_region(start, 0x1000); | |
269 | } else | |
270 | *maddr += base_offset; | |
271 | } | |
272 | } else | |
273 | error = ENOMEM; | |
274 | return (error); | |
275 | } | |
276 | ||
277 | int | |
278 | ahd_pci_map_registers(struct ahd_softc *ahd) | |
279 | { | |
280 | uint32_t command; | |
281 | u_long base; | |
282 | uint8_t __iomem *maddr; | |
283 | int error; | |
284 | ||
285 | /* | |
286 | * If its allowed, we prefer memory mapped access. | |
287 | */ | |
288 | command = ahd_pci_read_config(ahd->dev_softc, PCIR_COMMAND, 4); | |
289 | command &= ~(PCIM_CMD_PORTEN|PCIM_CMD_MEMEN); | |
290 | base = 0; | |
291 | maddr = NULL; | |
292 | error = ahd_linux_pci_reserve_mem_region(ahd, &base, &maddr); | |
293 | if (error == 0) { | |
294 | ahd->platform_data->mem_busaddr = base; | |
295 | ahd->tags[0] = BUS_SPACE_MEMIO; | |
296 | ahd->bshs[0].maddr = maddr; | |
297 | ahd->tags[1] = BUS_SPACE_MEMIO; | |
298 | ahd->bshs[1].maddr = maddr + 0x100; | |
299 | ahd_pci_write_config(ahd->dev_softc, PCIR_COMMAND, | |
300 | command | PCIM_CMD_MEMEN, 4); | |
301 | ||
302 | if (ahd_pci_test_register_access(ahd) != 0) { | |
303 | ||
304 | printf("aic79xx: PCI Device %d:%d:%d " | |
305 | "failed memory mapped test. Using PIO.\n", | |
306 | ahd_get_pci_bus(ahd->dev_softc), | |
307 | ahd_get_pci_slot(ahd->dev_softc), | |
308 | ahd_get_pci_function(ahd->dev_softc)); | |
309 | iounmap(maddr); | |
310 | release_mem_region(ahd->platform_data->mem_busaddr, | |
311 | 0x1000); | |
312 | ahd->bshs[0].maddr = NULL; | |
313 | maddr = NULL; | |
314 | } else | |
315 | command |= PCIM_CMD_MEMEN; | |
316 | } else if (bootverbose) { | |
317 | printf("aic79xx: PCI%d:%d:%d MEM region 0x%lx " | |
318 | "unavailable. Cannot memory map device.\n", | |
319 | ahd_get_pci_bus(ahd->dev_softc), | |
320 | ahd_get_pci_slot(ahd->dev_softc), | |
321 | ahd_get_pci_function(ahd->dev_softc), | |
322 | base); | |
323 | } | |
324 | ||
325 | if (maddr == NULL) { | |
326 | u_long base2; | |
327 | ||
328 | error = ahd_linux_pci_reserve_io_regions(ahd, &base, &base2); | |
329 | if (error == 0) { | |
330 | ahd->tags[0] = BUS_SPACE_PIO; | |
331 | ahd->tags[1] = BUS_SPACE_PIO; | |
332 | ahd->bshs[0].ioport = base; | |
333 | ahd->bshs[1].ioport = base2; | |
334 | command |= PCIM_CMD_PORTEN; | |
335 | } else { | |
336 | printf("aic79xx: PCI%d:%d:%d IO regions 0x%lx and 0x%lx" | |
337 | "unavailable. Cannot map device.\n", | |
338 | ahd_get_pci_bus(ahd->dev_softc), | |
339 | ahd_get_pci_slot(ahd->dev_softc), | |
340 | ahd_get_pci_function(ahd->dev_softc), | |
341 | base, base2); | |
342 | } | |
343 | } | |
344 | ahd_pci_write_config(ahd->dev_softc, PCIR_COMMAND, command, 4); | |
345 | return (error); | |
346 | } | |
347 | ||
348 | int | |
349 | ahd_pci_map_int(struct ahd_softc *ahd) | |
350 | { | |
351 | int error; | |
352 | ||
353 | error = request_irq(ahd->dev_softc->irq, ahd_linux_isr, | |
354 | SA_SHIRQ, "aic79xx", ahd); | |
355 | if (error == 0) | |
356 | ahd->platform_data->irq = ahd->dev_softc->irq; | |
357 | ||
358 | return (-error); | |
359 | } | |
360 | ||
361 | void | |
362 | ahd_power_state_change(struct ahd_softc *ahd, ahd_power_state new_state) | |
363 | { | |
364 | pci_set_power_state(ahd->dev_softc, new_state); | |
365 | } |