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