twe.c revision 240209
1193323Sed/*-
2193323Sed * Copyright (c) 2000 Michael Smith
3193323Sed * Copyright (c) 2003 Paul Saab
4193323Sed * Copyright (c) 2003 Vinod Kashyap
5193323Sed * Copyright (c) 2000 BSDi
6193323Sed * All rights reserved.
7193323Sed *
8193323Sed * Redistribution and use in source and binary forms, with or without
9193323Sed * modification, are permitted provided that the following conditions
10193323Sed * are met:
11193323Sed * 1. Redistributions of source code must retain the above copyright
12193323Sed *    notice, this list of conditions and the following disclaimer.
13193323Sed * 2. Redistributions in binary form must reproduce the above copyright
14193323Sed *    notice, this list of conditions and the following disclaimer in the
15193323Sed *    documentation and/or other materials provided with the distribution.
16193323Sed *
17193323Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18193323Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19193323Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20193323Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21193323Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22193323Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23249423Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24193323Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25193323Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26193323Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27193323Sed * SUCH DAMAGE.
28193323Sed *
29193323Sed *	$FreeBSD: head/sys/dev/twe/twe.c 240209 2012-09-07 18:41:19Z jhb $
30193323Sed */
31193323Sed
32193323Sed/*
33193323Sed * Driver for the 3ware Escalade family of IDE RAID controllers.
34193323Sed */
35193323Sed
36193323Sed#include <dev/twe/twe_compat.h>
37193323Sed#include <dev/twe/twereg.h>
38193323Sed#include <dev/twe/tweio.h>
39193323Sed#include <dev/twe/twevar.h>
40193323Sed#define TWE_DEFINE_TABLES
41193323Sed#include <dev/twe/twe_tables.h>
42218893Sdim
43218893Sdim/*
44193323Sed * Command submission.
45193323Sed */
46218893Sdimstatic int	twe_get_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t *result);
47218893Sdimstatic int	twe_get_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t *result);
48193323Sedstatic int	twe_get_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t *result);
49193323Sedstatic void	*twe_get_param(struct twe_softc *sc, int table_id, int parameter_id, size_t size,
50193323Sed					       void (* func)(struct twe_request *tr));
51193323Sed#ifdef TWE_SHUTDOWN_NOTIFICATION
52193323Sedstatic int	twe_set_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t value);
53193323Sed#endif
54193323Sed#if 0
55193323Sedstatic int	twe_set_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t value);
56193323Sedstatic int	twe_set_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t value);
57193323Sed#endif
58193323Sedstatic int	twe_set_param(struct twe_softc *sc, int table_id, int param_id, int param_size,
59218893Sdim					      void *data);
60193323Sedstatic int	twe_init_connection(struct twe_softc *sc, int mode);
61218893Sdimstatic int	twe_wait_request(struct twe_request *tr);
62218893Sdimstatic int	twe_immediate_request(struct twe_request *tr, int usetmp);
63218893Sdimstatic void	twe_completeio(struct twe_request *tr);
64218893Sdimstatic void	twe_reset(struct twe_softc *sc);
65218893Sdimstatic int	twe_add_unit(struct twe_softc *sc, int unit);
66218893Sdimstatic int	twe_del_unit(struct twe_softc *sc, int unit);
67218893Sdim
68193323Sed/*
69193323Sed * Command I/O to controller.
70218893Sdim */
71193323Sedstatic void	twe_done(struct twe_softc *sc, int startio);
72218893Sdimstatic void	twe_complete(struct twe_softc *sc);
73218893Sdimstatic int	twe_wait_status(struct twe_softc *sc, u_int32_t status, int timeout);
74218893Sdimstatic int	twe_drain_response_queue(struct twe_softc *sc);
75218893Sdimstatic int	twe_check_bits(struct twe_softc *sc, u_int32_t status_reg);
76218893Sdimstatic int	twe_soft_reset(struct twe_softc *sc);
77218893Sdim
78218893Sdim/*
79218893Sdim * Interrupt handling.
80218893Sdim */
81218893Sdimstatic void	twe_host_intr(struct twe_softc *sc);
82193323Sedstatic void	twe_attention_intr(struct twe_softc *sc);
83193323Sedstatic void	twe_command_intr(struct twe_softc *sc);
84193323Sed
85193323Sed/*
86193323Sed * Asynchronous event handling.
87193323Sed */
88193323Sedstatic int	twe_fetch_aen(struct twe_softc *sc);
89193323Sedstatic void	twe_handle_aen(struct twe_request *tr);
90193323Sedstatic void	twe_enqueue_aen(struct twe_softc *sc, u_int16_t aen);
91193323Sedstatic u_int16_t	twe_dequeue_aen(struct twe_softc *sc);
92193323Sedstatic int	twe_drain_aen_queue(struct twe_softc *sc);
93193323Sedstatic int	twe_find_aen(struct twe_softc *sc, u_int16_t aen);
94193323Sed
95193323Sed/*
96193323Sed * Command buffer management.
97193323Sed */
98193323Sedstatic int	twe_get_request(struct twe_softc *sc, struct twe_request **tr);
99193323Sedstatic void	twe_release_request(struct twe_request *tr);
100193323Sed
101193323Sed/*
102193323Sed * Debugging.
103193323Sed */
104193323Sedstatic char 	*twe_format_aen(struct twe_softc *sc, u_int16_t aen);
105193323Sedstatic int	twe_report_request(struct twe_request *tr);
106193323Sedstatic void	twe_panic(struct twe_softc *sc, char *reason);
107193323Sed
108193323Sed/********************************************************************************
109193323Sed ********************************************************************************
110193323Sed                                                                Public Interfaces
111193323Sed ********************************************************************************
112226633Sdim ********************************************************************************/
113226633Sdim
114193323Sed/********************************************************************************
115193323Sed * Initialise the controller, set up driver data structures.
116193323Sed */
117193323Sedint
118193323Sedtwe_setup(struct twe_softc *sc)
119193323Sed{
120193323Sed    struct twe_request	*tr;
121193323Sed    TWE_Command		*cmd;
122193323Sed    u_int32_t		status_reg;
123193323Sed    int			i;
124193323Sed
125193323Sed    debug_called(4);
126193323Sed
127193323Sed    /*
128193323Sed     * Initialise request queues.
129193323Sed     */
130193323Sed    twe_initq_free(sc);
131193323Sed    twe_initq_bio(sc);
132193323Sed    twe_initq_ready(sc);
133193323Sed    twe_initq_busy(sc);
134193323Sed    twe_initq_complete(sc);
135193323Sed    sc->twe_wait_aen = -1;
136193323Sed
137193323Sed    /*
138193323Sed     * Allocate request structures up front.
139193323Sed     */
140193323Sed    for (i = 0; i < TWE_Q_LENGTH; i++) {
141193323Sed	if ((tr = twe_allocate_request(sc, i)) == NULL)
142193323Sed	    return(ENOMEM);
143193323Sed	/*
144193323Sed	 * Set global defaults that won't change.
145193323Sed	 */
146193323Sed	cmd = TWE_FIND_COMMAND(tr);
147193323Sed	cmd->generic.host_id = sc->twe_host_id;		/* controller-assigned host ID */
148226633Sdim	cmd->generic.request_id = i;			/* our index number */
149226633Sdim	sc->twe_lookup[i] = tr;
150226633Sdim
151212904Sdim	/*
152212904Sdim	 * Put command onto the freelist.
153193323Sed	 */
154193323Sed	TWE_IO_LOCK(sc);
155193323Sed	twe_release_request(tr);
156193323Sed	TWE_IO_UNLOCK(sc);
157193323Sed    }
158193323Sed    TWE_IO_LOCK(sc);
159193323Sed
160193323Sed    /*
161193323Sed     * Check status register for errors, clear them.
162193323Sed     */
163193323Sed    status_reg = TWE_STATUS(sc);
164193323Sed    twe_check_bits(sc, status_reg);
165193323Sed
166193323Sed    /*
167193323Sed     * Wait for the controller to come ready.
168193323Sed     */
169193323Sed    if (twe_wait_status(sc, TWE_STATUS_MICROCONTROLLER_READY, 60)) {
170193323Sed	TWE_IO_UNLOCK(sc);
171193323Sed	twe_printf(sc, "microcontroller not ready\n");
172193323Sed	return(ENXIO);
173193323Sed    }
174193323Sed
175193323Sed    /*
176193323Sed     * Disable interrupts from the card.
177193323Sed     */
178193323Sed    twe_disable_interrupts(sc);
179198090Srdivacky
180193323Sed    /*
181193323Sed     * Soft reset the controller, look for the AEN acknowledging the reset,
182193323Sed     * check for errors, drain the response queue.
183198090Srdivacky     */
184198090Srdivacky    for (i = 0; i < TWE_MAX_RESET_TRIES; i++) {
185193323Sed
186193323Sed	if (i > 0)
187193323Sed	    twe_printf(sc, "reset %d failed, trying again\n", i);
188193323Sed
189193323Sed	if (!twe_soft_reset(sc))
190193323Sed	    break;			/* reset process complete */
191193323Sed    }
192193323Sed    TWE_IO_UNLOCK(sc);
193193323Sed    /* did we give up? */
194193323Sed    if (i >= TWE_MAX_RESET_TRIES) {
195193323Sed	twe_printf(sc, "can't initialise controller, giving up\n");
196193323Sed	return(ENXIO);
197193323Sed    }
198193323Sed
199193323Sed    return(0);
200193323Sed}
201193323Sed
202193323Sedstatic int
203193323Sedtwe_add_unit(struct twe_softc *sc, int unit)
204193323Sed{
205193323Sed    struct twe_drive		*dr;
206218893Sdim    int				table, error = 0;
207218893Sdim    u_int16_t			dsize;
208193323Sed    TWE_Param			*drives = NULL, *param = NULL;
209193323Sed    TWE_Array_Descriptor	*ud;
210193323Sed
211193323Sed    TWE_CONFIG_ASSERT_LOCKED(sc);
212193323Sed    if (unit < 0 || unit > TWE_MAX_UNITS)
213193323Sed	return (EINVAL);
214193323Sed
215193323Sed    /*
216193323Sed     * The controller is in a safe state, so try to find drives attached to it.
217193323Sed     */
218193323Sed    TWE_IO_LOCK(sc);
219193323Sed    if ((drives = twe_get_param(sc, TWE_PARAM_UNITSUMMARY, TWE_PARAM_UNITSUMMARY_Status,
220193323Sed				TWE_MAX_UNITS, NULL)) == NULL) {
221193323Sed	TWE_IO_UNLOCK(sc);
222193323Sed	twe_printf(sc, "can't detect attached units\n");
223193323Sed	return (EIO);
224193323Sed    }
225193323Sed
226193323Sed    dr = &sc->twe_drive[unit];
227243830Sdim    /* check that the drive is online */
228243830Sdim    if (!(drives->data[unit] & TWE_PARAM_UNITSTATUS_Online)) {
229193323Sed	TWE_IO_UNLOCK(sc);
230193323Sed	error = ENXIO;
231193323Sed	goto out;
232193323Sed    }
233193323Sed
234193323Sed    table = TWE_PARAM_UNITINFO + unit;
235193323Sed
236193323Sed    if (twe_get_param_4(sc, table, TWE_PARAM_UNITINFO_Capacity, &dr->td_size)) {
237193323Sed	TWE_IO_UNLOCK(sc);
238193323Sed	twe_printf(sc, "error fetching capacity for unit %d\n", unit);
239193323Sed	error = EIO;
240193323Sed	goto out;
241193323Sed    }
242193323Sed    if (twe_get_param_1(sc, table, TWE_PARAM_UNITINFO_Status, &dr->td_state)) {
243193323Sed	TWE_IO_UNLOCK(sc);
244193323Sed	twe_printf(sc, "error fetching state for unit %d\n", unit);
245193323Sed	error = EIO;
246193323Sed	goto out;
247193323Sed    }
248193323Sed    if (twe_get_param_2(sc, table, TWE_PARAM_UNITINFO_DescriptorSize, &dsize)) {
249193323Sed	TWE_IO_UNLOCK(sc);
250193323Sed	twe_printf(sc, "error fetching descriptor size for unit %d\n", unit);
251218893Sdim	error = EIO;
252218893Sdim	goto out;
253193323Sed    }
254226633Sdim    if ((param = twe_get_param(sc, table, TWE_PARAM_UNITINFO_Descriptor, dsize - 3, NULL)) == NULL) {
255226633Sdim	TWE_IO_UNLOCK(sc);
256226633Sdim	twe_printf(sc, "error fetching descriptor for unit %d\n", unit);
257226633Sdim	error = EIO;
258226633Sdim	goto out;
259226633Sdim    }
260221345Sdim    ud = (TWE_Array_Descriptor *)param->data;
261193323Sed    dr->td_type = ud->configuration;
262193323Sed    dr->td_stripe = ud->stripe_size;
263193323Sed
264193323Sed    /* build synthetic geometry as per controller internal rules */
265234353Sdim    if (dr->td_size > 0x200000) {
266193323Sed	dr->td_heads = 255;
267193323Sed	dr->td_sectors = 63;
268193323Sed    } else {
269218893Sdim	dr->td_heads = 64;
270218893Sdim	dr->td_sectors = 32;
271226633Sdim    }
272193323Sed    dr->td_cylinders = dr->td_size / (dr->td_heads * dr->td_sectors);
273193323Sed    dr->td_twe_unit = unit;
274198090Srdivacky    TWE_IO_UNLOCK(sc);
275193323Sed
276193323Sed    error = twe_attach_drive(sc, dr);
277193323Sed
278193323Sedout:
279193323Sed    if (param != NULL)
280193323Sed	free(param, M_DEVBUF);
281198090Srdivacky    if (drives != NULL)
282198090Srdivacky	free(drives, M_DEVBUF);
283198090Srdivacky    return (error);
284198090Srdivacky}
285198090Srdivacky
286221345Sdimstatic int
287198090Srdivackytwe_del_unit(struct twe_softc *sc, int unit)
288198090Srdivacky{
289198090Srdivacky    int error;
290198090Srdivacky
291200581Srdivacky    TWE_CONFIG_ASSERT_LOCKED(sc);
292200581Srdivacky    if (unit < 0 || unit >= TWE_MAX_UNITS)
293200581Srdivacky	return (ENXIO);
294198090Srdivacky
295193323Sed    if (sc->twe_drive[unit].td_disk == NULL)
296193323Sed	return (ENXIO);
297193323Sed
298198090Srdivacky    error = twe_detach_drive(sc, unit);
299198090Srdivacky    return (error);
300198090Srdivacky}
301198090Srdivacky
302193323Sed/********************************************************************************
303198090Srdivacky * Locate disk devices and attach children to them.
304198090Srdivacky */
305193323Sedvoid
306193323Sedtwe_init(struct twe_softc *sc)
307193323Sed{
308193323Sed    int 		i;
309193323Sed
310193323Sed    /*
311193323Sed     * Scan for drives
312193323Sed     */
313193323Sed    TWE_CONFIG_LOCK(sc);
314193323Sed    for (i = 0; i < TWE_MAX_UNITS; i++)
315193323Sed	twe_add_unit(sc, i);
316193323Sed    TWE_CONFIG_UNLOCK(sc);
317193323Sed
318193323Sed    /*
319193323Sed     * Initialise connection with controller.
320193323Sed     */
321193323Sed    TWE_IO_LOCK(sc);
322193323Sed    twe_init_connection(sc, TWE_INIT_MESSAGE_CREDITS);
323193323Sed
324218893Sdim#ifdef TWE_SHUTDOWN_NOTIFICATION
325193323Sed    /*
326193323Sed     * Tell the controller we support shutdown notification.
327193323Sed     */
328193323Sed    twe_set_param_1(sc, TWE_PARAM_FEATURES, TWE_PARAM_FEATURES_DriverShutdown, 1);
329193323Sed#endif
330193323Sed
331226633Sdim    /*
332193323Sed     * Mark controller up and ready to run.
333193323Sed     */
334193323Sed    sc->twe_state &= ~TWE_STATE_SHUTDOWN;
335193323Sed
336218893Sdim    /*
337218893Sdim     * Finally enable interrupts.
338193323Sed     */
339193323Sed    twe_enable_interrupts(sc);
340193323Sed    TWE_IO_UNLOCK(sc);
341193323Sed}
342193323Sed
343226633Sdim/********************************************************************************
344193323Sed * Stop the controller
345193323Sed */
346193323Sedvoid
347193323Sedtwe_deinit(struct twe_softc *sc)
348193323Sed{
349193323Sed    /*
350193323Sed     * Mark the controller as shutting down, and disable any further interrupts.
351193323Sed     */
352193323Sed    TWE_IO_ASSERT_LOCKED(sc);
353193323Sed    sc->twe_state |= TWE_STATE_SHUTDOWN;
354193323Sed    twe_disable_interrupts(sc);
355218893Sdim
356218893Sdim#ifdef TWE_SHUTDOWN_NOTIFICATION
357218893Sdim    /*
358193323Sed     * Disconnect from the controller
359193323Sed     */
360193323Sed    twe_init_connection(sc, TWE_SHUTDOWN_MESSAGE_CREDITS);
361218893Sdim#endif
362218893Sdim}
363218893Sdim
364193323Sed/*******************************************************************************
365193323Sed * Take an interrupt, or be poked by other code to look for interrupt-worthy
366193323Sed * status.
367193323Sed */
368193323Sedvoid
369218893Sdimtwe_intr(struct twe_softc *sc)
370193323Sed{
371193323Sed    u_int32_t		status_reg;
372193323Sed
373193323Sed    debug_called(4);
374193323Sed
375193323Sed    /*
376193323Sed     * Collect current interrupt status.
377193323Sed     */
378193323Sed    status_reg = TWE_STATUS(sc);
379193323Sed    twe_check_bits(sc, status_reg);
380193323Sed
381193323Sed    /*
382193323Sed     * Dispatch based on interrupt status
383193323Sed     */
384193323Sed    if (status_reg & TWE_STATUS_HOST_INTERRUPT)
385193323Sed	twe_host_intr(sc);
386193323Sed    if (status_reg & TWE_STATUS_ATTENTION_INTERRUPT)
387193323Sed	twe_attention_intr(sc);
388193323Sed    if (status_reg & TWE_STATUS_COMMAND_INTERRUPT)
389193323Sed	twe_command_intr(sc);
390193323Sed    if (status_reg & TWE_STATUS_RESPONSE_INTERRUPT)
391193323Sed	twe_done(sc, 1);
392193323Sed};
393193323Sed
394193323Sed/********************************************************************************
395193323Sed * Pull as much work off the softc's work queue as possible and give it to the
396193323Sed * controller.
397193323Sed */
398193323Sedvoid
399193323Sedtwe_startio(struct twe_softc *sc)
400198090Srdivacky{
401193323Sed    struct twe_request	*tr;
402193323Sed    TWE_Command		*cmd;
403193323Sed    struct bio		*bp;
404193323Sed    int			error;
405193323Sed
406193323Sed    debug_called(4);
407193323Sed
408193323Sed    TWE_IO_ASSERT_LOCKED(sc);
409193323Sed    if (sc->twe_state & (TWE_STATE_CTLR_BUSY | TWE_STATE_FRZN))
410198090Srdivacky	return;
411193323Sed
412193323Sed    /* spin until something prevents us from doing any work */
413193323Sed    for (;;) {
414193323Sed
415193323Sed	/* try to get a command that's already ready to go */
416218893Sdim	tr = twe_dequeue_ready(sc);
417218893Sdim
418193323Sed	/* build a command from an outstanding bio */
419193323Sed	if (tr == NULL) {
420218893Sdim
421193323Sed	    /* get a command to handle the bio with */
422193323Sed	    if (twe_get_request(sc, &tr))
423193323Sed		break;
424218893Sdim
425218893Sdim	    /* see if there's work to be done */
426193323Sed	    if ((bp = twe_dequeue_bio(sc)) == NULL) {
427226633Sdim		twe_release_request(tr);
428193323Sed		break;
429193323Sed	    }
430198090Srdivacky
431193323Sed	    /* connect the bio to the command */
432193323Sed	    tr->tr_complete = twe_completeio;
433193323Sed	    tr->tr_private = bp;
434193323Sed	    tr->tr_data = bp->bio_data;
435193323Sed	    tr->tr_length = bp->bio_bcount;
436193323Sed	    cmd = TWE_FIND_COMMAND(tr);
437193323Sed	    if (bp->bio_cmd == BIO_READ) {
438		tr->tr_flags |= TWE_CMD_DATAIN;
439		cmd->io.opcode = TWE_OP_READ;
440	    } else {
441		tr->tr_flags |= TWE_CMD_DATAOUT;
442		cmd->io.opcode = TWE_OP_WRITE;
443	    }
444
445	    /* build a suitable I/O command (assumes 512-byte rounded transfers) */
446	    cmd->io.size = 3;
447	    cmd->io.unit = *(int *)(bp->bio_driver1);
448	    cmd->io.block_count = (tr->tr_length + TWE_BLOCK_SIZE - 1) / TWE_BLOCK_SIZE;
449	    cmd->io.lba = bp->bio_pblkno;
450	}
451
452	/* did we find something to do? */
453	if (tr == NULL)
454	    break;
455
456	/* try to map and submit the command to controller */
457	error = twe_map_request(tr);
458
459	if (error != 0) {
460	    if (error == EBUSY)
461		break;
462	    tr->tr_status = TWE_CMD_ERROR;
463	    if (tr->tr_private != NULL) {
464		bp = (struct bio *)(tr->tr_private);
465		bp->bio_error = error;
466		bp->bio_flags |= BIO_ERROR;
467		tr->tr_private = NULL;
468		twed_intr(bp);
469	        twe_release_request(tr);
470	    } else if (tr->tr_flags & TWE_CMD_SLEEPER)
471		wakeup_one(tr); /* wakeup the sleeping owner */
472	}
473    }
474}
475
476/********************************************************************************
477 * Write blocks from memory to disk, for system crash dumps.
478 */
479int
480twe_dump_blocks(struct twe_softc *sc, int unit,	u_int32_t lba, void *data, int nblks)
481{
482    struct twe_request	*tr;
483    TWE_Command		*cmd;
484    int			error;
485
486    if (twe_get_request(sc, &tr))
487	return(ENOMEM);
488
489    tr->tr_data = data;
490    tr->tr_status = TWE_CMD_SETUP;
491    tr->tr_length = nblks * TWE_BLOCK_SIZE;
492    tr->tr_flags = TWE_CMD_DATAOUT;
493
494    cmd = TWE_FIND_COMMAND(tr);
495    cmd->io.opcode = TWE_OP_WRITE;
496    cmd->io.size = 3;
497    cmd->io.unit = unit;
498    cmd->io.block_count = nblks;
499    cmd->io.lba = lba;
500
501    error = twe_immediate_request(tr, 0);
502    if (error == 0)
503	if (twe_report_request(tr))
504	    error = EIO;
505    twe_release_request(tr);
506    return(error);
507}
508
509/********************************************************************************
510 * Handle controller-specific control operations.
511 */
512int
513twe_ioctl(struct twe_softc *sc, u_long ioctlcmd, void *addr)
514{
515    struct twe_usercommand	*tu = (struct twe_usercommand *)addr;
516    struct twe_paramcommand	*tp = (struct twe_paramcommand *)addr;
517    struct twe_drivecommand	*td = (struct twe_drivecommand *)addr;
518    union twe_statrequest	*ts = (union twe_statrequest *)addr;
519    TWE_Param			*param;
520    TWE_Command			*cmd;
521    void			*data;
522    u_int16_t			*aen_code = (u_int16_t *)addr;
523    struct twe_request		*tr;
524    u_int8_t			srid;
525    int				error;
526    size_t			tr_length;
527
528    error = 0;
529    switch(ioctlcmd) {
530	/* handle a command from userspace */
531    case TWEIO_COMMAND:
532	/*
533	 * if there's a data buffer, allocate and copy it in.
534	 * Must be in multipled of 512 bytes.
535	 */
536	tr_length = roundup2(tu->tu_size, 512);
537	if (tr_length > 0) {
538	    data = malloc(tr_length, M_DEVBUF, M_WAITOK);
539	    error = copyin(tu->tu_data, data, tu->tu_size);
540	    if (error) {
541		free(data, M_DEVBUF);
542		break;
543	    }
544	} else
545	    data = NULL;
546
547	/* get a request */
548	TWE_IO_LOCK(sc);
549	while (twe_get_request(sc, &tr))
550	    mtx_sleep(sc, &sc->twe_io_lock, PPAUSE, "twioctl", hz);
551
552	/*
553	 * Save the command's request ID, copy the user-supplied command in,
554	 * restore the request ID.
555	 */
556	cmd = TWE_FIND_COMMAND(tr);
557	srid = cmd->generic.request_id;
558	bcopy(&tu->tu_command, cmd, sizeof(TWE_Command));
559	cmd->generic.request_id = srid;
560
561	tr->tr_length = tr_length;
562	tr->tr_data = data;
563	if (tr->tr_length > 0) {
564	    tr->tr_flags |= TWE_CMD_DATAIN | TWE_CMD_DATAOUT;
565	}
566
567	/* run the command */
568	error = twe_wait_request(tr);
569	TWE_IO_UNLOCK(sc);
570	if (error)
571	    goto cmd_done;
572
573	/* copy the command out again */
574	bcopy(cmd, &tu->tu_command, sizeof(TWE_Command));
575
576	/* if there was a data buffer, copy it out */
577	if (tr->tr_length > 0)
578	    error = copyout(tr->tr_data, tu->tu_data, tu->tu_size);
579
580    cmd_done:
581	/* free resources */
582	if (tr->tr_data != NULL)
583	    free(tr->tr_data, M_DEVBUF);
584	TWE_IO_LOCK(sc);
585	twe_release_request(tr);
586	TWE_IO_UNLOCK(sc);
587
588	break;
589
590	/* fetch statistics counter */
591    case TWEIO_STATS:
592	switch (ts->ts_item) {
593#ifdef TWE_PERFORMANCE_MONITOR
594	case TWEQ_FREE:
595	case TWEQ_BIO:
596	case TWEQ_READY:
597	case TWEQ_BUSY:
598	case TWEQ_COMPLETE:
599	    TWE_IO_LOCK(sc);
600	    bcopy(&sc->twe_qstat[ts->ts_item], &ts->ts_qstat, sizeof(struct twe_qstat));
601	    TWE_IO_UNLOCK(sc);
602	    break;
603#endif
604	default:
605	    error = ENOENT;
606	    break;
607	}
608	break;
609
610	/* poll for an AEN */
611    case TWEIO_AEN_POLL:
612	TWE_IO_LOCK(sc);
613	*aen_code = twe_dequeue_aen(sc);
614	TWE_IO_UNLOCK(sc);
615	break;
616
617	/* wait for another AEN to show up */
618    case TWEIO_AEN_WAIT:
619	TWE_IO_LOCK(sc);
620	while ((*aen_code = twe_dequeue_aen(sc)) == TWE_AEN_QUEUE_EMPTY) {
621	    error = mtx_sleep(&sc->twe_aen_queue, &sc->twe_io_lock, PRIBIO | PCATCH,
622		"tweaen", 0);
623	    if (error == EINTR)
624		break;
625	}
626	TWE_IO_UNLOCK(sc);
627	break;
628
629    case TWEIO_GET_PARAM:
630	TWE_IO_LOCK(sc);
631	param = twe_get_param(sc, tp->tp_table_id, tp->tp_param_id, tp->tp_size, NULL);
632	TWE_IO_UNLOCK(sc);
633	if (param == NULL) {
634	    twe_printf(sc, "TWEIO_GET_PARAM failed for 0x%x/0x%x/%d\n",
635		       tp->tp_table_id, tp->tp_param_id, tp->tp_size);
636	    error = EINVAL;
637	} else {
638	    if (param->parameter_size_bytes > tp->tp_size) {
639		twe_printf(sc, "TWEIO_GET_PARAM parameter too large (%d > %d)\n",
640			   param->parameter_size_bytes, tp->tp_size);
641		error = EFAULT;
642	    } else {
643		error = copyout(param->data, tp->tp_data, param->parameter_size_bytes);
644	    }
645	    free(param, M_DEVBUF);
646	}
647	break;
648
649    case TWEIO_SET_PARAM:
650	data = malloc(tp->tp_size, M_DEVBUF, M_WAITOK);
651	error = copyin(tp->tp_data, data, tp->tp_size);
652	if (error == 0) {
653	    TWE_IO_LOCK(sc);
654	    error = twe_set_param(sc, tp->tp_table_id, tp->tp_param_id, tp->tp_size, data);
655	    TWE_IO_UNLOCK(sc);
656	}
657	free(data, M_DEVBUF);
658	break;
659
660    case TWEIO_RESET:
661	TWE_IO_LOCK(sc);
662	twe_reset(sc);
663	TWE_IO_UNLOCK(sc);
664	break;
665
666    case TWEIO_ADD_UNIT:
667	TWE_CONFIG_LOCK(sc);
668	error = twe_add_unit(sc, td->td_unit);
669	TWE_CONFIG_UNLOCK(sc);
670	break;
671
672    case TWEIO_DEL_UNIT:
673	TWE_CONFIG_LOCK(sc);
674	error = twe_del_unit(sc, td->td_unit);
675	TWE_CONFIG_UNLOCK(sc);
676	break;
677
678	/* XXX implement ATA PASSTHROUGH */
679
680	/* nothing we understand */
681    default:
682	error = ENOTTY;
683    }
684
685    return(error);
686}
687
688/********************************************************************************
689 * Enable the useful interrupts from the controller.
690 */
691void
692twe_enable_interrupts(struct twe_softc *sc)
693{
694    sc->twe_state |= TWE_STATE_INTEN;
695    TWE_CONTROL(sc,
696	       TWE_CONTROL_CLEAR_ATTENTION_INTERRUPT |
697	       TWE_CONTROL_UNMASK_RESPONSE_INTERRUPT |
698	       TWE_CONTROL_ENABLE_INTERRUPTS);
699}
700
701/********************************************************************************
702 * Disable interrupts from the controller.
703 */
704void
705twe_disable_interrupts(struct twe_softc *sc)
706{
707    TWE_CONTROL(sc, TWE_CONTROL_DISABLE_INTERRUPTS);
708    sc->twe_state &= ~TWE_STATE_INTEN;
709}
710
711/********************************************************************************
712 ********************************************************************************
713                                                               Command Submission
714 ********************************************************************************
715 ********************************************************************************/
716
717/********************************************************************************
718 * Read integer parameter table entries.
719 */
720static int
721twe_get_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t *result)
722{
723    TWE_Param	*param;
724
725    if ((param = twe_get_param(sc, table_id, param_id, 1, NULL)) == NULL)
726	return(ENOENT);
727    *result = *(u_int8_t *)param->data;
728    free(param, M_DEVBUF);
729    return(0);
730}
731
732static int
733twe_get_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t *result)
734{
735    TWE_Param	*param;
736
737    if ((param = twe_get_param(sc, table_id, param_id, 2, NULL)) == NULL)
738	return(ENOENT);
739    *result = *(u_int16_t *)param->data;
740    free(param, M_DEVBUF);
741    return(0);
742}
743
744static int
745twe_get_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t *result)
746{
747    TWE_Param	*param;
748
749    if ((param = twe_get_param(sc, table_id, param_id, 4, NULL)) == NULL)
750	return(ENOENT);
751    *result = *(u_int32_t *)param->data;
752    free(param, M_DEVBUF);
753    return(0);
754}
755
756/********************************************************************************
757 * Perform a TWE_OP_GET_PARAM command.  If a callback function is provided, it
758 * will be called with the command when it's completed.  If no callback is
759 * provided, we will wait for the command to complete and then return just the data.
760 * The caller is responsible for freeing the data when done with it.
761 */
762static void *
763twe_get_param(struct twe_softc *sc, int table_id, int param_id, size_t param_size,
764	      void (* func)(struct twe_request *tr))
765{
766    struct twe_request	*tr;
767    TWE_Command		*cmd;
768    TWE_Param		*param;
769    int			error;
770
771    debug_called(4);
772
773    TWE_IO_ASSERT_LOCKED(sc);
774    tr = NULL;
775    param = NULL;
776
777    /* get a command */
778    if (twe_get_request(sc, &tr))
779	goto err;
780
781    /* get a buffer */
782    if ((param = (TWE_Param *)malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_NOWAIT)) == NULL)
783	goto err;
784    tr->tr_data = param;
785    tr->tr_length = TWE_SECTOR_SIZE;
786    tr->tr_flags = TWE_CMD_DATAIN | TWE_CMD_DATAOUT;
787
788    /* build the command for the controller */
789    cmd = TWE_FIND_COMMAND(tr);
790    cmd->param.opcode = TWE_OP_GET_PARAM;
791    cmd->param.size = 2;
792    cmd->param.unit = 0;
793    cmd->param.param_count = 1;
794
795    /* fill in the outbound parameter data */
796    param->table_id = table_id;
797    param->parameter_id = param_id;
798    param->parameter_size_bytes = param_size;
799
800    /* submit the command and either wait or let the callback handle it */
801    if (func == NULL) {
802	/* XXX could use twe_wait_request here if interrupts were enabled? */
803	error = twe_immediate_request(tr, 1 /* usetmp */);
804	if (error == 0) {
805	    if (twe_report_request(tr))
806		goto err;
807	} else {
808	    goto err;
809	}
810	twe_release_request(tr);
811	return(param);
812    } else {
813	tr->tr_complete = func;
814	error = twe_map_request(tr);
815	if ((error == 0) || (error == EBUSY))
816	    return(func);
817    }
818
819    /* something failed */
820err:
821    debug(1, "failed");
822    if (tr != NULL)
823	twe_release_request(tr);
824    if (param != NULL)
825	free(param, M_DEVBUF);
826    return(NULL);
827}
828
829/********************************************************************************
830 * Set integer parameter table entries.
831 */
832#ifdef TWE_SHUTDOWN_NOTIFICATION
833static int
834twe_set_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t value)
835{
836    return(twe_set_param(sc, table_id, param_id, sizeof(value), &value));
837}
838#endif
839
840#if 0
841static int
842twe_set_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t value)
843{
844    return(twe_set_param(sc, table_id, param_id, sizeof(value), &value));
845}
846
847static int
848twe_set_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t value)
849{
850    return(twe_set_param(sc, table_id, param_id, sizeof(value), &value));
851}
852#endif
853
854/********************************************************************************
855 * Perform a TWE_OP_SET_PARAM command, returns nonzero on error.
856 */
857static int
858twe_set_param(struct twe_softc *sc, int table_id, int param_id, int param_size, void *data)
859{
860    struct twe_request	*tr;
861    TWE_Command		*cmd;
862    TWE_Param		*param;
863    int			error;
864
865    debug_called(4);
866
867    TWE_IO_ASSERT_LOCKED(sc);
868    tr = NULL;
869    param = NULL;
870    error = ENOMEM;
871
872    /* get a command */
873    if (twe_get_request(sc, &tr))
874	goto out;
875
876    /* get a buffer */
877    if ((param = (TWE_Param *)malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_NOWAIT)) == NULL)
878	goto out;
879    tr->tr_data = param;
880    tr->tr_length = TWE_SECTOR_SIZE;
881    tr->tr_flags = TWE_CMD_DATAIN | TWE_CMD_DATAOUT;
882
883    /* build the command for the controller */
884    cmd = TWE_FIND_COMMAND(tr);
885    cmd->param.opcode = TWE_OP_SET_PARAM;
886    cmd->param.size = 2;
887    cmd->param.unit = 0;
888    cmd->param.param_count = 1;
889
890    /* fill in the outbound parameter data */
891    param->table_id = table_id;
892    param->parameter_id = param_id;
893    param->parameter_size_bytes = param_size;
894    bcopy(data, param->data, param_size);
895
896    /* XXX could use twe_wait_request here if interrupts were enabled? */
897    error = twe_immediate_request(tr, 1 /* usetmp */);
898    if (error == 0) {
899	if (twe_report_request(tr))
900	    error = EIO;
901    }
902
903out:
904    if (tr != NULL)
905	twe_release_request(tr);
906    if (param != NULL)
907	free(param, M_DEVBUF);
908    return(error);
909}
910
911/********************************************************************************
912 * Perform a TWE_OP_INIT_CONNECTION command, returns nonzero on error.
913 *
914 * Typically called with interrupts disabled.
915 */
916static int
917twe_init_connection(struct twe_softc *sc, int mode)
918{
919    struct twe_request	*tr;
920    TWE_Command		*cmd;
921    int			error;
922
923    debug_called(4);
924
925    TWE_IO_ASSERT_LOCKED(sc);
926
927    /* get a command */
928    if (twe_get_request(sc, &tr))
929	return(0);
930
931    /* build the command */
932    cmd = TWE_FIND_COMMAND(tr);
933    cmd->initconnection.opcode = TWE_OP_INIT_CONNECTION;
934    cmd->initconnection.size = 3;
935    cmd->initconnection.host_id = 0;
936    cmd->initconnection.message_credits = mode;
937    cmd->initconnection.response_queue_pointer = 0;
938
939    /* submit the command */
940    error = twe_immediate_request(tr, 0 /* usetmp */);
941    twe_release_request(tr);
942
943    if (mode == TWE_INIT_MESSAGE_CREDITS)
944	sc->twe_host_id = cmd->initconnection.host_id;
945    return(error);
946}
947
948/********************************************************************************
949 * Start the command (tr) and sleep waiting for it to complete.
950 *
951 * Successfully completed commands are dequeued.
952 */
953static int
954twe_wait_request(struct twe_request *tr)
955{
956
957    debug_called(4);
958
959    TWE_IO_ASSERT_LOCKED(tr->tr_sc);
960    tr->tr_flags |= TWE_CMD_SLEEPER;
961    tr->tr_status = TWE_CMD_BUSY;
962    twe_enqueue_ready(tr);
963    twe_startio(tr->tr_sc);
964    while (tr->tr_status == TWE_CMD_BUSY)
965	mtx_sleep(tr, &tr->tr_sc->twe_io_lock, PRIBIO, "twewait", 0);
966
967    return(tr->tr_status != TWE_CMD_COMPLETE);
968}
969
970/********************************************************************************
971 * Start the command (tr) and busy-wait for it to complete.
972 * This should only be used when interrupts are actually disabled (although it
973 * will work if they are not).
974 */
975static int
976twe_immediate_request(struct twe_request *tr, int usetmp)
977{
978    struct twe_softc *sc;
979    int		error;
980    int		count = 0;
981
982    debug_called(4);
983
984    sc = tr->tr_sc;
985
986    if (usetmp && (tr->tr_data != NULL)) {
987	tr->tr_flags |= TWE_CMD_IMMEDIATE;
988	if (tr->tr_length > MAXBSIZE)
989	    return (EINVAL);
990	bcopy(tr->tr_data, sc->twe_immediate, tr->tr_length);
991    }
992    tr->tr_status = TWE_CMD_BUSY;
993    if ((error = twe_map_request(tr)) != 0)
994	if (error != EBUSY)
995	    return(error);
996
997    /* Wait up to 5 seconds for the command to complete */
998    while ((count++ < 5000) && (tr->tr_status == TWE_CMD_BUSY)){
999	DELAY(1000);
1000	twe_done(sc, 1);
1001    }
1002    if (usetmp && (tr->tr_data != NULL))
1003	bcopy(sc->twe_immediate, tr->tr_data, tr->tr_length);
1004
1005    return(tr->tr_status != TWE_CMD_COMPLETE);
1006}
1007
1008/********************************************************************************
1009 * Handle completion of an I/O command.
1010 */
1011static void
1012twe_completeio(struct twe_request *tr)
1013{
1014    TWE_Command		*cmd = TWE_FIND_COMMAND(tr);
1015    struct twe_softc	*sc = tr->tr_sc;
1016    struct bio		*bp = tr->tr_private;
1017
1018    debug_called(4);
1019
1020    if (tr->tr_status == TWE_CMD_COMPLETE) {
1021
1022	if (cmd->generic.status)
1023	    if (twe_report_request(tr)) {
1024		bp->bio_error = EIO;
1025		bp->bio_flags |= BIO_ERROR;
1026	    }
1027
1028    } else {
1029	twe_panic(sc, "twe_completeio on incomplete command");
1030    }
1031    tr->tr_private = NULL;
1032    twed_intr(bp);
1033    twe_release_request(tr);
1034}
1035
1036/********************************************************************************
1037 * Reset the controller and pull all the active commands back onto the ready
1038 * queue.  Used to restart a controller that's exhibiting bad behaviour.
1039 */
1040static void
1041twe_reset(struct twe_softc *sc)
1042{
1043    struct twe_request	*tr;
1044    int			i;
1045
1046    /*
1047     * Sleep for a short period to allow AENs to be signalled.
1048     */
1049    mtx_sleep(sc, &sc->twe_io_lock, PRIBIO, "twereset", hz);
1050
1051    /*
1052     * Disable interrupts from the controller, and mask any accidental entry
1053     * into our interrupt handler.
1054     */
1055    twe_printf(sc, "controller reset in progress...\n");
1056    twe_disable_interrupts(sc);
1057
1058    /*
1059     * Try to soft-reset the controller.
1060     */
1061    for (i = 0; i < TWE_MAX_RESET_TRIES; i++) {
1062
1063	if (i > 0)
1064	    twe_printf(sc, "reset %d failed, trying again\n", i);
1065
1066	if (!twe_soft_reset(sc))
1067	    break;			/* reset process complete */
1068    }
1069    /* did we give up? */
1070    if (i >= TWE_MAX_RESET_TRIES) {
1071	twe_printf(sc, "can't reset controller, giving up\n");
1072	goto out;
1073    }
1074
1075    /*
1076     * Move all of the commands that were busy back to the ready queue.
1077     */
1078    i = 0;
1079    while ((tr = twe_dequeue_busy(sc)) != NULL) {
1080	twe_enqueue_ready(tr);
1081	i++;
1082    }
1083
1084    /*
1085     * Kick the controller to start things going again, then re-enable interrupts.
1086     */
1087    twe_startio(sc);
1088    twe_printf(sc, "controller reset done, %d commands restarted\n", i);
1089
1090out:
1091    twe_enable_interrupts(sc);
1092}
1093
1094/********************************************************************************
1095 ********************************************************************************
1096                                                        Command I/O to Controller
1097 ********************************************************************************
1098 ********************************************************************************/
1099
1100/********************************************************************************
1101 * Try to deliver (tr) to the controller.
1102 *
1103 * Can be called at any interrupt level, with or without interrupts enabled.
1104 */
1105int
1106twe_start(struct twe_request *tr)
1107{
1108    struct twe_softc	*sc = tr->tr_sc;
1109    TWE_Command		*cmd;
1110    int			i;
1111    u_int32_t		status_reg;
1112
1113    debug_called(4);
1114
1115    if (!dumping)
1116	TWE_IO_ASSERT_LOCKED(sc);
1117
1118    /* mark the command as currently being processed */
1119    tr->tr_status = TWE_CMD_BUSY;
1120    cmd = TWE_FIND_COMMAND(tr);
1121
1122    /*
1123     * Spin briefly waiting for the controller to come ready
1124     *
1125     * XXX it might be more efficient to return EBUSY immediately
1126     *     and let the command be rescheduled.
1127     */
1128    for (i = 100000; (i > 0); i--) {
1129
1130	/* check to see if we can post a command */
1131	status_reg = TWE_STATUS(sc);
1132	twe_check_bits(sc, status_reg);
1133
1134	if (!(status_reg & TWE_STATUS_COMMAND_QUEUE_FULL)) {
1135	    twe_enqueue_busy(tr);
1136
1137	    TWE_COMMAND_QUEUE(sc, TWE_FIND_COMMANDPHYS(tr));
1138
1139	    /* move command to work queue */
1140#ifdef TWE_DEBUG
1141	    if (tr->tr_complete != NULL) {
1142		debug(3, "queued request %d with callback %p", cmd->generic.request_id, tr->tr_complete);
1143	    } else if (tr->tr_flags & TWE_CMD_SLEEPER) {
1144		debug(3, "queued request %d with wait channel %p", cmd->generic.request_id, tr);
1145	    } else {
1146		debug(3, "queued request %d for polling caller", cmd->generic.request_id);
1147	    }
1148#endif
1149	    return(0);
1150	} else if (!(status_reg & TWE_STATUS_RESPONSE_QUEUE_EMPTY) && i > 1)
1151	    twe_done(sc, 0);
1152    }
1153
1154    /*
1155     * We couldn't get the controller to take the command; try submitting it again later.
1156     * This should only happen if something is wrong with the controller, or if we have
1157     * overestimated the number of commands it can accept.  (Should we actually reject
1158     * the command at this point?)
1159     */
1160    return(EBUSY);
1161}
1162
1163/********************************************************************************
1164 * Poll the controller (sc) for completed commands.
1165 *
1166 * Can be called at any interrupt level, with or without interrupts enabled.
1167 */
1168static void
1169twe_done(struct twe_softc *sc, int startio)
1170{
1171    TWE_Response_Queue	rq;
1172    TWE_Command		*cmd;
1173    struct twe_request	*tr;
1174    int			found;
1175    u_int32_t		status_reg;
1176
1177    debug_called(5);
1178
1179    /* loop collecting completed commands */
1180    found = 0;
1181    for (;;) {
1182	status_reg = TWE_STATUS(sc);
1183	twe_check_bits(sc, status_reg);		/* XXX should this fail? */
1184
1185	if (!(status_reg & TWE_STATUS_RESPONSE_QUEUE_EMPTY)) {
1186	    found = 1;
1187	    rq = TWE_RESPONSE_QUEUE(sc);
1188	    tr = sc->twe_lookup[rq.u.response_id];	/* find command */
1189	    cmd = TWE_FIND_COMMAND(tr);
1190	    if (tr->tr_status != TWE_CMD_BUSY)
1191		twe_printf(sc, "completion event for nonbusy command\n");
1192	    tr->tr_status = TWE_CMD_COMPLETE;
1193	    debug(3, "completed request id %d with status %d",
1194		  cmd->generic.request_id, cmd->generic.status);
1195	    /* move to completed queue */
1196	    twe_remove_busy(tr);
1197	    twe_enqueue_complete(tr);
1198	    sc->twe_state &= ~TWE_STATE_CTLR_BUSY;
1199	} else {
1200	    break;					/* no response ready */
1201	}
1202    }
1203
1204    /* if we've completed any commands, try posting some more */
1205    if (found && startio)
1206	twe_startio(sc);
1207
1208    /* handle completion and timeouts */
1209    twe_complete(sc);		/* XXX use deferred completion? */
1210}
1211
1212/********************************************************************************
1213 * Perform post-completion processing for commands on (sc).
1214 *
1215 * This is split from twe_done as it can be safely deferred and run at a lower
1216 * priority level should facilities for such a thing become available.
1217 */
1218static void
1219twe_complete(struct twe_softc *sc)
1220{
1221    struct twe_request	*tr;
1222
1223    debug_called(5);
1224
1225    /*
1226     * Pull commands off the completed list, dispatch them appropriately
1227     */
1228    while ((tr = twe_dequeue_complete(sc)) != NULL) {
1229	/* unmap the command's data buffer */
1230	twe_unmap_request(tr);
1231
1232	/* dispatch to suit command originator */
1233	if (tr->tr_complete != NULL) {		/* completion callback */
1234	    debug(2, "call completion handler %p", tr->tr_complete);
1235	    tr->tr_complete(tr);
1236
1237	} else if (tr->tr_flags & TWE_CMD_SLEEPER) {	/* caller is asleep waiting */
1238	    debug(2, "wake up command owner on %p", tr);
1239	    wakeup_one(tr);
1240
1241	} else {					/* caller is polling command */
1242	    debug(2, "command left for owner");
1243	}
1244    }
1245}
1246
1247/********************************************************************************
1248 * Wait for (status) to be set in the controller status register for up to
1249 * (timeout) seconds.  Returns 0 if found, nonzero if we time out.
1250 *
1251 * Note: this busy-waits, rather than sleeping, since we may be called with
1252 * eg. clock interrupts masked.
1253 */
1254static int
1255twe_wait_status(struct twe_softc *sc, u_int32_t status, int timeout)
1256{
1257    time_t	expiry;
1258    u_int32_t	status_reg;
1259
1260    debug_called(4);
1261
1262    expiry = time_second + timeout;
1263
1264    do {
1265	status_reg = TWE_STATUS(sc);
1266	if (status_reg & status)	/* got the required bit(s)? */
1267	    return(0);
1268	DELAY(100000);
1269    } while (time_second <= expiry);
1270
1271    return(1);
1272}
1273
1274/********************************************************************************
1275 * Drain the response queue, which may contain responses to commands we know
1276 * nothing about.
1277 */
1278static int
1279twe_drain_response_queue(struct twe_softc *sc)
1280{
1281    TWE_Response_Queue	rq;
1282    u_int32_t		status_reg;
1283
1284    debug_called(4);
1285
1286    for (;;) {				/* XXX give up eventually? */
1287	status_reg = TWE_STATUS(sc);
1288	if (twe_check_bits(sc, status_reg))
1289	    return(1);
1290	if (status_reg & TWE_STATUS_RESPONSE_QUEUE_EMPTY)
1291	    return(0);
1292	rq = TWE_RESPONSE_QUEUE(sc);
1293    }
1294}
1295
1296/********************************************************************************
1297 * Soft-reset the controller
1298 */
1299static int
1300twe_soft_reset(struct twe_softc *sc)
1301{
1302    u_int32_t		status_reg;
1303
1304    debug_called(2);
1305
1306    TWE_IO_ASSERT_LOCKED(sc);
1307    TWE_SOFT_RESET(sc);
1308
1309    if (twe_wait_status(sc, TWE_STATUS_ATTENTION_INTERRUPT, 30)) {
1310	twe_printf(sc, "no attention interrupt\n");
1311	return(1);
1312    }
1313    TWE_CONTROL(sc, TWE_CONTROL_CLEAR_ATTENTION_INTERRUPT);
1314    if (twe_drain_aen_queue(sc)) {
1315	twe_printf(sc, "can't drain AEN queue\n");
1316	return(1);
1317    }
1318    if (twe_find_aen(sc, TWE_AEN_SOFT_RESET)) {
1319	twe_printf(sc, "reset not reported\n");
1320	return(1);
1321    }
1322    status_reg = TWE_STATUS(sc);
1323    if (TWE_STATUS_ERRORS(status_reg) || twe_check_bits(sc, status_reg)) {
1324	twe_printf(sc, "controller errors detected\n");
1325	return(1);
1326    }
1327    if (twe_drain_response_queue(sc)) {
1328	twe_printf(sc, "can't drain response queue\n");
1329	return(1);
1330    }
1331    return(0);
1332}
1333
1334/********************************************************************************
1335 ********************************************************************************
1336                                                               Interrupt Handling
1337 ********************************************************************************
1338 ********************************************************************************/
1339
1340/********************************************************************************
1341 * Host interrupt.
1342 *
1343 * XXX what does this mean?
1344 */
1345static void
1346twe_host_intr(struct twe_softc *sc)
1347{
1348    debug_called(4);
1349
1350    twe_printf(sc, "host interrupt\n");
1351    TWE_CONTROL(sc, TWE_CONTROL_CLEAR_HOST_INTERRUPT);
1352}
1353
1354/********************************************************************************
1355 * Attention interrupt.
1356 *
1357 * Signalled when the controller has one or more AENs for us.
1358 */
1359static void
1360twe_attention_intr(struct twe_softc *sc)
1361{
1362    debug_called(4);
1363
1364    /* instigate a poll for AENs */
1365    if (twe_fetch_aen(sc)) {
1366	twe_printf(sc, "error polling for signalled AEN\n");
1367    } else {
1368	TWE_CONTROL(sc, TWE_CONTROL_CLEAR_ATTENTION_INTERRUPT);
1369    }
1370}
1371
1372/********************************************************************************
1373 * Command interrupt.
1374 *
1375 * Signalled when the controller can handle more commands.
1376 */
1377static void
1378twe_command_intr(struct twe_softc *sc)
1379{
1380    debug_called(4);
1381
1382    /*
1383     * We don't use this, rather we try to submit commands when we receive
1384     * them, and when other commands have completed.  Mask it so we don't get
1385     * another one.
1386     */
1387    TWE_CONTROL(sc, TWE_CONTROL_MASK_COMMAND_INTERRUPT);
1388}
1389
1390/********************************************************************************
1391 ********************************************************************************
1392                                                      Asynchronous Event Handling
1393 ********************************************************************************
1394 ********************************************************************************/
1395
1396/********************************************************************************
1397 * Request an AEN from the controller.
1398 */
1399static int
1400twe_fetch_aen(struct twe_softc *sc)
1401{
1402
1403    debug_called(4);
1404
1405    if ((twe_get_param(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode, 2, twe_handle_aen)) == NULL)
1406	return(EIO);
1407    return(0);
1408}
1409
1410/********************************************************************************
1411 * Handle an AEN returned by the controller.
1412 */
1413static void
1414twe_handle_aen(struct twe_request *tr)
1415{
1416    struct twe_softc	*sc = tr->tr_sc;
1417    TWE_Param		*param;
1418    u_int16_t		aen;
1419
1420    debug_called(4);
1421
1422    /* XXX check for command success somehow? */
1423
1424    param = (TWE_Param *)tr->tr_data;
1425    aen = *(u_int16_t *)(param->data);
1426
1427    free(tr->tr_data, M_DEVBUF);
1428    twe_release_request(tr);
1429    twe_enqueue_aen(sc, aen);
1430
1431    /* XXX poll for more AENs? */
1432}
1433
1434/********************************************************************************
1435 * Pull AENs out of the controller and park them in the queue, in a context where
1436 * interrupts aren't active.  Return nonzero if we encounter any errors in the
1437 * process of obtaining all the available AENs.
1438 */
1439static int
1440twe_drain_aen_queue(struct twe_softc *sc)
1441{
1442    u_int16_t	aen;
1443
1444    TWE_IO_ASSERT_LOCKED(sc);
1445    for (;;) {
1446	if (twe_get_param_2(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode, &aen))
1447	    return(1);
1448	if (aen == TWE_AEN_QUEUE_EMPTY)
1449	    return(0);
1450	twe_enqueue_aen(sc, aen);
1451    }
1452}
1453
1454/********************************************************************************
1455 * Push an AEN that we've received onto the queue.
1456 *
1457 * Note that we have to lock this against reentrance, since it may be called
1458 * from both interrupt and non-interrupt context.
1459 *
1460 * If someone is waiting for the AEN we have, wake them up.
1461 */
1462static void
1463twe_enqueue_aen(struct twe_softc *sc, u_int16_t aen)
1464{
1465    char	*msg;
1466    int		next, nextnext;
1467
1468    debug_called(4);
1469
1470    TWE_IO_ASSERT_LOCKED(sc);
1471    if ((msg = twe_format_aen(sc, aen)) != NULL)
1472	twe_printf(sc, "AEN: <%s>\n", msg);
1473
1474    /* enqueue the AEN */
1475    next = ((sc->twe_aen_head + 1) % TWE_Q_LENGTH);
1476    nextnext = ((sc->twe_aen_head + 2) % TWE_Q_LENGTH);
1477
1478    /* check to see if this is the last free slot, and subvert the AEN if it is */
1479    if (nextnext == sc->twe_aen_tail)
1480	aen = TWE_AEN_QUEUE_FULL;
1481
1482    /* look to see if there's room for this AEN */
1483    if (next != sc->twe_aen_tail) {
1484	sc->twe_aen_queue[sc->twe_aen_head] = aen;
1485	sc->twe_aen_head = next;
1486    }
1487
1488    /* wake up anyone asleep on the queue */
1489    wakeup(&sc->twe_aen_queue);
1490
1491    /* anyone looking for this AEN? */
1492    if (sc->twe_wait_aen == aen) {
1493	sc->twe_wait_aen = -1;
1494	wakeup(&sc->twe_wait_aen);
1495    }
1496}
1497
1498/********************************************************************************
1499 * Pop an AEN off the queue, or return -1 if there are none left.
1500 *
1501 * We are more or less interrupt-safe, so don't block interrupts.
1502 */
1503static u_int16_t
1504twe_dequeue_aen(struct twe_softc *sc)
1505{
1506    u_int16_t	result;
1507
1508    debug_called(4);
1509
1510    TWE_IO_ASSERT_LOCKED(sc);
1511    if (sc->twe_aen_tail == sc->twe_aen_head) {
1512	result = TWE_AEN_QUEUE_EMPTY;
1513    } else {
1514	result = sc->twe_aen_queue[sc->twe_aen_tail];
1515	sc->twe_aen_tail = ((sc->twe_aen_tail + 1) % TWE_Q_LENGTH);
1516    }
1517    return(result);
1518}
1519
1520/********************************************************************************
1521 * Check to see if the requested AEN is in the queue.
1522 *
1523 * XXX we could probably avoid masking interrupts here
1524 */
1525static int
1526twe_find_aen(struct twe_softc *sc, u_int16_t aen)
1527{
1528    int		i, missing;
1529
1530    missing = 1;
1531    for (i = sc->twe_aen_tail; (i != sc->twe_aen_head) && missing; i = (i + 1) % TWE_Q_LENGTH) {
1532	if (sc->twe_aen_queue[i] == aen)
1533	    missing = 0;
1534    }
1535    return(missing);
1536}
1537
1538
1539#if 0	/* currently unused */
1540/********************************************************************************
1541 * Sleep waiting for at least (timeout) seconds until we see (aen) as
1542 * requested.  Returns nonzero on timeout or failure.
1543 *
1544 * XXX: this should not be used in cases where there may be more than one sleeper
1545 *      without a mechanism for registering multiple sleepers.
1546 */
1547static int
1548twe_wait_aen(struct twe_softc *sc, int aen, int timeout)
1549{
1550    time_t	expiry;
1551    int		found;
1552
1553    debug_called(4);
1554
1555    expiry = time_second + timeout;
1556    found = 0;
1557
1558    sc->twe_wait_aen = aen;
1559    do {
1560	twe_fetch_aen(sc);
1561	mtx_sleep(&sc->twe_wait_aen, &sc->twe_io_lock, PZERO, "twewaen", hz);
1562	if (sc->twe_wait_aen == -1)
1563	    found = 1;
1564    } while ((time_second <= expiry) && !found);
1565    return(!found);
1566}
1567#endif
1568
1569/********************************************************************************
1570 ********************************************************************************
1571                                                        Command Buffer Management
1572 ********************************************************************************
1573 ********************************************************************************/
1574
1575/********************************************************************************
1576 * Get a new command buffer.
1577 *
1578 * This will return NULL if all command buffers are in use.
1579 */
1580static int
1581twe_get_request(struct twe_softc *sc, struct twe_request **tr)
1582{
1583    TWE_Command		*cmd;
1584    debug_called(4);
1585
1586    if (!dumping)
1587	TWE_IO_ASSERT_LOCKED(sc);
1588
1589    /* try to reuse an old buffer */
1590    *tr = twe_dequeue_free(sc);
1591
1592    /* initialise some fields to their defaults */
1593    if (*tr != NULL) {
1594	cmd = TWE_FIND_COMMAND(*tr);
1595	(*tr)->tr_data = NULL;
1596	(*tr)->tr_private = NULL;
1597	(*tr)->tr_status = TWE_CMD_SETUP;		/* command is in setup phase */
1598	(*tr)->tr_flags = 0;
1599	(*tr)->tr_complete = NULL;
1600	cmd->generic.status = 0;			/* before submission to controller */
1601	cmd->generic.flags = 0;				/* not used */
1602    }
1603    return(*tr == NULL);
1604}
1605
1606/********************************************************************************
1607 * Release a command buffer for reuse.
1608 *
1609 */
1610static void
1611twe_release_request(struct twe_request *tr)
1612{
1613    debug_called(4);
1614
1615    if (!dumping)
1616	TWE_IO_ASSERT_LOCKED(tr->tr_sc);
1617    if (tr->tr_private != NULL)
1618	twe_panic(tr->tr_sc, "tr_private != NULL");
1619    twe_enqueue_free(tr);
1620}
1621
1622/********************************************************************************
1623 ********************************************************************************
1624                                                                        Debugging
1625 ********************************************************************************
1626 ********************************************************************************/
1627
1628/********************************************************************************
1629 * Print some information about the controller
1630 */
1631void
1632twe_describe_controller(struct twe_softc *sc)
1633{
1634    TWE_Param		*p[6];
1635    u_int8_t		ports;
1636    u_int32_t		size;
1637    int			i;
1638
1639    debug_called(2);
1640
1641    TWE_IO_LOCK(sc);
1642
1643    /* get the port count */
1644    twe_get_param_1(sc, TWE_PARAM_CONTROLLER, TWE_PARAM_CONTROLLER_PortCount, &ports);
1645
1646    /* get version strings */
1647    p[0] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_FW,   16, NULL);
1648    p[1] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_BIOS, 16, NULL);
1649    if (p[0] && p[1])
1650	 twe_printf(sc, "%d ports, Firmware %.16s, BIOS %.16s\n", ports, p[0]->data, p[1]->data);
1651
1652    if (bootverbose) {
1653	p[2] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_Mon,  16, NULL);
1654	p[3] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_PCB,  8, NULL);
1655	p[4] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_ATA,  8, NULL);
1656	p[5] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_PCI,  8, NULL);
1657
1658	if (p[2] && p[3] && p[4] && p[5])
1659	    twe_printf(sc, "Monitor %.16s, PCB %.8s, Achip %.8s, Pchip %.8s\n", p[2]->data, p[3]->data,
1660		p[4]->data, p[5]->data);
1661	if (p[2])
1662	    free(p[2], M_DEVBUF);
1663	if (p[3])
1664	    free(p[3], M_DEVBUF);
1665	if (p[4])
1666	    free(p[4], M_DEVBUF);
1667	if (p[5])
1668	    free(p[5], M_DEVBUF);
1669    }
1670    if (p[0])
1671	free(p[0], M_DEVBUF);
1672    if (p[1])
1673	free(p[1], M_DEVBUF);
1674
1675    /* print attached drives */
1676    if (bootverbose) {
1677	p[0] = twe_get_param(sc, TWE_PARAM_DRIVESUMMARY, TWE_PARAM_DRIVESUMMARY_Status, 16, NULL);
1678	for (i = 0; i < ports; i++) {
1679	    if (p[0]->data[i] != TWE_PARAM_DRIVESTATUS_Present)
1680		continue;
1681	    twe_get_param_4(sc, TWE_PARAM_DRIVEINFO + i, TWE_PARAM_DRIVEINFO_Size, &size);
1682	    p[1] = twe_get_param(sc, TWE_PARAM_DRIVEINFO + i, TWE_PARAM_DRIVEINFO_Model, 40, NULL);
1683	    if (p[1] != NULL) {
1684		twe_printf(sc, "port %d: %.40s %dMB\n", i, p[1]->data, size / 2048);
1685		free(p[1], M_DEVBUF);
1686	    } else {
1687		twe_printf(sc, "port %d, drive status unavailable\n", i);
1688	    }
1689	}
1690	if (p[0])
1691	    free(p[0], M_DEVBUF);
1692    }
1693    TWE_IO_UNLOCK(sc);
1694}
1695
1696/********************************************************************************
1697 * Look up a text description of a numeric code and return a pointer to same.
1698 */
1699char *
1700twe_describe_code(struct twe_code_lookup *table, u_int32_t code)
1701{
1702    int         i;
1703
1704    for (i = 0; table[i].string != NULL; i++)
1705	if (table[i].code == code)
1706	    return(table[i].string);
1707    return(table[i+1].string);
1708}
1709
1710/********************************************************************************
1711 * Complain if the status bits aren't what we're expecting.
1712 *
1713 * Rate-limit the complaints to at most one of each every five seconds, but
1714 * always return the correct status.
1715 */
1716static int
1717twe_check_bits(struct twe_softc *sc, u_int32_t status_reg)
1718{
1719    int			result;
1720    static time_t	lastwarn[2] = {0, 0};
1721
1722    /*
1723     * This can be a little problematic, as twe_panic may call twe_reset if
1724     * TWE_DEBUG is not set, which will call us again as part of the soft reset.
1725     */
1726    if ((status_reg & TWE_STATUS_PANIC_BITS) != 0) {
1727	twe_printf(sc, "FATAL STATUS BIT(S) %b\n", status_reg & TWE_STATUS_PANIC_BITS,
1728		   TWE_STATUS_BITS_DESCRIPTION);
1729	twe_panic(sc, "fatal status bits");
1730    }
1731
1732    result = 0;
1733    if ((status_reg & TWE_STATUS_EXPECTED_BITS) != TWE_STATUS_EXPECTED_BITS) {
1734	if (time_second > (lastwarn[0] + 5)) {
1735	    twe_printf(sc, "missing expected status bit(s) %b\n", ~status_reg & TWE_STATUS_EXPECTED_BITS,
1736		       TWE_STATUS_BITS_DESCRIPTION);
1737	    lastwarn[0] = time_second;
1738	}
1739	result = 1;
1740    }
1741
1742    if ((status_reg & TWE_STATUS_UNEXPECTED_BITS) != 0) {
1743	if (time_second > (lastwarn[1] + 5)) {
1744	    twe_printf(sc, "unexpected status bit(s) %b\n", status_reg & TWE_STATUS_UNEXPECTED_BITS,
1745		       TWE_STATUS_BITS_DESCRIPTION);
1746	    lastwarn[1] = time_second;
1747	}
1748	result = 1;
1749	if (status_reg & TWE_STATUS_PCI_PARITY_ERROR) {
1750	    twe_printf(sc, "PCI parity error: Reseat card, move card or buggy device present.\n");
1751	    twe_clear_pci_parity_error(sc);
1752	}
1753	if (status_reg & TWE_STATUS_PCI_ABORT) {
1754	    twe_printf(sc, "PCI abort, clearing.\n");
1755	    twe_clear_pci_abort(sc);
1756	}
1757    }
1758
1759    return(result);
1760}
1761
1762/********************************************************************************
1763 * Return a string describing (aen).
1764 *
1765 * The low 8 bits of the aen are the code, the high 8 bits give the unit number
1766 * where an AEN is specific to a unit.
1767 *
1768 * Note that we could expand this routine to handle eg. up/downgrading the status
1769 * of a drive if we had some idea of what the drive's initial status was.
1770 */
1771
1772static char *
1773twe_format_aen(struct twe_softc *sc, u_int16_t aen)
1774{
1775    device_t	child;
1776    char	*code, *msg;
1777
1778    code = twe_describe_code(twe_table_aen, TWE_AEN_CODE(aen));
1779    msg = code + 2;
1780
1781    switch (*code) {
1782    case 'q':
1783	if (!bootverbose)
1784	    return(NULL);
1785	/* FALLTHROUGH */
1786    case 'a':
1787	return(msg);
1788
1789    case 'c':
1790	if ((child = sc->twe_drive[TWE_AEN_UNIT(aen)].td_disk) != NULL) {
1791	    snprintf(sc->twe_aen_buf, sizeof(sc->twe_aen_buf), "twed%d: %s",
1792		device_get_unit(child), msg);
1793	} else {
1794	    snprintf(sc->twe_aen_buf, sizeof(sc->twe_aen_buf),
1795		"twe%d: %s for unknown unit %d", device_get_unit(sc->twe_dev),
1796		msg, TWE_AEN_UNIT(aen));
1797	}
1798	return(sc->twe_aen_buf);
1799
1800    case 'p':
1801	snprintf(sc->twe_aen_buf, sizeof(sc->twe_aen_buf),
1802	    "twe%d: port %d: %s", device_get_unit(sc->twe_dev),
1803	    TWE_AEN_UNIT(aen), msg);
1804	return(sc->twe_aen_buf);
1805
1806
1807    case 'x':
1808    default:
1809	break;
1810    }
1811    snprintf(sc->twe_aen_buf, sizeof(sc->twe_aen_buf), "unknown AEN 0x%x", aen);
1812    return(sc->twe_aen_buf);
1813}
1814
1815/********************************************************************************
1816 * Print a diagnostic if the status of the command warrants it, and return
1817 * either zero (command was ok) or nonzero (command failed).
1818 */
1819static int
1820twe_report_request(struct twe_request *tr)
1821{
1822    struct twe_softc	*sc = tr->tr_sc;
1823    TWE_Command		*cmd = TWE_FIND_COMMAND(tr);
1824    int			result = 0;
1825
1826    /*
1827     * Check the command status value and handle accordingly.
1828     */
1829    if (cmd->generic.status == TWE_STATUS_RESET) {
1830	/*
1831	 * The status code 0xff requests a controller reset.
1832	 */
1833	twe_printf(sc, "command returned with controller reset request\n");
1834	twe_reset(sc);
1835	result = 1;
1836    } else if (cmd->generic.status > TWE_STATUS_FATAL) {
1837	/*
1838	 * Fatal errors that don't require controller reset.
1839	 *
1840	 * We know a few special flags values.
1841	 */
1842	switch (cmd->generic.flags) {
1843	case 0x1b:
1844	    device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
1845			  "drive timeout");
1846	    break;
1847	case 0x51:
1848	    device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
1849			  "unrecoverable drive error");
1850	    break;
1851	default:
1852	    device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
1853			  "controller error - %s (flags = 0x%x)\n",
1854			  twe_describe_code(twe_table_status, cmd->generic.status),
1855			  cmd->generic.flags);
1856	    result = 1;
1857	}
1858    } else if (cmd->generic.status > TWE_STATUS_WARNING) {
1859	/*
1860	 * Warning level status.
1861	 */
1862	device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
1863		      "warning - %s (flags = 0x%x)\n",
1864		      twe_describe_code(twe_table_status, cmd->generic.status),
1865		      cmd->generic.flags);
1866    } else if (cmd->generic.status > 0x40) {
1867	/*
1868	 * Info level status.
1869	 */
1870	device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
1871		      "attention - %s (flags = 0x%x)\n",
1872		      twe_describe_code(twe_table_status, cmd->generic.status),
1873		      cmd->generic.flags);
1874    }
1875
1876    return(result);
1877}
1878
1879/********************************************************************************
1880 * Print some controller state to aid in debugging error/panic conditions
1881 */
1882void
1883twe_print_controller(struct twe_softc *sc)
1884{
1885    u_int32_t		status_reg;
1886
1887    status_reg = TWE_STATUS(sc);
1888    twe_printf(sc, "status   %b\n", status_reg, TWE_STATUS_BITS_DESCRIPTION);
1889    twe_printf(sc, "          current  max    min\n");
1890    twe_printf(sc, "free      %04d     %04d   %04d\n",
1891	sc->twe_qstat[TWEQ_FREE].q_length, sc->twe_qstat[TWEQ_FREE].q_max, sc->twe_qstat[TWEQ_FREE].q_min);
1892
1893    twe_printf(sc, "ready     %04d     %04d   %04d\n",
1894	sc->twe_qstat[TWEQ_READY].q_length, sc->twe_qstat[TWEQ_READY].q_max, sc->twe_qstat[TWEQ_READY].q_min);
1895
1896    twe_printf(sc, "busy      %04d     %04d   %04d\n",
1897	sc->twe_qstat[TWEQ_BUSY].q_length, sc->twe_qstat[TWEQ_BUSY].q_max, sc->twe_qstat[TWEQ_BUSY].q_min);
1898
1899    twe_printf(sc, "complete  %04d     %04d   %04d\n",
1900	sc->twe_qstat[TWEQ_COMPLETE].q_length, sc->twe_qstat[TWEQ_COMPLETE].q_max, sc->twe_qstat[TWEQ_COMPLETE].q_min);
1901
1902    twe_printf(sc, "bioq      %04d     %04d   %04d\n",
1903	sc->twe_qstat[TWEQ_BIO].q_length, sc->twe_qstat[TWEQ_BIO].q_max, sc->twe_qstat[TWEQ_BIO].q_min);
1904
1905    twe_printf(sc, "AEN queue head %d  tail %d\n", sc->twe_aen_head, sc->twe_aen_tail);
1906}
1907
1908static void
1909twe_panic(struct twe_softc *sc, char *reason)
1910{
1911    twe_print_controller(sc);
1912#ifdef TWE_DEBUG
1913    panic(reason);
1914#else
1915    twe_reset(sc);
1916#endif
1917}
1918
1919#if 0
1920/********************************************************************************
1921 * Print a request/command in human-readable format.
1922 */
1923static void
1924twe_print_request(struct twe_request *tr)
1925{
1926    struct twe_softc	*sc = tr->tr_sc;
1927    TWE_Command	*cmd = TWE_FIND_COMMAND(tr);
1928    int		i;
1929
1930    twe_printf(sc, "CMD: request_id %d  opcode <%s>  size %d  unit %d  host_id %d\n",
1931	       cmd->generic.request_id, twe_describe_code(twe_table_opcode, cmd->generic.opcode), cmd->generic.size,
1932	       cmd->generic.unit, cmd->generic.host_id);
1933    twe_printf(sc, " status %d  flags 0x%x  count %d  sgl_offset %d\n",
1934	       cmd->generic.status, cmd->generic.flags, cmd->generic.count, cmd->generic.sgl_offset);
1935
1936    switch(cmd->generic.opcode) {	/* XXX add more opcodes? */
1937    case TWE_OP_READ:
1938    case TWE_OP_WRITE:
1939	twe_printf(sc, " lba %d\n", cmd->io.lba);
1940	for (i = 0; (i < TWE_MAX_SGL_LENGTH) && (cmd->io.sgl[i].length != 0); i++)
1941	    twe_printf(sc, "  %d: 0x%x/%d\n",
1942		       i, cmd->io.sgl[i].address, cmd->io.sgl[i].length);
1943	break;
1944
1945    case TWE_OP_GET_PARAM:
1946    case TWE_OP_SET_PARAM:
1947	for (i = 0; (i < TWE_MAX_SGL_LENGTH) && (cmd->param.sgl[i].length != 0); i++)
1948	    twe_printf(sc, "  %d: 0x%x/%d\n",
1949		       i, cmd->param.sgl[i].address, cmd->param.sgl[i].length);
1950	break;
1951
1952    case TWE_OP_INIT_CONNECTION:
1953	twe_printf(sc, " response queue pointer 0x%x\n",
1954		   cmd->initconnection.response_queue_pointer);
1955	break;
1956
1957    default:
1958	break;
1959    }
1960    twe_printf(sc, " tr_command %p/0x%x  tr_data %p/0x%x,%d\n",
1961	       tr, TWE_FIND_COMMANDPHYS(tr), tr->tr_data, tr->tr_dataphys, tr->tr_length);
1962    twe_printf(sc, " tr_status %d  tr_flags 0x%x  tr_complete %p  tr_private %p\n",
1963	       tr->tr_status, tr->tr_flags, tr->tr_complete, tr->tr_private);
1964}
1965
1966#endif
1967