Home | History | Annotate | Line # | Download | only in ic
isp_netbsd.c revision 1.12.2.2
      1  1.12.2.2       he /* $NetBSD: isp_netbsd.c,v 1.12.2.2 2000/05/13 17:14:03 he Exp $ */
      2       1.1   mjacob /*
      3       1.1   mjacob  * Platform (NetBSD) dependent common attachment code for Qlogic adapters.
      4  1.12.2.1       he  * Matthew Jacob <mjacob (at) nas.nasa.gov>
      5  1.12.2.1       he  */
      6  1.12.2.1       he /*
      7  1.12.2.1       he  * 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.12.2.1       he  *    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.12.2.1       he  *    derived from this software without specific prior written permission
     20       1.1   mjacob  *
     21  1.12.2.1       he  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.12.2.1       he  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.12.2.1       he  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.12.2.1       he  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.12.2.1       he  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  1.12.2.1       he  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  1.12.2.1       he  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  1.12.2.1       he  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  1.12.2.1       he  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  1.12.2.1       he  * 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.12.2.1       he #include <sys/scsiio.h>
     35       1.1   mjacob 
     36       1.1   mjacob static void ispminphys __P((struct buf *));
     37  1.12.2.1       he static int32_t ispcmd_slow __P((ISP_SCSI_XFER_T *));
     38       1.1   mjacob static int32_t ispcmd __P((ISP_SCSI_XFER_T *));
     39  1.12.2.1       he static int
     40  1.12.2.1       he ispioctl __P((struct scsipi_link *, u_long, caddr_t, int, struct proc *));
     41       1.1   mjacob 
     42       1.1   mjacob static struct scsipi_device isp_dev = { NULL, NULL, NULL, NULL };
     43       1.1   mjacob static int isp_poll __P((struct ispsoftc *, ISP_SCSI_XFER_T *, int));
     44      1.10   mjacob static void isp_watch __P((void *));
     45  1.12.2.1       he static void isp_command_requeue __P((void *));
     46      1.10   mjacob static void isp_internal_restart __P((void *));
     47      1.10   mjacob 
     48       1.1   mjacob /*
     49       1.1   mjacob  * Complete attachment of hardware, include subdevices.
     50       1.1   mjacob  */
     51       1.1   mjacob void
     52       1.1   mjacob isp_attach(isp)
     53       1.1   mjacob 	struct ispsoftc *isp;
     54       1.1   mjacob {
     55       1.6  thorpej 
     56       1.6  thorpej 	isp->isp_osinfo._adapter.scsipi_minphys = ispminphys;
     57  1.12.2.1       he 	isp->isp_osinfo._adapter.scsipi_ioctl = ispioctl;
     58       1.6  thorpej 
     59       1.1   mjacob 	isp->isp_state = ISP_RUNSTATE;
     60  1.12.2.1       he 	isp->isp_osinfo._link.scsipi_scsi.channel =
     61  1.12.2.1       he 	    (IS_DUALBUS(isp))? 0 : SCSI_CHANNEL_ONLY_ONE;
     62       1.1   mjacob 	isp->isp_osinfo._link.adapter_softc = isp;
     63       1.1   mjacob 	isp->isp_osinfo._link.device = &isp_dev;
     64       1.6  thorpej 	isp->isp_osinfo._link.adapter = &isp->isp_osinfo._adapter;
     65  1.12.2.1       he 	isp->isp_osinfo._link.openings = isp->isp_maxcmds;
     66  1.12.2.1       he 	TAILQ_INIT(&isp->isp_osinfo.waitq);	/* XXX 2nd Bus? */
     67       1.1   mjacob 
     68  1.12.2.1       he 	if (IS_FC(isp)) {
     69      1.11   mjacob 		/*
     70      1.11   mjacob 		 * Give it another chance here to come alive...
     71      1.11   mjacob 		 */
     72  1.12.2.1       he 		isp->isp_osinfo._adapter.scsipi_cmd = ispcmd;
     73       1.1   mjacob 		isp->isp_osinfo._link.scsipi_scsi.max_target = MAX_FC_TARG-1;
     74      1.11   mjacob #ifdef	ISP2100_SCCLUN
     75       1.7   mjacob 		/*
     76       1.7   mjacob 		 * 16 bits worth, but let's be reasonable..
     77       1.7   mjacob 		 */
     78       1.7   mjacob 		isp->isp_osinfo._link.scsipi_scsi.max_lun = 255;
     79       1.7   mjacob #else
     80       1.7   mjacob 		isp->isp_osinfo._link.scsipi_scsi.max_lun = 15;
     81       1.7   mjacob #endif
     82  1.12.2.1       he 		/* set below */
     83       1.1   mjacob 	} else {
     84  1.12.2.1       he 		sdparam *sdp = isp->isp_param;
     85  1.12.2.1       he 		isp->isp_osinfo._adapter.scsipi_cmd = ispcmd_slow;
     86       1.1   mjacob 		isp->isp_osinfo._link.scsipi_scsi.max_target = MAX_TARGETS-1;
     87  1.12.2.1       he 		isp->isp_osinfo._link.scsipi_scsi.max_lun = 7;
     88       1.1   mjacob 		isp->isp_osinfo._link.scsipi_scsi.adapter_target =
     89  1.12.2.1       he 		    sdp->isp_initiator_id;
     90  1.12.2.1       he 		isp->isp_osinfo.discovered[0] = 1 << sdp->isp_initiator_id;
     91  1.12.2.1       he 		if (IS_DUALBUS(isp)) {
     92  1.12.2.1       he 			isp->isp_osinfo._link_b = isp->isp_osinfo._link;
     93  1.12.2.1       he 			sdp++;
     94  1.12.2.1       he 			isp->isp_osinfo.discovered[1] =
     95  1.12.2.1       he 			    1 << sdp->isp_initiator_id;
     96  1.12.2.1       he 			isp->isp_osinfo._link_b.scsipi_scsi.adapter_target =
     97  1.12.2.1       he 			    sdp->isp_initiator_id;
     98  1.12.2.1       he 			isp->isp_osinfo._link_b.scsipi_scsi.channel = 1;
     99  1.12.2.1       he 		}
    100       1.1   mjacob 	}
    101       1.1   mjacob 	isp->isp_osinfo._link.type = BUS_SCSI;
    102       1.8   mjacob 
    103       1.8   mjacob 	/*
    104      1.10   mjacob 	 * Send a SCSI Bus Reset (used to be done as part of attach,
    105      1.10   mjacob 	 * but now left to the OS outer layers).
    106      1.10   mjacob 	 */
    107  1.12.2.1       he 	if (IS_SCSI(isp)) {
    108  1.12.2.1       he 		int bus = 0;
    109  1.12.2.1       he 		(void) isp_control(isp, ISPCTL_RESET_BUS, &bus);
    110  1.12.2.1       he 		if (IS_DUALBUS(isp)) {
    111  1.12.2.1       he 			bus++;
    112  1.12.2.1       he 			(void) isp_control(isp, ISPCTL_RESET_BUS, &bus);
    113  1.12.2.1       he 		}
    114      1.11   mjacob 		SYS_DELAY(2*1000000);
    115      1.11   mjacob 	} else {
    116  1.12.2.1       he 		int i, j;
    117  1.12.2.1       he 		fcparam *fcp = isp->isp_param;
    118  1.12.2.1       he 		delay(2 * 1000000);
    119  1.12.2.1       he 		for (j = 0; j < 5; j++) {
    120  1.12.2.1       he 			for (i = 0; i < 5; i++) {
    121  1.12.2.1       he 				if (isp_control(isp, ISPCTL_FCLINK_TEST, NULL))
    122  1.12.2.1       he 					continue;
    123  1.12.2.1       he #ifdef	ISP2100_FABRIC
    124  1.12.2.1       he 				/*
    125  1.12.2.1       he 				 * Wait extra time to see if the f/w
    126  1.12.2.1       he 				 * eventually completed an FLOGI that
    127  1.12.2.1       he 				 * will allow us to know we're on a
    128  1.12.2.1       he 				 * fabric.
    129  1.12.2.1       he 				 */
    130  1.12.2.1       he 				if (fcp->isp_onfabric == 0) {
    131  1.12.2.1       he 					delay(1 * 1000000);
    132  1.12.2.1       he 					continue;
    133  1.12.2.1       he 				}
    134  1.12.2.1       he #endif
    135  1.12.2.1       he 				break;
    136  1.12.2.1       he 			}
    137  1.12.2.1       he 			if (fcp->isp_fwstate == FW_READY &&
    138  1.12.2.1       he 			    fcp->isp_loopstate >= LOOP_PDB_RCVD) {
    139  1.12.2.1       he 				break;
    140  1.12.2.1       he 			}
    141  1.12.2.1       he 		}
    142  1.12.2.1       he 		isp->isp_osinfo._link.scsipi_scsi.adapter_target =
    143  1.12.2.1       he 			fcp->isp_loopid;
    144      1.11   mjacob 	}
    145      1.10   mjacob 
    146      1.10   mjacob 	/*
    147       1.8   mjacob 	 * Start the watchdog.
    148       1.8   mjacob 	 */
    149       1.8   mjacob 	isp->isp_dogactive = 1;
    150  1.12.2.1       he 	timeout(isp_watch, isp, WATCH_INTERVAL * hz);
    151       1.8   mjacob 
    152       1.8   mjacob 	/*
    153       1.8   mjacob 	 * And attach children (if any).
    154       1.8   mjacob 	 */
    155       1.1   mjacob 	config_found((void *)isp, &isp->isp_osinfo._link, scsiprint);
    156  1.12.2.1       he 	if (IS_DUALBUS(isp)) {
    157  1.12.2.1       he 		config_found((void *)isp, &isp->isp_osinfo._link_b, scsiprint);
    158  1.12.2.1       he 	}
    159       1.1   mjacob }
    160       1.1   mjacob 
    161       1.1   mjacob /*
    162       1.1   mjacob  * minphys our xfers
    163       1.1   mjacob  *
    164       1.1   mjacob  * Unfortunately, the buffer pointer describes the target device- not the
    165       1.1   mjacob  * adapter device, so we can't use the pointer to find out what kind of
    166       1.1   mjacob  * adapter we are and adjust accordingly.
    167       1.1   mjacob  */
    168       1.1   mjacob 
    169       1.1   mjacob static void
    170       1.1   mjacob ispminphys(bp)
    171       1.1   mjacob 	struct buf *bp;
    172       1.1   mjacob {
    173       1.1   mjacob 	/*
    174       1.1   mjacob 	 * XX: Only the 1020 has a 24 bit limit.
    175       1.1   mjacob 	 */
    176       1.1   mjacob 	if (bp->b_bcount >= (1 << 24)) {
    177       1.1   mjacob 		bp->b_bcount = (1 << 24);
    178       1.1   mjacob 	}
    179       1.1   mjacob 	minphys(bp);
    180       1.1   mjacob }
    181       1.1   mjacob 
    182  1.12.2.1       he static int32_t
    183  1.12.2.1       he ispcmd_slow(xs)
    184       1.1   mjacob 	ISP_SCSI_XFER_T *xs;
    185       1.1   mjacob {
    186  1.12.2.1       he 	sdparam *sdp;
    187  1.12.2.1       he 	int tgt, chan, s;
    188  1.12.2.1       he 	u_int16_t flags;
    189  1.12.2.1       he 	struct ispsoftc *isp = XS_ISP(xs);
    190       1.1   mjacob 
    191       1.3   mjacob 	/*
    192  1.12.2.1       he 	 * Have we completed discovery for this target on this adapter?
    193       1.3   mjacob 	 */
    194  1.12.2.1       he 	tgt = XS_TGT(xs);
    195  1.12.2.1       he 	chan = XS_CHANNEL(xs);
    196  1.12.2.1       he 	if ((xs->flags & SCSI_AUTOCONF) != 0 ||
    197  1.12.2.1       he 	    (isp->isp_osinfo.discovered[chan] & (1 << tgt)) != 0) {
    198  1.12.2.1       he 		return (ispcmd(xs));
    199  1.12.2.1       he 	}
    200  1.12.2.1       he 
    201  1.12.2.1       he 	flags = DPARM_DEFAULT;
    202  1.12.2.1       he 	if (xs->sc_link->quirks & SDEV_NOSYNC) {
    203  1.12.2.1       he 		flags ^= DPARM_SYNC;
    204  1.12.2.1       he #ifdef	DEBUG
    205  1.12.2.1       he 	} else {
    206  1.12.2.1       he 		printf("%s: channel %d target %d can do SYNC xfers\n",
    207  1.12.2.1       he 		    isp->isp_name, chan, tgt);
    208  1.12.2.1       he #endif
    209  1.12.2.1       he 	}
    210  1.12.2.1       he 	if (xs->sc_link->quirks & SDEV_NOWIDE) {
    211  1.12.2.1       he 		flags ^= DPARM_WIDE;
    212  1.12.2.1       he #ifdef	DEBUG
    213  1.12.2.1       he 	} else {
    214  1.12.2.1       he 		printf("%s: channel %d target %d can do WIDE xfers\n",
    215  1.12.2.1       he 		    isp->isp_name, chan, tgt);
    216  1.12.2.1       he #endif
    217  1.12.2.1       he 	}
    218  1.12.2.1       he 	if (xs->sc_link->quirks & SDEV_NOTAG) {
    219  1.12.2.1       he 		flags ^= DPARM_TQING;
    220  1.12.2.1       he #ifdef	DEBUG
    221  1.12.2.1       he 	} else {
    222  1.12.2.1       he 		printf("%s: channel %d target %d can do TAGGED xfers\n",
    223  1.12.2.1       he 		    isp->isp_name, chan, tgt);
    224  1.12.2.1       he #endif
    225  1.12.2.1       he 	}
    226  1.12.2.1       he 	/*
    227  1.12.2.1       he 	 * Okay, we know about this device now,
    228  1.12.2.1       he 	 * so mark parameters to be updated for it.
    229  1.12.2.1       he 	 */
    230  1.12.2.1       he 	s = splbio();
    231  1.12.2.1       he 	isp->isp_osinfo.discovered[chan] |= (1 << tgt);
    232  1.12.2.1       he 	sdp = isp->isp_param;
    233  1.12.2.1       he 	sdp += chan;
    234  1.12.2.1       he 	sdp->isp_devparam[tgt].dev_flags = flags;
    235  1.12.2.1       he 	sdp->isp_devparam[tgt].dev_update = 1;
    236  1.12.2.1       he 	isp->isp_update |= (1 << chan);
    237  1.12.2.1       he 	splx(s);
    238  1.12.2.1       he 	return (ispcmd(xs));
    239  1.12.2.1       he }
    240  1.12.2.1       he 
    241  1.12.2.1       he static int
    242  1.12.2.1       he ispioctl(sc_link, cmd, addr, flag, p)
    243  1.12.2.1       he 	struct scsipi_link *sc_link;
    244  1.12.2.1       he 	u_long cmd;
    245  1.12.2.1       he 	caddr_t addr;
    246  1.12.2.1       he 	int flag;
    247  1.12.2.1       he 	struct proc *p;
    248  1.12.2.1       he {
    249  1.12.2.1       he 	struct ispsoftc *isp = sc_link->adapter_softc;
    250  1.12.2.1       he 	int s, chan, retval = ENOTTY;
    251  1.12.2.1       he 
    252  1.12.2.1       he 	switch (cmd) {
    253  1.12.2.1       he 	case SCBUSIORESET:
    254  1.12.2.1       he 		chan = sc_link->scsipi_scsi.channel;
    255  1.12.2.1       he 		s = splbio();
    256  1.12.2.1       he 		if (isp_control(isp, ISPCTL_RESET_BUS, &chan))
    257  1.12.2.1       he 			retval = EIO;
    258  1.12.2.1       he 		else
    259  1.12.2.1       he 			retval = 0;
    260  1.12.2.1       he 		(void) splx(s);
    261  1.12.2.1       he 		break;
    262  1.12.2.1       he 	default:
    263  1.12.2.1       he 		break;
    264       1.3   mjacob 	}
    265  1.12.2.1       he 	return (retval);
    266  1.12.2.1       he }
    267  1.12.2.1       he 
    268  1.12.2.1       he 
    269  1.12.2.1       he static int32_t
    270  1.12.2.1       he ispcmd(xs)
    271  1.12.2.1       he 	ISP_SCSI_XFER_T *xs;
    272  1.12.2.1       he {
    273  1.12.2.1       he 	struct ispsoftc *isp;
    274  1.12.2.1       he 	int result, s;
    275      1.11   mjacob 
    276  1.12.2.1       he 	isp = XS_ISP(xs);
    277  1.12.2.1       he 	s = splbio();
    278      1.11   mjacob 	if (isp->isp_state < ISP_RUNSTATE) {
    279      1.11   mjacob 		DISABLE_INTS(isp);
    280      1.11   mjacob 		isp_init(isp);
    281      1.11   mjacob                 if (isp->isp_state != ISP_INITSTATE) {
    282      1.11   mjacob 			ENABLE_INTS(isp);
    283      1.11   mjacob                         (void) splx(s);
    284      1.11   mjacob                         XS_SETERR(xs, HBA_BOTCH);
    285  1.12.2.1       he                         return (COMPLETE);
    286      1.11   mjacob                 }
    287      1.11   mjacob                 isp->isp_state = ISP_RUNSTATE;
    288      1.11   mjacob 		ENABLE_INTS(isp);
    289      1.11   mjacob         }
    290      1.11   mjacob 
    291      1.10   mjacob 	/*
    292      1.10   mjacob 	 * Check for queue blockage...
    293      1.10   mjacob 	 */
    294      1.10   mjacob 	if (isp->isp_osinfo.blocked) {
    295      1.10   mjacob 		if (xs->flags & SCSI_POLL) {
    296      1.10   mjacob 			xs->error = XS_DRIVER_STUFFUP;
    297      1.10   mjacob 			splx(s);
    298      1.10   mjacob 			return (TRY_AGAIN_LATER);
    299      1.10   mjacob 		}
    300      1.10   mjacob 		TAILQ_INSERT_TAIL(&isp->isp_osinfo.waitq, xs, adapter_q);
    301      1.10   mjacob 		splx(s);
    302  1.12.2.1       he 		return (SUCCESSFULLY_QUEUED);
    303      1.10   mjacob 	}
    304      1.11   mjacob 	DISABLE_INTS(isp);
    305       1.3   mjacob 	result = ispscsicmd(xs);
    306      1.11   mjacob 	ENABLE_INTS(isp);
    307  1.12.2.1       he 
    308  1.12.2.1       he 	if ((xs->flags & SCSI_POLL) == 0) {
    309  1.12.2.1       he 		switch (result) {
    310  1.12.2.1       he 		case CMD_QUEUED:
    311  1.12.2.1       he 			result = SUCCESSFULLY_QUEUED;
    312  1.12.2.1       he 			break;
    313  1.12.2.1       he 		case CMD_EAGAIN:
    314  1.12.2.1       he 			result = TRY_AGAIN_LATER;
    315  1.12.2.1       he 			break;
    316  1.12.2.1       he 		case CMD_RQLATER:
    317  1.12.2.1       he 			result = SUCCESSFULLY_QUEUED;
    318  1.12.2.1       he 			timeout(isp_command_requeue, xs, hz);
    319  1.12.2.1       he 			break;
    320  1.12.2.1       he 		case CMD_COMPLETE:
    321  1.12.2.1       he 			result = COMPLETE;
    322  1.12.2.1       he 			break;
    323  1.12.2.1       he 		}
    324      1.10   mjacob 		(void) splx(s);
    325       1.3   mjacob 		return (result);
    326       1.1   mjacob 	}
    327       1.1   mjacob 
    328  1.12.2.1       he 	switch (result) {
    329  1.12.2.1       he 	case CMD_QUEUED:
    330  1.12.2.1       he 		result = SUCCESSFULLY_QUEUED;
    331  1.12.2.1       he 		break;
    332  1.12.2.1       he 	case CMD_RQLATER:
    333  1.12.2.1       he 	case CMD_EAGAIN:
    334  1.12.2.1       he 		if (XS_NOERR(xs)) {
    335  1.12.2.1       he 			xs->error = XS_DRIVER_STUFFUP;
    336  1.12.2.1       he 		}
    337  1.12.2.1       he 		result = TRY_AGAIN_LATER;
    338  1.12.2.1       he 		break;
    339  1.12.2.1       he 	case CMD_COMPLETE:
    340  1.12.2.1       he 		result = COMPLETE;
    341  1.12.2.1       he 		break;
    342  1.12.2.1       he 
    343  1.12.2.1       he 	}
    344       1.1   mjacob 	/*
    345       1.1   mjacob 	 * If we can't use interrupts, poll on completion.
    346       1.1   mjacob 	 */
    347  1.12.2.1       he 	if (result == SUCCESSFULLY_QUEUED) {
    348  1.12.2.1       he 		if (isp_poll(isp, xs, XS_TIME(xs))) {
    349  1.12.2.1       he 			/*
    350  1.12.2.1       he 			 * If no other error occurred but we didn't finish,
    351  1.12.2.1       he 			 * something bad happened.
    352  1.12.2.1       he 			 */
    353  1.12.2.1       he 			if (XS_IS_CMD_DONE(xs) == 0) {
    354  1.12.2.1       he 				if (isp_control(isp, ISPCTL_ABORT_CMD, xs)) {
    355  1.12.2.1       he 					isp_restart(isp);
    356  1.12.2.1       he 				}
    357  1.12.2.1       he 				if (XS_NOERR(xs)) {
    358  1.12.2.1       he 					XS_SETERR(xs, HBA_BOTCH);
    359  1.12.2.1       he 				}
    360       1.1   mjacob 			}
    361       1.1   mjacob 		}
    362  1.12.2.1       he 		result = COMPLETE;
    363       1.1   mjacob 	}
    364      1.10   mjacob 	(void) splx(s);
    365  1.12.2.1       he 	return (result);
    366       1.1   mjacob }
    367       1.1   mjacob 
    368       1.1   mjacob static int
    369       1.1   mjacob isp_poll(isp, xs, mswait)
    370       1.1   mjacob 	struct ispsoftc *isp;
    371       1.1   mjacob 	ISP_SCSI_XFER_T *xs;
    372       1.1   mjacob 	int mswait;
    373       1.1   mjacob {
    374       1.1   mjacob 
    375       1.1   mjacob 	while (mswait) {
    376       1.1   mjacob 		/* Try the interrupt handling routine */
    377       1.1   mjacob 		(void)isp_intr((void *)isp);
    378       1.1   mjacob 
    379       1.1   mjacob 		/* See if the xs is now done */
    380       1.1   mjacob 		if (XS_IS_CMD_DONE(xs)) {
    381       1.1   mjacob 			return (0);
    382       1.1   mjacob 		}
    383       1.1   mjacob 		SYS_DELAY(1000);	/* wait one millisecond */
    384       1.1   mjacob 		mswait--;
    385       1.1   mjacob 	}
    386       1.1   mjacob 	return (1);
    387       1.8   mjacob }
    388       1.8   mjacob 
    389       1.8   mjacob static void
    390       1.8   mjacob isp_watch(arg)
    391       1.8   mjacob 	void *arg;
    392       1.8   mjacob {
    393       1.8   mjacob 	int i;
    394       1.8   mjacob 	struct ispsoftc *isp = arg;
    395  1.12.2.1       he 	struct scsipi_xfer *xs;
    396  1.12.2.1       he 	int s;
    397       1.8   mjacob 
    398       1.8   mjacob 	/*
    399       1.8   mjacob 	 * Look for completely dead commands (but not polled ones).
    400       1.8   mjacob 	 */
    401  1.12.2.1       he 	s = splbio();
    402  1.12.2.1       he 	for (i = 0; i < isp->isp_maxcmds; i++) {
    403  1.12.2.1       he 		xs = isp->isp_xflist[i];
    404  1.12.2.1       he 		if (xs == NULL) {
    405       1.8   mjacob 			continue;
    406       1.8   mjacob 		}
    407  1.12.2.1       he 		if (xs->timeout == 0 || (xs->flags & SCSI_POLL)) {
    408       1.8   mjacob 			continue;
    409       1.8   mjacob 		}
    410  1.12.2.1       he 		xs->timeout -= (WATCH_INTERVAL * 1000);
    411       1.8   mjacob 		/*
    412       1.8   mjacob 		 * Avoid later thinking that this
    413       1.8   mjacob 		 * transaction is not being timed.
    414       1.8   mjacob 		 * Then give ourselves to watchdog
    415       1.8   mjacob 		 * periods of grace.
    416       1.8   mjacob 		 */
    417  1.12.2.1       he 		if (xs->timeout == 0) {
    418  1.12.2.1       he 			xs->timeout = 1;
    419  1.12.2.1       he 		} else if (xs->timeout > -(2 * WATCH_INTERVAL * 1000)) {
    420       1.8   mjacob 			continue;
    421       1.8   mjacob 		}
    422       1.8   mjacob 		if (isp_control(isp, ISPCTL_ABORT_CMD, xs)) {
    423       1.8   mjacob 			printf("%s: isp_watch failed to abort command\n",
    424       1.8   mjacob 			    isp->isp_name);
    425       1.8   mjacob 			isp_restart(isp);
    426       1.8   mjacob 			break;
    427       1.8   mjacob 		}
    428       1.8   mjacob 	}
    429       1.8   mjacob 	timeout(isp_watch, isp, WATCH_INTERVAL * hz);
    430       1.8   mjacob 	isp->isp_dogactive = 1;
    431  1.12.2.1       he 	(void) splx(s);
    432       1.8   mjacob }
    433       1.8   mjacob 
    434       1.8   mjacob /*
    435       1.8   mjacob  * Free any associated resources prior to decommissioning and
    436       1.8   mjacob  * set the card to a known state (so it doesn't wake up and kick
    437       1.8   mjacob  * us when we aren't expecting it to).
    438       1.8   mjacob  *
    439       1.8   mjacob  * Locks are held before coming here.
    440       1.8   mjacob  */
    441       1.8   mjacob void
    442       1.8   mjacob isp_uninit(isp)
    443       1.8   mjacob 	struct ispsoftc *isp;
    444       1.8   mjacob {
    445       1.8   mjacob 	ISP_ILOCKVAL_DECL;
    446       1.8   mjacob 	ISP_ILOCK(isp);
    447       1.8   mjacob 	/*
    448       1.8   mjacob 	 * Leave with interrupts disabled.
    449       1.8   mjacob 	 */
    450       1.8   mjacob 	DISABLE_INTS(isp);
    451       1.8   mjacob 
    452       1.8   mjacob 	/*
    453       1.8   mjacob 	 * Turn off the watchdog (if active).
    454       1.8   mjacob 	 */
    455       1.8   mjacob 	if (isp->isp_dogactive) {
    456       1.8   mjacob 		untimeout(isp_watch, isp);
    457       1.8   mjacob 		isp->isp_dogactive = 0;
    458       1.8   mjacob 	}
    459       1.8   mjacob 
    460       1.8   mjacob 	ISP_IUNLOCK(isp);
    461       1.9   mjacob }
    462       1.9   mjacob 
    463      1.10   mjacob /*
    464  1.12.2.1       he  * Restart function for a command to be requeued later.
    465  1.12.2.1       he  */
    466  1.12.2.1       he static void
    467  1.12.2.1       he isp_command_requeue(arg)
    468  1.12.2.1       he 	void *arg;
    469  1.12.2.1       he {
    470  1.12.2.1       he 	struct scsipi_xfer *xs = arg;
    471  1.12.2.1       he 	struct ispsoftc *isp = XS_ISP(xs);
    472  1.12.2.1       he 	int s = splbio();
    473  1.12.2.1       he 	switch (ispcmd_slow(xs)) {
    474  1.12.2.1       he 	case SUCCESSFULLY_QUEUED:
    475  1.12.2.1       he 		printf("%s: isp_command_requeue: requeued for %d.%d\n",
    476  1.12.2.1       he 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
    477  1.12.2.1       he 		break;
    478  1.12.2.1       he 	case TRY_AGAIN_LATER:
    479  1.12.2.1       he 		printf("%s: EAGAIN for %d.%d\n",
    480  1.12.2.1       he 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
    481  1.12.2.1       he 		/* FALLTHROUGH */
    482  1.12.2.1       he 	case COMPLETE:
    483  1.12.2.1       he 		/* can only be an error */
    484  1.12.2.1       he 		xs->flags |= ITSDONE;
    485  1.12.2.1       he 		if (XS_NOERR(xs)) {
    486  1.12.2.1       he 			XS_SETERR(xs, HBA_BOTCH);
    487  1.12.2.1       he 		}
    488  1.12.2.1       he 		scsipi_done(xs);
    489  1.12.2.1       he 		break;
    490  1.12.2.1       he 	}
    491  1.12.2.1       he 	(void) splx(s);
    492  1.12.2.1       he }
    493  1.12.2.1       he 
    494  1.12.2.1       he /*
    495      1.10   mjacob  * Restart function after a LOOP UP event (e.g.),
    496      1.10   mjacob  * done as a timeout for some hysteresis.
    497      1.10   mjacob  */
    498      1.10   mjacob static void
    499      1.10   mjacob isp_internal_restart(arg)
    500      1.10   mjacob 	void *arg;
    501      1.10   mjacob {
    502      1.10   mjacob 	struct ispsoftc *isp = arg;
    503      1.11   mjacob 	int result, nrestarted = 0, s;
    504      1.10   mjacob 
    505      1.11   mjacob 	s = splbio();
    506      1.10   mjacob 	if (isp->isp_osinfo.blocked == 0) {
    507      1.10   mjacob 		struct scsipi_xfer *xs;
    508      1.10   mjacob 		while ((xs = TAILQ_FIRST(&isp->isp_osinfo.waitq)) != NULL) {
    509      1.10   mjacob 			TAILQ_REMOVE(&isp->isp_osinfo.waitq, xs, adapter_q);
    510      1.11   mjacob 			DISABLE_INTS(isp);
    511      1.11   mjacob 			result = ispscsicmd(xs);
    512      1.11   mjacob 			ENABLE_INTS(isp);
    513      1.11   mjacob 			if (result != CMD_QUEUED) {
    514      1.10   mjacob 				printf("%s: botched command restart (0x%x)\n",
    515      1.10   mjacob 				    isp->isp_name, result);
    516      1.10   mjacob 				xs->flags |= ITSDONE;
    517      1.10   mjacob 				if (xs->error == XS_NOERROR)
    518      1.10   mjacob 					xs->error = XS_DRIVER_STUFFUP;
    519      1.10   mjacob 				scsipi_done(xs);
    520      1.10   mjacob 			}
    521      1.10   mjacob 			nrestarted++;
    522      1.10   mjacob 		}
    523      1.10   mjacob 		printf("%s: requeued %d commands\n", isp->isp_name, nrestarted);
    524      1.10   mjacob 	}
    525      1.10   mjacob 	(void) splx(s);
    526      1.10   mjacob }
    527  1.12.2.1       he 
    528       1.9   mjacob int
    529       1.9   mjacob isp_async(isp, cmd, arg)
    530       1.9   mjacob 	struct ispsoftc *isp;
    531       1.9   mjacob 	ispasync_t cmd;
    532       1.9   mjacob 	void *arg;
    533       1.9   mjacob {
    534  1.12.2.1       he 	int bus, tgt;
    535      1.10   mjacob 	int s = splbio();
    536       1.9   mjacob 	switch (cmd) {
    537       1.9   mjacob 	case ISPASYNC_NEW_TGT_PARAMS:
    538  1.12.2.1       he 	if (IS_SCSI(isp) && isp->isp_dblev) {
    539  1.12.2.1       he 		sdparam *sdp = isp->isp_param;
    540  1.12.2.1       he 		char *wt;
    541  1.12.2.1       he 		int mhz, flags, period;
    542  1.12.2.1       he 
    543  1.12.2.1       he 		tgt = *((int *) arg);
    544  1.12.2.1       he 		bus = (tgt >> 16) & 0xffff;
    545  1.12.2.1       he 		tgt &= 0xffff;
    546  1.12.2.1       he 		sdp += bus;
    547  1.12.2.1       he 		flags = sdp->isp_devparam[tgt].cur_dflags;
    548  1.12.2.1       he 		period = sdp->isp_devparam[tgt].cur_period;
    549  1.12.2.1       he 
    550  1.12.2.1       he 		if ((flags & DPARM_SYNC) && period &&
    551  1.12.2.1       he 		    (sdp->isp_devparam[tgt].cur_offset) != 0) {
    552  1.12.2.1       he #if	0
    553  1.12.2.1       he 			/* CAUSES PANICS */
    554  1.12.2.1       he 			static char *m = "%s: bus %d now %s mode\n";
    555  1.12.2.1       he 			u_int16_t r, l;
    556  1.12.2.1       he 			if (bus == 1)
    557  1.12.2.1       he 				r = SXP_PINS_DIFF | SXP_BANK1_SELECT;
    558  1.12.2.1       he 			else
    559  1.12.2.1       he 				r = SXP_PINS_DIFF;
    560  1.12.2.1       he 			l = ISP_READ(isp, r) & ISP1080_MODE_MASK;
    561  1.12.2.1       he 			switch (l) {
    562  1.12.2.1       he 			case ISP1080_LVD_MODE:
    563  1.12.2.1       he 				sdp->isp_lvdmode = 1;
    564  1.12.2.1       he 				printf(m, isp->isp_name, bus, "LVD");
    565  1.12.2.1       he 				break;
    566  1.12.2.1       he 			case ISP1080_HVD_MODE:
    567  1.12.2.1       he 				sdp->isp_diffmode = 1;
    568  1.12.2.1       he 				printf(m, isp->isp_name, bus, "Differential");
    569  1.12.2.1       he 				break;
    570  1.12.2.1       he 			case ISP1080_SE_MODE:
    571  1.12.2.1       he 				sdp->isp_ultramode = 1;
    572  1.12.2.1       he 				printf(m, isp->isp_name, bus, "Single-Ended");
    573  1.12.2.1       he 				break;
    574  1.12.2.1       he 			default:
    575  1.12.2.1       he 				printf("%s: unknown mode on bus %d (0x%x)\n",
    576  1.12.2.1       he 				    isp->isp_name, bus, l);
    577  1.12.2.1       he 				break;
    578  1.12.2.1       he 			}
    579  1.12.2.1       he #endif
    580  1.12.2.1       he 			/*
    581  1.12.2.1       he 			 * There's some ambiguity about our negotiated speed
    582  1.12.2.1       he 			 * if we haven't detected LVD mode correctly (which
    583  1.12.2.1       he 			 * seems to happen, unfortunately). If we're in LVD
    584  1.12.2.1       he 			 * mode, then different rules apply about speed.
    585  1.12.2.1       he 			 */
    586  1.12.2.1       he 			if (sdp->isp_lvdmode || period < 0xc) {
    587  1.12.2.1       he 				switch (period) {
    588  1.12.2.2       he 				case 0x9:
    589  1.12.2.2       he 					mhz = 80;
    590  1.12.2.2       he 					break;
    591  1.12.2.1       he 				case 0xa:
    592  1.12.2.1       he 					mhz = 40;
    593  1.12.2.1       he 					break;
    594  1.12.2.1       he 				case 0xb:
    595  1.12.2.1       he 					mhz = 33;
    596  1.12.2.1       he 					break;
    597  1.12.2.1       he 				case 0xc:
    598  1.12.2.1       he 					mhz = 25;
    599  1.12.2.1       he 					break;
    600  1.12.2.1       he 				default:
    601      1.12   mjacob 					mhz = 1000 / (period * 4);
    602  1.12.2.1       he 					break;
    603      1.12   mjacob 				}
    604       1.9   mjacob 			} else {
    605  1.12.2.1       he 				mhz = 1000 / (period * 4);
    606       1.9   mjacob 			}
    607  1.12.2.1       he 		} else {
    608  1.12.2.1       he 			mhz = 0;
    609  1.12.2.1       he 		}
    610  1.12.2.1       he 		switch (flags & (DPARM_WIDE|DPARM_TQING)) {
    611  1.12.2.1       he 		case DPARM_WIDE:
    612  1.12.2.1       he 			wt = ", 16 bit wide\n";
    613  1.12.2.1       he 			break;
    614  1.12.2.1       he 		case DPARM_TQING:
    615  1.12.2.1       he 			wt = ", Tagged Queueing Enabled\n";
    616  1.12.2.1       he 			break;
    617  1.12.2.1       he 		case DPARM_WIDE|DPARM_TQING:
    618  1.12.2.1       he 			wt = ", 16 bit wide, Tagged Queueing Enabled\n";
    619  1.12.2.1       he 			break;
    620  1.12.2.1       he 		default:
    621  1.12.2.1       he 			wt = "\n";
    622  1.12.2.1       he 			break;
    623  1.12.2.1       he 		}
    624  1.12.2.1       he 		if (mhz) {
    625  1.12.2.1       he 			CFGPRINTF("%s: Bus %d Target %d at %dMHz Max "
    626  1.12.2.1       he 			    "Offset %d%s", isp->isp_name, bus, tgt, mhz,
    627  1.12.2.1       he 			    sdp->isp_devparam[tgt].cur_offset, wt);
    628  1.12.2.1       he 		} else {
    629  1.12.2.1       he 			CFGPRINTF("%s: Bus %d Target %d Async Mode%s",
    630  1.12.2.1       he 			    isp->isp_name, bus, tgt, wt);
    631       1.9   mjacob 		}
    632      1.11   mjacob 		break;
    633  1.12.2.1       he 	}
    634      1.11   mjacob 	case ISPASYNC_BUS_RESET:
    635  1.12.2.1       he 		if (arg)
    636  1.12.2.1       he 			bus = *((int *) arg);
    637  1.12.2.1       he 		else
    638  1.12.2.1       he 			bus = 0;
    639  1.12.2.1       he 		printf("%s: SCSI bus %d reset detected\n", isp->isp_name, bus);
    640      1.11   mjacob 		break;
    641      1.11   mjacob 	case ISPASYNC_LOOP_DOWN:
    642      1.11   mjacob 		/*
    643      1.11   mjacob 		 * Hopefully we get here in time to minimize the number
    644      1.11   mjacob 		 * of commands we are firing off that are sure to die.
    645      1.11   mjacob 		 */
    646      1.11   mjacob 		isp->isp_osinfo.blocked = 1;
    647      1.11   mjacob 		printf("%s: Loop DOWN\n", isp->isp_name);
    648      1.11   mjacob 		break;
    649      1.11   mjacob         case ISPASYNC_LOOP_UP:
    650      1.11   mjacob 		isp->isp_osinfo.blocked = 0;
    651      1.11   mjacob 		timeout(isp_internal_restart, isp, 1);
    652      1.11   mjacob 		printf("%s: Loop UP\n", isp->isp_name);
    653      1.11   mjacob 		break;
    654  1.12.2.1       he 	case ISPASYNC_PDB_CHANGED:
    655  1.12.2.1       he 	if (IS_FC(isp) && isp->isp_dblev) {
    656  1.12.2.1       he 		const char *fmt = "%s: Target %d (Loop 0x%x) Port ID 0x%x "
    657  1.12.2.1       he 		    "role %s %s\n Port WWN 0x%08x%08x\n Node WWN 0x%08x%08x\n";
    658  1.12.2.1       he 		const static char *roles[4] = {
    659      1.11   mjacob 		    "No", "Target", "Initiator", "Target/Initiator"
    660      1.11   mjacob 		};
    661  1.12.2.1       he 		char *ptr;
    662  1.12.2.1       he 		fcparam *fcp = isp->isp_param;
    663  1.12.2.1       he 		int tgt = *((int *) arg);
    664  1.12.2.1       he 		struct lportdb *lp = &fcp->portdb[tgt];
    665  1.12.2.1       he 
    666  1.12.2.1       he 		if (lp->valid) {
    667  1.12.2.1       he 			ptr = "arrived";
    668  1.12.2.1       he 		} else {
    669  1.12.2.1       he 			ptr = "disappeared";
    670      1.11   mjacob 		}
    671  1.12.2.1       he 		printf(fmt, isp->isp_name, tgt, lp->loopid, lp->portid,
    672  1.12.2.1       he 		    roles[lp->roles & 0x3], ptr,
    673  1.12.2.1       he 		    (u_int32_t) (lp->port_wwn >> 32),
    674  1.12.2.1       he 		    (u_int32_t) (lp->port_wwn & 0xffffffffLL),
    675  1.12.2.1       he 		    (u_int32_t) (lp->node_wwn >> 32),
    676  1.12.2.1       he 		    (u_int32_t) (lp->node_wwn & 0xffffffffLL));
    677      1.11   mjacob 		break;
    678      1.11   mjacob 	}
    679  1.12.2.1       he #ifdef	ISP2100_FABRIC
    680      1.11   mjacob 	case ISPASYNC_CHANGE_NOTIFY:
    681      1.11   mjacob 		printf("%s: Name Server Database Changed\n", isp->isp_name);
    682       1.9   mjacob 		break;
    683  1.12.2.1       he 	case ISPASYNC_FABRIC_DEV:
    684  1.12.2.1       he 	{
    685  1.12.2.1       he 		int target;
    686  1.12.2.1       he 		struct lportdb *lp;
    687  1.12.2.1       he 		sns_scrsp_t *resp = (sns_scrsp_t *) arg;
    688  1.12.2.1       he 		u_int32_t portid;
    689  1.12.2.1       he 		u_int64_t wwn;
    690  1.12.2.1       he 		fcparam *fcp = isp->isp_param;
    691  1.12.2.1       he 
    692  1.12.2.1       he 		portid =
    693  1.12.2.1       he 		    (((u_int32_t) resp->snscb_port_id[0]) << 16) |
    694  1.12.2.1       he 		    (((u_int32_t) resp->snscb_port_id[1]) << 8) |
    695  1.12.2.1       he 		    (((u_int32_t) resp->snscb_port_id[2]));
    696  1.12.2.1       he 		wwn =
    697  1.12.2.1       he 		    (((u_int64_t)resp->snscb_portname[0]) << 56) |
    698  1.12.2.1       he 		    (((u_int64_t)resp->snscb_portname[1]) << 48) |
    699  1.12.2.1       he 		    (((u_int64_t)resp->snscb_portname[2]) << 40) |
    700  1.12.2.1       he 		    (((u_int64_t)resp->snscb_portname[3]) << 32) |
    701  1.12.2.1       he 		    (((u_int64_t)resp->snscb_portname[4]) << 24) |
    702  1.12.2.1       he 		    (((u_int64_t)resp->snscb_portname[5]) << 16) |
    703  1.12.2.1       he 		    (((u_int64_t)resp->snscb_portname[6]) <<  8) |
    704  1.12.2.1       he 		    (((u_int64_t)resp->snscb_portname[7]));
    705  1.12.2.1       he 		printf("%s: Fabric Device (Type 0x%x)@PortID 0x%x WWN "
    706  1.12.2.1       he 		    "0x%08x%08x\n", isp->isp_name, resp->snscb_port_type,
    707  1.12.2.1       he 		    portid, ((u_int32_t)(wwn >> 32)),
    708  1.12.2.1       he 		    ((u_int32_t)(wwn & 0xffffffff)));
    709  1.12.2.1       he 		if (resp->snscb_port_type != 2)
    710  1.12.2.1       he 			break;
    711  1.12.2.1       he 		for (target = FC_SNS_ID+1; target < MAX_FC_TARG; target++) {
    712  1.12.2.1       he 			lp = &fcp->portdb[target];
    713  1.12.2.1       he 			if (lp->port_wwn == wwn)
    714  1.12.2.1       he 				break;
    715  1.12.2.1       he 		}
    716  1.12.2.1       he 		if (target < MAX_FC_TARG) {
    717  1.12.2.1       he 			break;
    718  1.12.2.1       he 		}
    719  1.12.2.1       he 		for (target = FC_SNS_ID+1; target < MAX_FC_TARG; target++) {
    720  1.12.2.1       he 			lp = &fcp->portdb[target];
    721  1.12.2.1       he 			if (lp->port_wwn == 0)
    722  1.12.2.1       he 				break;
    723  1.12.2.1       he 		}
    724  1.12.2.1       he 		if (target == MAX_FC_TARG) {
    725  1.12.2.1       he 			printf("%s: no more space for fabric devices\n",
    726  1.12.2.1       he 			    isp->isp_name);
    727  1.12.2.1       he 			return (-1);
    728  1.12.2.1       he 		}
    729  1.12.2.1       he 		lp->port_wwn = lp->node_wwn = wwn;
    730  1.12.2.1       he 		lp->portid = portid;
    731  1.12.2.1       he 		break;
    732  1.12.2.1       he 	}
    733  1.12.2.1       he #endif
    734       1.9   mjacob 	default:
    735       1.9   mjacob 		break;
    736       1.9   mjacob 	}
    737      1.10   mjacob 	(void) splx(s);
    738       1.9   mjacob 	return (0);
    739       1.1   mjacob }
    740