Home | History | Annotate | Line # | Download | only in ic
isp_netbsd.c revision 1.39.2.4
      1  1.39.2.4  nathanw /* $NetBSD: isp_netbsd.c,v 1.39.2.4 2001/09/21 22:35:39 nathanw Exp $ */
      2      1.30   mjacob /*
      3      1.30   mjacob  * This driver, which is contained in NetBSD in the files:
      4      1.30   mjacob  *
      5      1.30   mjacob  *	sys/dev/ic/isp.c
      6      1.34      wiz  *	sys/dev/ic/isp_inline.h
      7      1.34      wiz  *	sys/dev/ic/isp_netbsd.c
      8      1.34      wiz  *	sys/dev/ic/isp_netbsd.h
      9      1.34      wiz  *	sys/dev/ic/isp_target.c
     10      1.34      wiz  *	sys/dev/ic/isp_target.h
     11      1.34      wiz  *	sys/dev/ic/isp_tpublic.h
     12      1.34      wiz  *	sys/dev/ic/ispmbox.h
     13      1.34      wiz  *	sys/dev/ic/ispreg.h
     14      1.34      wiz  *	sys/dev/ic/ispvar.h
     15      1.30   mjacob  *	sys/microcode/isp/asm_sbus.h
     16      1.30   mjacob  *	sys/microcode/isp/asm_1040.h
     17      1.30   mjacob  *	sys/microcode/isp/asm_1080.h
     18      1.30   mjacob  *	sys/microcode/isp/asm_12160.h
     19      1.30   mjacob  *	sys/microcode/isp/asm_2100.h
     20      1.30   mjacob  *	sys/microcode/isp/asm_2200.h
     21      1.30   mjacob  *	sys/pci/isp_pci.c
     22      1.30   mjacob  *	sys/sbus/isp_sbus.c
     23      1.30   mjacob  *
     24      1.30   mjacob  * Is being actively maintained by Matthew Jacob (mjacob (at) netbsd.org).
     25      1.30   mjacob  * This driver also is shared source with FreeBSD, OpenBSD, Linux, Solaris,
     26      1.30   mjacob  * Linux versions. This tends to be an interesting maintenance problem.
     27      1.30   mjacob  *
     28      1.30   mjacob  * Please coordinate with Matthew Jacob on changes you wish to make here.
     29      1.30   mjacob  */
     30       1.1   mjacob /*
     31       1.1   mjacob  * Platform (NetBSD) dependent common attachment code for Qlogic adapters.
     32      1.15   mjacob  * Matthew Jacob <mjacob (at) nas.nasa.gov>
     33      1.15   mjacob  */
     34      1.15   mjacob /*
     35      1.15   mjacob  * Copyright (C) 1997, 1998, 1999 National Aeronautics & Space Administration
     36       1.1   mjacob  * All rights reserved.
     37       1.1   mjacob  *
     38       1.1   mjacob  * Redistribution and use in source and binary forms, with or without
     39       1.1   mjacob  * modification, are permitted provided that the following conditions
     40       1.1   mjacob  * are met:
     41       1.1   mjacob  * 1. Redistributions of source code must retain the above copyright
     42      1.15   mjacob  *    notice, this list of conditions and the following disclaimer.
     43       1.1   mjacob  * 2. Redistributions in binary form must reproduce the above copyright
     44       1.1   mjacob  *    notice, this list of conditions and the following disclaimer in the
     45       1.1   mjacob  *    documentation and/or other materials provided with the distribution.
     46       1.1   mjacob  * 3. The name of the author may not be used to endorse or promote products
     47      1.15   mjacob  *    derived from this software without specific prior written permission
     48       1.1   mjacob  *
     49      1.15   mjacob  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     50      1.15   mjacob  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     51      1.15   mjacob  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     52      1.15   mjacob  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     53      1.15   mjacob  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     54      1.15   mjacob  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     55      1.15   mjacob  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     56      1.15   mjacob  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     57      1.15   mjacob  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     58      1.15   mjacob  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     59       1.1   mjacob  */
     60       1.1   mjacob 
     61       1.1   mjacob #include <dev/ic/isp_netbsd.h>
     62      1.17   mjacob #include <sys/scsiio.h>
     63       1.1   mjacob 
     64      1.27   mjacob 
     65      1.27   mjacob /*
     66      1.27   mjacob  * Set a timeout for the watchdogging of a command.
     67      1.27   mjacob  *
     68      1.27   mjacob  * The dimensional analysis is
     69      1.27   mjacob  *
     70      1.27   mjacob  *	milliseconds * (seconds/millisecond) * (ticks/second) = ticks
     71      1.27   mjacob  *
     72      1.27   mjacob  *			=
     73      1.27   mjacob  *
     74      1.27   mjacob  *	(milliseconds / 1000) * hz = ticks
     75      1.27   mjacob  *
     76      1.27   mjacob  *
     77      1.27   mjacob  * For timeouts less than 1 second, we'll get zero. Because of this, and
     78      1.27   mjacob  * because we want to establish *our* timeout to be longer than what the
     79      1.27   mjacob  * firmware might do, we just add 3 seconds at the back end.
     80      1.27   mjacob  */
     81      1.27   mjacob #define	_XT(xs)	((((xs)->timeout/1000) * hz) + (3 * hz))
     82      1.26   mjacob 
     83  1.39.2.3  nathanw static void isp_config_interrupts(struct device *);
     84  1.39.2.2  nathanw static void ispminphys_1020(struct buf *);
     85  1.39.2.1  nathanw static void ispminphys(struct buf *);
     86  1.39.2.2  nathanw static INLINE void ispcmd(struct ispsoftc *, XS_T *);
     87  1.39.2.2  nathanw static void isprequest(struct scsipi_channel *, scsipi_adapter_req_t, void *);
     88  1.39.2.2  nathanw static int
     89  1.39.2.2  nathanw ispioctl(struct scsipi_channel *, u_long, caddr_t, int, struct proc *);
     90       1.1   mjacob 
     91  1.39.2.2  nathanw static void isp_polled_cmd(struct ispsoftc *, XS_T *);
     92  1.39.2.1  nathanw static void isp_dog(void *);
     93  1.39.2.2  nathanw static void isp_create_fc_worker(void *);
     94  1.39.2.2  nathanw static void isp_fc_worker(void *);
     95      1.10   mjacob 
     96       1.1   mjacob /*
     97       1.1   mjacob  * Complete attachment of hardware, include subdevices.
     98       1.1   mjacob  */
     99       1.1   mjacob void
    100  1.39.2.1  nathanw isp_attach(struct ispsoftc *isp)
    101       1.1   mjacob {
    102       1.1   mjacob 	isp->isp_state = ISP_RUNSTATE;
    103  1.39.2.2  nathanw 
    104  1.39.2.2  nathanw 	isp->isp_osinfo._adapter.adapt_dev = &isp->isp_osinfo._dev;
    105  1.39.2.2  nathanw 	isp->isp_osinfo._adapter.adapt_nchannels = IS_DUALBUS(isp) ? 2 : 1;
    106  1.39.2.2  nathanw 	isp->isp_osinfo._adapter.adapt_openings = isp->isp_maxcmds;
    107  1.39.2.2  nathanw 	/*
    108  1.39.2.2  nathanw 	 * It's not stated whether max_periph is limited by SPI
    109  1.39.2.2  nathanw 	 * tag uage, but let's assume that it is.
    110  1.39.2.2  nathanw 	 */
    111  1.39.2.2  nathanw 	isp->isp_osinfo._adapter.adapt_max_periph = min(isp->isp_maxcmds, 255);
    112  1.39.2.2  nathanw 	isp->isp_osinfo._adapter.adapt_ioctl = ispioctl;
    113  1.39.2.2  nathanw 	isp->isp_osinfo._adapter.adapt_request = isprequest;
    114  1.39.2.2  nathanw 	if (isp->isp_type <= ISP_HA_SCSI_1020A) {
    115  1.39.2.2  nathanw 		isp->isp_osinfo._adapter.adapt_minphys = ispminphys_1020;
    116  1.39.2.2  nathanw 	} else {
    117  1.39.2.2  nathanw 		isp->isp_osinfo._adapter.adapt_minphys = ispminphys;
    118  1.39.2.2  nathanw 	}
    119  1.39.2.2  nathanw 
    120  1.39.2.2  nathanw 	isp->isp_osinfo._chan.chan_adapter = &isp->isp_osinfo._adapter;
    121  1.39.2.2  nathanw 	isp->isp_osinfo._chan.chan_bustype = &scsi_bustype;
    122  1.39.2.2  nathanw 	isp->isp_osinfo._chan.chan_channel = 0;
    123  1.39.2.2  nathanw 
    124      1.28   mjacob 	/*
    125      1.28   mjacob 	 * Until the midlayer is fixed to use REPORT LUNS, limit to 8 luns.
    126      1.28   mjacob 	 */
    127  1.39.2.2  nathanw 	isp->isp_osinfo._chan.chan_nluns = min(isp->isp_maxluns, 8);
    128       1.1   mjacob 
    129      1.15   mjacob 	if (IS_FC(isp)) {
    130  1.39.2.2  nathanw 		isp->isp_osinfo._chan.chan_ntargets = MAX_FC_TARG;
    131  1.39.2.2  nathanw 		isp->isp_osinfo._chan.chan_id = MAX_FC_TARG;
    132  1.39.2.2  nathanw 		isp->isp_osinfo.threadwork = 1;
    133  1.39.2.2  nathanw 		/*
    134  1.39.2.2  nathanw 		 * Note that isp_create_fc_worker won't get called
    135  1.39.2.2  nathanw 		 * until much much later (after proc0 is created).
    136  1.39.2.2  nathanw 		 */
    137  1.39.2.2  nathanw 		kthread_create(isp_create_fc_worker, isp);
    138       1.1   mjacob 	} else {
    139  1.39.2.2  nathanw 		int bus = 0;
    140      1.14   mjacob 		sdparam *sdp = isp->isp_param;
    141  1.39.2.2  nathanw 
    142  1.39.2.2  nathanw 		isp->isp_osinfo._chan.chan_ntargets = MAX_TARGETS;
    143  1.39.2.2  nathanw 		isp->isp_osinfo._chan.chan_id = sdp->isp_initiator_id;
    144      1.17   mjacob 		isp->isp_osinfo.discovered[0] = 1 << sdp->isp_initiator_id;
    145      1.21   mjacob 		if (IS_DUALBUS(isp)) {
    146  1.39.2.2  nathanw 			isp->isp_osinfo._chan_b = isp->isp_osinfo._chan;
    147      1.14   mjacob 			sdp++;
    148      1.18   mjacob 			isp->isp_osinfo.discovered[1] =
    149      1.18   mjacob 			    1 << sdp->isp_initiator_id;
    150  1.39.2.2  nathanw 			isp->isp_osinfo._chan_b.chan_id = sdp->isp_initiator_id;
    151  1.39.2.2  nathanw 			isp->isp_osinfo._chan_b.chan_channel = 1;
    152      1.14   mjacob 		}
    153      1.28   mjacob 		ISP_LOCK(isp);
    154      1.14   mjacob 		(void) isp_control(isp, ISPCTL_RESET_BUS, &bus);
    155      1.21   mjacob 		if (IS_DUALBUS(isp)) {
    156      1.14   mjacob 			bus++;
    157      1.14   mjacob 			(void) isp_control(isp, ISPCTL_RESET_BUS, &bus);
    158      1.14   mjacob 		}
    159      1.29   mjacob 		ISP_UNLOCK(isp);
    160      1.11   mjacob 	}
    161      1.10   mjacob 
    162  1.39.2.3  nathanw 
    163      1.10   mjacob 	/*
    164  1.39.2.3  nathanw          * Defer enabling mailbox interrupts until later.
    165  1.39.2.3  nathanw          */
    166  1.39.2.3  nathanw         config_interrupts((struct device *) isp, isp_config_interrupts);
    167      1.28   mjacob 
    168      1.28   mjacob 	/*
    169       1.8   mjacob 	 * And attach children (if any).
    170       1.8   mjacob 	 */
    171  1.39.2.2  nathanw 	config_found((void *)isp, &isp->isp_chanA, scsiprint);
    172      1.21   mjacob 	if (IS_DUALBUS(isp)) {
    173  1.39.2.2  nathanw 		config_found((void *)isp, &isp->isp_chanB, scsiprint);
    174      1.14   mjacob 	}
    175       1.1   mjacob }
    176       1.1   mjacob 
    177  1.39.2.3  nathanw 
    178  1.39.2.3  nathanw static void
    179  1.39.2.3  nathanw isp_config_interrupts(struct device *self)
    180  1.39.2.3  nathanw {
    181  1.39.2.4  nathanw #if	0
    182  1.39.2.3  nathanw         struct ispsoftc *isp = (struct ispsoftc *) self;
    183  1.39.2.3  nathanw 
    184  1.39.2.3  nathanw 	/*
    185  1.39.2.3  nathanw 	 * After this point, we'll be doing the new configuration
    186  1.39.2.4  nathanw 	 * schema which allows interrupts, so we can do tsleep/wakeup
    187  1.39.2.3  nathanw 	 * for mailbox stuff at that point.
    188  1.39.2.3  nathanw 	 */
    189  1.39.2.4  nathanw 
    190  1.39.2.4  nathanw 	/*
    191  1.39.2.4  nathanw 	 * Argh. We cannot use this until we know whether isprequest
    192  1.39.2.4  nathanw 	 * was *not* called via a hardclock (timed thaw). So- we'll
    193  1.39.2.4  nathanw 	 * only allow a window of the FC kernel thread doing this
    194  1.39.2.4  nathanw 	 * when calling isp_fc_runstate.
    195  1.39.2.4  nathanw 	 */
    196  1.39.2.3  nathanw 	isp->isp_osinfo.no_mbox_ints = 0;
    197  1.39.2.4  nathanw #endif
    198  1.39.2.3  nathanw }
    199  1.39.2.3  nathanw 
    200  1.39.2.3  nathanw 
    201       1.1   mjacob /*
    202       1.1   mjacob  * minphys our xfers
    203       1.1   mjacob  */
    204       1.1   mjacob 
    205       1.1   mjacob static void
    206  1.39.2.2  nathanw ispminphys_1020(struct buf *bp)
    207       1.1   mjacob {
    208       1.1   mjacob 	if (bp->b_bcount >= (1 << 24)) {
    209       1.1   mjacob 		bp->b_bcount = (1 << 24);
    210       1.1   mjacob 	}
    211       1.1   mjacob 	minphys(bp);
    212       1.1   mjacob }
    213       1.1   mjacob 
    214  1.39.2.2  nathanw static void
    215  1.39.2.2  nathanw ispminphys(struct buf *bp)
    216  1.39.2.2  nathanw {
    217  1.39.2.2  nathanw 	if (bp->b_bcount >= (1 << 30)) {
    218  1.39.2.2  nathanw 		bp->b_bcount = (1 << 30);
    219  1.39.2.2  nathanw 	}
    220  1.39.2.2  nathanw 	minphys(bp);
    221  1.39.2.2  nathanw }
    222  1.39.2.2  nathanw 
    223      1.17   mjacob static int
    224  1.39.2.2  nathanw ispioctl(struct scsipi_channel *chan, u_long cmd, caddr_t addr, int flag,
    225  1.39.2.2  nathanw 	struct proc *p)
    226      1.17   mjacob {
    227  1.39.2.2  nathanw 	struct ispsoftc *isp = (void *)chan->chan_adapter->adapt_dev;
    228  1.39.2.2  nathanw 	int retval = ENOTTY;
    229      1.17   mjacob 
    230      1.17   mjacob 	switch (cmd) {
    231  1.39.2.2  nathanw 	case SCBUSIORESET:
    232  1.39.2.2  nathanw 		ISP_LOCK(isp);
    233  1.39.2.2  nathanw 		if (isp_control(isp, ISPCTL_RESET_BUS, &chan->chan_channel))
    234  1.39.2.2  nathanw 			retval = EIO;
    235  1.39.2.2  nathanw 		else
    236  1.39.2.2  nathanw 			retval = 0;
    237  1.39.2.2  nathanw 		ISP_UNLOCK(isp);
    238  1.39.2.2  nathanw 		break;
    239  1.39.2.2  nathanw 	case ISP_SDBLEV:
    240      1.29   mjacob 	{
    241  1.39.2.2  nathanw 		int olddblev = isp->isp_dblev;
    242  1.39.2.2  nathanw 		isp->isp_dblev = *(int *)addr;
    243  1.39.2.2  nathanw 		*(int *)addr = olddblev;
    244      1.29   mjacob 		retval = 0;
    245      1.29   mjacob 		break;
    246      1.29   mjacob 	}
    247  1.39.2.2  nathanw 	case ISP_RESETHBA:
    248  1.39.2.2  nathanw 		ISP_LOCK(isp);
    249  1.39.2.2  nathanw 		isp_reinit(isp);
    250  1.39.2.2  nathanw 		ISP_UNLOCK(isp);
    251  1.39.2.2  nathanw 		retval = 0;
    252  1.39.2.2  nathanw 		break;
    253  1.39.2.2  nathanw 	case ISP_FC_RESCAN:
    254  1.39.2.2  nathanw 		if (IS_FC(isp)) {
    255  1.39.2.2  nathanw 			ISP_LOCK(isp);
    256  1.39.2.2  nathanw 			if (isp_fc_runstate(isp, 5 * 1000000)) {
    257  1.39.2.2  nathanw 				retval = EIO;
    258  1.39.2.2  nathanw 			} else {
    259  1.39.2.2  nathanw 				retval = 0;
    260  1.39.2.2  nathanw 			}
    261  1.39.2.2  nathanw 			ISP_UNLOCK(isp);
    262  1.39.2.2  nathanw 		}
    263  1.39.2.2  nathanw 		break;
    264  1.39.2.2  nathanw 	case ISP_FC_LIP:
    265  1.39.2.2  nathanw 		if (IS_FC(isp)) {
    266  1.39.2.2  nathanw 			ISP_LOCK(isp);
    267  1.39.2.2  nathanw 			if (isp_control(isp, ISPCTL_SEND_LIP, 0)) {
    268  1.39.2.2  nathanw 				retval = EIO;
    269  1.39.2.2  nathanw 			} else {
    270  1.39.2.2  nathanw 				retval = 0;
    271  1.39.2.2  nathanw 			}
    272  1.39.2.2  nathanw 			ISP_UNLOCK(isp);
    273  1.39.2.2  nathanw 		}
    274  1.39.2.2  nathanw 		break;
    275  1.39.2.2  nathanw 	case ISP_FC_GETDINFO:
    276  1.39.2.2  nathanw 	{
    277  1.39.2.2  nathanw 		struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
    278  1.39.2.2  nathanw 		struct lportdb *lp;
    279  1.39.2.2  nathanw 
    280  1.39.2.2  nathanw 		if (ifc->loopid < 0 || ifc->loopid >= MAX_FC_TARG) {
    281  1.39.2.2  nathanw 			retval = EINVAL;
    282  1.39.2.2  nathanw 			break;
    283  1.39.2.2  nathanw 		}
    284  1.39.2.2  nathanw 		ISP_LOCK(isp);
    285  1.39.2.2  nathanw 		lp = &FCPARAM(isp)->portdb[ifc->loopid];
    286  1.39.2.2  nathanw 		if (lp->valid) {
    287  1.39.2.2  nathanw 			ifc->loopid = lp->loopid;
    288  1.39.2.2  nathanw 			ifc->portid = lp->portid;
    289  1.39.2.2  nathanw 			ifc->node_wwn = lp->node_wwn;
    290  1.39.2.2  nathanw 			ifc->port_wwn = lp->port_wwn;
    291      1.17   mjacob 			retval = 0;
    292  1.39.2.2  nathanw 		} else {
    293  1.39.2.2  nathanw 			retval = ENODEV;
    294  1.39.2.2  nathanw 		}
    295  1.39.2.2  nathanw 		ISP_UNLOCK(isp);
    296      1.17   mjacob 		break;
    297  1.39.2.2  nathanw 	}
    298      1.17   mjacob 	default:
    299      1.17   mjacob 		break;
    300       1.3   mjacob 	}
    301      1.17   mjacob 	return (retval);
    302      1.17   mjacob }
    303      1.17   mjacob 
    304  1.39.2.2  nathanw static INLINE void
    305  1.39.2.2  nathanw ispcmd(struct ispsoftc *isp, XS_T *xs)
    306      1.17   mjacob {
    307  1.39.2.2  nathanw 	ISP_LOCK(isp);
    308      1.11   mjacob 	if (isp->isp_state < ISP_RUNSTATE) {
    309      1.11   mjacob 		DISABLE_INTS(isp);
    310      1.11   mjacob 		isp_init(isp);
    311  1.39.2.2  nathanw 		if (isp->isp_state != ISP_INITSTATE) {
    312      1.11   mjacob 			ENABLE_INTS(isp);
    313  1.39.2.2  nathanw 			ISP_UNLOCK(isp);
    314  1.39.2.2  nathanw 			XS_SETERR(xs, HBA_BOTCH);
    315  1.39.2.2  nathanw 			scsipi_done(xs);
    316  1.39.2.2  nathanw 			return;
    317  1.39.2.2  nathanw 		}
    318  1.39.2.2  nathanw 		isp->isp_state = ISP_RUNSTATE;
    319      1.11   mjacob 		ENABLE_INTS(isp);
    320  1.39.2.2  nathanw 	}
    321      1.10   mjacob 	/*
    322  1.39.2.2  nathanw 	 * Handle the case of a FC card where the FC thread hasn't
    323  1.39.2.2  nathanw 	 * fired up yet and we have loop state to clean up. If we
    324  1.39.2.2  nathanw 	 * can't clear things up and we've never seen loop up, bounce
    325  1.39.2.2  nathanw 	 * the command.
    326      1.10   mjacob 	 */
    327  1.39.2.2  nathanw 	if (IS_FC(isp) && isp->isp_osinfo.threadwork &&
    328  1.39.2.2  nathanw 	    isp->isp_osinfo.thread == 0) {
    329  1.39.2.2  nathanw 		volatile u_int8_t ombi = isp->isp_osinfo.no_mbox_ints;
    330  1.39.2.2  nathanw 		int delay_time;
    331  1.39.2.2  nathanw 
    332      1.16  thorpej 		if (xs->xs_control & XS_CTL_POLL) {
    333  1.39.2.2  nathanw 			isp->isp_osinfo.no_mbox_ints = 1;
    334      1.10   mjacob 		}
    335  1.39.2.2  nathanw 
    336  1.39.2.2  nathanw 		if (isp->isp_osinfo.loop_checked == 0) {
    337  1.39.2.2  nathanw 			delay_time = 10 * 1000000;
    338  1.39.2.2  nathanw 			isp->isp_osinfo.loop_checked = 1;
    339  1.39.2.2  nathanw 		} else {
    340  1.39.2.2  nathanw 			delay_time = 250000;
    341  1.39.2.2  nathanw 		}
    342  1.39.2.2  nathanw 
    343  1.39.2.2  nathanw 		if (isp_fc_runstate(isp, delay_time) != 0) {
    344  1.39.2.2  nathanw 			if (xs->xs_control & XS_CTL_POLL) {
    345  1.39.2.2  nathanw 				isp->isp_osinfo.no_mbox_ints = ombi;
    346  1.39.2.2  nathanw 			}
    347  1.39.2.2  nathanw 			if (FCPARAM(isp)->loop_seen_once == 0) {
    348  1.39.2.2  nathanw 				XS_SETERR(xs, HBA_SELTIMEOUT);
    349  1.39.2.2  nathanw 				scsipi_done(xs);
    350  1.39.2.2  nathanw 				ISP_UNLOCK(isp);
    351  1.39.2.2  nathanw 				return;
    352  1.39.2.2  nathanw 			}
    353  1.39.2.2  nathanw 			/*
    354  1.39.2.2  nathanw 			 * Otherwise, fall thru to be queued up for later.
    355  1.39.2.2  nathanw 			 */
    356  1.39.2.2  nathanw 		} else {
    357  1.39.2.2  nathanw 			int wasblocked =
    358  1.39.2.2  nathanw 			    (isp->isp_osinfo.blocked || isp->isp_osinfo.paused);
    359  1.39.2.2  nathanw 			isp->isp_osinfo.threadwork = 0;
    360  1.39.2.2  nathanw 			isp->isp_osinfo.blocked =
    361  1.39.2.2  nathanw 			    isp->isp_osinfo.paused = 0;
    362  1.39.2.2  nathanw 			if (wasblocked) {
    363  1.39.2.2  nathanw 				scsipi_channel_thaw(&isp->isp_chanA, 1);
    364  1.39.2.2  nathanw 			}
    365  1.39.2.2  nathanw 		}
    366  1.39.2.2  nathanw 		if (xs->xs_control & XS_CTL_POLL) {
    367  1.39.2.2  nathanw 			isp->isp_osinfo.no_mbox_ints = ombi;
    368  1.39.2.2  nathanw 		}
    369  1.39.2.2  nathanw 	}
    370  1.39.2.2  nathanw 
    371  1.39.2.2  nathanw 	if (isp->isp_osinfo.paused) {
    372  1.39.2.2  nathanw 		isp_prt(isp, ISP_LOGWARN, "I/O while paused");
    373  1.39.2.2  nathanw 		xs->error = XS_RESOURCE_SHORTAGE;
    374  1.39.2.2  nathanw 		scsipi_done(xs);
    375  1.39.2.2  nathanw 		ISP_UNLOCK(isp);
    376  1.39.2.2  nathanw 		return;
    377  1.39.2.2  nathanw 	}
    378  1.39.2.2  nathanw 	if (isp->isp_osinfo.blocked) {
    379  1.39.2.2  nathanw 		isp_prt(isp, ISP_LOGWARN, "I/O while blocked");
    380  1.39.2.2  nathanw 		xs->error = XS_REQUEUE;
    381  1.39.2.2  nathanw 		scsipi_done(xs);
    382  1.39.2.2  nathanw 		ISP_UNLOCK(isp);
    383  1.39.2.2  nathanw 		return;
    384      1.10   mjacob 	}
    385      1.17   mjacob 
    386      1.26   mjacob 	if (xs->xs_control & XS_CTL_POLL) {
    387      1.28   mjacob 		volatile u_int8_t ombi = isp->isp_osinfo.no_mbox_ints;
    388      1.28   mjacob 		isp->isp_osinfo.no_mbox_ints = 1;
    389  1.39.2.2  nathanw 		isp_polled_cmd(isp, xs);
    390      1.28   mjacob 		isp->isp_osinfo.no_mbox_ints = ombi;
    391  1.39.2.2  nathanw 		ISP_UNLOCK(isp);
    392  1.39.2.2  nathanw 		return;
    393       1.1   mjacob 	}
    394       1.1   mjacob 
    395  1.39.2.2  nathanw 	switch (isp_start(xs)) {
    396      1.26   mjacob 	case CMD_QUEUED:
    397      1.26   mjacob 		if (xs->timeout) {
    398      1.26   mjacob 			callout_reset(&xs->xs_callout, _XT(xs), isp_dog, xs);
    399      1.26   mjacob 		}
    400      1.26   mjacob 		break;
    401      1.26   mjacob 	case CMD_EAGAIN:
    402  1.39.2.2  nathanw 		isp->isp_osinfo.paused = 1;
    403  1.39.2.2  nathanw 		xs->error = XS_RESOURCE_SHORTAGE;
    404  1.39.2.2  nathanw 		scsipi_channel_freeze(&isp->isp_chanA, 1);
    405  1.39.2.2  nathanw 		if (IS_DUALBUS(isp)) {
    406  1.39.2.2  nathanw 			scsipi_channel_freeze(&isp->isp_chanB, 1);
    407  1.39.2.2  nathanw 		}
    408  1.39.2.2  nathanw 		scsipi_done(xs);
    409      1.26   mjacob 		break;
    410      1.26   mjacob 	case CMD_RQLATER:
    411  1.39.2.2  nathanw 		/*
    412  1.39.2.2  nathanw 		 * We can only get RQLATER from FC devices (1 channel only)
    413  1.39.2.2  nathanw 		 *
    414  1.39.2.2  nathanw 		 * Also, if we've never seen loop up, bounce the command
    415  1.39.2.2  nathanw 		 * (somebody has booted with no FC cable connected)
    416  1.39.2.2  nathanw 		 */
    417  1.39.2.2  nathanw 		if (FCPARAM(isp)->loop_seen_once == 0) {
    418  1.39.2.2  nathanw 			XS_SETERR(xs, HBA_SELTIMEOUT);
    419  1.39.2.2  nathanw 			scsipi_done(xs);
    420  1.39.2.2  nathanw 			break;
    421  1.39.2.2  nathanw 		}
    422  1.39.2.2  nathanw 		if (isp->isp_osinfo.blocked == 0) {
    423  1.39.2.2  nathanw 			isp->isp_osinfo.blocked = 1;
    424  1.39.2.2  nathanw 			scsipi_channel_freeze(&isp->isp_chanA, 1);
    425  1.39.2.2  nathanw 		}
    426  1.39.2.2  nathanw 		xs->error = XS_REQUEUE;
    427  1.39.2.2  nathanw 		scsipi_done(xs);
    428      1.26   mjacob 		break;
    429      1.26   mjacob 	case CMD_COMPLETE:
    430  1.39.2.2  nathanw 		scsipi_done(xs);
    431      1.26   mjacob 		break;
    432      1.26   mjacob 	}
    433  1.39.2.2  nathanw 	ISP_UNLOCK(isp);
    434      1.26   mjacob }
    435      1.26   mjacob 
    436  1.39.2.2  nathanw static void
    437  1.39.2.2  nathanw isprequest(struct scsipi_channel *chan, scsipi_adapter_req_t req, void *arg)
    438  1.39.2.2  nathanw {
    439  1.39.2.2  nathanw 	struct ispsoftc *isp = (void *)chan->chan_adapter->adapt_dev;
    440  1.39.2.2  nathanw 
    441  1.39.2.2  nathanw 	switch (req) {
    442  1.39.2.2  nathanw 	case ADAPTER_REQ_RUN_XFER:
    443  1.39.2.2  nathanw 		ispcmd(isp, (XS_T *) arg);
    444  1.39.2.2  nathanw 		break;
    445  1.39.2.2  nathanw 
    446  1.39.2.2  nathanw 	case ADAPTER_REQ_GROW_RESOURCES:
    447  1.39.2.2  nathanw 		/* Not supported. */
    448  1.39.2.2  nathanw 		break;
    449  1.39.2.2  nathanw 
    450  1.39.2.2  nathanw 	case ADAPTER_REQ_SET_XFER_MODE:
    451  1.39.2.2  nathanw 	if (IS_SCSI(isp)) {
    452  1.39.2.2  nathanw 		struct scsipi_xfer_mode *xm = arg;
    453  1.39.2.2  nathanw 		int dflags = 0;
    454  1.39.2.2  nathanw 		sdparam *sdp = SDPARAM(isp);
    455  1.39.2.2  nathanw 
    456  1.39.2.2  nathanw 		sdp += chan->chan_channel;
    457  1.39.2.2  nathanw 		if (xm->xm_mode & PERIPH_CAP_TQING)
    458  1.39.2.2  nathanw 			dflags |= DPARM_TQING;
    459  1.39.2.2  nathanw 		if (xm->xm_mode & PERIPH_CAP_WIDE16)
    460  1.39.2.2  nathanw 			dflags |= DPARM_WIDE;
    461  1.39.2.2  nathanw 		if (xm->xm_mode & PERIPH_CAP_SYNC)
    462  1.39.2.2  nathanw 			dflags |= DPARM_SYNC;
    463  1.39.2.2  nathanw 		ISP_LOCK(isp);
    464  1.39.2.4  nathanw 		sdp->isp_devparam[xm->xm_target].goal_flags |= dflags;
    465  1.39.2.4  nathanw 		dflags = sdp->isp_devparam[xm->xm_target].goal_flags;
    466  1.39.2.2  nathanw 		sdp->isp_devparam[xm->xm_target].dev_update = 1;
    467  1.39.2.2  nathanw 		isp->isp_update |= (1 << chan->chan_channel);
    468  1.39.2.2  nathanw 		ISP_UNLOCK(isp);
    469  1.39.2.2  nathanw 		isp_prt(isp, ISP_LOGDEBUG1,
    470  1.39.2.2  nathanw 		    "ispioctl: device flags 0x%x for %d.%d.X",
    471  1.39.2.2  nathanw 		    dflags, chan->chan_channel, xm->xm_target);
    472  1.39.2.2  nathanw 		break;
    473  1.39.2.2  nathanw 	}
    474  1.39.2.2  nathanw 	default:
    475  1.39.2.2  nathanw 		break;
    476  1.39.2.2  nathanw 	}
    477  1.39.2.2  nathanw }
    478  1.39.2.2  nathanw 
    479  1.39.2.2  nathanw static void
    480  1.39.2.1  nathanw isp_polled_cmd(struct ispsoftc *isp, XS_T *xs)
    481      1.26   mjacob {
    482      1.26   mjacob 	int result;
    483      1.26   mjacob 	int infinite = 0, mswait;
    484      1.26   mjacob 
    485      1.28   mjacob 	result = isp_start(xs);
    486      1.26   mjacob 
    487      1.17   mjacob 	switch (result) {
    488      1.17   mjacob 	case CMD_QUEUED:
    489      1.17   mjacob 		break;
    490      1.17   mjacob 	case CMD_RQLATER:
    491  1.39.2.2  nathanw 		if (XS_NOERR(xs)) {
    492  1.39.2.2  nathanw 			xs->error = XS_REQUEUE;
    493  1.39.2.2  nathanw 		}
    494      1.17   mjacob 	case CMD_EAGAIN:
    495      1.17   mjacob 		if (XS_NOERR(xs)) {
    496  1.39.2.2  nathanw 			xs->error = XS_RESOURCE_SHORTAGE;
    497      1.17   mjacob 		}
    498  1.39.2.2  nathanw 		/* FALLTHROUGH */
    499      1.17   mjacob 	case CMD_COMPLETE:
    500  1.39.2.2  nathanw 		scsipi_done(xs);
    501  1.39.2.2  nathanw 		return;
    502      1.17   mjacob 
    503      1.17   mjacob 	}
    504      1.26   mjacob 
    505       1.1   mjacob 	/*
    506       1.1   mjacob 	 * If we can't use interrupts, poll on completion.
    507       1.1   mjacob 	 */
    508      1.26   mjacob 	if ((mswait = XS_TIME(xs)) == 0)
    509      1.26   mjacob 		infinite = 1;
    510      1.26   mjacob 
    511      1.26   mjacob 	while (mswait || infinite) {
    512  1.39.2.4  nathanw 		u_int16_t isr, sema, mbox;
    513  1.39.2.4  nathanw 		if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
    514  1.39.2.4  nathanw 			isp_intr(isp, isr, sema, mbox);
    515      1.26   mjacob 			if (XS_CMD_DONE_P(xs)) {
    516      1.26   mjacob 				break;
    517       1.1   mjacob 			}
    518       1.1   mjacob 		}
    519      1.28   mjacob 		USEC_DELAY(1000);
    520      1.26   mjacob 		mswait -= 1;
    521      1.26   mjacob 	}
    522      1.26   mjacob 
    523      1.26   mjacob 	/*
    524      1.26   mjacob 	 * If no other error occurred but we didn't finish,
    525      1.26   mjacob 	 * something bad happened.
    526      1.26   mjacob 	 */
    527      1.26   mjacob 	if (XS_CMD_DONE_P(xs) == 0) {
    528      1.26   mjacob 		if (isp_control(isp, ISPCTL_ABORT_CMD, xs)) {
    529      1.28   mjacob 			isp_reinit(isp);
    530      1.26   mjacob 		}
    531      1.26   mjacob 		if (XS_NOERR(xs)) {
    532      1.26   mjacob 			XS_SETERR(xs, HBA_BOTCH);
    533      1.26   mjacob 		}
    534       1.1   mjacob 	}
    535  1.39.2.2  nathanw 	scsipi_done(xs);
    536       1.1   mjacob }
    537       1.1   mjacob 
    538      1.26   mjacob void
    539  1.39.2.1  nathanw isp_done(XS_T *xs)
    540       1.1   mjacob {
    541      1.26   mjacob 	XS_CMD_S_DONE(xs);
    542      1.26   mjacob 	if (XS_CMD_WDOG_P(xs) == 0) {
    543      1.26   mjacob 		struct ispsoftc *isp = XS_ISP(xs);
    544      1.26   mjacob 		callout_stop(&xs->xs_callout);
    545      1.26   mjacob 		if (XS_CMD_GRACE_P(xs)) {
    546      1.28   mjacob 			isp_prt(isp, ISP_LOGDEBUG1,
    547      1.28   mjacob 			    "finished command on borrowed time");
    548       1.1   mjacob 		}
    549      1.26   mjacob 		XS_CMD_S_CLEAR(xs);
    550  1.39.2.2  nathanw 		/*
    551  1.39.2.2  nathanw 		 * Fixup- if we get a QFULL, we need
    552  1.39.2.2  nathanw 		 * to set XS_BUSY as the error.
    553  1.39.2.2  nathanw 		 */
    554  1.39.2.2  nathanw 		if (xs->status == SCSI_QUEUE_FULL) {
    555  1.39.2.2  nathanw 			xs->error = XS_BUSY;
    556  1.39.2.2  nathanw 		}
    557  1.39.2.2  nathanw 		if (isp->isp_osinfo.paused) {
    558  1.39.2.2  nathanw 			isp->isp_osinfo.paused = 0;
    559  1.39.2.2  nathanw 			scsipi_channel_timed_thaw(&isp->isp_chanA);
    560  1.39.2.2  nathanw 			if (IS_DUALBUS(isp)) {
    561  1.39.2.2  nathanw 				scsipi_channel_timed_thaw(&isp->isp_chanB);
    562  1.39.2.2  nathanw 			}
    563  1.39.2.2  nathanw 		}
    564      1.26   mjacob 		scsipi_done(xs);
    565       1.1   mjacob 	}
    566       1.8   mjacob }
    567       1.8   mjacob 
    568       1.8   mjacob static void
    569  1.39.2.1  nathanw isp_dog(void *arg)
    570       1.8   mjacob {
    571      1.28   mjacob 	XS_T *xs = arg;
    572      1.26   mjacob 	struct ispsoftc *isp = XS_ISP(xs);
    573  1.39.2.1  nathanw 	u_int16_t handle;
    574       1.8   mjacob 
    575      1.30   mjacob 	ISP_ILOCK(isp);
    576       1.8   mjacob 	/*
    577      1.26   mjacob 	 * We've decided this command is dead. Make sure we're not trying
    578      1.26   mjacob 	 * to kill a command that's already dead by getting it's handle and
    579      1.26   mjacob 	 * and seeing whether it's still alive.
    580      1.26   mjacob 	 */
    581      1.26   mjacob 	handle = isp_find_handle(isp, xs);
    582      1.26   mjacob 	if (handle) {
    583  1.39.2.4  nathanw 		u_int16_t isr, mbox, sema;
    584      1.26   mjacob 
    585      1.26   mjacob 		if (XS_CMD_DONE_P(xs)) {
    586      1.28   mjacob 			isp_prt(isp, ISP_LOGDEBUG1,
    587      1.28   mjacob 			    "watchdog found done cmd (handle 0x%x)", handle);
    588      1.30   mjacob 			ISP_IUNLOCK(isp);
    589      1.26   mjacob 			return;
    590      1.26   mjacob 		}
    591      1.26   mjacob 
    592      1.26   mjacob 		if (XS_CMD_WDOG_P(xs)) {
    593      1.28   mjacob 			isp_prt(isp, ISP_LOGDEBUG1,
    594      1.28   mjacob 			    "recursive watchdog (handle 0x%x)", handle);
    595      1.30   mjacob 			ISP_IUNLOCK(isp);
    596      1.26   mjacob 			return;
    597      1.26   mjacob 		}
    598      1.26   mjacob 
    599      1.26   mjacob 		XS_CMD_S_WDOG(xs);
    600      1.26   mjacob 
    601  1.39.2.4  nathanw 		if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
    602  1.39.2.4  nathanw 			isp_intr(isp, isr, sema, mbox);
    603  1.39.2.4  nathanw 
    604  1.39.2.4  nathanw 		}
    605  1.39.2.4  nathanw 		if (XS_CMD_DONE_P(xs)) {
    606  1.39.2.4  nathanw 			isp_prt(isp, ISP_LOGDEBUG1,
    607  1.39.2.4  nathanw 			    "watchdog cleanup for handle 0x%x", handle);
    608      1.26   mjacob 			XS_CMD_C_WDOG(xs);
    609      1.26   mjacob 			isp_done(xs);
    610      1.26   mjacob 		} else if (XS_CMD_GRACE_P(xs)) {
    611  1.39.2.4  nathanw 			isp_prt(isp, ISP_LOGDEBUG1,
    612  1.39.2.4  nathanw 			    "watchdog timeout for handle 0x%x", handle);
    613      1.26   mjacob 			/*
    614      1.26   mjacob 			 * Make sure the command is *really* dead before we
    615      1.26   mjacob 			 * release the handle (and DMA resources) for reuse.
    616      1.26   mjacob 			 */
    617      1.26   mjacob 			(void) isp_control(isp, ISPCTL_ABORT_CMD, arg);
    618      1.26   mjacob 
    619      1.26   mjacob 			/*
    620      1.26   mjacob 			 * After this point, the comamnd is really dead.
    621      1.26   mjacob 			 */
    622      1.26   mjacob 			if (XS_XFRLEN(xs)) {
    623      1.26   mjacob 				ISP_DMAFREE(isp, xs, handle);
    624      1.26   mjacob 			}
    625      1.26   mjacob 			isp_destroy_handle(isp, handle);
    626      1.26   mjacob 			XS_SETERR(xs, XS_TIMEOUT);
    627      1.26   mjacob 			XS_CMD_S_CLEAR(xs);
    628      1.26   mjacob 			isp_done(xs);
    629      1.26   mjacob 		} else {
    630      1.26   mjacob 			u_int16_t iptr, optr;
    631      1.26   mjacob 			ispreq_t *mp;
    632      1.28   mjacob 			isp_prt(isp, ISP_LOGDEBUG2,
    633  1.39.2.4  nathanw 			    "possible command timeout on handle %x", handle);
    634      1.26   mjacob 			XS_CMD_C_WDOG(xs);
    635      1.26   mjacob 			callout_reset(&xs->xs_callout, hz, isp_dog, xs);
    636      1.26   mjacob 			if (isp_getrqentry(isp, &iptr, &optr, (void **) &mp)) {
    637      1.33   mjacob 				ISP_UNLOCK(isp);
    638      1.26   mjacob 				return;
    639      1.26   mjacob 			}
    640      1.26   mjacob 			XS_CMD_S_GRACE(xs);
    641      1.26   mjacob 			MEMZERO((void *) mp, sizeof (*mp));
    642      1.26   mjacob 			mp->req_header.rqs_entry_count = 1;
    643      1.26   mjacob 			mp->req_header.rqs_entry_type = RQSTYPE_MARKER;
    644      1.26   mjacob 			mp->req_modifier = SYNC_ALL;
    645      1.26   mjacob 			mp->req_target = XS_CHANNEL(xs) << 7;
    646      1.26   mjacob 			ISP_SWIZZLE_REQUEST(isp, mp);
    647      1.26   mjacob 			ISP_ADD_REQUEST(isp, iptr);
    648       1.8   mjacob 		}
    649      1.30   mjacob 	} else {
    650      1.30   mjacob 		isp_prt(isp, ISP_LOGDEBUG0, "watchdog with no command");
    651       1.8   mjacob 	}
    652      1.30   mjacob 	ISP_IUNLOCK(isp);
    653       1.8   mjacob }
    654       1.8   mjacob 
    655       1.8   mjacob /*
    656  1.39.2.2  nathanw  * Fibre Channel state cleanup thread
    657  1.39.2.2  nathanw  */
    658  1.39.2.2  nathanw static void
    659  1.39.2.2  nathanw isp_create_fc_worker(void *arg)
    660  1.39.2.2  nathanw {
    661  1.39.2.2  nathanw 	struct ispsoftc *isp = arg;
    662  1.39.2.2  nathanw 
    663  1.39.2.2  nathanw 	if (kthread_create1(isp_fc_worker, isp, &isp->isp_osinfo.thread,
    664  1.39.2.2  nathanw 	    "%s:fc_thrd", isp->isp_name)) {
    665  1.39.2.2  nathanw 		isp_prt(isp, ISP_LOGERR, "unable to create FC worker thread");
    666  1.39.2.2  nathanw 		panic("isp_create_fc_worker");
    667  1.39.2.2  nathanw 	}
    668  1.39.2.2  nathanw 
    669  1.39.2.2  nathanw }
    670  1.39.2.2  nathanw 
    671  1.39.2.2  nathanw static void
    672  1.39.2.2  nathanw isp_fc_worker(void *arg)
    673  1.39.2.2  nathanw {
    674  1.39.2.2  nathanw 	void scsipi_run_queue(struct scsipi_channel *);
    675  1.39.2.2  nathanw 	struct ispsoftc *isp = arg;
    676  1.39.2.2  nathanw 
    677  1.39.2.2  nathanw 	for (;;) {
    678  1.39.2.2  nathanw 		int s;
    679  1.39.2.2  nathanw 
    680  1.39.2.2  nathanw 		/*
    681  1.39.2.2  nathanw 		 * Note we do *not* use the ISP_LOCK/ISP_UNLOCK macros here.
    682  1.39.2.2  nathanw 		 */
    683  1.39.2.2  nathanw 		s = splbio();
    684  1.39.2.2  nathanw 		while (isp->isp_osinfo.threadwork) {
    685  1.39.2.4  nathanw 			int omb, r;
    686  1.39.2.2  nathanw 			isp->isp_osinfo.threadwork = 0;
    687  1.39.2.4  nathanw 			omb = isp->isp_osinfo.no_mbox_ints;
    688  1.39.2.4  nathanw 			isp->isp_osinfo.no_mbox_ints = 0;
    689  1.39.2.4  nathanw 			r = isp_fc_runstate(isp, 10 * 1000000);
    690  1.39.2.4  nathanw 			isp->isp_osinfo.no_mbox_ints = omb;
    691  1.39.2.4  nathanw 			if (r) {
    692  1.39.2.2  nathanw 				break;
    693  1.39.2.2  nathanw 			}
    694  1.39.2.4  nathanw 			if  (isp->isp_osinfo.loop_checked &&
    695  1.39.2.4  nathanw 			     FCPARAM(isp)->loop_seen_once == 0) {
    696  1.39.2.4  nathanw 				splx(s);
    697  1.39.2.4  nathanw 				goto skip;
    698  1.39.2.4  nathanw 			}
    699  1.39.2.2  nathanw 			isp->isp_osinfo.threadwork = 1;
    700  1.39.2.2  nathanw 			splx(s);
    701  1.39.2.2  nathanw 			delay(500 * 1000);
    702  1.39.2.2  nathanw 			s = splbio();
    703  1.39.2.2  nathanw 		}
    704  1.39.2.2  nathanw 		if (FCPARAM(isp)->isp_fwstate != FW_READY ||
    705  1.39.2.2  nathanw 		    FCPARAM(isp)->isp_loopstate != LOOP_READY) {
    706  1.39.2.2  nathanw 			isp_prt(isp, ISP_LOGINFO, "isp_fc_runstate in vain");
    707  1.39.2.2  nathanw 			isp->isp_osinfo.threadwork = 1;
    708  1.39.2.2  nathanw 			splx(s);
    709  1.39.2.2  nathanw 			continue;
    710  1.39.2.2  nathanw 		}
    711  1.39.2.2  nathanw 
    712  1.39.2.2  nathanw 		if (isp->isp_osinfo.blocked) {
    713  1.39.2.2  nathanw 			isp->isp_osinfo.blocked = 0;
    714  1.39.2.4  nathanw 			isp_prt(isp, ISP_LOGDEBUG0,
    715  1.39.2.4  nathanw 			    "restarting queues (freeze count %d)",
    716  1.39.2.4  nathanw 			    isp->isp_chanA.chan_qfreeze);
    717  1.39.2.2  nathanw 			scsipi_channel_thaw(&isp->isp_chanA, 1);
    718  1.39.2.2  nathanw 		}
    719  1.39.2.2  nathanw 
    720  1.39.2.2  nathanw 		if (isp->isp_osinfo.thread == NULL)
    721  1.39.2.2  nathanw 			break;
    722  1.39.2.2  nathanw 
    723  1.39.2.4  nathanw skip:
    724  1.39.2.2  nathanw 		(void) tsleep(&isp->isp_osinfo.thread, PRIBIO, "fcclnup", 0);
    725  1.39.2.2  nathanw 
    726  1.39.2.2  nathanw 		splx(s);
    727  1.39.2.2  nathanw 	}
    728  1.39.2.2  nathanw 
    729  1.39.2.2  nathanw 	/* In case parent is waiting for us to exit. */
    730  1.39.2.2  nathanw 	wakeup(&isp->isp_osinfo.thread);
    731  1.39.2.2  nathanw 
    732  1.39.2.2  nathanw 	kthread_exit(0);
    733  1.39.2.2  nathanw }
    734  1.39.2.2  nathanw 
    735  1.39.2.2  nathanw /*
    736       1.8   mjacob  * Free any associated resources prior to decommissioning and
    737       1.8   mjacob  * set the card to a known state (so it doesn't wake up and kick
    738       1.8   mjacob  * us when we aren't expecting it to).
    739       1.8   mjacob  *
    740       1.8   mjacob  * Locks are held before coming here.
    741       1.8   mjacob  */
    742       1.8   mjacob void
    743  1.39.2.1  nathanw isp_uninit(struct ispsoftc *isp)
    744       1.8   mjacob {
    745      1.28   mjacob 	isp_lock(isp);
    746       1.8   mjacob 	/*
    747       1.8   mjacob 	 * Leave with interrupts disabled.
    748       1.8   mjacob 	 */
    749       1.8   mjacob 	DISABLE_INTS(isp);
    750      1.28   mjacob 	isp_unlock(isp);
    751       1.9   mjacob }
    752       1.9   mjacob 
    753       1.9   mjacob int
    754  1.39.2.1  nathanw isp_async(struct ispsoftc *isp, ispasync_t cmd, void *arg)
    755       1.9   mjacob {
    756      1.14   mjacob 	int bus, tgt;
    757  1.39.2.2  nathanw 
    758       1.9   mjacob 	switch (cmd) {
    759       1.9   mjacob 	case ISPASYNC_NEW_TGT_PARAMS:
    760      1.17   mjacob 	if (IS_SCSI(isp) && isp->isp_dblev) {
    761      1.17   mjacob 		sdparam *sdp = isp->isp_param;
    762  1.39.2.2  nathanw 		int flags;
    763  1.39.2.2  nathanw 		struct scsipi_xfer_mode xm;
    764      1.17   mjacob 
    765      1.17   mjacob 		tgt = *((int *) arg);
    766      1.17   mjacob 		bus = (tgt >> 16) & 0xffff;
    767      1.17   mjacob 		tgt &= 0xffff;
    768      1.21   mjacob 		sdp += bus;
    769  1.39.2.4  nathanw 		flags = sdp->isp_devparam[tgt].actv_flags;
    770      1.21   mjacob 
    771  1.39.2.2  nathanw 		xm.xm_mode = 0;
    772  1.39.2.4  nathanw 		xm.xm_period = sdp->isp_devparam[tgt].actv_period;
    773  1.39.2.4  nathanw 		xm.xm_offset = sdp->isp_devparam[tgt].actv_offset;
    774  1.39.2.2  nathanw 		xm.xm_target = tgt;
    775  1.39.2.2  nathanw 
    776  1.39.2.2  nathanw 		if ((flags & DPARM_SYNC) && xm.xm_period && xm.xm_offset)
    777  1.39.2.2  nathanw 			xm.xm_mode |= PERIPH_CAP_SYNC;
    778  1.39.2.2  nathanw 		if (flags & DPARM_WIDE)
    779  1.39.2.2  nathanw 			xm.xm_mode |= PERIPH_CAP_WIDE16;
    780  1.39.2.2  nathanw 		if (flags & DPARM_TQING)
    781  1.39.2.2  nathanw 			xm.xm_mode |= PERIPH_CAP_TQING;
    782  1.39.2.2  nathanw 		scsipi_async_event(bus? &isp->isp_chanB : &isp->isp_chanA,
    783  1.39.2.2  nathanw 		    ASYNC_EVENT_XFER_MODE, &xm);
    784      1.11   mjacob 		break;
    785      1.17   mjacob 	}
    786      1.11   mjacob 	case ISPASYNC_BUS_RESET:
    787  1.39.2.2  nathanw 		bus = *((int *) arg);
    788  1.39.2.2  nathanw 		scsipi_async_event(bus? &isp->isp_chanB : &isp->isp_chanA,
    789  1.39.2.2  nathanw 		    ASYNC_EVENT_RESET, NULL);
    790      1.28   mjacob 		isp_prt(isp, ISP_LOGINFO, "SCSI bus %d reset detected", bus);
    791      1.11   mjacob 		break;
    792  1.39.2.2  nathanw 	case ISPASYNC_LIP:
    793  1.39.2.2  nathanw 		/*
    794  1.39.2.2  nathanw 		 * Don't do queue freezes or blockage until we have the
    795  1.39.2.2  nathanw 		 * thread running that can unfreeze/unblock us.
    796  1.39.2.2  nathanw 		 */
    797  1.39.2.2  nathanw 		if (isp->isp_osinfo.blocked == 0)  {
    798  1.39.2.2  nathanw 			if (isp->isp_osinfo.thread) {
    799  1.39.2.2  nathanw 				isp->isp_osinfo.blocked = 1;
    800  1.39.2.2  nathanw 				scsipi_channel_freeze(&isp->isp_chanA, 1);
    801  1.39.2.2  nathanw 			}
    802  1.39.2.2  nathanw 		}
    803  1.39.2.2  nathanw 		isp_prt(isp, ISP_LOGINFO, "LIP Received");
    804  1.39.2.2  nathanw 		break;
    805  1.39.2.2  nathanw 	case ISPASYNC_LOOP_RESET:
    806  1.39.2.2  nathanw 		/*
    807  1.39.2.2  nathanw 		 * Don't do queue freezes or blockage until we have the
    808  1.39.2.2  nathanw 		 * thread running that can unfreeze/unblock us.
    809  1.39.2.2  nathanw 		 */
    810  1.39.2.2  nathanw 		if (isp->isp_osinfo.blocked == 0) {
    811  1.39.2.2  nathanw 			if (isp->isp_osinfo.thread) {
    812  1.39.2.2  nathanw 				isp->isp_osinfo.blocked = 1;
    813  1.39.2.2  nathanw 				scsipi_channel_freeze(&isp->isp_chanA, 1);
    814  1.39.2.2  nathanw 			}
    815  1.39.2.2  nathanw 		}
    816  1.39.2.2  nathanw 		isp_prt(isp, ISP_LOGINFO, "Loop Reset Received");
    817  1.39.2.2  nathanw 		break;
    818      1.11   mjacob 	case ISPASYNC_LOOP_DOWN:
    819      1.11   mjacob 		/*
    820  1.39.2.2  nathanw 		 * Don't do queue freezes or blockage until we have the
    821  1.39.2.2  nathanw 		 * thread running that can unfreeze/unblock us.
    822      1.11   mjacob 		 */
    823  1.39.2.2  nathanw 		if (isp->isp_osinfo.blocked == 0) {
    824  1.39.2.2  nathanw 			if (isp->isp_osinfo.thread) {
    825  1.39.2.2  nathanw 				isp->isp_osinfo.blocked = 1;
    826  1.39.2.2  nathanw 				scsipi_channel_freeze(&isp->isp_chanA, 1);
    827  1.39.2.2  nathanw 			}
    828  1.39.2.2  nathanw 		}
    829      1.28   mjacob 		isp_prt(isp, ISP_LOGINFO, "Loop DOWN");
    830      1.11   mjacob 		break;
    831      1.11   mjacob         case ISPASYNC_LOOP_UP:
    832  1.39.2.2  nathanw 		/*
    833  1.39.2.2  nathanw 		 * Let the subsequent ISPASYNC_CHANGE_NOTIFY invoke
    834  1.39.2.2  nathanw 		 * the FC worker thread. When the FC worker thread
    835  1.39.2.2  nathanw 		 * is done, let *it* call scsipi_channel_thaw...
    836  1.39.2.2  nathanw 		 */
    837      1.28   mjacob 		isp_prt(isp, ISP_LOGINFO, "Loop UP");
    838      1.11   mjacob 		break;
    839      1.39   mjacob 	case ISPASYNC_PROMENADE:
    840      1.17   mjacob 	if (IS_FC(isp) && isp->isp_dblev) {
    841      1.37   mjacob 		const char fmt[] = "Target %d (Loop 0x%x) Port ID 0x%x "
    842      1.39   mjacob 		    "(role %s) %s\n Port WWN 0x%08x%08x\n Node WWN 0x%08x%08x";
    843      1.15   mjacob 		const static char *roles[4] = {
    844  1.39.2.2  nathanw 		    "None", "Target", "Initiator", "Target/Initiator"
    845      1.11   mjacob 		};
    846      1.15   mjacob 		fcparam *fcp = isp->isp_param;
    847      1.15   mjacob 		int tgt = *((int *) arg);
    848      1.15   mjacob 		struct lportdb *lp = &fcp->portdb[tgt];
    849      1.15   mjacob 
    850      1.28   mjacob 		isp_prt(isp, ISP_LOGINFO, fmt, tgt, lp->loopid, lp->portid,
    851      1.39   mjacob 		    roles[lp->roles & 0x3],
    852      1.39   mjacob 		    (lp->valid)? "Arrived" : "Departed",
    853      1.15   mjacob 		    (u_int32_t) (lp->port_wwn >> 32),
    854      1.15   mjacob 		    (u_int32_t) (lp->port_wwn & 0xffffffffLL),
    855      1.15   mjacob 		    (u_int32_t) (lp->node_wwn >> 32),
    856      1.15   mjacob 		    (u_int32_t) (lp->node_wwn & 0xffffffffLL));
    857      1.11   mjacob 		break;
    858      1.11   mjacob 	}
    859      1.11   mjacob 	case ISPASYNC_CHANGE_NOTIFY:
    860  1.39.2.2  nathanw 		if (arg == ISPASYNC_CHANGE_PDB) {
    861  1.39.2.2  nathanw 			isp_prt(isp, ISP_LOGINFO, "Port Database Changed");
    862  1.39.2.2  nathanw 		} else if (arg == ISPASYNC_CHANGE_SNS) {
    863      1.38   mjacob 			isp_prt(isp, ISP_LOGINFO,
    864      1.38   mjacob 			    "Name Server Database Changed");
    865  1.39.2.2  nathanw 		}
    866  1.39.2.2  nathanw 
    867  1.39.2.2  nathanw 		/*
    868  1.39.2.2  nathanw 		 * We can set blocked here because we know it's now okay
    869  1.39.2.2  nathanw 		 * to try and run isp_fc_runstate (in order to build loop
    870  1.39.2.2  nathanw 		 * state). But we don't try and freeze the midlayer's queue
    871  1.39.2.2  nathanw 		 * if we have no thread that we can wake to later unfreeze
    872  1.39.2.2  nathanw 		 * it.
    873  1.39.2.2  nathanw 		 */
    874  1.39.2.2  nathanw 		if (isp->isp_osinfo.blocked == 0) {
    875  1.39.2.2  nathanw 			isp->isp_osinfo.blocked = 1;
    876  1.39.2.2  nathanw 			if (isp->isp_osinfo.thread) {
    877  1.39.2.2  nathanw 				scsipi_channel_freeze(&isp->isp_chanA, 1);
    878  1.39.2.2  nathanw 			}
    879  1.39.2.2  nathanw 		}
    880  1.39.2.2  nathanw 		/*
    881  1.39.2.2  nathanw 		 * Note that we have work for the thread to do, and
    882  1.39.2.2  nathanw 		 * if the thread is here already, wake it up.
    883  1.39.2.2  nathanw 		 */
    884  1.39.2.2  nathanw 		isp->isp_osinfo.threadwork++;
    885  1.39.2.2  nathanw 		if (isp->isp_osinfo.thread) {
    886  1.39.2.2  nathanw 			wakeup(&isp->isp_osinfo.thread);
    887      1.38   mjacob 		} else {
    888  1.39.2.2  nathanw 			isp_prt(isp, ISP_LOGDEBUG1, "no FC thread yet");
    889      1.38   mjacob 		}
    890       1.9   mjacob 		break;
    891      1.15   mjacob 	case ISPASYNC_FABRIC_DEV:
    892      1.15   mjacob 	{
    893      1.39   mjacob 		int target, lrange;
    894      1.39   mjacob 		struct lportdb *lp = NULL;
    895      1.39   mjacob 		char *pt;
    896      1.39   mjacob 		sns_ganrsp_t *resp = (sns_ganrsp_t *) arg;
    897      1.15   mjacob 		u_int32_t portid;
    898      1.39   mjacob 		u_int64_t wwpn, wwnn;
    899      1.15   mjacob 		fcparam *fcp = isp->isp_param;
    900      1.15   mjacob 
    901      1.15   mjacob 		portid =
    902      1.15   mjacob 		    (((u_int32_t) resp->snscb_port_id[0]) << 16) |
    903      1.15   mjacob 		    (((u_int32_t) resp->snscb_port_id[1]) << 8) |
    904      1.15   mjacob 		    (((u_int32_t) resp->snscb_port_id[2]));
    905      1.39   mjacob 
    906      1.39   mjacob 		wwpn =
    907      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[0]) << 56) |
    908      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[1]) << 48) |
    909      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[2]) << 40) |
    910      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[3]) << 32) |
    911      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[4]) << 24) |
    912      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[5]) << 16) |
    913      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[6]) <<  8) |
    914      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[7]));
    915      1.31   mjacob 
    916      1.39   mjacob 		wwnn =
    917      1.39   mjacob 		    (((u_int64_t)resp->snscb_nodename[0]) << 56) |
    918      1.39   mjacob 		    (((u_int64_t)resp->snscb_nodename[1]) << 48) |
    919      1.39   mjacob 		    (((u_int64_t)resp->snscb_nodename[2]) << 40) |
    920      1.39   mjacob 		    (((u_int64_t)resp->snscb_nodename[3]) << 32) |
    921      1.39   mjacob 		    (((u_int64_t)resp->snscb_nodename[4]) << 24) |
    922      1.39   mjacob 		    (((u_int64_t)resp->snscb_nodename[5]) << 16) |
    923      1.39   mjacob 		    (((u_int64_t)resp->snscb_nodename[6]) <<  8) |
    924      1.39   mjacob 		    (((u_int64_t)resp->snscb_nodename[7]));
    925      1.39   mjacob 		if (portid == 0 || wwpn == 0) {
    926      1.39   mjacob 			break;
    927      1.39   mjacob 		}
    928      1.39   mjacob 
    929      1.39   mjacob 		switch (resp->snscb_port_type) {
    930      1.39   mjacob 		case 1:
    931      1.39   mjacob 			pt = "   N_Port";
    932      1.39   mjacob 			break;
    933      1.39   mjacob 		case 2:
    934      1.39   mjacob 			pt = "  NL_Port";
    935      1.39   mjacob 			break;
    936      1.39   mjacob 		case 3:
    937      1.39   mjacob 			pt = "F/NL_Port";
    938      1.39   mjacob 			break;
    939      1.39   mjacob 		case 0x7f:
    940      1.39   mjacob 			pt = "  Nx_Port";
    941      1.39   mjacob 			break;
    942      1.39   mjacob 		case 0x81:
    943      1.39   mjacob 			pt = "  F_port";
    944      1.39   mjacob 			break;
    945      1.39   mjacob 		case 0x82:
    946      1.39   mjacob 			pt = "  FL_Port";
    947      1.39   mjacob 			break;
    948      1.39   mjacob 		case 0x84:
    949      1.39   mjacob 			pt = "   E_port";
    950      1.39   mjacob 			break;
    951      1.39   mjacob 		default:
    952      1.39   mjacob 			pt = "?";
    953      1.39   mjacob 			break;
    954      1.39   mjacob 		}
    955      1.28   mjacob 		isp_prt(isp, ISP_LOGINFO,
    956      1.39   mjacob 		    "%s @ 0x%x, Node 0x%08x%08x Port %08x%08x",
    957      1.39   mjacob 		    pt, portid, ((u_int32_t) (wwnn >> 32)), ((u_int32_t) wwnn),
    958      1.39   mjacob 		    ((u_int32_t) (wwpn >> 32)), ((u_int32_t) wwpn));
    959      1.39   mjacob 		/*
    960      1.39   mjacob 		 * We're only interested in SCSI_FCP types (for now)
    961      1.39   mjacob 		 */
    962      1.39   mjacob 		if ((resp->snscb_fc4_types[2] & 1) == 0) {
    963      1.39   mjacob 			break;
    964      1.39   mjacob 		}
    965      1.39   mjacob 		if (fcp->isp_topo != TOPO_F_PORT)
    966      1.39   mjacob 			lrange = FC_SNS_ID+1;
    967      1.39   mjacob 		else
    968      1.39   mjacob 			lrange = 0;
    969      1.39   mjacob 		/*
    970      1.39   mjacob 		 * Is it already in our list?
    971      1.39   mjacob 		 */
    972      1.39   mjacob 		for (target = lrange; target < MAX_FC_TARG; target++) {
    973      1.39   mjacob 			if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
    974      1.39   mjacob 				continue;
    975      1.39   mjacob 			}
    976      1.15   mjacob 			lp = &fcp->portdb[target];
    977      1.39   mjacob 			if (lp->port_wwn == wwpn && lp->node_wwn == wwnn) {
    978      1.39   mjacob 				lp->fabric_dev = 1;
    979      1.15   mjacob 				break;
    980      1.39   mjacob 			}
    981      1.15   mjacob 		}
    982      1.15   mjacob 		if (target < MAX_FC_TARG) {
    983      1.15   mjacob 			break;
    984      1.15   mjacob 		}
    985      1.39   mjacob 		for (target = lrange; target < MAX_FC_TARG; target++) {
    986      1.39   mjacob 			if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
    987      1.39   mjacob 				continue;
    988      1.39   mjacob 			}
    989      1.15   mjacob 			lp = &fcp->portdb[target];
    990      1.39   mjacob 			if (lp->port_wwn == 0) {
    991      1.15   mjacob 				break;
    992      1.39   mjacob 			}
    993      1.15   mjacob 		}
    994      1.15   mjacob 		if (target == MAX_FC_TARG) {
    995      1.28   mjacob 			isp_prt(isp, ISP_LOGWARN,
    996      1.28   mjacob 			    "no more space for fabric devices");
    997      1.39   mjacob 			break;
    998      1.15   mjacob 		}
    999      1.39   mjacob 		lp->node_wwn = wwnn;
   1000      1.39   mjacob 		lp->port_wwn = wwpn;
   1001      1.15   mjacob 		lp->portid = portid;
   1002      1.39   mjacob 		lp->fabric_dev = 1;
   1003  1.39.2.3  nathanw 		break;
   1004  1.39.2.3  nathanw 	}
   1005  1.39.2.3  nathanw 	case ISPASYNC_FW_CRASH:
   1006  1.39.2.3  nathanw 	{
   1007  1.39.2.3  nathanw 		u_int16_t mbox1, mbox6;
   1008  1.39.2.3  nathanw 		mbox1 = ISP_READ(isp, OUTMAILBOX1);
   1009  1.39.2.3  nathanw 		if (IS_DUALBUS(isp)) {
   1010  1.39.2.3  nathanw 			mbox6 = ISP_READ(isp, OUTMAILBOX6);
   1011  1.39.2.3  nathanw 		} else {
   1012  1.39.2.3  nathanw 			mbox6 = 0;
   1013  1.39.2.3  nathanw 		}
   1014  1.39.2.3  nathanw                 isp_prt(isp, ISP_LOGERR,
   1015  1.39.2.4  nathanw                     "Internal Firmware Error on bus %d @ RISC Address 0x%x",
   1016  1.39.2.3  nathanw                     mbox6, mbox1);
   1017  1.39.2.3  nathanw 		isp_reinit(isp);
   1018      1.15   mjacob 		break;
   1019      1.15   mjacob 	}
   1020       1.9   mjacob 	default:
   1021       1.9   mjacob 		break;
   1022       1.9   mjacob 	}
   1023       1.9   mjacob 	return (0);
   1024      1.28   mjacob }
   1025      1.28   mjacob 
   1026      1.28   mjacob #include <machine/stdarg.h>
   1027      1.28   mjacob void
   1028      1.28   mjacob isp_prt(struct ispsoftc *isp, int level, const char *fmt, ...)
   1029      1.28   mjacob {
   1030      1.28   mjacob 	va_list ap;
   1031      1.28   mjacob 	if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
   1032      1.28   mjacob 		return;
   1033      1.28   mjacob 	}
   1034      1.28   mjacob 	printf("%s: ", isp->isp_name);
   1035      1.28   mjacob 	va_start(ap, fmt);
   1036      1.28   mjacob 	vprintf(fmt, ap);
   1037      1.28   mjacob 	va_end(ap);
   1038      1.28   mjacob 	printf("\n");
   1039       1.1   mjacob }
   1040