isp_netbsd.c revision 1.16 1 /* $NetBSD: isp_netbsd.c,v 1.16 1999/09/30 23:06:18 thorpej 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 0 /* XXX THORPEJ */
183 if ((xs->xs_control & XS_CTL_DISCOVERY) == 0 &&
184 (isp->isp_type & ISP_HA_SCSI)) {
185 sdparam *sdp = isp->isp_param;
186 sdp += XS_CHANNEL(xs);
187 if (sdp->isp_devparam[XS_TGT(xs)].dev_flags !=
188 sdp->isp_devparam[XS_TGT(xs)].cur_dflags) {
189 u_int16_t f = DPARM_WIDE|DPARM_SYNC|DPARM_TQING;
190 if (xs->sc_link->quirks & SDEV_NOSYNC)
191 f &= ~DPARM_SYNC;
192 if (xs->sc_link->quirks & SDEV_NOWIDE)
193 f &= ~DPARM_WIDE;
194 if (xs->sc_link->quirks & SDEV_NOTAG)
195 f &= ~DPARM_TQING;
196 sdp->isp_devparam[XS_TGT(xs)].dev_flags &=
197 ~(DPARM_WIDE|DPARM_SYNC|DPARM_TQING);
198 sdp->isp_devparam[XS_TGT(xs)].dev_flags |= f;
199 sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
200 isp->isp_update |= (1 << XS_CHANNEL(xs));
201 }
202 }
203 #endif /* XXX THORPEJ */
204
205 if (isp->isp_state < ISP_RUNSTATE) {
206 DISABLE_INTS(isp);
207 isp_init(isp);
208 if (isp->isp_state != ISP_INITSTATE) {
209 ENABLE_INTS(isp);
210 (void) splx(s);
211 XS_SETERR(xs, HBA_BOTCH);
212 return (CMD_COMPLETE);
213 }
214 isp->isp_state = ISP_RUNSTATE;
215 ENABLE_INTS(isp);
216 }
217
218 /*
219 * Check for queue blockage...
220 */
221
222 if (isp->isp_osinfo.blocked) {
223 if (xs->xs_control & XS_CTL_POLL) {
224 xs->error = XS_DRIVER_STUFFUP;
225 splx(s);
226 return (TRY_AGAIN_LATER);
227 }
228 TAILQ_INSERT_TAIL(&isp->isp_osinfo.waitq, xs, adapter_q);
229 splx(s);
230 return (CMD_QUEUED);
231 }
232
233 DISABLE_INTS(isp);
234 result = ispscsicmd(xs);
235 ENABLE_INTS(isp);
236 if (result != CMD_QUEUED || (xs->xs_control & XS_CTL_POLL) == 0) {
237 (void) splx(s);
238 return (result);
239 }
240
241 /*
242 * If we can't use interrupts, poll on completion.
243 */
244 if (isp_poll(isp, xs, XS_TIME(xs))) {
245 /*
246 * If no other error occurred but we didn't finish,
247 * something bad happened.
248 */
249 if (XS_IS_CMD_DONE(xs) == 0) {
250 isp->isp_nactive--;
251 if (isp->isp_nactive < 0)
252 isp->isp_nactive = 0;
253 if (XS_NOERR(xs)) {
254 isp_lostcmd(isp, xs);
255 XS_SETERR(xs, HBA_BOTCH);
256 }
257 }
258 }
259 (void) splx(s);
260 return (CMD_COMPLETE);
261 }
262
263 static int
264 isp_poll(isp, xs, mswait)
265 struct ispsoftc *isp;
266 ISP_SCSI_XFER_T *xs;
267 int mswait;
268 {
269
270 while (mswait) {
271 /* Try the interrupt handling routine */
272 (void)isp_intr((void *)isp);
273
274 /* See if the xs is now done */
275 if (XS_IS_CMD_DONE(xs)) {
276 return (0);
277 }
278 SYS_DELAY(1000); /* wait one millisecond */
279 mswait--;
280 }
281 return (1);
282 }
283
284 static void
285 isp_watch(arg)
286 void *arg;
287 {
288 int i;
289 struct ispsoftc *isp = arg;
290 ISP_SCSI_XFER_T *xs;
291 ISP_ILOCKVAL_DECL;
292
293 /*
294 * Look for completely dead commands (but not polled ones).
295 */
296 ISP_ILOCK(isp);
297 for (i = 0; i < RQUEST_QUEUE_LEN; i++) {
298 if ((xs = (ISP_SCSI_XFER_T *) isp->isp_xflist[i]) == NULL) {
299 continue;
300 }
301 if (XS_TIME(xs) == 0) {
302 continue;
303 }
304 XS_TIME(xs) -= (WATCH_INTERVAL * 1000);
305 /*
306 * Avoid later thinking that this
307 * transaction is not being timed.
308 * Then give ourselves to watchdog
309 * periods of grace.
310 */
311 if (XS_TIME(xs) == 0) {
312 XS_TIME(xs) = 1;
313 } else if (XS_TIME(xs) > -(2 * WATCH_INTERVAL * 1000)) {
314 continue;
315 }
316 if (isp_control(isp, ISPCTL_ABORT_CMD, xs)) {
317 printf("%s: isp_watch failed to abort command\n",
318 isp->isp_name);
319 isp_restart(isp);
320 break;
321 }
322 }
323 timeout(isp_watch, isp, WATCH_INTERVAL * hz);
324 isp->isp_dogactive = 1;
325 ISP_IUNLOCK(isp);
326 }
327
328 /*
329 * Free any associated resources prior to decommissioning and
330 * set the card to a known state (so it doesn't wake up and kick
331 * us when we aren't expecting it to).
332 *
333 * Locks are held before coming here.
334 */
335 void
336 isp_uninit(isp)
337 struct ispsoftc *isp;
338 {
339 ISP_ILOCKVAL_DECL;
340 ISP_ILOCK(isp);
341 /*
342 * Leave with interrupts disabled.
343 */
344 DISABLE_INTS(isp);
345
346 /*
347 * Turn off the watchdog (if active).
348 */
349 if (isp->isp_dogactive) {
350 untimeout(isp_watch, isp);
351 isp->isp_dogactive = 0;
352 }
353
354 ISP_IUNLOCK(isp);
355 }
356
357 /*
358 * Restart function after a LOOP UP event (e.g.),
359 * done as a timeout for some hysteresis.
360 */
361 static void
362 isp_internal_restart(arg)
363 void *arg;
364 {
365 struct ispsoftc *isp = arg;
366 int result, nrestarted = 0, s;
367
368 s = splbio();
369 if (isp->isp_osinfo.blocked == 0) {
370 struct scsipi_xfer *xs;
371 while ((xs = TAILQ_FIRST(&isp->isp_osinfo.waitq)) != NULL) {
372 TAILQ_REMOVE(&isp->isp_osinfo.waitq, xs, adapter_q);
373 DISABLE_INTS(isp);
374 result = ispscsicmd(xs);
375 ENABLE_INTS(isp);
376 if (result != CMD_QUEUED) {
377 printf("%s: botched command restart (0x%x)\n",
378 isp->isp_name, result);
379 xs->xs_status |= XS_STS_DONE;
380 if (xs->error == XS_NOERROR)
381 xs->error = XS_DRIVER_STUFFUP;
382 scsipi_done(xs);
383 }
384 nrestarted++;
385 }
386 printf("%s: requeued %d commands\n", isp->isp_name, nrestarted);
387 }
388 (void) splx(s);
389 }
390
391 int
392 isp_async(isp, cmd, arg)
393 struct ispsoftc *isp;
394 ispasync_t cmd;
395 void *arg;
396 {
397 int bus, tgt;
398 int s = splbio();
399 switch (cmd) {
400 case ISPASYNC_NEW_TGT_PARAMS:
401 if (isp->isp_type & ISP_HA_SCSI) {
402 sdparam *sdp = isp->isp_param;
403 char *wt;
404 int mhz, flags, period;
405
406 tgt = *((int *) arg);
407 bus = (tgt >> 16) & 0xffff;
408 tgt &= 0xffff;
409
410 flags = sdp->isp_devparam[tgt].cur_dflags;
411 period = sdp->isp_devparam[tgt].cur_period;
412 if ((flags & DPARM_SYNC) && period &&
413 (sdp->isp_devparam[tgt].cur_offset) != 0) {
414 if (sdp->isp_lvdmode) {
415 switch (period) {
416 case 0xa:
417 mhz = 40;
418 break;
419 case 0xb:
420 mhz = 33;
421 break;
422 case 0xc:
423 mhz = 25;
424 break;
425 default:
426 mhz = 1000 / (period * 4);
427 break;
428 }
429 } else {
430 mhz = 1000 / (period * 4);
431 }
432 } else {
433 mhz = 0;
434 }
435 switch (flags & (DPARM_WIDE|DPARM_TQING)) {
436 case DPARM_WIDE:
437 wt = ", 16 bit wide\n";
438 break;
439 case DPARM_TQING:
440 wt = ", Tagged Queueing Enabled\n";
441 break;
442 case DPARM_WIDE|DPARM_TQING:
443 wt = ", 16 bit wide, Tagged Queueing Enabled\n";
444 break;
445 default:
446 wt = "\n";
447 break;
448 }
449 if (mhz) {
450 printf("%s: Bus %d Target %d at %dMHz Max "
451 "Offset %d%s", isp->isp_name, bus, tgt, mhz,
452 sdp->isp_devparam[tgt].cur_offset, wt);
453 } else {
454 printf("%s: Bus %d Target %d Async Mode%s",
455 isp->isp_name, bus, tgt, wt);
456 }
457 }
458 break;
459 case ISPASYNC_BUS_RESET:
460 if (arg)
461 bus = *((int *) arg);
462 else
463 bus = 0;
464 printf("%s: SCSI bus %d reset detected\n", isp->isp_name, bus);
465 break;
466 case ISPASYNC_LOOP_DOWN:
467 /*
468 * Hopefully we get here in time to minimize the number
469 * of commands we are firing off that are sure to die.
470 */
471 isp->isp_osinfo.blocked = 1;
472 printf("%s: Loop DOWN\n", isp->isp_name);
473 break;
474 case ISPASYNC_LOOP_UP:
475 isp->isp_osinfo.blocked = 0;
476 timeout(isp_internal_restart, isp, 1);
477 printf("%s: Loop UP\n", isp->isp_name);
478 break;
479 case ISPASYNC_PDB_CHANGED:
480 if (IS_FC(isp)) {
481 const char *fmt = "%s: Target %d (Loop 0x%x) Port ID 0x%x "
482 "role %s %s\n Port WWN 0x%08x%08x\n Node WWN 0x%08x%08x\n";
483 const static char *roles[4] = {
484 "No", "Target", "Initiator", "Target/Initiator"
485 };
486 char *ptr;
487 fcparam *fcp = isp->isp_param;
488 int tgt = *((int *) arg);
489 struct lportdb *lp = &fcp->portdb[tgt];
490
491 if (lp->valid) {
492 ptr = "arrived";
493 } else {
494 ptr = "disappeared";
495 }
496 printf(fmt, isp->isp_name, tgt, lp->loopid, lp->portid,
497 roles[lp->roles & 0x3], ptr,
498 (u_int32_t) (lp->port_wwn >> 32),
499 (u_int32_t) (lp->port_wwn & 0xffffffffLL),
500 (u_int32_t) (lp->node_wwn >> 32),
501 (u_int32_t) (lp->node_wwn & 0xffffffffLL));
502 break;
503 }
504 #ifdef ISP2100_FABRIC
505 case ISPASYNC_CHANGE_NOTIFY:
506 printf("%s: Name Server Database Changed\n", isp->isp_name);
507 break;
508 case ISPASYNC_FABRIC_DEV:
509 {
510 int target;
511 struct lportdb *lp;
512 sns_scrsp_t *resp = (sns_scrsp_t *) arg;
513 u_int32_t portid;
514 u_int64_t wwn;
515 fcparam *fcp = isp->isp_param;
516
517 portid =
518 (((u_int32_t) resp->snscb_port_id[0]) << 16) |
519 (((u_int32_t) resp->snscb_port_id[1]) << 8) |
520 (((u_int32_t) resp->snscb_port_id[2]));
521 wwn =
522 (((u_int64_t)resp->snscb_portname[0]) << 56) |
523 (((u_int64_t)resp->snscb_portname[1]) << 48) |
524 (((u_int64_t)resp->snscb_portname[2]) << 40) |
525 (((u_int64_t)resp->snscb_portname[3]) << 32) |
526 (((u_int64_t)resp->snscb_portname[4]) << 24) |
527 (((u_int64_t)resp->snscb_portname[5]) << 16) |
528 (((u_int64_t)resp->snscb_portname[6]) << 8) |
529 (((u_int64_t)resp->snscb_portname[7]));
530 printf("%s: Fabric Device (Type 0x%x)@PortID 0x%x WWN "
531 "0x%08x%08x\n", isp->isp_name, resp->snscb_port_type,
532 portid, ((u_int32_t)(wwn >> 32)),
533 ((u_int32_t)(wwn & 0xffffffff)));
534 if (resp->snscb_port_type != 2)
535 break;
536 for (target = FC_SNS_ID+1; target < MAX_FC_TARG; target++) {
537 lp = &fcp->portdb[target];
538 if (lp->port_wwn == wwn)
539 break;
540 }
541 if (target < MAX_FC_TARG) {
542 break;
543 }
544 for (target = FC_SNS_ID+1; target < MAX_FC_TARG; target++) {
545 lp = &fcp->portdb[target];
546 if (lp->port_wwn == 0)
547 break;
548 }
549 if (target == MAX_FC_TARG) {
550 printf("%s: no more space for fabric devices\n",
551 isp->isp_name);
552 return (-1);
553 }
554 lp->port_wwn = lp->node_wwn = wwn;
555 lp->portid = portid;
556 break;
557 }
558 #endif
559 default:
560 break;
561 }
562 (void) splx(s);
563 return (0);
564 }
565