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#46 $
40 */
41
42 #include "aic7xxx_osm.h"
43
44 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
45 struct pci_device_id
46 {
47 };
48 #endif
49
50 static int ahc_linux_pci_dev_probe(struct pci_dev *pdev,
51 const struct pci_device_id *ent);
52 static int ahc_linux_pci_reserve_io_region(struct ahc_softc *ahc,
53 u_long *base);
54 #ifdef MMAPIO
55 static int ahc_linux_pci_reserve_mem_region(struct ahc_softc *ahc,
56 u_long *bus_addr,
57 uint8_t **maddr);
58 #endif /* MMAPIO */
59 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
60 static void ahc_linux_pci_dev_remove(struct pci_dev *pdev);
61
62 /* We do our own ID filtering. So, grab all SCSI storage class devices. */
63 static struct pci_device_id ahc_linux_pci_id_table[] = {
64 {
65 0x9004, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
66 PCI_CLASS_STORAGE_SCSI << 8, 0xFFFF00, 0
67 },
68 {
69 0x9005, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
70 PCI_CLASS_STORAGE_SCSI << 8, 0xFFFF00, 0
71 },
72 { 0 }
73 };
74
75 MODULE_DEVICE_TABLE(pci, ahc_linux_pci_id_table);
76
77 struct pci_driver aic7xxx_pci_driver = {
78 name: "aic7xxx",
79 probe: ahc_linux_pci_dev_probe,
80 remove: ahc_linux_pci_dev_remove,
81 id_table: ahc_linux_pci_id_table
82 };
83
84 static void
ahc_linux_pci_dev_remove(struct pci_dev * pdev)85 ahc_linux_pci_dev_remove(struct pci_dev *pdev)
86 {
87 struct ahc_softc *ahc;
88 u_long l;
89
90 /*
91 * We should be able to just perform
92 * the free directly, but check our
93 * list for extra sanity.
94 */
95 ahc_list_lock(&l);
96 ahc = ahc_find_softc((struct ahc_softc *)pci_get_drvdata(pdev));
97 if (ahc != NULL) {
98 u_long s;
99
100 ahc_lock(ahc, &s);
101 ahc_intr_enable(ahc, FALSE);
102 ahc_unlock(ahc, &s);
103 ahc_free(ahc);
104 }
105 ahc_list_unlock(&l);
106 }
107 #endif /* !LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0) */
108
109 static int
ahc_linux_pci_dev_probe(struct pci_dev * pdev,const struct pci_device_id * ent)110 ahc_linux_pci_dev_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
111 {
112 char buf[80];
113 bus_addr_t mask_39bit;
114 struct ahc_softc *ahc;
115 ahc_dev_softc_t pci;
116 struct ahc_pci_identity *entry;
117 char *name;
118 int error;
119
120 /*
121 * Some BIOSen report the same device multiple times.
122 */
123 TAILQ_FOREACH(ahc, &ahc_tailq, links) {
124 struct pci_dev *probed_pdev;
125
126 probed_pdev = ahc->dev_softc;
127 if (probed_pdev->bus->number == pdev->bus->number
128 && probed_pdev->devfn == pdev->devfn)
129 break;
130 }
131 if (ahc != NULL) {
132 /* Skip duplicate. */
133 return (-ENODEV);
134 }
135
136 pci = pdev;
137 entry = ahc_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, "ahc_pci:%d:%d:%d",
147 ahc_get_pci_bus(pci),
148 ahc_get_pci_slot(pci),
149 ahc_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 ahc = ahc_alloc(NULL, name);
155 if (ahc == NULL)
156 return (-ENOMEM);
157 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
158 if (pci_enable_device(pdev)) {
159 ahc_free(ahc);
160 return (-ENODEV);
161 }
162 pci_set_master(pdev);
163
164 mask_39bit = (bus_addr_t)0x7FFFFFFFFFULL;
165 if (sizeof(bus_addr_t) > 4
166 && ahc_linux_get_memsize() > 0x80000000
167 && ahc_pci_set_dma_mask(pdev, mask_39bit) == 0) {
168 ahc->flags |= AHC_39BIT_ADDRESSING;
169 ahc->platform_data->hw_dma_mask = mask_39bit;
170 } else {
171 ahc_pci_set_dma_mask(pdev, 0xFFFFFFFF);
172 ahc->platform_data->hw_dma_mask = 0xFFFFFFFF;
173 }
174 #endif
175 ahc->dev_softc = pci;
176 error = ahc_pci_config(ahc, entry);
177 if (error != 0) {
178 ahc_free(ahc);
179 return (-error);
180 }
181 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
182 pci_set_drvdata(pdev, ahc);
183 if (aic7xxx_detect_complete) {
184 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
185 ahc_linux_register_host(ahc, &aic7xxx_driver_template);
186 #else
187 printf("aic7xxx: ignoring PCI device found after "
188 "initialization\n");
189 return (-ENODEV);
190 #endif
191 }
192 #endif
193 return (0);
194 }
195
196 int
ahc_linux_pci_init(void)197 ahc_linux_pci_init(void)
198 {
199 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
200 return (pci_module_init(&aic7xxx_pci_driver));
201 #else
202 struct pci_dev *pdev;
203 u_int class;
204 int found;
205
206 /* If we don't have a PCI bus, we can't find any adapters. */
207 if (pci_present() == 0)
208 return (0);
209
210 found = 0;
211 pdev = NULL;
212 class = PCI_CLASS_STORAGE_SCSI << 8;
213 while ((pdev = pci_find_class(class, pdev)) != NULL) {
214 ahc_dev_softc_t pci;
215 int error;
216
217 pci = pdev;
218 error = ahc_linux_pci_dev_probe(pdev, /*pci_devid*/NULL);
219 if (error == 0)
220 found++;
221 }
222 return (found);
223 #endif
224 }
225
226 void
ahc_linux_pci_exit(void)227 ahc_linux_pci_exit(void)
228 {
229 pci_unregister_driver(&aic7xxx_pci_driver);
230 }
231
232 static int
ahc_linux_pci_reserve_io_region(struct ahc_softc * ahc,u_long * base)233 ahc_linux_pci_reserve_io_region(struct ahc_softc *ahc, u_long *base)
234 {
235 if (aic7xxx_allow_memio == 0)
236 return (ENOMEM);
237
238 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
239 *base = pci_resource_start(ahc->dev_softc, 0);
240 #else
241 *base = ahc_pci_read_config(ahc->dev_softc, PCIR_MAPS, 4);
242 *base &= PCI_BASE_ADDRESS_IO_MASK;
243 #endif
244 if (*base == 0)
245 return (ENOMEM);
246 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
247 if (check_region(*base, 256) != 0)
248 return (ENOMEM);
249 request_region(*base, 256, "aic7xxx");
250 #else
251 if (request_region(*base, 256, "aic7xxx") == 0)
252 return (ENOMEM);
253 #endif
254 return (0);
255 }
256
257 #ifdef MMAPIO
258 static int
ahc_linux_pci_reserve_mem_region(struct ahc_softc * ahc,u_long * bus_addr,uint8_t ** maddr)259 ahc_linux_pci_reserve_mem_region(struct ahc_softc *ahc,
260 u_long *bus_addr,
261 uint8_t **maddr)
262 {
263 u_long start;
264 u_long base_page;
265 u_long base_offset;
266 int error;
267
268 error = 0;
269 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
270 start = pci_resource_start(ahc->dev_softc, 1);
271 base_page = start & PAGE_MASK;
272 base_offset = start - base_page;
273 #else
274 start = ahc_pci_read_config(ahc->dev_softc, PCIR_MAPS+4, 4);
275 base_offset = start & PCI_BASE_ADDRESS_MEM_MASK;
276 base_page = base_offset & PAGE_MASK;
277 base_offset -= base_page;
278 #endif
279 if (start != 0) {
280 *bus_addr = start;
281 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
282 if (request_mem_region(start, 0x1000, "aic7xxx") == 0)
283 error = ENOMEM;
284 #endif
285 if (error == 0) {
286 *maddr = ioremap_nocache(base_page, base_offset + 256);
287 if (*maddr == NULL) {
288 error = ENOMEM;
289 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
290 release_mem_region(start, 0x1000);
291 #endif
292 } else
293 *maddr += base_offset;
294 }
295 } else
296 error = ENOMEM;
297 return (error);
298 }
299 #endif /* MMAPIO */
300
301 int
ahc_pci_map_registers(struct ahc_softc * ahc)302 ahc_pci_map_registers(struct ahc_softc *ahc)
303 {
304 uint32_t command;
305 u_long base;
306 uint8_t *maddr;
307 int error;
308
309 /*
310 * If its allowed, we prefer memory mapped access.
311 */
312 command = ahc_pci_read_config(ahc->dev_softc, PCIR_COMMAND, 4);
313 command &= ~(PCIM_CMD_PORTEN|PCIM_CMD_MEMEN);
314 base = 0;
315 maddr = NULL;
316 #ifdef MMAPIO
317 error = ahc_linux_pci_reserve_mem_region(ahc, &base, &maddr);
318 if (error == 0) {
319 ahc->platform_data->mem_busaddr = base;
320 ahc->tag = BUS_SPACE_MEMIO;
321 ahc->bsh.maddr = maddr;
322 ahc_pci_write_config(ahc->dev_softc, PCIR_COMMAND,
323 command | PCIM_CMD_MEMEN, 4);
324
325 /*
326 * Do a quick test to see if memory mapped
327 * I/O is functioning correctly.
328 */
329 if (ahc_pci_test_register_access(ahc) != 0) {
330
331 printf("aic7xxx: PCI Device %d:%d:%d "
332 "failed memory mapped test. Using PIO.\n",
333 ahc_get_pci_bus(ahc->dev_softc),
334 ahc_get_pci_slot(ahc->dev_softc),
335 ahc_get_pci_function(ahc->dev_softc));
336 iounmap((void *)((u_long)maddr & PAGE_MASK));
337 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
338 release_mem_region(ahc->platform_data->mem_busaddr,
339 0x1000);
340 #endif
341 ahc->bsh.maddr = NULL;
342 maddr = NULL;
343 } else
344 command |= PCIM_CMD_MEMEN;
345 } else {
346 printf("aic7xxx: PCI%d:%d:%d MEM region 0x%lx "
347 "unavailable. Cannot memory map device.\n",
348 ahc_get_pci_bus(ahc->dev_softc),
349 ahc_get_pci_slot(ahc->dev_softc),
350 ahc_get_pci_function(ahc->dev_softc),
351 base);
352 }
353 #endif /* MMAPIO */
354
355 /*
356 * We always prefer memory mapped access.
357 */
358 if (maddr == NULL) {
359
360 error = ahc_linux_pci_reserve_io_region(ahc, &base);
361 if (error == 0) {
362 ahc->tag = BUS_SPACE_PIO;
363 ahc->bsh.ioport = base;
364 command |= PCIM_CMD_PORTEN;
365 } else {
366 printf("aic7xxx: PCI%d:%d:%d IO region 0x%lx[0..255] "
367 "unavailable. Cannot map device.\n",
368 ahc_get_pci_bus(ahc->dev_softc),
369 ahc_get_pci_slot(ahc->dev_softc),
370 ahc_get_pci_function(ahc->dev_softc),
371 base);
372 }
373 }
374 ahc_pci_write_config(ahc->dev_softc, PCIR_COMMAND, command, 4);
375 return (error);
376 }
377
378 int
ahc_pci_map_int(struct ahc_softc * ahc)379 ahc_pci_map_int(struct ahc_softc *ahc)
380 {
381 int error;
382
383 error = request_irq(ahc->dev_softc->irq, ahc_linux_isr,
384 SA_SHIRQ, "aic7xxx", ahc);
385 if (error == 0)
386 ahc->platform_data->irq = ahc->dev_softc->irq;
387
388 return (-error);
389 }
390
391 void
ahc_power_state_change(struct ahc_softc * ahc,ahc_power_state new_state)392 ahc_power_state_change(struct ahc_softc *ahc, ahc_power_state new_state)
393 {
394 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
395 pci_set_power_state(ahc->dev_softc, new_state);
396 #else
397 uint32_t cap;
398 u_int cap_offset;
399
400 /*
401 * Traverse the capability list looking for
402 * the power management capability.
403 */
404 cap = 0;
405 cap_offset = ahc_pci_read_config(ahc->dev_softc,
406 PCIR_CAP_PTR, /*bytes*/1);
407 while (cap_offset != 0) {
408
409 cap = ahc_pci_read_config(ahc->dev_softc,
410 cap_offset, /*bytes*/4);
411 if ((cap & 0xFF) == 1
412 && ((cap >> 16) & 0x3) > 0) {
413 uint32_t pm_control;
414
415 pm_control = ahc_pci_read_config(ahc->dev_softc,
416 cap_offset + 4,
417 /*bytes*/4);
418 pm_control &= ~0x3;
419 pm_control |= new_state;
420 ahc_pci_write_config(ahc->dev_softc,
421 cap_offset + 4,
422 pm_control, /*bytes*/2);
423 break;
424 }
425 cap_offset = (cap >> 8) & 0xFF;
426 }
427 #endif
428 }
429