xref: /freebsd/sys/dev/ciss/ciss.c (revision b27556e012b0437aeca5ef8f55ef596dd6f78fa3)
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 struct cam_periph *ciss_find_periph(struct ciss_softc *sc,
184c6131460SPaul Saab 					   int bus, int target);
185c6131460SPaul Saab static int	ciss_name_device(struct ciss_softc *sc, int bus, int target);
1863a31b7ebSMike Smith 
1873a31b7ebSMike Smith /* periodic status monitoring */
1883a31b7ebSMike Smith static void	ciss_periodic(void *arg);
18954d02e0aSMitsuru IWASAKI static void	ciss_nop_complete(struct ciss_request *cr);
190e08d902aSMitsuru IWASAKI static void	ciss_disable_adapter(struct ciss_softc *sc);
1913a31b7ebSMike Smith static void	ciss_notify_event(struct ciss_softc *sc);
1923a31b7ebSMike Smith static void	ciss_notify_complete(struct ciss_request *cr);
1933a31b7ebSMike Smith static int	ciss_notify_abort(struct ciss_softc *sc);
1943a31b7ebSMike Smith static int	ciss_notify_abort_bmic(struct ciss_softc *sc);
1951fe6c4eeSScott Long static void	ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn);
1963a31b7ebSMike Smith static void	ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn);
1973a31b7ebSMike Smith static void	ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn);
1983a31b7ebSMike Smith 
1993a31b7ebSMike Smith /* debugging output */
2003a31b7ebSMike Smith static void	ciss_print_request(struct ciss_request *cr);
2013a31b7ebSMike Smith static void	ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld);
2023a31b7ebSMike Smith static const char *ciss_name_ldrive_status(int status);
2033a31b7ebSMike Smith static int	ciss_decode_ldrive_status(int status);
2043a31b7ebSMike Smith static const char *ciss_name_ldrive_org(int org);
2053a31b7ebSMike Smith static const char *ciss_name_command_status(int status);
2063a31b7ebSMike Smith 
2073a31b7ebSMike Smith /*
2083a31b7ebSMike Smith  * PCI bus interface.
2093a31b7ebSMike Smith  */
2103a31b7ebSMike Smith static device_method_t ciss_methods[] = {
2113a31b7ebSMike Smith     /* Device interface */
2123a31b7ebSMike Smith     DEVMETHOD(device_probe,	ciss_probe),
2133a31b7ebSMike Smith     DEVMETHOD(device_attach,	ciss_attach),
2143a31b7ebSMike Smith     DEVMETHOD(device_detach,	ciss_detach),
2153a31b7ebSMike Smith     DEVMETHOD(device_shutdown,	ciss_shutdown),
2163a31b7ebSMike Smith     { 0, 0 }
2173a31b7ebSMike Smith };
2183a31b7ebSMike Smith 
2193a31b7ebSMike Smith static driver_t ciss_pci_driver = {
2203a31b7ebSMike Smith     "ciss",
2213a31b7ebSMike Smith     ciss_methods,
2223a31b7ebSMike Smith     sizeof(struct ciss_softc)
2233a31b7ebSMike Smith };
2243a31b7ebSMike Smith 
2253a31b7ebSMike Smith static devclass_t	ciss_devclass;
2263a31b7ebSMike Smith DRIVER_MODULE(ciss, pci, ciss_pci_driver, ciss_devclass, 0, 0);
22791d7721cSMaxim Konovalov MODULE_DEPEND(ciss, cam, 1, 1, 1);
22891d7721cSMaxim Konovalov MODULE_DEPEND(ciss, pci, 1, 1, 1);
2293a31b7ebSMike Smith 
2303a31b7ebSMike Smith /*
2313a31b7ebSMike Smith  * Control device interface.
2323a31b7ebSMike Smith  */
2333a31b7ebSMike Smith static d_open_t		ciss_open;
2343a31b7ebSMike Smith static d_close_t	ciss_close;
2353a31b7ebSMike Smith static d_ioctl_t	ciss_ioctl;
2363a31b7ebSMike Smith 
2373a31b7ebSMike Smith static struct cdevsw ciss_cdevsw = {
238dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
239975b7318SScott Long 	.d_flags =	0,
2407ac40f5fSPoul-Henning Kamp 	.d_open =	ciss_open,
2417ac40f5fSPoul-Henning Kamp 	.d_close =	ciss_close,
2427ac40f5fSPoul-Henning Kamp 	.d_ioctl =	ciss_ioctl,
2437ac40f5fSPoul-Henning Kamp 	.d_name =	"ciss",
2443a31b7ebSMike Smith };
2453a31b7ebSMike Smith 
2461fe6c4eeSScott Long /*
2471fe6c4eeSScott Long  * This tunable can be set at boot time and controls whether physical devices
2481fe6c4eeSScott Long  * that are marked hidden by the firmware should be exposed anyways.
2491fe6c4eeSScott Long  */
2501fe6c4eeSScott Long static unsigned int ciss_expose_hidden_physical = 0;
2511fe6c4eeSScott Long TUNABLE_INT("hw.ciss.expose_hidden_physical", &ciss_expose_hidden_physical);
2521fe6c4eeSScott Long 
2535a3c4d69SMitsuru IWASAKI static unsigned int ciss_nop_message_heartbeat = 0;
2545a3c4d69SMitsuru IWASAKI TUNABLE_INT("hw.ciss.nop_message_heartbeat", &ciss_nop_message_heartbeat);
2555a3c4d69SMitsuru IWASAKI 
25622657ce1SScott Long /*
25722657ce1SScott Long  * This tunable can force a particular transport to be used:
25822657ce1SScott Long  * <= 0 : use default
25922657ce1SScott Long  *    1 : force simple
26022657ce1SScott Long  *    2 : force performant
26122657ce1SScott Long  */
26222657ce1SScott Long static int ciss_force_transport = 0;
26322657ce1SScott Long TUNABLE_INT("hw.ciss.force_transport", &ciss_force_transport);
26422657ce1SScott Long 
26522657ce1SScott Long /*
26622657ce1SScott Long  * This tunable can force a particular interrupt delivery method to be used:
26722657ce1SScott Long  * <= 0 : use default
26822657ce1SScott Long  *    1 : force INTx
26922657ce1SScott Long  *    2 : force MSIX
27022657ce1SScott Long  */
27122657ce1SScott Long static int ciss_force_interrupt = 0;
27222657ce1SScott Long TUNABLE_INT("hw.ciss.force_interrupt", &ciss_force_interrupt);
27322657ce1SScott Long 
2743a31b7ebSMike Smith /************************************************************************
2753a31b7ebSMike Smith  * CISS adapters amazingly don't have a defined programming interface
2763a31b7ebSMike Smith  * value.  (One could say some very despairing things about PCI and
2773a31b7ebSMike Smith  * people just not getting the general idea.)  So we are forced to
2783a31b7ebSMike Smith  * stick with matching against subvendor/subdevice, and thus have to
2793a31b7ebSMike Smith  * be updated for every new CISS adapter that appears.
2803a31b7ebSMike Smith  */
2812fdaa90dSScott Long #define CISS_BOARD_UNKNWON	0
2822fdaa90dSScott Long #define CISS_BOARD_SA5		1
2832fdaa90dSScott Long #define CISS_BOARD_SA5B		2
2842fdaa90dSScott Long #define CISS_BOARD_NOMSI	(1<<4)
285ee4827b2SSean Bruno #define CISS_BOARD_SIMPLE       (1<<5)
2863a31b7ebSMike Smith 
2873a31b7ebSMike Smith static struct
2883a31b7ebSMike Smith {
2893a31b7ebSMike Smith     u_int16_t	subvendor;
2903a31b7ebSMike Smith     u_int16_t	subdevice;
2913a31b7ebSMike Smith     int		flags;
2923a31b7ebSMike Smith     char	*desc;
2933a31b7ebSMike Smith } ciss_vendor_data[] = {
294ee4827b2SSean Bruno     { 0x0e11, 0x4070, CISS_BOARD_SA5|CISS_BOARD_NOMSI|CISS_BOARD_SIMPLE,
295ee4827b2SSean Bruno                                                         "Compaq Smart Array 5300" },
2962fdaa90dSScott Long     { 0x0e11, 0x4080, CISS_BOARD_SA5B|CISS_BOARD_NOMSI,	"Compaq Smart Array 5i" },
2972fdaa90dSScott Long     { 0x0e11, 0x4082, CISS_BOARD_SA5B|CISS_BOARD_NOMSI,	"Compaq Smart Array 532" },
2982fdaa90dSScott Long     { 0x0e11, 0x4083, CISS_BOARD_SA5B|CISS_BOARD_NOMSI,	"HP Smart Array 5312" },
299b26392c7SPaul Saab     { 0x0e11, 0x4091, CISS_BOARD_SA5,	"HP Smart Array 6i" },
3009e7313bbSPaul Saab     { 0x0e11, 0x409A, CISS_BOARD_SA5,	"HP Smart Array 641" },
3019e7313bbSPaul Saab     { 0x0e11, 0x409B, CISS_BOARD_SA5,	"HP Smart Array 642" },
3029e7313bbSPaul Saab     { 0x0e11, 0x409C, CISS_BOARD_SA5,	"HP Smart Array 6400" },
3039e7313bbSPaul Saab     { 0x0e11, 0x409D, CISS_BOARD_SA5,	"HP Smart Array 6400 EM" },
304cad572c4SPaul Saab     { 0x103C, 0x3211, CISS_BOARD_SA5,	"HP Smart Array E200i" },
305cad572c4SPaul Saab     { 0x103C, 0x3212, CISS_BOARD_SA5,	"HP Smart Array E200" },
306cad572c4SPaul Saab     { 0x103C, 0x3213, CISS_BOARD_SA5,	"HP Smart Array E200i" },
307cad572c4SPaul Saab     { 0x103C, 0x3214, CISS_BOARD_SA5,	"HP Smart Array E200i" },
308cad572c4SPaul Saab     { 0x103C, 0x3215, CISS_BOARD_SA5,	"HP Smart Array E200i" },
309b612d5e1SPaul Saab     { 0x103C, 0x3220, CISS_BOARD_SA5,	"HP Smart Array" },
310b612d5e1SPaul Saab     { 0x103C, 0x3222, CISS_BOARD_SA5,	"HP Smart Array" },
3114bb10cf1SPaul Saab     { 0x103C, 0x3223, CISS_BOARD_SA5,	"HP Smart Array P800" },
312f3f82767SPaul Saab     { 0x103C, 0x3225, CISS_BOARD_SA5,	"HP Smart Array P600" },
313b612d5e1SPaul Saab     { 0x103C, 0x3230, CISS_BOARD_SA5,	"HP Smart Array" },
314cad572c4SPaul Saab     { 0x103C, 0x3231, CISS_BOARD_SA5,	"HP Smart Array" },
315b612d5e1SPaul Saab     { 0x103C, 0x3232, CISS_BOARD_SA5,	"HP Smart Array" },
316b612d5e1SPaul Saab     { 0x103C, 0x3233, CISS_BOARD_SA5,	"HP Smart Array" },
317cad572c4SPaul Saab     { 0x103C, 0x3234, CISS_BOARD_SA5,	"HP Smart Array P400" },
318cad572c4SPaul Saab     { 0x103C, 0x3235, CISS_BOARD_SA5,	"HP Smart Array P400i" },
319b612d5e1SPaul Saab     { 0x103C, 0x3236, CISS_BOARD_SA5,	"HP Smart Array" },
3207b6d3d4cSScott Long     { 0x103C, 0x3237, CISS_BOARD_SA5,	"HP Smart Array E500" },
321b612d5e1SPaul Saab     { 0x103C, 0x3238, CISS_BOARD_SA5,	"HP Smart Array" },
322b612d5e1SPaul Saab     { 0x103C, 0x3239, CISS_BOARD_SA5,	"HP Smart Array" },
323b612d5e1SPaul Saab     { 0x103C, 0x323A, CISS_BOARD_SA5,	"HP Smart Array" },
324b612d5e1SPaul Saab     { 0x103C, 0x323B, CISS_BOARD_SA5,	"HP Smart Array" },
325b612d5e1SPaul Saab     { 0x103C, 0x323C, CISS_BOARD_SA5,	"HP Smart Array" },
3267b6d3d4cSScott Long     { 0x103C, 0x323D, CISS_BOARD_SA5,	"HP Smart Array P700m" },
327160b4e6bSPaul Saab     { 0x103C, 0x3241, CISS_BOARD_SA5,	"HP Smart Array P212" },
328160b4e6bSPaul Saab     { 0x103C, 0x3243, CISS_BOARD_SA5,	"HP Smart Array P410" },
329160b4e6bSPaul Saab     { 0x103C, 0x3245, CISS_BOARD_SA5,	"HP Smart Array P410i" },
330160b4e6bSPaul Saab     { 0x103C, 0x3247, CISS_BOARD_SA5,	"HP Smart Array P411" },
331160b4e6bSPaul Saab     { 0x103C, 0x3249, CISS_BOARD_SA5,	"HP Smart Array P812" },
3327b6d3d4cSScott Long     { 0x103C, 0x324A, CISS_BOARD_SA5,	"HP Smart Array P712m" },
3337b6d3d4cSScott Long     { 0x103C, 0x324B, CISS_BOARD_SA5,	"HP Smart Array" },
334e17ef005SSean Bruno     { 0x103C, 0x3350, CISS_BOARD_SA5,   "HP Smart Array P222" },
3355564c1cdSSean Bruno     { 0x103C, 0x3351, CISS_BOARD_SA5,   "HP Smart Array P420" },
336e17ef005SSean Bruno     { 0x103C, 0x3352, CISS_BOARD_SA5,   "HP Smart Array P421" },
337e17ef005SSean Bruno     { 0x103C, 0x3353, CISS_BOARD_SA5,   "HP Smart Array P822" },
338e17ef005SSean Bruno     { 0x103C, 0x3354, CISS_BOARD_SA5,   "HP Smart Array P420i" },
339e17ef005SSean Bruno     { 0x103C, 0x3355, CISS_BOARD_SA5,   "HP Smart Array P220i" },
340e17ef005SSean Bruno     { 0x103C, 0x3356, CISS_BOARD_SA5,   "HP Smart Array P721m" },
3414a6a94d8SArchie Cobbs     { 0, 0, 0, NULL }
3423a31b7ebSMike Smith };
3433a31b7ebSMike Smith 
3443a31b7ebSMike Smith /************************************************************************
3453a31b7ebSMike Smith  * Find a match for the device in our list of known adapters.
3463a31b7ebSMike Smith  */
3473a31b7ebSMike Smith static int
3483a31b7ebSMike Smith ciss_lookup(device_t dev)
3493a31b7ebSMike Smith {
3503a31b7ebSMike Smith     int 	i;
3513a31b7ebSMike Smith 
3523a31b7ebSMike Smith     for (i = 0; ciss_vendor_data[i].desc != NULL; i++)
3533a31b7ebSMike Smith 	if ((pci_get_subvendor(dev) == ciss_vendor_data[i].subvendor) &&
3543a31b7ebSMike Smith 	    (pci_get_subdevice(dev) == ciss_vendor_data[i].subdevice)) {
3553a31b7ebSMike Smith 	    return(i);
3563a31b7ebSMike Smith 	}
3573a31b7ebSMike Smith     return(-1);
3583a31b7ebSMike Smith }
3593a31b7ebSMike Smith 
3603a31b7ebSMike Smith /************************************************************************
3613a31b7ebSMike Smith  * Match a known CISS adapter.
3623a31b7ebSMike Smith  */
3633a31b7ebSMike Smith static int
3643a31b7ebSMike Smith ciss_probe(device_t dev)
3653a31b7ebSMike Smith {
3663a31b7ebSMike Smith     int		i;
3673a31b7ebSMike Smith 
3683a31b7ebSMike Smith     i = ciss_lookup(dev);
3693a31b7ebSMike Smith     if (i != -1) {
3703a31b7ebSMike Smith 	device_set_desc(dev, ciss_vendor_data[i].desc);
371538565c4SWarner Losh 	return(BUS_PROBE_DEFAULT);
3723a31b7ebSMike Smith     }
3733a31b7ebSMike Smith     return(ENOENT);
3743a31b7ebSMike Smith }
3753a31b7ebSMike Smith 
3763a31b7ebSMike Smith /************************************************************************
3773a31b7ebSMike Smith  * Attach the driver to this adapter.
3783a31b7ebSMike Smith  */
3793a31b7ebSMike Smith static int
3803a31b7ebSMike Smith ciss_attach(device_t dev)
3813a31b7ebSMike Smith {
3823a31b7ebSMike Smith     struct ciss_softc	*sc;
383d69c9c78SScott Long     int			error;
3843a31b7ebSMike Smith 
3853a31b7ebSMike Smith     debug_called(1);
3863a31b7ebSMike Smith 
3873a31b7ebSMike Smith #ifdef CISS_DEBUG
3883a31b7ebSMike Smith     /* print structure/union sizes */
3893a31b7ebSMike Smith     debug_struct(ciss_command);
3903a31b7ebSMike Smith     debug_struct(ciss_header);
3913a31b7ebSMike Smith     debug_union(ciss_device_address);
3923a31b7ebSMike Smith     debug_struct(ciss_cdb);
3933a31b7ebSMike Smith     debug_struct(ciss_report_cdb);
3943a31b7ebSMike Smith     debug_struct(ciss_notify_cdb);
3953a31b7ebSMike Smith     debug_struct(ciss_notify);
3963a31b7ebSMike Smith     debug_struct(ciss_message_cdb);
3973a31b7ebSMike Smith     debug_struct(ciss_error_info_pointer);
3983a31b7ebSMike Smith     debug_struct(ciss_error_info);
3993a31b7ebSMike Smith     debug_struct(ciss_sg_entry);
4003a31b7ebSMike Smith     debug_struct(ciss_config_table);
4013a31b7ebSMike Smith     debug_struct(ciss_bmic_cdb);
4023a31b7ebSMike Smith     debug_struct(ciss_bmic_id_ldrive);
4033a31b7ebSMike Smith     debug_struct(ciss_bmic_id_lstatus);
4043a31b7ebSMike Smith     debug_struct(ciss_bmic_id_table);
4053a31b7ebSMike Smith     debug_struct(ciss_bmic_id_pdrive);
4063a31b7ebSMike Smith     debug_struct(ciss_bmic_blink_pdrive);
4073a31b7ebSMike Smith     debug_struct(ciss_bmic_flush_cache);
4083a31b7ebSMike Smith     debug_const(CISS_MAX_REQUESTS);
4093a31b7ebSMike Smith     debug_const(CISS_MAX_LOGICAL);
4103a31b7ebSMike Smith     debug_const(CISS_INTERRUPT_COALESCE_DELAY);
4113a31b7ebSMike Smith     debug_const(CISS_INTERRUPT_COALESCE_COUNT);
4123a31b7ebSMike Smith     debug_const(CISS_COMMAND_ALLOC_SIZE);
4133a31b7ebSMike Smith     debug_const(CISS_COMMAND_SG_LENGTH);
4143a31b7ebSMike Smith 
4153a31b7ebSMike Smith     debug_type(cciss_pci_info_struct);
4163a31b7ebSMike Smith     debug_type(cciss_coalint_struct);
4173a31b7ebSMike Smith     debug_type(cciss_coalint_struct);
4183a31b7ebSMike Smith     debug_type(NodeName_type);
4193a31b7ebSMike Smith     debug_type(NodeName_type);
4203a31b7ebSMike Smith     debug_type(Heartbeat_type);
4213a31b7ebSMike Smith     debug_type(BusTypes_type);
4223a31b7ebSMike Smith     debug_type(FirmwareVer_type);
4233a31b7ebSMike Smith     debug_type(DriverVer_type);
4243a31b7ebSMike Smith     debug_type(IOCTL_Command_struct);
4253a31b7ebSMike Smith #endif
4263a31b7ebSMike Smith 
4273a31b7ebSMike Smith     sc = device_get_softc(dev);
4283a31b7ebSMike Smith     sc->ciss_dev = dev;
42964a026bdSGavin Atkinson     mtx_init(&sc->ciss_mtx, "cissmtx", NULL, MTX_DEF);
430aa1385cdSJohn Baldwin     callout_init_mtx(&sc->ciss_periodic, &sc->ciss_mtx, 0);
4313a31b7ebSMike Smith 
4323a31b7ebSMike Smith     /*
4333a31b7ebSMike Smith      * Do PCI-specific init.
4343a31b7ebSMike Smith      */
4353a31b7ebSMike Smith     if ((error = ciss_init_pci(sc)) != 0)
4363a31b7ebSMike Smith 	goto out;
4373a31b7ebSMike Smith 
4383a31b7ebSMike Smith     /*
4393a31b7ebSMike Smith      * Initialise driver queues.
4403a31b7ebSMike Smith      */
4413a31b7ebSMike Smith     ciss_initq_free(sc);
442c6131460SPaul Saab     ciss_initq_notify(sc);
4433a31b7ebSMike Smith 
4443a31b7ebSMike Smith     /*
44517c0792dSPaul Saab      * Initalize device sysctls.
44617c0792dSPaul Saab      */
44717c0792dSPaul Saab     ciss_init_sysctl(sc);
44817c0792dSPaul Saab 
44917c0792dSPaul Saab     /*
4503a31b7ebSMike Smith      * Initialise command/request pool.
4513a31b7ebSMike Smith      */
4523a31b7ebSMike Smith     if ((error = ciss_init_requests(sc)) != 0)
4533a31b7ebSMike Smith 	goto out;
4543a31b7ebSMike Smith 
4553a31b7ebSMike Smith     /*
4563a31b7ebSMike Smith      * Get adapter information.
4573a31b7ebSMike Smith      */
4583a31b7ebSMike Smith     if ((error = ciss_identify_adapter(sc)) != 0)
4593a31b7ebSMike Smith 	goto out;
4603a31b7ebSMike Smith 
4613a31b7ebSMike Smith     /*
462c6131460SPaul Saab      * Find all the physical devices.
463c6131460SPaul Saab      */
464c6131460SPaul Saab     if ((error = ciss_init_physical(sc)) != 0)
465c6131460SPaul Saab 	goto out;
466c6131460SPaul Saab 
467c6131460SPaul Saab     /*
4683a31b7ebSMike Smith      * Build our private table of logical devices.
4693a31b7ebSMike Smith      */
4703a31b7ebSMike Smith     if ((error = ciss_init_logical(sc)) != 0)
4713a31b7ebSMike Smith 	goto out;
4723a31b7ebSMike Smith 
4733a31b7ebSMike Smith     /*
4743a31b7ebSMike Smith      * Enable interrupts so that the CAM scan can complete.
4753a31b7ebSMike Smith      */
4763a31b7ebSMike Smith     CISS_TL_SIMPLE_ENABLE_INTERRUPTS(sc);
4773a31b7ebSMike Smith 
4783a31b7ebSMike Smith     /*
4793a31b7ebSMike Smith      * Initialise the CAM interface.
4803a31b7ebSMike Smith      */
4813a31b7ebSMike Smith     if ((error = ciss_cam_init(sc)) != 0)
4823a31b7ebSMike Smith 	goto out;
4833a31b7ebSMike Smith 
4843a31b7ebSMike Smith     /*
4853a31b7ebSMike Smith      * Start the heartbeat routine and event chain.
4863a31b7ebSMike Smith      */
4873a31b7ebSMike Smith     ciss_periodic(sc);
4883a31b7ebSMike Smith 
4893a31b7ebSMike Smith    /*
4903a31b7ebSMike Smith      * Create the control device.
4913a31b7ebSMike Smith      */
4923a31b7ebSMike Smith     sc->ciss_dev_t = make_dev(&ciss_cdevsw, device_get_unit(sc->ciss_dev),
4933a31b7ebSMike Smith 			      UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
4943a31b7ebSMike Smith 			      "ciss%d", device_get_unit(sc->ciss_dev));
4953a31b7ebSMike Smith     sc->ciss_dev_t->si_drv1 = sc;
4963a31b7ebSMike Smith 
4973a31b7ebSMike Smith     /*
4983a31b7ebSMike Smith      * The adapter is running; synchronous commands can now sleep
4993a31b7ebSMike Smith      * waiting for an interrupt to signal completion.
5003a31b7ebSMike Smith      */
5013a31b7ebSMike Smith     sc->ciss_flags |= CISS_FLAG_RUNNING;
5023a31b7ebSMike Smith 
503c6131460SPaul Saab     ciss_spawn_notify_thread(sc);
504c6131460SPaul Saab 
5053a31b7ebSMike Smith     error = 0;
5063a31b7ebSMike Smith  out:
50764a026bdSGavin Atkinson     if (error != 0) {
50864a026bdSGavin Atkinson 	/* ciss_free() expects the mutex to be held */
50964a026bdSGavin Atkinson 	mtx_lock(&sc->ciss_mtx);
5103a31b7ebSMike Smith 	ciss_free(sc);
51164a026bdSGavin Atkinson     }
5123a31b7ebSMike Smith     return(error);
5133a31b7ebSMike Smith }
5143a31b7ebSMike Smith 
5153a31b7ebSMike Smith /************************************************************************
5163a31b7ebSMike Smith  * Detach the driver from this adapter.
5173a31b7ebSMike Smith  */
5183a31b7ebSMike Smith static int
5193a31b7ebSMike Smith ciss_detach(device_t dev)
5203a31b7ebSMike Smith {
5213a31b7ebSMike Smith     struct ciss_softc	*sc = device_get_softc(dev);
5223a31b7ebSMike Smith 
5233a31b7ebSMike Smith     debug_called(1);
5243a31b7ebSMike Smith 
525975b7318SScott Long     mtx_lock(&sc->ciss_mtx);
526975b7318SScott Long     if (sc->ciss_flags & CISS_FLAG_CONTROL_OPEN) {
527975b7318SScott Long 	mtx_unlock(&sc->ciss_mtx);
528ffdf82e1SPaul Saab 	return (EBUSY);
529975b7318SScott Long     }
530ffdf82e1SPaul Saab 
5313a31b7ebSMike Smith     /* flush adapter cache */
5323a31b7ebSMike Smith     ciss_flush_adapter(sc);
5333a31b7ebSMike Smith 
534975b7318SScott Long     /* release all resources.  The mutex is released and freed here too. */
5353a31b7ebSMike Smith     ciss_free(sc);
5363a31b7ebSMike Smith 
5373a31b7ebSMike Smith     return(0);
5383a31b7ebSMike Smith }
5393a31b7ebSMike Smith 
5403a31b7ebSMike Smith /************************************************************************
5413a31b7ebSMike Smith  * Prepare adapter for system shutdown.
5423a31b7ebSMike Smith  */
5433a31b7ebSMike Smith static int
5443a31b7ebSMike Smith ciss_shutdown(device_t dev)
5453a31b7ebSMike Smith {
5463a31b7ebSMike Smith     struct ciss_softc	*sc = device_get_softc(dev);
5473a31b7ebSMike Smith 
5483a31b7ebSMike Smith     debug_called(1);
5493a31b7ebSMike Smith 
550d4a4ddc6SScott Long     mtx_lock(&sc->ciss_mtx);
5513a31b7ebSMike Smith     /* flush adapter cache */
5523a31b7ebSMike Smith     ciss_flush_adapter(sc);
5533a31b7ebSMike Smith 
55417c0792dSPaul Saab     if (sc->ciss_soft_reset)
55517c0792dSPaul Saab 	ciss_soft_reset(sc);
556d4a4ddc6SScott Long     mtx_unlock(&sc->ciss_mtx);
55717c0792dSPaul Saab 
5583a31b7ebSMike Smith     return(0);
5593a31b7ebSMike Smith }
5603a31b7ebSMike Smith 
56117c0792dSPaul Saab static void
56217c0792dSPaul Saab ciss_init_sysctl(struct ciss_softc *sc)
56317c0792dSPaul Saab {
56417c0792dSPaul Saab 
56517c0792dSPaul Saab     SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->ciss_dev),
56617c0792dSPaul Saab 	SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ciss_dev)),
56717c0792dSPaul Saab 	OID_AUTO, "soft_reset", CTLFLAG_RW, &sc->ciss_soft_reset, 0, "");
56817c0792dSPaul Saab }
56917c0792dSPaul Saab 
5703a31b7ebSMike Smith /************************************************************************
5713a31b7ebSMike Smith  * Perform PCI-specific attachment actions.
5723a31b7ebSMike Smith  */
5733a31b7ebSMike Smith static int
5743a31b7ebSMike Smith ciss_init_pci(struct ciss_softc *sc)
5753a31b7ebSMike Smith {
5763a31b7ebSMike Smith     uintptr_t		cbase, csize, cofs;
57722657ce1SScott Long     uint32_t		method, supported_methods;
578d69c9c78SScott Long     int			error, sqmask, i;
57922657ce1SScott Long     void		*intr;
5803a31b7ebSMike Smith 
5813a31b7ebSMike Smith     debug_called(1);
5823a31b7ebSMike Smith 
5833a31b7ebSMike Smith     /*
584d69c9c78SScott Long      * Work out adapter type.
585d69c9c78SScott Long      */
586d69c9c78SScott Long     i = ciss_lookup(sc->ciss_dev);
587d69c9c78SScott Long     if (i < 0) {
588d69c9c78SScott Long 	ciss_printf(sc, "unknown adapter type\n");
589d69c9c78SScott Long 	return (ENXIO);
590d69c9c78SScott Long     }
591d69c9c78SScott Long 
592d69c9c78SScott Long     if (ciss_vendor_data[i].flags & CISS_BOARD_SA5) {
593d69c9c78SScott Long 	sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5;
594d69c9c78SScott Long     } else if (ciss_vendor_data[i].flags & CISS_BOARD_SA5B) {
595d69c9c78SScott Long 	sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5B;
596d69c9c78SScott Long     } else {
597d69c9c78SScott Long 	/*
598d69c9c78SScott Long 	 * XXX Big hammer, masks/unmasks all possible interrupts.  This should
599d69c9c78SScott Long 	 * work on all hardware variants.  Need to add code to handle the
600d69c9c78SScott Long 	 * "controller crashed" interupt bit that this unmasks.
601d69c9c78SScott Long 	 */
602d69c9c78SScott Long 	sqmask = ~0;
603d69c9c78SScott Long     }
604d69c9c78SScott Long 
605d69c9c78SScott Long     /*
6063a31b7ebSMike Smith      * Allocate register window first (we need this to find the config
6073a31b7ebSMike Smith      * struct).
6083a31b7ebSMike Smith      */
6093a31b7ebSMike Smith     error = ENXIO;
6103a31b7ebSMike Smith     sc->ciss_regs_rid = CISS_TL_SIMPLE_BAR_REGS;
6113a31b7ebSMike Smith     if ((sc->ciss_regs_resource =
6125f96beb9SNate Lawson 	 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
6135f96beb9SNate Lawson 				&sc->ciss_regs_rid, RF_ACTIVE)) == NULL) {
6143a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate register window\n");
6153a31b7ebSMike Smith 	return(ENXIO);
6163a31b7ebSMike Smith     }
6173a31b7ebSMike Smith     sc->ciss_regs_bhandle = rman_get_bushandle(sc->ciss_regs_resource);
6183a31b7ebSMike Smith     sc->ciss_regs_btag = rman_get_bustag(sc->ciss_regs_resource);
6193a31b7ebSMike Smith 
6203a31b7ebSMike Smith     /*
6213a31b7ebSMike Smith      * Find the BAR holding the config structure.  If it's not the one
6223a31b7ebSMike Smith      * we already mapped for registers, map it too.
6233a31b7ebSMike Smith      */
6243a31b7ebSMike Smith     sc->ciss_cfg_rid = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_BAR) & 0xffff;
6253a31b7ebSMike Smith     if (sc->ciss_cfg_rid != sc->ciss_regs_rid) {
6263a31b7ebSMike Smith 	if ((sc->ciss_cfg_resource =
6275f96beb9SNate Lawson 	     bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
6285f96beb9SNate Lawson 				    &sc->ciss_cfg_rid, RF_ACTIVE)) == NULL) {
6293a31b7ebSMike Smith 	    ciss_printf(sc, "can't allocate config window\n");
6303a31b7ebSMike Smith 	    return(ENXIO);
6313a31b7ebSMike Smith 	}
6323a31b7ebSMike Smith 	cbase = (uintptr_t)rman_get_virtual(sc->ciss_cfg_resource);
6333a31b7ebSMike Smith 	csize = rman_get_end(sc->ciss_cfg_resource) -
6343a31b7ebSMike Smith 	    rman_get_start(sc->ciss_cfg_resource) + 1;
6353a31b7ebSMike Smith     } else {
6363a31b7ebSMike Smith 	cbase = (uintptr_t)rman_get_virtual(sc->ciss_regs_resource);
6373a31b7ebSMike Smith 	csize = rman_get_end(sc->ciss_regs_resource) -
6383a31b7ebSMike Smith 	    rman_get_start(sc->ciss_regs_resource) + 1;
6393a31b7ebSMike Smith     }
6403a31b7ebSMike Smith     cofs = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_OFF);
6413a31b7ebSMike Smith 
6423a31b7ebSMike Smith     /*
6433a31b7ebSMike Smith      * Use the base/size/offset values we just calculated to
6443a31b7ebSMike Smith      * sanity-check the config structure.  If it's OK, point to it.
6453a31b7ebSMike Smith      */
6463a31b7ebSMike Smith     if ((cofs + sizeof(struct ciss_config_table)) > csize) {
6473a31b7ebSMike Smith 	ciss_printf(sc, "config table outside window\n");
6483a31b7ebSMike Smith 	return(ENXIO);
6493a31b7ebSMike Smith     }
6503a31b7ebSMike Smith     sc->ciss_cfg = (struct ciss_config_table *)(cbase + cofs);
6513a31b7ebSMike Smith     debug(1, "config struct at %p", sc->ciss_cfg);
6523a31b7ebSMike Smith 
6533a31b7ebSMike Smith     /*
65422657ce1SScott Long      * Calculate the number of request structures/commands we are
65522657ce1SScott Long      * going to provide for this adapter.
65622657ce1SScott Long      */
65722657ce1SScott Long     sc->ciss_max_requests = min(CISS_MAX_REQUESTS, sc->ciss_cfg->max_outstanding_commands);
65822657ce1SScott Long 
65922657ce1SScott Long     /*
6603a31b7ebSMike Smith      * Validate the config structure.  If we supported other transport
6613a31b7ebSMike Smith      * methods, we could select amongst them at this point in time.
6623a31b7ebSMike Smith      */
6633a31b7ebSMike Smith     if (strncmp(sc->ciss_cfg->signature, "CISS", 4)) {
6643a31b7ebSMike Smith 	ciss_printf(sc, "config signature mismatch (got '%c%c%c%c')\n",
6653a31b7ebSMike Smith 		    sc->ciss_cfg->signature[0], sc->ciss_cfg->signature[1],
6663a31b7ebSMike Smith 		    sc->ciss_cfg->signature[2], sc->ciss_cfg->signature[3]);
6673a31b7ebSMike Smith 	return(ENXIO);
6683a31b7ebSMike Smith     }
6693a31b7ebSMike Smith 
6703a31b7ebSMike Smith     /*
67122657ce1SScott Long      * Select the mode of operation, prefer Performant.
6723a31b7ebSMike Smith      */
67322657ce1SScott Long     if (!(sc->ciss_cfg->supported_methods &
67422657ce1SScott Long 	(CISS_TRANSPORT_METHOD_SIMPLE | CISS_TRANSPORT_METHOD_PERF))) {
67522657ce1SScott Long 	ciss_printf(sc, "No supported transport layers: 0x%x\n",
67622657ce1SScott Long 	    sc->ciss_cfg->supported_methods);
6773a31b7ebSMike Smith     }
67822657ce1SScott Long 
67922657ce1SScott Long     switch (ciss_force_transport) {
68022657ce1SScott Long     case 1:
68122657ce1SScott Long 	supported_methods = CISS_TRANSPORT_METHOD_SIMPLE;
68222657ce1SScott Long 	break;
68322657ce1SScott Long     case 2:
68422657ce1SScott Long 	supported_methods = CISS_TRANSPORT_METHOD_PERF;
68522657ce1SScott Long 	break;
68622657ce1SScott Long     default:
687ee4827b2SSean Bruno         /*
688ee4827b2SSean Bruno          * Override the capabilities of the BOARD and specify SIMPLE
689ee4827b2SSean Bruno          * MODE
690ee4827b2SSean Bruno          */
691ee4827b2SSean Bruno         if (ciss_vendor_data[i].flags & CISS_BOARD_SIMPLE)
692ee4827b2SSean Bruno                 supported_methods = CISS_TRANSPORT_METHOD_SIMPLE;
693ee4827b2SSean Bruno         else
69422657ce1SScott Long                 supported_methods = sc->ciss_cfg->supported_methods;
69522657ce1SScott Long         break;
69622657ce1SScott Long     }
69722657ce1SScott Long 
69822657ce1SScott Long setup:
6992fdaa90dSScott Long     if ((supported_methods & CISS_TRANSPORT_METHOD_PERF) != 0) {
70022657ce1SScott Long 	method = CISS_TRANSPORT_METHOD_PERF;
70122657ce1SScott Long 	sc->ciss_perf = (struct ciss_perf_config *)(cbase + cofs +
70222657ce1SScott Long 	    sc->ciss_cfg->transport_offset);
70322657ce1SScott Long 	if (ciss_init_perf(sc)) {
70422657ce1SScott Long 	    supported_methods &= ~method;
70522657ce1SScott Long 	    goto setup;
70622657ce1SScott Long 	}
70722657ce1SScott Long     } else if (supported_methods & CISS_TRANSPORT_METHOD_SIMPLE) {
70822657ce1SScott Long 	method = CISS_TRANSPORT_METHOD_SIMPLE;
70922657ce1SScott Long     } else {
71022657ce1SScott Long 	ciss_printf(sc, "No supported transport methods: 0x%x\n",
71122657ce1SScott Long 	    sc->ciss_cfg->supported_methods);
71222657ce1SScott Long 	return(ENXIO);
71322657ce1SScott Long     }
71422657ce1SScott Long 
71522657ce1SScott Long     /*
71622657ce1SScott Long      * Tell it we're using the low 4GB of RAM.  Set the default interrupt
71722657ce1SScott Long      * coalescing options.
71822657ce1SScott Long      */
71922657ce1SScott Long     sc->ciss_cfg->requested_method = method;
7203a31b7ebSMike Smith     sc->ciss_cfg->command_physlimit = 0;
7213a31b7ebSMike Smith     sc->ciss_cfg->interrupt_coalesce_delay = CISS_INTERRUPT_COALESCE_DELAY;
7223a31b7ebSMike Smith     sc->ciss_cfg->interrupt_coalesce_count = CISS_INTERRUPT_COALESCE_COUNT;
7233a31b7ebSMike Smith 
724a891a3bfSPaul Saab #ifdef __i386__
725a891a3bfSPaul Saab     sc->ciss_cfg->host_driver |= CISS_DRIVER_SCSI_PREFETCH;
726a891a3bfSPaul Saab #endif
727a891a3bfSPaul Saab 
7283a31b7ebSMike Smith     if (ciss_update_config(sc)) {
7293a31b7ebSMike Smith 	ciss_printf(sc, "adapter refuses to accept config update (IDBR 0x%x)\n",
7303a31b7ebSMike Smith 		    CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR));
7313a31b7ebSMike Smith 	return(ENXIO);
7323a31b7ebSMike Smith     }
73322657ce1SScott Long     if ((sc->ciss_cfg->active_method & method) == 0) {
73422657ce1SScott Long 	supported_methods &= ~method;
73522657ce1SScott Long 	if (supported_methods == 0) {
73622657ce1SScott Long 	    ciss_printf(sc, "adapter refuses to go into available transports "
73722657ce1SScott Long 		"mode (0x%x, 0x%x)\n", supported_methods,
73822657ce1SScott Long 		sc->ciss_cfg->active_method);
7393a31b7ebSMike Smith 	    return(ENXIO);
74022657ce1SScott Long 	} else
74122657ce1SScott Long 	    goto setup;
7423a31b7ebSMike Smith     }
7433a31b7ebSMike Smith 
7443a31b7ebSMike Smith     /*
7453a31b7ebSMike Smith      * Wait for the adapter to come ready.
7463a31b7ebSMike Smith      */
7473a31b7ebSMike Smith     if ((error = ciss_wait_adapter(sc)) != 0)
7483a31b7ebSMike Smith 	return(error);
7493a31b7ebSMike Smith 
75022657ce1SScott Long     /* Prepare to possibly use MSIX and/or PERFORMANT interrupts.  Normal
75122657ce1SScott Long      * interrupts have a rid of 0, this will be overridden if MSIX is used.
75222657ce1SScott Long      */
75322657ce1SScott Long     sc->ciss_irq_rid[0] = 0;
75422657ce1SScott Long     if (method == CISS_TRANSPORT_METHOD_PERF) {
75522657ce1SScott Long 	ciss_printf(sc, "PERFORMANT Transport\n");
756d69c9c78SScott Long 	if ((ciss_force_interrupt != 1) && (ciss_setup_msix(sc) == 0)) {
75722657ce1SScott Long 	    intr = ciss_perf_msi_intr;
758d69c9c78SScott Long 	} else {
75922657ce1SScott Long 	    intr = ciss_perf_intr;
760d69c9c78SScott Long 	}
761b23be3e0SScott Long 	/* XXX The docs say that the 0x01 bit is only for SAS controllers.
762b23be3e0SScott Long 	 * Unfortunately, there is no good way to know if this is a SAS
763b23be3e0SScott Long 	 * controller.  Hopefully enabling this bit universally will work OK.
764b23be3e0SScott Long 	 * It seems to work fine for SA6i controllers.
765b23be3e0SScott Long 	 */
766b23be3e0SScott Long 	sc->ciss_interrupt_mask = CISS_TL_PERF_INTR_OPQ | CISS_TL_PERF_INTR_MSI;
767b23be3e0SScott Long 
76822657ce1SScott Long     } else {
76922657ce1SScott Long 	ciss_printf(sc, "SIMPLE Transport\n");
77022657ce1SScott Long 	/* MSIX doesn't seem to work in SIMPLE mode, only enable if it forced */
77122657ce1SScott Long 	if (ciss_force_interrupt == 2)
77222657ce1SScott Long 	    /* If this fails, we automatically revert to INTx */
77322657ce1SScott Long 	    ciss_setup_msix(sc);
77422657ce1SScott Long 	sc->ciss_perf = NULL;
77522657ce1SScott Long 	intr = ciss_intr;
776d69c9c78SScott Long 	sc->ciss_interrupt_mask = sqmask;
77722657ce1SScott Long     }
77822657ce1SScott Long 
7793a31b7ebSMike Smith     /*
7803a31b7ebSMike Smith      * Turn off interrupts before we go routing anything.
7813a31b7ebSMike Smith      */
7823a31b7ebSMike Smith     CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
7833a31b7ebSMike Smith 
7843a31b7ebSMike Smith     /*
7853a31b7ebSMike Smith      * Allocate and set up our interrupt.
7863a31b7ebSMike Smith      */
7873a31b7ebSMike Smith     if ((sc->ciss_irq_resource =
78822657ce1SScott Long 	 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_IRQ, &sc->ciss_irq_rid[0],
7893a31b7ebSMike Smith 				RF_ACTIVE | RF_SHAREABLE)) == NULL) {
7903a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate interrupt\n");
7913a31b7ebSMike Smith 	return(ENXIO);
7923a31b7ebSMike Smith     }
79322657ce1SScott Long 
794cc850363SPeter Wemm     if (bus_setup_intr(sc->ciss_dev, sc->ciss_irq_resource,
79522657ce1SScott Long 		       INTR_TYPE_CAM|INTR_MPSAFE, NULL, intr, sc,
7963a31b7ebSMike Smith 		       &sc->ciss_intr)) {
7973a31b7ebSMike Smith 	ciss_printf(sc, "can't set up interrupt\n");
7983a31b7ebSMike Smith 	return(ENXIO);
7993a31b7ebSMike Smith     }
8003a31b7ebSMike Smith 
8013a31b7ebSMike Smith     /*
8023a31b7ebSMike Smith      * Allocate the parent bus DMA tag appropriate for our PCI
8033a31b7ebSMike Smith      * interface.
8043a31b7ebSMike Smith      *
8053a31b7ebSMike Smith      * Note that "simple" adapters can only address within a 32-bit
8063a31b7ebSMike Smith      * span.
8073a31b7ebSMike Smith      */
808b6f97155SScott Long     if (bus_dma_tag_create(bus_get_dma_tag(sc->ciss_dev),/* PCI parent */
8093a31b7ebSMike Smith 			   1, 0, 			/* alignment, boundary */
810639717c8SPaul Saab 			   BUS_SPACE_MAXADDR,		/* lowaddr */
8113a31b7ebSMike Smith 			   BUS_SPACE_MAXADDR, 		/* highaddr */
8123a31b7ebSMike Smith 			   NULL, NULL, 			/* filter, filterarg */
813639717c8SPaul Saab 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
8142fdaa90dSScott Long 			   CISS_MAX_SG_ELEMENTS,	/* nsegments */
8153a31b7ebSMike Smith 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
81622657ce1SScott Long 			   0,				/* flags */
817f6b1c44dSScott Long 			   NULL, NULL,			/* lockfunc, lockarg */
8183a31b7ebSMike Smith 			   &sc->ciss_parent_dmat)) {
8193a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate parent DMA tag\n");
8203a31b7ebSMike Smith 	return(ENOMEM);
8213a31b7ebSMike Smith     }
8223a31b7ebSMike Smith 
8233a31b7ebSMike Smith     /*
8243a31b7ebSMike Smith      * Create DMA tag for mapping buffers into adapter-addressable
8253a31b7ebSMike Smith      * space.
8263a31b7ebSMike Smith      */
8273a31b7ebSMike Smith     if (bus_dma_tag_create(sc->ciss_parent_dmat, 	/* parent */
8283a31b7ebSMike Smith 			   1, 0, 			/* alignment, boundary */
8293a31b7ebSMike Smith 			   BUS_SPACE_MAXADDR,		/* lowaddr */
8303a31b7ebSMike Smith 			   BUS_SPACE_MAXADDR, 		/* highaddr */
8313a31b7ebSMike Smith 			   NULL, NULL, 			/* filter, filterarg */
8322fdaa90dSScott Long 			   MAXBSIZE, CISS_MAX_SG_ELEMENTS,	/* maxsize, nsegments */
8333a31b7ebSMike Smith 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
83422657ce1SScott Long 			   BUS_DMA_ALLOCNOW,		/* flags */
835975b7318SScott Long 			   busdma_lock_mutex, &sc->ciss_mtx,	/* lockfunc, lockarg */
8363a31b7ebSMike Smith 			   &sc->ciss_buffer_dmat)) {
8373a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate buffer DMA tag\n");
8383a31b7ebSMike Smith 	return(ENOMEM);
8393a31b7ebSMike Smith     }
8403a31b7ebSMike Smith     return(0);
8413a31b7ebSMike Smith }
8423a31b7ebSMike Smith 
8433a31b7ebSMike Smith /************************************************************************
84422657ce1SScott Long  * Setup MSI/MSIX operation (Performant only)
8452fdaa90dSScott Long  * Four interrupts are available, but we only use 1 right now.  If MSI-X
8462fdaa90dSScott Long  * isn't avaialble, try using MSI instead.
84722657ce1SScott Long  */
84822657ce1SScott Long static int
84922657ce1SScott Long ciss_setup_msix(struct ciss_softc *sc)
85022657ce1SScott Long {
85122657ce1SScott Long     int val, i;
85222657ce1SScott Long 
85322657ce1SScott Long     /* Weed out devices that don't actually support MSI */
8542fdaa90dSScott Long     i = ciss_lookup(sc->ciss_dev);
8552fdaa90dSScott Long     if (ciss_vendor_data[i].flags & CISS_BOARD_NOMSI)
85622657ce1SScott Long 	return (EINVAL);
85722657ce1SScott Long 
8582fdaa90dSScott Long     /*
8592fdaa90dSScott Long      * Only need to use the minimum number of MSI vectors, as the driver
8602fdaa90dSScott Long      * doesn't support directed MSIX interrupts.
8612fdaa90dSScott Long      */
86222657ce1SScott Long     val = pci_msix_count(sc->ciss_dev);
8632fdaa90dSScott Long     if (val < CISS_MSI_COUNT) {
8642fdaa90dSScott Long 	val = pci_msi_count(sc->ciss_dev);
8652fdaa90dSScott Long 	device_printf(sc->ciss_dev, "got %d MSI messages]\n", val);
866b23be3e0SScott Long 	if (val < CISS_MSI_COUNT)
867b23be3e0SScott Long 	    return (EINVAL);
8682fdaa90dSScott Long     }
869b23be3e0SScott Long     val = MIN(val, CISS_MSI_COUNT);
8702fdaa90dSScott Long     if (pci_alloc_msix(sc->ciss_dev, &val) != 0) {
8712fdaa90dSScott Long 	if (pci_alloc_msi(sc->ciss_dev, &val) != 0)
87222657ce1SScott Long 	    return (EINVAL);
8732fdaa90dSScott Long     }
87422657ce1SScott Long 
87522657ce1SScott Long     sc->ciss_msi = val;
8762fdaa90dSScott Long     if (bootverbose)
8772fdaa90dSScott Long 	ciss_printf(sc, "Using %d MSIX interrupt%s\n", val,
8782fdaa90dSScott Long 	    (val != 1) ? "s" : "");
87922657ce1SScott Long 
8802fdaa90dSScott Long     for (i = 0; i < val; i++)
88122657ce1SScott Long 	sc->ciss_irq_rid[i] = i + 1;
88222657ce1SScott Long 
88322657ce1SScott Long     return (0);
88422657ce1SScott Long 
88522657ce1SScott Long }
88622657ce1SScott Long 
88722657ce1SScott Long /************************************************************************
88822657ce1SScott Long  * Setup the Performant structures.
88922657ce1SScott Long  */
89022657ce1SScott Long static int
89122657ce1SScott Long ciss_init_perf(struct ciss_softc *sc)
89222657ce1SScott Long {
89322657ce1SScott Long     struct ciss_perf_config *pc = sc->ciss_perf;
89422657ce1SScott Long     int reply_size;
89522657ce1SScott Long 
89622657ce1SScott Long     /*
89722657ce1SScott Long      * Create the DMA tag for the reply queue.
89822657ce1SScott Long      */
89922657ce1SScott Long     reply_size = sizeof(uint64_t) * sc->ciss_max_requests;
90022657ce1SScott Long     if (bus_dma_tag_create(sc->ciss_parent_dmat,	/* parent */
90122657ce1SScott Long 			   1, 0, 			/* alignment, boundary */
90222657ce1SScott Long 			   BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
90322657ce1SScott Long 			   BUS_SPACE_MAXADDR, 		/* highaddr */
90422657ce1SScott Long 			   NULL, NULL, 			/* filter, filterarg */
90522657ce1SScott Long 			   reply_size, 1,		/* maxsize, nsegments */
90622657ce1SScott Long 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
90722657ce1SScott Long 			   0,				/* flags */
90822657ce1SScott Long 			   NULL, NULL,			/* lockfunc, lockarg */
90922657ce1SScott Long 			   &sc->ciss_reply_dmat)) {
91022657ce1SScott Long 	ciss_printf(sc, "can't allocate reply DMA tag\n");
91122657ce1SScott Long 	return(ENOMEM);
91222657ce1SScott Long     }
91322657ce1SScott Long     /*
91422657ce1SScott Long      * Allocate memory and make it available for DMA.
91522657ce1SScott Long      */
91622657ce1SScott Long     if (bus_dmamem_alloc(sc->ciss_reply_dmat, (void **)&sc->ciss_reply,
91722657ce1SScott Long 			 BUS_DMA_NOWAIT, &sc->ciss_reply_map)) {
91822657ce1SScott Long 	ciss_printf(sc, "can't allocate reply memory\n");
91922657ce1SScott Long 	return(ENOMEM);
92022657ce1SScott Long     }
92122657ce1SScott Long     bus_dmamap_load(sc->ciss_reply_dmat, sc->ciss_reply_map, sc->ciss_reply,
92222657ce1SScott Long 		    reply_size, ciss_command_map_helper, &sc->ciss_reply_phys, 0);
92322657ce1SScott Long     bzero(sc->ciss_reply, reply_size);
92422657ce1SScott Long 
92522657ce1SScott Long     sc->ciss_cycle = 0x1;
92622657ce1SScott Long     sc->ciss_rqidx = 0;
92722657ce1SScott Long 
92822657ce1SScott Long     /*
92922657ce1SScott Long      * Preload the fetch table with common command sizes.  This allows the
93022657ce1SScott Long      * hardware to not waste bus cycles for typical i/o commands, but also not
93122657ce1SScott Long      * tax the driver to be too exact in choosing sizes.  The table is optimized
93222657ce1SScott Long      * for page-aligned i/o's, but since most i/o comes from the various pagers,
93322657ce1SScott Long      * it's a reasonable assumption to make.
93422657ce1SScott Long      */
93522657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_NONE] = (sizeof(struct ciss_command) + 15) / 16;
93622657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_1] =
93722657ce1SScott Long 	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 1 + 15) / 16;
93822657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_2] =
93922657ce1SScott Long 	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 2 + 15) / 16;
94022657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_4] =
94122657ce1SScott Long 	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 4 + 15) / 16;
94222657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_8] =
94322657ce1SScott Long 	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 8 + 15) / 16;
94422657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_16] =
94522657ce1SScott Long 	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 16 + 15) / 16;
94622657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_32] =
94722657ce1SScott Long 	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 32 + 15) / 16;
94822657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_MAX] = (CISS_COMMAND_ALLOC_SIZE + 15) / 16;
94922657ce1SScott Long 
95022657ce1SScott Long     pc->rq_size = sc->ciss_max_requests; /* XXX less than the card supports? */
95122657ce1SScott Long     pc->rq_count = 1;	/* XXX Hardcode for a single queue */
95222657ce1SScott Long     pc->rq_bank_hi = 0;
95322657ce1SScott Long     pc->rq_bank_lo = 0;
95422657ce1SScott Long     pc->rq[0].rq_addr_hi = 0x0;
95522657ce1SScott Long     pc->rq[0].rq_addr_lo = sc->ciss_reply_phys;
95622657ce1SScott Long 
95722657ce1SScott Long     return(0);
95822657ce1SScott Long }
95922657ce1SScott Long 
96022657ce1SScott Long /************************************************************************
9613a31b7ebSMike Smith  * Wait for the adapter to come ready.
9623a31b7ebSMike Smith  */
9633a31b7ebSMike Smith static int
9643a31b7ebSMike Smith ciss_wait_adapter(struct ciss_softc *sc)
9653a31b7ebSMike Smith {
9663a31b7ebSMike Smith     int		i;
9673a31b7ebSMike Smith 
9683a31b7ebSMike Smith     debug_called(1);
9693a31b7ebSMike Smith 
9703a31b7ebSMike Smith     /*
9713a31b7ebSMike Smith      * Wait for the adapter to come ready.
9723a31b7ebSMike Smith      */
9733a31b7ebSMike Smith     if (!(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY)) {
9743a31b7ebSMike Smith 	ciss_printf(sc, "waiting for adapter to come ready...\n");
9753a31b7ebSMike Smith 	for (i = 0; !(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY); i++) {
9763a31b7ebSMike Smith 	    DELAY(1000000);	/* one second */
9773a31b7ebSMike Smith 	    if (i > 30) {
9783a31b7ebSMike Smith 		ciss_printf(sc, "timed out waiting for adapter to come ready\n");
9793a31b7ebSMike Smith 		return(EIO);
9803a31b7ebSMike Smith 	    }
9813a31b7ebSMike Smith 	}
9823a31b7ebSMike Smith     }
9833a31b7ebSMike Smith     return(0);
9843a31b7ebSMike Smith }
9853a31b7ebSMike Smith 
9863a31b7ebSMike Smith /************************************************************************
9873a31b7ebSMike Smith  * Flush the adapter cache.
9883a31b7ebSMike Smith  */
9893a31b7ebSMike Smith static int
9903a31b7ebSMike Smith ciss_flush_adapter(struct ciss_softc *sc)
9913a31b7ebSMike Smith {
9923a31b7ebSMike Smith     struct ciss_request			*cr;
9933a31b7ebSMike Smith     struct ciss_bmic_flush_cache	*cbfc;
9943a31b7ebSMike Smith     int					error, command_status;
9953a31b7ebSMike Smith 
9963a31b7ebSMike Smith     debug_called(1);
9973a31b7ebSMike Smith 
9983a31b7ebSMike Smith     cr = NULL;
9993a31b7ebSMike Smith     cbfc = NULL;
10003a31b7ebSMike Smith 
10013a31b7ebSMike Smith     /*
10023a31b7ebSMike Smith      * Build a BMIC request to flush the cache.  We don't disable
10033a31b7ebSMike Smith      * it, as we may be going to do more I/O (eg. we are emulating
10043a31b7ebSMike Smith      * the Synchronise Cache command).
10053a31b7ebSMike Smith      */
10063a31b7ebSMike Smith     if ((cbfc = malloc(sizeof(*cbfc), CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
10073a31b7ebSMike Smith 	error = ENOMEM;
10083a31b7ebSMike Smith 	goto out;
10093a31b7ebSMike Smith     }
10103a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_FLUSH_CACHE,
10113a31b7ebSMike Smith 				       (void **)&cbfc, sizeof(*cbfc))) != 0)
10123a31b7ebSMike Smith 	goto out;
10133a31b7ebSMike Smith 
10143a31b7ebSMike Smith     /*
10153a31b7ebSMike Smith      * Submit the request and wait for it to complete.
10163a31b7ebSMike Smith      */
10173a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
10183a31b7ebSMike Smith 	ciss_printf(sc, "error sending BMIC FLUSH_CACHE command (%d)\n", error);
10193a31b7ebSMike Smith 	goto out;
10203a31b7ebSMike Smith     }
10213a31b7ebSMike Smith 
10223a31b7ebSMike Smith     /*
10233a31b7ebSMike Smith      * Check response.
10243a31b7ebSMike Smith      */
10253a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
10263a31b7ebSMike Smith     switch(command_status) {
10273a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:
10283a31b7ebSMike Smith 	break;
10293a31b7ebSMike Smith     default:
10303a31b7ebSMike Smith 	ciss_printf(sc, "error flushing cache (%s)\n",
10313a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
10323a31b7ebSMike Smith 	error = EIO;
10333a31b7ebSMike Smith 	goto out;
10343a31b7ebSMike Smith     }
10353a31b7ebSMike Smith 
10363a31b7ebSMike Smith out:
10373a31b7ebSMike Smith     if (cbfc != NULL)
10383a31b7ebSMike Smith 	free(cbfc, CISS_MALLOC_CLASS);
10393a31b7ebSMike Smith     if (cr != NULL)
10403a31b7ebSMike Smith 	ciss_release_request(cr);
10413a31b7ebSMike Smith     return(error);
10423a31b7ebSMike Smith }
10433a31b7ebSMike Smith 
104417c0792dSPaul Saab static void
104517c0792dSPaul Saab ciss_soft_reset(struct ciss_softc *sc)
104617c0792dSPaul Saab {
104717c0792dSPaul Saab     struct ciss_request		*cr = NULL;
104817c0792dSPaul Saab     struct ciss_command		*cc;
104917c0792dSPaul Saab     int				i, error = 0;
105017c0792dSPaul Saab 
105117c0792dSPaul Saab     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
105217c0792dSPaul Saab 	/* only reset proxy controllers */
105317c0792dSPaul Saab 	if (sc->ciss_controllers[i].physical.bus == 0)
105417c0792dSPaul Saab 	    continue;
105517c0792dSPaul Saab 
105617c0792dSPaul Saab 	if ((error = ciss_get_request(sc, &cr)) != 0)
105717c0792dSPaul Saab 	    break;
105817c0792dSPaul Saab 
105917c0792dSPaul Saab 	if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_SOFT_RESET,
106017c0792dSPaul Saab 					   NULL, 0)) != 0)
106117c0792dSPaul Saab 	    break;
106217c0792dSPaul Saab 
10632fdaa90dSScott Long 	cc = cr->cr_cc;
106417c0792dSPaul Saab 	cc->header.address = sc->ciss_controllers[i];
106517c0792dSPaul Saab 
106617c0792dSPaul Saab 	if ((error = ciss_synch_request(cr, 60 * 1000)) != 0)
106717c0792dSPaul Saab 	    break;
106817c0792dSPaul Saab 
106917c0792dSPaul Saab 	ciss_release_request(cr);
107017c0792dSPaul Saab     }
107117c0792dSPaul Saab 
107217c0792dSPaul Saab     if (error)
107317c0792dSPaul Saab 	ciss_printf(sc, "error resetting controller (%d)\n", error);
107417c0792dSPaul Saab 
107517c0792dSPaul Saab     if (cr != NULL)
107617c0792dSPaul Saab 	ciss_release_request(cr);
107717c0792dSPaul Saab }
107817c0792dSPaul Saab 
10793a31b7ebSMike Smith /************************************************************************
10803a31b7ebSMike Smith  * Allocate memory for the adapter command structures, initialise
10813a31b7ebSMike Smith  * the request structures.
10823a31b7ebSMike Smith  *
10833a31b7ebSMike Smith  * Note that the entire set of commands are allocated in a single
10843a31b7ebSMike Smith  * contiguous slab.
10853a31b7ebSMike Smith  */
10863a31b7ebSMike Smith static int
10873a31b7ebSMike Smith ciss_init_requests(struct ciss_softc *sc)
10883a31b7ebSMike Smith {
10893a31b7ebSMike Smith     struct ciss_request	*cr;
10903a31b7ebSMike Smith     int			i;
10913a31b7ebSMike Smith 
10923a31b7ebSMike Smith     debug_called(1);
10933a31b7ebSMike Smith 
10944bca277bSPaul Saab     if (bootverbose)
10953a31b7ebSMike Smith 	ciss_printf(sc, "using %d of %d available commands\n",
10963a31b7ebSMike Smith 		    sc->ciss_max_requests, sc->ciss_cfg->max_outstanding_commands);
10973a31b7ebSMike Smith 
10983a31b7ebSMike Smith     /*
10993a31b7ebSMike Smith      * Create the DMA tag for commands.
11003a31b7ebSMike Smith      */
11013a31b7ebSMike Smith     if (bus_dma_tag_create(sc->ciss_parent_dmat,	/* parent */
110222657ce1SScott Long 			   32, 0, 			/* alignment, boundary */
1103639717c8SPaul Saab 			   BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
11043a31b7ebSMike Smith 			   BUS_SPACE_MAXADDR, 		/* highaddr */
11053a31b7ebSMike Smith 			   NULL, NULL, 			/* filter, filterarg */
11063a31b7ebSMike Smith 			   CISS_COMMAND_ALLOC_SIZE *
11073a31b7ebSMike Smith 			   sc->ciss_max_requests, 1,	/* maxsize, nsegments */
11083a31b7ebSMike Smith 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
110922657ce1SScott Long 			   0,				/* flags */
1110639717c8SPaul Saab 			   NULL, NULL,			/* lockfunc, lockarg */
11113a31b7ebSMike Smith 			   &sc->ciss_command_dmat)) {
11123a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate command DMA tag\n");
11133a31b7ebSMike Smith 	return(ENOMEM);
11143a31b7ebSMike Smith     }
11153a31b7ebSMike Smith     /*
11163a31b7ebSMike Smith      * Allocate memory and make it available for DMA.
11173a31b7ebSMike Smith      */
11183a31b7ebSMike Smith     if (bus_dmamem_alloc(sc->ciss_command_dmat, (void **)&sc->ciss_command,
11193a31b7ebSMike Smith 			 BUS_DMA_NOWAIT, &sc->ciss_command_map)) {
11203a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate command memory\n");
11213a31b7ebSMike Smith 	return(ENOMEM);
11223a31b7ebSMike Smith     }
11233a31b7ebSMike Smith     bus_dmamap_load(sc->ciss_command_dmat, sc->ciss_command_map,sc->ciss_command,
1124be241f79SPaul Saab 		    CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests,
112522657ce1SScott Long 		    ciss_command_map_helper, &sc->ciss_command_phys, 0);
11263a31b7ebSMike Smith     bzero(sc->ciss_command, CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests);
11273a31b7ebSMike Smith 
11283a31b7ebSMike Smith     /*
11293a31b7ebSMike Smith      * Set up the request and command structures, push requests onto
11303a31b7ebSMike Smith      * the free queue.
11313a31b7ebSMike Smith      */
11323a31b7ebSMike Smith     for (i = 1; i < sc->ciss_max_requests; i++) {
11333a31b7ebSMike Smith 	cr = &sc->ciss_request[i];
11343a31b7ebSMike Smith 	cr->cr_sc = sc;
11353a31b7ebSMike Smith 	cr->cr_tag = i;
11362fdaa90dSScott Long 	cr->cr_cc = (struct ciss_command *)((uintptr_t)sc->ciss_command +
11372fdaa90dSScott Long 	    CISS_COMMAND_ALLOC_SIZE * i);
11382fdaa90dSScott Long 	cr->cr_ccphys = sc->ciss_command_phys + CISS_COMMAND_ALLOC_SIZE * i;
11393284b9eeSPaul Saab 	bus_dmamap_create(sc->ciss_buffer_dmat, 0, &cr->cr_datamap);
11403a31b7ebSMike Smith 	ciss_enqueue_free(cr);
11413a31b7ebSMike Smith     }
11423a31b7ebSMike Smith     return(0);
11433a31b7ebSMike Smith }
11443a31b7ebSMike Smith 
11453a31b7ebSMike Smith static void
11463a31b7ebSMike Smith ciss_command_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
11473a31b7ebSMike Smith {
114822657ce1SScott Long     uint32_t *addr;
11493a31b7ebSMike Smith 
115022657ce1SScott Long     addr = arg;
115122657ce1SScott Long     *addr = segs[0].ds_addr;
11523a31b7ebSMike Smith }
11533a31b7ebSMike Smith 
11543a31b7ebSMike Smith /************************************************************************
11553a31b7ebSMike Smith  * Identify the adapter, print some information about it.
11563a31b7ebSMike Smith  */
11573a31b7ebSMike Smith static int
11583a31b7ebSMike Smith ciss_identify_adapter(struct ciss_softc *sc)
11593a31b7ebSMike Smith {
11603a31b7ebSMike Smith     struct ciss_request	*cr;
11613a31b7ebSMike Smith     int			error, command_status;
11623a31b7ebSMike Smith 
11633a31b7ebSMike Smith     debug_called(1);
11643a31b7ebSMike Smith 
11653a31b7ebSMike Smith     cr = NULL;
11663a31b7ebSMike Smith 
11673a31b7ebSMike Smith     /*
11683a31b7ebSMike Smith      * Get a request, allocate storage for the adapter data.
11693a31b7ebSMike Smith      */
11703a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_CTLR,
11713a31b7ebSMike Smith 				       (void **)&sc->ciss_id,
11723a31b7ebSMike Smith 				       sizeof(*sc->ciss_id))) != 0)
11733a31b7ebSMike Smith 	goto out;
11743a31b7ebSMike Smith 
11753a31b7ebSMike Smith     /*
11763a31b7ebSMike Smith      * Submit the request and wait for it to complete.
11773a31b7ebSMike Smith      */
11783a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
11793a31b7ebSMike Smith 	ciss_printf(sc, "error sending BMIC ID_CTLR command (%d)\n", error);
11803a31b7ebSMike Smith 	goto out;
11813a31b7ebSMike Smith     }
11823a31b7ebSMike Smith 
11833a31b7ebSMike Smith     /*
11843a31b7ebSMike Smith      * Check response.
11853a31b7ebSMike Smith      */
11863a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
11873a31b7ebSMike Smith     switch(command_status) {
11883a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
11893a31b7ebSMike Smith 	break;
11903a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_UNDERRUN:
11913a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_OVERRUN:
11923a31b7ebSMike Smith 	ciss_printf(sc, "data over/underrun reading adapter information\n");
11933a31b7ebSMike Smith     default:
11943a31b7ebSMike Smith 	ciss_printf(sc, "error reading adapter information (%s)\n",
11953a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
11963a31b7ebSMike Smith 	error = EIO;
11973a31b7ebSMike Smith 	goto out;
11983a31b7ebSMike Smith     }
11993a31b7ebSMike Smith 
12003a31b7ebSMike Smith     /* sanity-check reply */
12013a31b7ebSMike Smith     if (!sc->ciss_id->big_map_supported) {
12023a31b7ebSMike Smith 	ciss_printf(sc, "adapter does not support BIG_MAP\n");
12033a31b7ebSMike Smith 	error = ENXIO;
12043a31b7ebSMike Smith 	goto out;
12053a31b7ebSMike Smith     }
12063a31b7ebSMike Smith 
1207d4aa427fSPaul Saab #if 0
12083a31b7ebSMike Smith     /* XXX later revisions may not need this */
12093a31b7ebSMike Smith     sc->ciss_flags |= CISS_FLAG_FAKE_SYNCH;
1210d4aa427fSPaul Saab #endif
12113a31b7ebSMike Smith 
12123a31b7ebSMike Smith     /* XXX only really required for old 5300 adapters? */
12133a31b7ebSMike Smith     sc->ciss_flags |= CISS_FLAG_BMIC_ABORT;
12143a31b7ebSMike Smith 
1215e3edca22SSean Bruno     /*
1216e3edca22SSean Bruno      * Earlier controller specs do not contain these config
1217e3edca22SSean Bruno      * entries, so assume that a 0 means its old and assign
1218e3edca22SSean Bruno      * these values to the defaults that were established
1219e3edca22SSean Bruno      * when this driver was developed for them
1220e3edca22SSean Bruno      */
1221e3edca22SSean Bruno     if (sc->ciss_cfg->max_logical_supported == 0)
1222e3edca22SSean Bruno         sc->ciss_cfg->max_logical_supported = CISS_MAX_LOGICAL;
1223e3edca22SSean Bruno     if (sc->ciss_cfg->max_physical_supported == 0)
1224e3edca22SSean Bruno 	sc->ciss_cfg->max_physical_supported = CISS_MAX_PHYSICAL;
12253a31b7ebSMike Smith     /* print information */
12264bca277bSPaul Saab     if (bootverbose) {
12273a31b7ebSMike Smith 	ciss_printf(sc, "  %d logical drive%s configured\n",
12283a31b7ebSMike Smith 		    sc->ciss_id->configured_logical_drives,
12293a31b7ebSMike Smith 		    (sc->ciss_id->configured_logical_drives == 1) ? "" : "s");
12303a31b7ebSMike Smith 	ciss_printf(sc, "  firmware %4.4s\n", sc->ciss_id->running_firmware_revision);
12313a31b7ebSMike Smith 	ciss_printf(sc, "  %d SCSI channels\n", sc->ciss_id->scsi_bus_count);
12323a31b7ebSMike Smith 
12333a31b7ebSMike Smith 	ciss_printf(sc, "  signature '%.4s'\n", sc->ciss_cfg->signature);
12343a31b7ebSMike Smith 	ciss_printf(sc, "  valence %d\n", sc->ciss_cfg->valence);
12353a31b7ebSMike Smith 	ciss_printf(sc, "  supported I/O methods 0x%b\n",
12363a31b7ebSMike Smith 		    sc->ciss_cfg->supported_methods,
12373a31b7ebSMike Smith 		    "\20\1READY\2simple\3performant\4MEMQ\n");
12383a31b7ebSMike Smith 	ciss_printf(sc, "  active I/O method 0x%b\n",
12393a31b7ebSMike Smith 		    sc->ciss_cfg->active_method, "\20\2simple\3performant\4MEMQ\n");
12403a31b7ebSMike Smith 	ciss_printf(sc, "  4G page base 0x%08x\n",
12413a31b7ebSMike Smith 		    sc->ciss_cfg->command_physlimit);
12423a31b7ebSMike Smith 	ciss_printf(sc, "  interrupt coalesce delay %dus\n",
12433a31b7ebSMike Smith 		    sc->ciss_cfg->interrupt_coalesce_delay);
12443a31b7ebSMike Smith 	ciss_printf(sc, "  interrupt coalesce count %d\n",
12453a31b7ebSMike Smith 		    sc->ciss_cfg->interrupt_coalesce_count);
12463a31b7ebSMike Smith 	ciss_printf(sc, "  max outstanding commands %d\n",
12473a31b7ebSMike Smith 		    sc->ciss_cfg->max_outstanding_commands);
12483a31b7ebSMike Smith 	ciss_printf(sc, "  bus types 0x%b\n", sc->ciss_cfg->bus_types,
12493a31b7ebSMike Smith 		    "\20\1ultra2\2ultra3\10fibre1\11fibre2\n");
12503a31b7ebSMike Smith 	ciss_printf(sc, "  server name '%.16s'\n", sc->ciss_cfg->server_name);
12513a31b7ebSMike Smith 	ciss_printf(sc, "  heartbeat 0x%x\n", sc->ciss_cfg->heartbeat);
1252e3edca22SSean Bruno     	ciss_printf(sc, "  max logical logical volumes: %d\n", sc->ciss_cfg->max_logical_supported);
1253e3edca22SSean Bruno     	ciss_printf(sc, "  max physical disks supported: %d\n", sc->ciss_cfg->max_physical_supported);
1254e3edca22SSean Bruno     	ciss_printf(sc, "  max physical disks per logical volume: %d\n", sc->ciss_cfg->max_physical_per_logical);
12553a31b7ebSMike Smith     }
12563a31b7ebSMike Smith 
12573a31b7ebSMike Smith out:
12583a31b7ebSMike Smith     if (error) {
12593a31b7ebSMike Smith 	if (sc->ciss_id != NULL) {
12603a31b7ebSMike Smith 	    free(sc->ciss_id, CISS_MALLOC_CLASS);
12613a31b7ebSMike Smith 	    sc->ciss_id = NULL;
12623a31b7ebSMike Smith 	}
12633a31b7ebSMike Smith     }
12643a31b7ebSMike Smith     if (cr != NULL)
12653a31b7ebSMike Smith 	ciss_release_request(cr);
12663a31b7ebSMike Smith     return(error);
12673a31b7ebSMike Smith }
12683a31b7ebSMike Smith 
12693a31b7ebSMike Smith /************************************************************************
1270c6131460SPaul Saab  * Helper routine for generating a list of logical and physical luns.
12713a31b7ebSMike Smith  */
1272c6131460SPaul Saab static struct ciss_lun_report *
1273c6131460SPaul Saab ciss_report_luns(struct ciss_softc *sc, int opcode, int nunits)
12743a31b7ebSMike Smith {
12753a31b7ebSMike Smith     struct ciss_request		*cr;
12763a31b7ebSMike Smith     struct ciss_command		*cc;
12773a31b7ebSMike Smith     struct ciss_report_cdb	*crc;
12783a31b7ebSMike Smith     struct ciss_lun_report	*cll;
12793a31b7ebSMike Smith     int				command_status;
1280c6131460SPaul Saab     int				report_size;
1281c6131460SPaul Saab     int				error = 0;
12823a31b7ebSMike Smith 
12833a31b7ebSMike Smith     debug_called(1);
12843a31b7ebSMike Smith 
12853a31b7ebSMike Smith     cr = NULL;
12863a31b7ebSMike Smith     cll = NULL;
12873a31b7ebSMike Smith 
12883a31b7ebSMike Smith     /*
12893a31b7ebSMike Smith      * Get a request, allocate storage for the address list.
12903a31b7ebSMike Smith      */
12913a31b7ebSMike Smith     if ((error = ciss_get_request(sc, &cr)) != 0)
12923a31b7ebSMike Smith 	goto out;
1293c6131460SPaul Saab     report_size = sizeof(*cll) + nunits * sizeof(union ciss_device_address);
12943a31b7ebSMike Smith     if ((cll = malloc(report_size, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
1295c6131460SPaul Saab 	ciss_printf(sc, "can't allocate memory for lun report\n");
12963a31b7ebSMike Smith 	error = ENOMEM;
12973a31b7ebSMike Smith 	goto out;
12983a31b7ebSMike Smith     }
12993a31b7ebSMike Smith 
13003a31b7ebSMike Smith     /*
1301c6131460SPaul Saab      * Build the Report Logical/Physical LUNs command.
13023a31b7ebSMike Smith      */
13032fdaa90dSScott Long     cc = cr->cr_cc;
13043a31b7ebSMike Smith     cr->cr_data = cll;
13053a31b7ebSMike Smith     cr->cr_length = report_size;
13063a31b7ebSMike Smith     cr->cr_flags = CISS_REQ_DATAIN;
13073a31b7ebSMike Smith 
13083a31b7ebSMike Smith     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
13093a31b7ebSMike Smith     cc->header.address.physical.bus = 0;
13103a31b7ebSMike Smith     cc->header.address.physical.target = 0;
13113a31b7ebSMike Smith     cc->cdb.cdb_length = sizeof(*crc);
13123a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
13133a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
13143a31b7ebSMike Smith     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
13153a31b7ebSMike Smith     cc->cdb.timeout = 30;	/* XXX better suggestions? */
13163a31b7ebSMike Smith 
13173a31b7ebSMike Smith     crc = (struct ciss_report_cdb *)&(cc->cdb.cdb[0]);
13183a31b7ebSMike Smith     bzero(crc, sizeof(*crc));
1319c6131460SPaul Saab     crc->opcode = opcode;
13203a31b7ebSMike Smith     crc->length = htonl(report_size);			/* big-endian field */
13213a31b7ebSMike Smith     cll->list_size = htonl(report_size - sizeof(*cll));	/* big-endian field */
13223a31b7ebSMike Smith 
13233a31b7ebSMike Smith     /*
13243a31b7ebSMike Smith      * Submit the request and wait for it to complete.  (timeout
13253a31b7ebSMike Smith      * here should be much greater than above)
13263a31b7ebSMike Smith      */
13273a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1328c6131460SPaul Saab 	ciss_printf(sc, "error sending %d LUN command (%d)\n", opcode, error);
13293a31b7ebSMike Smith 	goto out;
13303a31b7ebSMike Smith     }
13313a31b7ebSMike Smith 
13323a31b7ebSMike Smith     /*
13333a31b7ebSMike Smith      * Check response.  Note that data over/underrun is OK.
13343a31b7ebSMike Smith      */
13353a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
13363a31b7ebSMike Smith     switch(command_status) {
13373a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:	/* buffer right size */
13383a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_UNDERRUN:	/* buffer too large, not bad */
13393a31b7ebSMike Smith 	break;
13403a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_OVERRUN:
1341c6131460SPaul Saab 	ciss_printf(sc, "WARNING: more units than driver limit (%d)\n",
1342e3edca22SSean Bruno 		    sc->ciss_cfg->max_logical_supported);
13433a31b7ebSMike Smith 	break;
13443a31b7ebSMike Smith     default:
13453a31b7ebSMike Smith 	ciss_printf(sc, "error detecting logical drive configuration (%s)\n",
13463a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
13473a31b7ebSMike Smith 	error = EIO;
13483a31b7ebSMike Smith 	goto out;
13493a31b7ebSMike Smith     }
13503a31b7ebSMike Smith     ciss_release_request(cr);
13513a31b7ebSMike Smith     cr = NULL;
13523a31b7ebSMike Smith 
1353c6131460SPaul Saab out:
1354c6131460SPaul Saab     if (cr != NULL)
1355c6131460SPaul Saab 	ciss_release_request(cr);
1356c6131460SPaul Saab     if (error && cll != NULL) {
1357c6131460SPaul Saab 	free(cll, CISS_MALLOC_CLASS);
1358c6131460SPaul Saab 	cll = NULL;
1359c6131460SPaul Saab     }
1360c6131460SPaul Saab     return(cll);
1361c6131460SPaul Saab }
1362c6131460SPaul Saab 
1363c6131460SPaul Saab /************************************************************************
1364c6131460SPaul Saab  * Find logical drives on the adapter.
1365c6131460SPaul Saab  */
1366c6131460SPaul Saab static int
1367c6131460SPaul Saab ciss_init_logical(struct ciss_softc *sc)
1368c6131460SPaul Saab {
1369c6131460SPaul Saab     struct ciss_lun_report	*cll;
1370c6131460SPaul Saab     int				error = 0, i, j;
1371c6131460SPaul Saab     int				ndrives;
1372c6131460SPaul Saab 
1373c6131460SPaul Saab     debug_called(1);
1374c6131460SPaul Saab 
1375c6131460SPaul Saab     cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
1376e3edca22SSean Bruno 			   sc->ciss_cfg->max_logical_supported);
1377c6131460SPaul Saab     if (cll == NULL) {
1378c6131460SPaul Saab 	error = ENXIO;
1379c6131460SPaul Saab 	goto out;
1380c6131460SPaul Saab     }
1381c6131460SPaul Saab 
13823a31b7ebSMike Smith     /* sanity-check reply */
13833a31b7ebSMike Smith     ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1384e3edca22SSean Bruno     if ((ndrives < 0) || (ndrives > sc->ciss_cfg->max_logical_supported)) {
13853a31b7ebSMike Smith 	ciss_printf(sc, "adapter claims to report absurd number of logical drives (%d > %d)\n",
1386e3edca22SSean Bruno 	    	ndrives, sc->ciss_cfg->max_logical_supported);
1387c6131460SPaul Saab 	error = ENXIO;
1388c6131460SPaul Saab 	goto out;
13893a31b7ebSMike Smith     }
13903a31b7ebSMike Smith 
13913a31b7ebSMike Smith     /*
13923a31b7ebSMike Smith      * Save logical drive information.
13933a31b7ebSMike Smith      */
1394c6131460SPaul Saab     if (bootverbose) {
1395c6131460SPaul Saab 	ciss_printf(sc, "%d logical drive%s\n",
1396c6131460SPaul Saab 	    ndrives, (ndrives > 1 || ndrives == 0) ? "s" : "");
1397c6131460SPaul Saab     }
1398c6131460SPaul Saab 
1399c6131460SPaul Saab     sc->ciss_logical =
14001fe6c4eeSScott Long 	malloc(sc->ciss_max_logical_bus * sizeof(struct ciss_ldrive *),
1401c6131460SPaul Saab 	       CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1402c6131460SPaul Saab     if (sc->ciss_logical == NULL) {
1403c6131460SPaul Saab 	error = ENXIO;
1404c6131460SPaul Saab 	goto out;
1405c6131460SPaul Saab     }
1406c6131460SPaul Saab 
14071fe6c4eeSScott Long     for (i = 0; i <= sc->ciss_max_logical_bus; i++) {
1408c6131460SPaul Saab 	sc->ciss_logical[i] =
1409e3edca22SSean Bruno 	    malloc(sc->ciss_cfg->max_logical_supported *
1410e3edca22SSean Bruno 		   sizeof(struct ciss_ldrive),
1411c6131460SPaul Saab 		   CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1412c6131460SPaul Saab 	if (sc->ciss_logical[i] == NULL) {
1413c6131460SPaul Saab 	    error = ENXIO;
1414c6131460SPaul Saab 	    goto out;
1415c6131460SPaul Saab 	}
1416c6131460SPaul Saab 
1417e3edca22SSean Bruno 	for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++)
1418c6131460SPaul Saab 	    sc->ciss_logical[i][j].cl_status = CISS_LD_NONEXISTENT;
1419c6131460SPaul Saab     }
1420c6131460SPaul Saab 
1421c6131460SPaul Saab 
1422e3edca22SSean Bruno     for (i = 0; i < sc->ciss_cfg->max_logical_supported; i++) {
14233a31b7ebSMike Smith 	if (i < ndrives) {
1424c6131460SPaul Saab 	    struct ciss_ldrive	*ld;
1425c6131460SPaul Saab 	    int			bus, target;
1426c6131460SPaul Saab 
1427c6131460SPaul Saab 	    bus		= CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
1428c6131460SPaul Saab 	    target	= CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
1429c6131460SPaul Saab 	    ld		= &sc->ciss_logical[bus][target];
1430c6131460SPaul Saab 
1431c6131460SPaul Saab 	    ld->cl_address	= cll->lun[i];
1432c6131460SPaul Saab 	    ld->cl_controller	= &sc->ciss_controllers[bus];
1433c6131460SPaul Saab 	    if (ciss_identify_logical(sc, ld) != 0)
14343a31b7ebSMike Smith 		continue;
14353a31b7ebSMike Smith 	    /*
14363a31b7ebSMike Smith 	     * If the drive has had media exchanged, we should bring it online.
14373a31b7ebSMike Smith 	     */
1438c6131460SPaul Saab 	    if (ld->cl_lstatus->media_exchanged)
143988c78a38SPaul Saab 		ciss_accept_media(sc, ld);
14403a31b7ebSMike Smith 
14413a31b7ebSMike Smith 	}
14423a31b7ebSMike Smith     }
14433a31b7ebSMike Smith 
14443a31b7ebSMike Smith  out:
14453a31b7ebSMike Smith     if (cll != NULL)
14463a31b7ebSMike Smith 	free(cll, CISS_MALLOC_CLASS);
14473a31b7ebSMike Smith     return(error);
14483a31b7ebSMike Smith }
14493a31b7ebSMike Smith 
1450440baa08SPaul Saab static int
1451c6131460SPaul Saab ciss_init_physical(struct ciss_softc *sc)
1452c6131460SPaul Saab {
1453c6131460SPaul Saab     struct ciss_lun_report	*cll;
1454c6131460SPaul Saab     int				error = 0, i;
1455c6131460SPaul Saab     int				nphys;
14561fe6c4eeSScott Long     int				bus, target;
1457c6131460SPaul Saab 
1458c6131460SPaul Saab     debug_called(1);
1459c6131460SPaul Saab 
14601fe6c4eeSScott Long     bus = 0;
14611fe6c4eeSScott Long     target = 0;
14621fe6c4eeSScott Long 
1463c6131460SPaul Saab     cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
1464e3edca22SSean Bruno 			   sc->ciss_cfg->max_physical_supported);
1465c6131460SPaul Saab     if (cll == NULL) {
1466c6131460SPaul Saab 	error = ENXIO;
1467c6131460SPaul Saab 	goto out;
1468c6131460SPaul Saab     }
1469c6131460SPaul Saab 
1470c6131460SPaul Saab     nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1471c6131460SPaul Saab 
1472c6131460SPaul Saab     if (bootverbose) {
1473c6131460SPaul Saab 	ciss_printf(sc, "%d physical device%s\n",
1474c6131460SPaul Saab 	    nphys, (nphys > 1 || nphys == 0) ? "s" : "");
1475c6131460SPaul Saab     }
1476c6131460SPaul Saab 
1477c6131460SPaul Saab     /*
14781fe6c4eeSScott Long      * Figure out the bus mapping.
14791fe6c4eeSScott Long      * Logical buses include both the local logical bus for local arrays and
14801fe6c4eeSScott Long      * proxy buses for remote arrays.  Physical buses are numbered by the
14811fe6c4eeSScott Long      * controller and represent physical buses that hold physical devices.
14821fe6c4eeSScott Long      * We shift these bus numbers so that everything fits into a single flat
14831fe6c4eeSScott Long      * numbering space for CAM.  Logical buses occupy the first 32 CAM bus
14841fe6c4eeSScott Long      * numbers, and the physical bus numbers are shifted to be above that.
14851fe6c4eeSScott Long      * This results in the various driver arrays being indexed as follows:
14861fe6c4eeSScott Long      *
14871fe6c4eeSScott Long      * ciss_controllers[] - indexed by logical bus
14881fe6c4eeSScott Long      * ciss_cam_sim[]     - indexed by both logical and physical, with physical
14891fe6c4eeSScott Long      *                      being shifted by 32.
14901fe6c4eeSScott Long      * ciss_logical[][]   - indexed by logical bus
14911fe6c4eeSScott Long      * ciss_physical[][]  - indexed by physical bus
14921fe6c4eeSScott Long      *
14931fe6c4eeSScott Long      * XXX This is getting more and more hackish.  CISS really doesn't play
14941fe6c4eeSScott Long      *     well with a standard SCSI model; devices are addressed via magic
14951fe6c4eeSScott Long      *     cookies, not via b/t/l addresses.  Since there is no way to store
14961fe6c4eeSScott Long      *     the cookie in the CAM device object, we have to keep these lookup
14971fe6c4eeSScott Long      *     tables handy so that the devices can be found quickly at the cost
14981fe6c4eeSScott Long      *     of wasting memory and having a convoluted lookup scheme.  This
14991fe6c4eeSScott Long      *     driver should probably be converted to block interface.
15001fe6c4eeSScott Long      */
15011fe6c4eeSScott Long     /*
1502c6131460SPaul Saab      * If the L2 and L3 SCSI addresses are 0, this signifies a proxy
1503c6131460SPaul Saab      * controller. A proxy controller is another physical controller
1504c6131460SPaul Saab      * behind the primary PCI controller. We need to know about this
1505c6131460SPaul Saab      * so that BMIC commands can be properly targeted.  There can be
1506c6131460SPaul Saab      * proxy controllers attached to a single PCI controller, so
1507c6131460SPaul Saab      * find the highest numbered one so the array can be properly
1508c6131460SPaul Saab      * sized.
1509c6131460SPaul Saab      */
15101fe6c4eeSScott Long     sc->ciss_max_logical_bus = 1;
1511c6131460SPaul Saab     for (i = 0; i < nphys; i++) {
1512c6131460SPaul Saab 	if (cll->lun[i].physical.extra_address == 0) {
15131fe6c4eeSScott Long 	    bus = cll->lun[i].physical.bus;
15141fe6c4eeSScott Long 	    sc->ciss_max_logical_bus = max(sc->ciss_max_logical_bus, bus) + 1;
15151fe6c4eeSScott Long 	} else {
15161fe6c4eeSScott Long 	    bus = CISS_EXTRA_BUS2(cll->lun[i].physical.extra_address);
15171fe6c4eeSScott Long 	    sc->ciss_max_physical_bus = max(sc->ciss_max_physical_bus, bus);
1518c6131460SPaul Saab 	}
1519c6131460SPaul Saab     }
1520c6131460SPaul Saab 
1521c6131460SPaul Saab     sc->ciss_controllers =
15221fe6c4eeSScott Long 	malloc(sc->ciss_max_logical_bus * sizeof (union ciss_device_address),
1523c6131460SPaul Saab 	       CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1524c6131460SPaul Saab 
1525c6131460SPaul Saab     if (sc->ciss_controllers == NULL) {
1526c6131460SPaul Saab 	ciss_printf(sc, "Could not allocate memory for controller map\n");
1527c6131460SPaul Saab 	error = ENOMEM;
1528c6131460SPaul Saab 	goto out;
1529c6131460SPaul Saab     }
1530c6131460SPaul Saab 
1531c6131460SPaul Saab     /* setup a map of controller addresses */
1532c6131460SPaul Saab     for (i = 0; i < nphys; i++) {
1533c6131460SPaul Saab 	if (cll->lun[i].physical.extra_address == 0) {
1534c6131460SPaul Saab 	    sc->ciss_controllers[cll->lun[i].physical.bus] = cll->lun[i];
1535c6131460SPaul Saab 	}
1536c6131460SPaul Saab     }
1537c6131460SPaul Saab 
15381fe6c4eeSScott Long     sc->ciss_physical =
15391fe6c4eeSScott Long 	malloc(sc->ciss_max_physical_bus * sizeof(struct ciss_pdrive *),
15401fe6c4eeSScott Long 	       CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
15411fe6c4eeSScott Long     if (sc->ciss_physical == NULL) {
15421fe6c4eeSScott Long 	ciss_printf(sc, "Could not allocate memory for physical device map\n");
15431fe6c4eeSScott Long 	error = ENOMEM;
15441fe6c4eeSScott Long 	goto out;
15451fe6c4eeSScott Long     }
15461fe6c4eeSScott Long 
15471fe6c4eeSScott Long     for (i = 0; i < sc->ciss_max_physical_bus; i++) {
15481fe6c4eeSScott Long 	sc->ciss_physical[i] =
15491fe6c4eeSScott Long 	    malloc(sizeof(struct ciss_pdrive) * CISS_MAX_PHYSTGT,
15501fe6c4eeSScott Long 		   CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
15511fe6c4eeSScott Long 	if (sc->ciss_physical[i] == NULL) {
15521fe6c4eeSScott Long 	    ciss_printf(sc, "Could not allocate memory for target map\n");
15531fe6c4eeSScott Long 	    error = ENOMEM;
15541fe6c4eeSScott Long 	    goto out;
15551fe6c4eeSScott Long 	}
15561fe6c4eeSScott Long     }
15571fe6c4eeSScott Long 
15581fe6c4eeSScott Long     ciss_filter_physical(sc, cll);
15591fe6c4eeSScott Long 
1560c6131460SPaul Saab out:
1561c6131460SPaul Saab     if (cll != NULL)
1562c6131460SPaul Saab 	free(cll, CISS_MALLOC_CLASS);
1563c6131460SPaul Saab 
1564c6131460SPaul Saab     return(error);
1565c6131460SPaul Saab }
1566c6131460SPaul Saab 
1567c6131460SPaul Saab static int
15681fe6c4eeSScott Long ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll)
15691fe6c4eeSScott Long {
15701fe6c4eeSScott Long     u_int32_t ea;
15711fe6c4eeSScott Long     int i, nphys;
15721fe6c4eeSScott Long     int	bus, target;
15731fe6c4eeSScott Long 
15741fe6c4eeSScott Long     nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
15751fe6c4eeSScott Long     for (i = 0; i < nphys; i++) {
15761fe6c4eeSScott Long 	if (cll->lun[i].physical.extra_address == 0)
15771fe6c4eeSScott Long 	    continue;
15781fe6c4eeSScott Long 
15791fe6c4eeSScott Long 	/*
15801fe6c4eeSScott Long 	 * Filter out devices that we don't want.  Level 3 LUNs could
15811fe6c4eeSScott Long 	 * probably be supported, but the docs don't give enough of a
15821fe6c4eeSScott Long 	 * hint to know how.
15831fe6c4eeSScott Long 	 *
15841fe6c4eeSScott Long 	 * The mode field of the physical address is likely set to have
15851fe6c4eeSScott Long 	 * hard disks masked out.  Honor it unless the user has overridden
15861fe6c4eeSScott Long 	 * us with the tunable.  We also munge the inquiry data for these
15871fe6c4eeSScott Long 	 * disks so that they only show up as passthrough devices.  Keeping
15881fe6c4eeSScott Long 	 * them visible in this fashion is useful for doing things like
15891fe6c4eeSScott Long 	 * flashing firmware.
15901fe6c4eeSScott Long 	 */
15911fe6c4eeSScott Long 	ea = cll->lun[i].physical.extra_address;
15921fe6c4eeSScott Long 	if ((CISS_EXTRA_BUS3(ea) != 0) || (CISS_EXTRA_TARGET3(ea) != 0) ||
15931fe6c4eeSScott Long 	    (CISS_EXTRA_MODE2(ea) == 0x3))
15941fe6c4eeSScott Long 	    continue;
15951fe6c4eeSScott Long 	if ((ciss_expose_hidden_physical == 0) &&
15961fe6c4eeSScott Long 	   (cll->lun[i].physical.mode == CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL))
15971fe6c4eeSScott Long 	    continue;
15981fe6c4eeSScott Long 
15991fe6c4eeSScott Long 	/*
16001fe6c4eeSScott Long 	 * Note: CISS firmware numbers physical busses starting at '1', not
16011fe6c4eeSScott Long 	 *       '0'.  This numbering is internal to the firmware and is only
16021fe6c4eeSScott Long 	 *       used as a hint here.
16031fe6c4eeSScott Long 	 */
16041fe6c4eeSScott Long 	bus = CISS_EXTRA_BUS2(ea) - 1;
16051fe6c4eeSScott Long 	target = CISS_EXTRA_TARGET2(ea);
16061fe6c4eeSScott Long 	sc->ciss_physical[bus][target].cp_address = cll->lun[i];
16071fe6c4eeSScott Long 	sc->ciss_physical[bus][target].cp_online = 1;
16081fe6c4eeSScott Long     }
16091fe6c4eeSScott Long 
16101fe6c4eeSScott Long     return (0);
16111fe6c4eeSScott Long }
16121fe6c4eeSScott Long 
16131fe6c4eeSScott Long static int
1614440baa08SPaul Saab ciss_inquiry_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1615440baa08SPaul Saab {
1616440baa08SPaul Saab     struct ciss_request			*cr;
1617440baa08SPaul Saab     struct ciss_command			*cc;
1618440baa08SPaul Saab     struct scsi_inquiry			*inq;
1619440baa08SPaul Saab     int					error;
1620440baa08SPaul Saab     int					command_status;
1621440baa08SPaul Saab 
1622440baa08SPaul Saab     cr = NULL;
1623440baa08SPaul Saab 
1624440baa08SPaul Saab     bzero(&ld->cl_geometry, sizeof(ld->cl_geometry));
1625440baa08SPaul Saab 
1626440baa08SPaul Saab     if ((error = ciss_get_request(sc, &cr)) != 0)
1627440baa08SPaul Saab 	goto out;
1628440baa08SPaul Saab 
16292fdaa90dSScott Long     cc = cr->cr_cc;
1630440baa08SPaul Saab     cr->cr_data = &ld->cl_geometry;
1631440baa08SPaul Saab     cr->cr_length = sizeof(ld->cl_geometry);
1632440baa08SPaul Saab     cr->cr_flags = CISS_REQ_DATAIN;
1633440baa08SPaul Saab 
1634c6131460SPaul Saab     cc->header.address = ld->cl_address;
1635440baa08SPaul Saab     cc->cdb.cdb_length = 6;
1636440baa08SPaul Saab     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1637440baa08SPaul Saab     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1638440baa08SPaul Saab     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1639440baa08SPaul Saab     cc->cdb.timeout = 30;
1640440baa08SPaul Saab 
1641440baa08SPaul Saab     inq = (struct scsi_inquiry *)&(cc->cdb.cdb[0]);
1642440baa08SPaul Saab     inq->opcode = INQUIRY;
1643440baa08SPaul Saab     inq->byte2 = SI_EVPD;
1644440baa08SPaul Saab     inq->page_code = CISS_VPD_LOGICAL_DRIVE_GEOMETRY;
1645130f4520SKenneth D. Merry     scsi_ulto2b(sizeof(ld->cl_geometry), inq->length);
1646440baa08SPaul Saab 
1647440baa08SPaul Saab     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1648440baa08SPaul Saab 	ciss_printf(sc, "error getting geometry (%d)\n", error);
1649440baa08SPaul Saab 	goto out;
1650440baa08SPaul Saab     }
1651440baa08SPaul Saab 
1652440baa08SPaul Saab     ciss_report_request(cr, &command_status, NULL);
1653440baa08SPaul Saab     switch(command_status) {
1654440baa08SPaul Saab     case CISS_CMD_STATUS_SUCCESS:
1655440baa08SPaul Saab     case CISS_CMD_STATUS_DATA_UNDERRUN:
1656440baa08SPaul Saab 	break;
1657440baa08SPaul Saab     case CISS_CMD_STATUS_DATA_OVERRUN:
1658440baa08SPaul Saab 	ciss_printf(sc, "WARNING: Data overrun\n");
1659440baa08SPaul Saab 	break;
1660440baa08SPaul Saab     default:
1661440baa08SPaul Saab 	ciss_printf(sc, "Error detecting logical drive geometry (%s)\n",
1662440baa08SPaul Saab 		    ciss_name_command_status(command_status));
1663440baa08SPaul Saab 	break;
1664440baa08SPaul Saab     }
1665440baa08SPaul Saab 
1666440baa08SPaul Saab out:
1667440baa08SPaul Saab     if (cr != NULL)
1668440baa08SPaul Saab 	ciss_release_request(cr);
1669440baa08SPaul Saab     return(error);
1670440baa08SPaul Saab }
16713a31b7ebSMike Smith /************************************************************************
16723a31b7ebSMike Smith  * Identify a logical drive, initialise state related to it.
16733a31b7ebSMike Smith  */
16743a31b7ebSMike Smith static int
16753a31b7ebSMike Smith ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
16763a31b7ebSMike Smith {
16773a31b7ebSMike Smith     struct ciss_request		*cr;
16783a31b7ebSMike Smith     struct ciss_command		*cc;
16793a31b7ebSMike Smith     struct ciss_bmic_cdb	*cbc;
16803a31b7ebSMike Smith     int				error, command_status;
16813a31b7ebSMike Smith 
16823a31b7ebSMike Smith     debug_called(1);
16833a31b7ebSMike Smith 
16843a31b7ebSMike Smith     cr = NULL;
16853a31b7ebSMike Smith 
16863a31b7ebSMike Smith     /*
16873a31b7ebSMike Smith      * Build a BMIC request to fetch the drive ID.
16883a31b7ebSMike Smith      */
16893a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LDRIVE,
16903a31b7ebSMike Smith 				       (void **)&ld->cl_ldrive,
16913a31b7ebSMike Smith 				       sizeof(*ld->cl_ldrive))) != 0)
16923a31b7ebSMike Smith 	goto out;
16932fdaa90dSScott Long     cc = cr->cr_cc;
1694c6131460SPaul Saab     cc->header.address = *ld->cl_controller;	/* target controller */
16953a31b7ebSMike Smith     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1696c6131460SPaul Saab     cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
16973a31b7ebSMike Smith 
16983a31b7ebSMike Smith     /*
16993a31b7ebSMike Smith      * Submit the request and wait for it to complete.
17003a31b7ebSMike Smith      */
17013a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
17023a31b7ebSMike Smith 	ciss_printf(sc, "error sending BMIC LDRIVE command (%d)\n", error);
17033a31b7ebSMike Smith 	goto out;
17043a31b7ebSMike Smith     }
17053a31b7ebSMike Smith 
17063a31b7ebSMike Smith     /*
17073a31b7ebSMike Smith      * Check response.
17083a31b7ebSMike Smith      */
17093a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
17103a31b7ebSMike Smith     switch(command_status) {
17113a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
17123a31b7ebSMike Smith 	break;
17133a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_UNDERRUN:
17143a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_OVERRUN:
17153a31b7ebSMike Smith 	ciss_printf(sc, "data over/underrun reading logical drive ID\n");
17163a31b7ebSMike Smith     default:
17173a31b7ebSMike Smith 	ciss_printf(sc, "error reading logical drive ID (%s)\n",
17183a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
17193a31b7ebSMike Smith 	error = EIO;
17203a31b7ebSMike Smith 	goto out;
17213a31b7ebSMike Smith     }
17223a31b7ebSMike Smith     ciss_release_request(cr);
17233a31b7ebSMike Smith     cr = NULL;
17243a31b7ebSMike Smith 
17253a31b7ebSMike Smith     /*
17263a31b7ebSMike Smith      * Build a CISS BMIC command to get the logical drive status.
17273a31b7ebSMike Smith      */
17283a31b7ebSMike Smith     if ((error = ciss_get_ldrive_status(sc, ld)) != 0)
17293a31b7ebSMike Smith 	goto out;
17303a31b7ebSMike Smith 
17313a31b7ebSMike Smith     /*
1732440baa08SPaul Saab      * Get the logical drive geometry.
1733440baa08SPaul Saab      */
1734440baa08SPaul Saab     if ((error = ciss_inquiry_logical(sc, ld)) != 0)
1735440baa08SPaul Saab 	goto out;
1736440baa08SPaul Saab 
1737440baa08SPaul Saab     /*
17383a31b7ebSMike Smith      * Print the drive's basic characteristics.
17393a31b7ebSMike Smith      */
17404bca277bSPaul Saab     if (bootverbose) {
1741c6131460SPaul Saab 	ciss_printf(sc, "logical drive (b%dt%d): %s, %dMB ",
1742c6131460SPaul Saab 		    CISS_LUN_TO_BUS(ld->cl_address.logical.lun),
1743c6131460SPaul Saab 		    CISS_LUN_TO_TARGET(ld->cl_address.logical.lun),
174479adbf76SPaul Saab 		    ciss_name_ldrive_org(ld->cl_ldrive->fault_tolerance),
17453a31b7ebSMike Smith 		    ((ld->cl_ldrive->blocks_available / (1024 * 1024)) *
17463a31b7ebSMike Smith 		     ld->cl_ldrive->block_size));
17473a31b7ebSMike Smith 
17483a31b7ebSMike Smith 	ciss_print_ldrive(sc, ld);
17493a31b7ebSMike Smith     }
17503a31b7ebSMike Smith out:
17513a31b7ebSMike Smith     if (error != 0) {
17523a31b7ebSMike Smith 	/* make the drive not-exist */
17533a31b7ebSMike Smith 	ld->cl_status = CISS_LD_NONEXISTENT;
17543a31b7ebSMike Smith 	if (ld->cl_ldrive != NULL) {
17553a31b7ebSMike Smith 	    free(ld->cl_ldrive, CISS_MALLOC_CLASS);
17563a31b7ebSMike Smith 	    ld->cl_ldrive = NULL;
17573a31b7ebSMike Smith 	}
17583a31b7ebSMike Smith 	if (ld->cl_lstatus != NULL) {
17593a31b7ebSMike Smith 	    free(ld->cl_lstatus, CISS_MALLOC_CLASS);
17603a31b7ebSMike Smith 	    ld->cl_lstatus = NULL;
17613a31b7ebSMike Smith 	}
17623a31b7ebSMike Smith     }
17633a31b7ebSMike Smith     if (cr != NULL)
17643a31b7ebSMike Smith 	ciss_release_request(cr);
17653a31b7ebSMike Smith 
17663a31b7ebSMike Smith     return(error);
17673a31b7ebSMike Smith }
17683a31b7ebSMike Smith 
17693a31b7ebSMike Smith /************************************************************************
17703a31b7ebSMike Smith  * Get status for a logical drive.
17713a31b7ebSMike Smith  *
17723a31b7ebSMike Smith  * XXX should we also do this in response to Test Unit Ready?
17733a31b7ebSMike Smith  */
17743a31b7ebSMike Smith static int
17753a31b7ebSMike Smith ciss_get_ldrive_status(struct ciss_softc *sc,  struct ciss_ldrive *ld)
17763a31b7ebSMike Smith {
17773a31b7ebSMike Smith     struct ciss_request		*cr;
17783a31b7ebSMike Smith     struct ciss_command		*cc;
17793a31b7ebSMike Smith     struct ciss_bmic_cdb	*cbc;
17803a31b7ebSMike Smith     int				error, command_status;
17813a31b7ebSMike Smith 
17823a31b7ebSMike Smith     /*
17833a31b7ebSMike Smith      * Build a CISS BMIC command to get the logical drive status.
17843a31b7ebSMike Smith      */
17853a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LSTATUS,
17863a31b7ebSMike Smith 				       (void **)&ld->cl_lstatus,
17873a31b7ebSMike Smith 				       sizeof(*ld->cl_lstatus))) != 0)
17883a31b7ebSMike Smith 	goto out;
17892fdaa90dSScott Long     cc = cr->cr_cc;
1790c6131460SPaul Saab     cc->header.address = *ld->cl_controller;	/* target controller */
17913a31b7ebSMike Smith     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1792c6131460SPaul Saab     cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
17933a31b7ebSMike Smith 
17943a31b7ebSMike Smith     /*
17953a31b7ebSMike Smith      * Submit the request and wait for it to complete.
17963a31b7ebSMike Smith      */
17973a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
17983a31b7ebSMike Smith 	ciss_printf(sc, "error sending BMIC LSTATUS command (%d)\n", error);
17993a31b7ebSMike Smith 	goto out;
18003a31b7ebSMike Smith     }
18013a31b7ebSMike Smith 
18023a31b7ebSMike Smith     /*
18033a31b7ebSMike Smith      * Check response.
18043a31b7ebSMike Smith      */
18053a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
18063a31b7ebSMike Smith     switch(command_status) {
18073a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
18083a31b7ebSMike Smith 	break;
18093a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_UNDERRUN:
18103a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_OVERRUN:
18113a31b7ebSMike Smith 	ciss_printf(sc, "data over/underrun reading logical drive status\n");
18123a31b7ebSMike Smith     default:
18133a31b7ebSMike Smith 	ciss_printf(sc, "error reading logical drive status (%s)\n",
18143a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
18153a31b7ebSMike Smith 	error = EIO;
18163a31b7ebSMike Smith 	goto out;
18173a31b7ebSMike Smith     }
18183a31b7ebSMike Smith 
18193a31b7ebSMike Smith     /*
18203a31b7ebSMike Smith      * Set the drive's summary status based on the returned status.
18213a31b7ebSMike Smith      *
18223a31b7ebSMike Smith      * XXX testing shows that a failed JBOD drive comes back at next
18233a31b7ebSMike Smith      * boot in "queued for expansion" mode.  WTF?
18243a31b7ebSMike Smith      */
18253a31b7ebSMike Smith     ld->cl_status = ciss_decode_ldrive_status(ld->cl_lstatus->status);
18263a31b7ebSMike Smith 
18273a31b7ebSMike Smith out:
18283a31b7ebSMike Smith     if (cr != NULL)
18293a31b7ebSMike Smith 	ciss_release_request(cr);
18303a31b7ebSMike Smith     return(error);
18313a31b7ebSMike Smith }
18323a31b7ebSMike Smith 
18333a31b7ebSMike Smith /************************************************************************
18343a31b7ebSMike Smith  * Notify the adapter of a config update.
18353a31b7ebSMike Smith  */
18363a31b7ebSMike Smith static int
18373a31b7ebSMike Smith ciss_update_config(struct ciss_softc *sc)
18383a31b7ebSMike Smith {
18393a31b7ebSMike Smith     int		i;
18403a31b7ebSMike Smith 
18413a31b7ebSMike Smith     debug_called(1);
18423a31b7ebSMike Smith 
18433a31b7ebSMike Smith     CISS_TL_SIMPLE_WRITE(sc, CISS_TL_SIMPLE_IDBR, CISS_TL_SIMPLE_IDBR_CFG_TABLE);
18443a31b7ebSMike Smith     for (i = 0; i < 1000; i++) {
18453a31b7ebSMike Smith 	if (!(CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR) &
18463a31b7ebSMike Smith 	      CISS_TL_SIMPLE_IDBR_CFG_TABLE)) {
18473a31b7ebSMike Smith 	    return(0);
18483a31b7ebSMike Smith 	}
18493a31b7ebSMike Smith 	DELAY(1000);
18503a31b7ebSMike Smith     }
18513a31b7ebSMike Smith     return(1);
18523a31b7ebSMike Smith }
18533a31b7ebSMike Smith 
18543a31b7ebSMike Smith /************************************************************************
18553a31b7ebSMike Smith  * Accept new media into a logical drive.
18563a31b7ebSMike Smith  *
18573a31b7ebSMike Smith  * XXX The drive has previously been offline; it would be good if we
18583a31b7ebSMike Smith  *     could make sure it's not open right now.
18593a31b7ebSMike Smith  */
18603a31b7ebSMike Smith static int
186188c78a38SPaul Saab ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld)
18623a31b7ebSMike Smith {
18633a31b7ebSMike Smith     struct ciss_request		*cr;
18643a31b7ebSMike Smith     struct ciss_command		*cc;
18653a31b7ebSMike Smith     struct ciss_bmic_cdb	*cbc;
186688c78a38SPaul Saab     int				command_status;
186788c78a38SPaul Saab     int				error = 0, ldrive;
1868609caf8dSPaul Saab 
1869609caf8dSPaul Saab     ldrive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
18703a31b7ebSMike Smith 
18713ea59fd3SSean Bruno     debug(0, "bringing logical drive %d back online", ldrive);
18723a31b7ebSMike Smith 
18733a31b7ebSMike Smith     /*
18743a31b7ebSMike Smith      * Build a CISS BMIC command to bring the drive back online.
18753a31b7ebSMike Smith      */
18763a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ACCEPT_MEDIA,
18773a31b7ebSMike Smith 				       NULL, 0)) != 0)
18783a31b7ebSMike Smith 	goto out;
18792fdaa90dSScott Long     cc = cr->cr_cc;
1880c6131460SPaul Saab     cc->header.address = *ld->cl_controller;	/* target controller */
18813a31b7ebSMike Smith     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1882609caf8dSPaul Saab     cbc->log_drive = ldrive;
18833a31b7ebSMike Smith 
18843a31b7ebSMike Smith     /*
18853a31b7ebSMike Smith      * Submit the request and wait for it to complete.
18863a31b7ebSMike Smith      */
18873a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
188888c78a38SPaul Saab 	ciss_printf(sc, "error sending BMIC ACCEPT MEDIA command (%d)\n", error);
18893a31b7ebSMike Smith 	goto out;
18903a31b7ebSMike Smith     }
18913a31b7ebSMike Smith 
18923a31b7ebSMike Smith     /*
18933a31b7ebSMike Smith      * Check response.
18943a31b7ebSMike Smith      */
18953a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
18963a31b7ebSMike Smith     switch(command_status) {
18973a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:		/* all OK */
18983a31b7ebSMike Smith 	/* we should get a logical drive status changed event here */
18993a31b7ebSMike Smith 	break;
19003a31b7ebSMike Smith     default:
19013a31b7ebSMike Smith 	ciss_printf(cr->cr_sc, "error accepting media into failed logical drive (%s)\n",
19023a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
19033a31b7ebSMike Smith 	break;
19043a31b7ebSMike Smith     }
190588c78a38SPaul Saab 
190688c78a38SPaul Saab out:
190788c78a38SPaul Saab     if (cr != NULL)
19083a31b7ebSMike Smith 	ciss_release_request(cr);
190988c78a38SPaul Saab     return(error);
19103a31b7ebSMike Smith }
19113a31b7ebSMike Smith 
19123a31b7ebSMike Smith /************************************************************************
19133a31b7ebSMike Smith  * Release adapter resources.
19143a31b7ebSMike Smith  */
19153a31b7ebSMike Smith static void
19163a31b7ebSMike Smith ciss_free(struct ciss_softc *sc)
19173a31b7ebSMike Smith {
19183284b9eeSPaul Saab     struct ciss_request *cr;
1919ee626b40SPaul Saab     int			i, j;
19203284b9eeSPaul Saab 
19213a31b7ebSMike Smith     debug_called(1);
19223a31b7ebSMike Smith 
19233a31b7ebSMike Smith     /* we're going away */
19243a31b7ebSMike Smith     sc->ciss_flags |= CISS_FLAG_ABORTING;
19253a31b7ebSMike Smith 
19263a31b7ebSMike Smith     /* terminate the periodic heartbeat routine */
19272472e51eSScott Long     callout_stop(&sc->ciss_periodic);
19283a31b7ebSMike Smith 
19293a31b7ebSMike Smith     /* cancel the Event Notify chain */
19303a31b7ebSMike Smith     ciss_notify_abort(sc);
19313a31b7ebSMike Smith 
1932c6131460SPaul Saab     ciss_kill_notify_thread(sc);
1933c6131460SPaul Saab 
19342472e51eSScott Long     /* disconnect from CAM */
19352472e51eSScott Long     if (sc->ciss_cam_sim) {
19362472e51eSScott Long 	for (i = 0; i < sc->ciss_max_logical_bus; i++) {
19372472e51eSScott Long 	    if (sc->ciss_cam_sim[i]) {
19382472e51eSScott Long 		xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
19392472e51eSScott Long 		cam_sim_free(sc->ciss_cam_sim[i], 0);
19402472e51eSScott Long 	    }
19412472e51eSScott Long 	}
19422472e51eSScott Long 	for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
19432472e51eSScott Long 	     CISS_PHYSICAL_BASE; i++) {
19442472e51eSScott Long 	    if (sc->ciss_cam_sim[i]) {
19452472e51eSScott Long 		xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
19462472e51eSScott Long 		cam_sim_free(sc->ciss_cam_sim[i], 0);
19472472e51eSScott Long 	    }
19482472e51eSScott Long 	}
19492472e51eSScott Long 	free(sc->ciss_cam_sim, CISS_MALLOC_CLASS);
19502472e51eSScott Long     }
19512472e51eSScott Long     if (sc->ciss_cam_devq)
19522472e51eSScott Long 	cam_simq_free(sc->ciss_cam_devq);
19532472e51eSScott Long 
195478d03361SPaul Saab     /* remove the control device */
19552472e51eSScott Long     mtx_unlock(&sc->ciss_mtx);
195678d03361SPaul Saab     if (sc->ciss_dev_t != NULL)
195778d03361SPaul Saab 	destroy_dev(sc->ciss_dev_t);
195878d03361SPaul Saab 
19592472e51eSScott Long     /* Final cleanup of the callout. */
19602472e51eSScott Long     callout_drain(&sc->ciss_periodic);
19612472e51eSScott Long     mtx_destroy(&sc->ciss_mtx);
19622472e51eSScott Long 
19633a31b7ebSMike Smith     /* free the controller data */
19643a31b7ebSMike Smith     if (sc->ciss_id != NULL)
19653a31b7ebSMike Smith 	free(sc->ciss_id, CISS_MALLOC_CLASS);
19663a31b7ebSMike Smith 
19673a31b7ebSMike Smith     /* release I/O resources */
19683a31b7ebSMike Smith     if (sc->ciss_regs_resource != NULL)
19693a31b7ebSMike Smith 	bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
19703a31b7ebSMike Smith 			     sc->ciss_regs_rid, sc->ciss_regs_resource);
19713a31b7ebSMike Smith     if (sc->ciss_cfg_resource != NULL)
19723a31b7ebSMike Smith 	bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
19733a31b7ebSMike Smith 			     sc->ciss_cfg_rid, sc->ciss_cfg_resource);
19743a31b7ebSMike Smith     if (sc->ciss_intr != NULL)
19753a31b7ebSMike Smith 	bus_teardown_intr(sc->ciss_dev, sc->ciss_irq_resource, sc->ciss_intr);
19763a31b7ebSMike Smith     if (sc->ciss_irq_resource != NULL)
19773a31b7ebSMike Smith 	bus_release_resource(sc->ciss_dev, SYS_RES_IRQ,
197822657ce1SScott Long 			     sc->ciss_irq_rid[0], sc->ciss_irq_resource);
197922657ce1SScott Long     if (sc->ciss_msi)
198022657ce1SScott Long 	pci_release_msi(sc->ciss_dev);
19813284b9eeSPaul Saab 
19823284b9eeSPaul Saab     while ((cr = ciss_dequeue_free(sc)) != NULL)
19833284b9eeSPaul Saab 	bus_dmamap_destroy(sc->ciss_buffer_dmat, cr->cr_datamap);
19843a31b7ebSMike Smith     if (sc->ciss_buffer_dmat)
19853a31b7ebSMike Smith 	bus_dma_tag_destroy(sc->ciss_buffer_dmat);
19863a31b7ebSMike Smith 
19873a31b7ebSMike Smith     /* destroy command memory and DMA tag */
19883a31b7ebSMike Smith     if (sc->ciss_command != NULL) {
19893a31b7ebSMike Smith 	bus_dmamap_unload(sc->ciss_command_dmat, sc->ciss_command_map);
19903a31b7ebSMike Smith 	bus_dmamem_free(sc->ciss_command_dmat, sc->ciss_command, sc->ciss_command_map);
19913a31b7ebSMike Smith     }
19923284b9eeSPaul Saab     if (sc->ciss_command_dmat)
19933a31b7ebSMike Smith 	bus_dma_tag_destroy(sc->ciss_command_dmat);
19943a31b7ebSMike Smith 
199522657ce1SScott Long     if (sc->ciss_reply) {
199622657ce1SScott Long 	bus_dmamap_unload(sc->ciss_reply_dmat, sc->ciss_reply_map);
199722657ce1SScott Long 	bus_dmamem_free(sc->ciss_reply_dmat, sc->ciss_reply, sc->ciss_reply_map);
199822657ce1SScott Long     }
199922657ce1SScott Long     if (sc->ciss_reply_dmat)
200022657ce1SScott Long 	bus_dma_tag_destroy(sc->ciss_reply_dmat);
200122657ce1SScott Long 
200222657ce1SScott Long     /* destroy DMA tags */
200322657ce1SScott Long     if (sc->ciss_parent_dmat)
200422657ce1SScott Long 	bus_dma_tag_destroy(sc->ciss_parent_dmat);
2005c6131460SPaul Saab     if (sc->ciss_logical) {
2006ee626b40SPaul Saab 	for (i = 0; i <= sc->ciss_max_logical_bus; i++) {
2007e3edca22SSean Bruno 	    for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
2008ee626b40SPaul Saab 		if (sc->ciss_logical[i][j].cl_ldrive)
2009ee626b40SPaul Saab 		    free(sc->ciss_logical[i][j].cl_ldrive, CISS_MALLOC_CLASS);
2010ee626b40SPaul Saab 		if (sc->ciss_logical[i][j].cl_lstatus)
2011ee626b40SPaul Saab 		    free(sc->ciss_logical[i][j].cl_lstatus, CISS_MALLOC_CLASS);
2012ee626b40SPaul Saab 	    }
2013c6131460SPaul Saab 	    free(sc->ciss_logical[i], CISS_MALLOC_CLASS);
2014ee626b40SPaul Saab 	}
2015c6131460SPaul Saab 	free(sc->ciss_logical, CISS_MALLOC_CLASS);
2016c6131460SPaul Saab     }
2017c6131460SPaul Saab 
20181fe6c4eeSScott Long     if (sc->ciss_physical) {
20191fe6c4eeSScott Long 	for (i = 0; i < sc->ciss_max_physical_bus; i++)
20201fe6c4eeSScott Long 	    free(sc->ciss_physical[i], CISS_MALLOC_CLASS);
20211fe6c4eeSScott Long 	free(sc->ciss_physical, CISS_MALLOC_CLASS);
20221fe6c4eeSScott Long     }
20231fe6c4eeSScott Long 
2024c6131460SPaul Saab     if (sc->ciss_controllers)
2025c6131460SPaul Saab 	free(sc->ciss_controllers, CISS_MALLOC_CLASS);
2026975b7318SScott Long 
20273a31b7ebSMike Smith }
20283a31b7ebSMike Smith 
20293a31b7ebSMike Smith /************************************************************************
20303a31b7ebSMike Smith  * Give a command to the adapter.
20313a31b7ebSMike Smith  *
20323a31b7ebSMike Smith  * Note that this uses the simple transport layer directly.  If we
20333a31b7ebSMike Smith  * want to add support for other layers, we'll need a switch of some
20343a31b7ebSMike Smith  * sort.
20353a31b7ebSMike Smith  *
20363a31b7ebSMike Smith  * Note that the simple transport layer has no way of refusing a
20373a31b7ebSMike Smith  * command; we only have as many request structures as the adapter
20383a31b7ebSMike Smith  * supports commands, so we don't have to check (this presumes that
20393a31b7ebSMike Smith  * the adapter can handle commands as fast as we throw them at it).
20403a31b7ebSMike Smith  */
20413a31b7ebSMike Smith static int
20423a31b7ebSMike Smith ciss_start(struct ciss_request *cr)
20433a31b7ebSMike Smith {
20443a31b7ebSMike Smith     struct ciss_command	*cc;	/* XXX debugging only */
20453a31b7ebSMike Smith     int			error;
20463a31b7ebSMike Smith 
20472fdaa90dSScott Long     cc = cr->cr_cc;
20483a31b7ebSMike Smith     debug(2, "post command %d tag %d ", cr->cr_tag, cc->header.host_tag);
20493a31b7ebSMike Smith 
20503a31b7ebSMike Smith     /*
20513a31b7ebSMike Smith      * Map the request's data.
20523a31b7ebSMike Smith      */
20533a31b7ebSMike Smith     if ((error = ciss_map_request(cr)))
20543a31b7ebSMike Smith 	return(error);
20553a31b7ebSMike Smith 
20563a31b7ebSMike Smith #if 0
20573a31b7ebSMike Smith     ciss_print_request(cr);
20583a31b7ebSMike Smith #endif
20593a31b7ebSMike Smith 
20603a31b7ebSMike Smith     return(0);
20613a31b7ebSMike Smith }
20623a31b7ebSMike Smith 
20633a31b7ebSMike Smith /************************************************************************
20643a31b7ebSMike Smith  * Fetch completed request(s) from the adapter, queue them for
20653a31b7ebSMike Smith  * completion handling.
20663a31b7ebSMike Smith  *
20673a31b7ebSMike Smith  * Note that this uses the simple transport layer directly.  If we
20683a31b7ebSMike Smith  * want to add support for other layers, we'll need a switch of some
20693a31b7ebSMike Smith  * sort.
20703a31b7ebSMike Smith  *
20713a31b7ebSMike Smith  * Note that the simple transport mechanism does not require any
20723a31b7ebSMike Smith  * reentrancy protection; the OPQ read is atomic.  If there is a
20733a31b7ebSMike Smith  * chance of a race with something else that might move the request
20743a31b7ebSMike Smith  * off the busy list, then we will have to lock against that
20753a31b7ebSMike Smith  * (eg. timeouts, etc.)
20763a31b7ebSMike Smith  */
20773a31b7ebSMike Smith static void
207822657ce1SScott Long ciss_done(struct ciss_softc *sc, cr_qhead_t *qh)
20793a31b7ebSMike Smith {
20803a31b7ebSMike Smith     struct ciss_request	*cr;
20813a31b7ebSMike Smith     struct ciss_command	*cc;
20823a31b7ebSMike Smith     u_int32_t		tag, index;
20833a31b7ebSMike Smith 
20843a31b7ebSMike Smith     debug_called(3);
20853a31b7ebSMike Smith 
20863a31b7ebSMike Smith     /*
20873a31b7ebSMike Smith      * Loop quickly taking requests from the adapter and moving them
208822657ce1SScott Long      * to the completed queue.
20893a31b7ebSMike Smith      */
20903a31b7ebSMike Smith     for (;;) {
20913a31b7ebSMike Smith 
20923a31b7ebSMike Smith 	tag = CISS_TL_SIMPLE_FETCH_CMD(sc);
20933a31b7ebSMike Smith 	if (tag == CISS_TL_SIMPLE_OPQ_EMPTY)
20943a31b7ebSMike Smith 	    break;
20953a31b7ebSMike Smith 	index = tag >> 2;
20963a31b7ebSMike Smith 	debug(2, "completed command %d%s", index,
20973a31b7ebSMike Smith 	      (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
20983a31b7ebSMike Smith 	if (index >= sc->ciss_max_requests) {
20993a31b7ebSMike Smith 	    ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
21003a31b7ebSMike Smith 	    continue;
21013a31b7ebSMike Smith 	}
21023a31b7ebSMike Smith 	cr = &(sc->ciss_request[index]);
21032fdaa90dSScott Long 	cc = cr->cr_cc;
21043a31b7ebSMike Smith 	cc->header.host_tag = tag;	/* not updated by adapter */
210522657ce1SScott Long 	ciss_enqueue_complete(cr, qh);
21063a31b7ebSMike Smith     }
21073a31b7ebSMike Smith 
210822657ce1SScott Long }
210922657ce1SScott Long 
211022657ce1SScott Long static void
211122657ce1SScott Long ciss_perf_done(struct ciss_softc *sc, cr_qhead_t *qh)
211222657ce1SScott Long {
211322657ce1SScott Long     struct ciss_request	*cr;
211422657ce1SScott Long     struct ciss_command	*cc;
211522657ce1SScott Long     u_int32_t		tag, index;
211622657ce1SScott Long 
211722657ce1SScott Long     debug_called(3);
211822657ce1SScott Long 
21193a31b7ebSMike Smith     /*
212022657ce1SScott Long      * Loop quickly taking requests from the adapter and moving them
212122657ce1SScott Long      * to the completed queue.
21223a31b7ebSMike Smith      */
212322657ce1SScott Long     for (;;) {
212422657ce1SScott Long 	tag = sc->ciss_reply[sc->ciss_rqidx];
212522657ce1SScott Long 	if ((tag & CISS_CYCLE_MASK) != sc->ciss_cycle)
212622657ce1SScott Long 	    break;
212722657ce1SScott Long 	index = tag >> 2;
212822657ce1SScott Long 	debug(2, "completed command %d%s\n", index,
212922657ce1SScott Long 	      (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
213022657ce1SScott Long 	if (index < sc->ciss_max_requests) {
213122657ce1SScott Long 	    cr = &(sc->ciss_request[index]);
21322fdaa90dSScott Long 	    cc = cr->cr_cc;
213322657ce1SScott Long 	    cc->header.host_tag = tag;	/* not updated by adapter */
213422657ce1SScott Long 	    ciss_enqueue_complete(cr, qh);
213522657ce1SScott Long 	} else {
213622657ce1SScott Long 	    ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
213722657ce1SScott Long 	}
213822657ce1SScott Long 	if (++sc->ciss_rqidx == sc->ciss_max_requests) {
213922657ce1SScott Long 	    sc->ciss_rqidx = 0;
214022657ce1SScott Long 	    sc->ciss_cycle ^= 1;
214122657ce1SScott Long 	}
214222657ce1SScott Long     }
214322657ce1SScott Long 
21443a31b7ebSMike Smith }
21453a31b7ebSMike Smith 
21463a31b7ebSMike Smith /************************************************************************
21473a31b7ebSMike Smith  * Take an interrupt from the adapter.
21483a31b7ebSMike Smith  */
21493a31b7ebSMike Smith static void
21503a31b7ebSMike Smith ciss_intr(void *arg)
21513a31b7ebSMike Smith {
215222657ce1SScott Long     cr_qhead_t qh;
21533a31b7ebSMike Smith     struct ciss_softc	*sc = (struct ciss_softc *)arg;
21543a31b7ebSMike Smith 
21553a31b7ebSMike Smith     /*
21563a31b7ebSMike Smith      * The only interrupt we recognise indicates that there are
21573a31b7ebSMike Smith      * entries in the outbound post queue.
21583a31b7ebSMike Smith      */
215922657ce1SScott Long     STAILQ_INIT(&qh);
216022657ce1SScott Long     ciss_done(sc, &qh);
2161975b7318SScott Long     mtx_lock(&sc->ciss_mtx);
216222657ce1SScott Long     ciss_complete(sc, &qh);
2163975b7318SScott Long     mtx_unlock(&sc->ciss_mtx);
21643a31b7ebSMike Smith }
21653a31b7ebSMike Smith 
216622657ce1SScott Long static void
216722657ce1SScott Long ciss_perf_intr(void *arg)
216822657ce1SScott Long {
216922657ce1SScott Long     struct ciss_softc	*sc = (struct ciss_softc *)arg;
217022657ce1SScott Long 
217122657ce1SScott Long     /* Clear the interrupt and flush the bridges.  Docs say that the flush
217222657ce1SScott Long      * needs to be done twice, which doesn't seem right.
217322657ce1SScott Long      */
217422657ce1SScott Long     CISS_TL_PERF_CLEAR_INT(sc);
217522657ce1SScott Long     CISS_TL_PERF_FLUSH_INT(sc);
217622657ce1SScott Long 
217722657ce1SScott Long     ciss_perf_msi_intr(sc);
217822657ce1SScott Long }
217922657ce1SScott Long 
218022657ce1SScott Long static void
218122657ce1SScott Long ciss_perf_msi_intr(void *arg)
218222657ce1SScott Long {
218322657ce1SScott Long     cr_qhead_t qh;
218422657ce1SScott Long     struct ciss_softc	*sc = (struct ciss_softc *)arg;
218522657ce1SScott Long 
218622657ce1SScott Long     STAILQ_INIT(&qh);
218722657ce1SScott Long     ciss_perf_done(sc, &qh);
218822657ce1SScott Long     mtx_lock(&sc->ciss_mtx);
218922657ce1SScott Long     ciss_complete(sc, &qh);
219022657ce1SScott Long     mtx_unlock(&sc->ciss_mtx);
219122657ce1SScott Long }
219222657ce1SScott Long 
219322657ce1SScott Long 
21943a31b7ebSMike Smith /************************************************************************
21953a31b7ebSMike Smith  * Process completed requests.
21963a31b7ebSMike Smith  *
21973a31b7ebSMike Smith  * Requests can be completed in three fashions:
21983a31b7ebSMike Smith  *
21993a31b7ebSMike Smith  * - by invoking a callback function (cr_complete is non-null)
22003a31b7ebSMike Smith  * - by waking up a sleeper (cr_flags has CISS_REQ_SLEEP set)
22013a31b7ebSMike Smith  * - by clearing the CISS_REQ_POLL flag in interrupt/timeout context
22023a31b7ebSMike Smith  */
22033a31b7ebSMike Smith static void
220422657ce1SScott Long ciss_complete(struct ciss_softc *sc, cr_qhead_t *qh)
22053a31b7ebSMike Smith {
22063a31b7ebSMike Smith     struct ciss_request	*cr;
22073a31b7ebSMike Smith 
22083a31b7ebSMike Smith     debug_called(2);
22093a31b7ebSMike Smith 
22103a31b7ebSMike Smith     /*
22113a31b7ebSMike Smith      * Loop taking requests off the completed queue and performing
22123a31b7ebSMike Smith      * completion processing on them.
22133a31b7ebSMike Smith      */
22143a31b7ebSMike Smith     for (;;) {
221522657ce1SScott Long 	if ((cr = ciss_dequeue_complete(sc, qh)) == NULL)
22163a31b7ebSMike Smith 	    break;
22173a31b7ebSMike Smith 	ciss_unmap_request(cr);
22183a31b7ebSMike Smith 
221922657ce1SScott Long 	if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
222022657ce1SScott Long 	    ciss_printf(sc, "WARNING: completing non-busy request\n");
222122657ce1SScott Long 	cr->cr_flags &= ~CISS_REQ_BUSY;
222222657ce1SScott Long 
22233a31b7ebSMike Smith 	/*
22243a31b7ebSMike Smith 	 * If the request has a callback, invoke it.
22253a31b7ebSMike Smith 	 */
22263a31b7ebSMike Smith 	if (cr->cr_complete != NULL) {
22273a31b7ebSMike Smith 	    cr->cr_complete(cr);
22283a31b7ebSMike Smith 	    continue;
22293a31b7ebSMike Smith 	}
22303a31b7ebSMike Smith 
22313a31b7ebSMike Smith 	/*
22323a31b7ebSMike Smith 	 * If someone is sleeping on this request, wake them up.
22333a31b7ebSMike Smith 	 */
22343a31b7ebSMike Smith 	if (cr->cr_flags & CISS_REQ_SLEEP) {
22353a31b7ebSMike Smith 	    cr->cr_flags &= ~CISS_REQ_SLEEP;
22363a31b7ebSMike Smith 	    wakeup(cr);
22373a31b7ebSMike Smith 	    continue;
22383a31b7ebSMike Smith 	}
22393a31b7ebSMike Smith 
22403a31b7ebSMike Smith 	/*
22413a31b7ebSMike Smith 	 * If someone is polling this request for completion, signal.
22423a31b7ebSMike Smith 	 */
22433a31b7ebSMike Smith 	if (cr->cr_flags & CISS_REQ_POLL) {
22443a31b7ebSMike Smith 	    cr->cr_flags &= ~CISS_REQ_POLL;
22453a31b7ebSMike Smith 	    continue;
22463a31b7ebSMike Smith 	}
22473a31b7ebSMike Smith 
22483a31b7ebSMike Smith 	/*
22493a31b7ebSMike Smith 	 * Give up and throw the request back on the free queue.  This
22503a31b7ebSMike Smith 	 * should never happen; resources will probably be lost.
22513a31b7ebSMike Smith 	 */
22523a31b7ebSMike Smith 	ciss_printf(sc, "WARNING: completed command with no submitter\n");
22533a31b7ebSMike Smith 	ciss_enqueue_free(cr);
22543a31b7ebSMike Smith     }
22553a31b7ebSMike Smith }
22563a31b7ebSMike Smith 
22573a31b7ebSMike Smith /************************************************************************
22583a31b7ebSMike Smith  * Report on the completion status of a request, and pass back SCSI
22593a31b7ebSMike Smith  * and command status values.
22603a31b7ebSMike Smith  */
22613a31b7ebSMike Smith static int
226222657ce1SScott Long _ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status, const char *func)
22633a31b7ebSMike Smith {
22643a31b7ebSMike Smith     struct ciss_command		*cc;
22653a31b7ebSMike Smith     struct ciss_error_info	*ce;
22663a31b7ebSMike Smith 
22673a31b7ebSMike Smith     debug_called(2);
22683a31b7ebSMike Smith 
22692fdaa90dSScott Long     cc = cr->cr_cc;
22703a31b7ebSMike Smith     ce = (struct ciss_error_info *)&(cc->sg[0]);
22713a31b7ebSMike Smith 
22723a31b7ebSMike Smith     /*
22733a31b7ebSMike Smith      * We don't consider data under/overrun an error for the Report
22743a31b7ebSMike Smith      * Logical/Physical LUNs commands.
22753a31b7ebSMike Smith      */
22763a31b7ebSMike Smith     if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) &&
22772514c5bfSPaul Saab 	((ce->command_status == CISS_CMD_STATUS_DATA_OVERRUN) ||
22782514c5bfSPaul Saab 	 (ce->command_status == CISS_CMD_STATUS_DATA_UNDERRUN)) &&
22793a31b7ebSMike Smith 	((cc->cdb.cdb[0] == CISS_OPCODE_REPORT_LOGICAL_LUNS) ||
22802514c5bfSPaul Saab 	 (cc->cdb.cdb[0] == CISS_OPCODE_REPORT_PHYSICAL_LUNS) ||
22812514c5bfSPaul Saab 	 (cc->cdb.cdb[0] == INQUIRY))) {
22823a31b7ebSMike Smith 	cc->header.host_tag &= ~CISS_HDR_HOST_TAG_ERROR;
22833a31b7ebSMike Smith 	debug(2, "ignoring irrelevant under/overrun error");
22843a31b7ebSMike Smith     }
22853a31b7ebSMike Smith 
22863a31b7ebSMike Smith     /*
22873a31b7ebSMike Smith      * Check the command's error bit, if clear, there's no status and
22883a31b7ebSMike Smith      * everything is OK.
22893a31b7ebSMike Smith      */
22903a31b7ebSMike Smith     if (!(cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR)) {
22913a31b7ebSMike Smith 	if (scsi_status != NULL)
22923a31b7ebSMike Smith 	    *scsi_status = SCSI_STATUS_OK;
22933a31b7ebSMike Smith 	if (command_status != NULL)
22943a31b7ebSMike Smith 	    *command_status = CISS_CMD_STATUS_SUCCESS;
22953a31b7ebSMike Smith 	return(0);
22963a31b7ebSMike Smith     } else {
22973a31b7ebSMike Smith 	if (command_status != NULL)
22983a31b7ebSMike Smith 	    *command_status = ce->command_status;
22993a31b7ebSMike Smith 	if (scsi_status != NULL) {
23003a31b7ebSMike Smith 	    if (ce->command_status == CISS_CMD_STATUS_TARGET_STATUS) {
23013a31b7ebSMike Smith 		*scsi_status = ce->scsi_status;
23023a31b7ebSMike Smith 	    } else {
23033a31b7ebSMike Smith 		*scsi_status = -1;
23043a31b7ebSMike Smith 	    }
23053a31b7ebSMike Smith 	}
23063a31b7ebSMike Smith 	if (bootverbose)
23073a31b7ebSMike Smith 	    ciss_printf(cr->cr_sc, "command status 0x%x (%s) scsi status 0x%x\n",
23083a31b7ebSMike Smith 			ce->command_status, ciss_name_command_status(ce->command_status),
23093a31b7ebSMike Smith 			ce->scsi_status);
23103a31b7ebSMike Smith 	if (ce->command_status == CISS_CMD_STATUS_INVALID_COMMAND) {
231122657ce1SScott Long 	    ciss_printf(cr->cr_sc, "invalid command, offense size %d at %d, value 0x%x, function %s\n",
23123a31b7ebSMike Smith 			ce->additional_error_info.invalid_command.offense_size,
23133a31b7ebSMike Smith 			ce->additional_error_info.invalid_command.offense_offset,
231422657ce1SScott Long 			ce->additional_error_info.invalid_command.offense_value,
231522657ce1SScott Long 			func);
23163a31b7ebSMike Smith 	}
23173a31b7ebSMike Smith     }
2318c6131460SPaul Saab #if 0
2319c6131460SPaul Saab     ciss_print_request(cr);
2320c6131460SPaul Saab #endif
23213a31b7ebSMike Smith     return(1);
23223a31b7ebSMike Smith }
23233a31b7ebSMike Smith 
23243a31b7ebSMike Smith /************************************************************************
23253a31b7ebSMike Smith  * Issue a request and don't return until it's completed.
23263a31b7ebSMike Smith  *
23273a31b7ebSMike Smith  * Depending on adapter status, we may poll or sleep waiting for
23283a31b7ebSMike Smith  * completion.
23293a31b7ebSMike Smith  */
23303a31b7ebSMike Smith static int
23313a31b7ebSMike Smith ciss_synch_request(struct ciss_request *cr, int timeout)
23323a31b7ebSMike Smith {
23333a31b7ebSMike Smith     if (cr->cr_sc->ciss_flags & CISS_FLAG_RUNNING) {
23343a31b7ebSMike Smith 	return(ciss_wait_request(cr, timeout));
23353a31b7ebSMike Smith     } else {
23363a31b7ebSMike Smith 	return(ciss_poll_request(cr, timeout));
23373a31b7ebSMike Smith     }
23383a31b7ebSMike Smith }
23393a31b7ebSMike Smith 
23403a31b7ebSMike Smith /************************************************************************
23413a31b7ebSMike Smith  * Issue a request and poll for completion.
23423a31b7ebSMike Smith  *
23433a31b7ebSMike Smith  * Timeout in milliseconds.
23443a31b7ebSMike Smith  */
23453a31b7ebSMike Smith static int
23463a31b7ebSMike Smith ciss_poll_request(struct ciss_request *cr, int timeout)
23473a31b7ebSMike Smith {
234822657ce1SScott Long     cr_qhead_t qh;
234922657ce1SScott Long     struct ciss_softc *sc;
23503a31b7ebSMike Smith     int		error;
23513a31b7ebSMike Smith 
23523a31b7ebSMike Smith     debug_called(2);
23533a31b7ebSMike Smith 
235422657ce1SScott Long     STAILQ_INIT(&qh);
235522657ce1SScott Long     sc = cr->cr_sc;
23563a31b7ebSMike Smith     cr->cr_flags |= CISS_REQ_POLL;
23573a31b7ebSMike Smith     if ((error = ciss_start(cr)) != 0)
23583a31b7ebSMike Smith 	return(error);
23593a31b7ebSMike Smith 
23603a31b7ebSMike Smith     do {
236122657ce1SScott Long 	if (sc->ciss_perf)
236222657ce1SScott Long 	    ciss_perf_done(sc, &qh);
236322657ce1SScott Long 	else
236422657ce1SScott Long 	    ciss_done(sc, &qh);
236522657ce1SScott Long 	ciss_complete(sc, &qh);
23663a31b7ebSMike Smith 	if (!(cr->cr_flags & CISS_REQ_POLL))
23673a31b7ebSMike Smith 	    return(0);
23683a31b7ebSMike Smith 	DELAY(1000);
23693a31b7ebSMike Smith     } while (timeout-- >= 0);
23703a31b7ebSMike Smith     return(EWOULDBLOCK);
23713a31b7ebSMike Smith }
23723a31b7ebSMike Smith 
23733a31b7ebSMike Smith /************************************************************************
23743a31b7ebSMike Smith  * Issue a request and sleep waiting for completion.
23753a31b7ebSMike Smith  *
23763a31b7ebSMike Smith  * Timeout in milliseconds.  Note that a spurious wakeup will reset
23773a31b7ebSMike Smith  * the timeout.
23783a31b7ebSMike Smith  */
23793a31b7ebSMike Smith static int
23803a31b7ebSMike Smith ciss_wait_request(struct ciss_request *cr, int timeout)
23813a31b7ebSMike Smith {
238222657ce1SScott Long     int		error;
23833a31b7ebSMike Smith 
23843a31b7ebSMike Smith     debug_called(2);
23853a31b7ebSMike Smith 
23863a31b7ebSMike Smith     cr->cr_flags |= CISS_REQ_SLEEP;
23873a31b7ebSMike Smith     if ((error = ciss_start(cr)) != 0)
23883a31b7ebSMike Smith 	return(error);
23893a31b7ebSMike Smith 
239040f05b02SPaul Saab     while ((cr->cr_flags & CISS_REQ_SLEEP) && (error != EWOULDBLOCK)) {
2391975b7318SScott Long 	error = msleep(cr, &cr->cr_sc->ciss_mtx, PRIBIO, "cissREQ", (timeout * hz) / 1000);
23923a31b7ebSMike Smith     }
23933a31b7ebSMike Smith     return(error);
23943a31b7ebSMike Smith }
23953a31b7ebSMike Smith 
23966197ca15SPeter Wemm #if 0
23973a31b7ebSMike Smith /************************************************************************
23983a31b7ebSMike Smith  * Abort a request.  Note that a potential exists here to race the
23993a31b7ebSMike Smith  * request being completed; the caller must deal with this.
24003a31b7ebSMike Smith  */
24013a31b7ebSMike Smith static int
24023a31b7ebSMike Smith ciss_abort_request(struct ciss_request *ar)
24033a31b7ebSMike Smith {
24043a31b7ebSMike Smith     struct ciss_request		*cr;
24053a31b7ebSMike Smith     struct ciss_command		*cc;
24063a31b7ebSMike Smith     struct ciss_message_cdb	*cmc;
24073a31b7ebSMike Smith     int				error;
24083a31b7ebSMike Smith 
24093a31b7ebSMike Smith     debug_called(1);
24103a31b7ebSMike Smith 
24113a31b7ebSMike Smith     /* get a request */
24123a31b7ebSMike Smith     if ((error = ciss_get_request(ar->cr_sc, &cr)) != 0)
24133a31b7ebSMike Smith 	return(error);
24143a31b7ebSMike Smith 
24153a31b7ebSMike Smith     /* build the abort command */
24162fdaa90dSScott Long     cc = cr->cr_cc;
24173a31b7ebSMike Smith     cc->header.address.mode.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;	/* addressing? */
24183a31b7ebSMike Smith     cc->header.address.physical.target = 0;
24193a31b7ebSMike Smith     cc->header.address.physical.bus = 0;
24203a31b7ebSMike Smith     cc->cdb.cdb_length = sizeof(*cmc);
24213a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
24223a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
24233a31b7ebSMike Smith     cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
24243a31b7ebSMike Smith     cc->cdb.timeout = 30;
24253a31b7ebSMike Smith 
24263a31b7ebSMike Smith     cmc = (struct ciss_message_cdb *)&(cc->cdb.cdb[0]);
24273a31b7ebSMike Smith     cmc->opcode = CISS_OPCODE_MESSAGE_ABORT;
24283a31b7ebSMike Smith     cmc->type = CISS_MESSAGE_ABORT_TASK;
24293a31b7ebSMike Smith     cmc->abort_tag = ar->cr_tag;	/* endianness?? */
24303a31b7ebSMike Smith 
24313a31b7ebSMike Smith     /*
24323a31b7ebSMike Smith      * Send the request and wait for a response.  If we believe we
24333a31b7ebSMike Smith      * aborted the request OK, clear the flag that indicates it's
24343a31b7ebSMike Smith      * running.
24353a31b7ebSMike Smith      */
24363a31b7ebSMike Smith     error = ciss_synch_request(cr, 35 * 1000);
24373a31b7ebSMike Smith     if (!error)
24383a31b7ebSMike Smith 	error = ciss_report_request(cr, NULL, NULL);
24393a31b7ebSMike Smith     ciss_release_request(cr);
24403a31b7ebSMike Smith 
24413a31b7ebSMike Smith     return(error);
24423a31b7ebSMike Smith }
24436197ca15SPeter Wemm #endif
24443a31b7ebSMike Smith 
24453a31b7ebSMike Smith 
24463a31b7ebSMike Smith /************************************************************************
24473a31b7ebSMike Smith  * Fetch and initialise a request
24483a31b7ebSMike Smith  */
24493a31b7ebSMike Smith static int
24503a31b7ebSMike Smith ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp)
24513a31b7ebSMike Smith {
24523a31b7ebSMike Smith     struct ciss_request *cr;
24533a31b7ebSMike Smith 
24543a31b7ebSMike Smith     debug_called(2);
24553a31b7ebSMike Smith 
24563a31b7ebSMike Smith     /*
24573a31b7ebSMike Smith      * Get a request and clean it up.
24583a31b7ebSMike Smith      */
24593a31b7ebSMike Smith     if ((cr = ciss_dequeue_free(sc)) == NULL)
24603a31b7ebSMike Smith 	return(ENOMEM);
24613a31b7ebSMike Smith 
24623a31b7ebSMike Smith     cr->cr_data = NULL;
24633a31b7ebSMike Smith     cr->cr_flags = 0;
24643a31b7ebSMike Smith     cr->cr_complete = NULL;
2465639717c8SPaul Saab     cr->cr_private = NULL;
246622657ce1SScott Long     cr->cr_sg_tag = CISS_SG_MAX;	/* Backstop to prevent accidents */
24673a31b7ebSMike Smith 
24683a31b7ebSMike Smith     ciss_preen_command(cr);
24693a31b7ebSMike Smith     *crp = cr;
24703a31b7ebSMike Smith     return(0);
24713a31b7ebSMike Smith }
24723a31b7ebSMike Smith 
24733a31b7ebSMike Smith static void
24743a31b7ebSMike Smith ciss_preen_command(struct ciss_request *cr)
24753a31b7ebSMike Smith {
24763a31b7ebSMike Smith     struct ciss_command	*cc;
24773a31b7ebSMike Smith     u_int32_t		cmdphys;
24783a31b7ebSMike Smith 
24793a31b7ebSMike Smith     /*
24803a31b7ebSMike Smith      * Clean up the command structure.
24813a31b7ebSMike Smith      *
24823a31b7ebSMike Smith      * Note that we set up the error_info structure here, since the
24833a31b7ebSMike Smith      * length can be overwritten by any command.
24843a31b7ebSMike Smith      */
24852fdaa90dSScott Long     cc = cr->cr_cc;
24863a31b7ebSMike Smith     cc->header.sg_in_list = 0;		/* kinda inefficient this way */
24873a31b7ebSMike Smith     cc->header.sg_total = 0;
24883a31b7ebSMike Smith     cc->header.host_tag = cr->cr_tag << 2;
24893a31b7ebSMike Smith     cc->header.host_tag_zeroes = 0;
24902fdaa90dSScott Long     cmdphys = cr->cr_ccphys;
24913a31b7ebSMike Smith     cc->error_info.error_info_address = cmdphys + sizeof(struct ciss_command);
24923a31b7ebSMike Smith     cc->error_info.error_info_length = CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command);
24933a31b7ebSMike Smith }
24943a31b7ebSMike Smith 
24953a31b7ebSMike Smith /************************************************************************
24963a31b7ebSMike Smith  * Release a request to the free list.
24973a31b7ebSMike Smith  */
24983a31b7ebSMike Smith static void
24993a31b7ebSMike Smith ciss_release_request(struct ciss_request *cr)
25003a31b7ebSMike Smith {
25013a31b7ebSMike Smith     struct ciss_softc	*sc;
25023a31b7ebSMike Smith 
25033a31b7ebSMike Smith     debug_called(2);
25043a31b7ebSMike Smith 
25053a31b7ebSMike Smith     sc = cr->cr_sc;
25063a31b7ebSMike Smith 
25073a31b7ebSMike Smith     /* release the request to the free queue */
25083a31b7ebSMike Smith     ciss_requeue_free(cr);
25093a31b7ebSMike Smith }
25103a31b7ebSMike Smith 
25113a31b7ebSMike Smith /************************************************************************
25123a31b7ebSMike Smith  * Allocate a request that will be used to send a BMIC command.  Do some
25133a31b7ebSMike Smith  * of the common setup here to avoid duplicating it everywhere else.
25143a31b7ebSMike Smith  */
25153a31b7ebSMike Smith static int
25163a31b7ebSMike Smith ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
25173a31b7ebSMike Smith 		      int opcode, void **bufp, size_t bufsize)
25183a31b7ebSMike Smith {
25193a31b7ebSMike Smith     struct ciss_request		*cr;
25203a31b7ebSMike Smith     struct ciss_command		*cc;
25213a31b7ebSMike Smith     struct ciss_bmic_cdb	*cbc;
25223a31b7ebSMike Smith     void			*buf;
25233a31b7ebSMike Smith     int				error;
25243a31b7ebSMike Smith     int				dataout;
25253a31b7ebSMike Smith 
25263a31b7ebSMike Smith     debug_called(2);
25273a31b7ebSMike Smith 
25283a31b7ebSMike Smith     cr = NULL;
25293a31b7ebSMike Smith     buf = NULL;
25303a31b7ebSMike Smith 
25313a31b7ebSMike Smith     /*
25323a31b7ebSMike Smith      * Get a request.
25333a31b7ebSMike Smith      */
25343a31b7ebSMike Smith     if ((error = ciss_get_request(sc, &cr)) != 0)
25353a31b7ebSMike Smith 	goto out;
25363a31b7ebSMike Smith 
25373a31b7ebSMike Smith     /*
25383a31b7ebSMike Smith      * Allocate data storage if requested, determine the data direction.
25393a31b7ebSMike Smith      */
25403a31b7ebSMike Smith     dataout = 0;
25413a31b7ebSMike Smith     if ((bufsize > 0) && (bufp != NULL)) {
25423a31b7ebSMike Smith 	if (*bufp == NULL) {
25433a31b7ebSMike Smith 	    if ((buf = malloc(bufsize, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
25443a31b7ebSMike Smith 		error = ENOMEM;
25453a31b7ebSMike Smith 		goto out;
25463a31b7ebSMike Smith 	    }
25473a31b7ebSMike Smith 	} else {
25483a31b7ebSMike Smith 	    buf = *bufp;
25493a31b7ebSMike Smith 	    dataout = 1;	/* we are given a buffer, so we are writing */
25503a31b7ebSMike Smith 	}
25513a31b7ebSMike Smith     }
25523a31b7ebSMike Smith 
25533a31b7ebSMike Smith     /*
25543a31b7ebSMike Smith      * Build a CISS BMIC command to get the logical drive ID.
25553a31b7ebSMike Smith      */
25563a31b7ebSMike Smith     cr->cr_data = buf;
25573a31b7ebSMike Smith     cr->cr_length = bufsize;
25583a31b7ebSMike Smith     if (!dataout)
25593a31b7ebSMike Smith 	cr->cr_flags = CISS_REQ_DATAIN;
25603a31b7ebSMike Smith 
25612fdaa90dSScott Long     cc = cr->cr_cc;
25623a31b7ebSMike Smith     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
25633a31b7ebSMike Smith     cc->header.address.physical.bus = 0;
25643a31b7ebSMike Smith     cc->header.address.physical.target = 0;
25653a31b7ebSMike Smith     cc->cdb.cdb_length = sizeof(*cbc);
25663a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
25673a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
25683a31b7ebSMike Smith     cc->cdb.direction = dataout ? CISS_CDB_DIRECTION_WRITE : CISS_CDB_DIRECTION_READ;
25693a31b7ebSMike Smith     cc->cdb.timeout = 0;
25703a31b7ebSMike Smith 
25713a31b7ebSMike Smith     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
25723a31b7ebSMike Smith     bzero(cbc, sizeof(*cbc));
25733a31b7ebSMike Smith     cbc->opcode = dataout ? CISS_ARRAY_CONTROLLER_WRITE : CISS_ARRAY_CONTROLLER_READ;
25743a31b7ebSMike Smith     cbc->bmic_opcode = opcode;
25753a31b7ebSMike Smith     cbc->size = htons((u_int16_t)bufsize);
25763a31b7ebSMike Smith 
25773a31b7ebSMike Smith out:
25783a31b7ebSMike Smith     if (error) {
25793a31b7ebSMike Smith 	if (cr != NULL)
25803a31b7ebSMike Smith 	    ciss_release_request(cr);
25813a31b7ebSMike Smith     } else {
25823a31b7ebSMike Smith 	*crp = cr;
25833a31b7ebSMike Smith 	if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL))
25843a31b7ebSMike Smith 	    *bufp = buf;
25853a31b7ebSMike Smith     }
25863a31b7ebSMike Smith     return(error);
25873a31b7ebSMike Smith }
25883a31b7ebSMike Smith 
25893a31b7ebSMike Smith /************************************************************************
25903a31b7ebSMike Smith  * Handle a command passed in from userspace.
25913a31b7ebSMike Smith  */
25923a31b7ebSMike Smith static int
25933a31b7ebSMike Smith ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc)
25943a31b7ebSMike Smith {
25953a31b7ebSMike Smith     struct ciss_request		*cr;
25963a31b7ebSMike Smith     struct ciss_command		*cc;
25973a31b7ebSMike Smith     struct ciss_error_info	*ce;
25983c63a8b4SPaul Saab     int				error = 0;
25993a31b7ebSMike Smith 
26003a31b7ebSMike Smith     debug_called(1);
26013a31b7ebSMike Smith 
26023a31b7ebSMike Smith     cr = NULL;
26033a31b7ebSMike Smith 
26043a31b7ebSMike Smith     /*
26053a31b7ebSMike Smith      * Get a request.
26063a31b7ebSMike Smith      */
2607f83695f5SPaul Saab     while (ciss_get_request(sc, &cr) != 0)
2608975b7318SScott Long 	msleep(sc, &sc->ciss_mtx, PPAUSE, "cissREQ", hz);
26092fdaa90dSScott Long     cc = cr->cr_cc;
26103a31b7ebSMike Smith 
26113a31b7ebSMike Smith     /*
26123a31b7ebSMike Smith      * Allocate an in-kernel databuffer if required, copy in user data.
26133a31b7ebSMike Smith      */
2614744c0d23SScott Long     mtx_unlock(&sc->ciss_mtx);
26153a31b7ebSMike Smith     cr->cr_length = ioc->buf_size;
26163a31b7ebSMike Smith     if (ioc->buf_size > 0) {
2617975b7318SScott Long 	if ((cr->cr_data = malloc(ioc->buf_size, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
26183a31b7ebSMike Smith 	    error = ENOMEM;
2619744c0d23SScott Long 	    goto out_unlocked;
26203a31b7ebSMike Smith 	}
26213a31b7ebSMike Smith 	if ((error = copyin(ioc->buf, cr->cr_data, ioc->buf_size))) {
26223a31b7ebSMike Smith 	    debug(0, "copyin: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2623744c0d23SScott Long 	    goto out_unlocked;
26243a31b7ebSMike Smith 	}
26253a31b7ebSMike Smith     }
26263a31b7ebSMike Smith 
26273a31b7ebSMike Smith     /*
26283a31b7ebSMike Smith      * Build the request based on the user command.
26293a31b7ebSMike Smith      */
26303a31b7ebSMike Smith     bcopy(&ioc->LUN_info, &cc->header.address, sizeof(cc->header.address));
26313a31b7ebSMike Smith     bcopy(&ioc->Request, &cc->cdb, sizeof(cc->cdb));
26323a31b7ebSMike Smith 
26333a31b7ebSMike Smith     /* XXX anything else to populate here? */
2634744c0d23SScott Long     mtx_lock(&sc->ciss_mtx);
26353a31b7ebSMike Smith 
26363a31b7ebSMike Smith     /*
26373a31b7ebSMike Smith      * Run the command.
26383a31b7ebSMike Smith      */
26393a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000))) {
26403a31b7ebSMike Smith 	debug(0, "request failed - %d", error);
26413a31b7ebSMike Smith 	goto out;
26423a31b7ebSMike Smith     }
26433a31b7ebSMike Smith 
26443a31b7ebSMike Smith     /*
26453c63a8b4SPaul Saab      * Check to see if the command succeeded.
26463a31b7ebSMike Smith      */
26473a31b7ebSMike Smith     ce = (struct ciss_error_info *)&(cc->sg[0]);
26483c6b8353SPaul Saab     if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) == 0)
26493c63a8b4SPaul Saab 	bzero(ce, sizeof(*ce));
26503c63a8b4SPaul Saab 
26513c63a8b4SPaul Saab     /*
26523c63a8b4SPaul Saab      * Copy the results back to the user.
26533c63a8b4SPaul Saab      */
26543a31b7ebSMike Smith     bcopy(ce, &ioc->error_info, sizeof(*ce));
2655744c0d23SScott Long     mtx_unlock(&sc->ciss_mtx);
26563a31b7ebSMike Smith     if ((ioc->buf_size > 0) &&
26573a31b7ebSMike Smith 	(error = copyout(cr->cr_data, ioc->buf, ioc->buf_size))) {
26583a31b7ebSMike Smith 	debug(0, "copyout: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2659744c0d23SScott Long 	goto out_unlocked;
26603a31b7ebSMike Smith     }
26613a31b7ebSMike Smith 
26623a31b7ebSMike Smith     /* done OK */
26633a31b7ebSMike Smith     error = 0;
26643a31b7ebSMike Smith 
2665744c0d23SScott Long out_unlocked:
2666744c0d23SScott Long     mtx_lock(&sc->ciss_mtx);
2667744c0d23SScott Long 
26683a31b7ebSMike Smith out:
26693a31b7ebSMike Smith     if ((cr != NULL) && (cr->cr_data != NULL))
26703a31b7ebSMike Smith 	free(cr->cr_data, CISS_MALLOC_CLASS);
26713a31b7ebSMike Smith     if (cr != NULL)
26723a31b7ebSMike Smith 	ciss_release_request(cr);
26733a31b7ebSMike Smith     return(error);
26743a31b7ebSMike Smith }
26753a31b7ebSMike Smith 
26763a31b7ebSMike Smith /************************************************************************
26773a31b7ebSMike Smith  * Map a request into bus-visible space, initialise the scatter/gather
26783a31b7ebSMike Smith  * list.
26793a31b7ebSMike Smith  */
26803a31b7ebSMike Smith static int
26813a31b7ebSMike Smith ciss_map_request(struct ciss_request *cr)
26823a31b7ebSMike Smith {
26833a31b7ebSMike Smith     struct ciss_softc	*sc;
2684639717c8SPaul Saab     int			error = 0;
26853a31b7ebSMike Smith 
26863a31b7ebSMike Smith     debug_called(2);
26873a31b7ebSMike Smith 
26883a31b7ebSMike Smith     sc = cr->cr_sc;
26893a31b7ebSMike Smith 
26903a31b7ebSMike Smith     /* check that mapping is necessary */
2691639717c8SPaul Saab     if (cr->cr_flags & CISS_REQ_MAPPED)
26923a31b7ebSMike Smith 	return(0);
26933a31b7ebSMike Smith 
26943a31b7ebSMike Smith     cr->cr_flags |= CISS_REQ_MAPPED;
2695639717c8SPaul Saab 
2696639717c8SPaul Saab     bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2697639717c8SPaul Saab 		    BUS_DMASYNC_PREWRITE);
2698639717c8SPaul Saab 
2699639717c8SPaul Saab     if (cr->cr_data != NULL) {
2700dd0b4fb6SKonstantin Belousov 	if (cr->cr_flags & CISS_REQ_CCB)
2701dd0b4fb6SKonstantin Belousov 		error = bus_dmamap_load_ccb(sc->ciss_buffer_dmat,
2702dd0b4fb6SKonstantin Belousov 					cr->cr_datamap, cr->cr_data,
2703dd0b4fb6SKonstantin Belousov 					ciss_request_map_helper, cr, 0);
2704dd0b4fb6SKonstantin Belousov 	else
2705639717c8SPaul Saab 		error = bus_dmamap_load(sc->ciss_buffer_dmat, cr->cr_datamap,
2706639717c8SPaul Saab 					cr->cr_data, cr->cr_length,
2707639717c8SPaul Saab 					ciss_request_map_helper, cr, 0);
2708639717c8SPaul Saab 	if (error != 0)
2709639717c8SPaul Saab 	    return (error);
2710639717c8SPaul Saab     } else {
2711639717c8SPaul Saab 	/*
2712639717c8SPaul Saab 	 * Post the command to the adapter.
2713639717c8SPaul Saab 	 */
271422657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_NONE;
271522657ce1SScott Long 	cr->cr_flags |= CISS_REQ_BUSY;
271622657ce1SScott Long 	if (sc->ciss_perf)
271722657ce1SScott Long 	    CISS_TL_PERF_POST_CMD(sc, cr);
271822657ce1SScott Long 	else
27192fdaa90dSScott Long 	    CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
2720639717c8SPaul Saab     }
2721639717c8SPaul Saab 
27223a31b7ebSMike Smith     return(0);
27233a31b7ebSMike Smith }
27243a31b7ebSMike Smith 
27253a31b7ebSMike Smith static void
27263a31b7ebSMike Smith ciss_request_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
27273a31b7ebSMike Smith {
27283a31b7ebSMike Smith     struct ciss_command	*cc;
2729639717c8SPaul Saab     struct ciss_request *cr;
2730639717c8SPaul Saab     struct ciss_softc	*sc;
27313a31b7ebSMike Smith     int			i;
27323a31b7ebSMike Smith 
27333a31b7ebSMike Smith     debug_called(2);
27343a31b7ebSMike Smith 
2735639717c8SPaul Saab     cr = (struct ciss_request *)arg;
2736639717c8SPaul Saab     sc = cr->cr_sc;
27372fdaa90dSScott Long     cc = cr->cr_cc;
2738639717c8SPaul Saab 
27393a31b7ebSMike Smith     for (i = 0; i < nseg; i++) {
27403a31b7ebSMike Smith 	cc->sg[i].address = segs[i].ds_addr;
27413a31b7ebSMike Smith 	cc->sg[i].length = segs[i].ds_len;
27423a31b7ebSMike Smith 	cc->sg[i].extension = 0;
27433a31b7ebSMike Smith     }
27443a31b7ebSMike Smith     /* we leave the s/g table entirely within the command */
27453a31b7ebSMike Smith     cc->header.sg_in_list = nseg;
27463a31b7ebSMike Smith     cc->header.sg_total = nseg;
2747639717c8SPaul Saab 
2748639717c8SPaul Saab     if (cr->cr_flags & CISS_REQ_DATAIN)
2749639717c8SPaul Saab 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREREAD);
2750639717c8SPaul Saab     if (cr->cr_flags & CISS_REQ_DATAOUT)
2751639717c8SPaul Saab 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREWRITE);
2752639717c8SPaul Saab 
275362fdee1dSScott Long     if (nseg == 0)
275422657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_NONE;
275522657ce1SScott Long     else if (nseg == 1)
275622657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_1;
275722657ce1SScott Long     else if (nseg == 2)
275822657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_2;
275922657ce1SScott Long     else if (nseg <= 4)
276022657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_4;
276122657ce1SScott Long     else if (nseg <= 8)
276222657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_8;
276322657ce1SScott Long     else if (nseg <= 16)
276422657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_16;
276522657ce1SScott Long     else if (nseg <= 32)
276622657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_32;
276722657ce1SScott Long     else
276822657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_MAX;
276922657ce1SScott Long 
2770639717c8SPaul Saab     /*
2771639717c8SPaul Saab      * Post the command to the adapter.
2772639717c8SPaul Saab      */
277322657ce1SScott Long     cr->cr_flags |= CISS_REQ_BUSY;
277422657ce1SScott Long     if (sc->ciss_perf)
277522657ce1SScott Long 	CISS_TL_PERF_POST_CMD(sc, cr);
277622657ce1SScott Long     else
27772fdaa90dSScott Long 	CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
27783a31b7ebSMike Smith }
27793a31b7ebSMike Smith 
27803a31b7ebSMike Smith /************************************************************************
27813a31b7ebSMike Smith  * Unmap a request from bus-visible space.
27823a31b7ebSMike Smith  */
27833a31b7ebSMike Smith static void
27843a31b7ebSMike Smith ciss_unmap_request(struct ciss_request *cr)
27853a31b7ebSMike Smith {
27863a31b7ebSMike Smith     struct ciss_softc	*sc;
27873a31b7ebSMike Smith 
27883a31b7ebSMike Smith     debug_called(2);
27893a31b7ebSMike Smith 
27903a31b7ebSMike Smith     sc = cr->cr_sc;
27913a31b7ebSMike Smith 
27923a31b7ebSMike Smith     /* check that unmapping is necessary */
2793639717c8SPaul Saab     if ((cr->cr_flags & CISS_REQ_MAPPED) == 0)
27943a31b7ebSMike Smith 	return;
27953a31b7ebSMike Smith 
2796639717c8SPaul Saab     bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2797639717c8SPaul Saab 		    BUS_DMASYNC_POSTWRITE);
2798639717c8SPaul Saab 
2799639717c8SPaul Saab     if (cr->cr_data == NULL)
2800639717c8SPaul Saab 	goto out;
2801639717c8SPaul Saab 
28023a31b7ebSMike Smith     if (cr->cr_flags & CISS_REQ_DATAIN)
28033a31b7ebSMike Smith 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTREAD);
28043a31b7ebSMike Smith     if (cr->cr_flags & CISS_REQ_DATAOUT)
28053a31b7ebSMike Smith 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTWRITE);
28063a31b7ebSMike Smith 
28073a31b7ebSMike Smith     bus_dmamap_unload(sc->ciss_buffer_dmat, cr->cr_datamap);
2808639717c8SPaul Saab out:
28093a31b7ebSMike Smith     cr->cr_flags &= ~CISS_REQ_MAPPED;
28103a31b7ebSMike Smith }
28113a31b7ebSMike Smith 
28123a31b7ebSMike Smith /************************************************************************
28133a31b7ebSMike Smith  * Attach the driver to CAM.
28143a31b7ebSMike Smith  *
28153a31b7ebSMike Smith  * We put all the logical drives on a single SCSI bus.
28163a31b7ebSMike Smith  */
28173a31b7ebSMike Smith static int
28183a31b7ebSMike Smith ciss_cam_init(struct ciss_softc *sc)
28193a31b7ebSMike Smith {
28201fe6c4eeSScott Long     int			i, maxbus;
28213a31b7ebSMike Smith 
28223a31b7ebSMike Smith     debug_called(1);
28233a31b7ebSMike Smith 
28243a31b7ebSMike Smith     /*
28253a31b7ebSMike Smith      * Allocate a devq.  We can reuse this for the masked physical
28263a31b7ebSMike Smith      * devices if we decide to export these as well.
28273a31b7ebSMike Smith      */
282895dab4f2SAlexander Motin     if ((sc->ciss_cam_devq = cam_simq_alloc(sc->ciss_max_requests - 2)) == NULL) {
28293a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate CAM SIM queue\n");
28303a31b7ebSMike Smith 	return(ENOMEM);
28313a31b7ebSMike Smith     }
28323a31b7ebSMike Smith 
28333a31b7ebSMike Smith     /*
28343a31b7ebSMike Smith      * Create a SIM.
28351fe6c4eeSScott Long      *
28361fe6c4eeSScott Long      * This naturally wastes a bit of memory.  The alternative is to allocate
28371fe6c4eeSScott Long      * and register each bus as it is found, and then track them on a linked
28381fe6c4eeSScott Long      * list.  Unfortunately, the driver has a few places where it needs to
28391fe6c4eeSScott Long      * look up the SIM based solely on bus number, and it's unclear whether
28401fe6c4eeSScott Long      * a list traversal would work for these situations.
28413a31b7ebSMike Smith      */
28421fe6c4eeSScott Long     maxbus = max(sc->ciss_max_logical_bus, sc->ciss_max_physical_bus +
28431fe6c4eeSScott Long 		 CISS_PHYSICAL_BASE);
28441fe6c4eeSScott Long     sc->ciss_cam_sim = malloc(maxbus * sizeof(struct cam_sim*),
2845c6131460SPaul Saab 			      CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
2846c6131460SPaul Saab     if (sc->ciss_cam_sim == NULL) {
2847c6131460SPaul Saab 	ciss_printf(sc, "can't allocate memory for controller SIM\n");
2848c6131460SPaul Saab 	return(ENOMEM);
2849c6131460SPaul Saab     }
2850c6131460SPaul Saab 
28511fe6c4eeSScott Long     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
2852c6131460SPaul Saab 	if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
2853c6131460SPaul Saab 						 "ciss", sc,
28542aa6b972SPaul Saab 						 device_get_unit(sc->ciss_dev),
285545650f52SScott Long 						 &sc->ciss_mtx,
285622657ce1SScott Long 						 2,
2857d4aa427fSPaul Saab 						 sc->ciss_max_requests - 2,
28583a31b7ebSMike Smith 						 sc->ciss_cam_devq)) == NULL) {
2859c6131460SPaul Saab 	    ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
28603a31b7ebSMike Smith 	    return(ENOMEM);
28613a31b7ebSMike Smith 	}
28623a31b7ebSMike Smith 
28633a31b7ebSMike Smith 	/*
2864c6131460SPaul Saab 	 * Register bus with this SIM.
28653a31b7ebSMike Smith 	 */
2866975b7318SScott Long 	mtx_lock(&sc->ciss_mtx);
28671fe6c4eeSScott Long 	if (i == 0 || sc->ciss_controllers[i].physical.bus != 0) {
2868b50569b7SScott Long 	    if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
28691fe6c4eeSScott Long 		ciss_printf(sc, "can't register SCSI bus %d\n", i);
2870975b7318SScott Long 		mtx_unlock(&sc->ciss_mtx);
28711fe6c4eeSScott Long 		return (ENXIO);
28721fe6c4eeSScott Long 	    }
28731fe6c4eeSScott Long 	}
2874975b7318SScott Long 	mtx_unlock(&sc->ciss_mtx);
28751fe6c4eeSScott Long     }
28761fe6c4eeSScott Long 
28771fe6c4eeSScott Long     for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
28781fe6c4eeSScott Long 	 CISS_PHYSICAL_BASE; i++) {
28791fe6c4eeSScott Long 	if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
28801fe6c4eeSScott Long 						 "ciss", sc,
28811fe6c4eeSScott Long 						 device_get_unit(sc->ciss_dev),
2882975b7318SScott Long 						 &sc->ciss_mtx, 1,
28831fe6c4eeSScott Long 						 sc->ciss_max_requests - 2,
28841fe6c4eeSScott Long 						 sc->ciss_cam_devq)) == NULL) {
28851fe6c4eeSScott Long 	    ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
28861fe6c4eeSScott Long 	    return (ENOMEM);
28871fe6c4eeSScott Long 	}
28881fe6c4eeSScott Long 
2889975b7318SScott Long 	mtx_lock(&sc->ciss_mtx);
2890b50569b7SScott Long 	if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
2891c6131460SPaul Saab 	    ciss_printf(sc, "can't register SCSI bus %d\n", i);
2892975b7318SScott Long 	    mtx_unlock(&sc->ciss_mtx);
28933a31b7ebSMike Smith 	    return (ENXIO);
28943a31b7ebSMike Smith 	}
2895975b7318SScott Long 	mtx_unlock(&sc->ciss_mtx);
2896c6131460SPaul Saab     }
28973a31b7ebSMike Smith 
28983a31b7ebSMike Smith     return(0);
28993a31b7ebSMike Smith }
29003a31b7ebSMike Smith 
29013a31b7ebSMike Smith /************************************************************************
29023a31b7ebSMike Smith  * Initiate a rescan of the 'logical devices' SIM
29033a31b7ebSMike Smith  */
29043a31b7ebSMike Smith static void
2905c6131460SPaul Saab ciss_cam_rescan_target(struct ciss_softc *sc, int bus, int target)
29063a31b7ebSMike Smith {
29073a31b7ebSMike Smith     union ccb		*ccb;
29083a31b7ebSMike Smith 
29093a31b7ebSMike Smith     debug_called(1);
29103a31b7ebSMike Smith 
291183c5d981SAlexander Motin     if ((ccb = xpt_alloc_ccb_nowait()) == NULL) {
29123a31b7ebSMike Smith 	ciss_printf(sc, "rescan failed (can't allocate CCB)\n");
29133a31b7ebSMike Smith 	return;
29143a31b7ebSMike Smith     }
29153a31b7ebSMike Smith 
291683c5d981SAlexander Motin     if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
291783c5d981SAlexander Motin 	    cam_sim_path(sc->ciss_cam_sim[bus]),
2918c6131460SPaul Saab 	    target, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
29193a31b7ebSMike Smith 	ciss_printf(sc, "rescan failed (can't create path)\n");
292083c5d981SAlexander Motin 	xpt_free_ccb(ccb);
29213a31b7ebSMike Smith 	return;
29223a31b7ebSMike Smith     }
292383c5d981SAlexander Motin     xpt_rescan(ccb);
29243a31b7ebSMike Smith     /* scan is now in progress */
29253a31b7ebSMike Smith }
29263a31b7ebSMike Smith 
29273a31b7ebSMike Smith /************************************************************************
29283a31b7ebSMike Smith  * Handle requests coming from CAM
29293a31b7ebSMike Smith  */
29303a31b7ebSMike Smith static void
29313a31b7ebSMike Smith ciss_cam_action(struct cam_sim *sim, union ccb *ccb)
29323a31b7ebSMike Smith {
2933440baa08SPaul Saab     struct ciss_softc	*sc;
2934440baa08SPaul Saab     struct ccb_scsiio	*csio;
2935c6131460SPaul Saab     int			bus, target;
29361fe6c4eeSScott Long     int			physical;
2937440baa08SPaul Saab 
2938440baa08SPaul Saab     sc = cam_sim_softc(sim);
2939c6131460SPaul Saab     bus = cam_sim_bus(sim);
2940440baa08SPaul Saab     csio = (struct ccb_scsiio *)&ccb->csio;
2941440baa08SPaul Saab     target = csio->ccb_h.target_id;
29421fe6c4eeSScott Long     physical = CISS_IS_PHYSICAL(bus);
2943440baa08SPaul Saab 
29443a31b7ebSMike Smith     switch (ccb->ccb_h.func_code) {
29453a31b7ebSMike Smith 
29463a31b7ebSMike Smith 	/* perform SCSI I/O */
29473a31b7ebSMike Smith     case XPT_SCSI_IO:
2948440baa08SPaul Saab 	if (!ciss_cam_action_io(sim, csio))
29493a31b7ebSMike Smith 	    return;
29503a31b7ebSMike Smith 	break;
29513a31b7ebSMike Smith 
29523a31b7ebSMike Smith 	/* perform geometry calculations */
29533a31b7ebSMike Smith     case XPT_CALC_GEOMETRY:
29543a31b7ebSMike Smith     {
29553a31b7ebSMike Smith 	struct ccb_calc_geometry	*ccg = &ccb->ccg;
29561fe6c4eeSScott Long 	struct ciss_ldrive		*ld;
29573a31b7ebSMike Smith 
29583a31b7ebSMike Smith 	debug(1, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
29593a31b7ebSMike Smith 
29601fe6c4eeSScott Long 	ld = NULL;
29611fe6c4eeSScott Long 	if (!physical)
29621fe6c4eeSScott Long 	    ld = &sc->ciss_logical[bus][target];
29631fe6c4eeSScott Long 
29643a31b7ebSMike Smith 	/*
2965440baa08SPaul Saab 	 * Use the cached geometry settings unless the fault tolerance
2966440baa08SPaul Saab 	 * is invalid.
29673a31b7ebSMike Smith 	 */
29681fe6c4eeSScott Long 	if (physical || ld->cl_geometry.fault_tolerance == 0xFF) {
2969440baa08SPaul Saab 	    u_int32_t			secs_per_cylinder;
2970440baa08SPaul Saab 
29713a31b7ebSMike Smith 	    ccg->heads = 255;
29723a31b7ebSMike Smith 	    ccg->secs_per_track = 32;
29733a31b7ebSMike Smith 	    secs_per_cylinder = ccg->heads * ccg->secs_per_track;
29743a31b7ebSMike Smith 	    ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2975440baa08SPaul Saab 	} else {
2976440baa08SPaul Saab 	    ccg->heads = ld->cl_geometry.heads;
2977440baa08SPaul Saab 	    ccg->secs_per_track = ld->cl_geometry.sectors;
2978440baa08SPaul Saab 	    ccg->cylinders = ntohs(ld->cl_geometry.cylinders);
2979440baa08SPaul Saab 	}
29803a31b7ebSMike Smith 	ccb->ccb_h.status = CAM_REQ_CMP;
29813a31b7ebSMike Smith         break;
29823a31b7ebSMike Smith     }
29833a31b7ebSMike Smith 
29843a31b7ebSMike Smith 	/* handle path attribute inquiry */
29853a31b7ebSMike Smith     case XPT_PATH_INQ:
29863a31b7ebSMike Smith     {
29873a31b7ebSMike Smith 	struct ccb_pathinq	*cpi = &ccb->cpi;
29883a31b7ebSMike Smith 
29893a31b7ebSMike Smith 	debug(1, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
29903a31b7ebSMike Smith 
29913a31b7ebSMike Smith 	cpi->version_num = 1;
29923a31b7ebSMike Smith 	cpi->hba_inquiry = PI_TAG_ABLE;	/* XXX is this correct? */
29933a31b7ebSMike Smith 	cpi->target_sprt = 0;
29943a31b7ebSMike Smith 	cpi->hba_misc = 0;
2995e3edca22SSean Bruno 	cpi->max_target = sc->ciss_cfg->max_logical_supported;
29963a31b7ebSMike Smith 	cpi->max_lun = 0;		/* 'logical drive' channel only */
2997e3edca22SSean Bruno 	cpi->initiator_id = sc->ciss_cfg->max_logical_supported;
29983a31b7ebSMike Smith 	strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
29993a31b7ebSMike Smith         strncpy(cpi->hba_vid, "msmith@freebsd.org", HBA_IDLEN);
30003a31b7ebSMike Smith         strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
30013a31b7ebSMike Smith         cpi->unit_number = cam_sim_unit(sim);
30023a31b7ebSMike Smith         cpi->bus_id = cam_sim_bus(sim);
30033a31b7ebSMike Smith 	cpi->base_transfer_speed = 132 * 1024;	/* XXX what to set this to? */
3004fa9ed865SMatt Jacob 	cpi->transport = XPORT_SPI;
3005fa9ed865SMatt Jacob 	cpi->transport_version = 2;
3006fa9ed865SMatt Jacob 	cpi->protocol = PROTO_SCSI;
3007fa9ed865SMatt Jacob 	cpi->protocol_version = SCSI_REV_2;
300852c9ce25SScott Long 	cpi->maxio = (CISS_MAX_SG_ELEMENTS - 1) * PAGE_SIZE;
30093a31b7ebSMike Smith 	ccb->ccb_h.status = CAM_REQ_CMP;
30103a31b7ebSMike Smith 	break;
30113a31b7ebSMike Smith     }
30123a31b7ebSMike Smith 
30133a31b7ebSMike Smith     case XPT_GET_TRAN_SETTINGS:
30143a31b7ebSMike Smith     {
30153a31b7ebSMike Smith 	struct ccb_trans_settings	*cts = &ccb->cts;
30163a31b7ebSMike Smith 	int				bus, target;
301763b9228bSScott Long 	struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
301863b9228bSScott Long 	struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
30193a31b7ebSMike Smith 
30203a31b7ebSMike Smith 	bus = cam_sim_bus(sim);
30213a31b7ebSMike Smith 	target = cts->ccb_h.target_id;
30223a31b7ebSMike Smith 
30233a31b7ebSMike Smith 	debug(1, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target);
30243a31b7ebSMike Smith 	/* disconnect always OK */
3025fa9ed865SMatt Jacob 	cts->protocol = PROTO_SCSI;
3026fa9ed865SMatt Jacob 	cts->protocol_version = SCSI_REV_2;
3027fa9ed865SMatt Jacob 	cts->transport = XPORT_SPI;
3028fa9ed865SMatt Jacob 	cts->transport_version = 2;
3029fa9ed865SMatt Jacob 
3030fa9ed865SMatt Jacob 	spi->valid = CTS_SPI_VALID_DISC;
3031fa9ed865SMatt Jacob 	spi->flags = CTS_SPI_FLAGS_DISC_ENB;
30323a31b7ebSMike Smith 
303363b9228bSScott Long 	scsi->valid = CTS_SCSI_VALID_TQ;
303463b9228bSScott Long 	scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
303563b9228bSScott Long 
30363a31b7ebSMike Smith 	cts->ccb_h.status = CAM_REQ_CMP;
30373a31b7ebSMike Smith 	break;
30383a31b7ebSMike Smith     }
30393a31b7ebSMike Smith 
30403a31b7ebSMike Smith     default:		/* we can't do this */
30413a31b7ebSMike Smith 	debug(1, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
30423a31b7ebSMike Smith 	ccb->ccb_h.status = CAM_REQ_INVALID;
30433a31b7ebSMike Smith 	break;
30443a31b7ebSMike Smith     }
30453a31b7ebSMike Smith 
30463a31b7ebSMike Smith     xpt_done(ccb);
30473a31b7ebSMike Smith }
30483a31b7ebSMike Smith 
30493a31b7ebSMike Smith /************************************************************************
30503a31b7ebSMike Smith  * Handle a CAM SCSI I/O request.
30513a31b7ebSMike Smith  */
30523a31b7ebSMike Smith static int
30533a31b7ebSMike Smith ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio)
30543a31b7ebSMike Smith {
30553a31b7ebSMike Smith     struct ciss_softc	*sc;
30563a31b7ebSMike Smith     int			bus, target;
30573a31b7ebSMike Smith     struct ciss_request	*cr;
30583a31b7ebSMike Smith     struct ciss_command	*cc;
30593a31b7ebSMike Smith     int			error;
30603a31b7ebSMike Smith 
30613a31b7ebSMike Smith     sc = cam_sim_softc(sim);
30623a31b7ebSMike Smith     bus = cam_sim_bus(sim);
30633a31b7ebSMike Smith     target = csio->ccb_h.target_id;
30643a31b7ebSMike Smith 
30653a31b7ebSMike Smith     debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, csio->ccb_h.target_lun);
30663a31b7ebSMike Smith 
30673a31b7ebSMike Smith     /* check that the CDB pointer is not to a physical address */
30683a31b7ebSMike Smith     if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
30693a31b7ebSMike Smith 	debug(3, "  CDB pointer is to physical address");
30703a31b7ebSMike Smith 	csio->ccb_h.status = CAM_REQ_CMP_ERR;
30713a31b7ebSMike Smith     }
30723a31b7ebSMike Smith 
30733a31b7ebSMike Smith     /* abandon aborted ccbs or those that have failed validation */
30743a31b7ebSMike Smith     if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
30753a31b7ebSMike Smith 	debug(3, "abandoning CCB due to abort/validation failure");
30763a31b7ebSMike Smith 	return(EINVAL);
30773a31b7ebSMike Smith     }
30783a31b7ebSMike Smith 
30793a31b7ebSMike Smith     /* handle emulation of some SCSI commands ourself */
30803a31b7ebSMike Smith     if (ciss_cam_emulate(sc, csio))
30813a31b7ebSMike Smith 	return(0);
30823a31b7ebSMike Smith 
30833a31b7ebSMike Smith     /*
30843a31b7ebSMike Smith      * Get a request to manage this command.  If we can't, return the
30853a31b7ebSMike Smith      * ccb, freeze the queue and flag so that we unfreeze it when a
30863a31b7ebSMike Smith      * request completes.
30873a31b7ebSMike Smith      */
30883a31b7ebSMike Smith     if ((error = ciss_get_request(sc, &cr)) != 0) {
30891fe6c4eeSScott Long 	xpt_freeze_simq(sim, 1);
309095dab4f2SAlexander Motin 	sc->ciss_flags |= CISS_FLAG_BUSY;
30913a31b7ebSMike Smith 	csio->ccb_h.status |= CAM_REQUEUE_REQ;
30923a31b7ebSMike Smith 	return(error);
30933a31b7ebSMike Smith     }
30943a31b7ebSMike Smith 
30953a31b7ebSMike Smith     /*
30963a31b7ebSMike Smith      * Build the command.
30973a31b7ebSMike Smith      */
30982fdaa90dSScott Long     cc = cr->cr_cc;
3099dd0b4fb6SKonstantin Belousov     cr->cr_data = csio;
31003a31b7ebSMike Smith     cr->cr_length = csio->dxfer_len;
31013a31b7ebSMike Smith     cr->cr_complete = ciss_cam_complete;
31023a31b7ebSMike Smith     cr->cr_private = csio;
31033a31b7ebSMike Smith 
3104c6131460SPaul Saab     /*
3105c6131460SPaul Saab      * Target the right logical volume.
3106c6131460SPaul Saab      */
31071fe6c4eeSScott Long     if (CISS_IS_PHYSICAL(bus))
31081fe6c4eeSScott Long 	cc->header.address =
31091fe6c4eeSScott Long 	    sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_address;
31101fe6c4eeSScott Long     else
31111fe6c4eeSScott Long 	cc->header.address =
31121fe6c4eeSScott Long 	    sc->ciss_logical[bus][target].cl_address;
31133a31b7ebSMike Smith     cc->cdb.cdb_length = csio->cdb_len;
31143a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
31153a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;	/* XXX ordered tags? */
31163a31b7ebSMike Smith     if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
3117dd0b4fb6SKonstantin Belousov 	cr->cr_flags = CISS_REQ_DATAOUT | CISS_REQ_CCB;
31183a31b7ebSMike Smith 	cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
31193a31b7ebSMike Smith     } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
3120dd0b4fb6SKonstantin Belousov 	cr->cr_flags = CISS_REQ_DATAIN | CISS_REQ_CCB;
31213a31b7ebSMike Smith 	cc->cdb.direction = CISS_CDB_DIRECTION_READ;
31223a31b7ebSMike Smith     } else {
3123dd0b4fb6SKonstantin Belousov 	cr->cr_data = NULL;
31243a31b7ebSMike Smith 	cr->cr_flags = 0;
31253a31b7ebSMike Smith 	cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
31263a31b7ebSMike Smith     }
31273a31b7ebSMike Smith     cc->cdb.timeout = (csio->ccb_h.timeout / 1000) + 1;
31283a31b7ebSMike Smith     if (csio->ccb_h.flags & CAM_CDB_POINTER) {
31293a31b7ebSMike Smith 	bcopy(csio->cdb_io.cdb_ptr, &cc->cdb.cdb[0], csio->cdb_len);
31303a31b7ebSMike Smith     } else {
31313a31b7ebSMike Smith 	bcopy(csio->cdb_io.cdb_bytes, &cc->cdb.cdb[0], csio->cdb_len);
31323a31b7ebSMike Smith     }
31333a31b7ebSMike Smith 
31343a31b7ebSMike Smith     /*
31353a31b7ebSMike Smith      * Submit the request to the adapter.
31363a31b7ebSMike Smith      *
31373a31b7ebSMike Smith      * Note that this may fail if we're unable to map the request (and
31383a31b7ebSMike Smith      * if we ever learn a transport layer other than simple, may fail
31393a31b7ebSMike Smith      * if the adapter rejects the command).
31403a31b7ebSMike Smith      */
31413a31b7ebSMike Smith     if ((error = ciss_start(cr)) != 0) {
31421fe6c4eeSScott Long 	xpt_freeze_simq(sim, 1);
3143639717c8SPaul Saab 	csio->ccb_h.status |= CAM_RELEASE_SIMQ;
31440a65b79fSAlexander Motin 	if (error == EINPROGRESS) {
3145639717c8SPaul Saab 	    error = 0;
3146639717c8SPaul Saab 	} else {
31473a31b7ebSMike Smith 	    csio->ccb_h.status |= CAM_REQUEUE_REQ;
31483a31b7ebSMike Smith 	    ciss_release_request(cr);
3149639717c8SPaul Saab 	}
31503a31b7ebSMike Smith 	return(error);
31513a31b7ebSMike Smith     }
31523a31b7ebSMike Smith 
31533a31b7ebSMike Smith     return(0);
31543a31b7ebSMike Smith }
31553a31b7ebSMike Smith 
31563a31b7ebSMike Smith /************************************************************************
31573a31b7ebSMike Smith  * Emulate SCSI commands the adapter doesn't handle as we might like.
31583a31b7ebSMike Smith  */
31593a31b7ebSMike Smith static int
31603a31b7ebSMike Smith ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio)
31613a31b7ebSMike Smith {
3162c6131460SPaul Saab     int		bus, target;
31633a31b7ebSMike Smith     u_int8_t	opcode;
31643a31b7ebSMike Smith 
31653a31b7ebSMike Smith     target = csio->ccb_h.target_id;
3166c6131460SPaul Saab     bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
31673a31b7ebSMike Smith     opcode = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
31683a31b7ebSMike Smith 	*(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0];
31693a31b7ebSMike Smith 
31701fe6c4eeSScott Long     if (CISS_IS_PHYSICAL(bus)) {
31711fe6c4eeSScott Long 	if (sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_online != 1) {
31720a65b79fSAlexander Motin 	    csio->ccb_h.status |= CAM_SEL_TIMEOUT;
31731fe6c4eeSScott Long 	    xpt_done((union ccb *)csio);
31741fe6c4eeSScott Long 	    return(1);
31751fe6c4eeSScott Long 	} else
31761fe6c4eeSScott Long 	    return(0);
31771fe6c4eeSScott Long     }
31781fe6c4eeSScott Long 
31793a31b7ebSMike Smith     /*
31806f71a953SPaul Saab      * Handle requests for volumes that don't exist or are not online.
31816f71a953SPaul Saab      * A selection timeout is slightly better than an illegal request.
31826f71a953SPaul Saab      * Other errors might be better.
31833a31b7ebSMike Smith      */
31846f71a953SPaul Saab     if (sc->ciss_logical[bus][target].cl_status != CISS_LD_ONLINE) {
31850a65b79fSAlexander Motin 	csio->ccb_h.status |= CAM_SEL_TIMEOUT;
31863a31b7ebSMike Smith 	xpt_done((union ccb *)csio);
31873a31b7ebSMike Smith 	return(1);
31883a31b7ebSMike Smith     }
31893a31b7ebSMike Smith 
31903a31b7ebSMike Smith     /* if we have to fake Synchronise Cache */
31913a31b7ebSMike Smith     if (sc->ciss_flags & CISS_FLAG_FAKE_SYNCH) {
31923a31b7ebSMike Smith 	/*
31933a31b7ebSMike Smith 	 * If this is a Synchronise Cache command, typically issued when
31943a31b7ebSMike Smith 	 * a device is closed, flush the adapter and complete now.
31953a31b7ebSMike Smith 	 */
31963a31b7ebSMike Smith 	if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
31973a31b7ebSMike Smith 	     *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE) {
31983a31b7ebSMike Smith 	    ciss_flush_adapter(sc);
31990a65b79fSAlexander Motin 	    csio->ccb_h.status |= CAM_REQ_CMP;
32003a31b7ebSMike Smith 	    xpt_done((union ccb *)csio);
32013a31b7ebSMike Smith 	    return(1);
32023a31b7ebSMike Smith 	}
32033a31b7ebSMike Smith     }
32043a31b7ebSMike Smith 
32053a31b7ebSMike Smith     return(0);
32063a31b7ebSMike Smith }
32073a31b7ebSMike Smith 
32083a31b7ebSMike Smith /************************************************************************
32093a31b7ebSMike Smith  * Check for possibly-completed commands.
32103a31b7ebSMike Smith  */
32113a31b7ebSMike Smith static void
32123a31b7ebSMike Smith ciss_cam_poll(struct cam_sim *sim)
32133a31b7ebSMike Smith {
321422657ce1SScott Long     cr_qhead_t qh;
32153a31b7ebSMike Smith     struct ciss_softc	*sc = cam_sim_softc(sim);
32163a31b7ebSMike Smith 
32173a31b7ebSMike Smith     debug_called(2);
32183a31b7ebSMike Smith 
321922657ce1SScott Long     STAILQ_INIT(&qh);
322022657ce1SScott Long     if (sc->ciss_perf)
322122657ce1SScott Long 	ciss_perf_done(sc, &qh);
322222657ce1SScott Long     else
322322657ce1SScott Long 	ciss_done(sc, &qh);
322422657ce1SScott Long     ciss_complete(sc, &qh);
32253a31b7ebSMike Smith }
32263a31b7ebSMike Smith 
32273a31b7ebSMike Smith /************************************************************************
32283a31b7ebSMike Smith  * Handle completion of a command - pass results back through the CCB
32293a31b7ebSMike Smith  */
32303a31b7ebSMike Smith static void
32313a31b7ebSMike Smith ciss_cam_complete(struct ciss_request *cr)
32323a31b7ebSMike Smith {
32333a31b7ebSMike Smith     struct ciss_softc		*sc;
32343a31b7ebSMike Smith     struct ciss_command		*cc;
32353a31b7ebSMike Smith     struct ciss_error_info	*ce;
32363a31b7ebSMike Smith     struct ccb_scsiio		*csio;
32373a31b7ebSMike Smith     int				scsi_status;
32383a31b7ebSMike Smith     int				command_status;
32393a31b7ebSMike Smith 
32403a31b7ebSMike Smith     debug_called(2);
32413a31b7ebSMike Smith 
32423a31b7ebSMike Smith     sc = cr->cr_sc;
32432fdaa90dSScott Long     cc = cr->cr_cc;
32443a31b7ebSMike Smith     ce = (struct ciss_error_info *)&(cc->sg[0]);
32453a31b7ebSMike Smith     csio = (struct ccb_scsiio *)cr->cr_private;
32463a31b7ebSMike Smith 
32473a31b7ebSMike Smith     /*
32483a31b7ebSMike Smith      * Extract status values from request.
32493a31b7ebSMike Smith      */
32503a31b7ebSMike Smith     ciss_report_request(cr, &command_status, &scsi_status);
32513a31b7ebSMike Smith     csio->scsi_status = scsi_status;
32523a31b7ebSMike Smith 
32533a31b7ebSMike Smith     /*
32543a31b7ebSMike Smith      * Handle specific SCSI status values.
32553a31b7ebSMike Smith      */
32563a31b7ebSMike Smith     switch(scsi_status) {
32573a31b7ebSMike Smith 	/* no status due to adapter error */
32583a31b7ebSMike Smith     case -1:
32593a31b7ebSMike Smith 	debug(0, "adapter error");
32600a65b79fSAlexander Motin 	csio->ccb_h.status |= CAM_REQ_CMP_ERR;
32613a31b7ebSMike Smith 	break;
32623a31b7ebSMike Smith 
32633a31b7ebSMike Smith 	/* no status due to command completed OK */
32643a31b7ebSMike Smith     case SCSI_STATUS_OK:		/* CISS_SCSI_STATUS_GOOD */
32653a31b7ebSMike Smith 	debug(2, "SCSI_STATUS_OK");
32660a65b79fSAlexander Motin 	csio->ccb_h.status |= CAM_REQ_CMP;
32673a31b7ebSMike Smith 	break;
32683a31b7ebSMike Smith 
32693a31b7ebSMike Smith 	/* check condition, sense data included */
32703a31b7ebSMike Smith     case SCSI_STATUS_CHECK_COND:	/* CISS_SCSI_STATUS_CHECK_CONDITION */
3271c6131460SPaul Saab 	debug(0, "SCSI_STATUS_CHECK_COND  sense size %d  resid %d\n",
32723a31b7ebSMike Smith 	      ce->sense_length, ce->residual_count);
32733a31b7ebSMike Smith 	bzero(&csio->sense_data, SSD_FULL_SIZE);
32743a31b7ebSMike Smith 	bcopy(&ce->sense_info[0], &csio->sense_data, ce->sense_length);
32751cc052e8SKenneth D. Merry 	if (csio->sense_len > ce->sense_length)
32761cc052e8SKenneth D. Merry 		csio->sense_resid = csio->sense_len - ce->sense_length;
32771cc052e8SKenneth D. Merry 	else
32781cc052e8SKenneth D. Merry 		csio->sense_resid = 0;
32793a31b7ebSMike Smith 	csio->resid = ce->residual_count;
32800a65b79fSAlexander Motin 	csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
32813a31b7ebSMike Smith #ifdef CISS_DEBUG
32823a31b7ebSMike Smith 	{
32833a31b7ebSMike Smith 	    struct scsi_sense_data	*sns = (struct scsi_sense_data *)&ce->sense_info[0];
32841cc052e8SKenneth D. Merry 	    debug(0, "sense key %x", scsi_get_sense_key(sns, csio->sense_len -
32851cc052e8SKenneth D. Merry 		  csio->sense_resid, /*show_errors*/ 1));
32863a31b7ebSMike Smith 	}
32873a31b7ebSMike Smith #endif
32883a31b7ebSMike Smith 	break;
32893a31b7ebSMike Smith 
32903a31b7ebSMike Smith     case SCSI_STATUS_BUSY:		/* CISS_SCSI_STATUS_BUSY */
32913a31b7ebSMike Smith 	debug(0, "SCSI_STATUS_BUSY");
32920a65b79fSAlexander Motin 	csio->ccb_h.status |= CAM_SCSI_BUSY;
32933a31b7ebSMike Smith 	break;
32943a31b7ebSMike Smith 
32953a31b7ebSMike Smith     default:
32963a31b7ebSMike Smith 	debug(0, "unknown status 0x%x", csio->scsi_status);
32970a65b79fSAlexander Motin 	csio->ccb_h.status |= CAM_REQ_CMP_ERR;
32983a31b7ebSMike Smith 	break;
32993a31b7ebSMike Smith     }
33003a31b7ebSMike Smith 
33013a31b7ebSMike Smith     /* handle post-command fixup */
33023a31b7ebSMike Smith     ciss_cam_complete_fixup(sc, csio);
33033a31b7ebSMike Smith 
33043a31b7ebSMike Smith     ciss_release_request(cr);
330595dab4f2SAlexander Motin     if (sc->ciss_flags & CISS_FLAG_BUSY) {
330695dab4f2SAlexander Motin 	sc->ciss_flags &= ~CISS_FLAG_BUSY;
330795dab4f2SAlexander Motin 	if (csio->ccb_h.status & CAM_RELEASE_SIMQ)
330895dab4f2SAlexander Motin 	    xpt_release_simq(xpt_path_sim(csio->ccb_h.path), 0);
330995dab4f2SAlexander Motin 	else
331095dab4f2SAlexander Motin 	    csio->ccb_h.status |= CAM_RELEASE_SIMQ;
331195dab4f2SAlexander Motin     }
331222657ce1SScott Long     xpt_done((union ccb *)csio);
33133a31b7ebSMike Smith }
33143a31b7ebSMike Smith 
33153a31b7ebSMike Smith /********************************************************************************
33163a31b7ebSMike Smith  * Fix up the result of some commands here.
33173a31b7ebSMike Smith  */
33183a31b7ebSMike Smith static void
33193a31b7ebSMike Smith ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio)
33203a31b7ebSMike Smith {
33213a31b7ebSMike Smith     struct scsi_inquiry_data	*inq;
33223a31b7ebSMike Smith     struct ciss_ldrive		*cl;
332357ae362cSAlexander Motin     uint8_t			*cdb;
3324c6131460SPaul Saab     int				bus, target;
33253a31b7ebSMike Smith 
332657ae362cSAlexander Motin     cdb = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
332757ae362cSAlexander Motin 	 (uint8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes;
332857ae362cSAlexander Motin     if (cdb[0] == INQUIRY &&
332957ae362cSAlexander Motin 	(cdb[1] & SI_EVPD) == 0 &&
333057ae362cSAlexander Motin 	(csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN &&
333157ae362cSAlexander Motin 	csio->dxfer_len >= SHORT_INQUIRY_LENGTH) {
33323a31b7ebSMike Smith 
33333a31b7ebSMike Smith 	inq = (struct scsi_inquiry_data *)csio->data_ptr;
33343a31b7ebSMike Smith 	target = csio->ccb_h.target_id;
3335c6131460SPaul Saab 	bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
33361fe6c4eeSScott Long 
33371fe6c4eeSScott Long 	/*
33381fe6c4eeSScott Long 	 * Don't let hard drives be seen by the DA driver.  They will still be
33391fe6c4eeSScott Long 	 * attached by the PASS driver.
33401fe6c4eeSScott Long 	 */
33411fe6c4eeSScott Long 	if (CISS_IS_PHYSICAL(bus)) {
33421fe6c4eeSScott Long 	    if (SID_TYPE(inq) == T_DIRECT)
33431fe6c4eeSScott Long 		inq->device = (inq->device & 0xe0) | T_NODEVICE;
33441fe6c4eeSScott Long 	    return;
33451fe6c4eeSScott Long 	}
33461fe6c4eeSScott Long 
3347c6131460SPaul Saab 	cl = &sc->ciss_logical[bus][target];
33483a31b7ebSMike Smith 
3349d4aa427fSPaul Saab 	padstr(inq->vendor, "COMPAQ", 8);
3350d4aa427fSPaul Saab 	padstr(inq->product, ciss_name_ldrive_org(cl->cl_ldrive->fault_tolerance), 8);
3351d4aa427fSPaul Saab 	padstr(inq->revision, ciss_name_ldrive_status(cl->cl_lstatus->status), 16);
33523a31b7ebSMike Smith     }
33533a31b7ebSMike Smith }
33543a31b7ebSMike Smith 
33553a31b7ebSMike Smith 
33563a31b7ebSMike Smith /********************************************************************************
3357d4aa427fSPaul Saab  * Find a peripheral attached at (target)
33583a31b7ebSMike Smith  */
33593a31b7ebSMike Smith static struct cam_periph *
3360c6131460SPaul Saab ciss_find_periph(struct ciss_softc *sc, int bus, int target)
33613a31b7ebSMike Smith {
33623a31b7ebSMike Smith     struct cam_periph	*periph;
33633a31b7ebSMike Smith     struct cam_path	*path;
33643a31b7ebSMike Smith     int			status;
33653a31b7ebSMike Smith 
3366c6131460SPaul Saab     status = xpt_create_path(&path, NULL, cam_sim_path(sc->ciss_cam_sim[bus]),
3367c6131460SPaul Saab 			     target, 0);
33683a31b7ebSMike Smith     if (status == CAM_REQ_CMP) {
33693a31b7ebSMike Smith 	periph = cam_periph_find(path, NULL);
33703a31b7ebSMike Smith 	xpt_free_path(path);
33713a31b7ebSMike Smith     } else {
33723a31b7ebSMike Smith 	periph = NULL;
33733a31b7ebSMike Smith     }
33743a31b7ebSMike Smith     return(periph);
33753a31b7ebSMike Smith }
33763a31b7ebSMike Smith 
33773a31b7ebSMike Smith /********************************************************************************
33783a31b7ebSMike Smith  * Name the device at (target)
33793a31b7ebSMike Smith  *
33803a31b7ebSMike Smith  * XXX is this strictly correct?
33813a31b7ebSMike Smith  */
338237c84183SPoul-Henning Kamp static int
3383c6131460SPaul Saab ciss_name_device(struct ciss_softc *sc, int bus, int target)
33843a31b7ebSMike Smith {
33853a31b7ebSMike Smith     struct cam_periph	*periph;
33863a31b7ebSMike Smith 
3387d49f1379SPaul Saab     if (CISS_IS_PHYSICAL(bus))
33881fe6c4eeSScott Long 	return (0);
3389c6131460SPaul Saab     if ((periph = ciss_find_periph(sc, bus, target)) != NULL) {
33901fe6c4eeSScott Long 	sprintf(sc->ciss_logical[bus][target].cl_name, "%s%d",
33911fe6c4eeSScott Long 		periph->periph_name, periph->unit_number);
33923a31b7ebSMike Smith 	return(0);
33933a31b7ebSMike Smith     }
3394c6131460SPaul Saab     sc->ciss_logical[bus][target].cl_name[0] = 0;
33953a31b7ebSMike Smith     return(ENOENT);
33963a31b7ebSMike Smith }
33973a31b7ebSMike Smith 
33983a31b7ebSMike Smith /************************************************************************
33993a31b7ebSMike Smith  * Periodic status monitoring.
34003a31b7ebSMike Smith  */
34013a31b7ebSMike Smith static void
34023a31b7ebSMike Smith ciss_periodic(void *arg)
34033a31b7ebSMike Smith {
34043a31b7ebSMike Smith     struct ciss_softc	*sc;
3405e08d902aSMitsuru IWASAKI     struct ciss_request	*cr = NULL;
3406e08d902aSMitsuru IWASAKI     struct ciss_command	*cc = NULL;
3407e08d902aSMitsuru IWASAKI     int			error = 0;
34083a31b7ebSMike Smith 
34093a31b7ebSMike Smith     debug_called(1);
34103a31b7ebSMike Smith 
34113a31b7ebSMike Smith     sc = (struct ciss_softc *)arg;
34123a31b7ebSMike Smith 
34133a31b7ebSMike Smith     /*
34143a31b7ebSMike Smith      * Check the adapter heartbeat.
34153a31b7ebSMike Smith      */
34163a31b7ebSMike Smith     if (sc->ciss_cfg->heartbeat == sc->ciss_heartbeat) {
34173a31b7ebSMike Smith 	sc->ciss_heart_attack++;
34183a31b7ebSMike Smith 	debug(0, "adapter heart attack in progress 0x%x/%d",
34193a31b7ebSMike Smith 	      sc->ciss_heartbeat, sc->ciss_heart_attack);
34203a31b7ebSMike Smith 	if (sc->ciss_heart_attack == 3) {
34213a31b7ebSMike Smith 	    ciss_printf(sc, "ADAPTER HEARTBEAT FAILED\n");
3422e08d902aSMitsuru IWASAKI 	    ciss_disable_adapter(sc);
3423e08d902aSMitsuru IWASAKI 	    return;
34243a31b7ebSMike Smith 	}
34253a31b7ebSMike Smith     } else {
34263a31b7ebSMike Smith 	sc->ciss_heartbeat = sc->ciss_cfg->heartbeat;
34273a31b7ebSMike Smith 	sc->ciss_heart_attack = 0;
34283a31b7ebSMike Smith 	debug(3, "new heartbeat 0x%x", sc->ciss_heartbeat);
34293a31b7ebSMike Smith     }
34303a31b7ebSMike Smith 
34313a31b7ebSMike Smith     /*
3432e08d902aSMitsuru IWASAKI      * Send the NOP message and wait for a response.
3433e08d902aSMitsuru IWASAKI      */
34345a3c4d69SMitsuru IWASAKI     if (ciss_nop_message_heartbeat != 0 && (error = ciss_get_request(sc, &cr)) == 0) {
34352fdaa90dSScott Long 	cc = cr->cr_cc;
343654d02e0aSMitsuru IWASAKI 	cr->cr_complete = ciss_nop_complete;
3437e08d902aSMitsuru IWASAKI 	cc->cdb.cdb_length = 1;
3438e08d902aSMitsuru IWASAKI 	cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
3439e08d902aSMitsuru IWASAKI 	cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3440e08d902aSMitsuru IWASAKI 	cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
3441e08d902aSMitsuru IWASAKI 	cc->cdb.timeout = 0;
3442e08d902aSMitsuru IWASAKI 	cc->cdb.cdb[0] = CISS_OPCODE_MESSAGE_NOP;
3443e08d902aSMitsuru IWASAKI 
344454d02e0aSMitsuru IWASAKI 	if ((error = ciss_start(cr)) != 0) {
3445e08d902aSMitsuru IWASAKI 	    ciss_printf(sc, "SENDING NOP MESSAGE FAILED\n");
3446e08d902aSMitsuru IWASAKI 	}
3447e08d902aSMitsuru IWASAKI     }
3448e08d902aSMitsuru IWASAKI 
3449e08d902aSMitsuru IWASAKI     /*
34503a31b7ebSMike Smith      * If the notify event request has died for some reason, or has
34513a31b7ebSMike Smith      * not started yet, restart it.
34523a31b7ebSMike Smith      */
34533a31b7ebSMike Smith     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK)) {
34543a31b7ebSMike Smith 	debug(0, "(re)starting Event Notify chain");
34553a31b7ebSMike Smith 	ciss_notify_event(sc);
34563a31b7ebSMike Smith     }
34573a31b7ebSMike Smith 
34583a31b7ebSMike Smith     /*
34593a31b7ebSMike Smith      * Reschedule.
34603a31b7ebSMike Smith      */
3461975b7318SScott Long     callout_reset(&sc->ciss_periodic, CISS_HEARTBEAT_RATE * hz, ciss_periodic, sc);
34623a31b7ebSMike Smith }
34633a31b7ebSMike Smith 
346454d02e0aSMitsuru IWASAKI static void
346554d02e0aSMitsuru IWASAKI ciss_nop_complete(struct ciss_request *cr)
346654d02e0aSMitsuru IWASAKI {
346754d02e0aSMitsuru IWASAKI     struct ciss_softc		*sc;
34680aeee4bdSMitsuru IWASAKI     static int			first_time = 1;
346954d02e0aSMitsuru IWASAKI 
347054d02e0aSMitsuru IWASAKI     sc = cr->cr_sc;
347154d02e0aSMitsuru IWASAKI     if (ciss_report_request(cr, NULL, NULL) != 0) {
34720aeee4bdSMitsuru IWASAKI 	if (first_time == 1) {
34730aeee4bdSMitsuru IWASAKI 	    first_time = 0;
34740aeee4bdSMitsuru IWASAKI 	    ciss_printf(sc, "SENDING NOP MESSAGE FAILED (not logging anymore)\n");
34750aeee4bdSMitsuru IWASAKI 	}
347654d02e0aSMitsuru IWASAKI     }
347754d02e0aSMitsuru IWASAKI 
347854d02e0aSMitsuru IWASAKI     ciss_release_request(cr);
347954d02e0aSMitsuru IWASAKI }
348054d02e0aSMitsuru IWASAKI 
34813a31b7ebSMike Smith /************************************************************************
3482e08d902aSMitsuru IWASAKI  * Disable the adapter.
3483e08d902aSMitsuru IWASAKI  *
3484e08d902aSMitsuru IWASAKI  * The all requests in completed queue is failed with hardware error.
3485e08d902aSMitsuru IWASAKI  * This will cause failover in a multipath configuration.
3486e08d902aSMitsuru IWASAKI  */
3487e08d902aSMitsuru IWASAKI static void
3488e08d902aSMitsuru IWASAKI ciss_disable_adapter(struct ciss_softc *sc)
3489e08d902aSMitsuru IWASAKI {
349022657ce1SScott Long     cr_qhead_t			qh;
3491e08d902aSMitsuru IWASAKI     struct ciss_request		*cr;
3492e08d902aSMitsuru IWASAKI     struct ciss_command		*cc;
3493e08d902aSMitsuru IWASAKI     struct ciss_error_info	*ce;
349422657ce1SScott Long     int				i;
3495e08d902aSMitsuru IWASAKI 
3496e08d902aSMitsuru IWASAKI     CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
3497e08d902aSMitsuru IWASAKI     pci_disable_busmaster(sc->ciss_dev);
3498e08d902aSMitsuru IWASAKI     sc->ciss_flags &= ~CISS_FLAG_RUNNING;
3499e08d902aSMitsuru IWASAKI 
350022657ce1SScott Long     for (i = 1; i < sc->ciss_max_requests; i++) {
350122657ce1SScott Long 	cr = &sc->ciss_request[i];
350222657ce1SScott Long 	if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
350322657ce1SScott Long 	    continue;
3504e08d902aSMitsuru IWASAKI 
35052fdaa90dSScott Long 	cc = cr->cr_cc;
3506e08d902aSMitsuru IWASAKI 	ce = (struct ciss_error_info *)&(cc->sg[0]);
3507e08d902aSMitsuru IWASAKI 	ce->command_status = CISS_CMD_STATUS_HARDWARE_ERROR;
350822657ce1SScott Long 	ciss_enqueue_complete(cr, &qh);
3509e08d902aSMitsuru IWASAKI     }
3510e08d902aSMitsuru IWASAKI 
3511e08d902aSMitsuru IWASAKI     for (;;) {
351222657ce1SScott Long 	if ((cr = ciss_dequeue_complete(sc, &qh)) == NULL)
3513e08d902aSMitsuru IWASAKI 	    break;
3514e08d902aSMitsuru IWASAKI 
3515e08d902aSMitsuru IWASAKI 	/*
3516e08d902aSMitsuru IWASAKI 	 * If the request has a callback, invoke it.
3517e08d902aSMitsuru IWASAKI 	 */
3518e08d902aSMitsuru IWASAKI 	if (cr->cr_complete != NULL) {
3519e08d902aSMitsuru IWASAKI 	    cr->cr_complete(cr);
3520e08d902aSMitsuru IWASAKI 	    continue;
3521e08d902aSMitsuru IWASAKI 	}
3522e08d902aSMitsuru IWASAKI 
3523e08d902aSMitsuru IWASAKI 	/*
3524e08d902aSMitsuru IWASAKI 	 * If someone is sleeping on this request, wake them up.
3525e08d902aSMitsuru IWASAKI 	 */
3526e08d902aSMitsuru IWASAKI 	if (cr->cr_flags & CISS_REQ_SLEEP) {
3527e08d902aSMitsuru IWASAKI 	    cr->cr_flags &= ~CISS_REQ_SLEEP;
3528e08d902aSMitsuru IWASAKI 	    wakeup(cr);
3529e08d902aSMitsuru IWASAKI 	    continue;
3530e08d902aSMitsuru IWASAKI 	}
3531e08d902aSMitsuru IWASAKI     }
3532e08d902aSMitsuru IWASAKI }
3533e08d902aSMitsuru IWASAKI 
3534e08d902aSMitsuru IWASAKI /************************************************************************
35353a31b7ebSMike Smith  * Request a notification response from the adapter.
35363a31b7ebSMike Smith  *
35373a31b7ebSMike Smith  * If (cr) is NULL, this is the first request of the adapter, so
35383a31b7ebSMike Smith  * reset the adapter's message pointer and start with the oldest
35393a31b7ebSMike Smith  * message available.
35403a31b7ebSMike Smith  */
35413a31b7ebSMike Smith static void
35423a31b7ebSMike Smith ciss_notify_event(struct ciss_softc *sc)
35433a31b7ebSMike Smith {
35443a31b7ebSMike Smith     struct ciss_request		*cr;
35453a31b7ebSMike Smith     struct ciss_command		*cc;
35463a31b7ebSMike Smith     struct ciss_notify_cdb	*cnc;
35473a31b7ebSMike Smith     int				error;
35483a31b7ebSMike Smith 
35493a31b7ebSMike Smith     debug_called(1);
35503a31b7ebSMike Smith 
35513a31b7ebSMike Smith     cr = sc->ciss_periodic_notify;
35523a31b7ebSMike Smith 
35533a31b7ebSMike Smith     /* get a request if we don't already have one */
35543a31b7ebSMike Smith     if (cr == NULL) {
35553a31b7ebSMike Smith 	if ((error = ciss_get_request(sc, &cr)) != 0) {
35563a31b7ebSMike Smith 	    debug(0, "can't get notify event request");
35573a31b7ebSMike Smith 	    goto out;
35583a31b7ebSMike Smith 	}
35593a31b7ebSMike Smith 	sc->ciss_periodic_notify = cr;
35603a31b7ebSMike Smith 	cr->cr_complete = ciss_notify_complete;
35613a31b7ebSMike Smith 	debug(1, "acquired request %d", cr->cr_tag);
35623a31b7ebSMike Smith     }
35633a31b7ebSMike Smith 
35643a31b7ebSMike Smith     /*
35653a31b7ebSMike Smith      * Get a databuffer if we don't already have one, note that the
35663a31b7ebSMike Smith      * adapter command wants a larger buffer than the actual
35673a31b7ebSMike Smith      * structure.
35683a31b7ebSMike Smith      */
35693a31b7ebSMike Smith     if (cr->cr_data == NULL) {
35703a31b7ebSMike Smith 	if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
35713a31b7ebSMike Smith 	    debug(0, "can't get notify event request buffer");
35723a31b7ebSMike Smith 	    error = ENOMEM;
35733a31b7ebSMike Smith 	    goto out;
35743a31b7ebSMike Smith 	}
35753a31b7ebSMike Smith 	cr->cr_length = CISS_NOTIFY_DATA_SIZE;
35763a31b7ebSMike Smith     }
35773a31b7ebSMike Smith 
35783a31b7ebSMike Smith     /* re-setup the request's command (since we never release it) XXX overkill*/
35793a31b7ebSMike Smith     ciss_preen_command(cr);
35803a31b7ebSMike Smith 
35813a31b7ebSMike Smith     /* (re)build the notify event command */
35822fdaa90dSScott Long     cc = cr->cr_cc;
35833a31b7ebSMike Smith     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
35843a31b7ebSMike Smith     cc->header.address.physical.bus = 0;
35853a31b7ebSMike Smith     cc->header.address.physical.target = 0;
35863a31b7ebSMike Smith 
35873a31b7ebSMike Smith     cc->cdb.cdb_length = sizeof(*cnc);
35883a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
35893a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
35903a31b7ebSMike Smith     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
35913a31b7ebSMike Smith     cc->cdb.timeout = 0;	/* no timeout, we hope */
35923a31b7ebSMike Smith 
35933a31b7ebSMike Smith     cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
35943a31b7ebSMike Smith     bzero(cr->cr_data, CISS_NOTIFY_DATA_SIZE);
35953a31b7ebSMike Smith     cnc->opcode = CISS_OPCODE_READ;
35963a31b7ebSMike Smith     cnc->command = CISS_COMMAND_NOTIFY_ON_EVENT;
35973a31b7ebSMike Smith     cnc->timeout = 0;		/* no timeout, we hope */
35983a31b7ebSMike Smith     cnc->synchronous = 0;
35993a31b7ebSMike Smith     cnc->ordered = 0;
36003a31b7ebSMike Smith     cnc->seek_to_oldest = 0;
36012e80ca8aSPaul Saab     if ((sc->ciss_flags & CISS_FLAG_RUNNING) == 0)
36022e80ca8aSPaul Saab 	cnc->new_only = 1;
36032e80ca8aSPaul Saab     else
36043a31b7ebSMike Smith 	cnc->new_only = 0;
36053a31b7ebSMike Smith     cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
36063a31b7ebSMike Smith 
36073a31b7ebSMike Smith     /* submit the request */
36083a31b7ebSMike Smith     error = ciss_start(cr);
36093a31b7ebSMike Smith 
36103a31b7ebSMike Smith  out:
36113a31b7ebSMike Smith     if (error) {
36123a31b7ebSMike Smith 	if (cr != NULL) {
36133a31b7ebSMike Smith 	    if (cr->cr_data != NULL)
36143a31b7ebSMike Smith 		free(cr->cr_data, CISS_MALLOC_CLASS);
36153a31b7ebSMike Smith 	    ciss_release_request(cr);
36163a31b7ebSMike Smith 	}
36173a31b7ebSMike Smith 	sc->ciss_periodic_notify = NULL;
36183a31b7ebSMike Smith 	debug(0, "can't submit notify event request");
36193a31b7ebSMike Smith 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
36203a31b7ebSMike Smith     } else {
36213a31b7ebSMike Smith 	debug(1, "notify event submitted");
36223a31b7ebSMike Smith 	sc->ciss_flags |= CISS_FLAG_NOTIFY_OK;
36233a31b7ebSMike Smith     }
36243a31b7ebSMike Smith }
36253a31b7ebSMike Smith 
36263a31b7ebSMike Smith static void
36273a31b7ebSMike Smith ciss_notify_complete(struct ciss_request *cr)
36283a31b7ebSMike Smith {
36293a31b7ebSMike Smith     struct ciss_command	*cc;
36303a31b7ebSMike Smith     struct ciss_notify	*cn;
36313a31b7ebSMike Smith     struct ciss_softc	*sc;
36323a31b7ebSMike Smith     int			scsi_status;
36333a31b7ebSMike Smith     int			command_status;
36343a31b7ebSMike Smith     debug_called(1);
36353a31b7ebSMike Smith 
36362fdaa90dSScott Long     cc = cr->cr_cc;
36373a31b7ebSMike Smith     cn = (struct ciss_notify *)cr->cr_data;
36383a31b7ebSMike Smith     sc = cr->cr_sc;
36393a31b7ebSMike Smith 
36403a31b7ebSMike Smith     /*
36413a31b7ebSMike Smith      * Report request results, decode status.
36423a31b7ebSMike Smith      */
36433a31b7ebSMike Smith     ciss_report_request(cr, &command_status, &scsi_status);
36443a31b7ebSMike Smith 
36453a31b7ebSMike Smith     /*
36463a31b7ebSMike Smith      * Abort the chain on a fatal error.
36473a31b7ebSMike Smith      *
36483a31b7ebSMike Smith      * XXX which of these are actually errors?
36493a31b7ebSMike Smith      */
36503a31b7ebSMike Smith     if ((command_status != CISS_CMD_STATUS_SUCCESS) &&
36513a31b7ebSMike Smith 	(command_status != CISS_CMD_STATUS_TARGET_STATUS) &&
36523a31b7ebSMike Smith 	(command_status != CISS_CMD_STATUS_TIMEOUT)) {	/* XXX timeout? */
36533a31b7ebSMike Smith 	ciss_printf(sc, "fatal error in Notify Event request (%s)\n",
36543a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
36553a31b7ebSMike Smith 	ciss_release_request(cr);
36563a31b7ebSMike Smith 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
36573a31b7ebSMike Smith 	return;
36583a31b7ebSMike Smith     }
36593a31b7ebSMike Smith 
36603a31b7ebSMike Smith     /*
36613a31b7ebSMike Smith      * If the adapter gave us a text message, print it.
36623a31b7ebSMike Smith      */
36633a31b7ebSMike Smith     if (cn->message[0] != 0)
36643a31b7ebSMike Smith 	ciss_printf(sc, "*** %.80s\n", cn->message);
36653a31b7ebSMike Smith 
36663a31b7ebSMike Smith     debug(0, "notify event class %d subclass %d detail %d",
36673a31b7ebSMike Smith 		cn->class, cn->subclass, cn->detail);
36683a31b7ebSMike Smith 
36693a31b7ebSMike Smith     /*
36703a31b7ebSMike Smith      * If the response indicates that the notifier has been aborted,
36713a31b7ebSMike Smith      * release the notifier command.
36723a31b7ebSMike Smith      */
36733a31b7ebSMike Smith     if ((cn->class == CISS_NOTIFY_NOTIFIER) &&
36743a31b7ebSMike Smith 	(cn->subclass == CISS_NOTIFY_NOTIFIER_STATUS) &&
36753a31b7ebSMike Smith 	(cn->detail == 1)) {
36763a31b7ebSMike Smith 	debug(0, "notifier exiting");
36773a31b7ebSMike Smith 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
36783a31b7ebSMike Smith 	ciss_release_request(cr);
36793a31b7ebSMike Smith 	sc->ciss_periodic_notify = NULL;
36803a31b7ebSMike Smith 	wakeup(&sc->ciss_periodic_notify);
3681c6131460SPaul Saab     } else {
3682c6131460SPaul Saab 	/* Handle notify events in a kernel thread */
3683c6131460SPaul Saab 	ciss_enqueue_notify(cr);
3684c6131460SPaul Saab 	sc->ciss_periodic_notify = NULL;
3685c6131460SPaul Saab 	wakeup(&sc->ciss_periodic_notify);
3686c6131460SPaul Saab 	wakeup(&sc->ciss_notify);
36873a31b7ebSMike Smith     }
36883a31b7ebSMike Smith     /*
36893a31b7ebSMike Smith      * Send a new notify event command, if we're not aborting.
36903a31b7ebSMike Smith      */
36913a31b7ebSMike Smith     if (!(sc->ciss_flags & CISS_FLAG_ABORTING)) {
36923a31b7ebSMike Smith 	ciss_notify_event(sc);
36933a31b7ebSMike Smith     }
36943a31b7ebSMike Smith }
36953a31b7ebSMike Smith 
36963a31b7ebSMike Smith /************************************************************************
36973a31b7ebSMike Smith  * Abort the Notify Event chain.
36983a31b7ebSMike Smith  *
36993a31b7ebSMike Smith  * Note that we can't just abort the command in progress; we have to
37003a31b7ebSMike Smith  * explicitly issue an Abort Notify Event command in order for the
37013a31b7ebSMike Smith  * adapter to clean up correctly.
37023a31b7ebSMike Smith  *
37033a31b7ebSMike Smith  * If we are called with CISS_FLAG_ABORTING set in the adapter softc,
37043a31b7ebSMike Smith  * the chain will not restart itself.
37053a31b7ebSMike Smith  */
37063a31b7ebSMike Smith static int
37073a31b7ebSMike Smith ciss_notify_abort(struct ciss_softc *sc)
37083a31b7ebSMike Smith {
37093a31b7ebSMike Smith     struct ciss_request		*cr;
37103a31b7ebSMike Smith     struct ciss_command		*cc;
37113a31b7ebSMike Smith     struct ciss_notify_cdb	*cnc;
371222657ce1SScott Long     int				error, command_status, scsi_status;
37133a31b7ebSMike Smith 
37143a31b7ebSMike Smith     debug_called(1);
37153a31b7ebSMike Smith 
37163a31b7ebSMike Smith     cr = NULL;
37173a31b7ebSMike Smith     error = 0;
37183a31b7ebSMike Smith 
37193a31b7ebSMike Smith     /* verify that there's an outstanding command */
37203a31b7ebSMike Smith     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
37213a31b7ebSMike Smith 	goto out;
37223a31b7ebSMike Smith 
37233a31b7ebSMike Smith     /* get a command to issue the abort with */
37243a31b7ebSMike Smith     if ((error = ciss_get_request(sc, &cr)))
37253a31b7ebSMike Smith 	goto out;
37263a31b7ebSMike Smith 
37273a31b7ebSMike Smith     /* get a buffer for the result */
37283a31b7ebSMike Smith     if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
37293a31b7ebSMike Smith 	debug(0, "can't get notify event request buffer");
37303a31b7ebSMike Smith 	error = ENOMEM;
37313a31b7ebSMike Smith 	goto out;
37323a31b7ebSMike Smith     }
37333a31b7ebSMike Smith     cr->cr_length = CISS_NOTIFY_DATA_SIZE;
37343a31b7ebSMike Smith 
37353a31b7ebSMike Smith     /* build the CDB */
37362fdaa90dSScott Long     cc = cr->cr_cc;
37373a31b7ebSMike Smith     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
37383a31b7ebSMike Smith     cc->header.address.physical.bus = 0;
37393a31b7ebSMike Smith     cc->header.address.physical.target = 0;
37403a31b7ebSMike Smith     cc->cdb.cdb_length = sizeof(*cnc);
37413a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
37423a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
37433a31b7ebSMike Smith     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
37443a31b7ebSMike Smith     cc->cdb.timeout = 0;	/* no timeout, we hope */
37453a31b7ebSMike Smith 
37463a31b7ebSMike Smith     cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
37473a31b7ebSMike Smith     bzero(cnc, sizeof(*cnc));
37483a31b7ebSMike Smith     cnc->opcode = CISS_OPCODE_WRITE;
37493a31b7ebSMike Smith     cnc->command = CISS_COMMAND_ABORT_NOTIFY;
37503a31b7ebSMike Smith     cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
37513a31b7ebSMike Smith 
37523a31b7ebSMike Smith     ciss_print_request(cr);
37533a31b7ebSMike Smith 
37543a31b7ebSMike Smith     /*
37553a31b7ebSMike Smith      * Submit the request and wait for it to complete.
37563a31b7ebSMike Smith      */
37573a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
37583a31b7ebSMike Smith 	ciss_printf(sc, "Abort Notify Event command failed (%d)\n", error);
37593a31b7ebSMike Smith 	goto out;
37603a31b7ebSMike Smith     }
37613a31b7ebSMike Smith 
37623a31b7ebSMike Smith     /*
37633a31b7ebSMike Smith      * Check response.
37643a31b7ebSMike Smith      */
37653a31b7ebSMike Smith     ciss_report_request(cr, &command_status, &scsi_status);
37663a31b7ebSMike Smith     switch(command_status) {
37673a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:
37683a31b7ebSMike Smith 	break;
37693a31b7ebSMike Smith     case CISS_CMD_STATUS_INVALID_COMMAND:
37703a31b7ebSMike Smith 	/*
37713a31b7ebSMike Smith 	 * Some older adapters don't support the CISS version of this
37723a31b7ebSMike Smith 	 * command.  Fall back to using the BMIC version.
37733a31b7ebSMike Smith 	 */
37743a31b7ebSMike Smith 	error = ciss_notify_abort_bmic(sc);
37753a31b7ebSMike Smith 	if (error != 0)
37763a31b7ebSMike Smith 	    goto out;
37773a31b7ebSMike Smith 	break;
37783a31b7ebSMike Smith 
37793a31b7ebSMike Smith     case CISS_CMD_STATUS_TARGET_STATUS:
37803a31b7ebSMike Smith 	/*
37813a31b7ebSMike Smith 	 * This can happen if the adapter thinks there wasn't an outstanding
37823a31b7ebSMike Smith 	 * Notify Event command but we did.  We clean up here.
37833a31b7ebSMike Smith 	 */
37843a31b7ebSMike Smith 	if (scsi_status == CISS_SCSI_STATUS_CHECK_CONDITION) {
37853a31b7ebSMike Smith 	    if (sc->ciss_periodic_notify != NULL)
37863a31b7ebSMike Smith 		ciss_release_request(sc->ciss_periodic_notify);
37873a31b7ebSMike Smith 	    error = 0;
37883a31b7ebSMike Smith 	    goto out;
37893a31b7ebSMike Smith 	}
37903a31b7ebSMike Smith 	/* FALLTHROUGH */
37913a31b7ebSMike Smith 
37923a31b7ebSMike Smith     default:
37933a31b7ebSMike Smith 	ciss_printf(sc, "Abort Notify Event command failed (%s)\n",
37943a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
37953a31b7ebSMike Smith 	error = EIO;
37963a31b7ebSMike Smith 	goto out;
37973a31b7ebSMike Smith     }
37983a31b7ebSMike Smith 
37993a31b7ebSMike Smith     /*
38003a31b7ebSMike Smith      * Sleep waiting for the notifier command to complete.  Note
38013a31b7ebSMike Smith      * that if it doesn't, we may end up in a bad situation, since
38023a31b7ebSMike Smith      * the adapter may deliver it later.  Also note that the adapter
38033a31b7ebSMike Smith      * requires the Notify Event command to be cancelled in order to
38043a31b7ebSMike Smith      * maintain internal bookkeeping.
38053a31b7ebSMike Smith      */
38063a31b7ebSMike Smith     while (sc->ciss_periodic_notify != NULL) {
3807975b7318SScott Long 	error = msleep(&sc->ciss_periodic_notify, &sc->ciss_mtx, PRIBIO, "cissNEA", hz * 5);
38083a31b7ebSMike Smith 	if (error == EWOULDBLOCK) {
38093a31b7ebSMike Smith 	    ciss_printf(sc, "Notify Event command failed to abort, adapter may wedge.\n");
38103a31b7ebSMike Smith 	    break;
38113a31b7ebSMike Smith 	}
38123a31b7ebSMike Smith     }
38133a31b7ebSMike Smith 
38143a31b7ebSMike Smith  out:
38153a31b7ebSMike Smith     /* release the cancel request */
38163a31b7ebSMike Smith     if (cr != NULL) {
38173a31b7ebSMike Smith 	if (cr->cr_data != NULL)
38183a31b7ebSMike Smith 	    free(cr->cr_data, CISS_MALLOC_CLASS);
38193a31b7ebSMike Smith 	ciss_release_request(cr);
38203a31b7ebSMike Smith     }
38213a31b7ebSMike Smith     if (error == 0)
38223a31b7ebSMike Smith 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
38233a31b7ebSMike Smith     return(error);
38243a31b7ebSMike Smith }
38253a31b7ebSMike Smith 
38263a31b7ebSMike Smith /************************************************************************
38273a31b7ebSMike Smith  * Abort the Notify Event chain using a BMIC command.
38283a31b7ebSMike Smith  */
38293a31b7ebSMike Smith static int
38303a31b7ebSMike Smith ciss_notify_abort_bmic(struct ciss_softc *sc)
38313a31b7ebSMike Smith {
38323a31b7ebSMike Smith     struct ciss_request			*cr;
38333a31b7ebSMike Smith     int					error, command_status;
38343a31b7ebSMike Smith 
38353a31b7ebSMike Smith     debug_called(1);
38363a31b7ebSMike Smith 
38373a31b7ebSMike Smith     cr = NULL;
38383a31b7ebSMike Smith     error = 0;
38393a31b7ebSMike Smith 
38403a31b7ebSMike Smith     /* verify that there's an outstanding command */
38413a31b7ebSMike Smith     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
38423a31b7ebSMike Smith 	goto out;
38433a31b7ebSMike Smith 
38443a31b7ebSMike Smith     /*
38453a31b7ebSMike Smith      * Build a BMIC command to cancel the Notify on Event command.
38463a31b7ebSMike Smith      *
38473a31b7ebSMike Smith      * Note that we are sending a CISS opcode here.  Odd.
38483a31b7ebSMike Smith      */
38493a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_COMMAND_ABORT_NOTIFY,
38503a31b7ebSMike Smith 				       NULL, 0)) != 0)
38513a31b7ebSMike Smith 	goto out;
38523a31b7ebSMike Smith 
38533a31b7ebSMike Smith     /*
38543a31b7ebSMike Smith      * Submit the request and wait for it to complete.
38553a31b7ebSMike Smith      */
38563a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
38573a31b7ebSMike Smith 	ciss_printf(sc, "error sending BMIC Cancel Notify on Event command (%d)\n", error);
38583a31b7ebSMike Smith 	goto out;
38593a31b7ebSMike Smith     }
38603a31b7ebSMike Smith 
38613a31b7ebSMike Smith     /*
38623a31b7ebSMike Smith      * Check response.
38633a31b7ebSMike Smith      */
38643a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
38653a31b7ebSMike Smith     switch(command_status) {
38663a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:
38673a31b7ebSMike Smith 	break;
38683a31b7ebSMike Smith     default:
38693a31b7ebSMike Smith 	ciss_printf(sc, "error cancelling Notify on Event (%s)\n",
38703a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
38713a31b7ebSMike Smith 	error = EIO;
38723a31b7ebSMike Smith 	goto out;
38733a31b7ebSMike Smith     }
38743a31b7ebSMike Smith 
38753a31b7ebSMike Smith out:
38763a31b7ebSMike Smith     if (cr != NULL)
38773a31b7ebSMike Smith 	ciss_release_request(cr);
38783a31b7ebSMike Smith     return(error);
38793a31b7ebSMike Smith }
38803a31b7ebSMike Smith 
38813a31b7ebSMike Smith /************************************************************************
3882c6131460SPaul Saab  * Handle rescanning all the logical volumes when a notify event
3883c6131460SPaul Saab  * causes the drives to come online or offline.
3884c6131460SPaul Saab  */
3885c6131460SPaul Saab static void
3886c6131460SPaul Saab ciss_notify_rescan_logical(struct ciss_softc *sc)
3887c6131460SPaul Saab {
3888c6131460SPaul Saab     struct ciss_lun_report      *cll;
3889c6131460SPaul Saab     struct ciss_ldrive		*ld;
3890c6131460SPaul Saab     int                         i, j, ndrives;
3891c6131460SPaul Saab 
3892c6131460SPaul Saab     /*
3893c6131460SPaul Saab      * We must rescan all logical volumes to get the right logical
3894c6131460SPaul Saab      * drive address.
3895c6131460SPaul Saab      */
3896c6131460SPaul Saab     cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
3897e3edca22SSean Bruno                            sc->ciss_cfg->max_logical_supported);
3898c6131460SPaul Saab     if (cll == NULL)
3899c6131460SPaul Saab         return;
3900c6131460SPaul Saab 
3901c6131460SPaul Saab     ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
3902c6131460SPaul Saab 
3903c6131460SPaul Saab     /*
3904c6131460SPaul Saab      * Delete any of the drives which were destroyed by the
3905c6131460SPaul Saab      * firmware.
3906c6131460SPaul Saab      */
39071fe6c4eeSScott Long     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
3908e3edca22SSean Bruno 	for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
3909c6131460SPaul Saab 	    ld = &sc->ciss_logical[i][j];
3910c6131460SPaul Saab 
3911c6131460SPaul Saab 	    if (ld->cl_update == 0)
3912c6131460SPaul Saab 		continue;
3913c6131460SPaul Saab 
3914c6131460SPaul Saab 	    if (ld->cl_status != CISS_LD_ONLINE) {
3915c6131460SPaul Saab 		ciss_cam_rescan_target(sc, i, j);
3916c6131460SPaul Saab 		ld->cl_update = 0;
3917c6131460SPaul Saab 		if (ld->cl_ldrive)
3918c6131460SPaul Saab 		    free(ld->cl_ldrive, CISS_MALLOC_CLASS);
3919c6131460SPaul Saab 		if (ld->cl_lstatus)
3920c6131460SPaul Saab 		    free(ld->cl_lstatus, CISS_MALLOC_CLASS);
3921c6131460SPaul Saab 
3922c6131460SPaul Saab 		ld->cl_ldrive = NULL;
3923c6131460SPaul Saab 		ld->cl_lstatus = NULL;
3924c6131460SPaul Saab 	    }
3925c6131460SPaul Saab 	}
3926c6131460SPaul Saab     }
3927c6131460SPaul Saab 
3928c6131460SPaul Saab     /*
3929c6131460SPaul Saab      * Scan for new drives.
3930c6131460SPaul Saab      */
3931c6131460SPaul Saab     for (i = 0; i < ndrives; i++) {
3932c6131460SPaul Saab 	int	bus, target;
3933c6131460SPaul Saab 
3934c6131460SPaul Saab 	bus 	= CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
3935c6131460SPaul Saab 	target	= CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
3936c6131460SPaul Saab 	ld	= &sc->ciss_logical[bus][target];
3937c6131460SPaul Saab 
3938c6131460SPaul Saab 	if (ld->cl_update == 0)
3939c6131460SPaul Saab 		continue;
3940c6131460SPaul Saab 
39415cf5a430SPaul Saab 	ld->cl_update		= 0;
3942c6131460SPaul Saab 	ld->cl_address		= cll->lun[i];
3943c6131460SPaul Saab 	ld->cl_controller	= &sc->ciss_controllers[bus];
3944c6131460SPaul Saab 	if (ciss_identify_logical(sc, ld) == 0) {
3945c6131460SPaul Saab 	    ciss_cam_rescan_target(sc, bus, target);
3946c6131460SPaul Saab 	}
3947c6131460SPaul Saab     }
3948c6131460SPaul Saab     free(cll, CISS_MALLOC_CLASS);
3949c6131460SPaul Saab }
3950c6131460SPaul Saab 
3951c6131460SPaul Saab /************************************************************************
39523a31b7ebSMike Smith  * Handle a notify event relating to the status of a logical drive.
39533a31b7ebSMike Smith  *
39543a31b7ebSMike Smith  * XXX need to be able to defer some of these to properly handle
39553a31b7ebSMike Smith  *     calling the "ID Physical drive" command, unless the 'extended'
39563a31b7ebSMike Smith  *     drive IDs are always in BIG_MAP format.
39573a31b7ebSMike Smith  */
39583a31b7ebSMike Smith static void
39593a31b7ebSMike Smith ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn)
39603a31b7ebSMike Smith {
39613a31b7ebSMike Smith     struct ciss_ldrive	*ld;
3962c6131460SPaul Saab     int			ostatus, bus, target;
39633a31b7ebSMike Smith 
39643a31b7ebSMike Smith     debug_called(2);
39653a31b7ebSMike Smith 
3966c6131460SPaul Saab     bus		= cn->device.physical.bus;
3967c6131460SPaul Saab     target	= cn->data.logical_status.logical_drive;
3968c6131460SPaul Saab     ld		= &sc->ciss_logical[bus][target];
39693a31b7ebSMike Smith 
39703a31b7ebSMike Smith     switch (cn->subclass) {
39713a31b7ebSMike Smith     case CISS_NOTIFY_LOGICAL_STATUS:
39723a31b7ebSMike Smith 	switch (cn->detail) {
39733a31b7ebSMike Smith 	case 0:
3974c6131460SPaul Saab 	    ciss_name_device(sc, bus, target);
39753a31b7ebSMike Smith 	    ciss_printf(sc, "logical drive %d (%s) changed status %s->%s, spare status 0x%b\n",
39763a31b7ebSMike Smith 			cn->data.logical_status.logical_drive, ld->cl_name,
39773a31b7ebSMike Smith 			ciss_name_ldrive_status(cn->data.logical_status.previous_state),
39783a31b7ebSMike Smith 			ciss_name_ldrive_status(cn->data.logical_status.new_state),
39793a31b7ebSMike Smith 			cn->data.logical_status.spare_state,
39803a31b7ebSMike Smith 			"\20\1configured\2rebuilding\3failed\4in use\5available\n");
39813a31b7ebSMike Smith 
39823a31b7ebSMike Smith 	    /*
39833a31b7ebSMike Smith 	     * Update our idea of the drive's status.
39843a31b7ebSMike Smith 	     */
39853a31b7ebSMike Smith 	    ostatus = ciss_decode_ldrive_status(cn->data.logical_status.previous_state);
39863a31b7ebSMike Smith 	    ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
3987d4aa427fSPaul Saab 	    if (ld->cl_lstatus != NULL)
39883a31b7ebSMike Smith 		ld->cl_lstatus->status = cn->data.logical_status.new_state;
39893a31b7ebSMike Smith 
3990d4aa427fSPaul Saab 	    /*
3991d4aa427fSPaul Saab 	     * Have CAM rescan the drive if its status has changed.
3992d4aa427fSPaul Saab 	     */
3993c6131460SPaul Saab 	    if (ostatus != ld->cl_status) {
3994c6131460SPaul Saab 		ld->cl_update = 1;
3995c6131460SPaul Saab 		ciss_notify_rescan_logical(sc);
3996c6131460SPaul Saab 	    }
3997d4aa427fSPaul Saab 
39983a31b7ebSMike Smith 	    break;
39993a31b7ebSMike Smith 
40003a31b7ebSMike Smith 	case 1:	/* logical drive has recognised new media, needs Accept Media Exchange */
4001c6131460SPaul Saab 	    ciss_name_device(sc, bus, target);
40023a31b7ebSMike Smith 	    ciss_printf(sc, "logical drive %d (%s) media exchanged, ready to go online\n",
40033a31b7ebSMike Smith 			cn->data.logical_status.logical_drive, ld->cl_name);
400488c78a38SPaul Saab 	    ciss_accept_media(sc, ld);
4005139233cbSPaul Saab 
4006139233cbSPaul Saab 	    ld->cl_update = 1;
4007139233cbSPaul Saab 	    ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
4008139233cbSPaul Saab 	    ciss_notify_rescan_logical(sc);
40093a31b7ebSMike Smith 	    break;
40103a31b7ebSMike Smith 
40113a31b7ebSMike Smith 	case 2:
40123a31b7ebSMike Smith 	case 3:
40133a31b7ebSMike Smith 	    ciss_printf(sc, "rebuild of logical drive %d (%s) failed due to %s error\n",
40143a31b7ebSMike Smith 			cn->data.rebuild_aborted.logical_drive,
4015c6131460SPaul Saab 			ld->cl_name,
40163a31b7ebSMike Smith 			(cn->detail == 2) ? "read" : "write");
40173a31b7ebSMike Smith 	    break;
40183a31b7ebSMike Smith 	}
40193a31b7ebSMike Smith 	break;
40203a31b7ebSMike Smith 
40213a31b7ebSMike Smith     case CISS_NOTIFY_LOGICAL_ERROR:
40223a31b7ebSMike Smith 	if (cn->detail == 0) {
40233a31b7ebSMike Smith 	    ciss_printf(sc, "FATAL I/O ERROR on logical drive %d (%s), SCSI port %d ID %d\n",
40243a31b7ebSMike Smith 			cn->data.io_error.logical_drive,
4025c6131460SPaul Saab 			ld->cl_name,
40263a31b7ebSMike Smith 			cn->data.io_error.failure_bus,
40273a31b7ebSMike Smith 			cn->data.io_error.failure_drive);
40283a31b7ebSMike Smith 	    /* XXX should we take the drive down at this point, or will we be told? */
40293a31b7ebSMike Smith 	}
40303a31b7ebSMike Smith 	break;
40313a31b7ebSMike Smith 
40323a31b7ebSMike Smith     case CISS_NOTIFY_LOGICAL_SURFACE:
40333a31b7ebSMike Smith 	if (cn->detail == 0)
40343a31b7ebSMike Smith 	    ciss_printf(sc, "logical drive %d (%s) completed consistency initialisation\n",
40353a31b7ebSMike Smith 			cn->data.consistency_completed.logical_drive,
4036c6131460SPaul Saab 			ld->cl_name);
40373a31b7ebSMike Smith 	break;
40383a31b7ebSMike Smith     }
40393a31b7ebSMike Smith }
40403a31b7ebSMike Smith 
40413a31b7ebSMike Smith /************************************************************************
40423a31b7ebSMike Smith  * Handle a notify event relating to the status of a physical drive.
40433a31b7ebSMike Smith  */
40443a31b7ebSMike Smith static void
40453a31b7ebSMike Smith ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn)
40463a31b7ebSMike Smith {
4047c6131460SPaul Saab }
40483a31b7ebSMike Smith 
4049c6131460SPaul Saab /************************************************************************
40501fe6c4eeSScott Long  * Handle a notify event relating to the status of a physical drive.
40511fe6c4eeSScott Long  */
40521fe6c4eeSScott Long static void
40531fe6c4eeSScott Long ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn)
40541fe6c4eeSScott Long {
4055c1885ab8SPaul Saab     struct ciss_lun_report *cll = NULL;
40561fe6c4eeSScott Long     int bus, target;
40571fe6c4eeSScott Long 
40581fe6c4eeSScott Long     switch (cn->subclass) {
40591fe6c4eeSScott Long     case CISS_NOTIFY_HOTPLUG_PHYSICAL:
40601fe6c4eeSScott Long     case CISS_NOTIFY_HOTPLUG_NONDISK:
40611fe6c4eeSScott Long 	bus = CISS_BIG_MAP_BUS(sc, cn->data.drive.big_physical_drive_number);
40621fe6c4eeSScott Long 	target =
40631fe6c4eeSScott Long 	    CISS_BIG_MAP_TARGET(sc, cn->data.drive.big_physical_drive_number);
40641fe6c4eeSScott Long 
40651fe6c4eeSScott Long 	if (cn->detail == 0) {
40661fe6c4eeSScott Long 	    /*
40671fe6c4eeSScott Long 	     * Mark the device offline so that it'll start producing selection
40681fe6c4eeSScott Long 	     * timeouts to the upper layer.
40691fe6c4eeSScott Long 	     */
40705f265711SScott Long 	    if ((bus >= 0) && (target >= 0))
40711fe6c4eeSScott Long 		sc->ciss_physical[bus][target].cp_online = 0;
40721fe6c4eeSScott Long 	} else {
40731fe6c4eeSScott Long 	    /*
40741fe6c4eeSScott Long 	     * Rescan the physical lun list for new items
40751fe6c4eeSScott Long 	     */
40761fe6c4eeSScott Long 	    cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
4077e3edca22SSean Bruno 				   sc->ciss_cfg->max_physical_supported);
40781fe6c4eeSScott Long 	    if (cll == NULL) {
40791fe6c4eeSScott Long 		ciss_printf(sc, "Warning, cannot get physical lun list\n");
40801fe6c4eeSScott Long 		break;
40811fe6c4eeSScott Long 	    }
40821fe6c4eeSScott Long 	    ciss_filter_physical(sc, cll);
40831fe6c4eeSScott Long 	}
40841fe6c4eeSScott Long 	break;
40851fe6c4eeSScott Long 
40861fe6c4eeSScott Long     default:
40871fe6c4eeSScott Long 	ciss_printf(sc, "Unknown hotplug event %d\n", cn->subclass);
40881fe6c4eeSScott Long 	return;
40891fe6c4eeSScott Long     }
4090c1885ab8SPaul Saab 
4091c1885ab8SPaul Saab     if (cll != NULL)
4092c1885ab8SPaul Saab 	free(cll, CISS_MALLOC_CLASS);
40931fe6c4eeSScott Long }
40941fe6c4eeSScott Long 
40951fe6c4eeSScott Long /************************************************************************
4096c6131460SPaul Saab  * Handle deferred processing of notify events.  Notify events may need
4097c6131460SPaul Saab  * sleep which is unsafe during an interrupt.
4098c6131460SPaul Saab  */
4099c6131460SPaul Saab static void
4100c6131460SPaul Saab ciss_notify_thread(void *arg)
4101c6131460SPaul Saab {
4102c6131460SPaul Saab     struct ciss_softc		*sc;
4103c6131460SPaul Saab     struct ciss_request		*cr;
4104c6131460SPaul Saab     struct ciss_notify		*cn;
4105c6131460SPaul Saab 
4106c6131460SPaul Saab     sc = (struct ciss_softc *)arg;
4107975b7318SScott Long #if __FreeBSD_version >= 500000
4108975b7318SScott Long     mtx_lock(&sc->ciss_mtx);
4109975b7318SScott Long #endif
4110c6131460SPaul Saab 
4111c6131460SPaul Saab     for (;;) {
411222657ce1SScott Long 	if (STAILQ_EMPTY(&sc->ciss_notify) != 0 &&
4113c6131460SPaul Saab 	    (sc->ciss_flags & CISS_FLAG_THREAD_SHUT) == 0) {
4114975b7318SScott Long 	    msleep(&sc->ciss_notify, &sc->ciss_mtx, PUSER, "idle", 0);
4115c6131460SPaul Saab 	}
4116c6131460SPaul Saab 
4117c6131460SPaul Saab 	if (sc->ciss_flags & CISS_FLAG_THREAD_SHUT)
4118c6131460SPaul Saab 	    break;
4119c6131460SPaul Saab 
4120c6131460SPaul Saab 	cr = ciss_dequeue_notify(sc);
4121c6131460SPaul Saab 
4122c6131460SPaul Saab 	if (cr == NULL)
4123c6131460SPaul Saab 		panic("cr null");
4124c6131460SPaul Saab 	cn = (struct ciss_notify *)cr->cr_data;
4125c6131460SPaul Saab 
4126c6131460SPaul Saab 	switch (cn->class) {
41271fe6c4eeSScott Long 	case CISS_NOTIFY_HOTPLUG:
41281fe6c4eeSScott Long 	    ciss_notify_hotplug(sc, cn);
41291fe6c4eeSScott Long 	    break;
4130c6131460SPaul Saab 	case CISS_NOTIFY_LOGICAL:
4131c6131460SPaul Saab 	    ciss_notify_logical(sc, cn);
4132c6131460SPaul Saab 	    break;
4133c6131460SPaul Saab 	case CISS_NOTIFY_PHYSICAL:
4134c6131460SPaul Saab 	    ciss_notify_physical(sc, cn);
4135c6131460SPaul Saab 	    break;
4136c6131460SPaul Saab 	}
4137c6131460SPaul Saab 
4138c6131460SPaul Saab 	ciss_release_request(cr);
4139c6131460SPaul Saab 
4140c6131460SPaul Saab     }
4141c6131460SPaul Saab     sc->ciss_notify_thread = NULL;
4142c6131460SPaul Saab     wakeup(&sc->ciss_notify_thread);
4143c6131460SPaul Saab 
4144c6131460SPaul Saab #if __FreeBSD_version >= 500000
4145975b7318SScott Long     mtx_unlock(&sc->ciss_mtx);
4146c6131460SPaul Saab #endif
41473745c395SJulian Elischer     kproc_exit(0);
4148c6131460SPaul Saab }
4149c6131460SPaul Saab 
4150c6131460SPaul Saab /************************************************************************
4151c6131460SPaul Saab  * Start the notification kernel thread.
4152c6131460SPaul Saab  */
4153c6131460SPaul Saab static void
4154c6131460SPaul Saab ciss_spawn_notify_thread(struct ciss_softc *sc)
4155c6131460SPaul Saab {
4156c6131460SPaul Saab 
415778d03361SPaul Saab #if __FreeBSD_version > 500005
41583745c395SJulian Elischer     if (kproc_create((void(*)(void *))ciss_notify_thread, sc,
4159c6131460SPaul Saab 		       &sc->ciss_notify_thread, 0, 0, "ciss_notify%d",
4160c6131460SPaul Saab 		       device_get_unit(sc->ciss_dev)))
416178d03361SPaul Saab #else
41623745c395SJulian Elischer     if (kproc_create((void(*)(void *))ciss_notify_thread, sc,
416378d03361SPaul Saab 		       &sc->ciss_notify_thread, "ciss_notify%d",
416478d03361SPaul Saab 		       device_get_unit(sc->ciss_dev)))
416578d03361SPaul Saab #endif
4166c6131460SPaul Saab 	panic("Could not create notify thread\n");
4167c6131460SPaul Saab }
4168c6131460SPaul Saab 
4169c6131460SPaul Saab /************************************************************************
4170c6131460SPaul Saab  * Kill the notification kernel thread.
4171c6131460SPaul Saab  */
4172c6131460SPaul Saab static void
4173c6131460SPaul Saab ciss_kill_notify_thread(struct ciss_softc *sc)
4174c6131460SPaul Saab {
4175c6131460SPaul Saab 
4176c6131460SPaul Saab     if (sc->ciss_notify_thread == NULL)
4177c6131460SPaul Saab 	return;
4178c6131460SPaul Saab 
4179c6131460SPaul Saab     sc->ciss_flags |= CISS_FLAG_THREAD_SHUT;
4180c6131460SPaul Saab     wakeup(&sc->ciss_notify);
4181975b7318SScott Long     msleep(&sc->ciss_notify_thread, &sc->ciss_mtx, PUSER, "thtrm", 0);
41823a31b7ebSMike Smith }
41833a31b7ebSMike Smith 
41843a31b7ebSMike Smith /************************************************************************
41853a31b7ebSMike Smith  * Print a request.
41863a31b7ebSMike Smith  */
41873a31b7ebSMike Smith static void
41883a31b7ebSMike Smith ciss_print_request(struct ciss_request *cr)
41893a31b7ebSMike Smith {
41903a31b7ebSMike Smith     struct ciss_softc	*sc;
41913a31b7ebSMike Smith     struct ciss_command	*cc;
41923a31b7ebSMike Smith     int			i;
41933a31b7ebSMike Smith 
41943a31b7ebSMike Smith     sc = cr->cr_sc;
41952fdaa90dSScott Long     cc = cr->cr_cc;
41963a31b7ebSMike Smith 
41973a31b7ebSMike Smith     ciss_printf(sc, "REQUEST @ %p\n", cr);
41983a31b7ebSMike Smith     ciss_printf(sc, "  data %p/%d  tag %d  flags %b\n",
41993a31b7ebSMike Smith 	      cr->cr_data, cr->cr_length, cr->cr_tag, cr->cr_flags,
42003a31b7ebSMike Smith 	      "\20\1mapped\2sleep\3poll\4dataout\5datain\n");
42013a31b7ebSMike Smith     ciss_printf(sc, "  sg list/total %d/%d  host tag 0x%x\n",
42023a31b7ebSMike Smith 		cc->header.sg_in_list, cc->header.sg_total, cc->header.host_tag);
42033a31b7ebSMike Smith     switch(cc->header.address.mode.mode) {
42043a31b7ebSMike Smith     case CISS_HDR_ADDRESS_MODE_PERIPHERAL:
42053a31b7ebSMike Smith     case CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL:
42063a31b7ebSMike Smith 	ciss_printf(sc, "  physical bus %d target %d\n",
42073a31b7ebSMike Smith 		    cc->header.address.physical.bus, cc->header.address.physical.target);
42083a31b7ebSMike Smith 	break;
42093a31b7ebSMike Smith     case CISS_HDR_ADDRESS_MODE_LOGICAL:
42103a31b7ebSMike Smith 	ciss_printf(sc, "  logical unit %d\n", cc->header.address.logical.lun);
42113a31b7ebSMike Smith 	break;
42123a31b7ebSMike Smith     }
42133a31b7ebSMike Smith     ciss_printf(sc, "  %s cdb length %d type %s attribute %s\n",
42143a31b7ebSMike Smith 		(cc->cdb.direction == CISS_CDB_DIRECTION_NONE) ? "no-I/O" :
42153a31b7ebSMike Smith 		(cc->cdb.direction == CISS_CDB_DIRECTION_READ) ? "READ" :
42163a31b7ebSMike Smith 		(cc->cdb.direction == CISS_CDB_DIRECTION_WRITE) ? "WRITE" : "??",
42173a31b7ebSMike Smith 		cc->cdb.cdb_length,
42183a31b7ebSMike Smith 		(cc->cdb.type == CISS_CDB_TYPE_COMMAND) ? "command" :
42193a31b7ebSMike Smith 		(cc->cdb.type == CISS_CDB_TYPE_MESSAGE) ? "message" : "??",
42203a31b7ebSMike Smith 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_UNTAGGED) ? "untagged" :
42213a31b7ebSMike Smith 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_SIMPLE) ? "simple" :
42223a31b7ebSMike Smith 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_HEAD_OF_QUEUE) ? "head-of-queue" :
42233a31b7ebSMike Smith 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_ORDERED) ? "ordered" :
42243a31b7ebSMike Smith 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_AUTO_CONTINGENT) ? "auto-contingent" : "??");
42253a31b7ebSMike Smith     ciss_printf(sc, "  %*D\n", cc->cdb.cdb_length, &cc->cdb.cdb[0], " ");
42263a31b7ebSMike Smith 
42273a31b7ebSMike Smith     if (cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) {
42283a31b7ebSMike Smith 	/* XXX print error info */
42293a31b7ebSMike Smith     } else {
42303a31b7ebSMike Smith 	/* since we don't use chained s/g, don't support it here */
42313a31b7ebSMike Smith 	for (i = 0; i < cc->header.sg_in_list; i++) {
42323a31b7ebSMike Smith 	    if ((i % 4) == 0)
42333a31b7ebSMike Smith 		ciss_printf(sc, "   ");
42343a31b7ebSMike Smith 	    printf("0x%08x/%d ", (u_int32_t)cc->sg[i].address, cc->sg[i].length);
42353a31b7ebSMike Smith 	    if ((((i + 1) % 4) == 0) || (i == (cc->header.sg_in_list - 1)))
42363a31b7ebSMike Smith 		printf("\n");
42373a31b7ebSMike Smith 	}
42383a31b7ebSMike Smith     }
42393a31b7ebSMike Smith }
42403a31b7ebSMike Smith 
42413a31b7ebSMike Smith /************************************************************************
42423a31b7ebSMike Smith  * Print information about the status of a logical drive.
42433a31b7ebSMike Smith  */
42443a31b7ebSMike Smith static void
42453a31b7ebSMike Smith ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld)
42463a31b7ebSMike Smith {
42473a31b7ebSMike Smith     int		bus, target, i;
42483a31b7ebSMike Smith 
4249d4aa427fSPaul Saab     if (ld->cl_lstatus == NULL) {
4250d4aa427fSPaul Saab 	printf("does not exist\n");
4251d4aa427fSPaul Saab 	return;
4252d4aa427fSPaul Saab     }
4253d4aa427fSPaul Saab 
42543a31b7ebSMike Smith     /* print drive status */
42553a31b7ebSMike Smith     switch(ld->cl_lstatus->status) {
42563a31b7ebSMike Smith     case CISS_LSTATUS_OK:
42573a31b7ebSMike Smith 	printf("online\n");
42583a31b7ebSMike Smith 	break;
42593a31b7ebSMike Smith     case CISS_LSTATUS_INTERIM_RECOVERY:
42603a31b7ebSMike Smith 	printf("in interim recovery mode\n");
42613a31b7ebSMike Smith 	break;
42623a31b7ebSMike Smith     case CISS_LSTATUS_READY_RECOVERY:
42633a31b7ebSMike Smith 	printf("ready to begin recovery\n");
42643a31b7ebSMike Smith 	break;
42653a31b7ebSMike Smith     case CISS_LSTATUS_RECOVERING:
42663a31b7ebSMike Smith 	bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
42673a31b7ebSMike Smith 	target = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
42683a31b7ebSMike Smith 	printf("being recovered, working on physical drive %d.%d, %u blocks remaining\n",
42693a31b7ebSMike Smith 	       bus, target, ld->cl_lstatus->blocks_to_recover);
42703a31b7ebSMike Smith 	break;
42713a31b7ebSMike Smith     case CISS_LSTATUS_EXPANDING:
42723a31b7ebSMike Smith 	printf("being expanded, %u blocks remaining\n",
42733a31b7ebSMike Smith 	       ld->cl_lstatus->blocks_to_recover);
42743a31b7ebSMike Smith 	break;
42753a31b7ebSMike Smith     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
42763a31b7ebSMike Smith 	printf("queued for expansion\n");
42773a31b7ebSMike Smith 	break;
42783a31b7ebSMike Smith     case CISS_LSTATUS_FAILED:
42793a31b7ebSMike Smith 	printf("queued for expansion\n");
42803a31b7ebSMike Smith 	break;
42813a31b7ebSMike Smith     case CISS_LSTATUS_WRONG_PDRIVE:
42823a31b7ebSMike Smith 	printf("wrong physical drive inserted\n");
42833a31b7ebSMike Smith 	break;
42843a31b7ebSMike Smith     case CISS_LSTATUS_MISSING_PDRIVE:
42853a31b7ebSMike Smith 	printf("missing a needed physical drive\n");
42863a31b7ebSMike Smith 	break;
42873a31b7ebSMike Smith     case CISS_LSTATUS_BECOMING_READY:
42883a31b7ebSMike Smith 	printf("becoming ready\n");
42893a31b7ebSMike Smith 	break;
42903a31b7ebSMike Smith     }
42913a31b7ebSMike Smith 
4292d4aa427fSPaul Saab     /* print failed physical drives */
42933a31b7ebSMike Smith     for (i = 0; i < CISS_BIG_MAP_ENTRIES / 8; i++) {
42943a31b7ebSMike Smith 	bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_failure_map[i]);
42953a31b7ebSMike Smith 	target = CISS_BIG_MAP_TARGET(sc, ld->cl_lstatus->drive_failure_map[i]);
42963a31b7ebSMike Smith 	if (bus == -1)
42973a31b7ebSMike Smith 	    continue;
42983a31b7ebSMike Smith 	ciss_printf(sc, "physical drive %d:%d (%x) failed\n", bus, target,
42993a31b7ebSMike Smith 		    ld->cl_lstatus->drive_failure_map[i]);
43003a31b7ebSMike Smith     }
43013a31b7ebSMike Smith }
43023a31b7ebSMike Smith 
4303d4aa427fSPaul Saab #ifdef CISS_DEBUG
4304*b27556e0SSean Bruno #include "opt_ddb.h"
4305*b27556e0SSean Bruno #ifdef DDB
4306*b27556e0SSean Bruno #include <ddb/ddb.h>
4307d4aa427fSPaul Saab /************************************************************************
4308d4aa427fSPaul Saab  * Print information about the controller/driver.
4309d4aa427fSPaul Saab  */
4310d4aa427fSPaul Saab static void
4311d4aa427fSPaul Saab ciss_print_adapter(struct ciss_softc *sc)
4312d4aa427fSPaul Saab {
4313609caf8dSPaul Saab     int		i, j;
4314d4aa427fSPaul Saab 
4315d4aa427fSPaul Saab     ciss_printf(sc, "ADAPTER:\n");
4316d4aa427fSPaul Saab     for (i = 0; i < CISSQ_COUNT; i++) {
4317d4aa427fSPaul Saab 	ciss_printf(sc, "%s     %d/%d\n",
4318d4aa427fSPaul Saab 	    i == 0 ? "free" :
4319d4aa427fSPaul Saab 	    i == 1 ? "busy" : "complete",
4320d4aa427fSPaul Saab 	    sc->ciss_qstat[i].q_length,
4321d4aa427fSPaul Saab 	    sc->ciss_qstat[i].q_max);
4322d4aa427fSPaul Saab     }
4323d4aa427fSPaul Saab     ciss_printf(sc, "max_requests %d\n", sc->ciss_max_requests);
4324d4aa427fSPaul Saab     ciss_printf(sc, "flags %b\n", sc->ciss_flags,
4325d4aa427fSPaul Saab 	"\20\1notify_ok\2control_open\3aborting\4running\21fake_synch\22bmic_abort\n");
4326d4aa427fSPaul Saab 
43271fe6c4eeSScott Long     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
4328e3edca22SSean Bruno 	for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
4329d4aa427fSPaul Saab 	    ciss_printf(sc, "LOGICAL DRIVE %d:  ", i);
4330609caf8dSPaul Saab 	    ciss_print_ldrive(sc, &sc->ciss_logical[i][j]);
4331609caf8dSPaul Saab 	}
4332d4aa427fSPaul Saab     }
4333d4aa427fSPaul Saab 
43341fe6c4eeSScott Long     /* XXX Should physical drives be printed out here? */
43351fe6c4eeSScott Long 
4336d4aa427fSPaul Saab     for (i = 1; i < sc->ciss_max_requests; i++)
4337d4aa427fSPaul Saab 	ciss_print_request(sc->ciss_request + i);
4338d4aa427fSPaul Saab }
4339d4aa427fSPaul Saab 
4340d4aa427fSPaul Saab /* DDB hook */
4341*b27556e0SSean Bruno DB_COMMAND(ciss_prt, db_ciss_prt)
4342d4aa427fSPaul Saab {
4343d4aa427fSPaul Saab     struct ciss_softc	*sc;
4344d4aa427fSPaul Saab 
4345d4aa427fSPaul Saab     sc = devclass_get_softc(devclass_find("ciss"), 0);
4346d4aa427fSPaul Saab     if (sc == NULL) {
4347d4aa427fSPaul Saab 	printf("no ciss controllers\n");
4348d4aa427fSPaul Saab     } else {
4349d4aa427fSPaul Saab 	ciss_print_adapter(sc);
4350d4aa427fSPaul Saab     }
4351d4aa427fSPaul Saab }
4352d4aa427fSPaul Saab #endif
4353*b27556e0SSean Bruno #endif
4354d4aa427fSPaul Saab 
43553a31b7ebSMike Smith /************************************************************************
43563a31b7ebSMike Smith  * Return a name for a logical drive status value.
43573a31b7ebSMike Smith  */
43583a31b7ebSMike Smith static const char *
43593a31b7ebSMike Smith ciss_name_ldrive_status(int status)
43603a31b7ebSMike Smith {
43613a31b7ebSMike Smith     switch (status) {
43623a31b7ebSMike Smith     case CISS_LSTATUS_OK:
43633a31b7ebSMike Smith 	return("OK");
43643a31b7ebSMike Smith     case CISS_LSTATUS_FAILED:
43653a31b7ebSMike Smith 	return("failed");
43663a31b7ebSMike Smith     case CISS_LSTATUS_NOT_CONFIGURED:
43673a31b7ebSMike Smith 	return("not configured");
43683a31b7ebSMike Smith     case CISS_LSTATUS_INTERIM_RECOVERY:
43693a31b7ebSMike Smith 	return("interim recovery");
43703a31b7ebSMike Smith     case CISS_LSTATUS_READY_RECOVERY:
43713a31b7ebSMike Smith 	return("ready for recovery");
43723a31b7ebSMike Smith     case CISS_LSTATUS_RECOVERING:
43733a31b7ebSMike Smith 	return("recovering");
43743a31b7ebSMike Smith     case CISS_LSTATUS_WRONG_PDRIVE:
43753a31b7ebSMike Smith 	return("wrong physical drive inserted");
43763a31b7ebSMike Smith     case CISS_LSTATUS_MISSING_PDRIVE:
43773a31b7ebSMike Smith 	return("missing physical drive");
43783a31b7ebSMike Smith     case CISS_LSTATUS_EXPANDING:
43793a31b7ebSMike Smith 	return("expanding");
43803a31b7ebSMike Smith     case CISS_LSTATUS_BECOMING_READY:
43813a31b7ebSMike Smith 	return("becoming ready");
43823a31b7ebSMike Smith     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
43833a31b7ebSMike Smith 	return("queued for expansion");
43843a31b7ebSMike Smith     }
43853a31b7ebSMike Smith     return("unknown status");
43863a31b7ebSMike Smith }
43873a31b7ebSMike Smith 
43883a31b7ebSMike Smith /************************************************************************
43893a31b7ebSMike Smith  * Return an online/offline/nonexistent value for a logical drive
43903a31b7ebSMike Smith  * status value.
43913a31b7ebSMike Smith  */
43923a31b7ebSMike Smith static int
43933a31b7ebSMike Smith ciss_decode_ldrive_status(int status)
43943a31b7ebSMike Smith {
43953a31b7ebSMike Smith     switch(status) {
43963a31b7ebSMike Smith     case CISS_LSTATUS_NOT_CONFIGURED:
43973a31b7ebSMike Smith 	return(CISS_LD_NONEXISTENT);
43983a31b7ebSMike Smith 
43993a31b7ebSMike Smith     case CISS_LSTATUS_OK:
44003a31b7ebSMike Smith     case CISS_LSTATUS_INTERIM_RECOVERY:
44013a31b7ebSMike Smith     case CISS_LSTATUS_READY_RECOVERY:
44023a31b7ebSMike Smith     case CISS_LSTATUS_RECOVERING:
44033a31b7ebSMike Smith     case CISS_LSTATUS_EXPANDING:
44043a31b7ebSMike Smith     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
44053a31b7ebSMike Smith 	return(CISS_LD_ONLINE);
44063a31b7ebSMike Smith 
44073a31b7ebSMike Smith     case CISS_LSTATUS_FAILED:
44083a31b7ebSMike Smith     case CISS_LSTATUS_WRONG_PDRIVE:
44093a31b7ebSMike Smith     case CISS_LSTATUS_MISSING_PDRIVE:
44103a31b7ebSMike Smith     case CISS_LSTATUS_BECOMING_READY:
44113a31b7ebSMike Smith     default:
44123a31b7ebSMike Smith 	return(CISS_LD_OFFLINE);
44133a31b7ebSMike Smith     }
44143a31b7ebSMike Smith }
44153a31b7ebSMike Smith 
44163a31b7ebSMike Smith 
44173a31b7ebSMike Smith /************************************************************************
44183a31b7ebSMike Smith  * Return a name for a logical drive's organisation.
44193a31b7ebSMike Smith  */
44203a31b7ebSMike Smith static const char *
44213a31b7ebSMike Smith ciss_name_ldrive_org(int org)
44223a31b7ebSMike Smith {
44233a31b7ebSMike Smith     switch(org) {
44243a31b7ebSMike Smith     case CISS_LDRIVE_RAID0:
44253a31b7ebSMike Smith 	return("RAID 0");
44263a31b7ebSMike Smith     case CISS_LDRIVE_RAID1:
4427d000b49dSKonstantin Belousov 	return("RAID 1(1+0)");
44283a31b7ebSMike Smith     case CISS_LDRIVE_RAID4:
44293a31b7ebSMike Smith 	return("RAID 4");
44303a31b7ebSMike Smith     case CISS_LDRIVE_RAID5:
44313a31b7ebSMike Smith 	return("RAID 5");
4432aaf8327bSPaul Saab     case CISS_LDRIVE_RAID51:
4433aaf8327bSPaul Saab 	return("RAID 5+1");
4434aaf8327bSPaul Saab     case CISS_LDRIVE_RAIDADG:
4435aaf8327bSPaul Saab 	return("RAID ADG");
44363a31b7ebSMike Smith     }
44373a31b7ebSMike Smith     return("unkown");
44383a31b7ebSMike Smith }
44393a31b7ebSMike Smith 
44403a31b7ebSMike Smith /************************************************************************
44413a31b7ebSMike Smith  * Return a name for a command status value.
44423a31b7ebSMike Smith  */
44433a31b7ebSMike Smith static const char *
44443a31b7ebSMike Smith ciss_name_command_status(int status)
44453a31b7ebSMike Smith {
44463a31b7ebSMike Smith     switch(status) {
44473a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:
44483a31b7ebSMike Smith 	return("success");
44493a31b7ebSMike Smith     case CISS_CMD_STATUS_TARGET_STATUS:
44503a31b7ebSMike Smith 	return("target status");
44513a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_UNDERRUN:
44523a31b7ebSMike Smith 	return("data underrun");
44533a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_OVERRUN:
44543a31b7ebSMike Smith 	return("data overrun");
44553a31b7ebSMike Smith     case CISS_CMD_STATUS_INVALID_COMMAND:
44563a31b7ebSMike Smith 	return("invalid command");
44573a31b7ebSMike Smith     case CISS_CMD_STATUS_PROTOCOL_ERROR:
44583a31b7ebSMike Smith 	return("protocol error");
44593a31b7ebSMike Smith     case CISS_CMD_STATUS_HARDWARE_ERROR:
44603a31b7ebSMike Smith 	return("hardware error");
44613a31b7ebSMike Smith     case CISS_CMD_STATUS_CONNECTION_LOST:
44623a31b7ebSMike Smith 	return("connection lost");
44633a31b7ebSMike Smith     case CISS_CMD_STATUS_ABORTED:
44643a31b7ebSMike Smith 	return("aborted");
44653a31b7ebSMike Smith     case CISS_CMD_STATUS_ABORT_FAILED:
44663a31b7ebSMike Smith 	return("abort failed");
44673a31b7ebSMike Smith     case CISS_CMD_STATUS_UNSOLICITED_ABORT:
44683a31b7ebSMike Smith 	return("unsolicited abort");
44693a31b7ebSMike Smith     case CISS_CMD_STATUS_TIMEOUT:
44703a31b7ebSMike Smith 	return("timeout");
44713a31b7ebSMike Smith     case CISS_CMD_STATUS_UNABORTABLE:
44723a31b7ebSMike Smith 	return("unabortable");
44733a31b7ebSMike Smith     }
44743a31b7ebSMike Smith     return("unknown status");
44753a31b7ebSMike Smith }
44763a31b7ebSMike Smith 
44773a31b7ebSMike Smith /************************************************************************
44783a31b7ebSMike Smith  * Handle an open on the control device.
44793a31b7ebSMike Smith  */
44803a31b7ebSMike Smith static int
448100b4e54aSWarner Losh ciss_open(struct cdev *dev, int flags, int fmt, struct thread *p)
44823a31b7ebSMike Smith {
44833a31b7ebSMike Smith     struct ciss_softc	*sc;
44843a31b7ebSMike Smith 
44853a31b7ebSMike Smith     debug_called(1);
44863a31b7ebSMike Smith 
44873a31b7ebSMike Smith     sc = (struct ciss_softc *)dev->si_drv1;
44883a31b7ebSMike Smith 
44893a31b7ebSMike Smith     /* we might want to veto if someone already has us open */
44903a31b7ebSMike Smith 
4491975b7318SScott Long     mtx_lock(&sc->ciss_mtx);
44923a31b7ebSMike Smith     sc->ciss_flags |= CISS_FLAG_CONTROL_OPEN;
4493975b7318SScott Long     mtx_unlock(&sc->ciss_mtx);
44943a31b7ebSMike Smith     return(0);
44953a31b7ebSMike Smith }
44963a31b7ebSMike Smith 
44973a31b7ebSMike Smith /************************************************************************
44983a31b7ebSMike Smith  * Handle the last close on the control device.
44993a31b7ebSMike Smith  */
45003a31b7ebSMike Smith static int
450100b4e54aSWarner Losh ciss_close(struct cdev *dev, int flags, int fmt, struct thread *p)
45023a31b7ebSMike Smith {
45033a31b7ebSMike Smith     struct ciss_softc	*sc;
45043a31b7ebSMike Smith 
45053a31b7ebSMike Smith     debug_called(1);
45063a31b7ebSMike Smith 
45073a31b7ebSMike Smith     sc = (struct ciss_softc *)dev->si_drv1;
45083a31b7ebSMike Smith 
4509975b7318SScott Long     mtx_lock(&sc->ciss_mtx);
45103a31b7ebSMike Smith     sc->ciss_flags &= ~CISS_FLAG_CONTROL_OPEN;
4511975b7318SScott Long     mtx_unlock(&sc->ciss_mtx);
45123a31b7ebSMike Smith     return (0);
45133a31b7ebSMike Smith }
45143a31b7ebSMike Smith 
45153a31b7ebSMike Smith /********************************************************************************
45163a31b7ebSMike Smith  * Handle adapter-specific control operations.
45173a31b7ebSMike Smith  *
45183a31b7ebSMike Smith  * Note that the API here is compatible with the Linux driver, in order to
45193a31b7ebSMike Smith  * simplify the porting of Compaq's userland tools.
45203a31b7ebSMike Smith  */
45213a31b7ebSMike Smith static int
452200b4e54aSWarner Losh ciss_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *p)
45233a31b7ebSMike Smith {
45243a31b7ebSMike Smith     struct ciss_softc		*sc;
45257caeec6aSPaul Saab     IOCTL_Command_struct	*ioc	= (IOCTL_Command_struct *)addr;
45267caeec6aSPaul Saab #ifdef __amd64__
45277caeec6aSPaul Saab     IOCTL_Command_struct32	*ioc32	= (IOCTL_Command_struct32 *)addr;
45287caeec6aSPaul Saab     IOCTL_Command_struct	ioc_swab;
45297caeec6aSPaul Saab #endif
45303a31b7ebSMike Smith     int				error;
45313a31b7ebSMike Smith 
45323a31b7ebSMike Smith     debug_called(1);
45333a31b7ebSMike Smith 
45343a31b7ebSMike Smith     sc = (struct ciss_softc *)dev->si_drv1;
45353a31b7ebSMike Smith     error = 0;
4536975b7318SScott Long     mtx_lock(&sc->ciss_mtx);
45373a31b7ebSMike Smith 
45383a31b7ebSMike Smith     switch(cmd) {
453922657ce1SScott Long     case CCISS_GETQSTATS:
454022657ce1SScott Long     {
454122657ce1SScott Long 	union ciss_statrequest *cr = (union ciss_statrequest *)addr;
454222657ce1SScott Long 
454322657ce1SScott Long 	switch (cr->cs_item) {
454422657ce1SScott Long 	case CISSQ_FREE:
454522657ce1SScott Long 	case CISSQ_NOTIFY:
454622657ce1SScott Long 	    bcopy(&sc->ciss_qstat[cr->cs_item], &cr->cs_qstat,
454722657ce1SScott Long 		sizeof(struct ciss_qstat));
454822657ce1SScott Long 	    break;
454922657ce1SScott Long 	default:
455022657ce1SScott Long 	    error = ENOIOCTL;
455122657ce1SScott Long 	    break;
455222657ce1SScott Long 	}
455322657ce1SScott Long 
455422657ce1SScott Long 	break;
455522657ce1SScott Long     }
455622657ce1SScott Long 
45573a31b7ebSMike Smith     case CCISS_GETPCIINFO:
45583a31b7ebSMike Smith     {
45593a31b7ebSMike Smith 	cciss_pci_info_struct	*pis = (cciss_pci_info_struct *)addr;
45603a31b7ebSMike Smith 
45613a31b7ebSMike Smith 	pis->bus = pci_get_bus(sc->ciss_dev);
45623a31b7ebSMike Smith 	pis->dev_fn = pci_get_slot(sc->ciss_dev);
4563e17ef005SSean Bruno         pis->board_id = (pci_get_subvendor(sc->ciss_dev) << 16) |
4564e17ef005SSean Bruno                 pci_get_subdevice(sc->ciss_dev);
45653a31b7ebSMike Smith 
45663a31b7ebSMike Smith 	break;
45673a31b7ebSMike Smith     }
45683a31b7ebSMike Smith 
45693a31b7ebSMike Smith     case CCISS_GETINTINFO:
45703a31b7ebSMike Smith     {
45713a31b7ebSMike Smith 	cciss_coalint_struct	*cis = (cciss_coalint_struct *)addr;
45723a31b7ebSMike Smith 
45733a31b7ebSMike Smith 	cis->delay = sc->ciss_cfg->interrupt_coalesce_delay;
45743a31b7ebSMike Smith 	cis->count = sc->ciss_cfg->interrupt_coalesce_count;
45753a31b7ebSMike Smith 
45763a31b7ebSMike Smith 	break;
45773a31b7ebSMike Smith     }
45783a31b7ebSMike Smith 
45793a31b7ebSMike Smith     case CCISS_SETINTINFO:
45803a31b7ebSMike Smith     {
45813a31b7ebSMike Smith 	cciss_coalint_struct	*cis = (cciss_coalint_struct *)addr;
45823a31b7ebSMike Smith 
45833a31b7ebSMike Smith 	if ((cis->delay == 0) && (cis->count == 0)) {
45843a31b7ebSMike Smith 	    error = EINVAL;
45853a31b7ebSMike Smith 	    break;
45863a31b7ebSMike Smith 	}
45873a31b7ebSMike Smith 
45883a31b7ebSMike Smith 	/*
45893a31b7ebSMike Smith 	 * XXX apparently this is only safe if the controller is idle,
45903a31b7ebSMike Smith 	 *     we should suspend it before doing this.
45913a31b7ebSMike Smith 	 */
45923a31b7ebSMike Smith 	sc->ciss_cfg->interrupt_coalesce_delay = cis->delay;
45933a31b7ebSMike Smith 	sc->ciss_cfg->interrupt_coalesce_count = cis->count;
45943a31b7ebSMike Smith 
45953a31b7ebSMike Smith 	if (ciss_update_config(sc))
45963a31b7ebSMike Smith 	    error = EIO;
45973a31b7ebSMike Smith 
45983a31b7ebSMike Smith 	/* XXX resume the controller here */
45993a31b7ebSMike Smith 	break;
46003a31b7ebSMike Smith     }
46013a31b7ebSMike Smith 
46023a31b7ebSMike Smith     case CCISS_GETNODENAME:
46033a31b7ebSMike Smith 	bcopy(sc->ciss_cfg->server_name, (NodeName_type *)addr,
46043a31b7ebSMike Smith 	      sizeof(NodeName_type));
46053a31b7ebSMike Smith 	break;
46063a31b7ebSMike Smith 
46073a31b7ebSMike Smith     case CCISS_SETNODENAME:
46083a31b7ebSMike Smith 	bcopy((NodeName_type *)addr, sc->ciss_cfg->server_name,
46093a31b7ebSMike Smith 	      sizeof(NodeName_type));
46103a31b7ebSMike Smith 	if (ciss_update_config(sc))
46113a31b7ebSMike Smith 	    error = EIO;
46123a31b7ebSMike Smith 	break;
46133a31b7ebSMike Smith 
46143a31b7ebSMike Smith     case CCISS_GETHEARTBEAT:
46153a31b7ebSMike Smith 	*(Heartbeat_type *)addr = sc->ciss_cfg->heartbeat;
46163a31b7ebSMike Smith 	break;
46173a31b7ebSMike Smith 
46183a31b7ebSMike Smith     case CCISS_GETBUSTYPES:
46193a31b7ebSMike Smith 	*(BusTypes_type *)addr = sc->ciss_cfg->bus_types;
46203a31b7ebSMike Smith 	break;
46213a31b7ebSMike Smith 
46223a31b7ebSMike Smith     case CCISS_GETFIRMVER:
46233a31b7ebSMike Smith 	bcopy(sc->ciss_id->running_firmware_revision, (FirmwareVer_type *)addr,
46243a31b7ebSMike Smith 	      sizeof(FirmwareVer_type));
46253a31b7ebSMike Smith 	break;
46263a31b7ebSMike Smith 
46273a31b7ebSMike Smith     case CCISS_GETDRIVERVER:
46283a31b7ebSMike Smith 	*(DriverVer_type *)addr = CISS_DRIVER_VERSION;
46293a31b7ebSMike Smith 	break;
46303a31b7ebSMike Smith 
46313a31b7ebSMike Smith     case CCISS_REVALIDVOLS:
46323a31b7ebSMike Smith 	/*
46333a31b7ebSMike Smith 	 * This is a bit ugly; to do it "right" we really need
46343a31b7ebSMike Smith 	 * to find any disks that have changed, kick CAM off them,
46353a31b7ebSMike Smith 	 * then rescan only these disks.  It'd be nice if they
46363a31b7ebSMike Smith 	 * a) told us which disk(s) they were going to play with,
46373a31b7ebSMike Smith 	 * and b) which ones had arrived. 8(
46383a31b7ebSMike Smith 	 */
46393a31b7ebSMike Smith 	break;
46403a31b7ebSMike Smith 
46417caeec6aSPaul Saab #ifdef __amd64__
46427caeec6aSPaul Saab     case CCISS_PASSTHRU32:
46437caeec6aSPaul Saab 	ioc_swab.LUN_info	= ioc32->LUN_info;
46447caeec6aSPaul Saab 	ioc_swab.Request	= ioc32->Request;
46457caeec6aSPaul Saab 	ioc_swab.error_info	= ioc32->error_info;
46467caeec6aSPaul Saab 	ioc_swab.buf_size	= ioc32->buf_size;
46477caeec6aSPaul Saab 	ioc_swab.buf		= (u_int8_t *)(uintptr_t)ioc32->buf;
46487caeec6aSPaul Saab 	ioc			= &ioc_swab;
46497caeec6aSPaul Saab 	/* FALLTHROUGH */
46507caeec6aSPaul Saab #endif
46517caeec6aSPaul Saab 
46523a31b7ebSMike Smith     case CCISS_PASSTHRU:
46537caeec6aSPaul Saab 	error = ciss_user_command(sc, ioc);
46543a31b7ebSMike Smith 	break;
46553a31b7ebSMike Smith 
46563a31b7ebSMike Smith     default:
46573a31b7ebSMike Smith 	debug(0, "unknown ioctl 0x%lx", cmd);
46583a31b7ebSMike Smith 
46593a31b7ebSMike Smith 	debug(1, "CCISS_GETPCIINFO:   0x%lx", CCISS_GETPCIINFO);
46603a31b7ebSMike Smith 	debug(1, "CCISS_GETINTINFO:   0x%lx", CCISS_GETINTINFO);
46613a31b7ebSMike Smith 	debug(1, "CCISS_SETINTINFO:   0x%lx", CCISS_SETINTINFO);
46623a31b7ebSMike Smith 	debug(1, "CCISS_GETNODENAME:  0x%lx", CCISS_GETNODENAME);
46633a31b7ebSMike Smith 	debug(1, "CCISS_SETNODENAME:  0x%lx", CCISS_SETNODENAME);
46643a31b7ebSMike Smith 	debug(1, "CCISS_GETHEARTBEAT: 0x%lx", CCISS_GETHEARTBEAT);
46653a31b7ebSMike Smith 	debug(1, "CCISS_GETBUSTYPES:  0x%lx", CCISS_GETBUSTYPES);
46663a31b7ebSMike Smith 	debug(1, "CCISS_GETFIRMVER:   0x%lx", CCISS_GETFIRMVER);
46673a31b7ebSMike Smith 	debug(1, "CCISS_GETDRIVERVER: 0x%lx", CCISS_GETDRIVERVER);
46683a31b7ebSMike Smith 	debug(1, "CCISS_REVALIDVOLS:  0x%lx", CCISS_REVALIDVOLS);
46693a31b7ebSMike Smith 	debug(1, "CCISS_PASSTHRU:     0x%lx", CCISS_PASSTHRU);
46703a31b7ebSMike Smith 
46713a31b7ebSMike Smith 	error = ENOIOCTL;
46723a31b7ebSMike Smith 	break;
46733a31b7ebSMike Smith     }
46743a31b7ebSMike Smith 
4675975b7318SScott Long     mtx_unlock(&sc->ciss_mtx);
46763a31b7ebSMike Smith     return(error);
46773a31b7ebSMike Smith }
4678