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