2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2003 Paul Saab
4 * Copyright (c) 2003 Vinod Kashyap
5 * Copyright (c) 2000 BSDi
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Driver for the 3ware Escalade family of IDE RAID controllers.
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>
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);
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);
58 static int twe_set_param(struct twe_softc *sc, int table_id, int param_id, int param_size,
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);
69 * Command I/O to controller.
71 static void twe_done(struct twe_softc *sc);
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);
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);
86 * Asynchronous event handling.
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);
96 * Command buffer management.
98 static int twe_get_request(struct twe_softc *sc, struct twe_request **tr);
99 static void twe_release_request(struct twe_request *tr);
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);
108 /********************************************************************************
109 ********************************************************************************
111 ********************************************************************************
112 ********************************************************************************/
114 /********************************************************************************
115 * Initialise the controller, set up driver data structures.
118 twe_setup(struct twe_softc *sc)
120 struct twe_request *tr;
122 u_int32_t status_reg;
128 * Initialise request queues.
134 twe_initq_complete(sc);
135 sc->twe_wait_aen = -1;
138 * Allocate request structures up front.
140 for (i = 0; i < TWE_Q_LENGTH; i++) {
141 if ((tr = twe_allocate_request(sc, i)) == NULL)
144 * Set global defaults that won't change.
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;
152 * Put command onto the freelist.
154 twe_release_request(tr);
158 * Check status register for errors, clear them.
160 status_reg = TWE_STATUS(sc);
161 twe_check_bits(sc, status_reg);
164 * Wait for the controller to come ready.
166 if (twe_wait_status(sc, TWE_STATUS_MICROCONTROLLER_READY, 60)) {
167 twe_printf(sc, "microcontroller not ready\n");
172 * Disable interrupts from the card.
174 twe_disable_interrupts(sc);
177 * Soft reset the controller, look for the AEN acknowledging the reset,
178 * check for errors, drain the response queue.
180 for (i = 0; i < TWE_MAX_RESET_TRIES; i++) {
183 twe_printf(sc, "reset %d failed, trying again\n", i);
185 if (!twe_soft_reset(sc))
186 break; /* reset process complete */
188 /* did we give up? */
189 if (i >= TWE_MAX_RESET_TRIES) {
190 twe_printf(sc, "can't initialise controller, giving up\n");
198 twe_add_unit(struct twe_softc *sc, int unit)
200 struct twe_drive *dr;
201 int table, error = 0;
203 TWE_Param *drives = NULL, *param = NULL;
204 TWE_Unit_Descriptor *ud;
206 if (unit < 0 || unit > TWE_MAX_UNITS)
210 * The controller is in a safe state, so try to find drives attached to it.
212 if ((drives = twe_get_param(sc, TWE_PARAM_UNITSUMMARY, TWE_PARAM_UNITSUMMARY_Status,
213 TWE_MAX_UNITS, NULL)) == NULL) {
214 twe_printf(sc, "can't detect attached units\n");
218 dr = &sc->twe_drive[unit];
219 /* check that the drive is online */
220 if (!(drives->data[unit] & TWE_PARAM_UNITSTATUS_Online)) {
225 table = TWE_PARAM_UNITINFO + unit;
227 if (twe_get_param_4(sc, table, TWE_PARAM_UNITINFO_Capacity, &dr->td_size)) {
228 twe_printf(sc, "error fetching capacity for unit %d\n", unit);
232 if (twe_get_param_1(sc, table, TWE_PARAM_UNITINFO_Status, &dr->td_state)) {
233 twe_printf(sc, "error fetching state for unit %d\n", unit);
237 if (twe_get_param_2(sc, table, TWE_PARAM_UNITINFO_DescriptorSize, &dsize)) {
238 twe_printf(sc, "error fetching descriptor size for unit %d\n", unit);
242 if ((param = twe_get_param(sc, table, TWE_PARAM_UNITINFO_Descriptor, dsize - 3, NULL)) == NULL) {
243 twe_printf(sc, "error fetching descriptor for unit %d\n", unit);
247 ud = (TWE_Unit_Descriptor *)param->data;
248 dr->td_type = ud->configuration;
250 /* build synthetic geometry as per controller internal rules */
251 if (dr->td_size > 0x200000) {
258 dr->td_cylinders = dr->td_size / (dr->td_heads * dr->td_sectors);
259 dr->td_twe_unit = unit;
261 error = twe_attach_drive(sc, dr);
265 free(param, M_DEVBUF);
267 free(drives, M_DEVBUF);
272 twe_del_unit(struct twe_softc *sc, int unit)
276 if (unit < 0 || unit >= TWE_MAX_UNITS)
279 if (sc->twe_drive[unit].td_disk == NULL)
282 error = twe_detach_drive(sc, unit);
286 /********************************************************************************
287 * Locate disk devices and attach children to them.
290 twe_init(struct twe_softc *sc)
298 for (i = 0; i < TWE_MAX_UNITS; i++)
303 * Initialise connection with controller.
305 twe_init_connection(sc, TWE_INIT_MESSAGE_CREDITS);
307 #ifdef TWE_SHUTDOWN_NOTIFICATION
309 * Tell the controller we support shutdown notification.
311 twe_set_param_1(sc, TWE_PARAM_FEATURES, TWE_PARAM_FEATURES_DriverShutdown, 1);
315 * Mark controller up and ready to run.
317 sc->twe_state &= ~TWE_STATE_SHUTDOWN;
320 * Finally enable interrupts.
322 twe_enable_interrupts(sc);
325 /********************************************************************************
326 * Stop the controller
329 twe_deinit(struct twe_softc *sc)
332 * Mark the controller as shutting down, and disable any further interrupts.
334 sc->twe_state |= TWE_STATE_SHUTDOWN;
335 twe_disable_interrupts(sc);
337 #ifdef TWE_SHUTDOWN_NOTIFICATION
339 * Disconnect from the controller
341 twe_init_connection(sc, TWE_SHUTDOWN_MESSAGE_CREDITS);
345 /*******************************************************************************
346 * Take an interrupt, or be poked by other code to look for interrupt-worthy
350 twe_intr(struct twe_softc *sc)
352 u_int32_t status_reg;
357 * Collect current interrupt status.
359 status_reg = TWE_STATUS(sc);
360 twe_check_bits(sc, status_reg);
363 * Dispatch based on interrupt status
365 if (status_reg & TWE_STATUS_HOST_INTERRUPT)
367 if (status_reg & TWE_STATUS_ATTENTION_INTERRUPT)
368 twe_attention_intr(sc);
369 if (status_reg & TWE_STATUS_COMMAND_INTERRUPT)
370 twe_command_intr(sc);
371 if (status_reg & TWE_STATUS_RESPONSE_INTERRUPT)
375 /********************************************************************************
376 * Pull as much work off the softc's work queue as possible and give it to the
380 twe_startio(struct twe_softc *sc)
382 struct twe_request *tr;
389 if (sc->twe_state & (TWE_STATE_CTLR_BUSY | TWE_STATE_FRZN))
392 /* spin until something prevents us from doing any work */
395 /* try to get a command that's already ready to go */
396 tr = twe_dequeue_ready(sc);
398 /* build a command from an outstanding bio */
401 /* get a command to handle the bio with */
402 if (twe_get_request(sc, &tr))
405 /* see if there's work to be done */
406 if ((bp = twe_dequeue_bio(sc)) == NULL) {
407 twe_release_request(tr);
411 /* connect the bio to the command */
412 tr->tr_complete = twe_completeio;
414 tr->tr_data = TWE_BIO_DATA(bp);
415 tr->tr_length = TWE_BIO_LENGTH(bp);
416 cmd = TWE_FIND_COMMAND(tr);
417 if (TWE_BIO_IS_READ(bp)) {
418 tr->tr_flags |= TWE_CMD_DATAIN;
419 cmd->io.opcode = TWE_OP_READ;
421 tr->tr_flags |= TWE_CMD_DATAOUT;
422 cmd->io.opcode = TWE_OP_WRITE;
425 /* build a suitable I/O command (assumes 512-byte rounded transfers) */
427 cmd->io.unit = TWE_BIO_UNIT(bp);
428 cmd->io.block_count = (tr->tr_length + TWE_BLOCK_SIZE - 1) / TWE_BLOCK_SIZE;
429 cmd->io.lba = TWE_BIO_LBA(bp);
432 /* did we find something to do? */
436 /* try to map and submit the command to controller */
437 error = twe_map_request(tr);
442 tr->tr_status = TWE_CMD_ERROR;
443 if (tr->tr_private != NULL) {
444 bp = (twe_bio *)(tr->tr_private);
445 TWE_BIO_SET_ERROR(bp, error);
446 tr->tr_private = NULL;
448 twe_release_request(tr);
449 } else if (tr->tr_flags & TWE_CMD_SLEEPER)
450 wakeup_one(tr); /* wakeup the sleeping owner */
455 /********************************************************************************
456 * Write blocks from memory to disk, for system crash dumps.
459 twe_dump_blocks(struct twe_softc *sc, int unit, u_int32_t lba, void *data, int nblks)
461 struct twe_request *tr;
465 if (twe_get_request(sc, &tr))
469 tr->tr_status = TWE_CMD_SETUP;
470 tr->tr_length = nblks * TWE_BLOCK_SIZE;
471 tr->tr_flags = TWE_CMD_DATAOUT;
473 cmd = TWE_FIND_COMMAND(tr);
474 cmd->io.opcode = TWE_OP_WRITE;
477 cmd->io.block_count = nblks;
480 error = twe_immediate_request(tr, 0);
482 if (twe_report_request(tr))
484 twe_release_request(tr);
488 /********************************************************************************
489 * Handle controller-specific control operations.
492 twe_ioctl(struct twe_softc *sc, int ioctlcmd, void *addr)
494 struct twe_usercommand *tu = (struct twe_usercommand *)addr;
495 struct twe_paramcommand *tp = (struct twe_paramcommand *)addr;
496 struct twe_drivecommand *td = (struct twe_drivecommand *)addr;
497 union twe_statrequest *ts = (union twe_statrequest *)addr;
501 u_int16_t *aen_code = (u_int16_t *)addr;
502 struct twe_request *tr;
508 /* handle a command from userspace */
511 while (twe_get_request(sc, &tr))
512 tsleep(sc, PPAUSE, "twioctl", hz);
515 * Save the command's request ID, copy the user-supplied command in,
516 * restore the request ID.
518 cmd = TWE_FIND_COMMAND(tr);
519 srid = cmd->generic.request_id;
520 bcopy(&tu->tu_command, cmd, sizeof(TWE_Command));
521 cmd->generic.request_id = srid;
524 * if there's a data buffer, allocate and copy it in.
525 * Must be in multipled of 512 bytes.
527 tr->tr_length = (tu->tu_size + 511) & ~511;
528 if (tr->tr_length > 0) {
529 if ((tr->tr_data = malloc(tr->tr_length, M_DEVBUF, M_WAITOK)) == NULL) {
533 if ((error = copyin(tu->tu_data, tr->tr_data, tu->tu_size)) != 0)
535 tr->tr_flags |= TWE_CMD_DATAIN | TWE_CMD_DATAOUT;
538 /* run the command */
539 error = twe_wait_request(tr);
543 /* copy the command out again */
544 bcopy(cmd, &tu->tu_command, sizeof(TWE_Command));
546 /* if there was a data buffer, copy it out */
547 if (tr->tr_length > 0)
548 error = copyout(tr->tr_data, tu->tu_data, tu->tu_size);
552 if (tr->tr_data != NULL)
553 free(tr->tr_data, M_DEVBUF);
555 twe_release_request(tr);
559 /* fetch statistics counter */
561 switch (ts->ts_item) {
562 #ifdef TWE_PERFORMANCE_MONITOR
568 bcopy(&sc->twe_qstat[ts->ts_item], &ts->ts_qstat, sizeof(struct twe_qstat));
577 /* poll for an AEN */
579 *aen_code = twe_dequeue_aen(sc);
582 /* wait for another AEN to show up */
585 while ((*aen_code = twe_dequeue_aen(sc)) == TWE_AEN_QUEUE_EMPTY) {
586 error = tsleep(&sc->twe_aen_queue, PRIBIO | PCATCH, "tweaen", 0);
593 case TWEIO_GET_PARAM:
594 if ((param = twe_get_param(sc, tp->tp_table_id, tp->tp_param_id, tp->tp_size, NULL)) == NULL) {
595 twe_printf(sc, "TWEIO_GET_PARAM failed for 0x%x/0x%x/%d\n",
596 tp->tp_table_id, tp->tp_param_id, tp->tp_size);
599 if (param->parameter_size_bytes > tp->tp_size) {
600 twe_printf(sc, "TWEIO_GET_PARAM parameter too large (%d > %d)\n",
601 param->parameter_size_bytes, tp->tp_size);
604 error = copyout(param->data, tp->tp_data, param->parameter_size_bytes);
606 free(param, M_DEVBUF);
610 case TWEIO_SET_PARAM:
611 if ((data = malloc(tp->tp_size, M_DEVBUF, M_WAITOK)) == NULL) {
614 error = copyin(tp->tp_data, data, tp->tp_size);
616 error = twe_set_param(sc, tp->tp_table_id, tp->tp_param_id, tp->tp_size, data);
617 free(data, M_DEVBUF);
627 error = twe_add_unit(sc, td->td_unit);
633 error = twe_del_unit(sc, td->td_unit);
637 /* XXX implement ATA PASSTHROUGH */
639 /* nothing we understand */
647 /********************************************************************************
648 * Enable the useful interrupts from the controller.
651 twe_enable_interrupts(struct twe_softc *sc)
653 sc->twe_state |= TWE_STATE_INTEN;
655 TWE_CONTROL_CLEAR_ATTENTION_INTERRUPT |
656 TWE_CONTROL_UNMASK_RESPONSE_INTERRUPT |
657 TWE_CONTROL_ENABLE_INTERRUPTS);
660 /********************************************************************************
661 * Disable interrupts from the controller.
664 twe_disable_interrupts(struct twe_softc *sc)
666 TWE_CONTROL(sc, TWE_CONTROL_DISABLE_INTERRUPTS);
667 sc->twe_state &= ~TWE_STATE_INTEN;
670 /********************************************************************************
671 ********************************************************************************
673 ********************************************************************************
674 ********************************************************************************/
676 /********************************************************************************
677 * Read integer parameter table entries.
680 twe_get_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t *result)
684 if ((param = twe_get_param(sc, table_id, param_id, 1, NULL)) == NULL)
686 *result = *(u_int8_t *)param->data;
687 free(param, M_DEVBUF);
692 twe_get_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t *result)
696 if ((param = twe_get_param(sc, table_id, param_id, 2, NULL)) == NULL)
698 *result = *(u_int16_t *)param->data;
699 free(param, M_DEVBUF);
704 twe_get_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t *result)
708 if ((param = twe_get_param(sc, table_id, param_id, 4, NULL)) == NULL)
710 *result = *(u_int32_t *)param->data;
711 free(param, M_DEVBUF);
715 /********************************************************************************
716 * Perform a TWE_OP_GET_PARAM command. If a callback function is provided, it
717 * will be called with the command when it's completed. If no callback is
718 * provided, we will wait for the command to complete and then return just the data.
719 * The caller is responsible for freeing the data when done with it.
722 twe_get_param(struct twe_softc *sc, int table_id, int param_id, size_t param_size,
723 void (* func)(struct twe_request *tr))
725 struct twe_request *tr;
736 if (twe_get_request(sc, &tr))
740 if ((param = (TWE_Param *)malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_NOWAIT)) == NULL)
743 tr->tr_length = TWE_SECTOR_SIZE;
744 tr->tr_flags = TWE_CMD_DATAIN | TWE_CMD_DATAOUT;
746 /* build the command for the controller */
747 cmd = TWE_FIND_COMMAND(tr);
748 cmd->param.opcode = TWE_OP_GET_PARAM;
751 cmd->param.param_count = 1;
753 /* fill in the outbound parameter data */
754 param->table_id = table_id;
755 param->parameter_id = param_id;
756 param->parameter_size_bytes = param_size;
758 /* submit the command and either wait or let the callback handle it */
760 /* XXX could use twe_wait_request here if interrupts were enabled? */
761 error = twe_immediate_request(tr, 1 /* usetmp */);
763 if (twe_report_request(tr))
768 twe_release_request(tr);
771 tr->tr_complete = func;
772 error = twe_map_request(tr);
773 if ((error == 0) || (error == EBUSY))
777 /* something failed */
781 twe_release_request(tr);
783 free(param, M_DEVBUF);
787 /********************************************************************************
788 * Set integer parameter table entries.
790 #ifdef TWE_SHUTDOWN_NOTIFICATION
792 twe_set_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t value)
794 return(twe_set_param(sc, table_id, param_id, sizeof(value), &value));
800 twe_set_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t value)
802 return(twe_set_param(sc, table_id, param_id, sizeof(value), &value));
806 twe_set_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t value)
808 return(twe_set_param(sc, table_id, param_id, sizeof(value), &value));
812 /********************************************************************************
813 * Perform a TWE_OP_SET_PARAM command, returns nonzero on error.
816 twe_set_param(struct twe_softc *sc, int table_id, int param_id, int param_size, void *data)
818 struct twe_request *tr;
830 if (twe_get_request(sc, &tr))
834 if ((param = (TWE_Param *)malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_NOWAIT)) == NULL)
837 tr->tr_length = TWE_SECTOR_SIZE;
838 tr->tr_flags = TWE_CMD_DATAIN | TWE_CMD_DATAOUT;
840 /* build the command for the controller */
841 cmd = TWE_FIND_COMMAND(tr);
842 cmd->param.opcode = TWE_OP_SET_PARAM;
845 cmd->param.param_count = 1;
847 /* fill in the outbound parameter data */
848 param->table_id = table_id;
849 param->parameter_id = param_id;
850 param->parameter_size_bytes = param_size;
851 bcopy(data, param->data, param_size);
853 /* XXX could use twe_wait_request here if interrupts were enabled? */
854 error = twe_immediate_request(tr, 1 /* usetmp */);
856 if (twe_report_request(tr))
862 twe_release_request(tr);
864 free(param, M_DEVBUF);
868 /********************************************************************************
869 * Perform a TWE_OP_INIT_CONNECTION command, returns nonzero on error.
871 * Typically called with interrupts disabled.
874 twe_init_connection(struct twe_softc *sc, int mode)
876 struct twe_request *tr;
883 if (twe_get_request(sc, &tr))
886 /* build the command */
887 cmd = TWE_FIND_COMMAND(tr);
888 cmd->initconnection.opcode = TWE_OP_INIT_CONNECTION;
889 cmd->initconnection.size = 3;
890 cmd->initconnection.host_id = 0;
891 cmd->initconnection.message_credits = mode;
892 cmd->initconnection.response_queue_pointer = 0;
894 /* submit the command */
895 error = twe_immediate_request(tr, 0 /* usetmp */);
896 twe_release_request(tr);
898 if (mode == TWE_INIT_MESSAGE_CREDITS)
899 sc->twe_host_id = cmd->initconnection.host_id;
903 /********************************************************************************
904 * Start the command (tr) and sleep waiting for it to complete.
906 * Successfully completed commands are dequeued.
909 twe_wait_request(struct twe_request *tr)
915 tr->tr_flags |= TWE_CMD_SLEEPER;
916 tr->tr_status = TWE_CMD_BUSY;
917 twe_enqueue_ready(tr);
918 twe_startio(tr->tr_sc);
920 while (tr->tr_status == TWE_CMD_BUSY)
921 tsleep(tr, PRIBIO, "twewait", 0);
924 return(tr->tr_status != TWE_CMD_COMPLETE);
927 /********************************************************************************
928 * Start the command (tr) and busy-wait for it to complete.
929 * This should only be used when interrupts are actually disabled (although it
930 * will work if they are not).
933 twe_immediate_request(struct twe_request *tr, int usetmp)
935 struct twe_softc *sc;
943 if (usetmp && (tr->tr_data != NULL)) {
944 tr->tr_flags |= TWE_CMD_IMMEDIATE;
945 if (tr->tr_length > MAXBSIZE)
947 bcopy(tr->tr_data, sc->twe_immediate, tr->tr_length);
949 tr->tr_status = TWE_CMD_BUSY;
950 if ((error = twe_map_request(tr)) != 0)
954 /* Wait up to 5 seconds for the command to complete */
955 while ((count++ < 5000) && (tr->tr_status == TWE_CMD_BUSY)){
959 if (usetmp && (tr->tr_data != NULL))
960 bcopy(sc->twe_immediate, tr->tr_data, tr->tr_length);
962 return(tr->tr_status != TWE_CMD_COMPLETE);
965 /********************************************************************************
966 * Handle completion of an I/O command.
969 twe_completeio(struct twe_request *tr)
971 TWE_Command *cmd = TWE_FIND_COMMAND(tr);
972 struct twe_softc *sc = tr->tr_sc;
973 twe_bio *bp = (twe_bio *)tr->tr_private;
977 if (tr->tr_status == TWE_CMD_COMPLETE) {
979 if (cmd->generic.status)
980 if (twe_report_request(tr))
981 TWE_BIO_SET_ERROR(bp, EIO);
984 twe_panic(sc, "twe_completeio on incomplete command");
986 tr->tr_private = NULL;
988 twe_release_request(tr);
991 /********************************************************************************
992 * Reset the controller and pull all the active commands back onto the ready
993 * queue. Used to restart a controller that's exhibiting bad behaviour.
996 twe_reset(struct twe_softc *sc)
998 struct twe_request *tr;
1002 * Sleep for a short period to allow AENs to be signalled.
1004 tsleep(sc, PRIBIO, "twereset", hz);
1007 * Disable interrupts from the controller, and mask any accidental entry
1008 * into our interrupt handler.
1010 twe_printf(sc, "controller reset in progress...\n");
1011 twe_disable_interrupts(sc);
1015 * Try to soft-reset the controller.
1017 for (i = 0; i < TWE_MAX_RESET_TRIES; i++) {
1020 twe_printf(sc, "reset %d failed, trying again\n", i);
1022 if (!twe_soft_reset(sc))
1023 break; /* reset process complete */
1025 /* did we give up? */
1026 if (i >= TWE_MAX_RESET_TRIES) {
1027 twe_printf(sc, "can't reset controller, giving up\n");
1032 * Move all of the commands that were busy back to the ready queue.
1035 while ((tr = twe_dequeue_busy(sc)) != NULL) {
1036 twe_enqueue_ready(tr);
1041 * Kick the controller to start things going again, then re-enable interrupts.
1044 twe_enable_interrupts(sc);
1045 twe_printf(sc, "controller reset done, %d commands restarted\n", i);
1049 twe_enable_interrupts(sc);
1052 /********************************************************************************
1053 ********************************************************************************
1054 Command I/O to Controller
1055 ********************************************************************************
1056 ********************************************************************************/
1058 /********************************************************************************
1059 * Try to deliver (tr) to the controller.
1061 * Can be called at any interrupt level, with or without interrupts enabled.
1064 twe_start(struct twe_request *tr)
1066 struct twe_softc *sc = tr->tr_sc;
1069 u_int32_t status_reg;
1073 /* mark the command as currently being processed */
1074 tr->tr_status = TWE_CMD_BUSY;
1075 cmd = TWE_FIND_COMMAND(tr);
1078 * Spin briefly waiting for the controller to come ready
1080 * XXX it might be more efficient to return EBUSY immediately
1081 * and let the command be rescheduled.
1083 for (i = 100000, done = 0; (i > 0) && !done; i--) {
1086 /* check to see if we can post a command */
1087 status_reg = TWE_STATUS(sc);
1088 twe_check_bits(sc, status_reg);
1090 if (!(status_reg & TWE_STATUS_COMMAND_QUEUE_FULL)) {
1091 twe_enqueue_busy(tr);
1093 TWE_COMMAND_QUEUE(sc, TWE_FIND_COMMANDPHYS(tr));
1095 /* move command to work queue */
1097 if (tr->tr_complete != NULL) {
1098 debug(3, "queued request %d with callback %p", cmd->generic.request_id, tr->tr_complete);
1099 } else if (tr->tr_flags & TWE_CMD_SLEEPER) {
1100 debug(3, "queued request %d with wait channel %p", cmd->generic.request_id, tr);
1102 debug(3, "queued request %d for polling caller", cmd->generic.request_id);
1106 splx(s); /* drop spl to allow completion interrupts */
1109 /* command is enqueued */
1114 * We couldn't get the controller to take the command; try submitting it again later.
1115 * This should only happen if something is wrong with the controller, or if we have
1116 * overestimated the number of commands it can accept. (Should we actually reject
1117 * the command at this point?)
1122 /********************************************************************************
1123 * Poll the controller (sc) for completed commands.
1125 * Can be called at any interrupt level, with or without interrupts enabled.
1128 twe_done(struct twe_softc *sc)
1130 TWE_Response_Queue rq;
1132 struct twe_request *tr;
1134 u_int32_t status_reg;
1138 /* loop collecting completed commands */
1142 status_reg = TWE_STATUS(sc);
1143 twe_check_bits(sc, status_reg); /* XXX should this fail? */
1145 if (!(status_reg & TWE_STATUS_RESPONSE_QUEUE_EMPTY)) {
1147 rq = TWE_RESPONSE_QUEUE(sc);
1148 tr = sc->twe_lookup[rq.u.response_id]; /* find command */
1149 cmd = TWE_FIND_COMMAND(tr);
1150 if (tr->tr_status != TWE_CMD_BUSY)
1151 twe_printf(sc, "completion event for nonbusy command\n");
1152 tr->tr_status = TWE_CMD_COMPLETE;
1153 debug(3, "completed request id %d with status %d",
1154 cmd->generic.request_id, cmd->generic.status);
1155 /* move to completed queue */
1156 twe_remove_busy(tr);
1157 twe_enqueue_complete(tr);
1158 sc->twe_state &= ~TWE_STATE_CTLR_BUSY;
1160 break; /* no response ready */
1165 /* if we've completed any commands, try posting some more */
1169 /* handle completion and timeouts */
1170 twe_complete(sc); /* XXX use deferred completion? */
1173 /********************************************************************************
1174 * Perform post-completion processing for commands on (sc).
1176 * This is split from twe_done as it can be safely deferred and run at a lower
1177 * priority level should facilities for such a thing become available.
1180 twe_complete(struct twe_softc *sc)
1182 struct twe_request *tr;
1187 * Pull commands off the completed list, dispatch them appropriately
1189 while ((tr = twe_dequeue_complete(sc)) != NULL) {
1190 /* unmap the command's data buffer */
1191 twe_unmap_request(tr);
1193 /* dispatch to suit command originator */
1194 if (tr->tr_complete != NULL) { /* completion callback */
1195 debug(2, "call completion handler %p", tr->tr_complete);
1196 tr->tr_complete(tr);
1198 } else if (tr->tr_flags & TWE_CMD_SLEEPER) { /* caller is asleep waiting */
1199 debug(2, "wake up command owner on %p", tr);
1202 } else { /* caller is polling command */
1203 debug(2, "command left for owner");
1208 /********************************************************************************
1209 * Wait for (status) to be set in the controller status register for up to
1210 * (timeout) seconds. Returns 0 if found, nonzero if we time out.
1212 * Note: this busy-waits, rather than sleeping, since we may be called with
1213 * eg. clock interrupts masked.
1216 twe_wait_status(struct twe_softc *sc, u_int32_t status, int timeout)
1219 u_int32_t status_reg;
1223 expiry = time_second + timeout;
1226 status_reg = TWE_STATUS(sc);
1227 if (status_reg & status) /* got the required bit(s)? */
1230 } while (time_second <= expiry);
1235 /********************************************************************************
1236 * Drain the response queue, which may contain responses to commands we know
1240 twe_drain_response_queue(struct twe_softc *sc)
1242 TWE_Response_Queue rq;
1243 u_int32_t status_reg;
1247 for (;;) { /* XXX give up eventually? */
1248 status_reg = TWE_STATUS(sc);
1249 if (twe_check_bits(sc, status_reg))
1251 if (status_reg & TWE_STATUS_RESPONSE_QUEUE_EMPTY)
1253 rq = TWE_RESPONSE_QUEUE(sc);
1257 /********************************************************************************
1258 * Soft-reset the controller
1261 twe_soft_reset(struct twe_softc *sc)
1263 u_int32_t status_reg;
1269 if (twe_wait_status(sc, TWE_STATUS_ATTENTION_INTERRUPT, 30)) {
1270 twe_printf(sc, "no attention interrupt\n");
1273 TWE_CONTROL(sc, TWE_CONTROL_CLEAR_ATTENTION_INTERRUPT);
1274 if (twe_drain_aen_queue(sc)) {
1275 twe_printf(sc, "can't drain AEN queue\n");
1278 if (twe_find_aen(sc, TWE_AEN_SOFT_RESET)) {
1279 twe_printf(sc, "reset not reported\n");
1282 status_reg = TWE_STATUS(sc);
1283 if (TWE_STATUS_ERRORS(status_reg) || twe_check_bits(sc, status_reg)) {
1284 twe_printf(sc, "controller errors detected\n");
1287 if (twe_drain_response_queue(sc)) {
1288 twe_printf(sc, "can't drain response queue\n");
1294 /********************************************************************************
1295 ********************************************************************************
1297 ********************************************************************************
1298 ********************************************************************************/
1300 /********************************************************************************
1303 * XXX what does this mean?
1306 twe_host_intr(struct twe_softc *sc)
1310 twe_printf(sc, "host interrupt\n");
1311 TWE_CONTROL(sc, TWE_CONTROL_CLEAR_HOST_INTERRUPT);
1314 /********************************************************************************
1315 * Attention interrupt.
1317 * Signalled when the controller has one or more AENs for us.
1320 twe_attention_intr(struct twe_softc *sc)
1324 /* instigate a poll for AENs */
1325 if (twe_fetch_aen(sc)) {
1326 twe_printf(sc, "error polling for signalled AEN\n");
1328 TWE_CONTROL(sc, TWE_CONTROL_CLEAR_ATTENTION_INTERRUPT);
1332 /********************************************************************************
1333 * Command interrupt.
1335 * Signalled when the controller can handle more commands.
1338 twe_command_intr(struct twe_softc *sc)
1343 * We don't use this, rather we try to submit commands when we receive
1344 * them, and when other commands have completed. Mask it so we don't get
1347 TWE_CONTROL(sc, TWE_CONTROL_MASK_COMMAND_INTERRUPT);
1350 /********************************************************************************
1351 ********************************************************************************
1352 Asynchronous Event Handling
1353 ********************************************************************************
1354 ********************************************************************************/
1356 /********************************************************************************
1357 * Request an AEN from the controller.
1360 twe_fetch_aen(struct twe_softc *sc)
1365 if ((twe_get_param(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode, 2, twe_handle_aen)) == NULL)
1370 /********************************************************************************
1371 * Handle an AEN returned by the controller.
1374 twe_handle_aen(struct twe_request *tr)
1376 struct twe_softc *sc = tr->tr_sc;
1382 /* XXX check for command success somehow? */
1384 param = (TWE_Param *)tr->tr_data;
1385 aen = *(u_int16_t *)(param->data);
1387 free(tr->tr_data, M_DEVBUF);
1388 twe_release_request(tr);
1389 twe_enqueue_aen(sc, aen);
1391 /* XXX poll for more AENs? */
1394 /********************************************************************************
1395 * Pull AENs out of the controller and park them in the queue, in a context where
1396 * interrupts aren't active. Return nonzero if we encounter any errors in the
1397 * process of obtaining all the available AENs.
1400 twe_drain_aen_queue(struct twe_softc *sc)
1405 if (twe_get_param_2(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode, &aen))
1407 if (aen == TWE_AEN_QUEUE_EMPTY)
1409 twe_enqueue_aen(sc, aen);
1413 /********************************************************************************
1414 * Push an AEN that we've received onto the queue.
1416 * Note that we have to lock this against reentrance, since it may be called
1417 * from both interrupt and non-interrupt context.
1419 * If someone is waiting for the AEN we have, wake them up.
1422 twe_enqueue_aen(struct twe_softc *sc, u_int16_t aen)
1425 int s, next, nextnext;
1429 if ((msg = twe_format_aen(sc, aen)) != NULL)
1430 twe_printf(sc, "AEN: <%s>\n", msg);
1433 /* enqueue the AEN */
1434 next = ((sc->twe_aen_head + 1) % TWE_Q_LENGTH);
1435 nextnext = ((sc->twe_aen_head + 2) % TWE_Q_LENGTH);
1437 /* check to see if this is the last free slot, and subvert the AEN if it is */
1438 if (nextnext == sc->twe_aen_tail)
1439 aen = TWE_AEN_QUEUE_FULL;
1441 /* look to see if there's room for this AEN */
1442 if (next != sc->twe_aen_tail) {
1443 sc->twe_aen_queue[sc->twe_aen_head] = aen;
1444 sc->twe_aen_head = next;
1447 /* wake up anyone asleep on the queue */
1448 wakeup(&sc->twe_aen_queue);
1450 /* anyone looking for this AEN? */
1451 if (sc->twe_wait_aen == aen) {
1452 sc->twe_wait_aen = -1;
1453 wakeup(&sc->twe_wait_aen);
1458 /********************************************************************************
1459 * Pop an AEN off the queue, or return -1 if there are none left.
1461 * We are more or less interrupt-safe, so don't block interrupts.
1464 twe_dequeue_aen(struct twe_softc *sc)
1470 if (sc->twe_aen_tail == sc->twe_aen_head) {
1471 result = TWE_AEN_QUEUE_EMPTY;
1473 result = sc->twe_aen_queue[sc->twe_aen_tail];
1474 sc->twe_aen_tail = ((sc->twe_aen_tail + 1) % TWE_Q_LENGTH);
1479 /********************************************************************************
1480 * Check to see if the requested AEN is in the queue.
1482 * XXX we could probably avoid masking interrupts here
1485 twe_find_aen(struct twe_softc *sc, u_int16_t aen)
1491 for (i = sc->twe_aen_tail; (i != sc->twe_aen_head) && missing; i = (i + 1) % TWE_Q_LENGTH) {
1492 if (sc->twe_aen_queue[i] == aen)
1500 #if 0 /* currently unused */
1501 /********************************************************************************
1502 * Sleep waiting for at least (timeout) seconds until we see (aen) as
1503 * requested. Returns nonzero on timeout or failure.
1505 * XXX: this should not be used in cases where there may be more than one sleeper
1506 * without a mechanism for registering multiple sleepers.
1509 twe_wait_aen(struct twe_softc *sc, int aen, int timeout)
1516 expiry = time_second + timeout;
1520 sc->twe_wait_aen = aen;
1523 tsleep(&sc->twe_wait_aen, PZERO, "twewaen", hz);
1524 if (sc->twe_wait_aen == -1)
1526 } while ((time_second <= expiry) && !found);
1532 /********************************************************************************
1533 ********************************************************************************
1534 Command Buffer Management
1535 ********************************************************************************
1536 ********************************************************************************/
1538 /********************************************************************************
1539 * Get a new command buffer.
1541 * This will return NULL if all command buffers are in use.
1544 twe_get_request(struct twe_softc *sc, struct twe_request **tr)
1549 /* try to reuse an old buffer */
1550 *tr = twe_dequeue_free(sc);
1552 /* initialise some fields to their defaults */
1554 cmd = TWE_FIND_COMMAND(*tr);
1555 (*tr)->tr_data = NULL;
1556 (*tr)->tr_private = NULL;
1557 (*tr)->tr_status = TWE_CMD_SETUP; /* command is in setup phase */
1558 (*tr)->tr_flags = 0;
1559 (*tr)->tr_complete = NULL;
1560 cmd->generic.status = 0; /* before submission to controller */
1561 cmd->generic.flags = 0; /* not used */
1563 return(*tr == NULL);
1566 /********************************************************************************
1567 * Release a command buffer for reuse.
1571 twe_release_request(struct twe_request *tr)
1575 if (tr->tr_private != NULL)
1576 twe_panic(tr->tr_sc, "tr_private != NULL");
1577 twe_enqueue_free(tr);
1580 /********************************************************************************
1581 ********************************************************************************
1583 ********************************************************************************
1584 ********************************************************************************/
1586 /********************************************************************************
1587 * Print some information about the controller
1590 twe_describe_controller(struct twe_softc *sc)
1599 /* get the port count */
1600 twe_get_param_1(sc, TWE_PARAM_CONTROLLER, TWE_PARAM_CONTROLLER_PortCount, &ports);
1602 /* get version strings */
1603 p[0] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_FW, 16, NULL);
1604 p[1] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_BIOS, 16, NULL);
1606 twe_printf(sc, "%d ports, Firmware %.16s, BIOS %.16s\n", ports, p[0]->data, p[1]->data);
1609 p[2] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_Mon, 16, NULL);
1610 p[3] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_PCB, 8, NULL);
1611 p[4] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_ATA, 8, NULL);
1612 p[5] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_PCI, 8, NULL);
1614 if (p[2] && p[3] && p[4] && p[5])
1615 twe_printf(sc, "Monitor %.16s, PCB %.8s, Achip %.8s, Pchip %.8s\n", p[2]->data, p[3]->data,
1616 p[4]->data, p[5]->data);
1618 free(p[2], M_DEVBUF);
1620 free(p[3], M_DEVBUF);
1622 free(p[4], M_DEVBUF);
1624 free(p[5], M_DEVBUF);
1627 free(p[0], M_DEVBUF);
1629 free(p[1], M_DEVBUF);
1631 /* print attached drives */
1633 p[0] = twe_get_param(sc, TWE_PARAM_DRIVESUMMARY, TWE_PARAM_DRIVESUMMARY_Status, 16, NULL);
1634 for (i = 0; i < ports; i++) {
1635 if (p[0]->data[i] != TWE_PARAM_DRIVESTATUS_Present)
1637 twe_get_param_4(sc, TWE_PARAM_DRIVEINFO + i, TWE_PARAM_DRIVEINFO_Size, &size);
1638 p[1] = twe_get_param(sc, TWE_PARAM_DRIVEINFO + i, TWE_PARAM_DRIVEINFO_Model, 40, NULL);
1640 twe_printf(sc, "port %d: %.40s %dMB\n", i, p[1]->data, size / 2048);
1641 free(p[1], M_DEVBUF);
1643 twe_printf(sc, "port %d, drive status unavailable\n", i);
1647 free(p[0], M_DEVBUF);
1651 /********************************************************************************
1652 * Look up a text description of a numeric code and return a pointer to same.
1655 twe_describe_code(struct twe_code_lookup *table, u_int32_t code)
1659 for (i = 0; table[i].string != NULL; i++)
1660 if (table[i].code == code)
1661 return(table[i].string);
1662 return(table[i+1].string);
1665 /********************************************************************************
1666 * Complain if the status bits aren't what we're expecting.
1668 * Rate-limit the complaints to at most one of each every five seconds, but
1669 * always return the correct status.
1672 twe_check_bits(struct twe_softc *sc, u_int32_t status_reg)
1675 static time_t lastwarn[2] = {0, 0};
1678 * This can be a little problematic, as twe_panic may call twe_reset if
1679 * TWE_DEBUG is not set, which will call us again as part of the soft reset.
1681 if ((status_reg & TWE_STATUS_PANIC_BITS) != 0) {
1682 twe_printf(sc, "FATAL STATUS BIT(S) %b\n", status_reg & TWE_STATUS_PANIC_BITS,
1683 TWE_STATUS_BITS_DESCRIPTION);
1684 twe_panic(sc, "fatal status bits");
1688 if ((status_reg & TWE_STATUS_EXPECTED_BITS) != TWE_STATUS_EXPECTED_BITS) {
1689 if (time_second > (lastwarn[0] + 5)) {
1690 twe_printf(sc, "missing expected status bit(s) %b\n", ~status_reg & TWE_STATUS_EXPECTED_BITS,
1691 TWE_STATUS_BITS_DESCRIPTION);
1692 lastwarn[0] = time_second;
1697 if ((status_reg & TWE_STATUS_UNEXPECTED_BITS) != 0) {
1698 if (time_second > (lastwarn[1] + 5)) {
1699 twe_printf(sc, "unexpected status bit(s) %b\n", status_reg & TWE_STATUS_UNEXPECTED_BITS,
1700 TWE_STATUS_BITS_DESCRIPTION);
1701 lastwarn[1] = time_second;
1704 if (status_reg & TWE_STATUS_PCI_PARITY_ERROR) {
1705 twe_printf(sc, "PCI parity error: Reseat card, move card or buggy device present.\n");
1706 twe_clear_pci_parity_error(sc);
1708 if (status_reg & TWE_STATUS_PCI_ABORT) {
1709 twe_printf(sc, "PCI abort, clearing.\n");
1710 twe_clear_pci_abort(sc);
1717 /********************************************************************************
1718 * Return a string describing (aen).
1720 * The low 8 bits of the aen are the code, the high 8 bits give the unit number
1721 * where an AEN is specific to a unit.
1723 * Note that we could expand this routine to handle eg. up/downgrading the status
1724 * of a drive if we had some idea of what the drive's initial status was.
1728 twe_format_aen(struct twe_softc *sc, u_int16_t aen)
1730 static char buf[80];
1734 code = twe_describe_code(twe_table_aen, TWE_AEN_CODE(aen));
1746 if ((child = sc->twe_drive[TWE_AEN_UNIT(aen)].td_disk) != NULL) {
1747 sprintf(buf, "twed%d: %s", device_get_unit(child), msg);
1749 sprintf(buf, "twe%d: %s for unknown unit %d", device_get_unit(sc->twe_dev),
1750 msg, TWE_AEN_UNIT(aen));
1755 sprintf(buf, "twe%d: port %d: %s", device_get_unit(sc->twe_dev), TWE_AEN_UNIT(aen),
1764 sprintf(buf, "unknown AEN 0x%x", aen);
1768 /********************************************************************************
1769 * Print a diagnostic if the status of the command warrants it, and return
1770 * either zero (command was ok) or nonzero (command failed).
1773 twe_report_request(struct twe_request *tr)
1775 struct twe_softc *sc = tr->tr_sc;
1776 TWE_Command *cmd = TWE_FIND_COMMAND(tr);
1780 * Check the command status value and handle accordingly.
1782 if (cmd->generic.status == TWE_STATUS_RESET) {
1784 * The status code 0xff requests a controller reset.
1786 twe_printf(sc, "command returned with controller reset request\n");
1789 } else if (cmd->generic.status > TWE_STATUS_FATAL) {
1791 * Fatal errors that don't require controller reset.
1793 * We know a few special flags values.
1795 switch (cmd->generic.flags) {
1797 device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
1801 device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
1802 "unrecoverable drive error");
1805 device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
1806 "controller error - %s (flags = 0x%x)\n",
1807 twe_describe_code(twe_table_status, cmd->generic.status),
1808 cmd->generic.flags);
1811 } else if (cmd->generic.status > TWE_STATUS_WARNING) {
1813 * Warning level status.
1815 device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
1816 "warning - %s (flags = 0x%x)\n",
1817 twe_describe_code(twe_table_status, cmd->generic.status),
1818 cmd->generic.flags);
1819 } else if (cmd->generic.status > 0x40) {
1821 * Info level status.
1823 device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
1824 "attention - %s (flags = 0x%x)\n",
1825 twe_describe_code(twe_table_status, cmd->generic.status),
1826 cmd->generic.flags);
1832 /********************************************************************************
1833 * Print some controller state to aid in debugging error/panic conditions
1836 twe_print_controller(struct twe_softc *sc)
1838 u_int32_t status_reg;
1840 status_reg = TWE_STATUS(sc);
1841 twe_printf(sc, "status %b\n", status_reg, TWE_STATUS_BITS_DESCRIPTION);
1842 twe_printf(sc, " current max min\n");
1843 twe_printf(sc, "free %04d %04d %04d\n",
1844 sc->twe_qstat[TWEQ_FREE].q_length, sc->twe_qstat[TWEQ_FREE].q_max, sc->twe_qstat[TWEQ_FREE].q_min);
1846 twe_printf(sc, "ready %04d %04d %04d\n",
1847 sc->twe_qstat[TWEQ_READY].q_length, sc->twe_qstat[TWEQ_READY].q_max, sc->twe_qstat[TWEQ_READY].q_min);
1849 twe_printf(sc, "busy %04d %04d %04d\n",
1850 sc->twe_qstat[TWEQ_BUSY].q_length, sc->twe_qstat[TWEQ_BUSY].q_max, sc->twe_qstat[TWEQ_BUSY].q_min);
1852 twe_printf(sc, "complete %04d %04d %04d\n",
1853 sc->twe_qstat[TWEQ_COMPLETE].q_length, sc->twe_qstat[TWEQ_COMPLETE].q_max, sc->twe_qstat[TWEQ_COMPLETE].q_min);
1855 twe_printf(sc, "bioq %04d %04d %04d\n",
1856 sc->twe_qstat[TWEQ_BIO].q_length, sc->twe_qstat[TWEQ_BIO].q_max, sc->twe_qstat[TWEQ_BIO].q_min);
1858 twe_printf(sc, "AEN queue head %d tail %d\n", sc->twe_aen_head, sc->twe_aen_tail);
1862 twe_panic(struct twe_softc *sc, char *reason)
1864 twe_print_controller(sc);
1873 /********************************************************************************
1874 * Print a request/command in human-readable format.
1877 twe_print_request(struct twe_request *tr)
1879 struct twe_softc *sc = tr->tr_sc;
1880 TWE_Command *cmd = TWE_FIND_COMMAND(tr);
1883 twe_printf(sc, "CMD: request_id %d opcode <%s> size %d unit %d host_id %d\n",
1884 cmd->generic.request_id, twe_describe_code(twe_table_opcode, cmd->generic.opcode), cmd->generic.size,
1885 cmd->generic.unit, cmd->generic.host_id);
1886 twe_printf(sc, " status %d flags 0x%x count %d sgl_offset %d\n",
1887 cmd->generic.status, cmd->generic.flags, cmd->generic.count, cmd->generic.sgl_offset);
1889 switch(cmd->generic.opcode) { /* XXX add more opcodes? */
1892 twe_printf(sc, " lba %d\n", cmd->io.lba);
1893 for (i = 0; (i < TWE_MAX_SGL_LENGTH) && (cmd->io.sgl[i].length != 0); i++)
1894 twe_printf(sc, " %d: 0x%x/%d\n",
1895 i, cmd->io.sgl[i].address, cmd->io.sgl[i].length);
1898 case TWE_OP_GET_PARAM:
1899 case TWE_OP_SET_PARAM:
1900 for (i = 0; (i < TWE_MAX_SGL_LENGTH) && (cmd->param.sgl[i].length != 0); i++)
1901 twe_printf(sc, " %d: 0x%x/%d\n",
1902 i, cmd->param.sgl[i].address, cmd->param.sgl[i].length);
1905 case TWE_OP_INIT_CONNECTION:
1906 twe_printf(sc, " response queue pointer 0x%x\n",
1907 cmd->initconnection.response_queue_pointer);
1913 twe_printf(sc, " tr_command %p/0x%x tr_data %p/0x%x,%d\n",
1914 tr, TWE_FIND_COMMANDPHYS(tr), tr->tr_data, tr->tr_dataphys, tr->tr_length);
1915 twe_printf(sc, " tr_status %d tr_flags 0x%x tr_complete %p tr_private %p\n",
1916 tr->tr_status, tr->tr_flags, tr->tr_complete, tr->tr_private);