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: aic7xxx_osm_pci.c,v 1.1.1.1 2008/10/15 03:26:55 james26_jang Exp $
40 */
41
42#include "aic7xxx_osm.h"
43
44#if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
45struct pci_device_id
46{
47};
48#endif
49
50static int	ahc_linux_pci_dev_probe(struct pci_dev *pdev,
51					const struct pci_device_id *ent);
52static int	ahc_linux_pci_reserve_io_region(struct ahc_softc *ahc,
53						u_long *base);
54#ifdef MMAPIO
55static 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)
60static 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. */
63static 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
75MODULE_DEVICE_TABLE(pci, ahc_linux_pci_id_table);
76
77struct 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
84static void
85ahc_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 *)pdev->driver_data);
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
109static int
110ahc_linux_pci_dev_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
111{
112	char		 buf[80];
113	struct		 ahc_softc *ahc;
114	ahc_dev_softc_t	 pci;
115	struct		 ahc_pci_identity *entry;
116	char		*name;
117	int		 error;
118
119	/*
120	 * Some BIOSen report the same device multiple times.
121	 */
122	TAILQ_FOREACH(ahc, &ahc_tailq, links) {
123		struct pci_dev *probed_pdev;
124
125		probed_pdev = ahc->dev_softc;
126		if (probed_pdev->bus->number == pdev->bus->number
127		 && probed_pdev->devfn == pdev->devfn)
128			break;
129	}
130	if (ahc != NULL) {
131		/* Skip duplicate. */
132		return (-ENODEV);
133	}
134
135	pci = pdev;
136	entry = ahc_find_pci_device(pci);
137	if (entry == NULL)
138		return (-ENODEV);
139
140	/*
141	 * Allocate a softc for this card and
142	 * set it up for attachment by our
143	 * common detect routine.
144	 */
145	sprintf(buf, "ahc_pci:%d:%d:%d",
146		ahc_get_pci_bus(pci),
147		ahc_get_pci_slot(pci),
148		ahc_get_pci_function(pci));
149	name = malloc(strlen(buf) + 1, M_DEVBUF, M_NOWAIT);
150	if (name == NULL)
151		return (-ENOMEM);
152	strcpy(name, buf);
153	ahc = ahc_alloc(NULL, name);
154	if (ahc == NULL)
155		return (-ENOMEM);
156#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
157	if (pci_enable_device(pdev)) {
158		ahc_free(ahc);
159		return (-ENODEV);
160	}
161	pci_set_master(pdev);
162
163	if (sizeof(bus_addr_t) > 4
164#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,3)
165	 && ahc_linux_get_memsize() > 0x80000000
166	 && pci_set_dma_mask(pdev, 0x7FFFFFFFFFULL) == 0) {
167#else
168	 && ahc_linux_get_memsize() > 0x80000000) {
169
170		ahc->dev_softc->dma_mask =
171		    (bus_addr_t)(0x7FFFFFFFFFULL & (bus_addr_t)~0);
172#endif
173		ahc->flags |= AHC_39BIT_ADDRESSING;
174		ahc->platform_data->hw_dma_mask =
175		    (bus_addr_t)(0x7FFFFFFFFFULL & (bus_addr_t)~0);
176	}
177#endif
178	ahc->dev_softc = pci;
179	error = ahc_pci_config(ahc, entry);
180	if (error != 0) {
181		ahc_free(ahc);
182		return (-error);
183	}
184#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
185	pci_set_drvdata(pdev, ahc);
186	if (aic7xxx_detect_complete)
187		ahc_linux_register_host(ahc, aic7xxx_driver_template);
188#endif
189	return (0);
190}
191
192int
193ahc_linux_pci_probe(Scsi_Host_Template *template)
194{
195#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
196	return (pci_module_init(&aic7xxx_pci_driver));
197#else
198	struct pci_dev *pdev;
199	u_int class;
200	int found;
201
202	/* If we don't have a PCI bus, we can't find any adapters. */
203	if (pci_present() == 0)
204		return (0);
205
206	found = 0;
207	pdev = NULL;
208	class = PCI_CLASS_STORAGE_SCSI << 8;
209	while ((pdev = pci_find_class(class, pdev)) != NULL) {
210		ahc_dev_softc_t pci;
211		int error;
212
213		pci = pdev;
214		error = ahc_linux_pci_dev_probe(pdev, /*pci_devid*/NULL);
215		if (error == 0)
216			found++;
217	}
218	return (found);
219#endif
220}
221
222static int
223ahc_linux_pci_reserve_io_region(struct ahc_softc *ahc, u_long *base)
224{
225#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
226	*base = pci_resource_start(ahc->dev_softc, 0);
227#else
228	*base = ahc_pci_read_config(ahc->dev_softc, PCIR_MAPS, 4);
229	*base &= PCI_BASE_ADDRESS_IO_MASK;
230#endif
231	if (*base == 0)
232		return (ENOMEM);
233#if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
234	if (check_region(*base, 256) != 0)
235		return (ENOMEM);
236	else
237		request_region(*base, 256, "aic7xxx");
238#else
239	if (request_region(*base, 256, "aic7xxx") == 0)
240		return (ENOMEM);
241#endif
242	return (0);
243}
244
245#ifdef MMAPIO
246static int
247ahc_linux_pci_reserve_mem_region(struct ahc_softc *ahc,
248				 u_long *bus_addr,
249				 uint8_t **maddr)
250{
251	u_long	start;
252	u_long	base_page;
253	u_long	base_offset;
254	int	error;
255
256	error = 0;
257#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
258	start = pci_resource_start(ahc->dev_softc, 1);
259	base_page = start & PAGE_MASK;
260	base_offset = start - base_page;
261#else
262	start = ahc_pci_read_config(ahc->dev_softc, PCIR_MAPS+4, 4);
263	base_offset = start & PCI_BASE_ADDRESS_MEM_MASK;
264	base_page = base_offset & PAGE_MASK;
265	base_offset -= base_page;
266#endif
267	if (start != 0) {
268		*bus_addr = start;
269#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
270		if (request_mem_region(start, 0x1000, "aic7xxx") == 0)
271			error = ENOMEM;
272#endif
273		if (error == 0) {
274			*maddr = ioremap_nocache(base_page, base_offset + 256);
275			if (*maddr == NULL) {
276				error = ENOMEM;
277#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
278				release_mem_region(start, 0x1000);
279#endif
280			} else
281				*maddr += base_offset;
282		}
283	} else
284		error = ENOMEM;
285	return (error);
286}
287#endif /* MMAPIO */
288
289int
290ahc_pci_map_registers(struct ahc_softc *ahc)
291{
292	uint32_t command;
293	u_long	 base;
294	uint8_t	*maddr;
295	int	 error;
296
297	/*
298	 * If its allowed, we prefer memory mapped access.
299	 */
300	command = ahc_pci_read_config(ahc->dev_softc, PCIR_COMMAND, 4);
301	command &= ~(PCIM_CMD_PORTEN|PCIM_CMD_MEMEN);
302	base = 0;
303	maddr = NULL;
304#ifdef MMAPIO
305	error = ahc_linux_pci_reserve_mem_region(ahc, &base, &maddr);
306	if (error == 0) {
307		ahc->platform_data->mem_busaddr = base;
308		ahc->tag = BUS_SPACE_MEMIO;
309		ahc->bsh.maddr = maddr;
310		ahc_pci_write_config(ahc->dev_softc, PCIR_COMMAND,
311				     command | PCIM_CMD_MEMEN, 4);
312
313		/*
314		 * Do a quick test to see if memory mapped
315		 * I/O is functioning correctly.
316		 */
317		if (ahc_inb(ahc, HCNTRL) == 0xFF) {
318
319			printf("aic7xxx: PCI Device %d:%d:%d "
320			       "failed memory mapped test\n",
321			       ahc_get_pci_bus(ahc->dev_softc),
322			       ahc_get_pci_slot(ahc->dev_softc),
323			       ahc_get_pci_function(ahc->dev_softc));
324			iounmap((void *)((u_long)maddr & PAGE_MASK));
325#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
326			release_mem_region(ahc->platform_data->mem_busaddr,
327					   0x1000);
328#endif
329			ahc->bsh.maddr = NULL;
330			maddr = NULL;
331		} else
332			command |= PCIM_CMD_MEMEN;
333	} else {
334		printf("aic7xxx: PCI%d:%d:%d MEM region 0x%lx "
335		       "unavailable. Cannot memory map device.\n",
336		       ahc_get_pci_bus(ahc->dev_softc),
337		       ahc_get_pci_slot(ahc->dev_softc),
338		       ahc_get_pci_function(ahc->dev_softc),
339		       base);
340	}
341#endif /* MMAPIO */
342
343	/*
344	 * We always prefer memory mapped access.
345	 */
346	if (maddr == NULL) {
347
348		error = ahc_linux_pci_reserve_io_region(ahc, &base);
349		if (error == 0) {
350			ahc->tag = BUS_SPACE_PIO;
351			ahc->bsh.ioport = base;
352			command |= PCIM_CMD_PORTEN;
353		} else {
354			printf("aic7xxx: PCI%d:%d:%d IO region 0x%lx[0..255] "
355			       "unavailable. Cannot map device.\n",
356			       ahc_get_pci_bus(ahc->dev_softc),
357			       ahc_get_pci_slot(ahc->dev_softc),
358			       ahc_get_pci_function(ahc->dev_softc),
359			       base);
360		}
361	}
362	ahc_pci_write_config(ahc->dev_softc, PCIR_COMMAND, command, 4);
363	return (error);
364}
365
366int
367ahc_pci_map_int(struct ahc_softc *ahc)
368{
369	int error;
370
371	error = request_irq(ahc->dev_softc->irq, ahc_linux_isr,
372			    SA_SHIRQ, "aic7xxx", ahc);
373	if (error == 0)
374		ahc->platform_data->irq = ahc->dev_softc->irq;
375
376	return (-error);
377}
378
379void
380ahc_power_state_change(struct ahc_softc *ahc, ahc_power_state new_state)
381{
382#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
383	pci_set_power_state(ahc->dev_softc, new_state);
384#else
385	uint32_t cap;
386	u_int cap_offset;
387
388	/*
389	 * Traverse the capability list looking for
390	 * the power management capability.
391	 */
392	cap = 0;
393	cap_offset = ahc_pci_read_config(ahc->dev_softc,
394					 PCIR_CAP_PTR, /*bytes*/1);
395	while (cap_offset != 0) {
396
397		cap = ahc_pci_read_config(ahc->dev_softc,
398					  cap_offset, /*bytes*/4);
399		if ((cap & 0xFF) == 1
400		 && ((cap >> 16) & 0x3) > 0) {
401			uint32_t pm_control;
402
403			pm_control = ahc_pci_read_config(ahc->dev_softc,
404							 cap_offset + 4,
405							 /*bytes*/4);
406			pm_control &= ~0x3;
407			pm_control |= new_state;
408			ahc_pci_write_config(ahc->dev_softc,
409					     cap_offset + 4,
410					     pm_control, /*bytes*/2);
411			break;
412		}
413		cap_offset = (cap >> 8) & 0xFF;
414	}
415#endif
416}
417