xref: /freebsd-12.1/sys/dev/isp/isp_sbus.c (revision bed7dbd1)
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$");
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 
54 static uint32_t isp_sbus_rd_reg(ispsoftc_t *, int);
55 static void isp_sbus_wr_reg(ispsoftc_t *, int, uint32_t);
56 static int isp_sbus_rd_isr(ispsoftc_t *, uint32_t *, uint16_t *, uint16_t *);
57 static int isp_sbus_mbxdma(ispsoftc_t *);
58 static int isp_sbus_dmasetup(ispsoftc_t *, XS_T *, void *);
59 
60 
61 static void isp_sbus_reset0(ispsoftc_t *);
62 static void isp_sbus_reset1(ispsoftc_t *);
63 static void isp_sbus_dumpregs(ispsoftc_t *, const char *);
64 
65 static 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 
79 static int isp_sbus_probe (device_t);
80 static int isp_sbus_attach (device_t);
81 
82 
83 #define	ISP_SBD(isp)	((struct isp_sbussoftc *)isp)->sbus_dev
84 struct isp_sbussoftc {
85 	ispsoftc_t			sbus_isp;
86 	device_t			sbus_dev;
87 	struct resource *		sbus_reg;
88 	void *				ih;
89 	int16_t				sbus_poff[_NREG_BLKS];
90 	sdparam				sbus_param;
91 	struct isp_spi			sbus_spi;
92 	struct ispmdvec			sbus_mdvec;
93 	struct resource *		sbus_ires;
94 };
95 
96 
97 static device_method_t isp_sbus_methods[] = {
98 	/* Device interface */
99 	DEVMETHOD(device_probe,		isp_sbus_probe),
100 	DEVMETHOD(device_attach,	isp_sbus_attach),
101 	{ 0, 0 }
102 };
103 
104 static driver_t isp_sbus_driver = {
105 	"isp", isp_sbus_methods, sizeof (struct isp_sbussoftc)
106 };
107 static devclass_t isp_devclass;
108 DRIVER_MODULE(isp, sbus, isp_sbus_driver, isp_devclass, 0, 0);
109 
110 static int
111 isp_sbus_probe(device_t dev)
112 {
113 	int found = 0;
114 	const char *name = ofw_bus_get_name(dev);
115 	if (strcmp(name, "SUNW,isp") == 0 ||
116 	    strcmp(name, "QLGC,isp") == 0 ||
117 	    strcmp(name, "ptisp") == 0 ||
118 	    strcmp(name, "PTI,ptisp") == 0) {
119 		found++;
120 	}
121 	if (!found)
122 		return (ENXIO);
123 
124 	if (isp_announced == 0 && bootverbose) {
125 		printf("Qlogic ISP Driver, FreeBSD Version %d.%d, "
126 		    "Core Version %d.%d\n",
127 		    ISP_PLATFORM_VERSION_MAJOR, ISP_PLATFORM_VERSION_MINOR,
128 		    ISP_CORE_VERSION_MAJOR, ISP_CORE_VERSION_MINOR);
129 		isp_announced++;
130 	}
131 	return (0);
132 }
133 
134 static int
135 isp_sbus_attach(device_t dev)
136 {
137 	struct resource *regs;
138 	int tval, iqd, isp_debug, role, rid, ispburst, default_id;
139 	struct isp_sbussoftc *sbs;
140 	ispsoftc_t *isp = NULL;
141 	int locksetup = 0;
142 	int ints_setup = 0;
143 
144 	/*
145 	 * Figure out if we're supposed to skip this one.
146 	 * If we are, we actually go to ISP_ROLE_NONE.
147 	 */
148 
149 	tval = 0;
150 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
151 	    "disable", &tval) == 0 && tval) {
152 		device_printf(dev, "device is disabled\n");
153 		/* but return 0 so the !$)$)*!$*) unit isn't reused */
154 		return (0);
155 	}
156 
157 	role = 0;
158 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
159 	    "role", &role) == 0 &&
160 	    ((role & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) == 0)) {
161 		device_printf(dev, "setting role to 0x%x\n", role);
162 	} else {
163 		role = ISP_DEFAULT_ROLES;
164 	}
165 
166 	sbs = malloc(sizeof (*sbs), M_DEVBUF, M_NOWAIT | M_ZERO);
167 	if (sbs == NULL) {
168 		device_printf(dev, "cannot allocate softc\n");
169 		return (ENOMEM);
170 	}
171 
172 	regs = NULL;
173 	iqd = 0;
174 	rid = 0;
175 	regs = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
176 	if (regs == 0) {
177 		device_printf(dev, "unable to map registers\n");
178 		goto bad;
179 	}
180 	sbs->sbus_dev = dev;
181 	sbs->sbus_reg = regs;
182 	sbs->sbus_mdvec = mdvec;
183 
184 	sbs->sbus_poff[BIU_BLOCK >> _BLK_REG_SHFT] = BIU_REGS_OFF;
185 	sbs->sbus_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = SBUS_MBOX_REGS_OFF;
186 	sbs->sbus_poff[SXP_BLOCK >> _BLK_REG_SHFT] = SBUS_SXP_REGS_OFF;
187 	sbs->sbus_poff[RISC_BLOCK >> _BLK_REG_SHFT] = SBUS_RISC_REGS_OFF;
188 	sbs->sbus_poff[DMA_BLOCK >> _BLK_REG_SHFT] = DMA_REGS_OFF;
189 	isp = &sbs->sbus_isp;
190 	isp->isp_bus_tag = rman_get_bustag(regs);
191 	isp->isp_bus_handle = rman_get_bushandle(regs);
192 	isp->isp_mdvec = &sbs->sbus_mdvec;
193 	isp->isp_bustype = ISP_BT_SBUS;
194 	isp->isp_type = ISP_HA_SCSI_UNKNOWN;
195 	isp->isp_param = &sbs->sbus_param;
196 	isp->isp_osinfo.pc.ptr = &sbs->sbus_spi;
197 	isp->isp_revision = 0;	/* XXX */
198 	isp->isp_dev = dev;
199 	isp->isp_nchan = 1;
200 	ISP_SET_PC(isp, 0, def_role, role);
201 
202 	/*
203 	 * Get the clock frequency and convert it from HZ to MHz,
204 	 * rounding up. This defaults to 25MHz if there isn't a
205 	 * device specific one in the OFW device tree.
206 	 */
207 	sbs->sbus_mdvec.dv_clock = (sbus_get_clockfreq(dev) + 500000)/1000000;
208 
209 	/*
210 	 * Now figure out what the proper burst sizes, etc., to use.
211 	 * Unfortunately, there is no ddi_dma_burstsizes here which
212 	 * walks up the tree finding the limiting burst size node (if
213 	 * any). We just use what's here for isp.
214 	 */
215 	ispburst = sbus_get_burstsz(dev);
216 	if (ispburst == 0) {
217 		ispburst = SBUS_BURST_32 - 1;
218 	}
219 	sbs->sbus_mdvec.dv_conf1 =  0;
220 	if (ispburst & (1 << 5)) {
221 		sbs->sbus_mdvec.dv_conf1 = BIU_SBUS_CONF1_FIFO_32;
222 	} else if (ispburst & (1 << 4)) {
223 		sbs->sbus_mdvec.dv_conf1 = BIU_SBUS_CONF1_FIFO_16;
224 	} else if (ispburst & (1 << 3)) {
225 		sbs->sbus_mdvec.dv_conf1 =
226 		    BIU_SBUS_CONF1_BURST8 | BIU_SBUS_CONF1_FIFO_8;
227 	}
228 	if (sbs->sbus_mdvec.dv_conf1) {
229 		sbs->sbus_mdvec.dv_conf1 |= BIU_BURST_ENABLE;
230 	}
231 
232 	/*
233 	 * We don't trust NVRAM on SBus cards
234 	 */
235 	isp->isp_confopts |= ISP_CFG_NONVRAM;
236 
237 	/*
238 	 * Mark things if we're a PTI SBus adapter.
239 	 */
240 	if (strcmp("PTI,ptisp", ofw_bus_get_name(dev)) == 0 ||
241 	    strcmp("ptisp", ofw_bus_get_name(dev)) == 0) {
242 		SDPARAM(isp, 0)->isp_ptisp = 1;
243 	}
244 
245 
246 	isp->isp_osinfo.fw = firmware_get("isp_1000");
247 	if (isp->isp_osinfo.fw) {
248 		union {
249 			const void *cp;
250 			uint16_t *sp;
251 		} stupid;
252 		stupid.cp = isp->isp_osinfo.fw->data;
253 		isp->isp_mdvec->dv_ispfw = stupid.sp;
254 	}
255 
256 	tval = 0;
257         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
258             "fwload_disable", &tval) == 0 && tval != 0) {
259 		isp->isp_confopts |= ISP_CFG_NORELOAD;
260 	}
261 
262 	default_id = -1;
263 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
264             "iid", &tval) == 0) {
265 		default_id = tval;
266 		isp->isp_confopts |= ISP_CFG_OWNLOOPID;
267 	}
268 	if (default_id == -1) {
269 		default_id = OF_getscsinitid(dev);
270 	}
271 	ISP_SPI_PC(isp, 0)->iid = default_id;
272 
273 	isp_debug = 0;
274         (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
275             "debug", &isp_debug);
276 
277 	/* Make sure the lock is set up. */
278 	mtx_init(&isp->isp_osinfo.lock, "isp", NULL, MTX_DEF);
279 	locksetup++;
280 
281 	iqd = 0;
282 	sbs->sbus_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, &iqd,
283 	    RF_ACTIVE | RF_SHAREABLE);
284 	if (sbs->sbus_ires == NULL) {
285 		device_printf(dev, "could not allocate interrupt\n");
286 		goto bad;
287 	}
288 
289 	if (isp_setup_intr(dev, sbs->sbus_ires, ISP_IFLAGS, NULL,
290 	    isp_platform_intr, isp, &sbs->ih)) {
291 		device_printf(dev, "could not setup interrupt\n");
292 		goto bad;
293 	}
294 	ints_setup++;
295 
296 	/*
297 	 * Set up logging levels.
298 	 */
299 	if (isp_debug) {
300 		isp->isp_dblev = isp_debug;
301 	} else {
302 		isp->isp_dblev = ISP_LOGWARN|ISP_LOGERR;
303 	}
304 	if (bootverbose) {
305 		isp->isp_dblev |= ISP_LOGCONFIG|ISP_LOGINFO;
306 	}
307 
308 	/*
309 	 * Make sure we're in reset state.
310 	 */
311 	ISP_LOCK(isp);
312 	isp_reset(isp, 1);
313 	if (isp->isp_state != ISP_RESETSTATE) {
314 		isp_uninit(isp);
315 		ISP_UNLOCK(isp);
316 		goto bad;
317 	}
318 	isp_init(isp);
319 	if (isp->isp_state == ISP_INITSTATE) {
320 		isp->isp_state = ISP_RUNSTATE;
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 
331 bad:
332 
333 	if (sbs && ints_setup) {
334 		(void) bus_teardown_intr(dev, sbs->sbus_ires, sbs->ih);
335 	}
336 
337 	if (sbs && sbs->sbus_ires) {
338 		bus_release_resource(dev, SYS_RES_IRQ, iqd, sbs->sbus_ires);
339 	}
340 
341 	if (locksetup && isp) {
342 		mtx_destroy(&isp->isp_osinfo.lock);
343 	}
344 
345 	if (regs) {
346 		(void) bus_release_resource(dev, SYS_RES_MEMORY, 0, regs);
347 	}
348 
349 	if (sbs) {
350 		free(sbs, M_DEVBUF);
351 	}
352 	return (ENXIO);
353 }
354 
355 #define	IspVirt2Off(a, x)	\
356 	(((struct isp_sbussoftc *)a)->sbus_poff[((x) & _BLK_REG_MASK) >> \
357 	_BLK_REG_SHFT] + ((x) & 0xff))
358 
359 #define	BXR2(sbc, off)		\
360 	bus_space_read_2(isp->isp_bus_tag, isp->isp_bus_handle, off)
361 
362 static int
363 isp_sbus_rd_isr(ispsoftc_t *isp, uint32_t *isrp, uint16_t *semap, uint16_t *mbp)
364 {
365 	uint16_t isr, sema;
366 
367 	isr = BXR2(sbc, IspVirt2Off(isp, BIU_ISR));
368 	sema = BXR2(sbc, IspVirt2Off(isp, BIU_SEMA));
369 	isp_prt(isp, ISP_LOGDEBUG3, "ISR 0x%x SEMA 0x%x", isr, sema);
370 	isr &= INT_PENDING_MASK(isp);
371 	sema &= BIU_SEMA_LOCK;
372 	if (isr == 0 && sema == 0) {
373 		return (0);
374 	}
375 	*isrp = isr;
376 	if ((*semap = sema) != 0) {
377 		*mbp = BXR2(sbc, IspVirt2Off(isp, OUTMAILBOX0));
378 	}
379 	return (1);
380 }
381 
382 static uint32_t
383 isp_sbus_rd_reg(ispsoftc_t *isp, int regoff)
384 {
385 	uint16_t rval;
386 	struct isp_sbussoftc *sbs = (struct isp_sbussoftc *) isp;
387 	int offset = sbs->sbus_poff[(regoff & _BLK_REG_MASK) >> _BLK_REG_SHFT];
388 	offset += (regoff & 0xff);
389 	rval = bus_space_read_2(isp->isp_bus_tag, isp->isp_bus_handle, offset);
390 	isp_prt(isp, ISP_LOGDEBUG3,
391 	    "isp_sbus_rd_reg(off %x) = %x", regoff, rval);
392 	return (rval);
393 }
394 
395 static void
396 isp_sbus_wr_reg(ispsoftc_t *isp, int regoff, uint32_t val)
397 {
398 	struct isp_sbussoftc *sbs = (struct isp_sbussoftc *) isp;
399 	int offset = sbs->sbus_poff[(regoff & _BLK_REG_MASK) >> _BLK_REG_SHFT];
400 	offset += (regoff & 0xff);
401 	isp_prt(isp, ISP_LOGDEBUG3,
402 	    "isp_sbus_wr_reg(off %x) = %x", regoff, val);
403 	bus_space_write_2(isp->isp_bus_tag, isp->isp_bus_handle, offset, val);
404 	MEMORYBARRIER(isp, SYNC_REG, offset, 2, -1);
405 }
406 
407 struct imush {
408 	ispsoftc_t *isp;
409 	int error;
410 };
411 
412 static void imc(void *, bus_dma_segment_t *, int, int);
413 
414 static void
415 imc(void *arg, bus_dma_segment_t *segs, int nseg, int error)
416 {
417 	struct imush *imushp = (struct imush *) arg;
418 	if (error) {
419 		imushp->error = error;
420 	} else {
421 		ispsoftc_t *isp =imushp->isp;
422 		bus_addr_t addr = segs->ds_addr;
423 
424 		isp->isp_rquest_dma = addr;
425 		addr += ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
426 		isp->isp_result_dma = addr;
427 	}
428 }
429 
430 static int
431 isp_sbus_mbxdma(ispsoftc_t *isp)
432 {
433 	caddr_t base;
434 	uint32_t len;
435 	int i, error, ns;
436 	struct imush im;
437 
438 	/*
439 	 * Already been here? If so, leave...
440 	 */
441 	if (isp->isp_rquest) {
442 		return (0);
443 	}
444 
445 	ISP_UNLOCK(isp);
446 
447 	len = sizeof (struct isp_pcmd) * isp->isp_maxcmds;
448 	isp->isp_osinfo.pcmd_pool = (struct isp_pcmd *)
449 	    malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
450 	if (isp->isp_osinfo.pcmd_pool == NULL) {
451 		isp_prt(isp, ISP_LOGERR, "cannot alloc pcmd pool");
452 		ISP_LOCK(isp);
453 		return (1);
454 	}
455 
456 	len = sizeof (isp_hdl_t *) * isp->isp_maxcmds;
457 	isp->isp_xflist = (isp_hdl_t *) malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
458 	if (isp->isp_xflist == NULL) {
459 		isp_prt(isp, ISP_LOGERR, "cannot alloc xflist array");
460 		ISP_LOCK(isp);
461 		return (1);
462 	}
463 	for (len = 0; len < isp->isp_maxcmds - 1; len++) {
464 		isp->isp_xflist[len].cmd = &isp->isp_xflist[len+1];
465 	}
466 	isp->isp_xffree = isp->isp_xflist;
467 	len = sizeof (bus_dmamap_t) * isp->isp_maxcmds;
468 
469 	if (isp_dma_tag_create(BUS_DMA_ROOTARG(ISP_SBD(isp)), 1,
470 	    BUS_SPACE_MAXADDR_24BIT+1, BUS_SPACE_MAXADDR_32BIT,
471 	    BUS_SPACE_MAXADDR_32BIT, NULL, NULL, BUS_SPACE_MAXSIZE_32BIT,
472 	    ISP_NSEGS, BUS_SPACE_MAXADDR_24BIT, 0, &isp->isp_osinfo.dmat)) {
473 		isp_prt(isp, ISP_LOGERR, "could not create master dma tag");
474 		free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
475 		free(isp->isp_xflist, M_DEVBUF);
476 		ISP_LOCK(isp);
477 		return(1);
478 	}
479 
480 	/*
481 	 * Allocate and map the request, result queues, plus FC scratch area.
482 	 */
483 	len = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
484 	len += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
485 
486 	ns = (len / PAGE_SIZE) + 1;
487 	if (isp_dma_tag_create(isp->isp_osinfo.dmat, QENTRY_LEN,
488 	    BUS_SPACE_MAXADDR_24BIT+1, BUS_SPACE_MAXADDR_32BIT,
489 	    BUS_SPACE_MAXADDR_32BIT, NULL, NULL, len, ns,
490 	    BUS_SPACE_MAXADDR_24BIT, 0, &isp->isp_osinfo.cdmat)) {
491 		isp_prt(isp, ISP_LOGERR,
492 		    "cannot create a dma tag for control spaces");
493 		free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
494 		free(isp->isp_xflist, M_DEVBUF);
495 		ISP_LOCK(isp);
496 		return (1);
497 	}
498 
499 	if (bus_dmamem_alloc(isp->isp_osinfo.cdmat, (void **)&base, BUS_DMA_NOWAIT | BUS_DMA_COHERENT,
500 	    &isp->isp_osinfo.cdmap) != 0) {
501 		isp_prt(isp, ISP_LOGERR,
502 		    "cannot allocate %d bytes of CCB memory", len);
503 		bus_dma_tag_destroy(isp->isp_osinfo.cdmat);
504 		free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
505 		free(isp->isp_xflist, M_DEVBUF);
506 		ISP_LOCK(isp);
507 		return (1);
508 	}
509 
510 	for (i = 0; i < isp->isp_maxcmds; i++) {
511 		struct isp_pcmd *pcmd = &isp->isp_osinfo.pcmd_pool[i];
512 		error = bus_dmamap_create(isp->isp_osinfo.dmat, 0, &pcmd->dmap);
513 		if (error) {
514 			isp_prt(isp, ISP_LOGERR,
515 			    "error %d creating per-cmd DMA maps", error);
516 			while (--i >= 0) {
517 				bus_dmamap_destroy(isp->isp_osinfo.dmat,
518 				    isp->isp_osinfo.pcmd_pool[i].dmap);
519 			}
520 			goto bad;
521 		}
522 		callout_init_mtx(&pcmd->wdog, &isp->isp_osinfo.lock, 0);
523 		if (i == isp->isp_maxcmds-1) {
524 			pcmd->next = NULL;
525 		} else {
526 			pcmd->next = &isp->isp_osinfo.pcmd_pool[i+1];
527 		}
528 	}
529 	isp->isp_osinfo.pcmd_free = &isp->isp_osinfo.pcmd_pool[0];
530 
531 	im.isp = isp;
532 	im.error = 0;
533 	bus_dmamap_load(isp->isp_osinfo.cdmat, isp->isp_osinfo.cdmap, base, len, imc, &im, 0);
534 	if (im.error) {
535 		isp_prt(isp, ISP_LOGERR,
536 		    "error %d loading dma map for control areas", im.error);
537 		goto bad;
538 	}
539 
540 	isp->isp_rquest = base;
541 	base += ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
542 	isp->isp_result = base;
543 	ISP_LOCK(isp);
544 	return (0);
545 
546 bad:
547 	bus_dmamem_free(isp->isp_osinfo.cdmat, base, isp->isp_osinfo.cdmap);
548 	bus_dma_tag_destroy(isp->isp_osinfo.cdmat);
549 	free(isp->isp_xflist, M_DEVBUF);
550 	free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
551 	isp->isp_rquest = NULL;
552 	ISP_LOCK(isp);
553 	return (1);
554 }
555 
556 typedef struct {
557 	ispsoftc_t *isp;
558 	void *cmd_token;
559 	void *rq;	/* original request */
560 	int error;
561 	bus_size_t mapsize;
562 } mush_t;
563 
564 #define	MUSHERR_NOQENTRIES	-2
565 
566 static void dma2(void *, bus_dma_segment_t *, int, int);
567 
568 static void
569 dma2(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
570 {
571 	mush_t *mp;
572 	ispsoftc_t *isp;
573 	struct ccb_scsiio *csio;
574 	isp_ddir_t ddir;
575 	ispreq_t *rq;
576 
577 	mp = (mush_t *) arg;
578 	if (error) {
579 		mp->error = error;
580 		return;
581 	}
582 	csio = mp->cmd_token;
583 	isp = mp->isp;
584 	rq = mp->rq;
585 	if (nseg) {
586 		if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
587 			bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_PREREAD);
588 			ddir = ISP_FROM_DEVICE;
589 		} else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
590 			bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_PREWRITE);
591 			ddir = ISP_TO_DEVICE;
592 		} else {
593 			ddir = ISP_NOXFR;
594 		}
595 	} else {
596 		dm_segs = NULL;
597 		nseg = 0;
598 		ddir = ISP_NOXFR;
599 	}
600 
601 	if (isp_send_cmd(isp, rq, dm_segs, nseg, XS_XFRLEN(csio), ddir) != CMD_QUEUED) {
602 		mp->error = MUSHERR_NOQENTRIES;
603 	}
604 }
605 
606 static int
607 isp_sbus_dmasetup(ispsoftc_t *isp, struct ccb_scsiio *csio, void *ff)
608 {
609 	mush_t mush, *mp;
610 	void (*eptr)(void *, bus_dma_segment_t *, int, int);
611 
612 	mp = &mush;
613 	mp->isp = isp;
614 	mp->cmd_token = csio;
615 	mp->rq = ff;
616 	mp->error = 0;
617 	mp->mapsize = 0;
618 
619 	eptr = dma2;
620 
621 	if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE || (csio->dxfer_len == 0)) {
622 		(*eptr)(mp, NULL, 0, 0);
623 	} else if ((csio->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
624 		if ((csio->ccb_h.flags & CAM_DATA_PHYS) == 0) {
625 			int error;
626 			error = bus_dmamap_load(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, csio->data_ptr, csio->dxfer_len, eptr, mp, 0);
627 #if 0
628 			xpt_print(csio->ccb_h.path, "%s: bus_dmamap_load " "ptr %p len %d returned %d\n", __func__, csio->data_ptr, csio->dxfer_len, error);
629 #endif
630 
631 			if (error == EINPROGRESS) {
632 				bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
633 				mp->error = EINVAL;
634 				isp_prt(isp, ISP_LOGERR, "deferred dma allocation not supported");
635 			} else if (error && mp->error == 0) {
636 #ifdef	DIAGNOSTIC
637 				isp_prt(isp, ISP_LOGERR, "error %d in dma mapping code", error);
638 #endif
639 				mp->error = error;
640 			}
641 		} else {
642 			/* Pointer to physical buffer */
643 			struct bus_dma_segment seg;
644 			seg.ds_addr = (bus_addr_t)(vm_offset_t)csio->data_ptr;
645 			seg.ds_len = csio->dxfer_len;
646 			(*eptr)(mp, &seg, 1, 0);
647 		}
648 	} else {
649 		struct bus_dma_segment *segs;
650 
651 		if ((csio->ccb_h.flags & CAM_DATA_PHYS) != 0) {
652 			isp_prt(isp, ISP_LOGERR, "Physical segment pointers unsupported");
653 			mp->error = EINVAL;
654 		} else if ((csio->ccb_h.flags & CAM_SG_LIST_PHYS) == 0) {
655 			isp_prt(isp, ISP_LOGERR, "Physical SG/LIST Phys segment pointers unsupported");
656 			mp->error = EINVAL;
657 		} else {
658 			/* Just use the segments provided */
659 			segs = (struct bus_dma_segment *) csio->data_ptr;
660 			(*eptr)(mp, segs, csio->sglist_cnt, 0);
661 		}
662 	}
663 	if (mp->error) {
664 		int retval = CMD_COMPLETE;
665 		if (mp->error == MUSHERR_NOQENTRIES) {
666 			retval = CMD_EAGAIN;
667 		} else if (mp->error == EFBIG) {
668 			XS_SETERR(csio, CAM_REQ_TOO_BIG);
669 		} else if (mp->error == EINVAL) {
670 			XS_SETERR(csio, CAM_REQ_INVALID);
671 		} else {
672 			XS_SETERR(csio, CAM_UNREC_HBA_ERROR);
673 		}
674 		return (retval);
675 	}
676 	return (CMD_QUEUED);
677 }
678 
679 static void
680 isp_sbus_reset0(ispsoftc_t *isp)
681 {
682 	ISP_DISABLE_INTS(isp);
683 }
684 
685 static void
686 isp_sbus_reset1(ispsoftc_t *isp)
687 {
688 	ISP_ENABLE_INTS(isp);
689 }
690 
691 static void
692 isp_sbus_dumpregs(ispsoftc_t *isp, const char *msg)
693 {
694 	if (msg)
695 		printf("%s: %s\n", device_get_nameunit(isp->isp_dev), msg);
696 	else
697 		printf("%s:\n", device_get_nameunit(isp->isp_dev));
698 	printf("    biu_conf1=%x", ISP_READ(isp, BIU_CONF1));
699 	printf(" biu_icr=%x biu_isr=%x biu_sema=%x ", ISP_READ(isp, BIU_ICR),
700 	    ISP_READ(isp, BIU_ISR), ISP_READ(isp, BIU_SEMA));
701 	printf("risc_hccr=%x\n", ISP_READ(isp, HCCR));
702 
703 
704 	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
705 	printf("    cdma_conf=%x cdma_sts=%x cdma_fifostat=%x\n",
706 		ISP_READ(isp, CDMA_CONF), ISP_READ(isp, CDMA_STATUS),
707 		ISP_READ(isp, CDMA_FIFO_STS));
708 	printf("    ddma_conf=%x ddma_sts=%x ddma_fifostat=%x\n",
709 		ISP_READ(isp, DDMA_CONF), ISP_READ(isp, DDMA_STATUS),
710 		ISP_READ(isp, DDMA_FIFO_STS));
711 	printf("    sxp_int=%x sxp_gross=%x sxp(scsi_ctrl)=%x\n",
712 		ISP_READ(isp, SXP_INTERRUPT),
713 		ISP_READ(isp, SXP_GROSS_ERR),
714 		ISP_READ(isp, SXP_PINS_CTRL));
715 	ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
716 	printf("    mbox regs: %x %x %x %x %x\n",
717 	    ISP_READ(isp, OUTMAILBOX0), ISP_READ(isp, OUTMAILBOX1),
718 	    ISP_READ(isp, OUTMAILBOX2), ISP_READ(isp, OUTMAILBOX3),
719 	    ISP_READ(isp, OUTMAILBOX4));
720 }
721