Home | History | Annotate | Line # | Download | only in ic
mlx.c revision 1.36
      1  1.36  drochner /*	$NetBSD: mlx.c,v 1.36 2005/08/25 22:33:19 drochner Exp $	*/
      2   1.1        ad 
      3   1.1        ad /*-
      4   1.1        ad  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5   1.1        ad  * All rights reserved.
      6   1.1        ad  *
      7   1.1        ad  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1        ad  * by Andrew Doran.
      9   1.1        ad  *
     10   1.1        ad  * Redistribution and use in source and binary forms, with or without
     11   1.1        ad  * modification, are permitted provided that the following conditions
     12   1.1        ad  * are met:
     13   1.1        ad  * 1. Redistributions of source code must retain the above copyright
     14   1.1        ad  *    notice, this list of conditions and the following disclaimer.
     15   1.1        ad  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1        ad  *    notice, this list of conditions and the following disclaimer in the
     17   1.1        ad  *    documentation and/or other materials provided with the distribution.
     18   1.1        ad  * 3. All advertising materials mentioning features or use of this software
     19   1.1        ad  *    must display the following acknowledgement:
     20   1.1        ad  *        This product includes software developed by the NetBSD
     21   1.1        ad  *        Foundation, Inc. and its contributors.
     22   1.1        ad  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1        ad  *    contributors may be used to endorse or promote products derived
     24   1.1        ad  *    from this software without specific prior written permission.
     25   1.1        ad  *
     26   1.1        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1        ad  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1        ad  */
     38   1.1        ad 
     39   1.1        ad /*-
     40   1.1        ad  * Copyright (c) 1999 Michael Smith
     41   1.1        ad  * All rights reserved.
     42   1.1        ad  *
     43   1.1        ad  * Redistribution and use in source and binary forms, with or without
     44   1.1        ad  * modification, are permitted provided that the following conditions
     45   1.1        ad  * are met:
     46   1.1        ad  * 1. Redistributions of source code must retain the above copyright
     47   1.1        ad  *    notice, this list of conditions and the following disclaimer.
     48   1.1        ad  * 2. Redistributions in binary form must reproduce the above copyright
     49   1.1        ad  *    notice, this list of conditions and the following disclaimer in the
     50   1.1        ad  *    documentation and/or other materials provided with the distribution.
     51   1.1        ad  *
     52   1.1        ad  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     53   1.1        ad  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54   1.1        ad  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55   1.1        ad  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     56   1.1        ad  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57   1.1        ad  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58   1.1        ad  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59   1.1        ad  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60   1.1        ad  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61   1.1        ad  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62   1.1        ad  * SUCH DAMAGE.
     63   1.1        ad  *
     64   1.1        ad  * from FreeBSD: mlx.c,v 1.14.2.3 2000/08/04 06:52:50 msmith Exp
     65   1.1        ad  */
     66   1.1        ad 
     67   1.1        ad /*
     68   1.1        ad  * Driver for the Mylex DAC960 family of RAID controllers.
     69   1.1        ad  *
     70   1.1        ad  * TODO:
     71   1.1        ad  *
     72   1.1        ad  * o Test and enable channel pause.
     73   1.1        ad  * o SCSI pass-through.
     74   1.1        ad  */
     75  1.15     lukem 
     76  1.15     lukem #include <sys/cdefs.h>
     77  1.36  drochner __KERNEL_RCSID(0, "$NetBSD: mlx.c,v 1.36 2005/08/25 22:33:19 drochner Exp $");
     78   1.1        ad 
     79   1.1        ad #include "ld.h"
     80   1.1        ad 
     81   1.1        ad #include <sys/param.h>
     82   1.1        ad #include <sys/systm.h>
     83   1.1        ad #include <sys/kernel.h>
     84   1.1        ad #include <sys/device.h>
     85   1.1        ad #include <sys/queue.h>
     86   1.1        ad #include <sys/proc.h>
     87   1.1        ad #include <sys/buf.h>
     88  1.31      yamt #include <sys/bufq.h>
     89   1.1        ad #include <sys/endian.h>
     90   1.1        ad #include <sys/malloc.h>
     91   1.1        ad #include <sys/conf.h>
     92   1.1        ad #include <sys/kthread.h>
     93   1.1        ad #include <sys/disk.h>
     94   1.1        ad 
     95   1.1        ad #include <machine/vmparam.h>
     96   1.1        ad #include <machine/bus.h>
     97   1.2     enami 
     98   1.2     enami #include <uvm/uvm_extern.h>
     99   1.1        ad 
    100   1.1        ad #include <dev/ldvar.h>
    101   1.1        ad 
    102   1.1        ad #include <dev/ic/mlxreg.h>
    103   1.1        ad #include <dev/ic/mlxio.h>
    104   1.1        ad #include <dev/ic/mlxvar.h>
    105   1.1        ad 
    106  1.30  drochner #include "locators.h"
    107  1.30  drochner 
    108   1.1        ad #define	MLX_TIMEOUT	60
    109   1.1        ad 
    110   1.1        ad #ifdef DIAGNOSTIC
    111   1.1        ad #define	DPRINTF(x)	printf x
    112   1.1        ad #else
    113   1.1        ad #define	DPRINTF(x)
    114   1.1        ad #endif
    115   1.1        ad 
    116   1.1        ad static void	mlx_adjqparam(struct mlx_softc *, int, int);
    117   1.1        ad static int	mlx_ccb_submit(struct mlx_softc *, struct mlx_ccb *);
    118   1.1        ad static int	mlx_check(struct mlx_softc *, int);
    119   1.1        ad static void	mlx_configure(struct mlx_softc *, int);
    120   1.1        ad static void	mlx_describe(struct mlx_softc *);
    121   1.1        ad static void	*mlx_enquire(struct mlx_softc *, int, size_t,
    122   1.1        ad 			     void (*)(struct mlx_ccb *), int);
    123   1.1        ad static int	mlx_fw_message(struct mlx_softc *, int, int, int);
    124   1.1        ad static void	mlx_pause_action(struct mlx_softc *);
    125   1.1        ad static void	mlx_pause_done(struct mlx_ccb *);
    126   1.1        ad static void	mlx_periodic(struct mlx_softc *);
    127   1.1        ad static void	mlx_periodic_create(void *);
    128   1.1        ad static void	mlx_periodic_enquiry(struct mlx_ccb *);
    129   1.1        ad static void	mlx_periodic_eventlog_poll(struct mlx_softc *);
    130   1.1        ad static void	mlx_periodic_eventlog_respond(struct mlx_ccb *);
    131   1.1        ad static void	mlx_periodic_rebuild(struct mlx_ccb *);
    132   1.1        ad static void	mlx_periodic_thread(void *);
    133   1.1        ad static int	mlx_print(void *, const char *);
    134   1.1        ad static int	mlx_rebuild(struct mlx_softc *, int, int);
    135   1.1        ad static void	mlx_shutdown(void *);
    136   1.1        ad static int	mlx_user_command(struct mlx_softc *, struct mlx_usercommand *);
    137   1.1        ad 
    138   1.1        ad static __inline__ time_t	mlx_curtime(void);
    139   1.1        ad 
    140  1.20   gehenna dev_type_open(mlxopen);
    141  1.20   gehenna dev_type_close(mlxclose);
    142  1.20   gehenna dev_type_ioctl(mlxioctl);
    143  1.20   gehenna 
    144  1.20   gehenna const struct cdevsw mlx_cdevsw = {
    145  1.20   gehenna 	mlxopen, mlxclose, noread, nowrite, mlxioctl,
    146  1.25  jdolecek 	nostop, notty, nopoll, nommap, nokqfilter,
    147  1.20   gehenna };
    148   1.1        ad 
    149  1.32     perry extern struct	cfdriver mlx_cd;
    150   1.1        ad static struct	proc *mlx_periodic_proc;
    151   1.1        ad static void	*mlx_sdh;
    152   1.1        ad 
    153   1.1        ad struct {
    154   1.1        ad 	int	hwid;
    155   1.1        ad 	const char	*name;
    156   1.1        ad } static const mlx_cname[] = {
    157  1.18        ad 	{ 0x00, "960E/960M" },
    158   1.1        ad 	{ 0x01, "960P/PD" },
    159   1.1        ad 	{ 0x02,	"960PL" },
    160   1.1        ad 	{ 0x10, "960PG" },
    161   1.1        ad 	{ 0x11, "960PJ" },
    162   1.1        ad 	{ 0x12, "960PR" },
    163   1.1        ad 	{ 0x13,	"960PT" },
    164   1.1        ad 	{ 0x14, "960PTL0" },
    165   1.1        ad 	{ 0x15, "960PRL" },
    166   1.1        ad 	{ 0x16, "960PTL1" },
    167   1.1        ad 	{ 0x20, "1164PVX" },
    168   1.1        ad };
    169   1.1        ad 
    170   1.1        ad static const char * const mlx_sense_msgs[] = {
    171   1.1        ad 	"because write recovery failed",
    172   1.1        ad 	"because of SCSI bus reset failure",
    173   1.1        ad 	"because of double check condition",
    174   1.1        ad 	"because it was removed",
    175   1.1        ad 	"because of gross error on SCSI chip",
    176   1.1        ad 	"because of bad tag returned from drive",
    177   1.1        ad 	"because of timeout on SCSI command",
    178   1.1        ad 	"because of reset SCSI command issued from system",
    179   1.1        ad 	"because busy or parity error count exceeded limit",
    180   1.1        ad 	"because of 'kill drive' command from system",
    181   1.1        ad 	"because of selection timeout",
    182   1.1        ad 	"due to SCSI phase sequence error",
    183   1.1        ad 	"due to unknown status"
    184   1.1        ad };
    185   1.1        ad 
    186   1.1        ad static const char * const mlx_status_msgs[] = {
    187   1.1        ad 	"normal completion",				/* 0 */
    188   1.1        ad 	"irrecoverable data error",			/* 1 */
    189   1.1        ad 	"drive does not exist, or is offline",		/* 2 */
    190   1.1        ad 	"attempt to write beyond end of drive",		/* 3 */
    191   1.1        ad 	"bad data encountered",				/* 4 */
    192   1.1        ad 	"invalid log entry request",			/* 5 */
    193   1.1        ad 	"attempt to rebuild online drive",		/* 6 */
    194   1.1        ad 	"new disk failed during rebuild",		/* 7 */
    195   1.1        ad 	"invalid channel/target",			/* 8 */
    196   1.1        ad 	"rebuild/check already in progress",		/* 9 */
    197   1.1        ad 	"one or more disks are dead",			/* 10 */
    198   1.1        ad 	"invalid or non-redundant drive",		/* 11 */
    199   1.1        ad 	"channel is busy",				/* 12 */
    200   1.1        ad 	"channel is not stopped",			/* 13 */
    201   1.1        ad 	"rebuild successfully terminated",		/* 14 */
    202   1.1        ad 	"unsupported command",				/* 15 */
    203   1.1        ad 	"check condition received",			/* 16 */
    204   1.1        ad 	"device is busy",				/* 17 */
    205   1.1        ad 	"selection or command timeout",			/* 18 */
    206   1.1        ad 	"command terminated abnormally",		/* 19 */
    207   1.1        ad 	"controller wedged",				/* 20 */
    208   1.1        ad 	"software timeout",				/* 21 */
    209   1.7     lukem 	"command busy (?)",				/* 22 */
    210   1.1        ad };
    211   1.1        ad 
    212   1.1        ad struct {
    213   1.1        ad 	u_char	command;
    214   1.1        ad 	u_char	msg;		/* Index into mlx_status_msgs[]. */
    215   1.1        ad 	u_short	status;
    216   1.1        ad } static const mlx_msgs[] = {
    217   1.1        ad 	{ MLX_CMD_READSG,	1,	0x0001 },
    218   1.1        ad 	{ MLX_CMD_READSG,	1,	0x0002 },
    219   1.1        ad 	{ MLX_CMD_READSG,	3,	0x0105 },
    220   1.1        ad 	{ MLX_CMD_READSG,	4,	0x010c },
    221   1.1        ad 	{ MLX_CMD_WRITESG,	1,	0x0001 },
    222   1.1        ad 	{ MLX_CMD_WRITESG,	1,	0x0002 },
    223   1.1        ad 	{ MLX_CMD_WRITESG,	3,	0x0105 },
    224   1.1        ad 	{ MLX_CMD_READSG_OLD,	1,	0x0001 },
    225   1.1        ad 	{ MLX_CMD_READSG_OLD,	1,	0x0002 },
    226   1.1        ad 	{ MLX_CMD_READSG_OLD,	3,	0x0105 },
    227   1.1        ad 	{ MLX_CMD_WRITESG_OLD,	1,	0x0001 },
    228   1.1        ad 	{ MLX_CMD_WRITESG_OLD,	1,	0x0002 },
    229   1.1        ad 	{ MLX_CMD_WRITESG_OLD,	3,	0x0105 },
    230   1.1        ad 	{ MLX_CMD_LOGOP,	5,	0x0105 },
    231   1.1        ad 	{ MLX_CMD_REBUILDASYNC,	6,	0x0002 },
    232   1.1        ad 	{ MLX_CMD_REBUILDASYNC,	7,	0x0004 },
    233   1.1        ad 	{ MLX_CMD_REBUILDASYNC,	8,	0x0105 },
    234   1.1        ad 	{ MLX_CMD_REBUILDASYNC,	9,	0x0106 },
    235   1.1        ad 	{ MLX_CMD_REBUILDASYNC,	14,	0x0107 },
    236   1.1        ad 	{ MLX_CMD_CHECKASYNC,	10,	0x0002 },
    237   1.1        ad 	{ MLX_CMD_CHECKASYNC,	11,	0x0105 },
    238   1.1        ad 	{ MLX_CMD_CHECKASYNC,	9,	0x0106 },
    239   1.1        ad 	{ MLX_CMD_STOPCHANNEL,	12,	0x0106 },
    240   1.1        ad 	{ MLX_CMD_STOPCHANNEL,	8,	0x0105 },
    241   1.1        ad 	{ MLX_CMD_STARTCHANNEL,	13,	0x0005 },
    242   1.1        ad 	{ MLX_CMD_STARTCHANNEL,	8,	0x0105 },
    243   1.1        ad 	{ MLX_CMD_DIRECT_CDB,	16,	0x0002 },
    244   1.1        ad 	{ MLX_CMD_DIRECT_CDB,	17,	0x0008 },
    245   1.1        ad 	{ MLX_CMD_DIRECT_CDB,	18,	0x000e },
    246   1.1        ad 	{ MLX_CMD_DIRECT_CDB,	19,	0x000f },
    247   1.1        ad 	{ MLX_CMD_DIRECT_CDB,	8,	0x0105 },
    248   1.1        ad 
    249   1.1        ad 	{ 0,			20,	MLX_STATUS_WEDGED },
    250   1.1        ad 	{ 0,			21,	MLX_STATUS_LOST },
    251   1.1        ad 	{ 0,			22,	MLX_STATUS_BUSY },
    252   1.1        ad 
    253   1.1        ad 	{ 0,			14,	0x0104 },
    254   1.1        ad };
    255   1.1        ad 
    256   1.1        ad /*
    257   1.1        ad  * Return the current time in seconds - we're not particularly interested in
    258   1.1        ad  * precision here.
    259   1.1        ad  */
    260   1.1        ad static __inline__ time_t
    261   1.1        ad mlx_curtime(void)
    262   1.1        ad {
    263   1.1        ad 	time_t rt;
    264   1.1        ad 	int s;
    265   1.1        ad 
    266   1.1        ad 	s = splclock();
    267   1.1        ad 	rt = mono_time.tv_sec;
    268   1.1        ad 	splx(s);
    269   1.1        ad 
    270   1.1        ad 	return (rt);
    271   1.1        ad }
    272   1.1        ad 
    273   1.1        ad /*
    274   1.1        ad  * Initialise the controller and our interface.
    275   1.1        ad  */
    276   1.1        ad void
    277   1.1        ad mlx_init(struct mlx_softc *mlx, const char *intrstr)
    278   1.1        ad {
    279   1.1        ad 	struct mlx_ccb *mc;
    280   1.1        ad 	struct mlx_enquiry_old *meo;
    281  1.18        ad 	struct mlx_enquiry2 *me2;
    282  1.18        ad 	struct mlx_cinfo *ci;
    283   1.1        ad 	int rv, fwminor, hscode, hserr, hsparam1, hsparam2, hsmsg;
    284   1.1        ad 	int size, i, rseg;
    285   1.1        ad 	const char *wantfwstr;
    286   1.1        ad 	bus_dma_segment_t seg;
    287   1.1        ad 
    288   1.1        ad 	SIMPLEQ_INIT(&mlx->mlx_ccb_queue);
    289   1.1        ad 	SLIST_INIT(&mlx->mlx_ccb_freelist);
    290   1.1        ad 	TAILQ_INIT(&mlx->mlx_ccb_worklist);
    291   1.1        ad 
    292   1.1        ad 	if (intrstr != NULL)
    293   1.1        ad 		printf("%s: interrupting at %s\n", mlx->mlx_dv.dv_xname,
    294   1.1        ad 		    intrstr);
    295   1.1        ad 
    296   1.1        ad 	/*
    297   1.1        ad 	 * Allocate the scatter/gather lists.
    298   1.1        ad 	 */
    299   1.1        ad         size = MLX_SGL_SIZE * MLX_MAX_QUEUECNT;
    300   1.1        ad 
    301   1.1        ad 	if ((rv = bus_dmamem_alloc(mlx->mlx_dmat, size, PAGE_SIZE, 0, &seg, 1,
    302   1.1        ad 	    &rseg, BUS_DMA_NOWAIT)) != 0) {
    303   1.1        ad 		printf("%s: unable to allocate sglists, rv = %d\n",
    304   1.1        ad 		    mlx->mlx_dv.dv_xname, rv);
    305   1.1        ad 		return;
    306   1.1        ad 	}
    307   1.1        ad 
    308  1.32     perry 	if ((rv = bus_dmamem_map(mlx->mlx_dmat, &seg, rseg, size,
    309   1.1        ad 	    (caddr_t *)&mlx->mlx_sgls,
    310   1.1        ad 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    311   1.1        ad 		printf("%s: unable to map sglists, rv = %d\n",
    312   1.1        ad 		    mlx->mlx_dv.dv_xname, rv);
    313   1.1        ad 		return;
    314   1.1        ad 	}
    315   1.1        ad 
    316  1.32     perry 	if ((rv = bus_dmamap_create(mlx->mlx_dmat, size, 1, size, 0,
    317   1.9        ad 	    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &mlx->mlx_dmamap)) != 0) {
    318   1.1        ad 		printf("%s: unable to create sglist DMA map, rv = %d\n",
    319   1.1        ad 		    mlx->mlx_dv.dv_xname, rv);
    320   1.1        ad 		return;
    321   1.1        ad 	}
    322   1.1        ad 
    323   1.1        ad 	if ((rv = bus_dmamap_load(mlx->mlx_dmat, mlx->mlx_dmamap,
    324   1.1        ad 	    mlx->mlx_sgls, size, NULL, BUS_DMA_NOWAIT)) != 0) {
    325   1.1        ad 		printf("%s: unable to load sglist DMA map, rv = %d\n",
    326   1.1        ad 		    mlx->mlx_dv.dv_xname, rv);
    327   1.1        ad 		return;
    328   1.1        ad 	}
    329   1.1        ad 
    330   1.1        ad 	mlx->mlx_sgls_paddr = mlx->mlx_dmamap->dm_segs[0].ds_addr;
    331   1.1        ad 	memset(mlx->mlx_sgls, 0, size);
    332   1.1        ad 
    333   1.1        ad 	/*
    334  1.14       wiz 	 * Allocate and initialize the CCBs.
    335   1.1        ad 	 */
    336   1.1        ad 	mc = malloc(sizeof(*mc) * MLX_MAX_QUEUECNT, M_DEVBUF, M_NOWAIT);
    337   1.1        ad 	mlx->mlx_ccbs = mc;
    338   1.1        ad 
    339   1.1        ad 	for (i = 0; i < MLX_MAX_QUEUECNT; i++, mc++) {
    340   1.1        ad 		mc->mc_ident = i;
    341   1.1        ad 		rv = bus_dmamap_create(mlx->mlx_dmat, MLX_MAX_XFER,
    342   1.9        ad 		    MLX_MAX_SEGS, MLX_MAX_XFER, 0,
    343   1.1        ad 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
    344   1.1        ad 		    &mc->mc_xfer_map);
    345   1.1        ad 		if (rv != 0)
    346   1.1        ad 			break;
    347   1.1        ad 		mlx->mlx_nccbs++;
    348   1.1        ad 		mlx_ccb_free(mlx, mc);
    349   1.1        ad 	}
    350   1.1        ad 	if (mlx->mlx_nccbs != MLX_MAX_QUEUECNT)
    351   1.1        ad 		printf("%s: %d/%d CCBs usable\n", mlx->mlx_dv.dv_xname,
    352   1.1        ad 		    mlx->mlx_nccbs, MLX_MAX_QUEUECNT);
    353   1.1        ad 
    354   1.1        ad 	/* Disable interrupts before we start talking to the controller */
    355   1.1        ad 	(*mlx->mlx_intaction)(mlx, 0);
    356   1.1        ad 
    357   1.8        ad 	/* If we've got a reset routine, then reset the controller now. */
    358   1.8        ad 	if (mlx->mlx_reset != NULL) {
    359   1.8        ad 		printf("%s: resetting controller...\n", mlx->mlx_dv.dv_xname);
    360   1.8        ad 		if ((*mlx->mlx_reset)(mlx) != 0) {
    361   1.8        ad 			printf("%s: reset failed\n", mlx->mlx_dv.dv_xname);
    362   1.8        ad 			return;
    363   1.8        ad 		}
    364   1.8        ad 	}
    365   1.8        ad 
    366  1.32     perry 	/*
    367   1.1        ad 	 * Wait for the controller to come ready, handshaking with the
    368   1.1        ad 	 * firmware if required.  This is typically only necessary on
    369   1.1        ad 	 * platforms where the controller BIOS does not run.
    370   1.1        ad 	 */
    371   1.1        ad 	hsmsg = 0;
    372   1.1        ad 
    373   1.1        ad 	for (;;) {
    374   1.1        ad 		hscode = (*mlx->mlx_fw_handshake)(mlx, &hserr, &hsparam1,
    375   1.1        ad 		    &hsparam2);
    376   1.1        ad 		if (hscode == 0) {
    377   1.1        ad 			if (hsmsg != 0)
    378   1.1        ad 				printf("%s: initialization complete\n",
    379   1.1        ad 				    mlx->mlx_dv.dv_xname);
    380   1.1        ad 			break;
    381   1.1        ad 		}
    382   1.1        ad 
    383   1.1        ad 		/* Report first time around... */
    384   1.1        ad 		if (hsmsg == 0) {
    385   1.1        ad 			printf("%s: initializing (may take some time)...\n",
    386   1.1        ad 			    mlx->mlx_dv.dv_xname);
    387   1.1        ad 			hsmsg = 1;
    388   1.1        ad 		}
    389   1.1        ad 
    390   1.1        ad 		/* Did we get a real message? */
    391   1.1        ad 		if (hscode == 2) {
    392   1.1        ad 			hscode = mlx_fw_message(mlx, hserr, hsparam1, hsparam2);
    393   1.1        ad 
    394   1.1        ad 			/* Fatal initialisation error? */
    395   1.1        ad 			if (hscode != 0)
    396   1.1        ad 				return;
    397   1.1        ad 		}
    398   1.1        ad 	}
    399   1.1        ad 
    400   1.1        ad 	/*
    401   1.1        ad 	 * Do quirk/feature related things.
    402   1.1        ad 	 */
    403  1.18        ad 	ci = &mlx->mlx_ci;
    404  1.18        ad 
    405  1.18        ad 	if (ci->ci_iftype > 1) {
    406  1.18        ad 		me2 = mlx_enquire(mlx, MLX_CMD_ENQUIRY2,
    407  1.18        ad 		    sizeof(struct mlx_enquiry2), NULL, 0);
    408  1.18        ad 		if (me2 == NULL) {
    409  1.18        ad 			printf("%s: ENQUIRY2 failed\n", mlx->mlx_dv.dv_xname);
    410  1.18        ad 			return;
    411  1.18        ad 		}
    412  1.18        ad 
    413  1.18        ad 		ci->ci_firmware_id[0] = me2->me_firmware_id[0];
    414  1.18        ad 		ci->ci_firmware_id[1] = me2->me_firmware_id[1];
    415  1.18        ad 		ci->ci_firmware_id[2] = me2->me_firmware_id[2];
    416  1.18        ad 		ci->ci_firmware_id[3] = me2->me_firmware_id[3];
    417  1.18        ad 		ci->ci_hardware_id = me2->me_hardware_id[0];
    418  1.18        ad 		ci->ci_mem_size = le32toh(me2->me_mem_size);
    419  1.18        ad 		ci->ci_max_sg = le16toh(me2->me_max_sg);
    420  1.18        ad 		ci->ci_max_commands = le16toh(me2->me_max_commands);
    421  1.18        ad 		ci->ci_nchan = me2->me_actual_channels;
    422  1.18        ad 
    423  1.18        ad 		free(me2, M_DEVBUF);
    424  1.18        ad 	}
    425  1.18        ad 
    426  1.18        ad 	if (ci->ci_iftype <= 2) {
    427   1.1        ad 		/*
    428   1.1        ad 		 * These controllers may not report the firmware version in
    429  1.18        ad 		 * the ENQUIRY2 response, or may not even support it.
    430   1.1        ad 		 */
    431   1.1        ad 		meo = mlx_enquire(mlx, MLX_CMD_ENQUIRY_OLD,
    432   1.1        ad 		    sizeof(struct mlx_enquiry_old), NULL, 0);
    433   1.1        ad 		if (meo == NULL) {
    434   1.1        ad 			printf("%s: ENQUIRY_OLD failed\n", mlx->mlx_dv.dv_xname);
    435   1.1        ad 			return;
    436   1.1        ad 		}
    437  1.18        ad 		ci->ci_firmware_id[0] = meo->me_fwmajor;
    438  1.18        ad 		ci->ci_firmware_id[1] = meo->me_fwminor;
    439  1.18        ad 		ci->ci_firmware_id[2] = 0;
    440  1.18        ad 		ci->ci_firmware_id[3] = '0';
    441  1.18        ad 
    442  1.18        ad 		if (ci->ci_iftype == 1) {
    443  1.18        ad 			ci->ci_hardware_id = 0;	/* XXX */
    444  1.18        ad 			ci->ci_mem_size = 0;	/* XXX */
    445  1.18        ad 			ci->ci_max_sg = 17;	/* XXX */
    446  1.18        ad 			ci->ci_max_commands = meo->me_max_commands;
    447  1.18        ad 		}
    448  1.18        ad 
    449   1.1        ad 		free(meo, M_DEVBUF);
    450   1.1        ad 	}
    451   1.1        ad 
    452   1.1        ad 	wantfwstr = NULL;
    453  1.18        ad 	fwminor = ci->ci_firmware_id[1];
    454   1.1        ad 
    455  1.18        ad 	switch (ci->ci_firmware_id[0]) {
    456   1.1        ad 	case 2:
    457  1.18        ad 		if (ci->ci_iftype == 1) {
    458   1.1        ad 			if (fwminor < 14)
    459   1.1        ad 				wantfwstr = "2.14";
    460   1.1        ad 		} else if (fwminor < 42)
    461   1.1        ad 			wantfwstr = "2.42";
    462   1.1        ad 		break;
    463   1.1        ad 
    464   1.1        ad 	case 3:
    465   1.1        ad 		if (fwminor < 51)
    466   1.1        ad 			wantfwstr = "3.51";
    467   1.1        ad 		break;
    468   1.1        ad 
    469   1.1        ad 	case 4:
    470   1.1        ad 		if (fwminor < 6)
    471   1.1        ad 			wantfwstr = "4.06";
    472   1.1        ad 		break;
    473   1.1        ad 
    474   1.1        ad 	case 5:
    475   1.1        ad 		if (fwminor < 7)
    476   1.1        ad 			wantfwstr = "5.07";
    477   1.1        ad 		break;
    478   1.1        ad 	}
    479   1.1        ad 
    480   1.1        ad 	/* Print a little information about the controller. */
    481   1.1        ad 	mlx_describe(mlx);
    482   1.1        ad 
    483   1.1        ad 	if (wantfwstr != NULL) {
    484   1.1        ad 		printf("%s: WARNING: this f/w revision is not recommended\n",
    485   1.1        ad 		    mlx->mlx_dv.dv_xname);
    486   1.1        ad 		printf("%s: WARNING: use revision %s or later\n",
    487   1.1        ad 		    mlx->mlx_dv.dv_xname, wantfwstr);
    488   1.1        ad 	}
    489   1.1        ad 
    490   1.1        ad 	/* We don't (yet) know where the event log is up to. */
    491   1.1        ad 	mlx->mlx_currevent = -1;
    492   1.1        ad 
    493   1.1        ad 	/* No user-requested background operation is in progress. */
    494   1.1        ad 	mlx->mlx_bg = 0;
    495   1.1        ad 	mlx->mlx_rebuildstat.rs_code = MLX_REBUILDSTAT_IDLE;
    496   1.1        ad 
    497   1.1        ad 	/* Set maximum number of queued commands for `regular' operations. */
    498   1.1        ad 	mlx->mlx_max_queuecnt =
    499  1.18        ad 	    min(ci->ci_max_commands, MLX_MAX_QUEUECNT) -
    500  1.21        ad 	    MLX_NCCBS_CONTROL;
    501   1.1        ad #ifdef DIAGNOSTIC
    502  1.21        ad 	if (mlx->mlx_max_queuecnt < MLX_NCCBS_CONTROL + MLX_MAX_DRIVES)
    503   1.1        ad 		printf("%s: WARNING: few CCBs available\n",
    504   1.1        ad 		    mlx->mlx_dv.dv_xname);
    505  1.18        ad 	if (ci->ci_max_sg < MLX_MAX_SEGS) {
    506   1.1        ad 		printf("%s: oops, not enough S/G segments\n",
    507   1.1        ad 		    mlx->mlx_dv.dv_xname);
    508   1.1        ad 		return;
    509   1.1        ad 	}
    510   1.1        ad #endif
    511   1.1        ad 
    512   1.1        ad 	if (mlx_sdh == NULL) {
    513   1.1        ad 		/*
    514   1.1        ad 		 * Set our `shutdownhook' before we start any device
    515   1.1        ad 		 * activity.
    516   1.1        ad 		 */
    517   1.1        ad 		mlx_sdh = shutdownhook_establish(mlx_shutdown, NULL);
    518   1.1        ad 
    519   1.1        ad 		/* Arrange to create a status monitoring thread. */
    520   1.1        ad 		kthread_create(mlx_periodic_create, NULL);
    521   1.1        ad 	}
    522   1.1        ad 
    523   1.1        ad 	/* Finally, attach child devices and enable interrupts. */
    524   1.1        ad 	mlx_configure(mlx, 0);
    525   1.1        ad 	(*mlx->mlx_intaction)(mlx, 1);
    526   1.1        ad 
    527   1.1        ad 	mlx->mlx_flags |= MLXF_INITOK;
    528   1.1        ad }
    529   1.1        ad 
    530   1.1        ad /*
    531   1.1        ad  * Tell the world about the controller.
    532   1.1        ad  */
    533   1.1        ad static void
    534   1.1        ad mlx_describe(struct mlx_softc *mlx)
    535   1.1        ad {
    536  1.18        ad 	struct mlx_cinfo *ci;
    537  1.33  christos 	static char tbuf[80];
    538   1.1        ad 	const char *model;
    539   1.1        ad 	int i;
    540   1.1        ad 
    541   1.1        ad 	model = NULL;
    542  1.18        ad 	ci = &mlx->mlx_ci;
    543   1.1        ad 
    544   1.1        ad 	for (i = 0; i < sizeof(mlx_cname) / sizeof(mlx_cname[0]); i++)
    545  1.18        ad 		if (ci->ci_hardware_id == mlx_cname[i].hwid) {
    546   1.1        ad 			model = mlx_cname[i].name;
    547   1.1        ad 			break;
    548   1.1        ad 		}
    549   1.1        ad 
    550   1.1        ad 	if (model == NULL) {
    551  1.33  christos 		snprintf(tbuf, sizeof(tbuf), " model 0x%x", ci->ci_hardware_id);
    552  1.33  christos 		model = tbuf;
    553   1.1        ad 	}
    554   1.1        ad 
    555  1.18        ad 	printf("%s: DAC%s, %d channel%s, firmware %d.%02d-%c-%02d",
    556  1.18        ad 	    mlx->mlx_dv.dv_xname, model, ci->ci_nchan,
    557  1.18        ad 	    ci->ci_nchan > 1 ? "s" : "",
    558  1.18        ad 	    ci->ci_firmware_id[0], ci->ci_firmware_id[1],
    559  1.18        ad 	    ci->ci_firmware_id[3], ci->ci_firmware_id[2]);
    560  1.18        ad 	if (ci->ci_mem_size != 0)
    561  1.18        ad 		printf(", %dMB RAM", ci->ci_mem_size >> 20);
    562  1.18        ad 	printf("\n");
    563   1.1        ad }
    564   1.1        ad 
    565   1.1        ad /*
    566   1.1        ad  * Locate disk resources and attach children to them.
    567   1.1        ad  */
    568   1.1        ad static void
    569   1.1        ad mlx_configure(struct mlx_softc *mlx, int waitok)
    570   1.1        ad {
    571   1.6        ad 	struct mlx_enquiry *me;
    572  1.10        ad 	struct mlx_enquiry_old *meo;
    573   1.1        ad 	struct mlx_enq_sys_drive *mes;
    574   1.1        ad 	struct mlx_sysdrive *ms;
    575   1.1        ad 	struct mlx_attach_args mlxa;
    576   1.1        ad 	int i, nunits;
    577   1.1        ad 	u_int size;
    578  1.35  drochner 	int locs[MLXCF_NLOCS];
    579   1.1        ad 
    580  1.12        ad 	mlx->mlx_flags |= MLXF_RESCANNING;
    581  1.12        ad 
    582  1.18        ad 	if (mlx->mlx_ci.ci_iftype <= 2) {
    583  1.10        ad 		meo = mlx_enquire(mlx, MLX_CMD_ENQUIRY_OLD,
    584  1.10        ad 		    sizeof(struct mlx_enquiry_old), NULL, waitok);
    585  1.10        ad 		if (meo == NULL) {
    586  1.10        ad 			printf("%s: ENQUIRY_OLD failed\n",
    587  1.10        ad 			    mlx->mlx_dv.dv_xname);
    588  1.12        ad 			goto out;
    589  1.10        ad 		}
    590  1.10        ad 		mlx->mlx_numsysdrives = meo->me_num_sys_drvs;
    591  1.10        ad 		free(meo, M_DEVBUF);
    592  1.10        ad 	} else {
    593  1.10        ad 		me = mlx_enquire(mlx, MLX_CMD_ENQUIRY,
    594  1.10        ad 		    sizeof(struct mlx_enquiry), NULL, waitok);
    595  1.10        ad 		if (me == NULL) {
    596  1.10        ad 			printf("%s: ENQUIRY failed\n", mlx->mlx_dv.dv_xname);
    597  1.12        ad 			goto out;
    598  1.10        ad 		}
    599  1.10        ad 		mlx->mlx_numsysdrives = me->me_num_sys_drvs;
    600  1.10        ad 		free(me, M_DEVBUF);
    601   1.6        ad 	}
    602   1.6        ad 
    603   1.1        ad 	mes = mlx_enquire(mlx, MLX_CMD_ENQSYSDRIVE,
    604   1.1        ad 	    sizeof(*mes) * MLX_MAX_DRIVES, NULL, waitok);
    605   1.1        ad 	if (mes == NULL) {
    606   1.1        ad 		printf("%s: error fetching drive status\n",
    607   1.1        ad 		    mlx->mlx_dv.dv_xname);
    608  1.12        ad 		goto out;
    609   1.1        ad 	}
    610   1.1        ad 
    611   1.1        ad 	/* Allow 1 queued command per unit while re-configuring. */
    612   1.1        ad 	mlx_adjqparam(mlx, 1, 0);
    613   1.1        ad 
    614   1.1        ad 	ms = &mlx->mlx_sysdrive[0];
    615   1.6        ad 	nunits = 0;
    616   1.1        ad 	for (i = 0; i < MLX_MAX_DRIVES; i++, ms++) {
    617   1.1        ad 		size = le32toh(mes[i].sd_size);
    618   1.6        ad 		ms->ms_state = mes[i].sd_state;
    619   1.6        ad 
    620   1.1        ad 		/*
    621   1.1        ad 		 * If an existing device has changed in some way (e.g. no
    622   1.1        ad 		 * longer present) then detach it.
    623   1.1        ad 		 */
    624   1.1        ad 		if (ms->ms_dv != NULL && (size != ms->ms_size ||
    625   1.1        ad 		    (mes[i].sd_raidlevel & 0xf) != ms->ms_raidlevel))
    626   1.1        ad 			config_detach(ms->ms_dv, DETACH_FORCE);
    627   1.1        ad 
    628   1.1        ad 		ms->ms_size = size;
    629   1.1        ad 		ms->ms_raidlevel = mes[i].sd_raidlevel & 0xf;
    630   1.1        ad 		ms->ms_state = mes[i].sd_state;
    631   1.1        ad 		ms->ms_dv = NULL;
    632   1.1        ad 
    633  1.10        ad 		if (i >= mlx->mlx_numsysdrives)
    634  1.10        ad 			continue;
    635   1.1        ad 		if (size == 0xffffffffU || size == 0)
    636   1.1        ad 			continue;
    637   1.1        ad 
    638   1.1        ad 		/*
    639   1.1        ad 		 * Attach a new device.
    640   1.1        ad 		 */
    641   1.1        ad 		mlxa.mlxa_unit = i;
    642  1.30  drochner 
    643  1.35  drochner 		locs[MLXCF_UNIT] = i;
    644  1.30  drochner 
    645  1.35  drochner 		ms->ms_dv = config_found_sm_loc(&mlx->mlx_dv, "mlx", locs,
    646  1.36  drochner 				&mlxa, mlx_print, config_stdsubmatch);
    647   1.6        ad 		nunits += (ms->ms_dv != NULL);
    648   1.1        ad 	}
    649   1.1        ad 
    650   1.1        ad 	free(mes, M_DEVBUF);
    651   1.6        ad 
    652   1.6        ad 	if (nunits != 0)
    653   1.6        ad 		mlx_adjqparam(mlx, mlx->mlx_max_queuecnt / nunits,
    654   1.6        ad 		    mlx->mlx_max_queuecnt % nunits);
    655  1.12        ad  out:
    656  1.12        ad  	mlx->mlx_flags &= ~MLXF_RESCANNING;
    657   1.1        ad }
    658   1.1        ad 
    659   1.1        ad /*
    660   1.1        ad  * Print autoconfiguration message for a sub-device.
    661   1.1        ad  */
    662   1.1        ad static int
    663   1.1        ad mlx_print(void *aux, const char *pnp)
    664   1.1        ad {
    665   1.1        ad 	struct mlx_attach_args *mlxa;
    666   1.1        ad 
    667   1.1        ad 	mlxa = (struct mlx_attach_args *)aux;
    668   1.1        ad 
    669   1.1        ad 	if (pnp != NULL)
    670  1.26   thorpej 		aprint_normal("block device at %s", pnp);
    671  1.26   thorpej 	aprint_normal(" unit %d", mlxa->mlxa_unit);
    672   1.1        ad 	return (UNCONF);
    673   1.1        ad }
    674   1.1        ad 
    675   1.1        ad /*
    676   1.1        ad  * Shut down all configured `mlx' devices.
    677   1.1        ad  */
    678   1.1        ad static void
    679   1.1        ad mlx_shutdown(void *cookie)
    680   1.1        ad {
    681   1.1        ad 	struct mlx_softc *mlx;
    682   1.1        ad 	int i;
    683   1.1        ad 
    684   1.1        ad 	for (i = 0; i < mlx_cd.cd_ndevs; i++)
    685   1.1        ad 		if ((mlx = device_lookup(&mlx_cd, i)) != NULL)
    686   1.1        ad 			mlx_flush(mlx, 0);
    687   1.1        ad }
    688   1.1        ad 
    689   1.1        ad /*
    690   1.1        ad  * Adjust queue parameters for all child devices.
    691   1.1        ad  */
    692   1.1        ad static void
    693   1.1        ad mlx_adjqparam(struct mlx_softc *mlx, int mpu, int slop)
    694   1.1        ad {
    695   1.1        ad #if NLD > 0
    696   1.1        ad 	extern struct cfdriver ld_cd;
    697   1.1        ad 	struct ld_softc *ld;
    698   1.1        ad 	int i;
    699   1.1        ad 
    700   1.1        ad 	for (i = 0; i < ld_cd.cd_ndevs; i++) {
    701   1.1        ad 		if ((ld = device_lookup(&ld_cd, i)) == NULL)
    702   1.1        ad 			continue;
    703   1.1        ad 		if (ld->sc_dv.dv_parent != &mlx->mlx_dv)
    704   1.1        ad 			continue;
    705   1.1        ad 		ldadjqparam(ld, mpu + (slop-- > 0));
    706   1.1        ad 	}
    707   1.1        ad #endif
    708   1.1        ad }
    709   1.1        ad 
    710   1.1        ad /*
    711   1.1        ad  * Accept an open operation on the control device.
    712   1.1        ad  */
    713   1.1        ad int
    714  1.28      fvdl mlxopen(dev_t dev, int flag, int mode, struct proc *p)
    715   1.1        ad {
    716   1.1        ad 	struct mlx_softc *mlx;
    717   1.1        ad 
    718   1.1        ad 	if ((mlx = device_lookup(&mlx_cd, minor(dev))) == NULL)
    719   1.1        ad 		return (ENXIO);
    720   1.1        ad 	if ((mlx->mlx_flags & MLXF_INITOK) == 0)
    721   1.1        ad 		return (ENXIO);
    722   1.1        ad 	if ((mlx->mlx_flags & MLXF_OPEN) != 0)
    723   1.1        ad 		return (EBUSY);
    724   1.1        ad 
    725   1.1        ad 	mlx->mlx_flags |= MLXF_OPEN;
    726   1.1        ad 	return (0);
    727   1.1        ad }
    728   1.1        ad 
    729   1.1        ad /*
    730   1.1        ad  * Accept the last close on the control device.
    731   1.1        ad  */
    732   1.1        ad int
    733  1.28      fvdl mlxclose(dev_t dev, int flag, int mode, struct proc *p)
    734   1.1        ad {
    735   1.1        ad 	struct mlx_softc *mlx;
    736   1.1        ad 
    737   1.1        ad 	mlx = device_lookup(&mlx_cd, minor(dev));
    738   1.1        ad 	mlx->mlx_flags &= ~MLXF_OPEN;
    739   1.1        ad 	return (0);
    740   1.1        ad }
    741   1.1        ad 
    742   1.1        ad /*
    743   1.1        ad  * Handle control operations.
    744   1.1        ad  */
    745   1.1        ad int
    746  1.28      fvdl mlxioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
    747   1.1        ad {
    748   1.1        ad 	struct mlx_softc *mlx;
    749   1.1        ad 	struct mlx_rebuild_request *rb;
    750   1.1        ad 	struct mlx_rebuild_status *rs;
    751   1.1        ad 	struct mlx_pause *mp;
    752   1.1        ad 	struct mlx_sysdrive *ms;
    753   1.1        ad 	int i, rv, *arg, result;
    754   1.1        ad 
    755  1.12        ad 	if (securelevel >= 2)
    756  1.12        ad 		return (EPERM);
    757  1.12        ad 
    758   1.1        ad 	mlx = device_lookup(&mlx_cd, minor(dev));
    759   1.1        ad 
    760   1.1        ad 	rb = (struct mlx_rebuild_request *)data;
    761   1.1        ad 	rs = (struct mlx_rebuild_status *)data;
    762   1.1        ad 	arg = (int *)data;
    763   1.1        ad 	rv = 0;
    764   1.1        ad 
    765   1.1        ad 	switch (cmd) {
    766   1.1        ad 	case MLX_RESCAN_DRIVES:
    767   1.1        ad 		/*
    768   1.1        ad 		 * Scan the controller to see whether new drives have
    769   1.1        ad 		 * appeared, or old ones disappeared.
    770   1.1        ad 		 */
    771   1.1        ad 		mlx_configure(mlx, 1);
    772   1.1        ad 		return (0);
    773   1.1        ad 
    774   1.1        ad 	case MLX_PAUSE_CHANNEL:
    775   1.1        ad 		/*
    776   1.1        ad 		 * Pause one or more SCSI channels for a period of time, to
    777   1.1        ad 		 * assist in the process of hot-swapping devices.
    778   1.1        ad 		 *
    779   1.1        ad 		 * Note that at least the 3.51 firmware on the DAC960PL
    780   1.1        ad 		 * doesn't seem to do this right.
    781   1.1        ad 		 */
    782   1.1        ad 		if ((mlx->mlx_flags & MLXF_PAUSEWORKS) == 0)
    783   1.1        ad 			return (EOPNOTSUPP);
    784   1.1        ad 
    785   1.1        ad 		mp = (struct mlx_pause *)data;
    786   1.1        ad 
    787   1.1        ad 		if ((mp->mp_which == MLX_PAUSE_CANCEL) &&
    788   1.1        ad 		    (mlx->mlx_pause.mp_when != 0)) {
    789   1.1        ad 			/* Cancel a pending pause operation. */
    790   1.1        ad 			mlx->mlx_pause.mp_which = 0;
    791   1.1        ad 			break;
    792   1.1        ad 		}
    793   1.1        ad 
    794   1.1        ad 		/* Fix for legal channels. */
    795  1.18        ad 		mp->mp_which &= ((1 << mlx->mlx_ci.ci_nchan) -1);
    796   1.1        ad 
    797   1.1        ad 		/* Check time values. */
    798   1.1        ad 		if (mp->mp_when < 0 || mp->mp_when > 3600 ||
    799   1.1        ad 		    mp->mp_howlong < 1 || mp->mp_howlong > (0xf * 30)) {
    800   1.1        ad 			rv = EINVAL;
    801   1.1        ad 			break;
    802   1.1        ad 		}
    803   1.1        ad 
    804   1.1        ad 		/* Check for a pause currently running. */
    805   1.1        ad 		if ((mlx->mlx_pause.mp_which != 0) &&
    806   1.1        ad 		    (mlx->mlx_pause.mp_when == 0)) {
    807   1.1        ad 			rv = EBUSY;
    808   1.1        ad 			break;
    809   1.1        ad 		}
    810   1.1        ad 
    811   1.1        ad 		/* Looks ok, go with it. */
    812   1.1        ad 		mlx->mlx_pause.mp_which = mp->mp_which;
    813   1.1        ad 		mlx->mlx_pause.mp_when = mlx_curtime() + mp->mp_when;
    814   1.1        ad 		mlx->mlx_pause.mp_howlong =
    815   1.1        ad 		    mlx->mlx_pause.mp_when + mp->mp_howlong;
    816   1.1        ad 
    817   1.1        ad 		return (0);
    818   1.1        ad 
    819   1.1        ad 	case MLX_COMMAND:
    820   1.1        ad 		/*
    821   1.1        ad 		 * Accept a command passthrough-style.
    822   1.1        ad 		 */
    823   1.1        ad 		return (mlx_user_command(mlx, (struct mlx_usercommand *)data));
    824   1.1        ad 
    825   1.1        ad 	case MLX_REBUILDASYNC:
    826   1.1        ad 		/*
    827   1.1        ad 		 * Start a rebuild on a given SCSI disk
    828   1.1        ad 		 */
    829   1.1        ad 		if (mlx->mlx_bg != 0) {
    830   1.1        ad 			rb->rr_status = 0x0106;
    831   1.1        ad 			rv = EBUSY;
    832   1.1        ad 			break;
    833   1.1        ad 		}
    834   1.1        ad 
    835   1.1        ad 		rb->rr_status = mlx_rebuild(mlx, rb->rr_channel, rb->rr_target);
    836   1.1        ad 		switch (rb->rr_status) {
    837   1.1        ad 		case 0:
    838   1.1        ad 			rv = 0;
    839   1.1        ad 			break;
    840   1.1        ad 		case 0x10000:
    841   1.1        ad 			rv = ENOMEM;	/* Couldn't set up the command. */
    842   1.1        ad 			break;
    843  1.32     perry 		case 0x0002:
    844   1.1        ad 			rv = EBUSY;
    845   1.1        ad 			break;
    846   1.1        ad 		case 0x0104:
    847   1.1        ad 			rv = EIO;
    848   1.1        ad 			break;
    849   1.1        ad 		case 0x0105:
    850   1.1        ad 			rv = ERANGE;
    851   1.1        ad 			break;
    852   1.1        ad 		case 0x0106:
    853   1.1        ad 			rv = EBUSY;
    854   1.1        ad 			break;
    855   1.1        ad 		default:
    856   1.1        ad 			rv = EINVAL;
    857   1.1        ad 			break;
    858   1.1        ad 		}
    859   1.1        ad 
    860   1.1        ad 		if (rv == 0)
    861   1.1        ad 			mlx->mlx_bg = MLX_BG_REBUILD;
    862   1.1        ad 
    863   1.1        ad 		return (0);
    864   1.1        ad 
    865   1.1        ad 	case MLX_REBUILDSTAT:
    866   1.1        ad 		/*
    867   1.1        ad 		 * Get the status of the current rebuild or consistency check.
    868   1.1        ad 		 */
    869   1.1        ad 		*rs = mlx->mlx_rebuildstat;
    870   1.1        ad 		return (0);
    871   1.1        ad 
    872   1.1        ad 	case MLX_GET_SYSDRIVE:
    873   1.1        ad 		/*
    874   1.1        ad 		 * Return the system drive number matching the `ld' device
    875   1.1        ad 		 * unit in (arg), if it happens to belong to us.
    876   1.1        ad 		 */
    877   1.1        ad 		for (i = 0; i < MLX_MAX_DRIVES; i++) {
    878   1.1        ad 			ms = &mlx->mlx_sysdrive[i];
    879   1.1        ad 			if (ms->ms_dv != NULL)
    880  1.16        ad 				if (ms->ms_dv->dv_xname[2] == '0' + *arg) {
    881  1.16        ad 					*arg = i;
    882  1.16        ad 					return (0);
    883  1.16        ad 				}
    884   1.1        ad 		}
    885   1.1        ad 		return (ENOENT);
    886  1.19        ad 
    887  1.19        ad 	case MLX_GET_CINFO:
    888  1.19        ad 		/*
    889  1.19        ad 		 * Return controller info.
    890  1.19        ad 		 */
    891  1.19        ad 		memcpy(arg, &mlx->mlx_ci, sizeof(mlx->mlx_ci));
    892  1.19        ad 		return (0);
    893   1.1        ad 	}
    894   1.1        ad 
    895   1.1        ad 	switch (cmd) {
    896   1.1        ad 	case MLXD_DETACH:
    897   1.1        ad 	case MLXD_STATUS:
    898   1.1        ad 	case MLXD_CHECKASYNC:
    899   1.1        ad 		if ((u_int)*arg >= MLX_MAX_DRIVES)
    900   1.1        ad 			return (EINVAL);
    901   1.1        ad 		ms = &mlx->mlx_sysdrive[*arg];
    902   1.1        ad 		if (*arg > MLX_MAX_DRIVES || ms->ms_dv == NULL)
    903   1.1        ad 			return (ENOENT);
    904   1.1        ad 		break;
    905   1.1        ad 
    906   1.1        ad 	default:
    907   1.1        ad 		return (ENOTTY);
    908   1.1        ad 	}
    909   1.1        ad 
    910   1.1        ad 	switch (cmd) {
    911   1.1        ad 	case MLXD_DETACH:
    912   1.1        ad 		/*
    913  1.32     perry 		 * Disconnect from the specified drive; it may be about to go
    914   1.1        ad 		 * away.
    915   1.1        ad 		 */
    916   1.1        ad 		return (config_detach(ms->ms_dv, 0));
    917   1.1        ad 
    918   1.1        ad 	case MLXD_STATUS:
    919   1.1        ad 		/*
    920   1.1        ad 		 * Return the current status of this drive.
    921   1.1        ad 		 */
    922   1.1        ad 		*arg = ms->ms_state;
    923   1.1        ad 		return (0);
    924   1.1        ad 
    925   1.1        ad 	case MLXD_CHECKASYNC:
    926   1.1        ad 		/*
    927   1.1        ad 		 * Start a background consistency check on this drive.
    928   1.1        ad 		 */
    929   1.1        ad 		if (mlx->mlx_bg != 0) {
    930   1.1        ad 			*arg = 0x0106;
    931   1.1        ad 			return (EBUSY);
    932   1.1        ad 		}
    933   1.1        ad 
    934   1.1        ad 		switch (result = mlx_check(mlx, *arg)) {
    935   1.1        ad 		case 0:
    936   1.1        ad 			rv = 0;
    937   1.1        ad 			break;
    938   1.1        ad 		case 0x10000:
    939   1.1        ad 			rv = ENOMEM;	/* Couldn't set up the command. */
    940   1.1        ad 			break;
    941  1.32     perry 		case 0x0002:
    942   1.1        ad 			rv = EIO;
    943   1.1        ad 			break;
    944   1.1        ad 		case 0x0105:
    945   1.1        ad 			rv = ERANGE;
    946   1.1        ad 			break;
    947   1.1        ad 		case 0x0106:
    948   1.1        ad 			rv = EBUSY;
    949   1.1        ad 			break;
    950   1.1        ad 		default:
    951   1.1        ad 			rv = EINVAL;
    952   1.1        ad 			break;
    953   1.1        ad 		}
    954   1.1        ad 
    955   1.1        ad 		if (rv == 0)
    956   1.1        ad 			mlx->mlx_bg = MLX_BG_CHECK;
    957   1.1        ad 		*arg = result;
    958   1.1        ad 		return (rv);
    959   1.1        ad 	}
    960   1.1        ad 
    961   1.1        ad 	return (ENOTTY);	/* XXX shut up gcc */
    962   1.1        ad }
    963   1.1        ad 
    964   1.1        ad /*
    965   1.1        ad  * Fire off commands to periodically check the status of connected drives.
    966   1.1        ad  * Check for commands that have timed out.
    967   1.1        ad  */
    968   1.1        ad static void
    969   1.1        ad mlx_periodic_create(void *cookie)
    970   1.1        ad {
    971   1.1        ad 	int rv;
    972   1.1        ad 
    973   1.1        ad 	rv = kthread_create1(mlx_periodic_thread, NULL, &mlx_periodic_proc,
    974  1.21        ad 	    "mlxtask");
    975   1.1        ad 	if (rv == 0)
    976   1.1        ad 		return;
    977   1.1        ad 
    978   1.1        ad 	printf("mlx_periodic_create: unable to create thread (%d)\n", rv);
    979   1.1        ad }
    980   1.1        ad 
    981   1.1        ad static void
    982   1.1        ad mlx_periodic_thread(void *cookie)
    983   1.1        ad {
    984   1.1        ad 	struct mlx_softc *mlx;
    985   1.1        ad 	int i;
    986   1.1        ad 
    987   1.1        ad 	for (;;) {
    988   1.1        ad 		for (i = 0; i < mlx_cd.cd_ndevs; i++)
    989   1.1        ad 			if ((mlx = device_lookup(&mlx_cd, i)) != NULL)
    990  1.18        ad 				if (mlx->mlx_ci.ci_iftype > 1)
    991  1.18        ad 					mlx_periodic(mlx);
    992   1.1        ad 
    993  1.21        ad 		tsleep(mlx_periodic_thread, PWAIT, "mlxzzz", hz * 2);
    994   1.1        ad 	}
    995   1.1        ad }
    996   1.1        ad 
    997   1.1        ad static void
    998   1.1        ad mlx_periodic(struct mlx_softc *mlx)
    999   1.1        ad {
   1000   1.1        ad 	struct mlx_ccb *mc, *nmc;
   1001   1.1        ad 	int etype, s;
   1002   1.1        ad 	time_t ct;
   1003   1.1        ad 
   1004   1.1        ad 	ct = mlx_curtime();
   1005   1.1        ad 
   1006   1.1        ad 	if ((mlx->mlx_pause.mp_which != 0) &&
   1007   1.1        ad 	    (mlx->mlx_pause.mp_when > 0) &&
   1008   1.1        ad 	    (ct >= mlx->mlx_pause.mp_when)) {
   1009   1.1        ad 	    	/*
   1010   1.1        ad 	    	 * Start bus pause.
   1011   1.1        ad 	    	 */
   1012   1.1        ad 		mlx_pause_action(mlx);
   1013   1.1        ad 		mlx->mlx_pause.mp_when = 0;
   1014   1.1        ad 	} else if ((mlx->mlx_pause.mp_which != 0) &&
   1015   1.1        ad 		   (mlx->mlx_pause.mp_when == 0)) {
   1016   1.1        ad 		/*
   1017   1.1        ad 		 * Stop pause if required.
   1018   1.1        ad 		 */
   1019   1.1        ad 		if (ct >= mlx->mlx_pause.mp_howlong) {
   1020   1.1        ad 			mlx_pause_action(mlx);
   1021   1.1        ad 			mlx->mlx_pause.mp_which = 0;
   1022   1.1        ad 		}
   1023   1.1        ad 	} else if (ct > (mlx->mlx_lastpoll + 10)) {
   1024  1.32     perry 		/*
   1025   1.1        ad 		 * Run normal periodic activities...
   1026   1.1        ad 		 */
   1027   1.1        ad 		mlx->mlx_lastpoll = ct;
   1028   1.1        ad 
   1029  1.32     perry 		/*
   1030   1.1        ad 		 * Check controller status.
   1031   1.1        ad 		 */
   1032   1.1        ad 		if ((mlx->mlx_flags & MLXF_PERIODIC_CTLR) == 0) {
   1033   1.1        ad 			mlx->mlx_flags |= MLXF_PERIODIC_CTLR;
   1034   1.1        ad 
   1035  1.18        ad 			if (mlx->mlx_ci.ci_iftype <= 2)
   1036   1.1        ad 				etype = MLX_CMD_ENQUIRY_OLD;
   1037   1.1        ad 			else
   1038   1.1        ad 				etype =  MLX_CMD_ENQUIRY;
   1039   1.1        ad 
   1040   1.1        ad 			mlx_enquire(mlx, etype, max(sizeof(struct mlx_enquiry),
   1041   1.1        ad 			    sizeof(struct mlx_enquiry_old)),
   1042   1.1        ad 			    mlx_periodic_enquiry, 1);
   1043   1.1        ad 		}
   1044   1.1        ad 
   1045   1.1        ad 		/*
   1046   1.1        ad 		 * Check system drive status.
   1047   1.1        ad 		 */
   1048   1.1        ad 		if ((mlx->mlx_flags & MLXF_PERIODIC_DRIVE) == 0) {
   1049   1.1        ad 			mlx->mlx_flags |= MLXF_PERIODIC_DRIVE;
   1050   1.1        ad 			mlx_enquire(mlx, MLX_CMD_ENQSYSDRIVE,
   1051   1.1        ad 			    sizeof(struct mlx_enq_sys_drive) * MLX_MAX_DRIVES,
   1052   1.1        ad 			    mlx_periodic_enquiry, 1);
   1053   1.1        ad 		}
   1054   1.1        ad 	}
   1055   1.1        ad 
   1056   1.1        ad 	/*
   1057   1.1        ad 	 * Get drive rebuild/check status.
   1058   1.1        ad 	 */
   1059   1.1        ad 	if ((mlx->mlx_flags & MLXF_PERIODIC_REBUILD) == 0) {
   1060   1.1        ad 		mlx->mlx_flags |= MLXF_PERIODIC_REBUILD;
   1061   1.1        ad 		mlx_enquire(mlx, MLX_CMD_REBUILDSTAT,
   1062   1.1        ad 		    sizeof(struct mlx_rebuild_stat), mlx_periodic_rebuild, 1);
   1063   1.1        ad 	}
   1064   1.1        ad 
   1065   1.1        ad 	/*
   1066   1.1        ad 	 * Time-out busy CCBs.
   1067   1.1        ad 	 */
   1068   1.1        ad 	s = splbio();
   1069   1.1        ad 	for (mc = TAILQ_FIRST(&mlx->mlx_ccb_worklist); mc != NULL; mc = nmc) {
   1070   1.1        ad 		nmc = TAILQ_NEXT(mc, mc_chain.tailq);
   1071   1.1        ad 		if (mc->mc_expiry > ct) {
   1072   1.1        ad 			/*
   1073   1.1        ad 			 * The remaining CCBs will expire after this one, so
   1074   1.1        ad 			 * there's no point in going further.
   1075   1.1        ad 			 */
   1076   1.1        ad 			break;
   1077   1.1        ad 		}
   1078  1.32     perry 		TAILQ_REMOVE(&mlx->mlx_ccb_worklist, mc, mc_chain.tailq);
   1079   1.1        ad 		mc->mc_status = MLX_STATUS_LOST;
   1080   1.1        ad 		if (mc->mc_mx.mx_handler != NULL)
   1081   1.1        ad 			(*mc->mc_mx.mx_handler)(mc);
   1082   1.1        ad 		else if ((mc->mc_flags & MC_WAITING) != 0)
   1083   1.1        ad 			wakeup(mc);
   1084   1.1        ad 	}
   1085   1.1        ad 	splx(s);
   1086   1.1        ad }
   1087   1.1        ad 
   1088   1.1        ad /*
   1089   1.1        ad  * Handle the result of an ENQUIRY command instigated by periodic status
   1090   1.1        ad  * polling.
   1091   1.1        ad  */
   1092   1.1        ad static void
   1093   1.1        ad mlx_periodic_enquiry(struct mlx_ccb *mc)
   1094   1.1        ad {
   1095   1.1        ad 	struct mlx_softc *mlx;
   1096   1.1        ad 	struct mlx_enquiry *me;
   1097   1.1        ad 	struct mlx_enquiry_old *meo;
   1098   1.1        ad 	struct mlx_enq_sys_drive *mes;
   1099   1.1        ad 	struct mlx_sysdrive *dr;
   1100   1.1        ad 	const char *statestr;
   1101   1.1        ad 	int i, j;
   1102   1.1        ad 	u_int lsn;
   1103   1.1        ad 
   1104   1.1        ad 	mlx = (struct mlx_softc *)mc->mc_mx.mx_dv;
   1105  1.34    mhitch 	mlx_ccb_unmap(mlx, mc);
   1106   1.1        ad 
   1107   1.1        ad 	/*
   1108   1.1        ad 	 * Command completed OK?
   1109   1.1        ad 	 */
   1110   1.1        ad 	if (mc->mc_status != 0) {
   1111   1.1        ad 		printf("%s: periodic enquiry failed - %s\n",
   1112   1.1        ad 		    mlx->mlx_dv.dv_xname, mlx_ccb_diagnose(mc));
   1113   1.1        ad 		goto out;
   1114   1.1        ad 	}
   1115   1.1        ad 
   1116   1.1        ad 	/*
   1117   1.1        ad 	 * Respond to command.
   1118   1.1        ad 	 */
   1119   1.1        ad 	switch (mc->mc_mbox[0]) {
   1120   1.1        ad 	case MLX_CMD_ENQUIRY_OLD:
   1121   1.1        ad 		/*
   1122   1.1        ad 		 * This is currently a bit fruitless, as we don't know how
   1123   1.1        ad 		 * to extract the eventlog pointer yet.
   1124   1.1        ad 		 */
   1125   1.1        ad 		me = (struct mlx_enquiry *)mc->mc_mx.mx_context;
   1126   1.1        ad 		meo = (struct mlx_enquiry_old *)mc->mc_mx.mx_context;
   1127   1.1        ad 
   1128   1.1        ad 		/* Convert data in-place to new format */
   1129   1.1        ad 		i = sizeof(me->me_dead) / sizeof(me->me_dead[0]);
   1130   1.1        ad 		while (--i >= 0) {
   1131   1.1        ad 			me->me_dead[i].dd_chan = meo->me_dead[i].dd_chan;
   1132   1.1        ad 			me->me_dead[i].dd_targ = meo->me_dead[i].dd_targ;
   1133   1.1        ad 		}
   1134   1.1        ad 
   1135   1.1        ad 		me->me_misc_flags = 0;
   1136   1.1        ad 		me->me_rebuild_count = meo->me_rebuild_count;
   1137   1.1        ad 		me->me_dead_count = meo->me_dead_count;
   1138   1.1        ad 		me->me_critical_sd_count = meo->me_critical_sd_count;
   1139   1.1        ad 		me->me_event_log_seq_num = 0;
   1140   1.1        ad 		me->me_offline_sd_count = meo->me_offline_sd_count;
   1141   1.1        ad 		me->me_max_commands = meo->me_max_commands;
   1142   1.1        ad 		me->me_rebuild_flag = meo->me_rebuild_flag;
   1143   1.1        ad 		me->me_fwmajor = meo->me_fwmajor;
   1144   1.1        ad 		me->me_fwminor = meo->me_fwminor;
   1145   1.1        ad 		me->me_status_flags = meo->me_status_flags;
   1146   1.1        ad 		me->me_flash_age = meo->me_flash_age;
   1147   1.1        ad 
   1148   1.1        ad 		i = sizeof(me->me_drvsize) / sizeof(me->me_drvsize[0]);
   1149   1.1        ad 		j = sizeof(meo->me_drvsize) / sizeof(meo->me_drvsize[0]);
   1150   1.1        ad 
   1151   1.1        ad 		while (--i >= 0) {
   1152   1.1        ad 			if (i >= j)
   1153   1.1        ad 				me->me_drvsize[i] = 0;
   1154   1.1        ad 			else
   1155   1.1        ad 				me->me_drvsize[i] = meo->me_drvsize[i];
   1156   1.1        ad 		}
   1157   1.1        ad 
   1158   1.1        ad 		me->me_num_sys_drvs = meo->me_num_sys_drvs;
   1159   1.1        ad 
   1160   1.1        ad 		/* FALLTHROUGH */
   1161   1.1        ad 
   1162   1.1        ad 	case MLX_CMD_ENQUIRY:
   1163   1.1        ad 		/*
   1164   1.1        ad 		 * Generic controller status update.  We could do more with
   1165   1.1        ad 		 * this than just checking the event log.
   1166   1.1        ad 		 */
   1167   1.1        ad 		me = (struct mlx_enquiry *)mc->mc_mx.mx_context;
   1168   1.1        ad 		lsn = le16toh(me->me_event_log_seq_num);
   1169   1.1        ad 
   1170   1.1        ad 		if (mlx->mlx_currevent == -1) {
   1171   1.1        ad 			/* Initialise our view of the event log. */
   1172   1.1        ad 			mlx->mlx_currevent = lsn;
   1173   1.1        ad 			mlx->mlx_lastevent = lsn;
   1174   1.1        ad 		} else if (lsn != mlx->mlx_lastevent &&
   1175   1.1        ad 			   (mlx->mlx_flags & MLXF_EVENTLOG_BUSY) == 0) {
   1176   1.1        ad 			/* Record where current events are up to */
   1177   1.1        ad 			mlx->mlx_currevent = lsn;
   1178   1.1        ad 
   1179   1.1        ad 			/* Mark the event log as busy. */
   1180   1.1        ad 			mlx->mlx_flags |= MLXF_EVENTLOG_BUSY;
   1181   1.1        ad 
   1182   1.1        ad 			/* Drain new eventlog entries. */
   1183   1.1        ad 			mlx_periodic_eventlog_poll(mlx);
   1184   1.1        ad 		}
   1185   1.1        ad 		break;
   1186   1.1        ad 
   1187   1.1        ad 	case MLX_CMD_ENQSYSDRIVE:
   1188  1.12        ad 		/*
   1189  1.32     perry 		 * Perform drive status comparison to see if something
   1190  1.12        ad 		 * has failed.  Don't perform the comparison if we're
   1191  1.12        ad 		 * reconfiguring, since the system drive table will be
   1192  1.12        ad 		 * changing.
   1193  1.12        ad 		 */
   1194  1.12        ad 		if ((mlx->mlx_flags & MLXF_RESCANNING) != 0)
   1195  1.12        ad 			break;
   1196  1.12        ad 
   1197   1.1        ad 		mes = (struct mlx_enq_sys_drive *)mc->mc_mx.mx_context;
   1198   1.1        ad 		dr = &mlx->mlx_sysdrive[0];
   1199   1.1        ad 
   1200  1.10        ad 		for (i = 0; i < mlx->mlx_numsysdrives; i++) {
   1201   1.1        ad 			/* Has state been changed by controller? */
   1202   1.1        ad 			if (dr->ms_state != mes[i].sd_state) {
   1203   1.1        ad 				switch (mes[i].sd_state) {
   1204   1.1        ad 				case MLX_SYSD_OFFLINE:
   1205   1.1        ad 					statestr = "offline";
   1206   1.1        ad 					break;
   1207   1.1        ad 
   1208   1.1        ad 				case MLX_SYSD_ONLINE:
   1209   1.1        ad 					statestr = "online";
   1210   1.1        ad 					break;
   1211   1.1        ad 
   1212   1.1        ad 				case MLX_SYSD_CRITICAL:
   1213   1.1        ad 					statestr = "critical";
   1214  1.10        ad 					break;
   1215  1.32     perry 
   1216  1.10        ad 				default:
   1217  1.10        ad 					statestr = "unknown";
   1218   1.1        ad 					break;
   1219   1.1        ad 				}
   1220   1.1        ad 
   1221   1.1        ad 				printf("%s: unit %d %s\n", mlx->mlx_dv.dv_xname,
   1222   1.1        ad 				    i, statestr);
   1223   1.1        ad 
   1224   1.1        ad 				/* Save new state. */
   1225   1.1        ad 				dr->ms_state = mes[i].sd_state;
   1226   1.1        ad 			}
   1227   1.1        ad 		}
   1228   1.1        ad 		break;
   1229   1.1        ad 
   1230   1.1        ad #ifdef DIAGNOSTIC
   1231   1.1        ad 	default:
   1232   1.1        ad 		printf("%s: mlx_periodic_enquiry: eh?\n",
   1233   1.1        ad 		    mlx->mlx_dv.dv_xname);
   1234   1.1        ad 		break;
   1235   1.1        ad #endif
   1236   1.1        ad 	}
   1237   1.1        ad 
   1238   1.3        ad  out:
   1239   1.1        ad 	if (mc->mc_mbox[0] == MLX_CMD_ENQSYSDRIVE)
   1240   1.1        ad 		mlx->mlx_flags &= ~MLXF_PERIODIC_DRIVE;
   1241   1.1        ad 	else
   1242   1.1        ad 		mlx->mlx_flags &= ~MLXF_PERIODIC_CTLR;
   1243   1.1        ad 
   1244   1.1        ad 	free(mc->mc_mx.mx_context, M_DEVBUF);
   1245   1.1        ad 	mlx_ccb_free(mlx, mc);
   1246   1.1        ad }
   1247   1.1        ad 
   1248   1.1        ad /*
   1249   1.1        ad  * Instigate a poll for one event log message on (mlx).  We only poll for
   1250   1.1        ad  * one message at a time, to keep our command usage down.
   1251   1.1        ad  */
   1252   1.1        ad static void
   1253   1.1        ad mlx_periodic_eventlog_poll(struct mlx_softc *mlx)
   1254   1.1        ad {
   1255   1.1        ad 	struct mlx_ccb *mc;
   1256   1.1        ad 	void *result;
   1257   1.1        ad 	int rv;
   1258   1.1        ad 
   1259   1.1        ad 	result = NULL;
   1260   1.1        ad 
   1261   1.1        ad 	if ((rv = mlx_ccb_alloc(mlx, &mc, 1)) != 0)
   1262   1.1        ad 		goto out;
   1263   1.1        ad 
   1264   1.1        ad 	if ((result = malloc(1024, M_DEVBUF, M_WAITOK)) == NULL) {
   1265   1.1        ad 		rv = ENOMEM;
   1266   1.1        ad 		goto out;
   1267   1.1        ad 	}
   1268   1.1        ad 	if ((rv = mlx_ccb_map(mlx, mc, result, 1024, MC_XFER_IN)) != 0)
   1269   1.1        ad 		goto out;
   1270   1.6        ad 	if (mc->mc_nsgent != 1) {
   1271   1.6        ad 		mlx_ccb_unmap(mlx, mc);
   1272   1.6        ad 		printf("mlx_periodic_eventlog_poll: too many segs\n");
   1273   1.6        ad 		goto out;
   1274   1.6        ad 	}
   1275   1.1        ad 
   1276   1.1        ad 	/* Build the command to get one log entry. */
   1277   1.1        ad 	mlx_make_type3(mc, MLX_CMD_LOGOP, MLX_LOGOP_GET, 1,
   1278   1.1        ad 	    mlx->mlx_lastevent, 0, 0, mc->mc_xfer_phys, 0);
   1279   1.1        ad 
   1280   1.1        ad 	mc->mc_mx.mx_handler = mlx_periodic_eventlog_respond;
   1281   1.1        ad 	mc->mc_mx.mx_dv = &mlx->mlx_dv;
   1282   1.1        ad 	mc->mc_mx.mx_context = result;
   1283   1.1        ad 
   1284   1.1        ad 	/* Start the command. */
   1285   1.1        ad 	mlx_ccb_enqueue(mlx, mc);
   1286   1.1        ad 
   1287   1.3        ad  out:
   1288   1.1        ad 	if (rv != 0) {
   1289   1.1        ad 		if (mc != NULL)
   1290   1.1        ad 			mlx_ccb_free(mlx, mc);
   1291   1.1        ad 		if (result != NULL)
   1292   1.1        ad 			free(result, M_DEVBUF);
   1293   1.1        ad 	}
   1294   1.1        ad }
   1295   1.1        ad 
   1296   1.1        ad /*
   1297   1.1        ad  * Handle the result of polling for a log message, generate diagnostic
   1298   1.1        ad  * output.  If this wasn't the last message waiting for us, we'll go collect
   1299   1.1        ad  * another.
   1300   1.1        ad  */
   1301   1.1        ad static void
   1302   1.1        ad mlx_periodic_eventlog_respond(struct mlx_ccb *mc)
   1303   1.1        ad {
   1304   1.1        ad 	struct mlx_softc *mlx;
   1305   1.1        ad 	struct mlx_eventlog_entry *el;
   1306   1.1        ad 	const char *reason;
   1307   1.1        ad 	u_int8_t sensekey, chan, targ;
   1308   1.1        ad 
   1309   1.1        ad 	mlx = (struct mlx_softc *)mc->mc_mx.mx_dv;
   1310   1.1        ad 	el = mc->mc_mx.mx_context;
   1311   1.1        ad 	mlx_ccb_unmap(mlx, mc);
   1312   1.1        ad 
   1313   1.1        ad 	mlx->mlx_lastevent++;
   1314   1.1        ad 
   1315   1.1        ad 	if (mc->mc_status == 0) {
   1316   1.1        ad 		switch (el->el_type) {
   1317   1.1        ad 		case MLX_LOGMSG_SENSE:		/* sense data */
   1318   1.1        ad 			sensekey = el->el_sense & 0x0f;
   1319   1.1        ad 			chan = (el->el_target >> 4) & 0x0f;
   1320   1.1        ad 			targ = el->el_target & 0x0f;
   1321   1.1        ad 
   1322   1.1        ad 			/*
   1323   1.1        ad 			 * This is the only sort of message we understand at
   1324   1.1        ad 			 * the moment.  The tests here are probably
   1325   1.1        ad 			 * incomplete.
   1326   1.1        ad 			 */
   1327   1.1        ad 
   1328   1.1        ad 			/*
   1329   1.1        ad 			 * Mylex vendor-specific message indicating a drive
   1330   1.1        ad 			 * was killed?
   1331   1.1        ad 			 */
   1332   1.1        ad 			if (sensekey == 9 && el->el_asc == 0x80) {
   1333   1.1        ad 				if (el->el_asq < sizeof(mlx_sense_msgs) /
   1334   1.1        ad 				    sizeof(mlx_sense_msgs[0]))
   1335   1.1        ad 					reason = mlx_sense_msgs[el->el_asq];
   1336   1.1        ad 				else
   1337   1.1        ad 					reason = "for unknown reason";
   1338   1.1        ad 
   1339   1.1        ad 				printf("%s: physical drive %d:%d killed %s\n",
   1340   1.1        ad 				    mlx->mlx_dv.dv_xname, chan, targ, reason);
   1341   1.1        ad 			}
   1342   1.1        ad 
   1343   1.1        ad 			/*
   1344   1.1        ad 			 * SCSI drive was reset?
   1345   1.1        ad 			 */
   1346   1.1        ad 			if (sensekey == 6 && el->el_asc == 0x29)
   1347   1.1        ad 				printf("%s: physical drive %d:%d reset\n",
   1348   1.1        ad 				    mlx->mlx_dv.dv_xname, chan, targ);
   1349   1.1        ad 
   1350   1.1        ad 			/*
   1351   1.1        ad 			 * SCSI drive error?
   1352   1.1        ad 			 */
   1353   1.1        ad 			if (!(sensekey == 0 ||
   1354   1.1        ad 			    (sensekey == 2 &&
   1355   1.1        ad 			    el->el_asc == 0x04 &&
   1356   1.1        ad 			    (el->el_asq == 0x01 || el->el_asq == 0x02)))) {
   1357   1.1        ad 				printf("%s: physical drive %d:%d error log: "
   1358   1.1        ad 				    "sense = %d asc = %x asq = %x\n",
   1359   1.1        ad 				    mlx->mlx_dv.dv_xname, chan, targ, sensekey,
   1360   1.1        ad 				    el->el_asc, el->el_asq);
   1361   1.1        ad 				printf("%s:   info = %d:%d:%d:%d "
   1362  1.12        ad 				    " csi = %d:%d:%d:%d\n",
   1363  1.12        ad 				    mlx->mlx_dv.dv_xname,
   1364  1.12        ad 				    el->el_information[0],
   1365  1.12        ad 				    el->el_information[1],
   1366  1.12        ad 				    el->el_information[2],
   1367  1.12        ad 				    el->el_information[3],
   1368   1.1        ad 				    el->el_csi[0], el->el_csi[1],
   1369   1.1        ad 				    el->el_csi[2], el->el_csi[3]);
   1370   1.1        ad 			}
   1371   1.1        ad 
   1372   1.1        ad 			break;
   1373  1.32     perry 
   1374   1.1        ad 		default:
   1375   1.1        ad 			printf("%s: unknown log message type 0x%x\n",
   1376   1.1        ad 			    mlx->mlx_dv.dv_xname, el->el_type);
   1377   1.1        ad 			break;
   1378   1.1        ad 		}
   1379   1.1        ad 	} else {
   1380   1.1        ad 		printf("%s: error reading message log - %s\n",
   1381   1.1        ad 		    mlx->mlx_dv.dv_xname, mlx_ccb_diagnose(mc));
   1382   1.1        ad 
   1383   1.1        ad 		/*
   1384   1.1        ad 		 * Give up on all the outstanding messages, as we may have
   1385   1.1        ad 		 * come unsynched.
   1386   1.1        ad 		 */
   1387   1.1        ad 		mlx->mlx_lastevent = mlx->mlx_currevent;
   1388   1.1        ad 	}
   1389   1.1        ad 
   1390   1.1        ad 	free(mc->mc_mx.mx_context, M_DEVBUF);
   1391   1.1        ad 	mlx_ccb_free(mlx, mc);
   1392   1.1        ad 
   1393   1.1        ad 	/*
   1394   1.1        ad 	 * Is there another message to obtain?
   1395   1.1        ad 	 */
   1396   1.1        ad 	if (mlx->mlx_lastevent != mlx->mlx_currevent)
   1397   1.1        ad 		mlx_periodic_eventlog_poll(mlx);
   1398   1.1        ad 	else
   1399   1.1        ad 		mlx->mlx_flags &= ~MLXF_EVENTLOG_BUSY;
   1400   1.1        ad }
   1401   1.1        ad 
   1402   1.1        ad /*
   1403   1.1        ad  * Handle check/rebuild operations in progress.
   1404   1.1        ad  */
   1405   1.1        ad static void
   1406   1.1        ad mlx_periodic_rebuild(struct mlx_ccb *mc)
   1407   1.1        ad {
   1408   1.1        ad 	struct mlx_softc *mlx;
   1409   1.1        ad 	const char *opstr;
   1410   1.1        ad 	struct mlx_rebuild_status *mr;
   1411   1.1        ad 
   1412   1.1        ad 	mlx = (struct mlx_softc *)mc->mc_mx.mx_dv;
   1413   1.1        ad 	mr = mc->mc_mx.mx_context;
   1414   1.1        ad 	mlx_ccb_unmap(mlx, mc);
   1415   1.1        ad 
   1416   1.1        ad 	switch (mc->mc_status) {
   1417   1.1        ad 	case 0:
   1418   1.1        ad 		/*
   1419   1.1        ad 		 * Operation running, update stats.
   1420   1.1        ad 		 */
   1421   1.1        ad 		mlx->mlx_rebuildstat = *mr;
   1422   1.1        ad 
   1423   1.1        ad 		/* Spontaneous rebuild/check? */
   1424   1.1        ad 		if (mlx->mlx_bg == 0) {
   1425   1.1        ad 			mlx->mlx_bg = MLX_BG_SPONTANEOUS;
   1426   1.1        ad 			printf("%s: background check/rebuild started\n",
   1427   1.1        ad 			    mlx->mlx_dv.dv_xname);
   1428   1.1        ad 		}
   1429   1.1        ad 		break;
   1430   1.1        ad 
   1431   1.1        ad 	case 0x0105:
   1432   1.1        ad 		/*
   1433   1.1        ad 		 * Nothing running, finalise stats and report.
   1434   1.1        ad 		 */
   1435   1.1        ad 		switch (mlx->mlx_bg) {
   1436   1.1        ad 		case MLX_BG_CHECK:
   1437   1.1        ad 			/* XXX Print drive? */
   1438   1.1        ad 			opstr = "consistency check";
   1439   1.1        ad 			break;
   1440   1.1        ad 
   1441   1.1        ad 		case MLX_BG_REBUILD:
   1442   1.1        ad 			/* XXX Print channel:target? */
   1443   1.1        ad 			opstr = "drive rebuild";
   1444   1.1        ad 			break;
   1445   1.1        ad 
   1446   1.1        ad 		case MLX_BG_SPONTANEOUS:
   1447   1.1        ad 		default:
   1448   1.1        ad 			/*
   1449   1.1        ad 			 * If we have previously been non-idle, report the
   1450   1.1        ad 			 * transition
   1451   1.1        ad 			 */
   1452   1.1        ad 			if (mlx->mlx_rebuildstat.rs_code !=
   1453   1.1        ad 			    MLX_REBUILDSTAT_IDLE)
   1454   1.1        ad 				opstr = "background check/rebuild";
   1455   1.1        ad 			else
   1456   1.1        ad 				opstr = NULL;
   1457   1.1        ad 		}
   1458   1.1        ad 
   1459   1.1        ad 		if (opstr != NULL)
   1460   1.1        ad 			printf("%s: %s completed\n", mlx->mlx_dv.dv_xname,
   1461   1.1        ad 			    opstr);
   1462   1.1        ad 
   1463   1.1        ad 		mlx->mlx_bg = 0;
   1464   1.1        ad 		mlx->mlx_rebuildstat.rs_code = MLX_REBUILDSTAT_IDLE;
   1465   1.1        ad 		break;
   1466   1.1        ad 	}
   1467   1.1        ad 
   1468   1.1        ad 	free(mc->mc_mx.mx_context, M_DEVBUF);
   1469   1.1        ad 	mlx_ccb_free(mlx, mc);
   1470   1.1        ad 	mlx->mlx_flags &= ~MLXF_PERIODIC_REBUILD;
   1471   1.1        ad }
   1472   1.1        ad 
   1473   1.1        ad /*
   1474   1.1        ad  * It's time to perform a channel pause action for (mlx), either start or
   1475   1.1        ad  * stop the pause.
   1476   1.1        ad  */
   1477   1.1        ad static void
   1478   1.1        ad mlx_pause_action(struct mlx_softc *mlx)
   1479   1.1        ad {
   1480   1.1        ad 	struct mlx_ccb *mc;
   1481   1.1        ad 	int failsafe, i, cmd;
   1482   1.1        ad 	time_t ct;
   1483   1.1        ad 
   1484   1.1        ad 	ct = mlx_curtime();
   1485   1.1        ad 
   1486   1.1        ad 	/* What are we doing here? */
   1487   1.1        ad 	if (mlx->mlx_pause.mp_when == 0) {
   1488   1.1        ad 		cmd = MLX_CMD_STARTCHANNEL;
   1489   1.1        ad 		failsafe = 0;
   1490   1.1        ad 	} else {
   1491   1.1        ad 		cmd = MLX_CMD_STOPCHANNEL;
   1492   1.1        ad 
   1493  1.32     perry 		/*
   1494   1.1        ad 		 * Channels will always start again after the failsafe
   1495  1.32     perry 		 * period, which is specified in multiples of 30 seconds.
   1496   1.1        ad 		 * This constrains us to a maximum pause of 450 seconds.
   1497   1.1        ad 		 */
   1498   1.1        ad 		failsafe = ((mlx->mlx_pause.mp_howlong - ct) + 5) / 30;
   1499   1.1        ad 
   1500   1.1        ad 		if (failsafe > 0xf) {
   1501   1.1        ad 			failsafe = 0xf;
   1502   1.1        ad 			mlx->mlx_pause.mp_howlong = ct + (0xf * 30) - 5;
   1503   1.1        ad 		}
   1504   1.1        ad 	}
   1505   1.1        ad 
   1506   1.1        ad 	/* Build commands for every channel requested. */
   1507  1.18        ad 	for (i = 0; i < mlx->mlx_ci.ci_nchan; i++) {
   1508   1.1        ad 		if ((1 << i) & mlx->mlx_pause.mp_which) {
   1509   1.1        ad 			if (mlx_ccb_alloc(mlx, &mc, 1) != 0) {
   1510   1.1        ad 				printf("%s: %s failed for channel %d\n",
   1511   1.1        ad 				    mlx->mlx_dv.dv_xname,
   1512   1.1        ad 				    cmd == MLX_CMD_STOPCHANNEL ?
   1513   1.1        ad 				    "pause" : "resume", i);
   1514   1.1        ad 				continue;
   1515   1.1        ad 			}
   1516   1.1        ad 
   1517   1.1        ad 			/* Build the command. */
   1518   1.1        ad 			mlx_make_type2(mc, cmd, (failsafe << 4) | i, 0, 0,
   1519   1.1        ad 			    0, 0, 0, 0, 0);
   1520   1.1        ad 			mc->mc_mx.mx_handler = mlx_pause_done;
   1521   1.1        ad 			mc->mc_mx.mx_dv = &mlx->mlx_dv;
   1522   1.1        ad 
   1523   1.1        ad 			mlx_ccb_enqueue(mlx, mc);
   1524   1.1        ad 		}
   1525   1.1        ad 	}
   1526   1.1        ad }
   1527   1.1        ad 
   1528   1.1        ad static void
   1529   1.1        ad mlx_pause_done(struct mlx_ccb *mc)
   1530   1.1        ad {
   1531   1.1        ad 	struct mlx_softc *mlx;
   1532   1.1        ad 	int command, channel;
   1533   1.1        ad 
   1534   1.1        ad 	mlx = (struct mlx_softc *)mc->mc_mx.mx_dv;
   1535   1.1        ad 	command = mc->mc_mbox[0];
   1536   1.1        ad 	channel = mc->mc_mbox[2] & 0xf;
   1537   1.1        ad 
   1538   1.1        ad 	if (mc->mc_status != 0)
   1539   1.1        ad 		printf("%s: %s command failed - %s\n", mlx->mlx_dv.dv_xname,
   1540   1.1        ad 		    command == MLX_CMD_STOPCHANNEL ? "pause" : "resume",
   1541   1.1        ad 		    mlx_ccb_diagnose(mc));
   1542   1.1        ad 	else if (command == MLX_CMD_STOPCHANNEL)
   1543   1.1        ad 		printf("%s: channel %d pausing for %ld seconds\n",
   1544   1.1        ad 		    mlx->mlx_dv.dv_xname, channel,
   1545   1.1        ad 		    (long)(mlx->mlx_pause.mp_howlong - mlx_curtime()));
   1546   1.1        ad 	else
   1547   1.1        ad 		printf("%s: channel %d resuming\n", mlx->mlx_dv.dv_xname,
   1548   1.1        ad 		    channel);
   1549   1.1        ad 
   1550   1.1        ad 	mlx_ccb_free(mlx, mc);
   1551   1.1        ad }
   1552   1.1        ad 
   1553   1.1        ad /*
   1554   1.1        ad  * Perform an Enquiry command using a type-3 command buffer and a return a
   1555   1.1        ad  * single linear result buffer.  If the completion function is specified, it
   1556   1.1        ad  * will be called with the completed command (and the result response will
   1557   1.1        ad  * not be valid until that point).  Otherwise, the command will either be
   1558   1.1        ad  * busy-waited for (interrupts must be blocked), or slept for.
   1559   1.1        ad  */
   1560   1.1        ad static void *
   1561   1.1        ad mlx_enquire(struct mlx_softc *mlx, int command, size_t bufsize,
   1562   1.1        ad 	    void (*handler)(struct mlx_ccb *mc), int waitok)
   1563   1.1        ad {
   1564   1.1        ad 	struct mlx_ccb *mc;
   1565   1.1        ad 	void *result;
   1566   1.1        ad 	int rv, mapped;
   1567   1.1        ad 
   1568   1.1        ad 	result = NULL;
   1569   1.1        ad 	mapped = 0;
   1570   1.1        ad 
   1571   1.1        ad 	if ((rv = mlx_ccb_alloc(mlx, &mc, 1)) != 0)
   1572   1.1        ad 		goto out;
   1573   1.1        ad 
   1574   1.1        ad 	result = malloc(bufsize, M_DEVBUF, waitok ? M_WAITOK : M_NOWAIT);
   1575   1.1        ad 	if (result == NULL) {
   1576   1.1        ad 		printf("mlx_enquire: malloc() failed\n");
   1577   1.1        ad 		goto out;
   1578   1.1        ad 	}
   1579   1.1        ad 	if ((rv = mlx_ccb_map(mlx, mc, result, bufsize, MC_XFER_IN)) != 0)
   1580   1.1        ad 		goto out;
   1581   1.1        ad 	mapped = 1;
   1582   1.6        ad 	if (mc->mc_nsgent != 1) {
   1583   1.6        ad 		printf("mlx_enquire: too many segs\n");
   1584   1.6        ad 		goto out;
   1585   1.6        ad 	}
   1586   1.1        ad 
   1587   1.1        ad 	/* Build an enquiry command. */
   1588   1.1        ad 	mlx_make_type2(mc, command, 0, 0, 0, 0, 0, 0, mc->mc_xfer_phys, 0);
   1589   1.1        ad 
   1590   1.1        ad 	/* Do we want a completion callback? */
   1591   1.1        ad 	if (handler != NULL) {
   1592   1.1        ad 		mc->mc_mx.mx_context = result;
   1593   1.1        ad 		mc->mc_mx.mx_dv = &mlx->mlx_dv;
   1594   1.1        ad 		mc->mc_mx.mx_handler = handler;
   1595   1.1        ad 		mlx_ccb_enqueue(mlx, mc);
   1596   1.1        ad 	} else {
   1597   1.1        ad 		/* Run the command in either polled or wait mode. */
   1598   1.9        ad 		if (waitok)
   1599   1.9        ad 			rv = mlx_ccb_wait(mlx, mc);
   1600   1.9        ad 		else
   1601   1.9        ad 			rv = mlx_ccb_poll(mlx, mc, 5000);
   1602   1.1        ad 	}
   1603   1.1        ad 
   1604   1.3        ad  out:
   1605   1.1        ad 	/* We got a command, but nobody else will free it. */
   1606   1.1        ad 	if (handler == NULL && mc != NULL) {
   1607   1.1        ad 		if (mapped)
   1608   1.1        ad 			mlx_ccb_unmap(mlx, mc);
   1609   1.1        ad 		mlx_ccb_free(mlx, mc);
   1610   1.1        ad 	}
   1611   1.1        ad 
   1612   1.1        ad 	/* We got an error, and we allocated a result. */
   1613   1.1        ad 	if (rv != 0 && result != NULL) {
   1614   1.1        ad 		if (handler != NULL && mc != NULL) {
   1615   1.1        ad 			if (mapped)
   1616   1.1        ad 				mlx_ccb_unmap(mlx, mc);
   1617   1.1        ad 			mlx_ccb_free(mlx, mc);
   1618   1.1        ad 		}
   1619   1.1        ad 		free(result, M_DEVBUF);
   1620   1.1        ad 		result = NULL;
   1621   1.1        ad 	}
   1622   1.1        ad 
   1623   1.1        ad 	return (result);
   1624   1.1        ad }
   1625   1.1        ad 
   1626   1.1        ad /*
   1627   1.1        ad  * Perform a Flush command on the nominated controller.
   1628   1.1        ad  *
   1629   1.1        ad  * May be called with interrupts enabled or disabled; will not return until
   1630   1.1        ad  * the flush operation completes or fails.
   1631   1.1        ad  */
   1632   1.1        ad int
   1633   1.1        ad mlx_flush(struct mlx_softc *mlx, int async)
   1634   1.1        ad {
   1635   1.1        ad 	struct mlx_ccb *mc;
   1636   1.1        ad 	int rv;
   1637   1.1        ad 
   1638   1.1        ad 	if ((rv = mlx_ccb_alloc(mlx, &mc, 1)) != 0)
   1639   1.1        ad 		goto out;
   1640   1.1        ad 
   1641   1.1        ad 	/* Build a flush command and fire it off. */
   1642   1.1        ad 	mlx_make_type2(mc, MLX_CMD_FLUSH, 0, 0, 0, 0, 0, 0, 0, 0);
   1643   1.1        ad 
   1644   1.1        ad 	if (async)
   1645   1.1        ad 		rv = mlx_ccb_wait(mlx, mc);
   1646   1.1        ad 	else
   1647   1.1        ad 		rv = mlx_ccb_poll(mlx, mc, MLX_TIMEOUT * 1000);
   1648   1.1        ad 	if (rv != 0)
   1649   1.1        ad 		goto out;
   1650   1.1        ad 
   1651   1.1        ad 	/* Command completed OK? */
   1652   1.1        ad 	if (mc->mc_status != 0) {
   1653   1.1        ad 		printf("%s: FLUSH failed - %s\n", mlx->mlx_dv.dv_xname,
   1654   1.1        ad 		    mlx_ccb_diagnose(mc));
   1655   1.1        ad 		rv = EIO;
   1656   1.1        ad 	}
   1657   1.3        ad  out:
   1658   1.1        ad 	if (mc != NULL)
   1659   1.1        ad 		mlx_ccb_free(mlx, mc);
   1660   1.1        ad 
   1661   1.1        ad 	return (rv);
   1662   1.1        ad }
   1663   1.1        ad 
   1664   1.1        ad /*
   1665   1.1        ad  * Start a background consistency check on (drive).
   1666   1.1        ad  */
   1667   1.1        ad static int
   1668   1.1        ad mlx_check(struct mlx_softc *mlx, int drive)
   1669   1.1        ad {
   1670   1.1        ad 	struct mlx_ccb *mc;
   1671   1.1        ad 	int rv;
   1672   1.1        ad 
   1673   1.1        ad 	/* Get ourselves a command buffer. */
   1674   1.1        ad 	rv = 0x10000;
   1675   1.1        ad 
   1676   1.1        ad 	if (mlx_ccb_alloc(mlx, &mc, 1) != 0)
   1677   1.1        ad 		goto out;
   1678   1.1        ad 
   1679   1.1        ad 	/* Build a checkasync command, set the "fix it" flag. */
   1680   1.1        ad 	mlx_make_type2(mc, MLX_CMD_CHECKASYNC, 0, 0, 0, 0, 0, drive | 0x80,
   1681   1.1        ad 	    0, 0);
   1682   1.1        ad 
   1683   1.1        ad 	/* Start the command and wait for it to be returned. */
   1684   1.1        ad 	if (mlx_ccb_wait(mlx, mc) != 0)
   1685   1.1        ad 		goto out;
   1686   1.1        ad 
   1687   1.1        ad 	/* Command completed OK? */
   1688   1.1        ad 	if (mc->mc_status != 0)
   1689   1.1        ad 		printf("%s: CHECK ASYNC failed - %s\n", mlx->mlx_dv.dv_xname,
   1690   1.1        ad 		    mlx_ccb_diagnose(mc));
   1691   1.1        ad 	else
   1692   1.1        ad 		printf("%s: consistency check started",
   1693   1.1        ad 		    mlx->mlx_sysdrive[drive].ms_dv->dv_xname);
   1694   1.1        ad 
   1695   1.1        ad 	rv = mc->mc_status;
   1696   1.3        ad  out:
   1697   1.1        ad 	if (mc != NULL)
   1698   1.1        ad 		mlx_ccb_free(mlx, mc);
   1699   1.1        ad 
   1700   1.1        ad 	return (rv);
   1701   1.1        ad }
   1702   1.1        ad 
   1703   1.1        ad /*
   1704   1.1        ad  * Start a background rebuild of the physical drive at (channel),(target).
   1705   1.1        ad  *
   1706   1.1        ad  * May be called with interrupts enabled or disabled; will return as soon as
   1707   1.1        ad  * the operation has started or been refused.
   1708   1.1        ad  */
   1709   1.1        ad static int
   1710   1.1        ad mlx_rebuild(struct mlx_softc *mlx, int channel, int target)
   1711   1.1        ad {
   1712   1.1        ad 	struct mlx_ccb *mc;
   1713   1.1        ad 	int error;
   1714   1.1        ad 
   1715   1.1        ad 	error = 0x10000;
   1716   1.1        ad 	if (mlx_ccb_alloc(mlx, &mc, 1) != 0)
   1717   1.1        ad 		goto out;
   1718   1.1        ad 
   1719   1.1        ad 	/* Build a rebuildasync command, set the "fix it" flag. */
   1720   1.1        ad 	mlx_make_type2(mc, MLX_CMD_REBUILDASYNC, channel, target, 0, 0, 0, 0,
   1721   1.1        ad 	    0, 0);
   1722   1.1        ad 
   1723   1.1        ad 	/* Start the command and wait for it to be returned. */
   1724   1.1        ad 	if (mlx_ccb_wait(mlx, mc) != 0)
   1725   1.1        ad 		goto out;
   1726   1.1        ad 
   1727   1.1        ad 	/* Command completed OK? */
   1728   1.1        ad 	printf("%s: ", mlx->mlx_dv.dv_xname);
   1729   1.1        ad 	if (mc->mc_status != 0)
   1730   1.1        ad 		printf("REBUILD ASYNC failed - %s\n", mlx_ccb_diagnose(mc));
   1731   1.1        ad 	else
   1732   1.1        ad 		printf("rebuild started for %d:%d\n", channel, target);
   1733   1.1        ad 
   1734   1.1        ad 	error = mc->mc_status;
   1735   1.1        ad 
   1736   1.3        ad  out:
   1737   1.1        ad 	if (mc != NULL)
   1738   1.1        ad 		mlx_ccb_free(mlx, mc);
   1739   1.1        ad 
   1740   1.1        ad 	return (error);
   1741   1.1        ad }
   1742   1.1        ad 
   1743   1.1        ad /*
   1744   1.1        ad  * Take a command from user-space and try to run it.
   1745   1.1        ad  *
   1746   1.1        ad  * XXX Note that this can't perform very much in the way of error checking,
   1747   1.1        ad  * XXX and as such, applications _must_ be considered trustworthy.
   1748   1.1        ad  *
   1749   1.1        ad  * XXX Commands using S/G for data are not supported.
   1750   1.1        ad  */
   1751   1.1        ad static int
   1752   1.1        ad mlx_user_command(struct mlx_softc *mlx, struct mlx_usercommand *mu)
   1753   1.1        ad {
   1754   1.1        ad 	struct mlx_ccb *mc;
   1755   1.1        ad 	struct mlx_dcdb *dcdb;
   1756   1.1        ad 	void *kbuf;
   1757   1.1        ad 	int rv, mapped;
   1758   1.1        ad 
   1759   1.1        ad 	if ((mu->mu_bufdir & ~MU_XFER_MASK) != 0)
   1760   1.1        ad 		return (EINVAL);
   1761   1.1        ad 
   1762   1.1        ad 	kbuf = NULL;
   1763   1.1        ad 	dcdb = NULL;
   1764   1.1        ad 	mapped = 0;
   1765   1.1        ad 
   1766   1.1        ad 	/* Get ourselves a command and copy in from user space. */
   1767   1.1        ad 	if ((rv = mlx_ccb_alloc(mlx, &mc, 1)) != 0) {
   1768   1.1        ad 		DPRINTF(("mlx_user_command: mlx_ccb_alloc = %d\n", rv));
   1769   1.1        ad 		goto out;
   1770   1.1        ad 	}
   1771   1.1        ad 
   1772   1.1        ad 	memcpy(mc->mc_mbox, mu->mu_command, sizeof(mc->mc_mbox));
   1773   1.1        ad 
   1774   1.1        ad 	/*
   1775   1.1        ad 	 * If we need a buffer for data transfer, allocate one and copy in
   1776   1.1        ad 	 * its initial contents.
   1777   1.1        ad 	 */
   1778   1.1        ad 	if (mu->mu_datasize > 0) {
   1779  1.13  jdolecek 		if (mu->mu_datasize > MAXPHYS)
   1780  1.13  jdolecek 			return (EINVAL);
   1781  1.13  jdolecek 
   1782   1.1        ad 		kbuf = malloc(mu->mu_datasize, M_DEVBUF, M_WAITOK);
   1783   1.1        ad 		if (kbuf == NULL) {
   1784   1.1        ad 			DPRINTF(("mlx_user_command: malloc = NULL\n"));
   1785   1.1        ad 			rv = ENOMEM;
   1786   1.1        ad 			goto out;
   1787   1.1        ad 		}
   1788   1.1        ad 
   1789   1.1        ad 		if ((mu->mu_bufdir & MU_XFER_OUT) != 0) {
   1790   1.1        ad 			rv = copyin(mu->mu_buf, kbuf, mu->mu_datasize);
   1791   1.1        ad 			if (rv != 0) {
   1792   1.1        ad 				DPRINTF(("mlx_user_command: copyin = %d\n",
   1793   1.1        ad 				    rv));
   1794   1.1        ad 				goto out;
   1795   1.1        ad 			}
   1796   1.1        ad 		}
   1797   1.1        ad 
   1798   1.1        ad 		/* Map the buffer so the controller can see it. */
   1799   1.1        ad 		rv = mlx_ccb_map(mlx, mc, kbuf, mu->mu_datasize, mu->mu_bufdir);
   1800   1.1        ad 		if (rv != 0) {
   1801   1.1        ad 			DPRINTF(("mlx_user_command: mlx_ccb_map = %d\n", rv));
   1802   1.1        ad 			goto out;
   1803   1.1        ad 		}
   1804   1.1        ad 		if (mc->mc_nsgent > 1) {
   1805   1.1        ad 			DPRINTF(("mlx_user_command: too many s/g entries\n"));
   1806   1.1        ad 			rv = EFBIG;
   1807   1.1        ad 			goto out;
   1808   1.1        ad 		}
   1809   1.1        ad 		mapped = 1;
   1810   1.1        ad 	}
   1811   1.1        ad 
   1812  1.32     perry 	/*
   1813  1.32     perry 	 * If this is a passthrough SCSI command, the DCDB is packed at the
   1814   1.1        ad 	 * beginning of the data area.  Fix up the DCDB to point to the correct physical
   1815   1.1        ad 	 * address and override any bufptr supplied by the caller since we know
   1816   1.1        ad 	 * what it's meant to be.
   1817   1.1        ad 	 */
   1818   1.1        ad 	if (mc->mc_mbox[0] == MLX_CMD_DIRECT_CDB) {
   1819   1.1        ad 		dcdb = (struct mlx_dcdb *)kbuf;
   1820   1.1        ad 		dcdb->dcdb_physaddr = mc->mc_xfer_phys + sizeof(*dcdb);
   1821   1.1        ad 		mu->mu_bufptr = 8;
   1822   1.1        ad 	}
   1823   1.1        ad 
   1824  1.32     perry 	/*
   1825   1.1        ad 	 * If there's a data buffer, fix up the command's buffer pointer.
   1826   1.1        ad 	 */
   1827   1.1        ad 	if (mu->mu_datasize > 0) {
   1828   1.1        ad 		/* Range check the pointer to physical buffer address. */
   1829   1.1        ad 		if (mu->mu_bufptr < 0 ||
   1830   1.1        ad 		    mu->mu_bufptr > sizeof(mu->mu_command) - 4) {
   1831   1.1        ad 			DPRINTF(("mlx_user_command: bufptr botch\n"));
   1832   1.1        ad 			rv = EINVAL;
   1833   1.1        ad 			goto out;
   1834   1.1        ad 		}
   1835   1.1        ad 
   1836   1.1        ad 		mc->mc_mbox[mu->mu_bufptr] = mc->mc_xfer_phys;
   1837   1.1        ad 		mc->mc_mbox[mu->mu_bufptr+1] = mc->mc_xfer_phys >> 8;
   1838   1.1        ad 		mc->mc_mbox[mu->mu_bufptr+2] = mc->mc_xfer_phys >> 16;
   1839   1.1        ad 		mc->mc_mbox[mu->mu_bufptr+3] = mc->mc_xfer_phys >> 24;
   1840   1.1        ad 	}
   1841   1.1        ad 
   1842   1.1        ad 	/* Submit the command and wait. */
   1843   1.1        ad 	if ((rv = mlx_ccb_wait(mlx, mc)) != 0) {
   1844   1.1        ad #ifdef DEBUG
   1845   1.1        ad 		printf("mlx_user_command: mlx_ccb_wait = %d\n", rv);
   1846   1.1        ad #endif
   1847   1.1        ad 	}
   1848   1.1        ad 
   1849   1.3        ad  out:
   1850   1.1        ad 	if (mc != NULL) {
   1851   1.1        ad 		if (mapped)
   1852   1.1        ad 			mlx_ccb_unmap(mlx, mc);
   1853   1.1        ad 		mlx_ccb_free(mlx, mc);
   1854   1.1        ad 	}
   1855   1.1        ad 
   1856   1.1        ad 	/* Copy out status and data */
   1857   1.1        ad 	mu->mu_status = mc->mc_status;
   1858   1.1        ad 
   1859   1.1        ad 	if (kbuf != NULL) {
   1860   1.1        ad 		if (mu->mu_datasize > 0 && (mu->mu_bufdir & MU_XFER_IN) != 0) {
   1861   1.1        ad 			rv = copyout(kbuf, mu->mu_buf, mu->mu_datasize);
   1862   1.1        ad #ifdef DIAGNOSTIC
   1863   1.1        ad 			if (rv != 0)
   1864   1.1        ad 				printf("mlx_user_command: copyout = %d\n", rv);
   1865   1.1        ad #endif
   1866   1.1        ad 		}
   1867   1.1        ad 	}
   1868   1.1        ad 	if (kbuf != NULL)
   1869   1.1        ad 		free(kbuf, M_DEVBUF);
   1870   1.1        ad 
   1871   1.1        ad 	return (rv);
   1872   1.1        ad }
   1873   1.1        ad 
   1874   1.1        ad /*
   1875   1.1        ad  * Allocate and initialise a CCB.
   1876   1.1        ad  */
   1877   1.1        ad int
   1878  1.21        ad mlx_ccb_alloc(struct mlx_softc *mlx, struct mlx_ccb **mcp, int control)
   1879   1.1        ad {
   1880   1.1        ad 	struct mlx_ccb *mc;
   1881   1.1        ad 	int s;
   1882   1.1        ad 
   1883   1.1        ad 	s = splbio();
   1884  1.24        ad 	mc = SLIST_FIRST(&mlx->mlx_ccb_freelist);
   1885  1.21        ad 	if (control) {
   1886  1.21        ad 		if (mlx->mlx_nccbs_ctrl >= MLX_NCCBS_CONTROL) {
   1887  1.21        ad 			splx(s);
   1888  1.21        ad 			*mcp = NULL;
   1889  1.21        ad 			return (EAGAIN);
   1890  1.21        ad 		}
   1891  1.21        ad 		mc->mc_flags |= MC_CONTROL;
   1892  1.21        ad 		mlx->mlx_nccbs_ctrl++;
   1893   1.1        ad 	}
   1894   1.1        ad 	SLIST_REMOVE_HEAD(&mlx->mlx_ccb_freelist, mc_chain.slist);
   1895   1.1        ad 	splx(s);
   1896   1.1        ad 
   1897   1.1        ad 	*mcp = mc;
   1898   1.1        ad 	return (0);
   1899   1.1        ad }
   1900   1.1        ad 
   1901   1.1        ad /*
   1902   1.1        ad  * Free a CCB.
   1903   1.1        ad  */
   1904   1.1        ad void
   1905   1.1        ad mlx_ccb_free(struct mlx_softc *mlx, struct mlx_ccb *mc)
   1906   1.1        ad {
   1907   1.1        ad 	int s;
   1908   1.1        ad 
   1909   1.1        ad 	s = splbio();
   1910  1.21        ad 	if ((mc->mc_flags & MC_CONTROL) != 0)
   1911  1.21        ad 		mlx->mlx_nccbs_ctrl--;
   1912   1.1        ad 	mc->mc_flags = 0;
   1913   1.1        ad 	SLIST_INSERT_HEAD(&mlx->mlx_ccb_freelist, mc, mc_chain.slist);
   1914   1.1        ad 	splx(s);
   1915   1.1        ad }
   1916   1.1        ad 
   1917   1.1        ad /*
   1918   1.1        ad  * If a CCB is specified, enqueue it.  Pull CCBs off the software queue in
   1919   1.1        ad  * the order that they were enqueued and try to submit their mailboxes to
   1920   1.1        ad  * the controller for execution.
   1921   1.1        ad  */
   1922   1.1        ad void
   1923   1.1        ad mlx_ccb_enqueue(struct mlx_softc *mlx, struct mlx_ccb *mc)
   1924   1.1        ad {
   1925   1.1        ad 	int s;
   1926   1.1        ad 
   1927   1.1        ad 	s = splbio();
   1928   1.1        ad 
   1929   1.1        ad 	if (mc != NULL)
   1930   1.1        ad 		SIMPLEQ_INSERT_TAIL(&mlx->mlx_ccb_queue, mc, mc_chain.simpleq);
   1931   1.1        ad 
   1932   1.1        ad 	while ((mc = SIMPLEQ_FIRST(&mlx->mlx_ccb_queue)) != NULL) {
   1933   1.1        ad 		if (mlx_ccb_submit(mlx, mc) != 0)
   1934   1.1        ad 			break;
   1935  1.17     lukem 		SIMPLEQ_REMOVE_HEAD(&mlx->mlx_ccb_queue, mc_chain.simpleq);
   1936  1.12        ad 		TAILQ_INSERT_TAIL(&mlx->mlx_ccb_worklist, mc, mc_chain.tailq);
   1937   1.1        ad 	}
   1938   1.1        ad 
   1939   1.1        ad 	splx(s);
   1940   1.1        ad }
   1941   1.1        ad 
   1942   1.1        ad /*
   1943   1.1        ad  * Map the specified CCB's data buffer onto the bus, and fill the
   1944   1.1        ad  * scatter-gather list.
   1945   1.1        ad  */
   1946   1.1        ad int
   1947   1.1        ad mlx_ccb_map(struct mlx_softc *mlx, struct mlx_ccb *mc, void *data, int size,
   1948   1.1        ad 	    int dir)
   1949   1.1        ad {
   1950   1.1        ad 	struct mlx_sgentry *sge;
   1951   1.1        ad 	int nsegs, i, rv, sgloff;
   1952   1.1        ad 	bus_dmamap_t xfer;
   1953   1.1        ad 
   1954   1.1        ad 	xfer = mc->mc_xfer_map;
   1955   1.1        ad 
   1956   1.1        ad 	rv = bus_dmamap_load(mlx->mlx_dmat, xfer, data, size, NULL,
   1957  1.11   thorpej 	    BUS_DMA_NOWAIT | BUS_DMA_STREAMING |
   1958  1.11   thorpej 	    ((dir & MC_XFER_IN) ? BUS_DMA_READ : BUS_DMA_WRITE));
   1959   1.1        ad 	if (rv != 0)
   1960   1.1        ad 		return (rv);
   1961   1.1        ad 
   1962   1.1        ad 	nsegs = xfer->dm_nsegs;
   1963   1.1        ad 	mc->mc_xfer_size = size;
   1964   1.1        ad 	mc->mc_flags |= dir;
   1965   1.1        ad 	mc->mc_nsgent = nsegs;
   1966   1.1        ad 	mc->mc_xfer_phys = xfer->dm_segs[0].ds_addr;
   1967   1.1        ad 
   1968   1.1        ad 	sgloff = MLX_SGL_SIZE * mc->mc_ident;
   1969   1.1        ad 	sge = (struct mlx_sgentry *)((caddr_t)mlx->mlx_sgls + sgloff);
   1970   1.1        ad 
   1971   1.1        ad 	for (i = 0; i < nsegs; i++, sge++) {
   1972   1.1        ad 		sge->sge_addr = htole32(xfer->dm_segs[i].ds_addr);
   1973   1.1        ad 		sge->sge_count = htole32(xfer->dm_segs[i].ds_len);
   1974   1.1        ad 	}
   1975   1.1        ad 
   1976   1.1        ad 	if ((dir & MC_XFER_OUT) != 0)
   1977   1.1        ad 		i = BUS_DMASYNC_PREWRITE;
   1978   1.1        ad 	else
   1979   1.1        ad 		i = 0;
   1980   1.1        ad 	if ((dir & MC_XFER_IN) != 0)
   1981   1.1        ad 		i |= BUS_DMASYNC_PREREAD;
   1982   1.1        ad 
   1983   1.1        ad 	bus_dmamap_sync(mlx->mlx_dmat, xfer, 0, mc->mc_xfer_size, i);
   1984   1.1        ad 	bus_dmamap_sync(mlx->mlx_dmat, mlx->mlx_dmamap, sgloff,
   1985   1.1        ad 	    MLX_SGL_SIZE, BUS_DMASYNC_PREWRITE);
   1986   1.1        ad 
   1987   1.1        ad 	return (0);
   1988   1.1        ad }
   1989   1.1        ad 
   1990   1.1        ad /*
   1991   1.1        ad  * Unmap the specified CCB's data buffer.
   1992   1.1        ad  */
   1993   1.1        ad void
   1994   1.1        ad mlx_ccb_unmap(struct mlx_softc *mlx, struct mlx_ccb *mc)
   1995   1.1        ad {
   1996   1.1        ad 	int i;
   1997   1.1        ad 
   1998   1.1        ad 	bus_dmamap_sync(mlx->mlx_dmat, mlx->mlx_dmamap,
   1999   1.1        ad 	    MLX_SGL_SIZE * mc->mc_ident, MLX_SGL_SIZE,
   2000   1.1        ad 	    BUS_DMASYNC_POSTWRITE);
   2001   1.1        ad 
   2002   1.1        ad 	if ((mc->mc_flags & MC_XFER_OUT) != 0)
   2003   1.6        ad 		i = BUS_DMASYNC_POSTWRITE;
   2004   1.1        ad 	else
   2005   1.1        ad 		i = 0;
   2006   1.1        ad 	if ((mc->mc_flags & MC_XFER_IN) != 0)
   2007   1.6        ad 		i |= BUS_DMASYNC_POSTREAD;
   2008   1.1        ad 
   2009   1.1        ad 	bus_dmamap_sync(mlx->mlx_dmat, mc->mc_xfer_map, 0, mc->mc_xfer_size, i);
   2010   1.1        ad 	bus_dmamap_unload(mlx->mlx_dmat, mc->mc_xfer_map);
   2011   1.1        ad }
   2012   1.1        ad 
   2013   1.1        ad /*
   2014   1.1        ad  * Submit the CCB, and busy-wait for it to complete.  Return non-zero on
   2015   1.1        ad  * timeout or submission error.  Must be called with interrupts blocked.
   2016   1.1        ad  */
   2017   1.1        ad int
   2018   1.1        ad mlx_ccb_poll(struct mlx_softc *mlx, struct mlx_ccb *mc, int timo)
   2019   1.1        ad {
   2020   1.1        ad 	int rv;
   2021   1.1        ad 
   2022   1.1        ad 	mc->mc_mx.mx_handler = NULL;
   2023   1.1        ad 
   2024   1.1        ad 	if ((rv = mlx_ccb_submit(mlx, mc)) != 0)
   2025   1.1        ad 		return (rv);
   2026  1.32     perry 	TAILQ_INSERT_TAIL(&mlx->mlx_ccb_worklist, mc, mc_chain.tailq);
   2027   1.1        ad 
   2028   1.1        ad 	for (timo *= 10; timo != 0; timo--) {
   2029   1.1        ad 		mlx_intr(mlx);
   2030   1.1        ad 		if (mc->mc_status != MLX_STATUS_BUSY)
   2031   1.1        ad 			break;
   2032   1.1        ad 		DELAY(100);
   2033   1.1        ad 	}
   2034   1.1        ad 
   2035   1.1        ad 	if (timo != 0) {
   2036   1.1        ad 		if (mc->mc_status != 0) {
   2037   1.1        ad 			printf("%s: command failed - %s\n",
   2038   1.1        ad 			    mlx->mlx_dv.dv_xname, mlx_ccb_diagnose(mc));
   2039   1.1        ad 			rv = EIO;
   2040   1.1        ad 		} else
   2041   1.1        ad 			rv = 0;
   2042   1.1        ad 	} else {
   2043   1.1        ad 		printf("%s: command timed out\n", mlx->mlx_dv.dv_xname);
   2044   1.1        ad 		rv = EIO;
   2045   1.1        ad 	}
   2046   1.1        ad 
   2047   1.1        ad 	return (rv);
   2048   1.1        ad }
   2049   1.1        ad 
   2050   1.1        ad /*
   2051   1.1        ad  * Enqueue the CCB, and sleep until it completes.  Return non-zero on
   2052   1.1        ad  * timeout or error.
   2053   1.1        ad  */
   2054   1.1        ad int
   2055   1.1        ad mlx_ccb_wait(struct mlx_softc *mlx, struct mlx_ccb *mc)
   2056   1.1        ad {
   2057   1.1        ad 	int s;
   2058   1.1        ad 
   2059   1.1        ad 	mc->mc_flags |= MC_WAITING;
   2060   1.1        ad 	mc->mc_mx.mx_handler = NULL;
   2061   1.1        ad 
   2062   1.1        ad 	s = splbio();
   2063   1.1        ad 	mlx_ccb_enqueue(mlx, mc);
   2064   1.1        ad 	tsleep(mc, PRIBIO, "mlxwccb", 0);
   2065   1.1        ad 	splx(s);
   2066   1.1        ad 
   2067   1.1        ad 	if (mc->mc_status != 0) {
   2068   1.1        ad 		printf("%s: command failed - %s\n", mlx->mlx_dv.dv_xname,
   2069   1.1        ad 		    mlx_ccb_diagnose(mc));
   2070   1.1        ad 		return (EIO);
   2071   1.1        ad 	}
   2072   1.1        ad 
   2073   1.1        ad 	return (0);
   2074   1.1        ad }
   2075   1.1        ad 
   2076   1.1        ad /*
   2077   1.1        ad  * Try to submit a CCB's mailbox to the controller for execution.  Return
   2078   1.1        ad  * non-zero on timeout or error.  Must be called with interrupts blocked.
   2079   1.1        ad  */
   2080   1.1        ad static int
   2081   1.1        ad mlx_ccb_submit(struct mlx_softc *mlx, struct mlx_ccb *mc)
   2082   1.1        ad {
   2083   1.1        ad 	int i, s, r;
   2084   1.1        ad 
   2085   1.1        ad 	/* Save the ident so we can handle this command when complete. */
   2086  1.21        ad 	mc->mc_mbox[1] = (u_int8_t)(mc->mc_ident + 1);
   2087   1.1        ad 
   2088   1.1        ad 	/* Mark the command as currently being processed. */
   2089   1.1        ad 	mc->mc_status = MLX_STATUS_BUSY;
   2090   1.1        ad 	mc->mc_expiry = mlx_curtime() + MLX_TIMEOUT;
   2091   1.1        ad 
   2092   1.1        ad 	/* Spin waiting for the mailbox. */
   2093   1.1        ad 	for (i = 100; i != 0; i--) {
   2094   1.1        ad 		s = splbio();
   2095   1.1        ad 		r = (*mlx->mlx_submit)(mlx, mc);
   2096   1.1        ad 		splx(s);
   2097   1.1        ad 		if (r != 0)
   2098   1.1        ad 			break;
   2099   1.1        ad 		DELAY(100);
   2100   1.1        ad 	}
   2101   1.4        ad 	if (i != 0)
   2102   1.1        ad 		return (0);
   2103   1.1        ad 
   2104   1.1        ad 	DPRINTF(("mlx_ccb_submit: rejected; queueing\n"));
   2105   1.1        ad 	mc->mc_status = MLX_STATUS_WEDGED;
   2106   1.1        ad 	return (EIO);
   2107   1.1        ad }
   2108   1.1        ad 
   2109   1.1        ad /*
   2110   1.1        ad  * Return a string that describes why a command has failed.
   2111   1.1        ad  */
   2112   1.1        ad const char *
   2113   1.1        ad mlx_ccb_diagnose(struct mlx_ccb *mc)
   2114   1.1        ad {
   2115  1.33  christos 	static char tbuf[80];
   2116   1.1        ad 	int i;
   2117   1.1        ad 
   2118   1.1        ad 	for (i = 0; i < sizeof(mlx_msgs) / sizeof(mlx_msgs[0]); i++)
   2119   1.1        ad 		if ((mc->mc_mbox[0] == mlx_msgs[i].command ||
   2120   1.1        ad 		    mlx_msgs[i].command == 0) &&
   2121   1.1        ad 		    mc->mc_status == mlx_msgs[i].status) {
   2122  1.33  christos 			snprintf(tbuf, sizeof(tbuf), "%s (0x%x)",
   2123   1.1        ad 			    mlx_status_msgs[mlx_msgs[i].msg], mc->mc_status);
   2124  1.33  christos 			return (tbuf);
   2125   1.1        ad 		}
   2126   1.1        ad 
   2127  1.33  christos 	snprintf(tbuf, sizeof(tbuf), "unknown response 0x%x for command 0x%x",
   2128   1.1        ad 	    (int)mc->mc_status, (int)mc->mc_mbox[0]);
   2129   1.1        ad 
   2130  1.33  christos 	return (tbuf);
   2131   1.1        ad }
   2132   1.1        ad 
   2133   1.1        ad /*
   2134   1.1        ad  * Poll the controller for completed commands.  Returns non-zero if one or
   2135   1.1        ad  * more commands were completed.  Must be called with interrupts blocked.
   2136   1.1        ad  */
   2137   1.1        ad int
   2138   1.1        ad mlx_intr(void *cookie)
   2139   1.1        ad {
   2140   1.1        ad 	struct mlx_softc *mlx;
   2141   1.1        ad 	struct mlx_ccb *mc;
   2142   1.1        ad 	int result;
   2143   1.1        ad 	u_int ident, status;
   2144   1.1        ad 
   2145   1.1        ad 	mlx = cookie;
   2146   1.1        ad 	result = 0;
   2147   1.1        ad 
   2148   1.1        ad 	while ((*mlx->mlx_findcomplete)(mlx, &ident, &status) != 0) {
   2149   1.1        ad 		result = 1;
   2150  1.21        ad 		ident--;
   2151   1.1        ad 
   2152   1.1        ad 		if (ident >= MLX_MAX_QUEUECNT) {
   2153   1.1        ad 			printf("%s: bad completion returned\n",
   2154   1.1        ad 			    mlx->mlx_dv.dv_xname);
   2155   1.1        ad 			continue;
   2156   1.1        ad 		}
   2157   1.1        ad 
   2158   1.1        ad 		mc = mlx->mlx_ccbs + ident;
   2159   1.1        ad 
   2160   1.1        ad 		if (mc->mc_status != MLX_STATUS_BUSY) {
   2161   1.1        ad 			printf("%s: bad completion returned\n",
   2162   1.1        ad 			    mlx->mlx_dv.dv_xname);
   2163   1.1        ad 			continue;
   2164   1.1        ad 		}
   2165   1.1        ad 
   2166  1.32     perry 		TAILQ_REMOVE(&mlx->mlx_ccb_worklist, mc, mc_chain.tailq);
   2167   1.1        ad 
   2168   1.1        ad 		/* Record status and notify the initiator, if requested. */
   2169   1.1        ad 		mc->mc_status = status;
   2170   1.1        ad 		if (mc->mc_mx.mx_handler != NULL)
   2171   1.1        ad 			(*mc->mc_mx.mx_handler)(mc);
   2172   1.1        ad 		else if ((mc->mc_flags & MC_WAITING) != 0)
   2173   1.1        ad 			wakeup(mc);
   2174   1.1        ad 	}
   2175   1.1        ad 
   2176   1.1        ad 	/* If we've completed any commands, try posting some more. */
   2177   1.1        ad 	if (result)
   2178   1.1        ad 		mlx_ccb_enqueue(mlx, NULL);
   2179   1.1        ad 
   2180   1.1        ad 	return (result);
   2181   1.1        ad }
   2182   1.1        ad 
   2183   1.1        ad /*
   2184   1.1        ad  * Emit a string describing the firmware handshake status code, and return a
   2185   1.1        ad  * flag indicating whether the code represents a fatal error.
   2186   1.1        ad  *
   2187   1.1        ad  * Error code interpretations are from the Linux driver, and don't directly
   2188   1.1        ad  * match the messages printed by Mylex's BIOS.  This may change if
   2189   1.1        ad  * documentation on the codes is forthcoming.
   2190   1.1        ad  */
   2191   1.1        ad static int
   2192   1.1        ad mlx_fw_message(struct mlx_softc *mlx, int error, int param1, int param2)
   2193   1.1        ad {
   2194   1.1        ad 	const char *fmt;
   2195   1.1        ad 
   2196   1.1        ad 	switch (error) {
   2197   1.1        ad 	case 0x00:
   2198   1.1        ad 		fmt = "physical drive %d:%d not responding";
   2199   1.1        ad 		break;
   2200   1.1        ad 
   2201   1.1        ad 	case 0x08:
   2202   1.1        ad 		/*
   2203   1.1        ad 		 * We could be neater about this and give some indication
   2204   1.1        ad 		 * when we receive more of them.
   2205   1.1        ad 		 */
   2206   1.1        ad 		if ((mlx->mlx_flags & MLXF_SPINUP_REPORTED) == 0) {
   2207   1.1        ad 			printf("%s: spinning up drives...\n",
   2208   1.1        ad 			    mlx->mlx_dv.dv_xname);
   2209   1.1        ad 			mlx->mlx_flags |= MLXF_SPINUP_REPORTED;
   2210   1.1        ad 		}
   2211  1.23  kristerw 		return (0);
   2212   1.1        ad 
   2213   1.1        ad 	case 0x30:
   2214   1.1        ad 		fmt = "configuration checksum error";
   2215   1.1        ad 		break;
   2216   1.1        ad 
   2217   1.1        ad 	case 0x60:
   2218   1.1        ad 		fmt = "mirror race recovery failed";
   2219   1.1        ad 		break;
   2220   1.1        ad 
   2221   1.1        ad 	case 0x70:
   2222   1.1        ad 		fmt = "mirror race recovery in progress";
   2223   1.1        ad 		break;
   2224   1.1        ad 
   2225   1.1        ad 	case 0x90:
   2226   1.1        ad 		fmt = "physical drive %d:%d COD mismatch";
   2227   1.1        ad 		break;
   2228   1.1        ad 
   2229   1.1        ad 	case 0xa0:
   2230   1.1        ad 		fmt = "logical drive installation aborted";
   2231   1.1        ad 		break;
   2232   1.1        ad 
   2233   1.1        ad 	case 0xb0:
   2234   1.1        ad 		fmt = "mirror race on a critical system drive";
   2235   1.1        ad 		break;
   2236   1.1        ad 
   2237   1.1        ad 	case 0xd0:
   2238   1.1        ad 		fmt = "new controller configuration found";
   2239   1.1        ad 		break;
   2240   1.1        ad 
   2241   1.1        ad 	case 0xf0:
   2242  1.23  kristerw 		printf("%s: FATAL MEMORY PARITY ERROR\n",
   2243  1.23  kristerw 		    mlx->mlx_dv.dv_xname);
   2244   1.1        ad 		return (1);
   2245   1.1        ad 
   2246   1.1        ad 	default:
   2247   1.1        ad 		printf("%s: unknown firmware init error %02x:%02x:%02x\n",
   2248   1.1        ad 		    mlx->mlx_dv.dv_xname, error, param1, param2);
   2249   1.1        ad 		return (0);
   2250   1.1        ad 	}
   2251   1.1        ad 
   2252   1.1        ad 	printf("%s: ", mlx->mlx_dv.dv_xname);
   2253   1.1        ad 	printf(fmt, param2, param1);
   2254   1.1        ad 	printf("\n");
   2255   1.1        ad 
   2256   1.1        ad 	return (0);
   2257   1.1        ad }
   2258