amr.c revision 68877
151974Smsmith/*- 265245Smsmith * Copyright (c) 1999,2000 Michael Smith 365245Smsmith * Copyright (c) 2000 BSDi 451974Smsmith * All rights reserved. 551974Smsmith * 651974Smsmith * Redistribution and use in source and binary forms, with or without 751974Smsmith * modification, are permitted provided that the following conditions 851974Smsmith * are met: 951974Smsmith * 1. Redistributions of source code must retain the above copyright 1051974Smsmith * notice, this list of conditions and the following disclaimer. 1151974Smsmith * 2. Redistributions in binary form must reproduce the above copyright 1251974Smsmith * notice, this list of conditions and the following disclaimer in the 1351974Smsmith * documentation and/or other materials provided with the distribution. 1451974Smsmith * 1551974Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1651974Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1751974Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1851974Smsmith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1951974Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2051974Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2151974Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2251974Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2351974Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2451974Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2551974Smsmith * SUCH DAMAGE. 2651974Smsmith * 2751974Smsmith * $FreeBSD: head/sys/dev/amr/amr.c 68877 2000-11-18 15:21:22Z dwmalone $ 2851974Smsmith */ 2951974Smsmith 3051974Smsmith/* 3165245Smsmith * Driver for the AMI MegaRaid family of controllers. 3251974Smsmith */ 3351974Smsmith 3451974Smsmith#include <sys/param.h> 3551974Smsmith#include <sys/systm.h> 3651974Smsmith#include <sys/malloc.h> 3751974Smsmith#include <sys/kernel.h> 3851974Smsmith 3965245Smsmith#include <dev/amr/amr_compat.h> 4051974Smsmith#include <sys/bus.h> 4151974Smsmith#include <sys/conf.h> 4251974Smsmith#include <sys/devicestat.h> 4351974Smsmith#include <sys/disk.h> 4465245Smsmith#include <sys/stat.h> 4551974Smsmith 4665245Smsmith#include <machine/bus_memio.h> 4765245Smsmith#include <machine/bus_pio.h> 4865245Smsmith#include <machine/bus.h> 4951974Smsmith#include <machine/resource.h> 5051974Smsmith#include <sys/rman.h> 5151974Smsmith 5265245Smsmith#include <pci/pcireg.h> 5365245Smsmith#include <pci/pcivar.h> 5465245Smsmith 5551974Smsmith#include <dev/amr/amrio.h> 5651974Smsmith#include <dev/amr/amrreg.h> 5751974Smsmith#include <dev/amr/amrvar.h> 5865245Smsmith#define AMR_DEFINE_TABLES 5965245Smsmith#include <dev/amr/amr_tables.h> 6051974Smsmith 6151974Smsmith#define AMR_CDEV_MAJOR 132 6251974Smsmith 6365245Smsmithstatic d_open_t amr_open; 6465245Smsmithstatic d_close_t amr_close; 6565245Smsmithstatic d_ioctl_t amr_ioctl; 6665245Smsmith 6751974Smsmithstatic struct cdevsw amr_cdevsw = { 6851974Smsmith /* open */ amr_open, 6951974Smsmith /* close */ amr_close, 7051974Smsmith /* read */ noread, 7151974Smsmith /* write */ nowrite, 7251974Smsmith /* ioctl */ amr_ioctl, 7351974Smsmith /* poll */ nopoll, 7451974Smsmith /* mmap */ nommap, 7551974Smsmith /* strategy */ nostrategy, 7651974Smsmith /* name */ "amr", 7751974Smsmith /* maj */ AMR_CDEV_MAJOR, 7851974Smsmith /* dump */ nodump, 7951974Smsmith /* psize */ nopsize, 8051974Smsmith /* flags */ 0, 8151974Smsmith /* bmaj */ 254 /* XXX magic no-bdev */ 8251974Smsmith}; 8351974Smsmith 8465245Smsmith/* 8565245Smsmith * Initialisation, bus interface. 8665245Smsmith */ 8765245Smsmithstatic void amr_startup(void *arg); 8851974Smsmith 8951974Smsmith/* 9051974Smsmith * Command wrappers 9151974Smsmith */ 9265245Smsmithstatic int amr_query_controller(struct amr_softc *sc); 9365245Smsmithstatic void *amr_enquiry(struct amr_softc *sc, size_t bufsize, 9465245Smsmith u_int8_t cmd, u_int8_t cmdsub, u_int8_t cmdqual); 9565245Smsmithstatic void amr_completeio(struct amr_command *ac); 9651974Smsmith 9751974Smsmith/* 9865245Smsmith * Command buffer allocation. 9951974Smsmith */ 10065245Smsmithstatic void amr_alloccmd_cluster(struct amr_softc *sc); 10165245Smsmithstatic void amr_freecmd_cluster(struct amr_command_cluster *acc); 10251974Smsmith 10351974Smsmith/* 10465245Smsmith * Command processing. 10551974Smsmith */ 10665245Smsmithstatic int amr_bio_command(struct amr_softc *sc, struct amr_command **acp); 10765245Smsmithstatic int amr_wait_command(struct amr_command *ac); 10865245Smsmithstatic int amr_poll_command(struct amr_command *ac); 10965245Smsmithstatic int amr_getslot(struct amr_command *ac); 11065245Smsmithstatic void amr_mapcmd(struct amr_command *ac); 11165245Smsmithstatic void amr_unmapcmd(struct amr_command *ac); 11265245Smsmithstatic int amr_start(struct amr_command *ac); 11365245Smsmithstatic void amr_complete(void *context, int pending); 11451974Smsmith 11551974Smsmith/* 11658883Smsmith * Status monitoring 11758883Smsmith */ 11865245Smsmithstatic void amr_periodic(void *data); 11958883Smsmith 12058883Smsmith/* 12151974Smsmith * Interface-specific shims 12251974Smsmith */ 12365245Smsmithstatic int amr_quartz_submit_command(struct amr_softc *sc); 12465245Smsmithstatic int amr_quartz_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave); 12551974Smsmith 12665245Smsmithstatic int amr_std_submit_command(struct amr_softc *sc); 12765245Smsmithstatic int amr_std_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave); 12865245Smsmithstatic void amr_std_attach_mailbox(struct amr_softc *sc); 12951974Smsmith 13065245Smsmith#ifdef AMR_BOARD_INIT 13165245Smsmithstatic int amr_quartz_init(struct amr_softc *sc); 13265245Smsmithstatic int amr_std_init(struct amr_softc *sc); 13365245Smsmith#endif 13465245Smsmith 13551974Smsmith/* 13651974Smsmith * Debugging 13751974Smsmith */ 13865245Smsmithstatic void amr_describe_controller(struct amr_softc *sc); 13965245Smsmith#ifdef AMR_DEBUG 14065245Smsmithstatic void amr_printcommand(struct amr_command *ac); 14165245Smsmith#endif 14251974Smsmith 14351974Smsmith/******************************************************************************** 14451974Smsmith ******************************************************************************** 14565245Smsmith Inline Glue 14651974Smsmith ******************************************************************************** 14751974Smsmith ********************************************************************************/ 14851974Smsmith 14951974Smsmith/******************************************************************************** 15065245Smsmith ******************************************************************************** 15165245Smsmith Public Interfaces 15265245Smsmith ******************************************************************************** 15365245Smsmith ********************************************************************************/ 15451974Smsmith 15551974Smsmith/******************************************************************************** 15651974Smsmith * Initialise the controller and softc. 15751974Smsmith */ 15851974Smsmithint 15951974Smsmithamr_attach(struct amr_softc *sc) 16051974Smsmith{ 16151974Smsmith 16265245Smsmith debug_called(1); 16365245Smsmith 16451974Smsmith /* 16551974Smsmith * Initialise per-controller queues. 16651974Smsmith */ 16765245Smsmith TAILQ_INIT(&sc->amr_completed); 16851974Smsmith TAILQ_INIT(&sc->amr_freecmds); 16965245Smsmith TAILQ_INIT(&sc->amr_cmd_clusters); 17065245Smsmith TAILQ_INIT(&sc->amr_ready); 17159249Sphk bioq_init(&sc->amr_bioq); 17251974Smsmith 17365245Smsmith#if __FreeBSD_version >= 500005 17451974Smsmith /* 17565245Smsmith * Initialise command-completion task. 17665245Smsmith */ 17765245Smsmith TASK_INIT(&sc->amr_task_complete, 0, amr_complete, sc); 17865245Smsmith#endif 17965245Smsmith 18065245Smsmith debug(2, "queue init done"); 18165245Smsmith 18265245Smsmith /* 18351974Smsmith * Configure for this controller type. 18451974Smsmith */ 18565245Smsmith if (AMR_IS_QUARTZ(sc)) { 18651974Smsmith sc->amr_submit_command = amr_quartz_submit_command; 18751974Smsmith sc->amr_get_work = amr_quartz_get_work; 18851974Smsmith } else { 18951974Smsmith sc->amr_submit_command = amr_std_submit_command; 19051974Smsmith sc->amr_get_work = amr_std_get_work; 19165245Smsmith amr_std_attach_mailbox(sc);; 19251974Smsmith } 19351974Smsmith 19465245Smsmith#ifdef AMR_BOARD_INIT 19565245Smsmith if ((AMR_IS_QUARTZ(sc) ? amr_quartz_init(sc) : amr_std_init(sc)))) 19665245Smsmith return(ENXIO); 19765245Smsmith#endif 19851974Smsmith 19951974Smsmith /* 20065245Smsmith * Quiz controller for features and limits. 20151974Smsmith */ 20265245Smsmith if (amr_query_controller(sc)) 20365245Smsmith return(ENXIO); 20451974Smsmith 20565245Smsmith debug(2, "controller query complete"); 20651974Smsmith 20765245Smsmith#ifdef AMR_SCSI_PASSTHROUGH 20851974Smsmith /* 20965245Smsmith * Attach our 'real' SCSI channels to CAM. 21051974Smsmith */ 21165245Smsmith if (amr_cam_attach(sc)) 21251974Smsmith return(ENXIO); 21365245Smsmith debug(2, "CAM attach done"); 21465245Smsmith#endif 21551974Smsmith 21651974Smsmith /* 21765245Smsmith * Create the control device. 21851974Smsmith */ 21965245Smsmith sc->amr_dev_t = make_dev(&amr_cdevsw, device_get_unit(sc->amr_dev), UID_ROOT, GID_OPERATOR, 22065245Smsmith S_IRUSR | S_IWUSR, "amr%d", device_get_unit(sc->amr_dev)); 22165245Smsmith sc->amr_dev_t->si_drv1 = sc; 22251974Smsmith 22351974Smsmith /* 22465245Smsmith * Schedule ourselves to bring the controller up once interrupts are 22565245Smsmith * available. 22651974Smsmith */ 22765245Smsmith bzero(&sc->amr_ich, sizeof(struct intr_config_hook)); 22865245Smsmith sc->amr_ich.ich_func = amr_startup; 22965245Smsmith sc->amr_ich.ich_arg = sc; 23065245Smsmith if (config_intrhook_establish(&sc->amr_ich) != 0) { 23165245Smsmith device_printf(sc->amr_dev, "can't establish configuration hook\n"); 23265245Smsmith return(ENOMEM); 23365245Smsmith } 23451974Smsmith 23558883Smsmith /* 23665245Smsmith * Print a little information about the controller. 23758883Smsmith */ 23865245Smsmith amr_describe_controller(sc); 23958883Smsmith 24065245Smsmith debug(2, "attach complete"); 24151974Smsmith return(0); 24251974Smsmith} 24351974Smsmith 24451974Smsmith/******************************************************************************** 24551974Smsmith * Locate disk resources and attach children to them. 24651974Smsmith */ 24765245Smsmithstatic void 24865245Smsmithamr_startup(void *arg) 24951974Smsmith{ 25065245Smsmith struct amr_softc *sc = (struct amr_softc *)arg; 25151974Smsmith struct amr_logdrive *dr; 25251974Smsmith int i, error; 25351974Smsmith 25465245Smsmith debug_called(1); 25551974Smsmith 25665245Smsmith /* pull ourselves off the intrhook chain */ 25765245Smsmith config_intrhook_disestablish(&sc->amr_ich); 25865245Smsmith 25951974Smsmith /* get up-to-date drive information */ 26051974Smsmith if (amr_query_controller(sc)) { 26165245Smsmith device_printf(sc->amr_dev, "can't scan controller for drives\n"); 26251974Smsmith return; 26351974Smsmith } 26451974Smsmith 26551974Smsmith /* iterate over available drives */ 26651974Smsmith for (i = 0, dr = &sc->amr_drive[0]; (i < AMR_MAXLD) && (dr->al_size != 0xffffffff); i++, dr++) { 26751974Smsmith /* are we already attached to this drive? */ 26851974Smsmith if (dr->al_disk == 0) { 26951974Smsmith /* generate geometry information */ 27051974Smsmith if (dr->al_size > 0x200000) { /* extended translation? */ 27151974Smsmith dr->al_heads = 255; 27251974Smsmith dr->al_sectors = 63; 27351974Smsmith } else { 27451974Smsmith dr->al_heads = 64; 27551974Smsmith dr->al_sectors = 32; 27651974Smsmith } 27751974Smsmith dr->al_cylinders = dr->al_size / (dr->al_heads * dr->al_sectors); 27851974Smsmith 27954073Smdodd dr->al_disk = device_add_child(sc->amr_dev, NULL, -1); 28051974Smsmith if (dr->al_disk == 0) 28151974Smsmith device_printf(sc->amr_dev, "device_add_child failed\n"); 28254073Smdodd device_set_ivars(dr->al_disk, dr); 28351974Smsmith } 28451974Smsmith } 28551974Smsmith 28651974Smsmith if ((error = bus_generic_attach(sc->amr_dev)) != 0) 28751974Smsmith device_printf(sc->amr_dev, "bus_generic_attach returned %d\n", error); 28851974Smsmith 28951974Smsmith /* mark controller back up */ 29051974Smsmith sc->amr_state &= ~AMR_STATE_SHUTDOWN; 29151974Smsmith 29251974Smsmith /* interrupts will be enabled before we do anything more */ 29351974Smsmith sc->amr_state |= AMR_STATE_INTEN; 29451974Smsmith 29551974Smsmith /* 29665245Smsmith * Start the timeout routine. 29751974Smsmith */ 29865245Smsmith/* sc->amr_timeout = timeout(amr_periodic, sc, hz);*/ 29951974Smsmith 30065245Smsmith return; 30151974Smsmith} 30251974Smsmith 30365245Smsmith/******************************************************************************* 30465245Smsmith * Free resources associated with a controller instance 30551974Smsmith */ 30665245Smsmithvoid 30765245Smsmithamr_free(struct amr_softc *sc) 30851974Smsmith{ 30965245Smsmith struct amr_command_cluster *acc; 31051974Smsmith 31165245Smsmith#ifdef AMR_SCSI_PASSTHROUGH 31265245Smsmith /* detach from CAM */ 31365245Smsmith amr_cam_detach(sc); 31465245Smsmith#endif 31551974Smsmith 31665245Smsmith /* cancel status timeout */ 31765245Smsmith untimeout(amr_periodic, sc, sc->amr_timeout); 31851974Smsmith 31965245Smsmith /* throw away any command buffers */ 32065245Smsmith while ((acc = TAILQ_FIRST(&sc->amr_cmd_clusters)) != NULL) { 32165245Smsmith TAILQ_REMOVE(&sc->amr_cmd_clusters, acc, acc_link); 32265245Smsmith amr_freecmd_cluster(acc); 32351974Smsmith } 32451974Smsmith} 32551974Smsmith 32651974Smsmith/******************************************************************************* 32765245Smsmith * Receive a bio structure from a child device and queue it on a particular 32851974Smsmith * disk resource, then poke the disk resource to start as much work as it can. 32951974Smsmith */ 33051974Smsmithint 33165245Smsmithamr_submit_bio(struct amr_softc *sc, struct bio *bio) 33251974Smsmith{ 33365245Smsmith debug_called(2); 33452543Smsmith 33565245Smsmith amr_enqueue_bio(sc, bio); 33651974Smsmith amr_startio(sc); 33751974Smsmith return(0); 33851974Smsmith} 33951974Smsmith 34051974Smsmith/******************************************************************************** 34151974Smsmith * Accept an open operation on the control device. 34251974Smsmith */ 34351974Smsmithint 34451974Smsmithamr_open(dev_t dev, int flags, int fmt, struct proc *p) 34551974Smsmith{ 34651974Smsmith int unit = minor(dev); 34751974Smsmith struct amr_softc *sc = devclass_get_softc(amr_devclass, unit); 34851974Smsmith 34965245Smsmith debug_called(1); 35065245Smsmith 35151974Smsmith sc->amr_state |= AMR_STATE_OPEN; 35251974Smsmith return(0); 35351974Smsmith} 35451974Smsmith 35551974Smsmith/******************************************************************************** 35651974Smsmith * Accept the last close on the control device. 35751974Smsmith */ 35851974Smsmithint 35951974Smsmithamr_close(dev_t dev, int flags, int fmt, struct proc *p) 36051974Smsmith{ 36151974Smsmith int unit = minor(dev); 36251974Smsmith struct amr_softc *sc = devclass_get_softc(amr_devclass, unit); 36351974Smsmith 36465245Smsmith debug_called(1); 36565245Smsmith 36651974Smsmith sc->amr_state &= ~AMR_STATE_OPEN; 36751974Smsmith return (0); 36851974Smsmith} 36951974Smsmith 37051974Smsmith/******************************************************************************** 37151974Smsmith * Handle controller-specific control operations. 37251974Smsmith */ 37351974Smsmithint 37451974Smsmithamr_ioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct proc *p) 37551974Smsmith{ 37665245Smsmith struct amr_softc *sc = (struct amr_softc *)dev->si_drv1; 37765245Smsmith int *arg = (int *)addr; 37865245Smsmith struct amr_user_ioctl *au = (struct amr_user_ioctl *)addr; 37965245Smsmith struct amr_command *ac; 38065245Smsmith struct amr_mailbox_ioctl *mbi; 38165245Smsmith struct amr_passthrough *ap; 38265245Smsmith void *dp; 38365245Smsmith int error; 38465245Smsmith 38565245Smsmith debug_called(1); 38665245Smsmith 38765245Smsmith error = 0; 38865245Smsmith dp = NULL; 38965245Smsmith ap = NULL; 39065245Smsmith ac = NULL; 39151974Smsmith switch(cmd) { 39265245Smsmith 39365245Smsmith case AMR_IO_VERSION: 39465245Smsmith debug(1, "AMR_IO_VERSION"); 39565245Smsmith *arg = AMR_IO_VERSION_NUMBER; 39665245Smsmith break; 39765245Smsmith 39865245Smsmith case AMR_IO_COMMAND: 39965245Smsmith debug(1, "AMR_IO_COMMAND"); 40065245Smsmith /* handle inbound data buffer */ 40165245Smsmith if (au->au_length != 0) { 40265245Smsmith if ((dp = malloc(au->au_length, M_DEVBUF, M_WAITOK)) == NULL) { 40365245Smsmith error = ENOMEM; 40465245Smsmith break; 40565245Smsmith } 40665245Smsmith if ((error = copyin(au->au_buffer, dp, au->au_length)) != 0) 40765245Smsmith break; 40865245Smsmith } 40965245Smsmith 41065245Smsmith if ((ac = amr_alloccmd(sc)) == NULL) { 41165245Smsmith error = ENOMEM; 41265245Smsmith break; 41365245Smsmith } 41465245Smsmith 41565245Smsmith /* handle SCSI passthrough command */ 41665245Smsmith if (au->au_cmd[0] == AMR_CMD_PASS) { 41768877Sdwmalone if ((ap = malloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO)) == NULL) { 41865245Smsmith error = ENOMEM; 41965245Smsmith break; 42065245Smsmith } 42165245Smsmith 42265245Smsmith /* copy cdb */ 42365245Smsmith ap->ap_cdb_length = au->au_cmd[2]; 42465245Smsmith bcopy(&au->au_cmd[3], &ap->ap_cdb[0], ap->ap_cdb_length); 42565245Smsmith 42665245Smsmith /* build passthrough */ 42765245Smsmith ap->ap_timeout = au->au_cmd[ap->ap_cdb_length + 3] & 0x07; 42865245Smsmith ap->ap_ars = (au->au_cmd[ap->ap_cdb_length + 3] & 0x08) ? 1 : 0; 42965245Smsmith ap->ap_islogical = (au->au_cmd[ap->ap_cdb_length + 3] & 0x80) ? 1 : 0; 43065245Smsmith ap->ap_logical_drive_no = au->au_cmd[ap->ap_cdb_length + 4]; 43165245Smsmith ap->ap_channel = au->au_cmd[ap->ap_cdb_length + 5]; 43265245Smsmith ap->ap_scsi_id = au->au_cmd[ap->ap_cdb_length + 6]; 43365245Smsmith ap->ap_request_sense_length = 14; 43465245Smsmith /* XXX what about the request-sense area? does the caller want it? */ 43565245Smsmith 43665245Smsmith /* build command */ 43765245Smsmith ac->ac_data = ap; 43865245Smsmith ac->ac_length = sizeof(*ap); 43965245Smsmith ac->ac_flags |= AMR_CMD_DATAOUT; 44065245Smsmith ac->ac_ccb_data = dp; 44165245Smsmith ac->ac_ccb_length = au->au_length; 44265245Smsmith if (au->au_direction & AMR_IO_READ) 44365245Smsmith ac->ac_flags |= AMR_CMD_CCB_DATAIN; 44465245Smsmith if (au->au_direction & AMR_IO_WRITE) 44565245Smsmith ac->ac_flags |= AMR_CMD_CCB_DATAOUT; 44665245Smsmith 44765245Smsmith ac->ac_mailbox.mb_command = AMR_CMD_PASS; 44865245Smsmith 44965245Smsmith } else { 45065245Smsmith /* direct command to controller */ 45165245Smsmith mbi = (struct amr_mailbox_ioctl *)&ac->ac_mailbox; 45265245Smsmith 45365245Smsmith /* copy pertinent mailbox items */ 45465245Smsmith mbi->mb_command = au->au_cmd[0]; 45565245Smsmith mbi->mb_channel = au->au_cmd[1]; 45665245Smsmith mbi->mb_param = au->au_cmd[2]; 45765245Smsmith mbi->mb_pad[0] = au->au_cmd[3]; 45865245Smsmith mbi->mb_drive = au->au_cmd[4]; 45965245Smsmith 46065245Smsmith /* build the command */ 46165245Smsmith ac->ac_data = dp; 46265245Smsmith ac->ac_length = au->au_length; 46365245Smsmith if (au->au_direction & AMR_IO_READ) 46465245Smsmith ac->ac_flags |= AMR_CMD_DATAIN; 46565245Smsmith if (au->au_direction & AMR_IO_WRITE) 46665245Smsmith ac->ac_flags |= AMR_CMD_DATAOUT; 46765245Smsmith } 46865245Smsmith 46965245Smsmith /* run the command */ 47065245Smsmith if ((error = amr_wait_command(ac)) != 0) 47165245Smsmith break; 47265245Smsmith 47365245Smsmith /* copy out data and set status */ 47465245Smsmith if (au->au_length != 0) 47565245Smsmith error = copyout(dp, au->au_buffer, au->au_length); 47665245Smsmith au->au_status = ac->ac_status; 47765245Smsmith break; 47865245Smsmith 47965245Smsmith default: 48065245Smsmith debug(1, "unknown ioctl 0x%lx", cmd); 48165245Smsmith error = ENOIOCTL; 48265245Smsmith break; 48351974Smsmith } 48451974Smsmith 48565245Smsmith if (dp != NULL) 48665245Smsmith free(dp, M_DEVBUF); 48765245Smsmith if (ap != NULL) 48865245Smsmith free(ap, M_DEVBUF); 48965245Smsmith if (ac != NULL) 49065245Smsmith amr_releasecmd(ac); 49165245Smsmith return(error); 49251974Smsmith} 49351974Smsmith 49451974Smsmith/******************************************************************************** 49551974Smsmith ******************************************************************************** 49658883Smsmith Status Monitoring 49758883Smsmith ******************************************************************************** 49858883Smsmith ********************************************************************************/ 49958883Smsmith 50058883Smsmith/******************************************************************************** 50158883Smsmith * Perform a periodic check of the controller status 50258883Smsmith */ 50358883Smsmithstatic void 50458883Smsmithamr_periodic(void *data) 50558883Smsmith{ 50658883Smsmith struct amr_softc *sc = (struct amr_softc *)data; 50758883Smsmith 50865245Smsmith debug_called(2); 50958883Smsmith 51065245Smsmith /* XXX perform periodic status checks here */ 51158883Smsmith 51265245Smsmith /* compensate for missed interrupts */ 51365245Smsmith amr_done(sc); 51465245Smsmith 51558883Smsmith /* reschedule */ 51658883Smsmith sc->amr_timeout = timeout(amr_periodic, sc, hz); 51758883Smsmith} 51858883Smsmith 51958883Smsmith/******************************************************************************** 52058883Smsmith ******************************************************************************** 52151974Smsmith Command Wrappers 52251974Smsmith ******************************************************************************** 52351974Smsmith ********************************************************************************/ 52451974Smsmith 52551974Smsmith/******************************************************************************** 52651974Smsmith * Interrogate the controller for the operational parameters we require. 52751974Smsmith */ 52851974Smsmithstatic int 52951974Smsmithamr_query_controller(struct amr_softc *sc) 53051974Smsmith{ 53165245Smsmith struct amr_enquiry3 *aex; 53265245Smsmith struct amr_prodinfo *ap; 53365245Smsmith struct amr_enquiry *ae; 53465245Smsmith int ldrv; 53551974Smsmith 53665245Smsmith /* 53765245Smsmith * If we haven't found the real limit yet, let us have a couple of commands in 53865245Smsmith * order to be able to probe. 53965245Smsmith */ 54065245Smsmith if (sc->amr_maxio == 0) 54165245Smsmith sc->amr_maxio = 2; 54251974Smsmith 54365245Smsmith /* 54465245Smsmith * Try to issue an ENQUIRY3 command 54565245Smsmith */ 54665245Smsmith if ((aex = amr_enquiry(sc, 2048, AMR_CMD_CONFIG, AMR_CONFIG_ENQ3, 54765245Smsmith AMR_CONFIG_ENQ3_SOLICITED_FULL)) != NULL) { 54851974Smsmith 54965245Smsmith /* 55065245Smsmith * Fetch current state of logical drives. 55165245Smsmith */ 55265245Smsmith for (ldrv = 0; ldrv < aex->ae_numldrives; ldrv++) { 55365245Smsmith sc->amr_drive[ldrv].al_size = aex->ae_drivesize[ldrv]; 55465245Smsmith sc->amr_drive[ldrv].al_state = aex->ae_drivestate[ldrv]; 55565245Smsmith sc->amr_drive[ldrv].al_properties = aex->ae_driveprop[ldrv]; 55665245Smsmith debug(2, " drive %d: %d state %x properties %x\n", ldrv, sc->amr_drive[ldrv].al_size, 55765245Smsmith sc->amr_drive[ldrv].al_state, sc->amr_drive[ldrv].al_properties); 55851974Smsmith } 55965245Smsmith free(aex, M_DEVBUF); 56058883Smsmith 56165245Smsmith /* 56265245Smsmith * Get product info for channel count. 56358883Smsmith */ 56465245Smsmith if ((ap = amr_enquiry(sc, 2048, AMR_CMD_CONFIG, AMR_CONFIG_PRODUCT_INFO, 0)) == NULL) { 56565245Smsmith device_printf(sc->amr_dev, "can't obtain product data from controller\n"); 56665245Smsmith return(1); 56765245Smsmith } 56865245Smsmith sc->amr_maxdrives = 40; 56965245Smsmith sc->amr_maxchan = ap->ap_nschan; 57065245Smsmith sc->amr_maxio = ap->ap_maxio; 57165245Smsmith sc->amr_type |= AMR_TYPE_40LD; 57265245Smsmith free(ap, M_DEVBUF); 57358883Smsmith 57465245Smsmith } else { 57565245Smsmith 57665245Smsmith /* failed, try the 8LD ENQUIRY commands */ 57765245Smsmith if ((ae = (struct amr_enquiry *)amr_enquiry(sc, 2048, AMR_CMD_EXT_ENQUIRY2, 0, 0)) == NULL) { 57865245Smsmith if ((ae = (struct amr_enquiry *)amr_enquiry(sc, 2048, AMR_CMD_ENQUIRY, 0, 0)) == NULL) { 57965245Smsmith device_printf(sc->amr_dev, "can't obtain configuration data from controller\n"); 58065245Smsmith return(1); 58165245Smsmith } 58265245Smsmith ae->ae_signature = 0; 58351974Smsmith } 58465245Smsmith 58558883Smsmith /* 58665245Smsmith * Fetch current state of logical drives. 58758883Smsmith */ 58865245Smsmith for (ldrv = 0; ldrv < ae->ae_ldrv.al_numdrives; ldrv++) { 58965245Smsmith sc->amr_drive[ldrv].al_size = ae->ae_ldrv.al_size[ldrv]; 59065245Smsmith sc->amr_drive[ldrv].al_state = ae->ae_ldrv.al_state[ldrv]; 59165245Smsmith sc->amr_drive[ldrv].al_properties = ae->ae_ldrv.al_properties[ldrv]; 59265245Smsmith debug(2, " drive %d: %d state %x properties %x\n", ldrv, sc->amr_drive[ldrv].al_size, 59365245Smsmith sc->amr_drive[ldrv].al_state, sc->amr_drive[ldrv].al_properties); 59451974Smsmith } 59565245Smsmith 59665245Smsmith sc->amr_maxdrives = 8; 59765245Smsmith sc->amr_maxchan = ae->ae_adapter.aa_channels; 59865245Smsmith sc->amr_maxio = ae->ae_adapter.aa_maxio; 59965245Smsmith free(ae, M_DEVBUF); 60051974Smsmith } 60165245Smsmith 60265245Smsmith /* 60365245Smsmith * Mark remaining drives as unused. 60465245Smsmith */ 60565245Smsmith for (; ldrv < AMR_MAXLD; ldrv++) 60665245Smsmith sc->amr_drive[ldrv].al_size = 0xffffffff; 60765245Smsmith 60865245Smsmith /* 60965245Smsmith * Cap the maximum number of outstanding I/Os. AMI's Linux driver doesn't trust 61065245Smsmith * the controller's reported value, and lockups have been seen when we do. 61165245Smsmith */ 61265245Smsmith sc->amr_maxio = imin(sc->amr_maxio, AMR_LIMITCMD); 61365245Smsmith 61451974Smsmith return(0); 61551974Smsmith} 61651974Smsmith 61751974Smsmith/******************************************************************************** 61851974Smsmith * Run a generic enquiry-style command. 61951974Smsmith */ 62051974Smsmithstatic void * 62151974Smsmithamr_enquiry(struct amr_softc *sc, size_t bufsize, u_int8_t cmd, u_int8_t cmdsub, u_int8_t cmdqual) 62251974Smsmith{ 62351974Smsmith struct amr_command *ac; 62451974Smsmith void *result; 62551974Smsmith u_int8_t *mbox; 62651974Smsmith int error; 62751974Smsmith 62865245Smsmith debug_called(1); 62951974Smsmith 63051974Smsmith error = 1; 63151974Smsmith result = NULL; 63251974Smsmith 63351974Smsmith /* get ourselves a command buffer */ 63451974Smsmith if ((ac = amr_alloccmd(sc)) == NULL) 63551974Smsmith goto out; 63651974Smsmith /* allocate the response structure */ 63751974Smsmith if ((result = malloc(bufsize, M_DEVBUF, M_NOWAIT)) == NULL) 63851974Smsmith goto out; 63965245Smsmith /* set command flags */ 64051974Smsmith ac->ac_flags |= AMR_CMD_PRIORITY | AMR_CMD_DATAOUT; 64151974Smsmith 64265245Smsmith /* point the command at our data */ 64351974Smsmith ac->ac_data = result; 64451974Smsmith ac->ac_length = bufsize; 64551974Smsmith 64651974Smsmith /* build the command proper */ 64751974Smsmith mbox = (u_int8_t *)&ac->ac_mailbox; /* XXX want a real structure for this? */ 64851974Smsmith mbox[0] = cmd; 64951974Smsmith mbox[2] = cmdsub; 65051974Smsmith mbox[3] = cmdqual; 65151974Smsmith 65258883Smsmith /* can't assume that interrupts are going to work here, so play it safe */ 65358883Smsmith if (amr_poll_command(ac)) 65451974Smsmith goto out; 65551974Smsmith error = ac->ac_status; 65651974Smsmith 65751974Smsmith out: 65851974Smsmith if (ac != NULL) 65951974Smsmith amr_releasecmd(ac); 66051974Smsmith if ((error != 0) && (result != NULL)) { 66151974Smsmith free(result, M_DEVBUF); 66251974Smsmith result = NULL; 66351974Smsmith } 66451974Smsmith return(result); 66551974Smsmith} 66651974Smsmith 66751974Smsmith/******************************************************************************** 66851974Smsmith * Flush the controller's internal cache, return status. 66951974Smsmith */ 67065245Smsmithint 67151974Smsmithamr_flush(struct amr_softc *sc) 67251974Smsmith{ 67351974Smsmith struct amr_command *ac; 67451974Smsmith int error; 67551974Smsmith 67651974Smsmith /* get ourselves a command buffer */ 67751974Smsmith error = 1; 67851974Smsmith if ((ac = amr_alloccmd(sc)) == NULL) 67951974Smsmith goto out; 68065245Smsmith /* set command flags */ 68151974Smsmith ac->ac_flags |= AMR_CMD_PRIORITY | AMR_CMD_DATAOUT; 68251974Smsmith 68351974Smsmith /* build the command proper */ 68451974Smsmith ac->ac_mailbox.mb_command = AMR_CMD_FLUSH; 68551974Smsmith 68658883Smsmith /* we have to poll, as the system may be going down or otherwise damaged */ 68758883Smsmith if (amr_poll_command(ac)) 68851974Smsmith goto out; 68951974Smsmith error = ac->ac_status; 69051974Smsmith 69151974Smsmith out: 69251974Smsmith if (ac != NULL) 69351974Smsmith amr_releasecmd(ac); 69451974Smsmith return(error); 69551974Smsmith} 69651974Smsmith 69751974Smsmith/******************************************************************************** 69865245Smsmith * Try to find I/O work for the controller from one or more of the work queues. 69951974Smsmith * 70065245Smsmith * We make the assumption that if the controller is not ready to take a command 70165245Smsmith * at some given time, it will generate an interrupt at some later time when 70265245Smsmith * it is. 70351974Smsmith */ 70465245Smsmithvoid 70551974Smsmithamr_startio(struct amr_softc *sc) 70651974Smsmith{ 70751974Smsmith struct amr_command *ac; 70851974Smsmith 70951974Smsmith /* spin until something prevents us from doing any work */ 71051974Smsmith for (;;) { 71151974Smsmith 71265245Smsmith /* try to get a ready command */ 71365245Smsmith ac = amr_dequeue_ready(sc); 71451974Smsmith 71565245Smsmith /* if that failed, build a command from a bio */ 71665245Smsmith if (ac == NULL) 71765245Smsmith (void)amr_bio_command(sc, &ac); 71851974Smsmith 71965245Smsmith#ifdef AMR_SCSI_PASSTHROUGH 72065245Smsmith /* if that failed, build a command from a ccb */ 72165245Smsmith if (ac == NULL) 72265245Smsmith (void)amr_cam_command(sc, &ac); 72365245Smsmith#endif 72465245Smsmith 72565245Smsmith /* if we don't have anything to do, give up */ 72665245Smsmith if (ac == NULL) 72765245Smsmith break; 72851974Smsmith 72965245Smsmith /* try to give the command to the controller; if this fails save it for later and give up */ 73065245Smsmith if (amr_start(ac)) { 73165245Smsmith debug(2, "controller busy, command deferred"); 73265245Smsmith amr_requeue_ready(ac); /* XXX schedule retry very soon? */ 73365245Smsmith break; 73451974Smsmith } 73551974Smsmith } 73651974Smsmith} 73751974Smsmith 73851974Smsmith/******************************************************************************** 73951974Smsmith * Handle completion of an I/O command. 74051974Smsmith */ 74151974Smsmithstatic void 74251974Smsmithamr_completeio(struct amr_command *ac) 74351974Smsmith{ 74451974Smsmith struct amr_softc *sc = ac->ac_sc; 74551974Smsmith 74651974Smsmith if (ac->ac_status != AMR_STATUS_SUCCESS) { /* could be more verbose here? */ 74765245Smsmith ac->ac_bio->bio_error = EIO; 74865245Smsmith ac->ac_bio->bio_flags |= BIO_ERROR; 74951974Smsmith 75065245Smsmith device_printf(sc->amr_dev, "I/O error - 0x%x\n", ac->ac_status); 75165245Smsmith/* amr_printcommand(ac);*/ 75251974Smsmith } 75365245Smsmith amrd_intr(ac->ac_bio); 75465245Smsmith amr_releasecmd(ac); 75551974Smsmith} 75651974Smsmith 75751974Smsmith/******************************************************************************** 75851974Smsmith ******************************************************************************** 75951974Smsmith Command Processing 76051974Smsmith ******************************************************************************** 76151974Smsmith ********************************************************************************/ 76251974Smsmith 76351974Smsmith/******************************************************************************** 76465245Smsmith * Convert a bio off the top of the bio queue into a command. 76565245Smsmith */ 76665245Smsmithstatic int 76765245Smsmithamr_bio_command(struct amr_softc *sc, struct amr_command **acp) 76865245Smsmith{ 76965245Smsmith struct amr_command *ac; 77065245Smsmith struct amrd_softc *amrd; 77165245Smsmith struct bio *bio; 77265245Smsmith int error; 77365245Smsmith int blkcount; 77465245Smsmith int driveno; 77565245Smsmith int cmd; 77665245Smsmith 77765245Smsmith ac = NULL; 77865245Smsmith error = 0; 77965245Smsmith 78065245Smsmith /* get a bio to work on */ 78165245Smsmith if ((bio = amr_dequeue_bio(sc)) == NULL) 78265245Smsmith goto out; 78365245Smsmith 78465245Smsmith /* get a command */ 78565245Smsmith if ((ac = amr_alloccmd(sc)) == NULL) { 78665245Smsmith error = ENOMEM; 78765245Smsmith goto out; 78865245Smsmith } 78965245Smsmith 79065245Smsmith /* connect the bio to the command */ 79165245Smsmith ac->ac_complete = amr_completeio; 79265245Smsmith ac->ac_bio = bio; 79365245Smsmith ac->ac_data = bio->bio_data; 79465245Smsmith ac->ac_length = bio->bio_bcount; 79565245Smsmith if (BIO_IS_READ(bio)) { 79665245Smsmith ac->ac_flags |= AMR_CMD_DATAIN; 79765245Smsmith cmd = AMR_CMD_LREAD; 79865245Smsmith } else { 79965245Smsmith ac->ac_flags |= AMR_CMD_DATAOUT; 80065245Smsmith cmd = AMR_CMD_LWRITE; 80165245Smsmith } 80265245Smsmith amrd = (struct amrd_softc *)bio->bio_dev->si_drv1; 80365245Smsmith driveno = amrd->amrd_drive - sc->amr_drive; 80465245Smsmith blkcount = (bio->bio_bcount + AMR_BLKSIZE - 1) / AMR_BLKSIZE; 80565245Smsmith 80665245Smsmith ac->ac_mailbox.mb_command = cmd; 80765245Smsmith ac->ac_mailbox.mb_blkcount = blkcount; 80865245Smsmith ac->ac_mailbox.mb_lba = bio->bio_pblkno; 80965245Smsmith ac->ac_mailbox.mb_drive = driveno; 81065245Smsmith /* we fill in the s/g related data when the command is mapped */ 81165245Smsmith 81265245Smsmith if ((bio->bio_pblkno + blkcount) > sc->amr_drive[driveno].al_size) 81365245Smsmith device_printf(sc->amr_dev, "I/O beyond end of unit (%u,%d > %u)\n", 81465245Smsmith bio->bio_pblkno, blkcount, sc->amr_drive[driveno].al_size); 81565245Smsmith 81665245Smsmithout: 81765245Smsmith if (error != 0) { 81865245Smsmith if (ac != NULL) 81965245Smsmith amr_releasecmd(ac); 82065245Smsmith if (bio != NULL) /* this breaks ordering... */ 82165245Smsmith amr_enqueue_bio(sc, bio); 82265245Smsmith } 82365245Smsmith *acp = ac; 82465245Smsmith return(error); 82565245Smsmith} 82665245Smsmith 82765245Smsmith/******************************************************************************** 82851974Smsmith * Take a command, submit it to the controller and sleep until it completes 82951974Smsmith * or fails. Interrupts must be enabled, returns nonzero on error. 83051974Smsmith */ 83151974Smsmithstatic int 83251974Smsmithamr_wait_command(struct amr_command *ac) 83351974Smsmith{ 83451974Smsmith struct amr_softc *sc = ac->ac_sc; 83551974Smsmith int error, count; 83651974Smsmith 83765245Smsmith debug_called(1); 83851974Smsmith 83951974Smsmith ac->ac_complete = NULL; 84065245Smsmith ac->ac_flags |= AMR_CMD_SLEEP; 84151974Smsmith if ((error = amr_start(ac)) != 0) 84251974Smsmith return(error); 84351974Smsmith 84451974Smsmith count = 0; 84551974Smsmith /* XXX better timeout? */ 84665245Smsmith while ((ac->ac_flags & AMR_CMD_BUSY) && (count < 30)) { 84765245Smsmith tsleep(ac, PRIBIO | PCATCH, "amrwcmd", hz); 84851974Smsmith } 84951974Smsmith 85051974Smsmith if (ac->ac_status != 0) { 85165245Smsmith device_printf(sc->amr_dev, "I/O error - 0x%x\n", ac->ac_status); 85251974Smsmith return(EIO); 85351974Smsmith } 85451974Smsmith return(0); 85551974Smsmith} 85651974Smsmith 85751974Smsmith/******************************************************************************** 85851974Smsmith * Take a command, submit it to the controller and busy-wait for it to return. 85951974Smsmith * Returns nonzero on error. Can be safely called with interrupts enabled. 86051974Smsmith */ 86151974Smsmithstatic int 86251974Smsmithamr_poll_command(struct amr_command *ac) 86351974Smsmith{ 86451974Smsmith struct amr_softc *sc = ac->ac_sc; 86565245Smsmith int error, count; 86651974Smsmith 86765245Smsmith debug_called(2); 86851974Smsmith 86951974Smsmith ac->ac_complete = NULL; 87051974Smsmith if ((error = amr_start(ac)) != 0) 87151974Smsmith return(error); 87251974Smsmith 87351974Smsmith count = 0; 87451974Smsmith do { 87551974Smsmith /* 87651974Smsmith * Poll for completion, although the interrupt handler may beat us to it. 87751974Smsmith * Note that the timeout here is somewhat arbitrary. 87851974Smsmith */ 87951974Smsmith amr_done(sc); 88065245Smsmith DELAY(1000); 88165245Smsmith } while ((ac->ac_flags & AMR_CMD_BUSY) && (count++ < 1000)); 88265245Smsmith if (!(ac->ac_flags & AMR_CMD_BUSY)) { 88351974Smsmith error = 0; 88451974Smsmith } else { 88565245Smsmith /* XXX the slot is now marked permanently busy */ 88651974Smsmith error = EIO; 88765245Smsmith device_printf(sc->amr_dev, "polled command timeout\n"); 88851974Smsmith } 88951974Smsmith return(error); 89051974Smsmith} 89151974Smsmith 89251974Smsmith/******************************************************************************** 89365245Smsmith * Get a free command slot for a command if it doesn't already have one. 89465245Smsmith * 89565245Smsmith * May be safely called multiple times for a given command. 89651974Smsmith */ 89751974Smsmithstatic int 89851974Smsmithamr_getslot(struct amr_command *ac) 89951974Smsmith{ 90051974Smsmith struct amr_softc *sc = ac->ac_sc; 90165245Smsmith int s, slot, limit, error; 90251974Smsmith 90365245Smsmith debug_called(3); 90465245Smsmith 90565245Smsmith /* if the command already has a slot, don't try to give it another one */ 90665245Smsmith if (ac->ac_slot != 0) 90765245Smsmith return(0); 90865245Smsmith 90951974Smsmith /* enforce slot usage limit */ 91051974Smsmith limit = (ac->ac_flags & AMR_CMD_PRIORITY) ? sc->amr_maxio : sc->amr_maxio - 4; 91165245Smsmith if (sc->amr_busyslots > limit) 91251974Smsmith return(EBUSY); 91351974Smsmith 91451974Smsmith /* 91565245Smsmith * Allocate a slot. XXX linear scan is slow 91651974Smsmith */ 91765245Smsmith error = EBUSY; 91851974Smsmith s = splbio(); 91951974Smsmith for (slot = 0; slot < sc->amr_maxio; slot++) { 92065245Smsmith if (sc->amr_busycmd[slot] == NULL) { 92165245Smsmith sc->amr_busycmd[slot] = ac; 92265245Smsmith sc->amr_busyslots++; 92365245Smsmith ac->ac_slot = slot; 92465245Smsmith error = 0; 92551974Smsmith break; 92665245Smsmith } 92751974Smsmith } 92851974Smsmith splx(s); 92951974Smsmith 93065245Smsmith return(error); 93151974Smsmith} 93251974Smsmith 93351974Smsmith/******************************************************************************** 93465245Smsmith * Map/unmap (ac)'s data in the controller's addressable space as required. 93565245Smsmith * 93665245Smsmith * These functions may be safely called multiple times on a given command. 93751974Smsmith */ 93851974Smsmithstatic void 93951974Smsmithamr_setup_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error) 94051974Smsmith{ 94151974Smsmith struct amr_command *ac = (struct amr_command *)arg; 94251974Smsmith struct amr_softc *sc = ac->ac_sc; 94351974Smsmith struct amr_sgentry *sg; 94451974Smsmith int i; 94551974Smsmith 94665245Smsmith debug_called(3); 94751974Smsmith 94851974Smsmith /* get base address of s/g table */ 94951974Smsmith sg = sc->amr_sgtable + (ac->ac_slot * AMR_NSEG); 95051974Smsmith 95165245Smsmith /* save data physical address */ 95251974Smsmith ac->ac_dataphys = segs[0].ds_addr; 95351974Smsmith 95465245Smsmith /* decide whether we need to populate the s/g table */ 95565245Smsmith if (nsegments < 2) { 95665245Smsmith ac->ac_mailbox.mb_nsgelem = 0; 95765245Smsmith ac->ac_mailbox.mb_physaddr = ac->ac_dataphys; 95865245Smsmith } else { 95965245Smsmith ac->ac_mailbox.mb_nsgelem = nsegments; 96065245Smsmith ac->ac_mailbox.mb_physaddr = sc->amr_sgbusaddr + (ac->ac_slot * AMR_NSEG * sizeof(struct amr_sgentry)); 96165245Smsmith for (i = 0; i < nsegments; i++, sg++) { 96265245Smsmith sg->sg_addr = segs[i].ds_addr; 96365245Smsmith sg->sg_count = segs[i].ds_len; 96465245Smsmith } 96565245Smsmith } 96665245Smsmith} 96765245Smsmith 96865245Smsmithstatic void 96965245Smsmithamr_setup_ccbmap(void *arg, bus_dma_segment_t *segs, int nsegments, int error) 97065245Smsmith{ 97165245Smsmith struct amr_command *ac = (struct amr_command *)arg; 97265245Smsmith struct amr_softc *sc = ac->ac_sc; 97365245Smsmith struct amr_sgentry *sg; 97465245Smsmith struct amr_passthrough *ap = (struct amr_passthrough *)ac->ac_data; 97565245Smsmith int i; 97665245Smsmith 97765245Smsmith /* get base address of s/g table */ 97865245Smsmith sg = sc->amr_sgtable + (ac->ac_slot * AMR_NSEG); 97965245Smsmith 98065245Smsmith /* save s/g table information in passthrough */ 98165245Smsmith ap->ap_no_sg_elements = nsegments; 98265245Smsmith ap->ap_data_transfer_address = sc->amr_sgbusaddr + (ac->ac_slot * AMR_NSEG * sizeof(struct amr_sgentry)); 98365245Smsmith 98465245Smsmith /* save pointer to passthrough in command XXX is this already done above? */ 98565245Smsmith ac->ac_mailbox.mb_physaddr = ac->ac_dataphys; 98665245Smsmith 98765245Smsmith debug(2, "slot %d %d segments at 0x%x, passthrough at 0x%x", ac->ac_slot, 98865245Smsmith ap->ap_no_sg_elements, ap->ap_data_transfer_address, ac->ac_dataphys); 98965245Smsmith 99065245Smsmith /* populate s/g table (overwrites previous call which mapped the passthrough) */ 99151974Smsmith for (i = 0; i < nsegments; i++, sg++) { 99251974Smsmith sg->sg_addr = segs[i].ds_addr; 99351974Smsmith sg->sg_count = segs[i].ds_len; 99465245Smsmith debug(2, " %d: 0x%x/%d", i, sg->sg_addr, sg->sg_count); 99551974Smsmith } 99651974Smsmith} 99751974Smsmith 99851974Smsmithstatic void 99951974Smsmithamr_mapcmd(struct amr_command *ac) 100051974Smsmith{ 100151974Smsmith struct amr_softc *sc = ac->ac_sc; 100251974Smsmith 100365245Smsmith debug_called(2); 100451974Smsmith 100565245Smsmith /* if the command involves data at all, and hasn't been mapped */ 100665245Smsmith if (!(ac->ac_flags & AMR_CMD_MAPPED)) { 100765245Smsmith 100865245Smsmith if (ac->ac_data != NULL) { 100965245Smsmith /* map the data buffers into bus space and build the s/g list */ 101065245Smsmith bus_dmamap_load(sc->amr_buffer_dmat, ac->ac_dmamap, ac->ac_data, ac->ac_length, 101165245Smsmith amr_setup_dmamap, ac, 0); 101265245Smsmith if (ac->ac_flags & AMR_CMD_DATAIN) 101365245Smsmith bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_dmamap, BUS_DMASYNC_PREREAD); 101465245Smsmith if (ac->ac_flags & AMR_CMD_DATAOUT) 101565245Smsmith bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_dmamap, BUS_DMASYNC_PREWRITE); 101665245Smsmith } 101765245Smsmith 101865245Smsmith if (ac->ac_ccb_data != NULL) { 101965245Smsmith bus_dmamap_load(sc->amr_buffer_dmat, ac->ac_ccb_dmamap, ac->ac_ccb_data, ac->ac_ccb_length, 102065245Smsmith amr_setup_ccbmap, ac, 0); 102165245Smsmith if (ac->ac_flags & AMR_CMD_CCB_DATAIN) 102265245Smsmith bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_ccb_dmamap, BUS_DMASYNC_PREREAD); 102365245Smsmith if (ac->ac_flags & AMR_CMD_CCB_DATAOUT) 102465245Smsmith bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_ccb_dmamap, BUS_DMASYNC_PREWRITE); 102565245Smsmith } 102665245Smsmith ac->ac_flags |= AMR_CMD_MAPPED; 102751974Smsmith } 102851974Smsmith} 102951974Smsmith 103051974Smsmithstatic void 103151974Smsmithamr_unmapcmd(struct amr_command *ac) 103251974Smsmith{ 103351974Smsmith struct amr_softc *sc = ac->ac_sc; 103451974Smsmith 103565245Smsmith debug_called(2); 103651974Smsmith 103765245Smsmith /* if the command involved data at all and was mapped */ 103865245Smsmith if (ac->ac_flags & AMR_CMD_MAPPED) { 103951974Smsmith 104065245Smsmith if (ac->ac_data != NULL) { 104165245Smsmith if (ac->ac_flags & AMR_CMD_DATAIN) 104265245Smsmith bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_dmamap, BUS_DMASYNC_POSTREAD); 104365245Smsmith if (ac->ac_flags & AMR_CMD_DATAOUT) 104465245Smsmith bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_dmamap, BUS_DMASYNC_POSTWRITE); 104565245Smsmith bus_dmamap_unload(sc->amr_buffer_dmat, ac->ac_dmamap); 104665245Smsmith } 104765245Smsmith 104865245Smsmith if (ac->ac_ccb_data != NULL) { 104965245Smsmith if (ac->ac_flags & AMR_CMD_CCB_DATAIN) 105065245Smsmith bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_ccb_dmamap, BUS_DMASYNC_POSTREAD); 105165245Smsmith if (ac->ac_flags & AMR_CMD_CCB_DATAOUT) 105265245Smsmith bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_ccb_dmamap, BUS_DMASYNC_POSTWRITE); 105365245Smsmith bus_dmamap_unload(sc->amr_buffer_dmat, ac->ac_ccb_dmamap); 105465245Smsmith } 105565245Smsmith ac->ac_flags &= ~AMR_CMD_MAPPED; 105651974Smsmith } 105751974Smsmith} 105851974Smsmith 105951974Smsmith/******************************************************************************** 106065245Smsmith * Take a command and give it to the controller, returns 0 if successful, or 106165245Smsmith * EBUSY if the command should be retried later. 106251974Smsmith */ 106351974Smsmithstatic int 106451974Smsmithamr_start(struct amr_command *ac) 106551974Smsmith{ 106651974Smsmith struct amr_softc *sc = ac->ac_sc; 106758883Smsmith int done, s, i; 106851974Smsmith 106965245Smsmith debug_called(2); 107051974Smsmith 107165245Smsmith /* mark command as busy so that polling consumer can tell */ 107265245Smsmith ac->ac_flags |= AMR_CMD_BUSY; 107365245Smsmith 107465245Smsmith /* get a command slot (freed in amr_done) */ 107565245Smsmith if (amr_getslot(ac)) 107665245Smsmith return(EBUSY); 107765245Smsmith 107865245Smsmith /* now we have a slot, we can map the command (unmapped in amr_complete) */ 107965245Smsmith amr_mapcmd(ac); 108065245Smsmith 108165245Smsmith /* mark the new mailbox we are going to copy in as busy */ 108265245Smsmith ac->ac_mailbox.mb_busy = 1; 108365245Smsmith 108465245Smsmith /* clear the poll/ack fields in the mailbox */ 108565245Smsmith sc->amr_mailbox->mb_poll = 0; 108665245Smsmith sc->amr_mailbox->mb_ack = 0; 108765245Smsmith 108851974Smsmith /* 108951974Smsmith * Save the slot number so that we can locate this command when complete. 109051974Smsmith * Note that ident = 0 seems to be special, so we don't use it. 109151974Smsmith */ 109251974Smsmith ac->ac_mailbox.mb_ident = ac->ac_slot + 1; 109351974Smsmith 109458883Smsmith /* 109565245Smsmith * Spin waiting for the mailbox, give up after ~1 second. We expect the 109665245Smsmith * controller to be able to handle our I/O. 109765245Smsmith * 109865245Smsmith * XXX perhaps we should wait for less time, and count on the deferred command 109965245Smsmith * handling to deal with retries? 110058883Smsmith */ 110165245Smsmith debug(2, "wait for mailbox"); 110258883Smsmith for (i = 10000, done = 0; (i > 0) && !done; i--) { 110351974Smsmith s = splbio(); 110451974Smsmith 110551974Smsmith /* is the mailbox free? */ 110651974Smsmith if (sc->amr_mailbox->mb_busy == 0) { 110765245Smsmith debug(2, "got mailbox"); 110851974Smsmith sc->amr_mailbox64->mb64_segment = 0; 110965245Smsmith bcopy(&ac->ac_mailbox, (void *)(uintptr_t)(volatile void *)sc->amr_mailbox, AMR_MBOX_CMDSIZE); 111051974Smsmith done = 1; 111151974Smsmith 111265245Smsmith /* not free, spin waiting */ 111351974Smsmith } else { 111465245Smsmith debug(3, "busy flag %x\n", sc->amr_mailbox->mb_busy); 111558883Smsmith /* this is somewhat ugly */ 111658883Smsmith DELAY(100); 111751974Smsmith } 111858883Smsmith splx(s); /* drop spl to allow completion interrupts */ 111951974Smsmith } 112065245Smsmith 112165245Smsmith /* 112265245Smsmith * Now give the command to the controller 112365245Smsmith */ 112451974Smsmith if (done) { 112565245Smsmith if (sc->amr_submit_command(sc)) { 112665245Smsmith /* the controller wasn't ready to take the command, forget that we tried to post it */ 112765245Smsmith sc->amr_mailbox->mb_busy = 0; 112865245Smsmith return(EBUSY); 112965245Smsmith } 113065245Smsmith debug(2, "posted command"); 113151974Smsmith return(0); 113251974Smsmith } 113351974Smsmith 113451974Smsmith /* 113565245Smsmith * The controller wouldn't take the command. Return the command as busy 113665245Smsmith * so that it is retried later. 113751974Smsmith */ 113865245Smsmith return(EBUSY); 113951974Smsmith} 114051974Smsmith 114151974Smsmith/******************************************************************************** 114251974Smsmith * Extract one or more completed commands from the controller (sc) 114351974Smsmith * 114452543Smsmith * Returns nonzero if any commands on the work queue were marked as completed. 114551974Smsmith */ 114665245Smsmithint 114751974Smsmithamr_done(struct amr_softc *sc) 114851974Smsmith{ 114951974Smsmith struct amr_command *ac; 115051974Smsmith struct amr_mailbox mbox; 115165245Smsmith int i, idx, result; 115251974Smsmith 115365245Smsmith debug_called(2); 115451974Smsmith 115551974Smsmith /* See if there's anything for us to do */ 115651974Smsmith result = 0; 115751974Smsmith 115858883Smsmith /* loop collecting completed commands */ 115958883Smsmith for (;;) { 116058883Smsmith /* poll for a completed command's identifier and status */ 116158883Smsmith if (sc->amr_get_work(sc, &mbox)) { 116258883Smsmith result = 1; 116358883Smsmith 116458883Smsmith /* iterate over completed commands in this result */ 116558883Smsmith for (i = 0; i < mbox.mb_nstatus; i++) { 116658883Smsmith /* get pointer to busy command */ 116758883Smsmith idx = mbox.mb_completed[i] - 1; 116858883Smsmith ac = sc->amr_busycmd[idx]; 116951974Smsmith 117058883Smsmith /* really a busy command? */ 117158883Smsmith if (ac != NULL) { 117258883Smsmith 117358883Smsmith /* pull the command from the busy index */ 117458883Smsmith sc->amr_busycmd[idx] = NULL; 117565245Smsmith sc->amr_busyslots--; 117651974Smsmith 117765245Smsmith /* save status for later use */ 117865245Smsmith ac->ac_status = mbox.mb_status; 117965245Smsmith amr_enqueue_completed(ac); 118065245Smsmith debug(3, "completed command with status %x", mbox.mb_status); 118165245Smsmith } else { 118265245Smsmith device_printf(sc->amr_dev, "bad slot %d completed\n", idx); 118351974Smsmith } 118451974Smsmith } 118558883Smsmith } else { 118665245Smsmith break; /* no work */ 118751974Smsmith } 118851974Smsmith } 118958883Smsmith 119058883Smsmith /* if we've completed any commands, try posting some more */ 119158883Smsmith if (result) 119258883Smsmith amr_startio(sc); 119358883Smsmith 119458883Smsmith /* handle completion and timeouts */ 119565245Smsmith#if __FreeBSD_version >= 500005 119665245Smsmith if (sc->amr_state & AMR_STATE_INTEN) 119765245Smsmith taskqueue_enqueue(taskqueue_swi, &sc->amr_task_complete); 119865245Smsmith else 119965245Smsmith#endif 120065245Smsmith amr_complete(sc, 0); 120158883Smsmith 120251974Smsmith return(result); 120351974Smsmith} 120451974Smsmith 120551974Smsmith/******************************************************************************** 120651974Smsmith * Do completion processing on done commands on (sc) 120751974Smsmith */ 120851974Smsmithstatic void 120965245Smsmithamr_complete(void *context, int pending) 121051974Smsmith{ 121165245Smsmith struct amr_softc *sc = (struct amr_softc *)context; 121265245Smsmith struct amr_command *ac; 121351974Smsmith 121465245Smsmith debug_called(2); 121551974Smsmith 121665245Smsmith /* pull completed commands off the queue */ 121765245Smsmith for (;;) { 121865245Smsmith ac = amr_dequeue_completed(sc); 121965245Smsmith if (ac == NULL) 122065245Smsmith break; 122158883Smsmith 122265245Smsmith /* unmap the command's data buffer */ 122365245Smsmith amr_unmapcmd(ac); 122451974Smsmith 122565245Smsmith /* unbusy the command */ 122665245Smsmith ac->ac_flags &= ~AMR_CMD_BUSY; 122751974Smsmith 122865245Smsmith /* 122965245Smsmith * Is there a completion handler? 123065245Smsmith */ 123165245Smsmith if (ac->ac_complete != NULL) { 123265245Smsmith ac->ac_complete(ac); 123365245Smsmith 123451974Smsmith /* 123565245Smsmith * Is someone sleeping on this one? 123651974Smsmith */ 123765245Smsmith } else if (ac->ac_flags & AMR_CMD_SLEEP) { 123865245Smsmith wakeup(ac); 123951974Smsmith } 124051974Smsmith } 124151974Smsmith} 124251974Smsmith 124351974Smsmith/******************************************************************************** 124451974Smsmith ******************************************************************************** 124551974Smsmith Command Buffer Management 124651974Smsmith ******************************************************************************** 124751974Smsmith ********************************************************************************/ 124851974Smsmith 124951974Smsmith/******************************************************************************** 125051974Smsmith * Get a new command buffer. 125151974Smsmith * 125251974Smsmith * This may return NULL in low-memory cases. 125351974Smsmith * 125451974Smsmith * If possible, we recycle a command buffer that's been used before. 125551974Smsmith */ 125665245Smsmithstruct amr_command * 125751974Smsmithamr_alloccmd(struct amr_softc *sc) 125851974Smsmith{ 125951974Smsmith struct amr_command *ac; 126051974Smsmith 126165245Smsmith debug_called(3); 126251974Smsmith 126365245Smsmith ac = amr_dequeue_free(sc); 126451974Smsmith if (ac == NULL) { 126565245Smsmith amr_alloccmd_cluster(sc); 126665245Smsmith ac = amr_dequeue_free(sc); 126751974Smsmith } 126865245Smsmith if (ac == NULL) 126965245Smsmith return(NULL); 127065245Smsmith 127165245Smsmith /* clear out significant fields */ 127265245Smsmith ac->ac_slot = 0; 127365245Smsmith ac->ac_status = 0; 127451974Smsmith bzero(&ac->ac_mailbox, sizeof(struct amr_mailbox)); 127565245Smsmith ac->ac_flags = 0; 127665245Smsmith ac->ac_bio = NULL; 127765245Smsmith ac->ac_data = NULL; 127865245Smsmith ac->ac_ccb_data = NULL; 127965245Smsmith ac->ac_complete = NULL; 128051974Smsmith return(ac); 128151974Smsmith} 128251974Smsmith 128351974Smsmith/******************************************************************************** 128451974Smsmith * Release a command buffer for recycling. 128551974Smsmith */ 128665245Smsmithvoid 128751974Smsmithamr_releasecmd(struct amr_command *ac) 128851974Smsmith{ 128965245Smsmith debug_called(3); 129051974Smsmith 129165245Smsmith amr_enqueue_free(ac); 129251974Smsmith} 129351974Smsmith 129451974Smsmith/******************************************************************************** 129565245Smsmith * Allocate a new command cluster and initialise it. 129651974Smsmith */ 129765245Smsmithvoid 129865245Smsmithamr_alloccmd_cluster(struct amr_softc *sc) 129951974Smsmith{ 130065245Smsmith struct amr_command_cluster *acc; 130165245Smsmith struct amr_command *ac; 130265245Smsmith int s, i; 130351974Smsmith 130465245Smsmith acc = malloc(AMR_CMD_CLUSTERSIZE, M_DEVBUF, M_NOWAIT); 130565245Smsmith if (acc != NULL) { 130665245Smsmith s = splbio(); 130765245Smsmith TAILQ_INSERT_TAIL(&sc->amr_cmd_clusters, acc, acc_link); 130865245Smsmith splx(s); 130965245Smsmith for (i = 0; i < AMR_CMD_CLUSTERCOUNT; i++) { 131065245Smsmith ac = &acc->acc_command[i]; 131165245Smsmith bzero(ac, sizeof(*ac)); 131265245Smsmith ac->ac_sc = sc; 131365245Smsmith if (!bus_dmamap_create(sc->amr_buffer_dmat, 0, &ac->ac_dmamap) && 131465245Smsmith !bus_dmamap_create(sc->amr_buffer_dmat, 0, &ac->ac_ccb_dmamap)) 131565245Smsmith amr_releasecmd(ac); 131665245Smsmith } 131765245Smsmith } 131851974Smsmith} 131951974Smsmith 132051974Smsmith/******************************************************************************** 132165245Smsmith * Free a command cluster 132265245Smsmith */ 132365245Smsmithvoid 132465245Smsmithamr_freecmd_cluster(struct amr_command_cluster *acc) 132565245Smsmith{ 132665245Smsmith struct amr_softc *sc = acc->acc_command[0].ac_sc; 132765245Smsmith int i; 132865245Smsmith 132965245Smsmith for (i = 0; i < AMR_CMD_CLUSTERCOUNT; i++) 133065245Smsmith bus_dmamap_destroy(sc->amr_buffer_dmat, acc->acc_command[i].ac_dmamap); 133165245Smsmith free(acc, M_DEVBUF); 133265245Smsmith} 133365245Smsmith 133465245Smsmith/******************************************************************************** 133551974Smsmith ******************************************************************************** 133651974Smsmith Interface-specific Shims 133751974Smsmith ******************************************************************************** 133851974Smsmith ********************************************************************************/ 133951974Smsmith 134051974Smsmith/******************************************************************************** 134151974Smsmith * Tell the controller that the mailbox contains a valid command 134251974Smsmith */ 134365245Smsmithstatic int 134451974Smsmithamr_quartz_submit_command(struct amr_softc *sc) 134551974Smsmith{ 134665245Smsmith debug_called(3); 134751974Smsmith 134865245Smsmith if (AMR_QGET_IDB(sc) & AMR_QIDB_SUBMIT) 134965245Smsmith return(EBUSY); 135051974Smsmith AMR_QPUT_IDB(sc, sc->amr_mailboxphys | AMR_QIDB_SUBMIT); 135165245Smsmith return(0); 135251974Smsmith} 135351974Smsmith 135465245Smsmithstatic int 135551974Smsmithamr_std_submit_command(struct amr_softc *sc) 135651974Smsmith{ 135765245Smsmith debug_called(3); 135851974Smsmith 135965245Smsmith if (AMR_SGET_MBSTAT(sc) & AMR_SMBOX_BUSYFLAG) 136065245Smsmith return(EBUSY); 136151974Smsmith AMR_SPOST_COMMAND(sc); 136265245Smsmith return(0); 136351974Smsmith} 136451974Smsmith 136551974Smsmith/******************************************************************************** 136651974Smsmith * Claim any work that the controller has completed; acknowledge completion, 136751974Smsmith * save details of the completion in (mbsave) 136851974Smsmith */ 136951974Smsmithstatic int 137051974Smsmithamr_quartz_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave) 137151974Smsmith{ 137251974Smsmith int s, worked; 137351974Smsmith u_int32_t outd; 137451974Smsmith 137565245Smsmith debug_called(3); 137665245Smsmith 137751974Smsmith worked = 0; 137851974Smsmith s = splbio(); 137951974Smsmith 138051974Smsmith /* work waiting for us? */ 138151974Smsmith if ((outd = AMR_QGET_ODB(sc)) == AMR_QODB_READY) { 138251974Smsmith 138351974Smsmith /* save mailbox, which contains a list of completed commands */ 138465245Smsmith bcopy((void *)(uintptr_t)(volatile void *)sc->amr_mailbox, mbsave, sizeof(*mbsave)); 138551974Smsmith 138665245Smsmith /* acknowledge interrupt */ 138765245Smsmith AMR_QPUT_ODB(sc, AMR_QODB_READY); 138865245Smsmith 138951974Smsmith /* acknowledge that we have the commands */ 139051974Smsmith AMR_QPUT_IDB(sc, sc->amr_mailboxphys | AMR_QIDB_ACK); 139165245Smsmith 139265763Smsmith#ifndef AMR_QUARTZ_GOFASTER 139365245Smsmith /* 139465245Smsmith * This waits for the controller to notice that we've taken the 139565245Smsmith * command from it. It's very inefficient, and we shouldn't do it, 139665245Smsmith * but if we remove this code, we stop completing commands under 139765245Smsmith * load. 139865245Smsmith * 139965245Smsmith * Peter J says we shouldn't do this. The documentation says we 140065245Smsmith * should. Who is right? 140165245Smsmith */ 140251974Smsmith while(AMR_QGET_IDB(sc) & AMR_QIDB_ACK) 140351974Smsmith ; /* XXX aiee! what if it dies? */ 140465245Smsmith#endif 140565245Smsmith 140651974Smsmith worked = 1; /* got some work */ 140751974Smsmith } 140851974Smsmith 140951974Smsmith splx(s); 141051974Smsmith return(worked); 141151974Smsmith} 141251974Smsmith 141351974Smsmithstatic int 141451974Smsmithamr_std_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave) 141551974Smsmith{ 141651974Smsmith int s, worked; 141751974Smsmith u_int8_t istat; 141851974Smsmith 141965245Smsmith debug_called(3); 142051974Smsmith 142151974Smsmith worked = 0; 142251974Smsmith s = splbio(); 142351974Smsmith 142451974Smsmith /* check for valid interrupt status */ 142551974Smsmith istat = AMR_SGET_ISTAT(sc); 142651974Smsmith if ((istat & AMR_SINTR_VALID) != 0) { 142751974Smsmith AMR_SPUT_ISTAT(sc, istat); /* ack interrupt status */ 142851974Smsmith 142951974Smsmith /* save mailbox, which contains a list of completed commands */ 143065245Smsmith bcopy((void *)(uintptr_t)(volatile void *)sc->amr_mailbox, mbsave, sizeof(*mbsave)); 143151974Smsmith 143251974Smsmith AMR_SACK_INTERRUPT(sc); /* acknowledge we have the mailbox */ 143351974Smsmith worked = 1; 143451974Smsmith } 143551974Smsmith 143651974Smsmith splx(s); 143751974Smsmith return(worked); 143851974Smsmith} 143951974Smsmith 144051974Smsmith/******************************************************************************** 144151974Smsmith * Notify the controller of the mailbox location. 144251974Smsmith */ 144351974Smsmithstatic void 144451974Smsmithamr_std_attach_mailbox(struct amr_softc *sc) 144551974Smsmith{ 144651974Smsmith 144751974Smsmith /* program the mailbox physical address */ 144851974Smsmith AMR_SBYTE_SET(sc, AMR_SMBOX_0, sc->amr_mailboxphys & 0xff); 144951974Smsmith AMR_SBYTE_SET(sc, AMR_SMBOX_1, (sc->amr_mailboxphys >> 8) & 0xff); 145051974Smsmith AMR_SBYTE_SET(sc, AMR_SMBOX_2, (sc->amr_mailboxphys >> 16) & 0xff); 145151974Smsmith AMR_SBYTE_SET(sc, AMR_SMBOX_3, (sc->amr_mailboxphys >> 24) & 0xff); 145251974Smsmith AMR_SBYTE_SET(sc, AMR_SMBOX_ENABLE, AMR_SMBOX_ADDR); 145351974Smsmith 145451974Smsmith /* clear any outstanding interrupt and enable interrupts proper */ 145551974Smsmith AMR_SACK_INTERRUPT(sc); 145651974Smsmith AMR_SENABLE_INTR(sc); 145751974Smsmith} 145851974Smsmith 145965245Smsmith#ifdef AMR_BOARD_INIT 146051974Smsmith/******************************************************************************** 146165245Smsmith * Initialise the controller 146265245Smsmith */ 146365245Smsmithstatic int 146465245Smsmithamr_quartz_init(struct amr_softc *sc) 146565245Smsmith{ 146665245Smsmith int status, ostatus; 146765245Smsmith 146865245Smsmith device_printf(sc->amr_dev, "initial init status %x\n", AMR_QGET_INITSTATUS(sc)); 146965245Smsmith 147065245Smsmith AMR_QRESET(sc); 147165245Smsmith 147265245Smsmith ostatus = 0xff; 147365245Smsmith while ((status = AMR_QGET_INITSTATUS(sc)) != AMR_QINIT_DONE) { 147465245Smsmith if (status != ostatus) { 147565245Smsmith device_printf(sc->amr_dev, "(%x) %s\n", status, amr_describe_code(amr_table_qinit, status)); 147665245Smsmith ostatus = status; 147765245Smsmith } 147865245Smsmith switch (status) { 147965245Smsmith case AMR_QINIT_NOMEM: 148065245Smsmith return(ENOMEM); 148165245Smsmith 148265245Smsmith case AMR_QINIT_SCAN: 148365245Smsmith /* XXX we could print channel/target here */ 148465245Smsmith break; 148565245Smsmith } 148665245Smsmith } 148765245Smsmith return(0); 148865245Smsmith} 148965245Smsmith 149065245Smsmithstatic int 149165245Smsmithamr_std_init(struct amr_softc *sc) 149265245Smsmith{ 149365245Smsmith int status, ostatus; 149465245Smsmith 149565245Smsmith device_printf(sc->amr_dev, "initial init status %x\n", AMR_SGET_INITSTATUS(sc)); 149665245Smsmith 149765245Smsmith AMR_SRESET(sc); 149865245Smsmith 149965245Smsmith ostatus = 0xff; 150065245Smsmith while ((status = AMR_SGET_INITSTATUS(sc)) != AMR_SINIT_DONE) { 150165245Smsmith if (status != ostatus) { 150265245Smsmith device_printf(sc->amr_dev, "(%x) %s\n", status, amr_describe_code(amr_table_sinit, status)); 150365245Smsmith ostatus = status; 150465245Smsmith } 150565245Smsmith switch (status) { 150665245Smsmith case AMR_SINIT_NOMEM: 150765245Smsmith return(ENOMEM); 150865245Smsmith 150965245Smsmith case AMR_SINIT_INPROG: 151065245Smsmith /* XXX we could print channel/target here? */ 151165245Smsmith break; 151265245Smsmith } 151365245Smsmith } 151465245Smsmith return(0); 151565245Smsmith} 151665245Smsmith#endif 151765245Smsmith 151865245Smsmith/******************************************************************************** 151951974Smsmith ******************************************************************************** 152051974Smsmith Debugging 152151974Smsmith ******************************************************************************** 152251974Smsmith ********************************************************************************/ 152351974Smsmith 152451974Smsmith/******************************************************************************** 152565245Smsmith * Identify the controller and print some information about it. 152665245Smsmith */ 152765245Smsmithstatic void 152865245Smsmithamr_describe_controller(struct amr_softc *sc) 152965245Smsmith{ 153065245Smsmith struct amr_prodinfo *ap; 153165245Smsmith struct amr_enquiry *ae; 153265245Smsmith char *prod; 153365245Smsmith 153465245Smsmith /* 153565245Smsmith * Try to get 40LD product info, which tells us what the card is labelled as. 153665245Smsmith */ 153765245Smsmith if ((ap = amr_enquiry(sc, 2048, AMR_CMD_CONFIG, AMR_CONFIG_PRODUCT_INFO, 0)) != NULL) { 153865245Smsmith device_printf(sc->amr_dev, "<%.80s> Firmware %.16s, BIOS %.16s, %dMB RAM\n", 153965245Smsmith ap->ap_product, ap->ap_firmware, ap->ap_bios, 154065245Smsmith ap->ap_memsize); 154165245Smsmith 154265245Smsmith free(ap, M_DEVBUF); 154365245Smsmith return; 154465245Smsmith } 154565245Smsmith 154665245Smsmith /* 154765245Smsmith * Try 8LD extended ENQUIRY to get controller signature, and use lookup table. 154865245Smsmith */ 154965245Smsmith if ((ae = (struct amr_enquiry *)amr_enquiry(sc, 2048, AMR_CMD_EXT_ENQUIRY2, 0, 0)) != NULL) { 155065245Smsmith prod = amr_describe_code(amr_table_adaptertype, ae->ae_signature); 155165245Smsmith 155265245Smsmith } else if ((ae = (struct amr_enquiry *)amr_enquiry(sc, 2048, AMR_CMD_ENQUIRY, 0, 0)) != NULL) { 155365245Smsmith 155465245Smsmith /* 155565245Smsmith * Try to work it out based on the PCI signatures. 155665245Smsmith */ 155765245Smsmith switch (pci_get_device(sc->amr_dev)) { 155865245Smsmith case 0x9010: 155965245Smsmith prod = "Series 428"; 156065245Smsmith break; 156165245Smsmith case 0x9060: 156265245Smsmith prod = "Series 434"; 156365245Smsmith break; 156465245Smsmith default: 156565245Smsmith prod = "unknown controller"; 156665245Smsmith break; 156765245Smsmith } 156865245Smsmith } else { 156965245Smsmith prod = "unsupported controller"; 157065245Smsmith } 157165245Smsmith device_printf(sc->amr_dev, "<%s> Firmware %.4s, BIOS %.4s, %dMB RAM\n", 157265245Smsmith prod, ae->ae_adapter.aa_firmware, ae->ae_adapter.aa_bios, 157365245Smsmith ae->ae_adapter.aa_memorysize); 157465245Smsmith free(ae, M_DEVBUF); 157565245Smsmith} 157665245Smsmith 157765245Smsmith#ifdef AMR_DEBUG 157865245Smsmith/******************************************************************************** 157951974Smsmith * Print the command (ac) in human-readable format 158051974Smsmith */ 158151974Smsmithstatic void 158251974Smsmithamr_printcommand(struct amr_command *ac) 158351974Smsmith{ 158451974Smsmith struct amr_softc *sc = ac->ac_sc; 158551974Smsmith struct amr_sgentry *sg; 158651974Smsmith int i; 158751974Smsmith 158851974Smsmith device_printf(sc->amr_dev, "cmd %x ident %d drive %d\n", 158951974Smsmith ac->ac_mailbox.mb_command, ac->ac_mailbox.mb_ident, ac->ac_mailbox.mb_drive); 159051974Smsmith device_printf(sc->amr_dev, "blkcount %d lba %d\n", 159151974Smsmith ac->ac_mailbox.mb_blkcount, ac->ac_mailbox.mb_lba); 159254512Speter device_printf(sc->amr_dev, "virtaddr %p length %lu\n", ac->ac_data, (unsigned long)ac->ac_length); 159358883Smsmith device_printf(sc->amr_dev, "sg physaddr %08x nsg %d\n", 159451974Smsmith ac->ac_mailbox.mb_physaddr, ac->ac_mailbox.mb_nsgelem); 159565245Smsmith device_printf(sc->amr_dev, "ccb %p bio %p\n", ac->ac_ccb_data, ac->ac_bio); 159651974Smsmith 159751974Smsmith /* get base address of s/g table */ 159851974Smsmith sg = sc->amr_sgtable + (ac->ac_slot * AMR_NSEG); 159951974Smsmith for (i = 0; i < ac->ac_mailbox.mb_nsgelem; i++, sg++) 160051974Smsmith device_printf(sc->amr_dev, " %x/%d\n", sg->sg_addr, sg->sg_count); 160151974Smsmith} 160265245Smsmith#endif 1603