2 * Copyright (c) 2003-04 3ware, Inc.
3 * Copyright (c) 2000 Michael Smith
4 * Copyright (c) 2000 BSDi
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $DragonFly: src/sys/dev/raid/twa/twa.c,v 1.2 2004/06/21 15:39:31 dillon Exp $
33 * 3ware driver for 9000 series storage controllers.
35 * Author: Vinod Kashyap
39 #include "twa_includes.h"
41 #ifdef TWA_FLASH_FIRMWARE
42 static int twa_flash_firmware(struct twa_softc *sc);
43 static int twa_hard_reset(struct twa_softc *sc);
44 #endif /* TWA_FLASH_FIRMWARE */
46 static int twa_init_ctlr(struct twa_softc *sc);
47 static void *twa_get_param(struct twa_softc *sc, int table_id,
48 int parameter_id, size_t size,
49 void (* callback)(struct twa_request *tr));
50 static int twa_set_param(struct twa_softc *sc, int table_id, int param_id,
51 int param_size, void *data,
52 void (* callback)(struct twa_request *tr));
53 static int twa_init_connection(struct twa_softc *sc, u_int16_t message_credits,
54 u_int32_t set_features, u_int16_t current_fw_srl,
55 u_int16_t current_fw_arch_id, u_int16_t current_fw_branch,
56 u_int16_t current_fw_build, u_int16_t *fw_on_ctlr_srl,
57 u_int16_t *fw_on_ctlr_arch_id, u_int16_t *fw_on_ctlr_branch,
58 u_int16_t *fw_on_ctlr_build, u_int32_t *init_connect_result);
60 static int twa_wait_request(struct twa_request *req, u_int32_t timeout);
61 static int twa_immediate_request(struct twa_request *req, u_int32_t timeout);
63 static int twa_done(struct twa_softc *sc);
64 static int twa_drain_pending_queue(struct twa_softc *sc);
65 static void twa_drain_complete_queue(struct twa_softc *sc);
66 static int twa_wait_status(struct twa_softc *sc, u_int32_t status, u_int32_t timeout);
67 static int twa_drain_response_queue(struct twa_softc *sc);
68 static int twa_check_ctlr_state(struct twa_softc *sc, u_int32_t status_reg);
69 static int twa_soft_reset(struct twa_softc *sc);
71 static void twa_host_intr(struct twa_softc *sc);
72 static void twa_attention_intr(struct twa_softc *sc);
73 static void twa_command_intr(struct twa_softc *sc);
75 static int twa_fetch_aen(struct twa_softc *sc);
76 static void twa_aen_callback(struct twa_request *tr);
77 static void twa_enqueue_aen(struct twa_softc *sc, struct twa_command_header *cmd_hdr);
78 static int twa_drain_aen_queue(struct twa_softc *sc);
79 static int twa_find_aen(struct twa_softc *sc, u_int16_t aen_code);
81 static void twa_panic(struct twa_softc *sc, int8_t *reason);
84 * Function name: twa_setup
85 * Description: Initializes driver data structures for the controller.
87 * Input: sc -- ptr to per ctlr structure
89 * Return value: 0 -- success
93 twa_setup(struct twa_softc *sc)
95 struct twa_event_packet *aen_queue;
99 twa_dbg_dprint_enter(3, sc);
101 /* Initialize request queues. */
104 twa_initq_pending(sc);
105 twa_initq_complete(sc);
107 if (twa_alloc_req_pkts(sc, TWA_Q_LENGTH)) {
108 twa_printf(sc, "Failed to allocate request packets.\n");
112 /* Allocate memory for the AEN queue. */
113 if ((aen_queue = malloc(sizeof(struct twa_event_packet) * TWA_Q_LENGTH,
114 M_DEVBUF, M_WAITOK)) == NULL) {
116 * This should not cause us to return error. We will only be
117 * unable to support AEN's. But then, we will have to check
118 * time and again to see if we can support AEN's, if we
119 * continue. So, we will just return error.
121 twa_printf(sc, "Could not allocate memory for AEN queue.\n");
122 return(ENOMEM); /* any unfreed memory will be freed by twa_free */
124 /* Initialize the aen queue. */
125 bzero(aen_queue, sizeof(struct twa_event_packet) * TWA_Q_LENGTH);
126 for (i = 0; i < TWA_Q_LENGTH; i++)
127 sc->twa_aen_queue[i] = &(aen_queue[i]);
130 * Disable interrupts from the card.
131 * Interrupts will be enabled back in twa_intrhook.
133 twa_disable_interrupts(sc);
135 /* Initialize the controller. */
136 if ((error = twa_init_ctlr(sc))) {
137 /* Soft reset the controller, and try one more time. */
138 twa_printf(sc, "Controller initialization failed. Retrying...\n");
139 if ((error = twa_soft_reset(sc)))
140 twa_printf(sc, "Controller soft reset failed.\n");
142 error = twa_init_ctlr(sc);
147 #ifdef TWA_FLASH_FIRMWARE
149 * Function name: twa_flash_firmware
150 * Description: Flashes bundled firmware image onto controller.
152 * Input: sc -- ptr to per ctlr structure
154 * Return value: 0 -- success
158 twa_flash_firmware(struct twa_softc *sc)
160 struct twa_request *tr;
161 struct twa_command_download_firmware *cmd;
162 u_int32_t fw_img_chunk_size;
163 u_int32_t this_chunk_size = 0;
164 u_int32_t remaining_img_size = 0;
168 if ((tr = twa_get_request(sc)) == NULL) {
169 /* No free request packets available. Can't proceed. */
173 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
174 /* Allocate sufficient memory to hold a chunk of the firmware image. */
175 fw_img_chunk_size = ((twa_fw_img_size/NUM_FW_IMAGE_CHUNKS) + 511) & ~511;
176 if ((tr->tr_data = malloc(fw_img_chunk_size, M_DEVBUF, M_WAITOK)) == NULL) {
177 twa_printf (sc, "Could not allocate memory for firmware image.\n");
181 remaining_img_size = twa_fw_img_size;
182 cmd = &(tr->tr_command->command.cmd_pkt_7k.download_fw);
184 for (i = 0; i < NUM_FW_IMAGE_CHUNKS; i++) {
185 /* Build a cmd pkt for downloading firmware. */
186 bzero(tr->tr_command, sizeof(struct twa_command_packet));
188 tr->tr_command->cmd_hdr.header_desc.size_header = 128;
190 cmd->opcode = TWA_OP_DOWNLOAD_FIRMWARE;
191 cmd->sgl_offset = 2;/* offset in dwords, to the beginning of sg list */
192 cmd->size = 2; /* this field will be updated at data map time */
193 cmd->request_id = tr->tr_request_id;
197 cmd->param = 8; /* prom image */
199 if (i != (NUM_FW_IMAGE_CHUNKS - 1))
200 this_chunk_size = fw_img_chunk_size;
201 else /* last chunk */
202 this_chunk_size = remaining_img_size;
204 remaining_img_size -= this_chunk_size;
205 bcopy(twa_fw_img + (i * fw_img_chunk_size),
206 tr->tr_data, this_chunk_size);
209 * The next line will effect only the last chunk.
211 tr->tr_length = (this_chunk_size + 511) & ~511;
213 tr->tr_flags |= TWA_CMD_DATA_OUT;
215 error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
217 twa_printf(sc, "Firmware flash request could not be posted. error = 0x%x\n",
219 if (error == ETIMEDOUT)
220 return(error); /* clean-up done by twa_immediate_request */
224 if (i != (NUM_FW_IMAGE_CHUNKS - 1)) {
225 if ((error = tr->tr_command->cmd_hdr.status_block.error) != TWA_ERROR_MORE_DATA) {
226 twa_printf(sc, "cmd = 0x%x: ERROR: (0x%02X: 0x%04X): %s: %s\n",
228 TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
230 twa_find_msg_string(twa_error_table, error),
231 tr->tr_command->cmd_hdr.err_specific_desc);
232 twa_printf(sc, "Firmware flash request failed. Intermediate error = 0x%x, i = %x\n",
234 /* Hard reset the controller, so that it doesn't wait for the remaining chunks. */
238 } else /* last chunk */
240 twa_printf(sc, "cmd = 0x%x: ERROR: (0x%02X: 0x%04X): %s: %s\n",
242 TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
243 tr->tr_command->cmd_hdr.status_block.error,
244 twa_find_msg_string(twa_error_table,
245 tr->tr_command->cmd_hdr.status_block.error),
246 tr->tr_command->cmd_hdr.err_specific_desc);
247 twa_printf(sc, "Firmware flash request failed. error = 0x%x\n", error);
248 /* Hard reset the controller, so that it doesn't wait for more chunks. */
254 free(tr->tr_data, M_DEVBUF);
257 twa_release_request(tr);
263 * Function name: twa_hard_reset
264 * Description: Hard reset the controller.
266 * Input: sc -- ptr to per ctlr structure
268 * Return value: 0 -- success
272 twa_hard_reset(struct twa_softc *sc)
274 struct twa_request *tr;
275 struct twa_command_reset_firmware *cmd;
278 if ((tr = twa_get_request(sc)) == NULL)
280 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
281 /* Build a cmd pkt for sending down the hard reset command. */
282 tr->tr_command->cmd_hdr.header_desc.size_header = 128;
284 cmd = &(tr->tr_command->command.cmd_pkt_7k.reset_fw);
285 cmd->opcode = TWA_OP_RESET_FIRMWARE;
286 cmd->size = 2; /* this field will be updated at data map time */
287 cmd->request_id = tr->tr_request_id;
291 cmd->param = 0; /* don't reload FPGA logic */
296 error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
298 twa_printf(sc, "Hard reset request could not be posted. error = 0x%x\n",
300 if (error == ETIMEDOUT)
301 return(error); /* clean-up done by twa_immediate_request */
304 if ((error = cmd->status)) {
305 twa_printf(sc, "cmd = 0x%x: ERROR: (0x%02X: 0x%04X): %s: %s\n",
307 TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
308 tr->tr_command->cmd_hdr.status_block.error,
309 twa_find_msg_string(twa_error_table,
310 tr->tr_command->cmd_hdr.status_block.error),
311 tr->tr_command->cmd_hdr.err_specific_desc);
312 twa_printf(sc, "Hard reset request failed. error = 0x%x\n", error);
317 twa_release_request(tr);
321 #endif /* TWA_FLASH_FIRMWARE */
324 * Function name: twa_init_ctlr
325 * Description: Establishes a logical connection with the controller.
326 * If bundled with firmware, determines whether or not
327 * to flash firmware, based on arch_id, fw SRL (Spec.
328 * Revision Level), branch & build #'s. Also determines
329 * whether or not the driver is compatible with the
330 * firmware on the controller, before proceeding to work
333 * Input: sc -- ptr to per ctlr structure
335 * Return value: 0 -- success
339 twa_init_ctlr(struct twa_softc *sc)
341 u_int16_t fw_on_ctlr_srl = 0;
342 u_int16_t fw_on_ctlr_arch_id = 0;
343 u_int16_t fw_on_ctlr_branch = 0;
344 u_int16_t fw_on_ctlr_build = 0;
345 u_int32_t init_connect_result = 0;
347 #ifdef TWA_FLASH_FIRMWARE
348 int8_t fw_flashed = FALSE;
349 int8_t fw_flash_failed = FALSE;
350 #endif /* TWA_FLASH_FIRMWARE */
352 twa_dbg_dprint_enter(3, sc);
354 /* Wait for the controller to become ready. */
355 if (twa_wait_status(sc, TWA_STATUS_MICROCONTROLLER_READY,
356 TWA_REQUEST_TIMEOUT_PERIOD)) {
357 twa_printf(sc, "Microcontroller not ready.\n");
360 /* Drain the response queue. */
361 if (twa_drain_response_queue(sc)) {
362 twa_printf(sc, "Can't drain response queue.\n");
365 /* Establish a logical connection with the controller. */
366 if ((error = twa_init_connection(sc, TWA_INIT_MESSAGE_CREDITS,
367 TWA_EXTENDED_INIT_CONNECT, TWA_CURRENT_FW_SRL,
368 TWA_9000_ARCH_ID, TWA_CURRENT_FW_BRANCH,
369 TWA_CURRENT_FW_BUILD, &fw_on_ctlr_srl,
370 &fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
371 &fw_on_ctlr_build, &init_connect_result))) {
372 twa_printf(sc, "Can't initialize connection in current mode.\n");
376 #ifdef TWA_FLASH_FIRMWARE
378 if ((init_connect_result & TWA_BUNDLED_FW_SAFE_TO_FLASH) &&
379 (init_connect_result & TWA_CTLR_FW_RECOMMENDS_FLASH)) {
381 * The bundled firmware is safe to flash, and the firmware
382 * on the controller recommends a flash. So, flash!
384 twa_printf(sc, "Flashing bundled firmware...\n");
385 if ((error = twa_flash_firmware(sc))) {
386 fw_flash_failed = TRUE;
387 twa_printf(sc, "Unable to flash bundled firmware.\n");
388 twa_printf(sc, "Will see if possible to work with firmware on controller...\n");
390 twa_printf(sc, "Successfully flashed bundled firmware.\n");
396 /* The firmware was flashed. Have the new image loaded */
397 error = twa_hard_reset(sc);
399 twa_printf(sc, "Could not reset controller after flash!\n");
400 else /* Go through initialization again. */
401 error = twa_init_ctlr(sc);
403 * If hard reset of controller failed, we need to return.
404 * Otherwise, the above recursive call to twa_init_ctlr will
405 * have completed the rest of the initialization (starting
406 * from twa_drain_aen_queue below). Don't do it again.
411 #endif /* TWA_FLASH_FIRMWARE */
414 * Either we are not bundled with a firmware image, or
415 * the bundled firmware is not safe to flash,
416 * or flash failed for some reason. See if we can at
417 * least work with the firmware on the controller in the
420 if (init_connect_result & TWA_CTLR_FW_COMPATIBLE) {
421 /* Yes, we can. Make note of the operating mode. */
422 sc->working_srl = TWA_CURRENT_FW_SRL;
423 sc->working_branch = TWA_CURRENT_FW_BRANCH;
424 sc->working_build = TWA_CURRENT_FW_BUILD;
427 * No, we can't. See if we can at least work with
428 * it in the base mode. We should never come here
429 * if firmware has just been flashed.
431 twa_printf(sc, "Driver/Firmware mismatch. Negotiating for base level...\n");
432 if ((error = twa_init_connection(sc, TWA_INIT_MESSAGE_CREDITS,
433 TWA_EXTENDED_INIT_CONNECT, TWA_BASE_FW_SRL,
434 TWA_9000_ARCH_ID, TWA_BASE_FW_BRANCH,
435 TWA_BASE_FW_BUILD, &fw_on_ctlr_srl,
436 &fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
437 &fw_on_ctlr_build, &init_connect_result))) {
438 twa_printf(sc, "Can't initialize connection in base mode.\n");
441 if (!(init_connect_result & TWA_CTLR_FW_COMPATIBLE)) {
443 * The firmware on the controller is not even
444 * compatible with our base mode. We cannot
445 * work with it. Bail...
447 twa_printf(sc, "Incompatible firmware on controller\n");
448 #ifdef TWA_FLASH_FIRMWARE
450 twa_printf(sc, "...and could not flash bundled firmware.\n");
452 twa_printf(sc, "...and bundled firmware not safe to flash.\n");
453 #endif /* TWA_FLASH_FIRMWARE */
456 /* We can work with this firmware, but only in base mode. */
457 sc->working_srl = TWA_BASE_FW_SRL;
458 sc->working_branch = TWA_BASE_FW_BRANCH;
459 sc->working_build = TWA_BASE_FW_BUILD;
460 sc->twa_operating_mode = TWA_BASE_MODE;
464 /* Drain the AEN queue */
465 if (twa_drain_aen_queue(sc)) {
467 * We will just print that we couldn't drain the AEN queue.
468 * There's no need to bail out.
470 twa_printf(sc, "Can't drain AEN queue.\n");
473 /* Set controller state to initialized. */
474 sc->twa_state &= ~TWA_STATE_SHUTDOWN;
476 twa_dbg_dprint_exit(3, sc);
482 * Function name: twa_deinit_ctlr
483 * Description: Close logical connection with the controller.
485 * Input: sc -- ptr to per ctlr structure
487 * Return value: 0 -- success
491 twa_deinit_ctlr(struct twa_softc *sc)
494 * Mark the controller as shutting down,
495 * and disable any further interrupts.
497 sc->twa_state |= TWA_STATE_SHUTDOWN;
498 twa_disable_interrupts(sc);
500 /* Let the controller know that we are going down. */
501 return(twa_init_connection(sc, TWA_SHUTDOWN_MESSAGE_CREDITS,
503 NULL, NULL, NULL, NULL, NULL));
508 * Function name: twa_interrupt
509 * Description: Interrupt handler. Determines the kind of interrupt,
510 * and calls the appropriate handler.
512 * Input: sc -- ptr to per ctlr structure
517 twa_interrupt(struct twa_softc *sc)
519 u_int32_t status_reg;
521 twa_dbg_dprint_enter(5, sc);
523 /* Collect current interrupt status. */
524 status_reg = TWA_READ_STATUS_REGISTER(sc);
525 if (twa_check_ctlr_state(sc, status_reg))
528 /* Dispatch based on the kind of interrupt. */
529 if (status_reg & TWA_STATUS_HOST_INTERRUPT)
531 if (status_reg & TWA_STATUS_ATTENTION_INTERRUPT)
532 twa_attention_intr(sc);
533 if (status_reg & TWA_STATUS_COMMAND_INTERRUPT)
534 twa_command_intr(sc);
535 if (status_reg & TWA_STATUS_RESPONSE_INTERRUPT)
541 * Function name: twa_ioctl
542 * Description: ioctl handler.
544 * Input: sc -- ptr to per ctlr structure
546 * buf -- ptr to buffer in kernel memory, which is
547 * a copy of the input buffer in user-space
548 * Output: buf -- ptr to buffer in kernel memory, which will
549 * be copied of the output buffer in user-space
550 * Return value: 0 -- success
554 twa_ioctl(struct twa_softc *sc, int cmd, void *buf)
556 struct twa_ioctl_9k *user_buf = (struct twa_ioctl_9k *)buf;
557 struct twa_event_packet event_buf;
564 case TWA_IOCTL_FIRMWARE_PASS_THROUGH:
566 struct twa_command_packet *cmdpkt;
567 struct twa_request *tr;
568 u_int32_t data_buf_size_adjusted;
571 twa_dbg_dprint(2, sc, "Firmware PassThru");
573 /* Get a request packet */
574 while ((tr = twa_get_request(sc)) == NULL)
576 * No free request packets available. Sleep until
577 * one becomes available.
579 tsleep(&(sc->twa_wait_timeout), PPAUSE, "twioctl", hz);
582 * Make sure that the data buffer sent to firmware is a
583 * 512 byte multiple in size.
585 data_buf_size_adjusted = (user_buf->twa_drvr_pkt.buffer_length + 511) & ~511;
586 if ((tr->tr_length = data_buf_size_adjusted)) {
587 if ((tr->tr_data = malloc(data_buf_size_adjusted, M_DEVBUF, M_WAITOK)) == NULL) {
588 twa_printf(sc, "Could not alloc mem for fw_passthru data_buf.\n");
590 goto fw_passthru_done;
592 /* Copy the payload. */
593 if ((error = copyin((void *) (user_buf->pdata),
594 (void *) (tr->tr_data),
595 user_buf->twa_drvr_pkt.buffer_length)) != 0) {
596 twa_printf (sc, "Could not copyin fw_passthru data_buf.\n");
597 goto fw_passthru_done;
599 tr->tr_flags |= TWA_CMD_DATA_IN | TWA_CMD_DATA_OUT;
601 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_IOCTL;
602 cmdpkt = tr->tr_command;
604 /* Copy the command packet. */
605 bcopy(&(user_buf->twa_cmd_pkt), cmdpkt,
606 sizeof(struct twa_command_packet));
607 cmdpkt->command.cmd_pkt_7k.generic.request_id = tr->tr_request_id;
609 twa_dbg_dprint(3, sc, "cmd_pkt_7k = %x %x %x %x %x %x %x",
610 cmdpkt->command.cmd_pkt_7k.generic.opcode,
611 cmdpkt->command.cmd_pkt_7k.generic.sgl_offset,
612 cmdpkt->command.cmd_pkt_7k.generic.size,
613 cmdpkt->command.cmd_pkt_7k.generic.request_id,
614 cmdpkt->command.cmd_pkt_7k.generic.unit,
615 cmdpkt->command.cmd_pkt_7k.generic.status,
616 cmdpkt->command.cmd_pkt_7k.generic.flags);
618 /* Send down the request, and wait for it to complete. */
619 if ((error = twa_wait_request(tr, TWA_REQUEST_TIMEOUT_PERIOD))) {
620 twa_printf(sc, "fw_passthru request failed. error = 0x%x\n", error);
621 if (error == ETIMEDOUT)
622 break; /* clean-up done by twa_wait_request */
623 goto fw_passthru_done;
626 /* Copy the command packet back into user space. */
627 bcopy(cmdpkt, &(user_buf->twa_cmd_pkt),
628 sizeof(struct twa_command_packet));
630 /* If there was a payload, copy it back too. */
632 error = copyout(tr->tr_data, user_buf->pdata,
633 user_buf->twa_drvr_pkt.buffer_length);
636 /* Free resources. */
638 free(tr->tr_data, M_DEVBUF);
640 twa_release_request(tr);
645 case TWA_IOCTL_SCAN_BUS:
646 /* Request CAM for a bus scan. */
647 twa_request_bus_scan(sc);
651 case TWA_IOCTL_GET_FIRST_EVENT:
652 twa_dbg_dprint(3, sc, "Get First Event");
654 if (sc->twa_aen_queue_wrapped) {
655 if (sc->twa_aen_queue_overflow) {
657 * The aen queue has wrapped, even before some
658 * events have been retrieved. Let the caller
659 * know that he missed out on some AEN's.
661 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_OVERFLOW;
662 sc->twa_aen_queue_overflow = FALSE;
664 user_buf->twa_drvr_pkt.status = 0;
665 event_index = sc->twa_aen_head;
667 if (sc->twa_aen_head == sc->twa_aen_tail) {
668 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
671 user_buf->twa_drvr_pkt.status = 0;
672 event_index = sc->twa_aen_tail; /* = 0 */
674 if ((error = copyout(sc->twa_aen_queue[event_index], user_buf->pdata,
675 sizeof(struct twa_event_packet))) != 0)
676 twa_printf(sc, "get_first: Could not copyout to event_buf. error = %x\n", error);
677 (sc->twa_aen_queue[event_index])->retrieved = TWA_AEN_RETRIEVED;
681 case TWA_IOCTL_GET_LAST_EVENT:
682 twa_dbg_dprint(3, sc, "Get Last Event");
684 if (sc->twa_aen_queue_wrapped) {
685 if (sc->twa_aen_queue_overflow) {
687 * The aen queue has wrapped, even before some
688 * events have been retrieved. Let the caller
689 * know that he missed out on some AEN's.
691 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_OVERFLOW;
692 sc->twa_aen_queue_overflow = FALSE;
694 user_buf->twa_drvr_pkt.status = 0;
696 if (sc->twa_aen_head == sc->twa_aen_tail) {
697 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
700 user_buf->twa_drvr_pkt.status = 0;
702 event_index = (sc->twa_aen_head - 1 + TWA_Q_LENGTH) % TWA_Q_LENGTH;
703 if ((error = copyout(sc->twa_aen_queue[event_index], user_buf->pdata,
704 sizeof(struct twa_event_packet))) != 0)
705 twa_printf(sc, "get_last: Could not copyout to event_buf. error = %x\n", error);
706 (sc->twa_aen_queue[event_index])->retrieved = TWA_AEN_RETRIEVED;
710 case TWA_IOCTL_GET_NEXT_EVENT:
711 twa_dbg_dprint(3, sc, "Get Next Event");
713 user_buf->twa_drvr_pkt.status = 0;
714 if (sc->twa_aen_queue_wrapped) {
715 twa_dbg_dprint(3, sc, "Get Next Event: wrapped");
716 if (sc->twa_aen_queue_overflow) {
718 * The aen queue has wrapped, even before some
719 * events have been retrieved. Let the caller
720 * know that he missed out on some AEN's.
722 twa_dbg_dprint(2, sc, "Get Next Event: overflow");
723 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_OVERFLOW;
724 sc->twa_aen_queue_overflow = FALSE;
726 start_index = sc->twa_aen_head;
728 if (sc->twa_aen_head == sc->twa_aen_tail) {
729 twa_dbg_dprint(3, sc, "Get Next Event: empty queue");
730 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
733 start_index = sc->twa_aen_tail; /* = 0 */
735 if ((error = copyin(user_buf->pdata, &event_buf,
736 sizeof(struct twa_event_packet))) != 0)
737 twa_printf(sc, "get_next: Could not copyin event_buf.\n");
739 event_index = (start_index + event_buf.sequence_id -
740 (sc->twa_aen_queue[start_index])->sequence_id + 1)
743 twa_dbg_dprint(3, sc, "Get Next Event: si = %x, ei = %x, ebsi = %x, sisi = %x, eisi = %x",
744 start_index, event_index, event_buf.sequence_id,
745 (sc->twa_aen_queue[start_index])->sequence_id,
746 (sc->twa_aen_queue[event_index])->sequence_id);
748 if (! ((sc->twa_aen_queue[event_index])->sequence_id >
749 event_buf.sequence_id)) {
750 if (user_buf->twa_drvr_pkt.status == TWA_ERROR_AEN_OVERFLOW)
751 sc->twa_aen_queue_overflow = TRUE; /* so we report the overflow next time */
752 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
755 if ((error = copyout(sc->twa_aen_queue[event_index], user_buf->pdata,
756 sizeof(struct twa_event_packet))) != 0)
757 twa_printf(sc, "get_next: Could not copyout to event_buf. error = %x\n", error);
759 (sc->twa_aen_queue[event_index])->retrieved = TWA_AEN_RETRIEVED;
763 case TWA_IOCTL_GET_PREVIOUS_EVENT:
764 twa_dbg_dprint(3, sc, "Get Previous Event");
766 user_buf->twa_drvr_pkt.status = 0;
767 if (sc->twa_aen_queue_wrapped) {
768 if (sc->twa_aen_queue_overflow) {
770 * The aen queue has wrapped, even before some
771 * events have been retrieved. Let the caller
772 * know that he missed out on some AEN's.
774 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_OVERFLOW;
775 sc->twa_aen_queue_overflow = FALSE;
777 start_index = sc->twa_aen_head;
779 if (sc->twa_aen_head == sc->twa_aen_tail) {
780 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
783 start_index = sc->twa_aen_tail; /* = 0 */
785 if ((error = copyin(user_buf->pdata, &event_buf,
786 sizeof(struct twa_event_packet))) != 0)
787 twa_printf(sc, "get_previous: Could not copyin event_buf.\n");
789 event_index = (start_index + event_buf.sequence_id -
790 (sc->twa_aen_queue[start_index])->sequence_id - 1) % TWA_Q_LENGTH;
791 if (! ((sc->twa_aen_queue[event_index])->sequence_id < event_buf.sequence_id)) {
792 if (user_buf->twa_drvr_pkt.status == TWA_ERROR_AEN_OVERFLOW)
793 sc->twa_aen_queue_overflow = TRUE; /* so we report the overflow next time */
794 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
797 if ((error = copyout(sc->twa_aen_queue [event_index], user_buf->pdata,
798 sizeof(struct twa_event_packet))) != 0)
799 twa_printf(sc, "get_previous: Could not copyout to event_buf. error = %x\n", error);
801 (sc->twa_aen_queue[event_index])->retrieved = TWA_AEN_RETRIEVED;
805 case TWA_IOCTL_GET_LOCK:
807 struct twa_lock_packet twa_lock;
810 cur_time = time_second - (tz.tz_minuteswest * 60) -
811 (wall_cmos_clock ? adjkerntz : 0);
812 copyin(user_buf->pdata, &twa_lock,
813 sizeof(struct twa_lock_packet));
815 if ((sc->twa_ioctl_lock.lock == TWA_LOCK_FREE) ||
816 (twa_lock.force_flag) ||
817 (cur_time >= sc->twa_ioctl_lock.timeout)) {
818 twa_dbg_dprint(3, sc, "GET_LOCK: Getting lock!");
819 sc->twa_ioctl_lock.lock = TWA_LOCK_HELD;
820 sc->twa_ioctl_lock.timeout = cur_time + (twa_lock.timeout_msec / 1000);
821 twa_lock.time_remaining_msec = twa_lock.timeout_msec;
822 user_buf->twa_drvr_pkt.status = 0;
824 twa_dbg_dprint(2, sc, "GET_LOCK: Lock already held!");
825 twa_lock.time_remaining_msec =
826 (sc->twa_ioctl_lock.timeout - cur_time) * 1000;
827 user_buf->twa_drvr_pkt.status =
828 TWA_ERROR_IOCTL_LOCK_ALREADY_HELD;
831 copyout(&twa_lock, user_buf->pdata,
832 sizeof(struct twa_lock_packet));
837 case TWA_IOCTL_RELEASE_LOCK:
839 if (sc->twa_ioctl_lock.lock == TWA_LOCK_FREE) {
840 twa_dbg_dprint(2, sc, "twa_ioctl: RELEASE_LOCK: Lock not held!");
841 user_buf->twa_drvr_pkt.status = TWA_ERROR_IOCTL_LOCK_NOT_HELD;
843 twa_dbg_dprint(3, sc, "RELEASE_LOCK: Releasing lock!");
844 sc->twa_ioctl_lock.lock = TWA_LOCK_FREE;
845 user_buf->twa_drvr_pkt.status = 0;
851 case TWA_IOCTL_GET_COMPATIBILITY_INFO:
853 struct twa_compatibility_packet comp_pkt;
855 bcopy(TWA_DRIVER_VERSION_STRING, comp_pkt.driver_version,
856 sizeof(TWA_DRIVER_VERSION_STRING));
857 comp_pkt.working_srl = sc->working_srl;
858 comp_pkt.working_branch = sc->working_branch;
859 comp_pkt.working_build = sc->working_build;
860 user_buf->twa_drvr_pkt.status = 0;
862 /* Copy compatibility information to user space. */
863 copyout(&comp_pkt, user_buf->pdata,
864 min(sizeof(struct twa_compatibility_packet),
865 user_buf->twa_drvr_pkt.buffer_length));
870 /* Unknown opcode. */
879 * Function name: twa_enable_interrupts
880 * Description: Enables interrupts on the controller
882 * Input: sc -- ptr to per ctlr structure
887 twa_enable_interrupts(struct twa_softc *sc)
889 sc->twa_state |= TWA_STATE_INTR_ENABLED;
890 TWA_WRITE_CONTROL_REGISTER(sc,
891 TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT |
892 TWA_CONTROL_UNMASK_RESPONSE_INTERRUPT |
893 TWA_CONTROL_ENABLE_INTERRUPTS);
898 * Function name: twa_setup
899 * Description: Disables interrupts on the controller
901 * Input: sc -- ptr to per ctlr structure
906 twa_disable_interrupts(struct twa_softc *sc)
908 TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_DISABLE_INTERRUPTS);
909 sc->twa_state &= ~TWA_STATE_INTR_ENABLED;
915 * Function name: twa_get_param
916 * Description: Get a firmware parameter.
918 * Input: sc -- ptr to per ctlr structure
919 * table_id -- parameter table #
920 * param_id -- index of the parameter in the table
921 * param_size -- size of the parameter in bytes
922 * callback -- ptr to function, if any, to be called
923 * back on completion; NULL if no callback.
925 * Return value: ptr to param structure -- success
929 twa_get_param(struct twa_softc *sc, int table_id, int param_id,
930 size_t param_size, void (* callback)(struct twa_request *tr))
932 struct twa_request *tr;
933 union twa_command_7k *cmd;
934 struct twa_param_9k *param = NULL;
937 twa_dbg_dprint_enter(4, sc);
939 /* Get a request packet. */
940 if ((tr = twa_get_request(sc)) == NULL)
942 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
944 /* Allocate memory to read data into. */
945 param = malloc(TWA_SECTOR_SIZE, M_DEVBUF, M_INTWAIT);
946 bzero(param, sizeof(struct twa_param_9k) - 1 + param_size);
948 tr->tr_length = TWA_SECTOR_SIZE;
949 tr->tr_flags = TWA_CMD_DATA_IN | TWA_CMD_DATA_OUT;
951 /* Build the cmd pkt. */
952 cmd = &(tr->tr_command->command.cmd_pkt_7k);
954 tr->tr_command->cmd_hdr.header_desc.size_header = 128;
956 cmd->param.opcode = TWA_OP_GET_PARAM;
957 cmd->param.sgl_offset = 2;
959 cmd->param.request_id = tr->tr_request_id;
961 cmd->param.param_count = 1;
963 /* Specify which parameter we need. */
964 param->table_id = table_id | TWA_9K_PARAM_DESCRIPTOR;
965 param->parameter_id = param_id;
966 param->parameter_size_bytes = param_size;
968 /* Submit the command. */
969 if (callback == NULL) {
970 /* There's no call back; wait till the command completes. */
971 error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
972 if (error == ETIMEDOUT)
973 return(NULL); /* clean-up done by twa_immediate_request */
976 if ((error = cmd->param.status)) {
977 twa_printf(sc, "cmd = 0x%x: ERROR: (0x%02X: 0x%04X): %s: %s\n",
979 TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
980 tr->tr_command->cmd_hdr.status_block.error,
981 twa_find_msg_string(twa_error_table,
982 tr->tr_command->cmd_hdr.status_block.error),
983 tr->tr_command->cmd_hdr.err_specific_desc);
984 goto out; /* twa_drain_complete_queue will have done the unmapping */
986 twa_release_request(tr);
989 /* There's a call back. Simply submit the command. */
990 tr->tr_callback = callback;
991 if ((error = twa_map_request(tr))) {
992 twa_printf(tr->tr_sc, "%s: twa_map_request returned 0x%x\n",
1000 twa_printf(sc, "get_param failed. error = 0x%x\n", error);
1002 free(param, M_DEVBUF);
1004 twa_release_request(tr);
1010 * Function name: twa_set_param
1011 * Description: Set a firmware parameter.
1013 * Input: sc -- ptr to per ctlr structure
1014 * table_id -- parameter table #
1015 * param_id -- index of the parameter in the table
1016 * param_size -- size of the parameter in bytes
1017 * callback -- ptr to function, if any, to be called
1018 * back on completion; NULL if no callback.
1020 * Return value: 0 -- success
1021 * non-zero-- failure
1024 twa_set_param(struct twa_softc *sc, int table_id,
1025 int param_id, int param_size, void *data,
1026 void (* callback)(struct twa_request *tr))
1028 struct twa_request *tr;
1029 union twa_command_7k *cmd;
1030 struct twa_param_9k *param = NULL;
1033 twa_dbg_dprint_enter(4, sc);
1035 /* Get a request packet. */
1036 if ((tr = twa_get_request(sc)) == NULL)
1038 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
1040 /* Allocate memory to send data using. */
1041 param = malloc(TWA_SECTOR_SIZE, M_DEVBUF, M_INTWAIT);
1042 bzero(param, sizeof(struct twa_param_9k) - 1 + param_size);
1043 tr->tr_data = param;
1044 tr->tr_length = TWA_SECTOR_SIZE;
1045 tr->tr_flags = TWA_CMD_DATA_IN | TWA_CMD_DATA_OUT;
1047 /* Build the cmd pkt. */
1048 cmd = &(tr->tr_command->command.cmd_pkt_7k);
1050 tr->tr_command->cmd_hdr.header_desc.size_header = 128;
1052 cmd->param.opcode = TWA_OP_SET_PARAM;
1053 cmd->param.sgl_offset = 2;
1054 cmd->param.size = 2;
1055 cmd->param.request_id = tr->tr_request_id;
1056 cmd->param.unit = 0;
1057 cmd->param.param_count = 1;
1059 /* Specify which parameter we want to set. */
1060 param->table_id = table_id | TWA_9K_PARAM_DESCRIPTOR;
1061 param->parameter_id = param_id;
1062 param->parameter_size_bytes = param_size;
1063 bcopy(data, param->data, param_size);
1065 /* Submit the command. */
1066 if (callback == NULL) {
1067 /* There's no call back; wait till the command completes. */
1068 error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
1069 if (error == ETIMEDOUT)
1070 return(error); /* clean-up done by twa_immediate_request */
1073 if ((error = cmd->param.status)) {
1074 twa_printf(sc, "cmd = 0x%x: ERROR: (0x%02X: 0x%04X): %s: %s\n",
1076 TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
1077 tr->tr_command->cmd_hdr.status_block.error,
1078 twa_find_msg_string(twa_error_table,
1079 tr->tr_command->cmd_hdr.status_block.error),
1080 tr->tr_command->cmd_hdr.err_specific_desc);
1081 goto out; /* twa_drain_complete_queue will have done the unmapping */
1083 free(param, M_DEVBUF);
1084 twa_release_request(tr);
1087 /* There's a call back. Simply submit the command. */
1088 tr->tr_callback = callback;
1089 if ((error = twa_map_request(tr))) {
1090 twa_printf(tr->tr_sc, "%s: twa_map_request returned 0x%x\n",
1098 twa_printf(sc, "set_param failed. error = 0x%x\n", error);
1100 free(param, M_DEVBUF);
1102 twa_release_request(tr);
1108 * Function name: twa_init_connection
1109 * Description: Send init_connection cmd to firmware
1111 * Input: sc -- ptr to per ctlr structure
1112 * message_credits -- max # of requests that we might send
1113 * down simultaneously. This will be
1114 * typically set to 256 at init-time or
1115 * after a reset, and to 1 at shutdown-time
1116 * set_features -- indicates if we intend to use 64-bit
1117 * sg, also indicates if we want to do a
1118 * basic or an extended init_connection;
1120 * Note: The following input/output parameters are valid, only in case of an
1121 * extended init_connection:
1123 * current_fw_srl -- srl of fw we are bundled
1124 * with, if any; 0 otherwise
1125 * current_fw_arch_id -- arch_id of fw we are bundled
1126 * with, if any; 0 otherwise
1127 * current_fw_branch -- branch # of fw we are bundled
1128 * with, if any; 0 otherwise
1129 * current_fw_build -- build # of fw we are bundled
1130 * with, if any; 0 otherwise
1131 * Output: fw_on_ctlr_srl -- srl of fw on ctlr
1132 * fw_on_ctlr_arch_id -- arch_id of fw on ctlr
1133 * fw_on_ctlr_branch -- branch # of fw on ctlr
1134 * fw_on_ctlr_build -- build # of fw on ctlr
1135 * init_connect_result -- result bitmap of fw response
1136 * Return value: 0 -- success
1137 * non-zero-- failure
1140 twa_init_connection(struct twa_softc *sc, u_int16_t message_credits,
1141 u_int32_t set_features, u_int16_t current_fw_srl,
1142 u_int16_t current_fw_arch_id, u_int16_t current_fw_branch,
1143 u_int16_t current_fw_build, u_int16_t *fw_on_ctlr_srl,
1144 u_int16_t *fw_on_ctlr_arch_id, u_int16_t *fw_on_ctlr_branch,
1145 u_int16_t *fw_on_ctlr_build, u_int32_t *init_connect_result)
1147 struct twa_request *tr;
1148 struct twa_command_init_connect *init_connect;
1151 twa_dbg_dprint_enter(3, sc);
1153 /* Get a request packet. */
1154 if ((tr = twa_get_request(sc)) == NULL)
1156 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
1157 /* Build the cmd pkt. */
1158 init_connect = &(tr->tr_command->command.cmd_pkt_7k.init_connect);
1160 tr->tr_command->cmd_hdr.header_desc.size_header = 128;
1162 init_connect->opcode = TWA_OP_INIT_CONNECTION;
1163 init_connect->request_id = tr->tr_request_id;
1164 init_connect->message_credits = message_credits;
1165 init_connect->features = set_features;
1166 if (TWA_64BIT_ADDRESSES)
1167 init_connect->features |= TWA_64BIT_SG_ADDRESSES;
1168 if (set_features & TWA_EXTENDED_INIT_CONNECT) {
1169 /* Fill in the extra fields needed for an extended init_connect. */
1170 init_connect->size = 6;
1171 init_connect->fw_srl = current_fw_srl;
1172 init_connect->fw_arch_id = current_fw_arch_id;
1173 init_connect->fw_branch = current_fw_branch;
1174 init_connect->fw_build = current_fw_build;
1176 init_connect->size = 3;
1178 /* Submit the command, and wait for it to complete. */
1179 error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
1180 if (error == ETIMEDOUT)
1181 return(error); /* clean-up done by twa_immediate_request */
1184 if ((error = init_connect->status)) {
1185 twa_printf(sc, "cmd = 0x%x: ERROR: (0x%02X: 0x%04X): %s: %s\n",
1186 init_connect->opcode,
1187 TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
1188 tr->tr_command->cmd_hdr.status_block.error,
1189 twa_find_msg_string(twa_error_table,
1190 tr->tr_command->cmd_hdr.status_block.error),
1191 tr->tr_command->cmd_hdr.err_specific_desc);
1192 goto out; /* twa_drain_complete_queue will have done the unmapping */
1194 if (set_features & TWA_EXTENDED_INIT_CONNECT) {
1195 *fw_on_ctlr_srl = init_connect->fw_srl;
1196 *fw_on_ctlr_arch_id = init_connect->fw_arch_id;
1197 *fw_on_ctlr_branch = init_connect->fw_branch;
1198 *fw_on_ctlr_build = init_connect->fw_build;
1199 *init_connect_result = init_connect->result;
1201 twa_release_request(tr);
1205 twa_printf(sc, "init_connection failed. error = 0x%x\n", error);
1207 twa_release_request(tr);
1214 * Function name: twa_wait_request
1215 * Description: Sends down a firmware cmd, and waits for the completion,
1216 * but NOT in a tight loop.
1218 * Input: tr -- ptr to request pkt
1219 * timeout -- max # of seconds to wait before giving up
1221 * Return value: 0 -- success
1222 * non-zero-- failure
1225 twa_wait_request(struct twa_request *tr, u_int32_t timeout)
1230 twa_dbg_dprint_enter(4, tr->tr_sc);
1232 tr->tr_flags |= TWA_CMD_SLEEP_ON_REQUEST;
1233 tr->tr_status = TWA_CMD_BUSY;
1234 if ((error = twa_map_request(tr))) {
1235 twa_printf(tr->tr_sc, "%s: twa_map_request returned 0x%x\n",
1240 end_time = time_second + timeout;
1241 while (tr->tr_status != TWA_CMD_COMPLETE) {
1242 if ((error = tr->tr_error))
1244 if ((error = tsleep(tr, 0, "twawait", timeout * hz)) == 0) {
1245 error = (tr->tr_status != TWA_CMD_COMPLETE);
1248 if (error == EWOULDBLOCK) {
1250 twa_printf(tr->tr_sc, "%s: Request %p timed out.\n",
1253 * We will reset the controller only if the request has
1254 * already been submitted, so as to not lose the
1255 * request packet. If a busy request timed out, the
1256 * reset will take care of freeing resources. If a
1257 * pending request timed out, we will free resources
1258 * for that request, right here. So, the caller is
1259 * expected to NOT cleanup when ETIMEDOUT is returned.
1261 if (tr->tr_status != TWA_CMD_PENDING)
1262 twa_reset(tr->tr_sc);
1264 /* Request was never submitted. Clean up. */
1265 twa_remove_pending(tr);
1266 twa_unmap_request(tr);
1268 free(tr->tr_data, M_DEVBUF);
1269 twa_release_request(tr);
1274 * Either the request got completed, or we were woken up by a
1275 * signal. Calculate the new timeout, in case it was the latter.
1277 timeout = (end_time - time_second);
1279 twa_unmap_request(tr);
1286 * Function name: twa_immediate_request
1287 * Description: Sends down a firmware cmd, and waits for the completion
1290 * Input: tr -- ptr to request pkt
1291 * timeout -- max # of seconds to wait before giving up
1293 * Return value: 0 -- success
1294 * non-zero-- failure
1297 twa_immediate_request(struct twa_request *tr, u_int32_t timeout)
1302 twa_dbg_dprint_enter(4, tr->tr_sc);
1304 if ((error = twa_map_request(tr))) {
1305 twa_printf(tr->tr_sc, "%s: twa_map_request returned 0x%x\n",
1310 end_time = time_second + timeout;
1312 if ((error = tr->tr_error))
1314 twa_done(tr->tr_sc);
1315 if ((tr->tr_status != TWA_CMD_BUSY) &&
1316 (tr->tr_status != TWA_CMD_PENDING)) {
1317 twa_unmap_request(tr);
1318 return(tr->tr_status != TWA_CMD_COMPLETE);
1320 } while (time_second <= end_time);
1323 twa_printf(tr->tr_sc, "%s: Request %p timed out.\n", __func__, tr);
1325 * We will reset the controller only if the request has
1326 * already been submitted, so as to not lose the
1327 * request packet. If a busy request timed out, the
1328 * reset will take care of freeing resources. If a
1329 * pending request timed out, we will free resources
1330 * for that request, right here. So, the caller is
1331 * expected to NOT cleanup when ETIMEDOUT is returned.
1333 if (tr->tr_status != TWA_CMD_PENDING)
1334 twa_reset(tr->tr_sc);
1336 /* Request was never submitted. Clean up. */
1337 twa_remove_pending(tr);
1338 twa_unmap_request(tr);
1340 free(tr->tr_data, M_DEVBUF);
1341 twa_release_request(tr);
1349 * Function name: twa_complete_io
1350 * Description: Callback on scsi requests to fw.
1352 * Input: tr -- ptr to request pkt
1354 * Return value: None
1357 twa_complete_io(struct twa_request *tr)
1359 struct twa_softc *sc = tr->tr_sc;
1361 twa_dbg_dprint_enter(8, sc);
1363 if (tr->tr_status != TWA_CMD_COMPLETE)
1364 twa_panic(sc, "twa_complete_io on incomplete command");
1365 if (tr->tr_private) /* This is a scsi cmd. Complete it. */
1366 twa_scsi_complete(tr);
1367 twa_release_request(tr);
1372 * Function name: twa_reset
1373 * Description: Soft resets and then initializes the controller;
1374 * drains any incomplete requests.
1376 * Input: sc -- ptr to per ctlr structure
1378 * Return value: 0 -- success
1379 * non-zero-- failure
1382 twa_reset(struct twa_softc *sc)
1387 twa_dbg_dprint_enter(2, sc);
1390 * Disable interrupts from the controller, and mask any
1391 * accidental entry into our interrupt handler.
1393 twa_disable_interrupts(sc);
1396 /* Soft reset the controller. */
1397 if ((error = twa_soft_reset(sc))) {
1398 twa_printf (sc, "Controller reset failed.\n");
1402 /* Re-establish logical connection with the controller. */
1403 if ((error = twa_init_connection(sc, TWA_INIT_MESSAGE_CREDITS,
1405 NULL, NULL, NULL, NULL, NULL))) {
1406 twa_printf(sc, "Can't initialize connection after reset.\n");
1410 twa_printf(sc, "Controller reset done!\n");
1413 * Complete all requests in the complete queue; error back all requests
1414 * in the busy queue. Any internal requests will be simply freed.
1415 * Re-submit any requests in the pending queue.
1417 twa_drain_complete_queue(sc);
1418 twa_drain_busy_queue(sc);
1423 * Enable interrupts, and also clear attention and response interrupts.
1425 twa_enable_interrupts(sc);
1432 * Function name: twa_soft_reset
1433 * Description: Does the actual soft reset.
1435 * Input: sc -- ptr to per ctlr structure
1437 * Return value: 0 -- success
1438 * non-zero-- failure
1441 twa_soft_reset(struct twa_softc *sc)
1443 u_int32_t status_reg;
1445 twa_dbg_dprint_enter(1, sc);
1447 twa_printf(sc, "Resetting controller...\n");
1450 if (twa_wait_status(sc, TWA_STATUS_MICROCONTROLLER_READY |
1451 TWA_STATUS_ATTENTION_INTERRUPT, 30)) {
1452 twa_printf(sc, "Micro-ctlr not ready/No attn intr after reset.\n");
1455 TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT);
1456 if (twa_drain_response_queue(sc)) {
1457 twa_printf(sc, "Can't drain response queue.\n");
1460 if (twa_drain_aen_queue(sc)) {
1461 twa_printf(sc, "Can't drain AEN queue.\n");
1464 if (twa_find_aen(sc, TWA_AEN_SOFT_RESET)) {
1465 twa_printf(sc, "Reset not reported by controller.\n");
1468 status_reg = TWA_READ_STATUS_REGISTER(sc);
1469 if (TWA_STATUS_ERRORS(status_reg) ||
1470 twa_check_ctlr_state(sc, status_reg)) {
1471 twa_printf(sc, "Controller errors detected.\n");
1480 * Function name: twa_submit_io
1481 * Description: Wrapper to twa_start.
1483 * Input: tr -- ptr to request pkt
1485 * Return value: 0 -- success
1486 * non-zero-- failure
1489 twa_submit_io(struct twa_request *tr)
1493 if ((error = twa_start(tr))) {
1494 if (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_EXTERNAL) {
1497 * Cmd queue is full. Freeze the simq to
1498 * maintain ccb ordering. The next ccb that
1499 * gets completed will unfreeze the simq.
1501 twa_disallow_new_requests(tr->tr_sc);
1503 /* It's a controller error. */
1504 twa_printf(tr->tr_sc, "SCSI cmd = 0x%x: ERROR: (0x%02X: 0x%04X)\n",
1505 tr->tr_command->command.cmd_pkt_9k.cdb[0],
1506 TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
1509 tr->tr_error = error;
1510 twa_scsi_complete(tr);
1513 error = 0; /* the request will be in the pending queue */
1515 twa_printf(tr->tr_sc, "cmd = 0x%x: ERROR: (0x%02X: 0x%04X)\n",
1516 (tr->tr_cmd_pkt_type == TWA_CMD_PKT_TYPE_9K) ?
1517 (tr->tr_command->command.cmd_pkt_9k.command.opcode) :
1518 (tr->tr_command->command.cmd_pkt_7k.generic.opcode),
1519 TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
1521 tr->tr_error = error;
1531 * Function name: twa_start
1532 * Description: Posts a cmd to firmware.
1534 * Input: tr -- ptr to request pkt
1536 * Return value: 0 -- success
1537 * non-zero-- failure
1540 twa_start(struct twa_request *tr)
1542 struct twa_softc *sc = tr->tr_sc;
1543 u_int32_t status_reg;
1547 twa_dbg_dprint_enter(10, sc);
1550 /* Check to see if we can post a command. */
1551 status_reg = TWA_READ_STATUS_REGISTER(sc);
1552 if ((error = twa_check_ctlr_state(sc, status_reg)))
1555 if (status_reg & TWA_STATUS_COMMAND_QUEUE_FULL) {
1556 if ((tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_INTERNAL) ||
1557 (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_IOCTL)) {
1558 if (tr->tr_status != TWA_CMD_PENDING) {
1559 twa_dbg_dprint(2, sc, "pending internal/ioctl request");
1560 tr->tr_status = TWA_CMD_PENDING;
1561 twa_enqueue_pending(tr);
1563 TWA_WRITE_CONTROL_REGISTER(sc,
1564 TWA_CONTROL_UNMASK_COMMAND_INTERRUPT);
1568 /* Cmd queue is not full. Post the command. */
1569 TWA_WRITE_COMMAND_QUEUE(sc,
1570 tr->tr_cmd_phys + sizeof(struct twa_command_header));
1571 /* Mark the request as currently being processed. */
1572 tr->tr_status = TWA_CMD_BUSY;
1573 /* Move the request into the busy queue. */
1574 twa_enqueue_busy(tr);
1585 * Function name: twa_done
1586 * Description: Looks for cmd completions from fw; queues cmds completed
1587 * by fw into complete queue.
1589 * Input: sc -- ptr to per ctlr structure
1591 * Return value: 0 -- no ctlr error
1592 * non-zero-- ctlr error
1595 twa_done(struct twa_softc *sc)
1597 union twa_response_queue rq;
1598 struct twa_request *tr;
1601 u_int32_t status_reg;
1603 twa_dbg_dprint_enter(10, sc);
1607 status_reg = TWA_READ_STATUS_REGISTER(sc);
1608 if ((error = twa_check_ctlr_state(sc, status_reg)))
1610 if (status_reg & TWA_STATUS_RESPONSE_QUEUE_EMPTY)
1612 /* Response queue is not empty. */
1613 rq = TWA_READ_RESPONSE_QUEUE(sc);
1614 tr = sc->twa_lookup[rq.u.response_id]; /* lookup the request */
1615 if (tr->tr_status != TWA_CMD_BUSY)
1616 twa_printf(sc, "ERROR: Unposted command completed!! req = %p; status = %d\n",
1618 tr->tr_status = TWA_CMD_COMPLETE;
1619 /* Enqueue request in the complete queue. */
1620 twa_remove_busy(tr);
1621 twa_enqueue_complete(tr);
1625 /* Complete this, and other requests in the complete queue. */
1626 twa_drain_complete_queue(sc);
1633 * Function name: twa_drain_pending_queue
1634 * Description: Kick starts any requests in the pending queue.
1636 * Input: sc -- ptr to per ctlr structure
1638 * Return value: 0 -- all pending requests drained
1639 * non-zero-- otherwise
1642 twa_drain_pending_queue(struct twa_softc *sc)
1644 struct twa_request *tr;
1647 twa_dbg_dprint_enter(10, sc);
1650 * Pull requests off the pending queue, and submit them.
1652 while ((tr = twa_dequeue_pending(sc)) != NULL) {
1653 if ((error = twa_start(tr))) {
1654 if (error == EBUSY) {
1655 twa_dbg_dprint(2, sc, "Requeueing pending request");
1656 tr->tr_status = TWA_CMD_PENDING;
1657 twa_requeue_pending(tr);/* queue at the head */
1660 twa_printf(sc, "%s: twa_start returned 0x%x\n",
1662 if (tr->tr_flags & TWA_CMD_SLEEP_ON_REQUEST)
1663 wakeup_one(tr);/* let the caller know it failed */
1664 tr->tr_error = error;
1675 * Function name: twa_drain_complete_queue
1676 * Description: Does unmapping for each request completed by fw,
1677 * and lets the request originators know of the completion.
1679 * Input: sc -- ptr to per ctlr structure
1681 * Return value: None
1684 twa_drain_complete_queue(struct twa_softc *sc)
1686 struct twa_request *tr;
1688 twa_dbg_dprint_enter(10, sc);
1691 * Pull commands off the completed list, dispatch them appropriately.
1693 while ((tr = twa_dequeue_complete(sc)) != NULL) {
1694 /* Unmap the command packet, and any associated data buffer. */
1695 twa_unmap_request(tr);
1697 /* Call the callback, if there's one. */
1698 if (tr->tr_callback)
1699 tr->tr_callback(tr);
1701 if (tr->tr_flags & TWA_CMD_SLEEP_ON_REQUEST) {
1702 /* Wake up the sleeping command originator. */
1703 twa_dbg_dprint(7, sc, "Waking up originator of request %p", tr);
1712 * Function name: twa_wait_status
1713 * Description: Wait for a given status to show up in the fw status register.
1715 * Input: sc -- ptr to per ctlr structure
1716 * status -- status to look for
1717 * timeout -- max # of seconds to wait before giving up
1719 * Return value: 0 -- success
1720 * non-zero-- failure
1723 twa_wait_status(struct twa_softc *sc, u_int32_t status, u_int32_t timeout)
1726 u_int32_t status_reg;
1728 twa_dbg_dprint_enter(4, sc);
1730 end_time = time_second + timeout;
1732 status_reg = TWA_READ_STATUS_REGISTER(sc);
1733 if ((status_reg & status) == status)/* got the required bit(s)? */
1736 } while (time_second <= end_time);
1744 * Function name: twa_drain_response_queue
1745 * Description: Drain the response queue.
1747 * Input: sc -- ptr to per ctlr structure
1749 * Return value: 0 -- success
1750 * non-zero-- failure
1753 twa_drain_response_queue(struct twa_softc *sc)
1755 union twa_response_queue rq;
1756 u_int32_t status_reg;
1758 twa_dbg_dprint_enter(4, sc);
1761 status_reg = TWA_READ_STATUS_REGISTER(sc);
1762 if (twa_check_ctlr_state(sc, status_reg))
1764 if (status_reg & TWA_STATUS_RESPONSE_QUEUE_EMPTY)
1765 return(0); /* no more response queue entries */
1766 rq = TWA_READ_RESPONSE_QUEUE(sc);
1773 * Function name: twa_host_intr
1774 * Description: This function gets called if we triggered an interrupt.
1775 * We don't use it as of now.
1777 * Input: sc -- ptr to per ctlr structure
1779 * Return value: None
1782 twa_host_intr(struct twa_softc *sc)
1784 twa_dbg_dprint_enter(6, sc);
1786 TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_HOST_INTERRUPT);
1792 * Function name: twa_attention_intr
1793 * Description: This function gets called if the fw posted an AEN
1794 * (Asynchronous Event Notification). It fetches
1795 * all the AEN's that the fw might have posted.
1797 * Input: sc -- ptr to per ctlr structure
1799 * Return value: None
1802 twa_attention_intr(struct twa_softc *sc)
1806 twa_dbg_dprint_enter(6, sc);
1808 if ((error = twa_fetch_aen(sc)))
1809 twa_printf(sc, "Fetch AEN failed. error = 0x%x\n", error);
1810 TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT);
1816 * Function name: twa_command_intr
1817 * Description: This function gets called if we hit a queue full
1818 * condition earlier, and the fw is now ready for
1819 * new cmds. Submits any pending requests.
1821 * Input: sc -- ptr to per ctlr structure
1823 * Return value: None
1826 twa_command_intr(struct twa_softc *sc)
1828 twa_dbg_dprint_enter(6, sc);
1830 /* Start any requests that might be in the pending queue. */
1831 if (! twa_drain_pending_queue(sc))
1832 TWA_WRITE_CONTROL_REGISTER(sc,
1833 TWA_CONTROL_MASK_COMMAND_INTERRUPT);
1839 * Function name: twa_fetch_aen
1840 * Description: Send down a Request Sense cmd to fw to fetch an AEN.
1842 * Input: sc -- ptr to per ctlr structure
1844 * Return value: 0 -- success
1845 * non-zero-- failure
1848 twa_fetch_aen(struct twa_softc *sc)
1850 struct twa_request *tr;
1853 twa_dbg_dprint_enter(4, sc);
1855 if ((tr = twa_get_request(sc)) == NULL)
1857 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
1858 tr->tr_callback = twa_aen_callback;
1859 if ((error = twa_send_scsi_cmd(tr, 0x03 /* REQUEST_SENSE */))) {
1861 free(tr->tr_data, M_DEVBUF);
1862 twa_release_request(tr);
1870 * Function name: twa_aen_callback
1871 * Description: Callback for requests to fetch AEN's.
1873 * Input: tr -- ptr to completed request pkt
1875 * Return value: None
1878 twa_aen_callback(struct twa_request *tr)
1880 struct twa_softc *sc = tr->tr_sc;
1881 struct twa_command_header *cmd_hdr = (struct twa_command_header *)(tr->tr_data);
1882 struct twa_command_9k *cmd = &(tr->tr_command->command.cmd_pkt_9k);
1885 twa_dbg_dprint_enter(4, sc);
1887 twa_dbg_dprint(4, sc, "req_id = 0x%x, status = 0x%x",
1891 if (! cmd->status) {
1892 if ((tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_9K) &&
1893 (cmd->cdb[0] == 0x3 /* REQUEST_SENSE */))
1894 twa_enqueue_aen(sc, cmd_hdr);
1896 cmd_hdr->err_specific_desc[sizeof(cmd_hdr->err_specific_desc) - 1] = '\0';
1897 twa_printf(sc, "%s: cmd = 0x%x: ERROR: (0x%02X: 0x%04X): %s: %s\n",
1898 __func__, cmd->command.opcode,
1899 TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
1900 cmd_hdr->status_block.error,
1901 twa_find_msg_string(twa_error_table,
1902 cmd_hdr->status_block.error),
1903 cmd_hdr->err_specific_desc);
1904 twa_dbg_print(2, "sense info: ");
1905 for (i = 0; i < 18; i++)
1906 twa_dbg_print(2, "%x\t", tr->tr_command->cmd_hdr.sense_data[i]);
1907 twa_dbg_print(2, ""); /* print new line */
1908 for (i = 0; i < 128; i++)
1909 twa_dbg_print(7, "%x\t", ((int8_t *)(tr->tr_data))[i]);
1913 free(tr->tr_data, M_DEVBUF);
1914 twa_release_request(tr);
1920 * Function name: twa_drain_aen_queue
1921 * Description: Fetches all un-retrieved AEN's posted by fw.
1923 * Input: sc -- ptr to per ctlr structure
1925 * Return value: 0 -- success
1926 * non-zero-- failure
1929 twa_drain_aen_queue(struct twa_softc *sc)
1931 struct twa_request *tr;
1932 struct twa_command_header *cmd_hdr;
1937 if ((tr = twa_get_request(sc)) == NULL) {
1941 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
1942 tr->tr_callback = NULL;
1943 if ((error = twa_send_scsi_cmd(tr, 0x03 /* REQUEST_SENSE */))) {
1944 twa_dbg_dprint(1, sc, "Cannot send command to fetch aen");
1948 end_time = time_second + TWA_REQUEST_TIMEOUT_PERIOD;
1950 twa_done(tr->tr_sc);
1951 if (tr->tr_status != TWA_CMD_BUSY)
1953 } while (time_second <= end_time);
1955 if (tr->tr_status != TWA_CMD_COMPLETE) {
1960 if ((error = tr->tr_command->command.cmd_pkt_9k.status))
1963 cmd_hdr = (struct twa_command_header *)(tr->tr_data);
1964 if ((cmd_hdr->status_block.error) /* aen_code */
1965 == TWA_AEN_QUEUE_EMPTY)
1967 twa_enqueue_aen(sc, cmd_hdr);
1969 free(tr->tr_data, M_DEVBUF);
1970 twa_release_request(tr);
1975 free(tr->tr_data, M_DEVBUF);
1976 twa_release_request(tr);
1984 * Function name: twa_enqueue_aen
1985 * Description: Queues AEN's to be supplied to user-space tools on request.
1987 * Input: sc -- ptr to per ctlr structure
1988 * cmd_hdr -- ptr to hdr of fw cmd pkt, from where the AEN
1989 * details can be retrieved.
1991 * Return value: None
1994 twa_enqueue_aen(struct twa_softc *sc, struct twa_command_header *cmd_hdr)
1996 struct twa_event_packet *event;
1997 unsigned short aen_code;
1998 unsigned long local_time;
1999 unsigned long sync_time;
2002 twa_dbg_dprint_enter(4, sc);
2004 aen_code = cmd_hdr->status_block.error;
2007 case TWA_AEN_SYNC_TIME_WITH_HOST:
2008 twa_dbg_dprint(4, sc, "Received AEN_SYNC_TIME");
2009 /* Calculate time (in seconds) since last Sunday 12.00 AM. */
2010 local_time = time_second - (tz.tz_minuteswest * 60) -
2011 (wall_cmos_clock ? adjkerntz : 0);
2012 sync_time = (local_time - (3 * 86400)) % 604800;
2013 if (twa_set_param(sc, TWA_PARAM_TIME_TABLE,
2014 TWA_PARAM_TIME_SchedulerTime, 4,
2015 &sync_time, twa_aen_callback))
2016 twa_printf(sc, "Unable to sync time with ctlr!\n");
2019 case TWA_AEN_QUEUE_EMPTY:
2020 twa_dbg_dprint(4, sc, "AEN queue empty");
2024 /* Queue the event. */
2025 event = sc->twa_aen_queue[sc->twa_aen_head];
2026 if (event->retrieved == TWA_AEN_NOT_RETRIEVED)
2027 sc->twa_aen_queue_overflow = TRUE;
2028 event->severity = cmd_hdr->status_block.substatus_block.severity;
2029 local_time = time_second - (tz.tz_minuteswest * 60) -
2030 (wall_cmos_clock ? adjkerntz : 0);
2031 event->time_stamp_sec = local_time;
2032 event->aen_code = aen_code;
2033 event->retrieved = TWA_AEN_NOT_RETRIEVED;
2034 event->sequence_id = ++(sc->twa_current_sequence_id);
2035 cmd_hdr->err_specific_desc[sizeof(cmd_hdr->err_specific_desc) - 1] = '\0';
2036 event->parameter_len = strlen(cmd_hdr->err_specific_desc);
2037 bcopy(cmd_hdr->err_specific_desc, event->parameter_data,
2038 event->parameter_len);
2040 twa_dbg_dprint(4, sc, "event = %x %x %x %x %x %x %x\n %s",
2042 event->time_stamp_sec,
2046 event->repeat_count,
2047 event->parameter_len,
2048 event->parameter_data);
2050 twa_dbg_dprint(4, sc, "cmd_hdr = %x %lx %x %x %x %x %x\n %s",
2051 sc->twa_current_sequence_id,
2053 cmd_hdr->status_block.error,
2054 cmd_hdr->status_block.substatus_block.severity,
2055 TWA_AEN_NOT_RETRIEVED,
2057 strlen(cmd_hdr->err_specific_desc),
2058 cmd_hdr->err_specific_desc);
2060 /* Print the event. */
2061 if (event->severity < TWA_AEN_SEVERITY_DEBUG)
2062 twa_printf(sc, "%s: (0x%02X: 0x%04X): %s: %s\n",
2063 twa_aen_severity_table[event->severity],
2064 TWA_MESSAGE_SOURCE_CONTROLLER_EVENT,
2066 twa_find_msg_string(twa_aen_table, aen_code),
2067 event->parameter_data);
2069 if ((sc->twa_aen_head + 1) == TWA_Q_LENGTH)
2070 sc->twa_aen_queue_wrapped = TRUE;
2071 sc->twa_aen_head = (sc->twa_aen_head + 1) % TWA_Q_LENGTH;
2080 * Function name: twa_find_aen
2081 * Description: Reports whether a given AEN ever occurred.
2083 * Input: sc -- ptr to per ctlr structure
2084 * aen_code-- AEN to look for
2086 * Return value: 0 -- success
2087 * non-zero-- failure
2090 twa_find_aen(struct twa_softc *sc, u_int16_t aen_code)
2092 u_int32_t last_index;
2098 if (sc->twa_aen_queue_wrapped)
2099 last_index = sc->twa_aen_head;
2103 i = sc->twa_aen_head;
2105 i = (i + TWA_Q_LENGTH - 1) % TWA_Q_LENGTH;
2106 if ((sc->twa_aen_queue[i])->aen_code == aen_code) {
2110 } while (i != last_index);
2119 * Function name: twa_find_msg_string
2120 * Description: Looks up a given table, and returns the message string
2121 * corresponding to a given code (error code or AEN code).
2123 * Input: sc -- ptr to per ctlr structure
2124 * code -- code, the message string corresponding to
2125 * which is to be returned.
2127 * Return value: ptr to corresponding msg string -- success
2131 twa_find_msg_string(struct twa_message *table, u_int16_t code)
2135 for (i = 0; table[i].message != NULL; i++)
2136 if (table[i].code == code)
2137 return(table[i].message);
2139 return(table[i].message);
2145 * Function name: twa_get_request
2146 * Description: Gets a request pkt from the free queue.
2148 * Input: sc -- ptr to per ctlr structure
2150 * Return value: ptr to request pkt -- success
2153 struct twa_request *
2154 twa_get_request(struct twa_softc *sc)
2156 struct twa_request *tr;
2158 twa_dbg_dprint_enter(4, sc);
2160 /* Get a free request packet. */
2161 tr = twa_dequeue_free(sc);
2163 /* Initialize some fields to their defaults. */
2166 tr->tr_real_data = NULL;
2168 tr->tr_real_length = 0;
2169 tr->tr_status = TWA_CMD_SETUP;/* command is in setup phase */
2172 tr->tr_private = NULL;
2173 tr->tr_callback = NULL;
2174 tr->tr_cmd_pkt_type = 0;
2177 * Look at the status field in the command packet to see how
2178 * it completed the last time it was used, and zero out only
2179 * the portions that might have changed. Note that we don't
2180 * care to zero out the sglist.
2182 if (tr->tr_command->command.cmd_pkt_9k.status)
2183 bzero(tr->tr_command,
2184 sizeof(struct twa_command_header) + 28 /* max bytes before sglist */);
2186 bzero(&(tr->tr_command->command), 28 /* max bytes before sglist */);
2194 * Function name: twa_release_request
2195 * Description: Puts a request pkt into the free queue.
2197 * Input: tr -- ptr to request pkt to be freed
2199 * Return value: None
2202 twa_release_request(struct twa_request *tr)
2204 twa_dbg_dprint_enter(4, tr->tr_sc);
2206 twa_enqueue_free(tr);
2212 * Function name: twa_describe_controller
2213 * Description: Describes the controller, in terms of its fw version,
2216 * Input: sc -- ptr to per ctlr structure
2218 * Return value: None
2221 twa_describe_controller(struct twa_softc *sc)
2223 struct twa_param_9k *p[6];
2224 u_int8_t num_ports = 0;
2226 twa_dbg_dprint_enter(2, sc);
2228 /* Get the port count. */
2229 p[0] = twa_get_param(sc, TWA_PARAM_CONTROLLER_TABLE,
2230 TWA_PARAM_CONTROLLER_PORT_COUNT, 1, NULL);
2232 num_ports = *(u_int8_t *)(p[0]->data);
2233 free(p[0], M_DEVBUF);
2236 /* Get the firmware and BIOS versions. */
2237 p[0] = twa_get_param(sc, TWA_PARAM_VERSION_TABLE,
2238 TWA_PARAM_VERSION_FW, 16, NULL);
2239 p[1] = twa_get_param(sc, TWA_PARAM_VERSION_TABLE,
2240 TWA_PARAM_VERSION_BIOS, 16, NULL);
2242 twa_printf(sc, "%d ports, Firmware %.16s, BIOS %.16s\n",
2243 num_ports, p[0]?(p[0]->data):NULL, p[1]?(p[1]->data):NULL);
2245 /* Get more versions. */
2246 p[2] = twa_get_param(sc, TWA_PARAM_VERSION_TABLE,
2247 TWA_PARAM_VERSION_MONITOR, 16, NULL);
2248 p[3] = twa_get_param(sc, TWA_PARAM_VERSION_TABLE,
2249 TWA_PARAM_VERSION_PCBA, 8, NULL);
2250 p[4] = twa_get_param(sc, TWA_PARAM_VERSION_TABLE,
2251 TWA_PARAM_VERSION_ATA, 8, NULL);
2252 p[5] = twa_get_param(sc, TWA_PARAM_VERSION_TABLE,
2253 TWA_PARAM_VERSION_PCI, 8, NULL);
2255 twa_printf(sc, "Monitor %.16s, PCB %.8s, Achip %.8s, Pchip %.8s\n",
2256 p[2]?(p[2]->data):NULL, p[3]?(p[3]->data):NULL,
2257 p[4]?(p[4]->data):NULL, p[5]?(p[5]->data):NULL);
2260 free(p[2], M_DEVBUF);
2262 free(p[3], M_DEVBUF);
2264 free(p[4], M_DEVBUF);
2266 free(p[5], M_DEVBUF);
2269 free(p[0], M_DEVBUF);
2271 free(p[1], M_DEVBUF);
2277 * Function name: twa_check_ctlr_state
2278 * Description: Makes sure that the fw status register reports a
2281 * Input: sc -- ptr to per ctlr structure
2282 * status_reg -- value in the status register
2284 * Return value: 0 -- no errors
2288 twa_check_ctlr_state(struct twa_softc *sc, u_int32_t status_reg)
2291 static time_t last_warning[2] = {0, 0};
2293 /* Check if the 'micro-controller ready' bit is not set. */
2294 if ((status_reg & TWA_STATUS_EXPECTED_BITS) !=
2295 TWA_STATUS_EXPECTED_BITS) {
2296 if (time_second > (last_warning[0] + 5)) {
2297 twa_printf(sc, "Missing expected status bit(s) %b\n",
2298 ~status_reg & TWA_STATUS_EXPECTED_BITS,
2299 TWA_STATUS_BITS_DESCRIPTION);
2300 last_warning[0] = time_second;
2305 /* Check if any error bits are set. */
2306 if ((status_reg & TWA_STATUS_UNEXPECTED_BITS) != 0) {
2307 if (time_second > (last_warning[1] + 5)) {
2308 twa_printf(sc, "Unexpected status bit(s) %b\n",
2309 status_reg & TWA_STATUS_UNEXPECTED_BITS,
2310 TWA_STATUS_BITS_DESCRIPTION);
2311 last_warning[1] = time_second;
2313 if (status_reg & TWA_STATUS_PCI_PARITY_ERROR_INTERRUPT) {
2314 twa_printf(sc, "PCI parity error: clearing... Re-seat/move/replace card.\n");
2315 TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_PARITY_ERROR);
2316 twa_write_pci_config(sc, TWA_PCI_CONFIG_CLEAR_PARITY_ERROR, 2);
2318 if (status_reg & TWA_STATUS_PCI_ABORT_INTERRUPT) {
2319 twa_printf(sc, "PCI abort: clearing...\n");
2320 TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_PCI_ABORT);
2321 twa_write_pci_config(sc, TWA_PCI_CONFIG_CLEAR_PCI_ABORT, 2);
2323 if (status_reg & TWA_STATUS_QUEUE_ERROR_INTERRUPT) {
2324 twa_printf(sc, "Controller queue error: clearing...\n");
2325 TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_PCI_ABORT);
2327 if (status_reg & TWA_STATUS_SBUF_WRITE_ERROR) {
2328 twa_printf(sc, "SBUF write error: clearing...\n");
2329 TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_SBUF_WRITE_ERROR);
2331 if (status_reg & TWA_STATUS_MICROCONTROLLER_ERROR) {
2332 twa_printf(sc, "Micro-controller error!\n");
2342 * Function name: twa_print_controller
2343 * Description: Prints the current status of the controller.
2345 * Input: sc -- ptr to per ctlr structure
2347 * Return value: None
2350 twa_print_controller(struct twa_softc *sc)
2352 u_int32_t status_reg;
2354 /* Print current controller details. */
2355 status_reg = TWA_READ_STATUS_REGISTER(sc);
2356 twa_printf(sc, "status %b\n", status_reg, TWA_STATUS_BITS_DESCRIPTION);
2358 twa_printf(sc, "q type current max\n");
2359 twa_printf(sc, "free %04d %04d\n",
2360 sc->twa_qstats[TWAQ_FREE].q_length, sc->twa_qstats[TWAQ_FREE].q_max);
2361 twa_printf(sc, "busy %04d %04d\n",
2362 sc->twa_qstats[TWAQ_BUSY].q_length, sc->twa_qstats[TWAQ_BUSY].q_max);
2363 twa_printf(sc, "pending %04d %04d\n",
2364 sc->twa_qstats[TWAQ_PENDING].q_length, sc->twa_qstats[TWAQ_PENDING].q_max);
2365 twa_printf(sc, "complete %04d %04d\n",
2366 sc->twa_qstats[TWAQ_COMPLETE].q_length, sc->twa_qstats[TWAQ_COMPLETE].q_max);
2367 #endif /* TWA_DEBUG */
2368 twa_printf(sc, "AEN queue head %d tail %d\n",
2369 sc->twa_aen_head, sc->twa_aen_tail);
2375 * Function name: twa_panic
2376 * Description: Called when something is seriously wrong with the ctlr.
2377 * Hits the debugger if the debugger is turned on, else
2380 * Input: sc -- ptr to per ctlr structure
2381 * reason -- string describing what went wrong
2383 * Return value: None
2386 twa_panic(struct twa_softc *sc, int8_t *reason)
2388 twa_print_controller(sc);
2392 twa_printf(sc, "twa_panic: RESETTING CONTROLLER...\n");