xref: /freebsd-13-stable/sys/dev/twe/twe.c (revision f8167e0404dab9ffeaca95853dd237ab7c587f82)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2000 Michael Smith
5  * Copyright (c) 2003 Paul Saab
6  * Copyright (c) 2003 Vinod Kashyap
7  * Copyright (c) 2000 BSDi
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * Driver for the 3ware Escalade family of IDE RAID controllers.
34  */
35 
36 #include <dev/twe/twe_compat.h>
37 #include <dev/twe/twereg.h>
38 #include <dev/twe/tweio.h>
39 #include <dev/twe/twevar.h>
40 #define TWE_DEFINE_TABLES
41 #include <dev/twe/twe_tables.h>
42 
43 /*
44  * Command submission.
45  */
46 static int	twe_get_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t *result);
47 static int	twe_get_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t *result);
48 static int	twe_get_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t *result);
49 static void	*twe_get_param(struct twe_softc *sc, int table_id, int parameter_id, size_t size,
50 					       void (* func)(struct twe_request *tr));
51 #ifdef TWE_SHUTDOWN_NOTIFICATION
52 static int	twe_set_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t value);
53 #endif
54 #if 0
55 static int	twe_set_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t value);
56 static int	twe_set_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t value);
57 #endif
58 static int	twe_set_param(struct twe_softc *sc, int table_id, int param_id, int param_size,
59 					      void *data);
60 static int	twe_init_connection(struct twe_softc *sc, int mode);
61 static int	twe_wait_request(struct twe_request *tr);
62 static int	twe_immediate_request(struct twe_request *tr, int usetmp);
63 static void	twe_completeio(struct twe_request *tr);
64 static void	twe_reset(struct twe_softc *sc);
65 static int	twe_add_unit(struct twe_softc *sc, int unit);
66 static int	twe_del_unit(struct twe_softc *sc, int unit);
67 
68 /*
69  * Command I/O to controller.
70  */
71 static void	twe_done(struct twe_softc *sc, int startio);
72 static void	twe_complete(struct twe_softc *sc);
73 static int	twe_wait_status(struct twe_softc *sc, u_int32_t status, int timeout);
74 static int	twe_drain_response_queue(struct twe_softc *sc);
75 static int	twe_check_bits(struct twe_softc *sc, u_int32_t status_reg);
76 static int	twe_soft_reset(struct twe_softc *sc);
77 
78 /*
79  * Interrupt handling.
80  */
81 static void	twe_host_intr(struct twe_softc *sc);
82 static void	twe_attention_intr(struct twe_softc *sc);
83 static void	twe_command_intr(struct twe_softc *sc);
84 
85 /*
86  * Asynchronous event handling.
87  */
88 static int	twe_fetch_aen(struct twe_softc *sc);
89 static void	twe_handle_aen(struct twe_request *tr);
90 static void	twe_enqueue_aen(struct twe_softc *sc, u_int16_t aen);
91 static u_int16_t	twe_dequeue_aen(struct twe_softc *sc);
92 static int	twe_drain_aen_queue(struct twe_softc *sc);
93 static int	twe_find_aen(struct twe_softc *sc, u_int16_t aen);
94 
95 /*
96  * Command buffer management.
97  */
98 static int	twe_get_request(struct twe_softc *sc, struct twe_request **tr);
99 static void	twe_release_request(struct twe_request *tr);
100 
101 /*
102  * Debugging.
103  */
104 static char 	*twe_format_aen(struct twe_softc *sc, u_int16_t aen);
105 static int	twe_report_request(struct twe_request *tr);
106 static void	twe_panic(struct twe_softc *sc, char *reason);
107 
108 /********************************************************************************
109  ********************************************************************************
110                                                                 Public Interfaces
111  ********************************************************************************
112  ********************************************************************************/
113 
114 /********************************************************************************
115  * Initialise the controller, set up driver data structures.
116  */
117 int
twe_setup(struct twe_softc * sc)118 twe_setup(struct twe_softc *sc)
119 {
120     struct twe_request	*tr;
121     TWE_Command		*cmd;
122     u_int32_t		status_reg;
123     int			i;
124 
125     debug_called(4);
126 
127     /*
128      * Initialise request queues.
129      */
130     twe_initq_free(sc);
131     twe_initq_bio(sc);
132     twe_initq_ready(sc);
133     twe_initq_busy(sc);
134     twe_initq_complete(sc);
135     sc->twe_wait_aen = -1;
136 
137     /*
138      * Allocate request structures up front.
139      */
140     for (i = 0; i < TWE_Q_LENGTH; i++) {
141 	if ((tr = twe_allocate_request(sc, i)) == NULL)
142 	    return(ENOMEM);
143 	/*
144 	 * Set global defaults that won't change.
145 	 */
146 	cmd = TWE_FIND_COMMAND(tr);
147 	cmd->generic.host_id = sc->twe_host_id;		/* controller-assigned host ID */
148 	cmd->generic.request_id = i;			/* our index number */
149 	sc->twe_lookup[i] = tr;
150 
151 	/*
152 	 * Put command onto the freelist.
153 	 */
154 	TWE_IO_LOCK(sc);
155 	twe_release_request(tr);
156 	TWE_IO_UNLOCK(sc);
157     }
158     TWE_IO_LOCK(sc);
159 
160     /*
161      * Check status register for errors, clear them.
162      */
163     status_reg = TWE_STATUS(sc);
164     twe_check_bits(sc, status_reg);
165 
166     /*
167      * Wait for the controller to come ready.
168      */
169     if (twe_wait_status(sc, TWE_STATUS_MICROCONTROLLER_READY, 60)) {
170 	TWE_IO_UNLOCK(sc);
171 	twe_printf(sc, "microcontroller not ready\n");
172 	return(ENXIO);
173     }
174 
175     /*
176      * Disable interrupts from the card.
177      */
178     twe_disable_interrupts(sc);
179 
180     /*
181      * Soft reset the controller, look for the AEN acknowledging the reset,
182      * check for errors, drain the response queue.
183      */
184     for (i = 0; i < TWE_MAX_RESET_TRIES; i++) {
185 	if (i > 0)
186 	    twe_printf(sc, "reset %d failed, trying again\n", i);
187 
188 	if (!twe_soft_reset(sc))
189 	    break;			/* reset process complete */
190     }
191     TWE_IO_UNLOCK(sc);
192     /* did we give up? */
193     if (i >= TWE_MAX_RESET_TRIES) {
194 	twe_printf(sc, "can't initialise controller, giving up\n");
195 	return(ENXIO);
196     }
197 
198     return(0);
199 }
200 
201 static int
twe_add_unit(struct twe_softc * sc,int unit)202 twe_add_unit(struct twe_softc *sc, int unit)
203 {
204     struct twe_drive		*dr;
205     int				table, error = 0;
206     u_int16_t			dsize;
207     TWE_Param			*drives = NULL, *param = NULL;
208     TWE_Array_Descriptor	*ud;
209 
210     TWE_CONFIG_ASSERT_LOCKED(sc);
211     if (unit < 0 || unit > TWE_MAX_UNITS)
212 	return (EINVAL);
213 
214     /*
215      * The controller is in a safe state, so try to find drives attached to it.
216      */
217     TWE_IO_LOCK(sc);
218     if ((drives = twe_get_param(sc, TWE_PARAM_UNITSUMMARY, TWE_PARAM_UNITSUMMARY_Status,
219 				TWE_MAX_UNITS, NULL)) == NULL) {
220 	TWE_IO_UNLOCK(sc);
221 	twe_printf(sc, "can't detect attached units\n");
222 	return (EIO);
223     }
224 
225     dr = &sc->twe_drive[unit];
226     /* check that the drive is online */
227     if (!(drives->data[unit] & TWE_PARAM_UNITSTATUS_Online)) {
228 	TWE_IO_UNLOCK(sc);
229 	error = ENXIO;
230 	goto out;
231     }
232 
233     table = TWE_PARAM_UNITINFO + unit;
234 
235     if (twe_get_param_4(sc, table, TWE_PARAM_UNITINFO_Capacity, &dr->td_size)) {
236 	TWE_IO_UNLOCK(sc);
237 	twe_printf(sc, "error fetching capacity for unit %d\n", unit);
238 	error = EIO;
239 	goto out;
240     }
241     if (twe_get_param_1(sc, table, TWE_PARAM_UNITINFO_Status, &dr->td_state)) {
242 	TWE_IO_UNLOCK(sc);
243 	twe_printf(sc, "error fetching state for unit %d\n", unit);
244 	error = EIO;
245 	goto out;
246     }
247     if (twe_get_param_2(sc, table, TWE_PARAM_UNITINFO_DescriptorSize, &dsize)) {
248 	TWE_IO_UNLOCK(sc);
249 	twe_printf(sc, "error fetching descriptor size for unit %d\n", unit);
250 	error = EIO;
251 	goto out;
252     }
253     if ((param = twe_get_param(sc, table, TWE_PARAM_UNITINFO_Descriptor, dsize - 3, NULL)) == NULL) {
254 	TWE_IO_UNLOCK(sc);
255 	twe_printf(sc, "error fetching descriptor for unit %d\n", unit);
256 	error = EIO;
257 	goto out;
258     }
259     ud = (TWE_Array_Descriptor *)param->data;
260     dr->td_type = ud->configuration;
261     dr->td_stripe = ud->stripe_size;
262 
263     /* build synthetic geometry as per controller internal rules */
264     if (dr->td_size > 0x200000) {
265 	dr->td_heads = 255;
266 	dr->td_sectors = 63;
267     } else {
268 	dr->td_heads = 64;
269 	dr->td_sectors = 32;
270     }
271     dr->td_cylinders = dr->td_size / (dr->td_heads * dr->td_sectors);
272     dr->td_twe_unit = unit;
273     TWE_IO_UNLOCK(sc);
274 
275     error = twe_attach_drive(sc, dr);
276 
277 out:
278     if (param != NULL)
279 	free(param, M_DEVBUF);
280     if (drives != NULL)
281 	free(drives, M_DEVBUF);
282     return (error);
283 }
284 
285 static int
twe_del_unit(struct twe_softc * sc,int unit)286 twe_del_unit(struct twe_softc *sc, int unit)
287 {
288     int error;
289 
290     TWE_CONFIG_ASSERT_LOCKED(sc);
291     if (unit < 0 || unit >= TWE_MAX_UNITS)
292 	return (ENXIO);
293 
294     if (sc->twe_drive[unit].td_disk == NULL)
295 	return (ENXIO);
296 
297     error = twe_detach_drive(sc, unit);
298     return (error);
299 }
300 
301 /********************************************************************************
302  * Locate disk devices and attach children to them.
303  */
304 void
twe_init(struct twe_softc * sc)305 twe_init(struct twe_softc *sc)
306 {
307     int 		i;
308 
309     /*
310      * Scan for drives
311      */
312     TWE_CONFIG_LOCK(sc);
313     for (i = 0; i < TWE_MAX_UNITS; i++)
314 	twe_add_unit(sc, i);
315     TWE_CONFIG_UNLOCK(sc);
316 
317     /*
318      * Initialise connection with controller.
319      */
320     TWE_IO_LOCK(sc);
321     twe_init_connection(sc, TWE_INIT_MESSAGE_CREDITS);
322 
323 #ifdef TWE_SHUTDOWN_NOTIFICATION
324     /*
325      * Tell the controller we support shutdown notification.
326      */
327     twe_set_param_1(sc, TWE_PARAM_FEATURES, TWE_PARAM_FEATURES_DriverShutdown, 1);
328 #endif
329 
330     /*
331      * Mark controller up and ready to run.
332      */
333     sc->twe_state &= ~TWE_STATE_SHUTDOWN;
334 
335     /*
336      * Finally enable interrupts.
337      */
338     twe_enable_interrupts(sc);
339     TWE_IO_UNLOCK(sc);
340 }
341 
342 /********************************************************************************
343  * Stop the controller
344  */
345 void
twe_deinit(struct twe_softc * sc)346 twe_deinit(struct twe_softc *sc)
347 {
348     /*
349      * Mark the controller as shutting down, and disable any further interrupts.
350      */
351     TWE_IO_ASSERT_LOCKED(sc);
352     sc->twe_state |= TWE_STATE_SHUTDOWN;
353     twe_disable_interrupts(sc);
354 
355 #ifdef TWE_SHUTDOWN_NOTIFICATION
356     /*
357      * Disconnect from the controller
358      */
359     twe_init_connection(sc, TWE_SHUTDOWN_MESSAGE_CREDITS);
360 #endif
361 }
362 
363 /*******************************************************************************
364  * Take an interrupt, or be poked by other code to look for interrupt-worthy
365  * status.
366  */
367 void
twe_intr(struct twe_softc * sc)368 twe_intr(struct twe_softc *sc)
369 {
370     u_int32_t		status_reg;
371 
372     debug_called(4);
373 
374     /*
375      * Collect current interrupt status.
376      */
377     status_reg = TWE_STATUS(sc);
378     twe_check_bits(sc, status_reg);
379 
380     /*
381      * Dispatch based on interrupt status
382      */
383     if (status_reg & TWE_STATUS_HOST_INTERRUPT)
384 	twe_host_intr(sc);
385     if (status_reg & TWE_STATUS_ATTENTION_INTERRUPT)
386 	twe_attention_intr(sc);
387     if (status_reg & TWE_STATUS_COMMAND_INTERRUPT)
388 	twe_command_intr(sc);
389     if (status_reg & TWE_STATUS_RESPONSE_INTERRUPT)
390 	twe_done(sc, 1);
391 };
392 
393 /********************************************************************************
394  * Pull as much work off the softc's work queue as possible and give it to the
395  * controller.
396  */
397 void
twe_startio(struct twe_softc * sc)398 twe_startio(struct twe_softc *sc)
399 {
400     struct twe_request	*tr;
401     TWE_Command		*cmd;
402     struct bio		*bp;
403     int			error;
404 
405     debug_called(4);
406 
407     TWE_IO_ASSERT_LOCKED(sc);
408     if (sc->twe_state & (TWE_STATE_CTLR_BUSY | TWE_STATE_FRZN))
409 	return;
410 
411     /* spin until something prevents us from doing any work */
412     for (;;) {
413 	/* try to get a command that's already ready to go */
414 	tr = twe_dequeue_ready(sc);
415 
416 	/* build a command from an outstanding bio */
417 	if (tr == NULL) {
418 
419 	    /* get a command to handle the bio with */
420 	    if (twe_get_request(sc, &tr))
421 		break;
422 
423 	    /* see if there's work to be done */
424 	    if ((bp = twe_dequeue_bio(sc)) == NULL) {
425 		twe_release_request(tr);
426 		break;
427 	    }
428 
429 	    /* connect the bio to the command */
430 	    tr->tr_complete = twe_completeio;
431 	    tr->tr_private = bp;
432 	    tr->tr_data = bp->bio_data;
433 	    tr->tr_length = bp->bio_bcount;
434 	    cmd = TWE_FIND_COMMAND(tr);
435 	    if (bp->bio_cmd == BIO_READ) {
436 		tr->tr_flags |= TWE_CMD_DATAIN;
437 		cmd->io.opcode = TWE_OP_READ;
438 	    } else if (bp->bio_cmd == BIO_WRITE) {
439 		tr->tr_flags |= TWE_CMD_DATAOUT;
440 		cmd->io.opcode = TWE_OP_WRITE;
441 	    } else {
442 		twe_release_request(tr);
443 		biofinish(bp, NULL, EOPNOTSUPP);
444 		break;
445 	    }
446 
447 	    /* build a suitable I/O command (assumes 512-byte rounded transfers) */
448 	    cmd->io.size = 3;
449 	    cmd->io.unit = *(int *)(bp->bio_driver1);
450 	    cmd->io.block_count = (tr->tr_length + TWE_BLOCK_SIZE - 1) / TWE_BLOCK_SIZE;
451 	    cmd->io.lba = bp->bio_pblkno;
452 	}
453 
454 	/* did we find something to do? */
455 	if (tr == NULL)
456 	    break;
457 
458 	/* try to map and submit the command to controller */
459 	error = twe_map_request(tr);
460 
461 	if (error != 0) {
462 	    if (error == EBUSY)
463 		break;
464 	    tr->tr_status = TWE_CMD_ERROR;
465 	    if (tr->tr_private != NULL) {
466 		bp = (struct bio *)(tr->tr_private);
467 		bp->bio_error = error;
468 		bp->bio_flags |= BIO_ERROR;
469 		tr->tr_private = NULL;
470 		twed_intr(bp);
471 	        twe_release_request(tr);
472 	    } else if (tr->tr_flags & TWE_CMD_SLEEPER)
473 		wakeup_one(tr); /* wakeup the sleeping owner */
474 	}
475     }
476 }
477 
478 /********************************************************************************
479  * Write blocks from memory to disk, for system crash dumps.
480  */
481 int
twe_dump_blocks(struct twe_softc * sc,int unit,u_int32_t lba,void * data,int nblks)482 twe_dump_blocks(struct twe_softc *sc, int unit,	u_int32_t lba, void *data, int nblks)
483 {
484     struct twe_request	*tr;
485     TWE_Command		*cmd;
486     int			error;
487 
488     if (twe_get_request(sc, &tr))
489 	return(ENOMEM);
490 
491     tr->tr_data = data;
492     tr->tr_status = TWE_CMD_SETUP;
493     tr->tr_length = nblks * TWE_BLOCK_SIZE;
494     tr->tr_flags = TWE_CMD_DATAOUT;
495 
496     cmd = TWE_FIND_COMMAND(tr);
497     cmd->io.opcode = TWE_OP_WRITE;
498     cmd->io.size = 3;
499     cmd->io.unit = unit;
500     cmd->io.block_count = nblks;
501     cmd->io.lba = lba;
502 
503     error = twe_immediate_request(tr, 0);
504     if (error == 0)
505 	if (twe_report_request(tr))
506 	    error = EIO;
507     twe_release_request(tr);
508     return(error);
509 }
510 
511 /********************************************************************************
512  * Handle controller-specific control operations.
513  */
514 int
twe_ioctl(struct twe_softc * sc,u_long ioctlcmd,void * addr)515 twe_ioctl(struct twe_softc *sc, u_long ioctlcmd, void *addr)
516 {
517     struct twe_usercommand	*tu = (struct twe_usercommand *)addr;
518     struct twe_paramcommand	*tp = (struct twe_paramcommand *)addr;
519     struct twe_drivecommand	*td = (struct twe_drivecommand *)addr;
520     union twe_statrequest	*ts = (union twe_statrequest *)addr;
521     TWE_Param			*param;
522     TWE_Command			*cmd;
523     void			*data;
524     u_int16_t			*aen_code = (u_int16_t *)addr;
525     struct twe_request		*tr;
526     u_int8_t			srid;
527     int				error;
528     size_t			tr_length;
529 
530     error = 0;
531     switch(ioctlcmd) {
532 	/* handle a command from userspace */
533     case TWEIO_COMMAND:
534 	/*
535 	 * if there's a data buffer, allocate and copy it in.
536 	 * Must be in multiplied of 512 bytes.
537 	 */
538 	tr_length = roundup2(tu->tu_size, 512);
539 	if (tr_length > 0) {
540 	    data = malloc(tr_length, M_DEVBUF, M_WAITOK);
541 	    error = copyin(tu->tu_data, data, tu->tu_size);
542 	    if (error) {
543 		free(data, M_DEVBUF);
544 		break;
545 	    }
546 	} else
547 	    data = NULL;
548 
549 	/* get a request */
550 	TWE_IO_LOCK(sc);
551 	while (twe_get_request(sc, &tr))
552 	    mtx_sleep(sc, &sc->twe_io_lock, PPAUSE, "twioctl", hz);
553 
554 	/*
555 	 * Save the command's request ID, copy the user-supplied command in,
556 	 * restore the request ID.
557 	 */
558 	cmd = TWE_FIND_COMMAND(tr);
559 	srid = cmd->generic.request_id;
560 	bcopy(&tu->tu_command, cmd, sizeof(TWE_Command));
561 	cmd->generic.request_id = srid;
562 
563 	tr->tr_length = tr_length;
564 	tr->tr_data = data;
565 	if (tr->tr_length > 0) {
566 	    tr->tr_flags |= TWE_CMD_DATAIN | TWE_CMD_DATAOUT;
567 	}
568 
569 	/* run the command */
570 	error = twe_wait_request(tr);
571 	TWE_IO_UNLOCK(sc);
572 	if (error)
573 	    goto cmd_done;
574 
575 	/* copy the command out again */
576 	bcopy(cmd, &tu->tu_command, sizeof(TWE_Command));
577 
578 	/* if there was a data buffer, copy it out */
579 	if (tr->tr_length > 0)
580 	    error = copyout(tr->tr_data, tu->tu_data, tu->tu_size);
581 
582     cmd_done:
583 	/* free resources */
584 	if (tr->tr_data != NULL)
585 	    free(tr->tr_data, M_DEVBUF);
586 	TWE_IO_LOCK(sc);
587 	twe_release_request(tr);
588 	TWE_IO_UNLOCK(sc);
589 
590 	break;
591 
592 	/* fetch statistics counter */
593     case TWEIO_STATS:
594 	switch (ts->ts_item) {
595 #ifdef TWE_PERFORMANCE_MONITOR
596 	case TWEQ_FREE:
597 	case TWEQ_BIO:
598 	case TWEQ_READY:
599 	case TWEQ_BUSY:
600 	case TWEQ_COMPLETE:
601 	    TWE_IO_LOCK(sc);
602 	    bcopy(&sc->twe_qstat[ts->ts_item], &ts->ts_qstat, sizeof(struct twe_qstat));
603 	    TWE_IO_UNLOCK(sc);
604 	    break;
605 #endif
606 	default:
607 	    error = ENOENT;
608 	    break;
609 	}
610 	break;
611 
612 	/* poll for an AEN */
613     case TWEIO_AEN_POLL:
614 	TWE_IO_LOCK(sc);
615 	*aen_code = twe_dequeue_aen(sc);
616 	TWE_IO_UNLOCK(sc);
617 	break;
618 
619 	/* wait for another AEN to show up */
620     case TWEIO_AEN_WAIT:
621 	TWE_IO_LOCK(sc);
622 	while ((*aen_code = twe_dequeue_aen(sc)) == TWE_AEN_QUEUE_EMPTY) {
623 	    error = mtx_sleep(&sc->twe_aen_queue, &sc->twe_io_lock, PRIBIO | PCATCH,
624 		"tweaen", 0);
625 	    if (error == EINTR)
626 		break;
627 	}
628 	TWE_IO_UNLOCK(sc);
629 	break;
630 
631     case TWEIO_GET_PARAM:
632 	TWE_IO_LOCK(sc);
633 	param = twe_get_param(sc, tp->tp_table_id, tp->tp_param_id, tp->tp_size, NULL);
634 	TWE_IO_UNLOCK(sc);
635 	if (param == NULL) {
636 	    twe_printf(sc, "TWEIO_GET_PARAM failed for 0x%x/0x%x/%d\n",
637 		       tp->tp_table_id, tp->tp_param_id, tp->tp_size);
638 	    error = EINVAL;
639 	} else {
640 	    if (param->parameter_size_bytes > tp->tp_size) {
641 		twe_printf(sc, "TWEIO_GET_PARAM parameter too large (%d > %d)\n",
642 			   param->parameter_size_bytes, tp->tp_size);
643 		error = EFAULT;
644 	    } else {
645 		error = copyout(param->data, tp->tp_data, param->parameter_size_bytes);
646 	    }
647 	    free(param, M_DEVBUF);
648 	}
649 	break;
650 
651     case TWEIO_SET_PARAM:
652 	data = malloc(tp->tp_size, M_DEVBUF, M_WAITOK);
653 	error = copyin(tp->tp_data, data, tp->tp_size);
654 	if (error == 0) {
655 	    TWE_IO_LOCK(sc);
656 	    error = twe_set_param(sc, tp->tp_table_id, tp->tp_param_id, tp->tp_size, data);
657 	    TWE_IO_UNLOCK(sc);
658 	}
659 	free(data, M_DEVBUF);
660 	break;
661 
662     case TWEIO_RESET:
663 	TWE_IO_LOCK(sc);
664 	twe_reset(sc);
665 	TWE_IO_UNLOCK(sc);
666 	break;
667 
668     case TWEIO_ADD_UNIT:
669 	TWE_CONFIG_LOCK(sc);
670 	error = twe_add_unit(sc, td->td_unit);
671 	TWE_CONFIG_UNLOCK(sc);
672 	break;
673 
674     case TWEIO_DEL_UNIT:
675 	TWE_CONFIG_LOCK(sc);
676 	error = twe_del_unit(sc, td->td_unit);
677 	TWE_CONFIG_UNLOCK(sc);
678 	break;
679 
680 	/* XXX implement ATA PASSTHROUGH */
681 
682 	/* nothing we understand */
683     default:
684 	error = ENOTTY;
685     }
686 
687     return(error);
688 }
689 
690 /********************************************************************************
691  * Enable the useful interrupts from the controller.
692  */
693 void
twe_enable_interrupts(struct twe_softc * sc)694 twe_enable_interrupts(struct twe_softc *sc)
695 {
696     sc->twe_state |= TWE_STATE_INTEN;
697     TWE_CONTROL(sc,
698 	       TWE_CONTROL_CLEAR_ATTENTION_INTERRUPT |
699 	       TWE_CONTROL_UNMASK_RESPONSE_INTERRUPT |
700 	       TWE_CONTROL_ENABLE_INTERRUPTS);
701 }
702 
703 /********************************************************************************
704  * Disable interrupts from the controller.
705  */
706 void
twe_disable_interrupts(struct twe_softc * sc)707 twe_disable_interrupts(struct twe_softc *sc)
708 {
709     TWE_CONTROL(sc, TWE_CONTROL_DISABLE_INTERRUPTS);
710     sc->twe_state &= ~TWE_STATE_INTEN;
711 }
712 
713 /********************************************************************************
714  ********************************************************************************
715                                                                Command Submission
716  ********************************************************************************
717  ********************************************************************************/
718 
719 /********************************************************************************
720  * Read integer parameter table entries.
721  */
722 static int
twe_get_param_1(struct twe_softc * sc,int table_id,int param_id,u_int8_t * result)723 twe_get_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t *result)
724 {
725     TWE_Param	*param;
726 
727     if ((param = twe_get_param(sc, table_id, param_id, 1, NULL)) == NULL)
728 	return(ENOENT);
729     *result = *(u_int8_t *)param->data;
730     free(param, M_DEVBUF);
731     return(0);
732 }
733 
734 static int
twe_get_param_2(struct twe_softc * sc,int table_id,int param_id,u_int16_t * result)735 twe_get_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t *result)
736 {
737     TWE_Param	*param;
738 
739     if ((param = twe_get_param(sc, table_id, param_id, 2, NULL)) == NULL)
740 	return(ENOENT);
741     *result = *(u_int16_t *)param->data;
742     free(param, M_DEVBUF);
743     return(0);
744 }
745 
746 static int
twe_get_param_4(struct twe_softc * sc,int table_id,int param_id,u_int32_t * result)747 twe_get_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t *result)
748 {
749     TWE_Param	*param;
750 
751     if ((param = twe_get_param(sc, table_id, param_id, 4, NULL)) == NULL)
752 	return(ENOENT);
753     *result = *(u_int32_t *)param->data;
754     free(param, M_DEVBUF);
755     return(0);
756 }
757 
758 /********************************************************************************
759  * Perform a TWE_OP_GET_PARAM command.  If a callback function is provided, it
760  * will be called with the command when it's completed.  If no callback is
761  * provided, we will wait for the command to complete and then return just the data.
762  * The caller is responsible for freeing the data when done with it.
763  */
764 static void *
twe_get_param(struct twe_softc * sc,int table_id,int param_id,size_t param_size,void (* func)(struct twe_request * tr))765 twe_get_param(struct twe_softc *sc, int table_id, int param_id, size_t param_size,
766 	      void (* func)(struct twe_request *tr))
767 {
768     struct twe_request	*tr;
769     TWE_Command		*cmd;
770     TWE_Param		*param;
771     int			error;
772 
773     debug_called(4);
774 
775     TWE_IO_ASSERT_LOCKED(sc);
776     tr = NULL;
777     param = NULL;
778 
779     /* get a command */
780     if (twe_get_request(sc, &tr))
781 	goto err;
782 
783     /* get a buffer */
784     if ((param = (TWE_Param *)malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_NOWAIT)) == NULL)
785 	goto err;
786     tr->tr_data = param;
787     tr->tr_length = TWE_SECTOR_SIZE;
788     tr->tr_flags = TWE_CMD_DATAIN | TWE_CMD_DATAOUT;
789 
790     /* build the command for the controller */
791     cmd = TWE_FIND_COMMAND(tr);
792     cmd->param.opcode = TWE_OP_GET_PARAM;
793     cmd->param.size = 2;
794     cmd->param.unit = 0;
795     cmd->param.param_count = 1;
796 
797     /* fill in the outbound parameter data */
798     param->table_id = table_id;
799     param->parameter_id = param_id;
800     param->parameter_size_bytes = param_size;
801 
802     /* submit the command and either wait or let the callback handle it */
803     if (func == NULL) {
804 	/* XXX could use twe_wait_request here if interrupts were enabled? */
805 	error = twe_immediate_request(tr, 1 /* usetmp */);
806 	if (error == 0) {
807 	    if (twe_report_request(tr))
808 		goto err;
809 	} else {
810 	    goto err;
811 	}
812 	twe_release_request(tr);
813 	return(param);
814     } else {
815 	tr->tr_complete = func;
816 	error = twe_map_request(tr);
817 	if ((error == 0) || (error == EBUSY))
818 	    return(func);
819     }
820 
821     /* something failed */
822 err:
823     debug(1, "failed");
824     if (tr != NULL)
825 	twe_release_request(tr);
826     if (param != NULL)
827 	free(param, M_DEVBUF);
828     return(NULL);
829 }
830 
831 /********************************************************************************
832  * Set integer parameter table entries.
833  */
834 #ifdef TWE_SHUTDOWN_NOTIFICATION
835 static int
twe_set_param_1(struct twe_softc * sc,int table_id,int param_id,u_int8_t value)836 twe_set_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t value)
837 {
838     return(twe_set_param(sc, table_id, param_id, sizeof(value), &value));
839 }
840 #endif
841 
842 #if 0
843 static int
844 twe_set_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t value)
845 {
846     return(twe_set_param(sc, table_id, param_id, sizeof(value), &value));
847 }
848 
849 static int
850 twe_set_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t value)
851 {
852     return(twe_set_param(sc, table_id, param_id, sizeof(value), &value));
853 }
854 #endif
855 
856 /********************************************************************************
857  * Perform a TWE_OP_SET_PARAM command, returns nonzero on error.
858  */
859 static int
twe_set_param(struct twe_softc * sc,int table_id,int param_id,int param_size,void * data)860 twe_set_param(struct twe_softc *sc, int table_id, int param_id, int param_size, void *data)
861 {
862     struct twe_request	*tr;
863     TWE_Command		*cmd;
864     TWE_Param		*param;
865     int			error;
866 
867     debug_called(4);
868 
869     TWE_IO_ASSERT_LOCKED(sc);
870     tr = NULL;
871     param = NULL;
872     error = ENOMEM;
873 
874     /* get a command */
875     if (twe_get_request(sc, &tr))
876 	goto out;
877 
878     /* get a buffer */
879     if ((param = (TWE_Param *)malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_NOWAIT)) == NULL)
880 	goto out;
881     tr->tr_data = param;
882     tr->tr_length = TWE_SECTOR_SIZE;
883     tr->tr_flags = TWE_CMD_DATAIN | TWE_CMD_DATAOUT;
884 
885     /* build the command for the controller */
886     cmd = TWE_FIND_COMMAND(tr);
887     cmd->param.opcode = TWE_OP_SET_PARAM;
888     cmd->param.size = 2;
889     cmd->param.unit = 0;
890     cmd->param.param_count = 1;
891 
892     /* fill in the outbound parameter data */
893     param->table_id = table_id;
894     param->parameter_id = param_id;
895     param->parameter_size_bytes = param_size;
896     bcopy(data, param->data, param_size);
897 
898     /* XXX could use twe_wait_request here if interrupts were enabled? */
899     error = twe_immediate_request(tr, 1 /* usetmp */);
900     if (error == 0) {
901 	if (twe_report_request(tr))
902 	    error = EIO;
903     }
904 
905 out:
906     if (tr != NULL)
907 	twe_release_request(tr);
908     if (param != NULL)
909 	free(param, M_DEVBUF);
910     return(error);
911 }
912 
913 /********************************************************************************
914  * Perform a TWE_OP_INIT_CONNECTION command, returns nonzero on error.
915  *
916  * Typically called with interrupts disabled.
917  */
918 static int
twe_init_connection(struct twe_softc * sc,int mode)919 twe_init_connection(struct twe_softc *sc, int mode)
920 {
921     struct twe_request	*tr;
922     TWE_Command		*cmd;
923     int			error;
924 
925     debug_called(4);
926 
927     TWE_IO_ASSERT_LOCKED(sc);
928 
929     /* get a command */
930     if (twe_get_request(sc, &tr))
931 	return(0);
932 
933     /* build the command */
934     cmd = TWE_FIND_COMMAND(tr);
935     cmd->initconnection.opcode = TWE_OP_INIT_CONNECTION;
936     cmd->initconnection.size = 3;
937     cmd->initconnection.host_id = 0;
938     cmd->initconnection.message_credits = mode;
939     cmd->initconnection.response_queue_pointer = 0;
940 
941     /* submit the command */
942     error = twe_immediate_request(tr, 0 /* usetmp */);
943     twe_release_request(tr);
944 
945     if (mode == TWE_INIT_MESSAGE_CREDITS)
946 	sc->twe_host_id = cmd->initconnection.host_id;
947     return(error);
948 }
949 
950 /********************************************************************************
951  * Start the command (tr) and sleep waiting for it to complete.
952  *
953  * Successfully completed commands are dequeued.
954  */
955 static int
twe_wait_request(struct twe_request * tr)956 twe_wait_request(struct twe_request *tr)
957 {
958 
959     debug_called(4);
960 
961     TWE_IO_ASSERT_LOCKED(tr->tr_sc);
962     tr->tr_flags |= TWE_CMD_SLEEPER;
963     tr->tr_status = TWE_CMD_BUSY;
964     twe_enqueue_ready(tr);
965     twe_startio(tr->tr_sc);
966     while (tr->tr_status == TWE_CMD_BUSY)
967 	mtx_sleep(tr, &tr->tr_sc->twe_io_lock, PRIBIO, "twewait", 0);
968 
969     return(tr->tr_status != TWE_CMD_COMPLETE);
970 }
971 
972 /********************************************************************************
973  * Start the command (tr) and busy-wait for it to complete.
974  * This should only be used when interrupts are actually disabled (although it
975  * will work if they are not).
976  */
977 static int
twe_immediate_request(struct twe_request * tr,int usetmp)978 twe_immediate_request(struct twe_request *tr, int usetmp)
979 {
980     struct twe_softc *sc;
981     int		error;
982     int		count = 0;
983 
984     debug_called(4);
985 
986     sc = tr->tr_sc;
987 
988     if (usetmp && (tr->tr_data != NULL)) {
989 	tr->tr_flags |= TWE_CMD_IMMEDIATE;
990 	if (tr->tr_length > DFLTPHYS)
991 	    return (EINVAL);
992 	bcopy(tr->tr_data, sc->twe_immediate, tr->tr_length);
993     }
994     tr->tr_status = TWE_CMD_BUSY;
995     if ((error = twe_map_request(tr)) != 0)
996 	if (error != EBUSY)
997 	    return(error);
998 
999     /* Wait up to 5 seconds for the command to complete */
1000     while ((count++ < 5000) && (tr->tr_status == TWE_CMD_BUSY)){
1001 	DELAY(1000);
1002 	twe_done(sc, 1);
1003     }
1004     if (usetmp && (tr->tr_data != NULL))
1005 	bcopy(sc->twe_immediate, tr->tr_data, tr->tr_length);
1006 
1007     return(tr->tr_status != TWE_CMD_COMPLETE);
1008 }
1009 
1010 /********************************************************************************
1011  * Handle completion of an I/O command.
1012  */
1013 static void
twe_completeio(struct twe_request * tr)1014 twe_completeio(struct twe_request *tr)
1015 {
1016     TWE_Command		*cmd = TWE_FIND_COMMAND(tr);
1017     struct twe_softc	*sc = tr->tr_sc;
1018     struct bio		*bp = tr->tr_private;
1019 
1020     debug_called(4);
1021 
1022     if (tr->tr_status == TWE_CMD_COMPLETE) {
1023 	if (cmd->generic.status)
1024 	    if (twe_report_request(tr)) {
1025 		bp->bio_error = EIO;
1026 		bp->bio_flags |= BIO_ERROR;
1027 	    }
1028 
1029     } else {
1030 	twe_panic(sc, "twe_completeio on incomplete command");
1031     }
1032     tr->tr_private = NULL;
1033     twed_intr(bp);
1034     twe_release_request(tr);
1035 }
1036 
1037 /********************************************************************************
1038  * Reset the controller and pull all the active commands back onto the ready
1039  * queue.  Used to restart a controller that's exhibiting bad behaviour.
1040  */
1041 static void
twe_reset(struct twe_softc * sc)1042 twe_reset(struct twe_softc *sc)
1043 {
1044     struct twe_request	*tr;
1045     int			i;
1046 
1047     /*
1048      * Sleep for a short period to allow AENs to be signalled.
1049      */
1050     mtx_sleep(sc, &sc->twe_io_lock, PRIBIO, "twereset", hz);
1051 
1052     /*
1053      * Disable interrupts from the controller, and mask any accidental entry
1054      * into our interrupt handler.
1055      */
1056     twe_printf(sc, "controller reset in progress...\n");
1057     twe_disable_interrupts(sc);
1058 
1059     /*
1060      * Try to soft-reset the controller.
1061      */
1062     for (i = 0; i < TWE_MAX_RESET_TRIES; i++) {
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 
1090 out:
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  */
1105 int
twe_start(struct twe_request * tr)1106 twe_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 	/* check to see if we can post a command */
1130 	status_reg = TWE_STATUS(sc);
1131 	twe_check_bits(sc, status_reg);
1132 
1133 	if (!(status_reg & TWE_STATUS_COMMAND_QUEUE_FULL)) {
1134 	    twe_enqueue_busy(tr);
1135 
1136 	    TWE_COMMAND_QUEUE(sc, TWE_FIND_COMMANDPHYS(tr));
1137 
1138 	    /* move command to work queue */
1139 #ifdef TWE_DEBUG
1140 	    if (tr->tr_complete != NULL) {
1141 		debug(3, "queued request %d with callback %p", cmd->generic.request_id, tr->tr_complete);
1142 	    } else if (tr->tr_flags & TWE_CMD_SLEEPER) {
1143 		debug(3, "queued request %d with wait channel %p", cmd->generic.request_id, tr);
1144 	    } else {
1145 		debug(3, "queued request %d for polling caller", cmd->generic.request_id);
1146 	    }
1147 #endif
1148 	    return(0);
1149 	} else if (!(status_reg & TWE_STATUS_RESPONSE_QUEUE_EMPTY) && i > 1)
1150 	    twe_done(sc, 0);
1151     }
1152 
1153     /*
1154      * We couldn't get the controller to take the command; try submitting it again later.
1155      * This should only happen if something is wrong with the controller, or if we have
1156      * overestimated the number of commands it can accept.  (Should we actually reject
1157      * the command at this point?)
1158      */
1159     return(EBUSY);
1160 }
1161 
1162 /********************************************************************************
1163  * Poll the controller (sc) for completed commands.
1164  *
1165  * Can be called at any interrupt level, with or without interrupts enabled.
1166  */
1167 static void
twe_done(struct twe_softc * sc,int startio)1168 twe_done(struct twe_softc *sc, int startio)
1169 {
1170     TWE_Response_Queue	rq;
1171     TWE_Command		*cmd;
1172     struct twe_request	*tr;
1173     int			found;
1174     u_int32_t		status_reg;
1175 
1176     debug_called(5);
1177 
1178     /* loop collecting completed commands */
1179     found = 0;
1180     for (;;) {
1181 	status_reg = TWE_STATUS(sc);
1182 	twe_check_bits(sc, status_reg);		/* XXX should this fail? */
1183 
1184 	if (!(status_reg & TWE_STATUS_RESPONSE_QUEUE_EMPTY)) {
1185 	    found = 1;
1186 	    rq = TWE_RESPONSE_QUEUE(sc);
1187 	    tr = sc->twe_lookup[rq.u.response_id];	/* find command */
1188 	    cmd = TWE_FIND_COMMAND(tr);
1189 	    if (tr->tr_status != TWE_CMD_BUSY)
1190 		twe_printf(sc, "completion event for nonbusy command\n");
1191 	    tr->tr_status = TWE_CMD_COMPLETE;
1192 	    debug(3, "completed request id %d with status %d",
1193 		  cmd->generic.request_id, cmd->generic.status);
1194 	    /* move to completed queue */
1195 	    twe_remove_busy(tr);
1196 	    twe_enqueue_complete(tr);
1197 	    sc->twe_state &= ~TWE_STATE_CTLR_BUSY;
1198 	} else {
1199 	    break;					/* no response ready */
1200 	}
1201     }
1202 
1203     /* if we've completed any commands, try posting some more */
1204     if (found && startio)
1205 	twe_startio(sc);
1206 
1207     /* handle completion and timeouts */
1208     twe_complete(sc);		/* XXX use deferred completion? */
1209 }
1210 
1211 /********************************************************************************
1212  * Perform post-completion processing for commands on (sc).
1213  *
1214  * This is split from twe_done as it can be safely deferred and run at a lower
1215  * priority level should facilities for such a thing become available.
1216  */
1217 static void
twe_complete(struct twe_softc * sc)1218 twe_complete(struct twe_softc *sc)
1219 {
1220     struct twe_request	*tr;
1221 
1222     debug_called(5);
1223 
1224     /*
1225      * Pull commands off the completed list, dispatch them appropriately
1226      */
1227     while ((tr = twe_dequeue_complete(sc)) != NULL) {
1228 	/* unmap the command's data buffer */
1229 	twe_unmap_request(tr);
1230 
1231 	/* dispatch to suit command originator */
1232 	if (tr->tr_complete != NULL) {		/* completion callback */
1233 	    debug(2, "call completion handler %p", tr->tr_complete);
1234 	    tr->tr_complete(tr);
1235 
1236 	} else if (tr->tr_flags & TWE_CMD_SLEEPER) {	/* caller is asleep waiting */
1237 	    debug(2, "wake up command owner on %p", tr);
1238 	    wakeup_one(tr);
1239 
1240 	} else {					/* caller is polling command */
1241 	    debug(2, "command left for owner");
1242 	}
1243     }
1244 }
1245 
1246 /********************************************************************************
1247  * Wait for (status) to be set in the controller status register for up to
1248  * (timeout) seconds.  Returns 0 if found, nonzero if we time out.
1249  *
1250  * Note: this busy-waits, rather than sleeping, since we may be called with
1251  * eg. clock interrupts masked.
1252  */
1253 static int
twe_wait_status(struct twe_softc * sc,u_int32_t status,int timeout)1254 twe_wait_status(struct twe_softc *sc, u_int32_t status, int timeout)
1255 {
1256     time_t	expiry;
1257     u_int32_t	status_reg;
1258 
1259     debug_called(4);
1260 
1261     expiry = time_second + timeout;
1262 
1263     do {
1264 	status_reg = TWE_STATUS(sc);
1265 	if (status_reg & status)	/* got the required bit(s)? */
1266 	    return(0);
1267 	DELAY(100000);
1268     } while (time_second <= expiry);
1269 
1270     return(1);
1271 }
1272 
1273 /********************************************************************************
1274  * Drain the response queue, which may contain responses to commands we know
1275  * nothing about.
1276  */
1277 static int
twe_drain_response_queue(struct twe_softc * sc)1278 twe_drain_response_queue(struct twe_softc *sc)
1279 {
1280     TWE_Response_Queue	rq;
1281     u_int32_t		status_reg;
1282 
1283     debug_called(4);
1284 
1285     for (;;) {				/* XXX give up eventually? */
1286 	status_reg = TWE_STATUS(sc);
1287 	if (twe_check_bits(sc, status_reg))
1288 	    return(1);
1289 	if (status_reg & TWE_STATUS_RESPONSE_QUEUE_EMPTY)
1290 	    return(0);
1291 	rq = TWE_RESPONSE_QUEUE(sc);
1292     }
1293 }
1294 
1295 /********************************************************************************
1296  * Soft-reset the controller
1297  */
1298 static int
twe_soft_reset(struct twe_softc * sc)1299 twe_soft_reset(struct twe_softc *sc)
1300 {
1301     u_int32_t		status_reg;
1302 
1303     debug_called(2);
1304 
1305     TWE_IO_ASSERT_LOCKED(sc);
1306     TWE_SOFT_RESET(sc);
1307 
1308     if (twe_wait_status(sc, TWE_STATUS_ATTENTION_INTERRUPT, 30)) {
1309 	twe_printf(sc, "no attention interrupt\n");
1310 	return(1);
1311     }
1312     TWE_CONTROL(sc, TWE_CONTROL_CLEAR_ATTENTION_INTERRUPT);
1313     if (twe_drain_aen_queue(sc)) {
1314 	twe_printf(sc, "can't drain AEN queue\n");
1315 	return(1);
1316     }
1317     if (twe_find_aen(sc, TWE_AEN_SOFT_RESET)) {
1318 	twe_printf(sc, "reset not reported\n");
1319 	return(1);
1320     }
1321     status_reg = TWE_STATUS(sc);
1322     if (TWE_STATUS_ERRORS(status_reg) || twe_check_bits(sc, status_reg)) {
1323 	twe_printf(sc, "controller errors detected\n");
1324 	return(1);
1325     }
1326     if (twe_drain_response_queue(sc)) {
1327 	twe_printf(sc, "can't drain response queue\n");
1328 	return(1);
1329     }
1330     return(0);
1331 }
1332 
1333 /********************************************************************************
1334  ********************************************************************************
1335                                                                Interrupt Handling
1336  ********************************************************************************
1337  ********************************************************************************/
1338 
1339 /********************************************************************************
1340  * Host interrupt.
1341  *
1342  * XXX what does this mean?
1343  */
1344 static void
twe_host_intr(struct twe_softc * sc)1345 twe_host_intr(struct twe_softc *sc)
1346 {
1347     debug_called(4);
1348 
1349     twe_printf(sc, "host interrupt\n");
1350     TWE_CONTROL(sc, TWE_CONTROL_CLEAR_HOST_INTERRUPT);
1351 }
1352 
1353 /********************************************************************************
1354  * Attention interrupt.
1355  *
1356  * Signalled when the controller has one or more AENs for us.
1357  */
1358 static void
twe_attention_intr(struct twe_softc * sc)1359 twe_attention_intr(struct twe_softc *sc)
1360 {
1361     debug_called(4);
1362 
1363     /* instigate a poll for AENs */
1364     if (twe_fetch_aen(sc)) {
1365 	twe_printf(sc, "error polling for signalled AEN\n");
1366     } else {
1367 	TWE_CONTROL(sc, TWE_CONTROL_CLEAR_ATTENTION_INTERRUPT);
1368     }
1369 }
1370 
1371 /********************************************************************************
1372  * Command interrupt.
1373  *
1374  * Signalled when the controller can handle more commands.
1375  */
1376 static void
twe_command_intr(struct twe_softc * sc)1377 twe_command_intr(struct twe_softc *sc)
1378 {
1379     debug_called(4);
1380 
1381     /*
1382      * We don't use this, rather we try to submit commands when we receive
1383      * them, and when other commands have completed.  Mask it so we don't get
1384      * another one.
1385      */
1386     TWE_CONTROL(sc, TWE_CONTROL_MASK_COMMAND_INTERRUPT);
1387 }
1388 
1389 /********************************************************************************
1390  ********************************************************************************
1391                                                       Asynchronous Event Handling
1392  ********************************************************************************
1393  ********************************************************************************/
1394 
1395 /********************************************************************************
1396  * Request an AEN from the controller.
1397  */
1398 static int
twe_fetch_aen(struct twe_softc * sc)1399 twe_fetch_aen(struct twe_softc *sc)
1400 {
1401 
1402     debug_called(4);
1403 
1404     if ((twe_get_param(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode, 2, twe_handle_aen)) == NULL)
1405 	return(EIO);
1406     return(0);
1407 }
1408 
1409 /********************************************************************************
1410  * Handle an AEN returned by the controller.
1411  */
1412 static void
twe_handle_aen(struct twe_request * tr)1413 twe_handle_aen(struct twe_request *tr)
1414 {
1415     struct twe_softc	*sc = tr->tr_sc;
1416     TWE_Param		*param;
1417     u_int16_t		aen;
1418 
1419     debug_called(4);
1420 
1421     /* XXX check for command success somehow? */
1422 
1423     param = (TWE_Param *)tr->tr_data;
1424     aen = *(u_int16_t *)(param->data);
1425 
1426     free(tr->tr_data, M_DEVBUF);
1427     twe_release_request(tr);
1428     twe_enqueue_aen(sc, aen);
1429 
1430     /* XXX poll for more AENs? */
1431 }
1432 
1433 /********************************************************************************
1434  * Pull AENs out of the controller and park them in the queue, in a context where
1435  * interrupts aren't active.  Return nonzero if we encounter any errors in the
1436  * process of obtaining all the available AENs.
1437  */
1438 static int
twe_drain_aen_queue(struct twe_softc * sc)1439 twe_drain_aen_queue(struct twe_softc *sc)
1440 {
1441     u_int16_t	aen;
1442 
1443     TWE_IO_ASSERT_LOCKED(sc);
1444     for (;;) {
1445 	if (twe_get_param_2(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode, &aen))
1446 	    return(1);
1447 	if (aen == TWE_AEN_QUEUE_EMPTY)
1448 	    return(0);
1449 	twe_enqueue_aen(sc, aen);
1450     }
1451 }
1452 
1453 /********************************************************************************
1454  * Push an AEN that we've received onto the queue.
1455  *
1456  * Note that we have to lock this against reentrance, since it may be called
1457  * from both interrupt and non-interrupt context.
1458  *
1459  * If someone is waiting for the AEN we have, wake them up.
1460  */
1461 static void
twe_enqueue_aen(struct twe_softc * sc,u_int16_t aen)1462 twe_enqueue_aen(struct twe_softc *sc, u_int16_t aen)
1463 {
1464     char	*msg;
1465     int		next, nextnext;
1466 
1467     debug_called(4);
1468 
1469     TWE_IO_ASSERT_LOCKED(sc);
1470     if ((msg = twe_format_aen(sc, aen)) != NULL)
1471 	twe_printf(sc, "AEN: <%s>\n", msg);
1472 
1473     /* enqueue the AEN */
1474     next = ((sc->twe_aen_head + 1) % TWE_Q_LENGTH);
1475     nextnext = ((sc->twe_aen_head + 2) % TWE_Q_LENGTH);
1476 
1477     /* check to see if this is the last free slot, and subvert the AEN if it is */
1478     if (nextnext == sc->twe_aen_tail)
1479 	aen = TWE_AEN_QUEUE_FULL;
1480 
1481     /* look to see if there's room for this AEN */
1482     if (next != sc->twe_aen_tail) {
1483 	sc->twe_aen_queue[sc->twe_aen_head] = aen;
1484 	sc->twe_aen_head = next;
1485     }
1486 
1487     /* wake up anyone asleep on the queue */
1488     wakeup(&sc->twe_aen_queue);
1489 
1490     /* anyone looking for this AEN? */
1491     if (sc->twe_wait_aen == aen) {
1492 	sc->twe_wait_aen = -1;
1493 	wakeup(&sc->twe_wait_aen);
1494     }
1495 }
1496 
1497 /********************************************************************************
1498  * Pop an AEN off the queue, or return -1 if there are none left.
1499  *
1500  * We are more or less interrupt-safe, so don't block interrupts.
1501  */
1502 static u_int16_t
twe_dequeue_aen(struct twe_softc * sc)1503 twe_dequeue_aen(struct twe_softc *sc)
1504 {
1505     u_int16_t	result;
1506 
1507     debug_called(4);
1508 
1509     TWE_IO_ASSERT_LOCKED(sc);
1510     if (sc->twe_aen_tail == sc->twe_aen_head) {
1511 	result = TWE_AEN_QUEUE_EMPTY;
1512     } else {
1513 	result = sc->twe_aen_queue[sc->twe_aen_tail];
1514 	sc->twe_aen_tail = ((sc->twe_aen_tail + 1) % TWE_Q_LENGTH);
1515     }
1516     return(result);
1517 }
1518 
1519 /********************************************************************************
1520  * Check to see if the requested AEN is in the queue.
1521  *
1522  * XXX we could probably avoid masking interrupts here
1523  */
1524 static int
twe_find_aen(struct twe_softc * sc,u_int16_t aen)1525 twe_find_aen(struct twe_softc *sc, u_int16_t aen)
1526 {
1527     int		i, missing;
1528 
1529     missing = 1;
1530     for (i = sc->twe_aen_tail; (i != sc->twe_aen_head) && missing; i = (i + 1) % TWE_Q_LENGTH) {
1531 	if (sc->twe_aen_queue[i] == aen)
1532 	    missing = 0;
1533     }
1534     return(missing);
1535 }
1536 
1537 #if 0	/* currently unused */
1538 /********************************************************************************
1539  * Sleep waiting for at least (timeout) seconds until we see (aen) as
1540  * requested.  Returns nonzero on timeout or failure.
1541  *
1542  * XXX: this should not be used in cases where there may be more than one sleeper
1543  *      without a mechanism for registering multiple sleepers.
1544  */
1545 static int
1546 twe_wait_aen(struct twe_softc *sc, int aen, int timeout)
1547 {
1548     time_t	expiry;
1549     int		found;
1550 
1551     debug_called(4);
1552 
1553     expiry = time_second + timeout;
1554     found = 0;
1555 
1556     sc->twe_wait_aen = aen;
1557     do {
1558 	twe_fetch_aen(sc);
1559 	mtx_sleep(&sc->twe_wait_aen, &sc->twe_io_lock, PZERO, "twewaen", hz);
1560 	if (sc->twe_wait_aen == -1)
1561 	    found = 1;
1562     } while ((time_second <= expiry) && !found);
1563     return(!found);
1564 }
1565 #endif
1566 
1567 /********************************************************************************
1568  ********************************************************************************
1569                                                         Command Buffer Management
1570  ********************************************************************************
1571  ********************************************************************************/
1572 
1573 /********************************************************************************
1574  * Get a new command buffer.
1575  *
1576  * This will return NULL if all command buffers are in use.
1577  */
1578 static int
twe_get_request(struct twe_softc * sc,struct twe_request ** tr)1579 twe_get_request(struct twe_softc *sc, struct twe_request **tr)
1580 {
1581     TWE_Command		*cmd;
1582     debug_called(4);
1583 
1584     if (!dumping)
1585 	TWE_IO_ASSERT_LOCKED(sc);
1586 
1587     /* try to reuse an old buffer */
1588     *tr = twe_dequeue_free(sc);
1589 
1590     /* initialise some fields to their defaults */
1591     if (*tr != NULL) {
1592 	cmd = TWE_FIND_COMMAND(*tr);
1593 	(*tr)->tr_data = NULL;
1594 	(*tr)->tr_private = NULL;
1595 	(*tr)->tr_status = TWE_CMD_SETUP;		/* command is in setup phase */
1596 	(*tr)->tr_flags = 0;
1597 	(*tr)->tr_complete = NULL;
1598 	cmd->generic.status = 0;			/* before submission to controller */
1599 	cmd->generic.flags = 0;				/* not used */
1600     }
1601     return(*tr == NULL);
1602 }
1603 
1604 /********************************************************************************
1605  * Release a command buffer for reuse.
1606  *
1607  */
1608 static void
twe_release_request(struct twe_request * tr)1609 twe_release_request(struct twe_request *tr)
1610 {
1611     debug_called(4);
1612 
1613     if (!dumping)
1614 	TWE_IO_ASSERT_LOCKED(tr->tr_sc);
1615     if (tr->tr_private != NULL)
1616 	twe_panic(tr->tr_sc, "tr_private != NULL");
1617     twe_enqueue_free(tr);
1618 }
1619 
1620 /********************************************************************************
1621  ********************************************************************************
1622                                                                         Debugging
1623  ********************************************************************************
1624  ********************************************************************************/
1625 
1626 /********************************************************************************
1627  * Print some information about the controller
1628  */
1629 void
twe_describe_controller(struct twe_softc * sc)1630 twe_describe_controller(struct twe_softc *sc)
1631 {
1632     TWE_Param		*p[6];
1633     u_int8_t		ports;
1634     u_int32_t		size;
1635     int			i;
1636 
1637     debug_called(2);
1638 
1639     TWE_IO_LOCK(sc);
1640 
1641     /* get the port count */
1642     twe_get_param_1(sc, TWE_PARAM_CONTROLLER, TWE_PARAM_CONTROLLER_PortCount, &ports);
1643 
1644     /* get version strings */
1645     p[0] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_FW,   16, NULL);
1646     p[1] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_BIOS, 16, NULL);
1647     if (p[0] && p[1])
1648 	 twe_printf(sc, "%d ports, Firmware %.16s, BIOS %.16s\n", ports, p[0]->data, p[1]->data);
1649 
1650     if (bootverbose) {
1651 	p[2] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_Mon,  16, NULL);
1652 	p[3] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_PCB,  8, NULL);
1653 	p[4] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_ATA,  8, NULL);
1654 	p[5] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_PCI,  8, NULL);
1655 
1656 	if (p[2] && p[3] && p[4] && p[5])
1657 	    twe_printf(sc, "Monitor %.16s, PCB %.8s, Achip %.8s, Pchip %.8s\n", p[2]->data, p[3]->data,
1658 		p[4]->data, p[5]->data);
1659 	if (p[2])
1660 	    free(p[2], M_DEVBUF);
1661 	if (p[3])
1662 	    free(p[3], M_DEVBUF);
1663 	if (p[4])
1664 	    free(p[4], M_DEVBUF);
1665 	if (p[5])
1666 	    free(p[5], M_DEVBUF);
1667     }
1668     if (p[0])
1669 	free(p[0], M_DEVBUF);
1670     if (p[1])
1671 	free(p[1], M_DEVBUF);
1672 
1673     /* print attached drives */
1674     if (bootverbose) {
1675 	p[0] = twe_get_param(sc, TWE_PARAM_DRIVESUMMARY, TWE_PARAM_DRIVESUMMARY_Status, 16, NULL);
1676 	for (i = 0; i < ports; i++) {
1677 	    if (p[0]->data[i] != TWE_PARAM_DRIVESTATUS_Present)
1678 		continue;
1679 	    twe_get_param_4(sc, TWE_PARAM_DRIVEINFO + i, TWE_PARAM_DRIVEINFO_Size, &size);
1680 	    p[1] = twe_get_param(sc, TWE_PARAM_DRIVEINFO + i, TWE_PARAM_DRIVEINFO_Model, 40, NULL);
1681 	    if (p[1] != NULL) {
1682 		twe_printf(sc, "port %d: %.40s %dMB\n", i, p[1]->data, size / 2048);
1683 		free(p[1], M_DEVBUF);
1684 	    } else {
1685 		twe_printf(sc, "port %d, drive status unavailable\n", i);
1686 	    }
1687 	}
1688 	if (p[0])
1689 	    free(p[0], M_DEVBUF);
1690     }
1691     TWE_IO_UNLOCK(sc);
1692 }
1693 
1694 /********************************************************************************
1695  * Look up a text description of a numeric code and return a pointer to same.
1696  */
1697 char *
twe_describe_code(struct twe_code_lookup * table,u_int32_t code)1698 twe_describe_code(struct twe_code_lookup *table, u_int32_t code)
1699 {
1700     int         i;
1701 
1702     for (i = 0; table[i].string != NULL; i++)
1703 	if (table[i].code == code)
1704 	    return(table[i].string);
1705     return(table[i+1].string);
1706 }
1707 
1708 /********************************************************************************
1709  * Complain if the status bits aren't what we're expecting.
1710  *
1711  * Rate-limit the complaints to at most one of each every five seconds, but
1712  * always return the correct status.
1713  */
1714 static int
twe_check_bits(struct twe_softc * sc,u_int32_t status_reg)1715 twe_check_bits(struct twe_softc *sc, u_int32_t status_reg)
1716 {
1717     int			result;
1718     static time_t	lastwarn[2] = {0, 0};
1719 
1720     /*
1721      * This can be a little problematic, as twe_panic may call twe_reset if
1722      * TWE_DEBUG is not set, which will call us again as part of the soft reset.
1723      */
1724     if ((status_reg & TWE_STATUS_PANIC_BITS) != 0) {
1725 	twe_printf(sc, "FATAL STATUS BIT(S) %b\n", status_reg & TWE_STATUS_PANIC_BITS,
1726 		   TWE_STATUS_BITS_DESCRIPTION);
1727 	twe_panic(sc, "fatal status bits");
1728     }
1729 
1730     result = 0;
1731     if ((status_reg & TWE_STATUS_EXPECTED_BITS) != TWE_STATUS_EXPECTED_BITS) {
1732 	if (time_second > (lastwarn[0] + 5)) {
1733 	    twe_printf(sc, "missing expected status bit(s) %b\n", ~status_reg & TWE_STATUS_EXPECTED_BITS,
1734 		       TWE_STATUS_BITS_DESCRIPTION);
1735 	    lastwarn[0] = time_second;
1736 	}
1737 	result = 1;
1738     }
1739 
1740     if ((status_reg & TWE_STATUS_UNEXPECTED_BITS) != 0) {
1741 	if (time_second > (lastwarn[1] + 5)) {
1742 	    twe_printf(sc, "unexpected status bit(s) %b\n", status_reg & TWE_STATUS_UNEXPECTED_BITS,
1743 		       TWE_STATUS_BITS_DESCRIPTION);
1744 	    lastwarn[1] = time_second;
1745 	}
1746 	result = 1;
1747 	if (status_reg & TWE_STATUS_PCI_PARITY_ERROR) {
1748 	    twe_printf(sc, "PCI parity error: Reseat card, move card or buggy device present.\n");
1749 	    twe_clear_pci_parity_error(sc);
1750 	}
1751 	if (status_reg & TWE_STATUS_PCI_ABORT) {
1752 	    twe_printf(sc, "PCI abort, clearing.\n");
1753 	    twe_clear_pci_abort(sc);
1754 	}
1755     }
1756 
1757     return(result);
1758 }
1759 
1760 /********************************************************************************
1761  * Return a string describing (aen).
1762  *
1763  * The low 8 bits of the aen are the code, the high 8 bits give the unit number
1764  * where an AEN is specific to a unit.
1765  *
1766  * Note that we could expand this routine to handle eg. up/downgrading the status
1767  * of a drive if we had some idea of what the drive's initial status was.
1768  */
1769 
1770 static char *
twe_format_aen(struct twe_softc * sc,u_int16_t aen)1771 twe_format_aen(struct twe_softc *sc, u_int16_t aen)
1772 {
1773     device_t	child;
1774     char	*code, *msg;
1775 
1776     code = twe_describe_code(twe_table_aen, TWE_AEN_CODE(aen));
1777     msg = code + 2;
1778 
1779     switch (*code) {
1780     case 'q':
1781 	if (!bootverbose)
1782 	    return(NULL);
1783 	/* FALLTHROUGH */
1784     case 'a':
1785 	return(msg);
1786 
1787     case 'c':
1788 	if ((child = sc->twe_drive[TWE_AEN_UNIT(aen)].td_disk) != NULL) {
1789 	    snprintf(sc->twe_aen_buf, sizeof(sc->twe_aen_buf), "twed%d: %s",
1790 		device_get_unit(child), msg);
1791 	} else {
1792 	    snprintf(sc->twe_aen_buf, sizeof(sc->twe_aen_buf),
1793 		"twe%d: %s for unknown unit %d", device_get_unit(sc->twe_dev),
1794 		msg, TWE_AEN_UNIT(aen));
1795 	}
1796 	return(sc->twe_aen_buf);
1797 
1798     case 'p':
1799 	snprintf(sc->twe_aen_buf, sizeof(sc->twe_aen_buf),
1800 	    "twe%d: port %d: %s", device_get_unit(sc->twe_dev),
1801 	    TWE_AEN_UNIT(aen), msg);
1802 	return(sc->twe_aen_buf);
1803 
1804     case 'x':
1805     default:
1806 	break;
1807     }
1808     snprintf(sc->twe_aen_buf, sizeof(sc->twe_aen_buf), "unknown AEN 0x%x", aen);
1809     return(sc->twe_aen_buf);
1810 }
1811 
1812 /********************************************************************************
1813  * Print a diagnostic if the status of the command warrants it, and return
1814  * either zero (command was ok) or nonzero (command failed).
1815  */
1816 static int
twe_report_request(struct twe_request * tr)1817 twe_report_request(struct twe_request *tr)
1818 {
1819     struct twe_softc	*sc = tr->tr_sc;
1820     TWE_Command		*cmd = TWE_FIND_COMMAND(tr);
1821     int			result = 0;
1822 
1823     /*
1824      * Check the command status value and handle accordingly.
1825      */
1826     if (cmd->generic.status == TWE_STATUS_RESET) {
1827 	/*
1828 	 * The status code 0xff requests a controller reset.
1829 	 */
1830 	twe_printf(sc, "command returned with controller reset request\n");
1831 	twe_reset(sc);
1832 	result = 1;
1833     } else if (cmd->generic.status > TWE_STATUS_FATAL) {
1834 	/*
1835 	 * Fatal errors that don't require controller reset.
1836 	 *
1837 	 * We know a few special flags values.
1838 	 */
1839 	switch (cmd->generic.flags) {
1840 	case 0x1b:
1841 	    device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
1842 			  "drive timeout");
1843 	    break;
1844 	case 0x51:
1845 	    device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
1846 			  "unrecoverable drive error");
1847 	    break;
1848 	default:
1849 	    device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
1850 			  "controller error - %s (flags = 0x%x)\n",
1851 			  twe_describe_code(twe_table_status, cmd->generic.status),
1852 			  cmd->generic.flags);
1853 	    result = 1;
1854 	}
1855     } else if (cmd->generic.status > TWE_STATUS_WARNING) {
1856 	/*
1857 	 * Warning level status.
1858 	 */
1859 	device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
1860 		      "warning - %s (flags = 0x%x)\n",
1861 		      twe_describe_code(twe_table_status, cmd->generic.status),
1862 		      cmd->generic.flags);
1863     } else if (cmd->generic.status > 0x40) {
1864 	/*
1865 	 * Info level status.
1866 	 */
1867 	device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
1868 		      "attention - %s (flags = 0x%x)\n",
1869 		      twe_describe_code(twe_table_status, cmd->generic.status),
1870 		      cmd->generic.flags);
1871     }
1872 
1873     return(result);
1874 }
1875 
1876 /********************************************************************************
1877  * Print some controller state to aid in debugging error/panic conditions
1878  */
1879 void
twe_print_controller(struct twe_softc * sc)1880 twe_print_controller(struct twe_softc *sc)
1881 {
1882     u_int32_t		status_reg;
1883 
1884     status_reg = TWE_STATUS(sc);
1885     twe_printf(sc, "status   %b\n", status_reg, TWE_STATUS_BITS_DESCRIPTION);
1886     twe_printf(sc, "          current  max    min\n");
1887     twe_printf(sc, "free      %04d     %04d   %04d\n",
1888 	sc->twe_qstat[TWEQ_FREE].q_length, sc->twe_qstat[TWEQ_FREE].q_max, sc->twe_qstat[TWEQ_FREE].q_min);
1889 
1890     twe_printf(sc, "ready     %04d     %04d   %04d\n",
1891 	sc->twe_qstat[TWEQ_READY].q_length, sc->twe_qstat[TWEQ_READY].q_max, sc->twe_qstat[TWEQ_READY].q_min);
1892 
1893     twe_printf(sc, "busy      %04d     %04d   %04d\n",
1894 	sc->twe_qstat[TWEQ_BUSY].q_length, sc->twe_qstat[TWEQ_BUSY].q_max, sc->twe_qstat[TWEQ_BUSY].q_min);
1895 
1896     twe_printf(sc, "complete  %04d     %04d   %04d\n",
1897 	sc->twe_qstat[TWEQ_COMPLETE].q_length, sc->twe_qstat[TWEQ_COMPLETE].q_max, sc->twe_qstat[TWEQ_COMPLETE].q_min);
1898 
1899     twe_printf(sc, "bioq      %04d     %04d   %04d\n",
1900 	sc->twe_qstat[TWEQ_BIO].q_length, sc->twe_qstat[TWEQ_BIO].q_max, sc->twe_qstat[TWEQ_BIO].q_min);
1901 
1902     twe_printf(sc, "AEN queue head %d  tail %d\n", sc->twe_aen_head, sc->twe_aen_tail);
1903 }
1904 
1905 static void
twe_panic(struct twe_softc * sc,char * reason)1906 twe_panic(struct twe_softc *sc, char *reason)
1907 {
1908     twe_print_controller(sc);
1909 #ifdef TWE_DEBUG
1910     panic(reason);
1911 #else
1912     twe_reset(sc);
1913 #endif
1914 }
1915 
1916 #if 0
1917 /********************************************************************************
1918  * Print a request/command in human-readable format.
1919  */
1920 static void
1921 twe_print_request(struct twe_request *tr)
1922 {
1923     struct twe_softc	*sc = tr->tr_sc;
1924     TWE_Command	*cmd = TWE_FIND_COMMAND(tr);
1925     int		i;
1926 
1927     twe_printf(sc, "CMD: request_id %d  opcode <%s>  size %d  unit %d  host_id %d\n",
1928 	       cmd->generic.request_id, twe_describe_code(twe_table_opcode, cmd->generic.opcode), cmd->generic.size,
1929 	       cmd->generic.unit, cmd->generic.host_id);
1930     twe_printf(sc, " status %d  flags 0x%x  count %d  sgl_offset %d\n",
1931 	       cmd->generic.status, cmd->generic.flags, cmd->generic.count, cmd->generic.sgl_offset);
1932 
1933     switch(cmd->generic.opcode) {	/* XXX add more opcodes? */
1934     case TWE_OP_READ:
1935     case TWE_OP_WRITE:
1936 	twe_printf(sc, " lba %d\n", cmd->io.lba);
1937 	for (i = 0; (i < TWE_MAX_SGL_LENGTH) && (cmd->io.sgl[i].length != 0); i++)
1938 	    twe_printf(sc, "  %d: 0x%x/%d\n",
1939 		       i, cmd->io.sgl[i].address, cmd->io.sgl[i].length);
1940 	break;
1941 
1942     case TWE_OP_GET_PARAM:
1943     case TWE_OP_SET_PARAM:
1944 	for (i = 0; (i < TWE_MAX_SGL_LENGTH) && (cmd->param.sgl[i].length != 0); i++)
1945 	    twe_printf(sc, "  %d: 0x%x/%d\n",
1946 		       i, cmd->param.sgl[i].address, cmd->param.sgl[i].length);
1947 	break;
1948 
1949     case TWE_OP_INIT_CONNECTION:
1950 	twe_printf(sc, " response queue pointer 0x%x\n",
1951 		   cmd->initconnection.response_queue_pointer);
1952 	break;
1953 
1954     default:
1955 	break;
1956     }
1957     twe_printf(sc, " tr_command %p/0x%x  tr_data %p/0x%x,%d\n",
1958 	       tr, TWE_FIND_COMMANDPHYS(tr), tr->tr_data, tr->tr_dataphys, tr->tr_length);
1959     twe_printf(sc, " tr_status %d  tr_flags 0x%x  tr_complete %p  tr_private %p\n",
1960 	       tr->tr_status, tr->tr_flags, tr->tr_complete, tr->tr_private);
1961 }
1962 
1963 #endif
1964