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