1 /*******************************************************************************
2 *Copyright (c) 2014 PMC-Sierra, Inc. All rights reserved.
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
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.
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
21 ********************************************************************************/
22 /*****************************************************************************/
25 * The file implementing LL HW encapsulation for SCSI/ATA Translation (SAT).
28 /*****************************************************************************/
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 #include <dev/pms/config.h>
33 #include <dev/pms/freebsd/driver/common/osenv.h>
34 #include <dev/pms/freebsd/driver/common/ostypes.h>
35 #include <dev/pms/freebsd/driver/common/osdebug.h>
37 #include <dev/pms/RefTisa/sallsdk/api/sa.h>
38 #include <dev/pms/RefTisa/sallsdk/api/saapi.h>
39 #include <dev/pms/RefTisa/sallsdk/api/saosapi.h>
43 #include <dev/pms/RefTisa/tisa/api/titypes.h>
44 #include <dev/pms/RefTisa/tisa/api/ostiapi.h>
45 #include <dev/pms/RefTisa/tisa/api/tiapi.h>
46 #include <dev/pms/RefTisa/tisa/api/tiglobal.h>
49 #include <dev/pms/RefTisa/sat/api/sm.h>
50 #include <dev/pms/RefTisa/sat/api/smapi.h>
51 #include <dev/pms/RefTisa/sat/api/tdsmapi.h>
55 #include <dev/pms/RefTisa/discovery/api/dm.h>
56 #include <dev/pms/RefTisa/discovery/api/dmapi.h>
57 #include <dev/pms/RefTisa/discovery/api/tddmapi.h>
60 #include <dev/pms/RefTisa/tisa/sassata/sas/common/tdtypes.h>
61 #include <dev/pms/freebsd/driver/common/osstring.h>
62 #include <dev/pms/RefTisa/tisa/sassata/common/tdutil.h>
64 #ifdef INITIATOR_DRIVER
65 #include <dev/pms/RefTisa/tisa/sassata/sas/ini/itdtypes.h>
66 #include <dev/pms/RefTisa/tisa/sassata/sas/ini/itddefs.h>
67 #include <dev/pms/RefTisa/tisa/sassata/sas/ini/itdglobl.h>
71 #include <dev/pms/RefTisa/tisa/sassata/sas/tgt/ttdglobl.h>
72 #include <dev/pms/RefTisa/tisa/sassata/sas/tgt/ttdxchg.h>
73 #include <dev/pms/RefTisa/tisa/sassata/sas/tgt/ttdtypes.h>
76 #include <dev/pms/RefTisa/tisa/sassata/common/tdsatypes.h>
77 #include <dev/pms/RefTisa/tisa/sassata/common/tdproto.h>
79 #include <dev/pms/RefTisa/tisa/sassata/sata/host/sat.h>
80 #include <dev/pms/RefTisa/tisa/sassata/sata/host/satproto.h>
83 * This table is used to map LL Layer saSATAStart() status to TISA status.
85 static bit32 mapStat[3] =
93 /*****************************************************************************
94 *! \brief sataLLIOStart
96 * This routine is called to initiate a new SATA request to LL layer.
97 * This function implements/encapsulates HW and LL API dependency.
99 * \param tiRoot: Pointer to TISA initiator driver/port instance.
100 * \param tiIORequest: Pointer to TISA I/O request context for this I/O.
101 * \param tiDeviceHandle: Pointer to TISA device handle for this I/O.
102 * \param tiScsiRequest: Pointer to TISA SCSI I/O request and SGL list.
103 * \param satIOContext_t: Pointer to the SAT IO Context
107 * \e tiSuccess: I/O request successfully initiated.
108 * \e tiBusy: No resources available, try again later.
109 * \e tiIONoDevice: Invalid device handle.
110 * \e tiError: Other errors that prevent the I/O request to be started.
113 *****************************************************************************/
115 GLOBAL bit32 sataLLIOStart (
117 tiIORequest_t *tiIORequest,
118 tiDeviceHandle_t *tiDeviceHandle,
119 tiScsiInitiatorRequest_t *tiScsiRequest,
120 satIOContext_t *satIOContext
124 tdsaDeviceData_t *oneDeviceData;
126 agsaIORequest_t *agIORequest;
127 agsaDevHandle_t *agDevHandle;
129 tdIORequestBody_t *tdIORequestBody;
130 agsaSATAInitiatorRequest_t *agSATAReq;
131 satDeviceData_t *pSatDevData;
132 satInternalIo_t *satIntIo;
133 bit32 RLERecovery = agFALSE;
135 oneDeviceData = (tdsaDeviceData_t *)tiDeviceHandle->tdData;
136 agRoot = oneDeviceData->agRoot;
137 agDevHandle = oneDeviceData->agDevHandle;
138 tdIORequestBody = (tdIORequestBody_t *)satIOContext->tiRequestBody;
139 agSATAReq = &(tdIORequestBody->transport.SATA.agSATARequestBody);
140 pSatDevData = satIOContext->pSatDevData;
141 satIntIo = satIOContext->satIntIoContext;
144 * If this is a super I/O request, check for optional settings.
145 * Be careful. Use the superRequest pointer for all references
146 * in this block of code.
148 agSATAReq->option = 0;
149 if (satIOContext->superIOFlag)
151 tiSuperScsiInitiatorRequest_t *superRequest = (tiSuperScsiInitiatorRequest_t *) tiScsiRequest;
152 agBOOLEAN needPlusDataLenAdjustment = agFALSE;
153 agBOOLEAN needMinusDataLenAdjustment = agFALSE;
154 bit32 adjusted_length;
156 if (superRequest->flags & TI_SCSI_INITIATOR_ENCRYPT)
159 * Copy all of the relevant encrypt information
161 agSATAReq->option |= AGSA_SATA_ENABLE_ENCRYPTION;
162 osti_memcpy(&agSATAReq->encrypt, &superRequest->Encrypt, sizeof(agsaEncrypt_t));
165 if (superRequest->flags & TI_SCSI_INITIATOR_DIF)
168 * Copy all of the relevant DIF information
170 agSATAReq->option |= AGSA_SATA_ENABLE_DIF;
171 osti_memcpy(&agSATAReq->dif, &superRequest->Dif, sizeof(agsaDif_t));
175 * XXX This code needs to support more sector sizes
177 if (needPlusDataLenAdjustment == agTRUE)
179 adjusted_length = superRequest->scsiCmnd.expDataLength;
180 adjusted_length += (adjusted_length/512) * 8;
181 agSATAReq->dataLength = adjusted_length;
183 else if (needMinusDataLenAdjustment == agTRUE)
185 adjusted_length = superRequest->scsiCmnd.expDataLength;
186 adjusted_length -= (adjusted_length/520) * 8;
187 agSATAReq->dataLength = adjusted_length;
191 /* setting the data length */
192 agSATAReq->dataLength = superRequest->scsiCmnd.expDataLength;
195 tdIORequestBody->IOType.InitiatorRegIO.expDataLength = agSATAReq->dataLength;
199 /* initialize expDataLength */
200 if (satIOContext->reqType == AGSA_SATA_PROTOCOL_NON_DATA ||
201 satIOContext->reqType == AGSA_SATA_PROTOCOL_SRST_ASSERT ||
202 satIOContext->reqType == AGSA_SATA_PROTOCOL_SRST_DEASSERT
205 tdIORequestBody->IOType.InitiatorRegIO.expDataLength = 0;
209 tdIORequestBody->IOType.InitiatorRegIO.expDataLength = tiScsiRequest->scsiCmnd.expDataLength;
212 agSATAReq->dataLength = tdIORequestBody->IOType.InitiatorRegIO.expDataLength;
217 agSATAReq->option = 0;
218 /* initialize expDataLength */
219 if (satIOContext->reqType == AGSA_SATA_PROTOCOL_NON_DATA ||
220 satIOContext->reqType == AGSA_SATA_PROTOCOL_SRST_ASSERT ||
221 satIOContext->reqType == AGSA_SATA_PROTOCOL_SRST_DEASSERT
224 tdIORequestBody->IOType.InitiatorRegIO.expDataLength = 0;
228 tdIORequestBody->IOType.InitiatorRegIO.expDataLength = tiScsiRequest->scsiCmnd.expDataLength;
231 agSATAReq->dataLength = tdIORequestBody->IOType.InitiatorRegIO.expDataLength;
234 if ( (pSatDevData->satDriveState == SAT_DEV_STATE_IN_RECOVERY) &&
235 (satIOContext->pFis->h.command == SAT_READ_LOG_EXT)
238 RLERecovery = agTRUE;
242 /* be sure to free */
243 if ( (pSatDevData->satDriveState != SAT_DEV_STATE_IN_RECOVERY) ||
244 (RLERecovery == agTRUE)
247 if (RLERecovery == agFALSE) /* RLE is not checked against pending IO's */
249 if ( (satIOContext->reqType == AGSA_SATA_PROTOCOL_FPDMA_WRITE) ||
250 (satIOContext->reqType == AGSA_SATA_PROTOCOL_FPDMA_READ) )
252 if (pSatDevData->satPendingNCQIO >= pSatDevData->satNCQMaxIO ||
253 pSatDevData->satPendingNONNCQIO != 0)
255 TI_DBG1(("sataLLIOStart: 1st busy NCQ. NCQ Pending %d NONNCQ Pending %d\n", pSatDevData->satPendingNCQIO, pSatDevData->satPendingNONNCQIO));
257 satFreeIntIoResource( tiRoot,
265 if (pSatDevData->satPendingNONNCQIO >= SAT_NONNCQ_MAX ||
266 pSatDevData->satPendingNCQIO != 0)
268 TI_DBG1(("sataLLIOStart: 2nd busy NON-NCQ. NCQ Pending %d NON-NCQ Pending %d\n", pSatDevData->satPendingNCQIO, pSatDevData->satPendingNONNCQIO));
270 satFreeIntIoResource( tiRoot,
277 /* for internal SATA command only */
278 if (satIOContext->satOrgIOContext != agNULL)
280 /* Initialize tiIORequest */
281 tdIORequestBody->tiIORequest = tiIORequest;
283 /* Initialize tiDevhandle */
284 tdIORequestBody->tiDevHandle = tiDeviceHandle;
286 /* Initializes Scatter Gather and ESGL */
287 status = itdsataIOPrepareSGL( tiRoot,
289 &tiScsiRequest->agSgl1,
290 tiScsiRequest->sglVirtualAddr );
292 if (status != tiSuccess)
294 TI_DBG1(("sataLLIOStart: can't get SGL\n"));
299 /* Initialize LL Layer agIORequest */
300 agIORequest = &(tdIORequestBody->agIORequest);
301 agIORequest->osData = (void *) tdIORequestBody;
302 agIORequest->sdkData = agNULL; /* SA takes care of this */
304 tdIORequestBody->ioStarted = agTRUE;
305 tdIORequestBody->ioCompleted = agFALSE;
310 GLOBAL bit32 saSATAStart(
312 agsaIORequest_t *agIORequest,
313 agsaDevHandle_t *agDevHandle,
315 agsaSATAInitiatorRequest_t *agSATAReq,
319 GLOBAL bit32 saSATAStart(
321 agsaIORequest_t *agIORequest,
322 agsaDevHandle_t *agDevHandle,
324 agsaSATAInitiatorRequest_t *agSATAReq,
326 ossaSATACompletedCB_t agCB
330 /* assign tag value for SATA */
331 if ( (satIOContext->reqType == AGSA_SATA_PROTOCOL_FPDMA_WRITE) ||
332 (satIOContext->reqType == AGSA_SATA_PROTOCOL_FPDMA_READ) )
334 if (agFALSE == satTagAlloc(tiRoot, pSatDevData, &satIOContext->sataTag))
336 TI_DBG1(("sataLLIOStart: No more NCQ tag\n"));
337 tdIORequestBody->ioStarted = agFALSE;
338 tdIORequestBody->ioCompleted = agTRUE;
341 TI_DBG3(("sataLLIOStart: ncq tag 0x%x\n",satIOContext->sataTag));
345 satIOContext->sataTag = 0xFF;
348 else /* AGSA_SATA_PROTOCOL_SRST_ASSERT or AGSA_SATA_PROTOCOL_SRST_DEASSERT
349 or SAT_CHECK_POWER_MODE as ABORT */
353 /* for internal SATA command only */
354 if (satIOContext->satOrgIOContext != agNULL)
356 /* Initialize tiIORequest */
357 tdIORequestBody->tiIORequest = tiIORequest;
359 /* Initialize tiDevhandle */
360 tdIORequestBody->tiDevHandle = tiDeviceHandle;
363 tdIORequestBody->IOType.InitiatorRegIO.expDataLength = 0;
364 /* SGL for SATA request */
365 agSgl = &(tdIORequestBody->transport.SATA.agSATARequestBody.agSgl);
371 CLEAR_ESGL_EXTEND(agSgl->extReserved);
373 /* Initialize LL Layer agIORequest */
374 agIORequest = &(tdIORequestBody->agIORequest);
375 agIORequest->osData = (void *) tdIORequestBody;
376 agIORequest->sdkData = agNULL; /* SA takes care of this */
378 tdIORequestBody->ioStarted = agTRUE;
379 tdIORequestBody->ioCompleted = agFALSE;
381 /* setting the data length */
382 agSATAReq->dataLength = 0;
386 tdIORequestBody->reTries = 0;
387 osti_memset(agSATAReq->scsiCDB, 0, 16);
388 osti_memcpy(agSATAReq->scsiCDB, tiScsiRequest->scsiCmnd.cdb, 16);
389 #ifdef TD_INTERNAL_DEBUG
390 tdhexdump("sataLLIOStart", (bit8 *)satIOContext->pFis, sizeof(agsaFisRegHostToDevice_t));
391 tdhexdump("sataLLIOStart LL", (bit8 *)&agSATAReq->fis.fisRegHostToDev,
392 sizeof(agsaFisRegHostToDevice_t));
395 TI_DBG6(("sataLLIOStart: agDevHandle %p\n", agDevHandle));
396 status = saSATAStart( agRoot,
398 tdsaRotateQnumber(tiRoot, oneDeviceData),
400 satIOContext->reqType,
402 satIOContext->sataTag,
406 if (status == AGSA_RC_SUCCESS)
408 tdsaSingleThreadedEnter(tiRoot, TD_SATA_LOCK);
409 oneDeviceData->satDevData.satPendingIO++;
410 if ( (satIOContext->reqType == AGSA_SATA_PROTOCOL_FPDMA_WRITE) ||
411 (satIOContext->reqType == AGSA_SATA_PROTOCOL_FPDMA_READ) )
413 oneDeviceData->satDevData.satPendingNCQIO++;
417 oneDeviceData->satDevData.satPendingNONNCQIO++;
420 TDLIST_INIT_ELEMENT (&satIOContext->satIoContextLink);
421 TDLIST_ENQUEUE_AT_TAIL (&satIOContext->satIoContextLink,
422 &oneDeviceData->satDevData.satIoLinkList);
423 tdsaSingleThreadedLeave(tiRoot, TD_SATA_LOCK);
424 // TI_DBG5(("sataLLIOStart: device %p pending IO %d\n", oneDeviceData->satDevData,oneDeviceData->satDevData.satPendingIO));
428 if (status == AGSA_RC_BUSY)
430 TI_DBG1(("sataLLIOStart: saSATAStart busy\n"));
434 TI_DBG1(("sataLLIOStart: saSATAStart failed\n"));
436 if ( (satIOContext->reqType == AGSA_SATA_PROTOCOL_FPDMA_WRITE) ||
437 (satIOContext->reqType == AGSA_SATA_PROTOCOL_FPDMA_READ) )
439 satTagRelease(tiRoot, pSatDevData, satIOContext->sataTag);
442 /* Free the ESGL pages associated with this I/O */
443 tdIORequestBody->ioStarted = agFALSE;
444 tdIORequestBody->ioCompleted = agTRUE;
446 * Map the SAS/SATA LL layer status to the TISA status
448 status = mapStat[status];
457 /*****************************************************************************
458 *! \brief itdsataIOPrepareSGL
460 * This function is called to prepare and translate the TISA SGL information
461 * to the SAS/SATA LL layer specific SGL. This function is similar to
462 * itdssIOPrepareSGL(), except the request body reflects SATA host request.
464 * \param tiRoot: Pointer to initiator driver/port instance.
465 * \param IORequestBody: TD layer request body for the I/O.
466 * \param tiSgl1: First TISA SGL info.
467 * \param tiSgl2: Second TISA SGL info.
468 * \param sglVirtualAddr: The virtual address of the first element in
469 * tiSgl1 when tiSgl1 is used with the type tiSglList.
473 * \e tiSuccess: SGL initialized successfully.
474 * \e tiError: Failed to initialize SGL.
477 *****************************************************************************/\
478 osGLOBAL bit32 itdsataIOPrepareSGL(
480 tdIORequestBody_t *tdIORequestBody,
487 /* Uppper should be zero-out */
488 TI_DBG5(("itdsataIOPrepareSGL: start\n"));
490 TI_DBG5(("itdsataIOPrepareSGL: tiSgl1->upper %d tiSgl1->lower %d tiSgl1->len %d\n",
491 tiSgl1->upper, tiSgl1->lower, tiSgl1->len));
492 TI_DBG5(("itdsataIOPrepareSGL: tiSgl1->type %d\n", tiSgl1->type));
494 /* SGL for SATA request */
495 agSgl = &(tdIORequestBody->transport.SATA.agSATARequestBody.agSgl);
498 if (tiSgl1 == agNULL)
500 TI_DBG1(("itdsataIOPrepareSGL: Error tiSgl1 is NULL\n"));
504 if (tdIORequestBody->IOType.InitiatorRegIO.expDataLength == 0)
506 TI_DBG3(("itdsataIOPrepareSGL: expDataLength is 0\n"));
510 CLEAR_ESGL_EXTEND(agSgl->extReserved);
514 agSgl->sgUpper = tiSgl1->upper;
515 agSgl->sgLower = tiSgl1->lower;
516 agSgl->len = tiSgl1->len;
517 agSgl->extReserved = tiSgl1->type;
523 /*****************************************************************************
524 *! \brief sataLLIOAbort
526 * This routine is called to initiate an I/O abort to LL layer.
527 * This function implements/encapsulates HW and LL API dependency.
529 * \param tiRoot: Pointer to TISA initiator driver/port instance.
530 * \param taskTag: Pointer to TISA I/O context to be aborted.
534 * \e tiSuccess: Abort request was successfully initiated.
535 * \e tiBusy: No resources available, try again later.
536 * \e tiError: Other errors that prevent the abort request from being
540 *****************************************************************************/
541 #ifdef REMOVED /* not in use */
542 GLOBAL bit32 sataLLIOAbort (
544 tiIORequest_t *taskTag )
547 tdsaRoot_t *tdsaRoot;
548 tdsaContext_t *tdsaAllShared;
550 tdIORequestBody_t *tdIORequestBody;
551 agsaIORequest_t *agIORequest;
554 TI_DBG2(("sataLLIOAbort: start\n"));
556 tdsaRoot = (tdsaRoot_t *) tiRoot->tdData;
557 tdsaAllShared = (tdsaContext_t *)&tdsaRoot->tdsaAllShared;
558 agRoot = &(tdsaAllShared->agRootNonInt);
559 tdIORequestBody = (tdIORequestBody_t *)taskTag->tdData;
560 agIORequest = &(tdIORequestBody->agIORequest);
562 status = saSATAAbort(agRoot, 0, agIORequest);
564 TI_DBG2(("sataLLIOAbort: agIORequest %p\n", agIORequest));
565 TI_DBG2(("sataLLIOAbort: saSATAAbort returns status, %x\n", status));
567 if (status == AGSA_RC_SUCCESS)
580 /*****************************************************************************
581 *! \brief sataLLReset
583 * This routine is called to initiate a SATA device reset to LL layer.
584 * This function implements/encapsulates HW and LL API dependency.
586 * \param tiRoot: Pointer to TISA initiator driver/port instance.
587 * \param tiDeviceHandle: Pointer to TISA device handle for this I/O.
588 * \param option: SATA device reset option
593 *****************************************************************************/
595 GLOBAL void sataLLReset(
597 tiDeviceHandle_t *tiDeviceHandle,
601 tdsaRoot_t *tdsaRoot;
602 tdsaContext_t *tdsaAllShared;
603 tdsaDeviceData_t *oneDeviceData;
605 agsaDevHandle_t *agDevHandle;
607 TI_DBG2(("sataLLReset: extry\n"));
609 tdsaRoot = (tdsaRoot_t *) tiRoot->tdData;
610 tdsaAllShared = (tdsaContext_t *)&tdsaRoot->tdsaAllShared;
611 agRoot = &(tdsaAllShared->agRootNonInt);
612 oneDeviceData = (tdsaDeviceData_t *)tiDeviceHandle->tdData;
613 agDevHandle = oneDeviceData->agDevHandle;
615 satSATADeviceReset( tiRoot,
617 AGSA_PHY_HARD_RESET);
621 #endif /* #ifdef SATA_ENABLE */