aic79xx_osm_pci.c 11.4 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
2
 * Linux driver attachment glue for PCI based U320 controllers.
Linus Torvalds's avatar
Linus Torvalds committed
3
 *
Linus Torvalds's avatar
Linus Torvalds committed
4
 * Copyright (c) 2000-2001 Adaptec Inc.
Linus Torvalds's avatar
Linus Torvalds committed
5 6 7 8 9 10 11 12
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions, and the following disclaimer,
 *    without modification.
Linus Torvalds's avatar
Linus Torvalds committed
13 14 15 16 17 18 19 20
 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
 *    substantially similar to the "NO WARRANTY" disclaimer below
 *    ("Disclaimer") and any redistribution must be conditioned upon
 *    including a substantially similar Disclaimer requirement for further
 *    binary redistribution.
 * 3. Neither the names of the above-listed copyright holders nor the names
 *    of any contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
Linus Torvalds's avatar
Linus Torvalds committed
21 22
 *
 * Alternatively, this software may be distributed under the terms of the
Linus Torvalds's avatar
Linus Torvalds committed
23 24
 * GNU General Public License ("GPL") version 2 as published by the Free
 * Software Foundation.
Linus Torvalds's avatar
Linus Torvalds committed
25
 *
Linus Torvalds's avatar
Linus Torvalds committed
26 27 28 29 30 31
 * NO WARRANTY
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Linus Torvalds's avatar
Linus Torvalds committed
32 33
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Linus Torvalds's avatar
Linus Torvalds committed
34 35 36 37
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
Linus Torvalds's avatar
Linus Torvalds committed
38
 *
39
 * $Id: //depot/aic7xxx/linux/drivers/scsi/aic7xxx/aic79xx_osm_pci.c#13 $
Linus Torvalds's avatar
Linus Torvalds committed
40 41
 */

42 43
#include "aic79xx_osm.h"
#include "aic79xx_inline.h"
Linus Torvalds's avatar
Linus Torvalds committed
44 45 46 47 48 49 50

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
struct pci_device_id
{
};
#endif

51
static int	ahd_linux_pci_dev_probe(struct pci_dev *pdev,
Linus Torvalds's avatar
Linus Torvalds committed
52
					const struct pci_device_id *ent);
53 54 55
static int	ahd_linux_pci_reserve_io_regions(struct ahd_softc *ahd,
						 u_long *base, u_long *base2);
static int	ahd_linux_pci_reserve_mem_region(struct ahd_softc *ahd,
Linus Torvalds's avatar
Linus Torvalds committed
56 57
						 u_long *bus_addr,
						 uint8_t **maddr);
Linus Torvalds's avatar
Linus Torvalds committed
58
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
59
static void	ahd_linux_pci_dev_remove(struct pci_dev *pdev);
Linus Torvalds's avatar
Linus Torvalds committed
60

Linus Torvalds's avatar
Linus Torvalds committed
61
/* We do our own ID filtering.  So, grab all SCSI storage class devices. */
62
static struct pci_device_id ahd_linux_pci_id_table[] = {
Linus Torvalds's avatar
Linus Torvalds committed
63 64 65 66 67 68 69
	{
		0x9005, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
		PCI_CLASS_STORAGE_SCSI << 8, 0xFFFF00, 0
	},
	{ 0 }
};

70 71 72 73 74 75 76
MODULE_DEVICE_TABLE(pci, ahd_linux_pci_id_table);

struct pci_driver aic79xx_pci_driver = {
	name:		"aic79xx",
	probe:		ahd_linux_pci_dev_probe,
	remove:		ahd_linux_pci_dev_remove,
	id_table:	ahd_linux_pci_id_table
Linus Torvalds's avatar
Linus Torvalds committed
77 78 79
};

static void
80
ahd_linux_pci_dev_remove(struct pci_dev *pdev)
Linus Torvalds's avatar
Linus Torvalds committed
81
{
82 83
	struct ahd_softc *ahd;
	u_long l;
Linus Torvalds's avatar
Linus Torvalds committed
84 85 86 87 88 89

	/*
	 * We should be able to just perform
	 * the free directly, but check our
	 * list for extra sanity.
	 */
90 91 92 93 94 95 96 97 98
	ahd_list_lock(&l);
	ahd = ahd_find_softc((struct ahd_softc *)pdev->driver_data);
	if (ahd != NULL) {
		u_long s;

		ahd_lock(ahd, &s);
		ahd_intr_enable(ahd, FALSE);
		ahd_unlock(ahd, &s);
		ahd_free(ahd);
Linus Torvalds's avatar
Linus Torvalds committed
99
	}
100
	ahd_list_unlock(&l);
Linus Torvalds's avatar
Linus Torvalds committed
101 102 103 104
}
#endif /* !LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0) */

static int
105
ahd_linux_pci_dev_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Linus Torvalds's avatar
Linus Torvalds committed
106
{
Linus Torvalds's avatar
Linus Torvalds committed
107
	char		 buf[80];
108 109 110
	struct		 ahd_softc *ahd;
	ahd_dev_softc_t	 pci;
	struct		 ahd_pci_identity *entry;
Linus Torvalds's avatar
Linus Torvalds committed
111 112 113 114 115 116
	char		*name;
	int		 error;

	/*
	 * Some BIOSen report the same device multiple times.
	 */
117
	TAILQ_FOREACH(ahd, &ahd_tailq, links) {
Linus Torvalds's avatar
Linus Torvalds committed
118 119
		struct pci_dev *probed_pdev;

120
		probed_pdev = ahd->dev_softc;
Linus Torvalds's avatar
Linus Torvalds committed
121 122 123 124
		if (probed_pdev->bus->number == pdev->bus->number
		 && probed_pdev->devfn == pdev->devfn)
			break;
	}
125
	if (ahd != NULL) {
Linus Torvalds's avatar
Linus Torvalds committed
126 127 128
		/* Skip duplicate. */
		return (-ENODEV);
	}
Linus Torvalds's avatar
Linus Torvalds committed
129 130

	pci = pdev;
131
	entry = ahd_find_pci_device(pci);
Linus Torvalds's avatar
Linus Torvalds committed
132 133 134 135 136 137 138 139
	if (entry == NULL)
		return (-ENODEV);

	/*
	 * Allocate a softc for this card and
	 * set it up for attachment by our
	 * common detect routine.
	 */
140 141 142 143
	sprintf(buf, "ahd_pci:%d:%d:%d",
		ahd_get_pci_bus(pci),
		ahd_get_pci_slot(pci),
		ahd_get_pci_function(pci));
Linus Torvalds's avatar
Linus Torvalds committed
144 145 146 147
	name = malloc(strlen(buf) + 1, M_DEVBUF, M_NOWAIT);
	if (name == NULL)
		return (-ENOMEM);
	strcpy(name, buf);
148 149
	ahd = ahd_alloc(NULL, name);
	if (ahd == NULL)
Linus Torvalds's avatar
Linus Torvalds committed
150 151 152
		return (-ENOMEM);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
	if (pci_enable_device(pdev)) {
153
		ahd_free(ahd);
Linus Torvalds's avatar
Linus Torvalds committed
154 155 156
		return (-ENODEV);
	}
	pci_set_master(pdev);
Linus Torvalds's avatar
Linus Torvalds committed
157

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
	if (sizeof(bus_addr_t) > 4) {
		uint64_t memsize;

		memsize = ahd_linux_get_memsize();
		if (memsize >= 0x8000000000
	 	 && ahd_pci_set_dma_mask(pdev, 0xFFFFFFFFFFFFFFFFULL) == 0) {
			ahd->flags |= AHD_64BIT_ADDRESSING;
			ahd->platform_data->hw_dma_mask =
			    (bus_addr_t)(0xFFFFFFFFFFFFFFFFULL&(bus_addr_t)~0);
		} else if (memsize > 0x80000000
			&& ahd_pci_set_dma_mask(pdev, 0x7FFFFFFFFFULL) == 0) {
			ahd->flags |= AHD_39BIT_ADDRESSING;
			ahd->platform_data->hw_dma_mask =
			    (bus_addr_t)(0x7FFFFFFFFFULL & (bus_addr_t)~0);
		}
Linus Torvalds's avatar
Linus Torvalds committed
173
	}
Linus Torvalds's avatar
Linus Torvalds committed
174
#endif
175 176
	ahd->dev_softc = pci;
	error = ahd_pci_config(ahd, entry);
Linus Torvalds's avatar
Linus Torvalds committed
177
	if (error != 0) {
178
		ahd_free(ahd);
Linus Torvalds's avatar
Linus Torvalds committed
179 180 181
		return (-error);
	}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
182 183 184
	pdev->driver_data = ahd;
	if (aic79xx_detect_complete)
		ahd_linux_register_host(ahd, aic79xx_driver_template);
Linus Torvalds's avatar
Linus Torvalds committed
185 186 187 188 189
#endif
	return (0);
}

int
190
ahd_linux_pci_probe(Scsi_Host_Template *template)
Linus Torvalds's avatar
Linus Torvalds committed
191 192
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
193
	return (pci_module_init(&aic79xx_pci_driver));
Linus Torvalds's avatar
Linus Torvalds committed
194 195 196 197 198 199 200 201 202 203 204 205 206
#else
	struct pci_dev *pdev;
	u_int class;
	int found;

	/* If we don't have a PCI bus, we can't find any adapters. */
	if (pci_present() == 0)
		return (0);

	found = 0;
	pdev = NULL;
	class = PCI_CLASS_STORAGE_SCSI << 8;
	while ((pdev = pci_find_class(class, pdev)) != NULL) {
207
		ahd_dev_softc_t pci;
Linus Torvalds's avatar
Linus Torvalds committed
208 209 210
		int error;

		pci = pdev;
211
		error = ahd_linux_pci_dev_probe(pdev, /*pci_devid*/NULL);
Linus Torvalds's avatar
Linus Torvalds committed
212 213 214 215 216 217 218
		if (error == 0)
			found++;
	}
	return (found);
#endif
}

Linus Torvalds's avatar
Linus Torvalds committed
219
static int
220 221
ahd_linux_pci_reserve_io_regions(struct ahd_softc *ahd, u_long *base,
				 u_long *base2)
Linus Torvalds's avatar
Linus Torvalds committed
222
{
Linus Torvalds's avatar
Linus Torvalds committed
223
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
224 225 226 227 228 229 230
	*base = pci_resource_start(ahd->dev_softc, 0);
	/*
	 * This is really the 3rd bar and should be at index 2,
	 * but the Linux PCI code doesn't know how to "count" 64bit
	 * bars.
	 */
	*base2 = pci_resource_start(ahd->dev_softc, 3);
Linus Torvalds's avatar
Linus Torvalds committed
231
#else
232 233
	*base = ahd_pci_read_config(ahd->dev_softc, AHD_PCI_IOADDR0, 4);
	*base2 = ahd_pci_read_config(ahd->dev_softc, AHD_PCI_IOADDR1, 4);
Linus Torvalds's avatar
Linus Torvalds committed
234
	*base &= PCI_BASE_ADDRESS_IO_MASK;
235
	*base2 &= PCI_BASE_ADDRESS_IO_MASK;
Linus Torvalds's avatar
Linus Torvalds committed
236
#endif
237
	if (*base == 0 || *base2 == 0)
Linus Torvalds's avatar
Linus Torvalds committed
238 239
		return (ENOMEM);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
240 241
	if (check_region(*base, 256) != 0
	 || check_region(*base2, 256) != 0)
Linus Torvalds's avatar
Linus Torvalds committed
242
		return (ENOMEM);
243 244 245 246
	else {
		request_region(*base, 256, "aic79xx");
		request_region(*base2, 256, "aic79xx");
	}
Linus Torvalds's avatar
Linus Torvalds committed
247
#else
248
	if (request_region(*base, 256, "aic79xx") == 0)
Linus Torvalds's avatar
Linus Torvalds committed
249
		return (ENOMEM);
250 251 252 253
	if (request_region(*base2, 256, "aic79xx") == 0) {
		release_region(*base2, 256);
		return (ENOMEM);
	}
Linus Torvalds's avatar
Linus Torvalds committed
254 255 256
#endif
	return (0);
}
Linus Torvalds's avatar
Linus Torvalds committed
257

Linus Torvalds's avatar
Linus Torvalds committed
258
static int
259
ahd_linux_pci_reserve_mem_region(struct ahd_softc *ahd,
Linus Torvalds's avatar
Linus Torvalds committed
260 261 262 263 264 265 266 267
				 u_long *bus_addr,
				 uint8_t **maddr)
{
	u_long	start;
	u_long	base_page;
	u_long	base_offset;
	int	error;

268 269 270 271 272 273 274
	if (aic79xx_allow_memio == 0)
		return (ENOMEM);

	if ((ahd->chip & AHD_BUS_MASK) == AHD_PCIX
	 && (ahd->bugs & AHD_PCIX_MMAPIO_BUG) != 0)
		return (ENOMEM);

Linus Torvalds's avatar
Linus Torvalds committed
275
	error = 0;
Linus Torvalds's avatar
Linus Torvalds committed
276
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
277
	start = pci_resource_start(ahd->dev_softc, 1);
Linus Torvalds's avatar
Linus Torvalds committed
278 279 280
	base_page = start & PAGE_MASK;
	base_offset = start - base_page;
#else
281
	start = ahd_pci_read_config(ahd->dev_softc, PCIR_MAPS+4, 4);
Linus Torvalds's avatar
Linus Torvalds committed
282 283 284 285 286
	base_offset = start & PCI_BASE_ADDRESS_MEM_MASK;
	base_page = base_offset & PAGE_MASK;
	base_offset -= base_page;
#endif
	if (start != 0) {
Linus Torvalds's avatar
Linus Torvalds committed
287
		*bus_addr = start;
Linus Torvalds's avatar
Linus Torvalds committed
288
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
289
		if (request_mem_region(start, 0x1000, "aic79xx") == 0)
Linus Torvalds's avatar
Linus Torvalds committed
290 291 292 293
			error = ENOMEM;
#endif
		if (error == 0) {
			*maddr = ioremap_nocache(base_page, base_offset + 256);
294
			if (*maddr == NULL) {
Linus Torvalds's avatar
Linus Torvalds committed
295
				error = ENOMEM;
296 297 298 299
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
				release_mem_region(start, 0x1000);
#endif
			} else
Linus Torvalds's avatar
Linus Torvalds committed
300 301 302 303 304 305 306 307
				*maddr += base_offset;
		}
	} else
		error = ENOMEM;
	return (error);
}

int
308
ahd_pci_map_registers(struct ahd_softc *ahd)
Linus Torvalds's avatar
Linus Torvalds committed
309 310 311 312 313 314 315
{
	uint32_t command;
	u_long	 base;
	uint8_t	*maddr;
	int	 error;

	/*
316
	 * If its allowed, we prefer memory mapped access.
Linus Torvalds's avatar
Linus Torvalds committed
317
	 */
318
	command = ahd_pci_read_config(ahd->dev_softc, PCIR_COMMAND, 4);
Linus Torvalds's avatar
Linus Torvalds committed
319 320 321 322
	command &= ~(PCIM_CMD_PORTEN|PCIM_CMD_MEMEN);
	base = 0;
	maddr = NULL;
#ifdef MMAPIO
323
	error = ahd_linux_pci_reserve_mem_region(ahd, &base, &maddr);
Linus Torvalds's avatar
Linus Torvalds committed
324
	if (error == 0) {
325 326 327 328 329 330
		ahd->platform_data->mem_busaddr = base;
		ahd->tags[0] = BUS_SPACE_MEMIO;
		ahd->bshs[0].maddr = maddr;
		ahd->tags[1] = BUS_SPACE_MEMIO;
		ahd->bshs[1].maddr = maddr + 0x100;
		ahd_pci_write_config(ahd->dev_softc, PCIR_COMMAND,
Linus Torvalds's avatar
Linus Torvalds committed
331 332 333 334 335 336
				     command | PCIM_CMD_MEMEN, 4);

		/*
		 * Do a quick test to see if memory mapped
		 * I/O is functioning correctly.
		 */
337
		if (ahd_inb(ahd, HCNTRL) == 0xFF) {
Linus Torvalds's avatar
Linus Torvalds committed
338

339
			printf("aic79xx: PCI Device %d:%d:%d "
Linus Torvalds's avatar
Linus Torvalds committed
340
			       "failed memory mapped test\n",
341 342 343 344
			       ahd_get_pci_bus(ahd->dev_softc),
			       ahd_get_pci_slot(ahd->dev_softc),
			       ahd_get_pci_function(ahd->dev_softc));
			ahd->bshs[0].maddr = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
345
			maddr = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
346 347
		} else
			command |= PCIM_CMD_MEMEN;
348 349 350 351 352 353
	} else if (bootverbose) {
		printf("aic79xx: PCI%d:%d:%d MEM region 0x%lx "
		       "unavailable. Cannot memory map device.\n",
		       ahd_get_pci_bus(ahd->dev_softc),
		       ahd_get_pci_slot(ahd->dev_softc),
		       ahd_get_pci_function(ahd->dev_softc),
Linus Torvalds's avatar
Linus Torvalds committed
354
		       base);
Linus Torvalds's avatar
Linus Torvalds committed
355 356 357 358
	}
#endif

	if (maddr == NULL) {
359 360 361 362 363 364 365 366
		u_long	 base2;

		error = ahd_linux_pci_reserve_io_regions(ahd, &base, &base2);
		if (error == 0) {
			ahd->tags[0] = BUS_SPACE_PIO;
			ahd->tags[1] = BUS_SPACE_PIO;
			ahd->bshs[0].ioport = base;
			ahd->bshs[1].ioport = base2;
Linus Torvalds's avatar
Linus Torvalds committed
367
			command |= PCIM_CMD_PORTEN;
368 369 370 371 372 373 374
		} else {
			printf("aic79xx: PCI%d:%d:%d IO regions 0x%lx and 0x%lx"
			       "unavailable. Cannot map device.\n",
			       ahd_get_pci_bus(ahd->dev_softc),
			       ahd_get_pci_slot(ahd->dev_softc),
			       ahd_get_pci_function(ahd->dev_softc),
			       base, base2);
Linus Torvalds's avatar
Linus Torvalds committed
375 376
		}
	}
377
	ahd_pci_write_config(ahd->dev_softc, PCIR_COMMAND, command, 4);
Linus Torvalds's avatar
Linus Torvalds committed
378
	return (error);
Linus Torvalds's avatar
Linus Torvalds committed
379 380 381
}

int
382
ahd_pci_map_int(struct ahd_softc *ahd)
Linus Torvalds's avatar
Linus Torvalds committed
383 384 385
{
	int error;

386 387 388 389
	error = request_irq(ahd->dev_softc->irq, ahd_linux_isr,
			    SA_SHIRQ, "aic79xx", ahd);
	if (error == 0)
		ahd->platform_data->irq = ahd->dev_softc->irq;
Linus Torvalds's avatar
Linus Torvalds committed
390 391 392
	
	return (-error);
}
Linus Torvalds's avatar
Linus Torvalds committed
393 394

void
395
ahd_power_state_change(struct ahd_softc *ahd, ahd_power_state new_state)
Linus Torvalds's avatar
Linus Torvalds committed
396 397
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
398
	pci_set_power_state(ahd->dev_softc, new_state);
Linus Torvalds's avatar
Linus Torvalds committed
399 400 401 402 403 404 405 406 407
#else
	uint32_t cap;
	u_int cap_offset;

	/*
	 * Traverse the capability list looking for
	 * the power management capability.
	 */
	cap = 0;
408
	cap_offset = ahd_pci_read_config(ahd->dev_softc,
Linus Torvalds's avatar
Linus Torvalds committed
409 410 411
					 PCIR_CAP_PTR, /*bytes*/1);
	while (cap_offset != 0) {

412
		cap = ahd_pci_read_config(ahd->dev_softc,
Linus Torvalds's avatar
Linus Torvalds committed
413 414 415 416 417
					  cap_offset, /*bytes*/4);
		if ((cap & 0xFF) == 1
		 && ((cap >> 16) & 0x3) > 0) {
			uint32_t pm_control;

418
			pm_control = ahd_pci_read_config(ahd->dev_softc,
Linus Torvalds's avatar
Linus Torvalds committed
419 420 421 422
							 cap_offset + 4,
							 /*bytes*/4);
			pm_control &= ~0x3;
			pm_control |= new_state;
423
			ahd_pci_write_config(ahd->dev_softc,
Linus Torvalds's avatar
Linus Torvalds committed
424 425 426 427 428 429 430 431
					     cap_offset + 4,
					     pm_control, /*bytes*/2);
			break;
		}
		cap_offset = (cap >> 8) & 0xFF;
	}
#endif 
}