]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/scsi/aic7xxx/aic79xx_osm_pci.c
[SCSI] fix potential panic with proc on module removal
[mirror_ubuntu-zesty-kernel.git] / drivers / scsi / aic7xxx / aic79xx_osm_pci.c
CommitLineData
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
46static int ahd_linux_pci_dev_probe(struct pci_dev *pdev,
47 const struct pci_device_id *ent);
48static int ahd_linux_pci_reserve_io_regions(struct ahd_softc *ahd,
49 u_long *base, u_long *base2);
50static int ahd_linux_pci_reserve_mem_region(struct ahd_softc *ahd,
51 u_long *bus_addr,
52 uint8_t __iomem **maddr);
53static 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
61static 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
83MODULE_DEVICE_TABLE(pci, ahd_linux_pci_id_table);
84
85struct 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
92static void
93ahd_linux_pci_dev_remove(struct pci_dev *pdev)
94{
85a46523
CH
95 struct ahd_softc *ahd = pci_get_drvdata(pdev);
96 u_long s;
1da177e4 97
85a46523
CH
98 ahd_lock(ahd, &s);
99 ahd_intr_enable(ahd, FALSE);
100 ahd_unlock(ahd, &s);
101 ahd_free(ahd);
102}
103
104static void
105ahd_linux_pci_inherit_flags(struct ahd_softc *ahd)
106{
107 struct pci_dev *pdev = ahd->dev_softc, *master_pdev;
108 unsigned int master_devfn = PCI_DEVFN(PCI_SLOT(pdev->devfn), 0);
109
110 master_pdev = pci_get_slot(pdev->bus, master_devfn);
111 if (master_pdev) {
112 struct ahd_softc *master = pci_get_drvdata(master_pdev);
113 if (master) {
114 ahd->flags &= ~AHD_BIOS_ENABLED;
115 ahd->flags |= master->flags & AHD_BIOS_ENABLED;
116 } else
117 printk(KERN_ERR "aic79xx: no multichannel peer found!\n");
118 pci_dev_put(master_pdev);
119 }
1da177e4
LT
120}
121
122static int
123ahd_linux_pci_dev_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
124{
125 char buf[80];
126 struct ahd_softc *ahd;
127 ahd_dev_softc_t pci;
128 struct ahd_pci_identity *entry;
129 char *name;
130 int error;
131
1da177e4
LT
132 pci = pdev;
133 entry = ahd_find_pci_device(pci);
134 if (entry == NULL)
135 return (-ENODEV);
136
137 /*
138 * Allocate a softc for this card and
139 * set it up for attachment by our
140 * common detect routine.
141 */
142 sprintf(buf, "ahd_pci:%d:%d:%d",
143 ahd_get_pci_bus(pci),
144 ahd_get_pci_slot(pci),
145 ahd_get_pci_function(pci));
146 name = malloc(strlen(buf) + 1, M_DEVBUF, M_NOWAIT);
147 if (name == NULL)
148 return (-ENOMEM);
149 strcpy(name, buf);
150 ahd = ahd_alloc(NULL, name);
151 if (ahd == NULL)
152 return (-ENOMEM);
153 if (pci_enable_device(pdev)) {
154 ahd_free(ahd);
155 return (-ENODEV);
156 }
157 pci_set_master(pdev);
158
159 if (sizeof(dma_addr_t) > 4) {
160 uint64_t memsize;
161 const uint64_t mask_39bit = 0x7FFFFFFFFFULL;
162
163 memsize = ahd_linux_get_memsize();
164
165 if (memsize >= 0x8000000000ULL
166 && pci_set_dma_mask(pdev, DMA_64BIT_MASK) == 0) {
167 ahd->flags |= AHD_64BIT_ADDRESSING;
1da177e4
LT
168 } else if (memsize > 0x80000000
169 && pci_set_dma_mask(pdev, mask_39bit) == 0) {
170 ahd->flags |= AHD_39BIT_ADDRESSING;
1da177e4
LT
171 }
172 } else {
173 pci_set_dma_mask(pdev, DMA_32BIT_MASK);
1da177e4
LT
174 }
175 ahd->dev_softc = pci;
176 error = ahd_pci_config(ahd, entry);
177 if (error != 0) {
178 ahd_free(ahd);
179 return (-error);
180 }
85a46523
CH
181
182 /*
183 * Second Function PCI devices need to inherit some
184 * * settings from function 0.
185 */
186 if ((ahd->features & AHD_MULTI_FUNC) && PCI_FUNC(pdev->devfn) != 0)
187 ahd_linux_pci_inherit_flags(ahd);
188
1da177e4 189 pci_set_drvdata(pdev, ahd);
85a46523
CH
190
191 ahd_linux_register_host(ahd, &aic79xx_driver_template);
1da177e4
LT
192 return (0);
193}
194
195int
196ahd_linux_pci_init(void)
197{
198 return (pci_module_init(&aic79xx_pci_driver));
199}
200
201void
202ahd_linux_pci_exit(void)
203{
204 pci_unregister_driver(&aic79xx_pci_driver);
205}
206
207static int
208ahd_linux_pci_reserve_io_regions(struct ahd_softc *ahd, u_long *base,
209 u_long *base2)
210{
211 *base = pci_resource_start(ahd->dev_softc, 0);
212 /*
213 * This is really the 3rd bar and should be at index 2,
214 * but the Linux PCI code doesn't know how to "count" 64bit
215 * bars.
216 */
217 *base2 = pci_resource_start(ahd->dev_softc, 3);
218 if (*base == 0 || *base2 == 0)
219 return (ENOMEM);
220 if (request_region(*base, 256, "aic79xx") == 0)
221 return (ENOMEM);
222 if (request_region(*base2, 256, "aic79xx") == 0) {
223 release_region(*base2, 256);
224 return (ENOMEM);
225 }
226 return (0);
227}
228
229static int
230ahd_linux_pci_reserve_mem_region(struct ahd_softc *ahd,
231 u_long *bus_addr,
232 uint8_t __iomem **maddr)
233{
234 u_long start;
235 u_long base_page;
236 u_long base_offset;
237 int error;
238
239 if (aic79xx_allow_memio == 0)
240 return (ENOMEM);
241
242 if ((ahd->bugs & AHD_PCIX_MMAPIO_BUG) != 0)
243 return (ENOMEM);
244
245 error = 0;
246 start = pci_resource_start(ahd->dev_softc, 1);
247 base_page = start & PAGE_MASK;
248 base_offset = start - base_page;
249 if (start != 0) {
250 *bus_addr = start;
251 if (request_mem_region(start, 0x1000, "aic79xx") == 0)
252 error = ENOMEM;
253 if (error == 0) {
254 *maddr = ioremap_nocache(base_page, base_offset + 256);
255 if (*maddr == NULL) {
256 error = ENOMEM;
257 release_mem_region(start, 0x1000);
258 } else
259 *maddr += base_offset;
260 }
261 } else
262 error = ENOMEM;
263 return (error);
264}
265
266int
267ahd_pci_map_registers(struct ahd_softc *ahd)
268{
269 uint32_t command;
270 u_long base;
271 uint8_t __iomem *maddr;
272 int error;
273
274 /*
275 * If its allowed, we prefer memory mapped access.
276 */
277 command = ahd_pci_read_config(ahd->dev_softc, PCIR_COMMAND, 4);
278 command &= ~(PCIM_CMD_PORTEN|PCIM_CMD_MEMEN);
279 base = 0;
280 maddr = NULL;
281 error = ahd_linux_pci_reserve_mem_region(ahd, &base, &maddr);
282 if (error == 0) {
283 ahd->platform_data->mem_busaddr = base;
284 ahd->tags[0] = BUS_SPACE_MEMIO;
285 ahd->bshs[0].maddr = maddr;
286 ahd->tags[1] = BUS_SPACE_MEMIO;
287 ahd->bshs[1].maddr = maddr + 0x100;
288 ahd_pci_write_config(ahd->dev_softc, PCIR_COMMAND,
289 command | PCIM_CMD_MEMEN, 4);
290
291 if (ahd_pci_test_register_access(ahd) != 0) {
292
293 printf("aic79xx: PCI Device %d:%d:%d "
294 "failed memory mapped test. Using PIO.\n",
295 ahd_get_pci_bus(ahd->dev_softc),
296 ahd_get_pci_slot(ahd->dev_softc),
297 ahd_get_pci_function(ahd->dev_softc));
298 iounmap(maddr);
299 release_mem_region(ahd->platform_data->mem_busaddr,
300 0x1000);
301 ahd->bshs[0].maddr = NULL;
302 maddr = NULL;
303 } else
304 command |= PCIM_CMD_MEMEN;
305 } else if (bootverbose) {
306 printf("aic79xx: PCI%d:%d:%d MEM region 0x%lx "
307 "unavailable. Cannot memory map device.\n",
308 ahd_get_pci_bus(ahd->dev_softc),
309 ahd_get_pci_slot(ahd->dev_softc),
310 ahd_get_pci_function(ahd->dev_softc),
311 base);
312 }
313
314 if (maddr == NULL) {
315 u_long base2;
316
317 error = ahd_linux_pci_reserve_io_regions(ahd, &base, &base2);
318 if (error == 0) {
319 ahd->tags[0] = BUS_SPACE_PIO;
320 ahd->tags[1] = BUS_SPACE_PIO;
321 ahd->bshs[0].ioport = base;
322 ahd->bshs[1].ioport = base2;
323 command |= PCIM_CMD_PORTEN;
324 } else {
325 printf("aic79xx: PCI%d:%d:%d IO regions 0x%lx and 0x%lx"
326 "unavailable. Cannot map device.\n",
327 ahd_get_pci_bus(ahd->dev_softc),
328 ahd_get_pci_slot(ahd->dev_softc),
329 ahd_get_pci_function(ahd->dev_softc),
330 base, base2);
331 }
332 }
333 ahd_pci_write_config(ahd->dev_softc, PCIR_COMMAND, command, 4);
334 return (error);
335}
336
337int
338ahd_pci_map_int(struct ahd_softc *ahd)
339{
340 int error;
341
342 error = request_irq(ahd->dev_softc->irq, ahd_linux_isr,
343 SA_SHIRQ, "aic79xx", ahd);
344 if (error == 0)
345 ahd->platform_data->irq = ahd->dev_softc->irq;
346
347 return (-error);
348}
349
350void
351ahd_power_state_change(struct ahd_softc *ahd, ahd_power_state new_state)
352{
353 pci_set_power_state(ahd->dev_softc, new_state);
354}