xref: /freebsd/sys/dev/ciss/ciss.c (revision 0fb31ed07302817f365419b1513ba93f842f0f64)
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 
108d745c852SEd Schouten static MALLOC_DEFINE(CISS_MALLOC_CLASS, "ciss_data",
109d745c852SEd Schouten     "ciss internal data buffers");
1103a31b7ebSMike Smith 
1113a31b7ebSMike Smith /* pci interface */
1123a31b7ebSMike Smith static int	ciss_lookup(device_t dev);
1133a31b7ebSMike Smith static int	ciss_probe(device_t dev);
1143a31b7ebSMike Smith static int	ciss_attach(device_t dev);
1153a31b7ebSMike Smith static int	ciss_detach(device_t dev);
1163a31b7ebSMike Smith static int	ciss_shutdown(device_t dev);
1173a31b7ebSMike Smith 
1183a31b7ebSMike Smith /* (de)initialisation functions, control wrappers */
1193a31b7ebSMike Smith static int	ciss_init_pci(struct ciss_softc *sc);
12022657ce1SScott Long static int	ciss_setup_msix(struct ciss_softc *sc);
12122657ce1SScott Long static int	ciss_init_perf(struct ciss_softc *sc);
1223a31b7ebSMike Smith static int	ciss_wait_adapter(struct ciss_softc *sc);
1233a31b7ebSMike Smith static int	ciss_flush_adapter(struct ciss_softc *sc);
1243a31b7ebSMike Smith static int	ciss_init_requests(struct ciss_softc *sc);
1253a31b7ebSMike Smith static void	ciss_command_map_helper(void *arg, bus_dma_segment_t *segs,
1263a31b7ebSMike Smith 					int nseg, int error);
1273a31b7ebSMike Smith static int	ciss_identify_adapter(struct ciss_softc *sc);
1283a31b7ebSMike Smith static int	ciss_init_logical(struct ciss_softc *sc);
129c6131460SPaul Saab static int	ciss_init_physical(struct ciss_softc *sc);
1301fe6c4eeSScott Long static int	ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll);
1313a31b7ebSMike Smith static int	ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld);
1323a31b7ebSMike Smith static int	ciss_get_ldrive_status(struct ciss_softc *sc,  struct ciss_ldrive *ld);
1333a31b7ebSMike Smith static int	ciss_update_config(struct ciss_softc *sc);
13488c78a38SPaul Saab static int	ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld);
13517c0792dSPaul Saab static void	ciss_init_sysctl(struct ciss_softc *sc);
13617c0792dSPaul Saab static void	ciss_soft_reset(struct ciss_softc *sc);
1373a31b7ebSMike Smith static void	ciss_free(struct ciss_softc *sc);
138c6131460SPaul Saab static void	ciss_spawn_notify_thread(struct ciss_softc *sc);
139c6131460SPaul Saab static void	ciss_kill_notify_thread(struct ciss_softc *sc);
1403a31b7ebSMike Smith 
1413a31b7ebSMike Smith /* request submission/completion */
1423a31b7ebSMike Smith static int	ciss_start(struct ciss_request *cr);
14322657ce1SScott Long static void	ciss_done(struct ciss_softc *sc, cr_qhead_t *qh);
14422657ce1SScott Long static void	ciss_perf_done(struct ciss_softc *sc, cr_qhead_t *qh);
1453a31b7ebSMike Smith static void	ciss_intr(void *arg);
14622657ce1SScott Long static void	ciss_perf_intr(void *arg);
14722657ce1SScott Long static void	ciss_perf_msi_intr(void *arg);
14822657ce1SScott Long static void	ciss_complete(struct ciss_softc *sc, cr_qhead_t *qh);
14922657ce1SScott Long static int	_ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status, const char *func);
1503a31b7ebSMike Smith static int	ciss_synch_request(struct ciss_request *cr, int timeout);
1513a31b7ebSMike Smith static int	ciss_poll_request(struct ciss_request *cr, int timeout);
1523a31b7ebSMike Smith static int	ciss_wait_request(struct ciss_request *cr, int timeout);
1536197ca15SPeter Wemm #if 0
1543a31b7ebSMike Smith static int	ciss_abort_request(struct ciss_request *cr);
1556197ca15SPeter Wemm #endif
1563a31b7ebSMike Smith 
1573a31b7ebSMike Smith /* request queueing */
1583a31b7ebSMike Smith static int	ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp);
1593a31b7ebSMike Smith static void	ciss_preen_command(struct ciss_request *cr);
1603a31b7ebSMike Smith static void 	ciss_release_request(struct ciss_request *cr);
1613a31b7ebSMike Smith 
1623a31b7ebSMike Smith /* request helpers */
1633a31b7ebSMike Smith static int	ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
1643a31b7ebSMike Smith 				      int opcode, void **bufp, size_t bufsize);
1653a31b7ebSMike Smith static int	ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc);
1663a31b7ebSMike Smith 
1673a31b7ebSMike Smith /* DMA map/unmap */
1683a31b7ebSMike Smith static int	ciss_map_request(struct ciss_request *cr);
1693a31b7ebSMike Smith static void	ciss_request_map_helper(void *arg, bus_dma_segment_t *segs,
1703a31b7ebSMike Smith 					int nseg, int error);
1713a31b7ebSMike Smith static void	ciss_unmap_request(struct ciss_request *cr);
1723a31b7ebSMike Smith 
1733a31b7ebSMike Smith /* CAM interface */
1743a31b7ebSMike Smith static int	ciss_cam_init(struct ciss_softc *sc);
175c6131460SPaul Saab static void	ciss_cam_rescan_target(struct ciss_softc *sc,
176c6131460SPaul Saab 				       int bus, int target);
1773a31b7ebSMike Smith static void	ciss_cam_action(struct cam_sim *sim, union ccb *ccb);
1783a31b7ebSMike Smith static int	ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio);
1793a31b7ebSMike Smith static int	ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio);
1803a31b7ebSMike Smith static void	ciss_cam_poll(struct cam_sim *sim);
1813a31b7ebSMike Smith static void	ciss_cam_complete(struct ciss_request *cr);
1823a31b7ebSMike Smith static void	ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio);
183c6131460SPaul Saab static int	ciss_name_device(struct ciss_softc *sc, int bus, int target);
1843a31b7ebSMike Smith 
1853a31b7ebSMike Smith /* periodic status monitoring */
1863a31b7ebSMike Smith static void	ciss_periodic(void *arg);
18754d02e0aSMitsuru IWASAKI static void	ciss_nop_complete(struct ciss_request *cr);
188e08d902aSMitsuru IWASAKI static void	ciss_disable_adapter(struct ciss_softc *sc);
1893a31b7ebSMike Smith static void	ciss_notify_event(struct ciss_softc *sc);
1903a31b7ebSMike Smith static void	ciss_notify_complete(struct ciss_request *cr);
1913a31b7ebSMike Smith static int	ciss_notify_abort(struct ciss_softc *sc);
1923a31b7ebSMike Smith static int	ciss_notify_abort_bmic(struct ciss_softc *sc);
1931fe6c4eeSScott Long static void	ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn);
1943a31b7ebSMike Smith static void	ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn);
1953a31b7ebSMike Smith static void	ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn);
1963a31b7ebSMike Smith 
1973a31b7ebSMike Smith /* debugging output */
1983a31b7ebSMike Smith static void	ciss_print_request(struct ciss_request *cr);
1993a31b7ebSMike Smith static void	ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld);
2003a31b7ebSMike Smith static const char *ciss_name_ldrive_status(int status);
2013a31b7ebSMike Smith static int	ciss_decode_ldrive_status(int status);
2023a31b7ebSMike Smith static const char *ciss_name_ldrive_org(int org);
2033a31b7ebSMike Smith static const char *ciss_name_command_status(int status);
2043a31b7ebSMike Smith 
2053a31b7ebSMike Smith /*
2063a31b7ebSMike Smith  * PCI bus interface.
2073a31b7ebSMike Smith  */
2083a31b7ebSMike Smith static device_method_t ciss_methods[] = {
2093a31b7ebSMike Smith     /* Device interface */
2103a31b7ebSMike Smith     DEVMETHOD(device_probe,	ciss_probe),
2113a31b7ebSMike Smith     DEVMETHOD(device_attach,	ciss_attach),
2123a31b7ebSMike Smith     DEVMETHOD(device_detach,	ciss_detach),
2133a31b7ebSMike Smith     DEVMETHOD(device_shutdown,	ciss_shutdown),
2143a31b7ebSMike Smith     { 0, 0 }
2153a31b7ebSMike Smith };
2163a31b7ebSMike Smith 
2173a31b7ebSMike Smith static driver_t ciss_pci_driver = {
2183a31b7ebSMike Smith     "ciss",
2193a31b7ebSMike Smith     ciss_methods,
2203a31b7ebSMike Smith     sizeof(struct ciss_softc)
2213a31b7ebSMike Smith };
2223a31b7ebSMike Smith 
2233a31b7ebSMike Smith static devclass_t	ciss_devclass;
2243a31b7ebSMike Smith DRIVER_MODULE(ciss, pci, ciss_pci_driver, ciss_devclass, 0, 0);
22591d7721cSMaxim Konovalov MODULE_DEPEND(ciss, cam, 1, 1, 1);
22691d7721cSMaxim Konovalov MODULE_DEPEND(ciss, pci, 1, 1, 1);
2273a31b7ebSMike Smith 
2283a31b7ebSMike Smith /*
2293a31b7ebSMike Smith  * Control device interface.
2303a31b7ebSMike Smith  */
2313a31b7ebSMike Smith static d_open_t		ciss_open;
2323a31b7ebSMike Smith static d_close_t	ciss_close;
2333a31b7ebSMike Smith static d_ioctl_t	ciss_ioctl;
2343a31b7ebSMike Smith 
2353a31b7ebSMike Smith static struct cdevsw ciss_cdevsw = {
236dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
237975b7318SScott Long 	.d_flags =	0,
2387ac40f5fSPoul-Henning Kamp 	.d_open =	ciss_open,
2397ac40f5fSPoul-Henning Kamp 	.d_close =	ciss_close,
2407ac40f5fSPoul-Henning Kamp 	.d_ioctl =	ciss_ioctl,
2417ac40f5fSPoul-Henning Kamp 	.d_name =	"ciss",
2423a31b7ebSMike Smith };
2433a31b7ebSMike Smith 
2441fe6c4eeSScott Long /*
2451fe6c4eeSScott Long  * This tunable can be set at boot time and controls whether physical devices
2461fe6c4eeSScott Long  * that are marked hidden by the firmware should be exposed anyways.
2471fe6c4eeSScott Long  */
2481fe6c4eeSScott Long static unsigned int ciss_expose_hidden_physical = 0;
2491fe6c4eeSScott Long TUNABLE_INT("hw.ciss.expose_hidden_physical", &ciss_expose_hidden_physical);
2501fe6c4eeSScott Long 
2515a3c4d69SMitsuru IWASAKI static unsigned int ciss_nop_message_heartbeat = 0;
2525a3c4d69SMitsuru IWASAKI TUNABLE_INT("hw.ciss.nop_message_heartbeat", &ciss_nop_message_heartbeat);
2535a3c4d69SMitsuru IWASAKI 
25422657ce1SScott Long /*
25522657ce1SScott Long  * This tunable can force a particular transport to be used:
25622657ce1SScott Long  * <= 0 : use default
25722657ce1SScott Long  *    1 : force simple
25822657ce1SScott Long  *    2 : force performant
25922657ce1SScott Long  */
26022657ce1SScott Long static int ciss_force_transport = 0;
26122657ce1SScott Long TUNABLE_INT("hw.ciss.force_transport", &ciss_force_transport);
26222657ce1SScott Long 
26322657ce1SScott Long /*
26422657ce1SScott Long  * This tunable can force a particular interrupt delivery method to be used:
26522657ce1SScott Long  * <= 0 : use default
26622657ce1SScott Long  *    1 : force INTx
26722657ce1SScott Long  *    2 : force MSIX
26822657ce1SScott Long  */
26922657ce1SScott Long static int ciss_force_interrupt = 0;
27022657ce1SScott Long TUNABLE_INT("hw.ciss.force_interrupt", &ciss_force_interrupt);
27122657ce1SScott Long 
2723a31b7ebSMike Smith /************************************************************************
2733a31b7ebSMike Smith  * CISS adapters amazingly don't have a defined programming interface
2743a31b7ebSMike Smith  * value.  (One could say some very despairing things about PCI and
2753a31b7ebSMike Smith  * people just not getting the general idea.)  So we are forced to
2763a31b7ebSMike Smith  * stick with matching against subvendor/subdevice, and thus have to
2773a31b7ebSMike Smith  * be updated for every new CISS adapter that appears.
2783a31b7ebSMike Smith  */
2792fdaa90dSScott Long #define CISS_BOARD_UNKNWON	0
2802fdaa90dSScott Long #define CISS_BOARD_SA5		1
2812fdaa90dSScott Long #define CISS_BOARD_SA5B		2
2822fdaa90dSScott Long #define CISS_BOARD_NOMSI	(1<<4)
283ee4827b2SSean Bruno #define CISS_BOARD_SIMPLE       (1<<5)
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[] = {
292ee4827b2SSean Bruno     { 0x0e11, 0x4070, CISS_BOARD_SA5|CISS_BOARD_NOMSI|CISS_BOARD_SIMPLE,
293ee4827b2SSean Bruno                                                         "Compaq Smart Array 5300" },
2942fdaa90dSScott Long     { 0x0e11, 0x4080, CISS_BOARD_SA5B|CISS_BOARD_NOMSI,	"Compaq Smart Array 5i" },
2952fdaa90dSScott Long     { 0x0e11, 0x4082, CISS_BOARD_SA5B|CISS_BOARD_NOMSI,	"Compaq Smart Array 532" },
2962fdaa90dSScott Long     { 0x0e11, 0x4083, CISS_BOARD_SA5B|CISS_BOARD_NOMSI,	"HP Smart Array 5312" },
297b26392c7SPaul Saab     { 0x0e11, 0x4091, CISS_BOARD_SA5,	"HP Smart Array 6i" },
2989e7313bbSPaul Saab     { 0x0e11, 0x409A, CISS_BOARD_SA5,	"HP Smart Array 641" },
2999e7313bbSPaul Saab     { 0x0e11, 0x409B, CISS_BOARD_SA5,	"HP Smart Array 642" },
3009e7313bbSPaul Saab     { 0x0e11, 0x409C, CISS_BOARD_SA5,	"HP Smart Array 6400" },
3019e7313bbSPaul Saab     { 0x0e11, 0x409D, CISS_BOARD_SA5,	"HP Smart Array 6400 EM" },
302cad572c4SPaul Saab     { 0x103C, 0x3211, CISS_BOARD_SA5,	"HP Smart Array E200i" },
303cad572c4SPaul Saab     { 0x103C, 0x3212, CISS_BOARD_SA5,	"HP Smart Array E200" },
304cad572c4SPaul Saab     { 0x103C, 0x3213, CISS_BOARD_SA5,	"HP Smart Array E200i" },
305cad572c4SPaul Saab     { 0x103C, 0x3214, CISS_BOARD_SA5,	"HP Smart Array E200i" },
306cad572c4SPaul Saab     { 0x103C, 0x3215, CISS_BOARD_SA5,	"HP Smart Array E200i" },
307b612d5e1SPaul Saab     { 0x103C, 0x3220, CISS_BOARD_SA5,	"HP Smart Array" },
308b612d5e1SPaul Saab     { 0x103C, 0x3222, CISS_BOARD_SA5,	"HP Smart Array" },
3094bb10cf1SPaul Saab     { 0x103C, 0x3223, CISS_BOARD_SA5,	"HP Smart Array P800" },
310f3f82767SPaul Saab     { 0x103C, 0x3225, CISS_BOARD_SA5,	"HP Smart Array P600" },
311b612d5e1SPaul Saab     { 0x103C, 0x3230, CISS_BOARD_SA5,	"HP Smart Array" },
312cad572c4SPaul Saab     { 0x103C, 0x3231, CISS_BOARD_SA5,	"HP Smart Array" },
313b612d5e1SPaul Saab     { 0x103C, 0x3232, CISS_BOARD_SA5,	"HP Smart Array" },
314b612d5e1SPaul Saab     { 0x103C, 0x3233, CISS_BOARD_SA5,	"HP Smart Array" },
315cad572c4SPaul Saab     { 0x103C, 0x3234, CISS_BOARD_SA5,	"HP Smart Array P400" },
316cad572c4SPaul Saab     { 0x103C, 0x3235, CISS_BOARD_SA5,	"HP Smart Array P400i" },
317b612d5e1SPaul Saab     { 0x103C, 0x3236, CISS_BOARD_SA5,	"HP Smart Array" },
3187b6d3d4cSScott Long     { 0x103C, 0x3237, CISS_BOARD_SA5,	"HP Smart Array E500" },
319b612d5e1SPaul Saab     { 0x103C, 0x3238, CISS_BOARD_SA5,	"HP Smart Array" },
320b612d5e1SPaul Saab     { 0x103C, 0x3239, CISS_BOARD_SA5,	"HP Smart Array" },
321b612d5e1SPaul Saab     { 0x103C, 0x323A, CISS_BOARD_SA5,	"HP Smart Array" },
322b612d5e1SPaul Saab     { 0x103C, 0x323B, CISS_BOARD_SA5,	"HP Smart Array" },
323b612d5e1SPaul Saab     { 0x103C, 0x323C, CISS_BOARD_SA5,	"HP Smart Array" },
3247b6d3d4cSScott Long     { 0x103C, 0x323D, CISS_BOARD_SA5,	"HP Smart Array P700m" },
325160b4e6bSPaul Saab     { 0x103C, 0x3241, CISS_BOARD_SA5,	"HP Smart Array P212" },
326160b4e6bSPaul Saab     { 0x103C, 0x3243, CISS_BOARD_SA5,	"HP Smart Array P410" },
327160b4e6bSPaul Saab     { 0x103C, 0x3245, CISS_BOARD_SA5,	"HP Smart Array P410i" },
328160b4e6bSPaul Saab     { 0x103C, 0x3247, CISS_BOARD_SA5,	"HP Smart Array P411" },
329160b4e6bSPaul Saab     { 0x103C, 0x3249, CISS_BOARD_SA5,	"HP Smart Array P812" },
3307b6d3d4cSScott Long     { 0x103C, 0x324A, CISS_BOARD_SA5,	"HP Smart Array P712m" },
3317b6d3d4cSScott Long     { 0x103C, 0x324B, CISS_BOARD_SA5,	"HP Smart Array" },
332e17ef005SSean Bruno     { 0x103C, 0x3350, CISS_BOARD_SA5,   "HP Smart Array P222" },
3335564c1cdSSean Bruno     { 0x103C, 0x3351, CISS_BOARD_SA5,   "HP Smart Array P420" },
334e17ef005SSean Bruno     { 0x103C, 0x3352, CISS_BOARD_SA5,   "HP Smart Array P421" },
335e17ef005SSean Bruno     { 0x103C, 0x3353, CISS_BOARD_SA5,   "HP Smart Array P822" },
336e17ef005SSean Bruno     { 0x103C, 0x3354, CISS_BOARD_SA5,   "HP Smart Array P420i" },
337e17ef005SSean Bruno     { 0x103C, 0x3355, CISS_BOARD_SA5,   "HP Smart Array P220i" },
338e17ef005SSean Bruno     { 0x103C, 0x3356, CISS_BOARD_SA5,   "HP Smart Array P721m" },
339ec5d9810SSean Bruno     { 0x103C, 0x1920, CISS_BOARD_SA5,   "HP Smart Array P430i" },
340ec5d9810SSean Bruno     { 0x103C, 0x1921, CISS_BOARD_SA5,   "HP Smart Array P830i" },
341ec5d9810SSean Bruno     { 0x103C, 0x1922, CISS_BOARD_SA5,   "HP Smart Array P430" },
342ec5d9810SSean Bruno     { 0x103C, 0x1923, CISS_BOARD_SA5,   "HP Smart Array P431" },
343ec5d9810SSean Bruno     { 0x103C, 0x1924, CISS_BOARD_SA5,   "HP Smart Array P830" },
344ec5d9810SSean Bruno     { 0x103C, 0x1926, CISS_BOARD_SA5,   "HP Smart Array P731m" },
345ec5d9810SSean Bruno     { 0x103C, 0x1928, CISS_BOARD_SA5,   "HP Smart Array P230i" },
346ec5d9810SSean Bruno     { 0x103C, 0x1929, CISS_BOARD_SA5,   "HP Smart Array P530" },
347ec5d9810SSean Bruno     { 0x103C, 0x192A, CISS_BOARD_SA5,   "HP Smart Array P531" },
3484cd27111SSean Bruno     { 0x103C, 0x21BD, CISS_BOARD_SA5,   "HP Smart Array TBD" },
3494cd27111SSean Bruno     { 0x103C, 0x21BE, CISS_BOARD_SA5,   "HP Smart Array TBD" },
3504cd27111SSean Bruno     { 0x103C, 0x21BF, CISS_BOARD_SA5,   "HP Smart Array TBD" },
3514cd27111SSean Bruno     { 0x103C, 0x21C0, CISS_BOARD_SA5,   "HP Smart Array TBD" },
3524cd27111SSean Bruno     { 0x103C, 0x21C2, CISS_BOARD_SA5,   "HP Smart Array TBD" },
3534cd27111SSean Bruno     { 0x103C, 0x21C3, CISS_BOARD_SA5,   "HP Smart Array TBD" },
3544cd27111SSean Bruno     { 0x103C, 0x21C5, CISS_BOARD_SA5,   "HP Smart Array TBD" },
3554cd27111SSean Bruno     { 0x103C, 0x21C6, CISS_BOARD_SA5,   "HP Smart Array TBD" },
3564cd27111SSean Bruno     { 0x103C, 0x21C7, CISS_BOARD_SA5,   "HP Smart Array TBD" },
3574cd27111SSean Bruno     { 0x103C, 0x21C8, CISS_BOARD_SA5,   "HP Smart Array TBD" },
3584cd27111SSean Bruno     { 0x103C, 0x21CA, CISS_BOARD_SA5,   "HP Smart Array TBD" },
3594cd27111SSean Bruno     { 0x103C, 0x21CB, CISS_BOARD_SA5,   "HP Smart Array TBD" },
3604cd27111SSean Bruno     { 0x103C, 0x21CC, CISS_BOARD_SA5,   "HP Smart Array TBD" },
3614cd27111SSean Bruno     { 0x103C, 0x21CD, CISS_BOARD_SA5,   "HP Smart Array TBD" },
3624cd27111SSean Bruno     { 0x103C, 0x21CE, CISS_BOARD_SA5,   "HP Smart Array TBD" },
3634a6a94d8SArchie Cobbs     { 0, 0, 0, NULL }
3643a31b7ebSMike Smith };
3653a31b7ebSMike Smith 
3663a31b7ebSMike Smith /************************************************************************
3673a31b7ebSMike Smith  * Find a match for the device in our list of known adapters.
3683a31b7ebSMike Smith  */
3693a31b7ebSMike Smith static int
3703a31b7ebSMike Smith ciss_lookup(device_t dev)
3713a31b7ebSMike Smith {
3723a31b7ebSMike Smith     int 	i;
3733a31b7ebSMike Smith 
3743a31b7ebSMike Smith     for (i = 0; ciss_vendor_data[i].desc != NULL; i++)
3753a31b7ebSMike Smith 	if ((pci_get_subvendor(dev) == ciss_vendor_data[i].subvendor) &&
3763a31b7ebSMike Smith 	    (pci_get_subdevice(dev) == ciss_vendor_data[i].subdevice)) {
3773a31b7ebSMike Smith 	    return(i);
3783a31b7ebSMike Smith 	}
3793a31b7ebSMike Smith     return(-1);
3803a31b7ebSMike Smith }
3813a31b7ebSMike Smith 
3823a31b7ebSMike Smith /************************************************************************
3833a31b7ebSMike Smith  * Match a known CISS adapter.
3843a31b7ebSMike Smith  */
3853a31b7ebSMike Smith static int
3863a31b7ebSMike Smith ciss_probe(device_t dev)
3873a31b7ebSMike Smith {
3883a31b7ebSMike Smith     int		i;
3893a31b7ebSMike Smith 
3903a31b7ebSMike Smith     i = ciss_lookup(dev);
3913a31b7ebSMike Smith     if (i != -1) {
3923a31b7ebSMike Smith 	device_set_desc(dev, ciss_vendor_data[i].desc);
393538565c4SWarner Losh 	return(BUS_PROBE_DEFAULT);
3943a31b7ebSMike Smith     }
3953a31b7ebSMike Smith     return(ENOENT);
3963a31b7ebSMike Smith }
3973a31b7ebSMike Smith 
3983a31b7ebSMike Smith /************************************************************************
3993a31b7ebSMike Smith  * Attach the driver to this adapter.
4003a31b7ebSMike Smith  */
4013a31b7ebSMike Smith static int
4023a31b7ebSMike Smith ciss_attach(device_t dev)
4033a31b7ebSMike Smith {
4043a31b7ebSMike Smith     struct ciss_softc	*sc;
405d69c9c78SScott Long     int			error;
4063a31b7ebSMike Smith 
4073a31b7ebSMike Smith     debug_called(1);
4083a31b7ebSMike Smith 
4093a31b7ebSMike Smith #ifdef CISS_DEBUG
4103a31b7ebSMike Smith     /* print structure/union sizes */
4113a31b7ebSMike Smith     debug_struct(ciss_command);
4123a31b7ebSMike Smith     debug_struct(ciss_header);
4133a31b7ebSMike Smith     debug_union(ciss_device_address);
4143a31b7ebSMike Smith     debug_struct(ciss_cdb);
4153a31b7ebSMike Smith     debug_struct(ciss_report_cdb);
4163a31b7ebSMike Smith     debug_struct(ciss_notify_cdb);
4173a31b7ebSMike Smith     debug_struct(ciss_notify);
4183a31b7ebSMike Smith     debug_struct(ciss_message_cdb);
4193a31b7ebSMike Smith     debug_struct(ciss_error_info_pointer);
4203a31b7ebSMike Smith     debug_struct(ciss_error_info);
4213a31b7ebSMike Smith     debug_struct(ciss_sg_entry);
4223a31b7ebSMike Smith     debug_struct(ciss_config_table);
4233a31b7ebSMike Smith     debug_struct(ciss_bmic_cdb);
4243a31b7ebSMike Smith     debug_struct(ciss_bmic_id_ldrive);
4253a31b7ebSMike Smith     debug_struct(ciss_bmic_id_lstatus);
4263a31b7ebSMike Smith     debug_struct(ciss_bmic_id_table);
4273a31b7ebSMike Smith     debug_struct(ciss_bmic_id_pdrive);
4283a31b7ebSMike Smith     debug_struct(ciss_bmic_blink_pdrive);
4293a31b7ebSMike Smith     debug_struct(ciss_bmic_flush_cache);
4303a31b7ebSMike Smith     debug_const(CISS_MAX_REQUESTS);
4313a31b7ebSMike Smith     debug_const(CISS_MAX_LOGICAL);
4323a31b7ebSMike Smith     debug_const(CISS_INTERRUPT_COALESCE_DELAY);
4333a31b7ebSMike Smith     debug_const(CISS_INTERRUPT_COALESCE_COUNT);
4343a31b7ebSMike Smith     debug_const(CISS_COMMAND_ALLOC_SIZE);
4353a31b7ebSMike Smith     debug_const(CISS_COMMAND_SG_LENGTH);
4363a31b7ebSMike Smith 
4373a31b7ebSMike Smith     debug_type(cciss_pci_info_struct);
4383a31b7ebSMike Smith     debug_type(cciss_coalint_struct);
4393a31b7ebSMike Smith     debug_type(cciss_coalint_struct);
4403a31b7ebSMike Smith     debug_type(NodeName_type);
4413a31b7ebSMike Smith     debug_type(NodeName_type);
4423a31b7ebSMike Smith     debug_type(Heartbeat_type);
4433a31b7ebSMike Smith     debug_type(BusTypes_type);
4443a31b7ebSMike Smith     debug_type(FirmwareVer_type);
4453a31b7ebSMike Smith     debug_type(DriverVer_type);
4463a31b7ebSMike Smith     debug_type(IOCTL_Command_struct);
4473a31b7ebSMike Smith #endif
4483a31b7ebSMike Smith 
4493a31b7ebSMike Smith     sc = device_get_softc(dev);
4503a31b7ebSMike Smith     sc->ciss_dev = dev;
45164a026bdSGavin Atkinson     mtx_init(&sc->ciss_mtx, "cissmtx", NULL, MTX_DEF);
452aa1385cdSJohn Baldwin     callout_init_mtx(&sc->ciss_periodic, &sc->ciss_mtx, 0);
4533a31b7ebSMike Smith 
4543a31b7ebSMike Smith     /*
4553a31b7ebSMike Smith      * Do PCI-specific init.
4563a31b7ebSMike Smith      */
4573a31b7ebSMike Smith     if ((error = ciss_init_pci(sc)) != 0)
4583a31b7ebSMike Smith 	goto out;
4593a31b7ebSMike Smith 
4603a31b7ebSMike Smith     /*
4613a31b7ebSMike Smith      * Initialise driver queues.
4623a31b7ebSMike Smith      */
4633a31b7ebSMike Smith     ciss_initq_free(sc);
464c6131460SPaul Saab     ciss_initq_notify(sc);
4653a31b7ebSMike Smith 
4663a31b7ebSMike Smith     /*
46717c0792dSPaul Saab      * Initalize device sysctls.
46817c0792dSPaul Saab      */
46917c0792dSPaul Saab     ciss_init_sysctl(sc);
47017c0792dSPaul Saab 
47117c0792dSPaul Saab     /*
4723a31b7ebSMike Smith      * Initialise command/request pool.
4733a31b7ebSMike Smith      */
4743a31b7ebSMike Smith     if ((error = ciss_init_requests(sc)) != 0)
4753a31b7ebSMike Smith 	goto out;
4763a31b7ebSMike Smith 
4773a31b7ebSMike Smith     /*
4783a31b7ebSMike Smith      * Get adapter information.
4793a31b7ebSMike Smith      */
4803a31b7ebSMike Smith     if ((error = ciss_identify_adapter(sc)) != 0)
4813a31b7ebSMike Smith 	goto out;
4823a31b7ebSMike Smith 
4833a31b7ebSMike Smith     /*
484c6131460SPaul Saab      * Find all the physical devices.
485c6131460SPaul Saab      */
486c6131460SPaul Saab     if ((error = ciss_init_physical(sc)) != 0)
487c6131460SPaul Saab 	goto out;
488c6131460SPaul Saab 
489c6131460SPaul Saab     /*
4903a31b7ebSMike Smith      * Build our private table of logical devices.
4913a31b7ebSMike Smith      */
4923a31b7ebSMike Smith     if ((error = ciss_init_logical(sc)) != 0)
4933a31b7ebSMike Smith 	goto out;
4943a31b7ebSMike Smith 
4953a31b7ebSMike Smith     /*
4963a31b7ebSMike Smith      * Enable interrupts so that the CAM scan can complete.
4973a31b7ebSMike Smith      */
4983a31b7ebSMike Smith     CISS_TL_SIMPLE_ENABLE_INTERRUPTS(sc);
4993a31b7ebSMike Smith 
5003a31b7ebSMike Smith     /*
5013a31b7ebSMike Smith      * Initialise the CAM interface.
5023a31b7ebSMike Smith      */
5033a31b7ebSMike Smith     if ((error = ciss_cam_init(sc)) != 0)
5043a31b7ebSMike Smith 	goto out;
5053a31b7ebSMike Smith 
5063a31b7ebSMike Smith     /*
5073a31b7ebSMike Smith      * Start the heartbeat routine and event chain.
5083a31b7ebSMike Smith      */
5093a31b7ebSMike Smith     ciss_periodic(sc);
5103a31b7ebSMike Smith 
5113a31b7ebSMike Smith    /*
5123a31b7ebSMike Smith      * Create the control device.
5133a31b7ebSMike Smith      */
5143a31b7ebSMike Smith     sc->ciss_dev_t = make_dev(&ciss_cdevsw, device_get_unit(sc->ciss_dev),
5153a31b7ebSMike Smith 			      UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
5163a31b7ebSMike Smith 			      "ciss%d", device_get_unit(sc->ciss_dev));
5173a31b7ebSMike Smith     sc->ciss_dev_t->si_drv1 = sc;
5183a31b7ebSMike Smith 
5193a31b7ebSMike Smith     /*
5203a31b7ebSMike Smith      * The adapter is running; synchronous commands can now sleep
5213a31b7ebSMike Smith      * waiting for an interrupt to signal completion.
5223a31b7ebSMike Smith      */
5233a31b7ebSMike Smith     sc->ciss_flags |= CISS_FLAG_RUNNING;
5243a31b7ebSMike Smith 
525c6131460SPaul Saab     ciss_spawn_notify_thread(sc);
526c6131460SPaul Saab 
5273a31b7ebSMike Smith     error = 0;
5283a31b7ebSMike Smith  out:
52964a026bdSGavin Atkinson     if (error != 0) {
53064a026bdSGavin Atkinson 	/* ciss_free() expects the mutex to be held */
53164a026bdSGavin Atkinson 	mtx_lock(&sc->ciss_mtx);
5323a31b7ebSMike Smith 	ciss_free(sc);
53364a026bdSGavin Atkinson     }
5343a31b7ebSMike Smith     return(error);
5353a31b7ebSMike Smith }
5363a31b7ebSMike Smith 
5373a31b7ebSMike Smith /************************************************************************
5383a31b7ebSMike Smith  * Detach the driver from this adapter.
5393a31b7ebSMike Smith  */
5403a31b7ebSMike Smith static int
5413a31b7ebSMike Smith ciss_detach(device_t dev)
5423a31b7ebSMike Smith {
5433a31b7ebSMike Smith     struct ciss_softc	*sc = device_get_softc(dev);
5443a31b7ebSMike Smith 
5453a31b7ebSMike Smith     debug_called(1);
5463a31b7ebSMike Smith 
547975b7318SScott Long     mtx_lock(&sc->ciss_mtx);
548975b7318SScott Long     if (sc->ciss_flags & CISS_FLAG_CONTROL_OPEN) {
549975b7318SScott Long 	mtx_unlock(&sc->ciss_mtx);
550ffdf82e1SPaul Saab 	return (EBUSY);
551975b7318SScott Long     }
552ffdf82e1SPaul Saab 
5533a31b7ebSMike Smith     /* flush adapter cache */
5543a31b7ebSMike Smith     ciss_flush_adapter(sc);
5553a31b7ebSMike Smith 
556975b7318SScott Long     /* release all resources.  The mutex is released and freed here too. */
5573a31b7ebSMike Smith     ciss_free(sc);
5583a31b7ebSMike Smith 
5593a31b7ebSMike Smith     return(0);
5603a31b7ebSMike Smith }
5613a31b7ebSMike Smith 
5623a31b7ebSMike Smith /************************************************************************
5633a31b7ebSMike Smith  * Prepare adapter for system shutdown.
5643a31b7ebSMike Smith  */
5653a31b7ebSMike Smith static int
5663a31b7ebSMike Smith ciss_shutdown(device_t dev)
5673a31b7ebSMike Smith {
5683a31b7ebSMike Smith     struct ciss_softc	*sc = device_get_softc(dev);
5693a31b7ebSMike Smith 
5703a31b7ebSMike Smith     debug_called(1);
5713a31b7ebSMike Smith 
572d4a4ddc6SScott Long     mtx_lock(&sc->ciss_mtx);
5733a31b7ebSMike Smith     /* flush adapter cache */
5743a31b7ebSMike Smith     ciss_flush_adapter(sc);
5753a31b7ebSMike Smith 
57617c0792dSPaul Saab     if (sc->ciss_soft_reset)
57717c0792dSPaul Saab 	ciss_soft_reset(sc);
578d4a4ddc6SScott Long     mtx_unlock(&sc->ciss_mtx);
57917c0792dSPaul Saab 
5803a31b7ebSMike Smith     return(0);
5813a31b7ebSMike Smith }
5823a31b7ebSMike Smith 
58317c0792dSPaul Saab static void
58417c0792dSPaul Saab ciss_init_sysctl(struct ciss_softc *sc)
58517c0792dSPaul Saab {
58617c0792dSPaul Saab 
58717c0792dSPaul Saab     SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->ciss_dev),
58817c0792dSPaul Saab 	SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ciss_dev)),
58917c0792dSPaul Saab 	OID_AUTO, "soft_reset", CTLFLAG_RW, &sc->ciss_soft_reset, 0, "");
59017c0792dSPaul Saab }
59117c0792dSPaul Saab 
5923a31b7ebSMike Smith /************************************************************************
5933a31b7ebSMike Smith  * Perform PCI-specific attachment actions.
5943a31b7ebSMike Smith  */
5953a31b7ebSMike Smith static int
5963a31b7ebSMike Smith ciss_init_pci(struct ciss_softc *sc)
5973a31b7ebSMike Smith {
5983a31b7ebSMike Smith     uintptr_t		cbase, csize, cofs;
59922657ce1SScott Long     uint32_t		method, supported_methods;
600d69c9c78SScott Long     int			error, sqmask, i;
60122657ce1SScott Long     void		*intr;
6023a31b7ebSMike Smith 
6033a31b7ebSMike Smith     debug_called(1);
6043a31b7ebSMike Smith 
6053a31b7ebSMike Smith     /*
606d69c9c78SScott Long      * Work out adapter type.
607d69c9c78SScott Long      */
608d69c9c78SScott Long     i = ciss_lookup(sc->ciss_dev);
609d69c9c78SScott Long     if (i < 0) {
610d69c9c78SScott Long 	ciss_printf(sc, "unknown adapter type\n");
611d69c9c78SScott Long 	return (ENXIO);
612d69c9c78SScott Long     }
613d69c9c78SScott Long 
614d69c9c78SScott Long     if (ciss_vendor_data[i].flags & CISS_BOARD_SA5) {
615d69c9c78SScott Long 	sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5;
616d69c9c78SScott Long     } else if (ciss_vendor_data[i].flags & CISS_BOARD_SA5B) {
617d69c9c78SScott Long 	sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5B;
618d69c9c78SScott Long     } else {
619d69c9c78SScott Long 	/*
620d69c9c78SScott Long 	 * XXX Big hammer, masks/unmasks all possible interrupts.  This should
621d69c9c78SScott Long 	 * work on all hardware variants.  Need to add code to handle the
622d69c9c78SScott Long 	 * "controller crashed" interupt bit that this unmasks.
623d69c9c78SScott Long 	 */
624d69c9c78SScott Long 	sqmask = ~0;
625d69c9c78SScott Long     }
626d69c9c78SScott Long 
627d69c9c78SScott Long     /*
6283a31b7ebSMike Smith      * Allocate register window first (we need this to find the config
6293a31b7ebSMike Smith      * struct).
6303a31b7ebSMike Smith      */
6313a31b7ebSMike Smith     error = ENXIO;
6323a31b7ebSMike Smith     sc->ciss_regs_rid = CISS_TL_SIMPLE_BAR_REGS;
6333a31b7ebSMike Smith     if ((sc->ciss_regs_resource =
6345f96beb9SNate Lawson 	 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
6355f96beb9SNate Lawson 				&sc->ciss_regs_rid, RF_ACTIVE)) == NULL) {
6363a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate register window\n");
6373a31b7ebSMike Smith 	return(ENXIO);
6383a31b7ebSMike Smith     }
6393a31b7ebSMike Smith     sc->ciss_regs_bhandle = rman_get_bushandle(sc->ciss_regs_resource);
6403a31b7ebSMike Smith     sc->ciss_regs_btag = rman_get_bustag(sc->ciss_regs_resource);
6413a31b7ebSMike Smith 
6423a31b7ebSMike Smith     /*
6433a31b7ebSMike Smith      * Find the BAR holding the config structure.  If it's not the one
6443a31b7ebSMike Smith      * we already mapped for registers, map it too.
6453a31b7ebSMike Smith      */
6463a31b7ebSMike Smith     sc->ciss_cfg_rid = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_BAR) & 0xffff;
6473a31b7ebSMike Smith     if (sc->ciss_cfg_rid != sc->ciss_regs_rid) {
6483a31b7ebSMike Smith 	if ((sc->ciss_cfg_resource =
6495f96beb9SNate Lawson 	     bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
6505f96beb9SNate Lawson 				    &sc->ciss_cfg_rid, RF_ACTIVE)) == NULL) {
6513a31b7ebSMike Smith 	    ciss_printf(sc, "can't allocate config window\n");
6523a31b7ebSMike Smith 	    return(ENXIO);
6533a31b7ebSMike Smith 	}
6543a31b7ebSMike Smith 	cbase = (uintptr_t)rman_get_virtual(sc->ciss_cfg_resource);
6553a31b7ebSMike Smith 	csize = rman_get_end(sc->ciss_cfg_resource) -
6563a31b7ebSMike Smith 	    rman_get_start(sc->ciss_cfg_resource) + 1;
6573a31b7ebSMike Smith     } else {
6583a31b7ebSMike Smith 	cbase = (uintptr_t)rman_get_virtual(sc->ciss_regs_resource);
6593a31b7ebSMike Smith 	csize = rman_get_end(sc->ciss_regs_resource) -
6603a31b7ebSMike Smith 	    rman_get_start(sc->ciss_regs_resource) + 1;
6613a31b7ebSMike Smith     }
6623a31b7ebSMike Smith     cofs = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_OFF);
6633a31b7ebSMike Smith 
6643a31b7ebSMike Smith     /*
6653a31b7ebSMike Smith      * Use the base/size/offset values we just calculated to
6663a31b7ebSMike Smith      * sanity-check the config structure.  If it's OK, point to it.
6673a31b7ebSMike Smith      */
6683a31b7ebSMike Smith     if ((cofs + sizeof(struct ciss_config_table)) > csize) {
6693a31b7ebSMike Smith 	ciss_printf(sc, "config table outside window\n");
6703a31b7ebSMike Smith 	return(ENXIO);
6713a31b7ebSMike Smith     }
6723a31b7ebSMike Smith     sc->ciss_cfg = (struct ciss_config_table *)(cbase + cofs);
6733a31b7ebSMike Smith     debug(1, "config struct at %p", sc->ciss_cfg);
6743a31b7ebSMike Smith 
6753a31b7ebSMike Smith     /*
67622657ce1SScott Long      * Calculate the number of request structures/commands we are
67722657ce1SScott Long      * going to provide for this adapter.
67822657ce1SScott Long      */
67922657ce1SScott Long     sc->ciss_max_requests = min(CISS_MAX_REQUESTS, sc->ciss_cfg->max_outstanding_commands);
68022657ce1SScott Long 
68122657ce1SScott Long     /*
6823a31b7ebSMike Smith      * Validate the config structure.  If we supported other transport
6833a31b7ebSMike Smith      * methods, we could select amongst them at this point in time.
6843a31b7ebSMike Smith      */
6853a31b7ebSMike Smith     if (strncmp(sc->ciss_cfg->signature, "CISS", 4)) {
6863a31b7ebSMike Smith 	ciss_printf(sc, "config signature mismatch (got '%c%c%c%c')\n",
6873a31b7ebSMike Smith 		    sc->ciss_cfg->signature[0], sc->ciss_cfg->signature[1],
6883a31b7ebSMike Smith 		    sc->ciss_cfg->signature[2], sc->ciss_cfg->signature[3]);
6893a31b7ebSMike Smith 	return(ENXIO);
6903a31b7ebSMike Smith     }
6913a31b7ebSMike Smith 
6923a31b7ebSMike Smith     /*
69322657ce1SScott Long      * Select the mode of operation, prefer Performant.
6943a31b7ebSMike Smith      */
69522657ce1SScott Long     if (!(sc->ciss_cfg->supported_methods &
69622657ce1SScott Long 	(CISS_TRANSPORT_METHOD_SIMPLE | CISS_TRANSPORT_METHOD_PERF))) {
69722657ce1SScott Long 	ciss_printf(sc, "No supported transport layers: 0x%x\n",
69822657ce1SScott Long 	    sc->ciss_cfg->supported_methods);
6993a31b7ebSMike Smith     }
70022657ce1SScott Long 
70122657ce1SScott Long     switch (ciss_force_transport) {
70222657ce1SScott Long     case 1:
70322657ce1SScott Long 	supported_methods = CISS_TRANSPORT_METHOD_SIMPLE;
70422657ce1SScott Long 	break;
70522657ce1SScott Long     case 2:
70622657ce1SScott Long 	supported_methods = CISS_TRANSPORT_METHOD_PERF;
70722657ce1SScott Long 	break;
70822657ce1SScott Long     default:
709ee4827b2SSean Bruno         /*
710ee4827b2SSean Bruno          * Override the capabilities of the BOARD and specify SIMPLE
711ee4827b2SSean Bruno          * MODE
712ee4827b2SSean Bruno          */
713ee4827b2SSean Bruno         if (ciss_vendor_data[i].flags & CISS_BOARD_SIMPLE)
714ee4827b2SSean Bruno                 supported_methods = CISS_TRANSPORT_METHOD_SIMPLE;
715ee4827b2SSean Bruno         else
71622657ce1SScott Long                 supported_methods = sc->ciss_cfg->supported_methods;
71722657ce1SScott Long         break;
71822657ce1SScott Long     }
71922657ce1SScott Long 
72022657ce1SScott Long setup:
7212fdaa90dSScott Long     if ((supported_methods & CISS_TRANSPORT_METHOD_PERF) != 0) {
72222657ce1SScott Long 	method = CISS_TRANSPORT_METHOD_PERF;
72322657ce1SScott Long 	sc->ciss_perf = (struct ciss_perf_config *)(cbase + cofs +
72422657ce1SScott Long 	    sc->ciss_cfg->transport_offset);
72522657ce1SScott Long 	if (ciss_init_perf(sc)) {
72622657ce1SScott Long 	    supported_methods &= ~method;
72722657ce1SScott Long 	    goto setup;
72822657ce1SScott Long 	}
72922657ce1SScott Long     } else if (supported_methods & CISS_TRANSPORT_METHOD_SIMPLE) {
73022657ce1SScott Long 	method = CISS_TRANSPORT_METHOD_SIMPLE;
73122657ce1SScott Long     } else {
73222657ce1SScott Long 	ciss_printf(sc, "No supported transport methods: 0x%x\n",
73322657ce1SScott Long 	    sc->ciss_cfg->supported_methods);
73422657ce1SScott Long 	return(ENXIO);
73522657ce1SScott Long     }
73622657ce1SScott Long 
73722657ce1SScott Long     /*
73822657ce1SScott Long      * Tell it we're using the low 4GB of RAM.  Set the default interrupt
73922657ce1SScott Long      * coalescing options.
74022657ce1SScott Long      */
74122657ce1SScott Long     sc->ciss_cfg->requested_method = method;
7423a31b7ebSMike Smith     sc->ciss_cfg->command_physlimit = 0;
7433a31b7ebSMike Smith     sc->ciss_cfg->interrupt_coalesce_delay = CISS_INTERRUPT_COALESCE_DELAY;
7443a31b7ebSMike Smith     sc->ciss_cfg->interrupt_coalesce_count = CISS_INTERRUPT_COALESCE_COUNT;
7453a31b7ebSMike Smith 
746a891a3bfSPaul Saab #ifdef __i386__
747a891a3bfSPaul Saab     sc->ciss_cfg->host_driver |= CISS_DRIVER_SCSI_PREFETCH;
748a891a3bfSPaul Saab #endif
749a891a3bfSPaul Saab 
7503a31b7ebSMike Smith     if (ciss_update_config(sc)) {
7513a31b7ebSMike Smith 	ciss_printf(sc, "adapter refuses to accept config update (IDBR 0x%x)\n",
7523a31b7ebSMike Smith 		    CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR));
7533a31b7ebSMike Smith 	return(ENXIO);
7543a31b7ebSMike Smith     }
75522657ce1SScott Long     if ((sc->ciss_cfg->active_method & method) == 0) {
75622657ce1SScott Long 	supported_methods &= ~method;
75722657ce1SScott Long 	if (supported_methods == 0) {
75822657ce1SScott Long 	    ciss_printf(sc, "adapter refuses to go into available transports "
75922657ce1SScott Long 		"mode (0x%x, 0x%x)\n", supported_methods,
76022657ce1SScott Long 		sc->ciss_cfg->active_method);
7613a31b7ebSMike Smith 	    return(ENXIO);
76222657ce1SScott Long 	} else
76322657ce1SScott Long 	    goto setup;
7643a31b7ebSMike Smith     }
7653a31b7ebSMike Smith 
7663a31b7ebSMike Smith     /*
7673a31b7ebSMike Smith      * Wait for the adapter to come ready.
7683a31b7ebSMike Smith      */
7693a31b7ebSMike Smith     if ((error = ciss_wait_adapter(sc)) != 0)
7703a31b7ebSMike Smith 	return(error);
7713a31b7ebSMike Smith 
77222657ce1SScott Long     /* Prepare to possibly use MSIX and/or PERFORMANT interrupts.  Normal
77322657ce1SScott Long      * interrupts have a rid of 0, this will be overridden if MSIX is used.
77422657ce1SScott Long      */
77522657ce1SScott Long     sc->ciss_irq_rid[0] = 0;
77622657ce1SScott Long     if (method == CISS_TRANSPORT_METHOD_PERF) {
77722657ce1SScott Long 	ciss_printf(sc, "PERFORMANT Transport\n");
778d69c9c78SScott Long 	if ((ciss_force_interrupt != 1) && (ciss_setup_msix(sc) == 0)) {
77922657ce1SScott Long 	    intr = ciss_perf_msi_intr;
780d69c9c78SScott Long 	} else {
78122657ce1SScott Long 	    intr = ciss_perf_intr;
782d69c9c78SScott Long 	}
783b23be3e0SScott Long 	/* XXX The docs say that the 0x01 bit is only for SAS controllers.
784b23be3e0SScott Long 	 * Unfortunately, there is no good way to know if this is a SAS
785b23be3e0SScott Long 	 * controller.  Hopefully enabling this bit universally will work OK.
786b23be3e0SScott Long 	 * It seems to work fine for SA6i controllers.
787b23be3e0SScott Long 	 */
788b23be3e0SScott Long 	sc->ciss_interrupt_mask = CISS_TL_PERF_INTR_OPQ | CISS_TL_PERF_INTR_MSI;
789b23be3e0SScott Long 
79022657ce1SScott Long     } else {
79122657ce1SScott Long 	ciss_printf(sc, "SIMPLE Transport\n");
79222657ce1SScott Long 	/* MSIX doesn't seem to work in SIMPLE mode, only enable if it forced */
79322657ce1SScott Long 	if (ciss_force_interrupt == 2)
79422657ce1SScott Long 	    /* If this fails, we automatically revert to INTx */
79522657ce1SScott Long 	    ciss_setup_msix(sc);
79622657ce1SScott Long 	sc->ciss_perf = NULL;
79722657ce1SScott Long 	intr = ciss_intr;
798d69c9c78SScott Long 	sc->ciss_interrupt_mask = sqmask;
79922657ce1SScott Long     }
80022657ce1SScott Long 
8013a31b7ebSMike Smith     /*
8023a31b7ebSMike Smith      * Turn off interrupts before we go routing anything.
8033a31b7ebSMike Smith      */
8043a31b7ebSMike Smith     CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
8053a31b7ebSMike Smith 
8063a31b7ebSMike Smith     /*
8073a31b7ebSMike Smith      * Allocate and set up our interrupt.
8083a31b7ebSMike Smith      */
8093a31b7ebSMike Smith     if ((sc->ciss_irq_resource =
81022657ce1SScott Long 	 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_IRQ, &sc->ciss_irq_rid[0],
8113a31b7ebSMike Smith 				RF_ACTIVE | RF_SHAREABLE)) == NULL) {
8123a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate interrupt\n");
8133a31b7ebSMike Smith 	return(ENXIO);
8143a31b7ebSMike Smith     }
81522657ce1SScott Long 
816cc850363SPeter Wemm     if (bus_setup_intr(sc->ciss_dev, sc->ciss_irq_resource,
81722657ce1SScott Long 		       INTR_TYPE_CAM|INTR_MPSAFE, NULL, intr, sc,
8183a31b7ebSMike Smith 		       &sc->ciss_intr)) {
8193a31b7ebSMike Smith 	ciss_printf(sc, "can't set up interrupt\n");
8203a31b7ebSMike Smith 	return(ENXIO);
8213a31b7ebSMike Smith     }
8223a31b7ebSMike Smith 
8233a31b7ebSMike Smith     /*
8243a31b7ebSMike Smith      * Allocate the parent bus DMA tag appropriate for our PCI
8253a31b7ebSMike Smith      * interface.
8263a31b7ebSMike Smith      *
8273a31b7ebSMike Smith      * Note that "simple" adapters can only address within a 32-bit
8283a31b7ebSMike Smith      * span.
8293a31b7ebSMike Smith      */
830b6f97155SScott Long     if (bus_dma_tag_create(bus_get_dma_tag(sc->ciss_dev),/* PCI parent */
8313a31b7ebSMike Smith 			   1, 0, 			/* alignment, boundary */
832639717c8SPaul Saab 			   BUS_SPACE_MAXADDR,		/* lowaddr */
8333a31b7ebSMike Smith 			   BUS_SPACE_MAXADDR, 		/* highaddr */
8343a31b7ebSMike Smith 			   NULL, NULL, 			/* filter, filterarg */
835639717c8SPaul Saab 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
8362fdaa90dSScott Long 			   CISS_MAX_SG_ELEMENTS,	/* nsegments */
8373a31b7ebSMike Smith 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
83822657ce1SScott Long 			   0,				/* flags */
839f6b1c44dSScott Long 			   NULL, NULL,			/* lockfunc, lockarg */
8403a31b7ebSMike Smith 			   &sc->ciss_parent_dmat)) {
8413a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate parent DMA tag\n");
8423a31b7ebSMike Smith 	return(ENOMEM);
8433a31b7ebSMike Smith     }
8443a31b7ebSMike Smith 
8453a31b7ebSMike Smith     /*
8463a31b7ebSMike Smith      * Create DMA tag for mapping buffers into adapter-addressable
8473a31b7ebSMike Smith      * space.
8483a31b7ebSMike Smith      */
8493a31b7ebSMike Smith     if (bus_dma_tag_create(sc->ciss_parent_dmat, 	/* parent */
8503a31b7ebSMike Smith 			   1, 0, 			/* alignment, boundary */
8513a31b7ebSMike Smith 			   BUS_SPACE_MAXADDR,		/* lowaddr */
8523a31b7ebSMike Smith 			   BUS_SPACE_MAXADDR, 		/* highaddr */
8533a31b7ebSMike Smith 			   NULL, NULL, 			/* filter, filterarg */
8542fdaa90dSScott Long 			   MAXBSIZE, CISS_MAX_SG_ELEMENTS,	/* maxsize, nsegments */
8553a31b7ebSMike Smith 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
85622657ce1SScott Long 			   BUS_DMA_ALLOCNOW,		/* flags */
857975b7318SScott Long 			   busdma_lock_mutex, &sc->ciss_mtx,	/* lockfunc, lockarg */
8583a31b7ebSMike Smith 			   &sc->ciss_buffer_dmat)) {
8593a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate buffer DMA tag\n");
8603a31b7ebSMike Smith 	return(ENOMEM);
8613a31b7ebSMike Smith     }
8623a31b7ebSMike Smith     return(0);
8633a31b7ebSMike Smith }
8643a31b7ebSMike Smith 
8653a31b7ebSMike Smith /************************************************************************
86622657ce1SScott Long  * Setup MSI/MSIX operation (Performant only)
8672fdaa90dSScott Long  * Four interrupts are available, but we only use 1 right now.  If MSI-X
8682fdaa90dSScott Long  * isn't avaialble, try using MSI instead.
86922657ce1SScott Long  */
87022657ce1SScott Long static int
87122657ce1SScott Long ciss_setup_msix(struct ciss_softc *sc)
87222657ce1SScott Long {
87322657ce1SScott Long     int val, i;
87422657ce1SScott Long 
87522657ce1SScott Long     /* Weed out devices that don't actually support MSI */
8762fdaa90dSScott Long     i = ciss_lookup(sc->ciss_dev);
8772fdaa90dSScott Long     if (ciss_vendor_data[i].flags & CISS_BOARD_NOMSI)
87822657ce1SScott Long 	return (EINVAL);
87922657ce1SScott Long 
8802fdaa90dSScott Long     /*
8812fdaa90dSScott Long      * Only need to use the minimum number of MSI vectors, as the driver
8822fdaa90dSScott Long      * doesn't support directed MSIX interrupts.
8832fdaa90dSScott Long      */
88422657ce1SScott Long     val = pci_msix_count(sc->ciss_dev);
8852fdaa90dSScott Long     if (val < CISS_MSI_COUNT) {
8862fdaa90dSScott Long 	val = pci_msi_count(sc->ciss_dev);
8872fdaa90dSScott Long 	device_printf(sc->ciss_dev, "got %d MSI messages]\n", val);
888b23be3e0SScott Long 	if (val < CISS_MSI_COUNT)
889b23be3e0SScott Long 	    return (EINVAL);
8902fdaa90dSScott Long     }
891b23be3e0SScott Long     val = MIN(val, CISS_MSI_COUNT);
8922fdaa90dSScott Long     if (pci_alloc_msix(sc->ciss_dev, &val) != 0) {
8932fdaa90dSScott Long 	if (pci_alloc_msi(sc->ciss_dev, &val) != 0)
89422657ce1SScott Long 	    return (EINVAL);
8952fdaa90dSScott Long     }
89622657ce1SScott Long 
89722657ce1SScott Long     sc->ciss_msi = val;
8982fdaa90dSScott Long     if (bootverbose)
8992fdaa90dSScott Long 	ciss_printf(sc, "Using %d MSIX interrupt%s\n", val,
9002fdaa90dSScott Long 	    (val != 1) ? "s" : "");
90122657ce1SScott Long 
9022fdaa90dSScott Long     for (i = 0; i < val; i++)
90322657ce1SScott Long 	sc->ciss_irq_rid[i] = i + 1;
90422657ce1SScott Long 
90522657ce1SScott Long     return (0);
90622657ce1SScott Long 
90722657ce1SScott Long }
90822657ce1SScott Long 
90922657ce1SScott Long /************************************************************************
91022657ce1SScott Long  * Setup the Performant structures.
91122657ce1SScott Long  */
91222657ce1SScott Long static int
91322657ce1SScott Long ciss_init_perf(struct ciss_softc *sc)
91422657ce1SScott Long {
91522657ce1SScott Long     struct ciss_perf_config *pc = sc->ciss_perf;
91622657ce1SScott Long     int reply_size;
91722657ce1SScott Long 
91822657ce1SScott Long     /*
91922657ce1SScott Long      * Create the DMA tag for the reply queue.
92022657ce1SScott Long      */
92122657ce1SScott Long     reply_size = sizeof(uint64_t) * sc->ciss_max_requests;
92222657ce1SScott Long     if (bus_dma_tag_create(sc->ciss_parent_dmat,	/* parent */
92322657ce1SScott Long 			   1, 0, 			/* alignment, boundary */
92422657ce1SScott Long 			   BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
92522657ce1SScott Long 			   BUS_SPACE_MAXADDR, 		/* highaddr */
92622657ce1SScott Long 			   NULL, NULL, 			/* filter, filterarg */
92722657ce1SScott Long 			   reply_size, 1,		/* maxsize, nsegments */
92822657ce1SScott Long 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
92922657ce1SScott Long 			   0,				/* flags */
93022657ce1SScott Long 			   NULL, NULL,			/* lockfunc, lockarg */
93122657ce1SScott Long 			   &sc->ciss_reply_dmat)) {
93222657ce1SScott Long 	ciss_printf(sc, "can't allocate reply DMA tag\n");
93322657ce1SScott Long 	return(ENOMEM);
93422657ce1SScott Long     }
93522657ce1SScott Long     /*
93622657ce1SScott Long      * Allocate memory and make it available for DMA.
93722657ce1SScott Long      */
93822657ce1SScott Long     if (bus_dmamem_alloc(sc->ciss_reply_dmat, (void **)&sc->ciss_reply,
93922657ce1SScott Long 			 BUS_DMA_NOWAIT, &sc->ciss_reply_map)) {
94022657ce1SScott Long 	ciss_printf(sc, "can't allocate reply memory\n");
94122657ce1SScott Long 	return(ENOMEM);
94222657ce1SScott Long     }
94322657ce1SScott Long     bus_dmamap_load(sc->ciss_reply_dmat, sc->ciss_reply_map, sc->ciss_reply,
94422657ce1SScott Long 		    reply_size, ciss_command_map_helper, &sc->ciss_reply_phys, 0);
94522657ce1SScott Long     bzero(sc->ciss_reply, reply_size);
94622657ce1SScott Long 
94722657ce1SScott Long     sc->ciss_cycle = 0x1;
94822657ce1SScott Long     sc->ciss_rqidx = 0;
94922657ce1SScott Long 
95022657ce1SScott Long     /*
95122657ce1SScott Long      * Preload the fetch table with common command sizes.  This allows the
95222657ce1SScott Long      * hardware to not waste bus cycles for typical i/o commands, but also not
95322657ce1SScott Long      * tax the driver to be too exact in choosing sizes.  The table is optimized
95422657ce1SScott Long      * for page-aligned i/o's, but since most i/o comes from the various pagers,
95522657ce1SScott Long      * it's a reasonable assumption to make.
95622657ce1SScott Long      */
95722657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_NONE] = (sizeof(struct ciss_command) + 15) / 16;
95822657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_1] =
95922657ce1SScott Long 	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 1 + 15) / 16;
96022657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_2] =
96122657ce1SScott Long 	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 2 + 15) / 16;
96222657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_4] =
96322657ce1SScott Long 	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 4 + 15) / 16;
96422657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_8] =
96522657ce1SScott Long 	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 8 + 15) / 16;
96622657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_16] =
96722657ce1SScott Long 	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 16 + 15) / 16;
96822657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_32] =
96922657ce1SScott Long 	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 32 + 15) / 16;
97022657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_MAX] = (CISS_COMMAND_ALLOC_SIZE + 15) / 16;
97122657ce1SScott Long 
97222657ce1SScott Long     pc->rq_size = sc->ciss_max_requests; /* XXX less than the card supports? */
97322657ce1SScott Long     pc->rq_count = 1;	/* XXX Hardcode for a single queue */
97422657ce1SScott Long     pc->rq_bank_hi = 0;
97522657ce1SScott Long     pc->rq_bank_lo = 0;
97622657ce1SScott Long     pc->rq[0].rq_addr_hi = 0x0;
97722657ce1SScott Long     pc->rq[0].rq_addr_lo = sc->ciss_reply_phys;
97822657ce1SScott Long 
97922657ce1SScott Long     return(0);
98022657ce1SScott Long }
98122657ce1SScott Long 
98222657ce1SScott Long /************************************************************************
9833a31b7ebSMike Smith  * Wait for the adapter to come ready.
9843a31b7ebSMike Smith  */
9853a31b7ebSMike Smith static int
9863a31b7ebSMike Smith ciss_wait_adapter(struct ciss_softc *sc)
9873a31b7ebSMike Smith {
9883a31b7ebSMike Smith     int		i;
9893a31b7ebSMike Smith 
9903a31b7ebSMike Smith     debug_called(1);
9913a31b7ebSMike Smith 
9923a31b7ebSMike Smith     /*
9933a31b7ebSMike Smith      * Wait for the adapter to come ready.
9943a31b7ebSMike Smith      */
9953a31b7ebSMike Smith     if (!(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY)) {
9963a31b7ebSMike Smith 	ciss_printf(sc, "waiting for adapter to come ready...\n");
9973a31b7ebSMike Smith 	for (i = 0; !(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY); i++) {
9983a31b7ebSMike Smith 	    DELAY(1000000);	/* one second */
9993a31b7ebSMike Smith 	    if (i > 30) {
10003a31b7ebSMike Smith 		ciss_printf(sc, "timed out waiting for adapter to come ready\n");
10013a31b7ebSMike Smith 		return(EIO);
10023a31b7ebSMike Smith 	    }
10033a31b7ebSMike Smith 	}
10043a31b7ebSMike Smith     }
10053a31b7ebSMike Smith     return(0);
10063a31b7ebSMike Smith }
10073a31b7ebSMike Smith 
10083a31b7ebSMike Smith /************************************************************************
10093a31b7ebSMike Smith  * Flush the adapter cache.
10103a31b7ebSMike Smith  */
10113a31b7ebSMike Smith static int
10123a31b7ebSMike Smith ciss_flush_adapter(struct ciss_softc *sc)
10133a31b7ebSMike Smith {
10143a31b7ebSMike Smith     struct ciss_request			*cr;
10153a31b7ebSMike Smith     struct ciss_bmic_flush_cache	*cbfc;
10163a31b7ebSMike Smith     int					error, command_status;
10173a31b7ebSMike Smith 
10183a31b7ebSMike Smith     debug_called(1);
10193a31b7ebSMike Smith 
10203a31b7ebSMike Smith     cr = NULL;
10213a31b7ebSMike Smith     cbfc = NULL;
10223a31b7ebSMike Smith 
10233a31b7ebSMike Smith     /*
10243a31b7ebSMike Smith      * Build a BMIC request to flush the cache.  We don't disable
10253a31b7ebSMike Smith      * it, as we may be going to do more I/O (eg. we are emulating
10263a31b7ebSMike Smith      * the Synchronise Cache command).
10273a31b7ebSMike Smith      */
10283a31b7ebSMike Smith     if ((cbfc = malloc(sizeof(*cbfc), CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
10293a31b7ebSMike Smith 	error = ENOMEM;
10303a31b7ebSMike Smith 	goto out;
10313a31b7ebSMike Smith     }
10323a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_FLUSH_CACHE,
10333a31b7ebSMike Smith 				       (void **)&cbfc, sizeof(*cbfc))) != 0)
10343a31b7ebSMike Smith 	goto out;
10353a31b7ebSMike Smith 
10363a31b7ebSMike Smith     /*
10373a31b7ebSMike Smith      * Submit the request and wait for it to complete.
10383a31b7ebSMike Smith      */
10393a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
10403a31b7ebSMike Smith 	ciss_printf(sc, "error sending BMIC FLUSH_CACHE command (%d)\n", error);
10413a31b7ebSMike Smith 	goto out;
10423a31b7ebSMike Smith     }
10433a31b7ebSMike Smith 
10443a31b7ebSMike Smith     /*
10453a31b7ebSMike Smith      * Check response.
10463a31b7ebSMike Smith      */
10473a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
10483a31b7ebSMike Smith     switch(command_status) {
10493a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:
10503a31b7ebSMike Smith 	break;
10513a31b7ebSMike Smith     default:
10523a31b7ebSMike Smith 	ciss_printf(sc, "error flushing cache (%s)\n",
10533a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
10543a31b7ebSMike Smith 	error = EIO;
10553a31b7ebSMike Smith 	goto out;
10563a31b7ebSMike Smith     }
10573a31b7ebSMike Smith 
10583a31b7ebSMike Smith out:
10593a31b7ebSMike Smith     if (cbfc != NULL)
10603a31b7ebSMike Smith 	free(cbfc, CISS_MALLOC_CLASS);
10613a31b7ebSMike Smith     if (cr != NULL)
10623a31b7ebSMike Smith 	ciss_release_request(cr);
10633a31b7ebSMike Smith     return(error);
10643a31b7ebSMike Smith }
10653a31b7ebSMike Smith 
106617c0792dSPaul Saab static void
106717c0792dSPaul Saab ciss_soft_reset(struct ciss_softc *sc)
106817c0792dSPaul Saab {
106917c0792dSPaul Saab     struct ciss_request		*cr = NULL;
107017c0792dSPaul Saab     struct ciss_command		*cc;
107117c0792dSPaul Saab     int				i, error = 0;
107217c0792dSPaul Saab 
107317c0792dSPaul Saab     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
107417c0792dSPaul Saab 	/* only reset proxy controllers */
107517c0792dSPaul Saab 	if (sc->ciss_controllers[i].physical.bus == 0)
107617c0792dSPaul Saab 	    continue;
107717c0792dSPaul Saab 
107817c0792dSPaul Saab 	if ((error = ciss_get_request(sc, &cr)) != 0)
107917c0792dSPaul Saab 	    break;
108017c0792dSPaul Saab 
108117c0792dSPaul Saab 	if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_SOFT_RESET,
108217c0792dSPaul Saab 					   NULL, 0)) != 0)
108317c0792dSPaul Saab 	    break;
108417c0792dSPaul Saab 
10852fdaa90dSScott Long 	cc = cr->cr_cc;
108617c0792dSPaul Saab 	cc->header.address = sc->ciss_controllers[i];
108717c0792dSPaul Saab 
108817c0792dSPaul Saab 	if ((error = ciss_synch_request(cr, 60 * 1000)) != 0)
108917c0792dSPaul Saab 	    break;
109017c0792dSPaul Saab 
109117c0792dSPaul Saab 	ciss_release_request(cr);
109217c0792dSPaul Saab     }
109317c0792dSPaul Saab 
109417c0792dSPaul Saab     if (error)
109517c0792dSPaul Saab 	ciss_printf(sc, "error resetting controller (%d)\n", error);
109617c0792dSPaul Saab 
109717c0792dSPaul Saab     if (cr != NULL)
109817c0792dSPaul Saab 	ciss_release_request(cr);
109917c0792dSPaul Saab }
110017c0792dSPaul Saab 
11013a31b7ebSMike Smith /************************************************************************
11023a31b7ebSMike Smith  * Allocate memory for the adapter command structures, initialise
11033a31b7ebSMike Smith  * the request structures.
11043a31b7ebSMike Smith  *
11053a31b7ebSMike Smith  * Note that the entire set of commands are allocated in a single
11063a31b7ebSMike Smith  * contiguous slab.
11073a31b7ebSMike Smith  */
11083a31b7ebSMike Smith static int
11093a31b7ebSMike Smith ciss_init_requests(struct ciss_softc *sc)
11103a31b7ebSMike Smith {
11113a31b7ebSMike Smith     struct ciss_request	*cr;
11123a31b7ebSMike Smith     int			i;
11133a31b7ebSMike Smith 
11143a31b7ebSMike Smith     debug_called(1);
11153a31b7ebSMike Smith 
11164bca277bSPaul Saab     if (bootverbose)
11173a31b7ebSMike Smith 	ciss_printf(sc, "using %d of %d available commands\n",
11183a31b7ebSMike Smith 		    sc->ciss_max_requests, sc->ciss_cfg->max_outstanding_commands);
11193a31b7ebSMike Smith 
11203a31b7ebSMike Smith     /*
11213a31b7ebSMike Smith      * Create the DMA tag for commands.
11223a31b7ebSMike Smith      */
11233a31b7ebSMike Smith     if (bus_dma_tag_create(sc->ciss_parent_dmat,	/* parent */
112422657ce1SScott Long 			   32, 0, 			/* alignment, boundary */
1125639717c8SPaul Saab 			   BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
11263a31b7ebSMike Smith 			   BUS_SPACE_MAXADDR, 		/* highaddr */
11273a31b7ebSMike Smith 			   NULL, NULL, 			/* filter, filterarg */
11283a31b7ebSMike Smith 			   CISS_COMMAND_ALLOC_SIZE *
11293a31b7ebSMike Smith 			   sc->ciss_max_requests, 1,	/* maxsize, nsegments */
11303a31b7ebSMike Smith 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
113122657ce1SScott Long 			   0,				/* flags */
1132639717c8SPaul Saab 			   NULL, NULL,			/* lockfunc, lockarg */
11333a31b7ebSMike Smith 			   &sc->ciss_command_dmat)) {
11343a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate command DMA tag\n");
11353a31b7ebSMike Smith 	return(ENOMEM);
11363a31b7ebSMike Smith     }
11373a31b7ebSMike Smith     /*
11383a31b7ebSMike Smith      * Allocate memory and make it available for DMA.
11393a31b7ebSMike Smith      */
11403a31b7ebSMike Smith     if (bus_dmamem_alloc(sc->ciss_command_dmat, (void **)&sc->ciss_command,
11413a31b7ebSMike Smith 			 BUS_DMA_NOWAIT, &sc->ciss_command_map)) {
11423a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate command memory\n");
11433a31b7ebSMike Smith 	return(ENOMEM);
11443a31b7ebSMike Smith     }
11453a31b7ebSMike Smith     bus_dmamap_load(sc->ciss_command_dmat, sc->ciss_command_map,sc->ciss_command,
1146be241f79SPaul Saab 		    CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests,
114722657ce1SScott Long 		    ciss_command_map_helper, &sc->ciss_command_phys, 0);
11483a31b7ebSMike Smith     bzero(sc->ciss_command, CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests);
11493a31b7ebSMike Smith 
11503a31b7ebSMike Smith     /*
11513a31b7ebSMike Smith      * Set up the request and command structures, push requests onto
11523a31b7ebSMike Smith      * the free queue.
11533a31b7ebSMike Smith      */
11543a31b7ebSMike Smith     for (i = 1; i < sc->ciss_max_requests; i++) {
11553a31b7ebSMike Smith 	cr = &sc->ciss_request[i];
11563a31b7ebSMike Smith 	cr->cr_sc = sc;
11573a31b7ebSMike Smith 	cr->cr_tag = i;
11582fdaa90dSScott Long 	cr->cr_cc = (struct ciss_command *)((uintptr_t)sc->ciss_command +
11592fdaa90dSScott Long 	    CISS_COMMAND_ALLOC_SIZE * i);
11602fdaa90dSScott Long 	cr->cr_ccphys = sc->ciss_command_phys + CISS_COMMAND_ALLOC_SIZE * i;
11613284b9eeSPaul Saab 	bus_dmamap_create(sc->ciss_buffer_dmat, 0, &cr->cr_datamap);
11623a31b7ebSMike Smith 	ciss_enqueue_free(cr);
11633a31b7ebSMike Smith     }
11643a31b7ebSMike Smith     return(0);
11653a31b7ebSMike Smith }
11663a31b7ebSMike Smith 
11673a31b7ebSMike Smith static void
11683a31b7ebSMike Smith ciss_command_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
11693a31b7ebSMike Smith {
117022657ce1SScott Long     uint32_t *addr;
11713a31b7ebSMike Smith 
117222657ce1SScott Long     addr = arg;
117322657ce1SScott Long     *addr = segs[0].ds_addr;
11743a31b7ebSMike Smith }
11753a31b7ebSMike Smith 
11763a31b7ebSMike Smith /************************************************************************
11773a31b7ebSMike Smith  * Identify the adapter, print some information about it.
11783a31b7ebSMike Smith  */
11793a31b7ebSMike Smith static int
11803a31b7ebSMike Smith ciss_identify_adapter(struct ciss_softc *sc)
11813a31b7ebSMike Smith {
11823a31b7ebSMike Smith     struct ciss_request	*cr;
11833a31b7ebSMike Smith     int			error, command_status;
11843a31b7ebSMike Smith 
11853a31b7ebSMike Smith     debug_called(1);
11863a31b7ebSMike Smith 
11873a31b7ebSMike Smith     cr = NULL;
11883a31b7ebSMike Smith 
11893a31b7ebSMike Smith     /*
11903a31b7ebSMike Smith      * Get a request, allocate storage for the adapter data.
11913a31b7ebSMike Smith      */
11923a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_CTLR,
11933a31b7ebSMike Smith 				       (void **)&sc->ciss_id,
11943a31b7ebSMike Smith 				       sizeof(*sc->ciss_id))) != 0)
11953a31b7ebSMike Smith 	goto out;
11963a31b7ebSMike Smith 
11973a31b7ebSMike Smith     /*
11983a31b7ebSMike Smith      * Submit the request and wait for it to complete.
11993a31b7ebSMike Smith      */
12003a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
12013a31b7ebSMike Smith 	ciss_printf(sc, "error sending BMIC ID_CTLR command (%d)\n", error);
12023a31b7ebSMike Smith 	goto out;
12033a31b7ebSMike Smith     }
12043a31b7ebSMike Smith 
12053a31b7ebSMike Smith     /*
12063a31b7ebSMike Smith      * Check response.
12073a31b7ebSMike Smith      */
12083a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
12093a31b7ebSMike Smith     switch(command_status) {
12103a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
12113a31b7ebSMike Smith 	break;
12123a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_UNDERRUN:
12133a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_OVERRUN:
12143a31b7ebSMike Smith 	ciss_printf(sc, "data over/underrun reading adapter information\n");
12153a31b7ebSMike Smith     default:
12163a31b7ebSMike Smith 	ciss_printf(sc, "error reading adapter information (%s)\n",
12173a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
12183a31b7ebSMike Smith 	error = EIO;
12193a31b7ebSMike Smith 	goto out;
12203a31b7ebSMike Smith     }
12213a31b7ebSMike Smith 
12223a31b7ebSMike Smith     /* sanity-check reply */
1223*0fb31ed0SSean Bruno     if (!(sc->ciss_id->controller_flags & CONTROLLER_FLAGS_BIG_MAP_SUPPORT)) {
12243a31b7ebSMike Smith 	ciss_printf(sc, "adapter does not support BIG_MAP\n");
12253a31b7ebSMike Smith 	error = ENXIO;
12263a31b7ebSMike Smith 	goto out;
12273a31b7ebSMike Smith     }
12283a31b7ebSMike Smith 
1229d4aa427fSPaul Saab #if 0
12303a31b7ebSMike Smith     /* XXX later revisions may not need this */
12313a31b7ebSMike Smith     sc->ciss_flags |= CISS_FLAG_FAKE_SYNCH;
1232d4aa427fSPaul Saab #endif
12333a31b7ebSMike Smith 
12343a31b7ebSMike Smith     /* XXX only really required for old 5300 adapters? */
12353a31b7ebSMike Smith     sc->ciss_flags |= CISS_FLAG_BMIC_ABORT;
12363a31b7ebSMike Smith 
1237e3edca22SSean Bruno     /*
1238e3edca22SSean Bruno      * Earlier controller specs do not contain these config
1239e3edca22SSean Bruno      * entries, so assume that a 0 means its old and assign
1240e3edca22SSean Bruno      * these values to the defaults that were established
1241e3edca22SSean Bruno      * when this driver was developed for them
1242e3edca22SSean Bruno      */
1243e3edca22SSean Bruno     if (sc->ciss_cfg->max_logical_supported == 0)
1244e3edca22SSean Bruno         sc->ciss_cfg->max_logical_supported = CISS_MAX_LOGICAL;
1245e3edca22SSean Bruno     if (sc->ciss_cfg->max_physical_supported == 0)
1246e3edca22SSean Bruno 	sc->ciss_cfg->max_physical_supported = CISS_MAX_PHYSICAL;
12473a31b7ebSMike Smith     /* print information */
12484bca277bSPaul Saab     if (bootverbose) {
12493a31b7ebSMike Smith 	ciss_printf(sc, "  %d logical drive%s configured\n",
12503a31b7ebSMike Smith 		    sc->ciss_id->configured_logical_drives,
12513a31b7ebSMike Smith 		    (sc->ciss_id->configured_logical_drives == 1) ? "" : "s");
12523a31b7ebSMike Smith 	ciss_printf(sc, "  firmware %4.4s\n", sc->ciss_id->running_firmware_revision);
1253*0fb31ed0SSean Bruno 	ciss_printf(sc, "  %d SCSI channels\n", sc->ciss_id->scsi_chip_count);
12543a31b7ebSMike Smith 
12553a31b7ebSMike Smith 	ciss_printf(sc, "  signature '%.4s'\n", sc->ciss_cfg->signature);
12563a31b7ebSMike Smith 	ciss_printf(sc, "  valence %d\n", sc->ciss_cfg->valence);
12573a31b7ebSMike Smith 	ciss_printf(sc, "  supported I/O methods 0x%b\n",
12583a31b7ebSMike Smith 		    sc->ciss_cfg->supported_methods,
12593a31b7ebSMike Smith 		    "\20\1READY\2simple\3performant\4MEMQ\n");
12603a31b7ebSMike Smith 	ciss_printf(sc, "  active I/O method 0x%b\n",
12613a31b7ebSMike Smith 		    sc->ciss_cfg->active_method, "\20\2simple\3performant\4MEMQ\n");
12623a31b7ebSMike Smith 	ciss_printf(sc, "  4G page base 0x%08x\n",
12633a31b7ebSMike Smith 		    sc->ciss_cfg->command_physlimit);
12643a31b7ebSMike Smith 	ciss_printf(sc, "  interrupt coalesce delay %dus\n",
12653a31b7ebSMike Smith 		    sc->ciss_cfg->interrupt_coalesce_delay);
12663a31b7ebSMike Smith 	ciss_printf(sc, "  interrupt coalesce count %d\n",
12673a31b7ebSMike Smith 		    sc->ciss_cfg->interrupt_coalesce_count);
12683a31b7ebSMike Smith 	ciss_printf(sc, "  max outstanding commands %d\n",
12693a31b7ebSMike Smith 		    sc->ciss_cfg->max_outstanding_commands);
12703a31b7ebSMike Smith 	ciss_printf(sc, "  bus types 0x%b\n", sc->ciss_cfg->bus_types,
12713a31b7ebSMike Smith 		    "\20\1ultra2\2ultra3\10fibre1\11fibre2\n");
12723a31b7ebSMike Smith 	ciss_printf(sc, "  server name '%.16s'\n", sc->ciss_cfg->server_name);
12733a31b7ebSMike Smith 	ciss_printf(sc, "  heartbeat 0x%x\n", sc->ciss_cfg->heartbeat);
1274e3edca22SSean Bruno     	ciss_printf(sc, "  max logical logical volumes: %d\n", sc->ciss_cfg->max_logical_supported);
1275e3edca22SSean Bruno     	ciss_printf(sc, "  max physical disks supported: %d\n", sc->ciss_cfg->max_physical_supported);
1276e3edca22SSean Bruno     	ciss_printf(sc, "  max physical disks per logical volume: %d\n", sc->ciss_cfg->max_physical_per_logical);
1277*0fb31ed0SSean Bruno 	ciss_printf(sc, "  JBOD Support is %s\n", (sc->ciss_id->uiYetMoreControllerFlags & YMORE_CONTROLLER_FLAGS_JBOD_SUPPORTED) ?
1278*0fb31ed0SSean Bruno 			"Available" : "Unavailable");
1279*0fb31ed0SSean Bruno 	ciss_printf(sc, "  JBOD Mode is %s\n", (sc->ciss_id->PowerUPNvramFlags & PWR_UP_FLAG_JBOD_ENABLED) ?
1280*0fb31ed0SSean Bruno 			"Enabled" : "Disabled");
12813a31b7ebSMike Smith     }
12823a31b7ebSMike Smith 
12833a31b7ebSMike Smith out:
12843a31b7ebSMike Smith     if (error) {
12853a31b7ebSMike Smith 	if (sc->ciss_id != NULL) {
12863a31b7ebSMike Smith 	    free(sc->ciss_id, CISS_MALLOC_CLASS);
12873a31b7ebSMike Smith 	    sc->ciss_id = NULL;
12883a31b7ebSMike Smith 	}
12893a31b7ebSMike Smith     }
12903a31b7ebSMike Smith     if (cr != NULL)
12913a31b7ebSMike Smith 	ciss_release_request(cr);
12923a31b7ebSMike Smith     return(error);
12933a31b7ebSMike Smith }
12943a31b7ebSMike Smith 
12953a31b7ebSMike Smith /************************************************************************
1296c6131460SPaul Saab  * Helper routine for generating a list of logical and physical luns.
12973a31b7ebSMike Smith  */
1298c6131460SPaul Saab static struct ciss_lun_report *
1299c6131460SPaul Saab ciss_report_luns(struct ciss_softc *sc, int opcode, int nunits)
13003a31b7ebSMike Smith {
13013a31b7ebSMike Smith     struct ciss_request		*cr;
13023a31b7ebSMike Smith     struct ciss_command		*cc;
13033a31b7ebSMike Smith     struct ciss_report_cdb	*crc;
13043a31b7ebSMike Smith     struct ciss_lun_report	*cll;
13053a31b7ebSMike Smith     int				command_status;
1306c6131460SPaul Saab     int				report_size;
1307c6131460SPaul Saab     int				error = 0;
13083a31b7ebSMike Smith 
13093a31b7ebSMike Smith     debug_called(1);
13103a31b7ebSMike Smith 
13113a31b7ebSMike Smith     cr = NULL;
13123a31b7ebSMike Smith     cll = NULL;
13133a31b7ebSMike Smith 
13143a31b7ebSMike Smith     /*
13153a31b7ebSMike Smith      * Get a request, allocate storage for the address list.
13163a31b7ebSMike Smith      */
13173a31b7ebSMike Smith     if ((error = ciss_get_request(sc, &cr)) != 0)
13183a31b7ebSMike Smith 	goto out;
1319c6131460SPaul Saab     report_size = sizeof(*cll) + nunits * sizeof(union ciss_device_address);
13203a31b7ebSMike Smith     if ((cll = malloc(report_size, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
1321c6131460SPaul Saab 	ciss_printf(sc, "can't allocate memory for lun report\n");
13223a31b7ebSMike Smith 	error = ENOMEM;
13233a31b7ebSMike Smith 	goto out;
13243a31b7ebSMike Smith     }
13253a31b7ebSMike Smith 
13263a31b7ebSMike Smith     /*
1327c6131460SPaul Saab      * Build the Report Logical/Physical LUNs command.
13283a31b7ebSMike Smith      */
13292fdaa90dSScott Long     cc = cr->cr_cc;
13303a31b7ebSMike Smith     cr->cr_data = cll;
13313a31b7ebSMike Smith     cr->cr_length = report_size;
13323a31b7ebSMike Smith     cr->cr_flags = CISS_REQ_DATAIN;
13333a31b7ebSMike Smith 
13343a31b7ebSMike Smith     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
13353a31b7ebSMike Smith     cc->header.address.physical.bus = 0;
13363a31b7ebSMike Smith     cc->header.address.physical.target = 0;
13373a31b7ebSMike Smith     cc->cdb.cdb_length = sizeof(*crc);
13383a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
13393a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
13403a31b7ebSMike Smith     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
13413a31b7ebSMike Smith     cc->cdb.timeout = 30;	/* XXX better suggestions? */
13423a31b7ebSMike Smith 
13433a31b7ebSMike Smith     crc = (struct ciss_report_cdb *)&(cc->cdb.cdb[0]);
13443a31b7ebSMike Smith     bzero(crc, sizeof(*crc));
1345c6131460SPaul Saab     crc->opcode = opcode;
13463a31b7ebSMike Smith     crc->length = htonl(report_size);			/* big-endian field */
13473a31b7ebSMike Smith     cll->list_size = htonl(report_size - sizeof(*cll));	/* big-endian field */
13483a31b7ebSMike Smith 
13493a31b7ebSMike Smith     /*
13503a31b7ebSMike Smith      * Submit the request and wait for it to complete.  (timeout
13513a31b7ebSMike Smith      * here should be much greater than above)
13523a31b7ebSMike Smith      */
13533a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1354c6131460SPaul Saab 	ciss_printf(sc, "error sending %d LUN command (%d)\n", opcode, error);
13553a31b7ebSMike Smith 	goto out;
13563a31b7ebSMike Smith     }
13573a31b7ebSMike Smith 
13583a31b7ebSMike Smith     /*
13593a31b7ebSMike Smith      * Check response.  Note that data over/underrun is OK.
13603a31b7ebSMike Smith      */
13613a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
13623a31b7ebSMike Smith     switch(command_status) {
13633a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:	/* buffer right size */
13643a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_UNDERRUN:	/* buffer too large, not bad */
13653a31b7ebSMike Smith 	break;
13663a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_OVERRUN:
1367c6131460SPaul Saab 	ciss_printf(sc, "WARNING: more units than driver limit (%d)\n",
1368e3edca22SSean Bruno 		    sc->ciss_cfg->max_logical_supported);
13693a31b7ebSMike Smith 	break;
13703a31b7ebSMike Smith     default:
13713a31b7ebSMike Smith 	ciss_printf(sc, "error detecting logical drive configuration (%s)\n",
13723a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
13733a31b7ebSMike Smith 	error = EIO;
13743a31b7ebSMike Smith 	goto out;
13753a31b7ebSMike Smith     }
13763a31b7ebSMike Smith     ciss_release_request(cr);
13773a31b7ebSMike Smith     cr = NULL;
13783a31b7ebSMike Smith 
1379c6131460SPaul Saab out:
1380c6131460SPaul Saab     if (cr != NULL)
1381c6131460SPaul Saab 	ciss_release_request(cr);
1382c6131460SPaul Saab     if (error && cll != NULL) {
1383c6131460SPaul Saab 	free(cll, CISS_MALLOC_CLASS);
1384c6131460SPaul Saab 	cll = NULL;
1385c6131460SPaul Saab     }
1386c6131460SPaul Saab     return(cll);
1387c6131460SPaul Saab }
1388c6131460SPaul Saab 
1389c6131460SPaul Saab /************************************************************************
1390c6131460SPaul Saab  * Find logical drives on the adapter.
1391c6131460SPaul Saab  */
1392c6131460SPaul Saab static int
1393c6131460SPaul Saab ciss_init_logical(struct ciss_softc *sc)
1394c6131460SPaul Saab {
1395c6131460SPaul Saab     struct ciss_lun_report	*cll;
1396c6131460SPaul Saab     int				error = 0, i, j;
1397c6131460SPaul Saab     int				ndrives;
1398c6131460SPaul Saab 
1399c6131460SPaul Saab     debug_called(1);
1400c6131460SPaul Saab 
1401c6131460SPaul Saab     cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
1402e3edca22SSean Bruno 			   sc->ciss_cfg->max_logical_supported);
1403c6131460SPaul Saab     if (cll == NULL) {
1404c6131460SPaul Saab 	error = ENXIO;
1405c6131460SPaul Saab 	goto out;
1406c6131460SPaul Saab     }
1407c6131460SPaul Saab 
14083a31b7ebSMike Smith     /* sanity-check reply */
14093a31b7ebSMike Smith     ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1410e3edca22SSean Bruno     if ((ndrives < 0) || (ndrives > sc->ciss_cfg->max_logical_supported)) {
14113a31b7ebSMike Smith 	ciss_printf(sc, "adapter claims to report absurd number of logical drives (%d > %d)\n",
1412e3edca22SSean Bruno 	    	ndrives, sc->ciss_cfg->max_logical_supported);
1413c6131460SPaul Saab 	error = ENXIO;
1414c6131460SPaul Saab 	goto out;
14153a31b7ebSMike Smith     }
14163a31b7ebSMike Smith 
14173a31b7ebSMike Smith     /*
14183a31b7ebSMike Smith      * Save logical drive information.
14193a31b7ebSMike Smith      */
1420c6131460SPaul Saab     if (bootverbose) {
1421c6131460SPaul Saab 	ciss_printf(sc, "%d logical drive%s\n",
1422c6131460SPaul Saab 	    ndrives, (ndrives > 1 || ndrives == 0) ? "s" : "");
1423c6131460SPaul Saab     }
1424c6131460SPaul Saab 
1425c6131460SPaul Saab     sc->ciss_logical =
14261fe6c4eeSScott Long 	malloc(sc->ciss_max_logical_bus * sizeof(struct ciss_ldrive *),
1427c6131460SPaul Saab 	       CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1428c6131460SPaul Saab     if (sc->ciss_logical == NULL) {
1429c6131460SPaul Saab 	error = ENXIO;
1430c6131460SPaul Saab 	goto out;
1431c6131460SPaul Saab     }
1432c6131460SPaul Saab 
14331fe6c4eeSScott Long     for (i = 0; i <= sc->ciss_max_logical_bus; i++) {
1434c6131460SPaul Saab 	sc->ciss_logical[i] =
1435e3edca22SSean Bruno 	    malloc(sc->ciss_cfg->max_logical_supported *
1436e3edca22SSean Bruno 		   sizeof(struct ciss_ldrive),
1437c6131460SPaul Saab 		   CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1438c6131460SPaul Saab 	if (sc->ciss_logical[i] == NULL) {
1439c6131460SPaul Saab 	    error = ENXIO;
1440c6131460SPaul Saab 	    goto out;
1441c6131460SPaul Saab 	}
1442c6131460SPaul Saab 
1443e3edca22SSean Bruno 	for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++)
1444c6131460SPaul Saab 	    sc->ciss_logical[i][j].cl_status = CISS_LD_NONEXISTENT;
1445c6131460SPaul Saab     }
1446c6131460SPaul Saab 
1447c6131460SPaul Saab 
1448e3edca22SSean Bruno     for (i = 0; i < sc->ciss_cfg->max_logical_supported; i++) {
14493a31b7ebSMike Smith 	if (i < ndrives) {
1450c6131460SPaul Saab 	    struct ciss_ldrive	*ld;
1451c6131460SPaul Saab 	    int			bus, target;
1452c6131460SPaul Saab 
1453c6131460SPaul Saab 	    bus		= CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
1454c6131460SPaul Saab 	    target	= CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
1455c6131460SPaul Saab 	    ld		= &sc->ciss_logical[bus][target];
1456c6131460SPaul Saab 
1457c6131460SPaul Saab 	    ld->cl_address	= cll->lun[i];
1458c6131460SPaul Saab 	    ld->cl_controller	= &sc->ciss_controllers[bus];
1459c6131460SPaul Saab 	    if (ciss_identify_logical(sc, ld) != 0)
14603a31b7ebSMike Smith 		continue;
14613a31b7ebSMike Smith 	    /*
14623a31b7ebSMike Smith 	     * If the drive has had media exchanged, we should bring it online.
14633a31b7ebSMike Smith 	     */
1464c6131460SPaul Saab 	    if (ld->cl_lstatus->media_exchanged)
146588c78a38SPaul Saab 		ciss_accept_media(sc, ld);
14663a31b7ebSMike Smith 
14673a31b7ebSMike Smith 	}
14683a31b7ebSMike Smith     }
14693a31b7ebSMike Smith 
14703a31b7ebSMike Smith  out:
14713a31b7ebSMike Smith     if (cll != NULL)
14723a31b7ebSMike Smith 	free(cll, CISS_MALLOC_CLASS);
14733a31b7ebSMike Smith     return(error);
14743a31b7ebSMike Smith }
14753a31b7ebSMike Smith 
1476440baa08SPaul Saab static int
1477c6131460SPaul Saab ciss_init_physical(struct ciss_softc *sc)
1478c6131460SPaul Saab {
1479c6131460SPaul Saab     struct ciss_lun_report	*cll;
1480c6131460SPaul Saab     int				error = 0, i;
1481c6131460SPaul Saab     int				nphys;
14821fe6c4eeSScott Long     int				bus, target;
1483c6131460SPaul Saab 
1484c6131460SPaul Saab     debug_called(1);
1485c6131460SPaul Saab 
14861fe6c4eeSScott Long     bus = 0;
14871fe6c4eeSScott Long     target = 0;
14881fe6c4eeSScott Long 
1489c6131460SPaul Saab     cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
1490e3edca22SSean Bruno 			   sc->ciss_cfg->max_physical_supported);
1491c6131460SPaul Saab     if (cll == NULL) {
1492c6131460SPaul Saab 	error = ENXIO;
1493c6131460SPaul Saab 	goto out;
1494c6131460SPaul Saab     }
1495c6131460SPaul Saab 
1496c6131460SPaul Saab     nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1497c6131460SPaul Saab 
1498c6131460SPaul Saab     if (bootverbose) {
1499c6131460SPaul Saab 	ciss_printf(sc, "%d physical device%s\n",
1500c6131460SPaul Saab 	    nphys, (nphys > 1 || nphys == 0) ? "s" : "");
1501c6131460SPaul Saab     }
1502c6131460SPaul Saab 
1503c6131460SPaul Saab     /*
15041fe6c4eeSScott Long      * Figure out the bus mapping.
15051fe6c4eeSScott Long      * Logical buses include both the local logical bus for local arrays and
15061fe6c4eeSScott Long      * proxy buses for remote arrays.  Physical buses are numbered by the
15071fe6c4eeSScott Long      * controller and represent physical buses that hold physical devices.
15081fe6c4eeSScott Long      * We shift these bus numbers so that everything fits into a single flat
15091fe6c4eeSScott Long      * numbering space for CAM.  Logical buses occupy the first 32 CAM bus
15101fe6c4eeSScott Long      * numbers, and the physical bus numbers are shifted to be above that.
15111fe6c4eeSScott Long      * This results in the various driver arrays being indexed as follows:
15121fe6c4eeSScott Long      *
15131fe6c4eeSScott Long      * ciss_controllers[] - indexed by logical bus
15141fe6c4eeSScott Long      * ciss_cam_sim[]     - indexed by both logical and physical, with physical
15151fe6c4eeSScott Long      *                      being shifted by 32.
15161fe6c4eeSScott Long      * ciss_logical[][]   - indexed by logical bus
15171fe6c4eeSScott Long      * ciss_physical[][]  - indexed by physical bus
15181fe6c4eeSScott Long      *
15191fe6c4eeSScott Long      * XXX This is getting more and more hackish.  CISS really doesn't play
15201fe6c4eeSScott Long      *     well with a standard SCSI model; devices are addressed via magic
15211fe6c4eeSScott Long      *     cookies, not via b/t/l addresses.  Since there is no way to store
15221fe6c4eeSScott Long      *     the cookie in the CAM device object, we have to keep these lookup
15231fe6c4eeSScott Long      *     tables handy so that the devices can be found quickly at the cost
15241fe6c4eeSScott Long      *     of wasting memory and having a convoluted lookup scheme.  This
15251fe6c4eeSScott Long      *     driver should probably be converted to block interface.
15261fe6c4eeSScott Long      */
15271fe6c4eeSScott Long     /*
1528c6131460SPaul Saab      * If the L2 and L3 SCSI addresses are 0, this signifies a proxy
1529c6131460SPaul Saab      * controller. A proxy controller is another physical controller
1530c6131460SPaul Saab      * behind the primary PCI controller. We need to know about this
1531c6131460SPaul Saab      * so that BMIC commands can be properly targeted.  There can be
1532c6131460SPaul Saab      * proxy controllers attached to a single PCI controller, so
1533c6131460SPaul Saab      * find the highest numbered one so the array can be properly
1534c6131460SPaul Saab      * sized.
1535c6131460SPaul Saab      */
15361fe6c4eeSScott Long     sc->ciss_max_logical_bus = 1;
1537c6131460SPaul Saab     for (i = 0; i < nphys; i++) {
1538c6131460SPaul Saab 	if (cll->lun[i].physical.extra_address == 0) {
15391fe6c4eeSScott Long 	    bus = cll->lun[i].physical.bus;
15401fe6c4eeSScott Long 	    sc->ciss_max_logical_bus = max(sc->ciss_max_logical_bus, bus) + 1;
15411fe6c4eeSScott Long 	} else {
15421fe6c4eeSScott Long 	    bus = CISS_EXTRA_BUS2(cll->lun[i].physical.extra_address);
15431fe6c4eeSScott Long 	    sc->ciss_max_physical_bus = max(sc->ciss_max_physical_bus, bus);
1544c6131460SPaul Saab 	}
1545c6131460SPaul Saab     }
1546c6131460SPaul Saab 
1547c6131460SPaul Saab     sc->ciss_controllers =
15481fe6c4eeSScott Long 	malloc(sc->ciss_max_logical_bus * sizeof (union ciss_device_address),
1549c6131460SPaul Saab 	       CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1550c6131460SPaul Saab 
1551c6131460SPaul Saab     if (sc->ciss_controllers == NULL) {
1552c6131460SPaul Saab 	ciss_printf(sc, "Could not allocate memory for controller map\n");
1553c6131460SPaul Saab 	error = ENOMEM;
1554c6131460SPaul Saab 	goto out;
1555c6131460SPaul Saab     }
1556c6131460SPaul Saab 
1557c6131460SPaul Saab     /* setup a map of controller addresses */
1558c6131460SPaul Saab     for (i = 0; i < nphys; i++) {
1559c6131460SPaul Saab 	if (cll->lun[i].physical.extra_address == 0) {
1560c6131460SPaul Saab 	    sc->ciss_controllers[cll->lun[i].physical.bus] = cll->lun[i];
1561c6131460SPaul Saab 	}
1562c6131460SPaul Saab     }
1563c6131460SPaul Saab 
15641fe6c4eeSScott Long     sc->ciss_physical =
15651fe6c4eeSScott Long 	malloc(sc->ciss_max_physical_bus * sizeof(struct ciss_pdrive *),
15661fe6c4eeSScott Long 	       CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
15671fe6c4eeSScott Long     if (sc->ciss_physical == NULL) {
15681fe6c4eeSScott Long 	ciss_printf(sc, "Could not allocate memory for physical device map\n");
15691fe6c4eeSScott Long 	error = ENOMEM;
15701fe6c4eeSScott Long 	goto out;
15711fe6c4eeSScott Long     }
15721fe6c4eeSScott Long 
15731fe6c4eeSScott Long     for (i = 0; i < sc->ciss_max_physical_bus; i++) {
15741fe6c4eeSScott Long 	sc->ciss_physical[i] =
15751fe6c4eeSScott Long 	    malloc(sizeof(struct ciss_pdrive) * CISS_MAX_PHYSTGT,
15761fe6c4eeSScott Long 		   CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
15771fe6c4eeSScott Long 	if (sc->ciss_physical[i] == NULL) {
15781fe6c4eeSScott Long 	    ciss_printf(sc, "Could not allocate memory for target map\n");
15791fe6c4eeSScott Long 	    error = ENOMEM;
15801fe6c4eeSScott Long 	    goto out;
15811fe6c4eeSScott Long 	}
15821fe6c4eeSScott Long     }
15831fe6c4eeSScott Long 
15841fe6c4eeSScott Long     ciss_filter_physical(sc, cll);
15851fe6c4eeSScott Long 
1586c6131460SPaul Saab out:
1587c6131460SPaul Saab     if (cll != NULL)
1588c6131460SPaul Saab 	free(cll, CISS_MALLOC_CLASS);
1589c6131460SPaul Saab 
1590c6131460SPaul Saab     return(error);
1591c6131460SPaul Saab }
1592c6131460SPaul Saab 
1593c6131460SPaul Saab static int
15941fe6c4eeSScott Long ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll)
15951fe6c4eeSScott Long {
15961fe6c4eeSScott Long     u_int32_t ea;
15971fe6c4eeSScott Long     int i, nphys;
15981fe6c4eeSScott Long     int	bus, target;
15991fe6c4eeSScott Long 
16001fe6c4eeSScott Long     nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
16011fe6c4eeSScott Long     for (i = 0; i < nphys; i++) {
16021fe6c4eeSScott Long 	if (cll->lun[i].physical.extra_address == 0)
16031fe6c4eeSScott Long 	    continue;
16041fe6c4eeSScott Long 
16051fe6c4eeSScott Long 	/*
16061fe6c4eeSScott Long 	 * Filter out devices that we don't want.  Level 3 LUNs could
16071fe6c4eeSScott Long 	 * probably be supported, but the docs don't give enough of a
16081fe6c4eeSScott Long 	 * hint to know how.
16091fe6c4eeSScott Long 	 *
16101fe6c4eeSScott Long 	 * The mode field of the physical address is likely set to have
16111fe6c4eeSScott Long 	 * hard disks masked out.  Honor it unless the user has overridden
16121fe6c4eeSScott Long 	 * us with the tunable.  We also munge the inquiry data for these
16131fe6c4eeSScott Long 	 * disks so that they only show up as passthrough devices.  Keeping
16141fe6c4eeSScott Long 	 * them visible in this fashion is useful for doing things like
16151fe6c4eeSScott Long 	 * flashing firmware.
16161fe6c4eeSScott Long 	 */
16171fe6c4eeSScott Long 	ea = cll->lun[i].physical.extra_address;
16181fe6c4eeSScott Long 	if ((CISS_EXTRA_BUS3(ea) != 0) || (CISS_EXTRA_TARGET3(ea) != 0) ||
16191fe6c4eeSScott Long 	    (CISS_EXTRA_MODE2(ea) == 0x3))
16201fe6c4eeSScott Long 	    continue;
16211fe6c4eeSScott Long 	if ((ciss_expose_hidden_physical == 0) &&
16221fe6c4eeSScott Long 	   (cll->lun[i].physical.mode == CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL))
16231fe6c4eeSScott Long 	    continue;
16241fe6c4eeSScott Long 
16251fe6c4eeSScott Long 	/*
16261fe6c4eeSScott Long 	 * Note: CISS firmware numbers physical busses starting at '1', not
16271fe6c4eeSScott Long 	 *       '0'.  This numbering is internal to the firmware and is only
16281fe6c4eeSScott Long 	 *       used as a hint here.
16291fe6c4eeSScott Long 	 */
16301fe6c4eeSScott Long 	bus = CISS_EXTRA_BUS2(ea) - 1;
16311fe6c4eeSScott Long 	target = CISS_EXTRA_TARGET2(ea);
16321fe6c4eeSScott Long 	sc->ciss_physical[bus][target].cp_address = cll->lun[i];
16331fe6c4eeSScott Long 	sc->ciss_physical[bus][target].cp_online = 1;
16341fe6c4eeSScott Long     }
16351fe6c4eeSScott Long 
16361fe6c4eeSScott Long     return (0);
16371fe6c4eeSScott Long }
16381fe6c4eeSScott Long 
16391fe6c4eeSScott Long static int
1640440baa08SPaul Saab ciss_inquiry_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1641440baa08SPaul Saab {
1642440baa08SPaul Saab     struct ciss_request			*cr;
1643440baa08SPaul Saab     struct ciss_command			*cc;
1644440baa08SPaul Saab     struct scsi_inquiry			*inq;
1645440baa08SPaul Saab     int					error;
1646440baa08SPaul Saab     int					command_status;
1647440baa08SPaul Saab 
1648440baa08SPaul Saab     cr = NULL;
1649440baa08SPaul Saab 
1650440baa08SPaul Saab     bzero(&ld->cl_geometry, sizeof(ld->cl_geometry));
1651440baa08SPaul Saab 
1652440baa08SPaul Saab     if ((error = ciss_get_request(sc, &cr)) != 0)
1653440baa08SPaul Saab 	goto out;
1654440baa08SPaul Saab 
16552fdaa90dSScott Long     cc = cr->cr_cc;
1656440baa08SPaul Saab     cr->cr_data = &ld->cl_geometry;
1657440baa08SPaul Saab     cr->cr_length = sizeof(ld->cl_geometry);
1658440baa08SPaul Saab     cr->cr_flags = CISS_REQ_DATAIN;
1659440baa08SPaul Saab 
1660c6131460SPaul Saab     cc->header.address = ld->cl_address;
1661440baa08SPaul Saab     cc->cdb.cdb_length = 6;
1662440baa08SPaul Saab     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1663440baa08SPaul Saab     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1664440baa08SPaul Saab     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1665440baa08SPaul Saab     cc->cdb.timeout = 30;
1666440baa08SPaul Saab 
1667440baa08SPaul Saab     inq = (struct scsi_inquiry *)&(cc->cdb.cdb[0]);
1668440baa08SPaul Saab     inq->opcode = INQUIRY;
1669440baa08SPaul Saab     inq->byte2 = SI_EVPD;
1670440baa08SPaul Saab     inq->page_code = CISS_VPD_LOGICAL_DRIVE_GEOMETRY;
1671130f4520SKenneth D. Merry     scsi_ulto2b(sizeof(ld->cl_geometry), inq->length);
1672440baa08SPaul Saab 
1673440baa08SPaul Saab     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1674440baa08SPaul Saab 	ciss_printf(sc, "error getting geometry (%d)\n", error);
1675440baa08SPaul Saab 	goto out;
1676440baa08SPaul Saab     }
1677440baa08SPaul Saab 
1678440baa08SPaul Saab     ciss_report_request(cr, &command_status, NULL);
1679440baa08SPaul Saab     switch(command_status) {
1680440baa08SPaul Saab     case CISS_CMD_STATUS_SUCCESS:
1681440baa08SPaul Saab     case CISS_CMD_STATUS_DATA_UNDERRUN:
1682440baa08SPaul Saab 	break;
1683440baa08SPaul Saab     case CISS_CMD_STATUS_DATA_OVERRUN:
1684440baa08SPaul Saab 	ciss_printf(sc, "WARNING: Data overrun\n");
1685440baa08SPaul Saab 	break;
1686440baa08SPaul Saab     default:
1687440baa08SPaul Saab 	ciss_printf(sc, "Error detecting logical drive geometry (%s)\n",
1688440baa08SPaul Saab 		    ciss_name_command_status(command_status));
1689440baa08SPaul Saab 	break;
1690440baa08SPaul Saab     }
1691440baa08SPaul Saab 
1692440baa08SPaul Saab out:
1693440baa08SPaul Saab     if (cr != NULL)
1694440baa08SPaul Saab 	ciss_release_request(cr);
1695440baa08SPaul Saab     return(error);
1696440baa08SPaul Saab }
16973a31b7ebSMike Smith /************************************************************************
16983a31b7ebSMike Smith  * Identify a logical drive, initialise state related to it.
16993a31b7ebSMike Smith  */
17003a31b7ebSMike Smith static int
17013a31b7ebSMike Smith ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
17023a31b7ebSMike Smith {
17033a31b7ebSMike Smith     struct ciss_request		*cr;
17043a31b7ebSMike Smith     struct ciss_command		*cc;
17053a31b7ebSMike Smith     struct ciss_bmic_cdb	*cbc;
17063a31b7ebSMike Smith     int				error, command_status;
17073a31b7ebSMike Smith 
17083a31b7ebSMike Smith     debug_called(1);
17093a31b7ebSMike Smith 
17103a31b7ebSMike Smith     cr = NULL;
17113a31b7ebSMike Smith 
17123a31b7ebSMike Smith     /*
17133a31b7ebSMike Smith      * Build a BMIC request to fetch the drive ID.
17143a31b7ebSMike Smith      */
17153a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LDRIVE,
17163a31b7ebSMike Smith 				       (void **)&ld->cl_ldrive,
17173a31b7ebSMike Smith 				       sizeof(*ld->cl_ldrive))) != 0)
17183a31b7ebSMike Smith 	goto out;
17192fdaa90dSScott Long     cc = cr->cr_cc;
1720c6131460SPaul Saab     cc->header.address = *ld->cl_controller;	/* target controller */
17213a31b7ebSMike Smith     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1722c6131460SPaul Saab     cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
17233a31b7ebSMike Smith 
17243a31b7ebSMike Smith     /*
17253a31b7ebSMike Smith      * Submit the request and wait for it to complete.
17263a31b7ebSMike Smith      */
17273a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
17283a31b7ebSMike Smith 	ciss_printf(sc, "error sending BMIC LDRIVE command (%d)\n", error);
17293a31b7ebSMike Smith 	goto out;
17303a31b7ebSMike Smith     }
17313a31b7ebSMike Smith 
17323a31b7ebSMike Smith     /*
17333a31b7ebSMike Smith      * Check response.
17343a31b7ebSMike Smith      */
17353a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
17363a31b7ebSMike Smith     switch(command_status) {
17373a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
17383a31b7ebSMike Smith 	break;
17393a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_UNDERRUN:
17403a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_OVERRUN:
17413a31b7ebSMike Smith 	ciss_printf(sc, "data over/underrun reading logical drive ID\n");
17423a31b7ebSMike Smith     default:
17433a31b7ebSMike Smith 	ciss_printf(sc, "error reading logical drive ID (%s)\n",
17443a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
17453a31b7ebSMike Smith 	error = EIO;
17463a31b7ebSMike Smith 	goto out;
17473a31b7ebSMike Smith     }
17483a31b7ebSMike Smith     ciss_release_request(cr);
17493a31b7ebSMike Smith     cr = NULL;
17503a31b7ebSMike Smith 
17513a31b7ebSMike Smith     /*
17523a31b7ebSMike Smith      * Build a CISS BMIC command to get the logical drive status.
17533a31b7ebSMike Smith      */
17543a31b7ebSMike Smith     if ((error = ciss_get_ldrive_status(sc, ld)) != 0)
17553a31b7ebSMike Smith 	goto out;
17563a31b7ebSMike Smith 
17573a31b7ebSMike Smith     /*
1758440baa08SPaul Saab      * Get the logical drive geometry.
1759440baa08SPaul Saab      */
1760440baa08SPaul Saab     if ((error = ciss_inquiry_logical(sc, ld)) != 0)
1761440baa08SPaul Saab 	goto out;
1762440baa08SPaul Saab 
1763440baa08SPaul Saab     /*
17643a31b7ebSMike Smith      * Print the drive's basic characteristics.
17653a31b7ebSMike Smith      */
17664bca277bSPaul Saab     if (bootverbose) {
1767c6131460SPaul Saab 	ciss_printf(sc, "logical drive (b%dt%d): %s, %dMB ",
1768c6131460SPaul Saab 		    CISS_LUN_TO_BUS(ld->cl_address.logical.lun),
1769c6131460SPaul Saab 		    CISS_LUN_TO_TARGET(ld->cl_address.logical.lun),
177079adbf76SPaul Saab 		    ciss_name_ldrive_org(ld->cl_ldrive->fault_tolerance),
17713a31b7ebSMike Smith 		    ((ld->cl_ldrive->blocks_available / (1024 * 1024)) *
17723a31b7ebSMike Smith 		     ld->cl_ldrive->block_size));
17733a31b7ebSMike Smith 
17743a31b7ebSMike Smith 	ciss_print_ldrive(sc, ld);
17753a31b7ebSMike Smith     }
17763a31b7ebSMike Smith out:
17773a31b7ebSMike Smith     if (error != 0) {
17783a31b7ebSMike Smith 	/* make the drive not-exist */
17793a31b7ebSMike Smith 	ld->cl_status = CISS_LD_NONEXISTENT;
17803a31b7ebSMike Smith 	if (ld->cl_ldrive != NULL) {
17813a31b7ebSMike Smith 	    free(ld->cl_ldrive, CISS_MALLOC_CLASS);
17823a31b7ebSMike Smith 	    ld->cl_ldrive = NULL;
17833a31b7ebSMike Smith 	}
17843a31b7ebSMike Smith 	if (ld->cl_lstatus != NULL) {
17853a31b7ebSMike Smith 	    free(ld->cl_lstatus, CISS_MALLOC_CLASS);
17863a31b7ebSMike Smith 	    ld->cl_lstatus = NULL;
17873a31b7ebSMike Smith 	}
17883a31b7ebSMike Smith     }
17893a31b7ebSMike Smith     if (cr != NULL)
17903a31b7ebSMike Smith 	ciss_release_request(cr);
17913a31b7ebSMike Smith 
17923a31b7ebSMike Smith     return(error);
17933a31b7ebSMike Smith }
17943a31b7ebSMike Smith 
17953a31b7ebSMike Smith /************************************************************************
17963a31b7ebSMike Smith  * Get status for a logical drive.
17973a31b7ebSMike Smith  *
17983a31b7ebSMike Smith  * XXX should we also do this in response to Test Unit Ready?
17993a31b7ebSMike Smith  */
18003a31b7ebSMike Smith static int
18013a31b7ebSMike Smith ciss_get_ldrive_status(struct ciss_softc *sc,  struct ciss_ldrive *ld)
18023a31b7ebSMike Smith {
18033a31b7ebSMike Smith     struct ciss_request		*cr;
18043a31b7ebSMike Smith     struct ciss_command		*cc;
18053a31b7ebSMike Smith     struct ciss_bmic_cdb	*cbc;
18063a31b7ebSMike Smith     int				error, command_status;
18073a31b7ebSMike Smith 
18083a31b7ebSMike Smith     /*
18093a31b7ebSMike Smith      * Build a CISS BMIC command to get the logical drive status.
18103a31b7ebSMike Smith      */
18113a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LSTATUS,
18123a31b7ebSMike Smith 				       (void **)&ld->cl_lstatus,
18133a31b7ebSMike Smith 				       sizeof(*ld->cl_lstatus))) != 0)
18143a31b7ebSMike Smith 	goto out;
18152fdaa90dSScott Long     cc = cr->cr_cc;
1816c6131460SPaul Saab     cc->header.address = *ld->cl_controller;	/* target controller */
18173a31b7ebSMike Smith     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1818c6131460SPaul Saab     cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
18193a31b7ebSMike Smith 
18203a31b7ebSMike Smith     /*
18213a31b7ebSMike Smith      * Submit the request and wait for it to complete.
18223a31b7ebSMike Smith      */
18233a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
18243a31b7ebSMike Smith 	ciss_printf(sc, "error sending BMIC LSTATUS command (%d)\n", error);
18253a31b7ebSMike Smith 	goto out;
18263a31b7ebSMike Smith     }
18273a31b7ebSMike Smith 
18283a31b7ebSMike Smith     /*
18293a31b7ebSMike Smith      * Check response.
18303a31b7ebSMike Smith      */
18313a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
18323a31b7ebSMike Smith     switch(command_status) {
18333a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
18343a31b7ebSMike Smith 	break;
18353a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_UNDERRUN:
18363a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_OVERRUN:
18373a31b7ebSMike Smith 	ciss_printf(sc, "data over/underrun reading logical drive status\n");
18383a31b7ebSMike Smith     default:
18393a31b7ebSMike Smith 	ciss_printf(sc, "error reading logical drive status (%s)\n",
18403a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
18413a31b7ebSMike Smith 	error = EIO;
18423a31b7ebSMike Smith 	goto out;
18433a31b7ebSMike Smith     }
18443a31b7ebSMike Smith 
18453a31b7ebSMike Smith     /*
18463a31b7ebSMike Smith      * Set the drive's summary status based on the returned status.
18473a31b7ebSMike Smith      *
18483a31b7ebSMike Smith      * XXX testing shows that a failed JBOD drive comes back at next
18493a31b7ebSMike Smith      * boot in "queued for expansion" mode.  WTF?
18503a31b7ebSMike Smith      */
18513a31b7ebSMike Smith     ld->cl_status = ciss_decode_ldrive_status(ld->cl_lstatus->status);
18523a31b7ebSMike Smith 
18533a31b7ebSMike Smith out:
18543a31b7ebSMike Smith     if (cr != NULL)
18553a31b7ebSMike Smith 	ciss_release_request(cr);
18563a31b7ebSMike Smith     return(error);
18573a31b7ebSMike Smith }
18583a31b7ebSMike Smith 
18593a31b7ebSMike Smith /************************************************************************
18603a31b7ebSMike Smith  * Notify the adapter of a config update.
18613a31b7ebSMike Smith  */
18623a31b7ebSMike Smith static int
18633a31b7ebSMike Smith ciss_update_config(struct ciss_softc *sc)
18643a31b7ebSMike Smith {
18653a31b7ebSMike Smith     int		i;
18663a31b7ebSMike Smith 
18673a31b7ebSMike Smith     debug_called(1);
18683a31b7ebSMike Smith 
18693a31b7ebSMike Smith     CISS_TL_SIMPLE_WRITE(sc, CISS_TL_SIMPLE_IDBR, CISS_TL_SIMPLE_IDBR_CFG_TABLE);
18703a31b7ebSMike Smith     for (i = 0; i < 1000; i++) {
18713a31b7ebSMike Smith 	if (!(CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR) &
18723a31b7ebSMike Smith 	      CISS_TL_SIMPLE_IDBR_CFG_TABLE)) {
18733a31b7ebSMike Smith 	    return(0);
18743a31b7ebSMike Smith 	}
18753a31b7ebSMike Smith 	DELAY(1000);
18763a31b7ebSMike Smith     }
18773a31b7ebSMike Smith     return(1);
18783a31b7ebSMike Smith }
18793a31b7ebSMike Smith 
18803a31b7ebSMike Smith /************************************************************************
18813a31b7ebSMike Smith  * Accept new media into a logical drive.
18823a31b7ebSMike Smith  *
18833a31b7ebSMike Smith  * XXX The drive has previously been offline; it would be good if we
18843a31b7ebSMike Smith  *     could make sure it's not open right now.
18853a31b7ebSMike Smith  */
18863a31b7ebSMike Smith static int
188788c78a38SPaul Saab ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld)
18883a31b7ebSMike Smith {
18893a31b7ebSMike Smith     struct ciss_request		*cr;
18903a31b7ebSMike Smith     struct ciss_command		*cc;
18913a31b7ebSMike Smith     struct ciss_bmic_cdb	*cbc;
189288c78a38SPaul Saab     int				command_status;
189388c78a38SPaul Saab     int				error = 0, ldrive;
1894609caf8dSPaul Saab 
1895609caf8dSPaul Saab     ldrive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
18963a31b7ebSMike Smith 
18973ea59fd3SSean Bruno     debug(0, "bringing logical drive %d back online", ldrive);
18983a31b7ebSMike Smith 
18993a31b7ebSMike Smith     /*
19003a31b7ebSMike Smith      * Build a CISS BMIC command to bring the drive back online.
19013a31b7ebSMike Smith      */
19023a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ACCEPT_MEDIA,
19033a31b7ebSMike Smith 				       NULL, 0)) != 0)
19043a31b7ebSMike Smith 	goto out;
19052fdaa90dSScott Long     cc = cr->cr_cc;
1906c6131460SPaul Saab     cc->header.address = *ld->cl_controller;	/* target controller */
19073a31b7ebSMike Smith     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1908609caf8dSPaul Saab     cbc->log_drive = ldrive;
19093a31b7ebSMike Smith 
19103a31b7ebSMike Smith     /*
19113a31b7ebSMike Smith      * Submit the request and wait for it to complete.
19123a31b7ebSMike Smith      */
19133a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
191488c78a38SPaul Saab 	ciss_printf(sc, "error sending BMIC ACCEPT MEDIA command (%d)\n", error);
19153a31b7ebSMike Smith 	goto out;
19163a31b7ebSMike Smith     }
19173a31b7ebSMike Smith 
19183a31b7ebSMike Smith     /*
19193a31b7ebSMike Smith      * Check response.
19203a31b7ebSMike Smith      */
19213a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
19223a31b7ebSMike Smith     switch(command_status) {
19233a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:		/* all OK */
19243a31b7ebSMike Smith 	/* we should get a logical drive status changed event here */
19253a31b7ebSMike Smith 	break;
19263a31b7ebSMike Smith     default:
19273a31b7ebSMike Smith 	ciss_printf(cr->cr_sc, "error accepting media into failed logical drive (%s)\n",
19283a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
19293a31b7ebSMike Smith 	break;
19303a31b7ebSMike Smith     }
193188c78a38SPaul Saab 
193288c78a38SPaul Saab out:
193388c78a38SPaul Saab     if (cr != NULL)
19343a31b7ebSMike Smith 	ciss_release_request(cr);
193588c78a38SPaul Saab     return(error);
19363a31b7ebSMike Smith }
19373a31b7ebSMike Smith 
19383a31b7ebSMike Smith /************************************************************************
19393a31b7ebSMike Smith  * Release adapter resources.
19403a31b7ebSMike Smith  */
19413a31b7ebSMike Smith static void
19423a31b7ebSMike Smith ciss_free(struct ciss_softc *sc)
19433a31b7ebSMike Smith {
19443284b9eeSPaul Saab     struct ciss_request *cr;
1945ee626b40SPaul Saab     int			i, j;
19463284b9eeSPaul Saab 
19473a31b7ebSMike Smith     debug_called(1);
19483a31b7ebSMike Smith 
19493a31b7ebSMike Smith     /* we're going away */
19503a31b7ebSMike Smith     sc->ciss_flags |= CISS_FLAG_ABORTING;
19513a31b7ebSMike Smith 
19523a31b7ebSMike Smith     /* terminate the periodic heartbeat routine */
19532472e51eSScott Long     callout_stop(&sc->ciss_periodic);
19543a31b7ebSMike Smith 
19553a31b7ebSMike Smith     /* cancel the Event Notify chain */
19563a31b7ebSMike Smith     ciss_notify_abort(sc);
19573a31b7ebSMike Smith 
1958c6131460SPaul Saab     ciss_kill_notify_thread(sc);
1959c6131460SPaul Saab 
19602472e51eSScott Long     /* disconnect from CAM */
19612472e51eSScott Long     if (sc->ciss_cam_sim) {
19622472e51eSScott Long 	for (i = 0; i < sc->ciss_max_logical_bus; i++) {
19632472e51eSScott Long 	    if (sc->ciss_cam_sim[i]) {
19642472e51eSScott Long 		xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
19652472e51eSScott Long 		cam_sim_free(sc->ciss_cam_sim[i], 0);
19662472e51eSScott Long 	    }
19672472e51eSScott Long 	}
19682472e51eSScott Long 	for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
19692472e51eSScott Long 	     CISS_PHYSICAL_BASE; i++) {
19702472e51eSScott Long 	    if (sc->ciss_cam_sim[i]) {
19712472e51eSScott Long 		xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
19722472e51eSScott Long 		cam_sim_free(sc->ciss_cam_sim[i], 0);
19732472e51eSScott Long 	    }
19742472e51eSScott Long 	}
19752472e51eSScott Long 	free(sc->ciss_cam_sim, CISS_MALLOC_CLASS);
19762472e51eSScott Long     }
19772472e51eSScott Long     if (sc->ciss_cam_devq)
19782472e51eSScott Long 	cam_simq_free(sc->ciss_cam_devq);
19792472e51eSScott Long 
198078d03361SPaul Saab     /* remove the control device */
19812472e51eSScott Long     mtx_unlock(&sc->ciss_mtx);
198278d03361SPaul Saab     if (sc->ciss_dev_t != NULL)
198378d03361SPaul Saab 	destroy_dev(sc->ciss_dev_t);
198478d03361SPaul Saab 
19852472e51eSScott Long     /* Final cleanup of the callout. */
19862472e51eSScott Long     callout_drain(&sc->ciss_periodic);
19872472e51eSScott Long     mtx_destroy(&sc->ciss_mtx);
19882472e51eSScott Long 
19893a31b7ebSMike Smith     /* free the controller data */
19903a31b7ebSMike Smith     if (sc->ciss_id != NULL)
19913a31b7ebSMike Smith 	free(sc->ciss_id, CISS_MALLOC_CLASS);
19923a31b7ebSMike Smith 
19933a31b7ebSMike Smith     /* release I/O resources */
19943a31b7ebSMike Smith     if (sc->ciss_regs_resource != NULL)
19953a31b7ebSMike Smith 	bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
19963a31b7ebSMike Smith 			     sc->ciss_regs_rid, sc->ciss_regs_resource);
19973a31b7ebSMike Smith     if (sc->ciss_cfg_resource != NULL)
19983a31b7ebSMike Smith 	bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
19993a31b7ebSMike Smith 			     sc->ciss_cfg_rid, sc->ciss_cfg_resource);
20003a31b7ebSMike Smith     if (sc->ciss_intr != NULL)
20013a31b7ebSMike Smith 	bus_teardown_intr(sc->ciss_dev, sc->ciss_irq_resource, sc->ciss_intr);
20023a31b7ebSMike Smith     if (sc->ciss_irq_resource != NULL)
20033a31b7ebSMike Smith 	bus_release_resource(sc->ciss_dev, SYS_RES_IRQ,
200422657ce1SScott Long 			     sc->ciss_irq_rid[0], sc->ciss_irq_resource);
200522657ce1SScott Long     if (sc->ciss_msi)
200622657ce1SScott Long 	pci_release_msi(sc->ciss_dev);
20073284b9eeSPaul Saab 
20083284b9eeSPaul Saab     while ((cr = ciss_dequeue_free(sc)) != NULL)
20093284b9eeSPaul Saab 	bus_dmamap_destroy(sc->ciss_buffer_dmat, cr->cr_datamap);
20103a31b7ebSMike Smith     if (sc->ciss_buffer_dmat)
20113a31b7ebSMike Smith 	bus_dma_tag_destroy(sc->ciss_buffer_dmat);
20123a31b7ebSMike Smith 
20133a31b7ebSMike Smith     /* destroy command memory and DMA tag */
20143a31b7ebSMike Smith     if (sc->ciss_command != NULL) {
20153a31b7ebSMike Smith 	bus_dmamap_unload(sc->ciss_command_dmat, sc->ciss_command_map);
20163a31b7ebSMike Smith 	bus_dmamem_free(sc->ciss_command_dmat, sc->ciss_command, sc->ciss_command_map);
20173a31b7ebSMike Smith     }
20183284b9eeSPaul Saab     if (sc->ciss_command_dmat)
20193a31b7ebSMike Smith 	bus_dma_tag_destroy(sc->ciss_command_dmat);
20203a31b7ebSMike Smith 
202122657ce1SScott Long     if (sc->ciss_reply) {
202222657ce1SScott Long 	bus_dmamap_unload(sc->ciss_reply_dmat, sc->ciss_reply_map);
202322657ce1SScott Long 	bus_dmamem_free(sc->ciss_reply_dmat, sc->ciss_reply, sc->ciss_reply_map);
202422657ce1SScott Long     }
202522657ce1SScott Long     if (sc->ciss_reply_dmat)
202622657ce1SScott Long 	bus_dma_tag_destroy(sc->ciss_reply_dmat);
202722657ce1SScott Long 
202822657ce1SScott Long     /* destroy DMA tags */
202922657ce1SScott Long     if (sc->ciss_parent_dmat)
203022657ce1SScott Long 	bus_dma_tag_destroy(sc->ciss_parent_dmat);
2031c6131460SPaul Saab     if (sc->ciss_logical) {
2032ee626b40SPaul Saab 	for (i = 0; i <= sc->ciss_max_logical_bus; i++) {
2033e3edca22SSean Bruno 	    for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
2034ee626b40SPaul Saab 		if (sc->ciss_logical[i][j].cl_ldrive)
2035ee626b40SPaul Saab 		    free(sc->ciss_logical[i][j].cl_ldrive, CISS_MALLOC_CLASS);
2036ee626b40SPaul Saab 		if (sc->ciss_logical[i][j].cl_lstatus)
2037ee626b40SPaul Saab 		    free(sc->ciss_logical[i][j].cl_lstatus, CISS_MALLOC_CLASS);
2038ee626b40SPaul Saab 	    }
2039c6131460SPaul Saab 	    free(sc->ciss_logical[i], CISS_MALLOC_CLASS);
2040ee626b40SPaul Saab 	}
2041c6131460SPaul Saab 	free(sc->ciss_logical, CISS_MALLOC_CLASS);
2042c6131460SPaul Saab     }
2043c6131460SPaul Saab 
20441fe6c4eeSScott Long     if (sc->ciss_physical) {
20451fe6c4eeSScott Long 	for (i = 0; i < sc->ciss_max_physical_bus; i++)
20461fe6c4eeSScott Long 	    free(sc->ciss_physical[i], CISS_MALLOC_CLASS);
20471fe6c4eeSScott Long 	free(sc->ciss_physical, CISS_MALLOC_CLASS);
20481fe6c4eeSScott Long     }
20491fe6c4eeSScott Long 
2050c6131460SPaul Saab     if (sc->ciss_controllers)
2051c6131460SPaul Saab 	free(sc->ciss_controllers, CISS_MALLOC_CLASS);
2052975b7318SScott Long 
20533a31b7ebSMike Smith }
20543a31b7ebSMike Smith 
20553a31b7ebSMike Smith /************************************************************************
20563a31b7ebSMike Smith  * Give a command to the adapter.
20573a31b7ebSMike Smith  *
20583a31b7ebSMike Smith  * Note that this uses the simple transport layer directly.  If we
20593a31b7ebSMike Smith  * want to add support for other layers, we'll need a switch of some
20603a31b7ebSMike Smith  * sort.
20613a31b7ebSMike Smith  *
20623a31b7ebSMike Smith  * Note that the simple transport layer has no way of refusing a
20633a31b7ebSMike Smith  * command; we only have as many request structures as the adapter
20643a31b7ebSMike Smith  * supports commands, so we don't have to check (this presumes that
20653a31b7ebSMike Smith  * the adapter can handle commands as fast as we throw them at it).
20663a31b7ebSMike Smith  */
20673a31b7ebSMike Smith static int
20683a31b7ebSMike Smith ciss_start(struct ciss_request *cr)
20693a31b7ebSMike Smith {
20703a31b7ebSMike Smith     struct ciss_command	*cc;	/* XXX debugging only */
20713a31b7ebSMike Smith     int			error;
20723a31b7ebSMike Smith 
20732fdaa90dSScott Long     cc = cr->cr_cc;
20743a31b7ebSMike Smith     debug(2, "post command %d tag %d ", cr->cr_tag, cc->header.host_tag);
20753a31b7ebSMike Smith 
20763a31b7ebSMike Smith     /*
20773a31b7ebSMike Smith      * Map the request's data.
20783a31b7ebSMike Smith      */
20793a31b7ebSMike Smith     if ((error = ciss_map_request(cr)))
20803a31b7ebSMike Smith 	return(error);
20813a31b7ebSMike Smith 
20823a31b7ebSMike Smith #if 0
20833a31b7ebSMike Smith     ciss_print_request(cr);
20843a31b7ebSMike Smith #endif
20853a31b7ebSMike Smith 
20863a31b7ebSMike Smith     return(0);
20873a31b7ebSMike Smith }
20883a31b7ebSMike Smith 
20893a31b7ebSMike Smith /************************************************************************
20903a31b7ebSMike Smith  * Fetch completed request(s) from the adapter, queue them for
20913a31b7ebSMike Smith  * completion handling.
20923a31b7ebSMike Smith  *
20933a31b7ebSMike Smith  * Note that this uses the simple transport layer directly.  If we
20943a31b7ebSMike Smith  * want to add support for other layers, we'll need a switch of some
20953a31b7ebSMike Smith  * sort.
20963a31b7ebSMike Smith  *
20973a31b7ebSMike Smith  * Note that the simple transport mechanism does not require any
20983a31b7ebSMike Smith  * reentrancy protection; the OPQ read is atomic.  If there is a
20993a31b7ebSMike Smith  * chance of a race with something else that might move the request
21003a31b7ebSMike Smith  * off the busy list, then we will have to lock against that
21013a31b7ebSMike Smith  * (eg. timeouts, etc.)
21023a31b7ebSMike Smith  */
21033a31b7ebSMike Smith static void
210422657ce1SScott Long ciss_done(struct ciss_softc *sc, cr_qhead_t *qh)
21053a31b7ebSMike Smith {
21063a31b7ebSMike Smith     struct ciss_request	*cr;
21073a31b7ebSMike Smith     struct ciss_command	*cc;
21083a31b7ebSMike Smith     u_int32_t		tag, index;
21093a31b7ebSMike Smith 
21103a31b7ebSMike Smith     debug_called(3);
21113a31b7ebSMike Smith 
21123a31b7ebSMike Smith     /*
21133a31b7ebSMike Smith      * Loop quickly taking requests from the adapter and moving them
211422657ce1SScott Long      * to the completed queue.
21153a31b7ebSMike Smith      */
21163a31b7ebSMike Smith     for (;;) {
21173a31b7ebSMike Smith 
21183a31b7ebSMike Smith 	tag = CISS_TL_SIMPLE_FETCH_CMD(sc);
21193a31b7ebSMike Smith 	if (tag == CISS_TL_SIMPLE_OPQ_EMPTY)
21203a31b7ebSMike Smith 	    break;
21213a31b7ebSMike Smith 	index = tag >> 2;
21223a31b7ebSMike Smith 	debug(2, "completed command %d%s", index,
21233a31b7ebSMike Smith 	      (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
21243a31b7ebSMike Smith 	if (index >= sc->ciss_max_requests) {
21253a31b7ebSMike Smith 	    ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
21263a31b7ebSMike Smith 	    continue;
21273a31b7ebSMike Smith 	}
21283a31b7ebSMike Smith 	cr = &(sc->ciss_request[index]);
21292fdaa90dSScott Long 	cc = cr->cr_cc;
21303a31b7ebSMike Smith 	cc->header.host_tag = tag;	/* not updated by adapter */
213122657ce1SScott Long 	ciss_enqueue_complete(cr, qh);
21323a31b7ebSMike Smith     }
21333a31b7ebSMike Smith 
213422657ce1SScott Long }
213522657ce1SScott Long 
213622657ce1SScott Long static void
213722657ce1SScott Long ciss_perf_done(struct ciss_softc *sc, cr_qhead_t *qh)
213822657ce1SScott Long {
213922657ce1SScott Long     struct ciss_request	*cr;
214022657ce1SScott Long     struct ciss_command	*cc;
214122657ce1SScott Long     u_int32_t		tag, index;
214222657ce1SScott Long 
214322657ce1SScott Long     debug_called(3);
214422657ce1SScott Long 
21453a31b7ebSMike Smith     /*
214622657ce1SScott Long      * Loop quickly taking requests from the adapter and moving them
214722657ce1SScott Long      * to the completed queue.
21483a31b7ebSMike Smith      */
214922657ce1SScott Long     for (;;) {
215022657ce1SScott Long 	tag = sc->ciss_reply[sc->ciss_rqidx];
215122657ce1SScott Long 	if ((tag & CISS_CYCLE_MASK) != sc->ciss_cycle)
215222657ce1SScott Long 	    break;
215322657ce1SScott Long 	index = tag >> 2;
215422657ce1SScott Long 	debug(2, "completed command %d%s\n", index,
215522657ce1SScott Long 	      (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
215622657ce1SScott Long 	if (index < sc->ciss_max_requests) {
215722657ce1SScott Long 	    cr = &(sc->ciss_request[index]);
21582fdaa90dSScott Long 	    cc = cr->cr_cc;
215922657ce1SScott Long 	    cc->header.host_tag = tag;	/* not updated by adapter */
216022657ce1SScott Long 	    ciss_enqueue_complete(cr, qh);
216122657ce1SScott Long 	} else {
216222657ce1SScott Long 	    ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
216322657ce1SScott Long 	}
216422657ce1SScott Long 	if (++sc->ciss_rqidx == sc->ciss_max_requests) {
216522657ce1SScott Long 	    sc->ciss_rqidx = 0;
216622657ce1SScott Long 	    sc->ciss_cycle ^= 1;
216722657ce1SScott Long 	}
216822657ce1SScott Long     }
216922657ce1SScott Long 
21703a31b7ebSMike Smith }
21713a31b7ebSMike Smith 
21723a31b7ebSMike Smith /************************************************************************
21733a31b7ebSMike Smith  * Take an interrupt from the adapter.
21743a31b7ebSMike Smith  */
21753a31b7ebSMike Smith static void
21763a31b7ebSMike Smith ciss_intr(void *arg)
21773a31b7ebSMike Smith {
217822657ce1SScott Long     cr_qhead_t qh;
21793a31b7ebSMike Smith     struct ciss_softc	*sc = (struct ciss_softc *)arg;
21803a31b7ebSMike Smith 
21813a31b7ebSMike Smith     /*
21823a31b7ebSMike Smith      * The only interrupt we recognise indicates that there are
21833a31b7ebSMike Smith      * entries in the outbound post queue.
21843a31b7ebSMike Smith      */
218522657ce1SScott Long     STAILQ_INIT(&qh);
218622657ce1SScott Long     ciss_done(sc, &qh);
2187975b7318SScott Long     mtx_lock(&sc->ciss_mtx);
218822657ce1SScott Long     ciss_complete(sc, &qh);
2189975b7318SScott Long     mtx_unlock(&sc->ciss_mtx);
21903a31b7ebSMike Smith }
21913a31b7ebSMike Smith 
219222657ce1SScott Long static void
219322657ce1SScott Long ciss_perf_intr(void *arg)
219422657ce1SScott Long {
219522657ce1SScott Long     struct ciss_softc	*sc = (struct ciss_softc *)arg;
219622657ce1SScott Long 
219722657ce1SScott Long     /* Clear the interrupt and flush the bridges.  Docs say that the flush
219822657ce1SScott Long      * needs to be done twice, which doesn't seem right.
219922657ce1SScott Long      */
220022657ce1SScott Long     CISS_TL_PERF_CLEAR_INT(sc);
220122657ce1SScott Long     CISS_TL_PERF_FLUSH_INT(sc);
220222657ce1SScott Long 
220322657ce1SScott Long     ciss_perf_msi_intr(sc);
220422657ce1SScott Long }
220522657ce1SScott Long 
220622657ce1SScott Long static void
220722657ce1SScott Long ciss_perf_msi_intr(void *arg)
220822657ce1SScott Long {
220922657ce1SScott Long     cr_qhead_t qh;
221022657ce1SScott Long     struct ciss_softc	*sc = (struct ciss_softc *)arg;
221122657ce1SScott Long 
221222657ce1SScott Long     STAILQ_INIT(&qh);
221322657ce1SScott Long     ciss_perf_done(sc, &qh);
221422657ce1SScott Long     mtx_lock(&sc->ciss_mtx);
221522657ce1SScott Long     ciss_complete(sc, &qh);
221622657ce1SScott Long     mtx_unlock(&sc->ciss_mtx);
221722657ce1SScott Long }
221822657ce1SScott Long 
221922657ce1SScott Long 
22203a31b7ebSMike Smith /************************************************************************
22213a31b7ebSMike Smith  * Process completed requests.
22223a31b7ebSMike Smith  *
22233a31b7ebSMike Smith  * Requests can be completed in three fashions:
22243a31b7ebSMike Smith  *
22253a31b7ebSMike Smith  * - by invoking a callback function (cr_complete is non-null)
22263a31b7ebSMike Smith  * - by waking up a sleeper (cr_flags has CISS_REQ_SLEEP set)
22273a31b7ebSMike Smith  * - by clearing the CISS_REQ_POLL flag in interrupt/timeout context
22283a31b7ebSMike Smith  */
22293a31b7ebSMike Smith static void
223022657ce1SScott Long ciss_complete(struct ciss_softc *sc, cr_qhead_t *qh)
22313a31b7ebSMike Smith {
22323a31b7ebSMike Smith     struct ciss_request	*cr;
22333a31b7ebSMike Smith 
22343a31b7ebSMike Smith     debug_called(2);
22353a31b7ebSMike Smith 
22363a31b7ebSMike Smith     /*
22373a31b7ebSMike Smith      * Loop taking requests off the completed queue and performing
22383a31b7ebSMike Smith      * completion processing on them.
22393a31b7ebSMike Smith      */
22403a31b7ebSMike Smith     for (;;) {
224122657ce1SScott Long 	if ((cr = ciss_dequeue_complete(sc, qh)) == NULL)
22423a31b7ebSMike Smith 	    break;
22433a31b7ebSMike Smith 	ciss_unmap_request(cr);
22443a31b7ebSMike Smith 
224522657ce1SScott Long 	if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
224622657ce1SScott Long 	    ciss_printf(sc, "WARNING: completing non-busy request\n");
224722657ce1SScott Long 	cr->cr_flags &= ~CISS_REQ_BUSY;
224822657ce1SScott Long 
22493a31b7ebSMike Smith 	/*
22503a31b7ebSMike Smith 	 * If the request has a callback, invoke it.
22513a31b7ebSMike Smith 	 */
22523a31b7ebSMike Smith 	if (cr->cr_complete != NULL) {
22533a31b7ebSMike Smith 	    cr->cr_complete(cr);
22543a31b7ebSMike Smith 	    continue;
22553a31b7ebSMike Smith 	}
22563a31b7ebSMike Smith 
22573a31b7ebSMike Smith 	/*
22583a31b7ebSMike Smith 	 * If someone is sleeping on this request, wake them up.
22593a31b7ebSMike Smith 	 */
22603a31b7ebSMike Smith 	if (cr->cr_flags & CISS_REQ_SLEEP) {
22613a31b7ebSMike Smith 	    cr->cr_flags &= ~CISS_REQ_SLEEP;
22623a31b7ebSMike Smith 	    wakeup(cr);
22633a31b7ebSMike Smith 	    continue;
22643a31b7ebSMike Smith 	}
22653a31b7ebSMike Smith 
22663a31b7ebSMike Smith 	/*
22673a31b7ebSMike Smith 	 * If someone is polling this request for completion, signal.
22683a31b7ebSMike Smith 	 */
22693a31b7ebSMike Smith 	if (cr->cr_flags & CISS_REQ_POLL) {
22703a31b7ebSMike Smith 	    cr->cr_flags &= ~CISS_REQ_POLL;
22713a31b7ebSMike Smith 	    continue;
22723a31b7ebSMike Smith 	}
22733a31b7ebSMike Smith 
22743a31b7ebSMike Smith 	/*
22753a31b7ebSMike Smith 	 * Give up and throw the request back on the free queue.  This
22763a31b7ebSMike Smith 	 * should never happen; resources will probably be lost.
22773a31b7ebSMike Smith 	 */
22783a31b7ebSMike Smith 	ciss_printf(sc, "WARNING: completed command with no submitter\n");
22793a31b7ebSMike Smith 	ciss_enqueue_free(cr);
22803a31b7ebSMike Smith     }
22813a31b7ebSMike Smith }
22823a31b7ebSMike Smith 
22833a31b7ebSMike Smith /************************************************************************
22843a31b7ebSMike Smith  * Report on the completion status of a request, and pass back SCSI
22853a31b7ebSMike Smith  * and command status values.
22863a31b7ebSMike Smith  */
22873a31b7ebSMike Smith static int
228822657ce1SScott Long _ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status, const char *func)
22893a31b7ebSMike Smith {
22903a31b7ebSMike Smith     struct ciss_command		*cc;
22913a31b7ebSMike Smith     struct ciss_error_info	*ce;
22923a31b7ebSMike Smith 
22933a31b7ebSMike Smith     debug_called(2);
22943a31b7ebSMike Smith 
22952fdaa90dSScott Long     cc = cr->cr_cc;
22963a31b7ebSMike Smith     ce = (struct ciss_error_info *)&(cc->sg[0]);
22973a31b7ebSMike Smith 
22983a31b7ebSMike Smith     /*
22993a31b7ebSMike Smith      * We don't consider data under/overrun an error for the Report
23003a31b7ebSMike Smith      * Logical/Physical LUNs commands.
23013a31b7ebSMike Smith      */
23023a31b7ebSMike Smith     if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) &&
23032514c5bfSPaul Saab 	((ce->command_status == CISS_CMD_STATUS_DATA_OVERRUN) ||
23042514c5bfSPaul Saab 	 (ce->command_status == CISS_CMD_STATUS_DATA_UNDERRUN)) &&
23053a31b7ebSMike Smith 	((cc->cdb.cdb[0] == CISS_OPCODE_REPORT_LOGICAL_LUNS) ||
23062514c5bfSPaul Saab 	 (cc->cdb.cdb[0] == CISS_OPCODE_REPORT_PHYSICAL_LUNS) ||
23072514c5bfSPaul Saab 	 (cc->cdb.cdb[0] == INQUIRY))) {
23083a31b7ebSMike Smith 	cc->header.host_tag &= ~CISS_HDR_HOST_TAG_ERROR;
23093a31b7ebSMike Smith 	debug(2, "ignoring irrelevant under/overrun error");
23103a31b7ebSMike Smith     }
23113a31b7ebSMike Smith 
23123a31b7ebSMike Smith     /*
23133a31b7ebSMike Smith      * Check the command's error bit, if clear, there's no status and
23143a31b7ebSMike Smith      * everything is OK.
23153a31b7ebSMike Smith      */
23163a31b7ebSMike Smith     if (!(cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR)) {
23173a31b7ebSMike Smith 	if (scsi_status != NULL)
23183a31b7ebSMike Smith 	    *scsi_status = SCSI_STATUS_OK;
23193a31b7ebSMike Smith 	if (command_status != NULL)
23203a31b7ebSMike Smith 	    *command_status = CISS_CMD_STATUS_SUCCESS;
23213a31b7ebSMike Smith 	return(0);
23223a31b7ebSMike Smith     } else {
23233a31b7ebSMike Smith 	if (command_status != NULL)
23243a31b7ebSMike Smith 	    *command_status = ce->command_status;
23253a31b7ebSMike Smith 	if (scsi_status != NULL) {
23263a31b7ebSMike Smith 	    if (ce->command_status == CISS_CMD_STATUS_TARGET_STATUS) {
23273a31b7ebSMike Smith 		*scsi_status = ce->scsi_status;
23283a31b7ebSMike Smith 	    } else {
23293a31b7ebSMike Smith 		*scsi_status = -1;
23303a31b7ebSMike Smith 	    }
23313a31b7ebSMike Smith 	}
23323a31b7ebSMike Smith 	if (bootverbose)
23333a31b7ebSMike Smith 	    ciss_printf(cr->cr_sc, "command status 0x%x (%s) scsi status 0x%x\n",
23343a31b7ebSMike Smith 			ce->command_status, ciss_name_command_status(ce->command_status),
23353a31b7ebSMike Smith 			ce->scsi_status);
23363a31b7ebSMike Smith 	if (ce->command_status == CISS_CMD_STATUS_INVALID_COMMAND) {
233722657ce1SScott Long 	    ciss_printf(cr->cr_sc, "invalid command, offense size %d at %d, value 0x%x, function %s\n",
23383a31b7ebSMike Smith 			ce->additional_error_info.invalid_command.offense_size,
23393a31b7ebSMike Smith 			ce->additional_error_info.invalid_command.offense_offset,
234022657ce1SScott Long 			ce->additional_error_info.invalid_command.offense_value,
234122657ce1SScott Long 			func);
23423a31b7ebSMike Smith 	}
23433a31b7ebSMike Smith     }
2344c6131460SPaul Saab #if 0
2345c6131460SPaul Saab     ciss_print_request(cr);
2346c6131460SPaul Saab #endif
23473a31b7ebSMike Smith     return(1);
23483a31b7ebSMike Smith }
23493a31b7ebSMike Smith 
23503a31b7ebSMike Smith /************************************************************************
23513a31b7ebSMike Smith  * Issue a request and don't return until it's completed.
23523a31b7ebSMike Smith  *
23533a31b7ebSMike Smith  * Depending on adapter status, we may poll or sleep waiting for
23543a31b7ebSMike Smith  * completion.
23553a31b7ebSMike Smith  */
23563a31b7ebSMike Smith static int
23573a31b7ebSMike Smith ciss_synch_request(struct ciss_request *cr, int timeout)
23583a31b7ebSMike Smith {
23593a31b7ebSMike Smith     if (cr->cr_sc->ciss_flags & CISS_FLAG_RUNNING) {
23603a31b7ebSMike Smith 	return(ciss_wait_request(cr, timeout));
23613a31b7ebSMike Smith     } else {
23623a31b7ebSMike Smith 	return(ciss_poll_request(cr, timeout));
23633a31b7ebSMike Smith     }
23643a31b7ebSMike Smith }
23653a31b7ebSMike Smith 
23663a31b7ebSMike Smith /************************************************************************
23673a31b7ebSMike Smith  * Issue a request and poll for completion.
23683a31b7ebSMike Smith  *
23693a31b7ebSMike Smith  * Timeout in milliseconds.
23703a31b7ebSMike Smith  */
23713a31b7ebSMike Smith static int
23723a31b7ebSMike Smith ciss_poll_request(struct ciss_request *cr, int timeout)
23733a31b7ebSMike Smith {
237422657ce1SScott Long     cr_qhead_t qh;
237522657ce1SScott Long     struct ciss_softc *sc;
23763a31b7ebSMike Smith     int		error;
23773a31b7ebSMike Smith 
23783a31b7ebSMike Smith     debug_called(2);
23793a31b7ebSMike Smith 
238022657ce1SScott Long     STAILQ_INIT(&qh);
238122657ce1SScott Long     sc = cr->cr_sc;
23823a31b7ebSMike Smith     cr->cr_flags |= CISS_REQ_POLL;
23833a31b7ebSMike Smith     if ((error = ciss_start(cr)) != 0)
23843a31b7ebSMike Smith 	return(error);
23853a31b7ebSMike Smith 
23863a31b7ebSMike Smith     do {
238722657ce1SScott Long 	if (sc->ciss_perf)
238822657ce1SScott Long 	    ciss_perf_done(sc, &qh);
238922657ce1SScott Long 	else
239022657ce1SScott Long 	    ciss_done(sc, &qh);
239122657ce1SScott Long 	ciss_complete(sc, &qh);
23923a31b7ebSMike Smith 	if (!(cr->cr_flags & CISS_REQ_POLL))
23933a31b7ebSMike Smith 	    return(0);
23943a31b7ebSMike Smith 	DELAY(1000);
23953a31b7ebSMike Smith     } while (timeout-- >= 0);
23963a31b7ebSMike Smith     return(EWOULDBLOCK);
23973a31b7ebSMike Smith }
23983a31b7ebSMike Smith 
23993a31b7ebSMike Smith /************************************************************************
24003a31b7ebSMike Smith  * Issue a request and sleep waiting for completion.
24013a31b7ebSMike Smith  *
24023a31b7ebSMike Smith  * Timeout in milliseconds.  Note that a spurious wakeup will reset
24033a31b7ebSMike Smith  * the timeout.
24043a31b7ebSMike Smith  */
24053a31b7ebSMike Smith static int
24063a31b7ebSMike Smith ciss_wait_request(struct ciss_request *cr, int timeout)
24073a31b7ebSMike Smith {
240822657ce1SScott Long     int		error;
24093a31b7ebSMike Smith 
24103a31b7ebSMike Smith     debug_called(2);
24113a31b7ebSMike Smith 
24123a31b7ebSMike Smith     cr->cr_flags |= CISS_REQ_SLEEP;
24133a31b7ebSMike Smith     if ((error = ciss_start(cr)) != 0)
24143a31b7ebSMike Smith 	return(error);
24153a31b7ebSMike Smith 
241640f05b02SPaul Saab     while ((cr->cr_flags & CISS_REQ_SLEEP) && (error != EWOULDBLOCK)) {
2417975b7318SScott Long 	error = msleep(cr, &cr->cr_sc->ciss_mtx, PRIBIO, "cissREQ", (timeout * hz) / 1000);
24183a31b7ebSMike Smith     }
24193a31b7ebSMike Smith     return(error);
24203a31b7ebSMike Smith }
24213a31b7ebSMike Smith 
24226197ca15SPeter Wemm #if 0
24233a31b7ebSMike Smith /************************************************************************
24243a31b7ebSMike Smith  * Abort a request.  Note that a potential exists here to race the
24253a31b7ebSMike Smith  * request being completed; the caller must deal with this.
24263a31b7ebSMike Smith  */
24273a31b7ebSMike Smith static int
24283a31b7ebSMike Smith ciss_abort_request(struct ciss_request *ar)
24293a31b7ebSMike Smith {
24303a31b7ebSMike Smith     struct ciss_request		*cr;
24313a31b7ebSMike Smith     struct ciss_command		*cc;
24323a31b7ebSMike Smith     struct ciss_message_cdb	*cmc;
24333a31b7ebSMike Smith     int				error;
24343a31b7ebSMike Smith 
24353a31b7ebSMike Smith     debug_called(1);
24363a31b7ebSMike Smith 
24373a31b7ebSMike Smith     /* get a request */
24383a31b7ebSMike Smith     if ((error = ciss_get_request(ar->cr_sc, &cr)) != 0)
24393a31b7ebSMike Smith 	return(error);
24403a31b7ebSMike Smith 
24413a31b7ebSMike Smith     /* build the abort command */
24422fdaa90dSScott Long     cc = cr->cr_cc;
24433a31b7ebSMike Smith     cc->header.address.mode.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;	/* addressing? */
24443a31b7ebSMike Smith     cc->header.address.physical.target = 0;
24453a31b7ebSMike Smith     cc->header.address.physical.bus = 0;
24463a31b7ebSMike Smith     cc->cdb.cdb_length = sizeof(*cmc);
24473a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
24483a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
24493a31b7ebSMike Smith     cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
24503a31b7ebSMike Smith     cc->cdb.timeout = 30;
24513a31b7ebSMike Smith 
24523a31b7ebSMike Smith     cmc = (struct ciss_message_cdb *)&(cc->cdb.cdb[0]);
24533a31b7ebSMike Smith     cmc->opcode = CISS_OPCODE_MESSAGE_ABORT;
24543a31b7ebSMike Smith     cmc->type = CISS_MESSAGE_ABORT_TASK;
24553a31b7ebSMike Smith     cmc->abort_tag = ar->cr_tag;	/* endianness?? */
24563a31b7ebSMike Smith 
24573a31b7ebSMike Smith     /*
24583a31b7ebSMike Smith      * Send the request and wait for a response.  If we believe we
24593a31b7ebSMike Smith      * aborted the request OK, clear the flag that indicates it's
24603a31b7ebSMike Smith      * running.
24613a31b7ebSMike Smith      */
24623a31b7ebSMike Smith     error = ciss_synch_request(cr, 35 * 1000);
24633a31b7ebSMike Smith     if (!error)
24643a31b7ebSMike Smith 	error = ciss_report_request(cr, NULL, NULL);
24653a31b7ebSMike Smith     ciss_release_request(cr);
24663a31b7ebSMike Smith 
24673a31b7ebSMike Smith     return(error);
24683a31b7ebSMike Smith }
24696197ca15SPeter Wemm #endif
24703a31b7ebSMike Smith 
24713a31b7ebSMike Smith 
24723a31b7ebSMike Smith /************************************************************************
24733a31b7ebSMike Smith  * Fetch and initialise a request
24743a31b7ebSMike Smith  */
24753a31b7ebSMike Smith static int
24763a31b7ebSMike Smith ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp)
24773a31b7ebSMike Smith {
24783a31b7ebSMike Smith     struct ciss_request *cr;
24793a31b7ebSMike Smith 
24803a31b7ebSMike Smith     debug_called(2);
24813a31b7ebSMike Smith 
24823a31b7ebSMike Smith     /*
24833a31b7ebSMike Smith      * Get a request and clean it up.
24843a31b7ebSMike Smith      */
24853a31b7ebSMike Smith     if ((cr = ciss_dequeue_free(sc)) == NULL)
24863a31b7ebSMike Smith 	return(ENOMEM);
24873a31b7ebSMike Smith 
24883a31b7ebSMike Smith     cr->cr_data = NULL;
24893a31b7ebSMike Smith     cr->cr_flags = 0;
24903a31b7ebSMike Smith     cr->cr_complete = NULL;
2491639717c8SPaul Saab     cr->cr_private = NULL;
249222657ce1SScott Long     cr->cr_sg_tag = CISS_SG_MAX;	/* Backstop to prevent accidents */
24933a31b7ebSMike Smith 
24943a31b7ebSMike Smith     ciss_preen_command(cr);
24953a31b7ebSMike Smith     *crp = cr;
24963a31b7ebSMike Smith     return(0);
24973a31b7ebSMike Smith }
24983a31b7ebSMike Smith 
24993a31b7ebSMike Smith static void
25003a31b7ebSMike Smith ciss_preen_command(struct ciss_request *cr)
25013a31b7ebSMike Smith {
25023a31b7ebSMike Smith     struct ciss_command	*cc;
25033a31b7ebSMike Smith     u_int32_t		cmdphys;
25043a31b7ebSMike Smith 
25053a31b7ebSMike Smith     /*
25063a31b7ebSMike Smith      * Clean up the command structure.
25073a31b7ebSMike Smith      *
25083a31b7ebSMike Smith      * Note that we set up the error_info structure here, since the
25093a31b7ebSMike Smith      * length can be overwritten by any command.
25103a31b7ebSMike Smith      */
25112fdaa90dSScott Long     cc = cr->cr_cc;
25123a31b7ebSMike Smith     cc->header.sg_in_list = 0;		/* kinda inefficient this way */
25133a31b7ebSMike Smith     cc->header.sg_total = 0;
25143a31b7ebSMike Smith     cc->header.host_tag = cr->cr_tag << 2;
25153a31b7ebSMike Smith     cc->header.host_tag_zeroes = 0;
25163d3ddb44SSean Bruno     bzero(&(cc->sg[0]), CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command));
25172fdaa90dSScott Long     cmdphys = cr->cr_ccphys;
25183a31b7ebSMike Smith     cc->error_info.error_info_address = cmdphys + sizeof(struct ciss_command);
25193a31b7ebSMike Smith     cc->error_info.error_info_length = CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command);
25203a31b7ebSMike Smith }
25213a31b7ebSMike Smith 
25223a31b7ebSMike Smith /************************************************************************
25233a31b7ebSMike Smith  * Release a request to the free list.
25243a31b7ebSMike Smith  */
25253a31b7ebSMike Smith static void
25263a31b7ebSMike Smith ciss_release_request(struct ciss_request *cr)
25273a31b7ebSMike Smith {
25283a31b7ebSMike Smith     struct ciss_softc	*sc;
25293a31b7ebSMike Smith 
25303a31b7ebSMike Smith     debug_called(2);
25313a31b7ebSMike Smith 
25323a31b7ebSMike Smith     sc = cr->cr_sc;
25333a31b7ebSMike Smith 
25343a31b7ebSMike Smith     /* release the request to the free queue */
25353a31b7ebSMike Smith     ciss_requeue_free(cr);
25363a31b7ebSMike Smith }
25373a31b7ebSMike Smith 
25383a31b7ebSMike Smith /************************************************************************
25393a31b7ebSMike Smith  * Allocate a request that will be used to send a BMIC command.  Do some
25403a31b7ebSMike Smith  * of the common setup here to avoid duplicating it everywhere else.
25413a31b7ebSMike Smith  */
25423a31b7ebSMike Smith static int
25433a31b7ebSMike Smith ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
25443a31b7ebSMike Smith 		      int opcode, void **bufp, size_t bufsize)
25453a31b7ebSMike Smith {
25463a31b7ebSMike Smith     struct ciss_request		*cr;
25473a31b7ebSMike Smith     struct ciss_command		*cc;
25483a31b7ebSMike Smith     struct ciss_bmic_cdb	*cbc;
25493a31b7ebSMike Smith     void			*buf;
25503a31b7ebSMike Smith     int				error;
25513a31b7ebSMike Smith     int				dataout;
25523a31b7ebSMike Smith 
25533a31b7ebSMike Smith     debug_called(2);
25543a31b7ebSMike Smith 
25553a31b7ebSMike Smith     cr = NULL;
25563a31b7ebSMike Smith     buf = NULL;
25573a31b7ebSMike Smith 
25583a31b7ebSMike Smith     /*
25593a31b7ebSMike Smith      * Get a request.
25603a31b7ebSMike Smith      */
25613a31b7ebSMike Smith     if ((error = ciss_get_request(sc, &cr)) != 0)
25623a31b7ebSMike Smith 	goto out;
25633a31b7ebSMike Smith 
25643a31b7ebSMike Smith     /*
25653a31b7ebSMike Smith      * Allocate data storage if requested, determine the data direction.
25663a31b7ebSMike Smith      */
25673a31b7ebSMike Smith     dataout = 0;
25683a31b7ebSMike Smith     if ((bufsize > 0) && (bufp != NULL)) {
25693a31b7ebSMike Smith 	if (*bufp == NULL) {
25703a31b7ebSMike Smith 	    if ((buf = malloc(bufsize, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
25713a31b7ebSMike Smith 		error = ENOMEM;
25723a31b7ebSMike Smith 		goto out;
25733a31b7ebSMike Smith 	    }
25743a31b7ebSMike Smith 	} else {
25753a31b7ebSMike Smith 	    buf = *bufp;
25763a31b7ebSMike Smith 	    dataout = 1;	/* we are given a buffer, so we are writing */
25773a31b7ebSMike Smith 	}
25783a31b7ebSMike Smith     }
25793a31b7ebSMike Smith 
25803a31b7ebSMike Smith     /*
25813a31b7ebSMike Smith      * Build a CISS BMIC command to get the logical drive ID.
25823a31b7ebSMike Smith      */
25833a31b7ebSMike Smith     cr->cr_data = buf;
25843a31b7ebSMike Smith     cr->cr_length = bufsize;
25853a31b7ebSMike Smith     if (!dataout)
25863a31b7ebSMike Smith 	cr->cr_flags = CISS_REQ_DATAIN;
25873a31b7ebSMike Smith 
25882fdaa90dSScott Long     cc = cr->cr_cc;
25893a31b7ebSMike Smith     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
25903a31b7ebSMike Smith     cc->header.address.physical.bus = 0;
25913a31b7ebSMike Smith     cc->header.address.physical.target = 0;
25923a31b7ebSMike Smith     cc->cdb.cdb_length = sizeof(*cbc);
25933a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
25943a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
25953a31b7ebSMike Smith     cc->cdb.direction = dataout ? CISS_CDB_DIRECTION_WRITE : CISS_CDB_DIRECTION_READ;
25963a31b7ebSMike Smith     cc->cdb.timeout = 0;
25973a31b7ebSMike Smith 
25983a31b7ebSMike Smith     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
25993a31b7ebSMike Smith     bzero(cbc, sizeof(*cbc));
26003a31b7ebSMike Smith     cbc->opcode = dataout ? CISS_ARRAY_CONTROLLER_WRITE : CISS_ARRAY_CONTROLLER_READ;
26013a31b7ebSMike Smith     cbc->bmic_opcode = opcode;
26023a31b7ebSMike Smith     cbc->size = htons((u_int16_t)bufsize);
26033a31b7ebSMike Smith 
26043a31b7ebSMike Smith out:
26053a31b7ebSMike Smith     if (error) {
26063a31b7ebSMike Smith 	if (cr != NULL)
26073a31b7ebSMike Smith 	    ciss_release_request(cr);
26083a31b7ebSMike Smith     } else {
26093a31b7ebSMike Smith 	*crp = cr;
26103a31b7ebSMike Smith 	if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL))
26113a31b7ebSMike Smith 	    *bufp = buf;
26123a31b7ebSMike Smith     }
26133a31b7ebSMike Smith     return(error);
26143a31b7ebSMike Smith }
26153a31b7ebSMike Smith 
26163a31b7ebSMike Smith /************************************************************************
26173a31b7ebSMike Smith  * Handle a command passed in from userspace.
26183a31b7ebSMike Smith  */
26193a31b7ebSMike Smith static int
26203a31b7ebSMike Smith ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc)
26213a31b7ebSMike Smith {
26223a31b7ebSMike Smith     struct ciss_request		*cr;
26233a31b7ebSMike Smith     struct ciss_command		*cc;
26243a31b7ebSMike Smith     struct ciss_error_info	*ce;
26253c63a8b4SPaul Saab     int				error = 0;
26263a31b7ebSMike Smith 
26273a31b7ebSMike Smith     debug_called(1);
26283a31b7ebSMike Smith 
26293a31b7ebSMike Smith     cr = NULL;
26303a31b7ebSMike Smith 
26313a31b7ebSMike Smith     /*
26323a31b7ebSMike Smith      * Get a request.
26333a31b7ebSMike Smith      */
2634f83695f5SPaul Saab     while (ciss_get_request(sc, &cr) != 0)
2635975b7318SScott Long 	msleep(sc, &sc->ciss_mtx, PPAUSE, "cissREQ", hz);
26362fdaa90dSScott Long     cc = cr->cr_cc;
26373a31b7ebSMike Smith 
26383a31b7ebSMike Smith     /*
26393a31b7ebSMike Smith      * Allocate an in-kernel databuffer if required, copy in user data.
26403a31b7ebSMike Smith      */
2641744c0d23SScott Long     mtx_unlock(&sc->ciss_mtx);
26423a31b7ebSMike Smith     cr->cr_length = ioc->buf_size;
26433a31b7ebSMike Smith     if (ioc->buf_size > 0) {
2644975b7318SScott Long 	if ((cr->cr_data = malloc(ioc->buf_size, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
26453a31b7ebSMike Smith 	    error = ENOMEM;
2646744c0d23SScott Long 	    goto out_unlocked;
26473a31b7ebSMike Smith 	}
26483a31b7ebSMike Smith 	if ((error = copyin(ioc->buf, cr->cr_data, ioc->buf_size))) {
26493a31b7ebSMike Smith 	    debug(0, "copyin: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2650744c0d23SScott Long 	    goto out_unlocked;
26513a31b7ebSMike Smith 	}
26523a31b7ebSMike Smith     }
26533a31b7ebSMike Smith 
26543a31b7ebSMike Smith     /*
26553a31b7ebSMike Smith      * Build the request based on the user command.
26563a31b7ebSMike Smith      */
26573a31b7ebSMike Smith     bcopy(&ioc->LUN_info, &cc->header.address, sizeof(cc->header.address));
26583a31b7ebSMike Smith     bcopy(&ioc->Request, &cc->cdb, sizeof(cc->cdb));
26593a31b7ebSMike Smith 
26603a31b7ebSMike Smith     /* XXX anything else to populate here? */
2661744c0d23SScott Long     mtx_lock(&sc->ciss_mtx);
26623a31b7ebSMike Smith 
26633a31b7ebSMike Smith     /*
26643a31b7ebSMike Smith      * Run the command.
26653a31b7ebSMike Smith      */
26663a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000))) {
26673a31b7ebSMike Smith 	debug(0, "request failed - %d", error);
26683a31b7ebSMike Smith 	goto out;
26693a31b7ebSMike Smith     }
26703a31b7ebSMike Smith 
26713a31b7ebSMike Smith     /*
26723c63a8b4SPaul Saab      * Check to see if the command succeeded.
26733a31b7ebSMike Smith      */
26743a31b7ebSMike Smith     ce = (struct ciss_error_info *)&(cc->sg[0]);
26753c6b8353SPaul Saab     if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) == 0)
26763c63a8b4SPaul Saab 	bzero(ce, sizeof(*ce));
26773c63a8b4SPaul Saab 
26783c63a8b4SPaul Saab     /*
26793c63a8b4SPaul Saab      * Copy the results back to the user.
26803c63a8b4SPaul Saab      */
26813a31b7ebSMike Smith     bcopy(ce, &ioc->error_info, sizeof(*ce));
2682744c0d23SScott Long     mtx_unlock(&sc->ciss_mtx);
26833a31b7ebSMike Smith     if ((ioc->buf_size > 0) &&
26843a31b7ebSMike Smith 	(error = copyout(cr->cr_data, ioc->buf, ioc->buf_size))) {
26853a31b7ebSMike Smith 	debug(0, "copyout: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2686744c0d23SScott Long 	goto out_unlocked;
26873a31b7ebSMike Smith     }
26883a31b7ebSMike Smith 
26893a31b7ebSMike Smith     /* done OK */
26903a31b7ebSMike Smith     error = 0;
26913a31b7ebSMike Smith 
2692744c0d23SScott Long out_unlocked:
2693744c0d23SScott Long     mtx_lock(&sc->ciss_mtx);
2694744c0d23SScott Long 
26953a31b7ebSMike Smith out:
26963a31b7ebSMike Smith     if ((cr != NULL) && (cr->cr_data != NULL))
26973a31b7ebSMike Smith 	free(cr->cr_data, CISS_MALLOC_CLASS);
26983a31b7ebSMike Smith     if (cr != NULL)
26993a31b7ebSMike Smith 	ciss_release_request(cr);
27003a31b7ebSMike Smith     return(error);
27013a31b7ebSMike Smith }
27023a31b7ebSMike Smith 
27033a31b7ebSMike Smith /************************************************************************
27043a31b7ebSMike Smith  * Map a request into bus-visible space, initialise the scatter/gather
27053a31b7ebSMike Smith  * list.
27063a31b7ebSMike Smith  */
27073a31b7ebSMike Smith static int
27083a31b7ebSMike Smith ciss_map_request(struct ciss_request *cr)
27093a31b7ebSMike Smith {
27103a31b7ebSMike Smith     struct ciss_softc	*sc;
2711639717c8SPaul Saab     int			error = 0;
27123a31b7ebSMike Smith 
27133a31b7ebSMike Smith     debug_called(2);
27143a31b7ebSMike Smith 
27153a31b7ebSMike Smith     sc = cr->cr_sc;
27163a31b7ebSMike Smith 
27173a31b7ebSMike Smith     /* check that mapping is necessary */
2718639717c8SPaul Saab     if (cr->cr_flags & CISS_REQ_MAPPED)
27193a31b7ebSMike Smith 	return(0);
27203a31b7ebSMike Smith 
27213a31b7ebSMike Smith     cr->cr_flags |= CISS_REQ_MAPPED;
2722639717c8SPaul Saab 
2723639717c8SPaul Saab     bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2724639717c8SPaul Saab 		    BUS_DMASYNC_PREWRITE);
2725639717c8SPaul Saab 
2726639717c8SPaul Saab     if (cr->cr_data != NULL) {
2727dd0b4fb6SKonstantin Belousov 	if (cr->cr_flags & CISS_REQ_CCB)
2728dd0b4fb6SKonstantin Belousov 		error = bus_dmamap_load_ccb(sc->ciss_buffer_dmat,
2729dd0b4fb6SKonstantin Belousov 					cr->cr_datamap, cr->cr_data,
2730dd0b4fb6SKonstantin Belousov 					ciss_request_map_helper, cr, 0);
2731dd0b4fb6SKonstantin Belousov 	else
2732639717c8SPaul Saab 		error = bus_dmamap_load(sc->ciss_buffer_dmat, cr->cr_datamap,
2733639717c8SPaul Saab 					cr->cr_data, cr->cr_length,
2734639717c8SPaul Saab 					ciss_request_map_helper, cr, 0);
2735639717c8SPaul Saab 	if (error != 0)
2736639717c8SPaul Saab 	    return (error);
2737639717c8SPaul Saab     } else {
2738639717c8SPaul Saab 	/*
2739639717c8SPaul Saab 	 * Post the command to the adapter.
2740639717c8SPaul Saab 	 */
274122657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_NONE;
274222657ce1SScott Long 	cr->cr_flags |= CISS_REQ_BUSY;
274322657ce1SScott Long 	if (sc->ciss_perf)
274422657ce1SScott Long 	    CISS_TL_PERF_POST_CMD(sc, cr);
274522657ce1SScott Long 	else
27462fdaa90dSScott Long 	    CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
2747639717c8SPaul Saab     }
2748639717c8SPaul Saab 
27493a31b7ebSMike Smith     return(0);
27503a31b7ebSMike Smith }
27513a31b7ebSMike Smith 
27523a31b7ebSMike Smith static void
27533a31b7ebSMike Smith ciss_request_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
27543a31b7ebSMike Smith {
27553a31b7ebSMike Smith     struct ciss_command	*cc;
2756639717c8SPaul Saab     struct ciss_request *cr;
2757639717c8SPaul Saab     struct ciss_softc	*sc;
27583a31b7ebSMike Smith     int			i;
27593a31b7ebSMike Smith 
27603a31b7ebSMike Smith     debug_called(2);
27613a31b7ebSMike Smith 
2762639717c8SPaul Saab     cr = (struct ciss_request *)arg;
2763639717c8SPaul Saab     sc = cr->cr_sc;
27642fdaa90dSScott Long     cc = cr->cr_cc;
2765639717c8SPaul Saab 
27663a31b7ebSMike Smith     for (i = 0; i < nseg; i++) {
27673a31b7ebSMike Smith 	cc->sg[i].address = segs[i].ds_addr;
27683a31b7ebSMike Smith 	cc->sg[i].length = segs[i].ds_len;
27693a31b7ebSMike Smith 	cc->sg[i].extension = 0;
27703a31b7ebSMike Smith     }
27713a31b7ebSMike Smith     /* we leave the s/g table entirely within the command */
27723a31b7ebSMike Smith     cc->header.sg_in_list = nseg;
27733a31b7ebSMike Smith     cc->header.sg_total = nseg;
2774639717c8SPaul Saab 
2775639717c8SPaul Saab     if (cr->cr_flags & CISS_REQ_DATAIN)
2776639717c8SPaul Saab 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREREAD);
2777639717c8SPaul Saab     if (cr->cr_flags & CISS_REQ_DATAOUT)
2778639717c8SPaul Saab 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREWRITE);
2779639717c8SPaul Saab 
278062fdee1dSScott Long     if (nseg == 0)
278122657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_NONE;
278222657ce1SScott Long     else if (nseg == 1)
278322657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_1;
278422657ce1SScott Long     else if (nseg == 2)
278522657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_2;
278622657ce1SScott Long     else if (nseg <= 4)
278722657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_4;
278822657ce1SScott Long     else if (nseg <= 8)
278922657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_8;
279022657ce1SScott Long     else if (nseg <= 16)
279122657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_16;
279222657ce1SScott Long     else if (nseg <= 32)
279322657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_32;
279422657ce1SScott Long     else
279522657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_MAX;
279622657ce1SScott Long 
2797639717c8SPaul Saab     /*
2798639717c8SPaul Saab      * Post the command to the adapter.
2799639717c8SPaul Saab      */
280022657ce1SScott Long     cr->cr_flags |= CISS_REQ_BUSY;
280122657ce1SScott Long     if (sc->ciss_perf)
280222657ce1SScott Long 	CISS_TL_PERF_POST_CMD(sc, cr);
280322657ce1SScott Long     else
28042fdaa90dSScott Long 	CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
28053a31b7ebSMike Smith }
28063a31b7ebSMike Smith 
28073a31b7ebSMike Smith /************************************************************************
28083a31b7ebSMike Smith  * Unmap a request from bus-visible space.
28093a31b7ebSMike Smith  */
28103a31b7ebSMike Smith static void
28113a31b7ebSMike Smith ciss_unmap_request(struct ciss_request *cr)
28123a31b7ebSMike Smith {
28133a31b7ebSMike Smith     struct ciss_softc	*sc;
28143a31b7ebSMike Smith 
28153a31b7ebSMike Smith     debug_called(2);
28163a31b7ebSMike Smith 
28173a31b7ebSMike Smith     sc = cr->cr_sc;
28183a31b7ebSMike Smith 
28193a31b7ebSMike Smith     /* check that unmapping is necessary */
2820639717c8SPaul Saab     if ((cr->cr_flags & CISS_REQ_MAPPED) == 0)
28213a31b7ebSMike Smith 	return;
28223a31b7ebSMike Smith 
2823639717c8SPaul Saab     bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2824639717c8SPaul Saab 		    BUS_DMASYNC_POSTWRITE);
2825639717c8SPaul Saab 
2826639717c8SPaul Saab     if (cr->cr_data == NULL)
2827639717c8SPaul Saab 	goto out;
2828639717c8SPaul Saab 
28293a31b7ebSMike Smith     if (cr->cr_flags & CISS_REQ_DATAIN)
28303a31b7ebSMike Smith 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTREAD);
28313a31b7ebSMike Smith     if (cr->cr_flags & CISS_REQ_DATAOUT)
28323a31b7ebSMike Smith 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTWRITE);
28333a31b7ebSMike Smith 
28343a31b7ebSMike Smith     bus_dmamap_unload(sc->ciss_buffer_dmat, cr->cr_datamap);
2835639717c8SPaul Saab out:
28363a31b7ebSMike Smith     cr->cr_flags &= ~CISS_REQ_MAPPED;
28373a31b7ebSMike Smith }
28383a31b7ebSMike Smith 
28393a31b7ebSMike Smith /************************************************************************
28403a31b7ebSMike Smith  * Attach the driver to CAM.
28413a31b7ebSMike Smith  *
28423a31b7ebSMike Smith  * We put all the logical drives on a single SCSI bus.
28433a31b7ebSMike Smith  */
28443a31b7ebSMike Smith static int
28453a31b7ebSMike Smith ciss_cam_init(struct ciss_softc *sc)
28463a31b7ebSMike Smith {
28471fe6c4eeSScott Long     int			i, maxbus;
28483a31b7ebSMike Smith 
28493a31b7ebSMike Smith     debug_called(1);
28503a31b7ebSMike Smith 
28513a31b7ebSMike Smith     /*
28523a31b7ebSMike Smith      * Allocate a devq.  We can reuse this for the masked physical
28533a31b7ebSMike Smith      * devices if we decide to export these as well.
28543a31b7ebSMike Smith      */
285595dab4f2SAlexander Motin     if ((sc->ciss_cam_devq = cam_simq_alloc(sc->ciss_max_requests - 2)) == NULL) {
28563a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate CAM SIM queue\n");
28573a31b7ebSMike Smith 	return(ENOMEM);
28583a31b7ebSMike Smith     }
28593a31b7ebSMike Smith 
28603a31b7ebSMike Smith     /*
28613a31b7ebSMike Smith      * Create a SIM.
28621fe6c4eeSScott Long      *
28631fe6c4eeSScott Long      * This naturally wastes a bit of memory.  The alternative is to allocate
28641fe6c4eeSScott Long      * and register each bus as it is found, and then track them on a linked
28651fe6c4eeSScott Long      * list.  Unfortunately, the driver has a few places where it needs to
28661fe6c4eeSScott Long      * look up the SIM based solely on bus number, and it's unclear whether
28671fe6c4eeSScott Long      * a list traversal would work for these situations.
28683a31b7ebSMike Smith      */
28691fe6c4eeSScott Long     maxbus = max(sc->ciss_max_logical_bus, sc->ciss_max_physical_bus +
28701fe6c4eeSScott Long 		 CISS_PHYSICAL_BASE);
28711fe6c4eeSScott Long     sc->ciss_cam_sim = malloc(maxbus * sizeof(struct cam_sim*),
2872c6131460SPaul Saab 			      CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
2873c6131460SPaul Saab     if (sc->ciss_cam_sim == NULL) {
2874c6131460SPaul Saab 	ciss_printf(sc, "can't allocate memory for controller SIM\n");
2875c6131460SPaul Saab 	return(ENOMEM);
2876c6131460SPaul Saab     }
2877c6131460SPaul Saab 
28781fe6c4eeSScott Long     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
2879c6131460SPaul Saab 	if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
2880c6131460SPaul Saab 						 "ciss", sc,
28812aa6b972SPaul Saab 						 device_get_unit(sc->ciss_dev),
288245650f52SScott Long 						 &sc->ciss_mtx,
288322657ce1SScott Long 						 2,
2884d4aa427fSPaul Saab 						 sc->ciss_max_requests - 2,
28853a31b7ebSMike Smith 						 sc->ciss_cam_devq)) == NULL) {
2886c6131460SPaul Saab 	    ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
28873a31b7ebSMike Smith 	    return(ENOMEM);
28883a31b7ebSMike Smith 	}
28893a31b7ebSMike Smith 
28903a31b7ebSMike Smith 	/*
2891c6131460SPaul Saab 	 * Register bus with this SIM.
28923a31b7ebSMike Smith 	 */
2893975b7318SScott Long 	mtx_lock(&sc->ciss_mtx);
28941fe6c4eeSScott Long 	if (i == 0 || sc->ciss_controllers[i].physical.bus != 0) {
2895b50569b7SScott Long 	    if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
28961fe6c4eeSScott Long 		ciss_printf(sc, "can't register SCSI bus %d\n", i);
2897975b7318SScott Long 		mtx_unlock(&sc->ciss_mtx);
28981fe6c4eeSScott Long 		return (ENXIO);
28991fe6c4eeSScott Long 	    }
29001fe6c4eeSScott Long 	}
2901975b7318SScott Long 	mtx_unlock(&sc->ciss_mtx);
29021fe6c4eeSScott Long     }
29031fe6c4eeSScott Long 
29041fe6c4eeSScott Long     for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
29051fe6c4eeSScott Long 	 CISS_PHYSICAL_BASE; i++) {
29061fe6c4eeSScott Long 	if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
29071fe6c4eeSScott Long 						 "ciss", sc,
29081fe6c4eeSScott Long 						 device_get_unit(sc->ciss_dev),
2909975b7318SScott Long 						 &sc->ciss_mtx, 1,
29101fe6c4eeSScott Long 						 sc->ciss_max_requests - 2,
29111fe6c4eeSScott Long 						 sc->ciss_cam_devq)) == NULL) {
29121fe6c4eeSScott Long 	    ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
29131fe6c4eeSScott Long 	    return (ENOMEM);
29141fe6c4eeSScott Long 	}
29151fe6c4eeSScott Long 
2916975b7318SScott Long 	mtx_lock(&sc->ciss_mtx);
2917b50569b7SScott Long 	if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
2918c6131460SPaul Saab 	    ciss_printf(sc, "can't register SCSI bus %d\n", i);
2919975b7318SScott Long 	    mtx_unlock(&sc->ciss_mtx);
29203a31b7ebSMike Smith 	    return (ENXIO);
29213a31b7ebSMike Smith 	}
2922975b7318SScott Long 	mtx_unlock(&sc->ciss_mtx);
2923c6131460SPaul Saab     }
29243a31b7ebSMike Smith 
29253a31b7ebSMike Smith     return(0);
29263a31b7ebSMike Smith }
29273a31b7ebSMike Smith 
29283a31b7ebSMike Smith /************************************************************************
29293a31b7ebSMike Smith  * Initiate a rescan of the 'logical devices' SIM
29303a31b7ebSMike Smith  */
29313a31b7ebSMike Smith static void
2932c6131460SPaul Saab ciss_cam_rescan_target(struct ciss_softc *sc, int bus, int target)
29333a31b7ebSMike Smith {
29343a31b7ebSMike Smith     union ccb		*ccb;
29353a31b7ebSMike Smith 
29363a31b7ebSMike Smith     debug_called(1);
29373a31b7ebSMike Smith 
293883c5d981SAlexander Motin     if ((ccb = xpt_alloc_ccb_nowait()) == NULL) {
29393a31b7ebSMike Smith 	ciss_printf(sc, "rescan failed (can't allocate CCB)\n");
29403a31b7ebSMike Smith 	return;
29413a31b7ebSMike Smith     }
29423a31b7ebSMike Smith 
2943e5dfa058SAlexander Motin     if (xpt_create_path(&ccb->ccb_h.path, NULL,
294483c5d981SAlexander Motin 	    cam_sim_path(sc->ciss_cam_sim[bus]),
2945c6131460SPaul Saab 	    target, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
29463a31b7ebSMike Smith 	ciss_printf(sc, "rescan failed (can't create path)\n");
294783c5d981SAlexander Motin 	xpt_free_ccb(ccb);
29483a31b7ebSMike Smith 	return;
29493a31b7ebSMike Smith     }
295083c5d981SAlexander Motin     xpt_rescan(ccb);
29513a31b7ebSMike Smith     /* scan is now in progress */
29523a31b7ebSMike Smith }
29533a31b7ebSMike Smith 
29543a31b7ebSMike Smith /************************************************************************
29553a31b7ebSMike Smith  * Handle requests coming from CAM
29563a31b7ebSMike Smith  */
29573a31b7ebSMike Smith static void
29583a31b7ebSMike Smith ciss_cam_action(struct cam_sim *sim, union ccb *ccb)
29593a31b7ebSMike Smith {
2960440baa08SPaul Saab     struct ciss_softc	*sc;
2961440baa08SPaul Saab     struct ccb_scsiio	*csio;
2962c6131460SPaul Saab     int			bus, target;
29631fe6c4eeSScott Long     int			physical;
2964440baa08SPaul Saab 
2965440baa08SPaul Saab     sc = cam_sim_softc(sim);
2966c6131460SPaul Saab     bus = cam_sim_bus(sim);
2967440baa08SPaul Saab     csio = (struct ccb_scsiio *)&ccb->csio;
2968440baa08SPaul Saab     target = csio->ccb_h.target_id;
29691fe6c4eeSScott Long     physical = CISS_IS_PHYSICAL(bus);
2970440baa08SPaul Saab 
29713a31b7ebSMike Smith     switch (ccb->ccb_h.func_code) {
29723a31b7ebSMike Smith 
29733a31b7ebSMike Smith 	/* perform SCSI I/O */
29743a31b7ebSMike Smith     case XPT_SCSI_IO:
2975440baa08SPaul Saab 	if (!ciss_cam_action_io(sim, csio))
29763a31b7ebSMike Smith 	    return;
29773a31b7ebSMike Smith 	break;
29783a31b7ebSMike Smith 
29793a31b7ebSMike Smith 	/* perform geometry calculations */
29803a31b7ebSMike Smith     case XPT_CALC_GEOMETRY:
29813a31b7ebSMike Smith     {
29823a31b7ebSMike Smith 	struct ccb_calc_geometry	*ccg = &ccb->ccg;
29831fe6c4eeSScott Long 	struct ciss_ldrive		*ld;
29843a31b7ebSMike Smith 
29853a31b7ebSMike Smith 	debug(1, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
29863a31b7ebSMike Smith 
29871fe6c4eeSScott Long 	ld = NULL;
29881fe6c4eeSScott Long 	if (!physical)
29891fe6c4eeSScott Long 	    ld = &sc->ciss_logical[bus][target];
29901fe6c4eeSScott Long 
29913a31b7ebSMike Smith 	/*
2992440baa08SPaul Saab 	 * Use the cached geometry settings unless the fault tolerance
2993440baa08SPaul Saab 	 * is invalid.
29943a31b7ebSMike Smith 	 */
29951fe6c4eeSScott Long 	if (physical || ld->cl_geometry.fault_tolerance == 0xFF) {
2996440baa08SPaul Saab 	    u_int32_t			secs_per_cylinder;
2997440baa08SPaul Saab 
29983a31b7ebSMike Smith 	    ccg->heads = 255;
29993a31b7ebSMike Smith 	    ccg->secs_per_track = 32;
30003a31b7ebSMike Smith 	    secs_per_cylinder = ccg->heads * ccg->secs_per_track;
30013a31b7ebSMike Smith 	    ccg->cylinders = ccg->volume_size / secs_per_cylinder;
3002440baa08SPaul Saab 	} else {
3003440baa08SPaul Saab 	    ccg->heads = ld->cl_geometry.heads;
3004440baa08SPaul Saab 	    ccg->secs_per_track = ld->cl_geometry.sectors;
3005440baa08SPaul Saab 	    ccg->cylinders = ntohs(ld->cl_geometry.cylinders);
3006440baa08SPaul Saab 	}
30073a31b7ebSMike Smith 	ccb->ccb_h.status = CAM_REQ_CMP;
30083a31b7ebSMike Smith         break;
30093a31b7ebSMike Smith     }
30103a31b7ebSMike Smith 
30113a31b7ebSMike Smith 	/* handle path attribute inquiry */
30123a31b7ebSMike Smith     case XPT_PATH_INQ:
30133a31b7ebSMike Smith     {
30143a31b7ebSMike Smith 	struct ccb_pathinq	*cpi = &ccb->cpi;
3015bdde5705SSean Bruno 	int			sg_length;
30163a31b7ebSMike Smith 
30173a31b7ebSMike Smith 	debug(1, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
30183a31b7ebSMike Smith 
30193a31b7ebSMike Smith 	cpi->version_num = 1;
30203a31b7ebSMike Smith 	cpi->hba_inquiry = PI_TAG_ABLE;	/* XXX is this correct? */
30213a31b7ebSMike Smith 	cpi->target_sprt = 0;
30223a31b7ebSMike Smith 	cpi->hba_misc = 0;
3023e3edca22SSean Bruno 	cpi->max_target = sc->ciss_cfg->max_logical_supported;
30243a31b7ebSMike Smith 	cpi->max_lun = 0;		/* 'logical drive' channel only */
3025e3edca22SSean Bruno 	cpi->initiator_id = sc->ciss_cfg->max_logical_supported;
30263a31b7ebSMike Smith 	strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
30273a31b7ebSMike Smith         strncpy(cpi->hba_vid, "msmith@freebsd.org", HBA_IDLEN);
30283a31b7ebSMike Smith         strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
30293a31b7ebSMike Smith         cpi->unit_number = cam_sim_unit(sim);
30303a31b7ebSMike Smith         cpi->bus_id = cam_sim_bus(sim);
30313a31b7ebSMike Smith 	cpi->base_transfer_speed = 132 * 1024;	/* XXX what to set this to? */
3032fa9ed865SMatt Jacob 	cpi->transport = XPORT_SPI;
3033fa9ed865SMatt Jacob 	cpi->transport_version = 2;
3034fa9ed865SMatt Jacob 	cpi->protocol = PROTO_SCSI;
3035fa9ed865SMatt Jacob 	cpi->protocol_version = SCSI_REV_2;
3036bdde5705SSean Bruno 	if (sc->ciss_cfg->max_sg_length == 0) {
3037e8e4cdbaSSean Bruno 		sg_length = 17;
3038bdde5705SSean Bruno 	} else {
3039bdde5705SSean Bruno 	/* XXX Fix for ZMR cards that advertise max_sg_length == 32
3040bdde5705SSean Bruno 	 * Confusing bit here. max_sg_length is usually a power of 2. We always
3041bdde5705SSean Bruno 	 * need to subtract 1 to account for partial pages. Then we need to
3042bdde5705SSean Bruno 	 * align on a valid PAGE_SIZE so we round down to the nearest power of 2.
3043bdde5705SSean Bruno 	 * Add 1 so we can then subtract it out in the assignment to maxio.
3044bdde5705SSean Bruno 	 * The reason for all these shenanigans is to create a maxio value that
3045bdde5705SSean Bruno 	 * creates IO operations to volumes that yield consistent operations
3046bdde5705SSean Bruno 	 * with good performance.
3047bdde5705SSean Bruno 	 */
3048bdde5705SSean Bruno 		sg_length = sc->ciss_cfg->max_sg_length - 1;
3049bdde5705SSean Bruno 		sg_length = (1 << (fls(sg_length) - 1)) + 1;
3050bdde5705SSean Bruno 	}
3051bdde5705SSean Bruno 	cpi->maxio = (min(CISS_MAX_SG_ELEMENTS, sg_length) - 1) * PAGE_SIZE;
30523a31b7ebSMike Smith 	ccb->ccb_h.status = CAM_REQ_CMP;
30533a31b7ebSMike Smith 	break;
30543a31b7ebSMike Smith     }
30553a31b7ebSMike Smith 
30563a31b7ebSMike Smith     case XPT_GET_TRAN_SETTINGS:
30573a31b7ebSMike Smith     {
30583a31b7ebSMike Smith 	struct ccb_trans_settings	*cts = &ccb->cts;
30593a31b7ebSMike Smith 	int				bus, target;
306063b9228bSScott Long 	struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
306163b9228bSScott Long 	struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
30623a31b7ebSMike Smith 
30633a31b7ebSMike Smith 	bus = cam_sim_bus(sim);
30643a31b7ebSMike Smith 	target = cts->ccb_h.target_id;
30653a31b7ebSMike Smith 
30663a31b7ebSMike Smith 	debug(1, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target);
30673a31b7ebSMike Smith 	/* disconnect always OK */
3068fa9ed865SMatt Jacob 	cts->protocol = PROTO_SCSI;
3069fa9ed865SMatt Jacob 	cts->protocol_version = SCSI_REV_2;
3070fa9ed865SMatt Jacob 	cts->transport = XPORT_SPI;
3071fa9ed865SMatt Jacob 	cts->transport_version = 2;
3072fa9ed865SMatt Jacob 
3073fa9ed865SMatt Jacob 	spi->valid = CTS_SPI_VALID_DISC;
3074fa9ed865SMatt Jacob 	spi->flags = CTS_SPI_FLAGS_DISC_ENB;
30753a31b7ebSMike Smith 
307663b9228bSScott Long 	scsi->valid = CTS_SCSI_VALID_TQ;
307763b9228bSScott Long 	scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
307863b9228bSScott Long 
30793a31b7ebSMike Smith 	cts->ccb_h.status = CAM_REQ_CMP;
30803a31b7ebSMike Smith 	break;
30813a31b7ebSMike Smith     }
30823a31b7ebSMike Smith 
30833a31b7ebSMike Smith     default:		/* we can't do this */
30843a31b7ebSMike Smith 	debug(1, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
30853a31b7ebSMike Smith 	ccb->ccb_h.status = CAM_REQ_INVALID;
30863a31b7ebSMike Smith 	break;
30873a31b7ebSMike Smith     }
30883a31b7ebSMike Smith 
30893a31b7ebSMike Smith     xpt_done(ccb);
30903a31b7ebSMike Smith }
30913a31b7ebSMike Smith 
30923a31b7ebSMike Smith /************************************************************************
30933a31b7ebSMike Smith  * Handle a CAM SCSI I/O request.
30943a31b7ebSMike Smith  */
30953a31b7ebSMike Smith static int
30963a31b7ebSMike Smith ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio)
30973a31b7ebSMike Smith {
30983a31b7ebSMike Smith     struct ciss_softc	*sc;
30993a31b7ebSMike Smith     int			bus, target;
31003a31b7ebSMike Smith     struct ciss_request	*cr;
31013a31b7ebSMike Smith     struct ciss_command	*cc;
31023a31b7ebSMike Smith     int			error;
31033a31b7ebSMike Smith 
31043a31b7ebSMike Smith     sc = cam_sim_softc(sim);
31053a31b7ebSMike Smith     bus = cam_sim_bus(sim);
31063a31b7ebSMike Smith     target = csio->ccb_h.target_id;
31073a31b7ebSMike Smith 
31083a31b7ebSMike Smith     debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, csio->ccb_h.target_lun);
31093a31b7ebSMike Smith 
31103a31b7ebSMike Smith     /* check that the CDB pointer is not to a physical address */
31113a31b7ebSMike Smith     if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
31123a31b7ebSMike Smith 	debug(3, "  CDB pointer is to physical address");
31133a31b7ebSMike Smith 	csio->ccb_h.status = CAM_REQ_CMP_ERR;
31143a31b7ebSMike Smith     }
31153a31b7ebSMike Smith 
31163a31b7ebSMike Smith     /* abandon aborted ccbs or those that have failed validation */
31173a31b7ebSMike Smith     if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
31183a31b7ebSMike Smith 	debug(3, "abandoning CCB due to abort/validation failure");
31193a31b7ebSMike Smith 	return(EINVAL);
31203a31b7ebSMike Smith     }
31213a31b7ebSMike Smith 
31223a31b7ebSMike Smith     /* handle emulation of some SCSI commands ourself */
31233a31b7ebSMike Smith     if (ciss_cam_emulate(sc, csio))
31243a31b7ebSMike Smith 	return(0);
31253a31b7ebSMike Smith 
31263a31b7ebSMike Smith     /*
31273a31b7ebSMike Smith      * Get a request to manage this command.  If we can't, return the
31283a31b7ebSMike Smith      * ccb, freeze the queue and flag so that we unfreeze it when a
31293a31b7ebSMike Smith      * request completes.
31303a31b7ebSMike Smith      */
31313a31b7ebSMike Smith     if ((error = ciss_get_request(sc, &cr)) != 0) {
31321fe6c4eeSScott Long 	xpt_freeze_simq(sim, 1);
313395dab4f2SAlexander Motin 	sc->ciss_flags |= CISS_FLAG_BUSY;
31343a31b7ebSMike Smith 	csio->ccb_h.status |= CAM_REQUEUE_REQ;
31353a31b7ebSMike Smith 	return(error);
31363a31b7ebSMike Smith     }
31373a31b7ebSMike Smith 
31383a31b7ebSMike Smith     /*
31393a31b7ebSMike Smith      * Build the command.
31403a31b7ebSMike Smith      */
31412fdaa90dSScott Long     cc = cr->cr_cc;
3142dd0b4fb6SKonstantin Belousov     cr->cr_data = csio;
31433a31b7ebSMike Smith     cr->cr_length = csio->dxfer_len;
31443a31b7ebSMike Smith     cr->cr_complete = ciss_cam_complete;
31453a31b7ebSMike Smith     cr->cr_private = csio;
31463a31b7ebSMike Smith 
3147c6131460SPaul Saab     /*
3148c6131460SPaul Saab      * Target the right logical volume.
3149c6131460SPaul Saab      */
31501fe6c4eeSScott Long     if (CISS_IS_PHYSICAL(bus))
31511fe6c4eeSScott Long 	cc->header.address =
31521fe6c4eeSScott Long 	    sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_address;
31531fe6c4eeSScott Long     else
31541fe6c4eeSScott Long 	cc->header.address =
31551fe6c4eeSScott Long 	    sc->ciss_logical[bus][target].cl_address;
31563a31b7ebSMike Smith     cc->cdb.cdb_length = csio->cdb_len;
31573a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
31583a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;	/* XXX ordered tags? */
31593a31b7ebSMike Smith     if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
3160dd0b4fb6SKonstantin Belousov 	cr->cr_flags = CISS_REQ_DATAOUT | CISS_REQ_CCB;
31613a31b7ebSMike Smith 	cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
31623a31b7ebSMike Smith     } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
3163dd0b4fb6SKonstantin Belousov 	cr->cr_flags = CISS_REQ_DATAIN | CISS_REQ_CCB;
31643a31b7ebSMike Smith 	cc->cdb.direction = CISS_CDB_DIRECTION_READ;
31653a31b7ebSMike Smith     } else {
3166dd0b4fb6SKonstantin Belousov 	cr->cr_data = NULL;
31673a31b7ebSMike Smith 	cr->cr_flags = 0;
31683a31b7ebSMike Smith 	cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
31693a31b7ebSMike Smith     }
31703a31b7ebSMike Smith     cc->cdb.timeout = (csio->ccb_h.timeout / 1000) + 1;
31713a31b7ebSMike Smith     if (csio->ccb_h.flags & CAM_CDB_POINTER) {
31723a31b7ebSMike Smith 	bcopy(csio->cdb_io.cdb_ptr, &cc->cdb.cdb[0], csio->cdb_len);
31733a31b7ebSMike Smith     } else {
31743a31b7ebSMike Smith 	bcopy(csio->cdb_io.cdb_bytes, &cc->cdb.cdb[0], csio->cdb_len);
31753a31b7ebSMike Smith     }
31763a31b7ebSMike Smith 
31773a31b7ebSMike Smith     /*
31783a31b7ebSMike Smith      * Submit the request to the adapter.
31793a31b7ebSMike Smith      *
31803a31b7ebSMike Smith      * Note that this may fail if we're unable to map the request (and
31813a31b7ebSMike Smith      * if we ever learn a transport layer other than simple, may fail
31823a31b7ebSMike Smith      * if the adapter rejects the command).
31833a31b7ebSMike Smith      */
31843a31b7ebSMike Smith     if ((error = ciss_start(cr)) != 0) {
31851fe6c4eeSScott Long 	xpt_freeze_simq(sim, 1);
3186639717c8SPaul Saab 	csio->ccb_h.status |= CAM_RELEASE_SIMQ;
31870a65b79fSAlexander Motin 	if (error == EINPROGRESS) {
3188639717c8SPaul Saab 	    error = 0;
3189639717c8SPaul Saab 	} else {
31903a31b7ebSMike Smith 	    csio->ccb_h.status |= CAM_REQUEUE_REQ;
31913a31b7ebSMike Smith 	    ciss_release_request(cr);
3192639717c8SPaul Saab 	}
31933a31b7ebSMike Smith 	return(error);
31943a31b7ebSMike Smith     }
31953a31b7ebSMike Smith 
31963a31b7ebSMike Smith     return(0);
31973a31b7ebSMike Smith }
31983a31b7ebSMike Smith 
31993a31b7ebSMike Smith /************************************************************************
32003a31b7ebSMike Smith  * Emulate SCSI commands the adapter doesn't handle as we might like.
32013a31b7ebSMike Smith  */
32023a31b7ebSMike Smith static int
32033a31b7ebSMike Smith ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio)
32043a31b7ebSMike Smith {
3205c6131460SPaul Saab     int		bus, target;
32063a31b7ebSMike Smith     u_int8_t	opcode;
32073a31b7ebSMike Smith 
32083a31b7ebSMike Smith     target = csio->ccb_h.target_id;
3209c6131460SPaul Saab     bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
32103a31b7ebSMike Smith     opcode = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
32113a31b7ebSMike Smith 	*(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0];
32123a31b7ebSMike Smith 
32131fe6c4eeSScott Long     if (CISS_IS_PHYSICAL(bus)) {
32141fe6c4eeSScott Long 	if (sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_online != 1) {
32150a65b79fSAlexander Motin 	    csio->ccb_h.status |= CAM_SEL_TIMEOUT;
32161fe6c4eeSScott Long 	    xpt_done((union ccb *)csio);
32171fe6c4eeSScott Long 	    return(1);
32181fe6c4eeSScott Long 	} else
32191fe6c4eeSScott Long 	    return(0);
32201fe6c4eeSScott Long     }
32211fe6c4eeSScott Long 
32223a31b7ebSMike Smith     /*
32236f71a953SPaul Saab      * Handle requests for volumes that don't exist or are not online.
32246f71a953SPaul Saab      * A selection timeout is slightly better than an illegal request.
32256f71a953SPaul Saab      * Other errors might be better.
32263a31b7ebSMike Smith      */
32276f71a953SPaul Saab     if (sc->ciss_logical[bus][target].cl_status != CISS_LD_ONLINE) {
32280a65b79fSAlexander Motin 	csio->ccb_h.status |= CAM_SEL_TIMEOUT;
32293a31b7ebSMike Smith 	xpt_done((union ccb *)csio);
32303a31b7ebSMike Smith 	return(1);
32313a31b7ebSMike Smith     }
32323a31b7ebSMike Smith 
32333a31b7ebSMike Smith     /* if we have to fake Synchronise Cache */
32343a31b7ebSMike Smith     if (sc->ciss_flags & CISS_FLAG_FAKE_SYNCH) {
32353a31b7ebSMike Smith 	/*
32363a31b7ebSMike Smith 	 * If this is a Synchronise Cache command, typically issued when
32373a31b7ebSMike Smith 	 * a device is closed, flush the adapter and complete now.
32383a31b7ebSMike Smith 	 */
32393a31b7ebSMike Smith 	if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
32403a31b7ebSMike Smith 	     *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE) {
32413a31b7ebSMike Smith 	    ciss_flush_adapter(sc);
32420a65b79fSAlexander Motin 	    csio->ccb_h.status |= CAM_REQ_CMP;
32433a31b7ebSMike Smith 	    xpt_done((union ccb *)csio);
32443a31b7ebSMike Smith 	    return(1);
32453a31b7ebSMike Smith 	}
32463a31b7ebSMike Smith     }
32473a31b7ebSMike Smith 
3248a1abcc4fSSean Bruno     /*
3249a1abcc4fSSean Bruno      * A CISS target can only ever have one lun per target. REPORT_LUNS requires
3250a1abcc4fSSean Bruno      * at least one LUN field to be pre created for us, so snag it and fill in
3251a1abcc4fSSean Bruno      * the least significant byte indicating 1 LUN here.  Emulate the command
3252a1abcc4fSSean Bruno      * return to shut up warning on console of a CDB error.  swb
3253a1abcc4fSSean Bruno      */
3254a1abcc4fSSean Bruno     if (opcode == REPORT_LUNS && csio->dxfer_len > 0) {
3255a1abcc4fSSean Bruno        csio->data_ptr[3] = 8;
3256a1abcc4fSSean Bruno        csio->ccb_h.status |= CAM_REQ_CMP;
3257a1abcc4fSSean Bruno        xpt_done((union ccb *)csio);
3258a1abcc4fSSean Bruno        return(1);
3259a1abcc4fSSean Bruno     }
3260a1abcc4fSSean Bruno 
32613a31b7ebSMike Smith     return(0);
32623a31b7ebSMike Smith }
32633a31b7ebSMike Smith 
32643a31b7ebSMike Smith /************************************************************************
32653a31b7ebSMike Smith  * Check for possibly-completed commands.
32663a31b7ebSMike Smith  */
32673a31b7ebSMike Smith static void
32683a31b7ebSMike Smith ciss_cam_poll(struct cam_sim *sim)
32693a31b7ebSMike Smith {
327022657ce1SScott Long     cr_qhead_t qh;
32713a31b7ebSMike Smith     struct ciss_softc	*sc = cam_sim_softc(sim);
32723a31b7ebSMike Smith 
32733a31b7ebSMike Smith     debug_called(2);
32743a31b7ebSMike Smith 
327522657ce1SScott Long     STAILQ_INIT(&qh);
327622657ce1SScott Long     if (sc->ciss_perf)
327722657ce1SScott Long 	ciss_perf_done(sc, &qh);
327822657ce1SScott Long     else
327922657ce1SScott Long 	ciss_done(sc, &qh);
328022657ce1SScott Long     ciss_complete(sc, &qh);
32813a31b7ebSMike Smith }
32823a31b7ebSMike Smith 
32833a31b7ebSMike Smith /************************************************************************
32843a31b7ebSMike Smith  * Handle completion of a command - pass results back through the CCB
32853a31b7ebSMike Smith  */
32863a31b7ebSMike Smith static void
32873a31b7ebSMike Smith ciss_cam_complete(struct ciss_request *cr)
32883a31b7ebSMike Smith {
32893a31b7ebSMike Smith     struct ciss_softc		*sc;
32903a31b7ebSMike Smith     struct ciss_command		*cc;
32913a31b7ebSMike Smith     struct ciss_error_info	*ce;
32923a31b7ebSMike Smith     struct ccb_scsiio		*csio;
32933a31b7ebSMike Smith     int				scsi_status;
32943a31b7ebSMike Smith     int				command_status;
32953a31b7ebSMike Smith 
32963a31b7ebSMike Smith     debug_called(2);
32973a31b7ebSMike Smith 
32983a31b7ebSMike Smith     sc = cr->cr_sc;
32992fdaa90dSScott Long     cc = cr->cr_cc;
33003a31b7ebSMike Smith     ce = (struct ciss_error_info *)&(cc->sg[0]);
33013a31b7ebSMike Smith     csio = (struct ccb_scsiio *)cr->cr_private;
33023a31b7ebSMike Smith 
33033a31b7ebSMike Smith     /*
33043a31b7ebSMike Smith      * Extract status values from request.
33053a31b7ebSMike Smith      */
33063a31b7ebSMike Smith     ciss_report_request(cr, &command_status, &scsi_status);
33073a31b7ebSMike Smith     csio->scsi_status = scsi_status;
33083a31b7ebSMike Smith 
33093a31b7ebSMike Smith     /*
33103a31b7ebSMike Smith      * Handle specific SCSI status values.
33113a31b7ebSMike Smith      */
33123a31b7ebSMike Smith     switch(scsi_status) {
33133a31b7ebSMike Smith 	/* no status due to adapter error */
33143a31b7ebSMike Smith     case -1:
33153a31b7ebSMike Smith 	debug(0, "adapter error");
33160a65b79fSAlexander Motin 	csio->ccb_h.status |= CAM_REQ_CMP_ERR;
33173a31b7ebSMike Smith 	break;
33183a31b7ebSMike Smith 
33193a31b7ebSMike Smith 	/* no status due to command completed OK */
33203a31b7ebSMike Smith     case SCSI_STATUS_OK:		/* CISS_SCSI_STATUS_GOOD */
33213a31b7ebSMike Smith 	debug(2, "SCSI_STATUS_OK");
33220a65b79fSAlexander Motin 	csio->ccb_h.status |= CAM_REQ_CMP;
33233a31b7ebSMike Smith 	break;
33243a31b7ebSMike Smith 
33253a31b7ebSMike Smith 	/* check condition, sense data included */
33263a31b7ebSMike Smith     case SCSI_STATUS_CHECK_COND:	/* CISS_SCSI_STATUS_CHECK_CONDITION */
3327c6131460SPaul Saab 	debug(0, "SCSI_STATUS_CHECK_COND  sense size %d  resid %d\n",
33283a31b7ebSMike Smith 	      ce->sense_length, ce->residual_count);
33293a31b7ebSMike Smith 	bzero(&csio->sense_data, SSD_FULL_SIZE);
33303a31b7ebSMike Smith 	bcopy(&ce->sense_info[0], &csio->sense_data, ce->sense_length);
33311cc052e8SKenneth D. Merry 	if (csio->sense_len > ce->sense_length)
33321cc052e8SKenneth D. Merry 		csio->sense_resid = csio->sense_len - ce->sense_length;
33331cc052e8SKenneth D. Merry 	else
33341cc052e8SKenneth D. Merry 		csio->sense_resid = 0;
33353a31b7ebSMike Smith 	csio->resid = ce->residual_count;
33360a65b79fSAlexander Motin 	csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
33373a31b7ebSMike Smith #ifdef CISS_DEBUG
33383a31b7ebSMike Smith 	{
33393a31b7ebSMike Smith 	    struct scsi_sense_data	*sns = (struct scsi_sense_data *)&ce->sense_info[0];
33401cc052e8SKenneth D. Merry 	    debug(0, "sense key %x", scsi_get_sense_key(sns, csio->sense_len -
33411cc052e8SKenneth D. Merry 		  csio->sense_resid, /*show_errors*/ 1));
33423a31b7ebSMike Smith 	}
33433a31b7ebSMike Smith #endif
33443a31b7ebSMike Smith 	break;
33453a31b7ebSMike Smith 
33463a31b7ebSMike Smith     case SCSI_STATUS_BUSY:		/* CISS_SCSI_STATUS_BUSY */
33473a31b7ebSMike Smith 	debug(0, "SCSI_STATUS_BUSY");
33480a65b79fSAlexander Motin 	csio->ccb_h.status |= CAM_SCSI_BUSY;
33493a31b7ebSMike Smith 	break;
33503a31b7ebSMike Smith 
33513a31b7ebSMike Smith     default:
33523a31b7ebSMike Smith 	debug(0, "unknown status 0x%x", csio->scsi_status);
33530a65b79fSAlexander Motin 	csio->ccb_h.status |= CAM_REQ_CMP_ERR;
33543a31b7ebSMike Smith 	break;
33553a31b7ebSMike Smith     }
33563a31b7ebSMike Smith 
33573a31b7ebSMike Smith     /* handle post-command fixup */
33583a31b7ebSMike Smith     ciss_cam_complete_fixup(sc, csio);
33593a31b7ebSMike Smith 
33603a31b7ebSMike Smith     ciss_release_request(cr);
336195dab4f2SAlexander Motin     if (sc->ciss_flags & CISS_FLAG_BUSY) {
336295dab4f2SAlexander Motin 	sc->ciss_flags &= ~CISS_FLAG_BUSY;
336395dab4f2SAlexander Motin 	if (csio->ccb_h.status & CAM_RELEASE_SIMQ)
336495dab4f2SAlexander Motin 	    xpt_release_simq(xpt_path_sim(csio->ccb_h.path), 0);
336595dab4f2SAlexander Motin 	else
336695dab4f2SAlexander Motin 	    csio->ccb_h.status |= CAM_RELEASE_SIMQ;
336795dab4f2SAlexander Motin     }
336822657ce1SScott Long     xpt_done((union ccb *)csio);
33693a31b7ebSMike Smith }
33703a31b7ebSMike Smith 
33713a31b7ebSMike Smith /********************************************************************************
33723a31b7ebSMike Smith  * Fix up the result of some commands here.
33733a31b7ebSMike Smith  */
33743a31b7ebSMike Smith static void
33753a31b7ebSMike Smith ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio)
33763a31b7ebSMike Smith {
33773a31b7ebSMike Smith     struct scsi_inquiry_data	*inq;
33783a31b7ebSMike Smith     struct ciss_ldrive		*cl;
337957ae362cSAlexander Motin     uint8_t			*cdb;
3380c6131460SPaul Saab     int				bus, target;
33813a31b7ebSMike Smith 
338257ae362cSAlexander Motin     cdb = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
338357ae362cSAlexander Motin 	 (uint8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes;
338457ae362cSAlexander Motin     if (cdb[0] == INQUIRY &&
338557ae362cSAlexander Motin 	(cdb[1] & SI_EVPD) == 0 &&
338657ae362cSAlexander Motin 	(csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN &&
338757ae362cSAlexander Motin 	csio->dxfer_len >= SHORT_INQUIRY_LENGTH) {
33883a31b7ebSMike Smith 
33893a31b7ebSMike Smith 	inq = (struct scsi_inquiry_data *)csio->data_ptr;
33903a31b7ebSMike Smith 	target = csio->ccb_h.target_id;
3391c6131460SPaul Saab 	bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
33921fe6c4eeSScott Long 
33931fe6c4eeSScott Long 	/*
3394*0fb31ed0SSean Bruno 	 * If the controller is in JBOD mode, there are no logical volumes.
3395*0fb31ed0SSean Bruno 	 * Let the disks be probed and dealt with via CAM.  Else, mask off
3396*0fb31ed0SSean Bruno 	 * the physical disks and setup the parts of the inq structure for
3397*0fb31ed0SSean Bruno 	 * the logical volume.  swb
33981fe6c4eeSScott Long 	 */
3399*0fb31ed0SSean Bruno 	if( !(sc->ciss_id->PowerUPNvramFlags & PWR_UP_FLAG_JBOD_ENABLED)){
34001fe6c4eeSScott Long 		if (CISS_IS_PHYSICAL(bus)) {
34011fe6c4eeSScott Long 	    		if (SID_TYPE(inq) == T_DIRECT)
34021fe6c4eeSScott Long 				inq->device = (inq->device & 0xe0) | T_NODEVICE;
34031fe6c4eeSScott Long 	    		return;
34041fe6c4eeSScott Long 		}
3405c6131460SPaul Saab 		cl = &sc->ciss_logical[bus][target];
34063a31b7ebSMike Smith 
34071e8b29a4SSean Bruno 		padstr(inq->vendor, "HP",
3408a9112e5dSSean Bruno 	       		SID_VENDOR_SIZE);
3409a9112e5dSSean Bruno 		padstr(inq->product,
3410a9112e5dSSean Bruno 	       		ciss_name_ldrive_org(cl->cl_ldrive->fault_tolerance),
3411a9112e5dSSean Bruno 	       		SID_PRODUCT_SIZE);
3412a9112e5dSSean Bruno 		padstr(inq->revision,
3413a9112e5dSSean Bruno 	       		ciss_name_ldrive_status(cl->cl_lstatus->status),
3414a9112e5dSSean Bruno 	       		SID_REVISION_SIZE);
34153a31b7ebSMike Smith 	}
34163a31b7ebSMike Smith     }
3417*0fb31ed0SSean Bruno }
34183a31b7ebSMike Smith 
34193a31b7ebSMike Smith 
34203a31b7ebSMike Smith /********************************************************************************
34213a31b7ebSMike Smith  * Name the device at (target)
34223a31b7ebSMike Smith  *
34233a31b7ebSMike Smith  * XXX is this strictly correct?
34243a31b7ebSMike Smith  */
342537c84183SPoul-Henning Kamp static int
3426c6131460SPaul Saab ciss_name_device(struct ciss_softc *sc, int bus, int target)
34273a31b7ebSMike Smith {
34283a31b7ebSMike Smith     struct cam_periph	*periph;
3429739a9399SSean Bruno     struct cam_path	*path;
3430739a9399SSean Bruno     int			status;
34313a31b7ebSMike Smith 
3432d49f1379SPaul Saab     if (CISS_IS_PHYSICAL(bus))
34331fe6c4eeSScott Long 	return (0);
3434739a9399SSean Bruno 
3435739a9399SSean Bruno     status = xpt_create_path(&path, NULL, cam_sim_path(sc->ciss_cam_sim[bus]),
3436739a9399SSean Bruno 			     target, 0);
3437739a9399SSean Bruno 
3438739a9399SSean Bruno     if (status == CAM_REQ_CMP) {
343997ed09b5SSean Bruno 	mtx_lock(&sc->ciss_mtx);
3440739a9399SSean Bruno 	xpt_path_lock(path);
3441739a9399SSean Bruno 	periph = cam_periph_find(path, NULL);
344297ed09b5SSean Bruno 	xpt_path_unlock(path);
344397ed09b5SSean Bruno 	mtx_unlock(&sc->ciss_mtx);
344497ed09b5SSean Bruno 	xpt_free_path(path);
344597ed09b5SSean Bruno 	if (periph != NULL) {
34461fe6c4eeSScott Long 		sprintf(sc->ciss_logical[bus][target].cl_name, "%s%d",
34471fe6c4eeSScott Long 			periph->periph_name, periph->unit_number);
34483a31b7ebSMike Smith 		return(0);
34493a31b7ebSMike Smith 	}
345097ed09b5SSean Bruno     }
3451c6131460SPaul Saab     sc->ciss_logical[bus][target].cl_name[0] = 0;
34523a31b7ebSMike Smith     return(ENOENT);
34533a31b7ebSMike Smith }
34543a31b7ebSMike Smith 
34553a31b7ebSMike Smith /************************************************************************
34563a31b7ebSMike Smith  * Periodic status monitoring.
34573a31b7ebSMike Smith  */
34583a31b7ebSMike Smith static void
34593a31b7ebSMike Smith ciss_periodic(void *arg)
34603a31b7ebSMike Smith {
34613a31b7ebSMike Smith     struct ciss_softc	*sc;
3462e08d902aSMitsuru IWASAKI     struct ciss_request	*cr = NULL;
3463e08d902aSMitsuru IWASAKI     struct ciss_command	*cc = NULL;
3464e08d902aSMitsuru IWASAKI     int			error = 0;
34653a31b7ebSMike Smith 
34663a31b7ebSMike Smith     debug_called(1);
34673a31b7ebSMike Smith 
34683a31b7ebSMike Smith     sc = (struct ciss_softc *)arg;
34693a31b7ebSMike Smith 
34703a31b7ebSMike Smith     /*
34713a31b7ebSMike Smith      * Check the adapter heartbeat.
34723a31b7ebSMike Smith      */
34733a31b7ebSMike Smith     if (sc->ciss_cfg->heartbeat == sc->ciss_heartbeat) {
34743a31b7ebSMike Smith 	sc->ciss_heart_attack++;
34753a31b7ebSMike Smith 	debug(0, "adapter heart attack in progress 0x%x/%d",
34763a31b7ebSMike Smith 	      sc->ciss_heartbeat, sc->ciss_heart_attack);
34773a31b7ebSMike Smith 	if (sc->ciss_heart_attack == 3) {
34783a31b7ebSMike Smith 	    ciss_printf(sc, "ADAPTER HEARTBEAT FAILED\n");
3479e08d902aSMitsuru IWASAKI 	    ciss_disable_adapter(sc);
3480e08d902aSMitsuru IWASAKI 	    return;
34813a31b7ebSMike Smith 	}
34823a31b7ebSMike Smith     } else {
34833a31b7ebSMike Smith 	sc->ciss_heartbeat = sc->ciss_cfg->heartbeat;
34843a31b7ebSMike Smith 	sc->ciss_heart_attack = 0;
34853a31b7ebSMike Smith 	debug(3, "new heartbeat 0x%x", sc->ciss_heartbeat);
34863a31b7ebSMike Smith     }
34873a31b7ebSMike Smith 
34883a31b7ebSMike Smith     /*
3489e08d902aSMitsuru IWASAKI      * Send the NOP message and wait for a response.
3490e08d902aSMitsuru IWASAKI      */
34915a3c4d69SMitsuru IWASAKI     if (ciss_nop_message_heartbeat != 0 && (error = ciss_get_request(sc, &cr)) == 0) {
34922fdaa90dSScott Long 	cc = cr->cr_cc;
349354d02e0aSMitsuru IWASAKI 	cr->cr_complete = ciss_nop_complete;
3494e08d902aSMitsuru IWASAKI 	cc->cdb.cdb_length = 1;
3495e08d902aSMitsuru IWASAKI 	cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
3496e08d902aSMitsuru IWASAKI 	cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3497e08d902aSMitsuru IWASAKI 	cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
3498e08d902aSMitsuru IWASAKI 	cc->cdb.timeout = 0;
3499e08d902aSMitsuru IWASAKI 	cc->cdb.cdb[0] = CISS_OPCODE_MESSAGE_NOP;
3500e08d902aSMitsuru IWASAKI 
350154d02e0aSMitsuru IWASAKI 	if ((error = ciss_start(cr)) != 0) {
3502e08d902aSMitsuru IWASAKI 	    ciss_printf(sc, "SENDING NOP MESSAGE FAILED\n");
3503e08d902aSMitsuru IWASAKI 	}
3504e08d902aSMitsuru IWASAKI     }
3505e08d902aSMitsuru IWASAKI 
3506e08d902aSMitsuru IWASAKI     /*
35073a31b7ebSMike Smith      * If the notify event request has died for some reason, or has
35083a31b7ebSMike Smith      * not started yet, restart it.
35093a31b7ebSMike Smith      */
35103a31b7ebSMike Smith     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK)) {
35113a31b7ebSMike Smith 	debug(0, "(re)starting Event Notify chain");
35123a31b7ebSMike Smith 	ciss_notify_event(sc);
35133a31b7ebSMike Smith     }
35143a31b7ebSMike Smith 
35153a31b7ebSMike Smith     /*
35163a31b7ebSMike Smith      * Reschedule.
35173a31b7ebSMike Smith      */
3518975b7318SScott Long     callout_reset(&sc->ciss_periodic, CISS_HEARTBEAT_RATE * hz, ciss_periodic, sc);
35193a31b7ebSMike Smith }
35203a31b7ebSMike Smith 
352154d02e0aSMitsuru IWASAKI static void
352254d02e0aSMitsuru IWASAKI ciss_nop_complete(struct ciss_request *cr)
352354d02e0aSMitsuru IWASAKI {
352454d02e0aSMitsuru IWASAKI     struct ciss_softc		*sc;
35250aeee4bdSMitsuru IWASAKI     static int			first_time = 1;
352654d02e0aSMitsuru IWASAKI 
352754d02e0aSMitsuru IWASAKI     sc = cr->cr_sc;
352854d02e0aSMitsuru IWASAKI     if (ciss_report_request(cr, NULL, NULL) != 0) {
35290aeee4bdSMitsuru IWASAKI 	if (first_time == 1) {
35300aeee4bdSMitsuru IWASAKI 	    first_time = 0;
35310aeee4bdSMitsuru IWASAKI 	    ciss_printf(sc, "SENDING NOP MESSAGE FAILED (not logging anymore)\n");
35320aeee4bdSMitsuru IWASAKI 	}
353354d02e0aSMitsuru IWASAKI     }
353454d02e0aSMitsuru IWASAKI 
353554d02e0aSMitsuru IWASAKI     ciss_release_request(cr);
353654d02e0aSMitsuru IWASAKI }
353754d02e0aSMitsuru IWASAKI 
35383a31b7ebSMike Smith /************************************************************************
3539e08d902aSMitsuru IWASAKI  * Disable the adapter.
3540e08d902aSMitsuru IWASAKI  *
3541e08d902aSMitsuru IWASAKI  * The all requests in completed queue is failed with hardware error.
3542e08d902aSMitsuru IWASAKI  * This will cause failover in a multipath configuration.
3543e08d902aSMitsuru IWASAKI  */
3544e08d902aSMitsuru IWASAKI static void
3545e08d902aSMitsuru IWASAKI ciss_disable_adapter(struct ciss_softc *sc)
3546e08d902aSMitsuru IWASAKI {
354722657ce1SScott Long     cr_qhead_t			qh;
3548e08d902aSMitsuru IWASAKI     struct ciss_request		*cr;
3549e08d902aSMitsuru IWASAKI     struct ciss_command		*cc;
3550e08d902aSMitsuru IWASAKI     struct ciss_error_info	*ce;
355122657ce1SScott Long     int				i;
3552e08d902aSMitsuru IWASAKI 
3553e08d902aSMitsuru IWASAKI     CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
3554e08d902aSMitsuru IWASAKI     pci_disable_busmaster(sc->ciss_dev);
3555e08d902aSMitsuru IWASAKI     sc->ciss_flags &= ~CISS_FLAG_RUNNING;
3556e08d902aSMitsuru IWASAKI 
355722657ce1SScott Long     for (i = 1; i < sc->ciss_max_requests; i++) {
355822657ce1SScott Long 	cr = &sc->ciss_request[i];
355922657ce1SScott Long 	if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
356022657ce1SScott Long 	    continue;
3561e08d902aSMitsuru IWASAKI 
35622fdaa90dSScott Long 	cc = cr->cr_cc;
3563e08d902aSMitsuru IWASAKI 	ce = (struct ciss_error_info *)&(cc->sg[0]);
3564e08d902aSMitsuru IWASAKI 	ce->command_status = CISS_CMD_STATUS_HARDWARE_ERROR;
356522657ce1SScott Long 	ciss_enqueue_complete(cr, &qh);
3566e08d902aSMitsuru IWASAKI     }
3567e08d902aSMitsuru IWASAKI 
3568e08d902aSMitsuru IWASAKI     for (;;) {
356922657ce1SScott Long 	if ((cr = ciss_dequeue_complete(sc, &qh)) == NULL)
3570e08d902aSMitsuru IWASAKI 	    break;
3571e08d902aSMitsuru IWASAKI 
3572e08d902aSMitsuru IWASAKI 	/*
3573e08d902aSMitsuru IWASAKI 	 * If the request has a callback, invoke it.
3574e08d902aSMitsuru IWASAKI 	 */
3575e08d902aSMitsuru IWASAKI 	if (cr->cr_complete != NULL) {
3576e08d902aSMitsuru IWASAKI 	    cr->cr_complete(cr);
3577e08d902aSMitsuru IWASAKI 	    continue;
3578e08d902aSMitsuru IWASAKI 	}
3579e08d902aSMitsuru IWASAKI 
3580e08d902aSMitsuru IWASAKI 	/*
3581e08d902aSMitsuru IWASAKI 	 * If someone is sleeping on this request, wake them up.
3582e08d902aSMitsuru IWASAKI 	 */
3583e08d902aSMitsuru IWASAKI 	if (cr->cr_flags & CISS_REQ_SLEEP) {
3584e08d902aSMitsuru IWASAKI 	    cr->cr_flags &= ~CISS_REQ_SLEEP;
3585e08d902aSMitsuru IWASAKI 	    wakeup(cr);
3586e08d902aSMitsuru IWASAKI 	    continue;
3587e08d902aSMitsuru IWASAKI 	}
3588e08d902aSMitsuru IWASAKI     }
3589e08d902aSMitsuru IWASAKI }
3590e08d902aSMitsuru IWASAKI 
3591e08d902aSMitsuru IWASAKI /************************************************************************
35923a31b7ebSMike Smith  * Request a notification response from the adapter.
35933a31b7ebSMike Smith  *
35943a31b7ebSMike Smith  * If (cr) is NULL, this is the first request of the adapter, so
35953a31b7ebSMike Smith  * reset the adapter's message pointer and start with the oldest
35963a31b7ebSMike Smith  * message available.
35973a31b7ebSMike Smith  */
35983a31b7ebSMike Smith static void
35993a31b7ebSMike Smith ciss_notify_event(struct ciss_softc *sc)
36003a31b7ebSMike Smith {
36013a31b7ebSMike Smith     struct ciss_request		*cr;
36023a31b7ebSMike Smith     struct ciss_command		*cc;
36033a31b7ebSMike Smith     struct ciss_notify_cdb	*cnc;
36043a31b7ebSMike Smith     int				error;
36053a31b7ebSMike Smith 
36063a31b7ebSMike Smith     debug_called(1);
36073a31b7ebSMike Smith 
36083a31b7ebSMike Smith     cr = sc->ciss_periodic_notify;
36093a31b7ebSMike Smith 
36103a31b7ebSMike Smith     /* get a request if we don't already have one */
36113a31b7ebSMike Smith     if (cr == NULL) {
36123a31b7ebSMike Smith 	if ((error = ciss_get_request(sc, &cr)) != 0) {
36133a31b7ebSMike Smith 	    debug(0, "can't get notify event request");
36143a31b7ebSMike Smith 	    goto out;
36153a31b7ebSMike Smith 	}
36163a31b7ebSMike Smith 	sc->ciss_periodic_notify = cr;
36173a31b7ebSMike Smith 	cr->cr_complete = ciss_notify_complete;
36183a31b7ebSMike Smith 	debug(1, "acquired request %d", cr->cr_tag);
36193a31b7ebSMike Smith     }
36203a31b7ebSMike Smith 
36213a31b7ebSMike Smith     /*
36223a31b7ebSMike Smith      * Get a databuffer if we don't already have one, note that the
36233a31b7ebSMike Smith      * adapter command wants a larger buffer than the actual
36243a31b7ebSMike Smith      * structure.
36253a31b7ebSMike Smith      */
36263a31b7ebSMike Smith     if (cr->cr_data == NULL) {
36273a31b7ebSMike Smith 	if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
36283a31b7ebSMike Smith 	    debug(0, "can't get notify event request buffer");
36293a31b7ebSMike Smith 	    error = ENOMEM;
36303a31b7ebSMike Smith 	    goto out;
36313a31b7ebSMike Smith 	}
36323a31b7ebSMike Smith 	cr->cr_length = CISS_NOTIFY_DATA_SIZE;
36333a31b7ebSMike Smith     }
36343a31b7ebSMike Smith 
36353a31b7ebSMike Smith     /* re-setup the request's command (since we never release it) XXX overkill*/
36363a31b7ebSMike Smith     ciss_preen_command(cr);
36373a31b7ebSMike Smith 
36383a31b7ebSMike Smith     /* (re)build the notify event command */
36392fdaa90dSScott Long     cc = cr->cr_cc;
36403a31b7ebSMike Smith     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
36413a31b7ebSMike Smith     cc->header.address.physical.bus = 0;
36423a31b7ebSMike Smith     cc->header.address.physical.target = 0;
36433a31b7ebSMike Smith 
36443a31b7ebSMike Smith     cc->cdb.cdb_length = sizeof(*cnc);
36453a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
36463a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
36473a31b7ebSMike Smith     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
36483a31b7ebSMike Smith     cc->cdb.timeout = 0;	/* no timeout, we hope */
36493a31b7ebSMike Smith 
36503a31b7ebSMike Smith     cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
36513a31b7ebSMike Smith     bzero(cr->cr_data, CISS_NOTIFY_DATA_SIZE);
36523a31b7ebSMike Smith     cnc->opcode = CISS_OPCODE_READ;
36533a31b7ebSMike Smith     cnc->command = CISS_COMMAND_NOTIFY_ON_EVENT;
36543a31b7ebSMike Smith     cnc->timeout = 0;		/* no timeout, we hope */
36553a31b7ebSMike Smith     cnc->synchronous = 0;
36563a31b7ebSMike Smith     cnc->ordered = 0;
36573a31b7ebSMike Smith     cnc->seek_to_oldest = 0;
36582e80ca8aSPaul Saab     if ((sc->ciss_flags & CISS_FLAG_RUNNING) == 0)
36592e80ca8aSPaul Saab 	cnc->new_only = 1;
36602e80ca8aSPaul Saab     else
36613a31b7ebSMike Smith 	cnc->new_only = 0;
36623a31b7ebSMike Smith     cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
36633a31b7ebSMike Smith 
36643a31b7ebSMike Smith     /* submit the request */
36653a31b7ebSMike Smith     error = ciss_start(cr);
36663a31b7ebSMike Smith 
36673a31b7ebSMike Smith  out:
36683a31b7ebSMike Smith     if (error) {
36693a31b7ebSMike Smith 	if (cr != NULL) {
36703a31b7ebSMike Smith 	    if (cr->cr_data != NULL)
36713a31b7ebSMike Smith 		free(cr->cr_data, CISS_MALLOC_CLASS);
36723a31b7ebSMike Smith 	    ciss_release_request(cr);
36733a31b7ebSMike Smith 	}
36743a31b7ebSMike Smith 	sc->ciss_periodic_notify = NULL;
36753a31b7ebSMike Smith 	debug(0, "can't submit notify event request");
36763a31b7ebSMike Smith 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
36773a31b7ebSMike Smith     } else {
36783a31b7ebSMike Smith 	debug(1, "notify event submitted");
36793a31b7ebSMike Smith 	sc->ciss_flags |= CISS_FLAG_NOTIFY_OK;
36803a31b7ebSMike Smith     }
36813a31b7ebSMike Smith }
36823a31b7ebSMike Smith 
36833a31b7ebSMike Smith static void
36843a31b7ebSMike Smith ciss_notify_complete(struct ciss_request *cr)
36853a31b7ebSMike Smith {
36863a31b7ebSMike Smith     struct ciss_command	*cc;
36873a31b7ebSMike Smith     struct ciss_notify	*cn;
36883a31b7ebSMike Smith     struct ciss_softc	*sc;
36893a31b7ebSMike Smith     int			scsi_status;
36903a31b7ebSMike Smith     int			command_status;
36913a31b7ebSMike Smith     debug_called(1);
36923a31b7ebSMike Smith 
36932fdaa90dSScott Long     cc = cr->cr_cc;
36943a31b7ebSMike Smith     cn = (struct ciss_notify *)cr->cr_data;
36953a31b7ebSMike Smith     sc = cr->cr_sc;
36963a31b7ebSMike Smith 
36973a31b7ebSMike Smith     /*
36983a31b7ebSMike Smith      * Report request results, decode status.
36993a31b7ebSMike Smith      */
37003a31b7ebSMike Smith     ciss_report_request(cr, &command_status, &scsi_status);
37013a31b7ebSMike Smith 
37023a31b7ebSMike Smith     /*
37033a31b7ebSMike Smith      * Abort the chain on a fatal error.
37043a31b7ebSMike Smith      *
37053a31b7ebSMike Smith      * XXX which of these are actually errors?
37063a31b7ebSMike Smith      */
37073a31b7ebSMike Smith     if ((command_status != CISS_CMD_STATUS_SUCCESS) &&
37083a31b7ebSMike Smith 	(command_status != CISS_CMD_STATUS_TARGET_STATUS) &&
37093a31b7ebSMike Smith 	(command_status != CISS_CMD_STATUS_TIMEOUT)) {	/* XXX timeout? */
37103a31b7ebSMike Smith 	ciss_printf(sc, "fatal error in Notify Event request (%s)\n",
37113a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
37123a31b7ebSMike Smith 	ciss_release_request(cr);
37133a31b7ebSMike Smith 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
37143a31b7ebSMike Smith 	return;
37153a31b7ebSMike Smith     }
37163a31b7ebSMike Smith 
37173a31b7ebSMike Smith     /*
37183a31b7ebSMike Smith      * If the adapter gave us a text message, print it.
37193a31b7ebSMike Smith      */
37203a31b7ebSMike Smith     if (cn->message[0] != 0)
37213a31b7ebSMike Smith 	ciss_printf(sc, "*** %.80s\n", cn->message);
37223a31b7ebSMike Smith 
37233a31b7ebSMike Smith     debug(0, "notify event class %d subclass %d detail %d",
37243a31b7ebSMike Smith 		cn->class, cn->subclass, cn->detail);
37253a31b7ebSMike Smith 
37263a31b7ebSMike Smith     /*
37273a31b7ebSMike Smith      * If the response indicates that the notifier has been aborted,
37283a31b7ebSMike Smith      * release the notifier command.
37293a31b7ebSMike Smith      */
37303a31b7ebSMike Smith     if ((cn->class == CISS_NOTIFY_NOTIFIER) &&
37313a31b7ebSMike Smith 	(cn->subclass == CISS_NOTIFY_NOTIFIER_STATUS) &&
37323a31b7ebSMike Smith 	(cn->detail == 1)) {
37333a31b7ebSMike Smith 	debug(0, "notifier exiting");
37343a31b7ebSMike Smith 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
37353a31b7ebSMike Smith 	ciss_release_request(cr);
37363a31b7ebSMike Smith 	sc->ciss_periodic_notify = NULL;
37373a31b7ebSMike Smith 	wakeup(&sc->ciss_periodic_notify);
3738c6131460SPaul Saab     } else {
3739c6131460SPaul Saab 	/* Handle notify events in a kernel thread */
3740c6131460SPaul Saab 	ciss_enqueue_notify(cr);
3741c6131460SPaul Saab 	sc->ciss_periodic_notify = NULL;
3742c6131460SPaul Saab 	wakeup(&sc->ciss_periodic_notify);
3743c6131460SPaul Saab 	wakeup(&sc->ciss_notify);
37443a31b7ebSMike Smith     }
37453a31b7ebSMike Smith     /*
37463a31b7ebSMike Smith      * Send a new notify event command, if we're not aborting.
37473a31b7ebSMike Smith      */
37483a31b7ebSMike Smith     if (!(sc->ciss_flags & CISS_FLAG_ABORTING)) {
37493a31b7ebSMike Smith 	ciss_notify_event(sc);
37503a31b7ebSMike Smith     }
37513a31b7ebSMike Smith }
37523a31b7ebSMike Smith 
37533a31b7ebSMike Smith /************************************************************************
37543a31b7ebSMike Smith  * Abort the Notify Event chain.
37553a31b7ebSMike Smith  *
37563a31b7ebSMike Smith  * Note that we can't just abort the command in progress; we have to
37573a31b7ebSMike Smith  * explicitly issue an Abort Notify Event command in order for the
37583a31b7ebSMike Smith  * adapter to clean up correctly.
37593a31b7ebSMike Smith  *
37603a31b7ebSMike Smith  * If we are called with CISS_FLAG_ABORTING set in the adapter softc,
37613a31b7ebSMike Smith  * the chain will not restart itself.
37623a31b7ebSMike Smith  */
37633a31b7ebSMike Smith static int
37643a31b7ebSMike Smith ciss_notify_abort(struct ciss_softc *sc)
37653a31b7ebSMike Smith {
37663a31b7ebSMike Smith     struct ciss_request		*cr;
37673a31b7ebSMike Smith     struct ciss_command		*cc;
37683a31b7ebSMike Smith     struct ciss_notify_cdb	*cnc;
376922657ce1SScott Long     int				error, command_status, scsi_status;
37703a31b7ebSMike Smith 
37713a31b7ebSMike Smith     debug_called(1);
37723a31b7ebSMike Smith 
37733a31b7ebSMike Smith     cr = NULL;
37743a31b7ebSMike Smith     error = 0;
37753a31b7ebSMike Smith 
37763a31b7ebSMike Smith     /* verify that there's an outstanding command */
37773a31b7ebSMike Smith     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
37783a31b7ebSMike Smith 	goto out;
37793a31b7ebSMike Smith 
37803a31b7ebSMike Smith     /* get a command to issue the abort with */
37813a31b7ebSMike Smith     if ((error = ciss_get_request(sc, &cr)))
37823a31b7ebSMike Smith 	goto out;
37833a31b7ebSMike Smith 
37843a31b7ebSMike Smith     /* get a buffer for the result */
37853a31b7ebSMike Smith     if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
37863a31b7ebSMike Smith 	debug(0, "can't get notify event request buffer");
37873a31b7ebSMike Smith 	error = ENOMEM;
37883a31b7ebSMike Smith 	goto out;
37893a31b7ebSMike Smith     }
37903a31b7ebSMike Smith     cr->cr_length = CISS_NOTIFY_DATA_SIZE;
37913a31b7ebSMike Smith 
37923a31b7ebSMike Smith     /* build the CDB */
37932fdaa90dSScott Long     cc = cr->cr_cc;
37943a31b7ebSMike Smith     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
37953a31b7ebSMike Smith     cc->header.address.physical.bus = 0;
37963a31b7ebSMike Smith     cc->header.address.physical.target = 0;
37973a31b7ebSMike Smith     cc->cdb.cdb_length = sizeof(*cnc);
37983a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
37993a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
38003a31b7ebSMike Smith     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
38013a31b7ebSMike Smith     cc->cdb.timeout = 0;	/* no timeout, we hope */
38023a31b7ebSMike Smith 
38033a31b7ebSMike Smith     cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
38043a31b7ebSMike Smith     bzero(cnc, sizeof(*cnc));
38053a31b7ebSMike Smith     cnc->opcode = CISS_OPCODE_WRITE;
38063a31b7ebSMike Smith     cnc->command = CISS_COMMAND_ABORT_NOTIFY;
38073a31b7ebSMike Smith     cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
38083a31b7ebSMike Smith 
38093a31b7ebSMike Smith     ciss_print_request(cr);
38103a31b7ebSMike Smith 
38113a31b7ebSMike Smith     /*
38123a31b7ebSMike Smith      * Submit the request and wait for it to complete.
38133a31b7ebSMike Smith      */
38143a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
38153a31b7ebSMike Smith 	ciss_printf(sc, "Abort Notify Event command failed (%d)\n", error);
38163a31b7ebSMike Smith 	goto out;
38173a31b7ebSMike Smith     }
38183a31b7ebSMike Smith 
38193a31b7ebSMike Smith     /*
38203a31b7ebSMike Smith      * Check response.
38213a31b7ebSMike Smith      */
38223a31b7ebSMike Smith     ciss_report_request(cr, &command_status, &scsi_status);
38233a31b7ebSMike Smith     switch(command_status) {
38243a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:
38253a31b7ebSMike Smith 	break;
38263a31b7ebSMike Smith     case CISS_CMD_STATUS_INVALID_COMMAND:
38273a31b7ebSMike Smith 	/*
38283a31b7ebSMike Smith 	 * Some older adapters don't support the CISS version of this
38293a31b7ebSMike Smith 	 * command.  Fall back to using the BMIC version.
38303a31b7ebSMike Smith 	 */
38313a31b7ebSMike Smith 	error = ciss_notify_abort_bmic(sc);
38323a31b7ebSMike Smith 	if (error != 0)
38333a31b7ebSMike Smith 	    goto out;
38343a31b7ebSMike Smith 	break;
38353a31b7ebSMike Smith 
38363a31b7ebSMike Smith     case CISS_CMD_STATUS_TARGET_STATUS:
38373a31b7ebSMike Smith 	/*
38383a31b7ebSMike Smith 	 * This can happen if the adapter thinks there wasn't an outstanding
38393a31b7ebSMike Smith 	 * Notify Event command but we did.  We clean up here.
38403a31b7ebSMike Smith 	 */
38413a31b7ebSMike Smith 	if (scsi_status == CISS_SCSI_STATUS_CHECK_CONDITION) {
38423a31b7ebSMike Smith 	    if (sc->ciss_periodic_notify != NULL)
38433a31b7ebSMike Smith 		ciss_release_request(sc->ciss_periodic_notify);
38443a31b7ebSMike Smith 	    error = 0;
38453a31b7ebSMike Smith 	    goto out;
38463a31b7ebSMike Smith 	}
38473a31b7ebSMike Smith 	/* FALLTHROUGH */
38483a31b7ebSMike Smith 
38493a31b7ebSMike Smith     default:
38503a31b7ebSMike Smith 	ciss_printf(sc, "Abort Notify Event command failed (%s)\n",
38513a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
38523a31b7ebSMike Smith 	error = EIO;
38533a31b7ebSMike Smith 	goto out;
38543a31b7ebSMike Smith     }
38553a31b7ebSMike Smith 
38563a31b7ebSMike Smith     /*
38573a31b7ebSMike Smith      * Sleep waiting for the notifier command to complete.  Note
38583a31b7ebSMike Smith      * that if it doesn't, we may end up in a bad situation, since
38593a31b7ebSMike Smith      * the adapter may deliver it later.  Also note that the adapter
38603a31b7ebSMike Smith      * requires the Notify Event command to be cancelled in order to
38613a31b7ebSMike Smith      * maintain internal bookkeeping.
38623a31b7ebSMike Smith      */
38633a31b7ebSMike Smith     while (sc->ciss_periodic_notify != NULL) {
3864975b7318SScott Long 	error = msleep(&sc->ciss_periodic_notify, &sc->ciss_mtx, PRIBIO, "cissNEA", hz * 5);
38653a31b7ebSMike Smith 	if (error == EWOULDBLOCK) {
38663a31b7ebSMike Smith 	    ciss_printf(sc, "Notify Event command failed to abort, adapter may wedge.\n");
38673a31b7ebSMike Smith 	    break;
38683a31b7ebSMike Smith 	}
38693a31b7ebSMike Smith     }
38703a31b7ebSMike Smith 
38713a31b7ebSMike Smith  out:
38723a31b7ebSMike Smith     /* release the cancel request */
38733a31b7ebSMike Smith     if (cr != NULL) {
38743a31b7ebSMike Smith 	if (cr->cr_data != NULL)
38753a31b7ebSMike Smith 	    free(cr->cr_data, CISS_MALLOC_CLASS);
38763a31b7ebSMike Smith 	ciss_release_request(cr);
38773a31b7ebSMike Smith     }
38783a31b7ebSMike Smith     if (error == 0)
38793a31b7ebSMike Smith 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
38803a31b7ebSMike Smith     return(error);
38813a31b7ebSMike Smith }
38823a31b7ebSMike Smith 
38833a31b7ebSMike Smith /************************************************************************
38843a31b7ebSMike Smith  * Abort the Notify Event chain using a BMIC command.
38853a31b7ebSMike Smith  */
38863a31b7ebSMike Smith static int
38873a31b7ebSMike Smith ciss_notify_abort_bmic(struct ciss_softc *sc)
38883a31b7ebSMike Smith {
38893a31b7ebSMike Smith     struct ciss_request			*cr;
38903a31b7ebSMike Smith     int					error, command_status;
38913a31b7ebSMike Smith 
38923a31b7ebSMike Smith     debug_called(1);
38933a31b7ebSMike Smith 
38943a31b7ebSMike Smith     cr = NULL;
38953a31b7ebSMike Smith     error = 0;
38963a31b7ebSMike Smith 
38973a31b7ebSMike Smith     /* verify that there's an outstanding command */
38983a31b7ebSMike Smith     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
38993a31b7ebSMike Smith 	goto out;
39003a31b7ebSMike Smith 
39013a31b7ebSMike Smith     /*
39023a31b7ebSMike Smith      * Build a BMIC command to cancel the Notify on Event command.
39033a31b7ebSMike Smith      *
39043a31b7ebSMike Smith      * Note that we are sending a CISS opcode here.  Odd.
39053a31b7ebSMike Smith      */
39063a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_COMMAND_ABORT_NOTIFY,
39073a31b7ebSMike Smith 				       NULL, 0)) != 0)
39083a31b7ebSMike Smith 	goto out;
39093a31b7ebSMike Smith 
39103a31b7ebSMike Smith     /*
39113a31b7ebSMike Smith      * Submit the request and wait for it to complete.
39123a31b7ebSMike Smith      */
39133a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
39143a31b7ebSMike Smith 	ciss_printf(sc, "error sending BMIC Cancel Notify on Event command (%d)\n", error);
39153a31b7ebSMike Smith 	goto out;
39163a31b7ebSMike Smith     }
39173a31b7ebSMike Smith 
39183a31b7ebSMike Smith     /*
39193a31b7ebSMike Smith      * Check response.
39203a31b7ebSMike Smith      */
39213a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
39223a31b7ebSMike Smith     switch(command_status) {
39233a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:
39243a31b7ebSMike Smith 	break;
39253a31b7ebSMike Smith     default:
39263a31b7ebSMike Smith 	ciss_printf(sc, "error cancelling Notify on Event (%s)\n",
39273a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
39283a31b7ebSMike Smith 	error = EIO;
39293a31b7ebSMike Smith 	goto out;
39303a31b7ebSMike Smith     }
39313a31b7ebSMike Smith 
39323a31b7ebSMike Smith out:
39333a31b7ebSMike Smith     if (cr != NULL)
39343a31b7ebSMike Smith 	ciss_release_request(cr);
39353a31b7ebSMike Smith     return(error);
39363a31b7ebSMike Smith }
39373a31b7ebSMike Smith 
39383a31b7ebSMike Smith /************************************************************************
3939c6131460SPaul Saab  * Handle rescanning all the logical volumes when a notify event
3940c6131460SPaul Saab  * causes the drives to come online or offline.
3941c6131460SPaul Saab  */
3942c6131460SPaul Saab static void
3943c6131460SPaul Saab ciss_notify_rescan_logical(struct ciss_softc *sc)
3944c6131460SPaul Saab {
3945c6131460SPaul Saab     struct ciss_lun_report      *cll;
3946c6131460SPaul Saab     struct ciss_ldrive		*ld;
3947c6131460SPaul Saab     int                         i, j, ndrives;
3948c6131460SPaul Saab 
3949c6131460SPaul Saab     /*
3950c6131460SPaul Saab      * We must rescan all logical volumes to get the right logical
3951c6131460SPaul Saab      * drive address.
3952c6131460SPaul Saab      */
3953c6131460SPaul Saab     cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
3954e3edca22SSean Bruno                            sc->ciss_cfg->max_logical_supported);
3955c6131460SPaul Saab     if (cll == NULL)
3956c6131460SPaul Saab         return;
3957c6131460SPaul Saab 
3958c6131460SPaul Saab     ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
3959c6131460SPaul Saab 
3960c6131460SPaul Saab     /*
3961c6131460SPaul Saab      * Delete any of the drives which were destroyed by the
3962c6131460SPaul Saab      * firmware.
3963c6131460SPaul Saab      */
39641fe6c4eeSScott Long     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
3965e3edca22SSean Bruno 	for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
3966c6131460SPaul Saab 	    ld = &sc->ciss_logical[i][j];
3967c6131460SPaul Saab 
3968c6131460SPaul Saab 	    if (ld->cl_update == 0)
3969c6131460SPaul Saab 		continue;
3970c6131460SPaul Saab 
3971c6131460SPaul Saab 	    if (ld->cl_status != CISS_LD_ONLINE) {
3972c6131460SPaul Saab 		ciss_cam_rescan_target(sc, i, j);
3973c6131460SPaul Saab 		ld->cl_update = 0;
3974c6131460SPaul Saab 		if (ld->cl_ldrive)
3975c6131460SPaul Saab 		    free(ld->cl_ldrive, CISS_MALLOC_CLASS);
3976c6131460SPaul Saab 		if (ld->cl_lstatus)
3977c6131460SPaul Saab 		    free(ld->cl_lstatus, CISS_MALLOC_CLASS);
3978c6131460SPaul Saab 
3979c6131460SPaul Saab 		ld->cl_ldrive = NULL;
3980c6131460SPaul Saab 		ld->cl_lstatus = NULL;
3981c6131460SPaul Saab 	    }
3982c6131460SPaul Saab 	}
3983c6131460SPaul Saab     }
3984c6131460SPaul Saab 
3985c6131460SPaul Saab     /*
3986c6131460SPaul Saab      * Scan for new drives.
3987c6131460SPaul Saab      */
3988c6131460SPaul Saab     for (i = 0; i < ndrives; i++) {
3989c6131460SPaul Saab 	int	bus, target;
3990c6131460SPaul Saab 
3991c6131460SPaul Saab 	bus 	= CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
3992c6131460SPaul Saab 	target	= CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
3993c6131460SPaul Saab 	ld	= &sc->ciss_logical[bus][target];
3994c6131460SPaul Saab 
3995c6131460SPaul Saab 	if (ld->cl_update == 0)
3996c6131460SPaul Saab 		continue;
3997c6131460SPaul Saab 
39985cf5a430SPaul Saab 	ld->cl_update		= 0;
3999c6131460SPaul Saab 	ld->cl_address		= cll->lun[i];
4000c6131460SPaul Saab 	ld->cl_controller	= &sc->ciss_controllers[bus];
4001c6131460SPaul Saab 	if (ciss_identify_logical(sc, ld) == 0) {
4002c6131460SPaul Saab 	    ciss_cam_rescan_target(sc, bus, target);
4003c6131460SPaul Saab 	}
4004c6131460SPaul Saab     }
4005c6131460SPaul Saab     free(cll, CISS_MALLOC_CLASS);
4006c6131460SPaul Saab }
4007c6131460SPaul Saab 
4008c6131460SPaul Saab /************************************************************************
40093a31b7ebSMike Smith  * Handle a notify event relating to the status of a logical drive.
40103a31b7ebSMike Smith  *
40113a31b7ebSMike Smith  * XXX need to be able to defer some of these to properly handle
40123a31b7ebSMike Smith  *     calling the "ID Physical drive" command, unless the 'extended'
40133a31b7ebSMike Smith  *     drive IDs are always in BIG_MAP format.
40143a31b7ebSMike Smith  */
40153a31b7ebSMike Smith static void
40163a31b7ebSMike Smith ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn)
40173a31b7ebSMike Smith {
40183a31b7ebSMike Smith     struct ciss_ldrive	*ld;
40199d29c471SSean Bruno     int			bus, target;
40209d29c471SSean Bruno     int			rescan_ld;
40213a31b7ebSMike Smith 
40223a31b7ebSMike Smith     debug_called(2);
40233a31b7ebSMike Smith 
4024c6131460SPaul Saab     bus		= cn->device.physical.bus;
4025c6131460SPaul Saab     target	= cn->data.logical_status.logical_drive;
4026c6131460SPaul Saab     ld		= &sc->ciss_logical[bus][target];
40273a31b7ebSMike Smith 
40283a31b7ebSMike Smith     switch (cn->subclass) {
40293a31b7ebSMike Smith     case CISS_NOTIFY_LOGICAL_STATUS:
40303a31b7ebSMike Smith 	switch (cn->detail) {
40313a31b7ebSMike Smith 	case 0:
4032c6131460SPaul Saab 	    ciss_name_device(sc, bus, target);
40333a31b7ebSMike Smith 	    ciss_printf(sc, "logical drive %d (%s) changed status %s->%s, spare status 0x%b\n",
40343a31b7ebSMike Smith 			cn->data.logical_status.logical_drive, ld->cl_name,
40353a31b7ebSMike Smith 			ciss_name_ldrive_status(cn->data.logical_status.previous_state),
40363a31b7ebSMike Smith 			ciss_name_ldrive_status(cn->data.logical_status.new_state),
40373a31b7ebSMike Smith 			cn->data.logical_status.spare_state,
40383a31b7ebSMike Smith 			"\20\1configured\2rebuilding\3failed\4in use\5available\n");
40393a31b7ebSMike Smith 
40403a31b7ebSMike Smith 	    /*
40413a31b7ebSMike Smith 	     * Update our idea of the drive's status.
40423a31b7ebSMike Smith 	     */
40433a31b7ebSMike Smith 	    ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
4044d4aa427fSPaul Saab 	    if (ld->cl_lstatus != NULL)
40453a31b7ebSMike Smith 		ld->cl_lstatus->status = cn->data.logical_status.new_state;
40463a31b7ebSMike Smith 
4047d4aa427fSPaul Saab 	    /*
4048d4aa427fSPaul Saab 	     * Have CAM rescan the drive if its status has changed.
4049d4aa427fSPaul Saab 	     */
40509d29c471SSean Bruno             rescan_ld = (cn->data.logical_status.previous_state !=
40519d29c471SSean Bruno                          cn->data.logical_status.new_state) ? 1 : 0;
40529d29c471SSean Bruno 	    if (rescan_ld) {
4053c6131460SPaul Saab 		ld->cl_update = 1;
4054c6131460SPaul Saab 		ciss_notify_rescan_logical(sc);
4055c6131460SPaul Saab 	    }
4056d4aa427fSPaul Saab 
40573a31b7ebSMike Smith 	    break;
40583a31b7ebSMike Smith 
40593a31b7ebSMike Smith 	case 1:	/* logical drive has recognised new media, needs Accept Media Exchange */
4060c6131460SPaul Saab 	    ciss_name_device(sc, bus, target);
40613a31b7ebSMike Smith 	    ciss_printf(sc, "logical drive %d (%s) media exchanged, ready to go online\n",
40623a31b7ebSMike Smith 			cn->data.logical_status.logical_drive, ld->cl_name);
406388c78a38SPaul Saab 	    ciss_accept_media(sc, ld);
4064139233cbSPaul Saab 
4065139233cbSPaul Saab 	    ld->cl_update = 1;
4066139233cbSPaul Saab 	    ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
4067139233cbSPaul Saab 	    ciss_notify_rescan_logical(sc);
40683a31b7ebSMike Smith 	    break;
40693a31b7ebSMike Smith 
40703a31b7ebSMike Smith 	case 2:
40713a31b7ebSMike Smith 	case 3:
40723a31b7ebSMike Smith 	    ciss_printf(sc, "rebuild of logical drive %d (%s) failed due to %s error\n",
40733a31b7ebSMike Smith 			cn->data.rebuild_aborted.logical_drive,
4074c6131460SPaul Saab 			ld->cl_name,
40753a31b7ebSMike Smith 			(cn->detail == 2) ? "read" : "write");
40763a31b7ebSMike Smith 	    break;
40773a31b7ebSMike Smith 	}
40783a31b7ebSMike Smith 	break;
40793a31b7ebSMike Smith 
40803a31b7ebSMike Smith     case CISS_NOTIFY_LOGICAL_ERROR:
40813a31b7ebSMike Smith 	if (cn->detail == 0) {
40823a31b7ebSMike Smith 	    ciss_printf(sc, "FATAL I/O ERROR on logical drive %d (%s), SCSI port %d ID %d\n",
40833a31b7ebSMike Smith 			cn->data.io_error.logical_drive,
4084c6131460SPaul Saab 			ld->cl_name,
40853a31b7ebSMike Smith 			cn->data.io_error.failure_bus,
40863a31b7ebSMike Smith 			cn->data.io_error.failure_drive);
40873a31b7ebSMike Smith 	    /* XXX should we take the drive down at this point, or will we be told? */
40883a31b7ebSMike Smith 	}
40893a31b7ebSMike Smith 	break;
40903a31b7ebSMike Smith 
40913a31b7ebSMike Smith     case CISS_NOTIFY_LOGICAL_SURFACE:
40923a31b7ebSMike Smith 	if (cn->detail == 0)
40933a31b7ebSMike Smith 	    ciss_printf(sc, "logical drive %d (%s) completed consistency initialisation\n",
40943a31b7ebSMike Smith 			cn->data.consistency_completed.logical_drive,
4095c6131460SPaul Saab 			ld->cl_name);
40963a31b7ebSMike Smith 	break;
40973a31b7ebSMike Smith     }
40983a31b7ebSMike Smith }
40993a31b7ebSMike Smith 
41003a31b7ebSMike Smith /************************************************************************
41013a31b7ebSMike Smith  * Handle a notify event relating to the status of a physical drive.
41023a31b7ebSMike Smith  */
41033a31b7ebSMike Smith static void
41043a31b7ebSMike Smith ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn)
41053a31b7ebSMike Smith {
4106c6131460SPaul Saab }
41073a31b7ebSMike Smith 
4108c6131460SPaul Saab /************************************************************************
41091fe6c4eeSScott Long  * Handle a notify event relating to the status of a physical drive.
41101fe6c4eeSScott Long  */
41111fe6c4eeSScott Long static void
41121fe6c4eeSScott Long ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn)
41131fe6c4eeSScott Long {
4114c1885ab8SPaul Saab     struct ciss_lun_report *cll = NULL;
41151fe6c4eeSScott Long     int bus, target;
41161fe6c4eeSScott Long 
41171fe6c4eeSScott Long     switch (cn->subclass) {
41181fe6c4eeSScott Long     case CISS_NOTIFY_HOTPLUG_PHYSICAL:
41191fe6c4eeSScott Long     case CISS_NOTIFY_HOTPLUG_NONDISK:
41201fe6c4eeSScott Long 	bus = CISS_BIG_MAP_BUS(sc, cn->data.drive.big_physical_drive_number);
41211fe6c4eeSScott Long 	target =
41221fe6c4eeSScott Long 	    CISS_BIG_MAP_TARGET(sc, cn->data.drive.big_physical_drive_number);
41231fe6c4eeSScott Long 
41241fe6c4eeSScott Long 	if (cn->detail == 0) {
41251fe6c4eeSScott Long 	    /*
41261fe6c4eeSScott Long 	     * Mark the device offline so that it'll start producing selection
41271fe6c4eeSScott Long 	     * timeouts to the upper layer.
41281fe6c4eeSScott Long 	     */
41295f265711SScott Long 	    if ((bus >= 0) && (target >= 0))
41301fe6c4eeSScott Long 		sc->ciss_physical[bus][target].cp_online = 0;
41311fe6c4eeSScott Long 	} else {
41321fe6c4eeSScott Long 	    /*
41331fe6c4eeSScott Long 	     * Rescan the physical lun list for new items
41341fe6c4eeSScott Long 	     */
41351fe6c4eeSScott Long 	    cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
4136e3edca22SSean Bruno 				   sc->ciss_cfg->max_physical_supported);
41371fe6c4eeSScott Long 	    if (cll == NULL) {
41381fe6c4eeSScott Long 		ciss_printf(sc, "Warning, cannot get physical lun list\n");
41391fe6c4eeSScott Long 		break;
41401fe6c4eeSScott Long 	    }
41411fe6c4eeSScott Long 	    ciss_filter_physical(sc, cll);
41421fe6c4eeSScott Long 	}
41431fe6c4eeSScott Long 	break;
41441fe6c4eeSScott Long 
41451fe6c4eeSScott Long     default:
41461fe6c4eeSScott Long 	ciss_printf(sc, "Unknown hotplug event %d\n", cn->subclass);
41471fe6c4eeSScott Long 	return;
41481fe6c4eeSScott Long     }
4149c1885ab8SPaul Saab 
4150c1885ab8SPaul Saab     if (cll != NULL)
4151c1885ab8SPaul Saab 	free(cll, CISS_MALLOC_CLASS);
41521fe6c4eeSScott Long }
41531fe6c4eeSScott Long 
41541fe6c4eeSScott Long /************************************************************************
4155c6131460SPaul Saab  * Handle deferred processing of notify events.  Notify events may need
4156c6131460SPaul Saab  * sleep which is unsafe during an interrupt.
4157c6131460SPaul Saab  */
4158c6131460SPaul Saab static void
4159c6131460SPaul Saab ciss_notify_thread(void *arg)
4160c6131460SPaul Saab {
4161c6131460SPaul Saab     struct ciss_softc		*sc;
4162c6131460SPaul Saab     struct ciss_request		*cr;
4163c6131460SPaul Saab     struct ciss_notify		*cn;
4164c6131460SPaul Saab 
4165c6131460SPaul Saab     sc = (struct ciss_softc *)arg;
4166975b7318SScott Long #if __FreeBSD_version >= 500000
4167975b7318SScott Long     mtx_lock(&sc->ciss_mtx);
4168975b7318SScott Long #endif
4169c6131460SPaul Saab 
4170c6131460SPaul Saab     for (;;) {
417122657ce1SScott Long 	if (STAILQ_EMPTY(&sc->ciss_notify) != 0 &&
4172c6131460SPaul Saab 	    (sc->ciss_flags & CISS_FLAG_THREAD_SHUT) == 0) {
4173975b7318SScott Long 	    msleep(&sc->ciss_notify, &sc->ciss_mtx, PUSER, "idle", 0);
4174c6131460SPaul Saab 	}
4175c6131460SPaul Saab 
4176c6131460SPaul Saab 	if (sc->ciss_flags & CISS_FLAG_THREAD_SHUT)
4177c6131460SPaul Saab 	    break;
4178c6131460SPaul Saab 
4179c6131460SPaul Saab 	cr = ciss_dequeue_notify(sc);
4180c6131460SPaul Saab 
4181c6131460SPaul Saab 	if (cr == NULL)
4182c6131460SPaul Saab 		panic("cr null");
4183c6131460SPaul Saab 	cn = (struct ciss_notify *)cr->cr_data;
4184c6131460SPaul Saab 
4185c6131460SPaul Saab 	switch (cn->class) {
41861fe6c4eeSScott Long 	case CISS_NOTIFY_HOTPLUG:
41871fe6c4eeSScott Long 	    ciss_notify_hotplug(sc, cn);
41881fe6c4eeSScott Long 	    break;
4189c6131460SPaul Saab 	case CISS_NOTIFY_LOGICAL:
4190c6131460SPaul Saab 	    ciss_notify_logical(sc, cn);
4191c6131460SPaul Saab 	    break;
4192c6131460SPaul Saab 	case CISS_NOTIFY_PHYSICAL:
4193c6131460SPaul Saab 	    ciss_notify_physical(sc, cn);
4194c6131460SPaul Saab 	    break;
4195c6131460SPaul Saab 	}
4196c6131460SPaul Saab 
4197c6131460SPaul Saab 	ciss_release_request(cr);
4198c6131460SPaul Saab 
4199c6131460SPaul Saab     }
4200c6131460SPaul Saab     sc->ciss_notify_thread = NULL;
4201c6131460SPaul Saab     wakeup(&sc->ciss_notify_thread);
4202c6131460SPaul Saab 
4203c6131460SPaul Saab #if __FreeBSD_version >= 500000
4204975b7318SScott Long     mtx_unlock(&sc->ciss_mtx);
4205c6131460SPaul Saab #endif
42063745c395SJulian Elischer     kproc_exit(0);
4207c6131460SPaul Saab }
4208c6131460SPaul Saab 
4209c6131460SPaul Saab /************************************************************************
4210c6131460SPaul Saab  * Start the notification kernel thread.
4211c6131460SPaul Saab  */
4212c6131460SPaul Saab static void
4213c6131460SPaul Saab ciss_spawn_notify_thread(struct ciss_softc *sc)
4214c6131460SPaul Saab {
4215c6131460SPaul Saab 
421678d03361SPaul Saab #if __FreeBSD_version > 500005
42173745c395SJulian Elischer     if (kproc_create((void(*)(void *))ciss_notify_thread, sc,
4218c6131460SPaul Saab 		       &sc->ciss_notify_thread, 0, 0, "ciss_notify%d",
4219c6131460SPaul Saab 		       device_get_unit(sc->ciss_dev)))
422078d03361SPaul Saab #else
42213745c395SJulian Elischer     if (kproc_create((void(*)(void *))ciss_notify_thread, sc,
422278d03361SPaul Saab 		       &sc->ciss_notify_thread, "ciss_notify%d",
422378d03361SPaul Saab 		       device_get_unit(sc->ciss_dev)))
422478d03361SPaul Saab #endif
4225c6131460SPaul Saab 	panic("Could not create notify thread\n");
4226c6131460SPaul Saab }
4227c6131460SPaul Saab 
4228c6131460SPaul Saab /************************************************************************
4229c6131460SPaul Saab  * Kill the notification kernel thread.
4230c6131460SPaul Saab  */
4231c6131460SPaul Saab static void
4232c6131460SPaul Saab ciss_kill_notify_thread(struct ciss_softc *sc)
4233c6131460SPaul Saab {
4234c6131460SPaul Saab 
4235c6131460SPaul Saab     if (sc->ciss_notify_thread == NULL)
4236c6131460SPaul Saab 	return;
4237c6131460SPaul Saab 
4238c6131460SPaul Saab     sc->ciss_flags |= CISS_FLAG_THREAD_SHUT;
4239c6131460SPaul Saab     wakeup(&sc->ciss_notify);
4240975b7318SScott Long     msleep(&sc->ciss_notify_thread, &sc->ciss_mtx, PUSER, "thtrm", 0);
42413a31b7ebSMike Smith }
42423a31b7ebSMike Smith 
42433a31b7ebSMike Smith /************************************************************************
42443a31b7ebSMike Smith  * Print a request.
42453a31b7ebSMike Smith  */
42463a31b7ebSMike Smith static void
42473a31b7ebSMike Smith ciss_print_request(struct ciss_request *cr)
42483a31b7ebSMike Smith {
42493a31b7ebSMike Smith     struct ciss_softc	*sc;
42503a31b7ebSMike Smith     struct ciss_command	*cc;
42513a31b7ebSMike Smith     int			i;
42523a31b7ebSMike Smith 
42533a31b7ebSMike Smith     sc = cr->cr_sc;
42542fdaa90dSScott Long     cc = cr->cr_cc;
42553a31b7ebSMike Smith 
42563a31b7ebSMike Smith     ciss_printf(sc, "REQUEST @ %p\n", cr);
42573a31b7ebSMike Smith     ciss_printf(sc, "  data %p/%d  tag %d  flags %b\n",
42583a31b7ebSMike Smith 	      cr->cr_data, cr->cr_length, cr->cr_tag, cr->cr_flags,
42593a31b7ebSMike Smith 	      "\20\1mapped\2sleep\3poll\4dataout\5datain\n");
42603a31b7ebSMike Smith     ciss_printf(sc, "  sg list/total %d/%d  host tag 0x%x\n",
42613a31b7ebSMike Smith 		cc->header.sg_in_list, cc->header.sg_total, cc->header.host_tag);
42623a31b7ebSMike Smith     switch(cc->header.address.mode.mode) {
42633a31b7ebSMike Smith     case CISS_HDR_ADDRESS_MODE_PERIPHERAL:
42643a31b7ebSMike Smith     case CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL:
42653a31b7ebSMike Smith 	ciss_printf(sc, "  physical bus %d target %d\n",
42663a31b7ebSMike Smith 		    cc->header.address.physical.bus, cc->header.address.physical.target);
42673a31b7ebSMike Smith 	break;
42683a31b7ebSMike Smith     case CISS_HDR_ADDRESS_MODE_LOGICAL:
42693a31b7ebSMike Smith 	ciss_printf(sc, "  logical unit %d\n", cc->header.address.logical.lun);
42703a31b7ebSMike Smith 	break;
42713a31b7ebSMike Smith     }
42723a31b7ebSMike Smith     ciss_printf(sc, "  %s cdb length %d type %s attribute %s\n",
42733a31b7ebSMike Smith 		(cc->cdb.direction == CISS_CDB_DIRECTION_NONE) ? "no-I/O" :
42743a31b7ebSMike Smith 		(cc->cdb.direction == CISS_CDB_DIRECTION_READ) ? "READ" :
42753a31b7ebSMike Smith 		(cc->cdb.direction == CISS_CDB_DIRECTION_WRITE) ? "WRITE" : "??",
42763a31b7ebSMike Smith 		cc->cdb.cdb_length,
42773a31b7ebSMike Smith 		(cc->cdb.type == CISS_CDB_TYPE_COMMAND) ? "command" :
42783a31b7ebSMike Smith 		(cc->cdb.type == CISS_CDB_TYPE_MESSAGE) ? "message" : "??",
42793a31b7ebSMike Smith 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_UNTAGGED) ? "untagged" :
42803a31b7ebSMike Smith 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_SIMPLE) ? "simple" :
42813a31b7ebSMike Smith 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_HEAD_OF_QUEUE) ? "head-of-queue" :
42823a31b7ebSMike Smith 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_ORDERED) ? "ordered" :
42833a31b7ebSMike Smith 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_AUTO_CONTINGENT) ? "auto-contingent" : "??");
42843a31b7ebSMike Smith     ciss_printf(sc, "  %*D\n", cc->cdb.cdb_length, &cc->cdb.cdb[0], " ");
42853a31b7ebSMike Smith 
42863a31b7ebSMike Smith     if (cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) {
42873a31b7ebSMike Smith 	/* XXX print error info */
42883a31b7ebSMike Smith     } else {
42893a31b7ebSMike Smith 	/* since we don't use chained s/g, don't support it here */
42903a31b7ebSMike Smith 	for (i = 0; i < cc->header.sg_in_list; i++) {
42913a31b7ebSMike Smith 	    if ((i % 4) == 0)
42923a31b7ebSMike Smith 		ciss_printf(sc, "   ");
42933a31b7ebSMike Smith 	    printf("0x%08x/%d ", (u_int32_t)cc->sg[i].address, cc->sg[i].length);
42943a31b7ebSMike Smith 	    if ((((i + 1) % 4) == 0) || (i == (cc->header.sg_in_list - 1)))
42953a31b7ebSMike Smith 		printf("\n");
42963a31b7ebSMike Smith 	}
42973a31b7ebSMike Smith     }
42983a31b7ebSMike Smith }
42993a31b7ebSMike Smith 
43003a31b7ebSMike Smith /************************************************************************
43013a31b7ebSMike Smith  * Print information about the status of a logical drive.
43023a31b7ebSMike Smith  */
43033a31b7ebSMike Smith static void
43043a31b7ebSMike Smith ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld)
43053a31b7ebSMike Smith {
43063a31b7ebSMike Smith     int		bus, target, i;
43073a31b7ebSMike Smith 
4308d4aa427fSPaul Saab     if (ld->cl_lstatus == NULL) {
4309d4aa427fSPaul Saab 	printf("does not exist\n");
4310d4aa427fSPaul Saab 	return;
4311d4aa427fSPaul Saab     }
4312d4aa427fSPaul Saab 
43133a31b7ebSMike Smith     /* print drive status */
43143a31b7ebSMike Smith     switch(ld->cl_lstatus->status) {
43153a31b7ebSMike Smith     case CISS_LSTATUS_OK:
43163a31b7ebSMike Smith 	printf("online\n");
43173a31b7ebSMike Smith 	break;
43183a31b7ebSMike Smith     case CISS_LSTATUS_INTERIM_RECOVERY:
43193a31b7ebSMike Smith 	printf("in interim recovery mode\n");
43203a31b7ebSMike Smith 	break;
43213a31b7ebSMike Smith     case CISS_LSTATUS_READY_RECOVERY:
43223a31b7ebSMike Smith 	printf("ready to begin recovery\n");
43233a31b7ebSMike Smith 	break;
43243a31b7ebSMike Smith     case CISS_LSTATUS_RECOVERING:
43253a31b7ebSMike Smith 	bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
43263a31b7ebSMike Smith 	target = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
43273a31b7ebSMike Smith 	printf("being recovered, working on physical drive %d.%d, %u blocks remaining\n",
43283a31b7ebSMike Smith 	       bus, target, ld->cl_lstatus->blocks_to_recover);
43293a31b7ebSMike Smith 	break;
43303a31b7ebSMike Smith     case CISS_LSTATUS_EXPANDING:
43313a31b7ebSMike Smith 	printf("being expanded, %u blocks remaining\n",
43323a31b7ebSMike Smith 	       ld->cl_lstatus->blocks_to_recover);
43333a31b7ebSMike Smith 	break;
43343a31b7ebSMike Smith     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
43353a31b7ebSMike Smith 	printf("queued for expansion\n");
43363a31b7ebSMike Smith 	break;
43373a31b7ebSMike Smith     case CISS_LSTATUS_FAILED:
43383a31b7ebSMike Smith 	printf("queued for expansion\n");
43393a31b7ebSMike Smith 	break;
43403a31b7ebSMike Smith     case CISS_LSTATUS_WRONG_PDRIVE:
43413a31b7ebSMike Smith 	printf("wrong physical drive inserted\n");
43423a31b7ebSMike Smith 	break;
43433a31b7ebSMike Smith     case CISS_LSTATUS_MISSING_PDRIVE:
43443a31b7ebSMike Smith 	printf("missing a needed physical drive\n");
43453a31b7ebSMike Smith 	break;
43463a31b7ebSMike Smith     case CISS_LSTATUS_BECOMING_READY:
43473a31b7ebSMike Smith 	printf("becoming ready\n");
43483a31b7ebSMike Smith 	break;
43493a31b7ebSMike Smith     }
43503a31b7ebSMike Smith 
4351d4aa427fSPaul Saab     /* print failed physical drives */
43523a31b7ebSMike Smith     for (i = 0; i < CISS_BIG_MAP_ENTRIES / 8; i++) {
43533a31b7ebSMike Smith 	bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_failure_map[i]);
43543a31b7ebSMike Smith 	target = CISS_BIG_MAP_TARGET(sc, ld->cl_lstatus->drive_failure_map[i]);
43553a31b7ebSMike Smith 	if (bus == -1)
43563a31b7ebSMike Smith 	    continue;
43573a31b7ebSMike Smith 	ciss_printf(sc, "physical drive %d:%d (%x) failed\n", bus, target,
43583a31b7ebSMike Smith 		    ld->cl_lstatus->drive_failure_map[i]);
43593a31b7ebSMike Smith     }
43603a31b7ebSMike Smith }
43613a31b7ebSMike Smith 
4362d4aa427fSPaul Saab #ifdef CISS_DEBUG
4363b27556e0SSean Bruno #include "opt_ddb.h"
4364b27556e0SSean Bruno #ifdef DDB
4365b27556e0SSean Bruno #include <ddb/ddb.h>
4366d4aa427fSPaul Saab /************************************************************************
4367d4aa427fSPaul Saab  * Print information about the controller/driver.
4368d4aa427fSPaul Saab  */
4369d4aa427fSPaul Saab static void
4370d4aa427fSPaul Saab ciss_print_adapter(struct ciss_softc *sc)
4371d4aa427fSPaul Saab {
4372609caf8dSPaul Saab     int		i, j;
4373d4aa427fSPaul Saab 
4374d4aa427fSPaul Saab     ciss_printf(sc, "ADAPTER:\n");
4375d4aa427fSPaul Saab     for (i = 0; i < CISSQ_COUNT; i++) {
4376d4aa427fSPaul Saab 	ciss_printf(sc, "%s     %d/%d\n",
4377d4aa427fSPaul Saab 	    i == 0 ? "free" :
4378d4aa427fSPaul Saab 	    i == 1 ? "busy" : "complete",
4379d4aa427fSPaul Saab 	    sc->ciss_qstat[i].q_length,
4380d4aa427fSPaul Saab 	    sc->ciss_qstat[i].q_max);
4381d4aa427fSPaul Saab     }
4382d4aa427fSPaul Saab     ciss_printf(sc, "max_requests %d\n", sc->ciss_max_requests);
4383d4aa427fSPaul Saab     ciss_printf(sc, "flags %b\n", sc->ciss_flags,
4384d4aa427fSPaul Saab 	"\20\1notify_ok\2control_open\3aborting\4running\21fake_synch\22bmic_abort\n");
4385d4aa427fSPaul Saab 
43861fe6c4eeSScott Long     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
4387e3edca22SSean Bruno 	for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
4388d4aa427fSPaul Saab 	    ciss_printf(sc, "LOGICAL DRIVE %d:  ", i);
4389609caf8dSPaul Saab 	    ciss_print_ldrive(sc, &sc->ciss_logical[i][j]);
4390609caf8dSPaul Saab 	}
4391d4aa427fSPaul Saab     }
4392d4aa427fSPaul Saab 
43931fe6c4eeSScott Long     /* XXX Should physical drives be printed out here? */
43941fe6c4eeSScott Long 
4395d4aa427fSPaul Saab     for (i = 1; i < sc->ciss_max_requests; i++)
4396d4aa427fSPaul Saab 	ciss_print_request(sc->ciss_request + i);
4397d4aa427fSPaul Saab }
4398d4aa427fSPaul Saab 
4399d4aa427fSPaul Saab /* DDB hook */
4400b27556e0SSean Bruno DB_COMMAND(ciss_prt, db_ciss_prt)
4401d4aa427fSPaul Saab {
4402d4aa427fSPaul Saab     struct ciss_softc	*sc;
440311f9f6eaSSean Bruno     devclass_t dc;
440411f9f6eaSSean Bruno     int maxciss, i;
4405d4aa427fSPaul Saab 
440611f9f6eaSSean Bruno     dc = devclass_find("ciss");
440711f9f6eaSSean Bruno     if ( dc == NULL ) {
440811f9f6eaSSean Bruno         printf("%s: can't find devclass!\n", __func__);
440911f9f6eaSSean Bruno         return;
441011f9f6eaSSean Bruno     }
441111f9f6eaSSean Bruno     maxciss = devclass_get_maxunit(dc);
441211f9f6eaSSean Bruno     for (i = 0; i < maxciss; i++) {
441311f9f6eaSSean Bruno         sc = devclass_get_softc(dc, i);
4414d4aa427fSPaul Saab 	ciss_print_adapter(sc);
4415d4aa427fSPaul Saab     }
4416d4aa427fSPaul Saab }
4417d4aa427fSPaul Saab #endif
4418b27556e0SSean Bruno #endif
4419d4aa427fSPaul Saab 
44203a31b7ebSMike Smith /************************************************************************
44213a31b7ebSMike Smith  * Return a name for a logical drive status value.
44223a31b7ebSMike Smith  */
44233a31b7ebSMike Smith static const char *
44243a31b7ebSMike Smith ciss_name_ldrive_status(int status)
44253a31b7ebSMike Smith {
44263a31b7ebSMike Smith     switch (status) {
44273a31b7ebSMike Smith     case CISS_LSTATUS_OK:
44283a31b7ebSMike Smith 	return("OK");
44293a31b7ebSMike Smith     case CISS_LSTATUS_FAILED:
44303a31b7ebSMike Smith 	return("failed");
44313a31b7ebSMike Smith     case CISS_LSTATUS_NOT_CONFIGURED:
44323a31b7ebSMike Smith 	return("not configured");
44333a31b7ebSMike Smith     case CISS_LSTATUS_INTERIM_RECOVERY:
44343a31b7ebSMike Smith 	return("interim recovery");
44353a31b7ebSMike Smith     case CISS_LSTATUS_READY_RECOVERY:
44363a31b7ebSMike Smith 	return("ready for recovery");
44373a31b7ebSMike Smith     case CISS_LSTATUS_RECOVERING:
44383a31b7ebSMike Smith 	return("recovering");
44393a31b7ebSMike Smith     case CISS_LSTATUS_WRONG_PDRIVE:
44403a31b7ebSMike Smith 	return("wrong physical drive inserted");
44413a31b7ebSMike Smith     case CISS_LSTATUS_MISSING_PDRIVE:
44423a31b7ebSMike Smith 	return("missing physical drive");
44433a31b7ebSMike Smith     case CISS_LSTATUS_EXPANDING:
44443a31b7ebSMike Smith 	return("expanding");
44453a31b7ebSMike Smith     case CISS_LSTATUS_BECOMING_READY:
44463a31b7ebSMike Smith 	return("becoming ready");
44473a31b7ebSMike Smith     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
44483a31b7ebSMike Smith 	return("queued for expansion");
44493a31b7ebSMike Smith     }
44503a31b7ebSMike Smith     return("unknown status");
44513a31b7ebSMike Smith }
44523a31b7ebSMike Smith 
44533a31b7ebSMike Smith /************************************************************************
44543a31b7ebSMike Smith  * Return an online/offline/nonexistent value for a logical drive
44553a31b7ebSMike Smith  * status value.
44563a31b7ebSMike Smith  */
44573a31b7ebSMike Smith static int
44583a31b7ebSMike Smith ciss_decode_ldrive_status(int status)
44593a31b7ebSMike Smith {
44603a31b7ebSMike Smith     switch(status) {
44613a31b7ebSMike Smith     case CISS_LSTATUS_NOT_CONFIGURED:
44623a31b7ebSMike Smith 	return(CISS_LD_NONEXISTENT);
44633a31b7ebSMike Smith 
44643a31b7ebSMike Smith     case CISS_LSTATUS_OK:
44653a31b7ebSMike Smith     case CISS_LSTATUS_INTERIM_RECOVERY:
44663a31b7ebSMike Smith     case CISS_LSTATUS_READY_RECOVERY:
44673a31b7ebSMike Smith     case CISS_LSTATUS_RECOVERING:
44683a31b7ebSMike Smith     case CISS_LSTATUS_EXPANDING:
44693a31b7ebSMike Smith     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
44703a31b7ebSMike Smith 	return(CISS_LD_ONLINE);
44713a31b7ebSMike Smith 
44723a31b7ebSMike Smith     case CISS_LSTATUS_FAILED:
44733a31b7ebSMike Smith     case CISS_LSTATUS_WRONG_PDRIVE:
44743a31b7ebSMike Smith     case CISS_LSTATUS_MISSING_PDRIVE:
44753a31b7ebSMike Smith     case CISS_LSTATUS_BECOMING_READY:
44763a31b7ebSMike Smith     default:
44773a31b7ebSMike Smith 	return(CISS_LD_OFFLINE);
44783a31b7ebSMike Smith     }
44793a31b7ebSMike Smith }
44803a31b7ebSMike Smith 
44813a31b7ebSMike Smith 
44823a31b7ebSMike Smith /************************************************************************
44833a31b7ebSMike Smith  * Return a name for a logical drive's organisation.
44843a31b7ebSMike Smith  */
44853a31b7ebSMike Smith static const char *
44863a31b7ebSMike Smith ciss_name_ldrive_org(int org)
44873a31b7ebSMike Smith {
44883a31b7ebSMike Smith     switch(org) {
44893a31b7ebSMike Smith     case CISS_LDRIVE_RAID0:
44903a31b7ebSMike Smith 	return("RAID 0");
44913a31b7ebSMike Smith     case CISS_LDRIVE_RAID1:
4492d000b49dSKonstantin Belousov 	return("RAID 1(1+0)");
44933a31b7ebSMike Smith     case CISS_LDRIVE_RAID4:
44943a31b7ebSMike Smith 	return("RAID 4");
44953a31b7ebSMike Smith     case CISS_LDRIVE_RAID5:
44963a31b7ebSMike Smith 	return("RAID 5");
4497aaf8327bSPaul Saab     case CISS_LDRIVE_RAID51:
4498aaf8327bSPaul Saab 	return("RAID 5+1");
4499aaf8327bSPaul Saab     case CISS_LDRIVE_RAIDADG:
4500aaf8327bSPaul Saab 	return("RAID ADG");
45013a31b7ebSMike Smith     }
45023a31b7ebSMike Smith     return("unkown");
45033a31b7ebSMike Smith }
45043a31b7ebSMike Smith 
45053a31b7ebSMike Smith /************************************************************************
45063a31b7ebSMike Smith  * Return a name for a command status value.
45073a31b7ebSMike Smith  */
45083a31b7ebSMike Smith static const char *
45093a31b7ebSMike Smith ciss_name_command_status(int status)
45103a31b7ebSMike Smith {
45113a31b7ebSMike Smith     switch(status) {
45123a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:
45133a31b7ebSMike Smith 	return("success");
45143a31b7ebSMike Smith     case CISS_CMD_STATUS_TARGET_STATUS:
45153a31b7ebSMike Smith 	return("target status");
45163a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_UNDERRUN:
45173a31b7ebSMike Smith 	return("data underrun");
45183a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_OVERRUN:
45193a31b7ebSMike Smith 	return("data overrun");
45203a31b7ebSMike Smith     case CISS_CMD_STATUS_INVALID_COMMAND:
45213a31b7ebSMike Smith 	return("invalid command");
45223a31b7ebSMike Smith     case CISS_CMD_STATUS_PROTOCOL_ERROR:
45233a31b7ebSMike Smith 	return("protocol error");
45243a31b7ebSMike Smith     case CISS_CMD_STATUS_HARDWARE_ERROR:
45253a31b7ebSMike Smith 	return("hardware error");
45263a31b7ebSMike Smith     case CISS_CMD_STATUS_CONNECTION_LOST:
45273a31b7ebSMike Smith 	return("connection lost");
45283a31b7ebSMike Smith     case CISS_CMD_STATUS_ABORTED:
45293a31b7ebSMike Smith 	return("aborted");
45303a31b7ebSMike Smith     case CISS_CMD_STATUS_ABORT_FAILED:
45313a31b7ebSMike Smith 	return("abort failed");
45323a31b7ebSMike Smith     case CISS_CMD_STATUS_UNSOLICITED_ABORT:
45333a31b7ebSMike Smith 	return("unsolicited abort");
45343a31b7ebSMike Smith     case CISS_CMD_STATUS_TIMEOUT:
45353a31b7ebSMike Smith 	return("timeout");
45363a31b7ebSMike Smith     case CISS_CMD_STATUS_UNABORTABLE:
45373a31b7ebSMike Smith 	return("unabortable");
45383a31b7ebSMike Smith     }
45393a31b7ebSMike Smith     return("unknown status");
45403a31b7ebSMike Smith }
45413a31b7ebSMike Smith 
45423a31b7ebSMike Smith /************************************************************************
45433a31b7ebSMike Smith  * Handle an open on the control device.
45443a31b7ebSMike Smith  */
45453a31b7ebSMike Smith static int
454600b4e54aSWarner Losh ciss_open(struct cdev *dev, int flags, int fmt, struct thread *p)
45473a31b7ebSMike Smith {
45483a31b7ebSMike Smith     struct ciss_softc	*sc;
45493a31b7ebSMike Smith 
45503a31b7ebSMike Smith     debug_called(1);
45513a31b7ebSMike Smith 
45523a31b7ebSMike Smith     sc = (struct ciss_softc *)dev->si_drv1;
45533a31b7ebSMike Smith 
45543a31b7ebSMike Smith     /* we might want to veto if someone already has us open */
45553a31b7ebSMike Smith 
4556975b7318SScott Long     mtx_lock(&sc->ciss_mtx);
45573a31b7ebSMike Smith     sc->ciss_flags |= CISS_FLAG_CONTROL_OPEN;
4558975b7318SScott Long     mtx_unlock(&sc->ciss_mtx);
45593a31b7ebSMike Smith     return(0);
45603a31b7ebSMike Smith }
45613a31b7ebSMike Smith 
45623a31b7ebSMike Smith /************************************************************************
45633a31b7ebSMike Smith  * Handle the last close on the control device.
45643a31b7ebSMike Smith  */
45653a31b7ebSMike Smith static int
456600b4e54aSWarner Losh ciss_close(struct cdev *dev, int flags, int fmt, struct thread *p)
45673a31b7ebSMike Smith {
45683a31b7ebSMike Smith     struct ciss_softc	*sc;
45693a31b7ebSMike Smith 
45703a31b7ebSMike Smith     debug_called(1);
45713a31b7ebSMike Smith 
45723a31b7ebSMike Smith     sc = (struct ciss_softc *)dev->si_drv1;
45733a31b7ebSMike Smith 
4574975b7318SScott Long     mtx_lock(&sc->ciss_mtx);
45753a31b7ebSMike Smith     sc->ciss_flags &= ~CISS_FLAG_CONTROL_OPEN;
4576975b7318SScott Long     mtx_unlock(&sc->ciss_mtx);
45773a31b7ebSMike Smith     return (0);
45783a31b7ebSMike Smith }
45793a31b7ebSMike Smith 
45803a31b7ebSMike Smith /********************************************************************************
45813a31b7ebSMike Smith  * Handle adapter-specific control operations.
45823a31b7ebSMike Smith  *
45833a31b7ebSMike Smith  * Note that the API here is compatible with the Linux driver, in order to
45843a31b7ebSMike Smith  * simplify the porting of Compaq's userland tools.
45853a31b7ebSMike Smith  */
45863a31b7ebSMike Smith static int
458700b4e54aSWarner Losh ciss_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *p)
45883a31b7ebSMike Smith {
45893a31b7ebSMike Smith     struct ciss_softc		*sc;
45907caeec6aSPaul Saab     IOCTL_Command_struct	*ioc	= (IOCTL_Command_struct *)addr;
45917caeec6aSPaul Saab #ifdef __amd64__
45927caeec6aSPaul Saab     IOCTL_Command_struct32	*ioc32	= (IOCTL_Command_struct32 *)addr;
45937caeec6aSPaul Saab     IOCTL_Command_struct	ioc_swab;
45947caeec6aSPaul Saab #endif
45953a31b7ebSMike Smith     int				error;
45963a31b7ebSMike Smith 
45973a31b7ebSMike Smith     debug_called(1);
45983a31b7ebSMike Smith 
45993a31b7ebSMike Smith     sc = (struct ciss_softc *)dev->si_drv1;
46003a31b7ebSMike Smith     error = 0;
4601975b7318SScott Long     mtx_lock(&sc->ciss_mtx);
46023a31b7ebSMike Smith 
46033a31b7ebSMike Smith     switch(cmd) {
460422657ce1SScott Long     case CCISS_GETQSTATS:
460522657ce1SScott Long     {
460622657ce1SScott Long 	union ciss_statrequest *cr = (union ciss_statrequest *)addr;
460722657ce1SScott Long 
460822657ce1SScott Long 	switch (cr->cs_item) {
460922657ce1SScott Long 	case CISSQ_FREE:
461022657ce1SScott Long 	case CISSQ_NOTIFY:
461122657ce1SScott Long 	    bcopy(&sc->ciss_qstat[cr->cs_item], &cr->cs_qstat,
461222657ce1SScott Long 		sizeof(struct ciss_qstat));
461322657ce1SScott Long 	    break;
461422657ce1SScott Long 	default:
461522657ce1SScott Long 	    error = ENOIOCTL;
461622657ce1SScott Long 	    break;
461722657ce1SScott Long 	}
461822657ce1SScott Long 
461922657ce1SScott Long 	break;
462022657ce1SScott Long     }
462122657ce1SScott Long 
46223a31b7ebSMike Smith     case CCISS_GETPCIINFO:
46233a31b7ebSMike Smith     {
46243a31b7ebSMike Smith 	cciss_pci_info_struct	*pis = (cciss_pci_info_struct *)addr;
46253a31b7ebSMike Smith 
46263a31b7ebSMike Smith 	pis->bus = pci_get_bus(sc->ciss_dev);
46273a31b7ebSMike Smith 	pis->dev_fn = pci_get_slot(sc->ciss_dev);
4628e17ef005SSean Bruno         pis->board_id = (pci_get_subvendor(sc->ciss_dev) << 16) |
4629e17ef005SSean Bruno                 pci_get_subdevice(sc->ciss_dev);
46303a31b7ebSMike Smith 
46313a31b7ebSMike Smith 	break;
46323a31b7ebSMike Smith     }
46333a31b7ebSMike Smith 
46343a31b7ebSMike Smith     case CCISS_GETINTINFO:
46353a31b7ebSMike Smith     {
46363a31b7ebSMike Smith 	cciss_coalint_struct	*cis = (cciss_coalint_struct *)addr;
46373a31b7ebSMike Smith 
46383a31b7ebSMike Smith 	cis->delay = sc->ciss_cfg->interrupt_coalesce_delay;
46393a31b7ebSMike Smith 	cis->count = sc->ciss_cfg->interrupt_coalesce_count;
46403a31b7ebSMike Smith 
46413a31b7ebSMike Smith 	break;
46423a31b7ebSMike Smith     }
46433a31b7ebSMike Smith 
46443a31b7ebSMike Smith     case CCISS_SETINTINFO:
46453a31b7ebSMike Smith     {
46463a31b7ebSMike Smith 	cciss_coalint_struct	*cis = (cciss_coalint_struct *)addr;
46473a31b7ebSMike Smith 
46483a31b7ebSMike Smith 	if ((cis->delay == 0) && (cis->count == 0)) {
46493a31b7ebSMike Smith 	    error = EINVAL;
46503a31b7ebSMike Smith 	    break;
46513a31b7ebSMike Smith 	}
46523a31b7ebSMike Smith 
46533a31b7ebSMike Smith 	/*
46543a31b7ebSMike Smith 	 * XXX apparently this is only safe if the controller is idle,
46553a31b7ebSMike Smith 	 *     we should suspend it before doing this.
46563a31b7ebSMike Smith 	 */
46573a31b7ebSMike Smith 	sc->ciss_cfg->interrupt_coalesce_delay = cis->delay;
46583a31b7ebSMike Smith 	sc->ciss_cfg->interrupt_coalesce_count = cis->count;
46593a31b7ebSMike Smith 
46603a31b7ebSMike Smith 	if (ciss_update_config(sc))
46613a31b7ebSMike Smith 	    error = EIO;
46623a31b7ebSMike Smith 
46633a31b7ebSMike Smith 	/* XXX resume the controller here */
46643a31b7ebSMike Smith 	break;
46653a31b7ebSMike Smith     }
46663a31b7ebSMike Smith 
46673a31b7ebSMike Smith     case CCISS_GETNODENAME:
46683a31b7ebSMike Smith 	bcopy(sc->ciss_cfg->server_name, (NodeName_type *)addr,
46693a31b7ebSMike Smith 	      sizeof(NodeName_type));
46703a31b7ebSMike Smith 	break;
46713a31b7ebSMike Smith 
46723a31b7ebSMike Smith     case CCISS_SETNODENAME:
46733a31b7ebSMike Smith 	bcopy((NodeName_type *)addr, sc->ciss_cfg->server_name,
46743a31b7ebSMike Smith 	      sizeof(NodeName_type));
46753a31b7ebSMike Smith 	if (ciss_update_config(sc))
46763a31b7ebSMike Smith 	    error = EIO;
46773a31b7ebSMike Smith 	break;
46783a31b7ebSMike Smith 
46793a31b7ebSMike Smith     case CCISS_GETHEARTBEAT:
46803a31b7ebSMike Smith 	*(Heartbeat_type *)addr = sc->ciss_cfg->heartbeat;
46813a31b7ebSMike Smith 	break;
46823a31b7ebSMike Smith 
46833a31b7ebSMike Smith     case CCISS_GETBUSTYPES:
46843a31b7ebSMike Smith 	*(BusTypes_type *)addr = sc->ciss_cfg->bus_types;
46853a31b7ebSMike Smith 	break;
46863a31b7ebSMike Smith 
46873a31b7ebSMike Smith     case CCISS_GETFIRMVER:
46883a31b7ebSMike Smith 	bcopy(sc->ciss_id->running_firmware_revision, (FirmwareVer_type *)addr,
46893a31b7ebSMike Smith 	      sizeof(FirmwareVer_type));
46903a31b7ebSMike Smith 	break;
46913a31b7ebSMike Smith 
46923a31b7ebSMike Smith     case CCISS_GETDRIVERVER:
46933a31b7ebSMike Smith 	*(DriverVer_type *)addr = CISS_DRIVER_VERSION;
46943a31b7ebSMike Smith 	break;
46953a31b7ebSMike Smith 
46963a31b7ebSMike Smith     case CCISS_REVALIDVOLS:
46973a31b7ebSMike Smith 	/*
46983a31b7ebSMike Smith 	 * This is a bit ugly; to do it "right" we really need
46993a31b7ebSMike Smith 	 * to find any disks that have changed, kick CAM off them,
47003a31b7ebSMike Smith 	 * then rescan only these disks.  It'd be nice if they
47013a31b7ebSMike Smith 	 * a) told us which disk(s) they were going to play with,
47023a31b7ebSMike Smith 	 * and b) which ones had arrived. 8(
47033a31b7ebSMike Smith 	 */
47043a31b7ebSMike Smith 	break;
47053a31b7ebSMike Smith 
47067caeec6aSPaul Saab #ifdef __amd64__
47077caeec6aSPaul Saab     case CCISS_PASSTHRU32:
47087caeec6aSPaul Saab 	ioc_swab.LUN_info	= ioc32->LUN_info;
47097caeec6aSPaul Saab 	ioc_swab.Request	= ioc32->Request;
47107caeec6aSPaul Saab 	ioc_swab.error_info	= ioc32->error_info;
47117caeec6aSPaul Saab 	ioc_swab.buf_size	= ioc32->buf_size;
47127caeec6aSPaul Saab 	ioc_swab.buf		= (u_int8_t *)(uintptr_t)ioc32->buf;
47137caeec6aSPaul Saab 	ioc			= &ioc_swab;
47147caeec6aSPaul Saab 	/* FALLTHROUGH */
47157caeec6aSPaul Saab #endif
47167caeec6aSPaul Saab 
47173a31b7ebSMike Smith     case CCISS_PASSTHRU:
47187caeec6aSPaul Saab 	error = ciss_user_command(sc, ioc);
47193a31b7ebSMike Smith 	break;
47203a31b7ebSMike Smith 
47213a31b7ebSMike Smith     default:
47223a31b7ebSMike Smith 	debug(0, "unknown ioctl 0x%lx", cmd);
47233a31b7ebSMike Smith 
47243a31b7ebSMike Smith 	debug(1, "CCISS_GETPCIINFO:   0x%lx", CCISS_GETPCIINFO);
47253a31b7ebSMike Smith 	debug(1, "CCISS_GETINTINFO:   0x%lx", CCISS_GETINTINFO);
47263a31b7ebSMike Smith 	debug(1, "CCISS_SETINTINFO:   0x%lx", CCISS_SETINTINFO);
47273a31b7ebSMike Smith 	debug(1, "CCISS_GETNODENAME:  0x%lx", CCISS_GETNODENAME);
47283a31b7ebSMike Smith 	debug(1, "CCISS_SETNODENAME:  0x%lx", CCISS_SETNODENAME);
47293a31b7ebSMike Smith 	debug(1, "CCISS_GETHEARTBEAT: 0x%lx", CCISS_GETHEARTBEAT);
47303a31b7ebSMike Smith 	debug(1, "CCISS_GETBUSTYPES:  0x%lx", CCISS_GETBUSTYPES);
47313a31b7ebSMike Smith 	debug(1, "CCISS_GETFIRMVER:   0x%lx", CCISS_GETFIRMVER);
47323a31b7ebSMike Smith 	debug(1, "CCISS_GETDRIVERVER: 0x%lx", CCISS_GETDRIVERVER);
47333a31b7ebSMike Smith 	debug(1, "CCISS_REVALIDVOLS:  0x%lx", CCISS_REVALIDVOLS);
47343a31b7ebSMike Smith 	debug(1, "CCISS_PASSTHRU:     0x%lx", CCISS_PASSTHRU);
47353a31b7ebSMike Smith 
47363a31b7ebSMike Smith 	error = ENOIOCTL;
47373a31b7ebSMike Smith 	break;
47383a31b7ebSMike Smith     }
47393a31b7ebSMike Smith 
4740975b7318SScott Long     mtx_unlock(&sc->ciss_mtx);
47413a31b7ebSMike Smith     return(error);
47423a31b7ebSMike Smith }
4743