xref: /freebsd/sys/dev/ciss/ciss.c (revision 54d02e0aa7a441cd93b9c62b15906b7674286ed3)
13a31b7ebSMike Smith /*-
23a31b7ebSMike Smith  * Copyright (c) 2001 Michael Smith
3c6131460SPaul Saab  * Copyright (c) 2004 Paul Saab
43a31b7ebSMike Smith  * All rights reserved.
53a31b7ebSMike Smith  *
63a31b7ebSMike Smith  * Redistribution and use in source and binary forms, with or without
73a31b7ebSMike Smith  * modification, are permitted provided that the following conditions
83a31b7ebSMike Smith  * are met:
93a31b7ebSMike Smith  * 1. Redistributions of source code must retain the above copyright
103a31b7ebSMike Smith  *    notice, this list of conditions and the following disclaimer.
113a31b7ebSMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
123a31b7ebSMike Smith  *    notice, this list of conditions and the following disclaimer in the
133a31b7ebSMike Smith  *    documentation and/or other materials provided with the distribution.
143a31b7ebSMike Smith  *
153a31b7ebSMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
163a31b7ebSMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
173a31b7ebSMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
183a31b7ebSMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
193a31b7ebSMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
203a31b7ebSMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
213a31b7ebSMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
223a31b7ebSMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
233a31b7ebSMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
243a31b7ebSMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
253a31b7ebSMike Smith  * SUCH DAMAGE.
263a31b7ebSMike Smith  *
273a31b7ebSMike Smith  *	$FreeBSD$
283a31b7ebSMike Smith  */
293a31b7ebSMike Smith 
303a31b7ebSMike Smith /*
313a31b7ebSMike Smith  * Common Interface for SCSI-3 Support driver.
323a31b7ebSMike Smith  *
333a31b7ebSMike Smith  * CISS claims to provide a common interface between a generic SCSI
343a31b7ebSMike Smith  * transport and an intelligent host adapter.
353a31b7ebSMike Smith  *
363a31b7ebSMike Smith  * This driver supports CISS as defined in the document "CISS Command
373a31b7ebSMike Smith  * Interface for SCSI-3 Support Open Specification", Version 1.04,
383a31b7ebSMike Smith  * Valence Number 1, dated 20001127, produced by Compaq Computer
393a31b7ebSMike Smith  * Corporation.  This document appears to be a hastily and somewhat
403a31b7ebSMike Smith  * arbitrarlily cut-down version of a larger (and probably even more
413a31b7ebSMike Smith  * chaotic and inconsistent) Compaq internal document.  Various
423a31b7ebSMike Smith  * details were also gleaned from Compaq's "cciss" driver for Linux.
433a31b7ebSMike Smith  *
443a31b7ebSMike Smith  * We provide a shim layer between the CISS interface and CAM,
453a31b7ebSMike Smith  * offloading most of the queueing and being-a-disk chores onto CAM.
463a31b7ebSMike Smith  * Entry to the driver is via the PCI bus attachment (ciss_probe,
473a31b7ebSMike Smith  * ciss_attach, etc) and via the CAM interface (ciss_cam_action,
483a31b7ebSMike Smith  * ciss_cam_poll).  The Compaq CISS adapters are, however, poor SCSI
493a31b7ebSMike Smith  * citizens and we have to fake up some responses to get reasonable
503a31b7ebSMike Smith  * behaviour out of them.  In addition, the CISS command set is by no
513a31b7ebSMike Smith  * means adequate to support the functionality of a RAID controller,
523a31b7ebSMike Smith  * and thus the supported Compaq adapters utilise portions of the
533a31b7ebSMike Smith  * control protocol from earlier Compaq adapter families.
543a31b7ebSMike Smith  *
553a31b7ebSMike Smith  * Note that we only support the "simple" transport layer over PCI.
563a31b7ebSMike Smith  * This interface (ab)uses the I2O register set (specifically the post
573a31b7ebSMike Smith  * queues) to exchange commands with the adapter.  Other interfaces
583a31b7ebSMike Smith  * are available, but we aren't supposed to know about them, and it is
593a31b7ebSMike Smith  * dubious whether they would provide major performance improvements
603a31b7ebSMike Smith  * except under extreme load.
613a31b7ebSMike Smith  *
623a31b7ebSMike Smith  * Currently the only supported CISS adapters are the Compaq Smart
633a31b7ebSMike Smith  * Array 5* series (5300, 5i, 532).  Even with only three adapters,
643a31b7ebSMike Smith  * Compaq still manage to have interface variations.
653a31b7ebSMike Smith  *
663a31b7ebSMike Smith  *
673a31b7ebSMike Smith  * Thanks must go to Fred Harris and Darryl DeVinney at Compaq, as
683a31b7ebSMike Smith  * well as Paul Saab at Yahoo! for their assistance in making this
693a31b7ebSMike Smith  * driver happen.
70c6131460SPaul Saab  *
71c6131460SPaul Saab  * More thanks must go to John Cagle at HP for the countless hours
72c6131460SPaul Saab  * spent making this driver "work" with the MSA* series storage
73c6131460SPaul Saab  * enclosures.  Without his help (and nagging), this driver could not
74c6131460SPaul Saab  * be used with these enclosures.
753a31b7ebSMike Smith  */
763a31b7ebSMike Smith 
773a31b7ebSMike Smith #include <sys/param.h>
783a31b7ebSMike Smith #include <sys/systm.h>
793a31b7ebSMike Smith #include <sys/malloc.h>
803a31b7ebSMike Smith #include <sys/kernel.h>
813a31b7ebSMike Smith #include <sys/bus.h>
823a31b7ebSMike Smith #include <sys/conf.h>
833a31b7ebSMike Smith #include <sys/stat.h>
84c6131460SPaul Saab #include <sys/kthread.h>
85c6131460SPaul Saab #include <sys/queue.h>
8617c0792dSPaul Saab #include <sys/sysctl.h>
873a31b7ebSMike Smith 
883a31b7ebSMike Smith #include <cam/cam.h>
893a31b7ebSMike Smith #include <cam/cam_ccb.h>
903a31b7ebSMike Smith #include <cam/cam_periph.h>
913a31b7ebSMike Smith #include <cam/cam_sim.h>
923a31b7ebSMike Smith #include <cam/cam_xpt_sim.h>
933a31b7ebSMike Smith #include <cam/scsi/scsi_all.h>
943a31b7ebSMike Smith #include <cam/scsi/scsi_message.h>
953a31b7ebSMike Smith 
963a31b7ebSMike Smith #include <machine/bus.h>
973a31b7ebSMike Smith #include <machine/endian.h>
983a31b7ebSMike Smith #include <machine/resource.h>
993a31b7ebSMike Smith #include <sys/rman.h>
1003a31b7ebSMike Smith 
1014fbd232cSWarner Losh #include <dev/pci/pcireg.h>
1024fbd232cSWarner Losh #include <dev/pci/pcivar.h>
1033a31b7ebSMike Smith 
1043a31b7ebSMike Smith #include <dev/ciss/cissreg.h>
1053a31b7ebSMike Smith #include <dev/ciss/cissvar.h>
1063a31b7ebSMike Smith #include <dev/ciss/cissio.h>
1073a31b7ebSMike Smith 
1083a31b7ebSMike Smith MALLOC_DEFINE(CISS_MALLOC_CLASS, "ciss_data", "ciss internal data buffers");
1093a31b7ebSMike Smith 
1103a31b7ebSMike Smith /* pci interface */
1113a31b7ebSMike Smith static int	ciss_lookup(device_t dev);
1123a31b7ebSMike Smith static int	ciss_probe(device_t dev);
1133a31b7ebSMike Smith static int	ciss_attach(device_t dev);
1143a31b7ebSMike Smith static int	ciss_detach(device_t dev);
1153a31b7ebSMike Smith static int	ciss_shutdown(device_t dev);
1163a31b7ebSMike Smith 
1173a31b7ebSMike Smith /* (de)initialisation functions, control wrappers */
1183a31b7ebSMike Smith static int	ciss_init_pci(struct ciss_softc *sc);
1193a31b7ebSMike Smith static int	ciss_wait_adapter(struct ciss_softc *sc);
1203a31b7ebSMike Smith static int	ciss_flush_adapter(struct ciss_softc *sc);
1213a31b7ebSMike Smith static int	ciss_init_requests(struct ciss_softc *sc);
1223a31b7ebSMike Smith static void	ciss_command_map_helper(void *arg, bus_dma_segment_t *segs,
1233a31b7ebSMike Smith 					int nseg, int error);
1243a31b7ebSMike Smith static int	ciss_identify_adapter(struct ciss_softc *sc);
1253a31b7ebSMike Smith static int	ciss_init_logical(struct ciss_softc *sc);
126c6131460SPaul Saab static int	ciss_init_physical(struct ciss_softc *sc);
1271fe6c4eeSScott Long static int	ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll);
1283a31b7ebSMike Smith static int	ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld);
1293a31b7ebSMike Smith static int	ciss_get_ldrive_status(struct ciss_softc *sc,  struct ciss_ldrive *ld);
1303a31b7ebSMike Smith static int	ciss_update_config(struct ciss_softc *sc);
13188c78a38SPaul Saab static int	ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld);
13217c0792dSPaul Saab static void	ciss_init_sysctl(struct ciss_softc *sc);
13317c0792dSPaul Saab static void	ciss_soft_reset(struct ciss_softc *sc);
1343a31b7ebSMike Smith static void	ciss_free(struct ciss_softc *sc);
135c6131460SPaul Saab static void	ciss_spawn_notify_thread(struct ciss_softc *sc);
136c6131460SPaul Saab static void	ciss_kill_notify_thread(struct ciss_softc *sc);
1373a31b7ebSMike Smith 
1383a31b7ebSMike Smith /* request submission/completion */
1393a31b7ebSMike Smith static int	ciss_start(struct ciss_request *cr);
1403a31b7ebSMike Smith static void	ciss_done(struct ciss_softc *sc);
1413a31b7ebSMike Smith static void	ciss_intr(void *arg);
1423a31b7ebSMike Smith static void	ciss_complete(struct ciss_softc *sc);
1433a31b7ebSMike Smith static int	ciss_report_request(struct ciss_request *cr, int *command_status,
1443a31b7ebSMike Smith 				    int *scsi_status);
1453a31b7ebSMike Smith static int	ciss_synch_request(struct ciss_request *cr, int timeout);
1463a31b7ebSMike Smith static int	ciss_poll_request(struct ciss_request *cr, int timeout);
1473a31b7ebSMike Smith static int	ciss_wait_request(struct ciss_request *cr, int timeout);
1486197ca15SPeter Wemm #if 0
1493a31b7ebSMike Smith static int	ciss_abort_request(struct ciss_request *cr);
1506197ca15SPeter Wemm #endif
1513a31b7ebSMike Smith 
1523a31b7ebSMike Smith /* request queueing */
1533a31b7ebSMike Smith static int	ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp);
1543a31b7ebSMike Smith static void	ciss_preen_command(struct ciss_request *cr);
1553a31b7ebSMike Smith static void 	ciss_release_request(struct ciss_request *cr);
1563a31b7ebSMike Smith 
1573a31b7ebSMike Smith /* request helpers */
1583a31b7ebSMike Smith static int	ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
1593a31b7ebSMike Smith 				      int opcode, void **bufp, size_t bufsize);
1603a31b7ebSMike Smith static int	ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc);
1613a31b7ebSMike Smith 
1623a31b7ebSMike Smith /* DMA map/unmap */
1633a31b7ebSMike Smith static int	ciss_map_request(struct ciss_request *cr);
1643a31b7ebSMike Smith static void	ciss_request_map_helper(void *arg, bus_dma_segment_t *segs,
1653a31b7ebSMike Smith 					int nseg, int error);
1663a31b7ebSMike Smith static void	ciss_unmap_request(struct ciss_request *cr);
1673a31b7ebSMike Smith 
1683a31b7ebSMike Smith /* CAM interface */
1693a31b7ebSMike Smith static int	ciss_cam_init(struct ciss_softc *sc);
170c6131460SPaul Saab static void	ciss_cam_rescan_target(struct ciss_softc *sc,
171c6131460SPaul Saab 				       int bus, int target);
1723a31b7ebSMike Smith static void	ciss_cam_rescan_all(struct ciss_softc *sc);
1733a31b7ebSMike Smith static void	ciss_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb);
1743a31b7ebSMike Smith static void	ciss_cam_action(struct cam_sim *sim, union ccb *ccb);
1753a31b7ebSMike Smith static int	ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio);
1763a31b7ebSMike Smith static int	ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio);
1773a31b7ebSMike Smith static void	ciss_cam_poll(struct cam_sim *sim);
1783a31b7ebSMike Smith static void	ciss_cam_complete(struct ciss_request *cr);
1793a31b7ebSMike Smith static void	ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio);
180c6131460SPaul Saab static struct cam_periph *ciss_find_periph(struct ciss_softc *sc,
181c6131460SPaul Saab 					   int bus, int target);
182c6131460SPaul Saab static int	ciss_name_device(struct ciss_softc *sc, int bus, int target);
1833a31b7ebSMike Smith 
1843a31b7ebSMike Smith /* periodic status monitoring */
1853a31b7ebSMike Smith static void	ciss_periodic(void *arg);
18654d02e0aSMitsuru IWASAKI static void	ciss_nop_complete(struct ciss_request *cr);
187e08d902aSMitsuru IWASAKI static void	ciss_disable_adapter(struct ciss_softc *sc);
1883a31b7ebSMike Smith static void	ciss_notify_event(struct ciss_softc *sc);
1893a31b7ebSMike Smith static void	ciss_notify_complete(struct ciss_request *cr);
1903a31b7ebSMike Smith static int	ciss_notify_abort(struct ciss_softc *sc);
1913a31b7ebSMike Smith static int	ciss_notify_abort_bmic(struct ciss_softc *sc);
1921fe6c4eeSScott Long static void	ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn);
1933a31b7ebSMike Smith static void	ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn);
1943a31b7ebSMike Smith static void	ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn);
1953a31b7ebSMike Smith 
1963a31b7ebSMike Smith /* debugging output */
1973a31b7ebSMike Smith static void	ciss_print_request(struct ciss_request *cr);
1983a31b7ebSMike Smith static void	ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld);
1993a31b7ebSMike Smith static const char *ciss_name_ldrive_status(int status);
2003a31b7ebSMike Smith static int	ciss_decode_ldrive_status(int status);
2013a31b7ebSMike Smith static const char *ciss_name_ldrive_org(int org);
2023a31b7ebSMike Smith static const char *ciss_name_command_status(int status);
2033a31b7ebSMike Smith 
2043a31b7ebSMike Smith /*
2053a31b7ebSMike Smith  * PCI bus interface.
2063a31b7ebSMike Smith  */
2073a31b7ebSMike Smith static device_method_t ciss_methods[] = {
2083a31b7ebSMike Smith     /* Device interface */
2093a31b7ebSMike Smith     DEVMETHOD(device_probe,	ciss_probe),
2103a31b7ebSMike Smith     DEVMETHOD(device_attach,	ciss_attach),
2113a31b7ebSMike Smith     DEVMETHOD(device_detach,	ciss_detach),
2123a31b7ebSMike Smith     DEVMETHOD(device_shutdown,	ciss_shutdown),
2133a31b7ebSMike Smith     { 0, 0 }
2143a31b7ebSMike Smith };
2153a31b7ebSMike Smith 
2163a31b7ebSMike Smith static driver_t ciss_pci_driver = {
2173a31b7ebSMike Smith     "ciss",
2183a31b7ebSMike Smith     ciss_methods,
2193a31b7ebSMike Smith     sizeof(struct ciss_softc)
2203a31b7ebSMike Smith };
2213a31b7ebSMike Smith 
2223a31b7ebSMike Smith static devclass_t	ciss_devclass;
2233a31b7ebSMike Smith DRIVER_MODULE(ciss, pci, ciss_pci_driver, ciss_devclass, 0, 0);
22491d7721cSMaxim Konovalov MODULE_DEPEND(ciss, cam, 1, 1, 1);
22591d7721cSMaxim Konovalov MODULE_DEPEND(ciss, pci, 1, 1, 1);
2263a31b7ebSMike Smith 
2273a31b7ebSMike Smith /*
2283a31b7ebSMike Smith  * Control device interface.
2293a31b7ebSMike Smith  */
2303a31b7ebSMike Smith static d_open_t		ciss_open;
2313a31b7ebSMike Smith static d_close_t	ciss_close;
2323a31b7ebSMike Smith static d_ioctl_t	ciss_ioctl;
2333a31b7ebSMike Smith 
2343a31b7ebSMike Smith static struct cdevsw ciss_cdevsw = {
235dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
236975b7318SScott Long 	.d_flags =	0,
2377ac40f5fSPoul-Henning Kamp 	.d_open =	ciss_open,
2387ac40f5fSPoul-Henning Kamp 	.d_close =	ciss_close,
2397ac40f5fSPoul-Henning Kamp 	.d_ioctl =	ciss_ioctl,
2407ac40f5fSPoul-Henning Kamp 	.d_name =	"ciss",
2413a31b7ebSMike Smith };
2423a31b7ebSMike Smith 
2431fe6c4eeSScott Long /*
2441fe6c4eeSScott Long  * This tunable can be set at boot time and controls whether physical devices
2451fe6c4eeSScott Long  * that are marked hidden by the firmware should be exposed anyways.
2461fe6c4eeSScott Long  */
2471fe6c4eeSScott Long static unsigned int ciss_expose_hidden_physical = 0;
2481fe6c4eeSScott Long TUNABLE_INT("hw.ciss.expose_hidden_physical", &ciss_expose_hidden_physical);
2491fe6c4eeSScott Long 
2503a31b7ebSMike Smith /************************************************************************
2513a31b7ebSMike Smith  * CISS adapters amazingly don't have a defined programming interface
2523a31b7ebSMike Smith  * value.  (One could say some very despairing things about PCI and
2533a31b7ebSMike Smith  * people just not getting the general idea.)  So we are forced to
2543a31b7ebSMike Smith  * stick with matching against subvendor/subdevice, and thus have to
2553a31b7ebSMike Smith  * be updated for every new CISS adapter that appears.
2563a31b7ebSMike Smith  */
2573a31b7ebSMike Smith #define CISS_BOARD_SA5	(1<<0)
2583a31b7ebSMike Smith #define CISS_BOARD_SA5B	(1<<1)
2593a31b7ebSMike Smith 
2603a31b7ebSMike Smith static struct
2613a31b7ebSMike Smith {
2623a31b7ebSMike Smith     u_int16_t	subvendor;
2633a31b7ebSMike Smith     u_int16_t	subdevice;
2643a31b7ebSMike Smith     int		flags;
2653a31b7ebSMike Smith     char	*desc;
2663a31b7ebSMike Smith } ciss_vendor_data[] = {
2673a31b7ebSMike Smith     { 0x0e11, 0x4070, CISS_BOARD_SA5,	"Compaq Smart Array 5300" },
2683a31b7ebSMike Smith     { 0x0e11, 0x4080, CISS_BOARD_SA5B,	"Compaq Smart Array 5i" },
2693a31b7ebSMike Smith     { 0x0e11, 0x4082, CISS_BOARD_SA5B,	"Compaq Smart Array 532" },
2702648ded5SPaul Saab     { 0x0e11, 0x4083, CISS_BOARD_SA5B,	"HP Smart Array 5312" },
271b26392c7SPaul Saab     { 0x0e11, 0x4091, CISS_BOARD_SA5,	"HP Smart Array 6i" },
2729e7313bbSPaul Saab     { 0x0e11, 0x409A, CISS_BOARD_SA5,	"HP Smart Array 641" },
2739e7313bbSPaul Saab     { 0x0e11, 0x409B, CISS_BOARD_SA5,	"HP Smart Array 642" },
2749e7313bbSPaul Saab     { 0x0e11, 0x409C, CISS_BOARD_SA5,	"HP Smart Array 6400" },
2759e7313bbSPaul Saab     { 0x0e11, 0x409D, CISS_BOARD_SA5,	"HP Smart Array 6400 EM" },
276cad572c4SPaul Saab     { 0x103C, 0x3211, CISS_BOARD_SA5,	"HP Smart Array E200i" },
277cad572c4SPaul Saab     { 0x103C, 0x3212, CISS_BOARD_SA5,	"HP Smart Array E200" },
278cad572c4SPaul Saab     { 0x103C, 0x3213, CISS_BOARD_SA5,	"HP Smart Array E200i" },
279cad572c4SPaul Saab     { 0x103C, 0x3214, CISS_BOARD_SA5,	"HP Smart Array E200i" },
280cad572c4SPaul Saab     { 0x103C, 0x3215, CISS_BOARD_SA5,	"HP Smart Array E200i" },
281b612d5e1SPaul Saab     { 0x103C, 0x3220, CISS_BOARD_SA5,	"HP Smart Array" },
282b612d5e1SPaul Saab     { 0x103C, 0x3222, CISS_BOARD_SA5,	"HP Smart Array" },
2834bb10cf1SPaul Saab     { 0x103C, 0x3223, CISS_BOARD_SA5,	"HP Smart Array P800" },
284f3f82767SPaul Saab     { 0x103C, 0x3225, CISS_BOARD_SA5,	"HP Smart Array P600" },
285b612d5e1SPaul Saab     { 0x103C, 0x3230, CISS_BOARD_SA5,	"HP Smart Array" },
286cad572c4SPaul Saab     { 0x103C, 0x3231, CISS_BOARD_SA5,	"HP Smart Array" },
287b612d5e1SPaul Saab     { 0x103C, 0x3232, CISS_BOARD_SA5,	"HP Smart Array" },
288b612d5e1SPaul Saab     { 0x103C, 0x3233, CISS_BOARD_SA5,	"HP Smart Array" },
289cad572c4SPaul Saab     { 0x103C, 0x3234, CISS_BOARD_SA5,	"HP Smart Array P400" },
290cad572c4SPaul Saab     { 0x103C, 0x3235, CISS_BOARD_SA5,	"HP Smart Array P400i" },
291b612d5e1SPaul Saab     { 0x103C, 0x3236, CISS_BOARD_SA5,	"HP Smart Array" },
292b612d5e1SPaul Saab     { 0x103C, 0x3237, CISS_BOARD_SA5,	"HP Smart Array" },
293b612d5e1SPaul Saab     { 0x103C, 0x3238, CISS_BOARD_SA5,	"HP Smart Array" },
294b612d5e1SPaul Saab     { 0x103C, 0x3239, CISS_BOARD_SA5,	"HP Smart Array" },
295b612d5e1SPaul Saab     { 0x103C, 0x323A, CISS_BOARD_SA5,	"HP Smart Array" },
296b612d5e1SPaul Saab     { 0x103C, 0x323B, CISS_BOARD_SA5,	"HP Smart Array" },
297b612d5e1SPaul Saab     { 0x103C, 0x323C, CISS_BOARD_SA5,	"HP Smart Array" },
2984a6a94d8SArchie Cobbs     { 0, 0, 0, NULL }
2993a31b7ebSMike Smith };
3003a31b7ebSMike Smith 
3013a31b7ebSMike Smith /************************************************************************
3023a31b7ebSMike Smith  * Find a match for the device in our list of known adapters.
3033a31b7ebSMike Smith  */
3043a31b7ebSMike Smith static int
3053a31b7ebSMike Smith ciss_lookup(device_t dev)
3063a31b7ebSMike Smith {
3073a31b7ebSMike Smith     int 	i;
3083a31b7ebSMike Smith 
3093a31b7ebSMike Smith     for (i = 0; ciss_vendor_data[i].desc != NULL; i++)
3103a31b7ebSMike Smith 	if ((pci_get_subvendor(dev) == ciss_vendor_data[i].subvendor) &&
3113a31b7ebSMike Smith 	    (pci_get_subdevice(dev) == ciss_vendor_data[i].subdevice)) {
3123a31b7ebSMike Smith 	    return(i);
3133a31b7ebSMike Smith 	}
3143a31b7ebSMike Smith     return(-1);
3153a31b7ebSMike Smith }
3163a31b7ebSMike Smith 
3173a31b7ebSMike Smith /************************************************************************
3183a31b7ebSMike Smith  * Match a known CISS adapter.
3193a31b7ebSMike Smith  */
3203a31b7ebSMike Smith static int
3213a31b7ebSMike Smith ciss_probe(device_t dev)
3223a31b7ebSMike Smith {
3233a31b7ebSMike Smith     int		i;
3243a31b7ebSMike Smith 
3253a31b7ebSMike Smith     i = ciss_lookup(dev);
3263a31b7ebSMike Smith     if (i != -1) {
3273a31b7ebSMike Smith 	device_set_desc(dev, ciss_vendor_data[i].desc);
328538565c4SWarner Losh 	return(BUS_PROBE_DEFAULT);
3293a31b7ebSMike Smith     }
3303a31b7ebSMike Smith     return(ENOENT);
3313a31b7ebSMike Smith }
3323a31b7ebSMike Smith 
3333a31b7ebSMike Smith /************************************************************************
3343a31b7ebSMike Smith  * Attach the driver to this adapter.
3353a31b7ebSMike Smith  */
3363a31b7ebSMike Smith static int
3373a31b7ebSMike Smith ciss_attach(device_t dev)
3383a31b7ebSMike Smith {
3393a31b7ebSMike Smith     struct ciss_softc	*sc;
3403a31b7ebSMike Smith     int			i, error;
3413a31b7ebSMike Smith 
3423a31b7ebSMike Smith     debug_called(1);
3433a31b7ebSMike Smith 
3443a31b7ebSMike Smith #ifdef CISS_DEBUG
3453a31b7ebSMike Smith     /* print structure/union sizes */
3463a31b7ebSMike Smith     debug_struct(ciss_command);
3473a31b7ebSMike Smith     debug_struct(ciss_header);
3483a31b7ebSMike Smith     debug_union(ciss_device_address);
3493a31b7ebSMike Smith     debug_struct(ciss_cdb);
3503a31b7ebSMike Smith     debug_struct(ciss_report_cdb);
3513a31b7ebSMike Smith     debug_struct(ciss_notify_cdb);
3523a31b7ebSMike Smith     debug_struct(ciss_notify);
3533a31b7ebSMike Smith     debug_struct(ciss_message_cdb);
3543a31b7ebSMike Smith     debug_struct(ciss_error_info_pointer);
3553a31b7ebSMike Smith     debug_struct(ciss_error_info);
3563a31b7ebSMike Smith     debug_struct(ciss_sg_entry);
3573a31b7ebSMike Smith     debug_struct(ciss_config_table);
3583a31b7ebSMike Smith     debug_struct(ciss_bmic_cdb);
3593a31b7ebSMike Smith     debug_struct(ciss_bmic_id_ldrive);
3603a31b7ebSMike Smith     debug_struct(ciss_bmic_id_lstatus);
3613a31b7ebSMike Smith     debug_struct(ciss_bmic_id_table);
3623a31b7ebSMike Smith     debug_struct(ciss_bmic_id_pdrive);
3633a31b7ebSMike Smith     debug_struct(ciss_bmic_blink_pdrive);
3643a31b7ebSMike Smith     debug_struct(ciss_bmic_flush_cache);
3653a31b7ebSMike Smith     debug_const(CISS_MAX_REQUESTS);
3663a31b7ebSMike Smith     debug_const(CISS_MAX_LOGICAL);
3673a31b7ebSMike Smith     debug_const(CISS_INTERRUPT_COALESCE_DELAY);
3683a31b7ebSMike Smith     debug_const(CISS_INTERRUPT_COALESCE_COUNT);
3693a31b7ebSMike Smith     debug_const(CISS_COMMAND_ALLOC_SIZE);
3703a31b7ebSMike Smith     debug_const(CISS_COMMAND_SG_LENGTH);
3713a31b7ebSMike Smith 
3723a31b7ebSMike Smith     debug_type(cciss_pci_info_struct);
3733a31b7ebSMike Smith     debug_type(cciss_coalint_struct);
3743a31b7ebSMike Smith     debug_type(cciss_coalint_struct);
3753a31b7ebSMike Smith     debug_type(NodeName_type);
3763a31b7ebSMike Smith     debug_type(NodeName_type);
3773a31b7ebSMike Smith     debug_type(Heartbeat_type);
3783a31b7ebSMike Smith     debug_type(BusTypes_type);
3793a31b7ebSMike Smith     debug_type(FirmwareVer_type);
3803a31b7ebSMike Smith     debug_type(DriverVer_type);
3813a31b7ebSMike Smith     debug_type(IOCTL_Command_struct);
3823a31b7ebSMike Smith #endif
3833a31b7ebSMike Smith 
3843a31b7ebSMike Smith     sc = device_get_softc(dev);
3853a31b7ebSMike Smith     sc->ciss_dev = dev;
3863a31b7ebSMike Smith 
3873a31b7ebSMike Smith     /*
3883a31b7ebSMike Smith      * Work out adapter type.
3893a31b7ebSMike Smith      */
3903a31b7ebSMike Smith     i = ciss_lookup(dev);
39114d15addSSam Leffler     if (i < 0) {
39214d15addSSam Leffler 	ciss_printf(sc, "unknown adapter type\n");
39314d15addSSam Leffler 	error = ENXIO;
39414d15addSSam Leffler 	goto out;
39514d15addSSam Leffler     }
3963a31b7ebSMike Smith     if (ciss_vendor_data[i].flags & CISS_BOARD_SA5) {
3973a31b7ebSMike Smith 	sc->ciss_interrupt_mask = CISS_TL_SIMPLE_INTR_OPQ_SA5;
3983a31b7ebSMike Smith     } else if (ciss_vendor_data[i].flags & CISS_BOARD_SA5B) {
3993a31b7ebSMike Smith 	sc->ciss_interrupt_mask = CISS_TL_SIMPLE_INTR_OPQ_SA5B;
4003a31b7ebSMike Smith     } else {
4013a31b7ebSMike Smith 	/* really an error on our part */
4023a31b7ebSMike Smith 	ciss_printf(sc, "unable to determine hardware type\n");
4033a31b7ebSMike Smith 	error = ENXIO;
4043a31b7ebSMike Smith 	goto out;
4053a31b7ebSMike Smith     }
4063a31b7ebSMike Smith 
4073a31b7ebSMike Smith     /*
4083a31b7ebSMike Smith      * Do PCI-specific init.
4093a31b7ebSMike Smith      */
4103a31b7ebSMike Smith     if ((error = ciss_init_pci(sc)) != 0)
4113a31b7ebSMike Smith 	goto out;
4123a31b7ebSMike Smith 
4133a31b7ebSMike Smith     /*
4143a31b7ebSMike Smith      * Initialise driver queues.
4153a31b7ebSMike Smith      */
4163a31b7ebSMike Smith     ciss_initq_free(sc);
4173a31b7ebSMike Smith     ciss_initq_busy(sc);
4183a31b7ebSMike Smith     ciss_initq_complete(sc);
419c6131460SPaul Saab     ciss_initq_notify(sc);
420975b7318SScott Long     mtx_init(&sc->ciss_mtx, "cissmtx", NULL, MTX_DEF);
421975b7318SScott Long     callout_init_mtx(&sc->ciss_periodic, &sc->ciss_mtx, 0);
4223a31b7ebSMike Smith 
4233a31b7ebSMike Smith     /*
42417c0792dSPaul Saab      * Initalize device sysctls.
42517c0792dSPaul Saab      */
42617c0792dSPaul Saab     ciss_init_sysctl(sc);
42717c0792dSPaul Saab 
42817c0792dSPaul Saab     /*
4293a31b7ebSMike Smith      * Initialise command/request pool.
4303a31b7ebSMike Smith      */
4313a31b7ebSMike Smith     if ((error = ciss_init_requests(sc)) != 0)
4323a31b7ebSMike Smith 	goto out;
4333a31b7ebSMike Smith 
4343a31b7ebSMike Smith     /*
4353a31b7ebSMike Smith      * Get adapter information.
4363a31b7ebSMike Smith      */
4373a31b7ebSMike Smith     if ((error = ciss_identify_adapter(sc)) != 0)
4383a31b7ebSMike Smith 	goto out;
4393a31b7ebSMike Smith 
4403a31b7ebSMike Smith     /*
441c6131460SPaul Saab      * Find all the physical devices.
442c6131460SPaul Saab      */
443c6131460SPaul Saab     if ((error = ciss_init_physical(sc)) != 0)
444c6131460SPaul Saab 	goto out;
445c6131460SPaul Saab 
446c6131460SPaul Saab     /*
4473a31b7ebSMike Smith      * Build our private table of logical devices.
4483a31b7ebSMike Smith      */
4493a31b7ebSMike Smith     if ((error = ciss_init_logical(sc)) != 0)
4503a31b7ebSMike Smith 	goto out;
4513a31b7ebSMike Smith 
4523a31b7ebSMike Smith     /*
4533a31b7ebSMike Smith      * Enable interrupts so that the CAM scan can complete.
4543a31b7ebSMike Smith      */
4553a31b7ebSMike Smith     CISS_TL_SIMPLE_ENABLE_INTERRUPTS(sc);
4563a31b7ebSMike Smith 
4573a31b7ebSMike Smith     /*
4583a31b7ebSMike Smith      * Initialise the CAM interface.
4593a31b7ebSMike Smith      */
4603a31b7ebSMike Smith     if ((error = ciss_cam_init(sc)) != 0)
4613a31b7ebSMike Smith 	goto out;
4623a31b7ebSMike Smith 
4633a31b7ebSMike Smith     /*
4643a31b7ebSMike Smith      * Start the heartbeat routine and event chain.
4653a31b7ebSMike Smith      */
4663a31b7ebSMike Smith     ciss_periodic(sc);
4673a31b7ebSMike Smith 
4683a31b7ebSMike Smith    /*
4693a31b7ebSMike Smith      * Create the control device.
4703a31b7ebSMike Smith      */
4713a31b7ebSMike Smith     sc->ciss_dev_t = make_dev(&ciss_cdevsw, device_get_unit(sc->ciss_dev),
4723a31b7ebSMike Smith 			      UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
4733a31b7ebSMike Smith 			      "ciss%d", device_get_unit(sc->ciss_dev));
4743a31b7ebSMike Smith     sc->ciss_dev_t->si_drv1 = sc;
4753a31b7ebSMike Smith 
4763a31b7ebSMike Smith     /*
4773a31b7ebSMike Smith      * The adapter is running; synchronous commands can now sleep
4783a31b7ebSMike Smith      * waiting for an interrupt to signal completion.
4793a31b7ebSMike Smith      */
4803a31b7ebSMike Smith     sc->ciss_flags |= CISS_FLAG_RUNNING;
4813a31b7ebSMike Smith 
482c6131460SPaul Saab     ciss_spawn_notify_thread(sc);
483c6131460SPaul Saab 
4843a31b7ebSMike Smith     error = 0;
4853a31b7ebSMike Smith  out:
4863a31b7ebSMike Smith     if (error != 0)
4873a31b7ebSMike Smith 	ciss_free(sc);
4883a31b7ebSMike Smith     return(error);
4893a31b7ebSMike Smith }
4903a31b7ebSMike Smith 
4913a31b7ebSMike Smith /************************************************************************
4923a31b7ebSMike Smith  * Detach the driver from this adapter.
4933a31b7ebSMike Smith  */
4943a31b7ebSMike Smith static int
4953a31b7ebSMike Smith ciss_detach(device_t dev)
4963a31b7ebSMike Smith {
4973a31b7ebSMike Smith     struct ciss_softc	*sc = device_get_softc(dev);
4983a31b7ebSMike Smith 
4993a31b7ebSMike Smith     debug_called(1);
5003a31b7ebSMike Smith 
501975b7318SScott Long     mtx_lock(&sc->ciss_mtx);
502975b7318SScott Long     if (sc->ciss_flags & CISS_FLAG_CONTROL_OPEN) {
503975b7318SScott Long 	mtx_unlock(&sc->ciss_mtx);
504ffdf82e1SPaul Saab 	return (EBUSY);
505975b7318SScott Long     }
506ffdf82e1SPaul Saab 
5073a31b7ebSMike Smith     /* flush adapter cache */
5083a31b7ebSMike Smith     ciss_flush_adapter(sc);
5093a31b7ebSMike Smith 
510975b7318SScott Long     /* release all resources.  The mutex is released and freed here too. */
5113a31b7ebSMike Smith     ciss_free(sc);
5123a31b7ebSMike Smith 
5133a31b7ebSMike Smith     return(0);
5143a31b7ebSMike Smith }
5153a31b7ebSMike Smith 
5163a31b7ebSMike Smith /************************************************************************
5173a31b7ebSMike Smith  * Prepare adapter for system shutdown.
5183a31b7ebSMike Smith  */
5193a31b7ebSMike Smith static int
5203a31b7ebSMike Smith ciss_shutdown(device_t dev)
5213a31b7ebSMike Smith {
5223a31b7ebSMike Smith     struct ciss_softc	*sc = device_get_softc(dev);
5233a31b7ebSMike Smith 
5243a31b7ebSMike Smith     debug_called(1);
5253a31b7ebSMike Smith 
526d4a4ddc6SScott Long     mtx_lock(&sc->ciss_mtx);
5273a31b7ebSMike Smith     /* flush adapter cache */
5283a31b7ebSMike Smith     ciss_flush_adapter(sc);
5293a31b7ebSMike Smith 
53017c0792dSPaul Saab     if (sc->ciss_soft_reset)
53117c0792dSPaul Saab 	ciss_soft_reset(sc);
532d4a4ddc6SScott Long     mtx_unlock(&sc->ciss_mtx);
53317c0792dSPaul Saab 
5343a31b7ebSMike Smith     return(0);
5353a31b7ebSMike Smith }
5363a31b7ebSMike Smith 
53717c0792dSPaul Saab static void
53817c0792dSPaul Saab ciss_init_sysctl(struct ciss_softc *sc)
53917c0792dSPaul Saab {
54017c0792dSPaul Saab 
54117c0792dSPaul Saab     SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->ciss_dev),
54217c0792dSPaul Saab 	SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ciss_dev)),
54317c0792dSPaul Saab 	OID_AUTO, "soft_reset", CTLFLAG_RW, &sc->ciss_soft_reset, 0, "");
54417c0792dSPaul Saab }
54517c0792dSPaul Saab 
5463a31b7ebSMike Smith /************************************************************************
5473a31b7ebSMike Smith  * Perform PCI-specific attachment actions.
5483a31b7ebSMike Smith  */
5493a31b7ebSMike Smith static int
5503a31b7ebSMike Smith ciss_init_pci(struct ciss_softc *sc)
5513a31b7ebSMike Smith {
5523a31b7ebSMike Smith     uintptr_t		cbase, csize, cofs;
5533a31b7ebSMike Smith     int			error;
5543a31b7ebSMike Smith 
5553a31b7ebSMike Smith     debug_called(1);
5563a31b7ebSMike Smith 
5573a31b7ebSMike Smith     /*
5583a31b7ebSMike Smith      * Allocate register window first (we need this to find the config
5593a31b7ebSMike Smith      * struct).
5603a31b7ebSMike Smith      */
5613a31b7ebSMike Smith     error = ENXIO;
5623a31b7ebSMike Smith     sc->ciss_regs_rid = CISS_TL_SIMPLE_BAR_REGS;
5633a31b7ebSMike Smith     if ((sc->ciss_regs_resource =
5645f96beb9SNate Lawson 	 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
5655f96beb9SNate Lawson 				&sc->ciss_regs_rid, RF_ACTIVE)) == NULL) {
5663a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate register window\n");
5673a31b7ebSMike Smith 	return(ENXIO);
5683a31b7ebSMike Smith     }
5693a31b7ebSMike Smith     sc->ciss_regs_bhandle = rman_get_bushandle(sc->ciss_regs_resource);
5703a31b7ebSMike Smith     sc->ciss_regs_btag = rman_get_bustag(sc->ciss_regs_resource);
5713a31b7ebSMike Smith 
5723a31b7ebSMike Smith     /*
5733a31b7ebSMike Smith      * Find the BAR holding the config structure.  If it's not the one
5743a31b7ebSMike Smith      * we already mapped for registers, map it too.
5753a31b7ebSMike Smith      */
5763a31b7ebSMike Smith     sc->ciss_cfg_rid = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_BAR) & 0xffff;
5773a31b7ebSMike Smith     if (sc->ciss_cfg_rid != sc->ciss_regs_rid) {
5783a31b7ebSMike Smith 	if ((sc->ciss_cfg_resource =
5795f96beb9SNate Lawson 	     bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
5805f96beb9SNate Lawson 				    &sc->ciss_cfg_rid, RF_ACTIVE)) == NULL) {
5813a31b7ebSMike Smith 	    ciss_printf(sc, "can't allocate config window\n");
5823a31b7ebSMike Smith 	    return(ENXIO);
5833a31b7ebSMike Smith 	}
5843a31b7ebSMike Smith 	cbase = (uintptr_t)rman_get_virtual(sc->ciss_cfg_resource);
5853a31b7ebSMike Smith 	csize = rman_get_end(sc->ciss_cfg_resource) -
5863a31b7ebSMike Smith 	    rman_get_start(sc->ciss_cfg_resource) + 1;
5873a31b7ebSMike Smith     } else {
5883a31b7ebSMike Smith 	cbase = (uintptr_t)rman_get_virtual(sc->ciss_regs_resource);
5893a31b7ebSMike Smith 	csize = rman_get_end(sc->ciss_regs_resource) -
5903a31b7ebSMike Smith 	    rman_get_start(sc->ciss_regs_resource) + 1;
5913a31b7ebSMike Smith     }
5923a31b7ebSMike Smith     cofs = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_OFF);
5933a31b7ebSMike Smith 
5943a31b7ebSMike Smith     /*
5953a31b7ebSMike Smith      * Use the base/size/offset values we just calculated to
5963a31b7ebSMike Smith      * sanity-check the config structure.  If it's OK, point to it.
5973a31b7ebSMike Smith      */
5983a31b7ebSMike Smith     if ((cofs + sizeof(struct ciss_config_table)) > csize) {
5993a31b7ebSMike Smith 	ciss_printf(sc, "config table outside window\n");
6003a31b7ebSMike Smith 	return(ENXIO);
6013a31b7ebSMike Smith     }
6023a31b7ebSMike Smith     sc->ciss_cfg = (struct ciss_config_table *)(cbase + cofs);
6033a31b7ebSMike Smith     debug(1, "config struct at %p", sc->ciss_cfg);
6043a31b7ebSMike Smith 
6053a31b7ebSMike Smith     /*
6063a31b7ebSMike Smith      * Validate the config structure.  If we supported other transport
6073a31b7ebSMike Smith      * methods, we could select amongst them at this point in time.
6083a31b7ebSMike Smith      */
6093a31b7ebSMike Smith     if (strncmp(sc->ciss_cfg->signature, "CISS", 4)) {
6103a31b7ebSMike Smith 	ciss_printf(sc, "config signature mismatch (got '%c%c%c%c')\n",
6113a31b7ebSMike Smith 		    sc->ciss_cfg->signature[0], sc->ciss_cfg->signature[1],
6123a31b7ebSMike Smith 		    sc->ciss_cfg->signature[2], sc->ciss_cfg->signature[3]);
6133a31b7ebSMike Smith 	return(ENXIO);
6143a31b7ebSMike Smith     }
6153a31b7ebSMike Smith 
6163a31b7ebSMike Smith     /*
6173a31b7ebSMike Smith      * Put the board into simple mode, and tell it we're using the low
6183a31b7ebSMike Smith      * 4GB of RAM.  Set the default interrupt coalescing options.
6193a31b7ebSMike Smith      */
6203a31b7ebSMike Smith     if (!(sc->ciss_cfg->supported_methods & CISS_TRANSPORT_METHOD_SIMPLE)) {
6213a31b7ebSMike Smith 	ciss_printf(sc, "adapter does not support 'simple' transport layer\n");
6223a31b7ebSMike Smith 	return(ENXIO);
6233a31b7ebSMike Smith     }
6243a31b7ebSMike Smith     sc->ciss_cfg->requested_method = CISS_TRANSPORT_METHOD_SIMPLE;
6253a31b7ebSMike Smith     sc->ciss_cfg->command_physlimit = 0;
6263a31b7ebSMike Smith     sc->ciss_cfg->interrupt_coalesce_delay = CISS_INTERRUPT_COALESCE_DELAY;
6273a31b7ebSMike Smith     sc->ciss_cfg->interrupt_coalesce_count = CISS_INTERRUPT_COALESCE_COUNT;
6283a31b7ebSMike Smith 
629a891a3bfSPaul Saab #ifdef __i386__
630a891a3bfSPaul Saab     sc->ciss_cfg->host_driver |= CISS_DRIVER_SCSI_PREFETCH;
631a891a3bfSPaul Saab #endif
632a891a3bfSPaul Saab 
6333a31b7ebSMike Smith     if (ciss_update_config(sc)) {
6343a31b7ebSMike Smith 	ciss_printf(sc, "adapter refuses to accept config update (IDBR 0x%x)\n",
6353a31b7ebSMike Smith 		    CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR));
6363a31b7ebSMike Smith 	return(ENXIO);
6373a31b7ebSMike Smith     }
6383a31b7ebSMike Smith     if (!(sc->ciss_cfg->active_method != CISS_TRANSPORT_METHOD_SIMPLE)) {
6393a31b7ebSMike Smith 	ciss_printf(sc,
6403a31b7ebSMike Smith 		    "adapter refuses to go into 'simple' transport mode (0x%x, 0x%x)\n",
6413a31b7ebSMike Smith 		    sc->ciss_cfg->supported_methods, sc->ciss_cfg->active_method);
6423a31b7ebSMike Smith 	return(ENXIO);
6433a31b7ebSMike Smith     }
6443a31b7ebSMike Smith 
6453a31b7ebSMike Smith     /*
6463a31b7ebSMike Smith      * Wait for the adapter to come ready.
6473a31b7ebSMike Smith      */
6483a31b7ebSMike Smith     if ((error = ciss_wait_adapter(sc)) != 0)
6493a31b7ebSMike Smith 	return(error);
6503a31b7ebSMike Smith 
6513a31b7ebSMike Smith     /*
6523a31b7ebSMike Smith      * Turn off interrupts before we go routing anything.
6533a31b7ebSMike Smith      */
6543a31b7ebSMike Smith     CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
6553a31b7ebSMike Smith 
6563a31b7ebSMike Smith     /*
6573a31b7ebSMike Smith      * Allocate and set up our interrupt.
6583a31b7ebSMike Smith      */
6593a31b7ebSMike Smith     sc->ciss_irq_rid = 0;
6603a31b7ebSMike Smith     if ((sc->ciss_irq_resource =
6615f96beb9SNate Lawson 	 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_IRQ, &sc->ciss_irq_rid,
6623a31b7ebSMike Smith 				RF_ACTIVE | RF_SHAREABLE)) == NULL) {
6633a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate interrupt\n");
6643a31b7ebSMike Smith 	return(ENXIO);
6653a31b7ebSMike Smith     }
666cc850363SPeter Wemm     if (bus_setup_intr(sc->ciss_dev, sc->ciss_irq_resource,
667975b7318SScott Long 		       INTR_TYPE_CAM|INTR_MPSAFE, NULL, ciss_intr, sc,
6683a31b7ebSMike Smith 		       &sc->ciss_intr)) {
6693a31b7ebSMike Smith 	ciss_printf(sc, "can't set up interrupt\n");
6703a31b7ebSMike Smith 	return(ENXIO);
6713a31b7ebSMike Smith     }
6723a31b7ebSMike Smith 
6733a31b7ebSMike Smith     /*
6743a31b7ebSMike Smith      * Allocate the parent bus DMA tag appropriate for our PCI
6753a31b7ebSMike Smith      * interface.
6763a31b7ebSMike Smith      *
6773a31b7ebSMike Smith      * Note that "simple" adapters can only address within a 32-bit
6783a31b7ebSMike Smith      * span.
6793a31b7ebSMike Smith      */
6803a31b7ebSMike Smith     if (bus_dma_tag_create(NULL, 			/* parent */
6813a31b7ebSMike Smith 			   1, 0, 			/* alignment, boundary */
682639717c8SPaul Saab 			   BUS_SPACE_MAXADDR,		/* lowaddr */
6833a31b7ebSMike Smith 			   BUS_SPACE_MAXADDR, 		/* highaddr */
6843a31b7ebSMike Smith 			   NULL, NULL, 			/* filter, filterarg */
685639717c8SPaul Saab 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
686639717c8SPaul Saab 			   CISS_COMMAND_SG_LENGTH,	/* nsegments */
6873a31b7ebSMike Smith 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
6883a31b7ebSMike Smith 			   BUS_DMA_ALLOCNOW,		/* flags */
689f6b1c44dSScott Long 			   NULL, NULL,			/* lockfunc, lockarg */
6903a31b7ebSMike Smith 			   &sc->ciss_parent_dmat)) {
6913a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate parent DMA tag\n");
6923a31b7ebSMike Smith 	return(ENOMEM);
6933a31b7ebSMike Smith     }
6943a31b7ebSMike Smith 
6953a31b7ebSMike Smith     /*
6963a31b7ebSMike Smith      * Create DMA tag for mapping buffers into adapter-addressable
6973a31b7ebSMike Smith      * space.
6983a31b7ebSMike Smith      */
6993a31b7ebSMike Smith     if (bus_dma_tag_create(sc->ciss_parent_dmat, 	/* parent */
7003a31b7ebSMike Smith 			   1, 0, 			/* alignment, boundary */
7013a31b7ebSMike Smith 			   BUS_SPACE_MAXADDR,		/* lowaddr */
7023a31b7ebSMike Smith 			   BUS_SPACE_MAXADDR, 		/* highaddr */
7033a31b7ebSMike Smith 			   NULL, NULL, 			/* filter, filterarg */
7043a31b7ebSMike Smith 			   MAXBSIZE, CISS_COMMAND_SG_LENGTH,	/* maxsize, nsegments */
7053a31b7ebSMike Smith 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
7063a31b7ebSMike Smith 			   0,				/* flags */
707975b7318SScott Long 			   busdma_lock_mutex, &sc->ciss_mtx,	/* lockfunc, lockarg */
7083a31b7ebSMike Smith 			   &sc->ciss_buffer_dmat)) {
7093a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate buffer DMA tag\n");
7103a31b7ebSMike Smith 	return(ENOMEM);
7113a31b7ebSMike Smith     }
7123a31b7ebSMike Smith     return(0);
7133a31b7ebSMike Smith }
7143a31b7ebSMike Smith 
7153a31b7ebSMike Smith /************************************************************************
7163a31b7ebSMike Smith  * Wait for the adapter to come ready.
7173a31b7ebSMike Smith  */
7183a31b7ebSMike Smith static int
7193a31b7ebSMike Smith ciss_wait_adapter(struct ciss_softc *sc)
7203a31b7ebSMike Smith {
7213a31b7ebSMike Smith     int		i;
7223a31b7ebSMike Smith 
7233a31b7ebSMike Smith     debug_called(1);
7243a31b7ebSMike Smith 
7253a31b7ebSMike Smith     /*
7263a31b7ebSMike Smith      * Wait for the adapter to come ready.
7273a31b7ebSMike Smith      */
7283a31b7ebSMike Smith     if (!(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY)) {
7293a31b7ebSMike Smith 	ciss_printf(sc, "waiting for adapter to come ready...\n");
7303a31b7ebSMike Smith 	for (i = 0; !(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY); i++) {
7313a31b7ebSMike Smith 	    DELAY(1000000);	/* one second */
7323a31b7ebSMike Smith 	    if (i > 30) {
7333a31b7ebSMike Smith 		ciss_printf(sc, "timed out waiting for adapter to come ready\n");
7343a31b7ebSMike Smith 		return(EIO);
7353a31b7ebSMike Smith 	    }
7363a31b7ebSMike Smith 	}
7373a31b7ebSMike Smith     }
7383a31b7ebSMike Smith     return(0);
7393a31b7ebSMike Smith }
7403a31b7ebSMike Smith 
7413a31b7ebSMike Smith /************************************************************************
7423a31b7ebSMike Smith  * Flush the adapter cache.
7433a31b7ebSMike Smith  */
7443a31b7ebSMike Smith static int
7453a31b7ebSMike Smith ciss_flush_adapter(struct ciss_softc *sc)
7463a31b7ebSMike Smith {
7473a31b7ebSMike Smith     struct ciss_request			*cr;
7483a31b7ebSMike Smith     struct ciss_bmic_flush_cache	*cbfc;
7493a31b7ebSMike Smith     int					error, command_status;
7503a31b7ebSMike Smith 
7513a31b7ebSMike Smith     debug_called(1);
7523a31b7ebSMike Smith 
7533a31b7ebSMike Smith     cr = NULL;
7543a31b7ebSMike Smith     cbfc = NULL;
7553a31b7ebSMike Smith 
7563a31b7ebSMike Smith     /*
7573a31b7ebSMike Smith      * Build a BMIC request to flush the cache.  We don't disable
7583a31b7ebSMike Smith      * it, as we may be going to do more I/O (eg. we are emulating
7593a31b7ebSMike Smith      * the Synchronise Cache command).
7603a31b7ebSMike Smith      */
7613a31b7ebSMike Smith     if ((cbfc = malloc(sizeof(*cbfc), CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
7623a31b7ebSMike Smith 	error = ENOMEM;
7633a31b7ebSMike Smith 	goto out;
7643a31b7ebSMike Smith     }
7653a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_FLUSH_CACHE,
7663a31b7ebSMike Smith 				       (void **)&cbfc, sizeof(*cbfc))) != 0)
7673a31b7ebSMike Smith 	goto out;
7683a31b7ebSMike Smith 
7693a31b7ebSMike Smith     /*
7703a31b7ebSMike Smith      * Submit the request and wait for it to complete.
7713a31b7ebSMike Smith      */
7723a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
7733a31b7ebSMike Smith 	ciss_printf(sc, "error sending BMIC FLUSH_CACHE command (%d)\n", error);
7743a31b7ebSMike Smith 	goto out;
7753a31b7ebSMike Smith     }
7763a31b7ebSMike Smith 
7773a31b7ebSMike Smith     /*
7783a31b7ebSMike Smith      * Check response.
7793a31b7ebSMike Smith      */
7803a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
7813a31b7ebSMike Smith     switch(command_status) {
7823a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:
7833a31b7ebSMike Smith 	break;
7843a31b7ebSMike Smith     default:
7853a31b7ebSMike Smith 	ciss_printf(sc, "error flushing cache (%s)\n",
7863a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
7873a31b7ebSMike Smith 	error = EIO;
7883a31b7ebSMike Smith 	goto out;
7893a31b7ebSMike Smith     }
7903a31b7ebSMike Smith 
7913a31b7ebSMike Smith out:
7923a31b7ebSMike Smith     if (cbfc != NULL)
7933a31b7ebSMike Smith 	free(cbfc, CISS_MALLOC_CLASS);
7943a31b7ebSMike Smith     if (cr != NULL)
7953a31b7ebSMike Smith 	ciss_release_request(cr);
7963a31b7ebSMike Smith     return(error);
7973a31b7ebSMike Smith }
7983a31b7ebSMike Smith 
79917c0792dSPaul Saab static void
80017c0792dSPaul Saab ciss_soft_reset(struct ciss_softc *sc)
80117c0792dSPaul Saab {
80217c0792dSPaul Saab     struct ciss_request		*cr = NULL;
80317c0792dSPaul Saab     struct ciss_command		*cc;
80417c0792dSPaul Saab     int				i, error = 0;
80517c0792dSPaul Saab 
80617c0792dSPaul Saab     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
80717c0792dSPaul Saab 	/* only reset proxy controllers */
80817c0792dSPaul Saab 	if (sc->ciss_controllers[i].physical.bus == 0)
80917c0792dSPaul Saab 	    continue;
81017c0792dSPaul Saab 
81117c0792dSPaul Saab 	if ((error = ciss_get_request(sc, &cr)) != 0)
81217c0792dSPaul Saab 	    break;
81317c0792dSPaul Saab 
81417c0792dSPaul Saab 	if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_SOFT_RESET,
81517c0792dSPaul Saab 					   NULL, 0)) != 0)
81617c0792dSPaul Saab 	    break;
81717c0792dSPaul Saab 
81817c0792dSPaul Saab 	cc = CISS_FIND_COMMAND(cr);
81917c0792dSPaul Saab 	cc->header.address = sc->ciss_controllers[i];
82017c0792dSPaul Saab 
82117c0792dSPaul Saab 	if ((error = ciss_synch_request(cr, 60 * 1000)) != 0)
82217c0792dSPaul Saab 	    break;
82317c0792dSPaul Saab 
82417c0792dSPaul Saab 	ciss_release_request(cr);
82517c0792dSPaul Saab     }
82617c0792dSPaul Saab 
82717c0792dSPaul Saab     if (error)
82817c0792dSPaul Saab 	ciss_printf(sc, "error resetting controller (%d)\n", error);
82917c0792dSPaul Saab 
83017c0792dSPaul Saab     if (cr != NULL)
83117c0792dSPaul Saab 	ciss_release_request(cr);
83217c0792dSPaul Saab }
83317c0792dSPaul Saab 
8343a31b7ebSMike Smith /************************************************************************
8353a31b7ebSMike Smith  * Allocate memory for the adapter command structures, initialise
8363a31b7ebSMike Smith  * the request structures.
8373a31b7ebSMike Smith  *
8383a31b7ebSMike Smith  * Note that the entire set of commands are allocated in a single
8393a31b7ebSMike Smith  * contiguous slab.
8403a31b7ebSMike Smith  */
8413a31b7ebSMike Smith static int
8423a31b7ebSMike Smith ciss_init_requests(struct ciss_softc *sc)
8433a31b7ebSMike Smith {
8443a31b7ebSMike Smith     struct ciss_request	*cr;
8453a31b7ebSMike Smith     int			i;
8463a31b7ebSMike Smith 
8473a31b7ebSMike Smith     debug_called(1);
8483a31b7ebSMike Smith 
8493a31b7ebSMike Smith     /*
8503a31b7ebSMike Smith      * Calculate the number of request structures/commands we are
8513a31b7ebSMike Smith      * going to provide for this adapter.
8523a31b7ebSMike Smith      */
8533a31b7ebSMike Smith     sc->ciss_max_requests = min(CISS_MAX_REQUESTS, sc->ciss_cfg->max_outstanding_commands);
8543a31b7ebSMike Smith 
8554bca277bSPaul Saab     if (bootverbose)
8563a31b7ebSMike Smith 	ciss_printf(sc, "using %d of %d available commands\n",
8573a31b7ebSMike Smith 		    sc->ciss_max_requests, sc->ciss_cfg->max_outstanding_commands);
8583a31b7ebSMike Smith 
8593a31b7ebSMike Smith     /*
8603a31b7ebSMike Smith      * Create the DMA tag for commands.
8613a31b7ebSMike Smith      */
8623a31b7ebSMike Smith     if (bus_dma_tag_create(sc->ciss_parent_dmat,	/* parent */
8633a31b7ebSMike Smith 			   1, 0, 			/* alignment, boundary */
864639717c8SPaul Saab 			   BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
8653a31b7ebSMike Smith 			   BUS_SPACE_MAXADDR, 		/* highaddr */
8663a31b7ebSMike Smith 			   NULL, NULL, 			/* filter, filterarg */
8673a31b7ebSMike Smith 			   CISS_COMMAND_ALLOC_SIZE *
8683a31b7ebSMike Smith 			   sc->ciss_max_requests, 1,	/* maxsize, nsegments */
8693a31b7ebSMike Smith 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
870639717c8SPaul Saab 			   BUS_DMA_ALLOCNOW,		/* flags */
871639717c8SPaul Saab 			   NULL, NULL,			/* lockfunc, lockarg */
8723a31b7ebSMike Smith 			   &sc->ciss_command_dmat)) {
8733a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate command DMA tag\n");
8743a31b7ebSMike Smith 	return(ENOMEM);
8753a31b7ebSMike Smith     }
8763a31b7ebSMike Smith     /*
8773a31b7ebSMike Smith      * Allocate memory and make it available for DMA.
8783a31b7ebSMike Smith      */
8793a31b7ebSMike Smith     if (bus_dmamem_alloc(sc->ciss_command_dmat, (void **)&sc->ciss_command,
8803a31b7ebSMike Smith 			 BUS_DMA_NOWAIT, &sc->ciss_command_map)) {
8813a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate command memory\n");
8823a31b7ebSMike Smith 	return(ENOMEM);
8833a31b7ebSMike Smith     }
8843a31b7ebSMike Smith     bus_dmamap_load(sc->ciss_command_dmat, sc->ciss_command_map, sc->ciss_command,
885be241f79SPaul Saab 		    CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests,
8863a31b7ebSMike Smith 		    ciss_command_map_helper, sc, 0);
8873a31b7ebSMike Smith     bzero(sc->ciss_command, CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests);
8883a31b7ebSMike Smith 
8893a31b7ebSMike Smith     /*
8903a31b7ebSMike Smith      * Set up the request and command structures, push requests onto
8913a31b7ebSMike Smith      * the free queue.
8923a31b7ebSMike Smith      */
8933a31b7ebSMike Smith     for (i = 1; i < sc->ciss_max_requests; i++) {
8943a31b7ebSMike Smith 	cr = &sc->ciss_request[i];
8953a31b7ebSMike Smith 	cr->cr_sc = sc;
8963a31b7ebSMike Smith 	cr->cr_tag = i;
8973284b9eeSPaul Saab 	bus_dmamap_create(sc->ciss_buffer_dmat, 0, &cr->cr_datamap);
8983a31b7ebSMike Smith 	ciss_enqueue_free(cr);
8993a31b7ebSMike Smith     }
9003a31b7ebSMike Smith     return(0);
9013a31b7ebSMike Smith }
9023a31b7ebSMike Smith 
9033a31b7ebSMike Smith static void
9043a31b7ebSMike Smith ciss_command_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
9053a31b7ebSMike Smith {
9063a31b7ebSMike Smith     struct ciss_softc	*sc = (struct ciss_softc *)arg;
9073a31b7ebSMike Smith 
9083a31b7ebSMike Smith     sc->ciss_command_phys = segs->ds_addr;
9093a31b7ebSMike Smith }
9103a31b7ebSMike Smith 
9113a31b7ebSMike Smith /************************************************************************
9123a31b7ebSMike Smith  * Identify the adapter, print some information about it.
9133a31b7ebSMike Smith  */
9143a31b7ebSMike Smith static int
9153a31b7ebSMike Smith ciss_identify_adapter(struct ciss_softc *sc)
9163a31b7ebSMike Smith {
9173a31b7ebSMike Smith     struct ciss_request	*cr;
9183a31b7ebSMike Smith     int			error, command_status;
9193a31b7ebSMike Smith 
9203a31b7ebSMike Smith     debug_called(1);
9213a31b7ebSMike Smith 
9223a31b7ebSMike Smith     cr = NULL;
9233a31b7ebSMike Smith 
9243a31b7ebSMike Smith     /*
9253a31b7ebSMike Smith      * Get a request, allocate storage for the adapter data.
9263a31b7ebSMike Smith      */
9273a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_CTLR,
9283a31b7ebSMike Smith 				       (void **)&sc->ciss_id,
9293a31b7ebSMike Smith 				       sizeof(*sc->ciss_id))) != 0)
9303a31b7ebSMike Smith 	goto out;
9313a31b7ebSMike Smith 
9323a31b7ebSMike Smith     /*
9333a31b7ebSMike Smith      * Submit the request and wait for it to complete.
9343a31b7ebSMike Smith      */
9353a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
9363a31b7ebSMike Smith 	ciss_printf(sc, "error sending BMIC ID_CTLR command (%d)\n", error);
9373a31b7ebSMike Smith 	goto out;
9383a31b7ebSMike Smith     }
9393a31b7ebSMike Smith 
9403a31b7ebSMike Smith     /*
9413a31b7ebSMike Smith      * Check response.
9423a31b7ebSMike Smith      */
9433a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
9443a31b7ebSMike Smith     switch(command_status) {
9453a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
9463a31b7ebSMike Smith 	break;
9473a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_UNDERRUN:
9483a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_OVERRUN:
9493a31b7ebSMike Smith 	ciss_printf(sc, "data over/underrun reading adapter information\n");
9503a31b7ebSMike Smith     default:
9513a31b7ebSMike Smith 	ciss_printf(sc, "error reading adapter information (%s)\n",
9523a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
9533a31b7ebSMike Smith 	error = EIO;
9543a31b7ebSMike Smith 	goto out;
9553a31b7ebSMike Smith     }
9563a31b7ebSMike Smith 
9573a31b7ebSMike Smith     /* sanity-check reply */
9583a31b7ebSMike Smith     if (!sc->ciss_id->big_map_supported) {
9593a31b7ebSMike Smith 	ciss_printf(sc, "adapter does not support BIG_MAP\n");
9603a31b7ebSMike Smith 	error = ENXIO;
9613a31b7ebSMike Smith 	goto out;
9623a31b7ebSMike Smith     }
9633a31b7ebSMike Smith 
964d4aa427fSPaul Saab #if 0
9653a31b7ebSMike Smith     /* XXX later revisions may not need this */
9663a31b7ebSMike Smith     sc->ciss_flags |= CISS_FLAG_FAKE_SYNCH;
967d4aa427fSPaul Saab #endif
9683a31b7ebSMike Smith 
9693a31b7ebSMike Smith     /* XXX only really required for old 5300 adapters? */
9703a31b7ebSMike Smith     sc->ciss_flags |= CISS_FLAG_BMIC_ABORT;
9713a31b7ebSMike Smith 
9723a31b7ebSMike Smith     /* print information */
9734bca277bSPaul Saab     if (bootverbose) {
974c6131460SPaul Saab #if 0	/* XXX proxy volumes??? */
9753a31b7ebSMike Smith 	ciss_printf(sc, "  %d logical drive%s configured\n",
9763a31b7ebSMike Smith 		    sc->ciss_id->configured_logical_drives,
9773a31b7ebSMike Smith 		    (sc->ciss_id->configured_logical_drives == 1) ? "" : "s");
978c6131460SPaul Saab #endif
9793a31b7ebSMike Smith 	ciss_printf(sc, "  firmware %4.4s\n", sc->ciss_id->running_firmware_revision);
9803a31b7ebSMike Smith 	ciss_printf(sc, "  %d SCSI channels\n", sc->ciss_id->scsi_bus_count);
9813a31b7ebSMike Smith 
9823a31b7ebSMike Smith 	ciss_printf(sc, "  signature '%.4s'\n", sc->ciss_cfg->signature);
9833a31b7ebSMike Smith 	ciss_printf(sc, "  valence %d\n", sc->ciss_cfg->valence);
9843a31b7ebSMike Smith 	ciss_printf(sc, "  supported I/O methods 0x%b\n",
9853a31b7ebSMike Smith 		    sc->ciss_cfg->supported_methods,
9863a31b7ebSMike Smith 		    "\20\1READY\2simple\3performant\4MEMQ\n");
9873a31b7ebSMike Smith 	ciss_printf(sc, "  active I/O method 0x%b\n",
9883a31b7ebSMike Smith 		    sc->ciss_cfg->active_method, "\20\2simple\3performant\4MEMQ\n");
9893a31b7ebSMike Smith 	ciss_printf(sc, "  4G page base 0x%08x\n",
9903a31b7ebSMike Smith 		    sc->ciss_cfg->command_physlimit);
9913a31b7ebSMike Smith 	ciss_printf(sc, "  interrupt coalesce delay %dus\n",
9923a31b7ebSMike Smith 		    sc->ciss_cfg->interrupt_coalesce_delay);
9933a31b7ebSMike Smith 	ciss_printf(sc, "  interrupt coalesce count %d\n",
9943a31b7ebSMike Smith 		    sc->ciss_cfg->interrupt_coalesce_count);
9953a31b7ebSMike Smith 	ciss_printf(sc, "  max outstanding commands %d\n",
9963a31b7ebSMike Smith 		    sc->ciss_cfg->max_outstanding_commands);
9973a31b7ebSMike Smith 	ciss_printf(sc, "  bus types 0x%b\n", sc->ciss_cfg->bus_types,
9983a31b7ebSMike Smith 		    "\20\1ultra2\2ultra3\10fibre1\11fibre2\n");
9993a31b7ebSMike Smith 	ciss_printf(sc, "  server name '%.16s'\n", sc->ciss_cfg->server_name);
10003a31b7ebSMike Smith 	ciss_printf(sc, "  heartbeat 0x%x\n", sc->ciss_cfg->heartbeat);
10013a31b7ebSMike Smith     }
10023a31b7ebSMike Smith 
10033a31b7ebSMike Smith out:
10043a31b7ebSMike Smith     if (error) {
10053a31b7ebSMike Smith 	if (sc->ciss_id != NULL) {
10063a31b7ebSMike Smith 	    free(sc->ciss_id, CISS_MALLOC_CLASS);
10073a31b7ebSMike Smith 	    sc->ciss_id = NULL;
10083a31b7ebSMike Smith 	}
10093a31b7ebSMike Smith     }
10103a31b7ebSMike Smith     if (cr != NULL)
10113a31b7ebSMike Smith 	ciss_release_request(cr);
10123a31b7ebSMike Smith     return(error);
10133a31b7ebSMike Smith }
10143a31b7ebSMike Smith 
10153a31b7ebSMike Smith /************************************************************************
1016c6131460SPaul Saab  * Helper routine for generating a list of logical and physical luns.
10173a31b7ebSMike Smith  */
1018c6131460SPaul Saab static struct ciss_lun_report *
1019c6131460SPaul Saab ciss_report_luns(struct ciss_softc *sc, int opcode, int nunits)
10203a31b7ebSMike Smith {
10213a31b7ebSMike Smith     struct ciss_request		*cr;
10223a31b7ebSMike Smith     struct ciss_command		*cc;
10233a31b7ebSMike Smith     struct ciss_report_cdb	*crc;
10243a31b7ebSMike Smith     struct ciss_lun_report	*cll;
10253a31b7ebSMike Smith     int				command_status;
1026c6131460SPaul Saab     int				report_size;
1027c6131460SPaul Saab     int				error = 0;
10283a31b7ebSMike Smith 
10293a31b7ebSMike Smith     debug_called(1);
10303a31b7ebSMike Smith 
10313a31b7ebSMike Smith     cr = NULL;
10323a31b7ebSMike Smith     cll = NULL;
10333a31b7ebSMike Smith 
10343a31b7ebSMike Smith     /*
10353a31b7ebSMike Smith      * Get a request, allocate storage for the address list.
10363a31b7ebSMike Smith      */
10373a31b7ebSMike Smith     if ((error = ciss_get_request(sc, &cr)) != 0)
10383a31b7ebSMike Smith 	goto out;
1039c6131460SPaul Saab     report_size = sizeof(*cll) + nunits * sizeof(union ciss_device_address);
10403a31b7ebSMike Smith     if ((cll = malloc(report_size, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
1041c6131460SPaul Saab 	ciss_printf(sc, "can't allocate memory for lun report\n");
10423a31b7ebSMike Smith 	error = ENOMEM;
10433a31b7ebSMike Smith 	goto out;
10443a31b7ebSMike Smith     }
10453a31b7ebSMike Smith 
10463a31b7ebSMike Smith     /*
1047c6131460SPaul Saab      * Build the Report Logical/Physical LUNs command.
10483a31b7ebSMike Smith      */
10493a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
10503a31b7ebSMike Smith     cr->cr_data = cll;
10513a31b7ebSMike Smith     cr->cr_length = report_size;
10523a31b7ebSMike Smith     cr->cr_flags = CISS_REQ_DATAIN;
10533a31b7ebSMike Smith 
10543a31b7ebSMike Smith     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
10553a31b7ebSMike Smith     cc->header.address.physical.bus = 0;
10563a31b7ebSMike Smith     cc->header.address.physical.target = 0;
10573a31b7ebSMike Smith     cc->cdb.cdb_length = sizeof(*crc);
10583a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
10593a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
10603a31b7ebSMike Smith     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
10613a31b7ebSMike Smith     cc->cdb.timeout = 30;	/* XXX better suggestions? */
10623a31b7ebSMike Smith 
10633a31b7ebSMike Smith     crc = (struct ciss_report_cdb *)&(cc->cdb.cdb[0]);
10643a31b7ebSMike Smith     bzero(crc, sizeof(*crc));
1065c6131460SPaul Saab     crc->opcode = opcode;
10663a31b7ebSMike Smith     crc->length = htonl(report_size);			/* big-endian field */
10673a31b7ebSMike Smith     cll->list_size = htonl(report_size - sizeof(*cll));	/* big-endian field */
10683a31b7ebSMike Smith 
10693a31b7ebSMike Smith     /*
10703a31b7ebSMike Smith      * Submit the request and wait for it to complete.  (timeout
10713a31b7ebSMike Smith      * here should be much greater than above)
10723a31b7ebSMike Smith      */
10733a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1074c6131460SPaul Saab 	ciss_printf(sc, "error sending %d LUN command (%d)\n", opcode, error);
10753a31b7ebSMike Smith 	goto out;
10763a31b7ebSMike Smith     }
10773a31b7ebSMike Smith 
10783a31b7ebSMike Smith     /*
10793a31b7ebSMike Smith      * Check response.  Note that data over/underrun is OK.
10803a31b7ebSMike Smith      */
10813a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
10823a31b7ebSMike Smith     switch(command_status) {
10833a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:	/* buffer right size */
10843a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_UNDERRUN:	/* buffer too large, not bad */
10853a31b7ebSMike Smith 	break;
10863a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_OVERRUN:
1087c6131460SPaul Saab 	ciss_printf(sc, "WARNING: more units than driver limit (%d)\n",
10883a31b7ebSMike Smith 		    CISS_MAX_LOGICAL);
10893a31b7ebSMike Smith 	break;
10903a31b7ebSMike Smith     default:
10913a31b7ebSMike Smith 	ciss_printf(sc, "error detecting logical drive configuration (%s)\n",
10923a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
10933a31b7ebSMike Smith 	error = EIO;
10943a31b7ebSMike Smith 	goto out;
10953a31b7ebSMike Smith     }
10963a31b7ebSMike Smith     ciss_release_request(cr);
10973a31b7ebSMike Smith     cr = NULL;
10983a31b7ebSMike Smith 
1099c6131460SPaul Saab out:
1100c6131460SPaul Saab     if (cr != NULL)
1101c6131460SPaul Saab 	ciss_release_request(cr);
1102c6131460SPaul Saab     if (error && cll != NULL) {
1103c6131460SPaul Saab 	free(cll, CISS_MALLOC_CLASS);
1104c6131460SPaul Saab 	cll = NULL;
1105c6131460SPaul Saab     }
1106c6131460SPaul Saab     return(cll);
1107c6131460SPaul Saab }
1108c6131460SPaul Saab 
1109c6131460SPaul Saab /************************************************************************
1110c6131460SPaul Saab  * Find logical drives on the adapter.
1111c6131460SPaul Saab  */
1112c6131460SPaul Saab static int
1113c6131460SPaul Saab ciss_init_logical(struct ciss_softc *sc)
1114c6131460SPaul Saab {
1115c6131460SPaul Saab     struct ciss_lun_report	*cll;
1116c6131460SPaul Saab     int				error = 0, i, j;
1117c6131460SPaul Saab     int				ndrives;
1118c6131460SPaul Saab 
1119c6131460SPaul Saab     debug_called(1);
1120c6131460SPaul Saab 
1121c6131460SPaul Saab     cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
1122c6131460SPaul Saab 			   CISS_MAX_LOGICAL);
1123c6131460SPaul Saab     if (cll == NULL) {
1124c6131460SPaul Saab 	error = ENXIO;
1125c6131460SPaul Saab 	goto out;
1126c6131460SPaul Saab     }
1127c6131460SPaul Saab 
11283a31b7ebSMike Smith     /* sanity-check reply */
11293a31b7ebSMike Smith     ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1130777d1b39SPoul-Henning Kamp     if ((ndrives < 0) || (ndrives >= CISS_MAX_LOGICAL)) {
11313a31b7ebSMike Smith 	ciss_printf(sc, "adapter claims to report absurd number of logical drives (%d > %d)\n",
11323a31b7ebSMike Smith 		    ndrives, CISS_MAX_LOGICAL);
1133c6131460SPaul Saab 	error = ENXIO;
1134c6131460SPaul Saab 	goto out;
11353a31b7ebSMike Smith     }
11363a31b7ebSMike Smith 
11373a31b7ebSMike Smith     /*
11383a31b7ebSMike Smith      * Save logical drive information.
11393a31b7ebSMike Smith      */
1140c6131460SPaul Saab     if (bootverbose) {
1141c6131460SPaul Saab 	ciss_printf(sc, "%d logical drive%s\n",
1142c6131460SPaul Saab 	    ndrives, (ndrives > 1 || ndrives == 0) ? "s" : "");
1143c6131460SPaul Saab     }
1144c6131460SPaul Saab 
1145c6131460SPaul Saab     sc->ciss_logical =
11461fe6c4eeSScott Long 	malloc(sc->ciss_max_logical_bus * sizeof(struct ciss_ldrive *),
1147c6131460SPaul Saab 	       CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1148c6131460SPaul Saab     if (sc->ciss_logical == NULL) {
1149c6131460SPaul Saab 	error = ENXIO;
1150c6131460SPaul Saab 	goto out;
1151c6131460SPaul Saab     }
1152c6131460SPaul Saab 
11531fe6c4eeSScott Long     for (i = 0; i <= sc->ciss_max_logical_bus; i++) {
1154c6131460SPaul Saab 	sc->ciss_logical[i] =
1155c6131460SPaul Saab 	    malloc(CISS_MAX_LOGICAL * sizeof(struct ciss_ldrive),
1156c6131460SPaul Saab 		   CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1157c6131460SPaul Saab 	if (sc->ciss_logical[i] == NULL) {
1158c6131460SPaul Saab 	    error = ENXIO;
1159c6131460SPaul Saab 	    goto out;
1160c6131460SPaul Saab 	}
1161c6131460SPaul Saab 
1162c6131460SPaul Saab 	for (j = 0; j < CISS_MAX_LOGICAL; j++)
1163c6131460SPaul Saab 	    sc->ciss_logical[i][j].cl_status = CISS_LD_NONEXISTENT;
1164c6131460SPaul Saab     }
1165c6131460SPaul Saab 
1166c6131460SPaul Saab 
11673a31b7ebSMike Smith     for (i = 0; i < CISS_MAX_LOGICAL; i++) {
11683a31b7ebSMike Smith 	if (i < ndrives) {
1169c6131460SPaul Saab 	    struct ciss_ldrive	*ld;
1170c6131460SPaul Saab 	    int			bus, target;
1171c6131460SPaul Saab 
1172c6131460SPaul Saab 	    bus		= CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
1173c6131460SPaul Saab 	    target	= CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
1174c6131460SPaul Saab 	    ld		= &sc->ciss_logical[bus][target];
1175c6131460SPaul Saab 
1176c6131460SPaul Saab 	    ld->cl_address	= cll->lun[i];
1177c6131460SPaul Saab 	    ld->cl_controller	= &sc->ciss_controllers[bus];
1178c6131460SPaul Saab 	    if (ciss_identify_logical(sc, ld) != 0)
11793a31b7ebSMike Smith 		continue;
11803a31b7ebSMike Smith 	    /*
11813a31b7ebSMike Smith 	     * If the drive has had media exchanged, we should bring it online.
11823a31b7ebSMike Smith 	     */
1183c6131460SPaul Saab 	    if (ld->cl_lstatus->media_exchanged)
118488c78a38SPaul Saab 		ciss_accept_media(sc, ld);
11853a31b7ebSMike Smith 
11863a31b7ebSMike Smith 	}
11873a31b7ebSMike Smith     }
11883a31b7ebSMike Smith 
11893a31b7ebSMike Smith  out:
11903a31b7ebSMike Smith     if (cll != NULL)
11913a31b7ebSMike Smith 	free(cll, CISS_MALLOC_CLASS);
11923a31b7ebSMike Smith     return(error);
11933a31b7ebSMike Smith }
11943a31b7ebSMike Smith 
1195440baa08SPaul Saab static int
1196c6131460SPaul Saab ciss_init_physical(struct ciss_softc *sc)
1197c6131460SPaul Saab {
1198c6131460SPaul Saab     struct ciss_lun_report	*cll;
1199c6131460SPaul Saab     int				error = 0, i;
1200c6131460SPaul Saab     int				nphys;
12011fe6c4eeSScott Long     int				bus, target;
1202c6131460SPaul Saab 
1203c6131460SPaul Saab     debug_called(1);
1204c6131460SPaul Saab 
12051fe6c4eeSScott Long     bus = 0;
12061fe6c4eeSScott Long     target = 0;
12071fe6c4eeSScott Long 
1208c6131460SPaul Saab     cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
1209c6131460SPaul Saab 			   CISS_MAX_PHYSICAL);
1210c6131460SPaul Saab     if (cll == NULL) {
1211c6131460SPaul Saab 	error = ENXIO;
1212c6131460SPaul Saab 	goto out;
1213c6131460SPaul Saab     }
1214c6131460SPaul Saab 
1215c6131460SPaul Saab     nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1216c6131460SPaul Saab 
1217c6131460SPaul Saab     if (bootverbose) {
1218c6131460SPaul Saab 	ciss_printf(sc, "%d physical device%s\n",
1219c6131460SPaul Saab 	    nphys, (nphys > 1 || nphys == 0) ? "s" : "");
1220c6131460SPaul Saab     }
1221c6131460SPaul Saab 
1222c6131460SPaul Saab     /*
12231fe6c4eeSScott Long      * Figure out the bus mapping.
12241fe6c4eeSScott Long      * Logical buses include both the local logical bus for local arrays and
12251fe6c4eeSScott Long      * proxy buses for remote arrays.  Physical buses are numbered by the
12261fe6c4eeSScott Long      * controller and represent physical buses that hold physical devices.
12271fe6c4eeSScott Long      * We shift these bus numbers so that everything fits into a single flat
12281fe6c4eeSScott Long      * numbering space for CAM.  Logical buses occupy the first 32 CAM bus
12291fe6c4eeSScott Long      * numbers, and the physical bus numbers are shifted to be above that.
12301fe6c4eeSScott Long      * This results in the various driver arrays being indexed as follows:
12311fe6c4eeSScott Long      *
12321fe6c4eeSScott Long      * ciss_controllers[] - indexed by logical bus
12331fe6c4eeSScott Long      * ciss_cam_sim[]     - indexed by both logical and physical, with physical
12341fe6c4eeSScott Long      *                      being shifted by 32.
12351fe6c4eeSScott Long      * ciss_logical[][]   - indexed by logical bus
12361fe6c4eeSScott Long      * ciss_physical[][]  - indexed by physical bus
12371fe6c4eeSScott Long      *
12381fe6c4eeSScott Long      * XXX This is getting more and more hackish.  CISS really doesn't play
12391fe6c4eeSScott Long      *     well with a standard SCSI model; devices are addressed via magic
12401fe6c4eeSScott Long      *     cookies, not via b/t/l addresses.  Since there is no way to store
12411fe6c4eeSScott Long      *     the cookie in the CAM device object, we have to keep these lookup
12421fe6c4eeSScott Long      *     tables handy so that the devices can be found quickly at the cost
12431fe6c4eeSScott Long      *     of wasting memory and having a convoluted lookup scheme.  This
12441fe6c4eeSScott Long      *     driver should probably be converted to block interface.
12451fe6c4eeSScott Long      */
12461fe6c4eeSScott Long     /*
1247c6131460SPaul Saab      * If the L2 and L3 SCSI addresses are 0, this signifies a proxy
1248c6131460SPaul Saab      * controller. A proxy controller is another physical controller
1249c6131460SPaul Saab      * behind the primary PCI controller. We need to know about this
1250c6131460SPaul Saab      * so that BMIC commands can be properly targeted.  There can be
1251c6131460SPaul Saab      * proxy controllers attached to a single PCI controller, so
1252c6131460SPaul Saab      * find the highest numbered one so the array can be properly
1253c6131460SPaul Saab      * sized.
1254c6131460SPaul Saab      */
12551fe6c4eeSScott Long     sc->ciss_max_logical_bus = 1;
1256c6131460SPaul Saab     for (i = 0; i < nphys; i++) {
1257c6131460SPaul Saab 	if (cll->lun[i].physical.extra_address == 0) {
12581fe6c4eeSScott Long 	    bus = cll->lun[i].physical.bus;
12591fe6c4eeSScott Long 	    sc->ciss_max_logical_bus = max(sc->ciss_max_logical_bus, bus) + 1;
12601fe6c4eeSScott Long 	} else {
12611fe6c4eeSScott Long 	    bus = CISS_EXTRA_BUS2(cll->lun[i].physical.extra_address);
12621fe6c4eeSScott Long 	    sc->ciss_max_physical_bus = max(sc->ciss_max_physical_bus, bus);
1263c6131460SPaul Saab 	}
1264c6131460SPaul Saab     }
1265c6131460SPaul Saab 
1266c6131460SPaul Saab     sc->ciss_controllers =
12671fe6c4eeSScott Long 	malloc(sc->ciss_max_logical_bus * sizeof (union ciss_device_address),
1268c6131460SPaul Saab 	       CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1269c6131460SPaul Saab 
1270c6131460SPaul Saab     if (sc->ciss_controllers == NULL) {
1271c6131460SPaul Saab 	ciss_printf(sc, "Could not allocate memory for controller map\n");
1272c6131460SPaul Saab 	error = ENOMEM;
1273c6131460SPaul Saab 	goto out;
1274c6131460SPaul Saab     }
1275c6131460SPaul Saab 
1276c6131460SPaul Saab     /* setup a map of controller addresses */
1277c6131460SPaul Saab     for (i = 0; i < nphys; i++) {
1278c6131460SPaul Saab 	if (cll->lun[i].physical.extra_address == 0) {
1279c6131460SPaul Saab 	    sc->ciss_controllers[cll->lun[i].physical.bus] = cll->lun[i];
1280c6131460SPaul Saab 	}
1281c6131460SPaul Saab     }
1282c6131460SPaul Saab 
12831fe6c4eeSScott Long     sc->ciss_physical =
12841fe6c4eeSScott Long 	malloc(sc->ciss_max_physical_bus * sizeof(struct ciss_pdrive *),
12851fe6c4eeSScott Long 	       CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
12861fe6c4eeSScott Long     if (sc->ciss_physical == NULL) {
12871fe6c4eeSScott Long 	ciss_printf(sc, "Could not allocate memory for physical device map\n");
12881fe6c4eeSScott Long 	error = ENOMEM;
12891fe6c4eeSScott Long 	goto out;
12901fe6c4eeSScott Long     }
12911fe6c4eeSScott Long 
12921fe6c4eeSScott Long     for (i = 0; i < sc->ciss_max_physical_bus; i++) {
12931fe6c4eeSScott Long 	sc->ciss_physical[i] =
12941fe6c4eeSScott Long 	    malloc(sizeof(struct ciss_pdrive) * CISS_MAX_PHYSTGT,
12951fe6c4eeSScott Long 		   CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
12961fe6c4eeSScott Long 	if (sc->ciss_physical[i] == NULL) {
12971fe6c4eeSScott Long 	    ciss_printf(sc, "Could not allocate memory for target map\n");
12981fe6c4eeSScott Long 	    error = ENOMEM;
12991fe6c4eeSScott Long 	    goto out;
13001fe6c4eeSScott Long 	}
13011fe6c4eeSScott Long     }
13021fe6c4eeSScott Long 
13031fe6c4eeSScott Long     ciss_filter_physical(sc, cll);
13041fe6c4eeSScott Long 
1305c6131460SPaul Saab out:
1306c6131460SPaul Saab     if (cll != NULL)
1307c6131460SPaul Saab 	free(cll, CISS_MALLOC_CLASS);
1308c6131460SPaul Saab 
1309c6131460SPaul Saab     return(error);
1310c6131460SPaul Saab }
1311c6131460SPaul Saab 
1312c6131460SPaul Saab static int
13131fe6c4eeSScott Long ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll)
13141fe6c4eeSScott Long {
13151fe6c4eeSScott Long     u_int32_t ea;
13161fe6c4eeSScott Long     int i, nphys;
13171fe6c4eeSScott Long     int	bus, target;
13181fe6c4eeSScott Long 
13191fe6c4eeSScott Long     nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
13201fe6c4eeSScott Long     for (i = 0; i < nphys; i++) {
13211fe6c4eeSScott Long 	if (cll->lun[i].physical.extra_address == 0)
13221fe6c4eeSScott Long 	    continue;
13231fe6c4eeSScott Long 
13241fe6c4eeSScott Long 	/*
13251fe6c4eeSScott Long 	 * Filter out devices that we don't want.  Level 3 LUNs could
13261fe6c4eeSScott Long 	 * probably be supported, but the docs don't give enough of a
13271fe6c4eeSScott Long 	 * hint to know how.
13281fe6c4eeSScott Long 	 *
13291fe6c4eeSScott Long 	 * The mode field of the physical address is likely set to have
13301fe6c4eeSScott Long 	 * hard disks masked out.  Honor it unless the user has overridden
13311fe6c4eeSScott Long 	 * us with the tunable.  We also munge the inquiry data for these
13321fe6c4eeSScott Long 	 * disks so that they only show up as passthrough devices.  Keeping
13331fe6c4eeSScott Long 	 * them visible in this fashion is useful for doing things like
13341fe6c4eeSScott Long 	 * flashing firmware.
13351fe6c4eeSScott Long 	 */
13361fe6c4eeSScott Long 	ea = cll->lun[i].physical.extra_address;
13371fe6c4eeSScott Long 	if ((CISS_EXTRA_BUS3(ea) != 0) || (CISS_EXTRA_TARGET3(ea) != 0) ||
13381fe6c4eeSScott Long 	    (CISS_EXTRA_MODE2(ea) == 0x3))
13391fe6c4eeSScott Long 	    continue;
13401fe6c4eeSScott Long 	if ((ciss_expose_hidden_physical == 0) &&
13411fe6c4eeSScott Long 	   (cll->lun[i].physical.mode == CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL))
13421fe6c4eeSScott Long 	    continue;
13431fe6c4eeSScott Long 
13441fe6c4eeSScott Long 	/*
13451fe6c4eeSScott Long 	 * Note: CISS firmware numbers physical busses starting at '1', not
13461fe6c4eeSScott Long 	 *       '0'.  This numbering is internal to the firmware and is only
13471fe6c4eeSScott Long 	 *       used as a hint here.
13481fe6c4eeSScott Long 	 */
13491fe6c4eeSScott Long 	bus = CISS_EXTRA_BUS2(ea) - 1;
13501fe6c4eeSScott Long 	target = CISS_EXTRA_TARGET2(ea);
13511fe6c4eeSScott Long 	sc->ciss_physical[bus][target].cp_address = cll->lun[i];
13521fe6c4eeSScott Long 	sc->ciss_physical[bus][target].cp_online = 1;
13531fe6c4eeSScott Long     }
13541fe6c4eeSScott Long 
13551fe6c4eeSScott Long     return (0);
13561fe6c4eeSScott Long }
13571fe6c4eeSScott Long 
13581fe6c4eeSScott Long static int
1359440baa08SPaul Saab ciss_inquiry_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1360440baa08SPaul Saab {
1361440baa08SPaul Saab     struct ciss_request			*cr;
1362440baa08SPaul Saab     struct ciss_command			*cc;
1363440baa08SPaul Saab     struct scsi_inquiry			*inq;
1364440baa08SPaul Saab     int					error;
1365440baa08SPaul Saab     int					command_status;
1366440baa08SPaul Saab 
1367440baa08SPaul Saab     cr = NULL;
1368440baa08SPaul Saab 
1369440baa08SPaul Saab     bzero(&ld->cl_geometry, sizeof(ld->cl_geometry));
1370440baa08SPaul Saab 
1371440baa08SPaul Saab     if ((error = ciss_get_request(sc, &cr)) != 0)
1372440baa08SPaul Saab 	goto out;
1373440baa08SPaul Saab 
1374440baa08SPaul Saab     cc = CISS_FIND_COMMAND(cr);
1375440baa08SPaul Saab     cr->cr_data = &ld->cl_geometry;
1376440baa08SPaul Saab     cr->cr_length = sizeof(ld->cl_geometry);
1377440baa08SPaul Saab     cr->cr_flags = CISS_REQ_DATAIN;
1378440baa08SPaul Saab 
1379c6131460SPaul Saab     cc->header.address = ld->cl_address;
1380440baa08SPaul Saab     cc->cdb.cdb_length = 6;
1381440baa08SPaul Saab     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1382440baa08SPaul Saab     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1383440baa08SPaul Saab     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1384440baa08SPaul Saab     cc->cdb.timeout = 30;
1385440baa08SPaul Saab 
1386440baa08SPaul Saab     inq = (struct scsi_inquiry *)&(cc->cdb.cdb[0]);
1387440baa08SPaul Saab     inq->opcode = INQUIRY;
1388440baa08SPaul Saab     inq->byte2 = SI_EVPD;
1389440baa08SPaul Saab     inq->page_code = CISS_VPD_LOGICAL_DRIVE_GEOMETRY;
1390440baa08SPaul Saab     inq->length = sizeof(ld->cl_geometry);
1391440baa08SPaul Saab 
1392440baa08SPaul Saab     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1393440baa08SPaul Saab 	ciss_printf(sc, "error getting geometry (%d)\n", error);
1394440baa08SPaul Saab 	goto out;
1395440baa08SPaul Saab     }
1396440baa08SPaul Saab 
1397440baa08SPaul Saab     ciss_report_request(cr, &command_status, NULL);
1398440baa08SPaul Saab     switch(command_status) {
1399440baa08SPaul Saab     case CISS_CMD_STATUS_SUCCESS:
1400440baa08SPaul Saab     case CISS_CMD_STATUS_DATA_UNDERRUN:
1401440baa08SPaul Saab 	break;
1402440baa08SPaul Saab     case CISS_CMD_STATUS_DATA_OVERRUN:
1403440baa08SPaul Saab 	ciss_printf(sc, "WARNING: Data overrun\n");
1404440baa08SPaul Saab 	break;
1405440baa08SPaul Saab     default:
1406440baa08SPaul Saab 	ciss_printf(sc, "Error detecting logical drive geometry (%s)\n",
1407440baa08SPaul Saab 		    ciss_name_command_status(command_status));
1408440baa08SPaul Saab 	break;
1409440baa08SPaul Saab     }
1410440baa08SPaul Saab 
1411440baa08SPaul Saab out:
1412440baa08SPaul Saab     if (cr != NULL)
1413440baa08SPaul Saab 	ciss_release_request(cr);
1414440baa08SPaul Saab     return(error);
1415440baa08SPaul Saab }
14163a31b7ebSMike Smith /************************************************************************
14173a31b7ebSMike Smith  * Identify a logical drive, initialise state related to it.
14183a31b7ebSMike Smith  */
14193a31b7ebSMike Smith static int
14203a31b7ebSMike Smith ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
14213a31b7ebSMike Smith {
14223a31b7ebSMike Smith     struct ciss_request		*cr;
14233a31b7ebSMike Smith     struct ciss_command		*cc;
14243a31b7ebSMike Smith     struct ciss_bmic_cdb	*cbc;
14253a31b7ebSMike Smith     int				error, command_status;
14263a31b7ebSMike Smith 
14273a31b7ebSMike Smith     debug_called(1);
14283a31b7ebSMike Smith 
14293a31b7ebSMike Smith     cr = NULL;
14303a31b7ebSMike Smith 
14313a31b7ebSMike Smith     /*
14323a31b7ebSMike Smith      * Build a BMIC request to fetch the drive ID.
14333a31b7ebSMike Smith      */
14343a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LDRIVE,
14353a31b7ebSMike Smith 				       (void **)&ld->cl_ldrive,
14363a31b7ebSMike Smith 				       sizeof(*ld->cl_ldrive))) != 0)
14373a31b7ebSMike Smith 	goto out;
14383a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
1439c6131460SPaul Saab     cc->header.address = *ld->cl_controller;	/* target controller */
14403a31b7ebSMike Smith     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1441c6131460SPaul Saab     cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
14423a31b7ebSMike Smith 
14433a31b7ebSMike Smith     /*
14443a31b7ebSMike Smith      * Submit the request and wait for it to complete.
14453a31b7ebSMike Smith      */
14463a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
14473a31b7ebSMike Smith 	ciss_printf(sc, "error sending BMIC LDRIVE command (%d)\n", error);
14483a31b7ebSMike Smith 	goto out;
14493a31b7ebSMike Smith     }
14503a31b7ebSMike Smith 
14513a31b7ebSMike Smith     /*
14523a31b7ebSMike Smith      * Check response.
14533a31b7ebSMike Smith      */
14543a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
14553a31b7ebSMike Smith     switch(command_status) {
14563a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
14573a31b7ebSMike Smith 	break;
14583a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_UNDERRUN:
14593a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_OVERRUN:
14603a31b7ebSMike Smith 	ciss_printf(sc, "data over/underrun reading logical drive ID\n");
14613a31b7ebSMike Smith     default:
14623a31b7ebSMike Smith 	ciss_printf(sc, "error reading logical drive ID (%s)\n",
14633a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
14643a31b7ebSMike Smith 	error = EIO;
14653a31b7ebSMike Smith 	goto out;
14663a31b7ebSMike Smith     }
14673a31b7ebSMike Smith     ciss_release_request(cr);
14683a31b7ebSMike Smith     cr = NULL;
14693a31b7ebSMike Smith 
14703a31b7ebSMike Smith     /*
14713a31b7ebSMike Smith      * Build a CISS BMIC command to get the logical drive status.
14723a31b7ebSMike Smith      */
14733a31b7ebSMike Smith     if ((error = ciss_get_ldrive_status(sc, ld)) != 0)
14743a31b7ebSMike Smith 	goto out;
14753a31b7ebSMike Smith 
14763a31b7ebSMike Smith     /*
1477440baa08SPaul Saab      * Get the logical drive geometry.
1478440baa08SPaul Saab      */
1479440baa08SPaul Saab     if ((error = ciss_inquiry_logical(sc, ld)) != 0)
1480440baa08SPaul Saab 	goto out;
1481440baa08SPaul Saab 
1482440baa08SPaul Saab     /*
14833a31b7ebSMike Smith      * Print the drive's basic characteristics.
14843a31b7ebSMike Smith      */
14854bca277bSPaul Saab     if (bootverbose) {
1486c6131460SPaul Saab 	ciss_printf(sc, "logical drive (b%dt%d): %s, %dMB ",
1487c6131460SPaul Saab 		    CISS_LUN_TO_BUS(ld->cl_address.logical.lun),
1488c6131460SPaul Saab 		    CISS_LUN_TO_TARGET(ld->cl_address.logical.lun),
148979adbf76SPaul Saab 		    ciss_name_ldrive_org(ld->cl_ldrive->fault_tolerance),
14903a31b7ebSMike Smith 		    ((ld->cl_ldrive->blocks_available / (1024 * 1024)) *
14913a31b7ebSMike Smith 		     ld->cl_ldrive->block_size));
14923a31b7ebSMike Smith 
14933a31b7ebSMike Smith 	ciss_print_ldrive(sc, ld);
14943a31b7ebSMike Smith     }
14953a31b7ebSMike Smith out:
14963a31b7ebSMike Smith     if (error != 0) {
14973a31b7ebSMike Smith 	/* make the drive not-exist */
14983a31b7ebSMike Smith 	ld->cl_status = CISS_LD_NONEXISTENT;
14993a31b7ebSMike Smith 	if (ld->cl_ldrive != NULL) {
15003a31b7ebSMike Smith 	    free(ld->cl_ldrive, CISS_MALLOC_CLASS);
15013a31b7ebSMike Smith 	    ld->cl_ldrive = NULL;
15023a31b7ebSMike Smith 	}
15033a31b7ebSMike Smith 	if (ld->cl_lstatus != NULL) {
15043a31b7ebSMike Smith 	    free(ld->cl_lstatus, CISS_MALLOC_CLASS);
15053a31b7ebSMike Smith 	    ld->cl_lstatus = NULL;
15063a31b7ebSMike Smith 	}
15073a31b7ebSMike Smith     }
15083a31b7ebSMike Smith     if (cr != NULL)
15093a31b7ebSMike Smith 	ciss_release_request(cr);
15103a31b7ebSMike Smith 
15113a31b7ebSMike Smith     return(error);
15123a31b7ebSMike Smith }
15133a31b7ebSMike Smith 
15143a31b7ebSMike Smith /************************************************************************
15153a31b7ebSMike Smith  * Get status for a logical drive.
15163a31b7ebSMike Smith  *
15173a31b7ebSMike Smith  * XXX should we also do this in response to Test Unit Ready?
15183a31b7ebSMike Smith  */
15193a31b7ebSMike Smith static int
15203a31b7ebSMike Smith ciss_get_ldrive_status(struct ciss_softc *sc,  struct ciss_ldrive *ld)
15213a31b7ebSMike Smith {
15223a31b7ebSMike Smith     struct ciss_request		*cr;
15233a31b7ebSMike Smith     struct ciss_command		*cc;
15243a31b7ebSMike Smith     struct ciss_bmic_cdb	*cbc;
15253a31b7ebSMike Smith     int				error, command_status;
15263a31b7ebSMike Smith 
15273a31b7ebSMike Smith     /*
15283a31b7ebSMike Smith      * Build a CISS BMIC command to get the logical drive status.
15293a31b7ebSMike Smith      */
15303a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LSTATUS,
15313a31b7ebSMike Smith 				       (void **)&ld->cl_lstatus,
15323a31b7ebSMike Smith 				       sizeof(*ld->cl_lstatus))) != 0)
15333a31b7ebSMike Smith 	goto out;
15343a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
1535c6131460SPaul Saab     cc->header.address = *ld->cl_controller;	/* target controller */
15363a31b7ebSMike Smith     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1537c6131460SPaul Saab     cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
15383a31b7ebSMike Smith 
15393a31b7ebSMike Smith     /*
15403a31b7ebSMike Smith      * Submit the request and wait for it to complete.
15413a31b7ebSMike Smith      */
15423a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
15433a31b7ebSMike Smith 	ciss_printf(sc, "error sending BMIC LSTATUS command (%d)\n", error);
15443a31b7ebSMike Smith 	goto out;
15453a31b7ebSMike Smith     }
15463a31b7ebSMike Smith 
15473a31b7ebSMike Smith     /*
15483a31b7ebSMike Smith      * Check response.
15493a31b7ebSMike Smith      */
15503a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
15513a31b7ebSMike Smith     switch(command_status) {
15523a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
15533a31b7ebSMike Smith 	break;
15543a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_UNDERRUN:
15553a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_OVERRUN:
15563a31b7ebSMike Smith 	ciss_printf(sc, "data over/underrun reading logical drive status\n");
15573a31b7ebSMike Smith     default:
15583a31b7ebSMike Smith 	ciss_printf(sc, "error reading logical drive status (%s)\n",
15593a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
15603a31b7ebSMike Smith 	error = EIO;
15613a31b7ebSMike Smith 	goto out;
15623a31b7ebSMike Smith     }
15633a31b7ebSMike Smith 
15643a31b7ebSMike Smith     /*
15653a31b7ebSMike Smith      * Set the drive's summary status based on the returned status.
15663a31b7ebSMike Smith      *
15673a31b7ebSMike Smith      * XXX testing shows that a failed JBOD drive comes back at next
15683a31b7ebSMike Smith      * boot in "queued for expansion" mode.  WTF?
15693a31b7ebSMike Smith      */
15703a31b7ebSMike Smith     ld->cl_status = ciss_decode_ldrive_status(ld->cl_lstatus->status);
15713a31b7ebSMike Smith 
15723a31b7ebSMike Smith out:
15733a31b7ebSMike Smith     if (cr != NULL)
15743a31b7ebSMike Smith 	ciss_release_request(cr);
15753a31b7ebSMike Smith     return(error);
15763a31b7ebSMike Smith }
15773a31b7ebSMike Smith 
15783a31b7ebSMike Smith /************************************************************************
15793a31b7ebSMike Smith  * Notify the adapter of a config update.
15803a31b7ebSMike Smith  */
15813a31b7ebSMike Smith static int
15823a31b7ebSMike Smith ciss_update_config(struct ciss_softc *sc)
15833a31b7ebSMike Smith {
15843a31b7ebSMike Smith     int		i;
15853a31b7ebSMike Smith 
15863a31b7ebSMike Smith     debug_called(1);
15873a31b7ebSMike Smith 
15883a31b7ebSMike Smith     CISS_TL_SIMPLE_WRITE(sc, CISS_TL_SIMPLE_IDBR, CISS_TL_SIMPLE_IDBR_CFG_TABLE);
15893a31b7ebSMike Smith     for (i = 0; i < 1000; i++) {
15903a31b7ebSMike Smith 	if (!(CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR) &
15913a31b7ebSMike Smith 	      CISS_TL_SIMPLE_IDBR_CFG_TABLE)) {
15923a31b7ebSMike Smith 	    return(0);
15933a31b7ebSMike Smith 	}
15943a31b7ebSMike Smith 	DELAY(1000);
15953a31b7ebSMike Smith     }
15963a31b7ebSMike Smith     return(1);
15973a31b7ebSMike Smith }
15983a31b7ebSMike Smith 
15993a31b7ebSMike Smith /************************************************************************
16003a31b7ebSMike Smith  * Accept new media into a logical drive.
16013a31b7ebSMike Smith  *
16023a31b7ebSMike Smith  * XXX The drive has previously been offline; it would be good if we
16033a31b7ebSMike Smith  *     could make sure it's not open right now.
16043a31b7ebSMike Smith  */
16053a31b7ebSMike Smith static int
160688c78a38SPaul Saab ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld)
16073a31b7ebSMike Smith {
16083a31b7ebSMike Smith     struct ciss_request		*cr;
16093a31b7ebSMike Smith     struct ciss_command		*cc;
16103a31b7ebSMike Smith     struct ciss_bmic_cdb	*cbc;
161188c78a38SPaul Saab     int				command_status;
161288c78a38SPaul Saab     int				error = 0, ldrive;
1613609caf8dSPaul Saab 
1614609caf8dSPaul Saab     ldrive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
16153a31b7ebSMike Smith 
161688c78a38SPaul Saab     debug(0, "bringing logical drive %d back online");
16173a31b7ebSMike Smith 
16183a31b7ebSMike Smith     /*
16193a31b7ebSMike Smith      * Build a CISS BMIC command to bring the drive back online.
16203a31b7ebSMike Smith      */
16213a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ACCEPT_MEDIA,
16223a31b7ebSMike Smith 				       NULL, 0)) != 0)
16233a31b7ebSMike Smith 	goto out;
16243a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
1625c6131460SPaul Saab     cc->header.address = *ld->cl_controller;	/* target controller */
16263a31b7ebSMike Smith     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1627609caf8dSPaul Saab     cbc->log_drive = ldrive;
16283a31b7ebSMike Smith 
16293a31b7ebSMike Smith     /*
16303a31b7ebSMike Smith      * Submit the request and wait for it to complete.
16313a31b7ebSMike Smith      */
16323a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
163388c78a38SPaul Saab 	ciss_printf(sc, "error sending BMIC ACCEPT MEDIA command (%d)\n", error);
16343a31b7ebSMike Smith 	goto out;
16353a31b7ebSMike Smith     }
16363a31b7ebSMike Smith 
16373a31b7ebSMike Smith     /*
16383a31b7ebSMike Smith      * Check response.
16393a31b7ebSMike Smith      */
16403a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
16413a31b7ebSMike Smith     switch(command_status) {
16423a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:		/* all OK */
16433a31b7ebSMike Smith 	/* we should get a logical drive status changed event here */
16443a31b7ebSMike Smith 	break;
16453a31b7ebSMike Smith     default:
16463a31b7ebSMike Smith 	ciss_printf(cr->cr_sc, "error accepting media into failed logical drive (%s)\n",
16473a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
16483a31b7ebSMike Smith 	break;
16493a31b7ebSMike Smith     }
165088c78a38SPaul Saab 
165188c78a38SPaul Saab out:
165288c78a38SPaul Saab     if (cr != NULL)
16533a31b7ebSMike Smith 	ciss_release_request(cr);
165488c78a38SPaul Saab     return(error);
16553a31b7ebSMike Smith }
16563a31b7ebSMike Smith 
16573a31b7ebSMike Smith /************************************************************************
16583a31b7ebSMike Smith  * Release adapter resources.
16593a31b7ebSMike Smith  */
16603a31b7ebSMike Smith static void
16613a31b7ebSMike Smith ciss_free(struct ciss_softc *sc)
16623a31b7ebSMike Smith {
16633284b9eeSPaul Saab     struct ciss_request *cr;
1664ee626b40SPaul Saab     int			i, j;
16653284b9eeSPaul Saab 
16663a31b7ebSMike Smith     debug_called(1);
16673a31b7ebSMike Smith 
16683a31b7ebSMike Smith     /* we're going away */
16693a31b7ebSMike Smith     sc->ciss_flags |= CISS_FLAG_ABORTING;
16703a31b7ebSMike Smith 
16713a31b7ebSMike Smith     /* terminate the periodic heartbeat routine */
16722472e51eSScott Long     callout_stop(&sc->ciss_periodic);
16733a31b7ebSMike Smith 
16743a31b7ebSMike Smith     /* cancel the Event Notify chain */
16753a31b7ebSMike Smith     ciss_notify_abort(sc);
16763a31b7ebSMike Smith 
1677c6131460SPaul Saab     ciss_kill_notify_thread(sc);
1678c6131460SPaul Saab 
16792472e51eSScott Long     /* disconnect from CAM */
16802472e51eSScott Long     if (sc->ciss_cam_sim) {
16812472e51eSScott Long 	for (i = 0; i < sc->ciss_max_logical_bus; i++) {
16822472e51eSScott Long 	    if (sc->ciss_cam_sim[i]) {
16832472e51eSScott Long 		xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
16842472e51eSScott Long 		cam_sim_free(sc->ciss_cam_sim[i], 0);
16852472e51eSScott Long 	    }
16862472e51eSScott Long 	}
16872472e51eSScott Long 	for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
16882472e51eSScott Long 	     CISS_PHYSICAL_BASE; i++) {
16892472e51eSScott Long 	    if (sc->ciss_cam_sim[i]) {
16902472e51eSScott Long 		xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
16912472e51eSScott Long 		cam_sim_free(sc->ciss_cam_sim[i], 0);
16922472e51eSScott Long 	    }
16932472e51eSScott Long 	}
16942472e51eSScott Long 	free(sc->ciss_cam_sim, CISS_MALLOC_CLASS);
16952472e51eSScott Long     }
16962472e51eSScott Long     if (sc->ciss_cam_devq)
16972472e51eSScott Long 	cam_simq_free(sc->ciss_cam_devq);
16982472e51eSScott Long 
169978d03361SPaul Saab     /* remove the control device */
17002472e51eSScott Long     mtx_unlock(&sc->ciss_mtx);
170178d03361SPaul Saab     if (sc->ciss_dev_t != NULL)
170278d03361SPaul Saab 	destroy_dev(sc->ciss_dev_t);
170378d03361SPaul Saab 
17042472e51eSScott Long     /* Final cleanup of the callout. */
17052472e51eSScott Long     callout_drain(&sc->ciss_periodic);
17062472e51eSScott Long     mtx_destroy(&sc->ciss_mtx);
17072472e51eSScott Long 
17083a31b7ebSMike Smith     /* free the controller data */
17093a31b7ebSMike Smith     if (sc->ciss_id != NULL)
17103a31b7ebSMike Smith 	free(sc->ciss_id, CISS_MALLOC_CLASS);
17113a31b7ebSMike Smith 
17123a31b7ebSMike Smith     /* release I/O resources */
17133a31b7ebSMike Smith     if (sc->ciss_regs_resource != NULL)
17143a31b7ebSMike Smith 	bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
17153a31b7ebSMike Smith 			     sc->ciss_regs_rid, sc->ciss_regs_resource);
17163a31b7ebSMike Smith     if (sc->ciss_cfg_resource != NULL)
17173a31b7ebSMike Smith 	bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
17183a31b7ebSMike Smith 			     sc->ciss_cfg_rid, sc->ciss_cfg_resource);
17193a31b7ebSMike Smith     if (sc->ciss_intr != NULL)
17203a31b7ebSMike Smith 	bus_teardown_intr(sc->ciss_dev, sc->ciss_irq_resource, sc->ciss_intr);
17213a31b7ebSMike Smith     if (sc->ciss_irq_resource != NULL)
17223a31b7ebSMike Smith 	bus_release_resource(sc->ciss_dev, SYS_RES_IRQ,
17233a31b7ebSMike Smith 			     sc->ciss_irq_rid, sc->ciss_irq_resource);
17243a31b7ebSMike Smith 
17253a31b7ebSMike Smith     /* destroy DMA tags */
17263a31b7ebSMike Smith     if (sc->ciss_parent_dmat)
17273a31b7ebSMike Smith 	bus_dma_tag_destroy(sc->ciss_parent_dmat);
17283284b9eeSPaul Saab 
17293284b9eeSPaul Saab     while ((cr = ciss_dequeue_free(sc)) != NULL)
17303284b9eeSPaul Saab 	bus_dmamap_destroy(sc->ciss_buffer_dmat, cr->cr_datamap);
17313a31b7ebSMike Smith     if (sc->ciss_buffer_dmat)
17323a31b7ebSMike Smith 	bus_dma_tag_destroy(sc->ciss_buffer_dmat);
17333a31b7ebSMike Smith 
17343a31b7ebSMike Smith     /* destroy command memory and DMA tag */
17353a31b7ebSMike Smith     if (sc->ciss_command != NULL) {
17363a31b7ebSMike Smith 	bus_dmamap_unload(sc->ciss_command_dmat, sc->ciss_command_map);
17373a31b7ebSMike Smith 	bus_dmamem_free(sc->ciss_command_dmat, sc->ciss_command, sc->ciss_command_map);
17383a31b7ebSMike Smith     }
17393284b9eeSPaul Saab     if (sc->ciss_command_dmat)
17403a31b7ebSMike Smith 	bus_dma_tag_destroy(sc->ciss_command_dmat);
17413a31b7ebSMike Smith 
1742c6131460SPaul Saab     if (sc->ciss_logical) {
1743ee626b40SPaul Saab 	for (i = 0; i <= sc->ciss_max_logical_bus; i++) {
1744ee626b40SPaul Saab 	    for (j = 0; j < CISS_MAX_LOGICAL; j++) {
1745ee626b40SPaul Saab 		if (sc->ciss_logical[i][j].cl_ldrive)
1746ee626b40SPaul Saab 		    free(sc->ciss_logical[i][j].cl_ldrive, CISS_MALLOC_CLASS);
1747ee626b40SPaul Saab 		if (sc->ciss_logical[i][j].cl_lstatus)
1748ee626b40SPaul Saab 		    free(sc->ciss_logical[i][j].cl_lstatus, CISS_MALLOC_CLASS);
1749ee626b40SPaul Saab 	    }
1750c6131460SPaul Saab 	    free(sc->ciss_logical[i], CISS_MALLOC_CLASS);
1751ee626b40SPaul Saab 	}
1752c6131460SPaul Saab 	free(sc->ciss_logical, CISS_MALLOC_CLASS);
1753c6131460SPaul Saab     }
1754c6131460SPaul Saab 
17551fe6c4eeSScott Long     if (sc->ciss_physical) {
17561fe6c4eeSScott Long 	for (i = 0; i < sc->ciss_max_physical_bus; i++)
17571fe6c4eeSScott Long 	    free(sc->ciss_physical[i], CISS_MALLOC_CLASS);
17581fe6c4eeSScott Long 	free(sc->ciss_physical, CISS_MALLOC_CLASS);
17591fe6c4eeSScott Long     }
17601fe6c4eeSScott Long 
1761c6131460SPaul Saab     if (sc->ciss_controllers)
1762c6131460SPaul Saab 	free(sc->ciss_controllers, CISS_MALLOC_CLASS);
1763975b7318SScott Long 
17643a31b7ebSMike Smith }
17653a31b7ebSMike Smith 
17663a31b7ebSMike Smith /************************************************************************
17673a31b7ebSMike Smith  * Give a command to the adapter.
17683a31b7ebSMike Smith  *
17693a31b7ebSMike Smith  * Note that this uses the simple transport layer directly.  If we
17703a31b7ebSMike Smith  * want to add support for other layers, we'll need a switch of some
17713a31b7ebSMike Smith  * sort.
17723a31b7ebSMike Smith  *
17733a31b7ebSMike Smith  * Note that the simple transport layer has no way of refusing a
17743a31b7ebSMike Smith  * command; we only have as many request structures as the adapter
17753a31b7ebSMike Smith  * supports commands, so we don't have to check (this presumes that
17763a31b7ebSMike Smith  * the adapter can handle commands as fast as we throw them at it).
17773a31b7ebSMike Smith  */
17783a31b7ebSMike Smith static int
17793a31b7ebSMike Smith ciss_start(struct ciss_request *cr)
17803a31b7ebSMike Smith {
17813a31b7ebSMike Smith     struct ciss_command	*cc;	/* XXX debugging only */
17823a31b7ebSMike Smith     int			error;
17833a31b7ebSMike Smith 
17843a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
17853a31b7ebSMike Smith     debug(2, "post command %d tag %d ", cr->cr_tag, cc->header.host_tag);
17863a31b7ebSMike Smith 
17873a31b7ebSMike Smith     /*
17883a31b7ebSMike Smith      * Map the request's data.
17893a31b7ebSMike Smith      */
17903a31b7ebSMike Smith     if ((error = ciss_map_request(cr)))
17913a31b7ebSMike Smith 	return(error);
17923a31b7ebSMike Smith 
17933a31b7ebSMike Smith #if 0
17943a31b7ebSMike Smith     ciss_print_request(cr);
17953a31b7ebSMike Smith #endif
17963a31b7ebSMike Smith 
17973a31b7ebSMike Smith     return(0);
17983a31b7ebSMike Smith }
17993a31b7ebSMike Smith 
18003a31b7ebSMike Smith /************************************************************************
18013a31b7ebSMike Smith  * Fetch completed request(s) from the adapter, queue them for
18023a31b7ebSMike Smith  * completion handling.
18033a31b7ebSMike Smith  *
18043a31b7ebSMike Smith  * Note that this uses the simple transport layer directly.  If we
18053a31b7ebSMike Smith  * want to add support for other layers, we'll need a switch of some
18063a31b7ebSMike Smith  * sort.
18073a31b7ebSMike Smith  *
18083a31b7ebSMike Smith  * Note that the simple transport mechanism does not require any
18093a31b7ebSMike Smith  * reentrancy protection; the OPQ read is atomic.  If there is a
18103a31b7ebSMike Smith  * chance of a race with something else that might move the request
18113a31b7ebSMike Smith  * off the busy list, then we will have to lock against that
18123a31b7ebSMike Smith  * (eg. timeouts, etc.)
18133a31b7ebSMike Smith  */
18143a31b7ebSMike Smith static void
18153a31b7ebSMike Smith ciss_done(struct ciss_softc *sc)
18163a31b7ebSMike Smith {
18173a31b7ebSMike Smith     struct ciss_request	*cr;
18183a31b7ebSMike Smith     struct ciss_command	*cc;
18193a31b7ebSMike Smith     u_int32_t		tag, index;
18203a31b7ebSMike Smith     int			complete;
18213a31b7ebSMike Smith 
18223a31b7ebSMike Smith     debug_called(3);
18233a31b7ebSMike Smith 
18243a31b7ebSMike Smith     /*
18253a31b7ebSMike Smith      * Loop quickly taking requests from the adapter and moving them
18263a31b7ebSMike Smith      * from the busy queue to the completed queue.
18273a31b7ebSMike Smith      */
18283a31b7ebSMike Smith     complete = 0;
18293a31b7ebSMike Smith     for (;;) {
18303a31b7ebSMike Smith 
18313a31b7ebSMike Smith 	/* see if the OPQ contains anything */
18323a31b7ebSMike Smith 	if (!CISS_TL_SIMPLE_OPQ_INTERRUPT(sc))
18333a31b7ebSMike Smith 	    break;
18343a31b7ebSMike Smith 
18353a31b7ebSMike Smith 	tag = CISS_TL_SIMPLE_FETCH_CMD(sc);
18363a31b7ebSMike Smith 	if (tag == CISS_TL_SIMPLE_OPQ_EMPTY)
18373a31b7ebSMike Smith 	    break;
18383a31b7ebSMike Smith 	index = tag >> 2;
18393a31b7ebSMike Smith 	debug(2, "completed command %d%s", index,
18403a31b7ebSMike Smith 	      (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
18413a31b7ebSMike Smith 	if (index >= sc->ciss_max_requests) {
18423a31b7ebSMike Smith 	    ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
18433a31b7ebSMike Smith 	    continue;
18443a31b7ebSMike Smith 	}
18453a31b7ebSMike Smith 	cr = &(sc->ciss_request[index]);
18463a31b7ebSMike Smith 	cc = CISS_FIND_COMMAND(cr);
18473a31b7ebSMike Smith 	cc->header.host_tag = tag;	/* not updated by adapter */
18483a31b7ebSMike Smith 	if (ciss_remove_busy(cr)) {
18493a31b7ebSMike Smith 	    /* assume this is garbage out of the adapter */
18503a31b7ebSMike Smith 	    ciss_printf(sc, "completed nonbusy request %d\n", index);
18513a31b7ebSMike Smith 	} else {
18523a31b7ebSMike Smith 	    ciss_enqueue_complete(cr);
18533a31b7ebSMike Smith 	}
18543a31b7ebSMike Smith 	complete = 1;
18553a31b7ebSMike Smith     }
18563a31b7ebSMike Smith 
18573a31b7ebSMike Smith     /*
18583a31b7ebSMike Smith      * Invoke completion processing.  If we can defer this out of
18593a31b7ebSMike Smith      * interrupt context, that'd be good.
18603a31b7ebSMike Smith      */
18613a31b7ebSMike Smith     if (complete)
18623a31b7ebSMike Smith 	ciss_complete(sc);
18633a31b7ebSMike Smith }
18643a31b7ebSMike Smith 
18653a31b7ebSMike Smith /************************************************************************
18663a31b7ebSMike Smith  * Take an interrupt from the adapter.
18673a31b7ebSMike Smith  */
18683a31b7ebSMike Smith static void
18693a31b7ebSMike Smith ciss_intr(void *arg)
18703a31b7ebSMike Smith {
18713a31b7ebSMike Smith     struct ciss_softc	*sc = (struct ciss_softc *)arg;
18723a31b7ebSMike Smith 
18733a31b7ebSMike Smith     /*
18743a31b7ebSMike Smith      * The only interrupt we recognise indicates that there are
18753a31b7ebSMike Smith      * entries in the outbound post queue.
18763a31b7ebSMike Smith      */
1877975b7318SScott Long     mtx_lock(&sc->ciss_mtx);
18783a31b7ebSMike Smith     ciss_done(sc);
1879975b7318SScott Long     mtx_unlock(&sc->ciss_mtx);
18803a31b7ebSMike Smith }
18813a31b7ebSMike Smith 
18823a31b7ebSMike Smith /************************************************************************
18833a31b7ebSMike Smith  * Process completed requests.
18843a31b7ebSMike Smith  *
18853a31b7ebSMike Smith  * Requests can be completed in three fashions:
18863a31b7ebSMike Smith  *
18873a31b7ebSMike Smith  * - by invoking a callback function (cr_complete is non-null)
18883a31b7ebSMike Smith  * - by waking up a sleeper (cr_flags has CISS_REQ_SLEEP set)
18893a31b7ebSMike Smith  * - by clearing the CISS_REQ_POLL flag in interrupt/timeout context
18903a31b7ebSMike Smith  */
18913a31b7ebSMike Smith static void
18923a31b7ebSMike Smith ciss_complete(struct ciss_softc *sc)
18933a31b7ebSMike Smith {
18943a31b7ebSMike Smith     struct ciss_request	*cr;
18953a31b7ebSMike Smith 
18963a31b7ebSMike Smith     debug_called(2);
18973a31b7ebSMike Smith 
18983a31b7ebSMike Smith     /*
18993a31b7ebSMike Smith      * Loop taking requests off the completed queue and performing
19003a31b7ebSMike Smith      * completion processing on them.
19013a31b7ebSMike Smith      */
19023a31b7ebSMike Smith     for (;;) {
19033a31b7ebSMike Smith 	if ((cr = ciss_dequeue_complete(sc)) == NULL)
19043a31b7ebSMike Smith 	    break;
19053a31b7ebSMike Smith 	ciss_unmap_request(cr);
19063a31b7ebSMike Smith 
19073a31b7ebSMike Smith 	/*
19083a31b7ebSMike Smith 	 * If the request has a callback, invoke it.
19093a31b7ebSMike Smith 	 */
19103a31b7ebSMike Smith 	if (cr->cr_complete != NULL) {
19113a31b7ebSMike Smith 	    cr->cr_complete(cr);
19123a31b7ebSMike Smith 	    continue;
19133a31b7ebSMike Smith 	}
19143a31b7ebSMike Smith 
19153a31b7ebSMike Smith 	/*
19163a31b7ebSMike Smith 	 * If someone is sleeping on this request, wake them up.
19173a31b7ebSMike Smith 	 */
19183a31b7ebSMike Smith 	if (cr->cr_flags & CISS_REQ_SLEEP) {
19193a31b7ebSMike Smith 	    cr->cr_flags &= ~CISS_REQ_SLEEP;
19203a31b7ebSMike Smith 	    wakeup(cr);
19213a31b7ebSMike Smith 	    continue;
19223a31b7ebSMike Smith 	}
19233a31b7ebSMike Smith 
19243a31b7ebSMike Smith 	/*
19253a31b7ebSMike Smith 	 * If someone is polling this request for completion, signal.
19263a31b7ebSMike Smith 	 */
19273a31b7ebSMike Smith 	if (cr->cr_flags & CISS_REQ_POLL) {
19283a31b7ebSMike Smith 	    cr->cr_flags &= ~CISS_REQ_POLL;
19293a31b7ebSMike Smith 	    continue;
19303a31b7ebSMike Smith 	}
19313a31b7ebSMike Smith 
19323a31b7ebSMike Smith 	/*
19333a31b7ebSMike Smith 	 * Give up and throw the request back on the free queue.  This
19343a31b7ebSMike Smith 	 * should never happen; resources will probably be lost.
19353a31b7ebSMike Smith 	 */
19363a31b7ebSMike Smith 	ciss_printf(sc, "WARNING: completed command with no submitter\n");
19373a31b7ebSMike Smith 	ciss_enqueue_free(cr);
19383a31b7ebSMike Smith     }
19393a31b7ebSMike Smith }
19403a31b7ebSMike Smith 
19413a31b7ebSMike Smith /************************************************************************
19423a31b7ebSMike Smith  * Report on the completion status of a request, and pass back SCSI
19433a31b7ebSMike Smith  * and command status values.
19443a31b7ebSMike Smith  */
19453a31b7ebSMike Smith static int
19463a31b7ebSMike Smith ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status)
19473a31b7ebSMike Smith {
19483a31b7ebSMike Smith     struct ciss_command		*cc;
19493a31b7ebSMike Smith     struct ciss_error_info	*ce;
19503a31b7ebSMike Smith 
19513a31b7ebSMike Smith     debug_called(2);
19523a31b7ebSMike Smith 
19533a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
19543a31b7ebSMike Smith     ce = (struct ciss_error_info *)&(cc->sg[0]);
19553a31b7ebSMike Smith 
19563a31b7ebSMike Smith     /*
19573a31b7ebSMike Smith      * We don't consider data under/overrun an error for the Report
19583a31b7ebSMike Smith      * Logical/Physical LUNs commands.
19593a31b7ebSMike Smith      */
19603a31b7ebSMike Smith     if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) &&
19612514c5bfSPaul Saab 	((ce->command_status == CISS_CMD_STATUS_DATA_OVERRUN) ||
19622514c5bfSPaul Saab 	 (ce->command_status == CISS_CMD_STATUS_DATA_UNDERRUN)) &&
19633a31b7ebSMike Smith 	((cc->cdb.cdb[0] == CISS_OPCODE_REPORT_LOGICAL_LUNS) ||
19642514c5bfSPaul Saab 	 (cc->cdb.cdb[0] == CISS_OPCODE_REPORT_PHYSICAL_LUNS) ||
19652514c5bfSPaul Saab 	 (cc->cdb.cdb[0] == INQUIRY))) {
19663a31b7ebSMike Smith 	cc->header.host_tag &= ~CISS_HDR_HOST_TAG_ERROR;
19673a31b7ebSMike Smith 	debug(2, "ignoring irrelevant under/overrun error");
19683a31b7ebSMike Smith     }
19693a31b7ebSMike Smith 
19703a31b7ebSMike Smith     /*
19713a31b7ebSMike Smith      * Check the command's error bit, if clear, there's no status and
19723a31b7ebSMike Smith      * everything is OK.
19733a31b7ebSMike Smith      */
19743a31b7ebSMike Smith     if (!(cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR)) {
19753a31b7ebSMike Smith 	if (scsi_status != NULL)
19763a31b7ebSMike Smith 	    *scsi_status = SCSI_STATUS_OK;
19773a31b7ebSMike Smith 	if (command_status != NULL)
19783a31b7ebSMike Smith 	    *command_status = CISS_CMD_STATUS_SUCCESS;
19793a31b7ebSMike Smith 	return(0);
19803a31b7ebSMike Smith     } else {
19813a31b7ebSMike Smith 	if (command_status != NULL)
19823a31b7ebSMike Smith 	    *command_status = ce->command_status;
19833a31b7ebSMike Smith 	if (scsi_status != NULL) {
19843a31b7ebSMike Smith 	    if (ce->command_status == CISS_CMD_STATUS_TARGET_STATUS) {
19853a31b7ebSMike Smith 		*scsi_status = ce->scsi_status;
19863a31b7ebSMike Smith 	    } else {
19873a31b7ebSMike Smith 		*scsi_status = -1;
19883a31b7ebSMike Smith 	    }
19893a31b7ebSMike Smith 	}
19903a31b7ebSMike Smith 	if (bootverbose)
19913a31b7ebSMike Smith 	    ciss_printf(cr->cr_sc, "command status 0x%x (%s) scsi status 0x%x\n",
19923a31b7ebSMike Smith 			ce->command_status, ciss_name_command_status(ce->command_status),
19933a31b7ebSMike Smith 			ce->scsi_status);
19943a31b7ebSMike Smith 	if (ce->command_status == CISS_CMD_STATUS_INVALID_COMMAND) {
19953a31b7ebSMike Smith 	    ciss_printf(cr->cr_sc, "invalid command, offense size %d at %d, value 0x%x\n",
19963a31b7ebSMike Smith 			ce->additional_error_info.invalid_command.offense_size,
19973a31b7ebSMike Smith 			ce->additional_error_info.invalid_command.offense_offset,
19983a31b7ebSMike Smith 			ce->additional_error_info.invalid_command.offense_value);
19993a31b7ebSMike Smith 	}
20003a31b7ebSMike Smith     }
2001c6131460SPaul Saab #if 0
2002c6131460SPaul Saab     ciss_print_request(cr);
2003c6131460SPaul Saab #endif
20043a31b7ebSMike Smith     return(1);
20053a31b7ebSMike Smith }
20063a31b7ebSMike Smith 
20073a31b7ebSMike Smith /************************************************************************
20083a31b7ebSMike Smith  * Issue a request and don't return until it's completed.
20093a31b7ebSMike Smith  *
20103a31b7ebSMike Smith  * Depending on adapter status, we may poll or sleep waiting for
20113a31b7ebSMike Smith  * completion.
20123a31b7ebSMike Smith  */
20133a31b7ebSMike Smith static int
20143a31b7ebSMike Smith ciss_synch_request(struct ciss_request *cr, int timeout)
20153a31b7ebSMike Smith {
20163a31b7ebSMike Smith     if (cr->cr_sc->ciss_flags & CISS_FLAG_RUNNING) {
20173a31b7ebSMike Smith 	return(ciss_wait_request(cr, timeout));
20183a31b7ebSMike Smith     } else {
20193a31b7ebSMike Smith 	return(ciss_poll_request(cr, timeout));
20203a31b7ebSMike Smith     }
20213a31b7ebSMike Smith }
20223a31b7ebSMike Smith 
20233a31b7ebSMike Smith /************************************************************************
20243a31b7ebSMike Smith  * Issue a request and poll for completion.
20253a31b7ebSMike Smith  *
20263a31b7ebSMike Smith  * Timeout in milliseconds.
20273a31b7ebSMike Smith  */
20283a31b7ebSMike Smith static int
20293a31b7ebSMike Smith ciss_poll_request(struct ciss_request *cr, int timeout)
20303a31b7ebSMike Smith {
20313a31b7ebSMike Smith     int		error;
20323a31b7ebSMike Smith 
20333a31b7ebSMike Smith     debug_called(2);
20343a31b7ebSMike Smith 
20353a31b7ebSMike Smith     cr->cr_flags |= CISS_REQ_POLL;
20363a31b7ebSMike Smith     if ((error = ciss_start(cr)) != 0)
20373a31b7ebSMike Smith 	return(error);
20383a31b7ebSMike Smith 
20393a31b7ebSMike Smith     do {
20403a31b7ebSMike Smith 	ciss_done(cr->cr_sc);
20413a31b7ebSMike Smith 	if (!(cr->cr_flags & CISS_REQ_POLL))
20423a31b7ebSMike Smith 	    return(0);
20433a31b7ebSMike Smith 	DELAY(1000);
20443a31b7ebSMike Smith     } while (timeout-- >= 0);
20453a31b7ebSMike Smith     return(EWOULDBLOCK);
20463a31b7ebSMike Smith }
20473a31b7ebSMike Smith 
20483a31b7ebSMike Smith /************************************************************************
20493a31b7ebSMike Smith  * Issue a request and sleep waiting for completion.
20503a31b7ebSMike Smith  *
20513a31b7ebSMike Smith  * Timeout in milliseconds.  Note that a spurious wakeup will reset
20523a31b7ebSMike Smith  * the timeout.
20533a31b7ebSMike Smith  */
20543a31b7ebSMike Smith static int
20553a31b7ebSMike Smith ciss_wait_request(struct ciss_request *cr, int timeout)
20563a31b7ebSMike Smith {
20573a31b7ebSMike Smith     int		s, error;
20583a31b7ebSMike Smith 
20593a31b7ebSMike Smith     debug_called(2);
20603a31b7ebSMike Smith 
20613a31b7ebSMike Smith     cr->cr_flags |= CISS_REQ_SLEEP;
20623a31b7ebSMike Smith     if ((error = ciss_start(cr)) != 0)
20633a31b7ebSMike Smith 	return(error);
20643a31b7ebSMike Smith 
20653a31b7ebSMike Smith     s = splcam();
206640f05b02SPaul Saab     while ((cr->cr_flags & CISS_REQ_SLEEP) && (error != EWOULDBLOCK)) {
2067975b7318SScott Long 	error = msleep(cr, &cr->cr_sc->ciss_mtx, PRIBIO, "cissREQ", (timeout * hz) / 1000);
20683a31b7ebSMike Smith     }
20693a31b7ebSMike Smith     splx(s);
20703a31b7ebSMike Smith     return(error);
20713a31b7ebSMike Smith }
20723a31b7ebSMike Smith 
20736197ca15SPeter Wemm #if 0
20743a31b7ebSMike Smith /************************************************************************
20753a31b7ebSMike Smith  * Abort a request.  Note that a potential exists here to race the
20763a31b7ebSMike Smith  * request being completed; the caller must deal with this.
20773a31b7ebSMike Smith  */
20783a31b7ebSMike Smith static int
20793a31b7ebSMike Smith ciss_abort_request(struct ciss_request *ar)
20803a31b7ebSMike Smith {
20813a31b7ebSMike Smith     struct ciss_request		*cr;
20823a31b7ebSMike Smith     struct ciss_command		*cc;
20833a31b7ebSMike Smith     struct ciss_message_cdb	*cmc;
20843a31b7ebSMike Smith     int				error;
20853a31b7ebSMike Smith 
20863a31b7ebSMike Smith     debug_called(1);
20873a31b7ebSMike Smith 
20883a31b7ebSMike Smith     /* get a request */
20893a31b7ebSMike Smith     if ((error = ciss_get_request(ar->cr_sc, &cr)) != 0)
20903a31b7ebSMike Smith 	return(error);
20913a31b7ebSMike Smith 
20923a31b7ebSMike Smith     /* build the abort command */
20933a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
20943a31b7ebSMike Smith     cc->header.address.mode.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;	/* addressing? */
20953a31b7ebSMike Smith     cc->header.address.physical.target = 0;
20963a31b7ebSMike Smith     cc->header.address.physical.bus = 0;
20973a31b7ebSMike Smith     cc->cdb.cdb_length = sizeof(*cmc);
20983a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
20993a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
21003a31b7ebSMike Smith     cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
21013a31b7ebSMike Smith     cc->cdb.timeout = 30;
21023a31b7ebSMike Smith 
21033a31b7ebSMike Smith     cmc = (struct ciss_message_cdb *)&(cc->cdb.cdb[0]);
21043a31b7ebSMike Smith     cmc->opcode = CISS_OPCODE_MESSAGE_ABORT;
21053a31b7ebSMike Smith     cmc->type = CISS_MESSAGE_ABORT_TASK;
21063a31b7ebSMike Smith     cmc->abort_tag = ar->cr_tag;	/* endianness?? */
21073a31b7ebSMike Smith 
21083a31b7ebSMike Smith     /*
21093a31b7ebSMike Smith      * Send the request and wait for a response.  If we believe we
21103a31b7ebSMike Smith      * aborted the request OK, clear the flag that indicates it's
21113a31b7ebSMike Smith      * running.
21123a31b7ebSMike Smith      */
21133a31b7ebSMike Smith     error = ciss_synch_request(cr, 35 * 1000);
21143a31b7ebSMike Smith     if (!error)
21153a31b7ebSMike Smith 	error = ciss_report_request(cr, NULL, NULL);
21163a31b7ebSMike Smith     ciss_release_request(cr);
21173a31b7ebSMike Smith 
21183a31b7ebSMike Smith     return(error);
21193a31b7ebSMike Smith }
21206197ca15SPeter Wemm #endif
21213a31b7ebSMike Smith 
21223a31b7ebSMike Smith 
21233a31b7ebSMike Smith /************************************************************************
21243a31b7ebSMike Smith  * Fetch and initialise a request
21253a31b7ebSMike Smith  */
21263a31b7ebSMike Smith static int
21273a31b7ebSMike Smith ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp)
21283a31b7ebSMike Smith {
21293a31b7ebSMike Smith     struct ciss_request *cr;
21303a31b7ebSMike Smith 
21313a31b7ebSMike Smith     debug_called(2);
21323a31b7ebSMike Smith 
21333a31b7ebSMike Smith     /*
21343a31b7ebSMike Smith      * Get a request and clean it up.
21353a31b7ebSMike Smith      */
21363a31b7ebSMike Smith     if ((cr = ciss_dequeue_free(sc)) == NULL)
21373a31b7ebSMike Smith 	return(ENOMEM);
21383a31b7ebSMike Smith 
21393a31b7ebSMike Smith     cr->cr_data = NULL;
21403a31b7ebSMike Smith     cr->cr_flags = 0;
21413a31b7ebSMike Smith     cr->cr_complete = NULL;
2142639717c8SPaul Saab     cr->cr_private = NULL;
21433a31b7ebSMike Smith 
21443a31b7ebSMike Smith     ciss_preen_command(cr);
21453a31b7ebSMike Smith     *crp = cr;
21463a31b7ebSMike Smith     return(0);
21473a31b7ebSMike Smith }
21483a31b7ebSMike Smith 
21493a31b7ebSMike Smith static void
21503a31b7ebSMike Smith ciss_preen_command(struct ciss_request *cr)
21513a31b7ebSMike Smith {
21523a31b7ebSMike Smith     struct ciss_command	*cc;
21533a31b7ebSMike Smith     u_int32_t		cmdphys;
21543a31b7ebSMike Smith 
21553a31b7ebSMike Smith     /*
21563a31b7ebSMike Smith      * Clean up the command structure.
21573a31b7ebSMike Smith      *
21583a31b7ebSMike Smith      * Note that we set up the error_info structure here, since the
21593a31b7ebSMike Smith      * length can be overwritten by any command.
21603a31b7ebSMike Smith      */
21613a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
21623a31b7ebSMike Smith     cc->header.sg_in_list = 0;		/* kinda inefficient this way */
21633a31b7ebSMike Smith     cc->header.sg_total = 0;
21643a31b7ebSMike Smith     cc->header.host_tag = cr->cr_tag << 2;
21653a31b7ebSMike Smith     cc->header.host_tag_zeroes = 0;
21663a31b7ebSMike Smith     cmdphys = CISS_FIND_COMMANDPHYS(cr);
21673a31b7ebSMike Smith     cc->error_info.error_info_address = cmdphys + sizeof(struct ciss_command);
21683a31b7ebSMike Smith     cc->error_info.error_info_length = CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command);
21693a31b7ebSMike Smith }
21703a31b7ebSMike Smith 
21713a31b7ebSMike Smith /************************************************************************
21723a31b7ebSMike Smith  * Release a request to the free list.
21733a31b7ebSMike Smith  */
21743a31b7ebSMike Smith static void
21753a31b7ebSMike Smith ciss_release_request(struct ciss_request *cr)
21763a31b7ebSMike Smith {
21773a31b7ebSMike Smith     struct ciss_softc	*sc;
21783a31b7ebSMike Smith 
21793a31b7ebSMike Smith     debug_called(2);
21803a31b7ebSMike Smith 
21813a31b7ebSMike Smith     sc = cr->cr_sc;
21823a31b7ebSMike Smith 
21833a31b7ebSMike Smith     /* release the request to the free queue */
21843a31b7ebSMike Smith     ciss_requeue_free(cr);
21853a31b7ebSMike Smith }
21863a31b7ebSMike Smith 
21873a31b7ebSMike Smith /************************************************************************
21883a31b7ebSMike Smith  * Allocate a request that will be used to send a BMIC command.  Do some
21893a31b7ebSMike Smith  * of the common setup here to avoid duplicating it everywhere else.
21903a31b7ebSMike Smith  */
21913a31b7ebSMike Smith static int
21923a31b7ebSMike Smith ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
21933a31b7ebSMike Smith 		      int opcode, void **bufp, size_t bufsize)
21943a31b7ebSMike Smith {
21953a31b7ebSMike Smith     struct ciss_request		*cr;
21963a31b7ebSMike Smith     struct ciss_command		*cc;
21973a31b7ebSMike Smith     struct ciss_bmic_cdb	*cbc;
21983a31b7ebSMike Smith     void			*buf;
21993a31b7ebSMike Smith     int				error;
22003a31b7ebSMike Smith     int				dataout;
22013a31b7ebSMike Smith 
22023a31b7ebSMike Smith     debug_called(2);
22033a31b7ebSMike Smith 
22043a31b7ebSMike Smith     cr = NULL;
22053a31b7ebSMike Smith     buf = NULL;
22063a31b7ebSMike Smith 
22073a31b7ebSMike Smith     /*
22083a31b7ebSMike Smith      * Get a request.
22093a31b7ebSMike Smith      */
22103a31b7ebSMike Smith     if ((error = ciss_get_request(sc, &cr)) != 0)
22113a31b7ebSMike Smith 	goto out;
22123a31b7ebSMike Smith 
22133a31b7ebSMike Smith     /*
22143a31b7ebSMike Smith      * Allocate data storage if requested, determine the data direction.
22153a31b7ebSMike Smith      */
22163a31b7ebSMike Smith     dataout = 0;
22173a31b7ebSMike Smith     if ((bufsize > 0) && (bufp != NULL)) {
22183a31b7ebSMike Smith 	if (*bufp == NULL) {
22193a31b7ebSMike Smith 	    if ((buf = malloc(bufsize, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
22203a31b7ebSMike Smith 		error = ENOMEM;
22213a31b7ebSMike Smith 		goto out;
22223a31b7ebSMike Smith 	    }
22233a31b7ebSMike Smith 	} else {
22243a31b7ebSMike Smith 	    buf = *bufp;
22253a31b7ebSMike Smith 	    dataout = 1;	/* we are given a buffer, so we are writing */
22263a31b7ebSMike Smith 	}
22273a31b7ebSMike Smith     }
22283a31b7ebSMike Smith 
22293a31b7ebSMike Smith     /*
22303a31b7ebSMike Smith      * Build a CISS BMIC command to get the logical drive ID.
22313a31b7ebSMike Smith      */
22323a31b7ebSMike Smith     cr->cr_data = buf;
22333a31b7ebSMike Smith     cr->cr_length = bufsize;
22343a31b7ebSMike Smith     if (!dataout)
22353a31b7ebSMike Smith 	cr->cr_flags = CISS_REQ_DATAIN;
22363a31b7ebSMike Smith 
22373a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
22383a31b7ebSMike Smith     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
22393a31b7ebSMike Smith     cc->header.address.physical.bus = 0;
22403a31b7ebSMike Smith     cc->header.address.physical.target = 0;
22413a31b7ebSMike Smith     cc->cdb.cdb_length = sizeof(*cbc);
22423a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
22433a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
22443a31b7ebSMike Smith     cc->cdb.direction = dataout ? CISS_CDB_DIRECTION_WRITE : CISS_CDB_DIRECTION_READ;
22453a31b7ebSMike Smith     cc->cdb.timeout = 0;
22463a31b7ebSMike Smith 
22473a31b7ebSMike Smith     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
22483a31b7ebSMike Smith     bzero(cbc, sizeof(*cbc));
22493a31b7ebSMike Smith     cbc->opcode = dataout ? CISS_ARRAY_CONTROLLER_WRITE : CISS_ARRAY_CONTROLLER_READ;
22503a31b7ebSMike Smith     cbc->bmic_opcode = opcode;
22513a31b7ebSMike Smith     cbc->size = htons((u_int16_t)bufsize);
22523a31b7ebSMike Smith 
22533a31b7ebSMike Smith out:
22543a31b7ebSMike Smith     if (error) {
22553a31b7ebSMike Smith 	if (cr != NULL)
22563a31b7ebSMike Smith 	    ciss_release_request(cr);
22573a31b7ebSMike Smith     } else {
22583a31b7ebSMike Smith 	*crp = cr;
22593a31b7ebSMike Smith 	if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL))
22603a31b7ebSMike Smith 	    *bufp = buf;
22613a31b7ebSMike Smith     }
22623a31b7ebSMike Smith     return(error);
22633a31b7ebSMike Smith }
22643a31b7ebSMike Smith 
22653a31b7ebSMike Smith /************************************************************************
22663a31b7ebSMike Smith  * Handle a command passed in from userspace.
22673a31b7ebSMike Smith  */
22683a31b7ebSMike Smith static int
22693a31b7ebSMike Smith ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc)
22703a31b7ebSMike Smith {
22713a31b7ebSMike Smith     struct ciss_request		*cr;
22723a31b7ebSMike Smith     struct ciss_command		*cc;
22733a31b7ebSMike Smith     struct ciss_error_info	*ce;
22743c63a8b4SPaul Saab     int				error = 0;
22753a31b7ebSMike Smith 
22763a31b7ebSMike Smith     debug_called(1);
22773a31b7ebSMike Smith 
22783a31b7ebSMike Smith     cr = NULL;
22793a31b7ebSMike Smith 
22803a31b7ebSMike Smith     /*
22813a31b7ebSMike Smith      * Get a request.
22823a31b7ebSMike Smith      */
2283f83695f5SPaul Saab     while (ciss_get_request(sc, &cr) != 0)
2284975b7318SScott Long 	msleep(sc, &sc->ciss_mtx, PPAUSE, "cissREQ", hz);
22853a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
22863a31b7ebSMike Smith 
22873a31b7ebSMike Smith     /*
22883a31b7ebSMike Smith      * Allocate an in-kernel databuffer if required, copy in user data.
22893a31b7ebSMike Smith      */
22903a31b7ebSMike Smith     cr->cr_length = ioc->buf_size;
22913a31b7ebSMike Smith     if (ioc->buf_size > 0) {
2292975b7318SScott Long 	if ((cr->cr_data = malloc(ioc->buf_size, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
22933a31b7ebSMike Smith 	    error = ENOMEM;
22943a31b7ebSMike Smith 	    goto out;
22953a31b7ebSMike Smith 	}
22963a31b7ebSMike Smith 	if ((error = copyin(ioc->buf, cr->cr_data, ioc->buf_size))) {
22973a31b7ebSMike Smith 	    debug(0, "copyin: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
22983a31b7ebSMike Smith 	    goto out;
22993a31b7ebSMike Smith 	}
23003a31b7ebSMike Smith     }
23013a31b7ebSMike Smith 
23023a31b7ebSMike Smith     /*
23033a31b7ebSMike Smith      * Build the request based on the user command.
23043a31b7ebSMike Smith      */
23053a31b7ebSMike Smith     bcopy(&ioc->LUN_info, &cc->header.address, sizeof(cc->header.address));
23063a31b7ebSMike Smith     bcopy(&ioc->Request, &cc->cdb, sizeof(cc->cdb));
23073a31b7ebSMike Smith 
23083a31b7ebSMike Smith     /* XXX anything else to populate here? */
23093a31b7ebSMike Smith 
23103a31b7ebSMike Smith     /*
23113a31b7ebSMike Smith      * Run the command.
23123a31b7ebSMike Smith      */
23133a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000))) {
23143a31b7ebSMike Smith 	debug(0, "request failed - %d", error);
23153a31b7ebSMike Smith 	goto out;
23163a31b7ebSMike Smith     }
23173a31b7ebSMike Smith 
23183a31b7ebSMike Smith     /*
23193c63a8b4SPaul Saab      * Check to see if the command succeeded.
23203a31b7ebSMike Smith      */
23213a31b7ebSMike Smith     ce = (struct ciss_error_info *)&(cc->sg[0]);
23223c6b8353SPaul Saab     if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) == 0)
23233c63a8b4SPaul Saab 	bzero(ce, sizeof(*ce));
23243c63a8b4SPaul Saab 
23253c63a8b4SPaul Saab     /*
23263c63a8b4SPaul Saab      * Copy the results back to the user.
23273c63a8b4SPaul Saab      */
23283a31b7ebSMike Smith     bcopy(ce, &ioc->error_info, sizeof(*ce));
23293a31b7ebSMike Smith     if ((ioc->buf_size > 0) &&
23303a31b7ebSMike Smith 	(error = copyout(cr->cr_data, ioc->buf, ioc->buf_size))) {
23313a31b7ebSMike Smith 	debug(0, "copyout: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
23323a31b7ebSMike Smith 	goto out;
23333a31b7ebSMike Smith     }
23343a31b7ebSMike Smith 
23353a31b7ebSMike Smith     /* done OK */
23363a31b7ebSMike Smith     error = 0;
23373a31b7ebSMike Smith 
23383a31b7ebSMike Smith out:
23393a31b7ebSMike Smith     if ((cr != NULL) && (cr->cr_data != NULL))
23403a31b7ebSMike Smith 	free(cr->cr_data, CISS_MALLOC_CLASS);
23413a31b7ebSMike Smith     if (cr != NULL)
23423a31b7ebSMike Smith 	ciss_release_request(cr);
23433a31b7ebSMike Smith     return(error);
23443a31b7ebSMike Smith }
23453a31b7ebSMike Smith 
23463a31b7ebSMike Smith /************************************************************************
23473a31b7ebSMike Smith  * Map a request into bus-visible space, initialise the scatter/gather
23483a31b7ebSMike Smith  * list.
23493a31b7ebSMike Smith  */
23503a31b7ebSMike Smith static int
23513a31b7ebSMike Smith ciss_map_request(struct ciss_request *cr)
23523a31b7ebSMike Smith {
23533a31b7ebSMike Smith     struct ciss_softc	*sc;
2354639717c8SPaul Saab     int			error = 0;
23553a31b7ebSMike Smith 
23563a31b7ebSMike Smith     debug_called(2);
23573a31b7ebSMike Smith 
23583a31b7ebSMike Smith     sc = cr->cr_sc;
23593a31b7ebSMike Smith 
23603a31b7ebSMike Smith     /* check that mapping is necessary */
2361639717c8SPaul Saab     if (cr->cr_flags & CISS_REQ_MAPPED)
23623a31b7ebSMike Smith 	return(0);
23633a31b7ebSMike Smith 
23643a31b7ebSMike Smith     cr->cr_flags |= CISS_REQ_MAPPED;
2365639717c8SPaul Saab 
2366639717c8SPaul Saab     bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2367639717c8SPaul Saab 		    BUS_DMASYNC_PREWRITE);
2368639717c8SPaul Saab 
2369639717c8SPaul Saab     if (cr->cr_data != NULL) {
2370639717c8SPaul Saab 	error = bus_dmamap_load(sc->ciss_buffer_dmat, cr->cr_datamap,
2371639717c8SPaul Saab 				cr->cr_data, cr->cr_length,
2372639717c8SPaul Saab 				ciss_request_map_helper, cr, 0);
2373639717c8SPaul Saab 	if (error != 0)
2374639717c8SPaul Saab 	    return (error);
2375639717c8SPaul Saab     } else {
2376639717c8SPaul Saab 	/*
2377639717c8SPaul Saab 	 * Post the command to the adapter.
2378639717c8SPaul Saab 	 */
2379639717c8SPaul Saab 	ciss_enqueue_busy(cr);
2380639717c8SPaul Saab 	CISS_TL_SIMPLE_POST_CMD(cr->cr_sc, CISS_FIND_COMMANDPHYS(cr));
2381639717c8SPaul Saab     }
2382639717c8SPaul Saab 
23833a31b7ebSMike Smith     return(0);
23843a31b7ebSMike Smith }
23853a31b7ebSMike Smith 
23863a31b7ebSMike Smith static void
23873a31b7ebSMike Smith ciss_request_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
23883a31b7ebSMike Smith {
23893a31b7ebSMike Smith     struct ciss_command	*cc;
2390639717c8SPaul Saab     struct ciss_request *cr;
2391639717c8SPaul Saab     struct ciss_softc	*sc;
23923a31b7ebSMike Smith     int			i;
23933a31b7ebSMike Smith 
23943a31b7ebSMike Smith     debug_called(2);
23953a31b7ebSMike Smith 
2396639717c8SPaul Saab     cr = (struct ciss_request *)arg;
2397639717c8SPaul Saab     sc = cr->cr_sc;
2398639717c8SPaul Saab     cc = CISS_FIND_COMMAND(cr);
2399639717c8SPaul Saab 
24003a31b7ebSMike Smith     for (i = 0; i < nseg; i++) {
24013a31b7ebSMike Smith 	cc->sg[i].address = segs[i].ds_addr;
24023a31b7ebSMike Smith 	cc->sg[i].length = segs[i].ds_len;
24033a31b7ebSMike Smith 	cc->sg[i].extension = 0;
24043a31b7ebSMike Smith     }
24053a31b7ebSMike Smith     /* we leave the s/g table entirely within the command */
24063a31b7ebSMike Smith     cc->header.sg_in_list = nseg;
24073a31b7ebSMike Smith     cc->header.sg_total = nseg;
2408639717c8SPaul Saab 
2409639717c8SPaul Saab     if (cr->cr_flags & CISS_REQ_DATAIN)
2410639717c8SPaul Saab 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREREAD);
2411639717c8SPaul Saab     if (cr->cr_flags & CISS_REQ_DATAOUT)
2412639717c8SPaul Saab 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREWRITE);
2413639717c8SPaul Saab 
2414639717c8SPaul Saab     /*
2415639717c8SPaul Saab      * Post the command to the adapter.
2416639717c8SPaul Saab      */
2417639717c8SPaul Saab     ciss_enqueue_busy(cr);
2418639717c8SPaul Saab     CISS_TL_SIMPLE_POST_CMD(cr->cr_sc, CISS_FIND_COMMANDPHYS(cr));
24193a31b7ebSMike Smith }
24203a31b7ebSMike Smith 
24213a31b7ebSMike Smith /************************************************************************
24223a31b7ebSMike Smith  * Unmap a request from bus-visible space.
24233a31b7ebSMike Smith  */
24243a31b7ebSMike Smith static void
24253a31b7ebSMike Smith ciss_unmap_request(struct ciss_request *cr)
24263a31b7ebSMike Smith {
24273a31b7ebSMike Smith     struct ciss_softc	*sc;
24283a31b7ebSMike Smith 
24293a31b7ebSMike Smith     debug_called(2);
24303a31b7ebSMike Smith 
24313a31b7ebSMike Smith     sc = cr->cr_sc;
24323a31b7ebSMike Smith 
24333a31b7ebSMike Smith     /* check that unmapping is necessary */
2434639717c8SPaul Saab     if ((cr->cr_flags & CISS_REQ_MAPPED) == 0)
24353a31b7ebSMike Smith 	return;
24363a31b7ebSMike Smith 
2437639717c8SPaul Saab     bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2438639717c8SPaul Saab 		    BUS_DMASYNC_POSTWRITE);
2439639717c8SPaul Saab 
2440639717c8SPaul Saab     if (cr->cr_data == NULL)
2441639717c8SPaul Saab 	goto out;
2442639717c8SPaul Saab 
24433a31b7ebSMike Smith     if (cr->cr_flags & CISS_REQ_DATAIN)
24443a31b7ebSMike Smith 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTREAD);
24453a31b7ebSMike Smith     if (cr->cr_flags & CISS_REQ_DATAOUT)
24463a31b7ebSMike Smith 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTWRITE);
24473a31b7ebSMike Smith 
24483a31b7ebSMike Smith     bus_dmamap_unload(sc->ciss_buffer_dmat, cr->cr_datamap);
2449639717c8SPaul Saab out:
24503a31b7ebSMike Smith     cr->cr_flags &= ~CISS_REQ_MAPPED;
24513a31b7ebSMike Smith }
24523a31b7ebSMike Smith 
24533a31b7ebSMike Smith /************************************************************************
24543a31b7ebSMike Smith  * Attach the driver to CAM.
24553a31b7ebSMike Smith  *
24563a31b7ebSMike Smith  * We put all the logical drives on a single SCSI bus.
24573a31b7ebSMike Smith  */
24583a31b7ebSMike Smith static int
24593a31b7ebSMike Smith ciss_cam_init(struct ciss_softc *sc)
24603a31b7ebSMike Smith {
24611fe6c4eeSScott Long     int			i, maxbus;
24623a31b7ebSMike Smith 
24633a31b7ebSMike Smith     debug_called(1);
24643a31b7ebSMike Smith 
24653a31b7ebSMike Smith     /*
24663a31b7ebSMike Smith      * Allocate a devq.  We can reuse this for the masked physical
24673a31b7ebSMike Smith      * devices if we decide to export these as well.
24683a31b7ebSMike Smith      */
24693a31b7ebSMike Smith     if ((sc->ciss_cam_devq = cam_simq_alloc(sc->ciss_max_requests)) == NULL) {
24703a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate CAM SIM queue\n");
24713a31b7ebSMike Smith 	return(ENOMEM);
24723a31b7ebSMike Smith     }
24733a31b7ebSMike Smith 
24743a31b7ebSMike Smith     /*
24753a31b7ebSMike Smith      * Create a SIM.
24761fe6c4eeSScott Long      *
24771fe6c4eeSScott Long      * This naturally wastes a bit of memory.  The alternative is to allocate
24781fe6c4eeSScott Long      * and register each bus as it is found, and then track them on a linked
24791fe6c4eeSScott Long      * list.  Unfortunately, the driver has a few places where it needs to
24801fe6c4eeSScott Long      * look up the SIM based solely on bus number, and it's unclear whether
24811fe6c4eeSScott Long      * a list traversal would work for these situations.
24823a31b7ebSMike Smith      */
24831fe6c4eeSScott Long     maxbus = max(sc->ciss_max_logical_bus, sc->ciss_max_physical_bus +
24841fe6c4eeSScott Long 		 CISS_PHYSICAL_BASE);
24851fe6c4eeSScott Long     sc->ciss_cam_sim = malloc(maxbus * sizeof(struct cam_sim*),
2486c6131460SPaul Saab 			      CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
2487c6131460SPaul Saab     if (sc->ciss_cam_sim == NULL) {
2488c6131460SPaul Saab 	ciss_printf(sc, "can't allocate memory for controller SIM\n");
2489c6131460SPaul Saab 	return(ENOMEM);
2490c6131460SPaul Saab     }
2491c6131460SPaul Saab 
24921fe6c4eeSScott Long     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
2493c6131460SPaul Saab 	if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
2494c6131460SPaul Saab 						 "ciss", sc,
24952aa6b972SPaul Saab 						 device_get_unit(sc->ciss_dev),
249645650f52SScott Long 						 &sc->ciss_mtx,
249745650f52SScott Long 						 sc->ciss_max_requests - 2,
2498d4aa427fSPaul Saab 						 sc->ciss_max_requests - 2,
24993a31b7ebSMike Smith 						 sc->ciss_cam_devq)) == NULL) {
2500c6131460SPaul Saab 	    ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
25013a31b7ebSMike Smith 	    return(ENOMEM);
25023a31b7ebSMike Smith 	}
25033a31b7ebSMike Smith 
25043a31b7ebSMike Smith 	/*
2505c6131460SPaul Saab 	 * Register bus with this SIM.
25063a31b7ebSMike Smith 	 */
2507975b7318SScott Long 	mtx_lock(&sc->ciss_mtx);
25081fe6c4eeSScott Long 	if (i == 0 || sc->ciss_controllers[i].physical.bus != 0) {
2509b50569b7SScott Long 	    if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
25101fe6c4eeSScott Long 		ciss_printf(sc, "can't register SCSI bus %d\n", i);
2511975b7318SScott Long 		mtx_unlock(&sc->ciss_mtx);
25121fe6c4eeSScott Long 		return (ENXIO);
25131fe6c4eeSScott Long 	    }
25141fe6c4eeSScott Long 	}
2515975b7318SScott Long 	mtx_unlock(&sc->ciss_mtx);
25161fe6c4eeSScott Long     }
25171fe6c4eeSScott Long 
25181fe6c4eeSScott Long     for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
25191fe6c4eeSScott Long 	 CISS_PHYSICAL_BASE; i++) {
25201fe6c4eeSScott Long 	if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
25211fe6c4eeSScott Long 						 "ciss", sc,
25221fe6c4eeSScott Long 						 device_get_unit(sc->ciss_dev),
2523975b7318SScott Long 						 &sc->ciss_mtx, 1,
25241fe6c4eeSScott Long 						 sc->ciss_max_requests - 2,
25251fe6c4eeSScott Long 						 sc->ciss_cam_devq)) == NULL) {
25261fe6c4eeSScott Long 	    ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
25271fe6c4eeSScott Long 	    return (ENOMEM);
25281fe6c4eeSScott Long 	}
25291fe6c4eeSScott Long 
2530975b7318SScott Long 	mtx_lock(&sc->ciss_mtx);
2531b50569b7SScott Long 	if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
2532c6131460SPaul Saab 	    ciss_printf(sc, "can't register SCSI bus %d\n", i);
2533975b7318SScott Long 	    mtx_unlock(&sc->ciss_mtx);
25343a31b7ebSMike Smith 	    return (ENXIO);
25353a31b7ebSMike Smith 	}
2536975b7318SScott Long 	mtx_unlock(&sc->ciss_mtx);
2537c6131460SPaul Saab     }
25383a31b7ebSMike Smith 
25393a31b7ebSMike Smith     /*
25403a31b7ebSMike Smith      * Initiate a rescan of the bus.
25413a31b7ebSMike Smith      */
2542975b7318SScott Long     mtx_lock(&sc->ciss_mtx);
25433a31b7ebSMike Smith     ciss_cam_rescan_all(sc);
2544975b7318SScott Long     mtx_unlock(&sc->ciss_mtx);
25453a31b7ebSMike Smith 
25463a31b7ebSMike Smith     return(0);
25473a31b7ebSMike Smith }
25483a31b7ebSMike Smith 
25493a31b7ebSMike Smith /************************************************************************
25503a31b7ebSMike Smith  * Initiate a rescan of the 'logical devices' SIM
25513a31b7ebSMike Smith  */
25523a31b7ebSMike Smith static void
2553c6131460SPaul Saab ciss_cam_rescan_target(struct ciss_softc *sc, int bus, int target)
25543a31b7ebSMike Smith {
2555c6131460SPaul Saab     struct cam_path	*path;
25563a31b7ebSMike Smith     union ccb		*ccb;
25573a31b7ebSMike Smith 
25583a31b7ebSMike Smith     debug_called(1);
25593a31b7ebSMike Smith 
2560e62c19b2SScott Long     if ((ccb = malloc(sizeof(union ccb), CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
25613a31b7ebSMike Smith 	ciss_printf(sc, "rescan failed (can't allocate CCB)\n");
25623a31b7ebSMike Smith 	return;
25633a31b7ebSMike Smith     }
25643a31b7ebSMike Smith 
2565c6131460SPaul Saab     if (xpt_create_path(&path, xpt_periph, cam_sim_path(sc->ciss_cam_sim[bus]),
2566c6131460SPaul Saab 			target, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
25673a31b7ebSMike Smith 	ciss_printf(sc, "rescan failed (can't create path)\n");
2568e62c19b2SScott Long 	free(ccb, CISS_MALLOC_CLASS);
25693a31b7ebSMike Smith 	return;
25703a31b7ebSMike Smith     }
25713a31b7ebSMike Smith 
2572c6131460SPaul Saab     xpt_setup_ccb(&ccb->ccb_h, path, 5/*priority (low)*/);
25733a31b7ebSMike Smith     ccb->ccb_h.func_code = XPT_SCAN_BUS;
25743a31b7ebSMike Smith     ccb->ccb_h.cbfcnp = ciss_cam_rescan_callback;
25753a31b7ebSMike Smith     ccb->crcn.flags = CAM_FLAG_NONE;
25763a31b7ebSMike Smith     xpt_action(ccb);
25773a31b7ebSMike Smith 
25783a31b7ebSMike Smith     /* scan is now in progress */
25793a31b7ebSMike Smith }
25803a31b7ebSMike Smith 
25813a31b7ebSMike Smith static void
25823a31b7ebSMike Smith ciss_cam_rescan_all(struct ciss_softc *sc)
25833a31b7ebSMike Smith {
2584c6131460SPaul Saab     int i;
2585c6131460SPaul Saab 
25861fe6c4eeSScott Long     /* Rescan the logical buses */
25871fe6c4eeSScott Long     for (i = 0; i < sc->ciss_max_logical_bus; i++)
25881fe6c4eeSScott Long 	ciss_cam_rescan_target(sc, i, CAM_TARGET_WILDCARD);
25891fe6c4eeSScott Long     /* Rescan the physical buses */
259067688644SPaul Saab     for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
25911fe6c4eeSScott Long 	 CISS_PHYSICAL_BASE; i++)
2592c6131460SPaul Saab 	ciss_cam_rescan_target(sc, i, CAM_TARGET_WILDCARD);
25933a31b7ebSMike Smith }
25943a31b7ebSMike Smith 
25953a31b7ebSMike Smith static void
25963a31b7ebSMike Smith ciss_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
25973a31b7ebSMike Smith {
2598e010b6bcSPaul Saab     xpt_free_path(ccb->ccb_h.path);
2599e62c19b2SScott Long     free(ccb, CISS_MALLOC_CLASS);
26003a31b7ebSMike Smith }
26013a31b7ebSMike Smith 
26023a31b7ebSMike Smith /************************************************************************
26033a31b7ebSMike Smith  * Handle requests coming from CAM
26043a31b7ebSMike Smith  */
26053a31b7ebSMike Smith static void
26063a31b7ebSMike Smith ciss_cam_action(struct cam_sim *sim, union ccb *ccb)
26073a31b7ebSMike Smith {
2608440baa08SPaul Saab     struct ciss_softc	*sc;
2609440baa08SPaul Saab     struct ccb_scsiio	*csio;
2610c6131460SPaul Saab     int			bus, target;
26111fe6c4eeSScott Long     int			physical;
2612440baa08SPaul Saab 
2613440baa08SPaul Saab     sc = cam_sim_softc(sim);
2614c6131460SPaul Saab     bus = cam_sim_bus(sim);
2615440baa08SPaul Saab     csio = (struct ccb_scsiio *)&ccb->csio;
2616440baa08SPaul Saab     target = csio->ccb_h.target_id;
26171fe6c4eeSScott Long     physical = CISS_IS_PHYSICAL(bus);
2618440baa08SPaul Saab 
26193a31b7ebSMike Smith     switch (ccb->ccb_h.func_code) {
26203a31b7ebSMike Smith 
26213a31b7ebSMike Smith 	/* perform SCSI I/O */
26223a31b7ebSMike Smith     case XPT_SCSI_IO:
2623440baa08SPaul Saab 	if (!ciss_cam_action_io(sim, csio))
26243a31b7ebSMike Smith 	    return;
26253a31b7ebSMike Smith 	break;
26263a31b7ebSMike Smith 
26273a31b7ebSMike Smith 	/* perform geometry calculations */
26283a31b7ebSMike Smith     case XPT_CALC_GEOMETRY:
26293a31b7ebSMike Smith     {
26303a31b7ebSMike Smith 	struct ccb_calc_geometry	*ccg = &ccb->ccg;
26311fe6c4eeSScott Long 	struct ciss_ldrive		*ld;
26323a31b7ebSMike Smith 
26333a31b7ebSMike Smith 	debug(1, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
26343a31b7ebSMike Smith 
26351fe6c4eeSScott Long 	ld = NULL;
26361fe6c4eeSScott Long 	if (!physical)
26371fe6c4eeSScott Long 	    ld = &sc->ciss_logical[bus][target];
26381fe6c4eeSScott Long 
26393a31b7ebSMike Smith 	/*
2640440baa08SPaul Saab 	 * Use the cached geometry settings unless the fault tolerance
2641440baa08SPaul Saab 	 * is invalid.
26423a31b7ebSMike Smith 	 */
26431fe6c4eeSScott Long 	if (physical || ld->cl_geometry.fault_tolerance == 0xFF) {
2644440baa08SPaul Saab 	    u_int32_t			secs_per_cylinder;
2645440baa08SPaul Saab 
26463a31b7ebSMike Smith 	    ccg->heads = 255;
26473a31b7ebSMike Smith 	    ccg->secs_per_track = 32;
26483a31b7ebSMike Smith 	    secs_per_cylinder = ccg->heads * ccg->secs_per_track;
26493a31b7ebSMike Smith 	    ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2650440baa08SPaul Saab 	} else {
2651440baa08SPaul Saab 	    ccg->heads = ld->cl_geometry.heads;
2652440baa08SPaul Saab 	    ccg->secs_per_track = ld->cl_geometry.sectors;
2653440baa08SPaul Saab 	    ccg->cylinders = ntohs(ld->cl_geometry.cylinders);
2654440baa08SPaul Saab 	}
26553a31b7ebSMike Smith 	ccb->ccb_h.status = CAM_REQ_CMP;
26563a31b7ebSMike Smith         break;
26573a31b7ebSMike Smith     }
26583a31b7ebSMike Smith 
26593a31b7ebSMike Smith 	/* handle path attribute inquiry */
26603a31b7ebSMike Smith     case XPT_PATH_INQ:
26613a31b7ebSMike Smith     {
26623a31b7ebSMike Smith 	struct ccb_pathinq	*cpi = &ccb->cpi;
26633a31b7ebSMike Smith 
26643a31b7ebSMike Smith 	debug(1, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
26653a31b7ebSMike Smith 
26663a31b7ebSMike Smith 	cpi->version_num = 1;
26673a31b7ebSMike Smith 	cpi->hba_inquiry = PI_TAG_ABLE;	/* XXX is this correct? */
26683a31b7ebSMike Smith 	cpi->target_sprt = 0;
26693a31b7ebSMike Smith 	cpi->hba_misc = 0;
26703a31b7ebSMike Smith 	cpi->max_target = CISS_MAX_LOGICAL;
26713a31b7ebSMike Smith 	cpi->max_lun = 0;		/* 'logical drive' channel only */
26723a31b7ebSMike Smith 	cpi->initiator_id = CISS_MAX_LOGICAL;
26733a31b7ebSMike Smith 	strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
26743a31b7ebSMike Smith         strncpy(cpi->hba_vid, "msmith@freebsd.org", HBA_IDLEN);
26753a31b7ebSMike Smith         strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
26763a31b7ebSMike Smith         cpi->unit_number = cam_sim_unit(sim);
26773a31b7ebSMike Smith         cpi->bus_id = cam_sim_bus(sim);
26783a31b7ebSMike Smith 	cpi->base_transfer_speed = 132 * 1024;	/* XXX what to set this to? */
2679fa9ed865SMatt Jacob 	cpi->transport = XPORT_SPI;
2680fa9ed865SMatt Jacob 	cpi->transport_version = 2;
2681fa9ed865SMatt Jacob 	cpi->protocol = PROTO_SCSI;
2682fa9ed865SMatt Jacob 	cpi->protocol_version = SCSI_REV_2;
26833a31b7ebSMike Smith 	ccb->ccb_h.status = CAM_REQ_CMP;
26843a31b7ebSMike Smith 	break;
26853a31b7ebSMike Smith     }
26863a31b7ebSMike Smith 
26873a31b7ebSMike Smith     case XPT_GET_TRAN_SETTINGS:
26883a31b7ebSMike Smith     {
26893a31b7ebSMike Smith 	struct ccb_trans_settings	*cts = &ccb->cts;
26903a31b7ebSMike Smith 	int				bus, target;
2691fa9ed865SMatt Jacob 	struct ccb_trans_settings_spi *spi =
2692fa9ed865SMatt Jacob 	    &cts->xport_specific.spi;
26933a31b7ebSMike Smith 
26943a31b7ebSMike Smith 	bus = cam_sim_bus(sim);
26953a31b7ebSMike Smith 	target = cts->ccb_h.target_id;
26963a31b7ebSMike Smith 
26973a31b7ebSMike Smith 	debug(1, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target);
26983a31b7ebSMike Smith 	/* disconnect always OK */
2699fa9ed865SMatt Jacob 	cts->protocol = PROTO_SCSI;
2700fa9ed865SMatt Jacob 	cts->protocol_version = SCSI_REV_2;
2701fa9ed865SMatt Jacob 	cts->transport = XPORT_SPI;
2702fa9ed865SMatt Jacob 	cts->transport_version = 2;
2703fa9ed865SMatt Jacob 
2704fa9ed865SMatt Jacob 	spi->valid = CTS_SPI_VALID_DISC;
2705fa9ed865SMatt Jacob 	spi->flags = CTS_SPI_FLAGS_DISC_ENB;
27063a31b7ebSMike Smith 
27073a31b7ebSMike Smith 	cts->ccb_h.status = CAM_REQ_CMP;
27083a31b7ebSMike Smith 	break;
27093a31b7ebSMike Smith     }
27103a31b7ebSMike Smith 
27113a31b7ebSMike Smith     default:		/* we can't do this */
27123a31b7ebSMike Smith 	debug(1, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
27133a31b7ebSMike Smith 	ccb->ccb_h.status = CAM_REQ_INVALID;
27143a31b7ebSMike Smith 	break;
27153a31b7ebSMike Smith     }
27163a31b7ebSMike Smith 
27173a31b7ebSMike Smith     xpt_done(ccb);
27183a31b7ebSMike Smith }
27193a31b7ebSMike Smith 
27203a31b7ebSMike Smith /************************************************************************
27213a31b7ebSMike Smith  * Handle a CAM SCSI I/O request.
27223a31b7ebSMike Smith  */
27233a31b7ebSMike Smith static int
27243a31b7ebSMike Smith ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio)
27253a31b7ebSMike Smith {
27263a31b7ebSMike Smith     struct ciss_softc	*sc;
27273a31b7ebSMike Smith     int			bus, target;
27283a31b7ebSMike Smith     struct ciss_request	*cr;
27293a31b7ebSMike Smith     struct ciss_command	*cc;
27303a31b7ebSMike Smith     int			error;
27313a31b7ebSMike Smith 
27323a31b7ebSMike Smith     sc = cam_sim_softc(sim);
27333a31b7ebSMike Smith     bus = cam_sim_bus(sim);
27343a31b7ebSMike Smith     target = csio->ccb_h.target_id;
27353a31b7ebSMike Smith 
27363a31b7ebSMike Smith     debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, csio->ccb_h.target_lun);
27373a31b7ebSMike Smith 
27383a31b7ebSMike Smith     /* firmware does not support commands > 10 bytes */
27393a31b7ebSMike Smith     if (csio->cdb_len > 12/*CISS_CDB_BUFFER_SIZE*/) {
27403a31b7ebSMike Smith 	debug(3, "  command too large (%d > %d)", csio->cdb_len, CISS_CDB_BUFFER_SIZE);
27413a31b7ebSMike Smith 	csio->ccb_h.status = CAM_REQ_CMP_ERR;
27423a31b7ebSMike Smith     }
27433a31b7ebSMike Smith 
27443a31b7ebSMike Smith     /* check that the CDB pointer is not to a physical address */
27453a31b7ebSMike Smith     if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
27463a31b7ebSMike Smith 	debug(3, "  CDB pointer is to physical address");
27473a31b7ebSMike Smith 	csio->ccb_h.status = CAM_REQ_CMP_ERR;
27483a31b7ebSMike Smith     }
27493a31b7ebSMike Smith 
27503a31b7ebSMike Smith     /* if there is data transfer, it must be to/from a virtual address */
27513a31b7ebSMike Smith     if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
27523a31b7ebSMike Smith 	if (csio->ccb_h.flags & CAM_DATA_PHYS) {		/* we can't map it */
27533a31b7ebSMike Smith 	    debug(3, "  data pointer is to physical address");
27543a31b7ebSMike Smith 	    csio->ccb_h.status = CAM_REQ_CMP_ERR;
27553a31b7ebSMike Smith 	}
27563a31b7ebSMike Smith 	if (csio->ccb_h.flags & CAM_SCATTER_VALID) {	/* we want to do the s/g setup */
27573a31b7ebSMike Smith 	    debug(3, "  data has premature s/g setup");
27583a31b7ebSMike Smith 	    csio->ccb_h.status = CAM_REQ_CMP_ERR;
27593a31b7ebSMike Smith 	}
27603a31b7ebSMike Smith     }
27613a31b7ebSMike Smith 
27623a31b7ebSMike Smith     /* abandon aborted ccbs or those that have failed validation */
27633a31b7ebSMike Smith     if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
27643a31b7ebSMike Smith 	debug(3, "abandoning CCB due to abort/validation failure");
27653a31b7ebSMike Smith 	return(EINVAL);
27663a31b7ebSMike Smith     }
27673a31b7ebSMike Smith 
27683a31b7ebSMike Smith     /* handle emulation of some SCSI commands ourself */
27693a31b7ebSMike Smith     if (ciss_cam_emulate(sc, csio))
27703a31b7ebSMike Smith 	return(0);
27713a31b7ebSMike Smith 
27723a31b7ebSMike Smith     /*
27733a31b7ebSMike Smith      * Get a request to manage this command.  If we can't, return the
27743a31b7ebSMike Smith      * ccb, freeze the queue and flag so that we unfreeze it when a
27753a31b7ebSMike Smith      * request completes.
27763a31b7ebSMike Smith      */
27773a31b7ebSMike Smith     if ((error = ciss_get_request(sc, &cr)) != 0) {
27781fe6c4eeSScott Long 	xpt_freeze_simq(sim, 1);
27793a31b7ebSMike Smith 	csio->ccb_h.status |= CAM_REQUEUE_REQ;
27803a31b7ebSMike Smith 	return(error);
27813a31b7ebSMike Smith     }
27823a31b7ebSMike Smith 
27833a31b7ebSMike Smith     /*
27843a31b7ebSMike Smith      * Build the command.
27853a31b7ebSMike Smith      */
27863a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
27873a31b7ebSMike Smith     cr->cr_data = csio->data_ptr;
27883a31b7ebSMike Smith     cr->cr_length = csio->dxfer_len;
27893a31b7ebSMike Smith     cr->cr_complete = ciss_cam_complete;
27903a31b7ebSMike Smith     cr->cr_private = csio;
27913a31b7ebSMike Smith 
2792c6131460SPaul Saab     /*
2793c6131460SPaul Saab      * Target the right logical volume.
2794c6131460SPaul Saab      */
27951fe6c4eeSScott Long     if (CISS_IS_PHYSICAL(bus))
27961fe6c4eeSScott Long 	cc->header.address =
27971fe6c4eeSScott Long 	    sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_address;
27981fe6c4eeSScott Long     else
27991fe6c4eeSScott Long 	cc->header.address =
28001fe6c4eeSScott Long 	    sc->ciss_logical[bus][target].cl_address;
28013a31b7ebSMike Smith     cc->cdb.cdb_length = csio->cdb_len;
28023a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
28033a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;	/* XXX ordered tags? */
28043a31b7ebSMike Smith     if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
28053a31b7ebSMike Smith 	cr->cr_flags = CISS_REQ_DATAOUT;
28063a31b7ebSMike Smith 	cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
28073a31b7ebSMike Smith     } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
28083a31b7ebSMike Smith 	cr->cr_flags = CISS_REQ_DATAIN;
28093a31b7ebSMike Smith 	cc->cdb.direction = CISS_CDB_DIRECTION_READ;
28103a31b7ebSMike Smith     } else {
28113a31b7ebSMike Smith 	cr->cr_flags = 0;
28123a31b7ebSMike Smith 	cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
28133a31b7ebSMike Smith     }
28143a31b7ebSMike Smith     cc->cdb.timeout = (csio->ccb_h.timeout / 1000) + 1;
28153a31b7ebSMike Smith     if (csio->ccb_h.flags & CAM_CDB_POINTER) {
28163a31b7ebSMike Smith 	bcopy(csio->cdb_io.cdb_ptr, &cc->cdb.cdb[0], csio->cdb_len);
28173a31b7ebSMike Smith     } else {
28183a31b7ebSMike Smith 	bcopy(csio->cdb_io.cdb_bytes, &cc->cdb.cdb[0], csio->cdb_len);
28193a31b7ebSMike Smith     }
28203a31b7ebSMike Smith 
28213a31b7ebSMike Smith     /*
28223a31b7ebSMike Smith      * Submit the request to the adapter.
28233a31b7ebSMike Smith      *
28243a31b7ebSMike Smith      * Note that this may fail if we're unable to map the request (and
28253a31b7ebSMike Smith      * if we ever learn a transport layer other than simple, may fail
28263a31b7ebSMike Smith      * if the adapter rejects the command).
28273a31b7ebSMike Smith      */
28283a31b7ebSMike Smith     if ((error = ciss_start(cr)) != 0) {
28291fe6c4eeSScott Long 	xpt_freeze_simq(sim, 1);
2830639717c8SPaul Saab 	if (error == EINPROGRESS) {
2831639717c8SPaul Saab 	    csio->ccb_h.status |= CAM_RELEASE_SIMQ;
2832639717c8SPaul Saab 	    error = 0;
2833639717c8SPaul Saab 	} else {
28343a31b7ebSMike Smith 	    csio->ccb_h.status |= CAM_REQUEUE_REQ;
28353a31b7ebSMike Smith 	    ciss_release_request(cr);
2836639717c8SPaul Saab 	}
28373a31b7ebSMike Smith 	return(error);
28383a31b7ebSMike Smith     }
28393a31b7ebSMike Smith 
28403a31b7ebSMike Smith     return(0);
28413a31b7ebSMike Smith }
28423a31b7ebSMike Smith 
28433a31b7ebSMike Smith /************************************************************************
28443a31b7ebSMike Smith  * Emulate SCSI commands the adapter doesn't handle as we might like.
28453a31b7ebSMike Smith  */
28463a31b7ebSMike Smith static int
28473a31b7ebSMike Smith ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio)
28483a31b7ebSMike Smith {
2849c6131460SPaul Saab     int		bus, target;
28503a31b7ebSMike Smith     u_int8_t	opcode;
28513a31b7ebSMike Smith 
28523a31b7ebSMike Smith     target = csio->ccb_h.target_id;
2853c6131460SPaul Saab     bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
28543a31b7ebSMike Smith     opcode = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
28553a31b7ebSMike Smith 	*(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0];
28563a31b7ebSMike Smith 
28571fe6c4eeSScott Long     if (CISS_IS_PHYSICAL(bus)) {
28581fe6c4eeSScott Long 	if (sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_online != 1) {
28591fe6c4eeSScott Long 	    csio->ccb_h.status = CAM_SEL_TIMEOUT;
28601fe6c4eeSScott Long 	    xpt_done((union ccb *)csio);
28611fe6c4eeSScott Long 	    return(1);
28621fe6c4eeSScott Long 	} else
28631fe6c4eeSScott Long 	    return(0);
28641fe6c4eeSScott Long     }
28651fe6c4eeSScott Long 
28663a31b7ebSMike Smith     /*
28676f71a953SPaul Saab      * Handle requests for volumes that don't exist or are not online.
28686f71a953SPaul Saab      * A selection timeout is slightly better than an illegal request.
28696f71a953SPaul Saab      * Other errors might be better.
28703a31b7ebSMike Smith      */
28716f71a953SPaul Saab     if (sc->ciss_logical[bus][target].cl_status != CISS_LD_ONLINE) {
28723a31b7ebSMike Smith 	csio->ccb_h.status = CAM_SEL_TIMEOUT;
28733a31b7ebSMike Smith 	xpt_done((union ccb *)csio);
28743a31b7ebSMike Smith 	return(1);
28753a31b7ebSMike Smith     }
28763a31b7ebSMike Smith 
28773a31b7ebSMike Smith     /* if we have to fake Synchronise Cache */
28783a31b7ebSMike Smith     if (sc->ciss_flags & CISS_FLAG_FAKE_SYNCH) {
28793a31b7ebSMike Smith 	/*
28803a31b7ebSMike Smith 	 * If this is a Synchronise Cache command, typically issued when
28813a31b7ebSMike Smith 	 * a device is closed, flush the adapter and complete now.
28823a31b7ebSMike Smith 	 */
28833a31b7ebSMike Smith 	if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
28843a31b7ebSMike Smith 	     *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE) {
28853a31b7ebSMike Smith 	    ciss_flush_adapter(sc);
28863a31b7ebSMike Smith 	    csio->ccb_h.status = CAM_REQ_CMP;
28873a31b7ebSMike Smith 	    xpt_done((union ccb *)csio);
28883a31b7ebSMike Smith 	    return(1);
28893a31b7ebSMike Smith 	}
28903a31b7ebSMike Smith     }
28913a31b7ebSMike Smith 
28923a31b7ebSMike Smith     return(0);
28933a31b7ebSMike Smith }
28943a31b7ebSMike Smith 
28953a31b7ebSMike Smith /************************************************************************
28963a31b7ebSMike Smith  * Check for possibly-completed commands.
28973a31b7ebSMike Smith  */
28983a31b7ebSMike Smith static void
28993a31b7ebSMike Smith ciss_cam_poll(struct cam_sim *sim)
29003a31b7ebSMike Smith {
29013a31b7ebSMike Smith     struct ciss_softc	*sc = cam_sim_softc(sim);
29023a31b7ebSMike Smith 
29033a31b7ebSMike Smith     debug_called(2);
29043a31b7ebSMike Smith 
29053a31b7ebSMike Smith     ciss_done(sc);
29063a31b7ebSMike Smith }
29073a31b7ebSMike Smith 
29083a31b7ebSMike Smith /************************************************************************
29093a31b7ebSMike Smith  * Handle completion of a command - pass results back through the CCB
29103a31b7ebSMike Smith  */
29113a31b7ebSMike Smith static void
29123a31b7ebSMike Smith ciss_cam_complete(struct ciss_request *cr)
29133a31b7ebSMike Smith {
29143a31b7ebSMike Smith     struct ciss_softc		*sc;
29153a31b7ebSMike Smith     struct ciss_command		*cc;
29163a31b7ebSMike Smith     struct ciss_error_info	*ce;
29173a31b7ebSMike Smith     struct ccb_scsiio		*csio;
29183a31b7ebSMike Smith     int				scsi_status;
29193a31b7ebSMike Smith     int				command_status;
29203a31b7ebSMike Smith 
29213a31b7ebSMike Smith     debug_called(2);
29223a31b7ebSMike Smith 
29233a31b7ebSMike Smith     sc = cr->cr_sc;
29243a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
29253a31b7ebSMike Smith     ce = (struct ciss_error_info *)&(cc->sg[0]);
29263a31b7ebSMike Smith     csio = (struct ccb_scsiio *)cr->cr_private;
29273a31b7ebSMike Smith 
29283a31b7ebSMike Smith     /*
29293a31b7ebSMike Smith      * Extract status values from request.
29303a31b7ebSMike Smith      */
29313a31b7ebSMike Smith     ciss_report_request(cr, &command_status, &scsi_status);
29323a31b7ebSMike Smith     csio->scsi_status = scsi_status;
29333a31b7ebSMike Smith 
29343a31b7ebSMike Smith     /*
29353a31b7ebSMike Smith      * Handle specific SCSI status values.
29363a31b7ebSMike Smith      */
29373a31b7ebSMike Smith     switch(scsi_status) {
29383a31b7ebSMike Smith 	/* no status due to adapter error */
29393a31b7ebSMike Smith     case -1:
29403a31b7ebSMike Smith 	debug(0, "adapter error");
29413a31b7ebSMike Smith 	csio->ccb_h.status = CAM_REQ_CMP_ERR;
29423a31b7ebSMike Smith 	break;
29433a31b7ebSMike Smith 
29443a31b7ebSMike Smith 	/* no status due to command completed OK */
29453a31b7ebSMike Smith     case SCSI_STATUS_OK:		/* CISS_SCSI_STATUS_GOOD */
29463a31b7ebSMike Smith 	debug(2, "SCSI_STATUS_OK");
29473a31b7ebSMike Smith 	csio->ccb_h.status = CAM_REQ_CMP;
29483a31b7ebSMike Smith 	break;
29493a31b7ebSMike Smith 
29503a31b7ebSMike Smith 	/* check condition, sense data included */
29513a31b7ebSMike Smith     case SCSI_STATUS_CHECK_COND:	/* CISS_SCSI_STATUS_CHECK_CONDITION */
2952c6131460SPaul Saab 	debug(0, "SCSI_STATUS_CHECK_COND  sense size %d  resid %d\n",
29533a31b7ebSMike Smith 	      ce->sense_length, ce->residual_count);
29543a31b7ebSMike Smith 	bzero(&csio->sense_data, SSD_FULL_SIZE);
29553a31b7ebSMike Smith 	bcopy(&ce->sense_info[0], &csio->sense_data, ce->sense_length);
29563a31b7ebSMike Smith 	csio->sense_len = ce->sense_length;
29573a31b7ebSMike Smith 	csio->resid = ce->residual_count;
29583a31b7ebSMike Smith 	csio->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
29593a31b7ebSMike Smith #ifdef CISS_DEBUG
29603a31b7ebSMike Smith 	{
29613a31b7ebSMike Smith 	    struct scsi_sense_data	*sns = (struct scsi_sense_data *)&ce->sense_info[0];
29623a31b7ebSMike Smith 	    debug(0, "sense key %x", sns->flags & SSD_KEY);
29633a31b7ebSMike Smith 	}
29643a31b7ebSMike Smith #endif
29653a31b7ebSMike Smith 	break;
29663a31b7ebSMike Smith 
29673a31b7ebSMike Smith     case SCSI_STATUS_BUSY:		/* CISS_SCSI_STATUS_BUSY */
29683a31b7ebSMike Smith 	debug(0, "SCSI_STATUS_BUSY");
29693a31b7ebSMike Smith 	csio->ccb_h.status = CAM_SCSI_BUSY;
29703a31b7ebSMike Smith 	break;
29713a31b7ebSMike Smith 
29723a31b7ebSMike Smith     default:
29733a31b7ebSMike Smith 	debug(0, "unknown status 0x%x", csio->scsi_status);
29743a31b7ebSMike Smith 	csio->ccb_h.status = CAM_REQ_CMP_ERR;
29753a31b7ebSMike Smith 	break;
29763a31b7ebSMike Smith     }
29773a31b7ebSMike Smith 
29783a31b7ebSMike Smith     /* handle post-command fixup */
29793a31b7ebSMike Smith     ciss_cam_complete_fixup(sc, csio);
29803a31b7ebSMike Smith 
2981d4aa427fSPaul Saab     /* tell CAM we're ready for more commands */
2982d4aa427fSPaul Saab     csio->ccb_h.status |= CAM_RELEASE_SIMQ;
2983d4aa427fSPaul Saab 
29843a31b7ebSMike Smith     xpt_done((union ccb *)csio);
29853a31b7ebSMike Smith     ciss_release_request(cr);
29863a31b7ebSMike Smith }
29873a31b7ebSMike Smith 
29883a31b7ebSMike Smith /********************************************************************************
29893a31b7ebSMike Smith  * Fix up the result of some commands here.
29903a31b7ebSMike Smith  */
29913a31b7ebSMike Smith static void
29923a31b7ebSMike Smith ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio)
29933a31b7ebSMike Smith {
29943a31b7ebSMike Smith     struct scsi_inquiry_data	*inq;
29953a31b7ebSMike Smith     struct ciss_ldrive		*cl;
2996c6131460SPaul Saab     int				bus, target;
29973a31b7ebSMike Smith 
29983a31b7ebSMike Smith     if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
29993a31b7ebSMike Smith 	 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == INQUIRY) {
30003a31b7ebSMike Smith 
30013a31b7ebSMike Smith 	inq = (struct scsi_inquiry_data *)csio->data_ptr;
30023a31b7ebSMike Smith 	target = csio->ccb_h.target_id;
3003c6131460SPaul Saab 	bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
30041fe6c4eeSScott Long 
30051fe6c4eeSScott Long 	/*
30061fe6c4eeSScott Long 	 * Don't let hard drives be seen by the DA driver.  They will still be
30071fe6c4eeSScott Long 	 * attached by the PASS driver.
30081fe6c4eeSScott Long 	 */
30091fe6c4eeSScott Long 	if (CISS_IS_PHYSICAL(bus)) {
30101fe6c4eeSScott Long 	    if (SID_TYPE(inq) == T_DIRECT)
30111fe6c4eeSScott Long 		inq->device = (inq->device & 0xe0) | T_NODEVICE;
30121fe6c4eeSScott Long 	    return;
30131fe6c4eeSScott Long 	}
30141fe6c4eeSScott Long 
3015c6131460SPaul Saab 	cl = &sc->ciss_logical[bus][target];
30163a31b7ebSMike Smith 
3017d4aa427fSPaul Saab 	padstr(inq->vendor, "COMPAQ", 8);
3018d4aa427fSPaul Saab 	padstr(inq->product, ciss_name_ldrive_org(cl->cl_ldrive->fault_tolerance), 8);
3019d4aa427fSPaul Saab 	padstr(inq->revision, ciss_name_ldrive_status(cl->cl_lstatus->status), 16);
30203a31b7ebSMike Smith     }
30213a31b7ebSMike Smith }
30223a31b7ebSMike Smith 
30233a31b7ebSMike Smith 
30243a31b7ebSMike Smith /********************************************************************************
3025d4aa427fSPaul Saab  * Find a peripheral attached at (target)
30263a31b7ebSMike Smith  */
30273a31b7ebSMike Smith static struct cam_periph *
3028c6131460SPaul Saab ciss_find_periph(struct ciss_softc *sc, int bus, int target)
30293a31b7ebSMike Smith {
30303a31b7ebSMike Smith     struct cam_periph	*periph;
30313a31b7ebSMike Smith     struct cam_path	*path;
30323a31b7ebSMike Smith     int			status;
30333a31b7ebSMike Smith 
3034c6131460SPaul Saab     status = xpt_create_path(&path, NULL, cam_sim_path(sc->ciss_cam_sim[bus]),
3035c6131460SPaul Saab 			     target, 0);
30363a31b7ebSMike Smith     if (status == CAM_REQ_CMP) {
30373a31b7ebSMike Smith 	periph = cam_periph_find(path, NULL);
30383a31b7ebSMike Smith 	xpt_free_path(path);
30393a31b7ebSMike Smith     } else {
30403a31b7ebSMike Smith 	periph = NULL;
30413a31b7ebSMike Smith     }
30423a31b7ebSMike Smith     return(periph);
30433a31b7ebSMike Smith }
30443a31b7ebSMike Smith 
30453a31b7ebSMike Smith /********************************************************************************
30463a31b7ebSMike Smith  * Name the device at (target)
30473a31b7ebSMike Smith  *
30483a31b7ebSMike Smith  * XXX is this strictly correct?
30493a31b7ebSMike Smith  */
305037c84183SPoul-Henning Kamp static int
3051c6131460SPaul Saab ciss_name_device(struct ciss_softc *sc, int bus, int target)
30523a31b7ebSMike Smith {
30533a31b7ebSMike Smith     struct cam_periph	*periph;
30543a31b7ebSMike Smith 
3055d49f1379SPaul Saab     if (CISS_IS_PHYSICAL(bus))
30561fe6c4eeSScott Long 	return (0);
3057c6131460SPaul Saab     if ((periph = ciss_find_periph(sc, bus, target)) != NULL) {
30581fe6c4eeSScott Long 	sprintf(sc->ciss_logical[bus][target].cl_name, "%s%d",
30591fe6c4eeSScott Long 		periph->periph_name, periph->unit_number);
30603a31b7ebSMike Smith 	return(0);
30613a31b7ebSMike Smith     }
3062c6131460SPaul Saab     sc->ciss_logical[bus][target].cl_name[0] = 0;
30633a31b7ebSMike Smith     return(ENOENT);
30643a31b7ebSMike Smith }
30653a31b7ebSMike Smith 
30663a31b7ebSMike Smith /************************************************************************
30673a31b7ebSMike Smith  * Periodic status monitoring.
30683a31b7ebSMike Smith  */
30693a31b7ebSMike Smith static void
30703a31b7ebSMike Smith ciss_periodic(void *arg)
30713a31b7ebSMike Smith {
30723a31b7ebSMike Smith     struct ciss_softc	*sc;
3073e08d902aSMitsuru IWASAKI     struct ciss_request	*cr = NULL;
3074e08d902aSMitsuru IWASAKI     struct ciss_command	*cc = NULL;
3075e08d902aSMitsuru IWASAKI     int			error = 0;
30763a31b7ebSMike Smith 
30773a31b7ebSMike Smith     debug_called(1);
30783a31b7ebSMike Smith 
30793a31b7ebSMike Smith     sc = (struct ciss_softc *)arg;
30803a31b7ebSMike Smith 
30813a31b7ebSMike Smith     /*
30823a31b7ebSMike Smith      * Check the adapter heartbeat.
30833a31b7ebSMike Smith      */
30843a31b7ebSMike Smith     if (sc->ciss_cfg->heartbeat == sc->ciss_heartbeat) {
30853a31b7ebSMike Smith 	sc->ciss_heart_attack++;
30863a31b7ebSMike Smith 	debug(0, "adapter heart attack in progress 0x%x/%d",
30873a31b7ebSMike Smith 	      sc->ciss_heartbeat, sc->ciss_heart_attack);
30883a31b7ebSMike Smith 	if (sc->ciss_heart_attack == 3) {
30893a31b7ebSMike Smith 	    ciss_printf(sc, "ADAPTER HEARTBEAT FAILED\n");
3090e08d902aSMitsuru IWASAKI 	    ciss_disable_adapter(sc);
3091e08d902aSMitsuru IWASAKI 	    return;
30923a31b7ebSMike Smith 	}
30933a31b7ebSMike Smith     } else {
30943a31b7ebSMike Smith 	sc->ciss_heartbeat = sc->ciss_cfg->heartbeat;
30953a31b7ebSMike Smith 	sc->ciss_heart_attack = 0;
30963a31b7ebSMike Smith 	debug(3, "new heartbeat 0x%x", sc->ciss_heartbeat);
30973a31b7ebSMike Smith     }
30983a31b7ebSMike Smith 
30993a31b7ebSMike Smith     /*
3100e08d902aSMitsuru IWASAKI      * Send the NOP message and wait for a response.
3101e08d902aSMitsuru IWASAKI      */
3102e08d902aSMitsuru IWASAKI     if ((error = ciss_get_request(sc, &cr)) == 0) {
3103e08d902aSMitsuru IWASAKI 	cc = CISS_FIND_COMMAND(cr);
310454d02e0aSMitsuru IWASAKI 	cr->cr_complete = ciss_nop_complete;
3105e08d902aSMitsuru IWASAKI 	cc->cdb.cdb_length = 1;
3106e08d902aSMitsuru IWASAKI 	cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
3107e08d902aSMitsuru IWASAKI 	cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3108e08d902aSMitsuru IWASAKI 	cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
3109e08d902aSMitsuru IWASAKI 	cc->cdb.timeout = 0;
3110e08d902aSMitsuru IWASAKI 	cc->cdb.cdb[0] = CISS_OPCODE_MESSAGE_NOP;
3111e08d902aSMitsuru IWASAKI 
311254d02e0aSMitsuru IWASAKI 	if ((error = ciss_start(cr)) != 0) {
3113e08d902aSMitsuru IWASAKI 	    ciss_printf(sc, "SENDING NOP MESSAGE FAILED\n");
3114e08d902aSMitsuru IWASAKI 	}
3115e08d902aSMitsuru IWASAKI     }
3116e08d902aSMitsuru IWASAKI 
3117e08d902aSMitsuru IWASAKI     /*
31183a31b7ebSMike Smith      * If the notify event request has died for some reason, or has
31193a31b7ebSMike Smith      * not started yet, restart it.
31203a31b7ebSMike Smith      */
31213a31b7ebSMike Smith     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK)) {
31223a31b7ebSMike Smith 	debug(0, "(re)starting Event Notify chain");
31233a31b7ebSMike Smith 	ciss_notify_event(sc);
31243a31b7ebSMike Smith     }
31253a31b7ebSMike Smith 
31263a31b7ebSMike Smith     /*
31273a31b7ebSMike Smith      * Reschedule.
31283a31b7ebSMike Smith      */
3129975b7318SScott Long     callout_reset(&sc->ciss_periodic, CISS_HEARTBEAT_RATE * hz, ciss_periodic, sc);
31303a31b7ebSMike Smith }
31313a31b7ebSMike Smith 
313254d02e0aSMitsuru IWASAKI static void
313354d02e0aSMitsuru IWASAKI ciss_nop_complete(struct ciss_request *cr)
313454d02e0aSMitsuru IWASAKI {
313554d02e0aSMitsuru IWASAKI     struct ciss_softc		*sc;
313654d02e0aSMitsuru IWASAKI 
313754d02e0aSMitsuru IWASAKI     sc = cr->cr_sc;
313854d02e0aSMitsuru IWASAKI     if (ciss_report_request(cr, NULL, NULL) != 0) {
313954d02e0aSMitsuru IWASAKI 	ciss_printf(sc, "SENDING NOP MESSAGE FAILED\n");
314054d02e0aSMitsuru IWASAKI     }
314154d02e0aSMitsuru IWASAKI 
314254d02e0aSMitsuru IWASAKI     ciss_release_request(cr);
314354d02e0aSMitsuru IWASAKI }
314454d02e0aSMitsuru IWASAKI 
31453a31b7ebSMike Smith /************************************************************************
3146e08d902aSMitsuru IWASAKI  * Disable the adapter.
3147e08d902aSMitsuru IWASAKI  *
3148e08d902aSMitsuru IWASAKI  * The all requests in completed queue is failed with hardware error.
3149e08d902aSMitsuru IWASAKI  * This will cause failover in a multipath configuration.
3150e08d902aSMitsuru IWASAKI  */
3151e08d902aSMitsuru IWASAKI static void
3152e08d902aSMitsuru IWASAKI ciss_disable_adapter(struct ciss_softc *sc)
3153e08d902aSMitsuru IWASAKI {
3154e08d902aSMitsuru IWASAKI     struct ciss_request		*cr;
3155e08d902aSMitsuru IWASAKI     struct ciss_command		*cc;
3156e08d902aSMitsuru IWASAKI     struct ciss_error_info	*ce;
3157e08d902aSMitsuru IWASAKI     int				s;
3158e08d902aSMitsuru IWASAKI 
3159e08d902aSMitsuru IWASAKI     s = splcam();
3160e08d902aSMitsuru IWASAKI 
3161e08d902aSMitsuru IWASAKI     CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
3162e08d902aSMitsuru IWASAKI     pci_disable_busmaster(sc->ciss_dev);
3163e08d902aSMitsuru IWASAKI     sc->ciss_flags &= ~CISS_FLAG_RUNNING;
3164e08d902aSMitsuru IWASAKI 
3165e08d902aSMitsuru IWASAKI     for (;;) {
3166e08d902aSMitsuru IWASAKI 	if ((cr = ciss_dequeue_busy(sc)) == NULL)
3167e08d902aSMitsuru IWASAKI 	    break;
3168e08d902aSMitsuru IWASAKI 
3169e08d902aSMitsuru IWASAKI 	cc = CISS_FIND_COMMAND(cr);
3170e08d902aSMitsuru IWASAKI 	ce = (struct ciss_error_info *)&(cc->sg[0]);
3171e08d902aSMitsuru IWASAKI 	ce->command_status = CISS_CMD_STATUS_HARDWARE_ERROR;
3172e08d902aSMitsuru IWASAKI 	ciss_enqueue_complete(cr);
3173e08d902aSMitsuru IWASAKI     }
3174e08d902aSMitsuru IWASAKI 
3175e08d902aSMitsuru IWASAKI     for (;;) {
3176e08d902aSMitsuru IWASAKI 	if ((cr = ciss_dequeue_complete(sc)) == NULL)
3177e08d902aSMitsuru IWASAKI 	    break;
3178e08d902aSMitsuru IWASAKI 
3179e08d902aSMitsuru IWASAKI 	/*
3180e08d902aSMitsuru IWASAKI 	 * If the request has a callback, invoke it.
3181e08d902aSMitsuru IWASAKI 	 */
3182e08d902aSMitsuru IWASAKI 	if (cr->cr_complete != NULL) {
3183e08d902aSMitsuru IWASAKI 	    cr->cr_complete(cr);
3184e08d902aSMitsuru IWASAKI 	    continue;
3185e08d902aSMitsuru IWASAKI 	}
3186e08d902aSMitsuru IWASAKI 
3187e08d902aSMitsuru IWASAKI 	/*
3188e08d902aSMitsuru IWASAKI 	 * If someone is sleeping on this request, wake them up.
3189e08d902aSMitsuru IWASAKI 	 */
3190e08d902aSMitsuru IWASAKI 	if (cr->cr_flags & CISS_REQ_SLEEP) {
3191e08d902aSMitsuru IWASAKI 	    cr->cr_flags &= ~CISS_REQ_SLEEP;
3192e08d902aSMitsuru IWASAKI 	    wakeup(cr);
3193e08d902aSMitsuru IWASAKI 	    continue;
3194e08d902aSMitsuru IWASAKI 	}
3195e08d902aSMitsuru IWASAKI     }
3196e08d902aSMitsuru IWASAKI 
3197e08d902aSMitsuru IWASAKI     splx(s);
3198e08d902aSMitsuru IWASAKI }
3199e08d902aSMitsuru IWASAKI 
3200e08d902aSMitsuru IWASAKI /************************************************************************
32013a31b7ebSMike Smith  * Request a notification response from the adapter.
32023a31b7ebSMike Smith  *
32033a31b7ebSMike Smith  * If (cr) is NULL, this is the first request of the adapter, so
32043a31b7ebSMike Smith  * reset the adapter's message pointer and start with the oldest
32053a31b7ebSMike Smith  * message available.
32063a31b7ebSMike Smith  */
32073a31b7ebSMike Smith static void
32083a31b7ebSMike Smith ciss_notify_event(struct ciss_softc *sc)
32093a31b7ebSMike Smith {
32103a31b7ebSMike Smith     struct ciss_request		*cr;
32113a31b7ebSMike Smith     struct ciss_command		*cc;
32123a31b7ebSMike Smith     struct ciss_notify_cdb	*cnc;
32133a31b7ebSMike Smith     int				error;
32143a31b7ebSMike Smith 
32153a31b7ebSMike Smith     debug_called(1);
32163a31b7ebSMike Smith 
32173a31b7ebSMike Smith     cr = sc->ciss_periodic_notify;
32183a31b7ebSMike Smith 
32193a31b7ebSMike Smith     /* get a request if we don't already have one */
32203a31b7ebSMike Smith     if (cr == NULL) {
32213a31b7ebSMike Smith 	if ((error = ciss_get_request(sc, &cr)) != 0) {
32223a31b7ebSMike Smith 	    debug(0, "can't get notify event request");
32233a31b7ebSMike Smith 	    goto out;
32243a31b7ebSMike Smith 	}
32253a31b7ebSMike Smith 	sc->ciss_periodic_notify = cr;
32263a31b7ebSMike Smith 	cr->cr_complete = ciss_notify_complete;
32273a31b7ebSMike Smith 	debug(1, "acquired request %d", cr->cr_tag);
32283a31b7ebSMike Smith     }
32293a31b7ebSMike Smith 
32303a31b7ebSMike Smith     /*
32313a31b7ebSMike Smith      * Get a databuffer if we don't already have one, note that the
32323a31b7ebSMike Smith      * adapter command wants a larger buffer than the actual
32333a31b7ebSMike Smith      * structure.
32343a31b7ebSMike Smith      */
32353a31b7ebSMike Smith     if (cr->cr_data == NULL) {
32363a31b7ebSMike Smith 	if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
32373a31b7ebSMike Smith 	    debug(0, "can't get notify event request buffer");
32383a31b7ebSMike Smith 	    error = ENOMEM;
32393a31b7ebSMike Smith 	    goto out;
32403a31b7ebSMike Smith 	}
32413a31b7ebSMike Smith 	cr->cr_length = CISS_NOTIFY_DATA_SIZE;
32423a31b7ebSMike Smith     }
32433a31b7ebSMike Smith 
32443a31b7ebSMike Smith     /* re-setup the request's command (since we never release it) XXX overkill*/
32453a31b7ebSMike Smith     ciss_preen_command(cr);
32463a31b7ebSMike Smith 
32473a31b7ebSMike Smith     /* (re)build the notify event command */
32483a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
32493a31b7ebSMike Smith     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
32503a31b7ebSMike Smith     cc->header.address.physical.bus = 0;
32513a31b7ebSMike Smith     cc->header.address.physical.target = 0;
32523a31b7ebSMike Smith 
32533a31b7ebSMike Smith     cc->cdb.cdb_length = sizeof(*cnc);
32543a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
32553a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
32563a31b7ebSMike Smith     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
32573a31b7ebSMike Smith     cc->cdb.timeout = 0;	/* no timeout, we hope */
32583a31b7ebSMike Smith 
32593a31b7ebSMike Smith     cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
32603a31b7ebSMike Smith     bzero(cr->cr_data, CISS_NOTIFY_DATA_SIZE);
32613a31b7ebSMike Smith     cnc->opcode = CISS_OPCODE_READ;
32623a31b7ebSMike Smith     cnc->command = CISS_COMMAND_NOTIFY_ON_EVENT;
32633a31b7ebSMike Smith     cnc->timeout = 0;		/* no timeout, we hope */
32643a31b7ebSMike Smith     cnc->synchronous = 0;
32653a31b7ebSMike Smith     cnc->ordered = 0;
32663a31b7ebSMike Smith     cnc->seek_to_oldest = 0;
32672e80ca8aSPaul Saab     if ((sc->ciss_flags & CISS_FLAG_RUNNING) == 0)
32682e80ca8aSPaul Saab 	cnc->new_only = 1;
32692e80ca8aSPaul Saab     else
32703a31b7ebSMike Smith 	cnc->new_only = 0;
32713a31b7ebSMike Smith     cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
32723a31b7ebSMike Smith 
32733a31b7ebSMike Smith     /* submit the request */
32743a31b7ebSMike Smith     error = ciss_start(cr);
32753a31b7ebSMike Smith 
32763a31b7ebSMike Smith  out:
32773a31b7ebSMike Smith     if (error) {
32783a31b7ebSMike Smith 	if (cr != NULL) {
32793a31b7ebSMike Smith 	    if (cr->cr_data != NULL)
32803a31b7ebSMike Smith 		free(cr->cr_data, CISS_MALLOC_CLASS);
32813a31b7ebSMike Smith 	    ciss_release_request(cr);
32823a31b7ebSMike Smith 	}
32833a31b7ebSMike Smith 	sc->ciss_periodic_notify = NULL;
32843a31b7ebSMike Smith 	debug(0, "can't submit notify event request");
32853a31b7ebSMike Smith 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
32863a31b7ebSMike Smith     } else {
32873a31b7ebSMike Smith 	debug(1, "notify event submitted");
32883a31b7ebSMike Smith 	sc->ciss_flags |= CISS_FLAG_NOTIFY_OK;
32893a31b7ebSMike Smith     }
32903a31b7ebSMike Smith }
32913a31b7ebSMike Smith 
32923a31b7ebSMike Smith static void
32933a31b7ebSMike Smith ciss_notify_complete(struct ciss_request *cr)
32943a31b7ebSMike Smith {
32953a31b7ebSMike Smith     struct ciss_command	*cc;
32963a31b7ebSMike Smith     struct ciss_notify	*cn;
32973a31b7ebSMike Smith     struct ciss_softc	*sc;
32983a31b7ebSMike Smith     int			scsi_status;
32993a31b7ebSMike Smith     int			command_status;
33003a31b7ebSMike Smith     debug_called(1);
33013a31b7ebSMike Smith 
33023a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
33033a31b7ebSMike Smith     cn = (struct ciss_notify *)cr->cr_data;
33043a31b7ebSMike Smith     sc = cr->cr_sc;
33053a31b7ebSMike Smith 
33063a31b7ebSMike Smith     /*
33073a31b7ebSMike Smith      * Report request results, decode status.
33083a31b7ebSMike Smith      */
33093a31b7ebSMike Smith     ciss_report_request(cr, &command_status, &scsi_status);
33103a31b7ebSMike Smith 
33113a31b7ebSMike Smith     /*
33123a31b7ebSMike Smith      * Abort the chain on a fatal error.
33133a31b7ebSMike Smith      *
33143a31b7ebSMike Smith      * XXX which of these are actually errors?
33153a31b7ebSMike Smith      */
33163a31b7ebSMike Smith     if ((command_status != CISS_CMD_STATUS_SUCCESS) &&
33173a31b7ebSMike Smith 	(command_status != CISS_CMD_STATUS_TARGET_STATUS) &&
33183a31b7ebSMike Smith 	(command_status != CISS_CMD_STATUS_TIMEOUT)) {	/* XXX timeout? */
33193a31b7ebSMike Smith 	ciss_printf(sc, "fatal error in Notify Event request (%s)\n",
33203a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
33213a31b7ebSMike Smith 	ciss_release_request(cr);
33223a31b7ebSMike Smith 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
33233a31b7ebSMike Smith 	return;
33243a31b7ebSMike Smith     }
33253a31b7ebSMike Smith 
33263a31b7ebSMike Smith     /*
33273a31b7ebSMike Smith      * If the adapter gave us a text message, print it.
33283a31b7ebSMike Smith      */
33293a31b7ebSMike Smith     if (cn->message[0] != 0)
33303a31b7ebSMike Smith 	ciss_printf(sc, "*** %.80s\n", cn->message);
33313a31b7ebSMike Smith 
33323a31b7ebSMike Smith     debug(0, "notify event class %d subclass %d detail %d",
33333a31b7ebSMike Smith 		cn->class, cn->subclass, cn->detail);
33343a31b7ebSMike Smith 
33353a31b7ebSMike Smith     /*
33363a31b7ebSMike Smith      * If the response indicates that the notifier has been aborted,
33373a31b7ebSMike Smith      * release the notifier command.
33383a31b7ebSMike Smith      */
33393a31b7ebSMike Smith     if ((cn->class == CISS_NOTIFY_NOTIFIER) &&
33403a31b7ebSMike Smith 	(cn->subclass == CISS_NOTIFY_NOTIFIER_STATUS) &&
33413a31b7ebSMike Smith 	(cn->detail == 1)) {
33423a31b7ebSMike Smith 	debug(0, "notifier exiting");
33433a31b7ebSMike Smith 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
33443a31b7ebSMike Smith 	ciss_release_request(cr);
33453a31b7ebSMike Smith 	sc->ciss_periodic_notify = NULL;
33463a31b7ebSMike Smith 	wakeup(&sc->ciss_periodic_notify);
3347c6131460SPaul Saab     } else {
3348c6131460SPaul Saab 	/* Handle notify events in a kernel thread */
3349c6131460SPaul Saab 	ciss_enqueue_notify(cr);
3350c6131460SPaul Saab 	sc->ciss_periodic_notify = NULL;
3351c6131460SPaul Saab 	wakeup(&sc->ciss_periodic_notify);
3352c6131460SPaul Saab 	wakeup(&sc->ciss_notify);
33533a31b7ebSMike Smith     }
33543a31b7ebSMike Smith     /*
33553a31b7ebSMike Smith      * Send a new notify event command, if we're not aborting.
33563a31b7ebSMike Smith      */
33573a31b7ebSMike Smith     if (!(sc->ciss_flags & CISS_FLAG_ABORTING)) {
33583a31b7ebSMike Smith 	ciss_notify_event(sc);
33593a31b7ebSMike Smith     }
33603a31b7ebSMike Smith }
33613a31b7ebSMike Smith 
33623a31b7ebSMike Smith /************************************************************************
33633a31b7ebSMike Smith  * Abort the Notify Event chain.
33643a31b7ebSMike Smith  *
33653a31b7ebSMike Smith  * Note that we can't just abort the command in progress; we have to
33663a31b7ebSMike Smith  * explicitly issue an Abort Notify Event command in order for the
33673a31b7ebSMike Smith  * adapter to clean up correctly.
33683a31b7ebSMike Smith  *
33693a31b7ebSMike Smith  * If we are called with CISS_FLAG_ABORTING set in the adapter softc,
33703a31b7ebSMike Smith  * the chain will not restart itself.
33713a31b7ebSMike Smith  */
33723a31b7ebSMike Smith static int
33733a31b7ebSMike Smith ciss_notify_abort(struct ciss_softc *sc)
33743a31b7ebSMike Smith {
33753a31b7ebSMike Smith     struct ciss_request		*cr;
33763a31b7ebSMike Smith     struct ciss_command		*cc;
33773a31b7ebSMike Smith     struct ciss_notify_cdb	*cnc;
33783a31b7ebSMike Smith     int				error, s, command_status, scsi_status;
33793a31b7ebSMike Smith 
33803a31b7ebSMike Smith     debug_called(1);
33813a31b7ebSMike Smith 
33823a31b7ebSMike Smith     cr = NULL;
33833a31b7ebSMike Smith     error = 0;
33843a31b7ebSMike Smith 
33853a31b7ebSMike Smith     /* verify that there's an outstanding command */
33863a31b7ebSMike Smith     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
33873a31b7ebSMike Smith 	goto out;
33883a31b7ebSMike Smith 
33893a31b7ebSMike Smith     /* get a command to issue the abort with */
33903a31b7ebSMike Smith     if ((error = ciss_get_request(sc, &cr)))
33913a31b7ebSMike Smith 	goto out;
33923a31b7ebSMike Smith 
33933a31b7ebSMike Smith     /* get a buffer for the result */
33943a31b7ebSMike Smith     if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
33953a31b7ebSMike Smith 	debug(0, "can't get notify event request buffer");
33963a31b7ebSMike Smith 	error = ENOMEM;
33973a31b7ebSMike Smith 	goto out;
33983a31b7ebSMike Smith     }
33993a31b7ebSMike Smith     cr->cr_length = CISS_NOTIFY_DATA_SIZE;
34003a31b7ebSMike Smith 
34013a31b7ebSMike Smith     /* build the CDB */
34023a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
34033a31b7ebSMike Smith     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
34043a31b7ebSMike Smith     cc->header.address.physical.bus = 0;
34053a31b7ebSMike Smith     cc->header.address.physical.target = 0;
34063a31b7ebSMike Smith     cc->cdb.cdb_length = sizeof(*cnc);
34073a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
34083a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
34093a31b7ebSMike Smith     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
34103a31b7ebSMike Smith     cc->cdb.timeout = 0;	/* no timeout, we hope */
34113a31b7ebSMike Smith 
34123a31b7ebSMike Smith     cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
34133a31b7ebSMike Smith     bzero(cnc, sizeof(*cnc));
34143a31b7ebSMike Smith     cnc->opcode = CISS_OPCODE_WRITE;
34153a31b7ebSMike Smith     cnc->command = CISS_COMMAND_ABORT_NOTIFY;
34163a31b7ebSMike Smith     cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
34173a31b7ebSMike Smith 
34183a31b7ebSMike Smith     ciss_print_request(cr);
34193a31b7ebSMike Smith 
34203a31b7ebSMike Smith     /*
34213a31b7ebSMike Smith      * Submit the request and wait for it to complete.
34223a31b7ebSMike Smith      */
34233a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
34243a31b7ebSMike Smith 	ciss_printf(sc, "Abort Notify Event command failed (%d)\n", error);
34253a31b7ebSMike Smith 	goto out;
34263a31b7ebSMike Smith     }
34273a31b7ebSMike Smith 
34283a31b7ebSMike Smith     /*
34293a31b7ebSMike Smith      * Check response.
34303a31b7ebSMike Smith      */
34313a31b7ebSMike Smith     ciss_report_request(cr, &command_status, &scsi_status);
34323a31b7ebSMike Smith     switch(command_status) {
34333a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:
34343a31b7ebSMike Smith 	break;
34353a31b7ebSMike Smith     case CISS_CMD_STATUS_INVALID_COMMAND:
34363a31b7ebSMike Smith 	/*
34373a31b7ebSMike Smith 	 * Some older adapters don't support the CISS version of this
34383a31b7ebSMike Smith 	 * command.  Fall back to using the BMIC version.
34393a31b7ebSMike Smith 	 */
34403a31b7ebSMike Smith 	error = ciss_notify_abort_bmic(sc);
34413a31b7ebSMike Smith 	if (error != 0)
34423a31b7ebSMike Smith 	    goto out;
34433a31b7ebSMike Smith 	break;
34443a31b7ebSMike Smith 
34453a31b7ebSMike Smith     case CISS_CMD_STATUS_TARGET_STATUS:
34463a31b7ebSMike Smith 	/*
34473a31b7ebSMike Smith 	 * This can happen if the adapter thinks there wasn't an outstanding
34483a31b7ebSMike Smith 	 * Notify Event command but we did.  We clean up here.
34493a31b7ebSMike Smith 	 */
34503a31b7ebSMike Smith 	if (scsi_status == CISS_SCSI_STATUS_CHECK_CONDITION) {
34513a31b7ebSMike Smith 	    if (sc->ciss_periodic_notify != NULL)
34523a31b7ebSMike Smith 		ciss_release_request(sc->ciss_periodic_notify);
34533a31b7ebSMike Smith 	    error = 0;
34543a31b7ebSMike Smith 	    goto out;
34553a31b7ebSMike Smith 	}
34563a31b7ebSMike Smith 	/* FALLTHROUGH */
34573a31b7ebSMike Smith 
34583a31b7ebSMike Smith     default:
34593a31b7ebSMike Smith 	ciss_printf(sc, "Abort Notify Event command failed (%s)\n",
34603a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
34613a31b7ebSMike Smith 	error = EIO;
34623a31b7ebSMike Smith 	goto out;
34633a31b7ebSMike Smith     }
34643a31b7ebSMike Smith 
34653a31b7ebSMike Smith     /*
34663a31b7ebSMike Smith      * Sleep waiting for the notifier command to complete.  Note
34673a31b7ebSMike Smith      * that if it doesn't, we may end up in a bad situation, since
34683a31b7ebSMike Smith      * the adapter may deliver it later.  Also note that the adapter
34693a31b7ebSMike Smith      * requires the Notify Event command to be cancelled in order to
34703a31b7ebSMike Smith      * maintain internal bookkeeping.
34713a31b7ebSMike Smith      */
34723a31b7ebSMike Smith     s = splcam();
34733a31b7ebSMike Smith     while (sc->ciss_periodic_notify != NULL) {
3474975b7318SScott Long 	error = msleep(&sc->ciss_periodic_notify, &sc->ciss_mtx, PRIBIO, "cissNEA", hz * 5);
34753a31b7ebSMike Smith 	if (error == EWOULDBLOCK) {
34763a31b7ebSMike Smith 	    ciss_printf(sc, "Notify Event command failed to abort, adapter may wedge.\n");
34773a31b7ebSMike Smith 	    break;
34783a31b7ebSMike Smith 	}
34793a31b7ebSMike Smith     }
34803a31b7ebSMike Smith     splx(s);
34813a31b7ebSMike Smith 
34823a31b7ebSMike Smith  out:
34833a31b7ebSMike Smith     /* release the cancel request */
34843a31b7ebSMike Smith     if (cr != NULL) {
34853a31b7ebSMike Smith 	if (cr->cr_data != NULL)
34863a31b7ebSMike Smith 	    free(cr->cr_data, CISS_MALLOC_CLASS);
34873a31b7ebSMike Smith 	ciss_release_request(cr);
34883a31b7ebSMike Smith     }
34893a31b7ebSMike Smith     if (error == 0)
34903a31b7ebSMike Smith 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
34913a31b7ebSMike Smith     return(error);
34923a31b7ebSMike Smith }
34933a31b7ebSMike Smith 
34943a31b7ebSMike Smith /************************************************************************
34953a31b7ebSMike Smith  * Abort the Notify Event chain using a BMIC command.
34963a31b7ebSMike Smith  */
34973a31b7ebSMike Smith static int
34983a31b7ebSMike Smith ciss_notify_abort_bmic(struct ciss_softc *sc)
34993a31b7ebSMike Smith {
35003a31b7ebSMike Smith     struct ciss_request			*cr;
35013a31b7ebSMike Smith     int					error, command_status;
35023a31b7ebSMike Smith 
35033a31b7ebSMike Smith     debug_called(1);
35043a31b7ebSMike Smith 
35053a31b7ebSMike Smith     cr = NULL;
35063a31b7ebSMike Smith     error = 0;
35073a31b7ebSMike Smith 
35083a31b7ebSMike Smith     /* verify that there's an outstanding command */
35093a31b7ebSMike Smith     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
35103a31b7ebSMike Smith 	goto out;
35113a31b7ebSMike Smith 
35123a31b7ebSMike Smith     /*
35133a31b7ebSMike Smith      * Build a BMIC command to cancel the Notify on Event command.
35143a31b7ebSMike Smith      *
35153a31b7ebSMike Smith      * Note that we are sending a CISS opcode here.  Odd.
35163a31b7ebSMike Smith      */
35173a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_COMMAND_ABORT_NOTIFY,
35183a31b7ebSMike Smith 				       NULL, 0)) != 0)
35193a31b7ebSMike Smith 	goto out;
35203a31b7ebSMike Smith 
35213a31b7ebSMike Smith     /*
35223a31b7ebSMike Smith      * Submit the request and wait for it to complete.
35233a31b7ebSMike Smith      */
35243a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
35253a31b7ebSMike Smith 	ciss_printf(sc, "error sending BMIC Cancel Notify on Event command (%d)\n", error);
35263a31b7ebSMike Smith 	goto out;
35273a31b7ebSMike Smith     }
35283a31b7ebSMike Smith 
35293a31b7ebSMike Smith     /*
35303a31b7ebSMike Smith      * Check response.
35313a31b7ebSMike Smith      */
35323a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
35333a31b7ebSMike Smith     switch(command_status) {
35343a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:
35353a31b7ebSMike Smith 	break;
35363a31b7ebSMike Smith     default:
35373a31b7ebSMike Smith 	ciss_printf(sc, "error cancelling Notify on Event (%s)\n",
35383a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
35393a31b7ebSMike Smith 	error = EIO;
35403a31b7ebSMike Smith 	goto out;
35413a31b7ebSMike Smith     }
35423a31b7ebSMike Smith 
35433a31b7ebSMike Smith out:
35443a31b7ebSMike Smith     if (cr != NULL)
35453a31b7ebSMike Smith 	ciss_release_request(cr);
35463a31b7ebSMike Smith     return(error);
35473a31b7ebSMike Smith }
35483a31b7ebSMike Smith 
35493a31b7ebSMike Smith /************************************************************************
3550c6131460SPaul Saab  * Handle rescanning all the logical volumes when a notify event
3551c6131460SPaul Saab  * causes the drives to come online or offline.
3552c6131460SPaul Saab  */
3553c6131460SPaul Saab static void
3554c6131460SPaul Saab ciss_notify_rescan_logical(struct ciss_softc *sc)
3555c6131460SPaul Saab {
3556c6131460SPaul Saab     struct ciss_lun_report      *cll;
3557c6131460SPaul Saab     struct ciss_ldrive		*ld;
3558c6131460SPaul Saab     int                         i, j, ndrives;
3559c6131460SPaul Saab 
3560c6131460SPaul Saab     /*
3561c6131460SPaul Saab      * We must rescan all logical volumes to get the right logical
3562c6131460SPaul Saab      * drive address.
3563c6131460SPaul Saab      */
3564c6131460SPaul Saab     cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
3565c6131460SPaul Saab                            CISS_MAX_LOGICAL);
3566c6131460SPaul Saab     if (cll == NULL)
3567c6131460SPaul Saab         return;
3568c6131460SPaul Saab 
3569c6131460SPaul Saab     ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
3570c6131460SPaul Saab 
3571c6131460SPaul Saab     /*
3572c6131460SPaul Saab      * Delete any of the drives which were destroyed by the
3573c6131460SPaul Saab      * firmware.
3574c6131460SPaul Saab      */
35751fe6c4eeSScott Long     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
3576c6131460SPaul Saab 	for (j = 0; j < CISS_MAX_LOGICAL; j++) {
3577c6131460SPaul Saab 	    ld = &sc->ciss_logical[i][j];
3578c6131460SPaul Saab 
3579c6131460SPaul Saab 	    if (ld->cl_update == 0)
3580c6131460SPaul Saab 		continue;
3581c6131460SPaul Saab 
3582c6131460SPaul Saab 	    if (ld->cl_status != CISS_LD_ONLINE) {
3583c6131460SPaul Saab 		ciss_cam_rescan_target(sc, i, j);
3584c6131460SPaul Saab 		ld->cl_update = 0;
3585c6131460SPaul Saab 		if (ld->cl_ldrive)
3586c6131460SPaul Saab 		    free(ld->cl_ldrive, CISS_MALLOC_CLASS);
3587c6131460SPaul Saab 		if (ld->cl_lstatus)
3588c6131460SPaul Saab 		    free(ld->cl_lstatus, CISS_MALLOC_CLASS);
3589c6131460SPaul Saab 
3590c6131460SPaul Saab 		ld->cl_ldrive = NULL;
3591c6131460SPaul Saab 		ld->cl_lstatus = NULL;
3592c6131460SPaul Saab 	    }
3593c6131460SPaul Saab 	}
3594c6131460SPaul Saab     }
3595c6131460SPaul Saab 
3596c6131460SPaul Saab     /*
3597c6131460SPaul Saab      * Scan for new drives.
3598c6131460SPaul Saab      */
3599c6131460SPaul Saab     for (i = 0; i < ndrives; i++) {
3600c6131460SPaul Saab 	int	bus, target;
3601c6131460SPaul Saab 
3602c6131460SPaul Saab 	bus 	= CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
3603c6131460SPaul Saab 	target	= CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
3604c6131460SPaul Saab 	ld	= &sc->ciss_logical[bus][target];
3605c6131460SPaul Saab 
3606c6131460SPaul Saab 	if (ld->cl_update == 0)
3607c6131460SPaul Saab 		continue;
3608c6131460SPaul Saab 
36095cf5a430SPaul Saab 	ld->cl_update		= 0;
3610c6131460SPaul Saab 	ld->cl_address		= cll->lun[i];
3611c6131460SPaul Saab 	ld->cl_controller	= &sc->ciss_controllers[bus];
3612c6131460SPaul Saab 	if (ciss_identify_logical(sc, ld) == 0) {
3613c6131460SPaul Saab 	    ciss_cam_rescan_target(sc, bus, target);
3614c6131460SPaul Saab 	}
3615c6131460SPaul Saab     }
3616c6131460SPaul Saab     free(cll, CISS_MALLOC_CLASS);
3617c6131460SPaul Saab }
3618c6131460SPaul Saab 
3619c6131460SPaul Saab /************************************************************************
36203a31b7ebSMike Smith  * Handle a notify event relating to the status of a logical drive.
36213a31b7ebSMike Smith  *
36223a31b7ebSMike Smith  * XXX need to be able to defer some of these to properly handle
36233a31b7ebSMike Smith  *     calling the "ID Physical drive" command, unless the 'extended'
36243a31b7ebSMike Smith  *     drive IDs are always in BIG_MAP format.
36253a31b7ebSMike Smith  */
36263a31b7ebSMike Smith static void
36273a31b7ebSMike Smith ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn)
36283a31b7ebSMike Smith {
36293a31b7ebSMike Smith     struct ciss_ldrive	*ld;
3630c6131460SPaul Saab     int			ostatus, bus, target;
36313a31b7ebSMike Smith 
36323a31b7ebSMike Smith     debug_called(2);
36333a31b7ebSMike Smith 
3634c6131460SPaul Saab     bus		= cn->device.physical.bus;
3635c6131460SPaul Saab     target	= cn->data.logical_status.logical_drive;
3636c6131460SPaul Saab     ld		= &sc->ciss_logical[bus][target];
36373a31b7ebSMike Smith 
36383a31b7ebSMike Smith     switch (cn->subclass) {
36393a31b7ebSMike Smith     case CISS_NOTIFY_LOGICAL_STATUS:
36403a31b7ebSMike Smith 	switch (cn->detail) {
36413a31b7ebSMike Smith 	case 0:
3642c6131460SPaul Saab 	    ciss_name_device(sc, bus, target);
36433a31b7ebSMike Smith 	    ciss_printf(sc, "logical drive %d (%s) changed status %s->%s, spare status 0x%b\n",
36443a31b7ebSMike Smith 			cn->data.logical_status.logical_drive, ld->cl_name,
36453a31b7ebSMike Smith 			ciss_name_ldrive_status(cn->data.logical_status.previous_state),
36463a31b7ebSMike Smith 			ciss_name_ldrive_status(cn->data.logical_status.new_state),
36473a31b7ebSMike Smith 			cn->data.logical_status.spare_state,
36483a31b7ebSMike Smith 			"\20\1configured\2rebuilding\3failed\4in use\5available\n");
36493a31b7ebSMike Smith 
36503a31b7ebSMike Smith 	    /*
36513a31b7ebSMike Smith 	     * Update our idea of the drive's status.
36523a31b7ebSMike Smith 	     */
36533a31b7ebSMike Smith 	    ostatus = ciss_decode_ldrive_status(cn->data.logical_status.previous_state);
36543a31b7ebSMike Smith 	    ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
3655d4aa427fSPaul Saab 	    if (ld->cl_lstatus != NULL)
36563a31b7ebSMike Smith 		ld->cl_lstatus->status = cn->data.logical_status.new_state;
36573a31b7ebSMike Smith 
3658d4aa427fSPaul Saab 	    /*
3659d4aa427fSPaul Saab 	     * Have CAM rescan the drive if its status has changed.
3660d4aa427fSPaul Saab 	     */
3661c6131460SPaul Saab 	    if (ostatus != ld->cl_status) {
3662c6131460SPaul Saab 		ld->cl_update = 1;
3663c6131460SPaul Saab 		ciss_notify_rescan_logical(sc);
3664c6131460SPaul Saab 	    }
3665d4aa427fSPaul Saab 
36663a31b7ebSMike Smith 	    break;
36673a31b7ebSMike Smith 
36683a31b7ebSMike Smith 	case 1:	/* logical drive has recognised new media, needs Accept Media Exchange */
3669c6131460SPaul Saab 	    ciss_name_device(sc, bus, target);
36703a31b7ebSMike Smith 	    ciss_printf(sc, "logical drive %d (%s) media exchanged, ready to go online\n",
36713a31b7ebSMike Smith 			cn->data.logical_status.logical_drive, ld->cl_name);
367288c78a38SPaul Saab 	    ciss_accept_media(sc, ld);
3673139233cbSPaul Saab 
3674139233cbSPaul Saab 	    ld->cl_update = 1;
3675139233cbSPaul Saab 	    ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
3676139233cbSPaul Saab 	    ciss_notify_rescan_logical(sc);
36773a31b7ebSMike Smith 	    break;
36783a31b7ebSMike Smith 
36793a31b7ebSMike Smith 	case 2:
36803a31b7ebSMike Smith 	case 3:
36813a31b7ebSMike Smith 	    ciss_printf(sc, "rebuild of logical drive %d (%s) failed due to %s error\n",
36823a31b7ebSMike Smith 			cn->data.rebuild_aborted.logical_drive,
3683c6131460SPaul Saab 			ld->cl_name,
36843a31b7ebSMike Smith 			(cn->detail == 2) ? "read" : "write");
36853a31b7ebSMike Smith 	    break;
36863a31b7ebSMike Smith 	}
36873a31b7ebSMike Smith 	break;
36883a31b7ebSMike Smith 
36893a31b7ebSMike Smith     case CISS_NOTIFY_LOGICAL_ERROR:
36903a31b7ebSMike Smith 	if (cn->detail == 0) {
36913a31b7ebSMike Smith 	    ciss_printf(sc, "FATAL I/O ERROR on logical drive %d (%s), SCSI port %d ID %d\n",
36923a31b7ebSMike Smith 			cn->data.io_error.logical_drive,
3693c6131460SPaul Saab 			ld->cl_name,
36943a31b7ebSMike Smith 			cn->data.io_error.failure_bus,
36953a31b7ebSMike Smith 			cn->data.io_error.failure_drive);
36963a31b7ebSMike Smith 	    /* XXX should we take the drive down at this point, or will we be told? */
36973a31b7ebSMike Smith 	}
36983a31b7ebSMike Smith 	break;
36993a31b7ebSMike Smith 
37003a31b7ebSMike Smith     case CISS_NOTIFY_LOGICAL_SURFACE:
37013a31b7ebSMike Smith 	if (cn->detail == 0)
37023a31b7ebSMike Smith 	    ciss_printf(sc, "logical drive %d (%s) completed consistency initialisation\n",
37033a31b7ebSMike Smith 			cn->data.consistency_completed.logical_drive,
3704c6131460SPaul Saab 			ld->cl_name);
37053a31b7ebSMike Smith 	break;
37063a31b7ebSMike Smith     }
37073a31b7ebSMike Smith }
37083a31b7ebSMike Smith 
37093a31b7ebSMike Smith /************************************************************************
37103a31b7ebSMike Smith  * Handle a notify event relating to the status of a physical drive.
37113a31b7ebSMike Smith  */
37123a31b7ebSMike Smith static void
37133a31b7ebSMike Smith ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn)
37143a31b7ebSMike Smith {
3715c6131460SPaul Saab }
37163a31b7ebSMike Smith 
3717c6131460SPaul Saab /************************************************************************
37181fe6c4eeSScott Long  * Handle a notify event relating to the status of a physical drive.
37191fe6c4eeSScott Long  */
37201fe6c4eeSScott Long static void
37211fe6c4eeSScott Long ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn)
37221fe6c4eeSScott Long {
3723c1885ab8SPaul Saab     struct ciss_lun_report *cll = NULL;
37241fe6c4eeSScott Long     int bus, target;
37251fe6c4eeSScott Long     int s;
37261fe6c4eeSScott Long 
37271fe6c4eeSScott Long     switch (cn->subclass) {
37281fe6c4eeSScott Long     case CISS_NOTIFY_HOTPLUG_PHYSICAL:
37291fe6c4eeSScott Long     case CISS_NOTIFY_HOTPLUG_NONDISK:
37301fe6c4eeSScott Long 	bus = CISS_BIG_MAP_BUS(sc, cn->data.drive.big_physical_drive_number);
37311fe6c4eeSScott Long 	target =
37321fe6c4eeSScott Long 	    CISS_BIG_MAP_TARGET(sc, cn->data.drive.big_physical_drive_number);
37331fe6c4eeSScott Long 
37341fe6c4eeSScott Long 	s = splcam();
37351fe6c4eeSScott Long 	if (cn->detail == 0) {
37361fe6c4eeSScott Long 	    /*
37371fe6c4eeSScott Long 	     * Mark the device offline so that it'll start producing selection
37381fe6c4eeSScott Long 	     * timeouts to the upper layer.
37391fe6c4eeSScott Long 	     */
37405f265711SScott Long 	    if ((bus >= 0) && (target >= 0))
37411fe6c4eeSScott Long 		sc->ciss_physical[bus][target].cp_online = 0;
37421fe6c4eeSScott Long 	} else {
37431fe6c4eeSScott Long 	    /*
37441fe6c4eeSScott Long 	     * Rescan the physical lun list for new items
37451fe6c4eeSScott Long 	     */
37461fe6c4eeSScott Long 	    cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
37471fe6c4eeSScott Long 				   CISS_MAX_PHYSICAL);
37481fe6c4eeSScott Long 	    if (cll == NULL) {
37491fe6c4eeSScott Long 		ciss_printf(sc, "Warning, cannot get physical lun list\n");
37501fe6c4eeSScott Long 		break;
37511fe6c4eeSScott Long 	    }
37521fe6c4eeSScott Long 	    ciss_filter_physical(sc, cll);
37531fe6c4eeSScott Long 	}
37541fe6c4eeSScott Long 	splx(s);
37551fe6c4eeSScott Long 	break;
37561fe6c4eeSScott Long 
37571fe6c4eeSScott Long     default:
37581fe6c4eeSScott Long 	ciss_printf(sc, "Unknown hotplug event %d\n", cn->subclass);
37591fe6c4eeSScott Long 	return;
37601fe6c4eeSScott Long     }
3761c1885ab8SPaul Saab 
3762c1885ab8SPaul Saab     if (cll != NULL)
3763c1885ab8SPaul Saab 	free(cll, CISS_MALLOC_CLASS);
37641fe6c4eeSScott Long }
37651fe6c4eeSScott Long 
37661fe6c4eeSScott Long /************************************************************************
3767c6131460SPaul Saab  * Handle deferred processing of notify events.  Notify events may need
3768c6131460SPaul Saab  * sleep which is unsafe during an interrupt.
3769c6131460SPaul Saab  */
3770c6131460SPaul Saab static void
3771c6131460SPaul Saab ciss_notify_thread(void *arg)
3772c6131460SPaul Saab {
3773c6131460SPaul Saab     struct ciss_softc		*sc;
3774c6131460SPaul Saab     struct ciss_request		*cr;
3775c6131460SPaul Saab     struct ciss_notify		*cn;
3776c6131460SPaul Saab     int				s;
3777c6131460SPaul Saab 
3778c6131460SPaul Saab     sc = (struct ciss_softc *)arg;
3779975b7318SScott Long #if __FreeBSD_version >= 500000
3780975b7318SScott Long     mtx_lock(&sc->ciss_mtx);
3781975b7318SScott Long #endif
3782c6131460SPaul Saab 
3783c6131460SPaul Saab     s = splcam();
3784c6131460SPaul Saab     for (;;) {
3785c6131460SPaul Saab 	if (TAILQ_EMPTY(&sc->ciss_notify) != 0 &&
3786c6131460SPaul Saab 	    (sc->ciss_flags & CISS_FLAG_THREAD_SHUT) == 0) {
3787975b7318SScott Long 	    msleep(&sc->ciss_notify, &sc->ciss_mtx, PUSER, "idle", 0);
3788c6131460SPaul Saab 	}
3789c6131460SPaul Saab 
3790c6131460SPaul Saab 	if (sc->ciss_flags & CISS_FLAG_THREAD_SHUT)
3791c6131460SPaul Saab 	    break;
3792c6131460SPaul Saab 
3793c6131460SPaul Saab 	cr = ciss_dequeue_notify(sc);
3794c6131460SPaul Saab 	splx(s);
3795c6131460SPaul Saab 
3796c6131460SPaul Saab 	if (cr == NULL)
3797c6131460SPaul Saab 		panic("cr null");
3798c6131460SPaul Saab 	cn = (struct ciss_notify *)cr->cr_data;
3799c6131460SPaul Saab 
3800c6131460SPaul Saab 	switch (cn->class) {
38011fe6c4eeSScott Long 	case CISS_NOTIFY_HOTPLUG:
38021fe6c4eeSScott Long 	    ciss_notify_hotplug(sc, cn);
38031fe6c4eeSScott Long 	    break;
3804c6131460SPaul Saab 	case CISS_NOTIFY_LOGICAL:
3805c6131460SPaul Saab 	    ciss_notify_logical(sc, cn);
3806c6131460SPaul Saab 	    break;
3807c6131460SPaul Saab 	case CISS_NOTIFY_PHYSICAL:
3808c6131460SPaul Saab 	    ciss_notify_physical(sc, cn);
3809c6131460SPaul Saab 	    break;
3810c6131460SPaul Saab 	}
3811c6131460SPaul Saab 
3812c6131460SPaul Saab 	ciss_release_request(cr);
3813c6131460SPaul Saab 
3814c6131460SPaul Saab 	s = splcam();
3815c6131460SPaul Saab     }
3816c6131460SPaul Saab     sc->ciss_notify_thread = NULL;
3817c6131460SPaul Saab     wakeup(&sc->ciss_notify_thread);
3818c6131460SPaul Saab     splx(s);
3819c6131460SPaul Saab 
3820c6131460SPaul Saab #if __FreeBSD_version >= 500000
3821975b7318SScott Long     mtx_unlock(&sc->ciss_mtx);
3822c6131460SPaul Saab #endif
38233745c395SJulian Elischer     kproc_exit(0);
3824c6131460SPaul Saab }
3825c6131460SPaul Saab 
3826c6131460SPaul Saab /************************************************************************
3827c6131460SPaul Saab  * Start the notification kernel thread.
3828c6131460SPaul Saab  */
3829c6131460SPaul Saab static void
3830c6131460SPaul Saab ciss_spawn_notify_thread(struct ciss_softc *sc)
3831c6131460SPaul Saab {
3832c6131460SPaul Saab 
383378d03361SPaul Saab #if __FreeBSD_version > 500005
38343745c395SJulian Elischer     if (kproc_create((void(*)(void *))ciss_notify_thread, sc,
3835c6131460SPaul Saab 		       &sc->ciss_notify_thread, 0, 0, "ciss_notify%d",
3836c6131460SPaul Saab 		       device_get_unit(sc->ciss_dev)))
383778d03361SPaul Saab #else
38383745c395SJulian Elischer     if (kproc_create((void(*)(void *))ciss_notify_thread, sc,
383978d03361SPaul Saab 		       &sc->ciss_notify_thread, "ciss_notify%d",
384078d03361SPaul Saab 		       device_get_unit(sc->ciss_dev)))
384178d03361SPaul Saab #endif
3842c6131460SPaul Saab 	panic("Could not create notify thread\n");
3843c6131460SPaul Saab }
3844c6131460SPaul Saab 
3845c6131460SPaul Saab /************************************************************************
3846c6131460SPaul Saab  * Kill the notification kernel thread.
3847c6131460SPaul Saab  */
3848c6131460SPaul Saab static void
3849c6131460SPaul Saab ciss_kill_notify_thread(struct ciss_softc *sc)
3850c6131460SPaul Saab {
3851c6131460SPaul Saab 
3852c6131460SPaul Saab     if (sc->ciss_notify_thread == NULL)
3853c6131460SPaul Saab 	return;
3854c6131460SPaul Saab 
3855c6131460SPaul Saab     sc->ciss_flags |= CISS_FLAG_THREAD_SHUT;
3856c6131460SPaul Saab     wakeup(&sc->ciss_notify);
3857975b7318SScott Long     msleep(&sc->ciss_notify_thread, &sc->ciss_mtx, PUSER, "thtrm", 0);
38583a31b7ebSMike Smith }
38593a31b7ebSMike Smith 
38603a31b7ebSMike Smith /************************************************************************
38613a31b7ebSMike Smith  * Print a request.
38623a31b7ebSMike Smith  */
38633a31b7ebSMike Smith static void
38643a31b7ebSMike Smith ciss_print_request(struct ciss_request *cr)
38653a31b7ebSMike Smith {
38663a31b7ebSMike Smith     struct ciss_softc	*sc;
38673a31b7ebSMike Smith     struct ciss_command	*cc;
38683a31b7ebSMike Smith     int			i;
38693a31b7ebSMike Smith 
38703a31b7ebSMike Smith     sc = cr->cr_sc;
38713a31b7ebSMike Smith     cc = CISS_FIND_COMMAND(cr);
38723a31b7ebSMike Smith 
38733a31b7ebSMike Smith     ciss_printf(sc, "REQUEST @ %p\n", cr);
38743a31b7ebSMike Smith     ciss_printf(sc, "  data %p/%d  tag %d  flags %b\n",
38753a31b7ebSMike Smith 	      cr->cr_data, cr->cr_length, cr->cr_tag, cr->cr_flags,
38763a31b7ebSMike Smith 	      "\20\1mapped\2sleep\3poll\4dataout\5datain\n");
38773a31b7ebSMike Smith     ciss_printf(sc, "  sg list/total %d/%d  host tag 0x%x\n",
38783a31b7ebSMike Smith 		cc->header.sg_in_list, cc->header.sg_total, cc->header.host_tag);
38793a31b7ebSMike Smith     switch(cc->header.address.mode.mode) {
38803a31b7ebSMike Smith     case CISS_HDR_ADDRESS_MODE_PERIPHERAL:
38813a31b7ebSMike Smith     case CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL:
38823a31b7ebSMike Smith 	ciss_printf(sc, "  physical bus %d target %d\n",
38833a31b7ebSMike Smith 		    cc->header.address.physical.bus, cc->header.address.physical.target);
38843a31b7ebSMike Smith 	break;
38853a31b7ebSMike Smith     case CISS_HDR_ADDRESS_MODE_LOGICAL:
38863a31b7ebSMike Smith 	ciss_printf(sc, "  logical unit %d\n", cc->header.address.logical.lun);
38873a31b7ebSMike Smith 	break;
38883a31b7ebSMike Smith     }
38893a31b7ebSMike Smith     ciss_printf(sc, "  %s cdb length %d type %s attribute %s\n",
38903a31b7ebSMike Smith 		(cc->cdb.direction == CISS_CDB_DIRECTION_NONE) ? "no-I/O" :
38913a31b7ebSMike Smith 		(cc->cdb.direction == CISS_CDB_DIRECTION_READ) ? "READ" :
38923a31b7ebSMike Smith 		(cc->cdb.direction == CISS_CDB_DIRECTION_WRITE) ? "WRITE" : "??",
38933a31b7ebSMike Smith 		cc->cdb.cdb_length,
38943a31b7ebSMike Smith 		(cc->cdb.type == CISS_CDB_TYPE_COMMAND) ? "command" :
38953a31b7ebSMike Smith 		(cc->cdb.type == CISS_CDB_TYPE_MESSAGE) ? "message" : "??",
38963a31b7ebSMike Smith 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_UNTAGGED) ? "untagged" :
38973a31b7ebSMike Smith 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_SIMPLE) ? "simple" :
38983a31b7ebSMike Smith 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_HEAD_OF_QUEUE) ? "head-of-queue" :
38993a31b7ebSMike Smith 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_ORDERED) ? "ordered" :
39003a31b7ebSMike Smith 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_AUTO_CONTINGENT) ? "auto-contingent" : "??");
39013a31b7ebSMike Smith     ciss_printf(sc, "  %*D\n", cc->cdb.cdb_length, &cc->cdb.cdb[0], " ");
39023a31b7ebSMike Smith 
39033a31b7ebSMike Smith     if (cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) {
39043a31b7ebSMike Smith 	/* XXX print error info */
39053a31b7ebSMike Smith     } else {
39063a31b7ebSMike Smith 	/* since we don't use chained s/g, don't support it here */
39073a31b7ebSMike Smith 	for (i = 0; i < cc->header.sg_in_list; i++) {
39083a31b7ebSMike Smith 	    if ((i % 4) == 0)
39093a31b7ebSMike Smith 		ciss_printf(sc, "   ");
39103a31b7ebSMike Smith 	    printf("0x%08x/%d ", (u_int32_t)cc->sg[i].address, cc->sg[i].length);
39113a31b7ebSMike Smith 	    if ((((i + 1) % 4) == 0) || (i == (cc->header.sg_in_list - 1)))
39123a31b7ebSMike Smith 		printf("\n");
39133a31b7ebSMike Smith 	}
39143a31b7ebSMike Smith     }
39153a31b7ebSMike Smith }
39163a31b7ebSMike Smith 
39173a31b7ebSMike Smith /************************************************************************
39183a31b7ebSMike Smith  * Print information about the status of a logical drive.
39193a31b7ebSMike Smith  */
39203a31b7ebSMike Smith static void
39213a31b7ebSMike Smith ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld)
39223a31b7ebSMike Smith {
39233a31b7ebSMike Smith     int		bus, target, i;
39243a31b7ebSMike Smith 
3925d4aa427fSPaul Saab     if (ld->cl_lstatus == NULL) {
3926d4aa427fSPaul Saab 	printf("does not exist\n");
3927d4aa427fSPaul Saab 	return;
3928d4aa427fSPaul Saab     }
3929d4aa427fSPaul Saab 
39303a31b7ebSMike Smith     /* print drive status */
39313a31b7ebSMike Smith     switch(ld->cl_lstatus->status) {
39323a31b7ebSMike Smith     case CISS_LSTATUS_OK:
39333a31b7ebSMike Smith 	printf("online\n");
39343a31b7ebSMike Smith 	break;
39353a31b7ebSMike Smith     case CISS_LSTATUS_INTERIM_RECOVERY:
39363a31b7ebSMike Smith 	printf("in interim recovery mode\n");
39373a31b7ebSMike Smith 	break;
39383a31b7ebSMike Smith     case CISS_LSTATUS_READY_RECOVERY:
39393a31b7ebSMike Smith 	printf("ready to begin recovery\n");
39403a31b7ebSMike Smith 	break;
39413a31b7ebSMike Smith     case CISS_LSTATUS_RECOVERING:
39423a31b7ebSMike Smith 	bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
39433a31b7ebSMike Smith 	target = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
39443a31b7ebSMike Smith 	printf("being recovered, working on physical drive %d.%d, %u blocks remaining\n",
39453a31b7ebSMike Smith 	       bus, target, ld->cl_lstatus->blocks_to_recover);
39463a31b7ebSMike Smith 	break;
39473a31b7ebSMike Smith     case CISS_LSTATUS_EXPANDING:
39483a31b7ebSMike Smith 	printf("being expanded, %u blocks remaining\n",
39493a31b7ebSMike Smith 	       ld->cl_lstatus->blocks_to_recover);
39503a31b7ebSMike Smith 	break;
39513a31b7ebSMike Smith     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
39523a31b7ebSMike Smith 	printf("queued for expansion\n");
39533a31b7ebSMike Smith 	break;
39543a31b7ebSMike Smith     case CISS_LSTATUS_FAILED:
39553a31b7ebSMike Smith 	printf("queued for expansion\n");
39563a31b7ebSMike Smith 	break;
39573a31b7ebSMike Smith     case CISS_LSTATUS_WRONG_PDRIVE:
39583a31b7ebSMike Smith 	printf("wrong physical drive inserted\n");
39593a31b7ebSMike Smith 	break;
39603a31b7ebSMike Smith     case CISS_LSTATUS_MISSING_PDRIVE:
39613a31b7ebSMike Smith 	printf("missing a needed physical drive\n");
39623a31b7ebSMike Smith 	break;
39633a31b7ebSMike Smith     case CISS_LSTATUS_BECOMING_READY:
39643a31b7ebSMike Smith 	printf("becoming ready\n");
39653a31b7ebSMike Smith 	break;
39663a31b7ebSMike Smith     }
39673a31b7ebSMike Smith 
3968d4aa427fSPaul Saab     /* print failed physical drives */
39693a31b7ebSMike Smith     for (i = 0; i < CISS_BIG_MAP_ENTRIES / 8; i++) {
39703a31b7ebSMike Smith 	bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_failure_map[i]);
39713a31b7ebSMike Smith 	target = CISS_BIG_MAP_TARGET(sc, ld->cl_lstatus->drive_failure_map[i]);
39723a31b7ebSMike Smith 	if (bus == -1)
39733a31b7ebSMike Smith 	    continue;
39743a31b7ebSMike Smith 	ciss_printf(sc, "physical drive %d:%d (%x) failed\n", bus, target,
39753a31b7ebSMike Smith 		    ld->cl_lstatus->drive_failure_map[i]);
39763a31b7ebSMike Smith     }
39773a31b7ebSMike Smith }
39783a31b7ebSMike Smith 
3979d4aa427fSPaul Saab #ifdef CISS_DEBUG
3980d4aa427fSPaul Saab /************************************************************************
3981d4aa427fSPaul Saab  * Print information about the controller/driver.
3982d4aa427fSPaul Saab  */
3983d4aa427fSPaul Saab static void
3984d4aa427fSPaul Saab ciss_print_adapter(struct ciss_softc *sc)
3985d4aa427fSPaul Saab {
3986609caf8dSPaul Saab     int		i, j;
3987d4aa427fSPaul Saab 
3988d4aa427fSPaul Saab     ciss_printf(sc, "ADAPTER:\n");
3989d4aa427fSPaul Saab     for (i = 0; i < CISSQ_COUNT; i++) {
3990d4aa427fSPaul Saab 	ciss_printf(sc, "%s     %d/%d\n",
3991d4aa427fSPaul Saab 	    i == 0 ? "free" :
3992d4aa427fSPaul Saab 	    i == 1 ? "busy" : "complete",
3993d4aa427fSPaul Saab 	    sc->ciss_qstat[i].q_length,
3994d4aa427fSPaul Saab 	    sc->ciss_qstat[i].q_max);
3995d4aa427fSPaul Saab     }
3996d4aa427fSPaul Saab     ciss_printf(sc, "max_requests %d\n", sc->ciss_max_requests);
3997d4aa427fSPaul Saab     ciss_printf(sc, "flags %b\n", sc->ciss_flags,
3998d4aa427fSPaul Saab 	"\20\1notify_ok\2control_open\3aborting\4running\21fake_synch\22bmic_abort\n");
3999d4aa427fSPaul Saab 
40001fe6c4eeSScott Long     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
4001609caf8dSPaul Saab 	for (j = 0; j < CISS_MAX_LOGICAL; j++) {
4002d4aa427fSPaul Saab 	    ciss_printf(sc, "LOGICAL DRIVE %d:  ", i);
4003609caf8dSPaul Saab 	    ciss_print_ldrive(sc, &sc->ciss_logical[i][j]);
4004609caf8dSPaul Saab 	}
4005d4aa427fSPaul Saab     }
4006d4aa427fSPaul Saab 
40071fe6c4eeSScott Long     /* XXX Should physical drives be printed out here? */
40081fe6c4eeSScott Long 
4009d4aa427fSPaul Saab     for (i = 1; i < sc->ciss_max_requests; i++)
4010d4aa427fSPaul Saab 	ciss_print_request(sc->ciss_request + i);
4011d4aa427fSPaul Saab }
4012d4aa427fSPaul Saab 
4013d4aa427fSPaul Saab /* DDB hook */
4014e6fccf7aSMaxime Henrion static void
4015d4aa427fSPaul Saab ciss_print0(void)
4016d4aa427fSPaul Saab {
4017d4aa427fSPaul Saab     struct ciss_softc	*sc;
4018d4aa427fSPaul Saab 
4019d4aa427fSPaul Saab     sc = devclass_get_softc(devclass_find("ciss"), 0);
4020d4aa427fSPaul Saab     if (sc == NULL) {
4021d4aa427fSPaul Saab 	printf("no ciss controllers\n");
4022d4aa427fSPaul Saab     } else {
4023d4aa427fSPaul Saab 	ciss_print_adapter(sc);
4024d4aa427fSPaul Saab     }
4025d4aa427fSPaul Saab }
4026d4aa427fSPaul Saab #endif
4027d4aa427fSPaul Saab 
40283a31b7ebSMike Smith /************************************************************************
40293a31b7ebSMike Smith  * Return a name for a logical drive status value.
40303a31b7ebSMike Smith  */
40313a31b7ebSMike Smith static const char *
40323a31b7ebSMike Smith ciss_name_ldrive_status(int status)
40333a31b7ebSMike Smith {
40343a31b7ebSMike Smith     switch (status) {
40353a31b7ebSMike Smith     case CISS_LSTATUS_OK:
40363a31b7ebSMike Smith 	return("OK");
40373a31b7ebSMike Smith     case CISS_LSTATUS_FAILED:
40383a31b7ebSMike Smith 	return("failed");
40393a31b7ebSMike Smith     case CISS_LSTATUS_NOT_CONFIGURED:
40403a31b7ebSMike Smith 	return("not configured");
40413a31b7ebSMike Smith     case CISS_LSTATUS_INTERIM_RECOVERY:
40423a31b7ebSMike Smith 	return("interim recovery");
40433a31b7ebSMike Smith     case CISS_LSTATUS_READY_RECOVERY:
40443a31b7ebSMike Smith 	return("ready for recovery");
40453a31b7ebSMike Smith     case CISS_LSTATUS_RECOVERING:
40463a31b7ebSMike Smith 	return("recovering");
40473a31b7ebSMike Smith     case CISS_LSTATUS_WRONG_PDRIVE:
40483a31b7ebSMike Smith 	return("wrong physical drive inserted");
40493a31b7ebSMike Smith     case CISS_LSTATUS_MISSING_PDRIVE:
40503a31b7ebSMike Smith 	return("missing physical drive");
40513a31b7ebSMike Smith     case CISS_LSTATUS_EXPANDING:
40523a31b7ebSMike Smith 	return("expanding");
40533a31b7ebSMike Smith     case CISS_LSTATUS_BECOMING_READY:
40543a31b7ebSMike Smith 	return("becoming ready");
40553a31b7ebSMike Smith     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
40563a31b7ebSMike Smith 	return("queued for expansion");
40573a31b7ebSMike Smith     }
40583a31b7ebSMike Smith     return("unknown status");
40593a31b7ebSMike Smith }
40603a31b7ebSMike Smith 
40613a31b7ebSMike Smith /************************************************************************
40623a31b7ebSMike Smith  * Return an online/offline/nonexistent value for a logical drive
40633a31b7ebSMike Smith  * status value.
40643a31b7ebSMike Smith  */
40653a31b7ebSMike Smith static int
40663a31b7ebSMike Smith ciss_decode_ldrive_status(int status)
40673a31b7ebSMike Smith {
40683a31b7ebSMike Smith     switch(status) {
40693a31b7ebSMike Smith     case CISS_LSTATUS_NOT_CONFIGURED:
40703a31b7ebSMike Smith 	return(CISS_LD_NONEXISTENT);
40713a31b7ebSMike Smith 
40723a31b7ebSMike Smith     case CISS_LSTATUS_OK:
40733a31b7ebSMike Smith     case CISS_LSTATUS_INTERIM_RECOVERY:
40743a31b7ebSMike Smith     case CISS_LSTATUS_READY_RECOVERY:
40753a31b7ebSMike Smith     case CISS_LSTATUS_RECOVERING:
40763a31b7ebSMike Smith     case CISS_LSTATUS_EXPANDING:
40773a31b7ebSMike Smith     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
40783a31b7ebSMike Smith 	return(CISS_LD_ONLINE);
40793a31b7ebSMike Smith 
40803a31b7ebSMike Smith     case CISS_LSTATUS_FAILED:
40813a31b7ebSMike Smith     case CISS_LSTATUS_WRONG_PDRIVE:
40823a31b7ebSMike Smith     case CISS_LSTATUS_MISSING_PDRIVE:
40833a31b7ebSMike Smith     case CISS_LSTATUS_BECOMING_READY:
40843a31b7ebSMike Smith     default:
40853a31b7ebSMike Smith 	return(CISS_LD_OFFLINE);
40863a31b7ebSMike Smith     }
40873a31b7ebSMike Smith }
40883a31b7ebSMike Smith 
40893a31b7ebSMike Smith 
40903a31b7ebSMike Smith /************************************************************************
40913a31b7ebSMike Smith  * Return a name for a logical drive's organisation.
40923a31b7ebSMike Smith  */
40933a31b7ebSMike Smith static const char *
40943a31b7ebSMike Smith ciss_name_ldrive_org(int org)
40953a31b7ebSMike Smith {
40963a31b7ebSMike Smith     switch(org) {
40973a31b7ebSMike Smith     case CISS_LDRIVE_RAID0:
40983a31b7ebSMike Smith 	return("RAID 0");
40993a31b7ebSMike Smith     case CISS_LDRIVE_RAID1:
41003a31b7ebSMike Smith 	return("RAID 1");
41013a31b7ebSMike Smith     case CISS_LDRIVE_RAID4:
41023a31b7ebSMike Smith 	return("RAID 4");
41033a31b7ebSMike Smith     case CISS_LDRIVE_RAID5:
41043a31b7ebSMike Smith 	return("RAID 5");
4105aaf8327bSPaul Saab     case CISS_LDRIVE_RAID51:
4106aaf8327bSPaul Saab 	return("RAID 5+1");
4107aaf8327bSPaul Saab     case CISS_LDRIVE_RAIDADG:
4108aaf8327bSPaul Saab 	return("RAID ADG");
41093a31b7ebSMike Smith     }
41103a31b7ebSMike Smith     return("unkown");
41113a31b7ebSMike Smith }
41123a31b7ebSMike Smith 
41133a31b7ebSMike Smith /************************************************************************
41143a31b7ebSMike Smith  * Return a name for a command status value.
41153a31b7ebSMike Smith  */
41163a31b7ebSMike Smith static const char *
41173a31b7ebSMike Smith ciss_name_command_status(int status)
41183a31b7ebSMike Smith {
41193a31b7ebSMike Smith     switch(status) {
41203a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:
41213a31b7ebSMike Smith 	return("success");
41223a31b7ebSMike Smith     case CISS_CMD_STATUS_TARGET_STATUS:
41233a31b7ebSMike Smith 	return("target status");
41243a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_UNDERRUN:
41253a31b7ebSMike Smith 	return("data underrun");
41263a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_OVERRUN:
41273a31b7ebSMike Smith 	return("data overrun");
41283a31b7ebSMike Smith     case CISS_CMD_STATUS_INVALID_COMMAND:
41293a31b7ebSMike Smith 	return("invalid command");
41303a31b7ebSMike Smith     case CISS_CMD_STATUS_PROTOCOL_ERROR:
41313a31b7ebSMike Smith 	return("protocol error");
41323a31b7ebSMike Smith     case CISS_CMD_STATUS_HARDWARE_ERROR:
41333a31b7ebSMike Smith 	return("hardware error");
41343a31b7ebSMike Smith     case CISS_CMD_STATUS_CONNECTION_LOST:
41353a31b7ebSMike Smith 	return("connection lost");
41363a31b7ebSMike Smith     case CISS_CMD_STATUS_ABORTED:
41373a31b7ebSMike Smith 	return("aborted");
41383a31b7ebSMike Smith     case CISS_CMD_STATUS_ABORT_FAILED:
41393a31b7ebSMike Smith 	return("abort failed");
41403a31b7ebSMike Smith     case CISS_CMD_STATUS_UNSOLICITED_ABORT:
41413a31b7ebSMike Smith 	return("unsolicited abort");
41423a31b7ebSMike Smith     case CISS_CMD_STATUS_TIMEOUT:
41433a31b7ebSMike Smith 	return("timeout");
41443a31b7ebSMike Smith     case CISS_CMD_STATUS_UNABORTABLE:
41453a31b7ebSMike Smith 	return("unabortable");
41463a31b7ebSMike Smith     }
41473a31b7ebSMike Smith     return("unknown status");
41483a31b7ebSMike Smith }
41493a31b7ebSMike Smith 
41503a31b7ebSMike Smith /************************************************************************
41513a31b7ebSMike Smith  * Handle an open on the control device.
41523a31b7ebSMike Smith  */
41533a31b7ebSMike Smith static int
415489c9c53dSPoul-Henning Kamp ciss_open(struct cdev *dev, int flags, int fmt, d_thread_t *p)
41553a31b7ebSMike Smith {
41563a31b7ebSMike Smith     struct ciss_softc	*sc;
41573a31b7ebSMike Smith 
41583a31b7ebSMike Smith     debug_called(1);
41593a31b7ebSMike Smith 
41603a31b7ebSMike Smith     sc = (struct ciss_softc *)dev->si_drv1;
41613a31b7ebSMike Smith 
41623a31b7ebSMike Smith     /* we might want to veto if someone already has us open */
41633a31b7ebSMike Smith 
4164975b7318SScott Long     mtx_lock(&sc->ciss_mtx);
41653a31b7ebSMike Smith     sc->ciss_flags |= CISS_FLAG_CONTROL_OPEN;
4166975b7318SScott Long     mtx_unlock(&sc->ciss_mtx);
41673a31b7ebSMike Smith     return(0);
41683a31b7ebSMike Smith }
41693a31b7ebSMike Smith 
41703a31b7ebSMike Smith /************************************************************************
41713a31b7ebSMike Smith  * Handle the last close on the control device.
41723a31b7ebSMike Smith  */
41733a31b7ebSMike Smith static int
417489c9c53dSPoul-Henning Kamp ciss_close(struct cdev *dev, int flags, int fmt, d_thread_t *p)
41753a31b7ebSMike Smith {
41763a31b7ebSMike Smith     struct ciss_softc	*sc;
41773a31b7ebSMike Smith 
41783a31b7ebSMike Smith     debug_called(1);
41793a31b7ebSMike Smith 
41803a31b7ebSMike Smith     sc = (struct ciss_softc *)dev->si_drv1;
41813a31b7ebSMike Smith 
4182975b7318SScott Long     mtx_lock(&sc->ciss_mtx);
41833a31b7ebSMike Smith     sc->ciss_flags &= ~CISS_FLAG_CONTROL_OPEN;
4184975b7318SScott Long     mtx_unlock(&sc->ciss_mtx);
41853a31b7ebSMike Smith     return (0);
41863a31b7ebSMike Smith }
41873a31b7ebSMike Smith 
41883a31b7ebSMike Smith /********************************************************************************
41893a31b7ebSMike Smith  * Handle adapter-specific control operations.
41903a31b7ebSMike Smith  *
41913a31b7ebSMike Smith  * Note that the API here is compatible with the Linux driver, in order to
41923a31b7ebSMike Smith  * simplify the porting of Compaq's userland tools.
41933a31b7ebSMike Smith  */
41943a31b7ebSMike Smith static int
419589c9c53dSPoul-Henning Kamp ciss_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag, d_thread_t *p)
41963a31b7ebSMike Smith {
41973a31b7ebSMike Smith     struct ciss_softc		*sc;
41987caeec6aSPaul Saab     IOCTL_Command_struct	*ioc	= (IOCTL_Command_struct *)addr;
41997caeec6aSPaul Saab #ifdef __amd64__
42007caeec6aSPaul Saab     IOCTL_Command_struct32	*ioc32	= (IOCTL_Command_struct32 *)addr;
42017caeec6aSPaul Saab     IOCTL_Command_struct	ioc_swab;
42027caeec6aSPaul Saab #endif
42033a31b7ebSMike Smith     int				error;
42043a31b7ebSMike Smith 
42053a31b7ebSMike Smith     debug_called(1);
42063a31b7ebSMike Smith 
42073a31b7ebSMike Smith     sc = (struct ciss_softc *)dev->si_drv1;
42083a31b7ebSMike Smith     error = 0;
4209975b7318SScott Long     mtx_lock(&sc->ciss_mtx);
42103a31b7ebSMike Smith 
42113a31b7ebSMike Smith     switch(cmd) {
42123a31b7ebSMike Smith     case CCISS_GETPCIINFO:
42133a31b7ebSMike Smith     {
42143a31b7ebSMike Smith 	cciss_pci_info_struct	*pis = (cciss_pci_info_struct *)addr;
42153a31b7ebSMike Smith 
42163a31b7ebSMike Smith 	pis->bus = pci_get_bus(sc->ciss_dev);
42173a31b7ebSMike Smith 	pis->dev_fn = pci_get_slot(sc->ciss_dev);
42183a31b7ebSMike Smith 	pis->board_id = pci_get_devid(sc->ciss_dev);
42193a31b7ebSMike Smith 
42203a31b7ebSMike Smith 	break;
42213a31b7ebSMike Smith     }
42223a31b7ebSMike Smith 
42233a31b7ebSMike Smith     case CCISS_GETINTINFO:
42243a31b7ebSMike Smith     {
42253a31b7ebSMike Smith 	cciss_coalint_struct	*cis = (cciss_coalint_struct *)addr;
42263a31b7ebSMike Smith 
42273a31b7ebSMike Smith 	cis->delay = sc->ciss_cfg->interrupt_coalesce_delay;
42283a31b7ebSMike Smith 	cis->count = sc->ciss_cfg->interrupt_coalesce_count;
42293a31b7ebSMike Smith 
42303a31b7ebSMike Smith 	break;
42313a31b7ebSMike Smith     }
42323a31b7ebSMike Smith 
42333a31b7ebSMike Smith     case CCISS_SETINTINFO:
42343a31b7ebSMike Smith     {
42353a31b7ebSMike Smith 	cciss_coalint_struct	*cis = (cciss_coalint_struct *)addr;
42363a31b7ebSMike Smith 
42373a31b7ebSMike Smith 	if ((cis->delay == 0) && (cis->count == 0)) {
42383a31b7ebSMike Smith 	    error = EINVAL;
42393a31b7ebSMike Smith 	    break;
42403a31b7ebSMike Smith 	}
42413a31b7ebSMike Smith 
42423a31b7ebSMike Smith 	/*
42433a31b7ebSMike Smith 	 * XXX apparently this is only safe if the controller is idle,
42443a31b7ebSMike Smith 	 *     we should suspend it before doing this.
42453a31b7ebSMike Smith 	 */
42463a31b7ebSMike Smith 	sc->ciss_cfg->interrupt_coalesce_delay = cis->delay;
42473a31b7ebSMike Smith 	sc->ciss_cfg->interrupt_coalesce_count = cis->count;
42483a31b7ebSMike Smith 
42493a31b7ebSMike Smith 	if (ciss_update_config(sc))
42503a31b7ebSMike Smith 	    error = EIO;
42513a31b7ebSMike Smith 
42523a31b7ebSMike Smith 	/* XXX resume the controller here */
42533a31b7ebSMike Smith 	break;
42543a31b7ebSMike Smith     }
42553a31b7ebSMike Smith 
42563a31b7ebSMike Smith     case CCISS_GETNODENAME:
42573a31b7ebSMike Smith 	bcopy(sc->ciss_cfg->server_name, (NodeName_type *)addr,
42583a31b7ebSMike Smith 	      sizeof(NodeName_type));
42593a31b7ebSMike Smith 	break;
42603a31b7ebSMike Smith 
42613a31b7ebSMike Smith     case CCISS_SETNODENAME:
42623a31b7ebSMike Smith 	bcopy((NodeName_type *)addr, sc->ciss_cfg->server_name,
42633a31b7ebSMike Smith 	      sizeof(NodeName_type));
42643a31b7ebSMike Smith 	if (ciss_update_config(sc))
42653a31b7ebSMike Smith 	    error = EIO;
42663a31b7ebSMike Smith 	break;
42673a31b7ebSMike Smith 
42683a31b7ebSMike Smith     case CCISS_GETHEARTBEAT:
42693a31b7ebSMike Smith 	*(Heartbeat_type *)addr = sc->ciss_cfg->heartbeat;
42703a31b7ebSMike Smith 	break;
42713a31b7ebSMike Smith 
42723a31b7ebSMike Smith     case CCISS_GETBUSTYPES:
42733a31b7ebSMike Smith 	*(BusTypes_type *)addr = sc->ciss_cfg->bus_types;
42743a31b7ebSMike Smith 	break;
42753a31b7ebSMike Smith 
42763a31b7ebSMike Smith     case CCISS_GETFIRMVER:
42773a31b7ebSMike Smith 	bcopy(sc->ciss_id->running_firmware_revision, (FirmwareVer_type *)addr,
42783a31b7ebSMike Smith 	      sizeof(FirmwareVer_type));
42793a31b7ebSMike Smith 	break;
42803a31b7ebSMike Smith 
42813a31b7ebSMike Smith     case CCISS_GETDRIVERVER:
42823a31b7ebSMike Smith 	*(DriverVer_type *)addr = CISS_DRIVER_VERSION;
42833a31b7ebSMike Smith 	break;
42843a31b7ebSMike Smith 
42853a31b7ebSMike Smith     case CCISS_REVALIDVOLS:
42863a31b7ebSMike Smith 	/*
42873a31b7ebSMike Smith 	 * This is a bit ugly; to do it "right" we really need
42883a31b7ebSMike Smith 	 * to find any disks that have changed, kick CAM off them,
42893a31b7ebSMike Smith 	 * then rescan only these disks.  It'd be nice if they
42903a31b7ebSMike Smith 	 * a) told us which disk(s) they were going to play with,
42913a31b7ebSMike Smith 	 * and b) which ones had arrived. 8(
42923a31b7ebSMike Smith 	 */
42933a31b7ebSMike Smith 	break;
42943a31b7ebSMike Smith 
42957caeec6aSPaul Saab #ifdef __amd64__
42967caeec6aSPaul Saab     case CCISS_PASSTHRU32:
42977caeec6aSPaul Saab 	ioc_swab.LUN_info	= ioc32->LUN_info;
42987caeec6aSPaul Saab 	ioc_swab.Request	= ioc32->Request;
42997caeec6aSPaul Saab 	ioc_swab.error_info	= ioc32->error_info;
43007caeec6aSPaul Saab 	ioc_swab.buf_size	= ioc32->buf_size;
43017caeec6aSPaul Saab 	ioc_swab.buf		= (u_int8_t *)(uintptr_t)ioc32->buf;
43027caeec6aSPaul Saab 	ioc			= &ioc_swab;
43037caeec6aSPaul Saab 	/* FALLTHROUGH */
43047caeec6aSPaul Saab #endif
43057caeec6aSPaul Saab 
43063a31b7ebSMike Smith     case CCISS_PASSTHRU:
43077caeec6aSPaul Saab 	error = ciss_user_command(sc, ioc);
43083a31b7ebSMike Smith 	break;
43093a31b7ebSMike Smith 
43103a31b7ebSMike Smith     default:
43113a31b7ebSMike Smith 	debug(0, "unknown ioctl 0x%lx", cmd);
43123a31b7ebSMike Smith 
43133a31b7ebSMike Smith 	debug(1, "CCISS_GETPCIINFO:   0x%lx", CCISS_GETPCIINFO);
43143a31b7ebSMike Smith 	debug(1, "CCISS_GETINTINFO:   0x%lx", CCISS_GETINTINFO);
43153a31b7ebSMike Smith 	debug(1, "CCISS_SETINTINFO:   0x%lx", CCISS_SETINTINFO);
43163a31b7ebSMike Smith 	debug(1, "CCISS_GETNODENAME:  0x%lx", CCISS_GETNODENAME);
43173a31b7ebSMike Smith 	debug(1, "CCISS_SETNODENAME:  0x%lx", CCISS_SETNODENAME);
43183a31b7ebSMike Smith 	debug(1, "CCISS_GETHEARTBEAT: 0x%lx", CCISS_GETHEARTBEAT);
43193a31b7ebSMike Smith 	debug(1, "CCISS_GETBUSTYPES:  0x%lx", CCISS_GETBUSTYPES);
43203a31b7ebSMike Smith 	debug(1, "CCISS_GETFIRMVER:   0x%lx", CCISS_GETFIRMVER);
43213a31b7ebSMike Smith 	debug(1, "CCISS_GETDRIVERVER: 0x%lx", CCISS_GETDRIVERVER);
43223a31b7ebSMike Smith 	debug(1, "CCISS_REVALIDVOLS:  0x%lx", CCISS_REVALIDVOLS);
43233a31b7ebSMike Smith 	debug(1, "CCISS_PASSTHRU:     0x%lx", CCISS_PASSTHRU);
43243a31b7ebSMike Smith 
43253a31b7ebSMike Smith 	error = ENOIOCTL;
43263a31b7ebSMike Smith 	break;
43273a31b7ebSMike Smith     }
43283a31b7ebSMike Smith 
4329975b7318SScott Long     mtx_unlock(&sc->ciss_mtx);
43303a31b7ebSMike Smith     return(error);
43313a31b7ebSMike Smith }
4332