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