1285242Sachim/*******************************************************************************
2285242Sachim*Copyright (c) 2014 PMC-Sierra, Inc.  All rights reserved.
3285242Sachim*
4285242Sachim*Redistribution and use in source and binary forms, with or without modification, are permitted provided
5285242Sachim*that the following conditions are met:
6285242Sachim*1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
7285242Sachim*following disclaimer.
8285242Sachim*2. Redistributions in binary form must reproduce the above copyright notice,
9285242Sachim*this list of conditions and the following disclaimer in the documentation and/or other materials provided
10285242Sachim*with the distribution.
11285242Sachim*
12285242Sachim*THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED
13285242Sachim*WARRANTIES,INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14285242Sachim*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
15285242Sachim*FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
16285242Sachim*NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
17285242Sachim*BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
18285242Sachim*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
19285242Sachim*SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
20285242Sachim
21285242Sachim********************************************************************************/
22285242Sachim/*****************************************************************************/
23285242Sachim/** \file
24285242Sachim *
25285242Sachim * The file implementing LL HW encapsulation for SCSI/ATA Translation (SAT).
26285242Sachim *
27285242Sachim */
28285242Sachim/*****************************************************************************/
29285242Sachim#include <sys/cdefs.h>
30285242Sachim__FBSDID("$FreeBSD: releng/11.0/sys/dev/pms/RefTisa/tisa/sassata/sata/host/sathw.c 285242 2015-07-07 13:17:02Z achim $");
31285242Sachim#include <dev/pms/config.h>
32285242Sachim
33285242Sachim#include <dev/pms/freebsd/driver/common/osenv.h>
34285242Sachim#include <dev/pms/freebsd/driver/common/ostypes.h>
35285242Sachim#include <dev/pms/freebsd/driver/common/osdebug.h>
36285242Sachim
37285242Sachim#include <dev/pms/RefTisa/sallsdk/api/sa.h>
38285242Sachim#include <dev/pms/RefTisa/sallsdk/api/saapi.h>
39285242Sachim#include <dev/pms/RefTisa/sallsdk/api/saosapi.h>
40285242Sachim
41285242Sachim#ifdef SATA_ENABLE
42285242Sachim
43285242Sachim#include <dev/pms/RefTisa/tisa/api/titypes.h>
44285242Sachim#include <dev/pms/RefTisa/tisa/api/ostiapi.h>
45285242Sachim#include <dev/pms/RefTisa/tisa/api/tiapi.h>
46285242Sachim#include <dev/pms/RefTisa/tisa/api/tiglobal.h>
47285242Sachim
48285242Sachim#ifdef FDS_SM
49285242Sachim#include <dev/pms/RefTisa/sat/api/sm.h>
50285242Sachim#include <dev/pms/RefTisa/sat/api/smapi.h>
51285242Sachim#include <dev/pms/RefTisa/sat/api/tdsmapi.h>
52285242Sachim#endif
53285242Sachim
54285242Sachim#ifdef FDS_DM
55285242Sachim#include <dev/pms/RefTisa/discovery/api/dm.h>
56285242Sachim#include <dev/pms/RefTisa/discovery/api/dmapi.h>
57285242Sachim#include <dev/pms/RefTisa/discovery/api/tddmapi.h>
58285242Sachim#endif
59285242Sachim
60285242Sachim#include <dev/pms/RefTisa/tisa/sassata/sas/common/tdtypes.h>
61285242Sachim#include <dev/pms/freebsd/driver/common/osstring.h>
62285242Sachim#include <dev/pms/RefTisa/tisa/sassata/common/tdutil.h>
63285242Sachim
64285242Sachim#ifdef INITIATOR_DRIVER
65285242Sachim#include <dev/pms/RefTisa/tisa/sassata/sas/ini/itdtypes.h>
66285242Sachim#include <dev/pms/RefTisa/tisa/sassata/sas/ini/itddefs.h>
67285242Sachim#include <dev/pms/RefTisa/tisa/sassata/sas/ini/itdglobl.h>
68285242Sachim#endif
69285242Sachim
70285242Sachim#ifdef TARGET_DRIVER
71285242Sachim#include <dev/pms/RefTisa/tisa/sassata/sas/tgt/ttdglobl.h>
72285242Sachim#include <dev/pms/RefTisa/tisa/sassata/sas/tgt/ttdxchg.h>
73285242Sachim#include <dev/pms/RefTisa/tisa/sassata/sas/tgt/ttdtypes.h>
74285242Sachim#endif
75285242Sachim
76285242Sachim#include <dev/pms/RefTisa/tisa/sassata/common/tdsatypes.h>
77285242Sachim#include <dev/pms/RefTisa/tisa/sassata/common/tdproto.h>
78285242Sachim
79285242Sachim#include <dev/pms/RefTisa/tisa/sassata/sata/host/sat.h>
80285242Sachim#include <dev/pms/RefTisa/tisa/sassata/sata/host/satproto.h>
81285242Sachim
82285242Sachim/*
83285242Sachim * This table is used to map LL Layer saSATAStart() status to TISA status.
84285242Sachim */
85285242Sachimstatic bit32 mapStat[3] =
86285242Sachim{
87285242Sachim  tiSuccess,
88285242Sachim  tiError,
89285242Sachim  tiBusy
90285242Sachim};
91285242Sachim
92285242Sachim
93285242Sachim/*****************************************************************************
94285242Sachim *! \brief  sataLLIOStart
95285242Sachim *
96285242Sachim *   This routine is called to initiate a new SATA request to LL layer.
97285242Sachim *   This function implements/encapsulates HW and LL API dependency.
98285242Sachim *
99285242Sachim *  \param   tiRoot:           Pointer to TISA initiator driver/port instance.
100285242Sachim *  \param   tiIORequest:      Pointer to TISA I/O request context for this I/O.
101285242Sachim *  \param   tiDeviceHandle:   Pointer to TISA device handle for this I/O.
102285242Sachim *  \param   tiScsiRequest:    Pointer to TISA SCSI I/O request and SGL list.
103285242Sachim *  \param   satIOContext_t:   Pointer to the SAT IO Context
104285242Sachim *
105285242Sachim *  \return:
106285242Sachim *
107285242Sachim *  \e tiSuccess:     I/O request successfully initiated.
108285242Sachim *  \e tiBusy:        No resources available, try again later.
109285242Sachim *  \e tiIONoDevice:  Invalid device handle.
110285242Sachim *  \e tiError:       Other errors that prevent the I/O request to be started.
111285242Sachim *
112285242Sachim *
113285242Sachim *****************************************************************************/
114285242Sachim
115285242SachimGLOBAL bit32  sataLLIOStart (
116285242Sachim                   tiRoot_t                  *tiRoot,
117285242Sachim                   tiIORequest_t             *tiIORequest,
118285242Sachim                   tiDeviceHandle_t          *tiDeviceHandle,
119285242Sachim                   tiScsiInitiatorRequest_t  *tiScsiRequest,
120285242Sachim                   satIOContext_t            *satIOContext
121285242Sachim                  )
122285242Sachim{
123285242Sachim
124285242Sachim  tdsaDeviceData_t            *oneDeviceData;
125285242Sachim  agsaRoot_t                  *agRoot;
126285242Sachim  agsaIORequest_t             *agIORequest;
127285242Sachim  agsaDevHandle_t             *agDevHandle;
128285242Sachim  bit32                       status;
129285242Sachim  tdIORequestBody_t           *tdIORequestBody;
130285242Sachim  agsaSATAInitiatorRequest_t  *agSATAReq;
131285242Sachim  satDeviceData_t             *pSatDevData;
132285242Sachim  satInternalIo_t             *satIntIo;
133285242Sachim  bit32                       RLERecovery = agFALSE;
134285242Sachim
135285242Sachim  oneDeviceData   = (tdsaDeviceData_t *)tiDeviceHandle->tdData;
136285242Sachim  agRoot          = oneDeviceData->agRoot;
137285242Sachim  agDevHandle     = oneDeviceData->agDevHandle;
138285242Sachim  tdIORequestBody = (tdIORequestBody_t *)satIOContext->tiRequestBody;
139285242Sachim  agSATAReq       = &(tdIORequestBody->transport.SATA.agSATARequestBody);
140285242Sachim  pSatDevData     = satIOContext->pSatDevData;
141285242Sachim  satIntIo        = satIOContext->satIntIoContext;
142285242Sachim
143285242Sachim  /*
144285242Sachim   * If this is a super I/O request, check for optional settings.
145285242Sachim   * Be careful. Use the superRequest pointer for all references
146285242Sachim   * in this block of code.
147285242Sachim   */
148285242Sachim  agSATAReq->option = 0;
149285242Sachim  if (satIOContext->superIOFlag)
150285242Sachim  {
151285242Sachim      tiSuperScsiInitiatorRequest_t *superRequest = (tiSuperScsiInitiatorRequest_t *) tiScsiRequest;
152285242Sachim      agBOOLEAN                 needPlusDataLenAdjustment = agFALSE;
153285242Sachim      agBOOLEAN                 needMinusDataLenAdjustment = agFALSE;
154285242Sachim      bit32                     adjusted_length;
155285242Sachim
156285242Sachim      if (superRequest->flags & TI_SCSI_INITIATOR_ENCRYPT)
157285242Sachim      {
158285242Sachim        /*
159285242Sachim         * Copy all of the relevant encrypt information
160285242Sachim         */
161285242Sachim        agSATAReq->option |= AGSA_SATA_ENABLE_ENCRYPTION;
162285242Sachim        osti_memcpy(&agSATAReq->encrypt, &superRequest->Encrypt, sizeof(agsaEncrypt_t));
163285242Sachim      }
164285242Sachim
165285242Sachim      if (superRequest->flags & TI_SCSI_INITIATOR_DIF)
166285242Sachim      {
167285242Sachim          /*
168285242Sachim           * Copy all of the relevant DIF information
169285242Sachim           */
170285242Sachim          agSATAReq->option |= AGSA_SATA_ENABLE_DIF;
171285242Sachim          osti_memcpy(&agSATAReq->dif, &superRequest->Dif, sizeof(agsaDif_t));
172285242Sachim
173285242Sachim          /*
174285242Sachim           * Set SGL data len
175285242Sachim           * XXX This code needs to support more sector sizes
176285242Sachim           */
177285242Sachim          if (needPlusDataLenAdjustment == agTRUE)
178285242Sachim          {
179285242Sachim              adjusted_length = superRequest->scsiCmnd.expDataLength;
180285242Sachim              adjusted_length += (adjusted_length/512) * 8;
181285242Sachim              agSATAReq->dataLength = adjusted_length;
182285242Sachim          }
183285242Sachim          else if (needMinusDataLenAdjustment == agTRUE)
184285242Sachim          {
185285242Sachim              adjusted_length = superRequest->scsiCmnd.expDataLength;
186285242Sachim              adjusted_length -= (adjusted_length/520) * 8;
187285242Sachim              agSATAReq->dataLength = adjusted_length;
188285242Sachim          }
189285242Sachim          else
190285242Sachim          {
191285242Sachim              /* setting the data length */
192285242Sachim              agSATAReq->dataLength  = superRequest->scsiCmnd.expDataLength;
193285242Sachim          }
194285242Sachim
195285242Sachim          tdIORequestBody->IOType.InitiatorRegIO.expDataLength = agSATAReq->dataLength;
196285242Sachim      }
197285242Sachim      else
198285242Sachim      {
199285242Sachim           /* initialize expDataLength */
200285242Sachim          if (satIOContext->reqType == AGSA_SATA_PROTOCOL_NON_DATA ||
201285242Sachim              satIOContext->reqType == AGSA_SATA_PROTOCOL_SRST_ASSERT ||
202285242Sachim              satIOContext->reqType == AGSA_SATA_PROTOCOL_SRST_DEASSERT
203285242Sachim             )
204285242Sachim          {
205285242Sachim              tdIORequestBody->IOType.InitiatorRegIO.expDataLength = 0;
206285242Sachim          }
207285242Sachim          else
208285242Sachim          {
209285242Sachim              tdIORequestBody->IOType.InitiatorRegIO.expDataLength = tiScsiRequest->scsiCmnd.expDataLength;
210285242Sachim          }
211285242Sachim
212285242Sachim          agSATAReq->dataLength = tdIORequestBody->IOType.InitiatorRegIO.expDataLength;
213285242Sachim      }
214285242Sachim  }
215285242Sachim  else
216285242Sachim  {
217285242Sachim      agSATAReq->option = 0;
218285242Sachim      /* initialize expDataLength */
219285242Sachim      if (satIOContext->reqType == AGSA_SATA_PROTOCOL_NON_DATA ||
220285242Sachim          satIOContext->reqType == AGSA_SATA_PROTOCOL_SRST_ASSERT ||
221285242Sachim          satIOContext->reqType == AGSA_SATA_PROTOCOL_SRST_DEASSERT
222285242Sachim         )
223285242Sachim      {
224285242Sachim          tdIORequestBody->IOType.InitiatorRegIO.expDataLength = 0;
225285242Sachim      }
226285242Sachim      else
227285242Sachim      {
228285242Sachim          tdIORequestBody->IOType.InitiatorRegIO.expDataLength = tiScsiRequest->scsiCmnd.expDataLength;
229285242Sachim      }
230285242Sachim
231285242Sachim      agSATAReq->dataLength = tdIORequestBody->IOType.InitiatorRegIO.expDataLength;
232285242Sachim  }
233285242Sachim
234285242Sachim  if ( (pSatDevData->satDriveState == SAT_DEV_STATE_IN_RECOVERY) &&
235285242Sachim       (satIOContext->pFis->h.command == SAT_READ_LOG_EXT)
236285242Sachim       )
237285242Sachim   {
238285242Sachim     RLERecovery = agTRUE;
239285242Sachim   }
240285242Sachim
241285242Sachim  /* check max io */
242285242Sachim  /* be sure to free */
243285242Sachim  if ( (pSatDevData->satDriveState != SAT_DEV_STATE_IN_RECOVERY) ||
244285242Sachim       (RLERecovery == agTRUE)
245285242Sachim      )
246285242Sachim  {
247285242Sachim    if (RLERecovery == agFALSE) /* RLE is not checked against pending IO's */
248285242Sachim    {
249285242Sachim      if ( (satIOContext->reqType == AGSA_SATA_PROTOCOL_FPDMA_WRITE) ||
250285242Sachim           (satIOContext->reqType == AGSA_SATA_PROTOCOL_FPDMA_READ) )
251285242Sachim      {
252285242Sachim        if (pSatDevData->satPendingNCQIO >= pSatDevData->satNCQMaxIO ||
253285242Sachim            pSatDevData->satPendingNONNCQIO != 0)
254285242Sachim        {
255285242Sachim          TI_DBG1(("sataLLIOStart: 1st busy NCQ. NCQ Pending %d NONNCQ Pending %d\n", pSatDevData->satPendingNCQIO, pSatDevData->satPendingNONNCQIO));
256285242Sachim          /* free resource */
257285242Sachim          satFreeIntIoResource( tiRoot,
258285242Sachim                                pSatDevData,
259285242Sachim                                satIntIo);
260285242Sachim          return tiBusy;
261285242Sachim        }
262285242Sachim      }
263285242Sachim      else
264285242Sachim      {
265285242Sachim        if (pSatDevData->satPendingNONNCQIO >= SAT_NONNCQ_MAX ||
266285242Sachim            pSatDevData->satPendingNCQIO != 0)
267285242Sachim        {
268285242Sachim          TI_DBG1(("sataLLIOStart: 2nd busy NON-NCQ. NCQ Pending %d NON-NCQ Pending %d\n", pSatDevData->satPendingNCQIO, pSatDevData->satPendingNONNCQIO));
269285242Sachim          /* free resource */
270285242Sachim          satFreeIntIoResource( tiRoot,
271285242Sachim                                pSatDevData,
272285242Sachim                                satIntIo);
273285242Sachim          return tiBusy;
274285242Sachim        }
275285242Sachim      }
276285242Sachim    } /* RLE */
277285242Sachim    /* for internal SATA command only */
278285242Sachim    if (satIOContext->satOrgIOContext != agNULL)
279285242Sachim    {
280285242Sachim      /* Initialize tiIORequest */
281285242Sachim      tdIORequestBody->tiIORequest = tiIORequest;
282285242Sachim    }
283285242Sachim    /* Initialize tiDevhandle */
284285242Sachim    tdIORequestBody->tiDevHandle = tiDeviceHandle;
285285242Sachim
286285242Sachim    /* Initializes Scatter Gather and ESGL */
287285242Sachim    status = itdsataIOPrepareSGL( tiRoot,
288285242Sachim                                  tdIORequestBody,
289285242Sachim                                  &tiScsiRequest->agSgl1,
290285242Sachim                                  tiScsiRequest->sglVirtualAddr );
291285242Sachim
292285242Sachim    if (status != tiSuccess)
293285242Sachim    {
294285242Sachim      TI_DBG1(("sataLLIOStart: can't get SGL\n"));
295285242Sachim      return status;
296285242Sachim    }
297285242Sachim
298285242Sachim
299285242Sachim    /* Initialize LL Layer agIORequest */
300285242Sachim    agIORequest = &(tdIORequestBody->agIORequest);
301285242Sachim    agIORequest->osData = (void *) tdIORequestBody;
302285242Sachim    agIORequest->sdkData = agNULL; /* SA takes care of this */
303285242Sachim
304285242Sachim    tdIORequestBody->ioStarted = agTRUE;
305285242Sachim    tdIORequestBody->ioCompleted = agFALSE;
306285242Sachim
307285242Sachim    /*
308285242Sachim
309285242Sachim  #ifdef PRE_SALL_v033
310285242SachimGLOBAL bit32 saSATAStart(
311285242Sachim                        agsaRoot_t      *agRoot,
312285242Sachim                        agsaIORequest_t *agIORequest,
313285242Sachim                        agsaDevHandle_t *agDevHandle,
314285242Sachim                        bit32           agRequestType,
315285242Sachim                        agsaSATAInitiatorRequest_t  *agSATAReq,
316285242Sachim                        bit8            *agTag
317285242Sachim                        );
318285242Sachim#endif
319285242SachimGLOBAL bit32 saSATAStart(
320285242Sachim                        agsaRoot_t                  *agRoot,
321285242Sachim                        agsaIORequest_t             *agIORequest,
322285242Sachim                        agsaDevHandle_t             *agDevHandle,
323285242Sachim                        bit32                       agRequestType,
324285242Sachim                        agsaSATAInitiatorRequest_t  *agSATAReq,
325285242Sachim                        bit8                        agTag,
326285242Sachim                        ossaSATACompletedCB_t       agCB
327285242Sachim                        );
328285242Sachim  */
329285242Sachim
330285242Sachim    /* assign tag value for SATA */
331285242Sachim    if ( (satIOContext->reqType == AGSA_SATA_PROTOCOL_FPDMA_WRITE) ||
332285242Sachim         (satIOContext->reqType == AGSA_SATA_PROTOCOL_FPDMA_READ) )
333285242Sachim    {
334285242Sachim      if (agFALSE == satTagAlloc(tiRoot, pSatDevData, &satIOContext->sataTag))
335285242Sachim      {
336285242Sachim        TI_DBG1(("sataLLIOStart: No more NCQ tag\n"));
337285242Sachim        tdIORequestBody->ioStarted = agFALSE;
338285242Sachim        tdIORequestBody->ioCompleted = agTRUE;
339285242Sachim        return tiBusy;
340285242Sachim      }
341285242Sachim      TI_DBG3(("sataLLIOStart: ncq tag 0x%x\n",satIOContext->sataTag));
342285242Sachim    }
343285242Sachim    else
344285242Sachim    {
345285242Sachim      satIOContext->sataTag = 0xFF;
346285242Sachim    }
347285242Sachim  }
348285242Sachim  else /* AGSA_SATA_PROTOCOL_SRST_ASSERT or AGSA_SATA_PROTOCOL_SRST_DEASSERT
349285242Sachim          or SAT_CHECK_POWER_MODE as ABORT */
350285242Sachim  {
351285242Sachim    agsaSgl_t          *agSgl;
352285242Sachim
353285242Sachim    /* for internal SATA command only */
354285242Sachim    if (satIOContext->satOrgIOContext != agNULL)
355285242Sachim    {
356285242Sachim      /* Initialize tiIORequest */
357285242Sachim      tdIORequestBody->tiIORequest = tiIORequest;
358285242Sachim    }
359285242Sachim    /* Initialize tiDevhandle */
360285242Sachim    tdIORequestBody->tiDevHandle = tiDeviceHandle;
361285242Sachim
362285242Sachim
363285242Sachim    tdIORequestBody->IOType.InitiatorRegIO.expDataLength = 0;
364285242Sachim    /* SGL for SATA request */
365285242Sachim    agSgl = &(tdIORequestBody->transport.SATA.agSATARequestBody.agSgl);
366285242Sachim    agSgl->len = 0;
367285242Sachim
368285242Sachim    agSgl->sgUpper = 0;
369285242Sachim    agSgl->sgLower = 0;
370285242Sachim    agSgl->len = 0;
371285242Sachim    CLEAR_ESGL_EXTEND(agSgl->extReserved);
372285242Sachim
373285242Sachim    /* Initialize LL Layer agIORequest */
374285242Sachim    agIORequest = &(tdIORequestBody->agIORequest);
375285242Sachim    agIORequest->osData = (void *) tdIORequestBody;
376285242Sachim    agIORequest->sdkData = agNULL; /* SA takes care of this */
377285242Sachim
378285242Sachim    tdIORequestBody->ioStarted = agTRUE;
379285242Sachim    tdIORequestBody->ioCompleted = agFALSE;
380285242Sachim
381285242Sachim    /* setting the data length */
382285242Sachim    agSATAReq->dataLength = 0;
383285242Sachim
384285242Sachim  }
385285242Sachim
386285242Sachim  tdIORequestBody->reTries = 0;
387285242Sachim  osti_memset(agSATAReq->scsiCDB, 0, 16);
388285242Sachim  osti_memcpy(agSATAReq->scsiCDB, tiScsiRequest->scsiCmnd.cdb, 16);
389285242Sachim#ifdef TD_INTERNAL_DEBUG
390285242Sachim  tdhexdump("sataLLIOStart", (bit8 *)satIOContext->pFis, sizeof(agsaFisRegHostToDevice_t));
391285242Sachim  tdhexdump("sataLLIOStart LL", (bit8 *)&agSATAReq->fis.fisRegHostToDev,
392285242Sachim            sizeof(agsaFisRegHostToDevice_t));
393285242Sachim#endif
394285242Sachim
395285242Sachim  TI_DBG6(("sataLLIOStart: agDevHandle %p\n", agDevHandle));
396285242Sachim  status = saSATAStart( agRoot,
397285242Sachim                        agIORequest,
398285242Sachim                        tdsaRotateQnumber(tiRoot, oneDeviceData),
399285242Sachim                        agDevHandle,
400285242Sachim                        satIOContext->reqType,
401285242Sachim                        agSATAReq,
402285242Sachim                        satIOContext->sataTag,
403285242Sachim                        ossaSATACompleted
404285242Sachim                        );
405285242Sachim
406285242Sachim  if (status == AGSA_RC_SUCCESS)
407285242Sachim  {
408285242Sachim    tdsaSingleThreadedEnter(tiRoot, TD_SATA_LOCK);
409285242Sachim    oneDeviceData->satDevData.satPendingIO++;
410285242Sachim    if ( (satIOContext->reqType == AGSA_SATA_PROTOCOL_FPDMA_WRITE) ||
411285242Sachim         (satIOContext->reqType == AGSA_SATA_PROTOCOL_FPDMA_READ) )
412285242Sachim    {
413285242Sachim      oneDeviceData->satDevData.satPendingNCQIO++;
414285242Sachim    }
415285242Sachim    else
416285242Sachim    {
417285242Sachim      oneDeviceData->satDevData.satPendingNONNCQIO++;
418285242Sachim    }
419285242Sachim
420285242Sachim    TDLIST_INIT_ELEMENT (&satIOContext->satIoContextLink);
421285242Sachim    TDLIST_ENQUEUE_AT_TAIL (&satIOContext->satIoContextLink,
422285242Sachim                            &oneDeviceData->satDevData.satIoLinkList);
423285242Sachim    tdsaSingleThreadedLeave(tiRoot, TD_SATA_LOCK);
424285242Sachim    //    TI_DBG5(("sataLLIOStart: device %p pending IO %d\n", oneDeviceData->satDevData,oneDeviceData->satDevData.satPendingIO));
425285242Sachim  }
426285242Sachim  else
427285242Sachim  {
428285242Sachim    if (status == AGSA_RC_BUSY)
429285242Sachim    {
430285242Sachim      TI_DBG1(("sataLLIOStart: saSATAStart busy\n"));
431285242Sachim    }
432285242Sachim    else
433285242Sachim    {
434285242Sachim      TI_DBG1(("sataLLIOStart: saSATAStart failed\n"));
435285242Sachim    }
436285242Sachim    if ( (satIOContext->reqType == AGSA_SATA_PROTOCOL_FPDMA_WRITE) ||
437285242Sachim         (satIOContext->reqType == AGSA_SATA_PROTOCOL_FPDMA_READ) )
438285242Sachim    {
439285242Sachim      satTagRelease(tiRoot, pSatDevData, satIOContext->sataTag);
440285242Sachim    }
441285242Sachim
442285242Sachim    /* Free the ESGL pages associated with this I/O */
443285242Sachim    tdIORequestBody->ioStarted = agFALSE;
444285242Sachim    tdIORequestBody->ioCompleted = agTRUE;
445285242Sachim    /*
446285242Sachim     * Map the SAS/SATA LL layer status to the TISA status
447285242Sachim     */
448285242Sachim    status = mapStat[status];
449285242Sachim    return (status);
450285242Sachim  }
451285242Sachim
452285242Sachim  return (tiSuccess);
453285242Sachim
454285242Sachim}
455285242Sachim
456285242Sachim
457285242Sachim/*****************************************************************************
458285242Sachim*! \brief itdsataIOPrepareSGL
459285242Sachim*
460285242Sachim*  This function is called to prepare and translate the TISA SGL information
461285242Sachim*  to the SAS/SATA LL layer specific SGL. This function is similar to
462285242Sachim*  itdssIOPrepareSGL(), except the request body reflects SATA host request.
463285242Sachim*
464285242Sachim*  \param    tiRoot:         Pointer to initiator driver/port instance.
465285242Sachim*  \param    IORequestBody:  TD layer request body for the I/O.
466285242Sachim*  \param    tiSgl1:         First TISA SGL info.
467285242Sachim*  \param    tiSgl2:         Second TISA SGL info.
468285242Sachim*  \param    sglVirtualAddr: The virtual address of the first element in
469285242Sachim*                            tiSgl1 when tiSgl1 is used with the type tiSglList.
470285242Sachim*
471285242Sachim*  \return:
472285242Sachim*
473285242Sachim*  \e tiSuccess:     SGL initialized successfully.
474285242Sachim*  \e tiError:       Failed to initialize SGL.
475285242Sachim*
476285242Sachim*
477285242Sachim*****************************************************************************/\
478285242SachimosGLOBAL bit32 itdsataIOPrepareSGL(
479285242Sachim                  tiRoot_t                 *tiRoot,
480285242Sachim                  tdIORequestBody_t        *tdIORequestBody,
481285242Sachim                  tiSgl_t                  *tiSgl1,
482285242Sachim                  void                     *sglVirtualAddr
483285242Sachim                  )
484285242Sachim{
485285242Sachim  agsaSgl_t          *agSgl;
486285242Sachim
487285242Sachim  /* Uppper should be zero-out */
488285242Sachim  TI_DBG5(("itdsataIOPrepareSGL: start\n"));
489285242Sachim
490285242Sachim  TI_DBG5(("itdsataIOPrepareSGL: tiSgl1->upper %d tiSgl1->lower %d tiSgl1->len %d\n",
491285242Sachim    tiSgl1->upper, tiSgl1->lower, tiSgl1->len));
492285242Sachim  TI_DBG5(("itdsataIOPrepareSGL: tiSgl1->type %d\n", tiSgl1->type));
493285242Sachim
494285242Sachim  /* SGL for SATA request */
495285242Sachim  agSgl = &(tdIORequestBody->transport.SATA.agSATARequestBody.agSgl);
496285242Sachim  agSgl->len = 0;
497285242Sachim
498285242Sachim  if (tiSgl1 == agNULL)
499285242Sachim  {
500285242Sachim    TI_DBG1(("itdsataIOPrepareSGL: Error tiSgl1 is NULL\n"));
501285242Sachim    return tiError;
502285242Sachim  }
503285242Sachim
504285242Sachim  if (tdIORequestBody->IOType.InitiatorRegIO.expDataLength == 0)
505285242Sachim  {
506285242Sachim    TI_DBG3(("itdsataIOPrepareSGL: expDataLength is 0\n"));
507285242Sachim    agSgl->sgUpper = 0;
508285242Sachim    agSgl->sgLower = 0;
509285242Sachim    agSgl->len = 0;
510285242Sachim    CLEAR_ESGL_EXTEND(agSgl->extReserved);
511285242Sachim    return tiSuccess;
512285242Sachim  }
513285242Sachim
514285242Sachim  agSgl->sgUpper = tiSgl1->upper;
515285242Sachim  agSgl->sgLower = tiSgl1->lower;
516285242Sachim  agSgl->len = tiSgl1->len;
517285242Sachim  agSgl->extReserved = tiSgl1->type;
518285242Sachim
519285242Sachim  return tiSuccess;
520285242Sachim
521285242Sachim}
522285242Sachim
523285242Sachim/*****************************************************************************
524285242Sachim *! \brief  sataLLIOAbort
525285242Sachim *
526285242Sachim *   This routine is called to initiate an I/O abort to LL layer.
527285242Sachim *   This function implements/encapsulates HW and LL API dependency.
528285242Sachim *
529285242Sachim *  \param   tiRoot:      Pointer to TISA initiator driver/port instance.
530285242Sachim *  \param   taskTag:     Pointer to TISA I/O context to be aborted.
531285242Sachim *
532285242Sachim *  \return:
533285242Sachim *
534285242Sachim *  \e tiSuccess:     Abort request was successfully initiated.
535285242Sachim *  \e tiBusy:        No resources available, try again later.
536285242Sachim *  \e tiError:       Other errors that prevent the abort request from being
537285242Sachim *                    started..
538285242Sachim *
539285242Sachim *
540285242Sachim *****************************************************************************/
541285242Sachim#ifdef REMOVED /* not in use */
542285242SachimGLOBAL bit32 sataLLIOAbort (
543285242Sachim                tiRoot_t        *tiRoot,
544285242Sachim                tiIORequest_t   *taskTag )
545285242Sachim
546285242Sachim{
547285242Sachim  tdsaRoot_t            *tdsaRoot;
548285242Sachim  tdsaContext_t         *tdsaAllShared;
549285242Sachim  agsaRoot_t            *agRoot;
550285242Sachim  tdIORequestBody_t     *tdIORequestBody;
551285242Sachim  agsaIORequest_t       *agIORequest;
552285242Sachim  bit32                 status;
553285242Sachim
554285242Sachim  TI_DBG2(("sataLLIOAbort: start\n"));
555285242Sachim
556285242Sachim  tdsaRoot        = (tdsaRoot_t *) tiRoot->tdData;
557285242Sachim  tdsaAllShared   = (tdsaContext_t *)&tdsaRoot->tdsaAllShared;
558285242Sachim  agRoot          = &(tdsaAllShared->agRootNonInt);
559285242Sachim  tdIORequestBody = (tdIORequestBody_t *)taskTag->tdData;
560285242Sachim  agIORequest     = &(tdIORequestBody->agIORequest);
561285242Sachim
562285242Sachim  status = saSATAAbort(agRoot, 0, agIORequest);
563285242Sachim
564285242Sachim  TI_DBG2(("sataLLIOAbort: agIORequest %p\n", agIORequest));
565285242Sachim  TI_DBG2(("sataLLIOAbort: saSATAAbort returns status, %x\n", status));
566285242Sachim
567285242Sachim  if (status == AGSA_RC_SUCCESS)
568285242Sachim  {
569285242Sachim    return tiSuccess;
570285242Sachim  }
571285242Sachim  else
572285242Sachim  {
573285242Sachim    return tiError;
574285242Sachim  }
575285242Sachim
576285242Sachim}
577285242Sachim#endif
578285242Sachim
579285242Sachim#ifdef REMOVED
580285242Sachim/*****************************************************************************
581285242Sachim *! \brief  sataLLReset
582285242Sachim *
583285242Sachim *   This routine is called to initiate a SATA device reset to LL layer.
584285242Sachim *   This function implements/encapsulates HW and LL API dependency.
585285242Sachim *
586285242Sachim *  \param   tiRoot:           Pointer to TISA initiator driver/port instance.
587285242Sachim *  \param   tiDeviceHandle:   Pointer to TISA device handle for this I/O.
588285242Sachim *  \param   option:           SATA device reset option
589285242Sachim *
590285242Sachim *  \return: None
591285242Sachim *
592285242Sachim *
593285242Sachim *****************************************************************************/
594285242Sachim/* not in use */
595285242SachimGLOBAL  void  sataLLReset(
596285242Sachim                  tiRoot_t          *tiRoot,
597285242Sachim                  tiDeviceHandle_t  *tiDeviceHandle,
598285242Sachim                  bit32             option)
599285242Sachim{
600285242Sachim
601285242Sachim  tdsaRoot_t            *tdsaRoot;
602285242Sachim  tdsaContext_t         *tdsaAllShared;
603285242Sachim  tdsaDeviceData_t      *oneDeviceData;
604285242Sachim  agsaRoot_t            *agRoot;
605285242Sachim  agsaDevHandle_t       *agDevHandle;
606285242Sachim
607285242Sachim  TI_DBG2(("sataLLReset: extry\n"));
608285242Sachim
609285242Sachim  tdsaRoot        = (tdsaRoot_t *) tiRoot->tdData;
610285242Sachim  tdsaAllShared   = (tdsaContext_t *)&tdsaRoot->tdsaAllShared;
611285242Sachim  agRoot          = &(tdsaAllShared->agRootNonInt);
612285242Sachim  oneDeviceData   = (tdsaDeviceData_t *)tiDeviceHandle->tdData;
613285242Sachim  agDevHandle     = oneDeviceData->agDevHandle;
614285242Sachim
615285242Sachim  satSATADeviceReset( tiRoot,
616285242Sachim                      oneDeviceData,
617285242Sachim                      AGSA_PHY_HARD_RESET);
618285242Sachim
619285242Sachim}
620285242Sachim#endif /* 0 */
621285242Sachim#endif  /* #ifdef SATA_ENABLE */
622