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.8 2008/01/06 16:55:51 swildner 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 aen_queue = kmalloc(sizeof(struct twa_event_packet) * TWA_Q_LENGTH,
114 M_DEVBUF, M_WAITOK | M_ZERO);
115 /* Initialize the aen queue. */
116 for (i = 0; i < TWA_Q_LENGTH; i++)
117 sc->twa_aen_queue[i] = &(aen_queue[i]);
120 * Disable interrupts from the card.
121 * Interrupts will be enabled back in twa_intrhook.
123 twa_disable_interrupts(sc);
125 /* Initialize the controller. */
126 if ((error = twa_init_ctlr(sc))) {
127 /* Soft reset the controller, and try one more time. */
128 twa_printf(sc, "Controller initialization failed. Retrying...\n");
129 if ((error = twa_soft_reset(sc)))
130 twa_printf(sc, "Controller soft reset failed.\n");
132 error = twa_init_ctlr(sc);
137 #ifdef TWA_FLASH_FIRMWARE
139 * Function name: twa_flash_firmware
140 * Description: Flashes bundled firmware image onto controller.
142 * Input: sc -- ptr to per ctlr structure
144 * Return value: 0 -- success
148 twa_flash_firmware(struct twa_softc *sc)
150 struct twa_request *tr;
151 struct twa_command_download_firmware *cmd;
152 u_int32_t fw_img_chunk_size;
153 u_int32_t this_chunk_size = 0;
154 u_int32_t remaining_img_size = 0;
158 if ((tr = twa_get_request(sc)) == NULL) {
159 /* No free request packets available. Can't proceed. */
163 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
164 /* Allocate sufficient memory to hold a chunk of the firmware image. */
165 fw_img_chunk_size = ((twa_fw_img_size/NUM_FW_IMAGE_CHUNKS) + 511) & ~511;
166 tr->tr_data = kmalloc(fw_img_chunk_size, M_DEVBUF, M_WAITOK);
167 remaining_img_size = twa_fw_img_size;
168 cmd = &(tr->tr_command->command.cmd_pkt_7k.download_fw);
170 for (i = 0; i < NUM_FW_IMAGE_CHUNKS; i++) {
171 /* Build a cmd pkt for downloading firmware. */
172 bzero(tr->tr_command, sizeof(struct twa_command_packet));
174 tr->tr_command->cmd_hdr.header_desc.size_header = 128;
176 cmd->opcode = TWA_OP_DOWNLOAD_FIRMWARE;
177 cmd->sgl_offset = 2;/* offset in dwords, to the beginning of sg list */
178 cmd->size = 2; /* this field will be updated at data map time */
179 cmd->request_id = tr->tr_request_id;
183 cmd->param = 8; /* prom image */
185 if (i != (NUM_FW_IMAGE_CHUNKS - 1))
186 this_chunk_size = fw_img_chunk_size;
187 else /* last chunk */
188 this_chunk_size = remaining_img_size;
190 remaining_img_size -= this_chunk_size;
191 bcopy(twa_fw_img + (i * fw_img_chunk_size),
192 tr->tr_data, this_chunk_size);
195 * The next line will effect only the last chunk.
197 tr->tr_length = (this_chunk_size + 511) & ~511;
199 tr->tr_flags |= TWA_CMD_DATA_OUT;
201 error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
203 twa_printf(sc, "Firmware flash request could not be posted. error = 0x%x\n",
205 if (error == ETIMEDOUT)
206 return(error); /* clean-up done by twa_immediate_request */
210 if (i != (NUM_FW_IMAGE_CHUNKS - 1)) {
211 if ((error = tr->tr_command->cmd_hdr.status_block.error) != TWA_ERROR_MORE_DATA) {
212 twa_printf(sc, "cmd = 0x%x: ERROR: (0x%02X: 0x%04X): %s: %s\n",
214 TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
216 twa_find_msg_string(twa_error_table, error),
217 tr->tr_command->cmd_hdr.err_specific_desc);
218 twa_printf(sc, "Firmware flash request failed. Intermediate error = 0x%x, i = %x\n",
220 /* Hard reset the controller, so that it doesn't wait for the remaining chunks. */
224 } else /* last chunk */
226 twa_printf(sc, "cmd = 0x%x: ERROR: (0x%02X: 0x%04X): %s: %s\n",
228 TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
229 tr->tr_command->cmd_hdr.status_block.error,
230 twa_find_msg_string(twa_error_table,
231 tr->tr_command->cmd_hdr.status_block.error),
232 tr->tr_command->cmd_hdr.err_specific_desc);
233 twa_printf(sc, "Firmware flash request failed. error = 0x%x\n", error);
234 /* Hard reset the controller, so that it doesn't wait for more chunks. */
240 kfree(tr->tr_data, M_DEVBUF);
243 twa_release_request(tr);
249 * Function name: twa_hard_reset
250 * Description: Hard reset the controller.
252 * Input: sc -- ptr to per ctlr structure
254 * Return value: 0 -- success
258 twa_hard_reset(struct twa_softc *sc)
260 struct twa_request *tr;
261 struct twa_command_reset_firmware *cmd;
264 if ((tr = twa_get_request(sc)) == NULL)
266 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
267 /* Build a cmd pkt for sending down the hard reset command. */
268 tr->tr_command->cmd_hdr.header_desc.size_header = 128;
270 cmd = &(tr->tr_command->command.cmd_pkt_7k.reset_fw);
271 cmd->opcode = TWA_OP_RESET_FIRMWARE;
272 cmd->size = 2; /* this field will be updated at data map time */
273 cmd->request_id = tr->tr_request_id;
277 cmd->param = 0; /* don't reload FPGA logic */
282 error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
284 twa_printf(sc, "Hard reset request could not be posted. error = 0x%x\n",
286 if (error == ETIMEDOUT)
287 return(error); /* clean-up done by twa_immediate_request */
290 if ((error = cmd->status)) {
291 twa_printf(sc, "cmd = 0x%x: ERROR: (0x%02X: 0x%04X): %s: %s\n",
293 TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
294 tr->tr_command->cmd_hdr.status_block.error,
295 twa_find_msg_string(twa_error_table,
296 tr->tr_command->cmd_hdr.status_block.error),
297 tr->tr_command->cmd_hdr.err_specific_desc);
298 twa_printf(sc, "Hard reset request failed. error = 0x%x\n", error);
303 twa_release_request(tr);
307 #endif /* TWA_FLASH_FIRMWARE */
310 * Function name: twa_init_ctlr
311 * Description: Establishes a logical connection with the controller.
312 * If bundled with firmware, determines whether or not
313 * to flash firmware, based on arch_id, fw SRL (Spec.
314 * Revision Level), branch & build #'s. Also determines
315 * whether or not the driver is compatible with the
316 * firmware on the controller, before proceeding to work
319 * Input: sc -- ptr to per ctlr structure
321 * Return value: 0 -- success
325 twa_init_ctlr(struct twa_softc *sc)
327 u_int16_t fw_on_ctlr_srl = 0;
328 u_int16_t fw_on_ctlr_arch_id = 0;
329 u_int16_t fw_on_ctlr_branch = 0;
330 u_int16_t fw_on_ctlr_build = 0;
331 u_int32_t init_connect_result = 0;
333 #ifdef TWA_FLASH_FIRMWARE
334 int8_t fw_flashed = FALSE;
335 int8_t fw_flash_failed = FALSE;
336 #endif /* TWA_FLASH_FIRMWARE */
338 twa_dbg_dprint_enter(3, sc);
340 /* Wait for the controller to become ready. */
341 if (twa_wait_status(sc, TWA_STATUS_MICROCONTROLLER_READY,
342 TWA_REQUEST_TIMEOUT_PERIOD)) {
343 twa_printf(sc, "Microcontroller not ready.\n");
346 /* Drain the response queue. */
347 if (twa_drain_response_queue(sc)) {
348 twa_printf(sc, "Can't drain response queue.\n");
351 /* Establish a logical connection with the controller. */
352 if ((error = twa_init_connection(sc, TWA_INIT_MESSAGE_CREDITS,
353 TWA_EXTENDED_INIT_CONNECT, TWA_CURRENT_FW_SRL,
354 TWA_9000_ARCH_ID, TWA_CURRENT_FW_BRANCH,
355 TWA_CURRENT_FW_BUILD, &fw_on_ctlr_srl,
356 &fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
357 &fw_on_ctlr_build, &init_connect_result))) {
358 twa_printf(sc, "Can't initialize connection in current mode.\n");
362 #ifdef TWA_FLASH_FIRMWARE
364 if ((init_connect_result & TWA_BUNDLED_FW_SAFE_TO_FLASH) &&
365 (init_connect_result & TWA_CTLR_FW_RECOMMENDS_FLASH)) {
367 * The bundled firmware is safe to flash, and the firmware
368 * on the controller recommends a flash. So, flash!
370 twa_printf(sc, "Flashing bundled firmware...\n");
371 if ((error = twa_flash_firmware(sc))) {
372 fw_flash_failed = TRUE;
373 twa_printf(sc, "Unable to flash bundled firmware.\n");
374 twa_printf(sc, "Will see if possible to work with firmware on controller...\n");
376 twa_printf(sc, "Successfully flashed bundled firmware.\n");
382 /* The firmware was flashed. Have the new image loaded */
383 error = twa_hard_reset(sc);
385 twa_printf(sc, "Could not reset controller after flash!\n");
386 else /* Go through initialization again. */
387 error = twa_init_ctlr(sc);
389 * If hard reset of controller failed, we need to return.
390 * Otherwise, the above recursive call to twa_init_ctlr will
391 * have completed the rest of the initialization (starting
392 * from twa_drain_aen_queue below). Don't do it again.
397 #endif /* TWA_FLASH_FIRMWARE */
400 * Either we are not bundled with a firmware image, or
401 * the bundled firmware is not safe to flash,
402 * or flash failed for some reason. See if we can at
403 * least work with the firmware on the controller in the
406 if (init_connect_result & TWA_CTLR_FW_COMPATIBLE) {
407 /* Yes, we can. Make note of the operating mode. */
408 sc->working_srl = TWA_CURRENT_FW_SRL;
409 sc->working_branch = TWA_CURRENT_FW_BRANCH;
410 sc->working_build = TWA_CURRENT_FW_BUILD;
413 * No, we can't. See if we can at least work with
414 * it in the base mode. We should never come here
415 * if firmware has just been flashed.
417 twa_printf(sc, "Driver/Firmware mismatch. Negotiating for base level...\n");
418 if ((error = twa_init_connection(sc, TWA_INIT_MESSAGE_CREDITS,
419 TWA_EXTENDED_INIT_CONNECT, TWA_BASE_FW_SRL,
420 TWA_9000_ARCH_ID, TWA_BASE_FW_BRANCH,
421 TWA_BASE_FW_BUILD, &fw_on_ctlr_srl,
422 &fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
423 &fw_on_ctlr_build, &init_connect_result))) {
424 twa_printf(sc, "Can't initialize connection in base mode.\n");
427 if (!(init_connect_result & TWA_CTLR_FW_COMPATIBLE)) {
429 * The firmware on the controller is not even
430 * compatible with our base mode. We cannot
431 * work with it. Bail...
433 twa_printf(sc, "Incompatible firmware on controller\n");
434 #ifdef TWA_FLASH_FIRMWARE
436 twa_printf(sc, "...and could not flash bundled firmware.\n");
438 twa_printf(sc, "...and bundled firmware not safe to flash.\n");
439 #endif /* TWA_FLASH_FIRMWARE */
442 /* We can work with this firmware, but only in base mode. */
443 sc->working_srl = TWA_BASE_FW_SRL;
444 sc->working_branch = TWA_BASE_FW_BRANCH;
445 sc->working_build = TWA_BASE_FW_BUILD;
446 sc->twa_operating_mode = TWA_BASE_MODE;
450 /* Drain the AEN queue */
451 if (twa_drain_aen_queue(sc)) {
453 * We will just print that we couldn't drain the AEN queue.
454 * There's no need to bail out.
456 twa_printf(sc, "Can't drain AEN queue.\n");
459 /* Set controller state to initialized. */
460 sc->twa_state &= ~TWA_STATE_SHUTDOWN;
462 twa_dbg_dprint_exit(3, sc);
468 * Function name: twa_deinit_ctlr
469 * Description: Close logical connection with the controller.
471 * Input: sc -- ptr to per ctlr structure
473 * Return value: 0 -- success
477 twa_deinit_ctlr(struct twa_softc *sc)
480 * Mark the controller as shutting down,
481 * and disable any further interrupts.
483 sc->twa_state |= TWA_STATE_SHUTDOWN;
484 twa_disable_interrupts(sc);
486 /* Let the controller know that we are going down. */
487 return(twa_init_connection(sc, TWA_SHUTDOWN_MESSAGE_CREDITS,
489 NULL, NULL, NULL, NULL, NULL));
494 * Function name: twa_interrupt
495 * Description: Interrupt handler. Determines the kind of interrupt,
496 * and calls the appropriate handler.
498 * Input: sc -- ptr to per ctlr structure
503 twa_interrupt(struct twa_softc *sc)
505 u_int32_t status_reg;
507 twa_dbg_dprint_enter(5, sc);
509 /* Collect current interrupt status. */
510 status_reg = TWA_READ_STATUS_REGISTER(sc);
511 if (twa_check_ctlr_state(sc, status_reg))
514 /* Dispatch based on the kind of interrupt. */
515 if (status_reg & TWA_STATUS_HOST_INTERRUPT)
517 if (status_reg & TWA_STATUS_ATTENTION_INTERRUPT)
518 twa_attention_intr(sc);
519 if (status_reg & TWA_STATUS_COMMAND_INTERRUPT)
520 twa_command_intr(sc);
521 if (status_reg & TWA_STATUS_RESPONSE_INTERRUPT)
527 * Function name: twa_ioctl
528 * Description: ioctl handler.
530 * Input: sc -- ptr to per ctlr structure
532 * buf -- ptr to buffer in kernel memory, which is
533 * a copy of the input buffer in user-space
534 * Output: buf -- ptr to buffer in kernel memory, which will
535 * be copied of the output buffer in user-space
536 * Return value: 0 -- success
540 twa_ioctl(struct twa_softc *sc, int cmd, void *buf)
542 struct twa_ioctl_9k *user_buf = (struct twa_ioctl_9k *)buf;
543 struct twa_event_packet event_buf;
549 case TWA_IOCTL_FIRMWARE_PASS_THROUGH:
551 struct twa_command_packet *cmdpkt;
552 struct twa_request *tr;
553 u_int32_t data_buf_size_adjusted;
556 twa_dbg_dprint(2, sc, "Firmware PassThru");
558 /* Get a request packet */
559 while ((tr = twa_get_request(sc)) == NULL)
561 * No free request packets available. Sleep until
562 * one becomes available.
564 tsleep(&(sc->twa_wait_timeout), 0, "twioctl", hz);
567 * Make sure that the data buffer sent to firmware is a
568 * 512 byte multiple in size.
570 data_buf_size_adjusted = (user_buf->twa_drvr_pkt.buffer_length + 511) & ~511;
571 if ((tr->tr_length = data_buf_size_adjusted)) {
572 tr->tr_data = kmalloc(data_buf_size_adjusted, M_DEVBUF,
574 /* Copy the payload. */
575 if ((error = copyin((void *) (user_buf->pdata),
576 (void *) (tr->tr_data),
577 user_buf->twa_drvr_pkt.buffer_length)) != 0) {
578 twa_printf (sc, "Could not copyin fw_passthru data_buf.\n");
579 goto fw_passthru_done;
581 tr->tr_flags |= TWA_CMD_DATA_IN | TWA_CMD_DATA_OUT;
583 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_IOCTL;
584 cmdpkt = tr->tr_command;
586 /* Copy the command packet. */
587 bcopy(&(user_buf->twa_cmd_pkt), cmdpkt,
588 sizeof(struct twa_command_packet));
589 cmdpkt->command.cmd_pkt_7k.generic.request_id = tr->tr_request_id;
591 twa_dbg_dprint(3, sc, "cmd_pkt_7k = %x %x %x %x %x %x %x",
592 cmdpkt->command.cmd_pkt_7k.generic.opcode,
593 cmdpkt->command.cmd_pkt_7k.generic.sgl_offset,
594 cmdpkt->command.cmd_pkt_7k.generic.size,
595 cmdpkt->command.cmd_pkt_7k.generic.request_id,
596 cmdpkt->command.cmd_pkt_7k.generic.unit,
597 cmdpkt->command.cmd_pkt_7k.generic.status,
598 cmdpkt->command.cmd_pkt_7k.generic.flags);
600 /* Send down the request, and wait for it to complete. */
601 if ((error = twa_wait_request(tr, TWA_REQUEST_TIMEOUT_PERIOD))) {
602 twa_printf(sc, "fw_passthru request failed. error = 0x%x\n", error);
603 if (error == ETIMEDOUT)
604 break; /* clean-up done by twa_wait_request */
605 goto fw_passthru_done;
608 /* Copy the command packet back into user space. */
609 bcopy(cmdpkt, &(user_buf->twa_cmd_pkt),
610 sizeof(struct twa_command_packet));
612 /* If there was a payload, copy it back too. */
614 error = copyout(tr->tr_data, user_buf->pdata,
615 user_buf->twa_drvr_pkt.buffer_length);
618 /* Free resources. */
620 kfree(tr->tr_data, M_DEVBUF);
622 twa_release_request(tr);
627 case TWA_IOCTL_SCAN_BUS:
628 /* Request CAM for a bus scan. */
629 twa_request_bus_scan(sc);
633 case TWA_IOCTL_GET_FIRST_EVENT:
634 twa_dbg_dprint(3, sc, "Get First Event");
636 if (sc->twa_aen_queue_wrapped) {
637 if (sc->twa_aen_queue_overflow) {
639 * The aen queue has wrapped, even before some
640 * events have been retrieved. Let the caller
641 * know that he missed out on some AEN's.
643 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_OVERFLOW;
644 sc->twa_aen_queue_overflow = FALSE;
646 user_buf->twa_drvr_pkt.status = 0;
647 event_index = sc->twa_aen_head;
649 if (sc->twa_aen_head == sc->twa_aen_tail) {
650 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
653 user_buf->twa_drvr_pkt.status = 0;
654 event_index = sc->twa_aen_tail; /* = 0 */
656 if ((error = copyout(sc->twa_aen_queue[event_index], user_buf->pdata,
657 sizeof(struct twa_event_packet))) != 0)
658 twa_printf(sc, "get_first: Could not copyout to event_buf. error = %x\n", error);
659 (sc->twa_aen_queue[event_index])->retrieved = TWA_AEN_RETRIEVED;
663 case TWA_IOCTL_GET_LAST_EVENT:
664 twa_dbg_dprint(3, sc, "Get Last Event");
666 if (sc->twa_aen_queue_wrapped) {
667 if (sc->twa_aen_queue_overflow) {
669 * The aen queue has wrapped, even before some
670 * events have been retrieved. Let the caller
671 * know that he missed out on some AEN's.
673 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_OVERFLOW;
674 sc->twa_aen_queue_overflow = FALSE;
676 user_buf->twa_drvr_pkt.status = 0;
678 if (sc->twa_aen_head == sc->twa_aen_tail) {
679 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
682 user_buf->twa_drvr_pkt.status = 0;
684 event_index = (sc->twa_aen_head - 1 + TWA_Q_LENGTH) % TWA_Q_LENGTH;
685 if ((error = copyout(sc->twa_aen_queue[event_index], user_buf->pdata,
686 sizeof(struct twa_event_packet))) != 0)
687 twa_printf(sc, "get_last: Could not copyout to event_buf. error = %x\n", error);
688 (sc->twa_aen_queue[event_index])->retrieved = TWA_AEN_RETRIEVED;
692 case TWA_IOCTL_GET_NEXT_EVENT:
693 twa_dbg_dprint(3, sc, "Get Next Event");
695 user_buf->twa_drvr_pkt.status = 0;
696 if (sc->twa_aen_queue_wrapped) {
697 twa_dbg_dprint(3, sc, "Get Next Event: wrapped");
698 if (sc->twa_aen_queue_overflow) {
700 * The aen queue has wrapped, even before some
701 * events have been retrieved. Let the caller
702 * know that he missed out on some AEN's.
704 twa_dbg_dprint(2, sc, "Get Next Event: overflow");
705 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_OVERFLOW;
706 sc->twa_aen_queue_overflow = FALSE;
708 start_index = sc->twa_aen_head;
710 if (sc->twa_aen_head == sc->twa_aen_tail) {
711 twa_dbg_dprint(3, sc, "Get Next Event: empty queue");
712 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
715 start_index = sc->twa_aen_tail; /* = 0 */
717 if ((error = copyin(user_buf->pdata, &event_buf,
718 sizeof(struct twa_event_packet))) != 0)
719 twa_printf(sc, "get_next: Could not copyin event_buf.\n");
721 event_index = (start_index + event_buf.sequence_id -
722 (sc->twa_aen_queue[start_index])->sequence_id + 1)
725 twa_dbg_dprint(3, sc, "Get Next Event: si = %x, ei = %x, ebsi = %x, sisi = %x, eisi = %x",
726 start_index, event_index, event_buf.sequence_id,
727 (sc->twa_aen_queue[start_index])->sequence_id,
728 (sc->twa_aen_queue[event_index])->sequence_id);
730 if (! ((sc->twa_aen_queue[event_index])->sequence_id >
731 event_buf.sequence_id)) {
732 if (user_buf->twa_drvr_pkt.status == TWA_ERROR_AEN_OVERFLOW)
733 sc->twa_aen_queue_overflow = TRUE; /* so we report the overflow next time */
734 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
737 if ((error = copyout(sc->twa_aen_queue[event_index], user_buf->pdata,
738 sizeof(struct twa_event_packet))) != 0)
739 twa_printf(sc, "get_next: Could not copyout to event_buf. error = %x\n", error);
741 (sc->twa_aen_queue[event_index])->retrieved = TWA_AEN_RETRIEVED;
745 case TWA_IOCTL_GET_PREVIOUS_EVENT:
746 twa_dbg_dprint(3, sc, "Get Previous Event");
748 user_buf->twa_drvr_pkt.status = 0;
749 if (sc->twa_aen_queue_wrapped) {
750 if (sc->twa_aen_queue_overflow) {
752 * The aen queue has wrapped, even before some
753 * events have been retrieved. Let the caller
754 * know that he missed out on some AEN's.
756 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_OVERFLOW;
757 sc->twa_aen_queue_overflow = FALSE;
759 start_index = sc->twa_aen_head;
761 if (sc->twa_aen_head == sc->twa_aen_tail) {
762 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
765 start_index = sc->twa_aen_tail; /* = 0 */
767 if ((error = copyin(user_buf->pdata, &event_buf,
768 sizeof(struct twa_event_packet))) != 0)
769 twa_printf(sc, "get_previous: Could not copyin event_buf.\n");
771 event_index = (start_index + event_buf.sequence_id -
772 (sc->twa_aen_queue[start_index])->sequence_id - 1) % TWA_Q_LENGTH;
773 if (! ((sc->twa_aen_queue[event_index])->sequence_id < event_buf.sequence_id)) {
774 if (user_buf->twa_drvr_pkt.status == TWA_ERROR_AEN_OVERFLOW)
775 sc->twa_aen_queue_overflow = TRUE; /* so we report the overflow next time */
776 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
779 if ((error = copyout(sc->twa_aen_queue [event_index], user_buf->pdata,
780 sizeof(struct twa_event_packet))) != 0)
781 twa_printf(sc, "get_previous: Could not copyout to event_buf. error = %x\n", error);
783 (sc->twa_aen_queue[event_index])->retrieved = TWA_AEN_RETRIEVED;
787 case TWA_IOCTL_GET_LOCK:
789 struct twa_lock_packet twa_lock;
792 cur_time = time_second - (tz.tz_minuteswest * 60) -
793 (wall_cmos_clock ? adjkerntz : 0);
794 copyin(user_buf->pdata, &twa_lock,
795 sizeof(struct twa_lock_packet));
797 if ((sc->twa_ioctl_lock.lock == TWA_LOCK_FREE) ||
798 (twa_lock.force_flag) ||
799 (cur_time >= sc->twa_ioctl_lock.timeout)) {
800 twa_dbg_dprint(3, sc, "GET_LOCK: Getting lock!");
801 sc->twa_ioctl_lock.lock = TWA_LOCK_HELD;
802 sc->twa_ioctl_lock.timeout = cur_time + (twa_lock.timeout_msec / 1000);
803 twa_lock.time_remaining_msec = twa_lock.timeout_msec;
804 user_buf->twa_drvr_pkt.status = 0;
806 twa_dbg_dprint(2, sc, "GET_LOCK: Lock already held!");
807 twa_lock.time_remaining_msec =
808 (sc->twa_ioctl_lock.timeout - cur_time) * 1000;
809 user_buf->twa_drvr_pkt.status =
810 TWA_ERROR_IOCTL_LOCK_ALREADY_HELD;
813 copyout(&twa_lock, user_buf->pdata,
814 sizeof(struct twa_lock_packet));
819 case TWA_IOCTL_RELEASE_LOCK:
821 if (sc->twa_ioctl_lock.lock == TWA_LOCK_FREE) {
822 twa_dbg_dprint(2, sc, "twa_ioctl: RELEASE_LOCK: Lock not held!");
823 user_buf->twa_drvr_pkt.status = TWA_ERROR_IOCTL_LOCK_NOT_HELD;
825 twa_dbg_dprint(3, sc, "RELEASE_LOCK: Releasing lock!");
826 sc->twa_ioctl_lock.lock = TWA_LOCK_FREE;
827 user_buf->twa_drvr_pkt.status = 0;
833 case TWA_IOCTL_GET_COMPATIBILITY_INFO:
835 struct twa_compatibility_packet comp_pkt;
837 bcopy(TWA_DRIVER_VERSION_STRING, comp_pkt.driver_version,
838 sizeof(TWA_DRIVER_VERSION_STRING));
839 comp_pkt.working_srl = sc->working_srl;
840 comp_pkt.working_branch = sc->working_branch;
841 comp_pkt.working_build = sc->working_build;
842 user_buf->twa_drvr_pkt.status = 0;
844 /* Copy compatibility information to user space. */
845 copyout(&comp_pkt, user_buf->pdata,
846 min(sizeof(struct twa_compatibility_packet),
847 user_buf->twa_drvr_pkt.buffer_length));
852 /* Unknown opcode. */
861 * Function name: twa_enable_interrupts
862 * Description: Enables interrupts on the controller
864 * Input: sc -- ptr to per ctlr structure
869 twa_enable_interrupts(struct twa_softc *sc)
871 sc->twa_state |= TWA_STATE_INTR_ENABLED;
872 TWA_WRITE_CONTROL_REGISTER(sc,
873 TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT |
874 TWA_CONTROL_UNMASK_RESPONSE_INTERRUPT |
875 TWA_CONTROL_ENABLE_INTERRUPTS);
880 * Function name: twa_setup
881 * Description: Disables interrupts on the controller
883 * Input: sc -- ptr to per ctlr structure
888 twa_disable_interrupts(struct twa_softc *sc)
890 TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_DISABLE_INTERRUPTS);
891 sc->twa_state &= ~TWA_STATE_INTR_ENABLED;
897 * Function name: twa_get_param
898 * Description: Get a firmware parameter.
900 * Input: sc -- ptr to per ctlr structure
901 * table_id -- parameter table #
902 * param_id -- index of the parameter in the table
903 * param_size -- size of the parameter in bytes
904 * callback -- ptr to function, if any, to be called
905 * back on completion; NULL if no callback.
907 * Return value: ptr to param structure -- success
911 twa_get_param(struct twa_softc *sc, int table_id, int param_id,
912 size_t param_size, void (* callback)(struct twa_request *tr))
914 struct twa_request *tr;
915 union twa_command_7k *cmd;
916 struct twa_param_9k *param = NULL;
919 twa_dbg_dprint_enter(4, sc);
921 /* Get a request packet. */
922 if ((tr = twa_get_request(sc)) == NULL)
924 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
926 /* Allocate memory to read data into. */
927 param = kmalloc(TWA_SECTOR_SIZE, M_DEVBUF, M_INTWAIT);
928 bzero(param, sizeof(struct twa_param_9k) - 1 + param_size);
930 tr->tr_length = TWA_SECTOR_SIZE;
931 tr->tr_flags = TWA_CMD_DATA_IN | TWA_CMD_DATA_OUT;
933 /* Build the cmd pkt. */
934 cmd = &(tr->tr_command->command.cmd_pkt_7k);
936 tr->tr_command->cmd_hdr.header_desc.size_header = 128;
938 cmd->param.opcode = TWA_OP_GET_PARAM;
939 cmd->param.sgl_offset = 2;
941 cmd->param.request_id = tr->tr_request_id;
943 cmd->param.param_count = 1;
945 /* Specify which parameter we need. */
946 param->table_id = table_id | TWA_9K_PARAM_DESCRIPTOR;
947 param->parameter_id = param_id;
948 param->parameter_size_bytes = param_size;
950 /* Submit the command. */
951 if (callback == NULL) {
952 /* There's no call back; wait till the command completes. */
953 error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
954 if (error == ETIMEDOUT)
955 return(NULL); /* clean-up done by twa_immediate_request */
958 if ((error = cmd->param.status)) {
959 twa_printf(sc, "cmd = 0x%x: ERROR: (0x%02X: 0x%04X): %s: %s\n",
961 TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
962 tr->tr_command->cmd_hdr.status_block.error,
963 twa_find_msg_string(twa_error_table,
964 tr->tr_command->cmd_hdr.status_block.error),
965 tr->tr_command->cmd_hdr.err_specific_desc);
966 goto out; /* twa_drain_complete_queue will have done the unmapping */
968 twa_release_request(tr);
971 /* There's a call back. Simply submit the command. */
972 tr->tr_callback = callback;
973 if ((error = twa_map_request(tr))) {
974 twa_printf(tr->tr_sc, "%s: twa_map_request returned 0x%x\n",
982 twa_printf(sc, "get_param failed. error = 0x%x\n", error);
984 kfree(param, M_DEVBUF);
986 twa_release_request(tr);
992 * Function name: twa_set_param
993 * Description: Set a firmware parameter.
995 * Input: sc -- ptr to per ctlr structure
996 * table_id -- parameter table #
997 * param_id -- index of the parameter in the table
998 * param_size -- size of the parameter in bytes
999 * callback -- ptr to function, if any, to be called
1000 * back on completion; NULL if no callback.
1002 * Return value: 0 -- success
1003 * non-zero-- failure
1006 twa_set_param(struct twa_softc *sc, int table_id,
1007 int param_id, int param_size, void *data,
1008 void (* callback)(struct twa_request *tr))
1010 struct twa_request *tr;
1011 union twa_command_7k *cmd;
1012 struct twa_param_9k *param = NULL;
1015 twa_dbg_dprint_enter(4, sc);
1017 /* Get a request packet. */
1018 if ((tr = twa_get_request(sc)) == NULL)
1020 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
1022 /* Allocate memory to send data using. */
1023 param = kmalloc(TWA_SECTOR_SIZE, M_DEVBUF, M_INTWAIT);
1024 bzero(param, sizeof(struct twa_param_9k) - 1 + param_size);
1025 tr->tr_data = param;
1026 tr->tr_length = TWA_SECTOR_SIZE;
1027 tr->tr_flags = TWA_CMD_DATA_IN | TWA_CMD_DATA_OUT;
1029 /* Build the cmd pkt. */
1030 cmd = &(tr->tr_command->command.cmd_pkt_7k);
1032 tr->tr_command->cmd_hdr.header_desc.size_header = 128;
1034 cmd->param.opcode = TWA_OP_SET_PARAM;
1035 cmd->param.sgl_offset = 2;
1036 cmd->param.size = 2;
1037 cmd->param.request_id = tr->tr_request_id;
1038 cmd->param.unit = 0;
1039 cmd->param.param_count = 1;
1041 /* Specify which parameter we want to set. */
1042 param->table_id = table_id | TWA_9K_PARAM_DESCRIPTOR;
1043 param->parameter_id = param_id;
1044 param->parameter_size_bytes = param_size;
1045 bcopy(data, param->data, param_size);
1047 /* Submit the command. */
1048 if (callback == NULL) {
1049 /* There's no call back; wait till the command completes. */
1050 error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
1051 if (error == ETIMEDOUT)
1052 return(error); /* clean-up done by twa_immediate_request */
1055 if ((error = cmd->param.status)) {
1056 twa_printf(sc, "cmd = 0x%x: ERROR: (0x%02X: 0x%04X): %s: %s\n",
1058 TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
1059 tr->tr_command->cmd_hdr.status_block.error,
1060 twa_find_msg_string(twa_error_table,
1061 tr->tr_command->cmd_hdr.status_block.error),
1062 tr->tr_command->cmd_hdr.err_specific_desc);
1063 goto out; /* twa_drain_complete_queue will have done the unmapping */
1065 kfree(param, M_DEVBUF);
1066 twa_release_request(tr);
1069 /* There's a call back. Simply submit the command. */
1070 tr->tr_callback = callback;
1071 if ((error = twa_map_request(tr))) {
1072 twa_printf(tr->tr_sc, "%s: twa_map_request returned 0x%x\n",
1080 twa_printf(sc, "set_param failed. error = 0x%x\n", error);
1082 kfree(param, M_DEVBUF);
1084 twa_release_request(tr);
1090 * Function name: twa_init_connection
1091 * Description: Send init_connection cmd to firmware
1093 * Input: sc -- ptr to per ctlr structure
1094 * message_credits -- max # of requests that we might send
1095 * down simultaneously. This will be
1096 * typically set to 256 at init-time or
1097 * after a reset, and to 1 at shutdown-time
1098 * set_features -- indicates if we intend to use 64-bit
1099 * sg, also indicates if we want to do a
1100 * basic or an extended init_connection;
1102 * Note: The following input/output parameters are valid, only in case of an
1103 * extended init_connection:
1105 * current_fw_srl -- srl of fw we are bundled
1106 * with, if any; 0 otherwise
1107 * current_fw_arch_id -- arch_id of fw we are bundled
1108 * with, if any; 0 otherwise
1109 * current_fw_branch -- branch # of fw we are bundled
1110 * with, if any; 0 otherwise
1111 * current_fw_build -- build # of fw we are bundled
1112 * with, if any; 0 otherwise
1113 * Output: fw_on_ctlr_srl -- srl of fw on ctlr
1114 * fw_on_ctlr_arch_id -- arch_id of fw on ctlr
1115 * fw_on_ctlr_branch -- branch # of fw on ctlr
1116 * fw_on_ctlr_build -- build # of fw on ctlr
1117 * init_connect_result -- result bitmap of fw response
1118 * Return value: 0 -- success
1119 * non-zero-- failure
1122 twa_init_connection(struct twa_softc *sc, u_int16_t message_credits,
1123 u_int32_t set_features, u_int16_t current_fw_srl,
1124 u_int16_t current_fw_arch_id, u_int16_t current_fw_branch,
1125 u_int16_t current_fw_build, u_int16_t *fw_on_ctlr_srl,
1126 u_int16_t *fw_on_ctlr_arch_id, u_int16_t *fw_on_ctlr_branch,
1127 u_int16_t *fw_on_ctlr_build, u_int32_t *init_connect_result)
1129 struct twa_request *tr;
1130 struct twa_command_init_connect *init_connect;
1133 twa_dbg_dprint_enter(3, sc);
1135 /* Get a request packet. */
1136 if ((tr = twa_get_request(sc)) == NULL)
1138 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
1139 /* Build the cmd pkt. */
1140 init_connect = &(tr->tr_command->command.cmd_pkt_7k.init_connect);
1142 tr->tr_command->cmd_hdr.header_desc.size_header = 128;
1144 init_connect->opcode = TWA_OP_INIT_CONNECTION;
1145 init_connect->request_id = tr->tr_request_id;
1146 init_connect->message_credits = message_credits;
1147 init_connect->features = set_features;
1148 if (TWA_64BIT_ADDRESSES)
1149 init_connect->features |= TWA_64BIT_SG_ADDRESSES;
1150 if (set_features & TWA_EXTENDED_INIT_CONNECT) {
1151 /* Fill in the extra fields needed for an extended init_connect. */
1152 init_connect->size = 6;
1153 init_connect->fw_srl = current_fw_srl;
1154 init_connect->fw_arch_id = current_fw_arch_id;
1155 init_connect->fw_branch = current_fw_branch;
1156 init_connect->fw_build = current_fw_build;
1158 init_connect->size = 3;
1160 /* Submit the command, and wait for it to complete. */
1161 error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
1162 if (error == ETIMEDOUT)
1163 return(error); /* clean-up done by twa_immediate_request */
1166 if ((error = init_connect->status)) {
1167 twa_printf(sc, "cmd = 0x%x: ERROR: (0x%02X: 0x%04X): %s: %s\n",
1168 init_connect->opcode,
1169 TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
1170 tr->tr_command->cmd_hdr.status_block.error,
1171 twa_find_msg_string(twa_error_table,
1172 tr->tr_command->cmd_hdr.status_block.error),
1173 tr->tr_command->cmd_hdr.err_specific_desc);
1174 goto out; /* twa_drain_complete_queue will have done the unmapping */
1176 if (set_features & TWA_EXTENDED_INIT_CONNECT) {
1177 *fw_on_ctlr_srl = init_connect->fw_srl;
1178 *fw_on_ctlr_arch_id = init_connect->fw_arch_id;
1179 *fw_on_ctlr_branch = init_connect->fw_branch;
1180 *fw_on_ctlr_build = init_connect->fw_build;
1181 *init_connect_result = init_connect->result;
1183 twa_release_request(tr);
1187 twa_printf(sc, "init_connection failed. error = 0x%x\n", error);
1189 twa_release_request(tr);
1196 * Function name: twa_wait_request
1197 * Description: Sends down a firmware cmd, and waits for the completion,
1198 * but NOT in a tight loop.
1200 * Input: tr -- ptr to request pkt
1201 * timeout -- max # of seconds to wait before giving up
1203 * Return value: 0 -- success
1204 * non-zero-- failure
1207 twa_wait_request(struct twa_request *tr, u_int32_t timeout)
1212 twa_dbg_dprint_enter(4, tr->tr_sc);
1214 tr->tr_flags |= TWA_CMD_SLEEP_ON_REQUEST;
1215 tr->tr_status = TWA_CMD_BUSY;
1216 if ((error = twa_map_request(tr))) {
1217 twa_printf(tr->tr_sc, "%s: twa_map_request returned 0x%x\n",
1222 end_time = time_second + timeout;
1223 while (tr->tr_status != TWA_CMD_COMPLETE) {
1224 if ((error = tr->tr_error))
1226 if ((error = tsleep(tr, 0, "twawait", timeout * hz)) == 0) {
1227 error = (tr->tr_status != TWA_CMD_COMPLETE);
1230 if (error == EWOULDBLOCK) {
1232 twa_printf(tr->tr_sc, "%s: Request %p timed out.\n",
1235 * We will reset the controller only if the request has
1236 * already been submitted, so as to not lose the
1237 * request packet. If a busy request timed out, the
1238 * reset will take care of freeing resources. If a
1239 * pending request timed out, we will free resources
1240 * for that request, right here. So, the caller is
1241 * expected to NOT cleanup when ETIMEDOUT is returned.
1243 if (tr->tr_status != TWA_CMD_PENDING)
1244 twa_reset(tr->tr_sc);
1246 /* Request was never submitted. Clean up. */
1247 twa_remove_pending(tr);
1248 twa_unmap_request(tr);
1250 kfree(tr->tr_data, M_DEVBUF);
1251 twa_release_request(tr);
1256 * Either the request got completed, or we were woken up by a
1257 * signal. Calculate the new timeout, in case it was the latter.
1259 timeout = (end_time - time_second);
1261 twa_unmap_request(tr);
1268 * Function name: twa_immediate_request
1269 * Description: Sends down a firmware cmd, and waits for the completion
1272 * Input: tr -- ptr to request pkt
1273 * timeout -- max # of seconds to wait before giving up
1275 * Return value: 0 -- success
1276 * non-zero-- failure
1279 twa_immediate_request(struct twa_request *tr, u_int32_t timeout)
1284 twa_dbg_dprint_enter(4, tr->tr_sc);
1286 if ((error = twa_map_request(tr))) {
1287 twa_printf(tr->tr_sc, "%s: twa_map_request returned 0x%x\n",
1292 end_time = time_second + timeout;
1294 if ((error = tr->tr_error))
1296 twa_done(tr->tr_sc);
1297 if ((tr->tr_status != TWA_CMD_BUSY) &&
1298 (tr->tr_status != TWA_CMD_PENDING)) {
1299 twa_unmap_request(tr);
1300 return(tr->tr_status != TWA_CMD_COMPLETE);
1302 } while (time_second <= end_time);
1305 twa_printf(tr->tr_sc, "%s: Request %p timed out.\n", __func__, tr);
1307 * We will reset the controller only if the request has
1308 * already been submitted, so as to not lose the
1309 * request packet. If a busy request timed out, the
1310 * reset will take care of freeing resources. If a
1311 * pending request timed out, we will free resources
1312 * for that request, right here. So, the caller is
1313 * expected to NOT cleanup when ETIMEDOUT is returned.
1315 if (tr->tr_status != TWA_CMD_PENDING)
1316 twa_reset(tr->tr_sc);
1318 /* Request was never submitted. Clean up. */
1319 twa_remove_pending(tr);
1320 twa_unmap_request(tr);
1322 kfree(tr->tr_data, M_DEVBUF);
1323 twa_release_request(tr);
1331 * Function name: twa_complete_io
1332 * Description: Callback on scsi requests to fw.
1334 * Input: tr -- ptr to request pkt
1336 * Return value: None
1339 twa_complete_io(struct twa_request *tr)
1341 struct twa_softc *sc = tr->tr_sc;
1343 twa_dbg_dprint_enter(8, sc);
1345 if (tr->tr_status != TWA_CMD_COMPLETE)
1346 twa_panic(sc, "twa_complete_io on incomplete command");
1347 if (tr->tr_private) /* This is a scsi cmd. Complete it. */
1348 twa_scsi_complete(tr);
1349 twa_release_request(tr);
1354 * Function name: twa_reset
1355 * Description: Soft resets and then initializes the controller;
1356 * drains any incomplete requests.
1358 * Input: sc -- ptr to per ctlr structure
1360 * Return value: 0 -- success
1361 * non-zero-- failure
1364 twa_reset(struct twa_softc *sc)
1368 twa_dbg_dprint_enter(2, sc);
1371 * Disable interrupts from the controller, and mask any
1372 * accidental entry into our interrupt handler.
1374 twa_disable_interrupts(sc);
1377 /* Soft reset the controller. */
1378 if ((error = twa_soft_reset(sc))) {
1379 twa_printf (sc, "Controller reset failed.\n");
1383 /* Re-establish logical connection with the controller. */
1384 if ((error = twa_init_connection(sc, TWA_INIT_MESSAGE_CREDITS,
1386 NULL, NULL, NULL, NULL, NULL))) {
1387 twa_printf(sc, "Can't initialize connection after reset.\n");
1391 twa_printf(sc, "Controller reset done!\n");
1394 * Complete all requests in the complete queue; error back all requests
1395 * in the busy queue. Any internal requests will be simply freed.
1396 * Re-submit any requests in the pending queue.
1398 twa_drain_complete_queue(sc);
1399 twa_drain_busy_queue(sc);
1404 * Enable interrupts, and also clear attention and response interrupts.
1406 twa_enable_interrupts(sc);
1413 * Function name: twa_soft_reset
1414 * Description: Does the actual soft reset.
1416 * Input: sc -- ptr to per ctlr structure
1418 * Return value: 0 -- success
1419 * non-zero-- failure
1422 twa_soft_reset(struct twa_softc *sc)
1424 u_int32_t status_reg;
1426 twa_dbg_dprint_enter(1, sc);
1428 twa_printf(sc, "Resetting controller...\n");
1431 if (twa_wait_status(sc, TWA_STATUS_MICROCONTROLLER_READY |
1432 TWA_STATUS_ATTENTION_INTERRUPT, 30)) {
1433 twa_printf(sc, "Micro-ctlr not ready/No attn intr after reset.\n");
1436 TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT);
1437 if (twa_drain_response_queue(sc)) {
1438 twa_printf(sc, "Can't drain response queue.\n");
1441 if (twa_drain_aen_queue(sc)) {
1442 twa_printf(sc, "Can't drain AEN queue.\n");
1445 if (twa_find_aen(sc, TWA_AEN_SOFT_RESET)) {
1446 twa_printf(sc, "Reset not reported by controller.\n");
1449 status_reg = TWA_READ_STATUS_REGISTER(sc);
1450 if (TWA_STATUS_ERRORS(status_reg) ||
1451 twa_check_ctlr_state(sc, status_reg)) {
1452 twa_printf(sc, "Controller errors detected.\n");
1461 * Function name: twa_submit_io
1462 * Description: Wrapper to twa_start.
1464 * Input: tr -- ptr to request pkt
1466 * Return value: 0 -- success
1467 * non-zero-- failure
1470 twa_submit_io(struct twa_request *tr)
1474 if ((error = twa_start(tr))) {
1475 if (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_EXTERNAL) {
1478 * Cmd queue is full. Freeze the simq to
1479 * maintain ccb ordering. The next ccb that
1480 * gets completed will unfreeze the simq.
1482 twa_disallow_new_requests(tr->tr_sc);
1484 /* It's a controller error. */
1485 twa_printf(tr->tr_sc, "SCSI cmd = 0x%x: ERROR: (0x%02X: 0x%04X)\n",
1486 tr->tr_command->command.cmd_pkt_9k.cdb[0],
1487 TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
1490 tr->tr_error = error;
1491 twa_scsi_complete(tr);
1494 error = 0; /* the request will be in the pending queue */
1496 twa_printf(tr->tr_sc, "cmd = 0x%x: ERROR: (0x%02X: 0x%04X)\n",
1497 (tr->tr_cmd_pkt_type == TWA_CMD_PKT_TYPE_9K) ?
1498 (tr->tr_command->command.cmd_pkt_9k.command.opcode) :
1499 (tr->tr_command->command.cmd_pkt_7k.generic.opcode),
1500 TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
1502 tr->tr_error = error;
1512 * Function name: twa_start
1513 * Description: Posts a cmd to firmware.
1515 * Input: tr -- ptr to request pkt
1517 * Return value: 0 -- success
1518 * non-zero-- failure
1521 twa_start(struct twa_request *tr)
1523 struct twa_softc *sc = tr->tr_sc;
1524 u_int32_t status_reg;
1527 twa_dbg_dprint_enter(10, sc);
1530 /* Check to see if we can post a command. */
1531 status_reg = TWA_READ_STATUS_REGISTER(sc);
1532 if ((error = twa_check_ctlr_state(sc, status_reg)))
1535 if (status_reg & TWA_STATUS_COMMAND_QUEUE_FULL) {
1536 if ((tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_INTERNAL) ||
1537 (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_IOCTL)) {
1538 if (tr->tr_status != TWA_CMD_PENDING) {
1539 twa_dbg_dprint(2, sc, "pending internal/ioctl request");
1540 tr->tr_status = TWA_CMD_PENDING;
1541 twa_enqueue_pending(tr);
1543 TWA_WRITE_CONTROL_REGISTER(sc,
1544 TWA_CONTROL_UNMASK_COMMAND_INTERRUPT);
1548 /* Cmd queue is not full. Post the command. */
1549 TWA_WRITE_COMMAND_QUEUE(sc,
1550 tr->tr_cmd_phys + sizeof(struct twa_command_header));
1551 /* Mark the request as currently being processed. */
1552 tr->tr_status = TWA_CMD_BUSY;
1553 /* Move the request into the busy queue. */
1554 twa_enqueue_busy(tr);
1565 * Function name: twa_done
1566 * Description: Looks for cmd completions from fw; queues cmds completed
1567 * by fw into complete queue.
1569 * Input: sc -- ptr to per ctlr structure
1571 * Return value: 0 -- no ctlr error
1572 * non-zero-- ctlr error
1575 twa_done(struct twa_softc *sc)
1577 union twa_response_queue rq;
1578 struct twa_request *tr;
1580 u_int32_t status_reg;
1582 twa_dbg_dprint_enter(10, sc);
1586 status_reg = TWA_READ_STATUS_REGISTER(sc);
1587 if ((error = twa_check_ctlr_state(sc, status_reg)))
1589 if (status_reg & TWA_STATUS_RESPONSE_QUEUE_EMPTY)
1591 /* Response queue is not empty. */
1592 rq = TWA_READ_RESPONSE_QUEUE(sc);
1593 tr = sc->twa_lookup[rq.u.response_id]; /* lookup the request */
1594 if (tr->tr_status != TWA_CMD_BUSY)
1595 twa_printf(sc, "ERROR: Unposted command completed!! req = %p; status = %d\n",
1597 tr->tr_status = TWA_CMD_COMPLETE;
1598 /* Enqueue request in the complete queue. */
1599 twa_remove_busy(tr);
1600 twa_enqueue_complete(tr);
1604 /* Complete this, and other requests in the complete queue. */
1605 twa_drain_complete_queue(sc);
1612 * Function name: twa_drain_pending_queue
1613 * Description: Kick starts any requests in the pending queue.
1615 * Input: sc -- ptr to per ctlr structure
1617 * Return value: 0 -- all pending requests drained
1618 * non-zero-- otherwise
1621 twa_drain_pending_queue(struct twa_softc *sc)
1623 struct twa_request *tr;
1626 twa_dbg_dprint_enter(10, sc);
1629 * Pull requests off the pending queue, and submit them.
1631 while ((tr = twa_dequeue_pending(sc)) != NULL) {
1632 if ((error = twa_start(tr))) {
1633 if (error == EBUSY) {
1634 twa_dbg_dprint(2, sc, "Requeueing pending request");
1635 tr->tr_status = TWA_CMD_PENDING;
1636 twa_requeue_pending(tr);/* queue at the head */
1639 twa_printf(sc, "%s: twa_start returned 0x%x\n",
1641 if (tr->tr_flags & TWA_CMD_SLEEP_ON_REQUEST)
1642 wakeup_one(tr);/* let the caller know it failed */
1643 tr->tr_error = error;
1654 * Function name: twa_drain_complete_queue
1655 * Description: Does unmapping for each request completed by fw,
1656 * and lets the request originators know of the completion.
1658 * Input: sc -- ptr to per ctlr structure
1660 * Return value: None
1663 twa_drain_complete_queue(struct twa_softc *sc)
1665 struct twa_request *tr;
1667 twa_dbg_dprint_enter(10, sc);
1670 * Pull commands off the completed list, dispatch them appropriately.
1672 while ((tr = twa_dequeue_complete(sc)) != NULL) {
1673 /* Unmap the command packet, and any associated data buffer. */
1674 twa_unmap_request(tr);
1676 /* Call the callback, if there's one. */
1677 if (tr->tr_callback)
1678 tr->tr_callback(tr);
1680 if (tr->tr_flags & TWA_CMD_SLEEP_ON_REQUEST) {
1681 /* Wake up the sleeping command originator. */
1682 twa_dbg_dprint(7, sc, "Waking up originator of request %p", tr);
1691 * Function name: twa_wait_status
1692 * Description: Wait for a given status to show up in the fw status register.
1694 * Input: sc -- ptr to per ctlr structure
1695 * status -- status to look for
1696 * timeout -- max # of seconds to wait before giving up
1698 * Return value: 0 -- success
1699 * non-zero-- failure
1702 twa_wait_status(struct twa_softc *sc, u_int32_t status, u_int32_t timeout)
1705 u_int32_t status_reg;
1707 twa_dbg_dprint_enter(4, sc);
1709 end_time = time_second + timeout;
1711 status_reg = TWA_READ_STATUS_REGISTER(sc);
1712 if ((status_reg & status) == status)/* got the required bit(s)? */
1715 } while (time_second <= end_time);
1723 * Function name: twa_drain_response_queue
1724 * Description: Drain the response queue.
1726 * Input: sc -- ptr to per ctlr structure
1728 * Return value: 0 -- success
1729 * non-zero-- failure
1732 twa_drain_response_queue(struct twa_softc *sc)
1734 union twa_response_queue rq;
1735 u_int32_t status_reg;
1737 twa_dbg_dprint_enter(4, sc);
1740 status_reg = TWA_READ_STATUS_REGISTER(sc);
1741 if (twa_check_ctlr_state(sc, status_reg))
1743 if (status_reg & TWA_STATUS_RESPONSE_QUEUE_EMPTY)
1744 return(0); /* no more response queue entries */
1745 rq = TWA_READ_RESPONSE_QUEUE(sc);
1752 * Function name: twa_host_intr
1753 * Description: This function gets called if we triggered an interrupt.
1754 * We don't use it as of now.
1756 * Input: sc -- ptr to per ctlr structure
1758 * Return value: None
1761 twa_host_intr(struct twa_softc *sc)
1763 twa_dbg_dprint_enter(6, sc);
1765 TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_HOST_INTERRUPT);
1771 * Function name: twa_attention_intr
1772 * Description: This function gets called if the fw posted an AEN
1773 * (Asynchronous Event Notification). It fetches
1774 * all the AEN's that the fw might have posted.
1776 * Input: sc -- ptr to per ctlr structure
1778 * Return value: None
1781 twa_attention_intr(struct twa_softc *sc)
1785 twa_dbg_dprint_enter(6, sc);
1787 if ((error = twa_fetch_aen(sc)))
1788 twa_printf(sc, "Fetch AEN failed. error = 0x%x\n", error);
1789 TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT);
1795 * Function name: twa_command_intr
1796 * Description: This function gets called if we hit a queue full
1797 * condition earlier, and the fw is now ready for
1798 * new cmds. Submits any pending requests.
1800 * Input: sc -- ptr to per ctlr structure
1802 * Return value: None
1805 twa_command_intr(struct twa_softc *sc)
1807 twa_dbg_dprint_enter(6, sc);
1809 /* Start any requests that might be in the pending queue. */
1810 if (! twa_drain_pending_queue(sc))
1811 TWA_WRITE_CONTROL_REGISTER(sc,
1812 TWA_CONTROL_MASK_COMMAND_INTERRUPT);
1818 * Function name: twa_fetch_aen
1819 * Description: Send down a Request Sense cmd to fw to fetch an AEN.
1821 * Input: sc -- ptr to per ctlr structure
1823 * Return value: 0 -- success
1824 * non-zero-- failure
1827 twa_fetch_aen(struct twa_softc *sc)
1829 struct twa_request *tr;
1832 twa_dbg_dprint_enter(4, sc);
1834 if ((tr = twa_get_request(sc)) == NULL)
1836 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
1837 tr->tr_callback = twa_aen_callback;
1838 if ((error = twa_send_scsi_cmd(tr, 0x03 /* REQUEST_SENSE */))) {
1840 kfree(tr->tr_data, M_DEVBUF);
1841 twa_release_request(tr);
1849 * Function name: twa_aen_callback
1850 * Description: Callback for requests to fetch AEN's.
1852 * Input: tr -- ptr to completed request pkt
1854 * Return value: None
1857 twa_aen_callback(struct twa_request *tr)
1859 struct twa_softc *sc = tr->tr_sc;
1860 struct twa_command_header *cmd_hdr = (struct twa_command_header *)(tr->tr_data);
1861 struct twa_command_9k *cmd = &(tr->tr_command->command.cmd_pkt_9k);
1864 twa_dbg_dprint_enter(4, sc);
1866 twa_dbg_dprint(4, sc, "req_id = 0x%x, status = 0x%x",
1870 if (! cmd->status) {
1871 if ((tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_9K) &&
1872 (cmd->cdb[0] == 0x3 /* REQUEST_SENSE */))
1873 twa_enqueue_aen(sc, cmd_hdr);
1875 cmd_hdr->err_specific_desc[sizeof(cmd_hdr->err_specific_desc) - 1] = '\0';
1876 twa_printf(sc, "%s: cmd = 0x%x: ERROR: (0x%02X: 0x%04X): %s: %s\n",
1877 __func__, cmd->command.opcode,
1878 TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
1879 cmd_hdr->status_block.error,
1880 twa_find_msg_string(twa_error_table,
1881 cmd_hdr->status_block.error),
1882 cmd_hdr->err_specific_desc);
1883 twa_dbg_print(2, "sense info: ");
1884 for (i = 0; i < 18; i++)
1885 twa_dbg_print(2, "%x\t", tr->tr_command->cmd_hdr.sense_data[i]);
1886 twa_dbg_print(2, ""); /* print new line */
1887 for (i = 0; i < 128; i++)
1888 twa_dbg_print(7, "%x\t", ((int8_t *)(tr->tr_data))[i]);
1892 kfree(tr->tr_data, M_DEVBUF);
1893 twa_release_request(tr);
1899 * Function name: twa_drain_aen_queue
1900 * Description: Fetches all un-retrieved AEN's posted by fw.
1902 * Input: sc -- ptr to per ctlr structure
1904 * Return value: 0 -- success
1905 * non-zero-- failure
1908 twa_drain_aen_queue(struct twa_softc *sc)
1910 struct twa_request *tr;
1911 struct twa_command_header *cmd_hdr;
1916 if ((tr = twa_get_request(sc)) == NULL) {
1920 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
1921 tr->tr_callback = NULL;
1922 if ((error = twa_send_scsi_cmd(tr, 0x03 /* REQUEST_SENSE */))) {
1923 twa_dbg_dprint(1, sc, "Cannot send command to fetch aen");
1927 end_time = time_second + TWA_REQUEST_TIMEOUT_PERIOD;
1929 twa_done(tr->tr_sc);
1930 if (tr->tr_status != TWA_CMD_BUSY)
1932 } while (time_second <= end_time);
1934 if (tr->tr_status != TWA_CMD_COMPLETE) {
1939 if ((error = tr->tr_command->command.cmd_pkt_9k.status))
1942 cmd_hdr = (struct twa_command_header *)(tr->tr_data);
1943 if ((cmd_hdr->status_block.error) /* aen_code */
1944 == TWA_AEN_QUEUE_EMPTY)
1946 twa_enqueue_aen(sc, cmd_hdr);
1948 kfree(tr->tr_data, M_DEVBUF);
1949 twa_release_request(tr);
1954 kfree(tr->tr_data, M_DEVBUF);
1955 twa_release_request(tr);
1963 * Function name: twa_enqueue_aen
1964 * Description: Queues AEN's to be supplied to user-space tools on request.
1966 * Input: sc -- ptr to per ctlr structure
1967 * cmd_hdr -- ptr to hdr of fw cmd pkt, from where the AEN
1968 * details can be retrieved.
1970 * Return value: None
1973 twa_enqueue_aen(struct twa_softc *sc, struct twa_command_header *cmd_hdr)
1975 struct twa_event_packet *event;
1976 unsigned short aen_code;
1977 unsigned long local_time;
1978 unsigned long sync_time;
1980 twa_dbg_dprint_enter(4, sc);
1982 aen_code = cmd_hdr->status_block.error;
1985 case TWA_AEN_SYNC_TIME_WITH_HOST:
1986 twa_dbg_dprint(4, sc, "Received AEN_SYNC_TIME");
1987 /* Calculate time (in seconds) since last Sunday 12.00 AM. */
1988 local_time = time_second - (tz.tz_minuteswest * 60) -
1989 (wall_cmos_clock ? adjkerntz : 0);
1990 sync_time = (local_time - (3 * 86400)) % 604800;
1991 if (twa_set_param(sc, TWA_PARAM_TIME_TABLE,
1992 TWA_PARAM_TIME_SchedulerTime, 4,
1993 &sync_time, twa_aen_callback))
1994 twa_printf(sc, "Unable to sync time with ctlr!\n");
1997 case TWA_AEN_QUEUE_EMPTY:
1998 twa_dbg_dprint(4, sc, "AEN queue empty");
2002 /* Queue the event. */
2003 event = sc->twa_aen_queue[sc->twa_aen_head];
2004 if (event->retrieved == TWA_AEN_NOT_RETRIEVED)
2005 sc->twa_aen_queue_overflow = TRUE;
2006 event->severity = cmd_hdr->status_block.substatus_block.severity;
2007 local_time = time_second - (tz.tz_minuteswest * 60) -
2008 (wall_cmos_clock ? adjkerntz : 0);
2009 event->time_stamp_sec = local_time;
2010 event->aen_code = aen_code;
2011 event->retrieved = TWA_AEN_NOT_RETRIEVED;
2012 event->sequence_id = ++(sc->twa_current_sequence_id);
2013 cmd_hdr->err_specific_desc[sizeof(cmd_hdr->err_specific_desc) - 1] = '\0';
2014 event->parameter_len = strlen(cmd_hdr->err_specific_desc);
2015 bcopy(cmd_hdr->err_specific_desc, event->parameter_data,
2016 event->parameter_len);
2018 twa_dbg_dprint(4, sc, "event = %x %x %x %x %x %x %x\n %s",
2020 event->time_stamp_sec,
2024 event->repeat_count,
2025 event->parameter_len,
2026 event->parameter_data);
2028 twa_dbg_dprint(4, sc, "cmd_hdr = %x %lx %x %x %x %x %x\n %s",
2029 sc->twa_current_sequence_id,
2031 cmd_hdr->status_block.error,
2032 cmd_hdr->status_block.substatus_block.severity,
2033 TWA_AEN_NOT_RETRIEVED,
2035 strlen(cmd_hdr->err_specific_desc),
2036 cmd_hdr->err_specific_desc);
2038 /* Print the event. */
2039 if (event->severity < TWA_AEN_SEVERITY_DEBUG)
2040 twa_printf(sc, "%s: (0x%02X: 0x%04X): %s: %s\n",
2041 twa_aen_severity_table[event->severity],
2042 TWA_MESSAGE_SOURCE_CONTROLLER_EVENT,
2044 twa_find_msg_string(twa_aen_table, aen_code),
2045 event->parameter_data);
2047 if ((sc->twa_aen_head + 1) == TWA_Q_LENGTH)
2048 sc->twa_aen_queue_wrapped = TRUE;
2049 sc->twa_aen_head = (sc->twa_aen_head + 1) % TWA_Q_LENGTH;
2058 * Function name: twa_find_aen
2059 * Description: Reports whether a given AEN ever occurred.
2061 * Input: sc -- ptr to per ctlr structure
2062 * aen_code-- AEN to look for
2064 * Return value: 0 -- success
2065 * non-zero-- failure
2068 twa_find_aen(struct twa_softc *sc, u_int16_t aen_code)
2070 u_int32_t last_index;
2075 if (sc->twa_aen_queue_wrapped)
2076 last_index = sc->twa_aen_head;
2080 i = sc->twa_aen_head;
2082 i = (i + TWA_Q_LENGTH - 1) % TWA_Q_LENGTH;
2083 if ((sc->twa_aen_queue[i])->aen_code == aen_code) {
2087 } while (i != last_index);
2096 * Function name: twa_find_msg_string
2097 * Description: Looks up a given table, and returns the message string
2098 * corresponding to a given code (error code or AEN code).
2100 * Input: sc -- ptr to per ctlr structure
2101 * code -- code, the message string corresponding to
2102 * which is to be returned.
2104 * Return value: ptr to corresponding msg string -- success
2108 twa_find_msg_string(struct twa_message *table, u_int16_t code)
2112 for (i = 0; table[i].message != NULL; i++)
2113 if (table[i].code == code)
2114 return(table[i].message);
2116 return(table[i].message);
2122 * Function name: twa_get_request
2123 * Description: Gets a request pkt from the free queue.
2125 * Input: sc -- ptr to per ctlr structure
2127 * Return value: ptr to request pkt -- success
2130 struct twa_request *
2131 twa_get_request(struct twa_softc *sc)
2133 struct twa_request *tr;
2135 twa_dbg_dprint_enter(4, sc);
2137 /* Get a free request packet. */
2138 tr = twa_dequeue_free(sc);
2140 /* Initialize some fields to their defaults. */
2143 tr->tr_real_data = NULL;
2145 tr->tr_real_length = 0;
2146 tr->tr_status = TWA_CMD_SETUP;/* command is in setup phase */
2149 tr->tr_private = NULL;
2150 tr->tr_callback = NULL;
2151 tr->tr_cmd_pkt_type = 0;
2154 * Look at the status field in the command packet to see how
2155 * it completed the last time it was used, and zero out only
2156 * the portions that might have changed. Note that we don't
2157 * care to zero out the sglist.
2159 if (tr->tr_command->command.cmd_pkt_9k.status)
2160 bzero(tr->tr_command,
2161 sizeof(struct twa_command_header) + 28 /* max bytes before sglist */);
2163 bzero(&(tr->tr_command->command), 28 /* max bytes before sglist */);
2171 * Function name: twa_release_request
2172 * Description: Puts a request pkt into the free queue.
2174 * Input: tr -- ptr to request pkt to be freed
2176 * Return value: None
2179 twa_release_request(struct twa_request *tr)
2181 twa_dbg_dprint_enter(4, tr->tr_sc);
2183 twa_enqueue_free(tr);
2189 * Function name: twa_describe_controller
2190 * Description: Describes the controller, in terms of its fw version,
2193 * Input: sc -- ptr to per ctlr structure
2195 * Return value: None
2198 twa_describe_controller(struct twa_softc *sc)
2200 struct twa_param_9k *p[6];
2201 u_int8_t num_ports = 0;
2203 twa_dbg_dprint_enter(2, sc);
2205 /* Get the port count. */
2206 p[0] = twa_get_param(sc, TWA_PARAM_CONTROLLER_TABLE,
2207 TWA_PARAM_CONTROLLER_PORT_COUNT, 1, NULL);
2209 num_ports = *(u_int8_t *)(p[0]->data);
2210 kfree(p[0], M_DEVBUF);
2213 /* Get the firmware and BIOS versions. */
2214 p[0] = twa_get_param(sc, TWA_PARAM_VERSION_TABLE,
2215 TWA_PARAM_VERSION_FW, 16, NULL);
2216 p[1] = twa_get_param(sc, TWA_PARAM_VERSION_TABLE,
2217 TWA_PARAM_VERSION_BIOS, 16, NULL);
2219 twa_printf(sc, "%d ports, Firmware %.16s, BIOS %.16s\n",
2220 num_ports, p[0]?(p[0]->data):NULL, p[1]?(p[1]->data):NULL);
2222 /* Get more versions. */
2223 p[2] = twa_get_param(sc, TWA_PARAM_VERSION_TABLE,
2224 TWA_PARAM_VERSION_MONITOR, 16, NULL);
2225 p[3] = twa_get_param(sc, TWA_PARAM_VERSION_TABLE,
2226 TWA_PARAM_VERSION_PCBA, 8, NULL);
2227 p[4] = twa_get_param(sc, TWA_PARAM_VERSION_TABLE,
2228 TWA_PARAM_VERSION_ATA, 8, NULL);
2229 p[5] = twa_get_param(sc, TWA_PARAM_VERSION_TABLE,
2230 TWA_PARAM_VERSION_PCI, 8, NULL);
2232 twa_printf(sc, "Monitor %.16s, PCB %.8s, Achip %.8s, Pchip %.8s\n",
2233 p[2]?(p[2]->data):NULL, p[3]?(p[3]->data):NULL,
2234 p[4]?(p[4]->data):NULL, p[5]?(p[5]->data):NULL);
2237 kfree(p[2], M_DEVBUF);
2239 kfree(p[3], M_DEVBUF);
2241 kfree(p[4], M_DEVBUF);
2243 kfree(p[5], M_DEVBUF);
2246 kfree(p[0], M_DEVBUF);
2248 kfree(p[1], M_DEVBUF);
2254 * Function name: twa_check_ctlr_state
2255 * Description: Makes sure that the fw status register reports a
2258 * Input: sc -- ptr to per ctlr structure
2259 * status_reg -- value in the status register
2261 * Return value: 0 -- no errors
2265 twa_check_ctlr_state(struct twa_softc *sc, u_int32_t status_reg)
2268 static time_t last_warning[2] = {0, 0};
2270 /* Check if the 'micro-controller ready' bit is not set. */
2271 if ((status_reg & TWA_STATUS_EXPECTED_BITS) !=
2272 TWA_STATUS_EXPECTED_BITS) {
2273 if (time_second > (last_warning[0] + 5)) {
2274 twa_printf(sc, "Missing expected status bit(s) %b\n",
2275 ~status_reg & TWA_STATUS_EXPECTED_BITS,
2276 TWA_STATUS_BITS_DESCRIPTION);
2277 last_warning[0] = time_second;
2282 /* Check if any error bits are set. */
2283 if ((status_reg & TWA_STATUS_UNEXPECTED_BITS) != 0) {
2284 if (time_second > (last_warning[1] + 5)) {
2285 twa_printf(sc, "Unexpected status bit(s) %b\n",
2286 status_reg & TWA_STATUS_UNEXPECTED_BITS,
2287 TWA_STATUS_BITS_DESCRIPTION);
2288 last_warning[1] = time_second;
2290 if (status_reg & TWA_STATUS_PCI_PARITY_ERROR_INTERRUPT) {
2291 twa_printf(sc, "PCI parity error: clearing... Re-seat/move/replace card.\n");
2292 TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_PARITY_ERROR);
2293 twa_write_pci_config(sc, TWA_PCI_CONFIG_CLEAR_PARITY_ERROR, 2);
2295 if (status_reg & TWA_STATUS_PCI_ABORT_INTERRUPT) {
2296 twa_printf(sc, "PCI abort: clearing...\n");
2297 TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_PCI_ABORT);
2298 twa_write_pci_config(sc, TWA_PCI_CONFIG_CLEAR_PCI_ABORT, 2);
2300 if (status_reg & TWA_STATUS_QUEUE_ERROR_INTERRUPT) {
2301 twa_printf(sc, "Controller queue error: clearing...\n");
2302 TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_PCI_ABORT);
2304 if (status_reg & TWA_STATUS_SBUF_WRITE_ERROR) {
2305 twa_printf(sc, "SBUF write error: clearing...\n");
2306 TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_SBUF_WRITE_ERROR);
2308 if (status_reg & TWA_STATUS_MICROCONTROLLER_ERROR) {
2309 twa_printf(sc, "Micro-controller error!\n");
2319 * Function name: twa_print_controller
2320 * Description: Prints the current status of the controller.
2322 * Input: sc -- ptr to per ctlr structure
2324 * Return value: None
2327 twa_print_controller(struct twa_softc *sc)
2329 u_int32_t status_reg;
2331 /* Print current controller details. */
2332 status_reg = TWA_READ_STATUS_REGISTER(sc);
2333 twa_printf(sc, "status %b\n", status_reg, TWA_STATUS_BITS_DESCRIPTION);
2335 twa_printf(sc, "q type current max\n");
2336 twa_printf(sc, "free %04d %04d\n",
2337 sc->twa_qstats[TWAQ_FREE].q_length, sc->twa_qstats[TWAQ_FREE].q_max);
2338 twa_printf(sc, "busy %04d %04d\n",
2339 sc->twa_qstats[TWAQ_BUSY].q_length, sc->twa_qstats[TWAQ_BUSY].q_max);
2340 twa_printf(sc, "pending %04d %04d\n",
2341 sc->twa_qstats[TWAQ_PENDING].q_length, sc->twa_qstats[TWAQ_PENDING].q_max);
2342 twa_printf(sc, "complete %04d %04d\n",
2343 sc->twa_qstats[TWAQ_COMPLETE].q_length, sc->twa_qstats[TWAQ_COMPLETE].q_max);
2344 #endif /* TWA_DEBUG */
2345 twa_printf(sc, "AEN queue head %d tail %d\n",
2346 sc->twa_aen_head, sc->twa_aen_tail);
2352 * Function name: twa_panic
2353 * Description: Called when something is seriously wrong with the ctlr.
2354 * Hits the debugger if the debugger is turned on, else
2357 * Input: sc -- ptr to per ctlr structure
2358 * reason -- string describing what went wrong
2360 * Return value: None
2363 twa_panic(struct twa_softc *sc, int8_t *reason)
2365 twa_print_controller(sc);
2369 twa_printf(sc, "twa_panic: RESETTING CONTROLLER...\n");