1 /*
2  * Linux driver attachment glue for aic7770 based controllers.
3  *
4  * Copyright (c) 2000-2003 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/aic7770_osm.c#14 $
40  */
41 
42 #include "aic7xxx_osm.h"
43 
44 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
45 #include <linux/device.h>
46 #include <linux/eisa.h>
47 
48 #define EISA_MFCTR_CHAR0(ID) (char)(((ID>>26) & 0x1F) | '@')  /* Bits 26-30 */
49 #define EISA_MFCTR_CHAR1(ID) (char)(((ID>>21) & 0x1F) | '@')  /* Bits 21-25 */
50 #define EISA_MFCTR_CHAR2(ID) (char)(((ID>>16) & 0x1F) | '@')  /* Bits 16-20 */
51 #define EISA_PRODUCT_ID(ID)  (short)((ID>>4)  & 0xFFF)        /* Bits  4-15 */
52 #define EISA_REVISION_ID(ID) (uint8_t)(ID & 0x0F)             /* Bits  0-3  */
53 
54 static int aic7770_eisa_dev_probe(struct device *dev);
55 static int aic7770_eisa_dev_remove(struct device *dev);
56 static struct eisa_driver aic7770_driver = {
57 	.driver = {
58 		.name   = "aic7xxx",
59 		.probe  = aic7770_eisa_dev_probe,
60 		.remove = aic7770_eisa_dev_remove,
61 	}
62 };
63 
64 typedef  struct device *aic7770_dev_t;
65 #else
66 #define MINSLOT			1
67 #define NUMSLOTS		16
68 #define IDOFFSET		0x80
69 
70 typedef void *aic7770_dev_t;
71 #endif
72 
73 static int aic7770_linux_config(struct aic7770_identity *entry,
74 				aic7770_dev_t dev, u_int eisaBase);
75 
76 void
ahc_linux_eisa_init(void)77 ahc_linux_eisa_init(void)
78 {
79 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
80 	struct eisa_device_id *eid;
81 	struct aic7770_identity *id;
82 	int i;
83 
84 	if (aic7xxx_probe_eisa_vl == 0)
85 		return;
86 
87 	/*
88 	 * Linux requires the EISA IDs to be specified in
89 	 * the EISA ID string format.  Perform the conversion
90 	 * and setup a table with a NUL terminal entry.
91 	 */
92 	aic7770_driver.id_table = malloc(sizeof(struct eisa_device_id) *
93 					 (ahc_num_aic7770_devs + 1),
94 					 M_DEVBUF, M_NOWAIT);
95 	if (aic7770_driver.id_table == NULL)
96 		return;
97 
98 	for (eid = (struct eisa_device_id *)aic7770_driver.id_table,
99 	     id = aic7770_ident_table, i = 0;
100 	     i < ahc_num_aic7770_devs; eid++, id++, i++) {
101 
102 		sprintf(eid->sig, "%c%c%c%03X%01X",
103 			EISA_MFCTR_CHAR0(id->full_id),
104 			EISA_MFCTR_CHAR1(id->full_id),
105 			EISA_MFCTR_CHAR2(id->full_id),
106 			EISA_PRODUCT_ID(id->full_id),
107 			EISA_REVISION_ID(id->full_id));
108 		eid->driver_data = i;
109 	}
110 	eid->sig[0] = 0;
111 
112 	eisa_driver_register(&aic7770_driver);
113 #else /* LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0) */
114 	struct aic7770_identity *entry;
115 	u_int  slot;
116 	u_int  eisaBase;
117 	u_int  i;
118 
119 	if (aic7xxx_probe_eisa_vl == 0)
120 		return;
121 
122 	eisaBase = 0x1000 + AHC_EISA_SLOT_OFFSET;
123 	for (slot = 1; slot < NUMSLOTS; eisaBase+=0x1000, slot++) {
124 		uint32_t eisa_id;
125 		size_t	 id_size;
126 
127 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
128 		if (check_region(eisaBase, AHC_EISA_IOSIZE) != 0)
129 			continue;
130 		request_region(eisaBase, AHC_EISA_IOSIZE, "aic7xxx");
131 #else
132 		if (request_region(eisaBase, AHC_EISA_IOSIZE, "aic7xxx") == 0)
133 			continue;
134 #endif
135 
136 		eisa_id = 0;
137 		id_size = sizeof(eisa_id);
138 		for (i = 0; i < 4; i++) {
139 			/* VLcards require priming*/
140 			outb(0x80 + i, eisaBase + IDOFFSET);
141 			eisa_id |= inb(eisaBase + IDOFFSET + i)
142 				   << ((id_size-i-1) * 8);
143 		}
144 		release_region(eisaBase, AHC_EISA_IOSIZE);
145 		if (eisa_id & 0x80000000)
146 			continue;  /* no EISA card in slot */
147 
148 		entry = aic7770_find_device(eisa_id);
149 		if (entry != NULL)
150 			aic7770_linux_config(entry, NULL, eisaBase);
151 	}
152 #endif
153 }
154 
155 void
ahc_linux_eisa_exit(void)156 ahc_linux_eisa_exit(void)
157 {
158 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
159 	if (aic7xxx_probe_eisa_vl == 0)
160 		return;
161 
162 	if (aic7770_driver.id_table != NULL) {
163 		eisa_driver_unregister(&aic7770_driver);
164 		free(aic7770_driver.id_table, M_DEVBUF);
165 	}
166 #endif
167 }
168 
169 static int
aic7770_linux_config(struct aic7770_identity * entry,aic7770_dev_t dev,u_int eisaBase)170 aic7770_linux_config(struct aic7770_identity *entry, aic7770_dev_t dev,
171 		     u_int eisaBase)
172 {
173 	struct	ahc_softc *ahc;
174 	char	buf[80];
175 	char   *name;
176 	int	error;
177 
178 	/*
179 	 * Allocate a softc for this card and
180 	 * set it up for attachment by our
181 	 * common detect routine.
182 	 */
183 	sprintf(buf, "ahc_eisa:%d", eisaBase >> 12);
184 	name = malloc(strlen(buf) + 1, M_DEVBUF, M_NOWAIT);
185 	if (name == NULL)
186 		return (ENOMEM);
187 	strcpy(name, buf);
188 	ahc = ahc_alloc(&aic7xxx_driver_template, name);
189 	if (ahc == NULL) {
190 		free(name, M_DEVBUF);
191 		return (ENOMEM);
192 	}
193 	error = aic7770_config(ahc, entry, eisaBase);
194 	if (error != 0) {
195 		ahc->bsh.ioport = 0;
196 		ahc_free(ahc);
197 		return (error);
198 	}
199 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
200 	dev->driver_data = (void *)ahc;
201 	if (aic7xxx_detect_complete)
202 		error = ahc_linux_register_host(ahc, &aic7xxx_driver_template);
203 #endif
204 	return (error);
205 }
206 
207 int
aic7770_map_registers(struct ahc_softc * ahc,u_int port)208 aic7770_map_registers(struct ahc_softc *ahc, u_int port)
209 {
210 	/*
211 	 * Lock out other contenders for our i/o space.
212 	 */
213 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
214 	if (check_region(port, AHC_EISA_IOSIZE) != 0)
215 		return (ENOMEM);
216 	request_region(port, AHC_EISA_IOSIZE, "aic7xxx");
217 #else
218 	if (request_region(port, AHC_EISA_IOSIZE, "aic7xxx") == 0)
219 		return (ENOMEM);
220 #endif
221 	ahc->tag = BUS_SPACE_PIO;
222 	ahc->bsh.ioport = port;
223 	return (0);
224 }
225 
226 int
aic7770_map_int(struct ahc_softc * ahc,u_int irq)227 aic7770_map_int(struct ahc_softc *ahc, u_int irq)
228 {
229 	int error;
230 	int shared;
231 
232 	shared = 0;
233 	if ((ahc->flags & AHC_EDGE_INTERRUPT) == 0)
234 		shared = SA_SHIRQ;
235 
236 	error = request_irq(irq, ahc_linux_isr, shared, "aic7xxx", ahc);
237 	if (error == 0)
238 		ahc->platform_data->irq = irq;
239 
240 	return (-error);
241 }
242 
243 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
244 static int
aic7770_eisa_dev_probe(struct device * dev)245 aic7770_eisa_dev_probe(struct device *dev)
246 {
247 	struct eisa_device *edev;
248 
249 	edev = to_eisa_device(dev);
250 	return (aic7770_linux_config(aic7770_ident_table + edev->id.driver_data,
251 				    dev, edev->base_addr+AHC_EISA_SLOT_OFFSET));
252 }
253 
254 static int
aic7770_eisa_dev_remove(struct device * dev)255 aic7770_eisa_dev_remove(struct device *dev)
256 {
257 	struct ahc_softc *ahc;
258 	u_long l;
259 
260 	/*
261 	 * We should be able to just perform
262 	 * the free directly, but check our
263 	 * list for extra sanity.
264 	 */
265 	ahc_list_lock(&l);
266 	ahc = ahc_find_softc((struct ahc_softc *)dev->driver_data);
267 	if (ahc != NULL) {
268 		u_long s;
269 
270 		ahc_lock(ahc, &s);
271 		ahc_intr_enable(ahc, FALSE);
272 		ahc_unlock(ahc, &s);
273 		ahc_free(ahc);
274 	}
275 	ahc_list_unlock(&l);
276 
277 	return (0);
278 }
279 #endif
280