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