Merge from vendor branch CVS:
[dragonfly.git] / sys / dev / raid / twa / twa.c
1 /*-
2  * Copyright (c) 2003-04 3ware, Inc.
3  * Copyright (c) 2000 Michael Smith
4  * Copyright (c) 2000 BSDi
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  *
28  *      $FreeBSD$
29  * $DragonFly: src/sys/dev/raid/twa/twa.c,v 1.2 2004/06/21 15:39:31 dillon Exp $
30  */
31
32 /*
33  * 3ware driver for 9000 series storage controllers.
34  *
35  * Author: Vinod Kashyap
36  */
37
38
39 #include "twa_includes.h"
40
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 */
45
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);
59
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);
62
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);
70
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);
74
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);
80
81 static void     twa_panic(struct twa_softc *sc, int8_t *reason);
82
83 /*
84  * Function name:       twa_setup
85  * Description:         Initializes driver data structures for the controller.
86  *
87  * Input:               sc      -- ptr to per ctlr structure
88  * Output:              None
89  * Return value:        0       -- success
90  *                      non-zero-- failure
91  */
92 int
93 twa_setup(struct twa_softc *sc)
94 {
95         struct twa_event_packet *aen_queue;
96         int                     error = 0;
97         int                     i;
98
99         twa_dbg_dprint_enter(3, sc);
100
101         /* Initialize request queues. */
102         twa_initq_free(sc);
103         twa_initq_busy(sc);
104         twa_initq_pending(sc);
105         twa_initq_complete(sc);
106
107         if (twa_alloc_req_pkts(sc, TWA_Q_LENGTH)) {
108                 twa_printf(sc, "Failed to allocate request packets.\n");
109                 return(ENOMEM);
110         }
111
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) {
115                 /* 
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.
120                  */
121                 twa_printf(sc, "Could not allocate memory for AEN queue.\n");
122                 return(ENOMEM); /* any unfreed memory will be freed by twa_free */
123         }
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]);
128
129         /*
130          * Disable interrupts from the card.
131          * Interrupts will be enabled back in twa_intrhook.
132          */
133         twa_disable_interrupts(sc);
134
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");
141                 else
142                         error = twa_init_ctlr(sc);
143         }
144         return(error);
145 }
146
147 #ifdef TWA_FLASH_FIRMWARE
148 /*
149  * Function name:       twa_flash_firmware
150  * Description:         Flashes bundled firmware image onto controller.
151  *
152  * Input:               sc      -- ptr to per ctlr structure
153  * Output:              None
154  * Return value:        0       -- success
155  *                      non-zero-- failure
156  */
157 static int
158 twa_flash_firmware(struct twa_softc *sc)
159 {
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;
165         int                                     error;
166         int                                     i;
167
168         if ((tr = twa_get_request(sc)) == NULL) {
169                 /* No free request packets available.  Can't proceed. */
170                 error = EIO;
171                 goto out;
172         }
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"); 
178                 error = ENOMEM;
179                 goto out;
180         }
181         remaining_img_size = twa_fw_img_size;
182         cmd = &(tr->tr_command->command.cmd_pkt_7k.download_fw);
183
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));
187
188                 tr->tr_command->cmd_hdr.header_desc.size_header = 128;
189         
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;
194                 cmd->unit = 0;
195                 cmd->status = 0;
196                 cmd->flags = 0;
197                 cmd->param = 8; /* prom image */
198
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;
203         
204                 remaining_img_size -= this_chunk_size;
205                 bcopy(twa_fw_img + (i * fw_img_chunk_size),
206                                         tr->tr_data, this_chunk_size);
207
208                 /*
209                  * The next line will effect only the last chunk.
210                  */
211                 tr->tr_length = (this_chunk_size + 511) & ~511;
212
213                 tr->tr_flags |= TWA_CMD_DATA_OUT;
214
215                 error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
216                 if (error) {
217                         twa_printf(sc, "Firmware flash request could not be posted. error = 0x%x\n",
218                                                                         error);
219                         if (error == ETIMEDOUT)
220                                 return(error); /* clean-up done by twa_immediate_request */
221                         break;
222                 }
223                 error = cmd->status;
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",
227                                         cmd->opcode,
228                                         TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
229                                         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",
233                                                         cmd->status, i);
234                                 /* Hard reset the controller, so that it doesn't wait for the remaining chunks. */
235                                 twa_hard_reset(sc);
236                                 break;
237                         }
238                 } else   /* last chunk */
239                         if (error) {
240                                 twa_printf(sc, "cmd = 0x%x: ERROR: (0x%02X: 0x%04X): %s: %s\n",
241                                         cmd->opcode,
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. */
249                                 twa_hard_reset(sc);
250                         }
251         } /* for */
252
253         if (tr->tr_data)
254                 free(tr->tr_data, M_DEVBUF);
255 out:
256         if (tr)
257                 twa_release_request(tr);
258         return(error);
259 }
260
261
262 /*
263  * Function name:       twa_hard_reset
264  * Description:         Hard reset the controller.
265  *
266  * Input:               sc      -- ptr to per ctlr structure
267  * Output:              None
268  * Return value:        0       -- success
269  *                      non-zero-- failure
270  */
271 static int
272 twa_hard_reset(struct twa_softc *sc)
273 {
274         struct twa_request                      *tr;
275         struct twa_command_reset_firmware       *cmd;
276         int                                     error;
277
278         if ((tr = twa_get_request(sc)) == NULL)
279                 return(EIO);
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;
283         
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;
288         cmd->unit = 0;
289         cmd->status = 0;
290         cmd->flags = 0;
291         cmd->param = 0; /* don't reload FPGA logic */
292
293         tr->tr_data = NULL;
294         tr->tr_length = 0;
295
296         error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
297         if (error) {
298                 twa_printf(sc, "Hard reset request could not be posted. error = 0x%x\n",
299                                                                 error);
300                 if (error == ETIMEDOUT)
301                         return(error); /* clean-up done by twa_immediate_request */
302                 goto out;
303         }
304         if ((error = cmd->status)) {
305                 twa_printf(sc, "cmd = 0x%x: ERROR: (0x%02X: 0x%04X): %s: %s\n",
306                                         cmd->opcode,
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);
313         }
314
315 out:
316         if (tr)
317                 twa_release_request(tr);
318         return(error);
319 }
320
321 #endif /* TWA_FLASH_FIRMWARE */
322
323 /*
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
331  *                      with it.
332  *
333  * Input:               sc      -- ptr to per ctlr structure
334  * Output:              None
335  * Return value:        0       -- success
336  *                      non-zero-- failure
337  */
338 static int
339 twa_init_ctlr(struct twa_softc *sc)
340 {
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;
346         int             error = 0;
347 #ifdef TWA_FLASH_FIRMWARE
348         int8_t          fw_flashed = FALSE;
349         int8_t          fw_flash_failed = FALSE;
350 #endif /* TWA_FLASH_FIRMWARE */
351
352         twa_dbg_dprint_enter(3, sc);
353
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");
358                 return(ENXIO);
359         }
360         /* Drain the response queue. */
361         if (twa_drain_response_queue(sc)) {
362                 twa_printf(sc, "Can't drain response queue.\n");
363                 return(1);
364         }
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");
373                 return(error);
374         }
375
376 #ifdef TWA_FLASH_FIRMWARE
377
378         if ((init_connect_result & TWA_BUNDLED_FW_SAFE_TO_FLASH) &&
379                 (init_connect_result & TWA_CTLR_FW_RECOMMENDS_FLASH)) {
380                 /*
381                  * The bundled firmware is safe to flash, and the firmware
382                  * on the controller recommends a flash.  So, flash!
383                  */
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");
389                 } else {
390                         twa_printf(sc, "Successfully flashed bundled firmware.\n");
391                         fw_flashed = TRUE;
392                 }
393         }
394
395         if (fw_flashed) {
396                 /* The firmware was flashed.  Have the new image loaded */
397                 error = twa_hard_reset(sc);
398                 if (error)
399                         twa_printf(sc, "Could not reset controller after flash!\n");
400                 else    /* Go through initialization again. */
401                         error = twa_init_ctlr(sc);
402                 /*
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.
407                  * Just return.
408                  */
409                 return(error);
410         } else
411 #endif /* TWA_FLASH_FIRMWARE */
412         {
413                 /*
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
418                  * current mode.
419                  */
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;
425                 } else {
426                         /*
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.
430                          */
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");
439                                 return(error);
440                         }
441                         if (!(init_connect_result & TWA_CTLR_FW_COMPATIBLE)) {
442                                 /*
443                                  * The firmware on the controller is not even
444                                  * compatible with our base mode.  We cannot
445                                  * work with it.  Bail...
446                                  */
447                                 twa_printf(sc, "Incompatible firmware on controller\n");
448 #ifdef TWA_FLASH_FIRMWARE
449                                 if (fw_flash_failed)
450                                         twa_printf(sc, "...and could not flash bundled firmware.\n");
451                                 else
452                                         twa_printf(sc, "...and bundled firmware not safe to flash.\n");
453 #endif /* TWA_FLASH_FIRMWARE */
454                                 return(1);
455                         }
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;
461                 }
462         }
463
464         /* Drain the AEN queue */
465         if (twa_drain_aen_queue(sc)) {
466                 /* 
467                  * We will just print that we couldn't drain the AEN queue.
468                  * There's no need to bail out.
469                  */
470                 twa_printf(sc, "Can't drain AEN queue.\n");
471         }
472
473         /* Set controller state to initialized. */
474         sc->twa_state &= ~TWA_STATE_SHUTDOWN;
475
476         twa_dbg_dprint_exit(3, sc);
477         return(0);
478 }
479
480
481 /*
482  * Function name:       twa_deinit_ctlr
483  * Description:         Close logical connection with the controller.
484  *
485  * Input:               sc      -- ptr to per ctlr structure
486  * Output:              None
487  * Return value:        0       -- success
488  *                      non-zero-- failure
489  */
490 int
491 twa_deinit_ctlr(struct twa_softc *sc)
492 {
493         /*
494          * Mark the controller as shutting down,
495          * and disable any further interrupts.
496          */
497         sc->twa_state |= TWA_STATE_SHUTDOWN;
498         twa_disable_interrupts(sc);
499
500         /* Let the controller know that we are going down. */
501         return(twa_init_connection(sc, TWA_SHUTDOWN_MESSAGE_CREDITS,
502                                         0, 0, 0, 0, 0,
503                                         NULL, NULL, NULL, NULL, NULL));
504 }
505
506
507 /*
508  * Function name:       twa_interrupt
509  * Description:         Interrupt handler.  Determines the kind of interrupt,
510  *                      and calls the appropriate handler.
511  *
512  * Input:               sc      -- ptr to per ctlr structure
513  * Output:              None
514  * Return value:        None
515  */
516 void
517 twa_interrupt(struct twa_softc *sc)
518 {
519         u_int32_t       status_reg;
520
521         twa_dbg_dprint_enter(5, sc);
522
523         /* Collect current interrupt status. */
524         status_reg = TWA_READ_STATUS_REGISTER(sc);
525         if (twa_check_ctlr_state(sc, status_reg))
526                 return;
527
528         /* Dispatch based on the kind of interrupt. */
529         if (status_reg & TWA_STATUS_HOST_INTERRUPT)
530                 twa_host_intr(sc);
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)
536                 twa_done(sc);
537 }
538
539
540 /*
541  * Function name:       twa_ioctl
542  * Description:         ioctl handler.
543  *
544  * Input:               sc      -- ptr to per ctlr structure
545  *                      cmd     -- ioctl cmd
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
551  *                      non-zero-- failure
552  */
553 int
554 twa_ioctl(struct twa_softc *sc, int cmd, void *buf)
555 {
556         struct twa_ioctl_9k     *user_buf = (struct twa_ioctl_9k *)buf;
557         struct twa_event_packet event_buf;
558         int32_t                 event_index;
559         int32_t                 start_index;
560         int                     s;
561         int                     error = 0;
562                 
563         switch (cmd) {
564         case TWA_IOCTL_FIRMWARE_PASS_THROUGH:
565         {
566                 struct twa_command_packet       *cmdpkt;
567                 struct twa_request              *tr;
568                 u_int32_t                       data_buf_size_adjusted;
569
570
571                 twa_dbg_dprint(2, sc, "Firmware PassThru");
572
573                 /* Get a request packet */
574                 while ((tr = twa_get_request(sc)) == NULL)
575                         /*
576                          * No free request packets available.  Sleep until
577                          * one becomes available.
578                          */
579                         tsleep(&(sc->twa_wait_timeout), PPAUSE, "twioctl", hz);
580
581                 /*
582                  * Make sure that the data buffer sent to firmware is a 
583                  * 512 byte multiple in size.
584                  */
585                 data_buf_size_adjusted = (user_buf->twa_drvr_pkt.buffer_length + 511) & ~511;
586                 if ((tr->tr_length = data_buf_size_adjusted)) {
587                         if ((tr->tr_data = malloc(data_buf_size_adjusted, M_DEVBUF, M_WAITOK)) == NULL) {
588                                 twa_printf(sc, "Could not alloc mem for fw_passthru data_buf.\n");
589                                 error = ENOMEM;
590                                 goto fw_passthru_done;
591                         }
592                         /* Copy the payload. */
593                         if ((error = copyin((void *) (user_buf->pdata), 
594                                         (void *) (tr->tr_data),
595                                         user_buf->twa_drvr_pkt.buffer_length)) != 0) {
596                                 twa_printf (sc, "Could not copyin fw_passthru data_buf.\n"); 
597                                 goto fw_passthru_done;
598                         }
599                         tr->tr_flags |= TWA_CMD_DATA_IN | TWA_CMD_DATA_OUT;
600                 }
601                 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_IOCTL;
602                 cmdpkt = tr->tr_command;
603
604                 /* Copy the command packet. */
605                 bcopy(&(user_buf->twa_cmd_pkt), cmdpkt,
606                                         sizeof(struct twa_command_packet));
607                 cmdpkt->command.cmd_pkt_7k.generic.request_id = tr->tr_request_id;
608
609                 twa_dbg_dprint(3, sc, "cmd_pkt_7k = %x %x %x %x %x %x %x",
610                                         cmdpkt->command.cmd_pkt_7k.generic.opcode,
611                                         cmdpkt->command.cmd_pkt_7k.generic.sgl_offset,
612                                         cmdpkt->command.cmd_pkt_7k.generic.size,
613                                         cmdpkt->command.cmd_pkt_7k.generic.request_id,
614                                         cmdpkt->command.cmd_pkt_7k.generic.unit,
615                                         cmdpkt->command.cmd_pkt_7k.generic.status,
616                                         cmdpkt->command.cmd_pkt_7k.generic.flags);
617
618                 /* Send down the request, and wait for it to complete. */
619                 if ((error = twa_wait_request(tr, TWA_REQUEST_TIMEOUT_PERIOD))) {
620                         twa_printf(sc, "fw_passthru request failed. error = 0x%x\n", error);
621                         if (error == ETIMEDOUT)
622                                 break; /* clean-up done by twa_wait_request */
623                         goto fw_passthru_done;
624                 }
625
626                 /* Copy the command packet back into user space. */
627                 bcopy(cmdpkt, &(user_buf->twa_cmd_pkt),
628                                         sizeof(struct twa_command_packet));
629         
630                 /* If there was a payload, copy it back too. */
631                 if (tr->tr_length)
632                         error = copyout(tr->tr_data, user_buf->pdata,
633                                         user_buf->twa_drvr_pkt.buffer_length);
634
635 fw_passthru_done:
636                 /* Free resources. */
637                 if (tr->tr_data)
638                         free(tr->tr_data, M_DEVBUF);
639                 if (tr)
640                         twa_release_request(tr);
641                 break;
642         }
643
644
645         case TWA_IOCTL_SCAN_BUS:
646                 /* Request CAM for a bus scan. */
647                 twa_request_bus_scan(sc);
648                 break;
649
650
651         case TWA_IOCTL_GET_FIRST_EVENT:
652                 twa_dbg_dprint(3, sc, "Get First Event");
653
654                 if (sc->twa_aen_queue_wrapped) {
655                         if (sc->twa_aen_queue_overflow) {
656                                 /*
657                                  * The aen queue has wrapped, even before some
658                                  * events have been retrieved.  Let the caller
659                                  * know that he missed out on some AEN's.
660                                  */
661                                 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_OVERFLOW;
662                                 sc->twa_aen_queue_overflow = FALSE;
663                         } else
664                                 user_buf->twa_drvr_pkt.status = 0;
665                         event_index = sc->twa_aen_head;
666                 } else {
667                         if (sc->twa_aen_head == sc->twa_aen_tail) {
668                                 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
669                                 break;
670                         }
671                         user_buf->twa_drvr_pkt.status = 0;
672                         event_index = sc->twa_aen_tail; /* = 0 */
673                 }
674                 if ((error = copyout(sc->twa_aen_queue[event_index], user_buf->pdata,
675                                         sizeof(struct twa_event_packet))) != 0)
676                         twa_printf(sc, "get_first: Could not copyout to event_buf. error = %x\n", error);
677                 (sc->twa_aen_queue[event_index])->retrieved = TWA_AEN_RETRIEVED;
678                 break;
679
680
681         case TWA_IOCTL_GET_LAST_EVENT:
682                 twa_dbg_dprint(3, sc, "Get Last Event");
683
684                 if (sc->twa_aen_queue_wrapped) {
685                         if (sc->twa_aen_queue_overflow) {
686                                 /*
687                                  * The aen queue has wrapped, even before some
688                                  * events have been retrieved.  Let the caller
689                                  * know that he missed out on some AEN's.
690                                  */
691                                 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_OVERFLOW;
692                                 sc->twa_aen_queue_overflow = FALSE;
693                         } else
694                                 user_buf->twa_drvr_pkt.status = 0;
695                 } else {
696                         if (sc->twa_aen_head == sc->twa_aen_tail) {
697                                 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
698                                 break;
699                         }
700                         user_buf->twa_drvr_pkt.status = 0;
701                 }
702                 event_index = (sc->twa_aen_head - 1 + TWA_Q_LENGTH) % TWA_Q_LENGTH;
703                 if ((error = copyout(sc->twa_aen_queue[event_index], user_buf->pdata,
704                                         sizeof(struct twa_event_packet))) != 0)
705                         twa_printf(sc, "get_last: Could not copyout to event_buf. error = %x\n", error);
706                 (sc->twa_aen_queue[event_index])->retrieved = TWA_AEN_RETRIEVED;
707                 break;
708
709
710         case TWA_IOCTL_GET_NEXT_EVENT:
711                 twa_dbg_dprint(3, sc, "Get Next Event");
712
713                 user_buf->twa_drvr_pkt.status = 0;
714                 if (sc->twa_aen_queue_wrapped) {
715                         twa_dbg_dprint(3, sc, "Get Next Event: wrapped");
716                         if (sc->twa_aen_queue_overflow) {
717                                 /*
718                                  * The aen queue has wrapped, even before some
719                                  * events have been retrieved.  Let the caller
720                                  * know that he missed out on some AEN's.
721                                  */
722                                 twa_dbg_dprint(2, sc, "Get Next Event: overflow");
723                                 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_OVERFLOW;
724                                 sc->twa_aen_queue_overflow = FALSE;
725                         }
726                         start_index = sc->twa_aen_head;
727                 } else {
728                         if (sc->twa_aen_head == sc->twa_aen_tail) {
729                                 twa_dbg_dprint(3, sc, "Get Next Event: empty queue");
730                                 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
731                                 break;
732                         }
733                         start_index = sc->twa_aen_tail; /* = 0 */
734                 }
735                 if ((error = copyin(user_buf->pdata, &event_buf,
736                                 sizeof(struct twa_event_packet))) != 0)
737                         twa_printf(sc, "get_next: Could not copyin event_buf.\n");
738
739                 event_index = (start_index + event_buf.sequence_id -
740                                 (sc->twa_aen_queue[start_index])->sequence_id + 1)
741                                 % TWA_Q_LENGTH;
742
743                 twa_dbg_dprint(3, sc, "Get Next Event: si = %x, ei = %x, ebsi = %x, sisi = %x, eisi = %x",
744                                 start_index, event_index, event_buf.sequence_id,
745                                 (sc->twa_aen_queue[start_index])->sequence_id,
746                                 (sc->twa_aen_queue[event_index])->sequence_id);
747
748                 if (! ((sc->twa_aen_queue[event_index])->sequence_id >
749                                                 event_buf.sequence_id)) {
750                         if (user_buf->twa_drvr_pkt.status == TWA_ERROR_AEN_OVERFLOW)
751                                 sc->twa_aen_queue_overflow = TRUE; /* so we report the overflow next time */
752                         user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
753                         break;
754                 }
755                 if ((error = copyout(sc->twa_aen_queue[event_index], user_buf->pdata, 
756                                         sizeof(struct twa_event_packet))) != 0)
757                         twa_printf(sc, "get_next: Could not copyout to event_buf. error = %x\n", error);
758
759                 (sc->twa_aen_queue[event_index])->retrieved = TWA_AEN_RETRIEVED;
760                 break;
761
762
763         case TWA_IOCTL_GET_PREVIOUS_EVENT:
764                 twa_dbg_dprint(3, sc, "Get Previous Event");
765
766                 user_buf->twa_drvr_pkt.status = 0;
767                 if (sc->twa_aen_queue_wrapped) {
768                         if (sc->twa_aen_queue_overflow) {
769                                 /*
770                                  * The aen queue has wrapped, even before some
771                                  * events have been retrieved.  Let the caller
772                                  * know that he missed out on some AEN's.
773                                  */
774                                 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_OVERFLOW;
775                                 sc->twa_aen_queue_overflow = FALSE;
776                         }
777                         start_index = sc->twa_aen_head;
778                 } else {
779                         if (sc->twa_aen_head == sc->twa_aen_tail) {
780                                 user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
781                                 break;
782                         }
783                         start_index = sc->twa_aen_tail; /* = 0 */
784                 }
785                 if ((error = copyin(user_buf->pdata, &event_buf,
786                                 sizeof(struct twa_event_packet))) != 0)
787                         twa_printf(sc, "get_previous: Could not copyin event_buf.\n");
788
789                 event_index = (start_index + event_buf.sequence_id -
790                         (sc->twa_aen_queue[start_index])->sequence_id - 1) % TWA_Q_LENGTH;
791                 if (! ((sc->twa_aen_queue[event_index])->sequence_id < event_buf.sequence_id)) {
792                         if (user_buf->twa_drvr_pkt.status == TWA_ERROR_AEN_OVERFLOW)
793                                 sc->twa_aen_queue_overflow = TRUE; /* so we report the overflow next time */
794                         user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
795                         break;
796                 }
797                 if ((error = copyout(sc->twa_aen_queue [event_index], user_buf->pdata,
798                                         sizeof(struct twa_event_packet))) != 0)
799                         twa_printf(sc, "get_previous: Could not copyout to event_buf. error = %x\n", error);
800
801                 (sc->twa_aen_queue[event_index])->retrieved = TWA_AEN_RETRIEVED;
802                 break;
803
804
805         case TWA_IOCTL_GET_LOCK:
806         {
807                 struct twa_lock_packet  twa_lock;
808                 u_int32_t               cur_time;
809
810                 cur_time = time_second - (tz.tz_minuteswest * 60) - 
811                                         (wall_cmos_clock ? adjkerntz : 0);
812                 copyin(user_buf->pdata, &twa_lock,
813                                 sizeof(struct twa_lock_packet));
814                 s = splcam();
815                 if ((sc->twa_ioctl_lock.lock == TWA_LOCK_FREE) ||
816                                 (twa_lock.force_flag) ||
817                                 (cur_time >= sc->twa_ioctl_lock.timeout)) {
818                         twa_dbg_dprint(3, sc, "GET_LOCK: Getting lock!");
819                         sc->twa_ioctl_lock.lock = TWA_LOCK_HELD;
820                         sc->twa_ioctl_lock.timeout = cur_time + (twa_lock.timeout_msec / 1000);
821                         twa_lock.time_remaining_msec = twa_lock.timeout_msec;
822                         user_buf->twa_drvr_pkt.status = 0;
823                 } else {
824                         twa_dbg_dprint(2, sc, "GET_LOCK: Lock already held!");
825                         twa_lock.time_remaining_msec =
826                                 (sc->twa_ioctl_lock.timeout - cur_time) * 1000;
827                         user_buf->twa_drvr_pkt.status =
828                                         TWA_ERROR_IOCTL_LOCK_ALREADY_HELD;
829                 }
830                 splx(s);
831                 copyout(&twa_lock, user_buf->pdata,
832                                 sizeof(struct twa_lock_packet));
833                 break;
834         }
835
836
837         case TWA_IOCTL_RELEASE_LOCK:
838                 s = splcam();
839                 if (sc->twa_ioctl_lock.lock == TWA_LOCK_FREE) {
840                         twa_dbg_dprint(2, sc, "twa_ioctl: RELEASE_LOCK: Lock not held!");
841                         user_buf->twa_drvr_pkt.status = TWA_ERROR_IOCTL_LOCK_NOT_HELD;
842                 } else {
843                         twa_dbg_dprint(3, sc, "RELEASE_LOCK: Releasing lock!");
844                         sc->twa_ioctl_lock.lock = TWA_LOCK_FREE;
845                         user_buf->twa_drvr_pkt.status = 0;
846                 }
847                 splx(s);
848                 break;
849
850
851         case TWA_IOCTL_GET_COMPATIBILITY_INFO:
852         {
853                 struct twa_compatibility_packet comp_pkt;
854
855                 bcopy(TWA_DRIVER_VERSION_STRING, comp_pkt.driver_version,
856                                         sizeof(TWA_DRIVER_VERSION_STRING));
857                 comp_pkt.working_srl = sc->working_srl;
858                 comp_pkt.working_branch = sc->working_branch;
859                 comp_pkt.working_build = sc->working_build;
860                 user_buf->twa_drvr_pkt.status = 0;
861
862                 /* Copy compatibility information to user space. */
863                 copyout(&comp_pkt, user_buf->pdata,
864                                 min(sizeof(struct twa_compatibility_packet),
865                                         user_buf->twa_drvr_pkt.buffer_length));
866                 break;
867         }
868
869         default:        
870                 /* Unknown opcode. */
871                 error = ENOTTY;
872         }
873
874         return(error);
875 }
876
877
878 /*
879  * Function name:       twa_enable_interrupts
880  * Description:         Enables interrupts on the controller
881  *
882  * Input:               sc      -- ptr to per ctlr structure
883  * Output:              None
884  * Return value:        None
885  */
886 void
887 twa_enable_interrupts(struct twa_softc *sc)
888 {
889         sc->twa_state |= TWA_STATE_INTR_ENABLED;
890         TWA_WRITE_CONTROL_REGISTER(sc,
891                 TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT |
892                 TWA_CONTROL_UNMASK_RESPONSE_INTERRUPT |
893                 TWA_CONTROL_ENABLE_INTERRUPTS);
894 }
895
896
897 /*
898  * Function name:       twa_setup
899  * Description:         Disables interrupts on the controller
900  *
901  * Input:               sc      -- ptr to per ctlr structure
902  * Output:              None
903  * Return value:        None
904  */
905 void
906 twa_disable_interrupts(struct twa_softc *sc)
907 {
908         TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_DISABLE_INTERRUPTS);
909         sc->twa_state &= ~TWA_STATE_INTR_ENABLED;
910 }
911
912
913
914 /*
915  * Function name:       twa_get_param
916  * Description:         Get a firmware parameter.
917  *
918  * Input:               sc              -- ptr to per ctlr structure
919  *                      table_id        -- parameter table #
920  *                      param_id        -- index of the parameter in the table
921  *                      param_size      -- size of the parameter in bytes
922  *                      callback        -- ptr to function, if any, to be called
923  *                                      back on completion; NULL if no callback.
924  * Output:              None
925  * Return value:        ptr to param structure  -- success
926  *                      NULL                    -- failure
927  */
928 static void *
929 twa_get_param(struct twa_softc *sc, int table_id, int param_id,
930                 size_t param_size, void (* callback)(struct twa_request *tr))
931 {
932         struct twa_request      *tr;
933         union twa_command_7k    *cmd;
934         struct twa_param_9k     *param = NULL;
935         int                     error = ENOMEM;
936
937         twa_dbg_dprint_enter(4, sc);
938
939         /* Get a request packet. */
940         if ((tr = twa_get_request(sc)) == NULL)
941                 goto out;
942         tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
943
944         /* Allocate memory to read data into. */
945         param = malloc(TWA_SECTOR_SIZE, M_DEVBUF, M_INTWAIT);
946         bzero(param, sizeof(struct twa_param_9k) - 1 + param_size);
947         tr->tr_data = param;
948         tr->tr_length = TWA_SECTOR_SIZE;
949         tr->tr_flags = TWA_CMD_DATA_IN | TWA_CMD_DATA_OUT;
950
951         /* Build the cmd pkt. */
952         cmd = &(tr->tr_command->command.cmd_pkt_7k);
953
954         tr->tr_command->cmd_hdr.header_desc.size_header = 128;
955         
956         cmd->param.opcode = TWA_OP_GET_PARAM;
957         cmd->param.sgl_offset = 2;
958         cmd->param.size = 2;
959         cmd->param.request_id = tr->tr_request_id;
960         cmd->param.unit = 0;
961         cmd->param.param_count = 1;
962
963         /* Specify which parameter we need. */
964         param->table_id = table_id | TWA_9K_PARAM_DESCRIPTOR;
965         param->parameter_id = param_id;
966         param->parameter_size_bytes = param_size;
967
968         /* Submit the command. */
969         if (callback == NULL) {
970                 /* There's no call back; wait till the command completes. */
971                 error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
972                 if (error == ETIMEDOUT)
973                         return(NULL); /* clean-up done by twa_immediate_request */
974                 if (error)
975                         goto out;
976                 if ((error = cmd->param.status)) {
977                         twa_printf(sc, "cmd = 0x%x: ERROR: (0x%02X: 0x%04X): %s: %s\n",
978                                         cmd->param.opcode,
979                                         TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
980                                         tr->tr_command->cmd_hdr.status_block.error,
981                                         twa_find_msg_string(twa_error_table,
982                                                 tr->tr_command->cmd_hdr.status_block.error),
983                                         tr->tr_command->cmd_hdr.err_specific_desc);
984                         goto out; /* twa_drain_complete_queue will have done the unmapping */
985                 }
986                 twa_release_request(tr);
987                 return(param);
988         } else {
989                 /* There's a call back.  Simply submit the command. */
990                 tr->tr_callback = callback;
991                 if ((error = twa_map_request(tr))) {
992                         twa_printf(tr->tr_sc, "%s: twa_map_request returned 0x%x\n",
993                                                 __func__, error);
994                         goto out;
995                 }
996                 return(callback);
997         }
998
999 out:
1000         twa_printf(sc, "get_param failed. error = 0x%x\n", error);
1001         if (param)
1002                 free(param, M_DEVBUF);
1003         if (tr)
1004                 twa_release_request(tr);
1005         return(NULL);
1006 }
1007
1008
1009 /*
1010  * Function name:       twa_set_param
1011  * Description:         Set a firmware parameter.
1012  *
1013  * Input:               sc              -- ptr to per ctlr structure
1014  *                      table_id        -- parameter table #
1015  *                      param_id        -- index of the parameter in the table
1016  *                      param_size      -- size of the parameter in bytes
1017  *                      callback        -- ptr to function, if any, to be called
1018  *                                      back on completion; NULL if no callback.
1019  * Output:              None
1020  * Return value:        0       -- success
1021  *                      non-zero-- failure
1022  */
1023 static int
1024 twa_set_param(struct twa_softc *sc, int table_id,
1025                         int param_id, int param_size, void *data,
1026                         void (* callback)(struct twa_request *tr))
1027 {
1028         struct twa_request      *tr;
1029         union twa_command_7k    *cmd;
1030         struct twa_param_9k     *param = NULL;
1031         int                     error = ENOMEM;
1032
1033         twa_dbg_dprint_enter(4, sc);
1034
1035         /* Get a request packet. */
1036         if ((tr = twa_get_request(sc)) == NULL)
1037                 goto out;
1038         tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
1039
1040         /* Allocate memory to send data using. */
1041         param = malloc(TWA_SECTOR_SIZE, M_DEVBUF, M_INTWAIT);
1042         bzero(param, sizeof(struct twa_param_9k) - 1 + param_size);
1043         tr->tr_data = param;
1044         tr->tr_length = TWA_SECTOR_SIZE;
1045         tr->tr_flags = TWA_CMD_DATA_IN | TWA_CMD_DATA_OUT;
1046
1047         /* Build the cmd pkt. */
1048         cmd = &(tr->tr_command->command.cmd_pkt_7k);
1049
1050         tr->tr_command->cmd_hdr.header_desc.size_header = 128;
1051
1052         cmd->param.opcode = TWA_OP_SET_PARAM;
1053         cmd->param.sgl_offset = 2;
1054         cmd->param.size = 2;
1055         cmd->param.request_id = tr->tr_request_id;
1056         cmd->param.unit = 0;
1057         cmd->param.param_count = 1;
1058
1059         /* Specify which parameter we want to set. */
1060         param->table_id = table_id | TWA_9K_PARAM_DESCRIPTOR;
1061         param->parameter_id = param_id;
1062         param->parameter_size_bytes = param_size;
1063         bcopy(data, param->data, param_size);
1064
1065         /* Submit the command. */
1066         if (callback == NULL) {
1067                 /* There's no call back;  wait till the command completes. */
1068                 error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
1069                 if (error == ETIMEDOUT)
1070                         return(error); /* clean-up done by twa_immediate_request */
1071                 if (error)
1072                         goto out;
1073                 if ((error = cmd->param.status)) {
1074                         twa_printf(sc, "cmd = 0x%x: ERROR: (0x%02X: 0x%04X): %s: %s\n",
1075                                         cmd->param.opcode,
1076                                         TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
1077                                         tr->tr_command->cmd_hdr.status_block.error,
1078                                         twa_find_msg_string(twa_error_table,
1079                                                 tr->tr_command->cmd_hdr.status_block.error),
1080                                         tr->tr_command->cmd_hdr.err_specific_desc);
1081                         goto out; /* twa_drain_complete_queue will have done the unmapping */
1082                 }
1083                 free(param, M_DEVBUF);
1084                 twa_release_request(tr);
1085                 return(error);
1086         } else {
1087                 /* There's a call back.  Simply submit the command. */
1088                 tr->tr_callback = callback;
1089                 if ((error = twa_map_request(tr))) {
1090                         twa_printf(tr->tr_sc, "%s: twa_map_request returned 0x%x\n",
1091                                                 __func__, error);
1092                         goto out;
1093                 }
1094                 return(0);
1095         }
1096
1097 out:
1098         twa_printf(sc, "set_param failed. error = 0x%x\n", error);
1099         if (param)
1100                 free(param, M_DEVBUF);
1101         if (tr)
1102                 twa_release_request(tr);
1103         return(error);
1104 }
1105
1106
1107 /*
1108  * Function name:       twa_init_connection
1109  * Description:         Send init_connection cmd to firmware
1110  *
1111  * Input:               sc              -- ptr to per ctlr structure
1112  *                      message_credits -- max # of requests that we might send
1113  *                                       down simultaneously.  This will be
1114  *                                       typically set to 256 at init-time or
1115  *                                      after a reset, and to 1 at shutdown-time
1116  *                      set_features    -- indicates if we intend to use 64-bit
1117  *                                      sg, also indicates if we want to do a
1118  *                                      basic or an extended init_connection;
1119  *
1120  * Note: The following input/output parameters are valid, only in case of an
1121  *              extended init_connection:
1122  *
1123  *                      current_fw_srl          -- srl of fw we are bundled
1124  *                                              with, if any; 0 otherwise
1125  *                      current_fw_arch_id      -- arch_id of fw we are bundled
1126  *                                              with, if any; 0 otherwise
1127  *                      current_fw_branch       -- branch # of fw we are bundled
1128  *                                              with, if any; 0 otherwise
1129  *                      current_fw_build        -- build # of fw we are bundled
1130  *                                              with, if any; 0 otherwise
1131  * Output:              fw_on_ctlr_srl          -- srl of fw on ctlr
1132  *                      fw_on_ctlr_arch_id      -- arch_id of fw on ctlr
1133  *                      fw_on_ctlr_branch       -- branch # of fw on ctlr
1134  *                      fw_on_ctlr_build        -- build # of fw on ctlr
1135  *                      init_connect_result     -- result bitmap of fw response
1136  * Return value:        0       -- success
1137  *                      non-zero-- failure
1138  */
1139 static int
1140 twa_init_connection(struct twa_softc *sc, u_int16_t message_credits,
1141                         u_int32_t set_features, u_int16_t current_fw_srl,
1142                         u_int16_t current_fw_arch_id, u_int16_t current_fw_branch,
1143                         u_int16_t current_fw_build, u_int16_t *fw_on_ctlr_srl,
1144                         u_int16_t *fw_on_ctlr_arch_id, u_int16_t *fw_on_ctlr_branch,
1145                         u_int16_t *fw_on_ctlr_build, u_int32_t *init_connect_result)
1146 {
1147         struct twa_request              *tr;
1148         struct twa_command_init_connect *init_connect;
1149         int                             error = 1;
1150     
1151         twa_dbg_dprint_enter(3, sc);
1152
1153         /* Get a request packet. */
1154         if ((tr = twa_get_request(sc)) == NULL)
1155                 goto out;
1156         tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
1157         /* Build the cmd pkt. */
1158         init_connect = &(tr->tr_command->command.cmd_pkt_7k.init_connect);
1159
1160         tr->tr_command->cmd_hdr.header_desc.size_header = 128;
1161
1162         init_connect->opcode = TWA_OP_INIT_CONNECTION;
1163         init_connect->request_id = tr->tr_request_id;
1164         init_connect->message_credits = message_credits;
1165         init_connect->features = set_features;
1166         if (TWA_64BIT_ADDRESSES)
1167                 init_connect->features |= TWA_64BIT_SG_ADDRESSES;
1168         if (set_features & TWA_EXTENDED_INIT_CONNECT) {
1169                 /* Fill in the extra fields needed for an extended init_connect. */
1170                 init_connect->size = 6;
1171                 init_connect->fw_srl = current_fw_srl;
1172                 init_connect->fw_arch_id = current_fw_arch_id;
1173                 init_connect->fw_branch = current_fw_branch;
1174                 init_connect->fw_build = current_fw_build;
1175         } else
1176                 init_connect->size = 3;
1177
1178         /* Submit the command, and wait for it to complete. */
1179         error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
1180         if (error == ETIMEDOUT)
1181                 return(error); /* clean-up done by twa_immediate_request */
1182         if (error)
1183                 goto out;
1184         if ((error = init_connect->status)) {
1185                 twa_printf(sc, "cmd = 0x%x: ERROR: (0x%02X: 0x%04X): %s: %s\n",
1186                                         init_connect->opcode,
1187                                         TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
1188                                         tr->tr_command->cmd_hdr.status_block.error,
1189                                         twa_find_msg_string(twa_error_table,
1190                                                 tr->tr_command->cmd_hdr.status_block.error),
1191                                         tr->tr_command->cmd_hdr.err_specific_desc);
1192                 goto out; /* twa_drain_complete_queue will have done the unmapping */
1193         }
1194         if (set_features & TWA_EXTENDED_INIT_CONNECT) {
1195                 *fw_on_ctlr_srl = init_connect->fw_srl;
1196                 *fw_on_ctlr_arch_id = init_connect->fw_arch_id;
1197                 *fw_on_ctlr_branch = init_connect->fw_branch;
1198                 *fw_on_ctlr_build = init_connect->fw_build;
1199                 *init_connect_result = init_connect->result;
1200         }
1201         twa_release_request(tr);
1202         return(error);
1203
1204 out:
1205         twa_printf(sc, "init_connection failed. error = 0x%x\n", error);
1206         if (tr)
1207                 twa_release_request(tr);
1208         return(error);
1209 }
1210
1211
1212
1213 /*
1214  * Function name:       twa_wait_request
1215  * Description:         Sends down a firmware cmd, and waits for the completion,
1216  *                      but NOT in a tight loop.
1217  *
1218  * Input:               tr      -- ptr to request pkt
1219  *                      timeout -- max # of seconds to wait before giving up
1220  * Output:              None
1221  * Return value:        0       -- success
1222  *                      non-zero-- failure
1223  */
1224 static int
1225 twa_wait_request(struct twa_request *tr, u_int32_t timeout)
1226 {
1227         time_t  end_time;
1228         int     error;
1229
1230         twa_dbg_dprint_enter(4, tr->tr_sc);
1231
1232         tr->tr_flags |= TWA_CMD_SLEEP_ON_REQUEST;
1233         tr->tr_status = TWA_CMD_BUSY;
1234         if ((error = twa_map_request(tr))) {
1235                 twa_printf(tr->tr_sc, "%s: twa_map_request returned 0x%x\n",
1236                                                 __func__, error);
1237                 return(error);
1238         }
1239
1240         end_time = time_second + timeout;
1241         while (tr->tr_status != TWA_CMD_COMPLETE) {
1242                 if ((error = tr->tr_error))
1243                         return(error);
1244                 if ((error = tsleep(tr, 0, "twawait", timeout * hz)) == 0) {
1245                         error = (tr->tr_status != TWA_CMD_COMPLETE);
1246                         break;
1247                 }
1248                 if (error == EWOULDBLOCK) {
1249                         /* Time out! */
1250                         twa_printf(tr->tr_sc, "%s: Request %p timed out.\n",
1251                                                                 __func__, tr);
1252                         /*
1253                          * We will reset the controller only if the request has
1254                          * already been submitted, so as to not lose the
1255                          * request packet.  If a busy request timed out, the
1256                          * reset will take care of freeing resources.  If a
1257                          * pending request timed out, we will free resources
1258                          * for that request, right here.  So, the caller is
1259                          * expected to NOT cleanup when ETIMEDOUT is returned.
1260                          */
1261                         if (tr->tr_status != TWA_CMD_PENDING)
1262                                 twa_reset(tr->tr_sc);
1263                         else {
1264                                 /* Request was never submitted.  Clean up. */
1265                                 twa_remove_pending(tr);
1266                                 twa_unmap_request(tr);
1267                                 if (tr->tr_data)
1268                                         free(tr->tr_data, M_DEVBUF);
1269                                 twa_release_request(tr);
1270                         }
1271                         return(ETIMEDOUT);
1272                 }
1273                 /* 
1274                  * Either the request got completed, or we were woken up by a
1275                  * signal.  Calculate the new timeout, in case it was the latter.
1276                  */
1277                 timeout = (end_time - time_second);
1278         }
1279         twa_unmap_request(tr);
1280         return(error);
1281 }
1282
1283
1284
1285 /*
1286  * Function name:       twa_immediate_request
1287  * Description:         Sends down a firmware cmd, and waits for the completion
1288  *                      in a tight loop.
1289  *
1290  * Input:               tr      -- ptr to request pkt
1291  *                      timeout -- max # of seconds to wait before giving up
1292  * Output:              None
1293  * Return value:        0       -- success
1294  *                      non-zero-- failure
1295  */
1296 static int
1297 twa_immediate_request(struct twa_request *tr, u_int32_t timeout)
1298 {
1299         time_t  end_time;
1300         int     error = 0;
1301
1302         twa_dbg_dprint_enter(4, tr->tr_sc);
1303
1304         if ((error = twa_map_request(tr))) {
1305                 twa_printf(tr->tr_sc, "%s: twa_map_request returned 0x%x\n",
1306                                                 __func__, error);
1307                 return(error);
1308         }
1309
1310         end_time = time_second + timeout;
1311         do {
1312                 if ((error = tr->tr_error))
1313                         return(error);
1314                 twa_done(tr->tr_sc);
1315                 if ((tr->tr_status != TWA_CMD_BUSY) &&
1316                         (tr->tr_status != TWA_CMD_PENDING)) {
1317                         twa_unmap_request(tr);
1318                         return(tr->tr_status != TWA_CMD_COMPLETE);
1319                 }
1320         } while (time_second <= end_time);
1321
1322         /* Time out! */
1323         twa_printf(tr->tr_sc, "%s: Request %p timed out.\n", __func__, tr);
1324         /*
1325          * We will reset the controller only if the request has
1326          * already been submitted, so as to not lose the
1327          * request packet.  If a busy request timed out, the
1328          * reset will take care of freeing resources.  If a
1329          * pending request timed out, we will free resources
1330          * for that request, right here.  So, the caller is
1331          * expected to NOT cleanup when ETIMEDOUT is returned.
1332          */
1333         if (tr->tr_status != TWA_CMD_PENDING)
1334                 twa_reset(tr->tr_sc);
1335         else {
1336                 /* Request was never submitted.  Clean up. */
1337                 twa_remove_pending(tr);
1338                 twa_unmap_request(tr);
1339                 if (tr->tr_data)
1340                         free(tr->tr_data, M_DEVBUF);
1341                 twa_release_request(tr);
1342         }
1343         return(ETIMEDOUT);
1344 }
1345
1346
1347
1348 /*
1349  * Function name:       twa_complete_io
1350  * Description:         Callback on scsi requests to fw.
1351  *
1352  * Input:               tr      -- ptr to request pkt
1353  * Output:              None
1354  * Return value:        None
1355  */
1356 void
1357 twa_complete_io(struct twa_request *tr)
1358 {
1359         struct twa_softc        *sc = tr->tr_sc;
1360
1361         twa_dbg_dprint_enter(8, sc);
1362
1363         if (tr->tr_status != TWA_CMD_COMPLETE)
1364                 twa_panic(sc, "twa_complete_io on incomplete command");
1365         if (tr->tr_private) /* This is a scsi cmd.  Complete it. */
1366                 twa_scsi_complete(tr);
1367         twa_release_request(tr);
1368 }
1369
1370
1371 /*
1372  * Function name:       twa_reset
1373  * Description:         Soft resets and then initializes the controller;
1374  *                      drains any incomplete requests.
1375  *
1376  * Input:               sc      -- ptr to per ctlr structure
1377  * Output:              None
1378  * Return value:        0       -- success
1379  *                      non-zero-- failure
1380  */
1381 int
1382 twa_reset(struct twa_softc *sc)
1383 {
1384         int     s;
1385         int     error = 0;
1386
1387         twa_dbg_dprint_enter(2, sc);
1388
1389         /*
1390          * Disable interrupts from the controller, and mask any
1391          * accidental entry into our interrupt handler.
1392          */
1393         twa_disable_interrupts(sc);
1394         s = splcam();
1395         
1396         /* Soft reset the controller. */
1397         if ((error = twa_soft_reset(sc))) {
1398                 twa_printf (sc, "Controller reset failed.\n");
1399                 goto out;
1400         }
1401
1402         /* Re-establish logical connection with the controller. */
1403         if ((error = twa_init_connection(sc, TWA_INIT_MESSAGE_CREDITS,
1404                                         0, 0, 0, 0, 0,
1405                                         NULL, NULL, NULL, NULL, NULL))) {
1406                 twa_printf(sc, "Can't initialize connection after reset.\n");
1407                 goto out;
1408         }
1409
1410         twa_printf(sc, "Controller reset done!\n");
1411
1412         /*
1413          * Complete all requests in the complete queue; error back all requests
1414          * in the busy queue.  Any internal requests will be simply freed.
1415          * Re-submit any requests in the pending queue.
1416          */
1417         twa_drain_complete_queue(sc);
1418         twa_drain_busy_queue(sc);
1419
1420 out:
1421         splx(s);
1422         /*
1423          * Enable interrupts, and also clear attention and response interrupts.
1424          */
1425         twa_enable_interrupts(sc);
1426         return(error);
1427 }
1428
1429
1430
1431 /*
1432  * Function name:       twa_soft_reset
1433  * Description:         Does the actual soft reset.
1434  *
1435  * Input:               sc      -- ptr to per ctlr structure
1436  * Output:              None
1437  * Return value:        0       -- success
1438  *                      non-zero-- failure
1439  */
1440 static int
1441 twa_soft_reset(struct twa_softc *sc)
1442 {
1443         u_int32_t       status_reg;
1444
1445         twa_dbg_dprint_enter(1, sc);
1446
1447         twa_printf(sc, "Resetting controller...\n");
1448         TWA_SOFT_RESET(sc);
1449
1450         if (twa_wait_status(sc, TWA_STATUS_MICROCONTROLLER_READY |
1451                                 TWA_STATUS_ATTENTION_INTERRUPT, 30)) {
1452                 twa_printf(sc, "Micro-ctlr not ready/No attn intr after reset.\n");
1453                 return(1);
1454         }
1455         TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT);
1456         if (twa_drain_response_queue(sc)) {
1457                 twa_printf(sc, "Can't drain response queue.\n");
1458                 return(1);
1459         }
1460         if (twa_drain_aen_queue(sc)) {
1461                 twa_printf(sc, "Can't drain AEN queue.\n");
1462                 return(1);
1463         }
1464         if (twa_find_aen(sc, TWA_AEN_SOFT_RESET)) {
1465                 twa_printf(sc, "Reset not reported by controller.\n");
1466                 return(1);
1467         }
1468         status_reg = TWA_READ_STATUS_REGISTER(sc);
1469         if (TWA_STATUS_ERRORS(status_reg) ||
1470                                 twa_check_ctlr_state(sc, status_reg)) {
1471                 twa_printf(sc, "Controller errors detected.\n");
1472                 return(1);
1473         }
1474         return(0);
1475 }
1476
1477
1478
1479 /*
1480  * Function name:       twa_submit_io
1481  * Description:         Wrapper to twa_start.
1482  *
1483  * Input:               tr      -- ptr to request pkt
1484  * Output:              None
1485  * Return value:        0       -- success
1486  *                      non-zero-- failure
1487  */
1488 int
1489 twa_submit_io(struct twa_request *tr)
1490 {
1491         int     error;
1492
1493         if ((error = twa_start(tr))) {
1494                 if (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_EXTERNAL) {
1495                         if (error == EBUSY)
1496                                 /*
1497                                  * Cmd queue is full.  Freeze the simq to
1498                                  * maintain ccb ordering.  The next ccb that
1499                                  * gets completed will unfreeze the simq.
1500                                  */
1501                                 twa_disallow_new_requests(tr->tr_sc);
1502                         else
1503                                 /* It's a controller error. */
1504                                 twa_printf(tr->tr_sc, "SCSI cmd = 0x%x: ERROR: (0x%02X: 0x%04X)\n",
1505                                         tr->tr_command->command.cmd_pkt_9k.cdb[0],
1506                                         TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
1507                                         error);
1508                         
1509                         tr->tr_error = error;
1510                         twa_scsi_complete(tr);
1511                 } else {
1512                         if (error == EBUSY)
1513                                 error = 0; /* the request will be in the pending queue */
1514                         else {
1515                                 twa_printf(tr->tr_sc, "cmd = 0x%x: ERROR: (0x%02X: 0x%04X)\n",
1516                                                 (tr->tr_cmd_pkt_type == TWA_CMD_PKT_TYPE_9K) ?
1517                                                 (tr->tr_command->command.cmd_pkt_9k.command.opcode) :
1518                                                 (tr->tr_command->command.cmd_pkt_7k.generic.opcode),
1519                                                 TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
1520                                                 tr->tr_error);
1521                                 tr->tr_error = error;
1522                         }
1523                 }
1524         }
1525         return(error);
1526 }
1527
1528
1529
1530 /*
1531  * Function name:       twa_start
1532  * Description:         Posts a cmd to firmware.
1533  *
1534  * Input:               tr      -- ptr to request pkt
1535  * Output:              None
1536  * Return value:        0       -- success
1537  *                      non-zero-- failure
1538  */
1539 int
1540 twa_start(struct twa_request *tr)
1541 {
1542         struct twa_softc        *sc = tr->tr_sc;
1543         u_int32_t               status_reg;
1544         int                     s;
1545         int                     error;
1546
1547         twa_dbg_dprint_enter(10, sc);
1548
1549         s = splcam();
1550         /* Check to see if we can post a command. */
1551         status_reg = TWA_READ_STATUS_REGISTER(sc);
1552         if ((error = twa_check_ctlr_state(sc, status_reg)))
1553                 goto out;
1554
1555         if (status_reg & TWA_STATUS_COMMAND_QUEUE_FULL) {
1556                 if ((tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_INTERNAL) ||
1557                         (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_IOCTL)) {
1558                         if (tr->tr_status != TWA_CMD_PENDING) {
1559                                 twa_dbg_dprint(2, sc, "pending internal/ioctl request");
1560                                 tr->tr_status = TWA_CMD_PENDING;
1561                                 twa_enqueue_pending(tr);
1562                         }
1563                         TWA_WRITE_CONTROL_REGISTER(sc,
1564                                         TWA_CONTROL_UNMASK_COMMAND_INTERRUPT);
1565                 }
1566                 error = EBUSY;
1567         } else {
1568                 /* Cmd queue is not full.  Post the command. */
1569                 TWA_WRITE_COMMAND_QUEUE(sc,
1570                         tr->tr_cmd_phys + sizeof(struct twa_command_header));
1571                 /* Mark the request as currently being processed. */
1572                 tr->tr_status = TWA_CMD_BUSY;
1573                 /* Move the request into the busy queue. */
1574                 twa_enqueue_busy(tr);
1575         }
1576
1577 out:
1578         splx(s);
1579         return(error);
1580 }
1581
1582
1583
1584 /*
1585  * Function name:       twa_done
1586  * Description:         Looks for cmd completions from fw; queues cmds completed
1587  *                      by fw into complete queue.
1588  *
1589  * Input:               sc      -- ptr to per ctlr structure
1590  * Output:              None
1591  * Return value:        0       -- no ctlr error
1592  *                      non-zero-- ctlr error
1593  */
1594 static int
1595 twa_done(struct twa_softc *sc)
1596 {
1597         union twa_response_queue        rq;
1598         struct twa_request              *tr;
1599         int                             s;
1600         int                             error = 0;
1601         u_int32_t                       status_reg;
1602     
1603         twa_dbg_dprint_enter(10, sc);
1604
1605         s = splcam();
1606         for (;;) {
1607                 status_reg = TWA_READ_STATUS_REGISTER(sc);
1608                 if ((error = twa_check_ctlr_state(sc, status_reg)))
1609                         break;
1610                 if (status_reg & TWA_STATUS_RESPONSE_QUEUE_EMPTY)
1611                         break;
1612                 /* Response queue is not empty. */
1613                 rq = TWA_READ_RESPONSE_QUEUE(sc);
1614                 tr = sc->twa_lookup[rq.u.response_id];  /* lookup the request */
1615                 if (tr->tr_status != TWA_CMD_BUSY)
1616                         twa_printf(sc, "ERROR: Unposted command completed!! req = %p; status = %d\n",
1617                                         tr, tr->tr_status);
1618                 tr->tr_status = TWA_CMD_COMPLETE;
1619                 /* Enqueue request in the complete queue. */
1620                 twa_remove_busy(tr);
1621                 twa_enqueue_complete(tr);
1622         }
1623         splx(s);
1624
1625         /* Complete this, and other requests in the complete queue. */
1626         twa_drain_complete_queue(sc);
1627         return(error);
1628 }
1629
1630
1631
1632 /*
1633  * Function name:       twa_drain_pending_queue
1634  * Description:         Kick starts any requests in the pending queue.
1635  *
1636  * Input:               sc      -- ptr to per ctlr structure
1637  * Output:              None
1638  * Return value:        0       -- all pending requests drained
1639  *                      non-zero-- otherwise
1640  */
1641 static int
1642 twa_drain_pending_queue(struct twa_softc *sc)
1643 {
1644         struct twa_request      *tr;
1645         int                     error = 0;
1646     
1647         twa_dbg_dprint_enter(10, sc);
1648         
1649         /*
1650          * Pull requests off the pending queue, and submit them.
1651          */
1652         while ((tr = twa_dequeue_pending(sc)) != NULL) {
1653                 if ((error = twa_start(tr))) {
1654                         if (error == EBUSY) {
1655                                 twa_dbg_dprint(2, sc, "Requeueing pending request");
1656                                 tr->tr_status = TWA_CMD_PENDING;
1657                                 twa_requeue_pending(tr);/* queue at the head */
1658                                 break;
1659                         } else {
1660                                 twa_printf(sc, "%s: twa_start returned 0x%x\n",
1661                                                         __func__, error);
1662                                 if (tr->tr_flags & TWA_CMD_SLEEP_ON_REQUEST)
1663                                         wakeup_one(tr);/* let the caller know it failed */
1664                                 tr->tr_error = error;
1665                                 error = 0;
1666                         }
1667                 }
1668         }
1669         return(error);
1670 }
1671
1672
1673
1674 /*
1675  * Function name:       twa_drain_complete_queue
1676  * Description:         Does unmapping for each request completed by fw,
1677  *                      and lets the request originators know of the completion.
1678  *
1679  * Input:               sc      -- ptr to per ctlr structure
1680  * Output:              None
1681  * Return value:        None
1682  */
1683 static void
1684 twa_drain_complete_queue(struct twa_softc *sc)
1685 {
1686         struct twa_request      *tr;
1687     
1688         twa_dbg_dprint_enter(10, sc);
1689
1690         /*
1691          * Pull commands off the completed list, dispatch them appropriately.
1692          */
1693         while ((tr = twa_dequeue_complete(sc)) != NULL) {
1694                 /* Unmap the command packet, and any associated data buffer. */
1695                 twa_unmap_request(tr);
1696
1697                 /* Call the callback, if there's one. */
1698                 if (tr->tr_callback)
1699                         tr->tr_callback(tr);
1700                 else
1701                         if (tr->tr_flags & TWA_CMD_SLEEP_ON_REQUEST) {
1702                                 /* Wake up the sleeping command originator. */
1703                                 twa_dbg_dprint(7, sc, "Waking up originator of request %p", tr);
1704                                 wakeup_one(tr);
1705                         }
1706         }
1707 }
1708
1709
1710
1711 /*
1712  * Function name:       twa_wait_status
1713  * Description:         Wait for a given status to show up in the fw status register.
1714  *
1715  * Input:               sc      -- ptr to per ctlr structure
1716  *                      status  -- status to look for
1717  *                      timeout -- max # of seconds to wait before giving up
1718  * Output:              None
1719  * Return value:        0       -- success
1720  *                      non-zero-- failure
1721  */
1722 static int
1723 twa_wait_status(struct twa_softc *sc, u_int32_t status, u_int32_t timeout)
1724 {
1725         time_t          end_time;
1726         u_int32_t       status_reg;
1727
1728         twa_dbg_dprint_enter(4, sc);
1729
1730         end_time = time_second + timeout;
1731         do {
1732                 status_reg = TWA_READ_STATUS_REGISTER(sc);
1733                 if ((status_reg & status) == status)/* got the required bit(s)? */
1734                         return(0);
1735                 DELAY(100000);
1736         } while (time_second <= end_time);
1737
1738         return(1);
1739 }
1740
1741
1742
1743 /*
1744  * Function name:       twa_drain_response_queue
1745  * Description:         Drain the response queue.
1746  *
1747  * Input:               sc      -- ptr to per ctlr structure
1748  * Output:              None
1749  * Return value:        0       -- success
1750  *                      non-zero-- failure
1751  */
1752 static int
1753 twa_drain_response_queue(struct twa_softc *sc)
1754 {
1755         union twa_response_queue        rq;
1756         u_int32_t                       status_reg;
1757
1758         twa_dbg_dprint_enter(4, sc);
1759
1760         for (;;) {
1761                 status_reg = TWA_READ_STATUS_REGISTER(sc);
1762                 if (twa_check_ctlr_state(sc, status_reg))
1763                         return(1);
1764                 if (status_reg & TWA_STATUS_RESPONSE_QUEUE_EMPTY)
1765                         return(0); /* no more response queue entries */
1766                 rq = TWA_READ_RESPONSE_QUEUE(sc);
1767         }
1768 }
1769
1770
1771
1772 /*
1773  * Function name:       twa_host_intr
1774  * Description:         This function gets called if we triggered an interrupt.
1775  *                      We don't use it as of now.
1776  *
1777  * Input:               sc      -- ptr to per ctlr structure
1778  * Output:              None
1779  * Return value:        None
1780  */
1781 static void
1782 twa_host_intr(struct twa_softc *sc)
1783 {
1784         twa_dbg_dprint_enter(6, sc);
1785
1786         TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_HOST_INTERRUPT);
1787 }
1788
1789
1790
1791 /*
1792  * Function name:       twa_attention_intr
1793  * Description:         This function gets called if the fw posted an AEN
1794  *                      (Asynchronous Event Notification).  It fetches
1795  *                      all the AEN's that the fw might have posted.
1796  *
1797  * Input:               sc      -- ptr to per ctlr structure
1798  * Output:              None
1799  * Return value:        None
1800  */
1801 static void
1802 twa_attention_intr(struct twa_softc *sc)
1803 {
1804         int     error;
1805
1806         twa_dbg_dprint_enter(6, sc);
1807
1808         if ((error = twa_fetch_aen(sc)))
1809                 twa_printf(sc, "Fetch AEN failed. error = 0x%x\n", error);
1810         TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT);
1811 }
1812
1813
1814
1815 /*
1816  * Function name:       twa_command_intr
1817  * Description:         This function gets called if we hit a queue full
1818  *                      condition earlier, and the fw is now ready for
1819  *                      new cmds.  Submits any pending requests.
1820  *
1821  * Input:               sc      -- ptr to per ctlr structure
1822  * Output:              None
1823  * Return value:        None
1824  */
1825 static void
1826 twa_command_intr(struct twa_softc *sc)
1827 {
1828         twa_dbg_dprint_enter(6, sc);
1829
1830         /* Start any requests that might be in the pending queue. */
1831         if (! twa_drain_pending_queue(sc))
1832                 TWA_WRITE_CONTROL_REGISTER(sc,
1833                                 TWA_CONTROL_MASK_COMMAND_INTERRUPT);
1834 }
1835
1836
1837
1838 /*
1839  * Function name:       twa_fetch_aen
1840  * Description:         Send down a Request Sense cmd to fw to fetch an AEN.
1841  *
1842  * Input:               sc      -- ptr to per ctlr structure
1843  * Output:              None
1844  * Return value:        0       -- success
1845  *                      non-zero-- failure
1846  */
1847 static int
1848 twa_fetch_aen(struct twa_softc *sc)
1849 {
1850         struct twa_request      *tr;
1851         int                     error = 0;
1852
1853         twa_dbg_dprint_enter(4, sc);
1854
1855         if ((tr = twa_get_request(sc)) == NULL)
1856                 return(EIO);
1857         tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
1858         tr->tr_callback = twa_aen_callback;
1859         if ((error = twa_send_scsi_cmd(tr, 0x03 /* REQUEST_SENSE */))) {
1860                 if (tr->tr_data)
1861                         free(tr->tr_data, M_DEVBUF);
1862                 twa_release_request(tr);
1863         }
1864         return(error);
1865 }
1866
1867
1868
1869 /*
1870  * Function name:       twa_aen_callback
1871  * Description:         Callback for requests to fetch AEN's.
1872  *
1873  * Input:               tr      -- ptr to completed request pkt
1874  * Output:              None
1875  * Return value:        None
1876  */
1877 static void
1878 twa_aen_callback(struct twa_request *tr)
1879 {
1880         struct twa_softc                *sc = tr->tr_sc;
1881         struct twa_command_header       *cmd_hdr = (struct twa_command_header *)(tr->tr_data);
1882         struct twa_command_9k           *cmd = &(tr->tr_command->command.cmd_pkt_9k);
1883         int                             i;
1884
1885         twa_dbg_dprint_enter(4, sc);
1886
1887         twa_dbg_dprint(4, sc, "req_id = 0x%x, status = 0x%x",
1888                                 cmd->request_id,
1889                                 cmd->status);
1890
1891         if (! cmd->status) {
1892                 if ((tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_9K) &&
1893                         (cmd->cdb[0] == 0x3 /* REQUEST_SENSE */))
1894                         twa_enqueue_aen(sc, cmd_hdr);
1895         } else {
1896                 cmd_hdr->err_specific_desc[sizeof(cmd_hdr->err_specific_desc) - 1] = '\0';
1897                 twa_printf(sc, "%s: cmd = 0x%x: ERROR: (0x%02X: 0x%04X): %s: %s\n",
1898                                 __func__, cmd->command.opcode,
1899                                 TWA_MESSAGE_SOURCE_CONTROLLER_ERROR,
1900                                 cmd_hdr->status_block.error,
1901                                 twa_find_msg_string(twa_error_table,
1902                                                 cmd_hdr->status_block.error),
1903                                 cmd_hdr->err_specific_desc);
1904                 twa_dbg_print(2, "sense info: ");
1905                 for (i = 0; i < 18; i++)
1906                         twa_dbg_print(2, "%x\t", tr->tr_command->cmd_hdr.sense_data[i]);
1907                 twa_dbg_print(2, ""); /* print new line */
1908                 for (i = 0; i < 128; i++)
1909                         twa_dbg_print(7, "%x\t", ((int8_t *)(tr->tr_data))[i]);
1910         }
1911
1912         if (tr->tr_data)
1913                 free(tr->tr_data, M_DEVBUF);
1914         twa_release_request(tr);
1915 }
1916
1917
1918
1919 /*
1920  * Function name:       twa_drain_aen_queue
1921  * Description:         Fetches all un-retrieved AEN's posted by fw.
1922  *
1923  * Input:               sc      -- ptr to per ctlr structure
1924  * Output:              None
1925  * Return value:        0       -- success
1926  *                      non-zero-- failure
1927  */
1928 static int
1929 twa_drain_aen_queue(struct twa_softc *sc)
1930 {
1931         struct twa_request              *tr;
1932         struct twa_command_header       *cmd_hdr;
1933         time_t                          end_time;
1934         int                             error = 0;
1935
1936         for (;;) {
1937                 if ((tr = twa_get_request(sc)) == NULL) {
1938                         error = EIO;
1939                         break;
1940                 }
1941                 tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
1942                 tr->tr_callback = NULL;
1943                 if ((error = twa_send_scsi_cmd(tr, 0x03 /* REQUEST_SENSE */))) {
1944                         twa_dbg_dprint(1, sc, "Cannot send command to fetch aen");
1945                         break;
1946                 }
1947
1948                 end_time = time_second + TWA_REQUEST_TIMEOUT_PERIOD;
1949                 do {
1950                         twa_done(tr->tr_sc);
1951                         if (tr->tr_status != TWA_CMD_BUSY)
1952                                 break;
1953                 } while (time_second <= end_time);
1954
1955                 if (tr->tr_status != TWA_CMD_COMPLETE) {
1956                         error = ETIMEDOUT;
1957                         break;
1958                 }
1959
1960                 if ((error = tr->tr_command->command.cmd_pkt_9k.status))
1961                         break;
1962
1963                 cmd_hdr = (struct twa_command_header *)(tr->tr_data);
1964                 if ((cmd_hdr->status_block.error) /* aen_code */
1965                                 == TWA_AEN_QUEUE_EMPTY)
1966                         break;
1967                 twa_enqueue_aen(sc, cmd_hdr);
1968
1969                 free(tr->tr_data, M_DEVBUF);
1970                 twa_release_request(tr);
1971         }
1972
1973         if (tr) {
1974                 if (tr->tr_data)
1975                         free(tr->tr_data, M_DEVBUF);
1976                 twa_release_request(tr);
1977         }
1978         return(error);
1979 }
1980
1981
1982
1983 /*
1984  * Function name:       twa_enqueue_aen
1985  * Description:         Queues AEN's to be supplied to user-space tools on request.
1986  *
1987  * Input:               sc      -- ptr to per ctlr structure
1988  *                      cmd_hdr -- ptr to hdr of fw cmd pkt, from where the AEN
1989  *                                 details can be retrieved.
1990  * Output:              None
1991  * Return value:        None
1992  */
1993 static void
1994 twa_enqueue_aen(struct twa_softc *sc, struct twa_command_header *cmd_hdr)
1995 {
1996         struct twa_event_packet *event;
1997         unsigned short          aen_code;
1998         unsigned long           local_time;
1999         unsigned long           sync_time;
2000         int                     s;
2001
2002         twa_dbg_dprint_enter(4, sc);
2003         s = splcam();
2004         aen_code = cmd_hdr->status_block.error;
2005
2006         switch (aen_code) {
2007         case TWA_AEN_SYNC_TIME_WITH_HOST:
2008                 twa_dbg_dprint(4, sc, "Received AEN_SYNC_TIME");
2009                 /* Calculate time (in seconds) since last Sunday 12.00 AM. */
2010                 local_time = time_second - (tz.tz_minuteswest * 60) -
2011                                         (wall_cmos_clock ? adjkerntz : 0);
2012                 sync_time = (local_time - (3 * 86400)) % 604800;
2013                 if (twa_set_param(sc, TWA_PARAM_TIME_TABLE,
2014                                         TWA_PARAM_TIME_SchedulerTime, 4,
2015                                         &sync_time, twa_aen_callback))
2016                         twa_printf(sc, "Unable to sync time with ctlr!\n");
2017                 break;
2018
2019         case TWA_AEN_QUEUE_EMPTY:
2020                 twa_dbg_dprint(4, sc, "AEN queue empty");
2021                 break;
2022
2023         default:
2024                 /* Queue the event. */
2025                 event = sc->twa_aen_queue[sc->twa_aen_head];
2026                 if (event->retrieved == TWA_AEN_NOT_RETRIEVED)
2027                         sc->twa_aen_queue_overflow = TRUE;
2028                 event->severity = cmd_hdr->status_block.substatus_block.severity;
2029                 local_time = time_second - (tz.tz_minuteswest * 60) -
2030                                         (wall_cmos_clock ? adjkerntz : 0);
2031                 event->time_stamp_sec = local_time;
2032                 event->aen_code = aen_code;
2033                 event->retrieved = TWA_AEN_NOT_RETRIEVED;
2034                 event->sequence_id = ++(sc->twa_current_sequence_id);
2035                 cmd_hdr->err_specific_desc[sizeof(cmd_hdr->err_specific_desc) - 1] = '\0';
2036                 event->parameter_len = strlen(cmd_hdr->err_specific_desc);
2037                 bcopy(cmd_hdr->err_specific_desc, event->parameter_data,
2038                                         event->parameter_len);
2039
2040                 twa_dbg_dprint(4, sc, "event = %x %x %x %x %x %x %x\n %s",
2041                                 event->sequence_id,
2042                                 event->time_stamp_sec,
2043                                 event->aen_code,
2044                                 event->severity,
2045                                 event->retrieved,
2046                                 event->repeat_count,
2047                                 event->parameter_len,
2048                                 event->parameter_data);
2049
2050                 twa_dbg_dprint(4, sc, "cmd_hdr = %x %lx %x %x %x %x %x\n %s",
2051                                 sc->twa_current_sequence_id,
2052                                 local_time,
2053                                 cmd_hdr->status_block.error,
2054                                 cmd_hdr->status_block.substatus_block.severity,
2055                                 TWA_AEN_NOT_RETRIEVED,
2056                                 0,
2057                                 strlen(cmd_hdr->err_specific_desc),
2058                                 cmd_hdr->err_specific_desc);
2059
2060                 /* Print the event. */
2061                 if (event->severity < TWA_AEN_SEVERITY_DEBUG)
2062                         twa_printf(sc,  "%s: (0x%02X: 0x%04X): %s: %s\n",
2063                                         twa_aen_severity_table[event->severity],
2064                                         TWA_MESSAGE_SOURCE_CONTROLLER_EVENT,
2065                                         aen_code,
2066                                         twa_find_msg_string(twa_aen_table, aen_code),
2067                                         event->parameter_data);
2068
2069                 if ((sc->twa_aen_head + 1) == TWA_Q_LENGTH)
2070                         sc->twa_aen_queue_wrapped = TRUE;
2071                 sc->twa_aen_head = (sc->twa_aen_head + 1) % TWA_Q_LENGTH;
2072                 break;
2073         } /* switch */
2074         splx(s);
2075 }
2076
2077
2078
2079 /*
2080  * Function name:       twa_find_aen
2081  * Description:         Reports whether a given AEN ever occurred.
2082  *
2083  * Input:               sc      -- ptr to per ctlr structure
2084  *                      aen_code-- AEN to look for
2085  * Output:              None
2086  * Return value:        0       -- success
2087  *                      non-zero-- failure
2088  */
2089 static int
2090 twa_find_aen(struct twa_softc *sc, u_int16_t aen_code)
2091 {
2092         u_int32_t       last_index;
2093         int             s;
2094         int             i;
2095
2096         s = splcam();
2097
2098         if (sc->twa_aen_queue_wrapped)
2099                 last_index = sc->twa_aen_head;
2100         else
2101                 last_index = 0;
2102
2103         i = sc->twa_aen_head;
2104         do {
2105                 i = (i + TWA_Q_LENGTH - 1) % TWA_Q_LENGTH;
2106                 if ((sc->twa_aen_queue[i])->aen_code == aen_code) {
2107                         splx(s);
2108                         return(0);
2109                 }
2110         } while (i != last_index);
2111
2112         splx(s);
2113         return(1);
2114 }
2115
2116
2117
2118 /*
2119  * Function name:       twa_find_msg_string
2120  * Description:         Looks up a given table, and returns the message string
2121  *                      corresponding to a given code (error code or AEN code).
2122  *
2123  * Input:               sc      -- ptr to per ctlr structure
2124  *                      code    -- code, the message string corresponding to
2125  *                                 which is to be returned.
2126  * Output:              None
2127  * Return value:        ptr to corresponding msg string -- success
2128  *                      NULL                            -- failure
2129  */
2130 char *
2131 twa_find_msg_string(struct twa_message *table, u_int16_t code)
2132 {
2133         int     i;
2134
2135         for (i = 0; table[i].message != NULL; i++)
2136                 if (table[i].code == code)
2137                         return(table[i].message);
2138
2139         return(table[i].message);
2140 }
2141
2142
2143
2144 /*
2145  * Function name:       twa_get_request
2146  * Description:         Gets a request pkt from the free queue.
2147  *
2148  * Input:               sc      -- ptr to per ctlr structure
2149  * Output:              None
2150  * Return value:        ptr to request pkt      -- success
2151  *                      NULL                    -- failure
2152  */
2153 struct twa_request *
2154 twa_get_request(struct twa_softc *sc)
2155 {
2156         struct twa_request      *tr;
2157
2158         twa_dbg_dprint_enter(4, sc);
2159
2160         /* Get a free request packet. */
2161         tr = twa_dequeue_free(sc);
2162
2163         /* Initialize some fields to their defaults. */
2164         if (tr) {
2165                 tr->tr_data = NULL;
2166                 tr->tr_real_data = NULL;
2167                 tr->tr_length = 0;
2168                 tr->tr_real_length = 0;
2169                 tr->tr_status = TWA_CMD_SETUP;/* command is in setup phase */
2170                 tr->tr_flags = 0;
2171                 tr->tr_error = 0;
2172                 tr->tr_private = NULL;
2173                 tr->tr_callback = NULL;
2174                 tr->tr_cmd_pkt_type = 0;
2175
2176                 /*
2177                  * Look at the status field in the command packet to see how
2178                  * it completed the last time it was used, and zero out only
2179                  * the portions that might have changed.  Note that we don't
2180                  * care to zero out the sglist.
2181                  */
2182                 if (tr->tr_command->command.cmd_pkt_9k.status)
2183                         bzero(tr->tr_command,
2184                                 sizeof(struct twa_command_header) + 28 /* max bytes before sglist */);
2185                 else
2186                         bzero(&(tr->tr_command->command), 28 /* max bytes before sglist */);
2187         }
2188         return(tr);
2189 }
2190
2191
2192
2193 /*
2194  * Function name:       twa_release_request
2195  * Description:         Puts a request pkt into the free queue.
2196  *
2197  * Input:               tr      -- ptr to request pkt to be freed
2198  * Output:              None
2199  * Return value:        None
2200  */
2201 void
2202 twa_release_request(struct twa_request *tr)
2203 {
2204         twa_dbg_dprint_enter(4, tr->tr_sc);
2205
2206         twa_enqueue_free(tr);
2207 }
2208
2209
2210
2211 /*
2212  * Function name:       twa_describe_controller
2213  * Description:         Describes the controller, in terms of its fw version,
2214  *                      BIOS version etc.
2215  *
2216  * Input:               sc      -- ptr to per ctlr structure
2217  * Output:              None
2218  * Return value:        None
2219  */
2220 void
2221 twa_describe_controller(struct twa_softc *sc)
2222 {
2223         struct twa_param_9k     *p[6];
2224         u_int8_t                num_ports = 0;
2225
2226         twa_dbg_dprint_enter(2, sc);
2227
2228         /* Get the port count. */
2229         p[0] = twa_get_param(sc, TWA_PARAM_CONTROLLER_TABLE,
2230                                 TWA_PARAM_CONTROLLER_PORT_COUNT, 1, NULL);
2231         if (p[0]) {
2232                 num_ports = *(u_int8_t *)(p[0]->data);
2233                 free(p[0], M_DEVBUF);
2234         }
2235
2236         /* Get the firmware and BIOS versions. */
2237         p[0] = twa_get_param(sc, TWA_PARAM_VERSION_TABLE,
2238                                 TWA_PARAM_VERSION_FW, 16, NULL);
2239         p[1] = twa_get_param(sc, TWA_PARAM_VERSION_TABLE,
2240                                 TWA_PARAM_VERSION_BIOS, 16, NULL);
2241
2242         twa_printf(sc, "%d ports, Firmware %.16s, BIOS %.16s\n",
2243                         num_ports, p[0]?(p[0]->data):NULL, p[1]?(p[1]->data):NULL);
2244         if (bootverbose) {
2245                 /* Get more versions. */
2246                 p[2] = twa_get_param(sc, TWA_PARAM_VERSION_TABLE,
2247                                         TWA_PARAM_VERSION_MONITOR, 16, NULL);
2248                 p[3] = twa_get_param(sc, TWA_PARAM_VERSION_TABLE,
2249                                         TWA_PARAM_VERSION_PCBA, 8, NULL);
2250                 p[4] = twa_get_param(sc, TWA_PARAM_VERSION_TABLE,
2251                                         TWA_PARAM_VERSION_ATA, 8, NULL);
2252                 p[5] = twa_get_param(sc, TWA_PARAM_VERSION_TABLE,
2253                                         TWA_PARAM_VERSION_PCI, 8, NULL);
2254
2255                 twa_printf(sc, "Monitor %.16s, PCB %.8s, Achip %.8s, Pchip %.8s\n",
2256                                 p[2]?(p[2]->data):NULL, p[3]?(p[3]->data):NULL,
2257                                 p[4]?(p[4]->data):NULL, p[5]?(p[5]->data):NULL);
2258
2259                 if (p[2])
2260                         free(p[2], M_DEVBUF);
2261                 if (p[3])
2262                         free(p[3], M_DEVBUF);
2263                 if (p[4])
2264                         free(p[4], M_DEVBUF);
2265                 if (p[5])
2266                         free(p[5], M_DEVBUF);
2267         }
2268         if (p[0])
2269                 free(p[0], M_DEVBUF);
2270         if (p[1])
2271                 free(p[1], M_DEVBUF);
2272 }
2273
2274
2275
2276 /*
2277  * Function name:       twa_check_ctlr_state
2278  * Description:         Makes sure that the fw status register reports a
2279  *                      proper status.
2280  *
2281  * Input:               sc              -- ptr to per ctlr structure
2282  *                      status_reg      -- value in the status register
2283  * Output:              None
2284  * Return value:        0       -- no errors
2285  *                      non-zero-- errors
2286  */
2287 static int
2288 twa_check_ctlr_state(struct twa_softc *sc, u_int32_t status_reg)
2289 {
2290         int             result = 0;
2291         static time_t   last_warning[2] = {0, 0};
2292
2293         /* Check if the 'micro-controller ready' bit is not set. */
2294         if ((status_reg & TWA_STATUS_EXPECTED_BITS) !=
2295                                 TWA_STATUS_EXPECTED_BITS) {
2296                 if (time_second > (last_warning[0] + 5)) {
2297                         twa_printf(sc, "Missing expected status bit(s) %b\n",
2298                                         ~status_reg & TWA_STATUS_EXPECTED_BITS,
2299                                         TWA_STATUS_BITS_DESCRIPTION);
2300                         last_warning[0] = time_second;
2301                 }
2302                 result = 1;
2303         }
2304
2305         /* Check if any error bits are set. */
2306         if ((status_reg & TWA_STATUS_UNEXPECTED_BITS) != 0) {
2307                 if (time_second > (last_warning[1] + 5)) {
2308                         twa_printf(sc, "Unexpected status bit(s) %b\n",
2309                                         status_reg & TWA_STATUS_UNEXPECTED_BITS,
2310                                         TWA_STATUS_BITS_DESCRIPTION);
2311                         last_warning[1] = time_second;
2312                 }
2313                 if (status_reg & TWA_STATUS_PCI_PARITY_ERROR_INTERRUPT) {
2314                         twa_printf(sc, "PCI parity error: clearing... Re-seat/move/replace card.\n");
2315                         TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_PARITY_ERROR);
2316                         twa_write_pci_config(sc, TWA_PCI_CONFIG_CLEAR_PARITY_ERROR, 2);
2317                 }
2318                 if (status_reg & TWA_STATUS_PCI_ABORT_INTERRUPT) {
2319                         twa_printf(sc, "PCI abort: clearing...\n");
2320                         TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_PCI_ABORT);
2321                         twa_write_pci_config(sc, TWA_PCI_CONFIG_CLEAR_PCI_ABORT, 2);
2322                 }
2323                 if (status_reg & TWA_STATUS_QUEUE_ERROR_INTERRUPT) {
2324                         twa_printf(sc, "Controller queue error: clearing...\n");
2325                         TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_PCI_ABORT);
2326                 }
2327                 if (status_reg & TWA_STATUS_SBUF_WRITE_ERROR) {
2328                         twa_printf(sc, "SBUF write error: clearing...\n");
2329                         TWA_WRITE_CONTROL_REGISTER(sc, TWA_CONTROL_CLEAR_SBUF_WRITE_ERROR);
2330                 }
2331                 if (status_reg & TWA_STATUS_MICROCONTROLLER_ERROR) {
2332                         twa_printf(sc, "Micro-controller error!\n");
2333                         result = 1;
2334                 }
2335         }
2336         return(result);
2337 }       
2338
2339
2340
2341 /*
2342  * Function name:       twa_print_controller
2343  * Description:         Prints the current status of the controller.
2344  *
2345  * Input:               sc      -- ptr to per ctlr structure
2346  * Output:              None
2347  * Return value:        None
2348  */
2349 void
2350 twa_print_controller(struct twa_softc *sc)
2351 {
2352         u_int32_t       status_reg;
2353
2354         /* Print current controller details. */
2355         status_reg = TWA_READ_STATUS_REGISTER(sc);
2356         twa_printf(sc, "status   %b\n", status_reg, TWA_STATUS_BITS_DESCRIPTION);
2357 #ifdef TWA_DEBUG
2358         twa_printf(sc, "q type    current  max\n");
2359         twa_printf(sc, "free      %04d     %04d\n",
2360                 sc->twa_qstats[TWAQ_FREE].q_length, sc->twa_qstats[TWAQ_FREE].q_max);
2361         twa_printf(sc, "busy      %04d     %04d\n",
2362                 sc->twa_qstats[TWAQ_BUSY].q_length, sc->twa_qstats[TWAQ_BUSY].q_max);
2363         twa_printf(sc, "pending   %04d     %04d\n",
2364                 sc->twa_qstats[TWAQ_PENDING].q_length, sc->twa_qstats[TWAQ_PENDING].q_max);
2365         twa_printf(sc, "complete  %04d     %04d\n",
2366                 sc->twa_qstats[TWAQ_COMPLETE].q_length, sc->twa_qstats[TWAQ_COMPLETE].q_max);
2367 #endif /* TWA_DEBUG */
2368         twa_printf(sc, "AEN queue head %d  tail %d\n",
2369                         sc->twa_aen_head, sc->twa_aen_tail);
2370 }       
2371
2372
2373
2374 /*
2375  * Function name:       twa_panic
2376  * Description:         Called when something is seriously wrong with the ctlr.
2377  *                      Hits the debugger if the debugger is turned on, else
2378  *                      resets the ctlr.
2379  *
2380  * Input:               sc      -- ptr to per ctlr structure
2381  *                      reason  -- string describing what went wrong
2382  * Output:              None
2383  * Return value:        None
2384  */
2385 static void
2386 twa_panic(struct twa_softc *sc, int8_t *reason)
2387 {
2388         twa_print_controller(sc);
2389 #ifdef TWA_DEBUG
2390         panic(reason);
2391 #else
2392         twa_printf(sc, "twa_panic: RESETTING CONTROLLER...\n");
2393         twa_reset(sc);
2394 #endif
2395 }
2396