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