isp_sbus.c revision 291528
1/*-
2 * Copyright (c) 1997-2006 by Matthew Jacob
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice immediately at the beginning of the file, without modification,
10 *    this list of conditions, and the following disclaimer.
11 * 2. The name of the author may not be used to endorse or promote products
12 *    derived from this software without specific prior written permission.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26/*
27 * SBus specific probe and attach routines for Qlogic ISP SCSI adapters.
28 * FreeBSD Version.
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: stable/10/sys/dev/isp/isp_sbus.c 291528 2015-11-30 21:55:35Z mav $");
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/linker.h>
37#include <sys/firmware.h>
38#include <sys/bus.h>
39#include <sys/kernel.h>
40#include <sys/module.h>
41#include <sys/resource.h>
42
43#include <dev/ofw/ofw_bus.h>
44#include <dev/ofw/openfirm.h>
45
46#include <machine/bus.h>
47#include <machine/ofw_machdep.h>
48#include <machine/resource.h>
49#include <sys/rman.h>
50#include <sparc64/sbus/sbusvar.h>
51
52#include <dev/isp/isp_freebsd.h>
53
54static uint32_t isp_sbus_rd_reg(ispsoftc_t *, int);
55static void isp_sbus_wr_reg(ispsoftc_t *, int, uint32_t);
56static int isp_sbus_rd_isr(ispsoftc_t *, uint16_t *, uint16_t *, uint16_t *);
57static int isp_sbus_mbxdma(ispsoftc_t *);
58static int isp_sbus_dmasetup(ispsoftc_t *, XS_T *, void *);
59
60
61static void isp_sbus_reset0(ispsoftc_t *);
62static void isp_sbus_reset1(ispsoftc_t *);
63static void isp_sbus_dumpregs(ispsoftc_t *, const char *);
64
65static struct ispmdvec mdvec = {
66	isp_sbus_rd_isr,
67	isp_sbus_rd_reg,
68	isp_sbus_wr_reg,
69	isp_sbus_mbxdma,
70	isp_sbus_dmasetup,
71	isp_common_dmateardown,
72	isp_sbus_reset0,
73	isp_sbus_reset1,
74	isp_sbus_dumpregs,
75	NULL,
76	BIU_BURST_ENABLE|BIU_PCI_CONF1_FIFO_64
77};
78
79static int isp_sbus_probe (device_t);
80static int isp_sbus_attach (device_t);
81static int isp_sbus_detach (device_t);
82
83
84#define	ISP_SBD(isp)	((struct isp_sbussoftc *)isp)->sbus_dev
85struct isp_sbussoftc {
86	ispsoftc_t			sbus_isp;
87	device_t			sbus_dev;
88	struct resource *		regs;
89	void *				irq;
90	int				iqd;
91	int				rgd;
92	void *				ih;
93	int16_t				sbus_poff[_NREG_BLKS];
94	sdparam				sbus_param;
95	struct isp_spi			sbus_spi;
96	struct ispmdvec			sbus_mdvec;
97};
98
99
100static device_method_t isp_sbus_methods[] = {
101	/* Device interface */
102	DEVMETHOD(device_probe,		isp_sbus_probe),
103	DEVMETHOD(device_attach,	isp_sbus_attach),
104	DEVMETHOD(device_detach,	isp_sbus_detach),
105	{ 0, 0 }
106};
107
108static driver_t isp_sbus_driver = {
109	"isp", isp_sbus_methods, sizeof (struct isp_sbussoftc)
110};
111static devclass_t isp_devclass;
112DRIVER_MODULE(isp, sbus, isp_sbus_driver, isp_devclass, 0, 0);
113MODULE_DEPEND(isp, cam, 1, 1, 1);
114MODULE_DEPEND(isp, firmware, 1, 1, 1);
115
116static int
117isp_sbus_probe(device_t dev)
118{
119	int found = 0;
120	const char *name = ofw_bus_get_name(dev);
121	if (strcmp(name, "SUNW,isp") == 0 ||
122	    strcmp(name, "QLGC,isp") == 0 ||
123	    strcmp(name, "ptisp") == 0 ||
124	    strcmp(name, "PTI,ptisp") == 0) {
125		found++;
126	}
127	if (!found)
128		return (ENXIO);
129
130	if (isp_announced == 0 && bootverbose) {
131		printf("Qlogic ISP Driver, FreeBSD Version %d.%d, "
132		    "Core Version %d.%d\n",
133		    ISP_PLATFORM_VERSION_MAJOR, ISP_PLATFORM_VERSION_MINOR,
134		    ISP_CORE_VERSION_MAJOR, ISP_CORE_VERSION_MINOR);
135		isp_announced++;
136	}
137	return (0);
138}
139
140static int
141isp_sbus_attach(device_t dev)
142{
143	int tval, isp_debug, role, ispburst, default_id;
144	struct isp_sbussoftc *sbs;
145	ispsoftc_t *isp = NULL;
146	int locksetup = 0;
147	int ints_setup = 0;
148
149	sbs = device_get_softc(dev);
150	if (sbs == NULL) {
151		device_printf(dev, "cannot get softc\n");
152		return (ENOMEM);
153	}
154
155	sbs->sbus_dev = dev;
156	sbs->sbus_mdvec = mdvec;
157
158	/*
159	 * Figure out if we're supposed to skip this one.
160	 * If we are, we actually go to ISP_ROLE_NONE.
161	 */
162
163	tval = 0;
164	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
165	    "disable", &tval) == 0 && tval) {
166		device_printf(dev, "device is disabled\n");
167		/* but return 0 so the !$)$)*!$*) unit isn't reused */
168		return (0);
169	}
170
171	role = 0;
172	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
173	    "role", &role) == 0 &&
174	    ((role & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) == 0)) {
175		device_printf(dev, "setting role to 0x%x\n", role);
176	} else {
177		role = ISP_DEFAULT_ROLES;
178	}
179
180	sbs->irq = sbs->regs = NULL;
181	sbs->rgd = sbs->iqd = 0;
182
183	sbs->regs = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sbs->rgd,
184	    RF_ACTIVE);
185	if (sbs->regs == NULL) {
186		device_printf(dev, "unable to map registers\n");
187		goto bad;
188	}
189
190	sbs->sbus_poff[BIU_BLOCK >> _BLK_REG_SHFT] = BIU_REGS_OFF;
191	sbs->sbus_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = SBUS_MBOX_REGS_OFF;
192	sbs->sbus_poff[SXP_BLOCK >> _BLK_REG_SHFT] = SBUS_SXP_REGS_OFF;
193	sbs->sbus_poff[RISC_BLOCK >> _BLK_REG_SHFT] = SBUS_RISC_REGS_OFF;
194	sbs->sbus_poff[DMA_BLOCK >> _BLK_REG_SHFT] = DMA_REGS_OFF;
195	isp = &sbs->sbus_isp;
196	isp->isp_bus_tag = rman_get_bustag(sbs->regs);
197	isp->isp_bus_handle = rman_get_bushandle(sbs->regs);
198	isp->isp_mdvec = &sbs->sbus_mdvec;
199	isp->isp_bustype = ISP_BT_SBUS;
200	isp->isp_type = ISP_HA_SCSI_UNKNOWN;
201	isp->isp_param = &sbs->sbus_param;
202	isp->isp_osinfo.pc.ptr = &sbs->sbus_spi;
203	isp->isp_revision = 0;	/* XXX */
204	isp->isp_dev = dev;
205	isp->isp_nchan = 1;
206	if (IS_FC(isp))
207		ISP_FC_PC(isp, 0)->def_role = role;
208
209	/*
210	 * Get the clock frequency and convert it from HZ to MHz,
211	 * rounding up. This defaults to 25MHz if there isn't a
212	 * device specific one in the OFW device tree.
213	 */
214	sbs->sbus_mdvec.dv_clock = (sbus_get_clockfreq(dev) + 500000)/1000000;
215
216	/*
217	 * Now figure out what the proper burst sizes, etc., to use.
218	 * Unfortunately, there is no ddi_dma_burstsizes here which
219	 * walks up the tree finding the limiting burst size node (if
220	 * any). We just use what's here for isp.
221	 */
222	ispburst = sbus_get_burstsz(dev);
223	if (ispburst == 0) {
224		ispburst = SBUS_BURST_32 - 1;
225	}
226	sbs->sbus_mdvec.dv_conf1 =  0;
227	if (ispburst & (1 << 5)) {
228		sbs->sbus_mdvec.dv_conf1 = BIU_SBUS_CONF1_FIFO_32;
229	} else if (ispburst & (1 << 4)) {
230		sbs->sbus_mdvec.dv_conf1 = BIU_SBUS_CONF1_FIFO_16;
231	} else if (ispburst & (1 << 3)) {
232		sbs->sbus_mdvec.dv_conf1 =
233		    BIU_SBUS_CONF1_BURST8 | BIU_SBUS_CONF1_FIFO_8;
234	}
235	if (sbs->sbus_mdvec.dv_conf1) {
236		sbs->sbus_mdvec.dv_conf1 |= BIU_BURST_ENABLE;
237	}
238
239	/*
240	 * We don't trust NVRAM on SBus cards
241	 */
242	isp->isp_confopts |= ISP_CFG_NONVRAM;
243
244	/*
245	 * Mark things if we're a PTI SBus adapter.
246	 */
247	if (strcmp("PTI,ptisp", ofw_bus_get_name(dev)) == 0 ||
248	    strcmp("ptisp", ofw_bus_get_name(dev)) == 0) {
249		SDPARAM(isp, 0)->isp_ptisp = 1;
250	}
251
252	isp->isp_osinfo.fw = firmware_get("isp_1000");
253	if (isp->isp_osinfo.fw) {
254		union {
255			const void *cp;
256			uint16_t *sp;
257		} stupid;
258		stupid.cp = isp->isp_osinfo.fw->data;
259		isp->isp_mdvec->dv_ispfw = stupid.sp;
260	}
261
262	tval = 0;
263        if (resource_int_value(device_get_name(dev), device_get_unit(dev),
264            "fwload_disable", &tval) == 0 && tval != 0) {
265		isp->isp_confopts |= ISP_CFG_NORELOAD;
266	}
267
268	default_id = -1;
269	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
270            "iid", &tval) == 0) {
271		default_id = tval;
272		isp->isp_confopts |= ISP_CFG_OWNLOOPID;
273	}
274	if (default_id == -1) {
275		default_id = OF_getscsinitid(dev);
276	}
277	ISP_SPI_PC(isp, 0)->iid = default_id;
278
279	isp_debug = 0;
280        (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
281            "debug", &isp_debug);
282
283	/* Make sure the lock is set up. */
284	mtx_init(&isp->isp_osinfo.lock, "isp", NULL, MTX_DEF);
285	locksetup++;
286
287	sbs->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sbs->iqd,
288	    RF_ACTIVE | RF_SHAREABLE);
289	if (sbs->irq == NULL) {
290		device_printf(dev, "could not allocate interrupt\n");
291		goto bad;
292	}
293
294	if (isp_setup_intr(dev, sbs->irq, ISP_IFLAGS, NULL, isp_platform_intr,
295	    isp, &sbs->ih)) {
296		device_printf(dev, "could not setup interrupt\n");
297		goto bad;
298	}
299	ints_setup++;
300
301	/*
302	 * Set up logging levels.
303	 */
304	if (isp_debug) {
305		isp->isp_dblev = isp_debug;
306	} else {
307		isp->isp_dblev = ISP_LOGWARN|ISP_LOGERR;
308	}
309	if (bootverbose) {
310		isp->isp_dblev |= ISP_LOGCONFIG|ISP_LOGINFO;
311	}
312
313	/*
314	 * Make sure we're in reset state.
315	 */
316	ISP_LOCK(isp);
317	if (isp_reinit(isp, 1) != 0) {
318		isp_uninit(isp);
319		ISP_UNLOCK(isp);
320		goto bad;
321	}
322	ISP_UNLOCK(isp);
323	if (isp_attach(isp)) {
324		ISP_LOCK(isp);
325		isp_uninit(isp);
326		ISP_UNLOCK(isp);
327		goto bad;
328	}
329	return (0);
330
331bad:
332
333	if (sbs && ints_setup) {
334		(void) bus_teardown_intr(dev, sbs->irq, sbs->ih);
335	}
336
337	if (sbs && sbs->irq) {
338		bus_release_resource(dev, SYS_RES_IRQ, sbs->iqd, sbs->irq);
339	}
340
341	if (locksetup && isp) {
342		mtx_destroy(&isp->isp_osinfo.lock);
343	}
344
345	if (sbs->regs) {
346		(void) bus_release_resource(dev, SYS_RES_MEMORY, sbs->rgd,
347		    sbs->regs);
348	}
349	return (ENXIO);
350}
351
352static int
353isp_sbus_detach(device_t dev)
354{
355	struct isp_sbussoftc *sbs;
356	ispsoftc_t *isp;
357	int status;
358
359	sbs = device_get_softc(dev);
360	if (sbs == NULL) {
361		return (ENXIO);
362	}
363	isp = (ispsoftc_t *) sbs;
364	status = isp_detach(isp);
365	if (status)
366		return (status);
367	ISP_LOCK(isp);
368	isp_uninit(isp);
369	if (sbs->ih) {
370		(void) bus_teardown_intr(dev, sbs->irq, sbs->ih);
371	}
372	ISP_UNLOCK(isp);
373	mtx_destroy(&isp->isp_osinfo.lock);
374	(void) bus_release_resource(dev, SYS_RES_IRQ, sbs->iqd, sbs->irq);
375	(void) bus_release_resource(dev, SYS_RES_MEMORY, sbs->rgd, sbs->regs);
376	return (0);
377}
378
379#define	IspVirt2Off(a, x)	\
380	(((struct isp_sbussoftc *)a)->sbus_poff[((x) & _BLK_REG_MASK) >> \
381	_BLK_REG_SHFT] + ((x) & 0xff))
382
383#define	BXR2(sbc, off)		\
384	bus_space_read_2(isp->isp_bus_tag, isp->isp_bus_handle, off)
385
386static int
387isp_sbus_rd_isr(ispsoftc_t *isp, uint16_t *isrp, uint16_t *semap, uint16_t *info)
388{
389	uint16_t isr, sema;
390
391	isr = BXR2(sbc, IspVirt2Off(isp, BIU_ISR));
392	sema = BXR2(sbc, IspVirt2Off(isp, BIU_SEMA));
393	isp_prt(isp, ISP_LOGDEBUG3, "ISR 0x%x SEMA 0x%x", isr, sema);
394	isr &= INT_PENDING_MASK(isp);
395	sema &= BIU_SEMA_LOCK;
396	if (isr == 0 && sema == 0) {
397		return (0);
398	}
399	*isrp = isr;
400	if ((*semap = sema) != 0)
401		*info = BXR2(sbc, IspVirt2Off(isp, OUTMAILBOX0));
402	return (1);
403}
404
405static uint32_t
406isp_sbus_rd_reg(ispsoftc_t *isp, int regoff)
407{
408	uint16_t rval;
409	struct isp_sbussoftc *sbs = (struct isp_sbussoftc *) isp;
410	int offset = sbs->sbus_poff[(regoff & _BLK_REG_MASK) >> _BLK_REG_SHFT];
411	offset += (regoff & 0xff);
412	rval = bus_space_read_2(isp->isp_bus_tag, isp->isp_bus_handle, offset);
413	isp_prt(isp, ISP_LOGDEBUG3,
414	    "isp_sbus_rd_reg(off %x) = %x", regoff, rval);
415	return (rval);
416}
417
418static void
419isp_sbus_wr_reg(ispsoftc_t *isp, int regoff, uint32_t val)
420{
421	struct isp_sbussoftc *sbs = (struct isp_sbussoftc *) isp;
422	int offset = sbs->sbus_poff[(regoff & _BLK_REG_MASK) >> _BLK_REG_SHFT];
423	offset += (regoff & 0xff);
424	isp_prt(isp, ISP_LOGDEBUG3,
425	    "isp_sbus_wr_reg(off %x) = %x", regoff, val);
426	bus_space_write_2(isp->isp_bus_tag, isp->isp_bus_handle, offset, val);
427	MEMORYBARRIER(isp, SYNC_REG, offset, 2, -1);
428}
429
430struct imush {
431	ispsoftc_t *isp;
432	int error;
433};
434
435static void imc(void *, bus_dma_segment_t *, int, int);
436
437static void
438imc(void *arg, bus_dma_segment_t *segs, int nseg, int error)
439{
440	struct imush *imushp = (struct imush *) arg;
441	if (error) {
442		imushp->error = error;
443	} else {
444		ispsoftc_t *isp =imushp->isp;
445		bus_addr_t addr = segs->ds_addr;
446
447		isp->isp_rquest_dma = addr;
448		addr += ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
449		isp->isp_result_dma = addr;
450	}
451}
452
453static int
454isp_sbus_mbxdma(ispsoftc_t *isp)
455{
456	caddr_t base;
457	uint32_t len;
458	int i, error;
459	struct imush im;
460
461	/*
462	 * Already been here? If so, leave...
463	 */
464	if (isp->isp_rquest) {
465		return (0);
466	}
467
468	ISP_UNLOCK(isp);
469
470	len = sizeof (struct isp_pcmd) * isp->isp_maxcmds;
471	isp->isp_osinfo.pcmd_pool = (struct isp_pcmd *)
472	    malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
473	if (isp->isp_osinfo.pcmd_pool == NULL) {
474		isp_prt(isp, ISP_LOGERR, "cannot alloc pcmd pool");
475		ISP_LOCK(isp);
476		return (1);
477	}
478
479	len = sizeof (isp_hdl_t *) * isp->isp_maxcmds;
480	isp->isp_xflist = (isp_hdl_t *) malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
481	if (isp->isp_xflist == NULL) {
482		isp_prt(isp, ISP_LOGERR, "cannot alloc xflist array");
483		ISP_LOCK(isp);
484		return (1);
485	}
486	for (len = 0; len < isp->isp_maxcmds - 1; len++) {
487		isp->isp_xflist[len].cmd = &isp->isp_xflist[len+1];
488	}
489	isp->isp_xffree = isp->isp_xflist;
490	len = sizeof (bus_dmamap_t) * isp->isp_maxcmds;
491
492	if (isp_dma_tag_create(BUS_DMA_ROOTARG(ISP_SBD(isp)), 1,
493	    BUS_SPACE_MAXADDR_24BIT+1, BUS_SPACE_MAXADDR_32BIT,
494	    BUS_SPACE_MAXADDR_32BIT, NULL, NULL, BUS_SPACE_MAXSIZE_32BIT,
495	    ISP_NSEG_MAX, BUS_SPACE_MAXADDR_24BIT, 0, &isp->isp_osinfo.dmat)) {
496		isp_prt(isp, ISP_LOGERR, "could not create master dma tag");
497		free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
498		free(isp->isp_xflist, M_DEVBUF);
499		ISP_LOCK(isp);
500		return(1);
501	}
502
503	/*
504	 * Allocate and map the request, result queues, plus FC scratch area.
505	 */
506	len = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
507	len += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
508
509	if (isp_dma_tag_create(isp->isp_osinfo.dmat, QENTRY_LEN,
510	    BUS_SPACE_MAXADDR_24BIT+1, BUS_SPACE_MAXADDR_32BIT,
511	    BUS_SPACE_MAXADDR_32BIT, NULL, NULL, len, 1,
512	    BUS_SPACE_MAXADDR_24BIT, 0, &isp->isp_osinfo.cdmat)) {
513		isp_prt(isp, ISP_LOGERR,
514		    "cannot create a dma tag for control spaces");
515		free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
516		free(isp->isp_xflist, M_DEVBUF);
517		ISP_LOCK(isp);
518		return (1);
519	}
520
521	if (bus_dmamem_alloc(isp->isp_osinfo.cdmat, (void **)&base, BUS_DMA_NOWAIT | BUS_DMA_COHERENT,
522	    &isp->isp_osinfo.cdmap) != 0) {
523		isp_prt(isp, ISP_LOGERR,
524		    "cannot allocate %d bytes of CCB memory", len);
525		bus_dma_tag_destroy(isp->isp_osinfo.cdmat);
526		free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
527		free(isp->isp_xflist, M_DEVBUF);
528		ISP_LOCK(isp);
529		return (1);
530	}
531
532	for (i = 0; i < isp->isp_maxcmds; i++) {
533		struct isp_pcmd *pcmd = &isp->isp_osinfo.pcmd_pool[i];
534		error = bus_dmamap_create(isp->isp_osinfo.dmat, 0, &pcmd->dmap);
535		if (error) {
536			isp_prt(isp, ISP_LOGERR,
537			    "error %d creating per-cmd DMA maps", error);
538			while (--i >= 0) {
539				bus_dmamap_destroy(isp->isp_osinfo.dmat,
540				    isp->isp_osinfo.pcmd_pool[i].dmap);
541			}
542			goto bad;
543		}
544		callout_init_mtx(&pcmd->wdog, &isp->isp_osinfo.lock, 0);
545		if (i == isp->isp_maxcmds-1) {
546			pcmd->next = NULL;
547		} else {
548			pcmd->next = &isp->isp_osinfo.pcmd_pool[i+1];
549		}
550	}
551	isp->isp_osinfo.pcmd_free = &isp->isp_osinfo.pcmd_pool[0];
552
553	im.isp = isp;
554	im.error = 0;
555	bus_dmamap_load(isp->isp_osinfo.cdmat, isp->isp_osinfo.cdmap, base, len, imc, &im, 0);
556	if (im.error) {
557		isp_prt(isp, ISP_LOGERR,
558		    "error %d loading dma map for control areas", im.error);
559		goto bad;
560	}
561
562	isp->isp_rquest = base;
563	base += ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
564	isp->isp_result = base;
565	ISP_LOCK(isp);
566	return (0);
567
568bad:
569	bus_dmamem_free(isp->isp_osinfo.cdmat, base, isp->isp_osinfo.cdmap);
570	bus_dma_tag_destroy(isp->isp_osinfo.cdmat);
571	free(isp->isp_xflist, M_DEVBUF);
572	free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
573	isp->isp_rquest = NULL;
574	ISP_LOCK(isp);
575	return (1);
576}
577
578typedef struct {
579	ispsoftc_t *isp;
580	void *cmd_token;
581	void *rq;	/* original request */
582	int error;
583	bus_size_t mapsize;
584} mush_t;
585
586#define	MUSHERR_NOQENTRIES	-2
587
588static void dma2(void *, bus_dma_segment_t *, int, int);
589
590static void
591dma2(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
592{
593	mush_t *mp;
594	ispsoftc_t *isp;
595	struct ccb_scsiio *csio;
596	isp_ddir_t ddir;
597	ispreq_t *rq;
598
599	mp = (mush_t *) arg;
600	if (error) {
601		mp->error = error;
602		return;
603	}
604	csio = mp->cmd_token;
605	isp = mp->isp;
606	rq = mp->rq;
607	if (nseg) {
608		if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
609			bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_PREREAD);
610			ddir = ISP_FROM_DEVICE;
611		} else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
612			bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_PREWRITE);
613			ddir = ISP_TO_DEVICE;
614		} else {
615			ddir = ISP_NOXFR;
616		}
617	} else {
618		dm_segs = NULL;
619		nseg = 0;
620		ddir = ISP_NOXFR;
621	}
622
623	if (isp_send_cmd(isp, rq, dm_segs, nseg, XS_XFRLEN(csio), ddir, NULL) != CMD_QUEUED) {
624		mp->error = MUSHERR_NOQENTRIES;
625	}
626}
627
628static int
629isp_sbus_dmasetup(ispsoftc_t *isp, struct ccb_scsiio *csio, void *ff)
630{
631	mush_t mush, *mp;
632	void (*eptr)(void *, bus_dma_segment_t *, int, int);
633	int error;
634
635	mp = &mush;
636	mp->isp = isp;
637	mp->cmd_token = csio;
638	mp->rq = ff;
639	mp->error = 0;
640	mp->mapsize = 0;
641
642	eptr = dma2;
643
644	error = bus_dmamap_load_ccb(isp->isp_osinfo.dmat,
645	    PISP_PCMD(csio)->dmap, (union ccb *)csio, eptr, mp, 0);
646	if (error == EINPROGRESS) {
647		bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
648		mp->error = EINVAL;
649		isp_prt(isp, ISP_LOGERR,
650		    "deferred dma allocation not supported");
651	} else if (error && mp->error == 0) {
652#ifdef	DIAGNOSTIC
653		isp_prt(isp, ISP_LOGERR, "error %d in dma mapping code", error);
654#endif
655		mp->error = error;
656	}
657	if (mp->error) {
658		int retval = CMD_COMPLETE;
659		if (mp->error == MUSHERR_NOQENTRIES) {
660			retval = CMD_EAGAIN;
661		} else if (mp->error == EFBIG) {
662			XS_SETERR(csio, CAM_REQ_TOO_BIG);
663		} else if (mp->error == EINVAL) {
664			XS_SETERR(csio, CAM_REQ_INVALID);
665		} else {
666			XS_SETERR(csio, CAM_UNREC_HBA_ERROR);
667		}
668		return (retval);
669	}
670	return (CMD_QUEUED);
671}
672
673static void
674isp_sbus_reset0(ispsoftc_t *isp)
675{
676	ISP_DISABLE_INTS(isp);
677}
678
679static void
680isp_sbus_reset1(ispsoftc_t *isp)
681{
682	ISP_ENABLE_INTS(isp);
683}
684
685static void
686isp_sbus_dumpregs(ispsoftc_t *isp, const char *msg)
687{
688	if (msg)
689		printf("%s: %s\n", device_get_nameunit(isp->isp_dev), msg);
690	else
691		printf("%s:\n", device_get_nameunit(isp->isp_dev));
692	printf("    biu_conf1=%x", ISP_READ(isp, BIU_CONF1));
693	printf(" biu_icr=%x biu_isr=%x biu_sema=%x ", ISP_READ(isp, BIU_ICR),
694	    ISP_READ(isp, BIU_ISR), ISP_READ(isp, BIU_SEMA));
695	printf("risc_hccr=%x\n", ISP_READ(isp, HCCR));
696
697
698	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
699	printf("    cdma_conf=%x cdma_sts=%x cdma_fifostat=%x\n",
700		ISP_READ(isp, CDMA_CONF), ISP_READ(isp, CDMA_STATUS),
701		ISP_READ(isp, CDMA_FIFO_STS));
702	printf("    ddma_conf=%x ddma_sts=%x ddma_fifostat=%x\n",
703		ISP_READ(isp, DDMA_CONF), ISP_READ(isp, DDMA_STATUS),
704		ISP_READ(isp, DDMA_FIFO_STS));
705	printf("    sxp_int=%x sxp_gross=%x sxp(scsi_ctrl)=%x\n",
706		ISP_READ(isp, SXP_INTERRUPT),
707		ISP_READ(isp, SXP_GROSS_ERR),
708		ISP_READ(isp, SXP_PINS_CTRL));
709	ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
710	printf("    mbox regs: %x %x %x %x %x\n",
711	    ISP_READ(isp, OUTMAILBOX0), ISP_READ(isp, OUTMAILBOX1),
712	    ISP_READ(isp, OUTMAILBOX2), ISP_READ(isp, OUTMAILBOX3),
713	    ISP_READ(isp, OUTMAILBOX4));
714}
715