mpt_pci.c revision 157662
1/*-
2 * PCI specific probe and attach routines for LSI Fusion Adapters
3 * FreeBSD Version.
4 *
5 * Copyright (c) 2000, 2001 by Greg Ansley
6 * Partially derived from Matt Jacob's ISP driver.
7 * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002 by Matthew Jacob
8 * Feral Software
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice immediately at the beginning of the file, without modification,
16 *    this list of conditions, and the following disclaimer.
17 * 2. The name of the author may not be used to endorse or promote products
18 *    derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
24 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32/*-
33 * Copyright (c) 2002, 2006 by Matthew Jacob
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions are
38 * met:
39 * 1. Redistributions of source code must retain the above copyright
40 *    notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
42 *    substantially similar to the "NO WARRANTY" disclaimer below
43 *    ("Disclaimer") and any redistribution must be conditioned upon including
44 *    a substantially similar Disclaimer requirement for further binary
45 *    redistribution.
46 * 3. Neither the names of the above listed copyright holders nor the names
47 *    of any contributors may be used to endorse or promote products derived
48 *    from this software without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
51 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
54 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
55 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
56 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
57 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
58 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
59 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
60 * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 *
62 * Support from Chris Ellsworth in order to make SAS adapters work
63 * is gratefully acknowledged.
64 */
65/*
66 * Copyright (c) 2004, Avid Technology, Inc. and its contributors.
67 * Copyright (c) 2005, WHEEL Sp. z o.o.
68 * Copyright (c) 2004, 2005 Justin T. Gibbs
69 * All rights reserved.
70 *
71 * Redistribution and use in source and binary forms, with or without
72 * modification, are permitted provided that the following conditions are
73 * met:
74 * 1. Redistributions of source code must retain the above copyright
75 *    notice, this list of conditions and the following disclaimer.
76 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
77 *    substantially similar to the "NO WARRANTY" disclaimer below
78 *    ("Disclaimer") and any redistribution must be conditioned upon including
79 *    a substantially similar Disclaimer requirement for further binary
80 *    redistribution.
81 * 3. Neither the names of the above listed copyright holders nor the names
82 *    of any contributors may be used to endorse or promote products derived
83 *    from this software without specific prior written permission.
84 *
85 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
86 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
87 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
88 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
89 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
90 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
91 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
92 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
93 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
94 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
95 * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
96 */
97
98#include <sys/cdefs.h>
99__FBSDID("$FreeBSD: head/sys/dev/mpt/mpt_pci.c 157662 2006-04-11 16:47:30Z mjacob $");
100
101#include <dev/mpt/mpt.h>
102#include <dev/mpt/mpt_cam.h>
103#include <dev/mpt/mpt_raid.h>
104
105
106#ifndef	PCI_VENDOR_LSI
107#define	PCI_VENDOR_LSI			0x1000
108#endif
109
110#ifndef	PCI_PRODUCT_LSI_FC909
111#define	PCI_PRODUCT_LSI_FC909		0x0620
112#endif
113
114#ifndef	PCI_PRODUCT_LSI_FC909A
115#define	PCI_PRODUCT_LSI_FC909A		0x0621
116#endif
117
118#ifndef	PCI_PRODUCT_LSI_FC919
119#define	PCI_PRODUCT_LSI_FC919		0x0624
120#endif
121
122#ifndef	PCI_PRODUCT_LSI_FC929
123#define	PCI_PRODUCT_LSI_FC929		0x0622
124#endif
125
126#ifndef	PCI_PRODUCT_LSI_FC929X
127#define	PCI_PRODUCT_LSI_FC929X		0x0626
128#endif
129
130#ifndef	PCI_PRODUCT_LSI_1030
131#define	PCI_PRODUCT_LSI_1030		0x0030
132#endif
133
134#ifndef	PCI_PRODUCT_LSI_SAS1064
135#define PCI_PRODUCT_LSI_SAS1064		0x0050
136#endif
137
138#ifndef PCI_PRODUCT_LSI_SAS1064A
139#define PCI_PRODUCT_LSI_SAS1064A	0x005C
140#endif
141
142#ifndef PCI_PRODUCT_LSI_SAS1064E
143#define PCI_PRODUCT_LSI_SAS1064E	0x0056
144#endif
145
146#ifndef PCI_PRODUCT_LSI_SAS1066
147#define PCI_PRODUCT_LSI_SAS1066		0x005E
148#endif
149
150#ifndef PCI_PRODUCT_LSI_SAS1066E
151#define PCI_PRODUCT_LSI_SAS1066E	0x005A
152#endif
153
154#ifndef PCI_PRODUCT_LSI_SAS1068
155#define PCI_PRODUCT_LSI_SAS1068		0x0054
156#endif
157
158#ifndef PCI_PRODUCT_LSI_SAS1068E
159#define PCI_PRODUCT_LSI_SAS1068E	0x0058
160#endif
161
162#ifndef PCI_PRODUCT_LSI_SAS1078
163#define PCI_PRODUCT_LSI_SAS1078		0x0060
164#endif
165
166#ifndef	PCIM_CMD_SERRESPEN
167#define	PCIM_CMD_SERRESPEN	0x0100
168#endif
169
170
171#define	MPT_IO_BAR	0
172#define	MPT_MEM_BAR	1
173
174static int mpt_pci_probe(device_t);
175static int mpt_pci_attach(device_t);
176static void mpt_free_bus_resources(struct mpt_softc *mpt);
177static int mpt_pci_detach(device_t);
178static int mpt_pci_shutdown(device_t);
179static int mpt_dma_mem_alloc(struct mpt_softc *mpt);
180static void mpt_dma_mem_free(struct mpt_softc *mpt);
181static void mpt_read_config_regs(struct mpt_softc *mpt);
182static void mpt_pci_intr(void *);
183
184static device_method_t mpt_methods[] = {
185	/* Device interface */
186	DEVMETHOD(device_probe,		mpt_pci_probe),
187	DEVMETHOD(device_attach,	mpt_pci_attach),
188	DEVMETHOD(device_detach,	mpt_pci_detach),
189	DEVMETHOD(device_shutdown,	mpt_pci_shutdown),
190	{ 0, 0 }
191};
192
193static driver_t mpt_driver = {
194	"mpt", mpt_methods, sizeof(struct mpt_softc)
195};
196static devclass_t mpt_devclass;
197DRIVER_MODULE(mpt, pci, mpt_driver, mpt_devclass, 0, 0);
198MODULE_VERSION(mpt, 1);
199
200static int
201mpt_pci_probe(device_t dev)
202{
203	char *desc;
204
205	if (pci_get_vendor(dev) != PCI_VENDOR_LSI)
206		return (ENXIO);
207
208	switch ((pci_get_device(dev) & ~1)) {
209	case PCI_PRODUCT_LSI_FC909:
210		desc = "LSILogic FC909 FC Adapter";
211		break;
212	case PCI_PRODUCT_LSI_FC909A:
213		desc = "LSILogic FC909A FC Adapter";
214		break;
215	case PCI_PRODUCT_LSI_FC919:
216		desc = "LSILogic FC919 FC Adapter";
217		break;
218	case PCI_PRODUCT_LSI_FC929:
219		desc = "LSILogic FC929 FC Adapter";
220		break;
221	case PCI_PRODUCT_LSI_FC929X:
222		desc = "LSILogic FC929X FC Adapter";
223		break;
224	case PCI_PRODUCT_LSI_1030:
225		desc = "LSILogic 1030 Ultra4 Adapter";
226		break;
227	case PCI_PRODUCT_LSI_SAS1064:
228	case PCI_PRODUCT_LSI_SAS1064A:
229	case PCI_PRODUCT_LSI_SAS1064E:
230	case PCI_PRODUCT_LSI_SAS1066:
231	case PCI_PRODUCT_LSI_SAS1066E:
232	case PCI_PRODUCT_LSI_SAS1068:
233	case PCI_PRODUCT_LSI_SAS1068E:
234	case PCI_PRODUCT_LSI_SAS1078:
235		desc = "LSILogic SAS Adapter";
236		break;
237	default:
238		return (ENXIO);
239	}
240
241	device_set_desc(dev, desc);
242	return (0);
243}
244
245#if	__FreeBSD_version < 500000
246static void
247mpt_set_options(struct mpt_softc *mpt)
248{
249	int bitmap;
250
251	bitmap = 0;
252	if (getenv_int("mpt_disable", &bitmap)) {
253		if (bitmap & (1 << mpt->unit)) {
254			mpt->disabled = 1;
255		}
256	}
257	bitmap = 0;
258	if (getenv_int("mpt_debug", &bitmap)) {
259		if (bitmap & (1 << mpt->unit)) {
260			mpt->verbose = MPT_PRT_DEBUG;
261		}
262	}
263	bitmap = 0;
264	if (getenv_int("mpt_debug1", &bitmap)) {
265		if (bitmap & (1 << mpt->unit)) {
266			mpt->verbose = MPT_PRT_DEBUG1;
267		}
268	}
269	bitmap = 0;
270	if (getenv_int("mpt_debug2", &bitmap)) {
271		if (bitmap & (1 << mpt->unit)) {
272			mpt->verbose = MPT_PRT_DEBUG2;
273		}
274	}
275	bitmap = 0;
276	if (getenv_int("mpt_debug3", &bitmap)) {
277		if (bitmap & (1 << mpt->unit)) {
278			mpt->verbose = MPT_PRT_DEBUG3;
279		}
280	}
281}
282#else
283static void
284mpt_set_options(struct mpt_softc *mpt)
285{
286	int tval;
287
288	tval = 0;
289	if (resource_int_value(device_get_name(mpt->dev),
290	    device_get_unit(mpt->dev), "disable", &tval) == 0 && tval != 0) {
291		mpt->disabled = 1;
292	}
293	tval = 0;
294	if (resource_int_value(device_get_name(mpt->dev),
295	    device_get_unit(mpt->dev), "debug", &tval) == 0 && tval != 0) {
296		mpt->verbose += tval;
297	}
298	tval = 0;
299	if (resource_int_value(device_get_name(mpt->dev),
300	    device_get_unit(mpt->dev), "role", &tval) == 0 && tval != 0 &&
301	    tval <= 3) {
302		mpt->role = tval;
303	}
304}
305#endif
306
307
308static void
309mpt_link_peer(struct mpt_softc *mpt)
310{
311	struct mpt_softc *mpt2;
312
313	if (mpt->unit == 0) {
314		return;
315	}
316	/*
317	 * XXX: depends on probe order
318	 */
319	mpt2 = (struct mpt_softc *)devclass_get_softc(mpt_devclass,mpt->unit-1);
320
321	if (mpt2 == NULL) {
322		return;
323	}
324	if (pci_get_vendor(mpt2->dev) != pci_get_vendor(mpt->dev)) {
325		return;
326	}
327	if (pci_get_device(mpt2->dev) != pci_get_device(mpt->dev)) {
328		return;
329	}
330	mpt->mpt2 = mpt2;
331	mpt2->mpt2 = mpt;
332	if (mpt->verbose >= MPT_PRT_DEBUG) {
333		mpt_prt(mpt, "linking with peer (mpt%d)\n",
334		    device_get_unit(mpt2->dev));
335	}
336}
337
338static void
339mpt_unlink_peer(struct mpt_softc *mpt)
340{
341	if (mpt->mpt2) {
342		mpt->mpt2->mpt2 = NULL;
343	}
344}
345
346
347static int
348mpt_pci_attach(device_t dev)
349{
350	struct mpt_softc *mpt;
351	int		  iqd;
352	uint32_t	  data, cmd;
353
354	/* Allocate the softc structure */
355	mpt  = (struct mpt_softc*)device_get_softc(dev);
356	if (mpt == NULL) {
357		device_printf(dev, "cannot allocate softc\n");
358		return (ENOMEM);
359	}
360	memset(mpt, 0, sizeof(struct mpt_softc));
361	switch ((pci_get_device(dev) & ~1)) {
362	case PCI_PRODUCT_LSI_FC909:
363	case PCI_PRODUCT_LSI_FC909A:
364	case PCI_PRODUCT_LSI_FC919:
365	case PCI_PRODUCT_LSI_FC929:
366		mpt->is_fc = 1;
367		break;
368	case PCI_PRODUCT_LSI_SAS1064:
369	case PCI_PRODUCT_LSI_SAS1064A:
370	case PCI_PRODUCT_LSI_SAS1064E:
371	case PCI_PRODUCT_LSI_SAS1066:
372	case PCI_PRODUCT_LSI_SAS1066E:
373	case PCI_PRODUCT_LSI_SAS1068:
374	case PCI_PRODUCT_LSI_SAS1068E:
375	case PCI_PRODUCT_LSI_SAS1078:
376		mpt->is_sas = 1;
377		break;
378	default:
379		break;
380	}
381	mpt->dev = dev;
382	mpt->unit = device_get_unit(dev);
383	mpt->raid_resync_rate = MPT_RAID_RESYNC_RATE_DEFAULT;
384	mpt->raid_mwce_setting = MPT_RAID_MWCE_DEFAULT;
385	mpt->raid_queue_depth = MPT_RAID_QUEUE_DEPTH_DEFAULT;
386	mpt->verbose = MPT_PRT_NONE;
387	mpt->role = MPT_ROLE_NONE;
388	mpt_set_options(mpt);
389	if (mpt->verbose == MPT_PRT_NONE) {
390		mpt->verbose = MPT_PRT_WARN;
391		/* Print INFO level (if any) if bootverbose is set */
392		mpt->verbose += (bootverbose != 0)? 1 : 0;
393	}
394	/* Make sure memory access decoders are enabled */
395	cmd = pci_read_config(dev, PCIR_COMMAND, 2);
396	if ((cmd & PCIM_CMD_MEMEN) == 0) {
397		device_printf(dev, "Memory accesses disabled");
398		return (ENXIO);
399	}
400
401	/*
402	 * Make sure that SERR, PERR, WRITE INVALIDATE and BUSMASTER are set.
403	 */
404	cmd |=
405	    PCIM_CMD_SERRESPEN | PCIM_CMD_PERRESPEN |
406	    PCIM_CMD_BUSMASTEREN | PCIM_CMD_MWRICEN;
407	pci_write_config(dev, PCIR_COMMAND, cmd, 2);
408
409	/*
410	 * Make sure we've disabled the ROM.
411	 */
412	data = pci_read_config(dev, PCIR_BIOS, 4);
413	data &= ~1;
414	pci_write_config(dev, PCIR_BIOS, data, 4);
415
416	/*
417	 * Is this part a dual?
418	 * If so, link with our partner (around yet)
419	 */
420	if ((pci_get_device(dev) & ~1) == PCI_PRODUCT_LSI_FC929 ||
421	    (pci_get_device(dev) & ~1) == PCI_PRODUCT_LSI_1030) {
422		mpt_link_peer(mpt);
423	}
424
425	/*
426	 * Set up register access.  PIO mode is required for
427	 * certain reset operations (but must be disabled for
428	 * some cards otherwise).
429	 */
430	mpt->pci_pio_rid = PCIR_BAR(MPT_IO_BAR);
431	mpt->pci_pio_reg = bus_alloc_resource(dev, SYS_RES_IOPORT,
432			    &mpt->pci_pio_rid, 0, ~0, 0, RF_ACTIVE);
433	if (mpt->pci_pio_reg == NULL) {
434		device_printf(dev, "unable to map registers in PIO mode\n");
435		goto bad;
436	}
437	mpt->pci_pio_st = rman_get_bustag(mpt->pci_pio_reg);
438	mpt->pci_pio_sh = rman_get_bushandle(mpt->pci_pio_reg);
439
440	/* Allocate kernel virtual memory for the 9x9's Mem0 region */
441	mpt->pci_mem_rid = PCIR_BAR(MPT_MEM_BAR);
442	mpt->pci_reg = bus_alloc_resource(dev, SYS_RES_MEMORY,
443			&mpt->pci_mem_rid, 0, ~0, 0, RF_ACTIVE);
444	if (mpt->pci_reg == NULL) {
445		device_printf(dev, "Unable to memory map registers.\n");
446		if (mpt->is_sas) {
447			device_printf(dev, "Giving Up.\n");
448			goto bad;
449		}
450		device_printf(dev, "Falling back to PIO mode.\n");
451		mpt->pci_st = mpt->pci_pio_st;
452		mpt->pci_sh = mpt->pci_pio_sh;
453	} else {
454		mpt->pci_st = rman_get_bustag(mpt->pci_reg);
455		mpt->pci_sh = rman_get_bushandle(mpt->pci_reg);
456	}
457
458	/* Get a handle to the interrupt */
459	iqd = 0;
460#if __FreeBSD_version < 500000
461	mpt->pci_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &iqd, 0, ~0, 1,
462	    RF_ACTIVE | RF_SHAREABLE);
463#else
464	mpt->pci_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &iqd,
465	    RF_ACTIVE | RF_SHAREABLE);
466#endif
467	if (mpt->pci_irq == NULL) {
468		device_printf(dev, "could not allocate interrupt\n");
469		goto bad;
470	}
471
472	MPT_LOCK_SETUP(mpt);
473
474	/* Disable interrupts at the part */
475	mpt_disable_ints(mpt);
476
477	/* Register the interrupt handler */
478	if (bus_setup_intr(dev, mpt->pci_irq, MPT_IFLAGS, mpt_pci_intr,
479	    mpt, &mpt->ih)) {
480		device_printf(dev, "could not setup interrupt\n");
481		goto bad;
482	}
483
484	/* Allocate dma memory */
485/* XXX JGibbs -Should really be done based on IOCFacts. */
486	if (mpt_dma_mem_alloc(mpt)) {
487		device_printf(dev, "Could not allocate DMA memory\n");
488		goto bad;
489	}
490
491	/*
492	 * Save the PCI config register values
493 	 *
494	 * Hard resets are known to screw up the BAR for diagnostic
495	 * memory accesses (Mem1).
496	 *
497	 * Using Mem1 is known to make the chip stop responding to
498	 * configuration space transfers, so we need to save it now
499	 */
500
501	mpt_read_config_regs(mpt);
502
503	/*
504	 * Disable PIO until we need it
505	 */
506	pci_disable_io(dev, SYS_RES_IOPORT);
507
508	/* Initialize the hardware */
509	if (mpt->disabled == 0) {
510		MPT_LOCK(mpt);
511		if (mpt_attach(mpt) != 0) {
512			MPT_UNLOCK(mpt);
513			goto bad;
514		}
515		MPT_UNLOCK(mpt);
516	} else {
517		mpt_prt(mpt, "device disabled at user request\n");
518		goto bad;
519	}
520
521	mpt->eh = EVENTHANDLER_REGISTER(shutdown_post_sync, mpt_pci_shutdown,
522	    dev, SHUTDOWN_PRI_DEFAULT);
523
524	if (mpt->eh == NULL) {
525		mpt_prt(mpt, "shutdown event registration failed\n");
526		MPT_LOCK(mpt);
527		(void) mpt_detach(mpt);
528		MPT_UNLOCK(mpt);
529		goto bad;
530	}
531	KASSERT(MPT_OWNED(mpt) == 0, ("leaving attach with device locked"));
532	return (0);
533
534bad:
535	mpt_dma_mem_free(mpt);
536	mpt_free_bus_resources(mpt);
537	mpt_unlink_peer(mpt);
538
539	MPT_LOCK_DESTROY(mpt);
540
541	/*
542	 * but return zero to preserve unit numbering
543	 */
544	return (0);
545}
546
547/*
548 * Free bus resources
549 */
550static void
551mpt_free_bus_resources(struct mpt_softc *mpt)
552{
553	if (mpt->ih) {
554		bus_teardown_intr(mpt->dev, mpt->pci_irq, mpt->ih);
555		mpt->ih = 0;
556	}
557
558	if (mpt->pci_irq) {
559		bus_release_resource(mpt->dev, SYS_RES_IRQ, 0, mpt->pci_irq);
560		mpt->pci_irq = 0;
561	}
562
563	if (mpt->pci_pio_reg) {
564		bus_release_resource(mpt->dev, SYS_RES_IOPORT, mpt->pci_pio_rid,
565			mpt->pci_pio_reg);
566		mpt->pci_pio_reg = 0;
567	}
568	if (mpt->pci_reg) {
569		bus_release_resource(mpt->dev, SYS_RES_MEMORY, mpt->pci_mem_rid,
570			mpt->pci_reg);
571		mpt->pci_reg = 0;
572	}
573	MPT_LOCK_DESTROY(mpt);
574}
575
576
577/*
578 * Disconnect ourselves from the system.
579 */
580static int
581mpt_pci_detach(device_t dev)
582{
583	struct mpt_softc *mpt;
584
585	mpt  = (struct mpt_softc*)device_get_softc(dev);
586
587	if (mpt) {
588		MPT_LOCK(mpt);
589		mpt_disable_ints(mpt);
590		mpt_detach(mpt);
591		mpt_reset(mpt, /*reinit*/FALSE);
592		mpt_dma_mem_free(mpt);
593		mpt_free_bus_resources(mpt);
594		if (mpt->raid_volumes != NULL && mpt->ioc_page2 != NULL) {
595			int i;
596			for (i = 0; i < mpt->ioc_page2->MaxVolumes; i++) {
597				struct mpt_raid_volume *mpt_vol;
598
599				mpt_vol = &mpt->raid_volumes[i];
600				if (mpt_vol->config_page) {
601					free(mpt_vol->config_page, M_DEVBUF);
602				}
603			}
604		}
605		if (mpt->ioc_page2 != NULL)
606			free(mpt->ioc_page2, M_DEVBUF);
607		if (mpt->ioc_page3 != NULL)
608			free(mpt->ioc_page3, M_DEVBUF);
609		if (mpt->raid_volumes != NULL)
610			free(mpt->raid_volumes, M_DEVBUF);
611		if (mpt->raid_disks != NULL)
612			free(mpt->raid_disks, M_DEVBUF);
613		if (mpt->eh != NULL)
614                        EVENTHANDLER_DEREGISTER(shutdown_final, mpt->eh);
615		MPT_UNLOCK(mpt);
616	}
617	return(0);
618}
619
620
621/*
622 * Disable the hardware
623 */
624static int
625mpt_pci_shutdown(device_t dev)
626{
627	struct mpt_softc *mpt;
628
629	mpt = (struct mpt_softc *)device_get_softc(dev);
630	if (mpt) {
631		int r;
632		MPT_LOCK(mpt);
633		r = mpt_shutdown(mpt);
634		MPT_UNLOCK(mpt);
635		return (r);
636	}
637	return(0);
638}
639
640static int
641mpt_dma_mem_alloc(struct mpt_softc *mpt)
642{
643	int i, error, nsegs;
644	uint8_t *vptr;
645	uint32_t pptr, end;
646	size_t len;
647	struct mpt_map_info mi;
648	device_t dev = mpt->dev;
649
650	/* Check if we alreay have allocated the reply memory */
651	if (mpt->reply_phys != 0) {
652		return 0;
653	}
654
655	len = sizeof (request_t) * MPT_MAX_REQUESTS(mpt);
656#ifdef	RELENG_4
657	mpt->request_pool = (request_t *)malloc(len, M_DEVBUF, M_WAITOK);
658	if (mpt->request_pool == NULL) {
659		device_printf(dev, "cannot allocate request pool\n");
660		return (1);
661	}
662	memset(mpt->request_pool, 0, len);
663#else
664	mpt->request_pool = (request_t *)malloc(len, M_DEVBUF, M_WAITOK|M_ZERO);
665	if (mpt->request_pool == NULL) {
666		device_printf(dev, "cannot allocate request pool\n");
667		return (1);
668	}
669#endif
670
671	/*
672	 * Create a parent dma tag for this device.
673	 *
674	 * Align at byte boundaries, limit to 32-bit addressing for
675	 * request/reply queues.
676	 */
677	if (mpt_dma_tag_create(mpt, /*parent*/NULL, /*alignment*/1,
678	    /*boundary*/0, /*lowaddr*/BUS_SPACE_MAXADDR,
679	    /*highaddr*/BUS_SPACE_MAXADDR, /*filter*/NULL, /*filterarg*/NULL,
680	    /*maxsize*/BUS_SPACE_MAXSIZE_32BIT,
681	    /*nsegments*/BUS_SPACE_MAXSIZE_32BIT,
682	    /*maxsegsz*/BUS_SPACE_UNRESTRICTED, /*flags*/0,
683	    &mpt->parent_dmat) != 0) {
684		device_printf(dev, "cannot create parent dma tag\n");
685		return (1);
686	}
687
688	/* Create a child tag for reply buffers */
689	if (mpt_dma_tag_create(mpt, mpt->parent_dmat, 2 * PAGE_SIZE,
690	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
691	    NULL, NULL, 2 * PAGE_SIZE, 1, BUS_SPACE_MAXSIZE_32BIT, 0,
692	    &mpt->reply_dmat) != 0) {
693		device_printf(dev, "cannot create a dma tag for replies\n");
694		return (1);
695	}
696
697	/* Allocate some DMA accessable memory for replies */
698	if (bus_dmamem_alloc(mpt->reply_dmat, (void **)&mpt->reply,
699	    BUS_DMA_NOWAIT, &mpt->reply_dmap) != 0) {
700		device_printf(dev,
701		    "cannot allocate %lu bytes of reply memory\n",
702		    (u_long) (2 * PAGE_SIZE));
703		return (1);
704	}
705
706	mi.mpt = mpt;
707	mi.error = 0;
708
709	/* Load and lock it into "bus space" */
710	bus_dmamap_load(mpt->reply_dmat, mpt->reply_dmap, mpt->reply,
711	    2 * PAGE_SIZE, mpt_map_rquest, &mi, 0);
712
713	if (mi.error) {
714		device_printf(dev,
715		    "error %d loading dma map for DMA reply queue\n", mi.error);
716		return (1);
717	}
718	mpt->reply_phys = mi.phys;
719
720	/* Create a child tag for data buffers */
721
722	/*
723	 * XXX: we should say that nsegs is 'unrestricted, but that
724	 * XXX: tickles a horrible bug in the busdma code. Instead,
725	 * XXX: we'll derive a reasonable segment limit from MAXPHYS
726	 */
727	nsegs = (MAXPHYS / PAGE_SIZE) + 1;
728	if (mpt_dma_tag_create(mpt, mpt->parent_dmat, 1,
729	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
730	    NULL, NULL, MAXBSIZE, nsegs, BUS_SPACE_MAXSIZE_32BIT, 0,
731	    &mpt->buffer_dmat) != 0) {
732		device_printf(dev,
733		    "cannot create a dma tag for data buffers\n");
734		return (1);
735	}
736
737	/* Create a child tag for request buffers */
738	if (mpt_dma_tag_create(mpt, mpt->parent_dmat, PAGE_SIZE,
739	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
740	    NULL, NULL, MPT_REQ_MEM_SIZE(mpt), 1, BUS_SPACE_MAXSIZE_32BIT, 0,
741	    &mpt->request_dmat) != 0) {
742		device_printf(dev, "cannot create a dma tag for requests\n");
743		return (1);
744	}
745
746	/* Allocate some DMA accessable memory for requests */
747	if (bus_dmamem_alloc(mpt->request_dmat, (void **)&mpt->request,
748	    BUS_DMA_NOWAIT, &mpt->request_dmap) != 0) {
749		device_printf(dev,
750		    "cannot allocate %d bytes of request memory\n",
751		    MPT_REQ_MEM_SIZE(mpt));
752		return (1);
753	}
754
755	mi.mpt = mpt;
756	mi.error = 0;
757
758	/* Load and lock it into "bus space" */
759        bus_dmamap_load(mpt->request_dmat, mpt->request_dmap, mpt->request,
760	    MPT_REQ_MEM_SIZE(mpt), mpt_map_rquest, &mi, 0);
761
762	if (mi.error) {
763		device_printf(dev,
764		    "error %d loading dma map for DMA request queue\n",
765		    mi.error);
766		return (1);
767	}
768	mpt->request_phys = mi.phys;
769
770	i = 0;
771	pptr =  mpt->request_phys;
772	vptr =  mpt->request;
773	end = pptr + MPT_REQ_MEM_SIZE(mpt);
774	while(pptr < end) {
775		request_t *req = &mpt->request_pool[i];
776		req->index = i++;
777
778		/* Store location of Request Data */
779		req->req_pbuf = pptr;
780		req->req_vbuf = vptr;
781
782		pptr += MPT_REQUEST_AREA;
783		vptr += MPT_REQUEST_AREA;
784
785		req->sense_pbuf = (pptr - MPT_SENSE_SIZE);
786		req->sense_vbuf = (vptr - MPT_SENSE_SIZE);
787
788		error = bus_dmamap_create(mpt->buffer_dmat, 0, &req->dmap);
789		if (error) {
790			device_printf(dev,
791			     "error %d creating per-cmd DMA maps\n", error);
792			return (1);
793		}
794	}
795	return (0);
796}
797
798
799
800/* Deallocate memory that was allocated by mpt_dma_mem_alloc
801 */
802static void
803mpt_dma_mem_free(struct mpt_softc *mpt)
804{
805	int i;
806
807        /* Make sure we aren't double destroying */
808        if (mpt->reply_dmat == 0) {
809		if (mpt->verbose >= MPT_PRT_DEBUG)
810			device_printf(mpt->dev,"Already released dma memory\n");
811		return;
812        }
813
814	for (i = 0; i < MPT_MAX_REQUESTS(mpt); i++) {
815		bus_dmamap_destroy(mpt->buffer_dmat, mpt->request_pool[i].dmap);
816	}
817	bus_dmamap_unload(mpt->request_dmat, mpt->request_dmap);
818	bus_dmamem_free(mpt->request_dmat, mpt->request, mpt->request_dmap);
819	bus_dma_tag_destroy(mpt->request_dmat);
820	bus_dma_tag_destroy(mpt->buffer_dmat);
821	bus_dmamap_unload(mpt->reply_dmat, mpt->reply_dmap);
822	bus_dmamem_free(mpt->reply_dmat, mpt->reply, mpt->reply_dmap);
823	bus_dma_tag_destroy(mpt->reply_dmat);
824	bus_dma_tag_destroy(mpt->parent_dmat);
825	mpt->reply_dmat = 0;
826	free(mpt->request_pool, M_DEVBUF);
827	mpt->request_pool = 0;
828
829}
830
831
832
833/* Reads modifiable (via PCI transactions) config registers */
834static void
835mpt_read_config_regs(struct mpt_softc *mpt)
836{
837	mpt->pci_cfg.Command = pci_read_config(mpt->dev, PCIR_COMMAND, 2);
838	mpt->pci_cfg.LatencyTimer_LineSize =
839	    pci_read_config(mpt->dev, PCIR_CACHELNSZ, 2);
840	mpt->pci_cfg.IO_BAR = pci_read_config(mpt->dev, PCIR_BAR(0), 4);
841	mpt->pci_cfg.Mem0_BAR[0] = pci_read_config(mpt->dev, PCIR_BAR(1), 4);
842	mpt->pci_cfg.Mem0_BAR[1] = pci_read_config(mpt->dev, PCIR_BAR(2), 4);
843	mpt->pci_cfg.Mem1_BAR[0] = pci_read_config(mpt->dev, PCIR_BAR(3), 4);
844	mpt->pci_cfg.Mem1_BAR[1] = pci_read_config(mpt->dev, PCIR_BAR(4), 4);
845	mpt->pci_cfg.ROM_BAR = pci_read_config(mpt->dev, PCIR_BIOS, 4);
846	mpt->pci_cfg.IntLine = pci_read_config(mpt->dev, PCIR_INTLINE, 1);
847	mpt->pci_cfg.PMCSR = pci_read_config(mpt->dev, 0x44, 4);
848}
849
850/* Sets modifiable config registers */
851void
852mpt_set_config_regs(struct mpt_softc *mpt)
853{
854	uint32_t val;
855
856#define MPT_CHECK(reg, offset, size)					\
857	val = pci_read_config(mpt->dev, offset, size);			\
858	if (mpt->pci_cfg.reg != val) {					\
859		mpt_prt(mpt,						\
860		    "Restoring " #reg " to 0x%X from 0x%X\n",		\
861		    mpt->pci_cfg.reg, val);				\
862	}
863
864	if (mpt->verbose >= MPT_PRT_DEBUG) {
865		MPT_CHECK(Command, PCIR_COMMAND, 2);
866		MPT_CHECK(LatencyTimer_LineSize, PCIR_CACHELNSZ, 2);
867		MPT_CHECK(IO_BAR, PCIR_BAR(0), 4);
868		MPT_CHECK(Mem0_BAR[0], PCIR_BAR(1), 4);
869		MPT_CHECK(Mem0_BAR[1], PCIR_BAR(2), 4);
870		MPT_CHECK(Mem1_BAR[0], PCIR_BAR(3), 4);
871		MPT_CHECK(Mem1_BAR[1], PCIR_BAR(4), 4);
872		MPT_CHECK(ROM_BAR, PCIR_BIOS, 4);
873		MPT_CHECK(IntLine, PCIR_INTLINE, 1);
874		MPT_CHECK(PMCSR, 0x44, 4);
875	}
876#undef MPT_CHECK
877
878	pci_write_config(mpt->dev, PCIR_COMMAND, mpt->pci_cfg.Command, 2);
879	pci_write_config(mpt->dev, PCIR_CACHELNSZ,
880	    mpt->pci_cfg.LatencyTimer_LineSize, 2);
881	pci_write_config(mpt->dev, PCIR_BAR(0), mpt->pci_cfg.IO_BAR, 4);
882	pci_write_config(mpt->dev, PCIR_BAR(1), mpt->pci_cfg.Mem0_BAR[0], 4);
883	pci_write_config(mpt->dev, PCIR_BAR(2), mpt->pci_cfg.Mem0_BAR[1], 4);
884	pci_write_config(mpt->dev, PCIR_BAR(3), mpt->pci_cfg.Mem1_BAR[0], 4);
885	pci_write_config(mpt->dev, PCIR_BAR(4), mpt->pci_cfg.Mem1_BAR[1], 4);
886	pci_write_config(mpt->dev, PCIR_BIOS, mpt->pci_cfg.ROM_BAR, 4);
887	pci_write_config(mpt->dev, PCIR_INTLINE, mpt->pci_cfg.IntLine, 1);
888	pci_write_config(mpt->dev, 0x44, mpt->pci_cfg.PMCSR, 4);
889}
890
891static void
892mpt_pci_intr(void *arg)
893{
894	struct mpt_softc *mpt;
895
896	mpt = (struct mpt_softc *)arg;
897	MPT_LOCK(mpt);
898	mpt_intr(mpt);
899	MPT_UNLOCK(mpt);
900}
901