xref: /freebsd/sys/dev/ciss/ciss.c (revision 4fbd232c8640bb1d58dca36e50df7c1c8eb4ca72)
13a31b7ebSMike Smith /*-
23a31b7ebSMike Smith  * Copyright (c) 2001 Michael Smith
33a31b7ebSMike Smith  * All rights reserved.
43a31b7ebSMike Smith  *
53a31b7ebSMike Smith  * Redistribution and use in source and binary forms, with or without
63a31b7ebSMike Smith  * modification, are permitted provided that the following conditions
73a31b7ebSMike Smith  * are met:
83a31b7ebSMike Smith  * 1. Redistributions of source code must retain the above copyright
93a31b7ebSMike Smith  *    notice, this list of conditions and the following disclaimer.
103a31b7ebSMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
113a31b7ebSMike Smith  *    notice, this list of conditions and the following disclaimer in the
123a31b7ebSMike Smith  *    documentation and/or other materials provided with the distribution.
133a31b7ebSMike Smith  *
143a31b7ebSMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
153a31b7ebSMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
163a31b7ebSMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
173a31b7ebSMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
183a31b7ebSMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
193a31b7ebSMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
203a31b7ebSMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
213a31b7ebSMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
223a31b7ebSMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
233a31b7ebSMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
243a31b7ebSMike Smith  * SUCH DAMAGE.
253a31b7ebSMike Smith  *
263a31b7ebSMike Smith  *	$FreeBSD$
273a31b7ebSMike Smith  */
283a31b7ebSMike Smith 
293a31b7ebSMike Smith /*
303a31b7ebSMike Smith  * Common Interface for SCSI-3 Support driver.
313a31b7ebSMike Smith  *
323a31b7ebSMike Smith  * CISS claims to provide a common interface between a generic SCSI
333a31b7ebSMike Smith  * transport and an intelligent host adapter.
343a31b7ebSMike Smith  *
353a31b7ebSMike Smith  * This driver supports CISS as defined in the document "CISS Command
363a31b7ebSMike Smith  * Interface for SCSI-3 Support Open Specification", Version 1.04,
373a31b7ebSMike Smith  * Valence Number 1, dated 20001127, produced by Compaq Computer
383a31b7ebSMike Smith  * Corporation.  This document appears to be a hastily and somewhat
393a31b7ebSMike Smith  * arbitrarlily cut-down version of a larger (and probably even more
403a31b7ebSMike Smith  * chaotic and inconsistent) Compaq internal document.  Various
413a31b7ebSMike Smith  * details were also gleaned from Compaq's "cciss" driver for Linux.
423a31b7ebSMike Smith  *
433a31b7ebSMike Smith  * We provide a shim layer between the CISS interface and CAM,
443a31b7ebSMike Smith  * offloading most of the queueing and being-a-disk chores onto CAM.
453a31b7ebSMike Smith  * Entry to the driver is via the PCI bus attachment (ciss_probe,
463a31b7ebSMike Smith  * ciss_attach, etc) and via the CAM interface (ciss_cam_action,
473a31b7ebSMike Smith  * ciss_cam_poll).  The Compaq CISS adapters are, however, poor SCSI
483a31b7ebSMike Smith  * citizens and we have to fake up some responses to get reasonable
493a31b7ebSMike Smith  * behaviour out of them.  In addition, the CISS command set is by no
503a31b7ebSMike Smith  * means adequate to support the functionality of a RAID controller,
513a31b7ebSMike Smith  * and thus the supported Compaq adapters utilise portions of the
523a31b7ebSMike Smith  * control protocol from earlier Compaq adapter families.
533a31b7ebSMike Smith  *
543a31b7ebSMike Smith  * Note that we only support the "simple" transport layer over PCI.
553a31b7ebSMike Smith  * This interface (ab)uses the I2O register set (specifically the post
563a31b7ebSMike Smith  * queues) to exchange commands with the adapter.  Other interfaces
573a31b7ebSMike Smith  * are available, but we aren't supposed to know about them, and it is
583a31b7ebSMike Smith  * dubious whether they would provide major performance improvements
593a31b7ebSMike Smith  * except under extreme load.
603a31b7ebSMike Smith  *
613a31b7ebSMike Smith  * Currently the only supported CISS adapters are the Compaq Smart
623a31b7ebSMike Smith  * Array 5* series (5300, 5i, 532).  Even with only three adapters,
633a31b7ebSMike Smith  * Compaq still manage to have interface variations.
643a31b7ebSMike Smith  *
653a31b7ebSMike Smith  *
663a31b7ebSMike Smith  * Thanks must go to Fred Harris and Darryl DeVinney at Compaq, as
673a31b7ebSMike Smith  * well as Paul Saab at Yahoo! for their assistance in making this
683a31b7ebSMike Smith  * driver happen.
693a31b7ebSMike Smith  */
703a31b7ebSMike Smith 
713a31b7ebSMike Smith #include <sys/param.h>
723a31b7ebSMike Smith #include <sys/systm.h>
733a31b7ebSMike Smith #include <sys/malloc.h>
743a31b7ebSMike Smith #include <sys/kernel.h>
753a31b7ebSMike Smith #include <sys/bus.h>
763a31b7ebSMike Smith #include <sys/conf.h>
773a31b7ebSMike Smith #include <sys/stat.h>
783a31b7ebSMike Smith 
793a31b7ebSMike Smith #include <cam/cam.h>
803a31b7ebSMike Smith #include <cam/cam_ccb.h>
813a31b7ebSMike Smith #include <cam/cam_periph.h>
823a31b7ebSMike Smith #include <cam/cam_sim.h>
833a31b7ebSMike Smith #include <cam/cam_xpt_sim.h>
843a31b7ebSMike Smith #include <cam/scsi/scsi_all.h>
853a31b7ebSMike Smith #include <cam/scsi/scsi_message.h>
863a31b7ebSMike Smith 
873a31b7ebSMike Smith #include <machine/clock.h>
883a31b7ebSMike Smith #include <machine/bus_memio.h>
893a31b7ebSMike Smith #include <machine/bus.h>
903a31b7ebSMike Smith #include <machine/endian.h>
913a31b7ebSMike Smith #include <machine/resource.h>
923a31b7ebSMike Smith #include <sys/rman.h>
933a31b7ebSMike Smith 
944fbd232cSWarner Losh #include <dev/pci/pcireg.h>
954fbd232cSWarner Losh #include <dev/pci/pcivar.h>
963a31b7ebSMike Smith 
973a31b7ebSMike Smith #include <dev/ciss/cissreg.h>
983a31b7ebSMike Smith #include <dev/ciss/cissvar.h>
993a31b7ebSMike Smith #include <dev/ciss/cissio.h>
1003a31b7ebSMike Smith 
1013a31b7ebSMike Smith MALLOC_DEFINE(CISS_MALLOC_CLASS, "ciss_data", "ciss internal data buffers");
1023a31b7ebSMike Smith 
1033a31b7ebSMike Smith /* pci interface */
1043a31b7ebSMike Smith static int	ciss_lookup(device_t dev);
1053a31b7ebSMike Smith static int	ciss_probe(device_t dev);
1063a31b7ebSMike Smith static int	ciss_attach(device_t dev);
1073a31b7ebSMike Smith static int	ciss_detach(device_t dev);
1083a31b7ebSMike Smith static int	ciss_shutdown(device_t dev);
1093a31b7ebSMike Smith 
1103a31b7ebSMike Smith /* (de)initialisation functions, control wrappers */
1113a31b7ebSMike Smith static int	ciss_init_pci(struct ciss_softc *sc);
1123a31b7ebSMike Smith static int	ciss_wait_adapter(struct ciss_softc *sc);
1133a31b7ebSMike Smith static int	ciss_flush_adapter(struct ciss_softc *sc);
1143a31b7ebSMike Smith static int	ciss_init_requests(struct ciss_softc *sc);
1153a31b7ebSMike Smith static void	ciss_command_map_helper(void *arg, bus_dma_segment_t *segs,
1163a31b7ebSMike Smith 					int nseg, int error);
1173a31b7ebSMike Smith static int	ciss_identify_adapter(struct ciss_softc *sc);
1183a31b7ebSMike Smith static int	ciss_init_logical(struct ciss_softc *sc);
1193a31b7ebSMike Smith static int	ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld);
1203a31b7ebSMike Smith static int	ciss_get_ldrive_status(struct ciss_softc *sc,  struct ciss_ldrive *ld);
1213a31b7ebSMike Smith static int	ciss_update_config(struct ciss_softc *sc);
1223a31b7ebSMike Smith static int	ciss_accept_media(struct ciss_softc *sc, int ldrive, int async);
1233a31b7ebSMike Smith static void	ciss_accept_media_complete(struct ciss_request *cr);
1243a31b7ebSMike Smith static void	ciss_free(struct ciss_softc *sc);
1253a31b7ebSMike Smith 
1263a31b7ebSMike Smith /* request submission/completion */
1273a31b7ebSMike Smith static int	ciss_start(struct ciss_request *cr);
1283a31b7ebSMike Smith static void	ciss_done(struct ciss_softc *sc);
1293a31b7ebSMike Smith static void	ciss_intr(void *arg);
1303a31b7ebSMike Smith static void	ciss_complete(struct ciss_softc *sc);
1313a31b7ebSMike Smith static int	ciss_report_request(struct ciss_request *cr, int *command_status,
1323a31b7ebSMike Smith 				    int *scsi_status);
1333a31b7ebSMike Smith static int	ciss_synch_request(struct ciss_request *cr, int timeout);
1343a31b7ebSMike Smith static int	ciss_poll_request(struct ciss_request *cr, int timeout);
1353a31b7ebSMike Smith static int	ciss_wait_request(struct ciss_request *cr, int timeout);
1366197ca15SPeter Wemm #if 0
1373a31b7ebSMike Smith static int	ciss_abort_request(struct ciss_request *cr);
1386197ca15SPeter Wemm #endif
1393a31b7ebSMike Smith 
1403a31b7ebSMike Smith /* request queueing */
1413a31b7ebSMike Smith static int	ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp);
1423a31b7ebSMike Smith static void	ciss_preen_command(struct ciss_request *cr);
1433a31b7ebSMike Smith static void 	ciss_release_request(struct ciss_request *cr);
1443a31b7ebSMike Smith 
1453a31b7ebSMike Smith /* request helpers */
1463a31b7ebSMike Smith static int	ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
1473a31b7ebSMike Smith 				      int opcode, void **bufp, size_t bufsize);
1483a31b7ebSMike Smith static int	ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc);
1493a31b7ebSMike Smith 
1503a31b7ebSMike Smith /* DMA map/unmap */
1513a31b7ebSMike Smith static int	ciss_map_request(struct ciss_request *cr);
1523a31b7ebSMike Smith static void	ciss_request_map_helper(void *arg, bus_dma_segment_t *segs,
1533a31b7ebSMike Smith 					int nseg, int error);
1543a31b7ebSMike Smith static void	ciss_unmap_request(struct ciss_request *cr);
1553a31b7ebSMike Smith 
1563a31b7ebSMike Smith /* CAM interface */
1573a31b7ebSMike Smith static int	ciss_cam_init(struct ciss_softc *sc);
1583a31b7ebSMike Smith static void	ciss_cam_rescan_target(struct ciss_softc *sc, int target);
1593a31b7ebSMike Smith static void	ciss_cam_rescan_all(struct ciss_softc *sc);
1603a31b7ebSMike Smith static void	ciss_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb);
1613a31b7ebSMike Smith static void	ciss_cam_action(struct cam_sim *sim, union ccb *ccb);
1623a31b7ebSMike Smith static int	ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio);
1633a31b7ebSMike Smith static int	ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio);
1643a31b7ebSMike Smith static void	ciss_cam_poll(struct cam_sim *sim);
1653a31b7ebSMike Smith static void	ciss_cam_complete(struct ciss_request *cr);
1663a31b7ebSMike Smith static void	ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio);
1673a31b7ebSMike Smith static struct cam_periph *ciss_find_periph(struct ciss_softc *sc, int target);
1683a31b7ebSMike Smith static int	ciss_name_device(struct ciss_softc *sc, int target);
1693a31b7ebSMike Smith 
1703a31b7ebSMike Smith /* periodic status monitoring */
1713a31b7ebSMike Smith static void	ciss_periodic(void *arg);
1723a31b7ebSMike Smith static void	ciss_notify_event(struct ciss_softc *sc);
1733a31b7ebSMike Smith static void	ciss_notify_complete(struct ciss_request *cr);
1743a31b7ebSMike Smith static int	ciss_notify_abort(struct ciss_softc *sc);
1753a31b7ebSMike Smith static int	ciss_notify_abort_bmic(struct ciss_softc *sc);
1763a31b7ebSMike Smith static void	ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn);
1773a31b7ebSMike Smith static void	ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn);
1783a31b7ebSMike Smith 
1793a31b7ebSMike Smith /* debugging output */
1803a31b7ebSMike Smith static void	ciss_print_request(struct ciss_request *cr);
1813a31b7ebSMike Smith static void	ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld);
1823a31b7ebSMike Smith static const char *ciss_name_ldrive_status(int status);
1833a31b7ebSMike Smith static int	ciss_decode_ldrive_status(int status);
1843a31b7ebSMike Smith static const char *ciss_name_ldrive_org(int org);
1853a31b7ebSMike Smith static const char *ciss_name_command_status(int status);
1863a31b7ebSMike Smith 
1873a31b7ebSMike Smith /*
1883a31b7ebSMike Smith  * PCI bus interface.
1893a31b7ebSMike Smith  */
1903a31b7ebSMike Smith static device_method_t ciss_methods[] = {
1913a31b7ebSMike Smith     /* Device interface */
1923a31b7ebSMike Smith     DEVMETHOD(device_probe,	ciss_probe),
1933a31b7ebSMike Smith     DEVMETHOD(device_attach,	ciss_attach),
1943a31b7ebSMike Smith     DEVMETHOD(device_detach,	ciss_detach),
1953a31b7ebSMike Smith     DEVMETHOD(device_shutdown,	ciss_shutdown),
1963a31b7ebSMike Smith     { 0, 0 }
1973a31b7ebSMike Smith };
1983a31b7ebSMike Smith 
1993a31b7ebSMike Smith static driver_t ciss_pci_driver = {
2003a31b7ebSMike Smith     "ciss",
2013a31b7ebSMike Smith     ciss_methods,
2023a31b7ebSMike Smith     sizeof(struct ciss_softc)
2033a31b7ebSMike Smith };
2043a31b7ebSMike Smith 
2053a31b7ebSMike Smith static devclass_t	ciss_devclass;
2063a31b7ebSMike Smith DRIVER_MODULE(ciss, pci, ciss_pci_driver, ciss_devclass, 0, 0);
2073a31b7ebSMike Smith 
2083a31b7ebSMike Smith /*
2093a31b7ebSMike Smith  * Control device interface.
2103a31b7ebSMike Smith  */
2113a31b7ebSMike Smith static d_open_t		ciss_open;
2123a31b7ebSMike Smith static d_close_t	ciss_close;
2133a31b7ebSMike Smith static d_ioctl_t	ciss_ioctl;
2143a31b7ebSMike Smith 
2153a31b7ebSMike Smith #define CISS_CDEV_MAJOR  166
2163a31b7ebSMike Smith 
2173a31b7ebSMike Smith static struct cdevsw ciss_cdevsw = {
2187ac40f5fSPoul-Henning Kamp 	.d_open =	ciss_open,
2197ac40f5fSPoul-Henning Kamp 	.d_close =	ciss_close,
2207ac40f5fSPoul-Henning Kamp 	.d_ioctl =	ciss_ioctl,
2217ac40f5fSPoul-Henning Kamp 	.d_name =	"ciss",
2227ac40f5fSPoul-Henning Kamp 	.d_maj =	CISS_CDEV_MAJOR,
2233a31b7ebSMike Smith };
2243a31b7ebSMike Smith 
2253a31b7ebSMike Smith /************************************************************************
2263a31b7ebSMike Smith  * CISS adapters amazingly don't have a defined programming interface
2273a31b7ebSMike Smith  * value.  (One could say some very despairing things about PCI and
2283a31b7ebSMike Smith  * people just not getting the general idea.)  So we are forced to
2293a31b7ebSMike Smith  * stick with matching against subvendor/subdevice, and thus have to
2303a31b7ebSMike Smith  * be updated for every new CISS adapter that appears.
2313a31b7ebSMike Smith  */
2323a31b7ebSMike Smith #define CISS_BOARD_SA5	(1<<0)
2333a31b7ebSMike Smith #define CISS_BOARD_SA5B	(1<<1)
2343a31b7ebSMike Smith 
2353a31b7ebSMike Smith static struct
2363a31b7ebSMike Smith {
2373a31b7ebSMike Smith     u_int16_t	subvendor;
2383a31b7ebSMike Smith     u_int16_t	subdevice;
2393a31b7ebSMike Smith     int		flags;
2403a31b7ebSMike Smith     char	*desc;
2413a31b7ebSMike Smith } ciss_vendor_data[] = {
2423a31b7ebSMike Smith     { 0x0e11, 0x4070, CISS_BOARD_SA5,	"Compaq Smart Array 5300" },
2433a31b7ebSMike Smith     { 0x0e11, 0x4080, CISS_BOARD_SA5B,	"Compaq Smart Array 5i" },
2443a31b7ebSMike Smith     { 0x0e11, 0x4082, CISS_BOARD_SA5B,	"Compaq Smart Array 532" },
2452648ded5SPaul Saab     { 0x0e11, 0x4083, CISS_BOARD_SA5B,	"HP Smart Array 5312" },
2469e7313bbSPaul Saab     { 0x0e11, 0x409A, CISS_BOARD_SA5,	"HP Smart Array 641" },
2479e7313bbSPaul Saab     { 0x0e11, 0x409B, CISS_BOARD_SA5,	"HP Smart Array 642" },
2489e7313bbSPaul Saab     { 0x0e11, 0x409C, CISS_BOARD_SA5,	"HP Smart Array 6400" },
2499e7313bbSPaul Saab     { 0x0e11, 0x409D, CISS_BOARD_SA5,	"HP Smart Array 6400 EM" },
2504a6a94d8SArchie Cobbs     { 0, 0, 0, NULL }
2513a31b7ebSMike Smith };
2523a31b7ebSMike Smith 
2533a31b7ebSMike Smith /************************************************************************
2543a31b7ebSMike Smith  * Find a match for the device in our list of known adapters.
2553a31b7ebSMike Smith  */
2563a31b7ebSMike Smith static int
2573a31b7ebSMike Smith ciss_lookup(device_t dev)
2583a31b7ebSMike Smith {
2593a31b7ebSMike Smith     int 	i;
2603a31b7ebSMike Smith 
2613a31b7ebSMike Smith     for (i = 0; ciss_vendor_data[i].desc != NULL; i++)
2623a31b7ebSMike Smith 	if ((pci_get_subvendor(dev) == ciss_vendor_data[i].subvendor) &&
2633a31b7ebSMike Smith 	    (pci_get_subdevice(dev) == ciss_vendor_data[i].subdevice)) {
2643a31b7ebSMike Smith 	    return(i);
2653a31b7ebSMike Smith 	}
2663a31b7ebSMike Smith     return(-1);
2673a31b7ebSMike Smith }
2683a31b7ebSMike Smith 
2693a31b7ebSMike Smith /************************************************************************
2703a31b7ebSMike Smith  * Match a known CISS adapter.
2713a31b7ebSMike Smith  */
2723a31b7ebSMike Smith static int
2733a31b7ebSMike Smith ciss_probe(device_t dev)
2743a31b7ebSMike Smith {
2753a31b7ebSMike Smith     int		i;
2763a31b7ebSMike Smith 
2773a31b7ebSMike Smith     i = ciss_lookup(dev);
2783a31b7ebSMike Smith     if (i != -1) {
2793a31b7ebSMike Smith 	device_set_desc(dev, ciss_vendor_data[i].desc);
2803a31b7ebSMike Smith 	return(-10);
2813a31b7ebSMike Smith     }
2823a31b7ebSMike Smith     return(ENOENT);
2833a31b7ebSMike Smith }
2843a31b7ebSMike Smith 
2853a31b7ebSMike Smith /************************************************************************
2863a31b7ebSMike Smith  * Attach the driver to this adapter.
2873a31b7ebSMike Smith  */
2883a31b7ebSMike Smith static int
2893a31b7ebSMike Smith ciss_attach(device_t dev)
2903a31b7ebSMike Smith {
2913a31b7ebSMike Smith     struct ciss_softc	*sc;
2923a31b7ebSMike Smith     int			i, error;
2933a31b7ebSMike Smith 
2943a31b7ebSMike Smith     debug_called(1);
2953a31b7ebSMike Smith 
2963a31b7ebSMike Smith #ifdef CISS_DEBUG
2973a31b7ebSMike Smith     /* print structure/union sizes */
2983a31b7ebSMike Smith     debug_struct(ciss_command);
2993a31b7ebSMike Smith     debug_struct(ciss_header);
3003a31b7ebSMike Smith     debug_union(ciss_device_address);
3013a31b7ebSMike Smith     debug_struct(ciss_cdb);
3023a31b7ebSMike Smith     debug_struct(ciss_report_cdb);
3033a31b7ebSMike Smith     debug_struct(ciss_notify_cdb);
3043a31b7ebSMike Smith     debug_struct(ciss_notify);
3053a31b7ebSMike Smith     debug_struct(ciss_message_cdb);
3063a31b7ebSMike Smith     debug_struct(ciss_error_info_pointer);
3073a31b7ebSMike Smith     debug_struct(ciss_error_info);
3083a31b7ebSMike Smith     debug_struct(ciss_sg_entry);
3093a31b7ebSMike Smith     debug_struct(ciss_config_table);
3103a31b7ebSMike Smith     debug_struct(ciss_bmic_cdb);
3113a31b7ebSMike Smith     debug_struct(ciss_bmic_id_ldrive);
3123a31b7ebSMike Smith     debug_struct(ciss_bmic_id_lstatus);
3133a31b7ebSMike Smith     debug_struct(ciss_bmic_id_table);
3143a31b7ebSMike Smith     debug_struct(ciss_bmic_id_pdrive);
3153a31b7ebSMike Smith     debug_struct(ciss_bmic_blink_pdrive);
3163a31b7ebSMike Smith     debug_struct(ciss_bmic_flush_cache);
3173a31b7ebSMike Smith     debug_const(CISS_MAX_REQUESTS);
3183a31b7ebSMike Smith     debug_const(CISS_MAX_LOGICAL);
3193a31b7ebSMike Smith     debug_const(CISS_INTERRUPT_COALESCE_DELAY);
3203a31b7ebSMike Smith     debug_const(CISS_INTERRUPT_COALESCE_COUNT);
3213a31b7ebSMike Smith     debug_const(CISS_COMMAND_ALLOC_SIZE);
3223a31b7ebSMike Smith     debug_const(CISS_COMMAND_SG_LENGTH);
3233a31b7ebSMike Smith 
3243a31b7ebSMike Smith     debug_type(cciss_pci_info_struct);
3253a31b7ebSMike Smith     debug_type(cciss_coalint_struct);
3263a31b7ebSMike Smith     debug_type(cciss_coalint_struct);
3273a31b7ebSMike Smith     debug_type(NodeName_type);
3283a31b7ebSMike Smith     debug_type(NodeName_type);
3293a31b7ebSMike Smith     debug_type(Heartbeat_type);
3303a31b7ebSMike Smith     debug_type(BusTypes_type);
3313a31b7ebSMike Smith     debug_type(FirmwareVer_type);
3323a31b7ebSMike Smith     debug_type(DriverVer_type);
3333a31b7ebSMike Smith     debug_type(IOCTL_Command_struct);
3343a31b7ebSMike Smith #endif
3353a31b7ebSMike Smith 
3363a31b7ebSMike Smith     sc = device_get_softc(dev);
3373a31b7ebSMike Smith     sc->ciss_dev = dev;
3383a31b7ebSMike Smith 
3393a31b7ebSMike Smith     /*
3403a31b7ebSMike Smith      * Work out adapter type.
3413a31b7ebSMike Smith      */
3423a31b7ebSMike Smith     i = ciss_lookup(dev);
3433a31b7ebSMike Smith     if (ciss_vendor_data[i].flags & CISS_BOARD_SA5) {
3443a31b7ebSMike Smith 	sc->ciss_interrupt_mask = CISS_TL_SIMPLE_INTR_OPQ_SA5;
3453a31b7ebSMike Smith     } else if (ciss_vendor_data[i].flags & CISS_BOARD_SA5B) {
3463a31b7ebSMike Smith 	sc->ciss_interrupt_mask = CISS_TL_SIMPLE_INTR_OPQ_SA5B;
3473a31b7ebSMike Smith     } else {
3483a31b7ebSMike Smith 	/* really an error on our part */
3493a31b7ebSMike Smith 	ciss_printf(sc, "unable to determine hardware type\n");
3503a31b7ebSMike Smith 	error = ENXIO;
3513a31b7ebSMike Smith 	goto out;
3523a31b7ebSMike Smith     }
3533a31b7ebSMike Smith 
3543a31b7ebSMike Smith     /*
3553a31b7ebSMike Smith      * Do PCI-specific init.
3563a31b7ebSMike Smith      */
3573a31b7ebSMike Smith     if ((error = ciss_init_pci(sc)) != 0)
3583a31b7ebSMike Smith 	goto out;
3593a31b7ebSMike Smith 
3603a31b7ebSMike Smith     /*
3613a31b7ebSMike Smith      * Initialise driver queues.
3623a31b7ebSMike Smith      */
3633a31b7ebSMike Smith     ciss_initq_free(sc);
3643a31b7ebSMike Smith     ciss_initq_busy(sc);
3653a31b7ebSMike Smith     ciss_initq_complete(sc);
3663a31b7ebSMike Smith 
3673a31b7ebSMike Smith     /*
3683a31b7ebSMike Smith      * Initialise command/request pool.
3693a31b7ebSMike Smith      */
3703a31b7ebSMike Smith     if ((error = ciss_init_requests(sc)) != 0)
3713a31b7ebSMike Smith 	goto out;
3723a31b7ebSMike Smith 
3733a31b7ebSMike Smith     /*
3743a31b7ebSMike Smith      * Get adapter information.
3753a31b7ebSMike Smith      */
3763a31b7ebSMike Smith     if ((error = ciss_identify_adapter(sc)) != 0)
3773a31b7ebSMike Smith 	goto out;
3783a31b7ebSMike Smith 
3793a31b7ebSMike Smith     /*
3803a31b7ebSMike Smith      * Build our private table of logical devices.
3813a31b7ebSMike Smith      */
3823a31b7ebSMike Smith     if ((error = ciss_init_logical(sc)) != 0)
3833a31b7ebSMike Smith 	goto out;
3843a31b7ebSMike Smith 
3853a31b7ebSMike Smith     /*
3863a31b7ebSMike Smith      * Enable interrupts so that the CAM scan can complete.
3873a31b7ebSMike Smith      */
3883a31b7ebSMike Smith     CISS_TL_SIMPLE_ENABLE_INTERRUPTS(sc);
3893a31b7ebSMike Smith 
3903a31b7ebSMike Smith     /*
3913a31b7ebSMike Smith      * Initialise the CAM interface.
3923a31b7ebSMike Smith      */
3933a31b7ebSMike Smith     if ((error = ciss_cam_init(sc)) != 0)
3943a31b7ebSMike Smith 	goto out;
3953a31b7ebSMike Smith 
3963a31b7ebSMike Smith     /*
3973a31b7ebSMike Smith      * Start the heartbeat routine and event chain.
3983a31b7ebSMike Smith      */
3993a31b7ebSMike Smith     ciss_periodic(sc);
4003a31b7ebSMike Smith 
4013a31b7ebSMike Smith    /*
4023a31b7ebSMike Smith      * Create the control device.
4033a31b7ebSMike Smith      */
4043a31b7ebSMike Smith     sc->ciss_dev_t = make_dev(&ciss_cdevsw, device_get_unit(sc->ciss_dev),
4053a31b7ebSMike Smith 			      UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
4063a31b7ebSMike Smith 			      "ciss%d", device_get_unit(sc->ciss_dev));
4073a31b7ebSMike Smith     sc->ciss_dev_t->si_drv1 = sc;
4083a31b7ebSMike Smith 
4093a31b7ebSMike Smith     /*
4103a31b7ebSMike Smith      * The adapter is running; synchronous commands can now sleep
4113a31b7ebSMike Smith      * waiting for an interrupt to signal completion.
4123a31b7ebSMike Smith      */
4133a31b7ebSMike Smith     sc->ciss_flags |= CISS_FLAG_RUNNING;
4143a31b7ebSMike Smith 
4153a31b7ebSMike Smith     error = 0;
4163a31b7ebSMike Smith  out:
4173a31b7ebSMike Smith     if (error != 0)
4183a31b7ebSMike Smith 	ciss_free(sc);
4193a31b7ebSMike Smith     return(error);
4203a31b7ebSMike Smith }
4213a31b7ebSMike Smith 
4223a31b7ebSMike Smith /************************************************************************
4233a31b7ebSMike Smith  * Detach the driver from this adapter.
4243a31b7ebSMike Smith  */
4253a31b7ebSMike Smith static int
4263a31b7ebSMike Smith ciss_detach(device_t dev)
4273a31b7ebSMike Smith {
4283a31b7ebSMike Smith     struct ciss_softc	*sc = device_get_softc(dev);
4293a31b7ebSMike Smith 
4303a31b7ebSMike Smith     debug_called(1);
4313a31b7ebSMike Smith 
4323a31b7ebSMike Smith     /* flush adapter cache */
4333a31b7ebSMike Smith     ciss_flush_adapter(sc);
4343a31b7ebSMike Smith 
435a8cdde69SMaxime Henrion     destroy_dev(sc->ciss_dev_t);
436a8cdde69SMaxime Henrion 
4373a31b7ebSMike Smith     /* release all resources */
4383a31b7ebSMike Smith     ciss_free(sc);
4393a31b7ebSMike Smith 
4403a31b7ebSMike Smith     return(0);
4413a31b7ebSMike Smith 
4423a31b7ebSMike Smith }
4433a31b7ebSMike Smith 
4443a31b7ebSMike Smith /************************************************************************
4453a31b7ebSMike Smith  * Prepare adapter for system shutdown.
4463a31b7ebSMike Smith  */
4473a31b7ebSMike Smith static int
4483a31b7ebSMike Smith ciss_shutdown(device_t dev)
4493a31b7ebSMike Smith {
4503a31b7ebSMike Smith     struct ciss_softc	*sc = device_get_softc(dev);
4513a31b7ebSMike Smith 
4523a31b7ebSMike Smith     debug_called(1);
4533a31b7ebSMike Smith 
4543a31b7ebSMike Smith     /* flush adapter cache */
4553a31b7ebSMike Smith     ciss_flush_adapter(sc);
4563a31b7ebSMike Smith 
4573a31b7ebSMike Smith     return(0);
4583a31b7ebSMike Smith }
4593a31b7ebSMike Smith 
4603a31b7ebSMike Smith /************************************************************************
4613a31b7ebSMike Smith  * Perform PCI-specific attachment actions.
4623a31b7ebSMike Smith  */
4633a31b7ebSMike Smith static int
4643a31b7ebSMike Smith ciss_init_pci(struct ciss_softc *sc)
4653a31b7ebSMike Smith {
4663a31b7ebSMike Smith     uintptr_t		cbase, csize, cofs;
4673a31b7ebSMike Smith     int			error;
4683a31b7ebSMike Smith 
4693a31b7ebSMike Smith     debug_called(1);
4703a31b7ebSMike Smith 
4713a31b7ebSMike Smith     /*
4723a31b7ebSMike Smith      * Allocate register window first (we need this to find the config
4733a31b7ebSMike Smith      * struct).
4743a31b7ebSMike Smith      */
4753a31b7ebSMike Smith     error = ENXIO;
4763a31b7ebSMike Smith     sc->ciss_regs_rid = CISS_TL_SIMPLE_BAR_REGS;
4773a31b7ebSMike Smith     if ((sc->ciss_regs_resource =
4783a31b7ebSMike Smith 	 bus_alloc_resource(sc->ciss_dev, SYS_RES_MEMORY, &sc->ciss_regs_rid,
4793a31b7ebSMike Smith 			    0, ~0, 1, RF_ACTIVE)) == NULL) {
4803a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate register window\n");
4813a31b7ebSMike Smith 	return(ENXIO);
4823a31b7ebSMike Smith     }
4833a31b7ebSMike Smith     sc->ciss_regs_bhandle = rman_get_bushandle(sc->ciss_regs_resource);
4843a31b7ebSMike Smith     sc->ciss_regs_btag = rman_get_bustag(sc->ciss_regs_resource);
4853a31b7ebSMike Smith 
4863a31b7ebSMike Smith     /*
4873a31b7ebSMike Smith      * Find the BAR holding the config structure.  If it's not the one
4883a31b7ebSMike Smith      * we already mapped for registers, map it too.
4893a31b7ebSMike Smith      */
4903a31b7ebSMike Smith     sc->ciss_cfg_rid = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_BAR) & 0xffff;
4913a31b7ebSMike Smith     if (sc->ciss_cfg_rid != sc->ciss_regs_rid) {
4923a31b7ebSMike Smith 	if ((sc->ciss_cfg_resource =
4933a31b7ebSMike Smith 	     bus_alloc_resource(sc->ciss_dev, SYS_RES_MEMORY, &sc->ciss_cfg_rid,
4943a31b7ebSMike Smith 				0, ~0, 1, RF_ACTIVE)) == NULL) {
4953a31b7ebSMike Smith 	    ciss_printf(sc, "can't allocate config window\n");
4963a31b7ebSMike Smith 	    return(ENXIO);
4973a31b7ebSMike Smith 	}
4983a31b7ebSMike Smith 	cbase = (uintptr_t)rman_get_virtual(sc->ciss_cfg_resource);
4993a31b7ebSMike Smith 	csize = rman_get_end(sc->ciss_cfg_resource) -
5003a31b7ebSMike Smith 	    rman_get_start(sc->ciss_cfg_resource) + 1;
5013a31b7ebSMike Smith     } else {
5023a31b7ebSMike Smith 	cbase = (uintptr_t)rman_get_virtual(sc->ciss_regs_resource);
5033a31b7ebSMike Smith 	csize = rman_get_end(sc->ciss_regs_resource) -
5043a31b7ebSMike Smith 	    rman_get_start(sc->ciss_regs_resource) + 1;
5053a31b7ebSMike Smith     }
5063a31b7ebSMike Smith     cofs = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_OFF);
5073a31b7ebSMike Smith 
5083a31b7ebSMike Smith     /*
5093a31b7ebSMike Smith      * Use the base/size/offset values we just calculated to
5103a31b7ebSMike Smith      * sanity-check the config structure.  If it's OK, point to it.
5113a31b7ebSMike Smith      */
5123a31b7ebSMike Smith     if ((cofs + sizeof(struct ciss_config_table)) > csize) {
5133a31b7ebSMike Smith 	ciss_printf(sc, "config table outside window\n");
5143a31b7ebSMike Smith 	return(ENXIO);
5153a31b7ebSMike Smith     }
5163a31b7ebSMike Smith     sc->ciss_cfg = (struct ciss_config_table *)(cbase + cofs);
5173a31b7ebSMike Smith     debug(1, "config struct at %p", sc->ciss_cfg);
5183a31b7ebSMike Smith 
5193a31b7ebSMike Smith     /*
5203a31b7ebSMike Smith      * Validate the config structure.  If we supported other transport
5213a31b7ebSMike Smith      * methods, we could select amongst them at this point in time.
5223a31b7ebSMike Smith      */
5233a31b7ebSMike Smith     if (strncmp(sc->ciss_cfg->signature, "CISS", 4)) {
5243a31b7ebSMike Smith 	ciss_printf(sc, "config signature mismatch (got '%c%c%c%c')\n",
5253a31b7ebSMike Smith 		    sc->ciss_cfg->signature[0], sc->ciss_cfg->signature[1],
5263a31b7ebSMike Smith 		    sc->ciss_cfg->signature[2], sc->ciss_cfg->signature[3]);
5273a31b7ebSMike Smith 	return(ENXIO);
5283a31b7ebSMike Smith     }
5293a31b7ebSMike Smith     if ((sc->ciss_cfg->valence < CISS_MIN_VALENCE) ||
5303a31b7ebSMike Smith 	(sc->ciss_cfg->valence > CISS_MAX_VALENCE)) {
5313a31b7ebSMike Smith 	ciss_printf(sc, "adapter interface specification (%d) unsupported\n",
5323a31b7ebSMike Smith 		    sc->ciss_cfg->valence);
5333a31b7ebSMike Smith 	return(ENXIO);
5343a31b7ebSMike Smith     }
5353a31b7ebSMike Smith 
5363a31b7ebSMike Smith     /*
5373a31b7ebSMike Smith      * Put the board into simple mode, and tell it we're using the low
5383a31b7ebSMike Smith      * 4GB of RAM.  Set the default interrupt coalescing options.
5393a31b7ebSMike Smith      */
5403a31b7ebSMike Smith     if (!(sc->ciss_cfg->supported_methods & CISS_TRANSPORT_METHOD_SIMPLE)) {
5413a31b7ebSMike Smith 	ciss_printf(sc, "adapter does not support 'simple' transport layer\n");
5423a31b7ebSMike Smith 	return(ENXIO);
5433a31b7ebSMike Smith     }
5443a31b7ebSMike Smith     sc->ciss_cfg->requested_method = CISS_TRANSPORT_METHOD_SIMPLE;
5453a31b7ebSMike Smith     sc->ciss_cfg->command_physlimit = 0;
5463a31b7ebSMike Smith     sc->ciss_cfg->interrupt_coalesce_delay = CISS_INTERRUPT_COALESCE_DELAY;
5473a31b7ebSMike Smith     sc->ciss_cfg->interrupt_coalesce_count = CISS_INTERRUPT_COALESCE_COUNT;
5483a31b7ebSMike Smith 
5493a31b7ebSMike Smith     if (ciss_update_config(sc)) {
5503a31b7ebSMike Smith 	ciss_printf(sc, "adapter refuses to accept config update (IDBR 0x%x)\n",
5513a31b7ebSMike Smith 		    CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR));
5523a31b7ebSMike Smith 	return(ENXIO);
5533a31b7ebSMike Smith     }
5543a31b7ebSMike Smith     if (!(sc->ciss_cfg->active_method != CISS_TRANSPORT_METHOD_SIMPLE)) {
5553a31b7ebSMike Smith 	ciss_printf(sc,
5563a31b7ebSMike Smith 		    "adapter refuses to go into 'simple' transport mode (0x%x, 0x%x)\n",
5573a31b7ebSMike Smith 		    sc->ciss_cfg->supported_methods, sc->ciss_cfg->active_method);
5583a31b7ebSMike Smith 	return(ENXIO);
5593a31b7ebSMike Smith     }
5603a31b7ebSMike Smith 
5613a31b7ebSMike Smith     /*
5623a31b7ebSMike Smith      * Wait for the adapter to come ready.
5633a31b7ebSMike Smith      */
5643a31b7ebSMike Smith     if ((error = ciss_wait_adapter(sc)) != 0)
5653a31b7ebSMike Smith 	return(error);
5663a31b7ebSMike Smith 
5673a31b7ebSMike Smith     /*
5683a31b7ebSMike Smith      * Turn off interrupts before we go routing anything.
5693a31b7ebSMike Smith      */
5703a31b7ebSMike Smith     CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
5713a31b7ebSMike Smith 
5723a31b7ebSMike Smith     /*
5733a31b7ebSMike Smith      * Allocate and set up our interrupt.
5743a31b7ebSMike Smith      */
5753a31b7ebSMike Smith     sc->ciss_irq_rid = 0;
5763a31b7ebSMike Smith     if ((sc->ciss_irq_resource =
5773a31b7ebSMike Smith 	 bus_alloc_resource(sc->ciss_dev, SYS_RES_IRQ, &sc->ciss_irq_rid, 0, ~0, 1,
5783a31b7ebSMike Smith 			    RF_ACTIVE | RF_SHAREABLE)) == NULL) {
5793a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate interrupt\n");
5803a31b7ebSMike Smith 	return(ENXIO);
5813a31b7ebSMike Smith     }
5823a31b7ebSMike Smith     if (bus_setup_intr(sc->ciss_dev, sc->ciss_irq_resource, INTR_TYPE_CAM, ciss_intr, sc,
5833a31b7ebSMike Smith 		       &sc->ciss_intr)) {
5843a31b7ebSMike Smith 	ciss_printf(sc, "can't set up interrupt\n");
5853a31b7ebSMike Smith 	return(ENXIO);
5863a31b7ebSMike Smith     }
5873a31b7ebSMike Smith 
5883a31b7ebSMike Smith     /*
5893a31b7ebSMike Smith      * Allocate the parent bus DMA tag appropriate for our PCI
5903a31b7ebSMike Smith      * interface.
5913a31b7ebSMike Smith      *
5923a31b7ebSMike Smith      * Note that "simple" adapters can only address within a 32-bit
5933a31b7ebSMike Smith      * span.
5943a31b7ebSMike Smith      */
5953a31b7ebSMike Smith     if (bus_dma_tag_create(NULL, 			/* parent */
5963a31b7ebSMike Smith 			   1, 0, 			/* alignment, boundary */
597639717c8SPaul Saab 			   BUS_SPACE_MAXADDR,		/* lowaddr */
5983a31b7ebSMike Smith 			   BUS_SPACE_MAXADDR, 		/* highaddr */
5993a31b7ebSMike Smith 			   NULL, NULL, 			/* filter, filterarg */
600639717c8SPaul Saab 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
601639717c8SPaul Saab 			   CISS_COMMAND_SG_LENGTH,	/* nsegments */
6023a31b7ebSMike Smith 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
6033a31b7ebSMike Smith 			   BUS_DMA_ALLOCNOW,		/* flags */
604f6b1c44dSScott Long 			   NULL, NULL,			/* lockfunc, lockarg */
6053a31b7ebSMike Smith 			   &sc->ciss_parent_dmat)) {
6063a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate parent DMA tag\n");
6073a31b7ebSMike Smith 	return(ENOMEM);
6083a31b7ebSMike Smith     }
6093a31b7ebSMike Smith 
6103a31b7ebSMike Smith     /*
6113a31b7ebSMike Smith      * Create DMA tag for mapping buffers into adapter-addressable
6123a31b7ebSMike Smith      * space.
6133a31b7ebSMike Smith      */
6143a31b7ebSMike Smith     if (bus_dma_tag_create(sc->ciss_parent_dmat, 	/* parent */
6153a31b7ebSMike Smith 			   1, 0, 			/* alignment, boundary */
6163a31b7ebSMike Smith 			   BUS_SPACE_MAXADDR,		/* lowaddr */
6173a31b7ebSMike Smith 			   BUS_SPACE_MAXADDR, 		/* highaddr */
6183a31b7ebSMike Smith 			   NULL, NULL, 			/* filter, filterarg */
6193a31b7ebSMike Smith 			   MAXBSIZE, CISS_COMMAND_SG_LENGTH,	/* maxsize, nsegments */
6203a31b7ebSMike Smith 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
6213a31b7ebSMike Smith 			   0,				/* flags */
622f6b1c44dSScott Long 			   busdma_lock_mutex, &Giant,	/* lockfunc, lockarg */
6233a31b7ebSMike Smith 			   &sc->ciss_buffer_dmat)) {
6243a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate buffer DMA tag\n");
6253a31b7ebSMike Smith 	return(ENOMEM);
6263a31b7ebSMike Smith     }
6273a31b7ebSMike Smith     return(0);
6283a31b7ebSMike Smith }
6293a31b7ebSMike Smith 
6303a31b7ebSMike Smith /************************************************************************
6313a31b7ebSMike Smith  * Wait for the adapter to come ready.
6323a31b7ebSMike Smith  */
6333a31b7ebSMike Smith static int
6343a31b7ebSMike Smith ciss_wait_adapter(struct ciss_softc *sc)
6353a31b7ebSMike Smith {
6363a31b7ebSMike Smith     int		i;
6373a31b7ebSMike Smith 
6383a31b7ebSMike Smith     debug_called(1);
6393a31b7ebSMike Smith 
6403a31b7ebSMike Smith     /*
6413a31b7ebSMike Smith      * Wait for the adapter to come ready.
6423a31b7ebSMike Smith      */
6433a31b7ebSMike Smith     if (!(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY)) {
6443a31b7ebSMike Smith 	ciss_printf(sc, "waiting for adapter to come ready...\n");
6453a31b7ebSMike Smith 	for (i = 0; !(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY); i++) {
6463a31b7ebSMike Smith 	    DELAY(1000000);	/* one second */
6473a31b7ebSMike Smith 	    if (i > 30) {
6483a31b7ebSMike Smith 		ciss_printf(sc, "timed out waiting for adapter to come ready\n");
6493a31b7ebSMike Smith 		return(EIO);
6503a31b7ebSMike Smith 	    }
6513a31b7ebSMike Smith 	}
6523a31b7ebSMike Smith     }
6533a31b7ebSMike Smith     return(0);
6543a31b7ebSMike Smith }
6553a31b7ebSMike Smith 
6563a31b7ebSMike Smith /************************************************************************
6573a31b7ebSMike Smith  * Flush the adapter cache.
6583a31b7ebSMike Smith  */
6593a31b7ebSMike Smith static int
6603a31b7ebSMike Smith ciss_flush_adapter(struct ciss_softc *sc)
6613a31b7ebSMike Smith {
6623a31b7ebSMike Smith     struct ciss_request			*cr;
6633a31b7ebSMike Smith     struct ciss_bmic_flush_cache	*cbfc;
6643a31b7ebSMike Smith     int					error, command_status;
6653a31b7ebSMike Smith 
6663a31b7ebSMike Smith     debug_called(1);
6673a31b7ebSMike Smith 
6683a31b7ebSMike Smith     cr = NULL;
6693a31b7ebSMike Smith     cbfc = NULL;
6703a31b7ebSMike Smith 
6713a31b7ebSMike Smith     /*
6723a31b7ebSMike Smith      * Build a BMIC request to flush the cache.  We don't disable
6733a31b7ebSMike Smith      * it, as we may be going to do more I/O (eg. we are emulating
6743a31b7ebSMike Smith      * the Synchronise Cache command).
6753a31b7ebSMike Smith      */
6763a31b7ebSMike Smith     if ((cbfc = malloc(sizeof(*cbfc), CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
6773a31b7ebSMike Smith 	error = ENOMEM;
6783a31b7ebSMike Smith 	goto out;
6793a31b7ebSMike Smith     }
6803a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_FLUSH_CACHE,
6813a31b7ebSMike Smith 				       (void **)&cbfc, sizeof(*cbfc))) != 0)
6823a31b7ebSMike Smith 	goto out;
6833a31b7ebSMike Smith 
6843a31b7ebSMike Smith     /*
6853a31b7ebSMike Smith      * Submit the request and wait for it to complete.
6863a31b7ebSMike Smith      */
6873a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
6883a31b7ebSMike Smith 	ciss_printf(sc, "error sending BMIC FLUSH_CACHE command (%d)\n", error);
6893a31b7ebSMike Smith 	goto out;
6903a31b7ebSMike Smith     }
6913a31b7ebSMike Smith 
6923a31b7ebSMike Smith     /*
6933a31b7ebSMike Smith      * Check response.
6943a31b7ebSMike Smith      */
6953a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
6963a31b7ebSMike Smith     switch(command_status) {
6973a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:
6983a31b7ebSMike Smith 	break;
6993a31b7ebSMike Smith     default:
7003a31b7ebSMike Smith 	ciss_printf(sc, "error flushing cache (%s)\n",
7013a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
7023a31b7ebSMike Smith 	error = EIO;
7033a31b7ebSMike Smith 	goto out;
7043a31b7ebSMike Smith     }
7053a31b7ebSMike Smith 
7063a31b7ebSMike Smith out:
7073a31b7ebSMike Smith     if (cbfc != NULL)
7083a31b7ebSMike Smith 	free(cbfc, CISS_MALLOC_CLASS);
7093a31b7ebSMike Smith     if (cr != NULL)
7103a31b7ebSMike Smith 	ciss_release_request(cr);
7113a31b7ebSMike Smith     return(error);
7123a31b7ebSMike Smith }
7133a31b7ebSMike Smith 
7143a31b7ebSMike Smith /************************************************************************
7153a31b7ebSMike Smith  * Allocate memory for the adapter command structures, initialise
7163a31b7ebSMike Smith  * the request structures.
7173a31b7ebSMike Smith  *
7183a31b7ebSMike Smith  * Note that the entire set of commands are allocated in a single
7193a31b7ebSMike Smith  * contiguous slab.
7203a31b7ebSMike Smith  */
7213a31b7ebSMike Smith static int
7223a31b7ebSMike Smith ciss_init_requests(struct ciss_softc *sc)
7233a31b7ebSMike Smith {
7243a31b7ebSMike Smith     struct ciss_request	*cr;
7253a31b7ebSMike Smith     int			i;
7263a31b7ebSMike Smith 
7273a31b7ebSMike Smith     debug_called(1);
7283a31b7ebSMike Smith 
7293a31b7ebSMike Smith     /*
7303a31b7ebSMike Smith      * Calculate the number of request structures/commands we are
7313a31b7ebSMike Smith      * going to provide for this adapter.
7323a31b7ebSMike Smith      */
7333a31b7ebSMike Smith     sc->ciss_max_requests = min(CISS_MAX_REQUESTS, sc->ciss_cfg->max_outstanding_commands);
7343a31b7ebSMike Smith 
7354bca277bSPaul Saab     if (bootverbose)
7363a31b7ebSMike Smith 	ciss_printf(sc, "using %d of %d available commands\n",
7373a31b7ebSMike Smith 		    sc->ciss_max_requests, sc->ciss_cfg->max_outstanding_commands);
7383a31b7ebSMike Smith 
7393a31b7ebSMike Smith     /*
7403a31b7ebSMike Smith      * Create the DMA tag for commands.
7413a31b7ebSMike Smith      */
7423a31b7ebSMike Smith     if (bus_dma_tag_create(sc->ciss_parent_dmat,	/* parent */
7433a31b7ebSMike Smith 			   1, 0, 			/* alignment, boundary */
744639717c8SPaul Saab 			   BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
7453a31b7ebSMike Smith 			   BUS_SPACE_MAXADDR, 		/* highaddr */
7463a31b7ebSMike Smith 			   NULL, NULL, 			/* filter, filterarg */
7473a31b7ebSMike Smith 			   CISS_COMMAND_ALLOC_SIZE *
7483a31b7ebSMike Smith 			   sc->ciss_max_requests, 1,	/* maxsize, nsegments */
7493a31b7ebSMike Smith 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
750639717c8SPaul Saab 			   BUS_DMA_ALLOCNOW,		/* flags */
751639717c8SPaul Saab 			   NULL, NULL,			/* lockfunc, lockarg */
7523a31b7ebSMike Smith 			   &sc->ciss_command_dmat)) {
7533a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate command DMA tag\n");
7543a31b7ebSMike Smith 	return(ENOMEM);
7553a31b7ebSMike Smith     }
7563a31b7ebSMike Smith     /*
7573a31b7ebSMike Smith      * Allocate memory and make it available for DMA.
7583a31b7ebSMike Smith      */
7593a31b7ebSMike Smith     if (bus_dmamem_alloc(sc->ciss_command_dmat, (void **)&sc->ciss_command,
7603a31b7ebSMike Smith 			 BUS_DMA_NOWAIT, &sc->ciss_command_map)) {
7613a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate command memory\n");
7623a31b7ebSMike Smith 	return(ENOMEM);
7633a31b7ebSMike Smith     }
7643a31b7ebSMike Smith     bus_dmamap_load(sc->ciss_command_dmat, sc->ciss_command_map, sc->ciss_command,
765be241f79SPaul Saab 		    CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests,
7663a31b7ebSMike Smith 		    ciss_command_map_helper, sc, 0);
7673a31b7ebSMike Smith     bzero(sc->ciss_command, CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests);
7683a31b7ebSMike Smith 
7693a31b7ebSMike Smith     /*
7703a31b7ebSMike Smith      * Set up the request and command structures, push requests onto
7713a31b7ebSMike Smith      * the free queue.
7723a31b7ebSMike Smith      */
7733a31b7ebSMike Smith     for (i = 1; i < sc->ciss_max_requests; i++) {
7743a31b7ebSMike Smith 	cr = &sc->ciss_request[i];
7753a31b7ebSMike Smith 	cr->cr_sc = sc;
7763a31b7ebSMike Smith 	cr->cr_tag = i;
7773284b9eeSPaul Saab 	bus_dmamap_create(sc->ciss_buffer_dmat, 0, &cr->cr_datamap);
7783a31b7ebSMike Smith 	ciss_enqueue_free(cr);
7793a31b7ebSMike Smith     }
7803a31b7ebSMike Smith     return(0);
7813a31b7ebSMike Smith }
7823a31b7ebSMike Smith 
7833a31b7ebSMike Smith static void
7843a31b7ebSMike Smith ciss_command_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
7853a31b7ebSMike Smith {
7863a31b7ebSMike Smith     struct ciss_softc	*sc = (struct ciss_softc *)arg;
7873a31b7ebSMike Smith 
7883a31b7ebSMike Smith     sc->ciss_command_phys = segs->ds_addr;
7893a31b7ebSMike Smith }
7903a31b7ebSMike Smith 
7913a31b7ebSMike Smith /************************************************************************
7923a31b7ebSMike Smith  * Identify the adapter, print some information about it.
7933a31b7ebSMike Smith  */
7943a31b7ebSMike Smith static int
7953a31b7ebSMike Smith ciss_identify_adapter(struct ciss_softc *sc)
7963a31b7ebSMike Smith {
7973a31b7ebSMike Smith     struct ciss_request	*cr;
7983a31b7ebSMike Smith     int			error, command_status;
7993a31b7ebSMike Smith 
8003a31b7ebSMike Smith     debug_called(1);
8013a31b7ebSMike Smith 
8023a31b7ebSMike Smith     cr = NULL;
8033a31b7ebSMike Smith 
8043a31b7ebSMike Smith     /*
8053a31b7ebSMike Smith      * Get a request, allocate storage for the adapter data.
8063a31b7ebSMike Smith      */
8073a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_CTLR,
8083a31b7ebSMike Smith 				       (void **)&sc->ciss_id,
8093a31b7ebSMike Smith 				       sizeof(*sc->ciss_id))) != 0)
8103a31b7ebSMike Smith 	goto out;
8113a31b7ebSMike Smith 
8123a31b7ebSMike Smith     /*
8133a31b7ebSMike Smith      * Submit the request and wait for it to complete.
8143a31b7ebSMike Smith      */
8153a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
8163a31b7ebSMike Smith 	ciss_printf(sc, "error sending BMIC ID_CTLR command (%d)\n", error);
8173a31b7ebSMike Smith 	goto out;
8183a31b7ebSMike Smith     }
8193a31b7ebSMike Smith 
8203a31b7ebSMike Smith     /*
8213a31b7ebSMike Smith      * Check response.
8223a31b7ebSMike Smith      */
8233a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
8243a31b7ebSMike Smith     switch(command_status) {
8253a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
8263a31b7ebSMike Smith 	break;
8273a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_UNDERRUN:
8283a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_OVERRUN:
8293a31b7ebSMike Smith 	ciss_printf(sc, "data over/underrun reading adapter information\n");
8303a31b7ebSMike Smith     default:
8313a31b7ebSMike Smith 	ciss_printf(sc, "error reading adapter information (%s)\n",
8323a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
8333a31b7ebSMike Smith 	error = EIO;
8343a31b7ebSMike Smith 	goto out;
8353a31b7ebSMike Smith     }
8363a31b7ebSMike Smith 
8373a31b7ebSMike Smith     /* sanity-check reply */
8383a31b7ebSMike Smith     if (!sc->ciss_id->big_map_supported) {
8393a31b7ebSMike Smith 	ciss_printf(sc, "adapter does not support BIG_MAP\n");
8403a31b7ebSMike Smith 	error = ENXIO;
8413a31b7ebSMike Smith 	goto out;
8423a31b7ebSMike Smith     }
8433a31b7ebSMike Smith 
844d4aa427fSPaul Saab #if 0
8453a31b7ebSMike Smith     /* XXX later revisions may not need this */
8463a31b7ebSMike Smith     sc->ciss_flags |= CISS_FLAG_FAKE_SYNCH;
847d4aa427fSPaul Saab #endif
8483a31b7ebSMike Smith 
8493a31b7ebSMike Smith     /* XXX only really required for old 5300 adapters? */
8503a31b7ebSMike Smith     sc->ciss_flags |= CISS_FLAG_BMIC_ABORT;
8513a31b7ebSMike Smith 
8523a31b7ebSMike Smith     /* print information */
8534bca277bSPaul Saab     if (bootverbose) {
8543a31b7ebSMike Smith 	ciss_printf(sc, "  %d logical drive%s configured\n",
8553a31b7ebSMike Smith 		    sc->ciss_id->configured_logical_drives,
8563a31b7ebSMike Smith 		    (sc->ciss_id->configured_logical_drives == 1) ? "" : "s");
8573a31b7ebSMike Smith 	ciss_printf(sc, "  firmware %4.4s\n", sc->ciss_id->running_firmware_revision);
8583a31b7ebSMike Smith 	ciss_printf(sc, "  %d SCSI channels\n", sc->ciss_id->scsi_bus_count);
8593a31b7ebSMike Smith 
8603a31b7ebSMike Smith 	ciss_printf(sc, "  signature '%.4s'\n", sc->ciss_cfg->signature);
8613a31b7ebSMike Smith 	ciss_printf(sc, "  valence %d\n", sc->ciss_cfg->valence);
8623a31b7ebSMike Smith 	ciss_printf(sc, "  supported I/O methods 0x%b\n",
8633a31b7ebSMike Smith 		    sc->ciss_cfg->supported_methods,
8643a31b7ebSMike Smith 		    "\20\1READY\2simple\3performant\4MEMQ\n");
8653a31b7ebSMike Smith 	ciss_printf(sc, "  active I/O method 0x%b\n",
8663a31b7ebSMike Smith 		    sc->ciss_cfg->active_method, "\20\2simple\3performant\4MEMQ\n");
8673a31b7ebSMike Smith 	ciss_printf(sc, "  4G page base 0x%08x\n",
8683a31b7ebSMike Smith 		    sc->ciss_cfg->command_physlimit);
8693a31b7ebSMike Smith 	ciss_printf(sc, "  interrupt coalesce delay %dus\n",
8703a31b7ebSMike Smith 		    sc->ciss_cfg->interrupt_coalesce_delay);
8713a31b7ebSMike Smith 	ciss_printf(sc, "  interrupt coalesce count %d\n",
8723a31b7ebSMike Smith 		    sc->ciss_cfg->interrupt_coalesce_count);
8733a31b7ebSMike Smith 	ciss_printf(sc, "  max outstanding commands %d\n",
8743a31b7ebSMike Smith 		    sc->ciss_cfg->max_outstanding_commands);
8753a31b7ebSMike Smith 	ciss_printf(sc, "  bus types 0x%b\n", sc->ciss_cfg->bus_types,
8763a31b7ebSMike Smith 		    "\20\1ultra2\2ultra3\10fibre1\11fibre2\n");
8773a31b7ebSMike Smith 	ciss_printf(sc, "  server name '%.16s'\n", sc->ciss_cfg->server_name);
8783a31b7ebSMike Smith 	ciss_printf(sc, "  heartbeat 0x%x\n", sc->ciss_cfg->heartbeat);
8793a31b7ebSMike Smith     }
8803a31b7ebSMike Smith 
8813a31b7ebSMike Smith out:
8823a31b7ebSMike Smith     if (error) {
8833a31b7ebSMike Smith 	if (sc->ciss_id != NULL) {
8843a31b7ebSMike Smith 	    free(sc->ciss_id, CISS_MALLOC_CLASS);
8853a31b7ebSMike Smith 	    sc->ciss_id = NULL;
8863a31b7ebSMike Smith 	}
8873a31b7ebSMike Smith     }
8883a31b7ebSMike Smith     if (cr != NULL)
8893a31b7ebSMike Smith 	ciss_release_request(cr);
8903a31b7ebSMike Smith     return(error);
8913a31b7ebSMike Smith }
8923a31b7ebSMike Smith 
8933a31b7ebSMike Smith /************************************************************************
8943a31b7ebSMike Smith  * Find logical drives on the adapter.
8953a31b7ebSMike Smith  */
8963a31b7ebSMike Smith static int
8973a31b7ebSMike Smith ciss_init_logical(struct ciss_softc *sc)
8983a31b7ebSMike Smith {
8993a31b7ebSMike Smith     struct ciss_request		*cr;
9003a31b7ebSMike Smith     struct ciss_command		*cc;
9013a31b7ebSMike Smith     struct ciss_report_cdb	*crc;
9023a31b7ebSMike Smith     struct ciss_lun_report	*cll;
9033a31b7ebSMike Smith     int				error, i;
9043a31b7ebSMike Smith     size_t			report_size;
9053a31b7ebSMike Smith     int				ndrives;
9063a31b7ebSMike Smith     int				command_status;
9073a31b7ebSMike Smith 
9083a31b7ebSMike Smith     debug_called(1);
9093a31b7ebSMike Smith 
9103a31b7ebSMike Smith     cr = NULL;
9113a31b7ebSMike Smith     cll = NULL;
9123a31b7ebSMike Smith 
9133a31b7ebSMike Smith     /*
9143a31b7ebSMike Smith      * Get a request, allocate storage for the address list.
9153a31b7ebSMike Smith      */
9163a31b7ebSMike Smith     if ((error = ciss_get_request(sc, &cr)) != 0)
9173a31b7ebSMike Smith 	goto out;
9183a31b7ebSMike Smith     report_size = sizeof(*cll) + CISS_MAX_LOGICAL * sizeof(union ciss_device_address);
9193a31b7ebSMike Smith     if ((cll = malloc(report_size, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
9203a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate memory for logical drive list\n");
9213a31b7ebSMike Smith 	error = ENOMEM;
9223a31b7ebSMike Smith 	goto out;
9233a31b7ebSMike Smith     }
9243a31b7ebSMike Smith 
9253a31b7ebSMike Smith     /*
9263a31b7ebSMike Smith      * Build the Report Logical LUNs command.
9273a31b7ebSMike Smith      */
9283a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
9293a31b7ebSMike Smith     cr->cr_data = cll;
9303a31b7ebSMike Smith     cr->cr_length = report_size;
9313a31b7ebSMike Smith     cr->cr_flags = CISS_REQ_DATAIN;
9323a31b7ebSMike Smith 
9333a31b7ebSMike Smith     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
9343a31b7ebSMike Smith     cc->header.address.physical.bus = 0;
9353a31b7ebSMike Smith     cc->header.address.physical.target = 0;
9363a31b7ebSMike Smith     cc->cdb.cdb_length = sizeof(*crc);
9373a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
9383a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
9393a31b7ebSMike Smith     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
9403a31b7ebSMike Smith     cc->cdb.timeout = 30;	/* XXX better suggestions? */
9413a31b7ebSMike Smith 
9423a31b7ebSMike Smith     crc = (struct ciss_report_cdb *)&(cc->cdb.cdb[0]);
9433a31b7ebSMike Smith     bzero(crc, sizeof(*crc));
9443a31b7ebSMike Smith     crc->opcode = CISS_OPCODE_REPORT_LOGICAL_LUNS;
9453a31b7ebSMike Smith     crc->length = htonl(report_size);			/* big-endian field */
9463a31b7ebSMike Smith     cll->list_size = htonl(report_size - sizeof(*cll));	/* big-endian field */
9473a31b7ebSMike Smith 
9483a31b7ebSMike Smith     /*
9493a31b7ebSMike Smith      * Submit the request and wait for it to complete.  (timeout
9503a31b7ebSMike Smith      * here should be much greater than above)
9513a31b7ebSMike Smith      */
9523a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
9533a31b7ebSMike Smith 	ciss_printf(sc, "error sending Report Logical LUNs command (%d)\n", error);
9543a31b7ebSMike Smith 	goto out;
9553a31b7ebSMike Smith     }
9563a31b7ebSMike Smith 
9573a31b7ebSMike Smith     /*
9583a31b7ebSMike Smith      * Check response.  Note that data over/underrun is OK.
9593a31b7ebSMike Smith      */
9603a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
9613a31b7ebSMike Smith     switch(command_status) {
9623a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:	/* buffer right size */
9633a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_UNDERRUN:	/* buffer too large, not bad */
9643a31b7ebSMike Smith 	break;
9653a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_OVERRUN:
9663a31b7ebSMike Smith 	ciss_printf(sc, "WARNING: more logical drives than driver limit (%d), adjust CISS_MAX_LOGICAL\n",
9673a31b7ebSMike Smith 		    CISS_MAX_LOGICAL);
9683a31b7ebSMike Smith 	break;
9693a31b7ebSMike Smith     default:
9703a31b7ebSMike Smith 	ciss_printf(sc, "error detecting logical drive configuration (%s)\n",
9713a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
9723a31b7ebSMike Smith 	error = EIO;
9733a31b7ebSMike Smith 	goto out;
9743a31b7ebSMike Smith     }
9753a31b7ebSMike Smith     ciss_release_request(cr);
9763a31b7ebSMike Smith     cr = NULL;
9773a31b7ebSMike Smith 
9783a31b7ebSMike Smith     /* sanity-check reply */
9793a31b7ebSMike Smith     ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
980777d1b39SPoul-Henning Kamp     if ((ndrives < 0) || (ndrives >= CISS_MAX_LOGICAL)) {
9813a31b7ebSMike Smith 	ciss_printf(sc, "adapter claims to report absurd number of logical drives (%d > %d)\n",
9823a31b7ebSMike Smith 		    ndrives, CISS_MAX_LOGICAL);
9833a31b7ebSMike Smith 	return(ENXIO);
9843a31b7ebSMike Smith     }
9853a31b7ebSMike Smith 
9863a31b7ebSMike Smith     /*
9873a31b7ebSMike Smith      * Save logical drive information.
9883a31b7ebSMike Smith      */
9894bca277bSPaul Saab     if (bootverbose)
9903a31b7ebSMike Smith 	ciss_printf(sc, "%d logical drive%s\n", ndrives, (ndrives > 1) ? "s" : "");
9913a31b7ebSMike Smith     if (ndrives != sc->ciss_id->configured_logical_drives)
9923a31b7ebSMike Smith 	ciss_printf(sc, "logical drive map claims %d drives, but adapter claims %d\n",
9933a31b7ebSMike Smith 		    ndrives, sc->ciss_id->configured_logical_drives);
9943a31b7ebSMike Smith     for (i = 0; i < CISS_MAX_LOGICAL; i++) {
9953a31b7ebSMike Smith 	if (i < ndrives) {
9963a31b7ebSMike Smith 	    sc->ciss_logical[i].cl_address = cll->lun[i];	/* XXX endianness? */
9973a31b7ebSMike Smith 	    if (ciss_identify_logical(sc, &sc->ciss_logical[i]) != 0)
9983a31b7ebSMike Smith 		continue;
9993a31b7ebSMike Smith 	    /*
10003a31b7ebSMike Smith 	     * If the drive has had media exchanged, we should bring it online.
10013a31b7ebSMike Smith 	     */
10023a31b7ebSMike Smith 	    if (sc->ciss_logical[i].cl_lstatus->media_exchanged)
10033a31b7ebSMike Smith 		ciss_accept_media(sc, i, 0);
10043a31b7ebSMike Smith 
10053a31b7ebSMike Smith 	} else {
10063a31b7ebSMike Smith 	    sc->ciss_logical[i].cl_status = CISS_LD_NONEXISTENT;
10073a31b7ebSMike Smith 	}
10083a31b7ebSMike Smith     }
10093a31b7ebSMike Smith     error = 0;
10103a31b7ebSMike Smith 
10113a31b7ebSMike Smith  out:
10123a31b7ebSMike Smith     /*
10133a31b7ebSMike Smith      * Note that if the error is a timeout, we are taking a slight
10143a31b7ebSMike Smith      * risk here and assuming that the adapter will not respond at a
10153a31b7ebSMike Smith      * later time, scribbling over host memory.
10163a31b7ebSMike Smith      */
10173a31b7ebSMike Smith     if (cr != NULL)
10183a31b7ebSMike Smith 	ciss_release_request(cr);
10193a31b7ebSMike Smith     if (cll != NULL)
10203a31b7ebSMike Smith 	free(cll, CISS_MALLOC_CLASS);
10213a31b7ebSMike Smith     return(error);
10223a31b7ebSMike Smith }
10233a31b7ebSMike Smith 
1024440baa08SPaul Saab static int
1025440baa08SPaul Saab ciss_inquiry_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1026440baa08SPaul Saab {
1027440baa08SPaul Saab     struct ciss_request			*cr;
1028440baa08SPaul Saab     struct ciss_command			*cc;
1029440baa08SPaul Saab     struct scsi_inquiry			*inq;
1030440baa08SPaul Saab     int					error;
1031440baa08SPaul Saab     int					command_status;
1032440baa08SPaul Saab     int					lun;
1033440baa08SPaul Saab 
1034440baa08SPaul Saab     cr = NULL;
1035440baa08SPaul Saab     lun = ld->cl_address.logical.lun;
1036440baa08SPaul Saab 
1037440baa08SPaul Saab     bzero(&ld->cl_geometry, sizeof(ld->cl_geometry));
1038440baa08SPaul Saab 
1039440baa08SPaul Saab     if ((error = ciss_get_request(sc, &cr)) != 0)
1040440baa08SPaul Saab 	goto out;
1041440baa08SPaul Saab 
1042440baa08SPaul Saab     cc = CISS_FIND_COMMAND(cr);
1043440baa08SPaul Saab     cr->cr_data = &ld->cl_geometry;
1044440baa08SPaul Saab     cr->cr_length = sizeof(ld->cl_geometry);
1045440baa08SPaul Saab     cr->cr_flags = CISS_REQ_DATAIN;
1046440baa08SPaul Saab 
1047440baa08SPaul Saab     cc->header.address.logical.mode = CISS_HDR_ADDRESS_MODE_LOGICAL;
1048440baa08SPaul Saab     cc->header.address.logical.lun  = lun;
1049440baa08SPaul Saab     cc->cdb.cdb_length = 6;
1050440baa08SPaul Saab     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1051440baa08SPaul Saab     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1052440baa08SPaul Saab     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1053440baa08SPaul Saab     cc->cdb.timeout = 30;
1054440baa08SPaul Saab 
1055440baa08SPaul Saab     inq = (struct scsi_inquiry *)&(cc->cdb.cdb[0]);
1056440baa08SPaul Saab     inq->opcode = INQUIRY;
1057440baa08SPaul Saab     inq->byte2 = SI_EVPD;
1058440baa08SPaul Saab     inq->page_code = CISS_VPD_LOGICAL_DRIVE_GEOMETRY;
1059440baa08SPaul Saab     inq->length = sizeof(ld->cl_geometry);
1060440baa08SPaul Saab 
1061440baa08SPaul Saab     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1062440baa08SPaul Saab 	ciss_printf(sc, "error getting geometry (%d)\n", error);
1063440baa08SPaul Saab 	goto out;
1064440baa08SPaul Saab     }
1065440baa08SPaul Saab 
1066440baa08SPaul Saab     ciss_report_request(cr, &command_status, NULL);
1067440baa08SPaul Saab     switch(command_status) {
1068440baa08SPaul Saab     case CISS_CMD_STATUS_SUCCESS:
1069440baa08SPaul Saab     case CISS_CMD_STATUS_DATA_UNDERRUN:
1070440baa08SPaul Saab 	break;
1071440baa08SPaul Saab     case CISS_CMD_STATUS_DATA_OVERRUN:
1072440baa08SPaul Saab 	ciss_printf(sc, "WARNING: Data overrun\n");
1073440baa08SPaul Saab 	break;
1074440baa08SPaul Saab     default:
1075440baa08SPaul Saab 	ciss_printf(sc, "Error detecting logical drive geometry (%s)\n",
1076440baa08SPaul Saab 		    ciss_name_command_status(command_status));
1077440baa08SPaul Saab 	break;
1078440baa08SPaul Saab     }
1079440baa08SPaul Saab 
1080440baa08SPaul Saab out:
1081440baa08SPaul Saab     if (cr != NULL)
1082440baa08SPaul Saab 	ciss_release_request(cr);
1083440baa08SPaul Saab     return(error);
1084440baa08SPaul Saab }
10853a31b7ebSMike Smith /************************************************************************
10863a31b7ebSMike Smith  * Identify a logical drive, initialise state related to it.
10873a31b7ebSMike Smith  */
10883a31b7ebSMike Smith static int
10893a31b7ebSMike Smith ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
10903a31b7ebSMike Smith {
10913a31b7ebSMike Smith     struct ciss_request		*cr;
10923a31b7ebSMike Smith     struct ciss_command		*cc;
10933a31b7ebSMike Smith     struct ciss_bmic_cdb	*cbc;
10943a31b7ebSMike Smith     int				error, command_status;
10953a31b7ebSMike Smith 
10963a31b7ebSMike Smith     debug_called(1);
10973a31b7ebSMike Smith 
10983a31b7ebSMike Smith     cr = NULL;
10993a31b7ebSMike Smith 
11003a31b7ebSMike Smith     /*
11013a31b7ebSMike Smith      * Build a BMIC request to fetch the drive ID.
11023a31b7ebSMike Smith      */
11033a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LDRIVE,
11043a31b7ebSMike Smith 				       (void **)&ld->cl_ldrive,
11053a31b7ebSMike Smith 				       sizeof(*ld->cl_ldrive))) != 0)
11063a31b7ebSMike Smith 	goto out;
11073a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
11083a31b7ebSMike Smith     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
11093a31b7ebSMike Smith     cbc->log_drive = ld->cl_address.logical.lun;
11103a31b7ebSMike Smith 
11113a31b7ebSMike Smith     /*
11123a31b7ebSMike Smith      * Submit the request and wait for it to complete.
11133a31b7ebSMike Smith      */
11143a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
11153a31b7ebSMike Smith 	ciss_printf(sc, "error sending BMIC LDRIVE command (%d)\n", error);
11163a31b7ebSMike Smith 	goto out;
11173a31b7ebSMike Smith     }
11183a31b7ebSMike Smith 
11193a31b7ebSMike Smith     /*
11203a31b7ebSMike Smith      * Check response.
11213a31b7ebSMike Smith      */
11223a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
11233a31b7ebSMike Smith     switch(command_status) {
11243a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
11253a31b7ebSMike Smith 	break;
11263a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_UNDERRUN:
11273a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_OVERRUN:
11283a31b7ebSMike Smith 	ciss_printf(sc, "data over/underrun reading logical drive ID\n");
11293a31b7ebSMike Smith     default:
11303a31b7ebSMike Smith 	ciss_printf(sc, "error reading logical drive ID (%s)\n",
11313a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
11323a31b7ebSMike Smith 	error = EIO;
11333a31b7ebSMike Smith 	goto out;
11343a31b7ebSMike Smith     }
11353a31b7ebSMike Smith     ciss_release_request(cr);
11363a31b7ebSMike Smith     cr = NULL;
11373a31b7ebSMike Smith 
11383a31b7ebSMike Smith     /*
11393a31b7ebSMike Smith      * Build a CISS BMIC command to get the logical drive status.
11403a31b7ebSMike Smith      */
11413a31b7ebSMike Smith     if ((error = ciss_get_ldrive_status(sc, ld)) != 0)
11423a31b7ebSMike Smith 	goto out;
11433a31b7ebSMike Smith 
11443a31b7ebSMike Smith     /*
1145440baa08SPaul Saab      * Get the logical drive geometry.
1146440baa08SPaul Saab      */
1147440baa08SPaul Saab     if ((error = ciss_inquiry_logical(sc, ld)) != 0)
1148440baa08SPaul Saab 	goto out;
1149440baa08SPaul Saab 
1150440baa08SPaul Saab     /*
11513a31b7ebSMike Smith      * Print the drive's basic characteristics.
11523a31b7ebSMike Smith      */
11534bca277bSPaul Saab     if (bootverbose) {
11543a31b7ebSMike Smith 	ciss_printf(sc, "logical drive %d: %s, %dMB ",
11553a31b7ebSMike Smith 		    cbc->log_drive, ciss_name_ldrive_org(ld->cl_ldrive->fault_tolerance),
11563a31b7ebSMike Smith 		    ((ld->cl_ldrive->blocks_available / (1024 * 1024)) *
11573a31b7ebSMike Smith 		     ld->cl_ldrive->block_size));
11583a31b7ebSMike Smith 
11593a31b7ebSMike Smith 	ciss_print_ldrive(sc, ld);
11603a31b7ebSMike Smith     }
11613a31b7ebSMike Smith out:
11623a31b7ebSMike Smith     if (error != 0) {
11633a31b7ebSMike Smith 	/* make the drive not-exist */
11643a31b7ebSMike Smith 	ld->cl_status = CISS_LD_NONEXISTENT;
11653a31b7ebSMike Smith 	if (ld->cl_ldrive != NULL) {
11663a31b7ebSMike Smith 	    free(ld->cl_ldrive, CISS_MALLOC_CLASS);
11673a31b7ebSMike Smith 	    ld->cl_ldrive = NULL;
11683a31b7ebSMike Smith 	}
11693a31b7ebSMike Smith 	if (ld->cl_lstatus != NULL) {
11703a31b7ebSMike Smith 	    free(ld->cl_lstatus, CISS_MALLOC_CLASS);
11713a31b7ebSMike Smith 	    ld->cl_lstatus = NULL;
11723a31b7ebSMike Smith 	}
11733a31b7ebSMike Smith     }
11743a31b7ebSMike Smith     if (cr != NULL)
11753a31b7ebSMike Smith 	ciss_release_request(cr);
11763a31b7ebSMike Smith 
11773a31b7ebSMike Smith     return(error);
11783a31b7ebSMike Smith }
11793a31b7ebSMike Smith 
11803a31b7ebSMike Smith /************************************************************************
11813a31b7ebSMike Smith  * Get status for a logical drive.
11823a31b7ebSMike Smith  *
11833a31b7ebSMike Smith  * XXX should we also do this in response to Test Unit Ready?
11843a31b7ebSMike Smith  */
11853a31b7ebSMike Smith static int
11863a31b7ebSMike Smith ciss_get_ldrive_status(struct ciss_softc *sc,  struct ciss_ldrive *ld)
11873a31b7ebSMike Smith {
11883a31b7ebSMike Smith     struct ciss_request		*cr;
11893a31b7ebSMike Smith     struct ciss_command		*cc;
11903a31b7ebSMike Smith     struct ciss_bmic_cdb	*cbc;
11913a31b7ebSMike Smith     int				error, command_status;
11923a31b7ebSMike Smith 
11933a31b7ebSMike Smith     /*
11943a31b7ebSMike Smith      * Build a CISS BMIC command to get the logical drive status.
11953a31b7ebSMike Smith      */
11963a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LSTATUS,
11973a31b7ebSMike Smith 				       (void **)&ld->cl_lstatus,
11983a31b7ebSMike Smith 				       sizeof(*ld->cl_lstatus))) != 0)
11993a31b7ebSMike Smith 	goto out;
12003a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
12013a31b7ebSMike Smith     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
12023a31b7ebSMike Smith     cbc->log_drive = ld->cl_address.logical.lun;
12033a31b7ebSMike Smith 
12043a31b7ebSMike Smith     /*
12053a31b7ebSMike Smith      * Submit the request and wait for it to complete.
12063a31b7ebSMike Smith      */
12073a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
12083a31b7ebSMike Smith 	ciss_printf(sc, "error sending BMIC LSTATUS command (%d)\n", error);
12093a31b7ebSMike Smith 	goto out;
12103a31b7ebSMike Smith     }
12113a31b7ebSMike Smith 
12123a31b7ebSMike Smith     /*
12133a31b7ebSMike Smith      * Check response.
12143a31b7ebSMike Smith      */
12153a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
12163a31b7ebSMike Smith     switch(command_status) {
12173a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
12183a31b7ebSMike Smith 	break;
12193a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_UNDERRUN:
12203a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_OVERRUN:
12213a31b7ebSMike Smith 	ciss_printf(sc, "data over/underrun reading logical drive status\n");
12223a31b7ebSMike Smith     default:
12233a31b7ebSMike Smith 	ciss_printf(sc, "error reading logical drive status (%s)\n",
12243a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
12253a31b7ebSMike Smith 	error = EIO;
12263a31b7ebSMike Smith 	goto out;
12273a31b7ebSMike Smith     }
12283a31b7ebSMike Smith 
12293a31b7ebSMike Smith     /*
12303a31b7ebSMike Smith      * Set the drive's summary status based on the returned status.
12313a31b7ebSMike Smith      *
12323a31b7ebSMike Smith      * XXX testing shows that a failed JBOD drive comes back at next
12333a31b7ebSMike Smith      * boot in "queued for expansion" mode.  WTF?
12343a31b7ebSMike Smith      */
12353a31b7ebSMike Smith     ld->cl_status = ciss_decode_ldrive_status(ld->cl_lstatus->status);
12363a31b7ebSMike Smith 
12373a31b7ebSMike Smith out:
12383a31b7ebSMike Smith     if (cr != NULL)
12393a31b7ebSMike Smith 	ciss_release_request(cr);
12403a31b7ebSMike Smith     return(error);
12413a31b7ebSMike Smith }
12423a31b7ebSMike Smith 
12433a31b7ebSMike Smith /************************************************************************
12443a31b7ebSMike Smith  * Notify the adapter of a config update.
12453a31b7ebSMike Smith  */
12463a31b7ebSMike Smith static int
12473a31b7ebSMike Smith ciss_update_config(struct ciss_softc *sc)
12483a31b7ebSMike Smith {
12493a31b7ebSMike Smith     int		i;
12503a31b7ebSMike Smith 
12513a31b7ebSMike Smith     debug_called(1);
12523a31b7ebSMike Smith 
12533a31b7ebSMike Smith     CISS_TL_SIMPLE_WRITE(sc, CISS_TL_SIMPLE_IDBR, CISS_TL_SIMPLE_IDBR_CFG_TABLE);
12543a31b7ebSMike Smith     for (i = 0; i < 1000; i++) {
12553a31b7ebSMike Smith 	if (!(CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR) &
12563a31b7ebSMike Smith 	      CISS_TL_SIMPLE_IDBR_CFG_TABLE)) {
12573a31b7ebSMike Smith 	    return(0);
12583a31b7ebSMike Smith 	}
12593a31b7ebSMike Smith 	DELAY(1000);
12603a31b7ebSMike Smith     }
12613a31b7ebSMike Smith     return(1);
12623a31b7ebSMike Smith }
12633a31b7ebSMike Smith 
12643a31b7ebSMike Smith /************************************************************************
12653a31b7ebSMike Smith  * Accept new media into a logical drive.
12663a31b7ebSMike Smith  *
12673a31b7ebSMike Smith  * XXX The drive has previously been offline; it would be good if we
12683a31b7ebSMike Smith  *     could make sure it's not open right now.
12693a31b7ebSMike Smith  */
12703a31b7ebSMike Smith static int
12713a31b7ebSMike Smith ciss_accept_media(struct ciss_softc *sc, int ldrive, int async)
12723a31b7ebSMike Smith {
12733a31b7ebSMike Smith     struct ciss_request		*cr;
12743a31b7ebSMike Smith     struct ciss_command		*cc;
12753a31b7ebSMike Smith     struct ciss_bmic_cdb	*cbc;
12763a31b7ebSMike Smith     int				error;
12773a31b7ebSMike Smith 
12783a31b7ebSMike Smith     debug(0, "bringing logical drive %d back online %ssynchronously",
12793a31b7ebSMike Smith 	  ldrive, async ? "a" : "");
12803a31b7ebSMike Smith 
12813a31b7ebSMike Smith     /*
12823a31b7ebSMike Smith      * Build a CISS BMIC command to bring the drive back online.
12833a31b7ebSMike Smith      */
12843a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ACCEPT_MEDIA,
12853a31b7ebSMike Smith 				       NULL, 0)) != 0)
12863a31b7ebSMike Smith 	goto out;
12873a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
12883a31b7ebSMike Smith     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
12893a31b7ebSMike Smith     cbc->log_drive = ldrive;
12903a31b7ebSMike Smith 
12913a31b7ebSMike Smith     /*
12923a31b7ebSMike Smith      * Dispatch the request asynchronously if we can't sleep waiting
12933a31b7ebSMike Smith      * for it to complete.
12943a31b7ebSMike Smith      */
12953a31b7ebSMike Smith     if (async) {
12963a31b7ebSMike Smith 	cr->cr_complete = ciss_accept_media_complete;
12973a31b7ebSMike Smith 	if ((error = ciss_start(cr)) != 0)
12983a31b7ebSMike Smith 	    goto out;
12993a31b7ebSMike Smith 	return(0);
13003a31b7ebSMike Smith     } else {
13013a31b7ebSMike Smith 	/*
13023a31b7ebSMike Smith 	 * Submit the request and wait for it to complete.
13033a31b7ebSMike Smith 	 */
13043a31b7ebSMike Smith 	if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
13053a31b7ebSMike Smith 	    ciss_printf(sc, "error sending BMIC LSTATUS command (%d)\n", error);
13063a31b7ebSMike Smith 	    goto out;
13073a31b7ebSMike Smith 	}
13083a31b7ebSMike Smith     }
13093a31b7ebSMike Smith 
13103a31b7ebSMike Smith     /*
13113a31b7ebSMike Smith      * Call the completion callback manually.
13123a31b7ebSMike Smith      */
13133a31b7ebSMike Smith     ciss_accept_media_complete(cr);
13143a31b7ebSMike Smith     return(0);
13153a31b7ebSMike Smith 
13163a31b7ebSMike Smith out:
13173a31b7ebSMike Smith     if (cr != NULL)
13183a31b7ebSMike Smith 	ciss_release_request(cr);
13193a31b7ebSMike Smith     return(error);
13203a31b7ebSMike Smith }
13213a31b7ebSMike Smith 
13223a31b7ebSMike Smith static void
13233a31b7ebSMike Smith ciss_accept_media_complete(struct ciss_request *cr)
13243a31b7ebSMike Smith {
13253a31b7ebSMike Smith     int				command_status;
13263a31b7ebSMike Smith 
13273a31b7ebSMike Smith     /*
13283a31b7ebSMike Smith      * Check response.
13293a31b7ebSMike Smith      */
13303a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
13313a31b7ebSMike Smith     switch(command_status) {
13323a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:		/* all OK */
13333a31b7ebSMike Smith 	/* we should get a logical drive status changed event here */
13343a31b7ebSMike Smith 	break;
13353a31b7ebSMike Smith     default:
13363a31b7ebSMike Smith 	ciss_printf(cr->cr_sc, "error accepting media into failed logical drive (%s)\n",
13373a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
13383a31b7ebSMike Smith 	break;
13393a31b7ebSMike Smith     }
13403a31b7ebSMike Smith     ciss_release_request(cr);
13413a31b7ebSMike Smith }
13423a31b7ebSMike Smith 
13433a31b7ebSMike Smith /************************************************************************
13443a31b7ebSMike Smith  * Release adapter resources.
13453a31b7ebSMike Smith  */
13463a31b7ebSMike Smith static void
13473a31b7ebSMike Smith ciss_free(struct ciss_softc *sc)
13483a31b7ebSMike Smith {
13493284b9eeSPaul Saab     struct ciss_request *cr;
13503284b9eeSPaul Saab 
13513a31b7ebSMike Smith     debug_called(1);
13523a31b7ebSMike Smith 
13533a31b7ebSMike Smith     /* we're going away */
13543a31b7ebSMike Smith     sc->ciss_flags |= CISS_FLAG_ABORTING;
13553a31b7ebSMike Smith 
13563a31b7ebSMike Smith     /* terminate the periodic heartbeat routine */
13573a31b7ebSMike Smith     untimeout(ciss_periodic, sc, sc->ciss_periodic);
13583a31b7ebSMike Smith 
13593a31b7ebSMike Smith     /* cancel the Event Notify chain */
13603a31b7ebSMike Smith     ciss_notify_abort(sc);
13613a31b7ebSMike Smith 
13623a31b7ebSMike Smith     /* free the controller data */
13633a31b7ebSMike Smith     if (sc->ciss_id != NULL)
13643a31b7ebSMike Smith 	free(sc->ciss_id, CISS_MALLOC_CLASS);
13653a31b7ebSMike Smith 
13663a31b7ebSMike Smith     /* release I/O resources */
13673a31b7ebSMike Smith     if (sc->ciss_regs_resource != NULL)
13683a31b7ebSMike Smith 	bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
13693a31b7ebSMike Smith 			     sc->ciss_regs_rid, sc->ciss_regs_resource);
13703a31b7ebSMike Smith     if (sc->ciss_cfg_resource != NULL)
13713a31b7ebSMike Smith 	bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
13723a31b7ebSMike Smith 			     sc->ciss_cfg_rid, sc->ciss_cfg_resource);
13733a31b7ebSMike Smith     if (sc->ciss_intr != NULL)
13743a31b7ebSMike Smith 	bus_teardown_intr(sc->ciss_dev, sc->ciss_irq_resource, sc->ciss_intr);
13753a31b7ebSMike Smith     if (sc->ciss_irq_resource != NULL)
13763a31b7ebSMike Smith 	bus_release_resource(sc->ciss_dev, SYS_RES_IRQ,
13773a31b7ebSMike Smith 			     sc->ciss_irq_rid, sc->ciss_irq_resource);
13783a31b7ebSMike Smith 
13793a31b7ebSMike Smith     /* destroy DMA tags */
13803a31b7ebSMike Smith     if (sc->ciss_parent_dmat)
13813a31b7ebSMike Smith 	bus_dma_tag_destroy(sc->ciss_parent_dmat);
13823284b9eeSPaul Saab 
13833284b9eeSPaul Saab     while ((cr = ciss_dequeue_free(sc)) != NULL)
13843284b9eeSPaul Saab 	bus_dmamap_destroy(sc->ciss_buffer_dmat, cr->cr_datamap);
13853a31b7ebSMike Smith     if (sc->ciss_buffer_dmat)
13863a31b7ebSMike Smith 	bus_dma_tag_destroy(sc->ciss_buffer_dmat);
13873a31b7ebSMike Smith 
13883a31b7ebSMike Smith     /* destroy command memory and DMA tag */
13893a31b7ebSMike Smith     if (sc->ciss_command != NULL) {
13903a31b7ebSMike Smith 	bus_dmamap_unload(sc->ciss_command_dmat, sc->ciss_command_map);
13913a31b7ebSMike Smith 	bus_dmamem_free(sc->ciss_command_dmat, sc->ciss_command, sc->ciss_command_map);
13923a31b7ebSMike Smith     }
13933284b9eeSPaul Saab     if (sc->ciss_command_dmat)
13943a31b7ebSMike Smith 	bus_dma_tag_destroy(sc->ciss_command_dmat);
13953a31b7ebSMike Smith 
13963a31b7ebSMike Smith     /* disconnect from CAM */
13973a31b7ebSMike Smith     if (sc->ciss_cam_sim) {
13983a31b7ebSMike Smith 	xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim));
13993a31b7ebSMike Smith 	cam_sim_free(sc->ciss_cam_sim, 0);
14003a31b7ebSMike Smith     }
14013a31b7ebSMike Smith     if (sc->ciss_cam_devq)
14023a31b7ebSMike Smith 	cam_simq_free(sc->ciss_cam_devq);
14033a31b7ebSMike Smith     /* XXX what about ciss_cam_path? */
14043a31b7ebSMike Smith }
14053a31b7ebSMike Smith 
14063a31b7ebSMike Smith /************************************************************************
14073a31b7ebSMike Smith  * Give a command to the adapter.
14083a31b7ebSMike Smith  *
14093a31b7ebSMike Smith  * Note that this uses the simple transport layer directly.  If we
14103a31b7ebSMike Smith  * want to add support for other layers, we'll need a switch of some
14113a31b7ebSMike Smith  * sort.
14123a31b7ebSMike Smith  *
14133a31b7ebSMike Smith  * Note that the simple transport layer has no way of refusing a
14143a31b7ebSMike Smith  * command; we only have as many request structures as the adapter
14153a31b7ebSMike Smith  * supports commands, so we don't have to check (this presumes that
14163a31b7ebSMike Smith  * the adapter can handle commands as fast as we throw them at it).
14173a31b7ebSMike Smith  */
14183a31b7ebSMike Smith static int
14193a31b7ebSMike Smith ciss_start(struct ciss_request *cr)
14203a31b7ebSMike Smith {
14213a31b7ebSMike Smith     struct ciss_command	*cc;	/* XXX debugging only */
14223a31b7ebSMike Smith     int			error;
14233a31b7ebSMike Smith 
14243a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
14253a31b7ebSMike Smith     debug(2, "post command %d tag %d ", cr->cr_tag, cc->header.host_tag);
14263a31b7ebSMike Smith 
14273a31b7ebSMike Smith     /*
14283a31b7ebSMike Smith      * Map the request's data.
14293a31b7ebSMike Smith      */
14303a31b7ebSMike Smith     if ((error = ciss_map_request(cr)))
14313a31b7ebSMike Smith 	return(error);
14323a31b7ebSMike Smith 
14333a31b7ebSMike Smith #if 0
14343a31b7ebSMike Smith     ciss_print_request(cr);
14353a31b7ebSMike Smith #endif
14363a31b7ebSMike Smith 
14373a31b7ebSMike Smith     return(0);
14383a31b7ebSMike Smith }
14393a31b7ebSMike Smith 
14403a31b7ebSMike Smith /************************************************************************
14413a31b7ebSMike Smith  * Fetch completed request(s) from the adapter, queue them for
14423a31b7ebSMike Smith  * completion handling.
14433a31b7ebSMike Smith  *
14443a31b7ebSMike Smith  * Note that this uses the simple transport layer directly.  If we
14453a31b7ebSMike Smith  * want to add support for other layers, we'll need a switch of some
14463a31b7ebSMike Smith  * sort.
14473a31b7ebSMike Smith  *
14483a31b7ebSMike Smith  * Note that the simple transport mechanism does not require any
14493a31b7ebSMike Smith  * reentrancy protection; the OPQ read is atomic.  If there is a
14503a31b7ebSMike Smith  * chance of a race with something else that might move the request
14513a31b7ebSMike Smith  * off the busy list, then we will have to lock against that
14523a31b7ebSMike Smith  * (eg. timeouts, etc.)
14533a31b7ebSMike Smith  */
14543a31b7ebSMike Smith static void
14553a31b7ebSMike Smith ciss_done(struct ciss_softc *sc)
14563a31b7ebSMike Smith {
14573a31b7ebSMike Smith     struct ciss_request	*cr;
14583a31b7ebSMike Smith     struct ciss_command	*cc;
14593a31b7ebSMike Smith     u_int32_t		tag, index;
14603a31b7ebSMike Smith     int			complete;
14613a31b7ebSMike Smith 
14623a31b7ebSMike Smith     debug_called(3);
14633a31b7ebSMike Smith 
14643a31b7ebSMike Smith     /*
14653a31b7ebSMike Smith      * Loop quickly taking requests from the adapter and moving them
14663a31b7ebSMike Smith      * from the busy queue to the completed queue.
14673a31b7ebSMike Smith      */
14683a31b7ebSMike Smith     complete = 0;
14693a31b7ebSMike Smith     for (;;) {
14703a31b7ebSMike Smith 
14713a31b7ebSMike Smith 	/* see if the OPQ contains anything */
14723a31b7ebSMike Smith 	if (!CISS_TL_SIMPLE_OPQ_INTERRUPT(sc))
14733a31b7ebSMike Smith 	    break;
14743a31b7ebSMike Smith 
14753a31b7ebSMike Smith 	tag = CISS_TL_SIMPLE_FETCH_CMD(sc);
14763a31b7ebSMike Smith 	if (tag == CISS_TL_SIMPLE_OPQ_EMPTY)
14773a31b7ebSMike Smith 	    break;
14783a31b7ebSMike Smith 	index = tag >> 2;
14793a31b7ebSMike Smith 	debug(2, "completed command %d%s", index,
14803a31b7ebSMike Smith 	      (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
14813a31b7ebSMike Smith 	if (index >= sc->ciss_max_requests) {
14823a31b7ebSMike Smith 	    ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
14833a31b7ebSMike Smith 	    continue;
14843a31b7ebSMike Smith 	}
14853a31b7ebSMike Smith 	cr = &(sc->ciss_request[index]);
14863a31b7ebSMike Smith 	cc = CISS_FIND_COMMAND(cr);
14873a31b7ebSMike Smith 	cc->header.host_tag = tag;	/* not updated by adapter */
14883a31b7ebSMike Smith 	if (ciss_remove_busy(cr)) {
14893a31b7ebSMike Smith 	    /* assume this is garbage out of the adapter */
14903a31b7ebSMike Smith 	    ciss_printf(sc, "completed nonbusy request %d\n", index);
14913a31b7ebSMike Smith 	} else {
14923a31b7ebSMike Smith 	    ciss_enqueue_complete(cr);
14933a31b7ebSMike Smith 	}
14943a31b7ebSMike Smith 	complete = 1;
14953a31b7ebSMike Smith     }
14963a31b7ebSMike Smith 
14973a31b7ebSMike Smith     /*
14983a31b7ebSMike Smith      * Invoke completion processing.  If we can defer this out of
14993a31b7ebSMike Smith      * interrupt context, that'd be good.
15003a31b7ebSMike Smith      */
15013a31b7ebSMike Smith     if (complete)
15023a31b7ebSMike Smith 	ciss_complete(sc);
15033a31b7ebSMike Smith }
15043a31b7ebSMike Smith 
15053a31b7ebSMike Smith /************************************************************************
15063a31b7ebSMike Smith  * Take an interrupt from the adapter.
15073a31b7ebSMike Smith  */
15083a31b7ebSMike Smith static void
15093a31b7ebSMike Smith ciss_intr(void *arg)
15103a31b7ebSMike Smith {
15113a31b7ebSMike Smith     struct ciss_softc	*sc = (struct ciss_softc *)arg;
15123a31b7ebSMike Smith 
15133a31b7ebSMike Smith     /*
15143a31b7ebSMike Smith      * The only interrupt we recognise indicates that there are
15153a31b7ebSMike Smith      * entries in the outbound post queue.
15163a31b7ebSMike Smith      */
15173a31b7ebSMike Smith     ciss_done(sc);
15183a31b7ebSMike Smith }
15193a31b7ebSMike Smith 
15203a31b7ebSMike Smith /************************************************************************
15213a31b7ebSMike Smith  * Process completed requests.
15223a31b7ebSMike Smith  *
15233a31b7ebSMike Smith  * Requests can be completed in three fashions:
15243a31b7ebSMike Smith  *
15253a31b7ebSMike Smith  * - by invoking a callback function (cr_complete is non-null)
15263a31b7ebSMike Smith  * - by waking up a sleeper (cr_flags has CISS_REQ_SLEEP set)
15273a31b7ebSMike Smith  * - by clearing the CISS_REQ_POLL flag in interrupt/timeout context
15283a31b7ebSMike Smith  */
15293a31b7ebSMike Smith static void
15303a31b7ebSMike Smith ciss_complete(struct ciss_softc *sc)
15313a31b7ebSMike Smith {
15323a31b7ebSMike Smith     struct ciss_request	*cr;
15333a31b7ebSMike Smith 
15343a31b7ebSMike Smith     debug_called(2);
15353a31b7ebSMike Smith 
15363a31b7ebSMike Smith     /*
15373a31b7ebSMike Smith      * Loop taking requests off the completed queue and performing
15383a31b7ebSMike Smith      * completion processing on them.
15393a31b7ebSMike Smith      */
15403a31b7ebSMike Smith     for (;;) {
15413a31b7ebSMike Smith 	if ((cr = ciss_dequeue_complete(sc)) == NULL)
15423a31b7ebSMike Smith 	    break;
15433a31b7ebSMike Smith 	ciss_unmap_request(cr);
15443a31b7ebSMike Smith 
15453a31b7ebSMike Smith 	/*
15463a31b7ebSMike Smith 	 * If the request has a callback, invoke it.
15473a31b7ebSMike Smith 	 */
15483a31b7ebSMike Smith 	if (cr->cr_complete != NULL) {
15493a31b7ebSMike Smith 	    cr->cr_complete(cr);
15503a31b7ebSMike Smith 	    continue;
15513a31b7ebSMike Smith 	}
15523a31b7ebSMike Smith 
15533a31b7ebSMike Smith 	/*
15543a31b7ebSMike Smith 	 * If someone is sleeping on this request, wake them up.
15553a31b7ebSMike Smith 	 */
15563a31b7ebSMike Smith 	if (cr->cr_flags & CISS_REQ_SLEEP) {
15573a31b7ebSMike Smith 	    cr->cr_flags &= ~CISS_REQ_SLEEP;
15583a31b7ebSMike Smith 	    wakeup(cr);
15593a31b7ebSMike Smith 	    continue;
15603a31b7ebSMike Smith 	}
15613a31b7ebSMike Smith 
15623a31b7ebSMike Smith 	/*
15633a31b7ebSMike Smith 	 * If someone is polling this request for completion, signal.
15643a31b7ebSMike Smith 	 */
15653a31b7ebSMike Smith 	if (cr->cr_flags & CISS_REQ_POLL) {
15663a31b7ebSMike Smith 	    cr->cr_flags &= ~CISS_REQ_POLL;
15673a31b7ebSMike Smith 	    continue;
15683a31b7ebSMike Smith 	}
15693a31b7ebSMike Smith 
15703a31b7ebSMike Smith 	/*
15713a31b7ebSMike Smith 	 * Give up and throw the request back on the free queue.  This
15723a31b7ebSMike Smith 	 * should never happen; resources will probably be lost.
15733a31b7ebSMike Smith 	 */
15743a31b7ebSMike Smith 	ciss_printf(sc, "WARNING: completed command with no submitter\n");
15753a31b7ebSMike Smith 	ciss_enqueue_free(cr);
15763a31b7ebSMike Smith     }
15773a31b7ebSMike Smith }
15783a31b7ebSMike Smith 
15793a31b7ebSMike Smith /************************************************************************
15803a31b7ebSMike Smith  * Report on the completion status of a request, and pass back SCSI
15813a31b7ebSMike Smith  * and command status values.
15823a31b7ebSMike Smith  */
15833a31b7ebSMike Smith static int
15843a31b7ebSMike Smith ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status)
15853a31b7ebSMike Smith {
15863a31b7ebSMike Smith     struct ciss_command		*cc;
15873a31b7ebSMike Smith     struct ciss_error_info	*ce;
15883a31b7ebSMike Smith 
15893a31b7ebSMike Smith     debug_called(2);
15903a31b7ebSMike Smith 
15913a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
15923a31b7ebSMike Smith     ce = (struct ciss_error_info *)&(cc->sg[0]);
15933a31b7ebSMike Smith 
15943a31b7ebSMike Smith     /*
15953a31b7ebSMike Smith      * We don't consider data under/overrun an error for the Report
15963a31b7ebSMike Smith      * Logical/Physical LUNs commands.
15973a31b7ebSMike Smith      */
15983a31b7ebSMike Smith     if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) &&
15993a31b7ebSMike Smith 	((cc->cdb.cdb[0] == CISS_OPCODE_REPORT_LOGICAL_LUNS) ||
16003a31b7ebSMike Smith 	 (cc->cdb.cdb[0] == CISS_OPCODE_REPORT_PHYSICAL_LUNS))) {
16013a31b7ebSMike Smith 	cc->header.host_tag &= ~CISS_HDR_HOST_TAG_ERROR;
16023a31b7ebSMike Smith 	debug(2, "ignoring irrelevant under/overrun error");
16033a31b7ebSMike Smith     }
16043a31b7ebSMike Smith 
16053a31b7ebSMike Smith     /*
16063a31b7ebSMike Smith      * Check the command's error bit, if clear, there's no status and
16073a31b7ebSMike Smith      * everything is OK.
16083a31b7ebSMike Smith      */
16093a31b7ebSMike Smith     if (!(cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR)) {
16103a31b7ebSMike Smith 	if (scsi_status != NULL)
16113a31b7ebSMike Smith 	    *scsi_status = SCSI_STATUS_OK;
16123a31b7ebSMike Smith 	if (command_status != NULL)
16133a31b7ebSMike Smith 	    *command_status = CISS_CMD_STATUS_SUCCESS;
16143a31b7ebSMike Smith 	return(0);
16153a31b7ebSMike Smith     } else {
16163a31b7ebSMike Smith 	if (command_status != NULL)
16173a31b7ebSMike Smith 	    *command_status = ce->command_status;
16183a31b7ebSMike Smith 	if (scsi_status != NULL) {
16193a31b7ebSMike Smith 	    if (ce->command_status == CISS_CMD_STATUS_TARGET_STATUS) {
16203a31b7ebSMike Smith 		*scsi_status = ce->scsi_status;
16213a31b7ebSMike Smith 	    } else {
16223a31b7ebSMike Smith 		*scsi_status = -1;
16233a31b7ebSMike Smith 	    }
16243a31b7ebSMike Smith 	}
16253a31b7ebSMike Smith 	if (bootverbose)
16263a31b7ebSMike Smith 	    ciss_printf(cr->cr_sc, "command status 0x%x (%s) scsi status 0x%x\n",
16273a31b7ebSMike Smith 			ce->command_status, ciss_name_command_status(ce->command_status),
16283a31b7ebSMike Smith 			ce->scsi_status);
16293a31b7ebSMike Smith 	if (ce->command_status == CISS_CMD_STATUS_INVALID_COMMAND) {
16303a31b7ebSMike Smith 	    ciss_printf(cr->cr_sc, "invalid command, offense size %d at %d, value 0x%x\n",
16313a31b7ebSMike Smith 			ce->additional_error_info.invalid_command.offense_size,
16323a31b7ebSMike Smith 			ce->additional_error_info.invalid_command.offense_offset,
16333a31b7ebSMike Smith 			ce->additional_error_info.invalid_command.offense_value);
16343a31b7ebSMike Smith 	}
16353a31b7ebSMike Smith     }
16363a31b7ebSMike Smith     return(1);
16373a31b7ebSMike Smith }
16383a31b7ebSMike Smith 
16393a31b7ebSMike Smith /************************************************************************
16403a31b7ebSMike Smith  * Issue a request and don't return until it's completed.
16413a31b7ebSMike Smith  *
16423a31b7ebSMike Smith  * Depending on adapter status, we may poll or sleep waiting for
16433a31b7ebSMike Smith  * completion.
16443a31b7ebSMike Smith  */
16453a31b7ebSMike Smith static int
16463a31b7ebSMike Smith ciss_synch_request(struct ciss_request *cr, int timeout)
16473a31b7ebSMike Smith {
16483a31b7ebSMike Smith     if (cr->cr_sc->ciss_flags & CISS_FLAG_RUNNING) {
16493a31b7ebSMike Smith 	return(ciss_wait_request(cr, timeout));
16503a31b7ebSMike Smith     } else {
16513a31b7ebSMike Smith 	return(ciss_poll_request(cr, timeout));
16523a31b7ebSMike Smith     }
16533a31b7ebSMike Smith }
16543a31b7ebSMike Smith 
16553a31b7ebSMike Smith /************************************************************************
16563a31b7ebSMike Smith  * Issue a request and poll for completion.
16573a31b7ebSMike Smith  *
16583a31b7ebSMike Smith  * Timeout in milliseconds.
16593a31b7ebSMike Smith  */
16603a31b7ebSMike Smith static int
16613a31b7ebSMike Smith ciss_poll_request(struct ciss_request *cr, int timeout)
16623a31b7ebSMike Smith {
16633a31b7ebSMike Smith     int		error;
16643a31b7ebSMike Smith 
16653a31b7ebSMike Smith     debug_called(2);
16663a31b7ebSMike Smith 
16673a31b7ebSMike Smith     cr->cr_flags |= CISS_REQ_POLL;
16683a31b7ebSMike Smith     if ((error = ciss_start(cr)) != 0)
16693a31b7ebSMike Smith 	return(error);
16703a31b7ebSMike Smith 
16713a31b7ebSMike Smith     do {
16723a31b7ebSMike Smith 	ciss_done(cr->cr_sc);
16733a31b7ebSMike Smith 	if (!(cr->cr_flags & CISS_REQ_POLL))
16743a31b7ebSMike Smith 	    return(0);
16753a31b7ebSMike Smith 	DELAY(1000);
16763a31b7ebSMike Smith     } while (timeout-- >= 0);
16773a31b7ebSMike Smith     return(EWOULDBLOCK);
16783a31b7ebSMike Smith }
16793a31b7ebSMike Smith 
16803a31b7ebSMike Smith /************************************************************************
16813a31b7ebSMike Smith  * Issue a request and sleep waiting for completion.
16823a31b7ebSMike Smith  *
16833a31b7ebSMike Smith  * Timeout in milliseconds.  Note that a spurious wakeup will reset
16843a31b7ebSMike Smith  * the timeout.
16853a31b7ebSMike Smith  */
16863a31b7ebSMike Smith static int
16873a31b7ebSMike Smith ciss_wait_request(struct ciss_request *cr, int timeout)
16883a31b7ebSMike Smith {
16893a31b7ebSMike Smith     int		s, error;
16903a31b7ebSMike Smith 
16913a31b7ebSMike Smith     debug_called(2);
16923a31b7ebSMike Smith 
16933a31b7ebSMike Smith     cr->cr_flags |= CISS_REQ_SLEEP;
16943a31b7ebSMike Smith     if ((error = ciss_start(cr)) != 0)
16953a31b7ebSMike Smith 	return(error);
16963a31b7ebSMike Smith 
16973a31b7ebSMike Smith     s = splcam();
16983a31b7ebSMike Smith     while (cr->cr_flags & CISS_REQ_SLEEP) {
16993a31b7ebSMike Smith 	error = tsleep(cr, PCATCH, "cissREQ", (timeout * hz) / 1000);
17003a31b7ebSMike Smith 	/*
17013a31b7ebSMike Smith 	 * On wakeup or interruption due to restartable activity, go
17023a31b7ebSMike Smith 	 * back and check to see if we're done.
17033a31b7ebSMike Smith 	 */
17043a31b7ebSMike Smith 	if ((error == 0) || (error == ERESTART)) {
17053a31b7ebSMike Smith 	    error = 0;
17063a31b7ebSMike Smith 	    continue;
17073a31b7ebSMike Smith 	}
17083a31b7ebSMike Smith 	/*
17093a31b7ebSMike Smith 	 * Timeout, interrupted system call, etc.
17103a31b7ebSMike Smith 	 */
17113a31b7ebSMike Smith 	break;
17123a31b7ebSMike Smith     }
17133a31b7ebSMike Smith     splx(s);
17143a31b7ebSMike Smith     return(error);
17153a31b7ebSMike Smith }
17163a31b7ebSMike Smith 
17176197ca15SPeter Wemm #if 0
17183a31b7ebSMike Smith /************************************************************************
17193a31b7ebSMike Smith  * Abort a request.  Note that a potential exists here to race the
17203a31b7ebSMike Smith  * request being completed; the caller must deal with this.
17213a31b7ebSMike Smith  */
17223a31b7ebSMike Smith static int
17233a31b7ebSMike Smith ciss_abort_request(struct ciss_request *ar)
17243a31b7ebSMike Smith {
17253a31b7ebSMike Smith     struct ciss_request		*cr;
17263a31b7ebSMike Smith     struct ciss_command		*cc;
17273a31b7ebSMike Smith     struct ciss_message_cdb	*cmc;
17283a31b7ebSMike Smith     int				error;
17293a31b7ebSMike Smith 
17303a31b7ebSMike Smith     debug_called(1);
17313a31b7ebSMike Smith 
17323a31b7ebSMike Smith     /* get a request */
17333a31b7ebSMike Smith     if ((error = ciss_get_request(ar->cr_sc, &cr)) != 0)
17343a31b7ebSMike Smith 	return(error);
17353a31b7ebSMike Smith 
17363a31b7ebSMike Smith     /* build the abort command */
17373a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
17383a31b7ebSMike Smith     cc->header.address.mode.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;	/* addressing? */
17393a31b7ebSMike Smith     cc->header.address.physical.target = 0;
17403a31b7ebSMike Smith     cc->header.address.physical.bus = 0;
17413a31b7ebSMike Smith     cc->cdb.cdb_length = sizeof(*cmc);
17423a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
17433a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
17443a31b7ebSMike Smith     cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
17453a31b7ebSMike Smith     cc->cdb.timeout = 30;
17463a31b7ebSMike Smith 
17473a31b7ebSMike Smith     cmc = (struct ciss_message_cdb *)&(cc->cdb.cdb[0]);
17483a31b7ebSMike Smith     cmc->opcode = CISS_OPCODE_MESSAGE_ABORT;
17493a31b7ebSMike Smith     cmc->type = CISS_MESSAGE_ABORT_TASK;
17503a31b7ebSMike Smith     cmc->abort_tag = ar->cr_tag;	/* endianness?? */
17513a31b7ebSMike Smith 
17523a31b7ebSMike Smith     /*
17533a31b7ebSMike Smith      * Send the request and wait for a response.  If we believe we
17543a31b7ebSMike Smith      * aborted the request OK, clear the flag that indicates it's
17553a31b7ebSMike Smith      * running.
17563a31b7ebSMike Smith      */
17573a31b7ebSMike Smith     error = ciss_synch_request(cr, 35 * 1000);
17583a31b7ebSMike Smith     if (!error)
17593a31b7ebSMike Smith 	error = ciss_report_request(cr, NULL, NULL);
17603a31b7ebSMike Smith     ciss_release_request(cr);
17613a31b7ebSMike Smith 
17623a31b7ebSMike Smith     return(error);
17633a31b7ebSMike Smith }
17646197ca15SPeter Wemm #endif
17653a31b7ebSMike Smith 
17663a31b7ebSMike Smith 
17673a31b7ebSMike Smith /************************************************************************
17683a31b7ebSMike Smith  * Fetch and initialise a request
17693a31b7ebSMike Smith  */
17703a31b7ebSMike Smith static int
17713a31b7ebSMike Smith ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp)
17723a31b7ebSMike Smith {
17733a31b7ebSMike Smith     struct ciss_request *cr;
17743a31b7ebSMike Smith 
17753a31b7ebSMike Smith     debug_called(2);
17763a31b7ebSMike Smith 
17773a31b7ebSMike Smith     /*
17783a31b7ebSMike Smith      * Get a request and clean it up.
17793a31b7ebSMike Smith      */
17803a31b7ebSMike Smith     if ((cr = ciss_dequeue_free(sc)) == NULL)
17813a31b7ebSMike Smith 	return(ENOMEM);
17823a31b7ebSMike Smith 
17833a31b7ebSMike Smith     cr->cr_data = NULL;
17843a31b7ebSMike Smith     cr->cr_flags = 0;
17853a31b7ebSMike Smith     cr->cr_complete = NULL;
1786639717c8SPaul Saab     cr->cr_private = NULL;
17873a31b7ebSMike Smith 
17883a31b7ebSMike Smith     ciss_preen_command(cr);
17893a31b7ebSMike Smith     *crp = cr;
17903a31b7ebSMike Smith     return(0);
17913a31b7ebSMike Smith }
17923a31b7ebSMike Smith 
17933a31b7ebSMike Smith static void
17943a31b7ebSMike Smith ciss_preen_command(struct ciss_request *cr)
17953a31b7ebSMike Smith {
17963a31b7ebSMike Smith     struct ciss_command	*cc;
17973a31b7ebSMike Smith     u_int32_t		cmdphys;
17983a31b7ebSMike Smith 
17993a31b7ebSMike Smith     /*
18003a31b7ebSMike Smith      * Clean up the command structure.
18013a31b7ebSMike Smith      *
18023a31b7ebSMike Smith      * Note that we set up the error_info structure here, since the
18033a31b7ebSMike Smith      * length can be overwritten by any command.
18043a31b7ebSMike Smith      */
18053a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
18063a31b7ebSMike Smith     cc->header.sg_in_list = 0;		/* kinda inefficient this way */
18073a31b7ebSMike Smith     cc->header.sg_total = 0;
18083a31b7ebSMike Smith     cc->header.host_tag = cr->cr_tag << 2;
18093a31b7ebSMike Smith     cc->header.host_tag_zeroes = 0;
18103a31b7ebSMike Smith     cmdphys = CISS_FIND_COMMANDPHYS(cr);
18113a31b7ebSMike Smith     cc->error_info.error_info_address = cmdphys + sizeof(struct ciss_command);
18123a31b7ebSMike Smith     cc->error_info.error_info_length = CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command);
18133a31b7ebSMike Smith 
18143a31b7ebSMike Smith }
18153a31b7ebSMike Smith 
18163a31b7ebSMike Smith /************************************************************************
18173a31b7ebSMike Smith  * Release a request to the free list.
18183a31b7ebSMike Smith  */
18193a31b7ebSMike Smith static void
18203a31b7ebSMike Smith ciss_release_request(struct ciss_request *cr)
18213a31b7ebSMike Smith {
18223a31b7ebSMike Smith     struct ciss_softc	*sc;
18233a31b7ebSMike Smith 
18243a31b7ebSMike Smith     debug_called(2);
18253a31b7ebSMike Smith 
18263a31b7ebSMike Smith     sc = cr->cr_sc;
18273a31b7ebSMike Smith 
18283a31b7ebSMike Smith     /* release the request to the free queue */
18293a31b7ebSMike Smith     ciss_requeue_free(cr);
18303a31b7ebSMike Smith }
18313a31b7ebSMike Smith 
18323a31b7ebSMike Smith /************************************************************************
18333a31b7ebSMike Smith  * Allocate a request that will be used to send a BMIC command.  Do some
18343a31b7ebSMike Smith  * of the common setup here to avoid duplicating it everywhere else.
18353a31b7ebSMike Smith  */
18363a31b7ebSMike Smith static int
18373a31b7ebSMike Smith ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
18383a31b7ebSMike Smith 		      int opcode, void **bufp, size_t bufsize)
18393a31b7ebSMike Smith {
18403a31b7ebSMike Smith     struct ciss_request		*cr;
18413a31b7ebSMike Smith     struct ciss_command		*cc;
18423a31b7ebSMike Smith     struct ciss_bmic_cdb	*cbc;
18433a31b7ebSMike Smith     void			*buf;
18443a31b7ebSMike Smith     int				error;
18453a31b7ebSMike Smith     int				dataout;
18463a31b7ebSMike Smith 
18473a31b7ebSMike Smith     debug_called(2);
18483a31b7ebSMike Smith 
18493a31b7ebSMike Smith     cr = NULL;
18503a31b7ebSMike Smith     buf = NULL;
18513a31b7ebSMike Smith 
18523a31b7ebSMike Smith     /*
18533a31b7ebSMike Smith      * Get a request.
18543a31b7ebSMike Smith      */
18553a31b7ebSMike Smith     if ((error = ciss_get_request(sc, &cr)) != 0)
18563a31b7ebSMike Smith 	goto out;
18573a31b7ebSMike Smith 
18583a31b7ebSMike Smith     /*
18593a31b7ebSMike Smith      * Allocate data storage if requested, determine the data direction.
18603a31b7ebSMike Smith      */
18613a31b7ebSMike Smith     dataout = 0;
18623a31b7ebSMike Smith     if ((bufsize > 0) && (bufp != NULL)) {
18633a31b7ebSMike Smith 	if (*bufp == NULL) {
18643a31b7ebSMike Smith 	    if ((buf = malloc(bufsize, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
18653a31b7ebSMike Smith 		error = ENOMEM;
18663a31b7ebSMike Smith 		goto out;
18673a31b7ebSMike Smith 	    }
18683a31b7ebSMike Smith 	} else {
18693a31b7ebSMike Smith 	    buf = *bufp;
18703a31b7ebSMike Smith 	    dataout = 1;	/* we are given a buffer, so we are writing */
18713a31b7ebSMike Smith 	}
18723a31b7ebSMike Smith     }
18733a31b7ebSMike Smith 
18743a31b7ebSMike Smith     /*
18753a31b7ebSMike Smith      * Build a CISS BMIC command to get the logical drive ID.
18763a31b7ebSMike Smith      */
18773a31b7ebSMike Smith     cr->cr_data = buf;
18783a31b7ebSMike Smith     cr->cr_length = bufsize;
18793a31b7ebSMike Smith     if (!dataout)
18803a31b7ebSMike Smith 	cr->cr_flags = CISS_REQ_DATAIN;
18813a31b7ebSMike Smith 
18823a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
18833a31b7ebSMike Smith     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
18843a31b7ebSMike Smith     cc->header.address.physical.bus = 0;
18853a31b7ebSMike Smith     cc->header.address.physical.target = 0;
18863a31b7ebSMike Smith     cc->cdb.cdb_length = sizeof(*cbc);
18873a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
18883a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
18893a31b7ebSMike Smith     cc->cdb.direction = dataout ? CISS_CDB_DIRECTION_WRITE : CISS_CDB_DIRECTION_READ;
18903a31b7ebSMike Smith     cc->cdb.timeout = 0;
18913a31b7ebSMike Smith 
18923a31b7ebSMike Smith     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
18933a31b7ebSMike Smith     bzero(cbc, sizeof(*cbc));
18943a31b7ebSMike Smith     cbc->opcode = dataout ? CISS_ARRAY_CONTROLLER_WRITE : CISS_ARRAY_CONTROLLER_READ;
18953a31b7ebSMike Smith     cbc->bmic_opcode = opcode;
18963a31b7ebSMike Smith     cbc->size = htons((u_int16_t)bufsize);
18973a31b7ebSMike Smith 
18983a31b7ebSMike Smith out:
18993a31b7ebSMike Smith     if (error) {
19003a31b7ebSMike Smith 	if (cr != NULL)
19013a31b7ebSMike Smith 	    ciss_release_request(cr);
19023a31b7ebSMike Smith 	if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL))
19033a31b7ebSMike Smith 	    free(buf, CISS_MALLOC_CLASS);
19043a31b7ebSMike Smith     } else {
19053a31b7ebSMike Smith 	*crp = cr;
19063a31b7ebSMike Smith 	if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL))
19073a31b7ebSMike Smith 	    *bufp = buf;
19083a31b7ebSMike Smith     }
19093a31b7ebSMike Smith     return(error);
19103a31b7ebSMike Smith }
19113a31b7ebSMike Smith 
19123a31b7ebSMike Smith /************************************************************************
19133a31b7ebSMike Smith  * Handle a command passed in from userspace.
19143a31b7ebSMike Smith  */
19153a31b7ebSMike Smith static int
19163a31b7ebSMike Smith ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc)
19173a31b7ebSMike Smith {
19183a31b7ebSMike Smith     struct ciss_request		*cr;
19193a31b7ebSMike Smith     struct ciss_command		*cc;
19203a31b7ebSMike Smith     struct ciss_error_info	*ce;
19213c63a8b4SPaul Saab     int				error = 0;
19223a31b7ebSMike Smith 
19233a31b7ebSMike Smith     debug_called(1);
19243a31b7ebSMike Smith 
19253a31b7ebSMike Smith     cr = NULL;
19263a31b7ebSMike Smith 
19273a31b7ebSMike Smith     /*
19283a31b7ebSMike Smith      * Get a request.
19293a31b7ebSMike Smith      */
19303a31b7ebSMike Smith     if ((error = ciss_get_request(sc, &cr)) != 0)
19313a31b7ebSMike Smith 	goto out;
19323a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
19333a31b7ebSMike Smith 
19343a31b7ebSMike Smith     /*
19353a31b7ebSMike Smith      * Allocate an in-kernel databuffer if required, copy in user data.
19363a31b7ebSMike Smith      */
19373a31b7ebSMike Smith     cr->cr_length = ioc->buf_size;
19383a31b7ebSMike Smith     if (ioc->buf_size > 0) {
1939a163d034SWarner Losh 	if ((cr->cr_data = malloc(ioc->buf_size, CISS_MALLOC_CLASS, M_WAITOK)) == NULL) {
19403a31b7ebSMike Smith 	    error = ENOMEM;
19413a31b7ebSMike Smith 	    goto out;
19423a31b7ebSMike Smith 	}
19433a31b7ebSMike Smith 	if ((error = copyin(ioc->buf, cr->cr_data, ioc->buf_size))) {
19443a31b7ebSMike Smith 	    debug(0, "copyin: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
19453a31b7ebSMike Smith 	    goto out;
19463a31b7ebSMike Smith 	}
19473a31b7ebSMike Smith     }
19483a31b7ebSMike Smith 
19493a31b7ebSMike Smith     /*
19503a31b7ebSMike Smith      * Build the request based on the user command.
19513a31b7ebSMike Smith      */
19523a31b7ebSMike Smith     bcopy(&ioc->LUN_info, &cc->header.address, sizeof(cc->header.address));
19533a31b7ebSMike Smith     bcopy(&ioc->Request, &cc->cdb, sizeof(cc->cdb));
19543a31b7ebSMike Smith 
19553a31b7ebSMike Smith     /* XXX anything else to populate here? */
19563a31b7ebSMike Smith 
19573a31b7ebSMike Smith     /*
19583a31b7ebSMike Smith      * Run the command.
19593a31b7ebSMike Smith      */
19603a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000))) {
19613a31b7ebSMike Smith 	debug(0, "request failed - %d", error);
19623a31b7ebSMike Smith 	goto out;
19633a31b7ebSMike Smith     }
19643a31b7ebSMike Smith 
19653a31b7ebSMike Smith     /*
19663c63a8b4SPaul Saab      * Check to see if the command succeeded.
19673a31b7ebSMike Smith      */
19683a31b7ebSMike Smith     ce = (struct ciss_error_info *)&(cc->sg[0]);
19693c63a8b4SPaul Saab     if (ciss_report_request(cr, NULL, NULL) == 0)
19703c63a8b4SPaul Saab 	bzero(ce, sizeof(*ce));
19713c63a8b4SPaul Saab     else
19723c63a8b4SPaul Saab 	error = EIO;
19733c63a8b4SPaul Saab 
19743c63a8b4SPaul Saab     /*
19753c63a8b4SPaul Saab      * Copy the results back to the user.
19763c63a8b4SPaul Saab      */
19773a31b7ebSMike Smith     bcopy(ce, &ioc->error_info, sizeof(*ce));
19783a31b7ebSMike Smith     if ((ioc->buf_size > 0) &&
19793a31b7ebSMike Smith 	(error = copyout(cr->cr_data, ioc->buf, ioc->buf_size))) {
19803a31b7ebSMike Smith 	debug(0, "copyout: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
19813a31b7ebSMike Smith 	goto out;
19823a31b7ebSMike Smith     }
19833a31b7ebSMike Smith 
19843a31b7ebSMike Smith     /* done OK */
19853a31b7ebSMike Smith     error = 0;
19863a31b7ebSMike Smith 
19873a31b7ebSMike Smith out:
19883a31b7ebSMike Smith     if ((cr != NULL) && (cr->cr_data != NULL))
19893a31b7ebSMike Smith 	free(cr->cr_data, CISS_MALLOC_CLASS);
19903a31b7ebSMike Smith     if (cr != NULL)
19913a31b7ebSMike Smith 	ciss_release_request(cr);
19923a31b7ebSMike Smith     return(error);
19933a31b7ebSMike Smith }
19943a31b7ebSMike Smith 
19953a31b7ebSMike Smith /************************************************************************
19963a31b7ebSMike Smith  * Map a request into bus-visible space, initialise the scatter/gather
19973a31b7ebSMike Smith  * list.
19983a31b7ebSMike Smith  */
19993a31b7ebSMike Smith static int
20003a31b7ebSMike Smith ciss_map_request(struct ciss_request *cr)
20013a31b7ebSMike Smith {
20023a31b7ebSMike Smith     struct ciss_softc	*sc;
2003639717c8SPaul Saab     int			error = 0;
20043a31b7ebSMike Smith 
20053a31b7ebSMike Smith     debug_called(2);
20063a31b7ebSMike Smith 
20073a31b7ebSMike Smith     sc = cr->cr_sc;
20083a31b7ebSMike Smith 
20093a31b7ebSMike Smith     /* check that mapping is necessary */
2010639717c8SPaul Saab     if (cr->cr_flags & CISS_REQ_MAPPED)
20113a31b7ebSMike Smith 	return(0);
20123a31b7ebSMike Smith 
20133a31b7ebSMike Smith     cr->cr_flags |= CISS_REQ_MAPPED;
2014639717c8SPaul Saab 
2015639717c8SPaul Saab     bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2016639717c8SPaul Saab 		    BUS_DMASYNC_PREWRITE);
2017639717c8SPaul Saab 
2018639717c8SPaul Saab     if (cr->cr_data != NULL) {
2019639717c8SPaul Saab 	error = bus_dmamap_load(sc->ciss_buffer_dmat, cr->cr_datamap,
2020639717c8SPaul Saab 				cr->cr_data, cr->cr_length,
2021639717c8SPaul Saab 				ciss_request_map_helper, cr, 0);
2022639717c8SPaul Saab 	if (error != 0)
2023639717c8SPaul Saab 	    return (error);
2024639717c8SPaul Saab     } else {
2025639717c8SPaul Saab 	/*
2026639717c8SPaul Saab 	 * Post the command to the adapter.
2027639717c8SPaul Saab 	 */
2028639717c8SPaul Saab 	ciss_enqueue_busy(cr);
2029639717c8SPaul Saab 	CISS_TL_SIMPLE_POST_CMD(cr->cr_sc, CISS_FIND_COMMANDPHYS(cr));
2030639717c8SPaul Saab     }
2031639717c8SPaul Saab 
20323a31b7ebSMike Smith     return(0);
20333a31b7ebSMike Smith }
20343a31b7ebSMike Smith 
20353a31b7ebSMike Smith static void
20363a31b7ebSMike Smith ciss_request_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
20373a31b7ebSMike Smith {
20383a31b7ebSMike Smith     struct ciss_command	*cc;
2039639717c8SPaul Saab     struct ciss_request *cr;
2040639717c8SPaul Saab     struct ciss_softc	*sc;
20413a31b7ebSMike Smith     int			i;
20423a31b7ebSMike Smith 
20433a31b7ebSMike Smith     debug_called(2);
20443a31b7ebSMike Smith 
2045639717c8SPaul Saab     cr = (struct ciss_request *)arg;
2046639717c8SPaul Saab     sc = cr->cr_sc;
2047639717c8SPaul Saab     cc = CISS_FIND_COMMAND(cr);
2048639717c8SPaul Saab 
20493a31b7ebSMike Smith     for (i = 0; i < nseg; i++) {
20503a31b7ebSMike Smith 	cc->sg[i].address = segs[i].ds_addr;
20513a31b7ebSMike Smith 	cc->sg[i].length = segs[i].ds_len;
20523a31b7ebSMike Smith 	cc->sg[i].extension = 0;
20533a31b7ebSMike Smith     }
20543a31b7ebSMike Smith     /* we leave the s/g table entirely within the command */
20553a31b7ebSMike Smith     cc->header.sg_in_list = nseg;
20563a31b7ebSMike Smith     cc->header.sg_total = nseg;
2057639717c8SPaul Saab 
2058639717c8SPaul Saab     if (cr->cr_flags & CISS_REQ_DATAIN)
2059639717c8SPaul Saab 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREREAD);
2060639717c8SPaul Saab     if (cr->cr_flags & CISS_REQ_DATAOUT)
2061639717c8SPaul Saab 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREWRITE);
2062639717c8SPaul Saab 
2063639717c8SPaul Saab     /*
2064639717c8SPaul Saab      * Post the command to the adapter.
2065639717c8SPaul Saab      */
2066639717c8SPaul Saab     ciss_enqueue_busy(cr);
2067639717c8SPaul Saab     CISS_TL_SIMPLE_POST_CMD(cr->cr_sc, CISS_FIND_COMMANDPHYS(cr));
20683a31b7ebSMike Smith }
20693a31b7ebSMike Smith 
20703a31b7ebSMike Smith /************************************************************************
20713a31b7ebSMike Smith  * Unmap a request from bus-visible space.
20723a31b7ebSMike Smith  */
20733a31b7ebSMike Smith static void
20743a31b7ebSMike Smith ciss_unmap_request(struct ciss_request *cr)
20753a31b7ebSMike Smith {
20763a31b7ebSMike Smith     struct ciss_softc	*sc;
20773a31b7ebSMike Smith 
20783a31b7ebSMike Smith     debug_called(2);
20793a31b7ebSMike Smith 
20803a31b7ebSMike Smith     sc = cr->cr_sc;
20813a31b7ebSMike Smith 
20823a31b7ebSMike Smith     /* check that unmapping is necessary */
2083639717c8SPaul Saab     if ((cr->cr_flags & CISS_REQ_MAPPED) == 0)
20843a31b7ebSMike Smith 	return;
20853a31b7ebSMike Smith 
2086639717c8SPaul Saab     bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2087639717c8SPaul Saab 		    BUS_DMASYNC_POSTWRITE);
2088639717c8SPaul Saab 
2089639717c8SPaul Saab     if (cr->cr_data == NULL)
2090639717c8SPaul Saab 	goto out;
2091639717c8SPaul Saab 
20923a31b7ebSMike Smith     if (cr->cr_flags & CISS_REQ_DATAIN)
20933a31b7ebSMike Smith 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTREAD);
20943a31b7ebSMike Smith     if (cr->cr_flags & CISS_REQ_DATAOUT)
20953a31b7ebSMike Smith 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTWRITE);
20963a31b7ebSMike Smith 
20973a31b7ebSMike Smith     bus_dmamap_unload(sc->ciss_buffer_dmat, cr->cr_datamap);
2098639717c8SPaul Saab out:
20993a31b7ebSMike Smith     cr->cr_flags &= ~CISS_REQ_MAPPED;
21003a31b7ebSMike Smith }
21013a31b7ebSMike Smith 
21023a31b7ebSMike Smith /************************************************************************
21033a31b7ebSMike Smith  * Attach the driver to CAM.
21043a31b7ebSMike Smith  *
21053a31b7ebSMike Smith  * We put all the logical drives on a single SCSI bus.
21063a31b7ebSMike Smith  */
21073a31b7ebSMike Smith static int
21083a31b7ebSMike Smith ciss_cam_init(struct ciss_softc *sc)
21093a31b7ebSMike Smith {
21103a31b7ebSMike Smith 
21113a31b7ebSMike Smith     debug_called(1);
21123a31b7ebSMike Smith 
21133a31b7ebSMike Smith     /*
21143a31b7ebSMike Smith      * Allocate a devq.  We can reuse this for the masked physical
21153a31b7ebSMike Smith      * devices if we decide to export these as well.
21163a31b7ebSMike Smith      */
21173a31b7ebSMike Smith     if ((sc->ciss_cam_devq = cam_simq_alloc(sc->ciss_max_requests)) == NULL) {
21183a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate CAM SIM queue\n");
21193a31b7ebSMike Smith 	return(ENOMEM);
21203a31b7ebSMike Smith     }
21213a31b7ebSMike Smith 
21223a31b7ebSMike Smith     /*
21233a31b7ebSMike Smith      * Create a SIM.
21243a31b7ebSMike Smith      */
21253a31b7ebSMike Smith     if ((sc->ciss_cam_sim = cam_sim_alloc(ciss_cam_action, ciss_cam_poll, "ciss", sc,
21262aa6b972SPaul Saab 					  device_get_unit(sc->ciss_dev),
2127d4aa427fSPaul Saab 					  sc->ciss_max_requests - 2,
21282aa6b972SPaul Saab 					  1,
21293a31b7ebSMike Smith 					  sc->ciss_cam_devq)) == NULL) {
21303a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate CAM SIM\n");
21313a31b7ebSMike Smith 	return(ENOMEM);
21323a31b7ebSMike Smith     }
21333a31b7ebSMike Smith 
21343a31b7ebSMike Smith     /*
21353a31b7ebSMike Smith      * Register bus 0 (the 'logical drives' bus) with this SIM.
21363a31b7ebSMike Smith      */
21373a31b7ebSMike Smith     if (xpt_bus_register(sc->ciss_cam_sim, 0) != 0) {
21383a31b7ebSMike Smith 	ciss_printf(sc, "can't register SCSI bus 0\n");
21393a31b7ebSMike Smith 	return(ENXIO);
21403a31b7ebSMike Smith     }
21413a31b7ebSMike Smith 
21423a31b7ebSMike Smith     /*
21433a31b7ebSMike Smith      * Initiate a rescan of the bus.
21443a31b7ebSMike Smith      */
21453a31b7ebSMike Smith     ciss_cam_rescan_all(sc);
21463a31b7ebSMike Smith 
21473a31b7ebSMike Smith     return(0);
21483a31b7ebSMike Smith }
21493a31b7ebSMike Smith 
21503a31b7ebSMike Smith /************************************************************************
21513a31b7ebSMike Smith  * Initiate a rescan of the 'logical devices' SIM
21523a31b7ebSMike Smith  */
21533a31b7ebSMike Smith static void
21543a31b7ebSMike Smith ciss_cam_rescan_target(struct ciss_softc *sc, int target)
21553a31b7ebSMike Smith {
21563a31b7ebSMike Smith     union ccb	*ccb;
21573a31b7ebSMike Smith 
21583a31b7ebSMike Smith     debug_called(1);
21593a31b7ebSMike Smith 
2160a163d034SWarner Losh     if ((ccb = malloc(sizeof(union ccb), M_TEMP, M_WAITOK | M_ZERO)) == NULL) {
21613a31b7ebSMike Smith 	ciss_printf(sc, "rescan failed (can't allocate CCB)\n");
21623a31b7ebSMike Smith 	return;
21633a31b7ebSMike Smith     }
21643a31b7ebSMike Smith 
21653a31b7ebSMike Smith     if (xpt_create_path(&sc->ciss_cam_path, xpt_periph, cam_sim_path(sc->ciss_cam_sim), target, 0)
21663a31b7ebSMike Smith 	!= CAM_REQ_CMP) {
21673a31b7ebSMike Smith 	ciss_printf(sc, "rescan failed (can't create path)\n");
21683a31b7ebSMike Smith 	return;
21693a31b7ebSMike Smith     }
21703a31b7ebSMike Smith 
21713a31b7ebSMike Smith     xpt_setup_ccb(&ccb->ccb_h, sc->ciss_cam_path, 5/*priority (low)*/);
21723a31b7ebSMike Smith     ccb->ccb_h.func_code = XPT_SCAN_BUS;
21733a31b7ebSMike Smith     ccb->ccb_h.cbfcnp = ciss_cam_rescan_callback;
21743a31b7ebSMike Smith     ccb->crcn.flags = CAM_FLAG_NONE;
21753a31b7ebSMike Smith     xpt_action(ccb);
21763a31b7ebSMike Smith 
21773a31b7ebSMike Smith     /* scan is now in progress */
21783a31b7ebSMike Smith }
21793a31b7ebSMike Smith 
21803a31b7ebSMike Smith static void
21813a31b7ebSMike Smith ciss_cam_rescan_all(struct ciss_softc *sc)
21823a31b7ebSMike Smith {
2183777d1b39SPoul-Henning Kamp     ciss_cam_rescan_target(sc, 0);
21843a31b7ebSMike Smith }
21853a31b7ebSMike Smith 
21863a31b7ebSMike Smith static void
21873a31b7ebSMike Smith ciss_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
21883a31b7ebSMike Smith {
2189e010b6bcSPaul Saab     xpt_free_path(ccb->ccb_h.path);
21903a31b7ebSMike Smith     free(ccb, M_TEMP);
21913a31b7ebSMike Smith }
21923a31b7ebSMike Smith 
21933a31b7ebSMike Smith /************************************************************************
21943a31b7ebSMike Smith  * Handle requests coming from CAM
21953a31b7ebSMike Smith  */
21963a31b7ebSMike Smith static void
21973a31b7ebSMike Smith ciss_cam_action(struct cam_sim *sim, union ccb *ccb)
21983a31b7ebSMike Smith {
2199440baa08SPaul Saab     struct ciss_softc	*sc;
2200440baa08SPaul Saab     struct ccb_scsiio	*csio;
2201440baa08SPaul Saab     int			target;
2202440baa08SPaul Saab 
2203440baa08SPaul Saab     sc = cam_sim_softc(sim);
2204440baa08SPaul Saab     csio = (struct ccb_scsiio *)&ccb->csio;
2205440baa08SPaul Saab     target = csio->ccb_h.target_id;
2206440baa08SPaul Saab 
22073a31b7ebSMike Smith     switch (ccb->ccb_h.func_code) {
22083a31b7ebSMike Smith 
22093a31b7ebSMike Smith 	/* perform SCSI I/O */
22103a31b7ebSMike Smith     case XPT_SCSI_IO:
2211440baa08SPaul Saab 	if (!ciss_cam_action_io(sim, csio))
22123a31b7ebSMike Smith 	    return;
22133a31b7ebSMike Smith 	break;
22143a31b7ebSMike Smith 
22153a31b7ebSMike Smith 	/* perform geometry calculations */
22163a31b7ebSMike Smith     case XPT_CALC_GEOMETRY:
22173a31b7ebSMike Smith     {
22183a31b7ebSMike Smith 	struct ccb_calc_geometry	*ccg = &ccb->ccg;
2219440baa08SPaul Saab 	struct ciss_ldrive		*ld = &sc->ciss_logical[target];
22203a31b7ebSMike Smith 
22213a31b7ebSMike Smith 	debug(1, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
22223a31b7ebSMike Smith 
22233a31b7ebSMike Smith 	/*
2224440baa08SPaul Saab 	 * Use the cached geometry settings unless the fault tolerance
2225440baa08SPaul Saab 	 * is invalid.
22263a31b7ebSMike Smith 	 */
2227440baa08SPaul Saab 	if (ld->cl_geometry.fault_tolerance == 0xFF) {
2228440baa08SPaul Saab 	    u_int32_t			secs_per_cylinder;
2229440baa08SPaul Saab 
22303a31b7ebSMike Smith 	    ccg->heads = 255;
22313a31b7ebSMike Smith 	    ccg->secs_per_track = 32;
22323a31b7ebSMike Smith 	    secs_per_cylinder = ccg->heads * ccg->secs_per_track;
22333a31b7ebSMike Smith 	    ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2234440baa08SPaul Saab 	} else {
2235440baa08SPaul Saab 	    ccg->heads = ld->cl_geometry.heads;
2236440baa08SPaul Saab 	    ccg->secs_per_track = ld->cl_geometry.sectors;
2237440baa08SPaul Saab 	    ccg->cylinders = ntohs(ld->cl_geometry.cylinders);
2238440baa08SPaul Saab 	}
22393a31b7ebSMike Smith 	ccb->ccb_h.status = CAM_REQ_CMP;
22403a31b7ebSMike Smith         break;
22413a31b7ebSMike Smith     }
22423a31b7ebSMike Smith 
22433a31b7ebSMike Smith 	/* handle path attribute inquiry */
22443a31b7ebSMike Smith     case XPT_PATH_INQ:
22453a31b7ebSMike Smith     {
22463a31b7ebSMike Smith 	struct ccb_pathinq	*cpi = &ccb->cpi;
22473a31b7ebSMike Smith 
22483a31b7ebSMike Smith 	debug(1, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
22493a31b7ebSMike Smith 
22503a31b7ebSMike Smith 	cpi->version_num = 1;
22513a31b7ebSMike Smith 	cpi->hba_inquiry = PI_TAG_ABLE;	/* XXX is this correct? */
22523a31b7ebSMike Smith 	cpi->target_sprt = 0;
22533a31b7ebSMike Smith 	cpi->hba_misc = 0;
22543a31b7ebSMike Smith 	cpi->max_target = CISS_MAX_LOGICAL;
22553a31b7ebSMike Smith 	cpi->max_lun = 0;		/* 'logical drive' channel only */
22563a31b7ebSMike Smith 	cpi->initiator_id = CISS_MAX_LOGICAL;
22573a31b7ebSMike Smith 	strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
22583a31b7ebSMike Smith         strncpy(cpi->hba_vid, "msmith@freebsd.org", HBA_IDLEN);
22593a31b7ebSMike Smith         strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
22603a31b7ebSMike Smith         cpi->unit_number = cam_sim_unit(sim);
22613a31b7ebSMike Smith         cpi->bus_id = cam_sim_bus(sim);
22623a31b7ebSMike Smith 	cpi->base_transfer_speed = 132 * 1024;	/* XXX what to set this to? */
22633a31b7ebSMike Smith 	ccb->ccb_h.status = CAM_REQ_CMP;
22643a31b7ebSMike Smith 	break;
22653a31b7ebSMike Smith     }
22663a31b7ebSMike Smith 
22673a31b7ebSMike Smith     case XPT_GET_TRAN_SETTINGS:
22683a31b7ebSMike Smith     {
22693a31b7ebSMike Smith 	struct ccb_trans_settings	*cts = &ccb->cts;
22703a31b7ebSMike Smith 	int				bus, target;
22713a31b7ebSMike Smith 
22723a31b7ebSMike Smith 	bus = cam_sim_bus(sim);
22733a31b7ebSMike Smith 	target = cts->ccb_h.target_id;
22743a31b7ebSMike Smith 
22753a31b7ebSMike Smith 	debug(1, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target);
22763a31b7ebSMike Smith 	cts->valid = 0;
22773a31b7ebSMike Smith 
22783a31b7ebSMike Smith 	/* disconnect always OK */
22793a31b7ebSMike Smith 	cts->flags |= CCB_TRANS_DISC_ENB;
22803a31b7ebSMike Smith 	cts->valid |= CCB_TRANS_DISC_VALID;
22813a31b7ebSMike Smith 
22823a31b7ebSMike Smith 	cts->ccb_h.status = CAM_REQ_CMP;
22833a31b7ebSMike Smith 	break;
22843a31b7ebSMike Smith     }
22853a31b7ebSMike Smith 
22863a31b7ebSMike Smith     default:		/* we can't do this */
22873a31b7ebSMike Smith 	debug(1, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
22883a31b7ebSMike Smith 	ccb->ccb_h.status = CAM_REQ_INVALID;
22893a31b7ebSMike Smith 	break;
22903a31b7ebSMike Smith     }
22913a31b7ebSMike Smith 
22923a31b7ebSMike Smith     xpt_done(ccb);
22933a31b7ebSMike Smith }
22943a31b7ebSMike Smith 
22953a31b7ebSMike Smith /************************************************************************
22963a31b7ebSMike Smith  * Handle a CAM SCSI I/O request.
22973a31b7ebSMike Smith  */
22983a31b7ebSMike Smith static int
22993a31b7ebSMike Smith ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio)
23003a31b7ebSMike Smith {
23013a31b7ebSMike Smith     struct ciss_softc	*sc;
23023a31b7ebSMike Smith     int			bus, target;
23033a31b7ebSMike Smith     struct ciss_request	*cr;
23043a31b7ebSMike Smith     struct ciss_command	*cc;
23053a31b7ebSMike Smith     int			error;
23063a31b7ebSMike Smith 
23073a31b7ebSMike Smith     sc = cam_sim_softc(sim);
23083a31b7ebSMike Smith     bus = cam_sim_bus(sim);
23093a31b7ebSMike Smith     target = csio->ccb_h.target_id;
23103a31b7ebSMike Smith 
23113a31b7ebSMike Smith     debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, csio->ccb_h.target_lun);
23123a31b7ebSMike Smith 
23133a31b7ebSMike Smith     /* check for I/O attempt to nonexistent device */
23143a31b7ebSMike Smith     if ((bus != 0) ||
2315777d1b39SPoul-Henning Kamp 	(target >= CISS_MAX_LOGICAL) ||
23163a31b7ebSMike Smith 	(sc->ciss_logical[target].cl_status == CISS_LD_NONEXISTENT)) {
23173a31b7ebSMike Smith 	debug(3, "  device does not exist");
23183a31b7ebSMike Smith 	csio->ccb_h.status = CAM_REQ_CMP_ERR;
23193a31b7ebSMike Smith     }
23203a31b7ebSMike Smith 
23213a31b7ebSMike Smith     /* firmware does not support commands > 10 bytes */
23223a31b7ebSMike Smith     if (csio->cdb_len > 12/*CISS_CDB_BUFFER_SIZE*/) {
23233a31b7ebSMike Smith 	debug(3, "  command too large (%d > %d)", csio->cdb_len, CISS_CDB_BUFFER_SIZE);
23243a31b7ebSMike Smith 	csio->ccb_h.status = CAM_REQ_CMP_ERR;
23253a31b7ebSMike Smith     }
23263a31b7ebSMike Smith 
23273a31b7ebSMike Smith     /* check that the CDB pointer is not to a physical address */
23283a31b7ebSMike Smith     if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
23293a31b7ebSMike Smith 	debug(3, "  CDB pointer is to physical address");
23303a31b7ebSMike Smith 	csio->ccb_h.status = CAM_REQ_CMP_ERR;
23313a31b7ebSMike Smith     }
23323a31b7ebSMike Smith 
23333a31b7ebSMike Smith     /* if there is data transfer, it must be to/from a virtual address */
23343a31b7ebSMike Smith     if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
23353a31b7ebSMike Smith 	if (csio->ccb_h.flags & CAM_DATA_PHYS) {		/* we can't map it */
23363a31b7ebSMike Smith 	    debug(3, "  data pointer is to physical address");
23373a31b7ebSMike Smith 	    csio->ccb_h.status = CAM_REQ_CMP_ERR;
23383a31b7ebSMike Smith 	}
23393a31b7ebSMike Smith 	if (csio->ccb_h.flags & CAM_SCATTER_VALID) {	/* we want to do the s/g setup */
23403a31b7ebSMike Smith 	    debug(3, "  data has premature s/g setup");
23413a31b7ebSMike Smith 	    csio->ccb_h.status = CAM_REQ_CMP_ERR;
23423a31b7ebSMike Smith 	}
23433a31b7ebSMike Smith     }
23443a31b7ebSMike Smith 
23453a31b7ebSMike Smith     /* abandon aborted ccbs or those that have failed validation */
23463a31b7ebSMike Smith     if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
23473a31b7ebSMike Smith 	debug(3, "abandoning CCB due to abort/validation failure");
23483a31b7ebSMike Smith 	return(EINVAL);
23493a31b7ebSMike Smith     }
23503a31b7ebSMike Smith 
23513a31b7ebSMike Smith     /* handle emulation of some SCSI commands ourself */
23523a31b7ebSMike Smith     if (ciss_cam_emulate(sc, csio))
23533a31b7ebSMike Smith 	return(0);
23543a31b7ebSMike Smith 
23553a31b7ebSMike Smith     /*
23563a31b7ebSMike Smith      * Get a request to manage this command.  If we can't, return the
23573a31b7ebSMike Smith      * ccb, freeze the queue and flag so that we unfreeze it when a
23583a31b7ebSMike Smith      * request completes.
23593a31b7ebSMike Smith      */
23603a31b7ebSMike Smith     if ((error = ciss_get_request(sc, &cr)) != 0) {
23613a31b7ebSMike Smith 	xpt_freeze_simq(sc->ciss_cam_sim, 1);
23623a31b7ebSMike Smith 	csio->ccb_h.status |= CAM_REQUEUE_REQ;
23633a31b7ebSMike Smith 	return(error);
23643a31b7ebSMike Smith     }
23653a31b7ebSMike Smith 
23663a31b7ebSMike Smith     /*
23673a31b7ebSMike Smith      * Build the command.
23683a31b7ebSMike Smith      */
23693a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
23703a31b7ebSMike Smith     cr->cr_data = csio->data_ptr;
23713a31b7ebSMike Smith     cr->cr_length = csio->dxfer_len;
23723a31b7ebSMike Smith     cr->cr_complete = ciss_cam_complete;
23733a31b7ebSMike Smith     cr->cr_private = csio;
23743a31b7ebSMike Smith 
23753a31b7ebSMike Smith     cc->header.address.logical.mode = CISS_HDR_ADDRESS_MODE_LOGICAL;
23763a31b7ebSMike Smith     cc->header.address.logical.lun = target;
23773a31b7ebSMike Smith     cc->cdb.cdb_length = csio->cdb_len;
23783a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
23793a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;	/* XXX ordered tags? */
23803a31b7ebSMike Smith     if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
23813a31b7ebSMike Smith 	cr->cr_flags = CISS_REQ_DATAOUT;
23823a31b7ebSMike Smith 	cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
23833a31b7ebSMike Smith     } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
23843a31b7ebSMike Smith 	cr->cr_flags = CISS_REQ_DATAIN;
23853a31b7ebSMike Smith 	cc->cdb.direction = CISS_CDB_DIRECTION_READ;
23863a31b7ebSMike Smith     } else {
23873a31b7ebSMike Smith 	cr->cr_flags = 0;
23883a31b7ebSMike Smith 	cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
23893a31b7ebSMike Smith     }
23903a31b7ebSMike Smith     cc->cdb.timeout = (csio->ccb_h.timeout / 1000) + 1;
23913a31b7ebSMike Smith     if (csio->ccb_h.flags & CAM_CDB_POINTER) {
23923a31b7ebSMike Smith 	bcopy(csio->cdb_io.cdb_ptr, &cc->cdb.cdb[0], csio->cdb_len);
23933a31b7ebSMike Smith     } else {
23943a31b7ebSMike Smith 	bcopy(csio->cdb_io.cdb_bytes, &cc->cdb.cdb[0], csio->cdb_len);
23953a31b7ebSMike Smith     }
23963a31b7ebSMike Smith 
23973a31b7ebSMike Smith     /*
23983a31b7ebSMike Smith      * Submit the request to the adapter.
23993a31b7ebSMike Smith      *
24003a31b7ebSMike Smith      * Note that this may fail if we're unable to map the request (and
24013a31b7ebSMike Smith      * if we ever learn a transport layer other than simple, may fail
24023a31b7ebSMike Smith      * if the adapter rejects the command).
24033a31b7ebSMike Smith      */
24043a31b7ebSMike Smith     if ((error = ciss_start(cr)) != 0) {
24053a31b7ebSMike Smith 	xpt_freeze_simq(sc->ciss_cam_sim, 1);
2406639717c8SPaul Saab 	if (error == EINPROGRESS) {
2407639717c8SPaul Saab 	    csio->ccb_h.status |= CAM_RELEASE_SIMQ;
2408639717c8SPaul Saab 	    error = 0;
2409639717c8SPaul Saab 	} else {
24103a31b7ebSMike Smith 	    csio->ccb_h.status |= CAM_REQUEUE_REQ;
24113a31b7ebSMike Smith 	    ciss_release_request(cr);
2412639717c8SPaul Saab 	}
24133a31b7ebSMike Smith 	return(error);
24143a31b7ebSMike Smith     }
24153a31b7ebSMike Smith 
24163a31b7ebSMike Smith     return(0);
24173a31b7ebSMike Smith }
24183a31b7ebSMike Smith 
24193a31b7ebSMike Smith /************************************************************************
24203a31b7ebSMike Smith  * Emulate SCSI commands the adapter doesn't handle as we might like.
24213a31b7ebSMike Smith  */
24223a31b7ebSMike Smith static int
24233a31b7ebSMike Smith ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio)
24243a31b7ebSMike Smith {
24253a31b7ebSMike Smith     int		target;
24263a31b7ebSMike Smith     u_int8_t	opcode;
24273a31b7ebSMike Smith 
24283a31b7ebSMike Smith 
24293a31b7ebSMike Smith     target = csio->ccb_h.target_id;
24303a31b7ebSMike Smith     opcode = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
24313a31b7ebSMike Smith 	*(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0];
24323a31b7ebSMike Smith 
24333a31b7ebSMike Smith     /*
24343a31b7ebSMike Smith      * Handle requests for volumes that don't exist.  A selection timeout
24353a31b7ebSMike Smith      * is slightly better than an illegal request.  Other errors might be
24363a31b7ebSMike Smith      * better.
24373a31b7ebSMike Smith      */
24383a31b7ebSMike Smith     if (sc->ciss_logical[target].cl_status == CISS_LD_NONEXISTENT) {
24393a31b7ebSMike Smith 	csio->ccb_h.status = CAM_SEL_TIMEOUT;
24403a31b7ebSMike Smith 	xpt_done((union ccb *)csio);
24413a31b7ebSMike Smith 	return(1);
24423a31b7ebSMike Smith     }
24433a31b7ebSMike Smith 
24443a31b7ebSMike Smith     /*
24453a31b7ebSMike Smith      * Handle requests for volumes that exist but are offline.
24463a31b7ebSMike Smith      *
24473a31b7ebSMike Smith      * I/O operations should fail, everything else should work.
24483a31b7ebSMike Smith      */
24493a31b7ebSMike Smith     if (sc->ciss_logical[target].cl_status == CISS_LD_OFFLINE) {
24503a31b7ebSMike Smith 	switch(opcode) {
24513a31b7ebSMike Smith 	case READ_6:
24523a31b7ebSMike Smith 	case READ_10:
24533a31b7ebSMike Smith 	case READ_12:
24543a31b7ebSMike Smith 	case WRITE_6:
24553a31b7ebSMike Smith 	case WRITE_10:
24563a31b7ebSMike Smith 	case WRITE_12:
24573a31b7ebSMike Smith 	    csio->ccb_h.status = CAM_SEL_TIMEOUT;
24583a31b7ebSMike Smith 	    xpt_done((union ccb *)csio);
24593a31b7ebSMike Smith 	    return(1);
24603a31b7ebSMike Smith 	}
24613a31b7ebSMike Smith     }
24623a31b7ebSMike Smith 
24633a31b7ebSMike Smith 
24643a31b7ebSMike Smith     /* if we have to fake Synchronise Cache */
24653a31b7ebSMike Smith     if (sc->ciss_flags & CISS_FLAG_FAKE_SYNCH) {
24663a31b7ebSMike Smith 
24673a31b7ebSMike Smith 	/*
24683a31b7ebSMike Smith 	 * If this is a Synchronise Cache command, typically issued when
24693a31b7ebSMike Smith 	 * a device is closed, flush the adapter and complete now.
24703a31b7ebSMike Smith 	 */
24713a31b7ebSMike Smith 	if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
24723a31b7ebSMike Smith 	     *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE) {
24733a31b7ebSMike Smith 	    ciss_flush_adapter(sc);
24743a31b7ebSMike Smith 	    csio->ccb_h.status = CAM_REQ_CMP;
24753a31b7ebSMike Smith 	    xpt_done((union ccb *)csio);
24763a31b7ebSMike Smith 	    return(1);
24773a31b7ebSMike Smith 	}
24783a31b7ebSMike Smith     }
24793a31b7ebSMike Smith 
24803a31b7ebSMike Smith     return(0);
24813a31b7ebSMike Smith }
24823a31b7ebSMike Smith 
24833a31b7ebSMike Smith /************************************************************************
24843a31b7ebSMike Smith  * Check for possibly-completed commands.
24853a31b7ebSMike Smith  */
24863a31b7ebSMike Smith static void
24873a31b7ebSMike Smith ciss_cam_poll(struct cam_sim *sim)
24883a31b7ebSMike Smith {
24893a31b7ebSMike Smith     struct ciss_softc	*sc = cam_sim_softc(sim);
24903a31b7ebSMike Smith 
24913a31b7ebSMike Smith     debug_called(2);
24923a31b7ebSMike Smith 
24933a31b7ebSMike Smith     ciss_done(sc);
24943a31b7ebSMike Smith }
24953a31b7ebSMike Smith 
24963a31b7ebSMike Smith /************************************************************************
24973a31b7ebSMike Smith  * Handle completion of a command - pass results back through the CCB
24983a31b7ebSMike Smith  */
24993a31b7ebSMike Smith static void
25003a31b7ebSMike Smith ciss_cam_complete(struct ciss_request *cr)
25013a31b7ebSMike Smith {
25023a31b7ebSMike Smith     struct ciss_softc		*sc;
25033a31b7ebSMike Smith     struct ciss_command		*cc;
25043a31b7ebSMike Smith     struct ciss_error_info	*ce;
25053a31b7ebSMike Smith     struct ccb_scsiio		*csio;
25063a31b7ebSMike Smith     int				scsi_status;
25073a31b7ebSMike Smith     int				command_status;
25083a31b7ebSMike Smith 
25093a31b7ebSMike Smith     debug_called(2);
25103a31b7ebSMike Smith 
25113a31b7ebSMike Smith     sc = cr->cr_sc;
25123a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
25133a31b7ebSMike Smith     ce = (struct ciss_error_info *)&(cc->sg[0]);
25143a31b7ebSMike Smith     csio = (struct ccb_scsiio *)cr->cr_private;
25153a31b7ebSMike Smith 
25163a31b7ebSMike Smith     /*
25173a31b7ebSMike Smith      * Extract status values from request.
25183a31b7ebSMike Smith      */
25193a31b7ebSMike Smith     ciss_report_request(cr, &command_status, &scsi_status);
25203a31b7ebSMike Smith     csio->scsi_status = scsi_status;
25213a31b7ebSMike Smith 
25223a31b7ebSMike Smith     /*
25233a31b7ebSMike Smith      * Handle specific SCSI status values.
25243a31b7ebSMike Smith      */
25253a31b7ebSMike Smith     switch(scsi_status) {
25263a31b7ebSMike Smith 	/* no status due to adapter error */
25273a31b7ebSMike Smith     case -1:
25283a31b7ebSMike Smith 	debug(0, "adapter error");
25293a31b7ebSMike Smith 	csio->ccb_h.status = CAM_REQ_CMP_ERR;
25303a31b7ebSMike Smith 	break;
25313a31b7ebSMike Smith 
25323a31b7ebSMike Smith 	/* no status due to command completed OK */
25333a31b7ebSMike Smith     case SCSI_STATUS_OK:		/* CISS_SCSI_STATUS_GOOD */
25343a31b7ebSMike Smith 	debug(2, "SCSI_STATUS_OK");
25353a31b7ebSMike Smith 	csio->ccb_h.status = CAM_REQ_CMP;
25363a31b7ebSMike Smith 	break;
25373a31b7ebSMike Smith 
25383a31b7ebSMike Smith 	/* check condition, sense data included */
25393a31b7ebSMike Smith     case SCSI_STATUS_CHECK_COND:	/* CISS_SCSI_STATUS_CHECK_CONDITION */
25403a31b7ebSMike Smith 	debug(0, "SCSI_STATUS_CHECK_COND  sense size %d  resid %d",
25413a31b7ebSMike Smith 	      ce->sense_length, ce->residual_count);
25423a31b7ebSMike Smith 	bzero(&csio->sense_data, SSD_FULL_SIZE);
25433a31b7ebSMike Smith 	bcopy(&ce->sense_info[0], &csio->sense_data, ce->sense_length);
25443a31b7ebSMike Smith 	csio->sense_len = ce->sense_length;
25453a31b7ebSMike Smith 	csio->resid = ce->residual_count;
25463a31b7ebSMike Smith 	csio->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
25473a31b7ebSMike Smith #ifdef CISS_DEBUG
25483a31b7ebSMike Smith 	{
25493a31b7ebSMike Smith 	    struct scsi_sense_data	*sns = (struct scsi_sense_data *)&ce->sense_info[0];
25503a31b7ebSMike Smith 	    debug(0, "sense key %x", sns->flags & SSD_KEY);
25513a31b7ebSMike Smith 	}
25523a31b7ebSMike Smith #endif
25533a31b7ebSMike Smith 	break;
25543a31b7ebSMike Smith 
25553a31b7ebSMike Smith     case SCSI_STATUS_BUSY:		/* CISS_SCSI_STATUS_BUSY */
25563a31b7ebSMike Smith 	debug(0, "SCSI_STATUS_BUSY");
25573a31b7ebSMike Smith 	csio->ccb_h.status = CAM_SCSI_BUSY;
25583a31b7ebSMike Smith 	break;
25593a31b7ebSMike Smith 
25603a31b7ebSMike Smith     default:
25613a31b7ebSMike Smith 	debug(0, "unknown status 0x%x", csio->scsi_status);
25623a31b7ebSMike Smith 	csio->ccb_h.status = CAM_REQ_CMP_ERR;
25633a31b7ebSMike Smith 	break;
25643a31b7ebSMike Smith     }
25653a31b7ebSMike Smith 
25663a31b7ebSMike Smith     /* handle post-command fixup */
25673a31b7ebSMike Smith     ciss_cam_complete_fixup(sc, csio);
25683a31b7ebSMike Smith 
2569d4aa427fSPaul Saab     /* tell CAM we're ready for more commands */
2570d4aa427fSPaul Saab     csio->ccb_h.status |= CAM_RELEASE_SIMQ;
2571d4aa427fSPaul Saab 
25723a31b7ebSMike Smith     xpt_done((union ccb *)csio);
25733a31b7ebSMike Smith     ciss_release_request(cr);
25743a31b7ebSMike Smith }
25753a31b7ebSMike Smith 
25763a31b7ebSMike Smith /********************************************************************************
25773a31b7ebSMike Smith  * Fix up the result of some commands here.
25783a31b7ebSMike Smith  */
25793a31b7ebSMike Smith static void
25803a31b7ebSMike Smith ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio)
25813a31b7ebSMike Smith {
25823a31b7ebSMike Smith     struct scsi_inquiry_data	*inq;
25833a31b7ebSMike Smith     struct ciss_ldrive		*cl;
25843a31b7ebSMike Smith     int				target;
25853a31b7ebSMike Smith 
25863a31b7ebSMike Smith     if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
25873a31b7ebSMike Smith 	 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == INQUIRY) {
25883a31b7ebSMike Smith 
25893a31b7ebSMike Smith 	inq = (struct scsi_inquiry_data *)csio->data_ptr;
25903a31b7ebSMike Smith 	target = csio->ccb_h.target_id;
25913a31b7ebSMike Smith 	cl = &sc->ciss_logical[target];
25923a31b7ebSMike Smith 
2593d4aa427fSPaul Saab 	padstr(inq->vendor, "COMPAQ", 8);
2594d4aa427fSPaul Saab 	padstr(inq->product, ciss_name_ldrive_org(cl->cl_ldrive->fault_tolerance), 8);
2595d4aa427fSPaul Saab 	padstr(inq->revision, ciss_name_ldrive_status(cl->cl_lstatus->status), 16);
25963a31b7ebSMike Smith     }
25973a31b7ebSMike Smith }
25983a31b7ebSMike Smith 
25993a31b7ebSMike Smith 
26003a31b7ebSMike Smith /********************************************************************************
2601d4aa427fSPaul Saab  * Find a peripheral attached at (target)
26023a31b7ebSMike Smith  */
26033a31b7ebSMike Smith static struct cam_periph *
26043a31b7ebSMike Smith ciss_find_periph(struct ciss_softc *sc, int target)
26053a31b7ebSMike Smith {
26063a31b7ebSMike Smith     struct cam_periph	*periph;
26073a31b7ebSMike Smith     struct cam_path	*path;
26083a31b7ebSMike Smith     int			status;
26093a31b7ebSMike Smith 
26103a31b7ebSMike Smith     status = xpt_create_path(&path, NULL, cam_sim_path(sc->ciss_cam_sim), target, 0);
26113a31b7ebSMike Smith     if (status == CAM_REQ_CMP) {
26123a31b7ebSMike Smith 	periph = cam_periph_find(path, NULL);
26133a31b7ebSMike Smith 	xpt_free_path(path);
26143a31b7ebSMike Smith     } else {
26153a31b7ebSMike Smith 	periph = NULL;
26163a31b7ebSMike Smith     }
26173a31b7ebSMike Smith     return(periph);
26183a31b7ebSMike Smith }
26193a31b7ebSMike Smith 
26203a31b7ebSMike Smith /********************************************************************************
26213a31b7ebSMike Smith  * Name the device at (target)
26223a31b7ebSMike Smith  *
26233a31b7ebSMike Smith  * XXX is this strictly correct?
26243a31b7ebSMike Smith  */
262537c84183SPoul-Henning Kamp static int
26263a31b7ebSMike Smith ciss_name_device(struct ciss_softc *sc, int target)
26273a31b7ebSMike Smith {
26283a31b7ebSMike Smith     struct cam_periph	*periph;
26293a31b7ebSMike Smith 
26303a31b7ebSMike Smith     if ((periph = ciss_find_periph(sc, target)) != NULL) {
26313a31b7ebSMike Smith 	sprintf(sc->ciss_logical[target].cl_name, "%s%d", periph->periph_name, periph->unit_number);
26323a31b7ebSMike Smith 	return(0);
26333a31b7ebSMike Smith     }
26343a31b7ebSMike Smith     sc->ciss_logical[target].cl_name[0] = 0;
26353a31b7ebSMike Smith     return(ENOENT);
26363a31b7ebSMike Smith }
26373a31b7ebSMike Smith 
26383a31b7ebSMike Smith /************************************************************************
26393a31b7ebSMike Smith  * Periodic status monitoring.
26403a31b7ebSMike Smith  */
26413a31b7ebSMike Smith static void
26423a31b7ebSMike Smith ciss_periodic(void *arg)
26433a31b7ebSMike Smith {
26443a31b7ebSMike Smith     struct ciss_softc	*sc;
26453a31b7ebSMike Smith 
26463a31b7ebSMike Smith     debug_called(1);
26473a31b7ebSMike Smith 
26483a31b7ebSMike Smith     sc = (struct ciss_softc *)arg;
26493a31b7ebSMike Smith 
26503a31b7ebSMike Smith     /*
26513a31b7ebSMike Smith      * Check the adapter heartbeat.
26523a31b7ebSMike Smith      */
26533a31b7ebSMike Smith     if (sc->ciss_cfg->heartbeat == sc->ciss_heartbeat) {
26543a31b7ebSMike Smith 	sc->ciss_heart_attack++;
26553a31b7ebSMike Smith 	debug(0, "adapter heart attack in progress 0x%x/%d",
26563a31b7ebSMike Smith 	      sc->ciss_heartbeat, sc->ciss_heart_attack);
26573a31b7ebSMike Smith 	if (sc->ciss_heart_attack == 3) {
26583a31b7ebSMike Smith 	    ciss_printf(sc, "ADAPTER HEARTBEAT FAILED\n");
26593a31b7ebSMike Smith 	    /* XXX should reset adapter here */
26603a31b7ebSMike Smith 	}
26613a31b7ebSMike Smith     } else {
26623a31b7ebSMike Smith 	sc->ciss_heartbeat = sc->ciss_cfg->heartbeat;
26633a31b7ebSMike Smith 	sc->ciss_heart_attack = 0;
26643a31b7ebSMike Smith 	debug(3, "new heartbeat 0x%x", sc->ciss_heartbeat);
26653a31b7ebSMike Smith     }
26663a31b7ebSMike Smith 
26673a31b7ebSMike Smith     /*
26683a31b7ebSMike Smith      * If the notify event request has died for some reason, or has
26693a31b7ebSMike Smith      * not started yet, restart it.
26703a31b7ebSMike Smith      */
26713a31b7ebSMike Smith     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK)) {
26723a31b7ebSMike Smith 	debug(0, "(re)starting Event Notify chain");
26733a31b7ebSMike Smith 	ciss_notify_event(sc);
26743a31b7ebSMike Smith     }
26753a31b7ebSMike Smith 
26763a31b7ebSMike Smith     /*
26773a31b7ebSMike Smith      * Reschedule.
26783a31b7ebSMike Smith      */
26793a31b7ebSMike Smith     if (!(sc->ciss_flags & CISS_FLAG_ABORTING))
26803a31b7ebSMike Smith 	sc->ciss_periodic = timeout(ciss_periodic, sc, CISS_HEARTBEAT_RATE * hz);
26813a31b7ebSMike Smith }
26823a31b7ebSMike Smith 
26833a31b7ebSMike Smith /************************************************************************
26843a31b7ebSMike Smith  * Request a notification response from the adapter.
26853a31b7ebSMike Smith  *
26863a31b7ebSMike Smith  * If (cr) is NULL, this is the first request of the adapter, so
26873a31b7ebSMike Smith  * reset the adapter's message pointer and start with the oldest
26883a31b7ebSMike Smith  * message available.
26893a31b7ebSMike Smith  */
26903a31b7ebSMike Smith static void
26913a31b7ebSMike Smith ciss_notify_event(struct ciss_softc *sc)
26923a31b7ebSMike Smith {
26933a31b7ebSMike Smith     struct ciss_request		*cr;
26943a31b7ebSMike Smith     struct ciss_command		*cc;
26953a31b7ebSMike Smith     struct ciss_notify_cdb	*cnc;
26963a31b7ebSMike Smith     int				error;
26973a31b7ebSMike Smith 
26983a31b7ebSMike Smith     debug_called(1);
26993a31b7ebSMike Smith 
27003a31b7ebSMike Smith     cr = sc->ciss_periodic_notify;
27013a31b7ebSMike Smith 
27023a31b7ebSMike Smith     /* get a request if we don't already have one */
27033a31b7ebSMike Smith     if (cr == NULL) {
27043a31b7ebSMike Smith 	if ((error = ciss_get_request(sc, &cr)) != 0) {
27053a31b7ebSMike Smith 	    debug(0, "can't get notify event request");
27063a31b7ebSMike Smith 	    goto out;
27073a31b7ebSMike Smith 	}
27083a31b7ebSMike Smith 	sc->ciss_periodic_notify = cr;
27093a31b7ebSMike Smith 	cr->cr_complete = ciss_notify_complete;
27103a31b7ebSMike Smith 	debug(1, "acquired request %d", cr->cr_tag);
27113a31b7ebSMike Smith     }
27123a31b7ebSMike Smith 
27133a31b7ebSMike Smith     /*
27143a31b7ebSMike Smith      * Get a databuffer if we don't already have one, note that the
27153a31b7ebSMike Smith      * adapter command wants a larger buffer than the actual
27163a31b7ebSMike Smith      * structure.
27173a31b7ebSMike Smith      */
27183a31b7ebSMike Smith     if (cr->cr_data == NULL) {
27193a31b7ebSMike Smith 	if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
27203a31b7ebSMike Smith 	    debug(0, "can't get notify event request buffer");
27213a31b7ebSMike Smith 	    error = ENOMEM;
27223a31b7ebSMike Smith 	    goto out;
27233a31b7ebSMike Smith 	}
27243a31b7ebSMike Smith 	cr->cr_length = CISS_NOTIFY_DATA_SIZE;
27253a31b7ebSMike Smith     }
27263a31b7ebSMike Smith 
27273a31b7ebSMike Smith     /* re-setup the request's command (since we never release it) XXX overkill*/
27283a31b7ebSMike Smith     ciss_preen_command(cr);
27293a31b7ebSMike Smith 
27303a31b7ebSMike Smith     /* (re)build the notify event command */
27313a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
27323a31b7ebSMike Smith     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
27333a31b7ebSMike Smith     cc->header.address.physical.bus = 0;
27343a31b7ebSMike Smith     cc->header.address.physical.target = 0;
27353a31b7ebSMike Smith 
27363a31b7ebSMike Smith     cc->cdb.cdb_length = sizeof(*cnc);
27373a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
27383a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
27393a31b7ebSMike Smith     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
27403a31b7ebSMike Smith     cc->cdb.timeout = 0;	/* no timeout, we hope */
27413a31b7ebSMike Smith 
27423a31b7ebSMike Smith     cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
27433a31b7ebSMike Smith     bzero(cr->cr_data, CISS_NOTIFY_DATA_SIZE);
27443a31b7ebSMike Smith     cnc->opcode = CISS_OPCODE_READ;
27453a31b7ebSMike Smith     cnc->command = CISS_COMMAND_NOTIFY_ON_EVENT;
27463a31b7ebSMike Smith     cnc->timeout = 0;		/* no timeout, we hope */
27473a31b7ebSMike Smith     cnc->synchronous = 0;
27483a31b7ebSMike Smith     cnc->ordered = 0;
27493a31b7ebSMike Smith     cnc->seek_to_oldest = 0;
27503a31b7ebSMike Smith     cnc->new_only = 0;
27513a31b7ebSMike Smith     cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
27523a31b7ebSMike Smith 
27533a31b7ebSMike Smith     /* submit the request */
27543a31b7ebSMike Smith     error = ciss_start(cr);
27553a31b7ebSMike Smith 
27563a31b7ebSMike Smith  out:
27573a31b7ebSMike Smith     if (error) {
27583a31b7ebSMike Smith 	if (cr != NULL) {
27593a31b7ebSMike Smith 	    if (cr->cr_data != NULL)
27603a31b7ebSMike Smith 		free(cr->cr_data, CISS_MALLOC_CLASS);
27613a31b7ebSMike Smith 	    ciss_release_request(cr);
27623a31b7ebSMike Smith 	}
27633a31b7ebSMike Smith 	sc->ciss_periodic_notify = NULL;
27643a31b7ebSMike Smith 	debug(0, "can't submit notify event request");
27653a31b7ebSMike Smith 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
27663a31b7ebSMike Smith     } else {
27673a31b7ebSMike Smith 	debug(1, "notify event submitted");
27683a31b7ebSMike Smith 	sc->ciss_flags |= CISS_FLAG_NOTIFY_OK;
27693a31b7ebSMike Smith     }
27703a31b7ebSMike Smith }
27713a31b7ebSMike Smith 
27723a31b7ebSMike Smith static void
27733a31b7ebSMike Smith ciss_notify_complete(struct ciss_request *cr)
27743a31b7ebSMike Smith {
27753a31b7ebSMike Smith     struct ciss_command	*cc;
27763a31b7ebSMike Smith     struct ciss_notify	*cn;
27773a31b7ebSMike Smith     struct ciss_softc	*sc;
27783a31b7ebSMike Smith     int			scsi_status;
27793a31b7ebSMike Smith     int			command_status;
27803a31b7ebSMike Smith 
27813a31b7ebSMike Smith     debug_called(1);
27823a31b7ebSMike Smith 
27833a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
27843a31b7ebSMike Smith     cn = (struct ciss_notify *)cr->cr_data;
27853a31b7ebSMike Smith     sc = cr->cr_sc;
27863a31b7ebSMike Smith 
27873a31b7ebSMike Smith     /*
27883a31b7ebSMike Smith      * Report request results, decode status.
27893a31b7ebSMike Smith      */
27903a31b7ebSMike Smith     ciss_report_request(cr, &command_status, &scsi_status);
27913a31b7ebSMike Smith 
27923a31b7ebSMike Smith     /*
27933a31b7ebSMike Smith      * Abort the chain on a fatal error.
27943a31b7ebSMike Smith      *
27953a31b7ebSMike Smith      * XXX which of these are actually errors?
27963a31b7ebSMike Smith      */
27973a31b7ebSMike Smith     if ((command_status != CISS_CMD_STATUS_SUCCESS) &&
27983a31b7ebSMike Smith 	(command_status != CISS_CMD_STATUS_TARGET_STATUS) &&
27993a31b7ebSMike Smith 	(command_status != CISS_CMD_STATUS_TIMEOUT)) {	/* XXX timeout? */
28003a31b7ebSMike Smith 	ciss_printf(sc, "fatal error in Notify Event request (%s)\n",
28013a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
28023a31b7ebSMike Smith 	ciss_release_request(cr);
28033a31b7ebSMike Smith 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
28043a31b7ebSMike Smith 	return;
28053a31b7ebSMike Smith     }
28063a31b7ebSMike Smith 
28073a31b7ebSMike Smith     /*
28083a31b7ebSMike Smith      * If the adapter gave us a text message, print it.
28093a31b7ebSMike Smith      */
28103a31b7ebSMike Smith     if (cn->message[0] != 0)
28113a31b7ebSMike Smith 	ciss_printf(sc, "*** %.80s\n", cn->message);
28123a31b7ebSMike Smith 
28133a31b7ebSMike Smith     debug(0, "notify event class %d subclass %d detail %d",
28143a31b7ebSMike Smith 		cn->class, cn->subclass, cn->detail);
28153a31b7ebSMike Smith 
28163a31b7ebSMike Smith     /*
28173a31b7ebSMike Smith      * If there's room, save the event for a user-level tool.
28183a31b7ebSMike Smith      */
28193a31b7ebSMike Smith     if (((sc->ciss_notify_head + 1) % CISS_MAX_EVENTS) != sc->ciss_notify_tail) {
28203a31b7ebSMike Smith 	sc->ciss_notify[sc->ciss_notify_head] = *cn;
28213a31b7ebSMike Smith 	sc->ciss_notify_head = (sc->ciss_notify_head + 1) % CISS_MAX_EVENTS;
28223a31b7ebSMike Smith     }
28233a31b7ebSMike Smith 
28243a31b7ebSMike Smith     /*
28253a31b7ebSMike Smith      * Some events are directly of interest to us.
28263a31b7ebSMike Smith      */
28273a31b7ebSMike Smith     switch (cn->class) {
28283a31b7ebSMike Smith     case CISS_NOTIFY_LOGICAL:
28293a31b7ebSMike Smith 	ciss_notify_logical(sc, cn);
28303a31b7ebSMike Smith 	break;
28313a31b7ebSMike Smith     case CISS_NOTIFY_PHYSICAL:
28323a31b7ebSMike Smith 	ciss_notify_physical(sc, cn);
28333a31b7ebSMike Smith 	break;
28343a31b7ebSMike Smith     }
28353a31b7ebSMike Smith 
28363a31b7ebSMike Smith     /*
28373a31b7ebSMike Smith      * If the response indicates that the notifier has been aborted,
28383a31b7ebSMike Smith      * release the notifier command.
28393a31b7ebSMike Smith      */
28403a31b7ebSMike Smith     if ((cn->class == CISS_NOTIFY_NOTIFIER) &&
28413a31b7ebSMike Smith 	(cn->subclass == CISS_NOTIFY_NOTIFIER_STATUS) &&
28423a31b7ebSMike Smith 	(cn->detail == 1)) {
28433a31b7ebSMike Smith 	debug(0, "notifier exiting");
28443a31b7ebSMike Smith 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
28453a31b7ebSMike Smith 	ciss_release_request(cr);
28463a31b7ebSMike Smith 	sc->ciss_periodic_notify = NULL;
28473a31b7ebSMike Smith 	wakeup(&sc->ciss_periodic_notify);
28483a31b7ebSMike Smith     }
28493a31b7ebSMike Smith 
28503a31b7ebSMike Smith     /*
28513a31b7ebSMike Smith      * Send a new notify event command, if we're not aborting.
28523a31b7ebSMike Smith      */
28533a31b7ebSMike Smith     if (!(sc->ciss_flags & CISS_FLAG_ABORTING)) {
28543a31b7ebSMike Smith 	ciss_notify_event(sc);
28553a31b7ebSMike Smith     }
28563a31b7ebSMike Smith }
28573a31b7ebSMike Smith 
28583a31b7ebSMike Smith /************************************************************************
28593a31b7ebSMike Smith  * Abort the Notify Event chain.
28603a31b7ebSMike Smith  *
28613a31b7ebSMike Smith  * Note that we can't just abort the command in progress; we have to
28623a31b7ebSMike Smith  * explicitly issue an Abort Notify Event command in order for the
28633a31b7ebSMike Smith  * adapter to clean up correctly.
28643a31b7ebSMike Smith  *
28653a31b7ebSMike Smith  * If we are called with CISS_FLAG_ABORTING set in the adapter softc,
28663a31b7ebSMike Smith  * the chain will not restart itself.
28673a31b7ebSMike Smith  */
28683a31b7ebSMike Smith static int
28693a31b7ebSMike Smith ciss_notify_abort(struct ciss_softc *sc)
28703a31b7ebSMike Smith {
28713a31b7ebSMike Smith     struct ciss_request		*cr;
28723a31b7ebSMike Smith     struct ciss_command		*cc;
28733a31b7ebSMike Smith     struct ciss_notify_cdb	*cnc;
28743a31b7ebSMike Smith     int				error, s, command_status, scsi_status;
28753a31b7ebSMike Smith 
28763a31b7ebSMike Smith     debug_called(1);
28773a31b7ebSMike Smith 
28783a31b7ebSMike Smith     cr = NULL;
28793a31b7ebSMike Smith     error = 0;
28803a31b7ebSMike Smith 
28813a31b7ebSMike Smith     /* verify that there's an outstanding command */
28823a31b7ebSMike Smith     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
28833a31b7ebSMike Smith 	goto out;
28843a31b7ebSMike Smith 
28853a31b7ebSMike Smith     /* get a command to issue the abort with */
28863a31b7ebSMike Smith     if ((error = ciss_get_request(sc, &cr)))
28873a31b7ebSMike Smith 	goto out;
28883a31b7ebSMike Smith 
28893a31b7ebSMike Smith     /* get a buffer for the result */
28903a31b7ebSMike Smith     if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
28913a31b7ebSMike Smith 	debug(0, "can't get notify event request buffer");
28923a31b7ebSMike Smith 	error = ENOMEM;
28933a31b7ebSMike Smith 	goto out;
28943a31b7ebSMike Smith     }
28953a31b7ebSMike Smith     cr->cr_length = CISS_NOTIFY_DATA_SIZE;
28963a31b7ebSMike Smith 
28973a31b7ebSMike Smith     /* build the CDB */
28983a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
28993a31b7ebSMike Smith     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
29003a31b7ebSMike Smith     cc->header.address.physical.bus = 0;
29013a31b7ebSMike Smith     cc->header.address.physical.target = 0;
29023a31b7ebSMike Smith     cc->cdb.cdb_length = sizeof(*cnc);
29033a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
29043a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
29053a31b7ebSMike Smith     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
29063a31b7ebSMike Smith     cc->cdb.timeout = 0;	/* no timeout, we hope */
29073a31b7ebSMike Smith 
29083a31b7ebSMike Smith     cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
29093a31b7ebSMike Smith     bzero(cnc, sizeof(*cnc));
29103a31b7ebSMike Smith     cnc->opcode = CISS_OPCODE_WRITE;
29113a31b7ebSMike Smith     cnc->command = CISS_COMMAND_ABORT_NOTIFY;
29123a31b7ebSMike Smith     cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
29133a31b7ebSMike Smith 
29143a31b7ebSMike Smith     ciss_print_request(cr);
29153a31b7ebSMike Smith 
29163a31b7ebSMike Smith     /*
29173a31b7ebSMike Smith      * Submit the request and wait for it to complete.
29183a31b7ebSMike Smith      */
29193a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
29203a31b7ebSMike Smith 	ciss_printf(sc, "Abort Notify Event command failed (%d)\n", error);
29213a31b7ebSMike Smith 	goto out;
29223a31b7ebSMike Smith     }
29233a31b7ebSMike Smith 
29243a31b7ebSMike Smith     /*
29253a31b7ebSMike Smith      * Check response.
29263a31b7ebSMike Smith      */
29273a31b7ebSMike Smith     ciss_report_request(cr, &command_status, &scsi_status);
29283a31b7ebSMike Smith     switch(command_status) {
29293a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:
29303a31b7ebSMike Smith 	break;
29313a31b7ebSMike Smith     case CISS_CMD_STATUS_INVALID_COMMAND:
29323a31b7ebSMike Smith 	/*
29333a31b7ebSMike Smith 	 * Some older adapters don't support the CISS version of this
29343a31b7ebSMike Smith 	 * command.  Fall back to using the BMIC version.
29353a31b7ebSMike Smith 	 */
29363a31b7ebSMike Smith 	error = ciss_notify_abort_bmic(sc);
29373a31b7ebSMike Smith 	if (error != 0)
29383a31b7ebSMike Smith 	    goto out;
29393a31b7ebSMike Smith 	break;
29403a31b7ebSMike Smith 
29413a31b7ebSMike Smith     case CISS_CMD_STATUS_TARGET_STATUS:
29423a31b7ebSMike Smith 	/*
29433a31b7ebSMike Smith 	 * This can happen if the adapter thinks there wasn't an outstanding
29443a31b7ebSMike Smith 	 * Notify Event command but we did.  We clean up here.
29453a31b7ebSMike Smith 	 */
29463a31b7ebSMike Smith 	if (scsi_status == CISS_SCSI_STATUS_CHECK_CONDITION) {
29473a31b7ebSMike Smith 	    if (sc->ciss_periodic_notify != NULL)
29483a31b7ebSMike Smith 		ciss_release_request(sc->ciss_periodic_notify);
29493a31b7ebSMike Smith 	    error = 0;
29503a31b7ebSMike Smith 	    goto out;
29513a31b7ebSMike Smith 	}
29523a31b7ebSMike Smith 	/* FALLTHROUGH */
29533a31b7ebSMike Smith 
29543a31b7ebSMike Smith     default:
29553a31b7ebSMike Smith 	ciss_printf(sc, "Abort Notify Event command failed (%s)\n",
29563a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
29573a31b7ebSMike Smith 	error = EIO;
29583a31b7ebSMike Smith 	goto out;
29593a31b7ebSMike Smith     }
29603a31b7ebSMike Smith 
29613a31b7ebSMike Smith     /*
29623a31b7ebSMike Smith      * Sleep waiting for the notifier command to complete.  Note
29633a31b7ebSMike Smith      * that if it doesn't, we may end up in a bad situation, since
29643a31b7ebSMike Smith      * the adapter may deliver it later.  Also note that the adapter
29653a31b7ebSMike Smith      * requires the Notify Event command to be cancelled in order to
29663a31b7ebSMike Smith      * maintain internal bookkeeping.
29673a31b7ebSMike Smith      */
29683a31b7ebSMike Smith     s = splcam();
29693a31b7ebSMike Smith     while (sc->ciss_periodic_notify != NULL) {
29703a31b7ebSMike Smith 	error = tsleep(&sc->ciss_periodic_notify, 0, "cissNEA", hz * 5);
29713a31b7ebSMike Smith 	if (error == EWOULDBLOCK) {
29723a31b7ebSMike Smith 	    ciss_printf(sc, "Notify Event command failed to abort, adapter may wedge.\n");
29733a31b7ebSMike Smith 	    break;
29743a31b7ebSMike Smith 	}
29753a31b7ebSMike Smith     }
29763a31b7ebSMike Smith     splx(s);
29773a31b7ebSMike Smith 
29783a31b7ebSMike Smith  out:
29793a31b7ebSMike Smith     /* release the cancel request */
29803a31b7ebSMike Smith     if (cr != NULL) {
29813a31b7ebSMike Smith 	if (cr->cr_data != NULL)
29823a31b7ebSMike Smith 	    free(cr->cr_data, CISS_MALLOC_CLASS);
29833a31b7ebSMike Smith 	ciss_release_request(cr);
29843a31b7ebSMike Smith     }
29853a31b7ebSMike Smith     if (error == 0)
29863a31b7ebSMike Smith 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
29873a31b7ebSMike Smith     return(error);
29883a31b7ebSMike Smith }
29893a31b7ebSMike Smith 
29903a31b7ebSMike Smith /************************************************************************
29913a31b7ebSMike Smith  * Abort the Notify Event chain using a BMIC command.
29923a31b7ebSMike Smith  */
29933a31b7ebSMike Smith static int
29943a31b7ebSMike Smith ciss_notify_abort_bmic(struct ciss_softc *sc)
29953a31b7ebSMike Smith {
29963a31b7ebSMike Smith     struct ciss_request			*cr;
29973a31b7ebSMike Smith     int					error, command_status;
29983a31b7ebSMike Smith 
29993a31b7ebSMike Smith     debug_called(1);
30003a31b7ebSMike Smith 
30013a31b7ebSMike Smith     cr = NULL;
30023a31b7ebSMike Smith     error = 0;
30033a31b7ebSMike Smith 
30043a31b7ebSMike Smith     /* verify that there's an outstanding command */
30053a31b7ebSMike Smith     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
30063a31b7ebSMike Smith 	goto out;
30073a31b7ebSMike Smith 
30083a31b7ebSMike Smith     /*
30093a31b7ebSMike Smith      * Build a BMIC command to cancel the Notify on Event command.
30103a31b7ebSMike Smith      *
30113a31b7ebSMike Smith      * Note that we are sending a CISS opcode here.  Odd.
30123a31b7ebSMike Smith      */
30133a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_COMMAND_ABORT_NOTIFY,
30143a31b7ebSMike Smith 				       NULL, 0)) != 0)
30153a31b7ebSMike Smith 	goto out;
30163a31b7ebSMike Smith 
30173a31b7ebSMike Smith     /*
30183a31b7ebSMike Smith      * Submit the request and wait for it to complete.
30193a31b7ebSMike Smith      */
30203a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
30213a31b7ebSMike Smith 	ciss_printf(sc, "error sending BMIC Cancel Notify on Event command (%d)\n", error);
30223a31b7ebSMike Smith 	goto out;
30233a31b7ebSMike Smith     }
30243a31b7ebSMike Smith 
30253a31b7ebSMike Smith     /*
30263a31b7ebSMike Smith      * Check response.
30273a31b7ebSMike Smith      */
30283a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
30293a31b7ebSMike Smith     switch(command_status) {
30303a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:
30313a31b7ebSMike Smith 	break;
30323a31b7ebSMike Smith     default:
30333a31b7ebSMike Smith 	ciss_printf(sc, "error cancelling Notify on Event (%s)\n",
30343a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
30353a31b7ebSMike Smith 	error = EIO;
30363a31b7ebSMike Smith 	goto out;
30373a31b7ebSMike Smith     }
30383a31b7ebSMike Smith 
30393a31b7ebSMike Smith out:
30403a31b7ebSMike Smith     if (cr != NULL)
30413a31b7ebSMike Smith 	ciss_release_request(cr);
30423a31b7ebSMike Smith     return(error);
30433a31b7ebSMike Smith }
30443a31b7ebSMike Smith 
30453a31b7ebSMike Smith /************************************************************************
30463a31b7ebSMike Smith  * Handle a notify event relating to the status of a logical drive.
30473a31b7ebSMike Smith  *
30483a31b7ebSMike Smith  * XXX need to be able to defer some of these to properly handle
30493a31b7ebSMike Smith  *     calling the "ID Physical drive" command, unless the 'extended'
30503a31b7ebSMike Smith  *     drive IDs are always in BIG_MAP format.
30513a31b7ebSMike Smith  */
30523a31b7ebSMike Smith static void
30533a31b7ebSMike Smith ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn)
30543a31b7ebSMike Smith {
30553a31b7ebSMike Smith     struct ciss_ldrive	*ld;
30563a31b7ebSMike Smith     int			ostatus;
30573a31b7ebSMike Smith 
30583a31b7ebSMike Smith     debug_called(2);
30593a31b7ebSMike Smith 
30603a31b7ebSMike Smith     ld = &sc->ciss_logical[cn->data.logical_status.logical_drive];
30613a31b7ebSMike Smith 
30623a31b7ebSMike Smith     switch (cn->subclass) {
30633a31b7ebSMike Smith     case CISS_NOTIFY_LOGICAL_STATUS:
30643a31b7ebSMike Smith 	switch (cn->detail) {
30653a31b7ebSMike Smith 	case 0:
30663a31b7ebSMike Smith 	    ciss_name_device(sc, cn->data.logical_status.logical_drive);
30673a31b7ebSMike Smith 	    ciss_printf(sc, "logical drive %d (%s) changed status %s->%s, spare status 0x%b\n",
30683a31b7ebSMike Smith 			cn->data.logical_status.logical_drive, ld->cl_name,
30693a31b7ebSMike Smith 			ciss_name_ldrive_status(cn->data.logical_status.previous_state),
30703a31b7ebSMike Smith 			ciss_name_ldrive_status(cn->data.logical_status.new_state),
30713a31b7ebSMike Smith 			cn->data.logical_status.spare_state,
30723a31b7ebSMike Smith 			"\20\1configured\2rebuilding\3failed\4in use\5available\n");
30733a31b7ebSMike Smith 
30743a31b7ebSMike Smith 	    /*
30753a31b7ebSMike Smith 	     * Update our idea of the drive's status.
30763a31b7ebSMike Smith 	     */
30773a31b7ebSMike Smith 	    ostatus = ciss_decode_ldrive_status(cn->data.logical_status.previous_state);
30783a31b7ebSMike Smith 	    ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
3079d4aa427fSPaul Saab 	    if (ld->cl_lstatus != NULL)
30803a31b7ebSMike Smith 		ld->cl_lstatus->status = cn->data.logical_status.new_state;
30813a31b7ebSMike Smith 
3082d4aa427fSPaul Saab #if 0
3083d4aa427fSPaul Saab 	    /*
3084d4aa427fSPaul Saab 	     * Have CAM rescan the drive if its status has changed.
3085d4aa427fSPaul Saab 	     */
3086d4aa427fSPaul Saab 	    if (ostatus != ld->cl_status)
3087d4aa427fSPaul Saab 		ciss_cam_rescan_target(sc, cn->data.logical_status.logical_drive);
3088d4aa427fSPaul Saab #endif
3089d4aa427fSPaul Saab 
30903a31b7ebSMike Smith 	    break;
30913a31b7ebSMike Smith 
30923a31b7ebSMike Smith 	case 1:	/* logical drive has recognised new media, needs Accept Media Exchange */
30933a31b7ebSMike Smith 	    ciss_name_device(sc, cn->data.logical_status.logical_drive);
30943a31b7ebSMike Smith 	    ciss_printf(sc, "logical drive %d (%s) media exchanged, ready to go online\n",
30953a31b7ebSMike Smith 			cn->data.logical_status.logical_drive, ld->cl_name);
30963a31b7ebSMike Smith 	    ciss_accept_media(sc, cn->data.logical_status.logical_drive, 1);
30973a31b7ebSMike Smith 	    break;
30983a31b7ebSMike Smith 
30993a31b7ebSMike Smith 	case 2:
31003a31b7ebSMike Smith 	case 3:
31013a31b7ebSMike Smith 	    ciss_printf(sc, "rebuild of logical drive %d (%s) failed due to %s error\n",
31023a31b7ebSMike Smith 			cn->data.rebuild_aborted.logical_drive,
31033a31b7ebSMike Smith 			sc->ciss_logical[cn->data.rebuild_aborted.logical_drive].cl_name,
31043a31b7ebSMike Smith 			(cn->detail == 2) ? "read" : "write");
31053a31b7ebSMike Smith 	    break;
31063a31b7ebSMike Smith 	}
31073a31b7ebSMike Smith 	break;
31083a31b7ebSMike Smith 
31093a31b7ebSMike Smith     case CISS_NOTIFY_LOGICAL_ERROR:
31103a31b7ebSMike Smith 	if (cn->detail == 0) {
31113a31b7ebSMike Smith 	    ciss_printf(sc, "FATAL I/O ERROR on logical drive %d (%s), SCSI port %d ID %d\n",
31123a31b7ebSMike Smith 			cn->data.io_error.logical_drive,
31133a31b7ebSMike Smith 			sc->ciss_logical[cn->data.io_error.logical_drive].cl_name,
31143a31b7ebSMike Smith 			cn->data.io_error.failure_bus,
31153a31b7ebSMike Smith 			cn->data.io_error.failure_drive);
31163a31b7ebSMike Smith 	    /* XXX should we take the drive down at this point, or will we be told? */
31173a31b7ebSMike Smith 	}
31183a31b7ebSMike Smith 	break;
31193a31b7ebSMike Smith 
31203a31b7ebSMike Smith     case CISS_NOTIFY_LOGICAL_SURFACE:
31213a31b7ebSMike Smith 	if (cn->detail == 0)
31223a31b7ebSMike Smith 	    ciss_printf(sc, "logical drive %d (%s) completed consistency initialisation\n",
31233a31b7ebSMike Smith 			cn->data.consistency_completed.logical_drive,
31243a31b7ebSMike Smith 			sc->ciss_logical[cn->data.consistency_completed.logical_drive].cl_name);
31253a31b7ebSMike Smith 	break;
31263a31b7ebSMike Smith     }
31273a31b7ebSMike Smith }
31283a31b7ebSMike Smith 
31293a31b7ebSMike Smith /************************************************************************
31303a31b7ebSMike Smith  * Handle a notify event relating to the status of a physical drive.
31313a31b7ebSMike Smith  */
31323a31b7ebSMike Smith static void
31333a31b7ebSMike Smith ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn)
31343a31b7ebSMike Smith {
31353a31b7ebSMike Smith 
31363a31b7ebSMike Smith }
31373a31b7ebSMike Smith 
31383a31b7ebSMike Smith /************************************************************************
31393a31b7ebSMike Smith  * Print a request.
31403a31b7ebSMike Smith  */
31413a31b7ebSMike Smith static void
31423a31b7ebSMike Smith ciss_print_request(struct ciss_request *cr)
31433a31b7ebSMike Smith {
31443a31b7ebSMike Smith     struct ciss_softc	*sc;
31453a31b7ebSMike Smith     struct ciss_command	*cc;
31463a31b7ebSMike Smith     int			i;
31473a31b7ebSMike Smith 
31483a31b7ebSMike Smith     sc = cr->cr_sc;
31493a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
31503a31b7ebSMike Smith 
31513a31b7ebSMike Smith     ciss_printf(sc, "REQUEST @ %p\n", cr);
31523a31b7ebSMike Smith     ciss_printf(sc, "  data %p/%d  tag %d  flags %b\n",
31533a31b7ebSMike Smith 	      cr->cr_data, cr->cr_length, cr->cr_tag, cr->cr_flags,
31543a31b7ebSMike Smith 	      "\20\1mapped\2sleep\3poll\4dataout\5datain\n");
31553a31b7ebSMike Smith     ciss_printf(sc, "  sg list/total %d/%d  host tag 0x%x\n",
31563a31b7ebSMike Smith 		cc->header.sg_in_list, cc->header.sg_total, cc->header.host_tag);
31573a31b7ebSMike Smith     switch(cc->header.address.mode.mode) {
31583a31b7ebSMike Smith     case CISS_HDR_ADDRESS_MODE_PERIPHERAL:
31593a31b7ebSMike Smith     case CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL:
31603a31b7ebSMike Smith 	ciss_printf(sc, "  physical bus %d target %d\n",
31613a31b7ebSMike Smith 		    cc->header.address.physical.bus, cc->header.address.physical.target);
31623a31b7ebSMike Smith 	break;
31633a31b7ebSMike Smith     case CISS_HDR_ADDRESS_MODE_LOGICAL:
31643a31b7ebSMike Smith 	ciss_printf(sc, "  logical unit %d\n", cc->header.address.logical.lun);
31653a31b7ebSMike Smith 	break;
31663a31b7ebSMike Smith     }
31673a31b7ebSMike Smith     ciss_printf(sc, "  %s cdb length %d type %s attribute %s\n",
31683a31b7ebSMike Smith 		(cc->cdb.direction == CISS_CDB_DIRECTION_NONE) ? "no-I/O" :
31693a31b7ebSMike Smith 		(cc->cdb.direction == CISS_CDB_DIRECTION_READ) ? "READ" :
31703a31b7ebSMike Smith 		(cc->cdb.direction == CISS_CDB_DIRECTION_WRITE) ? "WRITE" : "??",
31713a31b7ebSMike Smith 		cc->cdb.cdb_length,
31723a31b7ebSMike Smith 		(cc->cdb.type == CISS_CDB_TYPE_COMMAND) ? "command" :
31733a31b7ebSMike Smith 		(cc->cdb.type == CISS_CDB_TYPE_MESSAGE) ? "message" : "??",
31743a31b7ebSMike Smith 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_UNTAGGED) ? "untagged" :
31753a31b7ebSMike Smith 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_SIMPLE) ? "simple" :
31763a31b7ebSMike Smith 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_HEAD_OF_QUEUE) ? "head-of-queue" :
31773a31b7ebSMike Smith 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_ORDERED) ? "ordered" :
31783a31b7ebSMike Smith 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_AUTO_CONTINGENT) ? "auto-contingent" : "??");
31793a31b7ebSMike Smith     ciss_printf(sc, "  %*D\n", cc->cdb.cdb_length, &cc->cdb.cdb[0], " ");
31803a31b7ebSMike Smith 
31813a31b7ebSMike Smith     if (cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) {
31823a31b7ebSMike Smith 	/* XXX print error info */
31833a31b7ebSMike Smith     } else {
31843a31b7ebSMike Smith 	/* since we don't use chained s/g, don't support it here */
31853a31b7ebSMike Smith 	for (i = 0; i < cc->header.sg_in_list; i++) {
31863a31b7ebSMike Smith 	    if ((i % 4) == 0)
31873a31b7ebSMike Smith 		ciss_printf(sc, "   ");
31883a31b7ebSMike Smith 	    printf("0x%08x/%d ", (u_int32_t)cc->sg[i].address, cc->sg[i].length);
31893a31b7ebSMike Smith 	    if ((((i + 1) % 4) == 0) || (i == (cc->header.sg_in_list - 1)))
31903a31b7ebSMike Smith 		printf("\n");
31913a31b7ebSMike Smith 	}
31923a31b7ebSMike Smith     }
31933a31b7ebSMike Smith }
31943a31b7ebSMike Smith 
31953a31b7ebSMike Smith /************************************************************************
31963a31b7ebSMike Smith  * Print information about the status of a logical drive.
31973a31b7ebSMike Smith  */
31983a31b7ebSMike Smith static void
31993a31b7ebSMike Smith ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld)
32003a31b7ebSMike Smith {
32013a31b7ebSMike Smith     int		bus, target, i;
32023a31b7ebSMike Smith 
3203d4aa427fSPaul Saab     if (ld->cl_lstatus == NULL) {
3204d4aa427fSPaul Saab 	printf("does not exist\n");
3205d4aa427fSPaul Saab 	return;
3206d4aa427fSPaul Saab     }
3207d4aa427fSPaul Saab 
32083a31b7ebSMike Smith     /* print drive status */
32093a31b7ebSMike Smith     switch(ld->cl_lstatus->status) {
32103a31b7ebSMike Smith     case CISS_LSTATUS_OK:
32113a31b7ebSMike Smith 	printf("online\n");
32123a31b7ebSMike Smith 	break;
32133a31b7ebSMike Smith     case CISS_LSTATUS_INTERIM_RECOVERY:
32143a31b7ebSMike Smith 	printf("in interim recovery mode\n");
32153a31b7ebSMike Smith 	break;
32163a31b7ebSMike Smith     case CISS_LSTATUS_READY_RECOVERY:
32173a31b7ebSMike Smith 	printf("ready to begin recovery\n");
32183a31b7ebSMike Smith 	break;
32193a31b7ebSMike Smith     case CISS_LSTATUS_RECOVERING:
32203a31b7ebSMike Smith 	bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
32213a31b7ebSMike Smith 	target = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
32223a31b7ebSMike Smith 	printf("being recovered, working on physical drive %d.%d, %u blocks remaining\n",
32233a31b7ebSMike Smith 	       bus, target, ld->cl_lstatus->blocks_to_recover);
32243a31b7ebSMike Smith 	break;
32253a31b7ebSMike Smith     case CISS_LSTATUS_EXPANDING:
32263a31b7ebSMike Smith 	printf("being expanded, %u blocks remaining\n",
32273a31b7ebSMike Smith 	       ld->cl_lstatus->blocks_to_recover);
32283a31b7ebSMike Smith 	break;
32293a31b7ebSMike Smith     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
32303a31b7ebSMike Smith 	printf("queued for expansion\n");
32313a31b7ebSMike Smith 	break;
32323a31b7ebSMike Smith     case CISS_LSTATUS_FAILED:
32333a31b7ebSMike Smith 	printf("queued for expansion\n");
32343a31b7ebSMike Smith 	break;
32353a31b7ebSMike Smith     case CISS_LSTATUS_WRONG_PDRIVE:
32363a31b7ebSMike Smith 	printf("wrong physical drive inserted\n");
32373a31b7ebSMike Smith 	break;
32383a31b7ebSMike Smith     case CISS_LSTATUS_MISSING_PDRIVE:
32393a31b7ebSMike Smith 	printf("missing a needed physical drive\n");
32403a31b7ebSMike Smith 	break;
32413a31b7ebSMike Smith     case CISS_LSTATUS_BECOMING_READY:
32423a31b7ebSMike Smith 	printf("becoming ready\n");
32433a31b7ebSMike Smith 	break;
32443a31b7ebSMike Smith     }
32453a31b7ebSMike Smith 
3246d4aa427fSPaul Saab     /* print failed physical drives */
32473a31b7ebSMike Smith     for (i = 0; i < CISS_BIG_MAP_ENTRIES / 8; i++) {
32483a31b7ebSMike Smith 	bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_failure_map[i]);
32493a31b7ebSMike Smith 	target = CISS_BIG_MAP_TARGET(sc, ld->cl_lstatus->drive_failure_map[i]);
32503a31b7ebSMike Smith 	if (bus == -1)
32513a31b7ebSMike Smith 	    continue;
32523a31b7ebSMike Smith 	ciss_printf(sc, "physical drive %d:%d (%x) failed\n", bus, target,
32533a31b7ebSMike Smith 		    ld->cl_lstatus->drive_failure_map[i]);
32543a31b7ebSMike Smith     }
32553a31b7ebSMike Smith }
32563a31b7ebSMike Smith 
3257d4aa427fSPaul Saab #ifdef CISS_DEBUG
3258d4aa427fSPaul Saab /************************************************************************
3259d4aa427fSPaul Saab  * Print information about the controller/driver.
3260d4aa427fSPaul Saab  */
3261d4aa427fSPaul Saab static void
3262d4aa427fSPaul Saab ciss_print_adapter(struct ciss_softc *sc)
3263d4aa427fSPaul Saab {
3264d4aa427fSPaul Saab     int		i;
3265d4aa427fSPaul Saab 
3266d4aa427fSPaul Saab     ciss_printf(sc, "ADAPTER:\n");
3267d4aa427fSPaul Saab     for (i = 0; i < CISSQ_COUNT; i++) {
3268d4aa427fSPaul Saab 	ciss_printf(sc, "%s     %d/%d\n",
3269d4aa427fSPaul Saab 	    i == 0 ? "free" :
3270d4aa427fSPaul Saab 	    i == 1 ? "busy" : "complete",
3271d4aa427fSPaul Saab 	    sc->ciss_qstat[i].q_length,
3272d4aa427fSPaul Saab 	    sc->ciss_qstat[i].q_max);
3273d4aa427fSPaul Saab     }
3274d4aa427fSPaul Saab     ciss_printf(sc, "max_requests %d\n", sc->ciss_max_requests);
3275d4aa427fSPaul Saab     ciss_printf(sc, "notify_head/tail %d/%d\n",
3276d4aa427fSPaul Saab 	sc->ciss_notify_head, sc->ciss_notify_tail);
3277d4aa427fSPaul Saab     ciss_printf(sc, "flags %b\n", sc->ciss_flags,
3278d4aa427fSPaul Saab 	"\20\1notify_ok\2control_open\3aborting\4running\21fake_synch\22bmic_abort\n");
3279d4aa427fSPaul Saab 
3280d4aa427fSPaul Saab     for (i = 0; i < CISS_MAX_LOGICAL; i++) {
3281d4aa427fSPaul Saab 	ciss_printf(sc, "LOGICAL DRIVE %d:  ", i);
3282d4aa427fSPaul Saab 	ciss_print_ldrive(sc, sc->ciss_logical + i);
3283d4aa427fSPaul Saab     }
3284d4aa427fSPaul Saab 
3285d4aa427fSPaul Saab     for (i = 1; i < sc->ciss_max_requests; i++)
3286d4aa427fSPaul Saab 	ciss_print_request(sc->ciss_request + i);
3287d4aa427fSPaul Saab 
3288d4aa427fSPaul Saab }
3289d4aa427fSPaul Saab 
3290d4aa427fSPaul Saab /* DDB hook */
3291e6fccf7aSMaxime Henrion static void
3292d4aa427fSPaul Saab ciss_print0(void)
3293d4aa427fSPaul Saab {
3294d4aa427fSPaul Saab     struct ciss_softc	*sc;
3295d4aa427fSPaul Saab 
3296d4aa427fSPaul Saab     sc = devclass_get_softc(devclass_find("ciss"), 0);
3297d4aa427fSPaul Saab     if (sc == NULL) {
3298d4aa427fSPaul Saab 	printf("no ciss controllers\n");
3299d4aa427fSPaul Saab     } else {
3300d4aa427fSPaul Saab 	ciss_print_adapter(sc);
3301d4aa427fSPaul Saab     }
3302d4aa427fSPaul Saab }
3303d4aa427fSPaul Saab #endif
3304d4aa427fSPaul Saab 
33053a31b7ebSMike Smith /************************************************************************
33063a31b7ebSMike Smith  * Return a name for a logical drive status value.
33073a31b7ebSMike Smith  */
33083a31b7ebSMike Smith static const char *
33093a31b7ebSMike Smith ciss_name_ldrive_status(int status)
33103a31b7ebSMike Smith {
33113a31b7ebSMike Smith     switch (status) {
33123a31b7ebSMike Smith     case CISS_LSTATUS_OK:
33133a31b7ebSMike Smith 	return("OK");
33143a31b7ebSMike Smith     case CISS_LSTATUS_FAILED:
33153a31b7ebSMike Smith 	return("failed");
33163a31b7ebSMike Smith     case CISS_LSTATUS_NOT_CONFIGURED:
33173a31b7ebSMike Smith 	return("not configured");
33183a31b7ebSMike Smith     case CISS_LSTATUS_INTERIM_RECOVERY:
33193a31b7ebSMike Smith 	return("interim recovery");
33203a31b7ebSMike Smith     case CISS_LSTATUS_READY_RECOVERY:
33213a31b7ebSMike Smith 	return("ready for recovery");
33223a31b7ebSMike Smith     case CISS_LSTATUS_RECOVERING:
33233a31b7ebSMike Smith 	return("recovering");
33243a31b7ebSMike Smith     case CISS_LSTATUS_WRONG_PDRIVE:
33253a31b7ebSMike Smith 	return("wrong physical drive inserted");
33263a31b7ebSMike Smith     case CISS_LSTATUS_MISSING_PDRIVE:
33273a31b7ebSMike Smith 	return("missing physical drive");
33283a31b7ebSMike Smith     case CISS_LSTATUS_EXPANDING:
33293a31b7ebSMike Smith 	return("expanding");
33303a31b7ebSMike Smith     case CISS_LSTATUS_BECOMING_READY:
33313a31b7ebSMike Smith 	return("becoming ready");
33323a31b7ebSMike Smith     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
33333a31b7ebSMike Smith 	return("queued for expansion");
33343a31b7ebSMike Smith     }
33353a31b7ebSMike Smith     return("unknown status");
33363a31b7ebSMike Smith }
33373a31b7ebSMike Smith 
33383a31b7ebSMike Smith /************************************************************************
33393a31b7ebSMike Smith  * Return an online/offline/nonexistent value for a logical drive
33403a31b7ebSMike Smith  * status value.
33413a31b7ebSMike Smith  */
33423a31b7ebSMike Smith static int
33433a31b7ebSMike Smith ciss_decode_ldrive_status(int status)
33443a31b7ebSMike Smith {
33453a31b7ebSMike Smith     switch(status) {
33463a31b7ebSMike Smith     case CISS_LSTATUS_NOT_CONFIGURED:
33473a31b7ebSMike Smith 	return(CISS_LD_NONEXISTENT);
33483a31b7ebSMike Smith 
33493a31b7ebSMike Smith     case CISS_LSTATUS_OK:
33503a31b7ebSMike Smith     case CISS_LSTATUS_INTERIM_RECOVERY:
33513a31b7ebSMike Smith     case CISS_LSTATUS_READY_RECOVERY:
33523a31b7ebSMike Smith     case CISS_LSTATUS_RECOVERING:
33533a31b7ebSMike Smith     case CISS_LSTATUS_EXPANDING:
33543a31b7ebSMike Smith     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
33553a31b7ebSMike Smith 	return(CISS_LD_ONLINE);
33563a31b7ebSMike Smith 
33573a31b7ebSMike Smith     case CISS_LSTATUS_FAILED:
33583a31b7ebSMike Smith     case CISS_LSTATUS_WRONG_PDRIVE:
33593a31b7ebSMike Smith     case CISS_LSTATUS_MISSING_PDRIVE:
33603a31b7ebSMike Smith     case CISS_LSTATUS_BECOMING_READY:
33613a31b7ebSMike Smith     default:
33623a31b7ebSMike Smith 	return(CISS_LD_OFFLINE);
33633a31b7ebSMike Smith     }
33643a31b7ebSMike Smith }
33653a31b7ebSMike Smith 
33663a31b7ebSMike Smith 
33673a31b7ebSMike Smith /************************************************************************
33683a31b7ebSMike Smith  * Return a name for a logical drive's organisation.
33693a31b7ebSMike Smith  */
33703a31b7ebSMike Smith static const char *
33713a31b7ebSMike Smith ciss_name_ldrive_org(int org)
33723a31b7ebSMike Smith {
33733a31b7ebSMike Smith     switch(org) {
33743a31b7ebSMike Smith     case CISS_LDRIVE_RAID0:
33753a31b7ebSMike Smith 	return("RAID 0");
33763a31b7ebSMike Smith     case CISS_LDRIVE_RAID1:
33773a31b7ebSMike Smith 	return("RAID 1");
33783a31b7ebSMike Smith     case CISS_LDRIVE_RAID4:
33793a31b7ebSMike Smith 	return("RAID 4");
33803a31b7ebSMike Smith     case CISS_LDRIVE_RAID5:
33813a31b7ebSMike Smith 	return("RAID 5");
33823a31b7ebSMike Smith     }
33833a31b7ebSMike Smith     return("unkown");
33843a31b7ebSMike Smith }
33853a31b7ebSMike Smith 
33863a31b7ebSMike Smith /************************************************************************
33873a31b7ebSMike Smith  * Return a name for a command status value.
33883a31b7ebSMike Smith  */
33893a31b7ebSMike Smith static const char *
33903a31b7ebSMike Smith ciss_name_command_status(int status)
33913a31b7ebSMike Smith {
33923a31b7ebSMike Smith     switch(status) {
33933a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:
33943a31b7ebSMike Smith 	return("success");
33953a31b7ebSMike Smith     case CISS_CMD_STATUS_TARGET_STATUS:
33963a31b7ebSMike Smith 	return("target status");
33973a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_UNDERRUN:
33983a31b7ebSMike Smith 	return("data underrun");
33993a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_OVERRUN:
34003a31b7ebSMike Smith 	return("data overrun");
34013a31b7ebSMike Smith     case CISS_CMD_STATUS_INVALID_COMMAND:
34023a31b7ebSMike Smith 	return("invalid command");
34033a31b7ebSMike Smith     case CISS_CMD_STATUS_PROTOCOL_ERROR:
34043a31b7ebSMike Smith 	return("protocol error");
34053a31b7ebSMike Smith     case CISS_CMD_STATUS_HARDWARE_ERROR:
34063a31b7ebSMike Smith 	return("hardware error");
34073a31b7ebSMike Smith     case CISS_CMD_STATUS_CONNECTION_LOST:
34083a31b7ebSMike Smith 	return("connection lost");
34093a31b7ebSMike Smith     case CISS_CMD_STATUS_ABORTED:
34103a31b7ebSMike Smith 	return("aborted");
34113a31b7ebSMike Smith     case CISS_CMD_STATUS_ABORT_FAILED:
34123a31b7ebSMike Smith 	return("abort failed");
34133a31b7ebSMike Smith     case CISS_CMD_STATUS_UNSOLICITED_ABORT:
34143a31b7ebSMike Smith 	return("unsolicited abort");
34153a31b7ebSMike Smith     case CISS_CMD_STATUS_TIMEOUT:
34163a31b7ebSMike Smith 	return("timeout");
34173a31b7ebSMike Smith     case CISS_CMD_STATUS_UNABORTABLE:
34183a31b7ebSMike Smith 	return("unabortable");
34193a31b7ebSMike Smith     }
34203a31b7ebSMike Smith     return("unknown status");
34213a31b7ebSMike Smith }
34223a31b7ebSMike Smith 
34233a31b7ebSMike Smith /************************************************************************
34243a31b7ebSMike Smith  * Handle an open on the control device.
34253a31b7ebSMike Smith  */
34263a31b7ebSMike Smith static int
3427d3e4392cSMike Smith ciss_open(dev_t dev, int flags, int fmt, d_thread_t *p)
34283a31b7ebSMike Smith {
34293a31b7ebSMike Smith     struct ciss_softc	*sc;
34303a31b7ebSMike Smith 
34313a31b7ebSMike Smith     debug_called(1);
34323a31b7ebSMike Smith 
34333a31b7ebSMike Smith     sc = (struct ciss_softc *)dev->si_drv1;
34343a31b7ebSMike Smith 
34353a31b7ebSMike Smith     /* we might want to veto if someone already has us open */
34363a31b7ebSMike Smith 
34373a31b7ebSMike Smith     sc->ciss_flags |= CISS_FLAG_CONTROL_OPEN;
34383a31b7ebSMike Smith     return(0);
34393a31b7ebSMike Smith }
34403a31b7ebSMike Smith 
34413a31b7ebSMike Smith /************************************************************************
34423a31b7ebSMike Smith  * Handle the last close on the control device.
34433a31b7ebSMike Smith  */
34443a31b7ebSMike Smith static int
3445d3e4392cSMike Smith ciss_close(dev_t dev, int flags, int fmt, d_thread_t *p)
34463a31b7ebSMike Smith {
34473a31b7ebSMike Smith     struct ciss_softc	*sc;
34483a31b7ebSMike Smith 
34493a31b7ebSMike Smith     debug_called(1);
34503a31b7ebSMike Smith 
34513a31b7ebSMike Smith     sc = (struct ciss_softc *)dev->si_drv1;
34523a31b7ebSMike Smith 
34533a31b7ebSMike Smith     sc->ciss_flags &= ~CISS_FLAG_CONTROL_OPEN;
34543a31b7ebSMike Smith     return (0);
34553a31b7ebSMike Smith }
34563a31b7ebSMike Smith 
34573a31b7ebSMike Smith /********************************************************************************
34583a31b7ebSMike Smith  * Handle adapter-specific control operations.
34593a31b7ebSMike Smith  *
34603a31b7ebSMike Smith  * Note that the API here is compatible with the Linux driver, in order to
34613a31b7ebSMike Smith  * simplify the porting of Compaq's userland tools.
34623a31b7ebSMike Smith  */
34633a31b7ebSMike Smith static int
3464d3e4392cSMike Smith ciss_ioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, d_thread_t *p)
34653a31b7ebSMike Smith {
34663a31b7ebSMike Smith     struct ciss_softc		*sc;
34673a31b7ebSMike Smith     int				error;
34683a31b7ebSMike Smith 
34693a31b7ebSMike Smith     debug_called(1);
34703a31b7ebSMike Smith 
34713a31b7ebSMike Smith     sc = (struct ciss_softc *)dev->si_drv1;
34723a31b7ebSMike Smith     error = 0;
34733a31b7ebSMike Smith 
34743a31b7ebSMike Smith     switch(cmd) {
34753a31b7ebSMike Smith     case CCISS_GETPCIINFO:
34763a31b7ebSMike Smith     {
34773a31b7ebSMike Smith 	cciss_pci_info_struct	*pis = (cciss_pci_info_struct *)addr;
34783a31b7ebSMike Smith 
34793a31b7ebSMike Smith 	pis->bus = pci_get_bus(sc->ciss_dev);
34803a31b7ebSMike Smith 	pis->dev_fn = pci_get_slot(sc->ciss_dev);
34813a31b7ebSMike Smith 	pis->board_id = pci_get_devid(sc->ciss_dev);
34823a31b7ebSMike Smith 
34833a31b7ebSMike Smith 	break;
34843a31b7ebSMike Smith     }
34853a31b7ebSMike Smith 
34863a31b7ebSMike Smith     case CCISS_GETINTINFO:
34873a31b7ebSMike Smith     {
34883a31b7ebSMike Smith 	cciss_coalint_struct	*cis = (cciss_coalint_struct *)addr;
34893a31b7ebSMike Smith 
34903a31b7ebSMike Smith 	cis->delay = sc->ciss_cfg->interrupt_coalesce_delay;
34913a31b7ebSMike Smith 	cis->count = sc->ciss_cfg->interrupt_coalesce_count;
34923a31b7ebSMike Smith 
34933a31b7ebSMike Smith 	break;
34943a31b7ebSMike Smith     }
34953a31b7ebSMike Smith 
34963a31b7ebSMike Smith     case CCISS_SETINTINFO:
34973a31b7ebSMike Smith     {
34983a31b7ebSMike Smith 	cciss_coalint_struct	*cis = (cciss_coalint_struct *)addr;
34993a31b7ebSMike Smith 
35003a31b7ebSMike Smith 	if ((cis->delay == 0) && (cis->count == 0)) {
35013a31b7ebSMike Smith 	    error = EINVAL;
35023a31b7ebSMike Smith 	    break;
35033a31b7ebSMike Smith 	}
35043a31b7ebSMike Smith 
35053a31b7ebSMike Smith 	/*
35063a31b7ebSMike Smith 	 * XXX apparently this is only safe if the controller is idle,
35073a31b7ebSMike Smith 	 *     we should suspend it before doing this.
35083a31b7ebSMike Smith 	 */
35093a31b7ebSMike Smith 	sc->ciss_cfg->interrupt_coalesce_delay = cis->delay;
35103a31b7ebSMike Smith 	sc->ciss_cfg->interrupt_coalesce_count = cis->count;
35113a31b7ebSMike Smith 
35123a31b7ebSMike Smith 	if (ciss_update_config(sc))
35133a31b7ebSMike Smith 	    error = EIO;
35143a31b7ebSMike Smith 
35153a31b7ebSMike Smith 	/* XXX resume the controller here */
35163a31b7ebSMike Smith 	break;
35173a31b7ebSMike Smith     }
35183a31b7ebSMike Smith 
35193a31b7ebSMike Smith     case CCISS_GETNODENAME:
35203a31b7ebSMike Smith 	bcopy(sc->ciss_cfg->server_name, (NodeName_type *)addr,
35213a31b7ebSMike Smith 	      sizeof(NodeName_type));
35223a31b7ebSMike Smith 	break;
35233a31b7ebSMike Smith 
35243a31b7ebSMike Smith     case CCISS_SETNODENAME:
35253a31b7ebSMike Smith 	bcopy((NodeName_type *)addr, sc->ciss_cfg->server_name,
35263a31b7ebSMike Smith 	      sizeof(NodeName_type));
35273a31b7ebSMike Smith 	if (ciss_update_config(sc))
35283a31b7ebSMike Smith 	    error = EIO;
35293a31b7ebSMike Smith 	break;
35303a31b7ebSMike Smith 
35313a31b7ebSMike Smith     case CCISS_GETHEARTBEAT:
35323a31b7ebSMike Smith 	*(Heartbeat_type *)addr = sc->ciss_cfg->heartbeat;
35333a31b7ebSMike Smith 	break;
35343a31b7ebSMike Smith 
35353a31b7ebSMike Smith     case CCISS_GETBUSTYPES:
35363a31b7ebSMike Smith 	*(BusTypes_type *)addr = sc->ciss_cfg->bus_types;
35373a31b7ebSMike Smith 	break;
35383a31b7ebSMike Smith 
35393a31b7ebSMike Smith     case CCISS_GETFIRMVER:
35403a31b7ebSMike Smith 	bcopy(sc->ciss_id->running_firmware_revision, (FirmwareVer_type *)addr,
35413a31b7ebSMike Smith 	      sizeof(FirmwareVer_type));
35423a31b7ebSMike Smith 	break;
35433a31b7ebSMike Smith 
35443a31b7ebSMike Smith     case CCISS_GETDRIVERVER:
35453a31b7ebSMike Smith 	*(DriverVer_type *)addr = CISS_DRIVER_VERSION;
35463a31b7ebSMike Smith 	break;
35473a31b7ebSMike Smith 
35483a31b7ebSMike Smith     case CCISS_REVALIDVOLS:
35493a31b7ebSMike Smith 	/*
35503a31b7ebSMike Smith 	 * This is a bit ugly; to do it "right" we really need
35513a31b7ebSMike Smith 	 * to find any disks that have changed, kick CAM off them,
35523a31b7ebSMike Smith 	 * then rescan only these disks.  It'd be nice if they
35533a31b7ebSMike Smith 	 * a) told us which disk(s) they were going to play with,
35543a31b7ebSMike Smith 	 * and b) which ones had arrived. 8(
35553a31b7ebSMike Smith 	 */
35563a31b7ebSMike Smith 	break;
35573a31b7ebSMike Smith 
35583a31b7ebSMike Smith     case CCISS_PASSTHRU:
35593a31b7ebSMike Smith 	error = ciss_user_command(sc, (IOCTL_Command_struct *)addr);
35603a31b7ebSMike Smith 	break;
35613a31b7ebSMike Smith 
35623a31b7ebSMike Smith     default:
35633a31b7ebSMike Smith 	debug(0, "unknown ioctl 0x%lx", cmd);
35643a31b7ebSMike Smith 
35653a31b7ebSMike Smith 	debug(1, "CCISS_GETPCIINFO:   0x%lx", CCISS_GETPCIINFO);
35663a31b7ebSMike Smith 	debug(1, "CCISS_GETINTINFO:   0x%lx", CCISS_GETINTINFO);
35673a31b7ebSMike Smith 	debug(1, "CCISS_SETINTINFO:   0x%lx", CCISS_SETINTINFO);
35683a31b7ebSMike Smith 	debug(1, "CCISS_GETNODENAME:  0x%lx", CCISS_GETNODENAME);
35693a31b7ebSMike Smith 	debug(1, "CCISS_SETNODENAME:  0x%lx", CCISS_SETNODENAME);
35703a31b7ebSMike Smith 	debug(1, "CCISS_GETHEARTBEAT: 0x%lx", CCISS_GETHEARTBEAT);
35713a31b7ebSMike Smith 	debug(1, "CCISS_GETBUSTYPES:  0x%lx", CCISS_GETBUSTYPES);
35723a31b7ebSMike Smith 	debug(1, "CCISS_GETFIRMVER:   0x%lx", CCISS_GETFIRMVER);
35733a31b7ebSMike Smith 	debug(1, "CCISS_GETDRIVERVER: 0x%lx", CCISS_GETDRIVERVER);
35743a31b7ebSMike Smith 	debug(1, "CCISS_REVALIDVOLS:  0x%lx", CCISS_REVALIDVOLS);
35753a31b7ebSMike Smith 	debug(1, "CCISS_PASSTHRU:     0x%lx", CCISS_PASSTHRU);
35763a31b7ebSMike Smith 
35773a31b7ebSMike Smith 	error = ENOIOCTL;
35783a31b7ebSMike Smith 	break;
35793a31b7ebSMike Smith     }
35803a31b7ebSMike Smith 
35813a31b7ebSMike Smith     return(error);
35823a31b7ebSMike Smith }
3583