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