Home | History | Annotate | Line # | Download | only in ic
isp_netbsd.c revision 1.39.2.2
      1  1.39.2.2  nathanw /* $NetBSD: isp_netbsd.c,v 1.39.2.2 2001/06/21 20:02:42 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.2  nathanw static void ispminphys_1020(struct buf *);
     84  1.39.2.1  nathanw static void ispminphys(struct buf *);
     85  1.39.2.2  nathanw static INLINE void ispcmd(struct ispsoftc *, XS_T *);
     86  1.39.2.2  nathanw static void isprequest(struct scsipi_channel *, scsipi_adapter_req_t, void *);
     87  1.39.2.2  nathanw static int
     88  1.39.2.2  nathanw ispioctl(struct scsipi_channel *, u_long, caddr_t, int, struct proc *);
     89       1.1   mjacob 
     90  1.39.2.2  nathanw static void isp_polled_cmd(struct ispsoftc *, XS_T *);
     91  1.39.2.1  nathanw static void isp_dog(void *);
     92  1.39.2.2  nathanw static void isp_create_fc_worker(void *);
     93  1.39.2.2  nathanw static void isp_fc_worker(void *);
     94      1.10   mjacob 
     95       1.1   mjacob /*
     96       1.1   mjacob  * Complete attachment of hardware, include subdevices.
     97       1.1   mjacob  */
     98       1.1   mjacob void
     99  1.39.2.1  nathanw isp_attach(struct ispsoftc *isp)
    100       1.1   mjacob {
    101       1.1   mjacob 	isp->isp_state = ISP_RUNSTATE;
    102  1.39.2.2  nathanw 
    103  1.39.2.2  nathanw 	isp->isp_osinfo._adapter.adapt_dev = &isp->isp_osinfo._dev;
    104  1.39.2.2  nathanw 	isp->isp_osinfo._adapter.adapt_nchannels = IS_DUALBUS(isp) ? 2 : 1;
    105  1.39.2.2  nathanw 	isp->isp_osinfo._adapter.adapt_openings = isp->isp_maxcmds;
    106  1.39.2.2  nathanw 	/*
    107  1.39.2.2  nathanw 	 * It's not stated whether max_periph is limited by SPI
    108  1.39.2.2  nathanw 	 * tag uage, but let's assume that it is.
    109  1.39.2.2  nathanw 	 */
    110  1.39.2.2  nathanw 	isp->isp_osinfo._adapter.adapt_max_periph = min(isp->isp_maxcmds, 255);
    111  1.39.2.2  nathanw 	isp->isp_osinfo._adapter.adapt_ioctl = ispioctl;
    112  1.39.2.2  nathanw 	isp->isp_osinfo._adapter.adapt_request = isprequest;
    113  1.39.2.2  nathanw 	if (isp->isp_type <= ISP_HA_SCSI_1020A) {
    114  1.39.2.2  nathanw 		isp->isp_osinfo._adapter.adapt_minphys = ispminphys_1020;
    115  1.39.2.2  nathanw 	} else {
    116  1.39.2.2  nathanw 		isp->isp_osinfo._adapter.adapt_minphys = ispminphys;
    117  1.39.2.2  nathanw 	}
    118  1.39.2.2  nathanw 
    119  1.39.2.2  nathanw 	isp->isp_osinfo._chan.chan_adapter = &isp->isp_osinfo._adapter;
    120  1.39.2.2  nathanw 	isp->isp_osinfo._chan.chan_bustype = &scsi_bustype;
    121  1.39.2.2  nathanw 	isp->isp_osinfo._chan.chan_channel = 0;
    122  1.39.2.2  nathanw 
    123      1.28   mjacob 	/*
    124      1.28   mjacob 	 * Until the midlayer is fixed to use REPORT LUNS, limit to 8 luns.
    125      1.28   mjacob 	 */
    126  1.39.2.2  nathanw 	isp->isp_osinfo._chan.chan_nluns = min(isp->isp_maxluns, 8);
    127       1.1   mjacob 
    128      1.15   mjacob 	if (IS_FC(isp)) {
    129  1.39.2.2  nathanw 		isp->isp_osinfo._chan.chan_ntargets = MAX_FC_TARG;
    130  1.39.2.2  nathanw 		isp->isp_osinfo._chan.chan_id = MAX_FC_TARG;
    131  1.39.2.2  nathanw 		isp->isp_osinfo.threadwork = 1;
    132  1.39.2.2  nathanw 		/*
    133  1.39.2.2  nathanw 		 * Note that isp_create_fc_worker won't get called
    134  1.39.2.2  nathanw 		 * until much much later (after proc0 is created).
    135  1.39.2.2  nathanw 		 */
    136  1.39.2.2  nathanw 		kthread_create(isp_create_fc_worker, isp);
    137       1.1   mjacob 	} else {
    138  1.39.2.2  nathanw 		int bus = 0;
    139      1.14   mjacob 		sdparam *sdp = isp->isp_param;
    140  1.39.2.2  nathanw 
    141  1.39.2.2  nathanw 		isp->isp_osinfo._chan.chan_ntargets = MAX_TARGETS;
    142  1.39.2.2  nathanw 		isp->isp_osinfo._chan.chan_id = sdp->isp_initiator_id;
    143      1.17   mjacob 		isp->isp_osinfo.discovered[0] = 1 << sdp->isp_initiator_id;
    144      1.21   mjacob 		if (IS_DUALBUS(isp)) {
    145  1.39.2.2  nathanw 			isp->isp_osinfo._chan_b = isp->isp_osinfo._chan;
    146      1.14   mjacob 			sdp++;
    147      1.18   mjacob 			isp->isp_osinfo.discovered[1] =
    148      1.18   mjacob 			    1 << sdp->isp_initiator_id;
    149  1.39.2.2  nathanw 			isp->isp_osinfo._chan_b.chan_id = sdp->isp_initiator_id;
    150  1.39.2.2  nathanw 			isp->isp_osinfo._chan_b.chan_channel = 1;
    151      1.14   mjacob 		}
    152      1.28   mjacob 		ISP_LOCK(isp);
    153      1.14   mjacob 		(void) isp_control(isp, ISPCTL_RESET_BUS, &bus);
    154      1.21   mjacob 		if (IS_DUALBUS(isp)) {
    155      1.14   mjacob 			bus++;
    156      1.14   mjacob 			(void) isp_control(isp, ISPCTL_RESET_BUS, &bus);
    157      1.14   mjacob 		}
    158      1.29   mjacob 		ISP_UNLOCK(isp);
    159      1.11   mjacob 	}
    160      1.10   mjacob 
    161      1.10   mjacob 	/*
    162      1.28   mjacob 	 * After this point, we'll be doing the new configuration
    163      1.28   mjacob 	 * schema which allows interrups, so we can do tsleep/wakeup
    164      1.28   mjacob 	 * for mailbox stuff at that point.
    165      1.28   mjacob 	 */
    166      1.28   mjacob 	isp->isp_osinfo.no_mbox_ints = 0;
    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.1   mjacob /*
    178       1.1   mjacob  * minphys our xfers
    179       1.1   mjacob  */
    180       1.1   mjacob 
    181       1.1   mjacob static void
    182  1.39.2.2  nathanw ispminphys_1020(struct buf *bp)
    183       1.1   mjacob {
    184       1.1   mjacob 	if (bp->b_bcount >= (1 << 24)) {
    185       1.1   mjacob 		bp->b_bcount = (1 << 24);
    186       1.1   mjacob 	}
    187       1.1   mjacob 	minphys(bp);
    188       1.1   mjacob }
    189       1.1   mjacob 
    190  1.39.2.2  nathanw static void
    191  1.39.2.2  nathanw ispminphys(struct buf *bp)
    192  1.39.2.2  nathanw {
    193  1.39.2.2  nathanw 	if (bp->b_bcount >= (1 << 30)) {
    194  1.39.2.2  nathanw 		bp->b_bcount = (1 << 30);
    195  1.39.2.2  nathanw 	}
    196  1.39.2.2  nathanw 	minphys(bp);
    197  1.39.2.2  nathanw }
    198  1.39.2.2  nathanw 
    199      1.17   mjacob static int
    200  1.39.2.2  nathanw ispioctl(struct scsipi_channel *chan, u_long cmd, caddr_t addr, int flag,
    201  1.39.2.2  nathanw 	struct proc *p)
    202      1.17   mjacob {
    203  1.39.2.2  nathanw 	struct ispsoftc *isp = (void *)chan->chan_adapter->adapt_dev;
    204  1.39.2.2  nathanw 	int retval = ENOTTY;
    205      1.17   mjacob 
    206      1.17   mjacob 	switch (cmd) {
    207  1.39.2.2  nathanw 	case SCBUSIORESET:
    208  1.39.2.2  nathanw 		ISP_LOCK(isp);
    209  1.39.2.2  nathanw 		if (isp_control(isp, ISPCTL_RESET_BUS, &chan->chan_channel))
    210  1.39.2.2  nathanw 			retval = EIO;
    211  1.39.2.2  nathanw 		else
    212  1.39.2.2  nathanw 			retval = 0;
    213  1.39.2.2  nathanw 		ISP_UNLOCK(isp);
    214  1.39.2.2  nathanw 		break;
    215  1.39.2.2  nathanw 	case ISP_SDBLEV:
    216      1.29   mjacob 	{
    217  1.39.2.2  nathanw 		int olddblev = isp->isp_dblev;
    218  1.39.2.2  nathanw 		isp->isp_dblev = *(int *)addr;
    219  1.39.2.2  nathanw 		*(int *)addr = olddblev;
    220      1.29   mjacob 		retval = 0;
    221      1.29   mjacob 		break;
    222      1.29   mjacob 	}
    223  1.39.2.2  nathanw 	case ISP_RESETHBA:
    224  1.39.2.2  nathanw 		ISP_LOCK(isp);
    225  1.39.2.2  nathanw 		isp_reinit(isp);
    226  1.39.2.2  nathanw 		ISP_UNLOCK(isp);
    227  1.39.2.2  nathanw 		retval = 0;
    228  1.39.2.2  nathanw 		break;
    229  1.39.2.2  nathanw 	case ISP_FC_RESCAN:
    230  1.39.2.2  nathanw 		if (IS_FC(isp)) {
    231  1.39.2.2  nathanw 			ISP_LOCK(isp);
    232  1.39.2.2  nathanw 			if (isp_fc_runstate(isp, 5 * 1000000)) {
    233  1.39.2.2  nathanw 				retval = EIO;
    234  1.39.2.2  nathanw 			} else {
    235  1.39.2.2  nathanw 				retval = 0;
    236  1.39.2.2  nathanw 			}
    237  1.39.2.2  nathanw 			ISP_UNLOCK(isp);
    238  1.39.2.2  nathanw 		}
    239  1.39.2.2  nathanw 		break;
    240  1.39.2.2  nathanw 	case ISP_FC_LIP:
    241  1.39.2.2  nathanw 		if (IS_FC(isp)) {
    242  1.39.2.2  nathanw 			ISP_LOCK(isp);
    243  1.39.2.2  nathanw 			if (isp_control(isp, ISPCTL_SEND_LIP, 0)) {
    244  1.39.2.2  nathanw 				retval = EIO;
    245  1.39.2.2  nathanw 			} else {
    246  1.39.2.2  nathanw 				retval = 0;
    247  1.39.2.2  nathanw 			}
    248  1.39.2.2  nathanw 			ISP_UNLOCK(isp);
    249  1.39.2.2  nathanw 		}
    250  1.39.2.2  nathanw 		break;
    251  1.39.2.2  nathanw 	case ISP_FC_GETDINFO:
    252  1.39.2.2  nathanw 	{
    253  1.39.2.2  nathanw 		struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
    254  1.39.2.2  nathanw 		struct lportdb *lp;
    255  1.39.2.2  nathanw 
    256  1.39.2.2  nathanw 		if (ifc->loopid < 0 || ifc->loopid >= MAX_FC_TARG) {
    257  1.39.2.2  nathanw 			retval = EINVAL;
    258  1.39.2.2  nathanw 			break;
    259  1.39.2.2  nathanw 		}
    260  1.39.2.2  nathanw 		ISP_LOCK(isp);
    261  1.39.2.2  nathanw 		lp = &FCPARAM(isp)->portdb[ifc->loopid];
    262  1.39.2.2  nathanw 		if (lp->valid) {
    263  1.39.2.2  nathanw 			ifc->loopid = lp->loopid;
    264  1.39.2.2  nathanw 			ifc->portid = lp->portid;
    265  1.39.2.2  nathanw 			ifc->node_wwn = lp->node_wwn;
    266  1.39.2.2  nathanw 			ifc->port_wwn = lp->port_wwn;
    267      1.17   mjacob 			retval = 0;
    268  1.39.2.2  nathanw 		} else {
    269  1.39.2.2  nathanw 			retval = ENODEV;
    270  1.39.2.2  nathanw 		}
    271  1.39.2.2  nathanw 		ISP_UNLOCK(isp);
    272      1.17   mjacob 		break;
    273  1.39.2.2  nathanw 	}
    274      1.17   mjacob 	default:
    275      1.17   mjacob 		break;
    276       1.3   mjacob 	}
    277      1.17   mjacob 	return (retval);
    278      1.17   mjacob }
    279      1.17   mjacob 
    280  1.39.2.2  nathanw static INLINE void
    281  1.39.2.2  nathanw ispcmd(struct ispsoftc *isp, XS_T *xs)
    282      1.17   mjacob {
    283  1.39.2.2  nathanw 	ISP_LOCK(isp);
    284      1.11   mjacob 	if (isp->isp_state < ISP_RUNSTATE) {
    285      1.11   mjacob 		DISABLE_INTS(isp);
    286      1.11   mjacob 		isp_init(isp);
    287  1.39.2.2  nathanw 		if (isp->isp_state != ISP_INITSTATE) {
    288      1.11   mjacob 			ENABLE_INTS(isp);
    289  1.39.2.2  nathanw 			ISP_UNLOCK(isp);
    290  1.39.2.2  nathanw 			XS_SETERR(xs, HBA_BOTCH);
    291  1.39.2.2  nathanw 			scsipi_done(xs);
    292  1.39.2.2  nathanw 			return;
    293  1.39.2.2  nathanw 		}
    294  1.39.2.2  nathanw 		isp->isp_state = ISP_RUNSTATE;
    295      1.11   mjacob 		ENABLE_INTS(isp);
    296  1.39.2.2  nathanw 	}
    297      1.10   mjacob 	/*
    298  1.39.2.2  nathanw 	 * Handle the case of a FC card where the FC thread hasn't
    299  1.39.2.2  nathanw 	 * fired up yet and we have loop state to clean up. If we
    300  1.39.2.2  nathanw 	 * can't clear things up and we've never seen loop up, bounce
    301  1.39.2.2  nathanw 	 * the command.
    302      1.10   mjacob 	 */
    303  1.39.2.2  nathanw 	if (IS_FC(isp) && isp->isp_osinfo.threadwork &&
    304  1.39.2.2  nathanw 	    isp->isp_osinfo.thread == 0) {
    305  1.39.2.2  nathanw 		volatile u_int8_t ombi = isp->isp_osinfo.no_mbox_ints;
    306  1.39.2.2  nathanw 		int delay_time;
    307  1.39.2.2  nathanw 
    308      1.16  thorpej 		if (xs->xs_control & XS_CTL_POLL) {
    309  1.39.2.2  nathanw 			isp->isp_osinfo.no_mbox_ints = 1;
    310      1.10   mjacob 		}
    311  1.39.2.2  nathanw 
    312  1.39.2.2  nathanw 		if (isp->isp_osinfo.loop_checked == 0) {
    313  1.39.2.2  nathanw 			delay_time = 10 * 1000000;
    314  1.39.2.2  nathanw 			isp->isp_osinfo.loop_checked = 1;
    315  1.39.2.2  nathanw 		} else {
    316  1.39.2.2  nathanw 			delay_time = 250000;
    317  1.39.2.2  nathanw 		}
    318  1.39.2.2  nathanw 
    319  1.39.2.2  nathanw 		if (isp_fc_runstate(isp, delay_time) != 0) {
    320  1.39.2.2  nathanw 			if (xs->xs_control & XS_CTL_POLL) {
    321  1.39.2.2  nathanw 				isp->isp_osinfo.no_mbox_ints = ombi;
    322  1.39.2.2  nathanw 			}
    323  1.39.2.2  nathanw 			if (FCPARAM(isp)->loop_seen_once == 0) {
    324  1.39.2.2  nathanw 				XS_SETERR(xs, HBA_SELTIMEOUT);
    325  1.39.2.2  nathanw 				scsipi_done(xs);
    326  1.39.2.2  nathanw 				ISP_UNLOCK(isp);
    327  1.39.2.2  nathanw 				return;
    328  1.39.2.2  nathanw 			}
    329  1.39.2.2  nathanw 			/*
    330  1.39.2.2  nathanw 			 * Otherwise, fall thru to be queued up for later.
    331  1.39.2.2  nathanw 			 */
    332  1.39.2.2  nathanw 		} else {
    333  1.39.2.2  nathanw 			int wasblocked =
    334  1.39.2.2  nathanw 			    (isp->isp_osinfo.blocked || isp->isp_osinfo.paused);
    335  1.39.2.2  nathanw 			isp->isp_osinfo.threadwork = 0;
    336  1.39.2.2  nathanw 			isp->isp_osinfo.blocked =
    337  1.39.2.2  nathanw 			    isp->isp_osinfo.paused = 0;
    338  1.39.2.2  nathanw isp_prt(isp, ISP_LOGALL, "ispcmd, manual runstate, (freeze count %d)", isp->isp_chanA.chan_qfreeze);
    339  1.39.2.2  nathanw 			if (wasblocked) {
    340  1.39.2.2  nathanw 				scsipi_channel_thaw(&isp->isp_chanA, 1);
    341  1.39.2.2  nathanw 			}
    342  1.39.2.2  nathanw 		}
    343  1.39.2.2  nathanw 		if (xs->xs_control & XS_CTL_POLL) {
    344  1.39.2.2  nathanw 			isp->isp_osinfo.no_mbox_ints = ombi;
    345  1.39.2.2  nathanw 		}
    346  1.39.2.2  nathanw 	}
    347  1.39.2.2  nathanw 
    348  1.39.2.2  nathanw 	if (isp->isp_osinfo.paused) {
    349  1.39.2.2  nathanw 		isp_prt(isp, ISP_LOGWARN, "I/O while paused");
    350  1.39.2.2  nathanw 		xs->error = XS_RESOURCE_SHORTAGE;
    351  1.39.2.2  nathanw 		scsipi_done(xs);
    352  1.39.2.2  nathanw 		ISP_UNLOCK(isp);
    353  1.39.2.2  nathanw 		return;
    354  1.39.2.2  nathanw 	}
    355  1.39.2.2  nathanw 	if (isp->isp_osinfo.blocked) {
    356  1.39.2.2  nathanw 		isp_prt(isp, ISP_LOGWARN, "I/O while blocked");
    357  1.39.2.2  nathanw 		xs->error = XS_REQUEUE;
    358  1.39.2.2  nathanw 		scsipi_done(xs);
    359  1.39.2.2  nathanw 		ISP_UNLOCK(isp);
    360  1.39.2.2  nathanw 		return;
    361      1.10   mjacob 	}
    362      1.17   mjacob 
    363      1.26   mjacob 	if (xs->xs_control & XS_CTL_POLL) {
    364      1.28   mjacob 		volatile u_int8_t ombi = isp->isp_osinfo.no_mbox_ints;
    365      1.28   mjacob 		isp->isp_osinfo.no_mbox_ints = 1;
    366  1.39.2.2  nathanw 		isp_polled_cmd(isp, xs);
    367      1.28   mjacob 		isp->isp_osinfo.no_mbox_ints = ombi;
    368  1.39.2.2  nathanw 		ISP_UNLOCK(isp);
    369  1.39.2.2  nathanw 		return;
    370       1.1   mjacob 	}
    371       1.1   mjacob 
    372  1.39.2.2  nathanw 	switch (isp_start(xs)) {
    373      1.26   mjacob 	case CMD_QUEUED:
    374      1.26   mjacob 		if (xs->timeout) {
    375      1.26   mjacob 			callout_reset(&xs->xs_callout, _XT(xs), isp_dog, xs);
    376      1.26   mjacob 		}
    377      1.26   mjacob 		break;
    378      1.26   mjacob 	case CMD_EAGAIN:
    379  1.39.2.2  nathanw 		isp->isp_osinfo.paused = 1;
    380  1.39.2.2  nathanw 		xs->error = XS_RESOURCE_SHORTAGE;
    381  1.39.2.2  nathanw 		scsipi_channel_freeze(&isp->isp_chanA, 1);
    382  1.39.2.2  nathanw 		if (IS_DUALBUS(isp)) {
    383  1.39.2.2  nathanw 			scsipi_channel_freeze(&isp->isp_chanB, 1);
    384  1.39.2.2  nathanw 		}
    385  1.39.2.2  nathanw 		scsipi_done(xs);
    386      1.26   mjacob 		break;
    387      1.26   mjacob 	case CMD_RQLATER:
    388  1.39.2.2  nathanw 		/*
    389  1.39.2.2  nathanw 		 * We can only get RQLATER from FC devices (1 channel only)
    390  1.39.2.2  nathanw 		 *
    391  1.39.2.2  nathanw 		 * Also, if we've never seen loop up, bounce the command
    392  1.39.2.2  nathanw 		 * (somebody has booted with no FC cable connected)
    393  1.39.2.2  nathanw 		 */
    394  1.39.2.2  nathanw 		if (FCPARAM(isp)->loop_seen_once == 0) {
    395  1.39.2.2  nathanw 			XS_SETERR(xs, HBA_SELTIMEOUT);
    396  1.39.2.2  nathanw 			scsipi_done(xs);
    397  1.39.2.2  nathanw 			break;
    398  1.39.2.2  nathanw 		}
    399  1.39.2.2  nathanw 		if (isp->isp_osinfo.blocked == 0) {
    400  1.39.2.2  nathanw 			isp->isp_osinfo.blocked = 1;
    401  1.39.2.2  nathanw 			scsipi_channel_freeze(&isp->isp_chanA, 1);
    402  1.39.2.2  nathanw isp_prt(isp, ISP_LOGALL, "ispcmd, RQLATER, (freeze count %d)", isp->isp_chanA.chan_qfreeze);
    403  1.39.2.2  nathanw 		}
    404  1.39.2.2  nathanw 		xs->error = XS_REQUEUE;
    405  1.39.2.2  nathanw 		scsipi_done(xs);
    406      1.26   mjacob 		break;
    407      1.26   mjacob 	case CMD_COMPLETE:
    408  1.39.2.2  nathanw 		scsipi_done(xs);
    409      1.26   mjacob 		break;
    410      1.26   mjacob 	}
    411  1.39.2.2  nathanw 	ISP_UNLOCK(isp);
    412      1.26   mjacob }
    413      1.26   mjacob 
    414  1.39.2.2  nathanw static void
    415  1.39.2.2  nathanw isprequest(struct scsipi_channel *chan, scsipi_adapter_req_t req, void *arg)
    416  1.39.2.2  nathanw {
    417  1.39.2.2  nathanw 	struct ispsoftc *isp = (void *)chan->chan_adapter->adapt_dev;
    418  1.39.2.2  nathanw 
    419  1.39.2.2  nathanw 	switch (req) {
    420  1.39.2.2  nathanw 	case ADAPTER_REQ_RUN_XFER:
    421  1.39.2.2  nathanw 		ispcmd(isp, (XS_T *) arg);
    422  1.39.2.2  nathanw 		break;
    423  1.39.2.2  nathanw 
    424  1.39.2.2  nathanw 	case ADAPTER_REQ_GROW_RESOURCES:
    425  1.39.2.2  nathanw 		/* Not supported. */
    426  1.39.2.2  nathanw 		break;
    427  1.39.2.2  nathanw 
    428  1.39.2.2  nathanw 	case ADAPTER_REQ_SET_XFER_MODE:
    429  1.39.2.2  nathanw 	if (IS_SCSI(isp)) {
    430  1.39.2.2  nathanw 		struct scsipi_xfer_mode *xm = arg;
    431  1.39.2.2  nathanw 		int dflags = 0;
    432  1.39.2.2  nathanw 		sdparam *sdp = SDPARAM(isp);
    433  1.39.2.2  nathanw 
    434  1.39.2.2  nathanw 		sdp += chan->chan_channel;
    435  1.39.2.2  nathanw 		if (xm->xm_mode & PERIPH_CAP_TQING)
    436  1.39.2.2  nathanw 			dflags |= DPARM_TQING;
    437  1.39.2.2  nathanw 		if (xm->xm_mode & PERIPH_CAP_WIDE16)
    438  1.39.2.2  nathanw 			dflags |= DPARM_WIDE;
    439  1.39.2.2  nathanw 		if (xm->xm_mode & PERIPH_CAP_SYNC)
    440  1.39.2.2  nathanw 			dflags |= DPARM_SYNC;
    441  1.39.2.2  nathanw 		ISP_LOCK(isp);
    442  1.39.2.2  nathanw 		sdp->isp_devparam[xm->xm_target].dev_flags |= dflags;
    443  1.39.2.2  nathanw 		dflags = sdp->isp_devparam[xm->xm_target].dev_flags;
    444  1.39.2.2  nathanw 		sdp->isp_devparam[xm->xm_target].dev_update = 1;
    445  1.39.2.2  nathanw 		isp->isp_update |= (1 << chan->chan_channel);
    446  1.39.2.2  nathanw 		ISP_UNLOCK(isp);
    447  1.39.2.2  nathanw 		isp_prt(isp, ISP_LOGDEBUG1,
    448  1.39.2.2  nathanw 		    "ispioctl: device flags 0x%x for %d.%d.X",
    449  1.39.2.2  nathanw 		    dflags, chan->chan_channel, xm->xm_target);
    450  1.39.2.2  nathanw 		break;
    451  1.39.2.2  nathanw 	}
    452  1.39.2.2  nathanw 	default:
    453  1.39.2.2  nathanw 		break;
    454  1.39.2.2  nathanw 	}
    455  1.39.2.2  nathanw }
    456  1.39.2.2  nathanw 
    457  1.39.2.2  nathanw static void
    458  1.39.2.1  nathanw isp_polled_cmd(struct ispsoftc *isp, XS_T *xs)
    459      1.26   mjacob {
    460      1.26   mjacob 	int result;
    461      1.26   mjacob 	int infinite = 0, mswait;
    462      1.26   mjacob 
    463      1.28   mjacob 	result = isp_start(xs);
    464      1.26   mjacob 
    465      1.17   mjacob 	switch (result) {
    466      1.17   mjacob 	case CMD_QUEUED:
    467      1.17   mjacob 		break;
    468      1.17   mjacob 	case CMD_RQLATER:
    469  1.39.2.2  nathanw 		if (XS_NOERR(xs)) {
    470  1.39.2.2  nathanw 			xs->error = XS_REQUEUE;
    471  1.39.2.2  nathanw 		}
    472      1.17   mjacob 	case CMD_EAGAIN:
    473      1.17   mjacob 		if (XS_NOERR(xs)) {
    474  1.39.2.2  nathanw 			xs->error = XS_RESOURCE_SHORTAGE;
    475      1.17   mjacob 		}
    476  1.39.2.2  nathanw 		/* FALLTHROUGH */
    477      1.17   mjacob 	case CMD_COMPLETE:
    478  1.39.2.2  nathanw 		scsipi_done(xs);
    479  1.39.2.2  nathanw 		return;
    480      1.17   mjacob 
    481      1.17   mjacob 	}
    482      1.26   mjacob 
    483       1.1   mjacob 	/*
    484       1.1   mjacob 	 * If we can't use interrupts, poll on completion.
    485       1.1   mjacob 	 */
    486      1.26   mjacob 	if ((mswait = XS_TIME(xs)) == 0)
    487      1.26   mjacob 		infinite = 1;
    488      1.26   mjacob 
    489      1.26   mjacob 	while (mswait || infinite) {
    490      1.26   mjacob 		if (isp_intr((void *)isp)) {
    491      1.26   mjacob 			if (XS_CMD_DONE_P(xs)) {
    492      1.26   mjacob 				break;
    493       1.1   mjacob 			}
    494       1.1   mjacob 		}
    495      1.28   mjacob 		USEC_DELAY(1000);
    496      1.26   mjacob 		mswait -= 1;
    497      1.26   mjacob 	}
    498      1.26   mjacob 
    499      1.26   mjacob 	/*
    500      1.26   mjacob 	 * If no other error occurred but we didn't finish,
    501      1.26   mjacob 	 * something bad happened.
    502      1.26   mjacob 	 */
    503      1.26   mjacob 	if (XS_CMD_DONE_P(xs) == 0) {
    504      1.26   mjacob 		if (isp_control(isp, ISPCTL_ABORT_CMD, xs)) {
    505      1.28   mjacob 			isp_reinit(isp);
    506      1.26   mjacob 		}
    507      1.26   mjacob 		if (XS_NOERR(xs)) {
    508      1.26   mjacob 			XS_SETERR(xs, HBA_BOTCH);
    509      1.26   mjacob 		}
    510       1.1   mjacob 	}
    511  1.39.2.2  nathanw 	scsipi_done(xs);
    512       1.1   mjacob }
    513       1.1   mjacob 
    514      1.26   mjacob void
    515  1.39.2.1  nathanw isp_done(XS_T *xs)
    516       1.1   mjacob {
    517      1.26   mjacob 	XS_CMD_S_DONE(xs);
    518      1.26   mjacob 	if (XS_CMD_WDOG_P(xs) == 0) {
    519      1.26   mjacob 		struct ispsoftc *isp = XS_ISP(xs);
    520      1.26   mjacob 		callout_stop(&xs->xs_callout);
    521      1.26   mjacob 		if (XS_CMD_GRACE_P(xs)) {
    522      1.28   mjacob 			isp_prt(isp, ISP_LOGDEBUG1,
    523      1.28   mjacob 			    "finished command on borrowed time");
    524       1.1   mjacob 		}
    525      1.26   mjacob 		XS_CMD_S_CLEAR(xs);
    526  1.39.2.2  nathanw 		/*
    527  1.39.2.2  nathanw 		 * Fixup- if we get a QFULL, we need
    528  1.39.2.2  nathanw 		 * to set XS_BUSY as the error.
    529  1.39.2.2  nathanw 		 */
    530  1.39.2.2  nathanw 		if (xs->status == SCSI_QUEUE_FULL) {
    531  1.39.2.2  nathanw 			xs->error = XS_BUSY;
    532  1.39.2.2  nathanw 		}
    533  1.39.2.2  nathanw 		if (isp->isp_osinfo.paused) {
    534  1.39.2.2  nathanw 			isp->isp_osinfo.paused = 0;
    535  1.39.2.2  nathanw 			scsipi_channel_timed_thaw(&isp->isp_chanA);
    536  1.39.2.2  nathanw 			if (IS_DUALBUS(isp)) {
    537  1.39.2.2  nathanw 				scsipi_channel_timed_thaw(&isp->isp_chanB);
    538  1.39.2.2  nathanw 			}
    539  1.39.2.2  nathanw 		}
    540      1.26   mjacob 		scsipi_done(xs);
    541       1.1   mjacob 	}
    542       1.8   mjacob }
    543       1.8   mjacob 
    544       1.8   mjacob static void
    545  1.39.2.1  nathanw isp_dog(void *arg)
    546       1.8   mjacob {
    547      1.28   mjacob 	XS_T *xs = arg;
    548      1.26   mjacob 	struct ispsoftc *isp = XS_ISP(xs);
    549  1.39.2.1  nathanw 	u_int16_t handle;
    550       1.8   mjacob 
    551      1.30   mjacob 	ISP_ILOCK(isp);
    552       1.8   mjacob 	/*
    553      1.26   mjacob 	 * We've decided this command is dead. Make sure we're not trying
    554      1.26   mjacob 	 * to kill a command that's already dead by getting it's handle and
    555      1.26   mjacob 	 * and seeing whether it's still alive.
    556      1.26   mjacob 	 */
    557      1.26   mjacob 	handle = isp_find_handle(isp, xs);
    558      1.26   mjacob 	if (handle) {
    559      1.26   mjacob 		u_int16_t r, r1, i;
    560      1.26   mjacob 
    561      1.26   mjacob 		if (XS_CMD_DONE_P(xs)) {
    562      1.28   mjacob 			isp_prt(isp, ISP_LOGDEBUG1,
    563      1.28   mjacob 			    "watchdog found done cmd (handle 0x%x)", handle);
    564      1.30   mjacob 			ISP_IUNLOCK(isp);
    565      1.26   mjacob 			return;
    566      1.26   mjacob 		}
    567      1.26   mjacob 
    568      1.26   mjacob 		if (XS_CMD_WDOG_P(xs)) {
    569      1.28   mjacob 			isp_prt(isp, ISP_LOGDEBUG1,
    570      1.28   mjacob 			    "recursive watchdog (handle 0x%x)", handle);
    571      1.30   mjacob 			ISP_IUNLOCK(isp);
    572      1.26   mjacob 			return;
    573      1.26   mjacob 		}
    574      1.26   mjacob 
    575      1.26   mjacob 		XS_CMD_S_WDOG(xs);
    576      1.26   mjacob 
    577      1.26   mjacob 		i = 0;
    578      1.26   mjacob 		do {
    579      1.26   mjacob 			r = ISP_READ(isp, BIU_ISR);
    580      1.28   mjacob 			USEC_DELAY(1);
    581      1.26   mjacob 			r1 = ISP_READ(isp, BIU_ISR);
    582      1.26   mjacob 		} while (r != r1 && ++i < 1000);
    583      1.26   mjacob 
    584      1.26   mjacob 		if (INT_PENDING(isp, r) && isp_intr(isp) && XS_CMD_DONE_P(xs)) {
    585      1.28   mjacob 			isp_prt(isp, ISP_LOGDEBUG1, "watchdog cleanup (%x, %x)",
    586      1.28   mjacob 			    handle, r);
    587      1.26   mjacob 			XS_CMD_C_WDOG(xs);
    588      1.26   mjacob 			isp_done(xs);
    589      1.26   mjacob 		} else if (XS_CMD_GRACE_P(xs)) {
    590      1.28   mjacob 			isp_prt(isp, ISP_LOGDEBUG1, "watchdog timeout (%x, %x)",
    591      1.28   mjacob 			    handle, r);
    592      1.26   mjacob 			/*
    593      1.26   mjacob 			 * Make sure the command is *really* dead before we
    594      1.26   mjacob 			 * release the handle (and DMA resources) for reuse.
    595      1.26   mjacob 			 */
    596      1.26   mjacob 			(void) isp_control(isp, ISPCTL_ABORT_CMD, arg);
    597      1.26   mjacob 
    598      1.26   mjacob 			/*
    599      1.26   mjacob 			 * After this point, the comamnd is really dead.
    600      1.26   mjacob 			 */
    601      1.26   mjacob 			if (XS_XFRLEN(xs)) {
    602      1.26   mjacob 				ISP_DMAFREE(isp, xs, handle);
    603      1.26   mjacob 			}
    604      1.26   mjacob 			isp_destroy_handle(isp, handle);
    605      1.26   mjacob 			XS_SETERR(xs, XS_TIMEOUT);
    606      1.26   mjacob 			XS_CMD_S_CLEAR(xs);
    607      1.26   mjacob 			isp_done(xs);
    608      1.26   mjacob 		} else {
    609      1.26   mjacob 			u_int16_t iptr, optr;
    610      1.26   mjacob 			ispreq_t *mp;
    611      1.28   mjacob 			isp_prt(isp, ISP_LOGDEBUG2,
    612      1.28   mjacob 			    "possible command timeout (%x, %x)", handle, r);
    613      1.26   mjacob 			XS_CMD_C_WDOG(xs);
    614      1.26   mjacob 			callout_reset(&xs->xs_callout, hz, isp_dog, xs);
    615      1.26   mjacob 			if (isp_getrqentry(isp, &iptr, &optr, (void **) &mp)) {
    616      1.33   mjacob 				ISP_UNLOCK(isp);
    617      1.26   mjacob 				return;
    618      1.26   mjacob 			}
    619      1.26   mjacob 			XS_CMD_S_GRACE(xs);
    620      1.26   mjacob 			MEMZERO((void *) mp, sizeof (*mp));
    621      1.26   mjacob 			mp->req_header.rqs_entry_count = 1;
    622      1.26   mjacob 			mp->req_header.rqs_entry_type = RQSTYPE_MARKER;
    623      1.26   mjacob 			mp->req_modifier = SYNC_ALL;
    624      1.26   mjacob 			mp->req_target = XS_CHANNEL(xs) << 7;
    625      1.26   mjacob 			ISP_SWIZZLE_REQUEST(isp, mp);
    626      1.26   mjacob 			ISP_ADD_REQUEST(isp, iptr);
    627       1.8   mjacob 		}
    628      1.30   mjacob 	} else {
    629      1.30   mjacob 		isp_prt(isp, ISP_LOGDEBUG0, "watchdog with no command");
    630       1.8   mjacob 	}
    631      1.30   mjacob 	ISP_IUNLOCK(isp);
    632       1.8   mjacob }
    633       1.8   mjacob 
    634       1.8   mjacob /*
    635  1.39.2.2  nathanw  * Fibre Channel state cleanup thread
    636  1.39.2.2  nathanw  */
    637  1.39.2.2  nathanw static void
    638  1.39.2.2  nathanw isp_create_fc_worker(void *arg)
    639  1.39.2.2  nathanw {
    640  1.39.2.2  nathanw 	struct ispsoftc *isp = arg;
    641  1.39.2.2  nathanw 
    642  1.39.2.2  nathanw 	if (kthread_create1(isp_fc_worker, isp, &isp->isp_osinfo.thread,
    643  1.39.2.2  nathanw 	    "%s:fc_thrd", isp->isp_name)) {
    644  1.39.2.2  nathanw 		isp_prt(isp, ISP_LOGERR, "unable to create FC worker thread");
    645  1.39.2.2  nathanw 		panic("isp_create_fc_worker");
    646  1.39.2.2  nathanw 	}
    647  1.39.2.2  nathanw 
    648  1.39.2.2  nathanw }
    649  1.39.2.2  nathanw 
    650  1.39.2.2  nathanw static void
    651  1.39.2.2  nathanw isp_fc_worker(void *arg)
    652  1.39.2.2  nathanw {
    653  1.39.2.2  nathanw 	void scsipi_run_queue(struct scsipi_channel *);
    654  1.39.2.2  nathanw 	struct ispsoftc *isp = arg;
    655  1.39.2.2  nathanw 
    656  1.39.2.2  nathanw 	for (;;) {
    657  1.39.2.2  nathanw 		int s;
    658  1.39.2.2  nathanw 
    659  1.39.2.2  nathanw 		/*
    660  1.39.2.2  nathanw 		 * Note we do *not* use the ISP_LOCK/ISP_UNLOCK macros here.
    661  1.39.2.2  nathanw 		 */
    662  1.39.2.2  nathanw 		s = splbio();
    663  1.39.2.2  nathanw 		while (isp->isp_osinfo.threadwork) {
    664  1.39.2.2  nathanw 			isp->isp_osinfo.threadwork = 0;
    665  1.39.2.2  nathanw 			if (isp_fc_runstate(isp, 10 * 1000000) == 0) {
    666  1.39.2.2  nathanw 				break;
    667  1.39.2.2  nathanw 			}
    668  1.39.2.2  nathanw 			isp->isp_osinfo.threadwork = 1;
    669  1.39.2.2  nathanw 			splx(s);
    670  1.39.2.2  nathanw 			delay(500 * 1000);
    671  1.39.2.2  nathanw 			s = splbio();
    672  1.39.2.2  nathanw 		}
    673  1.39.2.2  nathanw 		if (FCPARAM(isp)->isp_fwstate != FW_READY ||
    674  1.39.2.2  nathanw 		    FCPARAM(isp)->isp_loopstate != LOOP_READY) {
    675  1.39.2.2  nathanw 			isp_prt(isp, ISP_LOGINFO, "isp_fc_runstate in vain");
    676  1.39.2.2  nathanw 			isp->isp_osinfo.threadwork = 1;
    677  1.39.2.2  nathanw 			splx(s);
    678  1.39.2.2  nathanw 			continue;
    679  1.39.2.2  nathanw 		}
    680  1.39.2.2  nathanw 
    681  1.39.2.2  nathanw 		if (isp->isp_osinfo.blocked) {
    682  1.39.2.2  nathanw 			isp->isp_osinfo.blocked = 0;
    683  1.39.2.2  nathanw 			isp_prt(isp, /* ISP_LOGDEBUG0 */ ISP_LOGALL, "restarting queues (freeze count %d)", isp->isp_chanA.chan_qfreeze);
    684  1.39.2.2  nathanw 
    685  1.39.2.2  nathanw 			scsipi_channel_thaw(&isp->isp_chanA, 1);
    686  1.39.2.2  nathanw 		}
    687  1.39.2.2  nathanw 
    688  1.39.2.2  nathanw 		if (isp->isp_osinfo.thread == NULL)
    689  1.39.2.2  nathanw 			break;
    690  1.39.2.2  nathanw 
    691  1.39.2.2  nathanw 		(void) tsleep(&isp->isp_osinfo.thread, PRIBIO, "fcclnup", 0);
    692  1.39.2.2  nathanw 
    693  1.39.2.2  nathanw 		splx(s);
    694  1.39.2.2  nathanw 	}
    695  1.39.2.2  nathanw 
    696  1.39.2.2  nathanw 	/* In case parent is waiting for us to exit. */
    697  1.39.2.2  nathanw 	wakeup(&isp->isp_osinfo.thread);
    698  1.39.2.2  nathanw 
    699  1.39.2.2  nathanw 	kthread_exit(0);
    700  1.39.2.2  nathanw }
    701  1.39.2.2  nathanw 
    702  1.39.2.2  nathanw /*
    703       1.8   mjacob  * Free any associated resources prior to decommissioning and
    704       1.8   mjacob  * set the card to a known state (so it doesn't wake up and kick
    705       1.8   mjacob  * us when we aren't expecting it to).
    706       1.8   mjacob  *
    707       1.8   mjacob  * Locks are held before coming here.
    708       1.8   mjacob  */
    709       1.8   mjacob void
    710  1.39.2.1  nathanw isp_uninit(struct ispsoftc *isp)
    711       1.8   mjacob {
    712      1.28   mjacob 	isp_lock(isp);
    713       1.8   mjacob 	/*
    714       1.8   mjacob 	 * Leave with interrupts disabled.
    715       1.8   mjacob 	 */
    716       1.8   mjacob 	DISABLE_INTS(isp);
    717      1.28   mjacob 	isp_unlock(isp);
    718       1.9   mjacob }
    719       1.9   mjacob 
    720       1.9   mjacob int
    721  1.39.2.1  nathanw isp_async(struct ispsoftc *isp, ispasync_t cmd, void *arg)
    722       1.9   mjacob {
    723      1.14   mjacob 	int bus, tgt;
    724  1.39.2.2  nathanw 
    725       1.9   mjacob 	switch (cmd) {
    726       1.9   mjacob 	case ISPASYNC_NEW_TGT_PARAMS:
    727      1.17   mjacob 	if (IS_SCSI(isp) && isp->isp_dblev) {
    728      1.17   mjacob 		sdparam *sdp = isp->isp_param;
    729  1.39.2.2  nathanw 		int flags;
    730  1.39.2.2  nathanw 		struct scsipi_xfer_mode xm;
    731      1.17   mjacob 
    732      1.17   mjacob 		tgt = *((int *) arg);
    733      1.17   mjacob 		bus = (tgt >> 16) & 0xffff;
    734      1.17   mjacob 		tgt &= 0xffff;
    735      1.21   mjacob 		sdp += bus;
    736      1.17   mjacob 		flags = sdp->isp_devparam[tgt].cur_dflags;
    737      1.21   mjacob 
    738  1.39.2.2  nathanw 		xm.xm_mode = 0;
    739  1.39.2.2  nathanw 		xm.xm_period = sdp->isp_devparam[tgt].cur_period;
    740  1.39.2.2  nathanw 		xm.xm_offset = sdp->isp_devparam[tgt].cur_offset;
    741  1.39.2.2  nathanw 		xm.xm_target = tgt;
    742  1.39.2.2  nathanw 
    743  1.39.2.2  nathanw 		if ((flags & DPARM_SYNC) && xm.xm_period && xm.xm_offset)
    744  1.39.2.2  nathanw 			xm.xm_mode |= PERIPH_CAP_SYNC;
    745  1.39.2.2  nathanw 		if (flags & DPARM_WIDE)
    746  1.39.2.2  nathanw 			xm.xm_mode |= PERIPH_CAP_WIDE16;
    747  1.39.2.2  nathanw 		if (flags & DPARM_TQING)
    748  1.39.2.2  nathanw 			xm.xm_mode |= PERIPH_CAP_TQING;
    749  1.39.2.2  nathanw 		scsipi_async_event(bus? &isp->isp_chanB : &isp->isp_chanA,
    750  1.39.2.2  nathanw 		    ASYNC_EVENT_XFER_MODE, &xm);
    751      1.11   mjacob 		break;
    752      1.17   mjacob 	}
    753      1.11   mjacob 	case ISPASYNC_BUS_RESET:
    754  1.39.2.2  nathanw 		bus = *((int *) arg);
    755  1.39.2.2  nathanw 		scsipi_async_event(bus? &isp->isp_chanB : &isp->isp_chanA,
    756  1.39.2.2  nathanw 		    ASYNC_EVENT_RESET, NULL);
    757      1.28   mjacob 		isp_prt(isp, ISP_LOGINFO, "SCSI bus %d reset detected", bus);
    758      1.11   mjacob 		break;
    759  1.39.2.2  nathanw 	case ISPASYNC_LIP:
    760  1.39.2.2  nathanw 		/*
    761  1.39.2.2  nathanw 		 * Don't do queue freezes or blockage until we have the
    762  1.39.2.2  nathanw 		 * thread running that can unfreeze/unblock us.
    763  1.39.2.2  nathanw 		 */
    764  1.39.2.2  nathanw 		if (isp->isp_osinfo.blocked == 0)  {
    765  1.39.2.2  nathanw 			if (isp->isp_osinfo.thread) {
    766  1.39.2.2  nathanw 				isp->isp_osinfo.blocked = 1;
    767  1.39.2.2  nathanw 				scsipi_channel_freeze(&isp->isp_chanA, 1);
    768  1.39.2.2  nathanw 			}
    769  1.39.2.2  nathanw 		}
    770  1.39.2.2  nathanw 		isp_prt(isp, ISP_LOGINFO, "LIP Received");
    771  1.39.2.2  nathanw 		break;
    772  1.39.2.2  nathanw 	case ISPASYNC_LOOP_RESET:
    773  1.39.2.2  nathanw 		/*
    774  1.39.2.2  nathanw 		 * Don't do queue freezes or blockage until we have the
    775  1.39.2.2  nathanw 		 * thread running that can unfreeze/unblock us.
    776  1.39.2.2  nathanw 		 */
    777  1.39.2.2  nathanw 		if (isp->isp_osinfo.blocked == 0) {
    778  1.39.2.2  nathanw 			if (isp->isp_osinfo.thread) {
    779  1.39.2.2  nathanw 				isp->isp_osinfo.blocked = 1;
    780  1.39.2.2  nathanw 				scsipi_channel_freeze(&isp->isp_chanA, 1);
    781  1.39.2.2  nathanw 			}
    782  1.39.2.2  nathanw 		}
    783  1.39.2.2  nathanw 		isp_prt(isp, ISP_LOGINFO, "Loop Reset Received");
    784  1.39.2.2  nathanw 		break;
    785      1.11   mjacob 	case ISPASYNC_LOOP_DOWN:
    786      1.11   mjacob 		/*
    787  1.39.2.2  nathanw 		 * Don't do queue freezes or blockage until we have the
    788  1.39.2.2  nathanw 		 * thread running that can unfreeze/unblock us.
    789      1.11   mjacob 		 */
    790  1.39.2.2  nathanw 		if (isp->isp_osinfo.blocked == 0) {
    791  1.39.2.2  nathanw 			if (isp->isp_osinfo.thread) {
    792  1.39.2.2  nathanw 				isp->isp_osinfo.blocked = 1;
    793  1.39.2.2  nathanw 				scsipi_channel_freeze(&isp->isp_chanA, 1);
    794  1.39.2.2  nathanw 			}
    795  1.39.2.2  nathanw 		}
    796      1.28   mjacob 		isp_prt(isp, ISP_LOGINFO, "Loop DOWN");
    797      1.11   mjacob 		break;
    798      1.11   mjacob         case ISPASYNC_LOOP_UP:
    799  1.39.2.2  nathanw 		/*
    800  1.39.2.2  nathanw 		 * Let the subsequent ISPASYNC_CHANGE_NOTIFY invoke
    801  1.39.2.2  nathanw 		 * the FC worker thread. When the FC worker thread
    802  1.39.2.2  nathanw 		 * is done, let *it* call scsipi_channel_thaw...
    803  1.39.2.2  nathanw 		 */
    804      1.28   mjacob 		isp_prt(isp, ISP_LOGINFO, "Loop UP");
    805      1.11   mjacob 		break;
    806      1.39   mjacob 	case ISPASYNC_PROMENADE:
    807      1.17   mjacob 	if (IS_FC(isp) && isp->isp_dblev) {
    808      1.37   mjacob 		const char fmt[] = "Target %d (Loop 0x%x) Port ID 0x%x "
    809      1.39   mjacob 		    "(role %s) %s\n Port WWN 0x%08x%08x\n Node WWN 0x%08x%08x";
    810      1.15   mjacob 		const static char *roles[4] = {
    811  1.39.2.2  nathanw 		    "None", "Target", "Initiator", "Target/Initiator"
    812      1.11   mjacob 		};
    813      1.15   mjacob 		fcparam *fcp = isp->isp_param;
    814      1.15   mjacob 		int tgt = *((int *) arg);
    815      1.15   mjacob 		struct lportdb *lp = &fcp->portdb[tgt];
    816      1.15   mjacob 
    817      1.28   mjacob 		isp_prt(isp, ISP_LOGINFO, fmt, tgt, lp->loopid, lp->portid,
    818      1.39   mjacob 		    roles[lp->roles & 0x3],
    819      1.39   mjacob 		    (lp->valid)? "Arrived" : "Departed",
    820      1.15   mjacob 		    (u_int32_t) (lp->port_wwn >> 32),
    821      1.15   mjacob 		    (u_int32_t) (lp->port_wwn & 0xffffffffLL),
    822      1.15   mjacob 		    (u_int32_t) (lp->node_wwn >> 32),
    823      1.15   mjacob 		    (u_int32_t) (lp->node_wwn & 0xffffffffLL));
    824      1.11   mjacob 		break;
    825      1.11   mjacob 	}
    826      1.11   mjacob 	case ISPASYNC_CHANGE_NOTIFY:
    827  1.39.2.2  nathanw 		if (arg == ISPASYNC_CHANGE_PDB) {
    828  1.39.2.2  nathanw 			isp_prt(isp, ISP_LOGINFO, "Port Database Changed");
    829  1.39.2.2  nathanw 		} else if (arg == ISPASYNC_CHANGE_SNS) {
    830      1.38   mjacob 			isp_prt(isp, ISP_LOGINFO,
    831      1.38   mjacob 			    "Name Server Database Changed");
    832  1.39.2.2  nathanw 		}
    833  1.39.2.2  nathanw 
    834  1.39.2.2  nathanw 		/*
    835  1.39.2.2  nathanw 		 * We can set blocked here because we know it's now okay
    836  1.39.2.2  nathanw 		 * to try and run isp_fc_runstate (in order to build loop
    837  1.39.2.2  nathanw 		 * state). But we don't try and freeze the midlayer's queue
    838  1.39.2.2  nathanw 		 * if we have no thread that we can wake to later unfreeze
    839  1.39.2.2  nathanw 		 * it.
    840  1.39.2.2  nathanw 		 */
    841  1.39.2.2  nathanw 		if (isp->isp_osinfo.blocked == 0) {
    842  1.39.2.2  nathanw 			isp->isp_osinfo.blocked = 1;
    843  1.39.2.2  nathanw 			if (isp->isp_osinfo.thread) {
    844  1.39.2.2  nathanw 				scsipi_channel_freeze(&isp->isp_chanA, 1);
    845  1.39.2.2  nathanw 			}
    846  1.39.2.2  nathanw 		}
    847  1.39.2.2  nathanw 		/*
    848  1.39.2.2  nathanw 		 * Note that we have work for the thread to do, and
    849  1.39.2.2  nathanw 		 * if the thread is here already, wake it up.
    850  1.39.2.2  nathanw 		 */
    851  1.39.2.2  nathanw 		isp->isp_osinfo.threadwork++;
    852  1.39.2.2  nathanw 		if (isp->isp_osinfo.thread) {
    853  1.39.2.2  nathanw 			wakeup(&isp->isp_osinfo.thread);
    854      1.38   mjacob 		} else {
    855  1.39.2.2  nathanw 			isp_prt(isp, ISP_LOGDEBUG1, "no FC thread yet");
    856      1.38   mjacob 		}
    857       1.9   mjacob 		break;
    858      1.15   mjacob 	case ISPASYNC_FABRIC_DEV:
    859      1.15   mjacob 	{
    860      1.39   mjacob 		int target, lrange;
    861      1.39   mjacob 		struct lportdb *lp = NULL;
    862      1.39   mjacob 		char *pt;
    863      1.39   mjacob 		sns_ganrsp_t *resp = (sns_ganrsp_t *) arg;
    864      1.15   mjacob 		u_int32_t portid;
    865      1.39   mjacob 		u_int64_t wwpn, wwnn;
    866      1.15   mjacob 		fcparam *fcp = isp->isp_param;
    867      1.15   mjacob 
    868      1.15   mjacob 		portid =
    869      1.15   mjacob 		    (((u_int32_t) resp->snscb_port_id[0]) << 16) |
    870      1.15   mjacob 		    (((u_int32_t) resp->snscb_port_id[1]) << 8) |
    871      1.15   mjacob 		    (((u_int32_t) resp->snscb_port_id[2]));
    872      1.39   mjacob 
    873      1.39   mjacob 		wwpn =
    874      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[0]) << 56) |
    875      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[1]) << 48) |
    876      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[2]) << 40) |
    877      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[3]) << 32) |
    878      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[4]) << 24) |
    879      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[5]) << 16) |
    880      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[6]) <<  8) |
    881      1.15   mjacob 		    (((u_int64_t)resp->snscb_portname[7]));
    882      1.31   mjacob 
    883      1.39   mjacob 		wwnn =
    884      1.39   mjacob 		    (((u_int64_t)resp->snscb_nodename[0]) << 56) |
    885      1.39   mjacob 		    (((u_int64_t)resp->snscb_nodename[1]) << 48) |
    886      1.39   mjacob 		    (((u_int64_t)resp->snscb_nodename[2]) << 40) |
    887      1.39   mjacob 		    (((u_int64_t)resp->snscb_nodename[3]) << 32) |
    888      1.39   mjacob 		    (((u_int64_t)resp->snscb_nodename[4]) << 24) |
    889      1.39   mjacob 		    (((u_int64_t)resp->snscb_nodename[5]) << 16) |
    890      1.39   mjacob 		    (((u_int64_t)resp->snscb_nodename[6]) <<  8) |
    891      1.39   mjacob 		    (((u_int64_t)resp->snscb_nodename[7]));
    892      1.39   mjacob 		if (portid == 0 || wwpn == 0) {
    893      1.39   mjacob 			break;
    894      1.39   mjacob 		}
    895      1.39   mjacob 
    896      1.39   mjacob 		switch (resp->snscb_port_type) {
    897      1.39   mjacob 		case 1:
    898      1.39   mjacob 			pt = "   N_Port";
    899      1.39   mjacob 			break;
    900      1.39   mjacob 		case 2:
    901      1.39   mjacob 			pt = "  NL_Port";
    902      1.39   mjacob 			break;
    903      1.39   mjacob 		case 3:
    904      1.39   mjacob 			pt = "F/NL_Port";
    905      1.39   mjacob 			break;
    906      1.39   mjacob 		case 0x7f:
    907      1.39   mjacob 			pt = "  Nx_Port";
    908      1.39   mjacob 			break;
    909      1.39   mjacob 		case 0x81:
    910      1.39   mjacob 			pt = "  F_port";
    911      1.39   mjacob 			break;
    912      1.39   mjacob 		case 0x82:
    913      1.39   mjacob 			pt = "  FL_Port";
    914      1.39   mjacob 			break;
    915      1.39   mjacob 		case 0x84:
    916      1.39   mjacob 			pt = "   E_port";
    917      1.39   mjacob 			break;
    918      1.39   mjacob 		default:
    919      1.39   mjacob 			pt = "?";
    920      1.39   mjacob 			break;
    921      1.39   mjacob 		}
    922      1.28   mjacob 		isp_prt(isp, ISP_LOGINFO,
    923      1.39   mjacob 		    "%s @ 0x%x, Node 0x%08x%08x Port %08x%08x",
    924      1.39   mjacob 		    pt, portid, ((u_int32_t) (wwnn >> 32)), ((u_int32_t) wwnn),
    925      1.39   mjacob 		    ((u_int32_t) (wwpn >> 32)), ((u_int32_t) wwpn));
    926      1.39   mjacob 		/*
    927      1.39   mjacob 		 * We're only interested in SCSI_FCP types (for now)
    928      1.39   mjacob 		 */
    929      1.39   mjacob 		if ((resp->snscb_fc4_types[2] & 1) == 0) {
    930      1.39   mjacob 			break;
    931      1.39   mjacob 		}
    932      1.39   mjacob 		if (fcp->isp_topo != TOPO_F_PORT)
    933      1.39   mjacob 			lrange = FC_SNS_ID+1;
    934      1.39   mjacob 		else
    935      1.39   mjacob 			lrange = 0;
    936      1.39   mjacob 		/*
    937      1.39   mjacob 		 * Is it already in our list?
    938      1.39   mjacob 		 */
    939      1.39   mjacob 		for (target = lrange; target < MAX_FC_TARG; target++) {
    940      1.39   mjacob 			if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
    941      1.39   mjacob 				continue;
    942      1.39   mjacob 			}
    943      1.15   mjacob 			lp = &fcp->portdb[target];
    944      1.39   mjacob 			if (lp->port_wwn == wwpn && lp->node_wwn == wwnn) {
    945      1.39   mjacob 				lp->fabric_dev = 1;
    946      1.15   mjacob 				break;
    947      1.39   mjacob 			}
    948      1.15   mjacob 		}
    949      1.15   mjacob 		if (target < MAX_FC_TARG) {
    950      1.15   mjacob 			break;
    951      1.15   mjacob 		}
    952      1.39   mjacob 		for (target = lrange; target < MAX_FC_TARG; target++) {
    953      1.39   mjacob 			if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
    954      1.39   mjacob 				continue;
    955      1.39   mjacob 			}
    956      1.15   mjacob 			lp = &fcp->portdb[target];
    957      1.39   mjacob 			if (lp->port_wwn == 0) {
    958      1.15   mjacob 				break;
    959      1.39   mjacob 			}
    960      1.15   mjacob 		}
    961      1.15   mjacob 		if (target == MAX_FC_TARG) {
    962      1.28   mjacob 			isp_prt(isp, ISP_LOGWARN,
    963      1.28   mjacob 			    "no more space for fabric devices");
    964      1.39   mjacob 			break;
    965      1.15   mjacob 		}
    966      1.39   mjacob 		lp->node_wwn = wwnn;
    967      1.39   mjacob 		lp->port_wwn = wwpn;
    968      1.15   mjacob 		lp->portid = portid;
    969      1.39   mjacob 		lp->fabric_dev = 1;
    970      1.15   mjacob 		break;
    971      1.15   mjacob 	}
    972       1.9   mjacob 	default:
    973       1.9   mjacob 		break;
    974       1.9   mjacob 	}
    975       1.9   mjacob 	return (0);
    976      1.28   mjacob }
    977      1.28   mjacob 
    978      1.28   mjacob #include <machine/stdarg.h>
    979      1.28   mjacob void
    980      1.28   mjacob isp_prt(struct ispsoftc *isp, int level, const char *fmt, ...)
    981      1.28   mjacob {
    982      1.28   mjacob 	va_list ap;
    983      1.28   mjacob 	if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
    984      1.28   mjacob 		return;
    985      1.28   mjacob 	}
    986      1.28   mjacob 	printf("%s: ", isp->isp_name);
    987      1.28   mjacob 	va_start(ap, fmt);
    988      1.28   mjacob 	vprintf(fmt, ap);
    989      1.28   mjacob 	va_end(ap);
    990      1.28   mjacob 	printf("\n");
    991       1.1   mjacob }
    992