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