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