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