Home | History | Annotate | Line # | Download | only in pci
twa.c revision 1.14
      1 /*	$NetBSD: twa.c,v 1.14 2006/10/11 05:12:06 jmcneill Exp $ */
      2 /*	$wasabi: twa.c,v 1.27 2006/07/28 18:17:21 wrstuden Exp $	*/
      3 
      4 /*-
      5  * Copyright (c) 2004 The NetBSD Foundation, Inc.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by Jordan Rhody of Wasabi Systems, Inc.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *        This product includes software developed by the NetBSD
     22  *        Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*-
     41  * Copyright (c) 2003-04 3ware, Inc.
     42  * Copyright (c) 2000 Michael Smith
     43  * Copyright (c) 2000 BSDi
     44  * All rights reserved.
     45  *
     46  * Redistribution and use in source and binary forms, with or without
     47  * modification, are permitted provided that the following conditions
     48  * are met:
     49  * 1. Redistributions of source code must retain the above copyright
     50  *    notice, this list of conditions and the following disclaimer.
     51  * 2. Redistributions in binary form must reproduce the above copyright
     52  *    notice, this list of conditions and the following disclaimer in the
     53  *    documentation and/or other materials provided with the distribution.
     54  *
     55  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65  * SUCH DAMAGE.
     66  *
     67  *	$FreeBSD: src/sys/dev/twa/twa.c,v 1.2 2004/04/02 15:09:57 des Exp $
     68  */
     69 
     70 /*
     71  * 3ware driver for 9000 series storage controllers.
     72  *
     73  * Author: Vinod Kashyap
     74  */
     75 
     76 #include <sys/cdefs.h>
     77 __KERNEL_RCSID(0, "$NetBSD: twa.c,v 1.14 2006/10/11 05:12:06 jmcneill Exp $");
     78 
     79 #include <sys/param.h>
     80 #include <sys/systm.h>
     81 #include <sys/kernel.h>
     82 #include <sys/device.h>
     83 #include <sys/queue.h>
     84 #include <sys/proc.h>
     85 #include <sys/bswap.h>
     86 #include <sys/buf.h>
     87 #include <sys/bufq.h>
     88 #include <sys/endian.h>
     89 #include <sys/malloc.h>
     90 #include <sys/conf.h>
     91 #include <sys/disk.h>
     92 #include <sys/sysctl.h>
     93 #include <sys/syslog.h>
     94 #if 1
     95 #include <sys/ktrace.h>
     96 #endif
     97 
     98 #include <uvm/uvm_extern.h>
     99 
    100 #include <machine/bus.h>
    101 
    102 #include <dev/pci/pcireg.h>
    103 #include <dev/pci/pcivar.h>
    104 #include <dev/pci/pcidevs.h>
    105 #include <dev/pci/twareg.h>
    106 #include <dev/pci/twavar.h>
    107 #include <dev/pci/twaio.h>
    108 
    109 #include <dev/scsipi/scsipi_all.h>
    110 #include <dev/scsipi/scsipi_disk.h>
    111 #include <dev/scsipi/scsipiconf.h>
    112 #include <dev/scsipi/scsi_spc.h>
    113 
    114 #include <dev/ldvar.h>
    115 
    116 #include "locators.h"
    117 
    118 #define	PCI_CBIO	0x10
    119 
    120 static int	twa_fetch_aen(struct twa_softc *);
    121 static void	twa_aen_callback(struct twa_request *);
    122 static int	twa_find_aen(struct twa_softc *sc, uint16_t);
    123 static uint16_t	twa_enqueue_aen(struct twa_softc *sc,
    124 			struct twa_command_header *);
    125 
    126 static void	twa_attach(struct device *, struct device *, void *);
    127 static void	twa_shutdown(void *);
    128 static int	twa_init_connection(struct twa_softc *, uint16_t, uint32_t,
    129 				    uint16_t, uint16_t, uint16_t, uint16_t, uint16_t *,
    130 					uint16_t *, uint16_t *, uint16_t *, uint32_t *);
    131 static int	twa_intr(void *);
    132 static int 	twa_match(struct device *, struct cfdata *, void *);
    133 static int	twa_reset(struct twa_softc *);
    134 
    135 static int	twa_print(void *, const char *);
    136 static int	twa_soft_reset(struct twa_softc *);
    137 
    138 static int	twa_check_ctlr_state(struct twa_softc *, uint32_t);
    139 static int	twa_get_param(struct twa_softc *, int, int, size_t,
    140 				void (* callback)(struct twa_request *),
    141 				struct twa_param_9k **);
    142 static int 	twa_set_param(struct twa_softc *, int, int, int, void *,
    143 				void (* callback)(struct twa_request *));
    144 static void	twa_describe_controller(struct twa_softc *);
    145 static int	twa_wait_status(struct twa_softc *, uint32_t, uint32_t);
    146 static int	twa_done(struct twa_softc *);
    147 #if 0
    148 static int	twa_flash_firmware(struct twa_softc *sc);
    149 static int	twa_hard_reset(struct twa_softc *sc);
    150 #endif
    151 
    152 extern struct	cfdriver twa_cd;
    153 extern uint32_t twa_fw_img_size;
    154 extern uint8_t	twa_fw_img[];
    155 
    156 CFATTACH_DECL(twa, sizeof(struct twa_softc),
    157     twa_match, twa_attach, NULL, NULL);
    158 
    159 /* FreeBSD driver revision for sysctl expected by the 3ware cli */
    160 const char twaver[] = "1.50.01.002";
    161 
    162 /* AEN messages. */
    163 static const struct twa_message	twa_aen_table[] = {
    164 	{0x0000, "AEN queue empty"},
    165 	{0x0001, "Controller reset occurred"},
    166 	{0x0002, "Degraded unit detected"},
    167 	{0x0003, "Controller error occured"},
    168 	{0x0004, "Background rebuild failed"},
    169 	{0x0005, "Background rebuild done"},
    170 	{0x0006, "Incomplete unit detected"},
    171 	{0x0007, "Background initialize done"},
    172 	{0x0008, "Unclean shutdown detected"},
    173 	{0x0009, "Drive timeout detected"},
    174 	{0x000A, "Drive error detected"},
    175 	{0x000B, "Rebuild started"},
    176 	{0x000C, "Background initialize started"},
    177 	{0x000D, "Entire logical unit was deleted"},
    178 	{0x000E, "Background initialize failed"},
    179 	{0x000F, "SMART attribute exceeded threshold"},
    180 	{0x0010, "Power supply reported AC under range"},
    181 	{0x0011, "Power supply reported DC out of range"},
    182 	{0x0012, "Power supply reported a malfunction"},
    183 	{0x0013, "Power supply predicted malfunction"},
    184 	{0x0014, "Battery charge is below threshold"},
    185 	{0x0015, "Fan speed is below threshold"},
    186 	{0x0016, "Temperature sensor is above threshold"},
    187 	{0x0017, "Power supply was removed"},
    188 	{0x0018, "Power supply was inserted"},
    189 	{0x0019, "Drive was removed from a bay"},
    190 	{0x001A, "Drive was inserted into a bay"},
    191 	{0x001B, "Drive bay cover door was opened"},
    192 	{0x001C, "Drive bay cover door was closed"},
    193 	{0x001D, "Product case was opened"},
    194 	{0x0020, "Prepare for shutdown (power-off)"},
    195 	{0x0021, "Downgrade UDMA mode to lower speed"},
    196 	{0x0022, "Upgrade UDMA mode to higher speed"},
    197 	{0x0023, "Sector repair completed"},
    198 	{0x0024, "Sbuf memory test failed"},
    199 	{0x0025, "Error flushing cached write data to disk"},
    200 	{0x0026, "Drive reported data ECC error"},
    201 	{0x0027, "DCB has checksum error"},
    202 	{0x0028, "DCB version is unsupported"},
    203 	{0x0029, "Background verify started"},
    204 	{0x002A, "Background verify failed"},
    205 	{0x002B, "Background verify done"},
    206 	{0x002C, "Bad sector overwritten during rebuild"},
    207 	{0x002E, "Replace failed because replacement drive too small"},
    208 	{0x002F, "Verify failed because array was never initialized"},
    209 	{0x0030, "Unsupported ATA drive"},
    210 	{0x0031, "Synchronize host/controller time"},
    211 	{0x0032, "Spare capacity is inadequate for some units"},
    212 	{0x0033, "Background migration started"},
    213 	{0x0034, "Background migration failed"},
    214 	{0x0035, "Background migration done"},
    215 	{0x0036, "Verify detected and fixed data/parity mismatch"},
    216 	{0x0037, "SO-DIMM incompatible"},
    217 	{0x0038, "SO-DIMM not detected"},
    218 	{0x0039, "Corrected Sbuf ECC error"},
    219 	{0x003A, "Drive power on reset detected"},
    220 	{0x003B, "Background rebuild paused"},
    221 	{0x003C, "Background initialize paused"},
    222 	{0x003D, "Background verify paused"},
    223 	{0x003E, "Background migration paused"},
    224 	{0x003F, "Corrupt flash file system detected"},
    225 	{0x0040, "Flash file system repaired"},
    226 	{0x0041, "Unit number assignments were lost"},
    227 	{0x0042, "Error during read of primary DCB"},
    228 	{0x0043, "Latent error found in backup DCB"},
    229 	{0x0044, "Battery voltage is normal"},
    230 	{0x0045, "Battery voltage is low"},
    231 	{0x0046, "Battery voltage is high"},
    232 	{0x0047, "Battery voltage is too low"},
    233 	{0x0048, "Battery voltage is too high"},
    234 	{0x0049, "Battery temperature is normal"},
    235 	{0x004A, "Battery temperature is low"},
    236 	{0x004B, "Battery temperature is high"},
    237 	{0x004C, "Battery temperature is too low"},
    238 	{0x004D, "Battery temperature is too high"},
    239 	{0x004E, "Battery capacity test started"},
    240 	{0x004F, "Cache synchronization skipped"},
    241 	{0x0050, "Battery capacity test completed"},
    242 	{0x0051, "Battery health check started"},
    243 	{0x0052, "Battery health check completed"},
    244 	{0x0053, "Need to do a capacity test"},
    245 	{0x0054, "Charge termination voltage is at high level"},
    246 	{0x0055, "Battery charging started"},
    247 	{0x0056, "Battery charging completed"},
    248 	{0x0057, "Battery charging fault"},
    249 	{0x0058, "Battery capacity is below warning level"},
    250 	{0x0059, "Battery capacity is below error level"},
    251 	{0x005A, "Battery is present"},
    252 	{0x005B, "Battery is not present"},
    253 	{0x005C, "Battery is weak"},
    254 	{0x005D, "Battery health check failed"},
    255 	{0x005E, "Cache synchronized after power fail"},
    256 	{0x005F, "Cache synchronization failed; some data lost"},
    257 	{0x0060, "Bad cache meta data checksum"},
    258 	{0x0061, "Bad cache meta data signature"},
    259 	{0x0062, "Cache meta data restore failed"},
    260 	{0x0063, "BBU not found after power fail"},
    261 	{0x00FC, "Recovered/finished array membership update"},
    262 	{0x00FD, "Handler lockup"},
    263 	{0x00FE, "Retrying PCI transfer"},
    264 	{0x00FF, "AEN queue is full"},
    265 	{0xFFFFFFFF, (char *)NULL}
    266 };
    267 
    268 /* AEN severity table. */
    269 static const char	*twa_aen_severity_table[] = {
    270 	"None",
    271 	"ERROR",
    272 	"WARNING",
    273 	"INFO",
    274 	"DEBUG",
    275 	(char *)NULL
    276 };
    277 
    278 /* Error messages. */
    279 static const struct twa_message	twa_error_table[] = {
    280 	{0x0100, "SGL entry contains zero data"},
    281 	{0x0101, "Invalid command opcode"},
    282 	{0x0102, "SGL entry has unaligned address"},
    283 	{0x0103, "SGL size does not match command"},
    284 	{0x0104, "SGL entry has illegal length"},
    285 	{0x0105, "Command packet is not aligned"},
    286 	{0x0106, "Invalid request ID"},
    287 	{0x0107, "Duplicate request ID"},
    288 	{0x0108, "ID not locked"},
    289 	{0x0109, "LBA out of range"},
    290 	{0x010A, "Logical unit not supported"},
    291 	{0x010B, "Parameter table does not exist"},
    292 	{0x010C, "Parameter index does not exist"},
    293 	{0x010D, "Invalid field in CDB"},
    294 	{0x010E, "Specified port has invalid drive"},
    295 	{0x010F, "Parameter item size mismatch"},
    296 	{0x0110, "Failed memory allocation"},
    297 	{0x0111, "Memory request too large"},
    298 	{0x0112, "Out of memory segments"},
    299 	{0x0113, "Invalid address to deallocate"},
    300 	{0x0114, "Out of memory"},
    301 	{0x0115, "Out of heap"},
    302 	{0x0120, "Double degrade"},
    303 	{0x0121, "Drive not degraded"},
    304 	{0x0122, "Reconstruct error"},
    305 	{0x0123, "Replace not accepted"},
    306 	{0x0124, "Replace drive capacity too small"},
    307 	{0x0125, "Sector count not allowed"},
    308 	{0x0126, "No spares left"},
    309 	{0x0127, "Reconstruct error"},
    310 	{0x0128, "Unit is offline"},
    311 	{0x0129, "Cannot update status to DCB"},
    312 	{0x0130, "Invalid stripe handle"},
    313 	{0x0131, "Handle that was not locked"},
    314 	{0x0132, "Handle that was not empy"},
    315 	{0x0133, "Handle has different owner"},
    316 	{0x0140, "IPR has parent"},
    317 	{0x0150, "Illegal Pbuf address alignment"},
    318 	{0x0151, "Illegal Pbuf transfer length"},
    319 	{0x0152, "Illegal Sbuf address alignment"},
    320 	{0x0153, "Illegal Sbuf transfer length"},
    321 	{0x0160, "Command packet too large"},
    322 	{0x0161, "SGL exceeds maximum length"},
    323 	{0x0162, "SGL has too many entries"},
    324 	{0x0170, "Insufficient resources for rebuilder"},
    325 	{0x0171, "Verify error (data != parity)"},
    326 	{0x0180, "Requested segment not in directory of this DCB"},
    327 	{0x0181, "DCB segment has unsupported version"},
    328 	{0x0182, "DCB segment has checksum error"},
    329 	{0x0183, "DCB support (settings) segment invalid"},
    330 	{0x0184, "DCB UDB (unit descriptor block) segment invalid"},
    331 	{0x0185, "DCB GUID (globally unique identifier) segment invalid"},
    332 	{0x01A0, "Could not clear Sbuf"},
    333 	{0x01C0, "Flash identify failed"},
    334 	{0x01C1, "Flash out of bounds"},
    335 	{0x01C2, "Flash verify error"},
    336 	{0x01C3, "Flash file object not found"},
    337 	{0x01C4, "Flash file already present"},
    338 	{0x01C5, "Flash file system full"},
    339 	{0x01C6, "Flash file not present"},
    340 	{0x01C7, "Flash file size error"},
    341 	{0x01C8, "Bad flash file checksum"},
    342 	{0x01CA, "Corrupt flash file system detected"},
    343 	{0x01D0, "Invalid field in parameter list"},
    344 	{0x01D1, "Parameter list length error"},
    345 	{0x01D2, "Parameter item is not changeable"},
    346 	{0x01D3, "Parameter item is not saveable"},
    347 	{0x0200, "UDMA CRC error"},
    348 	{0x0201, "Internal CRC error"},
    349 	{0x0202, "Data ECC error"},
    350 	{0x0203, "ADP level 1 error"},
    351 	{0x0204, "Port timeout"},
    352 	{0x0205, "Drive power on reset"},
    353 	{0x0206, "ADP level 2 error"},
    354 	{0x0207, "Soft reset failed"},
    355 	{0x0208, "Drive not ready"},
    356 	{0x0209, "Unclassified port error"},
    357 	{0x020A, "Drive aborted command"},
    358 	{0x0210, "Internal CRC error"},
    359 	{0x0211, "Host PCI bus abort"},
    360 	{0x0212, "Host PCI parity error"},
    361 	{0x0213, "Port handler error"},
    362 	{0x0214, "Token interrupt count error"},
    363 	{0x0215, "Timeout waiting for PCI transfer"},
    364 	{0x0216, "Corrected buffer ECC"},
    365 	{0x0217, "Uncorrected buffer ECC"},
    366 	{0x0230, "Unsupported command during flash recovery"},
    367 	{0x0231, "Next image buffer expected"},
    368 	{0x0232, "Binary image architecture incompatible"},
    369 	{0x0233, "Binary image has no signature"},
    370 	{0x0234, "Binary image has bad checksum"},
    371 	{0x0235, "Image downloaded overflowed buffer"},
    372 	{0x0240, "I2C device not found"},
    373 	{0x0241, "I2C transaction aborted"},
    374 	{0x0242, "SO-DIMM parameter(s) incompatible using defaults"},
    375 	{0x0243, "SO-DIMM unsupported"},
    376 	{0x0248, "SPI transfer status error"},
    377 	{0x0249, "SPI transfer timeout error"},
    378 	{0x0250, "Invalid unit descriptor size in CreateUnit"},
    379 	{0x0251, "Unit descriptor size exceeds data buffer in CreateUnit"},
    380 	{0x0252, "Invalid value in CreateUnit descriptor"},
    381 	{0x0253, "Inadequate disk space to support descriptor in CreateUnit"},
    382 	{0x0254, "Unable to create data channel for this unit descriptor"},
    383 	{0x0255, "CreateUnit descriptor specifies a drive already in use"},
    384        {0x0256, "Unable to write configuration to all disks during CreateUnit"},
    385 	{0x0257, "CreateUnit does not support this descriptor version"},
    386 	{0x0258, "Invalid subunit for RAID 0 or 5 in CreateUnit"},
    387 	{0x0259, "Too many descriptors in CreateUnit"},
    388 	{0x025A, "Invalid configuration specified in CreateUnit descriptor"},
    389 	{0x025B, "Invalid LBA offset specified in CreateUnit descriptor"},
    390 	{0x025C, "Invalid stripelet size specified in CreateUnit descriptor"},
    391 	{0x0260, "SMART attribute exceeded threshold"},
    392 	{0xFFFFFFFF, (char *)NULL}
    393 };
    394 
    395 struct twa_pci_identity {
    396 	uint32_t	vendor_id;
    397 	uint32_t	product_id;
    398 	const char	*name;
    399 };
    400 
    401 static const struct twa_pci_identity pci_twa_products[] = {
    402 	{ PCI_VENDOR_3WARE,
    403 	  PCI_PRODUCT_3WARE_9000,
    404 	  "3ware 9000 series",
    405 	},
    406 	{ PCI_VENDOR_3WARE,
    407 	  PCI_PRODUCT_3WARE_9550,
    408 	  "3ware 9550SX series",
    409 	},
    410 	{ 0,
    411 	  0,
    412 	  NULL,
    413 	},
    414 };
    415 
    416 
    417 static inline void
    418 twa_outl(struct twa_softc *sc, int off, uint32_t val)
    419 {
    420 
    421 	bus_space_write_4(sc->twa_bus_iot, sc->twa_bus_ioh, off, val);
    422 	bus_space_barrier(sc->twa_bus_iot, sc->twa_bus_ioh, off, 4,
    423 	    BUS_SPACE_BARRIER_WRITE);
    424 }
    425 
    426 static inline uint32_t	twa_inl(struct twa_softc *sc, int off)
    427 {
    428 
    429 	bus_space_barrier(sc->twa_bus_iot, sc->twa_bus_ioh, off, 4,
    430 	    BUS_SPACE_BARRIER_WRITE | BUS_SPACE_BARRIER_READ);
    431 	return (bus_space_read_4(sc->twa_bus_iot, sc->twa_bus_ioh, off));
    432 }
    433 
    434 void
    435 twa_request_wait_handler(struct twa_request *tr)
    436 {
    437 
    438 	wakeup(tr);
    439 }
    440 
    441 static int
    442 twa_match(struct device *parent, struct cfdata *cfdata, void *aux)
    443 {
    444 	int i;
    445 	struct pci_attach_args *pa = aux;
    446 	const struct twa_pci_identity *entry = 0;
    447 
    448 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_3WARE) {
    449 		for (i = 0; (pci_twa_products[i].product_id); i++) {
    450 			entry = &pci_twa_products[i];
    451 			if (entry->product_id == PCI_PRODUCT(pa->pa_id)) {
    452 				aprint_normal("%s: (rev. 0x%02x)\n",
    453 				    entry->name, PCI_REVISION(pa->pa_class));
    454 				return (1);
    455 			}
    456 		}
    457 	}
    458 	return (0);
    459 }
    460 
    461 static const char *
    462 twa_find_msg_string(const struct twa_message *table, uint16_t code)
    463 {
    464 	int	i;
    465 
    466 	for (i = 0; table[i].message != NULL; i++)
    467 		if (table[i].code == code)
    468 			return(table[i].message);
    469 
    470 	return(table[i].message);
    471 }
    472 
    473 void
    474 twa_release_request(struct twa_request *tr)
    475 {
    476 	int s;
    477 	struct twa_softc *sc;
    478 
    479 	sc = tr->tr_sc;
    480 
    481 	if ((tr->tr_flags & TWA_CMD_AEN) == 0) {
    482 		s = splbio();
    483 		TAILQ_INSERT_TAIL(&tr->tr_sc->twa_free, tr, tr_link);
    484 		splx(s);
    485 		if (__predict_false((tr->tr_sc->twa_sc_flags &
    486 		    TWA_STATE_REQUEST_WAIT) != 0)) {
    487 			tr->tr_sc->twa_sc_flags &= ~TWA_STATE_REQUEST_WAIT;
    488 			wakeup(&sc->twa_free);
    489 		}
    490 	} else
    491 		tr->tr_flags &= ~TWA_CMD_AEN_BUSY;
    492 }
    493 
    494 static void
    495 twa_unmap_request(struct twa_request *tr)
    496 {
    497 	struct twa_softc	*sc = tr->tr_sc;
    498 	uint8_t			cmd_status;
    499 	int s;
    500 
    501 	/* If the command involved data, unmap that too. */
    502 	if (tr->tr_data != NULL) {
    503 		if (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_9K)
    504 			cmd_status = tr->tr_command->command.cmd_pkt_9k.status;
    505 		else
    506 			cmd_status =
    507 			      tr->tr_command->command.cmd_pkt_7k.generic.status;
    508 
    509 		if (tr->tr_flags & TWA_CMD_DATA_OUT) {
    510 			bus_dmamap_sync(tr->tr_sc->twa_dma_tag, tr->tr_dma_map,
    511 				0, tr->tr_length, BUS_DMASYNC_POSTREAD);
    512 			/*
    513 			 * If we are using a bounce buffer, and we are reading
    514 			 * data, copy the real data in.
    515 			 */
    516 			if (tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED)
    517 				if (cmd_status == 0)
    518 					memcpy(tr->tr_real_data, tr->tr_data,
    519 						tr->tr_real_length);
    520 		}
    521 		if (tr->tr_flags & TWA_CMD_DATA_IN)
    522 			bus_dmamap_sync(tr->tr_sc->twa_dma_tag, tr->tr_dma_map,
    523 				0, tr->tr_length, BUS_DMASYNC_POSTWRITE);
    524 
    525 		bus_dmamap_unload(sc->twa_dma_tag, tr->tr_dma_map);
    526 	}
    527 
    528 	/* Free alignment buffer if it was used. */
    529 	if (tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED) {
    530 		s = splvm();
    531 		uvm_km_free(kmem_map, (vaddr_t)tr->tr_data,
    532 		    tr->tr_length, UVM_KMF_WIRED);
    533 		splx(s);
    534 		tr->tr_data = tr->tr_real_data;
    535 		tr->tr_length = tr->tr_real_length;
    536 	}
    537 }
    538 
    539 /*
    540  * Function name:	twa_wait_request
    541  * Description:		Sends down a firmware cmd, and waits for the completion,
    542  *			but NOT in a tight loop.
    543  *
    544  * Input:		tr	-- ptr to request pkt
    545  *			timeout -- max # of seconds to wait before giving up
    546  * Output:		None
    547  * Return value:	0	-- success
    548  *			non-zero-- failure
    549  */
    550 static int
    551 twa_wait_request(struct twa_request *tr, uint32_t timeout)
    552 {
    553 	time_t	end_time;
    554 	struct timeval	t1;
    555 	int	s, rv;
    556 
    557 	tr->tr_flags |= TWA_CMD_SLEEP_ON_REQUEST;
    558 	tr->tr_callback = twa_request_wait_handler;
    559 	tr->tr_status = TWA_CMD_BUSY;
    560 
    561 	rv = twa_map_request(tr);
    562 
    563 	if (rv != 0)
    564 		return (rv);
    565 
    566 	microtime(&t1);
    567 	end_time = t1.tv_usec +
    568 		(timeout * 1000 * 100);
    569 
    570 	while (tr->tr_status != TWA_CMD_COMPLETE) {
    571 		rv = tr->tr_error;
    572 		if (rv != 0)
    573 			return(rv);
    574 		if ((rv = tsleep(tr, PRIBIO, "twawait", timeout * hz)) == 0)
    575 			break;
    576 
    577 		if (rv == EWOULDBLOCK) {
    578 			/*
    579 			 * We will reset the controller only if the request has
    580 			 * already been submitted, so as to not lose the
    581 			 * request packet.  If a busy request timed out, the
    582 			 * reset will take care of freeing resources.  If a
    583 			 * pending request timed out, we will free resources
    584 			 * for that request, right here.  So, the caller is
    585 			 * expected to NOT cleanup when ETIMEDOUT is returned.
    586 			 */
    587 			if (tr->tr_status == TWA_CMD_BUSY)
    588 				twa_reset(tr->tr_sc);
    589 			else {
    590 				/* Request was never submitted.  Clean up. */
    591 				s = splbio();
    592 				TAILQ_REMOVE(&tr->tr_sc->twa_pending, tr,
    593 				    tr_link);
    594 				splx(s);
    595 
    596 				twa_unmap_request(tr);
    597 				if (tr->tr_data)
    598 					free(tr->tr_data, M_DEVBUF);
    599 
    600 				twa_release_request(tr);
    601 			}
    602 			return(ETIMEDOUT);
    603 		}
    604 		/*
    605 		 * Either the request got completed, or we were woken up by a
    606 		 * signal. Calculate the new timeout, in case it was the
    607 		 * latter.
    608 		 */
    609 		microtime(&t1);
    610 
    611 		timeout = (end_time - t1.tv_usec) / (1000 * 100);
    612 	}
    613 	return(rv);
    614 }
    615 
    616 /*
    617  * Function name:	twa_immediate_request
    618  * Description:		Sends down a firmware cmd, and waits for the completion
    619  *			in a tight loop.
    620  *
    621  * Input:		tr	-- ptr to request pkt
    622  *			timeout -- max # of seconds to wait before giving up
    623  * Output:		None
    624  * Return value:	0	-- success
    625  *			non-zero-- failure
    626  */
    627 static int
    628 twa_immediate_request(struct twa_request *tr, uint32_t timeout)
    629 {
    630 	struct timeval t1;
    631 	int	s = 0, rv = 0;
    632 
    633 	rv = twa_map_request(tr);
    634 
    635 	if (rv != 0)
    636 		return(rv);
    637 
    638 	timeout = (timeout * 10000 * 10);
    639 
    640 	microtime(&t1);
    641 
    642 	timeout += t1.tv_usec;
    643 
    644 	do {
    645 		rv = tr->tr_error;
    646 		if (rv != 0)
    647 			return(rv);
    648 		s = splbio();
    649 		twa_done(tr->tr_sc);
    650 		splx(s);
    651 		if (tr->tr_status == TWA_CMD_COMPLETE)
    652 			return(rv);
    653 		microtime(&t1);
    654 	} while (t1.tv_usec <= timeout);
    655 
    656 	/*
    657 	 * We will reset the controller only if the request has
    658 	 * already been submitted, so as to not lose the
    659 	 * request packet.  If a busy request timed out, the
    660 	 * reset will take care of freeing resources.  If a
    661 	 * pending request timed out, we will free resources
    662 	 * for that request, right here.  So, the caller is
    663 	 * expected to NOT cleanup when ETIMEDOUT is returned.
    664 	 */
    665 	rv = ETIMEDOUT;
    666 
    667 	if (tr->tr_status == TWA_CMD_BUSY)
    668 		twa_reset(tr->tr_sc);
    669 	else {
    670 		/* Request was never submitted.  Clean up. */
    671 		s = splbio();
    672 		TAILQ_REMOVE(&tr->tr_sc->twa_pending, tr, tr_link);
    673 		splx(s);
    674 		twa_unmap_request(tr);
    675 		if (tr->tr_data)
    676 			free(tr->tr_data, M_DEVBUF);
    677 
    678 		twa_release_request(tr);
    679 	}
    680 	return (rv);
    681 }
    682 
    683 static int
    684 twa_inquiry(struct twa_request *tr, int lunid)
    685 {
    686 	int error;
    687 	struct twa_command_9k *tr_9k_cmd;
    688 
    689 	if (tr->tr_data == NULL)
    690 		return (ENOMEM);
    691 
    692 	memset(tr->tr_data, 0, TWA_SECTOR_SIZE);
    693 
    694 	tr->tr_length = TWA_SECTOR_SIZE;
    695 	tr->tr_cmd_pkt_type = TWA_CMD_PKT_TYPE_9K;
    696 	tr->tr_flags |= TWA_CMD_DATA_IN;
    697 
    698 	tr_9k_cmd = &tr->tr_command->command.cmd_pkt_9k;
    699 
    700 	tr_9k_cmd->command.opcode = TWA_OP_EXECUTE_SCSI_COMMAND;
    701 	tr_9k_cmd->unit = lunid;
    702 	tr_9k_cmd->request_id = tr->tr_request_id;
    703 	tr_9k_cmd->status = 0;
    704 	tr_9k_cmd->sgl_offset = 16;
    705 	tr_9k_cmd->sgl_entries = 1;
    706 	/* create the CDB here */
    707 	tr_9k_cmd->cdb[0] = INQUIRY;
    708 	tr_9k_cmd->cdb[1] = ((lunid << 5) & 0x0e);
    709 	tr_9k_cmd->cdb[4] = 255;
    710 
    711 	/* XXXX setup page data no lun device
    712 	 * it seems 9000 series does not indicate
    713 	 * NOTPRESENT - need more investigation
    714 	 */
    715 	((struct scsipi_inquiry_data *)tr->tr_data)->device =
    716 		SID_QUAL_LU_NOTPRESENT;
    717 
    718 	error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
    719 
    720 	if (error != 0)
    721 		return (error);
    722 
    723 	if (((struct scsipi_inquiry_data *)tr->tr_data)->device ==
    724 		SID_QUAL_LU_NOTPRESENT)
    725 		error = 1;
    726 
    727 	return (error);
    728 }
    729 
    730 static int
    731 twa_print_inquiry_data(struct twa_softc *sc, struct scsipi_inquiry_data *scsipi)
    732 {
    733 
    734     printf("%s: %s\n", sc->twa_dv.dv_xname, scsipi->vendor);
    735 
    736     return (1);
    737 }
    738 
    739 
    740 static uint64_t
    741 twa_read_capacity(struct twa_request *tr, int lunid)
    742 {
    743 	int error;
    744 	struct twa_command_9k *tr_9k_cmd;
    745 	uint64_t array_size = 0LL;
    746 
    747 	if (tr->tr_data == NULL)
    748 		return (ENOMEM);
    749 
    750 	memset(tr->tr_data, 0, TWA_SECTOR_SIZE);
    751 
    752 	tr->tr_length = TWA_SECTOR_SIZE;
    753 	tr->tr_cmd_pkt_type = TWA_CMD_PKT_TYPE_9K;
    754 	tr->tr_flags |= TWA_CMD_DATA_OUT;
    755 
    756 	tr_9k_cmd = &tr->tr_command->command.cmd_pkt_9k;
    757 
    758 	tr_9k_cmd->command.opcode = TWA_OP_EXECUTE_SCSI_COMMAND;
    759 	tr_9k_cmd->unit = lunid;
    760 	tr_9k_cmd->request_id = tr->tr_request_id;
    761 	tr_9k_cmd->status = 0;
    762 	tr_9k_cmd->sgl_offset = 16;
    763 	tr_9k_cmd->sgl_entries = 1;
    764 	/* create the CDB here */
    765 	tr_9k_cmd->cdb[0] = READ_CAPACITY_16;
    766 	tr_9k_cmd->cdb[1] = ((lunid << 5) & 0x0e) | SRC16_SERVICE_ACTION;
    767 
    768 	error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
    769 
    770 	if (error == 0) {
    771 #if BYTE_ORDER == BIG_ENDIAN
    772 		array_size = bswap64(_8btol(
    773 		    ((struct scsipi_read_capacity_16_data *)tr->tr_data->addr) + 1);
    774 #else
    775 		array_size = _8btol(((struct scsipi_read_capacity_16_data *)
    776 				tr->tr_data)->addr) + 1;
    777 #endif
    778 	}
    779 	return (array_size);
    780 }
    781 
    782 static int
    783 twa_request_sense(struct twa_request *tr, int lunid)
    784 {
    785 	int error = 1;
    786 	struct twa_command_9k *tr_9k_cmd;
    787 
    788 	if (tr->tr_data == NULL)
    789 		return (error);
    790 
    791 	memset(tr->tr_data, 0, TWA_SECTOR_SIZE);
    792 
    793 	tr->tr_length = TWA_SECTOR_SIZE;
    794 	tr->tr_cmd_pkt_type = TWA_CMD_PKT_TYPE_9K;
    795 	tr->tr_flags |= TWA_CMD_DATA_OUT;
    796 
    797 	tr_9k_cmd = &tr->tr_command->command.cmd_pkt_9k;
    798 
    799 	tr_9k_cmd->command.opcode = TWA_OP_EXECUTE_SCSI_COMMAND;
    800 	tr_9k_cmd->unit = lunid;
    801 	tr_9k_cmd->request_id = tr->tr_request_id;
    802 	tr_9k_cmd->status = 0;
    803 	tr_9k_cmd->sgl_offset = 16;
    804 	tr_9k_cmd->sgl_entries = 1;
    805 	/* create the CDB here */
    806 	tr_9k_cmd->cdb[0] = SCSI_REQUEST_SENSE;
    807 	tr_9k_cmd->cdb[1] = ((lunid << 5) & 0x0e);
    808 	tr_9k_cmd->cdb[4] = 255;
    809 
    810 	/*XXX AEN notification called in interrupt context
    811 	 * so just queue the request. Return as quickly
    812 	 * as possible from interrupt
    813 	 */
    814 	if ((tr->tr_flags & TWA_CMD_AEN) != 0)
    815 		error = twa_map_request(tr);
    816  	else
    817 		error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
    818 
    819 	return (error);
    820 }
    821 
    822 static int
    823 twa_alloc_req_pkts(struct twa_softc *sc, int num_reqs)
    824 {
    825 	struct twa_request	*tr;
    826 	struct twa_command_packet *tc;
    827 	bus_dma_segment_t	seg;
    828 	size_t max_segs, max_xfer;
    829 	int	i, rv, rseg, size;
    830 
    831 	if ((sc->twa_req_buf = malloc(num_reqs * sizeof(struct twa_request),
    832 					M_DEVBUF, M_NOWAIT)) == NULL)
    833 		return(ENOMEM);
    834 
    835 	size = num_reqs * sizeof(struct twa_command_packet);
    836 
    837 	/* Allocate memory for cmd pkts. */
    838 	if ((rv = bus_dmamem_alloc(sc->twa_dma_tag,
    839 		size, PAGE_SIZE, 0, &seg,
    840 		1, &rseg, BUS_DMA_NOWAIT)) != 0){
    841 			aprint_error("%s: unable to allocate "
    842 				"command packets, rv = %d\n",
    843 				sc->twa_dv.dv_xname, rv);
    844 			return (ENOMEM);
    845 	}
    846 
    847 	if ((rv = bus_dmamem_map(sc->twa_dma_tag,
    848 		&seg, rseg, size, (caddr_t *)&sc->twa_cmds,
    849 		BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    850 			aprint_error("%s: unable to map commands, rv = %d\n",
    851 				sc->twa_dv.dv_xname, rv);
    852 			return (1);
    853 	}
    854 
    855 	if ((rv = bus_dmamap_create(sc->twa_dma_tag,
    856 		size, num_reqs, size,
    857 		0, BUS_DMA_NOWAIT, &sc->twa_cmd_map)) != 0) {
    858 			aprint_error("%s: unable to create command DMA map, "
    859 				"rv = %d\n", sc->twa_dv.dv_xname, rv);
    860 			return (ENOMEM);
    861 	}
    862 
    863 	if ((rv = bus_dmamap_load(sc->twa_dma_tag, sc->twa_cmd_map,
    864 		sc->twa_cmds, size, NULL,
    865 		BUS_DMA_NOWAIT)) != 0) {
    866 			aprint_error("%s: unable to load command DMA map, "
    867 				"rv = %d\n", sc->twa_dv.dv_xname, rv);
    868 			return (1);
    869 	}
    870 
    871 	if ((uintptr_t)sc->twa_cmds % TWA_ALIGNMENT) {
    872 		aprint_error("%s: DMA map memory not aligned on %d boundary\n",
    873 			sc->twa_dv.dv_xname, TWA_ALIGNMENT);
    874 
    875 		return (1);
    876 	}
    877 	tc = sc->twa_cmd_pkt_buf = (struct twa_command_packet *)sc->twa_cmds;
    878 	sc->twa_cmd_pkt_phys = sc->twa_cmd_map->dm_segs[0].ds_addr;
    879 
    880 	memset(sc->twa_req_buf, 0, num_reqs * sizeof(struct twa_request));
    881 	memset(sc->twa_cmd_pkt_buf, 0,
    882 		num_reqs * sizeof(struct twa_command_packet));
    883 
    884 	sc->sc_twa_request = sc->twa_req_buf;
    885 	max_segs = twa_get_maxsegs();
    886 	max_xfer = twa_get_maxxfer(max_segs);
    887 
    888 	for (i = 0; i < num_reqs; i++, tc++) {
    889 		tr = &(sc->twa_req_buf[i]);
    890 		tr->tr_command = tc;
    891 		tr->tr_cmd_phys = sc->twa_cmd_pkt_phys +
    892 				(i * sizeof(struct twa_command_packet));
    893 		tr->tr_request_id = i;
    894 		tr->tr_sc = sc;
    895 
    896 		/*
    897 		 * Create a map for data buffers.  maxsize (256 * 1024) used in
    898 		 * bus_dma_tag_create above should suffice the bounce page needs
    899 		 * for data buffers, since the max I/O size we support is 128KB.
    900 		 * If we supported I/O's bigger than 256KB, we would have to
    901 		 * create a second dma_tag, with the appropriate maxsize.
    902 		 */
    903 		if ((rv = bus_dmamap_create(sc->twa_dma_tag,
    904 			max_xfer, max_segs, 1, 0, BUS_DMA_NOWAIT,
    905 			&tr->tr_dma_map)) != 0) {
    906 				aprint_error("%s: unable to create command "
    907 					"DMA map, rv = %d\n",
    908 					sc->twa_dv.dv_xname, rv);
    909 				return (ENOMEM);
    910 		}
    911 		/* Insert request into the free queue. */
    912 		if (i != 0) {
    913 			sc->twa_lookup[i] = tr;
    914 			twa_release_request(tr);
    915 		} else
    916 			tr->tr_flags |= TWA_CMD_AEN;
    917 	}
    918 	return(0);
    919 }
    920 
    921 static void
    922 twa_recompute_openings(struct twa_softc *sc)
    923 {
    924 	struct twa_drive *td;
    925 	int unit;
    926 	int openings;
    927 
    928 	if (sc->sc_nunits != 0)
    929 		openings = ((TWA_Q_LENGTH / 2) / sc->sc_nunits);
    930 	else
    931 		openings = 0;
    932 	if (openings == sc->sc_openings)
    933 		return;
    934 	sc->sc_openings = openings;
    935 
    936 #ifdef TWA_DEBUG
    937 	printf("%s: %d array%s, %d openings per array\n",
    938 	    sc->twa_dv.dv_xname, sc->sc_nunits,
    939 	    sc->sc_nunits == 1 ? "" : "s", sc->sc_openings);
    940 #endif
    941 	for (unit = 0; unit < TWA_MAX_UNITS; unit++) {
    942 		td = &sc->sc_units[unit];
    943 		if (td->td_dev != NULL)
    944 			(*td->td_callbacks->tcb_openings)(td->td_dev,
    945 				sc->sc_openings);
    946 	}
    947 }
    948 
    949 static int
    950 twa_request_bus_scan(struct twa_softc *sc)
    951 {
    952 	struct twa_drive *td;
    953 	struct twa_request *tr;
    954 	struct twa_attach_args twaa;
    955 	int locs[TWACF_NLOCS];
    956 	int s, unit;
    957 
    958 	s = splbio();
    959 	for (unit = 0; unit < TWA_MAX_UNITS; unit++) {
    960 
    961 		if ((tr = twa_get_request(sc, 0)) == NULL) {
    962 			splx(s);
    963 			return (EIO);
    964 		}
    965 
    966 		tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
    967 
    968 		tr->tr_data = malloc(TWA_SECTOR_SIZE, M_DEVBUF, M_NOWAIT);
    969 
    970 		if (tr->tr_data == NULL) {
    971 			twa_release_request(tr);
    972 			splx(s);
    973 			return (ENOMEM);
    974 		}
    975 		td = &sc->sc_units[unit];
    976 
    977 		if (twa_inquiry(tr, unit) == 0) {
    978 			if (td->td_dev == NULL) {
    979             			twa_print_inquiry_data(sc,
    980 				   ((struct scsipi_inquiry_data *)tr->tr_data));
    981 
    982 				sc->sc_nunits++;
    983 
    984 				sc->sc_units[unit].td_size =
    985 					twa_read_capacity(tr, unit);
    986 
    987 				twaa.twaa_unit = unit;
    988 
    989 				twa_recompute_openings(sc);
    990 
    991 				locs[TWACF_UNIT] = unit;
    992 
    993 				sc->sc_units[unit].td_dev =
    994 				    config_found_sm_loc(&sc->twa_dv, "twa",
    995 				    locs, &twaa, twa_print, config_stdsubmatch);
    996 			}
    997 		} else {
    998 			if (td->td_dev != NULL) {
    999 				sc->sc_nunits--;
   1000 
   1001 				(void) config_detach(td->td_dev, DETACH_FORCE);
   1002 				td->td_dev = NULL;
   1003 				td->td_size = 0;
   1004 
   1005 				twa_recompute_openings(sc);
   1006 			}
   1007 		}
   1008 		free(tr->tr_data, M_DEVBUF);
   1009 
   1010 		twa_release_request(tr);
   1011 	}
   1012 	splx(s);
   1013 
   1014 	return (0);
   1015 }
   1016 
   1017 
   1018 #ifdef	DIAGNOSTIC
   1019 static inline void
   1020 twa_check_busy_q(struct twa_request *tr)
   1021 {
   1022 	struct twa_request *rq;
   1023 	struct twa_softc *sc = tr->tr_sc;
   1024 
   1025 	TAILQ_FOREACH(rq, &sc->twa_busy, tr_link) {
   1026 		if (tr->tr_request_id == rq->tr_request_id) {
   1027 			panic("cannot submit same request more than once");
   1028 		} else if (tr->bp == rq->bp && tr->bp != 0) {
   1029 			/* XXX A check for 0 for the buf ptr is needed to
   1030 			 * guard against ioctl requests with a buf ptr of
   1031 			 * 0 and also aen notifications. Looking for
   1032 			 * external cmds only.
   1033 			 */
   1034 			panic("cannot submit same buf more than once");
   1035 		} else {
   1036 			/* Empty else statement */
   1037 		}
   1038 	}
   1039 }
   1040 #endif
   1041 
   1042 static int
   1043 twa_start(struct twa_request *tr)
   1044 {
   1045 	struct twa_softc	*sc = tr->tr_sc;
   1046 	uint32_t		status_reg;
   1047 	int			s;
   1048 	int			error;
   1049 
   1050 	s = splbio();
   1051 	/* Check to see if we can post a command. */
   1052 	status_reg = twa_inl(sc, TWA_STATUS_REGISTER_OFFSET);
   1053 	if ((error = twa_check_ctlr_state(sc, status_reg)))
   1054 		goto out;
   1055 
   1056 	if (status_reg & TWA_STATUS_COMMAND_QUEUE_FULL) {
   1057 			if (tr->tr_status != TWA_CMD_PENDING) {
   1058 				tr->tr_status = TWA_CMD_PENDING;
   1059 				TAILQ_INSERT_TAIL(&tr->tr_sc->twa_pending,
   1060 					tr, tr_link);
   1061 			}
   1062 			twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
   1063 					TWA_CONTROL_UNMASK_COMMAND_INTERRUPT);
   1064 			error = EBUSY;
   1065 	} else {
   1066 	   	bus_dmamap_sync(sc->twa_dma_tag, sc->twa_cmd_map,
   1067 			(caddr_t)tr->tr_command - sc->twa_cmds,
   1068 			sizeof(struct twa_command_packet),
   1069 			BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   1070 
   1071 		/* Cmd queue is not full.  Post the command. */
   1072 		TWA_WRITE_COMMAND_QUEUE(sc, tr->tr_cmd_phys +
   1073 			sizeof(struct twa_command_header));
   1074 
   1075 		/* Mark the request as currently being processed. */
   1076 		tr->tr_status = TWA_CMD_BUSY;
   1077 
   1078 #ifdef	DIAGNOSTIC
   1079 		twa_check_busy_q(tr);
   1080 #endif
   1081 
   1082 		/* Move the request into the busy queue. */
   1083 		TAILQ_INSERT_TAIL(&tr->tr_sc->twa_busy, tr, tr_link);
   1084 	}
   1085 out:
   1086 	splx(s);
   1087 	return(error);
   1088 }
   1089 
   1090 static int
   1091 twa_drain_response_queue(struct twa_softc *sc)
   1092 {
   1093 	union twa_response_queue	rq;
   1094 	uint32_t			status_reg;
   1095 
   1096 	for (;;) {
   1097 		status_reg = twa_inl(sc, TWA_STATUS_REGISTER_OFFSET);
   1098 		if (twa_check_ctlr_state(sc, status_reg))
   1099 			return(1);
   1100 		if (status_reg & TWA_STATUS_RESPONSE_QUEUE_EMPTY)
   1101 			return(0); /* no more response queue entries */
   1102 		rq = (union twa_response_queue)twa_inl(sc,
   1103 		    TWA_RESPONSE_QUEUE_OFFSET);
   1104 	}
   1105 }
   1106 
   1107 static void
   1108 twa_drain_busy_queue(struct twa_softc *sc)
   1109 {
   1110 	struct twa_request	*tr;
   1111 
   1112 	/* Walk the busy queue. */
   1113 
   1114 	while ((tr = TAILQ_FIRST(&sc->twa_busy)) != NULL) {
   1115 		TAILQ_REMOVE(&sc->twa_busy, tr, tr_link);
   1116 
   1117 		twa_unmap_request(tr);
   1118 		if ((tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_INTERNAL) ||
   1119 			(tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_IOCTL)) {
   1120 			/* It's an internal/ioctl request.  Simply free it. */
   1121 			if (tr->tr_data)
   1122 				free(tr->tr_data, M_DEVBUF);
   1123 			twa_release_request(tr);
   1124 		} else {
   1125 			/* It's a SCSI request.  Complete it. */
   1126 			tr->tr_command->command.cmd_pkt_9k.status = EIO;
   1127 			if (tr->tr_callback)
   1128 				tr->tr_callback(tr);
   1129 		}
   1130 	}
   1131 }
   1132 
   1133 static int
   1134 twa_drain_pending_queue(struct twa_softc *sc)
   1135 {
   1136 	struct twa_request	*tr;
   1137 	int			s, error = 0;
   1138 
   1139 	/*
   1140 	 * Pull requests off the pending queue, and submit them.
   1141 	 */
   1142 	s = splbio();
   1143 	while ((tr = TAILQ_FIRST(&sc->twa_pending)) != NULL) {
   1144 		TAILQ_REMOVE(&sc->twa_pending, tr, tr_link);
   1145 
   1146 		if ((error = twa_start(tr))) {
   1147 			if (error == EBUSY) {
   1148 				tr->tr_status = TWA_CMD_PENDING;
   1149 
   1150 				/* queue at the head */
   1151 				TAILQ_INSERT_HEAD(&tr->tr_sc->twa_pending,
   1152 					tr, tr_link);
   1153 				error = 0;
   1154 				break;
   1155 			} else {
   1156 				if (tr->tr_flags & TWA_CMD_SLEEP_ON_REQUEST) {
   1157 					tr->tr_error = error;
   1158 					tr->tr_callback(tr);
   1159 					error = EIO;
   1160 				}
   1161 			}
   1162 		}
   1163 	}
   1164 	splx(s);
   1165 
   1166 	return(error);
   1167 }
   1168 
   1169 static int
   1170 twa_drain_aen_queue(struct twa_softc *sc)
   1171 {
   1172 	int				s, error = 0;
   1173 	struct twa_request		*tr;
   1174 	struct twa_command_header	*cmd_hdr;
   1175 	struct timeval	t1;
   1176 	uint32_t		timeout;
   1177 
   1178 	for (;;) {
   1179 		if ((tr = twa_get_request(sc, 0)) == NULL) {
   1180 			error = EIO;
   1181 			break;
   1182 		}
   1183 		tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
   1184 		tr->tr_callback = NULL;
   1185 
   1186 		tr->tr_data = malloc(TWA_SECTOR_SIZE, M_DEVBUF, M_NOWAIT);
   1187 
   1188 		if (tr->tr_data == NULL) {
   1189 			error = 1;
   1190 			goto out;
   1191 		}
   1192 
   1193 		if (twa_request_sense(tr, 0) != 0) {
   1194 			error = 1;
   1195 			break;
   1196 		}
   1197 
   1198 		timeout = (1000/*ms*/ * 100/*us*/ * TWA_REQUEST_TIMEOUT_PERIOD);
   1199 
   1200 		microtime(&t1);
   1201 
   1202 		timeout += t1.tv_usec;
   1203 
   1204 		do {
   1205 			s = splbio();
   1206 			twa_done(tr->tr_sc);
   1207 			splx(s);
   1208 			if (tr->tr_status != TWA_CMD_BUSY)
   1209 				break;
   1210 			microtime(&t1);
   1211 		} while (t1.tv_usec <= timeout);
   1212 
   1213 		if (tr->tr_status != TWA_CMD_COMPLETE) {
   1214 			error = ETIMEDOUT;
   1215 			break;
   1216 		}
   1217 
   1218 		if ((error = tr->tr_command->command.cmd_pkt_9k.status))
   1219 			break;
   1220 
   1221 		cmd_hdr = (struct twa_command_header *)(tr->tr_data);
   1222 		if ((cmd_hdr->status_block.error) /* aen_code */
   1223 				== TWA_AEN_QUEUE_EMPTY)
   1224 			break;
   1225 		(void)twa_enqueue_aen(sc, cmd_hdr);
   1226 
   1227 		free(tr->tr_data, M_DEVBUF);
   1228 		twa_release_request(tr);
   1229 	}
   1230 out:
   1231 	if (tr) {
   1232 		if (tr->tr_data)
   1233 			free(tr->tr_data, M_DEVBUF);
   1234 
   1235 		twa_release_request(tr);
   1236 	}
   1237 	return(error);
   1238 }
   1239 
   1240 
   1241 #ifdef		DIAGNOSTIC
   1242 static void
   1243 twa_check_response_q(struct twa_request *tr, int clear)
   1244 {
   1245 	int j;
   1246 	static int i = 0;
   1247 	static struct twa_request	*req = 0;
   1248 	static struct buf		*hist[255];
   1249 
   1250 
   1251 	if (clear) {
   1252 		i = 0;
   1253 		for (j = 0; j < 255; j++)
   1254 			hist[j] = 0;
   1255 		return;
   1256 	}
   1257 
   1258 	if (req == 0)
   1259 		req = tr;
   1260 
   1261 	if ((tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_EXTERNAL) != 0) {
   1262 		if (req->tr_request_id == tr->tr_request_id)
   1263 			panic("req id: %d on controller queue twice",
   1264 		    	    tr->tr_request_id);
   1265 
   1266 		for (j = 0; j < i; j++)
   1267 			if (tr->bp == hist[j])
   1268 				panic("req id: %d buf found twice",
   1269 		    	    	    tr->tr_request_id);
   1270 		}
   1271 	req = tr;
   1272 
   1273 	hist[i++] = req->bp;
   1274 }
   1275 #endif
   1276 
   1277 static int
   1278 twa_done(struct twa_softc *sc)
   1279 {
   1280 	union twa_response_queue	rq;
   1281 	struct twa_request		*tr;
   1282 	int				rv = 0;
   1283 	uint32_t			status_reg;
   1284 
   1285 	for (;;) {
   1286 		status_reg = twa_inl(sc, TWA_STATUS_REGISTER_OFFSET);
   1287 		if ((rv = twa_check_ctlr_state(sc, status_reg)))
   1288 			break;
   1289 		if (status_reg & TWA_STATUS_RESPONSE_QUEUE_EMPTY)
   1290 			break;
   1291 		/* Response queue is not empty. */
   1292 		rq = (union twa_response_queue)twa_inl(sc,
   1293 			TWA_RESPONSE_QUEUE_OFFSET);
   1294 		tr = sc->sc_twa_request + rq.u.response_id;
   1295 #ifdef		DIAGNOSTIC
   1296 		twa_check_response_q(tr, 0);
   1297 #endif
   1298 		/* Unmap the command packet, and any associated data buffer. */
   1299 		twa_unmap_request(tr);
   1300 
   1301 		tr->tr_status = TWA_CMD_COMPLETE;
   1302 		TAILQ_REMOVE(&tr->tr_sc->twa_busy, tr, tr_link);
   1303 
   1304 		if (tr->tr_callback)
   1305 			tr->tr_callback(tr);
   1306 	}
   1307 	(void)twa_drain_pending_queue(sc);
   1308 
   1309 #ifdef		DIAGNOSTIC
   1310 	twa_check_response_q(NULL, 1);
   1311 #endif
   1312 	return(rv);
   1313 }
   1314 
   1315 /*
   1316  * Function name:	twa_init_ctlr
   1317  * Description:		Establishes a logical connection with the controller.
   1318  *			If bundled with firmware, determines whether or not
   1319  *			to flash firmware, based on arch_id, fw SRL (Spec.
   1320  *			Revision Level), branch & build #'s.  Also determines
   1321  *			whether or not the driver is compatible with the
   1322  *			firmware on the controller, before proceeding to work
   1323  *			with it.
   1324  *
   1325  * Input:		sc	-- ptr to per ctlr structure
   1326  * Output:		None
   1327  * Return value:	0	-- success
   1328  *			non-zero-- failure
   1329  */
   1330 static int
   1331 twa_init_ctlr(struct twa_softc *sc)
   1332 {
   1333 	uint16_t	fw_on_ctlr_srl = 0;
   1334 	uint16_t	fw_on_ctlr_arch_id = 0;
   1335 	uint16_t	fw_on_ctlr_branch = 0;
   1336 	uint16_t	fw_on_ctlr_build = 0;
   1337 	uint32_t	init_connect_result = 0;
   1338 	int		error = 0;
   1339 #if 0
   1340 	int8_t		fw_flashed = FALSE;
   1341 	int8_t		fw_flash_failed = FALSE;
   1342 #endif
   1343 
   1344 	/* Wait for the controller to become ready. */
   1345 	if (twa_wait_status(sc, TWA_STATUS_MICROCONTROLLER_READY,
   1346 					TWA_REQUEST_TIMEOUT_PERIOD)) {
   1347 		return(ENXIO);
   1348 	}
   1349 	/* Drain the response queue. */
   1350 	if (twa_drain_response_queue(sc))
   1351 		return(1);
   1352 
   1353 	/* Establish a logical connection with the controller. */
   1354 	if ((error = twa_init_connection(sc, TWA_INIT_MESSAGE_CREDITS,
   1355 			TWA_EXTENDED_INIT_CONNECT, TWA_CURRENT_FW_SRL,
   1356 			TWA_9000_ARCH_ID, TWA_CURRENT_FW_BRANCH,
   1357 			TWA_CURRENT_FW_BUILD, &fw_on_ctlr_srl,
   1358 			&fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
   1359 			&fw_on_ctlr_build, &init_connect_result))) {
   1360 		return(error);
   1361 	}
   1362 #if 0
   1363 	if ((init_connect_result & TWA_BUNDLED_FW_SAFE_TO_FLASH) &&
   1364 		(init_connect_result & TWA_CTLR_FW_RECOMMENDS_FLASH)) {
   1365 		/*
   1366 		 * The bundled firmware is safe to flash, and the firmware
   1367 		 * on the controller recommends a flash.  So, flash!
   1368 		 */
   1369 		printf("%s: flashing bundled firmware...\n",
   1370 		    sc->twa_dv.dv_xname);
   1371 
   1372 		if ((error = twa_flash_firmware(sc))) {
   1373 			fw_flash_failed = TRUE;
   1374 
   1375 			printf("%s: unable to flash bundled firmware.\n",
   1376 			    sc->twa_dv.dv_xname);
   1377 		} else {
   1378 			printf("%s: successfully flashed bundled firmware.\n",
   1379 				 sc->twa_dv.dv_xname);
   1380 			fw_flashed = TRUE;
   1381 		}
   1382 	}
   1383 	if (fw_flashed) {
   1384 		/* The firmware was flashed.  Have the new image loaded */
   1385 		error = twa_hard_reset(sc);
   1386 		if (error == 0)
   1387 			error = twa_init_ctlr(sc);
   1388 		/*
   1389 		 * If hard reset of controller failed, we need to return.
   1390 		 * Otherwise, the above recursive call to twa_init_ctlr will
   1391 		 * have completed the rest of the initialization (starting
   1392 		 * from twa_drain_aen_queue below).  Don't do it again.
   1393 		 * Just return.
   1394 		 */
   1395 		return(error);
   1396 	} else {
   1397 		/*
   1398 		 * Either we are not bundled with a firmware image, or
   1399 		 * the bundled firmware is not safe to flash,
   1400 		 * or flash failed for some reason.  See if we can at
   1401 		 * least work with the firmware on the controller in the
   1402 		 * current mode.
   1403 		 */
   1404 		if (init_connect_result & TWA_CTLR_FW_COMPATIBLE) {
   1405 			/* Yes, we can.  Make note of the operating mode. */
   1406 			sc->working_srl = TWA_CURRENT_FW_SRL;
   1407 			sc->working_branch = TWA_CURRENT_FW_BRANCH;
   1408 			sc->working_build = TWA_CURRENT_FW_BUILD;
   1409 		} else {
   1410 			/*
   1411 			 * No, we can't.  See if we can at least work with
   1412 			 * it in the base mode.  We should never come here
   1413 			 * if firmware has just been flashed.
   1414 			 */
   1415 			printf("%s: Driver/Firmware mismatch.  Negotiating "
   1416 			    "for base level.\n", sc->twa_dv.dv_xname);
   1417 			if ((error = twa_init_connection(sc,
   1418 			    TWA_INIT_MESSAGE_CREDITS,
   1419 			    TWA_EXTENDED_INIT_CONNECT, TWA_BASE_FW_SRL,
   1420 			    TWA_9000_ARCH_ID, TWA_BASE_FW_BRANCH,
   1421 			    TWA_BASE_FW_BUILD, &fw_on_ctlr_srl,
   1422 			    &fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
   1423 			    &fw_on_ctlr_build, &init_connect_result))) {
   1424 				printf("%s: can't initialize connection in "
   1425 				    "base mode.\n", sc->twa_dv.dv_xname);
   1426 				return(error);
   1427 			}
   1428 			if (!(init_connect_result & TWA_CTLR_FW_COMPATIBLE)) {
   1429 				/*
   1430 				 * The firmware on the controller is not even
   1431 				 * compatible with our base mode.  We cannot
   1432 				 * work with it.  Bail...
   1433 				 */
   1434 				printf("Incompatible firmware on controller\n");
   1435 #ifdef TWA_FLASH_FIRMWARE
   1436 				if (fw_flash_failed)
   1437 					printf("...and could not flash bundled "
   1438 					    "firmware.\n");
   1439 				else
   1440 					printf("...and bundled firmware not "
   1441 					    "safe to flash.\n");
   1442 #endif /* TWA_FLASH_FIRMWARE */
   1443 				return(1);
   1444 			}
   1445 			/*
   1446 			 * We can work with this firmware, but only in
   1447 			 * base mode.
   1448 			 */
   1449 			sc->working_srl = TWA_BASE_FW_SRL;
   1450 			sc->working_branch = TWA_BASE_FW_BRANCH;
   1451 			sc->working_build = TWA_BASE_FW_BUILD;
   1452 			sc->twa_operating_mode = TWA_BASE_MODE;
   1453 		}
   1454 	}
   1455 #endif
   1456 	twa_drain_aen_queue(sc);
   1457 
   1458 	/* Set controller state to initialized. */
   1459 	sc->twa_state &= ~TWA_STATE_SHUTDOWN;
   1460 	return(0);
   1461 }
   1462 
   1463 static int
   1464 twa_setup(struct twa_softc *sc)
   1465 {
   1466 	struct tw_cl_event_packet *aen_queue;
   1467 	uint32_t		i = 0;
   1468 	int			error = 0;
   1469 
   1470 	/* Initialize request queues. */
   1471 	TAILQ_INIT(&sc->twa_free);
   1472 	TAILQ_INIT(&sc->twa_busy);
   1473 	TAILQ_INIT(&sc->twa_pending);
   1474 
   1475 	sc->sc_nunits = 0;
   1476 	sc->twa_sc_flags = 0;
   1477 
   1478 	if (twa_alloc_req_pkts(sc, TWA_Q_LENGTH)) {
   1479 
   1480 		return(ENOMEM);
   1481 	}
   1482 
   1483 	/* Allocate memory for the AEN queue. */
   1484 	if ((aen_queue = malloc(sizeof(struct tw_cl_event_packet) *
   1485 	    TWA_Q_LENGTH, M_DEVBUF, M_WAITOK)) == NULL) {
   1486 		/*
   1487 		 * This should not cause us to return error.  We will only be
   1488 		 * unable to support AEN's.  But then, we will have to check
   1489 		 * time and again to see if we can support AEN's, if we
   1490 		 * continue.  So, we will just return error.
   1491 		 */
   1492 		return (ENOMEM);
   1493 	}
   1494 	/* Initialize the aen queue. */
   1495 	memset(aen_queue, 0, sizeof(struct tw_cl_event_packet) * TWA_Q_LENGTH);
   1496 
   1497 	for (i = 0; i < TWA_Q_LENGTH; i++)
   1498 		sc->twa_aen_queue[i] = &(aen_queue[i]);
   1499 
   1500 	twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
   1501 		TWA_CONTROL_DISABLE_INTERRUPTS);
   1502 
   1503 	/* Initialize the controller. */
   1504 	if ((error = twa_init_ctlr(sc))) {
   1505 		/* Soft reset the controller, and try one more time. */
   1506 
   1507 		printf("%s: controller initialization failed. "
   1508 		    "Retrying initialization\n", sc->twa_dv.dv_xname);
   1509 
   1510 		if ((error = twa_soft_reset(sc)) == 0)
   1511 			error = twa_init_ctlr(sc);
   1512 	}
   1513 
   1514 	twa_describe_controller(sc);
   1515 
   1516 	error = twa_request_bus_scan(sc);
   1517 
   1518 	twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
   1519 		TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT |
   1520 		TWA_CONTROL_UNMASK_RESPONSE_INTERRUPT |
   1521 		TWA_CONTROL_ENABLE_INTERRUPTS);
   1522 
   1523 	return (error);
   1524 }
   1525 
   1526 void *twa_sdh;
   1527 
   1528 static void
   1529 twa_attach(struct device *parent, struct device *self, void *aux)
   1530 {
   1531 	struct pci_attach_args *pa;
   1532 	struct twa_softc *sc;
   1533 	pci_chipset_tag_t pc;
   1534 	pcireg_t csr;
   1535 	pci_intr_handle_t ih;
   1536 	const char *intrstr;
   1537 	struct ctlname ctlnames[] = CTL_NAMES;
   1538 	const struct sysctlnode *node;
   1539 	int i;
   1540 
   1541 	sc = (struct twa_softc *)self;
   1542 
   1543 	pa = aux;
   1544 	pc = pa->pa_pc;
   1545 	sc->pc = pa->pa_pc;
   1546 	sc->tag = pa->pa_tag;
   1547 	sc->twa_dma_tag = pa->pa_dmat;
   1548 
   1549 	aprint_naive(": RAID controller\n");
   1550 	aprint_normal(": 3ware Apache\n");
   1551 
   1552 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3WARE_9000) {
   1553 		if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_IO, 0,
   1554 	    	    &sc->twa_bus_iot, &sc->twa_bus_ioh, NULL, NULL)) {
   1555 			aprint_error("%s: can't map i/o space\n",
   1556 			    sc->twa_dv.dv_xname);
   1557 			return;
   1558 		}
   1559 	} else if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3WARE_9550) {
   1560 		if (pci_mapreg_map(pa, PCI_MAPREG_START + 0x08,
   1561 	    	    PCI_MAPREG_MEM_TYPE_64BIT, 0, &sc->twa_bus_iot,
   1562 		    &sc->twa_bus_ioh, NULL, NULL)) {
   1563 			aprint_error("%s: can't map mem space\n",
   1564 			    sc->twa_dv.dv_xname);
   1565 			return;
   1566 		}
   1567 	} else {
   1568 		aprint_error("%s: product id 0x%02x not recognized\n",
   1569 		    sc->twa_dv.dv_xname, PCI_PRODUCT(pa->pa_id));
   1570 		return;
   1571 	}
   1572 	/* Enable the device. */
   1573 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
   1574 
   1575 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
   1576 	    csr | PCI_COMMAND_MASTER_ENABLE);
   1577 
   1578 	/* Map and establish the interrupt. */
   1579 	if (pci_intr_map(pa, &ih)) {
   1580 		aprint_error("%s: can't map interrupt\n", sc->twa_dv.dv_xname);
   1581 		return;
   1582 	}
   1583 	intrstr = pci_intr_string(pc, ih);
   1584 
   1585 	sc->twa_ih = pci_intr_establish(pc, ih, IPL_BIO, twa_intr, sc);
   1586 	if (sc->twa_ih == NULL) {
   1587 		aprint_error("%s: can't establish interrupt%s%s\n",
   1588 			sc->twa_dv.dv_xname,
   1589 			(intrstr) ? " at " : "",
   1590 			(intrstr) ? intrstr : "");
   1591 		return;
   1592 	}
   1593 
   1594 	if (intrstr != NULL)
   1595 		aprint_normal("%s: interrupting at %s\n",
   1596 			sc->twa_dv.dv_xname, intrstr);
   1597 
   1598 	twa_setup(sc);
   1599 
   1600 	if (twa_sdh == NULL)
   1601 		twa_sdh = shutdownhook_establish(twa_shutdown, NULL);
   1602 
   1603 	/* sysctl set-up for 3ware cli */
   1604 	if (sysctl_createv(NULL, 0, NULL, NULL,
   1605 				CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw",
   1606 				NULL, NULL, 0, NULL, 0,
   1607 				CTL_HW, CTL_EOL) != 0) {
   1608 		printf("%s: could not create %s sysctl node\n",
   1609 			sc->twa_dv.dv_xname, ctlnames[CTL_HW].ctl_name);
   1610 		return;
   1611 	}
   1612 	if (sysctl_createv(NULL, 0, NULL, &node,
   1613         			0, CTLTYPE_NODE, sc->twa_dv.dv_xname,
   1614         			SYSCTL_DESCR("twa driver information"),
   1615         			NULL, 0, NULL, 0,
   1616 				CTL_HW, CTL_CREATE, CTL_EOL) != 0) {
   1617                 printf("%s: could not create %s.%s sysctl node\n",
   1618 			sc->twa_dv.dv_xname, ctlnames[CTL_HW].ctl_name,
   1619 			sc->twa_dv.dv_xname);
   1620 		return;
   1621 	}
   1622 	if ((i = sysctl_createv(NULL, 0, NULL, NULL,
   1623         			0, CTLTYPE_STRING, "driver_version",
   1624         			SYSCTL_DESCR("twa driver version"),
   1625         			NULL, 0, &twaver, 0,
   1626 				CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL))
   1627 				!= 0) {
   1628                 printf("%s: could not create %s.%s.driver_version sysctl\n",
   1629 			sc->twa_dv.dv_xname, ctlnames[CTL_HW].ctl_name,
   1630 			sc->twa_dv.dv_xname);
   1631 		return;
   1632 	}
   1633 
   1634 	return;
   1635 }
   1636 
   1637 static void
   1638 twa_shutdown(void *arg)
   1639 {
   1640 	extern struct cfdriver twa_cd;
   1641 	struct twa_softc *sc;
   1642 	int i, rv, unit;
   1643 
   1644 	for (i = 0; i < twa_cd.cd_ndevs; i++) {
   1645 		if ((sc = device_lookup(&twa_cd, i)) == NULL)
   1646 			continue;
   1647 
   1648 		for (unit = 0; unit < TWA_MAX_UNITS; unit++)
   1649 			if (sc->sc_units[unit].td_dev != NULL)
   1650 				(void) config_detach(sc->sc_units[unit].td_dev,
   1651 					DETACH_FORCE | DETACH_QUIET);
   1652 
   1653 		twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
   1654 			TWA_CONTROL_DISABLE_INTERRUPTS);
   1655 
   1656 		/* Let the controller know that we are going down. */
   1657 		rv = twa_init_connection(sc, TWA_SHUTDOWN_MESSAGE_CREDITS,
   1658 				0, 0, 0, 0, 0,
   1659 				NULL, NULL, NULL, NULL, NULL);
   1660 	}
   1661 }
   1662 
   1663 void
   1664 twa_register_callbacks(struct twa_softc *sc, int unit,
   1665     const struct twa_callbacks *tcb)
   1666 {
   1667 
   1668 	sc->sc_units[unit].td_callbacks = tcb;
   1669 }
   1670 
   1671 /*
   1672  * Print autoconfiguration message for a sub-device
   1673  */
   1674 static int
   1675 twa_print(void *aux, const char *pnp)
   1676 {
   1677 	struct twa_attach_args *twaa;
   1678 
   1679 	twaa = aux;
   1680 
   1681 	if (pnp !=NULL)
   1682 		aprint_normal("block device at %s\n", pnp);
   1683 	aprint_normal(" unit %d\n", twaa->twaa_unit);
   1684 	return (UNCONF);
   1685 }
   1686 
   1687 static void
   1688 twa_fillin_sgl(struct twa_sg *sgl, bus_dma_segment_t *segs, int nsegments)
   1689 {
   1690 	int	i;
   1691 	for (i = 0; i < nsegments; i++) {
   1692 		sgl[i].address = segs[i].ds_addr;
   1693 		sgl[i].length = (uint32_t)(segs[i].ds_len);
   1694 	}
   1695 }
   1696 
   1697 static int
   1698 twa_submit_io(struct twa_request *tr)
   1699 {
   1700 	int	error;
   1701 
   1702 	if ((error = twa_start(tr))) {
   1703 		if (error == EBUSY)
   1704 			error = 0; /* request is in the pending queue */
   1705 		else {
   1706 			tr->tr_error = error;
   1707 		}
   1708 	}
   1709 	return(error);
   1710 }
   1711 
   1712 /*
   1713  * Function name:	twa_setup_data_dmamap
   1714  * Description:		Callback of bus_dmamap_load for the buffer associated
   1715  *			with data.  Updates the cmd pkt (size/sgl_entries
   1716  *			fields, as applicable) to reflect the number of sg
   1717  *			elements.
   1718  *
   1719  * Input:		arg	-- ptr to request pkt
   1720  *			segs	-- ptr to a list of segment descriptors
   1721  *			nsegments--# of segments
   1722  *			error	-- 0 if no errors encountered before callback,
   1723  *				   non-zero if errors were encountered
   1724  * Output:		None
   1725  * Return value:	None
   1726  */
   1727 static int
   1728 twa_setup_data_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments,
   1729     int error)
   1730 {
   1731 	struct twa_request		*tr = (struct twa_request *)arg;
   1732 	struct twa_command_packet	*cmdpkt = tr->tr_command;
   1733 	struct twa_command_9k		*cmd9k;
   1734 	union twa_command_7k		*cmd7k;
   1735 	uint8_t				sgl_offset;
   1736 
   1737 	if (error == EFBIG) {
   1738 		tr->tr_error = error;
   1739 		goto out;
   1740 	}
   1741 
   1742 	if (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_9K) {
   1743 		cmd9k = &(cmdpkt->command.cmd_pkt_9k);
   1744 		twa_fillin_sgl(&(cmd9k->sg_list[0]), segs, nsegments);
   1745 		cmd9k->sgl_entries += nsegments - 1;
   1746 	} else {
   1747 		/* It's a 7000 command packet. */
   1748 		cmd7k = &(cmdpkt->command.cmd_pkt_7k);
   1749 		if ((sgl_offset = cmdpkt->command.cmd_pkt_7k.generic.sgl_offset))
   1750 			twa_fillin_sgl((struct twa_sg *)
   1751 					(((uint32_t *)cmd7k) + sgl_offset),
   1752 					segs, nsegments);
   1753 		/* Modify the size field, based on sg address size. */
   1754 		cmd7k->generic.size +=
   1755 			((TWA_64BIT_ADDRESSES ? 3 : 2) * nsegments);
   1756 	}
   1757 
   1758 	if (tr->tr_flags & TWA_CMD_DATA_IN)
   1759 		bus_dmamap_sync(tr->tr_sc->twa_dma_tag, tr->tr_dma_map, 0,
   1760 			tr->tr_length, BUS_DMASYNC_PREREAD);
   1761 	if (tr->tr_flags & TWA_CMD_DATA_OUT) {
   1762 		/*
   1763 		 * If we're using an alignment buffer, and we're
   1764 		 * writing data, copy the real data out.
   1765 		 */
   1766 		if (tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED)
   1767 			memcpy(tr->tr_data, tr->tr_real_data,
   1768 				tr->tr_real_length);
   1769 		bus_dmamap_sync(tr->tr_sc->twa_dma_tag, tr->tr_dma_map, 0,
   1770 			tr->tr_length, BUS_DMASYNC_PREWRITE);
   1771 	}
   1772 	error = twa_submit_io(tr);
   1773 
   1774 out:
   1775 	if (error) {
   1776 		twa_unmap_request(tr);
   1777 		/*
   1778 		 * If the caller had been returned EINPROGRESS, and he has
   1779 		 * registered a callback for handling completion, the callback
   1780 		 * will never get called because we were unable to submit the
   1781 		 * request.  So, free up the request right here.
   1782 		 */
   1783 		if ((tr->tr_flags & TWA_CMD_IN_PROGRESS) && (tr->tr_callback))
   1784 			twa_release_request(tr);
   1785 	}
   1786 	return (error);
   1787 }
   1788 
   1789 /*
   1790  * Function name:	twa_map_request
   1791  * Description:		Maps a cmd pkt and data associated with it, into
   1792  *			DMA'able memory.
   1793  *
   1794  * Input:		tr	-- ptr to request pkt
   1795  * Output:		None
   1796  * Return value:	0	-- success
   1797  *			non-zero-- failure
   1798  */
   1799 int
   1800 twa_map_request(struct twa_request *tr)
   1801 {
   1802 	struct twa_softc	*sc = tr->tr_sc;
   1803 	int			 s, rv, error = 0;
   1804 
   1805 	/* If the command involves data, map that too. */
   1806 	if (tr->tr_data != NULL) {
   1807 
   1808 		if (((u_long)tr->tr_data & (511)) != 0) {
   1809 			tr->tr_flags |= TWA_CMD_DATA_COPY_NEEDED;
   1810 			tr->tr_real_data = tr->tr_data;
   1811 			tr->tr_real_length = tr->tr_length;
   1812 			s = splvm();
   1813 			tr->tr_data = (void *)uvm_km_alloc(kmem_map,
   1814 			    tr->tr_length, 512, UVM_KMF_NOWAIT|UVM_KMF_WIRED);
   1815 			splx(s);
   1816 
   1817 			if (tr->tr_data == NULL) {
   1818 				tr->tr_data = tr->tr_real_data;
   1819 				tr->tr_length = tr->tr_real_length;
   1820 				return(ENOMEM);
   1821 			}
   1822 			if ((tr->tr_flags & TWA_CMD_DATA_IN) != 0)
   1823 				memcpy(tr->tr_data, tr->tr_real_data,
   1824 					tr->tr_length);
   1825 		}
   1826 
   1827 		/*
   1828 		 * Map the data buffer into bus space and build the S/G list.
   1829 		 */
   1830 		rv = bus_dmamap_load(sc->twa_dma_tag, tr->tr_dma_map,
   1831 			tr->tr_data, tr->tr_length, NULL, BUS_DMA_NOWAIT |
   1832 			BUS_DMA_STREAMING | (tr->tr_flags & TWA_CMD_DATA_OUT) ?
   1833 			BUS_DMA_READ : BUS_DMA_WRITE);
   1834 
   1835 		if (rv != 0) {
   1836 			if ((tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED) != 0) {
   1837 				s = splvm();
   1838 				uvm_km_free(kmem_map, (vaddr_t)tr->tr_data,
   1839 				    tr->tr_length, UVM_KMF_WIRED);
   1840 				splx(s);
   1841 			}
   1842 			return (rv);
   1843 		}
   1844 
   1845 		if ((rv = twa_setup_data_dmamap(tr,
   1846 				tr->tr_dma_map->dm_segs,
   1847 				tr->tr_dma_map->dm_nsegs, error))) {
   1848 
   1849 			if (tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED) {
   1850 				s = splvm();
   1851 				uvm_km_free(kmem_map, (vaddr_t)tr->tr_data,
   1852 				    tr->tr_length, UVM_KMF_WIRED);
   1853 				splx(s);
   1854 				tr->tr_data = tr->tr_real_data;
   1855 				tr->tr_length = tr->tr_real_length;
   1856 			}
   1857 		} else
   1858 			error = tr->tr_error;
   1859 
   1860 	} else
   1861 		if ((rv = twa_submit_io(tr)))
   1862 			twa_unmap_request(tr);
   1863 
   1864 	return (rv);
   1865 }
   1866 
   1867 #if 0
   1868 /*
   1869  * Function name:	twa_flash_firmware
   1870  * Description:		Flashes bundled firmware image onto controller.
   1871  *
   1872  * Input:		sc	-- ptr to per ctlr structure
   1873  * Output:		None
   1874  * Return value:	0	-- success
   1875  *			non-zero-- failure
   1876  */
   1877 static int
   1878 twa_flash_firmware(struct twa_softc *sc)
   1879 {
   1880 	struct twa_request			*tr;
   1881 	struct twa_command_download_firmware	*cmd;
   1882 	uint32_t				count;
   1883 	uint32_t				fw_img_chunk_size;
   1884 	uint32_t				this_chunk_size = 0;
   1885 	uint32_t				remaining_img_size = 0;
   1886 	int					s, error = 0;
   1887 	int					i;
   1888 
   1889 	if ((tr = twa_get_request(sc, 0)) == NULL) {
   1890 		/* No free request packets available.  Can't proceed. */
   1891 		error = EIO;
   1892 		goto out;
   1893 	}
   1894 
   1895 	count = (twa_fw_img_size / 65536);
   1896 
   1897 	count += ((twa_fw_img_size % 65536) != 0) ? 1 : 0;
   1898 
   1899 	tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
   1900 	/* Allocate sufficient memory to hold a chunk of the firmware image. */
   1901 	fw_img_chunk_size = ((twa_fw_img_size / count) + 511) & ~511;
   1902 
   1903 	s = splvm();
   1904 	tr->tr_data = (void *)uvm_km_alloc(kmem_map, fw_img_chunk_size, 512,
   1905 				UVM_KMF_WIRED);
   1906 	splx(s);
   1907 
   1908 	if (tr->tr_data == NULL) {
   1909 		error = ENOMEM;
   1910 		goto out;
   1911 	}
   1912 
   1913 	remaining_img_size = twa_fw_img_size;
   1914 	cmd = &(tr->tr_command->command.cmd_pkt_7k.download_fw);
   1915 
   1916 	for (i = 0; i < count; i++) {
   1917 		/* Build a cmd pkt for downloading firmware. */
   1918 		memset(tr->tr_command, 0, sizeof(struct twa_command_packet));
   1919 
   1920 		tr->tr_command->cmd_hdr.header_desc.size_header = 128;
   1921 
   1922 		cmd->opcode = TWA_OP_DOWNLOAD_FIRMWARE;
   1923 		cmd->sgl_offset = 2;	/* offset in dwords, to the beginning
   1924 					   of sg list */
   1925 		cmd->size = 2;		/* this field will be updated at data
   1926 					   map time */
   1927 		cmd->request_id = tr->tr_request_id;
   1928 		cmd->unit = 0;
   1929 		cmd->status = 0;
   1930 		cmd->flags = 0;
   1931 		cmd->param = 8;	/* prom image */
   1932 
   1933 		if (i != (count - 1))
   1934 			this_chunk_size = fw_img_chunk_size;
   1935 		else	 /* last chunk */
   1936 			this_chunk_size = remaining_img_size;
   1937 
   1938 		remaining_img_size -= this_chunk_size;
   1939 
   1940 		memset(tr->tr_data, 0, fw_img_chunk_size);
   1941 
   1942 		memcpy(tr->tr_data, twa_fw_img + (i * fw_img_chunk_size),
   1943 			this_chunk_size);
   1944 		/*
   1945 		 * The next line will effect only the last chunk.
   1946 		 */
   1947 		tr->tr_length = (this_chunk_size + 511) & ~511;
   1948 
   1949 		tr->tr_flags |= TWA_CMD_DATA_OUT;
   1950 
   1951 		error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
   1952 
   1953 		if (error) {
   1954 			if (error == ETIMEDOUT)
   1955 				/* clean-up done by twa_immediate_request */
   1956 				return(error);
   1957 			break;
   1958 		}
   1959 		error = cmd->status;
   1960 
   1961 		if (i != (count - 1)) {
   1962 
   1963 			/*
   1964 			 * XXX FreeBSD code doesn't check for no error condition
   1965 			 * but based on observation, error seems to return 0
   1966 			 */
   1967 			if ((error =
   1968 			    tr->tr_command->cmd_hdr.status_block.error) == 0) {
   1969 				continue;
   1970 			} else if ((error =
   1971 			    tr->tr_command->cmd_hdr.status_block.error) ==
   1972 			    TWA_ERROR_MORE_DATA) {
   1973 				    continue;
   1974 			} else {
   1975 				twa_hard_reset(sc);
   1976 				break;
   1977 			}
   1978 		} else	 /* last chunk */
   1979 			if (error) {
   1980 				printf("%s: firmware flash request failed. "
   1981 				    "error = 0x%x\n", sc->twa_dv.dv_xname,
   1982 				    error);
   1983 				twa_hard_reset(sc);
   1984 			}
   1985 	}
   1986 
   1987 	if (tr->tr_data) {
   1988 		s = splvm();
   1989 		uvm_km_free(kmem_map, (vaddr_t)tr->tr_data,
   1990 			fw_img_chunk_size, UVM_KMF_WIRED);
   1991 		splx(s);
   1992 	}
   1993 out:
   1994 	if (tr)
   1995 		twa_release_request(tr);
   1996 	return(error);
   1997 }
   1998 
   1999 /*
   2000  * Function name:	twa_hard_reset
   2001  * Description:		Hard reset the controller.
   2002  *
   2003  * Input:		sc	-- ptr to per ctlr structure
   2004  * Output:		None
   2005  * Return value:	0	-- success
   2006  *			non-zero-- failure
   2007  */
   2008 static int
   2009 twa_hard_reset(struct twa_softc *sc)
   2010 {
   2011 	struct twa_request			*tr;
   2012 	struct twa_command_reset_firmware	*cmd;
   2013 	int					error;
   2014 
   2015 	if ((tr = twa_get_request(sc, 0)) == NULL)
   2016 		return(EIO);
   2017 	tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
   2018 	/* Build a cmd pkt for sending down the hard reset command. */
   2019 	tr->tr_command->cmd_hdr.header_desc.size_header = 128;
   2020 
   2021 	cmd = &(tr->tr_command->command.cmd_pkt_7k.reset_fw);
   2022 	cmd->opcode = TWA_OP_RESET_FIRMWARE;
   2023 	cmd->size = 2;	/* this field will be updated at data map time */
   2024 	cmd->request_id = tr->tr_request_id;
   2025 	cmd->unit = 0;
   2026 	cmd->status = 0;
   2027 	cmd->flags = 0;
   2028 	cmd->param = 0;	/* don't reload FPGA logic */
   2029 
   2030 	tr->tr_data = NULL;
   2031 	tr->tr_length = 0;
   2032 
   2033 	error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
   2034 	if (error) {
   2035 		printf("%s: hard reset request could not be posted. "
   2036 		    "error = 0x%x\n", sc->twa_dv.dv_xname, error);
   2037 		if (error == ETIMEDOUT)
   2038 			/* clean-up done by twa_immediate_request */
   2039 			return(error);
   2040 		goto out;
   2041 	}
   2042 	if ((error = cmd->status)) {
   2043 		printf("%s: hard reset request failed. error = 0x%x\n",
   2044 			sc->twa_dv.dv_xname, error);
   2045 	}
   2046 
   2047 out:
   2048 	if (tr)
   2049 		twa_release_request(tr);
   2050 	return(error);
   2051 }
   2052 #endif
   2053 
   2054 /*
   2055  * Function name:	twa_intr
   2056  * Description:		Interrupt handler.  Determines the kind of interrupt,
   2057  *			and calls the appropriate handler.
   2058  *
   2059  * Input:		sc	-- ptr to per ctlr structure
   2060  * Output:		None
   2061  * Return value:	None
   2062  */
   2063 
   2064 static int
   2065 twa_intr(void *arg)
   2066 {
   2067 	int	caught, s, rv;
   2068 	struct twa_softc *sc;
   2069 	uint32_t	status_reg;
   2070 	sc = (struct twa_softc *)arg;
   2071 
   2072 	caught = 0;
   2073 	/* Collect current interrupt status. */
   2074 	status_reg = twa_inl(sc, TWA_STATUS_REGISTER_OFFSET);
   2075 	if (twa_check_ctlr_state(sc, status_reg)) {
   2076 		caught = 1;
   2077 		goto bail;
   2078 	}
   2079 	/* Dispatch based on the kind of interrupt. */
   2080 	if (status_reg & TWA_STATUS_HOST_INTERRUPT) {
   2081 		twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
   2082 			TWA_CONTROL_CLEAR_HOST_INTERRUPT);
   2083 		caught = 1;
   2084 	}
   2085 	if ((status_reg & TWA_STATUS_ATTENTION_INTERRUPT) != 0) {
   2086 		twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
   2087 			TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT);
   2088 		rv = twa_fetch_aen(sc);
   2089 #ifdef DIAGNOSTIC
   2090 		if (rv != 0)
   2091 			printf("%s: unable to retrieve AEN (%d)\n",
   2092 				sc->twa_dv.dv_xname, rv);
   2093 #endif
   2094 		caught = 1;
   2095 	}
   2096 	if (status_reg & TWA_STATUS_COMMAND_INTERRUPT) {
   2097 		/* Start any requests that might be in the pending queue. */
   2098 		twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
   2099 			TWA_CONTROL_MASK_COMMAND_INTERRUPT);
   2100 		(void)twa_drain_pending_queue(sc);
   2101 		caught = 1;
   2102 	}
   2103 	if (status_reg & TWA_STATUS_RESPONSE_INTERRUPT) {
   2104 		s = splbio();
   2105 		twa_done(sc);
   2106 		splx(s);
   2107 		caught = 1;
   2108 	}
   2109 bail:
   2110 	return (caught);
   2111 }
   2112 
   2113 /*
   2114  * Accept an open operation on the control device.
   2115  */
   2116 static int
   2117 twaopen(dev_t dev, int flag, int mode, struct lwp *l)
   2118 {
   2119 	struct twa_softc *twa;
   2120 
   2121 	if ((twa = device_lookup(&twa_cd, minor(dev))) == NULL)
   2122 		return (ENXIO);
   2123 
   2124 	twa->twa_sc_flags |= TWA_STATE_OPEN;
   2125 
   2126 	return (0);
   2127 }
   2128 
   2129 /*
   2130  * Accept the last close on the control device.
   2131  */
   2132 static int
   2133 twaclose(dev_t dev, int flag, int mode, struct lwp *l)
   2134 {
   2135 	struct twa_softc *twa;
   2136 
   2137 	twa = device_lookup(&twa_cd, minor(dev));
   2138 	twa->twa_sc_flags &= ~TWA_STATE_OPEN;
   2139 	return (0);
   2140 }
   2141 
   2142 /*
   2143  * Function name:	twaioctl
   2144  * Description:		ioctl handler.
   2145  *
   2146  * Input:		sc	-- ptr to per ctlr structure
   2147  *			cmd	-- ioctl cmd
   2148  *			buf	-- ptr to buffer in kernel memory, which is
   2149  *				   a copy of the input buffer in user-space
   2150  * Output:		buf	-- ptr to buffer in kernel memory, which will
   2151  *				   be copied of the output buffer in user-space
   2152  * Return value:	0	-- success
   2153  *			non-zero-- failure
   2154  */
   2155 static int
   2156 twaioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
   2157 {
   2158 	struct twa_softc *sc;
   2159 	struct twa_ioctl_9k	*user_buf = (struct twa_ioctl_9k *)data;
   2160 	struct tw_cl_event_packet event_buf;
   2161 	struct twa_request 	*tr = 0;
   2162 	int32_t			event_index = 0;
   2163 	int32_t			start_index;
   2164 	int			s, error = 0;
   2165 
   2166 	sc = device_lookup(&twa_cd, minor(dev));
   2167 
   2168 	switch (cmd) {
   2169 	case TW_OSL_IOCTL_FIRMWARE_PASS_THROUGH:
   2170 	{
   2171 		struct twa_command_packet	*cmdpkt;
   2172 		uint32_t			data_buf_size_adjusted;
   2173 
   2174 		/* Get a request packet */
   2175 		tr = twa_get_request_wait(sc, 0);
   2176 		KASSERT(tr != NULL);
   2177 		/*
   2178 		 * Make sure that the data buffer sent to firmware is a
   2179 		 * 512 byte multiple in size.
   2180 		 */
   2181 		data_buf_size_adjusted =
   2182 			(user_buf->twa_drvr_pkt.buffer_length + 511) & ~511;
   2183 
   2184 		if ((tr->tr_length = data_buf_size_adjusted)) {
   2185 			if ((tr->tr_data = malloc(data_buf_size_adjusted,
   2186 			    M_DEVBUF, M_WAITOK)) == NULL) {
   2187 				error = ENOMEM;
   2188 				goto fw_passthru_done;
   2189 			}
   2190 			/* Copy the payload. */
   2191 			if ((error = copyin((void *) (user_buf->pdata),
   2192 				(void *) (tr->tr_data),
   2193 				user_buf->twa_drvr_pkt.buffer_length)) != 0) {
   2194 					goto fw_passthru_done;
   2195 			}
   2196 			tr->tr_flags |= TWA_CMD_DATA_IN | TWA_CMD_DATA_OUT;
   2197 		}
   2198 		tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_IOCTL;
   2199 		cmdpkt = tr->tr_command;
   2200 
   2201 		/* Copy the command packet. */
   2202 		memcpy(cmdpkt, &(user_buf->twa_cmd_pkt),
   2203 			sizeof(struct twa_command_packet));
   2204 		cmdpkt->command.cmd_pkt_7k.generic.request_id =
   2205 			tr->tr_request_id;
   2206 
   2207 		/* Send down the request, and wait for it to complete. */
   2208 		if ((error = twa_wait_request(tr, TWA_REQUEST_TIMEOUT_PERIOD))) 		{
   2209 			if (error == ETIMEDOUT)
   2210 				break; /* clean-up done by twa_wait_request */
   2211 			goto fw_passthru_done;
   2212 		}
   2213 
   2214 		/* Copy the command packet back into user space. */
   2215 		memcpy(&user_buf->twa_cmd_pkt, cmdpkt,
   2216 			sizeof(struct twa_command_packet));
   2217 
   2218 		/* If there was a payload, copy it back too. */
   2219 		if (tr->tr_length)
   2220 			error = copyout(tr->tr_data, user_buf->pdata,
   2221 					user_buf->twa_drvr_pkt.buffer_length);
   2222 fw_passthru_done:
   2223 		/* Free resources. */
   2224 		if (tr->tr_data)
   2225 			free(tr->tr_data, M_DEVBUF);
   2226 
   2227 		if (tr)
   2228 			twa_release_request(tr);
   2229 		break;
   2230 	}
   2231 
   2232 	case TW_OSL_IOCTL_SCAN_BUS:
   2233 		twa_request_bus_scan(sc);
   2234 		break;
   2235 
   2236 	case TW_CL_IOCTL_GET_FIRST_EVENT:
   2237 		if (sc->twa_aen_queue_wrapped) {
   2238 			if (sc->twa_aen_queue_overflow) {
   2239 				/*
   2240 				 * The aen queue has wrapped, even before some
   2241 				 * events have been retrieved.  Let the caller
   2242 				 * know that he missed out on some AEN's.
   2243 				 */
   2244 				user_buf->twa_drvr_pkt.status =
   2245 					TWA_ERROR_AEN_OVERFLOW;
   2246 				sc->twa_aen_queue_overflow = FALSE;
   2247 			} else
   2248 				user_buf->twa_drvr_pkt.status = 0;
   2249 			event_index = sc->twa_aen_head;
   2250 		} else {
   2251 			if (sc->twa_aen_head == sc->twa_aen_tail) {
   2252 				user_buf->twa_drvr_pkt.status =
   2253 					TWA_ERROR_AEN_NO_EVENTS;
   2254 				break;
   2255 			}
   2256 			user_buf->twa_drvr_pkt.status = 0;
   2257 			event_index = sc->twa_aen_tail;	/* = 0 */
   2258 		}
   2259 		if ((error = copyout(sc->twa_aen_queue[event_index],
   2260 		    user_buf->pdata, sizeof(struct tw_cl_event_packet))) != 0)
   2261 			(sc->twa_aen_queue[event_index])->retrieved =
   2262 			    TWA_AEN_RETRIEVED;
   2263 		break;
   2264 
   2265 	case TW_CL_IOCTL_GET_LAST_EVENT:
   2266 		if (sc->twa_aen_queue_wrapped) {
   2267 			if (sc->twa_aen_queue_overflow) {
   2268 				/*
   2269 				 * The aen queue has wrapped, even before some
   2270 				 * events have been retrieved.  Let the caller
   2271 				 * know that he missed out on some AEN's.
   2272 				 */
   2273 				user_buf->twa_drvr_pkt.status =
   2274 					TWA_ERROR_AEN_OVERFLOW;
   2275 				sc->twa_aen_queue_overflow = FALSE;
   2276 			} else
   2277 				user_buf->twa_drvr_pkt.status = 0;
   2278 		} else {
   2279 			if (sc->twa_aen_head == sc->twa_aen_tail) {
   2280 				user_buf->twa_drvr_pkt.status =
   2281 					TWA_ERROR_AEN_NO_EVENTS;
   2282 				break;
   2283 			}
   2284 			user_buf->twa_drvr_pkt.status = 0;
   2285 		}
   2286 		event_index =
   2287 		    (sc->twa_aen_head - 1 + TWA_Q_LENGTH) % TWA_Q_LENGTH;
   2288 		if ((error = copyout(sc->twa_aen_queue[event_index],
   2289 		    user_buf->pdata, sizeof(struct tw_cl_event_packet))) != 0)
   2290 			(sc->twa_aen_queue[event_index])->retrieved =
   2291 			    TWA_AEN_RETRIEVED;
   2292 		break;
   2293 
   2294 	case TW_CL_IOCTL_GET_NEXT_EVENT:
   2295 		user_buf->twa_drvr_pkt.status = 0;
   2296 		if (sc->twa_aen_queue_wrapped) {
   2297 
   2298 			if (sc->twa_aen_queue_overflow) {
   2299 				/*
   2300 				 * The aen queue has wrapped, even before some
   2301 				 * events have been retrieved.  Let the caller
   2302 				 * know that he missed out on some AEN's.
   2303 				 */
   2304 				user_buf->twa_drvr_pkt.status =
   2305 					TWA_ERROR_AEN_OVERFLOW;
   2306 				sc->twa_aen_queue_overflow = FALSE;
   2307 			}
   2308 			start_index = sc->twa_aen_head;
   2309 		} else {
   2310 			if (sc->twa_aen_head == sc->twa_aen_tail) {
   2311 				user_buf->twa_drvr_pkt.status =
   2312 					TWA_ERROR_AEN_NO_EVENTS;
   2313 				break;
   2314 			}
   2315 			start_index = sc->twa_aen_tail;	/* = 0 */
   2316 		}
   2317 		error = copyin(user_buf->pdata, &event_buf,
   2318 				sizeof(struct tw_cl_event_packet));
   2319 
   2320 		event_index = (start_index + event_buf.sequence_id -
   2321 		    (sc->twa_aen_queue[start_index])->sequence_id + 1)
   2322 		    % TWA_Q_LENGTH;
   2323 
   2324 		if (!((sc->twa_aen_queue[event_index])->sequence_id >
   2325 		    event_buf.sequence_id)) {
   2326 			if (user_buf->twa_drvr_pkt.status ==
   2327 			    TWA_ERROR_AEN_OVERFLOW)
   2328 				/* so we report the overflow next time */
   2329 				sc->twa_aen_queue_overflow = TRUE;
   2330 			user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
   2331 			break;
   2332 		}
   2333 		if ((error = copyout(sc->twa_aen_queue[event_index],
   2334 		    user_buf->pdata, sizeof(struct tw_cl_event_packet))) != 0)
   2335 			(sc->twa_aen_queue[event_index])->retrieved =
   2336 			    TWA_AEN_RETRIEVED;
   2337 		break;
   2338 
   2339 	case TW_CL_IOCTL_GET_PREVIOUS_EVENT:
   2340 		user_buf->twa_drvr_pkt.status = 0;
   2341 		if (sc->twa_aen_queue_wrapped) {
   2342 			if (sc->twa_aen_queue_overflow) {
   2343 				/*
   2344 				 * The aen queue has wrapped, even before some
   2345 				 * events have been retrieved.  Let the caller
   2346 				 * know that he missed out on some AEN's.
   2347 				 */
   2348 				user_buf->twa_drvr_pkt.status =
   2349 					TWA_ERROR_AEN_OVERFLOW;
   2350 				sc->twa_aen_queue_overflow = FALSE;
   2351 			}
   2352 			start_index = sc->twa_aen_head;
   2353 		} else {
   2354 			if (sc->twa_aen_head == sc->twa_aen_tail) {
   2355 				user_buf->twa_drvr_pkt.status =
   2356 					TWA_ERROR_AEN_NO_EVENTS;
   2357 				break;
   2358 			}
   2359 			start_index = sc->twa_aen_tail;	/* = 0 */
   2360 		}
   2361 		if ((error = copyin(user_buf->pdata, &event_buf,
   2362 				sizeof(struct tw_cl_event_packet))) != 0)
   2363 
   2364 		event_index = (start_index + event_buf.sequence_id -
   2365 		    (sc->twa_aen_queue[start_index])->sequence_id - 1)
   2366 		    % TWA_Q_LENGTH;
   2367 		if (!((sc->twa_aen_queue[event_index])->sequence_id <
   2368 		    event_buf.sequence_id)) {
   2369 			if (user_buf->twa_drvr_pkt.status ==
   2370 			    TWA_ERROR_AEN_OVERFLOW)
   2371 				/* so we report the overflow next time */
   2372 				sc->twa_aen_queue_overflow = TRUE;
   2373 			user_buf->twa_drvr_pkt.status =
   2374 				TWA_ERROR_AEN_NO_EVENTS;
   2375 			break;
   2376 		}
   2377 		if ((error = copyout(sc->twa_aen_queue [event_index],
   2378 		    user_buf->pdata, sizeof(struct tw_cl_event_packet))) != 0)
   2379 			aprint_error("%s: get_previous: Could not copyout to "
   2380 			    "event_buf. error = %x\n", sc->twa_dv.dv_xname,
   2381 			    error);
   2382 		(sc->twa_aen_queue[event_index])->retrieved = TWA_AEN_RETRIEVED;
   2383 		break;
   2384 
   2385 	case TW_CL_IOCTL_GET_LOCK:
   2386 	{
   2387 		struct tw_cl_lock_packet	twa_lock;
   2388 
   2389 		copyin(user_buf->pdata, &twa_lock,
   2390 				sizeof(struct tw_cl_lock_packet));
   2391 		s = splbio();
   2392 		if ((sc->twa_ioctl_lock.lock == TWA_LOCK_FREE) ||
   2393 			(twa_lock.force_flag) ||
   2394 			(time_second >= sc->twa_ioctl_lock.timeout)) {
   2395 
   2396 			sc->twa_ioctl_lock.lock = TWA_LOCK_HELD;
   2397 			sc->twa_ioctl_lock.timeout = time_second +
   2398 				(twa_lock.timeout_msec / 1000);
   2399 			twa_lock.time_remaining_msec = twa_lock.timeout_msec;
   2400 			user_buf->twa_drvr_pkt.status = 0;
   2401 		} else {
   2402 			twa_lock.time_remaining_msec =
   2403 				(sc->twa_ioctl_lock.timeout - time_second) *
   2404 				1000;
   2405 			user_buf->twa_drvr_pkt.status =
   2406 					TWA_ERROR_IOCTL_LOCK_ALREADY_HELD;
   2407 		}
   2408 		splx(s);
   2409 		copyout(&twa_lock, user_buf->pdata,
   2410 				sizeof(struct tw_cl_lock_packet));
   2411 		break;
   2412 	}
   2413 
   2414 	case TW_CL_IOCTL_RELEASE_LOCK:
   2415 		s = splbio();
   2416 		if (sc->twa_ioctl_lock.lock == TWA_LOCK_FREE) {
   2417 			user_buf->twa_drvr_pkt.status =
   2418 				TWA_ERROR_IOCTL_LOCK_NOT_HELD;
   2419 		} else {
   2420 			sc->twa_ioctl_lock.lock = TWA_LOCK_FREE;
   2421 			user_buf->twa_drvr_pkt.status = 0;
   2422 		}
   2423 		splx(s);
   2424 		break;
   2425 
   2426 	case TW_CL_IOCTL_GET_COMPATIBILITY_INFO:
   2427 	{
   2428 		struct tw_cl_compatibility_packet	comp_pkt;
   2429 
   2430 		memcpy(comp_pkt.driver_version, TWA_DRIVER_VERSION_STRING,
   2431 					sizeof(TWA_DRIVER_VERSION_STRING));
   2432 		comp_pkt.working_srl = sc->working_srl;
   2433 		comp_pkt.working_branch = sc->working_branch;
   2434 		comp_pkt.working_build = sc->working_build;
   2435 		user_buf->twa_drvr_pkt.status = 0;
   2436 
   2437 		/* Copy compatibility information to user space. */
   2438 		copyout(&comp_pkt, user_buf->pdata,
   2439 				min(sizeof(struct tw_cl_compatibility_packet),
   2440 					user_buf->twa_drvr_pkt.buffer_length));
   2441 		break;
   2442 	}
   2443 
   2444 	case TWA_IOCTL_GET_UNITNAME:	/* WASABI EXTENSION */
   2445 	{
   2446 		struct twa_unitname	*tn;
   2447 		struct twa_drive	*tdr;
   2448 
   2449 		tn = (struct twa_unitname *)data;
   2450 			/* XXX mutex */
   2451 		if (tn->tn_unit < 0 || tn->tn_unit >= TWA_MAX_UNITS)
   2452 			return (EINVAL);
   2453 		tdr = &sc->sc_units[tn->tn_unit];
   2454 		if (tdr->td_dev == NULL)
   2455 			tn->tn_name[0] = '\0';
   2456 		else
   2457 			strlcpy(tn->tn_name, tdr->td_dev->dv_xname,
   2458 			    sizeof(tn->tn_name));
   2459 		return (0);
   2460 	}
   2461 
   2462 	default:
   2463 		/* Unknown opcode. */
   2464 		error = ENOTTY;
   2465 	}
   2466 
   2467 	return(error);
   2468 }
   2469 
   2470 const struct cdevsw twa_cdevsw = {
   2471 	twaopen, twaclose, noread, nowrite, twaioctl,
   2472 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
   2473 };
   2474 
   2475 /*
   2476  * Function name:	twa_get_param
   2477  * Description:		Get a firmware parameter.
   2478  *
   2479  * Input:		sc		-- ptr to per ctlr structure
   2480  *			table_id	-- parameter table #
   2481  *			param_id	-- index of the parameter in the table
   2482  *			param_size	-- size of the parameter in bytes
   2483  *			callback	-- ptr to function, if any, to be called
   2484  *					back on completion; NULL if no callback.
   2485  * Output:		None
   2486  * Return value:	ptr to param structure	-- success
   2487  *			NULL			-- failure
   2488  */
   2489 static int
   2490 twa_get_param(struct twa_softc *sc, int table_id, int param_id,
   2491     size_t param_size, void (* callback)(struct twa_request *tr),
   2492     struct twa_param_9k **param)
   2493 {
   2494 	int			rv = 0;
   2495 	struct twa_request	*tr;
   2496 	union twa_command_7k	*cmd;
   2497 
   2498 	/* Get a request packet. */
   2499 	if ((tr = twa_get_request(sc, 0)) == NULL) {
   2500 		rv = EAGAIN;
   2501 		goto out;
   2502 	}
   2503 
   2504 	tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
   2505 
   2506 	/* Allocate memory to read data into. */
   2507 	if ((*param = (struct twa_param_9k *)
   2508 		malloc(TWA_SECTOR_SIZE, M_DEVBUF, M_NOWAIT)) == NULL) {
   2509 		rv = ENOMEM;
   2510 		goto out;
   2511 	}
   2512 
   2513 	memset(*param, 0, sizeof(struct twa_param_9k) - 1 + param_size);
   2514 	tr->tr_data = *param;
   2515 	tr->tr_length = TWA_SECTOR_SIZE;
   2516 	tr->tr_flags = TWA_CMD_DATA_IN | TWA_CMD_DATA_OUT;
   2517 
   2518 	/* Build the cmd pkt. */
   2519 	cmd = &(tr->tr_command->command.cmd_pkt_7k);
   2520 
   2521 	tr->tr_command->cmd_hdr.header_desc.size_header = 128;
   2522 
   2523 	cmd->param.opcode = TWA_OP_GET_PARAM;
   2524 	cmd->param.sgl_offset = 2;
   2525 	cmd->param.size = 2;
   2526 	cmd->param.request_id = tr->tr_request_id;
   2527 	cmd->param.unit = 0;
   2528 	cmd->param.param_count = 1;
   2529 
   2530 	/* Specify which parameter we need. */
   2531 	(*param)->table_id = table_id | TWA_9K_PARAM_DESCRIPTOR;
   2532 	(*param)->parameter_id = param_id;
   2533 	(*param)->parameter_size_bytes = param_size;
   2534 
   2535 	/* Submit the command. */
   2536 	if (callback == NULL) {
   2537 		/* There's no call back; wait till the command completes. */
   2538 		rv = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
   2539 
   2540 		if (rv != 0)
   2541 			goto out;
   2542 
   2543 		if ((rv = cmd->param.status) != 0) {
   2544 		     /* twa_drain_complete_queue will have done the unmapping */
   2545 		     goto out;
   2546 		}
   2547 		twa_release_request(tr);
   2548 		return (rv);
   2549 	} else {
   2550 		/* There's a call back.  Simply submit the command. */
   2551 		tr->tr_callback = callback;
   2552 		rv = twa_map_request(tr);
   2553 		return (rv);
   2554 	}
   2555 out:
   2556 	if (tr)
   2557 		twa_release_request(tr);
   2558 	return(rv);
   2559 }
   2560 
   2561 /*
   2562  * Function name:	twa_set_param
   2563  * Description:		Set a firmware parameter.
   2564  *
   2565  * Input:		sc		-- ptr to per ctlr structure
   2566  *			table_id	-- parameter table #
   2567  *			param_id	-- index of the parameter in the table
   2568  *			param_size	-- size of the parameter in bytes
   2569  *			callback	-- ptr to function, if any, to be called
   2570  *					back on completion; NULL if no callback.
   2571  * Output:		None
   2572  * Return value:	0	-- success
   2573  *			non-zero-- failure
   2574  */
   2575 static int
   2576 twa_set_param(struct twa_softc *sc, int table_id, int param_id, int param_size,
   2577     void *data, void (* callback)(struct twa_request *tr))
   2578 {
   2579 	struct twa_request	*tr;
   2580 	union twa_command_7k	*cmd;
   2581 	struct twa_param_9k	*param = NULL;
   2582 	int			error = ENOMEM;
   2583 
   2584 	tr = twa_get_request(sc, 0);
   2585 	if (tr == NULL)
   2586 		return (EAGAIN);
   2587 
   2588 	tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
   2589 
   2590 	/* Allocate memory to send data using. */
   2591 	if ((param = (struct twa_param_9k *)
   2592 			malloc(TWA_SECTOR_SIZE, M_DEVBUF, M_NOWAIT)) == NULL)
   2593 		goto out;
   2594 	memset(param, 0, sizeof(struct twa_param_9k) - 1 + param_size);
   2595 	tr->tr_data = param;
   2596 	tr->tr_length = TWA_SECTOR_SIZE;
   2597 	tr->tr_flags = TWA_CMD_DATA_IN | TWA_CMD_DATA_OUT;
   2598 
   2599 	/* Build the cmd pkt. */
   2600 	cmd = &(tr->tr_command->command.cmd_pkt_7k);
   2601 
   2602 	tr->tr_command->cmd_hdr.header_desc.size_header = 128;
   2603 
   2604 	cmd->param.opcode = TWA_OP_SET_PARAM;
   2605 	cmd->param.sgl_offset = 2;
   2606 	cmd->param.size = 2;
   2607 	cmd->param.request_id = tr->tr_request_id;
   2608 	cmd->param.unit = 0;
   2609 	cmd->param.param_count = 1;
   2610 
   2611 	/* Specify which parameter we want to set. */
   2612 	param->table_id = table_id | TWA_9K_PARAM_DESCRIPTOR;
   2613 	param->parameter_id = param_id;
   2614 	param->parameter_size_bytes = param_size;
   2615 	memcpy(param->data, data, param_size);
   2616 
   2617 	/* Submit the command. */
   2618 	if (callback == NULL) {
   2619 		/* There's no call back;  wait till the command completes. */
   2620 		error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
   2621 		if (error == ETIMEDOUT)
   2622 			/* clean-up done by twa_immediate_request */
   2623 			return(error);
   2624 		if (error)
   2625 			goto out;
   2626 		if ((error = cmd->param.status)) {
   2627 			/*
   2628 			 * twa_drain_complete_queue will have done the
   2629 			 * unmapping.
   2630 			 */
   2631 			goto out;
   2632 		}
   2633 		free(param, M_DEVBUF);
   2634 		twa_release_request(tr);
   2635 		return(error);
   2636 	} else {
   2637 		/* There's a call back.  Simply submit the command. */
   2638 		tr->tr_callback = callback;
   2639 		if ((error = twa_map_request(tr)))
   2640 			goto out;
   2641 
   2642 		return (0);
   2643 	}
   2644 out:
   2645 	if (param)
   2646 		free(param, M_DEVBUF);
   2647 	if (tr)
   2648 		twa_release_request(tr);
   2649 	return(error);
   2650 }
   2651 
   2652 /*
   2653  * Function name:	twa_init_connection
   2654  * Description:		Send init_connection cmd to firmware
   2655  *
   2656  * Input:		sc		-- ptr to per ctlr structure
   2657  *			message_credits	-- max # of requests that we might send
   2658  *					 down simultaneously.  This will be
   2659  *					 typically set to 256 at init-time or
   2660  *					after a reset, and to 1 at shutdown-time
   2661  *			set_features	-- indicates if we intend to use 64-bit
   2662  *					sg, also indicates if we want to do a
   2663  *					basic or an extended init_connection;
   2664  *
   2665  * Note: The following input/output parameters are valid, only in case of an
   2666  *		extended init_connection:
   2667  *
   2668  *			current_fw_srl		-- srl of fw we are bundled
   2669  *						with, if any; 0 otherwise
   2670  *			current_fw_arch_id	-- arch_id of fw we are bundled
   2671  *						with, if any; 0 otherwise
   2672  *			current_fw_branch	-- branch # of fw we are bundled
   2673  *						with, if any; 0 otherwise
   2674  *			current_fw_build	-- build # of fw we are bundled
   2675  *						with, if any; 0 otherwise
   2676  * Output:		fw_on_ctlr_srl		-- srl of fw on ctlr
   2677  *			fw_on_ctlr_arch_id	-- arch_id of fw on ctlr
   2678  *			fw_on_ctlr_branch	-- branch # of fw on ctlr
   2679  *			fw_on_ctlr_build	-- build # of fw on ctlr
   2680  *			init_connect_result	-- result bitmap of fw response
   2681  * Return value:	0	-- success
   2682  *			non-zero-- failure
   2683  */
   2684 static int
   2685 twa_init_connection(struct twa_softc *sc, uint16_t message_credits,
   2686     uint32_t set_features, uint16_t current_fw_srl,
   2687     uint16_t current_fw_arch_id, uint16_t current_fw_branch,
   2688     uint16_t current_fw_build, uint16_t *fw_on_ctlr_srl,
   2689     uint16_t *fw_on_ctlr_arch_id, uint16_t *fw_on_ctlr_branch,
   2690     uint16_t *fw_on_ctlr_build, uint32_t *init_connect_result)
   2691 {
   2692 	struct twa_request		*tr;
   2693 	struct twa_command_init_connect	*init_connect;
   2694 	int				error = 1;
   2695 
   2696 	/* Get a request packet. */
   2697 	if ((tr = twa_get_request(sc, 0)) == NULL)
   2698 		goto out;
   2699 	tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
   2700 	/* Build the cmd pkt. */
   2701 	init_connect = &(tr->tr_command->command.cmd_pkt_7k.init_connect);
   2702 
   2703 	tr->tr_command->cmd_hdr.header_desc.size_header = 128;
   2704 
   2705 	init_connect->opcode = TWA_OP_INIT_CONNECTION;
   2706    	init_connect->request_id = tr->tr_request_id;
   2707 	init_connect->message_credits = message_credits;
   2708 	init_connect->features = set_features;
   2709 	if (TWA_64BIT_ADDRESSES) {
   2710 		printf("64 bit addressing supported for scatter/gather list\n");
   2711 		init_connect->features |= TWA_64BIT_SG_ADDRESSES;
   2712 	}
   2713 	if (set_features & TWA_EXTENDED_INIT_CONNECT) {
   2714 		/*
   2715 		 * Fill in the extra fields needed for
   2716 		 * an extended init_connect.
   2717 		 */
   2718 		init_connect->size = 6;
   2719 		init_connect->fw_srl = current_fw_srl;
   2720 		init_connect->fw_arch_id = current_fw_arch_id;
   2721 		init_connect->fw_branch = current_fw_branch;
   2722 	} else
   2723 		init_connect->size = 3;
   2724 
   2725 	/* Submit the command, and wait for it to complete. */
   2726 	error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
   2727 	if (error == ETIMEDOUT)
   2728 		return(error); /* clean-up done by twa_immediate_request */
   2729 	if (error)
   2730 		goto out;
   2731 	if ((error = init_connect->status)) {
   2732 		/* twa_drain_complete_queue will have done the unmapping */
   2733 		goto out;
   2734 	}
   2735 	if (set_features & TWA_EXTENDED_INIT_CONNECT) {
   2736 		*fw_on_ctlr_srl = init_connect->fw_srl;
   2737 		*fw_on_ctlr_arch_id = init_connect->fw_arch_id;
   2738 		*fw_on_ctlr_branch = init_connect->fw_branch;
   2739 		*fw_on_ctlr_build = init_connect->fw_build;
   2740 		*init_connect_result = init_connect->result;
   2741 	}
   2742 	twa_release_request(tr);
   2743 	return(error);
   2744 
   2745 out:
   2746 	if (tr)
   2747 		twa_release_request(tr);
   2748 	return(error);
   2749 }
   2750 
   2751 static int
   2752 twa_reset(struct twa_softc *sc)
   2753 {
   2754 	int	s;
   2755 	int	error = 0;
   2756 
   2757 	/*
   2758 	 * Disable interrupts from the controller, and mask any
   2759 	 * accidental entry into our interrupt handler.
   2760 	 */
   2761 	twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
   2762 		TWA_CONTROL_DISABLE_INTERRUPTS);
   2763 
   2764 	s = splbio();
   2765 
   2766 	/* Soft reset the controller. */
   2767 	if ((error = twa_soft_reset(sc)))
   2768 		goto out;
   2769 
   2770 	/* Re-establish logical connection with the controller. */
   2771 	if ((error = twa_init_connection(sc, TWA_INIT_MESSAGE_CREDITS,
   2772 					0, 0, 0, 0, 0,
   2773 					NULL, NULL, NULL, NULL, NULL))) {
   2774 		goto out;
   2775 	}
   2776 	/*
   2777 	 * Complete all requests in the complete queue; error back all requests
   2778 	 * in the busy queue.  Any internal requests will be simply freed.
   2779 	 * Re-submit any requests in the pending queue.
   2780 	 */
   2781 	twa_drain_busy_queue(sc);
   2782 
   2783 out:
   2784 	splx(s);
   2785 	/*
   2786 	 * Enable interrupts, and also clear attention and response interrupts.
   2787 	 */
   2788 	twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
   2789 		TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT |
   2790 		TWA_CONTROL_UNMASK_RESPONSE_INTERRUPT |
   2791 		TWA_CONTROL_ENABLE_INTERRUPTS);
   2792 	return(error);
   2793 }
   2794 
   2795 static int
   2796 twa_soft_reset(struct twa_softc *sc)
   2797 {
   2798 	uint32_t	status_reg;
   2799 
   2800 	twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
   2801 			TWA_CONTROL_ISSUE_SOFT_RESET |
   2802 			TWA_CONTROL_CLEAR_HOST_INTERRUPT |
   2803 			TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT |
   2804 			TWA_CONTROL_MASK_COMMAND_INTERRUPT |
   2805 			TWA_CONTROL_MASK_RESPONSE_INTERRUPT |
   2806 			TWA_CONTROL_DISABLE_INTERRUPTS);
   2807 
   2808 	if (twa_wait_status(sc, TWA_STATUS_MICROCONTROLLER_READY |
   2809 				TWA_STATUS_ATTENTION_INTERRUPT, 30)) {
   2810 		aprint_error("%s: no attention interrupt after reset.\n",
   2811 			sc->twa_dv.dv_xname);
   2812 		return(1);
   2813 	}
   2814 	twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
   2815 		TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT);
   2816 
   2817 	if (twa_drain_response_queue(sc)) {
   2818 		aprint_error("%s: cannot drain response queue.\n",
   2819 		    sc->twa_dv.dv_xname);
   2820 		return(1);
   2821 	}
   2822 	if (twa_drain_aen_queue(sc)) {
   2823 		aprint_error("%s: cannot drain AEN queue.\n",
   2824 		    sc->twa_dv.dv_xname);
   2825 		return(1);
   2826 	}
   2827 	if (twa_find_aen(sc, TWA_AEN_SOFT_RESET)) {
   2828 		aprint_error("%s: reset not reported by controller.\n",
   2829 			 sc->twa_dv.dv_xname);
   2830 		return(1);
   2831 	}
   2832 	status_reg = twa_inl(sc, TWA_STATUS_REGISTER_OFFSET);
   2833 	if (TWA_STATUS_ERRORS(status_reg) ||
   2834 	    twa_check_ctlr_state(sc, status_reg)) {
   2835 		aprint_error("%s: controller errors detected.\n",
   2836 		    sc->twa_dv.dv_xname);
   2837 		return(1);
   2838 	}
   2839 	return(0);
   2840 }
   2841 
   2842 static int
   2843 twa_wait_status(struct twa_softc *sc, uint32_t status, uint32_t timeout)
   2844 {
   2845 	struct timeval		t1;
   2846 	time_t		end_time;
   2847 	uint32_t	status_reg;
   2848 
   2849 	timeout = (timeout * 1000 * 100);
   2850 
   2851 	microtime(&t1);
   2852 
   2853 	end_time = t1.tv_usec + timeout;
   2854 
   2855 	do {
   2856 		status_reg = twa_inl(sc, TWA_STATUS_REGISTER_OFFSET);
   2857 		/* got the required bit(s)? */
   2858 		if ((status_reg & status) == status)
   2859 			return(0);
   2860 		DELAY(100000);
   2861 		microtime(&t1);
   2862 	} while (t1.tv_usec <= end_time);
   2863 
   2864 	return(1);
   2865 }
   2866 
   2867 static int
   2868 twa_fetch_aen(struct twa_softc *sc)
   2869 {
   2870 	struct twa_request	*tr;
   2871 	int			s, error = 0;
   2872 
   2873 	s = splbio();
   2874 
   2875 	if ((tr = twa_get_request(sc, TWA_CMD_AEN)) == NULL) {
   2876 		splx(s);
   2877 		return(EIO);
   2878 	}
   2879 	tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
   2880 	tr->tr_callback = twa_aen_callback;
   2881 	tr->tr_data = malloc(TWA_SECTOR_SIZE, M_DEVBUF, M_NOWAIT);
   2882 	if (twa_request_sense(tr, 0) != 0) {
   2883 		if (tr->tr_data)
   2884 			free(tr->tr_data, M_DEVBUF);
   2885 		twa_release_request(tr);
   2886 		error = 1;
   2887 	}
   2888 	splx(s);
   2889 
   2890 	return(error);
   2891 }
   2892 
   2893 /*
   2894  * Function name:	twa_aen_callback
   2895  * Description:		Callback for requests to fetch AEN's.
   2896  *
   2897  * Input:		tr	-- ptr to completed request pkt
   2898  * Output:		None
   2899  * Return value:	None
   2900  */
   2901 static void
   2902 twa_aen_callback(struct twa_request *tr)
   2903 {
   2904 	int i;
   2905 	int fetch_more_aens = 0;
   2906 	struct twa_softc		*sc = tr->tr_sc;
   2907 	struct twa_command_header	*cmd_hdr =
   2908 		(struct twa_command_header *)(tr->tr_data);
   2909 	struct twa_command_9k		*cmd =
   2910 		&(tr->tr_command->command.cmd_pkt_9k);
   2911 
   2912 	if (! cmd->status) {
   2913 		if ((tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_9K) &&
   2914 			(cmd->cdb[0] == 0x3 /* REQUEST_SENSE */))
   2915 			if (twa_enqueue_aen(sc, cmd_hdr)
   2916 				!= TWA_AEN_QUEUE_EMPTY)
   2917 				fetch_more_aens = 1;
   2918 	} else {
   2919 		cmd_hdr->err_specific_desc[sizeof(cmd_hdr->err_specific_desc) - 1] = '\0';
   2920 		for (i = 0; i < 18; i++)
   2921 			printf("%x\t", tr->tr_command->cmd_hdr.sense_data[i]);
   2922 
   2923 		printf(""); /* print new line */
   2924 
   2925 		for (i = 0; i < 128; i++)
   2926 			printf("%x\t", ((int8_t *)(tr->tr_data))[i]);
   2927 	}
   2928 	if (tr->tr_data)
   2929 		free(tr->tr_data, M_DEVBUF);
   2930 	twa_release_request(tr);
   2931 
   2932 	if (fetch_more_aens)
   2933 		twa_fetch_aen(sc);
   2934 }
   2935 
   2936 /*
   2937  * Function name:	twa_enqueue_aen
   2938  * Description:		Queues AEN's to be supplied to user-space tools on request.
   2939  *
   2940  * Input:		sc	-- ptr to per ctlr structure
   2941  *			cmd_hdr	-- ptr to hdr of fw cmd pkt, from where the AEN
   2942  *				   details can be retrieved.
   2943  * Output:		None
   2944  * Return value:	None
   2945  */
   2946 static uint16_t
   2947 twa_enqueue_aen(struct twa_softc *sc, struct twa_command_header *cmd_hdr)
   2948 {
   2949 	int			rv, s;
   2950 	struct tw_cl_event_packet *event;
   2951 	uint16_t		aen_code;
   2952 	unsigned long		sync_time;
   2953 
   2954 	s = splbio();
   2955 	aen_code = cmd_hdr->status_block.error;
   2956 
   2957 	switch (aen_code) {
   2958 	case TWA_AEN_SYNC_TIME_WITH_HOST:
   2959 
   2960 		sync_time = (time_second - (3 * 86400)) % 604800;
   2961 		rv = twa_set_param(sc, TWA_PARAM_TIME_TABLE,
   2962 				TWA_PARAM_TIME_SchedulerTime, 4,
   2963 				&sync_time, twa_aen_callback);
   2964 #ifdef DIAGNOSTIC
   2965 		if (rv != 0)
   2966 			printf("%s: unable to sync time with ctlr\n",
   2967 				sc->twa_dv.dv_xname);
   2968 #endif
   2969 		break;
   2970 
   2971 	case TWA_AEN_QUEUE_EMPTY:
   2972 		break;
   2973 
   2974 	default:
   2975 		/* Queue the event. */
   2976 		event = sc->twa_aen_queue[sc->twa_aen_head];
   2977 		if (event->retrieved == TWA_AEN_NOT_RETRIEVED)
   2978 			sc->twa_aen_queue_overflow = TRUE;
   2979 		event->severity =
   2980 			cmd_hdr->status_block.substatus_block.severity;
   2981 		event->time_stamp_sec = time_second;
   2982 		event->aen_code = aen_code;
   2983 		event->retrieved = TWA_AEN_NOT_RETRIEVED;
   2984 		event->sequence_id = ++(sc->twa_current_sequence_id);
   2985 		cmd_hdr->err_specific_desc[sizeof(cmd_hdr->err_specific_desc) - 1] = '\0';
   2986 		event->parameter_len = strlen(cmd_hdr->err_specific_desc);
   2987 		memcpy(event->parameter_data, cmd_hdr->err_specific_desc,
   2988 			event->parameter_len);
   2989 
   2990 		if (event->severity < TWA_AEN_SEVERITY_DEBUG) {
   2991 			printf("%s: AEN 0x%04X: %s: %s: %s\n",
   2992 				sc->twa_dv.dv_xname,
   2993 				aen_code,
   2994 				twa_aen_severity_table[event->severity],
   2995 				twa_find_msg_string(twa_aen_table, aen_code),
   2996 				event->parameter_data);
   2997 		}
   2998 
   2999 		if ((sc->twa_aen_head + 1) == TWA_Q_LENGTH)
   3000 			sc->twa_aen_queue_wrapped = TRUE;
   3001 		sc->twa_aen_head = (sc->twa_aen_head + 1) % TWA_Q_LENGTH;
   3002 		break;
   3003 	} /* switch */
   3004 	splx(s);
   3005 
   3006 	return (aen_code);
   3007 }
   3008 
   3009 /*
   3010  * Function name:	twa_find_aen
   3011  * Description:		Reports whether a given AEN ever occurred.
   3012  *
   3013  * Input:		sc	-- ptr to per ctlr structure
   3014  *			aen_code-- AEN to look for
   3015  * Output:		None
   3016  * Return value:	0	-- success
   3017  *			non-zero-- failure
   3018  */
   3019 static int
   3020 twa_find_aen(struct twa_softc *sc, uint16_t aen_code)
   3021 {
   3022 	uint32_t	last_index;
   3023 	int		s;
   3024 	int		i;
   3025 
   3026 	s = splbio();
   3027 
   3028 	if (sc->twa_aen_queue_wrapped)
   3029 		last_index = sc->twa_aen_head;
   3030 	else
   3031 		last_index = 0;
   3032 
   3033 	i = sc->twa_aen_head;
   3034 	do {
   3035 		i = (i + TWA_Q_LENGTH - 1) % TWA_Q_LENGTH;
   3036 		if ((sc->twa_aen_queue[i])->aen_code == aen_code) {
   3037 			splx(s);
   3038 			return(0);
   3039 		}
   3040 	} while (i != last_index);
   3041 
   3042 	splx(s);
   3043 	return(1);
   3044 }
   3045 
   3046 static inline void
   3047 twa_request_init(struct twa_request *tr, int flags)
   3048 {
   3049 	tr->tr_data = NULL;
   3050 	tr->tr_real_data = NULL;
   3051 	tr->tr_length = 0;
   3052 	tr->tr_real_length = 0;
   3053 	tr->tr_status = TWA_CMD_SETUP;/* command is in setup phase */
   3054 	tr->tr_flags = flags;
   3055 	tr->tr_error = 0;
   3056 	tr->tr_callback = NULL;
   3057 	tr->tr_cmd_pkt_type = 0;
   3058 	tr->bp = 0;
   3059 
   3060 	/*
   3061 	 * Look at the status field in the command packet to see how
   3062 	 * it completed the last time it was used, and zero out only
   3063 	 * the portions that might have changed.  Note that we don't
   3064 	 * care to zero out the sglist.
   3065 	 */
   3066 	if (tr->tr_command->command.cmd_pkt_9k.status)
   3067 		memset(tr->tr_command, 0,
   3068 			sizeof(struct twa_command_header) + 28);
   3069 	else
   3070 		memset(&(tr->tr_command->command), 0, 28);
   3071 }
   3072 
   3073 struct twa_request *
   3074 twa_get_request_wait(struct twa_softc *sc, int flags)
   3075 {
   3076 	struct twa_request *tr;
   3077 	int s;
   3078 
   3079 	KASSERT((flags & TWA_CMD_AEN) == 0);
   3080 
   3081 	s = splbio();
   3082 	while ((tr = TAILQ_FIRST(&sc->twa_free)) == NULL) {
   3083 		sc->twa_sc_flags |= TWA_STATE_REQUEST_WAIT;
   3084 		(void) tsleep(&sc->twa_free, PRIBIO, "twaccb", hz);
   3085 	}
   3086 	TAILQ_REMOVE(&sc->twa_free, tr, tr_link);
   3087 
   3088 	splx(s);
   3089 
   3090 	twa_request_init(tr, flags);
   3091 
   3092 	return(tr);
   3093 }
   3094 
   3095 struct twa_request *
   3096 twa_get_request(struct twa_softc *sc, int flags)
   3097 {
   3098 	int s;
   3099 	struct twa_request *tr;
   3100 
   3101 	/* Get a free request packet. */
   3102 	s = splbio();
   3103 	if (__predict_false((flags & TWA_CMD_AEN) != 0)) {
   3104 
   3105 		if ((sc->sc_twa_request->tr_flags & TWA_CMD_AEN_BUSY) == 0) {
   3106 			tr = sc->sc_twa_request;
   3107 			flags |= TWA_CMD_AEN_BUSY;
   3108 		} else {
   3109 			splx(s);
   3110 			return (NULL);
   3111 		}
   3112 	} else {
   3113 		if (__predict_false((tr =
   3114 				TAILQ_FIRST(&sc->twa_free)) == NULL)) {
   3115 			splx(s);
   3116 			return (NULL);
   3117 		}
   3118 		TAILQ_REMOVE(&sc->twa_free, tr, tr_link);
   3119 	}
   3120 	splx(s);
   3121 
   3122 	twa_request_init(tr, flags);
   3123 
   3124 	return(tr);
   3125 }
   3126 
   3127 /*
   3128  * Print some information about the controller
   3129  */
   3130 static void
   3131 twa_describe_controller(struct twa_softc *sc)
   3132 {
   3133 	struct twa_param_9k	*p[10];
   3134 	int			i, rv = 0;
   3135 	uint32_t		dsize;
   3136 	uint8_t			ports;
   3137 
   3138 	memset(p, sizeof(struct twa_param_9k *), 10);
   3139 
   3140 	/* Get the port count. */
   3141 	rv |= twa_get_param(sc, TWA_PARAM_CONTROLLER,
   3142 		TWA_PARAM_CONTROLLER_PortCount, 1, NULL, &p[0]);
   3143 
   3144 	/* get version strings */
   3145 	rv |= twa_get_param(sc, TWA_PARAM_VERSION, TWA_PARAM_VERSION_FW,
   3146 		16, NULL, &p[1]);
   3147 	rv |= twa_get_param(sc, TWA_PARAM_VERSION, TWA_PARAM_VERSION_BIOS,
   3148 		16, NULL, &p[2]);
   3149 	rv |= twa_get_param(sc, TWA_PARAM_VERSION, TWA_PARAM_VERSION_Mon,
   3150 		16, NULL, &p[3]);
   3151 	rv |= twa_get_param(sc, TWA_PARAM_VERSION, TWA_PARAM_VERSION_PCBA,
   3152 		8, NULL, &p[4]);
   3153 	rv |= twa_get_param(sc, TWA_PARAM_VERSION, TWA_PARAM_VERSION_ATA,
   3154 		8, NULL, &p[5]);
   3155 	rv |= twa_get_param(sc, TWA_PARAM_VERSION, TWA_PARAM_VERSION_PCI,
   3156 		8, NULL, &p[6]);
   3157 	rv |= twa_get_param(sc, TWA_PARAM_DRIVESUMMARY, TWA_PARAM_DRIVESTATUS,
   3158 		16, NULL, &p[7]);
   3159 
   3160 	if (rv) {
   3161 		/* some error occurred */
   3162 		aprint_error("%s: failed to fetch version information\n",
   3163 			sc->twa_dv.dv_xname);
   3164 		goto bail;
   3165 	}
   3166 
   3167 	ports = *(uint8_t *)(p[0]->data);
   3168 
   3169 	aprint_normal("%s: %d ports, Firmware %.16s, BIOS %.16s\n",
   3170 		sc->twa_dv.dv_xname, ports,
   3171 		p[1]->data, p[2]->data);
   3172 
   3173 	aprint_verbose("%s: Monitor %.16s, PCB %.8s, Achip %.8s, Pchip %.8s\n",
   3174 		sc->twa_dv.dv_xname,
   3175 		p[3]->data, p[4]->data,
   3176 		p[5]->data, p[6]->data);
   3177 
   3178 	for (i = 0; i < ports; i++) {
   3179 
   3180 		if ((*((char *)(p[7]->data + i)) & TWA_DRIVE_DETECTED) == 0)
   3181 			continue;
   3182 
   3183 		rv = twa_get_param(sc, TWA_PARAM_DRIVE_TABLE + i,
   3184 			TWA_PARAM_DRIVEMODELINDEX,
   3185 			TWA_PARAM_DRIVEMODEL_LENGTH, NULL, &p[8]);
   3186 
   3187 		if (rv != 0) {
   3188 			aprint_error("%s: unable to get drive model for port"
   3189 				" %d\n", sc->twa_dv.dv_xname, i);
   3190 			continue;
   3191 		}
   3192 
   3193 		rv = twa_get_param(sc, TWA_PARAM_DRIVE_TABLE + i,
   3194 			TWA_PARAM_DRIVESIZEINDEX,
   3195 			TWA_PARAM_DRIVESIZE_LENGTH, NULL, &p[9]);
   3196 
   3197 		if (rv != 0) {
   3198 			aprint_error("%s: unable to get drive size"
   3199 				" for port %d\n", sc->twa_dv.dv_xname,
   3200 					i);
   3201 			free(p[8], M_DEVBUF);
   3202 			continue;
   3203 		}
   3204 
   3205 		dsize = *(uint32_t *)(p[9]->data);
   3206 
   3207 		aprint_verbose("%s: port %d: %.40s %d MB\n",
   3208 		    sc->twa_dv.dv_xname, i, p[8]->data, dsize / 2048);
   3209 
   3210 		if (p[8])
   3211 			free(p[8], M_DEVBUF);
   3212 		if (p[9])
   3213 			free(p[9], M_DEVBUF);
   3214 	}
   3215 bail:
   3216 	if (p[0])
   3217 		free(p[0], M_DEVBUF);
   3218 	if (p[1])
   3219 		free(p[1], M_DEVBUF);
   3220 	if (p[2])
   3221 		free(p[2], M_DEVBUF);
   3222 	if (p[3])
   3223 		free(p[3], M_DEVBUF);
   3224 	if (p[4])
   3225 		free(p[4], M_DEVBUF);
   3226 	if (p[5])
   3227 		free(p[5], M_DEVBUF);
   3228 	if (p[6])
   3229 		free(p[6], M_DEVBUF);
   3230 }
   3231 
   3232 /*
   3233  * Function name:	twa_check_ctlr_state
   3234  * Description:		Makes sure that the fw status register reports a
   3235  *			proper status.
   3236  *
   3237  * Input:		sc		-- ptr to per ctlr structure
   3238  *			status_reg	-- value in the status register
   3239  * Output:		None
   3240  * Return value:	0	-- no errors
   3241  *			non-zero-- errors
   3242  */
   3243 static int
   3244 twa_check_ctlr_state(struct twa_softc *sc, uint32_t status_reg)
   3245 {
   3246 	int		result = 0;
   3247 	struct timeval	t1;
   3248 	static time_t	last_warning[2] = {0, 0};
   3249 
   3250 	/* Check if the 'micro-controller ready' bit is not set. */
   3251 	if ((status_reg & TWA_STATUS_EXPECTED_BITS) !=
   3252 				TWA_STATUS_EXPECTED_BITS) {
   3253 
   3254 		microtime(&t1);
   3255 
   3256 		last_warning[0] += (5 * 1000 * 100);
   3257 
   3258 		if (t1.tv_usec > last_warning[0]) {
   3259 			microtime(&t1);
   3260 			last_warning[0] = t1.tv_usec;
   3261 		}
   3262 		result = 1;
   3263 	}
   3264 
   3265 	/* Check if any error bits are set. */
   3266 	if ((status_reg & TWA_STATUS_UNEXPECTED_BITS) != 0) {
   3267 
   3268 		microtime(&t1);
   3269 		last_warning[1] += (5 * 1000 * 100);
   3270 		if (t1.tv_usec > last_warning[1]) {
   3271 		     	microtime(&t1);
   3272 			last_warning[1] = t1.tv_usec;
   3273 		}
   3274 		if (status_reg & TWA_STATUS_PCI_PARITY_ERROR_INTERRUPT) {
   3275 			aprint_error("%s: clearing PCI parity error "
   3276 				"re-seat/move/replace card.\n",
   3277 				 sc->twa_dv.dv_xname);
   3278 			twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
   3279 				TWA_CONTROL_CLEAR_PARITY_ERROR);
   3280 			pci_conf_write(sc->pc, sc->tag,
   3281 				PCI_COMMAND_STATUS_REG,
   3282 				TWA_PCI_CONFIG_CLEAR_PARITY_ERROR);
   3283 			result = 1;
   3284 		}
   3285 		if (status_reg & TWA_STATUS_PCI_ABORT_INTERRUPT) {
   3286 			aprint_error("%s: clearing PCI abort\n",
   3287 				sc->twa_dv.dv_xname);
   3288 			twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
   3289 				TWA_CONTROL_CLEAR_PCI_ABORT);
   3290 			pci_conf_write(sc->pc, sc->tag,
   3291 				PCI_COMMAND_STATUS_REG,
   3292 				TWA_PCI_CONFIG_CLEAR_PCI_ABORT);
   3293 			result = 1;
   3294 		}
   3295 		if (status_reg & TWA_STATUS_QUEUE_ERROR_INTERRUPT) {
   3296 			aprint_error("%s: clearing controller queue error\n",
   3297 				sc->twa_dv.dv_xname);
   3298 			twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
   3299 				TWA_CONTROL_CLEAR_PCI_ABORT);
   3300 			result = 1;
   3301 		}
   3302 		if (status_reg & TWA_STATUS_SBUF_WRITE_ERROR) {
   3303 			aprint_error("%s: clearing SBUF write error\n",
   3304 				sc->twa_dv.dv_xname);
   3305 			twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
   3306 				TWA_CONTROL_CLEAR_SBUF_WRITE_ERROR);
   3307 			result = 1;
   3308 		}
   3309 		if (status_reg & TWA_STATUS_MICROCONTROLLER_ERROR) {
   3310 			aprint_error("%s: micro-controller error\n",
   3311 				sc->twa_dv.dv_xname);
   3312 			result = 1;
   3313 		}
   3314 	}
   3315 	return(result);
   3316 }
   3317