mpt_pci.c revision 101704
1101704Smjacob/* $FreeBSD: head/sys/dev/mpt/mpt_pci.c 101704 2002-08-11 23:34:20Z mjacob $ */
2101704Smjacob/*
3101704Smjacob * PCI specific probe and attach routines for LSI '909 FC  adapters.
4101704Smjacob * FreeBSD Version.
5101704Smjacob *
6101704Smjacob * Copyright (c)  2000, 2001 by Greg Ansley
7101704Smjacob * Partially derived from Matt Jacob's ISP driver.
8101704Smjacob *
9101704Smjacob * Redistribution and use in source and binary forms, with or without
10101704Smjacob * modification, are permitted provided that the following conditions
11101704Smjacob * are met:
12101704Smjacob * 1. Redistributions of source code must retain the above copyright
13101704Smjacob *    notice immediately at the beginning of the file, without modification,
14101704Smjacob *    this list of conditions, and the following disclaimer.
15101704Smjacob * 2. The name of the author may not be used to endorse or promote products
16101704Smjacob *    derived from this software without specific prior written permission.
17101704Smjacob *
18101704Smjacob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19101704Smjacob * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20101704Smjacob * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21101704Smjacob * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22101704Smjacob * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23101704Smjacob * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24101704Smjacob * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25101704Smjacob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26101704Smjacob * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27101704Smjacob * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28101704Smjacob * SUCH DAMAGE.
29101704Smjacob */
30101704Smjacob/*
31101704Smjacob * Additional Copyright (c) 2002 by Matthew Jacob under same license.
32101704Smjacob */
33101704Smjacob
34101704Smjacob#include <sys/param.h>
35101704Smjacob#include <sys/systm.h>
36101704Smjacob#include <sys/kernel.h>
37101704Smjacob#include <sys/bus.h>
38101704Smjacob#include <sys/conf.h>
39101704Smjacob#include <sys/malloc.h>
40101704Smjacob#include <sys/mbuf.h>
41101704Smjacob#include <sys/module.h>
42101704Smjacob#include <pci/pcireg.h>
43101704Smjacob#include <pci/pcivar.h>
44101704Smjacob
45101704Smjacob#include <machine/bus_memio.h>
46101704Smjacob#include <machine/bus_pio.h>
47101704Smjacob#include <machine/bus.h>
48101704Smjacob#include <machine/resource.h>
49101704Smjacob#include <sys/rman.h>
50101704Smjacob
51101704Smjacob#include <dev/mpt/mpt.h>
52101704Smjacob#include <dev/mpt/mpt_freebsd.h>
53101704Smjacob
54101704Smjacob#ifndef	PCI_VENDOR_LSI
55101704Smjacob#define	PCI_VENDOR_LSI			0x1000
56101704Smjacob#endif
57101704Smjacob
58101704Smjacob#ifndef	PCI_PRODUCT_LSI_FC909
59101704Smjacob#define	PCI_PRODUCT_LSI_FC909		0x0620
60101704Smjacob#endif
61101704Smjacob
62101704Smjacob#ifndef	PCI_PRODUCT_LSI_FC929
63101704Smjacob#define	PCI_PRODUCT_LSI_FC929		0x0622
64101704Smjacob#endif
65101704Smjacob
66101704Smjacob#ifndef	PCI_PRODUCT_LSI_1030
67101704Smjacob#define	PCI_PRODUCT_LSI_1030		0x0030
68101704Smjacob#endif
69101704Smjacob
70101704Smjacob
71101704Smjacob
72101704Smjacob#define	MEM_MAP_REG	0x14
73101704Smjacob#define	MEM_MAP_SRAM	0x1C
74101704Smjacob
75101704Smjacobstatic int mpt_probe(device_t);
76101704Smjacobstatic int mpt_attach(device_t);
77101704Smjacobstatic void mpt_free_bus_resources(struct mpt_softc *mpt);
78101704Smjacobstatic int mpt_detach(device_t);
79101704Smjacobstatic int mpt_shutdown(device_t);
80101704Smjacobstatic int mpt_dma_mem_alloc(struct mpt_softc *mpt);
81101704Smjacobstatic void mpt_dma_mem_free(struct mpt_softc *mpt);
82101704Smjacobstatic void mpt_read_config_regs(struct mpt_softc *mpt);
83101704Smjacob
84101704Smjacobstatic device_method_t mpt_methods[] = {
85101704Smjacob	/* Device interface */
86101704Smjacob	DEVMETHOD(device_probe,		mpt_probe),
87101704Smjacob	DEVMETHOD(device_attach,	mpt_attach),
88101704Smjacob	DEVMETHOD(device_detach,	mpt_detach),
89101704Smjacob	DEVMETHOD(device_shutdown,	mpt_shutdown),
90101704Smjacob	{ 0, 0 }
91101704Smjacob};
92101704Smjacob
93101704Smjacobstatic driver_t mpt_driver = {
94101704Smjacob	"mpt", mpt_methods, sizeof (struct mpt_softc)
95101704Smjacob};
96101704Smjacobstatic devclass_t mpt_devclass;
97101704SmjacobDRIVER_MODULE(mpt, pci, mpt_driver, mpt_devclass, 0, 0);
98101704SmjacobMODULE_VERSION(mpt, 1);
99101704Smjacob
100101704Smjacobint
101101704Smjacobmpt_intr(void *dummy)
102101704Smjacob{
103101704Smjacob	u_int32_t reply;
104101704Smjacob	struct mpt_softc *mpt = (struct mpt_softc *)dummy;
105101704Smjacob
106101704Smjacob	reply = mpt_pop_reply_queue(mpt);
107101704Smjacob	while (reply != MPT_REPLY_EMPTY) {
108101704Smjacob		if (mpt->verbose > 1) {
109101704Smjacob			if ((reply & MPT_CONTEXT_REPLY) != 0)  {
110101704Smjacob				/* Address reply; IOC has something to say */
111101704Smjacob				mpt_print_reply(MPT_REPLY_PTOV(mpt, reply));
112101704Smjacob			} else {
113101704Smjacob				/* Context reply ; all went well */
114101704Smjacob				device_printf(mpt->dev,
115101704Smjacob				    "context %u reply OK\n", reply);
116101704Smjacob			}
117101704Smjacob		}
118101704Smjacob		mpt_done(mpt, reply);
119101704Smjacob		reply = mpt_pop_reply_queue(mpt);
120101704Smjacob	}
121101704Smjacob	return 0;
122101704Smjacob}
123101704Smjacob
124101704Smjacobstatic int
125101704Smjacobmpt_probe(device_t dev)
126101704Smjacob{
127101704Smjacob	char *desc;
128101704Smjacob
129101704Smjacob	if (pci_get_vendor(dev) != PCI_VENDOR_LSI)
130101704Smjacob		return (ENXIO);
131101704Smjacob
132101704Smjacob	switch ((pci_get_device(dev) & ~1)) {
133101704Smjacob	case PCI_PRODUCT_LSI_FC909:
134101704Smjacob		desc = "LSILogic FC909 FC Adapter";
135101704Smjacob		break;
136101704Smjacob	case PCI_PRODUCT_LSI_FC929:
137101704Smjacob		desc = "LSILogic FC929 FC Adapter";
138101704Smjacob		break;
139101704Smjacob	case PCI_PRODUCT_LSI_1030:
140101704Smjacob		desc = "LSILogic 1030 Ultra4 Adapter";
141101704Smjacob		break;
142101704Smjacob	default:
143101704Smjacob		return (ENXIO);
144101704Smjacob	}
145101704Smjacob
146101704Smjacob	device_set_desc(dev, desc);
147101704Smjacob	return (0);
148101704Smjacob}
149101704Smjacob
150101704Smjacob#ifdef	RELENG_4
151101704Smjacobstatic void
152101704Smjacobmpt_set_options(struct mpt_softc *mpt)
153101704Smjacob{
154101704Smjacob	int bitmap;
155101704Smjacob
156101704Smjacob	bitmap = 0;
157101704Smjacob	if (getenv_int("mpt_disable", &bitmap)) {
158101704Smjacob		if (bitmap & (1 << mpt->unit)) {
159101704Smjacob			mpt->disabled = 1;
160101704Smjacob		}
161101704Smjacob	}
162101704Smjacob
163101704Smjacob	bitmap = 0;
164101704Smjacob	if (getenv_int("mpt_debug", &bitmap)) {
165101704Smjacob		if (bitmap & (1 << mpt->unit)) {
166101704Smjacob			mpt->verbose = 2;
167101704Smjacob		}
168101704Smjacob	}
169101704Smjacob
170101704Smjacob	cmd = pci_read_config(dev, PCIR_COMMAND, 2);
171101704Smjacob}
172101704Smjacob#else
173101704Smjacobstatic void
174101704Smjacobmpt_set_options(struct mpt_softc *mpt)
175101704Smjacob{
176101704Smjacob	int tval;
177101704Smjacob
178101704Smjacob	tval = 0;
179101704Smjacob	if (resource_int_value(device_get_name(mpt->dev),
180101704Smjacob	    device_get_unit(mpt->dev), "disable", &tval) == 0 && tval != 0) {
181101704Smjacob		mpt->disabled = 1;
182101704Smjacob	}
183101704Smjacob	tval = 0;
184101704Smjacob	if (resource_int_value(device_get_name(mpt->dev),
185101704Smjacob	    device_get_unit(mpt->dev), "debug", &tval) == 0 && tval != 0) {
186101704Smjacob		mpt->verbose += tval;
187101704Smjacob	}
188101704Smjacob}
189101704Smjacob#endif
190101704Smjacob
191101704Smjacob
192101704Smjacobstatic int
193101704Smjacobmpt_attach(device_t dev)
194101704Smjacob{
195101704Smjacob	int iqd;
196101704Smjacob	u_int32_t data, cmd;
197101704Smjacob	struct mpt_softc *mpt;
198101704Smjacob
199101704Smjacob	/* Allocate the softc structure */
200101704Smjacob	mpt  = (struct mpt_softc*) device_get_softc(dev);
201101704Smjacob	if (mpt == NULL) {
202101704Smjacob		device_printf(dev, "cannot allocate softc\n");
203101704Smjacob		return (ENOMEM);
204101704Smjacob	}
205101704Smjacob	bzero(mpt, sizeof (struct mpt_softc));
206101704Smjacob	switch ((pci_get_device(dev) & ~1)) {
207101704Smjacob	case PCI_PRODUCT_LSI_FC909:
208101704Smjacob	case PCI_PRODUCT_LSI_FC929:
209101704Smjacob		mpt->is_fc = 1;
210101704Smjacob		break;
211101704Smjacob	default:
212101704Smjacob		break;
213101704Smjacob	}
214101704Smjacob	mpt->dev = dev;
215101704Smjacob	mpt->unit = device_get_unit(dev);
216101704Smjacob	mpt_set_options(mpt);
217101704Smjacob	mpt->verbose += (bootverbose != 0)? 1 : 0;
218101704Smjacob
219101704Smjacob	/* Make sure memory access decoders are enabled */
220101704Smjacob	cmd = pci_read_config(dev, PCIR_COMMAND, 2);
221101704Smjacob	if ((cmd & PCIM_CMD_MEMEN) == 0) {
222101704Smjacob		device_printf(dev, "Memory accesses disabled");
223101704Smjacob		goto bad;
224101704Smjacob	}
225101704Smjacob
226101704Smjacob	/*
227101704Smjacob	 * Make sure that SERR, PERR, WRITE INVALIDATE and BUSMASTER are set.
228101704Smjacob	 */
229101704Smjacob	cmd |=
230101704Smjacob	    PCIM_CMD_SERRESPEN | PCIM_CMD_PERRESPEN |
231101704Smjacob	    PCIM_CMD_BUSMASTEREN | PCIM_CMD_MWRICEN;
232101704Smjacob	pci_write_config(dev, PCIR_COMMAND, cmd, 2);
233101704Smjacob
234101704Smjacob	/*
235101704Smjacob	 * Make sure we've disabled the ROM.
236101704Smjacob	 */
237101704Smjacob	data = pci_read_config(dev, PCIR_BIOS, 4);
238101704Smjacob	data &= ~1;
239101704Smjacob	pci_write_config(dev, PCIR_BIOS, data, 4);
240101704Smjacob
241101704Smjacob
242101704Smjacob	/* Is this part a dual? */
243101704Smjacob	if ((pci_get_device(dev) & ~1) == PCI_PRODUCT_LSI_FC929) {
244101704Smjacob		/* Yes; is the previous device the counterpart? */
245101704Smjacob		if (mpt->unit) {
246101704Smjacob			mpt->mpt2 = (struct mpt_softc *)
247101704Smjacob				devclass_get_softc(mpt_devclass, mpt->unit-1);
248101704Smjacob
249101704Smjacob			if ((mpt->mpt2->mpt2 == NULL)
250101704Smjacob			  && (pci_get_vendor(mpt->mpt2->dev) == PCI_VENDOR_LSI)
251101704Smjacob			  && ((pci_get_device(mpt->mpt2->dev) & ~1) == PCI_PRODUCT_LSI_FC929) ) {
252101704Smjacob				/* Yes */
253101704Smjacob				mpt->mpt2->mpt2 = mpt;
254101704Smjacob				if (mpt->verbose) {
255101704Smjacob					device_printf(dev, "Detected dual\n");
256101704Smjacob				}
257101704Smjacob			} else {
258101704Smjacob				/* Nope */
259101704Smjacob				mpt->mpt2 = NULL;
260101704Smjacob			}
261101704Smjacob		}
262101704Smjacob	}
263101704Smjacob
264101704Smjacob	/* Set up the memory regions */
265101704Smjacob	/* Allocate kernel virtual memory for the 9x9's Mem0 region */
266101704Smjacob	mpt->pci_reg_id = MEM_MAP_REG;
267101704Smjacob	mpt->pci_reg = bus_alloc_resource(dev, SYS_RES_MEMORY,
268101704Smjacob			&mpt->pci_reg_id, 0, ~0, 0, RF_ACTIVE);
269101704Smjacob	if (mpt->pci_reg == NULL) {
270101704Smjacob		device_printf(dev, "unable to map any ports\n");
271101704Smjacob		goto bad;
272101704Smjacob	}
273101704Smjacob	mpt->pci_st = rman_get_bustag(mpt->pci_reg);
274101704Smjacob	mpt->pci_sh = rman_get_bushandle(mpt->pci_reg);
275101704Smjacob	/*   Get the Physical Address */
276101704Smjacob	mpt->pci_pa = rman_get_start(mpt->pci_reg);
277101704Smjacob
278101704Smjacob	/* Get a handle to the interrupt */
279101704Smjacob	iqd = 0;
280101704Smjacob	mpt->pci_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &iqd, 0, ~0,
281101704Smjacob	    1, RF_ACTIVE | RF_SHAREABLE);
282101704Smjacob	if (mpt->pci_irq == NULL) {
283101704Smjacob		device_printf(dev, "could not allocate interrupt\n");
284101704Smjacob		goto bad;
285101704Smjacob	}
286101704Smjacob
287101704Smjacob	/* Register the interrupt handler */
288101704Smjacob	if (bus_setup_intr(dev, mpt->pci_irq,
289101704Smjacob	    INTR_TYPE_CAM, (void (*)(void *))mpt_intr,
290101704Smjacob	    mpt, &mpt->ih)) {
291101704Smjacob		device_printf(dev, "could not setup interrupt\n");
292101704Smjacob		goto bad;
293101704Smjacob	}
294101704Smjacob
295101704Smjacob	/* Disable interrupts at the part */
296101704Smjacob	mpt_disable_ints(mpt);
297101704Smjacob
298101704Smjacob	/* Allocate dma memory */
299101704Smjacob	if (mpt_dma_mem_alloc(mpt)) {
300101704Smjacob		device_printf(dev, "Could not allocate DMA memory\n");
301101704Smjacob		goto bad;
302101704Smjacob	}
303101704Smjacob
304101704Smjacob	/* Initialize character device */
305101704Smjacob	/*   currently closed */
306101704Smjacob	mpt->open = 0;
307101704Smjacob
308101704Smjacob	/* Save the PCI config register values */
309101704Smjacob	/*   Hard resets are known to screw up the BAR for diagnostic
310101704Smjacob	     memory accesses (Mem1). */
311101704Smjacob	/*   Using Mem1 is known to make the chip stop responding to
312101704Smjacob	     configuration space transfers, so we need to save it now */
313101704Smjacob	mpt_read_config_regs(mpt);
314101704Smjacob
315101704Smjacob	/* Initialize the hardware */
316101704Smjacob	if (mpt->disabled == 0) {
317101704Smjacob		if (mpt_init(mpt, MPT_DB_INIT_HOST) != 0)
318101704Smjacob			goto bad;
319101704Smjacob
320101704Smjacob		/* Attach to CAM */
321101704Smjacob		mpt_cam_attach(mpt);
322101704Smjacob	}
323101704Smjacob
324101704Smjacob	/* Done */
325101704Smjacob	return (0);
326101704Smjacob
327101704Smjacobbad:
328101704Smjacob	mpt_dma_mem_free(mpt);
329101704Smjacob	mpt_free_bus_resources(mpt);
330101704Smjacob
331101704Smjacob	/*
332101704Smjacob	 * but return zero to preserve unit numbering
333101704Smjacob	 */
334101704Smjacob	return (0);
335101704Smjacob}
336101704Smjacob
337101704Smjacob/******************************************************************************
338101704Smjacob * Free bus resources
339101704Smjacob */
340101704Smjacobstatic void
341101704Smjacobmpt_free_bus_resources(struct mpt_softc *mpt)
342101704Smjacob{
343101704Smjacob	if (mpt->ih) {
344101704Smjacob		bus_teardown_intr(mpt->dev, mpt->pci_irq, mpt->ih);
345101704Smjacob		mpt->ih = 0;
346101704Smjacob	}
347101704Smjacob
348101704Smjacob	if (mpt->pci_irq) {
349101704Smjacob		bus_release_resource(mpt->dev, SYS_RES_IRQ, 0, mpt->pci_irq);
350101704Smjacob		mpt->pci_irq = 0;
351101704Smjacob	}
352101704Smjacob
353101704Smjacob	if (mpt->pci_reg) {
354101704Smjacob		bus_release_resource(mpt->dev, SYS_RES_MEMORY, mpt->pci_reg_id,
355101704Smjacob			mpt->pci_reg);
356101704Smjacob		mpt->pci_reg = 0;
357101704Smjacob	}
358101704Smjacob	if (mpt->pci_mem) {
359101704Smjacob		bus_release_resource(mpt->dev, SYS_RES_MEMORY, mpt->pci_mem_id,
360101704Smjacob			mpt->pci_mem);
361101704Smjacob		mpt->pci_mem = 0;
362101704Smjacob	}
363101704Smjacob
364101704Smjacob}
365101704Smjacob
366101704Smjacob
367101704Smjacob/******************************************************************************
368101704Smjacob * Disconnect ourselves from the system.
369101704Smjacob */
370101704Smjacobstatic int
371101704Smjacobmpt_detach(device_t dev)
372101704Smjacob{
373101704Smjacob	struct mpt_softc *mpt;
374101704Smjacob	mpt  = (struct mpt_softc*) device_get_softc(dev);
375101704Smjacob
376101704Smjacob	device_printf(mpt->dev,"mpt_detach!\n");
377101704Smjacob
378101704Smjacob	if (mpt) {
379101704Smjacob		mpt_disable_ints(mpt);
380101704Smjacob		mpt_cam_detach(mpt);
381101704Smjacob		mpt_reset(mpt);
382101704Smjacob		mpt_dma_mem_free(mpt);
383101704Smjacob		mpt_free_bus_resources(mpt);
384101704Smjacob	}
385101704Smjacob	return(0);
386101704Smjacob}
387101704Smjacob
388101704Smjacob
389101704Smjacob/******************************************************************************
390101704Smjacob * Disable the hardware
391101704Smjacob */
392101704Smjacobstatic int
393101704Smjacobmpt_shutdown(device_t dev)
394101704Smjacob{
395101704Smjacob	struct mpt_softc *mpt;
396101704Smjacob	mpt  = (struct mpt_softc*) device_get_softc(dev);
397101704Smjacob
398101704Smjacob	if (mpt) {
399101704Smjacob		mpt_reset(mpt);
400101704Smjacob	}
401101704Smjacob	return(0);
402101704Smjacob}
403101704Smjacob
404101704Smjacob
405101704Smjacobstruct imush {
406101704Smjacob	struct mpt_softc *mpt;
407101704Smjacob	int error;
408101704Smjacob	u_int32_t phys;
409101704Smjacob};
410101704Smjacob
411101704Smjacobstatic void
412101704Smjacobmpt_map_rquest(void *arg, bus_dma_segment_t *segs, int nseg, int error)
413101704Smjacob{
414101704Smjacob	struct imush *imushp = (struct imush *) arg;
415101704Smjacob	imushp->error = error;
416101704Smjacob	imushp->phys = segs->ds_addr;
417101704Smjacob}
418101704Smjacob
419101704Smjacob
420101704Smjacobstatic int
421101704Smjacobmpt_dma_mem_alloc(struct mpt_softc *mpt)
422101704Smjacob{
423101704Smjacob	int i, error;
424101704Smjacob	u_char *vptr;
425101704Smjacob	u_int32_t pptr, end;
426101704Smjacob	struct imush im;
427101704Smjacob	device_t dev = mpt->dev;
428101704Smjacob
429101704Smjacob	/* Check if we alreay have allocated the reply memory */
430101704Smjacob	if (mpt->reply_phys != NULL)
431101704Smjacob		return 0;
432101704Smjacob
433101704Smjacob	/*
434101704Smjacob	 * Create a dma tag for this device
435101704Smjacob	 *
436101704Smjacob	 * Align at page boundaries, limit to 32-bit addressing
437101704Smjacob	 * (The chip supports 64-bit addressing, but this driver doesn't)
438101704Smjacob	 */
439101704Smjacob	if (bus_dma_tag_create(NULL, PAGE_SIZE, 0, BUS_SPACE_MAXADDR_32BIT,
440101704Smjacob	    BUS_SPACE_MAXADDR, NULL, NULL, BUS_SPACE_MAXSIZE_32BIT,
441101704Smjacob	    BUS_SPACE_MAXSIZE_32BIT, BUS_SPACE_UNRESTRICTED, 0,
442101704Smjacob	    &mpt->parent_dmat) != 0) {
443101704Smjacob		device_printf(dev, "cannot create parent dma tag\n");
444101704Smjacob		return (1);
445101704Smjacob	}
446101704Smjacob
447101704Smjacob	/* Create a child tag for reply buffers */
448101704Smjacob	if (bus_dma_tag_create(mpt->parent_dmat, PAGE_SIZE,
449101704Smjacob	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
450101704Smjacob	    NULL, NULL, PAGE_SIZE, 1, BUS_SPACE_MAXSIZE_32BIT, 0,
451101704Smjacob	    &mpt->reply_dmat) != 0) {
452101704Smjacob		device_printf(dev, "cannot create a dma tag for replies\n");
453101704Smjacob		return (1);
454101704Smjacob	}
455101704Smjacob
456101704Smjacob	/* Allocate some DMA accessable memory for replies */
457101704Smjacob	if (bus_dmamem_alloc(mpt->reply_dmat, (void **)&mpt->reply,
458101704Smjacob	    BUS_DMA_NOWAIT, &mpt->reply_dmap) != 0) {
459101704Smjacob		device_printf(dev, "cannot allocate %d bytes of reply memory\n",
460101704Smjacob		     PAGE_SIZE);
461101704Smjacob		return (1);
462101704Smjacob	}
463101704Smjacob
464101704Smjacob	im.mpt = mpt;
465101704Smjacob	im.error = 0;
466101704Smjacob
467101704Smjacob	/* Load and lock it into "bus space" */
468101704Smjacob	bus_dmamap_load(mpt->reply_dmat, mpt->reply_dmap, mpt->reply,
469101704Smjacob	    PAGE_SIZE, mpt_map_rquest, &im, 0);
470101704Smjacob
471101704Smjacob	if (im.error) {
472101704Smjacob		device_printf(dev,
473101704Smjacob		    "error %d loading dma map for DMA reply queue\n", im.error);
474101704Smjacob		return (1);
475101704Smjacob	}
476101704Smjacob	mpt->reply_phys = im.phys;
477101704Smjacob
478101704Smjacob	/* Create a child tag for data buffers */
479101704Smjacob	if (bus_dma_tag_create(mpt->parent_dmat, PAGE_SIZE,
480101704Smjacob	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
481101704Smjacob	    NULL, NULL, MAXBSIZE, MPT_SGL_MAX, BUS_SPACE_MAXSIZE_32BIT, 0,
482101704Smjacob	    &mpt->buffer_dmat) != 0) {
483101704Smjacob		device_printf(dev,
484101704Smjacob		    "cannot create a dma tag for data buffers\n");
485101704Smjacob		return (1);
486101704Smjacob	}
487101704Smjacob
488101704Smjacob	/* Create a child tag for request buffers */
489101704Smjacob	if (bus_dma_tag_create(mpt->parent_dmat, PAGE_SIZE,
490101704Smjacob	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
491101704Smjacob	    NULL, NULL, MPT_REQ_MEM_SIZE, 1, BUS_SPACE_MAXSIZE_32BIT, 0,
492101704Smjacob	    &mpt->request_dmat) != 0) {
493101704Smjacob		device_printf(dev, "cannot create a dma tag for requests\n");
494101704Smjacob		return (1);
495101704Smjacob	}
496101704Smjacob
497101704Smjacob	/* Allocate some DMA accessable memory for requests */
498101704Smjacob	if (bus_dmamem_alloc(mpt->request_dmat, (void **)&mpt->request,
499101704Smjacob	    BUS_DMA_NOWAIT, &mpt->request_dmap) != 0) {
500101704Smjacob		device_printf(dev,
501101704Smjacob		    "cannot allocate %d bytes of request memory\n",
502101704Smjacob		    MPT_REQ_MEM_SIZE);
503101704Smjacob		return (1);
504101704Smjacob	}
505101704Smjacob
506101704Smjacob	im.mpt = mpt;
507101704Smjacob	im.error = 0;
508101704Smjacob
509101704Smjacob	/* Load and lock it into "bus space" */
510101704Smjacob        bus_dmamap_load(mpt->request_dmat, mpt->request_dmap, mpt->request,
511101704Smjacob	    MPT_REQ_MEM_SIZE, mpt_map_rquest, &im, 0);
512101704Smjacob
513101704Smjacob	if (im.error) {
514101704Smjacob		device_printf(dev,
515101704Smjacob		    "error %d loading dma map for DMA request queue\n",
516101704Smjacob		    im.error);
517101704Smjacob		return (1);
518101704Smjacob	}
519101704Smjacob	mpt->request_phys = im.phys;
520101704Smjacob
521101704Smjacob	i = 0;
522101704Smjacob	pptr =  mpt->request_phys;
523101704Smjacob	vptr =  mpt->request;
524101704Smjacob	end = pptr + MPT_REQ_MEM_SIZE;
525101704Smjacob	while(pptr < end) {
526101704Smjacob		request_t *req = &mpt->requests[i];
527101704Smjacob		req->index = i++;
528101704Smjacob
529101704Smjacob		/* Store location of Request Data */
530101704Smjacob		req->req_pbuf = pptr;
531101704Smjacob		req->req_vbuf = vptr;
532101704Smjacob
533101704Smjacob		pptr += MPT_REQUEST_AREA;
534101704Smjacob		vptr += MPT_REQUEST_AREA;
535101704Smjacob
536101704Smjacob		req->sense_pbuf = (pptr - MPT_SENSE_SIZE);
537101704Smjacob		req->sense_vbuf = (vptr - MPT_SENSE_SIZE);
538101704Smjacob
539101704Smjacob		error = bus_dmamap_create(mpt->buffer_dmat, 0, &req->dmap);
540101704Smjacob		if (error) {
541101704Smjacob			device_printf(dev,
542101704Smjacob			     "error %d creating per-cmd DMA maps\n", error);
543101704Smjacob			return (1);
544101704Smjacob		}
545101704Smjacob	}
546101704Smjacob	return (0);
547101704Smjacob}
548101704Smjacob
549101704Smjacob
550101704Smjacob
551101704Smjacob/* Deallocate memory that was allocated by mpt_dma_mem_alloc
552101704Smjacob */
553101704Smjacobstatic void
554101704Smjacobmpt_dma_mem_free(struct mpt_softc *mpt)
555101704Smjacob{
556101704Smjacob	int i;
557101704Smjacob
558101704Smjacob        /* Make sure we aren't double destroying */
559101704Smjacob        if (mpt->reply_dmat == 0) {
560101704Smjacob		if (mpt->verbose)
561101704Smjacob			device_printf(mpt->dev,"Already released dma memory\n");
562101704Smjacob		return;
563101704Smjacob        }
564101704Smjacob
565101704Smjacob	for (i = 0; i < MPT_MAX_REQUESTS; i++) {
566101704Smjacob		bus_dmamap_destroy(mpt->buffer_dmat, mpt->requests[i].dmap);
567101704Smjacob	}
568101704Smjacob	bus_dmamap_unload(mpt->request_dmat, mpt->request_dmap);
569101704Smjacob	bus_dmamem_free(mpt->request_dmat, mpt->request, mpt->request_dmap);
570101704Smjacob	bus_dma_tag_destroy(mpt->request_dmat);
571101704Smjacob	bus_dma_tag_destroy(mpt->buffer_dmat);
572101704Smjacob	bus_dmamap_unload(mpt->reply_dmat, mpt->reply_dmap);
573101704Smjacob	bus_dmamem_free(mpt->reply_dmat, mpt->reply, mpt->reply_dmap);
574101704Smjacob	bus_dma_tag_destroy(mpt->reply_dmat);
575101704Smjacob	bus_dma_tag_destroy(mpt->parent_dmat);
576101704Smjacob	mpt->reply_dmat = 0;
577101704Smjacob
578101704Smjacob}
579101704Smjacob
580101704Smjacob
581101704Smjacob
582101704Smjacob/* Reads modifiable (via PCI transactions) config registers */
583101704Smjacobstatic void
584101704Smjacobmpt_read_config_regs(struct mpt_softc *mpt)
585101704Smjacob{
586101704Smjacob	mpt->pci_cfg.Command = pci_read_config(mpt->dev, PCIR_COMMAND, 2);
587101704Smjacob	mpt->pci_cfg.LatencyTimer_LineSize =
588101704Smjacob	    pci_read_config(mpt->dev, PCIR_CACHELNSZ, 2);
589101704Smjacob	mpt->pci_cfg.IO_BAR = pci_read_config(mpt->dev, PCIR_MAPS, 4);
590101704Smjacob	mpt->pci_cfg.Mem0_BAR[0] = pci_read_config(mpt->dev, PCIR_MAPS+0x4, 4);
591101704Smjacob	mpt->pci_cfg.Mem0_BAR[1] = pci_read_config(mpt->dev, PCIR_MAPS+0x8, 4);
592101704Smjacob	mpt->pci_cfg.Mem1_BAR[0] = pci_read_config(mpt->dev, PCIR_MAPS+0xC, 4);
593101704Smjacob	mpt->pci_cfg.Mem1_BAR[1] = pci_read_config(mpt->dev, PCIR_MAPS+0x10, 4);
594101704Smjacob	mpt->pci_cfg.ROM_BAR = pci_read_config(mpt->dev, PCIR_BIOS, 4);
595101704Smjacob	mpt->pci_cfg.IntLine = pci_read_config(mpt->dev, PCIR_INTLINE, 1);
596101704Smjacob	mpt->pci_cfg.PMCSR = pci_read_config(mpt->dev, 0x44, 4);
597101704Smjacob}
598101704Smjacob
599101704Smjacob/* Sets modifiable config registers */
600101704Smjacobvoid
601101704Smjacobmpt_set_config_regs(struct mpt_softc *mpt)
602101704Smjacob{
603101704Smjacob	u_int32_t val;
604101704Smjacob
605101704Smjacob#define MPT_CHECK(reg, offset, size)					\
606101704Smjacob	val = pci_read_config(mpt->dev, offset, size);			\
607101704Smjacob	if (mpt->pci_cfg.reg != val) {					\
608101704Smjacob		device_printf(mpt->dev,					\
609101704Smjacob		    "Restoring " #reg " to 0x%X from 0x%X\n",		\
610101704Smjacob		    mpt->pci_cfg.reg, val);				\
611101704Smjacob	}
612101704Smjacob
613101704Smjacob	if (mpt->verbose) {
614101704Smjacob		MPT_CHECK(Command, PCIR_COMMAND, 2);
615101704Smjacob		MPT_CHECK(LatencyTimer_LineSize, PCIR_CACHELNSZ, 2);
616101704Smjacob		MPT_CHECK(IO_BAR, PCIR_MAPS, 4);
617101704Smjacob		MPT_CHECK(Mem0_BAR[0], PCIR_MAPS+0x4, 4);
618101704Smjacob		MPT_CHECK(Mem0_BAR[1], PCIR_MAPS+0x8, 4);
619101704Smjacob		MPT_CHECK(Mem1_BAR[0], PCIR_MAPS+0xC, 4);
620101704Smjacob		MPT_CHECK(Mem1_BAR[1], PCIR_MAPS+0x10, 4);
621101704Smjacob		MPT_CHECK(ROM_BAR, PCIR_BIOS, 4);
622101704Smjacob		MPT_CHECK(IntLine, PCIR_INTLINE, 1);
623101704Smjacob		MPT_CHECK(PMCSR, 0x44, 4);
624101704Smjacob	}
625101704Smjacob#undef MPT_CHECK
626101704Smjacob
627101704Smjacob	pci_write_config(mpt->dev, PCIR_COMMAND, mpt->pci_cfg.Command, 2);
628101704Smjacob	pci_write_config(mpt->dev, PCIR_CACHELNSZ,
629101704Smjacob	    mpt->pci_cfg.LatencyTimer_LineSize, 2);
630101704Smjacob	pci_write_config(mpt->dev, PCIR_MAPS, mpt->pci_cfg.IO_BAR, 4);
631101704Smjacob	pci_write_config(mpt->dev, PCIR_MAPS+0x4, mpt->pci_cfg.Mem0_BAR[0], 4);
632101704Smjacob	pci_write_config(mpt->dev, PCIR_MAPS+0x8, mpt->pci_cfg.Mem0_BAR[1], 4);
633101704Smjacob	pci_write_config(mpt->dev, PCIR_MAPS+0xC, mpt->pci_cfg.Mem1_BAR[0], 4);
634101704Smjacob	pci_write_config(mpt->dev, PCIR_MAPS+0x10, mpt->pci_cfg.Mem1_BAR[1], 4);
635101704Smjacob	pci_write_config(mpt->dev, PCIR_BIOS, mpt->pci_cfg.ROM_BAR, 4);
636101704Smjacob	pci_write_config(mpt->dev, PCIR_INTLINE, mpt->pci_cfg.IntLine, 1);
637101704Smjacob	pci_write_config(mpt->dev, 0x44, mpt->pci_cfg.PMCSR, 4);
638101704Smjacob}
639