Home | History | Annotate | Line # | Download | only in ic
isp_netbsd.c revision 1.18.2.4
      1  1.18.2.4  thorpej /* $NetBSD: isp_netbsd.c,v 1.18.2.4 1999/10/20 20:41:27 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.1  thorpej 			struct scsipi_periph *periph = arg;
    301  1.18.2.1  thorpej 			u_int16_t flags;
    302  1.18.2.1  thorpej 
    303  1.18.2.1  thorpej 			sdp = &sdp[periph->periph_channel->chan_channel];
    304  1.18.2.1  thorpej 
    305  1.18.2.1  thorpej 			flags =
    306  1.18.2.1  thorpej 			    sdp->isp_devparam[periph->periph_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.1  thorpej 			if (periph->periph_cap & PERIPH_CAP_SYNC)
    310  1.18.2.1  thorpej 				flags |= DPARM_SYNC;
    311  1.18.2.1  thorpej 
    312  1.18.2.1  thorpej 			if (periph->periph_cap & PERIPH_CAP_WIDE16)
    313  1.18.2.1  thorpej 				flags |= DPARM_WIDE;
    314  1.18.2.1  thorpej 
    315  1.18.2.1  thorpej 			if (periph->periph_cap & PERIPH_CAP_TQING)
    316  1.18.2.1  thorpej 				flags |= DPARM_TQING;
    317  1.18.2.1  thorpej 
    318  1.18.2.1  thorpej 			sdp->isp_devparam[periph->periph_target].dev_flags =
    319  1.18.2.1  thorpej 			    flags;
    320  1.18.2.1  thorpej 			sdp->isp_devparam[periph->periph_target].dev_update = 1;
    321  1.18.2.1  thorpej 			isp->isp_update |=
    322  1.18.2.1  thorpej 			    (1 << periph->periph_channel->chan_channel);
    323  1.18.2.1  thorpej 			(void) isp_control(isp, ISPCTL_UPDATE_PARAMS, NULL);
    324  1.18.2.1  thorpej 		}
    325  1.18.2.1  thorpej 		return;
    326  1.18.2.1  thorpej 
    327  1.18.2.1  thorpej 	case ADAPTER_REQ_GET_XFER_MODE:
    328  1.18.2.1  thorpej 		if (isp->isp_type & ISP_HA_SCSI) {
    329  1.18.2.1  thorpej 			sdparam *sdp = isp->isp_param;
    330  1.18.2.1  thorpej 			struct scsipi_periph *periph = arg;
    331  1.18.2.1  thorpej 			u_int16_t flags;
    332  1.18.2.1  thorpej 
    333  1.18.2.1  thorpej 			sdp = &sdp[periph->periph_channel->chan_channel];
    334  1.18.2.1  thorpej 
    335  1.18.2.1  thorpej 			periph->periph_mode = 0;
    336  1.18.2.1  thorpej 			periph->periph_period = 0;
    337  1.18.2.1  thorpej 			periph->periph_offset = 0;
    338  1.18.2.1  thorpej 
    339  1.18.2.1  thorpej 			flags =
    340  1.18.2.1  thorpej 			    sdp->isp_devparam[periph->periph_target].cur_dflags;
    341  1.18.2.1  thorpej 
    342  1.18.2.1  thorpej 			if (flags & DPARM_SYNC) {
    343  1.18.2.1  thorpej 				periph->periph_mode |= PERIPH_CAP_SYNC;
    344  1.18.2.1  thorpej 				periph->periph_period =
    345  1.18.2.1  thorpej 			    sdp->isp_devparam[periph->periph_target].cur_period;
    346  1.18.2.1  thorpej 				periph->periph_offset =
    347  1.18.2.1  thorpej 			    sdp->isp_devparam[periph->periph_target].cur_offset;
    348  1.18.2.1  thorpej 			}
    349  1.18.2.1  thorpej 			if (flags & DPARM_WIDE)
    350  1.18.2.1  thorpej 				periph->periph_mode |= PERIPH_CAP_WIDE16;
    351  1.18.2.1  thorpej 			if (flags & DPARM_TQING)
    352  1.18.2.1  thorpej 				periph->periph_mode |= PERIPH_CAP_TQING;
    353  1.18.2.1  thorpej 
    354  1.18.2.1  thorpej 			periph->periph_flags |= PERIPH_MODE_VALID;
    355  1.18.2.1  thorpej 		}
    356  1.18.2.1  thorpej 		return;
    357       1.1   mjacob 	}
    358       1.1   mjacob }
    359       1.1   mjacob 
    360       1.1   mjacob static int
    361       1.1   mjacob isp_poll(isp, xs, mswait)
    362       1.1   mjacob 	struct ispsoftc *isp;
    363       1.1   mjacob 	ISP_SCSI_XFER_T *xs;
    364       1.1   mjacob 	int mswait;
    365       1.1   mjacob {
    366       1.1   mjacob 
    367       1.1   mjacob 	while (mswait) {
    368       1.1   mjacob 		/* Try the interrupt handling routine */
    369       1.1   mjacob 		(void)isp_intr((void *)isp);
    370       1.1   mjacob 
    371       1.1   mjacob 		/* See if the xs is now done */
    372       1.1   mjacob 		if (XS_IS_CMD_DONE(xs)) {
    373       1.1   mjacob 			return (0);
    374       1.1   mjacob 		}
    375       1.1   mjacob 		SYS_DELAY(1000);	/* wait one millisecond */
    376       1.1   mjacob 		mswait--;
    377       1.1   mjacob 	}
    378       1.1   mjacob 	return (1);
    379       1.8   mjacob }
    380       1.8   mjacob 
    381       1.8   mjacob static void
    382       1.8   mjacob isp_watch(arg)
    383       1.8   mjacob 	void *arg;
    384       1.8   mjacob {
    385       1.8   mjacob 	int i;
    386       1.8   mjacob 	struct ispsoftc *isp = arg;
    387      1.17   mjacob 	struct scsipi_xfer *xs;
    388      1.17   mjacob 	int s;
    389       1.8   mjacob 
    390       1.8   mjacob 	/*
    391       1.8   mjacob 	 * Look for completely dead commands (but not polled ones).
    392       1.8   mjacob 	 */
    393      1.17   mjacob 	s = splbio();
    394      1.17   mjacob 	for (i = 0; i < isp->isp_maxcmds; i++) {
    395      1.17   mjacob 		xs = isp->isp_xflist[i];
    396      1.17   mjacob 		if (xs == NULL) {
    397       1.8   mjacob 			continue;
    398       1.8   mjacob 		}
    399      1.17   mjacob 		if (xs->timeout == 0 || (xs->xs_control & XS_CTL_POLL)) {
    400       1.8   mjacob 			continue;
    401       1.8   mjacob 		}
    402      1.17   mjacob 		xs->timeout -= (WATCH_INTERVAL * 1000);
    403       1.8   mjacob 		/*
    404       1.8   mjacob 		 * Avoid later thinking that this
    405       1.8   mjacob 		 * transaction is not being timed.
    406       1.8   mjacob 		 * Then give ourselves to watchdog
    407       1.8   mjacob 		 * periods of grace.
    408       1.8   mjacob 		 */
    409      1.17   mjacob 		if (xs->timeout == 0) {
    410      1.17   mjacob 			xs->timeout = 1;
    411      1.17   mjacob 		} else if (xs->timeout > -(2 * WATCH_INTERVAL * 1000)) {
    412       1.8   mjacob 			continue;
    413       1.8   mjacob 		}
    414       1.8   mjacob 		if (isp_control(isp, ISPCTL_ABORT_CMD, xs)) {
    415       1.8   mjacob 			printf("%s: isp_watch failed to abort command\n",
    416       1.8   mjacob 			    isp->isp_name);
    417       1.8   mjacob 			isp_restart(isp);
    418       1.8   mjacob 			break;
    419       1.8   mjacob 		}
    420       1.8   mjacob 	}
    421       1.8   mjacob 	timeout(isp_watch, isp, WATCH_INTERVAL * hz);
    422       1.8   mjacob 	isp->isp_dogactive = 1;
    423      1.17   mjacob 	(void) splx(s);
    424       1.8   mjacob }
    425       1.8   mjacob 
    426       1.8   mjacob /*
    427       1.8   mjacob  * Free any associated resources prior to decommissioning and
    428       1.8   mjacob  * set the card to a known state (so it doesn't wake up and kick
    429       1.8   mjacob  * us when we aren't expecting it to).
    430       1.8   mjacob  *
    431       1.8   mjacob  * Locks are held before coming here.
    432       1.8   mjacob  */
    433       1.8   mjacob void
    434       1.8   mjacob isp_uninit(isp)
    435       1.8   mjacob 	struct ispsoftc *isp;
    436       1.8   mjacob {
    437       1.8   mjacob 	ISP_ILOCKVAL_DECL;
    438       1.8   mjacob 	ISP_ILOCK(isp);
    439       1.8   mjacob 	/*
    440       1.8   mjacob 	 * Leave with interrupts disabled.
    441       1.8   mjacob 	 */
    442       1.8   mjacob 	DISABLE_INTS(isp);
    443       1.8   mjacob 
    444       1.8   mjacob 	/*
    445       1.8   mjacob 	 * Turn off the watchdog (if active).
    446       1.8   mjacob 	 */
    447       1.8   mjacob 	if (isp->isp_dogactive) {
    448       1.8   mjacob 		untimeout(isp_watch, isp);
    449       1.8   mjacob 		isp->isp_dogactive = 0;
    450       1.8   mjacob 	}
    451       1.8   mjacob 
    452       1.8   mjacob 	ISP_IUNLOCK(isp);
    453       1.9   mjacob }
    454       1.9   mjacob 
    455      1.10   mjacob /*
    456      1.17   mjacob  * Restart function for a command to be requeued later.
    457      1.17   mjacob  */
    458      1.17   mjacob static void
    459      1.17   mjacob isp_command_requeue(arg)
    460      1.17   mjacob 	void *arg;
    461      1.17   mjacob {
    462      1.17   mjacob 	struct scsipi_xfer *xs = arg;
    463  1.18.2.1  thorpej 	int s;
    464  1.18.2.1  thorpej 
    465  1.18.2.1  thorpej 	s = splbio();
    466  1.18.2.1  thorpej 	scsipi_adapter_request(xs->xs_periph->periph_channel,
    467  1.18.2.1  thorpej 	    ADAPTER_REQ_RUN_XFER, xs);
    468      1.17   mjacob 	(void) splx(s);
    469      1.17   mjacob }
    470      1.18   mjacob 
    471       1.9   mjacob int
    472       1.9   mjacob isp_async(isp, cmd, arg)
    473       1.9   mjacob 	struct ispsoftc *isp;
    474       1.9   mjacob 	ispasync_t cmd;
    475       1.9   mjacob 	void *arg;
    476       1.9   mjacob {
    477  1.18.2.3  thorpej 	int bus, tgt;
    478      1.10   mjacob 	int s = splbio();
    479       1.9   mjacob 	switch (cmd) {
    480  1.18.2.3  thorpej 	case ISPASYNC_NEW_TGT_PARAMS: {
    481  1.18.2.3  thorpej 		struct scsipi_xfer_mode xm;
    482  1.18.2.3  thorpej 
    483  1.18.2.3  thorpej 		tgt = *((int *) arg);
    484  1.18.2.3  thorpej 		bus = (tgt >> 16) & 0xffff;
    485  1.18.2.3  thorpej 		tgt &= 0xffff;
    486  1.18.2.3  thorpej 
    487  1.18.2.3  thorpej 		xm.xm_target = tgt;
    488  1.18.2.3  thorpej 		scsipi_async_event(&isp->isp_osinfo._channels[bus],
    489  1.18.2.3  thorpej 		    ASYNC_EVENT_XFER_MODE, &xm);
    490      1.11   mjacob 		break;
    491  1.18.2.3  thorpej 	}
    492      1.11   mjacob 	case ISPASYNC_BUS_RESET:
    493      1.14   mjacob 		if (arg)
    494      1.14   mjacob 			bus = *((int *) arg);
    495      1.14   mjacob 		else
    496      1.14   mjacob 			bus = 0;
    497      1.14   mjacob 		printf("%s: SCSI bus %d reset detected\n", isp->isp_name, bus);
    498      1.11   mjacob 		break;
    499      1.11   mjacob 	case ISPASYNC_LOOP_DOWN:
    500      1.11   mjacob 		/*
    501      1.11   mjacob 		 * Hopefully we get here in time to minimize the number
    502      1.11   mjacob 		 * of commands we are firing off that are sure to die.
    503  1.18.2.4  thorpej 		 *
    504  1.18.2.4  thorpej 		 * XXX Hard-code channel 0, but only one channel on FC
    505  1.18.2.4  thorpej 		 * XXX adapters, right?
    506      1.11   mjacob 		 */
    507  1.18.2.4  thorpej 		scsipi_channel_freeze(&isp->isp_osinfo._channels[0], 1);
    508      1.11   mjacob 		printf("%s: Loop DOWN\n", isp->isp_name);
    509      1.11   mjacob 		break;
    510      1.11   mjacob         case ISPASYNC_LOOP_UP:
    511  1.18.2.4  thorpej 		/*
    512  1.18.2.4  thorpej 		 * XXX Hard-code channel 0, but only one channel on FC
    513  1.18.2.4  thorpej 		 * XXX adapters, right?
    514  1.18.2.4  thorpej 		 */
    515  1.18.2.4  thorpej 		timeout(scsipi_channel_timed_thaw,
    516  1.18.2.4  thorpej 		    &isp->isp_osinfo._channels[0], 1);
    517      1.11   mjacob 		printf("%s: Loop UP\n", isp->isp_name);
    518      1.11   mjacob 		break;
    519      1.15   mjacob 	case ISPASYNC_PDB_CHANGED:
    520      1.17   mjacob 	if (IS_FC(isp) && isp->isp_dblev) {
    521      1.15   mjacob 		const char *fmt = "%s: Target %d (Loop 0x%x) Port ID 0x%x "
    522      1.15   mjacob 		    "role %s %s\n Port WWN 0x%08x%08x\n Node WWN 0x%08x%08x\n";
    523      1.15   mjacob 		const static char *roles[4] = {
    524      1.11   mjacob 		    "No", "Target", "Initiator", "Target/Initiator"
    525      1.11   mjacob 		};
    526      1.15   mjacob 		char *ptr;
    527      1.15   mjacob 		fcparam *fcp = isp->isp_param;
    528      1.15   mjacob 		int tgt = *((int *) arg);
    529      1.15   mjacob 		struct lportdb *lp = &fcp->portdb[tgt];
    530      1.15   mjacob 
    531      1.15   mjacob 		if (lp->valid) {
    532      1.15   mjacob 			ptr = "arrived";
    533      1.15   mjacob 		} else {
    534      1.15   mjacob 			ptr = "disappeared";
    535      1.11   mjacob 		}
    536      1.15   mjacob 		printf(fmt, isp->isp_name, tgt, lp->loopid, lp->portid,
    537      1.15   mjacob 		    roles[lp->roles & 0x3], ptr,
    538      1.15   mjacob 		    (u_int32_t) (lp->port_wwn >> 32),
    539      1.15   mjacob 		    (u_int32_t) (lp->port_wwn & 0xffffffffLL),
    540      1.15   mjacob 		    (u_int32_t) (lp->node_wwn >> 32),
    541      1.15   mjacob 		    (u_int32_t) (lp->node_wwn & 0xffffffffLL));
    542      1.11   mjacob 		break;
    543      1.11   mjacob 	}
    544      1.15   mjacob #ifdef	ISP2100_FABRIC
    545      1.11   mjacob 	case ISPASYNC_CHANGE_NOTIFY:
    546      1.11   mjacob 		printf("%s: Name Server Database Changed\n", isp->isp_name);
    547       1.9   mjacob 		break;
    548      1.15   mjacob 	case ISPASYNC_FABRIC_DEV:
    549      1.15   mjacob 	{
    550      1.15   mjacob 		int target;
    551      1.15   mjacob 		struct lportdb *lp;
    552      1.15   mjacob 		sns_scrsp_t *resp = (sns_scrsp_t *) arg;
    553      1.15   mjacob 		u_int32_t portid;
    554      1.15   mjacob 		u_int64_t wwn;
    555      1.15   mjacob 		fcparam *fcp = isp->isp_param;
    556      1.15   mjacob 
    557      1.15   mjacob 		portid =
    558      1.15   mjacob 		    (((u_int32_t) resp->snscb_port_id[0]) << 16) |
    559      1.15   mjacob 		    (((u_int32_t) resp->snscb_port_id[1]) << 8) |
    560      1.15   mjacob 		    (((u_int32_t) resp->snscb_port_id[2]));
    561      1.15   mjacob 		wwn =
    562      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[0]) << 56) |
    563      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[1]) << 48) |
    564      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[2]) << 40) |
    565      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[3]) << 32) |
    566      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[4]) << 24) |
    567      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[5]) << 16) |
    568      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[6]) <<  8) |
    569      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[7]));
    570      1.15   mjacob 		printf("%s: Fabric Device (Type 0x%x)@PortID 0x%x WWN "
    571      1.15   mjacob 		    "0x%08x%08x\n", isp->isp_name, resp->snscb_port_type,
    572      1.15   mjacob 		    portid, ((u_int32_t)(wwn >> 32)),
    573      1.15   mjacob 		    ((u_int32_t)(wwn & 0xffffffff)));
    574      1.15   mjacob 		if (resp->snscb_port_type != 2)
    575      1.15   mjacob 			break;
    576      1.15   mjacob 		for (target = FC_SNS_ID+1; target < MAX_FC_TARG; target++) {
    577      1.15   mjacob 			lp = &fcp->portdb[target];
    578      1.15   mjacob 			if (lp->port_wwn == wwn)
    579      1.15   mjacob 				break;
    580      1.15   mjacob 		}
    581      1.15   mjacob 		if (target < MAX_FC_TARG) {
    582      1.15   mjacob 			break;
    583      1.15   mjacob 		}
    584      1.15   mjacob 		for (target = FC_SNS_ID+1; target < MAX_FC_TARG; target++) {
    585      1.15   mjacob 			lp = &fcp->portdb[target];
    586      1.15   mjacob 			if (lp->port_wwn == 0)
    587      1.15   mjacob 				break;
    588      1.15   mjacob 		}
    589      1.15   mjacob 		if (target == MAX_FC_TARG) {
    590      1.15   mjacob 			printf("%s: no more space for fabric devices\n",
    591      1.15   mjacob 			    isp->isp_name);
    592      1.15   mjacob 			return (-1);
    593      1.15   mjacob 		}
    594      1.15   mjacob 		lp->port_wwn = lp->node_wwn = wwn;
    595      1.15   mjacob 		lp->portid = portid;
    596      1.15   mjacob 		break;
    597      1.15   mjacob 	}
    598      1.15   mjacob #endif
    599       1.9   mjacob 	default:
    600       1.9   mjacob 		break;
    601       1.9   mjacob 	}
    602      1.10   mjacob 	(void) splx(s);
    603       1.9   mjacob 	return (0);
    604       1.1   mjacob }
    605