xref: /freebsd/sys/dev/ciss/ciss.c (revision 57ae362c8f4dbf64535f80c3aa5f77daa80bf62b)
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/cissio.h>
10622657ce1SScott Long #include <dev/ciss/cissvar.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);
11922657ce1SScott Long static int	ciss_setup_msix(struct ciss_softc *sc);
12022657ce1SScott Long static int	ciss_init_perf(struct ciss_softc *sc);
1213a31b7ebSMike Smith static int	ciss_wait_adapter(struct ciss_softc *sc);
1223a31b7ebSMike Smith static int	ciss_flush_adapter(struct ciss_softc *sc);
1233a31b7ebSMike Smith static int	ciss_init_requests(struct ciss_softc *sc);
1243a31b7ebSMike Smith static void	ciss_command_map_helper(void *arg, bus_dma_segment_t *segs,
1253a31b7ebSMike Smith 					int nseg, int error);
1263a31b7ebSMike Smith static int	ciss_identify_adapter(struct ciss_softc *sc);
1273a31b7ebSMike Smith static int	ciss_init_logical(struct ciss_softc *sc);
128c6131460SPaul Saab static int	ciss_init_physical(struct ciss_softc *sc);
1291fe6c4eeSScott Long static int	ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll);
1303a31b7ebSMike Smith static int	ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld);
1313a31b7ebSMike Smith static int	ciss_get_ldrive_status(struct ciss_softc *sc,  struct ciss_ldrive *ld);
1323a31b7ebSMike Smith static int	ciss_update_config(struct ciss_softc *sc);
13388c78a38SPaul Saab static int	ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld);
13417c0792dSPaul Saab static void	ciss_init_sysctl(struct ciss_softc *sc);
13517c0792dSPaul Saab static void	ciss_soft_reset(struct ciss_softc *sc);
1363a31b7ebSMike Smith static void	ciss_free(struct ciss_softc *sc);
137c6131460SPaul Saab static void	ciss_spawn_notify_thread(struct ciss_softc *sc);
138c6131460SPaul Saab static void	ciss_kill_notify_thread(struct ciss_softc *sc);
1393a31b7ebSMike Smith 
1403a31b7ebSMike Smith /* request submission/completion */
1413a31b7ebSMike Smith static int	ciss_start(struct ciss_request *cr);
14222657ce1SScott Long static void	ciss_done(struct ciss_softc *sc, cr_qhead_t *qh);
14322657ce1SScott Long static void	ciss_perf_done(struct ciss_softc *sc, cr_qhead_t *qh);
1443a31b7ebSMike Smith static void	ciss_intr(void *arg);
14522657ce1SScott Long static void	ciss_perf_intr(void *arg);
14622657ce1SScott Long static void	ciss_perf_msi_intr(void *arg);
14722657ce1SScott Long static void	ciss_complete(struct ciss_softc *sc, cr_qhead_t *qh);
14822657ce1SScott Long static int	_ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status, const char *func);
1493a31b7ebSMike Smith static int	ciss_synch_request(struct ciss_request *cr, int timeout);
1503a31b7ebSMike Smith static int	ciss_poll_request(struct ciss_request *cr, int timeout);
1513a31b7ebSMike Smith static int	ciss_wait_request(struct ciss_request *cr, int timeout);
1526197ca15SPeter Wemm #if 0
1533a31b7ebSMike Smith static int	ciss_abort_request(struct ciss_request *cr);
1546197ca15SPeter Wemm #endif
1553a31b7ebSMike Smith 
1563a31b7ebSMike Smith /* request queueing */
1573a31b7ebSMike Smith static int	ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp);
1583a31b7ebSMike Smith static void	ciss_preen_command(struct ciss_request *cr);
1593a31b7ebSMike Smith static void 	ciss_release_request(struct ciss_request *cr);
1603a31b7ebSMike Smith 
1613a31b7ebSMike Smith /* request helpers */
1623a31b7ebSMike Smith static int	ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
1633a31b7ebSMike Smith 				      int opcode, void **bufp, size_t bufsize);
1643a31b7ebSMike Smith static int	ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc);
1653a31b7ebSMike Smith 
1663a31b7ebSMike Smith /* DMA map/unmap */
1673a31b7ebSMike Smith static int	ciss_map_request(struct ciss_request *cr);
1683a31b7ebSMike Smith static void	ciss_request_map_helper(void *arg, bus_dma_segment_t *segs,
1693a31b7ebSMike Smith 					int nseg, int error);
1703a31b7ebSMike Smith static void	ciss_unmap_request(struct ciss_request *cr);
1713a31b7ebSMike Smith 
1723a31b7ebSMike Smith /* CAM interface */
1733a31b7ebSMike Smith static int	ciss_cam_init(struct ciss_softc *sc);
174c6131460SPaul Saab static void	ciss_cam_rescan_target(struct ciss_softc *sc,
175c6131460SPaul Saab 				       int bus, int target);
1763a31b7ebSMike Smith static void	ciss_cam_action(struct cam_sim *sim, union ccb *ccb);
1773a31b7ebSMike Smith static int	ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio);
1783a31b7ebSMike Smith static int	ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio);
1793a31b7ebSMike Smith static void	ciss_cam_poll(struct cam_sim *sim);
1803a31b7ebSMike Smith static void	ciss_cam_complete(struct ciss_request *cr);
1813a31b7ebSMike Smith static void	ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio);
182c6131460SPaul Saab static struct cam_periph *ciss_find_periph(struct ciss_softc *sc,
183c6131460SPaul Saab 					   int bus, int target);
184c6131460SPaul Saab static int	ciss_name_device(struct ciss_softc *sc, int bus, int target);
1853a31b7ebSMike Smith 
1863a31b7ebSMike Smith /* periodic status monitoring */
1873a31b7ebSMike Smith static void	ciss_periodic(void *arg);
18854d02e0aSMitsuru IWASAKI static void	ciss_nop_complete(struct ciss_request *cr);
189e08d902aSMitsuru IWASAKI static void	ciss_disable_adapter(struct ciss_softc *sc);
1903a31b7ebSMike Smith static void	ciss_notify_event(struct ciss_softc *sc);
1913a31b7ebSMike Smith static void	ciss_notify_complete(struct ciss_request *cr);
1923a31b7ebSMike Smith static int	ciss_notify_abort(struct ciss_softc *sc);
1933a31b7ebSMike Smith static int	ciss_notify_abort_bmic(struct ciss_softc *sc);
1941fe6c4eeSScott Long static void	ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn);
1953a31b7ebSMike Smith static void	ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn);
1963a31b7ebSMike Smith static void	ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn);
1973a31b7ebSMike Smith 
1983a31b7ebSMike Smith /* debugging output */
1993a31b7ebSMike Smith static void	ciss_print_request(struct ciss_request *cr);
2003a31b7ebSMike Smith static void	ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld);
2013a31b7ebSMike Smith static const char *ciss_name_ldrive_status(int status);
2023a31b7ebSMike Smith static int	ciss_decode_ldrive_status(int status);
2033a31b7ebSMike Smith static const char *ciss_name_ldrive_org(int org);
2043a31b7ebSMike Smith static const char *ciss_name_command_status(int status);
2053a31b7ebSMike Smith 
2063a31b7ebSMike Smith /*
2073a31b7ebSMike Smith  * PCI bus interface.
2083a31b7ebSMike Smith  */
2093a31b7ebSMike Smith static device_method_t ciss_methods[] = {
2103a31b7ebSMike Smith     /* Device interface */
2113a31b7ebSMike Smith     DEVMETHOD(device_probe,	ciss_probe),
2123a31b7ebSMike Smith     DEVMETHOD(device_attach,	ciss_attach),
2133a31b7ebSMike Smith     DEVMETHOD(device_detach,	ciss_detach),
2143a31b7ebSMike Smith     DEVMETHOD(device_shutdown,	ciss_shutdown),
2153a31b7ebSMike Smith     { 0, 0 }
2163a31b7ebSMike Smith };
2173a31b7ebSMike Smith 
2183a31b7ebSMike Smith static driver_t ciss_pci_driver = {
2193a31b7ebSMike Smith     "ciss",
2203a31b7ebSMike Smith     ciss_methods,
2213a31b7ebSMike Smith     sizeof(struct ciss_softc)
2223a31b7ebSMike Smith };
2233a31b7ebSMike Smith 
2243a31b7ebSMike Smith static devclass_t	ciss_devclass;
2253a31b7ebSMike Smith DRIVER_MODULE(ciss, pci, ciss_pci_driver, ciss_devclass, 0, 0);
22691d7721cSMaxim Konovalov MODULE_DEPEND(ciss, cam, 1, 1, 1);
22791d7721cSMaxim Konovalov MODULE_DEPEND(ciss, pci, 1, 1, 1);
2283a31b7ebSMike Smith 
2293a31b7ebSMike Smith /*
2303a31b7ebSMike Smith  * Control device interface.
2313a31b7ebSMike Smith  */
2323a31b7ebSMike Smith static d_open_t		ciss_open;
2333a31b7ebSMike Smith static d_close_t	ciss_close;
2343a31b7ebSMike Smith static d_ioctl_t	ciss_ioctl;
2353a31b7ebSMike Smith 
2363a31b7ebSMike Smith static struct cdevsw ciss_cdevsw = {
237dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
238975b7318SScott Long 	.d_flags =	0,
2397ac40f5fSPoul-Henning Kamp 	.d_open =	ciss_open,
2407ac40f5fSPoul-Henning Kamp 	.d_close =	ciss_close,
2417ac40f5fSPoul-Henning Kamp 	.d_ioctl =	ciss_ioctl,
2427ac40f5fSPoul-Henning Kamp 	.d_name =	"ciss",
2433a31b7ebSMike Smith };
2443a31b7ebSMike Smith 
2451fe6c4eeSScott Long /*
2461fe6c4eeSScott Long  * This tunable can be set at boot time and controls whether physical devices
2471fe6c4eeSScott Long  * that are marked hidden by the firmware should be exposed anyways.
2481fe6c4eeSScott Long  */
2491fe6c4eeSScott Long static unsigned int ciss_expose_hidden_physical = 0;
2501fe6c4eeSScott Long TUNABLE_INT("hw.ciss.expose_hidden_physical", &ciss_expose_hidden_physical);
2511fe6c4eeSScott Long 
2525a3c4d69SMitsuru IWASAKI static unsigned int ciss_nop_message_heartbeat = 0;
2535a3c4d69SMitsuru IWASAKI TUNABLE_INT("hw.ciss.nop_message_heartbeat", &ciss_nop_message_heartbeat);
2545a3c4d69SMitsuru IWASAKI 
25522657ce1SScott Long /*
25622657ce1SScott Long  * This tunable can force a particular transport to be used:
25722657ce1SScott Long  * <= 0 : use default
25822657ce1SScott Long  *    1 : force simple
25922657ce1SScott Long  *    2 : force performant
26022657ce1SScott Long  */
26122657ce1SScott Long static int ciss_force_transport = 0;
26222657ce1SScott Long TUNABLE_INT("hw.ciss.force_transport", &ciss_force_transport);
26322657ce1SScott Long 
26422657ce1SScott Long /*
26522657ce1SScott Long  * This tunable can force a particular interrupt delivery method to be used:
26622657ce1SScott Long  * <= 0 : use default
26722657ce1SScott Long  *    1 : force INTx
26822657ce1SScott Long  *    2 : force MSIX
26922657ce1SScott Long  */
27022657ce1SScott Long static int ciss_force_interrupt = 0;
27122657ce1SScott Long TUNABLE_INT("hw.ciss.force_interrupt", &ciss_force_interrupt);
27222657ce1SScott Long 
2733a31b7ebSMike Smith /************************************************************************
2743a31b7ebSMike Smith  * CISS adapters amazingly don't have a defined programming interface
2753a31b7ebSMike Smith  * value.  (One could say some very despairing things about PCI and
2763a31b7ebSMike Smith  * people just not getting the general idea.)  So we are forced to
2773a31b7ebSMike Smith  * stick with matching against subvendor/subdevice, and thus have to
2783a31b7ebSMike Smith  * be updated for every new CISS adapter that appears.
2793a31b7ebSMike Smith  */
2802fdaa90dSScott Long #define CISS_BOARD_UNKNWON	0
2812fdaa90dSScott Long #define CISS_BOARD_SA5		1
2822fdaa90dSScott Long #define CISS_BOARD_SA5B		2
2832fdaa90dSScott Long #define CISS_BOARD_NOMSI	(1<<4)
2843a31b7ebSMike Smith 
2853a31b7ebSMike Smith static struct
2863a31b7ebSMike Smith {
2873a31b7ebSMike Smith     u_int16_t	subvendor;
2883a31b7ebSMike Smith     u_int16_t	subdevice;
2893a31b7ebSMike Smith     int		flags;
2903a31b7ebSMike Smith     char	*desc;
2913a31b7ebSMike Smith } ciss_vendor_data[] = {
2922fdaa90dSScott Long     { 0x0e11, 0x4070, CISS_BOARD_SA5|CISS_BOARD_NOMSI,	"Compaq Smart Array 5300" },
2932fdaa90dSScott Long     { 0x0e11, 0x4080, CISS_BOARD_SA5B|CISS_BOARD_NOMSI,	"Compaq Smart Array 5i" },
2942fdaa90dSScott Long     { 0x0e11, 0x4082, CISS_BOARD_SA5B|CISS_BOARD_NOMSI,	"Compaq Smart Array 532" },
2952fdaa90dSScott Long     { 0x0e11, 0x4083, CISS_BOARD_SA5B|CISS_BOARD_NOMSI,	"HP Smart Array 5312" },
296b26392c7SPaul Saab     { 0x0e11, 0x4091, CISS_BOARD_SA5,	"HP Smart Array 6i" },
2979e7313bbSPaul Saab     { 0x0e11, 0x409A, CISS_BOARD_SA5,	"HP Smart Array 641" },
2989e7313bbSPaul Saab     { 0x0e11, 0x409B, CISS_BOARD_SA5,	"HP Smart Array 642" },
2999e7313bbSPaul Saab     { 0x0e11, 0x409C, CISS_BOARD_SA5,	"HP Smart Array 6400" },
3009e7313bbSPaul Saab     { 0x0e11, 0x409D, CISS_BOARD_SA5,	"HP Smart Array 6400 EM" },
301cad572c4SPaul Saab     { 0x103C, 0x3211, CISS_BOARD_SA5,	"HP Smart Array E200i" },
302cad572c4SPaul Saab     { 0x103C, 0x3212, CISS_BOARD_SA5,	"HP Smart Array E200" },
303cad572c4SPaul Saab     { 0x103C, 0x3213, CISS_BOARD_SA5,	"HP Smart Array E200i" },
304cad572c4SPaul Saab     { 0x103C, 0x3214, CISS_BOARD_SA5,	"HP Smart Array E200i" },
305cad572c4SPaul Saab     { 0x103C, 0x3215, CISS_BOARD_SA5,	"HP Smart Array E200i" },
306b612d5e1SPaul Saab     { 0x103C, 0x3220, CISS_BOARD_SA5,	"HP Smart Array" },
307b612d5e1SPaul Saab     { 0x103C, 0x3222, CISS_BOARD_SA5,	"HP Smart Array" },
3084bb10cf1SPaul Saab     { 0x103C, 0x3223, CISS_BOARD_SA5,	"HP Smart Array P800" },
309f3f82767SPaul Saab     { 0x103C, 0x3225, CISS_BOARD_SA5,	"HP Smart Array P600" },
310b612d5e1SPaul Saab     { 0x103C, 0x3230, CISS_BOARD_SA5,	"HP Smart Array" },
311cad572c4SPaul Saab     { 0x103C, 0x3231, CISS_BOARD_SA5,	"HP Smart Array" },
312b612d5e1SPaul Saab     { 0x103C, 0x3232, CISS_BOARD_SA5,	"HP Smart Array" },
313b612d5e1SPaul Saab     { 0x103C, 0x3233, CISS_BOARD_SA5,	"HP Smart Array" },
314cad572c4SPaul Saab     { 0x103C, 0x3234, CISS_BOARD_SA5,	"HP Smart Array P400" },
315cad572c4SPaul Saab     { 0x103C, 0x3235, CISS_BOARD_SA5,	"HP Smart Array P400i" },
316b612d5e1SPaul Saab     { 0x103C, 0x3236, CISS_BOARD_SA5,	"HP Smart Array" },
3177b6d3d4cSScott Long     { 0x103C, 0x3237, CISS_BOARD_SA5,	"HP Smart Array E500" },
318b612d5e1SPaul Saab     { 0x103C, 0x3238, CISS_BOARD_SA5,	"HP Smart Array" },
319b612d5e1SPaul Saab     { 0x103C, 0x3239, CISS_BOARD_SA5,	"HP Smart Array" },
320b612d5e1SPaul Saab     { 0x103C, 0x323A, CISS_BOARD_SA5,	"HP Smart Array" },
321b612d5e1SPaul Saab     { 0x103C, 0x323B, CISS_BOARD_SA5,	"HP Smart Array" },
322b612d5e1SPaul Saab     { 0x103C, 0x323C, CISS_BOARD_SA5,	"HP Smart Array" },
3237b6d3d4cSScott Long     { 0x103C, 0x323D, CISS_BOARD_SA5,	"HP Smart Array P700m" },
324160b4e6bSPaul Saab     { 0x103C, 0x3241, CISS_BOARD_SA5,	"HP Smart Array P212" },
325160b4e6bSPaul Saab     { 0x103C, 0x3243, CISS_BOARD_SA5,	"HP Smart Array P410" },
326160b4e6bSPaul Saab     { 0x103C, 0x3245, CISS_BOARD_SA5,	"HP Smart Array P410i" },
327160b4e6bSPaul Saab     { 0x103C, 0x3247, CISS_BOARD_SA5,	"HP Smart Array P411" },
328160b4e6bSPaul Saab     { 0x103C, 0x3249, CISS_BOARD_SA5,	"HP Smart Array P812" },
3297b6d3d4cSScott Long     { 0x103C, 0x324A, CISS_BOARD_SA5,	"HP Smart Array P712m" },
3307b6d3d4cSScott Long     { 0x103C, 0x324B, CISS_BOARD_SA5,	"HP Smart Array" },
3314a6a94d8SArchie Cobbs     { 0, 0, 0, NULL }
3323a31b7ebSMike Smith };
3333a31b7ebSMike Smith 
3343a31b7ebSMike Smith /************************************************************************
3353a31b7ebSMike Smith  * Find a match for the device in our list of known adapters.
3363a31b7ebSMike Smith  */
3373a31b7ebSMike Smith static int
3383a31b7ebSMike Smith ciss_lookup(device_t dev)
3393a31b7ebSMike Smith {
3403a31b7ebSMike Smith     int 	i;
3413a31b7ebSMike Smith 
3423a31b7ebSMike Smith     for (i = 0; ciss_vendor_data[i].desc != NULL; i++)
3433a31b7ebSMike Smith 	if ((pci_get_subvendor(dev) == ciss_vendor_data[i].subvendor) &&
3443a31b7ebSMike Smith 	    (pci_get_subdevice(dev) == ciss_vendor_data[i].subdevice)) {
3453a31b7ebSMike Smith 	    return(i);
3463a31b7ebSMike Smith 	}
3473a31b7ebSMike Smith     return(-1);
3483a31b7ebSMike Smith }
3493a31b7ebSMike Smith 
3503a31b7ebSMike Smith /************************************************************************
3513a31b7ebSMike Smith  * Match a known CISS adapter.
3523a31b7ebSMike Smith  */
3533a31b7ebSMike Smith static int
3543a31b7ebSMike Smith ciss_probe(device_t dev)
3553a31b7ebSMike Smith {
3563a31b7ebSMike Smith     int		i;
3573a31b7ebSMike Smith 
3583a31b7ebSMike Smith     i = ciss_lookup(dev);
3593a31b7ebSMike Smith     if (i != -1) {
3603a31b7ebSMike Smith 	device_set_desc(dev, ciss_vendor_data[i].desc);
361538565c4SWarner Losh 	return(BUS_PROBE_DEFAULT);
3623a31b7ebSMike Smith     }
3633a31b7ebSMike Smith     return(ENOENT);
3643a31b7ebSMike Smith }
3653a31b7ebSMike Smith 
3663a31b7ebSMike Smith /************************************************************************
3673a31b7ebSMike Smith  * Attach the driver to this adapter.
3683a31b7ebSMike Smith  */
3693a31b7ebSMike Smith static int
3703a31b7ebSMike Smith ciss_attach(device_t dev)
3713a31b7ebSMike Smith {
3723a31b7ebSMike Smith     struct ciss_softc	*sc;
373d69c9c78SScott Long     int			error;
3743a31b7ebSMike Smith 
3753a31b7ebSMike Smith     debug_called(1);
3763a31b7ebSMike Smith 
3773a31b7ebSMike Smith #ifdef CISS_DEBUG
3783a31b7ebSMike Smith     /* print structure/union sizes */
3793a31b7ebSMike Smith     debug_struct(ciss_command);
3803a31b7ebSMike Smith     debug_struct(ciss_header);
3813a31b7ebSMike Smith     debug_union(ciss_device_address);
3823a31b7ebSMike Smith     debug_struct(ciss_cdb);
3833a31b7ebSMike Smith     debug_struct(ciss_report_cdb);
3843a31b7ebSMike Smith     debug_struct(ciss_notify_cdb);
3853a31b7ebSMike Smith     debug_struct(ciss_notify);
3863a31b7ebSMike Smith     debug_struct(ciss_message_cdb);
3873a31b7ebSMike Smith     debug_struct(ciss_error_info_pointer);
3883a31b7ebSMike Smith     debug_struct(ciss_error_info);
3893a31b7ebSMike Smith     debug_struct(ciss_sg_entry);
3903a31b7ebSMike Smith     debug_struct(ciss_config_table);
3913a31b7ebSMike Smith     debug_struct(ciss_bmic_cdb);
3923a31b7ebSMike Smith     debug_struct(ciss_bmic_id_ldrive);
3933a31b7ebSMike Smith     debug_struct(ciss_bmic_id_lstatus);
3943a31b7ebSMike Smith     debug_struct(ciss_bmic_id_table);
3953a31b7ebSMike Smith     debug_struct(ciss_bmic_id_pdrive);
3963a31b7ebSMike Smith     debug_struct(ciss_bmic_blink_pdrive);
3973a31b7ebSMike Smith     debug_struct(ciss_bmic_flush_cache);
3983a31b7ebSMike Smith     debug_const(CISS_MAX_REQUESTS);
3993a31b7ebSMike Smith     debug_const(CISS_MAX_LOGICAL);
4003a31b7ebSMike Smith     debug_const(CISS_INTERRUPT_COALESCE_DELAY);
4013a31b7ebSMike Smith     debug_const(CISS_INTERRUPT_COALESCE_COUNT);
4023a31b7ebSMike Smith     debug_const(CISS_COMMAND_ALLOC_SIZE);
4033a31b7ebSMike Smith     debug_const(CISS_COMMAND_SG_LENGTH);
4043a31b7ebSMike Smith 
4053a31b7ebSMike Smith     debug_type(cciss_pci_info_struct);
4063a31b7ebSMike Smith     debug_type(cciss_coalint_struct);
4073a31b7ebSMike Smith     debug_type(cciss_coalint_struct);
4083a31b7ebSMike Smith     debug_type(NodeName_type);
4093a31b7ebSMike Smith     debug_type(NodeName_type);
4103a31b7ebSMike Smith     debug_type(Heartbeat_type);
4113a31b7ebSMike Smith     debug_type(BusTypes_type);
4123a31b7ebSMike Smith     debug_type(FirmwareVer_type);
4133a31b7ebSMike Smith     debug_type(DriverVer_type);
4143a31b7ebSMike Smith     debug_type(IOCTL_Command_struct);
4153a31b7ebSMike Smith #endif
4163a31b7ebSMike Smith 
4173a31b7ebSMike Smith     sc = device_get_softc(dev);
4183a31b7ebSMike Smith     sc->ciss_dev = dev;
41964a026bdSGavin Atkinson     mtx_init(&sc->ciss_mtx, "cissmtx", NULL, MTX_DEF);
4203a31b7ebSMike Smith 
4213a31b7ebSMike Smith     /*
4223a31b7ebSMike Smith      * Do PCI-specific init.
4233a31b7ebSMike Smith      */
4243a31b7ebSMike Smith     if ((error = ciss_init_pci(sc)) != 0)
4253a31b7ebSMike Smith 	goto out;
4263a31b7ebSMike Smith 
4273a31b7ebSMike Smith     /*
4283a31b7ebSMike Smith      * Initialise driver queues.
4293a31b7ebSMike Smith      */
4303a31b7ebSMike Smith     ciss_initq_free(sc);
431c6131460SPaul Saab     ciss_initq_notify(sc);
432975b7318SScott Long     callout_init_mtx(&sc->ciss_periodic, &sc->ciss_mtx, 0);
4333a31b7ebSMike Smith 
4343a31b7ebSMike Smith     /*
43517c0792dSPaul Saab      * Initalize device sysctls.
43617c0792dSPaul Saab      */
43717c0792dSPaul Saab     ciss_init_sysctl(sc);
43817c0792dSPaul Saab 
43917c0792dSPaul Saab     /*
4403a31b7ebSMike Smith      * Initialise command/request pool.
4413a31b7ebSMike Smith      */
4423a31b7ebSMike Smith     if ((error = ciss_init_requests(sc)) != 0)
4433a31b7ebSMike Smith 	goto out;
4443a31b7ebSMike Smith 
4453a31b7ebSMike Smith     /*
4463a31b7ebSMike Smith      * Get adapter information.
4473a31b7ebSMike Smith      */
4483a31b7ebSMike Smith     if ((error = ciss_identify_adapter(sc)) != 0)
4493a31b7ebSMike Smith 	goto out;
4503a31b7ebSMike Smith 
4513a31b7ebSMike Smith     /*
452c6131460SPaul Saab      * Find all the physical devices.
453c6131460SPaul Saab      */
454c6131460SPaul Saab     if ((error = ciss_init_physical(sc)) != 0)
455c6131460SPaul Saab 	goto out;
456c6131460SPaul Saab 
457c6131460SPaul Saab     /*
4583a31b7ebSMike Smith      * Build our private table of logical devices.
4593a31b7ebSMike Smith      */
4603a31b7ebSMike Smith     if ((error = ciss_init_logical(sc)) != 0)
4613a31b7ebSMike Smith 	goto out;
4623a31b7ebSMike Smith 
4633a31b7ebSMike Smith     /*
4643a31b7ebSMike Smith      * Enable interrupts so that the CAM scan can complete.
4653a31b7ebSMike Smith      */
4663a31b7ebSMike Smith     CISS_TL_SIMPLE_ENABLE_INTERRUPTS(sc);
4673a31b7ebSMike Smith 
4683a31b7ebSMike Smith     /*
4693a31b7ebSMike Smith      * Initialise the CAM interface.
4703a31b7ebSMike Smith      */
4713a31b7ebSMike Smith     if ((error = ciss_cam_init(sc)) != 0)
4723a31b7ebSMike Smith 	goto out;
4733a31b7ebSMike Smith 
4743a31b7ebSMike Smith     /*
4753a31b7ebSMike Smith      * Start the heartbeat routine and event chain.
4763a31b7ebSMike Smith      */
4773a31b7ebSMike Smith     ciss_periodic(sc);
4783a31b7ebSMike Smith 
4793a31b7ebSMike Smith    /*
4803a31b7ebSMike Smith      * Create the control device.
4813a31b7ebSMike Smith      */
4823a31b7ebSMike Smith     sc->ciss_dev_t = make_dev(&ciss_cdevsw, device_get_unit(sc->ciss_dev),
4833a31b7ebSMike Smith 			      UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
4843a31b7ebSMike Smith 			      "ciss%d", device_get_unit(sc->ciss_dev));
4853a31b7ebSMike Smith     sc->ciss_dev_t->si_drv1 = sc;
4863a31b7ebSMike Smith 
4873a31b7ebSMike Smith     /*
4883a31b7ebSMike Smith      * The adapter is running; synchronous commands can now sleep
4893a31b7ebSMike Smith      * waiting for an interrupt to signal completion.
4903a31b7ebSMike Smith      */
4913a31b7ebSMike Smith     sc->ciss_flags |= CISS_FLAG_RUNNING;
4923a31b7ebSMike Smith 
493c6131460SPaul Saab     ciss_spawn_notify_thread(sc);
494c6131460SPaul Saab 
4953a31b7ebSMike Smith     error = 0;
4963a31b7ebSMike Smith  out:
49764a026bdSGavin Atkinson     if (error != 0) {
49864a026bdSGavin Atkinson 	/* ciss_free() expects the mutex to be held */
49964a026bdSGavin Atkinson 	mtx_lock(&sc->ciss_mtx);
5003a31b7ebSMike Smith 	ciss_free(sc);
50164a026bdSGavin Atkinson     }
5023a31b7ebSMike Smith     return(error);
5033a31b7ebSMike Smith }
5043a31b7ebSMike Smith 
5053a31b7ebSMike Smith /************************************************************************
5063a31b7ebSMike Smith  * Detach the driver from this adapter.
5073a31b7ebSMike Smith  */
5083a31b7ebSMike Smith static int
5093a31b7ebSMike Smith ciss_detach(device_t dev)
5103a31b7ebSMike Smith {
5113a31b7ebSMike Smith     struct ciss_softc	*sc = device_get_softc(dev);
5123a31b7ebSMike Smith 
5133a31b7ebSMike Smith     debug_called(1);
5143a31b7ebSMike Smith 
515975b7318SScott Long     mtx_lock(&sc->ciss_mtx);
516975b7318SScott Long     if (sc->ciss_flags & CISS_FLAG_CONTROL_OPEN) {
517975b7318SScott Long 	mtx_unlock(&sc->ciss_mtx);
518ffdf82e1SPaul Saab 	return (EBUSY);
519975b7318SScott Long     }
520ffdf82e1SPaul Saab 
5213a31b7ebSMike Smith     /* flush adapter cache */
5223a31b7ebSMike Smith     ciss_flush_adapter(sc);
5233a31b7ebSMike Smith 
524975b7318SScott Long     /* release all resources.  The mutex is released and freed here too. */
5253a31b7ebSMike Smith     ciss_free(sc);
5263a31b7ebSMike Smith 
5273a31b7ebSMike Smith     return(0);
5283a31b7ebSMike Smith }
5293a31b7ebSMike Smith 
5303a31b7ebSMike Smith /************************************************************************
5313a31b7ebSMike Smith  * Prepare adapter for system shutdown.
5323a31b7ebSMike Smith  */
5333a31b7ebSMike Smith static int
5343a31b7ebSMike Smith ciss_shutdown(device_t dev)
5353a31b7ebSMike Smith {
5363a31b7ebSMike Smith     struct ciss_softc	*sc = device_get_softc(dev);
5373a31b7ebSMike Smith 
5383a31b7ebSMike Smith     debug_called(1);
5393a31b7ebSMike Smith 
540d4a4ddc6SScott Long     mtx_lock(&sc->ciss_mtx);
5413a31b7ebSMike Smith     /* flush adapter cache */
5423a31b7ebSMike Smith     ciss_flush_adapter(sc);
5433a31b7ebSMike Smith 
54417c0792dSPaul Saab     if (sc->ciss_soft_reset)
54517c0792dSPaul Saab 	ciss_soft_reset(sc);
546d4a4ddc6SScott Long     mtx_unlock(&sc->ciss_mtx);
54717c0792dSPaul Saab 
5483a31b7ebSMike Smith     return(0);
5493a31b7ebSMike Smith }
5503a31b7ebSMike Smith 
55117c0792dSPaul Saab static void
55217c0792dSPaul Saab ciss_init_sysctl(struct ciss_softc *sc)
55317c0792dSPaul Saab {
55417c0792dSPaul Saab 
55517c0792dSPaul Saab     SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->ciss_dev),
55617c0792dSPaul Saab 	SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ciss_dev)),
55717c0792dSPaul Saab 	OID_AUTO, "soft_reset", CTLFLAG_RW, &sc->ciss_soft_reset, 0, "");
55817c0792dSPaul Saab }
55917c0792dSPaul Saab 
5603a31b7ebSMike Smith /************************************************************************
5613a31b7ebSMike Smith  * Perform PCI-specific attachment actions.
5623a31b7ebSMike Smith  */
5633a31b7ebSMike Smith static int
5643a31b7ebSMike Smith ciss_init_pci(struct ciss_softc *sc)
5653a31b7ebSMike Smith {
5663a31b7ebSMike Smith     uintptr_t		cbase, csize, cofs;
56722657ce1SScott Long     uint32_t		method, supported_methods;
568d69c9c78SScott Long     int			error, sqmask, i;
56922657ce1SScott Long     void		*intr;
5703a31b7ebSMike Smith 
5713a31b7ebSMike Smith     debug_called(1);
5723a31b7ebSMike Smith 
5733a31b7ebSMike Smith     /*
574d69c9c78SScott Long      * Work out adapter type.
575d69c9c78SScott Long      */
576d69c9c78SScott Long     i = ciss_lookup(sc->ciss_dev);
577d69c9c78SScott Long     if (i < 0) {
578d69c9c78SScott Long 	ciss_printf(sc, "unknown adapter type\n");
579d69c9c78SScott Long 	return (ENXIO);
580d69c9c78SScott Long     }
581d69c9c78SScott Long 
582d69c9c78SScott Long     if (ciss_vendor_data[i].flags & CISS_BOARD_SA5) {
583d69c9c78SScott Long 	sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5;
584d69c9c78SScott Long     } else if (ciss_vendor_data[i].flags & CISS_BOARD_SA5B) {
585d69c9c78SScott Long 	sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5B;
586d69c9c78SScott Long     } else {
587d69c9c78SScott Long 	/*
588d69c9c78SScott Long 	 * XXX Big hammer, masks/unmasks all possible interrupts.  This should
589d69c9c78SScott Long 	 * work on all hardware variants.  Need to add code to handle the
590d69c9c78SScott Long 	 * "controller crashed" interupt bit that this unmasks.
591d69c9c78SScott Long 	 */
592d69c9c78SScott Long 	sqmask = ~0;
593d69c9c78SScott Long     }
594d69c9c78SScott Long 
595d69c9c78SScott Long     /*
5963a31b7ebSMike Smith      * Allocate register window first (we need this to find the config
5973a31b7ebSMike Smith      * struct).
5983a31b7ebSMike Smith      */
5993a31b7ebSMike Smith     error = ENXIO;
6003a31b7ebSMike Smith     sc->ciss_regs_rid = CISS_TL_SIMPLE_BAR_REGS;
6013a31b7ebSMike Smith     if ((sc->ciss_regs_resource =
6025f96beb9SNate Lawson 	 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
6035f96beb9SNate Lawson 				&sc->ciss_regs_rid, RF_ACTIVE)) == NULL) {
6043a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate register window\n");
6053a31b7ebSMike Smith 	return(ENXIO);
6063a31b7ebSMike Smith     }
6073a31b7ebSMike Smith     sc->ciss_regs_bhandle = rman_get_bushandle(sc->ciss_regs_resource);
6083a31b7ebSMike Smith     sc->ciss_regs_btag = rman_get_bustag(sc->ciss_regs_resource);
6093a31b7ebSMike Smith 
6103a31b7ebSMike Smith     /*
6113a31b7ebSMike Smith      * Find the BAR holding the config structure.  If it's not the one
6123a31b7ebSMike Smith      * we already mapped for registers, map it too.
6133a31b7ebSMike Smith      */
6143a31b7ebSMike Smith     sc->ciss_cfg_rid = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_BAR) & 0xffff;
6153a31b7ebSMike Smith     if (sc->ciss_cfg_rid != sc->ciss_regs_rid) {
6163a31b7ebSMike Smith 	if ((sc->ciss_cfg_resource =
6175f96beb9SNate Lawson 	     bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
6185f96beb9SNate Lawson 				    &sc->ciss_cfg_rid, RF_ACTIVE)) == NULL) {
6193a31b7ebSMike Smith 	    ciss_printf(sc, "can't allocate config window\n");
6203a31b7ebSMike Smith 	    return(ENXIO);
6213a31b7ebSMike Smith 	}
6223a31b7ebSMike Smith 	cbase = (uintptr_t)rman_get_virtual(sc->ciss_cfg_resource);
6233a31b7ebSMike Smith 	csize = rman_get_end(sc->ciss_cfg_resource) -
6243a31b7ebSMike Smith 	    rman_get_start(sc->ciss_cfg_resource) + 1;
6253a31b7ebSMike Smith     } else {
6263a31b7ebSMike Smith 	cbase = (uintptr_t)rman_get_virtual(sc->ciss_regs_resource);
6273a31b7ebSMike Smith 	csize = rman_get_end(sc->ciss_regs_resource) -
6283a31b7ebSMike Smith 	    rman_get_start(sc->ciss_regs_resource) + 1;
6293a31b7ebSMike Smith     }
6303a31b7ebSMike Smith     cofs = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_OFF);
6313a31b7ebSMike Smith 
6323a31b7ebSMike Smith     /*
6333a31b7ebSMike Smith      * Use the base/size/offset values we just calculated to
6343a31b7ebSMike Smith      * sanity-check the config structure.  If it's OK, point to it.
6353a31b7ebSMike Smith      */
6363a31b7ebSMike Smith     if ((cofs + sizeof(struct ciss_config_table)) > csize) {
6373a31b7ebSMike Smith 	ciss_printf(sc, "config table outside window\n");
6383a31b7ebSMike Smith 	return(ENXIO);
6393a31b7ebSMike Smith     }
6403a31b7ebSMike Smith     sc->ciss_cfg = (struct ciss_config_table *)(cbase + cofs);
6413a31b7ebSMike Smith     debug(1, "config struct at %p", sc->ciss_cfg);
6423a31b7ebSMike Smith 
6433a31b7ebSMike Smith     /*
64422657ce1SScott Long      * Calculate the number of request structures/commands we are
64522657ce1SScott Long      * going to provide for this adapter.
64622657ce1SScott Long      */
64722657ce1SScott Long     sc->ciss_max_requests = min(CISS_MAX_REQUESTS, sc->ciss_cfg->max_outstanding_commands);
64822657ce1SScott Long 
64922657ce1SScott Long     /*
6503a31b7ebSMike Smith      * Validate the config structure.  If we supported other transport
6513a31b7ebSMike Smith      * methods, we could select amongst them at this point in time.
6523a31b7ebSMike Smith      */
6533a31b7ebSMike Smith     if (strncmp(sc->ciss_cfg->signature, "CISS", 4)) {
6543a31b7ebSMike Smith 	ciss_printf(sc, "config signature mismatch (got '%c%c%c%c')\n",
6553a31b7ebSMike Smith 		    sc->ciss_cfg->signature[0], sc->ciss_cfg->signature[1],
6563a31b7ebSMike Smith 		    sc->ciss_cfg->signature[2], sc->ciss_cfg->signature[3]);
6573a31b7ebSMike Smith 	return(ENXIO);
6583a31b7ebSMike Smith     }
6593a31b7ebSMike Smith 
6603a31b7ebSMike Smith     /*
66122657ce1SScott Long      * Select the mode of operation, prefer Performant.
6623a31b7ebSMike Smith      */
66322657ce1SScott Long     if (!(sc->ciss_cfg->supported_methods &
66422657ce1SScott Long 	(CISS_TRANSPORT_METHOD_SIMPLE | CISS_TRANSPORT_METHOD_PERF))) {
66522657ce1SScott Long 	ciss_printf(sc, "No supported transport layers: 0x%x\n",
66622657ce1SScott Long 	    sc->ciss_cfg->supported_methods);
6673a31b7ebSMike Smith     }
66822657ce1SScott Long 
66922657ce1SScott Long     switch (ciss_force_transport) {
67022657ce1SScott Long     case 1:
67122657ce1SScott Long 	supported_methods = CISS_TRANSPORT_METHOD_SIMPLE;
67222657ce1SScott Long 	break;
67322657ce1SScott Long     case 2:
67422657ce1SScott Long 	supported_methods = CISS_TRANSPORT_METHOD_PERF;
67522657ce1SScott Long 	break;
67622657ce1SScott Long     default:
67722657ce1SScott Long 	supported_methods = sc->ciss_cfg->supported_methods;
67822657ce1SScott Long 	break;
67922657ce1SScott Long     }
68022657ce1SScott Long 
68122657ce1SScott Long setup:
6822fdaa90dSScott Long     if ((supported_methods & CISS_TRANSPORT_METHOD_PERF) != 0) {
68322657ce1SScott Long 	method = CISS_TRANSPORT_METHOD_PERF;
68422657ce1SScott Long 	sc->ciss_perf = (struct ciss_perf_config *)(cbase + cofs +
68522657ce1SScott Long 	    sc->ciss_cfg->transport_offset);
68622657ce1SScott Long 	if (ciss_init_perf(sc)) {
68722657ce1SScott Long 	    supported_methods &= ~method;
68822657ce1SScott Long 	    goto setup;
68922657ce1SScott Long 	}
69022657ce1SScott Long     } else if (supported_methods & CISS_TRANSPORT_METHOD_SIMPLE) {
69122657ce1SScott Long 	method = CISS_TRANSPORT_METHOD_SIMPLE;
69222657ce1SScott Long     } else {
69322657ce1SScott Long 	ciss_printf(sc, "No supported transport methods: 0x%x\n",
69422657ce1SScott Long 	    sc->ciss_cfg->supported_methods);
69522657ce1SScott Long 	return(ENXIO);
69622657ce1SScott Long     }
69722657ce1SScott Long 
69822657ce1SScott Long     /*
69922657ce1SScott Long      * Tell it we're using the low 4GB of RAM.  Set the default interrupt
70022657ce1SScott Long      * coalescing options.
70122657ce1SScott Long      */
70222657ce1SScott Long     sc->ciss_cfg->requested_method = method;
7033a31b7ebSMike Smith     sc->ciss_cfg->command_physlimit = 0;
7043a31b7ebSMike Smith     sc->ciss_cfg->interrupt_coalesce_delay = CISS_INTERRUPT_COALESCE_DELAY;
7053a31b7ebSMike Smith     sc->ciss_cfg->interrupt_coalesce_count = CISS_INTERRUPT_COALESCE_COUNT;
7063a31b7ebSMike Smith 
707a891a3bfSPaul Saab #ifdef __i386__
708a891a3bfSPaul Saab     sc->ciss_cfg->host_driver |= CISS_DRIVER_SCSI_PREFETCH;
709a891a3bfSPaul Saab #endif
710a891a3bfSPaul Saab 
7113a31b7ebSMike Smith     if (ciss_update_config(sc)) {
7123a31b7ebSMike Smith 	ciss_printf(sc, "adapter refuses to accept config update (IDBR 0x%x)\n",
7133a31b7ebSMike Smith 		    CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR));
7143a31b7ebSMike Smith 	return(ENXIO);
7153a31b7ebSMike Smith     }
71622657ce1SScott Long     if ((sc->ciss_cfg->active_method & method) == 0) {
71722657ce1SScott Long 	supported_methods &= ~method;
71822657ce1SScott Long 	if (supported_methods == 0) {
71922657ce1SScott Long 	    ciss_printf(sc, "adapter refuses to go into available transports "
72022657ce1SScott Long 		"mode (0x%x, 0x%x)\n", supported_methods,
72122657ce1SScott Long 		sc->ciss_cfg->active_method);
7223a31b7ebSMike Smith 	    return(ENXIO);
72322657ce1SScott Long 	} else
72422657ce1SScott Long 	    goto setup;
7253a31b7ebSMike Smith     }
7263a31b7ebSMike Smith 
7273a31b7ebSMike Smith     /*
7283a31b7ebSMike Smith      * Wait for the adapter to come ready.
7293a31b7ebSMike Smith      */
7303a31b7ebSMike Smith     if ((error = ciss_wait_adapter(sc)) != 0)
7313a31b7ebSMike Smith 	return(error);
7323a31b7ebSMike Smith 
73322657ce1SScott Long     /* Prepare to possibly use MSIX and/or PERFORMANT interrupts.  Normal
73422657ce1SScott Long      * interrupts have a rid of 0, this will be overridden if MSIX is used.
73522657ce1SScott Long      */
73622657ce1SScott Long     sc->ciss_irq_rid[0] = 0;
73722657ce1SScott Long     if (method == CISS_TRANSPORT_METHOD_PERF) {
73822657ce1SScott Long 	ciss_printf(sc, "PERFORMANT Transport\n");
739d69c9c78SScott Long 	if ((ciss_force_interrupt != 1) && (ciss_setup_msix(sc) == 0)) {
74022657ce1SScott Long 	    intr = ciss_perf_msi_intr;
741d69c9c78SScott Long 	} else {
74222657ce1SScott Long 	    intr = ciss_perf_intr;
743d69c9c78SScott Long 	}
744b23be3e0SScott Long 	/* XXX The docs say that the 0x01 bit is only for SAS controllers.
745b23be3e0SScott Long 	 * Unfortunately, there is no good way to know if this is a SAS
746b23be3e0SScott Long 	 * controller.  Hopefully enabling this bit universally will work OK.
747b23be3e0SScott Long 	 * It seems to work fine for SA6i controllers.
748b23be3e0SScott Long 	 */
749b23be3e0SScott Long 	sc->ciss_interrupt_mask = CISS_TL_PERF_INTR_OPQ | CISS_TL_PERF_INTR_MSI;
750b23be3e0SScott Long 
75122657ce1SScott Long     } else {
75222657ce1SScott Long 	ciss_printf(sc, "SIMPLE Transport\n");
75322657ce1SScott Long 	/* MSIX doesn't seem to work in SIMPLE mode, only enable if it forced */
75422657ce1SScott Long 	if (ciss_force_interrupt == 2)
75522657ce1SScott Long 	    /* If this fails, we automatically revert to INTx */
75622657ce1SScott Long 	    ciss_setup_msix(sc);
75722657ce1SScott Long 	sc->ciss_perf = NULL;
75822657ce1SScott Long 	intr = ciss_intr;
759d69c9c78SScott Long 	sc->ciss_interrupt_mask = sqmask;
76022657ce1SScott Long     }
76122657ce1SScott Long 
7623a31b7ebSMike Smith     /*
7633a31b7ebSMike Smith      * Turn off interrupts before we go routing anything.
7643a31b7ebSMike Smith      */
7653a31b7ebSMike Smith     CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
7663a31b7ebSMike Smith 
7673a31b7ebSMike Smith     /*
7683a31b7ebSMike Smith      * Allocate and set up our interrupt.
7693a31b7ebSMike Smith      */
7703a31b7ebSMike Smith     if ((sc->ciss_irq_resource =
77122657ce1SScott Long 	 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_IRQ, &sc->ciss_irq_rid[0],
7723a31b7ebSMike Smith 				RF_ACTIVE | RF_SHAREABLE)) == NULL) {
7733a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate interrupt\n");
7743a31b7ebSMike Smith 	return(ENXIO);
7753a31b7ebSMike Smith     }
77622657ce1SScott Long 
777cc850363SPeter Wemm     if (bus_setup_intr(sc->ciss_dev, sc->ciss_irq_resource,
77822657ce1SScott Long 		       INTR_TYPE_CAM|INTR_MPSAFE, NULL, intr, sc,
7793a31b7ebSMike Smith 		       &sc->ciss_intr)) {
7803a31b7ebSMike Smith 	ciss_printf(sc, "can't set up interrupt\n");
7813a31b7ebSMike Smith 	return(ENXIO);
7823a31b7ebSMike Smith     }
7833a31b7ebSMike Smith 
7843a31b7ebSMike Smith     /*
7853a31b7ebSMike Smith      * Allocate the parent bus DMA tag appropriate for our PCI
7863a31b7ebSMike Smith      * interface.
7873a31b7ebSMike Smith      *
7883a31b7ebSMike Smith      * Note that "simple" adapters can only address within a 32-bit
7893a31b7ebSMike Smith      * span.
7903a31b7ebSMike Smith      */
7913a31b7ebSMike Smith     if (bus_dma_tag_create(NULL, 			/* parent */
7923a31b7ebSMike Smith 			   1, 0, 			/* alignment, boundary */
793639717c8SPaul Saab 			   BUS_SPACE_MAXADDR,		/* lowaddr */
7943a31b7ebSMike Smith 			   BUS_SPACE_MAXADDR, 		/* highaddr */
7953a31b7ebSMike Smith 			   NULL, NULL, 			/* filter, filterarg */
796639717c8SPaul Saab 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
7972fdaa90dSScott Long 			   CISS_MAX_SG_ELEMENTS,	/* nsegments */
7983a31b7ebSMike Smith 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
79922657ce1SScott Long 			   0,				/* flags */
800f6b1c44dSScott Long 			   NULL, NULL,			/* lockfunc, lockarg */
8013a31b7ebSMike Smith 			   &sc->ciss_parent_dmat)) {
8023a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate parent DMA tag\n");
8033a31b7ebSMike Smith 	return(ENOMEM);
8043a31b7ebSMike Smith     }
8053a31b7ebSMike Smith 
8063a31b7ebSMike Smith     /*
8073a31b7ebSMike Smith      * Create DMA tag for mapping buffers into adapter-addressable
8083a31b7ebSMike Smith      * space.
8093a31b7ebSMike Smith      */
8103a31b7ebSMike Smith     if (bus_dma_tag_create(sc->ciss_parent_dmat, 	/* parent */
8113a31b7ebSMike Smith 			   1, 0, 			/* alignment, boundary */
8123a31b7ebSMike Smith 			   BUS_SPACE_MAXADDR,		/* lowaddr */
8133a31b7ebSMike Smith 			   BUS_SPACE_MAXADDR, 		/* highaddr */
8143a31b7ebSMike Smith 			   NULL, NULL, 			/* filter, filterarg */
8152fdaa90dSScott Long 			   MAXBSIZE, CISS_MAX_SG_ELEMENTS,	/* maxsize, nsegments */
8163a31b7ebSMike Smith 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
81722657ce1SScott Long 			   BUS_DMA_ALLOCNOW,		/* flags */
818975b7318SScott Long 			   busdma_lock_mutex, &sc->ciss_mtx,	/* lockfunc, lockarg */
8193a31b7ebSMike Smith 			   &sc->ciss_buffer_dmat)) {
8203a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate buffer DMA tag\n");
8213a31b7ebSMike Smith 	return(ENOMEM);
8223a31b7ebSMike Smith     }
8233a31b7ebSMike Smith     return(0);
8243a31b7ebSMike Smith }
8253a31b7ebSMike Smith 
8263a31b7ebSMike Smith /************************************************************************
82722657ce1SScott Long  * Setup MSI/MSIX operation (Performant only)
8282fdaa90dSScott Long  * Four interrupts are available, but we only use 1 right now.  If MSI-X
8292fdaa90dSScott Long  * isn't avaialble, try using MSI instead.
83022657ce1SScott Long  */
83122657ce1SScott Long static int
83222657ce1SScott Long ciss_setup_msix(struct ciss_softc *sc)
83322657ce1SScott Long {
83422657ce1SScott Long     int val, i;
83522657ce1SScott Long 
83622657ce1SScott Long     /* Weed out devices that don't actually support MSI */
8372fdaa90dSScott Long     i = ciss_lookup(sc->ciss_dev);
8382fdaa90dSScott Long     if (ciss_vendor_data[i].flags & CISS_BOARD_NOMSI)
83922657ce1SScott Long 	return (EINVAL);
84022657ce1SScott Long 
8412fdaa90dSScott Long     /*
8422fdaa90dSScott Long      * Only need to use the minimum number of MSI vectors, as the driver
8432fdaa90dSScott Long      * doesn't support directed MSIX interrupts.
8442fdaa90dSScott Long      */
84522657ce1SScott Long     val = pci_msix_count(sc->ciss_dev);
8462fdaa90dSScott Long     if (val < CISS_MSI_COUNT) {
8472fdaa90dSScott Long 	val = pci_msi_count(sc->ciss_dev);
8482fdaa90dSScott Long 	device_printf(sc->ciss_dev, "got %d MSI messages]\n", val);
849b23be3e0SScott Long 	if (val < CISS_MSI_COUNT)
850b23be3e0SScott Long 	    return (EINVAL);
8512fdaa90dSScott Long     }
852b23be3e0SScott Long     val = MIN(val, CISS_MSI_COUNT);
8532fdaa90dSScott Long     if (pci_alloc_msix(sc->ciss_dev, &val) != 0) {
8542fdaa90dSScott Long 	if (pci_alloc_msi(sc->ciss_dev, &val) != 0)
85522657ce1SScott Long 	    return (EINVAL);
8562fdaa90dSScott Long     }
85722657ce1SScott Long 
85822657ce1SScott Long     sc->ciss_msi = val;
8592fdaa90dSScott Long     if (bootverbose)
8602fdaa90dSScott Long 	ciss_printf(sc, "Using %d MSIX interrupt%s\n", val,
8612fdaa90dSScott Long 	    (val != 1) ? "s" : "");
86222657ce1SScott Long 
8632fdaa90dSScott Long     for (i = 0; i < val; i++)
86422657ce1SScott Long 	sc->ciss_irq_rid[i] = i + 1;
86522657ce1SScott Long 
86622657ce1SScott Long     return (0);
86722657ce1SScott Long 
86822657ce1SScott Long }
86922657ce1SScott Long 
87022657ce1SScott Long /************************************************************************
87122657ce1SScott Long  * Setup the Performant structures.
87222657ce1SScott Long  */
87322657ce1SScott Long static int
87422657ce1SScott Long ciss_init_perf(struct ciss_softc *sc)
87522657ce1SScott Long {
87622657ce1SScott Long     struct ciss_perf_config *pc = sc->ciss_perf;
87722657ce1SScott Long     int reply_size;
87822657ce1SScott Long 
87922657ce1SScott Long     /*
88022657ce1SScott Long      * Create the DMA tag for the reply queue.
88122657ce1SScott Long      */
88222657ce1SScott Long     reply_size = sizeof(uint64_t) * sc->ciss_max_requests;
88322657ce1SScott Long     if (bus_dma_tag_create(sc->ciss_parent_dmat,	/* parent */
88422657ce1SScott Long 			   1, 0, 			/* alignment, boundary */
88522657ce1SScott Long 			   BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
88622657ce1SScott Long 			   BUS_SPACE_MAXADDR, 		/* highaddr */
88722657ce1SScott Long 			   NULL, NULL, 			/* filter, filterarg */
88822657ce1SScott Long 			   reply_size, 1,		/* maxsize, nsegments */
88922657ce1SScott Long 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
89022657ce1SScott Long 			   0,				/* flags */
89122657ce1SScott Long 			   NULL, NULL,			/* lockfunc, lockarg */
89222657ce1SScott Long 			   &sc->ciss_reply_dmat)) {
89322657ce1SScott Long 	ciss_printf(sc, "can't allocate reply DMA tag\n");
89422657ce1SScott Long 	return(ENOMEM);
89522657ce1SScott Long     }
89622657ce1SScott Long     /*
89722657ce1SScott Long      * Allocate memory and make it available for DMA.
89822657ce1SScott Long      */
89922657ce1SScott Long     if (bus_dmamem_alloc(sc->ciss_reply_dmat, (void **)&sc->ciss_reply,
90022657ce1SScott Long 			 BUS_DMA_NOWAIT, &sc->ciss_reply_map)) {
90122657ce1SScott Long 	ciss_printf(sc, "can't allocate reply memory\n");
90222657ce1SScott Long 	return(ENOMEM);
90322657ce1SScott Long     }
90422657ce1SScott Long     bus_dmamap_load(sc->ciss_reply_dmat, sc->ciss_reply_map, sc->ciss_reply,
90522657ce1SScott Long 		    reply_size, ciss_command_map_helper, &sc->ciss_reply_phys, 0);
90622657ce1SScott Long     bzero(sc->ciss_reply, reply_size);
90722657ce1SScott Long 
90822657ce1SScott Long     sc->ciss_cycle = 0x1;
90922657ce1SScott Long     sc->ciss_rqidx = 0;
91022657ce1SScott Long 
91122657ce1SScott Long     /*
91222657ce1SScott Long      * Preload the fetch table with common command sizes.  This allows the
91322657ce1SScott Long      * hardware to not waste bus cycles for typical i/o commands, but also not
91422657ce1SScott Long      * tax the driver to be too exact in choosing sizes.  The table is optimized
91522657ce1SScott Long      * for page-aligned i/o's, but since most i/o comes from the various pagers,
91622657ce1SScott Long      * it's a reasonable assumption to make.
91722657ce1SScott Long      */
91822657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_NONE] = (sizeof(struct ciss_command) + 15) / 16;
91922657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_1] =
92022657ce1SScott Long 	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 1 + 15) / 16;
92122657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_2] =
92222657ce1SScott Long 	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 2 + 15) / 16;
92322657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_4] =
92422657ce1SScott Long 	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 4 + 15) / 16;
92522657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_8] =
92622657ce1SScott Long 	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 8 + 15) / 16;
92722657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_16] =
92822657ce1SScott Long 	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 16 + 15) / 16;
92922657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_32] =
93022657ce1SScott Long 	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 32 + 15) / 16;
93122657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_MAX] = (CISS_COMMAND_ALLOC_SIZE + 15) / 16;
93222657ce1SScott Long 
93322657ce1SScott Long     pc->rq_size = sc->ciss_max_requests; /* XXX less than the card supports? */
93422657ce1SScott Long     pc->rq_count = 1;	/* XXX Hardcode for a single queue */
93522657ce1SScott Long     pc->rq_bank_hi = 0;
93622657ce1SScott Long     pc->rq_bank_lo = 0;
93722657ce1SScott Long     pc->rq[0].rq_addr_hi = 0x0;
93822657ce1SScott Long     pc->rq[0].rq_addr_lo = sc->ciss_reply_phys;
93922657ce1SScott Long 
94022657ce1SScott Long     return(0);
94122657ce1SScott Long }
94222657ce1SScott Long 
94322657ce1SScott Long /************************************************************************
9443a31b7ebSMike Smith  * Wait for the adapter to come ready.
9453a31b7ebSMike Smith  */
9463a31b7ebSMike Smith static int
9473a31b7ebSMike Smith ciss_wait_adapter(struct ciss_softc *sc)
9483a31b7ebSMike Smith {
9493a31b7ebSMike Smith     int		i;
9503a31b7ebSMike Smith 
9513a31b7ebSMike Smith     debug_called(1);
9523a31b7ebSMike Smith 
9533a31b7ebSMike Smith     /*
9543a31b7ebSMike Smith      * Wait for the adapter to come ready.
9553a31b7ebSMike Smith      */
9563a31b7ebSMike Smith     if (!(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY)) {
9573a31b7ebSMike Smith 	ciss_printf(sc, "waiting for adapter to come ready...\n");
9583a31b7ebSMike Smith 	for (i = 0; !(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY); i++) {
9593a31b7ebSMike Smith 	    DELAY(1000000);	/* one second */
9603a31b7ebSMike Smith 	    if (i > 30) {
9613a31b7ebSMike Smith 		ciss_printf(sc, "timed out waiting for adapter to come ready\n");
9623a31b7ebSMike Smith 		return(EIO);
9633a31b7ebSMike Smith 	    }
9643a31b7ebSMike Smith 	}
9653a31b7ebSMike Smith     }
9663a31b7ebSMike Smith     return(0);
9673a31b7ebSMike Smith }
9683a31b7ebSMike Smith 
9693a31b7ebSMike Smith /************************************************************************
9703a31b7ebSMike Smith  * Flush the adapter cache.
9713a31b7ebSMike Smith  */
9723a31b7ebSMike Smith static int
9733a31b7ebSMike Smith ciss_flush_adapter(struct ciss_softc *sc)
9743a31b7ebSMike Smith {
9753a31b7ebSMike Smith     struct ciss_request			*cr;
9763a31b7ebSMike Smith     struct ciss_bmic_flush_cache	*cbfc;
9773a31b7ebSMike Smith     int					error, command_status;
9783a31b7ebSMike Smith 
9793a31b7ebSMike Smith     debug_called(1);
9803a31b7ebSMike Smith 
9813a31b7ebSMike Smith     cr = NULL;
9823a31b7ebSMike Smith     cbfc = NULL;
9833a31b7ebSMike Smith 
9843a31b7ebSMike Smith     /*
9853a31b7ebSMike Smith      * Build a BMIC request to flush the cache.  We don't disable
9863a31b7ebSMike Smith      * it, as we may be going to do more I/O (eg. we are emulating
9873a31b7ebSMike Smith      * the Synchronise Cache command).
9883a31b7ebSMike Smith      */
9893a31b7ebSMike Smith     if ((cbfc = malloc(sizeof(*cbfc), CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
9903a31b7ebSMike Smith 	error = ENOMEM;
9913a31b7ebSMike Smith 	goto out;
9923a31b7ebSMike Smith     }
9933a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_FLUSH_CACHE,
9943a31b7ebSMike Smith 				       (void **)&cbfc, sizeof(*cbfc))) != 0)
9953a31b7ebSMike Smith 	goto out;
9963a31b7ebSMike Smith 
9973a31b7ebSMike Smith     /*
9983a31b7ebSMike Smith      * Submit the request and wait for it to complete.
9993a31b7ebSMike Smith      */
10003a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
10013a31b7ebSMike Smith 	ciss_printf(sc, "error sending BMIC FLUSH_CACHE command (%d)\n", error);
10023a31b7ebSMike Smith 	goto out;
10033a31b7ebSMike Smith     }
10043a31b7ebSMike Smith 
10053a31b7ebSMike Smith     /*
10063a31b7ebSMike Smith      * Check response.
10073a31b7ebSMike Smith      */
10083a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
10093a31b7ebSMike Smith     switch(command_status) {
10103a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:
10113a31b7ebSMike Smith 	break;
10123a31b7ebSMike Smith     default:
10133a31b7ebSMike Smith 	ciss_printf(sc, "error flushing cache (%s)\n",
10143a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
10153a31b7ebSMike Smith 	error = EIO;
10163a31b7ebSMike Smith 	goto out;
10173a31b7ebSMike Smith     }
10183a31b7ebSMike Smith 
10193a31b7ebSMike Smith out:
10203a31b7ebSMike Smith     if (cbfc != NULL)
10213a31b7ebSMike Smith 	free(cbfc, CISS_MALLOC_CLASS);
10223a31b7ebSMike Smith     if (cr != NULL)
10233a31b7ebSMike Smith 	ciss_release_request(cr);
10243a31b7ebSMike Smith     return(error);
10253a31b7ebSMike Smith }
10263a31b7ebSMike Smith 
102717c0792dSPaul Saab static void
102817c0792dSPaul Saab ciss_soft_reset(struct ciss_softc *sc)
102917c0792dSPaul Saab {
103017c0792dSPaul Saab     struct ciss_request		*cr = NULL;
103117c0792dSPaul Saab     struct ciss_command		*cc;
103217c0792dSPaul Saab     int				i, error = 0;
103317c0792dSPaul Saab 
103417c0792dSPaul Saab     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
103517c0792dSPaul Saab 	/* only reset proxy controllers */
103617c0792dSPaul Saab 	if (sc->ciss_controllers[i].physical.bus == 0)
103717c0792dSPaul Saab 	    continue;
103817c0792dSPaul Saab 
103917c0792dSPaul Saab 	if ((error = ciss_get_request(sc, &cr)) != 0)
104017c0792dSPaul Saab 	    break;
104117c0792dSPaul Saab 
104217c0792dSPaul Saab 	if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_SOFT_RESET,
104317c0792dSPaul Saab 					   NULL, 0)) != 0)
104417c0792dSPaul Saab 	    break;
104517c0792dSPaul Saab 
10462fdaa90dSScott Long 	cc = cr->cr_cc;
104717c0792dSPaul Saab 	cc->header.address = sc->ciss_controllers[i];
104817c0792dSPaul Saab 
104917c0792dSPaul Saab 	if ((error = ciss_synch_request(cr, 60 * 1000)) != 0)
105017c0792dSPaul Saab 	    break;
105117c0792dSPaul Saab 
105217c0792dSPaul Saab 	ciss_release_request(cr);
105317c0792dSPaul Saab     }
105417c0792dSPaul Saab 
105517c0792dSPaul Saab     if (error)
105617c0792dSPaul Saab 	ciss_printf(sc, "error resetting controller (%d)\n", error);
105717c0792dSPaul Saab 
105817c0792dSPaul Saab     if (cr != NULL)
105917c0792dSPaul Saab 	ciss_release_request(cr);
106017c0792dSPaul Saab }
106117c0792dSPaul Saab 
10623a31b7ebSMike Smith /************************************************************************
10633a31b7ebSMike Smith  * Allocate memory for the adapter command structures, initialise
10643a31b7ebSMike Smith  * the request structures.
10653a31b7ebSMike Smith  *
10663a31b7ebSMike Smith  * Note that the entire set of commands are allocated in a single
10673a31b7ebSMike Smith  * contiguous slab.
10683a31b7ebSMike Smith  */
10693a31b7ebSMike Smith static int
10703a31b7ebSMike Smith ciss_init_requests(struct ciss_softc *sc)
10713a31b7ebSMike Smith {
10723a31b7ebSMike Smith     struct ciss_request	*cr;
10733a31b7ebSMike Smith     int			i;
10743a31b7ebSMike Smith 
10753a31b7ebSMike Smith     debug_called(1);
10763a31b7ebSMike Smith 
10774bca277bSPaul Saab     if (bootverbose)
10783a31b7ebSMike Smith 	ciss_printf(sc, "using %d of %d available commands\n",
10793a31b7ebSMike Smith 		    sc->ciss_max_requests, sc->ciss_cfg->max_outstanding_commands);
10803a31b7ebSMike Smith 
10813a31b7ebSMike Smith     /*
10823a31b7ebSMike Smith      * Create the DMA tag for commands.
10833a31b7ebSMike Smith      */
10843a31b7ebSMike Smith     if (bus_dma_tag_create(sc->ciss_parent_dmat,	/* parent */
108522657ce1SScott Long 			   32, 0, 			/* alignment, boundary */
1086639717c8SPaul Saab 			   BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
10873a31b7ebSMike Smith 			   BUS_SPACE_MAXADDR, 		/* highaddr */
10883a31b7ebSMike Smith 			   NULL, NULL, 			/* filter, filterarg */
10893a31b7ebSMike Smith 			   CISS_COMMAND_ALLOC_SIZE *
10903a31b7ebSMike Smith 			   sc->ciss_max_requests, 1,	/* maxsize, nsegments */
10913a31b7ebSMike Smith 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
109222657ce1SScott Long 			   0,				/* flags */
1093639717c8SPaul Saab 			   NULL, NULL,			/* lockfunc, lockarg */
10943a31b7ebSMike Smith 			   &sc->ciss_command_dmat)) {
10953a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate command DMA tag\n");
10963a31b7ebSMike Smith 	return(ENOMEM);
10973a31b7ebSMike Smith     }
10983a31b7ebSMike Smith     /*
10993a31b7ebSMike Smith      * Allocate memory and make it available for DMA.
11003a31b7ebSMike Smith      */
11013a31b7ebSMike Smith     if (bus_dmamem_alloc(sc->ciss_command_dmat, (void **)&sc->ciss_command,
11023a31b7ebSMike Smith 			 BUS_DMA_NOWAIT, &sc->ciss_command_map)) {
11033a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate command memory\n");
11043a31b7ebSMike Smith 	return(ENOMEM);
11053a31b7ebSMike Smith     }
11063a31b7ebSMike Smith     bus_dmamap_load(sc->ciss_command_dmat, sc->ciss_command_map,sc->ciss_command,
1107be241f79SPaul Saab 		    CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests,
110822657ce1SScott Long 		    ciss_command_map_helper, &sc->ciss_command_phys, 0);
11093a31b7ebSMike Smith     bzero(sc->ciss_command, CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests);
11103a31b7ebSMike Smith 
11113a31b7ebSMike Smith     /*
11123a31b7ebSMike Smith      * Set up the request and command structures, push requests onto
11133a31b7ebSMike Smith      * the free queue.
11143a31b7ebSMike Smith      */
11153a31b7ebSMike Smith     for (i = 1; i < sc->ciss_max_requests; i++) {
11163a31b7ebSMike Smith 	cr = &sc->ciss_request[i];
11173a31b7ebSMike Smith 	cr->cr_sc = sc;
11183a31b7ebSMike Smith 	cr->cr_tag = i;
11192fdaa90dSScott Long 	cr->cr_cc = (struct ciss_command *)((uintptr_t)sc->ciss_command +
11202fdaa90dSScott Long 	    CISS_COMMAND_ALLOC_SIZE * i);
11212fdaa90dSScott Long 	cr->cr_ccphys = sc->ciss_command_phys + CISS_COMMAND_ALLOC_SIZE * i;
11223284b9eeSPaul Saab 	bus_dmamap_create(sc->ciss_buffer_dmat, 0, &cr->cr_datamap);
11233a31b7ebSMike Smith 	ciss_enqueue_free(cr);
11243a31b7ebSMike Smith     }
11253a31b7ebSMike Smith     return(0);
11263a31b7ebSMike Smith }
11273a31b7ebSMike Smith 
11283a31b7ebSMike Smith static void
11293a31b7ebSMike Smith ciss_command_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
11303a31b7ebSMike Smith {
113122657ce1SScott Long     uint32_t *addr;
11323a31b7ebSMike Smith 
113322657ce1SScott Long     addr = arg;
113422657ce1SScott Long     *addr = segs[0].ds_addr;
11353a31b7ebSMike Smith }
11363a31b7ebSMike Smith 
11373a31b7ebSMike Smith /************************************************************************
11383a31b7ebSMike Smith  * Identify the adapter, print some information about it.
11393a31b7ebSMike Smith  */
11403a31b7ebSMike Smith static int
11413a31b7ebSMike Smith ciss_identify_adapter(struct ciss_softc *sc)
11423a31b7ebSMike Smith {
11433a31b7ebSMike Smith     struct ciss_request	*cr;
11443a31b7ebSMike Smith     int			error, command_status;
11453a31b7ebSMike Smith 
11463a31b7ebSMike Smith     debug_called(1);
11473a31b7ebSMike Smith 
11483a31b7ebSMike Smith     cr = NULL;
11493a31b7ebSMike Smith 
11503a31b7ebSMike Smith     /*
11513a31b7ebSMike Smith      * Get a request, allocate storage for the adapter data.
11523a31b7ebSMike Smith      */
11533a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_CTLR,
11543a31b7ebSMike Smith 				       (void **)&sc->ciss_id,
11553a31b7ebSMike Smith 				       sizeof(*sc->ciss_id))) != 0)
11563a31b7ebSMike Smith 	goto out;
11573a31b7ebSMike Smith 
11583a31b7ebSMike Smith     /*
11593a31b7ebSMike Smith      * Submit the request and wait for it to complete.
11603a31b7ebSMike Smith      */
11613a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
11623a31b7ebSMike Smith 	ciss_printf(sc, "error sending BMIC ID_CTLR command (%d)\n", error);
11633a31b7ebSMike Smith 	goto out;
11643a31b7ebSMike Smith     }
11653a31b7ebSMike Smith 
11663a31b7ebSMike Smith     /*
11673a31b7ebSMike Smith      * Check response.
11683a31b7ebSMike Smith      */
11693a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
11703a31b7ebSMike Smith     switch(command_status) {
11713a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
11723a31b7ebSMike Smith 	break;
11733a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_UNDERRUN:
11743a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_OVERRUN:
11753a31b7ebSMike Smith 	ciss_printf(sc, "data over/underrun reading adapter information\n");
11763a31b7ebSMike Smith     default:
11773a31b7ebSMike Smith 	ciss_printf(sc, "error reading adapter information (%s)\n",
11783a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
11793a31b7ebSMike Smith 	error = EIO;
11803a31b7ebSMike Smith 	goto out;
11813a31b7ebSMike Smith     }
11823a31b7ebSMike Smith 
11833a31b7ebSMike Smith     /* sanity-check reply */
11843a31b7ebSMike Smith     if (!sc->ciss_id->big_map_supported) {
11853a31b7ebSMike Smith 	ciss_printf(sc, "adapter does not support BIG_MAP\n");
11863a31b7ebSMike Smith 	error = ENXIO;
11873a31b7ebSMike Smith 	goto out;
11883a31b7ebSMike Smith     }
11893a31b7ebSMike Smith 
1190d4aa427fSPaul Saab #if 0
11913a31b7ebSMike Smith     /* XXX later revisions may not need this */
11923a31b7ebSMike Smith     sc->ciss_flags |= CISS_FLAG_FAKE_SYNCH;
1193d4aa427fSPaul Saab #endif
11943a31b7ebSMike Smith 
11953a31b7ebSMike Smith     /* XXX only really required for old 5300 adapters? */
11963a31b7ebSMike Smith     sc->ciss_flags |= CISS_FLAG_BMIC_ABORT;
11973a31b7ebSMike Smith 
11983a31b7ebSMike Smith     /* print information */
11994bca277bSPaul Saab     if (bootverbose) {
1200c6131460SPaul Saab #if 0	/* XXX proxy volumes??? */
12013a31b7ebSMike Smith 	ciss_printf(sc, "  %d logical drive%s configured\n",
12023a31b7ebSMike Smith 		    sc->ciss_id->configured_logical_drives,
12033a31b7ebSMike Smith 		    (sc->ciss_id->configured_logical_drives == 1) ? "" : "s");
1204c6131460SPaul Saab #endif
12053a31b7ebSMike Smith 	ciss_printf(sc, "  firmware %4.4s\n", sc->ciss_id->running_firmware_revision);
12063a31b7ebSMike Smith 	ciss_printf(sc, "  %d SCSI channels\n", sc->ciss_id->scsi_bus_count);
12073a31b7ebSMike Smith 
12083a31b7ebSMike Smith 	ciss_printf(sc, "  signature '%.4s'\n", sc->ciss_cfg->signature);
12093a31b7ebSMike Smith 	ciss_printf(sc, "  valence %d\n", sc->ciss_cfg->valence);
12103a31b7ebSMike Smith 	ciss_printf(sc, "  supported I/O methods 0x%b\n",
12113a31b7ebSMike Smith 		    sc->ciss_cfg->supported_methods,
12123a31b7ebSMike Smith 		    "\20\1READY\2simple\3performant\4MEMQ\n");
12133a31b7ebSMike Smith 	ciss_printf(sc, "  active I/O method 0x%b\n",
12143a31b7ebSMike Smith 		    sc->ciss_cfg->active_method, "\20\2simple\3performant\4MEMQ\n");
12153a31b7ebSMike Smith 	ciss_printf(sc, "  4G page base 0x%08x\n",
12163a31b7ebSMike Smith 		    sc->ciss_cfg->command_physlimit);
12173a31b7ebSMike Smith 	ciss_printf(sc, "  interrupt coalesce delay %dus\n",
12183a31b7ebSMike Smith 		    sc->ciss_cfg->interrupt_coalesce_delay);
12193a31b7ebSMike Smith 	ciss_printf(sc, "  interrupt coalesce count %d\n",
12203a31b7ebSMike Smith 		    sc->ciss_cfg->interrupt_coalesce_count);
12213a31b7ebSMike Smith 	ciss_printf(sc, "  max outstanding commands %d\n",
12223a31b7ebSMike Smith 		    sc->ciss_cfg->max_outstanding_commands);
12233a31b7ebSMike Smith 	ciss_printf(sc, "  bus types 0x%b\n", sc->ciss_cfg->bus_types,
12243a31b7ebSMike Smith 		    "\20\1ultra2\2ultra3\10fibre1\11fibre2\n");
12253a31b7ebSMike Smith 	ciss_printf(sc, "  server name '%.16s'\n", sc->ciss_cfg->server_name);
12263a31b7ebSMike Smith 	ciss_printf(sc, "  heartbeat 0x%x\n", sc->ciss_cfg->heartbeat);
12273a31b7ebSMike Smith     }
12283a31b7ebSMike Smith 
12293a31b7ebSMike Smith out:
12303a31b7ebSMike Smith     if (error) {
12313a31b7ebSMike Smith 	if (sc->ciss_id != NULL) {
12323a31b7ebSMike Smith 	    free(sc->ciss_id, CISS_MALLOC_CLASS);
12333a31b7ebSMike Smith 	    sc->ciss_id = NULL;
12343a31b7ebSMike Smith 	}
12353a31b7ebSMike Smith     }
12363a31b7ebSMike Smith     if (cr != NULL)
12373a31b7ebSMike Smith 	ciss_release_request(cr);
12383a31b7ebSMike Smith     return(error);
12393a31b7ebSMike Smith }
12403a31b7ebSMike Smith 
12413a31b7ebSMike Smith /************************************************************************
1242c6131460SPaul Saab  * Helper routine for generating a list of logical and physical luns.
12433a31b7ebSMike Smith  */
1244c6131460SPaul Saab static struct ciss_lun_report *
1245c6131460SPaul Saab ciss_report_luns(struct ciss_softc *sc, int opcode, int nunits)
12463a31b7ebSMike Smith {
12473a31b7ebSMike Smith     struct ciss_request		*cr;
12483a31b7ebSMike Smith     struct ciss_command		*cc;
12493a31b7ebSMike Smith     struct ciss_report_cdb	*crc;
12503a31b7ebSMike Smith     struct ciss_lun_report	*cll;
12513a31b7ebSMike Smith     int				command_status;
1252c6131460SPaul Saab     int				report_size;
1253c6131460SPaul Saab     int				error = 0;
12543a31b7ebSMike Smith 
12553a31b7ebSMike Smith     debug_called(1);
12563a31b7ebSMike Smith 
12573a31b7ebSMike Smith     cr = NULL;
12583a31b7ebSMike Smith     cll = NULL;
12593a31b7ebSMike Smith 
12603a31b7ebSMike Smith     /*
12613a31b7ebSMike Smith      * Get a request, allocate storage for the address list.
12623a31b7ebSMike Smith      */
12633a31b7ebSMike Smith     if ((error = ciss_get_request(sc, &cr)) != 0)
12643a31b7ebSMike Smith 	goto out;
1265c6131460SPaul Saab     report_size = sizeof(*cll) + nunits * sizeof(union ciss_device_address);
12663a31b7ebSMike Smith     if ((cll = malloc(report_size, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
1267c6131460SPaul Saab 	ciss_printf(sc, "can't allocate memory for lun report\n");
12683a31b7ebSMike Smith 	error = ENOMEM;
12693a31b7ebSMike Smith 	goto out;
12703a31b7ebSMike Smith     }
12713a31b7ebSMike Smith 
12723a31b7ebSMike Smith     /*
1273c6131460SPaul Saab      * Build the Report Logical/Physical LUNs command.
12743a31b7ebSMike Smith      */
12752fdaa90dSScott Long     cc = cr->cr_cc;
12763a31b7ebSMike Smith     cr->cr_data = cll;
12773a31b7ebSMike Smith     cr->cr_length = report_size;
12783a31b7ebSMike Smith     cr->cr_flags = CISS_REQ_DATAIN;
12793a31b7ebSMike Smith 
12803a31b7ebSMike Smith     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
12813a31b7ebSMike Smith     cc->header.address.physical.bus = 0;
12823a31b7ebSMike Smith     cc->header.address.physical.target = 0;
12833a31b7ebSMike Smith     cc->cdb.cdb_length = sizeof(*crc);
12843a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
12853a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
12863a31b7ebSMike Smith     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
12873a31b7ebSMike Smith     cc->cdb.timeout = 30;	/* XXX better suggestions? */
12883a31b7ebSMike Smith 
12893a31b7ebSMike Smith     crc = (struct ciss_report_cdb *)&(cc->cdb.cdb[0]);
12903a31b7ebSMike Smith     bzero(crc, sizeof(*crc));
1291c6131460SPaul Saab     crc->opcode = opcode;
12923a31b7ebSMike Smith     crc->length = htonl(report_size);			/* big-endian field */
12933a31b7ebSMike Smith     cll->list_size = htonl(report_size - sizeof(*cll));	/* big-endian field */
12943a31b7ebSMike Smith 
12953a31b7ebSMike Smith     /*
12963a31b7ebSMike Smith      * Submit the request and wait for it to complete.  (timeout
12973a31b7ebSMike Smith      * here should be much greater than above)
12983a31b7ebSMike Smith      */
12993a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1300c6131460SPaul Saab 	ciss_printf(sc, "error sending %d LUN command (%d)\n", opcode, error);
13013a31b7ebSMike Smith 	goto out;
13023a31b7ebSMike Smith     }
13033a31b7ebSMike Smith 
13043a31b7ebSMike Smith     /*
13053a31b7ebSMike Smith      * Check response.  Note that data over/underrun is OK.
13063a31b7ebSMike Smith      */
13073a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
13083a31b7ebSMike Smith     switch(command_status) {
13093a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:	/* buffer right size */
13103a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_UNDERRUN:	/* buffer too large, not bad */
13113a31b7ebSMike Smith 	break;
13123a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_OVERRUN:
1313c6131460SPaul Saab 	ciss_printf(sc, "WARNING: more units than driver limit (%d)\n",
13143a31b7ebSMike Smith 		    CISS_MAX_LOGICAL);
13153a31b7ebSMike Smith 	break;
13163a31b7ebSMike Smith     default:
13173a31b7ebSMike Smith 	ciss_printf(sc, "error detecting logical drive configuration (%s)\n",
13183a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
13193a31b7ebSMike Smith 	error = EIO;
13203a31b7ebSMike Smith 	goto out;
13213a31b7ebSMike Smith     }
13223a31b7ebSMike Smith     ciss_release_request(cr);
13233a31b7ebSMike Smith     cr = NULL;
13243a31b7ebSMike Smith 
1325c6131460SPaul Saab out:
1326c6131460SPaul Saab     if (cr != NULL)
1327c6131460SPaul Saab 	ciss_release_request(cr);
1328c6131460SPaul Saab     if (error && cll != NULL) {
1329c6131460SPaul Saab 	free(cll, CISS_MALLOC_CLASS);
1330c6131460SPaul Saab 	cll = NULL;
1331c6131460SPaul Saab     }
1332c6131460SPaul Saab     return(cll);
1333c6131460SPaul Saab }
1334c6131460SPaul Saab 
1335c6131460SPaul Saab /************************************************************************
1336c6131460SPaul Saab  * Find logical drives on the adapter.
1337c6131460SPaul Saab  */
1338c6131460SPaul Saab static int
1339c6131460SPaul Saab ciss_init_logical(struct ciss_softc *sc)
1340c6131460SPaul Saab {
1341c6131460SPaul Saab     struct ciss_lun_report	*cll;
1342c6131460SPaul Saab     int				error = 0, i, j;
1343c6131460SPaul Saab     int				ndrives;
1344c6131460SPaul Saab 
1345c6131460SPaul Saab     debug_called(1);
1346c6131460SPaul Saab 
1347c6131460SPaul Saab     cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
1348c6131460SPaul Saab 			   CISS_MAX_LOGICAL);
1349c6131460SPaul Saab     if (cll == NULL) {
1350c6131460SPaul Saab 	error = ENXIO;
1351c6131460SPaul Saab 	goto out;
1352c6131460SPaul Saab     }
1353c6131460SPaul Saab 
13543a31b7ebSMike Smith     /* sanity-check reply */
13553a31b7ebSMike Smith     ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1356777d1b39SPoul-Henning Kamp     if ((ndrives < 0) || (ndrives >= CISS_MAX_LOGICAL)) {
13573a31b7ebSMike Smith 	ciss_printf(sc, "adapter claims to report absurd number of logical drives (%d > %d)\n",
13583a31b7ebSMike Smith 		    ndrives, CISS_MAX_LOGICAL);
1359c6131460SPaul Saab 	error = ENXIO;
1360c6131460SPaul Saab 	goto out;
13613a31b7ebSMike Smith     }
13623a31b7ebSMike Smith 
13633a31b7ebSMike Smith     /*
13643a31b7ebSMike Smith      * Save logical drive information.
13653a31b7ebSMike Smith      */
1366c6131460SPaul Saab     if (bootverbose) {
1367c6131460SPaul Saab 	ciss_printf(sc, "%d logical drive%s\n",
1368c6131460SPaul Saab 	    ndrives, (ndrives > 1 || ndrives == 0) ? "s" : "");
1369c6131460SPaul Saab     }
1370c6131460SPaul Saab 
1371c6131460SPaul Saab     sc->ciss_logical =
13721fe6c4eeSScott Long 	malloc(sc->ciss_max_logical_bus * sizeof(struct ciss_ldrive *),
1373c6131460SPaul Saab 	       CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1374c6131460SPaul Saab     if (sc->ciss_logical == NULL) {
1375c6131460SPaul Saab 	error = ENXIO;
1376c6131460SPaul Saab 	goto out;
1377c6131460SPaul Saab     }
1378c6131460SPaul Saab 
13791fe6c4eeSScott Long     for (i = 0; i <= sc->ciss_max_logical_bus; i++) {
1380c6131460SPaul Saab 	sc->ciss_logical[i] =
1381c6131460SPaul Saab 	    malloc(CISS_MAX_LOGICAL * sizeof(struct ciss_ldrive),
1382c6131460SPaul Saab 		   CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1383c6131460SPaul Saab 	if (sc->ciss_logical[i] == NULL) {
1384c6131460SPaul Saab 	    error = ENXIO;
1385c6131460SPaul Saab 	    goto out;
1386c6131460SPaul Saab 	}
1387c6131460SPaul Saab 
1388c6131460SPaul Saab 	for (j = 0; j < CISS_MAX_LOGICAL; j++)
1389c6131460SPaul Saab 	    sc->ciss_logical[i][j].cl_status = CISS_LD_NONEXISTENT;
1390c6131460SPaul Saab     }
1391c6131460SPaul Saab 
1392c6131460SPaul Saab 
13933a31b7ebSMike Smith     for (i = 0; i < CISS_MAX_LOGICAL; i++) {
13943a31b7ebSMike Smith 	if (i < ndrives) {
1395c6131460SPaul Saab 	    struct ciss_ldrive	*ld;
1396c6131460SPaul Saab 	    int			bus, target;
1397c6131460SPaul Saab 
1398c6131460SPaul Saab 	    bus		= CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
1399c6131460SPaul Saab 	    target	= CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
1400c6131460SPaul Saab 	    ld		= &sc->ciss_logical[bus][target];
1401c6131460SPaul Saab 
1402c6131460SPaul Saab 	    ld->cl_address	= cll->lun[i];
1403c6131460SPaul Saab 	    ld->cl_controller	= &sc->ciss_controllers[bus];
1404c6131460SPaul Saab 	    if (ciss_identify_logical(sc, ld) != 0)
14053a31b7ebSMike Smith 		continue;
14063a31b7ebSMike Smith 	    /*
14073a31b7ebSMike Smith 	     * If the drive has had media exchanged, we should bring it online.
14083a31b7ebSMike Smith 	     */
1409c6131460SPaul Saab 	    if (ld->cl_lstatus->media_exchanged)
141088c78a38SPaul Saab 		ciss_accept_media(sc, ld);
14113a31b7ebSMike Smith 
14123a31b7ebSMike Smith 	}
14133a31b7ebSMike Smith     }
14143a31b7ebSMike Smith 
14153a31b7ebSMike Smith  out:
14163a31b7ebSMike Smith     if (cll != NULL)
14173a31b7ebSMike Smith 	free(cll, CISS_MALLOC_CLASS);
14183a31b7ebSMike Smith     return(error);
14193a31b7ebSMike Smith }
14203a31b7ebSMike Smith 
1421440baa08SPaul Saab static int
1422c6131460SPaul Saab ciss_init_physical(struct ciss_softc *sc)
1423c6131460SPaul Saab {
1424c6131460SPaul Saab     struct ciss_lun_report	*cll;
1425c6131460SPaul Saab     int				error = 0, i;
1426c6131460SPaul Saab     int				nphys;
14271fe6c4eeSScott Long     int				bus, target;
1428c6131460SPaul Saab 
1429c6131460SPaul Saab     debug_called(1);
1430c6131460SPaul Saab 
14311fe6c4eeSScott Long     bus = 0;
14321fe6c4eeSScott Long     target = 0;
14331fe6c4eeSScott Long 
1434c6131460SPaul Saab     cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
1435c6131460SPaul Saab 			   CISS_MAX_PHYSICAL);
1436c6131460SPaul Saab     if (cll == NULL) {
1437c6131460SPaul Saab 	error = ENXIO;
1438c6131460SPaul Saab 	goto out;
1439c6131460SPaul Saab     }
1440c6131460SPaul Saab 
1441c6131460SPaul Saab     nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1442c6131460SPaul Saab 
1443c6131460SPaul Saab     if (bootverbose) {
1444c6131460SPaul Saab 	ciss_printf(sc, "%d physical device%s\n",
1445c6131460SPaul Saab 	    nphys, (nphys > 1 || nphys == 0) ? "s" : "");
1446c6131460SPaul Saab     }
1447c6131460SPaul Saab 
1448c6131460SPaul Saab     /*
14491fe6c4eeSScott Long      * Figure out the bus mapping.
14501fe6c4eeSScott Long      * Logical buses include both the local logical bus for local arrays and
14511fe6c4eeSScott Long      * proxy buses for remote arrays.  Physical buses are numbered by the
14521fe6c4eeSScott Long      * controller and represent physical buses that hold physical devices.
14531fe6c4eeSScott Long      * We shift these bus numbers so that everything fits into a single flat
14541fe6c4eeSScott Long      * numbering space for CAM.  Logical buses occupy the first 32 CAM bus
14551fe6c4eeSScott Long      * numbers, and the physical bus numbers are shifted to be above that.
14561fe6c4eeSScott Long      * This results in the various driver arrays being indexed as follows:
14571fe6c4eeSScott Long      *
14581fe6c4eeSScott Long      * ciss_controllers[] - indexed by logical bus
14591fe6c4eeSScott Long      * ciss_cam_sim[]     - indexed by both logical and physical, with physical
14601fe6c4eeSScott Long      *                      being shifted by 32.
14611fe6c4eeSScott Long      * ciss_logical[][]   - indexed by logical bus
14621fe6c4eeSScott Long      * ciss_physical[][]  - indexed by physical bus
14631fe6c4eeSScott Long      *
14641fe6c4eeSScott Long      * XXX This is getting more and more hackish.  CISS really doesn't play
14651fe6c4eeSScott Long      *     well with a standard SCSI model; devices are addressed via magic
14661fe6c4eeSScott Long      *     cookies, not via b/t/l addresses.  Since there is no way to store
14671fe6c4eeSScott Long      *     the cookie in the CAM device object, we have to keep these lookup
14681fe6c4eeSScott Long      *     tables handy so that the devices can be found quickly at the cost
14691fe6c4eeSScott Long      *     of wasting memory and having a convoluted lookup scheme.  This
14701fe6c4eeSScott Long      *     driver should probably be converted to block interface.
14711fe6c4eeSScott Long      */
14721fe6c4eeSScott Long     /*
1473c6131460SPaul Saab      * If the L2 and L3 SCSI addresses are 0, this signifies a proxy
1474c6131460SPaul Saab      * controller. A proxy controller is another physical controller
1475c6131460SPaul Saab      * behind the primary PCI controller. We need to know about this
1476c6131460SPaul Saab      * so that BMIC commands can be properly targeted.  There can be
1477c6131460SPaul Saab      * proxy controllers attached to a single PCI controller, so
1478c6131460SPaul Saab      * find the highest numbered one so the array can be properly
1479c6131460SPaul Saab      * sized.
1480c6131460SPaul Saab      */
14811fe6c4eeSScott Long     sc->ciss_max_logical_bus = 1;
1482c6131460SPaul Saab     for (i = 0; i < nphys; i++) {
1483c6131460SPaul Saab 	if (cll->lun[i].physical.extra_address == 0) {
14841fe6c4eeSScott Long 	    bus = cll->lun[i].physical.bus;
14851fe6c4eeSScott Long 	    sc->ciss_max_logical_bus = max(sc->ciss_max_logical_bus, bus) + 1;
14861fe6c4eeSScott Long 	} else {
14871fe6c4eeSScott Long 	    bus = CISS_EXTRA_BUS2(cll->lun[i].physical.extra_address);
14881fe6c4eeSScott Long 	    sc->ciss_max_physical_bus = max(sc->ciss_max_physical_bus, bus);
1489c6131460SPaul Saab 	}
1490c6131460SPaul Saab     }
1491c6131460SPaul Saab 
1492c6131460SPaul Saab     sc->ciss_controllers =
14931fe6c4eeSScott Long 	malloc(sc->ciss_max_logical_bus * sizeof (union ciss_device_address),
1494c6131460SPaul Saab 	       CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1495c6131460SPaul Saab 
1496c6131460SPaul Saab     if (sc->ciss_controllers == NULL) {
1497c6131460SPaul Saab 	ciss_printf(sc, "Could not allocate memory for controller map\n");
1498c6131460SPaul Saab 	error = ENOMEM;
1499c6131460SPaul Saab 	goto out;
1500c6131460SPaul Saab     }
1501c6131460SPaul Saab 
1502c6131460SPaul Saab     /* setup a map of controller addresses */
1503c6131460SPaul Saab     for (i = 0; i < nphys; i++) {
1504c6131460SPaul Saab 	if (cll->lun[i].physical.extra_address == 0) {
1505c6131460SPaul Saab 	    sc->ciss_controllers[cll->lun[i].physical.bus] = cll->lun[i];
1506c6131460SPaul Saab 	}
1507c6131460SPaul Saab     }
1508c6131460SPaul Saab 
15091fe6c4eeSScott Long     sc->ciss_physical =
15101fe6c4eeSScott Long 	malloc(sc->ciss_max_physical_bus * sizeof(struct ciss_pdrive *),
15111fe6c4eeSScott Long 	       CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
15121fe6c4eeSScott Long     if (sc->ciss_physical == NULL) {
15131fe6c4eeSScott Long 	ciss_printf(sc, "Could not allocate memory for physical device map\n");
15141fe6c4eeSScott Long 	error = ENOMEM;
15151fe6c4eeSScott Long 	goto out;
15161fe6c4eeSScott Long     }
15171fe6c4eeSScott Long 
15181fe6c4eeSScott Long     for (i = 0; i < sc->ciss_max_physical_bus; i++) {
15191fe6c4eeSScott Long 	sc->ciss_physical[i] =
15201fe6c4eeSScott Long 	    malloc(sizeof(struct ciss_pdrive) * CISS_MAX_PHYSTGT,
15211fe6c4eeSScott Long 		   CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
15221fe6c4eeSScott Long 	if (sc->ciss_physical[i] == NULL) {
15231fe6c4eeSScott Long 	    ciss_printf(sc, "Could not allocate memory for target map\n");
15241fe6c4eeSScott Long 	    error = ENOMEM;
15251fe6c4eeSScott Long 	    goto out;
15261fe6c4eeSScott Long 	}
15271fe6c4eeSScott Long     }
15281fe6c4eeSScott Long 
15291fe6c4eeSScott Long     ciss_filter_physical(sc, cll);
15301fe6c4eeSScott Long 
1531c6131460SPaul Saab out:
1532c6131460SPaul Saab     if (cll != NULL)
1533c6131460SPaul Saab 	free(cll, CISS_MALLOC_CLASS);
1534c6131460SPaul Saab 
1535c6131460SPaul Saab     return(error);
1536c6131460SPaul Saab }
1537c6131460SPaul Saab 
1538c6131460SPaul Saab static int
15391fe6c4eeSScott Long ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll)
15401fe6c4eeSScott Long {
15411fe6c4eeSScott Long     u_int32_t ea;
15421fe6c4eeSScott Long     int i, nphys;
15431fe6c4eeSScott Long     int	bus, target;
15441fe6c4eeSScott Long 
15451fe6c4eeSScott Long     nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
15461fe6c4eeSScott Long     for (i = 0; i < nphys; i++) {
15471fe6c4eeSScott Long 	if (cll->lun[i].physical.extra_address == 0)
15481fe6c4eeSScott Long 	    continue;
15491fe6c4eeSScott Long 
15501fe6c4eeSScott Long 	/*
15511fe6c4eeSScott Long 	 * Filter out devices that we don't want.  Level 3 LUNs could
15521fe6c4eeSScott Long 	 * probably be supported, but the docs don't give enough of a
15531fe6c4eeSScott Long 	 * hint to know how.
15541fe6c4eeSScott Long 	 *
15551fe6c4eeSScott Long 	 * The mode field of the physical address is likely set to have
15561fe6c4eeSScott Long 	 * hard disks masked out.  Honor it unless the user has overridden
15571fe6c4eeSScott Long 	 * us with the tunable.  We also munge the inquiry data for these
15581fe6c4eeSScott Long 	 * disks so that they only show up as passthrough devices.  Keeping
15591fe6c4eeSScott Long 	 * them visible in this fashion is useful for doing things like
15601fe6c4eeSScott Long 	 * flashing firmware.
15611fe6c4eeSScott Long 	 */
15621fe6c4eeSScott Long 	ea = cll->lun[i].physical.extra_address;
15631fe6c4eeSScott Long 	if ((CISS_EXTRA_BUS3(ea) != 0) || (CISS_EXTRA_TARGET3(ea) != 0) ||
15641fe6c4eeSScott Long 	    (CISS_EXTRA_MODE2(ea) == 0x3))
15651fe6c4eeSScott Long 	    continue;
15661fe6c4eeSScott Long 	if ((ciss_expose_hidden_physical == 0) &&
15671fe6c4eeSScott Long 	   (cll->lun[i].physical.mode == CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL))
15681fe6c4eeSScott Long 	    continue;
15691fe6c4eeSScott Long 
15701fe6c4eeSScott Long 	/*
15711fe6c4eeSScott Long 	 * Note: CISS firmware numbers physical busses starting at '1', not
15721fe6c4eeSScott Long 	 *       '0'.  This numbering is internal to the firmware and is only
15731fe6c4eeSScott Long 	 *       used as a hint here.
15741fe6c4eeSScott Long 	 */
15751fe6c4eeSScott Long 	bus = CISS_EXTRA_BUS2(ea) - 1;
15761fe6c4eeSScott Long 	target = CISS_EXTRA_TARGET2(ea);
15771fe6c4eeSScott Long 	sc->ciss_physical[bus][target].cp_address = cll->lun[i];
15781fe6c4eeSScott Long 	sc->ciss_physical[bus][target].cp_online = 1;
15791fe6c4eeSScott Long     }
15801fe6c4eeSScott Long 
15811fe6c4eeSScott Long     return (0);
15821fe6c4eeSScott Long }
15831fe6c4eeSScott Long 
15841fe6c4eeSScott Long static int
1585440baa08SPaul Saab ciss_inquiry_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1586440baa08SPaul Saab {
1587440baa08SPaul Saab     struct ciss_request			*cr;
1588440baa08SPaul Saab     struct ciss_command			*cc;
1589440baa08SPaul Saab     struct scsi_inquiry			*inq;
1590440baa08SPaul Saab     int					error;
1591440baa08SPaul Saab     int					command_status;
1592440baa08SPaul Saab 
1593440baa08SPaul Saab     cr = NULL;
1594440baa08SPaul Saab 
1595440baa08SPaul Saab     bzero(&ld->cl_geometry, sizeof(ld->cl_geometry));
1596440baa08SPaul Saab 
1597440baa08SPaul Saab     if ((error = ciss_get_request(sc, &cr)) != 0)
1598440baa08SPaul Saab 	goto out;
1599440baa08SPaul Saab 
16002fdaa90dSScott Long     cc = cr->cr_cc;
1601440baa08SPaul Saab     cr->cr_data = &ld->cl_geometry;
1602440baa08SPaul Saab     cr->cr_length = sizeof(ld->cl_geometry);
1603440baa08SPaul Saab     cr->cr_flags = CISS_REQ_DATAIN;
1604440baa08SPaul Saab 
1605c6131460SPaul Saab     cc->header.address = ld->cl_address;
1606440baa08SPaul Saab     cc->cdb.cdb_length = 6;
1607440baa08SPaul Saab     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1608440baa08SPaul Saab     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1609440baa08SPaul Saab     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1610440baa08SPaul Saab     cc->cdb.timeout = 30;
1611440baa08SPaul Saab 
1612440baa08SPaul Saab     inq = (struct scsi_inquiry *)&(cc->cdb.cdb[0]);
1613440baa08SPaul Saab     inq->opcode = INQUIRY;
1614440baa08SPaul Saab     inq->byte2 = SI_EVPD;
1615440baa08SPaul Saab     inq->page_code = CISS_VPD_LOGICAL_DRIVE_GEOMETRY;
1616440baa08SPaul Saab     inq->length = sizeof(ld->cl_geometry);
1617440baa08SPaul Saab 
1618440baa08SPaul Saab     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1619440baa08SPaul Saab 	ciss_printf(sc, "error getting geometry (%d)\n", error);
1620440baa08SPaul Saab 	goto out;
1621440baa08SPaul Saab     }
1622440baa08SPaul Saab 
1623440baa08SPaul Saab     ciss_report_request(cr, &command_status, NULL);
1624440baa08SPaul Saab     switch(command_status) {
1625440baa08SPaul Saab     case CISS_CMD_STATUS_SUCCESS:
1626440baa08SPaul Saab     case CISS_CMD_STATUS_DATA_UNDERRUN:
1627440baa08SPaul Saab 	break;
1628440baa08SPaul Saab     case CISS_CMD_STATUS_DATA_OVERRUN:
1629440baa08SPaul Saab 	ciss_printf(sc, "WARNING: Data overrun\n");
1630440baa08SPaul Saab 	break;
1631440baa08SPaul Saab     default:
1632440baa08SPaul Saab 	ciss_printf(sc, "Error detecting logical drive geometry (%s)\n",
1633440baa08SPaul Saab 		    ciss_name_command_status(command_status));
1634440baa08SPaul Saab 	break;
1635440baa08SPaul Saab     }
1636440baa08SPaul Saab 
1637440baa08SPaul Saab out:
1638440baa08SPaul Saab     if (cr != NULL)
1639440baa08SPaul Saab 	ciss_release_request(cr);
1640440baa08SPaul Saab     return(error);
1641440baa08SPaul Saab }
16423a31b7ebSMike Smith /************************************************************************
16433a31b7ebSMike Smith  * Identify a logical drive, initialise state related to it.
16443a31b7ebSMike Smith  */
16453a31b7ebSMike Smith static int
16463a31b7ebSMike Smith ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
16473a31b7ebSMike Smith {
16483a31b7ebSMike Smith     struct ciss_request		*cr;
16493a31b7ebSMike Smith     struct ciss_command		*cc;
16503a31b7ebSMike Smith     struct ciss_bmic_cdb	*cbc;
16513a31b7ebSMike Smith     int				error, command_status;
16523a31b7ebSMike Smith 
16533a31b7ebSMike Smith     debug_called(1);
16543a31b7ebSMike Smith 
16553a31b7ebSMike Smith     cr = NULL;
16563a31b7ebSMike Smith 
16573a31b7ebSMike Smith     /*
16583a31b7ebSMike Smith      * Build a BMIC request to fetch the drive ID.
16593a31b7ebSMike Smith      */
16603a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LDRIVE,
16613a31b7ebSMike Smith 				       (void **)&ld->cl_ldrive,
16623a31b7ebSMike Smith 				       sizeof(*ld->cl_ldrive))) != 0)
16633a31b7ebSMike Smith 	goto out;
16642fdaa90dSScott Long     cc = cr->cr_cc;
1665c6131460SPaul Saab     cc->header.address = *ld->cl_controller;	/* target controller */
16663a31b7ebSMike Smith     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1667c6131460SPaul Saab     cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
16683a31b7ebSMike Smith 
16693a31b7ebSMike Smith     /*
16703a31b7ebSMike Smith      * Submit the request and wait for it to complete.
16713a31b7ebSMike Smith      */
16723a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
16733a31b7ebSMike Smith 	ciss_printf(sc, "error sending BMIC LDRIVE command (%d)\n", error);
16743a31b7ebSMike Smith 	goto out;
16753a31b7ebSMike Smith     }
16763a31b7ebSMike Smith 
16773a31b7ebSMike Smith     /*
16783a31b7ebSMike Smith      * Check response.
16793a31b7ebSMike Smith      */
16803a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
16813a31b7ebSMike Smith     switch(command_status) {
16823a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
16833a31b7ebSMike Smith 	break;
16843a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_UNDERRUN:
16853a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_OVERRUN:
16863a31b7ebSMike Smith 	ciss_printf(sc, "data over/underrun reading logical drive ID\n");
16873a31b7ebSMike Smith     default:
16883a31b7ebSMike Smith 	ciss_printf(sc, "error reading logical drive ID (%s)\n",
16893a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
16903a31b7ebSMike Smith 	error = EIO;
16913a31b7ebSMike Smith 	goto out;
16923a31b7ebSMike Smith     }
16933a31b7ebSMike Smith     ciss_release_request(cr);
16943a31b7ebSMike Smith     cr = NULL;
16953a31b7ebSMike Smith 
16963a31b7ebSMike Smith     /*
16973a31b7ebSMike Smith      * Build a CISS BMIC command to get the logical drive status.
16983a31b7ebSMike Smith      */
16993a31b7ebSMike Smith     if ((error = ciss_get_ldrive_status(sc, ld)) != 0)
17003a31b7ebSMike Smith 	goto out;
17013a31b7ebSMike Smith 
17023a31b7ebSMike Smith     /*
1703440baa08SPaul Saab      * Get the logical drive geometry.
1704440baa08SPaul Saab      */
1705440baa08SPaul Saab     if ((error = ciss_inquiry_logical(sc, ld)) != 0)
1706440baa08SPaul Saab 	goto out;
1707440baa08SPaul Saab 
1708440baa08SPaul Saab     /*
17093a31b7ebSMike Smith      * Print the drive's basic characteristics.
17103a31b7ebSMike Smith      */
17114bca277bSPaul Saab     if (bootverbose) {
1712c6131460SPaul Saab 	ciss_printf(sc, "logical drive (b%dt%d): %s, %dMB ",
1713c6131460SPaul Saab 		    CISS_LUN_TO_BUS(ld->cl_address.logical.lun),
1714c6131460SPaul Saab 		    CISS_LUN_TO_TARGET(ld->cl_address.logical.lun),
171579adbf76SPaul Saab 		    ciss_name_ldrive_org(ld->cl_ldrive->fault_tolerance),
17163a31b7ebSMike Smith 		    ((ld->cl_ldrive->blocks_available / (1024 * 1024)) *
17173a31b7ebSMike Smith 		     ld->cl_ldrive->block_size));
17183a31b7ebSMike Smith 
17193a31b7ebSMike Smith 	ciss_print_ldrive(sc, ld);
17203a31b7ebSMike Smith     }
17213a31b7ebSMike Smith out:
17223a31b7ebSMike Smith     if (error != 0) {
17233a31b7ebSMike Smith 	/* make the drive not-exist */
17243a31b7ebSMike Smith 	ld->cl_status = CISS_LD_NONEXISTENT;
17253a31b7ebSMike Smith 	if (ld->cl_ldrive != NULL) {
17263a31b7ebSMike Smith 	    free(ld->cl_ldrive, CISS_MALLOC_CLASS);
17273a31b7ebSMike Smith 	    ld->cl_ldrive = NULL;
17283a31b7ebSMike Smith 	}
17293a31b7ebSMike Smith 	if (ld->cl_lstatus != NULL) {
17303a31b7ebSMike Smith 	    free(ld->cl_lstatus, CISS_MALLOC_CLASS);
17313a31b7ebSMike Smith 	    ld->cl_lstatus = NULL;
17323a31b7ebSMike Smith 	}
17333a31b7ebSMike Smith     }
17343a31b7ebSMike Smith     if (cr != NULL)
17353a31b7ebSMike Smith 	ciss_release_request(cr);
17363a31b7ebSMike Smith 
17373a31b7ebSMike Smith     return(error);
17383a31b7ebSMike Smith }
17393a31b7ebSMike Smith 
17403a31b7ebSMike Smith /************************************************************************
17413a31b7ebSMike Smith  * Get status for a logical drive.
17423a31b7ebSMike Smith  *
17433a31b7ebSMike Smith  * XXX should we also do this in response to Test Unit Ready?
17443a31b7ebSMike Smith  */
17453a31b7ebSMike Smith static int
17463a31b7ebSMike Smith ciss_get_ldrive_status(struct ciss_softc *sc,  struct ciss_ldrive *ld)
17473a31b7ebSMike Smith {
17483a31b7ebSMike Smith     struct ciss_request		*cr;
17493a31b7ebSMike Smith     struct ciss_command		*cc;
17503a31b7ebSMike Smith     struct ciss_bmic_cdb	*cbc;
17513a31b7ebSMike Smith     int				error, command_status;
17523a31b7ebSMike Smith 
17533a31b7ebSMike Smith     /*
17543a31b7ebSMike Smith      * Build a CISS BMIC command to get the logical drive status.
17553a31b7ebSMike Smith      */
17563a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LSTATUS,
17573a31b7ebSMike Smith 				       (void **)&ld->cl_lstatus,
17583a31b7ebSMike Smith 				       sizeof(*ld->cl_lstatus))) != 0)
17593a31b7ebSMike Smith 	goto out;
17602fdaa90dSScott Long     cc = cr->cr_cc;
1761c6131460SPaul Saab     cc->header.address = *ld->cl_controller;	/* target controller */
17623a31b7ebSMike Smith     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1763c6131460SPaul Saab     cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
17643a31b7ebSMike Smith 
17653a31b7ebSMike Smith     /*
17663a31b7ebSMike Smith      * Submit the request and wait for it to complete.
17673a31b7ebSMike Smith      */
17683a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
17693a31b7ebSMike Smith 	ciss_printf(sc, "error sending BMIC LSTATUS command (%d)\n", error);
17703a31b7ebSMike Smith 	goto out;
17713a31b7ebSMike Smith     }
17723a31b7ebSMike Smith 
17733a31b7ebSMike Smith     /*
17743a31b7ebSMike Smith      * Check response.
17753a31b7ebSMike Smith      */
17763a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
17773a31b7ebSMike Smith     switch(command_status) {
17783a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
17793a31b7ebSMike Smith 	break;
17803a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_UNDERRUN:
17813a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_OVERRUN:
17823a31b7ebSMike Smith 	ciss_printf(sc, "data over/underrun reading logical drive status\n");
17833a31b7ebSMike Smith     default:
17843a31b7ebSMike Smith 	ciss_printf(sc, "error reading logical drive status (%s)\n",
17853a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
17863a31b7ebSMike Smith 	error = EIO;
17873a31b7ebSMike Smith 	goto out;
17883a31b7ebSMike Smith     }
17893a31b7ebSMike Smith 
17903a31b7ebSMike Smith     /*
17913a31b7ebSMike Smith      * Set the drive's summary status based on the returned status.
17923a31b7ebSMike Smith      *
17933a31b7ebSMike Smith      * XXX testing shows that a failed JBOD drive comes back at next
17943a31b7ebSMike Smith      * boot in "queued for expansion" mode.  WTF?
17953a31b7ebSMike Smith      */
17963a31b7ebSMike Smith     ld->cl_status = ciss_decode_ldrive_status(ld->cl_lstatus->status);
17973a31b7ebSMike Smith 
17983a31b7ebSMike Smith out:
17993a31b7ebSMike Smith     if (cr != NULL)
18003a31b7ebSMike Smith 	ciss_release_request(cr);
18013a31b7ebSMike Smith     return(error);
18023a31b7ebSMike Smith }
18033a31b7ebSMike Smith 
18043a31b7ebSMike Smith /************************************************************************
18053a31b7ebSMike Smith  * Notify the adapter of a config update.
18063a31b7ebSMike Smith  */
18073a31b7ebSMike Smith static int
18083a31b7ebSMike Smith ciss_update_config(struct ciss_softc *sc)
18093a31b7ebSMike Smith {
18103a31b7ebSMike Smith     int		i;
18113a31b7ebSMike Smith 
18123a31b7ebSMike Smith     debug_called(1);
18133a31b7ebSMike Smith 
18143a31b7ebSMike Smith     CISS_TL_SIMPLE_WRITE(sc, CISS_TL_SIMPLE_IDBR, CISS_TL_SIMPLE_IDBR_CFG_TABLE);
18153a31b7ebSMike Smith     for (i = 0; i < 1000; i++) {
18163a31b7ebSMike Smith 	if (!(CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR) &
18173a31b7ebSMike Smith 	      CISS_TL_SIMPLE_IDBR_CFG_TABLE)) {
18183a31b7ebSMike Smith 	    return(0);
18193a31b7ebSMike Smith 	}
18203a31b7ebSMike Smith 	DELAY(1000);
18213a31b7ebSMike Smith     }
18223a31b7ebSMike Smith     return(1);
18233a31b7ebSMike Smith }
18243a31b7ebSMike Smith 
18253a31b7ebSMike Smith /************************************************************************
18263a31b7ebSMike Smith  * Accept new media into a logical drive.
18273a31b7ebSMike Smith  *
18283a31b7ebSMike Smith  * XXX The drive has previously been offline; it would be good if we
18293a31b7ebSMike Smith  *     could make sure it's not open right now.
18303a31b7ebSMike Smith  */
18313a31b7ebSMike Smith static int
183288c78a38SPaul Saab ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld)
18333a31b7ebSMike Smith {
18343a31b7ebSMike Smith     struct ciss_request		*cr;
18353a31b7ebSMike Smith     struct ciss_command		*cc;
18363a31b7ebSMike Smith     struct ciss_bmic_cdb	*cbc;
183788c78a38SPaul Saab     int				command_status;
183888c78a38SPaul Saab     int				error = 0, ldrive;
1839609caf8dSPaul Saab 
1840609caf8dSPaul Saab     ldrive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
18413a31b7ebSMike Smith 
184288c78a38SPaul Saab     debug(0, "bringing logical drive %d back online");
18433a31b7ebSMike Smith 
18443a31b7ebSMike Smith     /*
18453a31b7ebSMike Smith      * Build a CISS BMIC command to bring the drive back online.
18463a31b7ebSMike Smith      */
18473a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ACCEPT_MEDIA,
18483a31b7ebSMike Smith 				       NULL, 0)) != 0)
18493a31b7ebSMike Smith 	goto out;
18502fdaa90dSScott Long     cc = cr->cr_cc;
1851c6131460SPaul Saab     cc->header.address = *ld->cl_controller;	/* target controller */
18523a31b7ebSMike Smith     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1853609caf8dSPaul Saab     cbc->log_drive = ldrive;
18543a31b7ebSMike Smith 
18553a31b7ebSMike Smith     /*
18563a31b7ebSMike Smith      * Submit the request and wait for it to complete.
18573a31b7ebSMike Smith      */
18583a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
185988c78a38SPaul Saab 	ciss_printf(sc, "error sending BMIC ACCEPT MEDIA command (%d)\n", error);
18603a31b7ebSMike Smith 	goto out;
18613a31b7ebSMike Smith     }
18623a31b7ebSMike Smith 
18633a31b7ebSMike Smith     /*
18643a31b7ebSMike Smith      * Check response.
18653a31b7ebSMike Smith      */
18663a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
18673a31b7ebSMike Smith     switch(command_status) {
18683a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:		/* all OK */
18693a31b7ebSMike Smith 	/* we should get a logical drive status changed event here */
18703a31b7ebSMike Smith 	break;
18713a31b7ebSMike Smith     default:
18723a31b7ebSMike Smith 	ciss_printf(cr->cr_sc, "error accepting media into failed logical drive (%s)\n",
18733a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
18743a31b7ebSMike Smith 	break;
18753a31b7ebSMike Smith     }
187688c78a38SPaul Saab 
187788c78a38SPaul Saab out:
187888c78a38SPaul Saab     if (cr != NULL)
18793a31b7ebSMike Smith 	ciss_release_request(cr);
188088c78a38SPaul Saab     return(error);
18813a31b7ebSMike Smith }
18823a31b7ebSMike Smith 
18833a31b7ebSMike Smith /************************************************************************
18843a31b7ebSMike Smith  * Release adapter resources.
18853a31b7ebSMike Smith  */
18863a31b7ebSMike Smith static void
18873a31b7ebSMike Smith ciss_free(struct ciss_softc *sc)
18883a31b7ebSMike Smith {
18893284b9eeSPaul Saab     struct ciss_request *cr;
1890ee626b40SPaul Saab     int			i, j;
18913284b9eeSPaul Saab 
18923a31b7ebSMike Smith     debug_called(1);
18933a31b7ebSMike Smith 
18943a31b7ebSMike Smith     /* we're going away */
18953a31b7ebSMike Smith     sc->ciss_flags |= CISS_FLAG_ABORTING;
18963a31b7ebSMike Smith 
18973a31b7ebSMike Smith     /* terminate the periodic heartbeat routine */
18982472e51eSScott Long     callout_stop(&sc->ciss_periodic);
18993a31b7ebSMike Smith 
19003a31b7ebSMike Smith     /* cancel the Event Notify chain */
19013a31b7ebSMike Smith     ciss_notify_abort(sc);
19023a31b7ebSMike Smith 
1903c6131460SPaul Saab     ciss_kill_notify_thread(sc);
1904c6131460SPaul Saab 
19052472e51eSScott Long     /* disconnect from CAM */
19062472e51eSScott Long     if (sc->ciss_cam_sim) {
19072472e51eSScott Long 	for (i = 0; i < sc->ciss_max_logical_bus; i++) {
19082472e51eSScott Long 	    if (sc->ciss_cam_sim[i]) {
19092472e51eSScott Long 		xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
19102472e51eSScott Long 		cam_sim_free(sc->ciss_cam_sim[i], 0);
19112472e51eSScott Long 	    }
19122472e51eSScott Long 	}
19132472e51eSScott Long 	for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
19142472e51eSScott Long 	     CISS_PHYSICAL_BASE; i++) {
19152472e51eSScott Long 	    if (sc->ciss_cam_sim[i]) {
19162472e51eSScott Long 		xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
19172472e51eSScott Long 		cam_sim_free(sc->ciss_cam_sim[i], 0);
19182472e51eSScott Long 	    }
19192472e51eSScott Long 	}
19202472e51eSScott Long 	free(sc->ciss_cam_sim, CISS_MALLOC_CLASS);
19212472e51eSScott Long     }
19222472e51eSScott Long     if (sc->ciss_cam_devq)
19232472e51eSScott Long 	cam_simq_free(sc->ciss_cam_devq);
19242472e51eSScott Long 
192578d03361SPaul Saab     /* remove the control device */
19262472e51eSScott Long     mtx_unlock(&sc->ciss_mtx);
192778d03361SPaul Saab     if (sc->ciss_dev_t != NULL)
192878d03361SPaul Saab 	destroy_dev(sc->ciss_dev_t);
192978d03361SPaul Saab 
19302472e51eSScott Long     /* Final cleanup of the callout. */
19312472e51eSScott Long     callout_drain(&sc->ciss_periodic);
19322472e51eSScott Long     mtx_destroy(&sc->ciss_mtx);
19332472e51eSScott Long 
19343a31b7ebSMike Smith     /* free the controller data */
19353a31b7ebSMike Smith     if (sc->ciss_id != NULL)
19363a31b7ebSMike Smith 	free(sc->ciss_id, CISS_MALLOC_CLASS);
19373a31b7ebSMike Smith 
19383a31b7ebSMike Smith     /* release I/O resources */
19393a31b7ebSMike Smith     if (sc->ciss_regs_resource != NULL)
19403a31b7ebSMike Smith 	bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
19413a31b7ebSMike Smith 			     sc->ciss_regs_rid, sc->ciss_regs_resource);
19423a31b7ebSMike Smith     if (sc->ciss_cfg_resource != NULL)
19433a31b7ebSMike Smith 	bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
19443a31b7ebSMike Smith 			     sc->ciss_cfg_rid, sc->ciss_cfg_resource);
19453a31b7ebSMike Smith     if (sc->ciss_intr != NULL)
19463a31b7ebSMike Smith 	bus_teardown_intr(sc->ciss_dev, sc->ciss_irq_resource, sc->ciss_intr);
19473a31b7ebSMike Smith     if (sc->ciss_irq_resource != NULL)
19483a31b7ebSMike Smith 	bus_release_resource(sc->ciss_dev, SYS_RES_IRQ,
194922657ce1SScott Long 			     sc->ciss_irq_rid[0], sc->ciss_irq_resource);
195022657ce1SScott Long     if (sc->ciss_msi)
195122657ce1SScott Long 	pci_release_msi(sc->ciss_dev);
19523284b9eeSPaul Saab 
19533284b9eeSPaul Saab     while ((cr = ciss_dequeue_free(sc)) != NULL)
19543284b9eeSPaul Saab 	bus_dmamap_destroy(sc->ciss_buffer_dmat, cr->cr_datamap);
19553a31b7ebSMike Smith     if (sc->ciss_buffer_dmat)
19563a31b7ebSMike Smith 	bus_dma_tag_destroy(sc->ciss_buffer_dmat);
19573a31b7ebSMike Smith 
19583a31b7ebSMike Smith     /* destroy command memory and DMA tag */
19593a31b7ebSMike Smith     if (sc->ciss_command != NULL) {
19603a31b7ebSMike Smith 	bus_dmamap_unload(sc->ciss_command_dmat, sc->ciss_command_map);
19613a31b7ebSMike Smith 	bus_dmamem_free(sc->ciss_command_dmat, sc->ciss_command, sc->ciss_command_map);
19623a31b7ebSMike Smith     }
19633284b9eeSPaul Saab     if (sc->ciss_command_dmat)
19643a31b7ebSMike Smith 	bus_dma_tag_destroy(sc->ciss_command_dmat);
19653a31b7ebSMike Smith 
196622657ce1SScott Long     if (sc->ciss_reply) {
196722657ce1SScott Long 	bus_dmamap_unload(sc->ciss_reply_dmat, sc->ciss_reply_map);
196822657ce1SScott Long 	bus_dmamem_free(sc->ciss_reply_dmat, sc->ciss_reply, sc->ciss_reply_map);
196922657ce1SScott Long     }
197022657ce1SScott Long     if (sc->ciss_reply_dmat)
197122657ce1SScott Long 	bus_dma_tag_destroy(sc->ciss_reply_dmat);
197222657ce1SScott Long 
197322657ce1SScott Long     /* destroy DMA tags */
197422657ce1SScott Long     if (sc->ciss_parent_dmat)
197522657ce1SScott Long 	bus_dma_tag_destroy(sc->ciss_parent_dmat);
1976c6131460SPaul Saab     if (sc->ciss_logical) {
1977ee626b40SPaul Saab 	for (i = 0; i <= sc->ciss_max_logical_bus; i++) {
1978ee626b40SPaul Saab 	    for (j = 0; j < CISS_MAX_LOGICAL; j++) {
1979ee626b40SPaul Saab 		if (sc->ciss_logical[i][j].cl_ldrive)
1980ee626b40SPaul Saab 		    free(sc->ciss_logical[i][j].cl_ldrive, CISS_MALLOC_CLASS);
1981ee626b40SPaul Saab 		if (sc->ciss_logical[i][j].cl_lstatus)
1982ee626b40SPaul Saab 		    free(sc->ciss_logical[i][j].cl_lstatus, CISS_MALLOC_CLASS);
1983ee626b40SPaul Saab 	    }
1984c6131460SPaul Saab 	    free(sc->ciss_logical[i], CISS_MALLOC_CLASS);
1985ee626b40SPaul Saab 	}
1986c6131460SPaul Saab 	free(sc->ciss_logical, CISS_MALLOC_CLASS);
1987c6131460SPaul Saab     }
1988c6131460SPaul Saab 
19891fe6c4eeSScott Long     if (sc->ciss_physical) {
19901fe6c4eeSScott Long 	for (i = 0; i < sc->ciss_max_physical_bus; i++)
19911fe6c4eeSScott Long 	    free(sc->ciss_physical[i], CISS_MALLOC_CLASS);
19921fe6c4eeSScott Long 	free(sc->ciss_physical, CISS_MALLOC_CLASS);
19931fe6c4eeSScott Long     }
19941fe6c4eeSScott Long 
1995c6131460SPaul Saab     if (sc->ciss_controllers)
1996c6131460SPaul Saab 	free(sc->ciss_controllers, CISS_MALLOC_CLASS);
1997975b7318SScott Long 
19983a31b7ebSMike Smith }
19993a31b7ebSMike Smith 
20003a31b7ebSMike Smith /************************************************************************
20013a31b7ebSMike Smith  * Give a command to the adapter.
20023a31b7ebSMike Smith  *
20033a31b7ebSMike Smith  * Note that this uses the simple transport layer directly.  If we
20043a31b7ebSMike Smith  * want to add support for other layers, we'll need a switch of some
20053a31b7ebSMike Smith  * sort.
20063a31b7ebSMike Smith  *
20073a31b7ebSMike Smith  * Note that the simple transport layer has no way of refusing a
20083a31b7ebSMike Smith  * command; we only have as many request structures as the adapter
20093a31b7ebSMike Smith  * supports commands, so we don't have to check (this presumes that
20103a31b7ebSMike Smith  * the adapter can handle commands as fast as we throw them at it).
20113a31b7ebSMike Smith  */
20123a31b7ebSMike Smith static int
20133a31b7ebSMike Smith ciss_start(struct ciss_request *cr)
20143a31b7ebSMike Smith {
20153a31b7ebSMike Smith     struct ciss_command	*cc;	/* XXX debugging only */
20163a31b7ebSMike Smith     int			error;
20173a31b7ebSMike Smith 
20182fdaa90dSScott Long     cc = cr->cr_cc;
20193a31b7ebSMike Smith     debug(2, "post command %d tag %d ", cr->cr_tag, cc->header.host_tag);
20203a31b7ebSMike Smith 
20213a31b7ebSMike Smith     /*
20223a31b7ebSMike Smith      * Map the request's data.
20233a31b7ebSMike Smith      */
20243a31b7ebSMike Smith     if ((error = ciss_map_request(cr)))
20253a31b7ebSMike Smith 	return(error);
20263a31b7ebSMike Smith 
20273a31b7ebSMike Smith #if 0
20283a31b7ebSMike Smith     ciss_print_request(cr);
20293a31b7ebSMike Smith #endif
20303a31b7ebSMike Smith 
20313a31b7ebSMike Smith     return(0);
20323a31b7ebSMike Smith }
20333a31b7ebSMike Smith 
20343a31b7ebSMike Smith /************************************************************************
20353a31b7ebSMike Smith  * Fetch completed request(s) from the adapter, queue them for
20363a31b7ebSMike Smith  * completion handling.
20373a31b7ebSMike Smith  *
20383a31b7ebSMike Smith  * Note that this uses the simple transport layer directly.  If we
20393a31b7ebSMike Smith  * want to add support for other layers, we'll need a switch of some
20403a31b7ebSMike Smith  * sort.
20413a31b7ebSMike Smith  *
20423a31b7ebSMike Smith  * Note that the simple transport mechanism does not require any
20433a31b7ebSMike Smith  * reentrancy protection; the OPQ read is atomic.  If there is a
20443a31b7ebSMike Smith  * chance of a race with something else that might move the request
20453a31b7ebSMike Smith  * off the busy list, then we will have to lock against that
20463a31b7ebSMike Smith  * (eg. timeouts, etc.)
20473a31b7ebSMike Smith  */
20483a31b7ebSMike Smith static void
204922657ce1SScott Long ciss_done(struct ciss_softc *sc, cr_qhead_t *qh)
20503a31b7ebSMike Smith {
20513a31b7ebSMike Smith     struct ciss_request	*cr;
20523a31b7ebSMike Smith     struct ciss_command	*cc;
20533a31b7ebSMike Smith     u_int32_t		tag, index;
20543a31b7ebSMike Smith 
20553a31b7ebSMike Smith     debug_called(3);
20563a31b7ebSMike Smith 
20573a31b7ebSMike Smith     /*
20583a31b7ebSMike Smith      * Loop quickly taking requests from the adapter and moving them
205922657ce1SScott Long      * to the completed queue.
20603a31b7ebSMike Smith      */
20613a31b7ebSMike Smith     for (;;) {
20623a31b7ebSMike Smith 
20633a31b7ebSMike Smith 	tag = CISS_TL_SIMPLE_FETCH_CMD(sc);
20643a31b7ebSMike Smith 	if (tag == CISS_TL_SIMPLE_OPQ_EMPTY)
20653a31b7ebSMike Smith 	    break;
20663a31b7ebSMike Smith 	index = tag >> 2;
20673a31b7ebSMike Smith 	debug(2, "completed command %d%s", index,
20683a31b7ebSMike Smith 	      (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
20693a31b7ebSMike Smith 	if (index >= sc->ciss_max_requests) {
20703a31b7ebSMike Smith 	    ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
20713a31b7ebSMike Smith 	    continue;
20723a31b7ebSMike Smith 	}
20733a31b7ebSMike Smith 	cr = &(sc->ciss_request[index]);
20742fdaa90dSScott Long 	cc = cr->cr_cc;
20753a31b7ebSMike Smith 	cc->header.host_tag = tag;	/* not updated by adapter */
207622657ce1SScott Long 	ciss_enqueue_complete(cr, qh);
20773a31b7ebSMike Smith     }
20783a31b7ebSMike Smith 
207922657ce1SScott Long }
208022657ce1SScott Long 
208122657ce1SScott Long static void
208222657ce1SScott Long ciss_perf_done(struct ciss_softc *sc, cr_qhead_t *qh)
208322657ce1SScott Long {
208422657ce1SScott Long     struct ciss_request	*cr;
208522657ce1SScott Long     struct ciss_command	*cc;
208622657ce1SScott Long     u_int32_t		tag, index;
208722657ce1SScott Long 
208822657ce1SScott Long     debug_called(3);
208922657ce1SScott Long 
20903a31b7ebSMike Smith     /*
209122657ce1SScott Long      * Loop quickly taking requests from the adapter and moving them
209222657ce1SScott Long      * to the completed queue.
20933a31b7ebSMike Smith      */
209422657ce1SScott Long     for (;;) {
209522657ce1SScott Long 	tag = sc->ciss_reply[sc->ciss_rqidx];
209622657ce1SScott Long 	if ((tag & CISS_CYCLE_MASK) != sc->ciss_cycle)
209722657ce1SScott Long 	    break;
209822657ce1SScott Long 	index = tag >> 2;
209922657ce1SScott Long 	debug(2, "completed command %d%s\n", index,
210022657ce1SScott Long 	      (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
210122657ce1SScott Long 	if (index < sc->ciss_max_requests) {
210222657ce1SScott Long 	    cr = &(sc->ciss_request[index]);
21032fdaa90dSScott Long 	    cc = cr->cr_cc;
210422657ce1SScott Long 	    cc->header.host_tag = tag;	/* not updated by adapter */
210522657ce1SScott Long 	    ciss_enqueue_complete(cr, qh);
210622657ce1SScott Long 	} else {
210722657ce1SScott Long 	    ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
210822657ce1SScott Long 	}
210922657ce1SScott Long 	if (++sc->ciss_rqidx == sc->ciss_max_requests) {
211022657ce1SScott Long 	    sc->ciss_rqidx = 0;
211122657ce1SScott Long 	    sc->ciss_cycle ^= 1;
211222657ce1SScott Long 	}
211322657ce1SScott Long     }
211422657ce1SScott Long 
21153a31b7ebSMike Smith }
21163a31b7ebSMike Smith 
21173a31b7ebSMike Smith /************************************************************************
21183a31b7ebSMike Smith  * Take an interrupt from the adapter.
21193a31b7ebSMike Smith  */
21203a31b7ebSMike Smith static void
21213a31b7ebSMike Smith ciss_intr(void *arg)
21223a31b7ebSMike Smith {
212322657ce1SScott Long     cr_qhead_t qh;
21243a31b7ebSMike Smith     struct ciss_softc	*sc = (struct ciss_softc *)arg;
21253a31b7ebSMike Smith 
21263a31b7ebSMike Smith     /*
21273a31b7ebSMike Smith      * The only interrupt we recognise indicates that there are
21283a31b7ebSMike Smith      * entries in the outbound post queue.
21293a31b7ebSMike Smith      */
213022657ce1SScott Long     STAILQ_INIT(&qh);
213122657ce1SScott Long     ciss_done(sc, &qh);
2132975b7318SScott Long     mtx_lock(&sc->ciss_mtx);
213322657ce1SScott Long     ciss_complete(sc, &qh);
2134975b7318SScott Long     mtx_unlock(&sc->ciss_mtx);
21353a31b7ebSMike Smith }
21363a31b7ebSMike Smith 
213722657ce1SScott Long static void
213822657ce1SScott Long ciss_perf_intr(void *arg)
213922657ce1SScott Long {
214022657ce1SScott Long     struct ciss_softc	*sc = (struct ciss_softc *)arg;
214122657ce1SScott Long 
214222657ce1SScott Long     /* Clear the interrupt and flush the bridges.  Docs say that the flush
214322657ce1SScott Long      * needs to be done twice, which doesn't seem right.
214422657ce1SScott Long      */
214522657ce1SScott Long     CISS_TL_PERF_CLEAR_INT(sc);
214622657ce1SScott Long     CISS_TL_PERF_FLUSH_INT(sc);
214722657ce1SScott Long 
214822657ce1SScott Long     ciss_perf_msi_intr(sc);
214922657ce1SScott Long }
215022657ce1SScott Long 
215122657ce1SScott Long static void
215222657ce1SScott Long ciss_perf_msi_intr(void *arg)
215322657ce1SScott Long {
215422657ce1SScott Long     cr_qhead_t qh;
215522657ce1SScott Long     struct ciss_softc	*sc = (struct ciss_softc *)arg;
215622657ce1SScott Long 
215722657ce1SScott Long     STAILQ_INIT(&qh);
215822657ce1SScott Long     ciss_perf_done(sc, &qh);
215922657ce1SScott Long     mtx_lock(&sc->ciss_mtx);
216022657ce1SScott Long     ciss_complete(sc, &qh);
216122657ce1SScott Long     mtx_unlock(&sc->ciss_mtx);
216222657ce1SScott Long }
216322657ce1SScott Long 
216422657ce1SScott Long 
21653a31b7ebSMike Smith /************************************************************************
21663a31b7ebSMike Smith  * Process completed requests.
21673a31b7ebSMike Smith  *
21683a31b7ebSMike Smith  * Requests can be completed in three fashions:
21693a31b7ebSMike Smith  *
21703a31b7ebSMike Smith  * - by invoking a callback function (cr_complete is non-null)
21713a31b7ebSMike Smith  * - by waking up a sleeper (cr_flags has CISS_REQ_SLEEP set)
21723a31b7ebSMike Smith  * - by clearing the CISS_REQ_POLL flag in interrupt/timeout context
21733a31b7ebSMike Smith  */
21743a31b7ebSMike Smith static void
217522657ce1SScott Long ciss_complete(struct ciss_softc *sc, cr_qhead_t *qh)
21763a31b7ebSMike Smith {
21773a31b7ebSMike Smith     struct ciss_request	*cr;
21783a31b7ebSMike Smith 
21793a31b7ebSMike Smith     debug_called(2);
21803a31b7ebSMike Smith 
21813a31b7ebSMike Smith     /*
21823a31b7ebSMike Smith      * Loop taking requests off the completed queue and performing
21833a31b7ebSMike Smith      * completion processing on them.
21843a31b7ebSMike Smith      */
21853a31b7ebSMike Smith     for (;;) {
218622657ce1SScott Long 	if ((cr = ciss_dequeue_complete(sc, qh)) == NULL)
21873a31b7ebSMike Smith 	    break;
21883a31b7ebSMike Smith 	ciss_unmap_request(cr);
21893a31b7ebSMike Smith 
219022657ce1SScott Long 	if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
219122657ce1SScott Long 	    ciss_printf(sc, "WARNING: completing non-busy request\n");
219222657ce1SScott Long 	cr->cr_flags &= ~CISS_REQ_BUSY;
219322657ce1SScott Long 
21943a31b7ebSMike Smith 	/*
21953a31b7ebSMike Smith 	 * If the request has a callback, invoke it.
21963a31b7ebSMike Smith 	 */
21973a31b7ebSMike Smith 	if (cr->cr_complete != NULL) {
21983a31b7ebSMike Smith 	    cr->cr_complete(cr);
21993a31b7ebSMike Smith 	    continue;
22003a31b7ebSMike Smith 	}
22013a31b7ebSMike Smith 
22023a31b7ebSMike Smith 	/*
22033a31b7ebSMike Smith 	 * If someone is sleeping on this request, wake them up.
22043a31b7ebSMike Smith 	 */
22053a31b7ebSMike Smith 	if (cr->cr_flags & CISS_REQ_SLEEP) {
22063a31b7ebSMike Smith 	    cr->cr_flags &= ~CISS_REQ_SLEEP;
22073a31b7ebSMike Smith 	    wakeup(cr);
22083a31b7ebSMike Smith 	    continue;
22093a31b7ebSMike Smith 	}
22103a31b7ebSMike Smith 
22113a31b7ebSMike Smith 	/*
22123a31b7ebSMike Smith 	 * If someone is polling this request for completion, signal.
22133a31b7ebSMike Smith 	 */
22143a31b7ebSMike Smith 	if (cr->cr_flags & CISS_REQ_POLL) {
22153a31b7ebSMike Smith 	    cr->cr_flags &= ~CISS_REQ_POLL;
22163a31b7ebSMike Smith 	    continue;
22173a31b7ebSMike Smith 	}
22183a31b7ebSMike Smith 
22193a31b7ebSMike Smith 	/*
22203a31b7ebSMike Smith 	 * Give up and throw the request back on the free queue.  This
22213a31b7ebSMike Smith 	 * should never happen; resources will probably be lost.
22223a31b7ebSMike Smith 	 */
22233a31b7ebSMike Smith 	ciss_printf(sc, "WARNING: completed command with no submitter\n");
22243a31b7ebSMike Smith 	ciss_enqueue_free(cr);
22253a31b7ebSMike Smith     }
22263a31b7ebSMike Smith }
22273a31b7ebSMike Smith 
22283a31b7ebSMike Smith /************************************************************************
22293a31b7ebSMike Smith  * Report on the completion status of a request, and pass back SCSI
22303a31b7ebSMike Smith  * and command status values.
22313a31b7ebSMike Smith  */
22323a31b7ebSMike Smith static int
223322657ce1SScott Long _ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status, const char *func)
22343a31b7ebSMike Smith {
22353a31b7ebSMike Smith     struct ciss_command		*cc;
22363a31b7ebSMike Smith     struct ciss_error_info	*ce;
22373a31b7ebSMike Smith 
22383a31b7ebSMike Smith     debug_called(2);
22393a31b7ebSMike Smith 
22402fdaa90dSScott Long     cc = cr->cr_cc;
22413a31b7ebSMike Smith     ce = (struct ciss_error_info *)&(cc->sg[0]);
22423a31b7ebSMike Smith 
22433a31b7ebSMike Smith     /*
22443a31b7ebSMike Smith      * We don't consider data under/overrun an error for the Report
22453a31b7ebSMike Smith      * Logical/Physical LUNs commands.
22463a31b7ebSMike Smith      */
22473a31b7ebSMike Smith     if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) &&
22482514c5bfSPaul Saab 	((ce->command_status == CISS_CMD_STATUS_DATA_OVERRUN) ||
22492514c5bfSPaul Saab 	 (ce->command_status == CISS_CMD_STATUS_DATA_UNDERRUN)) &&
22503a31b7ebSMike Smith 	((cc->cdb.cdb[0] == CISS_OPCODE_REPORT_LOGICAL_LUNS) ||
22512514c5bfSPaul Saab 	 (cc->cdb.cdb[0] == CISS_OPCODE_REPORT_PHYSICAL_LUNS) ||
22522514c5bfSPaul Saab 	 (cc->cdb.cdb[0] == INQUIRY))) {
22533a31b7ebSMike Smith 	cc->header.host_tag &= ~CISS_HDR_HOST_TAG_ERROR;
22543a31b7ebSMike Smith 	debug(2, "ignoring irrelevant under/overrun error");
22553a31b7ebSMike Smith     }
22563a31b7ebSMike Smith 
22573a31b7ebSMike Smith     /*
22583a31b7ebSMike Smith      * Check the command's error bit, if clear, there's no status and
22593a31b7ebSMike Smith      * everything is OK.
22603a31b7ebSMike Smith      */
22613a31b7ebSMike Smith     if (!(cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR)) {
22623a31b7ebSMike Smith 	if (scsi_status != NULL)
22633a31b7ebSMike Smith 	    *scsi_status = SCSI_STATUS_OK;
22643a31b7ebSMike Smith 	if (command_status != NULL)
22653a31b7ebSMike Smith 	    *command_status = CISS_CMD_STATUS_SUCCESS;
22663a31b7ebSMike Smith 	return(0);
22673a31b7ebSMike Smith     } else {
22683a31b7ebSMike Smith 	if (command_status != NULL)
22693a31b7ebSMike Smith 	    *command_status = ce->command_status;
22703a31b7ebSMike Smith 	if (scsi_status != NULL) {
22713a31b7ebSMike Smith 	    if (ce->command_status == CISS_CMD_STATUS_TARGET_STATUS) {
22723a31b7ebSMike Smith 		*scsi_status = ce->scsi_status;
22733a31b7ebSMike Smith 	    } else {
22743a31b7ebSMike Smith 		*scsi_status = -1;
22753a31b7ebSMike Smith 	    }
22763a31b7ebSMike Smith 	}
22773a31b7ebSMike Smith 	if (bootverbose)
22783a31b7ebSMike Smith 	    ciss_printf(cr->cr_sc, "command status 0x%x (%s) scsi status 0x%x\n",
22793a31b7ebSMike Smith 			ce->command_status, ciss_name_command_status(ce->command_status),
22803a31b7ebSMike Smith 			ce->scsi_status);
22813a31b7ebSMike Smith 	if (ce->command_status == CISS_CMD_STATUS_INVALID_COMMAND) {
228222657ce1SScott Long 	    ciss_printf(cr->cr_sc, "invalid command, offense size %d at %d, value 0x%x, function %s\n",
22833a31b7ebSMike Smith 			ce->additional_error_info.invalid_command.offense_size,
22843a31b7ebSMike Smith 			ce->additional_error_info.invalid_command.offense_offset,
228522657ce1SScott Long 			ce->additional_error_info.invalid_command.offense_value,
228622657ce1SScott Long 			func);
22873a31b7ebSMike Smith 	}
22883a31b7ebSMike Smith     }
2289c6131460SPaul Saab #if 0
2290c6131460SPaul Saab     ciss_print_request(cr);
2291c6131460SPaul Saab #endif
22923a31b7ebSMike Smith     return(1);
22933a31b7ebSMike Smith }
22943a31b7ebSMike Smith 
22953a31b7ebSMike Smith /************************************************************************
22963a31b7ebSMike Smith  * Issue a request and don't return until it's completed.
22973a31b7ebSMike Smith  *
22983a31b7ebSMike Smith  * Depending on adapter status, we may poll or sleep waiting for
22993a31b7ebSMike Smith  * completion.
23003a31b7ebSMike Smith  */
23013a31b7ebSMike Smith static int
23023a31b7ebSMike Smith ciss_synch_request(struct ciss_request *cr, int timeout)
23033a31b7ebSMike Smith {
23043a31b7ebSMike Smith     if (cr->cr_sc->ciss_flags & CISS_FLAG_RUNNING) {
23053a31b7ebSMike Smith 	return(ciss_wait_request(cr, timeout));
23063a31b7ebSMike Smith     } else {
23073a31b7ebSMike Smith 	return(ciss_poll_request(cr, timeout));
23083a31b7ebSMike Smith     }
23093a31b7ebSMike Smith }
23103a31b7ebSMike Smith 
23113a31b7ebSMike Smith /************************************************************************
23123a31b7ebSMike Smith  * Issue a request and poll for completion.
23133a31b7ebSMike Smith  *
23143a31b7ebSMike Smith  * Timeout in milliseconds.
23153a31b7ebSMike Smith  */
23163a31b7ebSMike Smith static int
23173a31b7ebSMike Smith ciss_poll_request(struct ciss_request *cr, int timeout)
23183a31b7ebSMike Smith {
231922657ce1SScott Long     cr_qhead_t qh;
232022657ce1SScott Long     struct ciss_softc *sc;
23213a31b7ebSMike Smith     int		error;
23223a31b7ebSMike Smith 
23233a31b7ebSMike Smith     debug_called(2);
23243a31b7ebSMike Smith 
232522657ce1SScott Long     STAILQ_INIT(&qh);
232622657ce1SScott Long     sc = cr->cr_sc;
23273a31b7ebSMike Smith     cr->cr_flags |= CISS_REQ_POLL;
23283a31b7ebSMike Smith     if ((error = ciss_start(cr)) != 0)
23293a31b7ebSMike Smith 	return(error);
23303a31b7ebSMike Smith 
23313a31b7ebSMike Smith     do {
233222657ce1SScott Long 	if (sc->ciss_perf)
233322657ce1SScott Long 	    ciss_perf_done(sc, &qh);
233422657ce1SScott Long 	else
233522657ce1SScott Long 	    ciss_done(sc, &qh);
233622657ce1SScott Long 	ciss_complete(sc, &qh);
23373a31b7ebSMike Smith 	if (!(cr->cr_flags & CISS_REQ_POLL))
23383a31b7ebSMike Smith 	    return(0);
23393a31b7ebSMike Smith 	DELAY(1000);
23403a31b7ebSMike Smith     } while (timeout-- >= 0);
23413a31b7ebSMike Smith     return(EWOULDBLOCK);
23423a31b7ebSMike Smith }
23433a31b7ebSMike Smith 
23443a31b7ebSMike Smith /************************************************************************
23453a31b7ebSMike Smith  * Issue a request and sleep waiting for completion.
23463a31b7ebSMike Smith  *
23473a31b7ebSMike Smith  * Timeout in milliseconds.  Note that a spurious wakeup will reset
23483a31b7ebSMike Smith  * the timeout.
23493a31b7ebSMike Smith  */
23503a31b7ebSMike Smith static int
23513a31b7ebSMike Smith ciss_wait_request(struct ciss_request *cr, int timeout)
23523a31b7ebSMike Smith {
235322657ce1SScott Long     int		error;
23543a31b7ebSMike Smith 
23553a31b7ebSMike Smith     debug_called(2);
23563a31b7ebSMike Smith 
23573a31b7ebSMike Smith     cr->cr_flags |= CISS_REQ_SLEEP;
23583a31b7ebSMike Smith     if ((error = ciss_start(cr)) != 0)
23593a31b7ebSMike Smith 	return(error);
23603a31b7ebSMike Smith 
236140f05b02SPaul Saab     while ((cr->cr_flags & CISS_REQ_SLEEP) && (error != EWOULDBLOCK)) {
2362975b7318SScott Long 	error = msleep(cr, &cr->cr_sc->ciss_mtx, PRIBIO, "cissREQ", (timeout * hz) / 1000);
23633a31b7ebSMike Smith     }
23643a31b7ebSMike Smith     return(error);
23653a31b7ebSMike Smith }
23663a31b7ebSMike Smith 
23676197ca15SPeter Wemm #if 0
23683a31b7ebSMike Smith /************************************************************************
23693a31b7ebSMike Smith  * Abort a request.  Note that a potential exists here to race the
23703a31b7ebSMike Smith  * request being completed; the caller must deal with this.
23713a31b7ebSMike Smith  */
23723a31b7ebSMike Smith static int
23733a31b7ebSMike Smith ciss_abort_request(struct ciss_request *ar)
23743a31b7ebSMike Smith {
23753a31b7ebSMike Smith     struct ciss_request		*cr;
23763a31b7ebSMike Smith     struct ciss_command		*cc;
23773a31b7ebSMike Smith     struct ciss_message_cdb	*cmc;
23783a31b7ebSMike Smith     int				error;
23793a31b7ebSMike Smith 
23803a31b7ebSMike Smith     debug_called(1);
23813a31b7ebSMike Smith 
23823a31b7ebSMike Smith     /* get a request */
23833a31b7ebSMike Smith     if ((error = ciss_get_request(ar->cr_sc, &cr)) != 0)
23843a31b7ebSMike Smith 	return(error);
23853a31b7ebSMike Smith 
23863a31b7ebSMike Smith     /* build the abort command */
23872fdaa90dSScott Long     cc = cr->cr_cc;
23883a31b7ebSMike Smith     cc->header.address.mode.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;	/* addressing? */
23893a31b7ebSMike Smith     cc->header.address.physical.target = 0;
23903a31b7ebSMike Smith     cc->header.address.physical.bus = 0;
23913a31b7ebSMike Smith     cc->cdb.cdb_length = sizeof(*cmc);
23923a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
23933a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
23943a31b7ebSMike Smith     cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
23953a31b7ebSMike Smith     cc->cdb.timeout = 30;
23963a31b7ebSMike Smith 
23973a31b7ebSMike Smith     cmc = (struct ciss_message_cdb *)&(cc->cdb.cdb[0]);
23983a31b7ebSMike Smith     cmc->opcode = CISS_OPCODE_MESSAGE_ABORT;
23993a31b7ebSMike Smith     cmc->type = CISS_MESSAGE_ABORT_TASK;
24003a31b7ebSMike Smith     cmc->abort_tag = ar->cr_tag;	/* endianness?? */
24013a31b7ebSMike Smith 
24023a31b7ebSMike Smith     /*
24033a31b7ebSMike Smith      * Send the request and wait for a response.  If we believe we
24043a31b7ebSMike Smith      * aborted the request OK, clear the flag that indicates it's
24053a31b7ebSMike Smith      * running.
24063a31b7ebSMike Smith      */
24073a31b7ebSMike Smith     error = ciss_synch_request(cr, 35 * 1000);
24083a31b7ebSMike Smith     if (!error)
24093a31b7ebSMike Smith 	error = ciss_report_request(cr, NULL, NULL);
24103a31b7ebSMike Smith     ciss_release_request(cr);
24113a31b7ebSMike Smith 
24123a31b7ebSMike Smith     return(error);
24133a31b7ebSMike Smith }
24146197ca15SPeter Wemm #endif
24153a31b7ebSMike Smith 
24163a31b7ebSMike Smith 
24173a31b7ebSMike Smith /************************************************************************
24183a31b7ebSMike Smith  * Fetch and initialise a request
24193a31b7ebSMike Smith  */
24203a31b7ebSMike Smith static int
24213a31b7ebSMike Smith ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp)
24223a31b7ebSMike Smith {
24233a31b7ebSMike Smith     struct ciss_request *cr;
24243a31b7ebSMike Smith 
24253a31b7ebSMike Smith     debug_called(2);
24263a31b7ebSMike Smith 
24273a31b7ebSMike Smith     /*
24283a31b7ebSMike Smith      * Get a request and clean it up.
24293a31b7ebSMike Smith      */
24303a31b7ebSMike Smith     if ((cr = ciss_dequeue_free(sc)) == NULL)
24313a31b7ebSMike Smith 	return(ENOMEM);
24323a31b7ebSMike Smith 
24333a31b7ebSMike Smith     cr->cr_data = NULL;
24343a31b7ebSMike Smith     cr->cr_flags = 0;
24353a31b7ebSMike Smith     cr->cr_complete = NULL;
2436639717c8SPaul Saab     cr->cr_private = NULL;
243722657ce1SScott Long     cr->cr_sg_tag = CISS_SG_MAX;	/* Backstop to prevent accidents */
24383a31b7ebSMike Smith 
24393a31b7ebSMike Smith     ciss_preen_command(cr);
24403a31b7ebSMike Smith     *crp = cr;
24413a31b7ebSMike Smith     return(0);
24423a31b7ebSMike Smith }
24433a31b7ebSMike Smith 
24443a31b7ebSMike Smith static void
24453a31b7ebSMike Smith ciss_preen_command(struct ciss_request *cr)
24463a31b7ebSMike Smith {
24473a31b7ebSMike Smith     struct ciss_command	*cc;
24483a31b7ebSMike Smith     u_int32_t		cmdphys;
24493a31b7ebSMike Smith 
24503a31b7ebSMike Smith     /*
24513a31b7ebSMike Smith      * Clean up the command structure.
24523a31b7ebSMike Smith      *
24533a31b7ebSMike Smith      * Note that we set up the error_info structure here, since the
24543a31b7ebSMike Smith      * length can be overwritten by any command.
24553a31b7ebSMike Smith      */
24562fdaa90dSScott Long     cc = cr->cr_cc;
24573a31b7ebSMike Smith     cc->header.sg_in_list = 0;		/* kinda inefficient this way */
24583a31b7ebSMike Smith     cc->header.sg_total = 0;
24593a31b7ebSMike Smith     cc->header.host_tag = cr->cr_tag << 2;
24603a31b7ebSMike Smith     cc->header.host_tag_zeroes = 0;
24612fdaa90dSScott Long     cmdphys = cr->cr_ccphys;
24623a31b7ebSMike Smith     cc->error_info.error_info_address = cmdphys + sizeof(struct ciss_command);
24633a31b7ebSMike Smith     cc->error_info.error_info_length = CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command);
24643a31b7ebSMike Smith }
24653a31b7ebSMike Smith 
24663a31b7ebSMike Smith /************************************************************************
24673a31b7ebSMike Smith  * Release a request to the free list.
24683a31b7ebSMike Smith  */
24693a31b7ebSMike Smith static void
24703a31b7ebSMike Smith ciss_release_request(struct ciss_request *cr)
24713a31b7ebSMike Smith {
24723a31b7ebSMike Smith     struct ciss_softc	*sc;
24733a31b7ebSMike Smith 
24743a31b7ebSMike Smith     debug_called(2);
24753a31b7ebSMike Smith 
24763a31b7ebSMike Smith     sc = cr->cr_sc;
24773a31b7ebSMike Smith 
24783a31b7ebSMike Smith     /* release the request to the free queue */
24793a31b7ebSMike Smith     ciss_requeue_free(cr);
24803a31b7ebSMike Smith }
24813a31b7ebSMike Smith 
24823a31b7ebSMike Smith /************************************************************************
24833a31b7ebSMike Smith  * Allocate a request that will be used to send a BMIC command.  Do some
24843a31b7ebSMike Smith  * of the common setup here to avoid duplicating it everywhere else.
24853a31b7ebSMike Smith  */
24863a31b7ebSMike Smith static int
24873a31b7ebSMike Smith ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
24883a31b7ebSMike Smith 		      int opcode, void **bufp, size_t bufsize)
24893a31b7ebSMike Smith {
24903a31b7ebSMike Smith     struct ciss_request		*cr;
24913a31b7ebSMike Smith     struct ciss_command		*cc;
24923a31b7ebSMike Smith     struct ciss_bmic_cdb	*cbc;
24933a31b7ebSMike Smith     void			*buf;
24943a31b7ebSMike Smith     int				error;
24953a31b7ebSMike Smith     int				dataout;
24963a31b7ebSMike Smith 
24973a31b7ebSMike Smith     debug_called(2);
24983a31b7ebSMike Smith 
24993a31b7ebSMike Smith     cr = NULL;
25003a31b7ebSMike Smith     buf = NULL;
25013a31b7ebSMike Smith 
25023a31b7ebSMike Smith     /*
25033a31b7ebSMike Smith      * Get a request.
25043a31b7ebSMike Smith      */
25053a31b7ebSMike Smith     if ((error = ciss_get_request(sc, &cr)) != 0)
25063a31b7ebSMike Smith 	goto out;
25073a31b7ebSMike Smith 
25083a31b7ebSMike Smith     /*
25093a31b7ebSMike Smith      * Allocate data storage if requested, determine the data direction.
25103a31b7ebSMike Smith      */
25113a31b7ebSMike Smith     dataout = 0;
25123a31b7ebSMike Smith     if ((bufsize > 0) && (bufp != NULL)) {
25133a31b7ebSMike Smith 	if (*bufp == NULL) {
25143a31b7ebSMike Smith 	    if ((buf = malloc(bufsize, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
25153a31b7ebSMike Smith 		error = ENOMEM;
25163a31b7ebSMike Smith 		goto out;
25173a31b7ebSMike Smith 	    }
25183a31b7ebSMike Smith 	} else {
25193a31b7ebSMike Smith 	    buf = *bufp;
25203a31b7ebSMike Smith 	    dataout = 1;	/* we are given a buffer, so we are writing */
25213a31b7ebSMike Smith 	}
25223a31b7ebSMike Smith     }
25233a31b7ebSMike Smith 
25243a31b7ebSMike Smith     /*
25253a31b7ebSMike Smith      * Build a CISS BMIC command to get the logical drive ID.
25263a31b7ebSMike Smith      */
25273a31b7ebSMike Smith     cr->cr_data = buf;
25283a31b7ebSMike Smith     cr->cr_length = bufsize;
25293a31b7ebSMike Smith     if (!dataout)
25303a31b7ebSMike Smith 	cr->cr_flags = CISS_REQ_DATAIN;
25313a31b7ebSMike Smith 
25322fdaa90dSScott Long     cc = cr->cr_cc;
25333a31b7ebSMike Smith     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
25343a31b7ebSMike Smith     cc->header.address.physical.bus = 0;
25353a31b7ebSMike Smith     cc->header.address.physical.target = 0;
25363a31b7ebSMike Smith     cc->cdb.cdb_length = sizeof(*cbc);
25373a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
25383a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
25393a31b7ebSMike Smith     cc->cdb.direction = dataout ? CISS_CDB_DIRECTION_WRITE : CISS_CDB_DIRECTION_READ;
25403a31b7ebSMike Smith     cc->cdb.timeout = 0;
25413a31b7ebSMike Smith 
25423a31b7ebSMike Smith     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
25433a31b7ebSMike Smith     bzero(cbc, sizeof(*cbc));
25443a31b7ebSMike Smith     cbc->opcode = dataout ? CISS_ARRAY_CONTROLLER_WRITE : CISS_ARRAY_CONTROLLER_READ;
25453a31b7ebSMike Smith     cbc->bmic_opcode = opcode;
25463a31b7ebSMike Smith     cbc->size = htons((u_int16_t)bufsize);
25473a31b7ebSMike Smith 
25483a31b7ebSMike Smith out:
25493a31b7ebSMike Smith     if (error) {
25503a31b7ebSMike Smith 	if (cr != NULL)
25513a31b7ebSMike Smith 	    ciss_release_request(cr);
25523a31b7ebSMike Smith     } else {
25533a31b7ebSMike Smith 	*crp = cr;
25543a31b7ebSMike Smith 	if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL))
25553a31b7ebSMike Smith 	    *bufp = buf;
25563a31b7ebSMike Smith     }
25573a31b7ebSMike Smith     return(error);
25583a31b7ebSMike Smith }
25593a31b7ebSMike Smith 
25603a31b7ebSMike Smith /************************************************************************
25613a31b7ebSMike Smith  * Handle a command passed in from userspace.
25623a31b7ebSMike Smith  */
25633a31b7ebSMike Smith static int
25643a31b7ebSMike Smith ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc)
25653a31b7ebSMike Smith {
25663a31b7ebSMike Smith     struct ciss_request		*cr;
25673a31b7ebSMike Smith     struct ciss_command		*cc;
25683a31b7ebSMike Smith     struct ciss_error_info	*ce;
25693c63a8b4SPaul Saab     int				error = 0;
25703a31b7ebSMike Smith 
25713a31b7ebSMike Smith     debug_called(1);
25723a31b7ebSMike Smith 
25733a31b7ebSMike Smith     cr = NULL;
25743a31b7ebSMike Smith 
25753a31b7ebSMike Smith     /*
25763a31b7ebSMike Smith      * Get a request.
25773a31b7ebSMike Smith      */
2578f83695f5SPaul Saab     while (ciss_get_request(sc, &cr) != 0)
2579975b7318SScott Long 	msleep(sc, &sc->ciss_mtx, PPAUSE, "cissREQ", hz);
25802fdaa90dSScott Long     cc = cr->cr_cc;
25813a31b7ebSMike Smith 
25823a31b7ebSMike Smith     /*
25833a31b7ebSMike Smith      * Allocate an in-kernel databuffer if required, copy in user data.
25843a31b7ebSMike Smith      */
2585744c0d23SScott Long     mtx_unlock(&sc->ciss_mtx);
25863a31b7ebSMike Smith     cr->cr_length = ioc->buf_size;
25873a31b7ebSMike Smith     if (ioc->buf_size > 0) {
2588975b7318SScott Long 	if ((cr->cr_data = malloc(ioc->buf_size, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
25893a31b7ebSMike Smith 	    error = ENOMEM;
2590744c0d23SScott Long 	    goto out_unlocked;
25913a31b7ebSMike Smith 	}
25923a31b7ebSMike Smith 	if ((error = copyin(ioc->buf, cr->cr_data, ioc->buf_size))) {
25933a31b7ebSMike Smith 	    debug(0, "copyin: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2594744c0d23SScott Long 	    goto out_unlocked;
25953a31b7ebSMike Smith 	}
25963a31b7ebSMike Smith     }
25973a31b7ebSMike Smith 
25983a31b7ebSMike Smith     /*
25993a31b7ebSMike Smith      * Build the request based on the user command.
26003a31b7ebSMike Smith      */
26013a31b7ebSMike Smith     bcopy(&ioc->LUN_info, &cc->header.address, sizeof(cc->header.address));
26023a31b7ebSMike Smith     bcopy(&ioc->Request, &cc->cdb, sizeof(cc->cdb));
26033a31b7ebSMike Smith 
26043a31b7ebSMike Smith     /* XXX anything else to populate here? */
2605744c0d23SScott Long     mtx_lock(&sc->ciss_mtx);
26063a31b7ebSMike Smith 
26073a31b7ebSMike Smith     /*
26083a31b7ebSMike Smith      * Run the command.
26093a31b7ebSMike Smith      */
26103a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000))) {
26113a31b7ebSMike Smith 	debug(0, "request failed - %d", error);
26123a31b7ebSMike Smith 	goto out;
26133a31b7ebSMike Smith     }
26143a31b7ebSMike Smith 
26153a31b7ebSMike Smith     /*
26163c63a8b4SPaul Saab      * Check to see if the command succeeded.
26173a31b7ebSMike Smith      */
26183a31b7ebSMike Smith     ce = (struct ciss_error_info *)&(cc->sg[0]);
26193c6b8353SPaul Saab     if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) == 0)
26203c63a8b4SPaul Saab 	bzero(ce, sizeof(*ce));
26213c63a8b4SPaul Saab 
26223c63a8b4SPaul Saab     /*
26233c63a8b4SPaul Saab      * Copy the results back to the user.
26243c63a8b4SPaul Saab      */
26253a31b7ebSMike Smith     bcopy(ce, &ioc->error_info, sizeof(*ce));
2626744c0d23SScott Long     mtx_unlock(&sc->ciss_mtx);
26273a31b7ebSMike Smith     if ((ioc->buf_size > 0) &&
26283a31b7ebSMike Smith 	(error = copyout(cr->cr_data, ioc->buf, ioc->buf_size))) {
26293a31b7ebSMike Smith 	debug(0, "copyout: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2630744c0d23SScott Long 	goto out_unlocked;
26313a31b7ebSMike Smith     }
26323a31b7ebSMike Smith 
26333a31b7ebSMike Smith     /* done OK */
26343a31b7ebSMike Smith     error = 0;
26353a31b7ebSMike Smith 
2636744c0d23SScott Long out_unlocked:
2637744c0d23SScott Long     mtx_lock(&sc->ciss_mtx);
2638744c0d23SScott Long 
26393a31b7ebSMike Smith out:
26403a31b7ebSMike Smith     if ((cr != NULL) && (cr->cr_data != NULL))
26413a31b7ebSMike Smith 	free(cr->cr_data, CISS_MALLOC_CLASS);
26423a31b7ebSMike Smith     if (cr != NULL)
26433a31b7ebSMike Smith 	ciss_release_request(cr);
26443a31b7ebSMike Smith     return(error);
26453a31b7ebSMike Smith }
26463a31b7ebSMike Smith 
26473a31b7ebSMike Smith /************************************************************************
26483a31b7ebSMike Smith  * Map a request into bus-visible space, initialise the scatter/gather
26493a31b7ebSMike Smith  * list.
26503a31b7ebSMike Smith  */
26513a31b7ebSMike Smith static int
26523a31b7ebSMike Smith ciss_map_request(struct ciss_request *cr)
26533a31b7ebSMike Smith {
26543a31b7ebSMike Smith     struct ciss_softc	*sc;
2655639717c8SPaul Saab     int			error = 0;
26563a31b7ebSMike Smith 
26573a31b7ebSMike Smith     debug_called(2);
26583a31b7ebSMike Smith 
26593a31b7ebSMike Smith     sc = cr->cr_sc;
26603a31b7ebSMike Smith 
26613a31b7ebSMike Smith     /* check that mapping is necessary */
2662639717c8SPaul Saab     if (cr->cr_flags & CISS_REQ_MAPPED)
26633a31b7ebSMike Smith 	return(0);
26643a31b7ebSMike Smith 
26653a31b7ebSMike Smith     cr->cr_flags |= CISS_REQ_MAPPED;
2666639717c8SPaul Saab 
2667639717c8SPaul Saab     bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2668639717c8SPaul Saab 		    BUS_DMASYNC_PREWRITE);
2669639717c8SPaul Saab 
2670639717c8SPaul Saab     if (cr->cr_data != NULL) {
2671639717c8SPaul Saab 	error = bus_dmamap_load(sc->ciss_buffer_dmat, cr->cr_datamap,
2672639717c8SPaul Saab 				cr->cr_data, cr->cr_length,
2673639717c8SPaul Saab 				ciss_request_map_helper, cr, 0);
2674639717c8SPaul Saab 	if (error != 0)
2675639717c8SPaul Saab 	    return (error);
2676639717c8SPaul Saab     } else {
2677639717c8SPaul Saab 	/*
2678639717c8SPaul Saab 	 * Post the command to the adapter.
2679639717c8SPaul Saab 	 */
268022657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_NONE;
268122657ce1SScott Long 	cr->cr_flags |= CISS_REQ_BUSY;
268222657ce1SScott Long 	if (sc->ciss_perf)
268322657ce1SScott Long 	    CISS_TL_PERF_POST_CMD(sc, cr);
268422657ce1SScott Long 	else
26852fdaa90dSScott Long 	    CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
2686639717c8SPaul Saab     }
2687639717c8SPaul Saab 
26883a31b7ebSMike Smith     return(0);
26893a31b7ebSMike Smith }
26903a31b7ebSMike Smith 
26913a31b7ebSMike Smith static void
26923a31b7ebSMike Smith ciss_request_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
26933a31b7ebSMike Smith {
26943a31b7ebSMike Smith     struct ciss_command	*cc;
2695639717c8SPaul Saab     struct ciss_request *cr;
2696639717c8SPaul Saab     struct ciss_softc	*sc;
26973a31b7ebSMike Smith     int			i;
26983a31b7ebSMike Smith 
26993a31b7ebSMike Smith     debug_called(2);
27003a31b7ebSMike Smith 
2701639717c8SPaul Saab     cr = (struct ciss_request *)arg;
2702639717c8SPaul Saab     sc = cr->cr_sc;
27032fdaa90dSScott Long     cc = cr->cr_cc;
2704639717c8SPaul Saab 
27053a31b7ebSMike Smith     for (i = 0; i < nseg; i++) {
27063a31b7ebSMike Smith 	cc->sg[i].address = segs[i].ds_addr;
27073a31b7ebSMike Smith 	cc->sg[i].length = segs[i].ds_len;
27083a31b7ebSMike Smith 	cc->sg[i].extension = 0;
27093a31b7ebSMike Smith     }
27103a31b7ebSMike Smith     /* we leave the s/g table entirely within the command */
27113a31b7ebSMike Smith     cc->header.sg_in_list = nseg;
27123a31b7ebSMike Smith     cc->header.sg_total = nseg;
2713639717c8SPaul Saab 
2714639717c8SPaul Saab     if (cr->cr_flags & CISS_REQ_DATAIN)
2715639717c8SPaul Saab 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREREAD);
2716639717c8SPaul Saab     if (cr->cr_flags & CISS_REQ_DATAOUT)
2717639717c8SPaul Saab 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREWRITE);
2718639717c8SPaul Saab 
271962fdee1dSScott Long     if (nseg == 0)
272022657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_NONE;
272122657ce1SScott Long     else if (nseg == 1)
272222657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_1;
272322657ce1SScott Long     else if (nseg == 2)
272422657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_2;
272522657ce1SScott Long     else if (nseg <= 4)
272622657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_4;
272722657ce1SScott Long     else if (nseg <= 8)
272822657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_8;
272922657ce1SScott Long     else if (nseg <= 16)
273022657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_16;
273122657ce1SScott Long     else if (nseg <= 32)
273222657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_32;
273322657ce1SScott Long     else
273422657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_MAX;
273522657ce1SScott Long 
2736639717c8SPaul Saab     /*
2737639717c8SPaul Saab      * Post the command to the adapter.
2738639717c8SPaul Saab      */
273922657ce1SScott Long     cr->cr_flags |= CISS_REQ_BUSY;
274022657ce1SScott Long     if (sc->ciss_perf)
274122657ce1SScott Long 	CISS_TL_PERF_POST_CMD(sc, cr);
274222657ce1SScott Long     else
27432fdaa90dSScott Long 	CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
27443a31b7ebSMike Smith }
27453a31b7ebSMike Smith 
27463a31b7ebSMike Smith /************************************************************************
27473a31b7ebSMike Smith  * Unmap a request from bus-visible space.
27483a31b7ebSMike Smith  */
27493a31b7ebSMike Smith static void
27503a31b7ebSMike Smith ciss_unmap_request(struct ciss_request *cr)
27513a31b7ebSMike Smith {
27523a31b7ebSMike Smith     struct ciss_softc	*sc;
27533a31b7ebSMike Smith 
27543a31b7ebSMike Smith     debug_called(2);
27553a31b7ebSMike Smith 
27563a31b7ebSMike Smith     sc = cr->cr_sc;
27573a31b7ebSMike Smith 
27583a31b7ebSMike Smith     /* check that unmapping is necessary */
2759639717c8SPaul Saab     if ((cr->cr_flags & CISS_REQ_MAPPED) == 0)
27603a31b7ebSMike Smith 	return;
27613a31b7ebSMike Smith 
2762639717c8SPaul Saab     bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2763639717c8SPaul Saab 		    BUS_DMASYNC_POSTWRITE);
2764639717c8SPaul Saab 
2765639717c8SPaul Saab     if (cr->cr_data == NULL)
2766639717c8SPaul Saab 	goto out;
2767639717c8SPaul Saab 
27683a31b7ebSMike Smith     if (cr->cr_flags & CISS_REQ_DATAIN)
27693a31b7ebSMike Smith 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTREAD);
27703a31b7ebSMike Smith     if (cr->cr_flags & CISS_REQ_DATAOUT)
27713a31b7ebSMike Smith 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTWRITE);
27723a31b7ebSMike Smith 
27733a31b7ebSMike Smith     bus_dmamap_unload(sc->ciss_buffer_dmat, cr->cr_datamap);
2774639717c8SPaul Saab out:
27753a31b7ebSMike Smith     cr->cr_flags &= ~CISS_REQ_MAPPED;
27763a31b7ebSMike Smith }
27773a31b7ebSMike Smith 
27783a31b7ebSMike Smith /************************************************************************
27793a31b7ebSMike Smith  * Attach the driver to CAM.
27803a31b7ebSMike Smith  *
27813a31b7ebSMike Smith  * We put all the logical drives on a single SCSI bus.
27823a31b7ebSMike Smith  */
27833a31b7ebSMike Smith static int
27843a31b7ebSMike Smith ciss_cam_init(struct ciss_softc *sc)
27853a31b7ebSMike Smith {
27861fe6c4eeSScott Long     int			i, maxbus;
27873a31b7ebSMike Smith 
27883a31b7ebSMike Smith     debug_called(1);
27893a31b7ebSMike Smith 
27903a31b7ebSMike Smith     /*
27913a31b7ebSMike Smith      * Allocate a devq.  We can reuse this for the masked physical
27923a31b7ebSMike Smith      * devices if we decide to export these as well.
27933a31b7ebSMike Smith      */
27943a31b7ebSMike Smith     if ((sc->ciss_cam_devq = cam_simq_alloc(sc->ciss_max_requests)) == NULL) {
27953a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate CAM SIM queue\n");
27963a31b7ebSMike Smith 	return(ENOMEM);
27973a31b7ebSMike Smith     }
27983a31b7ebSMike Smith 
27993a31b7ebSMike Smith     /*
28003a31b7ebSMike Smith      * Create a SIM.
28011fe6c4eeSScott Long      *
28021fe6c4eeSScott Long      * This naturally wastes a bit of memory.  The alternative is to allocate
28031fe6c4eeSScott Long      * and register each bus as it is found, and then track them on a linked
28041fe6c4eeSScott Long      * list.  Unfortunately, the driver has a few places where it needs to
28051fe6c4eeSScott Long      * look up the SIM based solely on bus number, and it's unclear whether
28061fe6c4eeSScott Long      * a list traversal would work for these situations.
28073a31b7ebSMike Smith      */
28081fe6c4eeSScott Long     maxbus = max(sc->ciss_max_logical_bus, sc->ciss_max_physical_bus +
28091fe6c4eeSScott Long 		 CISS_PHYSICAL_BASE);
28101fe6c4eeSScott Long     sc->ciss_cam_sim = malloc(maxbus * sizeof(struct cam_sim*),
2811c6131460SPaul Saab 			      CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
2812c6131460SPaul Saab     if (sc->ciss_cam_sim == NULL) {
2813c6131460SPaul Saab 	ciss_printf(sc, "can't allocate memory for controller SIM\n");
2814c6131460SPaul Saab 	return(ENOMEM);
2815c6131460SPaul Saab     }
2816c6131460SPaul Saab 
28171fe6c4eeSScott Long     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
2818c6131460SPaul Saab 	if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
2819c6131460SPaul Saab 						 "ciss", sc,
28202aa6b972SPaul Saab 						 device_get_unit(sc->ciss_dev),
282145650f52SScott Long 						 &sc->ciss_mtx,
282222657ce1SScott Long 						 2,
2823d4aa427fSPaul Saab 						 sc->ciss_max_requests - 2,
28243a31b7ebSMike Smith 						 sc->ciss_cam_devq)) == NULL) {
2825c6131460SPaul Saab 	    ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
28263a31b7ebSMike Smith 	    return(ENOMEM);
28273a31b7ebSMike Smith 	}
28283a31b7ebSMike Smith 
28293a31b7ebSMike Smith 	/*
2830c6131460SPaul Saab 	 * Register bus with this SIM.
28313a31b7ebSMike Smith 	 */
2832975b7318SScott Long 	mtx_lock(&sc->ciss_mtx);
28331fe6c4eeSScott Long 	if (i == 0 || sc->ciss_controllers[i].physical.bus != 0) {
2834b50569b7SScott Long 	    if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
28351fe6c4eeSScott Long 		ciss_printf(sc, "can't register SCSI bus %d\n", i);
2836975b7318SScott Long 		mtx_unlock(&sc->ciss_mtx);
28371fe6c4eeSScott Long 		return (ENXIO);
28381fe6c4eeSScott Long 	    }
28391fe6c4eeSScott Long 	}
2840975b7318SScott Long 	mtx_unlock(&sc->ciss_mtx);
28411fe6c4eeSScott Long     }
28421fe6c4eeSScott Long 
28431fe6c4eeSScott Long     for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
28441fe6c4eeSScott Long 	 CISS_PHYSICAL_BASE; i++) {
28451fe6c4eeSScott Long 	if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
28461fe6c4eeSScott Long 						 "ciss", sc,
28471fe6c4eeSScott Long 						 device_get_unit(sc->ciss_dev),
2848975b7318SScott Long 						 &sc->ciss_mtx, 1,
28491fe6c4eeSScott Long 						 sc->ciss_max_requests - 2,
28501fe6c4eeSScott Long 						 sc->ciss_cam_devq)) == NULL) {
28511fe6c4eeSScott Long 	    ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
28521fe6c4eeSScott Long 	    return (ENOMEM);
28531fe6c4eeSScott Long 	}
28541fe6c4eeSScott Long 
2855975b7318SScott Long 	mtx_lock(&sc->ciss_mtx);
2856b50569b7SScott Long 	if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
2857c6131460SPaul Saab 	    ciss_printf(sc, "can't register SCSI bus %d\n", i);
2858975b7318SScott Long 	    mtx_unlock(&sc->ciss_mtx);
28593a31b7ebSMike Smith 	    return (ENXIO);
28603a31b7ebSMike Smith 	}
2861975b7318SScott Long 	mtx_unlock(&sc->ciss_mtx);
2862c6131460SPaul Saab     }
28633a31b7ebSMike Smith 
28643a31b7ebSMike Smith     return(0);
28653a31b7ebSMike Smith }
28663a31b7ebSMike Smith 
28673a31b7ebSMike Smith /************************************************************************
28683a31b7ebSMike Smith  * Initiate a rescan of the 'logical devices' SIM
28693a31b7ebSMike Smith  */
28703a31b7ebSMike Smith static void
2871c6131460SPaul Saab ciss_cam_rescan_target(struct ciss_softc *sc, int bus, int target)
28723a31b7ebSMike Smith {
28733a31b7ebSMike Smith     union ccb		*ccb;
28743a31b7ebSMike Smith 
28753a31b7ebSMike Smith     debug_called(1);
28763a31b7ebSMike Smith 
287783c5d981SAlexander Motin     if ((ccb = xpt_alloc_ccb_nowait()) == NULL) {
28783a31b7ebSMike Smith 	ciss_printf(sc, "rescan failed (can't allocate CCB)\n");
28793a31b7ebSMike Smith 	return;
28803a31b7ebSMike Smith     }
28813a31b7ebSMike Smith 
288283c5d981SAlexander Motin     if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
288383c5d981SAlexander Motin 	    cam_sim_path(sc->ciss_cam_sim[bus]),
2884c6131460SPaul Saab 	    target, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
28853a31b7ebSMike Smith 	ciss_printf(sc, "rescan failed (can't create path)\n");
288683c5d981SAlexander Motin 	xpt_free_ccb(ccb);
28873a31b7ebSMike Smith 	return;
28883a31b7ebSMike Smith     }
288983c5d981SAlexander Motin     xpt_rescan(ccb);
28903a31b7ebSMike Smith     /* scan is now in progress */
28913a31b7ebSMike Smith }
28923a31b7ebSMike Smith 
28933a31b7ebSMike Smith /************************************************************************
28943a31b7ebSMike Smith  * Handle requests coming from CAM
28953a31b7ebSMike Smith  */
28963a31b7ebSMike Smith static void
28973a31b7ebSMike Smith ciss_cam_action(struct cam_sim *sim, union ccb *ccb)
28983a31b7ebSMike Smith {
2899440baa08SPaul Saab     struct ciss_softc	*sc;
2900440baa08SPaul Saab     struct ccb_scsiio	*csio;
2901c6131460SPaul Saab     int			bus, target;
29021fe6c4eeSScott Long     int			physical;
2903440baa08SPaul Saab 
2904440baa08SPaul Saab     sc = cam_sim_softc(sim);
2905c6131460SPaul Saab     bus = cam_sim_bus(sim);
2906440baa08SPaul Saab     csio = (struct ccb_scsiio *)&ccb->csio;
2907440baa08SPaul Saab     target = csio->ccb_h.target_id;
29081fe6c4eeSScott Long     physical = CISS_IS_PHYSICAL(bus);
2909440baa08SPaul Saab 
29103a31b7ebSMike Smith     switch (ccb->ccb_h.func_code) {
29113a31b7ebSMike Smith 
29123a31b7ebSMike Smith 	/* perform SCSI I/O */
29133a31b7ebSMike Smith     case XPT_SCSI_IO:
2914440baa08SPaul Saab 	if (!ciss_cam_action_io(sim, csio))
29153a31b7ebSMike Smith 	    return;
29163a31b7ebSMike Smith 	break;
29173a31b7ebSMike Smith 
29183a31b7ebSMike Smith 	/* perform geometry calculations */
29193a31b7ebSMike Smith     case XPT_CALC_GEOMETRY:
29203a31b7ebSMike Smith     {
29213a31b7ebSMike Smith 	struct ccb_calc_geometry	*ccg = &ccb->ccg;
29221fe6c4eeSScott Long 	struct ciss_ldrive		*ld;
29233a31b7ebSMike Smith 
29243a31b7ebSMike Smith 	debug(1, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
29253a31b7ebSMike Smith 
29261fe6c4eeSScott Long 	ld = NULL;
29271fe6c4eeSScott Long 	if (!physical)
29281fe6c4eeSScott Long 	    ld = &sc->ciss_logical[bus][target];
29291fe6c4eeSScott Long 
29303a31b7ebSMike Smith 	/*
2931440baa08SPaul Saab 	 * Use the cached geometry settings unless the fault tolerance
2932440baa08SPaul Saab 	 * is invalid.
29333a31b7ebSMike Smith 	 */
29341fe6c4eeSScott Long 	if (physical || ld->cl_geometry.fault_tolerance == 0xFF) {
2935440baa08SPaul Saab 	    u_int32_t			secs_per_cylinder;
2936440baa08SPaul Saab 
29373a31b7ebSMike Smith 	    ccg->heads = 255;
29383a31b7ebSMike Smith 	    ccg->secs_per_track = 32;
29393a31b7ebSMike Smith 	    secs_per_cylinder = ccg->heads * ccg->secs_per_track;
29403a31b7ebSMike Smith 	    ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2941440baa08SPaul Saab 	} else {
2942440baa08SPaul Saab 	    ccg->heads = ld->cl_geometry.heads;
2943440baa08SPaul Saab 	    ccg->secs_per_track = ld->cl_geometry.sectors;
2944440baa08SPaul Saab 	    ccg->cylinders = ntohs(ld->cl_geometry.cylinders);
2945440baa08SPaul Saab 	}
29463a31b7ebSMike Smith 	ccb->ccb_h.status = CAM_REQ_CMP;
29473a31b7ebSMike Smith         break;
29483a31b7ebSMike Smith     }
29493a31b7ebSMike Smith 
29503a31b7ebSMike Smith 	/* handle path attribute inquiry */
29513a31b7ebSMike Smith     case XPT_PATH_INQ:
29523a31b7ebSMike Smith     {
29533a31b7ebSMike Smith 	struct ccb_pathinq	*cpi = &ccb->cpi;
29543a31b7ebSMike Smith 
29553a31b7ebSMike Smith 	debug(1, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
29563a31b7ebSMike Smith 
29573a31b7ebSMike Smith 	cpi->version_num = 1;
29583a31b7ebSMike Smith 	cpi->hba_inquiry = PI_TAG_ABLE;	/* XXX is this correct? */
29593a31b7ebSMike Smith 	cpi->target_sprt = 0;
29603a31b7ebSMike Smith 	cpi->hba_misc = 0;
29613a31b7ebSMike Smith 	cpi->max_target = CISS_MAX_LOGICAL;
29623a31b7ebSMike Smith 	cpi->max_lun = 0;		/* 'logical drive' channel only */
29633a31b7ebSMike Smith 	cpi->initiator_id = CISS_MAX_LOGICAL;
29643a31b7ebSMike Smith 	strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
29653a31b7ebSMike Smith         strncpy(cpi->hba_vid, "msmith@freebsd.org", HBA_IDLEN);
29663a31b7ebSMike Smith         strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
29673a31b7ebSMike Smith         cpi->unit_number = cam_sim_unit(sim);
29683a31b7ebSMike Smith         cpi->bus_id = cam_sim_bus(sim);
29693a31b7ebSMike Smith 	cpi->base_transfer_speed = 132 * 1024;	/* XXX what to set this to? */
2970fa9ed865SMatt Jacob 	cpi->transport = XPORT_SPI;
2971fa9ed865SMatt Jacob 	cpi->transport_version = 2;
2972fa9ed865SMatt Jacob 	cpi->protocol = PROTO_SCSI;
2973fa9ed865SMatt Jacob 	cpi->protocol_version = SCSI_REV_2;
297452c9ce25SScott Long 	cpi->maxio = (CISS_MAX_SG_ELEMENTS - 1) * PAGE_SIZE;
29753a31b7ebSMike Smith 	ccb->ccb_h.status = CAM_REQ_CMP;
29763a31b7ebSMike Smith 	break;
29773a31b7ebSMike Smith     }
29783a31b7ebSMike Smith 
29793a31b7ebSMike Smith     case XPT_GET_TRAN_SETTINGS:
29803a31b7ebSMike Smith     {
29813a31b7ebSMike Smith 	struct ccb_trans_settings	*cts = &ccb->cts;
29823a31b7ebSMike Smith 	int				bus, target;
298363b9228bSScott Long 	struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
298463b9228bSScott Long 	struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
29853a31b7ebSMike Smith 
29863a31b7ebSMike Smith 	bus = cam_sim_bus(sim);
29873a31b7ebSMike Smith 	target = cts->ccb_h.target_id;
29883a31b7ebSMike Smith 
29893a31b7ebSMike Smith 	debug(1, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target);
29903a31b7ebSMike Smith 	/* disconnect always OK */
2991fa9ed865SMatt Jacob 	cts->protocol = PROTO_SCSI;
2992fa9ed865SMatt Jacob 	cts->protocol_version = SCSI_REV_2;
2993fa9ed865SMatt Jacob 	cts->transport = XPORT_SPI;
2994fa9ed865SMatt Jacob 	cts->transport_version = 2;
2995fa9ed865SMatt Jacob 
2996fa9ed865SMatt Jacob 	spi->valid = CTS_SPI_VALID_DISC;
2997fa9ed865SMatt Jacob 	spi->flags = CTS_SPI_FLAGS_DISC_ENB;
29983a31b7ebSMike Smith 
299963b9228bSScott Long 	scsi->valid = CTS_SCSI_VALID_TQ;
300063b9228bSScott Long 	scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
300163b9228bSScott Long 
30023a31b7ebSMike Smith 	cts->ccb_h.status = CAM_REQ_CMP;
30033a31b7ebSMike Smith 	break;
30043a31b7ebSMike Smith     }
30053a31b7ebSMike Smith 
30063a31b7ebSMike Smith     default:		/* we can't do this */
30073a31b7ebSMike Smith 	debug(1, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
30083a31b7ebSMike Smith 	ccb->ccb_h.status = CAM_REQ_INVALID;
30093a31b7ebSMike Smith 	break;
30103a31b7ebSMike Smith     }
30113a31b7ebSMike Smith 
30123a31b7ebSMike Smith     xpt_done(ccb);
30133a31b7ebSMike Smith }
30143a31b7ebSMike Smith 
30153a31b7ebSMike Smith /************************************************************************
30163a31b7ebSMike Smith  * Handle a CAM SCSI I/O request.
30173a31b7ebSMike Smith  */
30183a31b7ebSMike Smith static int
30193a31b7ebSMike Smith ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio)
30203a31b7ebSMike Smith {
30213a31b7ebSMike Smith     struct ciss_softc	*sc;
30223a31b7ebSMike Smith     int			bus, target;
30233a31b7ebSMike Smith     struct ciss_request	*cr;
30243a31b7ebSMike Smith     struct ciss_command	*cc;
30253a31b7ebSMike Smith     int			error;
30263a31b7ebSMike Smith 
30273a31b7ebSMike Smith     sc = cam_sim_softc(sim);
30283a31b7ebSMike Smith     bus = cam_sim_bus(sim);
30293a31b7ebSMike Smith     target = csio->ccb_h.target_id;
30303a31b7ebSMike Smith 
30313a31b7ebSMike Smith     debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, csio->ccb_h.target_lun);
30323a31b7ebSMike Smith 
30333a31b7ebSMike Smith     /* check that the CDB pointer is not to a physical address */
30343a31b7ebSMike Smith     if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
30353a31b7ebSMike Smith 	debug(3, "  CDB pointer is to physical address");
30363a31b7ebSMike Smith 	csio->ccb_h.status = CAM_REQ_CMP_ERR;
30373a31b7ebSMike Smith     }
30383a31b7ebSMike Smith 
30393a31b7ebSMike Smith     /* if there is data transfer, it must be to/from a virtual address */
30403a31b7ebSMike Smith     if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
30413a31b7ebSMike Smith 	if (csio->ccb_h.flags & CAM_DATA_PHYS) {		/* we can't map it */
30423a31b7ebSMike Smith 	    debug(3, "  data pointer is to physical address");
30433a31b7ebSMike Smith 	    csio->ccb_h.status = CAM_REQ_CMP_ERR;
30443a31b7ebSMike Smith 	}
30453a31b7ebSMike Smith 	if (csio->ccb_h.flags & CAM_SCATTER_VALID) {	/* we want to do the s/g setup */
30463a31b7ebSMike Smith 	    debug(3, "  data has premature s/g setup");
30473a31b7ebSMike Smith 	    csio->ccb_h.status = CAM_REQ_CMP_ERR;
30483a31b7ebSMike Smith 	}
30493a31b7ebSMike Smith     }
30503a31b7ebSMike Smith 
30513a31b7ebSMike Smith     /* abandon aborted ccbs or those that have failed validation */
30523a31b7ebSMike Smith     if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
30533a31b7ebSMike Smith 	debug(3, "abandoning CCB due to abort/validation failure");
30543a31b7ebSMike Smith 	return(EINVAL);
30553a31b7ebSMike Smith     }
30563a31b7ebSMike Smith 
30573a31b7ebSMike Smith     /* handle emulation of some SCSI commands ourself */
30583a31b7ebSMike Smith     if (ciss_cam_emulate(sc, csio))
30593a31b7ebSMike Smith 	return(0);
30603a31b7ebSMike Smith 
30613a31b7ebSMike Smith     /*
30623a31b7ebSMike Smith      * Get a request to manage this command.  If we can't, return the
30633a31b7ebSMike Smith      * ccb, freeze the queue and flag so that we unfreeze it when a
30643a31b7ebSMike Smith      * request completes.
30653a31b7ebSMike Smith      */
30663a31b7ebSMike Smith     if ((error = ciss_get_request(sc, &cr)) != 0) {
30671fe6c4eeSScott Long 	xpt_freeze_simq(sim, 1);
30680a65b79fSAlexander Motin 	csio->ccb_h.status |= CAM_RELEASE_SIMQ;
30693a31b7ebSMike Smith 	csio->ccb_h.status |= CAM_REQUEUE_REQ;
30703a31b7ebSMike Smith 	return(error);
30713a31b7ebSMike Smith     }
30723a31b7ebSMike Smith 
30733a31b7ebSMike Smith     /*
30743a31b7ebSMike Smith      * Build the command.
30753a31b7ebSMike Smith      */
30762fdaa90dSScott Long     cc = cr->cr_cc;
30773a31b7ebSMike Smith     cr->cr_data = csio->data_ptr;
30783a31b7ebSMike Smith     cr->cr_length = csio->dxfer_len;
30793a31b7ebSMike Smith     cr->cr_complete = ciss_cam_complete;
30803a31b7ebSMike Smith     cr->cr_private = csio;
30813a31b7ebSMike Smith 
3082c6131460SPaul Saab     /*
3083c6131460SPaul Saab      * Target the right logical volume.
3084c6131460SPaul Saab      */
30851fe6c4eeSScott Long     if (CISS_IS_PHYSICAL(bus))
30861fe6c4eeSScott Long 	cc->header.address =
30871fe6c4eeSScott Long 	    sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_address;
30881fe6c4eeSScott Long     else
30891fe6c4eeSScott Long 	cc->header.address =
30901fe6c4eeSScott Long 	    sc->ciss_logical[bus][target].cl_address;
30913a31b7ebSMike Smith     cc->cdb.cdb_length = csio->cdb_len;
30923a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
30933a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;	/* XXX ordered tags? */
30943a31b7ebSMike Smith     if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
30953a31b7ebSMike Smith 	cr->cr_flags = CISS_REQ_DATAOUT;
30963a31b7ebSMike Smith 	cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
30973a31b7ebSMike Smith     } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
30983a31b7ebSMike Smith 	cr->cr_flags = CISS_REQ_DATAIN;
30993a31b7ebSMike Smith 	cc->cdb.direction = CISS_CDB_DIRECTION_READ;
31003a31b7ebSMike Smith     } else {
31013a31b7ebSMike Smith 	cr->cr_flags = 0;
31023a31b7ebSMike Smith 	cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
31033a31b7ebSMike Smith     }
31043a31b7ebSMike Smith     cc->cdb.timeout = (csio->ccb_h.timeout / 1000) + 1;
31053a31b7ebSMike Smith     if (csio->ccb_h.flags & CAM_CDB_POINTER) {
31063a31b7ebSMike Smith 	bcopy(csio->cdb_io.cdb_ptr, &cc->cdb.cdb[0], csio->cdb_len);
31073a31b7ebSMike Smith     } else {
31083a31b7ebSMike Smith 	bcopy(csio->cdb_io.cdb_bytes, &cc->cdb.cdb[0], csio->cdb_len);
31093a31b7ebSMike Smith     }
31103a31b7ebSMike Smith 
31113a31b7ebSMike Smith     /*
31123a31b7ebSMike Smith      * Submit the request to the adapter.
31133a31b7ebSMike Smith      *
31143a31b7ebSMike Smith      * Note that this may fail if we're unable to map the request (and
31153a31b7ebSMike Smith      * if we ever learn a transport layer other than simple, may fail
31163a31b7ebSMike Smith      * if the adapter rejects the command).
31173a31b7ebSMike Smith      */
31183a31b7ebSMike Smith     if ((error = ciss_start(cr)) != 0) {
31191fe6c4eeSScott Long 	xpt_freeze_simq(sim, 1);
3120639717c8SPaul Saab 	csio->ccb_h.status |= CAM_RELEASE_SIMQ;
31210a65b79fSAlexander Motin 	if (error == EINPROGRESS) {
3122639717c8SPaul Saab 	    error = 0;
3123639717c8SPaul Saab 	} else {
31243a31b7ebSMike Smith 	    csio->ccb_h.status |= CAM_REQUEUE_REQ;
31253a31b7ebSMike Smith 	    ciss_release_request(cr);
3126639717c8SPaul Saab 	}
31273a31b7ebSMike Smith 	return(error);
31283a31b7ebSMike Smith     }
31293a31b7ebSMike Smith 
31303a31b7ebSMike Smith     return(0);
31313a31b7ebSMike Smith }
31323a31b7ebSMike Smith 
31333a31b7ebSMike Smith /************************************************************************
31343a31b7ebSMike Smith  * Emulate SCSI commands the adapter doesn't handle as we might like.
31353a31b7ebSMike Smith  */
31363a31b7ebSMike Smith static int
31373a31b7ebSMike Smith ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio)
31383a31b7ebSMike Smith {
3139c6131460SPaul Saab     int		bus, target;
31403a31b7ebSMike Smith     u_int8_t	opcode;
31413a31b7ebSMike Smith 
31423a31b7ebSMike Smith     target = csio->ccb_h.target_id;
3143c6131460SPaul Saab     bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
31443a31b7ebSMike Smith     opcode = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
31453a31b7ebSMike Smith 	*(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0];
31463a31b7ebSMike Smith 
31471fe6c4eeSScott Long     if (CISS_IS_PHYSICAL(bus)) {
31481fe6c4eeSScott Long 	if (sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_online != 1) {
31490a65b79fSAlexander Motin 	    csio->ccb_h.status |= CAM_SEL_TIMEOUT;
31501fe6c4eeSScott Long 	    xpt_done((union ccb *)csio);
31511fe6c4eeSScott Long 	    return(1);
31521fe6c4eeSScott Long 	} else
31531fe6c4eeSScott Long 	    return(0);
31541fe6c4eeSScott Long     }
31551fe6c4eeSScott Long 
31563a31b7ebSMike Smith     /*
31576f71a953SPaul Saab      * Handle requests for volumes that don't exist or are not online.
31586f71a953SPaul Saab      * A selection timeout is slightly better than an illegal request.
31596f71a953SPaul Saab      * Other errors might be better.
31603a31b7ebSMike Smith      */
31616f71a953SPaul Saab     if (sc->ciss_logical[bus][target].cl_status != CISS_LD_ONLINE) {
31620a65b79fSAlexander Motin 	csio->ccb_h.status |= CAM_SEL_TIMEOUT;
31633a31b7ebSMike Smith 	xpt_done((union ccb *)csio);
31643a31b7ebSMike Smith 	return(1);
31653a31b7ebSMike Smith     }
31663a31b7ebSMike Smith 
31673a31b7ebSMike Smith     /* if we have to fake Synchronise Cache */
31683a31b7ebSMike Smith     if (sc->ciss_flags & CISS_FLAG_FAKE_SYNCH) {
31693a31b7ebSMike Smith 	/*
31703a31b7ebSMike Smith 	 * If this is a Synchronise Cache command, typically issued when
31713a31b7ebSMike Smith 	 * a device is closed, flush the adapter and complete now.
31723a31b7ebSMike Smith 	 */
31733a31b7ebSMike Smith 	if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
31743a31b7ebSMike Smith 	     *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE) {
31753a31b7ebSMike Smith 	    ciss_flush_adapter(sc);
31760a65b79fSAlexander Motin 	    csio->ccb_h.status |= CAM_REQ_CMP;
31773a31b7ebSMike Smith 	    xpt_done((union ccb *)csio);
31783a31b7ebSMike Smith 	    return(1);
31793a31b7ebSMike Smith 	}
31803a31b7ebSMike Smith     }
31813a31b7ebSMike Smith 
31823a31b7ebSMike Smith     return(0);
31833a31b7ebSMike Smith }
31843a31b7ebSMike Smith 
31853a31b7ebSMike Smith /************************************************************************
31863a31b7ebSMike Smith  * Check for possibly-completed commands.
31873a31b7ebSMike Smith  */
31883a31b7ebSMike Smith static void
31893a31b7ebSMike Smith ciss_cam_poll(struct cam_sim *sim)
31903a31b7ebSMike Smith {
319122657ce1SScott Long     cr_qhead_t qh;
31923a31b7ebSMike Smith     struct ciss_softc	*sc = cam_sim_softc(sim);
31933a31b7ebSMike Smith 
31943a31b7ebSMike Smith     debug_called(2);
31953a31b7ebSMike Smith 
319622657ce1SScott Long     STAILQ_INIT(&qh);
319722657ce1SScott Long     if (sc->ciss_perf)
319822657ce1SScott Long 	ciss_perf_done(sc, &qh);
319922657ce1SScott Long     else
320022657ce1SScott Long 	ciss_done(sc, &qh);
320122657ce1SScott Long     ciss_complete(sc, &qh);
32023a31b7ebSMike Smith }
32033a31b7ebSMike Smith 
32043a31b7ebSMike Smith /************************************************************************
32053a31b7ebSMike Smith  * Handle completion of a command - pass results back through the CCB
32063a31b7ebSMike Smith  */
32073a31b7ebSMike Smith static void
32083a31b7ebSMike Smith ciss_cam_complete(struct ciss_request *cr)
32093a31b7ebSMike Smith {
32103a31b7ebSMike Smith     struct ciss_softc		*sc;
32113a31b7ebSMike Smith     struct ciss_command		*cc;
32123a31b7ebSMike Smith     struct ciss_error_info	*ce;
32133a31b7ebSMike Smith     struct ccb_scsiio		*csio;
32143a31b7ebSMike Smith     int				scsi_status;
32153a31b7ebSMike Smith     int				command_status;
32163a31b7ebSMike Smith 
32173a31b7ebSMike Smith     debug_called(2);
32183a31b7ebSMike Smith 
32193a31b7ebSMike Smith     sc = cr->cr_sc;
32202fdaa90dSScott Long     cc = cr->cr_cc;
32213a31b7ebSMike Smith     ce = (struct ciss_error_info *)&(cc->sg[0]);
32223a31b7ebSMike Smith     csio = (struct ccb_scsiio *)cr->cr_private;
32233a31b7ebSMike Smith 
32243a31b7ebSMike Smith     /*
32253a31b7ebSMike Smith      * Extract status values from request.
32263a31b7ebSMike Smith      */
32273a31b7ebSMike Smith     ciss_report_request(cr, &command_status, &scsi_status);
32283a31b7ebSMike Smith     csio->scsi_status = scsi_status;
32293a31b7ebSMike Smith 
32303a31b7ebSMike Smith     /*
32313a31b7ebSMike Smith      * Handle specific SCSI status values.
32323a31b7ebSMike Smith      */
32333a31b7ebSMike Smith     switch(scsi_status) {
32343a31b7ebSMike Smith 	/* no status due to adapter error */
32353a31b7ebSMike Smith     case -1:
32363a31b7ebSMike Smith 	debug(0, "adapter error");
32370a65b79fSAlexander Motin 	csio->ccb_h.status |= CAM_REQ_CMP_ERR;
32383a31b7ebSMike Smith 	break;
32393a31b7ebSMike Smith 
32403a31b7ebSMike Smith 	/* no status due to command completed OK */
32413a31b7ebSMike Smith     case SCSI_STATUS_OK:		/* CISS_SCSI_STATUS_GOOD */
32423a31b7ebSMike Smith 	debug(2, "SCSI_STATUS_OK");
32430a65b79fSAlexander Motin 	csio->ccb_h.status |= CAM_REQ_CMP;
32443a31b7ebSMike Smith 	break;
32453a31b7ebSMike Smith 
32463a31b7ebSMike Smith 	/* check condition, sense data included */
32473a31b7ebSMike Smith     case SCSI_STATUS_CHECK_COND:	/* CISS_SCSI_STATUS_CHECK_CONDITION */
3248c6131460SPaul Saab 	debug(0, "SCSI_STATUS_CHECK_COND  sense size %d  resid %d\n",
32493a31b7ebSMike Smith 	      ce->sense_length, ce->residual_count);
32503a31b7ebSMike Smith 	bzero(&csio->sense_data, SSD_FULL_SIZE);
32513a31b7ebSMike Smith 	bcopy(&ce->sense_info[0], &csio->sense_data, ce->sense_length);
32523a31b7ebSMike Smith 	csio->sense_len = ce->sense_length;
32533a31b7ebSMike Smith 	csio->resid = ce->residual_count;
32540a65b79fSAlexander Motin 	csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
32553a31b7ebSMike Smith #ifdef CISS_DEBUG
32563a31b7ebSMike Smith 	{
32573a31b7ebSMike Smith 	    struct scsi_sense_data	*sns = (struct scsi_sense_data *)&ce->sense_info[0];
32583a31b7ebSMike Smith 	    debug(0, "sense key %x", sns->flags & SSD_KEY);
32593a31b7ebSMike Smith 	}
32603a31b7ebSMike Smith #endif
32613a31b7ebSMike Smith 	break;
32623a31b7ebSMike Smith 
32633a31b7ebSMike Smith     case SCSI_STATUS_BUSY:		/* CISS_SCSI_STATUS_BUSY */
32643a31b7ebSMike Smith 	debug(0, "SCSI_STATUS_BUSY");
32650a65b79fSAlexander Motin 	csio->ccb_h.status |= CAM_SCSI_BUSY;
32663a31b7ebSMike Smith 	break;
32673a31b7ebSMike Smith 
32683a31b7ebSMike Smith     default:
32693a31b7ebSMike Smith 	debug(0, "unknown status 0x%x", csio->scsi_status);
32700a65b79fSAlexander Motin 	csio->ccb_h.status |= CAM_REQ_CMP_ERR;
32713a31b7ebSMike Smith 	break;
32723a31b7ebSMike Smith     }
32733a31b7ebSMike Smith 
32743a31b7ebSMike Smith     /* handle post-command fixup */
32753a31b7ebSMike Smith     ciss_cam_complete_fixup(sc, csio);
32763a31b7ebSMike Smith 
32773a31b7ebSMike Smith     ciss_release_request(cr);
327822657ce1SScott Long     xpt_done((union ccb *)csio);
32793a31b7ebSMike Smith }
32803a31b7ebSMike Smith 
32813a31b7ebSMike Smith /********************************************************************************
32823a31b7ebSMike Smith  * Fix up the result of some commands here.
32833a31b7ebSMike Smith  */
32843a31b7ebSMike Smith static void
32853a31b7ebSMike Smith ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio)
32863a31b7ebSMike Smith {
32873a31b7ebSMike Smith     struct scsi_inquiry_data	*inq;
32883a31b7ebSMike Smith     struct ciss_ldrive		*cl;
328957ae362cSAlexander Motin     uint8_t			*cdb;
3290c6131460SPaul Saab     int				bus, target;
32913a31b7ebSMike Smith 
329257ae362cSAlexander Motin     cdb = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
329357ae362cSAlexander Motin 	 (uint8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes;
329457ae362cSAlexander Motin     if (cdb[0] == INQUIRY &&
329557ae362cSAlexander Motin 	(cdb[1] & SI_EVPD) == 0 &&
329657ae362cSAlexander Motin 	(csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN &&
329757ae362cSAlexander Motin 	csio->dxfer_len >= SHORT_INQUIRY_LENGTH) {
32983a31b7ebSMike Smith 
32993a31b7ebSMike Smith 	inq = (struct scsi_inquiry_data *)csio->data_ptr;
33003a31b7ebSMike Smith 	target = csio->ccb_h.target_id;
3301c6131460SPaul Saab 	bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
33021fe6c4eeSScott Long 
33031fe6c4eeSScott Long 	/*
33041fe6c4eeSScott Long 	 * Don't let hard drives be seen by the DA driver.  They will still be
33051fe6c4eeSScott Long 	 * attached by the PASS driver.
33061fe6c4eeSScott Long 	 */
33071fe6c4eeSScott Long 	if (CISS_IS_PHYSICAL(bus)) {
33081fe6c4eeSScott Long 	    if (SID_TYPE(inq) == T_DIRECT)
33091fe6c4eeSScott Long 		inq->device = (inq->device & 0xe0) | T_NODEVICE;
33101fe6c4eeSScott Long 	    return;
33111fe6c4eeSScott Long 	}
33121fe6c4eeSScott Long 
3313c6131460SPaul Saab 	cl = &sc->ciss_logical[bus][target];
33143a31b7ebSMike Smith 
3315d4aa427fSPaul Saab 	padstr(inq->vendor, "COMPAQ", 8);
3316d4aa427fSPaul Saab 	padstr(inq->product, ciss_name_ldrive_org(cl->cl_ldrive->fault_tolerance), 8);
3317d4aa427fSPaul Saab 	padstr(inq->revision, ciss_name_ldrive_status(cl->cl_lstatus->status), 16);
33183a31b7ebSMike Smith     }
33193a31b7ebSMike Smith }
33203a31b7ebSMike Smith 
33213a31b7ebSMike Smith 
33223a31b7ebSMike Smith /********************************************************************************
3323d4aa427fSPaul Saab  * Find a peripheral attached at (target)
33243a31b7ebSMike Smith  */
33253a31b7ebSMike Smith static struct cam_periph *
3326c6131460SPaul Saab ciss_find_periph(struct ciss_softc *sc, int bus, int target)
33273a31b7ebSMike Smith {
33283a31b7ebSMike Smith     struct cam_periph	*periph;
33293a31b7ebSMike Smith     struct cam_path	*path;
33303a31b7ebSMike Smith     int			status;
33313a31b7ebSMike Smith 
3332c6131460SPaul Saab     status = xpt_create_path(&path, NULL, cam_sim_path(sc->ciss_cam_sim[bus]),
3333c6131460SPaul Saab 			     target, 0);
33343a31b7ebSMike Smith     if (status == CAM_REQ_CMP) {
33353a31b7ebSMike Smith 	periph = cam_periph_find(path, NULL);
33363a31b7ebSMike Smith 	xpt_free_path(path);
33373a31b7ebSMike Smith     } else {
33383a31b7ebSMike Smith 	periph = NULL;
33393a31b7ebSMike Smith     }
33403a31b7ebSMike Smith     return(periph);
33413a31b7ebSMike Smith }
33423a31b7ebSMike Smith 
33433a31b7ebSMike Smith /********************************************************************************
33443a31b7ebSMike Smith  * Name the device at (target)
33453a31b7ebSMike Smith  *
33463a31b7ebSMike Smith  * XXX is this strictly correct?
33473a31b7ebSMike Smith  */
334837c84183SPoul-Henning Kamp static int
3349c6131460SPaul Saab ciss_name_device(struct ciss_softc *sc, int bus, int target)
33503a31b7ebSMike Smith {
33513a31b7ebSMike Smith     struct cam_periph	*periph;
33523a31b7ebSMike Smith 
3353d49f1379SPaul Saab     if (CISS_IS_PHYSICAL(bus))
33541fe6c4eeSScott Long 	return (0);
3355c6131460SPaul Saab     if ((periph = ciss_find_periph(sc, bus, target)) != NULL) {
33561fe6c4eeSScott Long 	sprintf(sc->ciss_logical[bus][target].cl_name, "%s%d",
33571fe6c4eeSScott Long 		periph->periph_name, periph->unit_number);
33583a31b7ebSMike Smith 	return(0);
33593a31b7ebSMike Smith     }
3360c6131460SPaul Saab     sc->ciss_logical[bus][target].cl_name[0] = 0;
33613a31b7ebSMike Smith     return(ENOENT);
33623a31b7ebSMike Smith }
33633a31b7ebSMike Smith 
33643a31b7ebSMike Smith /************************************************************************
33653a31b7ebSMike Smith  * Periodic status monitoring.
33663a31b7ebSMike Smith  */
33673a31b7ebSMike Smith static void
33683a31b7ebSMike Smith ciss_periodic(void *arg)
33693a31b7ebSMike Smith {
33703a31b7ebSMike Smith     struct ciss_softc	*sc;
3371e08d902aSMitsuru IWASAKI     struct ciss_request	*cr = NULL;
3372e08d902aSMitsuru IWASAKI     struct ciss_command	*cc = NULL;
3373e08d902aSMitsuru IWASAKI     int			error = 0;
33743a31b7ebSMike Smith 
33753a31b7ebSMike Smith     debug_called(1);
33763a31b7ebSMike Smith 
33773a31b7ebSMike Smith     sc = (struct ciss_softc *)arg;
33783a31b7ebSMike Smith 
33793a31b7ebSMike Smith     /*
33803a31b7ebSMike Smith      * Check the adapter heartbeat.
33813a31b7ebSMike Smith      */
33823a31b7ebSMike Smith     if (sc->ciss_cfg->heartbeat == sc->ciss_heartbeat) {
33833a31b7ebSMike Smith 	sc->ciss_heart_attack++;
33843a31b7ebSMike Smith 	debug(0, "adapter heart attack in progress 0x%x/%d",
33853a31b7ebSMike Smith 	      sc->ciss_heartbeat, sc->ciss_heart_attack);
33863a31b7ebSMike Smith 	if (sc->ciss_heart_attack == 3) {
33873a31b7ebSMike Smith 	    ciss_printf(sc, "ADAPTER HEARTBEAT FAILED\n");
3388e08d902aSMitsuru IWASAKI 	    ciss_disable_adapter(sc);
3389e08d902aSMitsuru IWASAKI 	    return;
33903a31b7ebSMike Smith 	}
33913a31b7ebSMike Smith     } else {
33923a31b7ebSMike Smith 	sc->ciss_heartbeat = sc->ciss_cfg->heartbeat;
33933a31b7ebSMike Smith 	sc->ciss_heart_attack = 0;
33943a31b7ebSMike Smith 	debug(3, "new heartbeat 0x%x", sc->ciss_heartbeat);
33953a31b7ebSMike Smith     }
33963a31b7ebSMike Smith 
33973a31b7ebSMike Smith     /*
3398e08d902aSMitsuru IWASAKI      * Send the NOP message and wait for a response.
3399e08d902aSMitsuru IWASAKI      */
34005a3c4d69SMitsuru IWASAKI     if (ciss_nop_message_heartbeat != 0 && (error = ciss_get_request(sc, &cr)) == 0) {
34012fdaa90dSScott Long 	cc = cr->cr_cc;
340254d02e0aSMitsuru IWASAKI 	cr->cr_complete = ciss_nop_complete;
3403e08d902aSMitsuru IWASAKI 	cc->cdb.cdb_length = 1;
3404e08d902aSMitsuru IWASAKI 	cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
3405e08d902aSMitsuru IWASAKI 	cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3406e08d902aSMitsuru IWASAKI 	cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
3407e08d902aSMitsuru IWASAKI 	cc->cdb.timeout = 0;
3408e08d902aSMitsuru IWASAKI 	cc->cdb.cdb[0] = CISS_OPCODE_MESSAGE_NOP;
3409e08d902aSMitsuru IWASAKI 
341054d02e0aSMitsuru IWASAKI 	if ((error = ciss_start(cr)) != 0) {
3411e08d902aSMitsuru IWASAKI 	    ciss_printf(sc, "SENDING NOP MESSAGE FAILED\n");
3412e08d902aSMitsuru IWASAKI 	}
3413e08d902aSMitsuru IWASAKI     }
3414e08d902aSMitsuru IWASAKI 
3415e08d902aSMitsuru IWASAKI     /*
34163a31b7ebSMike Smith      * If the notify event request has died for some reason, or has
34173a31b7ebSMike Smith      * not started yet, restart it.
34183a31b7ebSMike Smith      */
34193a31b7ebSMike Smith     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK)) {
34203a31b7ebSMike Smith 	debug(0, "(re)starting Event Notify chain");
34213a31b7ebSMike Smith 	ciss_notify_event(sc);
34223a31b7ebSMike Smith     }
34233a31b7ebSMike Smith 
34243a31b7ebSMike Smith     /*
34253a31b7ebSMike Smith      * Reschedule.
34263a31b7ebSMike Smith      */
3427975b7318SScott Long     callout_reset(&sc->ciss_periodic, CISS_HEARTBEAT_RATE * hz, ciss_periodic, sc);
34283a31b7ebSMike Smith }
34293a31b7ebSMike Smith 
343054d02e0aSMitsuru IWASAKI static void
343154d02e0aSMitsuru IWASAKI ciss_nop_complete(struct ciss_request *cr)
343254d02e0aSMitsuru IWASAKI {
343354d02e0aSMitsuru IWASAKI     struct ciss_softc		*sc;
34340aeee4bdSMitsuru IWASAKI     static int			first_time = 1;
343554d02e0aSMitsuru IWASAKI 
343654d02e0aSMitsuru IWASAKI     sc = cr->cr_sc;
343754d02e0aSMitsuru IWASAKI     if (ciss_report_request(cr, NULL, NULL) != 0) {
34380aeee4bdSMitsuru IWASAKI 	if (first_time == 1) {
34390aeee4bdSMitsuru IWASAKI 	    first_time = 0;
34400aeee4bdSMitsuru IWASAKI 	    ciss_printf(sc, "SENDING NOP MESSAGE FAILED (not logging anymore)\n");
34410aeee4bdSMitsuru IWASAKI 	}
344254d02e0aSMitsuru IWASAKI     }
344354d02e0aSMitsuru IWASAKI 
344454d02e0aSMitsuru IWASAKI     ciss_release_request(cr);
344554d02e0aSMitsuru IWASAKI }
344654d02e0aSMitsuru IWASAKI 
34473a31b7ebSMike Smith /************************************************************************
3448e08d902aSMitsuru IWASAKI  * Disable the adapter.
3449e08d902aSMitsuru IWASAKI  *
3450e08d902aSMitsuru IWASAKI  * The all requests in completed queue is failed with hardware error.
3451e08d902aSMitsuru IWASAKI  * This will cause failover in a multipath configuration.
3452e08d902aSMitsuru IWASAKI  */
3453e08d902aSMitsuru IWASAKI static void
3454e08d902aSMitsuru IWASAKI ciss_disable_adapter(struct ciss_softc *sc)
3455e08d902aSMitsuru IWASAKI {
345622657ce1SScott Long     cr_qhead_t			qh;
3457e08d902aSMitsuru IWASAKI     struct ciss_request		*cr;
3458e08d902aSMitsuru IWASAKI     struct ciss_command		*cc;
3459e08d902aSMitsuru IWASAKI     struct ciss_error_info	*ce;
346022657ce1SScott Long     int				i;
3461e08d902aSMitsuru IWASAKI 
3462e08d902aSMitsuru IWASAKI     CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
3463e08d902aSMitsuru IWASAKI     pci_disable_busmaster(sc->ciss_dev);
3464e08d902aSMitsuru IWASAKI     sc->ciss_flags &= ~CISS_FLAG_RUNNING;
3465e08d902aSMitsuru IWASAKI 
346622657ce1SScott Long     for (i = 1; i < sc->ciss_max_requests; i++) {
346722657ce1SScott Long 	cr = &sc->ciss_request[i];
346822657ce1SScott Long 	if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
346922657ce1SScott Long 	    continue;
3470e08d902aSMitsuru IWASAKI 
34712fdaa90dSScott Long 	cc = cr->cr_cc;
3472e08d902aSMitsuru IWASAKI 	ce = (struct ciss_error_info *)&(cc->sg[0]);
3473e08d902aSMitsuru IWASAKI 	ce->command_status = CISS_CMD_STATUS_HARDWARE_ERROR;
347422657ce1SScott Long 	ciss_enqueue_complete(cr, &qh);
3475e08d902aSMitsuru IWASAKI     }
3476e08d902aSMitsuru IWASAKI 
3477e08d902aSMitsuru IWASAKI     for (;;) {
347822657ce1SScott Long 	if ((cr = ciss_dequeue_complete(sc, &qh)) == NULL)
3479e08d902aSMitsuru IWASAKI 	    break;
3480e08d902aSMitsuru IWASAKI 
3481e08d902aSMitsuru IWASAKI 	/*
3482e08d902aSMitsuru IWASAKI 	 * If the request has a callback, invoke it.
3483e08d902aSMitsuru IWASAKI 	 */
3484e08d902aSMitsuru IWASAKI 	if (cr->cr_complete != NULL) {
3485e08d902aSMitsuru IWASAKI 	    cr->cr_complete(cr);
3486e08d902aSMitsuru IWASAKI 	    continue;
3487e08d902aSMitsuru IWASAKI 	}
3488e08d902aSMitsuru IWASAKI 
3489e08d902aSMitsuru IWASAKI 	/*
3490e08d902aSMitsuru IWASAKI 	 * If someone is sleeping on this request, wake them up.
3491e08d902aSMitsuru IWASAKI 	 */
3492e08d902aSMitsuru IWASAKI 	if (cr->cr_flags & CISS_REQ_SLEEP) {
3493e08d902aSMitsuru IWASAKI 	    cr->cr_flags &= ~CISS_REQ_SLEEP;
3494e08d902aSMitsuru IWASAKI 	    wakeup(cr);
3495e08d902aSMitsuru IWASAKI 	    continue;
3496e08d902aSMitsuru IWASAKI 	}
3497e08d902aSMitsuru IWASAKI     }
3498e08d902aSMitsuru IWASAKI }
3499e08d902aSMitsuru IWASAKI 
3500e08d902aSMitsuru IWASAKI /************************************************************************
35013a31b7ebSMike Smith  * Request a notification response from the adapter.
35023a31b7ebSMike Smith  *
35033a31b7ebSMike Smith  * If (cr) is NULL, this is the first request of the adapter, so
35043a31b7ebSMike Smith  * reset the adapter's message pointer and start with the oldest
35053a31b7ebSMike Smith  * message available.
35063a31b7ebSMike Smith  */
35073a31b7ebSMike Smith static void
35083a31b7ebSMike Smith ciss_notify_event(struct ciss_softc *sc)
35093a31b7ebSMike Smith {
35103a31b7ebSMike Smith     struct ciss_request		*cr;
35113a31b7ebSMike Smith     struct ciss_command		*cc;
35123a31b7ebSMike Smith     struct ciss_notify_cdb	*cnc;
35133a31b7ebSMike Smith     int				error;
35143a31b7ebSMike Smith 
35153a31b7ebSMike Smith     debug_called(1);
35163a31b7ebSMike Smith 
35173a31b7ebSMike Smith     cr = sc->ciss_periodic_notify;
35183a31b7ebSMike Smith 
35193a31b7ebSMike Smith     /* get a request if we don't already have one */
35203a31b7ebSMike Smith     if (cr == NULL) {
35213a31b7ebSMike Smith 	if ((error = ciss_get_request(sc, &cr)) != 0) {
35223a31b7ebSMike Smith 	    debug(0, "can't get notify event request");
35233a31b7ebSMike Smith 	    goto out;
35243a31b7ebSMike Smith 	}
35253a31b7ebSMike Smith 	sc->ciss_periodic_notify = cr;
35263a31b7ebSMike Smith 	cr->cr_complete = ciss_notify_complete;
35273a31b7ebSMike Smith 	debug(1, "acquired request %d", cr->cr_tag);
35283a31b7ebSMike Smith     }
35293a31b7ebSMike Smith 
35303a31b7ebSMike Smith     /*
35313a31b7ebSMike Smith      * Get a databuffer if we don't already have one, note that the
35323a31b7ebSMike Smith      * adapter command wants a larger buffer than the actual
35333a31b7ebSMike Smith      * structure.
35343a31b7ebSMike Smith      */
35353a31b7ebSMike Smith     if (cr->cr_data == NULL) {
35363a31b7ebSMike Smith 	if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
35373a31b7ebSMike Smith 	    debug(0, "can't get notify event request buffer");
35383a31b7ebSMike Smith 	    error = ENOMEM;
35393a31b7ebSMike Smith 	    goto out;
35403a31b7ebSMike Smith 	}
35413a31b7ebSMike Smith 	cr->cr_length = CISS_NOTIFY_DATA_SIZE;
35423a31b7ebSMike Smith     }
35433a31b7ebSMike Smith 
35443a31b7ebSMike Smith     /* re-setup the request's command (since we never release it) XXX overkill*/
35453a31b7ebSMike Smith     ciss_preen_command(cr);
35463a31b7ebSMike Smith 
35473a31b7ebSMike Smith     /* (re)build the notify event command */
35482fdaa90dSScott Long     cc = cr->cr_cc;
35493a31b7ebSMike Smith     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
35503a31b7ebSMike Smith     cc->header.address.physical.bus = 0;
35513a31b7ebSMike Smith     cc->header.address.physical.target = 0;
35523a31b7ebSMike Smith 
35533a31b7ebSMike Smith     cc->cdb.cdb_length = sizeof(*cnc);
35543a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
35553a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
35563a31b7ebSMike Smith     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
35573a31b7ebSMike Smith     cc->cdb.timeout = 0;	/* no timeout, we hope */
35583a31b7ebSMike Smith 
35593a31b7ebSMike Smith     cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
35603a31b7ebSMike Smith     bzero(cr->cr_data, CISS_NOTIFY_DATA_SIZE);
35613a31b7ebSMike Smith     cnc->opcode = CISS_OPCODE_READ;
35623a31b7ebSMike Smith     cnc->command = CISS_COMMAND_NOTIFY_ON_EVENT;
35633a31b7ebSMike Smith     cnc->timeout = 0;		/* no timeout, we hope */
35643a31b7ebSMike Smith     cnc->synchronous = 0;
35653a31b7ebSMike Smith     cnc->ordered = 0;
35663a31b7ebSMike Smith     cnc->seek_to_oldest = 0;
35672e80ca8aSPaul Saab     if ((sc->ciss_flags & CISS_FLAG_RUNNING) == 0)
35682e80ca8aSPaul Saab 	cnc->new_only = 1;
35692e80ca8aSPaul Saab     else
35703a31b7ebSMike Smith 	cnc->new_only = 0;
35713a31b7ebSMike Smith     cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
35723a31b7ebSMike Smith 
35733a31b7ebSMike Smith     /* submit the request */
35743a31b7ebSMike Smith     error = ciss_start(cr);
35753a31b7ebSMike Smith 
35763a31b7ebSMike Smith  out:
35773a31b7ebSMike Smith     if (error) {
35783a31b7ebSMike Smith 	if (cr != NULL) {
35793a31b7ebSMike Smith 	    if (cr->cr_data != NULL)
35803a31b7ebSMike Smith 		free(cr->cr_data, CISS_MALLOC_CLASS);
35813a31b7ebSMike Smith 	    ciss_release_request(cr);
35823a31b7ebSMike Smith 	}
35833a31b7ebSMike Smith 	sc->ciss_periodic_notify = NULL;
35843a31b7ebSMike Smith 	debug(0, "can't submit notify event request");
35853a31b7ebSMike Smith 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
35863a31b7ebSMike Smith     } else {
35873a31b7ebSMike Smith 	debug(1, "notify event submitted");
35883a31b7ebSMike Smith 	sc->ciss_flags |= CISS_FLAG_NOTIFY_OK;
35893a31b7ebSMike Smith     }
35903a31b7ebSMike Smith }
35913a31b7ebSMike Smith 
35923a31b7ebSMike Smith static void
35933a31b7ebSMike Smith ciss_notify_complete(struct ciss_request *cr)
35943a31b7ebSMike Smith {
35953a31b7ebSMike Smith     struct ciss_command	*cc;
35963a31b7ebSMike Smith     struct ciss_notify	*cn;
35973a31b7ebSMike Smith     struct ciss_softc	*sc;
35983a31b7ebSMike Smith     int			scsi_status;
35993a31b7ebSMike Smith     int			command_status;
36003a31b7ebSMike Smith     debug_called(1);
36013a31b7ebSMike Smith 
36022fdaa90dSScott Long     cc = cr->cr_cc;
36033a31b7ebSMike Smith     cn = (struct ciss_notify *)cr->cr_data;
36043a31b7ebSMike Smith     sc = cr->cr_sc;
36053a31b7ebSMike Smith 
36063a31b7ebSMike Smith     /*
36073a31b7ebSMike Smith      * Report request results, decode status.
36083a31b7ebSMike Smith      */
36093a31b7ebSMike Smith     ciss_report_request(cr, &command_status, &scsi_status);
36103a31b7ebSMike Smith 
36113a31b7ebSMike Smith     /*
36123a31b7ebSMike Smith      * Abort the chain on a fatal error.
36133a31b7ebSMike Smith      *
36143a31b7ebSMike Smith      * XXX which of these are actually errors?
36153a31b7ebSMike Smith      */
36163a31b7ebSMike Smith     if ((command_status != CISS_CMD_STATUS_SUCCESS) &&
36173a31b7ebSMike Smith 	(command_status != CISS_CMD_STATUS_TARGET_STATUS) &&
36183a31b7ebSMike Smith 	(command_status != CISS_CMD_STATUS_TIMEOUT)) {	/* XXX timeout? */
36193a31b7ebSMike Smith 	ciss_printf(sc, "fatal error in Notify Event request (%s)\n",
36203a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
36213a31b7ebSMike Smith 	ciss_release_request(cr);
36223a31b7ebSMike Smith 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
36233a31b7ebSMike Smith 	return;
36243a31b7ebSMike Smith     }
36253a31b7ebSMike Smith 
36263a31b7ebSMike Smith     /*
36273a31b7ebSMike Smith      * If the adapter gave us a text message, print it.
36283a31b7ebSMike Smith      */
36293a31b7ebSMike Smith     if (cn->message[0] != 0)
36303a31b7ebSMike Smith 	ciss_printf(sc, "*** %.80s\n", cn->message);
36313a31b7ebSMike Smith 
36323a31b7ebSMike Smith     debug(0, "notify event class %d subclass %d detail %d",
36333a31b7ebSMike Smith 		cn->class, cn->subclass, cn->detail);
36343a31b7ebSMike Smith 
36353a31b7ebSMike Smith     /*
36363a31b7ebSMike Smith      * If the response indicates that the notifier has been aborted,
36373a31b7ebSMike Smith      * release the notifier command.
36383a31b7ebSMike Smith      */
36393a31b7ebSMike Smith     if ((cn->class == CISS_NOTIFY_NOTIFIER) &&
36403a31b7ebSMike Smith 	(cn->subclass == CISS_NOTIFY_NOTIFIER_STATUS) &&
36413a31b7ebSMike Smith 	(cn->detail == 1)) {
36423a31b7ebSMike Smith 	debug(0, "notifier exiting");
36433a31b7ebSMike Smith 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
36443a31b7ebSMike Smith 	ciss_release_request(cr);
36453a31b7ebSMike Smith 	sc->ciss_periodic_notify = NULL;
36463a31b7ebSMike Smith 	wakeup(&sc->ciss_periodic_notify);
3647c6131460SPaul Saab     } else {
3648c6131460SPaul Saab 	/* Handle notify events in a kernel thread */
3649c6131460SPaul Saab 	ciss_enqueue_notify(cr);
3650c6131460SPaul Saab 	sc->ciss_periodic_notify = NULL;
3651c6131460SPaul Saab 	wakeup(&sc->ciss_periodic_notify);
3652c6131460SPaul Saab 	wakeup(&sc->ciss_notify);
36533a31b7ebSMike Smith     }
36543a31b7ebSMike Smith     /*
36553a31b7ebSMike Smith      * Send a new notify event command, if we're not aborting.
36563a31b7ebSMike Smith      */
36573a31b7ebSMike Smith     if (!(sc->ciss_flags & CISS_FLAG_ABORTING)) {
36583a31b7ebSMike Smith 	ciss_notify_event(sc);
36593a31b7ebSMike Smith     }
36603a31b7ebSMike Smith }
36613a31b7ebSMike Smith 
36623a31b7ebSMike Smith /************************************************************************
36633a31b7ebSMike Smith  * Abort the Notify Event chain.
36643a31b7ebSMike Smith  *
36653a31b7ebSMike Smith  * Note that we can't just abort the command in progress; we have to
36663a31b7ebSMike Smith  * explicitly issue an Abort Notify Event command in order for the
36673a31b7ebSMike Smith  * adapter to clean up correctly.
36683a31b7ebSMike Smith  *
36693a31b7ebSMike Smith  * If we are called with CISS_FLAG_ABORTING set in the adapter softc,
36703a31b7ebSMike Smith  * the chain will not restart itself.
36713a31b7ebSMike Smith  */
36723a31b7ebSMike Smith static int
36733a31b7ebSMike Smith ciss_notify_abort(struct ciss_softc *sc)
36743a31b7ebSMike Smith {
36753a31b7ebSMike Smith     struct ciss_request		*cr;
36763a31b7ebSMike Smith     struct ciss_command		*cc;
36773a31b7ebSMike Smith     struct ciss_notify_cdb	*cnc;
367822657ce1SScott Long     int				error, command_status, scsi_status;
36793a31b7ebSMike Smith 
36803a31b7ebSMike Smith     debug_called(1);
36813a31b7ebSMike Smith 
36823a31b7ebSMike Smith     cr = NULL;
36833a31b7ebSMike Smith     error = 0;
36843a31b7ebSMike Smith 
36853a31b7ebSMike Smith     /* verify that there's an outstanding command */
36863a31b7ebSMike Smith     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
36873a31b7ebSMike Smith 	goto out;
36883a31b7ebSMike Smith 
36893a31b7ebSMike Smith     /* get a command to issue the abort with */
36903a31b7ebSMike Smith     if ((error = ciss_get_request(sc, &cr)))
36913a31b7ebSMike Smith 	goto out;
36923a31b7ebSMike Smith 
36933a31b7ebSMike Smith     /* get a buffer for the result */
36943a31b7ebSMike Smith     if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
36953a31b7ebSMike Smith 	debug(0, "can't get notify event request buffer");
36963a31b7ebSMike Smith 	error = ENOMEM;
36973a31b7ebSMike Smith 	goto out;
36983a31b7ebSMike Smith     }
36993a31b7ebSMike Smith     cr->cr_length = CISS_NOTIFY_DATA_SIZE;
37003a31b7ebSMike Smith 
37013a31b7ebSMike Smith     /* build the CDB */
37022fdaa90dSScott Long     cc = cr->cr_cc;
37033a31b7ebSMike Smith     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
37043a31b7ebSMike Smith     cc->header.address.physical.bus = 0;
37053a31b7ebSMike Smith     cc->header.address.physical.target = 0;
37063a31b7ebSMike Smith     cc->cdb.cdb_length = sizeof(*cnc);
37073a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
37083a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
37093a31b7ebSMike Smith     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
37103a31b7ebSMike Smith     cc->cdb.timeout = 0;	/* no timeout, we hope */
37113a31b7ebSMike Smith 
37123a31b7ebSMike Smith     cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
37133a31b7ebSMike Smith     bzero(cnc, sizeof(*cnc));
37143a31b7ebSMike Smith     cnc->opcode = CISS_OPCODE_WRITE;
37153a31b7ebSMike Smith     cnc->command = CISS_COMMAND_ABORT_NOTIFY;
37163a31b7ebSMike Smith     cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
37173a31b7ebSMike Smith 
37183a31b7ebSMike Smith     ciss_print_request(cr);
37193a31b7ebSMike Smith 
37203a31b7ebSMike Smith     /*
37213a31b7ebSMike Smith      * Submit the request and wait for it to complete.
37223a31b7ebSMike Smith      */
37233a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
37243a31b7ebSMike Smith 	ciss_printf(sc, "Abort Notify Event command failed (%d)\n", error);
37253a31b7ebSMike Smith 	goto out;
37263a31b7ebSMike Smith     }
37273a31b7ebSMike Smith 
37283a31b7ebSMike Smith     /*
37293a31b7ebSMike Smith      * Check response.
37303a31b7ebSMike Smith      */
37313a31b7ebSMike Smith     ciss_report_request(cr, &command_status, &scsi_status);
37323a31b7ebSMike Smith     switch(command_status) {
37333a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:
37343a31b7ebSMike Smith 	break;
37353a31b7ebSMike Smith     case CISS_CMD_STATUS_INVALID_COMMAND:
37363a31b7ebSMike Smith 	/*
37373a31b7ebSMike Smith 	 * Some older adapters don't support the CISS version of this
37383a31b7ebSMike Smith 	 * command.  Fall back to using the BMIC version.
37393a31b7ebSMike Smith 	 */
37403a31b7ebSMike Smith 	error = ciss_notify_abort_bmic(sc);
37413a31b7ebSMike Smith 	if (error != 0)
37423a31b7ebSMike Smith 	    goto out;
37433a31b7ebSMike Smith 	break;
37443a31b7ebSMike Smith 
37453a31b7ebSMike Smith     case CISS_CMD_STATUS_TARGET_STATUS:
37463a31b7ebSMike Smith 	/*
37473a31b7ebSMike Smith 	 * This can happen if the adapter thinks there wasn't an outstanding
37483a31b7ebSMike Smith 	 * Notify Event command but we did.  We clean up here.
37493a31b7ebSMike Smith 	 */
37503a31b7ebSMike Smith 	if (scsi_status == CISS_SCSI_STATUS_CHECK_CONDITION) {
37513a31b7ebSMike Smith 	    if (sc->ciss_periodic_notify != NULL)
37523a31b7ebSMike Smith 		ciss_release_request(sc->ciss_periodic_notify);
37533a31b7ebSMike Smith 	    error = 0;
37543a31b7ebSMike Smith 	    goto out;
37553a31b7ebSMike Smith 	}
37563a31b7ebSMike Smith 	/* FALLTHROUGH */
37573a31b7ebSMike Smith 
37583a31b7ebSMike Smith     default:
37593a31b7ebSMike Smith 	ciss_printf(sc, "Abort Notify Event command failed (%s)\n",
37603a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
37613a31b7ebSMike Smith 	error = EIO;
37623a31b7ebSMike Smith 	goto out;
37633a31b7ebSMike Smith     }
37643a31b7ebSMike Smith 
37653a31b7ebSMike Smith     /*
37663a31b7ebSMike Smith      * Sleep waiting for the notifier command to complete.  Note
37673a31b7ebSMike Smith      * that if it doesn't, we may end up in a bad situation, since
37683a31b7ebSMike Smith      * the adapter may deliver it later.  Also note that the adapter
37693a31b7ebSMike Smith      * requires the Notify Event command to be cancelled in order to
37703a31b7ebSMike Smith      * maintain internal bookkeeping.
37713a31b7ebSMike Smith      */
37723a31b7ebSMike Smith     while (sc->ciss_periodic_notify != NULL) {
3773975b7318SScott Long 	error = msleep(&sc->ciss_periodic_notify, &sc->ciss_mtx, PRIBIO, "cissNEA", hz * 5);
37743a31b7ebSMike Smith 	if (error == EWOULDBLOCK) {
37753a31b7ebSMike Smith 	    ciss_printf(sc, "Notify Event command failed to abort, adapter may wedge.\n");
37763a31b7ebSMike Smith 	    break;
37773a31b7ebSMike Smith 	}
37783a31b7ebSMike Smith     }
37793a31b7ebSMike Smith 
37803a31b7ebSMike Smith  out:
37813a31b7ebSMike Smith     /* release the cancel request */
37823a31b7ebSMike Smith     if (cr != NULL) {
37833a31b7ebSMike Smith 	if (cr->cr_data != NULL)
37843a31b7ebSMike Smith 	    free(cr->cr_data, CISS_MALLOC_CLASS);
37853a31b7ebSMike Smith 	ciss_release_request(cr);
37863a31b7ebSMike Smith     }
37873a31b7ebSMike Smith     if (error == 0)
37883a31b7ebSMike Smith 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
37893a31b7ebSMike Smith     return(error);
37903a31b7ebSMike Smith }
37913a31b7ebSMike Smith 
37923a31b7ebSMike Smith /************************************************************************
37933a31b7ebSMike Smith  * Abort the Notify Event chain using a BMIC command.
37943a31b7ebSMike Smith  */
37953a31b7ebSMike Smith static int
37963a31b7ebSMike Smith ciss_notify_abort_bmic(struct ciss_softc *sc)
37973a31b7ebSMike Smith {
37983a31b7ebSMike Smith     struct ciss_request			*cr;
37993a31b7ebSMike Smith     int					error, command_status;
38003a31b7ebSMike Smith 
38013a31b7ebSMike Smith     debug_called(1);
38023a31b7ebSMike Smith 
38033a31b7ebSMike Smith     cr = NULL;
38043a31b7ebSMike Smith     error = 0;
38053a31b7ebSMike Smith 
38063a31b7ebSMike Smith     /* verify that there's an outstanding command */
38073a31b7ebSMike Smith     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
38083a31b7ebSMike Smith 	goto out;
38093a31b7ebSMike Smith 
38103a31b7ebSMike Smith     /*
38113a31b7ebSMike Smith      * Build a BMIC command to cancel the Notify on Event command.
38123a31b7ebSMike Smith      *
38133a31b7ebSMike Smith      * Note that we are sending a CISS opcode here.  Odd.
38143a31b7ebSMike Smith      */
38153a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_COMMAND_ABORT_NOTIFY,
38163a31b7ebSMike Smith 				       NULL, 0)) != 0)
38173a31b7ebSMike Smith 	goto out;
38183a31b7ebSMike Smith 
38193a31b7ebSMike Smith     /*
38203a31b7ebSMike Smith      * Submit the request and wait for it to complete.
38213a31b7ebSMike Smith      */
38223a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
38233a31b7ebSMike Smith 	ciss_printf(sc, "error sending BMIC Cancel Notify on Event command (%d)\n", error);
38243a31b7ebSMike Smith 	goto out;
38253a31b7ebSMike Smith     }
38263a31b7ebSMike Smith 
38273a31b7ebSMike Smith     /*
38283a31b7ebSMike Smith      * Check response.
38293a31b7ebSMike Smith      */
38303a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
38313a31b7ebSMike Smith     switch(command_status) {
38323a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:
38333a31b7ebSMike Smith 	break;
38343a31b7ebSMike Smith     default:
38353a31b7ebSMike Smith 	ciss_printf(sc, "error cancelling Notify on Event (%s)\n",
38363a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
38373a31b7ebSMike Smith 	error = EIO;
38383a31b7ebSMike Smith 	goto out;
38393a31b7ebSMike Smith     }
38403a31b7ebSMike Smith 
38413a31b7ebSMike Smith out:
38423a31b7ebSMike Smith     if (cr != NULL)
38433a31b7ebSMike Smith 	ciss_release_request(cr);
38443a31b7ebSMike Smith     return(error);
38453a31b7ebSMike Smith }
38463a31b7ebSMike Smith 
38473a31b7ebSMike Smith /************************************************************************
3848c6131460SPaul Saab  * Handle rescanning all the logical volumes when a notify event
3849c6131460SPaul Saab  * causes the drives to come online or offline.
3850c6131460SPaul Saab  */
3851c6131460SPaul Saab static void
3852c6131460SPaul Saab ciss_notify_rescan_logical(struct ciss_softc *sc)
3853c6131460SPaul Saab {
3854c6131460SPaul Saab     struct ciss_lun_report      *cll;
3855c6131460SPaul Saab     struct ciss_ldrive		*ld;
3856c6131460SPaul Saab     int                         i, j, ndrives;
3857c6131460SPaul Saab 
3858c6131460SPaul Saab     /*
3859c6131460SPaul Saab      * We must rescan all logical volumes to get the right logical
3860c6131460SPaul Saab      * drive address.
3861c6131460SPaul Saab      */
3862c6131460SPaul Saab     cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
3863c6131460SPaul Saab                            CISS_MAX_LOGICAL);
3864c6131460SPaul Saab     if (cll == NULL)
3865c6131460SPaul Saab         return;
3866c6131460SPaul Saab 
3867c6131460SPaul Saab     ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
3868c6131460SPaul Saab 
3869c6131460SPaul Saab     /*
3870c6131460SPaul Saab      * Delete any of the drives which were destroyed by the
3871c6131460SPaul Saab      * firmware.
3872c6131460SPaul Saab      */
38731fe6c4eeSScott Long     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
3874c6131460SPaul Saab 	for (j = 0; j < CISS_MAX_LOGICAL; j++) {
3875c6131460SPaul Saab 	    ld = &sc->ciss_logical[i][j];
3876c6131460SPaul Saab 
3877c6131460SPaul Saab 	    if (ld->cl_update == 0)
3878c6131460SPaul Saab 		continue;
3879c6131460SPaul Saab 
3880c6131460SPaul Saab 	    if (ld->cl_status != CISS_LD_ONLINE) {
3881c6131460SPaul Saab 		ciss_cam_rescan_target(sc, i, j);
3882c6131460SPaul Saab 		ld->cl_update = 0;
3883c6131460SPaul Saab 		if (ld->cl_ldrive)
3884c6131460SPaul Saab 		    free(ld->cl_ldrive, CISS_MALLOC_CLASS);
3885c6131460SPaul Saab 		if (ld->cl_lstatus)
3886c6131460SPaul Saab 		    free(ld->cl_lstatus, CISS_MALLOC_CLASS);
3887c6131460SPaul Saab 
3888c6131460SPaul Saab 		ld->cl_ldrive = NULL;
3889c6131460SPaul Saab 		ld->cl_lstatus = NULL;
3890c6131460SPaul Saab 	    }
3891c6131460SPaul Saab 	}
3892c6131460SPaul Saab     }
3893c6131460SPaul Saab 
3894c6131460SPaul Saab     /*
3895c6131460SPaul Saab      * Scan for new drives.
3896c6131460SPaul Saab      */
3897c6131460SPaul Saab     for (i = 0; i < ndrives; i++) {
3898c6131460SPaul Saab 	int	bus, target;
3899c6131460SPaul Saab 
3900c6131460SPaul Saab 	bus 	= CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
3901c6131460SPaul Saab 	target	= CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
3902c6131460SPaul Saab 	ld	= &sc->ciss_logical[bus][target];
3903c6131460SPaul Saab 
3904c6131460SPaul Saab 	if (ld->cl_update == 0)
3905c6131460SPaul Saab 		continue;
3906c6131460SPaul Saab 
39075cf5a430SPaul Saab 	ld->cl_update		= 0;
3908c6131460SPaul Saab 	ld->cl_address		= cll->lun[i];
3909c6131460SPaul Saab 	ld->cl_controller	= &sc->ciss_controllers[bus];
3910c6131460SPaul Saab 	if (ciss_identify_logical(sc, ld) == 0) {
3911c6131460SPaul Saab 	    ciss_cam_rescan_target(sc, bus, target);
3912c6131460SPaul Saab 	}
3913c6131460SPaul Saab     }
3914c6131460SPaul Saab     free(cll, CISS_MALLOC_CLASS);
3915c6131460SPaul Saab }
3916c6131460SPaul Saab 
3917c6131460SPaul Saab /************************************************************************
39183a31b7ebSMike Smith  * Handle a notify event relating to the status of a logical drive.
39193a31b7ebSMike Smith  *
39203a31b7ebSMike Smith  * XXX need to be able to defer some of these to properly handle
39213a31b7ebSMike Smith  *     calling the "ID Physical drive" command, unless the 'extended'
39223a31b7ebSMike Smith  *     drive IDs are always in BIG_MAP format.
39233a31b7ebSMike Smith  */
39243a31b7ebSMike Smith static void
39253a31b7ebSMike Smith ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn)
39263a31b7ebSMike Smith {
39273a31b7ebSMike Smith     struct ciss_ldrive	*ld;
3928c6131460SPaul Saab     int			ostatus, bus, target;
39293a31b7ebSMike Smith 
39303a31b7ebSMike Smith     debug_called(2);
39313a31b7ebSMike Smith 
3932c6131460SPaul Saab     bus		= cn->device.physical.bus;
3933c6131460SPaul Saab     target	= cn->data.logical_status.logical_drive;
3934c6131460SPaul Saab     ld		= &sc->ciss_logical[bus][target];
39353a31b7ebSMike Smith 
39363a31b7ebSMike Smith     switch (cn->subclass) {
39373a31b7ebSMike Smith     case CISS_NOTIFY_LOGICAL_STATUS:
39383a31b7ebSMike Smith 	switch (cn->detail) {
39393a31b7ebSMike Smith 	case 0:
3940c6131460SPaul Saab 	    ciss_name_device(sc, bus, target);
39413a31b7ebSMike Smith 	    ciss_printf(sc, "logical drive %d (%s) changed status %s->%s, spare status 0x%b\n",
39423a31b7ebSMike Smith 			cn->data.logical_status.logical_drive, ld->cl_name,
39433a31b7ebSMike Smith 			ciss_name_ldrive_status(cn->data.logical_status.previous_state),
39443a31b7ebSMike Smith 			ciss_name_ldrive_status(cn->data.logical_status.new_state),
39453a31b7ebSMike Smith 			cn->data.logical_status.spare_state,
39463a31b7ebSMike Smith 			"\20\1configured\2rebuilding\3failed\4in use\5available\n");
39473a31b7ebSMike Smith 
39483a31b7ebSMike Smith 	    /*
39493a31b7ebSMike Smith 	     * Update our idea of the drive's status.
39503a31b7ebSMike Smith 	     */
39513a31b7ebSMike Smith 	    ostatus = ciss_decode_ldrive_status(cn->data.logical_status.previous_state);
39523a31b7ebSMike Smith 	    ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
3953d4aa427fSPaul Saab 	    if (ld->cl_lstatus != NULL)
39543a31b7ebSMike Smith 		ld->cl_lstatus->status = cn->data.logical_status.new_state;
39553a31b7ebSMike Smith 
3956d4aa427fSPaul Saab 	    /*
3957d4aa427fSPaul Saab 	     * Have CAM rescan the drive if its status has changed.
3958d4aa427fSPaul Saab 	     */
3959c6131460SPaul Saab 	    if (ostatus != ld->cl_status) {
3960c6131460SPaul Saab 		ld->cl_update = 1;
3961c6131460SPaul Saab 		ciss_notify_rescan_logical(sc);
3962c6131460SPaul Saab 	    }
3963d4aa427fSPaul Saab 
39643a31b7ebSMike Smith 	    break;
39653a31b7ebSMike Smith 
39663a31b7ebSMike Smith 	case 1:	/* logical drive has recognised new media, needs Accept Media Exchange */
3967c6131460SPaul Saab 	    ciss_name_device(sc, bus, target);
39683a31b7ebSMike Smith 	    ciss_printf(sc, "logical drive %d (%s) media exchanged, ready to go online\n",
39693a31b7ebSMike Smith 			cn->data.logical_status.logical_drive, ld->cl_name);
397088c78a38SPaul Saab 	    ciss_accept_media(sc, ld);
3971139233cbSPaul Saab 
3972139233cbSPaul Saab 	    ld->cl_update = 1;
3973139233cbSPaul Saab 	    ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
3974139233cbSPaul Saab 	    ciss_notify_rescan_logical(sc);
39753a31b7ebSMike Smith 	    break;
39763a31b7ebSMike Smith 
39773a31b7ebSMike Smith 	case 2:
39783a31b7ebSMike Smith 	case 3:
39793a31b7ebSMike Smith 	    ciss_printf(sc, "rebuild of logical drive %d (%s) failed due to %s error\n",
39803a31b7ebSMike Smith 			cn->data.rebuild_aborted.logical_drive,
3981c6131460SPaul Saab 			ld->cl_name,
39823a31b7ebSMike Smith 			(cn->detail == 2) ? "read" : "write");
39833a31b7ebSMike Smith 	    break;
39843a31b7ebSMike Smith 	}
39853a31b7ebSMike Smith 	break;
39863a31b7ebSMike Smith 
39873a31b7ebSMike Smith     case CISS_NOTIFY_LOGICAL_ERROR:
39883a31b7ebSMike Smith 	if (cn->detail == 0) {
39893a31b7ebSMike Smith 	    ciss_printf(sc, "FATAL I/O ERROR on logical drive %d (%s), SCSI port %d ID %d\n",
39903a31b7ebSMike Smith 			cn->data.io_error.logical_drive,
3991c6131460SPaul Saab 			ld->cl_name,
39923a31b7ebSMike Smith 			cn->data.io_error.failure_bus,
39933a31b7ebSMike Smith 			cn->data.io_error.failure_drive);
39943a31b7ebSMike Smith 	    /* XXX should we take the drive down at this point, or will we be told? */
39953a31b7ebSMike Smith 	}
39963a31b7ebSMike Smith 	break;
39973a31b7ebSMike Smith 
39983a31b7ebSMike Smith     case CISS_NOTIFY_LOGICAL_SURFACE:
39993a31b7ebSMike Smith 	if (cn->detail == 0)
40003a31b7ebSMike Smith 	    ciss_printf(sc, "logical drive %d (%s) completed consistency initialisation\n",
40013a31b7ebSMike Smith 			cn->data.consistency_completed.logical_drive,
4002c6131460SPaul Saab 			ld->cl_name);
40033a31b7ebSMike Smith 	break;
40043a31b7ebSMike Smith     }
40053a31b7ebSMike Smith }
40063a31b7ebSMike Smith 
40073a31b7ebSMike Smith /************************************************************************
40083a31b7ebSMike Smith  * Handle a notify event relating to the status of a physical drive.
40093a31b7ebSMike Smith  */
40103a31b7ebSMike Smith static void
40113a31b7ebSMike Smith ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn)
40123a31b7ebSMike Smith {
4013c6131460SPaul Saab }
40143a31b7ebSMike Smith 
4015c6131460SPaul Saab /************************************************************************
40161fe6c4eeSScott Long  * Handle a notify event relating to the status of a physical drive.
40171fe6c4eeSScott Long  */
40181fe6c4eeSScott Long static void
40191fe6c4eeSScott Long ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn)
40201fe6c4eeSScott Long {
4021c1885ab8SPaul Saab     struct ciss_lun_report *cll = NULL;
40221fe6c4eeSScott Long     int bus, target;
40231fe6c4eeSScott Long 
40241fe6c4eeSScott Long     switch (cn->subclass) {
40251fe6c4eeSScott Long     case CISS_NOTIFY_HOTPLUG_PHYSICAL:
40261fe6c4eeSScott Long     case CISS_NOTIFY_HOTPLUG_NONDISK:
40271fe6c4eeSScott Long 	bus = CISS_BIG_MAP_BUS(sc, cn->data.drive.big_physical_drive_number);
40281fe6c4eeSScott Long 	target =
40291fe6c4eeSScott Long 	    CISS_BIG_MAP_TARGET(sc, cn->data.drive.big_physical_drive_number);
40301fe6c4eeSScott Long 
40311fe6c4eeSScott Long 	if (cn->detail == 0) {
40321fe6c4eeSScott Long 	    /*
40331fe6c4eeSScott Long 	     * Mark the device offline so that it'll start producing selection
40341fe6c4eeSScott Long 	     * timeouts to the upper layer.
40351fe6c4eeSScott Long 	     */
40365f265711SScott Long 	    if ((bus >= 0) && (target >= 0))
40371fe6c4eeSScott Long 		sc->ciss_physical[bus][target].cp_online = 0;
40381fe6c4eeSScott Long 	} else {
40391fe6c4eeSScott Long 	    /*
40401fe6c4eeSScott Long 	     * Rescan the physical lun list for new items
40411fe6c4eeSScott Long 	     */
40421fe6c4eeSScott Long 	    cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
40431fe6c4eeSScott Long 				   CISS_MAX_PHYSICAL);
40441fe6c4eeSScott Long 	    if (cll == NULL) {
40451fe6c4eeSScott Long 		ciss_printf(sc, "Warning, cannot get physical lun list\n");
40461fe6c4eeSScott Long 		break;
40471fe6c4eeSScott Long 	    }
40481fe6c4eeSScott Long 	    ciss_filter_physical(sc, cll);
40491fe6c4eeSScott Long 	}
40501fe6c4eeSScott Long 	break;
40511fe6c4eeSScott Long 
40521fe6c4eeSScott Long     default:
40531fe6c4eeSScott Long 	ciss_printf(sc, "Unknown hotplug event %d\n", cn->subclass);
40541fe6c4eeSScott Long 	return;
40551fe6c4eeSScott Long     }
4056c1885ab8SPaul Saab 
4057c1885ab8SPaul Saab     if (cll != NULL)
4058c1885ab8SPaul Saab 	free(cll, CISS_MALLOC_CLASS);
40591fe6c4eeSScott Long }
40601fe6c4eeSScott Long 
40611fe6c4eeSScott Long /************************************************************************
4062c6131460SPaul Saab  * Handle deferred processing of notify events.  Notify events may need
4063c6131460SPaul Saab  * sleep which is unsafe during an interrupt.
4064c6131460SPaul Saab  */
4065c6131460SPaul Saab static void
4066c6131460SPaul Saab ciss_notify_thread(void *arg)
4067c6131460SPaul Saab {
4068c6131460SPaul Saab     struct ciss_softc		*sc;
4069c6131460SPaul Saab     struct ciss_request		*cr;
4070c6131460SPaul Saab     struct ciss_notify		*cn;
4071c6131460SPaul Saab 
4072c6131460SPaul Saab     sc = (struct ciss_softc *)arg;
4073975b7318SScott Long #if __FreeBSD_version >= 500000
4074975b7318SScott Long     mtx_lock(&sc->ciss_mtx);
4075975b7318SScott Long #endif
4076c6131460SPaul Saab 
4077c6131460SPaul Saab     for (;;) {
407822657ce1SScott Long 	if (STAILQ_EMPTY(&sc->ciss_notify) != 0 &&
4079c6131460SPaul Saab 	    (sc->ciss_flags & CISS_FLAG_THREAD_SHUT) == 0) {
4080975b7318SScott Long 	    msleep(&sc->ciss_notify, &sc->ciss_mtx, PUSER, "idle", 0);
4081c6131460SPaul Saab 	}
4082c6131460SPaul Saab 
4083c6131460SPaul Saab 	if (sc->ciss_flags & CISS_FLAG_THREAD_SHUT)
4084c6131460SPaul Saab 	    break;
4085c6131460SPaul Saab 
4086c6131460SPaul Saab 	cr = ciss_dequeue_notify(sc);
4087c6131460SPaul Saab 
4088c6131460SPaul Saab 	if (cr == NULL)
4089c6131460SPaul Saab 		panic("cr null");
4090c6131460SPaul Saab 	cn = (struct ciss_notify *)cr->cr_data;
4091c6131460SPaul Saab 
4092c6131460SPaul Saab 	switch (cn->class) {
40931fe6c4eeSScott Long 	case CISS_NOTIFY_HOTPLUG:
40941fe6c4eeSScott Long 	    ciss_notify_hotplug(sc, cn);
40951fe6c4eeSScott Long 	    break;
4096c6131460SPaul Saab 	case CISS_NOTIFY_LOGICAL:
4097c6131460SPaul Saab 	    ciss_notify_logical(sc, cn);
4098c6131460SPaul Saab 	    break;
4099c6131460SPaul Saab 	case CISS_NOTIFY_PHYSICAL:
4100c6131460SPaul Saab 	    ciss_notify_physical(sc, cn);
4101c6131460SPaul Saab 	    break;
4102c6131460SPaul Saab 	}
4103c6131460SPaul Saab 
4104c6131460SPaul Saab 	ciss_release_request(cr);
4105c6131460SPaul Saab 
4106c6131460SPaul Saab     }
4107c6131460SPaul Saab     sc->ciss_notify_thread = NULL;
4108c6131460SPaul Saab     wakeup(&sc->ciss_notify_thread);
4109c6131460SPaul Saab 
4110c6131460SPaul Saab #if __FreeBSD_version >= 500000
4111975b7318SScott Long     mtx_unlock(&sc->ciss_mtx);
4112c6131460SPaul Saab #endif
41133745c395SJulian Elischer     kproc_exit(0);
4114c6131460SPaul Saab }
4115c6131460SPaul Saab 
4116c6131460SPaul Saab /************************************************************************
4117c6131460SPaul Saab  * Start the notification kernel thread.
4118c6131460SPaul Saab  */
4119c6131460SPaul Saab static void
4120c6131460SPaul Saab ciss_spawn_notify_thread(struct ciss_softc *sc)
4121c6131460SPaul Saab {
4122c6131460SPaul Saab 
412378d03361SPaul Saab #if __FreeBSD_version > 500005
41243745c395SJulian Elischer     if (kproc_create((void(*)(void *))ciss_notify_thread, sc,
4125c6131460SPaul Saab 		       &sc->ciss_notify_thread, 0, 0, "ciss_notify%d",
4126c6131460SPaul Saab 		       device_get_unit(sc->ciss_dev)))
412778d03361SPaul Saab #else
41283745c395SJulian Elischer     if (kproc_create((void(*)(void *))ciss_notify_thread, sc,
412978d03361SPaul Saab 		       &sc->ciss_notify_thread, "ciss_notify%d",
413078d03361SPaul Saab 		       device_get_unit(sc->ciss_dev)))
413178d03361SPaul Saab #endif
4132c6131460SPaul Saab 	panic("Could not create notify thread\n");
4133c6131460SPaul Saab }
4134c6131460SPaul Saab 
4135c6131460SPaul Saab /************************************************************************
4136c6131460SPaul Saab  * Kill the notification kernel thread.
4137c6131460SPaul Saab  */
4138c6131460SPaul Saab static void
4139c6131460SPaul Saab ciss_kill_notify_thread(struct ciss_softc *sc)
4140c6131460SPaul Saab {
4141c6131460SPaul Saab 
4142c6131460SPaul Saab     if (sc->ciss_notify_thread == NULL)
4143c6131460SPaul Saab 	return;
4144c6131460SPaul Saab 
4145c6131460SPaul Saab     sc->ciss_flags |= CISS_FLAG_THREAD_SHUT;
4146c6131460SPaul Saab     wakeup(&sc->ciss_notify);
4147975b7318SScott Long     msleep(&sc->ciss_notify_thread, &sc->ciss_mtx, PUSER, "thtrm", 0);
41483a31b7ebSMike Smith }
41493a31b7ebSMike Smith 
41503a31b7ebSMike Smith /************************************************************************
41513a31b7ebSMike Smith  * Print a request.
41523a31b7ebSMike Smith  */
41533a31b7ebSMike Smith static void
41543a31b7ebSMike Smith ciss_print_request(struct ciss_request *cr)
41553a31b7ebSMike Smith {
41563a31b7ebSMike Smith     struct ciss_softc	*sc;
41573a31b7ebSMike Smith     struct ciss_command	*cc;
41583a31b7ebSMike Smith     int			i;
41593a31b7ebSMike Smith 
41603a31b7ebSMike Smith     sc = cr->cr_sc;
41612fdaa90dSScott Long     cc = cr->cr_cc;
41623a31b7ebSMike Smith 
41633a31b7ebSMike Smith     ciss_printf(sc, "REQUEST @ %p\n", cr);
41643a31b7ebSMike Smith     ciss_printf(sc, "  data %p/%d  tag %d  flags %b\n",
41653a31b7ebSMike Smith 	      cr->cr_data, cr->cr_length, cr->cr_tag, cr->cr_flags,
41663a31b7ebSMike Smith 	      "\20\1mapped\2sleep\3poll\4dataout\5datain\n");
41673a31b7ebSMike Smith     ciss_printf(sc, "  sg list/total %d/%d  host tag 0x%x\n",
41683a31b7ebSMike Smith 		cc->header.sg_in_list, cc->header.sg_total, cc->header.host_tag);
41693a31b7ebSMike Smith     switch(cc->header.address.mode.mode) {
41703a31b7ebSMike Smith     case CISS_HDR_ADDRESS_MODE_PERIPHERAL:
41713a31b7ebSMike Smith     case CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL:
41723a31b7ebSMike Smith 	ciss_printf(sc, "  physical bus %d target %d\n",
41733a31b7ebSMike Smith 		    cc->header.address.physical.bus, cc->header.address.physical.target);
41743a31b7ebSMike Smith 	break;
41753a31b7ebSMike Smith     case CISS_HDR_ADDRESS_MODE_LOGICAL:
41763a31b7ebSMike Smith 	ciss_printf(sc, "  logical unit %d\n", cc->header.address.logical.lun);
41773a31b7ebSMike Smith 	break;
41783a31b7ebSMike Smith     }
41793a31b7ebSMike Smith     ciss_printf(sc, "  %s cdb length %d type %s attribute %s\n",
41803a31b7ebSMike Smith 		(cc->cdb.direction == CISS_CDB_DIRECTION_NONE) ? "no-I/O" :
41813a31b7ebSMike Smith 		(cc->cdb.direction == CISS_CDB_DIRECTION_READ) ? "READ" :
41823a31b7ebSMike Smith 		(cc->cdb.direction == CISS_CDB_DIRECTION_WRITE) ? "WRITE" : "??",
41833a31b7ebSMike Smith 		cc->cdb.cdb_length,
41843a31b7ebSMike Smith 		(cc->cdb.type == CISS_CDB_TYPE_COMMAND) ? "command" :
41853a31b7ebSMike Smith 		(cc->cdb.type == CISS_CDB_TYPE_MESSAGE) ? "message" : "??",
41863a31b7ebSMike Smith 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_UNTAGGED) ? "untagged" :
41873a31b7ebSMike Smith 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_SIMPLE) ? "simple" :
41883a31b7ebSMike Smith 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_HEAD_OF_QUEUE) ? "head-of-queue" :
41893a31b7ebSMike Smith 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_ORDERED) ? "ordered" :
41903a31b7ebSMike Smith 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_AUTO_CONTINGENT) ? "auto-contingent" : "??");
41913a31b7ebSMike Smith     ciss_printf(sc, "  %*D\n", cc->cdb.cdb_length, &cc->cdb.cdb[0], " ");
41923a31b7ebSMike Smith 
41933a31b7ebSMike Smith     if (cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) {
41943a31b7ebSMike Smith 	/* XXX print error info */
41953a31b7ebSMike Smith     } else {
41963a31b7ebSMike Smith 	/* since we don't use chained s/g, don't support it here */
41973a31b7ebSMike Smith 	for (i = 0; i < cc->header.sg_in_list; i++) {
41983a31b7ebSMike Smith 	    if ((i % 4) == 0)
41993a31b7ebSMike Smith 		ciss_printf(sc, "   ");
42003a31b7ebSMike Smith 	    printf("0x%08x/%d ", (u_int32_t)cc->sg[i].address, cc->sg[i].length);
42013a31b7ebSMike Smith 	    if ((((i + 1) % 4) == 0) || (i == (cc->header.sg_in_list - 1)))
42023a31b7ebSMike Smith 		printf("\n");
42033a31b7ebSMike Smith 	}
42043a31b7ebSMike Smith     }
42053a31b7ebSMike Smith }
42063a31b7ebSMike Smith 
42073a31b7ebSMike Smith /************************************************************************
42083a31b7ebSMike Smith  * Print information about the status of a logical drive.
42093a31b7ebSMike Smith  */
42103a31b7ebSMike Smith static void
42113a31b7ebSMike Smith ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld)
42123a31b7ebSMike Smith {
42133a31b7ebSMike Smith     int		bus, target, i;
42143a31b7ebSMike Smith 
4215d4aa427fSPaul Saab     if (ld->cl_lstatus == NULL) {
4216d4aa427fSPaul Saab 	printf("does not exist\n");
4217d4aa427fSPaul Saab 	return;
4218d4aa427fSPaul Saab     }
4219d4aa427fSPaul Saab 
42203a31b7ebSMike Smith     /* print drive status */
42213a31b7ebSMike Smith     switch(ld->cl_lstatus->status) {
42223a31b7ebSMike Smith     case CISS_LSTATUS_OK:
42233a31b7ebSMike Smith 	printf("online\n");
42243a31b7ebSMike Smith 	break;
42253a31b7ebSMike Smith     case CISS_LSTATUS_INTERIM_RECOVERY:
42263a31b7ebSMike Smith 	printf("in interim recovery mode\n");
42273a31b7ebSMike Smith 	break;
42283a31b7ebSMike Smith     case CISS_LSTATUS_READY_RECOVERY:
42293a31b7ebSMike Smith 	printf("ready to begin recovery\n");
42303a31b7ebSMike Smith 	break;
42313a31b7ebSMike Smith     case CISS_LSTATUS_RECOVERING:
42323a31b7ebSMike Smith 	bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
42333a31b7ebSMike Smith 	target = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
42343a31b7ebSMike Smith 	printf("being recovered, working on physical drive %d.%d, %u blocks remaining\n",
42353a31b7ebSMike Smith 	       bus, target, ld->cl_lstatus->blocks_to_recover);
42363a31b7ebSMike Smith 	break;
42373a31b7ebSMike Smith     case CISS_LSTATUS_EXPANDING:
42383a31b7ebSMike Smith 	printf("being expanded, %u blocks remaining\n",
42393a31b7ebSMike Smith 	       ld->cl_lstatus->blocks_to_recover);
42403a31b7ebSMike Smith 	break;
42413a31b7ebSMike Smith     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
42423a31b7ebSMike Smith 	printf("queued for expansion\n");
42433a31b7ebSMike Smith 	break;
42443a31b7ebSMike Smith     case CISS_LSTATUS_FAILED:
42453a31b7ebSMike Smith 	printf("queued for expansion\n");
42463a31b7ebSMike Smith 	break;
42473a31b7ebSMike Smith     case CISS_LSTATUS_WRONG_PDRIVE:
42483a31b7ebSMike Smith 	printf("wrong physical drive inserted\n");
42493a31b7ebSMike Smith 	break;
42503a31b7ebSMike Smith     case CISS_LSTATUS_MISSING_PDRIVE:
42513a31b7ebSMike Smith 	printf("missing a needed physical drive\n");
42523a31b7ebSMike Smith 	break;
42533a31b7ebSMike Smith     case CISS_LSTATUS_BECOMING_READY:
42543a31b7ebSMike Smith 	printf("becoming ready\n");
42553a31b7ebSMike Smith 	break;
42563a31b7ebSMike Smith     }
42573a31b7ebSMike Smith 
4258d4aa427fSPaul Saab     /* print failed physical drives */
42593a31b7ebSMike Smith     for (i = 0; i < CISS_BIG_MAP_ENTRIES / 8; i++) {
42603a31b7ebSMike Smith 	bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_failure_map[i]);
42613a31b7ebSMike Smith 	target = CISS_BIG_MAP_TARGET(sc, ld->cl_lstatus->drive_failure_map[i]);
42623a31b7ebSMike Smith 	if (bus == -1)
42633a31b7ebSMike Smith 	    continue;
42643a31b7ebSMike Smith 	ciss_printf(sc, "physical drive %d:%d (%x) failed\n", bus, target,
42653a31b7ebSMike Smith 		    ld->cl_lstatus->drive_failure_map[i]);
42663a31b7ebSMike Smith     }
42673a31b7ebSMike Smith }
42683a31b7ebSMike Smith 
4269d4aa427fSPaul Saab #ifdef CISS_DEBUG
4270d4aa427fSPaul Saab /************************************************************************
4271d4aa427fSPaul Saab  * Print information about the controller/driver.
4272d4aa427fSPaul Saab  */
4273d4aa427fSPaul Saab static void
4274d4aa427fSPaul Saab ciss_print_adapter(struct ciss_softc *sc)
4275d4aa427fSPaul Saab {
4276609caf8dSPaul Saab     int		i, j;
4277d4aa427fSPaul Saab 
4278d4aa427fSPaul Saab     ciss_printf(sc, "ADAPTER:\n");
4279d4aa427fSPaul Saab     for (i = 0; i < CISSQ_COUNT; i++) {
4280d4aa427fSPaul Saab 	ciss_printf(sc, "%s     %d/%d\n",
4281d4aa427fSPaul Saab 	    i == 0 ? "free" :
4282d4aa427fSPaul Saab 	    i == 1 ? "busy" : "complete",
4283d4aa427fSPaul Saab 	    sc->ciss_qstat[i].q_length,
4284d4aa427fSPaul Saab 	    sc->ciss_qstat[i].q_max);
4285d4aa427fSPaul Saab     }
4286d4aa427fSPaul Saab     ciss_printf(sc, "max_requests %d\n", sc->ciss_max_requests);
4287d4aa427fSPaul Saab     ciss_printf(sc, "flags %b\n", sc->ciss_flags,
4288d4aa427fSPaul Saab 	"\20\1notify_ok\2control_open\3aborting\4running\21fake_synch\22bmic_abort\n");
4289d4aa427fSPaul Saab 
42901fe6c4eeSScott Long     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
4291609caf8dSPaul Saab 	for (j = 0; j < CISS_MAX_LOGICAL; j++) {
4292d4aa427fSPaul Saab 	    ciss_printf(sc, "LOGICAL DRIVE %d:  ", i);
4293609caf8dSPaul Saab 	    ciss_print_ldrive(sc, &sc->ciss_logical[i][j]);
4294609caf8dSPaul Saab 	}
4295d4aa427fSPaul Saab     }
4296d4aa427fSPaul Saab 
42971fe6c4eeSScott Long     /* XXX Should physical drives be printed out here? */
42981fe6c4eeSScott Long 
4299d4aa427fSPaul Saab     for (i = 1; i < sc->ciss_max_requests; i++)
4300d4aa427fSPaul Saab 	ciss_print_request(sc->ciss_request + i);
4301d4aa427fSPaul Saab }
4302d4aa427fSPaul Saab 
4303d4aa427fSPaul Saab /* DDB hook */
4304e6fccf7aSMaxime Henrion static void
4305d4aa427fSPaul Saab ciss_print0(void)
4306d4aa427fSPaul Saab {
4307d4aa427fSPaul Saab     struct ciss_softc	*sc;
4308d4aa427fSPaul Saab 
4309d4aa427fSPaul Saab     sc = devclass_get_softc(devclass_find("ciss"), 0);
4310d4aa427fSPaul Saab     if (sc == NULL) {
4311d4aa427fSPaul Saab 	printf("no ciss controllers\n");
4312d4aa427fSPaul Saab     } else {
4313d4aa427fSPaul Saab 	ciss_print_adapter(sc);
4314d4aa427fSPaul Saab     }
4315d4aa427fSPaul Saab }
4316d4aa427fSPaul Saab #endif
4317d4aa427fSPaul Saab 
43183a31b7ebSMike Smith /************************************************************************
43193a31b7ebSMike Smith  * Return a name for a logical drive status value.
43203a31b7ebSMike Smith  */
43213a31b7ebSMike Smith static const char *
43223a31b7ebSMike Smith ciss_name_ldrive_status(int status)
43233a31b7ebSMike Smith {
43243a31b7ebSMike Smith     switch (status) {
43253a31b7ebSMike Smith     case CISS_LSTATUS_OK:
43263a31b7ebSMike Smith 	return("OK");
43273a31b7ebSMike Smith     case CISS_LSTATUS_FAILED:
43283a31b7ebSMike Smith 	return("failed");
43293a31b7ebSMike Smith     case CISS_LSTATUS_NOT_CONFIGURED:
43303a31b7ebSMike Smith 	return("not configured");
43313a31b7ebSMike Smith     case CISS_LSTATUS_INTERIM_RECOVERY:
43323a31b7ebSMike Smith 	return("interim recovery");
43333a31b7ebSMike Smith     case CISS_LSTATUS_READY_RECOVERY:
43343a31b7ebSMike Smith 	return("ready for recovery");
43353a31b7ebSMike Smith     case CISS_LSTATUS_RECOVERING:
43363a31b7ebSMike Smith 	return("recovering");
43373a31b7ebSMike Smith     case CISS_LSTATUS_WRONG_PDRIVE:
43383a31b7ebSMike Smith 	return("wrong physical drive inserted");
43393a31b7ebSMike Smith     case CISS_LSTATUS_MISSING_PDRIVE:
43403a31b7ebSMike Smith 	return("missing physical drive");
43413a31b7ebSMike Smith     case CISS_LSTATUS_EXPANDING:
43423a31b7ebSMike Smith 	return("expanding");
43433a31b7ebSMike Smith     case CISS_LSTATUS_BECOMING_READY:
43443a31b7ebSMike Smith 	return("becoming ready");
43453a31b7ebSMike Smith     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
43463a31b7ebSMike Smith 	return("queued for expansion");
43473a31b7ebSMike Smith     }
43483a31b7ebSMike Smith     return("unknown status");
43493a31b7ebSMike Smith }
43503a31b7ebSMike Smith 
43513a31b7ebSMike Smith /************************************************************************
43523a31b7ebSMike Smith  * Return an online/offline/nonexistent value for a logical drive
43533a31b7ebSMike Smith  * status value.
43543a31b7ebSMike Smith  */
43553a31b7ebSMike Smith static int
43563a31b7ebSMike Smith ciss_decode_ldrive_status(int status)
43573a31b7ebSMike Smith {
43583a31b7ebSMike Smith     switch(status) {
43593a31b7ebSMike Smith     case CISS_LSTATUS_NOT_CONFIGURED:
43603a31b7ebSMike Smith 	return(CISS_LD_NONEXISTENT);
43613a31b7ebSMike Smith 
43623a31b7ebSMike Smith     case CISS_LSTATUS_OK:
43633a31b7ebSMike Smith     case CISS_LSTATUS_INTERIM_RECOVERY:
43643a31b7ebSMike Smith     case CISS_LSTATUS_READY_RECOVERY:
43653a31b7ebSMike Smith     case CISS_LSTATUS_RECOVERING:
43663a31b7ebSMike Smith     case CISS_LSTATUS_EXPANDING:
43673a31b7ebSMike Smith     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
43683a31b7ebSMike Smith 	return(CISS_LD_ONLINE);
43693a31b7ebSMike Smith 
43703a31b7ebSMike Smith     case CISS_LSTATUS_FAILED:
43713a31b7ebSMike Smith     case CISS_LSTATUS_WRONG_PDRIVE:
43723a31b7ebSMike Smith     case CISS_LSTATUS_MISSING_PDRIVE:
43733a31b7ebSMike Smith     case CISS_LSTATUS_BECOMING_READY:
43743a31b7ebSMike Smith     default:
43753a31b7ebSMike Smith 	return(CISS_LD_OFFLINE);
43763a31b7ebSMike Smith     }
43773a31b7ebSMike Smith }
43783a31b7ebSMike Smith 
43793a31b7ebSMike Smith 
43803a31b7ebSMike Smith /************************************************************************
43813a31b7ebSMike Smith  * Return a name for a logical drive's organisation.
43823a31b7ebSMike Smith  */
43833a31b7ebSMike Smith static const char *
43843a31b7ebSMike Smith ciss_name_ldrive_org(int org)
43853a31b7ebSMike Smith {
43863a31b7ebSMike Smith     switch(org) {
43873a31b7ebSMike Smith     case CISS_LDRIVE_RAID0:
43883a31b7ebSMike Smith 	return("RAID 0");
43893a31b7ebSMike Smith     case CISS_LDRIVE_RAID1:
43903a31b7ebSMike Smith 	return("RAID 1");
43913a31b7ebSMike Smith     case CISS_LDRIVE_RAID4:
43923a31b7ebSMike Smith 	return("RAID 4");
43933a31b7ebSMike Smith     case CISS_LDRIVE_RAID5:
43943a31b7ebSMike Smith 	return("RAID 5");
4395aaf8327bSPaul Saab     case CISS_LDRIVE_RAID51:
4396aaf8327bSPaul Saab 	return("RAID 5+1");
4397aaf8327bSPaul Saab     case CISS_LDRIVE_RAIDADG:
4398aaf8327bSPaul Saab 	return("RAID ADG");
43993a31b7ebSMike Smith     }
44003a31b7ebSMike Smith     return("unkown");
44013a31b7ebSMike Smith }
44023a31b7ebSMike Smith 
44033a31b7ebSMike Smith /************************************************************************
44043a31b7ebSMike Smith  * Return a name for a command status value.
44053a31b7ebSMike Smith  */
44063a31b7ebSMike Smith static const char *
44073a31b7ebSMike Smith ciss_name_command_status(int status)
44083a31b7ebSMike Smith {
44093a31b7ebSMike Smith     switch(status) {
44103a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:
44113a31b7ebSMike Smith 	return("success");
44123a31b7ebSMike Smith     case CISS_CMD_STATUS_TARGET_STATUS:
44133a31b7ebSMike Smith 	return("target status");
44143a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_UNDERRUN:
44153a31b7ebSMike Smith 	return("data underrun");
44163a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_OVERRUN:
44173a31b7ebSMike Smith 	return("data overrun");
44183a31b7ebSMike Smith     case CISS_CMD_STATUS_INVALID_COMMAND:
44193a31b7ebSMike Smith 	return("invalid command");
44203a31b7ebSMike Smith     case CISS_CMD_STATUS_PROTOCOL_ERROR:
44213a31b7ebSMike Smith 	return("protocol error");
44223a31b7ebSMike Smith     case CISS_CMD_STATUS_HARDWARE_ERROR:
44233a31b7ebSMike Smith 	return("hardware error");
44243a31b7ebSMike Smith     case CISS_CMD_STATUS_CONNECTION_LOST:
44253a31b7ebSMike Smith 	return("connection lost");
44263a31b7ebSMike Smith     case CISS_CMD_STATUS_ABORTED:
44273a31b7ebSMike Smith 	return("aborted");
44283a31b7ebSMike Smith     case CISS_CMD_STATUS_ABORT_FAILED:
44293a31b7ebSMike Smith 	return("abort failed");
44303a31b7ebSMike Smith     case CISS_CMD_STATUS_UNSOLICITED_ABORT:
44313a31b7ebSMike Smith 	return("unsolicited abort");
44323a31b7ebSMike Smith     case CISS_CMD_STATUS_TIMEOUT:
44333a31b7ebSMike Smith 	return("timeout");
44343a31b7ebSMike Smith     case CISS_CMD_STATUS_UNABORTABLE:
44353a31b7ebSMike Smith 	return("unabortable");
44363a31b7ebSMike Smith     }
44373a31b7ebSMike Smith     return("unknown status");
44383a31b7ebSMike Smith }
44393a31b7ebSMike Smith 
44403a31b7ebSMike Smith /************************************************************************
44413a31b7ebSMike Smith  * Handle an open on the control device.
44423a31b7ebSMike Smith  */
44433a31b7ebSMike Smith static int
444400b4e54aSWarner Losh ciss_open(struct cdev *dev, int flags, int fmt, struct thread *p)
44453a31b7ebSMike Smith {
44463a31b7ebSMike Smith     struct ciss_softc	*sc;
44473a31b7ebSMike Smith 
44483a31b7ebSMike Smith     debug_called(1);
44493a31b7ebSMike Smith 
44503a31b7ebSMike Smith     sc = (struct ciss_softc *)dev->si_drv1;
44513a31b7ebSMike Smith 
44523a31b7ebSMike Smith     /* we might want to veto if someone already has us open */
44533a31b7ebSMike Smith 
4454975b7318SScott Long     mtx_lock(&sc->ciss_mtx);
44553a31b7ebSMike Smith     sc->ciss_flags |= CISS_FLAG_CONTROL_OPEN;
4456975b7318SScott Long     mtx_unlock(&sc->ciss_mtx);
44573a31b7ebSMike Smith     return(0);
44583a31b7ebSMike Smith }
44593a31b7ebSMike Smith 
44603a31b7ebSMike Smith /************************************************************************
44613a31b7ebSMike Smith  * Handle the last close on the control device.
44623a31b7ebSMike Smith  */
44633a31b7ebSMike Smith static int
446400b4e54aSWarner Losh ciss_close(struct cdev *dev, int flags, int fmt, struct thread *p)
44653a31b7ebSMike Smith {
44663a31b7ebSMike Smith     struct ciss_softc	*sc;
44673a31b7ebSMike Smith 
44683a31b7ebSMike Smith     debug_called(1);
44693a31b7ebSMike Smith 
44703a31b7ebSMike Smith     sc = (struct ciss_softc *)dev->si_drv1;
44713a31b7ebSMike Smith 
4472975b7318SScott Long     mtx_lock(&sc->ciss_mtx);
44733a31b7ebSMike Smith     sc->ciss_flags &= ~CISS_FLAG_CONTROL_OPEN;
4474975b7318SScott Long     mtx_unlock(&sc->ciss_mtx);
44753a31b7ebSMike Smith     return (0);
44763a31b7ebSMike Smith }
44773a31b7ebSMike Smith 
44783a31b7ebSMike Smith /********************************************************************************
44793a31b7ebSMike Smith  * Handle adapter-specific control operations.
44803a31b7ebSMike Smith  *
44813a31b7ebSMike Smith  * Note that the API here is compatible with the Linux driver, in order to
44823a31b7ebSMike Smith  * simplify the porting of Compaq's userland tools.
44833a31b7ebSMike Smith  */
44843a31b7ebSMike Smith static int
448500b4e54aSWarner Losh ciss_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *p)
44863a31b7ebSMike Smith {
44873a31b7ebSMike Smith     struct ciss_softc		*sc;
44887caeec6aSPaul Saab     IOCTL_Command_struct	*ioc	= (IOCTL_Command_struct *)addr;
44897caeec6aSPaul Saab #ifdef __amd64__
44907caeec6aSPaul Saab     IOCTL_Command_struct32	*ioc32	= (IOCTL_Command_struct32 *)addr;
44917caeec6aSPaul Saab     IOCTL_Command_struct	ioc_swab;
44927caeec6aSPaul Saab #endif
44933a31b7ebSMike Smith     int				error;
44943a31b7ebSMike Smith 
44953a31b7ebSMike Smith     debug_called(1);
44963a31b7ebSMike Smith 
44973a31b7ebSMike Smith     sc = (struct ciss_softc *)dev->si_drv1;
44983a31b7ebSMike Smith     error = 0;
4499975b7318SScott Long     mtx_lock(&sc->ciss_mtx);
45003a31b7ebSMike Smith 
45013a31b7ebSMike Smith     switch(cmd) {
450222657ce1SScott Long     case CCISS_GETQSTATS:
450322657ce1SScott Long     {
450422657ce1SScott Long 	union ciss_statrequest *cr = (union ciss_statrequest *)addr;
450522657ce1SScott Long 
450622657ce1SScott Long 	switch (cr->cs_item) {
450722657ce1SScott Long 	case CISSQ_FREE:
450822657ce1SScott Long 	case CISSQ_NOTIFY:
450922657ce1SScott Long 	    bcopy(&sc->ciss_qstat[cr->cs_item], &cr->cs_qstat,
451022657ce1SScott Long 		sizeof(struct ciss_qstat));
451122657ce1SScott Long 	    break;
451222657ce1SScott Long 	default:
451322657ce1SScott Long 	    error = ENOIOCTL;
451422657ce1SScott Long 	    break;
451522657ce1SScott Long 	}
451622657ce1SScott Long 
451722657ce1SScott Long 	break;
451822657ce1SScott Long     }
451922657ce1SScott Long 
45203a31b7ebSMike Smith     case CCISS_GETPCIINFO:
45213a31b7ebSMike Smith     {
45223a31b7ebSMike Smith 	cciss_pci_info_struct	*pis = (cciss_pci_info_struct *)addr;
45233a31b7ebSMike Smith 
45243a31b7ebSMike Smith 	pis->bus = pci_get_bus(sc->ciss_dev);
45253a31b7ebSMike Smith 	pis->dev_fn = pci_get_slot(sc->ciss_dev);
45263a31b7ebSMike Smith 	pis->board_id = pci_get_devid(sc->ciss_dev);
45273a31b7ebSMike Smith 
45283a31b7ebSMike Smith 	break;
45293a31b7ebSMike Smith     }
45303a31b7ebSMike Smith 
45313a31b7ebSMike Smith     case CCISS_GETINTINFO:
45323a31b7ebSMike Smith     {
45333a31b7ebSMike Smith 	cciss_coalint_struct	*cis = (cciss_coalint_struct *)addr;
45343a31b7ebSMike Smith 
45353a31b7ebSMike Smith 	cis->delay = sc->ciss_cfg->interrupt_coalesce_delay;
45363a31b7ebSMike Smith 	cis->count = sc->ciss_cfg->interrupt_coalesce_count;
45373a31b7ebSMike Smith 
45383a31b7ebSMike Smith 	break;
45393a31b7ebSMike Smith     }
45403a31b7ebSMike Smith 
45413a31b7ebSMike Smith     case CCISS_SETINTINFO:
45423a31b7ebSMike Smith     {
45433a31b7ebSMike Smith 	cciss_coalint_struct	*cis = (cciss_coalint_struct *)addr;
45443a31b7ebSMike Smith 
45453a31b7ebSMike Smith 	if ((cis->delay == 0) && (cis->count == 0)) {
45463a31b7ebSMike Smith 	    error = EINVAL;
45473a31b7ebSMike Smith 	    break;
45483a31b7ebSMike Smith 	}
45493a31b7ebSMike Smith 
45503a31b7ebSMike Smith 	/*
45513a31b7ebSMike Smith 	 * XXX apparently this is only safe if the controller is idle,
45523a31b7ebSMike Smith 	 *     we should suspend it before doing this.
45533a31b7ebSMike Smith 	 */
45543a31b7ebSMike Smith 	sc->ciss_cfg->interrupt_coalesce_delay = cis->delay;
45553a31b7ebSMike Smith 	sc->ciss_cfg->interrupt_coalesce_count = cis->count;
45563a31b7ebSMike Smith 
45573a31b7ebSMike Smith 	if (ciss_update_config(sc))
45583a31b7ebSMike Smith 	    error = EIO;
45593a31b7ebSMike Smith 
45603a31b7ebSMike Smith 	/* XXX resume the controller here */
45613a31b7ebSMike Smith 	break;
45623a31b7ebSMike Smith     }
45633a31b7ebSMike Smith 
45643a31b7ebSMike Smith     case CCISS_GETNODENAME:
45653a31b7ebSMike Smith 	bcopy(sc->ciss_cfg->server_name, (NodeName_type *)addr,
45663a31b7ebSMike Smith 	      sizeof(NodeName_type));
45673a31b7ebSMike Smith 	break;
45683a31b7ebSMike Smith 
45693a31b7ebSMike Smith     case CCISS_SETNODENAME:
45703a31b7ebSMike Smith 	bcopy((NodeName_type *)addr, sc->ciss_cfg->server_name,
45713a31b7ebSMike Smith 	      sizeof(NodeName_type));
45723a31b7ebSMike Smith 	if (ciss_update_config(sc))
45733a31b7ebSMike Smith 	    error = EIO;
45743a31b7ebSMike Smith 	break;
45753a31b7ebSMike Smith 
45763a31b7ebSMike Smith     case CCISS_GETHEARTBEAT:
45773a31b7ebSMike Smith 	*(Heartbeat_type *)addr = sc->ciss_cfg->heartbeat;
45783a31b7ebSMike Smith 	break;
45793a31b7ebSMike Smith 
45803a31b7ebSMike Smith     case CCISS_GETBUSTYPES:
45813a31b7ebSMike Smith 	*(BusTypes_type *)addr = sc->ciss_cfg->bus_types;
45823a31b7ebSMike Smith 	break;
45833a31b7ebSMike Smith 
45843a31b7ebSMike Smith     case CCISS_GETFIRMVER:
45853a31b7ebSMike Smith 	bcopy(sc->ciss_id->running_firmware_revision, (FirmwareVer_type *)addr,
45863a31b7ebSMike Smith 	      sizeof(FirmwareVer_type));
45873a31b7ebSMike Smith 	break;
45883a31b7ebSMike Smith 
45893a31b7ebSMike Smith     case CCISS_GETDRIVERVER:
45903a31b7ebSMike Smith 	*(DriverVer_type *)addr = CISS_DRIVER_VERSION;
45913a31b7ebSMike Smith 	break;
45923a31b7ebSMike Smith 
45933a31b7ebSMike Smith     case CCISS_REVALIDVOLS:
45943a31b7ebSMike Smith 	/*
45953a31b7ebSMike Smith 	 * This is a bit ugly; to do it "right" we really need
45963a31b7ebSMike Smith 	 * to find any disks that have changed, kick CAM off them,
45973a31b7ebSMike Smith 	 * then rescan only these disks.  It'd be nice if they
45983a31b7ebSMike Smith 	 * a) told us which disk(s) they were going to play with,
45993a31b7ebSMike Smith 	 * and b) which ones had arrived. 8(
46003a31b7ebSMike Smith 	 */
46013a31b7ebSMike Smith 	break;
46023a31b7ebSMike Smith 
46037caeec6aSPaul Saab #ifdef __amd64__
46047caeec6aSPaul Saab     case CCISS_PASSTHRU32:
46057caeec6aSPaul Saab 	ioc_swab.LUN_info	= ioc32->LUN_info;
46067caeec6aSPaul Saab 	ioc_swab.Request	= ioc32->Request;
46077caeec6aSPaul Saab 	ioc_swab.error_info	= ioc32->error_info;
46087caeec6aSPaul Saab 	ioc_swab.buf_size	= ioc32->buf_size;
46097caeec6aSPaul Saab 	ioc_swab.buf		= (u_int8_t *)(uintptr_t)ioc32->buf;
46107caeec6aSPaul Saab 	ioc			= &ioc_swab;
46117caeec6aSPaul Saab 	/* FALLTHROUGH */
46127caeec6aSPaul Saab #endif
46137caeec6aSPaul Saab 
46143a31b7ebSMike Smith     case CCISS_PASSTHRU:
46157caeec6aSPaul Saab 	error = ciss_user_command(sc, ioc);
46163a31b7ebSMike Smith 	break;
46173a31b7ebSMike Smith 
46183a31b7ebSMike Smith     default:
46193a31b7ebSMike Smith 	debug(0, "unknown ioctl 0x%lx", cmd);
46203a31b7ebSMike Smith 
46213a31b7ebSMike Smith 	debug(1, "CCISS_GETPCIINFO:   0x%lx", CCISS_GETPCIINFO);
46223a31b7ebSMike Smith 	debug(1, "CCISS_GETINTINFO:   0x%lx", CCISS_GETINTINFO);
46233a31b7ebSMike Smith 	debug(1, "CCISS_SETINTINFO:   0x%lx", CCISS_SETINTINFO);
46243a31b7ebSMike Smith 	debug(1, "CCISS_GETNODENAME:  0x%lx", CCISS_GETNODENAME);
46253a31b7ebSMike Smith 	debug(1, "CCISS_SETNODENAME:  0x%lx", CCISS_SETNODENAME);
46263a31b7ebSMike Smith 	debug(1, "CCISS_GETHEARTBEAT: 0x%lx", CCISS_GETHEARTBEAT);
46273a31b7ebSMike Smith 	debug(1, "CCISS_GETBUSTYPES:  0x%lx", CCISS_GETBUSTYPES);
46283a31b7ebSMike Smith 	debug(1, "CCISS_GETFIRMVER:   0x%lx", CCISS_GETFIRMVER);
46293a31b7ebSMike Smith 	debug(1, "CCISS_GETDRIVERVER: 0x%lx", CCISS_GETDRIVERVER);
46303a31b7ebSMike Smith 	debug(1, "CCISS_REVALIDVOLS:  0x%lx", CCISS_REVALIDVOLS);
46313a31b7ebSMike Smith 	debug(1, "CCISS_PASSTHRU:     0x%lx", CCISS_PASSTHRU);
46323a31b7ebSMike Smith 
46333a31b7ebSMike Smith 	error = ENOIOCTL;
46343a31b7ebSMike Smith 	break;
46353a31b7ebSMike Smith     }
46363a31b7ebSMike Smith 
4637975b7318SScott Long     mtx_unlock(&sc->ciss_mtx);
46383a31b7ebSMike Smith     return(error);
46393a31b7ebSMike Smith }
4640