/*-
* Copyright (c) 2004 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jordan Rhody of Wasabi Systems, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*-
* Copyright (c) 2003-04 3ware, Inc.
* Copyright (c) 2000 Michael Smith
* Copyright (c) 2000 BSDi
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD: src/sys/dev/twa/twa.c,v 1.2 2004/04/02 15:09:57 des Exp $
*/
/*
* 3ware driver for 9000 series storage controllers.
*
* Author: Vinod Kashyap
*/
/* FreeBSD driver revision for sysctl expected by the 3ware cli */
const char twaver[] = "1.50.01.002";
/* AEN messages. */
static const struct twa_message twa_aen_table[] = {
{0x0000, "AEN queue empty"},
{0x0001, "Controller reset occurred"},
{0x0002, "Degraded unit detected"},
{0x0003, "Controller error occurred"},
{0x0004, "Background rebuild failed"},
{0x0005, "Background rebuild done"},
{0x0006, "Incomplete unit detected"},
{0x0007, "Background initialize done"},
{0x0008, "Unclean shutdown detected"},
{0x0009, "Drive timeout detected"},
{0x000A, "Drive error detected"},
{0x000B, "Rebuild started"},
{0x000C, "Background initialize started"},
{0x000D, "Entire logical unit was deleted"},
{0x000E, "Background initialize failed"},
{0x000F, "SMART attribute exceeded threshold"},
{0x0010, "Power supply reported AC under range"},
{0x0011, "Power supply reported DC out of range"},
{0x0012, "Power supply reported a malfunction"},
{0x0013, "Power supply predicted malfunction"},
{0x0014, "Battery charge is below threshold"},
{0x0015, "Fan speed is below threshold"},
{0x0016, "Temperature sensor is above threshold"},
{0x0017, "Power supply was removed"},
{0x0018, "Power supply was inserted"},
{0x0019, "Drive was removed from a bay"},
{0x001A, "Drive was inserted into a bay"},
{0x001B, "Drive bay cover door was opened"},
{0x001C, "Drive bay cover door was closed"},
{0x001D, "Product case was opened"},
{0x0020, "Prepare for shutdown (power-off)"},
{0x0021, "Downgrade UDMA mode to lower speed"},
{0x0022, "Upgrade UDMA mode to higher speed"},
{0x0023, "Sector repair completed"},
{0x0024, "Sbuf memory test failed"},
{0x0025, "Error flushing cached write data to disk"},
{0x0026, "Drive reported data ECC error"},
{0x0027, "DCB has checksum error"},
{0x0028, "DCB version is unsupported"},
{0x0029, "Background verify started"},
{0x002A, "Background verify failed"},
{0x002B, "Background verify done"},
{0x002C, "Bad sector overwritten during rebuild"},
{0x002D, "Source drive error occurred"},
{0x002E, "Replace failed because replacement drive too small"},
{0x002F, "Verify failed because array was never initialized"},
{0x0030, "Unsupported ATA drive"},
{0x0031, "Synchronize host/controller time"},
{0x0032, "Spare capacity is inadequate for some units"},
{0x0033, "Background migration started"},
{0x0034, "Background migration failed"},
{0x0035, "Background migration done"},
{0x0036, "Verify detected and fixed data/parity mismatch"},
{0x0037, "SO-DIMM incompatible"},
{0x0038, "SO-DIMM not detected"},
{0x0039, "Corrected Sbuf ECC error"},
{0x003A, "Drive power on reset detected"},
{0x003B, "Background rebuild paused"},
{0x003C, "Background initialize paused"},
{0x003D, "Background verify paused"},
{0x003E, "Background migration paused"},
{0x003F, "Corrupt flash file system detected"},
{0x0040, "Flash file system repaired"},
{0x0041, "Unit number assignments were lost"},
{0x0042, "Error during read of primary DCB"},
{0x0043, "Latent error found in backup DCB"},
{0x0044, "Battery voltage is normal"},
{0x0045, "Battery voltage is low"},
{0x0046, "Battery voltage is high"},
{0x0047, "Battery voltage is too low"},
{0x0048, "Battery voltage is too high"},
{0x0049, "Battery temperature is normal"},
{0x004A, "Battery temperature is low"},
{0x004B, "Battery temperature is high"},
{0x004C, "Battery temperature is too low"},
{0x004D, "Battery temperature is too high"},
{0x004E, "Battery capacity test started"},
{0x004F, "Cache synchronization skipped"},
{0x0050, "Battery capacity test completed"},
{0x0051, "Battery health check started"},
{0x0052, "Battery health check completed"},
{0x0053, "Battery capacity test needed"},
{0x0054, "Battery charge termination voltage is at high level"},
{0x0055, "Battery charging started"},
{0x0056, "Battery charging completed"},
{0x0057, "Battery charging fault"},
{0x0058, "Battery capacity is below warning level"},
{0x0059, "Battery capacity is below error level"},
{0x005A, "Battery is present"},
{0x005B, "Battery is not present"},
{0x005C, "Battery is weak"},
{0x005D, "Battery health check failed"},
{0x005E, "Cache synchronized after power fail"},
{0x005F, "Cache synchronization failed; some data lost"},
{0x0060, "Bad cache meta data checksum"},
{0x0061, "Bad cache meta data signature"},
{0x0062, "Cache meta data restore failed"},
{0x0063, "BBU not found after power fail"},
{0x00FC, "Recovered/finished array membership update"},
{0x00FD, "Handler lockup"},
{0x00FE, "Retrying PCI transfer"},
{0x00FF, "AEN queue is full"},
{0xFFFFFFFF, NULL}
};
/* If the command involved data, unmap that too. */
if (tr->tr_data != NULL) {
if (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_9K)
cmd_status = tr->tr_command->command.cmd_pkt_9k.status;
else
cmd_status =
tr->tr_command->command.cmd_pkt_7k.generic.status;
if (tr->tr_flags & TWA_CMD_DATA_OUT) {
bus_dmamap_sync(tr->tr_sc->twa_dma_tag, tr->tr_dma_map,
0, tr->tr_length, BUS_DMASYNC_POSTREAD);
/*
* If we are using a bounce buffer, and we are reading
* data, copy the real data in.
*/
if (tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED)
if (cmd_status == 0)
memcpy(tr->tr_real_data, tr->tr_data,
tr->tr_real_length);
}
if (tr->tr_flags & TWA_CMD_DATA_IN)
bus_dmamap_sync(tr->tr_sc->twa_dma_tag, tr->tr_dma_map,
0, tr->tr_length, BUS_DMASYNC_POSTWRITE);
/* Free alignment buffer if it was used. */
if (tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED) {
s = splvm();
uvm_km_kmem_free(kmem_va_arena, (vaddr_t)tr->tr_data,
tr->tr_length);
splx(s);
tr->tr_data = tr->tr_real_data;
tr->tr_length = tr->tr_real_length;
}
}
/*
* Function name: twa_wait_request
* Description: Sends down a firmware cmd, and waits for the completion,
* but NOT in a tight loop.
*
* Input: tr -- ptr to request pkt
* timeout -- max # of seconds to wait before giving up
* Output: None
* Return value: 0 -- success
* non-zero-- failure
*/
static int
twa_wait_request(struct twa_request *tr, uint32_t timeout)
{
time_t end_time;
struct timeval t1;
int s, rv;
while (tr->tr_status != TWA_CMD_COMPLETE) {
rv = tr->tr_error;
if (rv != 0)
return(rv);
if ((rv = tsleep(tr, PRIBIO, "twawait", timeout * hz)) == 0)
break;
if (rv == EWOULDBLOCK) {
/*
* We will reset the controller only if the request has
* already been submitted, so as to not lose the
* request packet. If a busy request timed out, the
* reset will take care of freeing resources. If a
* pending request timed out, we will free resources
* for that request, right here. So, the caller is
* expected to NOT cleanup when ETIMEDOUT is returned.
*/
if (tr->tr_status == TWA_CMD_BUSY)
twa_reset(tr->tr_sc);
else {
/* Request was never submitted. Clean up. */
s = splbio();
TAILQ_REMOVE(&tr->tr_sc->twa_pending, tr,
tr_link);
splx(s);
twa_unmap_request(tr);
if (tr->tr_data)
free(tr->tr_data, M_DEVBUF);
twa_release_request(tr);
}
return(ETIMEDOUT);
}
/*
* Either the request got completed, or we were woken up by a
* signal. Calculate the new timeout, in case it was the
* latter.
*/
microtime(&t1);
/*
* Function name: twa_immediate_request
* Description: Sends down a firmware cmd, and waits for the completion
* in a tight loop.
*
* Input: tr -- ptr to request pkt
* timeout -- max # of seconds to wait before giving up
* Output: None
* Return value: 0 -- success
* non-zero-- failure
*/
static int
twa_immediate_request(struct twa_request *tr, uint32_t timeout)
{
struct timeval t1;
int s = 0, rv = 0;
rv = twa_map_request(tr);
if (rv != 0)
return(rv);
timeout = (timeout * 10000 * 10);
microtime(&t1);
timeout += t1.tv_usec;
do {
rv = tr->tr_error;
if (rv != 0)
return(rv);
s = splbio();
twa_done(tr->tr_sc);
splx(s);
if (tr->tr_status == TWA_CMD_COMPLETE)
return(rv);
microtime(&t1);
} while (t1.tv_usec <= timeout);
/*
* We will reset the controller only if the request has
* already been submitted, so as to not lose the
* request packet. If a busy request timed out, the
* reset will take care of freeing resources. If a
* pending request timed out, we will free resources
* for that request, right here. So, the caller is
* expected to NOT cleanup when ETIMEDOUT is returned.
*/
rv = ETIMEDOUT;
if (tr->tr_status == TWA_CMD_BUSY)
twa_reset(tr->tr_sc);
else {
/* Request was never submitted. Clean up. */
s = splbio();
TAILQ_REMOVE(&tr->tr_sc->twa_pending, tr, tr_link);
splx(s);
twa_unmap_request(tr);
if (tr->tr_data)
free(tr->tr_data, M_DEVBUF);
twa_release_request(tr);
}
return (rv);
}
static int
twa_inquiry(struct twa_request *tr, int lunid)
{
int error;
struct twa_command_9k *tr_9k_cmd;
/* XXXX setup page data no lun device
* it seems 9000 series does not indicate
* NOTPRESENT - need more investigation
*/
((struct scsipi_inquiry_data *)tr->tr_data)->device =
SID_QUAL_LU_NOTPRESENT;
error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
if (error != 0)
return (error);
if (((struct scsipi_inquiry_data *)tr->tr_data)->device ==
SID_QUAL_LU_NOTPRESENT)
error = 1;
return (error);
}
static int
twa_print_inquiry_data(struct twa_softc *sc, struct scsipi_inquiry_data *scsipi)
{
/*XXX AEN notification called in interrupt context
* so just queue the request. Return as quickly
* as possible from interrupt
*/
if ((tr->tr_flags & TWA_CMD_AEN) != 0)
error = twa_map_request(tr);
else
error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
return (error);
}
static int
twa_alloc_req_pkts(struct twa_softc *sc, int num_reqs)
{
struct twa_request *tr;
struct twa_command_packet *tc;
bus_dma_segment_t seg;
size_t max_segs, max_xfer;
int i, rv, rseg, size;
for (i = 0; i < num_reqs; i++, tc++) {
tr = &(sc->twa_req_buf[i]);
tr->tr_command = tc;
tr->tr_cmd_phys = sc->twa_cmd_pkt_phys +
(i * sizeof(struct twa_command_packet));
tr->tr_request_id = i;
tr->tr_sc = sc;
/*
* Create a map for data buffers. maxsize (256 * 1024) used in
* bus_dma_tag_create above should suffice the bounce page needs
* for data buffers, since the max I/O size we support is 128KB.
* If we supported I/O's bigger than 256KB, we would have to
* create a second dma_tag, with the appropriate maxsize.
*/
if ((rv = bus_dmamap_create(sc->twa_dma_tag,
max_xfer, max_segs, 1, 0, BUS_DMA_NOWAIT,
&tr->tr_dma_map)) != 0) {
aprint_error_dev(sc->twa_dv,
"unable to create command DMA map, "
"rv = %d\n", rv);
return (ENOMEM);
}
/* Insert request into the free queue. */
if (i != 0) {
sc->twa_lookup[i] = tr;
twa_release_request(tr);
} else
tr->tr_flags |= TWA_CMD_AEN;
}
return(0);
}
static void
twa_recompute_openings(struct twa_softc *sc)
{
struct twa_drive *td;
int unit;
int openings;
uint64_t total_size;
total_size = 0;
for (unit = 0; unit < sc->sc_nunits; unit++) {
td = &sc->sc_units[unit];
total_size += td->td_size;
}
for (unit = 0; unit < sc->sc_nunits; unit++) {
td = &sc->sc_units[unit];
/*
* In theory, TWA_Q_LENGTH - 1 should be usable, but
* keep one additional ccb for internal commands.
* This makes the controller more reliable under load.
*/
if (total_size > 0) {
openings = (TWA_Q_LENGTH - 2) * td->td_size
/ total_size;
} else
openings = 0;
if (openings == td->td_openings)
continue;
td->td_openings = openings;
#ifdef TWA_DEBUG
printf("%s: unit %d openings %d\n",
device_xname(sc->twa_dv), unit, openings);
#endif
if (td->td_dev != NULL)
(*td->td_callbacks->tcb_openings)(td->td_dev,
td->td_openings);
}
}
/* ARGSUSED */
static int
twa_request_bus_scan(device_t self, const char *attr, const int *flags)
{
struct twa_softc *sc = device_private(self);
struct twa_drive *td;
struct twa_request *tr;
struct twa_attach_args twaa;
int locs[TWACF_NLOCS];
int s, unit;
s = splbio();
for (unit = 0; unit < sc->sc_nunits; unit++) {
TAILQ_FOREACH(rq, &sc->twa_busy, tr_link) {
if (tr->tr_request_id == rq->tr_request_id) {
panic("cannot submit same request more than once");
} else if (tr->bp == rq->bp && tr->bp != 0) {
/* XXX A check for 0 for the buf ptr is needed to
* guard against ioctl requests with a buf ptr of
* 0 and also aen notifications. Looking for
* external cmds only.
*/
panic("cannot submit same buf more than once");
} else {
/* Empty else statement */
}
}
}
#endif
static int
twa_start(struct twa_request *tr)
{
struct twa_softc *sc = tr->tr_sc;
uint32_t status_reg;
int s;
int error;
s = splbio();
/*
* The 9650 and 9690 have a bug in the detection of the full queue
* condition.
*
* If a write operation has filled the queue and is directly followed
* by a status read, it sometimes doesn't return the correct result.
* To work around this, the upper 32bit are written first.
* This effectively serialises the hardware, but does not change
* the state of the queue.
*/
if (sc->sc_quirks & TWA_QUIRK_QUEUEFULL_BUG) {
/* Write lower 32 bits of address */
TWA_WRITE_COMMAND_QUEUE_LOW(sc, tr->tr_cmd_phys +
sizeof(struct twa_command_header));
}
/* Check to see if we can post a command. */
status_reg = twa_inl(sc, TWA_STATUS_REGISTER_OFFSET);
if ((error = twa_check_ctlr_state(sc, status_reg)))
goto out;
if (sc->sc_quirks & TWA_QUIRK_QUEUEFULL_BUG) {
/*
* Cmd queue is not full. Post the command
* by writing upper 32 bits of address.
*/
TWA_WRITE_COMMAND_QUEUE_HIGH(sc, tr->tr_cmd_phys +
sizeof(struct twa_command_header));
} else {
/* Cmd queue is not full. Post the command. */
TWA_WRITE_COMMAND_QUEUE(sc, tr->tr_cmd_phys +
sizeof(struct twa_command_header));
}
/* Mark the request as currently being processed. */
tr->tr_status = TWA_CMD_BUSY;
#ifdef DIAGNOSTIC
twa_check_busy_q(tr);
#endif
/* Move the request into the busy queue. */
TAILQ_INSERT_TAIL(&tr->tr_sc->twa_busy, tr, tr_link);
}
out:
splx(s);
return(error);
}
static int
twa_drain_response_queue(struct twa_softc *sc)
{
uint32_t status_reg;
for (;;) {
status_reg = twa_inl(sc, TWA_STATUS_REGISTER_OFFSET);
if (twa_check_ctlr_state(sc, status_reg))
return(1);
if (status_reg & TWA_STATUS_RESPONSE_QUEUE_EMPTY)
return(0); /* no more response queue entries */
(void)twa_inl(sc, TWA_RESPONSE_QUEUE_OFFSET);
}
}
/*
* twa_drain_response_queue_large:
*
* specific to the 9550 and 9650 controller to remove requests.
*
* Removes all requests from "large" response queue on the 9550 controller.
* This procedure is called as part of the 9550 controller reset sequence.
*/
static int
twa_drain_response_queue_large(struct twa_softc *sc, uint32_t timeout)
{
uint32_t start_time = 0, end_time;
uint32_t response = 0;
while ((tr = TAILQ_FIRST(&sc->twa_busy)) != NULL) {
TAILQ_REMOVE(&sc->twa_busy, tr, tr_link);
twa_unmap_request(tr);
if ((tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_INTERNAL) ||
(tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_IOCTL)) {
/* It's an internal/ioctl request. Simply free it. */
if (tr->tr_data)
free(tr->tr_data, M_DEVBUF);
twa_release_request(tr);
} else {
/* It's a SCSI request. Complete it. */
tr->tr_command->command.cmd_pkt_9k.status = EIO;
if (tr->tr_callback)
tr->tr_callback(tr);
}
}
}
static int
twa_drain_pending_queue(struct twa_softc *sc)
{
struct twa_request *tr;
int s, error = 0;
/*
* Pull requests off the pending queue, and submit them.
*/
s = splbio();
while ((tr = TAILQ_FIRST(&sc->twa_pending)) != NULL) {
TAILQ_REMOVE(&sc->twa_pending, tr, tr_link);
if ((error = twa_start(tr))) {
if (error == EBUSY) {
tr->tr_status = TWA_CMD_PENDING;
/* queue at the head */
TAILQ_INSERT_HEAD(&tr->tr_sc->twa_pending,
tr, tr_link);
error = 0;
break;
} else {
if (tr->tr_flags & TWA_CMD_SLEEP_ON_REQUEST) {
tr->tr_error = error;
tr->tr_callback(tr);
error = EIO;
}
}
}
}
splx(s);
free(tr->tr_data, M_DEVBUF);
twa_release_request(tr);
}
out:
if (tr) {
if (tr->tr_data)
free(tr->tr_data, M_DEVBUF);
twa_release_request(tr);
}
return(error);
}
#if 0
static void
twa_check_response_q(struct twa_request *tr, int clear)
{
int j;
static int i = 0;
static struct twa_request *req = 0;
static struct buf *hist[255];
if (clear) {
i = 0;
for (j = 0; j < 255; j++)
hist[j] = 0;
return;
}
if (req == 0)
req = tr;
if ((tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_EXTERNAL) != 0) {
/* XXX this is bogus ! req can't be anything else but tr ! */
if (req->tr_request_id == tr->tr_request_id)
panic("req id: %d on controller queue twice",
tr->tr_request_id);
for (j = 0; j < i; j++)
if (tr->bp == hist[j])
panic("req id: %d buf found twice",
tr->tr_request_id);
}
req = tr;
hist[i++] = req->bp;
}
#endif
static int
twa_done(struct twa_softc *sc)
{
union twa_response_queue rq;
struct twa_request *tr;
int rv = 0;
uint32_t status_reg;
for (;;) {
status_reg = twa_inl(sc, TWA_STATUS_REGISTER_OFFSET);
if ((rv = twa_check_ctlr_state(sc, status_reg)))
break;
if (status_reg & TWA_STATUS_RESPONSE_QUEUE_EMPTY)
break;
/* Response queue is not empty. */
rq.value = twa_inl(sc, TWA_RESPONSE_QUEUE_OFFSET);
tr = sc->sc_twa_request + rq.u.response_id;
#if 0
twa_check_response_q(tr, 0);
#endif
/* Unmap the command packet, and any associated data buffer. */
twa_unmap_request(tr);
/*
* Function name: twa_init_ctlr
* Description: Establishes a logical connection with the controller.
* If bundled with firmware, determines whether or not
* the driver is compatible with the firmware on the
* controller, before proceeding to work with it.
*
* Input: sc -- ptr to per ctlr structure
* Output: None
* Return value: 0 -- success
* non-zero-- failure
*/
static int
twa_init_ctlr(struct twa_softc *sc)
{
uint16_t fw_on_ctlr_srl = 0;
uint16_t fw_on_ctlr_arch_id = 0;
uint16_t fw_on_ctlr_branch = 0;
uint16_t fw_on_ctlr_build = 0;
uint32_t init_connect_result = 0;
int error = 0;
/* Wait for the controller to become ready. */
if (twa_wait_status(sc, TWA_STATUS_MICROCONTROLLER_READY,
TWA_REQUEST_TIMEOUT_PERIOD)) {
return(ENXIO);
}
/* Drain the response queue. */
if (twa_drain_response_queue(sc))
return(1);
/* Establish a logical connection with the controller. */
if ((error = twa_init_connection(sc, TWA_INIT_MESSAGE_CREDITS,
TWA_EXTENDED_INIT_CONNECT, TWA_CURRENT_FW_SRL,
TWA_9000_ARCH_ID, TWA_CURRENT_FW_BRANCH,
TWA_CURRENT_FW_BUILD, &fw_on_ctlr_srl,
&fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
&fw_on_ctlr_build, &init_connect_result))) {
return(error);
}
twa_drain_aen_queue(sc);
/* Set controller state to initialized. */
sc->twa_state &= ~TWA_STATE_SHUTDOWN;
return(0);
}
static int
twa_setup(device_t self)
{
struct twa_softc *sc;
struct tw_cl_event_packet *aen_queue;
uint32_t i = 0;
int error = 0;
/* Allocate memory for the AEN queue. */
if ((aen_queue = malloc(sizeof(struct tw_cl_event_packet) *
TWA_Q_LENGTH, M_DEVBUF, M_WAITOK)) == NULL) {
/*
* This should not cause us to return error. We will only be
* unable to support AEN's. But then, we will have to check
* time and again to see if we can support AEN's, if we
* continue. So, we will just return error.
*/
return (ENOMEM);
}
/* Initialize the aen queue. */
memset(aen_queue, 0, sizeof(struct tw_cl_event_packet) * TWA_Q_LENGTH);
for (i = 0; i < TWA_Q_LENGTH; i++)
sc->twa_aen_queue[i] = &(aen_queue[i]);
/* Let the controller know that we are going down. */
(void)twa_init_connection(sc, TWA_SHUTDOWN_MESSAGE_CREDITS,
0, 0, 0, 0, 0,
NULL, NULL, NULL, NULL, NULL);
}
}
void
twa_register_callbacks(struct twa_softc *sc, int unit,
const struct twa_callbacks *tcb)
{
sc->sc_units[unit].td_callbacks = tcb;
}
/*
* Print autoconfiguration message for a sub-device
*/
static int
twa_print(void *aux, const char *pnp)
{
struct twa_attach_args *twaa;
twaa = aux;
if (pnp !=NULL)
aprint_normal("block device at %s\n", pnp);
aprint_normal(" unit %d\n", twaa->twaa_unit);
return (UNCONF);
}
static void
twa_fillin_sgl(struct twa_sg *sgl, bus_dma_segment_t *segs, int nsegments)
{
int i;
for (i = 0; i < nsegments; i++) {
sgl[i].address = segs[i].ds_addr;
sgl[i].length = (uint32_t)(segs[i].ds_len);
}
}
static int
twa_submit_io(struct twa_request *tr)
{
int error;
if ((error = twa_start(tr))) {
if (error == EBUSY)
error = 0; /* request is in the pending queue */
else {
tr->tr_error = error;
}
}
return(error);
}
/*
* Function name: twa_setup_data_dmamap
* Description: Callback of bus_dmamap_load for the buffer associated
* with data. Updates the cmd pkt (size/sgl_entries
* fields, as applicable) to reflect the number of sg
* elements.
*
* Input: arg -- ptr to request pkt
* segs -- ptr to a list of segment descriptors
* nsegments--# of segments
* error -- 0 if no errors encountered before callback,
* non-zero if errors were encountered
* Output: None
* Return value: None
*/
static int
twa_setup_data_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments)
{
struct twa_request *tr = (struct twa_request *)arg;
struct twa_command_packet *cmdpkt = tr->tr_command;
struct twa_command_9k *cmd9k;
union twa_command_7k *cmd7k;
uint8_t sgl_offset;
int error;
if (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_9K) {
cmd9k = &(cmdpkt->command.cmd_pkt_9k);
twa_fillin_sgl(&(cmd9k->sg_list[0]), segs, nsegments);
cmd9k->sgl_entries += nsegments - 1;
} else {
/* It's a 7000 command packet. */
cmd7k = &(cmdpkt->command.cmd_pkt_7k);
if ((sgl_offset = cmdpkt->command.cmd_pkt_7k.generic.sgl_offset))
twa_fillin_sgl((struct twa_sg *)
(((uint32_t *)cmd7k) + sgl_offset),
segs, nsegments);
/* Modify the size field, based on sg address size. */
cmd7k->generic.size +=
((TWA_64BIT_ADDRESSES ? 3 : 2) * nsegments);
}
if (tr->tr_flags & TWA_CMD_DATA_IN)
bus_dmamap_sync(tr->tr_sc->twa_dma_tag, tr->tr_dma_map, 0,
tr->tr_length, BUS_DMASYNC_PREWRITE);
if (tr->tr_flags & TWA_CMD_DATA_OUT) {
/*
* If we're using an alignment buffer, and we're
* writing data, copy the real data out.
*/
if (tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED)
memcpy(tr->tr_data, tr->tr_real_data,
tr->tr_real_length);
bus_dmamap_sync(tr->tr_sc->twa_dma_tag, tr->tr_dma_map, 0,
tr->tr_length, BUS_DMASYNC_PREREAD);
}
error = twa_submit_io(tr);
if (error) {
twa_unmap_request(tr);
/*
* If the caller had been returned EINPROGRESS, and he has
* registered a callback for handling completion, the callback
* will never get called because we were unable to submit the
* request. So, free up the request right here.
*/
if (tr->tr_callback)
twa_release_request(tr);
}
return (error);
}
/*
* Function name: twa_map_request
* Description: Maps a cmd pkt and data associated with it, into
* DMA'able memory.
*
* Input: tr -- ptr to request pkt
* Output: None
* Return value: 0 -- success
* non-zero-- failure
*/
int
twa_map_request(struct twa_request *tr)
{
struct twa_softc *sc = tr->tr_sc;
int s, rv, rc;
/* If the command involves data, map that too. */
if (tr->tr_data != NULL) {
/*
* Map the data buffer into bus space and build the S/G list.
*/
rv = bus_dmamap_load(sc->twa_dma_tag, tr->tr_dma_map,
tr->tr_data, tr->tr_length, NULL,
BUS_DMA_NOWAIT | BUS_DMA_STREAMING);
if (rv != 0) {
if ((tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED) != 0) {
s = splvm();
uvm_km_kmem_free(kmem_va_arena,
(vaddr_t)tr->tr_data, tr->tr_length);
splx(s);
}
return (rv);
}
if ((rv = twa_setup_data_dmamap(tr,
tr->tr_dma_map->dm_segs,
tr->tr_dma_map->dm_nsegs))) {
if (tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED) {
s = splvm();
uvm_km_kmem_free(kmem_va_arena,
(vaddr_t)tr->tr_data, tr->tr_length);
splx(s);
tr->tr_data = tr->tr_real_data;
tr->tr_length = tr->tr_real_length;
}
}
} else
if ((rv = twa_submit_io(tr)))
twa_unmap_request(tr);
return (rv);
}
/*
* Function name: twa_intr
* Description: Interrupt handler. Determines the kind of interrupt,
* and calls the appropriate handler.
*
* Input: sc -- ptr to per ctlr structure
* Output: None
* Return value: None
*/
static int
twa_intr(void *arg)
{
int caught, s, rv __diagused;
struct twa_softc *sc;
uint32_t status_reg;
sc = (struct twa_softc *)arg;
caught = 0;
/* Collect current interrupt status. */
status_reg = twa_inl(sc, TWA_STATUS_REGISTER_OFFSET);
if (twa_check_ctlr_state(sc, status_reg)) {
caught = 1;
goto bail;
}
/* Dispatch based on the kind of interrupt. */
if (status_reg & TWA_STATUS_HOST_INTERRUPT) {
twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
TWA_CONTROL_CLEAR_HOST_INTERRUPT);
caught = 1;
}
if ((status_reg & TWA_STATUS_ATTENTION_INTERRUPT) != 0) {
twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT);
rv = twa_fetch_aen(sc);
#ifdef DIAGNOSTIC
if (rv != 0)
printf("%s: unable to retrieve AEN (%d)\n",
device_xname(sc->twa_dv), rv);
#endif
caught = 1;
}
if (status_reg & TWA_STATUS_COMMAND_INTERRUPT) {
/* Start any requests that might be in the pending queue. */
twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
TWA_CONTROL_MASK_COMMAND_INTERRUPT);
(void)twa_drain_pending_queue(sc);
caught = 1;
}
if (status_reg & TWA_STATUS_RESPONSE_INTERRUPT) {
s = splbio();
twa_done(sc);
splx(s);
caught = 1;
}
bail:
return (caught);
}
/*
* Accept an open operation on the control device.
*/
static int
twaopen(dev_t dev, int flag, int mode, struct lwp *l)
{
struct twa_softc *twa;
if ((twa = device_lookup_private(&twa_cd, minor(dev))) == NULL)
return (ENXIO);
if ((twa->twa_sc_flags & TWA_STATE_OPEN) != 0)
return (EBUSY);
twa->twa_sc_flags |= TWA_STATE_OPEN;
return (0);
}
/*
* Accept the last close on the control device.
*/
static int
twaclose(dev_t dev, int flag, int mode,
struct lwp *l)
{
struct twa_softc *twa;
twa = device_lookup_private(&twa_cd, minor(dev));
twa->twa_sc_flags &= ~TWA_STATE_OPEN;
return (0);
}
/*
* Function name: twaioctl
* Description: ioctl handler.
*
* Input: sc -- ptr to per ctlr structure
* cmd -- ioctl cmd
* buf -- ptr to buffer in kernel memory, which is
* a copy of the input buffer in user-space
* Output: buf -- ptr to buffer in kernel memory, which will
* be copied of the output buffer in user-space
* Return value: 0 -- success
* non-zero-- failure
*/
static int
twaioctl(dev_t dev, u_long cmd, void *data, int flag,
struct lwp *l)
{
struct twa_softc *sc;
struct twa_ioctl_9k *user_buf = (struct twa_ioctl_9k *)data;
struct tw_cl_event_packet event_buf;
struct twa_request *tr = 0;
int32_t event_index = 0;
int32_t start_index;
int s, error = 0;
/* Get a request packet */
tr = twa_get_request_wait(sc, 0);
KASSERT(tr != NULL);
/*
* Make sure that the data buffer sent to firmware is a
* 512 byte multiple in size.
*/
data_buf_size_adjusted =
(user_buf->twa_drvr_pkt.buffer_length + 511) & ~511;
/* Send down the request, and wait for it to complete. */
if ((error = twa_wait_request(tr, TWA_REQUEST_TIMEOUT_PERIOD))) {
if (error == ETIMEDOUT)
break; /* clean-up done by twa_wait_request */
goto fw_passthru_done;
}
/* Copy the command packet back into user space. */
memcpy(&user_buf->twa_cmd_pkt, cmdpkt,
sizeof(struct twa_command_packet));
/* If there was a payload, copy it back too. */
if (tr->tr_length)
error = copyout(tr->tr_data, user_buf->pdata,
user_buf->twa_drvr_pkt.buffer_length);
fw_passthru_done:
/* Free resources. */
if (tr->tr_data)
free(tr->tr_data, M_DEVBUF);
if (tr)
twa_release_request(tr);
break;
}
case TW_OSL_IOCTL_SCAN_BUS:
twa_request_bus_scan(sc->twa_dv, "twa", 0);
break;
case TW_CL_IOCTL_GET_FIRST_EVENT:
if (sc->twa_aen_queue_wrapped) {
if (sc->twa_aen_queue_overflow) {
/*
* The aen queue has wrapped, even before some
* events have been retrieved. Let the caller
* know that he missed out on some AEN's.
*/
user_buf->twa_drvr_pkt.status =
TWA_ERROR_AEN_OVERFLOW;
sc->twa_aen_queue_overflow = FALSE;
} else
user_buf->twa_drvr_pkt.status = 0;
event_index = sc->twa_aen_head;
} else {
if (sc->twa_aen_head == sc->twa_aen_tail) {
user_buf->twa_drvr_pkt.status =
TWA_ERROR_AEN_NO_EVENTS;
break;
}
user_buf->twa_drvr_pkt.status = 0;
event_index = sc->twa_aen_tail; /* = 0 */
}
if ((error = copyout(sc->twa_aen_queue[event_index],
user_buf->pdata, sizeof(struct tw_cl_event_packet))) != 0)
(sc->twa_aen_queue[event_index])->retrieved =
TWA_AEN_RETRIEVED;
break;
case TW_CL_IOCTL_GET_LAST_EVENT:
if (sc->twa_aen_queue_wrapped) {
if (sc->twa_aen_queue_overflow) {
/*
* The aen queue has wrapped, even before some
* events have been retrieved. Let the caller
* know that he missed out on some AEN's.
*/
user_buf->twa_drvr_pkt.status =
TWA_ERROR_AEN_OVERFLOW;
sc->twa_aen_queue_overflow = FALSE;
} else
user_buf->twa_drvr_pkt.status = 0;
} else {
if (sc->twa_aen_head == sc->twa_aen_tail) {
user_buf->twa_drvr_pkt.status =
TWA_ERROR_AEN_NO_EVENTS;
break;
}
user_buf->twa_drvr_pkt.status = 0;
}
event_index =
(sc->twa_aen_head - 1 + TWA_Q_LENGTH) % TWA_Q_LENGTH;
if ((error = copyout(sc->twa_aen_queue[event_index],
user_buf->pdata, sizeof(struct tw_cl_event_packet))) != 0)
(sc->twa_aen_queue[event_index])->retrieved =
TWA_AEN_RETRIEVED;
break;
case TW_CL_IOCTL_GET_NEXT_EVENT:
user_buf->twa_drvr_pkt.status = 0;
if (sc->twa_aen_queue_wrapped) {
if (sc->twa_aen_queue_overflow) {
/*
* The aen queue has wrapped, even before some
* events have been retrieved. Let the caller
* know that he missed out on some AEN's.
*/
user_buf->twa_drvr_pkt.status =
TWA_ERROR_AEN_OVERFLOW;
sc->twa_aen_queue_overflow = FALSE;
}
start_index = sc->twa_aen_head;
} else {
if (sc->twa_aen_head == sc->twa_aen_tail) {
user_buf->twa_drvr_pkt.status =
TWA_ERROR_AEN_NO_EVENTS;
break;
}
start_index = sc->twa_aen_tail; /* = 0 */
}
error = copyin(user_buf->pdata, &event_buf,
sizeof(struct tw_cl_event_packet));
if (!((sc->twa_aen_queue[event_index])->sequence_id >
event_buf.sequence_id)) {
if (user_buf->twa_drvr_pkt.status ==
TWA_ERROR_AEN_OVERFLOW)
/* so we report the overflow next time */
sc->twa_aen_queue_overflow = TRUE;
user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
break;
}
if ((error = copyout(sc->twa_aen_queue[event_index],
user_buf->pdata, sizeof(struct tw_cl_event_packet))) != 0)
(sc->twa_aen_queue[event_index])->retrieved =
TWA_AEN_RETRIEVED;
break;
case TW_CL_IOCTL_GET_PREVIOUS_EVENT:
user_buf->twa_drvr_pkt.status = 0;
if (sc->twa_aen_queue_wrapped) {
if (sc->twa_aen_queue_overflow) {
/*
* The aen queue has wrapped, even before some
* events have been retrieved. Let the caller
* know that he missed out on some AEN's.
*/
user_buf->twa_drvr_pkt.status =
TWA_ERROR_AEN_OVERFLOW;
sc->twa_aen_queue_overflow = FALSE;
}
start_index = sc->twa_aen_head;
} else {
if (sc->twa_aen_head == sc->twa_aen_tail) {
user_buf->twa_drvr_pkt.status =
TWA_ERROR_AEN_NO_EVENTS;
break;
}
start_index = sc->twa_aen_tail; /* = 0 */
}
if ((error = copyin(user_buf->pdata, &event_buf,
sizeof(struct tw_cl_event_packet))) != 0)
event_index = (start_index + event_buf.sequence_id -
(sc->twa_aen_queue[start_index])->sequence_id - 1)
% TWA_Q_LENGTH;
if (!((sc->twa_aen_queue[event_index])->sequence_id <
event_buf.sequence_id)) {
if (user_buf->twa_drvr_pkt.status ==
TWA_ERROR_AEN_OVERFLOW)
/* so we report the overflow next time */
sc->twa_aen_queue_overflow = TRUE;
user_buf->twa_drvr_pkt.status =
TWA_ERROR_AEN_NO_EVENTS;
break;
}
if ((error = copyout(sc->twa_aen_queue [event_index],
user_buf->pdata, sizeof(struct tw_cl_event_packet))) != 0)
aprint_error_dev(sc->twa_dv, "get_previous: Could not "
"copyout to event_buf. error = %x\n", error);
(sc->twa_aen_queue[event_index])->retrieved = TWA_AEN_RETRIEVED;
break;
case TW_CL_IOCTL_GET_LOCK:
{
struct tw_cl_lock_packet twa_lock;
copyin(user_buf->pdata, &twa_lock,
sizeof(struct tw_cl_lock_packet));
s = splbio();
if ((sc->twa_ioctl_lock.lock == TWA_LOCK_FREE) ||
(twa_lock.force_flag) ||
(time_second >= sc->twa_ioctl_lock.timeout)) {
/* Copy compatibility information to user space. */
copyout(&comp_pkt, user_buf->pdata,
uimin(sizeof(struct tw_cl_compatibility_packet),
user_buf->twa_drvr_pkt.buffer_length));
break;
}
/*
* Function name: twa_get_param
* Description: Get a firmware parameter.
*
* Input: sc -- ptr to per ctlr structure
* table_id -- parameter table #
* param_id -- index of the parameter in the table
* param_size -- size of the parameter in bytes
* callback -- ptr to function, if any, to be called
* back on completion; NULL if no callback.
* Output: None
* Return value: ptr to param structure -- success
* NULL -- failure
*/
static int
twa_get_param(struct twa_softc *sc, int table_id, int param_id,
size_t param_size, void (* callback)(struct twa_request *tr),
struct twa_param_9k **param)
{
int rv = 0;
struct twa_request *tr;
union twa_command_7k *cmd;
/* Get a request packet. */
if ((tr = twa_get_request(sc, 0)) == NULL) {
rv = EAGAIN;
goto out;
}
tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
/* Allocate memory to read data into. */
if ((*param = (struct twa_param_9k *)
malloc(TWA_SECTOR_SIZE, M_DEVBUF, M_NOWAIT)) == NULL) {
rv = ENOMEM;
goto out;
}
/* Specify which parameter we need. */
(*param)->table_id = table_id | TWA_9K_PARAM_DESCRIPTOR;
(*param)->parameter_id = param_id;
(*param)->parameter_size_bytes = param_size;
/* Submit the command. */
if (callback == NULL) {
/* There's no call back; wait till the command completes. */
rv = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
if (rv != 0)
goto out;
if ((rv = cmd->param.status) != 0) {
/* twa_drain_complete_queue will have done the unmapping */
goto out;
}
twa_release_request(tr);
return (rv);
} else {
/* There's a call back. Simply submit the command. */
tr->tr_callback = callback;
rv = twa_map_request(tr);
return (rv);
}
out:
if (tr)
twa_release_request(tr);
return(rv);
}
/*
* Function name: twa_set_param
* Description: Set a firmware parameter.
*
* Input: sc -- ptr to per ctlr structure
* table_id -- parameter table #
* param_id -- index of the parameter in the table
* param_size -- size of the parameter in bytes
* callback -- ptr to function, if any, to be called
* back on completion; NULL if no callback.
* Output: None
* Return value: 0 -- success
* non-zero-- failure
*/
static int
twa_set_param(struct twa_softc *sc, int table_id, int param_id, int param_size,
void *data, void (* callback)(struct twa_request *tr))
{
struct twa_request *tr;
union twa_command_7k *cmd;
struct twa_param_9k *param = NULL;
int error = ENOMEM;
tr = twa_get_request(sc, 0);
if (tr == NULL)
return (EAGAIN);
/* Specify which parameter we want to set. */
param->table_id = table_id | TWA_9K_PARAM_DESCRIPTOR;
param->parameter_id = param_id;
param->parameter_size_bytes = param_size;
memcpy(param->data, data, param_size);
/* Submit the command. */
if (callback == NULL) {
/* There's no call back; wait till the command completes. */
error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
if (error == ETIMEDOUT)
/* clean-up done by twa_immediate_request */
return(error);
if (error)
goto out;
if ((error = cmd->param.status)) {
/*
* twa_drain_complete_queue will have done the
* unmapping.
*/
goto out;
}
free(param, M_DEVBUF);
twa_release_request(tr);
return(error);
} else {
/* There's a call back. Simply submit the command. */
tr->tr_callback = callback;
if ((error = twa_map_request(tr)))
goto out;
return (0);
}
out:
if (param)
free(param, M_DEVBUF);
if (tr)
twa_release_request(tr);
return(error);
}
/*
* Function name: twa_init_connection
* Description: Send init_connection cmd to firmware
*
* Input: sc -- ptr to per ctlr structure
* message_credits -- max # of requests that we might send
* down simultaneously. This will be
* typically set to 256 at init-time or
* after a reset, and to 1 at shutdown-time
* set_features -- indicates if we intend to use 64-bit
* sg, also indicates if we want to do a
* basic or an extended init_connection;
*
* Note: The following input/output parameters are valid, only in case of an
* extended init_connection:
*
* current_fw_srl -- srl of fw we are bundled
* with, if any; 0 otherwise
* current_fw_arch_id -- arch_id of fw we are bundled
* with, if any; 0 otherwise
* current_fw_branch -- branch # of fw we are bundled
* with, if any; 0 otherwise
* current_fw_build -- build # of fw we are bundled
* with, if any; 0 otherwise
* Output: fw_on_ctlr_srl -- srl of fw on ctlr
* fw_on_ctlr_arch_id -- arch_id of fw on ctlr
* fw_on_ctlr_branch -- branch # of fw on ctlr
* fw_on_ctlr_build -- build # of fw on ctlr
* init_connect_result -- result bitmap of fw response
* Return value: 0 -- success
* non-zero-- failure
*/
static int
twa_init_connection(struct twa_softc *sc, uint16_t message_credits,
uint32_t set_features, uint16_t current_fw_srl,
uint16_t current_fw_arch_id, uint16_t current_fw_branch,
uint16_t current_fw_build, uint16_t *fw_on_ctlr_srl,
uint16_t *fw_on_ctlr_arch_id, uint16_t *fw_on_ctlr_branch,
uint16_t *fw_on_ctlr_build, uint32_t *init_connect_result)
{
struct twa_request *tr;
struct twa_command_init_connect *init_connect;
int error = 1;
/* Get a request packet. */
if ((tr = twa_get_request(sc, 0)) == NULL)
goto out;
tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
/* Build the cmd pkt. */
init_connect = &(tr->tr_command->command.cmd_pkt_7k.init_connect);
init_connect->opcode = TWA_OP_INIT_CONNECTION;
init_connect->request_id = tr->tr_request_id;
init_connect->message_credits = message_credits;
init_connect->features = set_features;
if (TWA_64BIT_ADDRESSES)
init_connect->features |= TWA_64BIT_SG_ADDRESSES;
if (set_features & TWA_EXTENDED_INIT_CONNECT) {
/*
* Fill in the extra fields needed for
* an extended init_connect.
*/
init_connect->size = 6;
init_connect->fw_srl = current_fw_srl;
init_connect->fw_arch_id = current_fw_arch_id;
init_connect->fw_branch = current_fw_branch;
} else
init_connect->size = 3;
/* Submit the command, and wait for it to complete. */
error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
if (error == ETIMEDOUT)
return(error); /* clean-up done by twa_immediate_request */
if (error)
goto out;
if ((error = init_connect->status)) {
/* twa_drain_complete_queue will have done the unmapping */
goto out;
}
if (set_features & TWA_EXTENDED_INIT_CONNECT) {
*fw_on_ctlr_srl = init_connect->fw_srl;
*fw_on_ctlr_arch_id = init_connect->fw_arch_id;
*fw_on_ctlr_branch = init_connect->fw_branch;
*fw_on_ctlr_build = init_connect->fw_build;
*init_connect_result = init_connect->result;
}
twa_release_request(tr);
return(error);
out:
if (tr)
twa_release_request(tr);
return(error);
}
static int
twa_reset(struct twa_softc *sc)
{
int s;
int error = 0;
/* Set the 'in reset' flag. */
sc->twa_sc_flags |= TWA_STATE_IN_RESET;
/*
* Disable interrupts from the controller, and mask any
* accidental entry into our interrupt handler.
*/
twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
TWA_CONTROL_DISABLE_INTERRUPTS);
s = splbio();
/* Soft reset the controller. */
if ((error = twa_soft_reset(sc)))
goto out;
/* Re-establish logical connection with the controller. */
if ((error = twa_init_connection(sc, TWA_INIT_MESSAGE_CREDITS,
0, 0, 0, 0, 0,
NULL, NULL, NULL, NULL, NULL))) {
goto out;
}
/*
* Complete all requests in the complete queue; error back all requests
* in the busy queue. Any internal requests will be simply freed.
* Re-submit any requests in the pending queue.
*/
twa_drain_busy_queue(sc);
out:
splx(s);
/*
* Enable interrupts, and also clear attention and response interrupts.
*/
twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT |
TWA_CONTROL_UNMASK_RESPONSE_INTERRUPT |
TWA_CONTROL_ENABLE_INTERRUPTS);
/* Clear the 'in reset' flag. */
sc->twa_sc_flags &= ~TWA_STATE_IN_RESET;
return(error);
}
static int
twa_soft_reset(struct twa_softc *sc)
{
uint32_t status_reg;
/*
* Function name: twa_aen_callback
* Description: Callback for requests to fetch AEN's.
*
* Input: tr -- ptr to completed request pkt
* Output: None
* Return value: None
*/
static void
twa_aen_callback(struct twa_request *tr)
{
int i;
int fetch_more_aens = 0;
struct twa_softc *sc = tr->tr_sc;
struct twa_command_header *cmd_hdr =
(struct twa_command_header *)(tr->tr_data);
struct twa_command_9k *cmd =
&(tr->tr_command->command.cmd_pkt_9k);
if (! cmd->status) {
if ((tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_9K) &&
(cmd->cdb[0] == 0x3 /* REQUEST_SENSE */))
if (twa_enqueue_aen(sc, cmd_hdr)
!= TWA_AEN_QUEUE_EMPTY)
fetch_more_aens = 1;
} else {
cmd_hdr->err_specific_desc[sizeof(cmd_hdr->err_specific_desc) - 1] = '\0';
for (i = 0; i < 18; i++)
printf("%x\t", tr->tr_command->cmd_hdr.sense_data[i]);
printf("\n"); /* print new line */
for (i = 0; i < 128; i++)
printf("%x\t", ((int8_t *)(tr->tr_data))[i]);
printf("\n"); /* print new line */
}
if (tr->tr_data)
free(tr->tr_data, M_DEVBUF);
twa_release_request(tr);
if (fetch_more_aens)
twa_fetch_aen(sc);
}
/*
* Function name: twa_enqueue_aen
* Description: Queues AEN's to be supplied to user-space tools on request.
*
* Input: sc -- ptr to per ctlr structure
* cmd_hdr -- ptr to hdr of fw cmd pkt, from where the AEN
* details can be retrieved.
* Output: None
* Return value: None
*/
static uint16_t
twa_enqueue_aen(struct twa_softc *sc, struct twa_command_header *cmd_hdr)
{
int rv __diagused, s;
struct tw_cl_event_packet *event;
uint16_t aen_code;
unsigned long sync_time;
s = splbio();
aen_code = cmd_hdr->status_block.error;
switch (aen_code) {
case TWA_AEN_SYNC_TIME_WITH_HOST:
sync_time = (time_second - (3 * 86400)) % 604800;
rv = twa_set_param(sc, TWA_PARAM_TIME_TABLE,
TWA_PARAM_TIME_SchedulerTime, 4,
&sync_time, twa_aen_callback);
#ifdef DIAGNOSTIC
if (rv != 0)
aprint_error_dev(sc->twa_dv,
"unable to sync time with ctlr\n");
#endif
break;
/*
* Function name: twa_find_aen
* Description: Reports whether a given AEN ever occurred.
*
* Input: sc -- ptr to per ctlr structure
* aen_code-- AEN to look for
* Output: None
* Return value: 0 -- success
* non-zero-- failure
*/
static int
twa_find_aen(struct twa_softc *sc, uint16_t aen_code)
{
uint32_t last_index;
int s;
int i;
s = splbio();
if (sc->twa_aen_queue_wrapped)
last_index = sc->twa_aen_head;
else
last_index = 0;
i = sc->twa_aen_head;
do {
i = (i + TWA_Q_LENGTH - 1) % TWA_Q_LENGTH;
if ((sc->twa_aen_queue[i])->aen_code == aen_code) {
splx(s);
return(0);
}
} while (i != last_index);
/*
* Look at the status field in the command packet to see how
* it completed the last time it was used, and zero out only
* the portions that might have changed. Note that we don't
* care to zero out the sglist.
*/
if (tr->tr_command->command.cmd_pkt_9k.status)
memset(tr->tr_command, 0,
sizeof(struct twa_command_header) + 28);
else
memset(&(tr->tr_command->command), 0, 28);
}
struct twa_request *
twa_get_request_wait(struct twa_softc *sc, int flags)
{
struct twa_request *tr;
int s;
/*
* Print some information about the controller
*/
static void
twa_describe_controller(struct twa_softc *sc)
{
struct twa_param_9k *p[10];
int i, rv = 0;
uint32_t dsize;
uint8_t ports;
memset(p, 0, sizeof(p));
/* Get the port count. */
rv |= twa_get_param(sc, TWA_PARAM_CONTROLLER,
TWA_PARAM_CONTROLLER_PortCount, 1, NULL, &p[0]);
if (p[8])
free(p[8], M_DEVBUF);
if (p[9])
free(p[9], M_DEVBUF);
}
bail:
if (p[0])
free(p[0], M_DEVBUF);
if (p[1])
free(p[1], M_DEVBUF);
if (p[2])
free(p[2], M_DEVBUF);
if (p[3])
free(p[3], M_DEVBUF);
if (p[4])
free(p[4], M_DEVBUF);
if (p[5])
free(p[5], M_DEVBUF);
if (p[6])
free(p[6], M_DEVBUF);
}
/*
* Function name: twa_check_ctlr_state
* Description: Makes sure that the fw status register reports a
* proper status.
*
* Input: sc -- ptr to per ctlr structure
* status_reg -- value in the status register
* Output: None
* Return value: 0 -- no errors
* non-zero-- errors
*/
static int
twa_check_ctlr_state(struct twa_softc *sc, uint32_t status_reg)
{
int result = 0;
struct timeval t1;
static time_t last_warning[2] = {0, 0};
/* Check if the 'micro-controller ready' bit is not set. */
if ((status_reg & TWA_STATUS_EXPECTED_BITS) !=
TWA_STATUS_EXPECTED_BITS) {
microtime(&t1);
last_warning[0] += (5 * 1000 * 100);
if (t1.tv_usec > last_warning[0]) {
microtime(&t1);
last_warning[0] = t1.tv_usec;
}
result = 1;
}
/* Check if any error bits are set. */
if ((status_reg & TWA_STATUS_UNEXPECTED_BITS) != 0) {
microtime(&t1);
last_warning[1] += (5 * 1000 * 100);
if (t1.tv_usec > last_warning[1]) {
microtime(&t1);
last_warning[1] = t1.tv_usec;
}
if (status_reg & TWA_STATUS_PCI_PARITY_ERROR_INTERRUPT) {
aprint_error_dev(sc->twa_dv, "clearing PCI parity "
"error re-seat/move/replace card.\n");
twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
TWA_CONTROL_CLEAR_PARITY_ERROR);
pci_conf_write(sc->pc, sc->tag,
PCI_COMMAND_STATUS_REG,
TWA_PCI_CONFIG_CLEAR_PARITY_ERROR);
}
if (status_reg & TWA_STATUS_PCI_ABORT_INTERRUPT) {
aprint_error_dev(sc->twa_dv, "clearing PCI abort\n");
twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
TWA_CONTROL_CLEAR_PCI_ABORT);
pci_conf_write(sc->pc, sc->tag,
PCI_COMMAND_STATUS_REG,
TWA_PCI_CONFIG_CLEAR_PCI_ABORT);
}
if (status_reg & TWA_STATUS_QUEUE_ERROR_INTERRUPT) {
/*
* As documented by 3ware, the 9650 erroneously
* flags queue errors during resets.
* Just ignore them during the reset instead of
* bothering the console.
*/
if ((sc->sc_product_id != PCI_PRODUCT_3WARE_9650) ||
((sc->twa_sc_flags & TWA_STATE_IN_RESET) == 0)) {
aprint_error_dev(sc->twa_dv,
"clearing controller queue error\n");
}
twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
TWA_CONTROL_CLEAR_QUEUE_ERROR);
}
if (status_reg & TWA_STATUS_MICROCONTROLLER_ERROR) {
aprint_error_dev(sc->twa_dv,
"micro-controller error\n");
result = 1;
}
}
return(result);
}
MODULE(MODULE_CLASS_DRIVER, twa, "pci");
#ifdef _MODULE
#include "ioconf.c"
#endif
static int
twa_modcmd(modcmd_t cmd, void *opaque)
{
int error = 0;