isp_target.c revision 1.12 1 /* $NetBSD: isp_target.c,v 1.12 2001/03/14 05:45:16 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 * Machine and OS Independent Target Mode Code for the Qlogic SCSI/FC adapters.
32 *
33 * Copyright (c) 1999, 2000, 2001 by Matthew Jacob
34 * All rights reserved.
35 * mjacob (at) feral.com
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice immediately at the beginning of the file, without modification,
42 * this list of conditions, and the following disclaimer.
43 * 2. The name of the author may not be used to endorse or promote products
44 * derived from this software without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
50 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 */
58
59 /*
60 * Include header file appropriate for platform we're building on.
61 */
62
63 #ifdef __NetBSD__
64 #include <dev/ic/isp_netbsd.h>
65 #endif
66 #ifdef __FreeBSD__
67 #include <dev/isp/isp_freebsd.h>
68 #endif
69 #ifdef __OpenBSD__
70 #include <dev/ic/isp_openbsd.h>
71 #endif
72 #ifdef __linux__
73 #include "isp_linux.h"
74 #endif
75
76 #ifdef ISP_TARGET_MODE
77 static char *atiocope =
78 "ATIO returned for lun %d because it was in the middle of Bus Device Reset";
79 static char *atior =
80 "ATIO returned for lun %d from initiator %d because a Bus Reset occurred";
81
82 static void isp_got_msg __P((struct ispsoftc *, int, in_entry_t *));
83 static void isp_got_msg_fc __P((struct ispsoftc *, int, in_fcentry_t *));
84 static void isp_notify_ack __P((struct ispsoftc *, void *));
85 static void isp_handle_atio(struct ispsoftc *, at_entry_t *);
86 static void isp_handle_atio2(struct ispsoftc *, at2_entry_t *);
87 static void isp_handle_ctio(struct ispsoftc *, ct_entry_t *);
88 static void isp_handle_ctio2(struct ispsoftc *, ct2_entry_t *);
89
90 /*
91 * The Qlogic driver gets an interrupt to look at response queue entries.
92 * Some of these are status completions for initiatior mode commands, but
93 * if target mode is enabled, we get a whole wad of response queue entries
94 * to be handled here.
95 *
96 * Basically the split into 3 main groups: Lun Enable/Modification responses,
97 * SCSI Command processing, and Immediate Notification events.
98 *
99 * You start by writing a request queue entry to enable target mode (and
100 * establish some resource limitations which you can modify later).
101 * The f/w responds with a LUN ENABLE or LUN MODIFY response with
102 * the status of this action. If the enable was successful, you can expect...
103 *
104 * Response queue entries with SCSI commands encapsulate show up in an ATIO
105 * (Accept Target IO) type- sometimes with enough info to stop the command at
106 * this level. Ultimately the driver has to feed back to the f/w's request
107 * queue a sequence of CTIOs (continue target I/O) that describe data to
108 * be moved and/or status to be sent) and finally finishing with sending
109 * to the f/w's response queue an ATIO which then completes the handshake
110 * with the f/w for that command. There's a lot of variations on this theme,
111 * including flags you can set in the CTIO for the Qlogic 2X00 fibre channel
112 * cards that 'auto-replenish' the f/w's ATIO count, but this is the basic
113 * gist of it.
114 *
115 * The third group that can show up in the response queue are Immediate
116 * Notification events. These include things like notifications of SCSI bus
117 * resets, or Bus Device Reset messages or other messages received. This
118 * a classic oddbins area. It can get a little weird because you then turn
119 * around and acknowledge the Immediate Notify by writing an entry onto the
120 * request queue and then the f/w turns around and gives you an acknowledgement
121 * to *your* acknowledgement on the response queue (the idea being to let
122 * the f/w tell you when the event is *really* over I guess).
123 *
124 */
125
126
127 /*
128 * A new response queue entry has arrived. The interrupt service code
129 * has already swizzled it into the platform dependent from canonical form.
130 *
131 * Because of the way this driver is designed, unfortunately most of the
132 * actual synchronization work has to be done in the platform specific
133 * code- we have no synchroniation primitives in the common code.
134 */
135
136 int
137 isp_target_notify(struct ispsoftc *isp, void *vptr, u_int16_t *optrp)
138 {
139 u_int16_t status, seqid;
140 union {
141 at_entry_t *atiop;
142 at2_entry_t *at2iop;
143 ct_entry_t *ctiop;
144 ct2_entry_t *ct2iop;
145 lun_entry_t *lunenp;
146 in_entry_t *inotp;
147 in_fcentry_t *inot_fcp;
148 na_entry_t *nackp;
149 na_fcentry_t *nack_fcp;
150 isphdr_t *hp;
151 void * *vp;
152 #define atiop unp.atiop
153 #define at2iop unp.at2iop
154 #define ctiop unp.ctiop
155 #define ct2iop unp.ct2iop
156 #define lunenp unp.lunenp
157 #define inotp unp.inotp
158 #define inot_fcp unp.inot_fcp
159 #define nackp unp.nackp
160 #define nack_fcp unp.nack_fcp
161 #define hdrp unp.hp
162 } unp;
163 int bus, rval = 0;
164
165 unp.vp = vptr;
166
167 ISP_TDQE(isp, "isp_target_notify", (int) *optrp, vptr);
168
169 switch(hdrp->rqs_entry_type) {
170 case RQSTYPE_ATIO:
171 isp_handle_atio(isp, atiop);
172 break;
173 case RQSTYPE_CTIO:
174 isp_handle_ctio(isp, ctiop);
175 break;
176 case RQSTYPE_ATIO2:
177 isp_handle_atio2(isp, at2iop);
178 break;
179 case RQSTYPE_CTIO2:
180 isp_handle_ctio2(isp, ct2iop);
181 break;
182 case RQSTYPE_ENABLE_LUN:
183 case RQSTYPE_MODIFY_LUN:
184 (void) isp_async(isp, ISPASYNC_TARGET_ACTION, vptr);
185 break;
186
187 case RQSTYPE_NOTIFY:
188 /*
189 * Either the ISP received a SCSI message it can't
190 * handle, or it's returning an Immed. Notify entry
191 * we sent. We can send Immed. Notify entries to
192 * increment the firmware's resource count for them
193 * (we set this initially in the Enable Lun entry).
194 */
195 bus = 0;
196 if (IS_FC(isp)) {
197 status = inot_fcp->in_status;
198 seqid = inot_fcp->in_seqid;
199 } else {
200 status = inotp->in_status & 0xff;
201 seqid = inotp->in_seqid;
202 if (IS_DUALBUS(isp)) {
203 bus = (inotp->in_iid & 0x80) >> 7;
204 inotp->in_iid &= ~0x80;
205 }
206 }
207 isp_prt(isp, ISP_LOGTDEBUG1,
208 "Immediate Notify, status=0x%x seqid=0x%x", status, seqid);
209 switch (status) {
210 case IN_RESET:
211 (void) isp_async(isp, ISPASYNC_BUS_RESET, &bus);
212 break;
213 case IN_MSG_RECEIVED:
214 case IN_IDE_RECEIVED:
215 if (IS_FC(isp)) {
216 isp_got_msg_fc(isp, bus, vptr);
217 } else {
218 isp_got_msg(isp, bus, vptr);
219 }
220 break;
221 case IN_RSRC_UNAVAIL:
222 isp_prt(isp, ISP_LOGWARN, "Firmware out of ATIOs");
223 break;
224 case IN_ABORT_TASK:
225 isp_prt(isp, ISP_LOGWARN,
226 "Abort Task for Initiator %d RX_ID 0x%x",
227 inot_fcp->in_iid, seqid);
228 break;
229 case IN_PORT_LOGOUT:
230 isp_prt(isp, ISP_LOGWARN,
231 "Port Logout for Initiator %d RX_ID 0x%x",
232 inot_fcp->in_iid, seqid);
233 break;
234 case IN_PORT_CHANGED:
235 isp_prt(isp, ISP_LOGWARN,
236 "Port Changed for Initiator %d RX_ID 0x%x",
237 inot_fcp->in_iid, seqid);
238 break;
239 case IN_GLOBAL_LOGO:
240 isp_prt(isp, ISP_LOGWARN, "All ports logged out");
241 break;
242 default:
243 isp_prt(isp, ISP_LOGERR,
244 "bad status (0x%x) in isp_target_notify", status);
245 break;
246 }
247 isp_notify_ack(isp, vptr);
248 break;
249
250 case RQSTYPE_NOTIFY_ACK:
251 /*
252 * The ISP is acknowledging our acknowledgement of an
253 * Immediate Notify entry for some asynchronous event.
254 */
255 if (IS_FC(isp)) {
256 isp_prt(isp, ISP_LOGTDEBUG1,
257 "Notify Ack status=0x%x seqid 0x%x",
258 nack_fcp->na_status, nack_fcp->na_seqid);
259 } else {
260 isp_prt(isp, ISP_LOGTDEBUG1,
261 "Notify Ack event 0x%x status=0x%x seqid 0x%x",
262 nackp->na_event, nackp->na_status, nackp->na_seqid);
263 }
264 break;
265 default:
266 isp_prt(isp, ISP_LOGERR,
267 "Unknown entry type 0x%x in isp_target_notify",
268 hdrp->rqs_entry_type);
269 rval = -1;
270 break;
271 }
272 #undef atiop
273 #undef at2iop
274 #undef ctiop
275 #undef ct2iop
276 #undef lunenp
277 #undef inotp
278 #undef inot_fcp
279 #undef nackp
280 #undef nack_fcp
281 #undef hdrp
282 return (rval);
283 }
284
285
286 /*
287 * Toggle (on/off) target mode for bus/target/lun
288 *
289 * The caller has checked for overlap and legality.
290 *
291 * Note that not all of bus, target or lun can be paid attention to.
292 * Note also that this action will not be complete until the f/w writes
293 * response entry. The caller is responsible for synchronizing this.
294 */
295 int
296 isp_lun_cmd(struct ispsoftc *isp, int cmd, int bus, int tgt,
297 int lun, u_int32_t opaque)
298 {
299 lun_entry_t el;
300 u_int16_t iptr, optr;
301 void *outp;
302
303
304 MEMZERO(&el, sizeof (el));
305 if (IS_DUALBUS(isp)) {
306 el.le_rsvd = (bus & 0x1) << 7;
307 }
308 el.le_cmd_count = DFLT_CMD_CNT;
309 el.le_in_count = DFLT_INOTIFY;
310 if (cmd == RQSTYPE_ENABLE_LUN) {
311 if (IS_SCSI(isp)) {
312 el.le_flags = LUN_TQAE|LUN_DISAD;
313 el.le_cdb6len = 12;
314 el.le_cdb7len = 12;
315 }
316 } else if (cmd == -RQSTYPE_ENABLE_LUN) {
317 cmd = RQSTYPE_ENABLE_LUN;
318 el.le_cmd_count = 0;
319 el.le_in_count = 0;
320 } else if (cmd == -RQSTYPE_MODIFY_LUN) {
321 cmd = RQSTYPE_MODIFY_LUN;
322 el.le_ops = LUN_CCDECR | LUN_INDECR;
323 } else {
324 el.le_ops = LUN_CCINCR | LUN_ININCR;
325 }
326 el.le_header.rqs_entry_type = cmd;
327 el.le_header.rqs_entry_count = 1;
328 el.le_reserved = opaque;
329 if (IS_SCSI(isp)) {
330 el.le_tgt = tgt;
331 el.le_lun = lun;
332 } else if (isp->isp_maxluns <= 16) {
333 el.le_lun = lun;
334 }
335
336 if (isp_getrqentry(isp, &iptr, &optr, &outp)) {
337 isp_prt(isp, ISP_LOGWARN,
338 "Request Queue Overflow in isp_lun_cmd");
339 return (-1);
340 }
341 ISP_SWIZ_ENABLE_LUN(isp, outp, &el);
342 ISP_TDQE(isp, "isp_lun_cmd", (int) optr, &el);
343 ISP_ADD_REQUEST(isp, iptr);
344 return (0);
345 }
346
347
348 int
349 isp_target_put_entry(struct ispsoftc *isp, void *ap)
350 {
351 void *outp;
352 u_int16_t iptr, optr;
353 u_int8_t etype = ((isphdr_t *) ap)->rqs_entry_type;
354
355 if (isp_getrqentry(isp, &iptr, &optr, &outp)) {
356 isp_prt(isp, ISP_LOGWARN,
357 "Request Queue Overflow in isp_target_put_entry");
358 return (-1);
359 }
360 switch (etype) {
361 case RQSTYPE_ATIO:
362 ISP_SWIZ_ATIO(isp, outp, ap);
363 break;
364 case RQSTYPE_ATIO2:
365 ISP_SWIZ_ATIO2(isp, outp, ap);
366 break;
367 case RQSTYPE_CTIO:
368 ISP_SWIZ_CTIO(isp, outp, ap);
369 break;
370 case RQSTYPE_CTIO2:
371 ISP_SWIZ_CTIO2(isp, outp, ap);
372 break;
373 default:
374 isp_prt(isp, ISP_LOGERR,
375 "Unknown type 0x%x in isp_put_entry", etype);
376 return (-1);
377 }
378
379 ISP_TDQE(isp, "isp_target_put_entry", (int) optr, ap);;
380
381 ISP_ADD_REQUEST(isp, iptr);
382 return (0);
383 }
384
385 int
386 isp_target_put_atio(struct ispsoftc *isp, int iid, int tgt, int lun, int ttype,
387 int tval)
388 {
389 union {
390 at_entry_t _atio;
391 at2_entry_t _atio2;
392 } atun;
393
394 MEMZERO(&atun, sizeof atun);
395 if (IS_FC(isp)) {
396 atun._atio2.at_header.rqs_entry_type = RQSTYPE_ATIO2;
397 atun._atio2.at_header.rqs_entry_count = 1;
398 if (isp->isp_maxluns > 16) {
399 atun._atio2.at_scclun = (u_int16_t) lun;
400 } else {
401 atun._atio2.at_lun = (u_int8_t) lun;
402 }
403 atun._atio2.at_status = CT_OK;
404 } else {
405 atun._atio.at_header.rqs_entry_type = RQSTYPE_ATIO;
406 atun._atio.at_header.rqs_entry_count = 1;
407 atun._atio.at_iid = iid;
408 atun._atio.at_tgt = tgt;
409 atun._atio.at_lun = lun;
410 atun._atio.at_tag_type = ttype;
411 atun._atio.at_tag_val = tval;
412 atun._atio.at_status = CT_OK;
413 }
414 return (isp_target_put_entry(isp, &atun));
415 }
416
417 /*
418 * Command completion- both for handling cases of no resources or
419 * no blackhole driver, or other cases where we have to, inline,
420 * finish the command sanely, or for normal command completion.
421 *
422 * The 'completion' code value has the scsi status byte in the low 8 bits.
423 * If status is a CHECK CONDITION and bit 8 is nonzero, then bits 12..15 have
424 * the sense key and bits 16..23 have the ASCQ and bits 24..31 have the ASC
425 * values.
426 *
427 * NB: the key, asc, ascq, cannot be used for parallel SCSI as it doesn't
428 * NB: inline SCSI sense reporting.
429 *
430 * For both parallel && fibre channel, we use the feature that does
431 * an automatic resource autoreplenish so we don't have then later do
432 * put of an atio to replenish the f/w's resource count.
433 */
434
435 int
436 isp_endcmd(struct ispsoftc *isp, void *arg, u_int32_t code, u_int16_t hdl)
437 {
438 int sts;
439 union {
440 ct_entry_t _ctio;
441 ct2_entry_t _ctio2;
442 } un;
443
444 MEMZERO(&un, sizeof un);
445 sts = code & 0xff;
446
447 if (IS_FC(isp)) {
448 at2_entry_t *aep = arg;
449 ct2_entry_t *cto = &un._ctio2;
450
451 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
452 cto->ct_header.rqs_entry_count = 1;
453 cto->ct_iid = aep->at_iid;
454 if (isp->isp_maxluns <= 16) {
455 cto->ct_lun = aep->at_lun;
456 }
457 cto->ct_rxid = aep->at_rxid;
458 cto->rsp.m1.ct_scsi_status = sts & 0xff;
459 cto->ct_flags = CT2_SENDSTATUS | CT2_NO_DATA | CT2_FLAG_MODE1;
460 if (hdl == 0) {
461 cto->ct_flags |= CT2_CCINCR;
462 }
463 if (aep->at_datalen) {
464 cto->ct_resid = aep->at_datalen;
465 cto->ct_flags |= CT2_DATA_UNDER;
466 }
467 if ((sts & 0xff) == SCSI_CHECK && (sts & ECMD_SVALID)) {
468 cto->rsp.m1.ct_resp[0] = 0xf0;
469 cto->rsp.m1.ct_resp[2] = (code >> 12) & 0xf;
470 cto->rsp.m1.ct_resp[7] = 8;
471 cto->rsp.m1.ct_resp[12] = (code >> 24) & 0xff;
472 cto->rsp.m1.ct_resp[13] = (code >> 16) & 0xff;
473 cto->rsp.m1.ct_senselen = 16;
474 cto->ct_flags |= CT2_SNSLEN_VALID;
475 }
476 cto->ct_syshandle = hdl;
477 } else {
478 at_entry_t *aep = arg;
479 ct_entry_t *cto = &un._ctio;
480
481 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO;
482 cto->ct_header.rqs_entry_count = 1;
483 cto->ct_fwhandle = aep->at_handle;
484 cto->ct_iid = aep->at_iid;
485 cto->ct_tgt = aep->at_tgt;
486 cto->ct_lun = aep->at_lun;
487 cto->ct_tag_type = aep->at_tag_type;
488 cto->ct_tag_val = aep->at_tag_val;
489 cto->ct_flags = CT_SENDSTATUS | CT_NO_DATA;
490 if (hdl == 0) {
491 cto->ct_flags |= CT_CCINCR;
492 }
493 cto->ct_scsi_status = sts;
494 cto->ct_syshandle = hdl;
495 }
496 return (isp_target_put_entry(isp, &un));
497 }
498
499 void
500 isp_target_async(struct ispsoftc *isp, int bus, int event)
501 {
502 tmd_event_t evt;
503 tmd_msg_t msg;
504
505 switch (event) {
506 /*
507 * These three we handle here to propagate an effective bus reset
508 * upstream, but these do not require any immediate notify actions
509 * so we return when done.
510 */
511 case ASYNC_LIP_OCCURRED:
512 case ASYNC_LOOP_UP:
513 case ASYNC_LOOP_DOWN:
514 evt.ev_bus = bus;
515 evt.ev_event = event;
516 (void) isp_async(isp, ISPASYNC_TARGET_EVENT, &evt);
517 return;
518
519 case ASYNC_LOOP_RESET:
520 case ASYNC_BUS_RESET:
521 case ASYNC_TIMEOUT_RESET:
522 if (IS_FC(isp)) {
523 return; /* we'll be getting an inotify instead */
524 }
525 evt.ev_bus = bus;
526 evt.ev_event = event;
527 (void) isp_async(isp, ISPASYNC_TARGET_EVENT, &evt);
528 break;
529 case ASYNC_DEVICE_RESET:
530 /*
531 * Bus Device Reset resets a specific target, so
532 * we pass this as a synthesized message.
533 */
534 MEMZERO(&msg, sizeof msg);
535 if (IS_FC(isp)) {
536 msg.nt_iid = FCPARAM(isp)->isp_loopid;
537 } else {
538 msg.nt_iid = SDPARAM(isp)->isp_initiator_id;
539 }
540 msg.nt_bus = bus;
541 msg.nt_msg[0] = MSG_BUS_DEV_RESET;
542 (void) isp_async(isp, ISPASYNC_TARGET_MESSAGE, &msg);
543 break;
544 default:
545 isp_prt(isp, ISP_LOGERR,
546 "isp_target_async: unknown event 0x%x", event);
547 break;
548 }
549 if (isp->isp_state == ISP_RUNSTATE)
550 isp_notify_ack(isp, NULL);
551 }
552
553
554 /*
555 * Process a received message.
556 * The ISP firmware can handle most messages, there are only
557 * a few that we need to deal with:
558 * - abort: clean up the current command
559 * - abort tag and clear queue
560 */
561
562 static void
563 isp_got_msg(struct ispsoftc *isp, int bus, in_entry_t *inp)
564 {
565 u_int8_t status = inp->in_status & ~QLTM_SVALID;
566
567 if (status == IN_IDE_RECEIVED || status == IN_MSG_RECEIVED) {
568 tmd_msg_t msg;
569
570 MEMZERO(&msg, sizeof (msg));
571 msg.nt_bus = bus;
572 msg.nt_iid = inp->in_iid;
573 msg.nt_tgt = inp->in_tgt;
574 msg.nt_lun = inp->in_lun;
575 msg.nt_tagtype = inp->in_tag_type;
576 msg.nt_tagval = inp->in_tag_val;
577 MEMCPY(msg.nt_msg, inp->in_msg, IN_MSGLEN);
578 (void) isp_async(isp, ISPASYNC_TARGET_MESSAGE, &msg);
579 } else {
580 isp_prt(isp, ISP_LOGERR,
581 "unknown immediate notify status 0x%x", inp->in_status);
582 }
583 }
584
585 /*
586 * Synthesize a message from the task management flags in a FCP_CMND_IU.
587 */
588 static void
589 isp_got_msg_fc(struct ispsoftc *isp, int bus, in_fcentry_t *inp)
590 {
591 static char *f1 = "%s from iid %d lun %d seq 0x%x";
592 static char *f2 =
593 "unknown %s 0x%x lun %d iid %d task flags 0x%x seq 0x%x\n";
594
595 if (inp->in_status != IN_MSG_RECEIVED) {
596 isp_prt(isp, ISP_LOGINFO, f2, "immediate notify status",
597 inp->in_status, inp->in_lun, inp->in_iid,
598 inp->in_task_flags, inp->in_seqid);
599 } else {
600 tmd_msg_t msg;
601
602 MEMZERO(&msg, sizeof (msg));
603 msg.nt_bus = bus;
604 msg.nt_iid = inp->in_iid;
605 if (isp->isp_maxluns > 16) {
606 msg.nt_lun = inp->in_scclun;
607 } else {
608 msg.nt_lun = inp->in_lun;
609 }
610 msg.nt_tagval = inp->in_seqid;
611
612 if (inp->in_task_flags & TASK_FLAGS_ABORT_TASK) {
613 isp_prt(isp, ISP_LOGINFO, f1, "ABORT TASK",
614 inp->in_iid, inp->in_lun, inp->in_seqid);
615 msg.nt_msg[0] = MSG_ABORT_TAG;
616 } else if (inp->in_task_flags & TASK_FLAGS_CLEAR_TASK_SET) {
617 isp_prt(isp, ISP_LOGINFO, f1, "CLEAR TASK SET",
618 inp->in_iid, inp->in_lun, inp->in_seqid);
619 msg.nt_msg[0] = MSG_CLEAR_QUEUE;
620 } else if (inp->in_task_flags & TASK_FLAGS_TARGET_RESET) {
621 isp_prt(isp, ISP_LOGINFO, f1, "TARGET RESET",
622 inp->in_iid, inp->in_lun, inp->in_seqid);
623 msg.nt_msg[0] = MSG_BUS_DEV_RESET;
624 } else if (inp->in_task_flags & TASK_FLAGS_CLEAR_ACA) {
625 isp_prt(isp, ISP_LOGINFO, f1, "CLEAR ACA",
626 inp->in_iid, inp->in_lun, inp->in_seqid);
627 /* ???? */
628 msg.nt_msg[0] = MSG_REL_RECOVERY;
629 } else if (inp->in_task_flags & TASK_FLAGS_TERMINATE_TASK) {
630 isp_prt(isp, ISP_LOGINFO, f1, "TERMINATE TASK",
631 inp->in_iid, inp->in_lun, inp->in_seqid);
632 msg.nt_msg[0] = MSG_TERM_IO_PROC;
633 } else {
634 isp_prt(isp, ISP_LOGWARN, f2, "task flag",
635 inp->in_status, inp->in_lun, inp->in_iid,
636 inp->in_task_flags, inp->in_seqid);
637 }
638 if (msg.nt_msg[0]) {
639 (void) isp_async(isp, ISPASYNC_TARGET_MESSAGE, &msg);
640 }
641 }
642 }
643
644 static void
645 isp_notify_ack(struct ispsoftc *isp, void *arg)
646 {
647 char storage[QENTRY_LEN];
648 u_int16_t iptr, optr;
649 void *outp;
650
651 if (isp_getrqentry(isp, &iptr, &optr, &outp)) {
652 isp_prt(isp, ISP_LOGWARN,
653 "Request Queue Overflow For isp_notify_ack");
654 return;
655 }
656
657 MEMZERO(storage, QENTRY_LEN);
658
659 if (IS_FC(isp)) {
660 na_fcentry_t *na = (na_fcentry_t *) storage;
661 if (arg) {
662 in_fcentry_t *inp = arg;
663 MEMCPY(storage, arg, sizeof (isphdr_t));
664 na->na_iid = inp->in_iid;
665 if (isp->isp_maxluns > 16) {
666 na->na_lun = inp->in_scclun;
667 } else {
668 na->na_lun = inp->in_lun;
669 }
670 na->na_task_flags = inp->in_task_flags;
671 na->na_seqid = inp->in_seqid;
672 na->na_flags = NAFC_RCOUNT;
673 if (inp->in_status == IN_RESET) {
674 na->na_flags |= NAFC_RST_CLRD;
675 }
676 } else {
677 na->na_flags = NAFC_RST_CLRD;
678 }
679 na->na_header.rqs_entry_type = RQSTYPE_NOTIFY_ACK;
680 na->na_header.rqs_entry_count = 1;
681 ISP_SWIZ_NOT_ACK_FC(isp, outp, na);
682 } else {
683 na_entry_t *na = (na_entry_t *) storage;
684 if (arg) {
685 in_entry_t *inp = arg;
686 MEMCPY(storage, arg, sizeof (isphdr_t));
687 na->na_iid = inp->in_iid;
688 na->na_lun = inp->in_lun;
689 na->na_tgt = inp->in_tgt;
690 na->na_seqid = inp->in_seqid;
691 if (inp->in_status == IN_RESET) {
692 na->na_event = NA_RST_CLRD;
693 }
694 } else {
695 na->na_event = NA_RST_CLRD;
696 }
697 na->na_header.rqs_entry_type = RQSTYPE_NOTIFY_ACK;
698 na->na_header.rqs_entry_count = 1;
699 ISP_SWIZ_NOT_ACK(isp, outp, na);
700 }
701 ISP_TDQE(isp, "isp_notify_ack", (int) optr, storage);
702 ISP_ADD_REQUEST(isp, iptr);
703 }
704
705 static void
706 isp_handle_atio(struct ispsoftc *isp, at_entry_t *aep)
707 {
708 int lun;
709 lun = aep->at_lun;
710 /*
711 * The firmware status (except for the QLTM_SVALID bit) indicates
712 * why this ATIO was sent to us.
713 *
714 * If QLTM_SVALID is set, the firware has recommended Sense Data.
715 *
716 * If the DISCONNECTS DISABLED bit is set in the flags field,
717 * we're still connected on the SCSI bus - i.e. the initiator
718 * did not set DiscPriv in the identify message. We don't care
719 * about this so it's ignored.
720 */
721
722 switch(aep->at_status & ~QLTM_SVALID) {
723 case AT_PATH_INVALID:
724 /*
725 * ATIO rejected by the firmware due to disabled lun.
726 */
727 isp_prt(isp, ISP_LOGERR,
728 "rejected ATIO for disabled lun %d", lun);
729 break;
730 case AT_NOCAP:
731 /*
732 * Requested Capability not available
733 * We sent an ATIO that overflowed the firmware's
734 * command resource count.
735 */
736 isp_prt(isp, ISP_LOGERR,
737 "rejected ATIO for lun %d because of command count"
738 " overflow", lun);
739 break;
740
741 case AT_BDR_MSG:
742 /*
743 * If we send an ATIO to the firmware to increment
744 * its command resource count, and the firmware is
745 * recovering from a Bus Device Reset, it returns
746 * the ATIO with this status. We set the command
747 * resource count in the Enable Lun entry and no
748 * not increment it. Therefore we should never get
749 * this status here.
750 */
751 isp_prt(isp, ISP_LOGERR, atiocope, lun);
752 break;
753
754 case AT_CDB: /* Got a CDB */
755 case AT_PHASE_ERROR: /* Bus Phase Sequence Error */
756 /*
757 * Punt to platform specific layer.
758 */
759 (void) isp_async(isp, ISPASYNC_TARGET_ACTION, aep);
760 break;
761
762 case AT_RESET:
763 /*
764 * A bus reset came along an blew away this command. Why
765 * they do this in addition the async event code stuff,
766 * I dunno.
767 *
768 * Ignore it because the async event will clear things
769 * up for us.
770 */
771 isp_prt(isp, ISP_LOGWARN, atior, lun, aep->at_iid);
772 break;
773
774
775 default:
776 isp_prt(isp, ISP_LOGERR,
777 "Unknown ATIO status 0x%x from initiator %d for lun %d",
778 aep->at_status, aep->at_iid, lun);
779 (void) isp_target_put_atio(isp, aep->at_iid, aep->at_tgt,
780 lun, aep->at_tag_type, aep->at_tag_val);
781 break;
782 }
783 }
784
785 static void
786 isp_handle_atio2(struct ispsoftc *isp, at2_entry_t *aep)
787 {
788 int lun;
789
790 if (isp->isp_maxluns > 16) {
791 lun = aep->at_scclun;
792 } else {
793 lun = aep->at_lun;
794 }
795
796 /*
797 * The firmware status (except for the QLTM_SVALID bit) indicates
798 * why this ATIO was sent to us.
799 *
800 * If QLTM_SVALID is set, the firware has recommended Sense Data.
801 *
802 * If the DISCONNECTS DISABLED bit is set in the flags field,
803 * we're still connected on the SCSI bus - i.e. the initiator
804 * did not set DiscPriv in the identify message. We don't care
805 * about this so it's ignored.
806 */
807
808 switch(aep->at_status & ~QLTM_SVALID) {
809 case AT_PATH_INVALID:
810 /*
811 * ATIO rejected by the firmware due to disabled lun.
812 */
813 isp_prt(isp, ISP_LOGERR,
814 "rejected ATIO2 for disabled lun %d", lun);
815 break;
816 case AT_NOCAP:
817 /*
818 * Requested Capability not available
819 * We sent an ATIO that overflowed the firmware's
820 * command resource count.
821 */
822 isp_prt(isp, ISP_LOGERR,
823 "rejected ATIO2 for lun %d- command count overflow", lun);
824 break;
825
826 case AT_BDR_MSG:
827 /*
828 * If we send an ATIO to the firmware to increment
829 * its command resource count, and the firmware is
830 * recovering from a Bus Device Reset, it returns
831 * the ATIO with this status. We set the command
832 * resource count in the Enable Lun entry and no
833 * not increment it. Therefore we should never get
834 * this status here.
835 */
836 isp_prt(isp, ISP_LOGERR, atiocope, lun);
837 break;
838
839 case AT_CDB: /* Got a CDB */
840 /*
841 * Punt to platform specific layer.
842 */
843 (void) isp_async(isp, ISPASYNC_TARGET_ACTION, aep);
844 break;
845
846 case AT_RESET:
847 /*
848 * A bus reset came along an blew away this command. Why
849 * they do this in addition the async event code stuff,
850 * I dunno.
851 *
852 * Ignore it because the async event will clear things
853 * up for us.
854 */
855 isp_prt(isp, ISP_LOGERR, atior, lun, aep->at_iid);
856 break;
857
858
859 default:
860 isp_prt(isp, ISP_LOGERR,
861 "Unknown ATIO2 status 0x%x from initiator %d for lun %d",
862 aep->at_status, aep->at_iid, lun);
863 (void) isp_target_put_atio(isp, aep->at_iid, 0, lun, 0, 0);
864 break;
865 }
866 }
867
868 static void
869 isp_handle_ctio(struct ispsoftc *isp, ct_entry_t *ct)
870 {
871 void *xs;
872 int pl = ISP_LOGTDEBUG2;
873 char *fmsg = NULL;
874
875 if (ct->ct_syshandle) {
876 xs = isp_find_xs(isp, ct->ct_syshandle);
877 if (xs == NULL)
878 pl = ISP_LOGALL;
879 } else {
880 pl = ISP_LOGTDEBUG1;
881 xs = NULL;
882 }
883
884 switch(ct->ct_status & ~QLTM_SVALID) {
885 case CT_OK:
886 /*
887 * There are generally 3 possibilities as to why we'd get
888 * this condition:
889 * We disconnected after receiving a CDB.
890 * We sent or received data.
891 * We sent status & command complete.
892 */
893
894 if (ct->ct_flags & CT_SENDSTATUS) {
895 break;
896 } else if ((ct->ct_flags & CT_DATAMASK) == CT_NO_DATA) {
897 /*
898 * Nothing to do in this case.
899 */
900 isp_prt(isp, pl, "CTIO- iid %d disconnected OK",
901 ct->ct_iid);
902 return;
903 }
904 break;
905
906 case CT_BDR_MSG:
907 /*
908 * Bus Device Reset message received or the SCSI Bus has
909 * been Reset; the firmware has gone to Bus Free.
910 *
911 * The firmware generates an async mailbox interupt to
912 * notify us of this and returns outstanding CTIOs with this
913 * status. These CTIOs are handled in that same way as
914 * CT_ABORTED ones, so just fall through here.
915 */
916 fmsg = "Bus Device Reset";
917 /*FALLTHROUGH*/
918 case CT_RESET:
919 if (fmsg == NULL)
920 fmsg = "Bus Reset";
921 /*FALLTHROUGH*/
922 case CT_ABORTED:
923 /*
924 * When an Abort message is received the firmware goes to
925 * Bus Free and returns all outstanding CTIOs with the status
926 * set, then sends us an Immediate Notify entry.
927 */
928 if (fmsg == NULL)
929 fmsg = "ABORT TASK sent by Initiator";
930
931 isp_prt(isp, ISP_LOGWARN, "CTIO destroyed by %s", fmsg);
932 break;
933
934 case CT_INVAL:
935 /*
936 * CTIO rejected by the firmware due to disabled lun.
937 * "Cannot Happen".
938 */
939 isp_prt(isp, ISP_LOGERR,
940 "Firmware rejected CTIO for disabled lun %d",
941 ct->ct_lun);
942 break;
943
944 case CT_NOPATH:
945 /*
946 * CTIO rejected by the firmware due "no path for the
947 * nondisconnecting nexus specified". This means that
948 * we tried to access the bus while a non-disconnecting
949 * command is in process.
950 */
951 isp_prt(isp, ISP_LOGERR,
952 "Firmware rejected CTIO for bad nexus %d/%d/%d",
953 ct->ct_iid, ct->ct_tgt, ct->ct_lun);
954 break;
955
956 case CT_RSELTMO:
957 fmsg = "Reselection";
958 /*FALLTHROUGH*/
959 case CT_TIMEOUT:
960 if (fmsg == NULL)
961 fmsg = "Command";
962 isp_prt(isp, ISP_LOGERR, "Firmware timed out on %s", fmsg);
963 break;
964
965 case CT_ERR:
966 fmsg = "Completed with Error";
967 /*FALLTHROUGH*/
968 case CT_PHASE_ERROR:
969 if (fmsg == NULL)
970 fmsg = "Phase Sequence Error";
971 /*FALLTHROUGH*/
972 case CT_TERMINATED:
973 if (fmsg == NULL)
974 fmsg = "terminated by TERMINATE TRANSFER";
975 /*FALLTHROUGH*/
976 case CT_NOACK:
977 if (fmsg == NULL)
978 fmsg = "unacknowledged Immediate Notify pending";
979
980 isp_prt(isp, ISP_LOGERR, "CTIO returned by f/w- %s", fmsg);
981 #if 0
982 if (status & SENSEVALID) {
983 bcopy((caddr_t) (cep + CTIO_SENSE_OFFSET),
984 (caddr_t) &cdp->cd_sensedata,
985 sizeof(scsi_sense_t));
986 cdp->cd_flags |= CDF_SENSEVALID;
987 }
988 #endif
989 break;
990 default:
991 isp_prt(isp, ISP_LOGERR, "Unknown CTIO status 0x%x",
992 ct->ct_status & ~QLTM_SVALID);
993 break;
994 }
995
996 if (xs == NULL) {
997 /*
998 * There may be more than one CTIO for a data transfer,
999 * or this may be a status CTIO we're not monitoring.
1000 *
1001 * The assumption is that they'll all be returned in the
1002 * order we got them.
1003 */
1004 if (ct->ct_syshandle == 0) {
1005 if ((ct->ct_flags & CT_SENDSTATUS) == 0) {
1006 isp_prt(isp, pl,
1007 "intermediate CTIO completed ok");
1008 } else {
1009 isp_prt(isp, pl,
1010 "unmonitored CTIO completed ok");
1011 }
1012 } else {
1013 isp_prt(isp, pl,
1014 "NO xs for CTIO (handle 0x%x) status 0x%x",
1015 ct->ct_syshandle, ct->ct_status & ~QLTM_SVALID);
1016 }
1017 } else {
1018 if (ct->ct_flags & CT_SENDSTATUS) {
1019 /*
1020 * Sent status and command complete.
1021 *
1022 * We're now really done with this command, so we
1023 * punt to the platform dependent layers because
1024 * only there can we do the appropriate command
1025 * complete thread synchronization.
1026 */
1027 isp_prt(isp, pl, "status CTIO complete");
1028 } else {
1029 /*
1030 * Final CTIO completed. Release DMA resources and
1031 * notify platform dependent layers.
1032 */
1033 isp_prt(isp, pl, "data CTIO complete");
1034 ISP_DMAFREE(isp, xs, ct->ct_syshandle);
1035 }
1036 (void) isp_async(isp, ISPASYNC_TARGET_ACTION, ct);
1037 /*
1038 * The platform layer will destroy the handle if appropriate.
1039 */
1040 }
1041 }
1042
1043 static void
1044 isp_handle_ctio2(struct ispsoftc *isp, ct2_entry_t *ct)
1045 {
1046 XS_T *xs;
1047 int pl = ISP_LOGTDEBUG2;
1048 char *fmsg = NULL;
1049
1050 if (ct->ct_syshandle) {
1051 xs = isp_find_xs(isp, ct->ct_syshandle);
1052 if (xs == NULL)
1053 pl = ISP_LOGALL;
1054 } else {
1055 pl = ISP_LOGTDEBUG1;
1056 xs = NULL;
1057 }
1058
1059 switch(ct->ct_status & ~QLTM_SVALID) {
1060 case CT_OK:
1061 /*
1062 * There are generally 2 possibilities as to why we'd get
1063 * this condition:
1064 * We sent or received data.
1065 * We sent status & command complete.
1066 */
1067
1068 break;
1069
1070 case CT_BDR_MSG:
1071 /*
1072 * Bus Device Reset message received or the SCSI Bus has
1073 * been Reset; the firmware has gone to Bus Free.
1074 *
1075 * The firmware generates an async mailbox interupt to
1076 * notify us of this and returns outstanding CTIOs with this
1077 * status. These CTIOs are handled in that same way as
1078 * CT_ABORTED ones, so just fall through here.
1079 */
1080 fmsg = "Bus Device Reset";
1081 /*FALLTHROUGH*/
1082 case CT_RESET:
1083 if (fmsg == NULL)
1084 fmsg = "Bus Reset";
1085 /*FALLTHROUGH*/
1086 case CT_ABORTED:
1087 /*
1088 * When an Abort message is received the firmware goes to
1089 * Bus Free and returns all outstanding CTIOs with the status
1090 * set, then sends us an Immediate Notify entry.
1091 */
1092 if (fmsg == NULL)
1093 fmsg = "ABORT TASK sent by Initiator";
1094
1095 isp_prt(isp, ISP_LOGERR, "CTIO2 destroyed by %s", fmsg);
1096 break;
1097
1098 case CT_INVAL:
1099 /*
1100 * CTIO rejected by the firmware - invalid data direction.
1101 */
1102 isp_prt(isp, ISP_LOGERR, "CTIO2 had wrong data directiond");
1103 break;
1104
1105 case CT_NOPATH:
1106 /*
1107 * CTIO rejected by the firmware due "no path for the
1108 * nondisconnecting nexus specified". This means that
1109 * we tried to access the bus while a non-disconnecting
1110 * command is in process.
1111 */
1112 isp_prt(isp, ISP_LOGERR,
1113 "Firmware rejected CTIO2 for bad nexus %d->%d",
1114 ct->ct_iid, ct->ct_lun);
1115 break;
1116
1117 case CT_RSELTMO:
1118 fmsg = "Reselection";
1119 /*FALLTHROUGH*/
1120 case CT_TIMEOUT:
1121 if (fmsg == NULL)
1122 fmsg = "Command";
1123 isp_prt(isp, ISP_LOGERR, "Firmware timed out on %s", fmsg);
1124 break;
1125
1126 case CT_ERR:
1127 fmsg = "Completed with Error";
1128 /*FALLTHROUGH*/
1129 case CT_PHASE_ERROR: /* Bus phase sequence error */
1130 if (fmsg == NULL)
1131 fmsg = "Phase Sequence Error";
1132 /*FALLTHROUGH*/
1133 case CT_TERMINATED:
1134 if (fmsg == NULL)
1135 fmsg = "terminated by TERMINATE TRANSFER";
1136 /*FALLTHROUGH*/
1137 case CT_LOGOUT:
1138 if (fmsg == NULL)
1139 fmsg = "Port Logout";
1140 /*FALLTHROUGH*/
1141 case CT_PORTNOTAVAIL:
1142 if (fmsg == NULL)
1143 fmsg = "Port not available";
1144 case CT_NOACK:
1145 if (fmsg == NULL)
1146 fmsg = "unacknowledged Immediate Notify pending";
1147
1148 isp_prt(isp, ISP_LOGERR, "CTIO returned by f/w- %s", fmsg);
1149 #if 0
1150 if (status & SENSEVALID) {
1151 bcopy((caddr_t) (cep + CTIO_SENSE_OFFSET),
1152 (caddr_t) &cdp->cd_sensedata,
1153 sizeof(scsi_sense_t));
1154 cdp->cd_flags |= CDF_SENSEVALID;
1155 }
1156 #endif
1157 break;
1158
1159 case CT_INVRXID:
1160 /*
1161 * CTIO rejected by the firmware because an invalid RX_ID.
1162 * Just print a message.
1163 */
1164 isp_prt(isp, ISP_LOGERR,
1165 "CTIO2 completed with Invalid RX_ID 0x%x", ct->ct_rxid);
1166 break;
1167
1168 default:
1169 isp_prt(isp, ISP_LOGERR, "Unknown CTIO status 0x%x",
1170 ct->ct_status & ~QLTM_SVALID);
1171 break;
1172 }
1173
1174 if (xs == NULL) {
1175 /*
1176 * There may be more than one CTIO for a data transfer,
1177 * or this may be a status CTIO we're not monitoring.
1178 *
1179 * The assumption is that they'll all be returned in the
1180 * order we got them.
1181 */
1182 if (ct->ct_syshandle == 0) {
1183 if ((ct->ct_flags & CT_SENDSTATUS) == 0) {
1184 isp_prt(isp, pl,
1185 "intermediate CTIO completed ok");
1186 } else {
1187 isp_prt(isp, pl,
1188 "unmonitored CTIO completed ok");
1189 }
1190 } else {
1191 isp_prt(isp, pl,
1192 "NO xs for CTIO (handle 0x%x) status 0x%x",
1193 ct->ct_syshandle, ct->ct_status & ~QLTM_SVALID);
1194 }
1195 } else {
1196 if (ct->ct_flags & CT_SENDSTATUS) {
1197 /*
1198 * Sent status and command complete.
1199 *
1200 * We're now really done with this command, so we
1201 * punt to the platform dependent layers because
1202 * only there can we do the appropriate command
1203 * complete thread synchronization.
1204 */
1205 isp_prt(isp, pl, "status CTIO complete");
1206 } else {
1207 /*
1208 * Final CTIO completed. Release DMA resources and
1209 * notify platform dependent layers.
1210 */
1211 isp_prt(isp, pl, "data CTIO complete");
1212 ISP_DMAFREE(isp, xs, ct->ct_syshandle);
1213 }
1214 (void) isp_async(isp, ISPASYNC_TARGET_ACTION, ct);
1215 /*
1216 * The platform layer will destroy the handle if appropriate.
1217 */
1218 }
1219 }
1220 #endif
1221