isp_sbus.c revision 169459
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: head/sys/dev/isp/isp_sbus.c 169459 2007-05-11 06:28:26Z mjacob $");
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#if __FreeBSD_version >= 700000
37#include <sys/linker.h>
38#include <sys/firmware.h>
39#endif
40#include <sys/bus.h>
41#include <sys/kernel.h>
42#include <sys/module.h>
43#include <sys/resource.h>
44
45#include <dev/ofw/ofw_bus.h>
46
47#include <machine/bus.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
55isp_sbus_rd_reg(ispsoftc_t *, int);
56static void
57isp_sbus_wr_reg(ispsoftc_t *, int, uint32_t);
58static int
59isp_sbus_rd_isr(ispsoftc_t *, uint32_t *, uint16_t *, uint16_t *);
60static int isp_sbus_mbxdma(ispsoftc_t *);
61static int
62isp_sbus_dmasetup(ispsoftc_t *, XS_T *, ispreq_t *, uint32_t *, uint32_t);
63
64static void isp_sbus_reset0(ispsoftc_t *);
65static void isp_sbus_reset1(ispsoftc_t *);
66static void isp_sbus_dumpregs(ispsoftc_t *, const char *);
67
68static struct ispmdvec mdvec = {
69	isp_sbus_rd_isr,
70	isp_sbus_rd_reg,
71	isp_sbus_wr_reg,
72	isp_sbus_mbxdma,
73	isp_sbus_dmasetup,
74	isp_common_dmateardown,
75	isp_sbus_reset0,
76	isp_sbus_reset1,
77	isp_sbus_dumpregs,
78	NULL,
79	BIU_BURST_ENABLE|BIU_PCI_CONF1_FIFO_64
80};
81
82static int isp_sbus_probe (device_t);
83static int isp_sbus_attach (device_t);
84
85
86#define	ISP_SBD(isp)	((struct isp_sbussoftc *)isp)->sbus_dev
87struct isp_sbussoftc {
88	ispsoftc_t			sbus_isp;
89	device_t			sbus_dev;
90	struct resource *		sbus_reg;
91	void *				ih;
92	int16_t				sbus_poff[_NREG_BLKS];
93	sdparam				sbus_param;
94	struct ispmdvec			sbus_mdvec;
95	struct resource *		sbus_ires;
96};
97
98
99static device_method_t isp_sbus_methods[] = {
100	/* Device interface */
101	DEVMETHOD(device_probe,		isp_sbus_probe),
102	DEVMETHOD(device_attach,	isp_sbus_attach),
103	{ 0, 0 }
104};
105
106static driver_t isp_sbus_driver = {
107	"isp", isp_sbus_methods, sizeof (struct isp_sbussoftc)
108};
109static devclass_t isp_devclass;
110DRIVER_MODULE(isp, sbus, isp_sbus_driver, isp_devclass, 0, 0);
111#if __FreeBSD_version < 700000
112extern ispfwfunc *isp_get_firmware_p;
113#endif
114
115static int
116isp_sbus_probe(device_t dev)
117{
118	int found = 0;
119	const char *name = ofw_bus_get_name(dev);
120	if (strcmp(name, "SUNW,isp") == 0 ||
121	    strcmp(name, "QLGC,isp") == 0 ||
122	    strcmp(name, "ptisp") == 0 ||
123	    strcmp(name, "PTI,ptisp") == 0) {
124		found++;
125	}
126	if (!found)
127		return (ENXIO);
128
129	if (isp_announced == 0 && bootverbose) {
130		printf("Qlogic ISP Driver, FreeBSD Version %d.%d, "
131		    "Core Version %d.%d\n",
132		    ISP_PLATFORM_VERSION_MAJOR, ISP_PLATFORM_VERSION_MINOR,
133		    ISP_CORE_VERSION_MAJOR, ISP_CORE_VERSION_MINOR);
134		isp_announced++;
135	}
136	return (0);
137}
138
139static int
140isp_sbus_attach(device_t dev)
141{
142	struct resource *regs;
143	int tval, iqd, isp_debug, role, rid, ispburst;
144	struct isp_sbussoftc *sbs;
145	ispsoftc_t *isp = NULL;
146	int locksetup = 0;
147	int ints_setup = 0;
148
149	/*
150	 * Figure out if we're supposed to skip this one.
151	 * If we are, we actually go to ISP_ROLE_NONE.
152	 */
153
154	tval = 0;
155	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
156	    "disable", &tval) == 0 && tval) {
157		device_printf(dev, "device is disabled\n");
158		/* but return 0 so the !$)$)*!$*) unit isn't reused */
159		return (0);
160	}
161
162	role = 0;
163	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
164	    "role", &role) == 0 &&
165	    ((role & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) == 0)) {
166		device_printf(dev, "setting role to 0x%x\n", role);
167	} else {
168#ifdef	ISP_TARGET_MODE
169		role = ISP_ROLE_INITIATOR|ISP_ROLE_TARGET;
170#else
171		role = ISP_DEFAULT_ROLES;
172#endif
173	}
174
175	sbs = malloc(sizeof (*sbs), M_DEVBUF, M_NOWAIT | M_ZERO);
176	if (sbs == NULL) {
177		device_printf(dev, "cannot allocate softc\n");
178		return (ENOMEM);
179	}
180
181	regs = NULL;
182	iqd = 0;
183	rid = 0;
184	regs = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
185	if (regs == 0) {
186		device_printf(dev, "unable to map registers\n");
187		goto bad;
188	}
189	sbs->sbus_dev = dev;
190	sbs->sbus_reg = regs;
191	sbs->sbus_mdvec = mdvec;
192
193	sbs->sbus_poff[BIU_BLOCK >> _BLK_REG_SHFT] = BIU_REGS_OFF;
194	sbs->sbus_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = SBUS_MBOX_REGS_OFF;
195	sbs->sbus_poff[SXP_BLOCK >> _BLK_REG_SHFT] = SBUS_SXP_REGS_OFF;
196	sbs->sbus_poff[RISC_BLOCK >> _BLK_REG_SHFT] = SBUS_RISC_REGS_OFF;
197	sbs->sbus_poff[DMA_BLOCK >> _BLK_REG_SHFT] = DMA_REGS_OFF;
198	isp = &sbs->sbus_isp;
199	isp->isp_bus_tag = rman_get_bustag(regs);
200	isp->isp_bus_handle = rman_get_bushandle(regs);
201	isp->isp_mdvec = &sbs->sbus_mdvec;
202	isp->isp_bustype = ISP_BT_SBUS;
203	isp->isp_type = ISP_HA_SCSI_UNKNOWN;
204	isp->isp_param = &sbs->sbus_param;
205	isp->isp_revision = 0;	/* XXX */
206	isp->isp_role = role;
207	isp->isp_dev = dev;
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)->isp_ptisp = 1;
250	}
251
252
253#if __FreeBSD_version >= 700000
254	isp->isp_osinfo.fw = firmware_get("isp_1000");
255	if (isp->isp_osinfo.fw) {
256		union {
257			const void *cp;
258			uint16_t *sp;
259		} stupid;
260		stupid.cp = isp->isp_osinfo.fw->data;
261		isp->isp_mdvec->dv_ispfw = stupid.sp;
262	}
263#else
264	/*
265	 * Try and find firmware for this device.
266	 */
267	if (isp_get_firmware_p) {
268		(*isp_get_firmware_p)(0, 0, 0x1000, &sbs->sbus_mdvec.dv_ispfw);
269	}
270#endif
271
272	tval = 0;
273        if (resource_int_value(device_get_name(dev), device_get_unit(dev),
274            "fwload_disable", &tval) == 0 && tval != 0) {
275		isp->isp_confopts |= ISP_CFG_NORELOAD;
276	}
277
278	isp->isp_osinfo.default_id = -1;
279	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
280            "iid", &tval) == 0) {
281		isp->isp_osinfo.default_id = tval;
282		isp->isp_confopts |= ISP_CFG_OWNLOOPID;
283	}
284	if (isp->isp_osinfo.default_id == -1) {
285		/*
286		 * XXX: should be a way to get properties w/o having
287		 * XXX: to call OF_xxx functions
288		 */
289		isp->isp_osinfo.default_id = 7;
290	}
291
292	isp_debug = 0;
293        (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
294            "debug", &isp_debug);
295
296	/* Make sure the lock is set up. */
297	mtx_init(&isp->isp_osinfo.lock, "isp", NULL, MTX_DEF);
298	locksetup++;
299
300	iqd = 0;
301	sbs->sbus_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, &iqd,
302	    RF_ACTIVE | RF_SHAREABLE);
303	if (sbs->sbus_ires == NULL) {
304		device_printf(dev, "could not allocate interrupt\n");
305		goto bad;
306	}
307
308	if (isp_setup_intr(dev, sbs->sbus_ires, ISP_IFLAGS, NULL,
309	    isp_platform_intr, isp, &sbs->ih)) {
310		device_printf(dev, "could not setup interrupt\n");
311		goto bad;
312	}
313	ints_setup++;
314
315	/*
316	 * Set up logging levels.
317	 */
318	if (isp_debug) {
319		isp->isp_dblev = isp_debug;
320	} else {
321		isp->isp_dblev = ISP_LOGWARN|ISP_LOGERR;
322	}
323	if (bootverbose) {
324		isp->isp_dblev |= ISP_LOGCONFIG|ISP_LOGINFO;
325	}
326
327	/*
328	 * Make sure we're in reset state.
329	 */
330	isp_reset(isp);
331	if (isp->isp_state != ISP_RESETSTATE) {
332		isp_uninit(isp);
333		goto bad;
334	}
335	isp_init(isp);
336	if (isp->isp_role != ISP_ROLE_NONE && isp->isp_state != ISP_INITSTATE) {
337		isp_uninit(isp);
338		goto bad;
339	}
340	isp_attach(isp);
341	if (isp->isp_role != ISP_ROLE_NONE && isp->isp_state != ISP_RUNSTATE) {
342		isp_uninit(isp);
343		goto bad;
344	}
345	return (0);
346
347bad:
348
349	if (sbs && ints_setup) {
350		(void) bus_teardown_intr(dev, sbs->sbus_ires, sbs->ih);
351	}
352
353	if (sbs && sbs->sbus_ires) {
354		bus_release_resource(dev, SYS_RES_IRQ, iqd, sbs->sbus_ires);
355	}
356
357	if (locksetup && isp) {
358		mtx_destroy(&isp->isp_osinfo.lock);
359	}
360
361	if (regs) {
362		(void) bus_release_resource(dev, 0, 0, regs);
363	}
364
365	if (sbs) {
366		if (sbs->sbus_isp.isp_param) {
367			free(sbs->sbus_isp.isp_param, M_DEVBUF);
368		}
369		free(sbs, M_DEVBUF);
370	}
371	return (ENXIO);
372}
373
374#define	IspVirt2Off(a, x)	\
375	(((struct isp_sbussoftc *)a)->sbus_poff[((x) & _BLK_REG_MASK) >> \
376	_BLK_REG_SHFT] + ((x) & 0xff))
377
378#define	BXR2(sbc, off)		\
379	bus_space_read_2(isp->isp_bus_tag, isp->isp_bus_handle, off)
380
381static int
382isp_sbus_rd_isr(ispsoftc_t *isp, uint32_t *isrp, uint16_t *semap, uint16_t *mbp)
383{
384	uint16_t isr, sema;
385
386	isr = BXR2(sbc, IspVirt2Off(isp, BIU_ISR));
387	sema = BXR2(sbc, IspVirt2Off(isp, BIU_SEMA));
388	isp_prt(isp, ISP_LOGDEBUG3, "ISR 0x%x SEMA 0x%x", isr, sema);
389	isr &= INT_PENDING_MASK(isp);
390	sema &= BIU_SEMA_LOCK;
391	if (isr == 0 && sema == 0) {
392		return (0);
393	}
394	*isrp = isr;
395	if ((*semap = sema) != 0) {
396		*mbp = BXR2(sbc, IspVirt2Off(isp, OUTMAILBOX0));
397	}
398	return (1);
399}
400
401static uint32_t
402isp_sbus_rd_reg(ispsoftc_t *isp, int regoff)
403{
404	uint16_t rval;
405	struct isp_sbussoftc *sbs = (struct isp_sbussoftc *) isp;
406	int offset = sbs->sbus_poff[(regoff & _BLK_REG_MASK) >> _BLK_REG_SHFT];
407	offset += (regoff & 0xff);
408	rval = bus_space_read_2(isp->isp_bus_tag, isp->isp_bus_handle, offset);
409	isp_prt(isp, ISP_LOGDEBUG3,
410	    "isp_sbus_rd_reg(off %x) = %x", regoff, rval);
411	return (rval);
412}
413
414static void
415isp_sbus_wr_reg(ispsoftc_t *isp, int regoff, uint32_t val)
416{
417	struct isp_sbussoftc *sbs = (struct isp_sbussoftc *) isp;
418	int offset = sbs->sbus_poff[(regoff & _BLK_REG_MASK) >> _BLK_REG_SHFT];
419	offset += (regoff & 0xff);
420	isp_prt(isp, ISP_LOGDEBUG3,
421	    "isp_sbus_wr_reg(off %x) = %x", regoff, val);
422	bus_space_write_2(isp->isp_bus_tag, isp->isp_bus_handle, offset, val);
423	MEMORYBARRIER(isp, SYNC_REG, offset, 2);
424}
425
426struct imush {
427	ispsoftc_t *isp;
428	int error;
429};
430
431static void imc(void *, bus_dma_segment_t *, int, int);
432
433static void
434imc(void *arg, bus_dma_segment_t *segs, int nseg, int error)
435{
436	struct imush *imushp = (struct imush *) arg;
437	if (error) {
438		imushp->error = error;
439	} else {
440		ispsoftc_t *isp =imushp->isp;
441		bus_addr_t addr = segs->ds_addr;
442
443		isp->isp_rquest_dma = addr;
444		addr += ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
445		isp->isp_result_dma = addr;
446	}
447}
448
449static int
450isp_sbus_mbxdma(ispsoftc_t *isp)
451{
452	caddr_t base;
453	uint32_t len;
454	int i, error, ns;
455	struct imush im;
456
457	/*
458	 * Already been here? If so, leave...
459	 */
460	if (isp->isp_rquest) {
461		return (0);
462	}
463
464	ISP_UNLOCK(isp);
465
466	len = sizeof (struct isp_pcmd) * isp->isp_maxcmds;
467	isp->isp_osinfo.pcmd_pool = (struct isp_pcmd *)
468	    malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
469	if (isp->isp_osinfo.pcmd_pool == NULL) {
470		isp_prt(isp, ISP_LOGERR, "cannot alloc pcmd pool");
471		ISP_LOCK(isp);
472		return (1);
473	}
474
475	len = sizeof (XS_T **) * isp->isp_maxcmds;
476	isp->isp_xflist = (XS_T **) malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
477	if (isp->isp_xflist == NULL) {
478		isp_prt(isp, ISP_LOGERR, "cannot alloc xflist array");
479		ISP_LOCK(isp);
480		return (1);
481	}
482	len = sizeof (bus_dmamap_t) * isp->isp_maxcmds;
483
484	if (isp_dma_tag_create(BUS_DMA_ROOTARG(ISP_SBD(isp), 1,
485	    BUS_SPACE_MAXADDR_24BIT+1, BUS_SPACE_MAXADDR_32BIT,
486	    BUS_SPACE_MAXADDR_32BIT, NULL, NULL, BUS_SPACE_MAXSIZE_32BIT,
487	    ISP_NSEGS, BUS_SPACE_MAXADDR_24BIT, 0, &isp->isp_osinfo.dmat)) {
488		isp_prt(isp, ISP_LOGERR, "could not create master dma tag");
489		free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
490		free(isp->isp_xflist, M_DEVBUF);
491		ISP_LOCK(isp);
492		return(1);
493	}
494
495	/*
496	 * Allocate and map the request, result queues, plus FC scratch area.
497	 */
498	len = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
499	len += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
500
501	ns = (len / PAGE_SIZE) + 1;
502	if (isp_dma_tag_create(isp->isp_osinfo.dmat, QENTRY_LEN,
503	    BUS_SPACE_MAXADDR_24BIT+1, BUS_SPACE_MAXADDR_32BIT,
504	    BUS_SPACE_MAXADDR_32BIT, NULL, NULL, len, ns,
505	    BUS_SPACE_MAXADDR_24BIT, 0, &isp->isp_cdmat)) {
506		isp_prt(isp, ISP_LOGERR,
507		    "cannot create a dma tag for control spaces");
508		free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
509		free(isp->isp_xflist, M_DEVBUF);
510		ISP_LOCK(isp);
511		return (1);
512	}
513
514	if (bus_dmamem_alloc(isp->isp_cdmat, (void **)&base, BUS_DMA_NOWAIT,
515	    &isp->isp_cdmap) != 0) {
516		isp_prt(isp, ISP_LOGERR,
517		    "cannot allocate %d bytes of CCB memory", len);
518		bus_dma_tag_destroy(isp->isp_cdmat);
519		free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
520		free(isp->isp_xflist, M_DEVBUF);
521		ISP_LOCK(isp);
522		return (1);
523	}
524
525	for (i = 0; i < isp->isp_maxcmds; i++) {
526		struct isp_pcmd *pcmd = &isp->isp_osinfo.pcmd_pool[i];
527		error = bus_dmamap_create(isp->isp_osinfo.dmat, 0, &pcmd->dmap);
528		if (error) {
529			isp_prt(isp, ISP_LOGERR,
530			    "error %d creating per-cmd DMA maps", error);
531			while (--i >= 0) {
532				bus_dmamap_destroy(isp->isp_osinfo.dmat,
533				    isp->isp_osinfo.pcmd_pool[i].dmap);
534			}
535			goto bad;
536		}
537		isp_callout_init(&pcmd->wdog);
538		if (i == isp->isp_maxcmds-1) {
539			pcmd->next = NULL;
540		} else {
541			pcmd->next = &isp->isp_osinfo.pcmd_pool[i+1];
542		}
543	}
544	isp->isp_osinfo.pcmd_free = &isp->isp_osinfo.pcmd_pool[0];
545
546	im.isp = isp;
547	im.error = 0;
548	bus_dmamap_load(isp->isp_cdmat, isp->isp_cdmap, base, len, imc, &im, 0);
549	if (im.error) {
550		isp_prt(isp, ISP_LOGERR,
551		    "error %d loading dma map for control areas", im.error);
552		goto bad;
553	}
554
555	isp->isp_rquest = base;
556	base += ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
557	isp->isp_result = base;
558	ISP_LOCK(isp);
559	return (0);
560
561bad:
562	bus_dmamem_free(isp->isp_cdmat, base, isp->isp_cdmap);
563	bus_dma_tag_destroy(isp->isp_cdmat);
564	free(isp->isp_xflist, M_DEVBUF);
565	free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
566	isp->isp_rquest = NULL;
567	ISP_LOCK(isp);
568	return (1);
569}
570
571typedef struct {
572	ispsoftc_t *isp;
573	void *cmd_token;
574	void *rq;
575	uint32_t *nxtip;
576	uint32_t optr;
577	int error;
578} mush_t;
579
580#define	MUSHERR_NOQENTRIES	-2
581
582
583static void dma2(void *, bus_dma_segment_t *, int, int);
584
585static void
586dma2(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
587{
588	mush_t *mp;
589	ispsoftc_t *isp;
590	struct ccb_scsiio *csio;
591	bus_dma_segment_t *eseg;
592	ispreq_t *rq;
593	int seglim, datalen;
594	uint16_t nxti;
595
596	mp = (mush_t *) arg;
597	if (error) {
598		mp->error = error;
599		return;
600	}
601
602	if (nseg < 1) {
603		isp_prt(mp->isp, ISP_LOGERR, "bad segment count (%d)", nseg);
604		mp->error = EFAULT;
605		return;
606	}
607	csio = mp->cmd_token;
608	isp = mp->isp;
609	rq = mp->rq;
610	nxti = *mp->nxtip;
611
612	if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
613		bus_dmamap_sync(isp->isp_osinfo.dmat,
614		    PISP_PCMD(csio)->dmap, BUS_DMASYNC_PREREAD);
615	} else {
616		bus_dmamap_sync(isp->isp_osinfo.dmat,
617		    PISP_PCMD(csio)->dmap, BUS_DMASYNC_PREWRITE);
618	}
619
620	datalen = XS_XFRLEN(csio);
621
622	/*
623	 * We're passed an initial partially filled in entry that
624	 * has most fields filled in except for data transfer
625	 * related values.
626	 *
627	 * Our job is to fill in the initial request queue entry and
628	 * then to start allocating and filling in continuation entries
629	 * until we've covered the entire transfer.
630	 */
631
632	if (csio->cdb_len > 12) {
633		seglim = 0;
634	} else {
635		seglim = ISP_RQDSEG;
636	}
637	if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
638		rq->req_flags |= REQFLAG_DATA_IN;
639	} else {
640		rq->req_flags |= REQFLAG_DATA_OUT;
641	}
642
643	eseg = dm_segs + nseg;
644
645	while (datalen != 0 && rq->req_seg_count < seglim && dm_segs != eseg) {
646		rq->req_dataseg[rq->req_seg_count].ds_base = dm_segs->ds_addr;
647		rq->req_dataseg[rq->req_seg_count].ds_count = dm_segs->ds_len;
648		datalen -= dm_segs->ds_len;
649		rq->req_seg_count++;
650		dm_segs++;
651	}
652
653	while (datalen > 0 && dm_segs != eseg) {
654		uint16_t onxti;
655		ispcontreq_t local, *crq = &local, *cqe;
656
657		cqe = (ispcontreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, nxti);
658		onxti = nxti;
659		nxti = ISP_NXT_QENTRY(onxti, RQUEST_QUEUE_LEN(isp));
660		if (nxti == mp->optr) {
661			isp_prt(isp, ISP_LOGDEBUG0, "Request Queue Overflow++");
662			mp->error = MUSHERR_NOQENTRIES;
663			return;
664		}
665		rq->req_header.rqs_entry_count++;
666		MEMZERO((void *)crq, sizeof (*crq));
667		crq->req_header.rqs_entry_count = 1;
668		crq->req_header.rqs_entry_type = RQSTYPE_DATASEG;
669
670		seglim = 0;
671		while (datalen > 0 && seglim < ISP_CDSEG && dm_segs != eseg) {
672			crq->req_dataseg[seglim].ds_base =
673			    dm_segs->ds_addr;
674			crq->req_dataseg[seglim].ds_count =
675			    dm_segs->ds_len;
676			rq->req_seg_count++;
677			dm_segs++;
678			seglim++;
679			datalen -= dm_segs->ds_len;
680		}
681		isp_put_cont_req(isp, crq, cqe);
682		MEMORYBARRIER(isp, SYNC_REQUEST, onxti, QENTRY_LEN);
683	}
684	*mp->nxtip = nxti;
685}
686
687static int
688isp_sbus_dmasetup(ispsoftc_t *isp, struct ccb_scsiio *csio, ispreq_t *rq,
689	uint32_t *nxtip, uint32_t optr)
690{
691	ispreq_t *qep;
692	mush_t mush, *mp;
693	void (*eptr)(void *, bus_dma_segment_t *, int, int);
694
695	qep = (ispreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, isp->isp_reqidx);
696	eptr = dma2;
697
698
699	if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE ||
700	    (csio->dxfer_len == 0)) {
701		rq->req_seg_count = 1;
702		goto mbxsync;
703	}
704
705	/*
706	 * Do a virtual grapevine step to collect info for
707	 * the callback dma allocation that we have to use...
708	 */
709	mp = &mush;
710	mp->isp = isp;
711	mp->cmd_token = csio;
712	mp->rq = rq;
713	mp->nxtip = nxtip;
714	mp->optr = optr;
715	mp->error = 0;
716
717	if ((csio->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
718		if ((csio->ccb_h.flags & CAM_DATA_PHYS) == 0) {
719			int error = bus_dmamap_load(isp->isp_osinfo.dmat,
720			    PISP_PCMD(csio)->dmap, csio->data_ptr,
721			    csio->dxfer_len, eptr, mp, 0);
722			if (error == EINPROGRESS) {
723				bus_dmamap_unload(isp->isp_osinfo.dmat,
724				    PISP_PCMD(csio)->dmap);
725				mp->error = EINVAL;
726				isp_prt(isp, ISP_LOGERR,
727				    "deferred dma allocation not supported");
728			} else if (error && mp->error == 0) {
729#ifdef	DIAGNOSTIC
730				isp_prt(isp, ISP_LOGERR,
731				    "error %d in dma mapping code", error);
732#endif
733				mp->error = error;
734			}
735		} else {
736			/* Pointer to physical buffer */
737			struct bus_dma_segment seg;
738			seg.ds_addr = (bus_addr_t)csio->data_ptr;
739			seg.ds_len = csio->dxfer_len;
740			(*eptr)(mp, &seg, 1, 0);
741		}
742	} else {
743		struct bus_dma_segment *segs;
744
745		if ((csio->ccb_h.flags & CAM_DATA_PHYS) != 0) {
746			isp_prt(isp, ISP_LOGERR,
747			    "Physical segment pointers unsupported");
748			mp->error = EINVAL;
749		} else if ((csio->ccb_h.flags & CAM_SG_LIST_PHYS) == 0) {
750			isp_prt(isp, ISP_LOGERR,
751			    "Virtual segment addresses unsupported");
752			mp->error = EINVAL;
753		} else {
754			/* Just use the segments provided */
755			segs = (struct bus_dma_segment *) csio->data_ptr;
756			(*eptr)(mp, segs, csio->sglist_cnt, 0);
757		}
758	}
759	if (mp->error) {
760		int retval = CMD_COMPLETE;
761		if (mp->error == MUSHERR_NOQENTRIES) {
762			retval = CMD_EAGAIN;
763		} else if (mp->error == EFBIG) {
764			XS_SETERR(csio, CAM_REQ_TOO_BIG);
765		} else if (mp->error == EINVAL) {
766			XS_SETERR(csio, CAM_REQ_INVALID);
767		} else {
768			XS_SETERR(csio, CAM_UNREC_HBA_ERROR);
769		}
770		return (retval);
771	}
772mbxsync:
773	if (isp->isp_dblev & ISP_LOGDEBUG1) {
774		isp_print_bytes(isp, "Request Queue Entry", QENTRY_LEN, rq);
775	}
776	switch (rq->req_header.rqs_entry_type) {
777	case RQSTYPE_REQUEST:
778		isp_put_request(isp, rq, qep);
779		break;
780	case RQSTYPE_CMDONLY:
781		isp_put_extended_request(isp, (ispextreq_t *)rq,
782		    (ispextreq_t *)qep);
783		break;
784	}
785	return (CMD_QUEUED);
786}
787
788static void
789isp_sbus_reset0(ispsoftc_t *isp)
790{
791	ISP_DISABLE_INTS(isp);
792}
793
794static void
795isp_sbus_reset1(ispsoftc_t *isp)
796{
797	ISP_ENABLE_INTS(isp);
798}
799
800static void
801isp_sbus_dumpregs(ispsoftc_t *isp, const char *msg)
802{
803	if (msg)
804		printf("%s: %s\n", device_get_nameunit(isp->isp_dev), msg);
805	else
806		printf("%s:\n", device_get_nameunit(isp->isp_dev));
807	printf("    biu_conf1=%x", ISP_READ(isp, BIU_CONF1));
808	printf(" biu_icr=%x biu_isr=%x biu_sema=%x ", ISP_READ(isp, BIU_ICR),
809	    ISP_READ(isp, BIU_ISR), ISP_READ(isp, BIU_SEMA));
810	printf("risc_hccr=%x\n", ISP_READ(isp, HCCR));
811
812
813	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
814	printf("    cdma_conf=%x cdma_sts=%x cdma_fifostat=%x\n",
815		ISP_READ(isp, CDMA_CONF), ISP_READ(isp, CDMA_STATUS),
816		ISP_READ(isp, CDMA_FIFO_STS));
817	printf("    ddma_conf=%x ddma_sts=%x ddma_fifostat=%x\n",
818		ISP_READ(isp, DDMA_CONF), ISP_READ(isp, DDMA_STATUS),
819		ISP_READ(isp, DDMA_FIFO_STS));
820	printf("    sxp_int=%x sxp_gross=%x sxp(scsi_ctrl)=%x\n",
821		ISP_READ(isp, SXP_INTERRUPT),
822		ISP_READ(isp, SXP_GROSS_ERR),
823		ISP_READ(isp, SXP_PINS_CTRL));
824	ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
825	printf("    mbox regs: %x %x %x %x %x\n",
826	    ISP_READ(isp, OUTMAILBOX0), ISP_READ(isp, OUTMAILBOX1),
827	    ISP_READ(isp, OUTMAILBOX2), ISP_READ(isp, OUTMAILBOX3),
828	    ISP_READ(isp, OUTMAILBOX4));
829}
830