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