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