xref: /freebsd/sys/dev/hptmv/entry.c (revision 2ef735f477367c1e47801c6e091a2b08e66813c9)
1718cf2ccSPedro F. Giffuni /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3718cf2ccSPedro F. Giffuni  *
4d2bd3ab9SScott Long  * Copyright (c) 2004-2005 HighPoint Technologies, Inc.
51713e81bSScott Long  * All rights reserved.
61713e81bSScott Long  *
71713e81bSScott Long  * Redistribution and use in source and binary forms, with or without
81713e81bSScott Long  * modification, are permitted provided that the following conditions
91713e81bSScott Long  * are met:
101713e81bSScott Long  * 1. Redistributions of source code must retain the above copyright
111713e81bSScott Long  *    notice, this list of conditions and the following disclaimer.
121713e81bSScott Long  * 2. Redistributions in binary form must reproduce the above copyright
131713e81bSScott Long  *    notice, this list of conditions and the following disclaimer in the
141713e81bSScott Long  *    documentation and/or other materials provided with the distribution.
151713e81bSScott Long  *
161713e81bSScott Long  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
171713e81bSScott Long  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
181713e81bSScott Long  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
191713e81bSScott Long  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
201713e81bSScott Long  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
211713e81bSScott Long  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
221713e81bSScott Long  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
231713e81bSScott Long  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
241713e81bSScott Long  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251713e81bSScott Long  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261713e81bSScott Long  * SUCH DAMAGE.
271713e81bSScott Long  */
28d2bd3ab9SScott Long 
29f3b080e6SMarius Strobl #include <sys/cdefs.h>
30f3b080e6SMarius Strobl __FBSDID("$FreeBSD$");
31f3b080e6SMarius Strobl 
321713e81bSScott Long #include <sys/param.h>
331713e81bSScott Long #include <sys/systm.h>
341713e81bSScott Long #include <sys/kernel.h>
351713e81bSScott Long #include <sys/bus.h>
361713e81bSScott Long #include <sys/malloc.h>
371713e81bSScott Long #include <sys/resource.h>
381713e81bSScott Long #include <sys/time.h>
391713e81bSScott Long #include <sys/callout.h>
401713e81bSScott Long #include <sys/signalvar.h>
411713e81bSScott Long #include <sys/eventhandler.h>
421713e81bSScott Long #include <sys/proc.h>
431713e81bSScott Long #include <sys/kthread.h>
441713e81bSScott Long 
45d2bd3ab9SScott Long #include <sys/mutex.h>
46d2bd3ab9SScott Long #include <sys/module.h>
4749b3fc40SJohn Baldwin #include <sys/sx.h>
48d2bd3ab9SScott Long 
491713e81bSScott Long #include <dev/pci/pcireg.h>
501713e81bSScott Long #include <dev/pci/pcivar.h>
51d2bd3ab9SScott Long 
52d2bd3ab9SScott Long #ifndef __KERNEL__
53d2bd3ab9SScott Long #define __KERNEL__
54d2bd3ab9SScott Long #endif
551713e81bSScott Long 
561713e81bSScott Long #include <dev/hptmv/global.h>
571713e81bSScott Long #include <dev/hptmv/hptintf.h>
581713e81bSScott Long #include <dev/hptmv/osbsd.h>
59f7f3900bSScott Long #include <dev/hptmv/access601.h>
601713e81bSScott Long 
61d2bd3ab9SScott Long 
621713e81bSScott Long #ifdef DEBUG
631713e81bSScott Long #ifdef DEBUG_LEVEL
641713e81bSScott Long int hpt_dbg_level = DEBUG_LEVEL;
651713e81bSScott Long #else
661713e81bSScott Long int hpt_dbg_level = 0;
671713e81bSScott Long #endif
681713e81bSScott Long #endif
691713e81bSScott Long 
701713e81bSScott Long #define MV_ERROR printf
71d2bd3ab9SScott Long 
721713e81bSScott Long /*
731713e81bSScott Long  * CAM SIM entry points
741713e81bSScott Long  */
751713e81bSScott Long static int 	hpt_probe (device_t dev);
76d2bd3ab9SScott Long static void launch_worker_thread(void);
771713e81bSScott Long static int 	hpt_attach(device_t dev);
781713e81bSScott Long static int 	hpt_detach(device_t dev);
791713e81bSScott Long static int 	hpt_shutdown(device_t dev);
801713e81bSScott Long static void hpt_poll(struct cam_sim *sim);
811713e81bSScott Long static void hpt_intr(void *arg);
82d2bd3ab9SScott Long static void hpt_async(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg);
831713e81bSScott Long static void hpt_action(struct cam_sim *sim, union ccb *ccb);
84d2bd3ab9SScott Long 
85d2bd3ab9SScott Long static device_method_t driver_methods[] = {
86d2bd3ab9SScott Long 	/* Device interface */
87d2bd3ab9SScott Long 	DEVMETHOD(device_probe,		hpt_probe),
88d2bd3ab9SScott Long 	DEVMETHOD(device_attach,	hpt_attach),
89d2bd3ab9SScott Long 	DEVMETHOD(device_detach,	hpt_detach),
90d2bd3ab9SScott Long 
91cd3ef666SXin LI 	DEVMETHOD(device_shutdown,	hpt_shutdown),
92f3b080e6SMarius Strobl 	DEVMETHOD_END
93d2bd3ab9SScott Long };
94d2bd3ab9SScott Long 
95d2bd3ab9SScott Long static driver_t hpt_pci_driver = {
96d2bd3ab9SScott Long 	__str(PROC_DIR_NAME),
97d2bd3ab9SScott Long 	driver_methods,
98d2bd3ab9SScott Long 	sizeof(IAL_ADAPTER_T)
99d2bd3ab9SScott Long };
100d2bd3ab9SScott Long 
101d2bd3ab9SScott Long static devclass_t	hpt_devclass;
102d2bd3ab9SScott Long 
103d2bd3ab9SScott Long #define __DRIVER_MODULE(p1, p2, p3, p4, p5, p6) DRIVER_MODULE(p1, p2, p3, p4, p5, p6)
104d2bd3ab9SScott Long __DRIVER_MODULE(PROC_DIR_NAME, pci, hpt_pci_driver, hpt_devclass, 0, 0);
105d45ce511SEitan Adler MODULE_DEPEND(PROC_DIR_NAME, cam, 1, 1, 1);
106d2bd3ab9SScott Long 
107d2bd3ab9SScott Long #define ccb_ccb_ptr spriv_ptr0
108d2bd3ab9SScott Long #define ccb_adapter ccb_h.spriv_ptr1
109d2bd3ab9SScott Long 
1101713e81bSScott Long static void SetInquiryData(PINQUIRYDATA inquiryData, PVDevice pVDev);
1111713e81bSScott Long static void HPTLIBAPI OsSendCommand (_VBUS_ARG union ccb * ccb);
1121713e81bSScott Long static void HPTLIBAPI fOsCommandDone(_VBUS_ARG PCommand pCmd);
1131713e81bSScott Long static void ccb_done(union ccb *ccb);
1141713e81bSScott Long static void hpt_queue_ccb(union ccb **ccb_Q, union ccb *ccb);
1151713e81bSScott Long static void hpt_free_ccb(union ccb **ccb_Q, union ccb *ccb);
11649b3fc40SJohn Baldwin static void hpt_intr_locked(IAL_ADAPTER_T *pAdapter);
1171713e81bSScott Long static void	hptmv_free_edma_queues(IAL_ADAPTER_T *pAdapter);
1181713e81bSScott Long static void	hptmv_free_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum);
1191713e81bSScott Long static void	handleEdmaError(_VBUS_ARG PCommand pCmd);
1201713e81bSScott Long static int	hptmv_init_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum);
1211713e81bSScott Long static int	fResetActiveCommands(PVBus _vbus_p);
1221713e81bSScott Long static void	fRegisterVdevice(IAL_ADAPTER_T *pAdapter);
1231713e81bSScott Long static int	hptmv_allocate_edma_queues(IAL_ADAPTER_T *pAdapter);
1241713e81bSScott Long static void	hptmv_handle_event_disconnect(void *data);
1251713e81bSScott Long static void	hptmv_handle_event_connect(void *data);
1261713e81bSScott Long static int	start_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum);
1271713e81bSScott Long static void	init_vdev_params(IAL_ADAPTER_T *pAdapter, MV_U8 channel);
1281713e81bSScott Long static int	hptmv_parse_identify_results(MV_SATA_CHANNEL *pMvSataChannel);
1291713e81bSScott Long static int HPTLIBAPI fOsBuildSgl(_VBUS_ARG PCommand pCmd, FPSCAT_GATH pSg,
1301713e81bSScott Long     int logical);
1311713e81bSScott Long static MV_BOOLEAN CommandCompletionCB(MV_SATA_ADAPTER *pMvSataAdapter,
1321713e81bSScott Long     MV_U8 channelNum, MV_COMPLETION_TYPE comp_type, MV_VOID_PTR commandId,
1331713e81bSScott Long     MV_U16 responseFlags, MV_U32 timeStamp,
1341713e81bSScott Long     MV_STORAGE_DEVICE_REGISTERS *registerStruct);
1351713e81bSScott Long static MV_BOOLEAN hptmv_event_notify(MV_SATA_ADAPTER *pMvSataAdapter,
1361713e81bSScott Long     MV_EVENT_TYPE eventType, MV_U32 param1, MV_U32 param2);
1371713e81bSScott Long 
1381713e81bSScott Long #define ccb_ccb_ptr spriv_ptr0
1391713e81bSScott Long #define ccb_adapter ccb_h.spriv_ptr1
1401713e81bSScott Long 
14149b3fc40SJohn Baldwin static struct sx hptmv_list_lock;
14249b3fc40SJohn Baldwin SX_SYSINIT(hptmv_list_lock, &hptmv_list_lock, "hptmv list");
1434d24901aSPedro F. Giffuni IAL_ADAPTER_T *gIal_Adapter = NULL;
1444d24901aSPedro F. Giffuni IAL_ADAPTER_T *pCurAdapter = NULL;
145d2bd3ab9SScott Long static MV_SATA_CHANNEL gMvSataChannels[MAX_VBUS][MV_SATA_CHANNELS_NUM];
1461713e81bSScott Long 
1471713e81bSScott Long typedef struct st_HPT_DPC {
1481713e81bSScott Long 	IAL_ADAPTER_T *pAdapter;
1491713e81bSScott Long 	void (*dpc)(IAL_ADAPTER_T *, void *, UCHAR);
1501713e81bSScott Long 	void *arg;
1511713e81bSScott Long 	UCHAR flags;
1521713e81bSScott Long } ST_HPT_DPC;
1531713e81bSScott Long 
1541713e81bSScott Long #define MAX_DPC 16
1551713e81bSScott Long UCHAR DPC_Request_Nums = 0;
1561713e81bSScott Long static ST_HPT_DPC DpcQueue[MAX_DPC];
1571713e81bSScott Long static int DpcQueue_First=0;
1581713e81bSScott Long static int DpcQueue_Last = 0;
15949b3fc40SJohn Baldwin static struct mtx DpcQueue_Lock;
16049b3fc40SJohn Baldwin MTX_SYSINIT(hpmtv_dpc_lock, &DpcQueue_Lock, "hptmv dpc", MTX_DEF);
1611713e81bSScott Long 
16264470755SXin LI char DRIVER_VERSION[] = "v1.16";
1631713e81bSScott Long 
1641713e81bSScott Long /*******************************************************************************
1651713e81bSScott Long  *	Name:	hptmv_free_channel
1661713e81bSScott Long  *
1671713e81bSScott Long  *	Description:	free allocated queues for the given channel
1681713e81bSScott Long  *
169453130d9SPedro F. Giffuni  *	Parameters:    	pMvSataAdapter - pointer to the RR18xx controller this
1701713e81bSScott Long  * 					channel connected to.
1711713e81bSScott Long  *			channelNum - channel number.
1721713e81bSScott Long  *
1731713e81bSScott Long  ******************************************************************************/
1741713e81bSScott Long static void
1751713e81bSScott Long hptmv_free_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum)
1761713e81bSScott Long {
1771713e81bSScott Long 	HPT_ASSERT(channelNum < MV_SATA_CHANNELS_NUM);
1781713e81bSScott Long 	pAdapter->mvSataAdapter.sataChannel[channelNum] = NULL;
179d2bd3ab9SScott Long }
1801713e81bSScott Long 
181d2bd3ab9SScott Long static void failDevice(PVDevice pVDev)
1821713e81bSScott Long {
183d2bd3ab9SScott Long 	PVBus _vbus_p = pVDev->pVBus;
184d2bd3ab9SScott Long 	IAL_ADAPTER_T *pAdapter = (IAL_ADAPTER_T *)_vbus_p->OsExt;
185d2bd3ab9SScott Long 
1861713e81bSScott Long 	pVDev->u.disk.df_on_line = 0;
1871713e81bSScott Long 	pVDev->vf_online = 0;
188d2bd3ab9SScott Long 	if (pVDev->pfnDeviceFailed)
189d2bd3ab9SScott Long 		CallWhenIdle(_VBUS_P (DPC_PROC)pVDev->pfnDeviceFailed, pVDev);
190d2bd3ab9SScott Long 
191d2bd3ab9SScott Long 	fNotifyGUI(ET_DEVICE_REMOVED, pVDev);
192d2bd3ab9SScott Long 
193d2bd3ab9SScott Long #ifndef FOR_DEMO
194d2bd3ab9SScott Long 	if (pAdapter->ver_601==2 && !pAdapter->beeping) {
195d2bd3ab9SScott Long 		pAdapter->beeping = 1;
196d2bd3ab9SScott Long 		BeepOn(pAdapter->mvSataAdapter.adapterIoBaseAddress);
197d2bd3ab9SScott Long 		set_fail_led(&pAdapter->mvSataAdapter, pVDev->u.disk.mv->channelNumber, 1);
1981713e81bSScott Long 	}
199d2bd3ab9SScott Long #endif
2001713e81bSScott Long }
2011713e81bSScott Long 
2021713e81bSScott Long int MvSataResetChannel(MV_SATA_ADAPTER *pMvSataAdapter, MV_U8 channel);
2031713e81bSScott Long 
2041713e81bSScott Long static void
2051713e81bSScott Long handleEdmaError(_VBUS_ARG PCommand pCmd)
2061713e81bSScott Long {
2071713e81bSScott Long 	PDevice pDevice = &pCmd->pVDevice->u.disk;
2081713e81bSScott Long 	MV_SATA_ADAPTER * pSataAdapter = pDevice->mv->mvSataAdapter;
2091713e81bSScott Long 
2101713e81bSScott Long 	if (!pDevice->df_on_line) {
2111713e81bSScott Long 		KdPrint(("Device is offline"));
2121713e81bSScott Long 		pCmd->Result = RETURN_BAD_DEVICE;
2131713e81bSScott Long 		CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
2141713e81bSScott Long 		return;
2151713e81bSScott Long 	}
2161713e81bSScott Long 
2171713e81bSScott Long 	if (pCmd->RetryCount++>5) {
218d2bd3ab9SScott Long 		hpt_printk(("too many retries on channel(%d)\n", pDevice->mv->channelNumber));
219d2bd3ab9SScott Long failed:
220d2bd3ab9SScott Long 		failDevice(pCmd->pVDevice);
2211713e81bSScott Long 		pCmd->Result = RETURN_IDE_ERROR;
2221713e81bSScott Long 		CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
2231713e81bSScott Long 		return;
2241713e81bSScott Long 	}
225d2bd3ab9SScott Long 
226d2bd3ab9SScott Long 	/* reset the channel and retry the command */
227d2bd3ab9SScott Long 	if (MvSataResetChannel(pSataAdapter, pDevice->mv->channelNumber))
228d2bd3ab9SScott Long 		goto failed;
229d2bd3ab9SScott Long 
230d2bd3ab9SScott Long 	fNotifyGUI(ET_DEVICE_ERROR, Map2pVDevice(pDevice));
231d2bd3ab9SScott Long 
232d2bd3ab9SScott Long 	hpt_printk(("Retry on channel(%d)\n", pDevice->mv->channelNumber));
2331713e81bSScott Long 	fDeviceSendCommand(_VBUS_P pCmd);
2341713e81bSScott Long }
2351713e81bSScott Long 
2361713e81bSScott Long /****************************************************************
2371713e81bSScott Long  *	Name:	hptmv_init_channel
2381713e81bSScott Long  *
239d2bd3ab9SScott Long  *	Description:	allocate request and response queues for the EDMA of the
240d2bd3ab9SScott Long  *					given channel and sets other fields.
241d2bd3ab9SScott Long  *
2421713e81bSScott Long  *	Parameters:
2431713e81bSScott Long  *		pAdapter - pointer to the emulated adapter data structure
2441713e81bSScott Long  *		channelNum - channel number.
2451713e81bSScott Long  *	Return:	0 on success, otherwise on failure
2461713e81bSScott Long  ****************************************************************/
2471713e81bSScott Long static int
2481713e81bSScott Long hptmv_init_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum)
2491713e81bSScott Long {
2501713e81bSScott Long 	MV_SATA_CHANNEL *pMvSataChannel;
2511713e81bSScott Long 	dma_addr_t    req_dma_addr;
2521713e81bSScott Long 	dma_addr_t    rsp_dma_addr;
2531713e81bSScott Long 
2541713e81bSScott Long 	if (channelNum >= MV_SATA_CHANNELS_NUM)
2551713e81bSScott Long 	{
25664470755SXin LI 		MV_ERROR("RR18xx[%d]: Bad channelNum=%d",
2571713e81bSScott Long 				 pAdapter->mvSataAdapter.adapterId, channelNum);
2581713e81bSScott Long 		return -1;
2591713e81bSScott Long 	}
2601713e81bSScott Long 
261d2bd3ab9SScott Long 	pMvSataChannel = &gMvSataChannels[pAdapter->mvSataAdapter.adapterId][channelNum];
2621713e81bSScott Long 	pAdapter->mvSataAdapter.sataChannel[channelNum] = pMvSataChannel;
2631713e81bSScott Long 	pMvSataChannel->channelNumber = channelNum;
2641713e81bSScott Long 	pMvSataChannel->lba48Address = MV_FALSE;
2651713e81bSScott Long 	pMvSataChannel->maxReadTransfer = MV_FALSE;
2661713e81bSScott Long 
267d2bd3ab9SScott Long 	pMvSataChannel->requestQueue = (struct mvDmaRequestQueueEntry *)
268d2bd3ab9SScott Long 								   (pAdapter->requestsArrayBaseAlignedAddr + (channelNum * MV_EDMA_REQUEST_QUEUE_SIZE));
269d2bd3ab9SScott Long 	req_dma_addr = pAdapter->requestsArrayBaseDmaAlignedAddr + (channelNum * MV_EDMA_REQUEST_QUEUE_SIZE);
2701713e81bSScott Long 
2711713e81bSScott Long 
272d2bd3ab9SScott Long 	KdPrint(("requestQueue addr is 0x%llX", (HPT_U64)(ULONG_PTR)req_dma_addr));
2731713e81bSScott Long 
2741713e81bSScott Long 	/* check the 1K alignment of the request queue*/
2751713e81bSScott Long 	if (req_dma_addr & 0x3ff)
2761713e81bSScott Long 	{
27764470755SXin LI 		MV_ERROR("RR18xx[%d]: request queue allocated isn't 1 K aligned,"
278d2bd3ab9SScott Long 				 " dma_addr=%llx channel=%d\n", pAdapter->mvSataAdapter.adapterId,
279d2bd3ab9SScott Long 				 (HPT_U64)(ULONG_PTR)req_dma_addr, channelNum);
2801713e81bSScott Long 		return -1;
2811713e81bSScott Long 	}
2821713e81bSScott Long 	pMvSataChannel->requestQueuePciLowAddress = req_dma_addr;
2831713e81bSScott Long 	pMvSataChannel->requestQueuePciHiAddress = 0;
28464470755SXin LI 	KdPrint(("RR18xx[%d,%d]: request queue allocated: 0x%p",
2851713e81bSScott Long 			  pAdapter->mvSataAdapter.adapterId, channelNum,
2861713e81bSScott Long 			  pMvSataChannel->requestQueue));
287d2bd3ab9SScott Long 	pMvSataChannel->responseQueue = (struct mvDmaResponseQueueEntry *)
288d2bd3ab9SScott Long 									(pAdapter->responsesArrayBaseAlignedAddr + (channelNum * MV_EDMA_RESPONSE_QUEUE_SIZE));
289d2bd3ab9SScott Long 	rsp_dma_addr = pAdapter->responsesArrayBaseDmaAlignedAddr + (channelNum * MV_EDMA_RESPONSE_QUEUE_SIZE);
2901713e81bSScott Long 
2911713e81bSScott Long 	/* check the 256 alignment of the response queue*/
2921713e81bSScott Long 	if (rsp_dma_addr & 0xff)
2931713e81bSScott Long 	{
29464470755SXin LI 		MV_ERROR("RR18xx[%d,%d]: response queue allocated isn't 256 byte "
295d2bd3ab9SScott Long 				 "aligned, dma_addr=%llx\n",
296d2bd3ab9SScott Long 				 pAdapter->mvSataAdapter.adapterId, channelNum, (HPT_U64)(ULONG_PTR)rsp_dma_addr);
2971713e81bSScott Long 		return -1;
2981713e81bSScott Long 	}
2991713e81bSScott Long 	pMvSataChannel->responseQueuePciLowAddress = rsp_dma_addr;
3001713e81bSScott Long 	pMvSataChannel->responseQueuePciHiAddress = 0;
30164470755SXin LI 	KdPrint(("RR18xx[%d,%d]: response queue allocated: 0x%p",
3021713e81bSScott Long 			  pAdapter->mvSataAdapter.adapterId, channelNum,
3031713e81bSScott Long 			  pMvSataChannel->responseQueue));
3041713e81bSScott Long 
3051713e81bSScott Long 	pAdapter->mvChannel[channelNum].online = MV_TRUE;
3061713e81bSScott Long 	return 0;
3071713e81bSScott Long }
3081713e81bSScott Long 
3091713e81bSScott Long /******************************************************************************
3101713e81bSScott Long  *	Name: hptmv_parse_identify_results
3111713e81bSScott Long  *
312d2bd3ab9SScott Long  *	Description:	this functions parses the identify command results, checks
31364470755SXin LI  *					that the connected deives can be accesed by RR18xx EDMA,
314453130d9SPedro F. Giffuni  *					and updates the channel structure accordingly.
315d2bd3ab9SScott Long  *
3161713e81bSScott Long  *	Parameters:     pMvSataChannel, pointer to the channel data structure.
3171713e81bSScott Long  *
3181713e81bSScott Long  *	Returns:       	=0 ->success, < 0 ->failure.
3191713e81bSScott Long  *
3201713e81bSScott Long  ******************************************************************************/
3211713e81bSScott Long static int
3221713e81bSScott Long hptmv_parse_identify_results(MV_SATA_CHANNEL *pMvSataChannel)
3231713e81bSScott Long {
3241713e81bSScott Long 	MV_U16  *iden = pMvSataChannel->identifyDevice;
3251713e81bSScott Long 
3261713e81bSScott Long 	/*LBA addressing*/
327d2bd3ab9SScott Long 	if (! (iden[IDEN_CAPACITY_1_OFFSET] & 0x200))
328d2bd3ab9SScott Long 	{
3291713e81bSScott Long 		KdPrint(("IAL Error in IDENTIFY info: LBA not supported\n"));
3301713e81bSScott Long 		return -1;
331d2bd3ab9SScott Long 	}
332d2bd3ab9SScott Long 	else
333d2bd3ab9SScott Long 	{
3341713e81bSScott Long 		KdPrint(("%25s - %s\n", "Capabilities", "LBA supported"));
3351713e81bSScott Long 	}
3361713e81bSScott Long 	/*DMA support*/
337d2bd3ab9SScott Long 	if (! (iden[IDEN_CAPACITY_1_OFFSET] & 0x100))
338d2bd3ab9SScott Long 	{
3391713e81bSScott Long 		KdPrint(("IAL Error in IDENTIFY info: DMA not supported\n"));
3401713e81bSScott Long 		return -1;
341d2bd3ab9SScott Long 	}
342d2bd3ab9SScott Long 	else
343d2bd3ab9SScott Long 	{
3441713e81bSScott Long 		KdPrint(("%25s - %s\n", "Capabilities", "DMA supported"));
3451713e81bSScott Long 	}
3461713e81bSScott Long 	/* PIO */
347d2bd3ab9SScott Long 	if ((iden[IDEN_VALID] & 2) == 0)
348d2bd3ab9SScott Long 	{
349d2bd3ab9SScott Long 		KdPrint(("IAL Error in IDENTIFY info: not able to find PIO mode\n"));
3501713e81bSScott Long 		return -1;
3511713e81bSScott Long 	}
3521713e81bSScott Long 	KdPrint(("%25s - 0x%02x\n", "PIO modes supported",
3531713e81bSScott Long 			  iden[IDEN_PIO_MODE_SPPORTED] & 0xff));
3541713e81bSScott Long 
3551713e81bSScott Long 	/*UDMA*/
356d2bd3ab9SScott Long 	if ((iden[IDEN_VALID] & 4) == 0)
357d2bd3ab9SScott Long 	{
358d2bd3ab9SScott Long 		KdPrint(("IAL Error in IDENTIFY info: not able to find UDMA mode\n"));
3591713e81bSScott Long 		return -1;
3601713e81bSScott Long 	}
3611713e81bSScott Long 
3621713e81bSScott Long 	/* 48 bit address */
363d2bd3ab9SScott Long 	if ((iden[IDEN_SUPPORTED_COMMANDS2] & 0x400))
364d2bd3ab9SScott Long 	{
3651713e81bSScott Long 		KdPrint(("%25s - %s\n", "LBA48 addressing", "supported"));
3661713e81bSScott Long 		pMvSataChannel->lba48Address = MV_TRUE;
367d2bd3ab9SScott Long 	}
368d2bd3ab9SScott Long 	else
369d2bd3ab9SScott Long 	{
3701713e81bSScott Long 		KdPrint(("%25s - %s\n", "LBA48 addressing", "Not supported"));
3711713e81bSScott Long 		pMvSataChannel->lba48Address = MV_FALSE;
3721713e81bSScott Long 	}
3731713e81bSScott Long 	return 0;
3741713e81bSScott Long }
3751713e81bSScott Long 
3761713e81bSScott Long static void
3771713e81bSScott Long init_vdev_params(IAL_ADAPTER_T *pAdapter, MV_U8 channel)
3781713e81bSScott Long {
379d2bd3ab9SScott Long 	PVDevice pVDev = &pAdapter->VDevices[channel];
380d2bd3ab9SScott Long 	MV_SATA_CHANNEL *pMvSataChannel = pAdapter->mvSataAdapter.sataChannel[channel];
381d2bd3ab9SScott Long 	MV_U16_PTR IdentifyData = pMvSataChannel->identifyDevice;
3821713e81bSScott Long 
3831713e81bSScott Long 	pMvSataChannel->outstandingCommands = 0;
3841713e81bSScott Long 
3851713e81bSScott Long 	pVDev->u.disk.mv         = pMvSataChannel;
3861713e81bSScott Long 	pVDev->u.disk.df_on_line = 1;
3871713e81bSScott Long 	pVDev->u.disk.pVBus      = &pAdapter->VBus;
3881713e81bSScott Long 	pVDev->pVBus             = &pAdapter->VBus;
3891713e81bSScott Long 
3901713e81bSScott Long #ifdef SUPPORT_48BIT_LBA
3911713e81bSScott Long 	if (pMvSataChannel->lba48Address == MV_TRUE)
392d2bd3ab9SScott Long 		pVDev->u.disk.dDeRealCapacity = ((IdentifyData[101]<<16) | IdentifyData[100]) - 1;
3931713e81bSScott Long 	else
3941713e81bSScott Long #endif
3951713e81bSScott Long 	if(IdentifyData[53] & 1) {
3961713e81bSScott Long 	pVDev->u.disk.dDeRealCapacity =
397d2bd3ab9SScott Long 	  (((IdentifyData[58]<<16 | IdentifyData[57]) < (IdentifyData[61]<<16 | IdentifyData[60])) ?
3981713e81bSScott Long 		  (IdentifyData[61]<<16 | IdentifyData[60]) :
3991713e81bSScott Long 				(IdentifyData[58]<<16 | IdentifyData[57])) - 1;
4001713e81bSScott Long 	} else
4011713e81bSScott Long 		pVDev->u.disk.dDeRealCapacity =
4021713e81bSScott Long 				 (IdentifyData[61]<<16 | IdentifyData[60]) - 1;
4031713e81bSScott Long 
4041713e81bSScott Long 	pVDev->u.disk.bDeUsable_Mode = pVDev->u.disk.bDeModeSetting =
405d2bd3ab9SScott Long 		pAdapter->mvChannel[channel].maxPioModeSupported - MV_ATA_TRANSFER_PIO_0;
4061713e81bSScott Long 
4071713e81bSScott Long 	if (pAdapter->mvChannel[channel].maxUltraDmaModeSupported!=0xFF) {
4081713e81bSScott Long 		pVDev->u.disk.bDeUsable_Mode = pVDev->u.disk.bDeModeSetting =
409d2bd3ab9SScott Long 			pAdapter->mvChannel[channel].maxUltraDmaModeSupported - MV_ATA_TRANSFER_UDMA_0 + 8;
4101713e81bSScott Long 	}
4111713e81bSScott Long }
4121713e81bSScott Long 
413d2bd3ab9SScott Long static void device_change(IAL_ADAPTER_T *pAdapter , MV_U8 channelIndex, int plugged)
4141713e81bSScott Long {
4151713e81bSScott Long 	PVDevice pVDev;
416d2bd3ab9SScott Long 	MV_SATA_ADAPTER  *pMvSataAdapter = &pAdapter->mvSataAdapter;
417d2bd3ab9SScott Long 	MV_SATA_CHANNEL  *pMvSataChannel = pMvSataAdapter->sataChannel[channelIndex];
4181713e81bSScott Long 
419d2bd3ab9SScott Long 	if (!pMvSataChannel) return;
4201713e81bSScott Long 
421d2bd3ab9SScott Long 	if (plugged)
422d2bd3ab9SScott Long 	{
4231713e81bSScott Long 		pVDev = &(pAdapter->VDevices[channelIndex]);
4241713e81bSScott Long 		init_vdev_params(pAdapter, channelIndex);
4251713e81bSScott Long 
4261713e81bSScott Long 		pVDev->VDeviceType = pVDev->u.disk.df_atapi? VD_ATAPI :
427d2bd3ab9SScott Long 			pVDev->u.disk.df_removable_drive? VD_REMOVABLE : VD_SINGLE_DISK;
4281713e81bSScott Long 
429d2bd3ab9SScott Long 		pVDev->VDeviceCapacity = pVDev->u.disk.dDeRealCapacity-SAVE_FOR_RAID_INFO;
4301713e81bSScott Long 		pVDev->pfnSendCommand = pfnSendCommand[pVDev->VDeviceType];
4311713e81bSScott Long 		pVDev->pfnDeviceFailed = pfnDeviceFailed[pVDev->VDeviceType];
4321713e81bSScott Long 		pVDev->vf_online = 1;
4331713e81bSScott Long 
4341713e81bSScott Long #ifdef SUPPORT_ARRAY
435d2bd3ab9SScott Long 		if(pVDev->pParent)
436d2bd3ab9SScott Long 		{
4371713e81bSScott Long 			int iMember;
438d2bd3ab9SScott Long 			for(iMember = 0; iMember < 	pVDev->pParent->u.array.bArnMember; iMember++)
4391713e81bSScott Long 				if((PVDevice)pVDev->pParent->u.array.pMember[iMember] == pVDev)
4401713e81bSScott Long 					pVDev->pParent->u.array.pMember[iMember] = NULL;
4411713e81bSScott Long 			pVDev->pParent = NULL;
4421713e81bSScott Long 		}
4431713e81bSScott Long #endif
4441713e81bSScott Long 		fNotifyGUI(ET_DEVICE_PLUGGED,pVDev);
4451713e81bSScott Long 		fCheckBootable(pVDev);
4461713e81bSScott Long 		RegisterVDevice(pVDev);
4471713e81bSScott Long 
4481713e81bSScott Long #ifndef FOR_DEMO
4491713e81bSScott Long 		if (pAdapter->beeping) {
4501713e81bSScott Long 			pAdapter->beeping = 0;
4511713e81bSScott Long 			BeepOff(pAdapter->mvSataAdapter.adapterIoBaseAddress);
4521713e81bSScott Long 		}
4531713e81bSScott Long #endif
4541713e81bSScott Long 
455d2bd3ab9SScott Long 	}
456d2bd3ab9SScott Long 	else
457d2bd3ab9SScott Long 	{
4581713e81bSScott Long 		pVDev  = &(pAdapter->VDevices[channelIndex]);
459d2bd3ab9SScott Long 		failDevice(pVDev);
4601713e81bSScott Long 	}
4611713e81bSScott Long }
4621713e81bSScott Long 
4631713e81bSScott Long static int
4641713e81bSScott Long start_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum)
4651713e81bSScott Long {
466d2bd3ab9SScott Long 	MV_SATA_ADAPTER *pMvSataAdapter = &pAdapter->mvSataAdapter;
467d2bd3ab9SScott Long 	MV_SATA_CHANNEL *pMvSataChannel = pMvSataAdapter->sataChannel[channelNum];
468d2bd3ab9SScott Long 	MV_CHANNEL		*pChannelInfo = &(pAdapter->mvChannel[channelNum]);
4691713e81bSScott Long 	MV_U32          udmaMode,pioMode;
4701713e81bSScott Long 
47164470755SXin LI 	KdPrint(("RR18xx [%d]: start channel (%d)", pMvSataAdapter->adapterId,
4721713e81bSScott Long 			 channelNum));
4731713e81bSScott Long 
4741713e81bSScott Long 
4751713e81bSScott Long 	/* Software reset channel */
476d2bd3ab9SScott Long 	if (mvStorageDevATASoftResetDevice(pMvSataAdapter, channelNum) == MV_FALSE)
477d2bd3ab9SScott Long 	{
47864470755SXin LI 		MV_ERROR("RR18xx [%d,%d]: failed to perform Software reset\n",
4791713e81bSScott Long 				 pMvSataAdapter->adapterId, channelNum);
4801713e81bSScott Long 		return -1;
4811713e81bSScott Long 	}
4821713e81bSScott Long 
4831713e81bSScott Long 	/* Hardware reset channel */
484d2bd3ab9SScott Long 	if (mvSataChannelHardReset(pMvSataAdapter, channelNum) == MV_FALSE)
485d2bd3ab9SScott Long 	{
486d2bd3ab9SScott Long 		/* If failed, try again - this is when trying to hardreset a channel */
487d2bd3ab9SScott Long 		/* when drive is just spinning up */
4881713e81bSScott Long 		StallExec(5000000); /* wait 5 sec before trying again */
489d2bd3ab9SScott Long 		if (mvSataChannelHardReset(pMvSataAdapter, channelNum) == MV_FALSE)
490d2bd3ab9SScott Long 		{
49164470755SXin LI 			MV_ERROR("RR18xx [%d,%d]: failed to perform Hard reset\n",
4921713e81bSScott Long 					 pMvSataAdapter->adapterId, channelNum);
4931713e81bSScott Long 			return -1;
4941713e81bSScott Long 		}
4951713e81bSScott Long 	}
4961713e81bSScott Long 
497d2bd3ab9SScott Long 	/* identify device*/
498d2bd3ab9SScott Long 	if (mvStorageDevATAIdentifyDevice(pMvSataAdapter, channelNum) == MV_FALSE)
499d2bd3ab9SScott Long 	{
50064470755SXin LI 		MV_ERROR("RR18xx [%d,%d]: failed to perform ATA Identify command\n"
501d2bd3ab9SScott Long 				 , pMvSataAdapter->adapterId, channelNum);
502d2bd3ab9SScott Long 		return -1;
503d2bd3ab9SScott Long 	}
504d2bd3ab9SScott Long 	if (hptmv_parse_identify_results(pMvSataChannel))
505d2bd3ab9SScott Long 	{
50664470755SXin LI 		MV_ERROR("RR18xx [%d,%d]: Error in parsing ATA Identify message\n"
507d2bd3ab9SScott Long 				 , pMvSataAdapter->adapterId, channelNum);
508d2bd3ab9SScott Long 		return -1;
509d2bd3ab9SScott Long 	}
510d2bd3ab9SScott Long 
511d2bd3ab9SScott Long 	/* mvStorageDevATASetFeatures */
512d2bd3ab9SScott Long 	/* Disable 8 bit PIO in case CFA enabled */
513d2bd3ab9SScott Long 	if (pMvSataChannel->identifyDevice[86] & 4)
514d2bd3ab9SScott Long 	{
51564470755SXin LI 		KdPrint(("RR18xx [%d]: Disable 8 bit PIO (CFA enabled) \n",
516d2bd3ab9SScott Long 				  pMvSataAdapter->adapterId));
517d2bd3ab9SScott Long 		if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
518d2bd3ab9SScott Long 									   MV_ATA_SET_FEATURES_DISABLE_8_BIT_PIO, 0,
519d2bd3ab9SScott Long 									   0, 0, 0) == MV_FALSE)
520d2bd3ab9SScott Long 		{
52164470755SXin LI 			MV_ERROR("RR18xx [%d]: channel %d: mvStorageDevATASetFeatures"
522d2bd3ab9SScott Long 					 " failed\n", pMvSataAdapter->adapterId, channelNum);
523d2bd3ab9SScott Long 			return -1;
524d2bd3ab9SScott Long 		}
525d2bd3ab9SScott Long 	}
5261713e81bSScott Long 	/* Write cache */
527d2bd3ab9SScott Long #ifdef ENABLE_WRITE_CACHE
528d2bd3ab9SScott Long 	if (pMvSataChannel->identifyDevice[82] & 0x20)
529d2bd3ab9SScott Long 	{
530d2bd3ab9SScott Long 		if (!(pMvSataChannel->identifyDevice[85] & 0x20)) /* if not enabled by default */
531d2bd3ab9SScott Long 		{
532d2bd3ab9SScott Long 			if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
533d2bd3ab9SScott Long 										   MV_ATA_SET_FEATURES_ENABLE_WCACHE, 0,
534d2bd3ab9SScott Long 										   0, 0, 0) == MV_FALSE)
535d2bd3ab9SScott Long 			{
53664470755SXin LI 				MV_ERROR("RR18xx [%d]: channel %d: mvStorageDevATASetFeatures failed\n",
5371713e81bSScott Long 						 pMvSataAdapter->adapterId, channelNum);
5381713e81bSScott Long 				return -1;
5391713e81bSScott Long 			}
5401713e81bSScott Long 		}
54164470755SXin LI 		KdPrint(("RR18xx [%d]: channel %d, write cache enabled\n",
5421713e81bSScott Long 				  pMvSataAdapter->adapterId, channelNum));
543d2bd3ab9SScott Long 	}
544d2bd3ab9SScott Long 	else
545d2bd3ab9SScott Long 	{
54664470755SXin LI 		KdPrint(("RR18xx [%d]: channel %d, write cache not supported\n",
5471713e81bSScott Long 				  pMvSataAdapter->adapterId, channelNum));
5481713e81bSScott Long 	}
549d2bd3ab9SScott Long #else /* disable write cache */
550d2bd3ab9SScott Long 	{
551d2bd3ab9SScott Long 		if (pMvSataChannel->identifyDevice[85] & 0x20)
552d2bd3ab9SScott Long 		{
55364470755SXin LI 			KdPrint(("RR18xx [%d]: channel =%d, disable write cache\n",
5541713e81bSScott Long 					  pMvSataAdapter->adapterId, channelNum));
5551713e81bSScott Long 			if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
556d2bd3ab9SScott Long 										   MV_ATA_SET_FEATURES_DISABLE_WCACHE, 0,
557d2bd3ab9SScott Long 										   0, 0, 0) == MV_FALSE)
558d2bd3ab9SScott Long 			{
55964470755SXin LI 				MV_ERROR("RR18xx [%d]: channel %d: mvStorageDevATASetFeatures failed\n",
5601713e81bSScott Long 						 pMvSataAdapter->adapterId, channelNum);
5611713e81bSScott Long 				return -1;
5621713e81bSScott Long 			}
5631713e81bSScott Long 		}
56464470755SXin LI 		KdPrint(("RR18xx [%d]: channel=%d, write cache disabled\n",
5651713e81bSScott Long 				  pMvSataAdapter->adapterId, channelNum));
566d2bd3ab9SScott Long 	}
5671713e81bSScott Long #endif
5681713e81bSScott Long 
5691713e81bSScott Long 	/* Set transfer mode */
57064470755SXin LI 	KdPrint(("RR18xx [%d] Set transfer mode XFER_PIO_SLOW\n",
5711713e81bSScott Long 			  pMvSataAdapter->adapterId));
5721713e81bSScott Long 	if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
573d2bd3ab9SScott Long 								   MV_ATA_SET_FEATURES_TRANSFER,
574d2bd3ab9SScott Long 								   MV_ATA_TRANSFER_PIO_SLOW, 0, 0, 0) ==
575d2bd3ab9SScott Long 		MV_FALSE)
576d2bd3ab9SScott Long 	{
57764470755SXin LI 		MV_ERROR("RR18xx [%d] channel %d: Set Features failed\n",
5781713e81bSScott Long 				 pMvSataAdapter->adapterId, channelNum);
5791713e81bSScott Long 		return -1;
5801713e81bSScott Long 	}
5811713e81bSScott Long 
582d2bd3ab9SScott Long 	if (pMvSataChannel->identifyDevice[IDEN_PIO_MODE_SPPORTED] & 1)
583d2bd3ab9SScott Long 	{
5841713e81bSScott Long 		pioMode = MV_ATA_TRANSFER_PIO_4;
585d2bd3ab9SScott Long 	}
586d2bd3ab9SScott Long 	else if (pMvSataChannel->identifyDevice[IDEN_PIO_MODE_SPPORTED] & 2)
587d2bd3ab9SScott Long 	{
5881713e81bSScott Long 		pioMode = MV_ATA_TRANSFER_PIO_3;
589d2bd3ab9SScott Long 	}
590d2bd3ab9SScott Long 	else
591d2bd3ab9SScott Long 	{
592d2bd3ab9SScott Long 		MV_ERROR("IAL Error in IDENTIFY info: PIO modes 3 and 4 not supported\n");
5931713e81bSScott Long 		pioMode = MV_ATA_TRANSFER_PIO_SLOW;
5941713e81bSScott Long 	}
5951713e81bSScott Long 
59664470755SXin LI 	KdPrint(("RR18xx [%d] Set transfer mode XFER_PIO_4\n",
5971713e81bSScott Long 			  pMvSataAdapter->adapterId));
5981713e81bSScott Long 	pAdapter->mvChannel[channelNum].maxPioModeSupported = pioMode;
5991713e81bSScott Long 	if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
600d2bd3ab9SScott Long 								   MV_ATA_SET_FEATURES_TRANSFER,
601d2bd3ab9SScott Long 								   pioMode, 0, 0, 0) == MV_FALSE)
602d2bd3ab9SScott Long 	{
60364470755SXin LI 		MV_ERROR("RR18xx [%d] channel %d: Set Features failed\n",
6041713e81bSScott Long 				 pMvSataAdapter->adapterId, channelNum);
6051713e81bSScott Long 		return -1;
6061713e81bSScott Long 	}
6071713e81bSScott Long 
6081713e81bSScott Long 	udmaMode = MV_ATA_TRANSFER_UDMA_0;
609d2bd3ab9SScott Long 	if (pMvSataChannel->identifyDevice[IDEN_UDMA_MODE] & 0x40)
610d2bd3ab9SScott Long 	{
6111713e81bSScott Long 		udmaMode =  MV_ATA_TRANSFER_UDMA_6;
612d2bd3ab9SScott Long 	}
613d2bd3ab9SScott Long 	else if (pMvSataChannel->identifyDevice[IDEN_UDMA_MODE] & 0x20)
614d2bd3ab9SScott Long 	{
6151713e81bSScott Long 		udmaMode =  MV_ATA_TRANSFER_UDMA_5;
616d2bd3ab9SScott Long 	}
617d2bd3ab9SScott Long 	else if (pMvSataChannel->identifyDevice[IDEN_UDMA_MODE] & 0x10)
618d2bd3ab9SScott Long 	{
6191713e81bSScott Long 		udmaMode =  MV_ATA_TRANSFER_UDMA_4;
620d2bd3ab9SScott Long 	}
621d2bd3ab9SScott Long 	else if (pMvSataChannel->identifyDevice[IDEN_UDMA_MODE] & 8)
622d2bd3ab9SScott Long 	{
6231713e81bSScott Long 		udmaMode =  MV_ATA_TRANSFER_UDMA_3;
624d2bd3ab9SScott Long 	}
625d2bd3ab9SScott Long 	else if (pMvSataChannel->identifyDevice[IDEN_UDMA_MODE] & 4)
626d2bd3ab9SScott Long 	{
6271713e81bSScott Long 		udmaMode =  MV_ATA_TRANSFER_UDMA_2;
6281713e81bSScott Long 	}
6291713e81bSScott Long 
63064470755SXin LI 	KdPrint(("RR18xx [%d] Set transfer mode XFER_UDMA_%d\n",
6311713e81bSScott Long 			  pMvSataAdapter->adapterId, udmaMode & 0xf));
6321713e81bSScott Long 	pChannelInfo->maxUltraDmaModeSupported = udmaMode;
6331713e81bSScott Long 
634d2bd3ab9SScott Long 	/*if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
635d2bd3ab9SScott Long 								   MV_ATA_SET_FEATURES_TRANSFER, udmaMode,
636d2bd3ab9SScott Long 								   0, 0, 0) == MV_FALSE)
637d2bd3ab9SScott Long 	{
63864470755SXin LI 		MV_ERROR("RR18xx [%d] channel %d: Set Features failed\n",
6391713e81bSScott Long 				 pMvSataAdapter->adapterId, channelNum);
6401713e81bSScott Long 		return -1;
641d2bd3ab9SScott Long 	}*/
6421713e81bSScott Long 	if (pChannelInfo->maxUltraDmaModeSupported == 0xFF)
6431713e81bSScott Long 		return TRUE;
644d2bd3ab9SScott Long 	else
645d2bd3ab9SScott Long 		do
646d2bd3ab9SScott Long 		{
6471713e81bSScott Long 			if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
6481713e81bSScott Long 								   MV_ATA_SET_FEATURES_TRANSFER,
649d2bd3ab9SScott Long 								   pChannelInfo->maxUltraDmaModeSupported,
650d2bd3ab9SScott Long 								   0, 0, 0) == MV_FALSE)
651d2bd3ab9SScott Long 			{
652d2bd3ab9SScott Long 				if (pChannelInfo->maxUltraDmaModeSupported > MV_ATA_TRANSFER_UDMA_0)
653d2bd3ab9SScott Long 				{
654d2bd3ab9SScott Long 					if (mvStorageDevATASoftResetDevice(pMvSataAdapter, channelNum) == MV_FALSE)
655d2bd3ab9SScott Long 					{
656d2bd3ab9SScott Long 						MV_REG_WRITE_BYTE(pMvSataAdapter->adapterIoBaseAddress,
657d2bd3ab9SScott Long 										  pMvSataChannel->eDmaRegsOffset +
658d2bd3ab9SScott Long 										  0x11c, /* command reg */
659d2bd3ab9SScott Long 										  MV_ATA_COMMAND_IDLE_IMMEDIATE);
6601713e81bSScott Long 						mvMicroSecondsDelay(10000);
6611713e81bSScott Long 						mvSataChannelHardReset(pMvSataAdapter, channelNum);
662d2bd3ab9SScott Long 						if (mvStorageDevATASoftResetDevice(pMvSataAdapter, channelNum) == MV_FALSE)
6631713e81bSScott Long 							return FALSE;
6641713e81bSScott Long 					}
665d2bd3ab9SScott Long 					if (mvSataChannelHardReset(pMvSataAdapter, channelNum) == MV_FALSE)
6661713e81bSScott Long 						return FALSE;
6671713e81bSScott Long 					pChannelInfo->maxUltraDmaModeSupported--;
668d2bd3ab9SScott Long 					continue;
669d2bd3ab9SScott Long 				}
670d2bd3ab9SScott Long 				else   return FALSE;
671d2bd3ab9SScott Long 			}
672d2bd3ab9SScott Long 			break;
6731713e81bSScott Long 		}while (1);
6741713e81bSScott Long 
6751713e81bSScott Long 	/* Read look ahead */
676d2bd3ab9SScott Long #ifdef ENABLE_READ_AHEAD
677d2bd3ab9SScott Long 	if (pMvSataChannel->identifyDevice[82] & 0x40)
678d2bd3ab9SScott Long 	{
679d2bd3ab9SScott Long 		if (!(pMvSataChannel->identifyDevice[85] & 0x40)) /* if not enabled by default */
680d2bd3ab9SScott Long 		{
681d2bd3ab9SScott Long 			if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
682d2bd3ab9SScott Long 										   MV_ATA_SET_FEATURES_ENABLE_RLA, 0, 0,
683d2bd3ab9SScott Long 										   0, 0) == MV_FALSE)
684d2bd3ab9SScott Long 			{
68564470755SXin LI 				MV_ERROR("RR18xx [%d] channel %d: Set Features failed\n",
686d2bd3ab9SScott Long 						 pMvSataAdapter->adapterId, channelNum);
6871713e81bSScott Long 				return -1;
6881713e81bSScott Long 			}
6891713e81bSScott Long 		}
69064470755SXin LI 		KdPrint(("RR18xx [%d]: channel=%d, read look ahead enabled\n",
6911713e81bSScott Long 				  pMvSataAdapter->adapterId, channelNum));
692d2bd3ab9SScott Long 	}
693d2bd3ab9SScott Long 	else
694d2bd3ab9SScott Long 	{
69564470755SXin LI 		KdPrint(("RR18xx [%d]: channel %d, Read Look Ahead not supported\n",
696d2bd3ab9SScott Long 				  pMvSataAdapter->adapterId, channelNum));
6971713e81bSScott Long 	}
6981713e81bSScott Long #else
699d2bd3ab9SScott Long 	{
700d2bd3ab9SScott Long 		if (pMvSataChannel->identifyDevice[86] & 0x20)
701d2bd3ab9SScott Long 		{
70264470755SXin LI 			KdPrint(("RR18xx [%d]:channel %d, disable read look ahead\n",
7031713e81bSScott Long 					  pMvSataAdapter->adapterId, channelNum));
7041713e81bSScott Long 			if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
705d2bd3ab9SScott Long 										   MV_ATA_SET_FEATURES_DISABLE_RLA, 0, 0,
706d2bd3ab9SScott Long 										   0, 0) == MV_FALSE)
707d2bd3ab9SScott Long 			{
70864470755SXin LI 				MV_ERROR("RR18xx [%d]:channel %d:  ATA Set Features failed\n",
709d2bd3ab9SScott Long 						 pMvSataAdapter->adapterId, channelNum);
7101713e81bSScott Long 				return -1;
7111713e81bSScott Long 			}
7121713e81bSScott Long 		}
71364470755SXin LI 		KdPrint(("RR18xx [%d]:channel %d, read look ahead disabled\n",
7141713e81bSScott Long 				  pMvSataAdapter->adapterId, channelNum));
715d2bd3ab9SScott Long 	}
7161713e81bSScott Long #endif
7171713e81bSScott Long 
718d2bd3ab9SScott Long 
719d2bd3ab9SScott Long 	{
72064470755SXin LI 		KdPrint(("RR18xx [%d]: channel %d config EDMA, Non Queued Mode\n",
721d2bd3ab9SScott Long 				  pMvSataAdapter->adapterId,
722d2bd3ab9SScott Long 				  channelNum));
723d2bd3ab9SScott Long 		if (mvSataConfigEdmaMode(pMvSataAdapter, channelNum,
724d2bd3ab9SScott Long 								 MV_EDMA_MODE_NOT_QUEUED, 0) == MV_FALSE)
725d2bd3ab9SScott Long 		{
72664470755SXin LI 			MV_ERROR("RR18xx [%d] channel %d Error: mvSataConfigEdmaMode failed\n",
7271713e81bSScott Long 					 pMvSataAdapter->adapterId, channelNum);
7281713e81bSScott Long 			return -1;
7291713e81bSScott Long 		}
7301713e81bSScott Long 	}
7311713e81bSScott Long 	/* Enable EDMA */
732d2bd3ab9SScott Long 	if (mvSataEnableChannelDma(pMvSataAdapter, channelNum) == MV_FALSE)
733d2bd3ab9SScott Long 	{
73464470755SXin LI 		MV_ERROR("RR18xx [%d] Failed to enable DMA, channel=%d\n",
7351713e81bSScott Long 				 pMvSataAdapter->adapterId, channelNum);
7361713e81bSScott Long 		return -1;
7371713e81bSScott Long 	}
73864470755SXin LI 	MV_ERROR("RR18xx [%d,%d]: channel started successfully\n",
7391713e81bSScott Long 			 pMvSataAdapter->adapterId, channelNum);
7401713e81bSScott Long 
7411713e81bSScott Long #ifndef FOR_DEMO
7421713e81bSScott Long 	set_fail_led(pMvSataAdapter, channelNum, 0);
7431713e81bSScott Long #endif
7441713e81bSScott Long 	return 0;
7451713e81bSScott Long }
7461713e81bSScott Long 
7471713e81bSScott Long static void
7481713e81bSScott Long hptmv_handle_event(void * data, int flag)
7491713e81bSScott Long {
750d2bd3ab9SScott Long 	IAL_ADAPTER_T   *pAdapter = (IAL_ADAPTER_T *)data;
751d2bd3ab9SScott Long 	MV_SATA_ADAPTER *pMvSataAdapter = &pAdapter->mvSataAdapter;
7521713e81bSScott Long 	MV_U8           channelIndex;
7531713e81bSScott Long 
75449b3fc40SJohn Baldwin 	mtx_assert(&pAdapter->lock, MA_OWNED);
755d2bd3ab9SScott Long /*	mvOsSemTake(&pMvSataAdapter->semaphore); */
756d2bd3ab9SScott Long 	for (channelIndex = 0; channelIndex < MV_SATA_CHANNELS_NUM; channelIndex++)
757d2bd3ab9SScott Long 	{
758d2bd3ab9SScott Long 		switch(pAdapter->sataEvents[channelIndex])
759d2bd3ab9SScott Long 		{
7601713e81bSScott Long 			case SATA_EVENT_CHANNEL_CONNECTED:
7611713e81bSScott Long 				/* Handle only connects */
7621713e81bSScott Long 				if (flag == 1)
7631713e81bSScott Long 					break;
76464470755SXin LI 				KdPrint(("RR18xx [%d,%d]: new device connected\n",
7651713e81bSScott Long 						 pMvSataAdapter->adapterId, channelIndex));
7661713e81bSScott Long 				hptmv_init_channel(pAdapter, channelIndex);
767d2bd3ab9SScott Long 				if (mvSataConfigureChannel( pMvSataAdapter, channelIndex) == MV_FALSE)
768d2bd3ab9SScott Long 				{
76964470755SXin LI 					MV_ERROR("RR18xx [%d,%d] Failed to configure\n",
770d2bd3ab9SScott Long 							 pMvSataAdapter->adapterId, channelIndex);
7711713e81bSScott Long 					hptmv_free_channel(pAdapter, channelIndex);
772d2bd3ab9SScott Long 				}
773d2bd3ab9SScott Long 				else
774d2bd3ab9SScott Long 				{
775d2bd3ab9SScott Long 					/*mvSataChannelHardReset(pMvSataAdapter, channel);*/
776d2bd3ab9SScott Long 					if (start_channel( pAdapter, channelIndex))
777d2bd3ab9SScott Long 					{
77864470755SXin LI 						MV_ERROR("RR18xx [%d,%d]Failed to start channel\n",
779d2bd3ab9SScott Long 								 pMvSataAdapter->adapterId, channelIndex);
780d2bd3ab9SScott Long 						hptmv_free_channel(pAdapter, channelIndex);
781d2bd3ab9SScott Long 					}
782d2bd3ab9SScott Long 					else
783d2bd3ab9SScott Long 					{
784d2bd3ab9SScott Long 						device_change(pAdapter, channelIndex, TRUE);
7851713e81bSScott Long 					}
7861713e81bSScott Long 				}
787d2bd3ab9SScott Long 				pAdapter->sataEvents[channelIndex] = SATA_EVENT_NO_CHANGE;
7881713e81bSScott Long 			   break;
7891713e81bSScott Long 
7901713e81bSScott Long 			case SATA_EVENT_CHANNEL_DISCONNECTED:
7911713e81bSScott Long 				/* Handle only disconnects */
7921713e81bSScott Long 				if (flag == 0)
7931713e81bSScott Long 					break;
79464470755SXin LI 				KdPrint(("RR18xx [%d,%d]: device disconnected\n",
7951713e81bSScott Long 						 pMvSataAdapter->adapterId, channelIndex));
7961713e81bSScott Long 					/* Flush pending commands */
797d2bd3ab9SScott Long 				if(pMvSataAdapter->sataChannel[channelIndex])
798d2bd3ab9SScott Long 				{
7991713e81bSScott Long 					_VBUS_INST(&pAdapter->VBus)
800d2bd3ab9SScott Long 					mvSataFlushDmaQueue (pMvSataAdapter, channelIndex,
801d2bd3ab9SScott Long 										 MV_FLUSH_TYPE_CALLBACK);
8021713e81bSScott Long 					CheckPendingCall(_VBUS_P0);
803d2bd3ab9SScott Long 					mvSataRemoveChannel(pMvSataAdapter,channelIndex);
8041713e81bSScott Long 					hptmv_free_channel(pAdapter, channelIndex);
805d2bd3ab9SScott Long 					pMvSataAdapter->sataChannel[channelIndex] = NULL;
80664470755SXin LI 					KdPrint(("RR18xx [%d,%d]: channel removed\n",
807d2bd3ab9SScott Long 						 pMvSataAdapter->adapterId, channelIndex));
808d2bd3ab9SScott Long 					if (pAdapter->outstandingCommands==0 && DPC_Request_Nums==0)
8091713e81bSScott Long 						Check_Idle_Call(pAdapter);
8101713e81bSScott Long 				}
811d2bd3ab9SScott Long 				else
812d2bd3ab9SScott Long 				{
81364470755SXin LI 					KdPrint(("RR18xx [%d,%d]: channel already removed!!\n",
814d2bd3ab9SScott Long 							 pMvSataAdapter->adapterId, channelIndex));
815d2bd3ab9SScott Long 				}
816d2bd3ab9SScott Long 				pAdapter->sataEvents[channelIndex] = SATA_EVENT_NO_CHANGE;
8171713e81bSScott Long 				break;
8181713e81bSScott Long 
8191713e81bSScott Long 			case SATA_EVENT_NO_CHANGE:
8201713e81bSScott Long 				break;
8211713e81bSScott Long 
8221713e81bSScott Long 			default:
8231713e81bSScott Long 				break;
8241713e81bSScott Long 		}
8251713e81bSScott Long 	}
826d2bd3ab9SScott Long /*	mvOsSemRelease(&pMvSataAdapter->semaphore); */
8271713e81bSScott Long }
8281713e81bSScott Long 
8291713e81bSScott Long #define EVENT_CONNECT					1
8301713e81bSScott Long #define EVENT_DISCONNECT				0
8311713e81bSScott Long 
8321713e81bSScott Long static void
8331713e81bSScott Long hptmv_handle_event_connect(void *data)
8341713e81bSScott Long {
8351713e81bSScott Long   hptmv_handle_event (data, 0);
8361713e81bSScott Long }
8371713e81bSScott Long 
8381713e81bSScott Long static void
8391713e81bSScott Long hptmv_handle_event_disconnect(void *data)
8401713e81bSScott Long {
8411713e81bSScott Long   hptmv_handle_event (data, 1);
8421713e81bSScott Long }
8431713e81bSScott Long 
8441713e81bSScott Long static MV_BOOLEAN
8451713e81bSScott Long hptmv_event_notify(MV_SATA_ADAPTER *pMvSataAdapter, MV_EVENT_TYPE eventType,
8461713e81bSScott Long 								   MV_U32 param1, MV_U32 param2)
8471713e81bSScott Long {
8481713e81bSScott Long 	IAL_ADAPTER_T   *pAdapter = pMvSataAdapter->IALData;
8491713e81bSScott Long 
850d2bd3ab9SScott Long 	switch (eventType)
851d2bd3ab9SScott Long 	{
8521713e81bSScott Long 		case MV_EVENT_TYPE_SATA_CABLE:
8531713e81bSScott Long 			{
8541713e81bSScott Long 				MV_U8   channel = param2;
8551713e81bSScott Long 
856d2bd3ab9SScott Long 				if (param1 == EVENT_CONNECT)
857d2bd3ab9SScott Long 				{
858d2bd3ab9SScott Long 					pAdapter->sataEvents[channel] = SATA_EVENT_CHANNEL_CONNECTED;
85964470755SXin LI 					KdPrint(("RR18xx [%d,%d]: device connected event received\n",
860d2bd3ab9SScott Long 							 pMvSataAdapter->adapterId, channel));
861d2bd3ab9SScott Long 					/* Delete previous timers (if multiple drives connected in the same time */
86249b3fc40SJohn Baldwin 					callout_reset(&pAdapter->event_timer_connect, 10 * hz, hptmv_handle_event_connect, pAdapter);
863d2bd3ab9SScott Long 				}
864d2bd3ab9SScott Long 				else if (param1 == EVENT_DISCONNECT)
865d2bd3ab9SScott Long 				{
866d2bd3ab9SScott Long 					pAdapter->sataEvents[channel] = SATA_EVENT_CHANNEL_DISCONNECTED;
86764470755SXin LI 					KdPrint(("RR18xx [%d,%d]: device disconnected event received \n",
868d2bd3ab9SScott Long 							 pMvSataAdapter->adapterId, channel));
8691713e81bSScott Long 					device_change(pAdapter, channel, FALSE);
870d2bd3ab9SScott Long 					/* Delete previous timers (if multiple drives disconnected in the same time */
87149b3fc40SJohn Baldwin 					/*callout_reset(&pAdapter->event_timer_disconnect, 10 * hz, hptmv_handle_event_disconnect, pAdapter); */
872d2bd3ab9SScott Long 					/*It is not necessary to wait, handle it directly*/
873d2bd3ab9SScott Long 					hptmv_handle_event_disconnect(pAdapter);
874d2bd3ab9SScott Long 				}
875d2bd3ab9SScott Long 				else
876d2bd3ab9SScott Long 				{
877d2bd3ab9SScott Long 
8787a2b450fSEitan Adler 					MV_ERROR("RR18xx: illegal value for param1(%d) at "
8797a2b450fSEitan Adler 							 "connect/disconnect event, host=%d\n", param1,
8801713e81bSScott Long 							 pMvSataAdapter->adapterId );
8811713e81bSScott Long 
8821713e81bSScott Long 				}
8831713e81bSScott Long 			}
884d2bd3ab9SScott Long 			break;
8851713e81bSScott Long 		case MV_EVENT_TYPE_ADAPTER_ERROR:
88664470755SXin LI 			KdPrint(("RR18xx: DEVICE error event received, pci cause "
8871713e81bSScott Long 					  "reg=%x,  don't how to handle this\n", param1));
8881713e81bSScott Long 			return MV_TRUE;
8891713e81bSScott Long 		default:
89064470755SXin LI 			MV_ERROR("RR18xx[%d]: unknown event type (%d)\n",
8911713e81bSScott Long 					 pMvSataAdapter->adapterId, eventType);
8921713e81bSScott Long 			return MV_FALSE;
8931713e81bSScott Long 	}
8941713e81bSScott Long 	return MV_TRUE;
8951713e81bSScott Long }
8961713e81bSScott Long 
8971713e81bSScott Long static int
8981713e81bSScott Long hptmv_allocate_edma_queues(IAL_ADAPTER_T *pAdapter)
8991713e81bSScott Long {
900d2bd3ab9SScott Long 	pAdapter->requestsArrayBaseAddr = (MV_U8 *)contigmalloc(REQUESTS_ARRAY_SIZE,
901d2bd3ab9SScott Long 			M_DEVBUF, M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0ul);
902d2bd3ab9SScott Long 	if (pAdapter->requestsArrayBaseAddr == NULL)
903d2bd3ab9SScott Long 	{
90464470755SXin LI 		MV_ERROR("RR18xx[%d]: Failed to allocate memory for EDMA request"
905d2bd3ab9SScott Long 				 " queues\n", pAdapter->mvSataAdapter.adapterId);
9061713e81bSScott Long 		return -1;
9071713e81bSScott Long 	}
908d2bd3ab9SScott Long 	pAdapter->requestsArrayBaseDmaAddr = fOsPhysicalAddress(pAdapter->requestsArrayBaseAddr);
909d2bd3ab9SScott Long 	pAdapter->requestsArrayBaseAlignedAddr = pAdapter->requestsArrayBaseAddr;
9101713e81bSScott Long 	pAdapter->requestsArrayBaseAlignedAddr += MV_EDMA_REQUEST_QUEUE_SIZE;
911d2bd3ab9SScott Long 	pAdapter->requestsArrayBaseAlignedAddr  = (MV_U8 *)
912d2bd3ab9SScott Long 		(((ULONG_PTR)pAdapter->requestsArrayBaseAlignedAddr) & ~(ULONG_PTR)(MV_EDMA_REQUEST_QUEUE_SIZE - 1));
913d2bd3ab9SScott Long 	pAdapter->requestsArrayBaseDmaAlignedAddr = pAdapter->requestsArrayBaseDmaAddr;
9141713e81bSScott Long 	pAdapter->requestsArrayBaseDmaAlignedAddr += MV_EDMA_REQUEST_QUEUE_SIZE;
915d2bd3ab9SScott Long 	pAdapter->requestsArrayBaseDmaAlignedAddr &= ~(ULONG_PTR)(MV_EDMA_REQUEST_QUEUE_SIZE - 1);
9161713e81bSScott Long 
917d2bd3ab9SScott Long 	if ((pAdapter->requestsArrayBaseDmaAlignedAddr - pAdapter->requestsArrayBaseDmaAddr) !=
918d2bd3ab9SScott Long 		(pAdapter->requestsArrayBaseAlignedAddr - pAdapter->requestsArrayBaseAddr))
919d2bd3ab9SScott Long 	{
92064470755SXin LI 		MV_ERROR("RR18xx[%d]: Error in Request Quueues Alignment\n",
9211713e81bSScott Long 				 pAdapter->mvSataAdapter.adapterId);
922d2bd3ab9SScott Long 		contigfree(pAdapter->requestsArrayBaseAddr, REQUESTS_ARRAY_SIZE, M_DEVBUF);
9231713e81bSScott Long 		return -1;
9241713e81bSScott Long 	}
9251713e81bSScott Long 	/* response queues */
926d2bd3ab9SScott Long 	pAdapter->responsesArrayBaseAddr = (MV_U8 *)contigmalloc(RESPONSES_ARRAY_SIZE,
927d2bd3ab9SScott Long 			M_DEVBUF, M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0ul);
928d2bd3ab9SScott Long 	if (pAdapter->responsesArrayBaseAddr == NULL)
929d2bd3ab9SScott Long 	{
93064470755SXin LI 		MV_ERROR("RR18xx[%d]: Failed to allocate memory for EDMA response"
931d2bd3ab9SScott Long 				 " queues\n", pAdapter->mvSataAdapter.adapterId);
932d2bd3ab9SScott Long 		contigfree(pAdapter->requestsArrayBaseAddr, RESPONSES_ARRAY_SIZE, M_DEVBUF);
9331713e81bSScott Long 		return -1;
9341713e81bSScott Long 	}
935d2bd3ab9SScott Long 	pAdapter->responsesArrayBaseDmaAddr = fOsPhysicalAddress(pAdapter->responsesArrayBaseAddr);
936d2bd3ab9SScott Long 	pAdapter->responsesArrayBaseAlignedAddr = pAdapter->responsesArrayBaseAddr;
9371713e81bSScott Long 	pAdapter->responsesArrayBaseAlignedAddr += MV_EDMA_RESPONSE_QUEUE_SIZE;
938d2bd3ab9SScott Long 	pAdapter->responsesArrayBaseAlignedAddr  = (MV_U8 *)
939d2bd3ab9SScott Long 		(((ULONG_PTR)pAdapter->responsesArrayBaseAlignedAddr) & ~(ULONG_PTR)(MV_EDMA_RESPONSE_QUEUE_SIZE - 1));
940d2bd3ab9SScott Long 	pAdapter->responsesArrayBaseDmaAlignedAddr = pAdapter->responsesArrayBaseDmaAddr;
941d2bd3ab9SScott Long 	pAdapter->responsesArrayBaseDmaAlignedAddr += MV_EDMA_RESPONSE_QUEUE_SIZE;
942d2bd3ab9SScott Long 	pAdapter->responsesArrayBaseDmaAlignedAddr &= ~(ULONG_PTR)(MV_EDMA_RESPONSE_QUEUE_SIZE - 1);
9431713e81bSScott Long 
944d2bd3ab9SScott Long 	if ((pAdapter->responsesArrayBaseDmaAlignedAddr - pAdapter->responsesArrayBaseDmaAddr) !=
945d2bd3ab9SScott Long 		(pAdapter->responsesArrayBaseAlignedAddr - pAdapter->responsesArrayBaseAddr))
946d2bd3ab9SScott Long 	{
9477a2b450fSEitan Adler 		MV_ERROR("RR18xx[%d]: Error in Response Queues Alignment\n",
9481713e81bSScott Long 				 pAdapter->mvSataAdapter.adapterId);
949d2bd3ab9SScott Long 		contigfree(pAdapter->requestsArrayBaseAddr, REQUESTS_ARRAY_SIZE, M_DEVBUF);
950d2bd3ab9SScott Long 		contigfree(pAdapter->responsesArrayBaseAddr, RESPONSES_ARRAY_SIZE, M_DEVBUF);
9511713e81bSScott Long 		return -1;
9521713e81bSScott Long 	}
9531713e81bSScott Long 	return 0;
9541713e81bSScott Long }
9551713e81bSScott Long 
9561713e81bSScott Long static void
9571713e81bSScott Long hptmv_free_edma_queues(IAL_ADAPTER_T *pAdapter)
9581713e81bSScott Long {
959d2bd3ab9SScott Long 	contigfree(pAdapter->requestsArrayBaseAddr, REQUESTS_ARRAY_SIZE, M_DEVBUF);
960d2bd3ab9SScott Long 	contigfree(pAdapter->responsesArrayBaseAddr, RESPONSES_ARRAY_SIZE, M_DEVBUF);
9611713e81bSScott Long }
9621713e81bSScott Long 
9631713e81bSScott Long static PVOID
9641713e81bSScott Long AllocatePRDTable(IAL_ADAPTER_T *pAdapter)
9651713e81bSScott Long {
9661713e81bSScott Long 	PVOID ret;
9671713e81bSScott Long 	if (pAdapter->pFreePRDLink) {
968d2bd3ab9SScott Long 		KdPrint(("pAdapter->pFreePRDLink:%p\n",pAdapter->pFreePRDLink));
9691713e81bSScott Long 		ret = pAdapter->pFreePRDLink;
9701713e81bSScott Long 		pAdapter->pFreePRDLink = *(void**)ret;
9711713e81bSScott Long 		return ret;
9721713e81bSScott Long 	}
9731713e81bSScott Long 	return NULL;
9741713e81bSScott Long }
9751713e81bSScott Long 
9761713e81bSScott Long static void
9771713e81bSScott Long FreePRDTable(IAL_ADAPTER_T *pAdapter, PVOID PRDTable)
9781713e81bSScott Long {
9791713e81bSScott Long 	*(void**)PRDTable = pAdapter->pFreePRDLink;
9801713e81bSScott Long 	pAdapter->pFreePRDLink = PRDTable;
9811713e81bSScott Long }
9821713e81bSScott Long 
9831713e81bSScott Long extern PVDevice fGetFirstChild(PVDevice pLogical);
9841713e81bSScott Long extern void fResetBootMark(PVDevice pLogical);
9851713e81bSScott Long static void
9861713e81bSScott Long fRegisterVdevice(IAL_ADAPTER_T *pAdapter)
9871713e81bSScott Long {
9881713e81bSScott Long 	PVDevice pPhysical, pLogical;
9891713e81bSScott Long 	PVBus  pVBus;
9901713e81bSScott Long 	int i,j;
9911713e81bSScott Long 
9921713e81bSScott Long 	for(i=0;i<MV_SATA_CHANNELS_NUM;i++) {
9931713e81bSScott Long 		pPhysical = &(pAdapter->VDevices[i]);
9941713e81bSScott Long 		pLogical = pPhysical;
9951713e81bSScott Long 		while (pLogical->pParent) pLogical = pLogical->pParent;
9961713e81bSScott Long 		if (pLogical->vf_online==0) {
9971713e81bSScott Long 			pPhysical->vf_bootmark = pLogical->vf_bootmark = 0;
9981713e81bSScott Long 			continue;
9991713e81bSScott Long 		}
1000d2bd3ab9SScott Long 		if (pLogical->VDeviceType==VD_SPARE || pPhysical!=fGetFirstChild(pLogical))
10011713e81bSScott Long 			continue;
10021713e81bSScott Long 
10031713e81bSScott Long 		pVBus = &pAdapter->VBus;
1004d2bd3ab9SScott Long 		if(pVBus)
1005d2bd3ab9SScott Long 		{
10061713e81bSScott Long 			j=0;
1007d2bd3ab9SScott Long 			while(j<MAX_VDEVICE_PER_VBUS && pVBus->pVDevice[j]) j++;
10081713e81bSScott Long 			if(j<MAX_VDEVICE_PER_VBUS){
10091713e81bSScott Long 				pVBus->pVDevice[j] = pLogical;
10101713e81bSScott Long 				pLogical->pVBus = pVBus;
10111713e81bSScott Long 
10121713e81bSScott Long 				if (j>0 && pLogical->vf_bootmark) {
10131713e81bSScott Long 					if (pVBus->pVDevice[0]->vf_bootmark) {
10141713e81bSScott Long 						fResetBootMark(pLogical);
1015d2bd3ab9SScott Long 					}
1016d2bd3ab9SScott Long 					else {
1017d2bd3ab9SScott Long 						do { pVBus->pVDevice[j] = pVBus->pVDevice[j-1]; } while (--j);
10181713e81bSScott Long 						pVBus->pVDevice[0] = pLogical;
10191713e81bSScott Long 					}
10201713e81bSScott Long 				}
10211713e81bSScott Long 			}
10221713e81bSScott Long 		}
10231713e81bSScott Long 	}
10241713e81bSScott Long }
10251713e81bSScott Long 
10261713e81bSScott Long PVDevice
10271713e81bSScott Long GetSpareDisk(_VBUS_ARG PVDevice pArray)
10281713e81bSScott Long {
1029d2bd3ab9SScott Long 	IAL_ADAPTER_T *pAdapter = (IAL_ADAPTER_T *)pArray->pVBus->OsExt;
103064470755SXin LI 	LBA_T capacity = LongDiv(pArray->VDeviceCapacity, pArray->u.array.bArnMember-1);
103164470755SXin LI 	LBA_T thiscap, maxcap = MAX_LBA_T;
10321713e81bSScott Long 	PVDevice pVDevice, pFind = NULL;
10331713e81bSScott Long 	int i;
10341713e81bSScott Long 
1035d2bd3ab9SScott Long 	for(i=0;i<MV_SATA_CHANNELS_NUM;i++)
1036d2bd3ab9SScott Long 	{
10371713e81bSScott Long 		pVDevice = &pAdapter->VDevices[i];
10381713e81bSScott Long 		if(!pVDevice)
10391713e81bSScott Long 			continue;
1040d2bd3ab9SScott Long 		thiscap = pArray->vf_format_v2? pVDevice->u.disk.dDeRealCapacity : pVDevice->VDeviceCapacity;
10411713e81bSScott Long 		/* find the smallest usable spare disk */
10421713e81bSScott Long 		if (pVDevice->VDeviceType==VD_SPARE &&
1043d2bd3ab9SScott Long 			pVDevice->u.disk.df_on_line &&
1044d2bd3ab9SScott Long 			thiscap < maxcap &&
1045d2bd3ab9SScott Long 			thiscap >= capacity)
1046d2bd3ab9SScott Long 		{
10471713e81bSScott Long 				maxcap = pVDevice->VDeviceCapacity;
10481713e81bSScott Long 				pFind = pVDevice;
10491713e81bSScott Long 		}
10501713e81bSScott Long 	}
10511713e81bSScott Long 	return pFind;
10521713e81bSScott Long }
10531713e81bSScott Long 
10541713e81bSScott Long /******************************************************************
10551713e81bSScott Long  * IO ATA Command
10561713e81bSScott Long  *******************************************************************/
10571713e81bSScott Long int HPTLIBAPI
10581713e81bSScott Long fDeReadWrite(PDevice pDev, ULONG Lba, UCHAR Cmd, void *tmpBuffer)
10591713e81bSScott Long {
10601713e81bSScott Long 	return mvReadWrite(pDev->mv, Lba, Cmd, tmpBuffer);
10611713e81bSScott Long }
10621713e81bSScott Long 
10631713e81bSScott Long void HPTLIBAPI fDeSelectMode(PDevice pDev, UCHAR NewMode)
10641713e81bSScott Long {
1065d2bd3ab9SScott Long 	MV_SATA_CHANNEL *pSataChannel = pDev->mv;
1066d2bd3ab9SScott Long 	MV_SATA_ADAPTER *pSataAdapter = pSataChannel->mvSataAdapter;
1067d2bd3ab9SScott Long 	MV_U8 channelIndex = pSataChannel->channelNumber;
10681713e81bSScott Long 	UCHAR mvMode;
10691713e81bSScott Long 	/* 508x don't use MW-DMA? */
10701713e81bSScott Long 	if (NewMode>4 && NewMode<8) NewMode = 4;
10711713e81bSScott Long 	pDev->bDeModeSetting = NewMode;
10721713e81bSScott Long 	if (NewMode<=4)
10731713e81bSScott Long 		mvMode = MV_ATA_TRANSFER_PIO_0 + NewMode;
10741713e81bSScott Long 	else
10751713e81bSScott Long 		mvMode = MV_ATA_TRANSFER_UDMA_0 + (NewMode-8);
10761713e81bSScott Long 
10771713e81bSScott Long 	/*To fix 88i8030 bug*/
10781713e81bSScott Long 	if (mvMode > MV_ATA_TRANSFER_UDMA_0 && mvMode < MV_ATA_TRANSFER_UDMA_4)
10791713e81bSScott Long 		mvMode = MV_ATA_TRANSFER_UDMA_0;
10801713e81bSScott Long 
10811713e81bSScott Long 	mvSataDisableChannelDma(pSataAdapter, channelIndex);
10821713e81bSScott Long 	/* Flush pending commands */
10831713e81bSScott Long 	mvSataFlushDmaQueue (pSataAdapter, channelIndex, MV_FLUSH_TYPE_NONE);
10841713e81bSScott Long 
10851713e81bSScott Long 	if (mvStorageDevATASetFeatures(pSataAdapter, channelIndex,
1086d2bd3ab9SScott Long 								   MV_ATA_SET_FEATURES_TRANSFER,
1087d2bd3ab9SScott Long 								   mvMode, 0, 0, 0) == MV_FALSE)
1088d2bd3ab9SScott Long 	{
10891713e81bSScott Long 		KdPrint(("channel %d: Set Features failed\n", channelIndex));
10901713e81bSScott Long 	}
10911713e81bSScott Long 	/* Enable EDMA */
10921713e81bSScott Long 	if (mvSataEnableChannelDma(pSataAdapter, channelIndex) == MV_FALSE)
10931713e81bSScott Long 		KdPrint(("Failed to enable DMA, channel=%d", channelIndex));
10941713e81bSScott Long }
10951713e81bSScott Long 
109664470755SXin LI int HPTLIBAPI fDeSetTCQ(PDevice pDev, int enable, int depth)
109764470755SXin LI {
109864470755SXin LI 	MV_SATA_CHANNEL *pSataChannel = pDev->mv;
109964470755SXin LI 	MV_SATA_ADAPTER *pSataAdapter = pSataChannel->mvSataAdapter;
110064470755SXin LI 	MV_U8 channelIndex = pSataChannel->channelNumber;
110164470755SXin LI 	IAL_ADAPTER_T *pAdapter = pSataAdapter->IALData;
110264470755SXin LI 	MV_CHANNEL		*channelInfo = &(pAdapter->mvChannel[channelIndex]);
110364470755SXin LI 	int dmaActive = pSataChannel->queueCommandsEnabled;
110464470755SXin LI 	int ret = 0;
110564470755SXin LI 
110664470755SXin LI 	if (dmaActive) {
110764470755SXin LI 		mvSataDisableChannelDma(pSataAdapter, channelIndex);
110864470755SXin LI 		mvSataFlushDmaQueue(pSataAdapter,channelIndex,MV_FLUSH_TYPE_CALLBACK);
110964470755SXin LI 	}
111064470755SXin LI 
111164470755SXin LI 	if (enable) {
111264470755SXin LI 		if (pSataChannel->queuedDMA == MV_EDMA_MODE_NOT_QUEUED &&
111364470755SXin LI 			(pSataChannel->identifyDevice[IDEN_SUPPORTED_COMMANDS2] & (0x2))) {
111464470755SXin LI 			UCHAR depth = ((pSataChannel->identifyDevice[IDEN_QUEUE_DEPTH]) & 0x1f) + 1;
111564470755SXin LI 			channelInfo->queueDepth = (depth==32)? 31 : depth;
111664470755SXin LI 			mvSataConfigEdmaMode(pSataAdapter, channelIndex, MV_EDMA_MODE_QUEUED, depth);
111764470755SXin LI 			ret = 1;
111864470755SXin LI 		}
111964470755SXin LI 	}
112064470755SXin LI 	else
112164470755SXin LI 	{
112264470755SXin LI 		if (pSataChannel->queuedDMA != MV_EDMA_MODE_NOT_QUEUED) {
112364470755SXin LI 			channelInfo->queueDepth = 2;
112464470755SXin LI 			mvSataConfigEdmaMode(pSataAdapter, channelIndex, MV_EDMA_MODE_NOT_QUEUED, 0);
112564470755SXin LI 			ret = 1;
112664470755SXin LI 		}
112764470755SXin LI 	}
112864470755SXin LI 
112964470755SXin LI 	if (dmaActive)
113064470755SXin LI 		mvSataEnableChannelDma(pSataAdapter,channelIndex);
113164470755SXin LI 	return ret;
113264470755SXin LI }
113364470755SXin LI 
113464470755SXin LI int HPTLIBAPI fDeSetNCQ(PDevice pDev, int enable, int depth)
113564470755SXin LI {
113664470755SXin LI 	return 0;
113764470755SXin LI }
113864470755SXin LI 
113964470755SXin LI int HPTLIBAPI fDeSetWriteCache(PDevice pDev, int enable)
114064470755SXin LI {
114164470755SXin LI 	MV_SATA_CHANNEL *pSataChannel = pDev->mv;
114264470755SXin LI 	MV_SATA_ADAPTER *pSataAdapter = pSataChannel->mvSataAdapter;
114364470755SXin LI 	MV_U8 channelIndex = pSataChannel->channelNumber;
114464470755SXin LI 	IAL_ADAPTER_T *pAdapter = pSataAdapter->IALData;
114564470755SXin LI 	MV_CHANNEL		*channelInfo = &(pAdapter->mvChannel[channelIndex]);
114664470755SXin LI 	int dmaActive = pSataChannel->queueCommandsEnabled;
114764470755SXin LI 	int ret = 0;
114864470755SXin LI 
114964470755SXin LI 	if (dmaActive) {
115064470755SXin LI 		mvSataDisableChannelDma(pSataAdapter, channelIndex);
115164470755SXin LI 		mvSataFlushDmaQueue(pSataAdapter,channelIndex,MV_FLUSH_TYPE_CALLBACK);
115264470755SXin LI 	}
115364470755SXin LI 
115464470755SXin LI 	if ((pSataChannel->identifyDevice[82] & (0x20))) {
115564470755SXin LI 		if (enable) {
115664470755SXin LI 			if (mvStorageDevATASetFeatures(pSataAdapter, channelIndex,
115764470755SXin LI 				MV_ATA_SET_FEATURES_ENABLE_WCACHE, 0, 0, 0, 0))
115864470755SXin LI 			{
115964470755SXin LI 				channelInfo->writeCacheEnabled = MV_TRUE;
116064470755SXin LI 				ret = 1;
116164470755SXin LI 			}
116264470755SXin LI 		}
116364470755SXin LI 		else {
116464470755SXin LI 			if (mvStorageDevATASetFeatures(pSataAdapter, channelIndex,
116564470755SXin LI 				MV_ATA_SET_FEATURES_DISABLE_WCACHE, 0, 0, 0, 0))
116664470755SXin LI 			{
116764470755SXin LI 				channelInfo->writeCacheEnabled = MV_FALSE;
116864470755SXin LI 				ret = 1;
116964470755SXin LI 			}
117064470755SXin LI 		}
117164470755SXin LI 	}
117264470755SXin LI 
117364470755SXin LI 	if (dmaActive)
117464470755SXin LI 		mvSataEnableChannelDma(pSataAdapter,channelIndex);
117564470755SXin LI 	return ret;
117664470755SXin LI }
117764470755SXin LI 
117864470755SXin LI int HPTLIBAPI fDeSetReadAhead(PDevice pDev, int enable)
117964470755SXin LI {
118064470755SXin LI 	MV_SATA_CHANNEL *pSataChannel = pDev->mv;
118164470755SXin LI 	MV_SATA_ADAPTER *pSataAdapter = pSataChannel->mvSataAdapter;
118264470755SXin LI 	MV_U8 channelIndex = pSataChannel->channelNumber;
118364470755SXin LI 	IAL_ADAPTER_T *pAdapter = pSataAdapter->IALData;
118464470755SXin LI 	MV_CHANNEL		*channelInfo = &(pAdapter->mvChannel[channelIndex]);
118564470755SXin LI 	int dmaActive = pSataChannel->queueCommandsEnabled;
118664470755SXin LI 	int ret = 0;
118764470755SXin LI 
118864470755SXin LI 	if (dmaActive) {
118964470755SXin LI 		mvSataDisableChannelDma(pSataAdapter, channelIndex);
119064470755SXin LI 		mvSataFlushDmaQueue(pSataAdapter,channelIndex,MV_FLUSH_TYPE_CALLBACK);
119164470755SXin LI 	}
119264470755SXin LI 
119364470755SXin LI 	if ((pSataChannel->identifyDevice[82] & (0x40))) {
119464470755SXin LI 		if (enable) {
119564470755SXin LI 			if (mvStorageDevATASetFeatures(pSataAdapter, channelIndex,
119664470755SXin LI 				MV_ATA_SET_FEATURES_ENABLE_RLA, 0, 0, 0, 0))
119764470755SXin LI 			{
119864470755SXin LI 				channelInfo->readAheadEnabled = MV_TRUE;
119964470755SXin LI 				ret = 1;
120064470755SXin LI 			}
120164470755SXin LI 		}
120264470755SXin LI 		else {
120364470755SXin LI 			if (mvStorageDevATASetFeatures(pSataAdapter, channelIndex,
120464470755SXin LI 				MV_ATA_SET_FEATURES_DISABLE_RLA, 0, 0, 0, 0))
120564470755SXin LI 			{
120664470755SXin LI 				channelInfo->readAheadEnabled = MV_FALSE;
120764470755SXin LI 				ret = 1;
120864470755SXin LI 			}
120964470755SXin LI 		}
121064470755SXin LI 	}
121164470755SXin LI 
121264470755SXin LI 	if (dmaActive)
121364470755SXin LI 		mvSataEnableChannelDma(pSataAdapter,channelIndex);
121464470755SXin LI 	return ret;
121564470755SXin LI }
121664470755SXin LI 
12171713e81bSScott Long #ifdef SUPPORT_ARRAY
12181713e81bSScott Long #define IdeRegisterVDevice  fCheckArray
12191713e81bSScott Long #else
12201713e81bSScott Long void
12211713e81bSScott Long IdeRegisterVDevice(PDevice pDev)
12221713e81bSScott Long {
12231713e81bSScott Long 	PVDevice pVDev = Map2pVDevice(pDev);
12241713e81bSScott Long 
12251713e81bSScott Long 	pVDev->VDeviceType = pDev->df_atapi? VD_ATAPI :
12261713e81bSScott Long 						 pDev->df_removable_drive? VD_REMOVABLE : VD_SINGLE_DISK;
12271713e81bSScott Long 	pVDev->vf_online = 1;
12281713e81bSScott Long 	pVDev->VDeviceCapacity = pDev->dDeRealCapacity;
12291713e81bSScott Long 	pVDev->pfnSendCommand = pfnSendCommand[pVDev->VDeviceType];
12301713e81bSScott Long 	pVDev->pfnDeviceFailed = pfnDeviceFailed[pVDev->VDeviceType];
12311713e81bSScott Long }
12321713e81bSScott Long #endif
12331713e81bSScott Long 
1234d2bd3ab9SScott Long static __inline PBUS_DMAMAP
1235d2bd3ab9SScott Long dmamap_get(struct IALAdapter * pAdapter)
1236d2bd3ab9SScott Long {
1237d2bd3ab9SScott Long 	PBUS_DMAMAP	p = pAdapter->pbus_dmamap_list;
1238d2bd3ab9SScott Long 	if (p)
1239d2bd3ab9SScott Long 		pAdapter->pbus_dmamap_list = p-> next;
1240d2bd3ab9SScott Long 	return p;
1241d2bd3ab9SScott Long }
1242d2bd3ab9SScott Long 
1243d2bd3ab9SScott Long static __inline void
1244d2bd3ab9SScott Long dmamap_put(PBUS_DMAMAP p)
1245d2bd3ab9SScott Long {
1246d2bd3ab9SScott Long 	p->next = p->pAdapter->pbus_dmamap_list;
1247d2bd3ab9SScott Long 	p->pAdapter->pbus_dmamap_list = p;
1248d2bd3ab9SScott Long }
1249d2bd3ab9SScott Long 
12501713e81bSScott Long static int num_adapters = 0;
12511713e81bSScott Long static int
12521713e81bSScott Long init_adapter(IAL_ADAPTER_T *pAdapter)
12531713e81bSScott Long {
12541713e81bSScott Long 	PVBus _vbus_p = &pAdapter->VBus;
12551713e81bSScott Long 	MV_SATA_ADAPTER *pMvSataAdapter;
1256d2bd3ab9SScott Long 	int i, channel, rid;
12571713e81bSScott Long 
12581713e81bSScott Long 	PVDevice pVDev;
12591713e81bSScott Long 
126049b3fc40SJohn Baldwin 	mtx_init(&pAdapter->lock, "hptsleeplock", NULL, MTX_DEF);
126149b3fc40SJohn Baldwin 	callout_init_mtx(&pAdapter->event_timer_connect, &pAdapter->lock, 0);
126249b3fc40SJohn Baldwin 	callout_init_mtx(&pAdapter->event_timer_disconnect, &pAdapter->lock, 0);
126364470755SXin LI 
126449b3fc40SJohn Baldwin 	sx_xlock(&hptmv_list_lock);
12651713e81bSScott Long 	pAdapter->next = 0;
12661713e81bSScott Long 
12674d24901aSPedro F. Giffuni 	if(gIal_Adapter == NULL){
12681713e81bSScott Long 		gIal_Adapter = pAdapter;
12691713e81bSScott Long 		pCurAdapter = gIal_Adapter;
1270d2bd3ab9SScott Long 	}
1271d2bd3ab9SScott Long 	else {
12721713e81bSScott Long 		pCurAdapter->next = pAdapter;
12731713e81bSScott Long 		pCurAdapter = pAdapter;
12741713e81bSScott Long 	}
127549b3fc40SJohn Baldwin 	sx_xunlock(&hptmv_list_lock);
12761713e81bSScott Long 
12771713e81bSScott Long 	pAdapter->outstandingCommands = 0;
12781713e81bSScott Long 
12791713e81bSScott Long 	pMvSataAdapter = &(pAdapter->mvSataAdapter);
12801713e81bSScott Long 	_vbus_p->OsExt = (void *)pAdapter;
12811713e81bSScott Long 	pMvSataAdapter->IALData = pAdapter;
12821713e81bSScott Long 
1283b6f97155SScott Long 	if (bus_dma_tag_create(bus_get_dma_tag(pAdapter->hpt_dev),/* parent */
1284d2bd3ab9SScott Long 			4,	/* alignment */
1285d2bd3ab9SScott Long 			BUS_SPACE_MAXADDR_32BIT+1, /* boundary */
1286d2bd3ab9SScott Long 			BUS_SPACE_MAXADDR,	/* lowaddr */
1287d2bd3ab9SScott Long 			BUS_SPACE_MAXADDR,	/* highaddr */
1288d2bd3ab9SScott Long 			NULL, NULL, 		/* filter, filterarg */
1289d2bd3ab9SScott Long 			PAGE_SIZE * (MAX_SG_DESCRIPTORS-1), /* maxsize */
1290d2bd3ab9SScott Long 			MAX_SG_DESCRIPTORS, /* nsegments */
1291d2bd3ab9SScott Long 			0x10000,	/* maxsegsize */
1292d2bd3ab9SScott Long 			BUS_DMA_WAITOK, 	/* flags */
1293d2bd3ab9SScott Long 			busdma_lock_mutex,	/* lockfunc */
129449b3fc40SJohn Baldwin 			&pAdapter->lock,	/* lockfuncarg */
1295d2bd3ab9SScott Long 			&pAdapter->io_dma_parent /* tag */))
1296d2bd3ab9SScott Long 		{
1297c2ede4b3SMartin Blapp 			return ENXIO;
12981713e81bSScott Long 	}
12991713e81bSScott Long 
13001713e81bSScott Long 
1301d2bd3ab9SScott Long 	if (hptmv_allocate_edma_queues(pAdapter))
1302d2bd3ab9SScott Long 	{
130364470755SXin LI 		MV_ERROR("RR18xx: Failed to allocate memory for EDMA queues\n");
1304d2bd3ab9SScott Long 		return ENOMEM;
13051713e81bSScott Long 	}
13061713e81bSScott Long 
13071713e81bSScott Long 	/* also map EPROM address */
13081713e81bSScott Long 	rid = 0x10;
1309eff83876SJustin Hibbits 	if (!(pAdapter->mem_res = bus_alloc_resource_any(pAdapter->hpt_dev,
1310eff83876SJustin Hibbits 			SYS_RES_MEMORY, &rid, RF_ACTIVE))
1311d2bd3ab9SScott Long 		||
1312d2bd3ab9SScott Long 		!(pMvSataAdapter->adapterIoBaseAddress = rman_get_virtual(pAdapter->mem_res)))
1313d2bd3ab9SScott Long 	{
131464470755SXin LI 		MV_ERROR("RR18xx: Failed to remap memory space\n");
1315d2bd3ab9SScott Long 		hptmv_free_edma_queues(pAdapter);
1316d2bd3ab9SScott Long 		return ENXIO;
13171713e81bSScott Long 	}
1318d2bd3ab9SScott Long 	else
1319d2bd3ab9SScott Long 	{
132064470755SXin LI 		KdPrint(("RR18xx: io base address 0x%p\n", pMvSataAdapter->adapterIoBaseAddress));
1321d2bd3ab9SScott Long 	}
13221713e81bSScott Long 
13231713e81bSScott Long 	pMvSataAdapter->adapterId = num_adapters++;
13241713e81bSScott Long 	/* get the revision ID */
1325d2bd3ab9SScott Long 	pMvSataAdapter->pciConfigRevisionId = pci_read_config(pAdapter->hpt_dev, PCIR_REVID, 1);
13261713e81bSScott Long 	pMvSataAdapter->pciConfigDeviceId = pci_get_device(pAdapter->hpt_dev);
13271713e81bSScott Long 
132864470755SXin LI 	/* init RR18xx */
13291713e81bSScott Long 	pMvSataAdapter->intCoalThre[0]= 1;
13301713e81bSScott Long 	pMvSataAdapter->intCoalThre[1]= 1;
13311713e81bSScott Long 	pMvSataAdapter->intTimeThre[0] = 1;
13321713e81bSScott Long 	pMvSataAdapter->intTimeThre[1] = 1;
13331713e81bSScott Long 	pMvSataAdapter->pciCommand = 0x0107E371;
13341713e81bSScott Long 	pMvSataAdapter->pciSerrMask = 0xd77fe6ul;
13351713e81bSScott Long 	pMvSataAdapter->pciInterruptMask = 0xd77fe6ul;
13361713e81bSScott Long 	pMvSataAdapter->mvSataEventNotify = hptmv_event_notify;
13371713e81bSScott Long 
1338d2bd3ab9SScott Long 	if (mvSataInitAdapter(pMvSataAdapter) == MV_FALSE)
1339d2bd3ab9SScott Long 	{
134064470755SXin LI 		MV_ERROR("RR18xx[%d]: core failed to initialize the adapter\n",
13411713e81bSScott Long 				 pMvSataAdapter->adapterId);
1342d2bd3ab9SScott Long unregister:
1343d2bd3ab9SScott Long 		bus_release_resource(pAdapter->hpt_dev, SYS_RES_MEMORY, rid, pAdapter->mem_res);
1344d2bd3ab9SScott Long 		hptmv_free_edma_queues(pAdapter);
1345d2bd3ab9SScott Long 		return ENXIO;
13461713e81bSScott Long 	}
13471713e81bSScott Long 	pAdapter->ver_601 = pMvSataAdapter->pcbVersion;
13481713e81bSScott Long 
13491713e81bSScott Long #ifndef FOR_DEMO
13501713e81bSScott Long 	set_fail_leds(pMvSataAdapter, 0);
13511713e81bSScott Long #endif
13521713e81bSScott Long 
13531713e81bSScott Long 	/* setup command blocks */
13541713e81bSScott Long 	KdPrint(("Allocate command blocks\n"));
13551713e81bSScott Long 	_vbus_(pFreeCommands) = 0;
1356d2bd3ab9SScott Long 	pAdapter->pCommandBlocks =
1357d2bd3ab9SScott Long 		malloc(sizeof(struct _Command) * MAX_COMMAND_BLOCKS_FOR_EACH_VBUS, M_DEVBUF, M_NOWAIT);
13587d9aed9cSScott Long 	KdPrint(("pCommandBlocks:%p\n",pAdapter->pCommandBlocks));
1359d2bd3ab9SScott Long 	if (!pAdapter->pCommandBlocks) {
1360d2bd3ab9SScott Long 		MV_ERROR("insufficient memory\n");
1361d2bd3ab9SScott Long 		goto unregister;
13621713e81bSScott Long 	}
13631713e81bSScott Long 
1364d2bd3ab9SScott Long 	for (i=0; i<MAX_COMMAND_BLOCKS_FOR_EACH_VBUS; i++) {
1365d2bd3ab9SScott Long 		FreeCommand(_VBUS_P &(pAdapter->pCommandBlocks[i]));
1366d2bd3ab9SScott Long 	}
1367d2bd3ab9SScott Long 
1368d2bd3ab9SScott Long 	/*Set up the bus_dmamap*/
1369d2bd3ab9SScott Long 	pAdapter->pbus_dmamap = (PBUS_DMAMAP)malloc (sizeof(struct _BUS_DMAMAP) * MAX_QUEUE_COMM, M_DEVBUF, M_NOWAIT);
1370d2bd3ab9SScott Long 	if(!pAdapter->pbus_dmamap) {
1371d2bd3ab9SScott Long 		MV_ERROR("insufficient memory\n");
1372d2bd3ab9SScott Long 		free(pAdapter->pCommandBlocks, M_DEVBUF);
1373d2bd3ab9SScott Long 		goto unregister;
1374d2bd3ab9SScott Long 	}
1375d2bd3ab9SScott Long 
1376d2bd3ab9SScott Long 	memset((void *)pAdapter->pbus_dmamap, 0, sizeof(struct _BUS_DMAMAP) * MAX_QUEUE_COMM);
1377d2bd3ab9SScott Long 	pAdapter->pbus_dmamap_list = 0;
1378d2bd3ab9SScott Long 	for (i=0; i < MAX_QUEUE_COMM; i++) {
1379d2bd3ab9SScott Long 		PBUS_DMAMAP  pmap = &(pAdapter->pbus_dmamap[i]);
1380d2bd3ab9SScott Long 		pmap->pAdapter = pAdapter;
1381d2bd3ab9SScott Long 		dmamap_put(pmap);
1382d2bd3ab9SScott Long 
1383d2bd3ab9SScott Long 		if(bus_dmamap_create(pAdapter->io_dma_parent, 0, &pmap->dma_map)) {
1384d2bd3ab9SScott Long 			MV_ERROR("Can not allocate dma map\n");
1385d2bd3ab9SScott Long 			free(pAdapter->pCommandBlocks, M_DEVBUF);
1386d2bd3ab9SScott Long 			free(pAdapter->pbus_dmamap, M_DEVBUF);
1387d2bd3ab9SScott Long 			goto unregister;
1388d2bd3ab9SScott Long 		}
138949b3fc40SJohn Baldwin 		callout_init_mtx(&pmap->timeout, &pAdapter->lock, 0);
1390d2bd3ab9SScott Long 	}
13911713e81bSScott Long 	/* setup PRD Tables */
13921713e81bSScott Long 	KdPrint(("Allocate PRD Tables\n"));
13931713e81bSScott Long 	pAdapter->pFreePRDLink = 0;
13941713e81bSScott Long 
1395d2bd3ab9SScott Long 	pAdapter->prdTableAddr = (PUCHAR)contigmalloc(
1396d2bd3ab9SScott Long 		(PRD_ENTRIES_SIZE*PRD_TABLES_FOR_VBUS + 32), M_DEVBUF, M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0ul);
13971713e81bSScott Long 
13987d9aed9cSScott Long 	KdPrint(("prdTableAddr:%p\n",pAdapter->prdTableAddr));
13991713e81bSScott Long 	if (!pAdapter->prdTableAddr) {
14001713e81bSScott Long 		MV_ERROR("insufficient PRD Tables\n");
14011713e81bSScott Long 		goto unregister;
14021713e81bSScott Long 	}
1403d2bd3ab9SScott Long 	pAdapter->prdTableAlignedAddr = (PUCHAR)(((ULONG_PTR)pAdapter->prdTableAddr + 0x1f) & ~(ULONG_PTR)0x1fL);
1404d2bd3ab9SScott Long 	{
1405d2bd3ab9SScott Long 		PUCHAR PRDTable = pAdapter->prdTableAlignedAddr;
1406d2bd3ab9SScott Long 		for (i=0; i<PRD_TABLES_FOR_VBUS; i++)
1407d2bd3ab9SScott Long 		{
1408d2bd3ab9SScott Long /*			KdPrint(("i=%d,pAdapter->pFreePRDLink=%p\n",i,pAdapter->pFreePRDLink)); */
14091713e81bSScott Long 			FreePRDTable(pAdapter, PRDTable);
14101713e81bSScott Long 			PRDTable += PRD_ENTRIES_SIZE;
14111713e81bSScott Long 		}
1412d2bd3ab9SScott Long 	}
14131713e81bSScott Long 
14141713e81bSScott Long 	/* enable the adapter interrupts */
14151713e81bSScott Long 
14161713e81bSScott Long 	/* configure and start the connected channels*/
1417d2bd3ab9SScott Long 	for (channel = 0; channel < MV_SATA_CHANNELS_NUM; channel++)
1418d2bd3ab9SScott Long 	{
14191713e81bSScott Long 		pAdapter->mvChannel[channel].online = MV_FALSE;
14201713e81bSScott Long 		if (mvSataIsStorageDeviceConnected(pMvSataAdapter, channel)
1421d2bd3ab9SScott Long 			== MV_TRUE)
1422d2bd3ab9SScott Long 		{
142364470755SXin LI 			KdPrint(("RR18xx[%d]: channel %d is connected\n",
14241713e81bSScott Long 					  pMvSataAdapter->adapterId, channel));
14251713e81bSScott Long 
1426d2bd3ab9SScott Long 			if (hptmv_init_channel(pAdapter, channel) == 0)
1427d2bd3ab9SScott Long 			{
1428d2bd3ab9SScott Long 				if (mvSataConfigureChannel(pMvSataAdapter, channel) == MV_FALSE)
1429d2bd3ab9SScott Long 				{
143064470755SXin LI 					MV_ERROR("RR18xx[%d]: Failed to configure channel"
1431d2bd3ab9SScott Long 							 " %d\n",pMvSataAdapter->adapterId, channel);
14321713e81bSScott Long 					hptmv_free_channel(pAdapter, channel);
14331713e81bSScott Long 				}
1434d2bd3ab9SScott Long 				else
1435d2bd3ab9SScott Long 				{
1436d2bd3ab9SScott Long 					if (start_channel(pAdapter, channel))
1437d2bd3ab9SScott Long 					{
143864470755SXin LI 						MV_ERROR("RR18xx[%d]: Failed to start channel,"
14391713e81bSScott Long 								 " channel=%d\n",pMvSataAdapter->adapterId,
14401713e81bSScott Long 								 channel);
14411713e81bSScott Long 						hptmv_free_channel(pAdapter, channel);
14421713e81bSScott Long 					}
14431713e81bSScott Long 					pAdapter->mvChannel[channel].online = MV_TRUE;
1444d2bd3ab9SScott Long 					/*  mvSataChannelSetEdmaLoopBackMode(pMvSataAdapter,
1445d2bd3ab9SScott Long 													   channel,
1446d2bd3ab9SScott Long 													   MV_TRUE);*/
1447d2bd3ab9SScott Long 				}
1448d2bd3ab9SScott Long 			}
14491713e81bSScott Long 		}
14501713e81bSScott Long 		KdPrint(("pAdapter->mvChannel[channel].online:%x, channel:%d\n",
14511713e81bSScott Long 			pAdapter->mvChannel[channel].online, channel));
14521713e81bSScott Long 	}
14531713e81bSScott Long 
14541713e81bSScott Long #ifdef SUPPORT_ARRAY
14551713e81bSScott Long 	for(i = MAX_ARRAY_DEVICE - 1; i >= 0; i--) {
14561713e81bSScott Long 		pVDev = ArrayTables(i);
14571713e81bSScott Long 		mArFreeArrayTable(pVDev);
14581713e81bSScott Long 	}
14591713e81bSScott Long #endif
14601713e81bSScott Long 
14611713e81bSScott Long 	KdPrint(("Initialize Devices\n"));
14621713e81bSScott Long 	for (channel = 0; channel < MV_SATA_CHANNELS_NUM; channel++) {
1463d2bd3ab9SScott Long 		MV_SATA_CHANNEL *pMvSataChannel = pMvSataAdapter->sataChannel[channel];
14641713e81bSScott Long 		if (pMvSataChannel) {
14651713e81bSScott Long 			init_vdev_params(pAdapter, channel);
14661713e81bSScott Long 			IdeRegisterVDevice(&pAdapter->VDevices[channel].u.disk);
14671713e81bSScott Long 		}
14681713e81bSScott Long 	}
14691713e81bSScott Long #ifdef SUPPORT_ARRAY
14701713e81bSScott Long 	CheckArrayCritical(_VBUS_P0);
14711713e81bSScott Long #endif
14721713e81bSScott Long 	_vbus_p->nInstances = 1;
14731713e81bSScott Long 	fRegisterVdevice(pAdapter);
14741713e81bSScott Long 
14751713e81bSScott Long 	for (channel=0;channel<MV_SATA_CHANNELS_NUM;channel++) {
14761713e81bSScott Long 		pVDev = _vbus_p->pVDevice[channel];
14771713e81bSScott Long 		if (pVDev && pVDev->vf_online)
14781713e81bSScott Long 			fCheckBootable(pVDev);
14791713e81bSScott Long 	}
14801713e81bSScott Long 
14811713e81bSScott Long #if defined(SUPPORT_ARRAY) && defined(_RAID5N_)
14821713e81bSScott Long 	init_raid5_memory(_VBUS_P0);
14831713e81bSScott Long 	_vbus_(r5).enable_write_back = 1;
148464470755SXin LI 	printf("RR18xx: RAID5 write-back %s\n", _vbus_(r5).enable_write_back? "enabled" : "disabled");
14851713e81bSScott Long #endif
14861713e81bSScott Long 
14871713e81bSScott Long 	mvSataUnmaskAdapterInterrupt(pMvSataAdapter);
14881713e81bSScott Long 	return 0;
14891713e81bSScott Long }
14901713e81bSScott Long 
14911713e81bSScott Long int
14921713e81bSScott Long MvSataResetChannel(MV_SATA_ADAPTER *pMvSataAdapter, MV_U8 channel)
14931713e81bSScott Long {
14941713e81bSScott Long 	IAL_ADAPTER_T   *pAdapter = (IAL_ADAPTER_T *)pMvSataAdapter->IALData;
14951713e81bSScott Long 
14961713e81bSScott Long 	mvSataDisableChannelDma(pMvSataAdapter, channel);
14971713e81bSScott Long 	/* Flush pending commands */
14981713e81bSScott Long 	mvSataFlushDmaQueue (pMvSataAdapter, channel, MV_FLUSH_TYPE_CALLBACK);
14991713e81bSScott Long 
15001713e81bSScott Long 	/* Software reset channel */
1501d2bd3ab9SScott Long 	if (mvStorageDevATASoftResetDevice(pMvSataAdapter, channel) == MV_FALSE)
1502d2bd3ab9SScott Long 	{
150364470755SXin LI 		MV_ERROR("RR18xx [%d,%d]: failed to perform Software reset\n",
15041713e81bSScott Long 				 pMvSataAdapter->adapterId, channel);
1505d2bd3ab9SScott Long 		hptmv_free_channel(pAdapter, channel);
15061713e81bSScott Long 		return -1;
15071713e81bSScott Long 	}
15081713e81bSScott Long 
15091713e81bSScott Long 	/* Hardware reset channel */
1510d2bd3ab9SScott Long 	if (mvSataChannelHardReset(pMvSataAdapter, channel)== MV_FALSE)
1511d2bd3ab9SScott Long 	{
151264470755SXin LI 		MV_ERROR("RR18xx [%d,%d] Failed to Hard reser the SATA channel\n",
1513d2bd3ab9SScott Long 				 pMvSataAdapter->adapterId, channel);
15141713e81bSScott Long 		hptmv_free_channel(pAdapter, channel);
15151713e81bSScott Long 		return -1;
15161713e81bSScott Long 	}
15171713e81bSScott Long 
1518d2bd3ab9SScott Long 	if (mvSataIsStorageDeviceConnected(pMvSataAdapter, channel) == MV_FALSE)
1519d2bd3ab9SScott Long 	{
152064470755SXin LI 		 MV_ERROR("RR18xx [%d,%d] Failed to Connect Device\n",
15211713e81bSScott Long 				 pMvSataAdapter->adapterId, channel);
15221713e81bSScott Long 		hptmv_free_channel(pAdapter, channel);
15231713e81bSScott Long 		return -1;
1524d2bd3ab9SScott Long 	}else
1525d2bd3ab9SScott Long 	{
1526d2bd3ab9SScott Long 		MV_ERROR("channel %d: perform recalibrate command", channel);
1527d2bd3ab9SScott Long 		if (!mvStorageDevATAExecuteNonUDMACommand(pMvSataAdapter, channel,
1528d2bd3ab9SScott Long 								MV_NON_UDMA_PROTOCOL_NON_DATA,
1529d2bd3ab9SScott Long 								MV_FALSE,
1530d2bd3ab9SScott Long 								NULL,	 /* pBuffer*/
1531d2bd3ab9SScott Long 								0,		 /* count  */
1532d2bd3ab9SScott Long 								0,		/*features*/
1533d2bd3ab9SScott Long 										/* sectorCount */
1534d2bd3ab9SScott Long 								0,
1535d2bd3ab9SScott Long 								0,	/* lbaLow */
1536d2bd3ab9SScott Long 								0,	/* lbaMid */
1537d2bd3ab9SScott Long 									/* lbaHigh */
1538d2bd3ab9SScott Long 								0,
1539d2bd3ab9SScott Long 								0,		/* device */
1540d2bd3ab9SScott Long 										/* command */
1541d2bd3ab9SScott Long 								0x10))
1542d2bd3ab9SScott Long 			MV_ERROR("channel %d: recalibrate failed", channel);
1543d2bd3ab9SScott Long 
15441713e81bSScott Long 		/* Set transfer mode */
15451713e81bSScott Long 		if((mvStorageDevATASetFeatures(pMvSataAdapter, channel,
1546d2bd3ab9SScott Long 						MV_ATA_SET_FEATURES_TRANSFER,
1547d2bd3ab9SScott Long 						MV_ATA_TRANSFER_PIO_SLOW, 0, 0, 0) == MV_FALSE) ||
15481713e81bSScott Long 			(mvStorageDevATASetFeatures(pMvSataAdapter, channel,
15491713e81bSScott Long 						MV_ATA_SET_FEATURES_TRANSFER,
1550d2bd3ab9SScott Long 						pAdapter->mvChannel[channel].maxPioModeSupported, 0, 0, 0) == MV_FALSE) ||
1551d2bd3ab9SScott Long 			(mvStorageDevATASetFeatures(pMvSataAdapter, channel,
1552d2bd3ab9SScott Long 						MV_ATA_SET_FEATURES_TRANSFER,
1553d2bd3ab9SScott Long 						pAdapter->mvChannel[channel].maxUltraDmaModeSupported, 0, 0, 0) == MV_FALSE) )
1554d2bd3ab9SScott Long 		{
15551713e81bSScott Long 			MV_ERROR("channel %d: Set Features failed", channel);
15561713e81bSScott Long 			hptmv_free_channel(pAdapter, channel);
15571713e81bSScott Long 			return -1;
15581713e81bSScott Long 		}
15591713e81bSScott Long 		/* Enable EDMA */
1560d2bd3ab9SScott Long 		if (mvSataEnableChannelDma(pMvSataAdapter, channel) == MV_FALSE)
1561d2bd3ab9SScott Long 		{
15621713e81bSScott Long 			MV_ERROR("Failed to enable DMA, channel=%d", channel);
15631713e81bSScott Long 			hptmv_free_channel(pAdapter, channel);
15641713e81bSScott Long 			return -1;
15651713e81bSScott Long 		}
15661713e81bSScott Long 	}
15671713e81bSScott Long 	return 0;
15681713e81bSScott Long }
15691713e81bSScott Long 
15701713e81bSScott Long static int
15711713e81bSScott Long fResetActiveCommands(PVBus _vbus_p)
15721713e81bSScott Long {
1573d2bd3ab9SScott Long 	MV_SATA_ADAPTER *pMvSataAdapter = &((IAL_ADAPTER_T *)_vbus_p->OsExt)->mvSataAdapter;
15741713e81bSScott Long 	MV_U8 channel;
15751713e81bSScott Long 	for (channel=0;channel< MV_SATA_CHANNELS_NUM;channel++) {
1576d2bd3ab9SScott Long 		if (pMvSataAdapter->sataChannel[channel] && pMvSataAdapter->sataChannel[channel]->outstandingCommands)
1577d2bd3ab9SScott Long 			MvSataResetChannel(pMvSataAdapter,channel);
15781713e81bSScott Long 	}
15791713e81bSScott Long 	return 0;
15801713e81bSScott Long }
15811713e81bSScott Long 
1582d2bd3ab9SScott Long void fCompleteAllCommandsSynchronously(PVBus _vbus_p)
15831713e81bSScott Long {
15841713e81bSScott Long 	UINT cont;
15851713e81bSScott Long 	ULONG ticks = 0;
15861713e81bSScott Long 	MV_U8 channel;
1587d2bd3ab9SScott Long 	MV_SATA_ADAPTER *pMvSataAdapter = &((IAL_ADAPTER_T *)_vbus_p->OsExt)->mvSataAdapter;
15881713e81bSScott Long 	MV_SATA_CHANNEL *pMvSataChannel;
15891713e81bSScott Long 
15901713e81bSScott Long 	do {
15911713e81bSScott Long check_cmds:
15921713e81bSScott Long 		cont = 0;
15931713e81bSScott Long 		CheckPendingCall(_VBUS_P0);
15941713e81bSScott Long #ifdef _RAID5N_
15951713e81bSScott Long 		dataxfer_poll();
15961713e81bSScott Long 		xor_poll();
15971713e81bSScott Long #endif
15981713e81bSScott Long 		for (channel=0;channel< MV_SATA_CHANNELS_NUM;channel++) {
15991713e81bSScott Long 			pMvSataChannel = pMvSataAdapter->sataChannel[channel];
1600d2bd3ab9SScott Long 			if (pMvSataChannel && pMvSataChannel->outstandingCommands)
1601d2bd3ab9SScott Long 			{
16021713e81bSScott Long 				while (pMvSataChannel->outstandingCommands) {
1603d2bd3ab9SScott Long 					if (!mvSataInterruptServiceRoutine(pMvSataAdapter)) {
16041713e81bSScott Long 						StallExec(1000);
16051713e81bSScott Long 						if (ticks++ > 3000) {
1606d2bd3ab9SScott Long 							MvSataResetChannel(pMvSataAdapter,channel);
16071713e81bSScott Long 							goto check_cmds;
16081713e81bSScott Long 						}
1609d2bd3ab9SScott Long 					}
1610d2bd3ab9SScott Long 					else
16111713e81bSScott Long 						ticks = 0;
16121713e81bSScott Long 				}
16131713e81bSScott Long 				cont = 1;
16141713e81bSScott Long 			}
16151713e81bSScott Long 		}
16161713e81bSScott Long 	} while (cont);
16171713e81bSScott Long }
16181713e81bSScott Long 
16191713e81bSScott Long void
16201713e81bSScott Long fResetVBus(_VBUS_ARG0)
16211713e81bSScott Long {
16227d9aed9cSScott Long 	KdPrint(("fMvResetBus(%p)", _vbus_p));
16231713e81bSScott Long 
16241713e81bSScott Long 	/* some commands may already finished. */
16251713e81bSScott Long 	CheckPendingCall(_VBUS_P0);
16261713e81bSScott Long 
16271713e81bSScott Long 	fResetActiveCommands(_vbus_p);
16281713e81bSScott Long 	/*
16291713e81bSScott Long 	 * the other pending commands may still be finished successfully.
16301713e81bSScott Long 	 */
16311713e81bSScott Long 	fCompleteAllCommandsSynchronously(_vbus_p);
16321713e81bSScott Long 
16331713e81bSScott Long 	/* Now there should be no pending commands. No more action needed. */
16341713e81bSScott Long 	CheckIdleCall(_VBUS_P0);
16351713e81bSScott Long 
16361713e81bSScott Long 	KdPrint(("fMvResetBus() done"));
16371713e81bSScott Long }
16381713e81bSScott Long 
1639d2bd3ab9SScott Long /*No rescan function*/
16401713e81bSScott Long void
16411713e81bSScott Long fRescanAllDevice(_VBUS_ARG0)
16421713e81bSScott Long {
16431713e81bSScott Long }
16441713e81bSScott Long 
16451713e81bSScott Long static MV_BOOLEAN
1646d2bd3ab9SScott Long CommandCompletionCB(MV_SATA_ADAPTER *pMvSataAdapter,
1647d2bd3ab9SScott Long 					MV_U8 channelNum,
1648d2bd3ab9SScott Long 					MV_COMPLETION_TYPE comp_type,
1649d2bd3ab9SScott Long 					MV_VOID_PTR commandId,
1650d2bd3ab9SScott Long 					MV_U16 responseFlags,
1651d2bd3ab9SScott Long 					MV_U32 timeStamp,
1652d2bd3ab9SScott Long 					MV_STORAGE_DEVICE_REGISTERS *registerStruct)
16531713e81bSScott Long {
16541713e81bSScott Long 	PCommand pCmd = (PCommand) commandId;
16551713e81bSScott Long 	_VBUS_INST(pCmd->pVDevice->pVBus)
16561713e81bSScott Long 
16571713e81bSScott Long 	if (pCmd->uScratch.sata_param.prdAddr)
1658d2bd3ab9SScott Long 		FreePRDTable(pMvSataAdapter->IALData,pCmd->uScratch.sata_param.prdAddr);
16591713e81bSScott Long 
1660d2bd3ab9SScott Long 	switch (comp_type)
1661d2bd3ab9SScott Long 	{
16621713e81bSScott Long 	case MV_COMPLETION_TYPE_NORMAL:
16631713e81bSScott Long 		pCmd->Result = RETURN_SUCCESS;
16641713e81bSScott Long 		break;
16651713e81bSScott Long 	case MV_COMPLETION_TYPE_ABORT:
16661713e81bSScott Long 		pCmd->Result = RETURN_BUS_RESET;
16671713e81bSScott Long 		break;
16681713e81bSScott Long 	case MV_COMPLETION_TYPE_ERROR:
1669d2bd3ab9SScott Long 		 MV_ERROR("IAL: COMPLETION ERROR, adapter %d, channel %d, flags=%x\n",
1670d2bd3ab9SScott Long 				 pMvSataAdapter->adapterId, channelNum, responseFlags);
16711713e81bSScott Long 
16721713e81bSScott Long 		if (responseFlags & 4) {
1673d2bd3ab9SScott Long 			MV_ERROR("ATA regs: error %x, sector count %x, LBA low %x, LBA mid %x,"
1674d2bd3ab9SScott Long 				" LBA high %x, device %x, status %x\n",
1675d2bd3ab9SScott Long 				registerStruct->errorRegister,
16761713e81bSScott Long 				registerStruct->sectorCountRegister,
16771713e81bSScott Long 				registerStruct->lbaLowRegister,
16781713e81bSScott Long 				registerStruct->lbaMidRegister,
16791713e81bSScott Long 				registerStruct->lbaHighRegister,
16801713e81bSScott Long 				registerStruct->deviceRegister,
16811713e81bSScott Long 				registerStruct->statusRegister);
16821713e81bSScott Long 		}
1683d2bd3ab9SScott Long 		/*We can't do handleEdmaError directly here, because CommandCompletionCB is called by
1684d2bd3ab9SScott Long 		 * mv's ISR, if we retry the command, than the internel data structure may be destroyed*/
16851713e81bSScott Long 		pCmd->uScratch.sata_param.responseFlags = responseFlags;
1686d2bd3ab9SScott Long 		pCmd->uScratch.sata_param.bIdeStatus = registerStruct->statusRegister;
1687d2bd3ab9SScott Long 		pCmd->uScratch.sata_param.errorRegister = registerStruct->errorRegister;
16881713e81bSScott Long 		pCmd->pVDevice->u.disk.QueueLength--;
16891713e81bSScott Long 		CallAfterReturn(_VBUS_P (DPC_PROC)handleEdmaError,pCmd);
16901713e81bSScott Long 		return TRUE;
16911713e81bSScott Long 
16921713e81bSScott Long 	default:
16931713e81bSScott Long 		MV_ERROR(" Unknown completion type (%d)\n", comp_type);
16941713e81bSScott Long 		return MV_FALSE;
16951713e81bSScott Long 	}
16961713e81bSScott Long 
1697d2bd3ab9SScott Long 	if (pCmd->uCmd.Ide.Command == IDE_COMMAND_VERIFY && pCmd->uScratch.sata_param.cmd_priv > 1) {
16981713e81bSScott Long 		pCmd->uScratch.sata_param.cmd_priv --;
16991713e81bSScott Long 		return TRUE;
17001713e81bSScott Long 	}
17011713e81bSScott Long 	pCmd->pVDevice->u.disk.QueueLength--;
17021713e81bSScott Long 	CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
17031713e81bSScott Long 	return TRUE;
17041713e81bSScott Long }
17051713e81bSScott Long 
17061713e81bSScott Long void
17071713e81bSScott Long fDeviceSendCommand(_VBUS_ARG PCommand pCmd)
17081713e81bSScott Long {
17091713e81bSScott Long 	MV_SATA_EDMA_PRD_ENTRY  *pPRDTable = 0;
17101713e81bSScott Long 	MV_SATA_ADAPTER *pMvSataAdapter;
17111713e81bSScott Long 	MV_SATA_CHANNEL *pMvSataChannel;
1712d2bd3ab9SScott Long 	PVDevice pVDevice = pCmd->pVDevice;
1713d2bd3ab9SScott Long 	PDevice  pDevice = &pVDevice->u.disk;
171464470755SXin LI 	LBA_T    Lba = pCmd->uCmd.Ide.Lba;
1715d2bd3ab9SScott Long 	USHORT   nSector = pCmd->uCmd.Ide.nSectors;
1716d2bd3ab9SScott Long 
17171713e81bSScott Long 	MV_QUEUE_COMMAND_RESULT result;
17181713e81bSScott Long 	MV_QUEUE_COMMAND_INFO commandInfo;
1719d2bd3ab9SScott Long 	MV_UDMA_COMMAND_PARAMS  *pUdmaParams = &commandInfo.commandParams.udmaCommand;
1720d2bd3ab9SScott Long 	MV_NONE_UDMA_COMMAND_PARAMS *pNoUdmaParams = &commandInfo.commandParams.NoneUdmaCommand;
1721d2bd3ab9SScott Long 
172264470755SXin LI 	MV_BOOLEAN is48bit;
17231713e81bSScott Long 	MV_U8      channel;
17241713e81bSScott Long 	int        i=0;
17251713e81bSScott Long 
17261713e81bSScott Long 	DECLARE_BUFFER(FPSCAT_GATH, tmpSg);
17271713e81bSScott Long 
17281713e81bSScott Long 	if (!pDevice->df_on_line) {
17291713e81bSScott Long 		MV_ERROR("Device is offline");
17301713e81bSScott Long 		pCmd->Result = RETURN_BAD_DEVICE;
17311713e81bSScott Long 		CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
17321713e81bSScott Long 		return;
17331713e81bSScott Long 	}
17341713e81bSScott Long 
17351713e81bSScott Long 	pDevice->HeadPosition = pCmd->uCmd.Ide.Lba + pCmd->uCmd.Ide.nSectors;
17361713e81bSScott Long 	pMvSataChannel = pDevice->mv;
17371713e81bSScott Long 	pMvSataAdapter = pMvSataChannel->mvSataAdapter;
17381713e81bSScott Long 	channel = pMvSataChannel->channelNumber;
17391713e81bSScott Long 
1740d2bd3ab9SScott Long 	/* old RAID0 has hidden lba. Remember to clear dDeHiddenLba when delete array! */
17411713e81bSScott Long 	Lba += pDevice->dDeHiddenLba;
17421713e81bSScott Long 	/* check LBA */
17431713e81bSScott Long 	if (Lba+nSector-1 > pDevice->dDeRealCapacity) {
17441713e81bSScott Long 		pCmd->Result = RETURN_INVALID_REQUEST;
17451713e81bSScott Long 		CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
17461713e81bSScott Long 		return;
17471713e81bSScott Long 	}
17481713e81bSScott Long 
174964470755SXin LI 	/*
175064470755SXin LI 	 * always use 48bit LBA if drive supports it.
175164470755SXin LI 	 * Some Seagate drives report error if you use a 28-bit command
175264470755SXin LI 	 * to access sector 0xfffffff.
175364470755SXin LI 	 */
175464470755SXin LI 	is48bit = pMvSataChannel->lba48Address;
17551713e81bSScott Long 
1756d2bd3ab9SScott Long 	switch (pCmd->uCmd.Ide.Command)
1757d2bd3ab9SScott Long 	{
17581713e81bSScott Long 	case IDE_COMMAND_READ:
17591713e81bSScott Long 	case IDE_COMMAND_WRITE:
17601713e81bSScott Long 		if (pDevice->bDeModeSetting<8) goto pio;
17611713e81bSScott Long 
17621713e81bSScott Long 		commandInfo.type = MV_QUEUED_COMMAND_TYPE_UDMA;
17631713e81bSScott Long 		pUdmaParams->isEXT = is48bit;
17641713e81bSScott Long 		pUdmaParams->numOfSectors = nSector;
17651713e81bSScott Long 		pUdmaParams->lowLBAAddress = Lba;
17661713e81bSScott Long 		pUdmaParams->highLBAAddress = 0;
17671713e81bSScott Long 		pUdmaParams->prdHighAddr = 0;
17681713e81bSScott Long 		pUdmaParams->callBack = CommandCompletionCB;
17691713e81bSScott Long 		pUdmaParams->commandId = (MV_VOID_PTR )pCmd;
17701713e81bSScott Long 		if(pCmd->uCmd.Ide.Command == IDE_COMMAND_READ)
17711713e81bSScott Long 			pUdmaParams->readWrite = MV_UDMA_TYPE_READ;
17721713e81bSScott Long 		else
17731713e81bSScott Long 			pUdmaParams->readWrite = MV_UDMA_TYPE_WRITE;
17741713e81bSScott Long 
17751713e81bSScott Long 		if (pCmd->pSgTable && pCmd->cf_physical_sg) {
17761713e81bSScott Long 			FPSCAT_GATH sg1=tmpSg, sg2=pCmd->pSgTable;
1777d2bd3ab9SScott Long 			do { *sg1++=*sg2; } while ((sg2++->wSgFlag & SG_FLAG_EOT)==0);
1778d2bd3ab9SScott Long 		}
1779d2bd3ab9SScott Long 		else {
1780d2bd3ab9SScott Long 			if (!pCmd->pfnBuildSgl || !pCmd->pfnBuildSgl(_VBUS_P pCmd, tmpSg, 0)) {
17811713e81bSScott Long pio:
17821713e81bSScott Long 				mvSataDisableChannelDma(pMvSataAdapter, channel);
1783d2bd3ab9SScott Long 				mvSataFlushDmaQueue(pMvSataAdapter, channel, MV_FLUSH_TYPE_CALLBACK);
17841713e81bSScott Long 
17851713e81bSScott Long 				if (pCmd->pSgTable && pCmd->cf_physical_sg==0) {
17861713e81bSScott Long 					FPSCAT_GATH sg1=tmpSg, sg2=pCmd->pSgTable;
1787d2bd3ab9SScott Long 					do { *sg1++=*sg2; } while ((sg2++->wSgFlag & SG_FLAG_EOT)==0);
1788d2bd3ab9SScott Long 				}
1789d2bd3ab9SScott Long 				else {
1790d2bd3ab9SScott Long 					if (!pCmd->pfnBuildSgl || !pCmd->pfnBuildSgl(_VBUS_P pCmd, tmpSg, 1)) {
17911713e81bSScott Long 						pCmd->Result = RETURN_NEED_LOGICAL_SG;
17921713e81bSScott Long 						goto finish_cmd;
17931713e81bSScott Long 					}
1794d2bd3ab9SScott Long 				}
17951713e81bSScott Long 
17961713e81bSScott Long 				do {
1797d2bd3ab9SScott Long 					ULONG size = tmpSg->wSgSize? tmpSg->wSgSize : 0x10000;
17981713e81bSScott Long 					ULONG_PTR addr = tmpSg->dSgAddress;
17991713e81bSScott Long 					if (size & 0x1ff) {
18001713e81bSScott Long 						pCmd->Result = RETURN_INVALID_REQUEST;
18011713e81bSScott Long 						goto finish_cmd;
18021713e81bSScott Long 					}
1803d2bd3ab9SScott Long 					if (mvStorageDevATAExecuteNonUDMACommand(pMvSataAdapter, channel,
1804d2bd3ab9SScott Long 						(pCmd->cf_data_out)?MV_NON_UDMA_PROTOCOL_PIO_DATA_OUT:MV_NON_UDMA_PROTOCOL_PIO_DATA_IN,
1805d2bd3ab9SScott Long 						is48bit,
1806d2bd3ab9SScott Long 						(MV_U16_PTR)addr,
18071713e81bSScott Long 						size >> 1,	/* count       */
18081713e81bSScott Long 						0,		/* features  N/A  */
18091713e81bSScott Long 						(MV_U16)(size>>9),	/*sector count*/
1810d2bd3ab9SScott Long 						(MV_U16)(  (is48bit? (MV_U16)((Lba >> 16) & 0xFF00) : 0 )  | (UCHAR)(Lba & 0xFF) ), /*lbalow*/
18111713e81bSScott Long 						(MV_U16)((Lba >> 8) & 0xFF), /* lbaMid      */
1812d2bd3ab9SScott Long 						(MV_U16)((Lba >> 16) & 0xFF),/* lbaHigh     */
1813d2bd3ab9SScott Long 						(MV_U8)(0x40 | (is48bit ? 0 : (UCHAR)(Lba >> 24) & 0xFF )),/* device      */
1814d2bd3ab9SScott Long 						(MV_U8)(is48bit ? (pCmd->cf_data_in?IDE_COMMAND_READ_EXT:IDE_COMMAND_WRITE_EXT):pCmd->uCmd.Ide.Command)
1815d2bd3ab9SScott Long 					)==MV_FALSE)
1816d2bd3ab9SScott Long 					{
18171713e81bSScott Long 						pCmd->Result = RETURN_IDE_ERROR;
18181713e81bSScott Long 						goto finish_cmd;
18191713e81bSScott Long 					}
18201713e81bSScott Long 					Lba += size>>9;
18211713e81bSScott Long 					if(Lba & 0xF0000000) is48bit = MV_TRUE;
18221713e81bSScott Long 				}
18231713e81bSScott Long 				while ((tmpSg++->wSgFlag & SG_FLAG_EOT)==0);
18241713e81bSScott Long 				pCmd->Result = RETURN_SUCCESS;
18251713e81bSScott Long finish_cmd:
18261713e81bSScott Long 				mvSataEnableChannelDma(pMvSataAdapter,channel);
1827d2bd3ab9SScott Long 				CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
18281713e81bSScott Long 				return;
18291713e81bSScott Long 			}
1830d2bd3ab9SScott Long 		}
18311713e81bSScott Long 
1832d2bd3ab9SScott Long 		pPRDTable = (MV_SATA_EDMA_PRD_ENTRY *) AllocatePRDTable(pMvSataAdapter->IALData);
18337d9aed9cSScott Long 		KdPrint(("pPRDTable:%p\n",pPRDTable));
18341713e81bSScott Long 		if (!pPRDTable) {
18351713e81bSScott Long 			pCmd->Result = RETURN_DEVICE_BUSY;
1836d2bd3ab9SScott Long 			CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
18371713e81bSScott Long 			HPT_ASSERT(0);
18381713e81bSScott Long 			return;
18391713e81bSScott Long 		}
18401713e81bSScott Long 
18411713e81bSScott Long 		do{
1842d2bd3ab9SScott Long 			pPRDTable[i].highBaseAddr = (sizeof(tmpSg->dSgAddress)>4 ? (MV_U32)(tmpSg->dSgAddress>>32) : 0);
18431713e81bSScott Long 			pPRDTable[i].flags = (MV_U16)tmpSg->wSgFlag;
18441713e81bSScott Long 			pPRDTable[i].byteCount = (MV_U16)tmpSg->wSgSize;
18451713e81bSScott Long 			pPRDTable[i].lowBaseAddr = (MV_U32)tmpSg->dSgAddress;
18461713e81bSScott Long 			pPRDTable[i].reserved = 0;
18471713e81bSScott Long 			i++;
18481713e81bSScott Long 		}while((tmpSg++->wSgFlag & SG_FLAG_EOT)==0);
18491713e81bSScott Long 
1850d2bd3ab9SScott Long 		pUdmaParams->prdLowAddr = (ULONG)fOsPhysicalAddress(pPRDTable);
1851d2bd3ab9SScott Long 		if ((pUdmaParams->numOfSectors == 256) && (pMvSataChannel->lba48Address == MV_FALSE)) {
18521713e81bSScott Long 			pUdmaParams->numOfSectors = 0;
18531713e81bSScott Long 		}
18541713e81bSScott Long 
18551713e81bSScott Long 		pCmd->uScratch.sata_param.prdAddr = (PVOID)pPRDTable;
18561713e81bSScott Long 
1857d2bd3ab9SScott Long 		result = mvSataQueueCommand(pMvSataAdapter, channel, &commandInfo);
18581713e81bSScott Long 
1859d2bd3ab9SScott Long 		if (result != MV_QUEUE_COMMAND_RESULT_OK)
1860d2bd3ab9SScott Long 		{
18611713e81bSScott Long queue_failed:
1862d2bd3ab9SScott Long 			switch (result)
1863d2bd3ab9SScott Long 			{
18641713e81bSScott Long 			case MV_QUEUE_COMMAND_RESULT_BAD_LBA_ADDRESS:
1865d2bd3ab9SScott Long 				MV_ERROR("IAL Error: Edma Queue command failed. Bad LBA "
1866d2bd3ab9SScott Long 						 "LBA[31:0](0x%08x)\n", pUdmaParams->lowLBAAddress);
18671713e81bSScott Long 				pCmd->Result = RETURN_IDE_ERROR;
18681713e81bSScott Long 				break;
18691713e81bSScott Long 			case MV_QUEUE_COMMAND_RESULT_QUEUED_MODE_DISABLED:
1870d2bd3ab9SScott Long 				MV_ERROR("IAL Error: Edma Queue command failed. EDMA"
1871d2bd3ab9SScott Long 						 " disabled adapter %d channel %d\n",
18721713e81bSScott Long 						 pMvSataAdapter->adapterId, channel);
18731713e81bSScott Long 				mvSataEnableChannelDma(pMvSataAdapter,channel);
18741713e81bSScott Long 				pCmd->Result = RETURN_IDE_ERROR;
18751713e81bSScott Long 				break;
18761713e81bSScott Long 			case MV_QUEUE_COMMAND_RESULT_FULL:
1877d2bd3ab9SScott Long 				MV_ERROR("IAL Error: Edma Queue command failed. Queue is"
1878d2bd3ab9SScott Long 						 " Full adapter %d channel %d\n",
18791713e81bSScott Long 						 pMvSataAdapter->adapterId, channel);
18801713e81bSScott Long 				pCmd->Result = RETURN_DEVICE_BUSY;
18811713e81bSScott Long 				break;
18821713e81bSScott Long 			case MV_QUEUE_COMMAND_RESULT_BAD_PARAMS:
1883d2bd3ab9SScott Long 				MV_ERROR("IAL Error: Edma Queue command failed. (Bad "
1884d2bd3ab9SScott Long 						 "Params), pMvSataAdapter: %p,  pSataChannel: %p.\n",
1885d2bd3ab9SScott Long 						 pMvSataAdapter, pMvSataAdapter->sataChannel[channel]);
18861713e81bSScott Long 				pCmd->Result = RETURN_IDE_ERROR;
18871713e81bSScott Long 				break;
18881713e81bSScott Long 			default:
1889d2bd3ab9SScott Long 				MV_ERROR("IAL Error: Bad result value (%d) from queue"
1890d2bd3ab9SScott Long 						 " command\n", result);
18911713e81bSScott Long 				pCmd->Result = RETURN_IDE_ERROR;
18921713e81bSScott Long 			}
18931713e81bSScott Long 			if(pPRDTable)
1894d2bd3ab9SScott Long 				FreePRDTable(pMvSataAdapter->IALData,pPRDTable);
1895d2bd3ab9SScott Long 			CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
18961713e81bSScott Long 		}
18971713e81bSScott Long 		pDevice->QueueLength++;
18981713e81bSScott Long 		return;
18991713e81bSScott Long 
19001713e81bSScott Long 	case IDE_COMMAND_VERIFY:
19011713e81bSScott Long 		commandInfo.type = MV_QUEUED_COMMAND_TYPE_NONE_UDMA;
19021713e81bSScott Long 		pNoUdmaParams->bufPtr = NULL;
19031713e81bSScott Long 		pNoUdmaParams->callBack = CommandCompletionCB;
19041713e81bSScott Long 		pNoUdmaParams->commandId = (MV_VOID_PTR)pCmd;
19051713e81bSScott Long 		pNoUdmaParams->count = 0;
19061713e81bSScott Long 		pNoUdmaParams->features = 0;
19071713e81bSScott Long 		pNoUdmaParams->protocolType = MV_NON_UDMA_PROTOCOL_NON_DATA;
19081713e81bSScott Long 
19091713e81bSScott Long 		pCmd->uScratch.sata_param.cmd_priv = 1;
19101713e81bSScott Long 		if (pMvSataChannel->lba48Address == MV_TRUE){
1911d2bd3ab9SScott Long 			pNoUdmaParams->command = MV_ATA_COMMAND_READ_VERIFY_SECTORS_EXT;
19121713e81bSScott Long 			pNoUdmaParams->isEXT = MV_TRUE;
1913d2bd3ab9SScott Long 			pNoUdmaParams->lbaHigh = (MV_U16)((Lba & 0xff0000) >> 16);
19141713e81bSScott Long 			pNoUdmaParams->lbaMid = (MV_U16)((Lba & 0xff00) >> 8);
19151713e81bSScott Long 			pNoUdmaParams->lbaLow =
19161713e81bSScott Long 				(MV_U16)(((Lba & 0xff000000) >> 16)| (Lba & 0xff));
19171713e81bSScott Long 			pNoUdmaParams->sectorCount = nSector;
19181713e81bSScott Long 			pNoUdmaParams->device = 0x40;
1919d2bd3ab9SScott Long 			result = mvSataQueueCommand(pMvSataAdapter, channel, &commandInfo);
19201713e81bSScott Long 			if (result != MV_QUEUE_COMMAND_RESULT_OK){
19211713e81bSScott Long 				goto queue_failed;
19221713e81bSScott Long 			}
19231713e81bSScott Long 			return;
19241713e81bSScott Long 		}
1925d2bd3ab9SScott Long 		else{
19261713e81bSScott Long 			pNoUdmaParams->command = MV_ATA_COMMAND_READ_VERIFY_SECTORS;
19271713e81bSScott Long 			pNoUdmaParams->isEXT = MV_FALSE;
19281713e81bSScott Long 			pNoUdmaParams->lbaHigh = (MV_U16)((Lba & 0xff0000) >> 16);
19291713e81bSScott Long 			pNoUdmaParams->lbaMid = (MV_U16)((Lba & 0xff00) >> 8);
19301713e81bSScott Long 			pNoUdmaParams->lbaLow = (MV_U16)(Lba & 0xff);
19311713e81bSScott Long 			pNoUdmaParams->sectorCount = 0xff & nSector;
19321713e81bSScott Long 			pNoUdmaParams->device = (MV_U8)(0x40 |
19331713e81bSScott Long 				((Lba & 0xf000000) >> 24));
19341713e81bSScott Long 			pNoUdmaParams->callBack = CommandCompletionCB;
1935d2bd3ab9SScott Long 			result = mvSataQueueCommand(pMvSataAdapter, channel, &commandInfo);
1936d2bd3ab9SScott Long 			/*FIXME: how about the commands already queued? but marvel also forgets to consider this*/
19371713e81bSScott Long 			if (result != MV_QUEUE_COMMAND_RESULT_OK){
19381713e81bSScott Long 				goto queue_failed;
19391713e81bSScott Long 			}
1940d2bd3ab9SScott Long 		}
19411713e81bSScott Long 		break;
19421713e81bSScott Long 	default:
19431713e81bSScott Long 		pCmd->Result = RETURN_INVALID_REQUEST;
19441713e81bSScott Long 		CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
19451713e81bSScott Long 		break;
19461713e81bSScott Long 	}
19471713e81bSScott Long }
19481713e81bSScott Long 
19491713e81bSScott Long /**********************************************************
19501713e81bSScott Long  *
19511713e81bSScott Long  *	Probe the hostadapter.
19521713e81bSScott Long  *
19531713e81bSScott Long  **********************************************************/
19541713e81bSScott Long static int
19551713e81bSScott Long hpt_probe(device_t dev)
19561713e81bSScott Long {
19571713e81bSScott Long 	if ((pci_get_vendor(dev) == MV_SATA_VENDOR_ID) &&
19581713e81bSScott Long 		(pci_get_device(dev) == MV_SATA_DEVICE_ID_5081
19591713e81bSScott Long #ifdef FOR_DEMO
19601713e81bSScott Long 		|| pci_get_device(dev) == MV_SATA_DEVICE_ID_5080
19611713e81bSScott Long #endif
1962d2bd3ab9SScott Long 		))
1963d2bd3ab9SScott Long 	{
19641713e81bSScott Long 		KdPrintI((CONTROLLER_NAME " found\n"));
19651713e81bSScott Long 		device_set_desc(dev, CONTROLLER_NAME);
19667634a04bSXin LI 		return (BUS_PROBE_DEFAULT);
19671713e81bSScott Long 	}
19681713e81bSScott Long 	else
19691713e81bSScott Long 		return(ENXIO);
19701713e81bSScott Long }
19711713e81bSScott Long 
19721713e81bSScott Long /***********************************************************
19731713e81bSScott Long  *
19741713e81bSScott Long  *      Auto configuration:  attach and init a host adapter.
19751713e81bSScott Long  *
19761713e81bSScott Long  ***********************************************************/
19771713e81bSScott Long static int
19781713e81bSScott Long hpt_attach(device_t dev)
19791713e81bSScott Long {
1980d2bd3ab9SScott Long 	IAL_ADAPTER_T * pAdapter = device_get_softc(dev);
19811713e81bSScott Long 	int rid;
19821713e81bSScott Long 	union ccb *ccb;
19831713e81bSScott Long 	struct cam_devq *devq;
19841713e81bSScott Long 	struct cam_sim *hpt_vsim;
19851713e81bSScott Long 
198649b3fc40SJohn Baldwin 	device_printf(dev, "%s Version %s \n", DRIVER_NAME, DRIVER_VERSION);
1987d2bd3ab9SScott Long 
19881713e81bSScott Long 	pAdapter->hpt_dev = dev;
19891713e81bSScott Long 
19901713e81bSScott Long 	rid = init_adapter(pAdapter);
19911713e81bSScott Long 	if (rid)
19921713e81bSScott Long 		return rid;
19931713e81bSScott Long 
19941713e81bSScott Long 	rid = 0;
199543cd6160SJustin Hibbits 	if ((pAdapter->hpt_irq = bus_alloc_resource_any(pAdapter->hpt_dev, SYS_RES_IRQ, &rid, RF_SHAREABLE | RF_ACTIVE)) == NULL)
1996d2bd3ab9SScott Long 	{
19971713e81bSScott Long 		hpt_printk(("can't allocate interrupt\n"));
19981713e81bSScott Long 		return(ENXIO);
19991713e81bSScott Long 	}
20001713e81bSScott Long 
200149b3fc40SJohn Baldwin 	if (bus_setup_intr(pAdapter->hpt_dev, pAdapter->hpt_irq,
200249b3fc40SJohn Baldwin 				INTR_TYPE_CAM | INTR_MPSAFE,
200364470755SXin LI 				NULL, hpt_intr, pAdapter, &pAdapter->hpt_intr))
2004d2bd3ab9SScott Long 	{
20051713e81bSScott Long 		hpt_printk(("can't set up interrupt\n"));
20061713e81bSScott Long 		free(pAdapter, M_DEVBUF);
20071713e81bSScott Long 		return(ENXIO);
20081713e81bSScott Long 	}
20091713e81bSScott Long 
2010d2bd3ab9SScott Long 
2011*2ef735f4SEdward Tomasz Napierala 	if ((ccb = xpt_alloc_ccb()) != NULL)
2012d2bd3ab9SScott Long 	{
20131713e81bSScott Long 		ccb->ccb_h.pinfo.priority = 1;
20141713e81bSScott Long 		ccb->ccb_h.pinfo.index = CAM_UNQUEUED_INDEX;
2015d2bd3ab9SScott Long 	}
2016d2bd3ab9SScott Long 	else
2017d2bd3ab9SScott Long 	{
20181713e81bSScott Long 		return ENOMEM;
20191713e81bSScott Long 	}
20201713e81bSScott Long 	/*
20211713e81bSScott Long 	 * Create the device queue for our SIM(s).
20221713e81bSScott Long 	 */
2023d2bd3ab9SScott Long 	if((devq = cam_simq_alloc(8/*MAX_QUEUE_COMM*/)) == NULL)
2024d2bd3ab9SScott Long 	{
20251713e81bSScott Long 		KdPrint(("ENXIO\n"));
20261713e81bSScott Long 		return ENOMEM;
20271713e81bSScott Long 	}
20281713e81bSScott Long 
20291713e81bSScott Long 	/*
20301713e81bSScott Long 	 * Construct our SIM entry
20311713e81bSScott Long 	 */
203264470755SXin LI 	hpt_vsim = cam_sim_alloc(hpt_action, hpt_poll, __str(PROC_DIR_NAME),
203349b3fc40SJohn Baldwin 			pAdapter, device_get_unit(pAdapter->hpt_dev),
203449b3fc40SJohn Baldwin 			&pAdapter->lock, 1, 8, devq);
203564470755SXin LI 	if (hpt_vsim == NULL) {
20361713e81bSScott Long 		cam_simq_free(devq);
20371713e81bSScott Long 		return ENOMEM;
20381713e81bSScott Long 	}
20391713e81bSScott Long 
204049b3fc40SJohn Baldwin 	mtx_lock(&pAdapter->lock);
2041b50569b7SScott Long 	if (xpt_bus_register(hpt_vsim, dev, 0) != CAM_SUCCESS)
2042d2bd3ab9SScott Long 	{
20431713e81bSScott Long 		cam_sim_free(hpt_vsim, /*free devq*/ TRUE);
204449b3fc40SJohn Baldwin 		mtx_unlock(&pAdapter->lock);
20451713e81bSScott Long 		hpt_vsim = NULL;
20461713e81bSScott Long 		return ENXIO;
20471713e81bSScott Long 	}
20481713e81bSScott Long 
20491713e81bSScott Long 	if(xpt_create_path(&pAdapter->path, /*periph */ NULL,
2050d2bd3ab9SScott Long 			cam_sim_path(hpt_vsim), CAM_TARGET_WILDCARD,
2051d2bd3ab9SScott Long 			CAM_LUN_WILDCARD) != CAM_REQ_CMP)
2052d2bd3ab9SScott Long 	{
20531713e81bSScott Long 		xpt_bus_deregister(cam_sim_path(hpt_vsim));
20541713e81bSScott Long 		cam_sim_free(hpt_vsim, /*free_devq*/TRUE);
205549b3fc40SJohn Baldwin 		mtx_unlock(&pAdapter->lock);
20561713e81bSScott Long 		hpt_vsim = NULL;
20571713e81bSScott Long 		return ENXIO;
20581713e81bSScott Long 	}
205949b3fc40SJohn Baldwin 	mtx_unlock(&pAdapter->lock);
20601713e81bSScott Long 
20611713e81bSScott Long 	xpt_setup_ccb(&(ccb->ccb_h), pAdapter->path, /*priority*/5);
20621713e81bSScott Long 	ccb->ccb_h.func_code = XPT_SASYNC_CB;
20631713e81bSScott Long 	ccb->csa.event_enable = AC_LOST_DEVICE;
20641713e81bSScott Long 	ccb->csa.callback = hpt_async;
20651713e81bSScott Long 	ccb->csa.callback_arg = hpt_vsim;
20661713e81bSScott Long 	xpt_action((union ccb *)ccb);
2067*2ef735f4SEdward Tomasz Napierala 	xpt_free_ccb(ccb);
20681713e81bSScott Long 
2069cd3ef666SXin LI 	if (device_get_unit(dev) == 0) {
20708eadd1b2SXin LI 		/* Start the work thread.  XXX */
20716e0e5d36SNate Lawson 		launch_worker_thread();
20726e0e5d36SNate Lawson 	}
20731713e81bSScott Long 
20741713e81bSScott Long 	return 0;
20751713e81bSScott Long }
20761713e81bSScott Long 
20771713e81bSScott Long static int
20781713e81bSScott Long hpt_detach(device_t dev)
20791713e81bSScott Long {
20801713e81bSScott Long 	return (EBUSY);
20811713e81bSScott Long }
20821713e81bSScott Long 
2083d2bd3ab9SScott Long 
20841713e81bSScott Long /***************************************************************
20851713e81bSScott Long  * The poll function is used to simulate the interrupt when
20861713e81bSScott Long  * the interrupt subsystem is not functioning.
20871713e81bSScott Long  *
20881713e81bSScott Long  ***************************************************************/
20891713e81bSScott Long static void
20901713e81bSScott Long hpt_poll(struct cam_sim *sim)
20911713e81bSScott Long {
209249b3fc40SJohn Baldwin 	IAL_ADAPTER_T *pAdapter;
209349b3fc40SJohn Baldwin 
209449b3fc40SJohn Baldwin 	pAdapter = cam_sim_softc(sim);
209549b3fc40SJohn Baldwin 
209649b3fc40SJohn Baldwin 	hpt_intr_locked((void *)cam_sim_softc(sim));
20971713e81bSScott Long }
20981713e81bSScott Long 
20991713e81bSScott Long /****************************************************************
21001713e81bSScott Long  *	Name:	hpt_intr
21011713e81bSScott Long  *	Description:	Interrupt handler.
21021713e81bSScott Long  ****************************************************************/
21031713e81bSScott Long static void
21041713e81bSScott Long hpt_intr(void *arg)
21051713e81bSScott Long {
210649b3fc40SJohn Baldwin 	IAL_ADAPTER_T *pAdapter;
21071713e81bSScott Long 
210849b3fc40SJohn Baldwin 	pAdapter = arg;
210949b3fc40SJohn Baldwin 	mtx_lock(&pAdapter->lock);
211049b3fc40SJohn Baldwin 	hpt_intr_locked(pAdapter);
211149b3fc40SJohn Baldwin 	mtx_unlock(&pAdapter->lock);
211249b3fc40SJohn Baldwin }
211349b3fc40SJohn Baldwin 
211449b3fc40SJohn Baldwin static void
211549b3fc40SJohn Baldwin hpt_intr_locked(IAL_ADAPTER_T *pAdapter)
211649b3fc40SJohn Baldwin {
211749b3fc40SJohn Baldwin 
211849b3fc40SJohn Baldwin 	mtx_assert(&pAdapter->lock, MA_OWNED);
21191713e81bSScott Long 	/* KdPrintI(("----- Entering Isr() -----\n")); */
2120d2bd3ab9SScott Long 	if (mvSataInterruptServiceRoutine(&pAdapter->mvSataAdapter) == MV_TRUE)
2121d2bd3ab9SScott Long 	{
21221713e81bSScott Long 		_VBUS_INST(&pAdapter->VBus)
21231713e81bSScott Long 		CheckPendingCall(_VBUS_P0);
21241713e81bSScott Long 	}
21251713e81bSScott Long 
21261713e81bSScott Long 	/* KdPrintI(("----- Leaving Isr() -----\n")); */
21271713e81bSScott Long }
21281713e81bSScott Long 
21291713e81bSScott Long /**********************************************************
21301713e81bSScott Long  * 			Asynchronous Events
21311713e81bSScott Long  *********************************************************/
21321713e81bSScott Long #if (!defined(UNREFERENCED_PARAMETER))
21331713e81bSScott Long #define UNREFERENCED_PARAMETER(x) (void)(x)
21341713e81bSScott Long #endif
21351713e81bSScott Long 
21361713e81bSScott Long static void
21371713e81bSScott Long hpt_async(void * callback_arg, u_int32_t code, struct cam_path * path,
21381713e81bSScott Long     void * arg)
21391713e81bSScott Long {
21401713e81bSScott Long 	/* debug XXXX */
21411713e81bSScott Long 	panic("Here");
21421713e81bSScott Long 	UNREFERENCED_PARAMETER(callback_arg);
21431713e81bSScott Long 	UNREFERENCED_PARAMETER(code);
21441713e81bSScott Long 	UNREFERENCED_PARAMETER(path);
21451713e81bSScott Long 	UNREFERENCED_PARAMETER(arg);
21461713e81bSScott Long 
21471713e81bSScott Long }
21481713e81bSScott Long 
21491713e81bSScott Long static void
21501713e81bSScott Long FlushAdapter(IAL_ADAPTER_T *pAdapter)
21511713e81bSScott Long {
21521713e81bSScott Long 	int i;
21531713e81bSScott Long 
21541713e81bSScott Long 	hpt_printk(("flush all devices\n"));
21551713e81bSScott Long 
21561713e81bSScott Long 	/* flush all devices */
21571713e81bSScott Long 	for (i=0; i<MAX_VDEVICE_PER_VBUS; i++) {
21581713e81bSScott Long 		PVDevice pVDev = pAdapter->VBus.pVDevice[i];
2159d2bd3ab9SScott Long 		if(pVDev) fFlushVDev(pVDev);
21601713e81bSScott Long 	}
21611713e81bSScott Long }
21621713e81bSScott Long 
21631713e81bSScott Long static int
21641713e81bSScott Long hpt_shutdown(device_t dev)
21651713e81bSScott Long {
21661713e81bSScott Long 		IAL_ADAPTER_T *pAdapter;
21671713e81bSScott Long 
21681713e81bSScott Long 		pAdapter = device_get_softc(dev);
21691713e81bSScott Long 
21701713e81bSScott Long 		EVENTHANDLER_DEREGISTER(shutdown_final, pAdapter->eh);
217149b3fc40SJohn Baldwin 		mtx_lock(&pAdapter->lock);
21721713e81bSScott Long 		FlushAdapter(pAdapter);
217349b3fc40SJohn Baldwin 		mtx_unlock(&pAdapter->lock);
2174d2bd3ab9SScott Long 		  /* give the flush some time to happen,
2175d2bd3ab9SScott Long 		    *otherwise "shutdown -p now" will make file system corrupted */
2176d2bd3ab9SScott Long 		DELAY(1000 * 1000 * 5);
21771713e81bSScott Long 		return 0;
21781713e81bSScott Long }
21791713e81bSScott Long 
21801713e81bSScott Long void
21811713e81bSScott Long Check_Idle_Call(IAL_ADAPTER_T *pAdapter)
21821713e81bSScott Long {
21831713e81bSScott Long 	_VBUS_INST(&pAdapter->VBus)
21841713e81bSScott Long 
21851713e81bSScott Long 	if (mWaitingForIdle(_VBUS_P0)) {
21861713e81bSScott Long 		CheckIdleCall(_VBUS_P0);
21871713e81bSScott Long #ifdef SUPPORT_ARRAY
2188d2bd3ab9SScott Long 		{
2189d2bd3ab9SScott Long 			int i;
21901713e81bSScott Long 			PVDevice pArray;
2191d2bd3ab9SScott Long 			for(i = 0; i < MAX_ARRAY_PER_VBUS; i++){
21921713e81bSScott Long 				if ((pArray=ArrayTables(i))->u.array.dArStamp==0)
21931713e81bSScott Long 					continue;
21941713e81bSScott Long 				else if (pArray->u.array.rf_auto_rebuild) {
21951713e81bSScott Long 						KdPrint(("auto rebuild.\n"));
21961713e81bSScott Long 						pArray->u.array.rf_auto_rebuild = 0;
2197d2bd3ab9SScott Long 						hpt_queue_dpc((HPT_DPC)hpt_rebuild_data_block, pAdapter, pArray, DUPLICATE);
2198d2bd3ab9SScott Long 				}
21991713e81bSScott Long 			}
22001713e81bSScott Long 		}
22011713e81bSScott Long #endif
22021713e81bSScott Long 	}
22031713e81bSScott Long 	/* launch the awaiting commands blocked by mWaitingForIdle */
2204d2bd3ab9SScott Long 	while(pAdapter->pending_Q!= NULL)
2205d2bd3ab9SScott Long 	{
22061713e81bSScott Long 		_VBUS_INST(&pAdapter->VBus)
2207d2bd3ab9SScott Long 		union ccb *ccb = (union ccb *)pAdapter->pending_Q->ccb_h.ccb_ccb_ptr;
22081713e81bSScott Long 		hpt_free_ccb(&pAdapter->pending_Q, ccb);
22091713e81bSScott Long 		CallAfterReturn(_VBUS_P (DPC_PROC)OsSendCommand, ccb);
22101713e81bSScott Long 	}
22111713e81bSScott Long }
22121713e81bSScott Long 
22131713e81bSScott Long static void
22141713e81bSScott Long ccb_done(union ccb *ccb)
22151713e81bSScott Long {
2216d2bd3ab9SScott Long 	PBUS_DMAMAP pmap = (PBUS_DMAMAP)ccb->ccb_adapter;
2217d2bd3ab9SScott Long 	IAL_ADAPTER_T * pAdapter = pmap->pAdapter;
2218d2bd3ab9SScott Long 	KdPrintI(("ccb_done: ccb %p status %x\n", ccb, ccb->ccb_h.status));
22191713e81bSScott Long 
2220d2bd3ab9SScott Long 	dmamap_put(pmap);
22211713e81bSScott Long 	xpt_done(ccb);
22221713e81bSScott Long 
22231713e81bSScott Long 	pAdapter->outstandingCommands--;
22241713e81bSScott Long 
2225d2bd3ab9SScott Long 	if (pAdapter->outstandingCommands == 0)
2226d2bd3ab9SScott Long 	{
22271713e81bSScott Long 		if(DPC_Request_Nums == 0)
22281713e81bSScott Long 			Check_Idle_Call(pAdapter);
222949b3fc40SJohn Baldwin 		wakeup(pAdapter);
22301713e81bSScott Long 	}
22311713e81bSScott Long }
22321713e81bSScott Long 
22331713e81bSScott Long /****************************************************************
22341713e81bSScott Long  *	Name:	hpt_action
22351713e81bSScott Long  *	Description:	Process a queued command from the CAM layer.
22361713e81bSScott Long  *	Parameters:		sim - Pointer to SIM object
22371713e81bSScott Long  *					ccb - Pointer to SCSI command structure.
22381713e81bSScott Long  ****************************************************************/
22391713e81bSScott Long 
22401713e81bSScott Long void
22411713e81bSScott Long hpt_action(struct cam_sim *sim, union ccb *ccb)
22421713e81bSScott Long {
22431713e81bSScott Long 	IAL_ADAPTER_T * pAdapter = (IAL_ADAPTER_T *) cam_sim_softc(sim);
2244d2bd3ab9SScott Long 	PBUS_DMAMAP  pmap;
22451713e81bSScott Long 	_VBUS_INST(&pAdapter->VBus)
22461713e81bSScott Long 
224749b3fc40SJohn Baldwin 	mtx_assert(&pAdapter->lock, MA_OWNED);
2248e1ab829aSScott Long 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("hpt_action\n"));
2249d2bd3ab9SScott Long 	KdPrint(("hpt_action(%lx,%lx{%x})\n", (u_long)sim, (u_long)ccb, ccb->ccb_h.func_code));
22501713e81bSScott Long 
2251d2bd3ab9SScott Long 	switch (ccb->ccb_h.func_code)
2252d2bd3ab9SScott Long 	{
22531713e81bSScott Long 		case XPT_SCSI_IO:	/* Execute the requested I/O operation */
2254d2bd3ab9SScott Long 		{
22551713e81bSScott Long 			/* ccb->ccb_h.path_id is not our bus id - don't check it */
22561713e81bSScott Long 
22571713e81bSScott Long 			if (ccb->ccb_h.target_lun)	{
22581713e81bSScott Long 				ccb->ccb_h.status = CAM_LUN_INVALID;
22591713e81bSScott Long 				xpt_done(ccb);
22601713e81bSScott Long 				return;
22611713e81bSScott Long 			}
22621713e81bSScott Long 			if (ccb->ccb_h.target_id >= MAX_VDEVICE_PER_VBUS ||
22631713e81bSScott Long 				pAdapter->VBus.pVDevice[ccb->ccb_h.target_id]==0) {
22641713e81bSScott Long 				ccb->ccb_h.status = CAM_TID_INVALID;
22651713e81bSScott Long 				xpt_done(ccb);
22661713e81bSScott Long 				return;
22671713e81bSScott Long 			}
22681713e81bSScott Long 
22691713e81bSScott Long 			if (pAdapter->outstandingCommands==0 && DPC_Request_Nums==0)
22701713e81bSScott Long 				Check_Idle_Call(pAdapter);
22711713e81bSScott Long 
2272d2bd3ab9SScott Long 			pmap = dmamap_get(pAdapter);
2273d2bd3ab9SScott Long 			HPT_ASSERT(pmap);
2274d2bd3ab9SScott Long 			ccb->ccb_adapter = pmap;
2275d2bd3ab9SScott Long 			memset((void *)pmap->psg, 0,  sizeof(pmap->psg));
2276d2bd3ab9SScott Long 
22771713e81bSScott Long 			if (mWaitingForIdle(_VBUS_P0))
22781713e81bSScott Long 				hpt_queue_ccb(&pAdapter->pending_Q, ccb);
22791713e81bSScott Long 			else
22801713e81bSScott Long 				OsSendCommand(_VBUS_P ccb);
22811713e81bSScott Long 
22821713e81bSScott Long 			/* KdPrint(("leave scsiio\n")); */
22831713e81bSScott Long 			break;
2284d2bd3ab9SScott Long 		}
22851713e81bSScott Long 
22861713e81bSScott Long 		case XPT_RESET_BUS:
22871713e81bSScott Long 			KdPrint(("reset bus\n"));
22881713e81bSScott Long 			fResetVBus(_VBUS_P0);
22891713e81bSScott Long 			xpt_done(ccb);
22901713e81bSScott Long 			break;
22911713e81bSScott Long 
22921713e81bSScott Long 		case XPT_RESET_DEV:	/* Bus Device Reset the specified SCSI device */
22931713e81bSScott Long 		case XPT_ABORT:			/* Abort the specified CCB */
22941713e81bSScott Long 		case XPT_TERM_IO:		/* Terminate the I/O process */
22951713e81bSScott Long 			/* XXX Implement */
22961713e81bSScott Long 			ccb->ccb_h.status = CAM_REQ_INVALID;
22971713e81bSScott Long 			xpt_done(ccb);
22981713e81bSScott Long 			break;
22991713e81bSScott Long 
23001713e81bSScott Long 		case XPT_GET_TRAN_SETTINGS:
23011713e81bSScott Long 		case XPT_SET_TRAN_SETTINGS:
23021713e81bSScott Long 			/* XXX Implement */
23031713e81bSScott Long 			ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
23041713e81bSScott Long 			xpt_done(ccb);
23051713e81bSScott Long 			break;
23061713e81bSScott Long 
23071713e81bSScott Long 		case XPT_CALC_GEOMETRY:
2308f3b080e6SMarius Strobl 			cam_calc_geometry(&ccb->ccg, 1);
23091713e81bSScott Long 			xpt_done(ccb);
23101713e81bSScott Long 			break;
23111713e81bSScott Long 
23121713e81bSScott Long 		case XPT_PATH_INQ:		/* Path routing inquiry */
23131713e81bSScott Long 		{
23141713e81bSScott Long 			struct ccb_pathinq *cpi = &ccb->cpi;
23151713e81bSScott Long 
23161713e81bSScott Long 			cpi->version_num = 1; /* XXX??? */
23171713e81bSScott Long 			cpi->hba_inquiry = PI_SDTR_ABLE;
23181713e81bSScott Long 			cpi->target_sprt = 0;
23191713e81bSScott Long 			/* Not necessary to reset bus */
23201713e81bSScott Long 			cpi->hba_misc = PIM_NOBUSRESET;
23211713e81bSScott Long 			cpi->hba_eng_cnt = 0;
23221713e81bSScott Long 
23231713e81bSScott Long 			cpi->max_target = MAX_VDEVICE_PER_VBUS;
23241713e81bSScott Long 			cpi->max_lun = 0;
23251713e81bSScott Long 			cpi->initiator_id = MAX_VDEVICE_PER_VBUS;
23261713e81bSScott Long 
23271713e81bSScott Long 			cpi->bus_id = cam_sim_bus(sim);
23281713e81bSScott Long 			cpi->base_transfer_speed = 3300;
23294195c7deSAlan Somers 			strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
23304195c7deSAlan Somers 			strlcpy(cpi->hba_vid, "HPT   ", HBA_IDLEN);
23314195c7deSAlan Somers 			strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
23321713e81bSScott Long 			cpi->unit_number = cam_sim_unit(sim);
23339085ea50SAlexander Motin 			cpi->transport = XPORT_SPI;
23349085ea50SAlexander Motin 			cpi->transport_version = 2;
23359085ea50SAlexander Motin 			cpi->protocol = PROTO_SCSI;
23369085ea50SAlexander Motin 			cpi->protocol_version = SCSI_REV_2;
23371713e81bSScott Long 			cpi->ccb_h.status = CAM_REQ_CMP;
23381713e81bSScott Long 			xpt_done(ccb);
23391713e81bSScott Long 			break;
23401713e81bSScott Long 		}
23411713e81bSScott Long 
23421713e81bSScott Long 		default:
23431713e81bSScott Long 			KdPrint(("invalid cmd\n"));
23441713e81bSScott Long 			ccb->ccb_h.status = CAM_REQ_INVALID;
23451713e81bSScott Long 			xpt_done(ccb);
23461713e81bSScott Long 			break;
23471713e81bSScott Long 	}
23481713e81bSScott Long 	/* KdPrint(("leave hpt_action..............\n")); */
23491713e81bSScott Long }
23501713e81bSScott Long 
23511713e81bSScott Long /* shall be called at lock_driver() */
23521713e81bSScott Long static void
23531713e81bSScott Long hpt_queue_ccb(union ccb **ccb_Q, union ccb *ccb)
23541713e81bSScott Long {
23551713e81bSScott Long 	if(*ccb_Q == NULL)
23561713e81bSScott Long 		ccb->ccb_h.ccb_ccb_ptr = ccb;
23571713e81bSScott Long 	else {
23581713e81bSScott Long 		ccb->ccb_h.ccb_ccb_ptr = (*ccb_Q)->ccb_h.ccb_ccb_ptr;
23591713e81bSScott Long 		(*ccb_Q)->ccb_h.ccb_ccb_ptr = (char *)ccb;
23601713e81bSScott Long 	}
23611713e81bSScott Long 
23621713e81bSScott Long 	*ccb_Q = ccb;
23631713e81bSScott Long }
23641713e81bSScott Long 
23651713e81bSScott Long /* shall be called at lock_driver() */
23661713e81bSScott Long static void
23671713e81bSScott Long hpt_free_ccb(union ccb **ccb_Q, union ccb *ccb)
23681713e81bSScott Long {
23691713e81bSScott Long 	union ccb *TempCCB;
23701713e81bSScott Long 
23711713e81bSScott Long 	TempCCB = *ccb_Q;
23721713e81bSScott Long 
2373d2bd3ab9SScott Long 	if(ccb->ccb_h.ccb_ccb_ptr == ccb) /*it means SCpnt is the last one in CURRCMDs*/
23741713e81bSScott Long 		*ccb_Q = NULL;
23751713e81bSScott Long 	else {
23761713e81bSScott Long 		while(TempCCB->ccb_h.ccb_ccb_ptr != (char *)ccb)
23771713e81bSScott Long 			TempCCB = (union ccb *)TempCCB->ccb_h.ccb_ccb_ptr;
23781713e81bSScott Long 
23791713e81bSScott Long 		TempCCB->ccb_h.ccb_ccb_ptr = ccb->ccb_h.ccb_ccb_ptr;
23801713e81bSScott Long 
23811713e81bSScott Long 		if(*ccb_Q == ccb)
23821713e81bSScott Long 			*ccb_Q = TempCCB;
23831713e81bSScott Long 	}
23841713e81bSScott Long }
23851713e81bSScott Long 
23861713e81bSScott Long #ifdef SUPPORT_ARRAY
23871713e81bSScott Long /***************************************************************************
23881713e81bSScott Long  * Function:     hpt_worker_thread
23891713e81bSScott Long  * Description:  Do background rebuilding. Execute in kernel thread context.
23901713e81bSScott Long  * Returns:      None
23911713e81bSScott Long  ***************************************************************************/
23921713e81bSScott Long static void hpt_worker_thread(void)
23931713e81bSScott Long {
23941713e81bSScott Long 
23951713e81bSScott Long 	for(;;)	{
239649b3fc40SJohn Baldwin 		mtx_lock(&DpcQueue_Lock);
23971713e81bSScott Long 		while (DpcQueue_First!=DpcQueue_Last) {
23981713e81bSScott Long 			ST_HPT_DPC p;
23991713e81bSScott Long 			p = DpcQueue[DpcQueue_First];
24001713e81bSScott Long 			DpcQueue_First++;
24011713e81bSScott Long 			DpcQueue_First %= MAX_DPC;
24021713e81bSScott Long 			DPC_Request_Nums++;
240349b3fc40SJohn Baldwin 			mtx_unlock(&DpcQueue_Lock);
24041713e81bSScott Long 			p.dpc(p.pAdapter, p.arg, p.flags);
24051713e81bSScott Long 
240649b3fc40SJohn Baldwin 			mtx_lock(&p.pAdapter->lock);
240749b3fc40SJohn Baldwin 			mtx_lock(&DpcQueue_Lock);
24081713e81bSScott Long 			DPC_Request_Nums--;
2409d2bd3ab9SScott Long 			/* since we may have prevented Check_Idle_Call, do it here */
24101713e81bSScott Long 			if (DPC_Request_Nums==0) {
24111713e81bSScott Long 				if (p.pAdapter->outstandingCommands == 0) {
24121713e81bSScott Long 					_VBUS_INST(&p.pAdapter->VBus);
24131713e81bSScott Long 					Check_Idle_Call(p.pAdapter);
24141713e81bSScott Long 					CheckPendingCall(_VBUS_P0);
24151713e81bSScott Long 				}
24161713e81bSScott Long 			}
241749b3fc40SJohn Baldwin 			mtx_unlock(&p.pAdapter->lock);
241849b3fc40SJohn Baldwin 			mtx_unlock(&DpcQueue_Lock);
24191713e81bSScott Long 
2420d2bd3ab9SScott Long 			/*Schedule out*/
2421d2bd3ab9SScott Long 			if (SIGISMEMBER(curproc->p_siglist, SIGSTOP)) {
24221713e81bSScott Long 				/* abort rebuilding process. */
2423d2bd3ab9SScott Long 				IAL_ADAPTER_T *pAdapter;
2424d2bd3ab9SScott Long 				PVDevice      pArray;
2425d2bd3ab9SScott Long 				PVBus         _vbus_p;
2426d2bd3ab9SScott Long 				int i;
242749b3fc40SJohn Baldwin 
242849b3fc40SJohn Baldwin 				sx_slock(&hptmv_list_lock);
24291713e81bSScott Long 				pAdapter = gIal_Adapter;
2430d2bd3ab9SScott Long 
24314d24901aSPedro F. Giffuni 				while(pAdapter != NULL){
243249b3fc40SJohn Baldwin 					mtx_lock(&pAdapter->lock);
24331713e81bSScott Long 					_vbus_p = &pAdapter->VBus;
2434d2bd3ab9SScott Long 
2435d2bd3ab9SScott Long 					for (i=0;i<MAX_ARRAY_PER_VBUS;i++)
2436d2bd3ab9SScott Long 					{
24371713e81bSScott Long 						if ((pArray=ArrayTables(i))->u.array.dArStamp==0)
24381713e81bSScott Long 							continue;
2439d2bd3ab9SScott Long 						else if (pArray->u.array.rf_rebuilding ||
24401713e81bSScott Long 								pArray->u.array.rf_verifying ||
2441d2bd3ab9SScott Long 								pArray->u.array.rf_initializing)
2442d2bd3ab9SScott Long 							{
24431713e81bSScott Long 								pArray->u.array.rf_abort_rebuild = 1;
24441713e81bSScott Long 							}
24451713e81bSScott Long 					}
244649b3fc40SJohn Baldwin 					mtx_unlock(&pAdapter->lock);
24471713e81bSScott Long 					pAdapter = pAdapter->next;
24481713e81bSScott Long 				}
244949b3fc40SJohn Baldwin 				sx_sunlock(&hptmv_list_lock);
24501713e81bSScott Long 			}
245149b3fc40SJohn Baldwin 			mtx_lock(&DpcQueue_Lock);
2452d2bd3ab9SScott Long 		}
245349b3fc40SJohn Baldwin 		mtx_unlock(&DpcQueue_Lock);
24541713e81bSScott Long 
2455d2bd3ab9SScott Long /*Remove this debug option*/
2456d2bd3ab9SScott Long /*
24571713e81bSScott Long #ifdef DEBUG
24581713e81bSScott Long 		if (SIGISMEMBER(curproc->p_siglist, SIGSTOP))
24594d70511aSJohn Baldwin 			pause("hptrdy", 2*hz);
24601713e81bSScott Long #endif
2461d2bd3ab9SScott Long */
24623745c395SJulian Elischer 		kproc_suspend_check(curproc);
246349b3fc40SJohn Baldwin 		pause("-", 2*hz);  /* wait for something to do */
24641713e81bSScott Long 	}
24651713e81bSScott Long }
24661713e81bSScott Long 
24671713e81bSScott Long static struct proc *hptdaemonproc;
24681713e81bSScott Long static struct kproc_desc hpt_kp = {
24691713e81bSScott Long 	"hpt_wt",
24701713e81bSScott Long 	hpt_worker_thread,
24711713e81bSScott Long 	&hptdaemonproc
24721713e81bSScott Long };
24731713e81bSScott Long 
2474d2bd3ab9SScott Long /*Start this thread in the hpt_attach, to prevent kernel from loading it without our controller.*/
24751713e81bSScott Long static void
24761713e81bSScott Long launch_worker_thread(void)
24771713e81bSScott Long {
24781713e81bSScott Long 	IAL_ADAPTER_T *pAdapTemp;
24791713e81bSScott Long 
24801713e81bSScott Long 	kproc_start(&hpt_kp);
24811713e81bSScott Long 
248249b3fc40SJohn Baldwin 	sx_slock(&hptmv_list_lock);
24831713e81bSScott Long 	for (pAdapTemp = gIal_Adapter; pAdapTemp; pAdapTemp = pAdapTemp->next) {
24841713e81bSScott Long 
24851713e81bSScott Long 		_VBUS_INST(&pAdapTemp->VBus)
24861713e81bSScott Long 		int i;
24871713e81bSScott Long 		PVDevice pVDev;
24881713e81bSScott Long 
24891713e81bSScott Long 		for(i = 0; i < MAX_ARRAY_PER_VBUS; i++)
24901713e81bSScott Long 			if ((pVDev=ArrayTables(i))->u.array.dArStamp==0)
24911713e81bSScott Long 				continue;
2492d2bd3ab9SScott Long 			else{
2493d2bd3ab9SScott Long 				if (pVDev->u.array.rf_need_rebuild && !pVDev->u.array.rf_rebuilding)
2494d2bd3ab9SScott Long 					hpt_queue_dpc((HPT_DPC)hpt_rebuild_data_block, pAdapTemp, pVDev,
2495d2bd3ab9SScott Long 					(UCHAR)((pVDev->u.array.CriticalMembers || pVDev->VDeviceType == VD_RAID_1)? DUPLICATE : REBUILD_PARITY));
24961713e81bSScott Long 			}
24971713e81bSScott Long 	}
249849b3fc40SJohn Baldwin 	sx_sunlock(&hptmv_list_lock);
24991713e81bSScott Long 
25001713e81bSScott Long 	/*
2501d2bd3ab9SScott Long 	 * hpt_worker_thread needs to be suspended after shutdown sync, when fs sync finished.
25021713e81bSScott Long 	 */
2503c33c9789SAlexander Motin 	EVENTHANDLER_REGISTER(shutdown_post_sync, kproc_shutdown, hptdaemonproc,
2504c33c9789SAlexander Motin 	    SHUTDOWN_PRI_LAST);
25051713e81bSScott Long }
2506d2bd3ab9SScott Long /*
2507d2bd3ab9SScott Long  *SYSINIT(hptwt, SI_SUB_KTHREAD_IDLE, SI_ORDER_FIRST, launch_worker_thread, NULL);
2508d2bd3ab9SScott Long */
25091713e81bSScott Long 
2510d2bd3ab9SScott Long #endif
25111713e81bSScott Long 
25121713e81bSScott Long /********************************************************************************/
25131713e81bSScott Long 
2514d2bd3ab9SScott Long int HPTLIBAPI fOsBuildSgl(_VBUS_ARG PCommand pCmd, FPSCAT_GATH pSg, int logical)
25151713e81bSScott Long {
25167634a04bSXin LI 	union ccb *ccb = (union ccb *)pCmd->pOrgCommand;
25171713e81bSScott Long 
25187634a04bSXin LI 	if (logical) {
25197634a04bSXin LI 		pSg->dSgAddress = (ULONG_PTR)(UCHAR *)ccb->csio.data_ptr;
25207634a04bSXin LI 		pSg->wSgSize = ccb->csio.dxfer_len;
25217634a04bSXin LI 		pSg->wSgFlag = SG_FLAG_EOT;
25227634a04bSXin LI 		return TRUE;
25237634a04bSXin LI 	}
2524d2bd3ab9SScott Long 	/* since we have provided physical sg, nobody will ask us to build physical sg */
2525d2bd3ab9SScott Long 	HPT_ASSERT(0);
25261713e81bSScott Long 	return FALSE;
25271713e81bSScott Long }
25281713e81bSScott Long 
25291713e81bSScott Long /*******************************************************************************/
25301713e81bSScott Long ULONG HPTLIBAPI
25311713e81bSScott Long GetStamp(void)
25321713e81bSScott Long {
25331713e81bSScott Long 	/*
25341713e81bSScott Long 	 * the system variable, ticks, can't be used since it hasn't yet been active
25351713e81bSScott Long 	 * when our driver starts (ticks==0, it's a invalid stamp value)
25361713e81bSScott Long 	 */
2537d2bd3ab9SScott Long 	ULONG stamp;
2538d2bd3ab9SScott Long 	do { stamp = random(); } while (stamp==0);
25391713e81bSScott Long 	return stamp;
25401713e81bSScott Long }
25411713e81bSScott Long 
25421713e81bSScott Long 
25431713e81bSScott Long static void
25441713e81bSScott Long SetInquiryData(PINQUIRYDATA inquiryData, PVDevice pVDev)
25451713e81bSScott Long {
25461713e81bSScott Long 	int i;
2547d2bd3ab9SScott Long 	IDENTIFY_DATA2 *pIdentify = (IDENTIFY_DATA2*)pVDev->u.disk.mv->identifyDevice;
25481713e81bSScott Long 
25491713e81bSScott Long 	inquiryData->DeviceType = T_DIRECT; /*DIRECT_ACCESS_DEVICE*/
25501713e81bSScott Long 	inquiryData->AdditionalLength = (UCHAR)(sizeof(INQUIRYDATA) - 5);
25511713e81bSScott Long #ifndef SERIAL_CMDS
25521713e81bSScott Long 	inquiryData->CommandQueue = 1;
25531713e81bSScott Long #endif
25541713e81bSScott Long 
25551713e81bSScott Long 	switch(pVDev->VDeviceType) {
25561713e81bSScott Long 	case VD_SINGLE_DISK:
25571713e81bSScott Long 	case VD_ATAPI:
25581713e81bSScott Long 	case VD_REMOVABLE:
25591713e81bSScott Long 		/* Set the removable bit, if applicable. */
2560d2bd3ab9SScott Long 		if ((pVDev->u.disk.df_removable_drive) || (pIdentify->GeneralConfiguration & 0x80))
25611713e81bSScott Long 			inquiryData->RemovableMedia = 1;
25621713e81bSScott Long 
25631713e81bSScott Long 		/* Fill in vendor identification fields. */
2564d2bd3ab9SScott Long 		for (i = 0; i < 20; i += 2) {
2565d2bd3ab9SScott Long 			inquiryData->VendorId[i] 	= ((PUCHAR)pIdentify->ModelNumber)[i + 1];
2566d2bd3ab9SScott Long 			inquiryData->VendorId[i+1] 	= ((PUCHAR)pIdentify->ModelNumber)[i];
25671713e81bSScott Long 
25681713e81bSScott Long 		}
25691713e81bSScott Long 
25701713e81bSScott Long 		/* Initialize unused portion of product id. */
25711713e81bSScott Long 		for (i = 0; i < 4; i++) inquiryData->ProductId[12+i] = ' ';
25721713e81bSScott Long 
25731713e81bSScott Long 		/* firmware revision */
2574d2bd3ab9SScott Long 		for (i = 0; i < 4; i += 2)
2575d2bd3ab9SScott Long 		{
2576d2bd3ab9SScott Long 			inquiryData->ProductRevisionLevel[i] 	= ((PUCHAR)pIdentify->FirmwareRevision)[i+1];
2577d2bd3ab9SScott Long 			inquiryData->ProductRevisionLevel[i+1] 	= ((PUCHAR)pIdentify->FirmwareRevision)[i];
25781713e81bSScott Long 		}
25791713e81bSScott Long 		break;
25801713e81bSScott Long 	default:
258164470755SXin LI 		memcpy(&inquiryData->VendorId, "RR18xx  ", 8);
25821713e81bSScott Long #ifdef SUPPORT_ARRAY
25831713e81bSScott Long 		switch(pVDev->VDeviceType){
25841713e81bSScott Long 		case VD_RAID_0:
2585d2bd3ab9SScott Long 			if ((pVDev->u.array.pMember[0] && mIsArray(pVDev->u.array.pMember[0])) ||
2586d2bd3ab9SScott Long 				(pVDev->u.array.pMember[1] && mIsArray(pVDev->u.array.pMember[1])))
2587d2bd3ab9SScott Long 				memcpy(&inquiryData->ProductId, "RAID 1/0 Array  ", 16);
25881713e81bSScott Long 			else
2589d2bd3ab9SScott Long 				memcpy(&inquiryData->ProductId, "RAID 0 Array    ", 16);
25901713e81bSScott Long 			break;
25911713e81bSScott Long 		case VD_RAID_1:
2592d2bd3ab9SScott Long 			if ((pVDev->u.array.pMember[0] && mIsArray(pVDev->u.array.pMember[0])) ||
2593d2bd3ab9SScott Long 				(pVDev->u.array.pMember[1] && mIsArray(pVDev->u.array.pMember[1])))
2594d2bd3ab9SScott Long 				memcpy(&inquiryData->ProductId, "RAID 0/1 Array  ", 16);
25951713e81bSScott Long 			else
2596d2bd3ab9SScott Long 				memcpy(&inquiryData->ProductId, "RAID 1 Array    ", 16);
25971713e81bSScott Long 			break;
25981713e81bSScott Long 		case VD_RAID_5:
25991713e81bSScott Long 			memcpy(&inquiryData->ProductId, "RAID 5 Array    ", 16);
26001713e81bSScott Long 			break;
26011713e81bSScott Long 		case VD_JBOD:
26021713e81bSScott Long 			memcpy(&inquiryData->ProductId, "JBOD Array      ", 16);
26031713e81bSScott Long 			break;
26041713e81bSScott Long 		}
26051713e81bSScott Long #endif
26061713e81bSScott Long 		memcpy(&inquiryData->ProductRevisionLevel, "3.00", 4);
26071713e81bSScott Long 		break;
26081713e81bSScott Long 	}
26091713e81bSScott Long }
26101713e81bSScott Long 
26111713e81bSScott Long static void
26121713e81bSScott Long hpt_timeout(void *arg)
26131713e81bSScott Long {
261449b3fc40SJohn Baldwin 	PBUS_DMAMAP pmap = (PBUS_DMAMAP)((union ccb *)arg)->ccb_adapter;
261549b3fc40SJohn Baldwin 	IAL_ADAPTER_T *pAdapter = pmap->pAdapter;
261649b3fc40SJohn Baldwin 	_VBUS_INST(&pAdapter->VBus)
261749b3fc40SJohn Baldwin 
261849b3fc40SJohn Baldwin 	mtx_assert(&pAdapter->lock, MA_OWNED);
26191713e81bSScott Long 	fResetVBus(_VBUS_P0);
26201713e81bSScott Long }
26211713e81bSScott Long 
2622d2bd3ab9SScott Long static void
2623d2bd3ab9SScott Long hpt_io_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
2624d2bd3ab9SScott Long {
2625d2bd3ab9SScott Long 	PCommand pCmd = (PCommand)arg;
2626d2bd3ab9SScott Long 	union ccb *ccb = pCmd->pOrgCommand;
2627d2bd3ab9SScott Long 	struct ccb_hdr *ccb_h = &ccb->ccb_h;
2628d2bd3ab9SScott Long 	PBUS_DMAMAP pmap = (PBUS_DMAMAP) ccb->ccb_adapter;
2629d2bd3ab9SScott Long 	IAL_ADAPTER_T *pAdapter = pmap->pAdapter;
2630d2bd3ab9SScott Long 	PVDevice	pVDev = pAdapter->VBus.pVDevice[ccb_h->target_id];
2631d2bd3ab9SScott Long 	FPSCAT_GATH psg = pCmd->pSgTable;
2632d2bd3ab9SScott Long 	int idx;
2633d2bd3ab9SScott Long 	_VBUS_INST(pVDev->pVBus)
2634d2bd3ab9SScott Long 
2635d2bd3ab9SScott Long 	HPT_ASSERT(pCmd->cf_physical_sg);
2636d2bd3ab9SScott Long 
2637dd0b4fb6SKonstantin Belousov 	if (error)
2638d2bd3ab9SScott Long 		panic("busdma error");
2639d2bd3ab9SScott Long 
2640d2bd3ab9SScott Long 	HPT_ASSERT(nsegs<= MAX_SG_DESCRIPTORS);
2641d2bd3ab9SScott Long 
2642dd0b4fb6SKonstantin Belousov 	if (nsegs != 0) {
2643d2bd3ab9SScott Long 		for (idx = 0; idx < nsegs; idx++, psg++) {
2644d2bd3ab9SScott Long 			psg->dSgAddress = (ULONG_PTR)(UCHAR *)segs[idx].ds_addr;
2645d2bd3ab9SScott Long 			psg->wSgSize = segs[idx].ds_len;
2646d2bd3ab9SScott Long 			psg->wSgFlag = (idx == nsegs-1)? SG_FLAG_EOT: 0;
2647d2bd3ab9SScott Long 	/*		KdPrint(("psg[%d]:add=%p,size=%x,flag=%x\n", idx, psg->dSgAddress,psg->wSgSize,psg->wSgFlag)); */
2648d2bd3ab9SScott Long 		}
2649d2bd3ab9SScott Long 		/*	psg[-1].wSgFlag = SG_FLAG_EOT; */
2650d2bd3ab9SScott Long 
2651d2bd3ab9SScott Long 		if (pCmd->cf_data_in) {
2652dd0b4fb6SKonstantin Belousov 			bus_dmamap_sync(pAdapter->io_dma_parent, pmap->dma_map,
2653dd0b4fb6SKonstantin Belousov 			    BUS_DMASYNC_PREREAD);
2654d2bd3ab9SScott Long 		}
2655d2bd3ab9SScott Long 		else if (pCmd->cf_data_out) {
2656dd0b4fb6SKonstantin Belousov 			bus_dmamap_sync(pAdapter->io_dma_parent, pmap->dma_map,
2657dd0b4fb6SKonstantin Belousov 			    BUS_DMASYNC_PREWRITE);
2658dd0b4fb6SKonstantin Belousov 		}
2659d2bd3ab9SScott Long 	}
2660d2bd3ab9SScott Long 
266149b3fc40SJohn Baldwin 	callout_reset(&pmap->timeout, 20 * hz, hpt_timeout, ccb);
2662d2bd3ab9SScott Long 	pVDev->pfnSendCommand(_VBUS_P pCmd);
2663d2bd3ab9SScott Long 	CheckPendingCall(_VBUS_P0);
2664d2bd3ab9SScott Long }
2665d2bd3ab9SScott Long 
2666d2bd3ab9SScott Long 
2667d2bd3ab9SScott Long 
26681713e81bSScott Long static void HPTLIBAPI
26691713e81bSScott Long OsSendCommand(_VBUS_ARG union ccb *ccb)
26701713e81bSScott Long {
2671d2bd3ab9SScott Long 	PBUS_DMAMAP pmap = (PBUS_DMAMAP)ccb->ccb_adapter;
2672d2bd3ab9SScott Long 	IAL_ADAPTER_T *pAdapter = pmap->pAdapter;
2673d2bd3ab9SScott Long 	struct ccb_hdr *ccb_h = &ccb->ccb_h;
2674d2bd3ab9SScott Long 	struct ccb_scsiio *csio = &ccb->csio;
2675d2bd3ab9SScott Long 	PVDevice	pVDev = pAdapter->VBus.pVDevice[ccb_h->target_id];
26761713e81bSScott Long 
26777d9aed9cSScott Long 	KdPrintI(("OsSendCommand: ccb %p  cdb %x-%x-%x\n",
26781713e81bSScott Long 		ccb,
26791713e81bSScott Long 		*(ULONG *)&ccb->csio.cdb_io.cdb_bytes[0],
26801713e81bSScott Long 		*(ULONG *)&ccb->csio.cdb_io.cdb_bytes[4],
26811713e81bSScott Long 		*(ULONG *)&ccb->csio.cdb_io.cdb_bytes[8]
26821713e81bSScott Long 	));
26831713e81bSScott Long 
26841713e81bSScott Long 	pAdapter->outstandingCommands++;
26851713e81bSScott Long 
26861713e81bSScott Long 	if (pVDev == NULL || pVDev->vf_online == 0) {
26871713e81bSScott Long 		ccb->ccb_h.status = CAM_REQ_INVALID;
26881713e81bSScott Long 		ccb_done(ccb);
26891713e81bSScott Long 		goto Command_Complished;
26901713e81bSScott Long 	}
26911713e81bSScott Long 
26921713e81bSScott Long 	switch(ccb->csio.cdb_io.cdb_bytes[0])
26931713e81bSScott Long 	{
26941713e81bSScott Long 		case TEST_UNIT_READY:
26951713e81bSScott Long 		case START_STOP_UNIT:
26961713e81bSScott Long 		case SYNCHRONIZE_CACHE:
26971713e81bSScott Long 			/* FALLTHROUGH */
26981713e81bSScott Long 			ccb->ccb_h.status = CAM_REQ_CMP;
26991713e81bSScott Long 			break;
27001713e81bSScott Long 
27011713e81bSScott Long 		case INQUIRY:
27021713e81bSScott Long 			ZeroMemory(ccb->csio.data_ptr, ccb->csio.dxfer_len);
27031713e81bSScott Long 			SetInquiryData((PINQUIRYDATA)ccb->csio.data_ptr, pVDev);
27041713e81bSScott Long 			ccb_h->status = CAM_REQ_CMP;
27051713e81bSScott Long 			break;
27061713e81bSScott Long 
27071713e81bSScott Long 		case READ_CAPACITY:
27081713e81bSScott Long 		{
270964470755SXin LI 			UCHAR *rbuf=csio->data_ptr;
271064470755SXin LI 			unsigned int cap;
271164470755SXin LI 
271264470755SXin LI 			if (pVDev->VDeviceCapacity > 0xfffffffful) {
271364470755SXin LI 				cap = 0xfffffffful;
271464470755SXin LI 			} else {
271564470755SXin LI 				cap = pVDev->VDeviceCapacity - 1;
271664470755SXin LI 			}
271764470755SXin LI 
271864470755SXin LI 			rbuf[0] = (UCHAR)(cap>>24);
271964470755SXin LI 			rbuf[1] = (UCHAR)(cap>>16);
272064470755SXin LI 			rbuf[2] = (UCHAR)(cap>>8);
272164470755SXin LI 			rbuf[3] = (UCHAR)cap;
27221713e81bSScott Long 			/* Claim 512 byte blocks (big-endian). */
272364470755SXin LI 			rbuf[4] = 0;
272464470755SXin LI 			rbuf[5] = 0;
272564470755SXin LI 			rbuf[6] = 2;
272664470755SXin LI 			rbuf[7] = 0;
272764470755SXin LI 
272864470755SXin LI 			ccb_h->status = CAM_REQ_CMP;
272964470755SXin LI 			break;
273064470755SXin LI 		}
273164470755SXin LI 
273264470755SXin LI 		case 0x9e: /*SERVICE_ACTION_IN*/
273364470755SXin LI 		{
273464470755SXin LI 			UCHAR *rbuf = csio->data_ptr;
273564470755SXin LI 			LBA_T cap = pVDev->VDeviceCapacity - 1;
273664470755SXin LI 
273764470755SXin LI 			rbuf[0] = (UCHAR)(cap>>56);
273864470755SXin LI 			rbuf[1] = (UCHAR)(cap>>48);
273964470755SXin LI 			rbuf[2] = (UCHAR)(cap>>40);
274064470755SXin LI 			rbuf[3] = (UCHAR)(cap>>32);
274164470755SXin LI 			rbuf[4] = (UCHAR)(cap>>24);
274264470755SXin LI 			rbuf[5] = (UCHAR)(cap>>16);
274364470755SXin LI 			rbuf[6] = (UCHAR)(cap>>8);
274464470755SXin LI 			rbuf[7] = (UCHAR)cap;
274564470755SXin LI 			rbuf[8] = 0;
274664470755SXin LI 			rbuf[9] = 0;
274764470755SXin LI 			rbuf[10] = 2;
274864470755SXin LI 			rbuf[11] = 0;
274964470755SXin LI 
27501713e81bSScott Long 			ccb_h->status = CAM_REQ_CMP;
27511713e81bSScott Long 			break;
27521713e81bSScott Long 		}
27531713e81bSScott Long 
27541713e81bSScott Long 		case READ_6:
27551713e81bSScott Long 		case WRITE_6:
27561713e81bSScott Long 		case READ_10:
27571713e81bSScott Long 		case WRITE_10:
275864470755SXin LI 		case 0x88: /* READ_16 */
275964470755SXin LI 		case 0x8a: /* WRITE_16 */
27601713e81bSScott Long 		case 0x13:
27611713e81bSScott Long 		case 0x2f:
27621713e81bSScott Long 		{
27631713e81bSScott Long 			UCHAR Cdb[16];
27641713e81bSScott Long 			UCHAR CdbLength;
27651713e81bSScott Long 			_VBUS_INST(pVDev->pVBus)
2766d2bd3ab9SScott Long 			PCommand pCmd = AllocateCommand(_VBUS_P0);
2767dd0b4fb6SKonstantin Belousov 			int error;
27681713e81bSScott Long 			HPT_ASSERT(pCmd);
27691713e81bSScott Long 
27701713e81bSScott Long 			CdbLength = csio->cdb_len;
2771d2bd3ab9SScott Long 			if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0)
2772d2bd3ab9SScott Long 			{
2773d2bd3ab9SScott Long 				if ((ccb->ccb_h.flags & CAM_CDB_PHYS) == 0)
2774d2bd3ab9SScott Long 				{
27751713e81bSScott Long 					bcopy(csio->cdb_io.cdb_ptr, Cdb, CdbLength);
2776d2bd3ab9SScott Long 				}
2777d2bd3ab9SScott Long 				else
2778d2bd3ab9SScott Long 				{
27791713e81bSScott Long 					KdPrintE(("ERROR!!!\n"));
27801713e81bSScott Long 					ccb->ccb_h.status = CAM_REQ_INVALID;
27811713e81bSScott Long 					break;
27821713e81bSScott Long 				}
2783d2bd3ab9SScott Long 			}
2784d2bd3ab9SScott Long 			else
2785d2bd3ab9SScott Long 			{
27861713e81bSScott Long 				bcopy(csio->cdb_io.cdb_bytes, Cdb, CdbLength);
27871713e81bSScott Long 			}
27881713e81bSScott Long 
2789d2bd3ab9SScott Long 			pCmd->pOrgCommand = ccb;
27901713e81bSScott Long 			pCmd->pVDevice = pVDev;
27911713e81bSScott Long 			pCmd->pfnCompletion = fOsCommandDone;
27921713e81bSScott Long 			pCmd->pfnBuildSgl = fOsBuildSgl;
2793d2bd3ab9SScott Long 			pCmd->pSgTable = pmap->psg;
27941713e81bSScott Long 
2795d2bd3ab9SScott Long 			switch (Cdb[0])
2796d2bd3ab9SScott Long 			{
27971713e81bSScott Long 				case READ_6:
27981713e81bSScott Long 				case WRITE_6:
27991713e81bSScott Long 				case 0x13:
2800d2bd3ab9SScott Long 					pCmd->uCmd.Ide.Lba =  ((ULONG)Cdb[1] << 16) | ((ULONG)Cdb[2] << 8) | (ULONG)Cdb[3];
28011713e81bSScott Long 					pCmd->uCmd.Ide.nSectors = (USHORT) Cdb[4];
28021713e81bSScott Long 					break;
2803d2bd3ab9SScott Long 
280464470755SXin LI 				case 0x88: /* READ_16 */
280564470755SXin LI 				case 0x8a: /* WRITE_16 */
280664470755SXin LI 					pCmd->uCmd.Ide.Lba =
280764470755SXin LI 						(HPT_U64)Cdb[2] << 56 |
280864470755SXin LI 						(HPT_U64)Cdb[3] << 48 |
280964470755SXin LI 						(HPT_U64)Cdb[4] << 40 |
281064470755SXin LI 						(HPT_U64)Cdb[5] << 32 |
281164470755SXin LI 						(HPT_U64)Cdb[6] << 24 |
281264470755SXin LI 						(HPT_U64)Cdb[7] << 16 |
281364470755SXin LI 						(HPT_U64)Cdb[8] << 8 |
281464470755SXin LI 						(HPT_U64)Cdb[9];
281564470755SXin LI 					pCmd->uCmd.Ide.nSectors = (USHORT)Cdb[12] << 8 | (USHORT)Cdb[13];
281664470755SXin LI 					break;
281764470755SXin LI 
28181713e81bSScott Long 				default:
2819d2bd3ab9SScott Long 					pCmd->uCmd.Ide.Lba = (ULONG)Cdb[5] | ((ULONG)Cdb[4] << 8) | ((ULONG)Cdb[3] << 16) | ((ULONG)Cdb[2] << 24);
2820d2bd3ab9SScott Long 					pCmd->uCmd.Ide.nSectors = (USHORT) Cdb[8] | ((USHORT)Cdb[7]<<8);
28211713e81bSScott Long 					break;
28221713e81bSScott Long 			}
28231713e81bSScott Long 
2824d2bd3ab9SScott Long 			switch (Cdb[0])
2825d2bd3ab9SScott Long 			{
28261713e81bSScott Long 				case READ_6:
28271713e81bSScott Long 				case READ_10:
282864470755SXin LI 				case 0x88: /* READ_16 */
28291713e81bSScott Long 					pCmd->uCmd.Ide.Command = IDE_COMMAND_READ;
28301713e81bSScott Long 					pCmd->cf_data_in = 1;
28311713e81bSScott Long 					break;
28321713e81bSScott Long 
28331713e81bSScott Long 				case WRITE_6:
28341713e81bSScott Long 				case WRITE_10:
283564470755SXin LI 				case 0x8a: /* WRITE_16 */
28361713e81bSScott Long 					pCmd->uCmd.Ide.Command = IDE_COMMAND_WRITE;
28371713e81bSScott Long 					pCmd->cf_data_out = 1;
28381713e81bSScott Long 					break;
28391713e81bSScott Long 				case 0x13:
28401713e81bSScott Long 				case 0x2f:
28411713e81bSScott Long 					pCmd->uCmd.Ide.Command = IDE_COMMAND_VERIFY;
28421713e81bSScott Long 					break;
28431713e81bSScott Long 			}
2844d2bd3ab9SScott Long /*///////////////////////// */
2845d2bd3ab9SScott Long 			pCmd->cf_physical_sg = 1;
2846dd0b4fb6SKonstantin Belousov 			error = bus_dmamap_load_ccb(pAdapter->io_dma_parent,
2847d2bd3ab9SScott Long 						    pmap->dma_map,
2848dd0b4fb6SKonstantin Belousov 						    ccb,
2849dd0b4fb6SKonstantin Belousov 						    hpt_io_dmamap_callback,
2850dd0b4fb6SKonstantin Belousov 						    pCmd, BUS_DMA_WAITOK
2851d2bd3ab9SScott Long 						    );
2852d2bd3ab9SScott Long 			KdPrint(("bus_dmamap_load return %d\n", error));
2853d2bd3ab9SScott Long 			if (error && error!=EINPROGRESS) {
2854d2bd3ab9SScott Long 				hpt_printk(("bus_dmamap_load error %d\n", error));
2855d2bd3ab9SScott Long 				FreeCommand(_VBUS_P pCmd);
2856d2bd3ab9SScott Long 				ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2857d2bd3ab9SScott Long 				dmamap_put(pmap);
2858d2bd3ab9SScott Long 				pAdapter->outstandingCommands--;
285949b3fc40SJohn Baldwin 				if (pAdapter->outstandingCommands == 0)
286049b3fc40SJohn Baldwin 					wakeup(pAdapter);
2861d2bd3ab9SScott Long 				xpt_done(ccb);
2862d2bd3ab9SScott Long 			}
28631713e81bSScott Long 			goto Command_Complished;
28641713e81bSScott Long 		}
28651713e81bSScott Long 
28661713e81bSScott Long 		default:
28671713e81bSScott Long 			ccb->ccb_h.status = CAM_REQ_INVALID;
28681713e81bSScott Long 			break;
28691713e81bSScott Long 	}
28701713e81bSScott Long 	ccb_done(ccb);
28711713e81bSScott Long Command_Complished:
28721713e81bSScott Long 	CheckPendingCall(_VBUS_P0);
28731713e81bSScott Long 	return;
28741713e81bSScott Long }
28751713e81bSScott Long 
28761713e81bSScott Long static void HPTLIBAPI
28771713e81bSScott Long fOsCommandDone(_VBUS_ARG PCommand pCmd)
28781713e81bSScott Long {
2879d2bd3ab9SScott Long 	union ccb *ccb = pCmd->pOrgCommand;
2880d2bd3ab9SScott Long 	PBUS_DMAMAP pmap = (PBUS_DMAMAP)ccb->ccb_adapter;
2881d2bd3ab9SScott Long 	IAL_ADAPTER_T *pAdapter = pmap->pAdapter;
28821713e81bSScott Long 
2883d2bd3ab9SScott Long 	KdPrint(("fOsCommandDone(pcmd=%p, result=%d)\n", pCmd, pCmd->Result));
28841713e81bSScott Long 
288549b3fc40SJohn Baldwin 	callout_stop(&pmap->timeout);
28861713e81bSScott Long 
28871713e81bSScott Long 	switch(pCmd->Result) {
28881713e81bSScott Long 	case RETURN_SUCCESS:
28891713e81bSScott Long 		ccb->ccb_h.status = CAM_REQ_CMP;
28901713e81bSScott Long 		break;
28911713e81bSScott Long 	case RETURN_BAD_DEVICE:
28921713e81bSScott Long 		ccb->ccb_h.status = CAM_DEV_NOT_THERE;
28931713e81bSScott Long 		break;
28941713e81bSScott Long 	case RETURN_DEVICE_BUSY:
28951713e81bSScott Long 		ccb->ccb_h.status = CAM_BUSY;
28961713e81bSScott Long 		break;
28971713e81bSScott Long 	case RETURN_INVALID_REQUEST:
28981713e81bSScott Long 		ccb->ccb_h.status = CAM_REQ_INVALID;
28991713e81bSScott Long 		break;
29001713e81bSScott Long 	case RETURN_SELECTION_TIMEOUT:
29011713e81bSScott Long 		ccb->ccb_h.status = CAM_SEL_TIMEOUT;
29021713e81bSScott Long 		break;
29031713e81bSScott Long 	case RETURN_RETRY:
29041713e81bSScott Long 		ccb->ccb_h.status = CAM_BUSY;
29051713e81bSScott Long 		break;
29061713e81bSScott Long 	default:
29071713e81bSScott Long 		ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
29081713e81bSScott Long 		break;
29091713e81bSScott Long 	}
29101713e81bSScott Long 
2911d2bd3ab9SScott Long 	if (pCmd->cf_data_in) {
2912d2bd3ab9SScott Long 		bus_dmamap_sync(pAdapter->io_dma_parent, pmap->dma_map, BUS_DMASYNC_POSTREAD);
29131713e81bSScott Long 	}
2914a8bc7437SXin LI 	else if (pCmd->cf_data_out) {
2915d2bd3ab9SScott Long 		bus_dmamap_sync(pAdapter->io_dma_parent, pmap->dma_map, BUS_DMASYNC_POSTWRITE);
2916d2bd3ab9SScott Long 	}
29171713e81bSScott Long 
2918d2bd3ab9SScott Long 	bus_dmamap_unload(pAdapter->io_dma_parent, pmap->dma_map);
2919d2bd3ab9SScott Long 
29201713e81bSScott Long 	FreeCommand(_VBUS_P pCmd);
29211713e81bSScott Long 	ccb_done(ccb);
29221713e81bSScott Long }
29231713e81bSScott Long 
29241713e81bSScott Long int
29251713e81bSScott Long hpt_queue_dpc(HPT_DPC dpc, IAL_ADAPTER_T * pAdapter, void *arg, UCHAR flags)
29261713e81bSScott Long {
29271713e81bSScott Long 	int p;
29281713e81bSScott Long 
292949b3fc40SJohn Baldwin 	mtx_lock(&DpcQueue_Lock);
29301713e81bSScott Long 	p = (DpcQueue_Last + 1) % MAX_DPC;
29311713e81bSScott Long 	if (p==DpcQueue_First) {
29321713e81bSScott Long 		KdPrint(("DPC Queue full!\n"));
293349b3fc40SJohn Baldwin 		mtx_unlock(&DpcQueue_Lock);
29341713e81bSScott Long 		return -1;
29351713e81bSScott Long 	}
29361713e81bSScott Long 
29371713e81bSScott Long 	DpcQueue[DpcQueue_Last].dpc = dpc;
29381713e81bSScott Long 	DpcQueue[DpcQueue_Last].pAdapter = pAdapter;
29391713e81bSScott Long 	DpcQueue[DpcQueue_Last].arg = arg;
29401713e81bSScott Long 	DpcQueue[DpcQueue_Last].flags = flags;
29411713e81bSScott Long 	DpcQueue_Last = p;
294249b3fc40SJohn Baldwin 	mtx_unlock(&DpcQueue_Lock);
29431713e81bSScott Long 
29441713e81bSScott Long 	return 0;
29451713e81bSScott Long }
29461713e81bSScott Long 
29471713e81bSScott Long #ifdef _RAID5N_
29481713e81bSScott Long /*
2949d2bd3ab9SScott Long  * Allocate memory above 16M, otherwise we may eat all low memory for ISA devices.
2950d2bd3ab9SScott Long  * How about the memory for 5081 request/response array and PRD table?
29511713e81bSScott Long  */
29521713e81bSScott Long void
29531713e81bSScott Long *os_alloc_page(_VBUS_ARG0)
29541713e81bSScott Long {
2955d2bd3ab9SScott Long 	return (void *)contigmalloc(0x1000, M_DEVBUF, M_NOWAIT, 0x1000000, 0xffffffff, PAGE_SIZE, 0ul);
29561713e81bSScott Long }
2957d2bd3ab9SScott Long 
29581713e81bSScott Long void
29591713e81bSScott Long *os_alloc_dma_page(_VBUS_ARG0)
29601713e81bSScott Long {
2961d2bd3ab9SScott Long 	return (void *)contigmalloc(0x1000, M_DEVBUF, M_NOWAIT, 0x1000000, 0xffffffff, PAGE_SIZE, 0ul);
29621713e81bSScott Long }
29631713e81bSScott Long 
29641713e81bSScott Long void
29651713e81bSScott Long os_free_page(_VBUS_ARG void *p)
29661713e81bSScott Long {
29671713e81bSScott Long 	contigfree(p, 0x1000, M_DEVBUF);
29681713e81bSScott Long }
29691713e81bSScott Long 
29701713e81bSScott Long void
29711713e81bSScott Long os_free_dma_page(_VBUS_ARG void *p)
29721713e81bSScott Long {
29731713e81bSScott Long 	contigfree(p, 0x1000, M_DEVBUF);
29741713e81bSScott Long }
29751713e81bSScott Long 
29761713e81bSScott Long void
29771713e81bSScott Long DoXor1(ULONG *p0, ULONG *p1, ULONG *p2, UINT nBytes)
29781713e81bSScott Long {
29791713e81bSScott Long 	UINT i;
2980d2bd3ab9SScott Long 	for (i = 0; i < nBytes / 4; i++) *p0++ = *p1++ ^ *p2++;
29811713e81bSScott Long }
29821713e81bSScott Long 
29831713e81bSScott Long void
29841713e81bSScott Long DoXor2(ULONG *p0, ULONG *p2, UINT nBytes)
29851713e81bSScott Long {
29861713e81bSScott Long 	UINT i;
2987d2bd3ab9SScott Long 	for (i = 0; i < nBytes / 4; i++) *p0++ ^= *p2++;
29881713e81bSScott Long }
29891713e81bSScott Long #endif
2990