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