xref: /freebsd/sys/dev/pms/RefTisa/sat/src/smsatcb.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 /*******************************************************************************
2 *Copyright (c) 2014 PMC-Sierra, Inc.  All rights reserved.
3 *
4 *Redistribution and use in source and binary forms, with or without modification, are permitted provided
5 *that the following conditions are met:
6 *1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
7 *following disclaimer.
8 *2. Redistributions in binary form must reproduce the above copyright notice,
9 *this list of conditions and the following disclaimer in the documentation and/or other materials provided
10 *with the distribution.
11 *
12 *THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED
13 *WARRANTIES,INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 *FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
15 *FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
16 *NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
17 *BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
18 *LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
19 *SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
20 
21 ********************************************************************************/
22 #include <sys/cdefs.h>
23 __FBSDID("$FreeBSD$");
24 #include <dev/pms/config.h>
25 
26 #include <dev/pms/freebsd/driver/common/osenv.h>
27 #include <dev/pms/freebsd/driver/common/ostypes.h>
28 #include <dev/pms/freebsd/driver/common/osdebug.h>
29 
30 #include <dev/pms/RefTisa/tisa/api/titypes.h>
31 
32 #include <dev/pms/RefTisa/sallsdk/api/sa.h>
33 #include <dev/pms/RefTisa/sallsdk/api/saapi.h>
34 #include <dev/pms/RefTisa/sallsdk/api/saosapi.h>
35 
36 #include <dev/pms/RefTisa/sat/api/sm.h>
37 #include <dev/pms/RefTisa/sat/api/smapi.h>
38 #include <dev/pms/RefTisa/sat/api/tdsmapi.h>
39 
40 #include <dev/pms/RefTisa/sat/src/smdefs.h>
41 #include <dev/pms/RefTisa/sat/src/smproto.h>
42 #include <dev/pms/RefTisa/sat/src/smtypes.h>
43 
44 extern smRoot_t *gsmRoot;
45 
46 /******************************** completion ***********************************************************/
47 
48 FORCEINLINE void
49 smllSATACompleted(
50                   agsaRoot_t        *agRoot,
51                   agsaIORequest_t   *agIORequest,
52                   bit32             agIOStatus,
53                   void              *agFirstDword,
54                   bit32             agIOInfoLen,
55                   void              *agParam
56                  )
57 {
58   smRoot_t             *smRoot = agNULL;
59 //  smIntRoot_t          *smIntRoot = agNULL;
60 //  smIntContext_t       *smAllShared = agNULL;
61   smIORequestBody_t    *smIORequestBody;
62   smSatIOContext_t       *satIOContext;
63   smDeviceData_t       *pSatDevData;
64   smDeviceHandle_t     *smDeviceHandle = agNULL;
65   smDeviceData_t       *oneDeviceData = agNULL;
66 
67   SM_DBG2(("smllSATACompleted: start\n"));
68 
69   if (agIORequest == agNULL)
70   {
71     SM_DBG1(("smllSATACompleted: agIORequest is NULL!!!\n"));
72     return;
73   }
74 
75   smIORequestBody = (smIORequestBody_t *)agIORequest->osData;
76 
77   if (smIORequestBody == agNULL)
78   {
79     SM_DBG1(("smllSATACompleted: smIORequestBody is NULL!!!\n"));
80     return;
81   }
82 
83   /* for debugging */
84   if (smIORequestBody->ioCompleted == agTRUE)
85   {
86     smDeviceHandle = smIORequestBody->smDevHandle;
87     if (smDeviceHandle == agNULL)
88     {
89       SM_DBG1(("smllSATACompleted: smDeviceHandle is NULL!!!\n"));
90       return;
91     }
92     oneDeviceData  = (smDeviceData_t *)smDeviceHandle->smData;
93     SM_DBG1(("smllSATACompleted: Error!!!!!! double completion!!!, ID %d!!!\n", smIORequestBody->id));
94     if (oneDeviceData == agNULL)
95     {
96       SM_DBG1(("smllSATACompleted: oneDeviceData is NULL!!!\n"));
97       return;
98     }
99     SM_DBG1(("smllSATACompleted: did %d!!!\n", oneDeviceData->id));
100     return;
101   }
102 
103   smIORequestBody->ioCompleted = agTRUE;
104   satIOContext    = &(smIORequestBody->transport.SATA.satIOContext);
105 
106   if (satIOContext == agNULL)
107   {
108     SM_DBG1(("smllSATACompleted: satIOContext is NULL!!!\n"));
109     return;
110   }
111 
112   pSatDevData     = satIOContext->pSatDevData;
113 
114   if (pSatDevData == agNULL)
115   {
116     SM_DBG1(("smllSATACompleted: pSatDevData is NULL loc 1, wrong!!!\n"));
117     if (satIOContext->satIntIoContext == agNULL)
118     {
119       SM_DBG1(("smllSATACompleted: external command!!!\n"));
120     }
121     else
122     {
123       SM_DBG1(("smllSATACompleted: internal command!!!\n"));
124     }
125     return;
126   }
127 
128   smDeviceHandle = smIORequestBody->smDevHandle;
129 
130   if (smDeviceHandle == agNULL)
131   {
132     SM_DBG1(("smllSATACompleted: smDeviceHandle is NULL!!!!\n"));
133     return;
134   }
135 
136   oneDeviceData = (smDeviceData_t *)smDeviceHandle->smData;
137 
138   if (oneDeviceData != pSatDevData)
139   {
140     SM_DBG1(("smllSATACompleted: diff device handle!!!\n"));
141     if (satIOContext->satIntIoContext == agNULL)
142     {
143       SM_DBG1(("smllSATACompleted: external command!!!\n"));
144     }
145     else
146     {
147       SM_DBG1(("smllSATACompleted: internal command!!!\n"));
148     }
149     return;
150   }
151 
152   if (oneDeviceData == agNULL)
153   {
154     SM_DBG1(("smllSATACompleted: oneDeviceData is NULL!!!!\n"));
155     if (satIOContext->satIntIoContext == agNULL)
156     {
157       SM_DBG1(("smllSATACompleted: external command!!!\n"));
158     }
159     else
160     {
161       SM_DBG1(("smllSATACompleted: internal command!!!\n"));
162     }
163     return;
164   }
165 
166   smRoot = oneDeviceData->smRoot;
167 
168   /* release tag value for SATA */
169   if ( (satIOContext->reqType == AGSA_SATA_PROTOCOL_FPDMA_WRITE) ||
170        (satIOContext->reqType == AGSA_SATA_PROTOCOL_FPDMA_READ) )
171   {
172     smsatTagRelease(smRoot, pSatDevData, satIOContext->sataTag);
173     SM_DBG3(("smllSATACompleted: ncq tag 0x%x\n",satIOContext->sataTag));
174   }
175 
176   /* just for debugging */
177   if (agIOStatus == OSSA_IO_DS_NON_OPERATIONAL)
178   {
179     SM_DBG1(("smllSATACompleted: agIOStatus is OSSA_IO_DS_NON_OPERATIONAL!!!\n"));
180   }
181   if (agIOStatus == OSSA_IO_DS_IN_RECOVERY)
182   {
183     SM_DBG1(("smllSATACompleted: agIOStatus is OSSA_IO_DS_IN_RECOVERY!!!\n"));
184   }
185   if (agIOStatus == OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS)
186   {
187     SM_DBG1(("smllSATACompleted: agIOStatus is OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS!!!\n"));
188   }
189 
190   satIOContext->satCompleteCB( agRoot,
191                                agIORequest,
192                                agIOStatus,
193                                agFirstDword,
194                                agIOInfoLen,
195                                agParam,
196                                satIOContext);
197 
198 
199 
200   return;
201 }
202 /*****************************************************************************
203 *! \brief  smsatPacketCB
204 *
205 *   This routine is a callback function called from smllSATACompleted().
206 *   This CB routine deals with normal Packet command I/O SATA request.
207 *
208 *  \param   agRoot:      Handles for this instance of SAS/SATA hardware
209 *  \param   agIORequest: Pointer to the LL I/O request context for this I/O.
210 *  \param   agIOStatus:  Status of completed I/O.
211 *  \param   agFirstDword:Pointer to the four bytes of FIS.
212 *  \param   agIOInfoLen: Length in bytes of overrun/underrun residual or FIS
213 *                        length.
214 *  \param   agParam:     Additional info based on status.
215 *  \param   ioContext:   Pointer to smSatIOContext_t.
216 *
217 *  \return: none
218 *
219 *****************************************************************************/
220 
221 osGLOBAL void
222 smsatPacketCB(
223    agsaRoot_t        *agRoot,
224    agsaIORequest_t   *agIORequest,
225    bit32             agIOStatus,
226    agsaFisHeader_t   *agFirstDword,
227    bit32             agIOInfoLen,
228    void              *agParam,
229    void              *ioContext
230    )
231 {
232   smRoot_t                 *smRoot = agNULL;
233   smIntRoot_t              *smIntRoot = agNULL;
234   smIntContext_t           *smAllShared = agNULL;
235   smIORequestBody_t        *smIORequestBody;
236   smIORequestBody_t        *smOrgIORequestBody;
237   smSatIOContext_t         *satIOContext;
238   smSatIOContext_t         *satOrgIOContext;
239   smSatIOContext_t         *satNewIOContext;
240   smSatInternalIo_t        *satIntIo;
241   smSatInternalIo_t        *satNewIntIo = agNULL;
242   smDeviceData_t           *oneDeviceData;
243   bit32                     interruptContext;
244   smScsiRspSense_t         *pSense;
245   smIniScsiCmnd_t          *scsiCmnd;
246   smIORequest_t            *smOrgIORequest;
247 //  bit32                     ataStatus = 0;
248 //  bit32                     ataError;
249 
250   bit32                     status = SM_RC_SUCCESS;
251 //  agsaFisRegD2HHeader_t     *statDevToHostFisHeader = agNULL;
252 //  bit32                     dataLength;
253   bit8                      bSenseKey = 0;
254   bit16                     bSenseCodeInfo = 0;
255 
256   SM_DBG3(("smsatPacketCB: start\n"));
257   smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
258   satIOContext           = (smSatIOContext_t *) ioContext;
259   if (satIOContext == agNULL)
260   {
261     SM_DBG1(("smsatPacketCB: satIOContext is NULL\n"));
262     return;
263   }
264   satIntIo               = satIOContext->satIntIoContext;
265   oneDeviceData          = satIOContext->pSatDevData;
266   interruptContext       = satIOContext->interruptContext;
267   smRoot                 = oneDeviceData->smRoot;
268   smIntRoot              = (smIntRoot_t *)smRoot->smData;
269   smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
270 
271   if (satIntIo == agNULL)
272   {
273     SM_DBG5(("smsatPacketCB: External smSatInternalIo_t satIntIoContext\n"));
274     satOrgIOContext = satIOContext;
275     smOrgIORequest  = smIORequestBody->smIORequest;
276     pSense          = satIOContext->pSense;
277     scsiCmnd        = satIOContext->pScsiCmnd;
278   }
279   else
280   {
281     SM_DBG5(("smsatPacketCB: Internal smSatInternalIo_t satIntIoContext\n"));
282     satOrgIOContext        = satIOContext->satOrgIOContext;
283     smOrgIORequestBody     = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
284     smOrgIORequest         = (smIORequest_t *)smOrgIORequestBody->smIORequest;
285     pSense        = satOrgIOContext->pSense;
286     scsiCmnd      = satOrgIOContext->pScsiCmnd;
287   }
288 
289   smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
290 
291   smIORequestBody->ioCompleted = agTRUE;
292   smIORequestBody->ioStarted   = agFALSE;
293 
294   /* interal structure free */
295   smsatFreeIntIoResource( smRoot, oneDeviceData, satIntIo);
296   if( agIOStatus == OSSA_IO_SUCCESS && agIOInfoLen == 0 && agFirstDword == agNULL)
297   {
298     SM_DBG3(("smsatPacketCB: First, agIOStatus == OSSA_IO_SUCCESS, agFirstDword == agNULL, agIOInfoLen = %d\n", agIOInfoLen));
299     tdsmIOCompletedCB(smRoot,
300                       smOrgIORequest,
301                       smIOSuccess,
302                       SCSI_STAT_GOOD,
303                       agNULL,
304                       interruptContext);
305   }
306   else if (agIOStatus == OSSA_IO_SUCCESS &&  !(agIOInfoLen == 0 && agFirstDword == agNULL))
307   {
308       SM_DBG2(("smsatPacketCB: Second, agIOStatus == OSSA_IO_SUCCESS , agFirstDword %p agIOInfoLen = %d\n", agFirstDword, agIOInfoLen));
309       /*The SCSI command status is error, need to send REQUEST SENSE for getting more sense information*/
310       satNewIntIo = smsatAllocIntIoResource( smRoot,
311                                        smOrgIORequest,
312                                        oneDeviceData,
313                                        SENSE_DATA_LENGTH,
314                                        satNewIntIo);
315       if (satNewIntIo == agNULL)
316       {
317           /* memory allocation failure */
318           /* just translate the ATAPI error register to sense information */
319           smsatTranslateATAPIErrorsToSCSIErrors(
320                           scsiCmnd->cdb[0],
321                           agFirstDword->D2H.status,
322                           agFirstDword->D2H.error,
323                           &bSenseKey,
324                           &bSenseCodeInfo
325                           );
326           smsatSetSensePayload(pSense, bSenseKey, 0, bSenseCodeInfo, satOrgIOContext);
327           tdsmIOCompletedCB( smRoot,
328                              smOrgIORequest,
329                              smIOSuccess,
330                              SCSI_STAT_CHECK_CONDITION,
331                              satOrgIOContext->pSmSenseData,
332                              interruptContext);
333           SM_DBG1(("smsatPacketCB: momory allocation fails\n"));
334           return;
335       } /* end memory allocation */
336 
337       satNewIOContext = smsatPrepareNewIO(satNewIntIo,
338                                         smOrgIORequest,
339                                         oneDeviceData,
340                                         scsiCmnd,
341                                         satOrgIOContext
342                                         );
343       /* sends request sense to ATAPI device for acquiring sense information */
344       status = smsatRequestSenseForATAPI(smRoot,
345                               &satNewIntIo->satIntSmIORequest,
346                               satNewIOContext->psmDeviceHandle,
347                               &satNewIntIo->satIntSmScsiXchg,
348                               satNewIOContext
349                               );
350       if (status != SM_RC_SUCCESS)
351       {
352           smsatFreeIntIoResource( smRoot,
353                                 oneDeviceData,
354                                 satNewIntIo);
355           /* just translate the ATAPI error register to sense information */
356           smsatTranslateATAPIErrorsToSCSIErrors(
357                           scsiCmnd->cdb[0],
358                           agFirstDword->D2H.status,
359                           agFirstDword->D2H.error,
360                           &bSenseKey,
361                           &bSenseCodeInfo
362                           );
363           smsatSetSensePayload(pSense, bSenseKey, 0, bSenseCodeInfo, satOrgIOContext);
364           tdsmIOCompletedCB(smRoot,
365                             smOrgIORequest,
366                             smIOSuccess,
367                             SCSI_STAT_CHECK_CONDITION,
368                             satOrgIOContext->pSmSenseData,
369                             interruptContext);
370           SM_DBG1(("smsatPacketCB: failed to call satRequestSenseForATAPI()\n"));
371       }
372   }
373   else if (agIOStatus != OSSA_IO_SUCCESS )
374   {
375       SM_DBG2(("smsatPacketCB: agIOStatus != OSSA_IO_SUCCESS, status %d\n", agIOStatus));
376       smsatProcessAbnormalCompletion(
377                     agRoot,
378                     agIORequest,
379                     agIOStatus,
380                     agFirstDword,
381                     agIOInfoLen,
382                     agParam,
383                     satIOContext);
384   }
385   else
386   {
387     SM_DBG1(("smsatPacketCB: Unknown error \n"));
388     tdsmIOCompletedCB(smRoot,
389                       smOrgIORequest,
390                       smIOFailed,
391                       smDetailOtherError,
392                       agNULL,
393                       interruptContext);
394   }
395 }
396 /*****************************************************************************
397 *! \brief  smsatRequestSenseForATAPICB
398 *
399 *   This routine is a callback function called from smllSATACompleted().
400 *   This CB routine deals with normal non-chained data I/O SATA request.
401 *
402 *  \param   agRoot:      Handles for this instance of SAS/SATA hardware
403 *  \param   agIORequest: Pointer to the LL I/O request context for this I/O.
404 *  \param   agIOStatus:  Status of completed I/O.
405 *  \param   agFirstDword:Pointer to the four bytes of FIS.
406 *  \param   agIOInfoLen: Length in bytes of overrun/underrun residual or FIS
407 *                        length.
408 *  \param   agParam:     Additional info based on status.
409 *  \param   ioContext:   Pointer to smSatIOContext_t.
410 *
411 *  \return: none
412 *
413 *****************************************************************************/
414 osGLOBAL void
415 smsatRequestSenseForATAPICB(
416    agsaRoot_t        *agRoot,
417    agsaIORequest_t   *agIORequest,
418    bit32             agIOStatus,
419    agsaFisHeader_t   *agFirstDword,
420    bit32             agIOInfoLen,
421    void              *agParam,
422    void              *ioContext
423    )
424 {
425    smRoot_t                  *smRoot = agNULL;
426    smIntRoot_t               *smIntRoot = agNULL;
427    smIntContext_t            *smAllShared = agNULL;
428    smIORequestBody_t         *smIORequestBody;
429    smIORequestBody_t         *smOrgIORequestBody;
430    smSatIOContext_t            *satIOContext;
431    smSatIOContext_t            *satOrgIOContext;
432 //   smSatIOContext_t            *satNewIOContext;
433    smSatInternalIo_t           *satIntIo;
434 //   smSatInternalIo_t           *satNewIntIo = agNULL;
435    smDeviceData_t            *oneDeviceData;
436    bit32                     interruptContext;
437    bit8                      dataLength;
438    smIniScsiCmnd_t           *scsiCmnd;
439    smIORequest_t             *smOrgIORequest;
440 
441    SM_DBG3(("smsatRequestSenseForATAPICB: start\n"));
442    smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
443    satIOContext           = (smSatIOContext_t *) ioContext;
444    if (satIOContext == agNULL)
445    {
446      SM_DBG1(("smsatRequestSenseForATAPICB: satIOContext is NULL\n"));
447      return;
448    }
449    satIntIo               = satIOContext->satIntIoContext;
450    oneDeviceData          = satIOContext->pSatDevData;
451    interruptContext = satIOContext->interruptContext;
452    smRoot                 = oneDeviceData->smRoot;
453    smIntRoot              = (smIntRoot_t *)smRoot->smData;
454    smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
455    if (satIntIo == agNULL)
456    {
457      SM_DBG5(("smsatRequestSenseForATAPICB: External smSatInternalIo_t satIntIoContext\n"));
458      satOrgIOContext = satIOContext;
459      smOrgIORequest  = smIORequestBody->smIORequest;
460      scsiCmnd        = satIOContext->pScsiCmnd;
461    }
462    else
463    {
464      SM_DBG5(("smsatRequestSenseForATAPICB: Internal smSatInternalIo_t satIntIoContext\n"));
465      satOrgIOContext        = satIOContext->satOrgIOContext;
466      smOrgIORequestBody     = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
467      smOrgIORequest         = (smIORequest_t *)smOrgIORequestBody->smIORequest;
468      scsiCmnd      = satOrgIOContext->pScsiCmnd;
469    }
470 
471    smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
472    smIORequestBody->ioCompleted = agTRUE;
473    smIORequestBody->ioStarted   = agFALSE;
474    if ( (agIOStatus == OSSA_IO_SUCCESS && agIOInfoLen == 0 && agFirstDword == agNULL))
475    {
476       /* copy the request sense buffer to original IO buffer*/
477       if (satIntIo)
478       {
479         sm_memcpy(satOrgIOContext->pSmSenseData->senseData, satIntIo->satIntDmaMem.virtPtr, SENSE_DATA_LENGTH);
480       }
481       satOrgIOContext->pSmSenseData->senseLen = SENSE_DATA_LENGTH;
482       /* interal structure free */
483       smsatFreeIntIoResource( smRoot, oneDeviceData, satIntIo);
484 
485       /* notify the OS to complete this SRB */
486       tdsmIOCompletedCB( smRoot,
487                   smOrgIORequest,
488                   smIOSuccess,
489                   SCSI_STAT_CHECK_CONDITION,
490                   satOrgIOContext->pSmSenseData,
491                   interruptContext);
492    }
493    else if (agIOStatus == OSSA_IO_UNDERFLOW )
494    {
495       /* copy the request sense buffer to original IO buffer*/
496       SM_DBG1(("smsatRequestSenseForATAPICB: OSSA_IO_UNDERFLOW agIOInfoLen = %d\n", agIOInfoLen));
497       dataLength = (bit8)(scsiCmnd->expDataLength - agIOInfoLen);
498       if (satIntIo)
499       {
500         sm_memcpy(satOrgIOContext->pSmSenseData->senseData, satIntIo->satIntDmaMem.virtPtr, dataLength);
501       }
502       satOrgIOContext->pSmSenseData->senseLen = dataLength;
503       /* interal structure free */
504       smsatFreeIntIoResource( smRoot, oneDeviceData, satIntIo);
505 
506       /* notify the OS to complete this SRB */
507       tdsmIOCompletedCB( smRoot,
508                   smOrgIORequest,
509                   smIOSuccess,
510                   SCSI_STAT_CHECK_CONDITION,
511                   satOrgIOContext->pSmSenseData,
512                   interruptContext);
513    }
514    else
515    {
516       SM_DBG1(("smsatRequestSenseForATAPICB: failed, agIOStatus error = 0x%x agIOInfoLen = %d\n", agIOStatus, agIOInfoLen));
517       /* interal structure free */
518       smsatFreeIntIoResource( smRoot, oneDeviceData, satIntIo);
519 
520       /* notify the OS to complete this SRB */
521       tdsmIOCompletedCB( smRoot,
522                   smOrgIORequest,
523                   smIOFailed,
524                   smDetailOtherError,
525                   agNULL,
526                   interruptContext);
527    }
528    SM_DBG3(("smsatRequestSenseForATAPICB: end\n"));
529 }
530 
531 /*****************************************************************************
532 *! \brief  smsatSetFeaturesPIOCB
533 *
534 *   This routine is a callback function called from smllSATACompleted().
535 *   This CB routine deals with normal non-chained data I/O SATA request.
536 *
537 *  \param   agRoot:      Handles for this instance of SAS/SATA hardware
538 *  \param   agIORequest: Pointer to the LL I/O request context for this I/O.
539 *  \param   agIOStatus:  Status of completed I/O.
540 *  \param   agFirstDword:Pointer to the four bytes of FIS.
541 *  \param   agIOInfoLen: Length in bytes of overrun/underrun residual or FIS
542 *                        length.
543 *  \param   agParam:     Additional info based on status.
544 *  \param   ioContext:   Pointer to smSatIOContext_t.
545 *
546 *  \return: none
547 *
548 *****************************************************************************/
549 osGLOBAL void
550 smsatSetFeaturesPIOCB(
551     agsaRoot_t        *agRoot,
552     agsaIORequest_t   *agIORequest,
553     bit32             agIOStatus,
554     agsaFisHeader_t   *agFirstDword,
555     bit32             agIOInfoLen,
556     void              *agParam,
557     void              *ioContext
558     )
559 {
560     smRoot_t                  *smRoot = agNULL;
561     smIntRoot_t               *smIntRoot = agNULL;
562     smIntContext_t            *smAllShared = agNULL;
563     smIORequestBody_t         *smIORequestBody;
564     smIORequestBody_t         *smOrgIORequestBody = agNULL;
565     smSatIOContext_t          *satIOContext;
566     smSatIOContext_t          *satOrgIOContext;
567     smSatIOContext_t          *satNewIOContext;
568     smSatInternalIo_t         *satIntIo;
569     smSatInternalIo_t         *satNewIntIo = agNULL;
570     smDeviceData_t            *oneDeviceData;
571     smIniScsiCmnd_t           *scsiCmnd;
572     smIORequest_t             *smOrgIORequest;
573     smDeviceHandle_t          *smDeviceHandle;
574     bit32                      status = SM_RC_FAILURE;
575     smIORequest_t             *smIORequest;
576 
577     SM_DBG2(("smsatSetFeaturesPIOCB: start\n"));
578     smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
579     satIOContext           = (smSatIOContext_t *) ioContext;
580     if (satIOContext == agNULL)
581     {
582       SM_DBG1(("smsatSetFeaturesPIOCB: satIOContext is NULL\n"));
583       return;
584     }
585     satIntIo               = satIOContext->satIntIoContext;
586     oneDeviceData          = satIOContext->pSatDevData;
587     smDeviceHandle         = satIOContext->psmDeviceHandle;
588     smRoot                 = oneDeviceData->smRoot;
589     smIntRoot              = (smIntRoot_t *)smRoot->smData;
590     smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
591     if (satIntIo == agNULL)
592     {
593       SM_DBG2(("smsatSetFeaturesPIOCB: External smSatInternalIo_t satIntIoContext\n"));
594       satOrgIOContext = satIOContext;
595       smOrgIORequest  = smIORequestBody->smIORequest;
596       scsiCmnd        = satIOContext->pScsiCmnd;
597     }
598     else
599     {
600       SM_DBG2(("smsatSetFeaturesPIOCB: Internal smSatInternalIo_t satIntIoContext\n"));
601       satOrgIOContext        = satIOContext->satOrgIOContext;
602       smOrgIORequestBody     = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
603       smOrgIORequest         = (smIORequest_t *)smOrgIORequestBody->smIORequest;
604       scsiCmnd      = satOrgIOContext->pScsiCmnd;
605     }
606     smIORequest  = smOrgIORequestBody->smIORequest;
607     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
608     smIORequestBody->ioCompleted = agTRUE;
609     smIORequestBody->ioStarted   = agFALSE;
610     /* interal structure free */
611     smsatFreeIntIoResource(smRoot,
612                            oneDeviceData,
613                            satIntIo);
614     if (smIORequest->tdData == smIORequest->smData)
615     {
616       SM_DBG1(("smsatSetFeaturesPIOCB: the same tdData and smData error!\n"));
617     }
618     /* check the agIOStatus */
619     if (agIOStatus == OSSA_IO_ABORTED ||
620         agIOStatus == OSSA_IO_NO_DEVICE ||
621         agIOStatus == OSSA_IO_PORT_IN_RESET ||
622         agIOStatus == OSSA_IO_DS_NON_OPERATIONAL ||
623         agIOStatus == OSSA_IO_DS_IN_RECOVERY ||
624         agIOStatus == OSSA_IO_DS_IN_ERROR ||
625         agIOStatus == OSSA_IO_DS_INVALID
626        )
627     {
628       SM_DBG1(("smsatSetFeaturesPIOCB: error status 0x%x\n", agIOStatus));
629       SM_DBG1(("smsatSetFeaturesPIOCB: did %d!!!\n", oneDeviceData->id));
630       tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIOSuccess, &(oneDeviceData->satIdentifyData));
631       return;
632     }
633     /*if the ATAPI device support DMA, then enble this feature*/
634     if (oneDeviceData->satDMASupport)
635     {
636         satNewIntIo = smsatAllocIntIoResource(smRoot,
637                                            smOrgIORequest,
638                                            oneDeviceData,
639                                            0,
640                                            satNewIntIo);
641         if (satNewIntIo == agNULL)
642         {
643             SM_DBG1(("smsatSetFeaturesPIOCB: memory allocation fails\n"));
644             /*Complete this identify packet device IO */
645             tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIOSuccess, &(oneDeviceData->satIdentifyData));
646             return;
647         } /* end memory allocation */
648 
649         satNewIOContext = smsatPrepareNewIO(satNewIntIo,
650                                           smOrgIORequest,
651                                           oneDeviceData,
652                                           scsiCmnd,
653                                           satOrgIOContext
654                                           );
655         /* sends another ATA SET FEATURES based on DMA bit */
656         status = smsatSetFeaturesDMA(smRoot,
657                                 &satNewIntIo->satIntSmIORequest,
658                                 satNewIOContext->psmDeviceHandle,
659                                 &satNewIntIo->satIntSmScsiXchg,
660                                 satNewIOContext
661                                 );
662         if (status != SM_RC_SUCCESS)
663         {
664             smsatFreeIntIoResource(smRoot, oneDeviceData, satNewIntIo);
665             SM_DBG2(("satSetFeaturesPIOCB: failed to call smsatSetFeatures()\n"));
666             /*Complete this identify packet device IO */
667             tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIOSuccess, &(oneDeviceData->satIdentifyData));
668         }
669     }
670     else
671     {
672         /*Complete this identify packet device IO */
673         tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIOSuccess, &(oneDeviceData->satIdentifyData));
674     }
675     SM_DBG2(("smsatSetFeaturesPIOCB: exit, agIOStatus 0x%x\n", agIOStatus));
676 }
677 
678 /*****************************************************************************
679 *! \brief  smsatDeviceResetCB
680 *
681 *   This routine is a callback function called from smllSATACompleted().
682 *   This CB routine deals with normal non-chained data I/O SATA request.
683 *
684 *  \param   agRoot:      Handles for this instance of SAS/SATA hardware
685 *  \param   agIORequest: Pointer to the LL I/O request context for this I/O.
686 *  \param   agIOStatus:  Status of completed I/O.
687 *  \param   agFirstDword:Pointer to the four bytes of FIS.
688 *  \param   agIOInfoLen: Length in bytes of overrun/underrun residual or FIS
689 *                        length.
690 *  \param   agParam:     Additional info based on status.
691 *  \param   ioContext:   Pointer to smSatIOContext_t.
692 *
693 *  \return: none
694 *
695 *****************************************************************************/
696 osGLOBAL void
697 smsatDeviceResetCB(
698     agsaRoot_t        *agRoot,
699     agsaIORequest_t   *agIORequest,
700     bit32             agIOStatus,
701     agsaFisHeader_t   *agFirstDword,
702     bit32             agIOInfoLen,
703     void              *agParam,
704     void              *ioContext
705     )
706 {
707     smRoot_t                 *smRoot = agNULL;
708     smIntRoot_t              *smIntRoot = agNULL;
709     smIntContext_t           *smAllShared = agNULL;
710     smIORequestBody_t        *smIORequestBody;
711     smSatIOContext_t         *satIOContext;
712     smSatIOContext_t         *satOrgIOContext;
713 //    smSatIOContext_t          *satNewIOContext;
714     smSatInternalIo_t        *satIntIo;
715 //    smSatInternalIo_t         *satNewIntIo = agNULL;
716     smDeviceData_t           *oneDeviceData;
717 #ifdef  TD_DEBUG_ENABLE
718     agsaFisPioSetupHeader_t  *satPIOSetupHeader = agNULL;
719     bit32                     ataStatus = 0;
720     bit32                     ataError;
721 #endif
722 //    bit32                     status;
723     bit32                     AbortTM = agFALSE;
724     smDeviceHandle_t         *smDeviceHandle;
725 
726     SM_DBG1(("smsatDeviceResetCB: start\n"));
727     smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
728     satIOContext           = (smSatIOContext_t *) ioContext;
729     satIntIo               = satIOContext->satIntIoContext;
730     oneDeviceData          = satIOContext->pSatDevData;
731     smRoot                 = oneDeviceData->smRoot;
732     smIntRoot              = (smIntRoot_t *)smRoot->smData;
733     smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
734     smDeviceHandle         = oneDeviceData->smDevHandle;
735     if (satIntIo == agNULL)
736     {
737       SM_DBG6(("smsatDeviceResetCB: External, OS generated\n"));
738       satOrgIOContext      = satIOContext;
739     }
740     else
741     {
742       SM_DBG6(("smsatDeviceResetCB: Internal, TD generated\n"));
743       satOrgIOContext        = satIOContext->satOrgIOContext;
744       if (satOrgIOContext == agNULL)
745       {
746         SM_DBG6(("smsatDeviceResetCB: satOrgIOContext is NULL, wrong\n"));
747         return;
748       }
749       else
750       {
751         SM_DBG6(("smsatDeviceResetCB: satOrgIOContext is NOT NULL\n"));
752       }
753     }
754     smIORequestBody->ioCompleted = agTRUE;
755     smIORequestBody->ioStarted = agFALSE;
756     if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
757     {
758       SM_DBG1(("smsatDeviceResetCB: wrong. agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
759       /* TM completed */
760       tdsmEventCB( smRoot,
761                    smDeviceHandle,
762                    smIntrEventTypeTaskManagement,
763                    smTMFailed,
764                    oneDeviceData->satTmTaskTag);
765       oneDeviceData->satTmTaskTag = agNULL;
766       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
767       smsatFreeIntIoResource( smRoot,
768                               oneDeviceData,
769                               satIntIo);
770       return;
771     }
772     if (agIOStatus == OSSA_IO_OPEN_CNX_ERROR_PROTOCOL_NOT_SUPPORTED ||
773         agIOStatus == OSSA_IO_OPEN_CNX_ERROR_ZONE_VIOLATION ||
774         agIOStatus == OSSA_IO_OPEN_CNX_ERROR_BREAK ||
775         agIOStatus == OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS ||
776         agIOStatus == OSSA_IO_OPEN_CNX_ERROR_BAD_DESTINATION ||
777         agIOStatus == OSSA_IO_OPEN_CNX_ERROR_CONNECTION_RATE_NOT_SUPPORTED ||
778         agIOStatus == OSSA_IO_OPEN_CNX_ERROR_WRONG_DESTINATION ||
779         agIOStatus == OSSA_IO_OPEN_CNX_ERROR_UNKNOWN_ERROR ||
780         agIOStatus == OSSA_IO_OPEN_CNX_ERROR_STP_RESOURCES_BUSY
781         )
782     {
783       SM_DBG1(("smsatDeviceResetCB: OSSA_IO_OPEN_CNX_ERROR!!!\n"));
784       /* TM completed */
785       tdsmEventCB( smRoot,
786                    smDeviceHandle,
787                    smIntrEventTypeTaskManagement,
788                    smTMFailed,
789                    oneDeviceData->satTmTaskTag);
790       oneDeviceData->satTmTaskTag = agNULL;
791       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
792       smsatFreeIntIoResource( smRoot,
793                               oneDeviceData,
794                               satIntIo);
795       return;
796     }
797     if (agIOStatus != OSSA_IO_SUCCESS)
798     {
799 #ifdef  TD_DEBUG_ENABLE
800        /* only agsaFisPioSetup_t is expected */
801        satPIOSetupHeader = (agsaFisPioSetupHeader_t *)&(agFirstDword->PioSetup);
802        ataStatus     = satPIOSetupHeader->status;   /* ATA Status register */
803        ataError      = satPIOSetupHeader->error;    /* ATA Eror register   */
804 #endif
805        SM_DBG1(("smsatDeviceResetCB: ataStatus 0x%x ataError 0x%x!!!\n", ataStatus, ataError));
806        /* TM completed */
807        tdsmEventCB( smRoot,
808                     smDeviceHandle,
809                     smIntrEventTypeTaskManagement,
810                     smTMFailed,
811                     oneDeviceData->satTmTaskTag);
812        oneDeviceData->satTmTaskTag = agNULL;
813        smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
814        smsatFreeIntIoResource( smRoot,
815                                oneDeviceData,
816                                satIntIo);
817        return;
818     }
819     /*success */
820     if (satOrgIOContext->TMF == AG_ABORT_TASK)
821     {
822       AbortTM = agTRUE;
823     }
824     if (AbortTM == agTRUE)
825     {
826       SM_DBG1(("smsatDeviceResetCB: calling satAbort!!!\n"));
827       smsatAbort(smRoot, agRoot, satOrgIOContext->satToBeAbortedIOContext);
828     }
829     oneDeviceData->satTmTaskTag = agNULL;
830     oneDeviceData->satDriveState = SAT_DEV_STATE_NORMAL;
831     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
832     SM_DBG1(("smsatDeviceResetCB: satPendingIO %d satNCQMaxIO %d!!!\n", oneDeviceData->satPendingIO, oneDeviceData->satNCQMaxIO ));
833     SM_DBG1(("smsatDeviceResetCB: satPendingNCQIO %d satPendingNONNCQIO %d!!!\n", oneDeviceData->satPendingNCQIO, oneDeviceData->satPendingNONNCQIO));
834     smsatFreeIntIoResource( smRoot,
835                             oneDeviceData,
836                             satIntIo);
837     /* TM completed */
838     tdsmEventCB( smRoot,
839                  smDeviceHandle,
840                  smIntrEventTypeTaskManagement,
841                  smTMOK,
842                  oneDeviceData->satTmTaskTag);
843 
844 
845     SM_DBG3(("smsatDeviceResetCB: return\n"));
846 }
847 
848 
849 /*****************************************************************************
850 *! \brief  smsatExecuteDeviceDiagnosticCB
851 *
852 *   This routine is a callback function called from smllSATACompleted().
853 *   This CB routine deals with normal non-chained data I/O SATA request.
854 *
855 *  \param   agRoot:      Handles for this instance of SAS/SATA hardware
856 *  \param   agIORequest: Pointer to the LL I/O request context for this I/O.
857 *  \param   agIOStatus:  Status of completed I/O.
858 *  \param   agFirstDword:Pointer to the four bytes of FIS.
859 *  \param   agIOInfoLen: Length in bytes of overrun/underrun residual or FIS
860 *                        length.
861 *  \param   agParam:     Additional info based on status.
862 *  \param   ioContext:   Pointer to smSatIOContext_t.
863 *
864 *  \return: none
865 *
866 *****************************************************************************/
867 osGLOBAL void
868 smsatExecuteDeviceDiagnosticCB(
869     agsaRoot_t        *agRoot,
870     agsaIORequest_t   *agIORequest,
871     bit32             agIOStatus,
872     agsaFisHeader_t   *agFirstDword,
873     bit32             agIOInfoLen,
874     void              *agParam,
875     void              *ioContext
876     )
877 {
878     smRoot_t                  *smRoot = agNULL;
879     smIntRoot_t               *smIntRoot = agNULL;
880     smIntContext_t            *smAllShared = agNULL;
881     smIORequestBody_t         *smIORequestBody;
882     smSatIOContext_t          *satIOContext;
883     smSatIOContext_t          *satOrgIOContext;
884 //    smSatIOContext_t            *satNewIOContext;
885     smSatInternalIo_t         *satIntIo;
886 //    smSatInternalIo_t           *satNewIntIo = agNULL;
887     smDeviceData_t            *oneDeviceData;
888 
889     SM_DBG6(("smsatSetFeaturesDMACB: start\n"));
890     smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
891     satIOContext           = (smSatIOContext_t *) ioContext;
892     satIntIo               = satIOContext->satIntIoContext;
893     oneDeviceData          = satIOContext->pSatDevData;
894     smRoot                 = oneDeviceData->smRoot;
895     smIntRoot              = (smIntRoot_t *)smRoot->smData;
896     smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
897     if (satIntIo == agNULL)
898     {
899       SM_DBG5(("smsatExecuteDeviceDiagnosticCB: External smSatInternalIo_t satIntIoContext\n"));
900       satOrgIOContext = satIOContext;
901     }
902     else
903     {
904       SM_DBG5(("smsatExecuteDeviceDiagnosticCB: Internal smSatInternalIo_t satIntIoContext\n"));
905       satOrgIOContext        = satIOContext->satOrgIOContext;
906       if (satOrgIOContext == agNULL)
907       {
908         SM_DBG5(("smsatExecuteDeviceDiagnosticCB: satOrgIOContext is NULL\n"));
909       }
910       else
911       {
912         SM_DBG5(("smsatExecuteDeviceDiagnosticCB: satOrgIOContext is NOT NULL\n"));
913       }
914     }
915     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
916     smIORequestBody->ioCompleted = agTRUE;
917     smIORequestBody->ioStarted   = agFALSE;
918      /* interal structure free */
919     smsatFreeIntIoResource(smRoot,oneDeviceData, satIntIo);
920 }
921 
922 GLOBAL void
923 smsatTranslateATAPIErrorsToSCSIErrors(
924     bit8   bCommand,
925     bit8   bATAStatus,
926     bit8   bATAError,
927     bit8   *pSenseKey,
928     bit16  *pSenseCodeInfo
929     )
930 {
931     if (pSenseKey == agNULL || pSenseCodeInfo == agNULL)
932     {
933         SM_DBG1(("TranslateATAErrorsToSCSIErros: pSenseKey == agNULL || pSenseCodeInfo == agNULL\n"));
934         return;
935     }
936     if (bATAStatus & ERR_ATA_STATUS_MASK )
937     {
938         if(bATAError & NM_ATA_ERROR_MASK)
939         {
940           *pSenseKey = SCSI_SNSKEY_NOT_READY;
941           *pSenseCodeInfo = 0x3a00;
942         }
943         else if(bATAError & ABRT_ATA_ERROR_MASK)
944         {
945           *pSenseKey = SCSI_SNSKEY_ABORTED_COMMAND;
946           *pSenseCodeInfo = 0;
947         }
948         else if(bATAError & MCR_ATA_ERROR_MASK)
949         {
950           *pSenseKey = SCSI_SNSKEY_UNIT_ATTENTION;
951           *pSenseCodeInfo = 0x5a01;
952         }
953         else if(bATAError & IDNF_ATA_ERROR_MASK)
954         {
955           *pSenseKey = SCSI_SNSKEY_MEDIUM_ERROR;
956           *pSenseCodeInfo = 0x1401;
957         }
958         else if(bATAError & MC_ATA_ERROR_MASK)
959         {
960           *pSenseKey = SCSI_SNSKEY_UNIT_ATTENTION;
961           *pSenseCodeInfo = 0x2800;
962         }
963         else if(bATAError & UNC_ATA_ERROR_MASK)
964         {
965           /*READ*/
966           *pSenseKey = SCSI_SNSKEY_MEDIUM_ERROR;
967           *pSenseCodeInfo = 0x1100;
968 
969           /*add WRITE here */
970         }
971         else if(bATAError & ICRC_ATA_ERROR_MASK)
972         {
973           *pSenseKey = SCSI_SNSKEY_ABORTED_COMMAND;
974           *pSenseCodeInfo = 0x4703;
975         }
976     }
977     else if((bATAStatus & DF_ATA_STATUS_MASK))
978     {
979         *pSenseKey = SCSI_SNSKEY_HARDWARE_ERROR;
980         *pSenseCodeInfo = 0x4400;
981     }
982     else
983     {
984         SM_DBG1(("unhandled ata error: bATAStatus = 0x%x, bATAError = 0x%x\n", bATAStatus, bATAError));
985     }
986 }
987 
988 GLOBAL void
989 smsatTranslateATAErrorsToSCSIErrors(
990     bit8   bATAStatus,
991     bit8   bATAError,
992     bit8   *pSenseKey,
993     bit16  *pSenseCodeInfo
994     )
995 {
996 
997   SM_DBG1(("TranslateATAErrorsToSCSIErros: bATAStatus=%d  bATAError= %d \n",bATAStatus,bATAError));
998 
999   if (pSenseKey == agNULL || pSenseCodeInfo == agNULL)
1000   {
1001     SM_DBG1(("TranslateATAErrorsToSCSIErros: pSenseKey == agNULL || pSenseCodeInfo == agNULL\n"));
1002     return;
1003   }
1004 
1005   if (bATAStatus & ERR_ATA_STATUS_MASK)
1006   {
1007     if(bATAError & NM_ATA_ERROR_MASK)
1008     {
1009       *pSenseKey = SCSI_SNSKEY_NOT_READY;
1010       *pSenseCodeInfo = SCSI_SNSCODE_MEDIUM_NOT_PRESENT;
1011     }
1012     else if(bATAError & UNC_ATA_ERROR_MASK)
1013     {
1014       *pSenseKey = SCSI_SNSKEY_MEDIUM_ERROR;
1015       *pSenseCodeInfo = SCSI_SNSCODE_UNRECOVERED_READ_ERROR;
1016     }
1017     else if(bATAError & IDNF_ATA_ERROR_MASK)
1018     {
1019       *pSenseKey = SCSI_SNSKEY_ILLEGAL_REQUEST;
1020       *pSenseCodeInfo = SCSI_SNSCODE_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1021     }
1022     else if(bATAError & ABRT_ATA_ERROR_MASK)
1023     {
1024       *pSenseKey = SCSI_SNSKEY_ABORTED_COMMAND;
1025       *pSenseCodeInfo = SCSI_SNSCODE_NO_ADDITIONAL_INFO;
1026     }
1027     else if(bATAError & MC_ATA_ERROR_MASK)
1028     {
1029       *pSenseKey = SCSI_SNSKEY_UNIT_ATTENTION;
1030       *pSenseCodeInfo = SCSI_SNSCODE_NOT_READY_TO_READY_CHANGE;
1031     }
1032     else if(bATAError & MCR_ATA_ERROR_MASK)
1033     {
1034       *pSenseKey = SCSI_SNSKEY_UNIT_ATTENTION;
1035       *pSenseCodeInfo = SCSI_SNSCODE_OPERATOR_MEDIUM_REMOVAL_REQUEST;
1036     }
1037     else if(bATAError & ICRC_ATA_ERROR_MASK)
1038     {
1039       *pSenseKey = SCSI_SNSKEY_ABORTED_COMMAND;
1040       *pSenseCodeInfo = SCSI_SNSCODE_INFORMATION_UNIT_CRC_ERROR;
1041     }
1042     else
1043     {
1044       *pSenseKey = SCSI_SNSKEY_NO_SENSE;
1045       *pSenseCodeInfo = SCSI_SNSCODE_NO_ADDITIONAL_INFO;
1046 
1047     }
1048   }
1049   else if (bATAStatus & DF_ATA_STATUS_MASK) /* INTERNAL TARGET FAILURE */
1050   {
1051     *pSenseKey = SCSI_SNSKEY_HARDWARE_ERROR;
1052     *pSenseCodeInfo = SCSI_SNSCODE_INTERNAL_TARGET_FAILURE;
1053   }
1054 
1055 
1056 }
1057 
1058 
1059 FORCEINLINE void
1060 smsatNonChainedDataIOCB(
1061                         agsaRoot_t        *agRoot,
1062                         agsaIORequest_t   *agIORequest,
1063                         bit32             agIOStatus,
1064                         agsaFisHeader_t   *agFirstDword,
1065                         bit32             agIOInfoLen,
1066                         void              *agParam,
1067                         void              *ioContext
1068                        )
1069 {
1070   smIORequestBody_t    *smIORequestBody = (smIORequestBody_t *)agIORequest->osData;
1071   smSatIOContext_t       *satIOContext    = (smSatIOContext_t *) ioContext;
1072   smSatInternalIo_t      *SatIntIo        = satIOContext->satIntIoContext;
1073   smDeviceData_t       *oneDeviceData   = satIOContext->pSatDevData;
1074   smRoot_t             *smRoot          = oneDeviceData->smRoot;
1075   smIntRoot_t          *smIntRoot       = (smIntRoot_t *)smRoot->smData;
1076   smIntContext_t       *smAllShared     = (smIntContext_t *)&smIntRoot->smAllShared;
1077   bit32                interruptContext = satIOContext->interruptContext;
1078 
1079   SM_DBG2(("smsatNonChainedDataIOCB: start\n"));
1080 
1081   smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
1082 
1083   smIORequestBody->ioCompleted = agTRUE;
1084   smIORequestBody->ioStarted   = agFALSE;
1085 
1086   /* interal structure free */
1087   smsatFreeIntIoResource( smRoot,
1088                           oneDeviceData,
1089                           SatIntIo);
1090 
1091   /* Process completion */
1092   if( (agIOStatus == OSSA_IO_SUCCESS) && (agIOInfoLen == 0))
1093   {
1094     SM_DBG5(("smsatNonChainedDataIOCB: success\n"));
1095     SM_DBG5(("smsatNonChainedDataIOCB: success agIORequest %p\n", agIORequest));
1096     /*
1097      * Command was completed OK, this is the normal path.
1098      * Now call the OS-App Specific layer about this completion.
1099      */
1100     tdsmIOCompletedCB( smRoot,
1101                        smIORequestBody->smIORequest,
1102                        smIOSuccess,
1103                        SCSI_STAT_GOOD,
1104                        agNULL,
1105                        interruptContext);
1106   }
1107   else
1108   {
1109     SM_DBG1(("smsatNonChainedDataIOCB: calling smsatProcessAbnormalCompletion!!!\n"));
1110     /* More checking needed */
1111     smsatProcessAbnormalCompletion( agRoot,
1112                                     agIORequest,
1113                                     agIOStatus,
1114                                     agFirstDword,
1115                                     agIOInfoLen,
1116                                     agParam,
1117                                     satIOContext);
1118   }
1119 
1120   return;
1121 }
1122 
1123 FORCEINLINE void
1124 smsatChainedDataIOCB(
1125                      agsaRoot_t        *agRoot,
1126                      agsaIORequest_t   *agIORequest,
1127                      bit32             agIOStatus,
1128                      agsaFisHeader_t   *agFirstDword,
1129                      bit32             agIOInfoLen,
1130                      void              *agParam,
1131                      void              *ioContext
1132                     )
1133 {
1134 
1135   smRoot_t                  *smRoot = agNULL;
1136   smIntRoot_t               *smIntRoot = agNULL;
1137   smIntContext_t            *smAllShared = agNULL;
1138   smIORequestBody_t         *smIORequestBody;
1139   smIORequestBody_t         *smOrgIORequestBody;
1140   smSatIOContext_t          *satIOContext;
1141   smSatIOContext_t          *satOrgIOContext;
1142   smSatIOContext_t          *satNewIOContext;
1143   smSatInternalIo_t         *satIntIo;
1144   smSatInternalIo_t         *satNewIntIo = agNULL;
1145 //  smDeviceData_t             *satDevData;
1146   smDeviceData_t            *oneDeviceData;
1147   smScsiRspSense_t          *pSense;
1148   smIniScsiCmnd_t           *scsiCmnd;
1149   smIORequest_t             *smOrgIORequest;
1150   agsaFisRegHostToDevice_t  *hostToDevFis = agNULL;
1151   bit32                      ataStatus = 0;
1152   bit32                      status = tiError;
1153   agsaFisRegD2HHeader_t     *statDevToHostFisHeader = agNULL;
1154   bit32                      dataLength;
1155 
1156   SM_DBG6(("smsatChainedDataIOCB: start\n"));
1157   smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
1158   satIOContext           = (smSatIOContext_t *) ioContext;
1159   if (satIOContext == agNULL)
1160   {
1161     SM_DBG1(("smsatChainedDataIOCB: satIOContext is NULL\n"));
1162     return;
1163   }
1164   satIntIo               = satIOContext->satIntIoContext;
1165   oneDeviceData          = satIOContext->pSatDevData;
1166   hostToDevFis           = satIOContext->pFis;
1167   smRoot                 = oneDeviceData->smRoot;
1168   smIntRoot              = (smIntRoot_t *)smRoot->smData;
1169   smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
1170   if (satIntIo == agNULL)
1171   {
1172     SM_DBG5(("smsatChainedDataIOCB: External smSatInternalIo_t satIntIoContext\n"));
1173     satOrgIOContext = satIOContext;
1174     smOrgIORequest  = smIORequestBody->smIORequest;
1175     pSense          = satIOContext->pSense;
1176     scsiCmnd        = satIOContext->pScsiCmnd;
1177   }
1178   else
1179   {
1180     SM_DBG5(("smsatChainedDataIOCB: Internal smSatInternalIo_t satIntIoContext\n"));
1181     satOrgIOContext        = satIOContext->satOrgIOContext;
1182     smOrgIORequestBody     = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
1183     smOrgIORequest         = (smIORequest_t *)smOrgIORequestBody->smIORequest;
1184     pSense        = satOrgIOContext->pSense;
1185     scsiCmnd      = satOrgIOContext->pScsiCmnd;
1186   }
1187   smIORequestBody->ioCompleted = agTRUE;
1188   smIORequestBody->ioStarted = agFALSE;
1189 
1190   if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
1191   {
1192      SM_DBG1(("smsatChainedDataIOCB: wrong. agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
1193      smsatSetSensePayload( pSense,
1194                            SCSI_SNSKEY_NO_SENSE,
1195                            0,
1196                            SCSI_SNSCODE_NO_ADDITIONAL_INFO,
1197                            satOrgIOContext);
1198 
1199      tdsmIOCompletedCB( smRoot,
1200                         smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
1201                         smIOSuccess,
1202                         SCSI_STAT_CHECK_CONDITION,
1203                         satOrgIOContext->pSmSenseData,
1204                         satOrgIOContext->interruptContext );
1205 
1206     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
1207 
1208     smsatFreeIntIoResource( smRoot,
1209                           oneDeviceData,
1210                           satIntIo);
1211     return;
1212   }
1213 
1214   /*
1215     checking IO status, FIS type and error status
1216   */
1217   if (agIOStatus != OSSA_IO_SUCCESS)
1218   {
1219     /* agsaFisPioSetup_t or agsaFisRegDeviceToHost_t or agsaFisSetDevBits_t for read
1220        agsaFisRegDeviceToHost_t or agsaFisSetDevBits_t for write
1221        first, assumed to be Reg Device to Host FIS
1222        This is OK to just find fis type
1223     */
1224     statDevToHostFisHeader = (agsaFisRegD2HHeader_t *)&(agFirstDword->D2H);
1225     ataStatus     = statDevToHostFisHeader->status;   /* ATA Status register */
1226     /* for debugging */
1227     if( (statDevToHostFisHeader->fisType != PIO_SETUP_DEV_TO_HOST_FIS) &&
1228         (statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS) &&
1229         (statDevToHostFisHeader->fisType != SET_DEV_BITS_FIS)
1230         )
1231     {
1232       SM_DBG1(("smsatChainedDataIOCB: FAILED, Wrong FIS type 0x%x!!!\n", statDevToHostFisHeader->fisType));
1233     }
1234 
1235     /* for debugging */
1236     if ( (ataStatus & ERR_ATA_STATUS_MASK) ||
1237          (ataStatus & DF_ATA_STATUS_MASK)
1238          )
1239     {
1240       SM_DBG1(("smsatChainedDataIOCB: FAILED, error status and command 0x%x!!!\n", hostToDevFis->h.command));
1241     }
1242 
1243     /* the function below handles abort case */
1244     smsatDelayedProcessAbnormalCompletion(agRoot,
1245                                           agIORequest,
1246                                           agIOStatus,
1247                                           agFirstDword,
1248                                           agIOInfoLen,
1249                                           agParam,
1250                                           satIOContext);
1251 
1252     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
1253     smsatFreeIntIoResource( smRoot,
1254                             oneDeviceData,
1255                             satIntIo);
1256     return;
1257   } /* end of error */
1258 
1259   switch (hostToDevFis->h.command)
1260   {
1261   case SAT_READ_DMA: /* fall through */
1262   case SAT_READ_SECTORS: /* fall through */
1263   case SAT_READ_DMA_EXT: /* fall through */
1264   case SAT_READ_SECTORS_EXT: /* fall through */
1265   case SAT_READ_FPDMA_QUEUED: /* fall through */
1266   case SAT_WRITE_DMA: /* fall through */
1267   case SAT_WRITE_SECTORS:/* fall through */
1268   case SAT_WRITE_DMA_FUA_EXT: /* fall through */
1269   case SAT_WRITE_DMA_EXT: /* fall through */
1270   case SAT_WRITE_SECTORS_EXT: /* fall through */
1271   case SAT_WRITE_FPDMA_QUEUED:
1272 
1273     SM_DBG5(("smsatChainedDataIOCB: READ/WRITE success case\n"));
1274 
1275     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
1276 
1277     /* done with internally genereated SAT_SMART_RETURN_STATUS */
1278     smsatFreeIntIoResource( smRoot,
1279                             oneDeviceData,
1280                             satIntIo);
1281     /* let's loop till TL */
1282 
1283     /* lba = lba + tl
1284        loopnum--;
1285        if (loopnum == 0) done
1286      */
1287     (satOrgIOContext->LoopNum)--;
1288     if (satOrgIOContext->LoopNum == 0)
1289     {
1290       /* done with read */
1291       tdsmIOCompletedCB( smRoot,
1292                          smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
1293                          smIOSuccess,
1294                          SCSI_STAT_GOOD,
1295                          agNULL,
1296                          satOrgIOContext->interruptContext );
1297       return;
1298     }
1299 
1300     /* don't need to allocate payload memory here. Use the one allocated by OS layer */
1301     dataLength = 0;
1302 
1303     satNewIntIo = smsatAllocIntIoResource( smRoot,
1304                                            smOrgIORequest,
1305                                            oneDeviceData,
1306                                            dataLength,
1307                                            satNewIntIo);
1308     if (satNewIntIo == agNULL)
1309     {
1310       tdsmIOCompletedCB( smRoot,
1311                          smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
1312                          smIOFailed,
1313                          smDetailOtherError,
1314                          agNULL,
1315                          satOrgIOContext->interruptContext );
1316 
1317       SM_DBG1(("smsatChainedDataIOCB: momory allocation fails!!!\n"));
1318       return;
1319     } /* end of memory allocation failure */
1320 
1321        /*
1322      * Need to initialize all the fields within satIOContext
1323      */
1324 
1325     satNewIOContext = smsatPrepareNewIO(
1326                                         satNewIntIo,
1327                                         smOrgIORequest,
1328                                         oneDeviceData,
1329                                         scsiCmnd,
1330                                         satOrgIOContext
1331                                         );
1332 
1333     /* sending another ATA command */
1334     switch (scsiCmnd->cdb[0])
1335     {
1336     case SCSIOPC_READ_6:
1337       /* no loop should occur with READ6 since it fits in one ATA command */
1338       break;
1339     case SCSIOPC_READ_10: /* fall through */
1340     case SCSIOPC_READ_12: /* fall through */
1341     case SCSIOPC_READ_16: /* fall through */
1342       status = smsatRead_1( smRoot,
1343                             &satNewIntIo->satIntSmIORequest,
1344                             satNewIOContext->psmDeviceHandle,
1345                             &satNewIntIo->satIntSmScsiXchg,
1346                             satNewIOContext);
1347       break;
1348     case SCSIOPC_WRITE_6:
1349       /* no loop should occur with WRITE6 since it fits in one ATA command */
1350       break;
1351     case SCSIOPC_WRITE_10: /* fall through */
1352     case SCSIOPC_WRITE_12: /* fall through */
1353     case SCSIOPC_WRITE_16: /* fall through */
1354       status = smsatWrite_1( smRoot,
1355                              &satNewIntIo->satIntSmIORequest,
1356                              satNewIOContext->psmDeviceHandle,
1357                              &satNewIntIo->satIntSmScsiXchg,
1358                              satNewIOContext);
1359       break;
1360     default:
1361       SM_DBG1(("smsatChainedDataIOCB: success but default case scsi cmd 0x%x ata cmd 0x%x!!!\n",scsiCmnd->cdb[0], hostToDevFis->h.command));
1362       status = tiError;
1363       break;
1364     }
1365 
1366 
1367 
1368     if (status != SM_RC_SUCCESS)
1369     {
1370       smsatFreeIntIoResource( smRoot,
1371                               oneDeviceData,
1372                               satNewIntIo);
1373       tdsmIOCompletedCB( smRoot,
1374                          smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
1375                          smIOFailed,
1376                          smDetailOtherError,
1377                          agNULL,
1378                          satOrgIOContext->interruptContext );
1379       SM_DBG1(("smsatChainedDataIOCB: calling satRead10_1 fails!!!\n"));
1380       return;
1381     }
1382 
1383     break;
1384 
1385 
1386   default:
1387     SM_DBG1(("smsatChainedDataIOCB: success but default case command 0x%x!!!\n",hostToDevFis->h.command));
1388     tdsmIOCompletedCB( smRoot,
1389                        smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
1390                        smIOFailed,
1391                        smDetailOtherError,
1392                        agNULL,
1393                        satOrgIOContext->interruptContext );
1394 
1395     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
1396 
1397     smsatFreeIntIoResource( smRoot,
1398                           oneDeviceData,
1399                           satIntIo);
1400 
1401     break;
1402   }
1403 
1404 
1405   return;
1406 }
1407 
1408 osGLOBAL void
1409 smsatNonChainedVerifyCB(
1410                         agsaRoot_t        *agRoot,
1411                         agsaIORequest_t   *agIORequest,
1412                         bit32             agIOStatus,
1413                         agsaFisHeader_t   *agFirstDword,
1414                         bit32             agIOInfoLen,
1415                         agsaFrameHandle_t agFrameHandle,
1416                         void              *ioContext
1417                        )
1418 {
1419 
1420 //  tdsaRootOsData_t        *osData = (tdsaRootOsData_t *)agRoot->osData;
1421 //  tiRoot_t                *tiRoot = (tiRoot_t *)osData->tiRoot;
1422 //  tdsaRoot_t              *tdsaRoot        = (tdsaRoot_t *) tiRoot->tdData;
1423 //  tdsaContext_t           *tdsaAllShared = (tdsaContext_t *)&tdsaRoot->tdsaAllShared;
1424   smRoot_t                *smRoot = agNULL;
1425   smIntRoot_t             *smIntRoot = agNULL;
1426   smIntContext_t          *smAllShared = agNULL;
1427   smIORequestBody_t       *smIORequestBody;
1428   smIORequestBody_t       *smOrgIORequestBody;
1429   smSatIOContext_t          *satIOContext;
1430   smSatIOContext_t          *satOrgIOContext;
1431   smSatInternalIo_t         *satIntIo;
1432 //  satDeviceData_t         *satDevData;
1433   smDeviceData_t          *oneDeviceData;
1434   smScsiRspSense_t          *pSense;
1435   smIORequest_t             *smOrgIORequest;
1436   agsaFisRegHostToDevice_t  *hostToDevFis = agNULL;
1437   bit32                     ataStatus = 0;
1438   agsaFisRegD2HHeader_t    *statDevToHostFisHeader = agNULL;
1439 
1440   SM_DBG2(("smsatNonChainedVerifyCB: start\n"));
1441   SM_DBG5(("smsatNonChainedVerifyCB: agIORequest=%p agIOStatus=0x%x agIOInfoLen %d\n", agIORequest, agIOStatus, agIOInfoLen));
1442 
1443   /* internally generate smIOContext */
1444   smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
1445   satIOContext           = (smSatIOContext_t *) ioContext;
1446   satIntIo               = satIOContext->satIntIoContext;
1447   oneDeviceData          = satIOContext->pSatDevData;
1448   hostToDevFis           = satIOContext->pFis;
1449   smRoot                 = oneDeviceData->smRoot;
1450   smIntRoot              = (smIntRoot_t *)smRoot->smData;
1451   smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
1452   if (satIntIo == agNULL)
1453   {
1454     SM_DBG4(("smsatNonChainedVerifyCB: External smSatInternalIo_t satIntIoContext\n"));
1455     satOrgIOContext = satIOContext;
1456     smOrgIORequest  = smIORequestBody->smIORequest;
1457     pSense          = satIOContext->pSense;
1458   }
1459   else
1460   {
1461     SM_DBG4(("smsatNonChainedVerifyCB: Internal smSatInternalIo_t satIntIoContext\n"));
1462     satOrgIOContext        = satIOContext->satOrgIOContext;
1463     if (satOrgIOContext == agNULL)
1464     {
1465       SM_DBG4(("smsatNonChainedVerifyCB: satOrgIOContext is NULL, wrong\n"));
1466       return;
1467     }
1468     else
1469     {
1470       SM_DBG4(("smsatNonChainedVerifyCB: satOrgIOContext is NOT NULL\n"));
1471     }
1472     smOrgIORequestBody     = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
1473     smOrgIORequest         = (smIORequest_t *)smOrgIORequestBody->smIORequest;
1474     pSense                 = satOrgIOContext->pSense;
1475   }
1476 
1477   smIORequestBody->ioCompleted = agTRUE;
1478   smIORequestBody->ioStarted = agFALSE;
1479   if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
1480   {
1481      SM_DBG1(("smsatNonChainedVerifyCB: wrong. agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
1482      smsatSetSensePayload( pSense,
1483                            SCSI_SNSKEY_NO_SENSE,
1484                            0,
1485                            SCSI_SNSCODE_NO_ADDITIONAL_INFO,
1486                            satOrgIOContext);
1487      tdsmIOCompletedCB( smRoot,
1488                         smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
1489                         smIOSuccess,
1490                         SCSI_STAT_CHECK_CONDITION,
1491                         satOrgIOContext->pSmSenseData,
1492                         satOrgIOContext->interruptContext );
1493     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
1494 
1495     smsatFreeIntIoResource( smRoot,
1496                             oneDeviceData,
1497                             satIntIo);
1498     return;
1499   }
1500   if (agIOStatus != OSSA_IO_SUCCESS)
1501   {
1502     /* only agsaFisRegDeviceToHost_t is expected */
1503     statDevToHostFisHeader = (agsaFisRegD2HHeader_t *)&(agFirstDword->D2H);
1504     ataStatus     = statDevToHostFisHeader->status;   /* ATA Status register */
1505   }
1506 
1507   if( agIOStatus != OSSA_IO_SUCCESS)
1508   {
1509     if ( (statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS) ||
1510          ((ataStatus & ERR_ATA_STATUS_MASK) || (ataStatus & DF_ATA_STATUS_MASK))
1511          )
1512     {
1513       /* for debugging */
1514       if( agIOStatus != OSSA_IO_SUCCESS)
1515       {
1516         SM_DBG1(("smsatNonChainedVerifyCB: FAILED, NOT IO_SUCCESS!!!\n"));
1517       }
1518       else if (statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS)
1519       {
1520         SM_DBG1(("smsatNonChainedVerifyCB: FAILED, Wrong FIS type 0x%x!!!\n",statDevToHostFisHeader->fisType));
1521       }
1522       else if ( (ataStatus & ERR_ATA_STATUS_MASK) ||
1523                 (ataStatus & DF_ATA_STATUS_MASK)
1524                 )
1525       {
1526         SM_DBG1(("smsatNonChainedVerifyCB: FAILED, FAILED, error status!!!\n"));
1527       }
1528 
1529       /* Process abort case */
1530       if (agIOStatus == OSSA_IO_ABORTED)
1531       {
1532         smsatProcessAbort(smRoot,
1533                           smOrgIORequest,
1534                           satOrgIOContext
1535                           );
1536 
1537         smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
1538 
1539         smsatFreeIntIoResource( smRoot,
1540                                 oneDeviceData,
1541                                 satIntIo);
1542         return;
1543       }
1544 
1545       /* for debugging */
1546       switch (hostToDevFis->h.command)
1547       {
1548       case SAT_READ_VERIFY_SECTORS:
1549         SM_DBG1(("smsatNonChainedVerifyCB: SAT_READ_VERIFY_SECTORS!!!\n"));
1550         break;
1551       case SAT_READ_VERIFY_SECTORS_EXT:
1552         SM_DBG1(("smsatNonChainedVerifyCB: SAT_READ_VERIFY_SECTORS_EXT!!!\n"));
1553         break;
1554       default:
1555         SM_DBG1(("smsatNonChainedVerifyCB: error default case command 0x%x!!!\n", hostToDevFis->h.command));
1556         break;
1557       }
1558 
1559       smsatSetSensePayload( pSense,
1560                             SCSI_SNSKEY_NO_SENSE,
1561                             0,
1562                             SCSI_SNSCODE_NO_ADDITIONAL_INFO,
1563                             satOrgIOContext);
1564 
1565       tdsmIOCompletedCB( smRoot,
1566                          smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
1567                          smIOSuccess,
1568                          SCSI_STAT_CHECK_CONDITION,
1569                          satOrgIOContext->pSmSenseData,
1570                          satOrgIOContext->interruptContext );
1571 
1572       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
1573 
1574       smsatFreeIntIoResource( smRoot,
1575                               oneDeviceData,
1576                               satIntIo);
1577       return;
1578     } /* end error checking */
1579   }
1580 
1581   /* process success from this point on */
1582   switch (hostToDevFis->h.command)
1583   {
1584   case SAT_READ_VERIFY_SECTORS: /* fall through */
1585   case SAT_READ_VERIFY_SECTORS_EXT:
1586     SM_DBG5(("smsatNonChainedVerifyCB: SAT_WRITE_DMA_EXT success \n"));
1587 
1588     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
1589 
1590     smsatFreeIntIoResource( smRoot,
1591                             oneDeviceData,
1592                             satIntIo);
1593 
1594     tdsmIOCompletedCB( smRoot,
1595                        smOrgIORequest,
1596                        smIOSuccess,
1597                        SCSI_STAT_GOOD,
1598                        agNULL,
1599                        satOrgIOContext->interruptContext);
1600     break;
1601   default:
1602     SM_DBG1(("smsatNonChainedVerifyCB: success but error default case command 0x%x!!!\n", hostToDevFis->h.command));
1603 
1604     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
1605 
1606     smsatFreeIntIoResource( smRoot,
1607                             oneDeviceData,
1608                             satIntIo);
1609 
1610     smsatSetSensePayload( pSense,
1611                           SCSI_SNSKEY_NO_SENSE,
1612                           0,
1613                           SCSI_SNSCODE_NO_ADDITIONAL_INFO,
1614                           satOrgIOContext);
1615 
1616     tdsmIOCompletedCB( smRoot,
1617                        smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
1618                        smIOSuccess,
1619                        SCSI_STAT_CHECK_CONDITION,
1620                        satOrgIOContext->pSmSenseData,
1621                        satOrgIOContext->interruptContext );
1622 
1623     break;
1624   }
1625 
1626   return;
1627 }
1628 
1629 osGLOBAL void
1630 smsatChainedVerifyCB(
1631                      agsaRoot_t        *agRoot,
1632                      agsaIORequest_t   *agIORequest,
1633                      bit32             agIOStatus,
1634                      agsaFisHeader_t   *agFirstDword,
1635                      bit32             agIOInfoLen,
1636                      agsaFrameHandle_t agFrameHandle,
1637                      void              *ioContext
1638                     )
1639 {
1640 
1641   smRoot_t                  *smRoot = agNULL;
1642   smIntRoot_t               *smIntRoot = agNULL;
1643   smIntContext_t            *smAllShared = agNULL;
1644   smIORequestBody_t         *smIORequestBody;
1645   smIORequestBody_t         *smOrgIORequestBody;
1646   smSatIOContext_t          *satIOContext;
1647   smSatIOContext_t          *satOrgIOContext;
1648   smSatIOContext_t          *satNewIOContext;
1649   smSatInternalIo_t         *satIntIo;
1650   smSatInternalIo_t         *satNewIntIo = agNULL;
1651   smDeviceData_t            *oneDeviceData;
1652   smScsiRspSense_t          *pSense;
1653   smIniScsiCmnd_t           *scsiCmnd;
1654   smIORequest_t             *smOrgIORequest;
1655   agsaFisRegHostToDevice_t  *hostToDevFis = agNULL;
1656   bit32                      ataStatus = 0;
1657   agsaFisRegD2HHeader_t     *statDevToHostFisHeader = agNULL;
1658   bit32                      status = tiError;
1659   bit32                      dataLength;
1660 
1661   SM_DBG2(("smsatChainedVerifyCB: start\n"));
1662   SM_DBG5(("smsatChainedVerifyCB: agIORequest=%p agIOStatus=0x%x agIOInfoLen %d\n", agIORequest, agIOStatus, agIOInfoLen));
1663 
1664   /* internally generate smIOContext */
1665   smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
1666   satIOContext           = (smSatIOContext_t *) ioContext;
1667   satIntIo               = satIOContext->satIntIoContext;
1668   oneDeviceData          = satIOContext->pSatDevData;
1669   hostToDevFis           = satIOContext->pFis;
1670   smRoot                 = oneDeviceData->smRoot;
1671   smIntRoot              = (smIntRoot_t *)smRoot->smData;
1672   smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
1673 
1674   if (satIntIo == agNULL)
1675   {
1676     SM_DBG4(("smsatChainedVerifyCB: External smSatInternalIo_t satIntIoContext\n"));
1677     satOrgIOContext = satIOContext;
1678     smOrgIORequest  = smIORequestBody->smIORequest;
1679     pSense          = satIOContext->pSense;
1680     scsiCmnd        = satIOContext->pScsiCmnd;
1681   }
1682   else
1683   {
1684     SM_DBG4(("smsatChainedVerifyCB: Internal smSatInternalIo_t satIntIoContext\n"));
1685     satOrgIOContext        = satIOContext->satOrgIOContext;
1686     if (satOrgIOContext == agNULL)
1687     {
1688       SM_DBG4(("smsatChainedVerifyCB: satOrgIOContext is NULL, wrong\n"));
1689       return;
1690     }
1691     else
1692     {
1693       SM_DBG4(("smsatChainedVerifyCB: satOrgIOContext is NOT NULL\n"));
1694     }
1695     smOrgIORequestBody     = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
1696     smOrgIORequest         = (smIORequest_t *)smOrgIORequestBody->smIORequest;
1697     pSense        = satOrgIOContext->pSense;
1698     scsiCmnd      = satOrgIOContext->pScsiCmnd;
1699   }
1700 
1701   smIORequestBody->ioCompleted = agTRUE;
1702   smIORequestBody->ioStarted = agFALSE;
1703 
1704   if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
1705   {
1706      SM_DBG1(("smsatChainedVerifyCB: wrong. agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
1707      smsatSetSensePayload( pSense,
1708                            SCSI_SNSKEY_NO_SENSE,
1709                            0,
1710                            SCSI_SNSCODE_NO_ADDITIONAL_INFO,
1711                            satOrgIOContext);
1712 
1713      tdsmIOCompletedCB( smRoot,
1714                         smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
1715                         smIOSuccess,
1716                         SCSI_STAT_CHECK_CONDITION,
1717                         satOrgIOContext->pSmSenseData,
1718                         satOrgIOContext->interruptContext );
1719 
1720      smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
1721 
1722      smsatFreeIntIoResource( smRoot,
1723                              oneDeviceData,
1724                              satIntIo);
1725      return;
1726   }
1727 
1728   if (agIOStatus != OSSA_IO_SUCCESS)
1729   {
1730     /* only agsaFisRegDeviceToHost_t is expected */
1731     statDevToHostFisHeader = (agsaFisRegD2HHeader_t *)&(agFirstDword->D2H);
1732     ataStatus     = statDevToHostFisHeader->status;   /* ATA Status register */
1733   }
1734 
1735   if( agIOStatus != OSSA_IO_SUCCESS)
1736   {
1737     if ( (statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS) ||
1738          ((ataStatus & ERR_ATA_STATUS_MASK) || (ataStatus & DF_ATA_STATUS_MASK))
1739          )
1740     {
1741       /* for debugging */
1742       if( agIOStatus != OSSA_IO_SUCCESS)
1743       {
1744         SM_DBG1(("smsatChainedVerifyCB: FAILED, NOT IO_SUCCESS!!!\n"));
1745       }
1746       else if (statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS)
1747       {
1748         SM_DBG1(("smsatChainedVerifyCB: FAILED, Wrong FIS type 0x%x!!!\n",statDevToHostFisHeader->fisType));
1749       }
1750       else if ( (ataStatus & ERR_ATA_STATUS_MASK) ||
1751                 (ataStatus & DF_ATA_STATUS_MASK)
1752                 )
1753       {
1754         SM_DBG1(("smsatChainedVerifyCB: FAILED, FAILED, error status!!!\n"));
1755       }
1756 
1757       /* Process abort case */
1758       if (agIOStatus == OSSA_IO_ABORTED)
1759       {
1760         smsatProcessAbort(smRoot,
1761                           smOrgIORequest,
1762                           satOrgIOContext
1763                           );
1764 
1765         smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
1766 
1767         smsatFreeIntIoResource( smRoot,
1768                                 oneDeviceData,
1769                                 satIntIo);
1770         return;
1771       }
1772 
1773       /* for debugging */
1774       switch (hostToDevFis->h.command)
1775       {
1776       case SAT_READ_VERIFY_SECTORS:
1777         SM_DBG1(("smsatChainedVerifyCB: SAT_READ_VERIFY_SECTORS!!!\n"));
1778         break;
1779       case SAT_READ_VERIFY_SECTORS_EXT:
1780         SM_DBG1(("smsatChainedVerifyCB: SAT_READ_VERIFY_SECTORS_EXT!!!\n"));
1781         break;
1782       default:
1783         SM_DBG1(("smsatChainedVerifyCB: error default case command 0x%x!!!\n", hostToDevFis->h.command));
1784         break;
1785       }
1786 
1787       smsatSetSensePayload( pSense,
1788                             SCSI_SNSKEY_NO_SENSE,
1789                             0,
1790                             SCSI_SNSCODE_NO_ADDITIONAL_INFO,
1791                             satOrgIOContext);
1792 
1793      tdsmIOCompletedCB( smRoot,
1794                         smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
1795                         smIOSuccess,
1796                         SCSI_STAT_CHECK_CONDITION,
1797                         satOrgIOContext->pSmSenseData,
1798                         satOrgIOContext->interruptContext );
1799 
1800       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
1801 
1802       smsatFreeIntIoResource( smRoot,
1803                               oneDeviceData,
1804                               satIntIo);
1805       return;
1806     } /* end error checking */
1807   }
1808 
1809   /* process success from this point on */
1810   switch (hostToDevFis->h.command)
1811   {
1812   case SAT_READ_VERIFY_SECTORS: /* fall through */
1813   case SAT_READ_VERIFY_SECTORS_EXT:
1814     SM_DBG5(("smsatChainedVerifyCB: SAT_WRITE_DMA_EXT success \n"));
1815 
1816     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
1817 
1818     smsatFreeIntIoResource( smRoot,
1819                             oneDeviceData,
1820                             satIntIo);
1821 
1822     /* let's loop till TL */
1823 
1824     /* lba = lba + tl
1825        loopnum--;
1826        if (loopnum == 0) done
1827      */
1828     (satOrgIOContext->LoopNum)--;
1829     if (satOrgIOContext->LoopNum == 0)
1830     {
1831       /*
1832         done with write and verify
1833       */
1834      tdsmIOCompletedCB( smRoot,
1835                         smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
1836                         smIOSuccess,
1837                         SCSI_STAT_GOOD,
1838                         agNULL,
1839                         satOrgIOContext->interruptContext );
1840       return;
1841     }
1842 
1843     if (satOrgIOContext->superIOFlag)
1844     {
1845       dataLength = ((tiSuperScsiInitiatorRequest_t *) satOrgIOContext->smScsiXchg)->scsiCmnd.expDataLength;
1846     }
1847     else
1848     {
1849       dataLength = ((tiScsiInitiatorRequest_t *) satOrgIOContext->smScsiXchg)->scsiCmnd.expDataLength;
1850     }
1851 
1852     satNewIntIo = smsatAllocIntIoResource( smRoot,
1853                                            smOrgIORequest,
1854                                            oneDeviceData,
1855                                            dataLength,
1856                                            satNewIntIo);
1857     if (satNewIntIo == agNULL)
1858     {
1859       tdsmIOCompletedCB( smRoot,
1860                          smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
1861                          smIOFailed,
1862                          smDetailOtherError,
1863                          agNULL,
1864                          satOrgIOContext->interruptContext );
1865 
1866       SM_DBG1(("smsatChainedVerifyCB: momory allocation fails!!!\n"));
1867       return;
1868     } /* end of memory allocation failure */
1869 
1870     /*
1871      * Need to initialize all the fields within satIOContext
1872      */
1873 
1874     satNewIOContext = smsatPrepareNewIO(
1875                                         satNewIntIo,
1876                                         smOrgIORequest,
1877                                         oneDeviceData,
1878                                         scsiCmnd,
1879                                         satOrgIOContext
1880                                        );
1881     status = smsatChainedVerify(smRoot,
1882                                 &satNewIntIo->satIntSmIORequest,
1883                                 satNewIOContext->psmDeviceHandle,
1884                                 &satNewIntIo->satIntSmScsiXchg,
1885                                 satNewIOContext);
1886 
1887     if (status != SM_RC_SUCCESS)
1888     {
1889       smsatFreeIntIoResource( smRoot,
1890                               oneDeviceData,
1891                               satNewIntIo);
1892       tdsmIOCompletedCB( smRoot,
1893                          smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
1894                          smIOFailed,
1895                          smDetailOtherError,
1896                          agNULL,
1897                          satOrgIOContext->interruptContext );
1898       SM_DBG1(("smsatChainedVerifyCB: calling satChainedVerify fails!!!\n"));
1899       return;
1900     }
1901 
1902     break;
1903   default:
1904     SM_DBG1(("smsatChainedVerifyCB: success but error default case command 0x%x!!!\n", hostToDevFis->h.command));
1905 
1906     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
1907 
1908     smsatFreeIntIoResource( smRoot,
1909                             oneDeviceData,
1910                             satIntIo);
1911 
1912     smsatSetSensePayload( pSense,
1913                           SCSI_SNSKEY_NO_SENSE,
1914                           0,
1915                           SCSI_SNSCODE_NO_ADDITIONAL_INFO,
1916                           satOrgIOContext);
1917 
1918     tdsmIOCompletedCB( smRoot,
1919                        smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
1920                        smIOSuccess,
1921                        SCSI_STAT_CHECK_CONDITION,
1922                        satOrgIOContext->pSmSenseData,
1923                        satOrgIOContext->interruptContext );
1924 
1925     break;
1926   }
1927   return;
1928 }
1929 
1930 
1931 osGLOBAL void
1932 smsatTestUnitReadyCB(
1933                      agsaRoot_t        *agRoot,
1934                      agsaIORequest_t   *agIORequest,
1935                      bit32             agIOStatus,
1936                      agsaFisHeader_t   *agFirstDword,
1937                      bit32             agIOInfoLen,
1938                      agsaFrameHandle_t agFrameHandle,
1939                      void              *ioContext
1940                     )
1941 {
1942   /*
1943     In the process of TestUnitReady
1944     Process SAT_GET_MEDIA_STATUS
1945     Process SAT_CHECK_POWER_MODE
1946   */
1947 //  tdsaRootOsData_t        *osData = (tdsaRootOsData_t *)agRoot->osData;
1948 //  tiRoot_t                *tiRoot = (tiRoot_t *)osData->tiRoot;
1949 //  tdsaRoot_t              *tdsaRoot        = (tdsaRoot_t *) tiRoot->tdData;
1950 //  tdsaContext_t           *tdsaAllShared = (tdsaContext_t *)&tdsaRoot->tdsaAllShared;
1951   smRoot_t                *smRoot = agNULL;
1952   smIntRoot_t             *smIntRoot = agNULL;
1953   smIntContext_t          *smAllShared = agNULL;
1954   smIORequestBody_t       *smIORequestBody;
1955   smIORequestBody_t       *smOrgIORequestBody;
1956   smSatIOContext_t          *satIOContext;
1957   smSatIOContext_t          *satOrgIOContext;
1958   smSatIOContext_t          *satNewIOContext;
1959   smSatInternalIo_t         *satIntIo;
1960   smSatInternalIo_t         *satNewIntIo = agNULL;
1961 //  satDeviceData_t         *satDevData;
1962   smDeviceData_t          *oneDeviceData;
1963 
1964   smScsiRspSense_t          *pSense;
1965   smIniScsiCmnd_t           *scsiCmnd;
1966   smIORequest_t             *smOrgIORequest;
1967 
1968   agsaFisRegHostToDevice_t  *hostToDevFis = agNULL;
1969   bit32                     ataStatus = 0;
1970   bit32                     ataError;
1971 
1972   bit32                     status;
1973   agsaFisRegD2HHeader_t    *statDevToHostFisHeader = agNULL;
1974 
1975   SM_DBG2(("smsatTestUnitReadyCB: start\n"));
1976   SM_DBG6(("smsatTestUnitReadyCB: agIORequest=%p agIOStatus=0x%x agIOInfoLen %d\n", agIORequest, agIOStatus, agIOInfoLen));
1977 
1978   /* internally generate smIOContext */
1979   smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
1980   satIOContext           = (smSatIOContext_t *) ioContext;
1981   satIntIo               = satIOContext->satIntIoContext;
1982   oneDeviceData          = satIOContext->pSatDevData;
1983   hostToDevFis           = satIOContext->pFis;
1984   smRoot                 = oneDeviceData->smRoot;
1985   smIntRoot              = (smIntRoot_t *)smRoot->smData;
1986   smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
1987 
1988   if (satIntIo == agNULL)
1989   {
1990     SM_DBG5(("smsatTestUnitReadyCB: no internal smSatInternalIo_t satIntIoContext\n"));
1991     pSense        = satIOContext->pSense;
1992     scsiCmnd      = satIOContext->pScsiCmnd;
1993     satOrgIOContext = satIOContext;
1994     smOrgIORequest  = smIORequestBody->smIORequest;
1995   }
1996   else
1997   {
1998     SM_DBG5(("smsatTestUnitReadyCB: yes internal smSatInternalIo_t satIntIoContext\n"));
1999 
2000     /* orginal smIOContext */
2001     smOrgIORequest         = (smIORequest_t *)satIOContext->satIntIoContext->satOrgSmIORequest;
2002     smOrgIORequestBody     = (smIORequestBody_t *)smOrgIORequest->tdData;
2003     satOrgIOContext        = &(smOrgIORequestBody->transport.SATA.satIOContext);
2004 
2005     pSense        = satOrgIOContext->pSense;
2006     scsiCmnd      = satOrgIOContext->pScsiCmnd;
2007   }
2008 
2009   smIORequestBody->ioCompleted = agTRUE;
2010   smIORequestBody->ioStarted = agFALSE;
2011 
2012   if (agIOStatus == OSSA_IO_ABORTED)
2013   {
2014     tdsmIOCompletedCB( smRoot,
2015                        smOrgIORequest,
2016                        smIOFailed,
2017                        smDetailAborted,
2018                        agNULL,
2019                        satIOContext->interruptContext);
2020 
2021     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
2022 
2023     smsatFreeIntIoResource( smRoot,
2024                             oneDeviceData,
2025                             satIntIo);
2026 
2027     return;
2028   }
2029 
2030   if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
2031   {
2032     SM_DBG1(("smsatTestUnitReadyCB: agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
2033     smsatSetSensePayload( pSense,
2034                           SCSI_SNSKEY_NOT_READY,
2035                           0,
2036                           SCSI_SNSCODE_LOGICAL_UNIT_NOT_READY_CAUSE_NOT_REPORTABLE,
2037                           satOrgIOContext);
2038 
2039     tdsmIOCompletedCB( smRoot,
2040                        smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
2041                        smIOSuccess,
2042                        SCSI_STAT_CHECK_CONDITION,
2043                        satOrgIOContext->pSmSenseData,
2044                        satOrgIOContext->interruptContext );
2045 
2046     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
2047 
2048     smsatFreeIntIoResource( smRoot,
2049                             oneDeviceData,
2050                             satIntIo);
2051 
2052     return;
2053   }
2054   /*
2055     HW checks an error for us and the results is agIOStatus
2056   */
2057   if (agIOStatus != OSSA_IO_SUCCESS)
2058   {
2059     /* only agsaFisRegDeviceToHost_t is expected */
2060     statDevToHostFisHeader = (agsaFisRegD2HHeader_t *)&(agFirstDword->D2H);
2061     ataStatus     = statDevToHostFisHeader->status;   /* ATA Status register */
2062     ataError      = statDevToHostFisHeader->error;    /* ATA Eror register   */
2063     if (statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS)
2064     {
2065       SM_DBG1(("smsatTestUnitReadyCB: FAILED, Wrong FIS type 0x%x!!!\n",statDevToHostFisHeader->fisType));
2066     }
2067     else if ( (ataStatus & ERR_ATA_STATUS_MASK) ||
2068               (ataStatus & DF_ATA_STATUS_MASK)
2069             )
2070     {
2071       SM_DBG1(("smsatTestUnitReadyCB: FAILED, FAILED, error status!!!\n"));
2072     }
2073 
2074     /* Process abort case */
2075     if (agIOStatus == OSSA_IO_ABORTED)
2076     {
2077       smsatProcessAbort(smRoot,
2078                         smOrgIORequest,
2079                         satOrgIOContext
2080                         );
2081 
2082       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
2083 
2084       smsatFreeIntIoResource( smRoot,
2085                               oneDeviceData,
2086                               satIntIo);
2087       return;
2088     }
2089 
2090     switch (hostToDevFis->h.command)
2091     {
2092     case SAT_GET_MEDIA_STATUS:
2093       SM_DBG1(("smsatTestUnitReadyCB: SAT_GET_MEDIA_STATUS failed!!! \n"));
2094 
2095       /* checking NM bit */
2096       if (ataError & SCSI_NM_MASK)
2097       {
2098         smsatSetSensePayload( pSense,
2099                               SCSI_SNSKEY_NOT_READY,
2100                               0,
2101                               SCSI_SNSCODE_MEDIUM_NOT_PRESENT,
2102                               satOrgIOContext);
2103       }
2104       else
2105       {
2106         smsatSetSensePayload( pSense,
2107                               SCSI_SNSKEY_NOT_READY,
2108                               0,
2109                               SCSI_SNSCODE_LOGICAL_UNIT_NOT_READY_CAUSE_NOT_REPORTABLE,
2110                               satOrgIOContext);
2111       }
2112 
2113       tdsmIOCompletedCB( smRoot,
2114                          smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
2115                          smIOSuccess,
2116                          SCSI_STAT_CHECK_CONDITION,
2117                          satOrgIOContext->pSmSenseData,
2118                          satOrgIOContext->interruptContext );
2119 
2120       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
2121 
2122       smsatFreeIntIoResource( smRoot,
2123                               oneDeviceData,
2124                               satIntIo);
2125       break;
2126     case SAT_CHECK_POWER_MODE:
2127       SM_DBG1(("smsatTestUnitReadyCB: SAT_CHECK_POWER_MODE failed!!! \n"));
2128       smsatSetSensePayload( pSense,
2129                             SCSI_SNSKEY_NOT_READY,
2130                             0,
2131                             SCSI_SNSCODE_LOGICAL_UNIT_DOES_NOT_RESPOND_TO_SELECTION,
2132                             satOrgIOContext);
2133 
2134       tdsmIOCompletedCB( smRoot,
2135                          smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
2136                          smIOSuccess,
2137                          SCSI_STAT_CHECK_CONDITION,
2138                          satOrgIOContext->pSmSenseData,
2139                          satOrgIOContext->interruptContext );
2140 
2141       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
2142 
2143       smsatFreeIntIoResource( smRoot,
2144                               oneDeviceData,
2145                               satIntIo);
2146       break;
2147     default:
2148       SM_DBG1(("smsatTestUnitReadyCB: default failed command %d!!!\n", hostToDevFis->h.command));
2149 
2150       smsatSetSensePayload( pSense,
2151                             SCSI_SNSKEY_NOT_READY,
2152                             0,
2153                             SCSI_SNSCODE_LOGICAL_UNIT_NOT_READY_CAUSE_NOT_REPORTABLE,
2154                             satOrgIOContext);
2155 
2156       tdsmIOCompletedCB( smRoot,
2157                          smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
2158                          smIOSuccess,
2159                          SCSI_STAT_CHECK_CONDITION,
2160                          satOrgIOContext->pSmSenseData,
2161                          satOrgIOContext->interruptContext );
2162 
2163       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
2164 
2165       smsatFreeIntIoResource( smRoot,
2166                               oneDeviceData,
2167                               satIntIo);
2168       break;
2169 
2170     }
2171     return;
2172   }/* end error */
2173 
2174   /* ATA command completes sucessfully */
2175   switch (hostToDevFis->h.command)
2176   {
2177   case SAT_GET_MEDIA_STATUS:
2178 
2179     SM_DBG5(("smsatTestUnitReadyCB: SAT_GET_MEDIA_STATUS success\n"));
2180 
2181     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
2182 
2183     smsatFreeIntIoResource( smRoot,
2184                             oneDeviceData,
2185                             satIntIo);
2186 
2187     satNewIntIo = smsatAllocIntIoResource( smRoot,
2188                                            smOrgIORequest,
2189                                            oneDeviceData,
2190                                            0,
2191                                            satNewIntIo);
2192     if (satNewIntIo == agNULL)
2193     {
2194       smsatSetSensePayload( pSense,
2195                             SCSI_SNSKEY_NOT_READY,
2196                             0,
2197                             SCSI_SNSCODE_LOGICAL_UNIT_NOT_READY_CAUSE_NOT_REPORTABLE,
2198                             satOrgIOContext);
2199 
2200       tdsmIOCompletedCB( smRoot,
2201                          smOrgIORequest,
2202                          smIOSuccess,
2203                          SCSI_STAT_CHECK_CONDITION,
2204                          satOrgIOContext->pSmSenseData,
2205                          satOrgIOContext->interruptContext );
2206 
2207       SM_DBG1(("smsatTestUnitReadyCB: momory allocation fails!!!\n"));
2208       return;
2209     }
2210 
2211     /*
2212      * Need to initialize all the fields within satIOContext
2213      */
2214 
2215     satNewIOContext = smsatPrepareNewIO(
2216                                         satNewIntIo,
2217                                         smOrgIORequest,
2218                                         oneDeviceData,
2219                                         scsiCmnd,
2220                                         satOrgIOContext
2221                                         );
2222 
2223     /* sends SAT_CHECK_POWER_MODE */
2224     status = smsatTestUnitReady_1( smRoot,
2225                                    &satNewIntIo->satIntSmIORequest,
2226                                    satNewIOContext->psmDeviceHandle,
2227                                    &satNewIntIo->satIntSmScsiXchg,
2228                                    satNewIOContext);
2229 
2230     if (status != SM_RC_SUCCESS)
2231     {
2232       /* sending SAT_CHECK_POWER_MODE fails */
2233       smsatFreeIntIoResource( smRoot,
2234                               oneDeviceData,
2235                               satNewIntIo);
2236       smsatSetSensePayload( pSense,
2237                             SCSI_SNSKEY_NOT_READY,
2238                             0,
2239                             SCSI_SNSCODE_LOGICAL_UNIT_NOT_READY_CAUSE_NOT_REPORTABLE,
2240                             satOrgIOContext);
2241 
2242       tdsmIOCompletedCB( smRoot,
2243                         smOrgIORequest,
2244                         smIOSuccess,
2245                         SCSI_STAT_CHECK_CONDITION,
2246                         satOrgIOContext->pSmSenseData,
2247                         satOrgIOContext->interruptContext );
2248 
2249        SM_DBG1(("smsatTestUnitReadyCB: calling satTestUnitReady_1 fails!!!\n"));
2250        return;
2251     }
2252 
2253     break;
2254   case SAT_CHECK_POWER_MODE:
2255     SM_DBG5(("smsatTestUnitReadyCB: SAT_CHECK_POWER_MODE success\n"));
2256 
2257 
2258     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
2259 
2260     smsatFreeIntIoResource( smRoot,
2261                             oneDeviceData,
2262                             satIntIo);
2263 
2264     /* returns good status */
2265     tdsmIOCompletedCB( smRoot,
2266                        smOrgIORequest,
2267                        smIOSuccess,
2268                        SCSI_STAT_GOOD,
2269                        agNULL,
2270                        satOrgIOContext->interruptContext );
2271 
2272     break;
2273   default:
2274     SM_DBG1(("smsatTestUnitReadyCB: default success command %d!!!\n", hostToDevFis->h.command));
2275     smsatSetSensePayload( pSense,
2276                           SCSI_SNSKEY_NOT_READY,
2277                           0,
2278                           SCSI_SNSCODE_LOGICAL_UNIT_NOT_READY_CAUSE_NOT_REPORTABLE,
2279                           satOrgIOContext);
2280 
2281     tdsmIOCompletedCB( smRoot,
2282                        smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
2283                        smIOSuccess,
2284                        SCSI_STAT_CHECK_CONDITION,
2285                        satOrgIOContext->pSmSenseData,
2286                        satOrgIOContext->interruptContext );
2287 
2288 
2289     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
2290 
2291     smsatFreeIntIoResource( smRoot,
2292                             oneDeviceData,
2293                             satIntIo);
2294     break;
2295   }
2296 
2297   return;
2298 }
2299 
2300 osGLOBAL void
2301 smsatRequestSenseCB(
2302                     agsaRoot_t        *agRoot,
2303                     agsaIORequest_t   *agIORequest,
2304                     bit32             agIOStatus,
2305                     agsaFisHeader_t   *agFirstDword,
2306                     bit32             agIOInfoLen,
2307                     void              *agParam,
2308                     void              *ioContext
2309                    )
2310 {
2311   /* ATA Vol 1, p299 SAT_SMART_RETURN_STATUS */
2312   /*
2313     if threshold exceeds, return SCSI_SNSCODE_HARDWARE_IMPENDING_FAILURE
2314     else call satRequestSense_1 to send CHECK_POWER_MODE
2315   */
2316 
2317 //  tdsaRootOsData_t        *osData = (tdsaRootOsData_t *)agRoot->osData;
2318 //  tiRoot_t                *tiRoot = (tiRoot_t *)osData->tiRoot;
2319 //  tdsaRoot_t              *tdsaRoot        = (tdsaRoot_t *) tiRoot->tdData;
2320 //  tdsaContext_t           *tdsaAllShared = (tdsaContext_t *)&tdsaRoot->tdsaAllShared;
2321   smRoot_t                *smRoot = agNULL;
2322   smIntRoot_t             *smIntRoot = agNULL;
2323   smIntContext_t          *smAllShared = agNULL;
2324   smIORequestBody_t       *smIORequestBody;
2325   smIORequestBody_t       *smOrgIORequestBody;
2326   smSatIOContext_t          *satIOContext;
2327   smSatIOContext_t          *satOrgIOContext;
2328   smSatIOContext_t          *satNewIOContext;
2329   smSatInternalIo_t         *satIntIo;
2330   smSatInternalIo_t         *satNewIntIo = agNULL;
2331 //  satDeviceData_t         *satDevData;
2332   smDeviceData_t          *oneDeviceData;
2333   smScsiRspSense_t          *pSense;
2334   smIniScsiCmnd_t           *scsiCmnd;
2335   smIORequest_t             *smOrgIORequest;
2336   agsaFisRegHostToDevice_t  *hostToDevFis = agNULL;
2337   bit32                     ataStatus = 0;
2338   bit32                     status;
2339   agsaFisRegD2HHeader_t     *statDevToHostFisHeader = agNULL;
2340   agsaFisRegD2HData_t       statDevToHostFisData;
2341   bit32                     allocationLen = 0;
2342   bit32                     dataLength;
2343   bit8                      *pDataBuffer = agNULL;
2344 
2345   SM_DBG2(("smsatRequestSenseCB: start\n"));
2346   SM_DBG4(("smsatRequestSenseCB:agIORequest=%p agIOStatus=0x%x agIOInfoLen %d\n", agIORequest, agIOStatus, agIOInfoLen));
2347 
2348   /* internally generate smIOContext */
2349   smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
2350   satIOContext           = (smSatIOContext_t *) ioContext;
2351   satIntIo               = satIOContext->satIntIoContext;
2352   oneDeviceData          = satIOContext->pSatDevData;
2353   hostToDevFis           = satIOContext->pFis;
2354   smRoot                 = oneDeviceData->smRoot;
2355   smIntRoot              = (smIntRoot_t *)smRoot->smData;
2356   smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
2357 
2358   /*ttttttthe one */
2359   if (satIntIo == agNULL)
2360   {
2361     SM_DBG4(("smsatRequestSenseCB: External smSatInternalIo_t satIntIoContext\n"));
2362     satOrgIOContext = satIOContext;
2363     smOrgIORequest  = smIORequestBody->smIORequest;
2364     if (satOrgIOContext->superIOFlag)
2365     {
2366         pDataBuffer = (bit8 *)(((tiSuperScsiInitiatorRequest_t *)satOrgIOContext->smScsiXchg)->sglVirtualAddr);//satOrgIOContext->pSense;
2367     }
2368     else
2369     {
2370         pDataBuffer = (bit8 *)(((tiScsiInitiatorRequest_t *) satOrgIOContext->smScsiXchg)->sglVirtualAddr);//satOrgIOContext->pSense;
2371 
2372     }
2373     scsiCmnd        = satOrgIOContext->pScsiCmnd;
2374     pSense          = satOrgIOContext->pSense;
2375   }
2376   else
2377   {
2378     SM_DBG4(("smsatRequestSenseCB: Internal smSatInternalIo_t satIntIoContext\n"));
2379     satOrgIOContext        = satIOContext->satOrgIOContext;
2380     if (satOrgIOContext == agNULL)
2381     {
2382       SM_DBG4(("smsatRequestSenseCB: satOrgIOContext is NULL, wrong\n"));
2383       return;
2384     }
2385     else
2386     {
2387       SM_DBG4(("smsatRequestSenseCB: satOrgIOContext is NOT NULL\n"));
2388     }
2389     smOrgIORequestBody     = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
2390     smOrgIORequest         = (smIORequest_t *)smOrgIORequestBody->smIORequest;
2391 
2392     if (satOrgIOContext->superIOFlag)
2393     {
2394       pDataBuffer = (bit8 *)(((tiSuperScsiInitiatorRequest_t *)satOrgIOContext->smScsiXchg)->sglVirtualAddr);//satOrgIOContext->pSense;
2395     }
2396     else
2397     {
2398       pDataBuffer = (bit8 *)(((tiScsiInitiatorRequest_t *) satOrgIOContext->smScsiXchg)->sglVirtualAddr);//satOrgIOContext->pSense;
2399 
2400     }
2401     scsiCmnd               = satOrgIOContext->pScsiCmnd;
2402     pSense                 = satOrgIOContext->pSense;
2403   }
2404 
2405   smIORequestBody->ioCompleted = agTRUE;
2406   smIORequestBody->ioStarted = agFALSE;
2407 
2408   SM_DBG4(("smsatRequestSenseCB: fis command 0x%x\n", hostToDevFis->h.command));
2409 
2410   allocationLen = scsiCmnd->cdb[4];
2411   allocationLen = MIN(allocationLen, scsiCmnd->expDataLength);
2412   SM_DBG1(("smsatRequestSenseCB: allocationLen in CDB %d 0x%x!!!\n", allocationLen,allocationLen));
2413 
2414   if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
2415   {
2416     SM_DBG1(("smsatRequestSenseCB: wrong. agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
2417     tdsmIOCompletedCB(
2418                        smRoot,
2419                        smOrgIORequest,
2420                        smIOFailed,
2421                        smDetailOtherError,
2422                        agNULL,
2423                        satOrgIOContext->interruptContext
2424                       );
2425 
2426     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
2427 
2428     smsatFreeIntIoResource( smRoot,
2429                             oneDeviceData,
2430                             satIntIo);
2431 
2432     return;
2433   }
2434 
2435   /*
2436     checking IO status, FIS type and error status
2437   */
2438   if (agIOStatus != OSSA_IO_SUCCESS)
2439   {
2440     /* only agsaFisRegDeviceToHost_t is expected */
2441     statDevToHostFisHeader = (agsaFisRegD2HHeader_t *)&(agFirstDword->D2H);
2442     ataStatus     = statDevToHostFisHeader->status;   /* ATA Status register */
2443 
2444     /* for debugging */
2445     if( statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS)
2446     {
2447       if (hostToDevFis->h.command == SAT_SMART && hostToDevFis->h.features == SAT_SMART_RETURN_STATUS)
2448       {
2449         SM_DBG1(("smsatRequestSenseCB: FAILED, Wrong FIS type 0x%x and SAT_SMART_RETURN_STATU!!!\n", statDevToHostFisHeader->fisType));
2450       }
2451       else
2452       {
2453         SM_DBG1(("smsatRequestSenseCB: FAILED, Wrong FIS type 0x%x and SAT_CHECK_POWER_MODE!!!\n",statDevToHostFisHeader->fisType));
2454       }
2455     }
2456 
2457     /* for debugging */
2458     if ( (ataStatus & ERR_ATA_STATUS_MASK) ||
2459          (ataStatus & DF_ATA_STATUS_MASK)
2460          )
2461     {
2462       if (hostToDevFis->h.command == SAT_SMART && hostToDevFis->h.features == SAT_SMART_RETURN_STATUS)
2463       {
2464         SM_DBG1(("smsatRequestSenseCB: FAILED, error status and SAT_SMART_RETURN_STATU!!!\n"));
2465       }
2466       else
2467       {
2468         SM_DBG1(("smsatRequestSenseCB: FAILED, error status and SAT_CHECK_POWER_MODE!!!\n"));
2469       }
2470     }
2471 
2472     /* Process abort case */
2473     if (agIOStatus == OSSA_IO_ABORTED)
2474     {
2475       smsatProcessAbort(smRoot,
2476                         smOrgIORequest,
2477                         satOrgIOContext
2478                         );
2479       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
2480 
2481       smsatFreeIntIoResource( smRoot,
2482                               oneDeviceData,
2483                               satIntIo);
2484       return;
2485     }
2486     if (hostToDevFis->h.command == SAT_SMART && hostToDevFis->h.features == SAT_SMART_RETURN_STATUS)
2487     {
2488       /* report using the original tiIOrequst */
2489       /* failed during sending SMART RETURN STATUS */
2490       smsatSetSensePayload( pSense,
2491                             SCSI_SNSKEY_NO_SENSE,
2492                             0,
2493                             SCSI_SNSCODE_HARDWARE_IMPENDING_FAILURE,
2494                             satOrgIOContext);
2495       sm_memcpy(pDataBuffer, pSense, MIN(SENSE_DATA_LENGTH, allocationLen));
2496       if (SENSE_DATA_LENGTH < allocationLen)
2497       {
2498         /* underrun */
2499         tdsmIOCompletedCB( smRoot,
2500                            smOrgIORequest, /* == satIntIo->satOrgSmIORequest */
2501                            smIOUnderRun,
2502                            allocationLen - SENSE_DATA_LENGTH,
2503                            agNULL,
2504                            satOrgIOContext->interruptContext );
2505       }
2506       else
2507       {
2508         tdsmIOCompletedCB( smRoot,
2509                            smOrgIORequest, /* == satIntIo->satOrgSmIORequest */
2510                            smIOSuccess,
2511                            SCSI_STAT_GOOD,
2512                            agNULL,
2513                            satOrgIOContext->interruptContext );
2514       }
2515     }
2516     else
2517     {
2518       /* report using the original tiIOrequst */
2519       /* failed during sending SAT_CHECK_POWER_MODE */
2520       smsatSetSensePayload( pSense,
2521                             SCSI_SNSKEY_NO_SENSE,
2522                             0,
2523                             SCSI_SNSCODE_LOW_POWER_CONDITION_ON,
2524                             satOrgIOContext);
2525       sm_memcpy(pDataBuffer, pSense, MIN(SENSE_DATA_LENGTH, allocationLen));
2526 
2527       if (SENSE_DATA_LENGTH < allocationLen)
2528       {
2529         /* underrun */
2530         tdsmIOCompletedCB( smRoot,
2531                            smOrgIORequest, /* == satIntIo->satOrgSmIORequest */
2532                            smIOUnderRun,
2533                            allocationLen - SENSE_DATA_LENGTH,
2534                            agNULL,
2535                            satOrgIOContext->interruptContext );
2536        }
2537        else
2538        {
2539         tdsmIOCompletedCB( smRoot,
2540                            smOrgIORequest, /* == satIntIo->satOrgSmIORequest */
2541                            smIOSuccess,
2542                            SCSI_STAT_GOOD,
2543                            agNULL,
2544                            satOrgIOContext->interruptContext );
2545        }
2546     }
2547 
2548 
2549     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
2550 
2551     smsatFreeIntIoResource( smRoot,
2552                             oneDeviceData,
2553                             satIntIo);
2554     return;
2555   }
2556 
2557   saFrameReadBlock(agRoot, agParam, 0, &statDevToHostFisData, sizeof(agsaFisRegD2HData_t));
2558 
2559   switch (hostToDevFis->h.command)
2560   {
2561   case SAT_SMART:
2562     SM_DBG4(("smsatRequestSenseCB: SAT_SMART_RETURN_STATUS case\n"));
2563     if (statDevToHostFisData.lbaMid == 0xF4 || statDevToHostFisData.lbaHigh == 0x2C)
2564     {
2565       /* threshold exceeds */
2566       SM_DBG1(("smsatRequestSenseCB: threshold exceeds!!!\n"));
2567 
2568 
2569       /* report using the original tiIOrequst */
2570       /* failed during sending SMART RETURN STATUS */
2571       smsatSetSensePayload( pSense,
2572                             SCSI_SNSKEY_NO_SENSE,
2573                             0,
2574                             SCSI_SNSCODE_HARDWARE_IMPENDING_FAILURE,
2575                             satOrgIOContext);
2576       sm_memcpy(pDataBuffer, pSense, MIN(SENSE_DATA_LENGTH, allocationLen));
2577 
2578       if (SENSE_DATA_LENGTH < allocationLen)
2579       {
2580         /* underrun */
2581         tdsmIOCompletedCB( smRoot,
2582                            smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
2583                            smIOUnderRun,
2584                            allocationLen - SENSE_DATA_LENGTH,
2585                            agNULL,
2586                            satOrgIOContext->interruptContext );
2587       }
2588       else
2589       {
2590         tdsmIOCompletedCB( smRoot,
2591                            smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
2592                            smIOSuccess,
2593                            SCSI_STAT_GOOD,
2594                            agNULL,
2595                            satOrgIOContext->interruptContext );
2596       }
2597 
2598 
2599       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
2600 
2601       smsatFreeIntIoResource( smRoot,
2602                               oneDeviceData,
2603                               satIntIo);
2604       return;
2605     }
2606 
2607 
2608     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
2609 
2610     /* done with internally genereated SAT_SMART_RETURN_STATUS */
2611     smsatFreeIntIoResource( smRoot,
2612                             oneDeviceData,
2613                             satIntIo);
2614 
2615     /* at this point, successful SMART_RETURN_STATUS
2616        xmit SAT_CHECK_POWER_MODE
2617     */
2618     if (satOrgIOContext->superIOFlag)
2619     {
2620         dataLength = ((tiSuperScsiInitiatorRequest_t *) satOrgIOContext->smScsiXchg)->scsiCmnd.expDataLength;
2621     }
2622     else
2623     {
2624         dataLength = ((tiScsiInitiatorRequest_t *) satOrgIOContext->smScsiXchg)->scsiCmnd.expDataLength;
2625     }
2626 
2627     satNewIntIo = smsatAllocIntIoResource( smRoot,
2628                                            smOrgIORequest,
2629                                            oneDeviceData,
2630                                            dataLength,
2631                                            satNewIntIo);
2632     if (satNewIntIo == agNULL)
2633     {
2634       /* failed as a part of sending SMART RETURN STATUS */
2635       smsatSetSensePayload( pSense,
2636                             SCSI_SNSKEY_NO_SENSE,
2637                             0,
2638                             SCSI_SNSCODE_HARDWARE_IMPENDING_FAILURE,
2639                             satOrgIOContext);
2640       sm_memcpy(pDataBuffer, pSense, MIN(SENSE_DATA_LENGTH, allocationLen));
2641 
2642       if (SENSE_DATA_LENGTH < allocationLen)
2643       {
2644         /* underrun */
2645         tdsmIOCompletedCB( smRoot,
2646                            smOrgIORequest,
2647                            smIOUnderRun,
2648                            allocationLen - SENSE_DATA_LENGTH,
2649                            agNULL,
2650                            satOrgIOContext->interruptContext );
2651       }
2652       else
2653       {
2654         tdsmIOCompletedCB( smRoot,
2655                            smOrgIORequest,
2656                            smIOSuccess,
2657                            SCSI_STAT_GOOD,
2658                            agNULL,
2659                            satOrgIOContext->interruptContext );
2660       }
2661 
2662       SM_DBG1(("smsatRequestSenseCB: momory allocation fails!!!\n"));
2663       return;
2664     } /* end of memory allocation failure */
2665 
2666 
2667     /*
2668      * Need to initialize all the fields within satIOContext
2669      */
2670 
2671     satNewIOContext = smsatPrepareNewIO(
2672                                         satNewIntIo,
2673                                         smOrgIORequest,
2674                                         oneDeviceData,
2675                                         scsiCmnd,
2676                                         satOrgIOContext
2677                                         );
2678 
2679     /* sending SAT_CHECK_POWER_MODE */
2680     status = smsatRequestSense_1( smRoot,
2681                                   &satNewIntIo->satIntSmIORequest,
2682                                   satNewIOContext->psmDeviceHandle,
2683                                   &satNewIntIo->satIntSmScsiXchg,
2684                                   satNewIOContext);
2685 
2686     if (status != SM_RC_SUCCESS)
2687     {
2688       /* sending SAT_CHECK_POWER_MODE fails */
2689       smsatFreeIntIoResource( smRoot,
2690                               oneDeviceData,
2691                               satNewIntIo);
2692 
2693       /* failed during sending SAT_CHECK_POWER_MODE */
2694       smsatSetSensePayload( pSense,
2695                             SCSI_SNSKEY_NO_SENSE,
2696                             0,
2697                             SCSI_SNSCODE_LOW_POWER_CONDITION_ON,
2698                             satOrgIOContext);
2699       sm_memcpy(pDataBuffer, pSense, MIN(SENSE_DATA_LENGTH, allocationLen));
2700 
2701       if (SENSE_DATA_LENGTH < allocationLen)
2702       {
2703         /* underrun */
2704         tdsmIOCompletedCB( smRoot,
2705                            smOrgIORequest,
2706                            smIOUnderRun,
2707                            allocationLen - SENSE_DATA_LENGTH,
2708                            agNULL,
2709                            satOrgIOContext->interruptContext );
2710       }
2711       else
2712       {
2713         tdsmIOCompletedCB( smRoot,
2714                            smOrgIORequest,
2715                            smIOSuccess,
2716                            SCSI_STAT_GOOD,
2717                            agNULL,
2718                            satOrgIOContext->interruptContext );
2719       }
2720 
2721       SM_DBG1(("smsatRequestSenseCB: calling satRequestSense_1 fails!!!\n"));
2722       return;
2723     }
2724 
2725     break;
2726   case SAT_CHECK_POWER_MODE:
2727     SM_DBG4(("smsatRequestSenseCB: SAT_CHECK_POWER_MODE case\n"));
2728 
2729     /* check ATA STANDBY state */
2730     if (statDevToHostFisData.sectorCount == 0x00)
2731     {
2732       /* in STANDBY */
2733       SM_DBG1(("smsatRequestSenseCB: in standby!!!\n"));
2734 
2735 
2736       /* report using the original tiIOrequst */
2737       /* failed during sending SAT_CHECK_POWER_MODE */
2738       smsatSetSensePayload( pSense,
2739                             SCSI_SNSKEY_NO_SENSE,
2740                             0,
2741                             SCSI_SNSCODE_LOW_POWER_CONDITION_ON,
2742                             satOrgIOContext);
2743       sm_memcpy(pDataBuffer, pSense, MIN(SENSE_DATA_LENGTH, allocationLen));
2744 
2745       if (SENSE_DATA_LENGTH < allocationLen)
2746       {
2747         /* underrun */
2748         tdsmIOCompletedCB( smRoot,
2749                            smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
2750                            smIOUnderRun,
2751                            allocationLen - SENSE_DATA_LENGTH,
2752                            agNULL,
2753                            satOrgIOContext->interruptContext );
2754       }
2755       else
2756       {
2757         tdsmIOCompletedCB( smRoot,
2758                            smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
2759                            smIOSuccess,
2760                            SCSI_STAT_GOOD,
2761                            agNULL,
2762                            satOrgIOContext->interruptContext );
2763       }
2764 
2765       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
2766 
2767       smsatFreeIntIoResource( smRoot,
2768                               oneDeviceData,
2769                               satIntIo);
2770       return;
2771     }
2772 
2773     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
2774 
2775     /* done with internnaly generated SAT_CHECK_POWER_MODE */
2776     smsatFreeIntIoResource( smRoot,
2777                             oneDeviceData,
2778                             satIntIo);
2779 
2780     if (oneDeviceData->satFormatState == agTRUE)
2781     {
2782       SM_DBG1(("smsatRequestSenseCB: in format!!!\n"));
2783 
2784 
2785       /* report using the original tiIOrequst */
2786       smsatSetSensePayload( pSense,
2787                             SCSI_SNSKEY_NOT_READY,
2788                             0,
2789                             SCSI_SNSCODE_LOGICAL_UNIT_NOT_READY_FORMAT_IN_PROGRESS,
2790                             satOrgIOContext);
2791       sm_memcpy(pDataBuffer, pSense, MIN(SENSE_DATA_LENGTH, allocationLen));
2792 
2793       if (SENSE_DATA_LENGTH < allocationLen)
2794       {
2795         /* underrun */
2796         tdsmIOCompletedCB( smRoot,
2797                            smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
2798                            smIOUnderRun,
2799                            allocationLen - SENSE_DATA_LENGTH,
2800                            agNULL,
2801                            satOrgIOContext->interruptContext );
2802       }
2803       else
2804       {
2805         tdsmIOCompletedCB( smRoot,
2806                            smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
2807                            smIOSuccess,
2808                            SCSI_STAT_GOOD,
2809                            agNULL,
2810                            satOrgIOContext->interruptContext );
2811       }
2812 
2813       return;
2814     }
2815 
2816     /* normal: returns good status for requestsense */
2817     /* report using the original tiIOrequst */
2818     smsatSetSensePayload( pSense,
2819                           SCSI_SNSKEY_NO_SENSE,
2820                           0,
2821                           SCSI_SNSCODE_NO_ADDITIONAL_INFO,
2822                           satOrgIOContext);
2823     sm_memcpy(pDataBuffer, pSense, MIN(SENSE_DATA_LENGTH, allocationLen));
2824     SM_DBG4(("smsatRequestSenseCB: returning good status for requestsense\n"));
2825     if (SENSE_DATA_LENGTH < allocationLen)
2826     {
2827       /* underrun */
2828       SM_DBG6(("smsatRequestSenseCB reporting underrun lenNeeded=0x%x lenReceived=0x%x smIORequest=%p\n",
2829         SENSE_DATA_LENGTH, allocationLen, smOrgIORequest));
2830       tdsmIOCompletedCB( smRoot,
2831                          smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
2832                          smIOUnderRun,
2833                          allocationLen - SENSE_DATA_LENGTH,
2834                          agNULL,
2835                          satOrgIOContext->interruptContext );
2836 
2837     }
2838     else
2839     {
2840       tdsmIOCompletedCB( smRoot,
2841                          smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
2842                          smIOSuccess,
2843                          SCSI_STAT_GOOD,
2844                          agNULL,
2845                          satOrgIOContext->interruptContext );
2846     }
2847 
2848     break;
2849   default:
2850      SM_DBG1(("smsatRequestSenseCB: success but error default case command 0x%x!!!\n", hostToDevFis->h.command));
2851      /* pSense here is a part of satOrgIOContext */
2852      pSense = satOrgIOContext->pSmSenseData->senseData;
2853      satOrgIOContext->pSmSenseData->senseLen = SENSE_DATA_LENGTH;
2854      /* unspecified case, return no sense and no addition info */
2855      smsatSetSensePayload( pSense,
2856                            SCSI_SNSKEY_NO_SENSE,
2857                            0,
2858                            SCSI_SNSCODE_NO_ADDITIONAL_INFO,
2859                            satOrgIOContext);
2860      sm_memcpy(pDataBuffer, pSense, MIN(SENSE_DATA_LENGTH, allocationLen));
2861 
2862      tdsmIOCompletedCB( smRoot,
2863                         smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
2864                         smIOSuccess,
2865                         SCSI_STAT_CHECK_CONDITION,
2866                         satOrgIOContext->pSmSenseData,
2867                         satOrgIOContext->interruptContext );
2868 
2869      smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
2870 
2871      smsatFreeIntIoResource( smRoot,
2872                              oneDeviceData,
2873                              satIntIo);
2874     break;
2875   } /* switch */
2876 
2877   return;
2878 
2879 }
2880 
2881 osGLOBAL void
2882 smsatSendDiagnosticCB(
2883                        agsaRoot_t        *agRoot,
2884                        agsaIORequest_t   *agIORequest,
2885                        bit32             agIOStatus,
2886                        agsaFisHeader_t   *agFirstDword,
2887                        bit32             agIOInfoLen,
2888                        agsaFrameHandle_t agFrameHandle,
2889                        void              *ioContext
2890                      )
2891 {
2892   /*
2893     In the process of SendDiagnotic
2894     Process READ VERIFY SECTOR(S) EXT two time
2895     Process SMART ECECUTE OFF-LINE IMMEDIATE
2896   */
2897 //  tdsaRootOsData_t        *osData = (tdsaRootOsData_t *)agRoot->osData;
2898 //  tiRoot_t                *tiRoot = (tiRoot_t *)osData->tiRoot;
2899 //  tdsaRoot_t              *tdsaRoot        = (tdsaRoot_t *) tiRoot->tdData;
2900 //  tdsaContext_t           *tdsaAllShared = (tdsaContext_t *)&tdsaRoot->tdsaAllShared;
2901   smRoot_t                 *smRoot = agNULL;
2902   smIntRoot_t              *smIntRoot = agNULL;
2903   smIntContext_t           *smAllShared = agNULL;
2904   smIORequestBody_t        *smIORequestBody;
2905   smIORequestBody_t        *smOrgIORequestBody;
2906   smSatIOContext_t         *satIOContext;
2907   smSatIOContext_t         *satOrgIOContext;
2908   smSatIOContext_t         *satNewIOContext;
2909   smSatInternalIo_t        *satIntIo;
2910   smSatInternalIo_t        *satNewIntIo = agNULL;
2911 //  satDeviceData_t         *satDevData;
2912   smDeviceData_t           *oneDeviceData;
2913   smScsiRspSense_t         *pSense;
2914   smIniScsiCmnd_t          *scsiCmnd;
2915   smIORequest_t            *smOrgIORequest;
2916   agsaFisRegHostToDevice_t *hostToDevFis = agNULL;
2917   bit32                     ataStatus = 0;
2918 
2919   bit32                     status;
2920   agsaFisRegD2HHeader_t    *statDevToHostFisHeader = agNULL;
2921 
2922   SM_DBG2(("smsatSendDiagnosticCB: start\n"));
2923   SM_DBG5(("smsatSendDiagnosticCB:agIORequest=%p agIOStatus=0x%x agIOInfoLen %d\n", agIORequest, agIOStatus, agIOInfoLen));
2924 
2925   /* internally generate smIOContext */
2926   smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
2927   satIOContext           = (smSatIOContext_t *) ioContext;
2928   satIntIo               = satIOContext->satIntIoContext;
2929   oneDeviceData          = satIOContext->pSatDevData;
2930   hostToDevFis           = satIOContext->pFis;
2931   smRoot                 = oneDeviceData->smRoot;
2932   smIntRoot              = (smIntRoot_t *)smRoot->smData;
2933   smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
2934 
2935   if (satIntIo == agNULL)
2936   {
2937     SM_DBG4(("smsatSendDiagnosticCB: External smSatInternalIo_t satIntIoContext\n"));
2938     satOrgIOContext = satIOContext;
2939     smOrgIORequest  = smIORequestBody->smIORequest;
2940     pSense          = satOrgIOContext->pSense;
2941     scsiCmnd        = satOrgIOContext->pScsiCmnd;
2942   }
2943   else
2944   {
2945     SM_DBG4(("smsatSendDiagnosticCB: Internal smSatInternalIo_t satIntIoContext\n"));
2946     satOrgIOContext        = satIOContext->satOrgIOContext;
2947     if (satOrgIOContext == agNULL)
2948     {
2949       SM_DBG4(("smsatSendDiagnosticCB: satOrgIOContext is NULL, wrong\n"));
2950       return;
2951     }
2952     else
2953     {
2954       SM_DBG4(("smsatSendDiagnosticCB: satOrgIOContext is NOT NULL\n"));
2955     }
2956     smOrgIORequestBody     = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
2957     smOrgIORequest         = (smIORequest_t *)smOrgIORequestBody->smIORequest;
2958     pSense                 = satOrgIOContext->pSense;
2959     scsiCmnd               = satOrgIOContext->pScsiCmnd;
2960   }
2961 
2962   smIORequestBody->ioCompleted = agTRUE;
2963   smIORequestBody->ioStarted = agFALSE;
2964 
2965   if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
2966   {
2967      SM_DBG1(("smsatSendDiagnosticCB: wrong. agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
2968      oneDeviceData->satVerifyState = 0;
2969      oneDeviceData->satBGPendingDiag = agFALSE;
2970 
2971     if (hostToDevFis->d.lbaLow != 0x01 && hostToDevFis->d.lbaLow != 0x02)
2972     {
2973       /* no completion for background send diagnotic. It is done in satSendDiagnostic() */
2974       tdsmIOCompletedCB(
2975                          smRoot,
2976                          smOrgIORequest,
2977                          smIOFailed,
2978                          smDetailOtherError,
2979                          agNULL,
2980                          satOrgIOContext->interruptContext
2981                         );
2982      }
2983      smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
2984 
2985      smsatFreeIntIoResource( smRoot,
2986                              oneDeviceData,
2987                               satIntIo);
2988     return;
2989 
2990   }
2991 
2992   if (agIOStatus != OSSA_IO_SUCCESS)
2993   {
2994     /* only agsaFisRegDeviceToHost_t is expected */
2995     statDevToHostFisHeader = (agsaFisRegD2HHeader_t *)&(agFirstDword->D2H);
2996     ataStatus     = statDevToHostFisHeader->status;   /* ATA Status register */
2997   }
2998 
2999   SM_DBG5(("smsatSendDiagnosticCB: fis command 0x%x\n", hostToDevFis->h.command));
3000 
3001   if( agIOStatus != OSSA_IO_SUCCESS)
3002   {
3003   /*
3004     checking IO status, FIS type and error status
3005   */
3006   oneDeviceData->satVerifyState = 0;
3007   oneDeviceData->satBGPendingDiag = agFALSE;
3008 
3009   if( (statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS) ||
3010       ((ataStatus & ERR_ATA_STATUS_MASK) || (ataStatus & DF_ATA_STATUS_MASK))
3011       )
3012   {
3013 
3014     /* for debugging */
3015     if( agIOStatus != OSSA_IO_SUCCESS)
3016     {
3017       if ( hostToDevFis->h.command == SAT_READ_VERIFY_SECTORS_EXT )
3018       {
3019         SM_DBG1(("smsatSendDiagnosticCB: FAILED, NOT IO_SUCCESS and SAT_READ_VERIFY_SECTORS(_EXT)!!!\n"));
3020       }
3021       else
3022       {
3023         SM_DBG1(("smsatSendDiagnosticCB: FAILED, NOT IO_SUCCESS and SAT_SMART_EXEUTE_OFF_LINE_IMMEDIATE!!!\n"));
3024       }
3025     }
3026 
3027     /* for debugging */
3028     if( statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS)
3029     {
3030       if ( hostToDevFis->h.command == SAT_READ_VERIFY_SECTORS_EXT )
3031       {
3032         SM_DBG1(("smsatSendDiagnosticCB: FAILED, Wrong FIS type 0x%x and SAT_READ_VERIFY_SECTORS(_EXT)!!!\n", statDevToHostFisHeader->fisType));
3033       }
3034       else
3035       {
3036         SM_DBG1(("smsatSendDiagnosticCB: FAILED, Wrong FIS type 0x%x and SAT_SMART_EXEUTE_OFF_LINE_IMMEDIATE!!!\n",statDevToHostFisHeader->fisType));
3037       }
3038     }
3039 
3040     /* for debugging */
3041     if ( (ataStatus & ERR_ATA_STATUS_MASK) ||
3042          (ataStatus & DF_ATA_STATUS_MASK)
3043          )
3044     {
3045       if ( hostToDevFis->h.command == SAT_READ_VERIFY_SECTORS_EXT )
3046       {
3047         SM_DBG1(("smsatSendDiagnosticCB: FAILED, error status and SAT_READ_VERIFY_SECTORS(_EXT)!!!\n"));
3048       }
3049       else
3050       {
3051         SM_DBG1(("smsatSendDiagnosticCB: FAILED, error status and SAT_SMART_EXEUTE_OFF_LINE_IMMEDIATE!!!\n"));
3052       }
3053     }
3054 
3055     /* Process abort case */
3056     if (agIOStatus == OSSA_IO_ABORTED)
3057     {
3058       smsatProcessAbort(smRoot,
3059                         smOrgIORequest,
3060                         satOrgIOContext
3061                         );
3062 
3063       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
3064 
3065       smsatFreeIntIoResource( smRoot,
3066                               oneDeviceData,
3067                               satIntIo);
3068       return;
3069     }
3070 
3071     if ( (hostToDevFis->h.command == SAT_READ_VERIFY_SECTORS) ||
3072          (hostToDevFis->h.command == SAT_READ_VERIFY_SECTORS_EXT) )
3073     {
3074       /* report using the original tiIOrequst */
3075       /* failed during sending SAT_READ_VERIFY_SECTORS(_EXT) */
3076       smsatSetSensePayload( pSense,
3077                             SCSI_SNSKEY_HARDWARE_ERROR,
3078                             0,
3079                             SCSI_SNSCODE_LOGICAL_UNIT_FAILED_SELF_TEST,
3080                             satOrgIOContext);
3081 
3082       tdsmIOCompletedCB( smRoot,
3083                          smOrgIORequest, /* == satIntIo->satOrgSmIORequest */
3084                          smIOSuccess,
3085                          SCSI_STAT_CHECK_CONDITION,
3086                          satOrgIOContext->pSmSenseData,
3087                          satOrgIOContext->interruptContext );
3088 
3089       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
3090 
3091       smsatFreeIntIoResource( smRoot,
3092                               oneDeviceData,
3093                               satIntIo);
3094       return;
3095     }
3096     else
3097     {
3098       /* report using the original tiIOrequst */
3099       /* failed during sending SAT_SMART_EXEUTE_OFF_LINE_IMMEDIATE */
3100       smsatSetSensePayload( pSense,
3101                             SCSI_SNSKEY_HARDWARE_ERROR,
3102                             0,
3103                             SCSI_SNSCODE_LOGICAL_UNIT_FAILED_SELF_TEST,
3104                             satOrgIOContext);
3105 
3106       if (hostToDevFis->d.lbaLow != 0x01 && hostToDevFis->d.lbaLow != 0x02)
3107       {
3108         /* no completion for background send diagnotic. It is done in satSendDiagnostic() */
3109         tdsmIOCompletedCB( smRoot,
3110                            smOrgIORequest, /* == satIntIo->satOrgSmIORequest */
3111                            smIOSuccess,
3112                            SCSI_STAT_CHECK_CONDITION,
3113                            satOrgIOContext->pSmSenseData,
3114                            satOrgIOContext->interruptContext );
3115 
3116       }
3117       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
3118 
3119       smsatFreeIntIoResource( smRoot,
3120                               oneDeviceData,
3121                               satIntIo);
3122       return;
3123     }
3124   }
3125   }
3126 
3127   /* processing success case */
3128   switch (hostToDevFis->h.command)
3129   {
3130   case SAT_READ_VERIFY_SECTORS:     /* fall through */
3131   case SAT_READ_VERIFY_SECTORS_EXT:
3132     SM_DBG5(("smsatSendDiagnosticCB: SAT_READ_VERIFY_SECTORS(_EXT) case\n"));
3133     tdsmSingleThreadedEnter(smRoot, SM_EXTERNAL_IO_LOCK);
3134     oneDeviceData->satVerifyState++;
3135     tdsmSingleThreadedLeave(smRoot, SM_EXTERNAL_IO_LOCK);
3136     SM_DBG5(("smsatSendDiagnosticCB: satVerifyState %d\n",oneDeviceData->satVerifyState));
3137 
3138     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
3139 
3140     /* done with internally genereated AT_READ_VERIFY_SECTORS(_EXT) */
3141     smsatFreeIntIoResource( smRoot,
3142                             oneDeviceData,
3143                             satIntIo);
3144 
3145     if (oneDeviceData->satVerifyState == 3)
3146     {
3147       /* reset satVerifyState */
3148       oneDeviceData->satVerifyState = 0;
3149       /* return GOOD status */
3150       SM_DBG5(("smsatSendDiagnosticCB: return GOOD status\n"));
3151       tdsmIOCompletedCB( smRoot,
3152                          smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
3153                          smIOSuccess,
3154                          SCSI_STAT_GOOD,
3155                          agNULL,
3156                          satOrgIOContext->interruptContext );
3157      return;
3158     }
3159     else
3160     {
3161       /* prepare SAT_READ_VERIFY_SECTORS(_EXT) */
3162       satNewIntIo = smsatAllocIntIoResource( smRoot,
3163                                              smOrgIORequest,
3164                                              oneDeviceData,
3165                                              0,
3166                                              satNewIntIo);
3167       if (satNewIntIo == agNULL)
3168       {
3169         /* reset satVerifyState */
3170         oneDeviceData->satVerifyState = 0;
3171 
3172         /* failed as a part of sending SAT_READ_VERIFY_SECTORS(_EXT) */
3173         smsatSetSensePayload( pSense,
3174                               SCSI_SNSKEY_HARDWARE_ERROR,
3175                               0,
3176                               SCSI_SNSCODE_LOGICAL_UNIT_FAILED_SELF_TEST,
3177                               satOrgIOContext);
3178 
3179         tdsmIOCompletedCB( smRoot,
3180                            smOrgIORequest,
3181                            smIOSuccess,
3182                            SCSI_STAT_CHECK_CONDITION,
3183                            satOrgIOContext->pSmSenseData,
3184                            satOrgIOContext->interruptContext );
3185 
3186         SM_DBG1(("smsatSendDiagnosticCB: momory allocation fails!!!\n"));
3187         return;
3188       } /* end of memory allocation failure */
3189 
3190       /*
3191        * Need to initialize all the fields within satIOContext
3192        */
3193 
3194       satNewIOContext = smsatPrepareNewIO(
3195                                           satNewIntIo,
3196                                           smOrgIORequest,
3197                                           oneDeviceData,
3198                                           scsiCmnd,
3199                                           satOrgIOContext
3200                                           );
3201 
3202       if (oneDeviceData->satVerifyState == 1)
3203       {
3204         /* sending SAT_CHECK_POWER_MODE */
3205         status = smsatSendDiagnostic_1( smRoot,
3206                                         &satNewIntIo->satIntSmIORequest,
3207                                         satNewIOContext->psmDeviceHandle,
3208                                         &satNewIntIo->satIntSmScsiXchg,
3209                                         satNewIOContext);
3210       }
3211       else
3212       {
3213         /* oneDeviceData->satVerifyState == 2 */
3214         status = smsatSendDiagnostic_2( smRoot,
3215                                         &satNewIntIo->satIntSmIORequest,
3216                                         satNewIOContext->psmDeviceHandle,
3217                                         &satNewIntIo->satIntSmScsiXchg,
3218                                         satNewIOContext);
3219       }
3220 
3221       if (status != SM_RC_SUCCESS)
3222       {
3223         /* sending SAT_READ_VERIFY_SECTORS(_EXT) fails */
3224         smsatFreeIntIoResource( smRoot,
3225                                 oneDeviceData,
3226                                 satNewIntIo);
3227 
3228         /* failed during sending SAT_READ_VERIFY_SECTORS(_EXT) */
3229         smsatSetSensePayload( pSense,
3230                               SCSI_SNSKEY_HARDWARE_ERROR,
3231                               0,
3232                               SCSI_SNSCODE_LOGICAL_UNIT_FAILED_SELF_TEST,
3233                               satOrgIOContext);
3234 
3235         tdsmIOCompletedCB( smRoot,
3236                            smOrgIORequest,
3237                            smIOSuccess,
3238                            SCSI_STAT_CHECK_CONDITION,
3239                            satOrgIOContext->pSmSenseData,
3240                            satOrgIOContext->interruptContext );
3241 
3242         /* reset satVerifyState */
3243         oneDeviceData->satVerifyState = 0;
3244         SM_DBG1(("smsatSendDiagnosticCB: calling satSendDiagnostic_1 or _2 fails!!!\n"));
3245         return;
3246       }
3247     } /* oneDeviceData->satVerifyState == 1 or 2 */
3248 
3249     break;
3250   case SAT_SMART:
3251     if (hostToDevFis->h.features == SAT_SMART_EXEUTE_OFF_LINE_IMMEDIATE)
3252     {
3253       SM_DBG5(("smsatSendDiagnosticCB: SAT_SMART_EXEUTE_OFF_LINE_IMMEDIATE case\n"));
3254 
3255       oneDeviceData->satBGPendingDiag = agFALSE;
3256 
3257       if (hostToDevFis->d.lbaLow == 0x01 || hostToDevFis->d.lbaLow == 0x02)
3258       {
3259         /* for background send diagnostic, no completion here. It is done already. */
3260         smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
3261 
3262         /* done with AT_SMART_EXEUTE_OFF_LINE_IMMEDIATE */
3263         smsatFreeIntIoResource( smRoot,
3264                                 oneDeviceData,
3265                                 satIntIo);
3266         SM_DBG5(("smsatSendDiagnosticCB: returning but no IOCompleted\n"));
3267       }
3268       else
3269       {
3270         SM_DBG5(("smsatSendDiagnosticCB: returning good status for senddiagnostic\n"));
3271         tdsmIOCompletedCB( smRoot,
3272                            smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
3273                            smIOSuccess,
3274                            SCSI_STAT_GOOD,
3275                            agNULL,
3276                            satOrgIOContext->interruptContext );
3277 
3278 
3279         smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
3280 
3281         /* done with AT_SMART_EXEUTE_OFF_LINE_IMMEDIATE */
3282         smsatFreeIntIoResource( smRoot,
3283                                 oneDeviceData,
3284                                 satIntIo);
3285       }
3286     }
3287     break;
3288   default:
3289     SM_DBG1(("smsatSendDiagnosticCB: success but error default case command 0x%x!!!\n", hostToDevFis->h.command));
3290     /* unspecified case, return no sense and no addition info */
3291     smsatSetSensePayload( pSense,
3292                           SCSI_SNSKEY_NO_SENSE,
3293                           0,
3294                           SCSI_SNSCODE_NO_ADDITIONAL_INFO,
3295                           satOrgIOContext);
3296 
3297     tdsmIOCompletedCB( smRoot,
3298                        smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
3299                        smIOSuccess,
3300                        SCSI_STAT_CHECK_CONDITION,
3301                        satOrgIOContext->pSmSenseData,
3302                        satOrgIOContext->interruptContext );
3303 
3304     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
3305 
3306     smsatFreeIntIoResource( smRoot,
3307                             oneDeviceData,
3308                             satIntIo);
3309     break;
3310   }
3311   return;
3312 
3313 }
3314 
3315 osGLOBAL void
3316 smsatStartStopUnitCB(
3317                      agsaRoot_t        *agRoot,
3318                      agsaIORequest_t   *agIORequest,
3319                      bit32             agIOStatus,
3320                      agsaFisHeader_t   *agFirstDword,
3321                      bit32             agIOInfoLen,
3322                      agsaFrameHandle_t agFrameHandle,
3323                      void              *ioContext
3324                     )
3325 {
3326   /*
3327     In the process of StartStopUnit
3328     Process FLUSH CACHE (EXT)
3329     Process STANDBY
3330     Process READ VERIFY SECTOR(S) EXT
3331     Process MEDIA EJECT
3332   */
3333 //  tdsaRootOsData_t        *osData = (tdsaRootOsData_t *)agRoot->osData;
3334 //  tiRoot_t                *tiRoot = (tiRoot_t *)osData->tiRoot;
3335 //  tdsaRoot_t              *tdsaRoot        = (tdsaRoot_t *) tiRoot->tdData;
3336 //  tdsaContext_t           *tdsaAllShared = (tdsaContext_t *)&tdsaRoot->tdsaAllShared;
3337   smRoot_t                 *smRoot = agNULL;
3338   smIntRoot_t              *smIntRoot = agNULL;
3339   smIntContext_t           *smAllShared = agNULL;
3340   smIORequestBody_t        *smIORequestBody;
3341   smIORequestBody_t        *smOrgIORequestBody;
3342   smSatIOContext_t         *satIOContext;
3343   smSatIOContext_t         *satOrgIOContext;
3344   smSatIOContext_t         *satNewIOContext;
3345   smSatInternalIo_t        *satIntIo;
3346   smSatInternalIo_t        *satNewIntIo = agNULL;
3347 //  satDeviceData_t         *satDevData;
3348   smDeviceData_t           *oneDeviceData;
3349   smScsiRspSense_t         *pSense;
3350   smIniScsiCmnd_t          *scsiCmnd;
3351   smIORequest_t            *smOrgIORequest;
3352   agsaFisRegHostToDevice_t *hostToDevFis = agNULL;
3353   bit32                     ataStatus = 0;
3354   bit32                     status;
3355   agsaFisRegD2HHeader_t    *statDevToHostFisHeader = agNULL;
3356 
3357   SM_DBG2(("smsatStartStopUnitCB: start\n"));
3358   SM_DBG5(("smsatStartStopUnitCB:agIORequest=%p agIOStatus=0x%x agIOInfoLen %d\n", agIORequest, agIOStatus, agIOInfoLen));
3359 
3360   /* internally generate smIOContext */
3361   smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
3362   satIOContext           = (smSatIOContext_t *) ioContext;
3363   satIntIo               = satIOContext->satIntIoContext;
3364   oneDeviceData          = satIOContext->pSatDevData;
3365   hostToDevFis           = satIOContext->pFis;
3366   smRoot                 = oneDeviceData->smRoot;
3367   smIntRoot              = (smIntRoot_t *)smRoot->smData;
3368   smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
3369   if (satIntIo == agNULL)
3370   {
3371     SM_DBG4(("smsatStartStopUnitCB: External smSatInternalIo_t satIntIoContext\n"));
3372     satOrgIOContext = satIOContext;
3373     smOrgIORequest  = smIORequestBody->smIORequest;
3374     pSense          = satIOContext->pSense;
3375     scsiCmnd        = satIOContext->pScsiCmnd;
3376   }
3377   else
3378   {
3379     SM_DBG4(("smsatStartStopUnitCB: Internal smSatInternalIo_t satIntIoContext\n"));
3380     satOrgIOContext        = satIOContext->satOrgIOContext;
3381     if (satOrgIOContext == agNULL)
3382     {
3383       SM_DBG4(("smsatStartStopUnitCB: satOrgIOContext is NULL, wrong\n"));
3384       return;
3385     }
3386     else
3387     {
3388       SM_DBG4(("smsatStartStopUnitCB: satOrgIOContext is NOT NULL\n"));
3389     }
3390     smOrgIORequestBody     = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
3391     smOrgIORequest         = (smIORequest_t *)smOrgIORequestBody->smIORequest;
3392 
3393     pSense        = satOrgIOContext->pSense;
3394     scsiCmnd      = satOrgIOContext->pScsiCmnd;
3395   }
3396 
3397   smIORequestBody->ioCompleted = agTRUE;
3398   smIORequestBody->ioStarted = agFALSE;
3399 
3400   if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
3401   {
3402      SM_DBG1(("smsatStartStopUnitCB: wrong. agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
3403 
3404       /* IMMED == 0 */
3405       if (!( scsiCmnd->cdb[1] & SCSI_IMMED_MASK))
3406       {
3407         SM_DBG1(("smsatStartStopUnitCB: immed bit 0!!!\n"));
3408         smsatSetSensePayload( pSense,
3409                               SCSI_SNSKEY_ABORTED_COMMAND,
3410                               0,
3411                               SCSI_SNSCODE_COMMAND_SEQUENCE_ERROR,
3412                               satOrgIOContext);
3413 
3414         tdsmIOCompletedCB( smRoot,
3415                            smOrgIORequest, /* == satIntIo->satOrgSmIORequest */
3416                            smIOSuccess,
3417                            SCSI_STAT_CHECK_CONDITION,
3418                            satOrgIOContext->pSmSenseData,
3419                            satOrgIOContext->interruptContext );
3420 
3421 
3422         smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
3423         smsatFreeIntIoResource( smRoot,
3424                                 oneDeviceData,
3425                                 satIntIo);
3426       }
3427       /* IMMED == 1 */
3428       if ( scsiCmnd->cdb[1] & SCSI_IMMED_MASK)
3429       {
3430         SM_DBG1(("smsatStartStopUnitCB: immed bit 1!!!\n"));
3431         smsatSetDeferredSensePayload( pSense,
3432                                       SCSI_SNSKEY_ABORTED_COMMAND,
3433                                       0,
3434                                       SCSI_SNSCODE_COMMAND_SEQUENCE_ERROR,
3435                                       satOrgIOContext);
3436 
3437         tdsmIOCompletedCB( smRoot,
3438                            smOrgIORequest, /* == satIntIo->satOrgSmIORequest */
3439                            smIOSuccess,
3440                            SCSI_STAT_CHECK_CONDITION,
3441                            satOrgIOContext->pSmSenseData,
3442                            satOrgIOContext->interruptContext );
3443 
3444 
3445         smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
3446         smsatFreeIntIoResource( smRoot,
3447                                 oneDeviceData,
3448                                 satIntIo);
3449      }
3450 
3451 
3452 
3453     return;
3454   }
3455 
3456   if (agIOStatus != OSSA_IO_SUCCESS)
3457   {
3458     /* only agsaFisRegDeviceToHost_t is expected */
3459     statDevToHostFisHeader = (agsaFisRegD2HHeader_t *)&(agFirstDword->D2H);
3460     ataStatus     = statDevToHostFisHeader->status;   /* ATA Status register */
3461   }
3462   /*
3463     checking IO status, FIS type and error status
3464   */
3465   if( agIOStatus != OSSA_IO_SUCCESS)
3466   {
3467   if( (statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS) ||
3468       ((ataStatus & ERR_ATA_STATUS_MASK) || (ataStatus & DF_ATA_STATUS_MASK))
3469       )
3470   {
3471     /* for debugging */
3472     if( agIOStatus != OSSA_IO_SUCCESS)
3473     {
3474       SM_DBG1(("smsatStartStopUnitCB: FAILED, NOT IO_SUCCESS!!!\n"));
3475     }
3476     else if (statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS)
3477     {
3478       SM_DBG1(("smsatStartStopUnitCB: FAILED, Wrong FIS type 0x%x!!!\n",statDevToHostFisHeader->fisType));
3479     }
3480     else if ( (ataStatus & ERR_ATA_STATUS_MASK) ||
3481          (ataStatus & DF_ATA_STATUS_MASK)
3482          )
3483     {
3484       SM_DBG1(("smsatStartStopUnitCB: FAILED, FAILED, error status!!!\n"));
3485     }
3486 
3487 
3488     /* Process abort case */
3489     if (agIOStatus == OSSA_IO_ABORTED)
3490     {
3491       smsatProcessAbort(smRoot,
3492                         smOrgIORequest,
3493                         satOrgIOContext
3494                         );
3495 
3496 
3497       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
3498 
3499       smsatFreeIntIoResource( smRoot,
3500                               oneDeviceData,
3501                               satIntIo);
3502       return;
3503     }
3504 
3505     switch (hostToDevFis->h.command)
3506     {
3507     case SAT_FLUSH_CACHE: /* fall through */
3508     case SAT_FLUSH_CACHE_EXT:
3509       SM_DBG1(("smsatStartStopUnitCB: SAT_FLUSH_CACHE(_EXT)!!!\n"));
3510       /* check immed bit in scsi command */
3511       /* IMMED == 0 */
3512       if (!( scsiCmnd->cdb[1] & SCSI_IMMED_MASK))
3513       {
3514         smsatSetSensePayload( pSense,
3515                               SCSI_SNSKEY_ABORTED_COMMAND,
3516                               0,
3517                               SCSI_SNSCODE_COMMAND_SEQUENCE_ERROR,
3518                               satOrgIOContext);
3519 
3520         tdsmIOCompletedCB( smRoot,
3521                            smOrgIORequest, /* == satIntIo->satOrgSmIORequest */
3522                            smIOSuccess,
3523                            SCSI_STAT_CHECK_CONDITION,
3524                            satOrgIOContext->pSmSenseData,
3525                            satOrgIOContext->interruptContext );
3526 
3527 
3528         smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
3529 
3530         smsatFreeIntIoResource( smRoot,
3531                                 oneDeviceData,
3532                                 satIntIo);
3533       }
3534       /* IMMED == 1 */
3535       if ( scsiCmnd->cdb[1] & SCSI_IMMED_MASK)
3536       {
3537         smsatSetDeferredSensePayload( pSense,
3538                                       SCSI_SNSKEY_ABORTED_COMMAND,
3539                                       0,
3540                                       SCSI_SNSCODE_COMMAND_SEQUENCE_ERROR,
3541                                       satOrgIOContext);
3542 
3543         tdsmIOCompletedCB( smRoot,
3544                            smOrgIORequest, /* == satIntIo->satOrgSmIORequest */
3545                            smIOSuccess,
3546                            SCSI_STAT_CHECK_CONDITION,
3547                            satOrgIOContext->pSmSenseData,
3548                            satOrgIOContext->interruptContext );
3549 
3550 
3551         smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
3552 
3553         smsatFreeIntIoResource( smRoot,
3554                                 oneDeviceData,
3555                                 satIntIo);
3556       }
3557       break;
3558     case SAT_STANDBY:
3559       SM_DBG5(("smsatStartStopUnitCB: SAT_STANDBY\n"));
3560       /* check immed bit in scsi command */
3561       /* IMMED == 0 */
3562       if (!( scsiCmnd->cdb[1] & SCSI_IMMED_MASK))
3563       {
3564         smsatSetSensePayload( pSense,
3565                               SCSI_SNSKEY_ABORTED_COMMAND,
3566                               0,
3567                               SCSI_SNSCODE_COMMAND_SEQUENCE_ERROR,
3568                               satOrgIOContext);
3569 
3570         tdsmIOCompletedCB( smRoot,
3571                            smOrgIORequest, /* == satIntIo->satOrgSmIORequest */
3572                            smIOSuccess,
3573                            SCSI_STAT_CHECK_CONDITION,
3574                            satOrgIOContext->pSmSenseData,
3575                            satOrgIOContext->interruptContext );
3576 
3577 
3578         smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
3579 
3580         smsatFreeIntIoResource( smRoot,
3581                                 oneDeviceData,
3582                                 satIntIo);
3583       }
3584       /* IMMED == 1 */
3585       if ( scsiCmnd->cdb[1] & SCSI_IMMED_MASK)
3586       {
3587         smsatSetDeferredSensePayload( pSense,
3588                                       SCSI_SNSKEY_ABORTED_COMMAND,
3589                                       0,
3590                                       SCSI_SNSCODE_COMMAND_SEQUENCE_ERROR,
3591                                       satOrgIOContext);
3592 
3593         tdsmIOCompletedCB( smRoot,
3594                            smOrgIORequest, /* == satIntIo->satOrgSmIORequest */
3595                            smIOSuccess,
3596                            SCSI_STAT_CHECK_CONDITION,
3597                            satOrgIOContext->pSmSenseData,
3598                            satOrgIOContext->interruptContext );
3599 
3600 
3601         smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
3602 
3603         smsatFreeIntIoResource( smRoot,
3604                                 oneDeviceData,
3605                                 satIntIo);
3606       }
3607       break;
3608     case SAT_READ_VERIFY_SECTORS:     /* fall through */
3609     case SAT_READ_VERIFY_SECTORS_EXT:
3610       SM_DBG5(("smsatStartStopUnitCB: SAT_READ_VERIFY_SECTORS(_EXT)\n"));
3611        /* IMMED == 0 */
3612       if (!( scsiCmnd->cdb[1] & SCSI_IMMED_MASK))
3613       {
3614         smsatSetSensePayload( pSense,
3615                               SCSI_SNSKEY_ABORTED_COMMAND,
3616                               0,
3617                               SCSI_SNSCODE_COMMAND_SEQUENCE_ERROR,
3618                               satOrgIOContext);
3619 
3620         tdsmIOCompletedCB( smRoot,
3621                            smOrgIORequest, /* == satIntIo->satOrgSmIORequest */
3622                            smIOSuccess,
3623                            SCSI_STAT_CHECK_CONDITION,
3624                            satOrgIOContext->pSmSenseData,
3625                            satOrgIOContext->interruptContext );
3626 
3627 
3628         smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
3629 
3630         smsatFreeIntIoResource( smRoot,
3631                                 oneDeviceData,
3632                                 satIntIo);
3633       }
3634       /* IMMED == 1 */
3635       if ( scsiCmnd->cdb[1] & SCSI_IMMED_MASK)
3636       {
3637         smsatSetDeferredSensePayload( pSense,
3638                                       SCSI_SNSKEY_ABORTED_COMMAND,
3639                                       0,
3640                                       SCSI_SNSCODE_COMMAND_SEQUENCE_ERROR,
3641                                       satOrgIOContext);
3642 
3643         tdsmIOCompletedCB( smRoot,
3644                            smOrgIORequest, /* == satIntIo->satOrgSmIORequest */
3645                            smIOSuccess,
3646                            SCSI_STAT_CHECK_CONDITION,
3647                            satOrgIOContext->pSmSenseData,
3648                            satOrgIOContext->interruptContext );
3649 
3650 
3651         smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
3652 
3653         smsatFreeIntIoResource( smRoot,
3654                                 oneDeviceData,
3655                                 satIntIo);
3656       }
3657       break;
3658     case SAT_MEDIA_EJECT:
3659       SM_DBG5(("smsatStartStopUnitCB: SAT_MEDIA_EJECT\n"));
3660        /* IMMED == 0 */
3661       if (!( scsiCmnd->cdb[1] & SCSI_IMMED_MASK))
3662       {
3663         smsatSetSensePayload( pSense,
3664                               SCSI_SNSKEY_ABORTED_COMMAND,
3665                               0,
3666                               SCSI_SNSCODE_MEDIA_LOAD_OR_EJECT_FAILED,
3667                               satOrgIOContext);
3668 
3669         tdsmIOCompletedCB( smRoot,
3670                            smOrgIORequest, /* == satIntIo->satOrgSmIORequest */
3671                            smIOSuccess,
3672                            SCSI_STAT_CHECK_CONDITION,
3673                            satOrgIOContext->pSmSenseData,
3674                            satOrgIOContext->interruptContext );
3675 
3676 
3677         smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
3678 
3679         smsatFreeIntIoResource( smRoot,
3680                                 oneDeviceData,
3681                                 satIntIo);
3682       }
3683       /* IMMED == 1 */
3684       if ( scsiCmnd->cdb[1] & SCSI_IMMED_MASK)
3685       {
3686         smsatSetDeferredSensePayload( pSense,
3687                                       SCSI_SNSKEY_ABORTED_COMMAND,
3688                                       0,
3689                                       SCSI_SNSCODE_MEDIA_LOAD_OR_EJECT_FAILED,
3690                                       satOrgIOContext);
3691 
3692         tdsmIOCompletedCB( smRoot,
3693                            smOrgIORequest, /* == satIntIo->satOrgSmIORequest */
3694                            smIOSuccess,
3695                            SCSI_STAT_CHECK_CONDITION,
3696                            satOrgIOContext->pSmSenseData,
3697                            satOrgIOContext->interruptContext );
3698 
3699         smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
3700 
3701         smsatFreeIntIoResource( smRoot,
3702                                 oneDeviceData,
3703                                 satIntIo);
3704       }
3705       break;
3706     default:
3707       /* unspecified case, return no sense and no addition info */
3708       SM_DBG5(("smsatStartStopUnitCB: default command %d\n", hostToDevFis->h.command));
3709       smsatSetSensePayload( pSense,
3710                             SCSI_SNSKEY_NO_SENSE,
3711                             0,
3712                             SCSI_SNSCODE_NO_ADDITIONAL_INFO,
3713                             satOrgIOContext);
3714 
3715       tdsmIOCompletedCB( smRoot,
3716                          smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
3717                          smIOSuccess,
3718                          SCSI_STAT_CHECK_CONDITION,
3719                          satOrgIOContext->pSmSenseData,
3720                          satOrgIOContext->interruptContext );
3721 
3722 
3723       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
3724 
3725       smsatFreeIntIoResource( smRoot,
3726                               oneDeviceData,
3727                               satIntIo);
3728       break;
3729     } /* switch */
3730 
3731     return;
3732   } /* error check */
3733   }
3734 
3735   /* ATA command completes sucessfully */
3736   switch (hostToDevFis->h.command)
3737   {
3738   case SAT_FLUSH_CACHE: /* fall through */
3739   case SAT_FLUSH_CACHE_EXT:
3740     SM_DBG5(("smsatStartStopUnitCB: SAT_READ_VERIFY_SECTORS(_EXT) success case\n"));
3741 
3742 
3743     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
3744 
3745     /* done with SAT_FLUSH_CACHE(_EXT) */
3746     smsatFreeIntIoResource( smRoot,
3747                             oneDeviceData,
3748                             satIntIo);
3749 
3750     /* at this point, successful SAT_READ_VERIFY_SECTORS(_EXT)
3751        send SAT_SATNDBY
3752     */
3753     satNewIntIo = smsatAllocIntIoResource( smRoot,
3754                                            smOrgIORequest,
3755                                            oneDeviceData,
3756                                            0,
3757                                            satNewIntIo);
3758     if (satNewIntIo == agNULL)
3759     {
3760       /* IMMED == 0 */
3761       if (!( scsiCmnd->cdb[1] & SCSI_IMMED_MASK))
3762       {
3763         smsatSetSensePayload( pSense,
3764                               SCSI_SNSKEY_ABORTED_COMMAND,
3765                               0,
3766                               SCSI_SNSCODE_COMMAND_SEQUENCE_ERROR,
3767                               satOrgIOContext);
3768       }
3769       else   /* IMMED == 1 */
3770       {
3771         smsatSetDeferredSensePayload( pSense,
3772                                       SCSI_SNSKEY_ABORTED_COMMAND,
3773                                       0,
3774                                       SCSI_SNSCODE_COMMAND_SEQUENCE_ERROR,
3775                                       satOrgIOContext);
3776       }
3777       tdsmIOCompletedCB( smRoot,
3778                          smOrgIORequest,
3779                          smIOSuccess,
3780                          SCSI_STAT_CHECK_CONDITION,
3781                          satOrgIOContext->pSmSenseData,
3782                          satOrgIOContext->interruptContext );
3783 
3784       SM_DBG1(("smsatStartStopUnitCB: momory allocation fails!!!\n"));
3785       return;
3786     } /* end of memory allocation failure */
3787 
3788     /*
3789      * Need to initialize all the fields within satIOContext
3790      */
3791 
3792     satNewIOContext = smsatPrepareNewIO(
3793                                         satNewIntIo,
3794                                         smOrgIORequest,
3795                                         oneDeviceData,
3796                                         scsiCmnd,
3797                                         satOrgIOContext
3798                                         );
3799 
3800     /* sending SAT_STANDBY */
3801     status = smsatStartStopUnit_1( smRoot,
3802                                    &satNewIntIo->satIntSmIORequest,
3803                                    satNewIOContext->psmDeviceHandle,
3804                                    &satNewIntIo->satIntSmScsiXchg,
3805                                    satNewIOContext);
3806 
3807     if (status != SM_RC_SUCCESS)
3808     {
3809       /* sending SAT_CHECK_POWER_MODE fails */
3810       smsatFreeIntIoResource( smRoot,
3811                               oneDeviceData,
3812                               satNewIntIo);
3813 
3814       /* IMMED == 0 */
3815       if (!( scsiCmnd->cdb[1] & SCSI_IMMED_MASK))
3816       {
3817         smsatSetSensePayload( pSense,
3818                               SCSI_SNSKEY_ABORTED_COMMAND,
3819                               0,
3820                               SCSI_SNSCODE_COMMAND_SEQUENCE_ERROR,
3821                               satOrgIOContext);
3822       }
3823       else   /* IMMED == 1 */
3824       {
3825         smsatSetDeferredSensePayload( pSense,
3826                                       SCSI_SNSKEY_ABORTED_COMMAND,
3827                                       0,
3828                                       SCSI_SNSCODE_COMMAND_SEQUENCE_ERROR,
3829                                       satOrgIOContext);
3830       }
3831       tdsmIOCompletedCB( smRoot,
3832                          smOrgIORequest,
3833                          smIOSuccess,
3834                          SCSI_STAT_CHECK_CONDITION,
3835                          satOrgIOContext->pSmSenseData,
3836                          satOrgIOContext->interruptContext );
3837 
3838       SM_DBG1(("smsatStartStopUnitCB: calling satStartStopUnit_1 fails!!!\n"));
3839       return;
3840     }
3841     break;
3842   case SAT_STANDBY:
3843     SM_DBG5(("smsatStartStopUnitCB: SAT_STANDBY success case\n"));
3844 
3845     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
3846 
3847     /* done with SAT_STANDBY */
3848     smsatFreeIntIoResource( smRoot,
3849                             oneDeviceData,
3850                             satIntIo);
3851     /*
3852       if immed == 0, return good status
3853      */
3854     /* IMMED == 0 */
3855     if (!( scsiCmnd->cdb[1] & SCSI_IMMED_MASK))
3856     {
3857         tdsmIOCompletedCB( smRoot,
3858                            smOrgIORequest,
3859                            smIOSuccess,
3860                            SCSI_STAT_GOOD,
3861                            agNULL,
3862                            satOrgIOContext->interruptContext );
3863     }
3864     oneDeviceData->satStopState = agTRUE;
3865     break;
3866   case SAT_READ_VERIFY_SECTORS:     /* fall through */
3867   case SAT_READ_VERIFY_SECTORS_EXT:
3868     SM_DBG5(("smsatStartStopUnitCB: SAT_READ_VERIFY_SECTORS(_EXT) success case\n"));
3869 
3870     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
3871 
3872     /* done with SAT_READ_VERIFY_SECTORS(_EXT) */
3873     smsatFreeIntIoResource( smRoot,
3874                             oneDeviceData,
3875                             satIntIo);
3876     /*
3877       if immed == 0, return good status
3878      */
3879     if (!( scsiCmnd->cdb[1] & SCSI_IMMED_MASK))
3880     {
3881         tdsmIOCompletedCB( smRoot,
3882                            smOrgIORequest,
3883                            smIOSuccess,
3884                            SCSI_STAT_GOOD,
3885                            agNULL,
3886                            satOrgIOContext->interruptContext );
3887     }
3888     /*
3889       if immed == 0, return good status
3890      */
3891     /*
3892       don't forget to check and set driver state; Active power state
3893     */
3894     oneDeviceData->satStopState = agFALSE;
3895     break;
3896   case SAT_MEDIA_EJECT:
3897     SM_DBG5(("smsatStartStopUnitCB: SAT_MEDIA_EJECT success case\n"));
3898 
3899     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
3900 
3901     /* done with SAT_READ_VERIFY_SECTORS(_EXT) */
3902     smsatFreeIntIoResource( smRoot,
3903                             oneDeviceData,
3904                             satIntIo);
3905     /*
3906       if immed == 0, return good status
3907      */
3908     if (!( scsiCmnd->cdb[1] & SCSI_IMMED_MASK))
3909     {
3910       tdsmIOCompletedCB( smRoot,
3911                          smOrgIORequest,
3912                          smIOSuccess,
3913                          SCSI_STAT_GOOD,
3914                          agNULL,
3915                          satOrgIOContext->interruptContext );
3916     }
3917     break;
3918   default:
3919     SM_DBG1(("smsatStartStopUnitCB:success but  error default case command 0x%x!!!\n", hostToDevFis->h.command));
3920 
3921     /* unspecified case, return no sense and no addition info */
3922     smsatSetSensePayload( pSense,
3923                           SCSI_SNSKEY_NO_SENSE,
3924                           0,
3925                           SCSI_SNSCODE_NO_ADDITIONAL_INFO,
3926                           satOrgIOContext);
3927 
3928     tdsmIOCompletedCB( smRoot,
3929                        smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
3930                        smIOSuccess,
3931                        SCSI_STAT_CHECK_CONDITION,
3932                        satOrgIOContext->pSmSenseData,
3933                        satOrgIOContext->interruptContext );
3934 
3935     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
3936 
3937     smsatFreeIntIoResource( smRoot,
3938                             oneDeviceData,
3939                             satIntIo);
3940     break;
3941   }
3942   return;
3943 
3944 }
3945 
3946 osGLOBAL void
3947 smsatWriteSame10CB(
3948                    agsaRoot_t        *agRoot,
3949                    agsaIORequest_t   *agIORequest,
3950                    bit32             agIOStatus,
3951                    agsaFisHeader_t   *agFirstDword,
3952                    bit32             agIOInfoLen,
3953                    agsaFrameHandle_t agFrameHandle,
3954                    void              *ioContext
3955                   )
3956 {
3957   smRoot_t                *smRoot = agNULL;
3958   smIntRoot_t             *smIntRoot = agNULL;
3959   smIntContext_t          *smAllShared = agNULL;
3960   smIORequestBody_t       *smIORequestBody;
3961   smIORequestBody_t       *smOrgIORequestBody;
3962   smIORequestBody_t       *smNewIORequestBody;
3963   smSatIOContext_t          *satIOContext;
3964   smSatIOContext_t          *satOrgIOContext;
3965   smSatIOContext_t          *satNewIOContext;
3966   smSatInternalIo_t         *satIntIo;
3967   smSatInternalIo_t         *satNewIntIo = agNULL;
3968   smDeviceData_t          *oneDeviceData;
3969   smScsiRspSense_t          *pSense;
3970   smIniScsiCmnd_t           *scsiCmnd;
3971   smIORequest_t             *smOrgIORequest;
3972   agsaFisRegHostToDevice_t  *hostToDevFis = agNULL;
3973   bit32                     ataStatus = 0;
3974   bit32                     status;
3975   bit32                     sectorcount = 0;
3976   bit32                     lba = 0, tl = 0;
3977   agsaFisRegD2HHeader_t     *statDevToHostFisHeader = agNULL;
3978   agsaFisSetDevBitsHeader_t *statSetDevBitFisHeader = agNULL;
3979 
3980   SM_DBG2(("smsatWriteSame10CB: start\n"));
3981   SM_DBG5(("smsatWriteSame10CB: agIORequest=%p agIOStatus=0x%x agIOInfoLen %d\n", agIORequest, agIOStatus, agIOInfoLen));
3982 
3983   /* internally generate smIOContext */
3984   smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
3985   satIOContext           = (smSatIOContext_t *) ioContext;
3986   satIntIo               = satIOContext->satIntIoContext;
3987   oneDeviceData          = satIOContext->pSatDevData;
3988   hostToDevFis           = satIOContext->pFis;
3989   smRoot                 = oneDeviceData->smRoot;
3990   smIntRoot              = (smIntRoot_t *)smRoot->smData;
3991   smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
3992 
3993   if (satIntIo == agNULL)
3994   {
3995     SM_DBG4(("smsatWriteSame10CB: External smSatInternalIo_t satIntIoContext\n"));
3996     satOrgIOContext = satIOContext;
3997     smOrgIORequest  = smIORequestBody->smIORequest;
3998     pSense          = satIOContext->pSense;
3999     scsiCmnd        = satIOContext->pScsiCmnd;
4000   }
4001   else
4002   {
4003     SM_DBG4(("smsatWriteSame10CB: Internal smSatInternalIo_t satIntIoContext\n"));
4004     satOrgIOContext        = satIOContext->satOrgIOContext;
4005     if (satOrgIOContext == agNULL)
4006     {
4007       SM_DBG4(("smsatWriteSame10CB: satOrgIOContext is NULL, wrong\n"));
4008       return;
4009     }
4010     else
4011     {
4012       SM_DBG4(("smsatWriteSame10CB: satOrgIOContext is NOT NULL\n"));
4013     }
4014     smOrgIORequestBody     = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
4015     smOrgIORequest         = (smIORequest_t *)smOrgIORequestBody->smIORequest;
4016 
4017     pSense        = satOrgIOContext->pSense;
4018     scsiCmnd      = satOrgIOContext->pScsiCmnd;
4019   }
4020 
4021 
4022   smIORequestBody->ioCompleted = agTRUE;
4023   smIORequestBody->ioStarted = agFALSE;
4024 
4025   if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
4026   {
4027      SM_DBG1(("smsatWriteSame10CB: wrong. agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
4028      smsatSetSensePayload( pSense,
4029                           SCSI_SNSKEY_NO_SENSE,
4030                           0,
4031                           SCSI_SNSCODE_NO_ADDITIONAL_INFO,
4032                           satOrgIOContext);
4033 
4034      tdsmIOCompletedCB( smRoot,
4035                         smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
4036                         smIOSuccess,
4037                         SCSI_STAT_CHECK_CONDITION,
4038                         satOrgIOContext->pSmSenseData,
4039                         satOrgIOContext->interruptContext );
4040 
4041     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
4042 
4043     smsatFreeIntIoResource( smRoot,
4044                             oneDeviceData,
4045                             satIntIo);
4046     return;
4047   }
4048 
4049   if (agIOStatus != OSSA_IO_SUCCESS)
4050   {
4051     /* FP, DMA and PIO write */
4052     /* First, assumed to be Reg Device to Host FIS */
4053     statDevToHostFisHeader = (agsaFisRegD2HHeader_t *)&(agFirstDword->D2H);
4054     ataStatus     = statDevToHostFisHeader->status;   /* ATA Status register */
4055   }
4056 
4057   if (agIOStatus != OSSA_IO_SUCCESS)
4058   {
4059     if (statDevToHostFisHeader->fisType == SET_DEV_BITS_FIS)
4060     {
4061       statSetDevBitFisHeader = (agsaFisSetDevBitsHeader_t *)&(agFirstDword->D2H);
4062 
4063       /* Get ATA Status register */
4064       ataStatus = (statSetDevBitFisHeader->statusHi_Lo & 0x70);               /* bits 4,5,6 */
4065       ataStatus = ataStatus | (statSetDevBitFisHeader->statusHi_Lo & 0x07);   /* bits 0,1,2 */
4066     }
4067   }
4068 
4069   if( agIOStatus != OSSA_IO_SUCCESS)
4070   {
4071   /*
4072     checking IO status, FIS type and error status
4073     FIS type should be either REG_DEV_TO_HOST_FIS or SET_DEV_BITS_FIS
4074   */
4075   if (  ((statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS) &&
4076         (statDevToHostFisHeader->fisType != SET_DEV_BITS_FIS)) ||
4077        ((ataStatus & ERR_ATA_STATUS_MASK) || (ataStatus & DF_ATA_STATUS_MASK))
4078        )
4079   {
4080     /* for debugging */
4081     if( agIOStatus != OSSA_IO_SUCCESS)
4082     {
4083       SM_DBG1(("smsatWriteSame10CB: FAILED, NOT IO_SUCCESS!!!\n"));
4084     }
4085     else if (statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS)
4086     {
4087       SM_DBG1(("smsatWriteSame10CB: FAILED, Wrong FIS type 0x%x!!!\n",statDevToHostFisHeader->fisType));
4088     }
4089     else if (statDevToHostFisHeader->fisType != SET_DEV_BITS_FIS)
4090     {
4091       SM_DBG1(("smsatWriteSame10CB: FAILED, Wrong FIS type 0x%x!!!\n",statDevToHostFisHeader->fisType));
4092     }
4093     else if ( (ataStatus & ERR_ATA_STATUS_MASK) ||
4094               (ataStatus & DF_ATA_STATUS_MASK)
4095               )
4096     {
4097       SM_DBG1(("smsatWriteSame10CB: FAILED, FAILED, error status!!!\n"));
4098     }
4099 
4100     /* Process abort case */
4101     if (agIOStatus == OSSA_IO_ABORTED)
4102     {
4103       smsatProcessAbort(smRoot,
4104                         smOrgIORequest,
4105                         satOrgIOContext
4106                         );
4107 
4108 
4109       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
4110 
4111       smsatFreeIntIoResource( smRoot,
4112                               oneDeviceData,
4113                               satIntIo);
4114       return;
4115     }
4116 
4117     /* for debugging */
4118     switch (hostToDevFis->h.command)
4119     {
4120     case SAT_WRITE_DMA_EXT:
4121       SM_DBG1(("smsatWriteSame10CB: SAT_WRITE_DMA_EXT!!!\n"));
4122       break;
4123     case SAT_WRITE_SECTORS_EXT:
4124       SM_DBG1(("smsatWriteSame10CB: SAT_WRITE_SECTORS_EXT!!!\n"));
4125       break;
4126     case SAT_WRITE_FPDMA_QUEUED:
4127       SM_DBG1(("smsatWriteSame10CB: SAT_WRITE_FPDMA_QUEUED!!!\n"));
4128       break;
4129     default:
4130       SM_DBG1(("smsatWriteSame10CB: error default case command 0x%x!!!\n", hostToDevFis->h.command));
4131       break;
4132     }
4133 
4134     smsatSetSensePayload( pSense,
4135                           SCSI_SNSKEY_NO_SENSE,
4136                           0,
4137                           SCSI_SNSCODE_NO_ADDITIONAL_INFO,
4138                           satOrgIOContext);
4139 
4140     tdsmIOCompletedCB( smRoot,
4141                        smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
4142                        smIOSuccess,
4143                        SCSI_STAT_CHECK_CONDITION,
4144                        satOrgIOContext->pSmSenseData,
4145                        satOrgIOContext->interruptContext );
4146 
4147 
4148     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
4149 
4150     smsatFreeIntIoResource( smRoot,
4151                             oneDeviceData,
4152                             satIntIo);
4153     return;
4154   } /* end error */
4155   }
4156 
4157   /* process success from this point on */
4158   /*
4159     note: inefficient implementation until a single block can be manipulated
4160   */
4161 
4162   if (hostToDevFis->h.command == SAT_WRITE_DMA_EXT)
4163   {
4164     SM_DBG5(("smsatWriteSame10CB: SAT_WRITE_DMA_EXT success\n"));
4165   }
4166   else if (hostToDevFis->h.command == SAT_WRITE_SECTORS_EXT)
4167   {
4168     SM_DBG5(("smsatWriteSame10CB: SAT_WRITE_SECTORS_EXT success\n"));
4169   }
4170   else if (hostToDevFis->h.command == SAT_WRITE_FPDMA_QUEUED)
4171   {
4172     SM_DBG5(("smsatWriteSame10CB: SAT_WRITE_FPDMA_QUEUED success\n"));
4173   }
4174   else
4175   {
4176     SM_DBG1(("smsatWriteSame10CB: error case command 0x%x success!!!\n", hostToDevFis->h.command));
4177     smsatSetSensePayload( pSense,
4178                           SCSI_SNSKEY_NO_SENSE,
4179                           0,
4180                           SCSI_SNSCODE_NO_ADDITIONAL_INFO,
4181                           satOrgIOContext);
4182 
4183     tdsmIOCompletedCB( smRoot,
4184                        smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
4185                        smIOSuccess,
4186                        SCSI_STAT_CHECK_CONDITION,
4187                        satOrgIOContext->pSmSenseData,
4188                        satOrgIOContext->interruptContext );
4189 
4190 
4191     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
4192 
4193     smsatFreeIntIoResource( smRoot,
4194                             oneDeviceData,
4195                             satIntIo);
4196     return;
4197   }
4198 
4199 
4200   smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
4201 
4202   /* free */
4203   smsatFreeIntIoResource( smRoot,
4204                           oneDeviceData,
4205                           satIntIo);
4206 
4207   /*
4208     increment LBA by one, keeping the same sector count(1)
4209     sends another ATA command with the changed parameters
4210   */
4211 
4212   tdsmSingleThreadedEnter(smRoot, SM_EXTERNAL_IO_LOCK);
4213   oneDeviceData->satSectorDone++;
4214   tdsmSingleThreadedLeave(smRoot, SM_EXTERNAL_IO_LOCK);
4215 
4216   SM_DBG1(("smsatWriteSame10CB: sectordone %d!!!\n", oneDeviceData->satSectorDone));
4217 
4218   lba = (scsiCmnd->cdb[2] << (8*3)) + (scsiCmnd->cdb[3] << (8*2))
4219       + (scsiCmnd->cdb[4] << 8) + scsiCmnd->cdb[5];
4220   tl = (scsiCmnd->cdb[7] << 8) + scsiCmnd->cdb[8];
4221 
4222   SM_DBG5(("smsatWriteSame10CB: lba 0x%x tl 0x%x\n", lba, tl));
4223 
4224   if (tl == 0)
4225   {
4226     /* (oneDeviceData->satMaxUserAddrSectors - 1) - lba*/
4227     sectorcount = (0x0FFFFFFF - 1) - lba;
4228   }
4229   else
4230   {
4231     sectorcount = tl;
4232   }
4233 
4234   if (sectorcount <= 0)
4235   {
4236     smsatSetSensePayload( pSense,
4237                           SCSI_SNSKEY_NO_SENSE,
4238                           0,
4239                           SCSI_SNSCODE_NO_ADDITIONAL_INFO,
4240                           satOrgIOContext);
4241 
4242     tdsmIOCompletedCB( smRoot,
4243                        smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
4244                        smIOSuccess,
4245                        SCSI_STAT_CHECK_CONDITION,
4246                        satOrgIOContext->pSmSenseData,
4247                        satOrgIOContext->interruptContext );
4248     SM_DBG1(("smsatWriteSame10CB: incorrect sectorcount 0x%x!!!\n", sectorcount));
4249     return;
4250   }
4251 
4252   if (sectorcount == oneDeviceData->satSectorDone)
4253   {
4254     /*
4255       done with writesame
4256     */
4257     SM_DBG1(("smsatWriteSame10CB: return writesame done!!!\n"));
4258     oneDeviceData->satSectorDone = 0;
4259 
4260     tdsmIOCompletedCB( smRoot,
4261                        smOrgIORequest,
4262                        smIOSuccess,
4263                        SCSI_STAT_GOOD,
4264                        agNULL,
4265                        satOrgIOContext->interruptContext );
4266   }
4267   else
4268   {
4269     /* sends another ATA command */
4270     if (hostToDevFis->h.command == SAT_WRITE_DMA_EXT)
4271     {
4272       SM_DBG1(("smsatWriteSame10CB: sends another SAT_WRITE_DMA_EXT!!!\n"));
4273     }
4274     else if (hostToDevFis->h.command == SAT_WRITE_SECTORS_EXT)
4275     {
4276       SM_DBG1(("smsatWriteSame10CB: sends another SAT_WRITE_SECTORS_EXT!!!\n"));
4277     }
4278     else if (hostToDevFis->h.command == SAT_WRITE_FPDMA_QUEUED)
4279     {
4280       SM_DBG1(("smsatWriteSame10CB: sends another SAT_WRITE_FPDMA_QUEUED!!!\n"));
4281     }
4282 
4283     satNewIntIo = smsatAllocIntIoResource( smRoot,
4284                                            smOrgIORequest,
4285                                            oneDeviceData,
4286                                            0,
4287                                            satNewIntIo);
4288     if (satNewIntIo == agNULL)
4289     {
4290       smsatSetSensePayload( pSense,
4291                             SCSI_SNSKEY_NO_SENSE,
4292                             0,
4293                             SCSI_SNSCODE_NO_ADDITIONAL_INFO,
4294                             satOrgIOContext);
4295 
4296       tdsmIOCompletedCB( smRoot,
4297                          smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
4298                          smIOSuccess,
4299                          SCSI_STAT_CHECK_CONDITION,
4300                          satOrgIOContext->pSmSenseData,
4301                          satOrgIOContext->interruptContext );
4302       SM_DBG1(("smsatWriteSame10CB: momory allocation fails!!!\n"));
4303       return;
4304     } /* end memory allocation */
4305 
4306     /* the one to be used */
4307     smNewIORequestBody = satNewIntIo->satIntRequestBody;
4308     satNewIOContext = &smNewIORequestBody->transport.SATA.satIOContext;
4309 
4310     satNewIOContext->pSatDevData   = oneDeviceData;
4311     satNewIOContext->pFis          = &smNewIORequestBody->transport.SATA.agSATARequestBody.fis.fisRegHostToDev;
4312     satNewIOContext->pScsiCmnd     = &satNewIntIo->satIntSmScsiXchg.scsiCmnd;
4313     /* saves scsi command for LBA and number of blocks */
4314     sm_memcpy(satNewIOContext->pScsiCmnd, scsiCmnd, sizeof(smIniScsiCmnd_t));
4315     satNewIOContext->pSense        = &smNewIORequestBody->transport.SATA.sensePayload;
4316     satNewIOContext->pSmSenseData  = &smNewIORequestBody->transport.SATA.smSenseData;
4317     satNewIOContext->pSmSenseData->senseData = satNewIOContext->pSense;
4318     satNewIOContext->smRequestBody = satNewIntIo->satIntRequestBody;
4319     satNewIOContext->interruptContext = satNewIOContext->interruptContext;
4320     satNewIOContext->satIntIoContext  = satNewIntIo;
4321     satNewIOContext->psmDeviceHandle = satIOContext->psmDeviceHandle;
4322     /* saves smScsiXchg; only for writesame10() */
4323     satNewIOContext->smScsiXchg = satOrgIOContext->smScsiXchg;
4324 
4325     if (hostToDevFis->h.command == SAT_WRITE_DMA_EXT)
4326     {
4327       status = smsatWriteSame10_1( smRoot,
4328                                    &satNewIntIo->satIntSmIORequest,
4329                                    satNewIOContext->psmDeviceHandle,
4330                                    &satNewIntIo->satIntSmScsiXchg,
4331                                    satNewIOContext,
4332                                    lba + oneDeviceData->satSectorDone
4333                                    );
4334     }
4335     else if (hostToDevFis->h.command == SAT_WRITE_SECTORS_EXT)
4336     {
4337       status = smsatWriteSame10_2( smRoot,
4338                                    &satNewIntIo->satIntSmIORequest,
4339                                    satNewIOContext->psmDeviceHandle,
4340                                    &satNewIntIo->satIntSmScsiXchg,
4341                                    satNewIOContext,
4342                                    lba + oneDeviceData->satSectorDone
4343                                   );
4344     }
4345     else if (hostToDevFis->h.command == SAT_WRITE_FPDMA_QUEUED)
4346     {
4347       status = smsatWriteSame10_3( smRoot,
4348                                    &satNewIntIo->satIntSmIORequest,
4349                                    satNewIOContext->psmDeviceHandle,
4350                                    &satNewIntIo->satIntSmScsiXchg,
4351                                    satNewIOContext,
4352                                    lba + oneDeviceData->satSectorDone
4353                                   );
4354     }
4355     else
4356     {
4357       status = tiError;
4358       SM_DBG1(("smsatWriteSame10CB: sucess but error in command 0x%x!!!\n", hostToDevFis->h.command));
4359     }
4360 
4361     if (status != SM_RC_SUCCESS)
4362     {
4363       /* sending ATA command fails */
4364       smsatFreeIntIoResource( smRoot,
4365                               oneDeviceData,
4366                               satNewIntIo);
4367       smsatSetSensePayload( pSense,
4368                             SCSI_SNSKEY_NO_SENSE,
4369                             0,
4370                             SCSI_SNSCODE_NO_ADDITIONAL_INFO,
4371                             satOrgIOContext);
4372 
4373       tdsmIOCompletedCB( smRoot,
4374                          smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
4375                          smIOSuccess,
4376                          SCSI_STAT_CHECK_CONDITION,
4377                          satOrgIOContext->pSmSenseData,
4378                          satOrgIOContext->interruptContext );
4379       SM_DBG1(("smsatWriteSame10CB:calling satWriteSame10_1 fails!!!\n"));
4380       return;
4381     } /* end send fails */
4382 
4383   } /* end sends another ATA command */
4384 
4385   return;
4386 
4387 }
4388 
4389 osGLOBAL void
4390 smsatLogSenseCB(
4391                 agsaRoot_t        *agRoot,
4392                 agsaIORequest_t   *agIORequest,
4393                 bit32             agIOStatus,
4394                 agsaFisHeader_t   *agFirstDword,
4395                 bit32             agIOInfoLen,
4396                 void              *agParam,
4397                 void              *ioContext
4398                )
4399 {
4400   smRoot_t                  *smRoot = agNULL;
4401   smIntRoot_t               *smIntRoot = agNULL;
4402   smIntContext_t            *smAllShared = agNULL;
4403   smIORequestBody_t         *smIORequestBody;
4404   smIORequestBody_t         *smOrgIORequestBody;
4405   smSatIOContext_t          *satIOContext;
4406   smSatIOContext_t          *satOrgIOContext;
4407   smSatInternalIo_t         *satIntIo;
4408 //  satDeviceData_t          *satDevData;
4409   smDeviceData_t            *oneDeviceData;
4410 
4411   smScsiRspSense_t          *pSense;
4412   smIORequest_t             *smOrgIORequest;
4413 
4414   agsaFisRegHostToDevice_t  *hostToDevFis = agNULL;
4415   bit32                      ataStatus = 0;
4416   smScsiInitiatorRequest_t  *smScsiRequest; /* tiScsiXchg */
4417   smScsiInitiatorRequest_t  *smOrgScsiRequest; /* tiScsiXchg */
4418   satReadLogExtSelfTest_t   *virtAddr1;
4419   satSmartReadLogSelfTest_t *virtAddr2;
4420   bit8                      *pLogPage;
4421   bit8                      LogPage[SELFTEST_RESULTS_LOG_PAGE_LENGTH];
4422   bit8                       SelfTestExecutionStatus = 0;
4423   bit32                      i = 0;
4424 
4425   agsaFisRegD2HHeader_t     *statDevToHostFisHeader = agNULL;
4426   agsaFisRegD2HData_t        statDevToHostFisData;
4427   smIniScsiCmnd_t           *scsiCmnd;
4428   bit32                      allocationLen = 0;
4429 
4430   SM_DBG2(("smsatLogSenseCB: start\n"));
4431   SM_DBG5(("smsatLogSenseCB:agIORequest=%p agIOStatus=0x%x agIOInfoLen %d\n", agIORequest, agIOStatus, agIOInfoLen));
4432 
4433   /* internally generate smIOContext */
4434   smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
4435   satIOContext           = (smSatIOContext_t *) ioContext;
4436   if (satIOContext == agNULL)
4437   {
4438     SM_DBG1(("smsatLogSenseCB: satIOContext is NULL\n"));
4439     return;
4440   }
4441   satIntIo               = satIOContext->satIntIoContext;
4442   oneDeviceData          = satIOContext->pSatDevData;
4443   hostToDevFis           = satIOContext->pFis;
4444   smRoot                 = oneDeviceData->smRoot;
4445   smIntRoot              = (smIntRoot_t *)smRoot->smData;
4446   smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
4447 
4448   if (satIntIo == agNULL)
4449   {
4450     SM_DBG4(("smsatLogSenseCB: External smSatInternalIo_t satIntIoContext\n"));
4451     satOrgIOContext = satIOContext;
4452     smOrgIORequest  = smIORequestBody->smIORequest;
4453     pSense          = satOrgIOContext->pSense;
4454     smOrgScsiRequest   = satOrgIOContext->smScsiXchg;
4455     /* SCSI command response payload to OS layer */
4456     pLogPage        = (bit8 *) smOrgScsiRequest->sglVirtualAddr;
4457     /* ATA command response payload */
4458     smScsiRequest   = satOrgIOContext->smScsiXchg;
4459     scsiCmnd        = satOrgIOContext->pScsiCmnd;
4460   }
4461   else
4462   {
4463     SM_DBG4(("smsatLogSenseCB: Internal smSatInternalIo_t satIntIoContext\n"));
4464     satOrgIOContext        = satIOContext->satOrgIOContext;
4465     smOrgIORequestBody     = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
4466     smOrgIORequest         = (smIORequest_t *)smOrgIORequestBody->smIORequest;
4467 
4468     pSense        = satOrgIOContext->pSense;
4469     smOrgScsiRequest   = satOrgIOContext->smScsiXchg;
4470     /* SCSI command response payload to OS layer */
4471     pLogPage        = (bit8 *) smOrgScsiRequest->sglVirtualAddr;
4472     /* ATA command response payload */
4473     smScsiRequest   =  (smScsiInitiatorRequest_t *)&(satIntIo->satIntSmScsiXchg);
4474     scsiCmnd        = satOrgIOContext->pScsiCmnd;
4475   }
4476 
4477   smIORequestBody->ioCompleted = agTRUE;
4478   smIORequestBody->ioStarted = agFALSE;
4479 
4480   if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
4481   {
4482     SM_DBG1(("smsatLogSenseCB: wrong. agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
4483     tdsmIOCompletedCB(
4484                        smRoot,
4485                        smOrgIORequest,
4486                        smIOFailed,
4487                        smDetailOtherError,
4488                        agNULL,
4489                        satOrgIOContext->interruptContext
4490                       );
4491     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
4492 
4493     smsatFreeIntIoResource( smRoot,
4494                             oneDeviceData,
4495                             satIntIo);
4496     return;
4497   }
4498 
4499   if (agIOStatus != OSSA_IO_SUCCESS)
4500   {
4501     /* non-data and pio read -> device to host and pio setup fis are expected */
4502     /*
4503       first, assumed to be Reg Device to Host FIS
4504       This is OK to just find fis type
4505     */
4506     statDevToHostFisHeader = (agsaFisRegD2HHeader_t *)&(agFirstDword->D2H);
4507     ataStatus     = statDevToHostFisHeader->status;   /* ATA Status register */
4508   }
4509 
4510   if( agIOStatus != OSSA_IO_SUCCESS)
4511   {
4512   if ( ((statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS) &&
4513         (statDevToHostFisHeader->fisType != PIO_SETUP_DEV_TO_HOST_FIS)) ||
4514        ((ataStatus & ERR_ATA_STATUS_MASK) || (ataStatus & DF_ATA_STATUS_MASK))
4515        )
4516   {
4517     /* for debugging */
4518     if( agIOStatus != OSSA_IO_SUCCESS)
4519     {
4520       SM_DBG1(("smsatLogSenseCB: FAILED, NOT IO_SUCCESS!!!\n"));
4521     }
4522     else if (statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS)
4523     {
4524       SM_DBG1(("smsatLogSenseCB: FAILED, Wrong FIS type 0x%x!!!\n",statDevToHostFisHeader->fisType));
4525     }
4526     else if (statDevToHostFisHeader->fisType != PIO_SETUP_DEV_TO_HOST_FIS)
4527     {
4528       SM_DBG1(("smsatLogSenseCB: FAILED, Wrong FIS type 0x%x!!!\n",statDevToHostFisHeader->fisType));
4529     }
4530     else if ( (ataStatus & ERR_ATA_STATUS_MASK) ||
4531               (ataStatus & DF_ATA_STATUS_MASK)
4532               )
4533     {
4534       SM_DBG1(("smsatLogSenseCB: FAILED, FAILED, error status!!!\n"));
4535     }
4536 
4537     /* Process abort case */
4538     if (agIOStatus == OSSA_IO_ABORTED)
4539     {
4540       smsatProcessAbort(smRoot,
4541                         smOrgIORequest,
4542                         satOrgIOContext
4543                         );
4544 
4545       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
4546 
4547       smsatFreeIntIoResource( smRoot,
4548                               oneDeviceData,
4549                               satIntIo);
4550       return;
4551     }
4552 
4553     /* for debugging */
4554     if (hostToDevFis->h.command == SAT_READ_LOG_EXT)
4555     {
4556       SM_DBG1(("smsatLogSenseCB: SAT_READ_LOG_EXT failed!!!\n"));
4557     }
4558     else if (hostToDevFis->h.command == SAT_SMART)
4559     {
4560       if (hostToDevFis->h.features == SAT_SMART_READ_LOG)
4561       {
4562         SM_DBG1(("smsatLogSenseCB: SAT_SMART_READ_LOG failed!!!\n"));
4563       }
4564       else if (hostToDevFis->h.features == SAT_SMART_RETURN_STATUS)
4565       {
4566         SM_DBG1(("smsatLogSenseCB: SAT_SMART_RETURN_STATUS failed!!!\n"));
4567       }
4568       else
4569       {
4570         SM_DBG1(("smsatLogSenseCB: error unknown command 0x%x feature 0x%x!!!\n", hostToDevFis->h.command, hostToDevFis->h.features));
4571       }
4572     }
4573     else
4574     {
4575       SM_DBG1(("smsatLogSenseCB: error default case command 0x%x!!!\n", hostToDevFis->h.command));
4576     }
4577 
4578     smsatSetSensePayload( pSense,
4579                           SCSI_SNSKEY_NO_SENSE,
4580                           0,
4581                           SCSI_SNSCODE_NO_ADDITIONAL_INFO,
4582                           satOrgIOContext);
4583 
4584     tdsmIOCompletedCB( smRoot,
4585                        smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
4586                        smIOSuccess,
4587                        SCSI_STAT_CHECK_CONDITION,
4588                        satOrgIOContext->pSmSenseData,
4589                        satOrgIOContext->interruptContext );
4590 
4591     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
4592 
4593     smsatFreeIntIoResource( smRoot,
4594                             oneDeviceData,
4595                             satIntIo);
4596     return;
4597 
4598   } /* error checking */
4599   }
4600 
4601   /* prcessing the success case */
4602   saFrameReadBlock(agRoot, agParam, 0, &statDevToHostFisData, sizeof(agsaFisRegD2HData_t));
4603 
4604   allocationLen = (scsiCmnd->cdb[7] << 8) + scsiCmnd->cdb[8];
4605   allocationLen = MIN(allocationLen, scsiCmnd->expDataLength);
4606   SM_DBG5(("smsatLogSenseCB: allocationLen in CDB %d 0x%x\n", allocationLen,allocationLen));
4607 
4608 
4609   if (hostToDevFis->h.command == SAT_READ_LOG_EXT)
4610   {
4611     SM_DBG5(("smsatLogSenseCB: SAT_READ_LOG_EXT success\n"));
4612 
4613     /* process log data and sends it to upper */
4614 
4615     /* ATA: Extended Self-Test Log */
4616     virtAddr1  = (satReadLogExtSelfTest_t *)(smScsiRequest->sglVirtualAddr);
4617     /*
4618       ATA/ATAPI VOLII, p197, 287
4619       self-test execution status (4 bits); ((virtAddr1->byte[5] & 0xF0) >> 4)
4620     */
4621     SelfTestExecutionStatus  = (bit8)(((virtAddr1->byte[5] & 0xF0) >> 4));
4622 
4623     /* fills in the log page from ATA log page */
4624     /* SPC-4, 7.2.10, Table 216, 217, p 259 - 260 */
4625     LogPage[0] = 0x10; /* page code */
4626     LogPage[1] = 0;
4627     LogPage[2] = 0x01;    /* 0x190, page length */
4628     LogPage[3] = 0x90;
4629 
4630     /* SPC-4, Table 217 */
4631     LogPage[4] = 0;    /* Parameter Code */
4632     LogPage[5] = 0x01; /* Parameter Code,  unspecfied but ... */
4633     LogPage[6] = 3;    /* unspecified but ... */
4634     LogPage[7] = 0x10; /* Parameter Length */
4635     LogPage[8] = (bit8)(0 | ((virtAddr1->byte[5] & 0xF0) >> 4)); /* Self Test Code and Self-Test Result */
4636     LogPage[9] = 0;    /* self test number */
4637     LogPage[10] = virtAddr1->byte[7];    /* time stamp, MSB */
4638     LogPage[11] = virtAddr1->byte[6];    /* time stamp, LSB */
4639 
4640     LogPage[12] = 0;    /* address of first failure MSB*/
4641     LogPage[13] = 0;    /* address of first failure */
4642     LogPage[14] = virtAddr1->byte[14];    /* address of first failure */
4643     LogPage[15] = virtAddr1->byte[13];    /* address of first failure */
4644     LogPage[16] = virtAddr1->byte[12];    /* address of first failure */
4645     LogPage[17] = virtAddr1->byte[11];    /* address of first failure */
4646     LogPage[18] = virtAddr1->byte[10];    /* address of first failure */
4647     LogPage[19] = virtAddr1->byte[9];    /* address of first failure LSB */
4648 
4649     /* SAT rev8 Table75, p 76 */
4650     switch (SelfTestExecutionStatus)
4651     {
4652     case 0:
4653       LogPage[20] = 0 | SCSI_SNSKEY_NO_SENSE;
4654       LogPage[21] = (SCSI_SNSCODE_NO_ADDITIONAL_INFO >> 8) & 0xFF;
4655       LogPage[22] = SCSI_SNSCODE_NO_ADDITIONAL_INFO & 0xFF;
4656       break;
4657     case 1:
4658       LogPage[20] = 0 | SCSI_SNSKEY_ABORTED_COMMAND;
4659       LogPage[21] = (SCSI_SNSCODE_DIAGNOSTIC_FAILURE_ON_COMPONENT_NN >> 8) & 0xFF;
4660       LogPage[22] = 0x81;
4661       break;
4662     case 2:
4663       LogPage[20] = 0 | SCSI_SNSKEY_ABORTED_COMMAND;
4664       LogPage[21] = (SCSI_SNSCODE_DIAGNOSTIC_FAILURE_ON_COMPONENT_NN >> 8) & 0xFF;
4665       LogPage[22] = 0x82;
4666       break;
4667     case 3:
4668       LogPage[20] = 0 | SCSI_SNSKEY_ABORTED_COMMAND;
4669       LogPage[21] = (SCSI_SNSCODE_DIAGNOSTIC_FAILURE_ON_COMPONENT_NN >> 8) & 0xFF;
4670       LogPage[22] = 0x83;
4671       break;
4672     case 4:
4673       LogPage[20] = 0 | SCSI_SNSKEY_HARDWARE_ERROR;
4674       LogPage[21] = (SCSI_SNSCODE_DIAGNOSTIC_FAILURE_ON_COMPONENT_NN >> 8) & 0xFF;
4675       LogPage[22] = 0x84;
4676     break;
4677     case 5:
4678       LogPage[20] = 0 | SCSI_SNSKEY_HARDWARE_ERROR;
4679       LogPage[21] = (SCSI_SNSCODE_DIAGNOSTIC_FAILURE_ON_COMPONENT_NN >> 8) & 0xFF;
4680       LogPage[22] = 0x85;
4681       break;
4682     case 6:
4683       LogPage[20] = 0 | SCSI_SNSKEY_HARDWARE_ERROR;
4684       LogPage[21] = (SCSI_SNSCODE_DIAGNOSTIC_FAILURE_ON_COMPONENT_NN >> 8) & 0xFF;
4685       LogPage[22] = 0x86;
4686       break;
4687     case 7:
4688       LogPage[20] = 0 | SCSI_SNSKEY_MEDIUM_ERROR;
4689       LogPage[21] = (SCSI_SNSCODE_DIAGNOSTIC_FAILURE_ON_COMPONENT_NN >> 8) & 0xFF;
4690       LogPage[22] = 0x87;
4691       break;
4692     case 8:
4693       LogPage[20] = 0 | SCSI_SNSKEY_HARDWARE_ERROR;
4694       LogPage[21] = (SCSI_SNSCODE_DIAGNOSTIC_FAILURE_ON_COMPONENT_NN >> 8) & 0xFF;
4695       LogPage[22] = 0x88;
4696       break;
4697     case 9: /* fall through */
4698     case 10:/* fall through */
4699     case 11:/* fall through */
4700     case 12:/* fall through */
4701     case 13:/* fall through */
4702     case 14:
4703       LogPage[20] = 0 | SCSI_SNSKEY_NO_SENSE;
4704       LogPage[21] = (SCSI_SNSCODE_NO_ADDITIONAL_INFO >> 8) & 0xFF;
4705       LogPage[22] = SCSI_SNSCODE_NO_ADDITIONAL_INFO & 0xFF;
4706       break;
4707     case 15:
4708       LogPage[20] = 0 | SCSI_SNSKEY_NO_SENSE;
4709       LogPage[21] = (SCSI_SNSCODE_NO_ADDITIONAL_INFO >> 8) & 0xFF;
4710       LogPage[22] = SCSI_SNSCODE_NO_ADDITIONAL_INFO & 0xFF;
4711       break;
4712     default:
4713       SM_DBG1(("smsatLogSenseCB: Error, incorrect SelfTestExecutionStatus 0x%x!!!\n", SelfTestExecutionStatus));
4714 
4715       smsatSetSensePayload( pSense,
4716                             SCSI_SNSKEY_NO_SENSE,
4717                             0,
4718                             SCSI_SNSCODE_NO_ADDITIONAL_INFO,
4719                             satOrgIOContext);
4720 
4721       tdsmIOCompletedCB( smRoot,
4722                          smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
4723                          smIOSuccess,
4724                          SCSI_STAT_CHECK_CONDITION,
4725                          satOrgIOContext->pSmSenseData,
4726                          satOrgIOContext->interruptContext );
4727 
4728       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
4729 
4730       smsatFreeIntIoResource( smRoot,
4731                               oneDeviceData,
4732                               satIntIo);
4733 
4734       return;
4735     }
4736 
4737     LogPage[23] = 0;    /* vendor specific */
4738 
4739     /* the rest of Self-test results log */
4740     /* 403 is from SPC-4, 7.2.10, Table 216, p 259*/
4741     for (i=24;i<=403;i++)
4742     {
4743       LogPage[i] = 0;    /* vendor specific */
4744     }
4745 
4746     sm_memcpy(pLogPage, LogPage, MIN(allocationLen, SELFTEST_RESULTS_LOG_PAGE_LENGTH));
4747     if (SELFTEST_RESULTS_LOG_PAGE_LENGTH < allocationLen)
4748     {
4749       SM_DBG6(("smsatLogSenseCB: 1st underrun allocationLen %d len %d \n", allocationLen, SELFTEST_RESULTS_LOG_PAGE_LENGTH));
4750 
4751       /* underrun */
4752       tdsmIOCompletedCB( smRoot,
4753                          smOrgIORequest, /* == satIntIo->satOrgSmIORequest */
4754                          smIOUnderRun,
4755                          allocationLen - SELFTEST_RESULTS_LOG_PAGE_LENGTH,
4756                          agNULL,
4757                          satOrgIOContext->interruptContext );
4758 
4759     }
4760     else
4761     {
4762       tdsmIOCompletedCB( smRoot,
4763                          smOrgIORequest,
4764                          smIOSuccess,
4765                          SCSI_STAT_GOOD,
4766                          agNULL,
4767                          satOrgIOContext->interruptContext);
4768     }
4769 
4770     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
4771 
4772     smsatFreeIntIoResource( smRoot,
4773                             oneDeviceData,
4774                             satIntIo);
4775 
4776     return;
4777   }
4778   else if (hostToDevFis->h.command == SAT_SMART)
4779   {
4780     if (hostToDevFis->h.features == SAT_SMART_READ_LOG)
4781     {
4782       SM_DBG5(("smsatLogSenseCB: SAT_SMART_READ_LOG success\n"));
4783       /* process log data and sends it to upper */
4784 
4785       /* ATA: Extended Self-Test Log */
4786       virtAddr2  = (satSmartReadLogSelfTest_t *)(smScsiRequest->sglVirtualAddr);
4787       /*
4788         SPC-4, p197, 287
4789         self-test execution status (4 bits); ((virtAddr2->byte[3] & 0xF0) >> 4)
4790       */
4791       SelfTestExecutionStatus  = (bit8)(((virtAddr2->byte[3] & 0xF0) >> 4));
4792 
4793       /* fills in the log page from ATA log page */
4794       /* SPC-4, 7.2.10, Table 216, 217, p 259 - 260 */
4795       LogPage[0] = 0x10;    /* page code */
4796       LogPage[1] = 0;
4797       LogPage[2] = 0x01;    /* 0x190, page length */
4798       LogPage[3] = 0x90;    /* 0x190, page length */
4799 
4800       /* SPC-4, Table 217 */
4801       LogPage[4] = 0;    /* Parameter Code */
4802       LogPage[5] = 0x01; /* Parameter Code unspecfied but ... */
4803       LogPage[6] = 3;    /* unspecified but ... */
4804       LogPage[7] = 0x10; /* Parameter Length */
4805       LogPage[8] = (bit8)(0 | ((virtAddr2->byte[3] & 0xF0) >> 4)); /* Self Test Code and Self-Test Result */
4806       LogPage[9] = 0;    /* self test number */
4807       LogPage[10] = virtAddr2->byte[5];    /* time stamp, MSB */
4808       LogPage[11] = virtAddr2->byte[4];    /* time stamp, LSB */
4809 
4810       LogPage[12] = 0;    /* address of first failure MSB*/
4811       LogPage[13] = 0;    /* address of first failure */
4812       LogPage[14] = 0;    /* address of first failure */
4813       LogPage[15] = 0;    /* address of first failure */
4814       LogPage[16] = virtAddr2->byte[10];    /* address of first failure */
4815       LogPage[17] = virtAddr2->byte[9];    /* address of first failure */
4816       LogPage[18] = virtAddr2->byte[8];    /* address of first failure */
4817       LogPage[19] = virtAddr2->byte[7];    /* address of first failure LSB */
4818 
4819       /* SAT rev8 Table75, p 76 */
4820       switch (SelfTestExecutionStatus)
4821       {
4822       case 0:
4823         LogPage[20] = 0 | SCSI_SNSKEY_NO_SENSE;
4824         LogPage[21] = (SCSI_SNSCODE_NO_ADDITIONAL_INFO >> 8) & 0xFF;
4825         LogPage[22] = SCSI_SNSCODE_NO_ADDITIONAL_INFO & 0xFF;
4826         break;
4827       case 1:
4828         LogPage[20] = 0 | SCSI_SNSKEY_ABORTED_COMMAND;
4829         LogPage[21] = (SCSI_SNSCODE_DIAGNOSTIC_FAILURE_ON_COMPONENT_NN >> 8) & 0xFF;
4830         LogPage[22] = 0x81;
4831         break;
4832       case 2:
4833         LogPage[20] = 0 | SCSI_SNSKEY_ABORTED_COMMAND;
4834         LogPage[21] = (SCSI_SNSCODE_DIAGNOSTIC_FAILURE_ON_COMPONENT_NN >> 8) & 0xFF;
4835         LogPage[22] = 0x82;
4836         break;
4837       case 3:
4838         LogPage[20] = 0 | SCSI_SNSKEY_ABORTED_COMMAND;
4839         LogPage[21] = (SCSI_SNSCODE_DIAGNOSTIC_FAILURE_ON_COMPONENT_NN >> 8) & 0xFF;
4840         LogPage[22] = 0x83;
4841         break;
4842       case 4:
4843         LogPage[20] = 0 | SCSI_SNSKEY_HARDWARE_ERROR;
4844         LogPage[21] = (SCSI_SNSCODE_DIAGNOSTIC_FAILURE_ON_COMPONENT_NN >> 8) & 0xFF;
4845         LogPage[22] = 0x84;
4846         break;
4847       case 5:
4848         LogPage[20] = 0 | SCSI_SNSKEY_HARDWARE_ERROR;
4849         LogPage[21] = (SCSI_SNSCODE_DIAGNOSTIC_FAILURE_ON_COMPONENT_NN >> 8) & 0xFF;
4850         LogPage[22] = 0x85;
4851         break;
4852       case 6:
4853         LogPage[20] = 0 | SCSI_SNSKEY_HARDWARE_ERROR;
4854         LogPage[21] = (SCSI_SNSCODE_DIAGNOSTIC_FAILURE_ON_COMPONENT_NN >> 8) & 0xFF;
4855         LogPage[22] = 0x86;
4856         break;
4857       case 7:
4858         LogPage[20] = 0 | SCSI_SNSKEY_MEDIUM_ERROR;
4859         LogPage[21] = (SCSI_SNSCODE_DIAGNOSTIC_FAILURE_ON_COMPONENT_NN >> 8) & 0xFF;
4860         LogPage[22] = 0x87;
4861         break;
4862       case 8:
4863         LogPage[20] = 0 | SCSI_SNSKEY_HARDWARE_ERROR;
4864         LogPage[21] = (SCSI_SNSCODE_DIAGNOSTIC_FAILURE_ON_COMPONENT_NN >> 8) & 0xFF;
4865         LogPage[22] = 0x88;
4866         break;
4867       case 9: /* fall through */
4868       case 10:/* fall through */
4869       case 11:/* fall through */
4870       case 12:/* fall through */
4871       case 13:/* fall through */
4872       case 14:
4873         /* unspecified */
4874         LogPage[20] = 0 | SCSI_SNSKEY_NO_SENSE;
4875         LogPage[21] = (SCSI_SNSCODE_NO_ADDITIONAL_INFO >> 8) & 0xFF;
4876         LogPage[22] = SCSI_SNSCODE_NO_ADDITIONAL_INFO & 0xFF;
4877         break;
4878       case 15:
4879         LogPage[20] = 0 | SCSI_SNSKEY_NO_SENSE;
4880         LogPage[21] = (SCSI_SNSCODE_NO_ADDITIONAL_INFO >> 8) & 0xFF;
4881         LogPage[22] = SCSI_SNSCODE_NO_ADDITIONAL_INFO & 0xFF;
4882         break;
4883       default:
4884         SM_DBG1(("smsatLogSenseCB: Error, incorrect SelfTestExecutionStatus 0x%x!!!\n", SelfTestExecutionStatus));
4885 
4886         smsatSetSensePayload( pSense,
4887                               SCSI_SNSKEY_NO_SENSE,
4888                               0,
4889                               SCSI_SNSCODE_NO_ADDITIONAL_INFO,
4890                               satOrgIOContext);
4891 
4892         tdsmIOCompletedCB( smRoot,
4893                            smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
4894                            smIOSuccess,
4895                            SCSI_STAT_CHECK_CONDITION,
4896                            satOrgIOContext->pSmSenseData,
4897                            satOrgIOContext->interruptContext );
4898 
4899         smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
4900 
4901         smsatFreeIntIoResource( smRoot,
4902                                 oneDeviceData,
4903                                 satIntIo);
4904 
4905         return;
4906       }
4907 
4908       LogPage[23] = 0;    /* vendor specific */
4909 
4910       /* the rest of Self-test results log */
4911       /* 403 is from SPC-4, 7.2.10, Table 216, p 259*/
4912       for (i=24;i<=403;i++)
4913       {
4914         LogPage[i] = 0;    /* vendor specific */
4915       }
4916 
4917       sm_memcpy(pLogPage, LogPage, MIN(allocationLen, SELFTEST_RESULTS_LOG_PAGE_LENGTH));
4918       if (SELFTEST_RESULTS_LOG_PAGE_LENGTH < allocationLen)
4919       {
4920         SM_DBG6(("smsatLogSenseCB: 2nd underrun allocationLen %d len %d \n", allocationLen, SELFTEST_RESULTS_LOG_PAGE_LENGTH));
4921 
4922         /* underrun */
4923         tdsmIOCompletedCB( smRoot,
4924                            smOrgIORequest, /* == satIntIo->satOrgSmIORequest */
4925                            smIOUnderRun,
4926                            allocationLen - SELFTEST_RESULTS_LOG_PAGE_LENGTH,
4927                            agNULL,
4928                            satOrgIOContext->interruptContext );
4929 
4930       }
4931       else
4932       {
4933         tdsmIOCompletedCB( smRoot,
4934                            smOrgIORequest,
4935                            smIOSuccess,
4936                            SCSI_STAT_GOOD,
4937                            agNULL,
4938                            satOrgIOContext->interruptContext);
4939       }
4940       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
4941 
4942       smsatFreeIntIoResource( smRoot,
4943                               oneDeviceData,
4944                               satIntIo);
4945 
4946       return;
4947     }
4948     else if (hostToDevFis->h.features == SAT_SMART_RETURN_STATUS)
4949     {
4950       SM_DBG5(("smsatLogSenseCB: SAT_SMART_RETURN_STATUS success\n"));
4951 
4952       /* fills in the log page from ATA output */
4953       /* SPC-4, 7.2.5, Table 209, 211, p 255 */
4954       LogPage[0] = 0x2F;    /* page code unspecified */
4955       LogPage[1] = 0;       /* reserved */
4956       LogPage[2] = 0;       /* page length */
4957       LogPage[3] = 0x07;    /* page length */
4958 
4959       /*
4960         SPC-4, 7.2.5, Table 211, p 255
4961         no vendor specific field
4962        */
4963       LogPage[4] = 0;    /* Parameter Code */
4964       LogPage[5] = 0;    /* Parameter Code unspecfied but to do: */
4965       LogPage[6] = 0;    /* unspecified */
4966       LogPage[7] = 0x03; /* Parameter length, unspecified */
4967 
4968       /* SAT rev8, 10.2.3.1 Table 72, p 73 */
4969       if (statDevToHostFisData.lbaMid == 0x4F || statDevToHostFisData.lbaHigh == 0xC2)
4970       {
4971         LogPage[8] = 0;   /* Sense code */
4972         LogPage[9] = 0;   /* Sense code qualifier */
4973       }
4974       else if (statDevToHostFisData.lbaMid == 0xF4 || statDevToHostFisData.lbaHigh == 0x2C)
4975       {
4976         LogPage[8] = 0x5D;   /* Sense code */
4977         LogPage[9] = 0x10;   /* Sense code qualifier */
4978       }
4979 
4980       /* Assumption: No support for SCT */
4981       LogPage[10] = 0xFF; /* Most Recent Temperature Reading */
4982 
4983       sm_memcpy(pLogPage, LogPage, MIN(allocationLen, INFORMATION_EXCEPTIONS_LOG_PAGE_LENGTH));
4984       if (INFORMATION_EXCEPTIONS_LOG_PAGE_LENGTH < allocationLen)
4985       {
4986         SM_DBG6(("smsatLogSenseCB: 3rd underrun allocationLen %d len %d \n", allocationLen, INFORMATION_EXCEPTIONS_LOG_PAGE_LENGTH));
4987 
4988         /* underrun */
4989         tdsmIOCompletedCB( smRoot,
4990                            smOrgIORequest, /* == satIntIo->satOrgSmIORequest */
4991                            smIOUnderRun,
4992                            allocationLen - INFORMATION_EXCEPTIONS_LOG_PAGE_LENGTH,
4993                            agNULL,
4994                            satOrgIOContext->interruptContext );
4995 
4996       }
4997       else
4998       {
4999         tdsmIOCompletedCB( smRoot,
5000                            smOrgIORequest,
5001                            smIOSuccess,
5002                            SCSI_STAT_GOOD,
5003                            agNULL,
5004                            satOrgIOContext->interruptContext);
5005       }
5006 
5007       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
5008 
5009       smsatFreeIntIoResource( smRoot,
5010                               oneDeviceData,
5011                               satIntIo);
5012 
5013 
5014       return;
5015     }
5016     else
5017     {
5018       SM_DBG1(("smsatLogSenseCB: error unknown command success 0x%x feature 0x%x!!!\n", hostToDevFis->h.command, hostToDevFis->h.features));
5019       smsatSetSensePayload( pSense,
5020                             SCSI_SNSKEY_NO_SENSE,
5021                             0,
5022                             SCSI_SNSCODE_NO_ADDITIONAL_INFO,
5023                             satOrgIOContext);
5024 
5025       tdsmIOCompletedCB( smRoot,
5026                          smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
5027                          smIOSuccess,
5028                          SCSI_STAT_CHECK_CONDITION,
5029                          satOrgIOContext->pSmSenseData,
5030                          satOrgIOContext->interruptContext );
5031       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
5032 
5033       smsatFreeIntIoResource( smRoot,
5034                               oneDeviceData,
5035                               satIntIo);
5036 
5037       return;
5038     }
5039   }
5040   else
5041   {
5042     SM_DBG1(("smsatLogSenseCB: error unknown command success 0x%x!!!\n", hostToDevFis->h.command));
5043     smsatSetSensePayload( pSense,
5044                           SCSI_SNSKEY_NO_SENSE,
5045                           0,
5046                           SCSI_SNSCODE_NO_ADDITIONAL_INFO,
5047                           satOrgIOContext);
5048 
5049     tdsmIOCompletedCB( smRoot,
5050                        smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
5051                        smIOSuccess,
5052                        SCSI_STAT_CHECK_CONDITION,
5053                        satOrgIOContext->pSmSenseData,
5054                        satOrgIOContext->interruptContext );
5055 
5056     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
5057 
5058     smsatFreeIntIoResource( smRoot,
5059                             oneDeviceData,
5060                             satIntIo);
5061 
5062     return;
5063   }
5064 
5065   return;
5066 }
5067 
5068 osGLOBAL void
5069 smsatSMARTEnableCB(
5070                    agsaRoot_t        *agRoot,
5071                    agsaIORequest_t   *agIORequest,
5072                    bit32             agIOStatus,
5073                    agsaFisHeader_t   *agFirstDword,
5074                    bit32             agIOInfoLen,
5075                    agsaFrameHandle_t agFrameHandle,
5076                    void              *ioContext
5077                   )
5078 {
5079 //  tdsaRootOsData_t        *osData = (tdsaRootOsData_t *)agRoot->osData;
5080 //  tiRoot_t                *tiRoot = (tiRoot_t *)osData->tiRoot;
5081 //  tdsaRoot_t              *tdsaRoot        = (tdsaRoot_t *) tiRoot->tdData;
5082 //  tdsaContext_t           *tdsaAllShared = (tdsaContext_t *)&tdsaRoot->tdsaAllShared;
5083   smRoot_t                 *smRoot = agNULL;
5084   smIntRoot_t              *smIntRoot = agNULL;
5085   smIntContext_t           *smAllShared = agNULL;
5086   smIORequestBody_t        *smIORequestBody;
5087   smIORequestBody_t        *smOrgIORequestBody;
5088   smSatIOContext_t         *satIOContext;
5089   smSatIOContext_t         *satOrgIOContext;
5090   smSatIOContext_t         *satNewIOContext;
5091   smSatInternalIo_t        *satIntIo;
5092   smSatInternalIo_t        *satNewIntIo = agNULL;
5093 //  satDeviceData_t           *satDevData;
5094   smDeviceData_t           *oneDeviceData;
5095   smIniScsiCmnd_t          *scsiCmnd;
5096   smIORequest_t            *smOrgIORequest;
5097   bit32                     status;
5098 
5099   SM_DBG2(("smsatSMARTEnableCB: start\n"));
5100   SM_DBG4(("smsatSMARTEnableCB:agIORequest=%p agIOStatus=0x%x agIOInfoLen %d\n", agIORequest, agIOStatus, agIOInfoLen));
5101 
5102   /* internally generate tiIOContext */
5103   smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
5104   satIOContext           = (smSatIOContext_t *) ioContext;
5105   satIntIo               = satIOContext->satIntIoContext;
5106   oneDeviceData          = satIOContext->pSatDevData;
5107   smRoot                 = oneDeviceData->smRoot;
5108   smIntRoot              = (smIntRoot_t *)smRoot->smData;
5109   smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
5110   /*ttttttthe one */
5111   if (satIntIo == agNULL)
5112   {
5113     SM_DBG4(("smsatSMARTEnableCB: External smSatInternalIo_t satIntIoContext\n"));
5114     satOrgIOContext = satIOContext;
5115     smOrgIORequest  = smIORequestBody->smIORequest;
5116     scsiCmnd        = satOrgIOContext->pScsiCmnd;
5117   }
5118   else
5119   {
5120     SM_DBG4(("smsatSMARTEnableCB: Internal smSatInternalIo_t satIntIoContext\n"));
5121     satOrgIOContext        = satIOContext->satOrgIOContext;
5122     if (satOrgIOContext == agNULL)
5123     {
5124       SM_DBG4(("smsatSMARTEnableCB: satOrgIOContext is NULL, wrong\n"));
5125       return;
5126     }
5127     else
5128     {
5129       SM_DBG4(("smsatSMARTEnableCB: satOrgIOContext is NOT NULL\n"));
5130     }
5131     smOrgIORequestBody     = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
5132     smOrgIORequest         = (smIORequest_t *)smOrgIORequestBody->smIORequest;
5133     scsiCmnd               = satOrgIOContext->pScsiCmnd;
5134   }
5135   smIORequestBody->ioCompleted = agTRUE;
5136   smIORequestBody->ioStarted = agFALSE;
5137 
5138   if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
5139   {
5140     SM_DBG1(("smsatSMARTEnableCB: wrong. agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
5141     tdsmIOCompletedCB(
5142                       smRoot,
5143                       smOrgIORequest,
5144                       smIOFailed,
5145                       smDetailOtherError,
5146                       agNULL,
5147                       satOrgIOContext->interruptContext
5148                      );
5149     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
5150     smsatFreeIntIoResource( smRoot,
5151                             oneDeviceData,
5152                             satIntIo);
5153     return;
5154   }
5155   /*
5156     checking IO status, FIS type and error status
5157   */
5158   if (agIOStatus != OSSA_IO_SUCCESS)
5159   {
5160     SM_DBG1(("smsatSMARTEnableCB: not success status, status %d!!!\n", agIOStatus));
5161     tdsmIOCompletedCB(
5162                        smRoot,
5163                        smOrgIORequest,
5164                        smIOFailed,
5165                        smDetailOtherError,
5166                        agNULL,
5167                        satOrgIOContext->interruptContext
5168                       );
5169     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
5170     smsatFreeIntIoResource( smRoot,
5171                             oneDeviceData,
5172                             satIntIo);
5173     return;
5174   }
5175   /* process success case */
5176   smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
5177   smsatFreeIntIoResource( smRoot,
5178                           oneDeviceData,
5179                           satIntIo);
5180   satNewIntIo = smsatAllocIntIoResource( smRoot,
5181                                          smOrgIORequest,
5182                                          oneDeviceData,
5183                                          512,
5184                                          satNewIntIo);
5185   if (satNewIntIo == agNULL)
5186   {
5187     tdsmIOCompletedCB(
5188                        smRoot,
5189                        smOrgIORequest,
5190                        smIOFailed,
5191                        smDetailOtherError,
5192                        agNULL,
5193                        satOrgIOContext->interruptContext
5194                       );
5195     return;
5196   }
5197   satNewIOContext = smsatPrepareNewIO(
5198                                       satNewIntIo,
5199                                       smOrgIORequest,
5200                                       oneDeviceData,
5201                                       scsiCmnd,
5202                                       satOrgIOContext
5203                                       );
5204   status = smsatLogSense_1(smRoot,
5205                            &satNewIntIo->satIntSmIORequest,
5206                            satNewIOContext->psmDeviceHandle,
5207                            &satNewIntIo->satIntSmScsiXchg,
5208                            satNewIOContext);
5209   if (status != SM_RC_SUCCESS)
5210   {
5211     /* sending SAT_CHECK_POWER_MODE fails */
5212     smsatFreeIntIoResource( smRoot,
5213                             oneDeviceData,
5214                             satNewIntIo);
5215     tdsmIOCompletedCB(
5216                       smRoot,
5217                       smOrgIORequest,
5218                       smIOFailed,
5219                       smDetailOtherError,
5220                       agNULL,
5221                       satOrgIOContext->interruptContext
5222                      );
5223     return;
5224   }
5225   return;
5226 }
5227 
5228 osGLOBAL void
5229 smsatModeSelect6n10CB(
5230                       agsaRoot_t        *agRoot,
5231                       agsaIORequest_t   *agIORequest,
5232                       bit32             agIOStatus,
5233                       agsaFisHeader_t   *agFirstDword,
5234                       bit32             agIOInfoLen,
5235                       agsaFrameHandle_t agFrameHandle,
5236                       void              *ioContext
5237                      )
5238 {
5239   smRoot_t                 *smRoot = agNULL;
5240   smIntRoot_t              *smIntRoot = agNULL;
5241   smIntContext_t           *smAllShared = agNULL;
5242   smIORequestBody_t        *smIORequestBody;
5243   smIORequestBody_t        *smOrgIORequestBody;
5244   smSatIOContext_t         *satIOContext;
5245   smSatIOContext_t         *satOrgIOContext;
5246   smSatIOContext_t         *satNewIOContext;
5247   smSatInternalIo_t        *satIntIo;
5248   smSatInternalIo_t        *satNewIntIo = agNULL;
5249 //  satDeviceData_t         *satDevData;
5250   smDeviceData_t           *oneDeviceData;
5251 
5252   smScsiRspSense_t         *pSense;
5253   smIniScsiCmnd_t          *scsiCmnd;
5254   smIORequest_t            *smOrgIORequest;
5255 
5256   agsaFisRegHostToDevice_t *hostToDevFis = agNULL;
5257   bit32                     ataStatus = 0;
5258   bit32                     status;
5259   smScsiInitiatorRequest_t *smScsiRequest; /* smScsiXchg */
5260   agsaFisRegD2HHeader_t    *statDevToHostFisHeader = agNULL;
5261 
5262   SM_DBG2(("smsatModeSelect6n10CB: start\n"));
5263   SM_DBG5(("smsatModeSelect6n10CB: agIORequest=%p agIOStatus=0x%x agIOInfoLen %d\n", agIORequest, agIOStatus, agIOInfoLen));
5264 
5265   /* internally generate smIOContext */
5266   smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
5267   satIOContext           = (smSatIOContext_t *) ioContext;
5268   satIntIo               = satIOContext->satIntIoContext;
5269   oneDeviceData          = satIOContext->pSatDevData;
5270   hostToDevFis           = satIOContext->pFis;
5271   smRoot                 = oneDeviceData->smRoot;
5272   smIntRoot              = (smIntRoot_t *)smRoot->smData;
5273   smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
5274 
5275   if (satIntIo == agNULL)
5276   {
5277     SM_DBG4(("smsatModeSelect6n10CB: External smSatInternalIo_t satIntIoContext\n"));
5278     satOrgIOContext = satIOContext;
5279     smOrgIORequest  = smIORequestBody->smIORequest;
5280     smScsiRequest   = satOrgIOContext->smScsiXchg;
5281     pSense          = satOrgIOContext->pSense;
5282     scsiCmnd        = satOrgIOContext->pScsiCmnd;
5283   }
5284   else
5285   {
5286     SM_DBG4(("smsatModeSelect6n10CB: Internal smSatInternalIo_t satIntIoContext\n"));
5287     satOrgIOContext        = satIOContext->satOrgIOContext;
5288     if (satOrgIOContext == agNULL)
5289     {
5290       SM_DBG4(("smsatModeSelect6n10CB: satOrgIOContext is NULL, wrong\n"));
5291       return;
5292     }
5293     else
5294     {
5295       SM_DBG4(("smsatModeSelect6n10CB: satOrgIOContext is NOT NULL\n"));
5296     }
5297     smOrgIORequestBody     = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
5298     smOrgIORequest         = (smIORequest_t *)smOrgIORequestBody->smIORequest;
5299 
5300     smScsiRequest = satOrgIOContext->smScsiXchg;
5301     pSense        = satOrgIOContext->pSense;
5302     scsiCmnd      = satOrgIOContext->pScsiCmnd;
5303   }
5304 
5305   smIORequestBody->ioCompleted = agTRUE;
5306   smIORequestBody->ioStarted = agFALSE;
5307 
5308   if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
5309   {
5310     SM_DBG1(("smsatModeSelect6n10CB: wrong. agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
5311     tdsmIOCompletedCB(
5312                        smRoot,
5313                        smOrgIORequest,
5314                        smIOFailed,
5315                        smDetailOtherError,
5316                        agNULL,
5317                        satOrgIOContext->interruptContext
5318                       );
5319     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
5320 
5321     smsatFreeIntIoResource( smRoot,
5322                             oneDeviceData,
5323                             satIntIo);
5324     return;
5325   }
5326 
5327   if (agIOStatus != OSSA_IO_SUCCESS)
5328   {
5329     /* only agsaFisRegDeviceToHost_t is expected */
5330     statDevToHostFisHeader = (agsaFisRegD2HHeader_t *)&(agFirstDword->D2H);
5331     ataStatus     = statDevToHostFisHeader->status;   /* ATA Status register */
5332   }
5333 
5334   if (agIOStatus != OSSA_IO_SUCCESS)
5335   {
5336   if ( (statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS) ||
5337        ((ataStatus & ERR_ATA_STATUS_MASK) || (ataStatus & DF_ATA_STATUS_MASK))
5338        )
5339   {
5340     /* for debugging */
5341     if( agIOStatus != OSSA_IO_SUCCESS)
5342     {
5343       SM_DBG1(("smsatModeSelect6n10CB: FAILED, NOT IO_SUCCESS!!!\n"));
5344     }
5345     else if (statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS)
5346     {
5347       SM_DBG1(("smsatModeSelect6n10CB: FAILED, Wrong FIS type 0x%x!!!\n",statDevToHostFisHeader->fisType));
5348     }
5349     else if ( (ataStatus & ERR_ATA_STATUS_MASK) ||
5350               (ataStatus & DF_ATA_STATUS_MASK)
5351               )
5352     {
5353       SM_DBG1(("smsatModeSelect6n10CB: FAILED, FAILED, error status!!!\n"));
5354     }
5355 
5356     /* Process abort case */
5357     if (agIOStatus == OSSA_IO_ABORTED)
5358     {
5359       smsatProcessAbort(smRoot,
5360                         smOrgIORequest,
5361                         satOrgIOContext
5362                         );
5363 
5364       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
5365 
5366       smsatFreeIntIoResource( smRoot,
5367                               oneDeviceData,
5368                               satIntIo);
5369       return;
5370     }
5371 
5372     /* for debugging */
5373     if (hostToDevFis->h.command == SAT_SET_FEATURES)
5374     {
5375       if ((hostToDevFis->h.features == 0x82) || (hostToDevFis->h.features == 0x02))
5376       {
5377         SM_DBG1(("smsatModeSelect6n10CB: 1 SAT_SET_FEATURES failed, feature 0x%x!!!\n", hostToDevFis->h.features));
5378       }
5379       else if ((hostToDevFis->h.features == 0xAA) || (hostToDevFis->h.features == 0x55))
5380       {
5381         SM_DBG1(("smsatModeSelect6n10CB: 2 SAT_SET_FEATURES failed, feature 0x%x!!!\n", hostToDevFis->h.features));
5382       }
5383       else
5384       {
5385         SM_DBG1(("smsatModeSelect6n10CB: error unknown command 0x%x feature 0x%x!!!\n", hostToDevFis->h.command, hostToDevFis->h.features));
5386       }
5387     }
5388     else if (hostToDevFis->h.command == SAT_SMART)
5389     {
5390       if ((hostToDevFis->h.features == SAT_SMART_ENABLE_OPERATIONS) || (hostToDevFis->h.features == SAT_SMART_DISABLE_OPERATIONS))
5391       {
5392         SM_DBG1(("smsatModeSelect6n10CB: SAT_SMART_ENABLE/DISABLE_OPERATIONS failed, feature 0x%x!!!\n", hostToDevFis->h.features));
5393       }
5394       else
5395       {
5396         SM_DBG1(("smsatModeSelect6n10CB: error unknown command 0x%x feature 0x%x!!!\n", hostToDevFis->h.command, hostToDevFis->h.features));
5397       }
5398     }
5399     else
5400     {
5401       SM_DBG1(("smsatModeSelect6n10CB: error default case command 0x%x!!!\n", hostToDevFis->h.command));
5402     }
5403 
5404 
5405     smsatSetSensePayload( pSense,
5406                           SCSI_SNSKEY_NO_SENSE,
5407                           0,
5408                           SCSI_SNSCODE_NO_ADDITIONAL_INFO,
5409                           satOrgIOContext);
5410 
5411     tdsmIOCompletedCB( smRoot,
5412                        smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
5413                        smIOSuccess,
5414                        SCSI_STAT_CHECK_CONDITION,
5415                        satOrgIOContext->pSmSenseData,
5416                        satOrgIOContext->interruptContext );
5417 
5418 
5419     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
5420 
5421     smsatFreeIntIoResource( smRoot,
5422                             oneDeviceData,
5423                             satIntIo);
5424     return;
5425   } /* error checking */
5426   }
5427 
5428 
5429   /* prcessing the success case */
5430 
5431 
5432   if (hostToDevFis->h.command == SAT_SET_FEATURES)
5433   {
5434     if ((hostToDevFis->h.features == 0x82) || (hostToDevFis->h.features == 0x02))
5435     {
5436       SM_DBG5(("smsatModeSelect6n10CB: 1 SAT_SET_FEATURES success, feature 0x%x\n", hostToDevFis->h.features));
5437       if (hostToDevFis->h.features == 0x02)
5438       {
5439         /* enable write cache */
5440         oneDeviceData->satWriteCacheEnabled = agTRUE;
5441       }
5442       else
5443       {
5444         /* disable write cache */
5445         oneDeviceData->satWriteCacheEnabled = agFALSE;
5446       }
5447 
5448       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
5449 
5450       smsatFreeIntIoResource( smRoot,
5451                               oneDeviceData,
5452                               satIntIo);
5453 
5454       satNewIntIo = smsatAllocIntIoResource( smRoot,
5455                                              smOrgIORequest,
5456                                              oneDeviceData,
5457                                              0,
5458                                              satNewIntIo);
5459       if (satNewIntIo == agNULL)
5460       {
5461         smsatSetSensePayload( pSense,
5462                               SCSI_SNSKEY_NO_SENSE,
5463                               0,
5464                               SCSI_SNSCODE_NO_ADDITIONAL_INFO,
5465                               satOrgIOContext);
5466 
5467         tdsmIOCompletedCB( smRoot,
5468                            smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
5469                            smIOSuccess,
5470                            SCSI_STAT_CHECK_CONDITION,
5471                            satOrgIOContext->pSmSenseData,
5472                            satOrgIOContext->interruptContext );
5473         SM_DBG1(("smsatModeSelect6n10CB: momory allocation fails!!!\n"));
5474         return;
5475       } /* end memory allocation */
5476 
5477       satNewIOContext = smsatPrepareNewIO(
5478                                           satNewIntIo,
5479                                           smOrgIORequest,
5480                                           oneDeviceData,
5481                                           scsiCmnd,
5482                                           satOrgIOContext
5483                                          );
5484       /* sends either ATA SET FEATURES based on DRA bit */
5485       status = smsatModeSelect6n10_1( smRoot,
5486                                       &satNewIntIo->satIntSmIORequest,
5487                                       satNewIOContext->psmDeviceHandle,
5488                                       smScsiRequest, /* orginal from OS layer */
5489                                       satNewIOContext
5490                                     );
5491 
5492       if (status != SM_RC_SUCCESS)
5493       {
5494         /* sending ATA command fails */
5495         smsatFreeIntIoResource( smRoot,
5496                                 oneDeviceData,
5497                                 satNewIntIo);
5498         smsatSetSensePayload( pSense,
5499                               SCSI_SNSKEY_NO_SENSE,
5500                               0,
5501                               SCSI_SNSCODE_NO_ADDITIONAL_INFO,
5502                               satOrgIOContext);
5503 
5504         tdsmIOCompletedCB( smRoot,
5505                            smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
5506                            smIOSuccess,
5507                            SCSI_STAT_CHECK_CONDITION,
5508                            satOrgIOContext->pSmSenseData,
5509                            satOrgIOContext->interruptContext );
5510         SM_DBG1(("smsatModeSelect6n10CB: calling satModeSelect6_1 fails!!!\n"));
5511         return;
5512       } /* end send fails */
5513       return;
5514     }
5515     else if ((hostToDevFis->h.features == 0xAA) || (hostToDevFis->h.features == 0x55))
5516     {
5517       SM_DBG5(("smsatModeSelect6n10CB: 2 SAT_SET_FEATURES success, feature 0x%x\n", hostToDevFis->h.features));
5518 
5519       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
5520 
5521       smsatFreeIntIoResource( smRoot,
5522                               oneDeviceData,
5523                               satIntIo);
5524 
5525       /* return stat_good */
5526       tdsmIOCompletedCB( smRoot,
5527                          smOrgIORequest,
5528                          smIOSuccess,
5529                          SCSI_STAT_GOOD,
5530                          agNULL,
5531                          satOrgIOContext->interruptContext );
5532       return;
5533     }
5534     else
5535     {
5536       SM_DBG1(("smsatModeSelect6n10CB: error unknown command success 0x%x feature 0x%x!!!\n", hostToDevFis->h.command, hostToDevFis->h.features));
5537 
5538       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
5539 
5540       smsatFreeIntIoResource( smRoot,
5541                               oneDeviceData,
5542                               satIntIo);
5543       smsatSetSensePayload( pSense,
5544                             SCSI_SNSKEY_NO_SENSE,
5545                             0,
5546                             SCSI_SNSCODE_NO_ADDITIONAL_INFO,
5547                             satOrgIOContext);
5548 
5549       tdsmIOCompletedCB( smRoot,
5550                          smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
5551                          smIOSuccess,
5552                          SCSI_STAT_CHECK_CONDITION,
5553                          satOrgIOContext->pSmSenseData,
5554                          satOrgIOContext->interruptContext );
5555       return;
5556     }
5557   }
5558   else if (hostToDevFis->h.command == SAT_SMART )
5559   {
5560     if ((hostToDevFis->h.features == SAT_SMART_ENABLE_OPERATIONS) || (hostToDevFis->h.features == SAT_SMART_DISABLE_OPERATIONS))
5561     {
5562       SM_DBG5(("smsatModeSelect6n10CB: SAT_SMART_ENABLE/DISABLE_OPERATIONS success, feature 0x%x\n", hostToDevFis->h.features));
5563 
5564       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
5565 
5566       smsatFreeIntIoResource( smRoot,
5567                               oneDeviceData,
5568                               satIntIo);
5569       /* return stat_good */
5570       tdsmIOCompletedCB( smRoot,
5571                          smOrgIORequest,
5572                          smIOSuccess,
5573                          SCSI_STAT_GOOD,
5574                          agNULL,
5575                          satOrgIOContext->interruptContext );
5576       return;
5577     }
5578     else
5579     {
5580       SM_DBG1(("smsatModeSelect6n10CB: error unknown command failed 0x%x feature 0x%x!!!\n", hostToDevFis->h.command, hostToDevFis->h.features));
5581 
5582       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
5583 
5584       smsatFreeIntIoResource( smRoot,
5585                               oneDeviceData,
5586                               satIntIo);
5587       smsatSetSensePayload( pSense,
5588                             SCSI_SNSKEY_NO_SENSE,
5589                             0,
5590                             SCSI_SNSCODE_NO_ADDITIONAL_INFO,
5591                             satOrgIOContext);
5592 
5593       tdsmIOCompletedCB( smRoot,
5594                          smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
5595                          smIOSuccess,
5596                          SCSI_STAT_CHECK_CONDITION,
5597                          satOrgIOContext->pSmSenseData,
5598                          satOrgIOContext->interruptContext );
5599       return;
5600     }
5601   }
5602 
5603   else
5604   {
5605     SM_DBG1(("smsatModeSelect6n10CB: error default case command success 0x%x!!!\n", hostToDevFis->h.command));
5606 
5607     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
5608 
5609     smsatFreeIntIoResource( smRoot,
5610                             oneDeviceData,
5611                             satIntIo);
5612 
5613     smsatSetSensePayload( pSense,
5614                           SCSI_SNSKEY_NO_SENSE,
5615                           0,
5616                           SCSI_SNSCODE_NO_ADDITIONAL_INFO,
5617                           satOrgIOContext);
5618 
5619     tdsmIOCompletedCB( smRoot,
5620                        smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
5621                        smIOSuccess,
5622                        SCSI_STAT_CHECK_CONDITION,
5623                        satOrgIOContext->pSmSenseData,
5624                        satOrgIOContext->interruptContext );
5625     return;
5626   }
5627 
5628   return;
5629 
5630 }
5631 
5632 osGLOBAL void
5633 smsatSynchronizeCache10n16CB(
5634                              agsaRoot_t        *agRoot,
5635                              agsaIORequest_t   *agIORequest,
5636                              bit32             agIOStatus,
5637                              agsaFisHeader_t   *agFirstDword,
5638                              bit32             agIOInfoLen,
5639                              agsaFrameHandle_t agFrameHandle,
5640                              void              *ioContext
5641                             )
5642 {
5643   /*
5644     In the process of SynchronizeCache10 and SynchronizeCache16
5645     Process SAT_FLUSH_CACHE_EXT
5646     Process SAT_FLUSH_CACHE
5647   */
5648 
5649 
5650   smRoot_t                 *smRoot = agNULL;
5651   smIntRoot_t              *smIntRoot = agNULL;
5652   smIntContext_t           *smAllShared = agNULL;
5653   smIORequestBody_t        *smIORequestBody;
5654   smIORequestBody_t        *smOrgIORequestBody;
5655   smSatIOContext_t         *satIOContext;
5656   smSatIOContext_t         *satOrgIOContext;
5657   smSatInternalIo_t        *satIntIo;
5658   smDeviceData_t           *oneDeviceData;
5659 
5660   smScsiRspSense_t         *pSense;
5661   smIniScsiCmnd_t          *scsiCmnd;
5662   smIORequest_t            *smOrgIORequest;
5663 
5664   agsaFisRegHostToDevice_t *hostToDevFis = agNULL;
5665   bit32                     ataStatus = 0;
5666   agsaFisRegD2HHeader_t    *statDevToHostFisHeader = agNULL;
5667 
5668   SM_DBG2(("smsatSynchronizeCache10n16CB: start\n"));
5669   SM_DBG5(("smsatSynchronizeCache10n16CB:agIORequest=%p agIOStatus=0x%x agIOInfoLen %d\n", agIORequest, agIOStatus, agIOInfoLen));
5670 
5671   /* internally generate smIOContext */
5672   smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
5673   satIOContext           = (smSatIOContext_t *) ioContext;
5674   satIntIo               = satIOContext->satIntIoContext;
5675   oneDeviceData          = satIOContext->pSatDevData;
5676   hostToDevFis           = satIOContext->pFis;
5677   smRoot                 = oneDeviceData->smRoot;
5678   smIntRoot              = (smIntRoot_t *)smRoot->smData;
5679   smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
5680 
5681   /* SPC: Self-Test Result Log page */
5682   if (satIntIo == agNULL)
5683   {
5684     SM_DBG4(("smsatSynchronizeCache10n16CB: External smSatInternalIo_t satIntIoContext\n"));
5685     satOrgIOContext = satIOContext;
5686     smOrgIORequest  = smIORequestBody->smIORequest;
5687     pSense          = satIOContext->pSense;
5688     scsiCmnd        = satIOContext->pScsiCmnd;
5689   }
5690   else
5691   {
5692     SM_DBG4(("smsatSynchronizeCache10n16CB: Internal smSatInternalIo_t satIntIoContext\n"));
5693     satOrgIOContext        = satIOContext->satOrgIOContext;
5694     if (satOrgIOContext == agNULL)
5695     {
5696       SM_DBG4(("smsatSynchronizeCache10n16CB: satOrgIOContext is NULL, wrong\n"));
5697       return;
5698     }
5699     else
5700     {
5701       SM_DBG4(("smsatSynchronizeCache10n16CB: satOrgIOContext is NOT NULL\n"));
5702     }
5703     smOrgIORequestBody     = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
5704     smOrgIORequest         = (smIORequest_t *)smOrgIORequestBody->smIORequest;
5705 
5706     pSense        = satOrgIOContext->pSense;
5707     scsiCmnd      = satOrgIOContext->pScsiCmnd;
5708   }
5709 
5710   smIORequestBody->ioCompleted = agTRUE;
5711   smIORequestBody->ioStarted = agFALSE;
5712 
5713   if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
5714   {
5715     SM_DBG1(("smsatSynchronizeCache10n16CB: wrong. agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
5716 
5717     tdsmIOCompletedCB(
5718                        smRoot,
5719                        smOrgIORequest,
5720                        smIOFailed,
5721                        smDetailOtherError,
5722                        agNULL,
5723                        satOrgIOContext->interruptContext
5724                       );
5725     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
5726 
5727     smsatFreeIntIoResource( smRoot,
5728                             oneDeviceData,
5729                             satIntIo);
5730     return;
5731   }
5732 
5733   if( agIOStatus != OSSA_IO_SUCCESS)
5734   {
5735     /* only agsaFisRegDeviceToHost_t is expected */
5736     statDevToHostFisHeader = (agsaFisRegD2HHeader_t *)&(agFirstDword->D2H);
5737     ataStatus     = statDevToHostFisHeader->status;   /* ATA Status register */
5738   }
5739 
5740   if( agIOStatus != OSSA_IO_SUCCESS)
5741   {
5742   if ( (statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS) ||
5743        ((ataStatus & ERR_ATA_STATUS_MASK) || (ataStatus & DF_ATA_STATUS_MASK))
5744        )
5745   {
5746     /* for debugging */
5747     if( agIOStatus != OSSA_IO_SUCCESS)
5748     {
5749       SM_DBG1(("smsatSynchronizeCache10n16CB: FAILED, NOT IO_SUCCESS!!!\n"));
5750     }
5751     else if (statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS)
5752     {
5753       SM_DBG1(("smsatSynchronizeCache10n16CB: FAILED, Wrong FIS type 0x%x!!!\n",statDevToHostFisHeader->fisType));
5754     }
5755     else if ( (ataStatus & ERR_ATA_STATUS_MASK) ||
5756               (ataStatus & DF_ATA_STATUS_MASK)
5757               )
5758     {
5759       SM_DBG1(("smsatSynchronizeCache10n16CB: FAILED, FAILED, error status!!!\n"));
5760     }
5761 
5762 
5763     /* Process abort case */
5764     if (agIOStatus == OSSA_IO_ABORTED)
5765     {
5766       smsatProcessAbort(smRoot,
5767                         smOrgIORequest,
5768                         satOrgIOContext
5769                         );
5770 
5771       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
5772 
5773       smsatFreeIntIoResource( smRoot,
5774                               oneDeviceData,
5775                               satIntIo);
5776       return;
5777     }
5778 
5779     switch (hostToDevFis->h.command)
5780     {
5781     case SAT_FLUSH_CACHE:
5782       SM_DBG1(("smsatSynchronizeCache10n16CB: SAT_FLUSH_CACHE failed!!!\n"));
5783       /* checking IMMED bit */
5784       if (scsiCmnd->cdb[1] & SCSI_FLUSH_CACHE_IMMED_MASK)
5785       {
5786         smsatSetDeferredSensePayload( pSense,
5787                                       SCSI_SNSKEY_NO_SENSE,
5788                                       0,
5789                                       SCSI_SNSCODE_NO_ADDITIONAL_INFO,
5790                                       satOrgIOContext);
5791       }
5792       else
5793       {
5794         smsatSetDeferredSensePayload( pSense,
5795                                       SCSI_SNSKEY_NO_SENSE,
5796                                       0,
5797                                       SCSI_SNSCODE_NO_ADDITIONAL_INFO,
5798                                       satOrgIOContext);
5799       }
5800 
5801 
5802       tdsmIOCompletedCB( smRoot,
5803                          smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
5804                          smIOSuccess,
5805                          SCSI_STAT_CHECK_CONDITION,
5806                          satOrgIOContext->pSmSenseData,
5807                          satOrgIOContext->interruptContext );
5808 
5809       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
5810 
5811       smsatFreeIntIoResource( smRoot,
5812                               oneDeviceData,
5813                               satIntIo);
5814       return;
5815       break;
5816     case SAT_FLUSH_CACHE_EXT:
5817       SM_DBG1(("smsatSynchronizeCache10n16CB: SAT_FLUSH_CACHE_EXT failed!!!\n"));
5818        /* checking IMMED bit */
5819       if (scsiCmnd->cdb[1] & SCSI_FLUSH_CACHE_IMMED_MASK)
5820       {
5821         smsatSetDeferredSensePayload( pSense,
5822                                       SCSI_SNSKEY_NO_SENSE,
5823                                       0,
5824                                       SCSI_SNSCODE_NO_ADDITIONAL_INFO,
5825                                       satOrgIOContext);
5826       }
5827       else
5828       {
5829         smsatSetDeferredSensePayload( pSense,
5830                                       SCSI_SNSKEY_NO_SENSE,
5831                                       0,
5832                                       SCSI_SNSCODE_NO_ADDITIONAL_INFO,
5833                                       satOrgIOContext);
5834       }
5835 
5836 
5837       tdsmIOCompletedCB( smRoot,
5838                          smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
5839                          smIOSuccess,
5840                          SCSI_STAT_CHECK_CONDITION,
5841                          satOrgIOContext->pSmSenseData,
5842                          satOrgIOContext->interruptContext );
5843 
5844       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
5845 
5846       smsatFreeIntIoResource( smRoot,
5847                               oneDeviceData,
5848                               satIntIo);
5849       return;
5850       break;
5851     default:
5852       SM_DBG1(("smsatSynchronizeCache10n16CB: error unknown command 0x%x!!!\n", hostToDevFis->h.command));
5853       smsatSetSensePayload( pSense,
5854                             SCSI_SNSKEY_NO_SENSE,
5855                             0,
5856                             SCSI_SNSCODE_NO_ADDITIONAL_INFO,
5857                             satOrgIOContext);
5858 
5859 
5860       tdsmIOCompletedCB( smRoot,
5861                          smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
5862                          smIOSuccess,
5863                          SCSI_STAT_CHECK_CONDITION,
5864                          satOrgIOContext->pSmSenseData,
5865                          satOrgIOContext->interruptContext );
5866 
5867       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
5868 
5869       smsatFreeIntIoResource( smRoot,
5870                               oneDeviceData,
5871                               satIntIo);
5872       return;
5873       break;
5874     }
5875 
5876     return;
5877   } /* end of error checking */
5878   }
5879 
5880   /* prcessing the success case */
5881   smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
5882 
5883   smsatFreeIntIoResource( smRoot,
5884                           oneDeviceData,
5885                           satIntIo);
5886 
5887 
5888   switch (hostToDevFis->h.command)
5889   {
5890   case SAT_FLUSH_CACHE:
5891     SM_DBG5(("smsatSynchronizeCache10n16CB: SAT_FLUSH_CACHE success\n"));
5892 
5893     /* checking IMMED bit */
5894     if ( !(scsiCmnd->cdb[1] & SCSI_FLUSH_CACHE_IMMED_MASK))
5895     {
5896       tdsmIOCompletedCB( smRoot,
5897                          smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
5898                          smIOSuccess,
5899                          SCSI_STAT_GOOD,
5900                          agNULL,
5901                          satOrgIOContext->interruptContext );
5902       return;
5903     }
5904 
5905 
5906     break;
5907   case SAT_FLUSH_CACHE_EXT:
5908     SM_DBG5(("smsatSynchronizeCache10n16CB: SAT_FLUSH_CACHE_EXT success\n"));
5909 
5910     /* checking IMMED bit */
5911     if ( !(scsiCmnd->cdb[1] & SCSI_FLUSH_CACHE_IMMED_MASK))
5912     {
5913       tdsmIOCompletedCB( smRoot,
5914                          smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
5915                          smIOSuccess,
5916                          SCSI_STAT_GOOD,
5917                          agNULL,
5918                          satOrgIOContext->interruptContext );
5919       return;
5920     }
5921 
5922     break;
5923   default:
5924     SM_DBG5(("smsatSynchronizeCache10n16CB: error unknown command 0x%x\n", hostToDevFis->h.command));
5925     smsatSetSensePayload( pSense,
5926                           SCSI_SNSKEY_NO_SENSE,
5927                           0,
5928                           SCSI_SNSCODE_NO_ADDITIONAL_INFO,
5929                           satOrgIOContext);
5930 
5931 
5932     tdsmIOCompletedCB( smRoot,
5933                        smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
5934                        smIOSuccess,
5935                        SCSI_STAT_CHECK_CONDITION,
5936                        satOrgIOContext->pSmSenseData,
5937                        satOrgIOContext->interruptContext );
5938 
5939     return;
5940     break;
5941   }
5942 
5943   return;
5944 }
5945 
5946 //qqqqqqqq
5947 osGLOBAL void
5948 smsatNonChainedWriteNVerifyCB(
5949                               agsaRoot_t        *agRoot,
5950                               agsaIORequest_t   *agIORequest,
5951                               bit32             agIOStatus,
5952                               agsaFisHeader_t   *agFirstDword,
5953                               bit32             agIOInfoLen,
5954                               void              *agParam,
5955                               void              *ioContext
5956                              )
5957 {
5958   /*
5959     In the process of WriteAndVerify10
5960     Process SAT_WRITE_DMA_FUA_EXT
5961     Process SAT_WRITE_DMA_EXT
5962     Process SAT_WRITE_SECTORS_EXT
5963     Process SAT_WRITE_FPDMA_QUEUED
5964     Process SAT_READ_VERIFY_SECTORS
5965     Process SAT_READ_VERIFY_SECTORS_EXT
5966     chained command
5967   */
5968 
5969 
5970   smRoot_t                *smRoot = agNULL;
5971   smIntRoot_t             *smIntRoot = agNULL;
5972   smIntContext_t          *smAllShared = agNULL;
5973   smIORequestBody_t       *smIORequestBody;
5974   smIORequestBody_t       *smOrgIORequestBody;
5975   smSatIOContext_t          *satIOContext;
5976   smSatIOContext_t          *satOrgIOContext;
5977   smSatIOContext_t          *satNewIOContext;
5978   smSatInternalIo_t         *satIntIo;
5979   smSatInternalIo_t         *satNewIntIo = agNULL;
5980 //  satDeviceData_t         *satDevData;
5981   smDeviceData_t          *oneDeviceData;
5982 
5983   smScsiRspSense_t          *pSense;
5984   smIniScsiCmnd_t           *scsiCmnd;
5985   smIORequest_t             *smOrgIORequest;
5986 
5987   agsaFisRegHostToDevice_t  *hostToDevFis = agNULL;
5988   bit32                     ataStatus = 0;
5989   bit32                     status;
5990   smScsiInitiatorRequest_t  *smScsiRequest; /* smScsiXchg */
5991   agsaFisRegD2HHeader_t     *statDevToHostFisHeader = agNULL;
5992   agsaFisSetDevBitsHeader_t *statSetDevBitFisHeader = agNULL;
5993 
5994   /* internally generate smIOContext */
5995   smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
5996   satIOContext           = (smSatIOContext_t *) ioContext;
5997   satIntIo               = satIOContext->satIntIoContext;
5998   oneDeviceData          = satIOContext->pSatDevData;
5999   hostToDevFis           = satIOContext->pFis;
6000   smRoot                 = oneDeviceData->smRoot;
6001   smIntRoot              = (smIntRoot_t *)smRoot->smData;
6002   smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
6003 
6004   /* SPC: Self-Test Result Log page */
6005   smScsiRequest          = satIOContext->smScsiXchg;
6006 
6007   SM_DBG2(("smsatNonChainedWriteNVerifyCB: start\n"));
6008   SM_DBG5(("smsatNonChainedWriteNVerifyCB: start agIORequest=%p agIOStatus=0x%x agIOInfoLen %d\n", agIORequest, agIOStatus, agIOInfoLen));
6009 
6010 
6011   if (satIntIo == agNULL)
6012   {
6013     SM_DBG4(("smsatNonChainedWriteNVerifyCB: External smSatInternalIo_t satIntIoContext\n"));
6014     satOrgIOContext = satIOContext;
6015     smOrgIORequest  = smIORequestBody->smIORequest;
6016     pSense          = satOrgIOContext->pSense;
6017     scsiCmnd        = satOrgIOContext->pScsiCmnd;
6018   }
6019   else
6020   {
6021     SM_DBG4(("smsatNonChainedWriteNVerifyCB: Internal smSatInternalIo_t satIntIoContext\n"));
6022     satOrgIOContext        = satIOContext->satOrgIOContext;
6023     if (satOrgIOContext == agNULL)
6024     {
6025       SM_DBG4(("smsatNonChainedWriteNVerifyCB: satOrgIOContext is NULL, wrong\n"));
6026       return;
6027     }
6028     else
6029     {
6030       SM_DBG4(("smsatNonChainedWriteNVerifyCB: satOrgIOContext is NOT NULL\n"));
6031     }
6032     smOrgIORequestBody     = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
6033     smOrgIORequest         = (smIORequest_t *)smOrgIORequestBody->smIORequest;
6034 
6035     pSense        = satOrgIOContext->pSense;
6036     scsiCmnd      = satOrgIOContext->pScsiCmnd;
6037   }
6038 
6039   smIORequestBody->ioCompleted = agTRUE;
6040   smIORequestBody->ioStarted = agFALSE;
6041 
6042 
6043   if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
6044   {
6045     SM_DBG1(("smsatNonChainedWriteNVerifyCB: wrong. agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
6046     tdsmIOCompletedCB( smRoot,
6047                        smOrgIORequest,
6048                        smIOFailed,
6049                        smDetailOtherError,
6050                        agNULL,
6051                        satOrgIOContext->interruptContext
6052                        );
6053     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
6054 
6055     smsatFreeIntIoResource( smRoot,
6056                             oneDeviceData,
6057                             satIntIo);
6058     return;
6059   }
6060 
6061 
6062   if (agIOStatus != OSSA_IO_SUCCESS)
6063   {
6064     /*
6065       FIS type should be either REG_DEV_TO_HOST_FIS or SET_DEV_BITS_FIS
6066     */
6067     /* First, assumed to be Reg Device to Host FIS */
6068     statDevToHostFisHeader = (agsaFisRegD2HHeader_t *)&(agFirstDword->D2H);
6069     ataStatus     = statDevToHostFisHeader->status;   /* ATA Status register */
6070   }
6071 
6072   if (agIOStatus != OSSA_IO_SUCCESS)
6073   {
6074     if (statDevToHostFisHeader->fisType == SET_DEV_BITS_FIS)
6075     {
6076       statSetDevBitFisHeader = (agsaFisSetDevBitsHeader_t *)&(agFirstDword->D2H);
6077 
6078       /* Get ATA Status register */
6079       ataStatus = (statSetDevBitFisHeader->statusHi_Lo & 0x70);               /* bits 4,5,6 */
6080       ataStatus = ataStatus | (statSetDevBitFisHeader->statusHi_Lo & 0x07);   /* bits 0,1,2 */
6081    }
6082   }
6083 
6084 
6085   if( agIOStatus != OSSA_IO_SUCCESS)
6086   {
6087   /*
6088     checking IO status, FIS type and error status
6089     FIS type should be either REG_DEV_TO_HOST_FIS or SET_DEV_BITS_FIS
6090     Both have fisType in the same location
6091   */
6092   if ( ((statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS) &&
6093         (statDevToHostFisHeader->fisType != SET_DEV_BITS_FIS)) ||
6094        ((ataStatus & ERR_ATA_STATUS_MASK) || (ataStatus & DF_ATA_STATUS_MASK))
6095        )
6096     {
6097       /* for debugging */
6098       if( agIOStatus != OSSA_IO_SUCCESS)
6099       {
6100         SM_DBG1(("smsatNonChainedWriteNVerifyCB: FAILED, NOT IO_SUCCESS!!!\n"));
6101       }
6102       else if (statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS)
6103       {
6104         SM_DBG1(("smsatNonChainedWriteNVerifyCB: FAILED, Wrong FIS type 0x%x!!!\n",statDevToHostFisHeader->fisType));
6105       }
6106       else if (statDevToHostFisHeader->fisType != SET_DEV_BITS_FIS)
6107       {
6108         SM_DBG1(("smsatNonChainedWriteNVerifyCB: FAILED, Wrong FIS type 0x%x!!!\n",statDevToHostFisHeader->fisType));
6109       }
6110       else if ( (ataStatus & ERR_ATA_STATUS_MASK) ||
6111                 (ataStatus & DF_ATA_STATUS_MASK)
6112                 )
6113       {
6114         SM_DBG1(("smsatNonChainedWriteNVerifyCB: FAILED, FAILED, error status!!!\n"));
6115       }
6116 
6117 
6118       /* Process abort case */
6119       if (agIOStatus == OSSA_IO_ABORTED)
6120       {
6121         smsatProcessAbort(smRoot,
6122                           smOrgIORequest,
6123                           satOrgIOContext
6124                           );
6125 
6126         smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
6127 
6128         smsatFreeIntIoResource( smRoot,
6129                                 oneDeviceData,
6130                                 satIntIo);
6131         return;
6132       }
6133 
6134       /* for debugging */
6135       switch (hostToDevFis->h.command)
6136       {
6137       case SAT_WRITE_DMA_FUA_EXT:
6138         SM_DBG1(("smsatNonChainedWriteNVerifyCB: SAT_WRITE_DMA_FUA_EXT!!!\n"));
6139         break;
6140       case SAT_WRITE_DMA_EXT:
6141         SM_DBG1(("smsatNonChainedWriteNVerifyCB: SAT_WRITE_DMA_EXT!!!\n"));
6142         break;
6143       case SAT_WRITE_SECTORS_EXT:
6144         SM_DBG1(("smsatNonChainedWriteNVerifyCB: SAT_WRITE_SECTORS_EXT!!!\n"));
6145         break;
6146       case SAT_WRITE_FPDMA_QUEUED:
6147         SM_DBG1(("smsatNonChainedWriteNVerifyCB: SAT_WRITE_FPDMA_QUEUED!!!\n"));
6148         break;
6149       case SAT_READ_VERIFY_SECTORS:
6150         SM_DBG1(("smsatNonChainedWriteNVerifyCB: SAT_READ_VERIFY_SECTORS!!!\n"));
6151         break;
6152       case SAT_READ_VERIFY_SECTORS_EXT:
6153         SM_DBG1(("smsatNonChainedWriteNVerifyCB: SAT_READ_VERIFY_SECTORS_EXT!!!\n"));
6154         break;
6155       default:
6156         SM_DBG1(("smsatNonChainedWriteNVerifyCB: error default case command 0x%x!!!\n", hostToDevFis->h.command));
6157         break;
6158       }
6159 
6160       smsatSetSensePayload( pSense,
6161                             SCSI_SNSKEY_NO_SENSE,
6162                             0,
6163                             SCSI_SNSCODE_NO_ADDITIONAL_INFO,
6164                             satOrgIOContext);
6165 
6166       tdsmIOCompletedCB( smRoot,
6167                          smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
6168                          smIOSuccess,
6169                          SCSI_STAT_CHECK_CONDITION,
6170                          satOrgIOContext->pSmSenseData,
6171                          satOrgIOContext->interruptContext );
6172 
6173       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
6174 
6175       smsatFreeIntIoResource( smRoot,
6176                               oneDeviceData,
6177                               satIntIo);
6178       return;
6179     } /* end error checking */
6180   }
6181 
6182   /* process success from this point on */
6183 
6184   switch (hostToDevFis->h.command)
6185   {
6186   case SAT_WRITE_DMA_FUA_EXT:
6187     SM_DBG5(("smsatNonChainedWriteNVerifyCB: SAT_WRITE_DMA_FUA_EXT success\n"));
6188     break;
6189   case SAT_WRITE_DMA_EXT:
6190     SM_DBG5(("smsatNonChainedWriteNVerifyCB: SAT_WRITE_DMA_EXT success\n"));
6191     break;
6192   case SAT_WRITE_SECTORS_EXT:
6193     SM_DBG5(("smsatNonChainedWriteNVerifyCB: SAT_WRITE_SECTORS_EXT succes\n"));
6194 
6195     break;
6196   case SAT_WRITE_FPDMA_QUEUED:
6197     SM_DBG5(("smsatNonChainedWriteNVerifyCB: SAT_WRITE_FPDMA_QUEUED succes\n"));
6198     break;
6199   case SAT_READ_VERIFY_SECTORS:
6200     SM_DBG5(("smsatNonChainedWriteNVerifyCB: SAT_READ_VERIFY_SECTORS succes\n"));
6201     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
6202 
6203     /* free */
6204     smsatFreeIntIoResource( smRoot,
6205                             oneDeviceData,
6206                             satIntIo);
6207 
6208     /* return stat_good */
6209     tdsmIOCompletedCB( smRoot,
6210                        smOrgIORequest,
6211                        smIOSuccess,
6212                        SCSI_STAT_GOOD,
6213                        agNULL,
6214                        satOrgIOContext->interruptContext );
6215     return;
6216     break;
6217   case SAT_READ_VERIFY_SECTORS_EXT:
6218     SM_DBG5(("smsatNonChainedWriteNVerifyCB: SAT_READ_VERIFY_SECTORS_EXT succes\n"));
6219     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
6220 
6221     /* free */
6222     smsatFreeIntIoResource( smRoot,
6223                             oneDeviceData,
6224                             satIntIo);
6225 
6226     /* return stat_good */
6227     tdsmIOCompletedCB( smRoot,
6228                        smOrgIORequest,
6229                        smIOSuccess,
6230                        SCSI_STAT_GOOD,
6231                        agNULL,
6232                        satOrgIOContext->interruptContext );
6233     return;
6234     break;
6235   default:
6236     SM_DBG1(("smsatNonChainedWriteNVerifyCB:  error default case command 0x%x success!!!\n", hostToDevFis->h.command));
6237 
6238     smsatSetSensePayload( pSense,
6239                           SCSI_SNSKEY_NO_SENSE,
6240                           0,
6241                           SCSI_SNSCODE_NO_ADDITIONAL_INFO,
6242                           satOrgIOContext);
6243 
6244     tdsmIOCompletedCB( smRoot,
6245                        smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
6246                        smIOSuccess,
6247                        SCSI_STAT_CHECK_CONDITION,
6248                        satOrgIOContext->pSmSenseData,
6249                        satOrgIOContext->interruptContext );
6250 
6251     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
6252 
6253     smsatFreeIntIoResource( smRoot,
6254                             oneDeviceData,
6255                             satIntIo);
6256     return;
6257     break;
6258   }
6259 
6260   smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
6261 
6262   /* free */
6263   smsatFreeIntIoResource( smRoot,
6264                           oneDeviceData,
6265                           satIntIo);
6266 
6267   satNewIntIo = smsatAllocIntIoResource( smRoot,
6268                                          smOrgIORequest,
6269                                          oneDeviceData,
6270                                          0,
6271                                          satNewIntIo);
6272   if (satNewIntIo == agNULL)
6273   {
6274     smsatSetSensePayload( pSense,
6275                           SCSI_SNSKEY_NO_SENSE,
6276                           0,
6277                           SCSI_SNSCODE_NO_ADDITIONAL_INFO,
6278                           satOrgIOContext);
6279 
6280     tdsmIOCompletedCB( smRoot,
6281                        smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
6282                        smIOSuccess,
6283                        SCSI_STAT_CHECK_CONDITION,
6284                        satOrgIOContext->pSmSenseData,
6285                        satOrgIOContext->interruptContext );
6286     SM_DBG1(("smsatNonChainedWriteNVerifyCB: momory allocation fails!!!\n"));
6287     return;
6288   } /* end memory allocation */
6289 
6290   satNewIOContext = smsatPrepareNewIO(satNewIntIo,
6291                                       smOrgIORequest,
6292                                       oneDeviceData,
6293                                       scsiCmnd,
6294                                       satOrgIOContext
6295                                      );
6296 
6297   /* sends ATA verify command(READ_VERIFY_SECTORS or READ_VERIFY_SECTORS_EXT) */
6298   status = smsatNonChainedWriteNVerify_Verify(smRoot,
6299                                               &satNewIntIo->satIntSmIORequest,
6300                                               satNewIOContext->psmDeviceHandle,
6301                                               smScsiRequest, /* orginal from OS layer */
6302                                               satNewIOContext
6303                                              );
6304 
6305 
6306   if (status != SM_RC_SUCCESS)
6307   {
6308     /* sending ATA command fails */
6309     smsatFreeIntIoResource( smRoot,
6310                             oneDeviceData,
6311                             satNewIntIo);
6312     smsatSetSensePayload( pSense,
6313                           SCSI_SNSKEY_NO_SENSE,
6314                           0,
6315                           SCSI_SNSCODE_NO_ADDITIONAL_INFO,
6316                           satOrgIOContext);
6317 
6318     tdsmIOCompletedCB( smRoot,
6319                        smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
6320                        smIOSuccess,
6321                        SCSI_STAT_CHECK_CONDITION,
6322                        satOrgIOContext->pSmSenseData,
6323                        satOrgIOContext->interruptContext );
6324     SM_DBG1(("smsatNonChainedWriteNVerifyCB: calling satWriteAndVerify10_1 fails!!!\n"));
6325     return;
6326   } /* end send fails */
6327 
6328   return;
6329 
6330 }
6331 
6332 osGLOBAL void
6333 smsatChainedWriteNVerifyCB(
6334                            agsaRoot_t        *agRoot,
6335                            agsaIORequest_t   *agIORequest,
6336                            bit32             agIOStatus,
6337                            agsaFisHeader_t   *agFirstDword,
6338                            bit32             agIOInfoLen,
6339                            void              *agParam,
6340                            void              *ioContext
6341                           )
6342 {
6343   /*
6344     send write in loop
6345     then, send verify in loop
6346   */
6347 
6348   smRoot_t                *smRoot = agNULL;
6349   smIntRoot_t             *smIntRoot = agNULL;
6350   smIntContext_t          *smAllShared = agNULL;
6351 //  tdsaRootOsData_t        *osData = (tdsaRootOsData_t *)agRoot->osData;
6352 //  tiRoot_t                *tiRoot = (tiRoot_t *)osData->tiRoot;
6353 //  tdsaRoot_t              *tdsaRoot        = (tdsaRoot_t *) tiRoot->tdData;
6354 //  tdsaContext_t           *tdsaAllShared = (tdsaContext_t *)&tdsaRoot->tdsaAllShared;
6355   smIORequestBody_t        *smIORequestBody;
6356   smIORequestBody_t        *smOrgIORequestBody;
6357   smSatIOContext_t         *satIOContext;
6358   smSatIOContext_t         *satOrgIOContext;
6359   smSatIOContext_t         *satNewIOContext;
6360   smSatInternalIo_t        *satIntIo;
6361   smSatInternalIo_t        *satNewIntIo = agNULL;
6362 //  satDeviceData_t         *satDevData;
6363   smDeviceData_t           *oneDeviceData;
6364 
6365   smScsiRspSense_t         *pSense;
6366   smIniScsiCmnd_t          *scsiCmnd;
6367   smIORequest_t            *smOrgIORequest;
6368   agsaFisRegHostToDevice_t *hostToDevFis = agNULL;
6369   bit32                     ataStatus = 0;
6370   bit32                     dataLength;
6371   bit32                     status = tiError;
6372   agsaFisRegD2HHeader_t    *statDevToHostFisHeader = agNULL;
6373 
6374   smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
6375   satIOContext           = (smSatIOContext_t *) ioContext;
6376   satIntIo               = satIOContext->satIntIoContext;
6377   oneDeviceData          = satIOContext->pSatDevData;
6378   hostToDevFis           = satIOContext->pFis;
6379   smRoot                 = oneDeviceData->smRoot;
6380   smIntRoot              = (smIntRoot_t *)smRoot->smData;
6381   smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
6382   SM_DBG2(("smsatChainedWriteNVerifyCB: start\n"));
6383   SM_DBG6(("smsatChainedWriteNVerifyCB: agIORequest=%p agIOStatus=0x%x agIOInfoLen %d\n",
6384            agIORequest, agIOStatus, agIOInfoLen));
6385 
6386   if (satIntIo == agNULL)
6387   {
6388     SM_DBG5(("smsatChainedWriteNVerifyCB: External smSatInternalIo_t satIntIoContext\n"));
6389     satOrgIOContext = satIOContext;
6390     smOrgIORequest  = smIORequestBody->smIORequest;
6391     pSense          = satIOContext->pSense;
6392     scsiCmnd        = satIOContext->pScsiCmnd;
6393   }
6394   else
6395   {
6396     SM_DBG5(("smsatChainedWriteNVerifyCB: Internal smSatInternalIo_t satIntIoContext\n"));
6397     satOrgIOContext        = satIOContext->satOrgIOContext;
6398     if (satOrgIOContext == agNULL)
6399     {
6400       SM_DBG5(("smsatChainedWriteNVerifyCB: satOrgIOContext is NULL, wrong\n"));
6401       return;
6402     }
6403     else
6404     {
6405       SM_DBG5(("smsatChainedWriteNVerifyCB: satOrgIOContext is NOT NULL\n"));
6406     }
6407     smOrgIORequestBody     = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
6408     smOrgIORequest         = (smIORequest_t *)smOrgIORequestBody->smIORequest;
6409 
6410     pSense        = satOrgIOContext->pSense;
6411     scsiCmnd      = satOrgIOContext->pScsiCmnd;
6412   }
6413 
6414   smIORequestBody->ioCompleted = agTRUE;
6415   smIORequestBody->ioStarted = agFALSE;
6416 
6417   if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
6418   {
6419      SM_DBG1(("smsatChainedWriteNVerifyCB: wrong. agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
6420      smsatSetSensePayload( pSense,
6421                            SCSI_SNSKEY_NO_SENSE,
6422                            0,
6423                            SCSI_SNSCODE_NO_ADDITIONAL_INFO,
6424                            satOrgIOContext);
6425 
6426     tdsmIOCompletedCB( smRoot,
6427                        smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
6428                        smIOSuccess,
6429                        SCSI_STAT_CHECK_CONDITION,
6430                        satOrgIOContext->pSmSenseData,
6431                        satOrgIOContext->interruptContext );
6432 
6433     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
6434 
6435     smsatFreeIntIoResource( smRoot,
6436                             oneDeviceData,
6437                             satIntIo);
6438     return;
6439   }
6440 
6441   /*
6442     checking IO status, FIS type and error status
6443   */
6444   if (agIOStatus != OSSA_IO_SUCCESS)
6445   {
6446     /* agsaFisPioSetup_t or agsaFisRegDeviceToHost_t or agsaFisSetDevBits_t for read
6447        agsaFisRegDeviceToHost_t or agsaFisSetDevBits_t for write
6448        first, assumed to be Reg Device to Host FIS
6449        This is OK to just find fis type
6450     */
6451     statDevToHostFisHeader = (agsaFisRegD2HHeader_t *)&(agFirstDword->D2H);
6452     ataStatus     = statDevToHostFisHeader->status;   /* ATA Status register */
6453     /* for debugging */
6454     if( (statDevToHostFisHeader->fisType != PIO_SETUP_DEV_TO_HOST_FIS) &&
6455         (statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS) &&
6456         (statDevToHostFisHeader->fisType != SET_DEV_BITS_FIS)
6457         )
6458     {
6459       SM_DBG1(("smsatChainedWriteNVerifyCB: FAILED, Wrong FIS type 0x%x!!!\n", statDevToHostFisHeader->fisType));
6460     }
6461 
6462     /* for debugging */
6463     if ( (ataStatus & ERR_ATA_STATUS_MASK) ||
6464          (ataStatus & DF_ATA_STATUS_MASK)
6465          )
6466     {
6467       SM_DBG1(("smsatChainedWriteNVerifyCB: FAILED, error status and command 0x%x!!!\n", hostToDevFis->h.command));
6468     }
6469 
6470     /* the function below handles abort case */
6471     smsatDelayedProcessAbnormalCompletion(agRoot,
6472                                           agIORequest,
6473                                           agIOStatus,
6474                                           agFirstDword,
6475                                           agIOInfoLen,
6476                                           agParam,
6477                                           satIOContext);
6478 
6479     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
6480     smsatFreeIntIoResource( smRoot,
6481                             oneDeviceData,
6482                             satIntIo);
6483     return;
6484   } /* end of error */
6485 
6486   /* process the success case */
6487   switch (hostToDevFis->h.command)
6488   {
6489   case SAT_WRITE_DMA: /* fall through */
6490   case SAT_WRITE_SECTORS:/* fall through */
6491 //  case SAT_WRITE_DMA_FUA_EXT: /* fall through */
6492   case SAT_WRITE_DMA_EXT: /* fall through */
6493   case SAT_WRITE_SECTORS_EXT: /* fall through */
6494   case SAT_WRITE_FPDMA_QUEUED:
6495 
6496     SM_DBG5(("smsatChainedWriteNVerifyCB: WRITE success case\n"));
6497 
6498     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
6499 
6500     /* done with internally genereated SAT_SMART_RETURN_STATUS */
6501     smsatFreeIntIoResource( smRoot,
6502                             oneDeviceData,
6503                             satIntIo);
6504     /* let's loop till TL */
6505 
6506     /* lba = lba + tl
6507        loopnum--;
6508        if (loopnum == 0) done
6509      */
6510     (satOrgIOContext->LoopNum)--;
6511 
6512     if (satOrgIOContext->superIOFlag)
6513     {
6514         dataLength = ((tiSuperScsiInitiatorRequest_t *) satOrgIOContext->smScsiXchg)->scsiCmnd.expDataLength;
6515     }
6516     else
6517     {
6518         dataLength = ((tiScsiInitiatorRequest_t *) satOrgIOContext->smScsiXchg)->scsiCmnd.expDataLength;
6519     }
6520 
6521     satNewIntIo = smsatAllocIntIoResource( smRoot,
6522                                            smOrgIORequest,
6523                                            oneDeviceData,
6524                                            dataLength,
6525                                            satNewIntIo);
6526     if (satNewIntIo == agNULL)
6527     {
6528       tdsmIOCompletedCB( smRoot,
6529                          smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
6530                          smIOFailed,
6531                          smDetailOtherError,
6532                          agNULL,
6533                          satOrgIOContext->interruptContext );
6534 
6535       SM_DBG1(("smsatChainedWriteNVerifyCB: momory allocation fails!!!\n"));
6536       return;
6537     } /* end of memory allocation failure */
6538 
6539     /*
6540      * Need to initialize all the fields within satIOContext
6541      */
6542 
6543     satNewIOContext = smsatPrepareNewIO(
6544                                         satNewIntIo,
6545                                         smOrgIORequest,
6546                                         oneDeviceData,
6547                                         scsiCmnd,
6548                                         satOrgIOContext
6549                                         );
6550 
6551     if (satOrgIOContext->LoopNum == 0)
6552     {
6553       /*
6554         done with write
6555         start with verify
6556       */
6557       satOrgIOContext->LoopNum = satOrgIOContext->LoopNum2;
6558       status = smsatChainedWriteNVerify_Start_Verify(smRoot,
6559                                                      &satNewIntIo->satIntSmIORequest,
6560                                                      satNewIOContext->psmDeviceHandle,
6561                                                      &satNewIntIo->satIntSmScsiXchg,
6562                                                      satNewIOContext);
6563     }
6564     else
6565     {
6566       status = smsatChainedWriteNVerify_Write(smRoot,
6567                                              &satNewIntIo->satIntSmIORequest,
6568                                              satNewIOContext->psmDeviceHandle,
6569                                              &satNewIntIo->satIntSmScsiXchg,
6570                                              satNewIOContext);
6571     }
6572 
6573     if (status != SM_RC_SUCCESS)
6574     {
6575       smsatFreeIntIoResource( smRoot,
6576                               oneDeviceData,
6577                               satNewIntIo);
6578       tdsmIOCompletedCB( smRoot,
6579                          smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
6580                          smIOFailed,
6581                          smDetailOtherError,
6582                          agNULL,
6583                          satOrgIOContext->interruptContext );
6584       SM_DBG1(("smsatChainedWriteNVerifyCB: calling satChainedWriteNVerify_Write fails!!!\n"));
6585       return;
6586     }
6587 
6588     break;
6589 
6590   case SAT_READ_VERIFY_SECTORS: /* fall through */
6591   case SAT_READ_VERIFY_SECTORS_EXT:
6592     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
6593 
6594     /* done with internally genereated SAT_SMART_RETURN_STATUS */
6595     smsatFreeIntIoResource( smRoot,
6596                             oneDeviceData,
6597                             satIntIo);
6598     /* let's loop till TL */
6599 
6600     /* lba = lba + tl
6601        loopnum--;
6602        if (loopnum == 0) done
6603      */
6604     (satOrgIOContext->LoopNum)--;
6605     if (satOrgIOContext->LoopNum == 0)
6606     {
6607       /*
6608         done with write and verify
6609       */
6610       tdsmIOCompletedCB( smRoot,
6611                          smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
6612                          smIOSuccess,
6613                          SCSI_STAT_GOOD,
6614                          agNULL,
6615                          satOrgIOContext->interruptContext );
6616       return;
6617     }
6618 
6619     if (satOrgIOContext->superIOFlag)
6620     {
6621         dataLength = ((tiSuperScsiInitiatorRequest_t *) satOrgIOContext->smScsiXchg)->scsiCmnd.expDataLength;
6622     }
6623     else
6624     {
6625         dataLength = ((tiScsiInitiatorRequest_t *) satOrgIOContext->smScsiXchg)->scsiCmnd.expDataLength;
6626     }
6627 
6628     satNewIntIo = smsatAllocIntIoResource( smRoot,
6629                                            smOrgIORequest,
6630                                            oneDeviceData,
6631                                            dataLength,
6632                                            satNewIntIo);
6633     if (satNewIntIo == agNULL)
6634     {
6635       tdsmIOCompletedCB( smRoot,
6636                          smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
6637                          smIOFailed,
6638                          smDetailOtherError,
6639                          agNULL,
6640                          satOrgIOContext->interruptContext );
6641 
6642       SM_DBG1(("smsatChainedWriteNVerifyCB: momory allocation fails!!!\n"));
6643       return;
6644     } /* end of memory allocation failure */
6645 
6646     /*
6647      * Need to initialize all the fields within satIOContext
6648      */
6649 
6650     satNewIOContext = smsatPrepareNewIO(
6651                                         satNewIntIo,
6652                                         smOrgIORequest,
6653                                         oneDeviceData,
6654                                         scsiCmnd,
6655                                         satOrgIOContext
6656                                         );
6657     status = smsatChainedWriteNVerify_Verify(smRoot,
6658                                              &satNewIntIo->satIntSmIORequest,
6659                                              satNewIOContext->psmDeviceHandle,
6660                                              &satNewIntIo->satIntSmScsiXchg,
6661                                              satNewIOContext);
6662 
6663     if (status != SM_RC_SUCCESS)
6664     {
6665       smsatFreeIntIoResource( smRoot,
6666                               oneDeviceData,
6667                               satNewIntIo);
6668       tdsmIOCompletedCB( smRoot,
6669                          smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
6670                          smIOFailed,
6671                          smDetailOtherError,
6672                          agNULL,
6673                          satOrgIOContext->interruptContext );
6674       SM_DBG1(("smsatChainedWriteNVerifyCB: calling satChainedWriteNVerify_Verify fails!!!\n"));
6675       return;
6676     }
6677 
6678     break;
6679 
6680   default:
6681     SM_DBG1(("smsatChainedWriteNVerifyCB: success but default case command 0x%x!!!\n",hostToDevFis->h.command));
6682     tdsmIOCompletedCB( smRoot,
6683                        smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
6684                        smIOFailed,
6685                        smDetailOtherError,
6686                        agNULL,
6687                        satOrgIOContext->interruptContext );
6688 
6689     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
6690 
6691     smsatFreeIntIoResource( smRoot,
6692                             oneDeviceData,
6693                             satIntIo);
6694 
6695     break;
6696   }
6697 
6698 
6699   return;
6700 }
6701 
6702 osGLOBAL void
6703 smsatReadMediaSerialNumberCB(
6704                              agsaRoot_t        *agRoot,
6705                              agsaIORequest_t   *agIORequest,
6706                              bit32             agIOStatus,
6707                              agsaFisHeader_t   *agFirstDword,
6708                              bit32             agIOInfoLen,
6709                              agsaFrameHandle_t agFrameHandle,
6710                              void              *ioContext
6711                             )
6712 {
6713 //  tdsaRootOsData_t        *osData = (tdsaRootOsData_t *)agRoot->osData;
6714 //  tiRoot_t                *tiRoot = (tiRoot_t *)osData->tiRoot;
6715 //  tdsaRoot_t              *tdsaRoot        = (tdsaRoot_t *) tiRoot->tdData;
6716 //  tdsaContext_t           *tdsaAllShared = (tdsaContext_t *)&tdsaRoot->tdsaAllShared;
6717   smRoot_t                 *smRoot = agNULL;
6718   smIntRoot_t              *smIntRoot = agNULL;
6719   smIntContext_t           *smAllShared = agNULL;
6720   smIORequestBody_t        *smIORequestBody;
6721   smIORequestBody_t        *smOrgIORequestBody;
6722   smSatIOContext_t         *satIOContext;
6723   smSatIOContext_t         *satOrgIOContext;
6724   smSatInternalIo_t        *satIntIo;
6725 //  satDeviceData_t           *satDevData;
6726   smDeviceData_t           *oneDeviceData;
6727 
6728   smScsiRspSense_t         *pSense;
6729   smIORequest_t            *smOrgIORequest;
6730 
6731   agsaFisRegHostToDevice_t *hostToDevFis = agNULL;
6732   smScsiInitiatorRequest_t *smOrgScsiRequest; /* tiScsiXchg */
6733   bit8                     *pMediaSerialNumber;
6734   bit8                      MediaSerialNumber[ZERO_MEDIA_SERIAL_NUMBER_LENGTH] = {0};
6735   smIniScsiCmnd_t          *scsiCmnd;
6736   bit32                     allocationLen = 0;
6737 
6738   SM_DBG2(("smsatReadMediaSerialNumberCB: start\n"));
6739   SM_DBG4(("smsatReadMediaSerialNumberCB:agIORequest=%p agIOStatus=0x%x agIOInfoLen %d\n", agIORequest, agIOStatus, agIOInfoLen));
6740 
6741   /* internally generate tiIOContext */
6742   smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
6743   satIOContext           = (smSatIOContext_t *) ioContext;
6744   satIntIo               = satIOContext->satIntIoContext;
6745   oneDeviceData          = satIOContext->pSatDevData;
6746   hostToDevFis           = satIOContext->pFis;
6747   smRoot                 = oneDeviceData->smRoot;
6748   smIntRoot              = (smIntRoot_t *)smRoot->smData;
6749   smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
6750 
6751   if (satIntIo == agNULL)
6752   {
6753     SM_DBG4(("smsatReadMediaSerialNumberCB: External smSatInternalIo_t satIntIoContext\n"));
6754     satOrgIOContext = satIOContext;
6755     smOrgIORequest  = smIORequestBody->smIORequest;
6756     pSense          = satOrgIOContext->pSense;
6757     smOrgScsiRequest          = satOrgIOContext->smScsiXchg;
6758     /* SCSI command response payload to OS layer */
6759     pMediaSerialNumber        = (bit8 *) smOrgScsiRequest->sglVirtualAddr;
6760     /* ATA command response payload */
6761     scsiCmnd        = satOrgIOContext->pScsiCmnd;
6762   }
6763   else
6764   {
6765     SM_DBG4(("smsatReadMediaSerialNumberCB: Internal smSatInternalIo_t satIntIoContext\n"));
6766     satOrgIOContext        = satIOContext->satOrgIOContext;
6767     if (satOrgIOContext == agNULL)
6768     {
6769       SM_DBG4(("smsatReadMediaSerialNumberCB: satOrgIOContext is NULL, wrong\n"));
6770       return;
6771     }
6772     else
6773     {
6774       SM_DBG4(("smsatReadMediaSerialNumberCB: satOrgIOContext is NOT NULL\n"));
6775     }
6776     smOrgIORequestBody = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
6777     smOrgIORequest     = (smIORequest_t *)smOrgIORequestBody->smIORequest;
6778 
6779     pSense             = satOrgIOContext->pSense;
6780     smOrgScsiRequest   = satOrgIOContext->smScsiXchg;
6781     /* SCSI command response payload to OS layer */
6782     pMediaSerialNumber = (bit8 *) smOrgScsiRequest->sglVirtualAddr;
6783     scsiCmnd           = satOrgIOContext->pScsiCmnd;
6784   }
6785 
6786   smIORequestBody->ioCompleted = agTRUE;
6787   smIORequestBody->ioStarted = agFALSE;
6788 
6789   if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
6790   {
6791     SM_DBG1(("smsatReadMediaSerialNumberCB: wrong. agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
6792     tdsmIOCompletedCB(
6793                        smRoot,
6794                        smOrgIORequest,
6795                        smIOFailed,
6796                        smDetailOtherError,
6797                        agNULL,
6798                        satOrgIOContext->interruptContext
6799                       );
6800     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
6801 
6802     smsatFreeIntIoResource( smRoot,
6803                             oneDeviceData,
6804                             satIntIo);
6805     return;
6806   }
6807 
6808   if( agIOStatus != OSSA_IO_SUCCESS)
6809   {
6810     /* Process abort case */
6811     if (agIOStatus == OSSA_IO_ABORTED)
6812     {
6813       smsatProcessAbort(smRoot,
6814                         smOrgIORequest,
6815                         satOrgIOContext
6816                         );
6817 
6818       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
6819 
6820       smsatFreeIntIoResource( smRoot,
6821                               oneDeviceData,
6822                               satIntIo);
6823       return;
6824     }
6825     smsatSetSensePayload( pSense,
6826                           SCSI_SNSKEY_NOT_READY,
6827                           0,
6828                           SCSI_SNSCODE_MEDIUM_NOT_PRESENT,
6829                           satOrgIOContext);
6830 
6831     tdsmIOCompletedCB( smRoot,
6832                        smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
6833                        smIOSuccess,
6834                        SCSI_STAT_CHECK_CONDITION,
6835                        satOrgIOContext->pSmSenseData,
6836                        satOrgIOContext->interruptContext );
6837 
6838     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
6839 
6840     smsatFreeIntIoResource( smRoot,
6841                             oneDeviceData,
6842                             satIntIo);
6843     return;
6844   }
6845 
6846   /* process success case */
6847   allocationLen = (scsiCmnd->cdb[6] << (8*3)) + (scsiCmnd->cdb[7] << (8*2))
6848                 + (scsiCmnd->cdb[8] << 8) + scsiCmnd->cdb[9];
6849   allocationLen = MIN(allocationLen, scsiCmnd->expDataLength);
6850   SM_DBG5(("smsatReadMediaSerialNumberCB: allocationLen in CDB %d 0x%x\n", allocationLen,allocationLen));
6851 
6852   if (hostToDevFis->h.command == SAT_READ_SECTORS ||
6853       hostToDevFis->h.command == SAT_READ_SECTORS_EXT
6854      )
6855   {
6856     MediaSerialNumber[0] = 0;
6857     MediaSerialNumber[1] = 0;
6858     MediaSerialNumber[2] = 0;
6859     MediaSerialNumber[3] = 4;
6860     MediaSerialNumber[4] = 0;
6861     MediaSerialNumber[5] = 0;
6862     MediaSerialNumber[6] = 0;
6863     MediaSerialNumber[7] = 0;
6864 
6865     sm_memcpy(pMediaSerialNumber, MediaSerialNumber, MIN(allocationLen, ZERO_MEDIA_SERIAL_NUMBER_LENGTH));
6866     if (ZERO_MEDIA_SERIAL_NUMBER_LENGTH < allocationLen)
6867     {
6868       SM_DBG1(("smsatReadMediaSerialNumberCB: 1st underrun allocationLen %d len %d !!!\n", allocationLen, ZERO_MEDIA_SERIAL_NUMBER_LENGTH));
6869 
6870       /* underrun */
6871       tdsmIOCompletedCB( smRoot,
6872                          smOrgIORequest, /* == satIntIo->satOrgSmIORequest */
6873                          smIOUnderRun,
6874                          allocationLen - ZERO_MEDIA_SERIAL_NUMBER_LENGTH,
6875                          agNULL,
6876                          satOrgIOContext->interruptContext );
6877 
6878     }
6879     else
6880     {
6881       tdsmIOCompletedCB( smRoot,
6882                          smOrgIORequest,
6883                          smIOSuccess,
6884                          SCSI_STAT_GOOD,
6885                          agNULL,
6886                          satOrgIOContext->interruptContext);
6887     }
6888     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
6889 
6890     smsatFreeIntIoResource( smRoot,
6891                             oneDeviceData,
6892                             satIntIo);
6893     return;
6894   }
6895   else
6896   {
6897     SM_DBG1(("smsatReadMediaSerialNumberCB: error unknown command success 0x%x!!!\n", hostToDevFis->h.command));
6898     smsatSetSensePayload( pSense,
6899                           SCSI_SNSKEY_NO_SENSE,
6900                           0,
6901                           SCSI_SNSCODE_NO_ADDITIONAL_INFO,
6902                           satOrgIOContext);
6903 
6904     tdsmIOCompletedCB( smRoot,
6905                        smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
6906                        smIOSuccess,
6907                        SCSI_STAT_CHECK_CONDITION,
6908                        satOrgIOContext->pSmSenseData,
6909                        satOrgIOContext->interruptContext );
6910 
6911     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
6912 
6913     smsatFreeIntIoResource( smRoot,
6914                             oneDeviceData,
6915                             satIntIo);
6916 
6917     return;
6918   }
6919   return;
6920 }
6921 
6922 osGLOBAL void
6923 smsatReadBufferCB(
6924                   agsaRoot_t        *agRoot,
6925                   agsaIORequest_t   *agIORequest,
6926                   bit32             agIOStatus,
6927                   agsaFisHeader_t   *agFirstDword,
6928                   bit32             agIOInfoLen,
6929                   agsaFrameHandle_t agFrameHandle,
6930                   void              *ioContext
6931                  )
6932 {
6933   smRoot_t                 *smRoot = agNULL;
6934   smIntRoot_t              *smIntRoot = agNULL;
6935   smIntContext_t           *smAllShared = agNULL;
6936   smIORequestBody_t        *smIORequestBody;
6937   smIORequestBody_t        *smOrgIORequestBody;
6938   smSatIOContext_t         *satIOContext;
6939   smSatIOContext_t         *satOrgIOContext;
6940   smSatInternalIo_t        *satIntIo;
6941   smDeviceData_t           *oneDeviceData;
6942   smScsiRspSense_t         *pSense;
6943   smIORequest_t            *smOrgIORequest;
6944   agsaFisRegHostToDevice_t *hostToDevFis = agNULL;
6945 
6946 
6947   SM_DBG2(("smsatReadBufferCB: start\n"));
6948   SM_DBG4(("smsatReadBufferCB:agIORequest=%p agIOStatus=0x%x agIOInfoLen %d\n", agIORequest, agIOStatus, agIOInfoLen));
6949   /* internally generate tiIOContext */
6950   smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
6951   satIOContext           = (smSatIOContext_t *) ioContext;
6952   satIntIo               = satIOContext->satIntIoContext;
6953   oneDeviceData          = satIOContext->pSatDevData;
6954   hostToDevFis           = satIOContext->pFis;
6955   smRoot                 = oneDeviceData->smRoot;
6956   smIntRoot              = (smIntRoot_t *)smRoot->smData;
6957   smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
6958   if (satIntIo == agNULL)
6959   {
6960     SM_DBG4(("smsatReadBufferCB: External smSatInternalIo_t satIntIoContext\n"));
6961     satOrgIOContext = satIOContext;
6962     smOrgIORequest  = smIORequestBody->smIORequest;
6963     pSense          = satOrgIOContext->pSense;
6964   }
6965   else
6966   {
6967     SM_DBG4(("smsatReadBufferCB: Internal smSatInternalIo_t satIntIoContext\n"));
6968     satOrgIOContext        = satIOContext->satOrgIOContext;
6969     if (satOrgIOContext == agNULL)
6970     {
6971       SM_DBG4(("smsatReadBufferCB: satOrgIOContext is NULL, wrong\n"));
6972       return;
6973     }
6974     else
6975     {
6976       SM_DBG4(("smsatReadBufferCB: satOrgIOContext is NOT NULL\n"));
6977     }
6978     smOrgIORequestBody     = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
6979     smOrgIORequest         = (smIORequest_t *)smOrgIORequestBody->smIORequest;
6980     pSense        = satOrgIOContext->pSense;
6981   }
6982   smIORequestBody->ioCompleted = agTRUE;
6983   smIORequestBody->ioStarted = agFALSE;
6984   if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
6985   {
6986     SM_DBG1(("smsatReadBufferCB: wrong. agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
6987     tdsmIOCompletedCB(
6988                        smRoot,
6989                        smOrgIORequest,
6990                        smIOFailed,
6991                        smDetailOtherError,
6992                        agNULL,
6993                        satOrgIOContext->interruptContext
6994                       );
6995     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
6996 
6997     smsatFreeIntIoResource( smRoot,
6998                             oneDeviceData,
6999                             satIntIo);
7000     return;
7001   }
7002 
7003   if( agIOStatus != OSSA_IO_SUCCESS)
7004   {
7005     /* Process abort case */
7006     if (agIOStatus == OSSA_IO_ABORTED)
7007     {
7008       smsatProcessAbort(smRoot,
7009                         smOrgIORequest,
7010                         satOrgIOContext
7011                         );
7012 
7013       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
7014 
7015       smsatFreeIntIoResource( smRoot,
7016                               oneDeviceData,
7017                               satIntIo);
7018       return;
7019     }
7020     smsatSetSensePayload( pSense,
7021                           SCSI_SNSKEY_NOT_READY,
7022                           0,
7023                           SCSI_SNSCODE_MEDIUM_NOT_PRESENT,
7024                           satOrgIOContext);
7025 
7026     tdsmIOCompletedCB( smRoot,
7027                        smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
7028                        smIOSuccess,
7029                        SCSI_STAT_CHECK_CONDITION,
7030                        satOrgIOContext->pSmSenseData,
7031                        satOrgIOContext->interruptContext );
7032 
7033     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
7034 
7035     smsatFreeIntIoResource( smRoot,
7036                             oneDeviceData,
7037                             satIntIo);
7038     return;
7039   }
7040 
7041   /* process success case */
7042   if (hostToDevFis->h.command == SAT_READ_BUFFER )
7043   {
7044 
7045     tdsmIOCompletedCB( smRoot,
7046                        smOrgIORequest,
7047                        smIOSuccess,
7048                        SCSI_STAT_GOOD,
7049                        agNULL,
7050                        satOrgIOContext->interruptContext);
7051     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
7052 
7053     smsatFreeIntIoResource( smRoot,
7054                             oneDeviceData,
7055                             satIntIo);
7056     return;
7057   }
7058   else
7059   {
7060     SM_DBG1(("smsatReadBufferCB: error unknown command success 0x%x!!!\n", hostToDevFis->h.command));
7061     smsatSetSensePayload( pSense,
7062                           SCSI_SNSKEY_NO_SENSE,
7063                           0,
7064                           SCSI_SNSCODE_NO_ADDITIONAL_INFO,
7065                           satOrgIOContext);
7066 
7067     tdsmIOCompletedCB( smRoot,
7068                        smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
7069                        smIOSuccess,
7070                        SCSI_STAT_CHECK_CONDITION,
7071                        satOrgIOContext->pSmSenseData,
7072                        satOrgIOContext->interruptContext );
7073 
7074     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
7075 
7076     smsatFreeIntIoResource( smRoot,
7077                             oneDeviceData,
7078                             satIntIo);
7079 
7080     return;
7081   }
7082 
7083   return;
7084 }
7085 
7086 osGLOBAL void
7087 smsatWriteBufferCB(
7088                    agsaRoot_t        *agRoot,
7089                    agsaIORequest_t   *agIORequest,
7090                    bit32             agIOStatus,
7091                    agsaFisHeader_t   *agFirstDword,
7092                    bit32             agIOInfoLen,
7093                    agsaFrameHandle_t agFrameHandle,
7094                    void              *ioContext
7095                   )
7096 {
7097   smRoot_t                *smRoot = agNULL;
7098   smIntRoot_t             *smIntRoot = agNULL;
7099   smIntContext_t          *smAllShared = agNULL;
7100   smIORequestBody_t       *smIORequestBody;
7101   smIORequestBody_t       *smOrgIORequestBody;
7102   smSatIOContext_t          *satIOContext;
7103   smSatIOContext_t          *satOrgIOContext;
7104   smSatInternalIo_t         *satIntIo;
7105   smDeviceData_t          *oneDeviceData;
7106   smScsiRspSense_t          *pSense;
7107   smIORequest_t             *smOrgIORequest;
7108   agsaFisRegHostToDevice_t  *hostToDevFis = agNULL;
7109 
7110 
7111   SM_DBG2(("smsatWriteBufferCB: start\n"));
7112   SM_DBG4(("smsatWriteBufferCB:agIORequest=%p agIOStatus=0x%x agIOInfoLen %d\n", agIORequest, agIOStatus, agIOInfoLen));
7113   /* internally generate tiIOContext */
7114   smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
7115   satIOContext           = (smSatIOContext_t *) ioContext;
7116   satIntIo               = satIOContext->satIntIoContext;
7117   oneDeviceData          = satIOContext->pSatDevData;
7118   hostToDevFis           = satIOContext->pFis;
7119   smRoot                 = oneDeviceData->smRoot;
7120   smIntRoot              = (smIntRoot_t *)smRoot->smData;
7121   smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
7122   if (satIntIo == agNULL)
7123   {
7124     SM_DBG4(("smsatWriteBufferCB: External smSatInternalIo_t satIntIoContext\n"));
7125     satOrgIOContext = satIOContext;
7126     smOrgIORequest  = smIORequestBody->smIORequest;
7127     pSense          = satOrgIOContext->pSense;
7128     /* SCSI command response payload to OS layer */
7129 //    pMediaSerialNumber        = (bit8 *) s,OrgScsiRequest->sglVirtualAddr;
7130   }
7131   else
7132   {
7133     SM_DBG4(("smsatWriteBufferCB: Internal smSatInternalIo_t satIntIoContext\n"));
7134     satOrgIOContext        = satIOContext->satOrgIOContext;
7135     if (satOrgIOContext == agNULL)
7136     {
7137       SM_DBG4(("smsatWriteBufferCB: satOrgIOContext is NULL, wrong\n"));
7138       return;
7139     }
7140     else
7141     {
7142       SM_DBG4(("smsatWriteBufferCB: satOrgIOContext is NOT NULL\n"));
7143     }
7144     smOrgIORequestBody     = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
7145     smOrgIORequest         = (smIORequest_t *)smOrgIORequestBody->smIORequest;
7146     pSense        = satOrgIOContext->pSense;
7147   }
7148   smIORequestBody->ioCompleted = agTRUE;
7149   smIORequestBody->ioStarted = agFALSE;
7150   if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
7151   {
7152     SM_DBG1(("smsatWriteBufferCB: wrong. agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
7153     tdsmIOCompletedCB(
7154                        smRoot,
7155                        smOrgIORequest,
7156                        smIOFailed,
7157                        smDetailOtherError,
7158                        agNULL,
7159                        satOrgIOContext->interruptContext
7160                       );
7161     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
7162 
7163     smsatFreeIntIoResource( smRoot,
7164                             oneDeviceData,
7165                             satIntIo);
7166     return;
7167   }
7168   if( agIOStatus != OSSA_IO_SUCCESS)
7169   {
7170     /* Process abort case */
7171     if (agIOStatus == OSSA_IO_ABORTED)
7172     {
7173       smsatProcessAbort(smRoot,
7174                         smOrgIORequest,
7175                         satOrgIOContext
7176                         );
7177       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
7178 
7179       smsatFreeIntIoResource( smRoot,
7180                               oneDeviceData,
7181                               satIntIo);
7182       return;
7183     }
7184     smsatSetSensePayload( pSense,
7185                           SCSI_SNSKEY_NOT_READY,
7186                           0,
7187                           SCSI_SNSCODE_MEDIUM_NOT_PRESENT,
7188                           satOrgIOContext);
7189     tdsmIOCompletedCB( smRoot,
7190                        smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
7191                        smIOSuccess,
7192                        SCSI_STAT_CHECK_CONDITION,
7193                        satOrgIOContext->pSmSenseData,
7194                        satOrgIOContext->interruptContext );
7195     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
7196 
7197     smsatFreeIntIoResource( smRoot,
7198                             oneDeviceData,
7199                             satIntIo);
7200     return;
7201   }
7202   /* process success case */
7203   if (hostToDevFis->h.command == SAT_WRITE_BUFFER )
7204   {
7205     tdsmIOCompletedCB( smRoot,
7206                        smOrgIORequest,
7207                        smIOSuccess,
7208                        SCSI_STAT_GOOD,
7209                        agNULL,
7210                        satOrgIOContext->interruptContext);
7211     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
7212 
7213     smsatFreeIntIoResource( smRoot,
7214                             oneDeviceData,
7215                             satIntIo);
7216     return;
7217   }
7218   else
7219   {
7220     SM_DBG1(("smsatWriteBufferCB: error unknown command success 0x%x!!!\n", hostToDevFis->h.command));
7221     smsatSetSensePayload( pSense,
7222                           SCSI_SNSKEY_NO_SENSE,
7223                           0,
7224                           SCSI_SNSCODE_NO_ADDITIONAL_INFO,
7225                           satOrgIOContext);
7226     tdsmIOCompletedCB( smRoot,
7227                        smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
7228                        smIOSuccess,
7229                        SCSI_STAT_CHECK_CONDITION,
7230                        satOrgIOContext->pSmSenseData,
7231                        satOrgIOContext->interruptContext );
7232     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
7233     smsatFreeIntIoResource( smRoot,
7234                             oneDeviceData,
7235                             satIntIo);
7236     return;
7237   }
7238   return;
7239 }
7240 
7241 osGLOBAL void
7242 smsatReassignBlocksCB(
7243                       agsaRoot_t        *agRoot,
7244                       agsaIORequest_t   *agIORequest,
7245                       bit32             agIOStatus,
7246                       agsaFisHeader_t   *agFirstDword,
7247                       bit32             agIOInfoLen,
7248                       agsaFrameHandle_t agFrameHandle,
7249                       void              *ioContext
7250                      )
7251 {
7252   smRoot_t                 *smRoot = agNULL;
7253   smIntRoot_t              *smIntRoot = agNULL;
7254   smIntContext_t           *smAllShared = agNULL;
7255   smIORequestBody_t        *smIORequestBody;
7256   smIORequestBody_t        *smOrgIORequestBody;
7257   smSatIOContext_t         *satIOContext;
7258   smSatIOContext_t         *satOrgIOContext;
7259   smSatIOContext_t         *satNewIOContext;
7260   smSatInternalIo_t        *satIntIo;
7261   smSatInternalIo_t        *satNewIntIo = agNULL;
7262   smDeviceData_t           *oneDeviceData;
7263   smScsiRspSense_t         *pSense;
7264   smIniScsiCmnd_t          *scsiCmnd;
7265   smIORequest_t            *smOrgIORequest;
7266   agsaFisRegHostToDevice_t *hostToDevFis = agNULL;
7267   bit32                     ataStatus = 0;
7268   bit32                     status;
7269   smScsiInitiatorRequest_t *smScsiRequest; /* smScsiXchg */
7270   agsaFisRegD2HHeader_t    *statDevToHostFisHeader = agNULL;
7271 
7272   SM_DBG2(("smsatReassignBlocksCB: start\n"));
7273   SM_DBG5(("smsatReassignBlocksCB: agIORequest=%p agIOStatus=0x%x agIOInfoLen %d\n", agIORequest, agIOStatus, agIOInfoLen));
7274 
7275   /* internally generate tiIOContext */
7276   smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
7277   satIOContext           = (smSatIOContext_t *) ioContext;
7278   satIntIo               = satIOContext->satIntIoContext;
7279   oneDeviceData          = satIOContext->pSatDevData;
7280   hostToDevFis           = satIOContext->pFis;
7281   smRoot                 = oneDeviceData->smRoot;
7282   smIntRoot              = (smIntRoot_t *)smRoot->smData;
7283   smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
7284   if (satIntIo == agNULL)
7285   {
7286     SM_DBG4(("smsatReassignBlocksCB: External smSatInternalIo_t satIntIoContext\n"));
7287     satOrgIOContext = satIOContext;
7288     smOrgIORequest  = smIORequestBody->smIORequest;
7289     smScsiRequest   = satOrgIOContext->smScsiXchg;
7290     pSense          = satOrgIOContext->pSense;
7291     scsiCmnd        = satOrgIOContext->pScsiCmnd;
7292   }
7293   else
7294   {
7295     SM_DBG4(("smsatReassignBlocksCB: Internal smSatInternalIo_t satIntIoContext\n"));
7296     satOrgIOContext        = satIOContext->satOrgIOContext;
7297     if (satOrgIOContext == agNULL)
7298     {
7299       SM_DBG4(("smsatReassignBlocksCB: satOrgIOContext is NULL, Wrong\n"));
7300       return;
7301     }
7302     else
7303     {
7304       SM_DBG4(("smsatReassignBlocksCB: satOrgIOContext is NOT NULL, Wrong\n"));
7305     }
7306     smOrgIORequestBody     = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
7307     smOrgIORequest         = (smIORequest_t *)smOrgIORequestBody->smIORequest;
7308     smScsiRequest = satOrgIOContext->smScsiXchg;
7309     pSense        = satOrgIOContext->pSense;
7310     scsiCmnd      = satOrgIOContext->pScsiCmnd;
7311   }
7312 
7313   smIORequestBody->ioCompleted = agTRUE;
7314   smIORequestBody->ioStarted = agFALSE;
7315 
7316   if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
7317   {
7318     SM_DBG1(("smsatReassignBlocksCB: wrong. agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
7319     tdsmIOCompletedCB(
7320                        smRoot,
7321                        smOrgIORequest,
7322                        smIOFailed,
7323                        smDetailOtherError,
7324                        agNULL,
7325                        satOrgIOContext->interruptContext
7326                       );
7327     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
7328 
7329     smsatFreeIntIoResource( smRoot,
7330                             oneDeviceData,
7331                             satIntIo);
7332     return;
7333   }
7334   if (agIOStatus != OSSA_IO_SUCCESS)
7335   {
7336     /* only agsaFisRegDeviceToHost_t is expected */
7337     statDevToHostFisHeader = (agsaFisRegD2HHeader_t *)&(agFirstDword->D2H);
7338     ataStatus     = statDevToHostFisHeader->status;   /* ATA Status register */
7339   }
7340   if (agIOStatus != OSSA_IO_SUCCESS)
7341   {
7342   if ( (statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS) ||
7343        ((ataStatus & ERR_ATA_STATUS_MASK) || (ataStatus & DF_ATA_STATUS_MASK))
7344        )
7345   {
7346     /* for debugging */
7347     if( agIOStatus != OSSA_IO_SUCCESS)
7348     {
7349       SM_DBG1(("smsatReassignBlocksCB FAILED, NOT IO_SUCCESS!!!\n"));
7350     }
7351     else if (statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS)
7352     {
7353       SM_DBG1(("smsatReassignBlocksCB FAILED, Wrong FIS type 0x%x!!!\n",statDevToHostFisHeader->fisType));
7354     }
7355     else if ( (ataStatus & ERR_ATA_STATUS_MASK) ||
7356               (ataStatus & DF_ATA_STATUS_MASK)
7357               )
7358     {
7359       SM_DBG1(("smsatReassignBlocksCB FAILED, FAILED, error status!!!\n"));
7360     }
7361 
7362     /* Process abort case */
7363     if (agIOStatus == OSSA_IO_ABORTED)
7364     {
7365       smsatProcessAbort(smRoot,
7366                         smOrgIORequest,
7367                         satOrgIOContext
7368                         );
7369       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
7370 
7371       smsatFreeIntIoResource( smRoot,
7372                               oneDeviceData,
7373                               satIntIo);
7374       return;
7375     }
7376     /* for debugging */
7377     if (hostToDevFis->h.command == SAT_READ_VERIFY_SECTORS ||
7378         hostToDevFis->h.command == SAT_READ_VERIFY_SECTORS_EXT
7379        )
7380     {
7381       SM_DBG1(("smsatReassignBlocksCB SAT_READ_VERIFY_SECTORS(_EXT) failed!!!\n"));
7382       /* Verify failed; send Write with same LBA */
7383       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
7384 
7385       smsatFreeIntIoResource( smRoot,
7386                               oneDeviceData,
7387                               satIntIo);
7388 
7389       satNewIntIo = smsatAllocIntIoResource( smRoot,
7390                                              smOrgIORequest,
7391                                              oneDeviceData,
7392                                              512, /* writing 1 sector */
7393                                              satNewIntIo);
7394       if (satNewIntIo == agNULL)
7395       {
7396         smsatSetSensePayload( pSense,
7397                               SCSI_SNSKEY_HARDWARE_ERROR,
7398                               0,
7399                               SCSI_SNSCODE_WRITE_ERROR_AUTO_REALLOCATION_FAILED,
7400                               satOrgIOContext);
7401         tdsmIOCompletedCB( smRoot,
7402                            smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
7403                            smIOSuccess,
7404                            SCSI_STAT_CHECK_CONDITION,
7405                            satOrgIOContext->pSmSenseData,
7406                            satOrgIOContext->interruptContext );
7407         SM_DBG1(("smsatReassignBlocksCB: momory allocation fails!!!\n"));
7408         return;
7409       } /* end memory allocation */
7410       satNewIOContext = smsatPrepareNewIO(
7411                                           satNewIntIo,
7412                                           smOrgIORequest,
7413                                           oneDeviceData,
7414                                           scsiCmnd,
7415                                           satOrgIOContext
7416                                           );
7417       /* send Write with same LBA */
7418       status = smsatReassignBlocks_2(
7419                                      smRoot,
7420                                      &satNewIntIo->satIntSmIORequest,
7421                                      satNewIOContext->psmDeviceHandle,
7422                                      &satNewIntIo->satIntSmScsiXchg,
7423                                      satNewIOContext,
7424                                      satOrgIOContext->LBA
7425                                     );
7426 
7427       if (status != SM_RC_SUCCESS)
7428       {
7429         /* sending ATA command fails */
7430         smsatFreeIntIoResource( smRoot,
7431                                 oneDeviceData,
7432                                 satNewIntIo);
7433         smsatSetSensePayload( pSense,
7434                               SCSI_SNSKEY_HARDWARE_ERROR,
7435                               0,
7436                               SCSI_SNSCODE_WRITE_ERROR_AUTO_REALLOCATION_FAILED,
7437                               satOrgIOContext);
7438 
7439         tdsmIOCompletedCB( smRoot,
7440                            smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
7441                            smIOSuccess,
7442                            SCSI_STAT_CHECK_CONDITION,
7443                            satOrgIOContext->pSmSenseData,
7444                            satOrgIOContext->interruptContext );
7445         SM_DBG1(("smsatReassignBlocksCB calling fail 1!!!\n"));
7446         return;
7447       } /* end send fails */
7448 
7449       return;
7450     }
7451     else if (hostToDevFis->h.command == SAT_WRITE_DMA ||
7452              hostToDevFis->h.command == SAT_WRITE_SECTORS ||
7453              hostToDevFis->h.command == SAT_WRITE_DMA_EXT ||
7454              hostToDevFis->h.command == SAT_WRITE_SECTORS_EXT ||
7455              hostToDevFis->h.command == SAT_WRITE_FPDMA_QUEUED
7456              )
7457     {
7458       SM_DBG1(("smsatReassignBlocksCB SAT_WRITE failed!!!\n"));
7459       /* fall through */
7460     }
7461     else
7462     {
7463       SM_DBG1(("smsatReassignBlocksCB error default case unexpected command 0x%x!!!\n", hostToDevFis->h.command));
7464     }
7465 
7466 
7467     smsatSetSensePayload( pSense,
7468                           SCSI_SNSKEY_HARDWARE_ERROR,
7469                           0,
7470                           SCSI_SNSCODE_WRITE_ERROR_AUTO_REALLOCATION_FAILED,
7471                           satOrgIOContext);
7472 
7473     tdsmIOCompletedCB( smRoot,
7474                        smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
7475                        smIOSuccess,
7476                        SCSI_STAT_CHECK_CONDITION,
7477                        satOrgIOContext->pSmSenseData,
7478                        satOrgIOContext->interruptContext );
7479 
7480 
7481     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
7482 
7483     smsatFreeIntIoResource( smRoot,
7484                             oneDeviceData,
7485                             satIntIo);
7486     return;
7487   } /* error checking */
7488   }
7489 
7490 
7491   /* prcessing the success case */
7492   if (hostToDevFis->h.command == SAT_READ_VERIFY_SECTORS ||
7493       hostToDevFis->h.command == SAT_READ_VERIFY_SECTORS_EXT ||
7494       hostToDevFis->h.command == SAT_WRITE_DMA ||
7495       hostToDevFis->h.command == SAT_WRITE_SECTORS ||
7496       hostToDevFis->h.command == SAT_WRITE_DMA_EXT ||
7497       hostToDevFis->h.command == SAT_WRITE_SECTORS_EXT ||
7498       hostToDevFis->h.command == SAT_WRITE_FPDMA_QUEUED
7499       )
7500   {
7501     /* next LBA; verify */
7502     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
7503 
7504     smsatFreeIntIoResource( smRoot,
7505                             oneDeviceData,
7506                             satIntIo);
7507 
7508     if (satOrgIOContext->ParmIndex >= satOrgIOContext->ParmLen)
7509     {
7510       SM_DBG5(("smsatReassignBlocksCB: GOOD status\n"));
7511       /* return stat_good */
7512       tdsmIOCompletedCB( smRoot,
7513                          smOrgIORequest,
7514                          smIOSuccess,
7515                          SCSI_STAT_GOOD,
7516                          agNULL,
7517                          satOrgIOContext->interruptContext );
7518       return;
7519     }
7520     else
7521     {
7522       SM_DBG5(("smsatReassignBlocksCB: processing next LBA\n"));
7523       satNewIntIo = smsatAllocIntIoResource( smRoot,
7524                                              smOrgIORequest,
7525                                              oneDeviceData,
7526                                              0,
7527                                              satNewIntIo);
7528       if (satNewIntIo == agNULL)
7529       {
7530         smsatSetSensePayload( pSense,
7531                               SCSI_SNSKEY_HARDWARE_ERROR,
7532                               0,
7533                               SCSI_SNSCODE_WRITE_ERROR_AUTO_REALLOCATION_FAILED,
7534                               satOrgIOContext);
7535 
7536         tdsmIOCompletedCB( smRoot,
7537                            smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
7538                            smIOSuccess,
7539                            SCSI_STAT_CHECK_CONDITION,
7540                            satOrgIOContext->pSmSenseData,
7541                            satOrgIOContext->interruptContext );
7542         SM_DBG1(("smsatReassignBlocksCB: momory allocation fails!!!\n"));
7543         return;
7544       } /* end memory allocation */
7545 
7546       satNewIOContext = smsatPrepareNewIO(
7547                                           satNewIntIo,
7548                                           smOrgIORequest,
7549                                           oneDeviceData,
7550                                           scsiCmnd,
7551                                           satOrgIOContext
7552                                           );
7553 
7554       /* send Verify with the next LBA */
7555       status = smsatReassignBlocks_1(
7556                                      smRoot,
7557                                      &satNewIntIo->satIntSmIORequest,
7558                                      satNewIOContext->psmDeviceHandle,
7559                                      smScsiRequest, /* orginal from OS layer */
7560                                      satNewIOContext,
7561                                      satOrgIOContext
7562                                      );
7563 
7564       if (status != SM_RC_SUCCESS)
7565       {
7566         /* sending ATA command fails */
7567         smsatFreeIntIoResource( smRoot,
7568                                 oneDeviceData,
7569                                 satNewIntIo);
7570         smsatSetSensePayload( pSense,
7571                               SCSI_SNSKEY_HARDWARE_ERROR,
7572                               0,
7573                               SCSI_SNSCODE_WRITE_ERROR_AUTO_REALLOCATION_FAILED,
7574                               satOrgIOContext);
7575 
7576         tdsmIOCompletedCB( smRoot,
7577                            smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
7578                            smIOSuccess,
7579                            SCSI_STAT_CHECK_CONDITION,
7580                            satOrgIOContext->pSmSenseData,
7581                            satOrgIOContext->interruptContext );
7582         SM_DBG1(("smsatReassignBlocksCB calling satModeSelect6_1 fails!!!\n"));
7583         return;
7584       } /* end send fails */
7585     } /* else */
7586     return;
7587 
7588   }
7589   else if (hostToDevFis->h.command == SAT_WRITE_DMA ||
7590            hostToDevFis->h.command == SAT_WRITE_SECTORS ||
7591            hostToDevFis->h.command == SAT_WRITE_DMA_EXT ||
7592            hostToDevFis->h.command == SAT_WRITE_SECTORS_EXT ||
7593            hostToDevFis->h.command == SAT_WRITE_FPDMA_QUEUED
7594            )
7595   {
7596     /* next LBA; verify */
7597   }
7598   else
7599   {
7600       SM_DBG1(("smsatReassignBlocksCB error unknown command success 0x%x !!!\n", hostToDevFis->h.command));
7601 
7602       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
7603 
7604       smsatFreeIntIoResource( smRoot,
7605                               oneDeviceData,
7606                               satIntIo);
7607       smsatSetSensePayload( pSense,
7608                             SCSI_SNSKEY_HARDWARE_ERROR,
7609                             0,
7610                             SCSI_SNSCODE_WRITE_ERROR_AUTO_REALLOCATION_FAILED,
7611                             satOrgIOContext);
7612 
7613       tdsmIOCompletedCB( smRoot,
7614                          smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
7615                          smIOSuccess,
7616                          SCSI_STAT_CHECK_CONDITION,
7617                          satOrgIOContext->pSmSenseData,
7618                          satOrgIOContext->interruptContext );
7619       return;
7620   }
7621   return;
7622 }
7623 
7624 
7625 osGLOBAL FORCEINLINE void
7626 smsatDecrementPendingIO(
7627                         smRoot_t                *smRoot,
7628                         smIntContext_t          *smAllShared,
7629                         smSatIOContext_t        *satIOContext
7630                         )
7631 {
7632 #ifdef CCFLAG_OPTIMIZE_SAT_LOCK
7633   bit32 volatile satPendingNCQIO = 0;
7634   bit32 volatile satPendingNONNCQIO = 0;
7635   bit32 volatile satPendingIO = 0;
7636 #endif /* CCFLAG_OPTIMIZE_SAT_LOCK */
7637   smDeviceData_t       *oneDeviceData   = satIOContext->pSatDevData;
7638   smSatInternalIo_t    *satIntIo        = satIOContext->satIntIoContext;
7639   smSatIOContext_t     *satOrgIOContext = satIOContext->satOrgIOContext;
7640 #ifdef  TD_DEBUG_ENABLE
7641   smIORequestBody_t    *smIORequestBody = agNULL;
7642   smIORequestBody = (smIORequestBody_t *)satIOContext->smRequestBody;
7643 #endif
7644 
7645   SM_DBG3(("smsatDecrementPendingIO: start\n"));
7646 
7647 #ifdef CCFLAG_OPTIMIZE_SAT_LOCK
7648   if ( (satIOContext->reqType == AGSA_SATA_PROTOCOL_FPDMA_WRITE) ||
7649        (satIOContext->reqType == AGSA_SATA_PROTOCOL_FPDMA_READ) )
7650   {
7651     tdsmInterlockedDecrement(smRoot,&oneDeviceData->satPendingNCQIO);
7652   }
7653   else
7654   {
7655     tdsmInterlockedDecrement(smRoot,&oneDeviceData->satPendingNONNCQIO);
7656   }
7657   tdsmInterlockedDecrement(smRoot,&oneDeviceData->satPendingIO);
7658   /* temp */
7659   tdsmInterlockedExchange(smRoot, &satPendingNCQIO, oneDeviceData->satPendingNCQIO);
7660   tdsmInterlockedExchange(smRoot, &satPendingNONNCQIO, oneDeviceData->satPendingNONNCQIO);
7661   tdsmInterlockedExchange(smRoot, &satPendingIO, oneDeviceData->satPendingIO);
7662   if (satPendingNCQIO == -1)
7663   {
7664     SM_DBG1(("smsatDecrementPendingIO: satPendingNCQIO adjustment!!!\n"));
7665     oneDeviceData->satPendingNCQIO = 0;
7666   }
7667   if (satPendingNONNCQIO == -1)
7668   {
7669     SM_DBG1(("smsatDecrementPendingIO: satPendingNONNCQIO adjustment!!!\n"));
7670     oneDeviceData->satPendingNONNCQIO = 0;
7671   }
7672   if (satPendingIO == -1)
7673   {
7674     SM_DBG1(("smsatDecrementPendingIO: satPendingIO adjustment!!!\n"));
7675     oneDeviceData->satPendingIO = 0;
7676   }
7677 
7678 #else
7679 
7680   if ( (satIOContext->reqType == AGSA_SATA_PROTOCOL_FPDMA_WRITE) ||
7681        (satIOContext->reqType == AGSA_SATA_PROTOCOL_FPDMA_READ) )
7682   {
7683     tdsmSingleThreadedEnter(smRoot, SM_EXTERNAL_IO_LOCK);
7684     oneDeviceData->satPendingNCQIO--;
7685     oneDeviceData->satPendingIO--;
7686     SMLIST_DEQUEUE_THIS (&satIOContext->satIoContextLink);
7687     /* temp */
7688     if (oneDeviceData->satPendingNCQIO == -1)
7689     {
7690       SM_DBG1(("smsatDecrementPendingIO: satPendingNCQIO adjustment!!!\n"));
7691       oneDeviceData->satPendingNCQIO = 0;
7692     }
7693     if (oneDeviceData->satPendingIO == -1)
7694     {
7695       SM_DBG1(("smsatDecrementPendingIO: satPendingIO adjustment!!!\n"));
7696       oneDeviceData->satPendingIO = 0;
7697     }
7698     tdsmSingleThreadedLeave(smRoot, SM_EXTERNAL_IO_LOCK);
7699   }
7700   else
7701   {
7702     tdsmSingleThreadedEnter(smRoot, SM_EXTERNAL_IO_LOCK);
7703     oneDeviceData->satPendingNONNCQIO--;
7704     oneDeviceData->satPendingIO--;
7705     SMLIST_DEQUEUE_THIS (&satIOContext->satIoContextLink);
7706     /* temp */
7707     if (oneDeviceData->satPendingNONNCQIO == -1)
7708     {
7709       SM_DBG1(("smsatDecrementPendingIO: satPendingNONNCQIO adjustment!!!\n"));
7710       oneDeviceData->satPendingNONNCQIO = 0;
7711     }
7712     if (oneDeviceData->satPendingIO == -1)
7713     {
7714       SM_DBG1(("smsatDecrementPendingIO: satPendingIO adjustment!!!\n"));
7715       oneDeviceData->satPendingIO = 0;
7716     }
7717     tdsmSingleThreadedLeave(smRoot, SM_EXTERNAL_IO_LOCK);
7718   }
7719 
7720 #endif /* CCFLAG_OPTIMIZE_SAT_LOCK */
7721 
7722   if (satIntIo == agNULL)
7723   {
7724     SM_DBG3(("smsatDecrementPendingIO: external command!!!\n"));
7725     /*smEnqueueIO(smRoot, satIOContext);*/
7726   }
7727   else
7728   {
7729     SM_DBG3(("smsatDecrementPendingIO: internal command!!!\n"));
7730     if (satOrgIOContext == agNULL)
7731     {
7732       /* No smEnqueueIO since only alloc used */
7733       SM_DBG3(("smsatDecrementPendingIO: internal only command!!!, ID %d!!!\n", smIORequestBody->id));
7734       return;
7735     }
7736     else
7737     {
7738       /* smDequeueIO used */
7739       /*smEnqueueIO(smRoot, satOrgIOContext);*/
7740     }
7741   }
7742 
7743   return;
7744 }
7745 
7746 
7747 osGLOBAL void
7748 smsatProcessAbnormalCompletion(
7749                                agsaRoot_t        *agRoot,
7750                                agsaIORequest_t   *agIORequest,
7751                                bit32             agIOStatus,
7752                                agsaFisHeader_t   *agFirstDword,
7753                                bit32             agIOInfoLen,
7754                                void              *agParam,
7755                                smSatIOContext_t    *satIOContext
7756                               )
7757 {
7758 
7759   smRoot_t             *smRoot = agNULL;
7760 //  tdsaRootOsData_t     *osData = (tdsaRootOsData_t *)agRoot->osData;
7761 //  tiRoot_t             *tiRoot = (tiRoot_t *)osData->tiRoot;
7762   bit32                interruptContext;
7763   smIORequestBody_t    *smIORequestBody;
7764 //  satDeviceData_t      *pSatDevData;
7765   smDeviceHandle_t     *smDeviceHandle;
7766   smDeviceData_t       *oneDeviceData = agNULL;
7767   agsaDevHandle_t      *agDevHandle = agNULL;
7768 
7769   smIORequestBody = (smIORequestBody_t *)agIORequest->osData;
7770   oneDeviceData   = satIOContext->pSatDevData;
7771   if (oneDeviceData == agNULL)
7772   {
7773     SM_DBG1(("smsatProcessAbnormalCompletion: oneDeviceData is NULL\n"));
7774     return;
7775   }
7776   smDeviceHandle  = satIOContext->psmDeviceHandle;
7777   smRoot          = oneDeviceData->smRoot;
7778   interruptContext = satIOContext->interruptContext;
7779 
7780   SM_DBG5(("smsatProcessAbnormalCompletion: start\n"));
7781 
7782   /* Get into the detail */
7783   switch(agIOStatus)
7784   {
7785   case OSSA_IO_SUCCESS:
7786     SM_DBG1(("smsatProcessAbnormalCompletion: agIOStatus = OSSA_IO_SUCCESS agIOInfoLen 0x%x calling smsatIOCompleted!!!\n", agIOInfoLen));
7787     /*
7788      * At this point agIOInfoLen should be non-zero and there is valid FIS
7789      * to read. Pass this info to the SAT layer in order to do the ATA status
7790      * to SCSI status translation.
7791      */
7792       smsatIOCompleted( smRoot,
7793                         smIORequestBody->smIORequest,
7794                         agFirstDword,
7795                         agIOInfoLen,
7796                         agParam,
7797                         satIOContext,
7798                         interruptContext);
7799     break;
7800 
7801 
7802   case OSSA_IO_ABORTED:
7803     SM_DBG1(("smsatProcessAbnormalCompletion: agIOStatus = OSSA_IO_ABORTED!!!\n"));
7804 
7805     tdsmIOCompletedCB( smRoot,
7806                        smIORequestBody->smIORequest,
7807                        smIOFailed,
7808                        smDetailAborted,
7809                        agNULL,
7810                        interruptContext);
7811 
7812 #ifdef REMOVED
7813     if ( oneDeviceData->satTmTaskTag != agNULL )
7814     {
7815       SM_DBG1(("smsatProcessAbnormalCompletion: TM callback!!!\n"));
7816       if (smDeviceHandle == agNULL)
7817       {
7818         SM_DBG1(("smsatProcessAbnormalCompletion: wrong, smDeviceHandle is NULL!!!\n"));
7819       }
7820       /* TM completed */
7821       tdsmEventCB( smRoot,
7822                    smDeviceHandle,
7823                    smIntrEventTypeTaskManagement,
7824                    smTMOK,
7825                    oneDeviceData->satTmTaskTag);
7826       /*
7827        * Reset flag
7828        */
7829       oneDeviceData->satTmTaskTag = agNULL;
7830     }
7831 #endif
7832 
7833     /*
7834      * Check if we are in recovery mode and need to update the recovery flag
7835      */
7836     if ((oneDeviceData->satDriveState == SAT_DEV_STATE_IN_RECOVERY ) &&
7837         (oneDeviceData->satPendingIO == 0 ))
7838     {
7839       oneDeviceData->satDriveState = SAT_DEV_STATE_NORMAL;
7840       SM_DBG1(("smsatProcessAbnormalCompletion: STATE NORMAL!!!\n"));
7841     }
7842 
7843     SM_DBG1(("smsatProcessAbnormalCompletion: did %d satDriveState %d!!!\n", oneDeviceData->id, oneDeviceData->satDriveState));
7844     SM_DBG1(("smsatProcessAbnormalCompletion: satPendingIO %d satNCQMaxIO %d!!!\n", oneDeviceData->satPendingIO, oneDeviceData->satNCQMaxIO ));
7845     SM_DBG1(("smsatProcessAbnormalCompletion: satPendingNCQIO %d satPendingNONNCQIO %d!!!\n", oneDeviceData->satPendingNCQIO, oneDeviceData->satPendingNONNCQIO));
7846 
7847     break;
7848 #ifdef REMOVED
7849   case OSSA_IO_OVERFLOW:
7850     SM_DBG1(("smsatProcessAbnormalCompletion: agIOStatus = OSSA_IO_OVERFLOW!!!\n"));
7851     tdsmIOCompletedCB( smRoot,
7852                        smIORequestBody->smIORequest,
7853                        smIOOverRun,
7854                        agIOInfoLen,
7855                        agNULL,
7856                        interruptContext);
7857     break;
7858 #endif
7859   case OSSA_IO_UNDERFLOW:
7860     SM_DBG1(("smsatProcessAbnormalCompletion: agIOStatus = OSSA_IO_UNDERFLOW!!!\n"));
7861     tdsmIOCompletedCB( smRoot,
7862                        smIORequestBody->smIORequest,
7863                        smIOUnderRun,
7864                        agIOInfoLen,
7865                        agNULL,
7866                        interruptContext);
7867     break;
7868 
7869 
7870   case OSSA_IO_FAILED:
7871     SM_DBG1(("smsatProcessAbnormalCompletion: agIOStatus = OSSA_IO_FAILED!!!\n"));
7872     tdsmIOCompletedCB( smRoot,
7873                        smIORequestBody->smIORequest,
7874                        smIOFailed,
7875                        smDetailOtherError,
7876                        agNULL,
7877                        interruptContext);
7878     break;
7879 
7880   case OSSA_IO_ABORT_RESET:
7881     SM_DBG1(("smsatProcessAbnormalCompletion: agIOStatus = OSSA_IO_ABORT_RESET!!!\n"));
7882     tdsmIOCompletedCB( smRoot,
7883                        smIORequestBody->smIORequest,
7884                        smIOFailed,
7885                        smDetailAbortReset,
7886                        agNULL,
7887                        interruptContext);
7888     /*
7889      * Check if we are in recovery mode and need to update the recovery flag
7890      */
7891     if ((oneDeviceData->satDriveState == SAT_DEV_STATE_IN_RECOVERY ) &&
7892         (oneDeviceData->satPendingIO == 0 ))
7893     {
7894       oneDeviceData->satDriveState = SAT_DEV_STATE_NORMAL;
7895       SM_DBG1(("smsatProcessAbnormalCompletion: STATE NORMAL!!!\n"));
7896     }
7897 
7898     SM_DBG1(("smsatProcessAbnormalCompletion: satDriveState %d!!!\n", oneDeviceData->satDriveState));
7899     SM_DBG1(("smsatProcessAbnormalCompletion: satPendingIO %d satNCQMaxIO %d!!!\n", oneDeviceData->satPendingIO, oneDeviceData->satNCQMaxIO ));
7900     SM_DBG1(("smsatProcessAbnormalCompletion: satPendingNCQIO %d satPendingNONNCQIO %d!!!\n", oneDeviceData->satPendingNCQIO, oneDeviceData->satPendingNONNCQIO));
7901 
7902     break;
7903 
7904 #ifdef REMOVED
7905   case OSSA_IO_NOT_VALID:
7906     SM_DBG1(("smsatProcessAbnormalCompletion: agIOStatus = OSSA_IO_NOT_VALID!!!\n"));
7907     tdsmIOCompletedCB( smRoot,
7908                        smIORequestBody->smIORequest,
7909                        smIOFailed,
7910                        smDetailNotValid,
7911                        agNULL,
7912                        interruptContext);
7913     break;
7914 #endif
7915 
7916   case OSSA_IO_NO_DEVICE:
7917     SM_DBG1(("smsatProcessAbnormalCompletion: agIOStatus = OSSA_IO_NO_DEVICE!!!\n"));
7918     tdsmIOCompletedCB( smRoot,
7919                        smIORequestBody->smIORequest,
7920                        smIOFailed,
7921                        smDetailNoLogin,
7922                        agNULL,
7923                        interruptContext);
7924     break;
7925 
7926 #ifdef REMOVED /* removed from spec */
7927   case OSSA_IO_ILLEGAL_PARAMETER:
7928     SM_DBG1(("smsatProcessAbnormalCompletion: agIOStatus = OSSA_IO_ILLEGAL_PARAMETER!!!\n"));
7929     tdsmIOCompletedCB( smRoot,
7930                        smIORequestBody->smIORequest,
7931                        smIOFailed,
7932                        smDetailOtherError,
7933                        agNULL,
7934                        interruptContext);
7935     break;
7936 
7937   case OSSA_IO_LINK_FAILURE:
7938     SM_DBG1(("smsatProcessAbnormalCompletion: agIOStatus = OSSA_IO_LINK_FAILURE!!!\n"));
7939     tdsmIOCompletedCB( smRoot,
7940                        smIORequestBody->smIORequest,
7941                        smIOFailed,
7942                        smDetailOtherError,
7943                        agNULL,
7944                        interruptContext);
7945     break;
7946 
7947   case OSSA_IO_PROG_ERROR:
7948     SM_DBG1(("smsatProcessAbnormalCompletion: agIOStatus = OSSA_IO_PROG_ERROR!!!\n"));
7949     tdsmIOCompletedCB( smRoot,
7950                        smIORequestBody->smIORequest,
7951                        smIOFailed,
7952                        smDetailOtherError,
7953                        agNULL,
7954                        interruptContext);
7955     break;
7956 #endif
7957   case OSSA_IO_OPEN_CNX_ERROR_PROTOCOL_NOT_SUPPORTED: /* fall through */
7958   case OSSA_IO_OPEN_CNX_ERROR_ZONE_VIOLATION: /* fall through */
7959   case OSSA_IO_OPEN_CNX_ERROR_BREAK: /* fall through */
7960   case OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS: /* fall through */
7961   case OSSA_IO_OPEN_CNX_ERROR_BAD_DESTINATION: /* fall through */
7962   case OSSA_IO_OPEN_CNX_ERROR_CONNECTION_RATE_NOT_SUPPORTED: /* fall through */
7963   case OSSA_IO_OPEN_CNX_ERROR_WRONG_DESTINATION: /* fall through */
7964   case OSSA_IO_OPEN_CNX_ERROR_OPEN_PREEMPTED: /* fall through */
7965 #ifdef REMOVED /* removed from spec */
7966   case OSSA_IO_OPEN_CNX_ERROR_UNKNOWN_ERROR: /* fall through */
7967 #endif
7968     SM_DBG1(("smsatProcessAbnormalCompletion: agIOStatus = OSSA_IO_OPEN_CNX_ERROR_* 0x%x!!!\n", agIOStatus));
7969     if (smDeviceHandle == agNULL)
7970     {
7971       SM_DBG1(("smsatProcessAbnormalCompletion: wrong, smDeviceHandle is NULL!!!\n"));
7972       tdsmIOCompletedCB( smRoot,
7973                          smIORequestBody->smIORequest,
7974                          smIOFailed,
7975                          smDetailOtherError,
7976                          agNULL,
7977                          interruptContext);
7978       return;
7979     }
7980     if (oneDeviceData == agNULL)
7981     {
7982       SM_DBG1(("smsatProcessAbnormalCompletion: wrong, oneDeviceData is NULL!!!\n"));
7983     }
7984     else
7985     {
7986       SM_DBG1(("smsatProcessAbnormalCompletion: did %d!!!\n", oneDeviceData->id));
7987     }
7988 
7989     tdsmIOCompletedCB( smRoot,
7990                        smIORequestBody->smIORequest,
7991                        smIOFailed,
7992                        smDetailOtherError,
7993                        agNULL,
7994                        interruptContext);
7995     break;
7996   case OSSA_IO_OPEN_CNX_ERROR_STP_RESOURCES_BUSY:
7997     SM_DBG1(("smsatProcessAbnormalCompletion: agIOStatus = OSSA_IO_OPEN_CNX_ERROR_STP_RESOURCES_BUSY!!!\n"));
7998     tdsmIOCompletedCB( smRoot,
7999                        smIORequestBody->smIORequest,
8000                        smIOFailed,
8001                        smDetailBusy,
8002                        agNULL,
8003                        interruptContext);
8004     break;
8005 #ifdef REMOVED
8006   case OSSA_IO_XFER_ERROR_BREAK: /* fall throuth */
8007 #endif
8008 
8009   case OSSA_IO_XFER_ERROR_PHY_NOT_READY: /* fall throuth */
8010   case OSSA_IO_XFER_ERROR_NAK_RECEIVED: /* fall throuth */
8011 
8012 #ifdef REMOVED
8013   case OSSA_IO_XFER_ERROR_ACK_NAK_TIMEOUT: /* fall throuth */
8014   case OSSA_IO_XFER_ERROR_PEER_ABORTED: /* fall throuth */
8015 #endif
8016   case OSSA_IO_XFER_ERROR_DMA: /* fall throuth */
8017 #ifdef REMOVED
8018   case OSSA_IO_XFER_ERROR_RX_FRAME: /* fall throuth */
8019   case OSSA_IO_XFER_ERROR_CREDIT_TIMEOUT: /* fall throuth */
8020   case OSSA_IO_XFER_ERROR_SATA: /* fall throuth */
8021 #endif
8022   case OSSA_IO_XFER_ERROR_SATA_LINK_TIMEOUT: /* fall throuth */
8023   case OSSA_IO_XFER_ERROR_ABORTED_DUE_TO_SRST: /* fall throuth */
8024   case OSSA_IO_XFER_ERROR_REJECTED_NCQ_MODE: /* fall throuth */
8025 #ifdef REMOVED
8026   case OSSA_IO_XFER_ERR_EOB_DATA_OVERRUN:
8027   case OSSA_IO_XFER_ERROR_ABORTED_NCQ_MODE: /* fall throuth */
8028   case OSSA_IO_XFER_ERROR_DISRUPTED_PHY_DOWN: /* fall throuth */
8029   case OSSA_IO_XFER_ERROR_OFFSET_MISMATCH: /* fall throuth */
8030   case OSSA_IO_XFER_ERROR_XFER_ZERO_DATA_LEN: /* fall throuth */
8031 #endif
8032     SM_DBG1(("smsatProcessAbnormalCompletion: agIOStatus = OSSA_IO_XFER_ERROR_* 0x%x!!!\n", agIOStatus));
8033     tdsmIOCompletedCB( smRoot,
8034                        smIORequestBody->smIORequest,
8035                        smIOFailed,
8036                        smDetailOtherError,
8037                        agNULL,
8038                        interruptContext);
8039     break;
8040 #ifdef REMOVED
8041   case OSSA_IO_XFER_ERROR_CMD_ISSUE_ACK_NAK_TIMEOUT: /* fall throuth */
8042   case OSSA_IO_XFER_ERROR_CMD_ISSUE_BREAK_BEFORE_ACK_NAK: /* fall throuth */
8043   case OSSA_IO_XFER_ERROR_CMD_ISSUE_PHY_DOWN_BEFORE_ACK_NAK: /* fall throuth */
8044     SM_DBG1(("smsatProcessAbnormalCompletion: agIOStatus = OSSA_IO_XFER_ERROR_CMD_ISSUE_* 0x%x!!!\n", agIOStatus));
8045     tdsmIOCompletedCB( smRoot,
8046                        smIORequestBody->smIORequest,
8047                        smIOFailed,
8048                        smDetailOtherError,
8049                        agNULL,
8050                        interruptContext);
8051     break;
8052   case OSSA_IO_XFER_PIO_SETUP_ERROR:
8053     SM_DBG1(("smsatProcessAbnormalCompletion: agIOStatus = OSSA_IO_XFER_PIO_SETUP_ERROR!!!\n"));
8054     tdsmIOCompletedCB( smRoot,
8055                        smIORequestBody->smIORequest,
8056                        smIOFailed,
8057                        smDetailOtherError,
8058                        agNULL,
8059                        interruptContext);
8060     break;
8061 #endif
8062   case OSSA_IO_DS_IN_ERROR:
8063     SM_DBG1(("smsatProcessAbnormalCompletion: agIOStatus = OSSA_IO_DS_IN_ERROR!!!\n"));
8064     if (smDeviceHandle == agNULL)
8065     {
8066       SM_DBG1(("smsatProcessAbnormalCompletion: wrong, smDeviceHandle is NULL!!!\n"));
8067       tdsmIOCompletedCB( smRoot,
8068                          smIORequestBody->smIORequest,
8069                          smIOFailed,
8070                          smDetailOtherError,
8071                          agNULL,
8072                          interruptContext);
8073       return;
8074     }
8075     if (oneDeviceData == agNULL)
8076     {
8077       SM_DBG1(("smsatProcessAbnormalCompletion: wrong, oneDeviceData is NULL!!!\n"));
8078     }
8079     else
8080     {
8081       SM_DBG1(("smsatProcessAbnormalCompletion: did %d!!!\n", oneDeviceData->id));
8082     }
8083     tdsmIOCompletedCB( smRoot,
8084                        smIORequestBody->smIORequest,
8085                        smIOFailed,
8086                        smDetailOtherError,
8087                        agNULL,
8088                        interruptContext);
8089     break;
8090   case OSSA_IO_DS_NON_OPERATIONAL:
8091     SM_DBG1(("smsatProcessAbnormalCompletion: agIOStatus = OSSA_IO_DS_NON_OPERATIONAL!!!\n"));
8092     if (smDeviceHandle == agNULL)
8093     {
8094       SM_DBG1(("smsatProcessAbnormalCompletion: wrong, smDeviceHandle is NULL!!!\n"));
8095       tdsmIOCompletedCB( smRoot,
8096                          smIORequestBody->smIORequest,
8097                          smIOFailed,
8098                          smDetailOtherError,
8099                          agNULL,
8100                          interruptContext);
8101       return;
8102     }
8103     if (oneDeviceData == agNULL)
8104     {
8105       SM_DBG1(("smsatProcessAbnormalCompletion: wrong, oneDeviceData is NULL!!!\n"));
8106     }
8107     else
8108     {
8109       SM_DBG1(("smsatProcessAbnormalCompletion: did %d!!!\n", oneDeviceData->id));
8110       agDevHandle = oneDeviceData->agDevHandle;
8111       if (oneDeviceData->valid == agTRUE)
8112       {
8113         saSetDeviceState(agRoot, agNULL, tdsmRotateQnumber(smRoot, smDeviceHandle), agDevHandle, SA_DS_OPERATIONAL);
8114       }
8115     }
8116 
8117     tdsmIOCompletedCB( smRoot,
8118                        smIORequestBody->smIORequest,
8119                        smIOFailed,
8120                        smDetailOtherError,
8121                        agNULL,
8122                        interruptContext);
8123     break;
8124 
8125   case OSSA_IO_PORT_IN_RESET:
8126   case OSSA_IO_DS_IN_RECOVERY:
8127     SM_DBG1(("smsatProcessAbnormalCompletion: OSSA_IO_DS_IN_RECOVERY or OSSA_IO_PORT_IN_RESET status %x\n", agIOStatus));
8128     tdsmIOCompletedCB( smRoot,
8129                        smIORequestBody->smIORequest,
8130                        smIOFailed,
8131                        smDetailOtherError,
8132                        agNULL,
8133                        interruptContext);
8134     break;
8135 
8136   case OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_OPEN_RETRY_BACKOFF_THRESHOLD_REACHED:
8137   case OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_OPEN_TMO:
8138   case OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_NO_DEST:
8139   case OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_OPEN_COLLIDE:
8140   case OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_PATHWAY_BLOCKED:
8141     SM_DBG1(("smsatProcessAbnormalCompletion: SSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_XX status %x\n", agIOStatus));
8142     tdsmIOCompletedCB( smRoot,
8143                       smIORequestBody->smIORequest,
8144                       smIOFailed,
8145                       smDetailOtherError,
8146                       agNULL,
8147                       interruptContext);
8148     break;
8149 
8150   case OSSA_MPI_IO_RQE_BUSY_FULL:
8151   case OSSA_MPI_ERR_IO_RESOURCE_UNAVAILABLE:
8152   case OSSA_MPI_ERR_ATAPI_DEVICE_BUSY:
8153     SM_DBG1(("smsatProcessAbnormalCompletion: agIOStatus = OSSA_MPI_%x!!!\n", agIOStatus));
8154     tdsmIOCompletedCB( smRoot,
8155                        smIORequestBody->smIORequest,
8156                        smIOFailed,
8157                        smDetailBusy,
8158                        agNULL,
8159                        interruptContext);
8160     break;
8161 
8162   case OSSA_IO_XFR_ERROR_DEK_KEY_CACHE_MISS: /* fall through */
8163 #ifdef REMOVED
8164   case OSSA_IO_XFR_ERROR_DEK_KEY_TAG_MISMATCH:
8165 #endif
8166   case OSSA_IO_XFR_ERROR_CIPHER_MODE_INVALID: /* fall through */
8167   case OSSA_IO_XFR_ERROR_DEK_IV_MISMATCH: /* fall through */
8168   case OSSA_IO_XFR_ERROR_DEK_RAM_INTERFACE_ERROR: /* fall through */
8169   case OSSA_IO_XFR_ERROR_DEK_INDEX_OUT_OF_BOUNDS:
8170   case OSSA_IO_XFR_ERROR_DEK_ILLEGAL_TABLE:
8171 
8172     SM_DBG1(("smsatProcessAbnormalCompletion: agIOStatus = ENCRYPTION ERROR 0x%x!!!\n", agIOStatus));
8173     smsatEncryptionHandler(smRoot,
8174                            agIORequest,
8175                            agIOStatus,
8176                            agIOInfoLen,
8177                            agParam,
8178                            0,
8179                            interruptContext);
8180     break;
8181 
8182 #ifdef REMOVED
8183   case OSSA_IO_XFR_ERROR_DIF_APPLICATION_TAG_MISMATCH: /* fall through */
8184   case OSSA_IO_XFR_ERROR_DIF_REFERENCE_TAG_MISMATCH: /* fall through */
8185   case OSSA_IO_XFR_ERROR_DIF_CRC_MISMATCH:
8186     SM_DBG1(("smsatProcessAbnormalCompletion: agIOStatus = DIF ERROR 0x%x!!!\n", agIOStatus));
8187     smsatDifHandler(smRoot,
8188                     agIORequest,
8189                     agIOStatus,
8190                     agIOInfoLen,
8191                     agParam,
8192                     0,
8193                     interruptContext);
8194     break;
8195 #endif
8196 
8197   default:
8198     SM_DBG1(("smsatProcessAbnormalCompletion: agIOStatus = unknown 0x%x!!!\n", agIOStatus));
8199     if (oneDeviceData != agNULL)
8200     {
8201       SM_DBG1(("smsatProcessAbnormalCompletion: did %d!!!\n", oneDeviceData->id));
8202     }
8203     else
8204     {
8205       SM_DBG1(("smsatProcessAbnormalCompletion: oneDeviceData is NULL!!!\n"));
8206     }
8207 
8208     tdsmIOCompletedCB( smRoot,
8209                        smIORequestBody->smIORequest,
8210                        smIOFailed,
8211                        smDetailOtherError,
8212                        agNULL,
8213                        interruptContext);
8214     break;
8215 
8216   } /* switch */
8217 
8218   return;
8219 }
8220 
8221 osGLOBAL void
8222 smsatDelayedProcessAbnormalCompletion(
8223                                       agsaRoot_t        *agRoot,
8224                                       agsaIORequest_t   *agIORequest,
8225                                       bit32             agIOStatus,
8226                                       agsaFisHeader_t   *agFirstDword,
8227                                       bit32             agIOInfoLen,
8228                                       void              *agParam,
8229                                       smSatIOContext_t    *satIOContext
8230                                      )
8231 {
8232   smRoot_t             *smRoot = agNULL;
8233 //  tdsaRootOsData_t     *osData = (tdsaRootOsData_t *)agRoot->osData;
8234 //  tiRoot_t             *tiRoot = (tiRoot_t *)osData->tiRoot;
8235 //  bit32                interruptContext = osData->IntContext;
8236   bit32                interruptContext;
8237   smIORequestBody_t    *smIORequestBody;
8238 //  satDeviceData_t      *pSatDevData;
8239   smDeviceHandle_t     *smDeviceHandle;
8240   smDeviceData_t       *oneDeviceData = agNULL;
8241   agsaDevHandle_t      *agDevHandle = agNULL;
8242 
8243   smIORequestBody = (smIORequestBody_t *)agIORequest->osData;
8244   oneDeviceData     = satIOContext->pSatDevData;
8245   if (oneDeviceData == agNULL)
8246   {
8247     SM_DBG1(("smsatDelayedProcessAbnormalCompletion: oneDeviceData is NULL\n"));
8248     return;
8249   }
8250   smDeviceHandle  = satIOContext->psmDeviceHandle;
8251   smRoot          = oneDeviceData->smRoot;
8252   interruptContext = satIOContext->interruptContext;
8253 
8254   SM_DBG5(("smsatDelayedProcessAbnormalCompletion: start\n"));
8255 
8256   /* Get into the detail */
8257   switch(agIOStatus)
8258   {
8259   case OSSA_IO_SUCCESS:
8260     SM_DBG1(("smsatDelayedProcessAbnormalCompletion: agIOStatus = OSSA_IO_SUCCESS calling smsatIOCompleted!!!\n"));
8261     /* do nothing */
8262     break;
8263 
8264 
8265   case OSSA_IO_ABORTED:
8266     SM_DBG1(("smsatDelayedProcessAbnormalCompletion: agIOStatus = OSSA_IO_ABORTED!!!\n"));
8267 
8268     tdsmIOCompletedCB( smRoot,
8269                        smIORequestBody->smIORequest,
8270                        smIOFailed,
8271                        smDetailAborted,
8272                        agNULL,
8273                        interruptContext);
8274 
8275     if ( oneDeviceData->satTmTaskTag != agNULL )
8276     {
8277       SM_DBG1(("smsatDelayedProcessAbnormalCompletion: TM callback!!!\n"));
8278       if (smDeviceHandle == agNULL)
8279       {
8280         SM_DBG1(("smsatDelayedProcessAbnormalCompletion: wrong, smDeviceHandle is NULL!!!\n"));
8281       }
8282       else
8283       {
8284         /* TM completed */
8285         tdsmEventCB( smRoot,
8286                      smDeviceHandle,
8287                      smIntrEventTypeTaskManagement,
8288                      smTMOK,
8289                      oneDeviceData->satTmTaskTag);
8290         /*
8291          * Reset flag
8292          */
8293         oneDeviceData->satTmTaskTag = agNULL;
8294       }
8295     }
8296 
8297     /*
8298      * Check if we are in recovery mode and need to update the recovery flag
8299      */
8300     if ((oneDeviceData->satDriveState == SAT_DEV_STATE_IN_RECOVERY ) &&
8301         (oneDeviceData->satPendingIO == 0 ))
8302     {
8303       oneDeviceData->satDriveState = SAT_DEV_STATE_NORMAL;
8304       SM_DBG1(("smsatDelayedProcessAbnormalCompletion: STATE NORMAL.!!!\n"));
8305     }
8306 
8307     SM_DBG1(("smsatDelayedProcessAbnormalCompletion: satDriveState %d!!!\n", oneDeviceData->satDriveState));
8308     SM_DBG1(("smsatDelayedProcessAbnormalCompletion: satPendingIO %d satNCQMaxIO %d!!!\n", oneDeviceData->satPendingIO, oneDeviceData->satNCQMaxIO ));
8309     SM_DBG1(("smsatDelayedProcessAbnormalCompletion: satPendingNCQIO %d satPendingNONNCQIO %d!!!\n", oneDeviceData->satPendingNCQIO, oneDeviceData->satPendingNONNCQIO));
8310 
8311     break;
8312 #ifdef REMOVED
8313   case OSSA_IO_OVERFLOW:
8314     SM_DBG1(("smsatDelayedProcessAbnormalCompletion: agIOStatus = OSSA_IO_OVERFLOW!!!\n"));
8315     tdsmIOCompletedCB( smRoot,
8316                        smIORequestBody->smIORequest,
8317                        smIOOverRun,
8318                        agIOInfoLen,
8319                        agNULL,
8320                        interruptContext);
8321     break;
8322 #endif
8323   case OSSA_IO_UNDERFLOW:
8324     SM_DBG1(("smsatDelayedProcessAbnormalCompletion: agIOStatus = OSSA_IO_UNDERFLOW!!!\n"));
8325     tdsmIOCompletedCB( smRoot,
8326                        smIORequestBody->smIORequest,
8327                        smIOUnderRun,
8328                        agIOInfoLen,
8329                        agNULL,
8330                        interruptContext);
8331     break;
8332 
8333 
8334   case OSSA_IO_FAILED:
8335     SM_DBG1(("smsatDelayedProcessAbnormalCompletion: agIOStatus = OSSA_IO_FAILED!!!\n"));
8336     tdsmIOCompletedCB( smRoot,
8337                        smIORequestBody->smIORequest,
8338                        smIOFailed,
8339                        smDetailOtherError,
8340                        agNULL,
8341                        interruptContext);
8342     break;
8343 
8344   case OSSA_IO_ABORT_RESET:
8345     SM_DBG1(("smsatDelayedProcessAbnormalCompletion: agIOStatus = OSSA_IO_ABORT_RESET!!!\n"));
8346     tdsmIOCompletedCB( smRoot,
8347                        smIORequestBody->smIORequest,
8348                        smIOFailed,
8349                        smDetailAbortReset,
8350                        agNULL,
8351                        interruptContext);
8352     /*
8353      * Check if we are in recovery mode and need to update the recovery flag
8354      */
8355     if ((oneDeviceData->satDriveState == SAT_DEV_STATE_IN_RECOVERY ) &&
8356         (oneDeviceData->satPendingIO == 0 ))
8357     {
8358       oneDeviceData->satDriveState = SAT_DEV_STATE_NORMAL;
8359       SM_DBG1(("smsatDelayedProcessAbnormalCompletion: STATE NORMAL.!!!\n"));
8360     }
8361 
8362     SM_DBG1(("smsatDelayedProcessAbnormalCompletion: satDriveState %d!!!\n", oneDeviceData->satDriveState));
8363     SM_DBG1(("smsatDelayedProcessAbnormalCompletion: satPendingIO %d satNCQMaxIO %d!!!\n", oneDeviceData->satPendingIO, oneDeviceData->satNCQMaxIO ));
8364     SM_DBG1(("smsatDelayedProcessAbnormalCompletion: satPendingNCQIO %d satPendingNONNCQIO %d!!!\n", oneDeviceData->satPendingNCQIO, oneDeviceData->satPendingNONNCQIO));
8365 
8366     break;
8367 
8368 #ifdef REMOVED
8369   case OSSA_IO_NOT_VALID:
8370     SM_DBG1(("smsatDelayedProcessAbnormalCompletion: agIOStatus = OSSA_IO_NOT_VALID!!!\n"));
8371     tdsmIOCompletedCB( smRoot,
8372                        smIORequestBody->smIORequest,
8373                        smIOFailed,
8374                        smDetailNotValid,
8375                        agNULL,
8376                        interruptContext);
8377     break;
8378 #endif
8379 
8380   case OSSA_IO_NO_DEVICE:
8381     SM_DBG1(("smsatDelayedProcessAbnormalCompletion: agIOStatus = OSSA_IO_NO_DEVICE!!!\n"));
8382     tdsmIOCompletedCB( smRoot,
8383                        smIORequestBody->smIORequest,
8384                        smIOFailed,
8385                        smDetailNoLogin,
8386                        agNULL,
8387                        interruptContext);
8388     break;
8389 
8390 #ifdef REMOVED /* removed from spec */
8391   case OSSA_IO_ILLEGAL_PARAMETER:
8392     SM_DBG1(("smsatDelayedProcessAbnormalCompletion: agIOStatus = OSSA_IO_ILLEGAL_PARAMETER!!!\n"));
8393     tdsmIOCompletedCB( smRoot,
8394                        smIORequestBody->smIORequest,
8395                        smIOFailed,
8396                        smDetailOtherError,
8397                        agNULL,
8398                        interruptContext);
8399     break;
8400 
8401   case OSSA_IO_LINK_FAILURE:
8402     SM_DBG1(("smsatDelayedProcessAbnormalCompletion: agIOStatus = OSSA_IO_LINK_FAILURE!!!\n"));
8403     tdsmIOCompletedCB( smRoot,
8404                        smIORequestBody->smIORequest,
8405                        smIOFailed,
8406                        smDetailOtherError,
8407                        agNULL,
8408                        interruptContext);
8409     break;
8410 
8411   case OSSA_IO_PROG_ERROR:
8412     SM_DBG1(("smsatDelayedProcessAbnormalCompletion: agIOStatus = OSSA_IO_PROG_ERROR!!!\n"));
8413     tdsmIOCompletedCB( smRoot,
8414                        smIORequestBody->smIORequest,
8415                        smIOFailed,
8416                        smDetailOtherError,
8417                        agNULL,
8418                        interruptContext);
8419     break;
8420 #endif
8421   case OSSA_IO_OPEN_CNX_ERROR_PROTOCOL_NOT_SUPPORTED: /* fall through */
8422   case OSSA_IO_OPEN_CNX_ERROR_ZONE_VIOLATION: /* fall through */
8423   case OSSA_IO_OPEN_CNX_ERROR_BREAK: /* fall through */
8424   case OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS: /* fall through */
8425   case OSSA_IO_OPEN_CNX_ERROR_BAD_DESTINATION: /* fall through */
8426   case OSSA_IO_OPEN_CNX_ERROR_CONNECTION_RATE_NOT_SUPPORTED: /* fall through */
8427   case OSSA_IO_OPEN_CNX_ERROR_WRONG_DESTINATION: /* fall through */
8428   case OSSA_IO_OPEN_CNX_ERROR_OPEN_PREEMPTED: /* fall through */
8429 #ifdef REMOVED /* removed from spec */
8430   case OSSA_IO_OPEN_CNX_ERROR_UNKNOWN_ERROR: /* fall through */
8431 #endif
8432     SM_DBG1(("smsatDelayedProcessAbnormalCompletion: agIOStatus = OSSA_IO_OPEN_CNX_ERROR_* 0x%x!!!\n", agIOStatus));
8433     if (smDeviceHandle == agNULL)
8434     {
8435       SM_DBG1(("smsatDelayedProcessAbnormalCompletion: wrong, smDeviceHandle is NULL!!!\n"));
8436       tdsmIOCompletedCB( smRoot,
8437                          smIORequestBody->smIORequest,
8438                          smIOFailed,
8439                          smDetailOtherError,
8440                          agNULL,
8441                          interruptContext);
8442       return;
8443     }
8444     if (oneDeviceData == agNULL)
8445     {
8446       SM_DBG1(("smsatDelayedProcessAbnormalCompletion: wrong, oneDeviceData is NULL!!!\n"));
8447     }
8448     else
8449     {
8450       SM_DBG1(("smsatDelayedProcessAbnormalCompletion: did %d!!!\n", oneDeviceData->id));
8451     }
8452     tdsmIOCompletedCB( smRoot,
8453                        smIORequestBody->smIORequest,
8454                        smIOFailed,
8455                        smDetailOtherError,
8456                        agNULL,
8457                        interruptContext);
8458     break;
8459 
8460   case OSSA_IO_OPEN_CNX_ERROR_STP_RESOURCES_BUSY:
8461     SM_DBG1(("smsatDelayedProcessAbnormalCompletion: agIOStatus = OSSA_IO_OPEN_CNX_ERROR_STP_RESOURCES_BUSY!!!\n"));
8462     tdsmIOCompletedCB( smRoot,
8463                        smIORequestBody->smIORequest,
8464                        smIOFailed,
8465                        smDetailBusy,
8466                        agNULL,
8467                        interruptContext);
8468     break;
8469 #ifdef REMOVED
8470   case OSSA_IO_XFER_ERROR_BREAK: /* fall throuth */
8471 #endif
8472 
8473   case OSSA_IO_XFER_ERROR_PHY_NOT_READY: /* fall throuth */
8474   case OSSA_IO_XFER_ERROR_NAK_RECEIVED: /* fall throuth */
8475 
8476 #ifdef REMOVED
8477   case OSSA_IO_XFER_ERROR_ACK_NAK_TIMEOUT: /* fall throuth */
8478   case OSSA_IO_XFER_ERROR_PEER_ABORTED: /* fall throuth */
8479 #endif
8480 
8481   case OSSA_IO_XFER_ERROR_DMA: /* fall throuth */
8482 
8483 #ifdef REMOVED
8484   case OSSA_IO_XFER_ERROR_RX_FRAME: /* fall throuth */
8485   case OSSA_IO_XFER_ERROR_CREDIT_TIMEOUT: /* fall throuth */
8486   case OSSA_IO_XFER_ERROR_SATA: /* fall throuth */
8487 #endif
8488   case OSSA_IO_XFER_ERROR_SATA_LINK_TIMEOUT: /* fall throuth */
8489   case OSSA_IO_XFER_ERROR_ABORTED_DUE_TO_SRST: /* fall throuth */
8490   case OSSA_IO_XFER_ERROR_REJECTED_NCQ_MODE: /* fall throuth */
8491 #ifdef REMOVED
8492   case OSSA_IO_XFER_ERROR_ABORTED_NCQ_MODE: /* fall throuth */
8493   case OSSA_IO_XFER_ERROR_DISRUPTED_PHY_DOWN: /* fall throuth */
8494   case OSSA_IO_XFER_ERROR_OFFSET_MISMATCH: /* fall throuth */
8495   case OSSA_IO_XFER_ERROR_XFER_ZERO_DATA_LEN: /* fall throuth */
8496 #endif
8497     SM_DBG1(("smsatDelayedProcessAbnormalCompletion: agIOStatus = OSSA_IO_XFER_ERROR_* 0x%x!!!\n", agIOStatus));
8498 
8499     tdsmIOCompletedCB( smRoot,
8500                        smIORequestBody->smIORequest,
8501                        smIOFailed,
8502                        smDetailOtherError,
8503                        agNULL,
8504                        interruptContext);
8505     break;
8506 #ifdef REMOVED
8507   case OSSA_IO_XFER_ERROR_CMD_ISSUE_ACK_NAK_TIMEOUT: /* fall throuth */
8508   case OSSA_IO_XFER_ERROR_CMD_ISSUE_BREAK_BEFORE_ACK_NAK: /* fall throuth */
8509   case OSSA_IO_XFER_ERROR_CMD_ISSUE_PHY_DOWN_BEFORE_ACK_NAK: /* fall throuth */
8510     SM_DBG1(("smsatDelayedProcessAbnormalCompletion: agIOStatus = OSSA_IO_XFER_ERROR_CMD_ISSUE_* 0x%x!!!\n", agIOStatus));
8511     tdsmIOCompletedCB( smRoot,
8512                        smIORequestBody->smIORequest,
8513                        smIOFailed,
8514                        smDetailOtherError,
8515                        agNULL,
8516                        interruptContext);
8517     break;
8518   case OSSA_IO_XFER_PIO_SETUP_ERROR:
8519     SM_DBG1(("smsatDelayedProcessAbnormalCompletion: agIOStatus = OSSA_IO_XFER_PIO_SETUP_ERROR!!!\n"));
8520     if (smDeviceHandle == agNULL)
8521     {
8522       SM_DBG1(("smsatDelayedProcessAbnormalCompletion: wrong, smDeviceHandle is NULL!!!\n"));
8523     }
8524     if (oneDeviceData == agNULL)
8525     {
8526       SM_DBG1(("smsatDelayedProcessAbnormalCompletion: wrong, oneDeviceData is NULL!!!\n"));
8527     }
8528     else
8529     {
8530       SM_DBG1(("smsatDelayedProcessAbnormalCompletion: did %d!!!\n", oneDeviceData->id));
8531     }
8532     tdsmIOCompletedCB( smRoot,
8533                        smIORequestBody->smIORequest,
8534                        smIOFailed,
8535                        smDetailOtherError,
8536                        agNULL,
8537                        interruptContext);
8538     break;
8539 #endif
8540   case OSSA_IO_DS_IN_ERROR:
8541     SM_DBG1(("smsatDelayedProcessAbnormalCompletion: agIOStatus = OSSA_IO_DS_IN_ERROR!!!\n"));
8542     if (smDeviceHandle == agNULL)
8543     {
8544       SM_DBG1(("smsatDelayedProcessAbnormalCompletion: wrong, smDeviceHandle is NULL!!!\n"));
8545       tdsmIOCompletedCB( smRoot,
8546                          smIORequestBody->smIORequest,
8547                          smIOFailed,
8548                          smDetailOtherError,
8549                          agNULL,
8550                          interruptContext);
8551       return;
8552     }
8553     if (oneDeviceData == agNULL)
8554     {
8555       SM_DBG1(("smsatDelayedProcessAbnormalCompletion: wrong, oneDeviceData is NULL!!!\n"));
8556     }
8557     else
8558     {
8559       SM_DBG1(("smsatDelayedProcessAbnormalCompletion: did %d!!!\n", oneDeviceData->id));
8560     }
8561     tdsmIOCompletedCB( smRoot,
8562                        smIORequestBody->smIORequest,
8563                        smIOFailed,
8564                        smDetailOtherError,
8565                        agNULL,
8566                        interruptContext);
8567     break;
8568   case OSSA_IO_DS_NON_OPERATIONAL:
8569     SM_DBG1(("smsatDelayedProcessAbnormalCompletion: agIOStatus = OSSA_IO_DS_NON_OPERATIONAL!!!\n"));
8570     if (smDeviceHandle == agNULL)
8571     {
8572       SM_DBG1(("smsatDelayedProcessAbnormalCompletion: wrong, smDeviceHandle is NULL!!!\n"));
8573       tdsmIOCompletedCB( smRoot,
8574                          smIORequestBody->smIORequest,
8575                          smIOFailed,
8576                          smDetailOtherError,
8577                          agNULL,
8578                          interruptContext);
8579       return;
8580     }
8581     if (oneDeviceData == agNULL)
8582     {
8583       SM_DBG1(("smsatDelayedProcessAbnormalCompletion: wrong, oneDeviceData is NULL!!!\n"));
8584     }
8585     else
8586     {
8587       SM_DBG1(("smsatDelayedProcessAbnormalCompletion: did %d!!!\n", oneDeviceData->id));
8588       agDevHandle = oneDeviceData->agDevHandle;
8589       if (oneDeviceData->valid == agTRUE)
8590       {
8591         saSetDeviceState(agRoot, agNULL, tdsmRotateQnumber(smRoot, smDeviceHandle), agDevHandle, SA_DS_OPERATIONAL);
8592       }
8593     }
8594     tdsmIOCompletedCB( smRoot,
8595                        smIORequestBody->smIORequest,
8596                        smIOFailed,
8597                        smDetailOtherError,
8598                        agNULL,
8599                        interruptContext);
8600     break;
8601 
8602   case OSSA_IO_PORT_IN_RESET:
8603   case OSSA_IO_DS_IN_RECOVERY:
8604     SM_DBG1(("smsatDelayedProcessAbnormalCompletion: OSSA_IO_DS_IN_RECOVERY or OSSA_IO_PORT_IN_RESET status %x\n", agIOStatus));
8605     tdsmIOCompletedCB( smRoot,
8606                        smIORequestBody->smIORequest,
8607                        smIOFailed,
8608                        smDetailOtherError,
8609                        agNULL,
8610                        interruptContext);
8611     break;
8612 
8613   case OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_OPEN_RETRY_BACKOFF_THRESHOLD_REACHED:
8614   case OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_OPEN_TMO:
8615   case OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_NO_DEST:
8616   case OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_OPEN_COLLIDE:
8617   case OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_PATHWAY_BLOCKED:
8618     SM_DBG1(("smsatDelayedProcessAbnormalCompletion: SSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_XX status %x\n", agIOStatus));
8619     tdsmIOCompletedCB( smRoot,
8620                       smIORequestBody->smIORequest,
8621                       smIOFailed,
8622                       smDetailOtherError,
8623                       agNULL,
8624                       interruptContext);
8625     break;
8626   case OSSA_IO_DS_INVALID:
8627     SM_DBG1(("smsatDelayedProcessAbnormalCompletion: OSSA_IO_DS_INVALID status %x\n", agIOStatus));
8628     tdsmIOCompletedCB( smRoot,
8629                       smIORequestBody->smIORequest,
8630                       smIOFailed,
8631                       smDetailOtherError,
8632                       agNULL,
8633                       interruptContext);
8634     break;
8635 
8636   case OSSA_MPI_IO_RQE_BUSY_FULL:
8637   case OSSA_MPI_ERR_IO_RESOURCE_UNAVAILABLE:
8638   case OSSA_MPI_ERR_ATAPI_DEVICE_BUSY:
8639     SM_DBG1(("smsatDelayedProcessAbnormalCompletion: agIOStatus = OSSA_MPI_%x!!!\n", agIOStatus));
8640     tdsmIOCompletedCB( smRoot,
8641                        smIORequestBody->smIORequest,
8642                        smIOFailed,
8643                        smDetailBusy,
8644                        agNULL,
8645                        interruptContext);
8646     break;
8647 
8648   case OSSA_IO_XFR_ERROR_DEK_KEY_CACHE_MISS: /* fall through */
8649 #ifdef REMOVED
8650   case OSSA_IO_XFR_ERROR_DEK_KEY_TAG_MISMATCH:
8651 #endif
8652   case OSSA_IO_XFR_ERROR_CIPHER_MODE_INVALID: /* fall through */
8653   case OSSA_IO_XFR_ERROR_DEK_IV_MISMATCH: /* fall through */
8654   case OSSA_IO_XFR_ERROR_DEK_RAM_INTERFACE_ERROR: /* fall through */
8655   case OSSA_IO_XFR_ERROR_DEK_INDEX_OUT_OF_BOUNDS:
8656   case OSSA_IO_XFR_ERROR_DEK_ILLEGAL_TABLE:
8657 
8658       SM_DBG1(("smsatDelayedProcessAbnormalCompletion: agIOStatus = ENCRYPTION ERROR 0x%x!!!\n", agIOStatus));
8659       smsatEncryptionHandler(smRoot,
8660                              agIORequest,
8661                              agIOStatus,
8662                              agIOInfoLen,
8663                              agParam,
8664                              0,
8665            interruptContext);
8666       break;
8667 
8668 #ifdef REMOVED
8669   case OSSA_IO_XFR_ERROR_DIF_APPLICATION_TAG_MISMATCH: /* fall through */
8670   case OSSA_IO_XFR_ERROR_DIF_REFERENCE_TAG_MISMATCH: /* fall through */
8671   case OSSA_IO_XFR_ERROR_DIF_CRC_MISMATCH:
8672       SM_DBG1(("smsatDelayedProcessAbnormalCompletion: agIOStatus = DIF ERROR 0x%x!!!\n", agIOStatus));
8673       smsatDifHandler(smRoot,
8674                       agIORequest,
8675                       agIOStatus,
8676                       agIOInfoLen,
8677                       agParam,
8678                       0,
8679                       interruptContext);
8680       break;
8681 #endif
8682 
8683   default:
8684     SM_DBG1(("smsatDelayedProcessAbnormalCompletion: agIOStatus = unknown!!!\n"));
8685     tdsmIOCompletedCB( smRoot,
8686                        smIORequestBody->smIORequest,
8687                        smIOFailed,
8688                        smDetailOtherError,
8689                        agNULL,
8690                        interruptContext);
8691     break;
8692 
8693   } /* switch */
8694   return;
8695 }
8696 
8697 osGLOBAL void
8698 smsatIDStartCB(
8699                 agsaRoot_t        *agRoot,
8700                 agsaIORequest_t   *agIORequest,
8701                 bit32             agIOStatus,
8702                 agsaFisHeader_t   *agFirstDword,
8703                 bit32             agIOInfoLen,
8704                 void              *agParam,
8705                 void              *ioContext
8706                )
8707 {
8708  /*
8709     In the process of SAT_IDENTIFY_DEVICE during discovery
8710   */
8711 //  tdsaRootOsData_t        *osData = (tdsaRootOsData_t *)agRoot->osData;
8712 //  tiRoot_t                *tiRoot = (tiRoot_t *)osData->tiRoot;
8713 //  tdsaRoot_t              *tdsaRoot        = (tdsaRoot_t *) tiRoot->tdData;
8714 //  tdsaContext_t           *tdsaAllShared = (tdsaContext_t *)&tdsaRoot->tdsaAllShared;
8715   smRoot_t                  *smRoot = agNULL;
8716   smIntRoot_t               *smIntRoot = agNULL;
8717   smIntContext_t            *smAllShared = agNULL;
8718   smIORequestBody_t         *smIORequestBody;
8719   smIORequestBody_t         *smOrgIORequestBody = agNULL;
8720   smDeviceHandle_t          *smDeviceHandle;
8721   smSatIOContext_t          *satIOContext;
8722   smSatIOContext_t          *satOrgIOContext;
8723   smSatIOContext_t          *satNewIOContext;
8724   smSatInternalIo_t         *satIntIo;
8725   smSatInternalIo_t         *satNewIntIo = agNULL;
8726   smDeviceData_t            *oneDeviceData;
8727   smIORequest_t             *smOrgIORequest = agNULL;
8728 //  agsaFisRegD2HData_t       *deviceToHostFisData = agNULL;
8729 //  bit8                      signature[8];
8730 #ifdef  TD_DEBUG_ENABLE
8731   agsaFisPioSetupHeader_t   *satPIOSetupHeader = agNULL;
8732   bit32                      ataStatus = 0;
8733   bit32                      ataError;
8734 #endif
8735   agsaSATAIdentifyData_t    *pSATAIdData;
8736   bit16                     *tmpptr, tmpptr_tmp;
8737   bit32                      x;
8738   void                      *sglVirtualAddr;
8739   bit32                      status = 0;
8740 //  tdsaPortContext_t         *onePortContext = agNULL;
8741 //  tiPortalContext_t         *tiPortalContext = agNULL;
8742 //  bit32                     retry_status;
8743   smIORequest_t             *smIORequest;
8744   agsaDevHandle_t           *agDevHandle = agNULL;
8745 
8746   SM_DBG1(("smsatIDStartCB: start\n"));
8747   smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
8748   satIOContext           = (smSatIOContext_t *) ioContext;
8749   satIntIo               = satIOContext->satIntIoContext;
8750   oneDeviceData          = satIOContext->pSatDevData;
8751   smDeviceHandle         = satIOContext->psmDeviceHandle;
8752   smRoot                 = oneDeviceData->smRoot;
8753   smIntRoot              = (smIntRoot_t *)smRoot->smData;
8754   smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
8755   SM_DBG1(("smsatIDStartCB: did %d\n", oneDeviceData->id));
8756 //  onePortContext = oneDeviceData->tdPortContext;
8757 //  tiPortalContext= onePortContext->tiPortalContext;
8758   oneDeviceData->IDDeviceValid = agFALSE;
8759   if (satIntIo == agNULL)
8760   {
8761     SM_DBG1(("smsatIDStartCB: External, OS generated!!!\n"));
8762     SM_DBG1(("smsatIDStartCB: Not possible case!!!\n"));
8763     satOrgIOContext      = satIOContext;
8764     smOrgIORequestBody     = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
8765 
8766     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
8767 
8768     smsatFreeIntIoResource( smRoot,
8769                             oneDeviceData,
8770                             satIntIo);
8771     return;
8772   }
8773   else
8774   {
8775     SM_DBG3(("smsatIDStartCB: Internal, SM generated\n"));
8776     satOrgIOContext        = satIOContext->satOrgIOContext;
8777     if (satOrgIOContext == agNULL)
8778     {
8779       SM_DBG5(("smsatIDStartCB: satOrgIOContext is NULL\n"));
8780     }
8781     else
8782     {
8783       SM_DBG5(("smsatIDStartCB: satOrgIOContext is NOT NULL\n"));
8784       smOrgIORequestBody     = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
8785       if (smOrgIORequestBody == agNULL)
8786       {
8787         SM_DBG1(("smsatIDStartCB: smOrgIORequestBody is NULL!!!\n"));
8788         smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
8789 
8790         smsatFreeIntIoResource(smRoot, oneDeviceData, satIntIo);
8791         return;
8792       }
8793     }
8794     sglVirtualAddr         = satIntIo->satIntSmScsiXchg.sglVirtualAddr;
8795   }
8796   smOrgIORequest           = smIORequestBody->smIORequest;
8797   smIORequest              = smOrgIORequestBody->smIORequest;
8798   smIORequestBody->ioCompleted = agTRUE;
8799   smIORequestBody->ioStarted = agFALSE;
8800 
8801 
8802   if ( agIOStatus == OSSA_IO_XFER_OPEN_RETRY_TIMEOUT ||
8803        agIOStatus == OSSA_IO_OPEN_CNX_ERROR_STP_RESOURCES_BUSY ||
8804        agIOStatus == OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_OPEN_RETRY_BACKOFF_THRESHOLD_REACHED ||
8805        agIOStatus == OSSA_IO_DS_NON_OPERATIONAL )
8806   {
8807     SM_DBG1(("smsatIDStartCB: OPEN_RETRY_TIMEOUT or STP_RESOURCES_BUSY or OPEN_RETRY_BACKOFF_THRESHOLD_REACHED or OSSA_IO_DS_NON_OPERATIONAL!!! 0x%x\n", agIOStatus));
8808     SM_DBG1(("smsatIDStartCB: did %d!!!\n", oneDeviceData->id));
8809     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
8810 
8811     smsatFreeIntIoResource( smRoot,
8812                             oneDeviceData,
8813                             satIntIo);
8814 
8815     SM_DBG2(("smsatIDStartCB: smOrgIORequestBody %p smIORequest %p\n", smOrgIORequestBody, smIORequest));
8816     SM_DBG2(("smsatIDStartCB: smOrgIORequestBody->id %d\n", smOrgIORequestBody->id));
8817     if (agIOStatus == OSSA_IO_XFER_OPEN_RETRY_TIMEOUT)
8818     {
8819       tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIORetry, &(oneDeviceData->satIdentifyData));
8820     }
8821     else if ( agIOStatus == OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_OPEN_RETRY_BACKOFF_THRESHOLD_REACHED ||
8822               agIOStatus == OSSA_IO_DS_NON_OPERATIONAL )
8823     {
8824       /* set device to operational */
8825       agDevHandle = oneDeviceData->agDevHandle;
8826       if (oneDeviceData->valid == agTRUE)
8827       {
8828         saSetDeviceState(agRoot, agNULL, tdsmRotateQnumber(smRoot, smDeviceHandle), agDevHandle, SA_DS_OPERATIONAL);
8829       }
8830       tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIORetry, &(oneDeviceData->satIdentifyData));
8831     }
8832     else
8833     {
8834       tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIOSTPResourceBusy, &(oneDeviceData->satIdentifyData));
8835     }
8836     return;
8837   }
8838 
8839   if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
8840   {
8841     SM_DBG1(("smsatIDStartCB: agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
8842     SM_DBG1(("smsatIDStartCB: did %d!!!\n", oneDeviceData->id));
8843     SM_DBG1(("smsatIDStartCB: before pending IO %d NCQ pending IO %d NONNCQ pending IO %d\n",
8844     oneDeviceData->satPendingIO, oneDeviceData->satPendingNCQIO, oneDeviceData->satPendingNONNCQIO));
8845 
8846     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
8847 
8848     SM_DBG1(("smsatIDStartCB: after pending IO %d NCQ pending IO %d NONNCQ pending IO %d\n",
8849     oneDeviceData->satPendingIO, oneDeviceData->satPendingNCQIO, oneDeviceData->satPendingNONNCQIO));
8850 
8851     smsatFreeIntIoResource( smRoot,
8852                             oneDeviceData,
8853                             satIntIo);
8854 
8855     SM_DBG2(("smsatIDStartCB: smOrgIORequestBody %p smIORequest %p\n", smOrgIORequestBody, smIORequest));
8856     SM_DBG2(("smsatIDStartCB: smOrgIORequestBody->id %d\n", smOrgIORequestBody->id));
8857     tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIOFailed, &(oneDeviceData->satIdentifyData));
8858     return;
8859   }
8860 
8861   if (agIOStatus == OSSA_IO_ABORTED ||
8862       agIOStatus == OSSA_IO_UNDERFLOW ||
8863       agIOStatus == OSSA_IO_XFER_ERROR_BREAK ||
8864       agIOStatus == OSSA_IO_XFER_ERROR_PHY_NOT_READY ||
8865       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_PROTOCOL_NOT_SUPPORTED ||
8866       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_BREAK ||
8867       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS ||
8868       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_BAD_DESTINATION ||
8869       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_CONNECTION_RATE_NOT_SUPPORTED ||
8870       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_WRONG_DESTINATION ||
8871       agIOStatus == OSSA_IO_XFER_ERROR_NAK_RECEIVED ||
8872       agIOStatus == OSSA_IO_XFER_ERROR_DMA ||
8873       agIOStatus == OSSA_IO_XFER_ERROR_SATA_LINK_TIMEOUT ||
8874       agIOStatus == OSSA_IO_XFER_ERROR_REJECTED_NCQ_MODE ||
8875       agIOStatus == OSSA_IO_NO_DEVICE ||
8876       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_ZONE_VIOLATION ||
8877       agIOStatus == OSSA_IO_PORT_IN_RESET ||
8878       agIOStatus == OSSA_IO_DS_NON_OPERATIONAL ||
8879       agIOStatus == OSSA_IO_DS_IN_RECOVERY ||
8880       agIOStatus == OSSA_IO_DS_IN_ERROR ||
8881       agIOStatus == OSSA_IO_DS_INVALID
8882       )
8883   {
8884     SM_DBG1(("smsatIDStartCB: OSSA_IO_OPEN_CNX_ERROR 0x%x!!!\n", agIOStatus));
8885     SM_DBG1(("smsatIDStartCB: did %d!!!\n", oneDeviceData->id));
8886     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
8887 
8888     smsatFreeIntIoResource( smRoot,
8889                             oneDeviceData,
8890                             satIntIo);
8891 
8892 
8893     SM_DBG2(("smsatIDStartCB: smOrgIORequestBody %p smIORequest %p\n", smOrgIORequestBody, smIORequest));
8894     SM_DBG2(("smsatIDStartCB: smOrgIORequestBody->id %d\n", smOrgIORequestBody->id));
8895     tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIOFailed, &(oneDeviceData->satIdentifyData));
8896     return;
8897   }
8898 
8899   if ( agIOStatus != OSSA_IO_SUCCESS ||
8900        (agIOStatus == OSSA_IO_SUCCESS && agFirstDword != agNULL && agIOInfoLen != 0)
8901      )
8902   {
8903 #ifdef  TD_DEBUG_ENABLE
8904     /* only agsaFisPioSetup_t is expected */
8905     satPIOSetupHeader = (agsaFisPioSetupHeader_t *)&(agFirstDword->PioSetup);
8906     ataStatus     = satPIOSetupHeader->status;   /* ATA Status register */
8907     ataError      = satPIOSetupHeader->error;    /* ATA Eror register   */
8908 #endif
8909     SM_DBG1(("smsatIDStartCB: ataStatus 0x%x ataError 0x%x!!!\n", ataStatus, ataError));
8910 
8911     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
8912 
8913     smsatFreeIntIoResource( smRoot,
8914                             oneDeviceData,
8915                             satIntIo);
8916 
8917     SM_DBG2(("smsatIDStartCB: smOrgIORequestBody %p smIORequest %p\n", smOrgIORequestBody, smIORequest));
8918     SM_DBG2(("smsatIDStartCB: smOrgIORequestBody->id %d\n", smOrgIORequestBody->id));
8919 
8920     {
8921        tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIOFailed, &(oneDeviceData->satIdentifyData));
8922     }
8923     return;
8924   }
8925 
8926 
8927   /* success */
8928   SM_DBG3(("smsatIDStartCB: Success\n"));
8929   SM_DBG3(("smsatIDStartCB: Success did %d\n", oneDeviceData->id));
8930 
8931   /* Convert to host endian */
8932   tmpptr = (bit16*)sglVirtualAddr;
8933   for (x=0; x < sizeof(agsaSATAIdentifyData_t)/sizeof(bit16); x++)
8934   {
8935     OSSA_READ_LE_16(AGROOT, &tmpptr_tmp, tmpptr, 0);
8936     *tmpptr = tmpptr_tmp;
8937     tmpptr++;
8938   }
8939 
8940   pSATAIdData = (agsaSATAIdentifyData_t *)sglVirtualAddr;
8941   //smhexdump("satAddSATAIDDevCB before", (bit8 *)pSATAIdData, sizeof(agsaSATAIdentifyData_t));
8942 
8943   SM_DBG5(("smsatIDStartCB: OS satOrgIOContext %p \n", satOrgIOContext));
8944   SM_DBG5(("smsatIDStartCB: TD satIOContext %p \n", satIOContext));
8945   SM_DBG5(("smsatIDStartCB: OS tiScsiXchg %p \n", satOrgIOContext->smScsiXchg));
8946   SM_DBG5(("smsatIDStartCB: TD tiScsiXchg %p \n", satIOContext->smScsiXchg));
8947 
8948 
8949    /* copy ID Dev data to oneDeviceData */
8950   oneDeviceData->satIdentifyData = *pSATAIdData;
8951   oneDeviceData->IDDeviceValid = agTRUE;
8952 
8953 #ifdef SM_INTERNAL_DEBUG
8954   smhexdump("smsatIDStartCB ID Dev data",(bit8 *)pSATAIdData, sizeof(agsaSATAIdentifyData_t));
8955   smhexdump("smsatIDStartCB Device ID Dev data",(bit8 *)&oneDeviceData->satIdentifyData, sizeof(agsaSATAIdentifyData_t));
8956 #endif
8957 
8958   /* set oneDeviceData fields from IndentifyData */
8959   smsatSetDevInfo(oneDeviceData,pSATAIdData);
8960   smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
8961 
8962   smsatFreeIntIoResource( smRoot,
8963                           oneDeviceData,
8964                           satIntIo);
8965 
8966   if (smIORequest->tdData == smIORequest->smData)
8967   {
8968     SM_DBG1(("smsatIDStartCB: the same tdData and smData error!\n"));
8969   }
8970 
8971   /* send the Set Feature ATA command to SATA device for enbling PIO and DMA transfer mode*/
8972   satNewIntIo = smsatAllocIntIoResource( smRoot,
8973                                    smOrgIORequest,
8974                                    oneDeviceData,
8975                                    0,
8976                                    satNewIntIo);
8977 
8978   if (satNewIntIo == agNULL)
8979   {
8980     SM_DBG1(("smsatIDStartCB: momory allocation fails\n"));
8981     tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIOFailed, &(oneDeviceData->satIdentifyData));
8982     return;
8983   } /* end memory allocation */
8984 
8985   satNewIOContext = smsatPrepareNewIO(satNewIntIo,
8986                                     smOrgIORequest,
8987                                     oneDeviceData,
8988                                     agNULL,
8989                                     satOrgIOContext
8990                                     );
8991   /*enable PIO mode*/
8992   status = smsatSetFeaturesPIO(smRoot,
8993                      &satNewIntIo->satIntSmIORequest,
8994                      satNewIOContext->psmDeviceHandle,
8995                      &satNewIntIo->satIntSmScsiXchg, /* orginal from OS layer */
8996                      satNewIOContext
8997                      );
8998 
8999   if (status != SM_RC_SUCCESS)
9000   {
9001       smsatFreeIntIoResource(smRoot,
9002                              oneDeviceData,
9003                              satNewIntIo);
9004       /* clean up TD layer's IORequestBody */
9005       tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIOFailed, &(oneDeviceData->satIdentifyData));
9006   }
9007 
9008   SM_DBG2(("smsatIDStartCB: End device id %d\n", oneDeviceData->id));
9009   return;
9010 }
9011 
9012 
9013 osGLOBAL void
9014 smsatIOCompleted(
9015                  smRoot_t           *smRoot,
9016                  smIORequest_t      *smIORequest,
9017                  agsaFisHeader_t    *agFirstDword,
9018                  bit32              respFisLen,
9019                  agsaFrameHandle_t  agFrameHandle,
9020                  smSatIOContext_t     *satIOContext,
9021                  bit32              interruptContext
9022     )
9023 {
9024 //  satDeviceData_t           *pSatDevData;
9025   smDeviceData_t            *oneDeviceData;
9026   smScsiRspSense_t          *pSense;
9027 #ifdef  TD_DEBUG_ENABLE
9028   smIniScsiCmnd_t           *pScsiCmnd;
9029 #endif
9030   agsaFisRegHostToDevice_t  *hostToDevFis = agNULL;
9031   bit32                      ataStatus = 0;
9032   bit32                      ataError;
9033   smSatInternalIo_t         *satIntIo = agNULL;
9034   bit32                      status;
9035 //  agsaRoot_t                *agRoot;
9036 //  agsaDevHandle_t           *agDevHandle;
9037   smDeviceHandle_t          *smDeviceHandle;
9038   smSatIOContext_t          *satIOContext2;
9039   smIORequestBody_t         *smIORequestBody;
9040   agsaFisRegD2HHeader_t     *statDevToHostFisHeader = agNULL;
9041   agsaFisSetDevBitsHeader_t *statSetDevBitFisHeader = agNULL;
9042   smIORequest_t              smIORequestTMP;
9043 
9044   pSense          = satIOContext->pSense;
9045   oneDeviceData   = satIOContext->pSatDevData;
9046 #ifdef  TD_DEBUG_ENABLE
9047   pScsiCmnd       = satIOContext->pScsiCmnd;
9048 #endif
9049   hostToDevFis    = satIOContext->pFis;
9050 
9051 
9052 //  agRoot          = ((tdsaDeviceData_t *)(pSatDevData->satSaDeviceData))->agRoot;
9053 //  agDevHandle     = ((tdsaDeviceData_t *)(pSatDevData->satSaDeviceData))->agDevHandle;
9054 //  tiDeviceHandle  = &((tdsaDeviceData_t *)(pSatDevData->satSaDeviceData))->tiDeviceHandle;
9055   smDeviceHandle    = satIOContext->psmDeviceHandle;
9056   /*
9057    * Find out the type of response FIS:
9058    * Set Device Bit FIS or Reg Device To Host FIS.
9059    */
9060 
9061   /* First assume it is Reg Device to Host FIS */
9062   statDevToHostFisHeader = (agsaFisRegD2HHeader_t *)&(agFirstDword->D2H);
9063   ataStatus     = statDevToHostFisHeader->status;   /* ATA Status register */
9064   ataError      = statDevToHostFisHeader->error;    /* ATA Eror register   */
9065 
9066   SM_DBG5(("smsatIOCompleted: start\n"));
9067 
9068   /* for debugging */
9069   SM_DBG1(("smsatIOCompleted: H to D command 0x%x!!!\n", hostToDevFis->h.command));
9070   SM_DBG1(("smsatIOCompleted: D to H fistype 0x%x!!!\n", statDevToHostFisHeader->fisType));
9071 
9072 
9073   if (statDevToHostFisHeader->fisType == SET_DEV_BITS_FIS)
9074   {
9075     /* It is Set Device Bits FIS */
9076     statSetDevBitFisHeader = (agsaFisSetDevBitsHeader_t *)&(agFirstDword->D2H);
9077     /* Get ATA Status register */
9078     ataStatus = (statSetDevBitFisHeader->statusHi_Lo & 0x70);               /* bits 4,5,6 */
9079     ataStatus = ataStatus | (statSetDevBitFisHeader->statusHi_Lo & 0x07);   /* bits 0,1,2 */
9080 
9081     /* ATA Eror register   */
9082     ataError  = statSetDevBitFisHeader->error;
9083 
9084     statDevToHostFisHeader = agNULL;
9085   }
9086 
9087   else if (statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS)
9088   {
9089     SM_DBG1(("smsatIOCompleted: *** UNEXPECTED RESP FIS TYPE 0x%x *** smIORequest=%p!!!\n",
9090                  statDevToHostFisHeader->fisType, smIORequest));
9091 
9092     smsatSetSensePayload( pSense,
9093                           SCSI_SNSKEY_HARDWARE_ERROR,
9094                           0,
9095                           SCSI_SNSCODE_INTERNAL_TARGET_FAILURE,
9096                           satIOContext);
9097 
9098     tdsmIOCompletedCB( smRoot,
9099                        smIORequest,
9100                        smIOSuccess,
9101                        SCSI_STAT_CHECK_CONDITION,
9102                        satIOContext->pSmSenseData,
9103                        interruptContext );
9104     return;
9105 
9106   }
9107 
9108   if ( ataStatus & DF_ATA_STATUS_MASK )
9109   {
9110     oneDeviceData->satDeviceFaultState = agTRUE;
9111   }
9112   else
9113   {
9114     oneDeviceData->satDeviceFaultState = agFALSE;
9115   }
9116 
9117   SM_DBG5(("smsatIOCompleted: smIORequest=%p  CDB=0x%x ATA CMD =0x%x\n",
9118     smIORequest, pScsiCmnd->cdb[0], hostToDevFis->h.command));
9119 
9120   /*
9121    * Decide which ATA command is the translation needed
9122    */
9123   switch(hostToDevFis->h.command)
9124   {
9125     case SAT_READ_FPDMA_QUEUED:
9126     case SAT_WRITE_FPDMA_QUEUED:
9127 
9128       /************************************************************************
9129        *
9130        * !!!! See Section 13.5.2.4 of SATA 2.5 specs.                      !!!!
9131        * !!!! If the NCQ error ends up here, it means that the device sent !!!!
9132        * !!!! Set Device Bit FIS (which has SActive register) instead of   !!!!
9133        * !!!! Register Device To Host FIS (which does not have SActive     !!!!
9134        * !!!! register). The callback ossaSATAEvent() deals with the case  !!!!
9135        * !!!! where Register Device To Host FIS was sent by the device.    !!!!
9136        *
9137        * For NCQ we need to issue READ LOG EXT command with log page 10h
9138        * to get the error and to allow other I/Os to continue.
9139        *
9140        * Here is the basic flow or sequence of error recovery, note that due
9141        * to the SATA HW assist that we have, this sequence is slighly different
9142        * from the one described in SATA 2.5:
9143        *
9144        * 1. Set SATA device flag to indicate error condition and returning busy
9145        *    for all new request.
9146        *   return SM_RC_SUCCESS;
9147 
9148        * 2. Because the HW/LL layer received Set Device Bit FIS, it can get the
9149        *    tag or I/O context for NCQ request, SATL would translate the ATA error
9150        *    to SCSI status and return the original NCQ I/O with the appopriate
9151        *    SCSI status.
9152        *
9153        * 3. Prepare READ LOG EXT page 10h command. Set flag to indicate that
9154        *    the failed I/O has been returned to the OS Layer. Send command.
9155        *
9156        * 4. When the device receives READ LOG EXT page 10h request all other
9157        *    pending I/O are implicitly aborted. No completion (aborted) status
9158        *    will be sent to the host for these aborted commands.
9159        *
9160        * 5. SATL receives the completion for READ LOG EXT command in
9161        *    smsatReadLogExtCB(). Steps 6,7,8,9 below are the step 1,2,3,4 in
9162        *    smsatReadLogExtCB().
9163        *
9164        * 6. Check flag that indicates whether the failed I/O has been returned
9165        *    to the OS Layer. If not, search the I/O context in device data
9166        *    looking for a matched tag. Then return the completion of the failed
9167        *    NCQ command with the appopriate/trasnlated SCSI status.
9168        *
9169        * 7. Issue abort to LL layer to all other pending I/Os for the same SATA
9170        *    drive.
9171        *
9172        * 8. Free resource allocated for the internally generated READ LOG EXT.
9173        *
9174        * 9. At the completion of abort, in the context of ossaSATACompleted(),
9175        *    return the I/O with error status to the OS-App Specific layer.
9176        *    When all I/O aborts are completed, clear SATA device flag to
9177        *    indicate ready to process new request.
9178        *
9179        ***********************************************************************/
9180 
9181       SM_DBG1(("smsatIOCompleted: NCQ ERROR smIORequest=%p ataStatus=0x%x ataError=0x%x!!!\n",
9182           smIORequest, ataStatus, ataError ));
9183 
9184       /* Set flag to indicate we are in recovery */
9185       oneDeviceData->satDriveState = SAT_DEV_STATE_IN_RECOVERY;
9186 
9187       /* Return the failed NCQ I/O to OS-Apps Specifiic layer */
9188       smsatDefaultTranslation( smRoot,
9189                                smIORequest,
9190                                satIOContext,
9191                                pSense,
9192                                (bit8)ataStatus,
9193                                (bit8)ataError,
9194                                interruptContext );
9195 
9196       /*
9197        * Allocate resource for READ LOG EXT page 10h
9198        */
9199       satIntIo = smsatAllocIntIoResource( smRoot,
9200                                           &(smIORequestTMP), /* anything but NULL */
9201                                           oneDeviceData,
9202                                           sizeof (satReadLogExtPage10h_t),
9203                                           satIntIo);
9204 
9205       /*
9206        * If we cannot allocate resource for READ LOG EXT 10 in order to do
9207        * the normal NCQ recovery, we will do SATA device reset.
9208        */
9209       if (satIntIo == agNULL)
9210       {
9211         SM_DBG1(("smsatIOCompleted: can't send RLE due to resource lack!!!\n"));
9212 
9213         /* Abort I/O after completion of device reset */
9214         oneDeviceData->satAbortAfterReset = agTRUE;
9215 #ifdef NOT_YET
9216         /* needs further investigation */
9217         /* no report to OS layer */
9218         satSubTM(smRoot,
9219                  smDeviceHandle,
9220                  SM_INTERNAL_TM_RESET,
9221                  agNULL,
9222                  agNULL,
9223                  agNULL,
9224                  agFALSE);
9225 #endif
9226 
9227 
9228         SM_DBG1(("smsatIOCompleted: calling saSATADeviceReset 1!!!\n"));
9229         return;
9230       }
9231 
9232 
9233       /*
9234        * Set flag to indicate that the failed I/O has been returned to the
9235        * OS-App specific Layer.
9236        */
9237       satIntIo->satIntFlag = AG_SAT_INT_IO_FLAG_ORG_IO_COMPLETED;
9238 
9239       /* compare to satPrepareNewIO() */
9240       /* Send READ LOG EXIT page 10h command */
9241 
9242       /*
9243        * Need to initialize all the fields within satIOContext except
9244        * reqType and satCompleteCB which will be set depending on cmd.
9245        */
9246 
9247       smIORequestBody = (smIORequestBody_t *)satIntIo->satIntRequestBody;
9248       satIOContext2 = &(smIORequestBody->transport.SATA.satIOContext);
9249 
9250       satIOContext2->pSatDevData   = oneDeviceData;
9251       satIOContext2->pFis          = &(smIORequestBody->transport.SATA.agSATARequestBody.fis.fisRegHostToDev);
9252       satIOContext2->pScsiCmnd     = &(satIntIo->satIntSmScsiXchg.scsiCmnd);
9253       satIOContext2->pSense        = &(smIORequestBody->transport.SATA.sensePayload);
9254       satIOContext2->pSmSenseData  = &(smIORequestBody->transport.SATA.smSenseData);
9255       satIOContext2->pSmSenseData->senseData = satIOContext2->pSense;
9256 
9257       satIOContext2->smRequestBody = satIntIo->satIntRequestBody;
9258       satIOContext2->interruptContext = interruptContext;
9259       satIOContext2->satIntIoContext  = satIntIo;
9260 
9261       satIOContext2->psmDeviceHandle = smDeviceHandle;
9262       satIOContext2->satOrgIOContext = agNULL;
9263       satIOContext2->smScsiXchg = agNULL;
9264 
9265       status = smsatSendReadLogExt( smRoot,
9266                                     &satIntIo->satIntSmIORequest,
9267                                     smDeviceHandle,
9268                                     &satIntIo->satIntSmScsiXchg,
9269                                     satIOContext2);
9270 
9271       if (status != SM_RC_SUCCESS)
9272       {
9273         SM_DBG1(("smsatIOCompleted: can't send RLE due to LL api failure!!!\n"));
9274         smsatFreeIntIoResource( smRoot,
9275                                 oneDeviceData,
9276                                 satIntIo);
9277 
9278         /* Abort I/O after completion of device reset */
9279         oneDeviceData->satAbortAfterReset = agTRUE;
9280 #ifdef NOT_YET
9281         /* needs further investigation */
9282         /* no report to OS layer */
9283         satSubTM(smRoot,
9284                  smDeviceHandle,
9285                  SM_INTERNAL_TM_RESET,
9286                  agNULL,
9287                  agNULL,
9288                  agNULL,
9289                  agFALSE);
9290 #endif
9291 
9292         SM_DBG1(("smsatIOCompleted: calling saSATADeviceReset 2!!!\n"));
9293         return;
9294       }
9295 
9296       break;
9297 
9298     case SAT_READ_DMA_EXT:
9299       /* fall through */
9300       /* Use default status/error translation */
9301 
9302     case SAT_READ_DMA:
9303       /* fall through */
9304       /* Use default status/error translation */
9305 
9306     default:
9307       smsatDefaultTranslation( smRoot,
9308                                smIORequest,
9309                                satIOContext,
9310                                pSense,
9311                                (bit8)ataStatus,
9312                                (bit8)ataError,
9313                                interruptContext );
9314       break;
9315 
9316   }  /* end switch  */
9317   return;
9318 }
9319 
9320 
9321 osGLOBAL void
9322 smsatEncryptionHandler(
9323                        smRoot_t                *smRoot,
9324                        agsaIORequest_t         *agIORequest,
9325                        bit32                   agIOStatus,
9326                        bit32                   agIOInfoLen,
9327                        void                    *agParam,
9328                        bit32                   agOtherInfo,
9329                        bit32                   interruptContext
9330                       )
9331 {
9332   smIORequestBody_t      *smIORequestBody;
9333   bit32                  errorDetail = smDetailOtherError;
9334 
9335   SM_DBG1(("smsatEncryptionHandler: start\n"));
9336   SM_DBG1(("smsatEncryptionHandler: agIOStatus 0x%x\n", agIOStatus));
9337 
9338   smIORequestBody = (smIORequestBody_t *)agIORequest->osData;
9339 
9340   switch (agIOStatus)
9341   {
9342   case OSSA_IO_XFR_ERROR_DEK_KEY_CACHE_MISS:
9343       SM_DBG1(("smsatEncryptionHandler: OSSA_IO_XFR_ERROR_DEK_KEY_CACHE_MISS\n"));
9344       errorDetail = smDetailDekKeyCacheMiss;
9345       break;
9346   case OSSA_IO_XFR_ERROR_CIPHER_MODE_INVALID:
9347       SM_DBG1(("smsatEncryptionHandler: OSSA_IO_XFR_ERROR_CIPHER_MODE_INVALID\n"));
9348       errorDetail = smDetailCipherModeInvalid;
9349       break;
9350   case OSSA_IO_XFR_ERROR_DEK_IV_MISMATCH:
9351       SM_DBG1(("smsatEncryptionHandler: OSSA_IO_XFR_ERROR_DEK_IV_MISMATCH\n"));
9352       errorDetail = smDetailDekIVMismatch;
9353       break;
9354   case OSSA_IO_XFR_ERROR_DEK_RAM_INTERFACE_ERROR:
9355       SM_DBG1(("smsatEncryptionHandler: OSSA_IO_XFR_ERROR_DEK_RAM_INTERFACE_ERROR\n"));
9356       errorDetail = smDetailDekRamInterfaceError;
9357       break;
9358   case OSSA_IO_XFR_ERROR_DEK_INDEX_OUT_OF_BOUNDS:
9359       SM_DBG1(("smsatEncryptionHandler: OSSA_IO_XFR_ERROR_DEK_INDEX_OUT_OF_BOUNDS\n"));
9360       errorDetail = smDetailDekIndexOutofBounds;
9361       break;
9362   case OSSA_IO_XFR_ERROR_DEK_ILLEGAL_TABLE:
9363       SM_DBG1(("smsatEncryptionHandler:OSSA_IO_XFR_ERROR_DEK_ILLEGAL_TABLE\n"));
9364       errorDetail = smDetailOtherError;
9365       break;
9366   default:
9367       SM_DBG1(("smsatEncryptionHandler: other error!!! 0x%x\n", agIOStatus));
9368       errorDetail = smDetailOtherError;
9369       break;
9370   }
9371 
9372   tdsmIOCompletedCB( smRoot,
9373                      smIORequestBody->smIORequest,
9374                      smIOEncryptError,
9375                      errorDetail,
9376                      agNULL,
9377                      interruptContext
9378                    );
9379   return;
9380 }
9381 
9382 osGLOBAL void
9383 smsatDifHandler(
9384                 smRoot_t                *smRoot,
9385                 agsaIORequest_t         *agIORequest,
9386                 bit32                   agIOStatus,
9387                 bit32                   agIOInfoLen,
9388                 void                    *agParam,
9389                 bit32                   agOtherInfo,
9390                 bit32                   interruptContext
9391                )
9392 {
9393   smIORequestBody_t      *smIORequestBody;
9394   bit32                  errorDetail = smDetailOtherError;
9395 #ifdef  TD_DEBUG_ENABLE
9396   agsaDifDetails_t       *DifDetail;
9397 #endif
9398 
9399   SM_DBG1(("smsatDifHandler: start\n"));
9400   SM_DBG1(("smsatDifHandler: agIOStatus 0x%x\n", agIOStatus));
9401   smIORequestBody = (smIORequestBody_t *)agIORequest->osData;
9402 #ifdef  TD_DEBUG_ENABLE
9403   DifDetail = (agsaDifDetails_t *)agParam;
9404 #endif
9405 
9406   switch (agIOStatus)
9407   {
9408   case OSSA_IO_XFR_ERROR_DIF_APPLICATION_TAG_MISMATCH:
9409       SM_DBG1(("smsatDifHandler: OSSA_IO_XFR_ERROR_DIF_APPLICATION_TAG_MISMATCH\n"));
9410       errorDetail = smDetailDifAppTagMismatch;
9411       break;
9412   case OSSA_IO_XFR_ERROR_DIF_REFERENCE_TAG_MISMATCH:
9413       SM_DBG1(("smsatDifHandler: OSSA_IO_XFR_ERROR_DIF_REFERENCE_TAG_MISMATCH\n"));
9414       errorDetail = smDetailDifRefTagMismatch;
9415       break;
9416   case OSSA_IO_XFR_ERROR_DIF_CRC_MISMATCH:
9417       SM_DBG1(("smsatDifHandler: OSSA_IO_XFR_ERROR_DIF_CRC_MISMATCH\n"));
9418       errorDetail = smDetailDifCrcMismatch;
9419       break;
9420   default:
9421       SM_DBG1(("smsatDifHandler: other error!!! 0x%x\n", agIOStatus));
9422       errorDetail = smDetailOtherError;
9423       break;
9424   }
9425 
9426   SM_DBG1(("smsatDifHandler: DIF detail UpperLBA 0x%08x LowerLBA 0x%08x\n", DifDetail->UpperLBA, DifDetail->LowerLBA));
9427 
9428   tdsmIOCompletedCB( smRoot,
9429                      smIORequestBody->smIORequest,
9430                      smIODifError,
9431                      errorDetail,
9432                      agNULL,
9433                      interruptContext
9434                    );
9435   return;
9436 }
9437 
9438 osGLOBAL void
9439 smsatProcessAbort(
9440                   smRoot_t           *smRoot,
9441                   smIORequest_t      *smIORequest,
9442                   smSatIOContext_t     *satIOContext
9443                  )
9444 {
9445   smDeviceData_t            *oneDeviceData;
9446 #ifdef REMOVED
9447   smDeviceHandle_t          *smDeviceHandle;
9448 #endif
9449   agsaFisRegHostToDevice_t  *hostToDevFis = agNULL;
9450 
9451   SM_DBG5(("smsatProcessAbort: start\n"));
9452 
9453   oneDeviceData   = satIOContext->pSatDevData;
9454 #ifdef REMOVED
9455   smDeviceHandle  = satIOContext->psmDeviceHandle;
9456 #endif
9457   hostToDevFis    = satIOContext->pFis;
9458 
9459   if ( (hostToDevFis->h.command == SAT_SMART && hostToDevFis->h.features == SAT_SMART_EXEUTE_OFF_LINE_IMMEDIATE) &&
9460        (hostToDevFis->d.lbaLow != 0x01 && hostToDevFis->d.lbaLow != 0x02)
9461       )
9462   {
9463     /* no completion for send diagnotic in background. It is done in satSendDiagnostic() */
9464     tdsmIOCompletedCB( smRoot,
9465                        smIORequest,
9466                        smIOFailed,
9467                        smDetailAborted,
9468                        agNULL,
9469                        satIOContext->interruptContext);
9470   }
9471 
9472   if ( oneDeviceData->satTmTaskTag != agNULL )
9473   {
9474     SM_DBG1(("smsatProcessAbort: TM callback!!!\n"));
9475 #ifdef REMOVED
9476     /* TM completed */
9477     tdsmEventCB( smRoot,
9478                  smDeviceHandle,
9479                  smIntrEventTypeTaskManagement,
9480                  smTMOK,
9481                  oneDeviceData->satTmTaskTag);
9482 #endif
9483     /*
9484      * Reset flag
9485      */
9486     oneDeviceData->satTmTaskTag = agNULL;
9487   }
9488 
9489   /*
9490    * Check if we are in recovery mode and need to update the recovery flag
9491    */
9492   if ((oneDeviceData->satDriveState == SAT_DEV_STATE_IN_RECOVERY ) &&
9493       (oneDeviceData->satPendingIO == 0 ))
9494   {
9495     oneDeviceData->satDriveState = SAT_DEV_STATE_NORMAL;
9496     SM_DBG1(("smsatProcessAbort: STATE NORMAL.!!!\n"));
9497   }
9498   SM_DBG1(("smsatProcessAbort: satDriveState %d!!!\n", oneDeviceData->satDriveState));
9499   SM_DBG1(("smsatProcessAbort: satPendingIO %d satNCQMaxIO %d!!!\n", oneDeviceData->satPendingIO, oneDeviceData->satNCQMaxIO ));
9500   SM_DBG1(("smsatProcessAbort: satPendingNCQIO %d satPendingNONNCQIO %d!!!\n", oneDeviceData->satPendingNCQIO, oneDeviceData->satPendingNONNCQIO));
9501 
9502   return;
9503 }
9504 
9505 
9506 osGLOBAL void
9507 smsatNonDataIOCB(
9508                   agsaRoot_t        *agRoot,
9509                   agsaIORequest_t   *agIORequest,
9510                   bit32             agIOStatus,
9511                   agsaFisHeader_t   *agFirstDword,
9512                   bit32             agIOInfoLen,
9513                   void              *agParam,
9514                   void              *ioContext
9515                  )
9516 {
9517   smRoot_t             *smRoot = agNULL;
9518   smIntRoot_t          *smIntRoot = agNULL;
9519   smIntContext_t       *smAllShared = agNULL;
9520   smIORequestBody_t    *smIORequestBody;
9521   bit32                interruptContext;
9522   smSatIOContext_t       *satIOContext;
9523   smDeviceData_t       *oneDeviceData;
9524 
9525   SM_DBG2(("smsatNonDataIOCB: start\n"));
9526   SM_DBG5(("satNonDataIOCB: agIORequest=%p agIOStatus=0x%x agIOInfoLen %d\n",
9527     agIORequest, agIOStatus, agIOInfoLen));
9528 
9529   smIORequestBody = (smIORequestBody_t *)agIORequest->osData;
9530   satIOContext    = (smSatIOContext_t *) ioContext;
9531   oneDeviceData   = satIOContext->pSatDevData;
9532   smRoot          = oneDeviceData->smRoot;
9533   smIntRoot       = (smIntRoot_t *)smRoot->smData;
9534   smAllShared     = (smIntContext_t *)&smIntRoot->smAllShared;
9535   interruptContext = satIOContext->interruptContext;
9536 
9537   smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
9538 
9539 
9540   smIORequestBody->ioCompleted = agTRUE;
9541   smIORequestBody->ioStarted = agFALSE;
9542 
9543   /* Process completion */
9544   if( (agIOStatus == OSSA_IO_SUCCESS) && (agIOInfoLen==0))
9545   {
9546 
9547     SM_DBG1(("satNonDataIOCB: *** ERROR***  agIORequest=%p agIOStatus=0x%x agIOInfoLen %d!!!\n",
9548       agIORequest, agIOStatus, agIOInfoLen));
9549 
9550     tdsmIOCompletedCB( smRoot,
9551                        smIORequestBody->smIORequest,
9552                        smIOFailed,
9553                        smDetailOtherError,
9554                        agNULL,
9555                        interruptContext);
9556   }
9557   else
9558   {
9559     /* More checking needed, for non-data IO this should be the normal case */
9560     smsatProcessAbnormalCompletion( agRoot,
9561                                     agIORequest,
9562                                     agIOStatus,
9563                                     agFirstDword,
9564                                     agIOInfoLen,
9565                                     agParam,
9566                                     satIOContext);
9567   }
9568   return;
9569 }
9570 
9571 osGLOBAL void
9572 smsatInquiryCB(
9573                agsaRoot_t        *agRoot,
9574                agsaIORequest_t   *agIORequest,
9575                bit32             agIOStatus,
9576                agsaFisHeader_t   *agFirstDword,
9577                bit32             agIOInfoLen,
9578                void              *agParam,
9579                void              *ioContext
9580               )
9581 {
9582   /*
9583     In the process of Inquiry
9584     Process SAT_IDENTIFY_DEVICE
9585   */
9586 //  tdsaRootOsData_t        *osData = (tdsaRootOsData_t *)agRoot->osData;
9587 //  tiRoot_t                *tiRoot = (tiRoot_t *)osData->tiRoot;
9588 //  tdsaRoot_t              *tdsaRoot        = (tdsaRoot_t *) tiRoot->tdData;
9589 //  tdsaContext_t           *tdsaAllShared = (tdsaContext_t *)&tdsaRoot->tdsaAllShared;
9590   smRoot_t                 *smRoot = agNULL;
9591   smIntRoot_t              *smIntRoot = agNULL;
9592   smIntContext_t           *smAllShared = agNULL;
9593   smIORequestBody_t        *smIORequestBody;
9594   smIORequestBody_t        *smOrgIORequestBody;
9595   smSatIOContext_t         *satIOContext;
9596   smSatIOContext_t         *satOrgIOContext;
9597   smSatInternalIo_t        *satIntIo;
9598 //  satDeviceData_t         *satDevData;
9599   smDeviceData_t           *oneDeviceData;
9600   smScsiRspSense_t         *pSense;
9601   smIniScsiCmnd_t          *scsiCmnd;
9602   smIORequest_t            *smOrgIORequest;
9603 #ifdef  TD_DEBUG_ENABLE
9604   agsaFisPioSetupHeader_t  *satPIOSetupHeader = agNULL;
9605   bit32                     ataStatus = 0;
9606   bit32                     ataError;
9607 #endif
9608   smScsiInitiatorRequest_t *smScsiRequest; /* TD's smScsiXchg */
9609   smScsiInitiatorRequest_t *smOrgScsiRequest; /* OS's smScsiXchg */
9610   agsaSATAIdentifyData_t   *pSATAIdData;
9611   bit8                     *pInquiry;
9612   bit8                      page = 0xFF;
9613   bit16                    *tmpptr,tmpptr_tmp;
9614   bit32                     x;
9615   bit32                     lenReceived = 0;
9616   bit32                     allocationLen = 0;
9617   bit32                     lenNeeded = 0;
9618   bit8                      dataBuffer[SATA_PAGE89_INQUIRY_SIZE] = {0};
9619 
9620 
9621   SM_DBG6(("smsatInquiryCB: start\n"));
9622   SM_DBG6(("smsatInquiryCB: agIORequest=%p agIOStatus=0x%x agIOInfoLen %d\n", agIORequest, agIOStatus, agIOInfoLen));
9623 
9624   smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
9625   satIOContext           = (smSatIOContext_t *) ioContext;
9626   satIntIo               = satIOContext->satIntIoContext;
9627   oneDeviceData          = satIOContext->pSatDevData;
9628   smScsiRequest          = satIOContext->smScsiXchg;
9629   smRoot                 = oneDeviceData->smRoot;
9630   smIntRoot              = (smIntRoot_t *)smRoot->smData;
9631   smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
9632 
9633   if (satIntIo == agNULL)
9634   {
9635     SM_DBG6(("smsatInquiryCB: External, OS generated\n"));
9636     pSense               = satIOContext->pSense;
9637     scsiCmnd             = satIOContext->pScsiCmnd;
9638     satOrgIOContext      = satIOContext;
9639     smOrgIORequest       = smIORequestBody->smIORequest;
9640   }
9641   else
9642   {
9643     SM_DBG6(("smsatInquiryCB: Internal, TD generated\n"));
9644     satOrgIOContext        = satIOContext->satOrgIOContext;
9645     if (satOrgIOContext == agNULL)
9646     {
9647       SM_DBG1(("smsatInquiryCB: satOrgIOContext is NULL, wrong!!!\n"));
9648       return;
9649     }
9650     else
9651     {
9652       SM_DBG6(("smsatInquiryCB: satOrgIOContext is NOT NULL\n"));
9653     }
9654     smOrgIORequestBody     = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
9655     smOrgIORequest         = (smIORequest_t *)smOrgIORequestBody->smIORequest;
9656     pSense                 = satOrgIOContext->pSense;
9657     scsiCmnd               = satOrgIOContext->pScsiCmnd;
9658   }
9659 
9660   smOrgScsiRequest         = satOrgIOContext->smScsiXchg;
9661   pInquiry                 = dataBuffer;
9662 
9663   smIORequestBody->ioCompleted = agTRUE;
9664   smIORequestBody->ioStarted = agFALSE;
9665 
9666   SM_DBG3(("smsatInquiryCB: did %d\n", oneDeviceData->id));
9667 
9668   if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
9669   {
9670     SM_DBG1(("smsatInquiryCB: agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
9671     if (agIOStatus == OSSA_IO_OPEN_CNX_ERROR_STP_RESOURCES_BUSY)
9672     {
9673       SM_DBG1(("smsatInquiryCB: OSSA_IO_OPEN_CNX_ERROR_STP_RESOURCES_BUSY!!!\n"));
9674       /* should NOT be retried */
9675       tdsmIOCompletedCB( smRoot,
9676                          smOrgIORequest,
9677                          smIOFailed,
9678                          smDetailNoLogin,
9679                          agNULL,
9680                          satOrgIOContext->interruptContext
9681                        );
9682     }
9683     else
9684     {
9685       SM_DBG1(("smsatInquiryCB: NOT OSSA_IO_OPEN_CNX_ERROR_STP_RESOURCES_BUSY!!!\n"));
9686       tdsmIOCompletedCB( smRoot,
9687                          smOrgIORequest,
9688                          smIOFailed,
9689                          smDetailNoLogin,
9690                          agNULL,
9691                          satOrgIOContext->interruptContext
9692                         );
9693     }
9694     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
9695 
9696     smsatFreeIntIoResource( smRoot,
9697                             oneDeviceData,
9698                             satIntIo);
9699     return;
9700   }
9701   if (agIOStatus == OSSA_IO_OPEN_CNX_ERROR_PROTOCOL_NOT_SUPPORTED ||
9702       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_ZONE_VIOLATION ||
9703       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_BREAK ||
9704       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS ||
9705       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_BAD_DESTINATION ||
9706       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_CONNECTION_RATE_NOT_SUPPORTED ||
9707       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_WRONG_DESTINATION ||
9708       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_UNKNOWN_ERROR ||
9709       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_STP_RESOURCES_BUSY
9710       )
9711   {
9712     SM_DBG1(("smsatInquiryCB: OSSA_IO_OPEN_CNX_ERROR!!!\n"));
9713 
9714     tdsmIOCompletedCB( smRoot,
9715                        smOrgIORequest,
9716                        smIOFailed,
9717                        smDetailNoLogin,
9718                        agNULL,
9719                        satOrgIOContext->interruptContext
9720                      );
9721 
9722     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
9723 
9724     smsatFreeIntIoResource( smRoot,
9725                             oneDeviceData,
9726                             satIntIo);
9727     return;
9728   }
9729 
9730  if ( agIOStatus != OSSA_IO_SUCCESS ||
9731       (agIOStatus == OSSA_IO_SUCCESS && agFirstDword != agNULL && agIOInfoLen != 0)
9732     )
9733  {
9734 #ifdef  TD_DEBUG_ENABLE
9735    /* only agsaFisPioSetup_t is expected */
9736    satPIOSetupHeader = (agsaFisPioSetupHeader_t *)&(agFirstDword->PioSetup);
9737    ataStatus     = satPIOSetupHeader->status;   /* ATA Status register */
9738    ataError      = satPIOSetupHeader->error;    /* ATA Eror register   */
9739 #endif
9740    SM_DBG1(("smsatInquiryCB: ataStatus 0x%x ataError 0x%x!!!\n", ataStatus, ataError));
9741 
9742    /* Process abort case */
9743    if (agIOStatus == OSSA_IO_ABORTED)
9744    {
9745      smsatProcessAbort(smRoot,
9746                        smOrgIORequest,
9747                        satOrgIOContext
9748                       );
9749 
9750      smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
9751 
9752      smsatFreeIntIoResource( smRoot,
9753                              oneDeviceData,
9754                              satIntIo);
9755      return;
9756    }
9757 
9758    tdsmIOCompletedCB( smRoot,
9759                       smOrgIORequest,
9760                       smIOFailed,
9761                       smDetailOtherError,
9762                       agNULL,
9763                       satOrgIOContext->interruptContext
9764                      );
9765 
9766    smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
9767 
9768    smsatFreeIntIoResource( smRoot,
9769                            oneDeviceData,
9770                            satIntIo);
9771    return;
9772   }
9773 
9774  /* success */
9775 
9776 
9777  /* Convert to host endian */
9778  tmpptr = (bit16*)(smScsiRequest->sglVirtualAddr);
9779  for (x=0; x < sizeof(agsaSATAIdentifyData_t)/sizeof(bit16); x++)
9780  {
9781    OSSA_READ_LE_16(AGROOT, &tmpptr_tmp, tmpptr, 0);
9782    *tmpptr = tmpptr_tmp;
9783    tmpptr++;
9784    /*Print tmpptr_tmp here for debugging purpose*/
9785  }
9786 
9787  pSATAIdData = (agsaSATAIdentifyData_t *)(smScsiRequest->sglVirtualAddr);
9788 
9789  SM_DBG5(("smsatInquiryCB: OS satOrgIOContext %p \n", satOrgIOContext));
9790  SM_DBG5(("smsatInquiryCB: TD satIOContext %p \n", satIOContext));
9791  SM_DBG5(("smsatInquiryCB: OS smScsiXchg %p \n", satOrgIOContext->smScsiXchg));
9792  SM_DBG5(("smsatInquiryCB: TD smScsiXchg %p \n", satIOContext->smScsiXchg));
9793 
9794  /* copy ID Dev data to oneDeviceData */
9795  oneDeviceData->satIdentifyData = *pSATAIdData;
9796  oneDeviceData->IDDeviceValid = agTRUE;
9797 #ifdef SM_INTERNAL_DEBUG
9798  smhexdump("smsatInquiryCB ID Dev data",(bit8 *)pSATAIdData, sizeof(agsaSATAIdentifyData_t));
9799  smhexdump("smsatInquiryCB Device ID Dev data",(bit8 *)&oneDeviceData->satIdentifyData, sizeof(agsaSATAIdentifyData_t));
9800 #endif
9801 // smhexdump("smsatInquiryCB Device ID Dev data",(bit8 *)&oneDeviceData->satIdentifyData, sizeof(agsaSATAIdentifyData_t));
9802 
9803  /* set oneDeviceData fields from IndentifyData */
9804  smsatSetDevInfo(oneDeviceData,pSATAIdData);
9805 
9806   allocationLen = ((scsiCmnd->cdb[3]) << 8) + scsiCmnd->cdb[4];
9807   allocationLen = MIN(allocationLen, scsiCmnd->expDataLength);
9808 
9809   /* SPC-4, spec 6.4 p 141 */
9810   /* EVPD bit == 0 */
9811   if (!(scsiCmnd->cdb[1] & SCSI_EVPD_MASK))
9812   {
9813     /* Returns the standard INQUIRY data */
9814     lenNeeded = STANDARD_INQUIRY_SIZE;
9815 
9816 
9817     smsatInquiryStandard(pInquiry, pSATAIdData, scsiCmnd);
9818     //smhexdump("smsatInquiryCB ***standard***", (bit8 *)pInquiry, 36);
9819 
9820   }
9821   else
9822   {
9823     /* EVPD bit != 0 && PAGE CODE != 0 */
9824     /* returns the pages of vital product data information */
9825 
9826     /* we must support page 00h, 83h and 89h */
9827     page = scsiCmnd->cdb[2];
9828     if ((page != INQUIRY_SUPPORTED_VPD_PAGE) &&
9829         (page != INQUIRY_DEVICE_IDENTIFICATION_VPD_PAGE) &&
9830         (page != INQUIRY_ATA_INFORMATION_VPD_PAGE) &&
9831         (page != INQUIRY_BLOCK_DEVICE_CHARACTERISTICS_VPD_PAGE))
9832     {
9833       smsatSetSensePayload( pSense,
9834                             SCSI_SNSKEY_ILLEGAL_REQUEST,
9835                             0,
9836                             SCSI_SNSCODE_INVALID_FIELD_IN_CDB,
9837                             satOrgIOContext);
9838 
9839       tdsmIOCompletedCB( smRoot,
9840                          smOrgIORequest,
9841                          smIOSuccess,
9842                          SCSI_STAT_CHECK_CONDITION,
9843                          satOrgIOContext->pSmSenseData,
9844                          satOrgIOContext->interruptContext );
9845 
9846       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
9847 
9848       smsatFreeIntIoResource( smRoot,
9849                               oneDeviceData,
9850                               satIntIo);
9851       SM_DBG1(("smsatInquiryCB: invalid PAGE CODE 0x%x!!!\n", page));
9852       return;
9853     }
9854 
9855     /* checking length */
9856     switch (page)
9857     {
9858     case INQUIRY_SUPPORTED_VPD_PAGE:
9859       lenNeeded = SATA_PAGE0_INQUIRY_SIZE; /* 9 */
9860       break;
9861     case INQUIRY_DEVICE_IDENTIFICATION_VPD_PAGE:
9862       if (oneDeviceData->satWWNSupport)
9863       {
9864         lenNeeded = SATA_PAGE83_INQUIRY_WWN_SIZE; /* 16 */
9865       }
9866       else
9867       {
9868         lenNeeded = SATA_PAGE83_INQUIRY_NO_WWN_SIZE; /* 76 */
9869       }
9870       break;
9871     case INQUIRY_ATA_INFORMATION_VPD_PAGE:
9872       lenNeeded = SATA_PAGE89_INQUIRY_SIZE; /* 572 */
9873       break;
9874     case INQUIRY_BLOCK_DEVICE_CHARACTERISTICS_VPD_PAGE:
9875       lenNeeded = SATA_PAGEB1_INQUIRY_SIZE; /* 64 */
9876       break;
9877     default:
9878       SM_DBG1(("smsatInquiryCB: wrong!!! invalid PAGE CODE 0x%x!!!\n", page));
9879       break;
9880     }
9881 
9882 
9883     /*
9884      * Fill in the Inquiry data depending on what Inquiry data we are returning.
9885      */
9886     switch (page)
9887     {
9888     case INQUIRY_SUPPORTED_VPD_PAGE:
9889       smsatInquiryPage0(pInquiry, pSATAIdData);
9890       break;
9891     case INQUIRY_DEVICE_IDENTIFICATION_VPD_PAGE:
9892       smsatInquiryPage83(pInquiry, pSATAIdData, oneDeviceData);
9893       break;
9894     case INQUIRY_ATA_INFORMATION_VPD_PAGE:
9895       smsatInquiryPage89(pInquiry, pSATAIdData, oneDeviceData, lenReceived);
9896       break;
9897     case INQUIRY_BLOCK_DEVICE_CHARACTERISTICS_VPD_PAGE:
9898       smsatInquiryPageB1(pInquiry, pSATAIdData);
9899       break;
9900     default:
9901       SM_DBG1(("smsatInquiryCB: wrong!!! invalidinvalid PAGE CODE 0x%x!!!\n", page));
9902       break;
9903     }
9904   } /* else */
9905 
9906   SM_DBG6(("smsatInquiryCB: calling tdsmIOCompletedCB\n"));
9907 
9908   /* if this is a standard Inquiry command, notify Stoport to set the device queue depth to max NCQ */
9909   if ( (oneDeviceData->satNCQ == agTRUE) &&
9910        ((scsiCmnd->cdb[1] & 0x01) == 0))
9911   {
9912     if (tdsmSetDeviceQueueDepth(smRoot,
9913                                 smOrgIORequest,
9914                                 oneDeviceData->satNCQMaxIO-1
9915                                 ) == agFALSE)
9916     {
9917       SM_DBG1(("smsatInquiryCB: failed to call tdsmSetDeviceQueueDepth()!!! Q=%d\n", oneDeviceData->satNCQMaxIO));
9918     }
9919   }
9920 
9921   sm_memcpy(smOrgScsiRequest->sglVirtualAddr, dataBuffer, MIN(allocationLen, lenNeeded));
9922   if (allocationLen > lenNeeded)
9923   {
9924     SM_DBG6(("smsatInquiryCB reporting underrun lenNeeded=0x%x allocationLen=0x%x smIORequest=%p\n",
9925         lenNeeded, allocationLen, smOrgIORequest));
9926 
9927     tdsmIOCompletedCB( smRoot,
9928                        smOrgIORequest,
9929                        smIOUnderRun,
9930                        allocationLen - lenNeeded,
9931                        agNULL,
9932                        satOrgIOContext->interruptContext );
9933   }
9934   else
9935   {
9936     tdsmIOCompletedCB( smRoot,
9937                        smOrgIORequest,
9938                        smIOSuccess,
9939                        SCSI_STAT_GOOD,
9940                        agNULL,
9941                        satOrgIOContext->interruptContext);
9942   }
9943 
9944   smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
9945 
9946   smsatFreeIntIoResource( smRoot,
9947                           oneDeviceData,
9948                           satIntIo);
9949   SM_DBG5(("smsatInquiryCB: device %p pending IO %d\n", oneDeviceData, oneDeviceData->satPendingIO));
9950   SM_DBG6(("smsatInquiryCB: end\n"));
9951   return;
9952 }
9953 
9954 osGLOBAL void
9955 smsatInquiryIntCB(
9956                    smRoot_t                  *smRoot,
9957                    smIORequest_t             *smIORequest,
9958                    smDeviceHandle_t          *smDeviceHandle,
9959                    smScsiInitiatorRequest_t  *smScsiRequest,
9960                    smSatIOContext_t            *satIOContext
9961                   )
9962 {
9963   smScsiRspSense_t          *pSense;
9964   smIniScsiCmnd_t           *scsiCmnd;
9965 //  satDeviceData_t           *satDevData;
9966   smDeviceData_t            *oneDeviceData;
9967   agsaSATAIdentifyData_t    *pSATAIdData;
9968 
9969   bit8                      *pInquiry;
9970   bit8                      page = 0xFF;
9971   bit32                     lenReceived = 0;
9972   bit32                     allocationLen = 0;
9973   bit32                     lenNeeded = 0;
9974   bit8                      dataBuffer[SATA_PAGE89_INQUIRY_SIZE] = {0};
9975 
9976   SM_DBG6(("smsatInquiryIntCB: start\n"));
9977 
9978   pSense      = satIOContext->pSense;
9979   scsiCmnd    = &smScsiRequest->scsiCmnd;
9980   pInquiry    = dataBuffer;
9981   oneDeviceData = satIOContext->pSatDevData;
9982   pSATAIdData = &oneDeviceData->satIdentifyData;
9983 
9984   allocationLen = ((scsiCmnd->cdb[3]) << 8) + scsiCmnd->cdb[4];
9985   allocationLen = MIN(allocationLen, scsiCmnd->expDataLength);
9986 
9987   /* SPC-4, spec 6.4 p 141 */
9988   /* EVPD bit == 0 */
9989   if (!(scsiCmnd->cdb[1] & SCSI_EVPD_MASK))
9990   {
9991     /* Returns the standard INQUIRY data */
9992     lenNeeded = STANDARD_INQUIRY_SIZE;
9993 
9994      smsatInquiryStandard(pInquiry, pSATAIdData, scsiCmnd);
9995     //smhexdump("satInquiryIntCB ***standard***", (bit8 *)pInquiry, 36);
9996 
9997   }
9998   else
9999   {
10000     /* EVPD bit != 0 && PAGE CODE != 0 */
10001     /* returns the pages of vital product data information */
10002 
10003     /* we must support page 00h, 83h and 89h */
10004     page = scsiCmnd->cdb[2];
10005     if ((page != INQUIRY_SUPPORTED_VPD_PAGE) &&
10006         (page != INQUIRY_DEVICE_IDENTIFICATION_VPD_PAGE) &&
10007         (page != INQUIRY_ATA_INFORMATION_VPD_PAGE) &&
10008         (page != INQUIRY_UNIT_SERIAL_NUMBER_VPD_PAGE) &&
10009         (page != INQUIRY_BLOCK_DEVICE_CHARACTERISTICS_VPD_PAGE))
10010     {
10011       smsatSetSensePayload( pSense,
10012                             SCSI_SNSKEY_ILLEGAL_REQUEST,
10013                             0,
10014                             SCSI_SNSCODE_INVALID_FIELD_IN_CDB,
10015                             satIOContext);
10016 
10017       tdsmIOCompletedCB( smRoot,
10018                          smIORequest,
10019                          smIOSuccess,
10020                          SCSI_STAT_CHECK_CONDITION,
10021                          satIOContext->pSmSenseData,
10022                          satIOContext->interruptContext );
10023 
10024       SM_DBG1(("smsatInquiryIntCB: invalid PAGE CODE 0x%x!!!\n", page));
10025       return;
10026     }
10027 
10028     /* checking length */
10029     switch (page)
10030     {
10031     case INQUIRY_SUPPORTED_VPD_PAGE:
10032       lenNeeded = SATA_PAGE0_INQUIRY_SIZE; /* 36 */
10033       break;
10034     case INQUIRY_DEVICE_IDENTIFICATION_VPD_PAGE:
10035       if (oneDeviceData->satWWNSupport)
10036       {
10037         lenNeeded = SATA_PAGE83_INQUIRY_WWN_SIZE; /* 16 */
10038       }
10039       else
10040       {
10041         lenNeeded = SATA_PAGE83_INQUIRY_NO_WWN_SIZE; /* 76 */
10042       }
10043       break;
10044     case INQUIRY_ATA_INFORMATION_VPD_PAGE:
10045       lenNeeded = SATA_PAGE89_INQUIRY_SIZE; /* 572 */
10046       break;
10047     case INQUIRY_UNIT_SERIAL_NUMBER_VPD_PAGE:
10048       lenNeeded = SATA_PAGE80_INQUIRY_SIZE; /* 24 */
10049       break;
10050     case INQUIRY_BLOCK_DEVICE_CHARACTERISTICS_VPD_PAGE:
10051       lenNeeded = SATA_PAGEB1_INQUIRY_SIZE; /* 64 */
10052       break;
10053     default:
10054       SM_DBG1(("smsatInquiryIntCB: wrong!!! invalidinvalid PAGE CODE 0x%x!!!\n", page));
10055       break;
10056     }
10057 
10058 
10059     /*
10060      * Fill in the Inquiry data depending on what Inquiry data we are returning.
10061      */
10062     switch (page)
10063     {
10064     case INQUIRY_SUPPORTED_VPD_PAGE:
10065       smsatInquiryPage0(pInquiry, pSATAIdData);
10066       break;
10067     case INQUIRY_DEVICE_IDENTIFICATION_VPD_PAGE:
10068       smsatInquiryPage83(pInquiry, pSATAIdData, oneDeviceData);
10069       break;
10070     case INQUIRY_ATA_INFORMATION_VPD_PAGE:
10071       smsatInquiryPage89(pInquiry, pSATAIdData, oneDeviceData, lenReceived);
10072       break;
10073     case INQUIRY_UNIT_SERIAL_NUMBER_VPD_PAGE:
10074       smsatInquiryPage80(pInquiry, pSATAIdData);
10075       break;
10076     case INQUIRY_BLOCK_DEVICE_CHARACTERISTICS_VPD_PAGE:
10077       smsatInquiryPageB1(pInquiry, pSATAIdData);
10078       break;
10079     default:
10080       SM_DBG1(("smsatInquiryIntCB: wrong!!! invalidinvalid PAGE CODE 0x%x!!!\n", page));
10081       break;
10082     }
10083   } /* else */
10084 
10085   SM_DBG6(("smsatInquiryIntCB: calling tdsmIOCompletedCB\n"));
10086 
10087   /* if this is a standard Inquiry command, notify Stoport to set the device queue depth to max NCQ */
10088   if ( (oneDeviceData->satNCQ == agTRUE) &&
10089        ((scsiCmnd->cdb[1] & 0x01) == 0))
10090   {
10091     if (tdsmSetDeviceQueueDepth(smRoot,
10092                                 smIORequest,
10093                                 oneDeviceData->satNCQMaxIO-1
10094                                 ) == agFALSE)
10095     {
10096       SM_DBG1(("smsatInquiryIntCB: failed to call tdsmSetDeviceQueueDepth()!!! Q=%d\n", oneDeviceData->satNCQMaxIO));
10097     }
10098   }
10099 
10100   sm_memcpy(smScsiRequest->sglVirtualAddr, dataBuffer, MIN(allocationLen, lenNeeded));
10101   if (allocationLen > lenNeeded)
10102   {
10103     SM_DBG6(("smsatInquiryIntCB reporting underrun lenNeeded=0x%x allocationLen=0x%x smIORequest=%p\n",
10104         lenNeeded, allocationLen, smIORequest));
10105 
10106     tdsmIOCompletedCB( smRoot,
10107                        smIORequest,
10108                        smIOUnderRun,
10109                        allocationLen - lenNeeded,
10110                        agNULL,
10111                        satIOContext->interruptContext );
10112   }
10113   else
10114   {
10115     tdsmIOCompletedCB( smRoot,
10116                        smIORequest,
10117                        smIOSuccess,
10118                        SCSI_STAT_GOOD,
10119                        agNULL,
10120                        satIOContext->interruptContext);
10121   }
10122 
10123   SM_DBG5(("smsatInquiryIntCB: device %p pending IO %d\n", oneDeviceData, oneDeviceData->satPendingIO));
10124   SM_DBG6(("smsatInquiryIntCB: end\n"));
10125   return;
10126 
10127 }
10128 
10129 osGLOBAL void
10130 smsatVerify10CB(
10131                 agsaRoot_t        *agRoot,
10132                 agsaIORequest_t   *agIORequest,
10133                 bit32             agIOStatus,
10134                 agsaFisHeader_t   *agFirstDword,
10135                 bit32             agIOInfoLen,
10136                 void              *agParam,
10137                 void              *ioContext
10138                )
10139 {
10140   smRoot_t                *smRoot = agNULL;
10141   smIntRoot_t             *smIntRoot = agNULL;
10142   smIntContext_t          *smAllShared = agNULL;
10143   smIORequestBody_t       *smIORequestBody;
10144   smIORequestBody_t       *smOrgIORequestBody;
10145   smSatIOContext_t          *satIOContext;
10146   smSatIOContext_t          *satOrgIOContext;
10147   smSatInternalIo_t         *satIntIo;
10148   smDeviceData_t          *oneDeviceData;
10149 
10150   smScsiRspSense_t          *pSense;
10151   smIORequest_t             *smOrgIORequest;
10152 
10153   agsaFisRegHostToDevice_t  *hostToDevFis = agNULL;
10154   bit32                     ataStatus = 0;
10155   agsaFisRegD2HHeader_t    *statDevToHostFisHeader = agNULL;
10156 
10157   SM_DBG5(("smsatVerify10CB: start\n"));
10158   SM_DBG5(("smsatVerify10CB: agIORequest=%p agIOStatus=0x%x agIOInfoLen %d\n", agIORequest, agIOStatus, agIOInfoLen));
10159 
10160   /* internally generate smIOContext */
10161   smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
10162   satIOContext           = (smSatIOContext_t *) ioContext;
10163   satIntIo               = satIOContext->satIntIoContext;
10164   oneDeviceData          = satIOContext->pSatDevData;
10165   hostToDevFis           = satIOContext->pFis;
10166   smRoot                 = oneDeviceData->smRoot;
10167   smIntRoot              = (smIntRoot_t *)smRoot->smData;
10168   smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
10169 
10170   if (satIntIo == agNULL)
10171   {
10172     SM_DBG4(("smsatVerify10CB: External smSatInternalIo_t satIntIoContext\n"));
10173     satOrgIOContext = satIOContext;
10174     smOrgIORequest  = smIORequestBody->smIORequest;
10175     pSense          = satIOContext->pSense;
10176   }
10177   else
10178   {
10179     SM_DBG4(("smsatVerify10CB: Internal smSatInternalIo_t satIntIoContext\n"));
10180     satOrgIOContext        = satIOContext->satOrgIOContext;
10181     if (satOrgIOContext == agNULL)
10182     {
10183       SM_DBG4(("smsatVerify10CB: satOrgIOContext is NULL, wrong\n"));
10184       return;
10185     }
10186     else
10187     {
10188       SM_DBG4(("smsatVerify10CB: satOrgIOContext is NOT NULL\n"));
10189     }
10190     smOrgIORequestBody     = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
10191     smOrgIORequest         = (smIORequest_t *)smOrgIORequestBody->smIORequest;
10192     pSense        = satOrgIOContext->pSense;
10193   }
10194 
10195   smIORequestBody->ioCompleted = agTRUE;
10196   smIORequestBody->ioStarted = agFALSE;
10197 
10198   if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
10199   {
10200      SM_DBG1(("smsatVerify10CB: wrong. agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
10201      smsatSetSensePayload( pSense,
10202                            SCSI_SNSKEY_NO_SENSE,
10203                            0,
10204                            SCSI_SNSCODE_NO_ADDITIONAL_INFO,
10205                            satOrgIOContext);
10206 
10207     tdsmIOCompletedCB( smRoot,
10208                        smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
10209                        smIOSuccess,
10210                        SCSI_STAT_CHECK_CONDITION,
10211                        satOrgIOContext->pSmSenseData,
10212                               satOrgIOContext->interruptContext );
10213 
10214     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
10215 
10216     smsatFreeIntIoResource( smRoot,
10217                             oneDeviceData,
10218                             satIntIo);
10219     return;
10220   }
10221 
10222   if (agIOStatus != OSSA_IO_SUCCESS)
10223   {
10224     /* only agsaFisRegDeviceToHost_t is expected */
10225     statDevToHostFisHeader = (agsaFisRegD2HHeader_t *)&(agFirstDword->D2H);
10226     ataStatus     = statDevToHostFisHeader->status;   /* ATA Status register */
10227   }
10228 
10229   if( agIOStatus != OSSA_IO_SUCCESS)
10230   {
10231   if ( (statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS) ||
10232        ((ataStatus & ERR_ATA_STATUS_MASK) || (ataStatus & DF_ATA_STATUS_MASK))
10233        )
10234   {
10235     /* for debugging */
10236     if( agIOStatus != OSSA_IO_SUCCESS)
10237     {
10238       SM_DBG1(("smsatVerify10CB: FAILED, NOT IO_SUCCESS!!!\n"));
10239     }
10240     else if (statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS)
10241     {
10242       SM_DBG1(("smsatVerify10CB: FAILED, Wrong FIS type 0x%x!!!\n",statDevToHostFisHeader->fisType));
10243     }
10244     else if ( (ataStatus & ERR_ATA_STATUS_MASK) ||
10245               (ataStatus & DF_ATA_STATUS_MASK)
10246               )
10247     {
10248       SM_DBG1(("smsatVerify10CB: FAILED, FAILED, error status!!!\n"));
10249     }
10250 
10251     /* Process abort case */
10252     if (agIOStatus == OSSA_IO_ABORTED)
10253     {
10254       smsatProcessAbort(smRoot,
10255                         smOrgIORequest,
10256                         satOrgIOContext
10257                         );
10258 
10259       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
10260 
10261       smsatFreeIntIoResource( smRoot,
10262                               oneDeviceData,
10263                               satIntIo);
10264       return;
10265     }
10266 
10267     /* for debugging */
10268     switch (hostToDevFis->h.command)
10269     {
10270     case SAT_READ_VERIFY_SECTORS_EXT:
10271       SM_DBG1(("smsatVerify10CB: SAT_READ_VERIFY_SECTORS_EXT!!!\n"));
10272       break;
10273     default:
10274       SM_DBG1(("smsatVerify10CB: error default case command 0x%x!!!\n", hostToDevFis->h.command));
10275       break;
10276     }
10277 
10278     smsatSetSensePayload( pSense,
10279                           SCSI_SNSKEY_NO_SENSE,
10280                           0,
10281                           SCSI_SNSCODE_NO_ADDITIONAL_INFO,
10282                           satOrgIOContext);
10283 
10284     tdsmIOCompletedCB( smRoot,
10285                        smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
10286                        smIOSuccess,
10287                        SCSI_STAT_CHECK_CONDITION,
10288                        satOrgIOContext->pSmSenseData,
10289                        satOrgIOContext->interruptContext );
10290 
10291     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
10292 
10293     smsatFreeIntIoResource( smRoot,
10294                             oneDeviceData,
10295                             satIntIo);
10296     return;
10297   } /* end error checking */
10298   }
10299 
10300   /* process success from this point on */
10301   switch (hostToDevFis->h.command)
10302   {
10303   case SAT_READ_VERIFY_SECTORS_EXT:
10304     SM_DBG5(("smsatVerify10CB: SAT_WRITE_DMA_EXT success \n"));
10305 
10306     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
10307 
10308     smsatFreeIntIoResource( smRoot,
10309                             oneDeviceData,
10310                             satIntIo);
10311 
10312     tdsmIOCompletedCB( smRoot,
10313                        smOrgIORequest,
10314                        smIOSuccess,
10315                        SCSI_STAT_GOOD,
10316                        agNULL,
10317                        satOrgIOContext->interruptContext);
10318     break;
10319   default:
10320     SM_DBG1(("smsatVerify10CB: success but error default case command 0x%x!!!\n", hostToDevFis->h.command));
10321 
10322     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
10323 
10324     smsatFreeIntIoResource( smRoot,
10325                             oneDeviceData,
10326                             satIntIo);
10327 
10328     smsatSetSensePayload( pSense,
10329                           SCSI_SNSKEY_NO_SENSE,
10330                           0,
10331                           SCSI_SNSCODE_NO_ADDITIONAL_INFO,
10332                           satOrgIOContext);
10333 
10334     tdsmIOCompletedCB( smRoot,
10335                        smOrgIORequest, /* == &satIntIo->satOrgSmIORequest */
10336                        smIOSuccess,
10337                        SCSI_STAT_CHECK_CONDITION,
10338                        satOrgIOContext->pSmSenseData,
10339                        satOrgIOContext->interruptContext );
10340 
10341     break;
10342   }
10343 
10344   return;
10345 }
10346 
10347 osGLOBAL void
10348 smsatReadLogExtCB(
10349                    agsaRoot_t        *agRoot,
10350                    agsaIORequest_t   *agIORequest,
10351                    bit32             agIOStatus,
10352                    agsaFisHeader_t   *agFirstDword,
10353                    bit32             agIOInfoLen,
10354                    void              *agParam,
10355                    void              *ioContext
10356                  )
10357 {
10358   smRoot_t                *smRoot = agNULL;
10359   smIntRoot_t             *smIntRoot = agNULL;
10360   smIntContext_t          *smAllShared = agNULL;
10361   smIORequestBody_t       *smIORequestBody;
10362   smSatIOContext_t          *satReadLogExtIOContext;
10363   smSatIOContext_t          *satIOContext;
10364   smSatInternalIo_t         *satIntIo;
10365   smDeviceData_t          *oneDeviceData;
10366   agsaIORequest_t         *agAbortIORequest;
10367   smIORequestBody_t       *smAbortIORequestBody;
10368   bit32                   PhysUpper32;
10369   bit32                   PhysLower32;
10370   bit32                   memAllocStatus;
10371   void                    *osMemHandle;
10372   smDeviceHandle_t        *smDeviceHandle;
10373 
10374   SM_DBG5(("smsatReadLogExtCB: start\n"));
10375   SM_DBG1(("smsatReadLogExtCB: agIORequest=%p agIOStatus=0x%x agIOInfoLen %d\n",
10376     agIORequest, agIOStatus, agIOInfoLen));
10377   smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
10378   satReadLogExtIOContext = (smSatIOContext_t *) ioContext;
10379   satIntIo               = satReadLogExtIOContext->satIntIoContext;
10380   oneDeviceData          = satReadLogExtIOContext->pSatDevData;
10381   smDeviceHandle         = satReadLogExtIOContext->psmDeviceHandle;
10382   smRoot                 = oneDeviceData->smRoot;
10383   smIntRoot              = (smIntRoot_t *)smRoot->smData;
10384   smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
10385   SM_DBG1(("smsatReadLogExtCB: did %d!!!\n", oneDeviceData->id));
10386   SM_DBG1(("smsatReadLogExtCB: smIORequestBody ID %d!!!\n", smIORequestBody->id));
10387   SM_DBG1(("smsatReadLogExtCB: smIORequestBody ioCompleted %d ioStarted %d\n", smIORequestBody->ioCompleted, smIORequestBody->ioStarted));
10388   smsatDecrementPendingIO(smRoot, smAllShared, satReadLogExtIOContext);
10389 
10390   smIORequestBody->ioCompleted = agTRUE;
10391   smIORequestBody->ioStarted = agFALSE;
10392 
10393   /*
10394    * If READ LOG EXT failed, we issue device reset.
10395    */
10396   if ( agIOStatus != OSSA_IO_SUCCESS ||
10397        (agIOStatus == OSSA_IO_SUCCESS && agFirstDword != agNULL && agIOInfoLen != 0)
10398      )
10399   {
10400     SM_DBG1(("smsatReadLogExtCB: FAILED.!!!\n"));
10401 
10402     smsatFreeIntIoResource( smRoot,
10403                             oneDeviceData,
10404                             satIntIo);
10405     /* Abort I/O after completion of device reset */
10406     oneDeviceData->satAbortAfterReset = agTRUE;
10407 #ifdef NOT_YET
10408     /* needs to investigate this case */
10409     /* no report to OS layer */
10410     satSubTM(smRoot,
10411              satReadLogExtIOContext->ptiDeviceHandle,
10412              TD_INTERNAL_TM_RESET,
10413              agNULL,
10414              agNULL,
10415              agNULL,
10416              agFALSE);
10417 #endif
10418     return;
10419   }
10420 
10421 
10422   /***************************************************************************
10423    * The following steps take place when READ LOG EXT successfully completed.
10424    ***************************************************************************/
10425 
10426   /************************************************************************
10427    *
10428    * 1. Issue abort to LL layer to all other pending I/Os for the same SATA
10429    *    drive.
10430    *
10431    * 2. Free resource allocated for the internally generated READ LOG EXT.
10432    *
10433    * 3. At the completion of abort, in the context of ossaSATACompleted(),
10434    *    return the I/O with error status to the OS-App Specific layer.
10435    *    When all I/O aborts are completed, clear SATA device flag to
10436    *    indicate ready to process new request.
10437    *
10438    ***********************************************************************/
10439 
10440   /*
10441    * Issue abort to LL layer to all other pending I/Os for the same SATA drive
10442    */
10443   /*
10444     replace the single IO abort with device abort
10445   */
10446 
10447   SM_DBG1(("smsatReadLogExtCB: issuing saSATAAbort. Device Abort!!!\n"));
10448   oneDeviceData->SMAbortAll = agTRUE;
10449   /*
10450   smAbortIORequestBody = smDequeueIO(smRoot);
10451 
10452   if (smAbortIORequestBody == agNULL)
10453   {
10454     SM_DBG1(("smsatReadLogExtCB: empty freeIOList!!!\n"));
10455     return;
10456   }
10457   */
10458   /* allocating agIORequest for abort itself */
10459   memAllocStatus = tdsmAllocMemory(
10460                                    smRoot,
10461                                    &osMemHandle,
10462                                    (void **)&smAbortIORequestBody,
10463                                    &PhysUpper32,
10464                                    &PhysLower32,
10465                                    8,
10466                                    sizeof(smIORequestBody_t),
10467                                    agTRUE
10468                                    );
10469 
10470   if (memAllocStatus != tiSuccess)
10471   {
10472     /* let os process IO */
10473     SM_DBG1(("smsatReadLogExtCB: ostiAllocMemory failed...\n"));
10474     return;
10475   }
10476 
10477   if (smAbortIORequestBody == agNULL)
10478   {
10479     /* let os process IO */
10480     SM_DBG1(("smsatReadLogExtCB: ostiAllocMemory returned NULL smAbortIORequestBody\n"));
10481     return;
10482   }
10483   smIOReInit(smRoot, smAbortIORequestBody);
10484   /* setup task management structure */
10485   smAbortIORequestBody->IOType.InitiatorTMIO.osMemHandle = osMemHandle;
10486   smAbortIORequestBody->smDevHandle = smDeviceHandle;
10487   /* setup task management structure */
10488 //  smAbortIORequestBody->IOType.InitiatorTMIO.osMemHandle = osMemHandle;
10489   satIOContext = &(smAbortIORequestBody->transport.SATA.satIOContext);
10490   satIOContext->smRequestBody = smAbortIORequestBody;
10491 
10492   /* initialize agIORequest */
10493   agAbortIORequest = &(smAbortIORequestBody->agIORequest);
10494   agAbortIORequest->osData = (void *) smAbortIORequestBody;
10495   agAbortIORequest->sdkData = agNULL; /* LL takes care of this */
10496 
10497   /*
10498    * Issue abort (device abort all)
10499    */
10500   saSATAAbort( agRoot, agAbortIORequest, tdsmRotateQnumber(smRoot, smDeviceHandle), oneDeviceData->agDevHandle, 1, agNULL, smaSATAAbortCB);
10501 
10502   /*
10503    * Free resource allocated for the internally generated READ LOG EXT.
10504    */
10505   smsatFreeIntIoResource( smRoot,
10506                           oneDeviceData,
10507                           satIntIo);
10508 
10509   /*
10510    * Sequence of recovery continue at some other context:
10511    * At the completion of abort, in the context of ossaSATACompleted(),
10512    * return the I/O with error status to the OS-App Specific layer.
10513    * When all I/O aborts are completed, clear SATA device flag to
10514    * indicate ready to process new request.
10515    */
10516 
10517   oneDeviceData->satDriveState = SAT_DEV_STATE_NORMAL;
10518 
10519   SM_DBG1(("smsatReadLogExtCB: end return!!!\n"));
10520   return;
10521 }
10522 
10523 osGLOBAL void
10524 ossaSATAEvent(
10525                agsaRoot_t              *agRoot,
10526                agsaIORequest_t         *agIORequest,
10527                agsaPortContext_t       *agPortContext,
10528                agsaDevHandle_t         *agDevHandle,
10529                bit32                   event,
10530                bit32                   agIOInfoLen,
10531                void                    *agParam
10532          )
10533 {
10534   smRoot_t                  *smRoot = gsmRoot;
10535   smIntRoot_t               *smIntRoot    = (smIntRoot_t *)smRoot->smData;
10536   smIntContext_t            *smAllShared = (smIntContext_t *)&smIntRoot->smAllShared;
10537   smDeviceHandle_t          *smDeviceHandle = agNULL;
10538   smDeviceData_t            *oneDeviceData = agNULL;
10539   smList_t                  *DeviceListList;
10540   bit32                     found = agFALSE;
10541   smIORequestBody_t         *smIORequestBody = agNULL;
10542   smSatInternalIo_t           *satIntIo = agNULL;
10543   smSatIOContext_t            *satIOContext2;
10544   smIORequest_t             smIORequestTMP;
10545   bit32                     status;
10546 #ifdef REMOVED
10547   agsaDifDetails_t          agDifDetails;
10548   bit8                      framePayload[256];
10549   bit16                     frameOffset = 0;
10550   bit16                     frameLen = 0;
10551 #endif
10552 
10553   SM_DBG1(("ossaSATAEvent: start\n"));
10554   if (event == OSSA_IO_XFER_ERROR_ABORTED_NCQ_MODE)
10555   {
10556     /* agIORequest is invalid, search for smDeviceHandle from smAllShared using agDevHandle */
10557     /* find a device's existence */
10558     DeviceListList = smAllShared->MainDeviceList.flink;
10559     while (DeviceListList != &(smAllShared->MainDeviceList))
10560     {
10561       oneDeviceData = SMLIST_OBJECT_BASE(smDeviceData_t, MainLink, DeviceListList);
10562       if (oneDeviceData == agNULL)
10563       {
10564         SM_DBG1(("ossaSATAEvent: oneDeviceData is NULL!!!\n"));
10565         return;
10566       }
10567       if (oneDeviceData->agDevHandle == agDevHandle)
10568       {
10569         SM_DBG2(("ossaSATAEvent: did %d\n", oneDeviceData->id));
10570         found = agTRUE;
10571         break;
10572       }
10573       DeviceListList = DeviceListList->flink;
10574     }
10575     if (found == agFALSE)
10576     {
10577       SM_DBG2(("ossaSATAEvent: not found!!!\n"));
10578       return;
10579     }
10580     if (oneDeviceData->valid == agFALSE)
10581     {
10582       SM_DBG2(("ossaSATAEvent: oneDeviceData is not valid did %d!!!\n", oneDeviceData->id));
10583       return;
10584     }
10585     /**************************************************************************
10586      *
10587      * !!!! See Section 13.5.2.4 of SATA 2.5 specs.                       !!!!
10588      * !!!! If the NCQ error ends up here, it means that the device sent  !!!!
10589      * !!!! Register Device To Host FIS (which does not have SActive      !!!!
10590      * !!!! register) instead of Set Device Bit FIS (which has SActive    !!!!
10591      * !!!! register). The routine osSatIOCompleted() deals with the case !!!!
10592      * !!!! where Set Device Bit FIS was sent by the device.              !!!!
10593      *
10594      * For NCQ we need to issue READ LOG EXT command with log page 10h
10595      * to get the error and to allow other I/Os to continue.
10596      *
10597      * Here is the basic flow or sequence of error recovery, this sequence is
10598      * similar to the one described in SATA 2.5:
10599      *
10600      * 1. Set SATA device flag to indicate error condition and returning busy
10601      *    for all new request.
10602      *
10603      * 2. Prepare READ LOG EXT page 10h command. Set flag to indicate that
10604      *    the failed I/O has NOT been returned to the OS Layer. Send command.
10605      *
10606      * 3. When the device receives READ LOG EXT page 10h request all other
10607      *    pending I/O are implicitly aborted. No completion (aborted) status
10608      *    will be sent to the host for these aborted commands.
10609      *
10610      * 4. SATL receives the completion for READ LOG EXT command in
10611      *    smsatReadLogExtCB(). Steps 5,6,7,8 below are the step 1,2,3,4 in
10612      *    smsatReadLogExtCB().
10613      *
10614      * 5. Check flag that indicates whether the failed I/O has been returned
10615      *    to the OS Layer. If not, search the I/O context in device data
10616      *    looking for a matched tag. Then return the completion of the failed
10617      *    NCQ command with the appopriate/trasnlated SCSI status.
10618      *
10619      * 6. Issue abort to LL layer to all other pending I/Os for the same SATA
10620      *    drive.
10621      *
10622      * 7. Free resource allocated for the internally generated READ LOG EXT.
10623      *
10624      * 8. At the completion of abort, in the context of ossaSATACompleted(),
10625      *    return the I/O with error status to the OS-App Specific layer.
10626      *    When all I/O aborts are completed, clear SATA device flag to
10627      *    indicate ready to process new request.
10628      *
10629      *************************************************************************/
10630 
10631     smDeviceHandle = oneDeviceData->smDevHandle;
10632     SM_DBG1(("ossaSATAEvent: did %d!!!\n", oneDeviceData->id));
10633 
10634     if (oneDeviceData->satDriveState == SAT_DEV_STATE_NORMAL)
10635     {
10636       SM_DBG1(("ossaSATAEvent: NCQ ERROR did %d!!!\n", oneDeviceData->id ));
10637 
10638       /* Set flag to indicate we are in recovery */
10639       oneDeviceData->satDriveState = SAT_DEV_STATE_IN_RECOVERY;
10640 
10641       /*
10642        * Allocate resource for READ LOG EXIT page 10h
10643        */
10644       satIntIo = smsatAllocIntIoResource( smRoot,
10645                                           &(smIORequestTMP), /* anything but NULL */
10646                                           oneDeviceData,
10647                                           sizeof (satReadLogExtPage10h_t),
10648                                           satIntIo);
10649 
10650       /*
10651        * If we cannot allocate resource to do the normal NCQ recovery, we
10652        * will do SATA device reset.
10653        */
10654       if (satIntIo == agNULL)
10655       {
10656         /* Abort I/O after completion of device reset */
10657         oneDeviceData->satAbortAfterReset = agTRUE;
10658         SM_DBG1(("ossaSATAEvent: can't send RLE due to resource lack!!!\n"));
10659 
10660 #ifdef NOT_YET
10661         /* needs to investigate this case */
10662         /* no report to OS layer */
10663         smsatSubTM(smRoot,
10664                    smDeviceHandle,
10665                    TD_INTERNAL_TM_RESET,
10666                    agNULL,
10667                    agNULL,
10668                    agNULL,
10669                    agFALSE);
10670 #endif
10671 
10672         return;
10673       }
10674 
10675 
10676       /*
10677        * Clear flag to indicate that the failed I/O has NOT been returned to the
10678        * OS-App specific Layer.
10679        */
10680       satIntIo->satIntFlag = 0;
10681 
10682       /* compare to satPrepareNewIO() */
10683       /* Send READ LOG EXIT page 10h command */
10684 
10685       /*
10686        * Need to initialize all the fields within satIOContext except
10687        * reqType and satCompleteCB which will be set depending on cmd.
10688        */
10689 
10690       smIORequestBody = (smIORequestBody_t *)satIntIo->satIntRequestBody;
10691       satIOContext2 = &(smIORequestBody->transport.SATA.satIOContext);
10692 
10693       satIOContext2->pSatDevData   = oneDeviceData;
10694       satIOContext2->pFis          = &(smIORequestBody->transport.SATA.agSATARequestBody.fis.fisRegHostToDev);
10695       satIOContext2->pScsiCmnd     = &(satIntIo->satIntSmScsiXchg.scsiCmnd);
10696       satIOContext2->pSense        = &(smIORequestBody->transport.SATA.sensePayload);
10697       satIOContext2->pSmSenseData  = &(smIORequestBody->transport.SATA.smSenseData);
10698       satIOContext2->pSmSenseData->senseData = satIOContext2->pSense;
10699 
10700       satIOContext2->smRequestBody = satIntIo->satIntRequestBody;
10701       //not used
10702 //      satIOContext2->interruptContext = interruptContext;
10703       satIOContext2->satIntIoContext  = satIntIo;
10704 
10705       satIOContext2->psmDeviceHandle = smDeviceHandle;
10706       satIOContext2->satOrgIOContext = agNULL;
10707       satIOContext2->smScsiXchg = agNULL;
10708 
10709       SM_DBG1(("ossaSATAEvent: smIORequestBody ID %d!!!\n", smIORequestBody->id));
10710       SM_DBG1(("ossaSATAEvent: smIORequestBody ioCompleted %d ioStarted %d\n", smIORequestBody->ioCompleted, smIORequestBody->ioStarted));
10711       status = smsatSendReadLogExt( smRoot,
10712                                     &satIntIo->satIntSmIORequest,
10713                                     smDeviceHandle,
10714                                     &satIntIo->satIntSmScsiXchg,
10715                                     satIOContext2);
10716 
10717       if (status != SM_RC_SUCCESS)
10718       {
10719         SM_DBG1(("ossaSATAEvent: can't send RLE due to LL api failure!!!\n"));
10720         smsatFreeIntIoResource( smRoot,
10721                                 oneDeviceData,
10722                                 satIntIo);
10723         /* Abort I/O after completion of device reset */
10724         oneDeviceData->satAbortAfterReset = agTRUE;
10725 #ifdef NOT_YET
10726         /* needs to investigate this case */
10727         /* no report to OS layer */
10728         smsatSubTM(smRoot,
10729                    smDeviceHandle,
10730                    TD_INTERNAL_TM_RESET,
10731                    agNULL,
10732                    agNULL,
10733                    agNULL,
10734                    agFALSE);
10735 #endif
10736 
10737         return;
10738       }
10739     }
10740     else
10741     {
10742       SM_DBG1(("ossaSATAEvent: NCQ ERROR but recovery in progress!!!\n"));
10743     }
10744   }
10745   else if (event == OSSA_IO_XFER_CMD_FRAME_ISSUED)
10746   {
10747     SM_DBG1(("ossaSATAEvent: OSSA_IO_XFER_CMD_FRAME_ISSUED\n"));
10748   }
10749   else if (event == OSSA_IO_XFER_PIO_SETUP_ERROR)
10750   {
10751     SM_DBG1(("ossaSATAEvent: OSSA_IO_XFER_PIO_SETUP_ERROR\n"));
10752   }
10753   else if (event == OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_OPEN_RETRY_BACKOFF_THRESHOLD_REACHED)
10754   {
10755     SM_DBG1(("ossaSATAEvent: OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_OPEN_RETRY_BACKOFF_THRESHOLD_REACHED\n"));
10756   }
10757   else if (event == OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_OPEN_TMO)
10758   {
10759     SM_DBG1(("ossaSATAEvent: OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_OPEN_TMO\n"));
10760   }
10761   else if (event == OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_NO_DEST)
10762   {
10763     SM_DBG1(("ossaSATAEvent: OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_NO_DEST\n"));
10764   }
10765   else if (event == OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_OPEN_COLLIDE)
10766   {
10767     SM_DBG1(("ossaSATAEvent: OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_OPEN_COLLIDE\n"));
10768   }
10769   else if (event == OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_PATHWAY_BLOCKED)
10770   {
10771     SM_DBG1(("ossaSATAEvent: OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_PATHWAY_BLOCKED\n"));
10772   }
10773   else if (event == OSSA_IO_XFR_ERROR_DEK_KEY_TAG_MISMATCH)
10774   {
10775     SM_DBG1(("ossaSATAEvent: OSSA_IO_XFR_ERROR_DEK_KEY_TAG_MISMATCH\n"));
10776   }
10777 #ifdef REMOVED
10778   else if (event == OSSA_IO_XFR_ERROR_DIF_MISMATCH || event == OSSA_IO_XFR_ERROR_DIF_APPLICATION_TAG_MISMATCH ||
10779            event == OSSA_IO_XFR_ERROR_DIF_REFERENCE_TAG_MISMATCH || event == OSSA_IO_XFR_ERROR_DIF_CRC_MISMATCH )
10780   {
10781     SM_DBG1(("ossaSATAEvent: DIF related, event 0x%x\n", event));
10782     /* process DIF detail information */
10783     SM_DBG2(("ossaSATAEvent: agIOInfoLen %d\n", agIOInfoLen));
10784     if (agParam == agNULL)
10785     {
10786       SM_DBG2(("ossaSATAEvent: agParam is NULL!!!\n"));
10787       return;
10788     }
10789     if (agIOInfoLen < sizeof(agsaDifDetails_t))
10790     {
10791       SM_DBG2(("ossaSATAEvent: wrong agIOInfoLen!!! agIOInfoLen %d sizeof(agsaDifDetails_t) %d\n", agIOInfoLen, (int)sizeof(agsaDifDetails_t)));
10792       return;
10793     }
10794     /* reads agsaDifDetails_t */
10795     saFrameReadBlock(agRoot, agParam, 0, &agDifDetails, sizeof(agsaDifDetails_t));
10796     frameOffset = (agDifDetails.ErrBoffsetEDataLen & 0xFFFF);
10797     frameLen = (agDifDetails.ErrBoffsetEDataLen & 0xFFFF0000) >> 16;
10798 
10799     SM_DBG2(("ossaSATAEvent: UpperLBA 0x%08x LowerLBA 0x%08x\n", agDifDetails.UpperLBA, agDifDetails.LowerLBA));
10800     SM_DBG2(("ossaSATAEvent: SASAddrHI 0x%08x SASAddrLO 0x%08x\n",
10801              SM_GET_SAS_ADDRESSHI(agDifDetails.sasAddressHi), SM_GET_SAS_ADDRESSLO(agDifDetails.sasAddressLo)));
10802     SM_DBG2(("ossaSATAEvent: DIF error mask 0x%x Device ID 0x%x\n",
10803              (agDifDetails.DIFErrDevID) & 0xFF, (agDifDetails.DIFErrDevID & 0xFFFF0000) >> 16));
10804     if (frameLen != 0 && frameLen <= 256)
10805     {
10806       saFrameReadBlock(agRoot, agParam, sizeof(agsaDifDetails_t), framePayload, frameLen);
10807       smhexdump("ossaSATAEvent frame", framePayload, frameLen);
10808     }
10809   }
10810 #endif
10811   else if (event == OSSA_IO_OPEN_CNX_ERROR_STP_RESOURCES_BUSY)
10812   {
10813     smIORequestBody = (smIORequestBody_t *)agIORequest->osData;
10814     if (smIORequestBody == agNULL)
10815     {
10816       SM_DBG1(("ossaSATAEvent: smIORequestBody is NULL!!!\n"));
10817       return;
10818     }
10819     smDeviceHandle = smIORequestBody->smDevHandle;
10820     if (smDeviceHandle == agNULL)
10821     {
10822       SM_DBG1(("ossaSATAEvent: smDeviceHandle is NULL!!!\n"));
10823       return;
10824     }
10825     oneDeviceData  = (smDeviceData_t *)smDeviceHandle->smData;
10826     if (oneDeviceData == agNULL)
10827     {
10828       SM_DBG1(("ossaSATAEvent: oneDeviceData is NULL!!!\n"));
10829       return;
10830     }
10831     SM_DBG1(("ossaSATAEvent: ERROR event %d did=%d\n", event, oneDeviceData->id));
10832 
10833 
10834     if (smAllShared->FCA)
10835     {
10836       if (oneDeviceData->SMNumOfFCA <= 0) /* does SMP HARD RESET only upto one time */
10837       {
10838         SM_DBG1(("ossaSATAEvent: OSSA_IO_OPEN_CNX_ERROR_STP_RESOURCES_BUSY; sending HARD_RESET\n"));
10839         oneDeviceData->SMNumOfFCA++;
10840         smPhyControlSend(smRoot,
10841                          oneDeviceData,
10842                          SMP_PHY_CONTROL_HARD_RESET,
10843                          agNULL,
10844                          tdsmRotateQnumber(smRoot, smDeviceHandle)
10845                         );
10846       }
10847       else
10848       {
10849         /* given up after one time of SMP HARD RESET; */
10850         SM_DBG1(("ossaSATAEvent: OSSA_IO_OPEN_CNX_ERROR_STP_RESOURCES_BUSY; but giving up sending HARD_RESET!!!\n"));
10851       }
10852     }
10853   }
10854   else if (event == OSSA_IO_XFER_ERROR_NAK_RECEIVED)
10855   {
10856     SM_DBG1(("ossaSATAEvent: OSSA_IO_XFER_ERROR_NAK_RECEIVED\n"));
10857   }
10858   else if (event == OSSA_IO_XFER_ERROR_DMA_ACTIVATE_TIMEOUT)
10859   {
10860     SM_DBG1(("ossaSATAEvent: OSSA_IO_XFER_ERROR_DMA_ACTIVATE_TIMEOUT\n"));
10861   }
10862   else
10863   {
10864     SM_DBG1(("ossaSATAEvent: other event 0x%x\n", event));
10865   }
10866 
10867   return;
10868 }
10869 
10870 osGLOBAL void
10871 smSMPCompletedCB(
10872                   agsaRoot_t            *agRoot,
10873                   agsaIORequest_t       *agIORequest,
10874                   bit32                 agIOStatus,
10875                   bit32                 agIOInfoLen,
10876                   agsaFrameHandle_t     agFrameHandle
10877                 )
10878 {
10879   smSMPRequestBody_t *smSMPRequestBody = (smSMPRequestBody_t *) agIORequest->osData;
10880 
10881   SM_DBG2(("smSMPCompletedCB: start\n"));
10882 
10883   if (smSMPRequestBody == agNULL)
10884   {
10885     SM_DBG1(("smSMPCompletedCB: smSMPRequestBody is NULL!!!\n"));
10886     return;
10887   }
10888 
10889   if (smSMPRequestBody->SMPCompletionFunc == agNULL)
10890   {
10891     SM_DBG1(("smSMPCompletedCB: smSMPRequestBody->SMPCompletionFunc is NULL!!!\n"));
10892     return;
10893   }
10894 
10895   /* calling smSMPCompleted */
10896   smSMPRequestBody->SMPCompletionFunc(
10897                                        agRoot,
10898                                        agIORequest,
10899                                        agIOStatus,
10900                                        agIOInfoLen,
10901                                        agFrameHandle
10902                                      );
10903   return;
10904 }
10905 
10906 osGLOBAL void
10907 smSMPCompleted(
10908                 agsaRoot_t            *agRoot,
10909                 agsaIORequest_t       *agIORequest,
10910                 bit32                 agIOStatus,
10911                 bit32                 agIOInfoLen,
10912                 agsaFrameHandle_t     agFrameHandle
10913               )
10914 {
10915   smRoot_t           *smRoot = gsmRoot;
10916   smSMPRequestBody_t *smSMPRequestBody = (smSMPRequestBody_t *) agIORequest->osData;
10917   smDeviceData_t     *oneDeviceData;
10918   smDeviceHandle_t   *smDeviceHandle;
10919   smIORequest_t      *CurrentTaskTag;
10920   bit8                smpHeader[4];
10921   smSMPFrameHeader_t *smSMPFrameHeader;
10922   agsaDevHandle_t    *agDevHandle = agNULL;
10923 
10924   SM_DBG2(("smSMPCompleted: start\n"));
10925 
10926   if (smSMPRequestBody == agNULL)
10927   {
10928     SM_DBG1(("smSMPCompleted: smSMPRequestBody is NULL, wrong!!!\n"));
10929     return;
10930   }
10931 
10932   CurrentTaskTag  = smSMPRequestBody->CurrentTaskTag;
10933   oneDeviceData = smSMPRequestBody->smDeviceData;
10934   smDeviceHandle = smSMPRequestBody->smDevHandle;
10935   if (smDeviceHandle == agNULL)
10936   {
10937     SM_DBG2(("smSMPCompleted: smDeviceHandle is NULL, wrong!!!\n"));
10938     return;
10939   }
10940 
10941   if (oneDeviceData == agNULL)
10942   {
10943     SM_DBG2(("smSMPCompleted: oneDeviceData is NULL, wrong!!!\n"));
10944     return;
10945   }
10946   agDevHandle = oneDeviceData->agExpDevHandle;
10947   if (agIOStatus == OSSA_IO_SUCCESS)
10948   {
10949     saFrameReadBlock(agRoot, agFrameHandle, 0, smpHeader, 4);
10950     smSMPFrameHeader = (smSMPFrameHeader_t *)smpHeader;
10951     if (smSMPFrameHeader->smpFunction == SMP_PHY_CONTROL)
10952     {
10953       SM_DBG3(("smSMPCompleted: phy control\n"));
10954       if (agIOInfoLen != 4 &&
10955           smSMPFrameHeader->smpFunctionResult == SMP_FUNCTION_ACCEPTED) /*zero length is expected */
10956       {
10957         SM_DBG1(("smSMPCompleted: mismatch len agIOInfoLen 0x%x 0x%x!!!\n", agIOInfoLen, 4));
10958         tdsmFreeMemory(
10959                        smRoot,
10960                        smSMPRequestBody->osMemHandle,
10961                        sizeof(smSMPRequestBody_t)
10962                       );
10963         if (CurrentTaskTag != agNULL)
10964         {
10965           tdsmEventCB(smRoot,
10966                       smDeviceHandle,
10967                       smIntrEventTypeTaskManagement,
10968                       smTMFailed,
10969                       CurrentTaskTag);
10970         }
10971 
10972         return;
10973       }
10974       smPhyControlRespRcvd(smRoot,
10975                            agRoot,
10976                            agIORequest,
10977                            oneDeviceData,
10978                            smSMPFrameHeader,
10979                            agFrameHandle,
10980                            CurrentTaskTag
10981                            );
10982     }
10983     else
10984     {
10985       /* unknown SMP function */
10986       SM_DBG2(("smSMPCompleted: unknown smSMPFrameHeader %d!!!\n", smSMPFrameHeader->smpFunction));
10987       tdsmFreeMemory(
10988                       smRoot,
10989                       smSMPRequestBody->osMemHandle,
10990                       sizeof(smSMPRequestBody_t)
10991                      );
10992       if (CurrentTaskTag != agNULL)
10993       {
10994         tdsmEventCB(smRoot,
10995                     smDeviceHandle,
10996                     smIntrEventTypeTaskManagement,
10997                     smTMFailed,
10998                     CurrentTaskTag);
10999       }
11000       return;
11001     }
11002   }
11003   else
11004   {
11005     SM_DBG2(("smSMPCompleted: failed agIOStatus %d!!!\n", agIOStatus));
11006     if (agIOStatus == OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS ||
11007         agIOStatus == OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_OPEN_RETRY_BACKOFF_THRESHOLD_REACHED ||
11008         agIOStatus == OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_OPEN_TMO ||
11009         agIOStatus == OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_NO_DEST ||
11010         agIOStatus == OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_OPEN_COLLIDE ||
11011         agIOStatus == OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_PATHWAY_BLOCKED ||
11012         agIOStatus == OSSA_IO_DS_NON_OPERATIONAL
11013        )
11014     {
11015       SM_DBG1(("smSMPCompleted: setting back to operational\n"));
11016       if (agDevHandle != agNULL)
11017       {
11018         saSetDeviceState(agRoot, agNULL, tdsmRotateQnumber(smRoot, smDeviceHandle), agDevHandle, SA_DS_OPERATIONAL);
11019       }
11020       else
11021       {
11022         SM_DBG1(("smSMPCompleted: agDevHandle is NULL\n"));
11023       }
11024     }
11025     tdsmFreeMemory(
11026                     smRoot,
11027                     smSMPRequestBody->osMemHandle,
11028                     sizeof(smSMPRequestBody_t)
11029                   );
11030     if (CurrentTaskTag != agNULL)
11031     {
11032       tdsmEventCB(smRoot,
11033                   smDeviceHandle,
11034                   smIntrEventTypeTaskManagement,
11035                   smTMFailed,
11036                   CurrentTaskTag);
11037     }
11038     return;
11039   }
11040 
11041   tdsmFreeMemory(
11042                   smRoot,
11043                   smSMPRequestBody->osMemHandle,
11044                   sizeof(smSMPRequestBody_t)
11045                 );
11046   return;
11047 }
11048 
11049 osGLOBAL void
11050 smPhyControlRespRcvd(
11051                       smRoot_t              *smRoot,
11052                       agsaRoot_t            *agRoot,
11053                       agsaIORequest_t       *agIORequest,
11054                       smDeviceData_t        *oneDeviceData, /* sata disk */
11055                       smSMPFrameHeader_t    *frameHeader,
11056                       agsaFrameHandle_t     frameHandle,
11057                       smIORequest_t         *CurrentTaskTag
11058                      )
11059 {
11060   smDeviceData_t        *TargetDeviceData = agNULL;
11061   agsaDevHandle_t       *agDevHandle = agNULL;
11062   smSMPRequestBody_t    *smSMPRequestBody;
11063   smDeviceHandle_t      *smDeviceHandle;
11064 
11065   SM_DBG2(("smPhyControlRespRcvd: start\n"));
11066 
11067   if (CurrentTaskTag == agNULL )
11068   {
11069     SM_DBG1(("smPhyControlRespRcvd: CurrentTaskTag is NULL; allowed\n"));
11070     return;
11071   }
11072 
11073   smSMPRequestBody = (smSMPRequestBody_t *)CurrentTaskTag->smData;
11074   if (smSMPRequestBody == agNULL)
11075   {
11076     SM_DBG1(("smPhyControlRespRcvd: smSMPRequestBody is NULL!!!\n"));
11077     return;
11078   }
11079 
11080   smDeviceHandle = smSMPRequestBody->smDevHandle;
11081   if (smDeviceHandle == agNULL)
11082   {
11083     SM_DBG2(("smPhyControlRespRcvd: smDeviceHandle is NULL!!!\n"));
11084     return;
11085   }
11086 
11087   TargetDeviceData = smSMPRequestBody->smDeviceData;
11088   if (oneDeviceData != TargetDeviceData)
11089   {
11090     SM_DBG1(("smPhyControlRespRcvd: oneDeviceData != TargetDeviceData!!!\n"));
11091     return;
11092   }
11093 
11094   agDevHandle = TargetDeviceData->agDevHandle;
11095 
11096 
11097   if (frameHeader->smpFunctionResult == SMP_FUNCTION_ACCEPTED)
11098   {
11099     SM_DBG2(("smPhyControlRespRcvd: SMP success\n"));
11100     SM_DBG1(("smPhyControlRespRcvd: callback to TD layer with success\n"));
11101     TargetDeviceData->satDriveState = SAT_DEV_STATE_NORMAL;
11102     saSetDeviceState(agRoot, agNULL, tdsmRotateQnumber(smRoot, smDeviceHandle), agDevHandle, SA_DS_OPERATIONAL);
11103 
11104     tdsmEventCB(smRoot,
11105                 smDeviceHandle,
11106                 smIntrEventTypeTaskManagement,
11107                 smTMOK,
11108                 CurrentTaskTag);
11109   }
11110   else
11111   {
11112     SM_DBG1(("smPhyControlRespRcvd: SMP failure; result %d!!!\n", frameHeader->smpFunctionResult));
11113     tdsmEventCB(smRoot,
11114                 smDeviceHandle,
11115                 smIntrEventTypeTaskManagement,
11116                 smTMFailed,
11117                 CurrentTaskTag);
11118   }
11119   return;
11120 }
11121 
11122 osGLOBAL void
11123 smsatCheckPowerModeCB(
11124                       agsaRoot_t        *agRoot,
11125                       agsaIORequest_t   *agIORequest,
11126                       bit32             agIOStatus,
11127                       agsaFisHeader_t   *agFirstDword,
11128                       bit32             agIOInfoLen,
11129                       agsaFrameHandle_t agFrameHandle,
11130                       void              *ioContext
11131                      )
11132 {
11133   /* callback for satDeResetDevice */
11134 //  tdsaRootOsData_t        *osData = (tdsaRootOsData_t *)agRoot->osData;
11135 //  tiRoot_t                *tiRoot = (tiRoot_t *)osData->tiRoot;
11136 //  tdsaRoot_t              *tdsaRoot        = (tdsaRoot_t *) tiRoot->tdData;
11137 //  tdsaContext_t           *tdsaAllShared = (tdsaContext_t *)&tdsaRoot->tdsaAllShared;
11138   smRoot_t                 *smRoot = agNULL;
11139   smIntRoot_t              *smIntRoot = agNULL;
11140   smIntContext_t           *smAllShared = agNULL;
11141   smIORequestBody_t        *smIORequestBody;
11142   smSatIOContext_t         *satIOContext;
11143   smSatIOContext_t         *satOrgIOContext;
11144   smSatInternalIo_t        *satIntIo;
11145 //  satDeviceData_t         *satDevData;
11146   smDeviceData_t           *oneDeviceData;
11147 #ifdef  TD_DEBUG_ENABLE
11148   bit32                     ataStatus = 0;
11149   bit32                     ataError;
11150   agsaFisPioSetupHeader_t  *satPIOSetupHeader = agNULL;
11151 #endif
11152   bit32                     AbortTM = agFALSE;
11153   smDeviceHandle_t         *smDeviceHandle;
11154 
11155   SM_DBG1(("smsatCheckPowerModeCB: start\n"));
11156   smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
11157   satIOContext           = (smSatIOContext_t *) ioContext;
11158   satIntIo               = satIOContext->satIntIoContext;
11159   oneDeviceData          = satIOContext->pSatDevData;
11160   smRoot                 = oneDeviceData->smRoot;
11161   smIntRoot              = (smIntRoot_t *)smRoot->smData;
11162   smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
11163   smDeviceHandle         = oneDeviceData->smDevHandle;
11164   if (satIntIo == agNULL)
11165   {
11166     SM_DBG6(("smsatCheckPowerModeCB: External, OS generated\n"));
11167     satOrgIOContext      = satIOContext;
11168   }
11169   else
11170   {
11171     SM_DBG6(("smsatCheckPowerModeCB: Internal, TD generated\n"));
11172     satOrgIOContext        = satIOContext->satOrgIOContext;
11173     if (satOrgIOContext == agNULL)
11174     {
11175       SM_DBG6(("smsatCheckPowerModeCB: satOrgIOContext is NULL, wrong\n"));
11176       return;
11177     }
11178     else
11179     {
11180       SM_DBG6(("smsatCheckPowerModeCB: satOrgIOContext is NOT NULL\n"));
11181     }
11182   }
11183   smIORequestBody->ioCompleted = agTRUE;
11184   smIORequestBody->ioStarted = agFALSE;
11185   if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
11186   {
11187     SM_DBG1(("smsatCheckPowerModeCB: wrong. agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
11188     /* TM completed */
11189     tdsmEventCB( smRoot,
11190                  smDeviceHandle,
11191                  smIntrEventTypeTaskManagement,
11192                  smTMFailed,
11193                  oneDeviceData->satTmTaskTag);
11194 
11195     oneDeviceData->satTmTaskTag = agNULL;
11196     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
11197 
11198     smsatFreeIntIoResource( smRoot,
11199                             oneDeviceData,
11200                             satIntIo);
11201     return;
11202   }
11203 
11204   if (agIOStatus == OSSA_IO_OPEN_CNX_ERROR_PROTOCOL_NOT_SUPPORTED ||
11205       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_ZONE_VIOLATION ||
11206       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_BREAK ||
11207       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS ||
11208       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_BAD_DESTINATION ||
11209       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_CONNECTION_RATE_NOT_SUPPORTED ||
11210       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_WRONG_DESTINATION ||
11211       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_UNKNOWN_ERROR ||
11212       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_STP_RESOURCES_BUSY
11213       )
11214   {
11215     SM_DBG1(("smsatCheckPowerModeCB: OSSA_IO_OPEN_CNX_ERROR!!!\n"));
11216     /* TM completed */
11217     tdsmEventCB( smRoot,
11218                  smDeviceHandle,
11219                  smIntrEventTypeTaskManagement,
11220                  smTMFailed,
11221                  oneDeviceData->satTmTaskTag);
11222     oneDeviceData->satTmTaskTag = agNULL;
11223 
11224     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
11225     smsatFreeIntIoResource( smRoot,
11226                             oneDeviceData,
11227                             satIntIo);
11228     return;
11229   }
11230  if (agIOStatus != OSSA_IO_SUCCESS)
11231   {
11232     /* only agsaFisPioSetup_t is expected */
11233 #ifdef  TD_DEBUG_ENABLE
11234     satPIOSetupHeader = (agsaFisPioSetupHeader_t *)&(agFirstDword->PioSetup);
11235     ataStatus     = satPIOSetupHeader->status;   /* ATA Status register */
11236     ataError      = satPIOSetupHeader->error;    /* ATA Eror register   */
11237 #endif
11238     SM_DBG1(("smsatCheckPowerModeCB: ataStatus 0x%x ataError 0x%x!!!\n", ataStatus, ataError));
11239     /* TM completed */
11240     tdsmEventCB( smRoot,
11241                  smDeviceHandle,
11242                  smIntrEventTypeTaskManagement,
11243                  smTMFailed,
11244                  oneDeviceData->satTmTaskTag);
11245     oneDeviceData->satTmTaskTag = agNULL;
11246     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
11247     smsatFreeIntIoResource( smRoot,
11248                             oneDeviceData,
11249                             satIntIo);
11250     return;
11251   }
11252   /* success */
11253   SM_DBG1(("smsatCheckPowerModeCB: success!!!\n"));
11254   SM_DBG1(("smsatCheckPowerModeCB: TMF %d!!!\n", satOrgIOContext->TMF));
11255 
11256   if (satOrgIOContext->TMF == AG_ABORT_TASK)
11257   {
11258     AbortTM = agTRUE;
11259   }
11260   if (AbortTM == agTRUE)
11261   {
11262     SM_DBG1(("smsatCheckPowerModeCB: calling local satAbort!!!\n"));
11263     smsatAbort(smRoot, agRoot, satOrgIOContext->satToBeAbortedIOContext);
11264   }
11265   oneDeviceData->satTmTaskTag = agNULL;
11266   oneDeviceData->satDriveState = SAT_DEV_STATE_NORMAL;
11267 
11268   smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
11269 
11270   SM_DBG1(("smsatCheckPowerModeCB: satPendingIO %d satNCQMaxIO %d!!!\n", oneDeviceData->satPendingIO, oneDeviceData->satNCQMaxIO ));
11271   SM_DBG1(("smsatCheckPowerModeCB: satPendingNCQIO %d satPendingNONNCQIO %d!!!\n", oneDeviceData->satPendingNCQIO, oneDeviceData->satPendingNONNCQIO));
11272   smsatFreeIntIoResource( smRoot,
11273                           oneDeviceData,
11274                           satIntIo);
11275 
11276   /* TM completed */
11277   tdsmEventCB( smRoot,
11278                smDeviceHandle,
11279                smIntrEventTypeTaskManagement,
11280                smTMOK,
11281                oneDeviceData->satTmTaskTag);
11282   SM_DBG5(("smsatCheckPowerModeCB: device %p pending IO %d\n", oneDeviceData, oneDeviceData->satPendingIO));
11283   SM_DBG2(("smsatCheckPowerModeCB: end\n"));
11284   return;
11285 }
11286 
11287 osGLOBAL void
11288 smsatCheckPowerModePassCB(
11289                       agsaRoot_t        *agRoot,
11290                       agsaIORequest_t   *agIORequest,
11291                       bit32             agIOStatus,
11292                       agsaFisHeader_t   *agFirstDword,
11293                       bit32             agIOInfoLen,
11294                       agsaFrameHandle_t agFrameHandle,
11295                       void              *ioContext
11296                      )
11297 
11298 {
11299 
11300   smRoot_t                 *smRoot = agNULL;
11301   smIntRoot_t              *smIntRoot = agNULL;
11302   smIntContext_t           *smAllShared = agNULL;
11303   smIORequestBody_t        *smIORequestBody;
11304   smSatIOContext_t         *satIOContext;
11305   smSatIOContext_t         *satOrgIOContext;
11306   smSatInternalIo_t        *satIntIo;
11307   smIORequest_t             *smOrgIORequest;
11308   smIORequestBody_t         *smOrgIORequestBody;
11309 //  satDeviceData_t         *satDevData;
11310   smDeviceData_t           *oneDeviceData;
11311 #ifdef  TD_DEBUG_ENABLE
11312   bit32                     ataStatus = 0;
11313   bit32                     ataError;
11314   agsaFisPioSetupHeader_t  *satPIOSetupHeader = agNULL;
11315 #endif
11316 
11317   smScsiRspSense_t			*pSense;
11318   bit8						bSenseKey = 0;
11319   bit16 					bSenseCodeInfo = 0;
11320 
11321   SM_DBG1(("smsatCheckPowerModePassCB: start\n"));
11322   smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
11323   satIOContext           = (smSatIOContext_t *) ioContext;
11324   satIntIo               = satIOContext->satIntIoContext;
11325   oneDeviceData          = satIOContext->pSatDevData;
11326   smRoot                 = oneDeviceData->smRoot;
11327   smIntRoot              = (smIntRoot_t *)smRoot->smData;
11328   smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
11329 
11330   if (satIntIo == agNULL)
11331   {
11332     SM_DBG6(("smsatCheckPowerModePassCB: External, OS generated\n"));
11333     satOrgIOContext      = satIOContext;
11334     smOrgIORequest  = smIORequestBody->smIORequest;
11335     pSense          = satOrgIOContext->pSense;
11336   }
11337   else
11338   {
11339     SM_DBG6(("smsatCheckPowerModePassCB: Internal, TD generated\n"));
11340     satOrgIOContext        = satIOContext->satOrgIOContext;
11341     smOrgIORequestBody     = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
11342     smOrgIORequest         = (smIORequest_t *)smOrgIORequestBody->smIORequest;
11343     pSense          = satOrgIOContext->pSense;
11344     if (satOrgIOContext == agNULL)
11345     {
11346       SM_DBG6(("smsatCheckPowerModePassCB: satOrgIOContext is NULL, wrong\n"));
11347       return;
11348     }
11349     else
11350     {
11351       SM_DBG6(("smsatCheckPowerModePassCB: satOrgIOContext is NOT NULL\n"));
11352     }
11353   }
11354   smIORequestBody->ioCompleted = agTRUE;
11355   smIORequestBody->ioStarted = agFALSE;
11356   if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
11357   {
11358     SM_DBG1(("smsatCheckPowerModePassCB: wrong. agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
11359 
11360     tdsmIOCompletedCB(
11361                        smRoot,
11362                        smOrgIORequest,
11363                        smIOFailed,
11364                        smDetailOtherError,
11365                        agNULL,
11366                        satOrgIOContext->interruptContext
11367                       );
11368     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
11369 
11370     smsatFreeIntIoResource( smRoot,
11371                             oneDeviceData,
11372                             satIntIo);
11373     return;
11374 
11375   }
11376 
11377   if (agIOStatus != OSSA_IO_SUCCESS)
11378   {
11379     /* only agsaFisPioSetup_t is expected */
11380 #ifdef  TD_DEBUG_ENABLE
11381     satPIOSetupHeader = (agsaFisPioSetupHeader_t *)&(agFirstDword->PioSetup);
11382     ataStatus     = satPIOSetupHeader->status;   /* ATA Status register */
11383     ataError      = satPIOSetupHeader->error;    /* ATA Eror register   */
11384 #endif
11385     SM_DBG1(("smsatCheckPowerModePassCB: ataStatus 0x%x ataError 0x%x!!!\n", ataStatus, ataError));
11386 
11387 
11388     if (agIOStatus == OSSA_IO_ABORTED)
11389     {
11390       smsatProcessAbort(smRoot,
11391                         smOrgIORequest,
11392                         satOrgIOContext
11393                         );
11394 
11395       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
11396 
11397       smsatFreeIntIoResource( smRoot,
11398 			      oneDeviceData,
11399 			      satIntIo);
11400       return;
11401     }
11402     smsatTranslateATAErrorsToSCSIErrors(
11403                                         agFirstDword->D2H.status,
11404                                         agFirstDword->D2H.error,
11405                                         &bSenseKey,
11406                                         &bSenseCodeInfo
11407                                         );
11408     smsatSetSensePayload(pSense, bSenseKey, 0, bSenseCodeInfo, satOrgIOContext);
11409     tdsmIOCompletedCB(smRoot,
11410                       smOrgIORequest,
11411                       smIOSuccess,
11412                       SCSI_STAT_CHECK_CONDITION,
11413                       satOrgIOContext->pSmSenseData,
11414                       satOrgIOContext->interruptContext );
11415 
11416 
11417     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
11418 
11419     smsatFreeIntIoResource( smRoot,
11420                             oneDeviceData,
11421                             satIntIo);
11422     return;
11423 
11424   }
11425   /* success */
11426   SM_DBG1(("smsatCheckPowerModePassCB: success!!!\n"));
11427 
11428   tdsmIOCompletedCB( smRoot,
11429                      smOrgIORequest,
11430                      smIOSuccess,
11431                      SCSI_STAT_GOOD,
11432                      agNULL,
11433                      satOrgIOContext->interruptContext);
11434 
11435 
11436   smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
11437 
11438   smsatFreeIntIoResource( smRoot,
11439                           oneDeviceData,
11440                           satIntIo);
11441 
11442 
11443   return;
11444 }
11445 
11446 osGLOBAL void
11447 smsatIDDataPassCB(
11448                   agsaRoot_t        *agRoot,
11449                   agsaIORequest_t   *agIORequest,
11450                   bit32             agIOStatus,
11451                   agsaFisHeader_t   *agFirstDword,
11452                   bit32             agIOInfoLen,
11453                   agsaFrameHandle_t agFrameHandle,
11454                   void              *ioContext
11455                  )
11456 {
11457   smRoot_t                 *smRoot = agNULL;
11458   smIntRoot_t              *smIntRoot = agNULL;
11459   smIntContext_t           *smAllShared = agNULL;
11460   smIORequestBody_t        *smIORequestBody;
11461   smSatIOContext_t         *satIOContext;
11462   smSatIOContext_t         *satOrgIOContext;
11463   smSatInternalIo_t        *satIntIo;
11464   smIORequest_t             *smOrgIORequest;
11465   smIORequestBody_t         *smOrgIORequestBody;
11466 //  satDeviceData_t         *satDevData;
11467   smDeviceData_t           *oneDeviceData;
11468 #ifdef  TD_DEBUG_ENABLE
11469   bit32                     ataStatus = 0;
11470   bit32                     ataError;
11471   agsaFisPioSetupHeader_t  *satPIOSetupHeader = agNULL;
11472 #endif
11473 
11474   smScsiRspSense_t			*pSense;
11475   bit8						bSenseKey = 0;
11476   bit16 					bSenseCodeInfo = 0;
11477 
11478   SM_DBG3(("smsatIDDataPassCB: start\n"));
11479   smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
11480   satIOContext           = (smSatIOContext_t *) ioContext;
11481   satIntIo               = satIOContext->satIntIoContext;
11482   oneDeviceData          = satIOContext->pSatDevData;
11483   smRoot                 = oneDeviceData->smRoot;
11484   smIntRoot              = (smIntRoot_t *)smRoot->smData;
11485   smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
11486 
11487   if (satIntIo == agNULL)
11488   {
11489     SM_DBG6(("smsatIDDataPassCB: External, OS generated\n"));
11490     satOrgIOContext      = satIOContext;
11491     smOrgIORequest  = smIORequestBody->smIORequest;
11492     pSense          = satOrgIOContext->pSense;
11493   }
11494   else
11495   {
11496     SM_DBG6(("smsatIDDataPassCB: Internal, TD generated\n"));
11497     satOrgIOContext        = satIOContext->satOrgIOContext;
11498     smOrgIORequestBody     = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
11499     smOrgIORequest         = (smIORequest_t *)smOrgIORequestBody->smIORequest;
11500     pSense          = satOrgIOContext->pSense;
11501     if (satOrgIOContext == agNULL)
11502     {
11503       SM_DBG6(("smsatIDDataPassCB: satOrgIOContext is NULL, wrong\n"));
11504       return;
11505     }
11506     else
11507     {
11508       SM_DBG6(("smsatIDDataPassCB: satOrgIOContext is NOT NULL\n"));
11509     }
11510   }
11511   smIORequestBody->ioCompleted = agTRUE;
11512   smIORequestBody->ioStarted = agFALSE;
11513   if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
11514   {
11515     SM_DBG1(("smsatIDDataPassCB: wrong. agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
11516 
11517     tdsmIOCompletedCB(
11518                        smRoot,
11519                        smOrgIORequest,
11520                        smIOFailed,
11521                        smDetailOtherError,
11522                        agNULL,
11523                        satOrgIOContext->interruptContext
11524                       );
11525     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
11526 
11527     smsatFreeIntIoResource( smRoot,
11528                             oneDeviceData,
11529                             satIntIo);
11530     return;
11531 
11532   }
11533 
11534   if (agIOStatus != OSSA_IO_SUCCESS)
11535   {
11536     /* only agsaFisPioSetup_t is expected */
11537 #ifdef  TD_DEBUG_ENABLE
11538     satPIOSetupHeader = (agsaFisPioSetupHeader_t *)&(agFirstDword->PioSetup);
11539     ataStatus     = satPIOSetupHeader->status;   /* ATA Status register */
11540     ataError      = satPIOSetupHeader->error;    /* ATA Eror register   */
11541 #endif
11542     SM_DBG1(("smsatIDDataPassCB: ataStatus 0x%x ataError 0x%x!!!\n", ataStatus, ataError));
11543 
11544 
11545     if (agIOStatus == OSSA_IO_ABORTED)
11546     {
11547       smsatProcessAbort(smRoot,
11548                         smOrgIORequest,
11549                         satOrgIOContext
11550                         );
11551 
11552       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
11553 
11554       smsatFreeIntIoResource( smRoot,
11555 			      oneDeviceData,
11556 			      satIntIo);
11557       return;
11558     }
11559     smsatTranslateATAErrorsToSCSIErrors(
11560                                         agFirstDword->D2H.status,
11561                                         agFirstDword->D2H.error,
11562                                         &bSenseKey,
11563                                         &bSenseCodeInfo
11564                                         );
11565     smsatSetSensePayload(pSense, bSenseKey, 0, bSenseCodeInfo, satOrgIOContext);
11566     tdsmIOCompletedCB(smRoot,
11567                       smOrgIORequest,
11568                       smIOSuccess,
11569                       SCSI_STAT_CHECK_CONDITION,
11570                       satOrgIOContext->pSmSenseData,
11571                       satOrgIOContext->interruptContext );
11572 
11573 
11574     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
11575 
11576     smsatFreeIntIoResource( smRoot,
11577                             oneDeviceData,
11578                             satIntIo);
11579     return;
11580 
11581   }
11582   /* success */
11583   SM_DBG3(("smsatIDDataPassCB: success!!!\n"));
11584 
11585   SM_DBG3(("smsatIDDataPassCB: extend 0x%x ck_cond 0x%x sectorCnt07 0x%x\n", satOrgIOContext->extend,
11586   satIOContext->ck_cond, satOrgIOContext->sectorCnt07));
11587   SM_DBG3(("smsatIDDataPassCB: LBAHigh07 0x%x LBAMid07 0x%x LBALow07 0x%x\n", satOrgIOContext->LBAHigh07,
11588   satOrgIOContext->LBAMid07, satOrgIOContext->LBALow07));
11589 
11590   if (satIOContext->ck_cond)
11591   {
11592     smsatSetSensePayload( pSense,
11593                           SCSI_SNSKEY_RECOVERED_ERROR,
11594                           satOrgIOContext->sectorCnt07,
11595                           SCSI_SNSCODE_ATA_PASS_THROUGH_INFORMATION_AVAILABLE,
11596                           satIOContext);
11597 
11598     tdsmIOCompletedCB( smRoot,
11599                        smOrgIORequest,
11600                        smIOSuccess,
11601                        SCSI_STAT_CHECK_CONDITION,
11602                        satIOContext->pSmSenseData,
11603                        satOrgIOContext->interruptContext );
11604   }
11605   else
11606   {
11607     tdsmIOCompletedCB( smRoot,
11608                        smOrgIORequest,
11609                        smIOSuccess,
11610                        SCSI_STAT_GOOD,
11611                        agNULL,
11612                        satOrgIOContext->interruptContext);
11613   }
11614 
11615   smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
11616 
11617   smsatFreeIntIoResource( smRoot,
11618                           oneDeviceData,
11619                           satIntIo);
11620 
11621 
11622   return;
11623 }
11624 
11625 osGLOBAL void
11626 smsatResetDeviceCB(
11627                     agsaRoot_t        *agRoot,
11628                     agsaIORequest_t   *agIORequest,
11629                     bit32             agIOStatus,
11630                     agsaFisHeader_t   *agFirstDword,
11631                     bit32             agIOInfoLen,
11632                     agsaFrameHandle_t agFrameHandle,
11633                     void              *ioContext
11634                   )
11635 {
11636   /* callback for satResetDevice */
11637 //  tdsaRootOsData_t        *osData = (tdsaRootOsData_t *)agRoot->osData;
11638 //  tiRoot_t                *tiRoot = (tiRoot_t *)osData->tiRoot;
11639 //  tdsaRoot_t              *tdsaRoot        = (tdsaRoot_t *) tiRoot->tdData;
11640 //  tdsaContext_t           *tdsaAllShared = (tdsaContext_t *)&tdsaRoot->tdsaAllShared;
11641   smRoot_t                  *smRoot = agNULL;
11642   smIntRoot_t               *smIntRoot = agNULL;
11643   smIntContext_t            *smAllShared = agNULL;
11644   smIORequestBody_t         *smIORequestBody;
11645   smIORequestBody_t         *smOrgIORequestBody;
11646   smSatIOContext_t          *satIOContext;
11647   smSatIOContext_t          *satOrgIOContext;
11648   smSatIOContext_t          *satNewIOContext;
11649   smSatInternalIo_t         *satIntIo;
11650   smSatInternalIo_t         *satNewIntIo = agNULL;
11651 //  satDeviceData_t         *satDevData;
11652   smDeviceData_t            *oneDeviceData;
11653   smIORequest_t             *smOrgIORequest;
11654 #ifdef  TD_DEBUG_ENABLE
11655   bit32                      ataStatus = 0;
11656   bit32                      ataError;
11657   agsaFisPioSetupHeader_t   *satPIOSetupHeader = agNULL;
11658 #endif
11659   bit32                      status;
11660   smDeviceHandle_t          *smDeviceHandle;
11661 
11662   SM_DBG1(("smsatResetDeviceCB: start\n"));
11663   smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
11664   satIOContext           = (smSatIOContext_t *) ioContext;
11665   satIntIo               = satIOContext->satIntIoContext;
11666   oneDeviceData          = satIOContext->pSatDevData;
11667   smRoot                 = oneDeviceData->smRoot;
11668   smIntRoot              = (smIntRoot_t *)smRoot->smData;
11669   smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
11670   smDeviceHandle         = oneDeviceData->smDevHandle;
11671 
11672   if (satIntIo == agNULL)
11673   {
11674     SM_DBG6(("smsatResetDeviceCB: External, OS generated\n"));
11675     satOrgIOContext      = satIOContext;
11676     smOrgIORequest       = smIORequestBody->smIORequest;
11677   }
11678   else
11679   {
11680     SM_DBG6(("smsatResetDeviceCB: Internal, TD generated\n"));
11681     satOrgIOContext        = satIOContext->satOrgIOContext;
11682     if (satOrgIOContext == agNULL)
11683     {
11684       SM_DBG6(("smsatResetDeviceCB: satOrgIOContext is NULL, wrong\n"));
11685       return;
11686     }
11687     else
11688     {
11689       SM_DBG6(("smsatResetDeviceCB: satOrgIOContext is NOT NULL\n"));
11690     }
11691     smOrgIORequestBody     = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
11692     smOrgIORequest         = (smIORequest_t *)smOrgIORequestBody->smIORequest;
11693   }
11694   smIORequestBody->ioCompleted = agTRUE;
11695   smIORequestBody->ioStarted = agFALSE;
11696   if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
11697   {
11698     SM_DBG1(("smsatResetDeviceCB: wrong. agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
11699     /* TM completed */
11700     tdsmEventCB( smRoot,
11701                  smDeviceHandle,
11702                  smIntrEventTypeTaskManagement,
11703                  smTMFailed,
11704                  oneDeviceData->satTmTaskTag);
11705     oneDeviceData->satTmTaskTag = agNULL;
11706     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
11707     smsatFreeIntIoResource( smRoot,
11708                             oneDeviceData,
11709                             satIntIo);
11710     return;
11711   }
11712   if (agIOStatus == OSSA_IO_OPEN_CNX_ERROR_PROTOCOL_NOT_SUPPORTED ||
11713       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_ZONE_VIOLATION ||
11714       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_BREAK ||
11715       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS ||
11716       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_BAD_DESTINATION ||
11717       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_CONNECTION_RATE_NOT_SUPPORTED ||
11718       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_WRONG_DESTINATION ||
11719       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_UNKNOWN_ERROR ||
11720       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_STP_RESOURCES_BUSY
11721       )
11722   {
11723     SM_DBG1(("smsatResetDeviceCB: OSSA_IO_OPEN_CNX_ERROR!!!\n"));
11724     /* TM completed */
11725     tdsmEventCB( smRoot,
11726                  smDeviceHandle,
11727                  smIntrEventTypeTaskManagement,
11728                  smTMFailed,
11729                  oneDeviceData->satTmTaskTag);
11730 
11731     oneDeviceData->satTmTaskTag = agNULL;
11732     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
11733     smsatFreeIntIoResource( smRoot,
11734                             oneDeviceData,
11735                             satIntIo);
11736     return;
11737   }
11738   if (agIOStatus != OSSA_IO_SUCCESS)
11739   {
11740     /* only agsaFisPioSetup_t is expected */
11741 #ifdef  TD_DEBUG_ENABLE
11742     satPIOSetupHeader = (agsaFisPioSetupHeader_t *)&(agFirstDword->PioSetup);
11743     ataStatus     = satPIOSetupHeader->status;   /* ATA Status register */
11744     ataError      = satPIOSetupHeader->error;    /* ATA Eror register   */
11745 #endif
11746     SM_DBG1(("smsatResetDeviceCB: ataStatus 0x%x ataError 0x%x!!!\n", ataStatus, ataError));
11747     /* TM completed */
11748     tdsmEventCB( smRoot,
11749                  smDeviceHandle,
11750                  smIntrEventTypeTaskManagement,
11751                  smTMFailed,
11752                  oneDeviceData->satTmTaskTag);
11753 
11754     oneDeviceData->satTmTaskTag = agNULL;
11755     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
11756     smsatFreeIntIoResource( smRoot,
11757                             oneDeviceData,
11758                             satIntIo);
11759     return;
11760   }
11761 
11762   /* success */
11763   satNewIntIo = smsatAllocIntIoResource( smRoot,
11764                                          smOrgIORequest,
11765                                          oneDeviceData,
11766                                          0,
11767                                          satNewIntIo);
11768   if (satNewIntIo == agNULL)
11769   {
11770     oneDeviceData->satTmTaskTag = agNULL;
11771 
11772     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
11773     /* memory allocation failure */
11774     smsatFreeIntIoResource( smRoot,
11775                             oneDeviceData,
11776                             satIntIo);
11777     /* TM completed */
11778     tdsmEventCB( smRoot,
11779                  smDeviceHandle,
11780                  smIntrEventTypeTaskManagement,
11781                  smTMFailed,
11782                  oneDeviceData->satTmTaskTag);
11783     SM_DBG1(("smsatResetDeviceCB: momory allocation fails!!!\n"));
11784     return;
11785   } /* end of memory allocation failure */
11786     /*
11787      * Need to initialize all the fields within satIOContext
11788      */
11789     satNewIOContext = smsatPrepareNewIO(
11790                                          satNewIntIo,
11791                                          smOrgIORequest,
11792                                          oneDeviceData,
11793                                          agNULL,
11794                                          satOrgIOContext
11795                                         );
11796     /* send AGSA_SATA_PROTOCOL_SRST_DEASSERT */
11797     status = smsatDeResetDevice(smRoot,
11798                                 smOrgIORequest,
11799                                 satOrgIOContext->psmDeviceHandle,
11800                                 agNULL,
11801                                 satNewIOContext
11802                                );
11803     if (status != SM_RC_SUCCESS)
11804     {
11805       /* TM completed */
11806       tdsmEventCB( smRoot,
11807                    smDeviceHandle,
11808                    smIntrEventTypeTaskManagement,
11809                    smTMFailed,
11810                    oneDeviceData->satTmTaskTag);
11811       /* sending AGSA_SATA_PROTOCOL_SRST_DEASSERT fails */
11812       oneDeviceData->satTmTaskTag = agNULL;
11813 
11814       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
11815       smsatFreeIntIoResource( smRoot,
11816                               oneDeviceData,
11817                               satIntIo);
11818 
11819       smsatFreeIntIoResource( smRoot,
11820                               oneDeviceData,
11821                               satNewIntIo);
11822       return;
11823     }
11824 //  oneDeviceData->satTmTaskTag = agNULL;
11825 
11826   smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
11827 
11828   smsatFreeIntIoResource( smRoot,
11829                           oneDeviceData,
11830                           satIntIo);
11831   SM_DBG5(("smsatResetDeviceCB: device %p pending IO %d\n", oneDeviceData, oneDeviceData->satPendingIO));
11832   SM_DBG6(("smsatResetDeviceCB: end\n"));
11833   return;
11834 }
11835 
11836 osGLOBAL void
11837 smsatDeResetDeviceCB(
11838                       agsaRoot_t        *agRoot,
11839                       agsaIORequest_t   *agIORequest,
11840                       bit32             agIOStatus,
11841                       agsaFisHeader_t   *agFirstDword,
11842                       bit32             agIOInfoLen,
11843                       agsaFrameHandle_t agFrameHandle,
11844                       void              *ioContext
11845                    )
11846 {
11847   /* callback for satDeResetDevice */
11848 //  tdsaRootOsData_t        *osData = (tdsaRootOsData_t *)agRoot->osData;
11849 //  tiRoot_t                *tiRoot = (tiRoot_t *)osData->tiRoot;
11850 //  tdsaRoot_t              *tdsaRoot        = (tdsaRoot_t *) tiRoot->tdData;
11851 //  tdsaContext_t           *tdsaAllShared = (tdsaContext_t *)&tdsaRoot->tdsaAllShared;
11852   smRoot_t                  *smRoot = agNULL;
11853   smIntRoot_t               *smIntRoot = agNULL;
11854   smIntContext_t            *smAllShared = agNULL;
11855   smIORequestBody_t         *smIORequestBody;
11856   smSatIOContext_t          *satIOContext;
11857   smSatIOContext_t          *satOrgIOContext;
11858   smSatInternalIo_t         *satIntIo;
11859 //  satDeviceData_t           *satDevData;
11860   smDeviceData_t            *oneDeviceData;
11861 #ifdef  TD_DEBUG_ENABLE
11862   bit32                      ataStatus = 0;
11863   bit32                      ataError;
11864   agsaFisPioSetupHeader_t   *satPIOSetupHeader = agNULL;
11865 #endif
11866   bit32                      AbortTM = agFALSE;
11867   smDeviceHandle_t          *smDeviceHandle;
11868 
11869   SM_DBG1(("smsatDeResetDeviceCB: start!!!\n"));
11870   smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
11871   satIOContext           = (smSatIOContext_t *) ioContext;
11872   satIntIo               = satIOContext->satIntIoContext;
11873   oneDeviceData          = satIOContext->pSatDevData;
11874   smRoot                 = oneDeviceData->smRoot;
11875   smIntRoot              = (smIntRoot_t *)smRoot->smData;
11876   smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
11877   smDeviceHandle         = oneDeviceData->smDevHandle;
11878   if (satIntIo == agNULL)
11879   {
11880     SM_DBG6(("smsatDeResetDeviceCB: External, OS generated\n"));
11881     satOrgIOContext      = satIOContext;
11882   }
11883   else
11884   {
11885     SM_DBG6(("smsatDeResetDeviceCB: Internal, TD generated\n"));
11886     satOrgIOContext        = satIOContext->satOrgIOContext;
11887     if (satOrgIOContext == agNULL)
11888     {
11889       SM_DBG6(("smsatDeResetDeviceCB: satOrgIOContext is NULL, wrong\n"));
11890       return;
11891     }
11892     else
11893     {
11894       SM_DBG6(("smsatDeResetDeviceCB: satOrgIOContext is NOT NULL\n"));
11895     }
11896   }
11897   smIORequestBody->ioCompleted = agTRUE;
11898   smIORequestBody->ioStarted = agFALSE;
11899   if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
11900   {
11901     SM_DBG1(("smsatDeResetDeviceCB: wrong. agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
11902     /* TM completed */
11903     tdsmEventCB( smRoot,
11904                  smDeviceHandle,
11905                  smIntrEventTypeTaskManagement,
11906                  smTMFailed,
11907                  oneDeviceData->satTmTaskTag);
11908     oneDeviceData->satTmTaskTag = agNULL;
11909     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
11910     smsatFreeIntIoResource( smRoot,
11911                             oneDeviceData,
11912                             satIntIo);
11913     return;
11914   }
11915 
11916   if (agIOStatus == OSSA_IO_OPEN_CNX_ERROR_PROTOCOL_NOT_SUPPORTED ||
11917       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_ZONE_VIOLATION ||
11918       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_BREAK ||
11919       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS ||
11920       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_BAD_DESTINATION ||
11921       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_CONNECTION_RATE_NOT_SUPPORTED ||
11922       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_WRONG_DESTINATION ||
11923       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_UNKNOWN_ERROR ||
11924       agIOStatus == OSSA_IO_OPEN_CNX_ERROR_STP_RESOURCES_BUSY
11925       )
11926   {
11927     SM_DBG1(("smsatDeResetDeviceCB: OSSA_IO_OPEN_CNX_ERROR!!!\n"));
11928 
11929     /* TM completed */
11930     tdsmEventCB( smRoot,
11931                  smDeviceHandle,
11932                  smIntrEventTypeTaskManagement,
11933                  smTMFailed,
11934                  oneDeviceData->satTmTaskTag);
11935     oneDeviceData->satTmTaskTag = agNULL;
11936     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
11937     smsatFreeIntIoResource( smRoot,
11938                             oneDeviceData,
11939                             satIntIo);
11940     return;
11941   }
11942  if (agIOStatus != OSSA_IO_SUCCESS)
11943   {
11944     /* only agsaFisPioSetup_t is expected */
11945 #ifdef  TD_DEBUG_ENABLE
11946     satPIOSetupHeader = (agsaFisPioSetupHeader_t *)&(agFirstDword->PioSetup);
11947     ataStatus     = satPIOSetupHeader->status;   /* ATA Status register */
11948     ataError      = satPIOSetupHeader->error;    /* ATA Eror register   */
11949 #endif
11950     SM_DBG1(("smsatDeResetDeviceCB: ataStatus 0x%x ataError 0x%x!!!\n", ataStatus, ataError));
11951     /* TM completed */
11952     tdsmEventCB( smRoot,
11953                  smDeviceHandle,
11954                  smIntrEventTypeTaskManagement,
11955                  smTMFailed,
11956                  oneDeviceData->satTmTaskTag);
11957     oneDeviceData->satTmTaskTag = agNULL;
11958     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
11959     smsatFreeIntIoResource( smRoot,
11960                             oneDeviceData,
11961                             satIntIo);
11962     return;
11963   }
11964 
11965   /* success */
11966   SM_DBG1(("smsatDeResetDeviceCB: success !!!\n"));
11967   SM_DBG1(("smsatDeResetDeviceCB: TMF %d!!!\n", satOrgIOContext->TMF));
11968 
11969   if (satOrgIOContext->TMF == AG_ABORT_TASK)
11970   {
11971     AbortTM = agTRUE;
11972   }
11973   if (AbortTM == agTRUE)
11974   {
11975     SM_DBG1(("smsatDeResetDeviceCB: calling satAbort!!!\n"));
11976     smsatAbort(smRoot, agRoot, satOrgIOContext->satToBeAbortedIOContext);
11977   }
11978   oneDeviceData->satTmTaskTag = agNULL;
11979   oneDeviceData->satDriveState = SAT_DEV_STATE_NORMAL;
11980   smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
11981 
11982   SM_DBG1(("smsatDeResetDeviceCB: satPendingIO %d satNCQMaxIO %d!!!\n", oneDeviceData->satPendingIO, oneDeviceData->satNCQMaxIO ));
11983   SM_DBG1(("smsatDeResetDeviceCB: satPendingNCQIO %d satPendingNONNCQIO %d!!!\n", oneDeviceData->satPendingNCQIO, oneDeviceData->satPendingNONNCQIO));
11984   smsatFreeIntIoResource( smRoot, oneDeviceData, satIntIo );
11985 
11986   /* TM completed */
11987   tdsmEventCB( smRoot,
11988                smDeviceHandle,
11989                smIntrEventTypeTaskManagement,
11990                smTMOK,
11991                oneDeviceData->satTmTaskTag);
11992   SM_DBG5(("smsatDeResetDeviceCB: device %p pending IO %d\n", oneDeviceData, oneDeviceData->satPendingIO));
11993   SM_DBG6(("smsatDeResetDeviceCB: end\n"));
11994   return;
11995 }
11996 
11997 osGLOBAL void
11998 smaSATAAbortCB(
11999                 agsaRoot_t        *agRoot,
12000                 agsaIORequest_t   *agIORequest,
12001                 bit32             flag,
12002                 bit32             status
12003         )
12004 {
12005   smRoot_t                  *smRoot = gsmRoot;
12006   smIORequestBody_t         *smIORequestBody = agNULL;
12007   smSatIOContext_t          *satIOContext;
12008   smDeviceHandle_t          *smDeviceHandle;
12009   smDeviceData_t            *oneDeviceData = agNULL;
12010 
12011   SM_DBG1(("smaSATAAbortCB: start\n"));
12012 
12013   smIORequestBody = (smIORequestBody_t *)agIORequest->osData;
12014   if (smIORequestBody == agNULL)
12015   {
12016     SM_DBG1(("smaSATAAbortCB: smIORequestBody is NULL!!! \n"));
12017     return;
12018   }
12019 
12020   satIOContext = &(smIORequestBody->transport.SATA.satIOContext);
12021   if (satIOContext == agNULL)
12022   {
12023     SM_DBG1(("smaSATAAbortCB: satIOContext is NULL!!! \n"));
12024     if (smIORequestBody->IOType.InitiatorTMIO.osMemHandle != agNULL)
12025     {
12026       tdsmFreeMemory(smRoot,
12027                      smIORequestBody->IOType.InitiatorTMIO.osMemHandle,
12028                      sizeof(smIORequestBody_t)
12029                      );
12030     }
12031     return;
12032   }
12033 
12034   smDeviceHandle = smIORequestBody->smDevHandle;
12035   if (smDeviceHandle == agNULL)
12036   {
12037     SM_DBG1(("smaSATAAbortCB: smDeviceHandle is NULL!!!\n"));
12038     if (smIORequestBody->IOType.InitiatorTMIO.osMemHandle != agNULL)
12039     {
12040       tdsmFreeMemory(smRoot,
12041                      smIORequestBody->IOType.InitiatorTMIO.osMemHandle,
12042                      sizeof(smIORequestBody_t)
12043                      );
12044     }
12045     return;
12046   }
12047 
12048   oneDeviceData = (smDeviceData_t *)smDeviceHandle->smData;
12049   if (oneDeviceData == agNULL)
12050   {
12051     SM_DBG1(("smaSATAAbortCB: oneDeviceData is NULL!!!\n"));
12052     if (smIORequestBody->IOType.InitiatorTMIO.osMemHandle != agNULL)
12053     {
12054       tdsmFreeMemory(smRoot,
12055                      smIORequestBody->IOType.InitiatorTMIO.osMemHandle,
12056                      sizeof(smIORequestBody_t)
12057                      );
12058     }
12059 
12060     return;
12061   }
12062 
12063   if (flag == 2)
12064   {
12065     /* abort per port */
12066     SM_DBG1(("smaSATAAbortCB: abort per port, not yet!!!\n"));
12067   }
12068   else if (flag == 1)
12069   {
12070      SM_DBG1(("smaSATAAbortCB: abort all!!!\n"));
12071     if (oneDeviceData->OSAbortAll == agTRUE)
12072     {
12073       oneDeviceData->OSAbortAll = agFALSE;
12074 #if 0
12075       ostiInitiatorEvent( tiRoot,
12076                             agNULL,
12077                             tiDeviceHandle,
12078                             tiIntrEventTypeLocalAbort,
12079                             tiAbortOK,
12080                             agNULL);
12081 #endif
12082 #if 1
12083       tdsmEventCB( smRoot,
12084                    smDeviceHandle,
12085                    smIntrEventTypeLocalAbort,
12086                    smTMOK,
12087                    agNULL);
12088 #endif
12089 
12090     }
12091     if (smIORequestBody->IOType.InitiatorTMIO.osMemHandle != agNULL)
12092     {
12093       tdsmFreeMemory(smRoot,
12094                      smIORequestBody->IOType.InitiatorTMIO.osMemHandle,
12095                      sizeof(smIORequestBody_t)
12096                      );
12097     }
12098   }
12099   else if (flag == 0)
12100   {
12101     SM_DBG1(("smaSATAAbortCB: abort one\n"));
12102     if (status == OSSA_IO_SUCCESS)
12103     {
12104       SM_DBG1(("smaSATAAbortCB: OSSA_IO_SUCCESS\n"));
12105     }
12106     else if (status == OSSA_IO_NOT_VALID)
12107     {
12108       SM_DBG1(("smaSATAAbortCB: OSSA_IO_NOT_VALID\n"));
12109     }
12110     else if (status == OSSA_IO_NO_DEVICE)
12111     {
12112       SM_DBG1(("smaSATAAbortCB: OSSA_IO_NO_DEVICE\n"));
12113     }
12114     else if (status == OSSA_IO_ABORT_IN_PROGRESS)
12115     {
12116       SM_DBG1(("smaSATAAbortCB: OSSA_IO_ABORT_IN_PROGRESS\n"));
12117     }
12118 #ifdef REMOVED
12119     else if (status == OSSA_IO_ABORT_DELAYED)
12120     {
12121       SM_DBG1(("smaSATAAbortCB: OSSA_IO_ABORT_DELAYED\n"));
12122     }
12123 #endif
12124     else
12125     {
12126       SM_DBG1(("smaSATAAbortCB: unspecified status 0x%x\n", status ));
12127     }
12128     if (smIORequestBody->IOType.InitiatorTMIO.osMemHandle != agNULL)
12129     {
12130       tdsmFreeMemory(smRoot,
12131                      smIORequestBody->IOType.InitiatorTMIO.osMemHandle,
12132                      sizeof(smIORequestBody_t)
12133                      );
12134     }
12135   }
12136   else
12137   {
12138     SM_DBG1(("smaSATAAbortCB: wrong flag %d\n", flag));
12139   }
12140 
12141   return;
12142 }
12143 
12144 osGLOBAL void
12145 smLocalPhyControlCB(
12146                      agsaRoot_t     *agRoot,
12147                      agsaContext_t  *agContext,
12148                      bit32          phyId,
12149                      bit32          phyOperation,
12150                      bit32          status,
12151                      void           *parm
12152                     )
12153 {
12154   smRoot_t                  *smRoot = gsmRoot;
12155   smIORequestBody_t         *smIORequestBody = agNULL;
12156   smDeviceHandle_t          *smDeviceHandle;
12157   smDeviceData_t            *oneDeviceData = agNULL;
12158   smIORequest_t             *currentTaskTag;
12159   agsaDevHandle_t           *agDevHandle = agNULL;
12160 
12161   SM_DBG1(("smLocalPhyControlCB: start phyId 0x%x phyOperation 0x%x status 0x%x\n",phyId,phyOperation,status));
12162 
12163   if (agContext == agNULL)
12164   {
12165     SM_DBG1(("smLocalPhyControlCB: agContext is NULL!!!\n"));
12166     return;
12167   }
12168   currentTaskTag = (smIORequest_t *)agContext->osData;
12169   if (currentTaskTag == agNULL)
12170   {
12171     SM_DBG1(("smLocalPhyControlCB: currentTaskTag is NULL!!!\n"));
12172     return;
12173   }
12174   smIORequestBody = (smIORequestBody_t *)currentTaskTag->smData;
12175   if (smIORequestBody == agNULL)
12176   {
12177     SM_DBG1(("smLocalPhyControlCB: smIORequestBody is NULL!!!\n"));
12178     return;
12179   }
12180   smDeviceHandle = smIORequestBody->smDevHandle;
12181   if (smDeviceHandle == agNULL)
12182   {
12183     SM_DBG1(("smLocalPhyControlCB: smDeviceHandle is NULL!!!\n"));
12184     return;
12185   }
12186   oneDeviceData = (smDeviceData_t *)smDeviceHandle->smData;
12187   if (oneDeviceData == agNULL)
12188   {
12189     SM_DBG1(("smLocalPhyControlCB: oneDeviceData is NULL!!!\n"));
12190     return;
12191   }
12192   switch (phyOperation)
12193   {
12194   case AGSA_PHY_LINK_RESET: /* fall through */
12195   case AGSA_PHY_HARD_RESET:
12196     if (status == OSSA_SUCCESS)
12197     {
12198       SM_DBG2(("smLocalPhyControlCB: callback to TD layer with success\n"));
12199       agDevHandle = oneDeviceData->agDevHandle;
12200       SM_DBG2(("smLocalPhyControlCB: satPendingIO %d satNCQMaxIO %d\n", oneDeviceData->satPendingIO, oneDeviceData->satNCQMaxIO ));
12201       SM_DBG1(("smLocalPhyControlCB: satPendingNCQIO %d satPendingNONNCQIO %d\n", oneDeviceData->satPendingNCQIO, oneDeviceData->satPendingNONNCQIO));
12202       oneDeviceData->satDriveState = SAT_DEV_STATE_NORMAL;
12203 #ifdef REMOVED
12204       saSetDeviceState(agRoot,
12205                        agNULL,
12206                        tdsmRotateQnumber(smRoot, smDeviceHandle),
12207                        agDevHandle,
12208                        SA_DS_OPERATIONAL
12209                        );
12210       /* TM completed */
12211       tdsmEventCB( smRoot,
12212                    smDeviceHandle,
12213                    smIntrEventTypeTaskManagement,
12214                    smTMOK,
12215                    currentTaskTag);
12216 #endif
12217     }
12218     else
12219     {
12220       SM_DBG1(("smLocalPhyControlCB: callback to TD layer with failure!!!\n"));
12221       /* TM completed */
12222       tdsmEventCB( smRoot,
12223                    smDeviceHandle,
12224                    smIntrEventTypeTaskManagement,
12225                    smTMFailed,
12226                    currentTaskTag);
12227     }
12228     break;
12229   default:
12230     SM_DBG1(("ossaLocalPhyControlCB: error default case. phyOperation is %d!!!\n", phyOperation));
12231     /* TM completed */
12232     tdsmEventCB( smRoot,
12233                  smDeviceHandle,
12234                  smIntrEventTypeTaskManagement,
12235                  smTMFailed,
12236                  currentTaskTag);
12237     break;
12238   }
12239   return;
12240 }
12241 
12242 osGLOBAL void
12243 smsatSetFeaturesAACB(
12244     agsaRoot_t        *agRoot,
12245     agsaIORequest_t   *agIORequest,
12246     bit32             agIOStatus,
12247     agsaFisHeader_t   *agFirstDword,
12248     bit32             agIOInfoLen,
12249     void              *agParam,
12250     void              *ioContext
12251     )
12252 {
12253     smRoot_t                  *smRoot = agNULL;
12254     smIntRoot_t               *smIntRoot = agNULL;
12255     smIntContext_t            *smAllShared = agNULL;
12256     smIORequestBody_t         *smIORequestBody;
12257     smIORequestBody_t         *smOrgIORequestBody = agNULL;
12258     smSatIOContext_t          *satIOContext;
12259     smSatIOContext_t          *satOrgIOContext;
12260     smSatInternalIo_t         *satIntIo;
12261     smDeviceData_t            *oneDeviceData;
12262     smIORequest_t             *smOrgIORequest;
12263     smDeviceHandle_t          *smDeviceHandle;
12264     smIORequest_t             *smIORequest;
12265     bit32                     ataStatus = 0;
12266     bit32                     ataError = 0;
12267     agsaFisRegD2HHeader_t     *statDevToHostFisHeader = agNULL;
12268 
12269     SM_DBG2(("smsatSetFeaturesAACB: start\n"));
12270     smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
12271     satIOContext           = (smSatIOContext_t *) ioContext;
12272     if (satIOContext == agNULL)
12273     {
12274       SM_DBG1(("smsatSetFeaturesAACB: satIOContext is NULL\n"));
12275       return;
12276     }
12277     satIntIo               = satIOContext->satIntIoContext;
12278     oneDeviceData          = satIOContext->pSatDevData;
12279     smDeviceHandle         = satIOContext->psmDeviceHandle;
12280     smRoot                 = oneDeviceData->smRoot;
12281     smIntRoot              = (smIntRoot_t *)smRoot->smData;
12282     smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
12283     if (satIntIo == agNULL)
12284     {
12285       SM_DBG5(("smsatSetFeaturesAACB: External smSatInternalIo_t satIntIoContext\n"));
12286       satOrgIOContext = satIOContext;
12287       smOrgIORequest  = smIORequestBody->smIORequest;
12288       smIORequest     = smOrgIORequest;
12289     }
12290     else
12291     {
12292       SM_DBG5(("smsatSetFeaturesAACB: Internal smSatInternalIo_t satIntIoContext\n"));
12293       satOrgIOContext        = satIOContext->satOrgIOContext;
12294       smOrgIORequestBody  = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
12295       smOrgIORequest      = (smIORequest_t *)smOrgIORequestBody->smIORequest;
12296     }
12297     smIORequest  = smOrgIORequestBody->smIORequest;
12298     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
12299     smIORequestBody->ioCompleted = agTRUE;
12300     smIORequestBody->ioStarted   = agFALSE;
12301     if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
12302     {
12303       SM_DBG1(("smsatSetFeaturesAACB: fail, case 1 agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
12304     }
12305     if (agIOStatus != OSSA_IO_SUCCESS)
12306     {
12307       SM_DBG1(("smsatSetFeaturesAACB: fail, case 2 status %d!!!\n", agIOStatus));
12308     }
12309     if (agIOInfoLen != 0 && agIOStatus == OSSA_IO_SUCCESS)
12310     {
12311       statDevToHostFisHeader = (agsaFisRegD2HHeader_t *)&(agFirstDword->D2H);
12312       ataStatus   = statDevToHostFisHeader->status;   /* ATA Status register */
12313       ataError    = statDevToHostFisHeader->error;    /* ATA Eror register   */
12314       if ((ataStatus & ERR_ATA_STATUS_MASK) || (ataStatus & DF_ATA_STATUS_MASK))
12315       {
12316         SM_DBG1(("smsatSetFeaturesAACB: fail, case 3 ataStatus %d ataError %d!!!\n", ataStatus, ataError));
12317       }
12318       if (ataError != 0)
12319       {
12320         SM_DBG1(("smsatSetFeaturesAACB: fail, case 4 ataStatus %d ataError %d!!!\n", ataStatus, ataError));
12321       }
12322     }
12323     /* interal structure free */
12324     smsatFreeIntIoResource(smRoot,oneDeviceData, satIntIo);
12325     if (smIORequest->tdData == smIORequest->smData)
12326     {
12327       SM_DBG1(("smsatSetFeaturesAACB: the same tdData and smData error!\n"));
12328     }
12329     /*Complete this identify device IO */
12330     tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIOSuccess, &(oneDeviceData->satIdentifyData));
12331     SM_DBG2(("smsatSetFeaturesAACB: end\n"));
12332 }
12333 
12334 /*****************************************************************************
12335 *! \brief  smsatSetFeaturesDMACB
12336 *
12337 *   This routine is a callback function called from smllSATACompleted().
12338 *   This CB routine deals with normal non-chained data I/O SATA request.
12339 *
12340 *  \param   agRoot:      Handles for this instance of SAS/SATA hardware
12341 *  \param   agIORequest: Pointer to the LL I/O request context for this I/O.
12342 *  \param   agIOStatus:  Status of completed I/O.
12343 *  \param   agFirstDword:Pointer to the four bytes of FIS.
12344 *  \param   agIOInfoLen: Length in bytes of overrun/underrun residual or FIS
12345 *                        length.
12346 *  \param   agParam:     Additional info based on status.
12347 *  \param   ioContext:   Pointer to smSatIOContext_t.
12348 *
12349 *  \return: none
12350 *
12351 *****************************************************************************/
12352 osGLOBAL void
12353 smsatSetFeaturesDMACB(
12354     agsaRoot_t        *agRoot,
12355     agsaIORequest_t   *agIORequest,
12356     bit32             agIOStatus,
12357     agsaFisHeader_t   *agFirstDword,
12358     bit32             agIOInfoLen,
12359     void              *agParam,
12360     void              *ioContext
12361     )
12362 {
12363     smRoot_t                  *smRoot = agNULL;
12364     smIntRoot_t               *smIntRoot = agNULL;
12365     smIntContext_t            *smAllShared = agNULL;
12366     smIORequestBody_t         *smIORequestBody;
12367     smIORequestBody_t         *smOrgIORequestBody = agNULL;
12368     smSatIOContext_t          *satIOContext;
12369     smSatIOContext_t          *satOrgIOContext;
12370     smSatIOContext_t          *satNewIOContext;
12371     smSatInternalIo_t         *satIntIo;
12372     smSatInternalIo_t         *satNewIntIo = agNULL;
12373     smDeviceData_t            *oneDeviceData;
12374     smIniScsiCmnd_t           *scsiCmnd;
12375     smIORequest_t             *smOrgIORequest;
12376     smDeviceHandle_t          *smDeviceHandle;
12377     bit32                      status = SM_RC_FAILURE;
12378     smIORequest_t             *smIORequest;
12379 
12380     SM_DBG2(("smsatSetFeaturesDMACB: start\n"));
12381     smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
12382     satIOContext           = (smSatIOContext_t *) ioContext;
12383     if (satIOContext == agNULL)
12384     {
12385       SM_DBG1(("smsatSetFeaturesDMACB: satIOContext is NULL\n"));
12386       return;
12387     }
12388     satIntIo               = satIOContext->satIntIoContext;
12389     oneDeviceData          = satIOContext->pSatDevData;
12390     smDeviceHandle         = satIOContext->psmDeviceHandle;
12391     smRoot                 = oneDeviceData->smRoot;
12392     smIntRoot              = (smIntRoot_t *)smRoot->smData;
12393     smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
12394     if (satIntIo == agNULL)
12395     {
12396       SM_DBG2(("smsatSetFeaturesDMACB: External smSatInternalIo_t satIntIoContext\n"));
12397       satOrgIOContext = satIOContext;
12398       smOrgIORequest  = smIORequestBody->smIORequest;
12399       scsiCmnd        = satIOContext->pScsiCmnd;
12400     }
12401     else
12402     {
12403       SM_DBG2(("smsatSetFeaturesDMACB: Internal smSatInternalIo_t satIntIoContext\n"));
12404       satOrgIOContext        = satIOContext->satOrgIOContext;
12405       smOrgIORequestBody     = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
12406       smOrgIORequest         = (smIORequest_t *)smOrgIORequestBody->smIORequest;
12407       scsiCmnd      = satOrgIOContext->pScsiCmnd;
12408     }
12409     smIORequest  = smOrgIORequestBody->smIORequest;
12410     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
12411     smIORequestBody->ioCompleted = agTRUE;
12412     smIORequestBody->ioStarted   = agFALSE;
12413 
12414     oneDeviceData->satDMAEnabled = agTRUE;
12415     /* interal structure free */
12416     smsatFreeIntIoResource(smRoot,
12417                            oneDeviceData,
12418                            satIntIo);
12419 
12420     if (smIORequest->tdData == smIORequest->smData)
12421     {
12422       SM_DBG1(("smsatSetFeaturesDMACB: the same tdData and smData error!\n"));
12423     }
12424     SM_DBG2(("smsatSetFeaturesDMACB: agIOStatus 0x%x\n", agIOStatus));
12425     /* check the agIOStatus */
12426     if (agIOStatus == OSSA_IO_ABORTED ||
12427         agIOStatus == OSSA_IO_NO_DEVICE ||
12428         agIOStatus == OSSA_IO_PORT_IN_RESET ||
12429         agIOStatus == OSSA_IO_DS_NON_OPERATIONAL ||
12430         agIOStatus == OSSA_IO_DS_IN_RECOVERY ||
12431         agIOStatus == OSSA_IO_DS_IN_ERROR ||
12432         agIOStatus == OSSA_IO_DS_INVALID
12433        )
12434     {
12435       SM_DBG1(("smsatSetFeaturesDMACB: error status 0x%x\n", agIOStatus));
12436       SM_DBG1(("smsatSetFeaturesDMACB: did %d!!!\n", oneDeviceData->id));
12437       tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIOSuccess, &(oneDeviceData->satIdentifyData));
12438       return;
12439     }
12440     if (oneDeviceData->satDeviceType == SATA_ATAPI_DEVICE)
12441     {
12442        /*if ATAPI device, only need to enable PIO and DMA transfer mode, then complete this identify device command */
12443        tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIOSuccess, &(oneDeviceData->satIdentifyData));
12444        return;
12445     }
12446 
12447     /* enble read look-ahead feature*/
12448     if (oneDeviceData->satReadLookAheadSupport == agTRUE)
12449     {
12450         satNewIntIo = smsatAllocIntIoResource(smRoot,
12451                                            smOrgIORequest,
12452                                            oneDeviceData,
12453                                            0,
12454                                            satNewIntIo);
12455         if (satNewIntIo == agNULL)
12456         {
12457             SM_DBG1(("smsatSetFeaturesDMACB: memory allocation fails\n"));
12458             /*Complete this identify packet device IO */
12459             tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIOSuccess, &(oneDeviceData->satIdentifyData));
12460             return;
12461         } /* end memory allocation */
12462 
12463         satNewIOContext = smsatPrepareNewIO(satNewIntIo,
12464                                           smOrgIORequest,
12465                                           oneDeviceData,
12466                                           scsiCmnd,
12467                                           satOrgIOContext
12468                                           );
12469         /* sends SET FEATURES  command to enable Read Look-Ahead  */
12470         status = smsatSetFeaturesReadLookAhead(smRoot,
12471                                 &satNewIntIo->satIntSmIORequest,
12472                                 satNewIOContext->psmDeviceHandle,
12473                                 &satNewIntIo->satIntSmScsiXchg,
12474                                 satNewIOContext
12475                                 );
12476         if (status != SM_RC_SUCCESS)
12477         {
12478             smsatFreeIntIoResource(smRoot, oneDeviceData, satNewIntIo);
12479             SM_DBG1(("smsatSetFeaturesDMACB: failed to call smsatSetFeatures()\n"));
12480             /*Complete this identify device IO */
12481             tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIOSuccess, &(oneDeviceData->satIdentifyData));
12482         }
12483         SM_DBG2(("smsatSetFeaturesDMACB: end\n"));
12484         return;
12485     }
12486     /* enble Volatile Write Cache feature*/
12487     if (oneDeviceData->satVolatileWriteCacheSupport == agTRUE)
12488     {
12489        satNewIntIo = smsatAllocIntIoResource(smRoot,
12490                                              smOrgIORequest,
12491                                              oneDeviceData,
12492                                              0,
12493                                              satNewIntIo);
12494         if (satNewIntIo == agNULL)
12495         {
12496            SM_DBG1(("smsatSetFeaturesDMACB: memory allocation fails\n"));
12497            /*Complete this identify packet device IO */
12498            tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIOSuccess, &(oneDeviceData->satIdentifyData));
12499            return;
12500         } /* end memory allocation */
12501         satNewIOContext = smsatPrepareNewIO(satNewIntIo,
12502                                             smOrgIORequest,
12503                                             oneDeviceData,
12504                                             scsiCmnd,
12505                                             satOrgIOContext
12506                                             );
12507         /* sends SET FEATURES command to enable Volatile Write Cache */
12508         status = smsatSetFeaturesVolatileWriteCache(smRoot,
12509                                     &satNewIntIo->satIntSmIORequest,
12510                                     satNewIOContext->psmDeviceHandle,
12511                                     &satNewIntIo->satIntSmScsiXchg,
12512                                     satNewIOContext
12513                                     );
12514         if (status != SM_RC_SUCCESS)
12515         {
12516            smsatFreeIntIoResource(smRoot, oneDeviceData, satNewIntIo);
12517            SM_DBG1(("smsatSetFeaturesDMACB: failed to call smsatSetFeatures()\n"));
12518            /*Complete this identify device IO */
12519            tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIOSuccess, &(oneDeviceData->satIdentifyData));
12520         }
12521         SM_DBG2(("smsatSetFeaturesDMACB: end\n"));
12522         return;
12523     }
12524     /* turn on DMA Setup FIS auto-activate by sending set feature FIS */
12525     if (oneDeviceData->satNCQ == agTRUE && oneDeviceData->satDMASetupAA == agTRUE)
12526     {
12527         satNewIntIo = smsatAllocIntIoResource( smRoot,
12528                                                smOrgIORequest,
12529                                                oneDeviceData,
12530                                                0,
12531                                                satNewIntIo);
12532 
12533         if (satNewIntIo == agNULL)
12534         {
12535           SM_DBG1(("smsatSetFeaturesDMACB: momory allocation fails; can't send set feature\n"));
12536           tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIOSuccess, &(oneDeviceData->satIdentifyData));
12537           return;
12538         } /* end memory allocation */
12539         satNewIOContext = smsatPrepareNewIO(satNewIntIo,
12540                                             smOrgIORequest,
12541                                             oneDeviceData,
12542                                             agNULL,
12543                                             satOrgIOContext
12544                                             );
12545         /* send the Set Feature ATA command to SATA device for enable DMA Setup FIS auto-activate */
12546         status = smsatSetFeaturesAA(smRoot,
12547                                     &satNewIntIo->satIntSmIORequest,
12548                                     satNewIOContext->psmDeviceHandle,
12549                                     &satNewIntIo->satIntSmScsiXchg, /* orginal from OS layer */
12550                                     satNewIOContext);
12551         if (status != SM_RC_SUCCESS)
12552         {
12553             SM_DBG1(("smsatSetFeaturesDMACB: failed to send set feature!!!\n"));
12554             smsatFreeIntIoResource( smRoot,
12555                                     oneDeviceData,
12556                                     satNewIntIo);
12557             tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIOFailed, &(oneDeviceData->satIdentifyData));
12558         }
12559     }
12560     else
12561     {
12562         /*Complete this identify device IO */
12563         tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIOSuccess, &(oneDeviceData->satIdentifyData));
12564     }
12565     SM_DBG2(("smsatSetFeaturesDMACB: end\n"));
12566 }
12567 
12568 /*****************************************************************************
12569 *! \brief  smsatSetFeaturesReadLookAheadCB
12570 *
12571 *   This routine is a callback function called from smllSATACompleted().
12572 *   This CB routine deals with normal non-chained data I/O SATA request.
12573 *
12574 *  \param   agRoot:      Handles for this instance of SAS/SATA hardware
12575 *  \param   agIORequest: Pointer to the LL I/O request context for this I/O.
12576 *  \param   agIOStatus:  Status of completed I/O.
12577 *  \param   agFirstDword:Pointer to the four bytes of FIS.
12578 *  \param   agIOInfoLen: Length in bytes of overrun/underrun residual or FIS
12579 *                        length.
12580 *  \param   agParam:     Additional info based on status.
12581 *  \param   ioContext:   Pointer to smSatIOContext_t.
12582 *
12583 *  \return: none
12584 *
12585 *****************************************************************************/
12586 osGLOBAL void
12587 smsatSetFeaturesReadLookAheadCB(
12588     agsaRoot_t        *agRoot,
12589     agsaIORequest_t   *agIORequest,
12590     bit32             agIOStatus,
12591     agsaFisHeader_t   *agFirstDword,
12592     bit32             agIOInfoLen,
12593     void              *agParam,
12594     void              *ioContext
12595     )
12596 {
12597     smRoot_t                  *smRoot = agNULL;
12598     smIntRoot_t               *smIntRoot = agNULL;
12599     smIntContext_t            *smAllShared = agNULL;
12600     smIORequestBody_t         *smIORequestBody;
12601     smIORequestBody_t         *smOrgIORequestBody = agNULL;
12602     smSatIOContext_t          *satIOContext;
12603     smSatIOContext_t          *satOrgIOContext;
12604     smSatIOContext_t          *satNewIOContext;
12605     smSatInternalIo_t         *satIntIo;
12606     smSatInternalIo_t         *satNewIntIo = agNULL;
12607     smDeviceData_t            *oneDeviceData;
12608     smIniScsiCmnd_t           *scsiCmnd;
12609     smIORequest_t             *smOrgIORequest;
12610     smDeviceHandle_t          *smDeviceHandle;
12611     bit32                      status = SM_RC_FAILURE;
12612     smIORequest_t             *smIORequest;
12613 
12614     SM_DBG2(("smsatSetFeaturesReadLookAheadCB: start\n"));
12615     smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
12616     satIOContext           = (smSatIOContext_t *) ioContext;
12617     if (satIOContext == agNULL)
12618     {
12619       SM_DBG1(("smsatSetFeaturesReadLookAheadCB: satIOContext is NULL\n"));
12620       return;
12621     }
12622     satIntIo               = satIOContext->satIntIoContext;
12623     oneDeviceData          = satIOContext->pSatDevData;
12624     smDeviceHandle         = satIOContext->psmDeviceHandle;
12625     smRoot                 = oneDeviceData->smRoot;
12626     smIntRoot              = (smIntRoot_t *)smRoot->smData;
12627     smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
12628 
12629     if (satIntIo == agNULL)
12630     {
12631       SM_DBG2(("smsatSetFeaturesReadLookAheadCB: External smSatInternalIo_t satIntIoContext\n"));
12632       satOrgIOContext = satIOContext;
12633       smOrgIORequest  = smIORequestBody->smIORequest;
12634       scsiCmnd        = satIOContext->pScsiCmnd;
12635     }
12636     else
12637     {
12638       SM_DBG2(("smsatSetFeaturesReadLookAheadCB: Internal smSatInternalIo_t satIntIoContext\n"));
12639       satOrgIOContext        = satIOContext->satOrgIOContext;
12640       smOrgIORequestBody     = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
12641       smOrgIORequest         = (smIORequest_t *)smOrgIORequestBody->smIORequest;
12642       scsiCmnd      = satOrgIOContext->pScsiCmnd;
12643     }
12644     smIORequest  = smOrgIORequestBody->smIORequest;
12645     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
12646 
12647     smIORequestBody->ioCompleted = agTRUE;
12648     smIORequestBody->ioStarted   = agFALSE;
12649 
12650     oneDeviceData->satLookAheadEnabled = agTRUE;
12651 
12652     /* interal structure free */
12653     smsatFreeIntIoResource(smRoot,
12654                            oneDeviceData,
12655                            satIntIo);
12656 
12657     /* check the agIOStatus */
12658     if (agIOStatus == OSSA_IO_ABORTED ||
12659         agIOStatus == OSSA_IO_NO_DEVICE ||
12660         agIOStatus == OSSA_IO_PORT_IN_RESET ||
12661         agIOStatus == OSSA_IO_DS_NON_OPERATIONAL ||
12662         agIOStatus == OSSA_IO_DS_IN_RECOVERY ||
12663         agIOStatus == OSSA_IO_DS_IN_ERROR ||
12664         agIOStatus == OSSA_IO_DS_INVALID
12665        )
12666     {
12667       SM_DBG1(("smsatSetFeaturesReadLookAheadCB: error status 0x%x\n", agIOStatus));
12668       SM_DBG1(("smsatSetFeaturesReadLookAheadCB: did %d!!!\n", oneDeviceData->id));
12669       tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIOSuccess, &(oneDeviceData->satIdentifyData));
12670       return;
12671     }
12672 
12673 
12674 
12675     /* enble Volatile Write Cache feature*/
12676     if (oneDeviceData->satVolatileWriteCacheSupport == agTRUE)
12677     {
12678         satNewIntIo = smsatAllocIntIoResource(smRoot,
12679                                            smOrgIORequest,
12680                                            oneDeviceData,
12681                                            0,
12682                                            satNewIntIo);
12683         if (satNewIntIo == agNULL)
12684         {
12685             SM_DBG1(("smsatSetFeaturesReadLookAheadCB: memory allocation fails\n"));
12686             /*Complete this identify packet device IO */
12687             tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIOSuccess, &(oneDeviceData->satIdentifyData));
12688             return;
12689         } /* end memory allocation */
12690 
12691         satNewIOContext = smsatPrepareNewIO(satNewIntIo,
12692                                           smOrgIORequest,
12693                                           oneDeviceData,
12694                                           scsiCmnd,
12695                                           satOrgIOContext
12696                                           );
12697         /* sends SET FEATURES command to enable Volatile Write Cache */
12698         status = smsatSetFeaturesVolatileWriteCache(smRoot,
12699                                 &satNewIntIo->satIntSmIORequest,
12700                                 satNewIOContext->psmDeviceHandle,
12701                                 &satNewIntIo->satIntSmScsiXchg,
12702                                 satNewIOContext
12703                                 );
12704         if (status != SM_RC_SUCCESS)
12705         {
12706             smsatFreeIntIoResource(smRoot, oneDeviceData, satNewIntIo);
12707             SM_DBG1(("smsatSetFeaturesReadLookAheadCB: failed to call smsatSetFeatures()\n"));
12708             /*Complete this identify device IO */
12709             tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIOSuccess, &(oneDeviceData->satIdentifyData));
12710         }
12711         SM_DBG2(("smsatSetFeaturesReadLookAheadCB: end\n"));
12712 
12713         return;
12714     }
12715 
12716     /* turn on DMA Setup FIS auto-activate by sending set feature FIS */
12717     if (oneDeviceData->satNCQ == agTRUE && oneDeviceData->satDMASetupAA == agTRUE)
12718     {
12719         satNewIntIo = smsatAllocIntIoResource( smRoot,
12720                                                smOrgIORequest,
12721                                                oneDeviceData,
12722                                                0,
12723                                                satNewIntIo);
12724 
12725         if (satNewIntIo == agNULL)
12726         {
12727           SM_DBG1(("smsatSetFeaturesReadLookAheadCB: momory allocation fails; can't send set feature\n"));
12728           tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIOSuccess, &(oneDeviceData->satIdentifyData));
12729           return;
12730         } /* end memory allocation */
12731 
12732         satNewIOContext = smsatPrepareNewIO(satNewIntIo,
12733                                             smOrgIORequest,
12734                                             oneDeviceData,
12735                                             agNULL,
12736                                             satOrgIOContext
12737                                             );
12738         /* send the Set Feature ATA command to SATA device for enable DMA Setup FIS auto-activate */
12739         status = smsatSetFeaturesAA(smRoot,
12740                                     &satNewIntIo->satIntSmIORequest,
12741                                     satNewIOContext->psmDeviceHandle,
12742                                     &satNewIntIo->satIntSmScsiXchg, /* orginal from OS layer */
12743                                     satNewIOContext);
12744 
12745         if (status != SM_RC_SUCCESS)
12746         {
12747             SM_DBG1(("smsatSetFeaturesReadLookAheadCB: failed to send set feature!!!\n"));
12748             smsatFreeIntIoResource( smRoot,
12749                                     oneDeviceData,
12750                                     satNewIntIo);
12751             /* clean up TD layer's IORequestBody */
12752             tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIOFailed, &(oneDeviceData->satIdentifyData));
12753         }
12754     }
12755     else
12756     {
12757         /*Complete this identify device IO */
12758         tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIOSuccess, &(oneDeviceData->satIdentifyData));
12759     }
12760     SM_DBG2(("smsatSetFeaturesReadLookAheadCB: end\n"));
12761 }
12762 /*****************************************************************************
12763 *! \brief  smsatSetFeaturesVolatileWriteCacheCB
12764 *
12765 *   This routine is a callback function called from smllSATACompleted().
12766 *   This CB routine deals with normal non-chained data I/O SATA request.
12767 *
12768 *  \param   agRoot:      Handles for this instance of SAS/SATA hardware
12769 *  \param   agIORequest: Pointer to the LL I/O request context for this I/O.
12770 *  \param   agIOStatus:  Status of completed I/O.
12771 *  \param   agFirstDword:Pointer to the four bytes of FIS.
12772 *  \param   agIOInfoLen: Length in bytes of overrun/underrun residual or FIS
12773 *                        length.
12774 *  \param   agParam:     Additional info based on status.
12775 *  \param   ioContext:   Pointer to smSatIOContext_t.
12776 *
12777 *  \return: none
12778 *
12779 *****************************************************************************/
12780 osGLOBAL void
12781 smsatSetFeaturesVolatileWriteCacheCB(
12782     agsaRoot_t        *agRoot,
12783     agsaIORequest_t   *agIORequest,
12784     bit32             agIOStatus,
12785     agsaFisHeader_t   *agFirstDword,
12786     bit32             agIOInfoLen,
12787     void              *agParam,
12788     void              *ioContext
12789     )
12790 {
12791     smRoot_t                  *smRoot = agNULL;
12792     smIntRoot_t               *smIntRoot = agNULL;
12793     smIntContext_t            *smAllShared = agNULL;
12794     smIORequestBody_t         *smIORequestBody;
12795     smIORequestBody_t         *smOrgIORequestBody = agNULL;
12796     smSatIOContext_t          *satIOContext;
12797     smSatIOContext_t          *satOrgIOContext;
12798     smSatIOContext_t          *satNewIOContext;
12799     smSatInternalIo_t         *satIntIo;
12800     smSatInternalIo_t         *satNewIntIo = agNULL;
12801     smDeviceData_t            *oneDeviceData;
12802     smIORequest_t             *smOrgIORequest;
12803     smDeviceHandle_t          *smDeviceHandle;
12804     smIORequest_t             *smIORequest;
12805     bit32                     ataStatus = 0;
12806     bit32                     ataError = 0;
12807     agsaFisRegD2HHeader_t     *statDevToHostFisHeader = agNULL;
12808     bit32                     status = SM_RC_FAILURE;
12809 
12810     SM_DBG2(("smsatSetFeaturesVolatileWriteCacheCB: start\n"));
12811     smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
12812     satIOContext           = (smSatIOContext_t *) ioContext;
12813     if (satIOContext == agNULL)
12814     {
12815       SM_DBG1(("smsatSetFeaturesVolatileWriteCacheCB: satIOContext is NULL\n"));
12816       return;
12817     }
12818     satIntIo               = satIOContext->satIntIoContext;
12819     oneDeviceData          = satIOContext->pSatDevData;
12820     smDeviceHandle         = satIOContext->psmDeviceHandle;
12821     smRoot                 = oneDeviceData->smRoot;
12822     smIntRoot              = (smIntRoot_t *)smRoot->smData;
12823     smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
12824     if (satIntIo == agNULL)
12825     {
12826       SM_DBG5(("smsatSetFeaturesVolatileWriteCacheCB: External smSatInternalIo_t satIntIoContext\n"));
12827       satOrgIOContext = satIOContext;
12828       smOrgIORequest  = smIORequestBody->smIORequest;
12829       smIORequest     = smOrgIORequest;
12830     }
12831     else
12832     {
12833       SM_DBG5(("smsatSetFeaturesVolatileWriteCacheCB: Internal smSatInternalIo_t satIntIoContext\n"));
12834       satOrgIOContext        = satIOContext->satOrgIOContext;
12835       smOrgIORequestBody  = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
12836       smOrgIORequest      = (smIORequest_t *)smOrgIORequestBody->smIORequest;
12837     }
12838     smIORequest  = smOrgIORequestBody->smIORequest;
12839     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
12840 
12841     smIORequestBody->ioCompleted = agTRUE;
12842     smIORequestBody->ioStarted   = agFALSE;
12843     if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
12844     {
12845       SM_DBG1(("smsatSetFeaturesVolatileWriteCacheCB: fail, case 1 agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
12846     }
12847     if (agIOStatus != OSSA_IO_SUCCESS)
12848     {
12849       SM_DBG1(("smsatSetFeaturesVolatileWriteCacheCB: fail, case 2 status %d!!!\n", agIOStatus));
12850     }
12851     if (agIOInfoLen != 0 && agIOStatus == OSSA_IO_SUCCESS)
12852     {
12853       statDevToHostFisHeader = (agsaFisRegD2HHeader_t *)&(agFirstDword->D2H);
12854       ataStatus     = statDevToHostFisHeader->status;   /* ATA Status register */
12855       ataError      = statDevToHostFisHeader->error;    /* ATA Eror register   */
12856       if ((ataStatus & ERR_ATA_STATUS_MASK) || (ataStatus & DF_ATA_STATUS_MASK))
12857       {
12858         SM_DBG1(("smsatSetFeaturesVolatileWriteCacheCB: fail, case 3 ataStatus %d ataError %d!!!\n", ataStatus, ataError));
12859       }
12860       if (ataError != 0)
12861       {
12862         SM_DBG1(("smsatSetFeaturesVolatileWriteCacheCB: fail, case 4 ataStatus %d ataError %d!!!\n", ataStatus, ataError));
12863       }
12864     }
12865 
12866     oneDeviceData->satWriteCacheEnabled = agTRUE;
12867 
12868     /* interal structure free */
12869     smsatFreeIntIoResource(smRoot,oneDeviceData, satIntIo);
12870     /* check the agIOStatus */
12871     if (agIOStatus == OSSA_IO_ABORTED ||
12872         agIOStatus == OSSA_IO_NO_DEVICE ||
12873         agIOStatus == OSSA_IO_PORT_IN_RESET ||
12874         agIOStatus == OSSA_IO_DS_NON_OPERATIONAL ||
12875         agIOStatus == OSSA_IO_DS_IN_RECOVERY ||
12876         agIOStatus == OSSA_IO_DS_IN_ERROR ||
12877         agIOStatus == OSSA_IO_DS_INVALID
12878        )
12879     {
12880       SM_DBG1(("smsatSetFeaturesVolatileWriteCacheCB: error status 0x%x\n", agIOStatus));
12881       SM_DBG1(("smsatSetFeaturesVolatileWriteCacheCB: did %d!!!\n", oneDeviceData->id));
12882       tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIOSuccess, &(oneDeviceData->satIdentifyData));
12883       return;
12884     }
12885     /* turn on DMA Setup FIS auto-activate by sending set feature FIS */
12886     if (oneDeviceData->satNCQ == agTRUE && oneDeviceData->satDMASetupAA == agTRUE)
12887     {
12888         satNewIntIo = smsatAllocIntIoResource( smRoot,
12889                                                smOrgIORequest,
12890                                                oneDeviceData,
12891                                                0,
12892                                                satNewIntIo);
12893         if (satNewIntIo == agNULL)
12894         {
12895           SM_DBG1(("smsatSetFeaturesVolatileWriteCacheCB: momory allocation fails; can't send set feature\n"));
12896           tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIOSuccess, &(oneDeviceData->satIdentifyData));
12897           return;
12898         } /* end memory allocation */
12899         satNewIOContext = smsatPrepareNewIO(satNewIntIo,
12900                                             smOrgIORequest,
12901                                             oneDeviceData,
12902                                             agNULL,
12903                                             satOrgIOContext
12904                                             );
12905         /* send the Set Feature ATA command to SATA device for enable DMA Setup FIS auto-activate */
12906         status = smsatSetFeaturesAA(smRoot,
12907                                     &satNewIntIo->satIntSmIORequest,
12908                                     satNewIOContext->psmDeviceHandle,
12909                                     &satNewIntIo->satIntSmScsiXchg, /* orginal from OS layer */
12910                                     satNewIOContext);
12911         if (status != SM_RC_SUCCESS)
12912         {
12913             SM_DBG1(("smsatSetFeaturesVolatileWriteCacheCB: failed to send set feature!!!\n"));
12914             smsatFreeIntIoResource( smRoot,
12915                                     oneDeviceData,
12916                                     satNewIntIo);
12917             /* clean up TD layer's IORequestBody */
12918             tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIOFailed, &(oneDeviceData->satIdentifyData));
12919         }
12920     }
12921     else
12922     {
12923         /*Complete this identify device IO */
12924         tdsmIDCompletedCB(smRoot, smIORequest, smDeviceHandle, smIOSuccess, &(oneDeviceData->satIdentifyData));
12925     }
12926     SM_DBG2(("smsatSetFeaturesVolatileWriteCacheCB: end\n"));
12927 }
12928 
12929 
12930 osGLOBAL void
12931 smsatSMARTEnablePassCB(
12932                      agsaRoot_t        *agRoot,
12933                      agsaIORequest_t   *agIORequest,
12934                      bit32             agIOStatus,
12935                      agsaFisHeader_t   *agFirstDword,
12936                      bit32             agIOInfoLen,
12937                      agsaFrameHandle_t agFrameHandle,
12938                      void              *ioContext
12939                     )
12940   {
12941 
12942   smRoot_t                 *smRoot = agNULL;
12943   smIntRoot_t              *smIntRoot = agNULL;
12944   smIntContext_t           *smAllShared = agNULL;
12945   smIORequestBody_t        *smIORequestBody;
12946   smIORequestBody_t        *smOrgIORequestBody;
12947   smSatIOContext_t         *satIOContext;
12948   smSatIOContext_t         *satOrgIOContext;
12949   //smSatIOContext_t         *satNewIOContext;
12950   smSatInternalIo_t        *satIntIo;
12951  //smSatInternalIo_t        *satNewIntIo = agNULL;
12952 //  satDeviceData_t           *satDevData;
12953   smDeviceData_t           *oneDeviceData;
12954   smIniScsiCmnd_t          *scsiCmnd;
12955   smIORequest_t            *smOrgIORequest;
12956   //bit32                     status;
12957   smScsiRspSense_t          *pSense;
12958   bit8						bSenseKey = 0;
12959   bit16 					bSenseCodeInfo = 0;
12960 
12961 
12962   SM_DBG2(("smsatSMARTEnablePassCB: start\n"));
12963   SM_DBG4(("smsatSMARTEnablePassCB:agIORequest=%p agIOStatus=0x%x agIOInfoLen %d\n", agIORequest, agIOStatus, agIOInfoLen));
12964 
12965   /* internally generate tiIOContext */
12966   smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
12967   satIOContext           = (smSatIOContext_t *) ioContext;
12968   satIntIo               = satIOContext->satIntIoContext;
12969   oneDeviceData          = satIOContext->pSatDevData;
12970   smRoot                 = oneDeviceData->smRoot;
12971   smIntRoot              = (smIntRoot_t *)smRoot->smData;
12972   smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
12973   /*ttttttthe one */
12974   if (satIntIo == agNULL)
12975   {
12976     SM_DBG4(("smsatSMARTEnablePassCB: External smSatInternalIo_t satIntIoContext\n"));
12977     satOrgIOContext = satIOContext;
12978     smOrgIORequest  = smIORequestBody->smIORequest;
12979     scsiCmnd        = satOrgIOContext->pScsiCmnd;
12980 	pSense          = satOrgIOContext->pSense;
12981   }
12982   else
12983   {
12984     SM_DBG4(("smsatSMARTEnablePassCB: Internal smSatInternalIo_t satIntIoContext\n"));
12985     satOrgIOContext        = satIOContext->satOrgIOContext;
12986     if (satOrgIOContext == agNULL)
12987     {
12988       SM_DBG4(("smsatSMARTEnablePassCB: satOrgIOContext is NULL, wrong\n"));
12989       return;
12990     }
12991     else
12992     {
12993       SM_DBG4(("smsatSMARTEnablePassCB: satOrgIOContext is NOT NULL\n"));
12994     }
12995     smOrgIORequestBody     = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
12996     smOrgIORequest         = (smIORequest_t *)smOrgIORequestBody->smIORequest;
12997     scsiCmnd               = satOrgIOContext->pScsiCmnd;
12998 	pSense          = satOrgIOContext->pSense;
12999   }
13000   smIORequestBody->ioCompleted = agTRUE;
13001   smIORequestBody->ioStarted = agFALSE;
13002 
13003   if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
13004   {
13005     SM_DBG1(("smsatSMARTEnablePassCB: wrong. agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
13006     tdsmIOCompletedCB(
13007                       smRoot,
13008                       smOrgIORequest,
13009                       smIOFailed,
13010                       smDetailOtherError,
13011                       agNULL,
13012                       satOrgIOContext->interruptContext
13013                      );
13014     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
13015     smsatFreeIntIoResource( smRoot,
13016                             oneDeviceData,
13017                             satIntIo);
13018     return;
13019   }
13020   /*
13021     checking IO status, FIS type and error status
13022   */
13023   if (agIOStatus != OSSA_IO_SUCCESS)
13024   {
13025     SM_DBG1(("smsatSMARTEnablePassCB: not success status, status %d!!!\n", agIOStatus));
13026     if (agIOStatus == OSSA_IO_ABORTED)
13027     {
13028       smsatProcessAbort(smRoot,
13029                         smOrgIORequest,
13030                         satOrgIOContext
13031                        );
13032 
13033       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
13034       smsatFreeIntIoResource( smRoot,
13035                               oneDeviceData,
13036                               satIntIo);
13037       return;
13038     }
13039 
13040     smsatTranslateATAErrorsToSCSIErrors(
13041 				agFirstDword->D2H.status,
13042 				agFirstDword->D2H.error,
13043 				&bSenseKey,
13044 				&bSenseCodeInfo
13045 				);
13046     smsatSetSensePayload(pSense, bSenseKey, 0, bSenseCodeInfo, satOrgIOContext);
13047     tdsmIOCompletedCB(smRoot,
13048                       smOrgIORequest,
13049                       smIOSuccess,
13050                       SCSI_STAT_CHECK_CONDITION,
13051                       satOrgIOContext->pSmSenseData,
13052                       satOrgIOContext->interruptContext );
13053 
13054 
13055     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
13056 
13057     smsatFreeIntIoResource( smRoot,
13058                             oneDeviceData,
13059                             satIntIo);
13060 
13061     return;
13062   }
13063   /* process success case */
13064   smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
13065   smsatFreeIntIoResource( smRoot,
13066                           oneDeviceData,
13067                           satIntIo);
13068 
13069  SM_DBG1(("smsatSMARTEnablePassCB:success status, status %d!!!\n", agIOStatus));
13070  tdsmIOCompletedCB(
13071 					smRoot,
13072 					smOrgIORequest,
13073 					smIOSuccess,
13074 					SCSI_STAT_GOOD,
13075 					agNULL,
13076 					satOrgIOContext->interruptContext
13077 				   );
13078 
13079 
13080 
13081   return;
13082 }
13083 
13084 osGLOBAL void
13085 smsatSMARTRStatusPassCB(
13086                 agsaRoot_t        *agRoot,
13087                 agsaIORequest_t   *agIORequest,
13088                 bit32             agIOStatus,
13089                 agsaFisHeader_t   *agFirstDword,
13090                 bit32             agIOInfoLen,
13091                 void              *agParam,
13092                 void              *ioContext
13093                )
13094 
13095 {
13096 
13097 
13098   smRoot_t                  *smRoot = agNULL;
13099   smIntRoot_t               *smIntRoot = agNULL;
13100   smIntContext_t            *smAllShared = agNULL;
13101   smIORequestBody_t         *smIORequestBody;
13102   smIORequestBody_t         *smOrgIORequestBody;
13103   smSatIOContext_t          *satIOContext;
13104   smSatIOContext_t          *satOrgIOContext;
13105   smSatInternalIo_t         *satIntIo;
13106 //  satDeviceData_t          *satDevData;
13107   smDeviceData_t            *oneDeviceData;
13108 
13109   smScsiRspSense_t          *pSense;
13110   smIORequest_t             *smOrgIORequest;
13111 
13112   agsaFisRegHostToDevice_t  *hostToDevFis = agNULL;
13113   bit32                      ataStatus = 0;
13114   smScsiInitiatorRequest_t  *smScsiRequest; /* tiScsiXchg */
13115   smScsiInitiatorRequest_t  *smOrgScsiRequest; /* tiScsiXchg */
13116   agsaFisRegD2HHeader_t     *statDevToHostFisHeader = agNULL;
13117 //  agsaFisRegD2HData_t        statDevToHostFisData;
13118   smIniScsiCmnd_t           *scsiCmnd;
13119   bit8						bSenseKey = 0;
13120   bit16 					bSenseCodeInfo = 0;
13121 
13122 
13123   SM_DBG2(("smsatSMARTRStatusPassCB: start\n"));
13124   SM_DBG5(("smsatSMARTRStatusPassCB:agIORequest=%p agIOStatus=0x%x agIOInfoLen %d\n", agIORequest, agIOStatus, agIOInfoLen));
13125 
13126   /* internally generate smIOContext */
13127   smIORequestBody        = (smIORequestBody_t *)agIORequest->osData;
13128   satIOContext           = (smSatIOContext_t *) ioContext;
13129   satIntIo               = satIOContext->satIntIoContext;
13130   oneDeviceData          = satIOContext->pSatDevData;
13131   hostToDevFis           = satIOContext->pFis;
13132   smRoot                 = oneDeviceData->smRoot;
13133   smIntRoot              = (smIntRoot_t *)smRoot->smData;
13134   smAllShared            = (smIntContext_t *)&smIntRoot->smAllShared;
13135 
13136   if (satIntIo == agNULL)
13137   {
13138     SM_DBG4(("smsatSMARTRStatusPassCB: External smSatInternalIo_t satIntIoContext\n"));
13139     satOrgIOContext = satIOContext;
13140     smOrgIORequest  = smIORequestBody->smIORequest;
13141     pSense          = satOrgIOContext->pSense;
13142     smOrgScsiRequest   = satOrgIOContext->smScsiXchg;
13143      /* ATA command response payload */
13144     smScsiRequest   = satOrgIOContext->smScsiXchg;
13145     scsiCmnd        = satOrgIOContext->pScsiCmnd;
13146 	SM_DBG1((" 0x%02x, 0x%02x, 0x%02x, 0x%02x,\n", scsiCmnd->cdb[0], scsiCmnd->cdb[1],scsiCmnd->cdb[2], scsiCmnd->cdb[3]));
13147 	SM_DBG1((" 0x%02x, 0x%02x, 0x%02x, 0x%02x,\n", scsiCmnd->cdb[4], scsiCmnd->cdb[5],scsiCmnd->cdb[6], scsiCmnd->cdb[7]));
13148 	SM_DBG1((" 0x%02x, 0x%02x, 0x%02x, 0x%02x,\n", scsiCmnd->cdb[8], scsiCmnd->cdb[9],scsiCmnd->cdb[10], scsiCmnd->cdb[11]));
13149 
13150 
13151   }
13152   else
13153   {
13154     SM_DBG4(("smsatSMARTRStatusPassCB: Internal smSatInternalIo_t satIntIoContext\n"));
13155     satOrgIOContext        = satIOContext->satOrgIOContext;
13156     if (satOrgIOContext == agNULL)
13157     {
13158       SM_DBG4(("smsatSMARTRStatusPassCB: satOrgIOContext is NULL\n"));
13159 
13160 	  return;
13161 
13162     }
13163     else
13164     {
13165       SM_DBG4(("smsatSMARTRStatusPassCB: satOrgIOContext is NOT NULL\n"));
13166     }
13167     smOrgIORequestBody     = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
13168     smOrgIORequest         = (smIORequest_t *)smOrgIORequestBody->smIORequest;
13169 
13170     smOrgScsiRequest   = satOrgIOContext->smScsiXchg;
13171     /* ATA command response payload */
13172     smScsiRequest   =  (smScsiInitiatorRequest_t *)&(satIntIo->satIntSmScsiXchg);
13173     scsiCmnd        = satOrgIOContext->pScsiCmnd;
13174 	pSense          = satOrgIOContext->pSense;
13175   }
13176 
13177   smIORequestBody->ioCompleted = agTRUE;
13178   smIORequestBody->ioStarted = agFALSE;
13179 
13180   if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
13181   {
13182     SM_DBG1(("smsatSMARTRStatusPassCB: wrong. agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
13183     tdsmIOCompletedCB(
13184                        smRoot,
13185                        smOrgIORequest,
13186                        smIOFailed,
13187                        smDetailOtherError,
13188                        agNULL,
13189                        satOrgIOContext->interruptContext
13190                       );
13191     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
13192 
13193     smsatFreeIntIoResource( smRoot,
13194                             oneDeviceData,
13195                             satIntIo);
13196     return;
13197   }
13198 
13199   if( agIOStatus != OSSA_IO_SUCCESS)
13200   {
13201 
13202     /* non-data -> device to host  fis are expected */
13203 
13204     statDevToHostFisHeader = (agsaFisRegD2HHeader_t *)&(agFirstDword->D2H);
13205     ataStatus     = statDevToHostFisHeader->status;   /* ATA Status register */
13206 
13207     if ( (statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS) ||
13208          ((ataStatus & ERR_ATA_STATUS_MASK) || (ataStatus & DF_ATA_STATUS_MASK))
13209        )
13210     {
13211       /* for debugging */
13212       if( agIOStatus != OSSA_IO_SUCCESS)
13213       {
13214         SM_DBG1(("smsatSMARTRStatusPassCB: FAILED, NOT IO_SUCCESS!!!\n"));
13215       }
13216       else if (statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS)
13217       {
13218         SM_DBG1(("smsatSMARTRStatusPassCB: FAILED, Wrong FIS type 0x%x!!!\n",statDevToHostFisHeader->fisType));
13219       }
13220       else if ( (ataStatus & ERR_ATA_STATUS_MASK) ||
13221                 (ataStatus & DF_ATA_STATUS_MASK)
13222                )
13223       {
13224         SM_DBG1(("smsatSMARTRStatusPassCB: FAILED, FAILED, error status!!!\n"));
13225       }
13226 
13227       /* Process abort case */
13228       if (agIOStatus == OSSA_IO_ABORTED)
13229       {
13230         smsatProcessAbort(smRoot,
13231                           smOrgIORequest,
13232                           satOrgIOContext
13233                          );
13234 
13235         smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
13236 
13237         smsatFreeIntIoResource( smRoot,
13238                                 oneDeviceData,
13239                                 satIntIo);
13240         return;
13241       }
13242 
13243       smsatTranslateATAErrorsToSCSIErrors(
13244 				agFirstDword->D2H.status,
13245 				agFirstDword->D2H.error,
13246 				&bSenseKey,
13247 				&bSenseCodeInfo
13248 				);
13249       smsatSetSensePayload(pSense, bSenseKey, 0, bSenseCodeInfo, satOrgIOContext);
13250       tdsmIOCompletedCB(smRoot,
13251                         smOrgIORequest,
13252                         smIOSuccess,
13253                         SCSI_STAT_CHECK_CONDITION,
13254                         satOrgIOContext->pSmSenseData,
13255                         satOrgIOContext->interruptContext );
13256 
13257 
13258       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
13259 
13260       smsatFreeIntIoResource( smRoot,
13261                               oneDeviceData,
13262                               satIntIo);
13263       return;
13264 
13265     } /* error checking */
13266   }
13267 
13268   /* prcessing the success case */
13269   SM_DBG5(("smsatSMARTRStatusPassCB: SAT_SMART_RETURN_STATUS success\n"));
13270 
13271   tdsmIOCompletedCB( smRoot,
13272                      smOrgIORequest,
13273                      smIOSuccess,
13274                      SCSI_STAT_GOOD,
13275                      agNULL,
13276                      satOrgIOContext->interruptContext);
13277 
13278 
13279   smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
13280 
13281   smsatFreeIntIoResource( smRoot,
13282                           oneDeviceData,
13283                           satIntIo);
13284 
13285 
13286  return;
13287 }
13288 
13289 osGLOBAL void
13290 smsatSMARTReadLogCB(
13291                 agsaRoot_t        *agRoot,
13292                 agsaIORequest_t   *agIORequest,
13293                 bit32             agIOStatus,
13294                 agsaFisHeader_t   *agFirstDword,
13295                 bit32             agIOInfoLen,
13296                 void              *agParam,
13297                 void              *ioContext
13298                )
13299 {
13300 
13301   smRoot_t                      *smRoot = agNULL;
13302   smIntRoot_t                   *smIntRoot = agNULL;
13303   smIntContext_t                *smAllShared = agNULL;
13304   smIORequestBody_t             *smIORequestBody;
13305   smIORequestBody_t             *smOrgIORequestBody;
13306   smSatIOContext_t              *satIOContext;
13307   smSatIOContext_t              *satOrgIOContext;
13308   smSatInternalIo_t             *satIntIo;
13309 //	satDeviceData_t 		 *satDevData;
13310   smDeviceData_t                *oneDeviceData;
13311 
13312   smScsiRspSense_t              *pSense;
13313   smIORequest_t                 *smOrgIORequest;
13314 
13315   agsaFisRegHostToDevice_t      *hostToDevFis = agNULL;
13316   bit32                         ataStatus = 0;
13317   smScsiInitiatorRequest_t      *smScsiRequest; /* tiScsiXchg */
13318   smScsiInitiatorRequest_t      *smOrgScsiRequest; /* tiScsiXchg */
13319 //	  satReadLogExtSelfTest_t	*virtAddr1;
13320 //	  satSmartReadLogSelfTest_t *virtAddr2;
13321   //bit8						*pLogPage;
13322 //	  bit8						 SelfTestExecutionStatus = 0;
13323 //	  bit32 					 i = 0;
13324 
13325   agsaFisRegD2HHeader_t         *statDevToHostFisHeader = agNULL;
13326 //	  agsaFisRegD2HData_t		 statDevToHostFisData;
13327   smIniScsiCmnd_t               *scsiCmnd;
13328 //	  bit32 					 lenReceived = 0;
13329   bit8                          bSenseKey = 0;
13330   bit16                         bSenseCodeInfo = 0;
13331 
13332   SM_DBG2(("smsatSMARTReadLogCB: start\n"));
13333   SM_DBG5(("smsatSMARTReadLogCB:agIORequest=%p agIOStatus=0x%x agIOInfoLen %d\n", agIORequest, agIOStatus, agIOInfoLen));
13334 
13335   /* internally generate smIOContext */
13336   smIORequestBody		 = (smIORequestBody_t *)agIORequest->osData;
13337   satIOContext			 = (smSatIOContext_t *) ioContext;
13338   satIntIo				 = satIOContext->satIntIoContext;
13339   oneDeviceData 		 = satIOContext->pSatDevData;
13340   hostToDevFis			 = satIOContext->pFis;
13341   smRoot				 = oneDeviceData->smRoot;
13342   smIntRoot 			 = (smIntRoot_t *)smRoot->smData;
13343   smAllShared			 = (smIntContext_t *)&smIntRoot->smAllShared;
13344 
13345   if (satIntIo == agNULL)
13346   {
13347     SM_DBG4(("smsatSMARTReadLogCB: External smSatInternalIo_t satIntIoContext\n"));
13348     satOrgIOContext = satIOContext;
13349     smOrgIORequest	= smIORequestBody->smIORequest;
13350     pSense			= satOrgIOContext->pSense;
13351     smOrgScsiRequest   = satOrgIOContext->smScsiXchg;
13352 
13353     /* ATA command response payload */
13354     smScsiRequest	= satOrgIOContext->smScsiXchg;
13355     scsiCmnd		= satOrgIOContext->pScsiCmnd;
13356 
13357 
13358   }
13359   else
13360   {
13361     SM_DBG4(("smsatSMARTReadLogCB: Internal smSatInternalIo_t satIntIoContext\n"));
13362     satOrgIOContext 	   = satIOContext->satOrgIOContext;
13363     if (satOrgIOContext == agNULL)
13364     {
13365       SM_DBG4(("smsatSMARTReadLogCB: satOrgIOContext is NULL\n"));
13366 
13367       return;
13368 
13369     }
13370     else
13371     {
13372       SM_DBG4(("smsatSMARTReadLogCB: satOrgIOContext is NOT NULL\n"));
13373     }
13374     smOrgIORequestBody	   = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
13375     smOrgIORequest		   = (smIORequest_t *)smOrgIORequestBody->smIORequest;
13376 
13377     pSense		  = satOrgIOContext->pSense;
13378     smOrgScsiRequest   = satOrgIOContext->smScsiXchg;
13379 
13380     /* ATA command response payload */
13381     smScsiRequest	=  (smScsiInitiatorRequest_t *)&(satIntIo->satIntSmScsiXchg);
13382     scsiCmnd		= satOrgIOContext->pScsiCmnd;
13383   }
13384 
13385   smIORequestBody->ioCompleted = agTRUE;
13386   smIORequestBody->ioStarted = agFALSE;
13387 
13388   if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
13389   {
13390     SM_DBG1(("smsatSMARTReadLogCB: wrong. agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
13391     tdsmIOCompletedCB(
13392                       smRoot,
13393                       smOrgIORequest,
13394                       smIOFailed,
13395                       smDetailOtherError,
13396                       agNULL,
13397                       satOrgIOContext->interruptContext
13398                      );
13399     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
13400 
13401     smsatFreeIntIoResource( smRoot,
13402                             oneDeviceData,
13403                             satIntIo);
13404     return;
13405   }
13406 
13407   //for Debuggings
13408   if(agFirstDword != NULL)
13409   {
13410     statDevToHostFisHeader = (agsaFisRegD2HHeader_t *)&(agFirstDword->D2H);
13411     SM_DBG1(("smsatSMARTReadLogCB: statDevToHostFisHeader->status, status %d!!!\n", statDevToHostFisHeader->status));
13412   }
13413   if ((agIOStatus != OSSA_IO_SUCCESS) && (agFirstDword != NULL))
13414   {
13415     /* non-data and pio read -> device to host and pio setup fis are expected */
13416     /*
13417       first, assumed to be Reg Device to Host FIS
13418       This is OK to just find fis type
13419     */
13420     statDevToHostFisHeader = (agsaFisRegD2HHeader_t *)&(agFirstDword->D2H);
13421     ataStatus	  = statDevToHostFisHeader->status;   /* ATA Status register */
13422   }
13423 
13424   if( agIOStatus != OSSA_IO_SUCCESS)
13425   {
13426     if ( ((statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS) &&
13427          (statDevToHostFisHeader->fisType != PIO_SETUP_DEV_TO_HOST_FIS)) ||
13428          ((ataStatus & ERR_ATA_STATUS_MASK) || (ataStatus & DF_ATA_STATUS_MASK))
13429        )
13430     {
13431       /* for debugging */
13432       if( agIOStatus != OSSA_IO_SUCCESS)
13433       {
13434         SM_DBG1(("smsatSMARTReadLogCB: FAILED, NOT IO_SUCCESS!!!\n"));
13435       }
13436       else if (statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS)
13437       {
13438         SM_DBG1(("smsatSMARTReadLogCB: FAILED, Wrong FIS type 0x%x!!!\n",statDevToHostFisHeader->fisType));
13439       }
13440       else if (statDevToHostFisHeader->fisType != PIO_SETUP_DEV_TO_HOST_FIS)
13441       {
13442         SM_DBG1(("smsatSMARTReadLogCB: FAILED, Wrong FIS type 0x%x!!!\n",statDevToHostFisHeader->fisType));
13443       }
13444       else if ( (ataStatus & ERR_ATA_STATUS_MASK) ||
13445                 (ataStatus & DF_ATA_STATUS_MASK)
13446               )
13447       {
13448         SM_DBG1(("smsatSMARTReadLogCB: FAILED, FAILED, error status!!!\n"));
13449       }
13450 
13451       /* Process abort case */
13452       if (agIOStatus == OSSA_IO_ABORTED)
13453       {
13454         smsatProcessAbort(smRoot,
13455                           smOrgIORequest,
13456                           satOrgIOContext
13457                          );
13458 
13459         smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
13460 
13461         smsatFreeIntIoResource( smRoot,
13462 						  oneDeviceData,
13463 						  satIntIo);
13464         return;
13465       }
13466 
13467       /* for debugging */
13468 
13469       if (hostToDevFis->h.command == SAT_SMART)
13470       {
13471         if (hostToDevFis->h.features == SAT_SMART_READ_LOG)
13472         {
13473           SM_DBG1(("smsatSMARTReadLogCB: SAT_SMART_READ_LOG failed!!!\n"));
13474         }
13475         else
13476         {
13477           SM_DBG1(("smsatSMARTReadLogCB: error unknown command 0x%x feature 0x%x!!!\n", hostToDevFis->h.command, hostToDevFis->h.features));
13478         }
13479       }
13480       else
13481       {
13482         SM_DBG1(("smsatSMARTReadLogCB: error default case command 0x%x!!!\n", hostToDevFis->h.command));
13483       }
13484 
13485       smsatTranslateATAErrorsToSCSIErrors(
13486 				agFirstDword->D2H.status,
13487 				agFirstDword->D2H.error,
13488 				&bSenseKey,
13489 				&bSenseCodeInfo
13490 				);
13491       smsatSetSensePayload(pSense, bSenseKey, 0, bSenseCodeInfo, satOrgIOContext);
13492       tdsmIOCompletedCB(smRoot,
13493                         smOrgIORequest,
13494                         smIOSuccess,
13495                         SCSI_STAT_CHECK_CONDITION,
13496                         satOrgIOContext->pSmSenseData,
13497                         satOrgIOContext->interruptContext );
13498 
13499 
13500       smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
13501 
13502       smsatFreeIntIoResource( smRoot,
13503                               oneDeviceData,
13504                               satIntIo);
13505       return;
13506 
13507     } /* error checking */
13508   }
13509 
13510   /* prcessing the success case */
13511 
13512 
13513   tdsmIOCompletedCB( smRoot,
13514                      smOrgIORequest,
13515                      smIOSuccess,
13516                      SCSI_STAT_GOOD,
13517                      agNULL,
13518                      satOrgIOContext->interruptContext);
13519 
13520   smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
13521 
13522   smsatFreeIntIoResource( smRoot,
13523                           oneDeviceData,
13524                           satIntIo);
13525 
13526   return;
13527 }
13528 
13529 osGLOBAL void
13530 smsatPassthroughCB(
13531                 agsaRoot_t        *agRoot,
13532                 agsaIORequest_t   *agIORequest,
13533                 bit32             agIOStatus,
13534                 agsaFisHeader_t   *agFirstDword,
13535                 bit32             agIOInfoLen,
13536                 void              *agParam,
13537                 void              *ioContext
13538                )
13539 {
13540   smRoot_t	                *smRoot = agNULL;
13541   smIntRoot_t			*smIntRoot = agNULL;
13542   smIntContext_t		*smAllShared = agNULL;
13543   smIORequestBody_t 		*smIORequestBody;
13544   smIORequestBody_t 		*smOrgIORequestBody;
13545   smSatIOContext_t		*satIOContext;
13546   smSatIOContext_t		*satOrgIOContext;
13547   smSatInternalIo_t 		*satIntIo;
13548   smDeviceData_t		*oneDeviceData;
13549   smScsiRspSense_t		*pSense;
13550   smIORequest_t 		*smOrgIORequest;
13551   agsaFisRegHostToDevice_t	*hostToDevFis = agNULL;
13552   bit32 			 ataStatus = 0;
13553   smScsiInitiatorRequest_t	*smScsiRequest; /* tiScsiXchg */
13554   smScsiInitiatorRequest_t	*smOrgScsiRequest; /* tiScsiXchg */
13555 
13556   agsaFisRegD2HHeader_t 	*statDevToHostFisHeader = agNULL;
13557   smIniScsiCmnd_t		*scsiCmnd;
13558   bit8				 bSenseKey = 0;
13559   bit16 			 bSenseCodeInfo = 0;
13560 
13561   SM_DBG2(("smsatPassthroughCB: start\n"));
13562   SM_DBG5(("smsatPassthroughCB:agIORequest=%p agIOStatus=0x%x agIOInfoLen %d\n", agIORequest, agIOStatus, agIOInfoLen));
13563 
13564   /* internally generate smIOContext */
13565   smIORequestBody		 = (smIORequestBody_t *)agIORequest->osData;
13566   satIOContext			 = (smSatIOContext_t *) ioContext;
13567   satIntIo			 = satIOContext->satIntIoContext;
13568   oneDeviceData 		 = satIOContext->pSatDevData;
13569   hostToDevFis			 = satIOContext->pFis;
13570   smRoot			 = oneDeviceData->smRoot;
13571   smIntRoot 			 = (smIntRoot_t *)smRoot->smData;
13572   smAllShared			 = (smIntContext_t *)&smIntRoot->smAllShared;
13573 
13574   if (satIntIo == agNULL)
13575   {
13576     SM_DBG4(("smsatPassthroughCB: External smSatInternalIo_t satIntIoContext\n"));
13577     satOrgIOContext = satIOContext;
13578     smOrgIORequest = smIORequestBody->smIORequest;
13579     pSense = satOrgIOContext->pSense;
13580     smOrgScsiRequest   = satOrgIOContext->smScsiXchg;
13581 
13582     /* ATA command response payload */
13583     smScsiRequest	= satOrgIOContext->smScsiXchg;
13584     scsiCmnd		= satOrgIOContext->pScsiCmnd;
13585   }
13586   else
13587   {
13588     SM_DBG4(("smsatPassthroughCB: Internal smSatInternalIo_t satIntIoContext\n"));
13589     satOrgIOContext 	   = satIOContext->satOrgIOContext;
13590     if (satOrgIOContext == agNULL)
13591     {
13592       SM_DBG4(("smsatPassthroughCB: satOrgIOContext is NULL\n"));
13593       return;
13594     }
13595     else
13596     {
13597       SM_DBG4(("smsatPassthroughCB: satOrgIOContext is NOT NULL\n"));
13598     }
13599     smOrgIORequestBody  = (smIORequestBody_t *)satOrgIOContext->smRequestBody;
13600     smOrgIORequest = (smIORequest_t *)smOrgIORequestBody->smIORequest;
13601 
13602     pSense = satOrgIOContext->pSense;
13603     smOrgScsiRequest   = satOrgIOContext->smScsiXchg;
13604 
13605     /* ATA command response payload */
13606     smScsiRequest	=  (smScsiInitiatorRequest_t *)&(satIntIo->satIntSmScsiXchg);
13607     scsiCmnd		= satOrgIOContext->pScsiCmnd;
13608   }
13609 
13610   smIORequestBody->ioCompleted = agTRUE;
13611   smIORequestBody->ioStarted = agFALSE;
13612 
13613 
13614    if (agIOStatus == OSSA_IO_UNDERFLOW)
13615   {
13616     SM_DBG1(("smsatPassthroughCB: IO_UNDERFLOW, status %d!!!\n", agIOStatus));
13617     tdsmIOCompletedCB( smRoot,
13618                        smOrgIORequest,
13619                        smIOUnderRun,
13620                        agIOInfoLen,
13621                        agNULL,
13622                        satOrgIOContext->interruptContext
13623                      );
13624     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
13625 
13626     smsatFreeIntIoResource( smRoot,
13627                             oneDeviceData,
13628                             satIntIo);
13629     return;
13630   }
13631 
13632 
13633   if (agFirstDword == agNULL && agIOStatus != OSSA_IO_SUCCESS)
13634   {
13635     SM_DBG1(("smsatPassthroughCB: wrong. agFirstDword is NULL when error, status %d!!!\n", agIOStatus));
13636     tdsmIOCompletedCB( smRoot,
13637 		       smOrgIORequest,
13638 		       smIOFailed,
13639 		       smDetailOtherError,
13640 		       agNULL,
13641 		       satOrgIOContext->interruptContext
13642 		     );
13643     smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
13644 
13645     smsatFreeIntIoResource( smRoot,
13646                             oneDeviceData,
13647 			    satIntIo);
13648     return;
13649   }
13650 
13651   //for Debuggings
13652 
13653    if ((agIOStatus != OSSA_IO_SUCCESS) && (agFirstDword != NULL))
13654    {
13655      /* non-data and pio read -> device to host and pio setup fis are expected */
13656        /*
13657           first, assumed to be Reg Device to Host FIS
13658 	  This is OK to just find fis type
13659         */
13660      statDevToHostFisHeader = (agsaFisRegD2HHeader_t *)&(agFirstDword->D2H);
13661      ataStatus	  = statDevToHostFisHeader->status;   /* ATA Status register */
13662    }
13663    if( agIOStatus != OSSA_IO_SUCCESS)
13664    {
13665      if ( ((statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS) &&
13666           (statDevToHostFisHeader->fisType != PIO_SETUP_DEV_TO_HOST_FIS)) ||
13667 	  ((ataStatus & ERR_ATA_STATUS_MASK) || (ataStatus & DF_ATA_STATUS_MASK))
13668         )
13669      {
13670        /* for debugging */
13671        if( agIOStatus != OSSA_IO_SUCCESS)
13672        {
13673          SM_DBG1(("smsatPassthroughCB: FAILED, NOT IO_SUCCESS!!!\n"));
13674        }
13675        else if (statDevToHostFisHeader->fisType != REG_DEV_TO_HOST_FIS)
13676        {
13677          SM_DBG1(("smsatPassthroughCB: FAILED, Wrong FIS type 0x%x!!!\n",statDevToHostFisHeader->fisType));
13678        }
13679        else if (statDevToHostFisHeader->fisType != PIO_SETUP_DEV_TO_HOST_FIS)
13680        {
13681          SM_DBG1(("smsatPassthroughCB: FAILED, Wrong FIS type 0x%x!!!\n",statDevToHostFisHeader->fisType));
13682        }
13683        else if ( (ataStatus & ERR_ATA_STATUS_MASK) ||
13684                  (ataStatus & DF_ATA_STATUS_MASK)
13685 	       )
13686        {
13687          SM_DBG1(("smsatPassthroughCB: FAILED, FAILED, error status!!!\n"));
13688        }
13689 
13690        /* Process abort case */
13691        if (agIOStatus == OSSA_IO_ABORTED)
13692        {
13693          smsatProcessAbort( smRoot,
13694 			    smOrgIORequest,
13695 			    satOrgIOContext);
13696 
13697 	 smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
13698 
13699          smsatFreeIntIoResource( smRoot,
13700 	                         oneDeviceData,
13701 				 satIntIo);
13702          return;
13703        }
13704 
13705        smsatTranslateATAErrorsToSCSIErrors( agFirstDword->D2H.status,
13706 					    agFirstDword->D2H.error,
13707 					    &bSenseKey,
13708 					    &bSenseCodeInfo
13709 					  );
13710        smsatSetSensePayload(pSense, bSenseKey, 0, bSenseCodeInfo, satOrgIOContext);
13711        tdsmIOCompletedCB( smRoot,
13712                           smOrgIORequest,
13713 		          smIOSuccess,
13714 			  SCSI_STAT_CHECK_CONDITION,
13715 			  satOrgIOContext->pSmSenseData,
13716 			  satOrgIOContext->interruptContext );
13717 
13718 
13719        smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
13720        smsatFreeIntIoResource( smRoot,
13721                                oneDeviceData,
13722                               satIntIo);
13723        return;
13724 
13725      } /* error checking */
13726    }
13727 
13728    /* prcessing the success case */
13729    if(agFirstDword != NULL)
13730    {
13731      statDevToHostFisHeader = (agsaFisRegD2HHeader_t *)&(agFirstDword->D2H);
13732      SM_DBG1(("smsatPassthroughCB: statDevToHostFisHeader->status, status %d!!!\n", statDevToHostFisHeader->status));
13733      smsatTranslateATAErrorsToSCSIErrors( agFirstDword->D2H.status,
13734 					  agFirstDword->D2H.error,
13735 					  &bSenseKey,
13736 					  &bSenseCodeInfo);
13737      smsatSetSensePayload(pSense, bSenseKey, 0, bSenseCodeInfo, satOrgIOContext);
13738      if(agFirstDword->D2H.status & 0x01)
13739      {
13740        tdsmIOCompletedCB( smRoot,
13741                           smOrgIORequest,
13742 			  smIOSuccess,
13743 			  SCSI_STAT_CHECK_CONDITION,
13744 			  satOrgIOContext->pSmSenseData,
13745 			  satOrgIOContext->interruptContext );
13746        smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
13747        smsatFreeIntIoResource( smRoot,
13748                                oneDeviceData,
13749                                satIntIo);
13750        return;
13751      }
13752    }
13753 
13754   tdsmIOCompletedCB( smRoot,
13755                      smOrgIORequest,
13756 	             smIOSuccess,
13757 	             SCSI_STAT_GOOD,
13758 	             agNULL,
13759 	             satOrgIOContext->interruptContext);
13760 
13761   smsatDecrementPendingIO(smRoot, smAllShared, satIOContext);
13762 
13763   smsatFreeIntIoResource( smRoot,
13764     			  oneDeviceData,
13765 			  satIntIo);
13766 
13767   return;
13768 }
13769 
13770