isp_netbsd.c revision 1.55 1 /* $NetBSD: isp_netbsd.c,v 1.55 2002/06/15 00:20:18 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 <sys/cdefs.h>
62 __KERNEL_RCSID(0, "$NetBSD: isp_netbsd.c,v 1.55 2002/06/15 00:20:18 mjacob Exp $");
63
64 #include <dev/ic/isp_netbsd.h>
65 #include <sys/scsiio.h>
66
67
68 /*
69 * Set a timeout for the watchdogging of a command.
70 *
71 * The dimensional analysis is
72 *
73 * milliseconds * (seconds/millisecond) * (ticks/second) = ticks
74 *
75 * =
76 *
77 * (milliseconds / 1000) * hz = ticks
78 *
79 *
80 * For timeouts less than 1 second, we'll get zero. Because of this, and
81 * because we want to establish *our* timeout to be longer than what the
82 * firmware might do, we just add 3 seconds at the back end.
83 */
84 #define _XT(xs) ((((xs)->timeout/1000) * hz) + (3 * hz))
85
86 static void isp_config_interrupts(struct device *);
87 static void ispminphys_1020(struct buf *);
88 static void ispminphys(struct buf *);
89 static INLINE void ispcmd(struct ispsoftc *, XS_T *);
90 static void isprequest(struct scsipi_channel *, scsipi_adapter_req_t, void *);
91 static int
92 ispioctl(struct scsipi_channel *, u_long, caddr_t, int, struct proc *);
93
94 static void isp_polled_cmd(struct ispsoftc *, XS_T *);
95 static void isp_dog(void *);
96 static void isp_create_fc_worker(void *);
97 static void isp_fc_worker(void *);
98
99 /*
100 * Complete attachment of hardware, include subdevices.
101 */
102 void
103 isp_attach(struct ispsoftc *isp)
104 {
105 isp->isp_state = ISP_RUNSTATE;
106
107 isp->isp_osinfo._adapter.adapt_dev = &isp->isp_osinfo._dev;
108 isp->isp_osinfo._adapter.adapt_nchannels = IS_DUALBUS(isp) ? 2 : 1;
109 isp->isp_osinfo._adapter.adapt_openings = isp->isp_maxcmds;
110 /*
111 * It's not stated whether max_periph is limited by SPI
112 * tag uage, but let's assume that it is.
113 */
114 isp->isp_osinfo._adapter.adapt_max_periph = min(isp->isp_maxcmds, 255);
115 isp->isp_osinfo._adapter.adapt_ioctl = ispioctl;
116 isp->isp_osinfo._adapter.adapt_request = isprequest;
117 if (isp->isp_type <= ISP_HA_SCSI_1020A) {
118 isp->isp_osinfo._adapter.adapt_minphys = ispminphys_1020;
119 } else {
120 isp->isp_osinfo._adapter.adapt_minphys = ispminphys;
121 }
122
123 isp->isp_osinfo._chan.chan_adapter = &isp->isp_osinfo._adapter;
124 isp->isp_osinfo._chan.chan_bustype = &scsi_bustype;
125 isp->isp_osinfo._chan.chan_channel = 0;
126
127 /*
128 * Until the midlayer is fixed to use REPORT LUNS, limit to 8 luns.
129 */
130 isp->isp_osinfo._chan.chan_nluns = min(isp->isp_maxluns, 8);
131
132 if (IS_FC(isp)) {
133 isp->isp_osinfo._chan.chan_ntargets = MAX_FC_TARG;
134 isp->isp_osinfo._chan.chan_id = MAX_FC_TARG;
135 isp->isp_osinfo.threadwork = 1;
136 /*
137 * Note that isp_create_fc_worker won't get called
138 * until much much later (after proc0 is created).
139 */
140 kthread_create(isp_create_fc_worker, isp);
141 #ifdef ISP_FW_CRASH_DUMP
142 if (IS_2200(isp)) {
143 FCPARAM(isp)->isp_dump_data =
144 malloc(QLA2200_RISC_IMAGE_DUMP_SIZE, M_DEVBUF,
145 M_NOWAIT);
146 } else if (IS_23XX(isp)) {
147 FCPARAM(isp)->isp_dump_data =
148 malloc(QLA2300_RISC_IMAGE_DUMP_SIZE, M_DEVBUF,
149 M_NOWAIT);
150 }
151 if (FCPARAM(isp)->isp_dump_data)
152 FCPARAM(isp)->isp_dump_data[0] = 0;
153 #endif
154 } else {
155 int bus = 0;
156 sdparam *sdp = isp->isp_param;
157
158 isp->isp_osinfo._chan.chan_ntargets = MAX_TARGETS;
159 isp->isp_osinfo._chan.chan_id = sdp->isp_initiator_id;
160 isp->isp_osinfo.discovered[0] = 1 << sdp->isp_initiator_id;
161 if (IS_DUALBUS(isp)) {
162 isp->isp_osinfo._chan_b = isp->isp_osinfo._chan;
163 sdp++;
164 isp->isp_osinfo.discovered[1] =
165 1 << sdp->isp_initiator_id;
166 isp->isp_osinfo._chan_b.chan_id = sdp->isp_initiator_id;
167 isp->isp_osinfo._chan_b.chan_channel = 1;
168 }
169 ISP_LOCK(isp);
170 (void) isp_control(isp, ISPCTL_RESET_BUS, &bus);
171 if (IS_DUALBUS(isp)) {
172 bus++;
173 (void) isp_control(isp, ISPCTL_RESET_BUS, &bus);
174 }
175 ISP_UNLOCK(isp);
176 }
177
178
179 /*
180 * Defer enabling mailbox interrupts until later.
181 */
182 config_interrupts((struct device *) isp, isp_config_interrupts);
183
184 /*
185 * And attach children (if any).
186 */
187 config_found((void *)isp, &isp->isp_chanA, scsiprint);
188 if (IS_DUALBUS(isp)) {
189 config_found((void *)isp, &isp->isp_chanB, scsiprint);
190 }
191 }
192
193
194 static void
195 isp_config_interrupts(struct device *self)
196 {
197 struct ispsoftc *isp = (struct ispsoftc *) self;
198
199 /*
200 * After this point, we'll be doing the new configuration
201 * schema which allows interrups, so we can do tsleep/wakeup
202 * for mailbox stuff at that point.
203 */
204 isp->isp_osinfo.no_mbox_ints = 0;
205 }
206
207
208 /*
209 * minphys our xfers
210 */
211
212 static void
213 ispminphys_1020(struct buf *bp)
214 {
215 if (bp->b_bcount >= (1 << 24)) {
216 bp->b_bcount = (1 << 24);
217 }
218 minphys(bp);
219 }
220
221 static void
222 ispminphys(struct buf *bp)
223 {
224 if (bp->b_bcount >= (1 << 30)) {
225 bp->b_bcount = (1 << 30);
226 }
227 minphys(bp);
228 }
229
230 static int
231 ispioctl(struct scsipi_channel *chan, u_long cmd, caddr_t addr, int flag,
232 struct proc *p)
233 {
234 struct ispsoftc *isp = (void *)chan->chan_adapter->adapt_dev;
235 int retval = ENOTTY;
236
237 switch (cmd) {
238 #ifdef ISP_FW_CRASH_DUMP
239 case ISP_GET_FW_CRASH_DUMP:
240 {
241 u_int16_t *ptr = FCPARAM(isp)->isp_dump_data;
242 size_t sz;
243
244 retval = 0;
245 if (IS_2200(isp))
246 sz = QLA2200_RISC_IMAGE_DUMP_SIZE;
247 else
248 sz = QLA2300_RISC_IMAGE_DUMP_SIZE;
249 ISP_LOCK(isp);
250 if (ptr && *ptr) {
251 void *uaddr = *((void **) addr);
252 if (copyout(ptr, uaddr, sz)) {
253 retval = EFAULT;
254 } else {
255 *ptr = 0;
256 }
257 } else {
258 retval = ENXIO;
259 }
260 ISP_UNLOCK(isp);
261 break;
262 }
263
264 case ISP_FORCE_CRASH_DUMP:
265 ISP_LOCK(isp);
266 if (isp->isp_osinfo.blocked == 0) {
267 isp->isp_osinfo.blocked = 1;
268 scsipi_channel_freeze(&isp->isp_chanA, 1);
269 }
270 isp_fw_dump(isp);
271 isp_reinit(isp);
272 ISP_UNLOCK(isp);
273 retval = 0;
274 break;
275 #endif
276 case ISP_SDBLEV:
277 {
278 int olddblev = isp->isp_dblev;
279 isp->isp_dblev = *(int *)addr;
280 *(int *)addr = olddblev;
281 retval = 0;
282 break;
283 }
284 case ISP_RESETHBA:
285 ISP_LOCK(isp);
286 isp_reinit(isp);
287 ISP_UNLOCK(isp);
288 retval = 0;
289 break;
290 case ISP_RESCAN:
291 if (IS_FC(isp)) {
292 ISP_LOCK(isp);
293 if (isp_fc_runstate(isp, 5 * 1000000)) {
294 retval = EIO;
295 } else {
296 retval = 0;
297 }
298 ISP_UNLOCK(isp);
299 }
300 break;
301 case ISP_FC_LIP:
302 if (IS_FC(isp)) {
303 ISP_LOCK(isp);
304 if (isp_control(isp, ISPCTL_SEND_LIP, 0)) {
305 retval = EIO;
306 } else {
307 retval = 0;
308 }
309 ISP_UNLOCK(isp);
310 }
311 break;
312 case ISP_FC_GETDINFO:
313 {
314 struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
315 struct lportdb *lp;
316
317 if (ifc->loopid < 0 || ifc->loopid >= MAX_FC_TARG) {
318 retval = EINVAL;
319 break;
320 }
321 ISP_LOCK(isp);
322 lp = &FCPARAM(isp)->portdb[ifc->loopid];
323 if (lp->valid) {
324 ifc->loopid = lp->loopid;
325 ifc->portid = lp->portid;
326 ifc->node_wwn = lp->node_wwn;
327 ifc->port_wwn = lp->port_wwn;
328 retval = 0;
329 } else {
330 retval = ENODEV;
331 }
332 ISP_UNLOCK(isp);
333 break;
334 }
335 case ISP_GET_STATS:
336 {
337 isp_stats_t *sp = (isp_stats_t *) addr;
338
339 MEMZERO(sp, sizeof (*sp));
340 sp->isp_stat_version = ISP_STATS_VERSION;
341 sp->isp_type = isp->isp_type;
342 sp->isp_revision = isp->isp_revision;
343 ISP_LOCK(isp);
344 sp->isp_stats[ISP_INTCNT] = isp->isp_intcnt;
345 sp->isp_stats[ISP_INTBOGUS] = isp->isp_intbogus;
346 sp->isp_stats[ISP_INTMBOXC] = isp->isp_intmboxc;
347 sp->isp_stats[ISP_INGOASYNC] = isp->isp_intoasync;
348 sp->isp_stats[ISP_RSLTCCMPLT] = isp->isp_rsltccmplt;
349 sp->isp_stats[ISP_FPHCCMCPLT] = isp->isp_fphccmplt;
350 sp->isp_stats[ISP_RSCCHIWAT] = isp->isp_rscchiwater;
351 sp->isp_stats[ISP_FPCCHIWAT] = isp->isp_fpcchiwater;
352 ISP_UNLOCK(isp);
353 retval = 0;
354 break;
355 }
356 case ISP_CLR_STATS:
357 ISP_LOCK(isp);
358 isp->isp_intcnt = 0;
359 isp->isp_intbogus = 0;
360 isp->isp_intmboxc = 0;
361 isp->isp_intoasync = 0;
362 isp->isp_rsltccmplt = 0;
363 isp->isp_fphccmplt = 0;
364 isp->isp_rscchiwater = 0;
365 isp->isp_fpcchiwater = 0;
366 ISP_UNLOCK(isp);
367 retval = 0;
368 break;
369 case ISP_FC_GETHINFO:
370 {
371 struct isp_hba_device *hba = (struct isp_hba_device *) addr;
372 MEMZERO(hba, sizeof (*hba));
373 ISP_LOCK(isp);
374 hba->fc_speed = FCPARAM(isp)->isp_gbspeed;
375 hba->fc_scsi_supported = 1;
376 hba->fc_topology = FCPARAM(isp)->isp_topo + 1;
377 hba->fc_loopid = FCPARAM(isp)->isp_loopid;
378 hba->active_node_wwn = FCPARAM(isp)->isp_nodewwn;
379 hba->active_port_wwn = FCPARAM(isp)->isp_portwwn;
380 ISP_UNLOCK(isp);
381 break;
382 }
383 case SCBUSIORESET:
384 ISP_LOCK(isp);
385 if (isp_control(isp, ISPCTL_RESET_BUS, &chan->chan_channel))
386 retval = EIO;
387 else
388 retval = 0;
389 ISP_UNLOCK(isp);
390 break;
391 default:
392 break;
393 }
394 return (retval);
395 }
396
397 static INLINE void
398 ispcmd(struct ispsoftc *isp, XS_T *xs)
399 {
400 ISP_LOCK(isp);
401 if (isp->isp_state < ISP_RUNSTATE) {
402 DISABLE_INTS(isp);
403 isp_init(isp);
404 if (isp->isp_state != ISP_INITSTATE) {
405 ENABLE_INTS(isp);
406 ISP_UNLOCK(isp);
407 XS_SETERR(xs, HBA_BOTCH);
408 scsipi_done(xs);
409 return;
410 }
411 isp->isp_state = ISP_RUNSTATE;
412 ENABLE_INTS(isp);
413 }
414 /*
415 * Handle the case of a FC card where the FC thread hasn't
416 * fired up yet and we have loop state to clean up. If we
417 * can't clear things up and we've never seen loop up, bounce
418 * the command.
419 */
420 if (IS_FC(isp) && isp->isp_osinfo.threadwork &&
421 isp->isp_osinfo.thread == 0) {
422 volatile u_int8_t ombi = isp->isp_osinfo.no_mbox_ints;
423 int delay_time;
424
425 if (xs->xs_control & XS_CTL_POLL) {
426 isp->isp_osinfo.no_mbox_ints = 1;
427 }
428
429 if (isp->isp_osinfo.loop_checked == 0) {
430 delay_time = 10 * 1000000;
431 isp->isp_osinfo.loop_checked = 1;
432 } else {
433 delay_time = 250000;
434 }
435
436 if (isp_fc_runstate(isp, delay_time) != 0) {
437 if (xs->xs_control & XS_CTL_POLL) {
438 isp->isp_osinfo.no_mbox_ints = ombi;
439 }
440 if (FCPARAM(isp)->loop_seen_once == 0) {
441 XS_SETERR(xs, HBA_SELTIMEOUT);
442 scsipi_done(xs);
443 ISP_UNLOCK(isp);
444 return;
445 }
446 /*
447 * Otherwise, fall thru to be queued up for later.
448 */
449 } else {
450 int wasblocked =
451 (isp->isp_osinfo.blocked || isp->isp_osinfo.paused);
452 isp->isp_osinfo.threadwork = 0;
453 isp->isp_osinfo.blocked =
454 isp->isp_osinfo.paused = 0;
455 if (wasblocked) {
456 scsipi_channel_thaw(&isp->isp_chanA, 1);
457 }
458 }
459 if (xs->xs_control & XS_CTL_POLL) {
460 isp->isp_osinfo.no_mbox_ints = ombi;
461 }
462 }
463
464 if (isp->isp_osinfo.paused) {
465 isp_prt(isp, ISP_LOGWARN, "I/O while paused");
466 xs->error = XS_RESOURCE_SHORTAGE;
467 scsipi_done(xs);
468 ISP_UNLOCK(isp);
469 return;
470 }
471 if (isp->isp_osinfo.blocked) {
472 isp_prt(isp, ISP_LOGWARN, "I/O while blocked");
473 xs->error = XS_REQUEUE;
474 scsipi_done(xs);
475 ISP_UNLOCK(isp);
476 return;
477 }
478
479 if (xs->xs_control & XS_CTL_POLL) {
480 volatile u_int8_t ombi = isp->isp_osinfo.no_mbox_ints;
481 isp->isp_osinfo.no_mbox_ints = 1;
482 isp_polled_cmd(isp, xs);
483 isp->isp_osinfo.no_mbox_ints = ombi;
484 ISP_UNLOCK(isp);
485 return;
486 }
487
488 switch (isp_start(xs)) {
489 case CMD_QUEUED:
490 if (xs->timeout) {
491 callout_reset(&xs->xs_callout, _XT(xs), isp_dog, xs);
492 }
493 break;
494 case CMD_EAGAIN:
495 isp->isp_osinfo.paused = 1;
496 xs->error = XS_RESOURCE_SHORTAGE;
497 scsipi_channel_freeze(&isp->isp_chanA, 1);
498 if (IS_DUALBUS(isp)) {
499 scsipi_channel_freeze(&isp->isp_chanB, 1);
500 }
501 scsipi_done(xs);
502 break;
503 case CMD_RQLATER:
504 /*
505 * We can only get RQLATER from FC devices (1 channel only)
506 *
507 * Also, if we've never seen loop up, bounce the command
508 * (somebody has booted with no FC cable connected)
509 */
510 if (FCPARAM(isp)->loop_seen_once == 0) {
511 XS_SETERR(xs, HBA_SELTIMEOUT);
512 scsipi_done(xs);
513 break;
514 }
515 if (isp->isp_osinfo.blocked == 0) {
516 isp->isp_osinfo.blocked = 1;
517 scsipi_channel_freeze(&isp->isp_chanA, 1);
518 }
519 xs->error = XS_REQUEUE;
520 scsipi_done(xs);
521 break;
522 case CMD_COMPLETE:
523 scsipi_done(xs);
524 break;
525 }
526 ISP_UNLOCK(isp);
527 }
528
529 static void
530 isprequest(struct scsipi_channel *chan, scsipi_adapter_req_t req, void *arg)
531 {
532 struct ispsoftc *isp = (void *)chan->chan_adapter->adapt_dev;
533
534 switch (req) {
535 case ADAPTER_REQ_RUN_XFER:
536 ispcmd(isp, (XS_T *) arg);
537 break;
538
539 case ADAPTER_REQ_GROW_RESOURCES:
540 /* Not supported. */
541 break;
542
543 case ADAPTER_REQ_SET_XFER_MODE:
544 if (IS_SCSI(isp)) {
545 struct scsipi_xfer_mode *xm = arg;
546 int dflags = 0;
547 sdparam *sdp = SDPARAM(isp);
548
549 sdp += chan->chan_channel;
550 if (xm->xm_mode & PERIPH_CAP_TQING)
551 dflags |= DPARM_TQING;
552 if (xm->xm_mode & PERIPH_CAP_WIDE16)
553 dflags |= DPARM_WIDE;
554 if (xm->xm_mode & PERIPH_CAP_SYNC)
555 dflags |= DPARM_SYNC;
556 ISP_LOCK(isp);
557 sdp->isp_devparam[xm->xm_target].goal_flags |= dflags;
558 dflags = sdp->isp_devparam[xm->xm_target].goal_flags;
559 sdp->isp_devparam[xm->xm_target].dev_update = 1;
560 isp->isp_update |= (1 << chan->chan_channel);
561 ISP_UNLOCK(isp);
562 isp_prt(isp, ISP_LOGDEBUG1,
563 "ispioctl: device flags 0x%x for %d.%d.X",
564 dflags, chan->chan_channel, xm->xm_target);
565 break;
566 }
567 default:
568 break;
569 }
570 }
571
572 static void
573 isp_polled_cmd(struct ispsoftc *isp, XS_T *xs)
574 {
575 int result;
576 int infinite = 0, mswait;
577
578 result = isp_start(xs);
579
580 switch (result) {
581 case CMD_QUEUED:
582 break;
583 case CMD_RQLATER:
584 if (XS_NOERR(xs)) {
585 xs->error = XS_REQUEUE;
586 }
587 case CMD_EAGAIN:
588 if (XS_NOERR(xs)) {
589 xs->error = XS_RESOURCE_SHORTAGE;
590 }
591 /* FALLTHROUGH */
592 case CMD_COMPLETE:
593 scsipi_done(xs);
594 return;
595
596 }
597
598 /*
599 * If we can't use interrupts, poll on completion.
600 */
601 if ((mswait = XS_TIME(xs)) == 0)
602 infinite = 1;
603
604 while (mswait || infinite) {
605 u_int16_t isr, sema, mbox;
606 if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
607 isp_intr(isp, isr, sema, mbox);
608 if (XS_CMD_DONE_P(xs)) {
609 break;
610 }
611 }
612 USEC_DELAY(1000);
613 mswait -= 1;
614 }
615
616 /*
617 * If no other error occurred but we didn't finish,
618 * something bad happened.
619 */
620 if (XS_CMD_DONE_P(xs) == 0) {
621 if (isp_control(isp, ISPCTL_ABORT_CMD, xs)) {
622 isp_reinit(isp);
623 }
624 if (XS_NOERR(xs)) {
625 XS_SETERR(xs, HBA_BOTCH);
626 }
627 }
628 scsipi_done(xs);
629 }
630
631 void
632 isp_done(XS_T *xs)
633 {
634 XS_CMD_S_DONE(xs);
635 if (XS_CMD_WDOG_P(xs) == 0) {
636 struct ispsoftc *isp = XS_ISP(xs);
637 callout_stop(&xs->xs_callout);
638 if (XS_CMD_GRACE_P(xs)) {
639 isp_prt(isp, ISP_LOGDEBUG1,
640 "finished command on borrowed time");
641 }
642 XS_CMD_S_CLEAR(xs);
643 /*
644 * Fixup- if we get a QFULL, we need
645 * to set XS_BUSY as the error.
646 */
647 if (xs->status == SCSI_QUEUE_FULL) {
648 xs->error = XS_BUSY;
649 }
650 if (isp->isp_osinfo.paused) {
651 isp->isp_osinfo.paused = 0;
652 scsipi_channel_timed_thaw(&isp->isp_chanA);
653 if (IS_DUALBUS(isp)) {
654 scsipi_channel_timed_thaw(&isp->isp_chanB);
655 }
656 }
657 scsipi_done(xs);
658 }
659 }
660
661 static void
662 isp_dog(void *arg)
663 {
664 XS_T *xs = arg;
665 struct ispsoftc *isp = XS_ISP(xs);
666 u_int16_t handle;
667
668 ISP_ILOCK(isp);
669 /*
670 * We've decided this command is dead. Make sure we're not trying
671 * to kill a command that's already dead by getting it's handle and
672 * and seeing whether it's still alive.
673 */
674 handle = isp_find_handle(isp, xs);
675 if (handle) {
676 u_int16_t isr, mbox, sema;
677
678 if (XS_CMD_DONE_P(xs)) {
679 isp_prt(isp, ISP_LOGDEBUG1,
680 "watchdog found done cmd (handle 0x%x)", handle);
681 ISP_IUNLOCK(isp);
682 return;
683 }
684
685 if (XS_CMD_WDOG_P(xs)) {
686 isp_prt(isp, ISP_LOGDEBUG1,
687 "recursive watchdog (handle 0x%x)", handle);
688 ISP_IUNLOCK(isp);
689 return;
690 }
691
692 XS_CMD_S_WDOG(xs);
693
694 if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
695 isp_intr(isp, isr, sema, mbox);
696
697 }
698 if (XS_CMD_DONE_P(xs)) {
699 isp_prt(isp, ISP_LOGDEBUG1,
700 "watchdog cleanup for handle 0x%x", handle);
701 XS_CMD_C_WDOG(xs);
702 isp_done(xs);
703 } else if (XS_CMD_GRACE_P(xs)) {
704 isp_prt(isp, ISP_LOGDEBUG1,
705 "watchdog timeout for handle 0x%x", handle);
706 /*
707 * Make sure the command is *really* dead before we
708 * release the handle (and DMA resources) for reuse.
709 */
710 (void) isp_control(isp, ISPCTL_ABORT_CMD, arg);
711
712 /*
713 * After this point, the comamnd is really dead.
714 */
715 if (XS_XFRLEN(xs)) {
716 ISP_DMAFREE(isp, xs, handle);
717 }
718 isp_destroy_handle(isp, handle);
719 XS_SETERR(xs, XS_TIMEOUT);
720 XS_CMD_S_CLEAR(xs);
721 isp_done(xs);
722 } else {
723 u_int16_t nxti, optr;
724 ispreq_t local, *mp = &local, *qe;
725 isp_prt(isp, ISP_LOGDEBUG2,
726 "possible command timeout on handle %x", handle);
727 XS_CMD_C_WDOG(xs);
728 callout_reset(&xs->xs_callout, hz, isp_dog, xs);
729 if (isp_getrqentry(isp, &nxti, &optr, (void **) &qe)) {
730 ISP_UNLOCK(isp);
731 return;
732 }
733 XS_CMD_S_GRACE(xs);
734 MEMZERO((void *) mp, sizeof (*mp));
735 mp->req_header.rqs_entry_count = 1;
736 mp->req_header.rqs_entry_type = RQSTYPE_MARKER;
737 mp->req_modifier = SYNC_ALL;
738 mp->req_target = XS_CHANNEL(xs) << 7;
739 isp_put_request(isp, mp, qe);
740 ISP_ADD_REQUEST(isp, nxti);
741 }
742 } else {
743 isp_prt(isp, ISP_LOGDEBUG0, "watchdog with no command");
744 }
745 ISP_IUNLOCK(isp);
746 }
747
748 /*
749 * Fibre Channel state cleanup thread
750 */
751 static void
752 isp_create_fc_worker(void *arg)
753 {
754 struct ispsoftc *isp = arg;
755
756 if (kthread_create1(isp_fc_worker, isp, &isp->isp_osinfo.thread,
757 "%s:fc_thrd", isp->isp_name)) {
758 isp_prt(isp, ISP_LOGERR, "unable to create FC worker thread");
759 panic("isp_create_fc_worker");
760 }
761
762 }
763
764 static void
765 isp_fc_worker(void *arg)
766 {
767 void scsipi_run_queue(struct scsipi_channel *);
768 struct ispsoftc *isp = arg;
769
770 for (;;) {
771 int s;
772
773 /*
774 * Note we do *not* use the ISP_LOCK/ISP_UNLOCK macros here.
775 */
776 s = splbio();
777 while (isp->isp_osinfo.threadwork) {
778 isp->isp_osinfo.threadwork = 0;
779 if (isp_fc_runstate(isp, 10 * 1000000) == 0) {
780 break;
781 }
782 if (isp->isp_osinfo.loop_checked &&
783 FCPARAM(isp)->loop_seen_once == 0) {
784 splx(s);
785 goto skip;
786 }
787 isp->isp_osinfo.threadwork = 1;
788 splx(s);
789 delay(500 * 1000);
790 s = splbio();
791 }
792 if (FCPARAM(isp)->isp_fwstate != FW_READY ||
793 FCPARAM(isp)->isp_loopstate != LOOP_READY) {
794 isp_prt(isp, ISP_LOGINFO, "isp_fc_runstate in vain");
795 isp->isp_osinfo.threadwork = 1;
796 splx(s);
797 continue;
798 }
799
800 if (isp->isp_osinfo.blocked) {
801 isp->isp_osinfo.blocked = 0;
802 isp_prt(isp, ISP_LOGDEBUG0,
803 "restarting queues (freeze count %d)",
804 isp->isp_chanA.chan_qfreeze);
805 scsipi_channel_thaw(&isp->isp_chanA, 1);
806 }
807
808 if (isp->isp_osinfo.thread == NULL)
809 break;
810
811 skip:
812 (void) tsleep(&isp->isp_osinfo.thread, PRIBIO, "fcclnup", 0);
813
814 splx(s);
815 }
816
817 /* In case parent is waiting for us to exit. */
818 wakeup(&isp->isp_osinfo.thread);
819
820 kthread_exit(0);
821 }
822
823 /*
824 * Free any associated resources prior to decommissioning and
825 * set the card to a known state (so it doesn't wake up and kick
826 * us when we aren't expecting it to).
827 *
828 * Locks are held before coming here.
829 */
830 void
831 isp_uninit(struct ispsoftc *isp)
832 {
833 isp_lock(isp);
834 /*
835 * Leave with interrupts disabled.
836 */
837 DISABLE_INTS(isp);
838 isp_unlock(isp);
839 }
840
841 int
842 isp_async(struct ispsoftc *isp, ispasync_t cmd, void *arg)
843 {
844 int bus, tgt;
845
846 switch (cmd) {
847 case ISPASYNC_NEW_TGT_PARAMS:
848 if (IS_SCSI(isp) && isp->isp_dblev) {
849 sdparam *sdp = isp->isp_param;
850 int flags;
851 struct scsipi_xfer_mode xm;
852
853 tgt = *((int *) arg);
854 bus = (tgt >> 16) & 0xffff;
855 tgt &= 0xffff;
856 sdp += bus;
857 flags = sdp->isp_devparam[tgt].actv_flags;
858
859 xm.xm_mode = 0;
860 xm.xm_period = sdp->isp_devparam[tgt].actv_period;
861 xm.xm_offset = sdp->isp_devparam[tgt].actv_offset;
862 xm.xm_target = tgt;
863
864 if ((flags & DPARM_SYNC) && xm.xm_period && xm.xm_offset)
865 xm.xm_mode |= PERIPH_CAP_SYNC;
866 if (flags & DPARM_WIDE)
867 xm.xm_mode |= PERIPH_CAP_WIDE16;
868 if (flags & DPARM_TQING)
869 xm.xm_mode |= PERIPH_CAP_TQING;
870 scsipi_async_event(bus? &isp->isp_chanB : &isp->isp_chanA,
871 ASYNC_EVENT_XFER_MODE, &xm);
872 break;
873 }
874 case ISPASYNC_BUS_RESET:
875 bus = *((int *) arg);
876 scsipi_async_event(bus? &isp->isp_chanB : &isp->isp_chanA,
877 ASYNC_EVENT_RESET, NULL);
878 isp_prt(isp, ISP_LOGINFO, "SCSI bus %d reset detected", bus);
879 break;
880 case ISPASYNC_LIP:
881 /*
882 * Don't do queue freezes or blockage until we have the
883 * thread running that can unfreeze/unblock us.
884 */
885 if (isp->isp_osinfo.blocked == 0) {
886 if (isp->isp_osinfo.thread) {
887 isp->isp_osinfo.blocked = 1;
888 scsipi_channel_freeze(&isp->isp_chanA, 1);
889 }
890 }
891 isp_prt(isp, ISP_LOGINFO, "LIP Received");
892 break;
893 case ISPASYNC_LOOP_RESET:
894 /*
895 * Don't do queue freezes or blockage until we have the
896 * thread running that can unfreeze/unblock us.
897 */
898 if (isp->isp_osinfo.blocked == 0) {
899 if (isp->isp_osinfo.thread) {
900 isp->isp_osinfo.blocked = 1;
901 scsipi_channel_freeze(&isp->isp_chanA, 1);
902 }
903 }
904 isp_prt(isp, ISP_LOGINFO, "Loop Reset Received");
905 break;
906 case ISPASYNC_LOOP_DOWN:
907 /*
908 * Don't do queue freezes or blockage until we have the
909 * thread running that can unfreeze/unblock us.
910 */
911 if (isp->isp_osinfo.blocked == 0) {
912 if (isp->isp_osinfo.thread) {
913 isp->isp_osinfo.blocked = 1;
914 scsipi_channel_freeze(&isp->isp_chanA, 1);
915 }
916 }
917 isp_prt(isp, ISP_LOGINFO, "Loop DOWN");
918 break;
919 case ISPASYNC_LOOP_UP:
920 /*
921 * Let the subsequent ISPASYNC_CHANGE_NOTIFY invoke
922 * the FC worker thread. When the FC worker thread
923 * is done, let *it* call scsipi_channel_thaw...
924 */
925 isp_prt(isp, ISP_LOGINFO, "Loop UP");
926 break;
927 case ISPASYNC_PROMENADE:
928 if (IS_FC(isp) && isp->isp_dblev) {
929 static const char fmt[] = "Target %d (Loop 0x%x) Port ID 0x%x "
930 "(role %s) %s\n Port WWN 0x%08x%08x\n Node WWN 0x%08x%08x";
931 const static char *const roles[4] = {
932 "None", "Target", "Initiator", "Target/Initiator"
933 };
934 fcparam *fcp = isp->isp_param;
935 int tgt = *((int *) arg);
936 struct lportdb *lp = &fcp->portdb[tgt];
937
938 isp_prt(isp, ISP_LOGINFO, fmt, tgt, lp->loopid, lp->portid,
939 roles[lp->roles & 0x3],
940 (lp->valid)? "Arrived" : "Departed",
941 (u_int32_t) (lp->port_wwn >> 32),
942 (u_int32_t) (lp->port_wwn & 0xffffffffLL),
943 (u_int32_t) (lp->node_wwn >> 32),
944 (u_int32_t) (lp->node_wwn & 0xffffffffLL));
945 break;
946 }
947 case ISPASYNC_CHANGE_NOTIFY:
948 if (arg == ISPASYNC_CHANGE_PDB) {
949 isp_prt(isp, ISP_LOGINFO, "Port Database Changed");
950 } else if (arg == ISPASYNC_CHANGE_SNS) {
951 isp_prt(isp, ISP_LOGINFO,
952 "Name Server Database Changed");
953 }
954
955 /*
956 * We can set blocked here because we know it's now okay
957 * to try and run isp_fc_runstate (in order to build loop
958 * state). But we don't try and freeze the midlayer's queue
959 * if we have no thread that we can wake to later unfreeze
960 * it.
961 */
962 if (isp->isp_osinfo.blocked == 0) {
963 isp->isp_osinfo.blocked = 1;
964 if (isp->isp_osinfo.thread) {
965 scsipi_channel_freeze(&isp->isp_chanA, 1);
966 }
967 }
968 /*
969 * Note that we have work for the thread to do, and
970 * if the thread is here already, wake it up.
971 */
972 isp->isp_osinfo.threadwork++;
973 if (isp->isp_osinfo.thread) {
974 wakeup(&isp->isp_osinfo.thread);
975 } else {
976 isp_prt(isp, ISP_LOGDEBUG1, "no FC thread yet");
977 }
978 break;
979 case ISPASYNC_FABRIC_DEV:
980 {
981 int target, base, lim;
982 fcparam *fcp = isp->isp_param;
983 struct lportdb *lp = NULL;
984 struct lportdb *clp = (struct lportdb *) arg;
985 char *pt;
986
987 switch (clp->port_type) {
988 case 1:
989 pt = " N_Port";
990 break;
991 case 2:
992 pt = " NL_Port";
993 break;
994 case 3:
995 pt = "F/NL_Port";
996 break;
997 case 0x7f:
998 pt = " Nx_Port";
999 break;
1000 case 0x81:
1001 pt = " F_port";
1002 break;
1003 case 0x82:
1004 pt = " FL_Port";
1005 break;
1006 case 0x84:
1007 pt = " E_port";
1008 break;
1009 default:
1010 pt = " ";
1011 break;
1012 }
1013
1014 isp_prt(isp, ISP_LOGINFO,
1015 "%s Fabric Device @ PortID 0x%x", pt, clp->portid);
1016
1017 /*
1018 * If we don't have an initiator role we bail.
1019 *
1020 * We just use ISPASYNC_FABRIC_DEV for announcement purposes.
1021 */
1022
1023 if ((isp->isp_role & ISP_ROLE_INITIATOR) == 0) {
1024 break;
1025 }
1026
1027 /*
1028 * Is this entry for us? If so, we bail.
1029 */
1030
1031 if (fcp->isp_portid == clp->portid) {
1032 break;
1033 }
1034
1035 /*
1036 * Else, the default policy is to find room for it in
1037 * our local port database. Later, when we execute
1038 * the call to isp_pdb_sync either this newly arrived
1039 * or already logged in device will be (re)announced.
1040 */
1041
1042 if (fcp->isp_topo == TOPO_FL_PORT)
1043 base = FC_SNS_ID+1;
1044 else
1045 base = 0;
1046
1047 if (fcp->isp_topo == TOPO_N_PORT)
1048 lim = 1;
1049 else
1050 lim = MAX_FC_TARG;
1051
1052 /*
1053 * Is it already in our list?
1054 */
1055 for (target = base; target < lim; target++) {
1056 if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
1057 continue;
1058 }
1059 lp = &fcp->portdb[target];
1060 if (lp->port_wwn == clp->port_wwn &&
1061 lp->node_wwn == clp->node_wwn) {
1062 lp->fabric_dev = 1;
1063 break;
1064 }
1065 }
1066 if (target < lim) {
1067 break;
1068 }
1069 for (target = base; target < lim; target++) {
1070 if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
1071 continue;
1072 }
1073 lp = &fcp->portdb[target];
1074 if (lp->port_wwn == 0) {
1075 break;
1076 }
1077 }
1078 if (target == lim) {
1079 isp_prt(isp, ISP_LOGWARN,
1080 "out of space for fabric devices");
1081 break;
1082 }
1083 lp->port_type = clp->port_type;
1084 lp->fc4_type = clp->fc4_type;
1085 lp->node_wwn = clp->node_wwn;
1086 lp->port_wwn = clp->port_wwn;
1087 lp->portid = clp->portid;
1088 lp->fabric_dev = 1;
1089 break;
1090 }
1091 case ISPASYNC_FW_CRASH:
1092 {
1093 u_int16_t mbox1, mbox6;
1094 mbox1 = ISP_READ(isp, OUTMAILBOX1);
1095 if (IS_DUALBUS(isp)) {
1096 mbox6 = ISP_READ(isp, OUTMAILBOX6);
1097 } else {
1098 mbox6 = 0;
1099 }
1100 isp_prt(isp, ISP_LOGERR,
1101 "Internal Firmware Error on bus %d @ RISC Address 0x%x",
1102 mbox6, mbox1);
1103 isp_reinit(isp);
1104 break;
1105 }
1106 default:
1107 break;
1108 }
1109 return (0);
1110 }
1111
1112 #include <machine/stdarg.h>
1113 void
1114 isp_prt(struct ispsoftc *isp, int level, const char *fmt, ...)
1115 {
1116 va_list ap;
1117 if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
1118 return;
1119 }
1120 printf("%s: ", isp->isp_name);
1121 va_start(ap, fmt);
1122 vprintf(fmt, ap);
1123 va_end(ap);
1124 printf("\n");
1125 }
1126