Home | History | Annotate | Line # | Download | only in ic
isp_netbsd.c revision 1.18.2.5
      1  1.18.2.5  thorpej /* $NetBSD: isp_netbsd.c,v 1.18.2.5 1999/10/26 23:10:16 thorpej Exp $ */
      2       1.1   mjacob /*
      3       1.1   mjacob  * Platform (NetBSD) dependent common attachment code for Qlogic adapters.
      4      1.15   mjacob  * Matthew Jacob <mjacob (at) nas.nasa.gov>
      5      1.15   mjacob  */
      6      1.15   mjacob /*
      7      1.15   mjacob  * Copyright (C) 1997, 1998, 1999 National Aeronautics & Space Administration
      8       1.1   mjacob  * All rights reserved.
      9       1.1   mjacob  *
     10       1.1   mjacob  * Redistribution and use in source and binary forms, with or without
     11       1.1   mjacob  * modification, are permitted provided that the following conditions
     12       1.1   mjacob  * are met:
     13       1.1   mjacob  * 1. Redistributions of source code must retain the above copyright
     14      1.15   mjacob  *    notice, this list of conditions and the following disclaimer.
     15       1.1   mjacob  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1   mjacob  *    notice, this list of conditions and the following disclaimer in the
     17       1.1   mjacob  *    documentation and/or other materials provided with the distribution.
     18       1.1   mjacob  * 3. The name of the author may not be used to endorse or promote products
     19      1.15   mjacob  *    derived from this software without specific prior written permission
     20       1.1   mjacob  *
     21      1.15   mjacob  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22      1.15   mjacob  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23      1.15   mjacob  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24      1.15   mjacob  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25      1.15   mjacob  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26      1.15   mjacob  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27      1.15   mjacob  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28      1.15   mjacob  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29      1.15   mjacob  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30      1.15   mjacob  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31       1.1   mjacob  */
     32       1.1   mjacob 
     33       1.1   mjacob #include <dev/ic/isp_netbsd.h>
     34      1.17   mjacob #include <sys/scsiio.h>
     35       1.1   mjacob 
     36       1.1   mjacob static void ispminphys __P((struct buf *));
     37  1.18.2.1  thorpej static void isp_scsipi_request __P((struct scsipi_channel *,
     38  1.18.2.1  thorpej 	scsipi_adapter_req_t, void *));
     39      1.17   mjacob static int
     40  1.18.2.1  thorpej ispioctl __P((struct scsipi_channel *, u_long, caddr_t, int, struct proc *));
     41       1.1   mjacob 
     42       1.1   mjacob static int isp_poll __P((struct ispsoftc *, ISP_SCSI_XFER_T *, int));
     43      1.10   mjacob static void isp_watch __P((void *));
     44      1.17   mjacob static void isp_command_requeue __P((void *));
     45      1.10   mjacob 
     46       1.1   mjacob /*
     47       1.1   mjacob  * Complete attachment of hardware, include subdevices.
     48       1.1   mjacob  */
     49       1.1   mjacob void
     50       1.1   mjacob isp_attach(isp)
     51       1.1   mjacob 	struct ispsoftc *isp;
     52       1.1   mjacob {
     53  1.18.2.1  thorpej 	struct scsipi_adapter *adapt = &isp->isp_osinfo._adapter;
     54  1.18.2.1  thorpej 	struct scsipi_channel *chan;
     55  1.18.2.1  thorpej 	int i;
     56       1.6  thorpej 
     57       1.1   mjacob 	isp->isp_state = ISP_RUNSTATE;
     58       1.1   mjacob 
     59  1.18.2.1  thorpej 	/*
     60  1.18.2.1  thorpej 	 * Fill in the scsipi_adapter.
     61  1.18.2.1  thorpej 	 */
     62  1.18.2.1  thorpej 	adapt->adapt_dev = &isp->isp_osinfo._dev;
     63  1.18.2.1  thorpej 	if (IS_FC(isp) == 0 && IS_12X0(isp) != 0)
     64  1.18.2.1  thorpej 		adapt->adapt_nchannels = 2;
     65  1.18.2.1  thorpej 	else
     66  1.18.2.1  thorpej 		adapt->adapt_nchannels = 1;
     67  1.18.2.1  thorpej 	adapt->adapt_openings = isp->isp_maxcmds;
     68  1.18.2.1  thorpej 	adapt->adapt_max_periph = adapt->adapt_openings;
     69  1.18.2.1  thorpej 	adapt->adapt_request = isp_scsipi_request;
     70  1.18.2.1  thorpej 	adapt->adapt_minphys = ispminphys;
     71  1.18.2.1  thorpej 	adapt->adapt_ioctl = ispioctl;
     72  1.18.2.1  thorpej 
     73  1.18.2.1  thorpej 	/*
     74  1.18.2.1  thorpej 	 * Fill in the scsipi_channel(s).
     75  1.18.2.1  thorpej 	 */
     76  1.18.2.1  thorpej 	for (i = 0; i < adapt->adapt_nchannels; i++) {
     77  1.18.2.1  thorpej 		chan = &isp->isp_osinfo._channels[i];
     78  1.18.2.1  thorpej 		chan->chan_adapter = adapt;
     79  1.18.2.1  thorpej 		chan->chan_bustype = &scsi_bustype;
     80  1.18.2.1  thorpej 		chan->chan_channel = i;
     81  1.18.2.1  thorpej 
     82  1.18.2.1  thorpej 		if (IS_FC(isp)) {
     83  1.18.2.1  thorpej 			fcparam *fcp = isp->isp_param;
     84  1.18.2.1  thorpej 			fcp = &fcp[i];
     85  1.18.2.1  thorpej 
     86  1.18.2.1  thorpej 			/*
     87  1.18.2.1  thorpej 			 * Give it another chance here to come alive...
     88  1.18.2.1  thorpej 			 */
     89  1.18.2.1  thorpej 			if (fcp->isp_fwstate != FW_READY) {
     90  1.18.2.1  thorpej 				(void) isp_control(isp, ISPCTL_FCLINK_TEST,
     91  1.18.2.1  thorpej 				    NULL);
     92  1.18.2.1  thorpej 			}
     93  1.18.2.1  thorpej 			chan->chan_ntargets = MAX_FC_TARG;
     94      1.11   mjacob #ifdef	ISP2100_SCCLUN
     95  1.18.2.1  thorpej 			/*
     96  1.18.2.1  thorpej 			 * 16 bits worth, but let's be reasonable..
     97  1.18.2.1  thorpej 			 */
     98  1.18.2.1  thorpej 			chan->chan_nluns = 256;
     99       1.7   mjacob #else
    100  1.18.2.1  thorpej 			chan->chan_nluns = 16;
    101       1.7   mjacob #endif
    102  1.18.2.1  thorpej 			chan->chan_id = fcp->isp_loopid;
    103       1.7   mjacob 		} else {
    104  1.18.2.1  thorpej 			sdparam *sdp = isp->isp_param;
    105  1.18.2.1  thorpej 			sdp = &sdp[i];
    106  1.18.2.1  thorpej 
    107  1.18.2.1  thorpej 			chan->chan_ntargets = MAX_TARGETS;
    108  1.18.2.1  thorpej 			if (isp->isp_bustype == ISP_BT_SBUS)
    109  1.18.2.1  thorpej 				chan->chan_nluns = 8;
    110  1.18.2.1  thorpej 			else {
    111  1.18.2.1  thorpej #if 0
    112  1.18.2.1  thorpej 				/* Too many broken targets... */
    113  1.18.2.1  thorpej 				if (isp->isp_fwrev >= ISP_FW_REV(7,55,0))
    114  1.18.2.1  thorpej 					chan->chan_nluns = 32;
    115  1.18.2.1  thorpej 				else
    116       1.7   mjacob #endif
    117  1.18.2.1  thorpej 					chan->chan_nluns = 7;
    118  1.18.2.1  thorpej 			}
    119  1.18.2.1  thorpej 			chan->chan_id = sdp->isp_initiator_id;
    120      1.14   mjacob 		}
    121       1.1   mjacob 	}
    122       1.8   mjacob 
    123       1.8   mjacob 	/*
    124      1.10   mjacob 	 * Send a SCSI Bus Reset (used to be done as part of attach,
    125      1.10   mjacob 	 * but now left to the OS outer layers).
    126      1.10   mjacob 	 */
    127      1.14   mjacob 	if (IS_SCSI(isp)) {
    128  1.18.2.1  thorpej 		for (i = 0; i < adapt->adapt_nchannels; i++)
    129  1.18.2.1  thorpej 			(void) isp_control(isp, ISPCTL_RESET_BUS, &i);
    130      1.11   mjacob 		SYS_DELAY(2*1000000);
    131      1.11   mjacob 	}
    132      1.10   mjacob 
    133      1.10   mjacob 	/*
    134       1.8   mjacob 	 * Start the watchdog.
    135       1.8   mjacob 	 */
    136       1.8   mjacob 	isp->isp_dogactive = 1;
    137      1.17   mjacob 	timeout(isp_watch, isp, WATCH_INTERVAL * hz);
    138       1.8   mjacob 
    139       1.8   mjacob 	/*
    140       1.8   mjacob 	 * And attach children (if any).
    141       1.8   mjacob 	 */
    142  1.18.2.1  thorpej 	for (i = 0; i < adapt->adapt_nchannels; i++)
    143  1.18.2.1  thorpej 		(void) config_found((void *)isp, &isp->isp_osinfo._channels[i],
    144  1.18.2.1  thorpej 		    scsiprint);
    145       1.1   mjacob }
    146       1.1   mjacob 
    147       1.1   mjacob /*
    148       1.1   mjacob  * minphys our xfers
    149       1.1   mjacob  *
    150       1.1   mjacob  * Unfortunately, the buffer pointer describes the target device- not the
    151       1.1   mjacob  * adapter device, so we can't use the pointer to find out what kind of
    152       1.1   mjacob  * adapter we are and adjust accordingly.
    153       1.1   mjacob  */
    154       1.1   mjacob 
    155       1.1   mjacob static void
    156       1.1   mjacob ispminphys(bp)
    157       1.1   mjacob 	struct buf *bp;
    158       1.1   mjacob {
    159       1.1   mjacob 	/*
    160       1.1   mjacob 	 * XX: Only the 1020 has a 24 bit limit.
    161       1.1   mjacob 	 */
    162       1.1   mjacob 	if (bp->b_bcount >= (1 << 24)) {
    163       1.1   mjacob 		bp->b_bcount = (1 << 24);
    164       1.1   mjacob 	}
    165       1.1   mjacob 	minphys(bp);
    166       1.1   mjacob }
    167       1.1   mjacob 
    168      1.17   mjacob static int
    169  1.18.2.1  thorpej ispioctl(chan, cmd, addr, flag, p)
    170  1.18.2.1  thorpej 	struct scsipi_channel *chan;
    171      1.17   mjacob 	u_long cmd;
    172      1.17   mjacob 	caddr_t addr;
    173      1.17   mjacob 	int flag;
    174      1.17   mjacob 	struct proc *p;
    175      1.17   mjacob {
    176  1.18.2.1  thorpej 	struct ispsoftc *isp = (void *)chan->chan_adapter->adapt_dev;
    177  1.18.2.1  thorpej 	int s, ch, retval = ENOTTY;
    178      1.17   mjacob 
    179      1.17   mjacob 	switch (cmd) {
    180      1.17   mjacob 	case SCBUSIORESET:
    181  1.18.2.1  thorpej 		ch = chan->chan_channel;
    182      1.17   mjacob 		s = splbio();
    183  1.18.2.1  thorpej 		if (isp_control(isp, ISPCTL_RESET_BUS, &ch))
    184      1.17   mjacob 			retval = EIO;
    185      1.17   mjacob 		else
    186      1.17   mjacob 			retval = 0;
    187      1.17   mjacob 		(void) splx(s);
    188      1.17   mjacob 		break;
    189      1.17   mjacob 	default:
    190      1.17   mjacob 		break;
    191       1.3   mjacob 	}
    192      1.17   mjacob 	return (retval);
    193      1.17   mjacob }
    194      1.17   mjacob 
    195  1.18.2.1  thorpej static void
    196  1.18.2.1  thorpej isp_scsipi_request(chan, req, arg)
    197  1.18.2.1  thorpej 	struct scsipi_channel *chan;
    198  1.18.2.1  thorpej 	scsipi_adapter_req_t req;
    199  1.18.2.1  thorpej 	void *arg;
    200      1.17   mjacob {
    201  1.18.2.1  thorpej 	struct scsipi_xfer *xs;
    202  1.18.2.1  thorpej 	struct ispsoftc *isp = (void *)chan->chan_adapter->adapt_dev;
    203      1.17   mjacob 	int result, s;
    204      1.11   mjacob 
    205  1.18.2.1  thorpej 	switch (req) {
    206  1.18.2.1  thorpej 	case ADAPTER_REQ_RUN_XFER:
    207  1.18.2.2  thorpej 		xs = arg;
    208  1.18.2.1  thorpej 		s = splbio();
    209  1.18.2.1  thorpej 		if (isp->isp_state < ISP_RUNSTATE) {
    210  1.18.2.1  thorpej 			DISABLE_INTS(isp);
    211  1.18.2.1  thorpej 			isp_init(isp);
    212  1.18.2.1  thorpej 			if (isp->isp_state != ISP_INITSTATE) {
    213  1.18.2.1  thorpej 				ENABLE_INTS(isp);
    214  1.18.2.1  thorpej 				(void) splx(s);
    215  1.18.2.1  thorpej 				XS_SETERR(xs, HBA_BOTCH);
    216  1.18.2.1  thorpej 				XS_CMD_DONE(xs);
    217  1.18.2.1  thorpej 				return;
    218  1.18.2.1  thorpej 			}
    219  1.18.2.1  thorpej 			isp->isp_state = ISP_RUNSTATE;
    220      1.11   mjacob 			ENABLE_INTS(isp);
    221  1.18.2.1  thorpej 		}
    222      1.11   mjacob 
    223  1.18.2.1  thorpej 		DISABLE_INTS(isp);
    224  1.18.2.1  thorpej 		result = ispscsicmd(xs);
    225  1.18.2.1  thorpej 		ENABLE_INTS(isp);
    226  1.18.2.1  thorpej 
    227      1.17   mjacob 		switch (result) {
    228      1.17   mjacob 		case CMD_QUEUED:
    229  1.18.2.1  thorpej 			/*
    230  1.18.2.1  thorpej 			 * If we're not polling for completion, just return.
    231  1.18.2.1  thorpej 			 */
    232  1.18.2.1  thorpej 			if ((xs->xs_control & XS_CTL_POLL) == 0) {
    233  1.18.2.1  thorpej 				(void) splx(s);
    234  1.18.2.1  thorpej 				return;
    235  1.18.2.1  thorpej 			}
    236      1.17   mjacob 			break;
    237  1.18.2.1  thorpej 
    238      1.17   mjacob 		case CMD_EAGAIN:
    239  1.18.2.1  thorpej 			/*
    240  1.18.2.1  thorpej 			 * Adapter resource shortage of some sort.  Should
    241  1.18.2.1  thorpej 			 * retry later.
    242  1.18.2.1  thorpej 			 */
    243  1.18.2.1  thorpej 			XS_SETERR(xs, XS_RESOURCE_SHORTAGE);
    244  1.18.2.1  thorpej 			XS_CMD_DONE(xs);
    245  1.18.2.1  thorpej 			(void) splx(s);
    246  1.18.2.1  thorpej 			return;
    247  1.18.2.1  thorpej 
    248      1.17   mjacob 		case CMD_RQLATER:
    249  1.18.2.1  thorpej 			/*
    250  1.18.2.1  thorpej 			 * XXX I think what we should do here is freeze
    251  1.18.2.1  thorpej 			 * XXX the channel queue, and do a timed thaw
    252  1.18.2.1  thorpej 			 * XXX of it.  Need to add this to the mid-layer.
    253  1.18.2.1  thorpej 			 */
    254  1.18.2.1  thorpej 			if ((xs->xs_control & XS_CTL_POLL) != 0) {
    255  1.18.2.1  thorpej 				XS_SETERR(xs, XS_DRIVER_STUFFUP);
    256  1.18.2.1  thorpej 				XS_CMD_DONE(xs);
    257  1.18.2.1  thorpej 			} else
    258  1.18.2.1  thorpej 				timeout(isp_command_requeue, xs, hz);
    259  1.18.2.1  thorpej 			(void) splx(s);
    260  1.18.2.1  thorpej 			return;
    261  1.18.2.1  thorpej 
    262      1.17   mjacob 		case CMD_COMPLETE:
    263  1.18.2.1  thorpej 			/*
    264  1.18.2.1  thorpej 			 * Something went horribly wrong, xs->error is set,
    265  1.18.2.1  thorpej 			 * and we just need to finish it off.
    266  1.18.2.1  thorpej 			 */
    267  1.18.2.1  thorpej 			XS_CMD_DONE(xs);
    268  1.18.2.1  thorpej 			(void) splx(s);
    269  1.18.2.1  thorpej 			return;
    270      1.17   mjacob 		}
    271       1.1   mjacob 
    272  1.18.2.1  thorpej 		/*
    273  1.18.2.1  thorpej 		 * If we can't use interrupts, poll on completion.
    274  1.18.2.1  thorpej 		 */
    275      1.17   mjacob 		if (isp_poll(isp, xs, XS_TIME(xs))) {
    276      1.17   mjacob 			/*
    277      1.17   mjacob 			 * If no other error occurred but we didn't finish,
    278      1.17   mjacob 			 * something bad happened.
    279      1.17   mjacob 			 */
    280      1.17   mjacob 			if (XS_IS_CMD_DONE(xs) == 0) {
    281      1.17   mjacob 				if (isp_control(isp, ISPCTL_ABORT_CMD, xs)) {
    282      1.17   mjacob 					isp_restart(isp);
    283      1.17   mjacob 				}
    284      1.17   mjacob 				if (XS_NOERR(xs)) {
    285      1.17   mjacob 					XS_SETERR(xs, HBA_BOTCH);
    286      1.17   mjacob 				}
    287  1.18.2.1  thorpej 				XS_CMD_DONE(xs);
    288       1.1   mjacob 			}
    289       1.1   mjacob 		}
    290  1.18.2.1  thorpej 		(void) splx(s);
    291  1.18.2.1  thorpej 		return;
    292  1.18.2.1  thorpej 
    293  1.18.2.1  thorpej 	case ADAPTER_REQ_GROW_RESOURCES:
    294  1.18.2.1  thorpej 		/* XXX Not supported. */
    295  1.18.2.1  thorpej 		return;
    296  1.18.2.1  thorpej 
    297  1.18.2.1  thorpej 	case ADAPTER_REQ_SET_XFER_MODE:
    298  1.18.2.1  thorpej 		if (isp->isp_type & ISP_HA_SCSI) {
    299  1.18.2.1  thorpej 			sdparam *sdp = isp->isp_param;
    300  1.18.2.5  thorpej 			struct scsipi_xfer_mode *xm = arg;
    301  1.18.2.1  thorpej 			u_int16_t flags;
    302  1.18.2.1  thorpej 
    303  1.18.2.5  thorpej 			sdp = &sdp[chan->chan_channel];
    304  1.18.2.1  thorpej 
    305  1.18.2.1  thorpej 			flags =
    306  1.18.2.5  thorpej 			    sdp->isp_devparam[xm->xm_target].dev_flags;
    307  1.18.2.1  thorpej 			flags &= ~(DPARM_WIDE|DPARM_SYNC|DPARM_TQING);
    308  1.18.2.1  thorpej 
    309  1.18.2.5  thorpej 			if (xm->xm_mode & PERIPH_CAP_SYNC)
    310  1.18.2.1  thorpej 				flags |= DPARM_SYNC;
    311  1.18.2.1  thorpej 
    312  1.18.2.5  thorpej 			if (xm->xm_mode & PERIPH_CAP_WIDE16)
    313  1.18.2.1  thorpej 				flags |= DPARM_WIDE;
    314  1.18.2.1  thorpej 
    315  1.18.2.5  thorpej 			if (xm->xm_mode & PERIPH_CAP_TQING)
    316  1.18.2.1  thorpej 				flags |= DPARM_TQING;
    317  1.18.2.1  thorpej 
    318  1.18.2.5  thorpej 			sdp->isp_devparam[xm->xm_target].dev_flags =
    319  1.18.2.1  thorpej 			    flags;
    320  1.18.2.5  thorpej 			sdp->isp_devparam[xm->xm_target].dev_update = 1;
    321  1.18.2.5  thorpej 			isp->isp_update |= (1 << chan->chan_channel);
    322  1.18.2.1  thorpej 			(void) isp_control(isp, ISPCTL_UPDATE_PARAMS, NULL);
    323  1.18.2.1  thorpej 		}
    324  1.18.2.1  thorpej 		return;
    325       1.1   mjacob 	}
    326       1.1   mjacob }
    327       1.1   mjacob 
    328       1.1   mjacob static int
    329       1.1   mjacob isp_poll(isp, xs, mswait)
    330       1.1   mjacob 	struct ispsoftc *isp;
    331       1.1   mjacob 	ISP_SCSI_XFER_T *xs;
    332       1.1   mjacob 	int mswait;
    333       1.1   mjacob {
    334       1.1   mjacob 
    335       1.1   mjacob 	while (mswait) {
    336       1.1   mjacob 		/* Try the interrupt handling routine */
    337       1.1   mjacob 		(void)isp_intr((void *)isp);
    338       1.1   mjacob 
    339       1.1   mjacob 		/* See if the xs is now done */
    340       1.1   mjacob 		if (XS_IS_CMD_DONE(xs)) {
    341       1.1   mjacob 			return (0);
    342       1.1   mjacob 		}
    343       1.1   mjacob 		SYS_DELAY(1000);	/* wait one millisecond */
    344       1.1   mjacob 		mswait--;
    345       1.1   mjacob 	}
    346       1.1   mjacob 	return (1);
    347       1.8   mjacob }
    348       1.8   mjacob 
    349       1.8   mjacob static void
    350       1.8   mjacob isp_watch(arg)
    351       1.8   mjacob 	void *arg;
    352       1.8   mjacob {
    353       1.8   mjacob 	int i;
    354       1.8   mjacob 	struct ispsoftc *isp = arg;
    355      1.17   mjacob 	struct scsipi_xfer *xs;
    356      1.17   mjacob 	int s;
    357       1.8   mjacob 
    358       1.8   mjacob 	/*
    359       1.8   mjacob 	 * Look for completely dead commands (but not polled ones).
    360       1.8   mjacob 	 */
    361      1.17   mjacob 	s = splbio();
    362      1.17   mjacob 	for (i = 0; i < isp->isp_maxcmds; i++) {
    363      1.17   mjacob 		xs = isp->isp_xflist[i];
    364      1.17   mjacob 		if (xs == NULL) {
    365       1.8   mjacob 			continue;
    366       1.8   mjacob 		}
    367      1.17   mjacob 		if (xs->timeout == 0 || (xs->xs_control & XS_CTL_POLL)) {
    368       1.8   mjacob 			continue;
    369       1.8   mjacob 		}
    370      1.17   mjacob 		xs->timeout -= (WATCH_INTERVAL * 1000);
    371       1.8   mjacob 		/*
    372       1.8   mjacob 		 * Avoid later thinking that this
    373       1.8   mjacob 		 * transaction is not being timed.
    374       1.8   mjacob 		 * Then give ourselves to watchdog
    375       1.8   mjacob 		 * periods of grace.
    376       1.8   mjacob 		 */
    377      1.17   mjacob 		if (xs->timeout == 0) {
    378      1.17   mjacob 			xs->timeout = 1;
    379      1.17   mjacob 		} else if (xs->timeout > -(2 * WATCH_INTERVAL * 1000)) {
    380       1.8   mjacob 			continue;
    381       1.8   mjacob 		}
    382       1.8   mjacob 		if (isp_control(isp, ISPCTL_ABORT_CMD, xs)) {
    383       1.8   mjacob 			printf("%s: isp_watch failed to abort command\n",
    384       1.8   mjacob 			    isp->isp_name);
    385       1.8   mjacob 			isp_restart(isp);
    386       1.8   mjacob 			break;
    387       1.8   mjacob 		}
    388       1.8   mjacob 	}
    389       1.8   mjacob 	timeout(isp_watch, isp, WATCH_INTERVAL * hz);
    390       1.8   mjacob 	isp->isp_dogactive = 1;
    391      1.17   mjacob 	(void) splx(s);
    392       1.8   mjacob }
    393       1.8   mjacob 
    394       1.8   mjacob /*
    395       1.8   mjacob  * Free any associated resources prior to decommissioning and
    396       1.8   mjacob  * set the card to a known state (so it doesn't wake up and kick
    397       1.8   mjacob  * us when we aren't expecting it to).
    398       1.8   mjacob  *
    399       1.8   mjacob  * Locks are held before coming here.
    400       1.8   mjacob  */
    401       1.8   mjacob void
    402       1.8   mjacob isp_uninit(isp)
    403       1.8   mjacob 	struct ispsoftc *isp;
    404       1.8   mjacob {
    405       1.8   mjacob 	ISP_ILOCKVAL_DECL;
    406       1.8   mjacob 	ISP_ILOCK(isp);
    407       1.8   mjacob 	/*
    408       1.8   mjacob 	 * Leave with interrupts disabled.
    409       1.8   mjacob 	 */
    410       1.8   mjacob 	DISABLE_INTS(isp);
    411       1.8   mjacob 
    412       1.8   mjacob 	/*
    413       1.8   mjacob 	 * Turn off the watchdog (if active).
    414       1.8   mjacob 	 */
    415       1.8   mjacob 	if (isp->isp_dogactive) {
    416       1.8   mjacob 		untimeout(isp_watch, isp);
    417       1.8   mjacob 		isp->isp_dogactive = 0;
    418       1.8   mjacob 	}
    419       1.8   mjacob 
    420       1.8   mjacob 	ISP_IUNLOCK(isp);
    421       1.9   mjacob }
    422       1.9   mjacob 
    423      1.10   mjacob /*
    424      1.17   mjacob  * Restart function for a command to be requeued later.
    425      1.17   mjacob  */
    426      1.17   mjacob static void
    427      1.17   mjacob isp_command_requeue(arg)
    428      1.17   mjacob 	void *arg;
    429      1.17   mjacob {
    430      1.17   mjacob 	struct scsipi_xfer *xs = arg;
    431  1.18.2.1  thorpej 	int s;
    432  1.18.2.1  thorpej 
    433  1.18.2.1  thorpej 	s = splbio();
    434  1.18.2.1  thorpej 	scsipi_adapter_request(xs->xs_periph->periph_channel,
    435  1.18.2.1  thorpej 	    ADAPTER_REQ_RUN_XFER, xs);
    436      1.17   mjacob 	(void) splx(s);
    437      1.17   mjacob }
    438      1.18   mjacob 
    439       1.9   mjacob int
    440       1.9   mjacob isp_async(isp, cmd, arg)
    441       1.9   mjacob 	struct ispsoftc *isp;
    442       1.9   mjacob 	ispasync_t cmd;
    443       1.9   mjacob 	void *arg;
    444       1.9   mjacob {
    445  1.18.2.3  thorpej 	int bus, tgt;
    446      1.10   mjacob 	int s = splbio();
    447       1.9   mjacob 	switch (cmd) {
    448  1.18.2.5  thorpej 	case ISPASYNC_NEW_TGT_PARAMS:
    449  1.18.2.5  thorpej 	if (isp->isp_type & ISP_HA_SCSI) {
    450  1.18.2.3  thorpej 		struct scsipi_xfer_mode xm;
    451  1.18.2.5  thorpej 		sdparam *sdp = isp->isp_param;
    452  1.18.2.5  thorpej 		u_int16_t flags;
    453  1.18.2.3  thorpej 
    454  1.18.2.3  thorpej 		tgt = *((int *) arg);
    455  1.18.2.3  thorpej 		bus = (tgt >> 16) & 0xffff;
    456  1.18.2.3  thorpej 		tgt &= 0xffff;
    457  1.18.2.3  thorpej 
    458  1.18.2.5  thorpej 		sdp = &sdp[bus];
    459  1.18.2.5  thorpej 
    460  1.18.2.3  thorpej 		xm.xm_target = tgt;
    461  1.18.2.5  thorpej 		xm.xm_mode = 0;
    462  1.18.2.5  thorpej 		xm.xm_period = 0;
    463  1.18.2.5  thorpej 		xm.xm_offset = 0;
    464  1.18.2.5  thorpej 
    465  1.18.2.5  thorpej 		flags = sdp->isp_devparam[tgt].cur_dflags;
    466  1.18.2.5  thorpej 
    467  1.18.2.5  thorpej 		if (flags & DPARM_SYNC) {
    468  1.18.2.5  thorpej 			xm.xm_mode |= PERIPH_CAP_SYNC;
    469  1.18.2.5  thorpej 			xm.xm_period = sdp->isp_devparam[tgt].cur_period;
    470  1.18.2.5  thorpej 			xm.xm_offset = sdp->isp_devparam[tgt].cur_offset;
    471  1.18.2.5  thorpej 		}
    472  1.18.2.5  thorpej 		if (flags & DPARM_WIDE)
    473  1.18.2.5  thorpej 			xm.xm_mode |= PERIPH_CAP_WIDE16;
    474  1.18.2.5  thorpej 		if (flags & DPARM_TQING)
    475  1.18.2.5  thorpej 			xm.xm_mode |= PERIPH_CAP_TQING;
    476  1.18.2.5  thorpej 
    477  1.18.2.3  thorpej 		scsipi_async_event(&isp->isp_osinfo._channels[bus],
    478  1.18.2.3  thorpej 		    ASYNC_EVENT_XFER_MODE, &xm);
    479      1.11   mjacob 		break;
    480  1.18.2.3  thorpej 	}
    481      1.11   mjacob 	case ISPASYNC_BUS_RESET:
    482      1.14   mjacob 		if (arg)
    483      1.14   mjacob 			bus = *((int *) arg);
    484      1.14   mjacob 		else
    485      1.14   mjacob 			bus = 0;
    486      1.14   mjacob 		printf("%s: SCSI bus %d reset detected\n", isp->isp_name, bus);
    487      1.11   mjacob 		break;
    488      1.11   mjacob 	case ISPASYNC_LOOP_DOWN:
    489      1.11   mjacob 		/*
    490      1.11   mjacob 		 * Hopefully we get here in time to minimize the number
    491      1.11   mjacob 		 * of commands we are firing off that are sure to die.
    492  1.18.2.4  thorpej 		 *
    493  1.18.2.4  thorpej 		 * XXX Hard-code channel 0, but only one channel on FC
    494  1.18.2.4  thorpej 		 * XXX adapters, right?
    495      1.11   mjacob 		 */
    496  1.18.2.4  thorpej 		scsipi_channel_freeze(&isp->isp_osinfo._channels[0], 1);
    497      1.11   mjacob 		printf("%s: Loop DOWN\n", isp->isp_name);
    498      1.11   mjacob 		break;
    499      1.11   mjacob         case ISPASYNC_LOOP_UP:
    500  1.18.2.4  thorpej 		/*
    501  1.18.2.4  thorpej 		 * XXX Hard-code channel 0, but only one channel on FC
    502  1.18.2.4  thorpej 		 * XXX adapters, right?
    503  1.18.2.4  thorpej 		 */
    504  1.18.2.4  thorpej 		timeout(scsipi_channel_timed_thaw,
    505  1.18.2.4  thorpej 		    &isp->isp_osinfo._channels[0], 1);
    506      1.11   mjacob 		printf("%s: Loop UP\n", isp->isp_name);
    507      1.11   mjacob 		break;
    508      1.15   mjacob 	case ISPASYNC_PDB_CHANGED:
    509      1.17   mjacob 	if (IS_FC(isp) && isp->isp_dblev) {
    510      1.15   mjacob 		const char *fmt = "%s: Target %d (Loop 0x%x) Port ID 0x%x "
    511      1.15   mjacob 		    "role %s %s\n Port WWN 0x%08x%08x\n Node WWN 0x%08x%08x\n";
    512      1.15   mjacob 		const static char *roles[4] = {
    513      1.11   mjacob 		    "No", "Target", "Initiator", "Target/Initiator"
    514      1.11   mjacob 		};
    515      1.15   mjacob 		char *ptr;
    516      1.15   mjacob 		fcparam *fcp = isp->isp_param;
    517      1.15   mjacob 		int tgt = *((int *) arg);
    518      1.15   mjacob 		struct lportdb *lp = &fcp->portdb[tgt];
    519      1.15   mjacob 
    520      1.15   mjacob 		if (lp->valid) {
    521      1.15   mjacob 			ptr = "arrived";
    522      1.15   mjacob 		} else {
    523      1.15   mjacob 			ptr = "disappeared";
    524      1.11   mjacob 		}
    525      1.15   mjacob 		printf(fmt, isp->isp_name, tgt, lp->loopid, lp->portid,
    526      1.15   mjacob 		    roles[lp->roles & 0x3], ptr,
    527      1.15   mjacob 		    (u_int32_t) (lp->port_wwn >> 32),
    528      1.15   mjacob 		    (u_int32_t) (lp->port_wwn & 0xffffffffLL),
    529      1.15   mjacob 		    (u_int32_t) (lp->node_wwn >> 32),
    530      1.15   mjacob 		    (u_int32_t) (lp->node_wwn & 0xffffffffLL));
    531      1.11   mjacob 		break;
    532      1.11   mjacob 	}
    533      1.15   mjacob #ifdef	ISP2100_FABRIC
    534      1.11   mjacob 	case ISPASYNC_CHANGE_NOTIFY:
    535      1.11   mjacob 		printf("%s: Name Server Database Changed\n", isp->isp_name);
    536       1.9   mjacob 		break;
    537      1.15   mjacob 	case ISPASYNC_FABRIC_DEV:
    538      1.15   mjacob 	{
    539      1.15   mjacob 		int target;
    540      1.15   mjacob 		struct lportdb *lp;
    541      1.15   mjacob 		sns_scrsp_t *resp = (sns_scrsp_t *) arg;
    542      1.15   mjacob 		u_int32_t portid;
    543      1.15   mjacob 		u_int64_t wwn;
    544      1.15   mjacob 		fcparam *fcp = isp->isp_param;
    545      1.15   mjacob 
    546      1.15   mjacob 		portid =
    547      1.15   mjacob 		    (((u_int32_t) resp->snscb_port_id[0]) << 16) |
    548      1.15   mjacob 		    (((u_int32_t) resp->snscb_port_id[1]) << 8) |
    549      1.15   mjacob 		    (((u_int32_t) resp->snscb_port_id[2]));
    550      1.15   mjacob 		wwn =
    551      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[0]) << 56) |
    552      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[1]) << 48) |
    553      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[2]) << 40) |
    554      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[3]) << 32) |
    555      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[4]) << 24) |
    556      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[5]) << 16) |
    557      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[6]) <<  8) |
    558      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[7]));
    559      1.15   mjacob 		printf("%s: Fabric Device (Type 0x%x)@PortID 0x%x WWN "
    560      1.15   mjacob 		    "0x%08x%08x\n", isp->isp_name, resp->snscb_port_type,
    561      1.15   mjacob 		    portid, ((u_int32_t)(wwn >> 32)),
    562      1.15   mjacob 		    ((u_int32_t)(wwn & 0xffffffff)));
    563      1.15   mjacob 		if (resp->snscb_port_type != 2)
    564      1.15   mjacob 			break;
    565      1.15   mjacob 		for (target = FC_SNS_ID+1; target < MAX_FC_TARG; target++) {
    566      1.15   mjacob 			lp = &fcp->portdb[target];
    567      1.15   mjacob 			if (lp->port_wwn == wwn)
    568      1.15   mjacob 				break;
    569      1.15   mjacob 		}
    570      1.15   mjacob 		if (target < MAX_FC_TARG) {
    571      1.15   mjacob 			break;
    572      1.15   mjacob 		}
    573      1.15   mjacob 		for (target = FC_SNS_ID+1; target < MAX_FC_TARG; target++) {
    574      1.15   mjacob 			lp = &fcp->portdb[target];
    575      1.15   mjacob 			if (lp->port_wwn == 0)
    576      1.15   mjacob 				break;
    577      1.15   mjacob 		}
    578      1.15   mjacob 		if (target == MAX_FC_TARG) {
    579      1.15   mjacob 			printf("%s: no more space for fabric devices\n",
    580      1.15   mjacob 			    isp->isp_name);
    581      1.15   mjacob 			return (-1);
    582      1.15   mjacob 		}
    583      1.15   mjacob 		lp->port_wwn = lp->node_wwn = wwn;
    584      1.15   mjacob 		lp->portid = portid;
    585      1.15   mjacob 		break;
    586      1.15   mjacob 	}
    587      1.15   mjacob #endif
    588       1.9   mjacob 	default:
    589       1.9   mjacob 		break;
    590       1.9   mjacob 	}
    591      1.10   mjacob 	(void) splx(s);
    592       1.9   mjacob 	return (0);
    593       1.1   mjacob }
    594