/*-
* Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Andrew Doran.
*
* 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) 1999,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.
*
* from FreeBSD: amr_pci.c,v 1.5 2000/08/30 07:52:40 msmith Exp
* from FreeBSD: amr.c,v 1.16 2000/08/30 07:52:40 msmith Exp
*/
/*
* Match a supported device.
*/
static int
amr_match(device_t parent, cfdata_t match, void *aux)
{
struct pci_attach_args *pa;
pcireg_t s;
int i;
pa = (struct pci_attach_args *)aux;
/*
* Don't match the device if it's operating in I2O mode. In this
* case it should be handled by the `iop' driver.
*/
if (PCI_CLASS(pa->pa_class) == PCI_CLASS_I2O)
return (0);
for (i = 0; i < sizeof(amr_pci_type) / sizeof(amr_pci_type[0]); i++)
if (PCI_VENDOR(pa->pa_id) == amr_pci_type[i].apt_vendor &&
PCI_PRODUCT(pa->pa_id) == amr_pci_type[i].apt_product)
break;
if (i == sizeof(amr_pci_type) / sizeof(amr_pci_type[0]))
return (0);
if ((amr_pci_type[i].apt_flags & AT_SIG) == 0)
return (1);
s = pci_conf_read(pa->pa_pc, pa->pa_tag, AMR_QUARTZ_SIG_REG) & 0xffff;
return (s == AMR_QUARTZ_SIG0 || s == AMR_QUARTZ_SIG1);
}
/* Map and establish the interrupt. */
if (pci_intr_map(pa, &ih)) {
aprint_error("can't map interrupt\n");
amr_teardown(amr);
return;
}
intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf));
amr->amr_ih = pci_intr_establish_xname(pc, ih, IPL_BIO, amr_intr, amr,
device_xname(self));
if (amr->amr_ih == NULL) {
aprint_error("can't establish interrupt");
if (intrstr != NULL)
aprint_error(" at %s", intrstr);
aprint_error("\n");
amr_teardown(amr);
return;
}
amr->amr_flags |= AMRF_PCI_INTR;
/*
* Allocate space for the mailbox and S/G lists. Some controllers
* don't like S/G lists to be located below 0x2000, so we allocate
* enough slop to enable us to compensate.
*
* The standard mailbox structure needs to be aligned on a 16-byte
* boundary. The 64-bit mailbox has one extra field, 4 bytes in
* size, which precedes the standard mailbox.
*/
size = AMR_SGL_SIZE * AMR_MAX_CMDS + 0x2000;
amr->amr_dmasize = size;
/*
* Retrieve parameters, and tell the world about us.
*/
amr->amr_enqbuf = malloc(AMR_ENQUIRY_BUFSIZE, M_DEVBUF, M_WAITOK);
amr->amr_flags |= AMRF_ENQBUF;
amr->amr_maxqueuecnt = i;
aprint_normal(": AMI RAID ");
if (amr_init(amr, intrstr, pa) != 0) {
amr_teardown(amr);
return;
}
/*
* Cap the maximum number of outstanding commands. AMI's Linux
* driver doesn't trust the controller's reported value, and lockups
* have been seen when we do.
*/
amr->amr_maxqueuecnt = uimin(amr->amr_maxqueuecnt, AMR_MAX_CMDS);
if (amr->amr_maxqueuecnt > i)
amr->amr_maxqueuecnt = i;
/* Set our `shutdownhook' before we start any device activity. */
if (amr_sdh == NULL)
amr_sdh = shutdownhook_establish(amr_shutdown, NULL);
/*
* Try to get 40LD product info, which tells us what the card is
* labelled as.
*/
ap = amr_enquire(amr, AMR_CMD_CONFIG, AMR_CONFIG_PRODUCT_INFO, 0,
amr->amr_enqbuf);
if (ap != NULL) {
aprint_normal("<%.80s>\n", ap->ap_product);
if (intrstr != NULL)
aprint_normal_dev(amr->amr_dv, "interrupting at %s\n",
intrstr);
aprint_normal_dev(amr->amr_dv,
"firmware %.16s, BIOS %.16s, %dMB RAM\n",
ap->ap_firmware, ap->ap_bios, le16toh(ap->ap_memsize));
amr->amr_maxqueuecnt = ap->ap_maxio;
/*
* Fetch and record state of logical drives.
*/
aex = amr_enquire(amr, AMR_CMD_CONFIG, AMR_CONFIG_ENQ3,
AMR_CONFIG_ENQ3_SOLICITED_FULL, amr->amr_enqbuf);
if (aex == NULL) {
aprint_error_dev(amr->amr_dv, "ENQUIRY3 failed\n");
return (-1);
}
if (aex->ae_numldrives > __arraycount(aex->ae_drivestate)) {
aprint_error_dev(amr->amr_dv, "Inquiry returned more "
"drives (%d) than the array can handle (%zu)\n",
aex->ae_numldrives,
__arraycount(aex->ae_drivestate));
aex->ae_numldrives = __arraycount(aex->ae_drivestate);
}
if (aex->ae_numldrives > AMR_MAX_UNITS) {
aprint_error_dev(amr->amr_dv,
"adjust AMR_MAX_UNITS to %d (currently %d)\n",
AMR_MAX_UNITS, amr->amr_numdrives);
amr->amr_numdrives = AMR_MAX_UNITS;
} else
amr->amr_numdrives = aex->ae_numldrives;
for (i = 0; i < amr->amr_numdrives; i++) {
amr->amr_drive[i].al_size =
le32toh(aex->ae_drivesize[i]);
amr->amr_drive[i].al_state = aex->ae_drivestate[i];
amr->amr_drive[i].al_properties = aex->ae_driveprop[i];
}
return (0);
}
/*
* Try 8LD extended ENQUIRY to get the controller signature. Once
* found, search for a product description.
*/
ae = amr_enquire(amr, AMR_CMD_EXT_ENQUIRY2, 0, 0, amr->amr_enqbuf);
if (ae != NULL) {
i = 0;
sig = le32toh(ae->ae_signature);
while (i < sizeof(amr_typestr) / sizeof(amr_typestr[0])) {
if (amr_typestr[i].at_sig == sig)
break;
i++;
}
if (i == sizeof(amr_typestr) / sizeof(amr_typestr[0])) {
snprintf(sbuf, sizeof(sbuf),
"unknown ENQUIRY2 sig (0x%08x)", sig);
prodstr = sbuf;
} else
prodstr = amr_typestr[i].at_str;
} else {
ae = amr_enquire(amr, AMR_CMD_ENQUIRY, 0, 0, amr->amr_enqbuf);
if (ae == NULL) {
aprint_error_dev(amr->amr_dv,
"unsupported controller\n");
return (-1);
}
/*
* HP NetRaid controllers have a special encoding of the firmware
* and BIOS versions. The AMI version seems to have it as strings
* whereas the HP version does it with a leading uppercase character
* and two binary numbers.
*/
aa = &ae->ae_adapter;
if (aa->aa_firmware[2] >= 'A' && aa->aa_firmware[2] <= 'Z' &&
aa->aa_firmware[1] < ' ' && aa->aa_firmware[0] < ' ' &&
aa->aa_bios[2] >= 'A' && aa->aa_bios[2] <= 'Z' &&
aa->aa_bios[1] < ' ' && aa->aa_bios[0] < ' ') {
if (le32toh(ae->ae_signature) == AMR_SIG_438) {
/* The AMI 438 is a NetRaid 3si in HP-land. */
prodstr = "HP NetRaid 3si";
}
ishp = 1;
} else
ishp = 0;
aprint_normal("<%s>\n", prodstr);
if (intrstr != NULL)
aprint_normal_dev(amr->amr_dv, "interrupting at %s\n",
intrstr);
/*
* Record state of logical drives.
*/
if (ae->ae_ldrv.al_numdrives > __arraycount(ae->ae_ldrv.al_size)) {
aprint_error_dev(amr->amr_dv, "Inquiry returned more drives "
"(%d) than the array can handle (%zu)\n",
ae->ae_ldrv.al_numdrives,
__arraycount(ae->ae_ldrv.al_size));
ae->ae_ldrv.al_numdrives = __arraycount(ae->ae_ldrv.al_size);
}
if (ae->ae_ldrv.al_numdrives > AMR_MAX_UNITS) {
aprint_error_dev(amr->amr_dv,
"adjust AMR_MAX_UNITS to %d (currently %d)\n",
ae->ae_ldrv.al_numdrives, AMR_MAX_UNITS);
amr->amr_numdrives = AMR_MAX_UNITS;
} else
amr->amr_numdrives = ae->ae_ldrv.al_numdrives;
for (i = 0; i < amr->amr_numdrives; i++) {
amr->amr_drive[i].al_size = le32toh(ae->ae_ldrv.al_size[i]);
amr->amr_drive[i].al_state = ae->ae_ldrv.al_state[i];
amr->amr_drive[i].al_properties = ae->ae_ldrv.al_properties[i];
}
return (0);
}
/*
* Flush the internal cache on each configured controller. Called at
* shutdown time.
*/
static void
amr_shutdown(void *cookie)
{
extern struct cfdriver amr_cd;
struct amr_softc *amr;
struct amr_ccb *ac;
int i, rv;
for (i = 0; i < amr_cd.cd_ndevs; i++) {
if ((amr = device_lookup_private(&amr_cd, i)) == NULL)
continue;
/*
* Interrupt service routine.
*/
static int
amr_intr(void *cookie)
{
struct amr_softc *amr;
struct amr_ccb *ac;
struct amr_mailbox_resp mbox;
u_int i, forus, idx;
amr = cookie;
forus = 0;
mutex_spin_enter(&amr->amr_mutex);
while ((*amr->amr_get_work)(amr, &mbox) == 0) {
/* Iterate over completed commands in this result. */
for (i = 0; i < mbox.mb_nstatus; i++) {
idx = mbox.mb_completed[i] - 1;
ac = amr->amr_ccbs + idx;
if (idx >= amr->amr_maxqueuecnt) {
printf("%s: bad status (bogus ID: %u=%u)\n",
device_xname(amr->amr_dv), i, idx);
continue;
}
if ((ac->ac_flags & AC_ACTIVE) == 0) {
printf("%s: bad status (not active; 0x04%x)\n",
device_xname(amr->amr_dv), ac->ac_flags);
continue;
}
al = amr->amr_drive;
for (i = 0; i < __arraycount(ae->ae_ldrv.al_state); i++, al++) {
if (al->al_dv == NULL)
continue;
if (al->al_state == ae->ae_ldrv.al_state[i])
continue;
/*
* If a CCB is specified, enqueue it. Pull CCBs off the software queue in
* the order that they were enqueued and try to submit their command blocks
* to the controller for execution.
*/
void
amr_ccb_enqueue(struct amr_softc *amr, struct amr_ccb *ac)
{
if (ac != NULL) {
mutex_spin_enter(&amr->amr_mutex);
SIMPLEQ_INSERT_TAIL(&amr->amr_ccb_queue, ac, ac_chain.simpleq);
mutex_spin_exit(&amr->amr_mutex);
}
/*
* Map the specified CCB's data buffer onto the bus, and fill the
* scatter-gather list.
*/
int
amr_ccb_map(struct amr_softc *amr, struct amr_ccb *ac, void *data, int size,
int tflag)
{
struct amr_sgentry *sge;
struct amr_mailbox_cmd *mb;
int nsegs, i, rv, sgloff;
bus_dmamap_t xfer;
int dmaflag = 0;
if (tflag & AC_XFER_OUT)
dmaflag |= BUS_DMASYNC_PREWRITE;
if (tflag & AC_XFER_IN)
dmaflag |= BUS_DMASYNC_PREREAD;
/* We don't need to use a scatter/gather list for just 1 segment. */
nsegs = xfer->dm_nsegs;
if (nsegs == 1) {
mb->mb_nsgelem = 0;
mb->mb_physaddr = htole32(xfer->dm_segs[0].ds_addr);
ac->ac_flags |= AC_NOSGL;
} else {
mb->mb_nsgelem = nsegs;
mb->mb_physaddr = htole32(amr->amr_sgls_paddr + sgloff);
sge = (struct amr_sgentry *)((char *)amr->amr_sgls + sgloff);
for (i = 0; i < nsegs; i++, sge++) {
sge->sge_addr = htole32(xfer->dm_segs[i].ds_addr);
sge->sge_count = htole32(xfer->dm_segs[i].ds_len);
}
}
/*
* Submit a command to the controller and poll on completion. Return
* non-zero on timeout or error.
*/
int
amr_ccb_poll(struct amr_softc *amr, struct amr_ccb *ac, int timo)
{
int rv, i;
/*
* Submit a command to the controller and sleep on completion. Return
* non-zero on error.
*/
int
amr_ccb_wait(struct amr_softc *amr, struct amr_ccb *ac)
{
amr_ccb_enqueue(amr, ac);
mutex_enter(&ac->ac_mutex);
cv_wait(&ac->ac_cv, &ac->ac_mutex);
mutex_exit(&ac->ac_mutex);
return (ac->ac_status != 0 ? EIO : 0);
}
#if 0
/*
* Wait for the mailbox to become available.
*/
static int
amr_mbox_wait(struct amr_softc *amr)
{
int timo;
for (timo = 10000; timo != 0; timo--) {
bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
sizeof(struct amr_mailbox), BUS_DMASYNC_POSTREAD);
if (amr->amr_mbox->mb_cmd.mb_busy == 0)
break;
DELAY(100);
}
if (timo == 0)
printf("%s: controller wedged\n", device_xname(amr->amr_dv));
return (timo != 0 ? 0 : EAGAIN);
}
#endif
/*
* Tell the controller that the mailbox contains a valid command. Must be
* called with interrupts blocked.
*/
static int
amr_quartz_submit(struct amr_softc *amr, struct amr_ccb *ac)
{
int i = 0;
u_int32_t v;
/*
* Claim any work that the controller has completed; acknowledge completion,
* save details of the completion in (mbsave). Must be called with
* interrupts blocked.
*/
static int
amr_quartz_get_work(struct amr_softc *amr, struct amr_mailbox_resp *mbsave)
{
bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
sizeof(struct amr_mailbox), BUS_DMASYNC_PREREAD);
/* Work waiting for us? */
if (amr_inl(amr, AMR_QREG_ODB) != AMR_QODB_READY)
return (-1);
/* Save the mailbox, which contains a list of completed commands. */
memcpy(mbsave, &amr->amr_mbox->mb_resp, sizeof(*mbsave));
/* Ack the interrupt and mailbox transfer. */
amr_outl(amr, AMR_QREG_ODB, AMR_QODB_READY);
amr_outl(amr, AMR_QREG_IDB, (amr->amr_mbox_paddr+16) | AMR_QIDB_ACK);
/*
* This waits for the controller to notice that we've taken the
* command from it. It's very inefficient, and we shouldn't do it,
* but if we remove this code, we stop completing commands under
* load.
*
* Peter J says we shouldn't do this. The documentation says we
* should. Who is right?
*/
while ((amr_inl(amr, AMR_QREG_IDB) & AMR_QIDB_ACK) != 0)
DELAY(10);
/*
* allocate kernel memory for data, doing I/O directly to user
* buffer isn't that easy. Correct allocation size for a bug
* in at least some versions of the device firmware, by using
* the amrioctl_buflen() function, defined above.
*/
dp = malloc(amrioctl_buflen(au_length), M_DEVBUF, M_WAITOK|M_ZERO);
if (dp == NULL)
return ENOMEM;
if ((error = copyin(au_buffer, dp, au_length)) != 0)
goto out;
/* direct command to controller */
while (amr_ccb_alloc(amr, &ac) != 0) {
mutex_enter(&thread_mutex);
error = cv_timedwait_sig(&thread_cv, &thread_mutex, hz);
mutex_exit(&thread_mutex);
if (error == EINTR)
goto out;
}