xref: /freebsd/sys/dev/ciss/ciss.c (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
13a31b7ebSMike Smith /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
43a31b7ebSMike Smith  * Copyright (c) 2001 Michael Smith
5c6131460SPaul Saab  * Copyright (c) 2004 Paul Saab
63a31b7ebSMike Smith  * All rights reserved.
73a31b7ebSMike Smith  *
83a31b7ebSMike Smith  * Redistribution and use in source and binary forms, with or without
93a31b7ebSMike Smith  * modification, are permitted provided that the following conditions
103a31b7ebSMike Smith  * are met:
113a31b7ebSMike Smith  * 1. Redistributions of source code must retain the above copyright
123a31b7ebSMike Smith  *    notice, this list of conditions and the following disclaimer.
133a31b7ebSMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
143a31b7ebSMike Smith  *    notice, this list of conditions and the following disclaimer in the
153a31b7ebSMike Smith  *    documentation and/or other materials provided with the distribution.
163a31b7ebSMike Smith  *
173a31b7ebSMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
183a31b7ebSMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
193a31b7ebSMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
203a31b7ebSMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
213a31b7ebSMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
223a31b7ebSMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
233a31b7ebSMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
243a31b7ebSMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
253a31b7ebSMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
263a31b7ebSMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
273a31b7ebSMike Smith  * SUCH DAMAGE.
283a31b7ebSMike Smith  *
293a31b7ebSMike Smith  *	$FreeBSD$
303a31b7ebSMike Smith  */
313a31b7ebSMike Smith 
323a31b7ebSMike Smith /*
333a31b7ebSMike Smith  * Common Interface for SCSI-3 Support driver.
343a31b7ebSMike Smith  *
353a31b7ebSMike Smith  * CISS claims to provide a common interface between a generic SCSI
363a31b7ebSMike Smith  * transport and an intelligent host adapter.
373a31b7ebSMike Smith  *
383a31b7ebSMike Smith  * This driver supports CISS as defined in the document "CISS Command
393a31b7ebSMike Smith  * Interface for SCSI-3 Support Open Specification", Version 1.04,
403a31b7ebSMike Smith  * Valence Number 1, dated 20001127, produced by Compaq Computer
413a31b7ebSMike Smith  * Corporation.  This document appears to be a hastily and somewhat
423a31b7ebSMike Smith  * arbitrarlily cut-down version of a larger (and probably even more
433a31b7ebSMike Smith  * chaotic and inconsistent) Compaq internal document.  Various
443a31b7ebSMike Smith  * details were also gleaned from Compaq's "cciss" driver for Linux.
453a31b7ebSMike Smith  *
463a31b7ebSMike Smith  * We provide a shim layer between the CISS interface and CAM,
473a31b7ebSMike Smith  * offloading most of the queueing and being-a-disk chores onto CAM.
483a31b7ebSMike Smith  * Entry to the driver is via the PCI bus attachment (ciss_probe,
493a31b7ebSMike Smith  * ciss_attach, etc) and via the CAM interface (ciss_cam_action,
503a31b7ebSMike Smith  * ciss_cam_poll).  The Compaq CISS adapters are, however, poor SCSI
513a31b7ebSMike Smith  * citizens and we have to fake up some responses to get reasonable
523a31b7ebSMike Smith  * behaviour out of them.  In addition, the CISS command set is by no
533a31b7ebSMike Smith  * means adequate to support the functionality of a RAID controller,
543a31b7ebSMike Smith  * and thus the supported Compaq adapters utilise portions of the
553a31b7ebSMike Smith  * control protocol from earlier Compaq adapter families.
563a31b7ebSMike Smith  *
573a31b7ebSMike Smith  * Note that we only support the "simple" transport layer over PCI.
583a31b7ebSMike Smith  * This interface (ab)uses the I2O register set (specifically the post
593a31b7ebSMike Smith  * queues) to exchange commands with the adapter.  Other interfaces
603a31b7ebSMike Smith  * are available, but we aren't supposed to know about them, and it is
613a31b7ebSMike Smith  * dubious whether they would provide major performance improvements
623a31b7ebSMike Smith  * except under extreme load.
633a31b7ebSMike Smith  *
643a31b7ebSMike Smith  * Currently the only supported CISS adapters are the Compaq Smart
653a31b7ebSMike Smith  * Array 5* series (5300, 5i, 532).  Even with only three adapters,
663a31b7ebSMike Smith  * Compaq still manage to have interface variations.
673a31b7ebSMike Smith  *
683a31b7ebSMike Smith  *
693a31b7ebSMike Smith  * Thanks must go to Fred Harris and Darryl DeVinney at Compaq, as
703a31b7ebSMike Smith  * well as Paul Saab at Yahoo! for their assistance in making this
713a31b7ebSMike Smith  * driver happen.
72c6131460SPaul Saab  *
73c6131460SPaul Saab  * More thanks must go to John Cagle at HP for the countless hours
74c6131460SPaul Saab  * spent making this driver "work" with the MSA* series storage
75c6131460SPaul Saab  * enclosures.  Without his help (and nagging), this driver could not
76c6131460SPaul Saab  * be used with these enclosures.
773a31b7ebSMike Smith  */
783a31b7ebSMike Smith 
793a31b7ebSMike Smith #include <sys/param.h>
803a31b7ebSMike Smith #include <sys/systm.h>
813a31b7ebSMike Smith #include <sys/malloc.h>
823a31b7ebSMike Smith #include <sys/kernel.h>
833a31b7ebSMike Smith #include <sys/bus.h>
843a31b7ebSMike Smith #include <sys/conf.h>
853a31b7ebSMike Smith #include <sys/stat.h>
86c6131460SPaul Saab #include <sys/kthread.h>
87c6131460SPaul Saab #include <sys/queue.h>
8817c0792dSPaul Saab #include <sys/sysctl.h>
893a31b7ebSMike Smith 
903a31b7ebSMike Smith #include <cam/cam.h>
913a31b7ebSMike Smith #include <cam/cam_ccb.h>
923a31b7ebSMike Smith #include <cam/cam_periph.h>
933a31b7ebSMike Smith #include <cam/cam_sim.h>
943a31b7ebSMike Smith #include <cam/cam_xpt_sim.h>
953a31b7ebSMike Smith #include <cam/scsi/scsi_all.h>
963a31b7ebSMike Smith #include <cam/scsi/scsi_message.h>
973a31b7ebSMike Smith 
983a31b7ebSMike Smith #include <machine/bus.h>
993a31b7ebSMike Smith #include <machine/endian.h>
1003a31b7ebSMike Smith #include <machine/resource.h>
1013a31b7ebSMike Smith #include <sys/rman.h>
1023a31b7ebSMike Smith 
1034fbd232cSWarner Losh #include <dev/pci/pcireg.h>
1044fbd232cSWarner Losh #include <dev/pci/pcivar.h>
1053a31b7ebSMike Smith 
1063a31b7ebSMike Smith #include <dev/ciss/cissreg.h>
1073a31b7ebSMike Smith #include <dev/ciss/cissio.h>
10822657ce1SScott Long #include <dev/ciss/cissvar.h>
1093a31b7ebSMike Smith 
1102e3d6d0fSWarner Losh #ifdef CISS_DEBUG
1112e3d6d0fSWarner Losh #include "opt_ddb.h"
1122e3d6d0fSWarner Losh #endif
1132e3d6d0fSWarner Losh 
114d745c852SEd Schouten static MALLOC_DEFINE(CISS_MALLOC_CLASS, "ciss_data",
115d745c852SEd Schouten     "ciss internal data buffers");
1163a31b7ebSMike Smith 
1173a31b7ebSMike Smith /* pci interface */
1183a31b7ebSMike Smith static int	ciss_lookup(device_t dev);
1193a31b7ebSMike Smith static int	ciss_probe(device_t dev);
1203a31b7ebSMike Smith static int	ciss_attach(device_t dev);
1213a31b7ebSMike Smith static int	ciss_detach(device_t dev);
1223a31b7ebSMike Smith static int	ciss_shutdown(device_t dev);
1233a31b7ebSMike Smith 
1243a31b7ebSMike Smith /* (de)initialisation functions, control wrappers */
1253a31b7ebSMike Smith static int	ciss_init_pci(struct ciss_softc *sc);
12622657ce1SScott Long static int	ciss_setup_msix(struct ciss_softc *sc);
12722657ce1SScott Long static int	ciss_init_perf(struct ciss_softc *sc);
1283a31b7ebSMike Smith static int	ciss_wait_adapter(struct ciss_softc *sc);
1293a31b7ebSMike Smith static int	ciss_flush_adapter(struct ciss_softc *sc);
1303a31b7ebSMike Smith static int	ciss_init_requests(struct ciss_softc *sc);
1313a31b7ebSMike Smith static void	ciss_command_map_helper(void *arg, bus_dma_segment_t *segs,
1323a31b7ebSMike Smith 					int nseg, int error);
1333a31b7ebSMike Smith static int	ciss_identify_adapter(struct ciss_softc *sc);
1343a31b7ebSMike Smith static int	ciss_init_logical(struct ciss_softc *sc);
135c6131460SPaul Saab static int	ciss_init_physical(struct ciss_softc *sc);
1361fe6c4eeSScott Long static int	ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll);
1373a31b7ebSMike Smith static int	ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld);
1383a31b7ebSMike Smith static int	ciss_get_ldrive_status(struct ciss_softc *sc,  struct ciss_ldrive *ld);
1393a31b7ebSMike Smith static int	ciss_update_config(struct ciss_softc *sc);
14088c78a38SPaul Saab static int	ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld);
14117c0792dSPaul Saab static void	ciss_init_sysctl(struct ciss_softc *sc);
14217c0792dSPaul Saab static void	ciss_soft_reset(struct ciss_softc *sc);
1433a31b7ebSMike Smith static void	ciss_free(struct ciss_softc *sc);
144c6131460SPaul Saab static void	ciss_spawn_notify_thread(struct ciss_softc *sc);
145c6131460SPaul Saab static void	ciss_kill_notify_thread(struct ciss_softc *sc);
1463a31b7ebSMike Smith 
1473a31b7ebSMike Smith /* request submission/completion */
1483a31b7ebSMike Smith static int	ciss_start(struct ciss_request *cr);
14922657ce1SScott Long static void	ciss_done(struct ciss_softc *sc, cr_qhead_t *qh);
15022657ce1SScott Long static void	ciss_perf_done(struct ciss_softc *sc, cr_qhead_t *qh);
1513a31b7ebSMike Smith static void	ciss_intr(void *arg);
15222657ce1SScott Long static void	ciss_perf_intr(void *arg);
15322657ce1SScott Long static void	ciss_perf_msi_intr(void *arg);
15422657ce1SScott Long static void	ciss_complete(struct ciss_softc *sc, cr_qhead_t *qh);
15522657ce1SScott Long static int	_ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status, const char *func);
1563a31b7ebSMike Smith static int	ciss_synch_request(struct ciss_request *cr, int timeout);
1573a31b7ebSMike Smith static int	ciss_poll_request(struct ciss_request *cr, int timeout);
1583a31b7ebSMike Smith static int	ciss_wait_request(struct ciss_request *cr, int timeout);
1596197ca15SPeter Wemm #if 0
1603a31b7ebSMike Smith static int	ciss_abort_request(struct ciss_request *cr);
1616197ca15SPeter Wemm #endif
1623a31b7ebSMike Smith 
1633a31b7ebSMike Smith /* request queueing */
1643a31b7ebSMike Smith static int	ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp);
1653a31b7ebSMike Smith static void	ciss_preen_command(struct ciss_request *cr);
1663a31b7ebSMike Smith static void 	ciss_release_request(struct ciss_request *cr);
1673a31b7ebSMike Smith 
1683a31b7ebSMike Smith /* request helpers */
1693a31b7ebSMike Smith static int	ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
1703a31b7ebSMike Smith 				      int opcode, void **bufp, size_t bufsize);
1713a31b7ebSMike Smith static int	ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc);
1723a31b7ebSMike Smith 
1733a31b7ebSMike Smith /* DMA map/unmap */
1743a31b7ebSMike Smith static int	ciss_map_request(struct ciss_request *cr);
1753a31b7ebSMike Smith static void	ciss_request_map_helper(void *arg, bus_dma_segment_t *segs,
1763a31b7ebSMike Smith 					int nseg, int error);
1773a31b7ebSMike Smith static void	ciss_unmap_request(struct ciss_request *cr);
1783a31b7ebSMike Smith 
1793a31b7ebSMike Smith /* CAM interface */
1803a31b7ebSMike Smith static int	ciss_cam_init(struct ciss_softc *sc);
181c6131460SPaul Saab static void	ciss_cam_rescan_target(struct ciss_softc *sc,
182c6131460SPaul Saab 				       int bus, int target);
1833a31b7ebSMike Smith static void	ciss_cam_action(struct cam_sim *sim, union ccb *ccb);
1843a31b7ebSMike Smith static int	ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio);
1853a31b7ebSMike Smith static int	ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio);
1863a31b7ebSMike Smith static void	ciss_cam_poll(struct cam_sim *sim);
1873a31b7ebSMike Smith static void	ciss_cam_complete(struct ciss_request *cr);
1883a31b7ebSMike Smith static void	ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio);
189c6131460SPaul Saab static int	ciss_name_device(struct ciss_softc *sc, int bus, int target);
1903a31b7ebSMike Smith 
1913a31b7ebSMike Smith /* periodic status monitoring */
1923a31b7ebSMike Smith static void	ciss_periodic(void *arg);
19354d02e0aSMitsuru IWASAKI static void	ciss_nop_complete(struct ciss_request *cr);
194e08d902aSMitsuru IWASAKI static void	ciss_disable_adapter(struct ciss_softc *sc);
1953a31b7ebSMike Smith static void	ciss_notify_event(struct ciss_softc *sc);
1963a31b7ebSMike Smith static void	ciss_notify_complete(struct ciss_request *cr);
1973a31b7ebSMike Smith static int	ciss_notify_abort(struct ciss_softc *sc);
1983a31b7ebSMike Smith static int	ciss_notify_abort_bmic(struct ciss_softc *sc);
1991fe6c4eeSScott Long static void	ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn);
2003a31b7ebSMike Smith static void	ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn);
2013a31b7ebSMike Smith static void	ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn);
2023a31b7ebSMike Smith 
2033a31b7ebSMike Smith /* debugging output */
2042e3d6d0fSWarner Losh #ifdef DDB
2053a31b7ebSMike Smith static void	ciss_print_request(struct ciss_request *cr);
2062e3d6d0fSWarner Losh #endif
2073a31b7ebSMike Smith static void	ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld);
2083a31b7ebSMike Smith static const char *ciss_name_ldrive_status(int status);
2093a31b7ebSMike Smith static int	ciss_decode_ldrive_status(int status);
2103a31b7ebSMike Smith static const char *ciss_name_ldrive_org(int org);
2113a31b7ebSMike Smith static const char *ciss_name_command_status(int status);
2123a31b7ebSMike Smith 
2133a31b7ebSMike Smith /*
2143a31b7ebSMike Smith  * PCI bus interface.
2153a31b7ebSMike Smith  */
2163a31b7ebSMike Smith static device_method_t ciss_methods[] = {
2173a31b7ebSMike Smith     /* Device interface */
2183a31b7ebSMike Smith     DEVMETHOD(device_probe,	ciss_probe),
2193a31b7ebSMike Smith     DEVMETHOD(device_attach,	ciss_attach),
2203a31b7ebSMike Smith     DEVMETHOD(device_detach,	ciss_detach),
2213a31b7ebSMike Smith     DEVMETHOD(device_shutdown,	ciss_shutdown),
2223a31b7ebSMike Smith     { 0, 0 }
2233a31b7ebSMike Smith };
2243a31b7ebSMike Smith 
2253a31b7ebSMike Smith static driver_t ciss_pci_driver = {
2263a31b7ebSMike Smith     "ciss",
2273a31b7ebSMike Smith     ciss_methods,
2283a31b7ebSMike Smith     sizeof(struct ciss_softc)
2293a31b7ebSMike Smith };
2303a31b7ebSMike Smith 
2313a31b7ebSMike Smith /*
2323a31b7ebSMike Smith  * Control device interface.
2333a31b7ebSMike Smith  */
2343a31b7ebSMike Smith static d_open_t		ciss_open;
2353a31b7ebSMike Smith static d_close_t	ciss_close;
2363a31b7ebSMike Smith static d_ioctl_t	ciss_ioctl;
2373a31b7ebSMike Smith 
2383a31b7ebSMike Smith static struct cdevsw ciss_cdevsw = {
239dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
240975b7318SScott Long 	.d_flags =	0,
2417ac40f5fSPoul-Henning Kamp 	.d_open =	ciss_open,
2427ac40f5fSPoul-Henning Kamp 	.d_close =	ciss_close,
2437ac40f5fSPoul-Henning Kamp 	.d_ioctl =	ciss_ioctl,
2447ac40f5fSPoul-Henning Kamp 	.d_name =	"ciss",
2453a31b7ebSMike Smith };
2463a31b7ebSMike Smith 
2471fe6c4eeSScott Long /*
2481fe6c4eeSScott Long  * This tunable can be set at boot time and controls whether physical devices
2491fe6c4eeSScott Long  * that are marked hidden by the firmware should be exposed anyways.
2501fe6c4eeSScott Long  */
2511fe6c4eeSScott Long static unsigned int ciss_expose_hidden_physical = 0;
2521fe6c4eeSScott Long TUNABLE_INT("hw.ciss.expose_hidden_physical", &ciss_expose_hidden_physical);
2531fe6c4eeSScott Long 
2545a3c4d69SMitsuru IWASAKI static unsigned int ciss_nop_message_heartbeat = 0;
2555a3c4d69SMitsuru IWASAKI TUNABLE_INT("hw.ciss.nop_message_heartbeat", &ciss_nop_message_heartbeat);
2565a3c4d69SMitsuru IWASAKI 
25722657ce1SScott Long /*
25822657ce1SScott Long  * This tunable can force a particular transport to be used:
25922657ce1SScott Long  * <= 0 : use default
26022657ce1SScott Long  *    1 : force simple
26122657ce1SScott Long  *    2 : force performant
26222657ce1SScott Long  */
26322657ce1SScott Long static int ciss_force_transport = 0;
26422657ce1SScott Long TUNABLE_INT("hw.ciss.force_transport", &ciss_force_transport);
26522657ce1SScott Long 
26622657ce1SScott Long /*
26722657ce1SScott Long  * This tunable can force a particular interrupt delivery method to be used:
26822657ce1SScott Long  * <= 0 : use default
26922657ce1SScott Long  *    1 : force INTx
27022657ce1SScott Long  *    2 : force MSIX
27122657ce1SScott Long  */
27222657ce1SScott Long static int ciss_force_interrupt = 0;
27322657ce1SScott Long TUNABLE_INT("hw.ciss.force_interrupt", &ciss_force_interrupt);
27422657ce1SScott Long 
2753a31b7ebSMike Smith /************************************************************************
2763a31b7ebSMike Smith  * CISS adapters amazingly don't have a defined programming interface
2773a31b7ebSMike Smith  * value.  (One could say some very despairing things about PCI and
2783a31b7ebSMike Smith  * people just not getting the general idea.)  So we are forced to
2793a31b7ebSMike Smith  * stick with matching against subvendor/subdevice, and thus have to
2803a31b7ebSMike Smith  * be updated for every new CISS adapter that appears.
2813a31b7ebSMike Smith  */
2824e2ddfbaSWarner Losh #define CISS_BOARD_UNKNOWN	0
2832fdaa90dSScott Long #define CISS_BOARD_SA5		1
2842fdaa90dSScott Long #define CISS_BOARD_SA5B		2
2852fdaa90dSScott Long #define CISS_BOARD_NOMSI	(1<<4)
286ee4827b2SSean Bruno #define CISS_BOARD_SIMPLE       (1<<5)
2873a31b7ebSMike Smith 
2883a31b7ebSMike Smith static struct
2893a31b7ebSMike Smith {
2903a31b7ebSMike Smith     u_int16_t	subvendor;
2913a31b7ebSMike Smith     u_int16_t	subdevice;
2923a31b7ebSMike Smith     int		flags;
2933a31b7ebSMike Smith     char	*desc;
2943a31b7ebSMike Smith } ciss_vendor_data[] = {
295ee4827b2SSean Bruno     { 0x0e11, 0x4070, CISS_BOARD_SA5|CISS_BOARD_NOMSI|CISS_BOARD_SIMPLE,
296ee4827b2SSean Bruno                                                         "Compaq Smart Array 5300" },
2972fdaa90dSScott Long     { 0x0e11, 0x4080, CISS_BOARD_SA5B|CISS_BOARD_NOMSI,	"Compaq Smart Array 5i" },
2982fdaa90dSScott Long     { 0x0e11, 0x4082, CISS_BOARD_SA5B|CISS_BOARD_NOMSI,	"Compaq Smart Array 532" },
2992fdaa90dSScott Long     { 0x0e11, 0x4083, CISS_BOARD_SA5B|CISS_BOARD_NOMSI,	"HP Smart Array 5312" },
300b26392c7SPaul Saab     { 0x0e11, 0x4091, CISS_BOARD_SA5,	"HP Smart Array 6i" },
3019e7313bbSPaul Saab     { 0x0e11, 0x409A, CISS_BOARD_SA5,	"HP Smart Array 641" },
3029e7313bbSPaul Saab     { 0x0e11, 0x409B, CISS_BOARD_SA5,	"HP Smart Array 642" },
3039e7313bbSPaul Saab     { 0x0e11, 0x409C, CISS_BOARD_SA5,	"HP Smart Array 6400" },
3049e7313bbSPaul Saab     { 0x0e11, 0x409D, CISS_BOARD_SA5,	"HP Smart Array 6400 EM" },
305cad572c4SPaul Saab     { 0x103C, 0x3211, CISS_BOARD_SA5,	"HP Smart Array E200i" },
306cad572c4SPaul Saab     { 0x103C, 0x3212, CISS_BOARD_SA5,	"HP Smart Array E200" },
307cad572c4SPaul Saab     { 0x103C, 0x3213, CISS_BOARD_SA5,	"HP Smart Array E200i" },
308cad572c4SPaul Saab     { 0x103C, 0x3214, CISS_BOARD_SA5,	"HP Smart Array E200i" },
309cad572c4SPaul Saab     { 0x103C, 0x3215, CISS_BOARD_SA5,	"HP Smart Array E200i" },
310b612d5e1SPaul Saab     { 0x103C, 0x3220, CISS_BOARD_SA5,	"HP Smart Array" },
311b612d5e1SPaul Saab     { 0x103C, 0x3222, CISS_BOARD_SA5,	"HP Smart Array" },
3124bb10cf1SPaul Saab     { 0x103C, 0x3223, CISS_BOARD_SA5,	"HP Smart Array P800" },
313f3f82767SPaul Saab     { 0x103C, 0x3225, CISS_BOARD_SA5,	"HP Smart Array P600" },
314b612d5e1SPaul Saab     { 0x103C, 0x3230, CISS_BOARD_SA5,	"HP Smart Array" },
315cad572c4SPaul Saab     { 0x103C, 0x3231, CISS_BOARD_SA5,	"HP Smart Array" },
316b612d5e1SPaul Saab     { 0x103C, 0x3232, CISS_BOARD_SA5,	"HP Smart Array" },
317b612d5e1SPaul Saab     { 0x103C, 0x3233, CISS_BOARD_SA5,	"HP Smart Array" },
318cad572c4SPaul Saab     { 0x103C, 0x3234, CISS_BOARD_SA5,	"HP Smart Array P400" },
319cad572c4SPaul Saab     { 0x103C, 0x3235, CISS_BOARD_SA5,	"HP Smart Array P400i" },
320b612d5e1SPaul Saab     { 0x103C, 0x3236, CISS_BOARD_SA5,	"HP Smart Array" },
3217b6d3d4cSScott Long     { 0x103C, 0x3237, CISS_BOARD_SA5,	"HP Smart Array E500" },
322b612d5e1SPaul Saab     { 0x103C, 0x3238, CISS_BOARD_SA5,	"HP Smart Array" },
323b612d5e1SPaul Saab     { 0x103C, 0x3239, CISS_BOARD_SA5,	"HP Smart Array" },
324b612d5e1SPaul Saab     { 0x103C, 0x323A, CISS_BOARD_SA5,	"HP Smart Array" },
325b612d5e1SPaul Saab     { 0x103C, 0x323B, CISS_BOARD_SA5,	"HP Smart Array" },
326b612d5e1SPaul Saab     { 0x103C, 0x323C, CISS_BOARD_SA5,	"HP Smart Array" },
3277b6d3d4cSScott Long     { 0x103C, 0x323D, CISS_BOARD_SA5,	"HP Smart Array P700m" },
328160b4e6bSPaul Saab     { 0x103C, 0x3241, CISS_BOARD_SA5,	"HP Smart Array P212" },
329160b4e6bSPaul Saab     { 0x103C, 0x3243, CISS_BOARD_SA5,	"HP Smart Array P410" },
330160b4e6bSPaul Saab     { 0x103C, 0x3245, CISS_BOARD_SA5,	"HP Smart Array P410i" },
331160b4e6bSPaul Saab     { 0x103C, 0x3247, CISS_BOARD_SA5,	"HP Smart Array P411" },
332160b4e6bSPaul Saab     { 0x103C, 0x3249, CISS_BOARD_SA5,	"HP Smart Array P812" },
3337b6d3d4cSScott Long     { 0x103C, 0x324A, CISS_BOARD_SA5,	"HP Smart Array P712m" },
3347b6d3d4cSScott Long     { 0x103C, 0x324B, CISS_BOARD_SA5,	"HP Smart Array" },
335e17ef005SSean Bruno     { 0x103C, 0x3350, CISS_BOARD_SA5,   "HP Smart Array P222" },
3365564c1cdSSean Bruno     { 0x103C, 0x3351, CISS_BOARD_SA5,   "HP Smart Array P420" },
337e17ef005SSean Bruno     { 0x103C, 0x3352, CISS_BOARD_SA5,   "HP Smart Array P421" },
338e17ef005SSean Bruno     { 0x103C, 0x3353, CISS_BOARD_SA5,   "HP Smart Array P822" },
339e17ef005SSean Bruno     { 0x103C, 0x3354, CISS_BOARD_SA5,   "HP Smart Array P420i" },
340e17ef005SSean Bruno     { 0x103C, 0x3355, CISS_BOARD_SA5,   "HP Smart Array P220i" },
341e17ef005SSean Bruno     { 0x103C, 0x3356, CISS_BOARD_SA5,   "HP Smart Array P721m" },
342ec5d9810SSean Bruno     { 0x103C, 0x1920, CISS_BOARD_SA5,   "HP Smart Array P430i" },
343ec5d9810SSean Bruno     { 0x103C, 0x1921, CISS_BOARD_SA5,   "HP Smart Array P830i" },
344ec5d9810SSean Bruno     { 0x103C, 0x1922, CISS_BOARD_SA5,   "HP Smart Array P430" },
345ec5d9810SSean Bruno     { 0x103C, 0x1923, CISS_BOARD_SA5,   "HP Smart Array P431" },
346ec5d9810SSean Bruno     { 0x103C, 0x1924, CISS_BOARD_SA5,   "HP Smart Array P830" },
347ec5d9810SSean Bruno     { 0x103C, 0x1926, CISS_BOARD_SA5,   "HP Smart Array P731m" },
348ec5d9810SSean Bruno     { 0x103C, 0x1928, CISS_BOARD_SA5,   "HP Smart Array P230i" },
349ec5d9810SSean Bruno     { 0x103C, 0x1929, CISS_BOARD_SA5,   "HP Smart Array P530" },
350ec5d9810SSean Bruno     { 0x103C, 0x192A, CISS_BOARD_SA5,   "HP Smart Array P531" },
351bf10f1a0SSean Bruno     { 0x103C, 0x21BD, CISS_BOARD_SA5,   "HP Smart Array P244br" },
352bf10f1a0SSean Bruno     { 0x103C, 0x21BE, CISS_BOARD_SA5,   "HP Smart Array P741m" },
353bf10f1a0SSean Bruno     { 0x103C, 0x21BF, CISS_BOARD_SA5,   "HP Smart Array H240ar" },
354bf10f1a0SSean Bruno     { 0x103C, 0x21C0, CISS_BOARD_SA5,   "HP Smart Array P440ar" },
355bf10f1a0SSean Bruno     { 0x103C, 0x21C1, CISS_BOARD_SA5,   "HP Smart Array P840ar" },
356bf10f1a0SSean Bruno     { 0x103C, 0x21C2, CISS_BOARD_SA5,   "HP Smart Array P440" },
357bf10f1a0SSean Bruno     { 0x103C, 0x21C3, CISS_BOARD_SA5,   "HP Smart Array P441" },
358bf10f1a0SSean Bruno     { 0x103C, 0x21C5, CISS_BOARD_SA5,   "HP Smart Array P841" },
359bf10f1a0SSean Bruno     { 0x103C, 0x21C6, CISS_BOARD_SA5,   "HP Smart Array H244br" },
360bf10f1a0SSean Bruno     { 0x103C, 0x21C7, CISS_BOARD_SA5,   "HP Smart Array H240" },
361bf10f1a0SSean Bruno     { 0x103C, 0x21C8, CISS_BOARD_SA5,   "HP Smart Array H241" },
362bf10f1a0SSean Bruno     { 0x103C, 0x21CA, CISS_BOARD_SA5,   "HP Smart Array P246br" },
363bf10f1a0SSean Bruno     { 0x103C, 0x21CB, CISS_BOARD_SA5,   "HP Smart Array P840" },
364d3d9a343SScott Long     { 0x103C, 0x21CC, CISS_BOARD_SA5,   "HP Smart Array P542d" },
365bf10f1a0SSean Bruno     { 0x103C, 0x21CD, CISS_BOARD_SA5,   "HP Smart Array P240nr" },
366bf10f1a0SSean Bruno     { 0x103C, 0x21CE, CISS_BOARD_SA5,   "HP Smart Array H240nr" },
3674a6a94d8SArchie Cobbs     { 0, 0, 0, NULL }
3683a31b7ebSMike Smith };
3693a31b7ebSMike Smith 
3706b66c298SJohn Baldwin DRIVER_MODULE(ciss, pci, ciss_pci_driver, 0, 0);
3714322d107SWarner Losh MODULE_PNP_INFO("U16:vendor;U16:device;", pci, ciss, ciss_vendor_data,
372329e817fSWarner Losh     nitems(ciss_vendor_data) - 1);
3734322d107SWarner Losh MODULE_DEPEND(ciss, cam, 1, 1, 1);
3744322d107SWarner Losh MODULE_DEPEND(ciss, pci, 1, 1, 1);
3754322d107SWarner Losh 
3763a31b7ebSMike Smith /************************************************************************
3773a31b7ebSMike Smith  * Find a match for the device in our list of known adapters.
3783a31b7ebSMike Smith  */
3793a31b7ebSMike Smith static int
3803a31b7ebSMike Smith ciss_lookup(device_t dev)
3813a31b7ebSMike Smith {
3823a31b7ebSMike Smith     int 	i;
3833a31b7ebSMike Smith 
3843a31b7ebSMike Smith     for (i = 0; ciss_vendor_data[i].desc != NULL; i++)
3853a31b7ebSMike Smith 	if ((pci_get_subvendor(dev) == ciss_vendor_data[i].subvendor) &&
3863a31b7ebSMike Smith 	    (pci_get_subdevice(dev) == ciss_vendor_data[i].subdevice)) {
3873a31b7ebSMike Smith 	    return(i);
3883a31b7ebSMike Smith 	}
3893a31b7ebSMike Smith     return(-1);
3903a31b7ebSMike Smith }
3913a31b7ebSMike Smith 
3923a31b7ebSMike Smith /************************************************************************
3933a31b7ebSMike Smith  * Match a known CISS adapter.
3943a31b7ebSMike Smith  */
3953a31b7ebSMike Smith static int
3963a31b7ebSMike Smith ciss_probe(device_t dev)
3973a31b7ebSMike Smith {
3983a31b7ebSMike Smith     int		i;
3993a31b7ebSMike Smith 
4003a31b7ebSMike Smith     i = ciss_lookup(dev);
4013a31b7ebSMike Smith     if (i != -1) {
4023a31b7ebSMike Smith 	device_set_desc(dev, ciss_vendor_data[i].desc);
403538565c4SWarner Losh 	return(BUS_PROBE_DEFAULT);
4043a31b7ebSMike Smith     }
4053a31b7ebSMike Smith     return(ENOENT);
4063a31b7ebSMike Smith }
4073a31b7ebSMike Smith 
4083a31b7ebSMike Smith /************************************************************************
4093a31b7ebSMike Smith  * Attach the driver to this adapter.
4103a31b7ebSMike Smith  */
4113a31b7ebSMike Smith static int
4123a31b7ebSMike Smith ciss_attach(device_t dev)
4133a31b7ebSMike Smith {
4143a31b7ebSMike Smith     struct ciss_softc	*sc;
415d69c9c78SScott Long     int			error;
4163a31b7ebSMike Smith 
4173a31b7ebSMike Smith     debug_called(1);
4183a31b7ebSMike Smith 
4193a31b7ebSMike Smith #ifdef CISS_DEBUG
4203a31b7ebSMike Smith     /* print structure/union sizes */
4213a31b7ebSMike Smith     debug_struct(ciss_command);
4223a31b7ebSMike Smith     debug_struct(ciss_header);
4233a31b7ebSMike Smith     debug_union(ciss_device_address);
4243a31b7ebSMike Smith     debug_struct(ciss_cdb);
4253a31b7ebSMike Smith     debug_struct(ciss_report_cdb);
4263a31b7ebSMike Smith     debug_struct(ciss_notify_cdb);
4273a31b7ebSMike Smith     debug_struct(ciss_notify);
4283a31b7ebSMike Smith     debug_struct(ciss_message_cdb);
4293a31b7ebSMike Smith     debug_struct(ciss_error_info_pointer);
4303a31b7ebSMike Smith     debug_struct(ciss_error_info);
4313a31b7ebSMike Smith     debug_struct(ciss_sg_entry);
4323a31b7ebSMike Smith     debug_struct(ciss_config_table);
4333a31b7ebSMike Smith     debug_struct(ciss_bmic_cdb);
4343a31b7ebSMike Smith     debug_struct(ciss_bmic_id_ldrive);
4353a31b7ebSMike Smith     debug_struct(ciss_bmic_id_lstatus);
4363a31b7ebSMike Smith     debug_struct(ciss_bmic_id_table);
4373a31b7ebSMike Smith     debug_struct(ciss_bmic_id_pdrive);
4383a31b7ebSMike Smith     debug_struct(ciss_bmic_blink_pdrive);
4393a31b7ebSMike Smith     debug_struct(ciss_bmic_flush_cache);
4403a31b7ebSMike Smith     debug_const(CISS_MAX_REQUESTS);
4413a31b7ebSMike Smith     debug_const(CISS_MAX_LOGICAL);
4423a31b7ebSMike Smith     debug_const(CISS_INTERRUPT_COALESCE_DELAY);
4433a31b7ebSMike Smith     debug_const(CISS_INTERRUPT_COALESCE_COUNT);
4443a31b7ebSMike Smith     debug_const(CISS_COMMAND_ALLOC_SIZE);
4453a31b7ebSMike Smith     debug_const(CISS_COMMAND_SG_LENGTH);
4463a31b7ebSMike Smith 
4473a31b7ebSMike Smith     debug_type(cciss_pci_info_struct);
4483a31b7ebSMike Smith     debug_type(cciss_coalint_struct);
4493a31b7ebSMike Smith     debug_type(cciss_coalint_struct);
4503a31b7ebSMike Smith     debug_type(NodeName_type);
4513a31b7ebSMike Smith     debug_type(NodeName_type);
4523a31b7ebSMike Smith     debug_type(Heartbeat_type);
4533a31b7ebSMike Smith     debug_type(BusTypes_type);
4543a31b7ebSMike Smith     debug_type(FirmwareVer_type);
4553a31b7ebSMike Smith     debug_type(DriverVer_type);
4563a31b7ebSMike Smith     debug_type(IOCTL_Command_struct);
4573a31b7ebSMike Smith #endif
4583a31b7ebSMike Smith 
4593a31b7ebSMike Smith     sc = device_get_softc(dev);
4603a31b7ebSMike Smith     sc->ciss_dev = dev;
46164a026bdSGavin Atkinson     mtx_init(&sc->ciss_mtx, "cissmtx", NULL, MTX_DEF);
462aa1385cdSJohn Baldwin     callout_init_mtx(&sc->ciss_periodic, &sc->ciss_mtx, 0);
4633a31b7ebSMike Smith 
4643a31b7ebSMike Smith     /*
4653a31b7ebSMike Smith      * Do PCI-specific init.
4663a31b7ebSMike Smith      */
4673a31b7ebSMike Smith     if ((error = ciss_init_pci(sc)) != 0)
4683a31b7ebSMike Smith 	goto out;
4693a31b7ebSMike Smith 
4703a31b7ebSMike Smith     /*
4713a31b7ebSMike Smith      * Initialise driver queues.
4723a31b7ebSMike Smith      */
4733a31b7ebSMike Smith     ciss_initq_free(sc);
474c6131460SPaul Saab     ciss_initq_notify(sc);
4753a31b7ebSMike Smith 
4763a31b7ebSMike Smith     /*
477b790c193SPedro F. Giffuni      * Initialize device sysctls.
47817c0792dSPaul Saab      */
47917c0792dSPaul Saab     ciss_init_sysctl(sc);
48017c0792dSPaul Saab 
48117c0792dSPaul Saab     /*
4823a31b7ebSMike Smith      * Initialise command/request pool.
4833a31b7ebSMike Smith      */
4843a31b7ebSMike Smith     if ((error = ciss_init_requests(sc)) != 0)
4853a31b7ebSMike Smith 	goto out;
4863a31b7ebSMike Smith 
4873a31b7ebSMike Smith     /*
4883a31b7ebSMike Smith      * Get adapter information.
4893a31b7ebSMike Smith      */
4903a31b7ebSMike Smith     if ((error = ciss_identify_adapter(sc)) != 0)
4913a31b7ebSMike Smith 	goto out;
4923a31b7ebSMike Smith 
4933a31b7ebSMike Smith     /*
494c6131460SPaul Saab      * Find all the physical devices.
495c6131460SPaul Saab      */
496c6131460SPaul Saab     if ((error = ciss_init_physical(sc)) != 0)
497c6131460SPaul Saab 	goto out;
498c6131460SPaul Saab 
499c6131460SPaul Saab     /*
5003a31b7ebSMike Smith      * Build our private table of logical devices.
5013a31b7ebSMike Smith      */
5023a31b7ebSMike Smith     if ((error = ciss_init_logical(sc)) != 0)
5033a31b7ebSMike Smith 	goto out;
5043a31b7ebSMike Smith 
5053a31b7ebSMike Smith     /*
5063a31b7ebSMike Smith      * Enable interrupts so that the CAM scan can complete.
5073a31b7ebSMike Smith      */
5083a31b7ebSMike Smith     CISS_TL_SIMPLE_ENABLE_INTERRUPTS(sc);
5093a31b7ebSMike Smith 
5103a31b7ebSMike Smith     /*
5113a31b7ebSMike Smith      * Initialise the CAM interface.
5123a31b7ebSMike Smith      */
5133a31b7ebSMike Smith     if ((error = ciss_cam_init(sc)) != 0)
5143a31b7ebSMike Smith 	goto out;
5153a31b7ebSMike Smith 
5163a31b7ebSMike Smith     /*
5173a31b7ebSMike Smith      * Start the heartbeat routine and event chain.
5183a31b7ebSMike Smith      */
5193a31b7ebSMike Smith     ciss_periodic(sc);
5203a31b7ebSMike Smith 
5213a31b7ebSMike Smith    /*
5223a31b7ebSMike Smith      * Create the control device.
5233a31b7ebSMike Smith      */
5243a31b7ebSMike Smith     sc->ciss_dev_t = make_dev(&ciss_cdevsw, device_get_unit(sc->ciss_dev),
5253a31b7ebSMike Smith 			      UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
5263a31b7ebSMike Smith 			      "ciss%d", device_get_unit(sc->ciss_dev));
5273a31b7ebSMike Smith     sc->ciss_dev_t->si_drv1 = sc;
5283a31b7ebSMike Smith 
5293a31b7ebSMike Smith     /*
5303a31b7ebSMike Smith      * The adapter is running; synchronous commands can now sleep
5313a31b7ebSMike Smith      * waiting for an interrupt to signal completion.
5323a31b7ebSMike Smith      */
5333a31b7ebSMike Smith     sc->ciss_flags |= CISS_FLAG_RUNNING;
5343a31b7ebSMike Smith 
535c6131460SPaul Saab     ciss_spawn_notify_thread(sc);
536c6131460SPaul Saab 
5373a31b7ebSMike Smith     error = 0;
5383a31b7ebSMike Smith  out:
53964a026bdSGavin Atkinson     if (error != 0) {
54064a026bdSGavin Atkinson 	/* ciss_free() expects the mutex to be held */
54164a026bdSGavin Atkinson 	mtx_lock(&sc->ciss_mtx);
5423a31b7ebSMike Smith 	ciss_free(sc);
54364a026bdSGavin Atkinson     }
5443a31b7ebSMike Smith     return(error);
5453a31b7ebSMike Smith }
5463a31b7ebSMike Smith 
5473a31b7ebSMike Smith /************************************************************************
5483a31b7ebSMike Smith  * Detach the driver from this adapter.
5493a31b7ebSMike Smith  */
5503a31b7ebSMike Smith static int
5513a31b7ebSMike Smith ciss_detach(device_t dev)
5523a31b7ebSMike Smith {
5533a31b7ebSMike Smith     struct ciss_softc	*sc = device_get_softc(dev);
5543a31b7ebSMike Smith 
5553a31b7ebSMike Smith     debug_called(1);
5563a31b7ebSMike Smith 
557975b7318SScott Long     mtx_lock(&sc->ciss_mtx);
558975b7318SScott Long     if (sc->ciss_flags & CISS_FLAG_CONTROL_OPEN) {
559975b7318SScott Long 	mtx_unlock(&sc->ciss_mtx);
560ffdf82e1SPaul Saab 	return (EBUSY);
561975b7318SScott Long     }
562ffdf82e1SPaul Saab 
5633a31b7ebSMike Smith     /* flush adapter cache */
5643a31b7ebSMike Smith     ciss_flush_adapter(sc);
5653a31b7ebSMike Smith 
566975b7318SScott Long     /* release all resources.  The mutex is released and freed here too. */
5673a31b7ebSMike Smith     ciss_free(sc);
5683a31b7ebSMike Smith 
5693a31b7ebSMike Smith     return(0);
5703a31b7ebSMike Smith }
5713a31b7ebSMike Smith 
5723a31b7ebSMike Smith /************************************************************************
5733a31b7ebSMike Smith  * Prepare adapter for system shutdown.
5743a31b7ebSMike Smith  */
5753a31b7ebSMike Smith static int
5763a31b7ebSMike Smith ciss_shutdown(device_t dev)
5773a31b7ebSMike Smith {
5783a31b7ebSMike Smith     struct ciss_softc	*sc = device_get_softc(dev);
5793a31b7ebSMike Smith 
5803a31b7ebSMike Smith     debug_called(1);
5813a31b7ebSMike Smith 
582d4a4ddc6SScott Long     mtx_lock(&sc->ciss_mtx);
5833a31b7ebSMike Smith     /* flush adapter cache */
5843a31b7ebSMike Smith     ciss_flush_adapter(sc);
5853a31b7ebSMike Smith 
58617c0792dSPaul Saab     if (sc->ciss_soft_reset)
58717c0792dSPaul Saab 	ciss_soft_reset(sc);
588d4a4ddc6SScott Long     mtx_unlock(&sc->ciss_mtx);
58917c0792dSPaul Saab 
5903a31b7ebSMike Smith     return(0);
5913a31b7ebSMike Smith }
5923a31b7ebSMike Smith 
59317c0792dSPaul Saab static void
59417c0792dSPaul Saab ciss_init_sysctl(struct ciss_softc *sc)
59517c0792dSPaul Saab {
59617c0792dSPaul Saab 
59717c0792dSPaul Saab     SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->ciss_dev),
59817c0792dSPaul Saab 	SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ciss_dev)),
59917c0792dSPaul Saab 	OID_AUTO, "soft_reset", CTLFLAG_RW, &sc->ciss_soft_reset, 0, "");
60017c0792dSPaul Saab }
60117c0792dSPaul Saab 
6023a31b7ebSMike Smith /************************************************************************
6033a31b7ebSMike Smith  * Perform PCI-specific attachment actions.
6043a31b7ebSMike Smith  */
6053a31b7ebSMike Smith static int
6063a31b7ebSMike Smith ciss_init_pci(struct ciss_softc *sc)
6073a31b7ebSMike Smith {
6083a31b7ebSMike Smith     uintptr_t		cbase, csize, cofs;
60922657ce1SScott Long     uint32_t		method, supported_methods;
610d69c9c78SScott Long     int			error, sqmask, i;
61122657ce1SScott Long     void		*intr;
6123a31b7ebSMike Smith 
6133a31b7ebSMike Smith     debug_called(1);
6143a31b7ebSMike Smith 
6153a31b7ebSMike Smith     /*
616d69c9c78SScott Long      * Work out adapter type.
617d69c9c78SScott Long      */
618d69c9c78SScott Long     i = ciss_lookup(sc->ciss_dev);
619d69c9c78SScott Long     if (i < 0) {
620d69c9c78SScott Long 	ciss_printf(sc, "unknown adapter type\n");
621d69c9c78SScott Long 	return (ENXIO);
622d69c9c78SScott Long     }
623d69c9c78SScott Long 
624d69c9c78SScott Long     if (ciss_vendor_data[i].flags & CISS_BOARD_SA5) {
625d69c9c78SScott Long 	sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5;
626d69c9c78SScott Long     } else if (ciss_vendor_data[i].flags & CISS_BOARD_SA5B) {
627d69c9c78SScott Long 	sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5B;
628d69c9c78SScott Long     } else {
629d69c9c78SScott Long 	/*
630d69c9c78SScott Long 	 * XXX Big hammer, masks/unmasks all possible interrupts.  This should
631d69c9c78SScott Long 	 * work on all hardware variants.  Need to add code to handle the
632b790c193SPedro F. Giffuni 	 * "controller crashed" interrupt bit that this unmasks.
633d69c9c78SScott Long 	 */
634d69c9c78SScott Long 	sqmask = ~0;
635d69c9c78SScott Long     }
636d69c9c78SScott Long 
637d69c9c78SScott Long     /*
6383a31b7ebSMike Smith      * Allocate register window first (we need this to find the config
6393a31b7ebSMike Smith      * struct).
6403a31b7ebSMike Smith      */
6413a31b7ebSMike Smith     error = ENXIO;
6423a31b7ebSMike Smith     sc->ciss_regs_rid = CISS_TL_SIMPLE_BAR_REGS;
6433a31b7ebSMike Smith     if ((sc->ciss_regs_resource =
6445f96beb9SNate Lawson 	 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
6455f96beb9SNate Lawson 				&sc->ciss_regs_rid, RF_ACTIVE)) == NULL) {
6463a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate register window\n");
6473a31b7ebSMike Smith 	return(ENXIO);
6483a31b7ebSMike Smith     }
6493a31b7ebSMike Smith     sc->ciss_regs_bhandle = rman_get_bushandle(sc->ciss_regs_resource);
6503a31b7ebSMike Smith     sc->ciss_regs_btag = rman_get_bustag(sc->ciss_regs_resource);
6513a31b7ebSMike Smith 
6523a31b7ebSMike Smith     /*
6533a31b7ebSMike Smith      * Find the BAR holding the config structure.  If it's not the one
6543a31b7ebSMike Smith      * we already mapped for registers, map it too.
6553a31b7ebSMike Smith      */
6563a31b7ebSMike Smith     sc->ciss_cfg_rid = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_BAR) & 0xffff;
6573a31b7ebSMike Smith     if (sc->ciss_cfg_rid != sc->ciss_regs_rid) {
6583a31b7ebSMike Smith 	if ((sc->ciss_cfg_resource =
6595f96beb9SNate Lawson 	     bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
6605f96beb9SNate Lawson 				    &sc->ciss_cfg_rid, RF_ACTIVE)) == NULL) {
6613a31b7ebSMike Smith 	    ciss_printf(sc, "can't allocate config window\n");
6623a31b7ebSMike Smith 	    return(ENXIO);
6633a31b7ebSMike Smith 	}
6643a31b7ebSMike Smith 	cbase = (uintptr_t)rman_get_virtual(sc->ciss_cfg_resource);
6653a31b7ebSMike Smith 	csize = rman_get_end(sc->ciss_cfg_resource) -
6663a31b7ebSMike Smith 	    rman_get_start(sc->ciss_cfg_resource) + 1;
6673a31b7ebSMike Smith     } else {
6683a31b7ebSMike Smith 	cbase = (uintptr_t)rman_get_virtual(sc->ciss_regs_resource);
6693a31b7ebSMike Smith 	csize = rman_get_end(sc->ciss_regs_resource) -
6703a31b7ebSMike Smith 	    rman_get_start(sc->ciss_regs_resource) + 1;
6713a31b7ebSMike Smith     }
6723a31b7ebSMike Smith     cofs = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_OFF);
6733a31b7ebSMike Smith 
6743a31b7ebSMike Smith     /*
6753a31b7ebSMike Smith      * Use the base/size/offset values we just calculated to
6763a31b7ebSMike Smith      * sanity-check the config structure.  If it's OK, point to it.
6773a31b7ebSMike Smith      */
6783a31b7ebSMike Smith     if ((cofs + sizeof(struct ciss_config_table)) > csize) {
6793a31b7ebSMike Smith 	ciss_printf(sc, "config table outside window\n");
6803a31b7ebSMike Smith 	return(ENXIO);
6813a31b7ebSMike Smith     }
6823a31b7ebSMike Smith     sc->ciss_cfg = (struct ciss_config_table *)(cbase + cofs);
6833a31b7ebSMike Smith     debug(1, "config struct at %p", sc->ciss_cfg);
6843a31b7ebSMike Smith 
6853a31b7ebSMike Smith     /*
68622657ce1SScott Long      * Calculate the number of request structures/commands we are
68722657ce1SScott Long      * going to provide for this adapter.
68822657ce1SScott Long      */
68922657ce1SScott Long     sc->ciss_max_requests = min(CISS_MAX_REQUESTS, sc->ciss_cfg->max_outstanding_commands);
69022657ce1SScott Long 
69122657ce1SScott Long     /*
6923a31b7ebSMike Smith      * Validate the config structure.  If we supported other transport
6933a31b7ebSMike Smith      * methods, we could select amongst them at this point in time.
6943a31b7ebSMike Smith      */
6953a31b7ebSMike Smith     if (strncmp(sc->ciss_cfg->signature, "CISS", 4)) {
6963a31b7ebSMike Smith 	ciss_printf(sc, "config signature mismatch (got '%c%c%c%c')\n",
6973a31b7ebSMike Smith 		    sc->ciss_cfg->signature[0], sc->ciss_cfg->signature[1],
6983a31b7ebSMike Smith 		    sc->ciss_cfg->signature[2], sc->ciss_cfg->signature[3]);
6993a31b7ebSMike Smith 	return(ENXIO);
7003a31b7ebSMike Smith     }
7013a31b7ebSMike Smith 
7023a31b7ebSMike Smith     /*
70322657ce1SScott Long      * Select the mode of operation, prefer Performant.
7043a31b7ebSMike Smith      */
70522657ce1SScott Long     if (!(sc->ciss_cfg->supported_methods &
70622657ce1SScott Long 	(CISS_TRANSPORT_METHOD_SIMPLE | CISS_TRANSPORT_METHOD_PERF))) {
70722657ce1SScott Long 	ciss_printf(sc, "No supported transport layers: 0x%x\n",
70822657ce1SScott Long 	    sc->ciss_cfg->supported_methods);
7093a31b7ebSMike Smith     }
71022657ce1SScott Long 
71122657ce1SScott Long     switch (ciss_force_transport) {
71222657ce1SScott Long     case 1:
71322657ce1SScott Long 	supported_methods = CISS_TRANSPORT_METHOD_SIMPLE;
71422657ce1SScott Long 	break;
71522657ce1SScott Long     case 2:
71622657ce1SScott Long 	supported_methods = CISS_TRANSPORT_METHOD_PERF;
71722657ce1SScott Long 	break;
71822657ce1SScott Long     default:
719ee4827b2SSean Bruno         /*
720ee4827b2SSean Bruno          * Override the capabilities of the BOARD and specify SIMPLE
721ee4827b2SSean Bruno          * MODE
722ee4827b2SSean Bruno          */
723ee4827b2SSean Bruno         if (ciss_vendor_data[i].flags & CISS_BOARD_SIMPLE)
724ee4827b2SSean Bruno                 supported_methods = CISS_TRANSPORT_METHOD_SIMPLE;
725ee4827b2SSean Bruno         else
72622657ce1SScott Long                 supported_methods = sc->ciss_cfg->supported_methods;
72722657ce1SScott Long         break;
72822657ce1SScott Long     }
72922657ce1SScott Long 
73022657ce1SScott Long setup:
7312fdaa90dSScott Long     if ((supported_methods & CISS_TRANSPORT_METHOD_PERF) != 0) {
73222657ce1SScott Long 	method = CISS_TRANSPORT_METHOD_PERF;
73322657ce1SScott Long 	sc->ciss_perf = (struct ciss_perf_config *)(cbase + cofs +
73422657ce1SScott Long 	    sc->ciss_cfg->transport_offset);
73522657ce1SScott Long 	if (ciss_init_perf(sc)) {
73622657ce1SScott Long 	    supported_methods &= ~method;
73722657ce1SScott Long 	    goto setup;
73822657ce1SScott Long 	}
73922657ce1SScott Long     } else if (supported_methods & CISS_TRANSPORT_METHOD_SIMPLE) {
74022657ce1SScott Long 	method = CISS_TRANSPORT_METHOD_SIMPLE;
74122657ce1SScott Long     } else {
74222657ce1SScott Long 	ciss_printf(sc, "No supported transport methods: 0x%x\n",
74322657ce1SScott Long 	    sc->ciss_cfg->supported_methods);
74422657ce1SScott Long 	return(ENXIO);
74522657ce1SScott Long     }
74622657ce1SScott Long 
74722657ce1SScott Long     /*
74822657ce1SScott Long      * Tell it we're using the low 4GB of RAM.  Set the default interrupt
74922657ce1SScott Long      * coalescing options.
75022657ce1SScott Long      */
75122657ce1SScott Long     sc->ciss_cfg->requested_method = method;
7523a31b7ebSMike Smith     sc->ciss_cfg->command_physlimit = 0;
7533a31b7ebSMike Smith     sc->ciss_cfg->interrupt_coalesce_delay = CISS_INTERRUPT_COALESCE_DELAY;
7543a31b7ebSMike Smith     sc->ciss_cfg->interrupt_coalesce_count = CISS_INTERRUPT_COALESCE_COUNT;
7553a31b7ebSMike Smith 
756a891a3bfSPaul Saab #ifdef __i386__
757a891a3bfSPaul Saab     sc->ciss_cfg->host_driver |= CISS_DRIVER_SCSI_PREFETCH;
758a891a3bfSPaul Saab #endif
759a891a3bfSPaul Saab 
7603a31b7ebSMike Smith     if (ciss_update_config(sc)) {
7613a31b7ebSMike Smith 	ciss_printf(sc, "adapter refuses to accept config update (IDBR 0x%x)\n",
7623a31b7ebSMike Smith 		    CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR));
7633a31b7ebSMike Smith 	return(ENXIO);
7643a31b7ebSMike Smith     }
76522657ce1SScott Long     if ((sc->ciss_cfg->active_method & method) == 0) {
76622657ce1SScott Long 	supported_methods &= ~method;
76722657ce1SScott Long 	if (supported_methods == 0) {
76822657ce1SScott Long 	    ciss_printf(sc, "adapter refuses to go into available transports "
76922657ce1SScott Long 		"mode (0x%x, 0x%x)\n", supported_methods,
77022657ce1SScott Long 		sc->ciss_cfg->active_method);
7713a31b7ebSMike Smith 	    return(ENXIO);
77222657ce1SScott Long 	} else
77322657ce1SScott Long 	    goto setup;
7743a31b7ebSMike Smith     }
7753a31b7ebSMike Smith 
7763a31b7ebSMike Smith     /*
7773a31b7ebSMike Smith      * Wait for the adapter to come ready.
7783a31b7ebSMike Smith      */
7793a31b7ebSMike Smith     if ((error = ciss_wait_adapter(sc)) != 0)
7803a31b7ebSMike Smith 	return(error);
7813a31b7ebSMike Smith 
78222657ce1SScott Long     /* Prepare to possibly use MSIX and/or PERFORMANT interrupts.  Normal
78322657ce1SScott Long      * interrupts have a rid of 0, this will be overridden if MSIX is used.
78422657ce1SScott Long      */
78522657ce1SScott Long     sc->ciss_irq_rid[0] = 0;
78622657ce1SScott Long     if (method == CISS_TRANSPORT_METHOD_PERF) {
78722657ce1SScott Long 	ciss_printf(sc, "PERFORMANT Transport\n");
788d69c9c78SScott Long 	if ((ciss_force_interrupt != 1) && (ciss_setup_msix(sc) == 0)) {
78922657ce1SScott Long 	    intr = ciss_perf_msi_intr;
790d69c9c78SScott Long 	} else {
79122657ce1SScott Long 	    intr = ciss_perf_intr;
792d69c9c78SScott Long 	}
793b23be3e0SScott Long 	/* XXX The docs say that the 0x01 bit is only for SAS controllers.
794b23be3e0SScott Long 	 * Unfortunately, there is no good way to know if this is a SAS
795b23be3e0SScott Long 	 * controller.  Hopefully enabling this bit universally will work OK.
796b23be3e0SScott Long 	 * It seems to work fine for SA6i controllers.
797b23be3e0SScott Long 	 */
798b23be3e0SScott Long 	sc->ciss_interrupt_mask = CISS_TL_PERF_INTR_OPQ | CISS_TL_PERF_INTR_MSI;
799b23be3e0SScott Long 
80022657ce1SScott Long     } else {
80122657ce1SScott Long 	ciss_printf(sc, "SIMPLE Transport\n");
80222657ce1SScott Long 	/* MSIX doesn't seem to work in SIMPLE mode, only enable if it forced */
80322657ce1SScott Long 	if (ciss_force_interrupt == 2)
80422657ce1SScott Long 	    /* If this fails, we automatically revert to INTx */
80522657ce1SScott Long 	    ciss_setup_msix(sc);
80622657ce1SScott Long 	sc->ciss_perf = NULL;
80722657ce1SScott Long 	intr = ciss_intr;
808d69c9c78SScott Long 	sc->ciss_interrupt_mask = sqmask;
80922657ce1SScott Long     }
81022657ce1SScott Long 
8113a31b7ebSMike Smith     /*
8123a31b7ebSMike Smith      * Turn off interrupts before we go routing anything.
8133a31b7ebSMike Smith      */
8143a31b7ebSMike Smith     CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
8153a31b7ebSMike Smith 
8163a31b7ebSMike Smith     /*
8173a31b7ebSMike Smith      * Allocate and set up our interrupt.
8183a31b7ebSMike Smith      */
8193a31b7ebSMike Smith     if ((sc->ciss_irq_resource =
82022657ce1SScott Long 	 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_IRQ, &sc->ciss_irq_rid[0],
8213a31b7ebSMike Smith 				RF_ACTIVE | RF_SHAREABLE)) == NULL) {
8223a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate interrupt\n");
8233a31b7ebSMike Smith 	return(ENXIO);
8243a31b7ebSMike Smith     }
82522657ce1SScott Long 
826cc850363SPeter Wemm     if (bus_setup_intr(sc->ciss_dev, sc->ciss_irq_resource,
82722657ce1SScott Long 		       INTR_TYPE_CAM|INTR_MPSAFE, NULL, intr, sc,
8283a31b7ebSMike Smith 		       &sc->ciss_intr)) {
8293a31b7ebSMike Smith 	ciss_printf(sc, "can't set up interrupt\n");
8303a31b7ebSMike Smith 	return(ENXIO);
8313a31b7ebSMike Smith     }
8323a31b7ebSMike Smith 
8333a31b7ebSMike Smith     /*
8343a31b7ebSMike Smith      * Allocate the parent bus DMA tag appropriate for our PCI
8353a31b7ebSMike Smith      * interface.
8363a31b7ebSMike Smith      *
8373a31b7ebSMike Smith      * Note that "simple" adapters can only address within a 32-bit
8383a31b7ebSMike Smith      * span.
8393a31b7ebSMike Smith      */
840b6f97155SScott Long     if (bus_dma_tag_create(bus_get_dma_tag(sc->ciss_dev),/* PCI parent */
8413a31b7ebSMike Smith 			   1, 0, 			/* alignment, boundary */
842639717c8SPaul Saab 			   BUS_SPACE_MAXADDR,		/* lowaddr */
8433a31b7ebSMike Smith 			   BUS_SPACE_MAXADDR, 		/* highaddr */
8443a31b7ebSMike Smith 			   NULL, NULL, 			/* filter, filterarg */
845639717c8SPaul Saab 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
8466f954fb3SAlexander Motin 			   BUS_SPACE_UNRESTRICTED,	/* nsegments */
8473a31b7ebSMike Smith 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
84822657ce1SScott Long 			   0,				/* flags */
849f6b1c44dSScott Long 			   NULL, NULL,			/* lockfunc, lockarg */
8503a31b7ebSMike Smith 			   &sc->ciss_parent_dmat)) {
8513a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate parent DMA tag\n");
8523a31b7ebSMike Smith 	return(ENOMEM);
8533a31b7ebSMike Smith     }
8543a31b7ebSMike Smith 
8553a31b7ebSMike Smith     /*
8563a31b7ebSMike Smith      * Create DMA tag for mapping buffers into adapter-addressable
8573a31b7ebSMike Smith      * space.
8583a31b7ebSMike Smith      */
8593a31b7ebSMike Smith     if (bus_dma_tag_create(sc->ciss_parent_dmat, 	/* parent */
8603a31b7ebSMike Smith 			   1, 0, 			/* alignment, boundary */
8613a31b7ebSMike Smith 			   BUS_SPACE_MAXADDR,		/* lowaddr */
8623a31b7ebSMike Smith 			   BUS_SPACE_MAXADDR, 		/* highaddr */
8633a31b7ebSMike Smith 			   NULL, NULL, 			/* filter, filterarg */
8646f954fb3SAlexander Motin 			   (CISS_MAX_SG_ELEMENTS - 1) * PAGE_SIZE, /* maxsize */
8656f954fb3SAlexander Motin 			   CISS_MAX_SG_ELEMENTS,	/* nsegments */
8663a31b7ebSMike Smith 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
86722657ce1SScott Long 			   BUS_DMA_ALLOCNOW,		/* flags */
868975b7318SScott Long 			   busdma_lock_mutex, &sc->ciss_mtx,	/* lockfunc, lockarg */
8693a31b7ebSMike Smith 			   &sc->ciss_buffer_dmat)) {
8703a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate buffer DMA tag\n");
8713a31b7ebSMike Smith 	return(ENOMEM);
8723a31b7ebSMike Smith     }
8733a31b7ebSMike Smith     return(0);
8743a31b7ebSMike Smith }
8753a31b7ebSMike Smith 
8763a31b7ebSMike Smith /************************************************************************
87722657ce1SScott Long  * Setup MSI/MSIX operation (Performant only)
8782fdaa90dSScott Long  * Four interrupts are available, but we only use 1 right now.  If MSI-X
8792fdaa90dSScott Long  * isn't avaialble, try using MSI instead.
88022657ce1SScott Long  */
88122657ce1SScott Long static int
88222657ce1SScott Long ciss_setup_msix(struct ciss_softc *sc)
88322657ce1SScott Long {
88422657ce1SScott Long     int val, i;
88522657ce1SScott Long 
88622657ce1SScott Long     /* Weed out devices that don't actually support MSI */
8872fdaa90dSScott Long     i = ciss_lookup(sc->ciss_dev);
8882fdaa90dSScott Long     if (ciss_vendor_data[i].flags & CISS_BOARD_NOMSI)
88922657ce1SScott Long 	return (EINVAL);
89022657ce1SScott Long 
8912fdaa90dSScott Long     /*
8922fdaa90dSScott Long      * Only need to use the minimum number of MSI vectors, as the driver
8932fdaa90dSScott Long      * doesn't support directed MSIX interrupts.
8942fdaa90dSScott Long      */
89522657ce1SScott Long     val = pci_msix_count(sc->ciss_dev);
8962fdaa90dSScott Long     if (val < CISS_MSI_COUNT) {
8972fdaa90dSScott Long 	val = pci_msi_count(sc->ciss_dev);
8982fdaa90dSScott Long 	device_printf(sc->ciss_dev, "got %d MSI messages]\n", val);
899b23be3e0SScott Long 	if (val < CISS_MSI_COUNT)
900b23be3e0SScott Long 	    return (EINVAL);
9012fdaa90dSScott Long     }
902b23be3e0SScott Long     val = MIN(val, CISS_MSI_COUNT);
9032fdaa90dSScott Long     if (pci_alloc_msix(sc->ciss_dev, &val) != 0) {
9042fdaa90dSScott Long 	if (pci_alloc_msi(sc->ciss_dev, &val) != 0)
90522657ce1SScott Long 	    return (EINVAL);
9062fdaa90dSScott Long     }
90722657ce1SScott Long 
90822657ce1SScott Long     sc->ciss_msi = val;
9092fdaa90dSScott Long     if (bootverbose)
9102fdaa90dSScott Long 	ciss_printf(sc, "Using %d MSIX interrupt%s\n", val,
9112fdaa90dSScott Long 	    (val != 1) ? "s" : "");
91222657ce1SScott Long 
9132fdaa90dSScott Long     for (i = 0; i < val; i++)
91422657ce1SScott Long 	sc->ciss_irq_rid[i] = i + 1;
91522657ce1SScott Long 
91622657ce1SScott Long     return (0);
91722657ce1SScott Long 
91822657ce1SScott Long }
91922657ce1SScott Long 
92022657ce1SScott Long /************************************************************************
92122657ce1SScott Long  * Setup the Performant structures.
92222657ce1SScott Long  */
92322657ce1SScott Long static int
92422657ce1SScott Long ciss_init_perf(struct ciss_softc *sc)
92522657ce1SScott Long {
92622657ce1SScott Long     struct ciss_perf_config *pc = sc->ciss_perf;
92722657ce1SScott Long     int reply_size;
92822657ce1SScott Long 
92922657ce1SScott Long     /*
93022657ce1SScott Long      * Create the DMA tag for the reply queue.
93122657ce1SScott Long      */
93222657ce1SScott Long     reply_size = sizeof(uint64_t) * sc->ciss_max_requests;
93322657ce1SScott Long     if (bus_dma_tag_create(sc->ciss_parent_dmat,	/* parent */
93422657ce1SScott Long 			   1, 0, 			/* alignment, boundary */
93522657ce1SScott Long 			   BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
93622657ce1SScott Long 			   BUS_SPACE_MAXADDR, 		/* highaddr */
93722657ce1SScott Long 			   NULL, NULL, 			/* filter, filterarg */
93822657ce1SScott Long 			   reply_size, 1,		/* maxsize, nsegments */
93922657ce1SScott Long 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
94022657ce1SScott Long 			   0,				/* flags */
94122657ce1SScott Long 			   NULL, NULL,			/* lockfunc, lockarg */
94222657ce1SScott Long 			   &sc->ciss_reply_dmat)) {
94322657ce1SScott Long 	ciss_printf(sc, "can't allocate reply DMA tag\n");
94422657ce1SScott Long 	return(ENOMEM);
94522657ce1SScott Long     }
94622657ce1SScott Long     /*
94722657ce1SScott Long      * Allocate memory and make it available for DMA.
94822657ce1SScott Long      */
94922657ce1SScott Long     if (bus_dmamem_alloc(sc->ciss_reply_dmat, (void **)&sc->ciss_reply,
95022657ce1SScott Long 			 BUS_DMA_NOWAIT, &sc->ciss_reply_map)) {
95122657ce1SScott Long 	ciss_printf(sc, "can't allocate reply memory\n");
95222657ce1SScott Long 	return(ENOMEM);
95322657ce1SScott Long     }
95422657ce1SScott Long     bus_dmamap_load(sc->ciss_reply_dmat, sc->ciss_reply_map, sc->ciss_reply,
95522657ce1SScott Long 		    reply_size, ciss_command_map_helper, &sc->ciss_reply_phys, 0);
95622657ce1SScott Long     bzero(sc->ciss_reply, reply_size);
95722657ce1SScott Long 
95822657ce1SScott Long     sc->ciss_cycle = 0x1;
95922657ce1SScott Long     sc->ciss_rqidx = 0;
96022657ce1SScott Long 
96122657ce1SScott Long     /*
96222657ce1SScott Long      * Preload the fetch table with common command sizes.  This allows the
96322657ce1SScott Long      * hardware to not waste bus cycles for typical i/o commands, but also not
96422657ce1SScott Long      * tax the driver to be too exact in choosing sizes.  The table is optimized
96522657ce1SScott Long      * for page-aligned i/o's, but since most i/o comes from the various pagers,
96622657ce1SScott Long      * it's a reasonable assumption to make.
96722657ce1SScott Long      */
96822657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_NONE] = (sizeof(struct ciss_command) + 15) / 16;
96922657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_1] =
97022657ce1SScott Long 	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 1 + 15) / 16;
97122657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_2] =
97222657ce1SScott Long 	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 2 + 15) / 16;
97322657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_4] =
97422657ce1SScott Long 	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 4 + 15) / 16;
97522657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_8] =
97622657ce1SScott Long 	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 8 + 15) / 16;
97722657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_16] =
97822657ce1SScott Long 	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 16 + 15) / 16;
97922657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_32] =
98022657ce1SScott Long 	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 32 + 15) / 16;
98122657ce1SScott Long     pc->fetch_count[CISS_SG_FETCH_MAX] = (CISS_COMMAND_ALLOC_SIZE + 15) / 16;
98222657ce1SScott Long 
98322657ce1SScott Long     pc->rq_size = sc->ciss_max_requests; /* XXX less than the card supports? */
98422657ce1SScott Long     pc->rq_count = 1;	/* XXX Hardcode for a single queue */
98522657ce1SScott Long     pc->rq_bank_hi = 0;
98622657ce1SScott Long     pc->rq_bank_lo = 0;
98722657ce1SScott Long     pc->rq[0].rq_addr_hi = 0x0;
98822657ce1SScott Long     pc->rq[0].rq_addr_lo = sc->ciss_reply_phys;
98922657ce1SScott Long 
99022657ce1SScott Long     return(0);
99122657ce1SScott Long }
99222657ce1SScott Long 
99322657ce1SScott Long /************************************************************************
9943a31b7ebSMike Smith  * Wait for the adapter to come ready.
9953a31b7ebSMike Smith  */
9963a31b7ebSMike Smith static int
9973a31b7ebSMike Smith ciss_wait_adapter(struct ciss_softc *sc)
9983a31b7ebSMike Smith {
9993a31b7ebSMike Smith     int		i;
10003a31b7ebSMike Smith 
10013a31b7ebSMike Smith     debug_called(1);
10023a31b7ebSMike Smith 
10033a31b7ebSMike Smith     /*
10043a31b7ebSMike Smith      * Wait for the adapter to come ready.
10053a31b7ebSMike Smith      */
10063a31b7ebSMike Smith     if (!(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY)) {
10073a31b7ebSMike Smith 	ciss_printf(sc, "waiting for adapter to come ready...\n");
10083a31b7ebSMike Smith 	for (i = 0; !(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY); i++) {
10093a31b7ebSMike Smith 	    DELAY(1000000);	/* one second */
10103a31b7ebSMike Smith 	    if (i > 30) {
10113a31b7ebSMike Smith 		ciss_printf(sc, "timed out waiting for adapter to come ready\n");
10123a31b7ebSMike Smith 		return(EIO);
10133a31b7ebSMike Smith 	    }
10143a31b7ebSMike Smith 	}
10153a31b7ebSMike Smith     }
10163a31b7ebSMike Smith     return(0);
10173a31b7ebSMike Smith }
10183a31b7ebSMike Smith 
10193a31b7ebSMike Smith /************************************************************************
10203a31b7ebSMike Smith  * Flush the adapter cache.
10213a31b7ebSMike Smith  */
10223a31b7ebSMike Smith static int
10233a31b7ebSMike Smith ciss_flush_adapter(struct ciss_softc *sc)
10243a31b7ebSMike Smith {
10253a31b7ebSMike Smith     struct ciss_request			*cr;
10263a31b7ebSMike Smith     struct ciss_bmic_flush_cache	*cbfc;
10273a31b7ebSMike Smith     int					error, command_status;
10283a31b7ebSMike Smith 
10293a31b7ebSMike Smith     debug_called(1);
10303a31b7ebSMike Smith 
10313a31b7ebSMike Smith     cr = NULL;
10323a31b7ebSMike Smith     cbfc = NULL;
10333a31b7ebSMike Smith 
10343a31b7ebSMike Smith     /*
10353a31b7ebSMike Smith      * Build a BMIC request to flush the cache.  We don't disable
10363a31b7ebSMike Smith      * it, as we may be going to do more I/O (eg. we are emulating
10373a31b7ebSMike Smith      * the Synchronise Cache command).
10383a31b7ebSMike Smith      */
10393a31b7ebSMike Smith     if ((cbfc = malloc(sizeof(*cbfc), CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
10403a31b7ebSMike Smith 	error = ENOMEM;
10413a31b7ebSMike Smith 	goto out;
10423a31b7ebSMike Smith     }
10433a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_FLUSH_CACHE,
10443a31b7ebSMike Smith 				       (void **)&cbfc, sizeof(*cbfc))) != 0)
10453a31b7ebSMike Smith 	goto out;
10463a31b7ebSMike Smith 
10473a31b7ebSMike Smith     /*
10483a31b7ebSMike Smith      * Submit the request and wait for it to complete.
10493a31b7ebSMike Smith      */
10503a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
10513a31b7ebSMike Smith 	ciss_printf(sc, "error sending BMIC FLUSH_CACHE command (%d)\n", error);
10523a31b7ebSMike Smith 	goto out;
10533a31b7ebSMike Smith     }
10543a31b7ebSMike Smith 
10553a31b7ebSMike Smith     /*
10563a31b7ebSMike Smith      * Check response.
10573a31b7ebSMike Smith      */
10583a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
10593a31b7ebSMike Smith     switch(command_status) {
10603a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:
10613a31b7ebSMike Smith 	break;
10623a31b7ebSMike Smith     default:
10633a31b7ebSMike Smith 	ciss_printf(sc, "error flushing cache (%s)\n",
10643a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
10653a31b7ebSMike Smith 	error = EIO;
10663a31b7ebSMike Smith 	goto out;
10673a31b7ebSMike Smith     }
10683a31b7ebSMike Smith 
10693a31b7ebSMike Smith out:
10703a31b7ebSMike Smith     if (cbfc != NULL)
10713a31b7ebSMike Smith 	free(cbfc, CISS_MALLOC_CLASS);
10723a31b7ebSMike Smith     if (cr != NULL)
10733a31b7ebSMike Smith 	ciss_release_request(cr);
10743a31b7ebSMike Smith     return(error);
10753a31b7ebSMike Smith }
10763a31b7ebSMike Smith 
107717c0792dSPaul Saab static void
107817c0792dSPaul Saab ciss_soft_reset(struct ciss_softc *sc)
107917c0792dSPaul Saab {
108017c0792dSPaul Saab     struct ciss_request		*cr = NULL;
108117c0792dSPaul Saab     struct ciss_command		*cc;
108217c0792dSPaul Saab     int				i, error = 0;
108317c0792dSPaul Saab 
108417c0792dSPaul Saab     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
108517c0792dSPaul Saab 	/* only reset proxy controllers */
108617c0792dSPaul Saab 	if (sc->ciss_controllers[i].physical.bus == 0)
108717c0792dSPaul Saab 	    continue;
108817c0792dSPaul Saab 
108917c0792dSPaul Saab 	if ((error = ciss_get_request(sc, &cr)) != 0)
109017c0792dSPaul Saab 	    break;
109117c0792dSPaul Saab 
109217c0792dSPaul Saab 	if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_SOFT_RESET,
109317c0792dSPaul Saab 					   NULL, 0)) != 0)
109417c0792dSPaul Saab 	    break;
109517c0792dSPaul Saab 
10962fdaa90dSScott Long 	cc = cr->cr_cc;
109717c0792dSPaul Saab 	cc->header.address = sc->ciss_controllers[i];
109817c0792dSPaul Saab 
109917c0792dSPaul Saab 	if ((error = ciss_synch_request(cr, 60 * 1000)) != 0)
110017c0792dSPaul Saab 	    break;
110117c0792dSPaul Saab 
110217c0792dSPaul Saab 	ciss_release_request(cr);
110317c0792dSPaul Saab     }
110417c0792dSPaul Saab 
110517c0792dSPaul Saab     if (error)
110617c0792dSPaul Saab 	ciss_printf(sc, "error resetting controller (%d)\n", error);
110717c0792dSPaul Saab 
110817c0792dSPaul Saab     if (cr != NULL)
110917c0792dSPaul Saab 	ciss_release_request(cr);
111017c0792dSPaul Saab }
111117c0792dSPaul Saab 
11123a31b7ebSMike Smith /************************************************************************
11133a31b7ebSMike Smith  * Allocate memory for the adapter command structures, initialise
11143a31b7ebSMike Smith  * the request structures.
11153a31b7ebSMike Smith  *
11163a31b7ebSMike Smith  * Note that the entire set of commands are allocated in a single
11173a31b7ebSMike Smith  * contiguous slab.
11183a31b7ebSMike Smith  */
11193a31b7ebSMike Smith static int
11203a31b7ebSMike Smith ciss_init_requests(struct ciss_softc *sc)
11213a31b7ebSMike Smith {
11223a31b7ebSMike Smith     struct ciss_request	*cr;
11233a31b7ebSMike Smith     int			i;
11243a31b7ebSMike Smith 
11253a31b7ebSMike Smith     debug_called(1);
11263a31b7ebSMike Smith 
11274bca277bSPaul Saab     if (bootverbose)
11283a31b7ebSMike Smith 	ciss_printf(sc, "using %d of %d available commands\n",
11293a31b7ebSMike Smith 		    sc->ciss_max_requests, sc->ciss_cfg->max_outstanding_commands);
11303a31b7ebSMike Smith 
11313a31b7ebSMike Smith     /*
11323a31b7ebSMike Smith      * Create the DMA tag for commands.
11333a31b7ebSMike Smith      */
11343a31b7ebSMike Smith     if (bus_dma_tag_create(sc->ciss_parent_dmat,	/* parent */
113522657ce1SScott Long 			   32, 0, 			/* alignment, boundary */
1136639717c8SPaul Saab 			   BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
11373a31b7ebSMike Smith 			   BUS_SPACE_MAXADDR, 		/* highaddr */
11383a31b7ebSMike Smith 			   NULL, NULL, 			/* filter, filterarg */
11393a31b7ebSMike Smith 			   CISS_COMMAND_ALLOC_SIZE *
11403a31b7ebSMike Smith 			   sc->ciss_max_requests, 1,	/* maxsize, nsegments */
11413a31b7ebSMike Smith 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
114222657ce1SScott Long 			   0,				/* flags */
1143639717c8SPaul Saab 			   NULL, NULL,			/* lockfunc, lockarg */
11443a31b7ebSMike Smith 			   &sc->ciss_command_dmat)) {
11453a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate command DMA tag\n");
11463a31b7ebSMike Smith 	return(ENOMEM);
11473a31b7ebSMike Smith     }
11483a31b7ebSMike Smith     /*
11493a31b7ebSMike Smith      * Allocate memory and make it available for DMA.
11503a31b7ebSMike Smith      */
11513a31b7ebSMike Smith     if (bus_dmamem_alloc(sc->ciss_command_dmat, (void **)&sc->ciss_command,
11523a31b7ebSMike Smith 			 BUS_DMA_NOWAIT, &sc->ciss_command_map)) {
11533a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate command memory\n");
11543a31b7ebSMike Smith 	return(ENOMEM);
11553a31b7ebSMike Smith     }
11563a31b7ebSMike Smith     bus_dmamap_load(sc->ciss_command_dmat, sc->ciss_command_map,sc->ciss_command,
1157be241f79SPaul Saab 		    CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests,
115822657ce1SScott Long 		    ciss_command_map_helper, &sc->ciss_command_phys, 0);
11593a31b7ebSMike Smith     bzero(sc->ciss_command, CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests);
11603a31b7ebSMike Smith 
11613a31b7ebSMike Smith     /*
11623a31b7ebSMike Smith      * Set up the request and command structures, push requests onto
11633a31b7ebSMike Smith      * the free queue.
11643a31b7ebSMike Smith      */
11653a31b7ebSMike Smith     for (i = 1; i < sc->ciss_max_requests; i++) {
11663a31b7ebSMike Smith 	cr = &sc->ciss_request[i];
11673a31b7ebSMike Smith 	cr->cr_sc = sc;
11683a31b7ebSMike Smith 	cr->cr_tag = i;
11692fdaa90dSScott Long 	cr->cr_cc = (struct ciss_command *)((uintptr_t)sc->ciss_command +
11702fdaa90dSScott Long 	    CISS_COMMAND_ALLOC_SIZE * i);
11712fdaa90dSScott Long 	cr->cr_ccphys = sc->ciss_command_phys + CISS_COMMAND_ALLOC_SIZE * i;
11723284b9eeSPaul Saab 	bus_dmamap_create(sc->ciss_buffer_dmat, 0, &cr->cr_datamap);
11733a31b7ebSMike Smith 	ciss_enqueue_free(cr);
11743a31b7ebSMike Smith     }
11753a31b7ebSMike Smith     return(0);
11763a31b7ebSMike Smith }
11773a31b7ebSMike Smith 
11783a31b7ebSMike Smith static void
11793a31b7ebSMike Smith ciss_command_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
11803a31b7ebSMike Smith {
118122657ce1SScott Long     uint32_t *addr;
11823a31b7ebSMike Smith 
118322657ce1SScott Long     addr = arg;
118422657ce1SScott Long     *addr = segs[0].ds_addr;
11853a31b7ebSMike Smith }
11863a31b7ebSMike Smith 
11873a31b7ebSMike Smith /************************************************************************
11883a31b7ebSMike Smith  * Identify the adapter, print some information about it.
11893a31b7ebSMike Smith  */
11903a31b7ebSMike Smith static int
11913a31b7ebSMike Smith ciss_identify_adapter(struct ciss_softc *sc)
11923a31b7ebSMike Smith {
11933a31b7ebSMike Smith     struct ciss_request	*cr;
11943a31b7ebSMike Smith     int			error, command_status;
11953a31b7ebSMike Smith 
11963a31b7ebSMike Smith     debug_called(1);
11973a31b7ebSMike Smith 
11983a31b7ebSMike Smith     cr = NULL;
11993a31b7ebSMike Smith 
12003a31b7ebSMike Smith     /*
12013a31b7ebSMike Smith      * Get a request, allocate storage for the adapter data.
12023a31b7ebSMike Smith      */
12033a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_CTLR,
12043a31b7ebSMike Smith 				       (void **)&sc->ciss_id,
12053a31b7ebSMike Smith 				       sizeof(*sc->ciss_id))) != 0)
12063a31b7ebSMike Smith 	goto out;
12073a31b7ebSMike Smith 
12083a31b7ebSMike Smith     /*
12093a31b7ebSMike Smith      * Submit the request and wait for it to complete.
12103a31b7ebSMike Smith      */
12113a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
12123a31b7ebSMike Smith 	ciss_printf(sc, "error sending BMIC ID_CTLR command (%d)\n", error);
12133a31b7ebSMike Smith 	goto out;
12143a31b7ebSMike Smith     }
12153a31b7ebSMike Smith 
12163a31b7ebSMike Smith     /*
12173a31b7ebSMike Smith      * Check response.
12183a31b7ebSMike Smith      */
12193a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
12203a31b7ebSMike Smith     switch(command_status) {
12213a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
12223a31b7ebSMike Smith 	break;
12233a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_UNDERRUN:
12243a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_OVERRUN:
12253a31b7ebSMike Smith 	ciss_printf(sc, "data over/underrun reading adapter information\n");
12263a31b7ebSMike Smith     default:
12273a31b7ebSMike Smith 	ciss_printf(sc, "error reading adapter information (%s)\n",
12283a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
12293a31b7ebSMike Smith 	error = EIO;
12303a31b7ebSMike Smith 	goto out;
12313a31b7ebSMike Smith     }
12323a31b7ebSMike Smith 
12333a31b7ebSMike Smith     /* sanity-check reply */
12340fb31ed0SSean Bruno     if (!(sc->ciss_id->controller_flags & CONTROLLER_FLAGS_BIG_MAP_SUPPORT)) {
12353a31b7ebSMike Smith 	ciss_printf(sc, "adapter does not support BIG_MAP\n");
12363a31b7ebSMike Smith 	error = ENXIO;
12373a31b7ebSMike Smith 	goto out;
12383a31b7ebSMike Smith     }
12393a31b7ebSMike Smith 
1240d4aa427fSPaul Saab #if 0
12413a31b7ebSMike Smith     /* XXX later revisions may not need this */
12423a31b7ebSMike Smith     sc->ciss_flags |= CISS_FLAG_FAKE_SYNCH;
1243d4aa427fSPaul Saab #endif
12443a31b7ebSMike Smith 
12453a31b7ebSMike Smith     /* XXX only really required for old 5300 adapters? */
12463a31b7ebSMike Smith     sc->ciss_flags |= CISS_FLAG_BMIC_ABORT;
12473a31b7ebSMike Smith 
1248e3edca22SSean Bruno     /*
1249e3edca22SSean Bruno      * Earlier controller specs do not contain these config
1250e3edca22SSean Bruno      * entries, so assume that a 0 means its old and assign
1251e3edca22SSean Bruno      * these values to the defaults that were established
1252e3edca22SSean Bruno      * when this driver was developed for them
1253e3edca22SSean Bruno      */
1254e3edca22SSean Bruno     if (sc->ciss_cfg->max_logical_supported == 0)
1255e3edca22SSean Bruno         sc->ciss_cfg->max_logical_supported = CISS_MAX_LOGICAL;
1256e3edca22SSean Bruno     if (sc->ciss_cfg->max_physical_supported == 0)
1257e3edca22SSean Bruno 	sc->ciss_cfg->max_physical_supported = CISS_MAX_PHYSICAL;
12583a31b7ebSMike Smith     /* print information */
12594bca277bSPaul Saab     if (bootverbose) {
12603a31b7ebSMike Smith 	ciss_printf(sc, "  %d logical drive%s configured\n",
12613a31b7ebSMike Smith 		    sc->ciss_id->configured_logical_drives,
12623a31b7ebSMike Smith 		    (sc->ciss_id->configured_logical_drives == 1) ? "" : "s");
12633a31b7ebSMike Smith 	ciss_printf(sc, "  firmware %4.4s\n", sc->ciss_id->running_firmware_revision);
12640fb31ed0SSean Bruno 	ciss_printf(sc, "  %d SCSI channels\n", sc->ciss_id->scsi_chip_count);
12653a31b7ebSMike Smith 
12663a31b7ebSMike Smith 	ciss_printf(sc, "  signature '%.4s'\n", sc->ciss_cfg->signature);
12673a31b7ebSMike Smith 	ciss_printf(sc, "  valence %d\n", sc->ciss_cfg->valence);
12683a31b7ebSMike Smith 	ciss_printf(sc, "  supported I/O methods 0x%b\n",
12693a31b7ebSMike Smith 		    sc->ciss_cfg->supported_methods,
12703a31b7ebSMike Smith 		    "\20\1READY\2simple\3performant\4MEMQ\n");
12713a31b7ebSMike Smith 	ciss_printf(sc, "  active I/O method 0x%b\n",
12723a31b7ebSMike Smith 		    sc->ciss_cfg->active_method, "\20\2simple\3performant\4MEMQ\n");
12733a31b7ebSMike Smith 	ciss_printf(sc, "  4G page base 0x%08x\n",
12743a31b7ebSMike Smith 		    sc->ciss_cfg->command_physlimit);
12753a31b7ebSMike Smith 	ciss_printf(sc, "  interrupt coalesce delay %dus\n",
12763a31b7ebSMike Smith 		    sc->ciss_cfg->interrupt_coalesce_delay);
12773a31b7ebSMike Smith 	ciss_printf(sc, "  interrupt coalesce count %d\n",
12783a31b7ebSMike Smith 		    sc->ciss_cfg->interrupt_coalesce_count);
12793a31b7ebSMike Smith 	ciss_printf(sc, "  max outstanding commands %d\n",
12803a31b7ebSMike Smith 		    sc->ciss_cfg->max_outstanding_commands);
12813a31b7ebSMike Smith 	ciss_printf(sc, "  bus types 0x%b\n", sc->ciss_cfg->bus_types,
12823a31b7ebSMike Smith 		    "\20\1ultra2\2ultra3\10fibre1\11fibre2\n");
12833a31b7ebSMike Smith 	ciss_printf(sc, "  server name '%.16s'\n", sc->ciss_cfg->server_name);
12843a31b7ebSMike Smith 	ciss_printf(sc, "  heartbeat 0x%x\n", sc->ciss_cfg->heartbeat);
12855f8cb13cSAlexander Motin 	ciss_printf(sc, "  max logical volumes: %d\n", sc->ciss_cfg->max_logical_supported);
1286e3edca22SSean Bruno     	ciss_printf(sc, "  max physical disks supported: %d\n", sc->ciss_cfg->max_physical_supported);
1287e3edca22SSean Bruno     	ciss_printf(sc, "  max physical disks per logical volume: %d\n", sc->ciss_cfg->max_physical_per_logical);
12880fb31ed0SSean Bruno 	ciss_printf(sc, "  JBOD Support is %s\n", (sc->ciss_id->uiYetMoreControllerFlags & YMORE_CONTROLLER_FLAGS_JBOD_SUPPORTED) ?
12890fb31ed0SSean Bruno 			"Available" : "Unavailable");
12900fb31ed0SSean Bruno 	ciss_printf(sc, "  JBOD Mode is %s\n", (sc->ciss_id->PowerUPNvramFlags & PWR_UP_FLAG_JBOD_ENABLED) ?
12910fb31ed0SSean Bruno 			"Enabled" : "Disabled");
12923a31b7ebSMike Smith     }
12933a31b7ebSMike Smith 
12943a31b7ebSMike Smith out:
12953a31b7ebSMike Smith     if (error) {
12963a31b7ebSMike Smith 	if (sc->ciss_id != NULL) {
12973a31b7ebSMike Smith 	    free(sc->ciss_id, CISS_MALLOC_CLASS);
12983a31b7ebSMike Smith 	    sc->ciss_id = NULL;
12993a31b7ebSMike Smith 	}
13003a31b7ebSMike Smith     }
13013a31b7ebSMike Smith     if (cr != NULL)
13023a31b7ebSMike Smith 	ciss_release_request(cr);
13033a31b7ebSMike Smith     return(error);
13043a31b7ebSMike Smith }
13053a31b7ebSMike Smith 
13063a31b7ebSMike Smith /************************************************************************
1307c6131460SPaul Saab  * Helper routine for generating a list of logical and physical luns.
13083a31b7ebSMike Smith  */
1309c6131460SPaul Saab static struct ciss_lun_report *
1310c6131460SPaul Saab ciss_report_luns(struct ciss_softc *sc, int opcode, int nunits)
13113a31b7ebSMike Smith {
13123a31b7ebSMike Smith     struct ciss_request		*cr;
13133a31b7ebSMike Smith     struct ciss_command		*cc;
13143a31b7ebSMike Smith     struct ciss_report_cdb	*crc;
13153a31b7ebSMike Smith     struct ciss_lun_report	*cll;
13163a31b7ebSMike Smith     int				command_status;
1317c6131460SPaul Saab     int				report_size;
1318c6131460SPaul Saab     int				error = 0;
13193a31b7ebSMike Smith 
13203a31b7ebSMike Smith     debug_called(1);
13213a31b7ebSMike Smith 
13223a31b7ebSMike Smith     cr = NULL;
13233a31b7ebSMike Smith     cll = NULL;
13243a31b7ebSMike Smith 
13253a31b7ebSMike Smith     /*
13263a31b7ebSMike Smith      * Get a request, allocate storage for the address list.
13273a31b7ebSMike Smith      */
13283a31b7ebSMike Smith     if ((error = ciss_get_request(sc, &cr)) != 0)
13293a31b7ebSMike Smith 	goto out;
1330c6131460SPaul Saab     report_size = sizeof(*cll) + nunits * sizeof(union ciss_device_address);
13313a31b7ebSMike Smith     if ((cll = malloc(report_size, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
1332c6131460SPaul Saab 	ciss_printf(sc, "can't allocate memory for lun report\n");
13333a31b7ebSMike Smith 	error = ENOMEM;
13343a31b7ebSMike Smith 	goto out;
13353a31b7ebSMike Smith     }
13363a31b7ebSMike Smith 
13373a31b7ebSMike Smith     /*
1338c6131460SPaul Saab      * Build the Report Logical/Physical LUNs command.
13393a31b7ebSMike Smith      */
13402fdaa90dSScott Long     cc = cr->cr_cc;
13413a31b7ebSMike Smith     cr->cr_data = cll;
13423a31b7ebSMike Smith     cr->cr_length = report_size;
13433a31b7ebSMike Smith     cr->cr_flags = CISS_REQ_DATAIN;
13443a31b7ebSMike Smith 
13453a31b7ebSMike Smith     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
13463a31b7ebSMike Smith     cc->header.address.physical.bus = 0;
13473a31b7ebSMike Smith     cc->header.address.physical.target = 0;
13483a31b7ebSMike Smith     cc->cdb.cdb_length = sizeof(*crc);
13493a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
13503a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
13513a31b7ebSMike Smith     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
13523a31b7ebSMike Smith     cc->cdb.timeout = 30;	/* XXX better suggestions? */
13533a31b7ebSMike Smith 
13543a31b7ebSMike Smith     crc = (struct ciss_report_cdb *)&(cc->cdb.cdb[0]);
13553a31b7ebSMike Smith     bzero(crc, sizeof(*crc));
1356c6131460SPaul Saab     crc->opcode = opcode;
13573a31b7ebSMike Smith     crc->length = htonl(report_size);			/* big-endian field */
13583a31b7ebSMike Smith     cll->list_size = htonl(report_size - sizeof(*cll));	/* big-endian field */
13593a31b7ebSMike Smith 
13603a31b7ebSMike Smith     /*
13613a31b7ebSMike Smith      * Submit the request and wait for it to complete.  (timeout
13623a31b7ebSMike Smith      * here should be much greater than above)
13633a31b7ebSMike Smith      */
13643a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1365c6131460SPaul Saab 	ciss_printf(sc, "error sending %d LUN command (%d)\n", opcode, error);
13663a31b7ebSMike Smith 	goto out;
13673a31b7ebSMike Smith     }
13683a31b7ebSMike Smith 
13693a31b7ebSMike Smith     /*
13703a31b7ebSMike Smith      * Check response.  Note that data over/underrun is OK.
13713a31b7ebSMike Smith      */
13723a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
13733a31b7ebSMike Smith     switch(command_status) {
13743a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:	/* buffer right size */
13753a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_UNDERRUN:	/* buffer too large, not bad */
13763a31b7ebSMike Smith 	break;
13773a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_OVERRUN:
1378c6131460SPaul Saab 	ciss_printf(sc, "WARNING: more units than driver limit (%d)\n",
1379e3edca22SSean Bruno 		    sc->ciss_cfg->max_logical_supported);
13803a31b7ebSMike Smith 	break;
13813a31b7ebSMike Smith     default:
13823a31b7ebSMike Smith 	ciss_printf(sc, "error detecting logical drive configuration (%s)\n",
13833a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
13843a31b7ebSMike Smith 	error = EIO;
13853a31b7ebSMike Smith 	goto out;
13863a31b7ebSMike Smith     }
13873a31b7ebSMike Smith     ciss_release_request(cr);
13883a31b7ebSMike Smith     cr = NULL;
13893a31b7ebSMike Smith 
1390c6131460SPaul Saab out:
1391c6131460SPaul Saab     if (cr != NULL)
1392c6131460SPaul Saab 	ciss_release_request(cr);
1393c6131460SPaul Saab     if (error && cll != NULL) {
1394c6131460SPaul Saab 	free(cll, CISS_MALLOC_CLASS);
1395c6131460SPaul Saab 	cll = NULL;
1396c6131460SPaul Saab     }
1397c6131460SPaul Saab     return(cll);
1398c6131460SPaul Saab }
1399c6131460SPaul Saab 
1400c6131460SPaul Saab /************************************************************************
1401c6131460SPaul Saab  * Find logical drives on the adapter.
1402c6131460SPaul Saab  */
1403c6131460SPaul Saab static int
1404c6131460SPaul Saab ciss_init_logical(struct ciss_softc *sc)
1405c6131460SPaul Saab {
1406c6131460SPaul Saab     struct ciss_lun_report	*cll;
1407c6131460SPaul Saab     int				error = 0, i, j;
1408c6131460SPaul Saab     int				ndrives;
1409c6131460SPaul Saab 
1410c6131460SPaul Saab     debug_called(1);
1411c6131460SPaul Saab 
1412c6131460SPaul Saab     cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
1413e3edca22SSean Bruno 			   sc->ciss_cfg->max_logical_supported);
1414c6131460SPaul Saab     if (cll == NULL) {
1415c6131460SPaul Saab 	error = ENXIO;
1416c6131460SPaul Saab 	goto out;
1417c6131460SPaul Saab     }
1418c6131460SPaul Saab 
14193a31b7ebSMike Smith     /* sanity-check reply */
14203a31b7ebSMike Smith     ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1421e3edca22SSean Bruno     if ((ndrives < 0) || (ndrives > sc->ciss_cfg->max_logical_supported)) {
14223a31b7ebSMike Smith 	ciss_printf(sc, "adapter claims to report absurd number of logical drives (%d > %d)\n",
1423e3edca22SSean Bruno 	    	ndrives, sc->ciss_cfg->max_logical_supported);
1424c6131460SPaul Saab 	error = ENXIO;
1425c6131460SPaul Saab 	goto out;
14263a31b7ebSMike Smith     }
14273a31b7ebSMike Smith 
14283a31b7ebSMike Smith     /*
14293a31b7ebSMike Smith      * Save logical drive information.
14303a31b7ebSMike Smith      */
1431c6131460SPaul Saab     if (bootverbose) {
1432c6131460SPaul Saab 	ciss_printf(sc, "%d logical drive%s\n",
1433c6131460SPaul Saab 	    ndrives, (ndrives > 1 || ndrives == 0) ? "s" : "");
1434c6131460SPaul Saab     }
1435c6131460SPaul Saab 
1436c6131460SPaul Saab     sc->ciss_logical =
1437ac2fffa4SPedro F. Giffuni 	malloc(sc->ciss_max_logical_bus * sizeof(struct ciss_ldrive *),
1438c6131460SPaul Saab 	       CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1439c6131460SPaul Saab     if (sc->ciss_logical == NULL) {
1440c6131460SPaul Saab 	error = ENXIO;
1441c6131460SPaul Saab 	goto out;
1442c6131460SPaul Saab     }
1443c6131460SPaul Saab 
14445eae46afSConrad Meyer     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
1445c6131460SPaul Saab 	sc->ciss_logical[i] =
1446ac2fffa4SPedro F. Giffuni 	    malloc(sc->ciss_cfg->max_logical_supported *
1447e3edca22SSean Bruno 		   sizeof(struct ciss_ldrive),
1448c6131460SPaul Saab 		   CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1449c6131460SPaul Saab 	if (sc->ciss_logical[i] == NULL) {
1450c6131460SPaul Saab 	    error = ENXIO;
1451c6131460SPaul Saab 	    goto out;
1452c6131460SPaul Saab 	}
1453c6131460SPaul Saab 
1454e3edca22SSean Bruno 	for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++)
1455c6131460SPaul Saab 	    sc->ciss_logical[i][j].cl_status = CISS_LD_NONEXISTENT;
1456c6131460SPaul Saab     }
1457c6131460SPaul Saab 
1458e3edca22SSean Bruno     for (i = 0; i < sc->ciss_cfg->max_logical_supported; i++) {
14593a31b7ebSMike Smith 	if (i < ndrives) {
1460c6131460SPaul Saab 	    struct ciss_ldrive	*ld;
1461c6131460SPaul Saab 	    int			bus, target;
1462c6131460SPaul Saab 
1463c6131460SPaul Saab 	    bus		= CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
1464c6131460SPaul Saab 	    target	= CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
1465c6131460SPaul Saab 	    ld		= &sc->ciss_logical[bus][target];
1466c6131460SPaul Saab 
1467c6131460SPaul Saab 	    ld->cl_address	= cll->lun[i];
1468c6131460SPaul Saab 	    ld->cl_controller	= &sc->ciss_controllers[bus];
1469c6131460SPaul Saab 	    if (ciss_identify_logical(sc, ld) != 0)
14703a31b7ebSMike Smith 		continue;
14713a31b7ebSMike Smith 	    /*
14723a31b7ebSMike Smith 	     * If the drive has had media exchanged, we should bring it online.
14733a31b7ebSMike Smith 	     */
1474c6131460SPaul Saab 	    if (ld->cl_lstatus->media_exchanged)
147588c78a38SPaul Saab 		ciss_accept_media(sc, ld);
14763a31b7ebSMike Smith 	}
14773a31b7ebSMike Smith     }
14783a31b7ebSMike Smith 
14793a31b7ebSMike Smith  out:
14803a31b7ebSMike Smith     if (cll != NULL)
14813a31b7ebSMike Smith 	free(cll, CISS_MALLOC_CLASS);
14823a31b7ebSMike Smith     return(error);
14833a31b7ebSMike Smith }
14843a31b7ebSMike Smith 
1485440baa08SPaul Saab static int
1486c6131460SPaul Saab ciss_init_physical(struct ciss_softc *sc)
1487c6131460SPaul Saab {
1488c6131460SPaul Saab     struct ciss_lun_report	*cll;
1489c6131460SPaul Saab     int				error = 0, i;
1490c6131460SPaul Saab     int				nphys;
149179ed7cb3SScott Long     int				bus;
1492c6131460SPaul Saab 
1493c6131460SPaul Saab     debug_called(1);
1494c6131460SPaul Saab 
14951fe6c4eeSScott Long     bus = 0;
14961fe6c4eeSScott Long 
1497c6131460SPaul Saab     cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
1498e3edca22SSean Bruno 			   sc->ciss_cfg->max_physical_supported);
1499c6131460SPaul Saab     if (cll == NULL) {
1500c6131460SPaul Saab 	error = ENXIO;
1501c6131460SPaul Saab 	goto out;
1502c6131460SPaul Saab     }
1503c6131460SPaul Saab 
1504c6131460SPaul Saab     nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1505c6131460SPaul Saab 
1506c6131460SPaul Saab     if (bootverbose) {
1507c6131460SPaul Saab 	ciss_printf(sc, "%d physical device%s\n",
1508c6131460SPaul Saab 	    nphys, (nphys > 1 || nphys == 0) ? "s" : "");
1509c6131460SPaul Saab     }
1510c6131460SPaul Saab 
1511c6131460SPaul Saab     /*
15121fe6c4eeSScott Long      * Figure out the bus mapping.
15131fe6c4eeSScott Long      * Logical buses include both the local logical bus for local arrays and
15141fe6c4eeSScott Long      * proxy buses for remote arrays.  Physical buses are numbered by the
15151fe6c4eeSScott Long      * controller and represent physical buses that hold physical devices.
15161fe6c4eeSScott Long      * We shift these bus numbers so that everything fits into a single flat
15171fe6c4eeSScott Long      * numbering space for CAM.  Logical buses occupy the first 32 CAM bus
15181fe6c4eeSScott Long      * numbers, and the physical bus numbers are shifted to be above that.
15191fe6c4eeSScott Long      * This results in the various driver arrays being indexed as follows:
15201fe6c4eeSScott Long      *
15211fe6c4eeSScott Long      * ciss_controllers[] - indexed by logical bus
15221fe6c4eeSScott Long      * ciss_cam_sim[]     - indexed by both logical and physical, with physical
15231fe6c4eeSScott Long      *                      being shifted by 32.
15241fe6c4eeSScott Long      * ciss_logical[][]   - indexed by logical bus
15251fe6c4eeSScott Long      * ciss_physical[][]  - indexed by physical bus
15261fe6c4eeSScott Long      *
15271fe6c4eeSScott Long      * XXX This is getting more and more hackish.  CISS really doesn't play
15281fe6c4eeSScott Long      *     well with a standard SCSI model; devices are addressed via magic
15291fe6c4eeSScott Long      *     cookies, not via b/t/l addresses.  Since there is no way to store
15301fe6c4eeSScott Long      *     the cookie in the CAM device object, we have to keep these lookup
15311fe6c4eeSScott Long      *     tables handy so that the devices can be found quickly at the cost
15321fe6c4eeSScott Long      *     of wasting memory and having a convoluted lookup scheme.  This
15331fe6c4eeSScott Long      *     driver should probably be converted to block interface.
15341fe6c4eeSScott Long      */
15351fe6c4eeSScott Long     /*
1536c6131460SPaul Saab      * If the L2 and L3 SCSI addresses are 0, this signifies a proxy
1537c6131460SPaul Saab      * controller. A proxy controller is another physical controller
1538c6131460SPaul Saab      * behind the primary PCI controller. We need to know about this
1539c6131460SPaul Saab      * so that BMIC commands can be properly targeted.  There can be
1540c6131460SPaul Saab      * proxy controllers attached to a single PCI controller, so
1541c6131460SPaul Saab      * find the highest numbered one so the array can be properly
1542c6131460SPaul Saab      * sized.
1543c6131460SPaul Saab      */
15441fe6c4eeSScott Long     sc->ciss_max_logical_bus = 1;
1545c6131460SPaul Saab     for (i = 0; i < nphys; i++) {
1546c6131460SPaul Saab 	if (cll->lun[i].physical.extra_address == 0) {
15471fe6c4eeSScott Long 	    bus = cll->lun[i].physical.bus;
15481fe6c4eeSScott Long 	    sc->ciss_max_logical_bus = max(sc->ciss_max_logical_bus, bus) + 1;
15491fe6c4eeSScott Long 	} else {
15501fe6c4eeSScott Long 	    bus = CISS_EXTRA_BUS2(cll->lun[i].physical.extra_address);
15511fe6c4eeSScott Long 	    sc->ciss_max_physical_bus = max(sc->ciss_max_physical_bus, bus);
1552c6131460SPaul Saab 	}
1553c6131460SPaul Saab     }
1554c6131460SPaul Saab 
1555c6131460SPaul Saab     sc->ciss_controllers =
1556ac2fffa4SPedro F. Giffuni 	malloc(sc->ciss_max_logical_bus * sizeof (union ciss_device_address),
1557c6131460SPaul Saab 	       CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1558c6131460SPaul Saab 
1559c6131460SPaul Saab     if (sc->ciss_controllers == NULL) {
1560c6131460SPaul Saab 	ciss_printf(sc, "Could not allocate memory for controller map\n");
1561c6131460SPaul Saab 	error = ENOMEM;
1562c6131460SPaul Saab 	goto out;
1563c6131460SPaul Saab     }
1564c6131460SPaul Saab 
1565c6131460SPaul Saab     /* setup a map of controller addresses */
1566c6131460SPaul Saab     for (i = 0; i < nphys; i++) {
1567c6131460SPaul Saab 	if (cll->lun[i].physical.extra_address == 0) {
1568c6131460SPaul Saab 	    sc->ciss_controllers[cll->lun[i].physical.bus] = cll->lun[i];
1569c6131460SPaul Saab 	}
1570c6131460SPaul Saab     }
1571c6131460SPaul Saab 
15721fe6c4eeSScott Long     sc->ciss_physical =
1573ac2fffa4SPedro F. Giffuni 	malloc(sc->ciss_max_physical_bus * sizeof(struct ciss_pdrive *),
15741fe6c4eeSScott Long 	       CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
15751fe6c4eeSScott Long     if (sc->ciss_physical == NULL) {
15761fe6c4eeSScott Long 	ciss_printf(sc, "Could not allocate memory for physical device map\n");
15771fe6c4eeSScott Long 	error = ENOMEM;
15781fe6c4eeSScott Long 	goto out;
15791fe6c4eeSScott Long     }
15801fe6c4eeSScott Long 
15811fe6c4eeSScott Long     for (i = 0; i < sc->ciss_max_physical_bus; i++) {
15821fe6c4eeSScott Long 	sc->ciss_physical[i] =
15831fe6c4eeSScott Long 	    malloc(sizeof(struct ciss_pdrive) * CISS_MAX_PHYSTGT,
15841fe6c4eeSScott Long 		   CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
15851fe6c4eeSScott Long 	if (sc->ciss_physical[i] == NULL) {
15861fe6c4eeSScott Long 	    ciss_printf(sc, "Could not allocate memory for target map\n");
15871fe6c4eeSScott Long 	    error = ENOMEM;
15881fe6c4eeSScott Long 	    goto out;
15891fe6c4eeSScott Long 	}
15901fe6c4eeSScott Long     }
15911fe6c4eeSScott Long 
15921fe6c4eeSScott Long     ciss_filter_physical(sc, cll);
15931fe6c4eeSScott Long 
1594c6131460SPaul Saab out:
1595c6131460SPaul Saab     if (cll != NULL)
1596c6131460SPaul Saab 	free(cll, CISS_MALLOC_CLASS);
1597c6131460SPaul Saab 
1598c6131460SPaul Saab     return(error);
1599c6131460SPaul Saab }
1600c6131460SPaul Saab 
1601c6131460SPaul Saab static int
16021fe6c4eeSScott Long ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll)
16031fe6c4eeSScott Long {
16041fe6c4eeSScott Long     u_int32_t ea;
16051fe6c4eeSScott Long     int i, nphys;
16061fe6c4eeSScott Long     int	bus, target;
16071fe6c4eeSScott Long 
16081fe6c4eeSScott Long     nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
16091fe6c4eeSScott Long     for (i = 0; i < nphys; i++) {
16101fe6c4eeSScott Long 	if (cll->lun[i].physical.extra_address == 0)
16111fe6c4eeSScott Long 	    continue;
16121fe6c4eeSScott Long 
16131fe6c4eeSScott Long 	/*
16141fe6c4eeSScott Long 	 * Filter out devices that we don't want.  Level 3 LUNs could
16151fe6c4eeSScott Long 	 * probably be supported, but the docs don't give enough of a
16161fe6c4eeSScott Long 	 * hint to know how.
16171fe6c4eeSScott Long 	 *
16181fe6c4eeSScott Long 	 * The mode field of the physical address is likely set to have
16191fe6c4eeSScott Long 	 * hard disks masked out.  Honor it unless the user has overridden
16201fe6c4eeSScott Long 	 * us with the tunable.  We also munge the inquiry data for these
16211fe6c4eeSScott Long 	 * disks so that they only show up as passthrough devices.  Keeping
16221fe6c4eeSScott Long 	 * them visible in this fashion is useful for doing things like
16231fe6c4eeSScott Long 	 * flashing firmware.
16241fe6c4eeSScott Long 	 */
16251fe6c4eeSScott Long 	ea = cll->lun[i].physical.extra_address;
16261fe6c4eeSScott Long 	if ((CISS_EXTRA_BUS3(ea) != 0) || (CISS_EXTRA_TARGET3(ea) != 0) ||
16271fe6c4eeSScott Long 	    (CISS_EXTRA_MODE2(ea) == 0x3))
16281fe6c4eeSScott Long 	    continue;
16291fe6c4eeSScott Long 	if ((ciss_expose_hidden_physical == 0) &&
16301fe6c4eeSScott Long 	   (cll->lun[i].physical.mode == CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL))
16311fe6c4eeSScott Long 	    continue;
16321fe6c4eeSScott Long 
16331fe6c4eeSScott Long 	/*
16341fe6c4eeSScott Long 	 * Note: CISS firmware numbers physical busses starting at '1', not
16351fe6c4eeSScott Long 	 *       '0'.  This numbering is internal to the firmware and is only
16361fe6c4eeSScott Long 	 *       used as a hint here.
16371fe6c4eeSScott Long 	 */
16381fe6c4eeSScott Long 	bus = CISS_EXTRA_BUS2(ea) - 1;
16391fe6c4eeSScott Long 	target = CISS_EXTRA_TARGET2(ea);
16401fe6c4eeSScott Long 	sc->ciss_physical[bus][target].cp_address = cll->lun[i];
16411fe6c4eeSScott Long 	sc->ciss_physical[bus][target].cp_online = 1;
16421fe6c4eeSScott Long     }
16431fe6c4eeSScott Long 
16441fe6c4eeSScott Long     return (0);
16451fe6c4eeSScott Long }
16461fe6c4eeSScott Long 
16471fe6c4eeSScott Long static int
1648440baa08SPaul Saab ciss_inquiry_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1649440baa08SPaul Saab {
1650440baa08SPaul Saab     struct ciss_request			*cr;
1651440baa08SPaul Saab     struct ciss_command			*cc;
1652440baa08SPaul Saab     struct scsi_inquiry			*inq;
1653440baa08SPaul Saab     int					error;
1654440baa08SPaul Saab     int					command_status;
1655440baa08SPaul Saab 
1656440baa08SPaul Saab     cr = NULL;
1657440baa08SPaul Saab 
1658440baa08SPaul Saab     bzero(&ld->cl_geometry, sizeof(ld->cl_geometry));
1659440baa08SPaul Saab 
1660440baa08SPaul Saab     if ((error = ciss_get_request(sc, &cr)) != 0)
1661440baa08SPaul Saab 	goto out;
1662440baa08SPaul Saab 
16632fdaa90dSScott Long     cc = cr->cr_cc;
1664440baa08SPaul Saab     cr->cr_data = &ld->cl_geometry;
1665440baa08SPaul Saab     cr->cr_length = sizeof(ld->cl_geometry);
1666440baa08SPaul Saab     cr->cr_flags = CISS_REQ_DATAIN;
1667440baa08SPaul Saab 
1668c6131460SPaul Saab     cc->header.address = ld->cl_address;
1669440baa08SPaul Saab     cc->cdb.cdb_length = 6;
1670440baa08SPaul Saab     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1671440baa08SPaul Saab     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1672440baa08SPaul Saab     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1673440baa08SPaul Saab     cc->cdb.timeout = 30;
1674440baa08SPaul Saab 
1675440baa08SPaul Saab     inq = (struct scsi_inquiry *)&(cc->cdb.cdb[0]);
1676440baa08SPaul Saab     inq->opcode = INQUIRY;
1677440baa08SPaul Saab     inq->byte2 = SI_EVPD;
1678440baa08SPaul Saab     inq->page_code = CISS_VPD_LOGICAL_DRIVE_GEOMETRY;
1679130f4520SKenneth D. Merry     scsi_ulto2b(sizeof(ld->cl_geometry), inq->length);
1680440baa08SPaul Saab 
1681440baa08SPaul Saab     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1682440baa08SPaul Saab 	ciss_printf(sc, "error getting geometry (%d)\n", error);
1683440baa08SPaul Saab 	goto out;
1684440baa08SPaul Saab     }
1685440baa08SPaul Saab 
1686440baa08SPaul Saab     ciss_report_request(cr, &command_status, NULL);
1687440baa08SPaul Saab     switch(command_status) {
1688440baa08SPaul Saab     case CISS_CMD_STATUS_SUCCESS:
1689440baa08SPaul Saab     case CISS_CMD_STATUS_DATA_UNDERRUN:
1690440baa08SPaul Saab 	break;
1691440baa08SPaul Saab     case CISS_CMD_STATUS_DATA_OVERRUN:
1692440baa08SPaul Saab 	ciss_printf(sc, "WARNING: Data overrun\n");
1693440baa08SPaul Saab 	break;
1694440baa08SPaul Saab     default:
1695440baa08SPaul Saab 	ciss_printf(sc, "Error detecting logical drive geometry (%s)\n",
1696440baa08SPaul Saab 		    ciss_name_command_status(command_status));
1697440baa08SPaul Saab 	break;
1698440baa08SPaul Saab     }
1699440baa08SPaul Saab 
1700440baa08SPaul Saab out:
1701440baa08SPaul Saab     if (cr != NULL)
1702440baa08SPaul Saab 	ciss_release_request(cr);
1703440baa08SPaul Saab     return(error);
1704440baa08SPaul Saab }
17053a31b7ebSMike Smith /************************************************************************
17063a31b7ebSMike Smith  * Identify a logical drive, initialise state related to it.
17073a31b7ebSMike Smith  */
17083a31b7ebSMike Smith static int
17093a31b7ebSMike Smith ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
17103a31b7ebSMike Smith {
17113a31b7ebSMike Smith     struct ciss_request		*cr;
17123a31b7ebSMike Smith     struct ciss_command		*cc;
17133a31b7ebSMike Smith     struct ciss_bmic_cdb	*cbc;
17143a31b7ebSMike Smith     int				error, command_status;
17153a31b7ebSMike Smith 
17163a31b7ebSMike Smith     debug_called(1);
17173a31b7ebSMike Smith 
17183a31b7ebSMike Smith     cr = NULL;
17193a31b7ebSMike Smith 
17203a31b7ebSMike Smith     /*
17213a31b7ebSMike Smith      * Build a BMIC request to fetch the drive ID.
17223a31b7ebSMike Smith      */
17233a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LDRIVE,
17243a31b7ebSMike Smith 				       (void **)&ld->cl_ldrive,
17253a31b7ebSMike Smith 				       sizeof(*ld->cl_ldrive))) != 0)
17263a31b7ebSMike Smith 	goto out;
17272fdaa90dSScott Long     cc = cr->cr_cc;
1728c6131460SPaul Saab     cc->header.address = *ld->cl_controller;	/* target controller */
17293a31b7ebSMike Smith     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1730c6131460SPaul Saab     cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
17313a31b7ebSMike Smith 
17323a31b7ebSMike Smith     /*
17333a31b7ebSMike Smith      * Submit the request and wait for it to complete.
17343a31b7ebSMike Smith      */
17353a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
17363a31b7ebSMike Smith 	ciss_printf(sc, "error sending BMIC LDRIVE command (%d)\n", error);
17373a31b7ebSMike Smith 	goto out;
17383a31b7ebSMike Smith     }
17393a31b7ebSMike Smith 
17403a31b7ebSMike Smith     /*
17413a31b7ebSMike Smith      * Check response.
17423a31b7ebSMike Smith      */
17433a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
17443a31b7ebSMike Smith     switch(command_status) {
17453a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
17463a31b7ebSMike Smith 	break;
17473a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_UNDERRUN:
17483a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_OVERRUN:
17493a31b7ebSMike Smith 	ciss_printf(sc, "data over/underrun reading logical drive ID\n");
17503a31b7ebSMike Smith     default:
17513a31b7ebSMike Smith 	ciss_printf(sc, "error reading logical drive ID (%s)\n",
17523a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
17533a31b7ebSMike Smith 	error = EIO;
17543a31b7ebSMike Smith 	goto out;
17553a31b7ebSMike Smith     }
17563a31b7ebSMike Smith     ciss_release_request(cr);
17573a31b7ebSMike Smith     cr = NULL;
17583a31b7ebSMike Smith 
17593a31b7ebSMike Smith     /*
17603a31b7ebSMike Smith      * Build a CISS BMIC command to get the logical drive status.
17613a31b7ebSMike Smith      */
17623a31b7ebSMike Smith     if ((error = ciss_get_ldrive_status(sc, ld)) != 0)
17633a31b7ebSMike Smith 	goto out;
17643a31b7ebSMike Smith 
17653a31b7ebSMike Smith     /*
1766440baa08SPaul Saab      * Get the logical drive geometry.
1767440baa08SPaul Saab      */
1768440baa08SPaul Saab     if ((error = ciss_inquiry_logical(sc, ld)) != 0)
1769440baa08SPaul Saab 	goto out;
1770440baa08SPaul Saab 
1771440baa08SPaul Saab     /*
17723a31b7ebSMike Smith      * Print the drive's basic characteristics.
17733a31b7ebSMike Smith      */
17744bca277bSPaul Saab     if (bootverbose) {
1775c6131460SPaul Saab 	ciss_printf(sc, "logical drive (b%dt%d): %s, %dMB ",
1776c6131460SPaul Saab 		    CISS_LUN_TO_BUS(ld->cl_address.logical.lun),
1777c6131460SPaul Saab 		    CISS_LUN_TO_TARGET(ld->cl_address.logical.lun),
177879adbf76SPaul Saab 		    ciss_name_ldrive_org(ld->cl_ldrive->fault_tolerance),
17793a31b7ebSMike Smith 		    ((ld->cl_ldrive->blocks_available / (1024 * 1024)) *
17803a31b7ebSMike Smith 		     ld->cl_ldrive->block_size));
17813a31b7ebSMike Smith 
17823a31b7ebSMike Smith 	ciss_print_ldrive(sc, ld);
17833a31b7ebSMike Smith     }
17843a31b7ebSMike Smith out:
17853a31b7ebSMike Smith     if (error != 0) {
17863a31b7ebSMike Smith 	/* make the drive not-exist */
17873a31b7ebSMike Smith 	ld->cl_status = CISS_LD_NONEXISTENT;
17883a31b7ebSMike Smith 	if (ld->cl_ldrive != NULL) {
17893a31b7ebSMike Smith 	    free(ld->cl_ldrive, CISS_MALLOC_CLASS);
17903a31b7ebSMike Smith 	    ld->cl_ldrive = NULL;
17913a31b7ebSMike Smith 	}
17923a31b7ebSMike Smith 	if (ld->cl_lstatus != NULL) {
17933a31b7ebSMike Smith 	    free(ld->cl_lstatus, CISS_MALLOC_CLASS);
17943a31b7ebSMike Smith 	    ld->cl_lstatus = NULL;
17953a31b7ebSMike Smith 	}
17963a31b7ebSMike Smith     }
17973a31b7ebSMike Smith     if (cr != NULL)
17983a31b7ebSMike Smith 	ciss_release_request(cr);
17993a31b7ebSMike Smith 
18003a31b7ebSMike Smith     return(error);
18013a31b7ebSMike Smith }
18023a31b7ebSMike Smith 
18033a31b7ebSMike Smith /************************************************************************
18043a31b7ebSMike Smith  * Get status for a logical drive.
18053a31b7ebSMike Smith  *
18063a31b7ebSMike Smith  * XXX should we also do this in response to Test Unit Ready?
18073a31b7ebSMike Smith  */
18083a31b7ebSMike Smith static int
18093a31b7ebSMike Smith ciss_get_ldrive_status(struct ciss_softc *sc,  struct ciss_ldrive *ld)
18103a31b7ebSMike Smith {
18113a31b7ebSMike Smith     struct ciss_request		*cr;
18123a31b7ebSMike Smith     struct ciss_command		*cc;
18133a31b7ebSMike Smith     struct ciss_bmic_cdb	*cbc;
18143a31b7ebSMike Smith     int				error, command_status;
18153a31b7ebSMike Smith 
18163a31b7ebSMike Smith     /*
18173a31b7ebSMike Smith      * Build a CISS BMIC command to get the logical drive status.
18183a31b7ebSMike Smith      */
18193a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LSTATUS,
18203a31b7ebSMike Smith 				       (void **)&ld->cl_lstatus,
18213a31b7ebSMike Smith 				       sizeof(*ld->cl_lstatus))) != 0)
18223a31b7ebSMike Smith 	goto out;
18232fdaa90dSScott Long     cc = cr->cr_cc;
1824c6131460SPaul Saab     cc->header.address = *ld->cl_controller;	/* target controller */
18253a31b7ebSMike Smith     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1826c6131460SPaul Saab     cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
18273a31b7ebSMike Smith 
18283a31b7ebSMike Smith     /*
18293a31b7ebSMike Smith      * Submit the request and wait for it to complete.
18303a31b7ebSMike Smith      */
18313a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
18323a31b7ebSMike Smith 	ciss_printf(sc, "error sending BMIC LSTATUS command (%d)\n", error);
18333a31b7ebSMike Smith 	goto out;
18343a31b7ebSMike Smith     }
18353a31b7ebSMike Smith 
18363a31b7ebSMike Smith     /*
18373a31b7ebSMike Smith      * Check response.
18383a31b7ebSMike Smith      */
18393a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
18403a31b7ebSMike Smith     switch(command_status) {
18413a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
18423a31b7ebSMike Smith 	break;
18433a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_UNDERRUN:
18443a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_OVERRUN:
18453a31b7ebSMike Smith 	ciss_printf(sc, "data over/underrun reading logical drive status\n");
18463a31b7ebSMike Smith     default:
18473a31b7ebSMike Smith 	ciss_printf(sc, "error reading logical drive status (%s)\n",
18483a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
18493a31b7ebSMike Smith 	error = EIO;
18503a31b7ebSMike Smith 	goto out;
18513a31b7ebSMike Smith     }
18523a31b7ebSMike Smith 
18533a31b7ebSMike Smith     /*
18543a31b7ebSMike Smith      * Set the drive's summary status based on the returned status.
18553a31b7ebSMike Smith      *
18563a31b7ebSMike Smith      * XXX testing shows that a failed JBOD drive comes back at next
18573a31b7ebSMike Smith      * boot in "queued for expansion" mode.  WTF?
18583a31b7ebSMike Smith      */
18593a31b7ebSMike Smith     ld->cl_status = ciss_decode_ldrive_status(ld->cl_lstatus->status);
18603a31b7ebSMike Smith 
18613a31b7ebSMike Smith out:
18623a31b7ebSMike Smith     if (cr != NULL)
18633a31b7ebSMike Smith 	ciss_release_request(cr);
18643a31b7ebSMike Smith     return(error);
18653a31b7ebSMike Smith }
18663a31b7ebSMike Smith 
18673a31b7ebSMike Smith /************************************************************************
18683a31b7ebSMike Smith  * Notify the adapter of a config update.
18693a31b7ebSMike Smith  */
18703a31b7ebSMike Smith static int
18713a31b7ebSMike Smith ciss_update_config(struct ciss_softc *sc)
18723a31b7ebSMike Smith {
18733a31b7ebSMike Smith     int		i;
18743a31b7ebSMike Smith 
18753a31b7ebSMike Smith     debug_called(1);
18763a31b7ebSMike Smith 
18773a31b7ebSMike Smith     CISS_TL_SIMPLE_WRITE(sc, CISS_TL_SIMPLE_IDBR, CISS_TL_SIMPLE_IDBR_CFG_TABLE);
18783a31b7ebSMike Smith     for (i = 0; i < 1000; i++) {
18793a31b7ebSMike Smith 	if (!(CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR) &
18803a31b7ebSMike Smith 	      CISS_TL_SIMPLE_IDBR_CFG_TABLE)) {
18813a31b7ebSMike Smith 	    return(0);
18823a31b7ebSMike Smith 	}
18833a31b7ebSMike Smith 	DELAY(1000);
18843a31b7ebSMike Smith     }
18853a31b7ebSMike Smith     return(1);
18863a31b7ebSMike Smith }
18873a31b7ebSMike Smith 
18883a31b7ebSMike Smith /************************************************************************
18893a31b7ebSMike Smith  * Accept new media into a logical drive.
18903a31b7ebSMike Smith  *
18913a31b7ebSMike Smith  * XXX The drive has previously been offline; it would be good if we
18923a31b7ebSMike Smith  *     could make sure it's not open right now.
18933a31b7ebSMike Smith  */
18943a31b7ebSMike Smith static int
189588c78a38SPaul Saab ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld)
18963a31b7ebSMike Smith {
18973a31b7ebSMike Smith     struct ciss_request		*cr;
18983a31b7ebSMike Smith     struct ciss_command		*cc;
18993a31b7ebSMike Smith     struct ciss_bmic_cdb	*cbc;
190088c78a38SPaul Saab     int				command_status;
190188c78a38SPaul Saab     int				error = 0, ldrive;
1902609caf8dSPaul Saab 
1903609caf8dSPaul Saab     ldrive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
19043a31b7ebSMike Smith 
19053ea59fd3SSean Bruno     debug(0, "bringing logical drive %d back online", ldrive);
19063a31b7ebSMike Smith 
19073a31b7ebSMike Smith     /*
19083a31b7ebSMike Smith      * Build a CISS BMIC command to bring the drive back online.
19093a31b7ebSMike Smith      */
19103a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ACCEPT_MEDIA,
19113a31b7ebSMike Smith 				       NULL, 0)) != 0)
19123a31b7ebSMike Smith 	goto out;
19132fdaa90dSScott Long     cc = cr->cr_cc;
1914c6131460SPaul Saab     cc->header.address = *ld->cl_controller;	/* target controller */
19153a31b7ebSMike Smith     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1916609caf8dSPaul Saab     cbc->log_drive = ldrive;
19173a31b7ebSMike Smith 
19183a31b7ebSMike Smith     /*
19193a31b7ebSMike Smith      * Submit the request and wait for it to complete.
19203a31b7ebSMike Smith      */
19213a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
192288c78a38SPaul Saab 	ciss_printf(sc, "error sending BMIC ACCEPT MEDIA command (%d)\n", error);
19233a31b7ebSMike Smith 	goto out;
19243a31b7ebSMike Smith     }
19253a31b7ebSMike Smith 
19263a31b7ebSMike Smith     /*
19273a31b7ebSMike Smith      * Check response.
19283a31b7ebSMike Smith      */
19293a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
19303a31b7ebSMike Smith     switch(command_status) {
19313a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:		/* all OK */
19323a31b7ebSMike Smith 	/* we should get a logical drive status changed event here */
19333a31b7ebSMike Smith 	break;
19343a31b7ebSMike Smith     default:
19353a31b7ebSMike Smith 	ciss_printf(cr->cr_sc, "error accepting media into failed logical drive (%s)\n",
19363a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
19373a31b7ebSMike Smith 	break;
19383a31b7ebSMike Smith     }
193988c78a38SPaul Saab 
194088c78a38SPaul Saab out:
194188c78a38SPaul Saab     if (cr != NULL)
19423a31b7ebSMike Smith 	ciss_release_request(cr);
194388c78a38SPaul Saab     return(error);
19443a31b7ebSMike Smith }
19453a31b7ebSMike Smith 
19463a31b7ebSMike Smith /************************************************************************
19473a31b7ebSMike Smith  * Release adapter resources.
19483a31b7ebSMike Smith  */
19493a31b7ebSMike Smith static void
19503a31b7ebSMike Smith ciss_free(struct ciss_softc *sc)
19513a31b7ebSMike Smith {
19523284b9eeSPaul Saab     struct ciss_request *cr;
1953ee626b40SPaul Saab     int			i, j;
19543284b9eeSPaul Saab 
19553a31b7ebSMike Smith     debug_called(1);
19563a31b7ebSMike Smith 
19573a31b7ebSMike Smith     /* we're going away */
19583a31b7ebSMike Smith     sc->ciss_flags |= CISS_FLAG_ABORTING;
19593a31b7ebSMike Smith 
19603a31b7ebSMike Smith     /* terminate the periodic heartbeat routine */
19612472e51eSScott Long     callout_stop(&sc->ciss_periodic);
19623a31b7ebSMike Smith 
19633a31b7ebSMike Smith     /* cancel the Event Notify chain */
19643a31b7ebSMike Smith     ciss_notify_abort(sc);
19653a31b7ebSMike Smith 
1966c6131460SPaul Saab     ciss_kill_notify_thread(sc);
1967c6131460SPaul Saab 
19682472e51eSScott Long     /* disconnect from CAM */
19692472e51eSScott Long     if (sc->ciss_cam_sim) {
19702472e51eSScott Long 	for (i = 0; i < sc->ciss_max_logical_bus; i++) {
19712472e51eSScott Long 	    if (sc->ciss_cam_sim[i]) {
19722472e51eSScott Long 		xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
19732472e51eSScott Long 		cam_sim_free(sc->ciss_cam_sim[i], 0);
19742472e51eSScott Long 	    }
19752472e51eSScott Long 	}
19762472e51eSScott Long 	for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
19772472e51eSScott Long 	     CISS_PHYSICAL_BASE; i++) {
19782472e51eSScott Long 	    if (sc->ciss_cam_sim[i]) {
19792472e51eSScott Long 		xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
19802472e51eSScott Long 		cam_sim_free(sc->ciss_cam_sim[i], 0);
19812472e51eSScott Long 	    }
19822472e51eSScott Long 	}
19832472e51eSScott Long 	free(sc->ciss_cam_sim, CISS_MALLOC_CLASS);
19842472e51eSScott Long     }
19852472e51eSScott Long     if (sc->ciss_cam_devq)
19862472e51eSScott Long 	cam_simq_free(sc->ciss_cam_devq);
19872472e51eSScott Long 
198878d03361SPaul Saab     /* remove the control device */
19892472e51eSScott Long     mtx_unlock(&sc->ciss_mtx);
199078d03361SPaul Saab     if (sc->ciss_dev_t != NULL)
199178d03361SPaul Saab 	destroy_dev(sc->ciss_dev_t);
199278d03361SPaul Saab 
19932472e51eSScott Long     /* Final cleanup of the callout. */
19942472e51eSScott Long     callout_drain(&sc->ciss_periodic);
19952472e51eSScott Long     mtx_destroy(&sc->ciss_mtx);
19962472e51eSScott Long 
19973a31b7ebSMike Smith     /* free the controller data */
19983a31b7ebSMike Smith     if (sc->ciss_id != NULL)
19993a31b7ebSMike Smith 	free(sc->ciss_id, CISS_MALLOC_CLASS);
20003a31b7ebSMike Smith 
20013a31b7ebSMike Smith     /* release I/O resources */
20023a31b7ebSMike Smith     if (sc->ciss_regs_resource != NULL)
20033a31b7ebSMike Smith 	bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
20043a31b7ebSMike Smith 			     sc->ciss_regs_rid, sc->ciss_regs_resource);
20053a31b7ebSMike Smith     if (sc->ciss_cfg_resource != NULL)
20063a31b7ebSMike Smith 	bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
20073a31b7ebSMike Smith 			     sc->ciss_cfg_rid, sc->ciss_cfg_resource);
20083a31b7ebSMike Smith     if (sc->ciss_intr != NULL)
20093a31b7ebSMike Smith 	bus_teardown_intr(sc->ciss_dev, sc->ciss_irq_resource, sc->ciss_intr);
20103a31b7ebSMike Smith     if (sc->ciss_irq_resource != NULL)
20113a31b7ebSMike Smith 	bus_release_resource(sc->ciss_dev, SYS_RES_IRQ,
201222657ce1SScott Long 			     sc->ciss_irq_rid[0], sc->ciss_irq_resource);
201322657ce1SScott Long     if (sc->ciss_msi)
201422657ce1SScott Long 	pci_release_msi(sc->ciss_dev);
20153284b9eeSPaul Saab 
20163284b9eeSPaul Saab     while ((cr = ciss_dequeue_free(sc)) != NULL)
20173284b9eeSPaul Saab 	bus_dmamap_destroy(sc->ciss_buffer_dmat, cr->cr_datamap);
20183a31b7ebSMike Smith     if (sc->ciss_buffer_dmat)
20193a31b7ebSMike Smith 	bus_dma_tag_destroy(sc->ciss_buffer_dmat);
20203a31b7ebSMike Smith 
20213a31b7ebSMike Smith     /* destroy command memory and DMA tag */
20223a31b7ebSMike Smith     if (sc->ciss_command != NULL) {
20233a31b7ebSMike Smith 	bus_dmamap_unload(sc->ciss_command_dmat, sc->ciss_command_map);
20243a31b7ebSMike Smith 	bus_dmamem_free(sc->ciss_command_dmat, sc->ciss_command, sc->ciss_command_map);
20253a31b7ebSMike Smith     }
20263284b9eeSPaul Saab     if (sc->ciss_command_dmat)
20273a31b7ebSMike Smith 	bus_dma_tag_destroy(sc->ciss_command_dmat);
20283a31b7ebSMike Smith 
202922657ce1SScott Long     if (sc->ciss_reply) {
203022657ce1SScott Long 	bus_dmamap_unload(sc->ciss_reply_dmat, sc->ciss_reply_map);
203122657ce1SScott Long 	bus_dmamem_free(sc->ciss_reply_dmat, sc->ciss_reply, sc->ciss_reply_map);
203222657ce1SScott Long     }
203322657ce1SScott Long     if (sc->ciss_reply_dmat)
203422657ce1SScott Long 	bus_dma_tag_destroy(sc->ciss_reply_dmat);
203522657ce1SScott Long 
203622657ce1SScott Long     /* destroy DMA tags */
203722657ce1SScott Long     if (sc->ciss_parent_dmat)
203822657ce1SScott Long 	bus_dma_tag_destroy(sc->ciss_parent_dmat);
2039c6131460SPaul Saab     if (sc->ciss_logical) {
20405eae46afSConrad Meyer 	for (i = 0; i < sc->ciss_max_logical_bus; i++) {
2041e3edca22SSean Bruno 	    for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
2042ee626b40SPaul Saab 		if (sc->ciss_logical[i][j].cl_ldrive)
2043ee626b40SPaul Saab 		    free(sc->ciss_logical[i][j].cl_ldrive, CISS_MALLOC_CLASS);
2044ee626b40SPaul Saab 		if (sc->ciss_logical[i][j].cl_lstatus)
2045ee626b40SPaul Saab 		    free(sc->ciss_logical[i][j].cl_lstatus, CISS_MALLOC_CLASS);
2046ee626b40SPaul Saab 	    }
2047c6131460SPaul Saab 	    free(sc->ciss_logical[i], CISS_MALLOC_CLASS);
2048ee626b40SPaul Saab 	}
2049c6131460SPaul Saab 	free(sc->ciss_logical, CISS_MALLOC_CLASS);
2050c6131460SPaul Saab     }
2051c6131460SPaul Saab 
20521fe6c4eeSScott Long     if (sc->ciss_physical) {
20531fe6c4eeSScott Long 	for (i = 0; i < sc->ciss_max_physical_bus; i++)
20541fe6c4eeSScott Long 	    free(sc->ciss_physical[i], CISS_MALLOC_CLASS);
20551fe6c4eeSScott Long 	free(sc->ciss_physical, CISS_MALLOC_CLASS);
20561fe6c4eeSScott Long     }
20571fe6c4eeSScott Long 
2058c6131460SPaul Saab     if (sc->ciss_controllers)
2059c6131460SPaul Saab 	free(sc->ciss_controllers, CISS_MALLOC_CLASS);
2060975b7318SScott Long 
20613a31b7ebSMike Smith }
20623a31b7ebSMike Smith 
20633a31b7ebSMike Smith /************************************************************************
20643a31b7ebSMike Smith  * Give a command to the adapter.
20653a31b7ebSMike Smith  *
20663a31b7ebSMike Smith  * Note that this uses the simple transport layer directly.  If we
20673a31b7ebSMike Smith  * want to add support for other layers, we'll need a switch of some
20683a31b7ebSMike Smith  * sort.
20693a31b7ebSMike Smith  *
20703a31b7ebSMike Smith  * Note that the simple transport layer has no way of refusing a
20713a31b7ebSMike Smith  * command; we only have as many request structures as the adapter
20723a31b7ebSMike Smith  * supports commands, so we don't have to check (this presumes that
20733a31b7ebSMike Smith  * the adapter can handle commands as fast as we throw them at it).
20743a31b7ebSMike Smith  */
20753a31b7ebSMike Smith static int
20763a31b7ebSMike Smith ciss_start(struct ciss_request *cr)
20773a31b7ebSMike Smith {
20783a31b7ebSMike Smith     int			error;
20793a31b7ebSMike Smith 
2080fa911920SScott Long     debug(2, "post command %d tag %d ", cr->cr_tag, cr->cr_cc->header.host_tag);
20813a31b7ebSMike Smith 
20823a31b7ebSMike Smith     /*
20833a31b7ebSMike Smith      * Map the request's data.
20843a31b7ebSMike Smith      */
20853a31b7ebSMike Smith     if ((error = ciss_map_request(cr)))
20863a31b7ebSMike Smith 	return(error);
20873a31b7ebSMike Smith 
20883a31b7ebSMike Smith #if 0
20893a31b7ebSMike Smith     ciss_print_request(cr);
20903a31b7ebSMike Smith #endif
20913a31b7ebSMike Smith 
20923a31b7ebSMike Smith     return(0);
20933a31b7ebSMike Smith }
20943a31b7ebSMike Smith 
20953a31b7ebSMike Smith /************************************************************************
20963a31b7ebSMike Smith  * Fetch completed request(s) from the adapter, queue them for
20973a31b7ebSMike Smith  * completion handling.
20983a31b7ebSMike Smith  *
20993a31b7ebSMike Smith  * Note that this uses the simple transport layer directly.  If we
21003a31b7ebSMike Smith  * want to add support for other layers, we'll need a switch of some
21013a31b7ebSMike Smith  * sort.
21023a31b7ebSMike Smith  *
21033a31b7ebSMike Smith  * Note that the simple transport mechanism does not require any
21043a31b7ebSMike Smith  * reentrancy protection; the OPQ read is atomic.  If there is a
21053a31b7ebSMike Smith  * chance of a race with something else that might move the request
21063a31b7ebSMike Smith  * off the busy list, then we will have to lock against that
21073a31b7ebSMike Smith  * (eg. timeouts, etc.)
21083a31b7ebSMike Smith  */
21093a31b7ebSMike Smith static void
211022657ce1SScott Long ciss_done(struct ciss_softc *sc, cr_qhead_t *qh)
21113a31b7ebSMike Smith {
21123a31b7ebSMike Smith     struct ciss_request	*cr;
21133a31b7ebSMike Smith     struct ciss_command	*cc;
21143a31b7ebSMike Smith     u_int32_t		tag, index;
21153a31b7ebSMike Smith 
21163a31b7ebSMike Smith     debug_called(3);
21173a31b7ebSMike Smith 
21183a31b7ebSMike Smith     /*
21193a31b7ebSMike Smith      * Loop quickly taking requests from the adapter and moving them
212022657ce1SScott Long      * to the completed queue.
21213a31b7ebSMike Smith      */
21223a31b7ebSMike Smith     for (;;) {
21233a31b7ebSMike Smith 	tag = CISS_TL_SIMPLE_FETCH_CMD(sc);
21243a31b7ebSMike Smith 	if (tag == CISS_TL_SIMPLE_OPQ_EMPTY)
21253a31b7ebSMike Smith 	    break;
21263a31b7ebSMike Smith 	index = tag >> 2;
21273a31b7ebSMike Smith 	debug(2, "completed command %d%s", index,
21283a31b7ebSMike Smith 	      (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
21293a31b7ebSMike Smith 	if (index >= sc->ciss_max_requests) {
21303a31b7ebSMike Smith 	    ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
21313a31b7ebSMike Smith 	    continue;
21323a31b7ebSMike Smith 	}
21333a31b7ebSMike Smith 	cr = &(sc->ciss_request[index]);
21342fdaa90dSScott Long 	cc = cr->cr_cc;
21353a31b7ebSMike Smith 	cc->header.host_tag = tag;	/* not updated by adapter */
213622657ce1SScott Long 	ciss_enqueue_complete(cr, qh);
21373a31b7ebSMike Smith     }
21383a31b7ebSMike Smith 
213922657ce1SScott Long }
214022657ce1SScott Long 
214122657ce1SScott Long static void
214222657ce1SScott Long ciss_perf_done(struct ciss_softc *sc, cr_qhead_t *qh)
214322657ce1SScott Long {
214422657ce1SScott Long     struct ciss_request	*cr;
214522657ce1SScott Long     struct ciss_command	*cc;
214622657ce1SScott Long     u_int32_t		tag, index;
214722657ce1SScott Long 
214822657ce1SScott Long     debug_called(3);
214922657ce1SScott Long 
21503a31b7ebSMike Smith     /*
215122657ce1SScott Long      * Loop quickly taking requests from the adapter and moving them
215222657ce1SScott Long      * to the completed queue.
21533a31b7ebSMike Smith      */
215422657ce1SScott Long     for (;;) {
215522657ce1SScott Long 	tag = sc->ciss_reply[sc->ciss_rqidx];
215622657ce1SScott Long 	if ((tag & CISS_CYCLE_MASK) != sc->ciss_cycle)
215722657ce1SScott Long 	    break;
215822657ce1SScott Long 	index = tag >> 2;
215922657ce1SScott Long 	debug(2, "completed command %d%s\n", index,
216022657ce1SScott Long 	      (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
216122657ce1SScott Long 	if (index < sc->ciss_max_requests) {
216222657ce1SScott Long 	    cr = &(sc->ciss_request[index]);
21632fdaa90dSScott Long 	    cc = cr->cr_cc;
216422657ce1SScott Long 	    cc->header.host_tag = tag;	/* not updated by adapter */
216522657ce1SScott Long 	    ciss_enqueue_complete(cr, qh);
216622657ce1SScott Long 	} else {
216722657ce1SScott Long 	    ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
216822657ce1SScott Long 	}
216922657ce1SScott Long 	if (++sc->ciss_rqidx == sc->ciss_max_requests) {
217022657ce1SScott Long 	    sc->ciss_rqidx = 0;
217122657ce1SScott Long 	    sc->ciss_cycle ^= 1;
217222657ce1SScott Long 	}
217322657ce1SScott Long     }
217422657ce1SScott Long 
21753a31b7ebSMike Smith }
21763a31b7ebSMike Smith 
21773a31b7ebSMike Smith /************************************************************************
21783a31b7ebSMike Smith  * Take an interrupt from the adapter.
21793a31b7ebSMike Smith  */
21803a31b7ebSMike Smith static void
21813a31b7ebSMike Smith ciss_intr(void *arg)
21823a31b7ebSMike Smith {
218322657ce1SScott Long     cr_qhead_t qh;
21843a31b7ebSMike Smith     struct ciss_softc	*sc = (struct ciss_softc *)arg;
21853a31b7ebSMike Smith 
21863a31b7ebSMike Smith     /*
21873a31b7ebSMike Smith      * The only interrupt we recognise indicates that there are
21883a31b7ebSMike Smith      * entries in the outbound post queue.
21893a31b7ebSMike Smith      */
219022657ce1SScott Long     STAILQ_INIT(&qh);
219122657ce1SScott Long     ciss_done(sc, &qh);
2192975b7318SScott Long     mtx_lock(&sc->ciss_mtx);
219322657ce1SScott Long     ciss_complete(sc, &qh);
2194975b7318SScott Long     mtx_unlock(&sc->ciss_mtx);
21953a31b7ebSMike Smith }
21963a31b7ebSMike Smith 
219722657ce1SScott Long static void
219822657ce1SScott Long ciss_perf_intr(void *arg)
219922657ce1SScott Long {
220022657ce1SScott Long     struct ciss_softc	*sc = (struct ciss_softc *)arg;
220122657ce1SScott Long 
220222657ce1SScott Long     /* Clear the interrupt and flush the bridges.  Docs say that the flush
220322657ce1SScott Long      * needs to be done twice, which doesn't seem right.
220422657ce1SScott Long      */
220522657ce1SScott Long     CISS_TL_PERF_CLEAR_INT(sc);
220622657ce1SScott Long     CISS_TL_PERF_FLUSH_INT(sc);
220722657ce1SScott Long 
220822657ce1SScott Long     ciss_perf_msi_intr(sc);
220922657ce1SScott Long }
221022657ce1SScott Long 
221122657ce1SScott Long static void
221222657ce1SScott Long ciss_perf_msi_intr(void *arg)
221322657ce1SScott Long {
221422657ce1SScott Long     cr_qhead_t qh;
221522657ce1SScott Long     struct ciss_softc	*sc = (struct ciss_softc *)arg;
221622657ce1SScott Long 
221722657ce1SScott Long     STAILQ_INIT(&qh);
221822657ce1SScott Long     ciss_perf_done(sc, &qh);
221922657ce1SScott Long     mtx_lock(&sc->ciss_mtx);
222022657ce1SScott Long     ciss_complete(sc, &qh);
222122657ce1SScott Long     mtx_unlock(&sc->ciss_mtx);
222222657ce1SScott Long }
222322657ce1SScott Long 
22243a31b7ebSMike Smith /************************************************************************
22253a31b7ebSMike Smith  * Process completed requests.
22263a31b7ebSMike Smith  *
22273a31b7ebSMike Smith  * Requests can be completed in three fashions:
22283a31b7ebSMike Smith  *
22293a31b7ebSMike Smith  * - by invoking a callback function (cr_complete is non-null)
22303a31b7ebSMike Smith  * - by waking up a sleeper (cr_flags has CISS_REQ_SLEEP set)
22313a31b7ebSMike Smith  * - by clearing the CISS_REQ_POLL flag in interrupt/timeout context
22323a31b7ebSMike Smith  */
22333a31b7ebSMike Smith static void
223422657ce1SScott Long ciss_complete(struct ciss_softc *sc, cr_qhead_t *qh)
22353a31b7ebSMike Smith {
22363a31b7ebSMike Smith     struct ciss_request	*cr;
22373a31b7ebSMike Smith 
22383a31b7ebSMike Smith     debug_called(2);
22393a31b7ebSMike Smith 
22403a31b7ebSMike Smith     /*
22413a31b7ebSMike Smith      * Loop taking requests off the completed queue and performing
22423a31b7ebSMike Smith      * completion processing on them.
22433a31b7ebSMike Smith      */
22443a31b7ebSMike Smith     for (;;) {
224522657ce1SScott Long 	if ((cr = ciss_dequeue_complete(sc, qh)) == NULL)
22463a31b7ebSMike Smith 	    break;
22473a31b7ebSMike Smith 	ciss_unmap_request(cr);
22483a31b7ebSMike Smith 
224922657ce1SScott Long 	if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
225022657ce1SScott Long 	    ciss_printf(sc, "WARNING: completing non-busy request\n");
225122657ce1SScott Long 	cr->cr_flags &= ~CISS_REQ_BUSY;
225222657ce1SScott Long 
22533a31b7ebSMike Smith 	/*
22543a31b7ebSMike Smith 	 * If the request has a callback, invoke it.
22553a31b7ebSMike Smith 	 */
22563a31b7ebSMike Smith 	if (cr->cr_complete != NULL) {
22573a31b7ebSMike Smith 	    cr->cr_complete(cr);
22583a31b7ebSMike Smith 	    continue;
22593a31b7ebSMike Smith 	}
22603a31b7ebSMike Smith 
22613a31b7ebSMike Smith 	/*
22623a31b7ebSMike Smith 	 * If someone is sleeping on this request, wake them up.
22633a31b7ebSMike Smith 	 */
22643a31b7ebSMike Smith 	if (cr->cr_flags & CISS_REQ_SLEEP) {
22653a31b7ebSMike Smith 	    cr->cr_flags &= ~CISS_REQ_SLEEP;
22663a31b7ebSMike Smith 	    wakeup(cr);
22673a31b7ebSMike Smith 	    continue;
22683a31b7ebSMike Smith 	}
22693a31b7ebSMike Smith 
22703a31b7ebSMike Smith 	/*
22713a31b7ebSMike Smith 	 * If someone is polling this request for completion, signal.
22723a31b7ebSMike Smith 	 */
22733a31b7ebSMike Smith 	if (cr->cr_flags & CISS_REQ_POLL) {
22743a31b7ebSMike Smith 	    cr->cr_flags &= ~CISS_REQ_POLL;
22753a31b7ebSMike Smith 	    continue;
22763a31b7ebSMike Smith 	}
22773a31b7ebSMike Smith 
22783a31b7ebSMike Smith 	/*
22793a31b7ebSMike Smith 	 * Give up and throw the request back on the free queue.  This
22803a31b7ebSMike Smith 	 * should never happen; resources will probably be lost.
22813a31b7ebSMike Smith 	 */
22823a31b7ebSMike Smith 	ciss_printf(sc, "WARNING: completed command with no submitter\n");
22833a31b7ebSMike Smith 	ciss_enqueue_free(cr);
22843a31b7ebSMike Smith     }
22853a31b7ebSMike Smith }
22863a31b7ebSMike Smith 
22873a31b7ebSMike Smith /************************************************************************
22883a31b7ebSMike Smith  * Report on the completion status of a request, and pass back SCSI
22893a31b7ebSMike Smith  * and command status values.
22903a31b7ebSMike Smith  */
22913a31b7ebSMike Smith static int
229222657ce1SScott Long _ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status, const char *func)
22933a31b7ebSMike Smith {
22943a31b7ebSMike Smith     struct ciss_command		*cc;
22953a31b7ebSMike Smith     struct ciss_error_info	*ce;
22963a31b7ebSMike Smith 
22973a31b7ebSMike Smith     debug_called(2);
22983a31b7ebSMike Smith 
22992fdaa90dSScott Long     cc = cr->cr_cc;
23003a31b7ebSMike Smith     ce = (struct ciss_error_info *)&(cc->sg[0]);
23013a31b7ebSMike Smith 
23023a31b7ebSMike Smith     /*
23033a31b7ebSMike Smith      * We don't consider data under/overrun an error for the Report
23043a31b7ebSMike Smith      * Logical/Physical LUNs commands.
23053a31b7ebSMike Smith      */
23063a31b7ebSMike Smith     if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) &&
23072514c5bfSPaul Saab 	((ce->command_status == CISS_CMD_STATUS_DATA_OVERRUN) ||
23082514c5bfSPaul Saab 	 (ce->command_status == CISS_CMD_STATUS_DATA_UNDERRUN)) &&
23093a31b7ebSMike Smith 	((cc->cdb.cdb[0] == CISS_OPCODE_REPORT_LOGICAL_LUNS) ||
23102514c5bfSPaul Saab 	 (cc->cdb.cdb[0] == CISS_OPCODE_REPORT_PHYSICAL_LUNS) ||
23112514c5bfSPaul Saab 	 (cc->cdb.cdb[0] == INQUIRY))) {
23123a31b7ebSMike Smith 	cc->header.host_tag &= ~CISS_HDR_HOST_TAG_ERROR;
23133a31b7ebSMike Smith 	debug(2, "ignoring irrelevant under/overrun error");
23143a31b7ebSMike Smith     }
23153a31b7ebSMike Smith 
23163a31b7ebSMike Smith     /*
23173a31b7ebSMike Smith      * Check the command's error bit, if clear, there's no status and
23183a31b7ebSMike Smith      * everything is OK.
23193a31b7ebSMike Smith      */
23203a31b7ebSMike Smith     if (!(cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR)) {
23213a31b7ebSMike Smith 	if (scsi_status != NULL)
23223a31b7ebSMike Smith 	    *scsi_status = SCSI_STATUS_OK;
23233a31b7ebSMike Smith 	if (command_status != NULL)
23243a31b7ebSMike Smith 	    *command_status = CISS_CMD_STATUS_SUCCESS;
23253a31b7ebSMike Smith 	return(0);
23263a31b7ebSMike Smith     } else {
23273a31b7ebSMike Smith 	if (command_status != NULL)
23283a31b7ebSMike Smith 	    *command_status = ce->command_status;
23293a31b7ebSMike Smith 	if (scsi_status != NULL) {
2330e8144a13SAlexander Motin 	    if (ce->command_status == CISS_CMD_STATUS_DATA_UNDERRUN) {
2331e8144a13SAlexander Motin 		*scsi_status = SCSI_STATUS_OK;
2332e8144a13SAlexander Motin 	    } else if (ce->command_status == CISS_CMD_STATUS_TARGET_STATUS) {
23333a31b7ebSMike Smith 		*scsi_status = ce->scsi_status;
23343a31b7ebSMike Smith 	    } else {
23353a31b7ebSMike Smith 		*scsi_status = -1;
23363a31b7ebSMike Smith 	    }
23373a31b7ebSMike Smith 	}
2338e8144a13SAlexander Motin 	if (bootverbose && ce->command_status != CISS_CMD_STATUS_DATA_UNDERRUN)
23393a31b7ebSMike Smith 	    ciss_printf(cr->cr_sc, "command status 0x%x (%s) scsi status 0x%x\n",
23403a31b7ebSMike Smith 			ce->command_status, ciss_name_command_status(ce->command_status),
23413a31b7ebSMike Smith 			ce->scsi_status);
23423a31b7ebSMike Smith 	if (ce->command_status == CISS_CMD_STATUS_INVALID_COMMAND) {
234322657ce1SScott Long 	    ciss_printf(cr->cr_sc, "invalid command, offense size %d at %d, value 0x%x, function %s\n",
23443a31b7ebSMike Smith 			ce->additional_error_info.invalid_command.offense_size,
23453a31b7ebSMike Smith 			ce->additional_error_info.invalid_command.offense_offset,
234622657ce1SScott Long 			ce->additional_error_info.invalid_command.offense_value,
234722657ce1SScott Long 			func);
23483a31b7ebSMike Smith 	}
23493a31b7ebSMike Smith     }
2350c6131460SPaul Saab #if 0
2351c6131460SPaul Saab     ciss_print_request(cr);
2352c6131460SPaul Saab #endif
23533a31b7ebSMike Smith     return(1);
23543a31b7ebSMike Smith }
23553a31b7ebSMike Smith 
23563a31b7ebSMike Smith /************************************************************************
23573a31b7ebSMike Smith  * Issue a request and don't return until it's completed.
23583a31b7ebSMike Smith  *
23593a31b7ebSMike Smith  * Depending on adapter status, we may poll or sleep waiting for
23603a31b7ebSMike Smith  * completion.
23613a31b7ebSMike Smith  */
23623a31b7ebSMike Smith static int
23633a31b7ebSMike Smith ciss_synch_request(struct ciss_request *cr, int timeout)
23643a31b7ebSMike Smith {
23653a31b7ebSMike Smith     if (cr->cr_sc->ciss_flags & CISS_FLAG_RUNNING) {
23663a31b7ebSMike Smith 	return(ciss_wait_request(cr, timeout));
23673a31b7ebSMike Smith     } else {
23683a31b7ebSMike Smith 	return(ciss_poll_request(cr, timeout));
23693a31b7ebSMike Smith     }
23703a31b7ebSMike Smith }
23713a31b7ebSMike Smith 
23723a31b7ebSMike Smith /************************************************************************
23733a31b7ebSMike Smith  * Issue a request and poll for completion.
23743a31b7ebSMike Smith  *
23753a31b7ebSMike Smith  * Timeout in milliseconds.
23763a31b7ebSMike Smith  */
23773a31b7ebSMike Smith static int
23783a31b7ebSMike Smith ciss_poll_request(struct ciss_request *cr, int timeout)
23793a31b7ebSMike Smith {
238022657ce1SScott Long     cr_qhead_t qh;
238122657ce1SScott Long     struct ciss_softc *sc;
23823a31b7ebSMike Smith     int		error;
23833a31b7ebSMike Smith 
23843a31b7ebSMike Smith     debug_called(2);
23853a31b7ebSMike Smith 
238622657ce1SScott Long     STAILQ_INIT(&qh);
238722657ce1SScott Long     sc = cr->cr_sc;
23883a31b7ebSMike Smith     cr->cr_flags |= CISS_REQ_POLL;
23893a31b7ebSMike Smith     if ((error = ciss_start(cr)) != 0)
23903a31b7ebSMike Smith 	return(error);
23913a31b7ebSMike Smith 
23923a31b7ebSMike Smith     do {
239322657ce1SScott Long 	if (sc->ciss_perf)
239422657ce1SScott Long 	    ciss_perf_done(sc, &qh);
239522657ce1SScott Long 	else
239622657ce1SScott Long 	    ciss_done(sc, &qh);
239722657ce1SScott Long 	ciss_complete(sc, &qh);
23983a31b7ebSMike Smith 	if (!(cr->cr_flags & CISS_REQ_POLL))
23993a31b7ebSMike Smith 	    return(0);
24003a31b7ebSMike Smith 	DELAY(1000);
24013a31b7ebSMike Smith     } while (timeout-- >= 0);
24023a31b7ebSMike Smith     return(EWOULDBLOCK);
24033a31b7ebSMike Smith }
24043a31b7ebSMike Smith 
24053a31b7ebSMike Smith /************************************************************************
24063a31b7ebSMike Smith  * Issue a request and sleep waiting for completion.
24073a31b7ebSMike Smith  *
24083a31b7ebSMike Smith  * Timeout in milliseconds.  Note that a spurious wakeup will reset
24093a31b7ebSMike Smith  * the timeout.
24103a31b7ebSMike Smith  */
24113a31b7ebSMike Smith static int
24123a31b7ebSMike Smith ciss_wait_request(struct ciss_request *cr, int timeout)
24133a31b7ebSMike Smith {
241422657ce1SScott Long     int		error;
24153a31b7ebSMike Smith 
24163a31b7ebSMike Smith     debug_called(2);
24173a31b7ebSMike Smith 
24183a31b7ebSMike Smith     cr->cr_flags |= CISS_REQ_SLEEP;
24193a31b7ebSMike Smith     if ((error = ciss_start(cr)) != 0)
24203a31b7ebSMike Smith 	return(error);
24213a31b7ebSMike Smith 
242240f05b02SPaul Saab     while ((cr->cr_flags & CISS_REQ_SLEEP) && (error != EWOULDBLOCK)) {
242385c9dd9dSSteven Hartland 	error = msleep_sbt(cr, &cr->cr_sc->ciss_mtx, PRIBIO, "cissREQ",
242485c9dd9dSSteven Hartland 	    SBT_1MS * timeout, 0, 0);
24253a31b7ebSMike Smith     }
24263a31b7ebSMike Smith     return(error);
24273a31b7ebSMike Smith }
24283a31b7ebSMike Smith 
24296197ca15SPeter Wemm #if 0
24303a31b7ebSMike Smith /************************************************************************
24313a31b7ebSMike Smith  * Abort a request.  Note that a potential exists here to race the
24323a31b7ebSMike Smith  * request being completed; the caller must deal with this.
24333a31b7ebSMike Smith  */
24343a31b7ebSMike Smith static int
24353a31b7ebSMike Smith ciss_abort_request(struct ciss_request *ar)
24363a31b7ebSMike Smith {
24373a31b7ebSMike Smith     struct ciss_request		*cr;
24383a31b7ebSMike Smith     struct ciss_command		*cc;
24393a31b7ebSMike Smith     struct ciss_message_cdb	*cmc;
24403a31b7ebSMike Smith     int				error;
24413a31b7ebSMike Smith 
24423a31b7ebSMike Smith     debug_called(1);
24433a31b7ebSMike Smith 
24443a31b7ebSMike Smith     /* get a request */
24453a31b7ebSMike Smith     if ((error = ciss_get_request(ar->cr_sc, &cr)) != 0)
24463a31b7ebSMike Smith 	return(error);
24473a31b7ebSMike Smith 
24483a31b7ebSMike Smith     /* build the abort command */
24492fdaa90dSScott Long     cc = cr->cr_cc;
24503a31b7ebSMike Smith     cc->header.address.mode.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;	/* addressing? */
24513a31b7ebSMike Smith     cc->header.address.physical.target = 0;
24523a31b7ebSMike Smith     cc->header.address.physical.bus = 0;
24533a31b7ebSMike Smith     cc->cdb.cdb_length = sizeof(*cmc);
24543a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
24553a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
24563a31b7ebSMike Smith     cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
24573a31b7ebSMike Smith     cc->cdb.timeout = 30;
24583a31b7ebSMike Smith 
24593a31b7ebSMike Smith     cmc = (struct ciss_message_cdb *)&(cc->cdb.cdb[0]);
24603a31b7ebSMike Smith     cmc->opcode = CISS_OPCODE_MESSAGE_ABORT;
24613a31b7ebSMike Smith     cmc->type = CISS_MESSAGE_ABORT_TASK;
24623a31b7ebSMike Smith     cmc->abort_tag = ar->cr_tag;	/* endianness?? */
24633a31b7ebSMike Smith 
24643a31b7ebSMike Smith     /*
24653a31b7ebSMike Smith      * Send the request and wait for a response.  If we believe we
24663a31b7ebSMike Smith      * aborted the request OK, clear the flag that indicates it's
24673a31b7ebSMike Smith      * running.
24683a31b7ebSMike Smith      */
24693a31b7ebSMike Smith     error = ciss_synch_request(cr, 35 * 1000);
24703a31b7ebSMike Smith     if (!error)
24713a31b7ebSMike Smith 	error = ciss_report_request(cr, NULL, NULL);
24723a31b7ebSMike Smith     ciss_release_request(cr);
24733a31b7ebSMike Smith 
24743a31b7ebSMike Smith     return(error);
24753a31b7ebSMike Smith }
24766197ca15SPeter Wemm #endif
24773a31b7ebSMike Smith 
24783a31b7ebSMike Smith /************************************************************************
24793a31b7ebSMike Smith  * Fetch and initialise a request
24803a31b7ebSMike Smith  */
24813a31b7ebSMike Smith static int
24823a31b7ebSMike Smith ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp)
24833a31b7ebSMike Smith {
24843a31b7ebSMike Smith     struct ciss_request *cr;
24853a31b7ebSMike Smith 
24863a31b7ebSMike Smith     debug_called(2);
24873a31b7ebSMike Smith 
24883a31b7ebSMike Smith     /*
24893a31b7ebSMike Smith      * Get a request and clean it up.
24903a31b7ebSMike Smith      */
24913a31b7ebSMike Smith     if ((cr = ciss_dequeue_free(sc)) == NULL)
24923a31b7ebSMike Smith 	return(ENOMEM);
24933a31b7ebSMike Smith 
24943a31b7ebSMike Smith     cr->cr_data = NULL;
24953a31b7ebSMike Smith     cr->cr_flags = 0;
24963a31b7ebSMike Smith     cr->cr_complete = NULL;
2497639717c8SPaul Saab     cr->cr_private = NULL;
249822657ce1SScott Long     cr->cr_sg_tag = CISS_SG_MAX;	/* Backstop to prevent accidents */
24993a31b7ebSMike Smith 
25003a31b7ebSMike Smith     ciss_preen_command(cr);
25013a31b7ebSMike Smith     *crp = cr;
25023a31b7ebSMike Smith     return(0);
25033a31b7ebSMike Smith }
25043a31b7ebSMike Smith 
25053a31b7ebSMike Smith static void
25063a31b7ebSMike Smith ciss_preen_command(struct ciss_request *cr)
25073a31b7ebSMike Smith {
25083a31b7ebSMike Smith     struct ciss_command	*cc;
25093a31b7ebSMike Smith     u_int32_t		cmdphys;
25103a31b7ebSMike Smith 
25113a31b7ebSMike Smith     /*
25123a31b7ebSMike Smith      * Clean up the command structure.
25133a31b7ebSMike Smith      *
25143a31b7ebSMike Smith      * Note that we set up the error_info structure here, since the
25153a31b7ebSMike Smith      * length can be overwritten by any command.
25163a31b7ebSMike Smith      */
25172fdaa90dSScott Long     cc = cr->cr_cc;
25183a31b7ebSMike Smith     cc->header.sg_in_list = 0;		/* kinda inefficient this way */
25193a31b7ebSMike Smith     cc->header.sg_total = 0;
25203a31b7ebSMike Smith     cc->header.host_tag = cr->cr_tag << 2;
25213a31b7ebSMike Smith     cc->header.host_tag_zeroes = 0;
25223d3ddb44SSean Bruno     bzero(&(cc->sg[0]), CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command));
25232fdaa90dSScott Long     cmdphys = cr->cr_ccphys;
25243a31b7ebSMike Smith     cc->error_info.error_info_address = cmdphys + sizeof(struct ciss_command);
25253a31b7ebSMike Smith     cc->error_info.error_info_length = CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command);
25263a31b7ebSMike Smith }
25273a31b7ebSMike Smith 
25283a31b7ebSMike Smith /************************************************************************
25293a31b7ebSMike Smith  * Release a request to the free list.
25303a31b7ebSMike Smith  */
25313a31b7ebSMike Smith static void
25323a31b7ebSMike Smith ciss_release_request(struct ciss_request *cr)
25333a31b7ebSMike Smith {
25343a31b7ebSMike Smith 
25353a31b7ebSMike Smith     debug_called(2);
25363a31b7ebSMike Smith 
25373a31b7ebSMike Smith     /* release the request to the free queue */
25383a31b7ebSMike Smith     ciss_requeue_free(cr);
25393a31b7ebSMike Smith }
25403a31b7ebSMike Smith 
25413a31b7ebSMike Smith /************************************************************************
25423a31b7ebSMike Smith  * Allocate a request that will be used to send a BMIC command.  Do some
25433a31b7ebSMike Smith  * of the common setup here to avoid duplicating it everywhere else.
25443a31b7ebSMike Smith  */
25453a31b7ebSMike Smith static int
25463a31b7ebSMike Smith ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
25473a31b7ebSMike Smith 		      int opcode, void **bufp, size_t bufsize)
25483a31b7ebSMike Smith {
25493a31b7ebSMike Smith     struct ciss_request		*cr;
25503a31b7ebSMike Smith     struct ciss_command		*cc;
25513a31b7ebSMike Smith     struct ciss_bmic_cdb	*cbc;
25523a31b7ebSMike Smith     void			*buf;
25533a31b7ebSMike Smith     int				error;
25543a31b7ebSMike Smith     int				dataout;
25553a31b7ebSMike Smith 
25563a31b7ebSMike Smith     debug_called(2);
25573a31b7ebSMike Smith 
25583a31b7ebSMike Smith     cr = NULL;
25593a31b7ebSMike Smith     buf = NULL;
25603a31b7ebSMike Smith 
25613a31b7ebSMike Smith     /*
25623a31b7ebSMike Smith      * Get a request.
25633a31b7ebSMike Smith      */
25643a31b7ebSMike Smith     if ((error = ciss_get_request(sc, &cr)) != 0)
25653a31b7ebSMike Smith 	goto out;
25663a31b7ebSMike Smith 
25673a31b7ebSMike Smith     /*
25683a31b7ebSMike Smith      * Allocate data storage if requested, determine the data direction.
25693a31b7ebSMike Smith      */
25703a31b7ebSMike Smith     dataout = 0;
25713a31b7ebSMike Smith     if ((bufsize > 0) && (bufp != NULL)) {
25723a31b7ebSMike Smith 	if (*bufp == NULL) {
25733a31b7ebSMike Smith 	    if ((buf = malloc(bufsize, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
25743a31b7ebSMike Smith 		error = ENOMEM;
25753a31b7ebSMike Smith 		goto out;
25763a31b7ebSMike Smith 	    }
25773a31b7ebSMike Smith 	} else {
25783a31b7ebSMike Smith 	    buf = *bufp;
25793a31b7ebSMike Smith 	    dataout = 1;	/* we are given a buffer, so we are writing */
25803a31b7ebSMike Smith 	}
25813a31b7ebSMike Smith     }
25823a31b7ebSMike Smith 
25833a31b7ebSMike Smith     /*
25843a31b7ebSMike Smith      * Build a CISS BMIC command to get the logical drive ID.
25853a31b7ebSMike Smith      */
25863a31b7ebSMike Smith     cr->cr_data = buf;
25873a31b7ebSMike Smith     cr->cr_length = bufsize;
25883a31b7ebSMike Smith     if (!dataout)
25893a31b7ebSMike Smith 	cr->cr_flags = CISS_REQ_DATAIN;
25903a31b7ebSMike Smith 
25912fdaa90dSScott Long     cc = cr->cr_cc;
25923a31b7ebSMike Smith     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
25933a31b7ebSMike Smith     cc->header.address.physical.bus = 0;
25943a31b7ebSMike Smith     cc->header.address.physical.target = 0;
25953a31b7ebSMike Smith     cc->cdb.cdb_length = sizeof(*cbc);
25963a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
25973a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
25983a31b7ebSMike Smith     cc->cdb.direction = dataout ? CISS_CDB_DIRECTION_WRITE : CISS_CDB_DIRECTION_READ;
25993a31b7ebSMike Smith     cc->cdb.timeout = 0;
26003a31b7ebSMike Smith 
26013a31b7ebSMike Smith     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
26023a31b7ebSMike Smith     bzero(cbc, sizeof(*cbc));
26033a31b7ebSMike Smith     cbc->opcode = dataout ? CISS_ARRAY_CONTROLLER_WRITE : CISS_ARRAY_CONTROLLER_READ;
26043a31b7ebSMike Smith     cbc->bmic_opcode = opcode;
26053a31b7ebSMike Smith     cbc->size = htons((u_int16_t)bufsize);
26063a31b7ebSMike Smith 
26073a31b7ebSMike Smith out:
26083a31b7ebSMike Smith     if (error) {
26093a31b7ebSMike Smith 	if (cr != NULL)
26103a31b7ebSMike Smith 	    ciss_release_request(cr);
26113a31b7ebSMike Smith     } else {
26123a31b7ebSMike Smith 	*crp = cr;
26133a31b7ebSMike Smith 	if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL))
26143a31b7ebSMike Smith 	    *bufp = buf;
26153a31b7ebSMike Smith     }
26163a31b7ebSMike Smith     return(error);
26173a31b7ebSMike Smith }
26183a31b7ebSMike Smith 
26193a31b7ebSMike Smith /************************************************************************
26203a31b7ebSMike Smith  * Handle a command passed in from userspace.
26213a31b7ebSMike Smith  */
26223a31b7ebSMike Smith static int
26233a31b7ebSMike Smith ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc)
26243a31b7ebSMike Smith {
26253a31b7ebSMike Smith     struct ciss_request		*cr;
26263a31b7ebSMike Smith     struct ciss_command		*cc;
26273a31b7ebSMike Smith     struct ciss_error_info	*ce;
26283c63a8b4SPaul Saab     int				error = 0;
26293a31b7ebSMike Smith 
26303a31b7ebSMike Smith     debug_called(1);
26313a31b7ebSMike Smith 
26323a31b7ebSMike Smith     cr = NULL;
26333a31b7ebSMike Smith 
26343a31b7ebSMike Smith     /*
26353a31b7ebSMike Smith      * Get a request.
26363a31b7ebSMike Smith      */
2637f83695f5SPaul Saab     while (ciss_get_request(sc, &cr) != 0)
2638975b7318SScott Long 	msleep(sc, &sc->ciss_mtx, PPAUSE, "cissREQ", hz);
26392fdaa90dSScott Long     cc = cr->cr_cc;
26403a31b7ebSMike Smith 
26413a31b7ebSMike Smith     /*
26423a31b7ebSMike Smith      * Allocate an in-kernel databuffer if required, copy in user data.
26433a31b7ebSMike Smith      */
2644744c0d23SScott Long     mtx_unlock(&sc->ciss_mtx);
26453a31b7ebSMike Smith     cr->cr_length = ioc->buf_size;
26463a31b7ebSMike Smith     if (ioc->buf_size > 0) {
2647975b7318SScott Long 	if ((cr->cr_data = malloc(ioc->buf_size, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
26483a31b7ebSMike Smith 	    error = ENOMEM;
2649744c0d23SScott Long 	    goto out_unlocked;
26503a31b7ebSMike Smith 	}
26513a31b7ebSMike Smith 	if ((error = copyin(ioc->buf, cr->cr_data, ioc->buf_size))) {
26523a31b7ebSMike Smith 	    debug(0, "copyin: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2653744c0d23SScott Long 	    goto out_unlocked;
26543a31b7ebSMike Smith 	}
26553a31b7ebSMike Smith     }
26563a31b7ebSMike Smith 
26573a31b7ebSMike Smith     /*
26583a31b7ebSMike Smith      * Build the request based on the user command.
26593a31b7ebSMike Smith      */
26603a31b7ebSMike Smith     bcopy(&ioc->LUN_info, &cc->header.address, sizeof(cc->header.address));
26613a31b7ebSMike Smith     bcopy(&ioc->Request, &cc->cdb, sizeof(cc->cdb));
26623a31b7ebSMike Smith 
26633a31b7ebSMike Smith     /* XXX anything else to populate here? */
2664744c0d23SScott Long     mtx_lock(&sc->ciss_mtx);
26653a31b7ebSMike Smith 
26663a31b7ebSMike Smith     /*
26673a31b7ebSMike Smith      * Run the command.
26683a31b7ebSMike Smith      */
26693a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000))) {
26703a31b7ebSMike Smith 	debug(0, "request failed - %d", error);
26713a31b7ebSMike Smith 	goto out;
26723a31b7ebSMike Smith     }
26733a31b7ebSMike Smith 
26743a31b7ebSMike Smith     /*
26753c63a8b4SPaul Saab      * Check to see if the command succeeded.
26763a31b7ebSMike Smith      */
26773a31b7ebSMike Smith     ce = (struct ciss_error_info *)&(cc->sg[0]);
26783c6b8353SPaul Saab     if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) == 0)
26793c63a8b4SPaul Saab 	bzero(ce, sizeof(*ce));
26803c63a8b4SPaul Saab 
26813c63a8b4SPaul Saab     /*
26823c63a8b4SPaul Saab      * Copy the results back to the user.
26833c63a8b4SPaul Saab      */
26843a31b7ebSMike Smith     bcopy(ce, &ioc->error_info, sizeof(*ce));
2685744c0d23SScott Long     mtx_unlock(&sc->ciss_mtx);
26863a31b7ebSMike Smith     if ((ioc->buf_size > 0) &&
26873a31b7ebSMike Smith 	(error = copyout(cr->cr_data, ioc->buf, ioc->buf_size))) {
26883a31b7ebSMike Smith 	debug(0, "copyout: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2689744c0d23SScott Long 	goto out_unlocked;
26903a31b7ebSMike Smith     }
26913a31b7ebSMike Smith 
26923a31b7ebSMike Smith     /* done OK */
26933a31b7ebSMike Smith     error = 0;
26943a31b7ebSMike Smith 
2695744c0d23SScott Long out_unlocked:
2696744c0d23SScott Long     mtx_lock(&sc->ciss_mtx);
2697744c0d23SScott Long 
26983a31b7ebSMike Smith out:
26993a31b7ebSMike Smith     if ((cr != NULL) && (cr->cr_data != NULL))
27003a31b7ebSMike Smith 	free(cr->cr_data, CISS_MALLOC_CLASS);
27013a31b7ebSMike Smith     if (cr != NULL)
27023a31b7ebSMike Smith 	ciss_release_request(cr);
27033a31b7ebSMike Smith     return(error);
27043a31b7ebSMike Smith }
27053a31b7ebSMike Smith 
27063a31b7ebSMike Smith /************************************************************************
27073a31b7ebSMike Smith  * Map a request into bus-visible space, initialise the scatter/gather
27083a31b7ebSMike Smith  * list.
27093a31b7ebSMike Smith  */
27103a31b7ebSMike Smith static int
27113a31b7ebSMike Smith ciss_map_request(struct ciss_request *cr)
27123a31b7ebSMike Smith {
27133a31b7ebSMike Smith     struct ciss_softc	*sc;
2714639717c8SPaul Saab     int			error = 0;
27153a31b7ebSMike Smith 
27163a31b7ebSMike Smith     debug_called(2);
27173a31b7ebSMike Smith 
27183a31b7ebSMike Smith     sc = cr->cr_sc;
27193a31b7ebSMike Smith 
27203a31b7ebSMike Smith     /* check that mapping is necessary */
2721639717c8SPaul Saab     if (cr->cr_flags & CISS_REQ_MAPPED)
27223a31b7ebSMike Smith 	return(0);
27233a31b7ebSMike Smith 
27243a31b7ebSMike Smith     cr->cr_flags |= CISS_REQ_MAPPED;
2725639717c8SPaul Saab 
2726639717c8SPaul Saab     bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2727639717c8SPaul Saab 		    BUS_DMASYNC_PREWRITE);
2728639717c8SPaul Saab 
2729639717c8SPaul Saab     if (cr->cr_data != NULL) {
2730dd0b4fb6SKonstantin Belousov 	if (cr->cr_flags & CISS_REQ_CCB)
2731dd0b4fb6SKonstantin Belousov 		error = bus_dmamap_load_ccb(sc->ciss_buffer_dmat,
2732dd0b4fb6SKonstantin Belousov 					cr->cr_datamap, cr->cr_data,
2733dd0b4fb6SKonstantin Belousov 					ciss_request_map_helper, cr, 0);
2734dd0b4fb6SKonstantin Belousov 	else
2735639717c8SPaul Saab 		error = bus_dmamap_load(sc->ciss_buffer_dmat, cr->cr_datamap,
2736639717c8SPaul Saab 					cr->cr_data, cr->cr_length,
2737639717c8SPaul Saab 					ciss_request_map_helper, cr, 0);
2738639717c8SPaul Saab 	if (error != 0)
2739639717c8SPaul Saab 	    return (error);
2740639717c8SPaul Saab     } else {
2741639717c8SPaul Saab 	/*
2742639717c8SPaul Saab 	 * Post the command to the adapter.
2743639717c8SPaul Saab 	 */
274422657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_NONE;
274522657ce1SScott Long 	cr->cr_flags |= CISS_REQ_BUSY;
274622657ce1SScott Long 	if (sc->ciss_perf)
274722657ce1SScott Long 	    CISS_TL_PERF_POST_CMD(sc, cr);
274822657ce1SScott Long 	else
27492fdaa90dSScott Long 	    CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
2750639717c8SPaul Saab     }
2751639717c8SPaul Saab 
27523a31b7ebSMike Smith     return(0);
27533a31b7ebSMike Smith }
27543a31b7ebSMike Smith 
27553a31b7ebSMike Smith static void
27563a31b7ebSMike Smith ciss_request_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
27573a31b7ebSMike Smith {
27583a31b7ebSMike Smith     struct ciss_command	*cc;
2759639717c8SPaul Saab     struct ciss_request *cr;
2760639717c8SPaul Saab     struct ciss_softc	*sc;
27613a31b7ebSMike Smith     int			i;
27623a31b7ebSMike Smith 
27633a31b7ebSMike Smith     debug_called(2);
27643a31b7ebSMike Smith 
2765639717c8SPaul Saab     cr = (struct ciss_request *)arg;
2766639717c8SPaul Saab     sc = cr->cr_sc;
27672fdaa90dSScott Long     cc = cr->cr_cc;
2768639717c8SPaul Saab 
27693a31b7ebSMike Smith     for (i = 0; i < nseg; i++) {
27703a31b7ebSMike Smith 	cc->sg[i].address = segs[i].ds_addr;
27713a31b7ebSMike Smith 	cc->sg[i].length = segs[i].ds_len;
27723a31b7ebSMike Smith 	cc->sg[i].extension = 0;
27733a31b7ebSMike Smith     }
27743a31b7ebSMike Smith     /* we leave the s/g table entirely within the command */
27753a31b7ebSMike Smith     cc->header.sg_in_list = nseg;
27763a31b7ebSMike Smith     cc->header.sg_total = nseg;
2777639717c8SPaul Saab 
2778639717c8SPaul Saab     if (cr->cr_flags & CISS_REQ_DATAIN)
2779639717c8SPaul Saab 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREREAD);
2780639717c8SPaul Saab     if (cr->cr_flags & CISS_REQ_DATAOUT)
2781639717c8SPaul Saab 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREWRITE);
2782639717c8SPaul Saab 
278362fdee1dSScott Long     if (nseg == 0)
278422657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_NONE;
278522657ce1SScott Long     else if (nseg == 1)
278622657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_1;
278722657ce1SScott Long     else if (nseg == 2)
278822657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_2;
278922657ce1SScott Long     else if (nseg <= 4)
279022657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_4;
279122657ce1SScott Long     else if (nseg <= 8)
279222657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_8;
279322657ce1SScott Long     else if (nseg <= 16)
279422657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_16;
279522657ce1SScott Long     else if (nseg <= 32)
279622657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_32;
279722657ce1SScott Long     else
279822657ce1SScott Long 	cr->cr_sg_tag = CISS_SG_MAX;
279922657ce1SScott Long 
2800639717c8SPaul Saab     /*
2801639717c8SPaul Saab      * Post the command to the adapter.
2802639717c8SPaul Saab      */
280322657ce1SScott Long     cr->cr_flags |= CISS_REQ_BUSY;
280422657ce1SScott Long     if (sc->ciss_perf)
280522657ce1SScott Long 	CISS_TL_PERF_POST_CMD(sc, cr);
280622657ce1SScott Long     else
28072fdaa90dSScott Long 	CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
28083a31b7ebSMike Smith }
28093a31b7ebSMike Smith 
28103a31b7ebSMike Smith /************************************************************************
28113a31b7ebSMike Smith  * Unmap a request from bus-visible space.
28123a31b7ebSMike Smith  */
28133a31b7ebSMike Smith static void
28143a31b7ebSMike Smith ciss_unmap_request(struct ciss_request *cr)
28153a31b7ebSMike Smith {
28163a31b7ebSMike Smith     struct ciss_softc	*sc;
28173a31b7ebSMike Smith 
28183a31b7ebSMike Smith     debug_called(2);
28193a31b7ebSMike Smith 
28203a31b7ebSMike Smith     sc = cr->cr_sc;
28213a31b7ebSMike Smith 
28223a31b7ebSMike Smith     /* check that unmapping is necessary */
2823639717c8SPaul Saab     if ((cr->cr_flags & CISS_REQ_MAPPED) == 0)
28243a31b7ebSMike Smith 	return;
28253a31b7ebSMike Smith 
2826639717c8SPaul Saab     bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2827639717c8SPaul Saab 		    BUS_DMASYNC_POSTWRITE);
2828639717c8SPaul Saab 
2829639717c8SPaul Saab     if (cr->cr_data == NULL)
2830639717c8SPaul Saab 	goto out;
2831639717c8SPaul Saab 
28323a31b7ebSMike Smith     if (cr->cr_flags & CISS_REQ_DATAIN)
28333a31b7ebSMike Smith 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTREAD);
28343a31b7ebSMike Smith     if (cr->cr_flags & CISS_REQ_DATAOUT)
28353a31b7ebSMike Smith 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTWRITE);
28363a31b7ebSMike Smith 
28373a31b7ebSMike Smith     bus_dmamap_unload(sc->ciss_buffer_dmat, cr->cr_datamap);
2838639717c8SPaul Saab out:
28393a31b7ebSMike Smith     cr->cr_flags &= ~CISS_REQ_MAPPED;
28403a31b7ebSMike Smith }
28413a31b7ebSMike Smith 
28423a31b7ebSMike Smith /************************************************************************
28433a31b7ebSMike Smith  * Attach the driver to CAM.
28443a31b7ebSMike Smith  *
28453a31b7ebSMike Smith  * We put all the logical drives on a single SCSI bus.
28463a31b7ebSMike Smith  */
28473a31b7ebSMike Smith static int
28483a31b7ebSMike Smith ciss_cam_init(struct ciss_softc *sc)
28493a31b7ebSMike Smith {
28501fe6c4eeSScott Long     int			i, maxbus;
28513a31b7ebSMike Smith 
28523a31b7ebSMike Smith     debug_called(1);
28533a31b7ebSMike Smith 
28543a31b7ebSMike Smith     /*
28553a31b7ebSMike Smith      * Allocate a devq.  We can reuse this for the masked physical
28563a31b7ebSMike Smith      * devices if we decide to export these as well.
28573a31b7ebSMike Smith      */
285895dab4f2SAlexander Motin     if ((sc->ciss_cam_devq = cam_simq_alloc(sc->ciss_max_requests - 2)) == NULL) {
28593a31b7ebSMike Smith 	ciss_printf(sc, "can't allocate CAM SIM queue\n");
28603a31b7ebSMike Smith 	return(ENOMEM);
28613a31b7ebSMike Smith     }
28623a31b7ebSMike Smith 
28633a31b7ebSMike Smith     /*
28643a31b7ebSMike Smith      * Create a SIM.
28651fe6c4eeSScott Long      *
28661fe6c4eeSScott Long      * This naturally wastes a bit of memory.  The alternative is to allocate
28671fe6c4eeSScott Long      * and register each bus as it is found, and then track them on a linked
28681fe6c4eeSScott Long      * list.  Unfortunately, the driver has a few places where it needs to
28691fe6c4eeSScott Long      * look up the SIM based solely on bus number, and it's unclear whether
28701fe6c4eeSScott Long      * a list traversal would work for these situations.
28713a31b7ebSMike Smith      */
28721fe6c4eeSScott Long     maxbus = max(sc->ciss_max_logical_bus, sc->ciss_max_physical_bus +
28731fe6c4eeSScott Long 		 CISS_PHYSICAL_BASE);
2874ac2fffa4SPedro F. Giffuni     sc->ciss_cam_sim = malloc(maxbus * sizeof(struct cam_sim*),
2875c6131460SPaul Saab 			      CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
2876c6131460SPaul Saab     if (sc->ciss_cam_sim == NULL) {
2877c6131460SPaul Saab 	ciss_printf(sc, "can't allocate memory for controller SIM\n");
2878c6131460SPaul Saab 	return(ENOMEM);
2879c6131460SPaul Saab     }
2880c6131460SPaul Saab 
28811fe6c4eeSScott Long     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
2882c6131460SPaul Saab 	if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
2883c6131460SPaul Saab 						 "ciss", sc,
28842aa6b972SPaul Saab 						 device_get_unit(sc->ciss_dev),
288545650f52SScott Long 						 &sc->ciss_mtx,
288622657ce1SScott Long 						 2,
2887d4aa427fSPaul Saab 						 sc->ciss_max_requests - 2,
28883a31b7ebSMike Smith 						 sc->ciss_cam_devq)) == NULL) {
2889c6131460SPaul Saab 	    ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
28903a31b7ebSMike Smith 	    return(ENOMEM);
28913a31b7ebSMike Smith 	}
28923a31b7ebSMike Smith 
28933a31b7ebSMike Smith 	/*
2894c6131460SPaul Saab 	 * Register bus with this SIM.
28953a31b7ebSMike Smith 	 */
2896975b7318SScott Long 	mtx_lock(&sc->ciss_mtx);
28971fe6c4eeSScott Long 	if (i == 0 || sc->ciss_controllers[i].physical.bus != 0) {
2898b50569b7SScott Long 	    if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
28991fe6c4eeSScott Long 		ciss_printf(sc, "can't register SCSI bus %d\n", i);
2900975b7318SScott Long 		mtx_unlock(&sc->ciss_mtx);
29011fe6c4eeSScott Long 		return (ENXIO);
29021fe6c4eeSScott Long 	    }
29031fe6c4eeSScott Long 	}
2904975b7318SScott Long 	mtx_unlock(&sc->ciss_mtx);
29051fe6c4eeSScott Long     }
29061fe6c4eeSScott Long 
29071fe6c4eeSScott Long     for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
29081fe6c4eeSScott Long 	 CISS_PHYSICAL_BASE; i++) {
29091fe6c4eeSScott Long 	if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
29101fe6c4eeSScott Long 						 "ciss", sc,
29111fe6c4eeSScott Long 						 device_get_unit(sc->ciss_dev),
2912975b7318SScott Long 						 &sc->ciss_mtx, 1,
29131fe6c4eeSScott Long 						 sc->ciss_max_requests - 2,
29141fe6c4eeSScott Long 						 sc->ciss_cam_devq)) == NULL) {
29151fe6c4eeSScott Long 	    ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
29161fe6c4eeSScott Long 	    return (ENOMEM);
29171fe6c4eeSScott Long 	}
29181fe6c4eeSScott Long 
2919975b7318SScott Long 	mtx_lock(&sc->ciss_mtx);
2920b50569b7SScott Long 	if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
2921c6131460SPaul Saab 	    ciss_printf(sc, "can't register SCSI bus %d\n", i);
2922975b7318SScott Long 	    mtx_unlock(&sc->ciss_mtx);
29233a31b7ebSMike Smith 	    return (ENXIO);
29243a31b7ebSMike Smith 	}
2925975b7318SScott Long 	mtx_unlock(&sc->ciss_mtx);
2926c6131460SPaul Saab     }
29273a31b7ebSMike Smith 
29283a31b7ebSMike Smith     return(0);
29293a31b7ebSMike Smith }
29303a31b7ebSMike Smith 
29313a31b7ebSMike Smith /************************************************************************
29323a31b7ebSMike Smith  * Initiate a rescan of the 'logical devices' SIM
29333a31b7ebSMike Smith  */
29343a31b7ebSMike Smith static void
2935c6131460SPaul Saab ciss_cam_rescan_target(struct ciss_softc *sc, int bus, int target)
29363a31b7ebSMike Smith {
29373a31b7ebSMike Smith     union ccb		*ccb;
29383a31b7ebSMike Smith 
29393a31b7ebSMike Smith     debug_called(1);
29403a31b7ebSMike Smith 
294183c5d981SAlexander Motin     if ((ccb = xpt_alloc_ccb_nowait()) == NULL) {
29423a31b7ebSMike Smith 	ciss_printf(sc, "rescan failed (can't allocate CCB)\n");
29433a31b7ebSMike Smith 	return;
29443a31b7ebSMike Smith     }
29453a31b7ebSMike Smith 
2946e5dfa058SAlexander Motin     if (xpt_create_path(&ccb->ccb_h.path, NULL,
294783c5d981SAlexander Motin 	    cam_sim_path(sc->ciss_cam_sim[bus]),
2948c6131460SPaul Saab 	    target, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
29493a31b7ebSMike Smith 	ciss_printf(sc, "rescan failed (can't create path)\n");
295083c5d981SAlexander Motin 	xpt_free_ccb(ccb);
29513a31b7ebSMike Smith 	return;
29523a31b7ebSMike Smith     }
295383c5d981SAlexander Motin     xpt_rescan(ccb);
29543a31b7ebSMike Smith     /* scan is now in progress */
29553a31b7ebSMike Smith }
29563a31b7ebSMike Smith 
29573a31b7ebSMike Smith /************************************************************************
29583a31b7ebSMike Smith  * Handle requests coming from CAM
29593a31b7ebSMike Smith  */
29603a31b7ebSMike Smith static void
29613a31b7ebSMike Smith ciss_cam_action(struct cam_sim *sim, union ccb *ccb)
29623a31b7ebSMike Smith {
2963440baa08SPaul Saab     struct ciss_softc	*sc;
2964440baa08SPaul Saab     struct ccb_scsiio	*csio;
2965c6131460SPaul Saab     int			bus, target;
29661fe6c4eeSScott Long     int			physical;
2967440baa08SPaul Saab 
2968440baa08SPaul Saab     sc = cam_sim_softc(sim);
2969c6131460SPaul Saab     bus = cam_sim_bus(sim);
2970440baa08SPaul Saab     csio = (struct ccb_scsiio *)&ccb->csio;
2971440baa08SPaul Saab     target = csio->ccb_h.target_id;
29721fe6c4eeSScott Long     physical = CISS_IS_PHYSICAL(bus);
2973440baa08SPaul Saab 
29743a31b7ebSMike Smith     switch (ccb->ccb_h.func_code) {
29753a31b7ebSMike Smith 	/* perform SCSI I/O */
29763a31b7ebSMike Smith     case XPT_SCSI_IO:
2977440baa08SPaul Saab 	if (!ciss_cam_action_io(sim, csio))
29783a31b7ebSMike Smith 	    return;
29793a31b7ebSMike Smith 	break;
29803a31b7ebSMike Smith 
29813a31b7ebSMike Smith 	/* perform geometry calculations */
29823a31b7ebSMike Smith     case XPT_CALC_GEOMETRY:
29833a31b7ebSMike Smith     {
29843a31b7ebSMike Smith 	struct ccb_calc_geometry	*ccg = &ccb->ccg;
29851fe6c4eeSScott Long 	struct ciss_ldrive		*ld;
29863a31b7ebSMike Smith 
29873a31b7ebSMike Smith 	debug(1, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
29883a31b7ebSMike Smith 
29891fe6c4eeSScott Long 	ld = NULL;
29901fe6c4eeSScott Long 	if (!physical)
29911fe6c4eeSScott Long 	    ld = &sc->ciss_logical[bus][target];
29921fe6c4eeSScott Long 
29933a31b7ebSMike Smith 	/*
2994440baa08SPaul Saab 	 * Use the cached geometry settings unless the fault tolerance
2995440baa08SPaul Saab 	 * is invalid.
29963a31b7ebSMike Smith 	 */
29971fe6c4eeSScott Long 	if (physical || ld->cl_geometry.fault_tolerance == 0xFF) {
2998440baa08SPaul Saab 	    u_int32_t			secs_per_cylinder;
2999440baa08SPaul Saab 
30003a31b7ebSMike Smith 	    ccg->heads = 255;
30013a31b7ebSMike Smith 	    ccg->secs_per_track = 32;
30023a31b7ebSMike Smith 	    secs_per_cylinder = ccg->heads * ccg->secs_per_track;
30033a31b7ebSMike Smith 	    ccg->cylinders = ccg->volume_size / secs_per_cylinder;
3004440baa08SPaul Saab 	} else {
3005440baa08SPaul Saab 	    ccg->heads = ld->cl_geometry.heads;
3006440baa08SPaul Saab 	    ccg->secs_per_track = ld->cl_geometry.sectors;
3007440baa08SPaul Saab 	    ccg->cylinders = ntohs(ld->cl_geometry.cylinders);
3008440baa08SPaul Saab 	}
30093a31b7ebSMike Smith 	ccb->ccb_h.status = CAM_REQ_CMP;
30103a31b7ebSMike Smith         break;
30113a31b7ebSMike Smith     }
30123a31b7ebSMike Smith 
30133a31b7ebSMike Smith 	/* handle path attribute inquiry */
30143a31b7ebSMike Smith     case XPT_PATH_INQ:
30153a31b7ebSMike Smith     {
30163a31b7ebSMike Smith 	struct ccb_pathinq	*cpi = &ccb->cpi;
3017bdde5705SSean Bruno 	int			sg_length;
30183a31b7ebSMike Smith 
30193a31b7ebSMike Smith 	debug(1, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
30203a31b7ebSMike Smith 
30213a31b7ebSMike Smith 	cpi->version_num = 1;
30223a31b7ebSMike Smith 	cpi->hba_inquiry = PI_TAG_ABLE;	/* XXX is this correct? */
30233a31b7ebSMike Smith 	cpi->target_sprt = 0;
30243a31b7ebSMike Smith 	cpi->hba_misc = 0;
3025e3edca22SSean Bruno 	cpi->max_target = sc->ciss_cfg->max_logical_supported;
30263a31b7ebSMike Smith 	cpi->max_lun = 0;		/* 'logical drive' channel only */
3027e3edca22SSean Bruno 	cpi->initiator_id = sc->ciss_cfg->max_logical_supported;
30284195c7deSAlan Somers 	strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
30294195c7deSAlan Somers 	strlcpy(cpi->hba_vid, "CISS", HBA_IDLEN);
30304195c7deSAlan Somers 	strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
30313a31b7ebSMike Smith 	cpi->unit_number = cam_sim_unit(sim);
30323a31b7ebSMike Smith 	cpi->bus_id = cam_sim_bus(sim);
30333a31b7ebSMike Smith 	cpi->base_transfer_speed = 132 * 1024;	/* XXX what to set this to? */
3034fa9ed865SMatt Jacob 	cpi->transport = XPORT_SPI;
3035fa9ed865SMatt Jacob 	cpi->transport_version = 2;
3036fa9ed865SMatt Jacob 	cpi->protocol = PROTO_SCSI;
3037fa9ed865SMatt Jacob 	cpi->protocol_version = SCSI_REV_2;
3038bdde5705SSean Bruno 	if (sc->ciss_cfg->max_sg_length == 0) {
3039e8e4cdbaSSean Bruno 		sg_length = 17;
3040bdde5705SSean Bruno 	} else {
3041bdde5705SSean Bruno 	/* XXX Fix for ZMR cards that advertise max_sg_length == 32
3042bdde5705SSean Bruno 	 * Confusing bit here. max_sg_length is usually a power of 2. We always
3043bdde5705SSean Bruno 	 * need to subtract 1 to account for partial pages. Then we need to
3044bdde5705SSean Bruno 	 * align on a valid PAGE_SIZE so we round down to the nearest power of 2.
3045bdde5705SSean Bruno 	 * Add 1 so we can then subtract it out in the assignment to maxio.
3046bdde5705SSean Bruno 	 * The reason for all these shenanigans is to create a maxio value that
3047bdde5705SSean Bruno 	 * creates IO operations to volumes that yield consistent operations
3048bdde5705SSean Bruno 	 * with good performance.
3049bdde5705SSean Bruno 	 */
3050bdde5705SSean Bruno 		sg_length = sc->ciss_cfg->max_sg_length - 1;
3051bdde5705SSean Bruno 		sg_length = (1 << (fls(sg_length) - 1)) + 1;
3052bdde5705SSean Bruno 	}
3053bdde5705SSean Bruno 	cpi->maxio = (min(CISS_MAX_SG_ELEMENTS, sg_length) - 1) * PAGE_SIZE;
30543a31b7ebSMike Smith 	ccb->ccb_h.status = CAM_REQ_CMP;
30553a31b7ebSMike Smith 	break;
30563a31b7ebSMike Smith     }
30573a31b7ebSMike Smith 
30583a31b7ebSMike Smith     case XPT_GET_TRAN_SETTINGS:
30593a31b7ebSMike Smith     {
30603a31b7ebSMike Smith 	struct ccb_trans_settings	*cts = &ccb->cts;
306163b9228bSScott Long 	struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
306263b9228bSScott Long 	struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
30633a31b7ebSMike Smith 
3064fa911920SScott Long 	debug(1, "XPT_GET_TRAN_SETTINGS %d:%d", cam_sim_bus(sim),
3065fa911920SScott Long 	    ctl->ccb_h.target_id);
30663a31b7ebSMike Smith 	/* disconnect always OK */
3067fa9ed865SMatt Jacob 	cts->protocol = PROTO_SCSI;
3068fa9ed865SMatt Jacob 	cts->protocol_version = SCSI_REV_2;
3069fa9ed865SMatt Jacob 	cts->transport = XPORT_SPI;
3070fa9ed865SMatt Jacob 	cts->transport_version = 2;
3071fa9ed865SMatt Jacob 
3072fa9ed865SMatt Jacob 	spi->valid = CTS_SPI_VALID_DISC;
3073fa9ed865SMatt Jacob 	spi->flags = CTS_SPI_FLAGS_DISC_ENB;
30743a31b7ebSMike Smith 
307563b9228bSScott Long 	scsi->valid = CTS_SCSI_VALID_TQ;
307663b9228bSScott Long 	scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
307763b9228bSScott Long 
30783a31b7ebSMike Smith 	cts->ccb_h.status = CAM_REQ_CMP;
30793a31b7ebSMike Smith 	break;
30803a31b7ebSMike Smith     }
30813a31b7ebSMike Smith 
30823a31b7ebSMike Smith     default:		/* we can't do this */
30833a31b7ebSMike Smith 	debug(1, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
30843a31b7ebSMike Smith 	ccb->ccb_h.status = CAM_REQ_INVALID;
30853a31b7ebSMike Smith 	break;
30863a31b7ebSMike Smith     }
30873a31b7ebSMike Smith 
30883a31b7ebSMike Smith     xpt_done(ccb);
30893a31b7ebSMike Smith }
30903a31b7ebSMike Smith 
30913a31b7ebSMike Smith /************************************************************************
30923a31b7ebSMike Smith  * Handle a CAM SCSI I/O request.
30933a31b7ebSMike Smith  */
30943a31b7ebSMike Smith static int
30953a31b7ebSMike Smith ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio)
30963a31b7ebSMike Smith {
30973a31b7ebSMike Smith     struct ciss_softc	*sc;
30983a31b7ebSMike Smith     int			bus, target;
30993a31b7ebSMike Smith     struct ciss_request	*cr;
31003a31b7ebSMike Smith     struct ciss_command	*cc;
31013a31b7ebSMike Smith     int			error;
31023a31b7ebSMike Smith 
31033a31b7ebSMike Smith     sc = cam_sim_softc(sim);
31043a31b7ebSMike Smith     bus = cam_sim_bus(sim);
31053a31b7ebSMike Smith     target = csio->ccb_h.target_id;
31063a31b7ebSMike Smith 
31073a31b7ebSMike Smith     debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, csio->ccb_h.target_lun);
31083a31b7ebSMike Smith 
31093a31b7ebSMike Smith     /* check that the CDB pointer is not to a physical address */
31103a31b7ebSMike Smith     if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
31113a31b7ebSMike Smith 	debug(3, "  CDB pointer is to physical address");
31123a31b7ebSMike Smith 	csio->ccb_h.status = CAM_REQ_CMP_ERR;
31133a31b7ebSMike Smith     }
31143a31b7ebSMike Smith 
31153a31b7ebSMike Smith     /* abandon aborted ccbs or those that have failed validation */
31163a31b7ebSMike Smith     if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
31173a31b7ebSMike Smith 	debug(3, "abandoning CCB due to abort/validation failure");
31183a31b7ebSMike Smith 	return(EINVAL);
31193a31b7ebSMike Smith     }
31203a31b7ebSMike Smith 
31213a31b7ebSMike Smith     /* handle emulation of some SCSI commands ourself */
31223a31b7ebSMike Smith     if (ciss_cam_emulate(sc, csio))
31233a31b7ebSMike Smith 	return(0);
31243a31b7ebSMike Smith 
31253a31b7ebSMike Smith     /*
31263a31b7ebSMike Smith      * Get a request to manage this command.  If we can't, return the
31273a31b7ebSMike Smith      * ccb, freeze the queue and flag so that we unfreeze it when a
31283a31b7ebSMike Smith      * request completes.
31293a31b7ebSMike Smith      */
31303a31b7ebSMike Smith     if ((error = ciss_get_request(sc, &cr)) != 0) {
31311fe6c4eeSScott Long 	xpt_freeze_simq(sim, 1);
313295dab4f2SAlexander Motin 	sc->ciss_flags |= CISS_FLAG_BUSY;
31333a31b7ebSMike Smith 	csio->ccb_h.status |= CAM_REQUEUE_REQ;
31343a31b7ebSMike Smith 	return(error);
31353a31b7ebSMike Smith     }
31363a31b7ebSMike Smith 
31373a31b7ebSMike Smith     /*
31383a31b7ebSMike Smith      * Build the command.
31393a31b7ebSMike Smith      */
31402fdaa90dSScott Long     cc = cr->cr_cc;
3141dd0b4fb6SKonstantin Belousov     cr->cr_data = csio;
31423a31b7ebSMike Smith     cr->cr_length = csio->dxfer_len;
31433a31b7ebSMike Smith     cr->cr_complete = ciss_cam_complete;
31443a31b7ebSMike Smith     cr->cr_private = csio;
31453a31b7ebSMike Smith 
3146c6131460SPaul Saab     /*
3147c6131460SPaul Saab      * Target the right logical volume.
3148c6131460SPaul Saab      */
31491fe6c4eeSScott Long     if (CISS_IS_PHYSICAL(bus))
31501fe6c4eeSScott Long 	cc->header.address =
31511fe6c4eeSScott Long 	    sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_address;
31521fe6c4eeSScott Long     else
31531fe6c4eeSScott Long 	cc->header.address =
31541fe6c4eeSScott Long 	    sc->ciss_logical[bus][target].cl_address;
31553a31b7ebSMike Smith     cc->cdb.cdb_length = csio->cdb_len;
31563a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
31573a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;	/* XXX ordered tags? */
31583a31b7ebSMike Smith     if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
3159dd0b4fb6SKonstantin Belousov 	cr->cr_flags = CISS_REQ_DATAOUT | CISS_REQ_CCB;
31603a31b7ebSMike Smith 	cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
31613a31b7ebSMike Smith     } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
3162dd0b4fb6SKonstantin Belousov 	cr->cr_flags = CISS_REQ_DATAIN | CISS_REQ_CCB;
31633a31b7ebSMike Smith 	cc->cdb.direction = CISS_CDB_DIRECTION_READ;
31643a31b7ebSMike Smith     } else {
3165dd0b4fb6SKonstantin Belousov 	cr->cr_data = NULL;
31663a31b7ebSMike Smith 	cr->cr_flags = 0;
31673a31b7ebSMike Smith 	cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
31683a31b7ebSMike Smith     }
31693a31b7ebSMike Smith     cc->cdb.timeout = (csio->ccb_h.timeout / 1000) + 1;
31703a31b7ebSMike Smith     if (csio->ccb_h.flags & CAM_CDB_POINTER) {
31713a31b7ebSMike Smith 	bcopy(csio->cdb_io.cdb_ptr, &cc->cdb.cdb[0], csio->cdb_len);
31723a31b7ebSMike Smith     } else {
31733a31b7ebSMike Smith 	bcopy(csio->cdb_io.cdb_bytes, &cc->cdb.cdb[0], csio->cdb_len);
31743a31b7ebSMike Smith     }
31753a31b7ebSMike Smith 
31763a31b7ebSMike Smith     /*
31773a31b7ebSMike Smith      * Submit the request to the adapter.
31783a31b7ebSMike Smith      *
31793a31b7ebSMike Smith      * Note that this may fail if we're unable to map the request (and
31803a31b7ebSMike Smith      * if we ever learn a transport layer other than simple, may fail
31813a31b7ebSMike Smith      * if the adapter rejects the command).
31823a31b7ebSMike Smith      */
31833a31b7ebSMike Smith     if ((error = ciss_start(cr)) != 0) {
31841fe6c4eeSScott Long 	xpt_freeze_simq(sim, 1);
3185639717c8SPaul Saab 	csio->ccb_h.status |= CAM_RELEASE_SIMQ;
31860a65b79fSAlexander Motin 	if (error == EINPROGRESS) {
3187639717c8SPaul Saab 	    error = 0;
3188639717c8SPaul Saab 	} else {
31893a31b7ebSMike Smith 	    csio->ccb_h.status |= CAM_REQUEUE_REQ;
31903a31b7ebSMike Smith 	    ciss_release_request(cr);
3191639717c8SPaul Saab 	}
31923a31b7ebSMike Smith 	return(error);
31933a31b7ebSMike Smith     }
31943a31b7ebSMike Smith 
31953a31b7ebSMike Smith     return(0);
31963a31b7ebSMike Smith }
31973a31b7ebSMike Smith 
31983a31b7ebSMike Smith /************************************************************************
31993a31b7ebSMike Smith  * Emulate SCSI commands the adapter doesn't handle as we might like.
32003a31b7ebSMike Smith  */
32013a31b7ebSMike Smith static int
32023a31b7ebSMike Smith ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio)
32033a31b7ebSMike Smith {
3204c6131460SPaul Saab     int		bus, target;
32053a31b7ebSMike Smith     u_int8_t	opcode;
32063a31b7ebSMike Smith 
32073a31b7ebSMike Smith     target = csio->ccb_h.target_id;
3208c6131460SPaul Saab     bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
32093a31b7ebSMike Smith     opcode = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
32103a31b7ebSMike Smith 	*(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0];
32113a31b7ebSMike Smith 
32121fe6c4eeSScott Long     if (CISS_IS_PHYSICAL(bus)) {
32131fe6c4eeSScott Long 	if (sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_online != 1) {
32140a65b79fSAlexander Motin 	    csio->ccb_h.status |= CAM_SEL_TIMEOUT;
32151fe6c4eeSScott Long 	    xpt_done((union ccb *)csio);
32161fe6c4eeSScott Long 	    return(1);
32171fe6c4eeSScott Long 	} else
32181fe6c4eeSScott Long 	    return(0);
32191fe6c4eeSScott Long     }
32201fe6c4eeSScott Long 
32213a31b7ebSMike Smith     /*
32226f71a953SPaul Saab      * Handle requests for volumes that don't exist or are not online.
32236f71a953SPaul Saab      * A selection timeout is slightly better than an illegal request.
32246f71a953SPaul Saab      * Other errors might be better.
32253a31b7ebSMike Smith      */
32266f71a953SPaul Saab     if (sc->ciss_logical[bus][target].cl_status != CISS_LD_ONLINE) {
32270a65b79fSAlexander Motin 	csio->ccb_h.status |= CAM_SEL_TIMEOUT;
32283a31b7ebSMike Smith 	xpt_done((union ccb *)csio);
32293a31b7ebSMike Smith 	return(1);
32303a31b7ebSMike Smith     }
32313a31b7ebSMike Smith 
32323a31b7ebSMike Smith     /* if we have to fake Synchronise Cache */
32333a31b7ebSMike Smith     if (sc->ciss_flags & CISS_FLAG_FAKE_SYNCH) {
32343a31b7ebSMike Smith 	/*
32353a31b7ebSMike Smith 	 * If this is a Synchronise Cache command, typically issued when
32363a31b7ebSMike Smith 	 * a device is closed, flush the adapter and complete now.
32373a31b7ebSMike Smith 	 */
32383a31b7ebSMike Smith 	if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
32393a31b7ebSMike Smith 	     *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE) {
32403a31b7ebSMike Smith 	    ciss_flush_adapter(sc);
32410a65b79fSAlexander Motin 	    csio->ccb_h.status |= CAM_REQ_CMP;
32423a31b7ebSMike Smith 	    xpt_done((union ccb *)csio);
32433a31b7ebSMike Smith 	    return(1);
32443a31b7ebSMike Smith 	}
32453a31b7ebSMike Smith     }
32463a31b7ebSMike Smith 
3247a1abcc4fSSean Bruno     /*
3248a1abcc4fSSean Bruno      * A CISS target can only ever have one lun per target. REPORT_LUNS requires
3249a1abcc4fSSean Bruno      * at least one LUN field to be pre created for us, so snag it and fill in
3250a1abcc4fSSean Bruno      * the least significant byte indicating 1 LUN here.  Emulate the command
3251a1abcc4fSSean Bruno      * return to shut up warning on console of a CDB error.  swb
3252a1abcc4fSSean Bruno      */
3253a1abcc4fSSean Bruno     if (opcode == REPORT_LUNS && csio->dxfer_len > 0) {
3254a1abcc4fSSean Bruno        csio->data_ptr[3] = 8;
3255a1abcc4fSSean Bruno        csio->ccb_h.status |= CAM_REQ_CMP;
3256a1abcc4fSSean Bruno        xpt_done((union ccb *)csio);
3257a1abcc4fSSean Bruno        return(1);
3258a1abcc4fSSean Bruno     }
3259a1abcc4fSSean Bruno 
32603a31b7ebSMike Smith     return(0);
32613a31b7ebSMike Smith }
32623a31b7ebSMike Smith 
32633a31b7ebSMike Smith /************************************************************************
32643a31b7ebSMike Smith  * Check for possibly-completed commands.
32653a31b7ebSMike Smith  */
32663a31b7ebSMike Smith static void
32673a31b7ebSMike Smith ciss_cam_poll(struct cam_sim *sim)
32683a31b7ebSMike Smith {
326922657ce1SScott Long     cr_qhead_t qh;
32703a31b7ebSMike Smith     struct ciss_softc	*sc = cam_sim_softc(sim);
32713a31b7ebSMike Smith 
32723a31b7ebSMike Smith     debug_called(2);
32733a31b7ebSMike Smith 
327422657ce1SScott Long     STAILQ_INIT(&qh);
327522657ce1SScott Long     if (sc->ciss_perf)
327622657ce1SScott Long 	ciss_perf_done(sc, &qh);
327722657ce1SScott Long     else
327822657ce1SScott Long 	ciss_done(sc, &qh);
327922657ce1SScott Long     ciss_complete(sc, &qh);
32803a31b7ebSMike Smith }
32813a31b7ebSMike Smith 
32823a31b7ebSMike Smith /************************************************************************
32833a31b7ebSMike Smith  * Handle completion of a command - pass results back through the CCB
32843a31b7ebSMike Smith  */
32853a31b7ebSMike Smith static void
32863a31b7ebSMike Smith ciss_cam_complete(struct ciss_request *cr)
32873a31b7ebSMike Smith {
32883a31b7ebSMike Smith     struct ciss_softc		*sc;
32893a31b7ebSMike Smith     struct ciss_command		*cc;
32903a31b7ebSMike Smith     struct ciss_error_info	*ce;
32913a31b7ebSMike Smith     struct ccb_scsiio		*csio;
32923a31b7ebSMike Smith     int				scsi_status;
32933a31b7ebSMike Smith     int				command_status;
32943a31b7ebSMike Smith 
32953a31b7ebSMike Smith     debug_called(2);
32963a31b7ebSMike Smith 
32973a31b7ebSMike Smith     sc = cr->cr_sc;
32982fdaa90dSScott Long     cc = cr->cr_cc;
32993a31b7ebSMike Smith     ce = (struct ciss_error_info *)&(cc->sg[0]);
33003a31b7ebSMike Smith     csio = (struct ccb_scsiio *)cr->cr_private;
33013a31b7ebSMike Smith 
33023a31b7ebSMike Smith     /*
33033a31b7ebSMike Smith      * Extract status values from request.
33043a31b7ebSMike Smith      */
33053a31b7ebSMike Smith     ciss_report_request(cr, &command_status, &scsi_status);
3306e8144a13SAlexander Motin     switch(command_status) {
3307e8144a13SAlexander Motin     case CISS_CMD_STATUS_DATA_UNDERRUN:
3308e8144a13SAlexander Motin 	csio->resid = ce->residual_count;
3309e8144a13SAlexander Motin 	/* FALLTHROUGH */
3310e8144a13SAlexander Motin     case CISS_CMD_STATUS_SUCCESS:
33113a31b7ebSMike Smith 	csio->scsi_status = scsi_status;
33123a31b7ebSMike Smith 	debug(2, "SCSI_STATUS_OK");
33130a65b79fSAlexander Motin 	csio->ccb_h.status |= CAM_REQ_CMP;
33143a31b7ebSMike Smith 	break;
3315e8144a13SAlexander Motin     case CISS_CMD_STATUS_TARGET_STATUS:
3316e8144a13SAlexander Motin 	csio->scsi_status = scsi_status;
33173a31b7ebSMike Smith 	bzero(&csio->sense_data, SSD_FULL_SIZE);
33183a31b7ebSMike Smith 	bcopy(&ce->sense_info[0], &csio->sense_data, ce->sense_length);
33191cc052e8SKenneth D. Merry 	if (csio->sense_len > ce->sense_length)
33201cc052e8SKenneth D. Merry 		csio->sense_resid = csio->sense_len - ce->sense_length;
33211cc052e8SKenneth D. Merry 	else
33221cc052e8SKenneth D. Merry 		csio->sense_resid = 0;
33233a31b7ebSMike Smith 	csio->resid = ce->residual_count;
33240a65b79fSAlexander Motin 	csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
33253a31b7ebSMike Smith 	break;
3326e8144a13SAlexander Motin     case CISS_CMD_STATUS_DATA_OVERRUN:
3327e8144a13SAlexander Motin 	csio->ccb_h.status |= CAM_DATA_RUN_ERR;
33283a31b7ebSMike Smith 	break;
33293a31b7ebSMike Smith     default:
33300a65b79fSAlexander Motin 	csio->ccb_h.status |= CAM_REQ_CMP_ERR;
33313a31b7ebSMike Smith 	break;
33323a31b7ebSMike Smith     }
33333a31b7ebSMike Smith 
33343a31b7ebSMike Smith     /* handle post-command fixup */
33353a31b7ebSMike Smith     ciss_cam_complete_fixup(sc, csio);
33363a31b7ebSMike Smith 
33373a31b7ebSMike Smith     ciss_release_request(cr);
333895dab4f2SAlexander Motin     if (sc->ciss_flags & CISS_FLAG_BUSY) {
333995dab4f2SAlexander Motin 	sc->ciss_flags &= ~CISS_FLAG_BUSY;
334095dab4f2SAlexander Motin 	if (csio->ccb_h.status & CAM_RELEASE_SIMQ)
334195dab4f2SAlexander Motin 	    xpt_release_simq(xpt_path_sim(csio->ccb_h.path), 0);
334295dab4f2SAlexander Motin 	else
334395dab4f2SAlexander Motin 	    csio->ccb_h.status |= CAM_RELEASE_SIMQ;
334495dab4f2SAlexander Motin     }
334522657ce1SScott Long     xpt_done((union ccb *)csio);
33463a31b7ebSMike Smith }
33473a31b7ebSMike Smith 
33483a31b7ebSMike Smith /********************************************************************************
33493a31b7ebSMike Smith  * Fix up the result of some commands here.
33503a31b7ebSMike Smith  */
33513a31b7ebSMike Smith static void
33523a31b7ebSMike Smith ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio)
33533a31b7ebSMike Smith {
33543a31b7ebSMike Smith     struct scsi_inquiry_data	*inq;
33553a31b7ebSMike Smith     struct ciss_ldrive		*cl;
335657ae362cSAlexander Motin     uint8_t			*cdb;
3357c6131460SPaul Saab     int				bus, target;
33583a31b7ebSMike Smith 
335957ae362cSAlexander Motin     cdb = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
336057ae362cSAlexander Motin 	 (uint8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes;
336157ae362cSAlexander Motin     if (cdb[0] == INQUIRY &&
336257ae362cSAlexander Motin 	(cdb[1] & SI_EVPD) == 0 &&
336357ae362cSAlexander Motin 	(csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN &&
336457ae362cSAlexander Motin 	csio->dxfer_len >= SHORT_INQUIRY_LENGTH) {
33653a31b7ebSMike Smith 	inq = (struct scsi_inquiry_data *)csio->data_ptr;
33663a31b7ebSMike Smith 	target = csio->ccb_h.target_id;
3367c6131460SPaul Saab 	bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
33681fe6c4eeSScott Long 
33691fe6c4eeSScott Long 	/*
33700fb31ed0SSean Bruno 	 * If the controller is in JBOD mode, there are no logical volumes.
33710fb31ed0SSean Bruno 	 * Let the disks be probed and dealt with via CAM.  Else, mask off
33720fb31ed0SSean Bruno 	 * the physical disks and setup the parts of the inq structure for
33730fb31ed0SSean Bruno 	 * the logical volume.  swb
33741fe6c4eeSScott Long 	 */
33750fb31ed0SSean Bruno 	if( !(sc->ciss_id->PowerUPNvramFlags & PWR_UP_FLAG_JBOD_ENABLED)){
33761fe6c4eeSScott Long 		if (CISS_IS_PHYSICAL(bus)) {
33771fe6c4eeSScott Long 	    		if (SID_TYPE(inq) == T_DIRECT)
33781fe6c4eeSScott Long 				inq->device = (inq->device & 0xe0) | T_NODEVICE;
33791fe6c4eeSScott Long 	    		return;
33801fe6c4eeSScott Long 		}
3381c6131460SPaul Saab 		cl = &sc->ciss_logical[bus][target];
33823a31b7ebSMike Smith 
33831e8b29a4SSean Bruno 		padstr(inq->vendor, "HP",
3384a9112e5dSSean Bruno 	       		SID_VENDOR_SIZE);
3385a9112e5dSSean Bruno 		padstr(inq->product,
3386a9112e5dSSean Bruno 	       		ciss_name_ldrive_org(cl->cl_ldrive->fault_tolerance),
3387a9112e5dSSean Bruno 	       		SID_PRODUCT_SIZE);
3388a9112e5dSSean Bruno 		padstr(inq->revision,
3389a9112e5dSSean Bruno 	       		ciss_name_ldrive_status(cl->cl_lstatus->status),
3390a9112e5dSSean Bruno 	       		SID_REVISION_SIZE);
33913a31b7ebSMike Smith 	}
33923a31b7ebSMike Smith     }
33930fb31ed0SSean Bruno }
33943a31b7ebSMike Smith 
33953a31b7ebSMike Smith /********************************************************************************
33963a31b7ebSMike Smith  * Name the device at (target)
33973a31b7ebSMike Smith  *
33983a31b7ebSMike Smith  * XXX is this strictly correct?
33993a31b7ebSMike Smith  */
340037c84183SPoul-Henning Kamp static int
3401c6131460SPaul Saab ciss_name_device(struct ciss_softc *sc, int bus, int target)
34023a31b7ebSMike Smith {
34033a31b7ebSMike Smith     struct cam_periph	*periph;
3404739a9399SSean Bruno     struct cam_path	*path;
3405739a9399SSean Bruno     int			status;
34063a31b7ebSMike Smith 
3407d49f1379SPaul Saab     if (CISS_IS_PHYSICAL(bus))
34081fe6c4eeSScott Long 	return (0);
3409739a9399SSean Bruno 
3410739a9399SSean Bruno     status = xpt_create_path(&path, NULL, cam_sim_path(sc->ciss_cam_sim[bus]),
3411739a9399SSean Bruno 			     target, 0);
3412739a9399SSean Bruno 
3413739a9399SSean Bruno     if (status == CAM_REQ_CMP) {
3414739a9399SSean Bruno 	xpt_path_lock(path);
3415739a9399SSean Bruno 	periph = cam_periph_find(path, NULL);
341697ed09b5SSean Bruno 	xpt_path_unlock(path);
341797ed09b5SSean Bruno 	xpt_free_path(path);
341897ed09b5SSean Bruno 	if (periph != NULL) {
34191fe6c4eeSScott Long 		sprintf(sc->ciss_logical[bus][target].cl_name, "%s%d",
34201fe6c4eeSScott Long 			periph->periph_name, periph->unit_number);
34213a31b7ebSMike Smith 		return(0);
34223a31b7ebSMike Smith 	}
342397ed09b5SSean Bruno     }
3424c6131460SPaul Saab     sc->ciss_logical[bus][target].cl_name[0] = 0;
34253a31b7ebSMike Smith     return(ENOENT);
34263a31b7ebSMike Smith }
34273a31b7ebSMike Smith 
34283a31b7ebSMike Smith /************************************************************************
34293a31b7ebSMike Smith  * Periodic status monitoring.
34303a31b7ebSMike Smith  */
34313a31b7ebSMike Smith static void
34323a31b7ebSMike Smith ciss_periodic(void *arg)
34333a31b7ebSMike Smith {
34343a31b7ebSMike Smith     struct ciss_softc	*sc;
3435e08d902aSMitsuru IWASAKI     struct ciss_request	*cr = NULL;
3436e08d902aSMitsuru IWASAKI     struct ciss_command	*cc = NULL;
3437e08d902aSMitsuru IWASAKI     int			error = 0;
34383a31b7ebSMike Smith 
34393a31b7ebSMike Smith     debug_called(1);
34403a31b7ebSMike Smith 
34413a31b7ebSMike Smith     sc = (struct ciss_softc *)arg;
34423a31b7ebSMike Smith 
34433a31b7ebSMike Smith     /*
34443a31b7ebSMike Smith      * Check the adapter heartbeat.
34453a31b7ebSMike Smith      */
34463a31b7ebSMike Smith     if (sc->ciss_cfg->heartbeat == sc->ciss_heartbeat) {
34473a31b7ebSMike Smith 	sc->ciss_heart_attack++;
34483a31b7ebSMike Smith 	debug(0, "adapter heart attack in progress 0x%x/%d",
34493a31b7ebSMike Smith 	      sc->ciss_heartbeat, sc->ciss_heart_attack);
34503a31b7ebSMike Smith 	if (sc->ciss_heart_attack == 3) {
34513a31b7ebSMike Smith 	    ciss_printf(sc, "ADAPTER HEARTBEAT FAILED\n");
3452e08d902aSMitsuru IWASAKI 	    ciss_disable_adapter(sc);
3453e08d902aSMitsuru IWASAKI 	    return;
34543a31b7ebSMike Smith 	}
34553a31b7ebSMike Smith     } else {
34563a31b7ebSMike Smith 	sc->ciss_heartbeat = sc->ciss_cfg->heartbeat;
34573a31b7ebSMike Smith 	sc->ciss_heart_attack = 0;
34583a31b7ebSMike Smith 	debug(3, "new heartbeat 0x%x", sc->ciss_heartbeat);
34593a31b7ebSMike Smith     }
34603a31b7ebSMike Smith 
34613a31b7ebSMike Smith     /*
3462e08d902aSMitsuru IWASAKI      * Send the NOP message and wait for a response.
3463e08d902aSMitsuru IWASAKI      */
34645a3c4d69SMitsuru IWASAKI     if (ciss_nop_message_heartbeat != 0 && (error = ciss_get_request(sc, &cr)) == 0) {
34652fdaa90dSScott Long 	cc = cr->cr_cc;
346654d02e0aSMitsuru IWASAKI 	cr->cr_complete = ciss_nop_complete;
3467e08d902aSMitsuru IWASAKI 	cc->cdb.cdb_length = 1;
3468e08d902aSMitsuru IWASAKI 	cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
3469e08d902aSMitsuru IWASAKI 	cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3470e08d902aSMitsuru IWASAKI 	cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
3471e08d902aSMitsuru IWASAKI 	cc->cdb.timeout = 0;
3472e08d902aSMitsuru IWASAKI 	cc->cdb.cdb[0] = CISS_OPCODE_MESSAGE_NOP;
3473e08d902aSMitsuru IWASAKI 
347454d02e0aSMitsuru IWASAKI 	if ((error = ciss_start(cr)) != 0) {
3475e08d902aSMitsuru IWASAKI 	    ciss_printf(sc, "SENDING NOP MESSAGE FAILED\n");
3476e08d902aSMitsuru IWASAKI 	}
3477e08d902aSMitsuru IWASAKI     }
3478e08d902aSMitsuru IWASAKI 
3479e08d902aSMitsuru IWASAKI     /*
34803a31b7ebSMike Smith      * If the notify event request has died for some reason, or has
34813a31b7ebSMike Smith      * not started yet, restart it.
34823a31b7ebSMike Smith      */
34833a31b7ebSMike Smith     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK)) {
34843a31b7ebSMike Smith 	debug(0, "(re)starting Event Notify chain");
34853a31b7ebSMike Smith 	ciss_notify_event(sc);
34863a31b7ebSMike Smith     }
34873a31b7ebSMike Smith 
34883a31b7ebSMike Smith     /*
34893a31b7ebSMike Smith      * Reschedule.
34903a31b7ebSMike Smith      */
3491975b7318SScott Long     callout_reset(&sc->ciss_periodic, CISS_HEARTBEAT_RATE * hz, ciss_periodic, sc);
34923a31b7ebSMike Smith }
34933a31b7ebSMike Smith 
349454d02e0aSMitsuru IWASAKI static void
349554d02e0aSMitsuru IWASAKI ciss_nop_complete(struct ciss_request *cr)
349654d02e0aSMitsuru IWASAKI {
349754d02e0aSMitsuru IWASAKI     struct ciss_softc		*sc;
34980aeee4bdSMitsuru IWASAKI     static int			first_time = 1;
349954d02e0aSMitsuru IWASAKI 
350054d02e0aSMitsuru IWASAKI     sc = cr->cr_sc;
350154d02e0aSMitsuru IWASAKI     if (ciss_report_request(cr, NULL, NULL) != 0) {
35020aeee4bdSMitsuru IWASAKI 	if (first_time == 1) {
35030aeee4bdSMitsuru IWASAKI 	    first_time = 0;
35040aeee4bdSMitsuru IWASAKI 	    ciss_printf(sc, "SENDING NOP MESSAGE FAILED (not logging anymore)\n");
35050aeee4bdSMitsuru IWASAKI 	}
350654d02e0aSMitsuru IWASAKI     }
350754d02e0aSMitsuru IWASAKI 
350854d02e0aSMitsuru IWASAKI     ciss_release_request(cr);
350954d02e0aSMitsuru IWASAKI }
351054d02e0aSMitsuru IWASAKI 
35113a31b7ebSMike Smith /************************************************************************
3512e08d902aSMitsuru IWASAKI  * Disable the adapter.
3513e08d902aSMitsuru IWASAKI  *
3514e08d902aSMitsuru IWASAKI  * The all requests in completed queue is failed with hardware error.
3515e08d902aSMitsuru IWASAKI  * This will cause failover in a multipath configuration.
3516e08d902aSMitsuru IWASAKI  */
3517e08d902aSMitsuru IWASAKI static void
3518e08d902aSMitsuru IWASAKI ciss_disable_adapter(struct ciss_softc *sc)
3519e08d902aSMitsuru IWASAKI {
352022657ce1SScott Long     cr_qhead_t			qh;
3521e08d902aSMitsuru IWASAKI     struct ciss_request		*cr;
3522e08d902aSMitsuru IWASAKI     struct ciss_command		*cc;
3523e08d902aSMitsuru IWASAKI     struct ciss_error_info	*ce;
352422657ce1SScott Long     int				i;
3525e08d902aSMitsuru IWASAKI 
3526e08d902aSMitsuru IWASAKI     CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
3527e08d902aSMitsuru IWASAKI     pci_disable_busmaster(sc->ciss_dev);
3528e08d902aSMitsuru IWASAKI     sc->ciss_flags &= ~CISS_FLAG_RUNNING;
3529e08d902aSMitsuru IWASAKI 
3530d26ae2c1SAlexander Motin     STAILQ_INIT(&qh);
353122657ce1SScott Long     for (i = 1; i < sc->ciss_max_requests; i++) {
353222657ce1SScott Long 	cr = &sc->ciss_request[i];
353322657ce1SScott Long 	if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
353422657ce1SScott Long 	    continue;
3535e08d902aSMitsuru IWASAKI 
35362fdaa90dSScott Long 	cc = cr->cr_cc;
3537e08d902aSMitsuru IWASAKI 	ce = (struct ciss_error_info *)&(cc->sg[0]);
3538e08d902aSMitsuru IWASAKI 	ce->command_status = CISS_CMD_STATUS_HARDWARE_ERROR;
353922657ce1SScott Long 	ciss_enqueue_complete(cr, &qh);
3540e08d902aSMitsuru IWASAKI     }
3541e08d902aSMitsuru IWASAKI 
3542e08d902aSMitsuru IWASAKI     for (;;) {
354322657ce1SScott Long 	if ((cr = ciss_dequeue_complete(sc, &qh)) == NULL)
3544e08d902aSMitsuru IWASAKI 	    break;
3545e08d902aSMitsuru IWASAKI 
3546e08d902aSMitsuru IWASAKI 	/*
3547e08d902aSMitsuru IWASAKI 	 * If the request has a callback, invoke it.
3548e08d902aSMitsuru IWASAKI 	 */
3549e08d902aSMitsuru IWASAKI 	if (cr->cr_complete != NULL) {
3550e08d902aSMitsuru IWASAKI 	    cr->cr_complete(cr);
3551e08d902aSMitsuru IWASAKI 	    continue;
3552e08d902aSMitsuru IWASAKI 	}
3553e08d902aSMitsuru IWASAKI 
3554e08d902aSMitsuru IWASAKI 	/*
3555e08d902aSMitsuru IWASAKI 	 * If someone is sleeping on this request, wake them up.
3556e08d902aSMitsuru IWASAKI 	 */
3557e08d902aSMitsuru IWASAKI 	if (cr->cr_flags & CISS_REQ_SLEEP) {
3558e08d902aSMitsuru IWASAKI 	    cr->cr_flags &= ~CISS_REQ_SLEEP;
3559e08d902aSMitsuru IWASAKI 	    wakeup(cr);
3560e08d902aSMitsuru IWASAKI 	    continue;
3561e08d902aSMitsuru IWASAKI 	}
3562e08d902aSMitsuru IWASAKI     }
3563e08d902aSMitsuru IWASAKI }
3564e08d902aSMitsuru IWASAKI 
3565e08d902aSMitsuru IWASAKI /************************************************************************
35663a31b7ebSMike Smith  * Request a notification response from the adapter.
35673a31b7ebSMike Smith  *
35683a31b7ebSMike Smith  * If (cr) is NULL, this is the first request of the adapter, so
35693a31b7ebSMike Smith  * reset the adapter's message pointer and start with the oldest
35703a31b7ebSMike Smith  * message available.
35713a31b7ebSMike Smith  */
35723a31b7ebSMike Smith static void
35733a31b7ebSMike Smith ciss_notify_event(struct ciss_softc *sc)
35743a31b7ebSMike Smith {
35753a31b7ebSMike Smith     struct ciss_request		*cr;
35763a31b7ebSMike Smith     struct ciss_command		*cc;
35773a31b7ebSMike Smith     struct ciss_notify_cdb	*cnc;
35783a31b7ebSMike Smith     int				error;
35793a31b7ebSMike Smith 
35803a31b7ebSMike Smith     debug_called(1);
35813a31b7ebSMike Smith 
35823a31b7ebSMike Smith     cr = sc->ciss_periodic_notify;
35833a31b7ebSMike Smith 
35843a31b7ebSMike Smith     /* get a request if we don't already have one */
35853a31b7ebSMike Smith     if (cr == NULL) {
35863a31b7ebSMike Smith 	if ((error = ciss_get_request(sc, &cr)) != 0) {
35873a31b7ebSMike Smith 	    debug(0, "can't get notify event request");
35883a31b7ebSMike Smith 	    goto out;
35893a31b7ebSMike Smith 	}
35903a31b7ebSMike Smith 	sc->ciss_periodic_notify = cr;
35913a31b7ebSMike Smith 	cr->cr_complete = ciss_notify_complete;
35923a31b7ebSMike Smith 	debug(1, "acquired request %d", cr->cr_tag);
35933a31b7ebSMike Smith     }
35943a31b7ebSMike Smith 
35953a31b7ebSMike Smith     /*
35963a31b7ebSMike Smith      * Get a databuffer if we don't already have one, note that the
35973a31b7ebSMike Smith      * adapter command wants a larger buffer than the actual
35983a31b7ebSMike Smith      * structure.
35993a31b7ebSMike Smith      */
36003a31b7ebSMike Smith     if (cr->cr_data == NULL) {
36013a31b7ebSMike Smith 	if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
36023a31b7ebSMike Smith 	    debug(0, "can't get notify event request buffer");
36033a31b7ebSMike Smith 	    error = ENOMEM;
36043a31b7ebSMike Smith 	    goto out;
36053a31b7ebSMike Smith 	}
36063a31b7ebSMike Smith 	cr->cr_length = CISS_NOTIFY_DATA_SIZE;
36073a31b7ebSMike Smith     }
36083a31b7ebSMike Smith 
36093a31b7ebSMike Smith     /* re-setup the request's command (since we never release it) XXX overkill*/
36103a31b7ebSMike Smith     ciss_preen_command(cr);
36113a31b7ebSMike Smith 
36123a31b7ebSMike Smith     /* (re)build the notify event command */
36132fdaa90dSScott Long     cc = cr->cr_cc;
36143a31b7ebSMike Smith     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
36153a31b7ebSMike Smith     cc->header.address.physical.bus = 0;
36163a31b7ebSMike Smith     cc->header.address.physical.target = 0;
36173a31b7ebSMike Smith 
36183a31b7ebSMike Smith     cc->cdb.cdb_length = sizeof(*cnc);
36193a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
36203a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
36213a31b7ebSMike Smith     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
36223a31b7ebSMike Smith     cc->cdb.timeout = 0;	/* no timeout, we hope */
36233a31b7ebSMike Smith 
36243a31b7ebSMike Smith     cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
36253a31b7ebSMike Smith     bzero(cr->cr_data, CISS_NOTIFY_DATA_SIZE);
36263a31b7ebSMike Smith     cnc->opcode = CISS_OPCODE_READ;
36273a31b7ebSMike Smith     cnc->command = CISS_COMMAND_NOTIFY_ON_EVENT;
36283a31b7ebSMike Smith     cnc->timeout = 0;		/* no timeout, we hope */
36293a31b7ebSMike Smith     cnc->synchronous = 0;
36303a31b7ebSMike Smith     cnc->ordered = 0;
36313a31b7ebSMike Smith     cnc->seek_to_oldest = 0;
36322e80ca8aSPaul Saab     if ((sc->ciss_flags & CISS_FLAG_RUNNING) == 0)
36332e80ca8aSPaul Saab 	cnc->new_only = 1;
36342e80ca8aSPaul Saab     else
36353a31b7ebSMike Smith 	cnc->new_only = 0;
36363a31b7ebSMike Smith     cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
36373a31b7ebSMike Smith 
36383a31b7ebSMike Smith     /* submit the request */
36393a31b7ebSMike Smith     error = ciss_start(cr);
36403a31b7ebSMike Smith 
36413a31b7ebSMike Smith  out:
36423a31b7ebSMike Smith     if (error) {
36433a31b7ebSMike Smith 	if (cr != NULL) {
36443a31b7ebSMike Smith 	    if (cr->cr_data != NULL)
36453a31b7ebSMike Smith 		free(cr->cr_data, CISS_MALLOC_CLASS);
36463a31b7ebSMike Smith 	    ciss_release_request(cr);
36473a31b7ebSMike Smith 	}
36483a31b7ebSMike Smith 	sc->ciss_periodic_notify = NULL;
36493a31b7ebSMike Smith 	debug(0, "can't submit notify event request");
36503a31b7ebSMike Smith 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
36513a31b7ebSMike Smith     } else {
36523a31b7ebSMike Smith 	debug(1, "notify event submitted");
36533a31b7ebSMike Smith 	sc->ciss_flags |= CISS_FLAG_NOTIFY_OK;
36543a31b7ebSMike Smith     }
36553a31b7ebSMike Smith }
36563a31b7ebSMike Smith 
36573a31b7ebSMike Smith static void
36583a31b7ebSMike Smith ciss_notify_complete(struct ciss_request *cr)
36593a31b7ebSMike Smith {
36603a31b7ebSMike Smith     struct ciss_notify	*cn;
36613a31b7ebSMike Smith     struct ciss_softc	*sc;
36623a31b7ebSMike Smith     int			scsi_status;
36633a31b7ebSMike Smith     int			command_status;
36643a31b7ebSMike Smith     debug_called(1);
36653a31b7ebSMike Smith 
36663a31b7ebSMike Smith     cn = (struct ciss_notify *)cr->cr_data;
36673a31b7ebSMike Smith     sc = cr->cr_sc;
36683a31b7ebSMike Smith 
36693a31b7ebSMike Smith     /*
36703a31b7ebSMike Smith      * Report request results, decode status.
36713a31b7ebSMike Smith      */
36723a31b7ebSMike Smith     ciss_report_request(cr, &command_status, &scsi_status);
36733a31b7ebSMike Smith 
36743a31b7ebSMike Smith     /*
36753a31b7ebSMike Smith      * Abort the chain on a fatal error.
36763a31b7ebSMike Smith      *
36773a31b7ebSMike Smith      * XXX which of these are actually errors?
36783a31b7ebSMike Smith      */
36793a31b7ebSMike Smith     if ((command_status != CISS_CMD_STATUS_SUCCESS) &&
36803a31b7ebSMike Smith 	(command_status != CISS_CMD_STATUS_TARGET_STATUS) &&
36813a31b7ebSMike Smith 	(command_status != CISS_CMD_STATUS_TIMEOUT)) {	/* XXX timeout? */
36823a31b7ebSMike Smith 	ciss_printf(sc, "fatal error in Notify Event request (%s)\n",
36833a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
36843a31b7ebSMike Smith 	ciss_release_request(cr);
36853a31b7ebSMike Smith 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
36863a31b7ebSMike Smith 	return;
36873a31b7ebSMike Smith     }
36883a31b7ebSMike Smith 
36893a31b7ebSMike Smith     /*
36903a31b7ebSMike Smith      * If the adapter gave us a text message, print it.
36913a31b7ebSMike Smith      */
36923a31b7ebSMike Smith     if (cn->message[0] != 0)
36933a31b7ebSMike Smith 	ciss_printf(sc, "*** %.80s\n", cn->message);
36943a31b7ebSMike Smith 
36953a31b7ebSMike Smith     debug(0, "notify event class %d subclass %d detail %d",
36963a31b7ebSMike Smith 		cn->class, cn->subclass, cn->detail);
36973a31b7ebSMike Smith 
36983a31b7ebSMike Smith     /*
36993a31b7ebSMike Smith      * If the response indicates that the notifier has been aborted,
37003a31b7ebSMike Smith      * release the notifier command.
37013a31b7ebSMike Smith      */
37023a31b7ebSMike Smith     if ((cn->class == CISS_NOTIFY_NOTIFIER) &&
37033a31b7ebSMike Smith 	(cn->subclass == CISS_NOTIFY_NOTIFIER_STATUS) &&
37043a31b7ebSMike Smith 	(cn->detail == 1)) {
37053a31b7ebSMike Smith 	debug(0, "notifier exiting");
37063a31b7ebSMike Smith 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
37073a31b7ebSMike Smith 	ciss_release_request(cr);
37083a31b7ebSMike Smith 	sc->ciss_periodic_notify = NULL;
37093a31b7ebSMike Smith 	wakeup(&sc->ciss_periodic_notify);
3710c6131460SPaul Saab     } else {
3711c6131460SPaul Saab 	/* Handle notify events in a kernel thread */
3712c6131460SPaul Saab 	ciss_enqueue_notify(cr);
3713c6131460SPaul Saab 	sc->ciss_periodic_notify = NULL;
3714c6131460SPaul Saab 	wakeup(&sc->ciss_periodic_notify);
3715c6131460SPaul Saab 	wakeup(&sc->ciss_notify);
37163a31b7ebSMike Smith     }
37173a31b7ebSMike Smith     /*
37183a31b7ebSMike Smith      * Send a new notify event command, if we're not aborting.
37193a31b7ebSMike Smith      */
37203a31b7ebSMike Smith     if (!(sc->ciss_flags & CISS_FLAG_ABORTING)) {
37213a31b7ebSMike Smith 	ciss_notify_event(sc);
37223a31b7ebSMike Smith     }
37233a31b7ebSMike Smith }
37243a31b7ebSMike Smith 
37253a31b7ebSMike Smith /************************************************************************
37263a31b7ebSMike Smith  * Abort the Notify Event chain.
37273a31b7ebSMike Smith  *
37283a31b7ebSMike Smith  * Note that we can't just abort the command in progress; we have to
37293a31b7ebSMike Smith  * explicitly issue an Abort Notify Event command in order for the
37303a31b7ebSMike Smith  * adapter to clean up correctly.
37313a31b7ebSMike Smith  *
37323a31b7ebSMike Smith  * If we are called with CISS_FLAG_ABORTING set in the adapter softc,
37333a31b7ebSMike Smith  * the chain will not restart itself.
37343a31b7ebSMike Smith  */
37353a31b7ebSMike Smith static int
37363a31b7ebSMike Smith ciss_notify_abort(struct ciss_softc *sc)
37373a31b7ebSMike Smith {
37383a31b7ebSMike Smith     struct ciss_request		*cr;
37393a31b7ebSMike Smith     struct ciss_command		*cc;
37403a31b7ebSMike Smith     struct ciss_notify_cdb	*cnc;
374122657ce1SScott Long     int				error, command_status, scsi_status;
37423a31b7ebSMike Smith 
37433a31b7ebSMike Smith     debug_called(1);
37443a31b7ebSMike Smith 
37453a31b7ebSMike Smith     cr = NULL;
37463a31b7ebSMike Smith     error = 0;
37473a31b7ebSMike Smith 
37483a31b7ebSMike Smith     /* verify that there's an outstanding command */
37493a31b7ebSMike Smith     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
37503a31b7ebSMike Smith 	goto out;
37513a31b7ebSMike Smith 
37523a31b7ebSMike Smith     /* get a command to issue the abort with */
37533a31b7ebSMike Smith     if ((error = ciss_get_request(sc, &cr)))
37543a31b7ebSMike Smith 	goto out;
37553a31b7ebSMike Smith 
37563a31b7ebSMike Smith     /* get a buffer for the result */
37573a31b7ebSMike Smith     if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
37583a31b7ebSMike Smith 	debug(0, "can't get notify event request buffer");
37593a31b7ebSMike Smith 	error = ENOMEM;
37603a31b7ebSMike Smith 	goto out;
37613a31b7ebSMike Smith     }
37623a31b7ebSMike Smith     cr->cr_length = CISS_NOTIFY_DATA_SIZE;
37633a31b7ebSMike Smith 
37643a31b7ebSMike Smith     /* build the CDB */
37652fdaa90dSScott Long     cc = cr->cr_cc;
37663a31b7ebSMike Smith     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
37673a31b7ebSMike Smith     cc->header.address.physical.bus = 0;
37683a31b7ebSMike Smith     cc->header.address.physical.target = 0;
37693a31b7ebSMike Smith     cc->cdb.cdb_length = sizeof(*cnc);
37703a31b7ebSMike Smith     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
37713a31b7ebSMike Smith     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
37723a31b7ebSMike Smith     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
37733a31b7ebSMike Smith     cc->cdb.timeout = 0;	/* no timeout, we hope */
37743a31b7ebSMike Smith 
37753a31b7ebSMike Smith     cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
37763a31b7ebSMike Smith     bzero(cnc, sizeof(*cnc));
37773a31b7ebSMike Smith     cnc->opcode = CISS_OPCODE_WRITE;
37783a31b7ebSMike Smith     cnc->command = CISS_COMMAND_ABORT_NOTIFY;
37793a31b7ebSMike Smith     cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
37802e3d6d0fSWarner Losh #if 0
37813a31b7ebSMike Smith     ciss_print_request(cr);
37822e3d6d0fSWarner Losh #endif
37833a31b7ebSMike Smith 
37843a31b7ebSMike Smith     /*
37853a31b7ebSMike Smith      * Submit the request and wait for it to complete.
37863a31b7ebSMike Smith      */
37873a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
37883a31b7ebSMike Smith 	ciss_printf(sc, "Abort Notify Event command failed (%d)\n", error);
37893a31b7ebSMike Smith 	goto out;
37903a31b7ebSMike Smith     }
37913a31b7ebSMike Smith 
37923a31b7ebSMike Smith     /*
37933a31b7ebSMike Smith      * Check response.
37943a31b7ebSMike Smith      */
37953a31b7ebSMike Smith     ciss_report_request(cr, &command_status, &scsi_status);
37963a31b7ebSMike Smith     switch(command_status) {
37973a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:
37983a31b7ebSMike Smith 	break;
37993a31b7ebSMike Smith     case CISS_CMD_STATUS_INVALID_COMMAND:
38003a31b7ebSMike Smith 	/*
38013a31b7ebSMike Smith 	 * Some older adapters don't support the CISS version of this
38023a31b7ebSMike Smith 	 * command.  Fall back to using the BMIC version.
38033a31b7ebSMike Smith 	 */
38043a31b7ebSMike Smith 	error = ciss_notify_abort_bmic(sc);
38053a31b7ebSMike Smith 	if (error != 0)
38063a31b7ebSMike Smith 	    goto out;
38073a31b7ebSMike Smith 	break;
38083a31b7ebSMike Smith 
38093a31b7ebSMike Smith     case CISS_CMD_STATUS_TARGET_STATUS:
38103a31b7ebSMike Smith 	/*
38113a31b7ebSMike Smith 	 * This can happen if the adapter thinks there wasn't an outstanding
38123a31b7ebSMike Smith 	 * Notify Event command but we did.  We clean up here.
38133a31b7ebSMike Smith 	 */
38143a31b7ebSMike Smith 	if (scsi_status == CISS_SCSI_STATUS_CHECK_CONDITION) {
38153a31b7ebSMike Smith 	    if (sc->ciss_periodic_notify != NULL)
38163a31b7ebSMike Smith 		ciss_release_request(sc->ciss_periodic_notify);
38173a31b7ebSMike Smith 	    error = 0;
38183a31b7ebSMike Smith 	    goto out;
38193a31b7ebSMike Smith 	}
38203a31b7ebSMike Smith 	/* FALLTHROUGH */
38213a31b7ebSMike Smith 
38223a31b7ebSMike Smith     default:
38233a31b7ebSMike Smith 	ciss_printf(sc, "Abort Notify Event command failed (%s)\n",
38243a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
38253a31b7ebSMike Smith 	error = EIO;
38263a31b7ebSMike Smith 	goto out;
38273a31b7ebSMike Smith     }
38283a31b7ebSMike Smith 
38293a31b7ebSMike Smith     /*
38303a31b7ebSMike Smith      * Sleep waiting for the notifier command to complete.  Note
38313a31b7ebSMike Smith      * that if it doesn't, we may end up in a bad situation, since
38323a31b7ebSMike Smith      * the adapter may deliver it later.  Also note that the adapter
38333a31b7ebSMike Smith      * requires the Notify Event command to be cancelled in order to
38343a31b7ebSMike Smith      * maintain internal bookkeeping.
38353a31b7ebSMike Smith      */
38363a31b7ebSMike Smith     while (sc->ciss_periodic_notify != NULL) {
3837975b7318SScott Long 	error = msleep(&sc->ciss_periodic_notify, &sc->ciss_mtx, PRIBIO, "cissNEA", hz * 5);
38383a31b7ebSMike Smith 	if (error == EWOULDBLOCK) {
38393a31b7ebSMike Smith 	    ciss_printf(sc, "Notify Event command failed to abort, adapter may wedge.\n");
38403a31b7ebSMike Smith 	    break;
38413a31b7ebSMike Smith 	}
38423a31b7ebSMike Smith     }
38433a31b7ebSMike Smith 
38443a31b7ebSMike Smith  out:
38453a31b7ebSMike Smith     /* release the cancel request */
38463a31b7ebSMike Smith     if (cr != NULL) {
38473a31b7ebSMike Smith 	if (cr->cr_data != NULL)
38483a31b7ebSMike Smith 	    free(cr->cr_data, CISS_MALLOC_CLASS);
38493a31b7ebSMike Smith 	ciss_release_request(cr);
38503a31b7ebSMike Smith     }
38513a31b7ebSMike Smith     if (error == 0)
38523a31b7ebSMike Smith 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
38533a31b7ebSMike Smith     return(error);
38543a31b7ebSMike Smith }
38553a31b7ebSMike Smith 
38563a31b7ebSMike Smith /************************************************************************
38573a31b7ebSMike Smith  * Abort the Notify Event chain using a BMIC command.
38583a31b7ebSMike Smith  */
38593a31b7ebSMike Smith static int
38603a31b7ebSMike Smith ciss_notify_abort_bmic(struct ciss_softc *sc)
38613a31b7ebSMike Smith {
38623a31b7ebSMike Smith     struct ciss_request			*cr;
38633a31b7ebSMike Smith     int					error, command_status;
38643a31b7ebSMike Smith 
38653a31b7ebSMike Smith     debug_called(1);
38663a31b7ebSMike Smith 
38673a31b7ebSMike Smith     cr = NULL;
38683a31b7ebSMike Smith     error = 0;
38693a31b7ebSMike Smith 
38703a31b7ebSMike Smith     /* verify that there's an outstanding command */
38713a31b7ebSMike Smith     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
38723a31b7ebSMike Smith 	goto out;
38733a31b7ebSMike Smith 
38743a31b7ebSMike Smith     /*
38753a31b7ebSMike Smith      * Build a BMIC command to cancel the Notify on Event command.
38763a31b7ebSMike Smith      *
38773a31b7ebSMike Smith      * Note that we are sending a CISS opcode here.  Odd.
38783a31b7ebSMike Smith      */
38793a31b7ebSMike Smith     if ((error = ciss_get_bmic_request(sc, &cr, CISS_COMMAND_ABORT_NOTIFY,
38803a31b7ebSMike Smith 				       NULL, 0)) != 0)
38813a31b7ebSMike Smith 	goto out;
38823a31b7ebSMike Smith 
38833a31b7ebSMike Smith     /*
38843a31b7ebSMike Smith      * Submit the request and wait for it to complete.
38853a31b7ebSMike Smith      */
38863a31b7ebSMike Smith     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
38873a31b7ebSMike Smith 	ciss_printf(sc, "error sending BMIC Cancel Notify on Event command (%d)\n", error);
38883a31b7ebSMike Smith 	goto out;
38893a31b7ebSMike Smith     }
38903a31b7ebSMike Smith 
38913a31b7ebSMike Smith     /*
38923a31b7ebSMike Smith      * Check response.
38933a31b7ebSMike Smith      */
38943a31b7ebSMike Smith     ciss_report_request(cr, &command_status, NULL);
38953a31b7ebSMike Smith     switch(command_status) {
38963a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:
38973a31b7ebSMike Smith 	break;
38983a31b7ebSMike Smith     default:
38993a31b7ebSMike Smith 	ciss_printf(sc, "error cancelling Notify on Event (%s)\n",
39003a31b7ebSMike Smith 		    ciss_name_command_status(command_status));
39013a31b7ebSMike Smith 	error = EIO;
39023a31b7ebSMike Smith 	goto out;
39033a31b7ebSMike Smith     }
39043a31b7ebSMike Smith 
39053a31b7ebSMike Smith out:
39063a31b7ebSMike Smith     if (cr != NULL)
39073a31b7ebSMike Smith 	ciss_release_request(cr);
39083a31b7ebSMike Smith     return(error);
39093a31b7ebSMike Smith }
39103a31b7ebSMike Smith 
39113a31b7ebSMike Smith /************************************************************************
3912c6131460SPaul Saab  * Handle rescanning all the logical volumes when a notify event
3913c6131460SPaul Saab  * causes the drives to come online or offline.
3914c6131460SPaul Saab  */
3915c6131460SPaul Saab static void
3916c6131460SPaul Saab ciss_notify_rescan_logical(struct ciss_softc *sc)
3917c6131460SPaul Saab {
3918c6131460SPaul Saab     struct ciss_lun_report      *cll;
3919c6131460SPaul Saab     struct ciss_ldrive		*ld;
3920c6131460SPaul Saab     int                         i, j, ndrives;
3921c6131460SPaul Saab 
3922c6131460SPaul Saab     /*
3923c6131460SPaul Saab      * We must rescan all logical volumes to get the right logical
3924c6131460SPaul Saab      * drive address.
3925c6131460SPaul Saab      */
3926c6131460SPaul Saab     cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
3927e3edca22SSean Bruno                            sc->ciss_cfg->max_logical_supported);
3928c6131460SPaul Saab     if (cll == NULL)
3929c6131460SPaul Saab         return;
3930c6131460SPaul Saab 
3931c6131460SPaul Saab     ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
3932c6131460SPaul Saab 
3933c6131460SPaul Saab     /*
3934c6131460SPaul Saab      * Delete any of the drives which were destroyed by the
3935c6131460SPaul Saab      * firmware.
3936c6131460SPaul Saab      */
39371fe6c4eeSScott Long     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
3938e3edca22SSean Bruno 	for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
3939c6131460SPaul Saab 	    ld = &sc->ciss_logical[i][j];
3940c6131460SPaul Saab 
3941c6131460SPaul Saab 	    if (ld->cl_update == 0)
3942c6131460SPaul Saab 		continue;
3943c6131460SPaul Saab 
3944c6131460SPaul Saab 	    if (ld->cl_status != CISS_LD_ONLINE) {
3945c6131460SPaul Saab 		ciss_cam_rescan_target(sc, i, j);
3946c6131460SPaul Saab 		ld->cl_update = 0;
3947c6131460SPaul Saab 		if (ld->cl_ldrive)
3948c6131460SPaul Saab 		    free(ld->cl_ldrive, CISS_MALLOC_CLASS);
3949c6131460SPaul Saab 		if (ld->cl_lstatus)
3950c6131460SPaul Saab 		    free(ld->cl_lstatus, CISS_MALLOC_CLASS);
3951c6131460SPaul Saab 
3952c6131460SPaul Saab 		ld->cl_ldrive = NULL;
3953c6131460SPaul Saab 		ld->cl_lstatus = NULL;
3954c6131460SPaul Saab 	    }
3955c6131460SPaul Saab 	}
3956c6131460SPaul Saab     }
3957c6131460SPaul Saab 
3958c6131460SPaul Saab     /*
3959c6131460SPaul Saab      * Scan for new drives.
3960c6131460SPaul Saab      */
3961c6131460SPaul Saab     for (i = 0; i < ndrives; i++) {
3962c6131460SPaul Saab 	int	bus, target;
3963c6131460SPaul Saab 
3964c6131460SPaul Saab 	bus 	= CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
3965c6131460SPaul Saab 	target	= CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
3966c6131460SPaul Saab 	ld	= &sc->ciss_logical[bus][target];
3967c6131460SPaul Saab 
3968c6131460SPaul Saab 	if (ld->cl_update == 0)
3969c6131460SPaul Saab 		continue;
3970c6131460SPaul Saab 
39715cf5a430SPaul Saab 	ld->cl_update		= 0;
3972c6131460SPaul Saab 	ld->cl_address		= cll->lun[i];
3973c6131460SPaul Saab 	ld->cl_controller	= &sc->ciss_controllers[bus];
3974c6131460SPaul Saab 	if (ciss_identify_logical(sc, ld) == 0) {
3975c6131460SPaul Saab 	    ciss_cam_rescan_target(sc, bus, target);
3976c6131460SPaul Saab 	}
3977c6131460SPaul Saab     }
3978c6131460SPaul Saab     free(cll, CISS_MALLOC_CLASS);
3979c6131460SPaul Saab }
3980c6131460SPaul Saab 
3981c6131460SPaul Saab /************************************************************************
39823a31b7ebSMike Smith  * Handle a notify event relating to the status of a logical drive.
39833a31b7ebSMike Smith  *
39843a31b7ebSMike Smith  * XXX need to be able to defer some of these to properly handle
39853a31b7ebSMike Smith  *     calling the "ID Physical drive" command, unless the 'extended'
39863a31b7ebSMike Smith  *     drive IDs are always in BIG_MAP format.
39873a31b7ebSMike Smith  */
39883a31b7ebSMike Smith static void
39893a31b7ebSMike Smith ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn)
39903a31b7ebSMike Smith {
39913a31b7ebSMike Smith     struct ciss_ldrive	*ld;
3992e6aa1412SSean Bruno     int			ostatus, bus, target;
39933a31b7ebSMike Smith 
39943a31b7ebSMike Smith     debug_called(2);
39953a31b7ebSMike Smith 
3996c6131460SPaul Saab     bus		= cn->device.physical.bus;
3997c6131460SPaul Saab     target	= cn->data.logical_status.logical_drive;
3998c6131460SPaul Saab     ld		= &sc->ciss_logical[bus][target];
39993a31b7ebSMike Smith 
40003a31b7ebSMike Smith     switch (cn->subclass) {
40013a31b7ebSMike Smith     case CISS_NOTIFY_LOGICAL_STATUS:
40023a31b7ebSMike Smith 	switch (cn->detail) {
40033a31b7ebSMike Smith 	case 0:
4004c6131460SPaul Saab 	    ciss_name_device(sc, bus, target);
40053a31b7ebSMike Smith 	    ciss_printf(sc, "logical drive %d (%s) changed status %s->%s, spare status 0x%b\n",
40063a31b7ebSMike Smith 			cn->data.logical_status.logical_drive, ld->cl_name,
40073a31b7ebSMike Smith 			ciss_name_ldrive_status(cn->data.logical_status.previous_state),
40083a31b7ebSMike Smith 			ciss_name_ldrive_status(cn->data.logical_status.new_state),
40093a31b7ebSMike Smith 			cn->data.logical_status.spare_state,
40103a31b7ebSMike Smith 			"\20\1configured\2rebuilding\3failed\4in use\5available\n");
40113a31b7ebSMike Smith 
40123a31b7ebSMike Smith 	    /*
40133a31b7ebSMike Smith 	     * Update our idea of the drive's status.
40143a31b7ebSMike Smith 	     */
4015e6aa1412SSean Bruno 	    ostatus = ciss_decode_ldrive_status(cn->data.logical_status.previous_state);
40163a31b7ebSMike Smith 	    ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
4017d4aa427fSPaul Saab 	    if (ld->cl_lstatus != NULL)
40183a31b7ebSMike Smith 		ld->cl_lstatus->status = cn->data.logical_status.new_state;
40193a31b7ebSMike Smith 
4020d4aa427fSPaul Saab 	    /*
4021d4aa427fSPaul Saab 	     * Have CAM rescan the drive if its status has changed.
4022d4aa427fSPaul Saab 	     */
4023e6aa1412SSean Bruno 	    if (ostatus != ld->cl_status) {
4024c6131460SPaul Saab 		ld->cl_update = 1;
4025c6131460SPaul Saab 		ciss_notify_rescan_logical(sc);
4026c6131460SPaul Saab 	    }
4027d4aa427fSPaul Saab 
40283a31b7ebSMike Smith 	    break;
40293a31b7ebSMike Smith 
40303a31b7ebSMike Smith 	case 1:	/* logical drive has recognised new media, needs Accept Media Exchange */
4031c6131460SPaul Saab 	    ciss_name_device(sc, bus, target);
40323a31b7ebSMike Smith 	    ciss_printf(sc, "logical drive %d (%s) media exchanged, ready to go online\n",
40333a31b7ebSMike Smith 			cn->data.logical_status.logical_drive, ld->cl_name);
403488c78a38SPaul Saab 	    ciss_accept_media(sc, ld);
4035139233cbSPaul Saab 
4036139233cbSPaul Saab 	    ld->cl_update = 1;
4037139233cbSPaul Saab 	    ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
4038139233cbSPaul Saab 	    ciss_notify_rescan_logical(sc);
40393a31b7ebSMike Smith 	    break;
40403a31b7ebSMike Smith 
40413a31b7ebSMike Smith 	case 2:
40423a31b7ebSMike Smith 	case 3:
40433a31b7ebSMike Smith 	    ciss_printf(sc, "rebuild of logical drive %d (%s) failed due to %s error\n",
40443a31b7ebSMike Smith 			cn->data.rebuild_aborted.logical_drive,
4045c6131460SPaul Saab 			ld->cl_name,
40463a31b7ebSMike Smith 			(cn->detail == 2) ? "read" : "write");
40473a31b7ebSMike Smith 	    break;
40483a31b7ebSMike Smith 	}
40493a31b7ebSMike Smith 	break;
40503a31b7ebSMike Smith 
40513a31b7ebSMike Smith     case CISS_NOTIFY_LOGICAL_ERROR:
40523a31b7ebSMike Smith 	if (cn->detail == 0) {
40533a31b7ebSMike Smith 	    ciss_printf(sc, "FATAL I/O ERROR on logical drive %d (%s), SCSI port %d ID %d\n",
40543a31b7ebSMike Smith 			cn->data.io_error.logical_drive,
4055c6131460SPaul Saab 			ld->cl_name,
40563a31b7ebSMike Smith 			cn->data.io_error.failure_bus,
40573a31b7ebSMike Smith 			cn->data.io_error.failure_drive);
40583a31b7ebSMike Smith 	    /* XXX should we take the drive down at this point, or will we be told? */
40593a31b7ebSMike Smith 	}
40603a31b7ebSMike Smith 	break;
40613a31b7ebSMike Smith 
40623a31b7ebSMike Smith     case CISS_NOTIFY_LOGICAL_SURFACE:
40633a31b7ebSMike Smith 	if (cn->detail == 0)
40643a31b7ebSMike Smith 	    ciss_printf(sc, "logical drive %d (%s) completed consistency initialisation\n",
40653a31b7ebSMike Smith 			cn->data.consistency_completed.logical_drive,
4066c6131460SPaul Saab 			ld->cl_name);
40673a31b7ebSMike Smith 	break;
40683a31b7ebSMike Smith     }
40693a31b7ebSMike Smith }
40703a31b7ebSMike Smith 
40713a31b7ebSMike Smith /************************************************************************
40723a31b7ebSMike Smith  * Handle a notify event relating to the status of a physical drive.
40733a31b7ebSMike Smith  */
40743a31b7ebSMike Smith static void
40753a31b7ebSMike Smith ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn)
40763a31b7ebSMike Smith {
4077c6131460SPaul Saab }
40783a31b7ebSMike Smith 
4079c6131460SPaul Saab /************************************************************************
40801fe6c4eeSScott Long  * Handle a notify event relating to the status of a physical drive.
40811fe6c4eeSScott Long  */
40821fe6c4eeSScott Long static void
40831fe6c4eeSScott Long ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn)
40841fe6c4eeSScott Long {
4085c1885ab8SPaul Saab     struct ciss_lun_report *cll = NULL;
40861fe6c4eeSScott Long     int bus, target;
40871fe6c4eeSScott Long 
40881fe6c4eeSScott Long     switch (cn->subclass) {
40891fe6c4eeSScott Long     case CISS_NOTIFY_HOTPLUG_PHYSICAL:
40901fe6c4eeSScott Long     case CISS_NOTIFY_HOTPLUG_NONDISK:
40911fe6c4eeSScott Long 	bus = CISS_BIG_MAP_BUS(sc, cn->data.drive.big_physical_drive_number);
40921fe6c4eeSScott Long 	target =
40931fe6c4eeSScott Long 	    CISS_BIG_MAP_TARGET(sc, cn->data.drive.big_physical_drive_number);
40941fe6c4eeSScott Long 
40951fe6c4eeSScott Long 	if (cn->detail == 0) {
40961fe6c4eeSScott Long 	    /*
40971fe6c4eeSScott Long 	     * Mark the device offline so that it'll start producing selection
40981fe6c4eeSScott Long 	     * timeouts to the upper layer.
40991fe6c4eeSScott Long 	     */
41005f265711SScott Long 	    if ((bus >= 0) && (target >= 0))
41011fe6c4eeSScott Long 		sc->ciss_physical[bus][target].cp_online = 0;
41021fe6c4eeSScott Long 	} else {
41031fe6c4eeSScott Long 	    /*
41041fe6c4eeSScott Long 	     * Rescan the physical lun list for new items
41051fe6c4eeSScott Long 	     */
41061fe6c4eeSScott Long 	    cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
4107e3edca22SSean Bruno 				   sc->ciss_cfg->max_physical_supported);
41081fe6c4eeSScott Long 	    if (cll == NULL) {
41091fe6c4eeSScott Long 		ciss_printf(sc, "Warning, cannot get physical lun list\n");
41101fe6c4eeSScott Long 		break;
41111fe6c4eeSScott Long 	    }
41121fe6c4eeSScott Long 	    ciss_filter_physical(sc, cll);
41131fe6c4eeSScott Long 	}
41141fe6c4eeSScott Long 	break;
41151fe6c4eeSScott Long 
41161fe6c4eeSScott Long     default:
41171fe6c4eeSScott Long 	ciss_printf(sc, "Unknown hotplug event %d\n", cn->subclass);
41181fe6c4eeSScott Long 	return;
41191fe6c4eeSScott Long     }
4120c1885ab8SPaul Saab 
4121c1885ab8SPaul Saab     if (cll != NULL)
4122c1885ab8SPaul Saab 	free(cll, CISS_MALLOC_CLASS);
41231fe6c4eeSScott Long }
41241fe6c4eeSScott Long 
41251fe6c4eeSScott Long /************************************************************************
4126c6131460SPaul Saab  * Handle deferred processing of notify events.  Notify events may need
4127c6131460SPaul Saab  * sleep which is unsafe during an interrupt.
4128c6131460SPaul Saab  */
4129c6131460SPaul Saab static void
4130c6131460SPaul Saab ciss_notify_thread(void *arg)
4131c6131460SPaul Saab {
4132c6131460SPaul Saab     struct ciss_softc		*sc;
4133c6131460SPaul Saab     struct ciss_request		*cr;
4134c6131460SPaul Saab     struct ciss_notify		*cn;
4135c6131460SPaul Saab 
4136c6131460SPaul Saab     sc = (struct ciss_softc *)arg;
4137975b7318SScott Long     mtx_lock(&sc->ciss_mtx);
4138c6131460SPaul Saab 
4139c6131460SPaul Saab     for (;;) {
414022657ce1SScott Long 	if (STAILQ_EMPTY(&sc->ciss_notify) != 0 &&
4141c6131460SPaul Saab 	    (sc->ciss_flags & CISS_FLAG_THREAD_SHUT) == 0) {
4142975b7318SScott Long 	    msleep(&sc->ciss_notify, &sc->ciss_mtx, PUSER, "idle", 0);
4143c6131460SPaul Saab 	}
4144c6131460SPaul Saab 
4145c6131460SPaul Saab 	if (sc->ciss_flags & CISS_FLAG_THREAD_SHUT)
4146c6131460SPaul Saab 	    break;
4147c6131460SPaul Saab 
4148c6131460SPaul Saab 	cr = ciss_dequeue_notify(sc);
4149c6131460SPaul Saab 
4150c6131460SPaul Saab 	if (cr == NULL)
4151c6131460SPaul Saab 		panic("cr null");
4152c6131460SPaul Saab 	cn = (struct ciss_notify *)cr->cr_data;
4153c6131460SPaul Saab 
4154c6131460SPaul Saab 	switch (cn->class) {
41551fe6c4eeSScott Long 	case CISS_NOTIFY_HOTPLUG:
41561fe6c4eeSScott Long 	    ciss_notify_hotplug(sc, cn);
41571fe6c4eeSScott Long 	    break;
4158c6131460SPaul Saab 	case CISS_NOTIFY_LOGICAL:
4159c6131460SPaul Saab 	    ciss_notify_logical(sc, cn);
4160c6131460SPaul Saab 	    break;
4161c6131460SPaul Saab 	case CISS_NOTIFY_PHYSICAL:
4162c6131460SPaul Saab 	    ciss_notify_physical(sc, cn);
4163c6131460SPaul Saab 	    break;
4164c6131460SPaul Saab 	}
4165c6131460SPaul Saab 
4166c6131460SPaul Saab 	ciss_release_request(cr);
4167c6131460SPaul Saab     }
4168c6131460SPaul Saab     sc->ciss_notify_thread = NULL;
4169c6131460SPaul Saab     wakeup(&sc->ciss_notify_thread);
4170c6131460SPaul Saab 
4171975b7318SScott Long     mtx_unlock(&sc->ciss_mtx);
41723745c395SJulian Elischer     kproc_exit(0);
4173c6131460SPaul Saab }
4174c6131460SPaul Saab 
4175c6131460SPaul Saab /************************************************************************
4176c6131460SPaul Saab  * Start the notification kernel thread.
4177c6131460SPaul Saab  */
4178c6131460SPaul Saab static void
4179c6131460SPaul Saab ciss_spawn_notify_thread(struct ciss_softc *sc)
4180c6131460SPaul Saab {
4181c6131460SPaul Saab 
41823745c395SJulian Elischer     if (kproc_create((void(*)(void *))ciss_notify_thread, sc,
4183c6131460SPaul Saab 		       &sc->ciss_notify_thread, 0, 0, "ciss_notify%d",
4184c6131460SPaul Saab 		       device_get_unit(sc->ciss_dev)))
4185c6131460SPaul Saab 	panic("Could not create notify thread\n");
4186c6131460SPaul Saab }
4187c6131460SPaul Saab 
4188c6131460SPaul Saab /************************************************************************
4189c6131460SPaul Saab  * Kill the notification kernel thread.
4190c6131460SPaul Saab  */
4191c6131460SPaul Saab static void
4192c6131460SPaul Saab ciss_kill_notify_thread(struct ciss_softc *sc)
4193c6131460SPaul Saab {
4194c6131460SPaul Saab 
4195c6131460SPaul Saab     if (sc->ciss_notify_thread == NULL)
4196c6131460SPaul Saab 	return;
4197c6131460SPaul Saab 
4198c6131460SPaul Saab     sc->ciss_flags |= CISS_FLAG_THREAD_SHUT;
4199c6131460SPaul Saab     wakeup(&sc->ciss_notify);
4200975b7318SScott Long     msleep(&sc->ciss_notify_thread, &sc->ciss_mtx, PUSER, "thtrm", 0);
42013a31b7ebSMike Smith }
42023a31b7ebSMike Smith 
42033a31b7ebSMike Smith /************************************************************************
42043a31b7ebSMike Smith  * Print a request.
42053a31b7ebSMike Smith  */
42062e3d6d0fSWarner Losh #ifdef DDB
42073a31b7ebSMike Smith static void
42083a31b7ebSMike Smith ciss_print_request(struct ciss_request *cr)
42093a31b7ebSMike Smith {
42103a31b7ebSMike Smith     struct ciss_softc	*sc;
42113a31b7ebSMike Smith     struct ciss_command	*cc;
42123a31b7ebSMike Smith     int			i;
42133a31b7ebSMike Smith 
42143a31b7ebSMike Smith     sc = cr->cr_sc;
42152fdaa90dSScott Long     cc = cr->cr_cc;
42163a31b7ebSMike Smith 
42173a31b7ebSMike Smith     ciss_printf(sc, "REQUEST @ %p\n", cr);
42183a31b7ebSMike Smith     ciss_printf(sc, "  data %p/%d  tag %d  flags %b\n",
42193a31b7ebSMike Smith 	      cr->cr_data, cr->cr_length, cr->cr_tag, cr->cr_flags,
42203a31b7ebSMike Smith 	      "\20\1mapped\2sleep\3poll\4dataout\5datain\n");
42213a31b7ebSMike Smith     ciss_printf(sc, "  sg list/total %d/%d  host tag 0x%x\n",
42223a31b7ebSMike Smith 		cc->header.sg_in_list, cc->header.sg_total, cc->header.host_tag);
42233a31b7ebSMike Smith     switch(cc->header.address.mode.mode) {
42243a31b7ebSMike Smith     case CISS_HDR_ADDRESS_MODE_PERIPHERAL:
42253a31b7ebSMike Smith     case CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL:
42263a31b7ebSMike Smith 	ciss_printf(sc, "  physical bus %d target %d\n",
42273a31b7ebSMike Smith 		    cc->header.address.physical.bus, cc->header.address.physical.target);
42283a31b7ebSMike Smith 	break;
42293a31b7ebSMike Smith     case CISS_HDR_ADDRESS_MODE_LOGICAL:
42303a31b7ebSMike Smith 	ciss_printf(sc, "  logical unit %d\n", cc->header.address.logical.lun);
42313a31b7ebSMike Smith 	break;
42323a31b7ebSMike Smith     }
42333a31b7ebSMike Smith     ciss_printf(sc, "  %s cdb length %d type %s attribute %s\n",
42343a31b7ebSMike Smith 		(cc->cdb.direction == CISS_CDB_DIRECTION_NONE) ? "no-I/O" :
42353a31b7ebSMike Smith 		(cc->cdb.direction == CISS_CDB_DIRECTION_READ) ? "READ" :
42363a31b7ebSMike Smith 		(cc->cdb.direction == CISS_CDB_DIRECTION_WRITE) ? "WRITE" : "??",
42373a31b7ebSMike Smith 		cc->cdb.cdb_length,
42383a31b7ebSMike Smith 		(cc->cdb.type == CISS_CDB_TYPE_COMMAND) ? "command" :
42393a31b7ebSMike Smith 		(cc->cdb.type == CISS_CDB_TYPE_MESSAGE) ? "message" : "??",
42403a31b7ebSMike Smith 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_UNTAGGED) ? "untagged" :
42413a31b7ebSMike Smith 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_SIMPLE) ? "simple" :
42423a31b7ebSMike Smith 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_HEAD_OF_QUEUE) ? "head-of-queue" :
42433a31b7ebSMike Smith 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_ORDERED) ? "ordered" :
42443a31b7ebSMike Smith 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_AUTO_CONTINGENT) ? "auto-contingent" : "??");
42453a31b7ebSMike Smith     ciss_printf(sc, "  %*D\n", cc->cdb.cdb_length, &cc->cdb.cdb[0], " ");
42463a31b7ebSMike Smith 
42473a31b7ebSMike Smith     if (cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) {
42483a31b7ebSMike Smith 	/* XXX print error info */
42493a31b7ebSMike Smith     } else {
42503a31b7ebSMike Smith 	/* since we don't use chained s/g, don't support it here */
42513a31b7ebSMike Smith 	for (i = 0; i < cc->header.sg_in_list; i++) {
42523a31b7ebSMike Smith 	    if ((i % 4) == 0)
42533a31b7ebSMike Smith 		ciss_printf(sc, "   ");
42543a31b7ebSMike Smith 	    printf("0x%08x/%d ", (u_int32_t)cc->sg[i].address, cc->sg[i].length);
42553a31b7ebSMike Smith 	    if ((((i + 1) % 4) == 0) || (i == (cc->header.sg_in_list - 1)))
42563a31b7ebSMike Smith 		printf("\n");
42573a31b7ebSMike Smith 	}
42583a31b7ebSMike Smith     }
42593a31b7ebSMike Smith }
42602e3d6d0fSWarner Losh #endif
42613a31b7ebSMike Smith 
42623a31b7ebSMike Smith /************************************************************************
42633a31b7ebSMike Smith  * Print information about the status of a logical drive.
42643a31b7ebSMike Smith  */
42653a31b7ebSMike Smith static void
42663a31b7ebSMike Smith ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld)
42673a31b7ebSMike Smith {
42683a31b7ebSMike Smith     int		bus, target, i;
42693a31b7ebSMike Smith 
4270d4aa427fSPaul Saab     if (ld->cl_lstatus == NULL) {
4271d4aa427fSPaul Saab 	printf("does not exist\n");
4272d4aa427fSPaul Saab 	return;
4273d4aa427fSPaul Saab     }
4274d4aa427fSPaul Saab 
42753a31b7ebSMike Smith     /* print drive status */
42763a31b7ebSMike Smith     switch(ld->cl_lstatus->status) {
42773a31b7ebSMike Smith     case CISS_LSTATUS_OK:
42783a31b7ebSMike Smith 	printf("online\n");
42793a31b7ebSMike Smith 	break;
42803a31b7ebSMike Smith     case CISS_LSTATUS_INTERIM_RECOVERY:
42813a31b7ebSMike Smith 	printf("in interim recovery mode\n");
42823a31b7ebSMike Smith 	break;
42833a31b7ebSMike Smith     case CISS_LSTATUS_READY_RECOVERY:
42843a31b7ebSMike Smith 	printf("ready to begin recovery\n");
42853a31b7ebSMike Smith 	break;
42863a31b7ebSMike Smith     case CISS_LSTATUS_RECOVERING:
42873a31b7ebSMike Smith 	bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
42883a31b7ebSMike Smith 	target = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
42893a31b7ebSMike Smith 	printf("being recovered, working on physical drive %d.%d, %u blocks remaining\n",
42903a31b7ebSMike Smith 	       bus, target, ld->cl_lstatus->blocks_to_recover);
42913a31b7ebSMike Smith 	break;
42923a31b7ebSMike Smith     case CISS_LSTATUS_EXPANDING:
42933a31b7ebSMike Smith 	printf("being expanded, %u blocks remaining\n",
42943a31b7ebSMike Smith 	       ld->cl_lstatus->blocks_to_recover);
42953a31b7ebSMike Smith 	break;
42963a31b7ebSMike Smith     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
42973a31b7ebSMike Smith 	printf("queued for expansion\n");
42983a31b7ebSMike Smith 	break;
42993a31b7ebSMike Smith     case CISS_LSTATUS_FAILED:
43003a31b7ebSMike Smith 	printf("queued for expansion\n");
43013a31b7ebSMike Smith 	break;
43023a31b7ebSMike Smith     case CISS_LSTATUS_WRONG_PDRIVE:
43033a31b7ebSMike Smith 	printf("wrong physical drive inserted\n");
43043a31b7ebSMike Smith 	break;
43053a31b7ebSMike Smith     case CISS_LSTATUS_MISSING_PDRIVE:
43063a31b7ebSMike Smith 	printf("missing a needed physical drive\n");
43073a31b7ebSMike Smith 	break;
43083a31b7ebSMike Smith     case CISS_LSTATUS_BECOMING_READY:
43093a31b7ebSMike Smith 	printf("becoming ready\n");
43103a31b7ebSMike Smith 	break;
43113a31b7ebSMike Smith     }
43123a31b7ebSMike Smith 
4313d4aa427fSPaul Saab     /* print failed physical drives */
43143a31b7ebSMike Smith     for (i = 0; i < CISS_BIG_MAP_ENTRIES / 8; i++) {
43153a31b7ebSMike Smith 	bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_failure_map[i]);
43163a31b7ebSMike Smith 	target = CISS_BIG_MAP_TARGET(sc, ld->cl_lstatus->drive_failure_map[i]);
43173a31b7ebSMike Smith 	if (bus == -1)
43183a31b7ebSMike Smith 	    continue;
43193a31b7ebSMike Smith 	ciss_printf(sc, "physical drive %d:%d (%x) failed\n", bus, target,
43203a31b7ebSMike Smith 		    ld->cl_lstatus->drive_failure_map[i]);
43213a31b7ebSMike Smith     }
43223a31b7ebSMike Smith }
43233a31b7ebSMike Smith 
4324b27556e0SSean Bruno #ifdef DDB
4325b27556e0SSean Bruno #include <ddb/ddb.h>
4326d4aa427fSPaul Saab /************************************************************************
4327d4aa427fSPaul Saab  * Print information about the controller/driver.
4328d4aa427fSPaul Saab  */
4329d4aa427fSPaul Saab static void
4330d4aa427fSPaul Saab ciss_print_adapter(struct ciss_softc *sc)
4331d4aa427fSPaul Saab {
4332609caf8dSPaul Saab     int		i, j;
4333d4aa427fSPaul Saab 
4334d4aa427fSPaul Saab     ciss_printf(sc, "ADAPTER:\n");
4335d4aa427fSPaul Saab     for (i = 0; i < CISSQ_COUNT; i++) {
4336d4aa427fSPaul Saab 	ciss_printf(sc, "%s     %d/%d\n",
4337d4aa427fSPaul Saab 	    i == 0 ? "free" :
4338d4aa427fSPaul Saab 	    i == 1 ? "busy" : "complete",
4339d4aa427fSPaul Saab 	    sc->ciss_qstat[i].q_length,
4340d4aa427fSPaul Saab 	    sc->ciss_qstat[i].q_max);
4341d4aa427fSPaul Saab     }
4342d4aa427fSPaul Saab     ciss_printf(sc, "max_requests %d\n", sc->ciss_max_requests);
4343d4aa427fSPaul Saab     ciss_printf(sc, "flags %b\n", sc->ciss_flags,
4344d4aa427fSPaul Saab 	"\20\1notify_ok\2control_open\3aborting\4running\21fake_synch\22bmic_abort\n");
4345d4aa427fSPaul Saab 
43461fe6c4eeSScott Long     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
4347e3edca22SSean Bruno 	for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
4348d4aa427fSPaul Saab 	    ciss_printf(sc, "LOGICAL DRIVE %d:  ", i);
4349609caf8dSPaul Saab 	    ciss_print_ldrive(sc, &sc->ciss_logical[i][j]);
4350609caf8dSPaul Saab 	}
4351d4aa427fSPaul Saab     }
4352d4aa427fSPaul Saab 
43531fe6c4eeSScott Long     /* XXX Should physical drives be printed out here? */
43541fe6c4eeSScott Long 
4355d4aa427fSPaul Saab     for (i = 1; i < sc->ciss_max_requests; i++)
4356d4aa427fSPaul Saab 	ciss_print_request(sc->ciss_request + i);
4357d4aa427fSPaul Saab }
4358d4aa427fSPaul Saab 
4359d4aa427fSPaul Saab /* DDB hook */
4360b27556e0SSean Bruno DB_COMMAND(ciss_prt, db_ciss_prt)
4361d4aa427fSPaul Saab {
4362d4aa427fSPaul Saab     struct ciss_softc	*sc;
436311f9f6eaSSean Bruno     devclass_t dc;
436411f9f6eaSSean Bruno     int maxciss, i;
4365d4aa427fSPaul Saab 
436611f9f6eaSSean Bruno     dc = devclass_find("ciss");
436711f9f6eaSSean Bruno     if ( dc == NULL ) {
436811f9f6eaSSean Bruno         printf("%s: can't find devclass!\n", __func__);
436911f9f6eaSSean Bruno         return;
437011f9f6eaSSean Bruno     }
437111f9f6eaSSean Bruno     maxciss = devclass_get_maxunit(dc);
437211f9f6eaSSean Bruno     for (i = 0; i < maxciss; i++) {
437311f9f6eaSSean Bruno         sc = devclass_get_softc(dc, i);
4374d4aa427fSPaul Saab 	ciss_print_adapter(sc);
4375d4aa427fSPaul Saab     }
4376d4aa427fSPaul Saab }
4377d4aa427fSPaul Saab #endif
4378d4aa427fSPaul Saab 
43793a31b7ebSMike Smith /************************************************************************
43803a31b7ebSMike Smith  * Return a name for a logical drive status value.
43813a31b7ebSMike Smith  */
43823a31b7ebSMike Smith static const char *
43833a31b7ebSMike Smith ciss_name_ldrive_status(int status)
43843a31b7ebSMike Smith {
43853a31b7ebSMike Smith     switch (status) {
43863a31b7ebSMike Smith     case CISS_LSTATUS_OK:
43873a31b7ebSMike Smith 	return("OK");
43883a31b7ebSMike Smith     case CISS_LSTATUS_FAILED:
43893a31b7ebSMike Smith 	return("failed");
43903a31b7ebSMike Smith     case CISS_LSTATUS_NOT_CONFIGURED:
43913a31b7ebSMike Smith 	return("not configured");
43923a31b7ebSMike Smith     case CISS_LSTATUS_INTERIM_RECOVERY:
43933a31b7ebSMike Smith 	return("interim recovery");
43943a31b7ebSMike Smith     case CISS_LSTATUS_READY_RECOVERY:
43953a31b7ebSMike Smith 	return("ready for recovery");
43963a31b7ebSMike Smith     case CISS_LSTATUS_RECOVERING:
43973a31b7ebSMike Smith 	return("recovering");
43983a31b7ebSMike Smith     case CISS_LSTATUS_WRONG_PDRIVE:
43993a31b7ebSMike Smith 	return("wrong physical drive inserted");
44003a31b7ebSMike Smith     case CISS_LSTATUS_MISSING_PDRIVE:
44013a31b7ebSMike Smith 	return("missing physical drive");
44023a31b7ebSMike Smith     case CISS_LSTATUS_EXPANDING:
44033a31b7ebSMike Smith 	return("expanding");
44043a31b7ebSMike Smith     case CISS_LSTATUS_BECOMING_READY:
44053a31b7ebSMike Smith 	return("becoming ready");
44063a31b7ebSMike Smith     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
44073a31b7ebSMike Smith 	return("queued for expansion");
44083a31b7ebSMike Smith     }
44093a31b7ebSMike Smith     return("unknown status");
44103a31b7ebSMike Smith }
44113a31b7ebSMike Smith 
44123a31b7ebSMike Smith /************************************************************************
44133a31b7ebSMike Smith  * Return an online/offline/nonexistent value for a logical drive
44143a31b7ebSMike Smith  * status value.
44153a31b7ebSMike Smith  */
44163a31b7ebSMike Smith static int
44173a31b7ebSMike Smith ciss_decode_ldrive_status(int status)
44183a31b7ebSMike Smith {
44193a31b7ebSMike Smith     switch(status) {
44203a31b7ebSMike Smith     case CISS_LSTATUS_NOT_CONFIGURED:
44213a31b7ebSMike Smith 	return(CISS_LD_NONEXISTENT);
44223a31b7ebSMike Smith 
44233a31b7ebSMike Smith     case CISS_LSTATUS_OK:
44243a31b7ebSMike Smith     case CISS_LSTATUS_INTERIM_RECOVERY:
44253a31b7ebSMike Smith     case CISS_LSTATUS_READY_RECOVERY:
44263a31b7ebSMike Smith     case CISS_LSTATUS_RECOVERING:
44273a31b7ebSMike Smith     case CISS_LSTATUS_EXPANDING:
44283a31b7ebSMike Smith     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
44293a31b7ebSMike Smith 	return(CISS_LD_ONLINE);
44303a31b7ebSMike Smith 
44313a31b7ebSMike Smith     case CISS_LSTATUS_FAILED:
44323a31b7ebSMike Smith     case CISS_LSTATUS_WRONG_PDRIVE:
44333a31b7ebSMike Smith     case CISS_LSTATUS_MISSING_PDRIVE:
44343a31b7ebSMike Smith     case CISS_LSTATUS_BECOMING_READY:
44353a31b7ebSMike Smith     default:
44363a31b7ebSMike Smith 	return(CISS_LD_OFFLINE);
44373a31b7ebSMike Smith     }
44383a31b7ebSMike Smith }
44393a31b7ebSMike Smith 
44403a31b7ebSMike Smith /************************************************************************
44413a31b7ebSMike Smith  * Return a name for a logical drive's organisation.
44423a31b7ebSMike Smith  */
44433a31b7ebSMike Smith static const char *
44443a31b7ebSMike Smith ciss_name_ldrive_org(int org)
44453a31b7ebSMike Smith {
44463a31b7ebSMike Smith     switch(org) {
44473a31b7ebSMike Smith     case CISS_LDRIVE_RAID0:
44483a31b7ebSMike Smith 	return("RAID 0");
44493a31b7ebSMike Smith     case CISS_LDRIVE_RAID1:
4450d000b49dSKonstantin Belousov 	return("RAID 1(1+0)");
44513a31b7ebSMike Smith     case CISS_LDRIVE_RAID4:
44523a31b7ebSMike Smith 	return("RAID 4");
44533a31b7ebSMike Smith     case CISS_LDRIVE_RAID5:
44543a31b7ebSMike Smith 	return("RAID 5");
4455aaf8327bSPaul Saab     case CISS_LDRIVE_RAID51:
4456aaf8327bSPaul Saab 	return("RAID 5+1");
4457aaf8327bSPaul Saab     case CISS_LDRIVE_RAIDADG:
4458aaf8327bSPaul Saab 	return("RAID ADG");
44593a31b7ebSMike Smith     }
4460b790c193SPedro F. Giffuni     return("unknown");
44613a31b7ebSMike Smith }
44623a31b7ebSMike Smith 
44633a31b7ebSMike Smith /************************************************************************
44643a31b7ebSMike Smith  * Return a name for a command status value.
44653a31b7ebSMike Smith  */
44663a31b7ebSMike Smith static const char *
44673a31b7ebSMike Smith ciss_name_command_status(int status)
44683a31b7ebSMike Smith {
44693a31b7ebSMike Smith     switch(status) {
44703a31b7ebSMike Smith     case CISS_CMD_STATUS_SUCCESS:
44713a31b7ebSMike Smith 	return("success");
44723a31b7ebSMike Smith     case CISS_CMD_STATUS_TARGET_STATUS:
44733a31b7ebSMike Smith 	return("target status");
44743a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_UNDERRUN:
44753a31b7ebSMike Smith 	return("data underrun");
44763a31b7ebSMike Smith     case CISS_CMD_STATUS_DATA_OVERRUN:
44773a31b7ebSMike Smith 	return("data overrun");
44783a31b7ebSMike Smith     case CISS_CMD_STATUS_INVALID_COMMAND:
44793a31b7ebSMike Smith 	return("invalid command");
44803a31b7ebSMike Smith     case CISS_CMD_STATUS_PROTOCOL_ERROR:
44813a31b7ebSMike Smith 	return("protocol error");
44823a31b7ebSMike Smith     case CISS_CMD_STATUS_HARDWARE_ERROR:
44833a31b7ebSMike Smith 	return("hardware error");
44843a31b7ebSMike Smith     case CISS_CMD_STATUS_CONNECTION_LOST:
44853a31b7ebSMike Smith 	return("connection lost");
44863a31b7ebSMike Smith     case CISS_CMD_STATUS_ABORTED:
44873a31b7ebSMike Smith 	return("aborted");
44883a31b7ebSMike Smith     case CISS_CMD_STATUS_ABORT_FAILED:
44893a31b7ebSMike Smith 	return("abort failed");
44903a31b7ebSMike Smith     case CISS_CMD_STATUS_UNSOLICITED_ABORT:
44913a31b7ebSMike Smith 	return("unsolicited abort");
44923a31b7ebSMike Smith     case CISS_CMD_STATUS_TIMEOUT:
44933a31b7ebSMike Smith 	return("timeout");
44943a31b7ebSMike Smith     case CISS_CMD_STATUS_UNABORTABLE:
44953a31b7ebSMike Smith 	return("unabortable");
44963a31b7ebSMike Smith     }
44973a31b7ebSMike Smith     return("unknown status");
44983a31b7ebSMike Smith }
44993a31b7ebSMike Smith 
45003a31b7ebSMike Smith /************************************************************************
45013a31b7ebSMike Smith  * Handle an open on the control device.
45023a31b7ebSMike Smith  */
45033a31b7ebSMike Smith static int
450400b4e54aSWarner Losh ciss_open(struct cdev *dev, int flags, int fmt, struct thread *p)
45053a31b7ebSMike Smith {
45063a31b7ebSMike Smith     struct ciss_softc	*sc;
45073a31b7ebSMike Smith 
45083a31b7ebSMike Smith     debug_called(1);
45093a31b7ebSMike Smith 
45103a31b7ebSMike Smith     sc = (struct ciss_softc *)dev->si_drv1;
45113a31b7ebSMike Smith 
45123a31b7ebSMike Smith     /* we might want to veto if someone already has us open */
45133a31b7ebSMike Smith 
4514975b7318SScott Long     mtx_lock(&sc->ciss_mtx);
45153a31b7ebSMike Smith     sc->ciss_flags |= CISS_FLAG_CONTROL_OPEN;
4516975b7318SScott Long     mtx_unlock(&sc->ciss_mtx);
45173a31b7ebSMike Smith     return(0);
45183a31b7ebSMike Smith }
45193a31b7ebSMike Smith 
45203a31b7ebSMike Smith /************************************************************************
45213a31b7ebSMike Smith  * Handle the last close on the control device.
45223a31b7ebSMike Smith  */
45233a31b7ebSMike Smith static int
452400b4e54aSWarner Losh ciss_close(struct cdev *dev, int flags, int fmt, struct thread *p)
45253a31b7ebSMike Smith {
45263a31b7ebSMike Smith     struct ciss_softc	*sc;
45273a31b7ebSMike Smith 
45283a31b7ebSMike Smith     debug_called(1);
45293a31b7ebSMike Smith 
45303a31b7ebSMike Smith     sc = (struct ciss_softc *)dev->si_drv1;
45313a31b7ebSMike Smith 
4532975b7318SScott Long     mtx_lock(&sc->ciss_mtx);
45333a31b7ebSMike Smith     sc->ciss_flags &= ~CISS_FLAG_CONTROL_OPEN;
4534975b7318SScott Long     mtx_unlock(&sc->ciss_mtx);
45353a31b7ebSMike Smith     return (0);
45363a31b7ebSMike Smith }
45373a31b7ebSMike Smith 
45383a31b7ebSMike Smith /********************************************************************************
45393a31b7ebSMike Smith  * Handle adapter-specific control operations.
45403a31b7ebSMike Smith  *
45413a31b7ebSMike Smith  * Note that the API here is compatible with the Linux driver, in order to
45423a31b7ebSMike Smith  * simplify the porting of Compaq's userland tools.
45433a31b7ebSMike Smith  */
45443a31b7ebSMike Smith static int
454500b4e54aSWarner Losh ciss_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *p)
45463a31b7ebSMike Smith {
45473a31b7ebSMike Smith     struct ciss_softc		*sc;
45487caeec6aSPaul Saab     IOCTL_Command_struct	*ioc	= (IOCTL_Command_struct *)addr;
45497caeec6aSPaul Saab #ifdef __amd64__
45507caeec6aSPaul Saab     IOCTL_Command_struct32	*ioc32	= (IOCTL_Command_struct32 *)addr;
45517caeec6aSPaul Saab     IOCTL_Command_struct	ioc_swab;
45527caeec6aSPaul Saab #endif
45533a31b7ebSMike Smith     int				error;
45543a31b7ebSMike Smith 
45553a31b7ebSMike Smith     debug_called(1);
45563a31b7ebSMike Smith 
45573a31b7ebSMike Smith     sc = (struct ciss_softc *)dev->si_drv1;
45583a31b7ebSMike Smith     error = 0;
4559975b7318SScott Long     mtx_lock(&sc->ciss_mtx);
45603a31b7ebSMike Smith 
45613a31b7ebSMike Smith     switch(cmd) {
456222657ce1SScott Long     case CCISS_GETQSTATS:
456322657ce1SScott Long     {
456422657ce1SScott Long 	union ciss_statrequest *cr = (union ciss_statrequest *)addr;
456522657ce1SScott Long 
456622657ce1SScott Long 	switch (cr->cs_item) {
456722657ce1SScott Long 	case CISSQ_FREE:
456822657ce1SScott Long 	case CISSQ_NOTIFY:
456922657ce1SScott Long 	    bcopy(&sc->ciss_qstat[cr->cs_item], &cr->cs_qstat,
457022657ce1SScott Long 		sizeof(struct ciss_qstat));
457122657ce1SScott Long 	    break;
457222657ce1SScott Long 	default:
457322657ce1SScott Long 	    error = ENOIOCTL;
457422657ce1SScott Long 	    break;
457522657ce1SScott Long 	}
457622657ce1SScott Long 
457722657ce1SScott Long 	break;
457822657ce1SScott Long     }
457922657ce1SScott Long 
45803a31b7ebSMike Smith     case CCISS_GETPCIINFO:
45813a31b7ebSMike Smith     {
45823a31b7ebSMike Smith 	cciss_pci_info_struct	*pis = (cciss_pci_info_struct *)addr;
45833a31b7ebSMike Smith 
45843a31b7ebSMike Smith 	pis->bus = pci_get_bus(sc->ciss_dev);
45853a31b7ebSMike Smith 	pis->dev_fn = pci_get_slot(sc->ciss_dev);
4586e17ef005SSean Bruno         pis->board_id = (pci_get_subvendor(sc->ciss_dev) << 16) |
4587e17ef005SSean Bruno                 pci_get_subdevice(sc->ciss_dev);
45883a31b7ebSMike Smith 
45893a31b7ebSMike Smith 	break;
45903a31b7ebSMike Smith     }
45913a31b7ebSMike Smith 
45923a31b7ebSMike Smith     case CCISS_GETINTINFO:
45933a31b7ebSMike Smith     {
45943a31b7ebSMike Smith 	cciss_coalint_struct	*cis = (cciss_coalint_struct *)addr;
45953a31b7ebSMike Smith 
45963a31b7ebSMike Smith 	cis->delay = sc->ciss_cfg->interrupt_coalesce_delay;
45973a31b7ebSMike Smith 	cis->count = sc->ciss_cfg->interrupt_coalesce_count;
45983a31b7ebSMike Smith 
45993a31b7ebSMike Smith 	break;
46003a31b7ebSMike Smith     }
46013a31b7ebSMike Smith 
46023a31b7ebSMike Smith     case CCISS_SETINTINFO:
46033a31b7ebSMike Smith     {
46043a31b7ebSMike Smith 	cciss_coalint_struct	*cis = (cciss_coalint_struct *)addr;
46053a31b7ebSMike Smith 
46063a31b7ebSMike Smith 	if ((cis->delay == 0) && (cis->count == 0)) {
46073a31b7ebSMike Smith 	    error = EINVAL;
46083a31b7ebSMike Smith 	    break;
46093a31b7ebSMike Smith 	}
46103a31b7ebSMike Smith 
46113a31b7ebSMike Smith 	/*
46123a31b7ebSMike Smith 	 * XXX apparently this is only safe if the controller is idle,
46133a31b7ebSMike Smith 	 *     we should suspend it before doing this.
46143a31b7ebSMike Smith 	 */
46153a31b7ebSMike Smith 	sc->ciss_cfg->interrupt_coalesce_delay = cis->delay;
46163a31b7ebSMike Smith 	sc->ciss_cfg->interrupt_coalesce_count = cis->count;
46173a31b7ebSMike Smith 
46183a31b7ebSMike Smith 	if (ciss_update_config(sc))
46193a31b7ebSMike Smith 	    error = EIO;
46203a31b7ebSMike Smith 
46213a31b7ebSMike Smith 	/* XXX resume the controller here */
46223a31b7ebSMike Smith 	break;
46233a31b7ebSMike Smith     }
46243a31b7ebSMike Smith 
46253a31b7ebSMike Smith     case CCISS_GETNODENAME:
46263a31b7ebSMike Smith 	bcopy(sc->ciss_cfg->server_name, (NodeName_type *)addr,
46273a31b7ebSMike Smith 	      sizeof(NodeName_type));
46283a31b7ebSMike Smith 	break;
46293a31b7ebSMike Smith 
46303a31b7ebSMike Smith     case CCISS_SETNODENAME:
46313a31b7ebSMike Smith 	bcopy((NodeName_type *)addr, sc->ciss_cfg->server_name,
46323a31b7ebSMike Smith 	      sizeof(NodeName_type));
46333a31b7ebSMike Smith 	if (ciss_update_config(sc))
46343a31b7ebSMike Smith 	    error = EIO;
46353a31b7ebSMike Smith 	break;
46363a31b7ebSMike Smith 
46373a31b7ebSMike Smith     case CCISS_GETHEARTBEAT:
46383a31b7ebSMike Smith 	*(Heartbeat_type *)addr = sc->ciss_cfg->heartbeat;
46393a31b7ebSMike Smith 	break;
46403a31b7ebSMike Smith 
46413a31b7ebSMike Smith     case CCISS_GETBUSTYPES:
46423a31b7ebSMike Smith 	*(BusTypes_type *)addr = sc->ciss_cfg->bus_types;
46433a31b7ebSMike Smith 	break;
46443a31b7ebSMike Smith 
46453a31b7ebSMike Smith     case CCISS_GETFIRMVER:
46463a31b7ebSMike Smith 	bcopy(sc->ciss_id->running_firmware_revision, (FirmwareVer_type *)addr,
46473a31b7ebSMike Smith 	      sizeof(FirmwareVer_type));
46483a31b7ebSMike Smith 	break;
46493a31b7ebSMike Smith 
46503a31b7ebSMike Smith     case CCISS_GETDRIVERVER:
46513a31b7ebSMike Smith 	*(DriverVer_type *)addr = CISS_DRIVER_VERSION;
46523a31b7ebSMike Smith 	break;
46533a31b7ebSMike Smith 
46543a31b7ebSMike Smith     case CCISS_REVALIDVOLS:
46553a31b7ebSMike Smith 	/*
46563a31b7ebSMike Smith 	 * This is a bit ugly; to do it "right" we really need
46573a31b7ebSMike Smith 	 * to find any disks that have changed, kick CAM off them,
46583a31b7ebSMike Smith 	 * then rescan only these disks.  It'd be nice if they
46593a31b7ebSMike Smith 	 * a) told us which disk(s) they were going to play with,
46603a31b7ebSMike Smith 	 * and b) which ones had arrived. 8(
46613a31b7ebSMike Smith 	 */
46623a31b7ebSMike Smith 	break;
46633a31b7ebSMike Smith 
46647caeec6aSPaul Saab #ifdef __amd64__
46657caeec6aSPaul Saab     case CCISS_PASSTHRU32:
46667caeec6aSPaul Saab 	ioc_swab.LUN_info	= ioc32->LUN_info;
46677caeec6aSPaul Saab 	ioc_swab.Request	= ioc32->Request;
46687caeec6aSPaul Saab 	ioc_swab.error_info	= ioc32->error_info;
46697caeec6aSPaul Saab 	ioc_swab.buf_size	= ioc32->buf_size;
46707caeec6aSPaul Saab 	ioc_swab.buf		= (u_int8_t *)(uintptr_t)ioc32->buf;
46717caeec6aSPaul Saab 	ioc			= &ioc_swab;
46727caeec6aSPaul Saab 	/* FALLTHROUGH */
46737caeec6aSPaul Saab #endif
46747caeec6aSPaul Saab 
46753a31b7ebSMike Smith     case CCISS_PASSTHRU:
46767caeec6aSPaul Saab 	error = ciss_user_command(sc, ioc);
46773a31b7ebSMike Smith 	break;
46783a31b7ebSMike Smith 
46793a31b7ebSMike Smith     default:
46803a31b7ebSMike Smith 	debug(0, "unknown ioctl 0x%lx", cmd);
46813a31b7ebSMike Smith 
46823a31b7ebSMike Smith 	debug(1, "CCISS_GETPCIINFO:   0x%lx", CCISS_GETPCIINFO);
46833a31b7ebSMike Smith 	debug(1, "CCISS_GETINTINFO:   0x%lx", CCISS_GETINTINFO);
46843a31b7ebSMike Smith 	debug(1, "CCISS_SETINTINFO:   0x%lx", CCISS_SETINTINFO);
46853a31b7ebSMike Smith 	debug(1, "CCISS_GETNODENAME:  0x%lx", CCISS_GETNODENAME);
46863a31b7ebSMike Smith 	debug(1, "CCISS_SETNODENAME:  0x%lx", CCISS_SETNODENAME);
46873a31b7ebSMike Smith 	debug(1, "CCISS_GETHEARTBEAT: 0x%lx", CCISS_GETHEARTBEAT);
46883a31b7ebSMike Smith 	debug(1, "CCISS_GETBUSTYPES:  0x%lx", CCISS_GETBUSTYPES);
46893a31b7ebSMike Smith 	debug(1, "CCISS_GETFIRMVER:   0x%lx", CCISS_GETFIRMVER);
46903a31b7ebSMike Smith 	debug(1, "CCISS_GETDRIVERVER: 0x%lx", CCISS_GETDRIVERVER);
46913a31b7ebSMike Smith 	debug(1, "CCISS_REVALIDVOLS:  0x%lx", CCISS_REVALIDVOLS);
46923a31b7ebSMike Smith 	debug(1, "CCISS_PASSTHRU:     0x%lx", CCISS_PASSTHRU);
46933a31b7ebSMike Smith 
46943a31b7ebSMike Smith 	error = ENOIOCTL;
46953a31b7ebSMike Smith 	break;
46963a31b7ebSMike Smith     }
46973a31b7ebSMike Smith 
4698975b7318SScott Long     mtx_unlock(&sc->ciss_mtx);
46993a31b7ebSMike Smith     return(error);
47003a31b7ebSMike Smith }
4701