1285809Sscottl/*******************************************************************************
2285809Sscottl*Copyright (c) 2014 PMC-Sierra, Inc.  All rights reserved.
3285809Sscottl*
4285809Sscottl*Redistribution and use in source and binary forms, with or without modification, are permitted provided
5285809Sscottl*that the following conditions are met:
6285809Sscottl*1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
7285809Sscottl*following disclaimer.
8285809Sscottl*2. Redistributions in binary form must reproduce the above copyright notice,
9285809Sscottl*this list of conditions and the following disclaimer in the documentation and/or other materials provided
10285809Sscottl*with the distribution.
11285809Sscottl*
12285809Sscottl*THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED
13285809Sscottl*WARRANTIES,INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14285809Sscottl*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
15285809Sscottl*FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
16285809Sscottl*NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
17285809Sscottl*BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
18285809Sscottl*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
19285809Sscottl*SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
20285809Sscottl
21285809Sscottl********************************************************************************/
22285809Sscottl/*****************************************************************************/
23285809Sscottl/** \file
24285809Sscottl *
25285809Sscottl * The file implementing LL HW encapsulation for SCSI/ATA Translation (SAT).
26285809Sscottl *
27285809Sscottl */
28285809Sscottl/*****************************************************************************/
29285809Sscottl#include <sys/cdefs.h>
30285809Sscottl__FBSDID("$FreeBSD$");
31285809Sscottl#include <dev/pms/config.h>
32285809Sscottl
33285809Sscottl#include <dev/pms/freebsd/driver/common/osenv.h>
34285809Sscottl#include <dev/pms/freebsd/driver/common/ostypes.h>
35285809Sscottl#include <dev/pms/freebsd/driver/common/osdebug.h>
36285809Sscottl
37285809Sscottl#include <dev/pms/RefTisa/sallsdk/api/sa.h>
38285809Sscottl#include <dev/pms/RefTisa/sallsdk/api/saapi.h>
39285809Sscottl#include <dev/pms/RefTisa/sallsdk/api/saosapi.h>
40285809Sscottl
41285809Sscottl#ifdef SATA_ENABLE
42285809Sscottl
43285809Sscottl#include <dev/pms/RefTisa/tisa/api/titypes.h>
44285809Sscottl#include <dev/pms/RefTisa/tisa/api/ostiapi.h>
45285809Sscottl#include <dev/pms/RefTisa/tisa/api/tiapi.h>
46285809Sscottl#include <dev/pms/RefTisa/tisa/api/tiglobal.h>
47285809Sscottl
48285809Sscottl#ifdef FDS_SM
49285809Sscottl#include <dev/pms/RefTisa/sat/api/sm.h>
50285809Sscottl#include <dev/pms/RefTisa/sat/api/smapi.h>
51285809Sscottl#include <dev/pms/RefTisa/sat/api/tdsmapi.h>
52285809Sscottl#endif
53285809Sscottl
54285809Sscottl#ifdef FDS_DM
55285809Sscottl#include <dev/pms/RefTisa/discovery/api/dm.h>
56285809Sscottl#include <dev/pms/RefTisa/discovery/api/dmapi.h>
57285809Sscottl#include <dev/pms/RefTisa/discovery/api/tddmapi.h>
58285809Sscottl#endif
59285809Sscottl
60285809Sscottl#include <dev/pms/RefTisa/tisa/sassata/sas/common/tdtypes.h>
61285809Sscottl#include <dev/pms/freebsd/driver/common/osstring.h>
62285809Sscottl#include <dev/pms/RefTisa/tisa/sassata/common/tdutil.h>
63285809Sscottl
64285809Sscottl#ifdef INITIATOR_DRIVER
65285809Sscottl#include <dev/pms/RefTisa/tisa/sassata/sas/ini/itdtypes.h>
66285809Sscottl#include <dev/pms/RefTisa/tisa/sassata/sas/ini/itddefs.h>
67285809Sscottl#include <dev/pms/RefTisa/tisa/sassata/sas/ini/itdglobl.h>
68285809Sscottl#endif
69285809Sscottl
70285809Sscottl#ifdef TARGET_DRIVER
71285809Sscottl#include <dev/pms/RefTisa/tisa/sassata/sas/tgt/ttdglobl.h>
72285809Sscottl#include <dev/pms/RefTisa/tisa/sassata/sas/tgt/ttdxchg.h>
73285809Sscottl#include <dev/pms/RefTisa/tisa/sassata/sas/tgt/ttdtypes.h>
74285809Sscottl#endif
75285809Sscottl
76285809Sscottl#include <dev/pms/RefTisa/tisa/sassata/common/tdsatypes.h>
77285809Sscottl#include <dev/pms/RefTisa/tisa/sassata/common/tdproto.h>
78285809Sscottl
79285809Sscottl#include <dev/pms/RefTisa/tisa/sassata/sata/host/sat.h>
80285809Sscottl#include <dev/pms/RefTisa/tisa/sassata/sata/host/satproto.h>
81285809Sscottl
82285809Sscottl/*
83285809Sscottl * This table is used to map LL Layer saSATAStart() status to TISA status.
84285809Sscottl */
85285809Sscottlstatic bit32 mapStat[3] =
86285809Sscottl{
87285809Sscottl  tiSuccess,
88285809Sscottl  tiError,
89285809Sscottl  tiBusy
90285809Sscottl};
91285809Sscottl
92285809Sscottl
93285809Sscottl/*****************************************************************************
94285809Sscottl *! \brief  sataLLIOStart
95285809Sscottl *
96285809Sscottl *   This routine is called to initiate a new SATA request to LL layer.
97285809Sscottl *   This function implements/encapsulates HW and LL API dependency.
98285809Sscottl *
99285809Sscottl *  \param   tiRoot:           Pointer to TISA initiator driver/port instance.
100285809Sscottl *  \param   tiIORequest:      Pointer to TISA I/O request context for this I/O.
101285809Sscottl *  \param   tiDeviceHandle:   Pointer to TISA device handle for this I/O.
102285809Sscottl *  \param   tiScsiRequest:    Pointer to TISA SCSI I/O request and SGL list.
103285809Sscottl *  \param   satIOContext_t:   Pointer to the SAT IO Context
104285809Sscottl *
105285809Sscottl *  \return:
106285809Sscottl *
107285809Sscottl *  \e tiSuccess:     I/O request successfully initiated.
108285809Sscottl *  \e tiBusy:        No resources available, try again later.
109285809Sscottl *  \e tiIONoDevice:  Invalid device handle.
110285809Sscottl *  \e tiError:       Other errors that prevent the I/O request to be started.
111285809Sscottl *
112285809Sscottl *
113285809Sscottl *****************************************************************************/
114285809Sscottl
115285809SscottlGLOBAL bit32  sataLLIOStart (
116285809Sscottl                   tiRoot_t                  *tiRoot,
117285809Sscottl                   tiIORequest_t             *tiIORequest,
118285809Sscottl                   tiDeviceHandle_t          *tiDeviceHandle,
119285809Sscottl                   tiScsiInitiatorRequest_t  *tiScsiRequest,
120285809Sscottl                   satIOContext_t            *satIOContext
121285809Sscottl                  )
122285809Sscottl{
123285809Sscottl
124285809Sscottl  tdsaDeviceData_t            *oneDeviceData;
125285809Sscottl  agsaRoot_t                  *agRoot;
126285809Sscottl  agsaIORequest_t             *agIORequest;
127285809Sscottl  agsaDevHandle_t             *agDevHandle;
128285809Sscottl  bit32                       status;
129285809Sscottl  tdIORequestBody_t           *tdIORequestBody;
130285809Sscottl  agsaSATAInitiatorRequest_t  *agSATAReq;
131285809Sscottl  satDeviceData_t             *pSatDevData;
132285809Sscottl  satInternalIo_t             *satIntIo;
133285809Sscottl  bit32                       RLERecovery = agFALSE;
134285809Sscottl
135285809Sscottl  oneDeviceData   = (tdsaDeviceData_t *)tiDeviceHandle->tdData;
136285809Sscottl  agRoot          = oneDeviceData->agRoot;
137285809Sscottl  agDevHandle     = oneDeviceData->agDevHandle;
138285809Sscottl  tdIORequestBody = (tdIORequestBody_t *)satIOContext->tiRequestBody;
139285809Sscottl  agSATAReq       = &(tdIORequestBody->transport.SATA.agSATARequestBody);
140285809Sscottl  pSatDevData     = satIOContext->pSatDevData;
141285809Sscottl  satIntIo        = satIOContext->satIntIoContext;
142285809Sscottl
143285809Sscottl  /*
144285809Sscottl   * If this is a super I/O request, check for optional settings.
145285809Sscottl   * Be careful. Use the superRequest pointer for all references
146285809Sscottl   * in this block of code.
147285809Sscottl   */
148285809Sscottl  agSATAReq->option = 0;
149285809Sscottl  if (satIOContext->superIOFlag)
150285809Sscottl  {
151285809Sscottl      tiSuperScsiInitiatorRequest_t *superRequest = (tiSuperScsiInitiatorRequest_t *) tiScsiRequest;
152285809Sscottl      agBOOLEAN                 needPlusDataLenAdjustment = agFALSE;
153285809Sscottl      agBOOLEAN                 needMinusDataLenAdjustment = agFALSE;
154285809Sscottl      bit32                     adjusted_length;
155285809Sscottl
156285809Sscottl      if (superRequest->flags & TI_SCSI_INITIATOR_ENCRYPT)
157285809Sscottl      {
158285809Sscottl        /*
159285809Sscottl         * Copy all of the relevant encrypt information
160285809Sscottl         */
161285809Sscottl        agSATAReq->option |= AGSA_SATA_ENABLE_ENCRYPTION;
162285809Sscottl        osti_memcpy(&agSATAReq->encrypt, &superRequest->Encrypt, sizeof(agsaEncrypt_t));
163285809Sscottl      }
164285809Sscottl
165285809Sscottl      if (superRequest->flags & TI_SCSI_INITIATOR_DIF)
166285809Sscottl      {
167285809Sscottl          /*
168285809Sscottl           * Copy all of the relevant DIF information
169285809Sscottl           */
170285809Sscottl          agSATAReq->option |= AGSA_SATA_ENABLE_DIF;
171285809Sscottl          osti_memcpy(&agSATAReq->dif, &superRequest->Dif, sizeof(agsaDif_t));
172285809Sscottl
173285809Sscottl          /*
174285809Sscottl           * Set SGL data len
175285809Sscottl           * XXX This code needs to support more sector sizes
176285809Sscottl           */
177285809Sscottl          if (needPlusDataLenAdjustment == agTRUE)
178285809Sscottl          {
179285809Sscottl              adjusted_length = superRequest->scsiCmnd.expDataLength;
180285809Sscottl              adjusted_length += (adjusted_length/512) * 8;
181285809Sscottl              agSATAReq->dataLength = adjusted_length;
182285809Sscottl          }
183285809Sscottl          else if (needMinusDataLenAdjustment == agTRUE)
184285809Sscottl          {
185285809Sscottl              adjusted_length = superRequest->scsiCmnd.expDataLength;
186285809Sscottl              adjusted_length -= (adjusted_length/520) * 8;
187285809Sscottl              agSATAReq->dataLength = adjusted_length;
188285809Sscottl          }
189285809Sscottl          else
190285809Sscottl          {
191285809Sscottl              /* setting the data length */
192285809Sscottl              agSATAReq->dataLength  = superRequest->scsiCmnd.expDataLength;
193285809Sscottl          }
194285809Sscottl
195285809Sscottl          tdIORequestBody->IOType.InitiatorRegIO.expDataLength = agSATAReq->dataLength;
196285809Sscottl      }
197285809Sscottl      else
198285809Sscottl      {
199285809Sscottl           /* initialize expDataLength */
200285809Sscottl          if (satIOContext->reqType == AGSA_SATA_PROTOCOL_NON_DATA ||
201285809Sscottl              satIOContext->reqType == AGSA_SATA_PROTOCOL_SRST_ASSERT ||
202285809Sscottl              satIOContext->reqType == AGSA_SATA_PROTOCOL_SRST_DEASSERT
203285809Sscottl             )
204285809Sscottl          {
205285809Sscottl              tdIORequestBody->IOType.InitiatorRegIO.expDataLength = 0;
206285809Sscottl          }
207285809Sscottl          else
208285809Sscottl          {
209285809Sscottl              tdIORequestBody->IOType.InitiatorRegIO.expDataLength = tiScsiRequest->scsiCmnd.expDataLength;
210285809Sscottl          }
211285809Sscottl
212285809Sscottl          agSATAReq->dataLength = tdIORequestBody->IOType.InitiatorRegIO.expDataLength;
213285809Sscottl      }
214285809Sscottl  }
215285809Sscottl  else
216285809Sscottl  {
217285809Sscottl      agSATAReq->option = 0;
218285809Sscottl      /* initialize expDataLength */
219285809Sscottl      if (satIOContext->reqType == AGSA_SATA_PROTOCOL_NON_DATA ||
220285809Sscottl          satIOContext->reqType == AGSA_SATA_PROTOCOL_SRST_ASSERT ||
221285809Sscottl          satIOContext->reqType == AGSA_SATA_PROTOCOL_SRST_DEASSERT
222285809Sscottl         )
223285809Sscottl      {
224285809Sscottl          tdIORequestBody->IOType.InitiatorRegIO.expDataLength = 0;
225285809Sscottl      }
226285809Sscottl      else
227285809Sscottl      {
228285809Sscottl          tdIORequestBody->IOType.InitiatorRegIO.expDataLength = tiScsiRequest->scsiCmnd.expDataLength;
229285809Sscottl      }
230285809Sscottl
231285809Sscottl      agSATAReq->dataLength = tdIORequestBody->IOType.InitiatorRegIO.expDataLength;
232285809Sscottl  }
233285809Sscottl
234285809Sscottl  if ( (pSatDevData->satDriveState == SAT_DEV_STATE_IN_RECOVERY) &&
235285809Sscottl       (satIOContext->pFis->h.command == SAT_READ_LOG_EXT)
236285809Sscottl       )
237285809Sscottl   {
238285809Sscottl     RLERecovery = agTRUE;
239285809Sscottl   }
240285809Sscottl
241285809Sscottl  /* check max io */
242285809Sscottl  /* be sure to free */
243285809Sscottl  if ( (pSatDevData->satDriveState != SAT_DEV_STATE_IN_RECOVERY) ||
244285809Sscottl       (RLERecovery == agTRUE)
245285809Sscottl      )
246285809Sscottl  {
247285809Sscottl    if (RLERecovery == agFALSE) /* RLE is not checked against pending IO's */
248285809Sscottl    {
249285809Sscottl      if ( (satIOContext->reqType == AGSA_SATA_PROTOCOL_FPDMA_WRITE) ||
250285809Sscottl           (satIOContext->reqType == AGSA_SATA_PROTOCOL_FPDMA_READ) )
251285809Sscottl      {
252285809Sscottl        if (pSatDevData->satPendingNCQIO >= pSatDevData->satNCQMaxIO ||
253285809Sscottl            pSatDevData->satPendingNONNCQIO != 0)
254285809Sscottl        {
255285809Sscottl          TI_DBG1(("sataLLIOStart: 1st busy NCQ. NCQ Pending %d NONNCQ Pending %d\n", pSatDevData->satPendingNCQIO, pSatDevData->satPendingNONNCQIO));
256285809Sscottl          /* free resource */
257285809Sscottl          satFreeIntIoResource( tiRoot,
258285809Sscottl                                pSatDevData,
259285809Sscottl                                satIntIo);
260285809Sscottl          return tiBusy;
261285809Sscottl        }
262285809Sscottl      }
263285809Sscottl      else
264285809Sscottl      {
265285809Sscottl        if (pSatDevData->satPendingNONNCQIO >= SAT_NONNCQ_MAX ||
266285809Sscottl            pSatDevData->satPendingNCQIO != 0)
267285809Sscottl        {
268285809Sscottl          TI_DBG1(("sataLLIOStart: 2nd busy NON-NCQ. NCQ Pending %d NON-NCQ Pending %d\n", pSatDevData->satPendingNCQIO, pSatDevData->satPendingNONNCQIO));
269285809Sscottl          /* free resource */
270285809Sscottl          satFreeIntIoResource( tiRoot,
271285809Sscottl                                pSatDevData,
272285809Sscottl                                satIntIo);
273285809Sscottl          return tiBusy;
274285809Sscottl        }
275285809Sscottl      }
276285809Sscottl    } /* RLE */
277285809Sscottl    /* for internal SATA command only */
278285809Sscottl    if (satIOContext->satOrgIOContext != agNULL)
279285809Sscottl    {
280285809Sscottl      /* Initialize tiIORequest */
281285809Sscottl      tdIORequestBody->tiIORequest = tiIORequest;
282285809Sscottl    }
283285809Sscottl    /* Initialize tiDevhandle */
284285809Sscottl    tdIORequestBody->tiDevHandle = tiDeviceHandle;
285285809Sscottl
286285809Sscottl    /* Initializes Scatter Gather and ESGL */
287285809Sscottl    status = itdsataIOPrepareSGL( tiRoot,
288285809Sscottl                                  tdIORequestBody,
289285809Sscottl                                  &tiScsiRequest->agSgl1,
290285809Sscottl                                  tiScsiRequest->sglVirtualAddr );
291285809Sscottl
292285809Sscottl    if (status != tiSuccess)
293285809Sscottl    {
294285809Sscottl      TI_DBG1(("sataLLIOStart: can't get SGL\n"));
295285809Sscottl      return status;
296285809Sscottl    }
297285809Sscottl
298285809Sscottl
299285809Sscottl    /* Initialize LL Layer agIORequest */
300285809Sscottl    agIORequest = &(tdIORequestBody->agIORequest);
301285809Sscottl    agIORequest->osData = (void *) tdIORequestBody;
302285809Sscottl    agIORequest->sdkData = agNULL; /* SA takes care of this */
303285809Sscottl
304285809Sscottl    tdIORequestBody->ioStarted = agTRUE;
305285809Sscottl    tdIORequestBody->ioCompleted = agFALSE;
306285809Sscottl
307285809Sscottl    /*
308285809Sscottl
309285809Sscottl  #ifdef PRE_SALL_v033
310285809SscottlGLOBAL bit32 saSATAStart(
311285809Sscottl                        agsaRoot_t      *agRoot,
312285809Sscottl                        agsaIORequest_t *agIORequest,
313285809Sscottl                        agsaDevHandle_t *agDevHandle,
314285809Sscottl                        bit32           agRequestType,
315285809Sscottl                        agsaSATAInitiatorRequest_t  *agSATAReq,
316285809Sscottl                        bit8            *agTag
317285809Sscottl                        );
318285809Sscottl#endif
319285809SscottlGLOBAL bit32 saSATAStart(
320285809Sscottl                        agsaRoot_t                  *agRoot,
321285809Sscottl                        agsaIORequest_t             *agIORequest,
322285809Sscottl                        agsaDevHandle_t             *agDevHandle,
323285809Sscottl                        bit32                       agRequestType,
324285809Sscottl                        agsaSATAInitiatorRequest_t  *agSATAReq,
325285809Sscottl                        bit8                        agTag,
326285809Sscottl                        ossaSATACompletedCB_t       agCB
327285809Sscottl                        );
328285809Sscottl  */
329285809Sscottl
330285809Sscottl    /* assign tag value for SATA */
331285809Sscottl    if ( (satIOContext->reqType == AGSA_SATA_PROTOCOL_FPDMA_WRITE) ||
332285809Sscottl         (satIOContext->reqType == AGSA_SATA_PROTOCOL_FPDMA_READ) )
333285809Sscottl    {
334285809Sscottl      if (agFALSE == satTagAlloc(tiRoot, pSatDevData, &satIOContext->sataTag))
335285809Sscottl      {
336285809Sscottl        TI_DBG1(("sataLLIOStart: No more NCQ tag\n"));
337285809Sscottl        tdIORequestBody->ioStarted = agFALSE;
338285809Sscottl        tdIORequestBody->ioCompleted = agTRUE;
339285809Sscottl        return tiBusy;
340285809Sscottl      }
341285809Sscottl      TI_DBG3(("sataLLIOStart: ncq tag 0x%x\n",satIOContext->sataTag));
342285809Sscottl    }
343285809Sscottl    else
344285809Sscottl    {
345285809Sscottl      satIOContext->sataTag = 0xFF;
346285809Sscottl    }
347285809Sscottl  }
348285809Sscottl  else /* AGSA_SATA_PROTOCOL_SRST_ASSERT or AGSA_SATA_PROTOCOL_SRST_DEASSERT
349285809Sscottl          or SAT_CHECK_POWER_MODE as ABORT */
350285809Sscottl  {
351285809Sscottl    agsaSgl_t          *agSgl;
352285809Sscottl
353285809Sscottl    /* for internal SATA command only */
354285809Sscottl    if (satIOContext->satOrgIOContext != agNULL)
355285809Sscottl    {
356285809Sscottl      /* Initialize tiIORequest */
357285809Sscottl      tdIORequestBody->tiIORequest = tiIORequest;
358285809Sscottl    }
359285809Sscottl    /* Initialize tiDevhandle */
360285809Sscottl    tdIORequestBody->tiDevHandle = tiDeviceHandle;
361285809Sscottl
362285809Sscottl
363285809Sscottl    tdIORequestBody->IOType.InitiatorRegIO.expDataLength = 0;
364285809Sscottl    /* SGL for SATA request */
365285809Sscottl    agSgl = &(tdIORequestBody->transport.SATA.agSATARequestBody.agSgl);
366285809Sscottl    agSgl->len = 0;
367285809Sscottl
368285809Sscottl    agSgl->sgUpper = 0;
369285809Sscottl    agSgl->sgLower = 0;
370285809Sscottl    agSgl->len = 0;
371285809Sscottl    CLEAR_ESGL_EXTEND(agSgl->extReserved);
372285809Sscottl
373285809Sscottl    /* Initialize LL Layer agIORequest */
374285809Sscottl    agIORequest = &(tdIORequestBody->agIORequest);
375285809Sscottl    agIORequest->osData = (void *) tdIORequestBody;
376285809Sscottl    agIORequest->sdkData = agNULL; /* SA takes care of this */
377285809Sscottl
378285809Sscottl    tdIORequestBody->ioStarted = agTRUE;
379285809Sscottl    tdIORequestBody->ioCompleted = agFALSE;
380285809Sscottl
381285809Sscottl    /* setting the data length */
382285809Sscottl    agSATAReq->dataLength = 0;
383285809Sscottl
384285809Sscottl  }
385285809Sscottl
386285809Sscottl  tdIORequestBody->reTries = 0;
387285809Sscottl  osti_memset(agSATAReq->scsiCDB, 0, 16);
388285809Sscottl  osti_memcpy(agSATAReq->scsiCDB, tiScsiRequest->scsiCmnd.cdb, 16);
389285809Sscottl#ifdef TD_INTERNAL_DEBUG
390285809Sscottl  tdhexdump("sataLLIOStart", (bit8 *)satIOContext->pFis, sizeof(agsaFisRegHostToDevice_t));
391285809Sscottl  tdhexdump("sataLLIOStart LL", (bit8 *)&agSATAReq->fis.fisRegHostToDev,
392285809Sscottl            sizeof(agsaFisRegHostToDevice_t));
393285809Sscottl#endif
394285809Sscottl
395285809Sscottl  TI_DBG6(("sataLLIOStart: agDevHandle %p\n", agDevHandle));
396285809Sscottl  status = saSATAStart( agRoot,
397285809Sscottl                        agIORequest,
398285809Sscottl                        tdsaRotateQnumber(tiRoot, oneDeviceData),
399285809Sscottl                        agDevHandle,
400285809Sscottl                        satIOContext->reqType,
401285809Sscottl                        agSATAReq,
402285809Sscottl                        satIOContext->sataTag,
403285809Sscottl                        ossaSATACompleted
404285809Sscottl                        );
405285809Sscottl
406285809Sscottl  if (status == AGSA_RC_SUCCESS)
407285809Sscottl  {
408285809Sscottl    tdsaSingleThreadedEnter(tiRoot, TD_SATA_LOCK);
409285809Sscottl    oneDeviceData->satDevData.satPendingIO++;
410285809Sscottl    if ( (satIOContext->reqType == AGSA_SATA_PROTOCOL_FPDMA_WRITE) ||
411285809Sscottl         (satIOContext->reqType == AGSA_SATA_PROTOCOL_FPDMA_READ) )
412285809Sscottl    {
413285809Sscottl      oneDeviceData->satDevData.satPendingNCQIO++;
414285809Sscottl    }
415285809Sscottl    else
416285809Sscottl    {
417285809Sscottl      oneDeviceData->satDevData.satPendingNONNCQIO++;
418285809Sscottl    }
419285809Sscottl
420285809Sscottl    TDLIST_INIT_ELEMENT (&satIOContext->satIoContextLink);
421285809Sscottl    TDLIST_ENQUEUE_AT_TAIL (&satIOContext->satIoContextLink,
422285809Sscottl                            &oneDeviceData->satDevData.satIoLinkList);
423285809Sscottl    tdsaSingleThreadedLeave(tiRoot, TD_SATA_LOCK);
424285809Sscottl    //    TI_DBG5(("sataLLIOStart: device %p pending IO %d\n", oneDeviceData->satDevData,oneDeviceData->satDevData.satPendingIO));
425285809Sscottl  }
426285809Sscottl  else
427285809Sscottl  {
428285809Sscottl    if (status == AGSA_RC_BUSY)
429285809Sscottl    {
430285809Sscottl      TI_DBG1(("sataLLIOStart: saSATAStart busy\n"));
431285809Sscottl    }
432285809Sscottl    else
433285809Sscottl    {
434285809Sscottl      TI_DBG1(("sataLLIOStart: saSATAStart failed\n"));
435285809Sscottl    }
436285809Sscottl    if ( (satIOContext->reqType == AGSA_SATA_PROTOCOL_FPDMA_WRITE) ||
437285809Sscottl         (satIOContext->reqType == AGSA_SATA_PROTOCOL_FPDMA_READ) )
438285809Sscottl    {
439285809Sscottl      satTagRelease(tiRoot, pSatDevData, satIOContext->sataTag);
440285809Sscottl    }
441285809Sscottl
442285809Sscottl    /* Free the ESGL pages associated with this I/O */
443285809Sscottl    tdIORequestBody->ioStarted = agFALSE;
444285809Sscottl    tdIORequestBody->ioCompleted = agTRUE;
445285809Sscottl    /*
446285809Sscottl     * Map the SAS/SATA LL layer status to the TISA status
447285809Sscottl     */
448285809Sscottl    status = mapStat[status];
449285809Sscottl    return (status);
450285809Sscottl  }
451285809Sscottl
452285809Sscottl  return (tiSuccess);
453285809Sscottl
454285809Sscottl}
455285809Sscottl
456285809Sscottl
457285809Sscottl/*****************************************************************************
458285809Sscottl*! \brief itdsataIOPrepareSGL
459285809Sscottl*
460285809Sscottl*  This function is called to prepare and translate the TISA SGL information
461285809Sscottl*  to the SAS/SATA LL layer specific SGL. This function is similar to
462285809Sscottl*  itdssIOPrepareSGL(), except the request body reflects SATA host request.
463285809Sscottl*
464285809Sscottl*  \param    tiRoot:         Pointer to initiator driver/port instance.
465285809Sscottl*  \param    IORequestBody:  TD layer request body for the I/O.
466285809Sscottl*  \param    tiSgl1:         First TISA SGL info.
467285809Sscottl*  \param    tiSgl2:         Second TISA SGL info.
468285809Sscottl*  \param    sglVirtualAddr: The virtual address of the first element in
469285809Sscottl*                            tiSgl1 when tiSgl1 is used with the type tiSglList.
470285809Sscottl*
471285809Sscottl*  \return:
472285809Sscottl*
473285809Sscottl*  \e tiSuccess:     SGL initialized successfully.
474285809Sscottl*  \e tiError:       Failed to initialize SGL.
475285809Sscottl*
476285809Sscottl*
477285809Sscottl*****************************************************************************/\
478285809SscottlosGLOBAL bit32 itdsataIOPrepareSGL(
479285809Sscottl                  tiRoot_t                 *tiRoot,
480285809Sscottl                  tdIORequestBody_t        *tdIORequestBody,
481285809Sscottl                  tiSgl_t                  *tiSgl1,
482285809Sscottl                  void                     *sglVirtualAddr
483285809Sscottl                  )
484285809Sscottl{
485285809Sscottl  agsaSgl_t          *agSgl;
486285809Sscottl
487285809Sscottl  /* Uppper should be zero-out */
488285809Sscottl  TI_DBG5(("itdsataIOPrepareSGL: start\n"));
489285809Sscottl
490285809Sscottl  TI_DBG5(("itdsataIOPrepareSGL: tiSgl1->upper %d tiSgl1->lower %d tiSgl1->len %d\n",
491285809Sscottl    tiSgl1->upper, tiSgl1->lower, tiSgl1->len));
492285809Sscottl  TI_DBG5(("itdsataIOPrepareSGL: tiSgl1->type %d\n", tiSgl1->type));
493285809Sscottl
494285809Sscottl  /* SGL for SATA request */
495285809Sscottl  agSgl = &(tdIORequestBody->transport.SATA.agSATARequestBody.agSgl);
496285809Sscottl  agSgl->len = 0;
497285809Sscottl
498285809Sscottl  if (tiSgl1 == agNULL)
499285809Sscottl  {
500285809Sscottl    TI_DBG1(("itdsataIOPrepareSGL: Error tiSgl1 is NULL\n"));
501285809Sscottl    return tiError;
502285809Sscottl  }
503285809Sscottl
504285809Sscottl  if (tdIORequestBody->IOType.InitiatorRegIO.expDataLength == 0)
505285809Sscottl  {
506285809Sscottl    TI_DBG3(("itdsataIOPrepareSGL: expDataLength is 0\n"));
507285809Sscottl    agSgl->sgUpper = 0;
508285809Sscottl    agSgl->sgLower = 0;
509285809Sscottl    agSgl->len = 0;
510285809Sscottl    CLEAR_ESGL_EXTEND(agSgl->extReserved);
511285809Sscottl    return tiSuccess;
512285809Sscottl  }
513285809Sscottl
514285809Sscottl  agSgl->sgUpper = tiSgl1->upper;
515285809Sscottl  agSgl->sgLower = tiSgl1->lower;
516285809Sscottl  agSgl->len = tiSgl1->len;
517285809Sscottl  agSgl->extReserved = tiSgl1->type;
518285809Sscottl
519285809Sscottl  return tiSuccess;
520285809Sscottl
521285809Sscottl}
522285809Sscottl
523285809Sscottl/*****************************************************************************
524285809Sscottl *! \brief  sataLLIOAbort
525285809Sscottl *
526285809Sscottl *   This routine is called to initiate an I/O abort to LL layer.
527285809Sscottl *   This function implements/encapsulates HW and LL API dependency.
528285809Sscottl *
529285809Sscottl *  \param   tiRoot:      Pointer to TISA initiator driver/port instance.
530285809Sscottl *  \param   taskTag:     Pointer to TISA I/O context to be aborted.
531285809Sscottl *
532285809Sscottl *  \return:
533285809Sscottl *
534285809Sscottl *  \e tiSuccess:     Abort request was successfully initiated.
535285809Sscottl *  \e tiBusy:        No resources available, try again later.
536285809Sscottl *  \e tiError:       Other errors that prevent the abort request from being
537285809Sscottl *                    started..
538285809Sscottl *
539285809Sscottl *
540285809Sscottl *****************************************************************************/
541285809Sscottl#ifdef REMOVED /* not in use */
542285809SscottlGLOBAL bit32 sataLLIOAbort (
543285809Sscottl                tiRoot_t        *tiRoot,
544285809Sscottl                tiIORequest_t   *taskTag )
545285809Sscottl
546285809Sscottl{
547285809Sscottl  tdsaRoot_t            *tdsaRoot;
548285809Sscottl  tdsaContext_t         *tdsaAllShared;
549285809Sscottl  agsaRoot_t            *agRoot;
550285809Sscottl  tdIORequestBody_t     *tdIORequestBody;
551285809Sscottl  agsaIORequest_t       *agIORequest;
552285809Sscottl  bit32                 status;
553285809Sscottl
554285809Sscottl  TI_DBG2(("sataLLIOAbort: start\n"));
555285809Sscottl
556285809Sscottl  tdsaRoot        = (tdsaRoot_t *) tiRoot->tdData;
557285809Sscottl  tdsaAllShared   = (tdsaContext_t *)&tdsaRoot->tdsaAllShared;
558285809Sscottl  agRoot          = &(tdsaAllShared->agRootNonInt);
559285809Sscottl  tdIORequestBody = (tdIORequestBody_t *)taskTag->tdData;
560285809Sscottl  agIORequest     = &(tdIORequestBody->agIORequest);
561285809Sscottl
562285809Sscottl  status = saSATAAbort(agRoot, 0, agIORequest);
563285809Sscottl
564285809Sscottl  TI_DBG2(("sataLLIOAbort: agIORequest %p\n", agIORequest));
565285809Sscottl  TI_DBG2(("sataLLIOAbort: saSATAAbort returns status, %x\n", status));
566285809Sscottl
567285809Sscottl  if (status == AGSA_RC_SUCCESS)
568285809Sscottl  {
569285809Sscottl    return tiSuccess;
570285809Sscottl  }
571285809Sscottl  else
572285809Sscottl  {
573285809Sscottl    return tiError;
574285809Sscottl  }
575285809Sscottl
576285809Sscottl}
577285809Sscottl#endif
578285809Sscottl
579285809Sscottl#ifdef REMOVED
580285809Sscottl/*****************************************************************************
581285809Sscottl *! \brief  sataLLReset
582285809Sscottl *
583285809Sscottl *   This routine is called to initiate a SATA device reset to LL layer.
584285809Sscottl *   This function implements/encapsulates HW and LL API dependency.
585285809Sscottl *
586285809Sscottl *  \param   tiRoot:           Pointer to TISA initiator driver/port instance.
587285809Sscottl *  \param   tiDeviceHandle:   Pointer to TISA device handle for this I/O.
588285809Sscottl *  \param   option:           SATA device reset option
589285809Sscottl *
590285809Sscottl *  \return: None
591285809Sscottl *
592285809Sscottl *
593285809Sscottl *****************************************************************************/
594285809Sscottl/* not in use */
595285809SscottlGLOBAL  void  sataLLReset(
596285809Sscottl                  tiRoot_t          *tiRoot,
597285809Sscottl                  tiDeviceHandle_t  *tiDeviceHandle,
598285809Sscottl                  bit32             option)
599285809Sscottl{
600285809Sscottl
601285809Sscottl  tdsaRoot_t            *tdsaRoot;
602285809Sscottl  tdsaContext_t         *tdsaAllShared;
603285809Sscottl  tdsaDeviceData_t      *oneDeviceData;
604285809Sscottl  agsaRoot_t            *agRoot;
605285809Sscottl  agsaDevHandle_t       *agDevHandle;
606285809Sscottl
607285809Sscottl  TI_DBG2(("sataLLReset: extry\n"));
608285809Sscottl
609285809Sscottl  tdsaRoot        = (tdsaRoot_t *) tiRoot->tdData;
610285809Sscottl  tdsaAllShared   = (tdsaContext_t *)&tdsaRoot->tdsaAllShared;
611285809Sscottl  agRoot          = &(tdsaAllShared->agRootNonInt);
612285809Sscottl  oneDeviceData   = (tdsaDeviceData_t *)tiDeviceHandle->tdData;
613285809Sscottl  agDevHandle     = oneDeviceData->agDevHandle;
614285809Sscottl
615285809Sscottl  satSATADeviceReset( tiRoot,
616285809Sscottl                      oneDeviceData,
617285809Sscottl                      AGSA_PHY_HARD_RESET);
618285809Sscottl
619285809Sscottl}
620285809Sscottl#endif /* 0 */
621285809Sscottl#endif  /* #ifdef SATA_ENABLE */
622