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
29 * $FreeBSD: src/sys/dev/twe/twe.c,v 1.1.2.10 2004/06/11 18:57:31 vkashyap Exp $
30 * $DragonFly: src/sys/dev/raid/twe/twe.c,v 1.19 2008/01/06 16:55:51 swildner Exp $
34 * Driver for the 3ware Escalade family of IDE RAID controllers.
37 #include <dev/raid/twe/twe_compat.h>
38 #include <dev/raid/twe/twereg.h>
39 #define TWE_DEFINE_TABLES
40 #include <dev/raid/twe/twe_tables.h>
41 #include <dev/raid/twe/tweio.h>
42 #include <dev/raid/twe/twevar.h>
47 static int twe_get_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t *result);
48 static int twe_get_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t *result);
49 static int twe_get_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t *result);
50 static void *twe_get_param(struct twe_softc *sc, int table_id, int parameter_id, size_t size,
51 void (* func)(struct twe_request *tr));
52 #ifdef TWE_SHUTDOWN_NOTIFICATION
53 static int twe_set_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t value);
56 static int twe_set_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t value);
57 static int twe_set_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t value);
59 static int twe_set_param(struct twe_softc *sc, int table_id, int param_id, int param_size,
61 static int twe_init_connection(struct twe_softc *sc, int mode);
62 static int twe_wait_request(struct twe_request *tr);
63 static int twe_immediate_request(struct twe_request *tr);
64 static void twe_completeio(struct twe_request *tr);
65 static void twe_reset(struct twe_softc *sc);
66 static int twe_add_unit(struct twe_softc *sc, int unit);
67 static int twe_del_unit(struct twe_softc *sc, int unit);
70 * Command I/O to controller.
72 static void twe_done(struct twe_softc *sc);
73 static void twe_complete(struct twe_softc *sc);
74 static int twe_wait_status(struct twe_softc *sc, u_int32_t status, int timeout);
75 static int twe_drain_response_queue(struct twe_softc *sc);
76 static int twe_check_bits(struct twe_softc *sc, u_int32_t status_reg);
77 static int twe_soft_reset(struct twe_softc *sc);
82 static void twe_host_intr(struct twe_softc *sc);
83 static void twe_attention_intr(struct twe_softc *sc);
84 static void twe_command_intr(struct twe_softc *sc);
87 * Asynchronous event handling.
89 static int twe_fetch_aen(struct twe_softc *sc);
90 static void twe_handle_aen(struct twe_request *tr);
91 static void twe_enqueue_aen(struct twe_softc *sc, u_int16_t aen);
92 static u_int16_t twe_dequeue_aen(struct twe_softc *sc);
93 static int twe_drain_aen_queue(struct twe_softc *sc);
94 static int twe_find_aen(struct twe_softc *sc, u_int16_t aen);
97 * Command buffer management.
99 static int twe_get_request(struct twe_softc *sc, struct twe_request **tr);
100 static void twe_release_request(struct twe_request *tr);
105 static char *twe_format_aen(struct twe_softc *sc, u_int16_t aen);
106 static int twe_report_request(struct twe_request *tr);
107 static void twe_panic(struct twe_softc *sc, char *reason);
109 /********************************************************************************
110 ********************************************************************************
112 ********************************************************************************
113 ********************************************************************************/
115 /********************************************************************************
116 * Initialise the controller, set up driver data structures.
119 twe_setup(struct twe_softc *sc)
121 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)) == NULL)
144 * Set global defaults that won't change.
146 tr->tr_command.generic.host_id = sc->twe_host_id; /* controller-assigned host ID */
147 tr->tr_command.generic.request_id = i; /* our index number */
148 sc->twe_lookup[i] = tr;
151 * Put command onto the freelist.
153 twe_release_request(tr);
157 * Check status register for errors, clear them.
159 status_reg = TWE_STATUS(sc);
160 twe_check_bits(sc, status_reg);
163 * Wait for the controller to come ready.
165 if (twe_wait_status(sc, TWE_STATUS_MICROCONTROLLER_READY, 60)) {
166 twe_printf(sc, "microcontroller not ready\n");
171 * Disable interrupts from the card.
173 twe_disable_interrupts(sc);
176 * Soft reset the controller, look for the AEN acknowledging the reset,
177 * check for errors, drain the response queue.
179 for (i = 0; i < TWE_MAX_RESET_TRIES; i++) {
182 twe_printf(sc, "reset %d failed, trying again\n", i);
184 if (!twe_soft_reset(sc))
185 break; /* reset process complete */
187 /* did we give up? */
188 if (i >= TWE_MAX_RESET_TRIES) {
189 twe_printf(sc, "can't initialise controller, giving up\n");
197 twe_add_unit(struct twe_softc *sc, int unit)
199 struct twe_drive *dr;
202 TWE_Param *drives = NULL, *param = NULL;
203 TWE_Unit_Descriptor *ud;
206 if (unit < 0 || unit > TWE_MAX_UNITS)
209 * The controller is in a safe state, so try to find drives attached to it.
211 if ((drives = twe_get_param(sc, TWE_PARAM_UNITSUMMARY, TWE_PARAM_UNITSUMMARY_Status,
212 TWE_MAX_UNITS, NULL)) == NULL) {
213 twe_printf(sc, "can't detect attached units\n");
217 dr = &sc->twe_drive[unit];
218 /* check that the drive is online */
219 if (!(drives->data[unit] & TWE_PARAM_UNITSTATUS_Online)) {
224 table = TWE_PARAM_UNITINFO + unit;
226 if (twe_get_param_4(sc, table, TWE_PARAM_UNITINFO_Capacity, &dr->td_size)) {
227 twe_printf(sc, "error fetching capacity for unit %d\n", unit);
231 if (twe_get_param_1(sc, table, TWE_PARAM_UNITINFO_Status, &dr->td_state)) {
232 twe_printf(sc, "error fetching state for unit %d\n", unit);
236 if (twe_get_param_2(sc, table, TWE_PARAM_UNITINFO_DescriptorSize, &dsize)) {
237 twe_printf(sc, "error fetching descriptor size for unit %d\n", unit);
241 if ((param = twe_get_param(sc, table, TWE_PARAM_UNITINFO_Descriptor, dsize - 3, NULL)) == NULL) {
242 twe_printf(sc, "error fetching descriptor for unit %d\n", unit);
246 ud = (TWE_Unit_Descriptor *)param->data;
247 dr->td_type = ud->configuration;
249 /* build synthetic geometry as per controller internal rules */
250 if (dr->td_size > 0x200000) {
257 dr->td_cylinders = dr->td_size / (dr->td_heads * dr->td_sectors);
258 dr->td_twe_unit = unit;
260 error = twe_attach_drive(sc, dr);
264 kfree(param, M_DEVBUF);
266 kfree(drives, M_DEVBUF);
271 twe_del_unit(struct twe_softc *sc, int unit)
275 if (unit < 0 || unit >= TWE_MAX_UNITS)
278 if (sc->twe_drive[unit].td_disk == NULL)
281 error = twe_detach_drive(sc, unit);
285 /********************************************************************************
286 * Locate disk devices and attach children to them.
289 twe_init(struct twe_softc *sc)
296 for (i = 0; i < TWE_MAX_UNITS; i++)
300 * Initialise connection with controller.
302 twe_init_connection(sc, TWE_INIT_MESSAGE_CREDITS);
304 #ifdef TWE_SHUTDOWN_NOTIFICATION
306 * Tell the controller we support shutdown notification.
308 twe_set_param_1(sc, TWE_PARAM_FEATURES, TWE_PARAM_FEATURES_DriverShutdown, 1);
312 * Mark controller up and ready to run.
314 sc->twe_state &= ~TWE_STATE_SHUTDOWN;
317 * Finally enable interrupts.
319 twe_enable_interrupts(sc);
322 /********************************************************************************
323 * Stop the controller
326 twe_deinit(struct twe_softc *sc)
329 * Mark the controller as shutting down, and disable any further interrupts.
331 sc->twe_state |= TWE_STATE_SHUTDOWN;
332 twe_disable_interrupts(sc);
334 #ifdef TWE_SHUTDOWN_NOTIFICATION
336 * Disconnect from the controller
338 twe_init_connection(sc, TWE_SHUTDOWN_MESSAGE_CREDITS);
342 /*******************************************************************************
343 * Take an interrupt, or be poked by other code to look for interrupt-worthy
347 twe_intr(struct twe_softc *sc)
349 u_int32_t status_reg;
354 * Collect current interrupt status.
356 status_reg = TWE_STATUS(sc);
357 twe_check_bits(sc, status_reg);
360 * Dispatch based on interrupt status
362 if (status_reg & TWE_STATUS_HOST_INTERRUPT)
364 if (status_reg & TWE_STATUS_ATTENTION_INTERRUPT)
365 twe_attention_intr(sc);
366 if (status_reg & TWE_STATUS_COMMAND_INTERRUPT)
367 twe_command_intr(sc);
368 if (status_reg & TWE_STATUS_RESPONSE_INTERRUPT)
372 /********************************************************************************
373 * Pull as much work off the softc's work queue as possible and give it to the
377 twe_startio(struct twe_softc *sc)
379 struct twe_request *tr;
386 if (sc->twe_state & (TWE_STATE_CTLR_BUSY | TWE_STATE_FRZN))
389 /* spin until something prevents us from doing any work */
392 /* try to get a command that's already ready to go */
393 tr = twe_dequeue_ready(sc);
395 /* build a command from an outstanding bio */
398 /* get a command to handle the bio with */
399 if (twe_get_request(sc, &tr))
402 /* see if there's work to be done */
403 if ((bio = twe_dequeue_bio(sc)) == NULL) {
404 twe_release_request(tr);
408 /* connect the bio to the command */
409 tr->tr_complete = twe_completeio;
410 tr->tr_private = bio;
411 tr->tr_data = bio->bio_buf->b_data;
412 tr->tr_length = bio->bio_buf->b_bcount;
413 cmd = &tr->tr_command;
414 if (bio->bio_buf->b_cmd == BUF_CMD_READ) {
415 tr->tr_flags |= TWE_CMD_DATAIN;
416 cmd->io.opcode = TWE_OP_READ;
418 tr->tr_flags |= TWE_CMD_DATAOUT;
419 cmd->io.opcode = TWE_OP_WRITE;
422 /* build a suitable I/O command (assumes 512-byte rounded transfers) */
424 cmd->io.unit = ((struct twed_softc *)bio->bio_driver_info)->twed_drive->td_twe_unit;
425 cmd->io.block_count = (tr->tr_length + TWE_BLOCK_SIZE - 1) / TWE_BLOCK_SIZE;
426 cmd->io.lba = (u_int32_t)(bio->bio_offset / TWE_BLOCK_SIZE);
427 KKASSERT(bio->bio_offset < 0x100000000ULL * TWE_BLOCK_SIZE);
430 /* did we find something to do? */
434 /* try to map and submit the command to controller */
435 error = twe_map_request(tr);
440 tr->tr_status = TWE_CMD_ERROR;
441 if (tr->tr_private != NULL) {
442 bio = (twe_bio *)tr->tr_private;
443 bio->bio_buf->b_error = error;
444 bio->bio_buf->b_flags |= B_ERROR;
445 tr->tr_private = NULL;
447 twe_release_request(tr);
448 } else if (tr->tr_flags & TWE_CMD_SLEEPER)
449 wakeup_one(tr); /* wakeup the sleeping owner */
454 /********************************************************************************
455 * Write blocks from memory to disk, for system crash dumps.
458 twe_dump_blocks(struct twe_softc *sc, int unit, u_int64_t lba, void *data, int nblks)
460 struct twe_request *tr;
464 if (twe_get_request(sc, &tr))
467 KKASSERT(lba < 0x100000000ULL);
470 tr->tr_status = TWE_CMD_SETUP;
471 tr->tr_length = nblks * TWE_BLOCK_SIZE;
472 tr->tr_flags = TWE_CMD_DATAOUT;
474 cmd = &tr->tr_command;
475 cmd->io.opcode = TWE_OP_WRITE;
478 cmd->io.block_count = nblks;
481 error = twe_immediate_request(tr);
483 if (twe_report_request(tr))
485 twe_release_request(tr);
489 /********************************************************************************
490 * Handle controller-specific control operations.
493 twe_ioctl(struct twe_softc *sc, int cmd, void *addr)
495 struct twe_usercommand *tu = (struct twe_usercommand *)addr;
496 struct twe_paramcommand *tp = (struct twe_paramcommand *)addr;
497 struct twe_drivecommand *td = (struct twe_drivecommand *)addr;
498 union twe_statrequest *ts = (union twe_statrequest *)addr;
501 unsigned short *aen_code = (unsigned short *) addr;
502 struct twe_request *tr;
508 /* handle a command from userspace */
511 while (twe_get_request(sc, &tr))
512 tsleep(sc, 0, "twioctl", hz);
515 * Save the command's request ID, copy the user-supplied command in,
516 * restore the request ID.
518 srid = tr->tr_command.generic.request_id;
519 bcopy(&tu->tu_command, &tr->tr_command, sizeof(TWE_Command));
520 tr->tr_command.generic.request_id = srid;
523 * if there's a data buffer, allocate and copy it in.
524 * Must be in multipled of 512 bytes.
526 tr->tr_length = (tu->tu_size + 511) & ~511;
527 if (tr->tr_length > 0) {
528 tr->tr_data = kmalloc(tr->tr_length, M_DEVBUF, M_WAITOK);
529 if ((error = copyin(tu->tu_data, tr->tr_data, tu->tu_size)) != 0)
531 tr->tr_flags |= TWE_CMD_DATAIN | TWE_CMD_DATAOUT;
534 /* run the command */
535 error = twe_wait_request(tr);
539 /* copy the command out again */
540 bcopy(&tr->tr_command, &tu->tu_command, sizeof(TWE_Command));
542 /* if there was a data buffer, copy it out */
543 if (tr->tr_length > 0)
544 error = copyout(tr->tr_data, tu->tu_data, tu->tu_size);
548 if (tr->tr_data != NULL)
549 kfree(tr->tr_data, M_DEVBUF);
551 twe_release_request(tr);
555 /* fetch statistics counter */
557 switch (ts->ts_item) {
558 #ifdef TWE_PERFORMANCE_MONITOR
564 bcopy(&sc->twe_qstat[ts->ts_item], &ts->ts_qstat, sizeof(struct twe_qstat));
573 /* poll for an AEN */
575 *aen_code = twe_dequeue_aen(sc);
578 /* wait for another AEN to show up */
581 while ((*aen_code = twe_dequeue_aen(sc)) == TWE_AEN_QUEUE_EMPTY) {
582 error = tsleep(&sc->twe_aen_queue, PCATCH, "tweaen", 0);
589 case TWEIO_GET_PARAM:
590 if ((param = twe_get_param(sc, tp->tp_table_id, tp->tp_param_id, tp->tp_size, NULL)) == NULL) {
591 twe_printf(sc, "TWEIO_GET_PARAM failed for 0x%x/0x%x/%d\n",
592 tp->tp_table_id, tp->tp_param_id, tp->tp_size);
595 if (param->parameter_size_bytes > tp->tp_size) {
596 twe_printf(sc, "TWEIO_GET_PARAM parameter too large (%d > %d)\n",
597 param->parameter_size_bytes, tp->tp_size);
600 error = copyout(param->data, tp->tp_data, param->parameter_size_bytes);
602 kfree(param, M_DEVBUF);
606 case TWEIO_SET_PARAM:
607 data = kmalloc(tp->tp_size, M_DEVBUF, M_WAITOK);
608 error = copyin(tp->tp_data, data, tp->tp_size);
610 error = twe_set_param(sc, tp->tp_table_id, tp->tp_param_id, tp->tp_size, data);
611 kfree(data, M_DEVBUF);
619 error = twe_add_unit(sc, td->td_unit);
623 error = twe_del_unit(sc, td->td_unit);
626 /* XXX implement ATA PASSTHROUGH */
628 /* nothing we understand */
636 /********************************************************************************
637 * Enable the useful interrupts from the controller.
640 twe_enable_interrupts(struct twe_softc *sc)
642 sc->twe_state |= TWE_STATE_INTEN;
644 TWE_CONTROL_CLEAR_ATTENTION_INTERRUPT |
645 TWE_CONTROL_UNMASK_RESPONSE_INTERRUPT |
646 TWE_CONTROL_ENABLE_INTERRUPTS);
649 /********************************************************************************
650 * Disable interrupts from the controller.
653 twe_disable_interrupts(struct twe_softc *sc)
655 TWE_CONTROL(sc, TWE_CONTROL_DISABLE_INTERRUPTS);
656 sc->twe_state &= ~TWE_STATE_INTEN;
659 /********************************************************************************
660 ********************************************************************************
662 ********************************************************************************
663 ********************************************************************************/
665 /********************************************************************************
666 * Read integer parameter table entries.
669 twe_get_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t *result)
673 if ((param = twe_get_param(sc, table_id, param_id, 1, NULL)) == NULL)
675 *result = *(u_int8_t *)param->data;
676 kfree(param, M_DEVBUF);
681 twe_get_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t *result)
685 if ((param = twe_get_param(sc, table_id, param_id, 2, NULL)) == NULL)
687 *result = *(u_int16_t *)param->data;
688 kfree(param, M_DEVBUF);
693 twe_get_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t *result)
697 if ((param = twe_get_param(sc, table_id, param_id, 4, NULL)) == NULL)
699 *result = *(u_int32_t *)param->data;
700 kfree(param, M_DEVBUF);
704 /********************************************************************************
705 * Perform a TWE_OP_GET_PARAM command. If a callback function is provided, it
706 * will be called with the command when it's completed. If no callback is
707 * provided, we will wait for the command to complete and then return just the data.
708 * The caller is responsible for freeing the data when done with it.
711 twe_get_param(struct twe_softc *sc, int table_id, int param_id, size_t param_size,
712 void (* func)(struct twe_request *tr))
714 struct twe_request *tr;
725 if (twe_get_request(sc, &tr))
729 param = (TWE_Param *)kmalloc(TWE_SECTOR_SIZE, M_DEVBUF, M_INTWAIT);
731 tr->tr_length = TWE_SECTOR_SIZE;
732 tr->tr_flags = TWE_CMD_DATAIN | TWE_CMD_DATAOUT;
734 /* build the command for the controller */
735 cmd = &tr->tr_command;
736 cmd->param.opcode = TWE_OP_GET_PARAM;
739 cmd->param.param_count = 1;
741 /* fill in the outbound parameter data */
742 param->table_id = table_id;
743 param->parameter_id = param_id;
744 param->parameter_size_bytes = param_size;
746 /* submit the command and either wait or let the callback handle it */
748 /* XXX could use twe_wait_request here if interrupts were enabled? */
749 error = twe_immediate_request(tr);
751 if (twe_report_request(tr))
756 twe_release_request(tr);
759 tr->tr_complete = func;
760 error = twe_map_request(tr);
761 if ((error == 0) || (error == EBUSY))
765 /* something failed */
769 twe_release_request(tr);
771 kfree(param, M_DEVBUF);
775 /********************************************************************************
776 * Set integer parameter table entries.
778 #ifdef TWE_SHUTDOWN_NOTIFICATION
780 twe_set_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t value)
782 return(twe_set_param(sc, table_id, param_id, sizeof(value), &value));
788 twe_set_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t value)
790 return(twe_set_param(sc, table_id, param_id, sizeof(value), &value));
794 twe_set_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t value)
796 return(twe_set_param(sc, table_id, param_id, sizeof(value), &value));
800 /********************************************************************************
801 * Perform a TWE_OP_SET_PARAM command, returns nonzero on error.
804 twe_set_param(struct twe_softc *sc, int table_id, int param_id, int param_size, void *data)
806 struct twe_request *tr;
818 if (twe_get_request(sc, &tr))
822 param = (TWE_Param *)kmalloc(TWE_SECTOR_SIZE, M_DEVBUF, M_INTWAIT);
824 tr->tr_length = TWE_SECTOR_SIZE;
825 tr->tr_flags = TWE_CMD_DATAIN | TWE_CMD_DATAOUT;
827 /* build the command for the controller */
828 cmd = &tr->tr_command;
829 cmd->param.opcode = TWE_OP_SET_PARAM;
832 cmd->param.param_count = 1;
834 /* fill in the outbound parameter data */
835 param->table_id = table_id;
836 param->parameter_id = param_id;
837 param->parameter_size_bytes = param_size;
838 bcopy(data, param->data, param_size);
840 /* XXX could use twe_wait_request here if interrupts were enabled? */
841 error = twe_immediate_request(tr);
843 if (twe_report_request(tr))
849 twe_release_request(tr);
851 kfree(param, M_DEVBUF);
855 /********************************************************************************
856 * Perform a TWE_OP_INIT_CONNECTION command, returns nonzero on error.
858 * Typically called with interrupts disabled.
861 twe_init_connection(struct twe_softc *sc, int mode)
863 struct twe_request *tr;
870 if (twe_get_request(sc, &tr))
873 /* build the command */
874 cmd = &tr->tr_command;
875 cmd->initconnection.opcode = TWE_OP_INIT_CONNECTION;
876 cmd->initconnection.size = 3;
877 cmd->initconnection.host_id = 0;
878 cmd->initconnection.message_credits = mode;
879 cmd->initconnection.response_queue_pointer = 0;
881 /* submit the command */
882 error = twe_immediate_request(tr);
883 twe_release_request(tr);
885 if (mode == TWE_INIT_MESSAGE_CREDITS)
886 sc->twe_host_id = cmd->initconnection.host_id;
890 /********************************************************************************
891 * Start the command (tr) and sleep waiting for it to complete.
893 * Successfully completed commands are dequeued.
896 twe_wait_request(struct twe_request *tr)
900 tr->tr_flags |= TWE_CMD_SLEEPER;
901 tr->tr_status = TWE_CMD_BUSY;
902 twe_enqueue_ready(tr);
903 twe_startio(tr->tr_sc);
905 while (tr->tr_status == TWE_CMD_BUSY)
906 tsleep(tr, 0, "twewait", 0);
909 return(tr->tr_status != TWE_CMD_COMPLETE);
912 /********************************************************************************
913 * Start the command (tr) and busy-wait for it to complete.
914 * This should only be used when interrupts are actually disabled (although it
915 * will work if they are not).
918 twe_immediate_request(struct twe_request *tr)
924 tr->tr_status = TWE_CMD_BUSY;
925 if ((error = twe_map_request(tr)) != 0)
928 while (tr->tr_status == TWE_CMD_BUSY){
931 return(tr->tr_status != TWE_CMD_COMPLETE);
934 /********************************************************************************
935 * Handle completion of an I/O command.
938 twe_completeio(struct twe_request *tr)
940 struct twe_softc *sc = tr->tr_sc;
941 struct bio *bio = (twe_bio *)tr->tr_private;
942 struct buf *bp = bio->bio_buf;
946 if (tr->tr_status == TWE_CMD_COMPLETE) {
947 if (tr->tr_command.generic.status)
948 if (twe_report_request(tr)) {
950 bp->b_flags |= B_ERROR;
953 twe_panic(sc, "twe_completeio on incomplete command");
955 tr->tr_private = NULL;
957 twe_release_request(tr);
960 /********************************************************************************
961 * Reset the controller and pull all the active commands back onto the ready
962 * queue. Used to restart a controller that's exhibiting bad behaviour.
965 twe_reset(struct twe_softc *sc)
967 struct twe_request *tr;
971 * Sleep for a short period to allow AENs to be signalled.
973 tsleep(sc, 0, "twereset", hz);
976 * Disable interrupts from the controller, and mask any accidental entry
977 * into our interrupt handler.
979 twe_printf(sc, "controller reset in progress...\n");
980 twe_disable_interrupts(sc);
984 * Try to soft-reset the controller.
986 for (i = 0; i < TWE_MAX_RESET_TRIES; i++) {
989 twe_printf(sc, "reset %d failed, trying again\n", i);
991 if (!twe_soft_reset(sc))
992 break; /* reset process complete */
994 /* did we give up? */
995 if (i >= TWE_MAX_RESET_TRIES) {
996 twe_printf(sc, "can't reset controller, giving up\n");
1001 * Move all of the commands that were busy back to the ready queue.
1004 while ((tr = twe_dequeue_busy(sc)) != NULL) {
1005 twe_enqueue_ready(tr);
1010 * Kick the controller to start things going again, then re-enable interrupts.
1013 twe_enable_interrupts(sc);
1014 twe_printf(sc, "controller reset done, %d commands restarted\n", i);
1018 twe_enable_interrupts(sc);
1021 /********************************************************************************
1022 ********************************************************************************
1023 Command I/O to Controller
1024 ********************************************************************************
1025 ********************************************************************************/
1027 /********************************************************************************
1028 * Try to deliver (tr) to the controller.
1030 * Can be called at any interrupt level, with or without interrupts enabled.
1033 twe_start(struct twe_request *tr)
1035 struct twe_softc *sc = tr->tr_sc;
1037 u_int32_t status_reg;
1041 /* mark the command as currently being processed */
1042 tr->tr_status = TWE_CMD_BUSY;
1045 * Spin briefly waiting for the controller to come ready
1047 * XXX it might be more efficient to return EBUSY immediately
1048 * and let the command be rescheduled.
1050 for (i = 100000, done = 0; (i > 0) && !done; i--) {
1053 /* check to see if we can post a command */
1054 status_reg = TWE_STATUS(sc);
1055 twe_check_bits(sc, status_reg);
1057 if (!(status_reg & TWE_STATUS_COMMAND_QUEUE_FULL)) {
1058 /* move command to work queue */
1059 twe_enqueue_busy(tr);
1060 TWE_COMMAND_QUEUE(sc, tr->tr_cmdphys);
1063 if (tr->tr_complete != NULL) {
1064 debug(3, "queued request %d with callback %p", tr->tr_command.generic.request_id, tr->tr_complete);
1065 } else if (tr->tr_flags & TWE_CMD_SLEEPER) {
1066 debug(3, "queued request %d with wait channel %p", tr->tr_command.generic.request_id, tr);
1068 debug(3, "queued request %d for polling caller", tr->tr_command.generic.request_id);
1072 crit_exit(); /* drop critical section to allow completion interrupts */
1075 /* command is enqueued */
1080 * We couldn't get the controller to take the command; try submitting it again later.
1081 * This should only happen if something is wrong with the controller, or if we have
1082 * overestimated the number of commands it can accept. (Should we actually reject
1083 * the command at this point?)
1088 /********************************************************************************
1089 * Poll the controller (sc) for completed commands.
1091 * Can be called at any interrupt level, with or without interrupts enabled.
1094 twe_done(struct twe_softc *sc)
1096 TWE_Response_Queue rq;
1097 struct twe_request *tr;
1099 u_int32_t status_reg;
1103 /* loop collecting completed commands */
1107 status_reg = TWE_STATUS(sc);
1108 twe_check_bits(sc, status_reg); /* XXX should this fail? */
1110 if (!(status_reg & TWE_STATUS_RESPONSE_QUEUE_EMPTY)) {
1112 rq = TWE_RESPONSE_QUEUE(sc);
1113 tr = sc->twe_lookup[rq.u.response_id]; /* find command */
1114 if (tr->tr_status != TWE_CMD_BUSY)
1115 twe_printf(sc, "completion event for nonbusy command\n");
1116 tr->tr_status = TWE_CMD_COMPLETE;
1117 debug(3, "completed request id %d with status %d",
1118 tr->tr_command.generic.request_id, tr->tr_command.generic.status);
1119 /* move to completed queue */
1120 twe_remove_busy(tr);
1121 twe_enqueue_complete(tr);
1122 sc->twe_state &= ~TWE_STATE_CTLR_BUSY;
1124 break; /* no response ready */
1129 /* if we've completed any commands, try posting some more */
1133 /* handle completion and timeouts */
1134 twe_complete(sc); /* XXX use deferred completion? */
1137 /********************************************************************************
1138 * Perform post-completion processing for commands on (sc).
1140 * This is split from twe_done as it can be safely deferred and run at a lower
1141 * priority level should facilities for such a thing become available.
1144 twe_complete(struct twe_softc *sc)
1146 struct twe_request *tr;
1151 * Pull commands off the completed list, dispatch them appropriately
1153 while ((tr = twe_dequeue_complete(sc)) != NULL) {
1155 /* unmap the command's data buffer */
1156 twe_unmap_request(tr);
1158 /* dispatch to suit command originator */
1159 if (tr->tr_complete != NULL) { /* completion callback */
1160 debug(2, "call completion handler %p", tr->tr_complete);
1161 tr->tr_complete(tr);
1163 } else if (tr->tr_flags & TWE_CMD_SLEEPER) { /* caller is asleep waiting */
1164 debug(2, "wake up command owner on %p", tr);
1167 } else { /* caller is polling command */
1168 debug(2, "command left for owner");
1173 /********************************************************************************
1174 * Wait for (status) to be set in the controller status register for up to
1175 * (timeout) seconds. Returns 0 if found, nonzero if we time out.
1177 * Note: this busy-waits, rather than sleeping, since we may be called with
1178 * eg. clock interrupts masked.
1181 twe_wait_status(struct twe_softc *sc, u_int32_t status, int timeout)
1184 u_int32_t status_reg;
1188 expiry = time_second + timeout;
1191 status_reg = TWE_STATUS(sc);
1192 if (status_reg & status) /* got the required bit(s)? */
1195 } while (time_second <= expiry);
1200 /********************************************************************************
1201 * Drain the response queue, which may contain responses to commands we know
1205 twe_drain_response_queue(struct twe_softc *sc)
1207 TWE_Response_Queue rq;
1208 u_int32_t status_reg;
1212 for (;;) { /* XXX give up eventually? */
1213 status_reg = TWE_STATUS(sc);
1214 if (twe_check_bits(sc, status_reg))
1216 if (status_reg & TWE_STATUS_RESPONSE_QUEUE_EMPTY)
1218 rq = TWE_RESPONSE_QUEUE(sc);
1222 /********************************************************************************
1223 * Soft-reset the controller
1226 twe_soft_reset(struct twe_softc *sc)
1228 u_int32_t status_reg;
1234 if (twe_wait_status(sc, TWE_STATUS_ATTENTION_INTERRUPT, 30)) {
1235 twe_printf(sc, "no attention interrupt\n");
1238 TWE_CONTROL(sc, TWE_CONTROL_CLEAR_ATTENTION_INTERRUPT);
1239 if (twe_drain_aen_queue(sc)) {
1240 twe_printf(sc, "can't drain AEN queue\n");
1243 if (twe_find_aen(sc, TWE_AEN_SOFT_RESET)) {
1244 twe_printf(sc, "reset not reported\n");
1247 status_reg = TWE_STATUS(sc);
1248 if (TWE_STATUS_ERRORS(status_reg) || twe_check_bits(sc, status_reg)) {
1249 twe_printf(sc, "controller errors detected\n");
1252 if (twe_drain_response_queue(sc)) {
1253 twe_printf(sc, "can't drain response queue\n");
1259 /********************************************************************************
1260 ********************************************************************************
1262 ********************************************************************************
1263 ********************************************************************************/
1265 /********************************************************************************
1268 * XXX what does this mean?
1271 twe_host_intr(struct twe_softc *sc)
1275 twe_printf(sc, "host interrupt\n");
1276 TWE_CONTROL(sc, TWE_CONTROL_CLEAR_HOST_INTERRUPT);
1279 /********************************************************************************
1280 * Attention interrupt.
1282 * Signalled when the controller has one or more AENs for us.
1285 twe_attention_intr(struct twe_softc *sc)
1289 /* instigate a poll for AENs */
1290 if (twe_fetch_aen(sc)) {
1291 twe_printf(sc, "error polling for signalled AEN\n");
1293 TWE_CONTROL(sc, TWE_CONTROL_CLEAR_ATTENTION_INTERRUPT);
1297 /********************************************************************************
1298 * Command interrupt.
1300 * Signalled when the controller can handle more commands.
1303 twe_command_intr(struct twe_softc *sc)
1308 * We don't use this, rather we try to submit commands when we receive
1309 * them, and when other commands have completed. Mask it so we don't get
1311 twe_printf(sc, "command interrupt\n");
1313 TWE_CONTROL(sc, TWE_CONTROL_MASK_COMMAND_INTERRUPT);
1316 /********************************************************************************
1317 ********************************************************************************
1318 Asynchronous Event Handling
1319 ********************************************************************************
1320 ********************************************************************************/
1322 /********************************************************************************
1323 * Request an AEN from the controller.
1326 twe_fetch_aen(struct twe_softc *sc)
1331 if ((twe_get_param(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode, 2, twe_handle_aen)) == NULL)
1336 /********************************************************************************
1337 * Handle an AEN returned by the controller.
1340 twe_handle_aen(struct twe_request *tr)
1342 struct twe_softc *sc = tr->tr_sc;
1348 /* XXX check for command success somehow? */
1350 param = (TWE_Param *)tr->tr_data;
1351 aen = *(u_int16_t *)(param->data);
1353 kfree(tr->tr_data, M_DEVBUF);
1354 twe_release_request(tr);
1355 twe_enqueue_aen(sc, aen);
1357 /* XXX poll for more AENs? */
1360 /********************************************************************************
1361 * Pull AENs out of the controller and park them in the queue, in a context where
1362 * interrupts aren't active. Return nonzero if we encounter any errors in the
1363 * process of obtaining all the available AENs.
1366 twe_drain_aen_queue(struct twe_softc *sc)
1371 if (twe_get_param_2(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode, &aen))
1373 if (aen == TWE_AEN_QUEUE_EMPTY)
1375 twe_enqueue_aen(sc, aen);
1379 /********************************************************************************
1380 * Push an AEN that we've received onto the queue.
1382 * Note that we have to lock this against reentrance, since it may be called
1383 * from both interrupt and non-interrupt context.
1385 * If someone is waiting for the AEN we have, wake them up.
1388 twe_enqueue_aen(struct twe_softc *sc, u_int16_t aen)
1395 if ((msg = twe_format_aen(sc, aen)) != NULL)
1396 twe_printf(sc, "AEN: <%s>\n", msg);
1399 /* enqueue the AEN */
1400 next = ((sc->twe_aen_head + 1) % TWE_Q_LENGTH);
1401 nextnext = ((sc->twe_aen_head + 2) % TWE_Q_LENGTH);
1403 /* check to see if this is the last free slot, and subvert the AEN if it is */
1404 if (nextnext == sc->twe_aen_tail)
1405 aen = TWE_AEN_QUEUE_FULL;
1407 /* look to see if there's room for this AEN */
1408 if (next != sc->twe_aen_tail) {
1409 sc->twe_aen_queue[sc->twe_aen_head] = aen;
1410 sc->twe_aen_head = next;
1413 /* wake up anyone asleep on the queue */
1414 wakeup(&sc->twe_aen_queue);
1416 /* anyone looking for this AEN? */
1417 if (sc->twe_wait_aen == aen) {
1418 sc->twe_wait_aen = -1;
1419 wakeup(&sc->twe_wait_aen);
1424 /********************************************************************************
1425 * Pop an AEN off the queue, or return -1 if there are none left.
1427 * We are more or less interrupt-safe, so don't block interrupts.
1430 twe_dequeue_aen(struct twe_softc *sc)
1436 if (sc->twe_aen_tail == sc->twe_aen_head) {
1437 result = TWE_AEN_QUEUE_EMPTY;
1439 result = sc->twe_aen_queue[sc->twe_aen_tail];
1440 sc->twe_aen_tail = ((sc->twe_aen_tail + 1) % TWE_Q_LENGTH);
1445 /********************************************************************************
1446 * Check to see if the requested AEN is in the queue.
1448 * XXX we could probably avoid masking interrupts here
1451 twe_find_aen(struct twe_softc *sc, u_int16_t aen)
1457 for (i = sc->twe_aen_tail; (i != sc->twe_aen_head) && missing; i = (i + 1) % TWE_Q_LENGTH) {
1458 if (sc->twe_aen_queue[i] == aen)
1466 #if 0 /* currently unused */
1467 /********************************************************************************
1468 * Sleep waiting for at least (timeout) seconds until we see (aen) as
1469 * requested. Returns nonzero on timeout or failure.
1471 * XXX: this should not be used in cases where there may be more than one sleeper
1472 * without a mechanism for registering multiple sleepers.
1475 twe_wait_aen(struct twe_softc *sc, int aen, int timeout)
1482 expiry = time_second + timeout;
1486 sc->twe_wait_aen = aen;
1489 tsleep(&sc->twe_wait_aen, PZERO, "twewaen", hz);
1490 if (sc->twe_wait_aen == -1)
1492 } while ((time_second <= expiry) && !found);
1498 /********************************************************************************
1499 ********************************************************************************
1500 Command Buffer Management
1501 ********************************************************************************
1502 ********************************************************************************/
1504 /********************************************************************************
1505 * Get a new command buffer.
1507 * This will return NULL if all command buffers are in use.
1510 twe_get_request(struct twe_softc *sc, struct twe_request **tr)
1514 /* try to reuse an old buffer */
1515 *tr = twe_dequeue_free(sc);
1517 /* initialise some fields to their defaults */
1519 (*tr)->tr_data = NULL;
1520 (*tr)->tr_private = NULL;
1521 (*tr)->tr_status = TWE_CMD_SETUP; /* command is in setup phase */
1522 (*tr)->tr_flags = 0;
1523 (*tr)->tr_complete = NULL;
1524 (*tr)->tr_command.generic.status = 0; /* before submission to controller */
1525 (*tr)->tr_command.generic.flags = 0; /* not used */
1527 return(*tr == NULL);
1530 /********************************************************************************
1531 * Release a command buffer for reuse.
1535 twe_release_request(struct twe_request *tr)
1539 if (tr->tr_private != NULL)
1540 twe_panic(tr->tr_sc, "tr_private != NULL");
1541 twe_enqueue_free(tr);
1544 /********************************************************************************
1545 ********************************************************************************
1547 ********************************************************************************
1548 ********************************************************************************/
1550 /********************************************************************************
1551 * Print some information about the controller
1554 twe_describe_controller(struct twe_softc *sc)
1563 /* get the port count */
1564 twe_get_param_1(sc, TWE_PARAM_CONTROLLER, TWE_PARAM_CONTROLLER_PortCount, &ports);
1566 /* get version strings */
1567 p[0] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_FW, 16, NULL);
1568 p[1] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_BIOS, 16, NULL);
1571 twe_printf(sc, "%d ports, Firmware %.16s, BIOS %.16s\n", ports, p[0]->data, p[1]->data);
1573 p[2] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_Mon, 16, NULL);
1574 p[3] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_PCB, 8, NULL);
1575 p[4] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_ATA, 8, NULL);
1576 p[5] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_PCI, 8, NULL);
1578 if (p[2] && p[3] && p[4] && p[5])
1579 twe_printf(sc, "Monitor %.16s, PCB %.8s, Achip %.8s, Pchip %.8s\n",
1580 p[2]->data, p[3]->data, p[4]->data, p[5]->data);
1583 kfree(p[2], M_DEVBUF);
1585 kfree(p[3], M_DEVBUF);
1587 kfree(p[4], M_DEVBUF);
1589 kfree(p[5], M_DEVBUF);
1592 kfree(p[0], M_DEVBUF);
1594 kfree(p[1], M_DEVBUF);
1596 /* print attached drives */
1598 p[0] = twe_get_param(sc, TWE_PARAM_DRIVESUMMARY, TWE_PARAM_DRIVESUMMARY_Status, 16, NULL);
1599 for (i = 0; i < ports; i++) {
1600 if (p[0]->data[i] != TWE_PARAM_DRIVESTATUS_Present)
1602 twe_get_param_4(sc, TWE_PARAM_DRIVEINFO + i, TWE_PARAM_DRIVEINFO_Size, &size);
1603 p[1] = twe_get_param(sc, TWE_PARAM_DRIVEINFO + i, TWE_PARAM_DRIVEINFO_Model, 40, NULL);
1605 twe_printf(sc, "port %d: %.40s %dMB\n", i, p[1]->data, size / 2048);
1606 kfree(p[1], M_DEVBUF);
1608 twe_printf(sc, "port %d, drive status unavailable\n", i);
1612 kfree(p[0], M_DEVBUF);
1617 /********************************************************************************
1618 * Look up a text description of a numeric code and return a pointer to same.
1621 twe_describe_code(struct twe_code_lookup *table, u_int32_t code)
1625 for (i = 0; table[i].string != NULL; i++)
1626 if (table[i].code == code)
1627 return(table[i].string);
1628 return(table[i+1].string);
1632 /********************************************************************************
1633 * Complain if the status bits aren't what we're expecting.
1635 * Rate-limit the complaints to at most one of each every five seconds, but
1636 * always return the correct status.
1639 twe_check_bits(struct twe_softc *sc, u_int32_t status_reg)
1642 static time_t lastwarn[2] = {0, 0};
1645 * This can be a little problematic, as twe_panic may call twe_reset if
1646 * TWE_DEBUG is not set, which will call us again as part of the soft reset.
1648 if ((status_reg & TWE_STATUS_PANIC_BITS) != 0) {
1649 twe_printf(sc, "FATAL STATUS BIT(S) %b\n", status_reg & TWE_STATUS_PANIC_BITS,
1650 TWE_STATUS_BITS_DESCRIPTION);
1651 twe_panic(sc, "fatal status bits");
1655 if ((status_reg & TWE_STATUS_EXPECTED_BITS) != TWE_STATUS_EXPECTED_BITS) {
1656 if (time_second > (lastwarn[0] + 5)) {
1657 twe_printf(sc, "missing expected status bit(s) %b\n", ~status_reg & TWE_STATUS_EXPECTED_BITS,
1658 TWE_STATUS_BITS_DESCRIPTION);
1659 lastwarn[0] = time_second;
1664 if ((status_reg & TWE_STATUS_UNEXPECTED_BITS) != 0) {
1665 if (time_second > (lastwarn[1] + 5)) {
1666 twe_printf(sc, "unexpected status bit(s) %b\n", status_reg & TWE_STATUS_UNEXPECTED_BITS,
1667 TWE_STATUS_BITS_DESCRIPTION);
1668 lastwarn[1] = time_second;
1671 if (status_reg & TWE_STATUS_PCI_PARITY_ERROR) {
1672 twe_printf(sc, "PCI parity error: Reseat card, move card or buggy device present.");
1673 twe_clear_pci_parity_error(sc);
1675 if (status_reg & TWE_STATUS_PCI_ABORT) {
1676 twe_printf(sc, "PCI abort, clearing.");
1677 twe_clear_pci_abort(sc);
1684 /********************************************************************************
1685 * Return a string describing (aen).
1687 * The low 8 bits of the aen are the code, the high 8 bits give the unit number
1688 * where an AEN is specific to a unit.
1690 * Note that we could expand this routine to handle eg. up/downgrading the status
1691 * of a drive if we had some idea of what the drive's initial status was.
1695 twe_format_aen(struct twe_softc *sc, u_int16_t aen)
1697 static char buf[80];
1701 code = twe_describe_code(twe_table_aen, TWE_AEN_CODE(aen));
1713 if ((child = sc->twe_drive[TWE_AEN_UNIT(aen)].td_disk) != NULL) {
1714 ksprintf(buf, "twed%d: %s", device_get_unit(child), msg);
1716 ksprintf(buf, "twe%d: %s for unknown unit %d", device_get_unit(sc->twe_dev),
1717 msg, TWE_AEN_UNIT(aen));
1722 ksprintf(buf, "twe%d: port %d: %s", device_get_unit(sc->twe_dev), TWE_AEN_UNIT(aen),
1731 ksprintf(buf, "unknown AEN 0x%x", aen);
1735 /********************************************************************************
1736 * Print a diagnostic if the status of the command warrants it, and return
1737 * either zero (command was ok) or nonzero (command failed).
1740 twe_report_request(struct twe_request *tr)
1742 struct twe_softc *sc = tr->tr_sc;
1743 TWE_Command *cmd = &tr->tr_command;
1747 * Check the command status value and handle accordingly.
1749 if (cmd->generic.status == TWE_STATUS_RESET) {
1751 * The status code 0xff requests a controller reset.
1753 twe_printf(sc, "command returned with controller reset request\n");
1756 } else if (cmd->generic.status > TWE_STATUS_FATAL) {
1758 * Fatal errors that don't require controller reset.
1760 * We know a few special flags values.
1762 switch (cmd->generic.flags) {
1764 device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
1768 device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
1769 "unrecoverable drive error");
1772 device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
1773 "controller error - %s (flags = 0x%x)\n",
1774 twe_describe_code(twe_table_status, cmd->generic.status),
1775 cmd->generic.flags);
1778 } else if (cmd->generic.status > TWE_STATUS_WARNING) {
1780 * Warning level status.
1782 device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
1783 "warning - %s (flags = 0x%x)\n",
1784 twe_describe_code(twe_table_status, cmd->generic.status),
1785 cmd->generic.flags);
1786 } else if (cmd->generic.status > 0x40) {
1788 * Info level status.
1790 device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
1791 "attention - %s (flags = 0x%x)\n",
1792 twe_describe_code(twe_table_status, cmd->generic.status),
1793 cmd->generic.flags);
1799 /********************************************************************************
1800 * Print some controller state to aid in debugging error/panic conditions
1803 twe_print_controller(struct twe_softc *sc)
1805 u_int32_t status_reg;
1807 status_reg = TWE_STATUS(sc);
1808 twe_printf(sc, "status %b\n", status_reg, TWE_STATUS_BITS_DESCRIPTION);
1809 twe_printf(sc, " current max min\n");
1810 twe_printf(sc, "free %04d %04d %04d\n", sc->twe_qstat[TWEQ_FREE].q_length, sc->twe_qstat[TWEQ_FREE].q_max, sc->twe_qstat[TWEQ_FREE].q_min);
1811 twe_printf(sc, "ready %04d %04d %04d\n", sc->twe_qstat[TWEQ_READY].q_length, sc->twe_qstat[TWEQ_READY].q_max, sc->twe_qstat[TWEQ_READY].q_min);
1812 twe_printf(sc, "busy %04d %04d %04d\n", sc->twe_qstat[TWEQ_BUSY].q_length, sc->twe_qstat[TWEQ_BUSY].q_max, sc->twe_qstat[TWEQ_BUSY].q_min);
1813 twe_printf(sc, "complete %04d %04d %04d\n", sc->twe_qstat[TWEQ_COMPLETE].q_length, sc->twe_qstat[TWEQ_COMPLETE].q_max, sc->twe_qstat[TWEQ_COMPLETE].q_min);
1814 twe_printf(sc, "bioq %04d %04d %04d\n", sc->twe_qstat[TWEQ_BIO].q_length, sc->twe_qstat[TWEQ_BIO].q_max, sc->twe_qstat[TWEQ_BIO].q_min);
1815 twe_printf(sc, "AEN queue head %d tail %d\n", sc->twe_aen_head, sc->twe_aen_tail);
1819 twe_panic(struct twe_softc *sc, char *reason)
1821 twe_print_controller(sc);
1830 /********************************************************************************
1831 * Print a request/command in human-readable format.
1834 twe_print_request(struct twe_request *tr)
1836 struct twe_softc *sc = tr->tr_sc;
1837 TWE_Command *cmd = &tr->tr_command;
1840 twe_printf(sc, "CMD: request_id %d opcode <%s> size %d unit %d host_id %d\n",
1841 cmd->generic.request_id, twe_describe_code(twe_table_opcode, cmd->generic.opcode), cmd->generic.size,
1842 cmd->generic.unit, cmd->generic.host_id);
1843 twe_printf(sc, " status %d flags 0x%x count %d sgl_offset %d\n",
1844 cmd->generic.status, cmd->generic.flags, cmd->generic.count, cmd->generic.sgl_offset);
1846 switch(cmd->generic.opcode) { /* XXX add more opcodes? */
1849 twe_printf(sc, " lba %d\n", cmd->io.lba);
1850 for (i = 0; (i < TWE_MAX_SGL_LENGTH) && (cmd->io.sgl[i].length != 0); i++)
1851 twe_printf(sc, " %d: 0x%x/%d\n",
1852 i, cmd->io.sgl[i].address, cmd->io.sgl[i].length);
1855 case TWE_OP_GET_PARAM:
1856 case TWE_OP_SET_PARAM:
1857 for (i = 0; (i < TWE_MAX_SGL_LENGTH) && (cmd->param.sgl[i].length != 0); i++)
1858 twe_printf(sc, " %d: 0x%x/%d\n",
1859 i, cmd->param.sgl[i].address, cmd->param.sgl[i].length);
1862 case TWE_OP_INIT_CONNECTION:
1863 twe_printf(sc, " response queue pointer 0x%x\n",
1864 cmd->initconnection.response_queue_pointer);
1870 twe_printf(sc, " tr_command %p/0x%x tr_data %p/0x%x,%d\n",
1871 tr, tr->tr_cmdphys, tr->tr_data, tr->tr_dataphys, tr->tr_length);
1872 twe_printf(sc, " tr_status %d tr_flags 0x%x tr_complete %p tr_private %p\n",
1873 tr->tr_status, tr->tr_flags, tr->tr_complete, tr->tr_private);