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