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.4 2005/06/26 22:03:28 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;
563 case TWA_IOCTL_FIRMWARE_PASS_THROUGH:
565 struct twa_command_packet *cmdpkt;
566 struct twa_request *tr;
567 u_int32_t data_buf_size_adjusted;
570 twa_dbg_dprint(2, sc, "Firmware PassThru");
572 /* Get a request packet */
573 while ((tr = twa_get_request(sc)) == NULL)
575 * No free request packets available. Sleep until
576 * one becomes available.
578 tsleep(&(sc->twa_wait_timeout), 0, "twioctl", hz);
581 * Make sure that the data buffer sent to firmware is a
582 * 512 byte multiple in size.
584 data_buf_size_adjusted = (user_buf->twa_drvr_pkt.buffer_length + 511) & ~511;
585 if ((tr->tr_length = data_buf_size_adjusted)) {
586 if ((tr->tr_data = malloc(data_buf_size_adjusted, M_DEVBUF, M_WAITOK)) == NULL) {
587 twa_printf(sc, "Could not alloc mem for fw_passthru data_buf.\n");
589 goto fw_passthru_done;
591 /* Copy the payload. */
592 if ((error = copyin((void *) (user_buf->pdata),
593 (void *) (tr->tr_data),
594 user_buf->twa_drvr_pkt.buffer_length)) != 0) {
595 twa_printf (sc, "Could not copyin fw_passthru data_buf.\n");
596 goto fw_passthru_done;
598 tr->tr_flags |= TWA_CMD_DATA_IN | TWA_CMD_DATA_OUT;
600 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_IOCTL;
601 cmdpkt = tr->tr_command;
603 /* Copy the command packet. */
604 bcopy(&(user_buf->twa_cmd_pkt), cmdpkt,
605 sizeof(struct twa_command_packet));
606 cmdpkt->command.cmd_pkt_7k.generic.request_id = tr->tr_request_id;
608 twa_dbg_dprint(3, sc, "cmd_pkt_7k = %x %x %x %x %x %x %x",
609 cmdpkt->command.cmd_pkt_7k.generic.opcode,
610 cmdpkt->command.cmd_pkt_7k.generic.sgl_offset,
611 cmdpkt->command.cmd_pkt_7k.generic.size,
612 cmdpkt->command.cmd_pkt_7k.generic.request_id,
613 cmdpkt->command.cmd_pkt_7k.generic.unit,
614 cmdpkt->command.cmd_pkt_7k.generic.status,
615 cmdpkt->command.cmd_pkt_7k.generic.flags);
617 /* Send down the request, and wait for it to complete. */
618 if ((error = twa_wait_request(tr, TWA_REQUEST_TIMEOUT_PERIOD))) {
619 twa_printf(sc, "fw_passthru request failed. error = 0x%x\n", error);
620 if (error == ETIMEDOUT)
621 break; /* clean-up done by twa_wait_request */
622 goto fw_passthru_done;
625 /* Copy the command packet back into user space. */
626 bcopy(cmdpkt, &(user_buf->twa_cmd_pkt),
627 sizeof(struct twa_command_packet));
629 /* If there was a payload, copy it back too. */
631 error = copyout(tr->tr_data, user_buf->pdata,
632 user_buf->twa_drvr_pkt.buffer_length);
635 /* Free resources. */
637 free(tr->tr_data, M_DEVBUF);
639 twa_release_request(tr);
644 case TWA_IOCTL_SCAN_BUS:
645 /* Request CAM for a bus scan. */
646 twa_request_bus_scan(sc);
650 case TWA_IOCTL_GET_FIRST_EVENT:
651 twa_dbg_dprint(3, sc, "Get First Event");
653 if (sc->twa_aen_queue_wrapped) {
654 if (sc->twa_aen_queue_overflow) {
656 * The aen queue has wrapped, even before some
657 * events have been retrieved. Let the caller
658 * know that he missed out on some AEN's.
660 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_OVERFLOW;
661 sc->twa_aen_queue_overflow = FALSE;
663 user_buf->twa_drvr_pkt.status = 0;
664 event_index = sc->twa_aen_head;
666 if (sc->twa_aen_head == sc->twa_aen_tail) {
667 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
670 user_buf->twa_drvr_pkt.status = 0;
671 event_index = sc->twa_aen_tail; /* = 0 */
673 if ((error = copyout(sc->twa_aen_queue[event_index], user_buf->pdata,
674 sizeof(struct twa_event_packet))) != 0)
675 twa_printf(sc, "get_first: Could not copyout to event_buf. error = %x\n", error);
676 (sc->twa_aen_queue[event_index])->retrieved = TWA_AEN_RETRIEVED;
680 case TWA_IOCTL_GET_LAST_EVENT:
681 twa_dbg_dprint(3, sc, "Get Last Event");
683 if (sc->twa_aen_queue_wrapped) {
684 if (sc->twa_aen_queue_overflow) {
686 * The aen queue has wrapped, even before some
687 * events have been retrieved. Let the caller
688 * know that he missed out on some AEN's.
690 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_OVERFLOW;
691 sc->twa_aen_queue_overflow = FALSE;
693 user_buf->twa_drvr_pkt.status = 0;
695 if (sc->twa_aen_head == sc->twa_aen_tail) {
696 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
699 user_buf->twa_drvr_pkt.status = 0;
701 event_index = (sc->twa_aen_head - 1 + TWA_Q_LENGTH) % TWA_Q_LENGTH;
702 if ((error = copyout(sc->twa_aen_queue[event_index], user_buf->pdata,
703 sizeof(struct twa_event_packet))) != 0)
704 twa_printf(sc, "get_last: Could not copyout to event_buf. error = %x\n", error);
705 (sc->twa_aen_queue[event_index])->retrieved = TWA_AEN_RETRIEVED;
709 case TWA_IOCTL_GET_NEXT_EVENT:
710 twa_dbg_dprint(3, sc, "Get Next Event");
712 user_buf->twa_drvr_pkt.status = 0;
713 if (sc->twa_aen_queue_wrapped) {
714 twa_dbg_dprint(3, sc, "Get Next Event: wrapped");
715 if (sc->twa_aen_queue_overflow) {
717 * The aen queue has wrapped, even before some
718 * events have been retrieved. Let the caller
719 * know that he missed out on some AEN's.
721 twa_dbg_dprint(2, sc, "Get Next Event: overflow");
722 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_OVERFLOW;
723 sc->twa_aen_queue_overflow = FALSE;
725 start_index = sc->twa_aen_head;
727 if (sc->twa_aen_head == sc->twa_aen_tail) {
728 twa_dbg_dprint(3, sc, "Get Next Event: empty queue");
729 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
732 start_index = sc->twa_aen_tail; /* = 0 */
734 if ((error = copyin(user_buf->pdata, &event_buf,
735 sizeof(struct twa_event_packet))) != 0)
736 twa_printf(sc, "get_next: Could not copyin event_buf.\n");
738 event_index = (start_index + event_buf.sequence_id -
739 (sc->twa_aen_queue[start_index])->sequence_id + 1)
742 twa_dbg_dprint(3, sc, "Get Next Event: si = %x, ei = %x, ebsi = %x, sisi = %x, eisi = %x",
743 start_index, event_index, event_buf.sequence_id,
744 (sc->twa_aen_queue[start_index])->sequence_id,
745 (sc->twa_aen_queue[event_index])->sequence_id);
747 if (! ((sc->twa_aen_queue[event_index])->sequence_id >
748 event_buf.sequence_id)) {
749 if (user_buf->twa_drvr_pkt.status == TWA_ERROR_AEN_OVERFLOW)
750 sc->twa_aen_queue_overflow = TRUE; /* so we report the overflow next time */
751 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
754 if ((error = copyout(sc->twa_aen_queue[event_index], user_buf->pdata,
755 sizeof(struct twa_event_packet))) != 0)
756 twa_printf(sc, "get_next: Could not copyout to event_buf. error = %x\n", error);
758 (sc->twa_aen_queue[event_index])->retrieved = TWA_AEN_RETRIEVED;
762 case TWA_IOCTL_GET_PREVIOUS_EVENT:
763 twa_dbg_dprint(3, sc, "Get Previous Event");
765 user_buf->twa_drvr_pkt.status = 0;
766 if (sc->twa_aen_queue_wrapped) {
767 if (sc->twa_aen_queue_overflow) {
769 * The aen queue has wrapped, even before some
770 * events have been retrieved. Let the caller
771 * know that he missed out on some AEN's.
773 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_OVERFLOW;
774 sc->twa_aen_queue_overflow = FALSE;
776 start_index = sc->twa_aen_head;
778 if (sc->twa_aen_head == sc->twa_aen_tail) {
779 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
782 start_index = sc->twa_aen_tail; /* = 0 */
784 if ((error = copyin(user_buf->pdata, &event_buf,
785 sizeof(struct twa_event_packet))) != 0)
786 twa_printf(sc, "get_previous: Could not copyin event_buf.\n");
788 event_index = (start_index + event_buf.sequence_id -
789 (sc->twa_aen_queue[start_index])->sequence_id - 1) % TWA_Q_LENGTH;
790 if (! ((sc->twa_aen_queue[event_index])->sequence_id < event_buf.sequence_id)) {
791 if (user_buf->twa_drvr_pkt.status == TWA_ERROR_AEN_OVERFLOW)
792 sc->twa_aen_queue_overflow = TRUE; /* so we report the overflow next time */
793 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
796 if ((error = copyout(sc->twa_aen_queue [event_index], user_buf->pdata,
797 sizeof(struct twa_event_packet))) != 0)
798 twa_printf(sc, "get_previous: Could not copyout to event_buf. error = %x\n", error);
800 (sc->twa_aen_queue[event_index])->retrieved = TWA_AEN_RETRIEVED;
804 case TWA_IOCTL_GET_LOCK:
806 struct twa_lock_packet twa_lock;
809 cur_time = time_second - (tz.tz_minuteswest * 60) -
810 (wall_cmos_clock ? adjkerntz : 0);
811 copyin(user_buf->pdata, &twa_lock,
812 sizeof(struct twa_lock_packet));
814 if ((sc->twa_ioctl_lock.lock == TWA_LOCK_FREE) ||
815 (twa_lock.force_flag) ||
816 (cur_time >= sc->twa_ioctl_lock.timeout)) {
817 twa_dbg_dprint(3, sc, "GET_LOCK: Getting lock!");
818 sc->twa_ioctl_lock.lock = TWA_LOCK_HELD;
819 sc->twa_ioctl_lock.timeout = cur_time + (twa_lock.timeout_msec / 1000);
820 twa_lock.time_remaining_msec = twa_lock.timeout_msec;
821 user_buf->twa_drvr_pkt.status = 0;
823 twa_dbg_dprint(2, sc, "GET_LOCK: Lock already held!");
824 twa_lock.time_remaining_msec =
825 (sc->twa_ioctl_lock.timeout - cur_time) * 1000;
826 user_buf->twa_drvr_pkt.status =
827 TWA_ERROR_IOCTL_LOCK_ALREADY_HELD;
830 copyout(&twa_lock, user_buf->pdata,
831 sizeof(struct twa_lock_packet));
836 case TWA_IOCTL_RELEASE_LOCK:
838 if (sc->twa_ioctl_lock.lock == TWA_LOCK_FREE) {
839 twa_dbg_dprint(2, sc, "twa_ioctl: RELEASE_LOCK: Lock not held!");
840 user_buf->twa_drvr_pkt.status = TWA_ERROR_IOCTL_LOCK_NOT_HELD;
842 twa_dbg_dprint(3, sc, "RELEASE_LOCK: Releasing lock!");
843 sc->twa_ioctl_lock.lock = TWA_LOCK_FREE;
844 user_buf->twa_drvr_pkt.status = 0;
850 case TWA_IOCTL_GET_COMPATIBILITY_INFO:
852 struct twa_compatibility_packet comp_pkt;
854 bcopy(TWA_DRIVER_VERSION_STRING, comp_pkt.driver_version,
855 sizeof(TWA_DRIVER_VERSION_STRING));
856 comp_pkt.working_srl = sc->working_srl;
857 comp_pkt.working_branch = sc->working_branch;
858 comp_pkt.working_build = sc->working_build;
859 user_buf->twa_drvr_pkt.status = 0;
861 /* Copy compatibility information to user space. */
862 copyout(&comp_pkt, user_buf->pdata,
863 min(sizeof(struct twa_compatibility_packet),
864 user_buf->twa_drvr_pkt.buffer_length));
869 /* Unknown opcode. */
878 * Function name: twa_enable_interrupts
879 * Description: Enables interrupts on the controller
881 * Input: sc -- ptr to per ctlr structure
886 twa_enable_interrupts(struct twa_softc *sc)
888 sc->twa_state |= TWA_STATE_INTR_ENABLED;
889 TWA_WRITE_CONTROL_REGISTER(sc,
890 TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT |
891 TWA_CONTROL_UNMASK_RESPONSE_INTERRUPT |
892 TWA_CONTROL_ENABLE_INTERRUPTS);
897 * Function name: twa_setup
898 * Description: Disables interrupts on the controller
900 * Input: sc -- ptr to per ctlr structure
905 twa_disable_interrupts(struct twa_softc *sc)
907 TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_DISABLE_INTERRUPTS);
908 sc->twa_state &= ~TWA_STATE_INTR_ENABLED;
914 * Function name: twa_get_param
915 * Description: Get a firmware parameter.
917 * Input: sc -- ptr to per ctlr structure
918 * table_id -- parameter table #
919 * param_id -- index of the parameter in the table
920 * param_size -- size of the parameter in bytes
921 * callback -- ptr to function, if any, to be called
922 * back on completion; NULL if no callback.
924 * Return value: ptr to param structure -- success
928 twa_get_param(struct twa_softc *sc, int table_id, int param_id,
929 size_t param_size, void (* callback)(struct twa_request *tr))
931 struct twa_request *tr;
932 union twa_command_7k *cmd;
933 struct twa_param_9k *param = NULL;
936 twa_dbg_dprint_enter(4, sc);
938 /* Get a request packet. */
939 if ((tr = twa_get_request(sc)) == NULL)
941 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
943 /* Allocate memory to read data into. */
944 param = malloc(TWA_SECTOR_SIZE, M_DEVBUF, M_INTWAIT);
945 bzero(param, sizeof(struct twa_param_9k) - 1 + param_size);
947 tr->tr_length = TWA_SECTOR_SIZE;
948 tr->tr_flags = TWA_CMD_DATA_IN | TWA_CMD_DATA_OUT;
950 /* Build the cmd pkt. */
951 cmd = &(tr->tr_command->command.cmd_pkt_7k);
953 tr->tr_command->cmd_hdr.header_desc.size_header = 128;
955 cmd->param.opcode = TWA_OP_GET_PARAM;
956 cmd->param.sgl_offset = 2;
958 cmd->param.request_id = tr->tr_request_id;
960 cmd->param.param_count = 1;
962 /* Specify which parameter we need. */
963 param->table_id = table_id | TWA_9K_PARAM_DESCRIPTOR;
964 param->parameter_id = param_id;
965 param->parameter_size_bytes = param_size;
967 /* Submit the command. */
968 if (callback == NULL) {
969 /* There's no call back; wait till the command completes. */
970 error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
971 if (error == ETIMEDOUT)
972 return(NULL); /* clean-up done by twa_immediate_request */
975 if ((error = cmd->param.status)) {
976 twa_printf(sc, "cmd = 0x%x: ERROR: (0x%02X: 0x%04X): %s: %s\n",
978 TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
979 tr->tr_command->cmd_hdr.status_block.error,
980 twa_find_msg_string(twa_error_table,
981 tr->tr_command->cmd_hdr.status_block.error),
982 tr->tr_command->cmd_hdr.err_specific_desc);
983 goto out; /* twa_drain_complete_queue will have done the unmapping */
985 twa_release_request(tr);
988 /* There's a call back. Simply submit the command. */
989 tr->tr_callback = callback;
990 if ((error = twa_map_request(tr))) {
991 twa_printf(tr->tr_sc, "%s: twa_map_request returned 0x%x\n",
999 twa_printf(sc, "get_param failed. error = 0x%x\n", error);
1001 free(param, M_DEVBUF);
1003 twa_release_request(tr);
1009 * Function name: twa_set_param
1010 * Description: Set a firmware parameter.
1012 * Input: sc -- ptr to per ctlr structure
1013 * table_id -- parameter table #
1014 * param_id -- index of the parameter in the table
1015 * param_size -- size of the parameter in bytes
1016 * callback -- ptr to function, if any, to be called
1017 * back on completion; NULL if no callback.
1019 * Return value: 0 -- success
1020 * non-zero-- failure
1023 twa_set_param(struct twa_softc *sc, int table_id,
1024 int param_id, int param_size, void *data,
1025 void (* callback)(struct twa_request *tr))
1027 struct twa_request *tr;
1028 union twa_command_7k *cmd;
1029 struct twa_param_9k *param = NULL;
1032 twa_dbg_dprint_enter(4, sc);
1034 /* Get a request packet. */
1035 if ((tr = twa_get_request(sc)) == NULL)
1037 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
1039 /* Allocate memory to send data using. */
1040 param = malloc(TWA_SECTOR_SIZE, M_DEVBUF, M_INTWAIT);
1041 bzero(param, sizeof(struct twa_param_9k) - 1 + param_size);
1042 tr->tr_data = param;
1043 tr->tr_length = TWA_SECTOR_SIZE;
1044 tr->tr_flags = TWA_CMD_DATA_IN | TWA_CMD_DATA_OUT;
1046 /* Build the cmd pkt. */
1047 cmd = &(tr->tr_command->command.cmd_pkt_7k);
1049 tr->tr_command->cmd_hdr.header_desc.size_header = 128;
1051 cmd->param.opcode = TWA_OP_SET_PARAM;
1052 cmd->param.sgl_offset = 2;
1053 cmd->param.size = 2;
1054 cmd->param.request_id = tr->tr_request_id;
1055 cmd->param.unit = 0;
1056 cmd->param.param_count = 1;
1058 /* Specify which parameter we want to set. */
1059 param->table_id = table_id | TWA_9K_PARAM_DESCRIPTOR;
1060 param->parameter_id = param_id;
1061 param->parameter_size_bytes = param_size;
1062 bcopy(data, param->data, param_size);
1064 /* Submit the command. */
1065 if (callback == NULL) {
1066 /* There's no call back; wait till the command completes. */
1067 error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
1068 if (error == ETIMEDOUT)
1069 return(error); /* clean-up done by twa_immediate_request */
1072 if ((error = cmd->param.status)) {
1073 twa_printf(sc, "cmd = 0x%x: ERROR: (0x%02X: 0x%04X): %s: %s\n",
1075 TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
1076 tr->tr_command->cmd_hdr.status_block.error,
1077 twa_find_msg_string(twa_error_table,
1078 tr->tr_command->cmd_hdr.status_block.error),
1079 tr->tr_command->cmd_hdr.err_specific_desc);
1080 goto out; /* twa_drain_complete_queue will have done the unmapping */
1082 free(param, M_DEVBUF);
1083 twa_release_request(tr);
1086 /* There's a call back. Simply submit the command. */
1087 tr->tr_callback = callback;
1088 if ((error = twa_map_request(tr))) {
1089 twa_printf(tr->tr_sc, "%s: twa_map_request returned 0x%x\n",
1097 twa_printf(sc, "set_param failed. error = 0x%x\n", error);
1099 free(param, M_DEVBUF);
1101 twa_release_request(tr);
1107 * Function name: twa_init_connection
1108 * Description: Send init_connection cmd to firmware
1110 * Input: sc -- ptr to per ctlr structure
1111 * message_credits -- max # of requests that we might send
1112 * down simultaneously. This will be
1113 * typically set to 256 at init-time or
1114 * after a reset, and to 1 at shutdown-time
1115 * set_features -- indicates if we intend to use 64-bit
1116 * sg, also indicates if we want to do a
1117 * basic or an extended init_connection;
1119 * Note: The following input/output parameters are valid, only in case of an
1120 * extended init_connection:
1122 * current_fw_srl -- srl of fw we are bundled
1123 * with, if any; 0 otherwise
1124 * current_fw_arch_id -- arch_id of fw we are bundled
1125 * with, if any; 0 otherwise
1126 * current_fw_branch -- branch # of fw we are bundled
1127 * with, if any; 0 otherwise
1128 * current_fw_build -- build # of fw we are bundled
1129 * with, if any; 0 otherwise
1130 * Output: fw_on_ctlr_srl -- srl of fw on ctlr
1131 * fw_on_ctlr_arch_id -- arch_id of fw on ctlr
1132 * fw_on_ctlr_branch -- branch # of fw on ctlr
1133 * fw_on_ctlr_build -- build # of fw on ctlr
1134 * init_connect_result -- result bitmap of fw response
1135 * Return value: 0 -- success
1136 * non-zero-- failure
1139 twa_init_connection(struct twa_softc *sc, u_int16_t message_credits,
1140 u_int32_t set_features, u_int16_t current_fw_srl,
1141 u_int16_t current_fw_arch_id, u_int16_t current_fw_branch,
1142 u_int16_t current_fw_build, u_int16_t *fw_on_ctlr_srl,
1143 u_int16_t *fw_on_ctlr_arch_id, u_int16_t *fw_on_ctlr_branch,
1144 u_int16_t *fw_on_ctlr_build, u_int32_t *init_connect_result)
1146 struct twa_request *tr;
1147 struct twa_command_init_connect *init_connect;
1150 twa_dbg_dprint_enter(3, sc);
1152 /* Get a request packet. */
1153 if ((tr = twa_get_request(sc)) == NULL)
1155 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
1156 /* Build the cmd pkt. */
1157 init_connect = &(tr->tr_command->command.cmd_pkt_7k.init_connect);
1159 tr->tr_command->cmd_hdr.header_desc.size_header = 128;
1161 init_connect->opcode = TWA_OP_INIT_CONNECTION;
1162 init_connect->request_id = tr->tr_request_id;
1163 init_connect->message_credits = message_credits;
1164 init_connect->features = set_features;
1165 if (TWA_64BIT_ADDRESSES)
1166 init_connect->features |= TWA_64BIT_SG_ADDRESSES;
1167 if (set_features & TWA_EXTENDED_INIT_CONNECT) {
1168 /* Fill in the extra fields needed for an extended init_connect. */
1169 init_connect->size = 6;
1170 init_connect->fw_srl = current_fw_srl;
1171 init_connect->fw_arch_id = current_fw_arch_id;
1172 init_connect->fw_branch = current_fw_branch;
1173 init_connect->fw_build = current_fw_build;
1175 init_connect->size = 3;
1177 /* Submit the command, and wait for it to complete. */
1178 error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
1179 if (error == ETIMEDOUT)
1180 return(error); /* clean-up done by twa_immediate_request */
1183 if ((error = init_connect->status)) {
1184 twa_printf(sc, "cmd = 0x%x: ERROR: (0x%02X: 0x%04X): %s: %s\n",
1185 init_connect->opcode,
1186 TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
1187 tr->tr_command->cmd_hdr.status_block.error,
1188 twa_find_msg_string(twa_error_table,
1189 tr->tr_command->cmd_hdr.status_block.error),
1190 tr->tr_command->cmd_hdr.err_specific_desc);
1191 goto out; /* twa_drain_complete_queue will have done the unmapping */
1193 if (set_features & TWA_EXTENDED_INIT_CONNECT) {
1194 *fw_on_ctlr_srl = init_connect->fw_srl;
1195 *fw_on_ctlr_arch_id = init_connect->fw_arch_id;
1196 *fw_on_ctlr_branch = init_connect->fw_branch;
1197 *fw_on_ctlr_build = init_connect->fw_build;
1198 *init_connect_result = init_connect->result;
1200 twa_release_request(tr);
1204 twa_printf(sc, "init_connection failed. error = 0x%x\n", error);
1206 twa_release_request(tr);
1213 * Function name: twa_wait_request
1214 * Description: Sends down a firmware cmd, and waits for the completion,
1215 * but NOT in a tight loop.
1217 * Input: tr -- ptr to request pkt
1218 * timeout -- max # of seconds to wait before giving up
1220 * Return value: 0 -- success
1221 * non-zero-- failure
1224 twa_wait_request(struct twa_request *tr, u_int32_t timeout)
1229 twa_dbg_dprint_enter(4, tr->tr_sc);
1231 tr->tr_flags |= TWA_CMD_SLEEP_ON_REQUEST;
1232 tr->tr_status = TWA_CMD_BUSY;
1233 if ((error = twa_map_request(tr))) {
1234 twa_printf(tr->tr_sc, "%s: twa_map_request returned 0x%x\n",
1239 end_time = time_second + timeout;
1240 while (tr->tr_status != TWA_CMD_COMPLETE) {
1241 if ((error = tr->tr_error))
1243 if ((error = tsleep(tr, 0, "twawait", timeout * hz)) == 0) {
1244 error = (tr->tr_status != TWA_CMD_COMPLETE);
1247 if (error == EWOULDBLOCK) {
1249 twa_printf(tr->tr_sc, "%s: Request %p timed out.\n",
1252 * We will reset the controller only if the request has
1253 * already been submitted, so as to not lose the
1254 * request packet. If a busy request timed out, the
1255 * reset will take care of freeing resources. If a
1256 * pending request timed out, we will free resources
1257 * for that request, right here. So, the caller is
1258 * expected to NOT cleanup when ETIMEDOUT is returned.
1260 if (tr->tr_status != TWA_CMD_PENDING)
1261 twa_reset(tr->tr_sc);
1263 /* Request was never submitted. Clean up. */
1264 twa_remove_pending(tr);
1265 twa_unmap_request(tr);
1267 free(tr->tr_data, M_DEVBUF);
1268 twa_release_request(tr);
1273 * Either the request got completed, or we were woken up by a
1274 * signal. Calculate the new timeout, in case it was the latter.
1276 timeout = (end_time - time_second);
1278 twa_unmap_request(tr);
1285 * Function name: twa_immediate_request
1286 * Description: Sends down a firmware cmd, and waits for the completion
1289 * Input: tr -- ptr to request pkt
1290 * timeout -- max # of seconds to wait before giving up
1292 * Return value: 0 -- success
1293 * non-zero-- failure
1296 twa_immediate_request(struct twa_request *tr, u_int32_t timeout)
1301 twa_dbg_dprint_enter(4, tr->tr_sc);
1303 if ((error = twa_map_request(tr))) {
1304 twa_printf(tr->tr_sc, "%s: twa_map_request returned 0x%x\n",
1309 end_time = time_second + timeout;
1311 if ((error = tr->tr_error))
1313 twa_done(tr->tr_sc);
1314 if ((tr->tr_status != TWA_CMD_BUSY) &&
1315 (tr->tr_status != TWA_CMD_PENDING)) {
1316 twa_unmap_request(tr);
1317 return(tr->tr_status != TWA_CMD_COMPLETE);
1319 } while (time_second <= end_time);
1322 twa_printf(tr->tr_sc, "%s: Request %p timed out.\n", __func__, tr);
1324 * We will reset the controller only if the request has
1325 * already been submitted, so as to not lose the
1326 * request packet. If a busy request timed out, the
1327 * reset will take care of freeing resources. If a
1328 * pending request timed out, we will free resources
1329 * for that request, right here. So, the caller is
1330 * expected to NOT cleanup when ETIMEDOUT is returned.
1332 if (tr->tr_status != TWA_CMD_PENDING)
1333 twa_reset(tr->tr_sc);
1335 /* Request was never submitted. Clean up. */
1336 twa_remove_pending(tr);
1337 twa_unmap_request(tr);
1339 free(tr->tr_data, M_DEVBUF);
1340 twa_release_request(tr);
1348 * Function name: twa_complete_io
1349 * Description: Callback on scsi requests to fw.
1351 * Input: tr -- ptr to request pkt
1353 * Return value: None
1356 twa_complete_io(struct twa_request *tr)
1358 struct twa_softc *sc = tr->tr_sc;
1360 twa_dbg_dprint_enter(8, sc);
1362 if (tr->tr_status != TWA_CMD_COMPLETE)
1363 twa_panic(sc, "twa_complete_io on incomplete command");
1364 if (tr->tr_private) /* This is a scsi cmd. Complete it. */
1365 twa_scsi_complete(tr);
1366 twa_release_request(tr);
1371 * Function name: twa_reset
1372 * Description: Soft resets and then initializes the controller;
1373 * drains any incomplete requests.
1375 * Input: sc -- ptr to per ctlr structure
1377 * Return value: 0 -- success
1378 * non-zero-- failure
1381 twa_reset(struct twa_softc *sc)
1385 twa_dbg_dprint_enter(2, sc);
1388 * Disable interrupts from the controller, and mask any
1389 * accidental entry into our interrupt handler.
1391 twa_disable_interrupts(sc);
1394 /* Soft reset the controller. */
1395 if ((error = twa_soft_reset(sc))) {
1396 twa_printf (sc, "Controller reset failed.\n");
1400 /* Re-establish logical connection with the controller. */
1401 if ((error = twa_init_connection(sc, TWA_INIT_MESSAGE_CREDITS,
1403 NULL, NULL, NULL, NULL, NULL))) {
1404 twa_printf(sc, "Can't initialize connection after reset.\n");
1408 twa_printf(sc, "Controller reset done!\n");
1411 * Complete all requests in the complete queue; error back all requests
1412 * in the busy queue. Any internal requests will be simply freed.
1413 * Re-submit any requests in the pending queue.
1415 twa_drain_complete_queue(sc);
1416 twa_drain_busy_queue(sc);
1421 * Enable interrupts, and also clear attention and response interrupts.
1423 twa_enable_interrupts(sc);
1430 * Function name: twa_soft_reset
1431 * Description: Does the actual soft reset.
1433 * Input: sc -- ptr to per ctlr structure
1435 * Return value: 0 -- success
1436 * non-zero-- failure
1439 twa_soft_reset(struct twa_softc *sc)
1441 u_int32_t status_reg;
1443 twa_dbg_dprint_enter(1, sc);
1445 twa_printf(sc, "Resetting controller...\n");
1448 if (twa_wait_status(sc, TWA_STATUS_MICROCONTROLLER_READY |
1449 TWA_STATUS_ATTENTION_INTERRUPT, 30)) {
1450 twa_printf(sc, "Micro-ctlr not ready/No attn intr after reset.\n");
1453 TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT);
1454 if (twa_drain_response_queue(sc)) {
1455 twa_printf(sc, "Can't drain response queue.\n");
1458 if (twa_drain_aen_queue(sc)) {
1459 twa_printf(sc, "Can't drain AEN queue.\n");
1462 if (twa_find_aen(sc, TWA_AEN_SOFT_RESET)) {
1463 twa_printf(sc, "Reset not reported by controller.\n");
1466 status_reg = TWA_READ_STATUS_REGISTER(sc);
1467 if (TWA_STATUS_ERRORS(status_reg) ||
1468 twa_check_ctlr_state(sc, status_reg)) {
1469 twa_printf(sc, "Controller errors detected.\n");
1478 * Function name: twa_submit_io
1479 * Description: Wrapper to twa_start.
1481 * Input: tr -- ptr to request pkt
1483 * Return value: 0 -- success
1484 * non-zero-- failure
1487 twa_submit_io(struct twa_request *tr)
1491 if ((error = twa_start(tr))) {
1492 if (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_EXTERNAL) {
1495 * Cmd queue is full. Freeze the simq to
1496 * maintain ccb ordering. The next ccb that
1497 * gets completed will unfreeze the simq.
1499 twa_disallow_new_requests(tr->tr_sc);
1501 /* It's a controller error. */
1502 twa_printf(tr->tr_sc, "SCSI cmd = 0x%x: ERROR: (0x%02X: 0x%04X)\n",
1503 tr->tr_command->command.cmd_pkt_9k.cdb[0],
1504 TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
1507 tr->tr_error = error;
1508 twa_scsi_complete(tr);
1511 error = 0; /* the request will be in the pending queue */
1513 twa_printf(tr->tr_sc, "cmd = 0x%x: ERROR: (0x%02X: 0x%04X)\n",
1514 (tr->tr_cmd_pkt_type == TWA_CMD_PKT_TYPE_9K) ?
1515 (tr->tr_command->command.cmd_pkt_9k.command.opcode) :
1516 (tr->tr_command->command.cmd_pkt_7k.generic.opcode),
1517 TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
1519 tr->tr_error = error;
1529 * Function name: twa_start
1530 * Description: Posts a cmd to firmware.
1532 * Input: tr -- ptr to request pkt
1534 * Return value: 0 -- success
1535 * non-zero-- failure
1538 twa_start(struct twa_request *tr)
1540 struct twa_softc *sc = tr->tr_sc;
1541 u_int32_t status_reg;
1544 twa_dbg_dprint_enter(10, sc);
1547 /* Check to see if we can post a command. */
1548 status_reg = TWA_READ_STATUS_REGISTER(sc);
1549 if ((error = twa_check_ctlr_state(sc, status_reg)))
1552 if (status_reg & TWA_STATUS_COMMAND_QUEUE_FULL) {
1553 if ((tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_INTERNAL) ||
1554 (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_IOCTL)) {
1555 if (tr->tr_status != TWA_CMD_PENDING) {
1556 twa_dbg_dprint(2, sc, "pending internal/ioctl request");
1557 tr->tr_status = TWA_CMD_PENDING;
1558 twa_enqueue_pending(tr);
1560 TWA_WRITE_CONTROL_REGISTER(sc,
1561 TWA_CONTROL_UNMASK_COMMAND_INTERRUPT);
1565 /* Cmd queue is not full. Post the command. */
1566 TWA_WRITE_COMMAND_QUEUE(sc,
1567 tr->tr_cmd_phys + sizeof(struct twa_command_header));
1568 /* Mark the request as currently being processed. */
1569 tr->tr_status = TWA_CMD_BUSY;
1570 /* Move the request into the busy queue. */
1571 twa_enqueue_busy(tr);
1582 * Function name: twa_done
1583 * Description: Looks for cmd completions from fw; queues cmds completed
1584 * by fw into complete queue.
1586 * Input: sc -- ptr to per ctlr structure
1588 * Return value: 0 -- no ctlr error
1589 * non-zero-- ctlr error
1592 twa_done(struct twa_softc *sc)
1594 union twa_response_queue rq;
1595 struct twa_request *tr;
1597 u_int32_t status_reg;
1599 twa_dbg_dprint_enter(10, sc);
1603 status_reg = TWA_READ_STATUS_REGISTER(sc);
1604 if ((error = twa_check_ctlr_state(sc, status_reg)))
1606 if (status_reg & TWA_STATUS_RESPONSE_QUEUE_EMPTY)
1608 /* Response queue is not empty. */
1609 rq = TWA_READ_RESPONSE_QUEUE(sc);
1610 tr = sc->twa_lookup[rq.u.response_id]; /* lookup the request */
1611 if (tr->tr_status != TWA_CMD_BUSY)
1612 twa_printf(sc, "ERROR: Unposted command completed!! req = %p; status = %d\n",
1614 tr->tr_status = TWA_CMD_COMPLETE;
1615 /* Enqueue request in the complete queue. */
1616 twa_remove_busy(tr);
1617 twa_enqueue_complete(tr);
1621 /* Complete this, and other requests in the complete queue. */
1622 twa_drain_complete_queue(sc);
1629 * Function name: twa_drain_pending_queue
1630 * Description: Kick starts any requests in the pending queue.
1632 * Input: sc -- ptr to per ctlr structure
1634 * Return value: 0 -- all pending requests drained
1635 * non-zero-- otherwise
1638 twa_drain_pending_queue(struct twa_softc *sc)
1640 struct twa_request *tr;
1643 twa_dbg_dprint_enter(10, sc);
1646 * Pull requests off the pending queue, and submit them.
1648 while ((tr = twa_dequeue_pending(sc)) != NULL) {
1649 if ((error = twa_start(tr))) {
1650 if (error == EBUSY) {
1651 twa_dbg_dprint(2, sc, "Requeueing pending request");
1652 tr->tr_status = TWA_CMD_PENDING;
1653 twa_requeue_pending(tr);/* queue at the head */
1656 twa_printf(sc, "%s: twa_start returned 0x%x\n",
1658 if (tr->tr_flags & TWA_CMD_SLEEP_ON_REQUEST)
1659 wakeup_one(tr);/* let the caller know it failed */
1660 tr->tr_error = error;
1671 * Function name: twa_drain_complete_queue
1672 * Description: Does unmapping for each request completed by fw,
1673 * and lets the request originators know of the completion.
1675 * Input: sc -- ptr to per ctlr structure
1677 * Return value: None
1680 twa_drain_complete_queue(struct twa_softc *sc)
1682 struct twa_request *tr;
1684 twa_dbg_dprint_enter(10, sc);
1687 * Pull commands off the completed list, dispatch them appropriately.
1689 while ((tr = twa_dequeue_complete(sc)) != NULL) {
1690 /* Unmap the command packet, and any associated data buffer. */
1691 twa_unmap_request(tr);
1693 /* Call the callback, if there's one. */
1694 if (tr->tr_callback)
1695 tr->tr_callback(tr);
1697 if (tr->tr_flags & TWA_CMD_SLEEP_ON_REQUEST) {
1698 /* Wake up the sleeping command originator. */
1699 twa_dbg_dprint(7, sc, "Waking up originator of request %p", tr);
1708 * Function name: twa_wait_status
1709 * Description: Wait for a given status to show up in the fw status register.
1711 * Input: sc -- ptr to per ctlr structure
1712 * status -- status to look for
1713 * timeout -- max # of seconds to wait before giving up
1715 * Return value: 0 -- success
1716 * non-zero-- failure
1719 twa_wait_status(struct twa_softc *sc, u_int32_t status, u_int32_t timeout)
1722 u_int32_t status_reg;
1724 twa_dbg_dprint_enter(4, sc);
1726 end_time = time_second + timeout;
1728 status_reg = TWA_READ_STATUS_REGISTER(sc);
1729 if ((status_reg & status) == status)/* got the required bit(s)? */
1732 } while (time_second <= end_time);
1740 * Function name: twa_drain_response_queue
1741 * Description: Drain the response queue.
1743 * Input: sc -- ptr to per ctlr structure
1745 * Return value: 0 -- success
1746 * non-zero-- failure
1749 twa_drain_response_queue(struct twa_softc *sc)
1751 union twa_response_queue rq;
1752 u_int32_t status_reg;
1754 twa_dbg_dprint_enter(4, sc);
1757 status_reg = TWA_READ_STATUS_REGISTER(sc);
1758 if (twa_check_ctlr_state(sc, status_reg))
1760 if (status_reg & TWA_STATUS_RESPONSE_QUEUE_EMPTY)
1761 return(0); /* no more response queue entries */
1762 rq = TWA_READ_RESPONSE_QUEUE(sc);
1769 * Function name: twa_host_intr
1770 * Description: This function gets called if we triggered an interrupt.
1771 * We don't use it as of now.
1773 * Input: sc -- ptr to per ctlr structure
1775 * Return value: None
1778 twa_host_intr(struct twa_softc *sc)
1780 twa_dbg_dprint_enter(6, sc);
1782 TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_HOST_INTERRUPT);
1788 * Function name: twa_attention_intr
1789 * Description: This function gets called if the fw posted an AEN
1790 * (Asynchronous Event Notification). It fetches
1791 * all the AEN's that the fw might have posted.
1793 * Input: sc -- ptr to per ctlr structure
1795 * Return value: None
1798 twa_attention_intr(struct twa_softc *sc)
1802 twa_dbg_dprint_enter(6, sc);
1804 if ((error = twa_fetch_aen(sc)))
1805 twa_printf(sc, "Fetch AEN failed. error = 0x%x\n", error);
1806 TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT);
1812 * Function name: twa_command_intr
1813 * Description: This function gets called if we hit a queue full
1814 * condition earlier, and the fw is now ready for
1815 * new cmds. Submits any pending requests.
1817 * Input: sc -- ptr to per ctlr structure
1819 * Return value: None
1822 twa_command_intr(struct twa_softc *sc)
1824 twa_dbg_dprint_enter(6, sc);
1826 /* Start any requests that might be in the pending queue. */
1827 if (! twa_drain_pending_queue(sc))
1828 TWA_WRITE_CONTROL_REGISTER(sc,
1829 TWA_CONTROL_MASK_COMMAND_INTERRUPT);
1835 * Function name: twa_fetch_aen
1836 * Description: Send down a Request Sense cmd to fw to fetch an AEN.
1838 * Input: sc -- ptr to per ctlr structure
1840 * Return value: 0 -- success
1841 * non-zero-- failure
1844 twa_fetch_aen(struct twa_softc *sc)
1846 struct twa_request *tr;
1849 twa_dbg_dprint_enter(4, sc);
1851 if ((tr = twa_get_request(sc)) == NULL)
1853 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
1854 tr->tr_callback = twa_aen_callback;
1855 if ((error = twa_send_scsi_cmd(tr, 0x03 /* REQUEST_SENSE */))) {
1857 free(tr->tr_data, M_DEVBUF);
1858 twa_release_request(tr);
1866 * Function name: twa_aen_callback
1867 * Description: Callback for requests to fetch AEN's.
1869 * Input: tr -- ptr to completed request pkt
1871 * Return value: None
1874 twa_aen_callback(struct twa_request *tr)
1876 struct twa_softc *sc = tr->tr_sc;
1877 struct twa_command_header *cmd_hdr = (struct twa_command_header *)(tr->tr_data);
1878 struct twa_command_9k *cmd = &(tr->tr_command->command.cmd_pkt_9k);
1881 twa_dbg_dprint_enter(4, sc);
1883 twa_dbg_dprint(4, sc, "req_id = 0x%x, status = 0x%x",
1887 if (! cmd->status) {
1888 if ((tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_9K) &&
1889 (cmd->cdb[0] == 0x3 /* REQUEST_SENSE */))
1890 twa_enqueue_aen(sc, cmd_hdr);
1892 cmd_hdr->err_specific_desc[sizeof(cmd_hdr->err_specific_desc) - 1] = '\0';
1893 twa_printf(sc, "%s: cmd = 0x%x: ERROR: (0x%02X: 0x%04X): %s: %s\n",
1894 __func__, cmd->command.opcode,
1895 TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
1896 cmd_hdr->status_block.error,
1897 twa_find_msg_string(twa_error_table,
1898 cmd_hdr->status_block.error),
1899 cmd_hdr->err_specific_desc);
1900 twa_dbg_print(2, "sense info: ");
1901 for (i = 0; i < 18; i++)
1902 twa_dbg_print(2, "%x\t", tr->tr_command->cmd_hdr.sense_data[i]);
1903 twa_dbg_print(2, ""); /* print new line */
1904 for (i = 0; i < 128; i++)
1905 twa_dbg_print(7, "%x\t", ((int8_t *)(tr->tr_data))[i]);
1909 free(tr->tr_data, M_DEVBUF);
1910 twa_release_request(tr);
1916 * Function name: twa_drain_aen_queue
1917 * Description: Fetches all un-retrieved AEN's posted by fw.
1919 * Input: sc -- ptr to per ctlr structure
1921 * Return value: 0 -- success
1922 * non-zero-- failure
1925 twa_drain_aen_queue(struct twa_softc *sc)
1927 struct twa_request *tr;
1928 struct twa_command_header *cmd_hdr;
1933 if ((tr = twa_get_request(sc)) == NULL) {
1937 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
1938 tr->tr_callback = NULL;
1939 if ((error = twa_send_scsi_cmd(tr, 0x03 /* REQUEST_SENSE */))) {
1940 twa_dbg_dprint(1, sc, "Cannot send command to fetch aen");
1944 end_time = time_second + TWA_REQUEST_TIMEOUT_PERIOD;
1946 twa_done(tr->tr_sc);
1947 if (tr->tr_status != TWA_CMD_BUSY)
1949 } while (time_second <= end_time);
1951 if (tr->tr_status != TWA_CMD_COMPLETE) {
1956 if ((error = tr->tr_command->command.cmd_pkt_9k.status))
1959 cmd_hdr = (struct twa_command_header *)(tr->tr_data);
1960 if ((cmd_hdr->status_block.error) /* aen_code */
1961 == TWA_AEN_QUEUE_EMPTY)
1963 twa_enqueue_aen(sc, cmd_hdr);
1965 free(tr->tr_data, M_DEVBUF);
1966 twa_release_request(tr);
1971 free(tr->tr_data, M_DEVBUF);
1972 twa_release_request(tr);
1980 * Function name: twa_enqueue_aen
1981 * Description: Queues AEN's to be supplied to user-space tools on request.
1983 * Input: sc -- ptr to per ctlr structure
1984 * cmd_hdr -- ptr to hdr of fw cmd pkt, from where the AEN
1985 * details can be retrieved.
1987 * Return value: None
1990 twa_enqueue_aen(struct twa_softc *sc, struct twa_command_header *cmd_hdr)
1992 struct twa_event_packet *event;
1993 unsigned short aen_code;
1994 unsigned long local_time;
1995 unsigned long sync_time;
1997 twa_dbg_dprint_enter(4, sc);
1999 aen_code = cmd_hdr->status_block.error;
2002 case TWA_AEN_SYNC_TIME_WITH_HOST:
2003 twa_dbg_dprint(4, sc, "Received AEN_SYNC_TIME");
2004 /* Calculate time (in seconds) since last Sunday 12.00 AM. */
2005 local_time = time_second - (tz.tz_minuteswest * 60) -
2006 (wall_cmos_clock ? adjkerntz : 0);
2007 sync_time = (local_time - (3 * 86400)) % 604800;
2008 if (twa_set_param(sc, TWA_PARAM_TIME_TABLE,
2009 TWA_PARAM_TIME_SchedulerTime, 4,
2010 &sync_time, twa_aen_callback))
2011 twa_printf(sc, "Unable to sync time with ctlr!\n");
2014 case TWA_AEN_QUEUE_EMPTY:
2015 twa_dbg_dprint(4, sc, "AEN queue empty");
2019 /* Queue the event. */
2020 event = sc->twa_aen_queue[sc->twa_aen_head];
2021 if (event->retrieved == TWA_AEN_NOT_RETRIEVED)
2022 sc->twa_aen_queue_overflow = TRUE;
2023 event->severity = cmd_hdr->status_block.substatus_block.severity;
2024 local_time = time_second - (tz.tz_minuteswest * 60) -
2025 (wall_cmos_clock ? adjkerntz : 0);
2026 event->time_stamp_sec = local_time;
2027 event->aen_code = aen_code;
2028 event->retrieved = TWA_AEN_NOT_RETRIEVED;
2029 event->sequence_id = ++(sc->twa_current_sequence_id);
2030 cmd_hdr->err_specific_desc[sizeof(cmd_hdr->err_specific_desc) - 1] = '\0';
2031 event->parameter_len = strlen(cmd_hdr->err_specific_desc);
2032 bcopy(cmd_hdr->err_specific_desc, event->parameter_data,
2033 event->parameter_len);
2035 twa_dbg_dprint(4, sc, "event = %x %x %x %x %x %x %x\n %s",
2037 event->time_stamp_sec,
2041 event->repeat_count,
2042 event->parameter_len,
2043 event->parameter_data);
2045 twa_dbg_dprint(4, sc, "cmd_hdr = %x %lx %x %x %x %x %x\n %s",
2046 sc->twa_current_sequence_id,
2048 cmd_hdr->status_block.error,
2049 cmd_hdr->status_block.substatus_block.severity,
2050 TWA_AEN_NOT_RETRIEVED,
2052 strlen(cmd_hdr->err_specific_desc),
2053 cmd_hdr->err_specific_desc);
2055 /* Print the event. */
2056 if (event->severity < TWA_AEN_SEVERITY_DEBUG)
2057 twa_printf(sc, "%s: (0x%02X: 0x%04X): %s: %s\n",
2058 twa_aen_severity_table[event->severity],
2059 TWA_MESSAGE_SOURCE_CONTROLLER_EVENT,
2061 twa_find_msg_string(twa_aen_table, aen_code),
2062 event->parameter_data);
2064 if ((sc->twa_aen_head + 1) == TWA_Q_LENGTH)
2065 sc->twa_aen_queue_wrapped = TRUE;
2066 sc->twa_aen_head = (sc->twa_aen_head + 1) % TWA_Q_LENGTH;
2075 * Function name: twa_find_aen
2076 * Description: Reports whether a given AEN ever occurred.
2078 * Input: sc -- ptr to per ctlr structure
2079 * aen_code-- AEN to look for
2081 * Return value: 0 -- success
2082 * non-zero-- failure
2085 twa_find_aen(struct twa_softc *sc, u_int16_t aen_code)
2087 u_int32_t last_index;
2092 if (sc->twa_aen_queue_wrapped)
2093 last_index = sc->twa_aen_head;
2097 i = sc->twa_aen_head;
2099 i = (i + TWA_Q_LENGTH - 1) % TWA_Q_LENGTH;
2100 if ((sc->twa_aen_queue[i])->aen_code == aen_code) {
2104 } while (i != last_index);
2113 * Function name: twa_find_msg_string
2114 * Description: Looks up a given table, and returns the message string
2115 * corresponding to a given code (error code or AEN code).
2117 * Input: sc -- ptr to per ctlr structure
2118 * code -- code, the message string corresponding to
2119 * which is to be returned.
2121 * Return value: ptr to corresponding msg string -- success
2125 twa_find_msg_string(struct twa_message *table, u_int16_t code)
2129 for (i = 0; table[i].message != NULL; i++)
2130 if (table[i].code == code)
2131 return(table[i].message);
2133 return(table[i].message);
2139 * Function name: twa_get_request
2140 * Description: Gets a request pkt from the free queue.
2142 * Input: sc -- ptr to per ctlr structure
2144 * Return value: ptr to request pkt -- success
2147 struct twa_request *
2148 twa_get_request(struct twa_softc *sc)
2150 struct twa_request *tr;
2152 twa_dbg_dprint_enter(4, sc);
2154 /* Get a free request packet. */
2155 tr = twa_dequeue_free(sc);
2157 /* Initialize some fields to their defaults. */
2160 tr->tr_real_data = NULL;
2162 tr->tr_real_length = 0;
2163 tr->tr_status = TWA_CMD_SETUP;/* command is in setup phase */
2166 tr->tr_private = NULL;
2167 tr->tr_callback = NULL;
2168 tr->tr_cmd_pkt_type = 0;
2171 * Look at the status field in the command packet to see how
2172 * it completed the last time it was used, and zero out only
2173 * the portions that might have changed. Note that we don't
2174 * care to zero out the sglist.
2176 if (tr->tr_command->command.cmd_pkt_9k.status)
2177 bzero(tr->tr_command,
2178 sizeof(struct twa_command_header) + 28 /* max bytes before sglist */);
2180 bzero(&(tr->tr_command->command), 28 /* max bytes before sglist */);
2188 * Function name: twa_release_request
2189 * Description: Puts a request pkt into the free queue.
2191 * Input: tr -- ptr to request pkt to be freed
2193 * Return value: None
2196 twa_release_request(struct twa_request *tr)
2198 twa_dbg_dprint_enter(4, tr->tr_sc);
2200 twa_enqueue_free(tr);
2206 * Function name: twa_describe_controller
2207 * Description: Describes the controller, in terms of its fw version,
2210 * Input: sc -- ptr to per ctlr structure
2212 * Return value: None
2215 twa_describe_controller(struct twa_softc *sc)
2217 struct twa_param_9k *p[6];
2218 u_int8_t num_ports = 0;
2220 twa_dbg_dprint_enter(2, sc);
2222 /* Get the port count. */
2223 p[0] = twa_get_param(sc, TWA_PARAM_CONTROLLER_TABLE,
2224 TWA_PARAM_CONTROLLER_PORT_COUNT, 1, NULL);
2226 num_ports = *(u_int8_t *)(p[0]->data);
2227 free(p[0], M_DEVBUF);
2230 /* Get the firmware and BIOS versions. */
2231 p[0] = twa_get_param(sc, TWA_PARAM_VERSION_TABLE,
2232 TWA_PARAM_VERSION_FW, 16, NULL);
2233 p[1] = twa_get_param(sc, TWA_PARAM_VERSION_TABLE,
2234 TWA_PARAM_VERSION_BIOS, 16, NULL);
2236 twa_printf(sc, "%d ports, Firmware %.16s, BIOS %.16s\n",
2237 num_ports, p[0]?(p[0]->data):NULL, p[1]?(p[1]->data):NULL);
2239 /* Get more versions. */
2240 p[2] = twa_get_param(sc, TWA_PARAM_VERSION_TABLE,
2241 TWA_PARAM_VERSION_MONITOR, 16, NULL);
2242 p[3] = twa_get_param(sc, TWA_PARAM_VERSION_TABLE,
2243 TWA_PARAM_VERSION_PCBA, 8, NULL);
2244 p[4] = twa_get_param(sc, TWA_PARAM_VERSION_TABLE,
2245 TWA_PARAM_VERSION_ATA, 8, NULL);
2246 p[5] = twa_get_param(sc, TWA_PARAM_VERSION_TABLE,
2247 TWA_PARAM_VERSION_PCI, 8, NULL);
2249 twa_printf(sc, "Monitor %.16s, PCB %.8s, Achip %.8s, Pchip %.8s\n",
2250 p[2]?(p[2]->data):NULL, p[3]?(p[3]->data):NULL,
2251 p[4]?(p[4]->data):NULL, p[5]?(p[5]->data):NULL);
2254 free(p[2], M_DEVBUF);
2256 free(p[3], M_DEVBUF);
2258 free(p[4], M_DEVBUF);
2260 free(p[5], M_DEVBUF);
2263 free(p[0], M_DEVBUF);
2265 free(p[1], M_DEVBUF);
2271 * Function name: twa_check_ctlr_state
2272 * Description: Makes sure that the fw status register reports a
2275 * Input: sc -- ptr to per ctlr structure
2276 * status_reg -- value in the status register
2278 * Return value: 0 -- no errors
2282 twa_check_ctlr_state(struct twa_softc *sc, u_int32_t status_reg)
2285 static time_t last_warning[2] = {0, 0};
2287 /* Check if the 'micro-controller ready' bit is not set. */
2288 if ((status_reg & TWA_STATUS_EXPECTED_BITS) !=
2289 TWA_STATUS_EXPECTED_BITS) {
2290 if (time_second > (last_warning[0] + 5)) {
2291 twa_printf(sc, "Missing expected status bit(s) %b\n",
2292 ~status_reg & TWA_STATUS_EXPECTED_BITS,
2293 TWA_STATUS_BITS_DESCRIPTION);
2294 last_warning[0] = time_second;
2299 /* Check if any error bits are set. */
2300 if ((status_reg & TWA_STATUS_UNEXPECTED_BITS) != 0) {
2301 if (time_second > (last_warning[1] + 5)) {
2302 twa_printf(sc, "Unexpected status bit(s) %b\n",
2303 status_reg & TWA_STATUS_UNEXPECTED_BITS,
2304 TWA_STATUS_BITS_DESCRIPTION);
2305 last_warning[1] = time_second;
2307 if (status_reg & TWA_STATUS_PCI_PARITY_ERROR_INTERRUPT) {
2308 twa_printf(sc, "PCI parity error: clearing... Re-seat/move/replace card.\n");
2309 TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_PARITY_ERROR);
2310 twa_write_pci_config(sc, TWA_PCI_CONFIG_CLEAR_PARITY_ERROR, 2);
2312 if (status_reg & TWA_STATUS_PCI_ABORT_INTERRUPT) {
2313 twa_printf(sc, "PCI abort: clearing...\n");
2314 TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_PCI_ABORT);
2315 twa_write_pci_config(sc, TWA_PCI_CONFIG_CLEAR_PCI_ABORT, 2);
2317 if (status_reg & TWA_STATUS_QUEUE_ERROR_INTERRUPT) {
2318 twa_printf(sc, "Controller queue error: clearing...\n");
2319 TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_PCI_ABORT);
2321 if (status_reg & TWA_STATUS_SBUF_WRITE_ERROR) {
2322 twa_printf(sc, "SBUF write error: clearing...\n");
2323 TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_SBUF_WRITE_ERROR);
2325 if (status_reg & TWA_STATUS_MICROCONTROLLER_ERROR) {
2326 twa_printf(sc, "Micro-controller error!\n");
2336 * Function name: twa_print_controller
2337 * Description: Prints the current status of the controller.
2339 * Input: sc -- ptr to per ctlr structure
2341 * Return value: None
2344 twa_print_controller(struct twa_softc *sc)
2346 u_int32_t status_reg;
2348 /* Print current controller details. */
2349 status_reg = TWA_READ_STATUS_REGISTER(sc);
2350 twa_printf(sc, "status %b\n", status_reg, TWA_STATUS_BITS_DESCRIPTION);
2352 twa_printf(sc, "q type current max\n");
2353 twa_printf(sc, "free %04d %04d\n",
2354 sc->twa_qstats[TWAQ_FREE].q_length, sc->twa_qstats[TWAQ_FREE].q_max);
2355 twa_printf(sc, "busy %04d %04d\n",
2356 sc->twa_qstats[TWAQ_BUSY].q_length, sc->twa_qstats[TWAQ_BUSY].q_max);
2357 twa_printf(sc, "pending %04d %04d\n",
2358 sc->twa_qstats[TWAQ_PENDING].q_length, sc->twa_qstats[TWAQ_PENDING].q_max);
2359 twa_printf(sc, "complete %04d %04d\n",
2360 sc->twa_qstats[TWAQ_COMPLETE].q_length, sc->twa_qstats[TWAQ_COMPLETE].q_max);
2361 #endif /* TWA_DEBUG */
2362 twa_printf(sc, "AEN queue head %d tail %d\n",
2363 sc->twa_aen_head, sc->twa_aen_tail);
2369 * Function name: twa_panic
2370 * Description: Called when something is seriously wrong with the ctlr.
2371 * Hits the debugger if the debugger is turned on, else
2374 * Input: sc -- ptr to per ctlr structure
2375 * reason -- string describing what went wrong
2377 * Return value: None
2380 twa_panic(struct twa_softc *sc, int8_t *reason)
2382 twa_print_controller(sc);
2386 twa_printf(sc, "twa_panic: RESETTING CONTROLLER...\n");