nrelease - fix/improve livecd
[dragonfly.git] / sys / dev / disk / isp / isp_target.c
1 /*-
2  *  Copyright (c) 1997-2009 by Matthew Jacob
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions
7  *  are met:
8  *
9  *  1. Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  *  2. Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  *
15  *  THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  *  ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  *  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  *  SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/dev/isp/isp_target.c,v 1.47 2010/02/27 05:41:23 mjacob Exp $
28  */
29 /*
30  * Machine and OS Independent Target Mode Code for the Qlogic SCSI/FC adapters.
31  */
32 /*
33  * Bug fixes gratefully acknowledged from:
34  *      Oded Kedem <oded@kashya.com>
35  */
36 /*
37  * Include header file appropriate for platform we're building on.
38  */
39
40 #include <dev/disk/isp/isp_freebsd.h>
41
42 #ifdef  ISP_TARGET_MODE
43 static const char atiocope[] = "ATIO returned for lun %d because it was in the middle of Bus Device Reset on bus %d";
44 static const char atior[] = "ATIO returned on for lun %d on from loopid %d because a Bus Reset occurred on bus %d";
45 static const char rqo[] = "%s: Request Queue Overflow";
46
47 static void isp_got_msg(ispsoftc_t *, in_entry_t *);
48 static void isp_got_msg_fc(ispsoftc_t *, in_fcentry_t *);
49 static void isp_got_tmf_24xx(ispsoftc_t *, at7_entry_t *);
50 static void isp_handle_atio(ispsoftc_t *, at_entry_t *);
51 static void isp_handle_atio2(ispsoftc_t *, at2_entry_t *);
52 static void isp_handle_ctio(ispsoftc_t *, ct_entry_t *);
53 static void isp_handle_ctio2(ispsoftc_t *, ct2_entry_t *);
54 static void isp_handle_ctio7(ispsoftc_t *, ct7_entry_t *);
55 static void isp_handle_24xx_inotify(ispsoftc_t *, in_fcentry_24xx_t *);
56
57 /*
58  * The Qlogic driver gets an interrupt to look at response queue entries.
59  * Some of these are status completions for initiatior mode commands, but
60  * if target mode is enabled, we get a whole wad of response queue entries
61  * to be handled here.
62  *
63  * Basically the split into 3 main groups: Lun Enable/Modification responses,
64  * SCSI Command processing, and Immediate Notification events.
65  *
66  * You start by writing a request queue entry to enable target mode (and
67  * establish some resource limitations which you can modify later).
68  * The f/w responds with a LUN ENABLE or LUN MODIFY response with
69  * the status of this action. If the enable was successful, you can expect...
70  *
71  * Response queue entries with SCSI commands encapsulate show up in an ATIO
72  * (Accept Target IO) type- sometimes with enough info to stop the command at
73  * this level. Ultimately the driver has to feed back to the f/w's request
74  * queue a sequence of CTIOs (continue target I/O) that describe data to
75  * be moved and/or status to be sent) and finally finishing with sending
76  * to the f/w's response queue an ATIO which then completes the handshake
77  * with the f/w for that command. There's a lot of variations on this theme,
78  * including flags you can set in the CTIO for the Qlogic 2X00 fibre channel
79  * cards that 'auto-replenish' the f/w's ATIO count, but this is the basic
80  * gist of it.
81  *
82  * The third group that can show up in the response queue are Immediate
83  * Notification events. These include things like notifications of SCSI bus
84  * resets, or Bus Device Reset messages or other messages received. This
85  * a classic oddbins area. It can get  a little weird because you then turn
86  * around and acknowledge the Immediate Notify by writing an entry onto the
87  * request queue and then the f/w turns around and gives you an acknowledgement
88  * to *your* acknowledgement on the response queue (the idea being to let
89  * the f/w tell you when the event is *really* over I guess).
90  *
91  */
92
93
94 /*
95  * A new response queue entry has arrived. The interrupt service code
96  * has already swizzled it into the platform dependent from canonical form.
97  *
98  * Because of the way this driver is designed, unfortunately most of the
99  * actual synchronization work has to be done in the platform specific
100  * code- we have no synchroniation primitives in the common code.
101  */
102
103 int
104 isp_target_notify(ispsoftc_t *isp, void *vptr, uint32_t *optrp)
105 {
106         uint16_t status;
107         uint32_t seqid;
108         union {
109                 at_entry_t      *atiop;
110                 at2_entry_t     *at2iop;
111                 at2e_entry_t    *at2eiop;
112                 at7_entry_t     *at7iop;
113                 ct_entry_t      *ctiop;
114                 ct2_entry_t     *ct2iop;
115                 ct2e_entry_t    *ct2eiop;
116                 ct7_entry_t     *ct7iop;
117                 lun_entry_t     *lunenp;
118                 in_entry_t      *inotp;
119                 in_fcentry_t    *inot_fcp;
120                 in_fcentry_e_t  *inote_fcp;
121                 in_fcentry_24xx_t *inot_24xx;
122                 na_entry_t      *nackp;
123                 na_fcentry_t    *nack_fcp;
124                 na_fcentry_e_t  *nacke_fcp;
125                 na_fcentry_24xx_t *nack_24xx;
126                 isphdr_t        *hp;
127                 abts_t          *abts;
128                 abts_rsp_t      *abts_rsp;
129                 els_t           *els;
130                 void *          *vp;
131 #define atiop           unp.atiop
132 #define at2iop          unp.at2iop
133 #define at2eiop         unp.at2eiop
134 #define at7iop          unp.at7iop
135 #define ctiop           unp.ctiop
136 #define ct2iop          unp.ct2iop
137 #define ct2eiop         unp.ct2eiop
138 #define ct7iop          unp.ct7iop
139 #define lunenp          unp.lunenp
140 #define inotp           unp.inotp
141 #define inot_fcp        unp.inot_fcp
142 #define inote_fcp       unp.inote_fcp
143 #define inot_24xx       unp.inot_24xx
144 #define nackp           unp.nackp
145 #define nack_fcp        unp.nack_fcp
146 #define nacke_fcp       unp.nacke_fcp
147 #define nack_24xx       unp.nack_24xx
148 #define abts            unp.abts
149 #define abts_rsp        unp.abts_rsp
150 #define els             unp.els
151 #define hdrp            unp.hp
152         } unp;
153         uint8_t local[QENTRY_LEN];
154         uint16_t iid;
155         int bus, type, level, rval = 1;
156         isp_notify_t notify;
157
158         type = isp_get_response_type(isp, (isphdr_t *)vptr);
159         unp.vp = vptr;
160
161         ISP_TDQE(isp, "isp_target_notify", (int) *optrp, vptr);
162
163         switch (type) {
164         case RQSTYPE_ATIO:
165                 if (IS_24XX(isp)) {
166                         int len;
167
168                         isp_get_atio7(isp, at7iop, (at7_entry_t *) local);
169                         at7iop = (at7_entry_t *) local;
170                         /*
171                          * Check for and do something with commands whose
172                          * IULEN extends past a single queue entry.
173                          */
174                         len = at7iop->at_ta_len & 0xfffff;
175                         if (len > (QENTRY_LEN - 8)) {
176                                 len -= (QENTRY_LEN - 8);
177                                 isp_prt(isp, ISP_LOGINFO, "long IU length (%d) ignored", len);
178                                 while (len > 0) {
179                                         *optrp =  ISP_NXT_QENTRY(*optrp, RESULT_QUEUE_LEN(isp));
180                                         len -= QENTRY_LEN;
181                                 }
182                         }
183                         /*
184                          * Check for a task management function
185                          */
186                         if (at7iop->at_cmnd.fcp_cmnd_task_management) {
187                                 isp_got_tmf_24xx(isp, at7iop);
188                                 break;
189                         }
190                         /*
191                          * Just go straight to outer layer for this one.
192                          */
193                         isp_async(isp, ISPASYNC_TARGET_ACTION, local);
194                 } else {
195                         isp_get_atio(isp, atiop, (at_entry_t *) local);
196                         isp_handle_atio(isp, (at_entry_t *) local);
197                 }
198                 break;
199
200         case RQSTYPE_CTIO:
201                 isp_get_ctio(isp, ctiop, (ct_entry_t *) local);
202                 isp_handle_ctio(isp, (ct_entry_t *) local);
203                 break;
204
205         case RQSTYPE_ATIO2:
206                 if (ISP_CAP_2KLOGIN(isp)) {
207                         isp_get_atio2e(isp, at2eiop, (at2e_entry_t *) local);
208                 } else {
209                         isp_get_atio2(isp, at2iop, (at2_entry_t *) local);
210                 }
211                 isp_handle_atio2(isp, (at2_entry_t *) local);
212                 break;
213
214         case RQSTYPE_CTIO3:
215         case RQSTYPE_CTIO2:
216                 if (ISP_CAP_2KLOGIN(isp)) {
217                         isp_get_ctio2e(isp, ct2eiop, (ct2e_entry_t *) local);
218                 } else {
219                         isp_get_ctio2(isp, ct2iop, (ct2_entry_t *) local);
220                 }
221                 isp_handle_ctio2(isp, (ct2_entry_t *) local);
222                 break;
223
224         case RQSTYPE_CTIO7:
225                 isp_get_ctio7(isp, ct7iop, (ct7_entry_t *) local);
226                 isp_handle_ctio7(isp, (ct7_entry_t *) local);
227                 break;
228
229         case RQSTYPE_ENABLE_LUN:
230         case RQSTYPE_MODIFY_LUN:
231                 isp_get_enable_lun(isp, lunenp, (lun_entry_t *) local);
232                 isp_async(isp, ISPASYNC_TARGET_ACTION, local);
233                 break;
234
235         case RQSTYPE_NOTIFY:
236                 bus = 0;
237                 if (IS_24XX(isp)) {
238                         isp_get_notify_24xx(isp, inot_24xx, (in_fcentry_24xx_t *)local);
239                         inot_24xx = (in_fcentry_24xx_t *) local;
240                         isp_handle_24xx_inotify(isp, inot_24xx);
241                         break;
242                 }
243                 if (IS_FC(isp)) {
244                         if (ISP_CAP_2KLOGIN(isp)) {
245                                 in_fcentry_e_t *ecp = (in_fcentry_e_t *)local;
246                                 isp_get_notify_fc_e(isp, inote_fcp, ecp);
247                                 iid = ecp->in_iid;
248                                 status = ecp->in_status;
249                                 seqid = ecp->in_seqid;
250                         } else {
251                                 in_fcentry_t *fcp = (in_fcentry_t *)local;
252                                 isp_get_notify_fc(isp, inot_fcp, fcp);
253                                 iid = fcp->in_iid;
254                                 status = fcp->in_status;
255                                 seqid = fcp->in_seqid;
256                         }
257                 } else {
258                         in_entry_t *inp = (in_entry_t *)local;
259                         isp_get_notify(isp, inotp, inp);
260                         status = inp->in_status & 0xff;
261                         seqid = inp->in_seqid;
262                         iid = inp->in_iid;
263                         if (IS_DUALBUS(isp)) {
264                                 bus = GET_BUS_VAL(inp->in_iid);
265                                 SET_BUS_VAL(inp->in_iid, 0);
266                         }
267                 }
268
269                 isp_prt(isp, ISP_LOGTDEBUG0, "Immediate Notify On Bus %d, status=0x%x seqid=0x%x", bus, status, seqid);
270
271                 switch (status) {
272                 case IN_MSG_RECEIVED:
273                 case IN_IDE_RECEIVED:
274                         if (IS_FC(isp)) {
275                                 isp_got_msg_fc(isp, (in_fcentry_t *)local);
276                         } else {
277                                 isp_got_msg(isp, (in_entry_t *)local);
278                         }
279                         break;
280                 case IN_RSRC_UNAVAIL:
281                         isp_prt(isp, ISP_LOGINFO, "Firmware out of ATIOs");
282                         (void) isp_notify_ack(isp, local);
283                         break;
284
285                 case IN_RESET:
286                         ISP_MEMZERO(&notify, sizeof (isp_notify_t));
287                         notify.nt_hba = isp;
288                         notify.nt_wwn = INI_ANY;
289                         notify.nt_tgt = TGT_ANY;
290                         notify.nt_nphdl = iid;
291                         notify.nt_sid = PORT_ANY;
292                         notify.nt_did = PORT_ANY;
293                         notify.nt_lun = LUN_ANY;
294                         notify.nt_tagval = TAG_ANY;
295                         notify.nt_tagval |= (((uint64_t)(isp->isp_serno++)) << 32);
296                         notify.nt_ncode = NT_BUS_RESET;
297                         notify.nt_need_ack = 1;
298                         notify.nt_lreserved = local;
299                         isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
300                         break;
301
302                 case IN_PORT_LOGOUT:
303                         ISP_MEMZERO(&notify, sizeof (isp_notify_t));
304                         notify.nt_hba = isp;
305                         notify.nt_wwn = INI_ANY;
306                         notify.nt_nphdl = iid;
307                         notify.nt_sid = PORT_ANY;
308                         notify.nt_did = PORT_ANY;
309                         notify.nt_ncode = NT_LOGOUT;
310                         notify.nt_need_ack = 1;
311                         notify.nt_lreserved = local;
312                         isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
313                         break;
314
315                 case IN_ABORT_TASK:
316                         ISP_MEMZERO(&notify, sizeof (isp_notify_t));
317                         notify.nt_hba = isp;
318                         notify.nt_wwn = INI_ANY;
319                         notify.nt_nphdl = iid;
320                         notify.nt_sid = PORT_ANY;
321                         notify.nt_did = PORT_ANY;
322                         notify.nt_ncode = NT_ABORT_TASK;
323                         notify.nt_need_ack = 1;
324                         notify.nt_lreserved = local;
325                         isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
326                         break;
327
328                 case IN_GLOBAL_LOGO:
329                         isp_prt(isp, ISP_LOGTINFO, "%s: all ports logged out", __func__);
330                         ISP_MEMZERO(&notify, sizeof (isp_notify_t));
331                         notify.nt_hba = isp;
332                         notify.nt_wwn = INI_ANY;
333                         notify.nt_nphdl = NIL_HANDLE;
334                         notify.nt_sid = PORT_ANY;
335                         notify.nt_did = PORT_ANY;
336                         notify.nt_ncode = NT_GLOBAL_LOGOUT;
337                         isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
338                         (void) isp_notify_ack(isp, local);
339                         break;
340
341                 case IN_PORT_CHANGED:
342                         isp_prt(isp, ISP_LOGTINFO, "%s: port changed", __func__);
343                         (void) isp_notify_ack(isp, local);
344                         break;
345
346                 default:
347                         ISP_SNPRINTF(local, sizeof local, "%s: unknown status to RQSTYPE_NOTIFY (0x%x)", __func__, status);
348                         isp_print_bytes(isp, local, QENTRY_LEN, vptr);
349                         (void) isp_notify_ack(isp, local);
350                         break;
351                 }
352                 break;
353
354         case RQSTYPE_NOTIFY_ACK:
355                 /*
356                  * The ISP is acknowledging our acknowledgement of an
357                  * Immediate Notify entry for some asynchronous event.
358                  */
359                 if (IS_24XX(isp)) {
360                         isp_get_notify_ack_24xx(isp, nack_24xx, (na_fcentry_24xx_t *) local);
361                         nack_24xx = (na_fcentry_24xx_t *) local;
362                         if (nack_24xx->na_status != NA_OK) {
363                                 level = ISP_LOGINFO;
364                         } else {
365                                 level = ISP_LOGTDEBUG1;
366                         }
367                         isp_prt(isp, level, "Notify Ack Status=0x%x; Subcode 0x%x seqid=0x%x", nack_24xx->na_status, nack_24xx->na_status_subcode, nack_24xx->na_rxid);
368                 } else if (IS_FC(isp)) {
369                         if (ISP_CAP_2KLOGIN(isp)) {
370                                 isp_get_notify_ack_fc_e(isp, nacke_fcp, (na_fcentry_e_t *)local);
371                         } else {
372                                 isp_get_notify_ack_fc(isp, nack_fcp, (na_fcentry_t *)local);
373                         }
374                         nack_fcp = (na_fcentry_t *)local;
375                         if (nack_fcp->na_status != NA_OK) {
376                                 level = ISP_LOGINFO;
377                         } else {
378                                 level = ISP_LOGTDEBUG1;
379                         }
380                         isp_prt(isp, level, "Notify Ack Status=0x%x seqid 0x%x", nack_fcp->na_status, nack_fcp->na_seqid);
381                 } else {
382                         isp_get_notify_ack(isp, nackp, (na_entry_t *)local);
383                         nackp = (na_entry_t *)local;
384                         if (nackp->na_status != NA_OK) {
385                                 level = ISP_LOGINFO;
386                         } else {
387                                 level = ISP_LOGTDEBUG1;
388                         }
389                         isp_prt(isp, level, "Notify Ack event 0x%x status=0x%x seqid 0x%x", nackp->na_event, nackp->na_status, nackp->na_seqid);
390                 }
391                 break;
392
393         case RQSTYPE_ABTS_RCVD:
394                 isp_get_abts(isp, abts, (abts_t *)local);
395                 isp_async(isp, ISPASYNC_TARGET_ACTION, &local);
396                 break;
397         case RQSTYPE_ABTS_RSP:
398                 isp_get_abts_rsp(isp, abts_rsp, (abts_rsp_t *)local);
399                 abts_rsp = (abts_rsp_t *) local;
400                 if (abts_rsp->abts_rsp_status) {
401                         level = ISP_LOGINFO;
402                 } else {
403                         level = ISP_LOGTDEBUG0;
404                 }
405                 isp_prt(isp, level, "ABTS RSP response[0x%x]: status=0x%x sub=(0x%x 0x%x)", abts_rsp->abts_rsp_rxid_task, abts_rsp->abts_rsp_status,
406                     abts_rsp->abts_rsp_payload.rsp.subcode1, abts_rsp->abts_rsp_payload.rsp.subcode2);
407                 break;
408         default:
409                 isp_prt(isp, ISP_LOGERR, "%s: unknown entry type 0x%x", __func__, type);
410                 rval = 0;
411                 break;
412         }
413 #undef  atiop
414 #undef  at2iop
415 #undef  at2eiop
416 #undef  at7iop
417 #undef  ctiop
418 #undef  ct2iop
419 #undef  ct2eiop
420 #undef  ct7iop
421 #undef  lunenp
422 #undef  inotp
423 #undef  inot_fcp
424 #undef  inote_fcp
425 #undef  inot_24xx
426 #undef  nackp
427 #undef  nack_fcp
428 #undef  nacke_fcp
429 #undef  hack_24xx
430 #undef  abts
431 #undef  abts_rsp
432 #undef  els
433 #undef  hdrp
434         return (rval);
435 }
436
437
438 /*
439  * Toggle (on/off) target mode for bus/target/lun.
440  *
441  * The caller has checked for overlap and legality.
442  *
443  * Note that not all of bus, target or lun can be paid attention to.
444  * Note also that this action will not be complete until the f/w writes
445  * a response entry. The caller is responsible for synchronizing with this.
446  */
447 int
448 isp_lun_cmd(ispsoftc_t *isp, int cmd, int bus, int lun, int cmd_cnt, int inot_cnt)
449 {
450         lun_entry_t el;
451         void *outp;
452
453         ISP_MEMZERO(&el, sizeof (el));
454         if (IS_DUALBUS(isp)) {
455                 el.le_rsvd = (bus & 0x1) << 7;
456         }
457         el.le_cmd_count = cmd_cnt;
458         el.le_in_count = inot_cnt;
459         if (cmd == RQSTYPE_ENABLE_LUN) {
460                 if (IS_SCSI(isp)) {
461                         el.le_flags = LUN_TQAE|LUN_DISAD;
462                         el.le_cdb6len = 12;
463                         el.le_cdb7len = 12;
464                 }
465         } else if (cmd == -RQSTYPE_ENABLE_LUN) {
466                 cmd = RQSTYPE_ENABLE_LUN;
467                 el.le_cmd_count = 0;
468                 el.le_in_count = 0;
469         } else if (cmd == -RQSTYPE_MODIFY_LUN) {
470                 cmd = RQSTYPE_MODIFY_LUN;
471                 el.le_ops = LUN_CCDECR | LUN_INDECR;
472         } else {
473                 el.le_ops = LUN_CCINCR | LUN_ININCR;
474         }
475         el.le_header.rqs_entry_type = cmd;
476         el.le_header.rqs_entry_count = 1;
477         if (IS_SCSI(isp)) {
478                 el.le_tgt = SDPARAM(isp, bus)->isp_initiator_id;
479                 el.le_lun = lun;
480         } else if (ISP_CAP_SCCFW(isp) == 0) {
481                 el.le_lun = lun;
482         }
483         el.le_timeout = 30;
484
485         outp = isp_getrqentry(isp);
486         if (outp == NULL) {
487                 isp_prt(isp, ISP_LOGERR, rqo, __func__);
488                 return (-1);
489         }
490         isp_put_enable_lun(isp, &el, outp);
491         ISP_TDQE(isp, "isp_lun_cmd", isp->isp_reqidx, &el);
492         ISP_SYNC_REQUEST(isp);
493         return (0);
494 }
495
496 int
497 isp_target_put_entry(ispsoftc_t *isp, void *ap)
498 {
499         void *outp;
500         uint8_t etype = ((isphdr_t *) ap)->rqs_entry_type;
501
502         outp = isp_getrqentry(isp);
503         if (outp == NULL) {
504                 isp_prt(isp, ISP_LOGWARN, rqo, __func__);
505                 return (-1);
506         }
507         switch (etype) {
508         case RQSTYPE_ATIO:
509                 isp_put_atio(isp, (at_entry_t *) ap, (at_entry_t *) outp);
510                 break;
511         case RQSTYPE_ATIO2:
512                 if (ISP_CAP_2KLOGIN(isp)) {
513                         isp_put_atio2e(isp, (at2e_entry_t *) ap, (at2e_entry_t *) outp);
514                 } else {
515                         isp_put_atio2(isp, (at2_entry_t *) ap, (at2_entry_t *) outp);
516                 }
517                 break;
518         case RQSTYPE_CTIO:
519                 isp_put_ctio(isp, (ct_entry_t *) ap, (ct_entry_t *) outp);
520                 break;
521         case RQSTYPE_CTIO2:
522                 if (ISP_CAP_2KLOGIN(isp)) {
523                         isp_put_ctio2e(isp, (ct2e_entry_t *) ap, (ct2e_entry_t *) outp);
524                 } else {
525                         isp_put_ctio2(isp, (ct2_entry_t *) ap, (ct2_entry_t *) outp);
526                 }
527                 break;
528         case RQSTYPE_CTIO7:
529                 isp_put_ctio7(isp, (ct7_entry_t *) ap, (ct7_entry_t *) outp);
530                 break;
531         default:
532                 isp_prt(isp, ISP_LOGERR, "%s: Unknown type 0x%x", __func__, etype);
533                 return (-1);
534         }
535         ISP_TDQE(isp, __func__, isp->isp_reqidx, ap);
536         ISP_SYNC_REQUEST(isp);
537         return (0);
538 }
539
540 int
541 isp_target_put_atio(ispsoftc_t *isp, void *arg)
542 {
543         union {
544                 at_entry_t _atio;
545                 at2_entry_t _atio2;
546                 at2e_entry_t _atio2e;
547         } atun;
548
549         ISP_MEMZERO(&atun, sizeof atun);
550         if (IS_FC(isp)) {
551                 at2_entry_t *aep = arg;
552                 atun._atio2.at_header.rqs_entry_type = RQSTYPE_ATIO2;
553                 atun._atio2.at_header.rqs_entry_count = 1;
554                 if (ISP_CAP_SCCFW(isp)) {
555                         atun._atio2.at_scclun = aep->at_scclun;
556                 } else {
557                         atun._atio2.at_lun = (uint8_t) aep->at_lun;
558                 }
559                 if (ISP_CAP_2KLOGIN(isp)) {
560                         atun._atio2e.at_iid = ((at2e_entry_t *)aep)->at_iid;
561                 } else {
562                         atun._atio2.at_iid = aep->at_iid;
563                 }
564                 atun._atio2.at_rxid = aep->at_rxid;
565                 atun._atio2.at_status = CT_OK;
566         } else {
567                 at_entry_t *aep = arg;
568                 atun._atio.at_header.rqs_entry_type = RQSTYPE_ATIO;
569                 atun._atio.at_header.rqs_entry_count = 1;
570                 atun._atio.at_handle = aep->at_handle;
571                 atun._atio.at_iid = aep->at_iid;
572                 atun._atio.at_tgt = aep->at_tgt;
573                 atun._atio.at_lun = aep->at_lun;
574                 atun._atio.at_tag_type = aep->at_tag_type;
575                 atun._atio.at_tag_val = aep->at_tag_val;
576                 atun._atio.at_status = (aep->at_flags & AT_TQAE);
577                 atun._atio.at_status |= CT_OK;
578         }
579         return (isp_target_put_entry(isp, &atun));
580 }
581
582 /*
583  * Command completion- both for handling cases of no resources or
584  * no blackhole driver, or other cases where we have to, inline,
585  * finish the command sanely, or for normal command completion.
586  *
587  * The 'completion' code value has the scsi status byte in the low 8 bits.
588  * If status is a CHECK CONDITION and bit 8 is nonzero, then bits 12..15 have
589  * the sense key and  bits 16..23 have the ASCQ and bits 24..31 have the ASC
590  * values.
591  *
592  * NB: the key, asc, ascq, cannot be used for parallel SCSI as it doesn't
593  * NB: inline SCSI sense reporting. As such, we lose this information. XXX.
594  *
595  * For both parallel && fibre channel, we use the feature that does
596  * an automatic resource autoreplenish so we don't have then later do
597  * put of an atio to replenish the f/w's resource count.
598  */
599
600 int
601 isp_endcmd(ispsoftc_t *isp, ...)
602 {
603         uint32_t code, hdl;
604         uint8_t sts;
605         union {
606                 ct_entry_t _ctio;
607                 ct2_entry_t _ctio2;
608                 ct2e_entry_t _ctio2e;
609                 ct7_entry_t _ctio7;
610         } un;
611         va_list ap;
612
613         ISP_MEMZERO(&un, sizeof un);
614
615         if (IS_24XX(isp)) {
616                 int vpidx, nphdl;
617                 at7_entry_t *aep;
618                 ct7_entry_t *cto = &un._ctio7;
619
620                 va_start(ap, isp);
621                 aep = va_arg(ap, at7_entry_t *);
622                 nphdl = va_arg(ap, int);
623                 /*
624                  * Note that vpidx may equal 0xff (unknown) here
625                  */
626                 vpidx = va_arg(ap, int);
627                 code = va_arg(ap, uint32_t);
628                 hdl = va_arg(ap, uint32_t);
629                 va_end(ap);
630                 isp_prt(isp, ISP_LOGTDEBUG0, "%s: [RX_ID 0x%x] chan %d code %x", __func__, aep->at_rxid, vpidx, code);
631
632                 sts = code & 0xff;
633                 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
634                 cto->ct_header.rqs_entry_count = 1;
635                 cto->ct_nphdl = nphdl;
636                 cto->ct_rxid = aep->at_rxid;
637                 cto->ct_iid_lo = (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
638                 cto->ct_iid_hi = aep->at_hdr.s_id[0];
639                 cto->ct_oxid = aep->at_hdr.ox_id;
640                 cto->ct_scsi_status = sts;
641                 cto->ct_vpidx = vpidx;
642                 cto->ct_flags = CT7_NOACK;
643                 if (code & ECMD_TERMINATE) {
644                         cto->ct_flags |= CT7_TERMINATE;
645                 } else if (code & ECMD_SVALID) {
646                         cto->ct_flags |= CT7_FLAG_MODE1 | CT7_SENDSTATUS;
647                         cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
648                         cto->rsp.m1.ct_resplen = cto->ct_senselen = min(16, MAXRESPLEN_24XX);
649                         ISP_MEMZERO(cto->rsp.m1.ct_resp, sizeof (cto->rsp.m1.ct_resp));
650                         cto->rsp.m1.ct_resp[0] = 0xf0;
651                         cto->rsp.m1.ct_resp[2] = (code >> 12) & 0xf;
652                         cto->rsp.m1.ct_resp[7] = 8;
653                         cto->rsp.m1.ct_resp[12] = (code >> 24) & 0xff;
654                         cto->rsp.m1.ct_resp[13] = (code >> 16) & 0xff;
655                 } else {
656                         cto->ct_flags |= CT7_FLAG_MODE1 | CT7_SENDSTATUS;
657                 }
658                 if (aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl) {
659                         cto->ct_resid = aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl;
660                         if (cto->ct_resid < 0) {
661                                  cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8);
662                         } else if (cto->ct_resid > 0) {
663                                  cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
664                         }
665                 }
666                 cto->ct_syshandle = hdl;
667         } else if (IS_FC(isp)) {
668                 at2_entry_t *aep;
669                 ct2_entry_t *cto = &un._ctio2;
670
671                 va_start(ap, isp);
672                 aep = va_arg(ap, at2_entry_t *);
673                 code = va_arg(ap, uint32_t);
674                 hdl = va_arg(ap, uint32_t);
675                 va_end(ap);
676
677                 isp_prt(isp, ISP_LOGTDEBUG0, "%s: [RX_ID 0x%x] code %x", __func__, aep->at_rxid, code);
678
679                 sts = code & 0xff;
680                 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
681                 cto->ct_header.rqs_entry_count = 1;
682                 if (ISP_CAP_SCCFW(isp) == 0) {
683                         cto->ct_lun = aep->at_lun;
684                 }
685                 if (ISP_CAP_2KLOGIN(isp)) {
686                         un._ctio2e.ct_iid = ((at2e_entry_t *)aep)->at_iid;
687                 } else {
688                         cto->ct_iid = aep->at_iid;
689                 }
690                 cto->ct_rxid = aep->at_rxid;
691                 cto->rsp.m1.ct_scsi_status = sts;
692                 cto->ct_flags = CT2_SENDSTATUS | CT2_NO_DATA | CT2_FLAG_MODE1;
693                 if (hdl == 0) {
694                         cto->ct_flags |= CT2_CCINCR;
695                 }
696                 if (aep->at_datalen) {
697                         cto->ct_resid = aep->at_datalen;
698                         cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
699                 }
700                 if (sts == SCSI_CHECK && (code & ECMD_SVALID)) {
701                         cto->rsp.m1.ct_resp[0] = 0xf0;
702                         cto->rsp.m1.ct_resp[2] = (code >> 12) & 0xf;
703                         cto->rsp.m1.ct_resp[7] = 8;
704                         cto->rsp.m1.ct_resp[12] = (code >> 24) & 0xff;
705                         cto->rsp.m1.ct_resp[13] = (code >> 16) & 0xff;
706                         cto->rsp.m1.ct_senselen = 16;
707                         cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
708                 }
709                 cto->ct_syshandle = hdl;
710         } else {
711                 at_entry_t *aep;
712                 ct_entry_t *cto = &un._ctio;
713
714                 va_start(ap, isp);
715                 aep = va_arg(ap, at_entry_t *);
716                 code = va_arg(ap, uint32_t);
717                 hdl = va_arg(ap, uint32_t);
718                 va_end(ap);
719                 isp_prt(isp, ISP_LOGTDEBUG0, "%s: [IID %d] code %x", __func__, aep->at_iid, code);
720                 sts = code;
721
722                 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO;
723                 cto->ct_header.rqs_entry_count = 1;
724                 cto->ct_fwhandle = aep->at_handle;
725                 cto->ct_iid = aep->at_iid;
726                 cto->ct_tgt = aep->at_tgt;
727                 cto->ct_lun = aep->at_lun;
728                 cto->ct_tag_type = aep->at_tag_type;
729                 cto->ct_tag_val = aep->at_tag_val;
730                 if (aep->at_flags & AT_TQAE) {
731                         cto->ct_flags |= CT_TQAE;
732                 }
733                 cto->ct_flags = CT_SENDSTATUS | CT_NO_DATA;
734                 if (hdl == 0) {
735                         cto->ct_flags |= CT_CCINCR;
736                 }
737                 cto->ct_scsi_status = sts;
738                 cto->ct_syshandle = hdl;
739         }
740         return (isp_target_put_entry(isp, &un));
741 }
742
743 /*
744  * These are either broadcast events or specifically CTIO fast completion
745  */
746
747 int
748 isp_target_async(ispsoftc_t *isp, int bus, int event)
749 {
750         isp_notify_t notify;
751
752         ISP_MEMZERO(&notify, sizeof (isp_notify_t));
753         notify.nt_hba = isp;
754         notify.nt_wwn = INI_ANY;
755         notify.nt_nphdl = NIL_HANDLE;
756         notify.nt_sid = PORT_ANY;
757         notify.nt_did = PORT_ANY;
758         notify.nt_tgt = TGT_ANY;
759         notify.nt_channel = bus;
760         notify.nt_lun = LUN_ANY;
761         notify.nt_tagval = TAG_ANY;
762         notify.nt_tagval |= (((uint64_t)(isp->isp_serno++)) << 32);
763
764         switch (event) {
765         case ASYNC_LOOP_UP:
766         case ASYNC_PTPMODE:
767                 isp_prt(isp, ISP_LOGTDEBUG0, "%s: LOOP UP", __func__);
768                 notify.nt_ncode = NT_LINK_UP;
769                 isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
770                 break;
771         case ASYNC_LOOP_DOWN:
772                 isp_prt(isp, ISP_LOGTDEBUG0, "%s: LOOP DOWN", __func__);
773                 notify.nt_ncode = NT_LINK_DOWN;
774                 isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
775                 break;
776         case ASYNC_LIP_ERROR:
777         case ASYNC_LIP_F8:
778         case ASYNC_LIP_OCCURRED:
779         case ASYNC_LOOP_RESET:
780                 isp_prt(isp, ISP_LOGTDEBUG0, "%s: LIP RESET", __func__);
781                 notify.nt_ncode = NT_LIP_RESET;
782                 isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
783                 break;
784         case ASYNC_BUS_RESET:
785         case ASYNC_TIMEOUT_RESET:       /* XXX: where does this come from ? */
786                 isp_prt(isp, ISP_LOGTDEBUG0, "%s: BUS RESET", __func__);
787                 notify.nt_ncode = NT_BUS_RESET;
788                 isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
789                 break;
790         case ASYNC_DEVICE_RESET:
791                 isp_prt(isp, ISP_LOGTDEBUG0, "%s: DEVICE RESET", __func__);
792                 notify.nt_ncode = NT_TARGET_RESET;
793                 isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
794                 break;
795         case ASYNC_CTIO_DONE:
796         {
797                 uint8_t storage[QENTRY_LEN];
798                 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO DONE", __func__);
799                 memset(storage, 0, QENTRY_LEN);
800                 if (IS_24XX(isp)) {
801                         ct7_entry_t *ct = (ct7_entry_t *) storage;
802                         ct->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
803                         ct->ct_nphdl = CT7_OK;
804                         ct->ct_syshandle = bus;
805                         ct->ct_flags = CT7_SENDSTATUS;
806                 } else if (IS_FC(isp)) {
807                         /* This should also suffice for 2K login code */
808                         ct2_entry_t *ct = (ct2_entry_t *) storage;
809                         ct->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
810                         ct->ct_status = CT_OK;
811                         ct->ct_syshandle = bus;
812                         ct->ct_flags = CT2_SENDSTATUS|CT2_FASTPOST;
813                 } else {
814                         ct_entry_t *ct = (ct_entry_t *) storage;
815                         ct->ct_header.rqs_entry_type = RQSTYPE_CTIO;
816                         ct->ct_status = CT_OK;
817                         ct->ct_syshandle = bus;
818                         /* we skip fwhandle here */
819                         ct->ct_fwhandle = 0;
820                         ct->ct_flags = CT_SENDSTATUS;
821                 }
822                 isp_async(isp, ISPASYNC_TARGET_ACTION, storage);
823                 break;
824         }
825         default:
826                 isp_prt(isp, ISP_LOGERR, "%s: unknown event 0x%x", __func__, event);
827                 if (isp->isp_state == ISP_RUNSTATE) {
828                         (void) isp_notify_ack(isp, NULL);
829                 }
830                 break;
831         }
832         return (0);
833 }
834
835
836 /*
837  * Process a received message.
838  * The ISP firmware can handle most messages, there are only
839  * a few that we need to deal with:
840  * - abort: clean up the current command
841  * - abort tag and clear queue
842  */
843
844 static void
845 isp_got_msg(ispsoftc_t *isp, in_entry_t *inp)
846 {
847         isp_notify_t notify;
848         uint8_t status = inp->in_status & ~QLTM_SVALID;
849
850         ISP_MEMZERO(&notify, sizeof (notify));
851         notify.nt_hba = isp;
852         notify.nt_wwn = INI_ANY;
853         notify.nt_nphdl = GET_IID_VAL(inp->in_iid);
854         notify.nt_sid = PORT_ANY;
855         notify.nt_did = PORT_ANY;
856         notify.nt_channel = GET_BUS_VAL(inp->in_iid);
857         notify.nt_tgt = inp->in_tgt;
858         notify.nt_lun = inp->in_lun;
859         IN_MAKE_TAGID(notify.nt_tagval, inp);
860         notify.nt_tagval |= (((uint64_t)(isp->isp_serno++)) << 32);
861         notify.nt_lreserved = inp;
862
863         if (status == IN_IDE_RECEIVED || status == IN_MSG_RECEIVED) {
864                 switch (inp->in_msg[0]) {
865                 case MSG_ABORT:
866                         notify.nt_ncode = NT_ABORT_TASK_SET;
867                         break;
868                 case MSG_BUS_DEV_RESET:
869                         notify.nt_ncode = NT_TARGET_RESET;
870                         break;
871                 case MSG_ABORT_TAG:
872                         notify.nt_ncode = NT_ABORT_TASK;
873                         break;
874                 case MSG_CLEAR_QUEUE:
875                         notify.nt_ncode = NT_CLEAR_TASK_SET;
876                         break;
877                 case MSG_REL_RECOVERY:
878                         notify.nt_ncode = NT_CLEAR_ACA;
879                         break;
880                 case MSG_TERM_IO_PROC:
881                         notify.nt_ncode = NT_ABORT_TASK;
882                         break;
883                 case MSG_LUN_RESET:
884                         notify.nt_ncode = NT_LUN_RESET;
885                         break;
886                 default:
887                         isp_prt(isp, ISP_LOGERR, "%s: unhandled message 0x%x", __func__, inp->in_msg[0]);
888                         (void) isp_notify_ack(isp, inp);
889                         return;
890                 }
891                 isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
892         } else {
893                 isp_prt(isp, ISP_LOGERR, "%s: unknown immediate notify status 0x%x", __func__, inp->in_status);
894                 (void) isp_notify_ack(isp, inp);
895         }
896 }
897
898 /*
899  * Synthesize a message from the task management flags in a FCP_CMND_IU.
900  */
901 static void
902 isp_got_msg_fc(ispsoftc_t *isp, in_fcentry_t *inp)
903 {
904         isp_notify_t notify;
905         static const char f1[] = "%s from N-port handle 0x%x lun %d seq 0x%x";
906         static const char f2[] = "unknown %s 0x%x lun %d N-Port handle 0x%x task flags 0x%x seq 0x%x\n";
907         uint16_t seqid, loopid;
908
909         ISP_MEMZERO(&notify, sizeof (isp_notify_t));
910         notify.nt_hba = isp;
911         notify.nt_wwn = INI_ANY;
912         if (ISP_CAP_2KLOGIN(isp)) {
913                 notify.nt_nphdl = ((in_fcentry_e_t *)inp)->in_iid;
914                 loopid = ((in_fcentry_e_t *)inp)->in_iid;
915                 seqid = ((in_fcentry_e_t *)inp)->in_seqid;
916         } else {
917                 notify.nt_nphdl = inp->in_iid;
918                 loopid = inp->in_iid;
919                 seqid = inp->in_seqid;
920         }
921         notify.nt_sid = PORT_ANY;
922         notify.nt_did = PORT_ANY;
923
924         /* nt_tgt set in outer layers */
925         if (ISP_CAP_SCCFW(isp)) {
926                 notify.nt_lun = inp->in_scclun;
927         } else {
928                 notify.nt_lun = inp->in_lun;
929         }
930         notify.nt_tagval = seqid;
931         notify.nt_tagval |= (((uint64_t)(isp->isp_serno++)) << 32);
932         notify.nt_need_ack = 1;
933         notify.nt_lreserved = inp;
934
935         if (inp->in_status != IN_MSG_RECEIVED) {
936                 isp_prt(isp, ISP_LOGINFO, f2, "immediate notify status", inp->in_status, notify.nt_lun, loopid, inp->in_task_flags, inp->in_seqid);
937                 (void) isp_notify_ack(isp, inp);
938                 return;
939         }
940
941         if (inp->in_task_flags & TASK_FLAGS_ABORT_TASK_SET) {
942                 isp_prt(isp, ISP_LOGINFO, f1, "ABORT TASK SET", loopid, notify.nt_lun, inp->in_seqid);
943                 notify.nt_ncode = NT_ABORT_TASK_SET;
944         } else if (inp->in_task_flags & TASK_FLAGS_CLEAR_TASK_SET) {
945                 isp_prt(isp, ISP_LOGINFO, f1, "CLEAR TASK SET", loopid, notify.nt_lun, inp->in_seqid);
946                 notify.nt_ncode = NT_CLEAR_TASK_SET;
947         } else if (inp->in_task_flags & TASK_FLAGS_LUN_RESET) {
948                 isp_prt(isp, ISP_LOGINFO, f1, "LUN RESET", loopid, notify.nt_lun, inp->in_seqid);
949                 notify.nt_ncode = NT_LUN_RESET;
950         } else if (inp->in_task_flags & TASK_FLAGS_TARGET_RESET) {
951                 isp_prt(isp, ISP_LOGINFO, f1, "TARGET RESET", loopid, notify.nt_lun, inp->in_seqid);
952                 notify.nt_ncode = NT_TARGET_RESET;
953         } else if (inp->in_task_flags & TASK_FLAGS_CLEAR_ACA) {
954                 isp_prt(isp, ISP_LOGINFO, f1, "CLEAR ACA", loopid, notify.nt_lun, inp->in_seqid);
955                 notify.nt_ncode = NT_CLEAR_ACA;
956         } else {
957                 isp_prt(isp, ISP_LOGWARN, f2, "task flag", inp->in_status, notify.nt_lun, loopid, inp->in_task_flags,  inp->in_seqid);
958                 (void) isp_notify_ack(isp, inp);
959                 return;
960         }
961         isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
962 }
963
964 static void
965 isp_got_tmf_24xx(ispsoftc_t *isp, at7_entry_t *aep)
966 {
967         isp_notify_t notify;
968         static const char f1[] = "%s from PortID 0x%06x lun %d seq 0x%08x";
969         static const char f2[] = "unknown Task Flag 0x%x lun %d PortID 0x%x tag 0x%08x";
970         uint16_t chan;
971         uint32_t sid, did;
972
973         ISP_MEMZERO(&notify, sizeof (isp_notify_t));
974         notify.nt_hba = isp;
975         notify.nt_wwn = INI_ANY;
976         notify.nt_lun = (aep->at_cmnd.fcp_cmnd_lun[0] << 8) | (aep->at_cmnd.fcp_cmnd_lun[1]);
977         notify.nt_tagval = aep->at_rxid;
978         notify.nt_tagval |= (((uint64_t)(isp->isp_serno++)) << 32);
979         notify.nt_lreserved = aep;
980         sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] <<  8) | (aep->at_hdr.s_id[2]);
981
982         /* Channel has to derived from D_ID */
983         did = (aep->at_hdr.d_id[0] << 16) | (aep->at_hdr.d_id[1] << 8) | aep->at_hdr.d_id[2];
984         for (chan = 0; chan < isp->isp_nchan; chan++) {
985                 if (FCPARAM(isp, chan)->isp_portid == did) {
986                         break;
987                 }
988         }
989         if (chan == isp->isp_nchan) {
990                 isp_prt(isp, ISP_LOGWARN, "%s: D_ID 0x%x not found on any channel", __func__, did);
991                 /* just drop on the floor */
992                 return;
993         }
994         notify.nt_nphdl = NIL_HANDLE; /* unknown here */
995         notify.nt_sid = sid;
996         notify.nt_did = did;
997         notify.nt_channel = chan;
998         if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_ABORT_TASK_SET) {
999                 isp_prt(isp, ISP_LOGINFO, f1, "ABORT TASK SET", sid, notify.nt_lun, aep->at_rxid);
1000                 notify.nt_ncode = NT_ABORT_TASK_SET;
1001         } else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_CLEAR_TASK_SET) {
1002                 isp_prt(isp, ISP_LOGINFO, f1, "CLEAR TASK SET", sid, notify.nt_lun, aep->at_rxid);
1003                 notify.nt_ncode = NT_CLEAR_TASK_SET;
1004         } else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_LUN_RESET) {
1005                 isp_prt(isp, ISP_LOGINFO, f1, "LUN RESET", sid, notify.nt_lun, aep->at_rxid);
1006                 notify.nt_ncode = NT_LUN_RESET;
1007         } else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_TGT_RESET) {
1008                 isp_prt(isp, ISP_LOGINFO, f1, "TARGET RESET", sid, notify.nt_lun, aep->at_rxid);
1009                 notify.nt_ncode = NT_TARGET_RESET;
1010         } else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_CLEAR_ACA) {
1011                 isp_prt(isp, ISP_LOGINFO, f1, "CLEAR ACA", sid, notify.nt_lun, aep->at_rxid);
1012                 notify.nt_ncode = NT_CLEAR_ACA;
1013         } else {
1014                 isp_prt(isp, ISP_LOGWARN, f2, aep->at_cmnd.fcp_cmnd_task_management, notify.nt_lun, sid, aep->at_rxid);
1015                 notify.nt_ncode = NT_UNKNOWN;
1016                 return;
1017         }
1018         isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
1019 }
1020
1021 int
1022 isp_notify_ack(ispsoftc_t *isp, void *arg)
1023 {
1024         char storage[QENTRY_LEN];
1025         void *outp;
1026
1027         /*
1028          * This is in case a Task Management Function ends up here.
1029          */
1030         if (IS_24XX(isp) && arg != NULL && (((isphdr_t *)arg)->rqs_entry_type == RQSTYPE_ATIO)) {
1031                 at7_entry_t *aep = arg;
1032                 return (isp_endcmd(isp, aep, NIL_HANDLE, 0, 0, 0));
1033         }
1034
1035         outp = isp_getrqentry(isp);
1036         if (outp == NULL) {
1037                 isp_prt(isp, ISP_LOGWARN, rqo, __func__);
1038                 return (1);
1039         }
1040
1041         ISP_MEMZERO(storage, QENTRY_LEN);
1042
1043         if (IS_24XX(isp)) {
1044                 na_fcentry_24xx_t *na = (na_fcentry_24xx_t *) storage;
1045                 if (arg) {
1046                         in_fcentry_24xx_t *in = arg;
1047                         na->na_nphdl = in->in_nphdl;
1048                         na->na_flags = in->in_flags & IN24XX_FLAG_PUREX_IOCB;
1049                         na->na_status = in->in_status;
1050                         na->na_status_subcode = in->in_status_subcode;
1051                         na->na_rxid = in->in_rxid;
1052                         na->na_oxid = in->in_oxid;
1053                         na->na_vpidx = in->in_vpidx;
1054                         if (in->in_status == IN24XX_SRR_RCVD) {
1055                                 na->na_srr_rxid = in->in_srr_rxid;
1056                                 na->na_srr_reloff_hi = in->in_srr_reloff_hi;
1057                                 na->na_srr_reloff_lo = in->in_srr_reloff_lo;
1058                                 na->na_srr_iu = in->in_srr_iu;
1059                                 na->na_srr_flags = 1;
1060                                 na->na_srr_reject_vunique = 0;
1061                                 na->na_srr_reject_explanation = 1;
1062                                 na->na_srr_reject_code = 1;
1063                         }
1064                 }
1065                 na->na_header.rqs_entry_type = RQSTYPE_NOTIFY_ACK;
1066                 na->na_header.rqs_entry_count = 1;
1067                 isp_put_notify_24xx_ack(isp, na, (na_fcentry_24xx_t *)outp);
1068         } else if (IS_FC(isp)) {
1069                 na_fcentry_t *na = (na_fcentry_t *) storage;
1070                 int iid = 0;
1071
1072                 if (arg) {
1073                         in_fcentry_t *inp = arg;
1074                         ISP_MEMCPY(storage, arg, sizeof (isphdr_t));
1075                         if (ISP_CAP_2KLOGIN(isp)) {
1076                                 ((na_fcentry_e_t *)na)->na_iid = ((in_fcentry_e_t *)inp)->in_iid;
1077                                 iid = ((na_fcentry_e_t *)na)->na_iid;
1078                         } else {
1079                                 na->na_iid = inp->in_iid;
1080                                 iid = na->na_iid;
1081                         }
1082                         na->na_task_flags = inp->in_task_flags & TASK_FLAGS_RESERVED_MASK;
1083                         na->na_seqid = inp->in_seqid;
1084                         na->na_flags = NAFC_RCOUNT;
1085                         na->na_status = inp->in_status;
1086                         if (inp->in_status == IN_RESET) {
1087                                 na->na_flags |= NAFC_RST_CLRD;
1088                         }
1089                         if (inp->in_status == IN_MSG_RECEIVED) {
1090                                 na->na_flags |= NAFC_TVALID;
1091                                 na->na_response = 0;    /* XXX SUCCEEDED XXX */
1092                         }
1093                 } else {
1094                         na->na_flags = NAFC_RST_CLRD;
1095                 }
1096                 na->na_header.rqs_entry_type = RQSTYPE_NOTIFY_ACK;
1097                 na->na_header.rqs_entry_count = 1;
1098                 if (ISP_CAP_2KLOGIN(isp)) {
1099                         isp_put_notify_ack_fc_e(isp, (na_fcentry_e_t *) na, (na_fcentry_e_t *)outp);
1100                 } else {
1101                         isp_put_notify_ack_fc(isp, na, (na_fcentry_t *)outp);
1102                 }
1103                 isp_prt(isp, ISP_LOGTDEBUG0, "notify ack loopid %u seqid %x flags %x tflags %x response %x", iid, na->na_seqid,
1104                     na->na_flags, na->na_task_flags, na->na_response);
1105         } else {
1106                 na_entry_t *na = (na_entry_t *) storage;
1107                 if (arg) {
1108                         in_entry_t *inp = arg;
1109                         ISP_MEMCPY(storage, arg, sizeof (isphdr_t));
1110                         na->na_iid = inp->in_iid;
1111                         na->na_lun = inp->in_lun;
1112                         na->na_tgt = inp->in_tgt;
1113                         na->na_seqid = inp->in_seqid;
1114                         if (inp->in_status == IN_RESET) {
1115                                 na->na_event = NA_RST_CLRD;
1116                         }
1117                 } else {
1118                         na->na_event = NA_RST_CLRD;
1119                 }
1120                 na->na_header.rqs_entry_type = RQSTYPE_NOTIFY_ACK;
1121                 na->na_header.rqs_entry_count = 1;
1122                 isp_put_notify_ack(isp, na, (na_entry_t *)outp);
1123                 isp_prt(isp, ISP_LOGTDEBUG0, "notify ack loopid %u lun %u tgt %u seqid %x event %x", na->na_iid, na->na_lun, na->na_tgt, na->na_seqid, na->na_event);
1124         }
1125         ISP_TDQE(isp, "isp_notify_ack", isp->isp_reqidx, storage);
1126         ISP_SYNC_REQUEST(isp);
1127         return (0);
1128 }
1129
1130 int
1131 isp_acknak_abts(ispsoftc_t *isp, void *arg, int errno)
1132 {
1133         char storage[QENTRY_LEN];
1134         uint16_t tmpw;
1135         uint8_t tmpb;
1136         abts_t *abts = arg;
1137         abts_rsp_t *rsp = (abts_rsp_t *) storage;
1138         void *outp;
1139
1140         if (!IS_24XX(isp)) {
1141                 isp_prt(isp, ISP_LOGERR, "%s: called for non-24XX card", __func__);
1142                 return (0);
1143         }
1144
1145         if (abts->abts_header.rqs_entry_type != RQSTYPE_ABTS_RCVD) {
1146                 isp_prt(isp, ISP_LOGERR, "%s: called for non-ABTS entry (0x%x)", __func__, abts->abts_header.rqs_entry_type);
1147                 return (0);
1148         }
1149
1150         outp = isp_getrqentry(isp);
1151         if (outp == NULL) {
1152                 isp_prt(isp, ISP_LOGWARN, rqo, __func__);
1153                 return (1);
1154         }
1155
1156         ISP_MEMCPY(rsp, abts, QENTRY_LEN);
1157         rsp->abts_rsp_header.rqs_entry_type = RQSTYPE_ABTS_RSP;
1158
1159         /*
1160          * Swap destination and source for response.
1161          */
1162         rsp->abts_rsp_r_ctl = BA_ACC;
1163         tmpw = rsp->abts_rsp_did_lo;
1164         tmpb = rsp->abts_rsp_did_hi;
1165         rsp->abts_rsp_did_lo = rsp->abts_rsp_sid_lo;
1166         rsp->abts_rsp_did_hi = rsp->abts_rsp_sid_hi;
1167         rsp->abts_rsp_sid_lo = tmpw;
1168         rsp->abts_rsp_sid_hi = tmpb;
1169
1170         rsp->abts_rsp_f_ctl_hi ^= 0x80;         /* invert Exchange Context */
1171         rsp->abts_rsp_f_ctl_hi &= ~0x7f;        /* clear Sequence Initiator and other bits */
1172         rsp->abts_rsp_f_ctl_hi |= 0x10;         /* abort the whole exchange */
1173         rsp->abts_rsp_f_ctl_hi |= 0x8;          /* last data frame of sequence */
1174         rsp->abts_rsp_f_ctl_hi |= 0x1;          /* transfer Sequence Initiative */
1175         rsp->abts_rsp_f_ctl_lo = 0;
1176
1177         if (errno == 0) {
1178                 uint16_t rx_id, ox_id;
1179
1180                 rx_id = rsp->abts_rsp_rx_id;
1181                 ox_id = rsp->abts_rsp_ox_id;
1182                 ISP_MEMZERO(&rsp->abts_rsp_payload.ba_acc, sizeof (rsp->abts_rsp_payload.ba_acc));
1183                 isp_prt(isp, ISP_LOGTINFO, "[0x%x] ABTS of 0x%x being BA_ACC'd", rsp->abts_rsp_rxid_abts, rsp->abts_rsp_rxid_task);
1184                 rsp->abts_rsp_payload.ba_acc.aborted_rx_id = rx_id;
1185                 rsp->abts_rsp_payload.ba_acc.aborted_ox_id = ox_id;
1186                 rsp->abts_rsp_payload.ba_acc.high_seq_cnt = 0xffff;
1187         } else {
1188                 ISP_MEMZERO(&rsp->abts_rsp_payload.ba_rjt, sizeof (rsp->abts_rsp_payload.ba_acc));
1189                 switch (errno) {
1190                 case ENOMEM:
1191                         rsp->abts_rsp_payload.ba_rjt.reason = 5;        /* Logical Busy */
1192                         break;
1193                 default:
1194                         rsp->abts_rsp_payload.ba_rjt.reason = 9;        /* Unable to perform command request */
1195                         break;
1196                 }
1197         }
1198
1199         /*
1200          * The caller will have set response values as appropriate
1201          * in the ABTS structure just before calling us.
1202          */
1203         isp_put_abts_rsp(isp, rsp, (abts_rsp_t *)outp);
1204         ISP_TDQE(isp, "isp_acknak_abts", isp->isp_reqidx, storage);
1205         ISP_SYNC_REQUEST(isp);
1206         return (0);
1207 }
1208
1209 static void
1210 isp_handle_atio(ispsoftc_t *isp, at_entry_t *aep)
1211 {
1212         int lun;
1213         lun = aep->at_lun;
1214         /*
1215          * The firmware status (except for the QLTM_SVALID bit) indicates
1216          * why this ATIO was sent to us.
1217          *
1218          * If QLTM_SVALID is set, the firware has recommended Sense Data.
1219          *
1220          * If the DISCONNECTS DISABLED bit is set in the flags field,
1221          * we're still connected on the SCSI bus - i.e. the initiator
1222          * did not set DiscPriv in the identify message. We don't care
1223          * about this so it's ignored.
1224          */
1225
1226         switch (aep->at_status & ~QLTM_SVALID) {
1227         case AT_PATH_INVALID:
1228                 /*
1229                  * ATIO rejected by the firmware due to disabled lun.
1230                  */
1231                 isp_prt(isp, ISP_LOGERR, "rejected ATIO for disabled lun %d", lun);
1232                 break;
1233         case AT_NOCAP:
1234                 /*
1235                  * Requested Capability not available
1236                  * We sent an ATIO that overflowed the firmware's
1237                  * command resource count.
1238                  */
1239                 isp_prt(isp, ISP_LOGERR, "rejected ATIO for lun %d because of command count overflow", lun);
1240                 break;
1241
1242         case AT_BDR_MSG:
1243                 /*
1244                  * If we send an ATIO to the firmware to increment
1245                  * its command resource count, and the firmware is
1246                  * recovering from a Bus Device Reset, it returns
1247                  * the ATIO with this status. We set the command
1248                  * resource count in the Enable Lun entry and do
1249                  * not increment it. Therefore we should never get
1250                  * this status here.
1251                  */
1252                 isp_prt(isp, ISP_LOGERR, atiocope, lun, GET_BUS_VAL(aep->at_iid));
1253                 break;
1254
1255         case AT_CDB:            /* Got a CDB */
1256         case AT_PHASE_ERROR:    /* Bus Phase Sequence Error */
1257                 /*
1258                  * Punt to platform specific layer.
1259                  */
1260                 isp_async(isp, ISPASYNC_TARGET_ACTION, aep);
1261                 break;
1262
1263         case AT_RESET:
1264                 /*
1265                  * A bus reset came along and blew away this command. Why
1266                  * they do this in addition the async event code stuff,
1267                  * I dunno.
1268                  *
1269                  * Ignore it because the async event will clear things
1270                  * up for us.
1271                  */
1272                 isp_prt(isp, ISP_LOGWARN, atior, lun, GET_IID_VAL(aep->at_iid), GET_BUS_VAL(aep->at_iid));
1273                 break;
1274
1275
1276         default:
1277                 isp_prt(isp, ISP_LOGERR, "Unknown ATIO status 0x%x from loopid %d for lun %d", aep->at_status, aep->at_iid, lun);
1278                 (void) isp_target_put_atio(isp, aep);
1279                 break;
1280         }
1281 }
1282
1283 static void
1284 isp_handle_atio2(ispsoftc_t *isp, at2_entry_t *aep)
1285 {
1286         int lun, iid;
1287
1288         if (ISP_CAP_SCCFW(isp)) {
1289                 lun = aep->at_scclun;
1290         } else {
1291                 lun = aep->at_lun;
1292         }
1293
1294         if (ISP_CAP_2KLOGIN(isp)) {
1295                 iid = ((at2e_entry_t *)aep)->at_iid;
1296         } else {
1297                 iid = aep->at_iid;
1298         }
1299
1300         /*
1301          * The firmware status (except for the QLTM_SVALID bit) indicates
1302          * why this ATIO was sent to us.
1303          *
1304          * If QLTM_SVALID is set, the firware has recommended Sense Data.
1305          *
1306          * If the DISCONNECTS DISABLED bit is set in the flags field,
1307          * we're still connected on the SCSI bus - i.e. the initiator
1308          * did not set DiscPriv in the identify message. We don't care
1309          * about this so it's ignored.
1310          */
1311
1312         switch (aep->at_status & ~QLTM_SVALID) {
1313         case AT_PATH_INVALID:
1314                 /*
1315                  * ATIO rejected by the firmware due to disabled lun.
1316                  */
1317                 isp_prt(isp, ISP_LOGERR, "rejected ATIO2 for disabled lun %d", lun);
1318                 break;
1319         case AT_NOCAP:
1320                 /*
1321                  * Requested Capability not available
1322                  * We sent an ATIO that overflowed the firmware's
1323                  * command resource count.
1324                  */
1325                 isp_prt(isp, ISP_LOGERR, "rejected ATIO2 for lun %d- command count overflow", lun);
1326                 break;
1327
1328         case AT_BDR_MSG:
1329                 /*
1330                  * If we send an ATIO to the firmware to increment
1331                  * its command resource count, and the firmware is
1332                  * recovering from a Bus Device Reset, it returns
1333                  * the ATIO with this status. We set the command
1334                  * resource count in the Enable Lun entry and no
1335                  * not increment it. Therefore we should never get
1336                  * this status here.
1337                  */
1338                 isp_prt(isp, ISP_LOGERR, atiocope, lun, 0);
1339                 break;
1340
1341         case AT_CDB:            /* Got a CDB */
1342                 /*
1343                  * Punt to platform specific layer.
1344                  */
1345                 isp_async(isp, ISPASYNC_TARGET_ACTION, aep);
1346                 break;
1347
1348         case AT_RESET:
1349                 /*
1350                  * A bus reset came along an blew away this command. Why
1351                  * they do this in addition the async event code stuff,
1352                  * I dunno.
1353                  *
1354                  * Ignore it because the async event will clear things
1355                  * up for us.
1356                  */
1357                 isp_prt(isp, ISP_LOGERR, atior, lun, iid, 0);
1358                 break;
1359
1360
1361         default:
1362                 isp_prt(isp, ISP_LOGERR, "Unknown ATIO2 status 0x%x from loopid %d for lun %d", aep->at_status, iid, lun);
1363                 (void) isp_target_put_atio(isp, aep);
1364                 break;
1365         }
1366 }
1367
1368 static void
1369 isp_handle_ctio(ispsoftc_t *isp, ct_entry_t *ct)
1370 {
1371         void *xs;
1372         int pl = ISP_LOGTDEBUG2;
1373         char *fmsg = NULL;
1374
1375         if (ct->ct_syshandle) {
1376                 xs = isp_find_xs_tgt(isp, ct->ct_syshandle);
1377                 if (xs == NULL) {
1378                         pl = ISP_LOGALL;
1379                 }
1380         } else {
1381                 xs = NULL;
1382         }
1383
1384         switch (ct->ct_status & ~QLTM_SVALID) {
1385         case CT_OK:
1386                 /*
1387                  * There are generally 3 possibilities as to why we'd get
1388                  * this condition:
1389                  *      We disconnected after receiving a CDB.
1390                  *      We sent or received data.
1391                  *      We sent status & command complete.
1392                  */
1393
1394                 if (ct->ct_flags & CT_SENDSTATUS) {
1395                         break;
1396                 } else if ((ct->ct_flags & CT_DATAMASK) == CT_NO_DATA) {
1397                         /*
1398                          * Nothing to do in this case.
1399                          */
1400                         isp_prt(isp, pl, "CTIO- iid %d disconnected OK", ct->ct_iid);
1401                         return;
1402                 }
1403                 break;
1404
1405         case CT_BDR_MSG:
1406                 /*
1407                  * Bus Device Reset message received or the SCSI Bus has
1408                  * been Reset; the firmware has gone to Bus Free.
1409                  *
1410                  * The firmware generates an async mailbox interrupt to
1411                  * notify us of this and returns outstanding CTIOs with this
1412                  * status. These CTIOs are handled in that same way as
1413                  * CT_ABORTED ones, so just fall through here.
1414                  */
1415                 fmsg = "Bus Device Reset";
1416                 /*FALLTHROUGH*/
1417         case CT_RESET:
1418                 if (fmsg == NULL)
1419                         fmsg = "Bus Reset";
1420                 /*FALLTHROUGH*/
1421         case CT_ABORTED:
1422                 /*
1423                  * When an Abort message is received the firmware goes to
1424                  * Bus Free and returns all outstanding CTIOs with the status
1425                  * set, then sends us an Immediate Notify entry.
1426                  */
1427                 if (fmsg == NULL)
1428                         fmsg = "ABORT TAG message sent by Initiator";
1429                 isp_prt(isp, ISP_LOGTDEBUG0, "CTIO destroyed by %s", fmsg);
1430                 break;
1431
1432         case CT_INVAL:
1433                 /*
1434                  * CTIO rejected by the firmware due to disabled lun.
1435                  * "Cannot Happen".
1436                  */
1437                 isp_prt(isp, ISP_LOGERR, "Firmware rejected CTIO for disabled lun %d", ct->ct_lun);
1438                 break;
1439
1440         case CT_NOPATH:
1441                 /*
1442                  * CTIO rejected by the firmware due "no path for the
1443                  * nondisconnecting nexus specified". This means that
1444                  * we tried to access the bus while a non-disconnecting
1445                  * command is in process.
1446                  */
1447                 isp_prt(isp, ISP_LOGERR, "Firmware rejected CTIO for bad nexus %d/%d/%d", ct->ct_iid, ct->ct_tgt, ct->ct_lun);
1448                 break;
1449
1450         case CT_RSELTMO:
1451                 fmsg = "Reselection";
1452                 /*FALLTHROUGH*/
1453         case CT_TIMEOUT:
1454                 if (fmsg == NULL)
1455                         fmsg = "Command";
1456                 isp_prt(isp, ISP_LOGWARN, "Firmware timed out on %s", fmsg);
1457                 break;
1458
1459         case    CT_PANIC:
1460                 if (fmsg == NULL)
1461                         fmsg = "Unrecoverable Error";
1462                 /*FALLTHROUGH*/
1463         case CT_ERR:
1464                 if (fmsg == NULL)
1465                         fmsg = "Completed with Error";
1466                 /*FALLTHROUGH*/
1467         case CT_PHASE_ERROR:
1468                 if (fmsg == NULL)
1469                         fmsg = "Phase Sequence Error";
1470                 /*FALLTHROUGH*/
1471         case CT_TERMINATED:
1472                 if (fmsg == NULL)
1473                         fmsg = "terminated by TERMINATE TRANSFER";
1474                 /*FALLTHROUGH*/
1475         case CT_NOACK:
1476                 if (fmsg == NULL)
1477                         fmsg = "unacknowledged Immediate Notify pending";
1478                 isp_prt(isp, ISP_LOGERR, "CTIO returned by f/w- %s", fmsg);
1479                 break;
1480         default:
1481                 isp_prt(isp, ISP_LOGERR, "Unknown CTIO status 0x%x", ct->ct_status & ~QLTM_SVALID);
1482                 break;
1483         }
1484
1485         if (xs == NULL) {
1486                 /*
1487                  * There may be more than one CTIO for a data transfer,
1488                  * or this may be a status CTIO we're not monitoring.
1489                  *
1490                  * The assumption is that they'll all be returned in the
1491                  * order we got them.
1492                  */
1493                 if (ct->ct_syshandle == 0) {
1494                         if ((ct->ct_flags & CT_SENDSTATUS) == 0) {
1495                                 isp_prt(isp, pl, "intermediate CTIO completed ok");
1496                         } else {
1497                                 isp_prt(isp, pl, "unmonitored CTIO completed ok");
1498                         }
1499                 } else {
1500                         isp_prt(isp, pl, "NO xs for CTIO (handle 0x%x) status 0x%x", ct->ct_syshandle, ct->ct_status & ~QLTM_SVALID);
1501                 }
1502         } else {
1503                 /*
1504                  * Final CTIO completed. Release DMA resources and
1505                  * notify platform dependent layers.
1506                  */
1507                 if ((ct->ct_flags & CT_DATAMASK) != CT_NO_DATA) {
1508                         ISP_DMAFREE(isp, xs, ct->ct_syshandle);
1509                 }
1510                 isp_prt(isp, pl, "final CTIO complete");
1511                 /*
1512                  * The platform layer will destroy the handle if appropriate.
1513                  */
1514                 isp_async(isp, ISPASYNC_TARGET_ACTION, ct);
1515         }
1516 }
1517
1518 static void
1519 isp_handle_ctio2(ispsoftc_t *isp, ct2_entry_t *ct)
1520 {
1521         void *xs;
1522         int pl = ISP_LOGTDEBUG2;
1523         char *fmsg = NULL;
1524
1525         if (ct->ct_syshandle) {
1526                 xs = isp_find_xs_tgt(isp, ct->ct_syshandle);
1527                 if (xs == NULL) {
1528                         pl = ISP_LOGALL;
1529                 }
1530         } else {
1531                 xs = NULL;
1532         }
1533
1534         switch (ct->ct_status & ~QLTM_SVALID) {
1535         case CT_BUS_ERROR:
1536                 isp_prt(isp, ISP_LOGERR, "PCI DMA Bus Error");
1537                 /* FALL Through */
1538         case CT_DATA_OVER:
1539         case CT_DATA_UNDER:
1540         case CT_OK:
1541                 /*
1542                  * There are generally 2 possibilities as to why we'd get
1543                  * this condition:
1544                  *      We sent or received data.
1545                  *      We sent status & command complete.
1546                  */
1547
1548                 break;
1549
1550         case CT_BDR_MSG:
1551                 /*
1552                  * Target Reset function received.
1553                  *
1554                  * The firmware generates an async mailbox interrupt to
1555                  * notify us of this and returns outstanding CTIOs with this
1556                  * status. These CTIOs are handled in that same way as
1557                  * CT_ABORTED ones, so just fall through here.
1558                  */
1559                 fmsg = "TARGET RESET";
1560                 /*FALLTHROUGH*/
1561         case CT_RESET:
1562                 if (fmsg == NULL)
1563                         fmsg = "LIP Reset";
1564                 /*FALLTHROUGH*/
1565         case CT_ABORTED:
1566                 /*
1567                  * When an Abort message is received the firmware goes to
1568                  * Bus Free and returns all outstanding CTIOs with the status
1569                  * set, then sends us an Immediate Notify entry.
1570                  */
1571                 if (fmsg == NULL) {
1572                         fmsg = "ABORT";
1573                 }
1574
1575                 isp_prt(isp, ISP_LOGTDEBUG0, "CTIO2 destroyed by %s: RX_ID=0x%x", fmsg, ct->ct_rxid);
1576                 break;
1577
1578         case CT_INVAL:
1579                 /*
1580                  * CTIO rejected by the firmware - invalid data direction.
1581                  */
1582                 isp_prt(isp, ISP_LOGERR, "CTIO2 had wrong data direction");
1583                 break;
1584
1585         case CT_RSELTMO:
1586                 fmsg = "failure to reconnect to initiator";
1587                 /*FALLTHROUGH*/
1588         case CT_TIMEOUT:
1589                 if (fmsg == NULL)
1590                         fmsg = "command";
1591                 isp_prt(isp, ISP_LOGWARN, "Firmware timed out on %s", fmsg);
1592                 break;
1593
1594         case CT_ERR:
1595                 fmsg = "Completed with Error";
1596                 /*FALLTHROUGH*/
1597         case CT_LOGOUT:
1598                 if (fmsg == NULL)
1599                         fmsg = "Port Logout";
1600                 /*FALLTHROUGH*/
1601         case CT_PORTUNAVAIL:
1602                 if (fmsg == NULL)
1603                         fmsg = "Port not available";
1604                 /*FALLTHROUGH*/
1605         case CT_PORTCHANGED:
1606                 if (fmsg == NULL)
1607                         fmsg = "Port Changed";
1608                 /*FALLTHROUGH*/
1609         case CT_NOACK:
1610                 if (fmsg == NULL)
1611                         fmsg = "unacknowledged Immediate Notify pending";
1612                 isp_prt(isp, ISP_LOGWARN, "CTIO returned by f/w- %s", fmsg);
1613                 break;
1614
1615         case CT_INVRXID:
1616                 /*
1617                  * CTIO rejected by the firmware because an invalid RX_ID.
1618                  * Just print a message.
1619                  */
1620                 isp_prt(isp, ISP_LOGWARN, "CTIO2 completed with Invalid RX_ID 0x%x", ct->ct_rxid);
1621                 break;
1622
1623         default:
1624                 isp_prt(isp, ISP_LOGERR, "Unknown CTIO2 status 0x%x", ct->ct_status & ~QLTM_SVALID);
1625                 break;
1626         }
1627
1628         if (xs == NULL) {
1629                 /*
1630                  * There may be more than one CTIO for a data transfer,
1631                  * or this may be a status CTIO we're not monitoring.
1632                  *
1633                  * The assumption is that they'll all be returned in the
1634                  * order we got them.
1635                  */
1636                 if (ct->ct_syshandle == 0) {
1637                         if ((ct->ct_flags & CT2_SENDSTATUS) == 0) {
1638                                 isp_prt(isp, pl, "intermediate CTIO completed ok");
1639                         } else {
1640                                 isp_prt(isp, pl, "unmonitored CTIO completed ok");
1641                         }
1642                 } else {
1643                         isp_prt(isp, pl, "NO xs for CTIO (handle 0x%x) status 0x%x", ct->ct_syshandle, ct->ct_status & ~QLTM_SVALID);
1644                 }
1645         } else {
1646                 if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA) {
1647                         ISP_DMAFREE(isp, xs, ct->ct_syshandle);
1648                 }
1649                 if (ct->ct_flags & CT2_SENDSTATUS) {
1650                         /*
1651                          * Sent status and command complete.
1652                          *
1653                          * We're now really done with this command, so we
1654                          * punt to the platform dependent layers because
1655                          * only there can we do the appropriate command
1656                          * complete thread synchronization.
1657                          */
1658                         isp_prt(isp, pl, "status CTIO complete");
1659                 } else {
1660                         /*
1661                          * Final CTIO completed. Release DMA resources and
1662                          * notify platform dependent layers.
1663                          */
1664                         isp_prt(isp, pl, "data CTIO complete");
1665                 }
1666                 isp_async(isp, ISPASYNC_TARGET_ACTION, ct);
1667                 /*
1668                  * The platform layer will destroy the handle if appropriate.
1669                  */
1670         }
1671 }
1672
1673 static void
1674 isp_handle_ctio7(ispsoftc_t *isp, ct7_entry_t *ct)
1675 {
1676         void *xs;
1677         int pl = ISP_LOGTDEBUG2;
1678         char *fmsg = NULL;
1679
1680         if (ct->ct_syshandle) {
1681                 xs = isp_find_xs_tgt(isp, ct->ct_syshandle);
1682                 if (xs == NULL) {
1683                         pl = ISP_LOGALL;
1684                 }
1685         } else {
1686                 xs = NULL;
1687         }
1688
1689         switch (ct->ct_nphdl) {
1690         case CT7_BUS_ERROR:
1691                 isp_prt(isp, ISP_LOGERR, "PCI DMA Bus Error");
1692                 /* FALL Through */
1693         case CT7_DATA_OVER:
1694         case CT7_DATA_UNDER:
1695         case CT7_OK:
1696                 /*
1697                  * There are generally 2 possibilities as to why we'd get
1698                  * this condition:
1699                  *      We sent or received data.
1700                  *      We sent status & command complete.
1701                  */
1702
1703                 break;
1704
1705         case CT7_RESET:
1706                 if (fmsg == NULL) {
1707                         fmsg = "LIP Reset";
1708                 }
1709                 /*FALLTHROUGH*/
1710         case CT7_ABORTED:
1711                 /*
1712                  * When an Abort message is received the firmware goes to
1713                  * Bus Free and returns all outstanding CTIOs with the status
1714                  * set, then sends us an Immediate Notify entry.
1715                  */
1716                 if (fmsg == NULL) {
1717                         fmsg = "ABORT";
1718                 }
1719                 isp_prt(isp, ISP_LOGTDEBUG0, "CTIO7 destroyed by %s: RX_ID=0x%x", fmsg, ct->ct_rxid);
1720                 break;
1721
1722         case CT7_TIMEOUT:
1723                 if (fmsg == NULL) {
1724                         fmsg = "command";
1725                 }
1726                 isp_prt(isp, ISP_LOGWARN, "Firmware timed out on %s", fmsg);
1727                 break;
1728
1729         case CT7_ERR:
1730                 fmsg = "Completed with Error";
1731                 /*FALLTHROUGH*/
1732         case CT7_LOGOUT:
1733                 if (fmsg == NULL) {
1734                         fmsg = "Port Logout";
1735                 }
1736                 /*FALLTHROUGH*/
1737         case CT7_PORTUNAVAIL:
1738                 if (fmsg == NULL) {
1739                         fmsg = "Port not available";
1740                 }
1741                 /*FALLTHROUGH*/
1742         case CT7_PORTCHANGED:
1743                 if (fmsg == NULL) {
1744                         fmsg = "Port Changed";
1745                 }
1746                 isp_prt(isp, ISP_LOGWARN, "CTIO returned by f/w- %s", fmsg);
1747                 break;
1748
1749         case CT7_INVRXID:
1750                 /*
1751                  * CTIO rejected by the firmware because an invalid RX_ID.
1752                  * Just print a message.
1753                  */
1754                 isp_prt(isp, ISP_LOGWARN, "CTIO7 completed with Invalid RX_ID 0x%x", ct->ct_rxid);
1755                 break;
1756
1757         case CT7_REASSY_ERR:
1758                 isp_prt(isp, ISP_LOGWARN, "reassembly error");
1759                 break;
1760
1761         case CT7_SRR:
1762                 isp_prt(isp, ISP_LOGWARN, "SRR received");
1763                 break;
1764
1765         default:
1766                 isp_prt(isp, ISP_LOGERR, "Unknown CTIO7 status 0x%x", ct->ct_nphdl);
1767                 break;
1768         }
1769
1770         if (xs == NULL) {
1771                 /*
1772                  * There may be more than one CTIO for a data transfer,
1773                  * or this may be a status CTIO we're not monitoring.
1774                  *
1775                  * The assumption is that they'll all be returned in the
1776                  * order we got them.
1777                  */
1778                 if (ct->ct_syshandle == 0) {
1779                         if (ct->ct_flags & CT7_TERMINATE) {
1780                                 isp_prt(isp, ISP_LOGINFO, "termination of 0x%x complete", ct->ct_rxid);
1781                         } else if ((ct->ct_flags & CT7_SENDSTATUS) == 0) {
1782                                 isp_prt(isp, pl, "intermediate CTIO completed ok");
1783                         } else {
1784                                 isp_prt(isp, pl, "unmonitored CTIO completed ok");
1785                         }
1786                 } else {
1787                         isp_prt(isp, pl, "NO xs for CTIO (handle 0x%x) status 0x%x", ct->ct_syshandle, ct->ct_nphdl);
1788                 }
1789         } else {
1790                 if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA) {
1791                         ISP_DMAFREE(isp, xs, ct->ct_syshandle);
1792                 }
1793                 if (ct->ct_flags & CT7_SENDSTATUS) {
1794                         /*
1795                          * Sent status and command complete.
1796                          *
1797                          * We're now really done with this command, so we
1798                          * punt to the platform dependent layers because
1799                          * only there can we do the appropriate command
1800                          * complete thread synchronization.
1801                          */
1802                         isp_prt(isp, pl, "status CTIO complete");
1803                 } else {
1804                         /*
1805                          * Final CTIO completed. Release DMA resources and
1806                          * notify platform dependent layers.
1807                          */
1808                         isp_prt(isp, pl, "data CTIO complete");
1809                 }
1810                 isp_async(isp, ISPASYNC_TARGET_ACTION, ct);
1811                 /*
1812                  * The platform layer will destroy the handle if appropriate.
1813                  */
1814         }
1815 }
1816
1817 static void
1818 isp_handle_24xx_inotify(ispsoftc_t *isp, in_fcentry_24xx_t *inot_24xx)
1819 {
1820         uint8_t ochan, chan, lochan, hichan;
1821
1822         /*
1823          * Check to see whether we got a wildcard channel.
1824          * If so, we have to iterate over all channels.
1825          */
1826         ochan = chan = ISP_GET_VPIDX(isp, inot_24xx->in_vpidx);
1827         if (chan == 0xff) {
1828                 lochan = 0;
1829                 hichan = isp->isp_nchan;
1830         } else {
1831                 if (chan >= isp->isp_nchan) {
1832                         char buf[64];
1833                         ISP_SNPRINTF(buf, sizeof buf, "%s: bad channel %d for status 0x%x", __func__, chan, inot_24xx->in_status);
1834                         isp_print_bytes(isp, buf, QENTRY_LEN, inot_24xx);
1835                         (void) isp_notify_ack(isp, inot_24xx);
1836                         return;
1837                 }
1838                 lochan = chan;
1839                 hichan = chan + 1;
1840         }
1841         isp_prt(isp, ISP_LOGTDEBUG1, "%s: Immediate Notify Channels %d..%d status=0x%x seqid=0x%x", __func__, lochan, hichan-1, inot_24xx->in_status, inot_24xx->in_rxid);
1842         for (chan = lochan; chan < hichan; chan++) {
1843                 switch (inot_24xx->in_status) {
1844                 case IN24XX_LIP_RESET:
1845                 case IN24XX_LINK_RESET:
1846                 case IN24XX_PORT_LOGOUT:
1847                 case IN24XX_PORT_CHANGED:
1848                 case IN24XX_LINK_FAILED:
1849                 case IN24XX_SRR_RCVD:
1850                 case IN24XX_ELS_RCVD:
1851                         inot_24xx->in_vpidx = chan;
1852                         isp_async(isp, ISPASYNC_TARGET_ACTION, inot_24xx);
1853                         break;
1854                 default:
1855                         isp_prt(isp, ISP_LOGINFO, "%s: unhandled status (0x%x) for chan %d", __func__, inot_24xx->in_status, chan);
1856                         (void) isp_notify_ack(isp, inot_24xx);
1857                         break;
1858                 }
1859         }
1860         inot_24xx->in_vpidx = ochan;
1861 }
1862 #endif