2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000 Michael Smith
5 * Copyright (c) 2003 Paul Saab
6 * Copyright (c) 2003 Vinod Kashyap
7 * Copyright (c) 2000 BSDi
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
35 * Driver for the 3ware Escalade family of IDE RAID controllers.
38 #include <dev/twe/twe_compat.h>
39 #include <dev/twe/twereg.h>
40 #include <dev/twe/tweio.h>
41 #include <dev/twe/twevar.h>
42 #define TWE_DEFINE_TABLES
43 #include <dev/twe/twe_tables.h>
48 static int twe_get_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t *result);
49 static int twe_get_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t *result);
50 static int twe_get_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t *result);
51 static void *twe_get_param(struct twe_softc *sc, int table_id, int parameter_id, size_t size,
52 void (* func)(struct twe_request *tr));
53 #ifdef TWE_SHUTDOWN_NOTIFICATION
54 static int twe_set_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t value);
57 static int twe_set_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t value);
58 static int twe_set_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t value);
60 static int twe_set_param(struct twe_softc *sc, int table_id, int param_id, int param_size,
62 static int twe_init_connection(struct twe_softc *sc, int mode);
63 static int twe_wait_request(struct twe_request *tr);
64 static int twe_immediate_request(struct twe_request *tr, int usetmp);
65 static void twe_completeio(struct twe_request *tr);
66 static void twe_reset(struct twe_softc *sc);
67 static int twe_add_unit(struct twe_softc *sc, int unit);
68 static int twe_del_unit(struct twe_softc *sc, int unit);
71 * Command I/O to controller.
73 static void twe_done(struct twe_softc *sc, int startio);
74 static void twe_complete(struct twe_softc *sc);
75 static int twe_wait_status(struct twe_softc *sc, u_int32_t status, int timeout);
76 static int twe_drain_response_queue(struct twe_softc *sc);
77 static int twe_check_bits(struct twe_softc *sc, u_int32_t status_reg);
78 static int twe_soft_reset(struct twe_softc *sc);
83 static void twe_host_intr(struct twe_softc *sc);
84 static void twe_attention_intr(struct twe_softc *sc);
85 static void twe_command_intr(struct twe_softc *sc);
88 * Asynchronous event handling.
90 static int twe_fetch_aen(struct twe_softc *sc);
91 static void twe_handle_aen(struct twe_request *tr);
92 static void twe_enqueue_aen(struct twe_softc *sc, u_int16_t aen);
93 static u_int16_t twe_dequeue_aen(struct twe_softc *sc);
94 static int twe_drain_aen_queue(struct twe_softc *sc);
95 static int twe_find_aen(struct twe_softc *sc, u_int16_t aen);
98 * Command buffer management.
100 static int twe_get_request(struct twe_softc *sc, struct twe_request **tr);
101 static void twe_release_request(struct twe_request *tr);
106 static char *twe_format_aen(struct twe_softc *sc, u_int16_t aen);
107 static int twe_report_request(struct twe_request *tr);
108 static void twe_panic(struct twe_softc *sc, char *reason);
110 /********************************************************************************
111 ********************************************************************************
113 ********************************************************************************
114 ********************************************************************************/
116 /********************************************************************************
117 * Initialise the controller, set up driver data structures.
120 twe_setup(struct twe_softc *sc)
122 struct twe_request *tr;
124 u_int32_t status_reg;
130 * Initialise request queues.
136 twe_initq_complete(sc);
137 sc->twe_wait_aen = -1;
140 * Allocate request structures up front.
142 for (i = 0; i < TWE_Q_LENGTH; i++) {
143 if ((tr = twe_allocate_request(sc, i)) == NULL)
146 * Set global defaults that won't change.
148 cmd = TWE_FIND_COMMAND(tr);
149 cmd->generic.host_id = sc->twe_host_id; /* controller-assigned host ID */
150 cmd->generic.request_id = i; /* our index number */
151 sc->twe_lookup[i] = tr;
154 * Put command onto the freelist.
157 twe_release_request(tr);
163 * Check status register for errors, clear them.
165 status_reg = TWE_STATUS(sc);
166 twe_check_bits(sc, status_reg);
169 * Wait for the controller to come ready.
171 if (twe_wait_status(sc, TWE_STATUS_MICROCONTROLLER_READY, 60)) {
173 twe_printf(sc, "microcontroller not ready\n");
178 * Disable interrupts from the card.
180 twe_disable_interrupts(sc);
183 * Soft reset the controller, look for the AEN acknowledging the reset,
184 * check for errors, drain the response queue.
186 for (i = 0; i < TWE_MAX_RESET_TRIES; i++) {
189 twe_printf(sc, "reset %d failed, trying again\n", i);
191 if (!twe_soft_reset(sc))
192 break; /* reset process complete */
195 /* did we give up? */
196 if (i >= TWE_MAX_RESET_TRIES) {
197 twe_printf(sc, "can't initialise controller, giving up\n");
205 twe_add_unit(struct twe_softc *sc, int unit)
207 struct twe_drive *dr;
208 int table, error = 0;
210 TWE_Param *drives = NULL, *param = NULL;
211 TWE_Array_Descriptor *ud;
213 TWE_CONFIG_ASSERT_LOCKED(sc);
214 if (unit < 0 || unit > TWE_MAX_UNITS)
218 * The controller is in a safe state, so try to find drives attached to it.
221 if ((drives = twe_get_param(sc, TWE_PARAM_UNITSUMMARY, TWE_PARAM_UNITSUMMARY_Status,
222 TWE_MAX_UNITS, NULL)) == NULL) {
224 twe_printf(sc, "can't detect attached units\n");
228 dr = &sc->twe_drive[unit];
229 /* check that the drive is online */
230 if (!(drives->data[unit] & TWE_PARAM_UNITSTATUS_Online)) {
236 table = TWE_PARAM_UNITINFO + unit;
238 if (twe_get_param_4(sc, table, TWE_PARAM_UNITINFO_Capacity, &dr->td_size)) {
240 twe_printf(sc, "error fetching capacity for unit %d\n", unit);
244 if (twe_get_param_1(sc, table, TWE_PARAM_UNITINFO_Status, &dr->td_state)) {
246 twe_printf(sc, "error fetching state for unit %d\n", unit);
250 if (twe_get_param_2(sc, table, TWE_PARAM_UNITINFO_DescriptorSize, &dsize)) {
252 twe_printf(sc, "error fetching descriptor size for unit %d\n", unit);
256 if ((param = twe_get_param(sc, table, TWE_PARAM_UNITINFO_Descriptor, dsize - 3, NULL)) == NULL) {
258 twe_printf(sc, "error fetching descriptor for unit %d\n", unit);
262 ud = (TWE_Array_Descriptor *)param->data;
263 dr->td_type = ud->configuration;
264 dr->td_stripe = ud->stripe_size;
266 /* build synthetic geometry as per controller internal rules */
267 if (dr->td_size > 0x200000) {
274 dr->td_cylinders = dr->td_size / (dr->td_heads * dr->td_sectors);
275 dr->td_twe_unit = unit;
278 error = twe_attach_drive(sc, dr);
282 free(param, M_DEVBUF);
284 free(drives, M_DEVBUF);
289 twe_del_unit(struct twe_softc *sc, int unit)
293 TWE_CONFIG_ASSERT_LOCKED(sc);
294 if (unit < 0 || unit >= TWE_MAX_UNITS)
297 if (sc->twe_drive[unit].td_disk == NULL)
300 error = twe_detach_drive(sc, unit);
304 /********************************************************************************
305 * Locate disk devices and attach children to them.
308 twe_init(struct twe_softc *sc)
316 for (i = 0; i < TWE_MAX_UNITS; i++)
318 TWE_CONFIG_UNLOCK(sc);
321 * Initialise connection with controller.
324 twe_init_connection(sc, TWE_INIT_MESSAGE_CREDITS);
326 #ifdef TWE_SHUTDOWN_NOTIFICATION
328 * Tell the controller we support shutdown notification.
330 twe_set_param_1(sc, TWE_PARAM_FEATURES, TWE_PARAM_FEATURES_DriverShutdown, 1);
334 * Mark controller up and ready to run.
336 sc->twe_state &= ~TWE_STATE_SHUTDOWN;
339 * Finally enable interrupts.
341 twe_enable_interrupts(sc);
345 /********************************************************************************
346 * Stop the controller
349 twe_deinit(struct twe_softc *sc)
352 * Mark the controller as shutting down, and disable any further interrupts.
354 TWE_IO_ASSERT_LOCKED(sc);
355 sc->twe_state |= TWE_STATE_SHUTDOWN;
356 twe_disable_interrupts(sc);
358 #ifdef TWE_SHUTDOWN_NOTIFICATION
360 * Disconnect from the controller
362 twe_init_connection(sc, TWE_SHUTDOWN_MESSAGE_CREDITS);
366 /*******************************************************************************
367 * Take an interrupt, or be poked by other code to look for interrupt-worthy
371 twe_intr(struct twe_softc *sc)
373 u_int32_t status_reg;
378 * Collect current interrupt status.
380 status_reg = TWE_STATUS(sc);
381 twe_check_bits(sc, status_reg);
384 * Dispatch based on interrupt status
386 if (status_reg & TWE_STATUS_HOST_INTERRUPT)
388 if (status_reg & TWE_STATUS_ATTENTION_INTERRUPT)
389 twe_attention_intr(sc);
390 if (status_reg & TWE_STATUS_COMMAND_INTERRUPT)
391 twe_command_intr(sc);
392 if (status_reg & TWE_STATUS_RESPONSE_INTERRUPT)
396 /********************************************************************************
397 * Pull as much work off the softc's work queue as possible and give it to the
401 twe_startio(struct twe_softc *sc)
403 struct twe_request *tr;
410 TWE_IO_ASSERT_LOCKED(sc);
411 if (sc->twe_state & (TWE_STATE_CTLR_BUSY | TWE_STATE_FRZN))
414 /* spin until something prevents us from doing any work */
417 /* try to get a command that's already ready to go */
418 tr = twe_dequeue_ready(sc);
420 /* build a command from an outstanding bio */
423 /* get a command to handle the bio with */
424 if (twe_get_request(sc, &tr))
427 /* see if there's work to be done */
428 if ((bp = twe_dequeue_bio(sc)) == NULL) {
429 twe_release_request(tr);
433 /* connect the bio to the command */
434 tr->tr_complete = twe_completeio;
436 tr->tr_data = bp->bio_data;
437 tr->tr_length = bp->bio_bcount;
438 cmd = TWE_FIND_COMMAND(tr);
439 if (bp->bio_cmd == BIO_READ) {
440 tr->tr_flags |= TWE_CMD_DATAIN;
441 cmd->io.opcode = TWE_OP_READ;
443 tr->tr_flags |= TWE_CMD_DATAOUT;
444 cmd->io.opcode = TWE_OP_WRITE;
447 /* build a suitable I/O command (assumes 512-byte rounded transfers) */
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;
454 /* did we find something to do? */
458 /* try to map and submit the command to controller */
459 error = twe_map_request(tr);
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;
471 twe_release_request(tr);
472 } else if (tr->tr_flags & TWE_CMD_SLEEPER)
473 wakeup_one(tr); /* wakeup the sleeping owner */
478 /********************************************************************************
479 * Write blocks from memory to disk, for system crash dumps.
482 twe_dump_blocks(struct twe_softc *sc, int unit, u_int32_t lba, void *data, int nblks)
484 struct twe_request *tr;
488 if (twe_get_request(sc, &tr))
492 tr->tr_status = TWE_CMD_SETUP;
493 tr->tr_length = nblks * TWE_BLOCK_SIZE;
494 tr->tr_flags = TWE_CMD_DATAOUT;
496 cmd = TWE_FIND_COMMAND(tr);
497 cmd->io.opcode = TWE_OP_WRITE;
500 cmd->io.block_count = nblks;
503 error = twe_immediate_request(tr, 0);
505 if (twe_report_request(tr))
507 twe_release_request(tr);
511 /********************************************************************************
512 * Handle controller-specific control operations.
515 twe_ioctl(struct twe_softc *sc, u_long ioctlcmd, void *addr)
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;
524 u_int16_t *aen_code = (u_int16_t *)addr;
525 struct twe_request *tr;
532 /* handle a command from userspace */
535 * if there's a data buffer, allocate and copy it in.
536 * Must be in multiplied of 512 bytes.
538 tr_length = roundup2(tu->tu_size, 512);
540 data = malloc(tr_length, M_DEVBUF, M_WAITOK);
541 error = copyin(tu->tu_data, data, tu->tu_size);
543 free(data, M_DEVBUF);
551 while (twe_get_request(sc, &tr))
552 mtx_sleep(sc, &sc->twe_io_lock, PPAUSE, "twioctl", hz);
555 * Save the command's request ID, copy the user-supplied command in,
556 * restore the request ID.
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;
563 tr->tr_length = tr_length;
565 if (tr->tr_length > 0) {
566 tr->tr_flags |= TWE_CMD_DATAIN | TWE_CMD_DATAOUT;
569 /* run the command */
570 error = twe_wait_request(tr);
575 /* copy the command out again */
576 bcopy(cmd, &tu->tu_command, sizeof(TWE_Command));
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);
584 if (tr->tr_data != NULL)
585 free(tr->tr_data, M_DEVBUF);
587 twe_release_request(tr);
592 /* fetch statistics counter */
594 switch (ts->ts_item) {
595 #ifdef TWE_PERFORMANCE_MONITOR
602 bcopy(&sc->twe_qstat[ts->ts_item], &ts->ts_qstat, sizeof(struct twe_qstat));
612 /* poll for an AEN */
615 *aen_code = twe_dequeue_aen(sc);
619 /* wait for another AEN to show up */
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,
631 case TWEIO_GET_PARAM:
633 param = twe_get_param(sc, tp->tp_table_id, tp->tp_param_id, tp->tp_size, 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);
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);
645 error = copyout(param->data, tp->tp_data, param->parameter_size_bytes);
647 free(param, M_DEVBUF);
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);
656 error = twe_set_param(sc, tp->tp_table_id, tp->tp_param_id, tp->tp_size, data);
659 free(data, M_DEVBUF);
670 error = twe_add_unit(sc, td->td_unit);
671 TWE_CONFIG_UNLOCK(sc);
676 error = twe_del_unit(sc, td->td_unit);
677 TWE_CONFIG_UNLOCK(sc);
680 /* XXX implement ATA PASSTHROUGH */
682 /* nothing we understand */
690 /********************************************************************************
691 * Enable the useful interrupts from the controller.
694 twe_enable_interrupts(struct twe_softc *sc)
696 sc->twe_state |= TWE_STATE_INTEN;
698 TWE_CONTROL_CLEAR_ATTENTION_INTERRUPT |
699 TWE_CONTROL_UNMASK_RESPONSE_INTERRUPT |
700 TWE_CONTROL_ENABLE_INTERRUPTS);
703 /********************************************************************************
704 * Disable interrupts from the controller.
707 twe_disable_interrupts(struct twe_softc *sc)
709 TWE_CONTROL(sc, TWE_CONTROL_DISABLE_INTERRUPTS);
710 sc->twe_state &= ~TWE_STATE_INTEN;
713 /********************************************************************************
714 ********************************************************************************
716 ********************************************************************************
717 ********************************************************************************/
719 /********************************************************************************
720 * Read integer parameter table entries.
723 twe_get_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t *result)
727 if ((param = twe_get_param(sc, table_id, param_id, 1, NULL)) == NULL)
729 *result = *(u_int8_t *)param->data;
730 free(param, M_DEVBUF);
735 twe_get_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t *result)
739 if ((param = twe_get_param(sc, table_id, param_id, 2, NULL)) == NULL)
741 *result = *(u_int16_t *)param->data;
742 free(param, M_DEVBUF);
747 twe_get_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t *result)
751 if ((param = twe_get_param(sc, table_id, param_id, 4, NULL)) == NULL)
753 *result = *(u_int32_t *)param->data;
754 free(param, M_DEVBUF);
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.
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))
768 struct twe_request *tr;
775 TWE_IO_ASSERT_LOCKED(sc);
780 if (twe_get_request(sc, &tr))
784 if ((param = (TWE_Param *)malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_NOWAIT)) == NULL)
787 tr->tr_length = TWE_SECTOR_SIZE;
788 tr->tr_flags = TWE_CMD_DATAIN | TWE_CMD_DATAOUT;
790 /* build the command for the controller */
791 cmd = TWE_FIND_COMMAND(tr);
792 cmd->param.opcode = TWE_OP_GET_PARAM;
795 cmd->param.param_count = 1;
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;
802 /* submit the command and either wait or let the callback handle it */
804 /* XXX could use twe_wait_request here if interrupts were enabled? */
805 error = twe_immediate_request(tr, 1 /* usetmp */);
807 if (twe_report_request(tr))
812 twe_release_request(tr);
815 tr->tr_complete = func;
816 error = twe_map_request(tr);
817 if ((error == 0) || (error == EBUSY))
821 /* something failed */
825 twe_release_request(tr);
827 free(param, M_DEVBUF);
831 /********************************************************************************
832 * Set integer parameter table entries.
834 #ifdef TWE_SHUTDOWN_NOTIFICATION
836 twe_set_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t value)
838 return(twe_set_param(sc, table_id, param_id, sizeof(value), &value));
844 twe_set_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t value)
846 return(twe_set_param(sc, table_id, param_id, sizeof(value), &value));
850 twe_set_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t value)
852 return(twe_set_param(sc, table_id, param_id, sizeof(value), &value));
856 /********************************************************************************
857 * Perform a TWE_OP_SET_PARAM command, returns nonzero on error.
860 twe_set_param(struct twe_softc *sc, int table_id, int param_id, int param_size, void *data)
862 struct twe_request *tr;
869 TWE_IO_ASSERT_LOCKED(sc);
875 if (twe_get_request(sc, &tr))
879 if ((param = (TWE_Param *)malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_NOWAIT)) == NULL)
882 tr->tr_length = TWE_SECTOR_SIZE;
883 tr->tr_flags = TWE_CMD_DATAIN | TWE_CMD_DATAOUT;
885 /* build the command for the controller */
886 cmd = TWE_FIND_COMMAND(tr);
887 cmd->param.opcode = TWE_OP_SET_PARAM;
890 cmd->param.param_count = 1;
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);
898 /* XXX could use twe_wait_request here if interrupts were enabled? */
899 error = twe_immediate_request(tr, 1 /* usetmp */);
901 if (twe_report_request(tr))
907 twe_release_request(tr);
909 free(param, M_DEVBUF);
913 /********************************************************************************
914 * Perform a TWE_OP_INIT_CONNECTION command, returns nonzero on error.
916 * Typically called with interrupts disabled.
919 twe_init_connection(struct twe_softc *sc, int mode)
921 struct twe_request *tr;
927 TWE_IO_ASSERT_LOCKED(sc);
930 if (twe_get_request(sc, &tr))
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;
941 /* submit the command */
942 error = twe_immediate_request(tr, 0 /* usetmp */);
943 twe_release_request(tr);
945 if (mode == TWE_INIT_MESSAGE_CREDITS)
946 sc->twe_host_id = cmd->initconnection.host_id;
950 /********************************************************************************
951 * Start the command (tr) and sleep waiting for it to complete.
953 * Successfully completed commands are dequeued.
956 twe_wait_request(struct twe_request *tr)
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);
969 return(tr->tr_status != TWE_CMD_COMPLETE);
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).
978 twe_immediate_request(struct twe_request *tr, int usetmp)
980 struct twe_softc *sc;
988 if (usetmp && (tr->tr_data != NULL)) {
989 tr->tr_flags |= TWE_CMD_IMMEDIATE;
990 if (tr->tr_length > DFLTPHYS)
992 bcopy(tr->tr_data, sc->twe_immediate, tr->tr_length);
994 tr->tr_status = TWE_CMD_BUSY;
995 if ((error = twe_map_request(tr)) != 0)
999 /* Wait up to 5 seconds for the command to complete */
1000 while ((count++ < 5000) && (tr->tr_status == TWE_CMD_BUSY)){
1004 if (usetmp && (tr->tr_data != NULL))
1005 bcopy(sc->twe_immediate, tr->tr_data, tr->tr_length);
1007 return(tr->tr_status != TWE_CMD_COMPLETE);
1010 /********************************************************************************
1011 * Handle completion of an I/O command.
1014 twe_completeio(struct twe_request *tr)
1016 TWE_Command *cmd = TWE_FIND_COMMAND(tr);
1017 struct twe_softc *sc = tr->tr_sc;
1018 struct bio *bp = tr->tr_private;
1022 if (tr->tr_status == TWE_CMD_COMPLETE) {
1024 if (cmd->generic.status)
1025 if (twe_report_request(tr)) {
1026 bp->bio_error = EIO;
1027 bp->bio_flags |= BIO_ERROR;
1031 twe_panic(sc, "twe_completeio on incomplete command");
1033 tr->tr_private = NULL;
1035 twe_release_request(tr);
1038 /********************************************************************************
1039 * Reset the controller and pull all the active commands back onto the ready
1040 * queue. Used to restart a controller that's exhibiting bad behaviour.
1043 twe_reset(struct twe_softc *sc)
1045 struct twe_request *tr;
1049 * Sleep for a short period to allow AENs to be signalled.
1051 mtx_sleep(sc, &sc->twe_io_lock, PRIBIO, "twereset", hz);
1054 * Disable interrupts from the controller, and mask any accidental entry
1055 * into our interrupt handler.
1057 twe_printf(sc, "controller reset in progress...\n");
1058 twe_disable_interrupts(sc);
1061 * Try to soft-reset the controller.
1063 for (i = 0; i < TWE_MAX_RESET_TRIES; i++) {
1066 twe_printf(sc, "reset %d failed, trying again\n", i);
1068 if (!twe_soft_reset(sc))
1069 break; /* reset process complete */
1071 /* did we give up? */
1072 if (i >= TWE_MAX_RESET_TRIES) {
1073 twe_printf(sc, "can't reset controller, giving up\n");
1078 * Move all of the commands that were busy back to the ready queue.
1081 while ((tr = twe_dequeue_busy(sc)) != NULL) {
1082 twe_enqueue_ready(tr);
1087 * Kick the controller to start things going again, then re-enable interrupts.
1090 twe_printf(sc, "controller reset done, %d commands restarted\n", i);
1093 twe_enable_interrupts(sc);
1096 /********************************************************************************
1097 ********************************************************************************
1098 Command I/O to Controller
1099 ********************************************************************************
1100 ********************************************************************************/
1102 /********************************************************************************
1103 * Try to deliver (tr) to the controller.
1105 * Can be called at any interrupt level, with or without interrupts enabled.
1108 twe_start(struct twe_request *tr)
1110 struct twe_softc *sc = tr->tr_sc;
1113 u_int32_t status_reg;
1118 TWE_IO_ASSERT_LOCKED(sc);
1120 /* mark the command as currently being processed */
1121 tr->tr_status = TWE_CMD_BUSY;
1122 cmd = TWE_FIND_COMMAND(tr);
1125 * Spin briefly waiting for the controller to come ready
1127 * XXX it might be more efficient to return EBUSY immediately
1128 * and let the command be rescheduled.
1130 for (i = 100000; (i > 0); i--) {
1132 /* check to see if we can post a command */
1133 status_reg = TWE_STATUS(sc);
1134 twe_check_bits(sc, status_reg);
1136 if (!(status_reg & TWE_STATUS_COMMAND_QUEUE_FULL)) {
1137 twe_enqueue_busy(tr);
1139 TWE_COMMAND_QUEUE(sc, TWE_FIND_COMMANDPHYS(tr));
1141 /* move command to work queue */
1143 if (tr->tr_complete != NULL) {
1144 debug(3, "queued request %d with callback %p", cmd->generic.request_id, tr->tr_complete);
1145 } else if (tr->tr_flags & TWE_CMD_SLEEPER) {
1146 debug(3, "queued request %d with wait channel %p", cmd->generic.request_id, tr);
1148 debug(3, "queued request %d for polling caller", cmd->generic.request_id);
1152 } else if (!(status_reg & TWE_STATUS_RESPONSE_QUEUE_EMPTY) && i > 1)
1157 * We couldn't get the controller to take the command; try submitting it again later.
1158 * This should only happen if something is wrong with the controller, or if we have
1159 * overestimated the number of commands it can accept. (Should we actually reject
1160 * the command at this point?)
1165 /********************************************************************************
1166 * Poll the controller (sc) for completed commands.
1168 * Can be called at any interrupt level, with or without interrupts enabled.
1171 twe_done(struct twe_softc *sc, int startio)
1173 TWE_Response_Queue rq;
1175 struct twe_request *tr;
1177 u_int32_t status_reg;
1181 /* loop collecting completed commands */
1184 status_reg = TWE_STATUS(sc);
1185 twe_check_bits(sc, status_reg); /* XXX should this fail? */
1187 if (!(status_reg & TWE_STATUS_RESPONSE_QUEUE_EMPTY)) {
1189 rq = TWE_RESPONSE_QUEUE(sc);
1190 tr = sc->twe_lookup[rq.u.response_id]; /* find command */
1191 cmd = TWE_FIND_COMMAND(tr);
1192 if (tr->tr_status != TWE_CMD_BUSY)
1193 twe_printf(sc, "completion event for nonbusy command\n");
1194 tr->tr_status = TWE_CMD_COMPLETE;
1195 debug(3, "completed request id %d with status %d",
1196 cmd->generic.request_id, cmd->generic.status);
1197 /* move to completed queue */
1198 twe_remove_busy(tr);
1199 twe_enqueue_complete(tr);
1200 sc->twe_state &= ~TWE_STATE_CTLR_BUSY;
1202 break; /* no response ready */
1206 /* if we've completed any commands, try posting some more */
1207 if (found && startio)
1210 /* handle completion and timeouts */
1211 twe_complete(sc); /* XXX use deferred completion? */
1214 /********************************************************************************
1215 * Perform post-completion processing for commands on (sc).
1217 * This is split from twe_done as it can be safely deferred and run at a lower
1218 * priority level should facilities for such a thing become available.
1221 twe_complete(struct twe_softc *sc)
1223 struct twe_request *tr;
1228 * Pull commands off the completed list, dispatch them appropriately
1230 while ((tr = twe_dequeue_complete(sc)) != NULL) {
1231 /* unmap the command's data buffer */
1232 twe_unmap_request(tr);
1234 /* dispatch to suit command originator */
1235 if (tr->tr_complete != NULL) { /* completion callback */
1236 debug(2, "call completion handler %p", tr->tr_complete);
1237 tr->tr_complete(tr);
1239 } else if (tr->tr_flags & TWE_CMD_SLEEPER) { /* caller is asleep waiting */
1240 debug(2, "wake up command owner on %p", tr);
1243 } else { /* caller is polling command */
1244 debug(2, "command left for owner");
1249 /********************************************************************************
1250 * Wait for (status) to be set in the controller status register for up to
1251 * (timeout) seconds. Returns 0 if found, nonzero if we time out.
1253 * Note: this busy-waits, rather than sleeping, since we may be called with
1254 * eg. clock interrupts masked.
1257 twe_wait_status(struct twe_softc *sc, u_int32_t status, int timeout)
1260 u_int32_t status_reg;
1264 expiry = time_second + timeout;
1267 status_reg = TWE_STATUS(sc);
1268 if (status_reg & status) /* got the required bit(s)? */
1271 } while (time_second <= expiry);
1276 /********************************************************************************
1277 * Drain the response queue, which may contain responses to commands we know
1281 twe_drain_response_queue(struct twe_softc *sc)
1283 TWE_Response_Queue rq;
1284 u_int32_t status_reg;
1288 for (;;) { /* XXX give up eventually? */
1289 status_reg = TWE_STATUS(sc);
1290 if (twe_check_bits(sc, status_reg))
1292 if (status_reg & TWE_STATUS_RESPONSE_QUEUE_EMPTY)
1294 rq = TWE_RESPONSE_QUEUE(sc);
1298 /********************************************************************************
1299 * Soft-reset the controller
1302 twe_soft_reset(struct twe_softc *sc)
1304 u_int32_t status_reg;
1308 TWE_IO_ASSERT_LOCKED(sc);
1311 if (twe_wait_status(sc, TWE_STATUS_ATTENTION_INTERRUPT, 30)) {
1312 twe_printf(sc, "no attention interrupt\n");
1315 TWE_CONTROL(sc, TWE_CONTROL_CLEAR_ATTENTION_INTERRUPT);
1316 if (twe_drain_aen_queue(sc)) {
1317 twe_printf(sc, "can't drain AEN queue\n");
1320 if (twe_find_aen(sc, TWE_AEN_SOFT_RESET)) {
1321 twe_printf(sc, "reset not reported\n");
1324 status_reg = TWE_STATUS(sc);
1325 if (TWE_STATUS_ERRORS(status_reg) || twe_check_bits(sc, status_reg)) {
1326 twe_printf(sc, "controller errors detected\n");
1329 if (twe_drain_response_queue(sc)) {
1330 twe_printf(sc, "can't drain response queue\n");
1336 /********************************************************************************
1337 ********************************************************************************
1339 ********************************************************************************
1340 ********************************************************************************/
1342 /********************************************************************************
1345 * XXX what does this mean?
1348 twe_host_intr(struct twe_softc *sc)
1352 twe_printf(sc, "host interrupt\n");
1353 TWE_CONTROL(sc, TWE_CONTROL_CLEAR_HOST_INTERRUPT);
1356 /********************************************************************************
1357 * Attention interrupt.
1359 * Signalled when the controller has one or more AENs for us.
1362 twe_attention_intr(struct twe_softc *sc)
1366 /* instigate a poll for AENs */
1367 if (twe_fetch_aen(sc)) {
1368 twe_printf(sc, "error polling for signalled AEN\n");
1370 TWE_CONTROL(sc, TWE_CONTROL_CLEAR_ATTENTION_INTERRUPT);
1374 /********************************************************************************
1375 * Command interrupt.
1377 * Signalled when the controller can handle more commands.
1380 twe_command_intr(struct twe_softc *sc)
1385 * We don't use this, rather we try to submit commands when we receive
1386 * them, and when other commands have completed. Mask it so we don't get
1389 TWE_CONTROL(sc, TWE_CONTROL_MASK_COMMAND_INTERRUPT);
1392 /********************************************************************************
1393 ********************************************************************************
1394 Asynchronous Event Handling
1395 ********************************************************************************
1396 ********************************************************************************/
1398 /********************************************************************************
1399 * Request an AEN from the controller.
1402 twe_fetch_aen(struct twe_softc *sc)
1407 if ((twe_get_param(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode, 2, twe_handle_aen)) == NULL)
1412 /********************************************************************************
1413 * Handle an AEN returned by the controller.
1416 twe_handle_aen(struct twe_request *tr)
1418 struct twe_softc *sc = tr->tr_sc;
1424 /* XXX check for command success somehow? */
1426 param = (TWE_Param *)tr->tr_data;
1427 aen = *(u_int16_t *)(param->data);
1429 free(tr->tr_data, M_DEVBUF);
1430 twe_release_request(tr);
1431 twe_enqueue_aen(sc, aen);
1433 /* XXX poll for more AENs? */
1436 /********************************************************************************
1437 * Pull AENs out of the controller and park them in the queue, in a context where
1438 * interrupts aren't active. Return nonzero if we encounter any errors in the
1439 * process of obtaining all the available AENs.
1442 twe_drain_aen_queue(struct twe_softc *sc)
1446 TWE_IO_ASSERT_LOCKED(sc);
1448 if (twe_get_param_2(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode, &aen))
1450 if (aen == TWE_AEN_QUEUE_EMPTY)
1452 twe_enqueue_aen(sc, aen);
1456 /********************************************************************************
1457 * Push an AEN that we've received onto the queue.
1459 * Note that we have to lock this against reentrance, since it may be called
1460 * from both interrupt and non-interrupt context.
1462 * If someone is waiting for the AEN we have, wake them up.
1465 twe_enqueue_aen(struct twe_softc *sc, u_int16_t aen)
1472 TWE_IO_ASSERT_LOCKED(sc);
1473 if ((msg = twe_format_aen(sc, aen)) != NULL)
1474 twe_printf(sc, "AEN: <%s>\n", msg);
1476 /* enqueue the AEN */
1477 next = ((sc->twe_aen_head + 1) % TWE_Q_LENGTH);
1478 nextnext = ((sc->twe_aen_head + 2) % TWE_Q_LENGTH);
1480 /* check to see if this is the last free slot, and subvert the AEN if it is */
1481 if (nextnext == sc->twe_aen_tail)
1482 aen = TWE_AEN_QUEUE_FULL;
1484 /* look to see if there's room for this AEN */
1485 if (next != sc->twe_aen_tail) {
1486 sc->twe_aen_queue[sc->twe_aen_head] = aen;
1487 sc->twe_aen_head = next;
1490 /* wake up anyone asleep on the queue */
1491 wakeup(&sc->twe_aen_queue);
1493 /* anyone looking for this AEN? */
1494 if (sc->twe_wait_aen == aen) {
1495 sc->twe_wait_aen = -1;
1496 wakeup(&sc->twe_wait_aen);
1500 /********************************************************************************
1501 * Pop an AEN off the queue, or return -1 if there are none left.
1503 * We are more or less interrupt-safe, so don't block interrupts.
1506 twe_dequeue_aen(struct twe_softc *sc)
1512 TWE_IO_ASSERT_LOCKED(sc);
1513 if (sc->twe_aen_tail == sc->twe_aen_head) {
1514 result = TWE_AEN_QUEUE_EMPTY;
1516 result = sc->twe_aen_queue[sc->twe_aen_tail];
1517 sc->twe_aen_tail = ((sc->twe_aen_tail + 1) % TWE_Q_LENGTH);
1522 /********************************************************************************
1523 * Check to see if the requested AEN is in the queue.
1525 * XXX we could probably avoid masking interrupts here
1528 twe_find_aen(struct twe_softc *sc, u_int16_t aen)
1533 for (i = sc->twe_aen_tail; (i != sc->twe_aen_head) && missing; i = (i + 1) % TWE_Q_LENGTH) {
1534 if (sc->twe_aen_queue[i] == aen)
1541 #if 0 /* currently unused */
1542 /********************************************************************************
1543 * Sleep waiting for at least (timeout) seconds until we see (aen) as
1544 * requested. Returns nonzero on timeout or failure.
1546 * XXX: this should not be used in cases where there may be more than one sleeper
1547 * without a mechanism for registering multiple sleepers.
1550 twe_wait_aen(struct twe_softc *sc, int aen, int timeout)
1557 expiry = time_second + timeout;
1560 sc->twe_wait_aen = aen;
1563 mtx_sleep(&sc->twe_wait_aen, &sc->twe_io_lock, PZERO, "twewaen", hz);
1564 if (sc->twe_wait_aen == -1)
1566 } while ((time_second <= expiry) && !found);
1571 /********************************************************************************
1572 ********************************************************************************
1573 Command Buffer Management
1574 ********************************************************************************
1575 ********************************************************************************/
1577 /********************************************************************************
1578 * Get a new command buffer.
1580 * This will return NULL if all command buffers are in use.
1583 twe_get_request(struct twe_softc *sc, struct twe_request **tr)
1589 TWE_IO_ASSERT_LOCKED(sc);
1591 /* try to reuse an old buffer */
1592 *tr = twe_dequeue_free(sc);
1594 /* initialise some fields to their defaults */
1596 cmd = TWE_FIND_COMMAND(*tr);
1597 (*tr)->tr_data = NULL;
1598 (*tr)->tr_private = NULL;
1599 (*tr)->tr_status = TWE_CMD_SETUP; /* command is in setup phase */
1600 (*tr)->tr_flags = 0;
1601 (*tr)->tr_complete = NULL;
1602 cmd->generic.status = 0; /* before submission to controller */
1603 cmd->generic.flags = 0; /* not used */
1605 return(*tr == NULL);
1608 /********************************************************************************
1609 * Release a command buffer for reuse.
1613 twe_release_request(struct twe_request *tr)
1618 TWE_IO_ASSERT_LOCKED(tr->tr_sc);
1619 if (tr->tr_private != NULL)
1620 twe_panic(tr->tr_sc, "tr_private != NULL");
1621 twe_enqueue_free(tr);
1624 /********************************************************************************
1625 ********************************************************************************
1627 ********************************************************************************
1628 ********************************************************************************/
1630 /********************************************************************************
1631 * Print some information about the controller
1634 twe_describe_controller(struct twe_softc *sc)
1645 /* get the port count */
1646 twe_get_param_1(sc, TWE_PARAM_CONTROLLER, TWE_PARAM_CONTROLLER_PortCount, &ports);
1648 /* get version strings */
1649 p[0] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_FW, 16, NULL);
1650 p[1] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_BIOS, 16, NULL);
1652 twe_printf(sc, "%d ports, Firmware %.16s, BIOS %.16s\n", ports, p[0]->data, p[1]->data);
1655 p[2] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_Mon, 16, NULL);
1656 p[3] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_PCB, 8, NULL);
1657 p[4] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_ATA, 8, NULL);
1658 p[5] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_PCI, 8, NULL);
1660 if (p[2] && p[3] && p[4] && p[5])
1661 twe_printf(sc, "Monitor %.16s, PCB %.8s, Achip %.8s, Pchip %.8s\n", p[2]->data, p[3]->data,
1662 p[4]->data, p[5]->data);
1664 free(p[2], M_DEVBUF);
1666 free(p[3], M_DEVBUF);
1668 free(p[4], M_DEVBUF);
1670 free(p[5], M_DEVBUF);
1673 free(p[0], M_DEVBUF);
1675 free(p[1], M_DEVBUF);
1677 /* print attached drives */
1679 p[0] = twe_get_param(sc, TWE_PARAM_DRIVESUMMARY, TWE_PARAM_DRIVESUMMARY_Status, 16, NULL);
1680 for (i = 0; i < ports; i++) {
1681 if (p[0]->data[i] != TWE_PARAM_DRIVESTATUS_Present)
1683 twe_get_param_4(sc, TWE_PARAM_DRIVEINFO + i, TWE_PARAM_DRIVEINFO_Size, &size);
1684 p[1] = twe_get_param(sc, TWE_PARAM_DRIVEINFO + i, TWE_PARAM_DRIVEINFO_Model, 40, NULL);
1686 twe_printf(sc, "port %d: %.40s %dMB\n", i, p[1]->data, size / 2048);
1687 free(p[1], M_DEVBUF);
1689 twe_printf(sc, "port %d, drive status unavailable\n", i);
1693 free(p[0], M_DEVBUF);
1698 /********************************************************************************
1699 * Look up a text description of a numeric code and return a pointer to same.
1702 twe_describe_code(struct twe_code_lookup *table, u_int32_t code)
1706 for (i = 0; table[i].string != NULL; i++)
1707 if (table[i].code == code)
1708 return(table[i].string);
1709 return(table[i+1].string);
1712 /********************************************************************************
1713 * Complain if the status bits aren't what we're expecting.
1715 * Rate-limit the complaints to at most one of each every five seconds, but
1716 * always return the correct status.
1719 twe_check_bits(struct twe_softc *sc, u_int32_t status_reg)
1722 static time_t lastwarn[2] = {0, 0};
1725 * This can be a little problematic, as twe_panic may call twe_reset if
1726 * TWE_DEBUG is not set, which will call us again as part of the soft reset.
1728 if ((status_reg & TWE_STATUS_PANIC_BITS) != 0) {
1729 twe_printf(sc, "FATAL STATUS BIT(S) %b\n", status_reg & TWE_STATUS_PANIC_BITS,
1730 TWE_STATUS_BITS_DESCRIPTION);
1731 twe_panic(sc, "fatal status bits");
1735 if ((status_reg & TWE_STATUS_EXPECTED_BITS) != TWE_STATUS_EXPECTED_BITS) {
1736 if (time_second > (lastwarn[0] + 5)) {
1737 twe_printf(sc, "missing expected status bit(s) %b\n", ~status_reg & TWE_STATUS_EXPECTED_BITS,
1738 TWE_STATUS_BITS_DESCRIPTION);
1739 lastwarn[0] = time_second;
1744 if ((status_reg & TWE_STATUS_UNEXPECTED_BITS) != 0) {
1745 if (time_second > (lastwarn[1] + 5)) {
1746 twe_printf(sc, "unexpected status bit(s) %b\n", status_reg & TWE_STATUS_UNEXPECTED_BITS,
1747 TWE_STATUS_BITS_DESCRIPTION);
1748 lastwarn[1] = time_second;
1751 if (status_reg & TWE_STATUS_PCI_PARITY_ERROR) {
1752 twe_printf(sc, "PCI parity error: Reseat card, move card or buggy device present.\n");
1753 twe_clear_pci_parity_error(sc);
1755 if (status_reg & TWE_STATUS_PCI_ABORT) {
1756 twe_printf(sc, "PCI abort, clearing.\n");
1757 twe_clear_pci_abort(sc);
1764 /********************************************************************************
1765 * Return a string describing (aen).
1767 * The low 8 bits of the aen are the code, the high 8 bits give the unit number
1768 * where an AEN is specific to a unit.
1770 * Note that we could expand this routine to handle eg. up/downgrading the status
1771 * of a drive if we had some idea of what the drive's initial status was.
1775 twe_format_aen(struct twe_softc *sc, u_int16_t aen)
1780 code = twe_describe_code(twe_table_aen, TWE_AEN_CODE(aen));
1792 if ((child = sc->twe_drive[TWE_AEN_UNIT(aen)].td_disk) != NULL) {
1793 snprintf(sc->twe_aen_buf, sizeof(sc->twe_aen_buf), "twed%d: %s",
1794 device_get_unit(child), msg);
1796 snprintf(sc->twe_aen_buf, sizeof(sc->twe_aen_buf),
1797 "twe%d: %s for unknown unit %d", device_get_unit(sc->twe_dev),
1798 msg, TWE_AEN_UNIT(aen));
1800 return(sc->twe_aen_buf);
1803 snprintf(sc->twe_aen_buf, sizeof(sc->twe_aen_buf),
1804 "twe%d: port %d: %s", device_get_unit(sc->twe_dev),
1805 TWE_AEN_UNIT(aen), msg);
1806 return(sc->twe_aen_buf);
1813 snprintf(sc->twe_aen_buf, sizeof(sc->twe_aen_buf), "unknown AEN 0x%x", aen);
1814 return(sc->twe_aen_buf);
1817 /********************************************************************************
1818 * Print a diagnostic if the status of the command warrants it, and return
1819 * either zero (command was ok) or nonzero (command failed).
1822 twe_report_request(struct twe_request *tr)
1824 struct twe_softc *sc = tr->tr_sc;
1825 TWE_Command *cmd = TWE_FIND_COMMAND(tr);
1829 * Check the command status value and handle accordingly.
1831 if (cmd->generic.status == TWE_STATUS_RESET) {
1833 * The status code 0xff requests a controller reset.
1835 twe_printf(sc, "command returned with controller reset request\n");
1838 } else if (cmd->generic.status > TWE_STATUS_FATAL) {
1840 * Fatal errors that don't require controller reset.
1842 * We know a few special flags values.
1844 switch (cmd->generic.flags) {
1846 device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
1850 device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
1851 "unrecoverable drive error");
1854 device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
1855 "controller error - %s (flags = 0x%x)\n",
1856 twe_describe_code(twe_table_status, cmd->generic.status),
1857 cmd->generic.flags);
1860 } else if (cmd->generic.status > TWE_STATUS_WARNING) {
1862 * Warning level status.
1864 device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
1865 "warning - %s (flags = 0x%x)\n",
1866 twe_describe_code(twe_table_status, cmd->generic.status),
1867 cmd->generic.flags);
1868 } else if (cmd->generic.status > 0x40) {
1870 * Info level status.
1872 device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
1873 "attention - %s (flags = 0x%x)\n",
1874 twe_describe_code(twe_table_status, cmd->generic.status),
1875 cmd->generic.flags);
1881 /********************************************************************************
1882 * Print some controller state to aid in debugging error/panic conditions
1885 twe_print_controller(struct twe_softc *sc)
1887 u_int32_t status_reg;
1889 status_reg = TWE_STATUS(sc);
1890 twe_printf(sc, "status %b\n", status_reg, TWE_STATUS_BITS_DESCRIPTION);
1891 twe_printf(sc, " current max min\n");
1892 twe_printf(sc, "free %04d %04d %04d\n",
1893 sc->twe_qstat[TWEQ_FREE].q_length, sc->twe_qstat[TWEQ_FREE].q_max, sc->twe_qstat[TWEQ_FREE].q_min);
1895 twe_printf(sc, "ready %04d %04d %04d\n",
1896 sc->twe_qstat[TWEQ_READY].q_length, sc->twe_qstat[TWEQ_READY].q_max, sc->twe_qstat[TWEQ_READY].q_min);
1898 twe_printf(sc, "busy %04d %04d %04d\n",
1899 sc->twe_qstat[TWEQ_BUSY].q_length, sc->twe_qstat[TWEQ_BUSY].q_max, sc->twe_qstat[TWEQ_BUSY].q_min);
1901 twe_printf(sc, "complete %04d %04d %04d\n",
1902 sc->twe_qstat[TWEQ_COMPLETE].q_length, sc->twe_qstat[TWEQ_COMPLETE].q_max, sc->twe_qstat[TWEQ_COMPLETE].q_min);
1904 twe_printf(sc, "bioq %04d %04d %04d\n",
1905 sc->twe_qstat[TWEQ_BIO].q_length, sc->twe_qstat[TWEQ_BIO].q_max, sc->twe_qstat[TWEQ_BIO].q_min);
1907 twe_printf(sc, "AEN queue head %d tail %d\n", sc->twe_aen_head, sc->twe_aen_tail);
1911 twe_panic(struct twe_softc *sc, char *reason)
1913 twe_print_controller(sc);
1922 /********************************************************************************
1923 * Print a request/command in human-readable format.
1926 twe_print_request(struct twe_request *tr)
1928 struct twe_softc *sc = tr->tr_sc;
1929 TWE_Command *cmd = TWE_FIND_COMMAND(tr);
1932 twe_printf(sc, "CMD: request_id %d opcode <%s> size %d unit %d host_id %d\n",
1933 cmd->generic.request_id, twe_describe_code(twe_table_opcode, cmd->generic.opcode), cmd->generic.size,
1934 cmd->generic.unit, cmd->generic.host_id);
1935 twe_printf(sc, " status %d flags 0x%x count %d sgl_offset %d\n",
1936 cmd->generic.status, cmd->generic.flags, cmd->generic.count, cmd->generic.sgl_offset);
1938 switch(cmd->generic.opcode) { /* XXX add more opcodes? */
1941 twe_printf(sc, " lba %d\n", cmd->io.lba);
1942 for (i = 0; (i < TWE_MAX_SGL_LENGTH) && (cmd->io.sgl[i].length != 0); i++)
1943 twe_printf(sc, " %d: 0x%x/%d\n",
1944 i, cmd->io.sgl[i].address, cmd->io.sgl[i].length);
1947 case TWE_OP_GET_PARAM:
1948 case TWE_OP_SET_PARAM:
1949 for (i = 0; (i < TWE_MAX_SGL_LENGTH) && (cmd->param.sgl[i].length != 0); i++)
1950 twe_printf(sc, " %d: 0x%x/%d\n",
1951 i, cmd->param.sgl[i].address, cmd->param.sgl[i].length);
1954 case TWE_OP_INIT_CONNECTION:
1955 twe_printf(sc, " response queue pointer 0x%x\n",
1956 cmd->initconnection.response_queue_pointer);
1962 twe_printf(sc, " tr_command %p/0x%x tr_data %p/0x%x,%d\n",
1963 tr, TWE_FIND_COMMANDPHYS(tr), tr->tr_data, tr->tr_dataphys, tr->tr_length);
1964 twe_printf(sc, " tr_status %d tr_flags 0x%x tr_complete %p tr_private %p\n",
1965 tr->tr_status, tr->tr_flags, tr->tr_complete, tr->tr_private);