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