xref: /freebsd/sys/dev/hptmv/entry.c (revision d1bdc2821fcd416ab9b238580386eb605a6128d0)
1718cf2ccSPedro F. Giffuni /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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 
291713e81bSScott Long #include <sys/param.h>
301713e81bSScott Long #include <sys/systm.h>
311713e81bSScott Long #include <sys/kernel.h>
321713e81bSScott Long #include <sys/bus.h>
331713e81bSScott Long #include <sys/malloc.h>
341713e81bSScott Long #include <sys/resource.h>
351713e81bSScott Long #include <sys/time.h>
361713e81bSScott Long #include <sys/callout.h>
371713e81bSScott Long #include <sys/signalvar.h>
381713e81bSScott Long #include <sys/eventhandler.h>
391713e81bSScott Long #include <sys/proc.h>
401713e81bSScott Long #include <sys/kthread.h>
411713e81bSScott Long 
42d2bd3ab9SScott Long #include <sys/mutex.h>
43d2bd3ab9SScott Long #include <sys/module.h>
4449b3fc40SJohn Baldwin #include <sys/sx.h>
45d2bd3ab9SScott Long 
461713e81bSScott Long #include <dev/pci/pcireg.h>
471713e81bSScott Long #include <dev/pci/pcivar.h>
48d2bd3ab9SScott Long 
49d2bd3ab9SScott Long #ifndef __KERNEL__
50d2bd3ab9SScott Long #define __KERNEL__
51d2bd3ab9SScott Long #endif
521713e81bSScott Long 
531713e81bSScott Long #include <dev/hptmv/global.h>
541713e81bSScott Long #include <dev/hptmv/hptintf.h>
551713e81bSScott Long #include <dev/hptmv/osbsd.h>
56f7f3900bSScott Long #include <dev/hptmv/access601.h>
571713e81bSScott Long 
58d2bd3ab9SScott Long 
591713e81bSScott Long #ifdef DEBUG
601713e81bSScott Long #ifdef DEBUG_LEVEL
611713e81bSScott Long int hpt_dbg_level = DEBUG_LEVEL;
621713e81bSScott Long #else
631713e81bSScott Long int hpt_dbg_level = 0;
641713e81bSScott Long #endif
651713e81bSScott Long #endif
661713e81bSScott Long 
671713e81bSScott Long #define MV_ERROR printf
68d2bd3ab9SScott Long 
691713e81bSScott Long /*
701713e81bSScott Long  * CAM SIM entry points
711713e81bSScott Long  */
721713e81bSScott Long static int 	hpt_probe (device_t dev);
73d2bd3ab9SScott Long static void launch_worker_thread(void);
741713e81bSScott Long static int 	hpt_attach(device_t dev);
751713e81bSScott Long static int 	hpt_detach(device_t dev);
761713e81bSScott Long static int 	hpt_shutdown(device_t dev);
771713e81bSScott Long static void hpt_poll(struct cam_sim *sim);
781713e81bSScott Long static void hpt_intr(void *arg);
79d2bd3ab9SScott Long static void hpt_async(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg);
801713e81bSScott Long static void hpt_action(struct cam_sim *sim, union ccb *ccb);
81d2bd3ab9SScott Long 
82d2bd3ab9SScott Long static device_method_t driver_methods[] = {
83d2bd3ab9SScott Long 	/* Device interface */
84d2bd3ab9SScott Long 	DEVMETHOD(device_probe,		hpt_probe),
85d2bd3ab9SScott Long 	DEVMETHOD(device_attach,	hpt_attach),
86d2bd3ab9SScott Long 	DEVMETHOD(device_detach,	hpt_detach),
87d2bd3ab9SScott Long 
88cd3ef666SXin LI 	DEVMETHOD(device_shutdown,	hpt_shutdown),
89f3b080e6SMarius Strobl 	DEVMETHOD_END
90d2bd3ab9SScott Long };
91d2bd3ab9SScott Long 
92d2bd3ab9SScott Long static driver_t hpt_pci_driver = {
93d2bd3ab9SScott Long 	__str(PROC_DIR_NAME),
94d2bd3ab9SScott Long 	driver_methods,
95d2bd3ab9SScott Long 	sizeof(IAL_ADAPTER_T)
96d2bd3ab9SScott Long };
97d2bd3ab9SScott Long 
988272dd4bSJohn Baldwin #define __DRIVER_MODULE(p1, p2, p3, p4, p5) DRIVER_MODULE(p1, p2, p3, p4, p5)
998272dd4bSJohn Baldwin __DRIVER_MODULE(PROC_DIR_NAME, pci, hpt_pci_driver, 0, 0);
100d45ce511SEitan Adler MODULE_DEPEND(PROC_DIR_NAME, cam, 1, 1, 1);
101d2bd3ab9SScott Long 
102d2bd3ab9SScott Long #define ccb_ccb_ptr spriv_ptr0
103d2bd3ab9SScott Long #define ccb_adapter ccb_h.spriv_ptr1
104d2bd3ab9SScott Long 
1051713e81bSScott Long static void SetInquiryData(PINQUIRYDATA inquiryData, PVDevice pVDev);
1061713e81bSScott Long static void HPTLIBAPI OsSendCommand (_VBUS_ARG union ccb * ccb);
1071713e81bSScott Long static void HPTLIBAPI fOsCommandDone(_VBUS_ARG PCommand pCmd);
1081713e81bSScott Long static void ccb_done(union ccb *ccb);
1091713e81bSScott Long static void hpt_queue_ccb(union ccb **ccb_Q, union ccb *ccb);
1101713e81bSScott Long static void hpt_free_ccb(union ccb **ccb_Q, union ccb *ccb);
11149b3fc40SJohn Baldwin static void hpt_intr_locked(IAL_ADAPTER_T *pAdapter);
1121713e81bSScott Long static void	hptmv_free_edma_queues(IAL_ADAPTER_T *pAdapter);
1131713e81bSScott Long static void	hptmv_free_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum);
1141713e81bSScott Long static void	handleEdmaError(_VBUS_ARG PCommand pCmd);
1151713e81bSScott Long static int	hptmv_init_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum);
1161713e81bSScott Long static int	fResetActiveCommands(PVBus _vbus_p);
1171713e81bSScott Long static void	fRegisterVdevice(IAL_ADAPTER_T *pAdapter);
1181713e81bSScott Long static int	hptmv_allocate_edma_queues(IAL_ADAPTER_T *pAdapter);
1191713e81bSScott Long static void	hptmv_handle_event_disconnect(void *data);
1201713e81bSScott Long static void	hptmv_handle_event_connect(void *data);
1211713e81bSScott Long static int	start_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum);
1221713e81bSScott Long static void	init_vdev_params(IAL_ADAPTER_T *pAdapter, MV_U8 channel);
1231713e81bSScott Long static int	hptmv_parse_identify_results(MV_SATA_CHANNEL *pMvSataChannel);
1241713e81bSScott Long static int HPTLIBAPI fOsBuildSgl(_VBUS_ARG PCommand pCmd, FPSCAT_GATH pSg,
1251713e81bSScott Long     int logical);
1261713e81bSScott Long static MV_BOOLEAN CommandCompletionCB(MV_SATA_ADAPTER *pMvSataAdapter,
1271713e81bSScott Long     MV_U8 channelNum, MV_COMPLETION_TYPE comp_type, MV_VOID_PTR commandId,
1281713e81bSScott Long     MV_U16 responseFlags, MV_U32 timeStamp,
1291713e81bSScott Long     MV_STORAGE_DEVICE_REGISTERS *registerStruct);
1301713e81bSScott Long static MV_BOOLEAN hptmv_event_notify(MV_SATA_ADAPTER *pMvSataAdapter,
1311713e81bSScott Long     MV_EVENT_TYPE eventType, MV_U32 param1, MV_U32 param2);
1321713e81bSScott Long 
1331713e81bSScott Long #define ccb_ccb_ptr spriv_ptr0
1341713e81bSScott Long #define ccb_adapter ccb_h.spriv_ptr1
1351713e81bSScott Long 
13649b3fc40SJohn Baldwin static struct sx hptmv_list_lock;
13749b3fc40SJohn Baldwin SX_SYSINIT(hptmv_list_lock, &hptmv_list_lock, "hptmv list");
1384d24901aSPedro F. Giffuni IAL_ADAPTER_T *gIal_Adapter = NULL;
1394d24901aSPedro F. Giffuni IAL_ADAPTER_T *pCurAdapter = NULL;
140d2bd3ab9SScott Long static MV_SATA_CHANNEL gMvSataChannels[MAX_VBUS][MV_SATA_CHANNELS_NUM];
1411713e81bSScott Long 
1421713e81bSScott Long typedef struct st_HPT_DPC {
1431713e81bSScott Long 	IAL_ADAPTER_T *pAdapter;
1441713e81bSScott Long 	void (*dpc)(IAL_ADAPTER_T *, void *, UCHAR);
1451713e81bSScott Long 	void *arg;
1461713e81bSScott Long 	UCHAR flags;
1471713e81bSScott Long } ST_HPT_DPC;
1481713e81bSScott Long 
1491713e81bSScott Long #define MAX_DPC 16
1501713e81bSScott Long UCHAR DPC_Request_Nums = 0;
1511713e81bSScott Long static ST_HPT_DPC DpcQueue[MAX_DPC];
1521713e81bSScott Long static int DpcQueue_First=0;
1531713e81bSScott Long static int DpcQueue_Last = 0;
15449b3fc40SJohn Baldwin static struct mtx DpcQueue_Lock;
15549b3fc40SJohn Baldwin MTX_SYSINIT(hpmtv_dpc_lock, &DpcQueue_Lock, "hptmv dpc", MTX_DEF);
1561713e81bSScott Long 
15764470755SXin LI char DRIVER_VERSION[] = "v1.16";
1581713e81bSScott Long 
1591713e81bSScott Long /*******************************************************************************
1601713e81bSScott Long  *	Name:	hptmv_free_channel
1611713e81bSScott Long  *
1621713e81bSScott Long  *	Description:	free allocated queues for the given channel
1631713e81bSScott Long  *
164453130d9SPedro F. Giffuni  *	Parameters:    	pMvSataAdapter - pointer to the RR18xx controller this
1651713e81bSScott Long  * 					channel connected to.
1661713e81bSScott Long  *			channelNum - channel number.
1671713e81bSScott Long  *
1681713e81bSScott Long  ******************************************************************************/
1691713e81bSScott Long static void
1701713e81bSScott Long hptmv_free_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum)
1711713e81bSScott Long {
1721713e81bSScott Long 	HPT_ASSERT(channelNum < MV_SATA_CHANNELS_NUM);
1731713e81bSScott Long 	pAdapter->mvSataAdapter.sataChannel[channelNum] = NULL;
174d2bd3ab9SScott Long }
1751713e81bSScott Long 
176d2bd3ab9SScott Long static void failDevice(PVDevice pVDev)
1771713e81bSScott Long {
178d2bd3ab9SScott Long 	PVBus _vbus_p = pVDev->pVBus;
179d2bd3ab9SScott Long 	IAL_ADAPTER_T *pAdapter = (IAL_ADAPTER_T *)_vbus_p->OsExt;
180d2bd3ab9SScott Long 
1811713e81bSScott Long 	pVDev->u.disk.df_on_line = 0;
1821713e81bSScott Long 	pVDev->vf_online = 0;
183d2bd3ab9SScott Long 	if (pVDev->pfnDeviceFailed)
184d2bd3ab9SScott Long 		CallWhenIdle(_VBUS_P (DPC_PROC)pVDev->pfnDeviceFailed, pVDev);
185d2bd3ab9SScott Long 
186d2bd3ab9SScott Long 	fNotifyGUI(ET_DEVICE_REMOVED, pVDev);
187d2bd3ab9SScott Long 
188d2bd3ab9SScott Long #ifndef FOR_DEMO
189d2bd3ab9SScott Long 	if (pAdapter->ver_601==2 && !pAdapter->beeping) {
190d2bd3ab9SScott Long 		pAdapter->beeping = 1;
191d2bd3ab9SScott Long 		BeepOn(pAdapter->mvSataAdapter.adapterIoBaseAddress);
192d2bd3ab9SScott Long 		set_fail_led(&pAdapter->mvSataAdapter, pVDev->u.disk.mv->channelNumber, 1);
1931713e81bSScott Long 	}
194d2bd3ab9SScott Long #endif
1951713e81bSScott Long }
1961713e81bSScott Long 
1971713e81bSScott Long int MvSataResetChannel(MV_SATA_ADAPTER *pMvSataAdapter, MV_U8 channel);
1981713e81bSScott Long 
1991713e81bSScott Long static void
2001713e81bSScott Long handleEdmaError(_VBUS_ARG PCommand pCmd)
2011713e81bSScott Long {
2021713e81bSScott Long 	PDevice pDevice = &pCmd->pVDevice->u.disk;
2031713e81bSScott Long 	MV_SATA_ADAPTER * pSataAdapter = pDevice->mv->mvSataAdapter;
2041713e81bSScott Long 
2051713e81bSScott Long 	if (!pDevice->df_on_line) {
2061713e81bSScott Long 		KdPrint(("Device is offline"));
2071713e81bSScott Long 		pCmd->Result = RETURN_BAD_DEVICE;
2081713e81bSScott Long 		CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
2091713e81bSScott Long 		return;
2101713e81bSScott Long 	}
2111713e81bSScott Long 
2121713e81bSScott Long 	if (pCmd->RetryCount++>5) {
213d2bd3ab9SScott Long 		hpt_printk(("too many retries on channel(%d)\n", pDevice->mv->channelNumber));
214d2bd3ab9SScott Long failed:
215d2bd3ab9SScott Long 		failDevice(pCmd->pVDevice);
2161713e81bSScott Long 		pCmd->Result = RETURN_IDE_ERROR;
2171713e81bSScott Long 		CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
2181713e81bSScott Long 		return;
2191713e81bSScott Long 	}
220d2bd3ab9SScott Long 
221d2bd3ab9SScott Long 	/* reset the channel and retry the command */
222d2bd3ab9SScott Long 	if (MvSataResetChannel(pSataAdapter, pDevice->mv->channelNumber))
223d2bd3ab9SScott Long 		goto failed;
224d2bd3ab9SScott Long 
225d2bd3ab9SScott Long 	fNotifyGUI(ET_DEVICE_ERROR, Map2pVDevice(pDevice));
226d2bd3ab9SScott Long 
227d2bd3ab9SScott Long 	hpt_printk(("Retry on channel(%d)\n", pDevice->mv->channelNumber));
2281713e81bSScott Long 	fDeviceSendCommand(_VBUS_P pCmd);
2291713e81bSScott Long }
2301713e81bSScott Long 
2311713e81bSScott Long /****************************************************************
2321713e81bSScott Long  *	Name:	hptmv_init_channel
2331713e81bSScott Long  *
234d2bd3ab9SScott Long  *	Description:	allocate request and response queues for the EDMA of the
235d2bd3ab9SScott Long  *					given channel and sets other fields.
236d2bd3ab9SScott Long  *
2371713e81bSScott Long  *	Parameters:
2381713e81bSScott Long  *		pAdapter - pointer to the emulated adapter data structure
2391713e81bSScott Long  *		channelNum - channel number.
2401713e81bSScott Long  *	Return:	0 on success, otherwise on failure
2411713e81bSScott Long  ****************************************************************/
2421713e81bSScott Long static int
2431713e81bSScott Long hptmv_init_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum)
2441713e81bSScott Long {
2451713e81bSScott Long 	MV_SATA_CHANNEL *pMvSataChannel;
2461713e81bSScott Long 	dma_addr_t    req_dma_addr;
2471713e81bSScott Long 	dma_addr_t    rsp_dma_addr;
2481713e81bSScott Long 
2491713e81bSScott Long 	if (channelNum >= MV_SATA_CHANNELS_NUM)
2501713e81bSScott Long 	{
25164470755SXin LI 		MV_ERROR("RR18xx[%d]: Bad channelNum=%d",
2521713e81bSScott Long 				 pAdapter->mvSataAdapter.adapterId, channelNum);
2531713e81bSScott Long 		return -1;
2541713e81bSScott Long 	}
2551713e81bSScott Long 
256d2bd3ab9SScott Long 	pMvSataChannel = &gMvSataChannels[pAdapter->mvSataAdapter.adapterId][channelNum];
2571713e81bSScott Long 	pAdapter->mvSataAdapter.sataChannel[channelNum] = pMvSataChannel;
2581713e81bSScott Long 	pMvSataChannel->channelNumber = channelNum;
2591713e81bSScott Long 	pMvSataChannel->lba48Address = MV_FALSE;
2601713e81bSScott Long 	pMvSataChannel->maxReadTransfer = MV_FALSE;
2611713e81bSScott Long 
262d2bd3ab9SScott Long 	pMvSataChannel->requestQueue = (struct mvDmaRequestQueueEntry *)
263d2bd3ab9SScott Long 								   (pAdapter->requestsArrayBaseAlignedAddr + (channelNum * MV_EDMA_REQUEST_QUEUE_SIZE));
264d2bd3ab9SScott Long 	req_dma_addr = pAdapter->requestsArrayBaseDmaAlignedAddr + (channelNum * MV_EDMA_REQUEST_QUEUE_SIZE);
2651713e81bSScott Long 
2661713e81bSScott Long 
267d2bd3ab9SScott Long 	KdPrint(("requestQueue addr is 0x%llX", (HPT_U64)(ULONG_PTR)req_dma_addr));
2681713e81bSScott Long 
2691713e81bSScott Long 	/* check the 1K alignment of the request queue*/
2701713e81bSScott Long 	if (req_dma_addr & 0x3ff)
2711713e81bSScott Long 	{
27264470755SXin LI 		MV_ERROR("RR18xx[%d]: request queue allocated isn't 1 K aligned,"
273d2bd3ab9SScott Long 				 " dma_addr=%llx channel=%d\n", pAdapter->mvSataAdapter.adapterId,
274d2bd3ab9SScott Long 				 (HPT_U64)(ULONG_PTR)req_dma_addr, channelNum);
2751713e81bSScott Long 		return -1;
2761713e81bSScott Long 	}
2771713e81bSScott Long 	pMvSataChannel->requestQueuePciLowAddress = req_dma_addr;
2781713e81bSScott Long 	pMvSataChannel->requestQueuePciHiAddress = 0;
27964470755SXin LI 	KdPrint(("RR18xx[%d,%d]: request queue allocated: 0x%p",
2801713e81bSScott Long 			  pAdapter->mvSataAdapter.adapterId, channelNum,
2811713e81bSScott Long 			  pMvSataChannel->requestQueue));
282d2bd3ab9SScott Long 	pMvSataChannel->responseQueue = (struct mvDmaResponseQueueEntry *)
283d2bd3ab9SScott Long 									(pAdapter->responsesArrayBaseAlignedAddr + (channelNum * MV_EDMA_RESPONSE_QUEUE_SIZE));
284d2bd3ab9SScott Long 	rsp_dma_addr = pAdapter->responsesArrayBaseDmaAlignedAddr + (channelNum * MV_EDMA_RESPONSE_QUEUE_SIZE);
2851713e81bSScott Long 
2861713e81bSScott Long 	/* check the 256 alignment of the response queue*/
2871713e81bSScott Long 	if (rsp_dma_addr & 0xff)
2881713e81bSScott Long 	{
28964470755SXin LI 		MV_ERROR("RR18xx[%d,%d]: response queue allocated isn't 256 byte "
290d2bd3ab9SScott Long 				 "aligned, dma_addr=%llx\n",
291d2bd3ab9SScott Long 				 pAdapter->mvSataAdapter.adapterId, channelNum, (HPT_U64)(ULONG_PTR)rsp_dma_addr);
2921713e81bSScott Long 		return -1;
2931713e81bSScott Long 	}
2941713e81bSScott Long 	pMvSataChannel->responseQueuePciLowAddress = rsp_dma_addr;
2951713e81bSScott Long 	pMvSataChannel->responseQueuePciHiAddress = 0;
29664470755SXin LI 	KdPrint(("RR18xx[%d,%d]: response queue allocated: 0x%p",
2971713e81bSScott Long 			  pAdapter->mvSataAdapter.adapterId, channelNum,
2981713e81bSScott Long 			  pMvSataChannel->responseQueue));
2991713e81bSScott Long 
3001713e81bSScott Long 	pAdapter->mvChannel[channelNum].online = MV_TRUE;
3011713e81bSScott Long 	return 0;
3021713e81bSScott Long }
3031713e81bSScott Long 
3041713e81bSScott Long /******************************************************************************
3051713e81bSScott Long  *	Name: hptmv_parse_identify_results
3061713e81bSScott Long  *
307d2bd3ab9SScott Long  *	Description:	this functions parses the identify command results, checks
30864470755SXin LI  *					that the connected deives can be accesed by RR18xx EDMA,
309453130d9SPedro F. Giffuni  *					and updates the channel structure accordingly.
310d2bd3ab9SScott Long  *
3111713e81bSScott Long  *	Parameters:     pMvSataChannel, pointer to the channel data structure.
3121713e81bSScott Long  *
3131713e81bSScott Long  *	Returns:       	=0 ->success, < 0 ->failure.
3141713e81bSScott Long  *
3151713e81bSScott Long  ******************************************************************************/
3161713e81bSScott Long static int
3171713e81bSScott Long hptmv_parse_identify_results(MV_SATA_CHANNEL *pMvSataChannel)
3181713e81bSScott Long {
3191713e81bSScott Long 	MV_U16  *iden = pMvSataChannel->identifyDevice;
3201713e81bSScott Long 
3211713e81bSScott Long 	/*LBA addressing*/
322d2bd3ab9SScott Long 	if (! (iden[IDEN_CAPACITY_1_OFFSET] & 0x200))
323d2bd3ab9SScott Long 	{
3241713e81bSScott Long 		KdPrint(("IAL Error in IDENTIFY info: LBA not supported\n"));
3251713e81bSScott Long 		return -1;
326d2bd3ab9SScott Long 	}
327d2bd3ab9SScott Long 	else
328d2bd3ab9SScott Long 	{
3291713e81bSScott Long 		KdPrint(("%25s - %s\n", "Capabilities", "LBA supported"));
3301713e81bSScott Long 	}
3311713e81bSScott Long 	/*DMA support*/
332d2bd3ab9SScott Long 	if (! (iden[IDEN_CAPACITY_1_OFFSET] & 0x100))
333d2bd3ab9SScott Long 	{
3341713e81bSScott Long 		KdPrint(("IAL Error in IDENTIFY info: DMA not supported\n"));
3351713e81bSScott Long 		return -1;
336d2bd3ab9SScott Long 	}
337d2bd3ab9SScott Long 	else
338d2bd3ab9SScott Long 	{
3391713e81bSScott Long 		KdPrint(("%25s - %s\n", "Capabilities", "DMA supported"));
3401713e81bSScott Long 	}
3411713e81bSScott Long 	/* PIO */
342d2bd3ab9SScott Long 	if ((iden[IDEN_VALID] & 2) == 0)
343d2bd3ab9SScott Long 	{
344d2bd3ab9SScott Long 		KdPrint(("IAL Error in IDENTIFY info: not able to find PIO mode\n"));
3451713e81bSScott Long 		return -1;
3461713e81bSScott Long 	}
3471713e81bSScott Long 	KdPrint(("%25s - 0x%02x\n", "PIO modes supported",
3481713e81bSScott Long 			  iden[IDEN_PIO_MODE_SPPORTED] & 0xff));
3491713e81bSScott Long 
3501713e81bSScott Long 	/*UDMA*/
351d2bd3ab9SScott Long 	if ((iden[IDEN_VALID] & 4) == 0)
352d2bd3ab9SScott Long 	{
353d2bd3ab9SScott Long 		KdPrint(("IAL Error in IDENTIFY info: not able to find UDMA mode\n"));
3541713e81bSScott Long 		return -1;
3551713e81bSScott Long 	}
3561713e81bSScott Long 
3571713e81bSScott Long 	/* 48 bit address */
358d2bd3ab9SScott Long 	if ((iden[IDEN_SUPPORTED_COMMANDS2] & 0x400))
359d2bd3ab9SScott Long 	{
3601713e81bSScott Long 		KdPrint(("%25s - %s\n", "LBA48 addressing", "supported"));
3611713e81bSScott Long 		pMvSataChannel->lba48Address = MV_TRUE;
362d2bd3ab9SScott Long 	}
363d2bd3ab9SScott Long 	else
364d2bd3ab9SScott Long 	{
3651713e81bSScott Long 		KdPrint(("%25s - %s\n", "LBA48 addressing", "Not supported"));
3661713e81bSScott Long 		pMvSataChannel->lba48Address = MV_FALSE;
3671713e81bSScott Long 	}
3681713e81bSScott Long 	return 0;
3691713e81bSScott Long }
3701713e81bSScott Long 
3711713e81bSScott Long static void
3721713e81bSScott Long init_vdev_params(IAL_ADAPTER_T *pAdapter, MV_U8 channel)
3731713e81bSScott Long {
374d2bd3ab9SScott Long 	PVDevice pVDev = &pAdapter->VDevices[channel];
375d2bd3ab9SScott Long 	MV_SATA_CHANNEL *pMvSataChannel = pAdapter->mvSataAdapter.sataChannel[channel];
376d2bd3ab9SScott Long 	MV_U16_PTR IdentifyData = pMvSataChannel->identifyDevice;
3771713e81bSScott Long 
3781713e81bSScott Long 	pMvSataChannel->outstandingCommands = 0;
3791713e81bSScott Long 
3801713e81bSScott Long 	pVDev->u.disk.mv         = pMvSataChannel;
3811713e81bSScott Long 	pVDev->u.disk.df_on_line = 1;
3821713e81bSScott Long 	pVDev->u.disk.pVBus      = &pAdapter->VBus;
3831713e81bSScott Long 	pVDev->pVBus             = &pAdapter->VBus;
3841713e81bSScott Long 
3851713e81bSScott Long #ifdef SUPPORT_48BIT_LBA
3861713e81bSScott Long 	if (pMvSataChannel->lba48Address == MV_TRUE)
387d2bd3ab9SScott Long 		pVDev->u.disk.dDeRealCapacity = ((IdentifyData[101]<<16) | IdentifyData[100]) - 1;
3881713e81bSScott Long 	else
3891713e81bSScott Long #endif
3901713e81bSScott Long 	if(IdentifyData[53] & 1) {
3911713e81bSScott Long 	pVDev->u.disk.dDeRealCapacity =
392d2bd3ab9SScott Long 	  (((IdentifyData[58]<<16 | IdentifyData[57]) < (IdentifyData[61]<<16 | IdentifyData[60])) ?
3931713e81bSScott Long 		  (IdentifyData[61]<<16 | IdentifyData[60]) :
3941713e81bSScott Long 				(IdentifyData[58]<<16 | IdentifyData[57])) - 1;
3951713e81bSScott Long 	} else
3961713e81bSScott Long 		pVDev->u.disk.dDeRealCapacity =
3971713e81bSScott Long 				 (IdentifyData[61]<<16 | IdentifyData[60]) - 1;
3981713e81bSScott Long 
3991713e81bSScott Long 	pVDev->u.disk.bDeUsable_Mode = pVDev->u.disk.bDeModeSetting =
400d2bd3ab9SScott Long 		pAdapter->mvChannel[channel].maxPioModeSupported - MV_ATA_TRANSFER_PIO_0;
4011713e81bSScott Long 
4021713e81bSScott Long 	if (pAdapter->mvChannel[channel].maxUltraDmaModeSupported!=0xFF) {
4031713e81bSScott Long 		pVDev->u.disk.bDeUsable_Mode = pVDev->u.disk.bDeModeSetting =
404d2bd3ab9SScott Long 			pAdapter->mvChannel[channel].maxUltraDmaModeSupported - MV_ATA_TRANSFER_UDMA_0 + 8;
4051713e81bSScott Long 	}
4061713e81bSScott Long }
4071713e81bSScott Long 
408d2bd3ab9SScott Long static void device_change(IAL_ADAPTER_T *pAdapter , MV_U8 channelIndex, int plugged)
4091713e81bSScott Long {
4101713e81bSScott Long 	PVDevice pVDev;
411d2bd3ab9SScott Long 	MV_SATA_ADAPTER  *pMvSataAdapter = &pAdapter->mvSataAdapter;
412d2bd3ab9SScott Long 	MV_SATA_CHANNEL  *pMvSataChannel = pMvSataAdapter->sataChannel[channelIndex];
4131713e81bSScott Long 
414d2bd3ab9SScott Long 	if (!pMvSataChannel) return;
4151713e81bSScott Long 
416d2bd3ab9SScott Long 	if (plugged)
417d2bd3ab9SScott Long 	{
4181713e81bSScott Long 		pVDev = &(pAdapter->VDevices[channelIndex]);
4191713e81bSScott Long 		init_vdev_params(pAdapter, channelIndex);
4201713e81bSScott Long 
4211713e81bSScott Long 		pVDev->VDeviceType = pVDev->u.disk.df_atapi? VD_ATAPI :
422d2bd3ab9SScott Long 			pVDev->u.disk.df_removable_drive? VD_REMOVABLE : VD_SINGLE_DISK;
4231713e81bSScott Long 
424d2bd3ab9SScott Long 		pVDev->VDeviceCapacity = pVDev->u.disk.dDeRealCapacity-SAVE_FOR_RAID_INFO;
4251713e81bSScott Long 		pVDev->pfnSendCommand = pfnSendCommand[pVDev->VDeviceType];
4261713e81bSScott Long 		pVDev->pfnDeviceFailed = pfnDeviceFailed[pVDev->VDeviceType];
4271713e81bSScott Long 		pVDev->vf_online = 1;
4281713e81bSScott Long 
4291713e81bSScott Long #ifdef SUPPORT_ARRAY
430d2bd3ab9SScott Long 		if(pVDev->pParent)
431d2bd3ab9SScott Long 		{
4321713e81bSScott Long 			int iMember;
433d2bd3ab9SScott Long 			for(iMember = 0; iMember < 	pVDev->pParent->u.array.bArnMember; iMember++)
4341713e81bSScott Long 				if((PVDevice)pVDev->pParent->u.array.pMember[iMember] == pVDev)
4351713e81bSScott Long 					pVDev->pParent->u.array.pMember[iMember] = NULL;
4361713e81bSScott Long 			pVDev->pParent = NULL;
4371713e81bSScott Long 		}
4381713e81bSScott Long #endif
4391713e81bSScott Long 		fNotifyGUI(ET_DEVICE_PLUGGED,pVDev);
4401713e81bSScott Long 		fCheckBootable(pVDev);
4411713e81bSScott Long 		RegisterVDevice(pVDev);
4421713e81bSScott Long 
4431713e81bSScott Long #ifndef FOR_DEMO
4441713e81bSScott Long 		if (pAdapter->beeping) {
4451713e81bSScott Long 			pAdapter->beeping = 0;
4461713e81bSScott Long 			BeepOff(pAdapter->mvSataAdapter.adapterIoBaseAddress);
4471713e81bSScott Long 		}
4481713e81bSScott Long #endif
4491713e81bSScott Long 
450d2bd3ab9SScott Long 	}
451d2bd3ab9SScott Long 	else
452d2bd3ab9SScott Long 	{
4531713e81bSScott Long 		pVDev  = &(pAdapter->VDevices[channelIndex]);
454d2bd3ab9SScott Long 		failDevice(pVDev);
4551713e81bSScott Long 	}
4561713e81bSScott Long }
4571713e81bSScott Long 
4581713e81bSScott Long static int
4591713e81bSScott Long start_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum)
4601713e81bSScott Long {
461d2bd3ab9SScott Long 	MV_SATA_ADAPTER *pMvSataAdapter = &pAdapter->mvSataAdapter;
462d2bd3ab9SScott Long 	MV_SATA_CHANNEL *pMvSataChannel = pMvSataAdapter->sataChannel[channelNum];
463d2bd3ab9SScott Long 	MV_CHANNEL		*pChannelInfo = &(pAdapter->mvChannel[channelNum]);
4641713e81bSScott Long 	MV_U32          udmaMode,pioMode;
4651713e81bSScott Long 
46664470755SXin LI 	KdPrint(("RR18xx [%d]: start channel (%d)", pMvSataAdapter->adapterId,
4671713e81bSScott Long 			 channelNum));
4681713e81bSScott Long 
4691713e81bSScott Long 
4701713e81bSScott Long 	/* Software reset channel */
471d2bd3ab9SScott Long 	if (mvStorageDevATASoftResetDevice(pMvSataAdapter, channelNum) == MV_FALSE)
472d2bd3ab9SScott Long 	{
47364470755SXin LI 		MV_ERROR("RR18xx [%d,%d]: failed to perform Software reset\n",
4741713e81bSScott Long 				 pMvSataAdapter->adapterId, channelNum);
4751713e81bSScott Long 		return -1;
4761713e81bSScott Long 	}
4771713e81bSScott Long 
4781713e81bSScott Long 	/* Hardware reset channel */
479d2bd3ab9SScott Long 	if (mvSataChannelHardReset(pMvSataAdapter, channelNum) == MV_FALSE)
480d2bd3ab9SScott Long 	{
481d2bd3ab9SScott Long 		/* If failed, try again - this is when trying to hardreset a channel */
482d2bd3ab9SScott Long 		/* when drive is just spinning up */
4831713e81bSScott Long 		StallExec(5000000); /* wait 5 sec before trying again */
484d2bd3ab9SScott Long 		if (mvSataChannelHardReset(pMvSataAdapter, channelNum) == MV_FALSE)
485d2bd3ab9SScott Long 		{
48664470755SXin LI 			MV_ERROR("RR18xx [%d,%d]: failed to perform Hard reset\n",
4871713e81bSScott Long 					 pMvSataAdapter->adapterId, channelNum);
4881713e81bSScott Long 			return -1;
4891713e81bSScott Long 		}
4901713e81bSScott Long 	}
4911713e81bSScott Long 
492d2bd3ab9SScott Long 	/* identify device*/
493d2bd3ab9SScott Long 	if (mvStorageDevATAIdentifyDevice(pMvSataAdapter, channelNum) == MV_FALSE)
494d2bd3ab9SScott Long 	{
49564470755SXin LI 		MV_ERROR("RR18xx [%d,%d]: failed to perform ATA Identify command\n"
496d2bd3ab9SScott Long 				 , pMvSataAdapter->adapterId, channelNum);
497d2bd3ab9SScott Long 		return -1;
498d2bd3ab9SScott Long 	}
499d2bd3ab9SScott Long 	if (hptmv_parse_identify_results(pMvSataChannel))
500d2bd3ab9SScott Long 	{
50164470755SXin LI 		MV_ERROR("RR18xx [%d,%d]: Error in parsing ATA Identify message\n"
502d2bd3ab9SScott Long 				 , pMvSataAdapter->adapterId, channelNum);
503d2bd3ab9SScott Long 		return -1;
504d2bd3ab9SScott Long 	}
505d2bd3ab9SScott Long 
506d2bd3ab9SScott Long 	/* mvStorageDevATASetFeatures */
507d2bd3ab9SScott Long 	/* Disable 8 bit PIO in case CFA enabled */
508d2bd3ab9SScott Long 	if (pMvSataChannel->identifyDevice[86] & 4)
509d2bd3ab9SScott Long 	{
51064470755SXin LI 		KdPrint(("RR18xx [%d]: Disable 8 bit PIO (CFA enabled) \n",
511d2bd3ab9SScott Long 				  pMvSataAdapter->adapterId));
512d2bd3ab9SScott Long 		if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
513d2bd3ab9SScott Long 									   MV_ATA_SET_FEATURES_DISABLE_8_BIT_PIO, 0,
514d2bd3ab9SScott Long 									   0, 0, 0) == MV_FALSE)
515d2bd3ab9SScott Long 		{
51664470755SXin LI 			MV_ERROR("RR18xx [%d]: channel %d: mvStorageDevATASetFeatures"
517d2bd3ab9SScott Long 					 " failed\n", pMvSataAdapter->adapterId, channelNum);
518d2bd3ab9SScott Long 			return -1;
519d2bd3ab9SScott Long 		}
520d2bd3ab9SScott Long 	}
5211713e81bSScott Long 	/* Write cache */
522d2bd3ab9SScott Long #ifdef ENABLE_WRITE_CACHE
523d2bd3ab9SScott Long 	if (pMvSataChannel->identifyDevice[82] & 0x20)
524d2bd3ab9SScott Long 	{
525d2bd3ab9SScott Long 		if (!(pMvSataChannel->identifyDevice[85] & 0x20)) /* if not enabled by default */
526d2bd3ab9SScott Long 		{
527d2bd3ab9SScott Long 			if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
528d2bd3ab9SScott Long 										   MV_ATA_SET_FEATURES_ENABLE_WCACHE, 0,
529d2bd3ab9SScott Long 										   0, 0, 0) == MV_FALSE)
530d2bd3ab9SScott Long 			{
53164470755SXin LI 				MV_ERROR("RR18xx [%d]: channel %d: mvStorageDevATASetFeatures failed\n",
5321713e81bSScott Long 						 pMvSataAdapter->adapterId, channelNum);
5331713e81bSScott Long 				return -1;
5341713e81bSScott Long 			}
5351713e81bSScott Long 		}
53664470755SXin LI 		KdPrint(("RR18xx [%d]: channel %d, write cache enabled\n",
5371713e81bSScott Long 				  pMvSataAdapter->adapterId, channelNum));
538d2bd3ab9SScott Long 	}
539d2bd3ab9SScott Long 	else
540d2bd3ab9SScott Long 	{
54164470755SXin LI 		KdPrint(("RR18xx [%d]: channel %d, write cache not supported\n",
5421713e81bSScott Long 				  pMvSataAdapter->adapterId, channelNum));
5431713e81bSScott Long 	}
544d2bd3ab9SScott Long #else /* disable write cache */
545d2bd3ab9SScott Long 	{
546d2bd3ab9SScott Long 		if (pMvSataChannel->identifyDevice[85] & 0x20)
547d2bd3ab9SScott Long 		{
54864470755SXin LI 			KdPrint(("RR18xx [%d]: channel =%d, disable write cache\n",
5491713e81bSScott Long 					  pMvSataAdapter->adapterId, channelNum));
5501713e81bSScott Long 			if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
551d2bd3ab9SScott Long 										   MV_ATA_SET_FEATURES_DISABLE_WCACHE, 0,
552d2bd3ab9SScott Long 										   0, 0, 0) == MV_FALSE)
553d2bd3ab9SScott Long 			{
55464470755SXin LI 				MV_ERROR("RR18xx [%d]: channel %d: mvStorageDevATASetFeatures failed\n",
5551713e81bSScott Long 						 pMvSataAdapter->adapterId, channelNum);
5561713e81bSScott Long 				return -1;
5571713e81bSScott Long 			}
5581713e81bSScott Long 		}
55964470755SXin LI 		KdPrint(("RR18xx [%d]: channel=%d, write cache disabled\n",
5601713e81bSScott Long 				  pMvSataAdapter->adapterId, channelNum));
561d2bd3ab9SScott Long 	}
5621713e81bSScott Long #endif
5631713e81bSScott Long 
5641713e81bSScott Long 	/* Set transfer mode */
56564470755SXin LI 	KdPrint(("RR18xx [%d] Set transfer mode XFER_PIO_SLOW\n",
5661713e81bSScott Long 			  pMvSataAdapter->adapterId));
5671713e81bSScott Long 	if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
568d2bd3ab9SScott Long 								   MV_ATA_SET_FEATURES_TRANSFER,
569d2bd3ab9SScott Long 								   MV_ATA_TRANSFER_PIO_SLOW, 0, 0, 0) ==
570d2bd3ab9SScott Long 		MV_FALSE)
571d2bd3ab9SScott Long 	{
57264470755SXin LI 		MV_ERROR("RR18xx [%d] channel %d: Set Features failed\n",
5731713e81bSScott Long 				 pMvSataAdapter->adapterId, channelNum);
5741713e81bSScott Long 		return -1;
5751713e81bSScott Long 	}
5761713e81bSScott Long 
577d2bd3ab9SScott Long 	if (pMvSataChannel->identifyDevice[IDEN_PIO_MODE_SPPORTED] & 1)
578d2bd3ab9SScott Long 	{
5791713e81bSScott Long 		pioMode = MV_ATA_TRANSFER_PIO_4;
580d2bd3ab9SScott Long 	}
581d2bd3ab9SScott Long 	else if (pMvSataChannel->identifyDevice[IDEN_PIO_MODE_SPPORTED] & 2)
582d2bd3ab9SScott Long 	{
5831713e81bSScott Long 		pioMode = MV_ATA_TRANSFER_PIO_3;
584d2bd3ab9SScott Long 	}
585d2bd3ab9SScott Long 	else
586d2bd3ab9SScott Long 	{
587d2bd3ab9SScott Long 		MV_ERROR("IAL Error in IDENTIFY info: PIO modes 3 and 4 not supported\n");
5881713e81bSScott Long 		pioMode = MV_ATA_TRANSFER_PIO_SLOW;
5891713e81bSScott Long 	}
5901713e81bSScott Long 
59164470755SXin LI 	KdPrint(("RR18xx [%d] Set transfer mode XFER_PIO_4\n",
5921713e81bSScott Long 			  pMvSataAdapter->adapterId));
5931713e81bSScott Long 	pAdapter->mvChannel[channelNum].maxPioModeSupported = pioMode;
5941713e81bSScott Long 	if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
595d2bd3ab9SScott Long 								   MV_ATA_SET_FEATURES_TRANSFER,
596d2bd3ab9SScott Long 								   pioMode, 0, 0, 0) == MV_FALSE)
597d2bd3ab9SScott Long 	{
59864470755SXin LI 		MV_ERROR("RR18xx [%d] channel %d: Set Features failed\n",
5991713e81bSScott Long 				 pMvSataAdapter->adapterId, channelNum);
6001713e81bSScott Long 		return -1;
6011713e81bSScott Long 	}
6021713e81bSScott Long 
6031713e81bSScott Long 	udmaMode = MV_ATA_TRANSFER_UDMA_0;
604d2bd3ab9SScott Long 	if (pMvSataChannel->identifyDevice[IDEN_UDMA_MODE] & 0x40)
605d2bd3ab9SScott Long 	{
6061713e81bSScott Long 		udmaMode =  MV_ATA_TRANSFER_UDMA_6;
607d2bd3ab9SScott Long 	}
608d2bd3ab9SScott Long 	else if (pMvSataChannel->identifyDevice[IDEN_UDMA_MODE] & 0x20)
609d2bd3ab9SScott Long 	{
6101713e81bSScott Long 		udmaMode =  MV_ATA_TRANSFER_UDMA_5;
611d2bd3ab9SScott Long 	}
612d2bd3ab9SScott Long 	else if (pMvSataChannel->identifyDevice[IDEN_UDMA_MODE] & 0x10)
613d2bd3ab9SScott Long 	{
6141713e81bSScott Long 		udmaMode =  MV_ATA_TRANSFER_UDMA_4;
615d2bd3ab9SScott Long 	}
616d2bd3ab9SScott Long 	else if (pMvSataChannel->identifyDevice[IDEN_UDMA_MODE] & 8)
617d2bd3ab9SScott Long 	{
6181713e81bSScott Long 		udmaMode =  MV_ATA_TRANSFER_UDMA_3;
619d2bd3ab9SScott Long 	}
620d2bd3ab9SScott Long 	else if (pMvSataChannel->identifyDevice[IDEN_UDMA_MODE] & 4)
621d2bd3ab9SScott Long 	{
6221713e81bSScott Long 		udmaMode =  MV_ATA_TRANSFER_UDMA_2;
6231713e81bSScott Long 	}
6241713e81bSScott Long 
62564470755SXin LI 	KdPrint(("RR18xx [%d] Set transfer mode XFER_UDMA_%d\n",
6261713e81bSScott Long 			  pMvSataAdapter->adapterId, udmaMode & 0xf));
6271713e81bSScott Long 	pChannelInfo->maxUltraDmaModeSupported = udmaMode;
6281713e81bSScott Long 
629d2bd3ab9SScott Long 	/*if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
630d2bd3ab9SScott Long 								   MV_ATA_SET_FEATURES_TRANSFER, udmaMode,
631d2bd3ab9SScott Long 								   0, 0, 0) == MV_FALSE)
632d2bd3ab9SScott Long 	{
63364470755SXin LI 		MV_ERROR("RR18xx [%d] channel %d: Set Features failed\n",
6341713e81bSScott Long 				 pMvSataAdapter->adapterId, channelNum);
6351713e81bSScott Long 		return -1;
636d2bd3ab9SScott Long 	}*/
6371713e81bSScott Long 	if (pChannelInfo->maxUltraDmaModeSupported == 0xFF)
6381713e81bSScott Long 		return TRUE;
639d2bd3ab9SScott Long 	else
640d2bd3ab9SScott Long 		do
641d2bd3ab9SScott Long 		{
6421713e81bSScott Long 			if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
6431713e81bSScott Long 								   MV_ATA_SET_FEATURES_TRANSFER,
644d2bd3ab9SScott Long 								   pChannelInfo->maxUltraDmaModeSupported,
645d2bd3ab9SScott Long 								   0, 0, 0) == MV_FALSE)
646d2bd3ab9SScott Long 			{
647d2bd3ab9SScott Long 				if (pChannelInfo->maxUltraDmaModeSupported > MV_ATA_TRANSFER_UDMA_0)
648d2bd3ab9SScott Long 				{
649d2bd3ab9SScott Long 					if (mvStorageDevATASoftResetDevice(pMvSataAdapter, channelNum) == MV_FALSE)
650d2bd3ab9SScott Long 					{
651d2bd3ab9SScott Long 						MV_REG_WRITE_BYTE(pMvSataAdapter->adapterIoBaseAddress,
652d2bd3ab9SScott Long 										  pMvSataChannel->eDmaRegsOffset +
653d2bd3ab9SScott Long 										  0x11c, /* command reg */
654d2bd3ab9SScott Long 										  MV_ATA_COMMAND_IDLE_IMMEDIATE);
6551713e81bSScott Long 						mvMicroSecondsDelay(10000);
6561713e81bSScott Long 						mvSataChannelHardReset(pMvSataAdapter, channelNum);
657d2bd3ab9SScott Long 						if (mvStorageDevATASoftResetDevice(pMvSataAdapter, channelNum) == MV_FALSE)
6581713e81bSScott Long 							return FALSE;
6591713e81bSScott Long 					}
660d2bd3ab9SScott Long 					if (mvSataChannelHardReset(pMvSataAdapter, channelNum) == MV_FALSE)
6611713e81bSScott Long 						return FALSE;
6621713e81bSScott Long 					pChannelInfo->maxUltraDmaModeSupported--;
663d2bd3ab9SScott Long 					continue;
664d2bd3ab9SScott Long 				}
665d2bd3ab9SScott Long 				else   return FALSE;
666d2bd3ab9SScott Long 			}
667d2bd3ab9SScott Long 			break;
6681713e81bSScott Long 		}while (1);
6691713e81bSScott Long 
6701713e81bSScott Long 	/* Read look ahead */
671d2bd3ab9SScott Long #ifdef ENABLE_READ_AHEAD
672d2bd3ab9SScott Long 	if (pMvSataChannel->identifyDevice[82] & 0x40)
673d2bd3ab9SScott Long 	{
674d2bd3ab9SScott Long 		if (!(pMvSataChannel->identifyDevice[85] & 0x40)) /* if not enabled by default */
675d2bd3ab9SScott Long 		{
676d2bd3ab9SScott Long 			if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
677d2bd3ab9SScott Long 										   MV_ATA_SET_FEATURES_ENABLE_RLA, 0, 0,
678d2bd3ab9SScott Long 										   0, 0) == MV_FALSE)
679d2bd3ab9SScott Long 			{
68064470755SXin LI 				MV_ERROR("RR18xx [%d] channel %d: Set Features failed\n",
681d2bd3ab9SScott Long 						 pMvSataAdapter->adapterId, channelNum);
6821713e81bSScott Long 				return -1;
6831713e81bSScott Long 			}
6841713e81bSScott Long 		}
68564470755SXin LI 		KdPrint(("RR18xx [%d]: channel=%d, read look ahead enabled\n",
6861713e81bSScott Long 				  pMvSataAdapter->adapterId, channelNum));
687d2bd3ab9SScott Long 	}
688d2bd3ab9SScott Long 	else
689d2bd3ab9SScott Long 	{
69064470755SXin LI 		KdPrint(("RR18xx [%d]: channel %d, Read Look Ahead not supported\n",
691d2bd3ab9SScott Long 				  pMvSataAdapter->adapterId, channelNum));
6921713e81bSScott Long 	}
6931713e81bSScott Long #else
694d2bd3ab9SScott Long 	{
695d2bd3ab9SScott Long 		if (pMvSataChannel->identifyDevice[86] & 0x20)
696d2bd3ab9SScott Long 		{
69764470755SXin LI 			KdPrint(("RR18xx [%d]:channel %d, disable read look ahead\n",
6981713e81bSScott Long 					  pMvSataAdapter->adapterId, channelNum));
6991713e81bSScott Long 			if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
700d2bd3ab9SScott Long 										   MV_ATA_SET_FEATURES_DISABLE_RLA, 0, 0,
701d2bd3ab9SScott Long 										   0, 0) == MV_FALSE)
702d2bd3ab9SScott Long 			{
70364470755SXin LI 				MV_ERROR("RR18xx [%d]:channel %d:  ATA Set Features failed\n",
704d2bd3ab9SScott Long 						 pMvSataAdapter->adapterId, channelNum);
7051713e81bSScott Long 				return -1;
7061713e81bSScott Long 			}
7071713e81bSScott Long 		}
70864470755SXin LI 		KdPrint(("RR18xx [%d]:channel %d, read look ahead disabled\n",
7091713e81bSScott Long 				  pMvSataAdapter->adapterId, channelNum));
710d2bd3ab9SScott Long 	}
7111713e81bSScott Long #endif
7121713e81bSScott Long 
713d2bd3ab9SScott Long 
714d2bd3ab9SScott Long 	{
71564470755SXin LI 		KdPrint(("RR18xx [%d]: channel %d config EDMA, Non Queued Mode\n",
716d2bd3ab9SScott Long 				  pMvSataAdapter->adapterId,
717d2bd3ab9SScott Long 				  channelNum));
718d2bd3ab9SScott Long 		if (mvSataConfigEdmaMode(pMvSataAdapter, channelNum,
719d2bd3ab9SScott Long 								 MV_EDMA_MODE_NOT_QUEUED, 0) == MV_FALSE)
720d2bd3ab9SScott Long 		{
72164470755SXin LI 			MV_ERROR("RR18xx [%d] channel %d Error: mvSataConfigEdmaMode failed\n",
7221713e81bSScott Long 					 pMvSataAdapter->adapterId, channelNum);
7231713e81bSScott Long 			return -1;
7241713e81bSScott Long 		}
7251713e81bSScott Long 	}
7261713e81bSScott Long 	/* Enable EDMA */
727d2bd3ab9SScott Long 	if (mvSataEnableChannelDma(pMvSataAdapter, channelNum) == MV_FALSE)
728d2bd3ab9SScott Long 	{
72964470755SXin LI 		MV_ERROR("RR18xx [%d] Failed to enable DMA, channel=%d\n",
7301713e81bSScott Long 				 pMvSataAdapter->adapterId, channelNum);
7311713e81bSScott Long 		return -1;
7321713e81bSScott Long 	}
73364470755SXin LI 	MV_ERROR("RR18xx [%d,%d]: channel started successfully\n",
7341713e81bSScott Long 			 pMvSataAdapter->adapterId, channelNum);
7351713e81bSScott Long 
7361713e81bSScott Long #ifndef FOR_DEMO
7371713e81bSScott Long 	set_fail_led(pMvSataAdapter, channelNum, 0);
7381713e81bSScott Long #endif
7391713e81bSScott Long 	return 0;
7401713e81bSScott Long }
7411713e81bSScott Long 
7421713e81bSScott Long static void
7431713e81bSScott Long hptmv_handle_event(void * data, int flag)
7441713e81bSScott Long {
745d2bd3ab9SScott Long 	IAL_ADAPTER_T   *pAdapter = (IAL_ADAPTER_T *)data;
746d2bd3ab9SScott Long 	MV_SATA_ADAPTER *pMvSataAdapter = &pAdapter->mvSataAdapter;
7471713e81bSScott Long 	MV_U8           channelIndex;
7481713e81bSScott Long 
74949b3fc40SJohn Baldwin 	mtx_assert(&pAdapter->lock, MA_OWNED);
750d2bd3ab9SScott Long /*	mvOsSemTake(&pMvSataAdapter->semaphore); */
751d2bd3ab9SScott Long 	for (channelIndex = 0; channelIndex < MV_SATA_CHANNELS_NUM; channelIndex++)
752d2bd3ab9SScott Long 	{
753d2bd3ab9SScott Long 		switch(pAdapter->sataEvents[channelIndex])
754d2bd3ab9SScott Long 		{
7551713e81bSScott Long 			case SATA_EVENT_CHANNEL_CONNECTED:
7561713e81bSScott Long 				/* Handle only connects */
7571713e81bSScott Long 				if (flag == 1)
7581713e81bSScott Long 					break;
75964470755SXin LI 				KdPrint(("RR18xx [%d,%d]: new device connected\n",
7601713e81bSScott Long 						 pMvSataAdapter->adapterId, channelIndex));
7611713e81bSScott Long 				hptmv_init_channel(pAdapter, channelIndex);
762d2bd3ab9SScott Long 				if (mvSataConfigureChannel( pMvSataAdapter, channelIndex) == MV_FALSE)
763d2bd3ab9SScott Long 				{
76464470755SXin LI 					MV_ERROR("RR18xx [%d,%d] Failed to configure\n",
765d2bd3ab9SScott Long 							 pMvSataAdapter->adapterId, channelIndex);
7661713e81bSScott Long 					hptmv_free_channel(pAdapter, channelIndex);
767d2bd3ab9SScott Long 				}
768d2bd3ab9SScott Long 				else
769d2bd3ab9SScott Long 				{
770d2bd3ab9SScott Long 					/*mvSataChannelHardReset(pMvSataAdapter, channel);*/
771d2bd3ab9SScott Long 					if (start_channel( pAdapter, channelIndex))
772d2bd3ab9SScott Long 					{
77364470755SXin LI 						MV_ERROR("RR18xx [%d,%d]Failed to start channel\n",
774d2bd3ab9SScott Long 								 pMvSataAdapter->adapterId, channelIndex);
775d2bd3ab9SScott Long 						hptmv_free_channel(pAdapter, channelIndex);
776d2bd3ab9SScott Long 					}
777d2bd3ab9SScott Long 					else
778d2bd3ab9SScott Long 					{
779d2bd3ab9SScott Long 						device_change(pAdapter, channelIndex, TRUE);
7801713e81bSScott Long 					}
7811713e81bSScott Long 				}
782d2bd3ab9SScott Long 				pAdapter->sataEvents[channelIndex] = SATA_EVENT_NO_CHANGE;
7831713e81bSScott Long 			   break;
7841713e81bSScott Long 
7851713e81bSScott Long 			case SATA_EVENT_CHANNEL_DISCONNECTED:
7861713e81bSScott Long 				/* Handle only disconnects */
7871713e81bSScott Long 				if (flag == 0)
7881713e81bSScott Long 					break;
78964470755SXin LI 				KdPrint(("RR18xx [%d,%d]: device disconnected\n",
7901713e81bSScott Long 						 pMvSataAdapter->adapterId, channelIndex));
7911713e81bSScott Long 					/* Flush pending commands */
792d2bd3ab9SScott Long 				if(pMvSataAdapter->sataChannel[channelIndex])
793d2bd3ab9SScott Long 				{
7941713e81bSScott Long 					_VBUS_INST(&pAdapter->VBus)
795d2bd3ab9SScott Long 					mvSataFlushDmaQueue (pMvSataAdapter, channelIndex,
796d2bd3ab9SScott Long 										 MV_FLUSH_TYPE_CALLBACK);
7971713e81bSScott Long 					CheckPendingCall(_VBUS_P0);
798d2bd3ab9SScott Long 					mvSataRemoveChannel(pMvSataAdapter,channelIndex);
7991713e81bSScott Long 					hptmv_free_channel(pAdapter, channelIndex);
800d2bd3ab9SScott Long 					pMvSataAdapter->sataChannel[channelIndex] = NULL;
80164470755SXin LI 					KdPrint(("RR18xx [%d,%d]: channel removed\n",
802d2bd3ab9SScott Long 						 pMvSataAdapter->adapterId, channelIndex));
803d2bd3ab9SScott Long 					if (pAdapter->outstandingCommands==0 && DPC_Request_Nums==0)
8041713e81bSScott Long 						Check_Idle_Call(pAdapter);
8051713e81bSScott Long 				}
806d2bd3ab9SScott Long 				else
807d2bd3ab9SScott Long 				{
80864470755SXin LI 					KdPrint(("RR18xx [%d,%d]: channel already removed!!\n",
809d2bd3ab9SScott Long 							 pMvSataAdapter->adapterId, channelIndex));
810d2bd3ab9SScott Long 				}
811d2bd3ab9SScott Long 				pAdapter->sataEvents[channelIndex] = SATA_EVENT_NO_CHANGE;
8121713e81bSScott Long 				break;
8131713e81bSScott Long 
8141713e81bSScott Long 			case SATA_EVENT_NO_CHANGE:
8151713e81bSScott Long 				break;
8161713e81bSScott Long 
8171713e81bSScott Long 			default:
8181713e81bSScott Long 				break;
8191713e81bSScott Long 		}
8201713e81bSScott Long 	}
821d2bd3ab9SScott Long /*	mvOsSemRelease(&pMvSataAdapter->semaphore); */
8221713e81bSScott Long }
8231713e81bSScott Long 
8241713e81bSScott Long #define EVENT_CONNECT					1
8251713e81bSScott Long #define EVENT_DISCONNECT				0
8261713e81bSScott Long 
8271713e81bSScott Long static void
8281713e81bSScott Long hptmv_handle_event_connect(void *data)
8291713e81bSScott Long {
8301713e81bSScott Long   hptmv_handle_event (data, 0);
8311713e81bSScott Long }
8321713e81bSScott Long 
8331713e81bSScott Long static void
8341713e81bSScott Long hptmv_handle_event_disconnect(void *data)
8351713e81bSScott Long {
8361713e81bSScott Long   hptmv_handle_event (data, 1);
8371713e81bSScott Long }
8381713e81bSScott Long 
8391713e81bSScott Long static MV_BOOLEAN
8401713e81bSScott Long hptmv_event_notify(MV_SATA_ADAPTER *pMvSataAdapter, MV_EVENT_TYPE eventType,
8411713e81bSScott Long 								   MV_U32 param1, MV_U32 param2)
8421713e81bSScott Long {
8431713e81bSScott Long 	IAL_ADAPTER_T   *pAdapter = pMvSataAdapter->IALData;
8441713e81bSScott Long 
845d2bd3ab9SScott Long 	switch (eventType)
846d2bd3ab9SScott Long 	{
8471713e81bSScott Long 		case MV_EVENT_TYPE_SATA_CABLE:
8481713e81bSScott Long 			{
8491713e81bSScott Long 				MV_U8   channel = param2;
8501713e81bSScott Long 
851d2bd3ab9SScott Long 				if (param1 == EVENT_CONNECT)
852d2bd3ab9SScott Long 				{
853d2bd3ab9SScott Long 					pAdapter->sataEvents[channel] = SATA_EVENT_CHANNEL_CONNECTED;
85464470755SXin LI 					KdPrint(("RR18xx [%d,%d]: device connected event received\n",
855d2bd3ab9SScott Long 							 pMvSataAdapter->adapterId, channel));
856d2bd3ab9SScott Long 					/* Delete previous timers (if multiple drives connected in the same time */
85749b3fc40SJohn Baldwin 					callout_reset(&pAdapter->event_timer_connect, 10 * hz, hptmv_handle_event_connect, pAdapter);
858d2bd3ab9SScott Long 				}
859d2bd3ab9SScott Long 				else if (param1 == EVENT_DISCONNECT)
860d2bd3ab9SScott Long 				{
861d2bd3ab9SScott Long 					pAdapter->sataEvents[channel] = SATA_EVENT_CHANNEL_DISCONNECTED;
86264470755SXin LI 					KdPrint(("RR18xx [%d,%d]: device disconnected event received \n",
863d2bd3ab9SScott Long 							 pMvSataAdapter->adapterId, channel));
8641713e81bSScott Long 					device_change(pAdapter, channel, FALSE);
865d2bd3ab9SScott Long 					/* Delete previous timers (if multiple drives disconnected in the same time */
86649b3fc40SJohn Baldwin 					/*callout_reset(&pAdapter->event_timer_disconnect, 10 * hz, hptmv_handle_event_disconnect, pAdapter); */
867d2bd3ab9SScott Long 					/*It is not necessary to wait, handle it directly*/
868d2bd3ab9SScott Long 					hptmv_handle_event_disconnect(pAdapter);
869d2bd3ab9SScott Long 				}
870d2bd3ab9SScott Long 				else
871d2bd3ab9SScott Long 				{
872d2bd3ab9SScott Long 
8737a2b450fSEitan Adler 					MV_ERROR("RR18xx: illegal value for param1(%d) at "
8747a2b450fSEitan Adler 							 "connect/disconnect event, host=%d\n", param1,
8751713e81bSScott Long 							 pMvSataAdapter->adapterId );
8761713e81bSScott Long 
8771713e81bSScott Long 				}
8781713e81bSScott Long 			}
879d2bd3ab9SScott Long 			break;
8801713e81bSScott Long 		case MV_EVENT_TYPE_ADAPTER_ERROR:
88164470755SXin LI 			KdPrint(("RR18xx: DEVICE error event received, pci cause "
8821713e81bSScott Long 					  "reg=%x,  don't how to handle this\n", param1));
8831713e81bSScott Long 			return MV_TRUE;
8841713e81bSScott Long 		default:
88564470755SXin LI 			MV_ERROR("RR18xx[%d]: unknown event type (%d)\n",
8861713e81bSScott Long 					 pMvSataAdapter->adapterId, eventType);
8871713e81bSScott Long 			return MV_FALSE;
8881713e81bSScott Long 	}
8891713e81bSScott Long 	return MV_TRUE;
8901713e81bSScott Long }
8911713e81bSScott Long 
8921713e81bSScott Long static int
8931713e81bSScott Long hptmv_allocate_edma_queues(IAL_ADAPTER_T *pAdapter)
8941713e81bSScott Long {
895d2bd3ab9SScott Long 	pAdapter->requestsArrayBaseAddr = (MV_U8 *)contigmalloc(REQUESTS_ARRAY_SIZE,
896d2bd3ab9SScott Long 			M_DEVBUF, M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0ul);
897d2bd3ab9SScott Long 	if (pAdapter->requestsArrayBaseAddr == NULL)
898d2bd3ab9SScott Long 	{
89964470755SXin LI 		MV_ERROR("RR18xx[%d]: Failed to allocate memory for EDMA request"
900d2bd3ab9SScott Long 				 " queues\n", pAdapter->mvSataAdapter.adapterId);
9011713e81bSScott Long 		return -1;
9021713e81bSScott Long 	}
903d2bd3ab9SScott Long 	pAdapter->requestsArrayBaseDmaAddr = fOsPhysicalAddress(pAdapter->requestsArrayBaseAddr);
904d2bd3ab9SScott Long 	pAdapter->requestsArrayBaseAlignedAddr = pAdapter->requestsArrayBaseAddr;
9051713e81bSScott Long 	pAdapter->requestsArrayBaseAlignedAddr += MV_EDMA_REQUEST_QUEUE_SIZE;
906d2bd3ab9SScott Long 	pAdapter->requestsArrayBaseAlignedAddr  = (MV_U8 *)
907d2bd3ab9SScott Long 		(((ULONG_PTR)pAdapter->requestsArrayBaseAlignedAddr) & ~(ULONG_PTR)(MV_EDMA_REQUEST_QUEUE_SIZE - 1));
908d2bd3ab9SScott Long 	pAdapter->requestsArrayBaseDmaAlignedAddr = pAdapter->requestsArrayBaseDmaAddr;
9091713e81bSScott Long 	pAdapter->requestsArrayBaseDmaAlignedAddr += MV_EDMA_REQUEST_QUEUE_SIZE;
910d2bd3ab9SScott Long 	pAdapter->requestsArrayBaseDmaAlignedAddr &= ~(ULONG_PTR)(MV_EDMA_REQUEST_QUEUE_SIZE - 1);
9111713e81bSScott Long 
912d2bd3ab9SScott Long 	if ((pAdapter->requestsArrayBaseDmaAlignedAddr - pAdapter->requestsArrayBaseDmaAddr) !=
913d2bd3ab9SScott Long 		(pAdapter->requestsArrayBaseAlignedAddr - pAdapter->requestsArrayBaseAddr))
914d2bd3ab9SScott Long 	{
91564470755SXin LI 		MV_ERROR("RR18xx[%d]: Error in Request Quueues Alignment\n",
9161713e81bSScott Long 				 pAdapter->mvSataAdapter.adapterId);
917*d1bdc282SBjoern A. Zeeb 		free(pAdapter->requestsArrayBaseAddr, M_DEVBUF);
9181713e81bSScott Long 		return -1;
9191713e81bSScott Long 	}
9201713e81bSScott Long 	/* response queues */
921d2bd3ab9SScott Long 	pAdapter->responsesArrayBaseAddr = (MV_U8 *)contigmalloc(RESPONSES_ARRAY_SIZE,
922d2bd3ab9SScott Long 			M_DEVBUF, M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0ul);
923d2bd3ab9SScott Long 	if (pAdapter->responsesArrayBaseAddr == NULL)
924d2bd3ab9SScott Long 	{
92564470755SXin LI 		MV_ERROR("RR18xx[%d]: Failed to allocate memory for EDMA response"
926d2bd3ab9SScott Long 				 " queues\n", pAdapter->mvSataAdapter.adapterId);
927*d1bdc282SBjoern A. Zeeb 		free(pAdapter->requestsArrayBaseAddr, M_DEVBUF);
9281713e81bSScott Long 		return -1;
9291713e81bSScott Long 	}
930d2bd3ab9SScott Long 	pAdapter->responsesArrayBaseDmaAddr = fOsPhysicalAddress(pAdapter->responsesArrayBaseAddr);
931d2bd3ab9SScott Long 	pAdapter->responsesArrayBaseAlignedAddr = pAdapter->responsesArrayBaseAddr;
9321713e81bSScott Long 	pAdapter->responsesArrayBaseAlignedAddr += MV_EDMA_RESPONSE_QUEUE_SIZE;
933d2bd3ab9SScott Long 	pAdapter->responsesArrayBaseAlignedAddr  = (MV_U8 *)
934d2bd3ab9SScott Long 		(((ULONG_PTR)pAdapter->responsesArrayBaseAlignedAddr) & ~(ULONG_PTR)(MV_EDMA_RESPONSE_QUEUE_SIZE - 1));
935d2bd3ab9SScott Long 	pAdapter->responsesArrayBaseDmaAlignedAddr = pAdapter->responsesArrayBaseDmaAddr;
936d2bd3ab9SScott Long 	pAdapter->responsesArrayBaseDmaAlignedAddr += MV_EDMA_RESPONSE_QUEUE_SIZE;
937d2bd3ab9SScott Long 	pAdapter->responsesArrayBaseDmaAlignedAddr &= ~(ULONG_PTR)(MV_EDMA_RESPONSE_QUEUE_SIZE - 1);
9381713e81bSScott Long 
939d2bd3ab9SScott Long 	if ((pAdapter->responsesArrayBaseDmaAlignedAddr - pAdapter->responsesArrayBaseDmaAddr) !=
940d2bd3ab9SScott Long 		(pAdapter->responsesArrayBaseAlignedAddr - pAdapter->responsesArrayBaseAddr))
941d2bd3ab9SScott Long 	{
9427a2b450fSEitan Adler 		MV_ERROR("RR18xx[%d]: Error in Response Queues Alignment\n",
9431713e81bSScott Long 				 pAdapter->mvSataAdapter.adapterId);
944*d1bdc282SBjoern A. Zeeb 		free(pAdapter->requestsArrayBaseAddr, M_DEVBUF);
945*d1bdc282SBjoern A. Zeeb 		free(pAdapter->responsesArrayBaseAddr, M_DEVBUF);
9461713e81bSScott Long 		return -1;
9471713e81bSScott Long 	}
9481713e81bSScott Long 	return 0;
9491713e81bSScott Long }
9501713e81bSScott Long 
9511713e81bSScott Long static void
9521713e81bSScott Long hptmv_free_edma_queues(IAL_ADAPTER_T *pAdapter)
9531713e81bSScott Long {
954*d1bdc282SBjoern A. Zeeb 	free(pAdapter->requestsArrayBaseAddr, M_DEVBUF);
955*d1bdc282SBjoern A. Zeeb 	free(pAdapter->responsesArrayBaseAddr, M_DEVBUF);
9561713e81bSScott Long }
9571713e81bSScott Long 
9581713e81bSScott Long static PVOID
9591713e81bSScott Long AllocatePRDTable(IAL_ADAPTER_T *pAdapter)
9601713e81bSScott Long {
9611713e81bSScott Long 	PVOID ret;
9621713e81bSScott Long 	if (pAdapter->pFreePRDLink) {
963d2bd3ab9SScott Long 		KdPrint(("pAdapter->pFreePRDLink:%p\n",pAdapter->pFreePRDLink));
9641713e81bSScott Long 		ret = pAdapter->pFreePRDLink;
9651713e81bSScott Long 		pAdapter->pFreePRDLink = *(void**)ret;
9661713e81bSScott Long 		return ret;
9671713e81bSScott Long 	}
9681713e81bSScott Long 	return NULL;
9691713e81bSScott Long }
9701713e81bSScott Long 
9711713e81bSScott Long static void
9721713e81bSScott Long FreePRDTable(IAL_ADAPTER_T *pAdapter, PVOID PRDTable)
9731713e81bSScott Long {
9741713e81bSScott Long 	*(void**)PRDTable = pAdapter->pFreePRDLink;
9751713e81bSScott Long 	pAdapter->pFreePRDLink = PRDTable;
9761713e81bSScott Long }
9771713e81bSScott Long 
9781713e81bSScott Long extern PVDevice fGetFirstChild(PVDevice pLogical);
9791713e81bSScott Long extern void fResetBootMark(PVDevice pLogical);
9801713e81bSScott Long static void
9811713e81bSScott Long fRegisterVdevice(IAL_ADAPTER_T *pAdapter)
9821713e81bSScott Long {
9831713e81bSScott Long 	PVDevice pPhysical, pLogical;
9841713e81bSScott Long 	PVBus  pVBus;
9851713e81bSScott Long 	int i,j;
9861713e81bSScott Long 
9871713e81bSScott Long 	for(i=0;i<MV_SATA_CHANNELS_NUM;i++) {
9881713e81bSScott Long 		pPhysical = &(pAdapter->VDevices[i]);
9891713e81bSScott Long 		pLogical = pPhysical;
9901713e81bSScott Long 		while (pLogical->pParent) pLogical = pLogical->pParent;
9911713e81bSScott Long 		if (pLogical->vf_online==0) {
9921713e81bSScott Long 			pPhysical->vf_bootmark = pLogical->vf_bootmark = 0;
9931713e81bSScott Long 			continue;
9941713e81bSScott Long 		}
995d2bd3ab9SScott Long 		if (pLogical->VDeviceType==VD_SPARE || pPhysical!=fGetFirstChild(pLogical))
9961713e81bSScott Long 			continue;
9971713e81bSScott Long 
9981713e81bSScott Long 		pVBus = &pAdapter->VBus;
999d2bd3ab9SScott Long 		if(pVBus)
1000d2bd3ab9SScott Long 		{
10011713e81bSScott Long 			j=0;
1002d2bd3ab9SScott Long 			while(j<MAX_VDEVICE_PER_VBUS && pVBus->pVDevice[j]) j++;
10031713e81bSScott Long 			if(j<MAX_VDEVICE_PER_VBUS){
10041713e81bSScott Long 				pVBus->pVDevice[j] = pLogical;
10051713e81bSScott Long 				pLogical->pVBus = pVBus;
10061713e81bSScott Long 
10071713e81bSScott Long 				if (j>0 && pLogical->vf_bootmark) {
10081713e81bSScott Long 					if (pVBus->pVDevice[0]->vf_bootmark) {
10091713e81bSScott Long 						fResetBootMark(pLogical);
1010d2bd3ab9SScott Long 					}
1011d2bd3ab9SScott Long 					else {
1012d2bd3ab9SScott Long 						do { pVBus->pVDevice[j] = pVBus->pVDevice[j-1]; } while (--j);
10131713e81bSScott Long 						pVBus->pVDevice[0] = pLogical;
10141713e81bSScott Long 					}
10151713e81bSScott Long 				}
10161713e81bSScott Long 			}
10171713e81bSScott Long 		}
10181713e81bSScott Long 	}
10191713e81bSScott Long }
10201713e81bSScott Long 
10211713e81bSScott Long PVDevice
10221713e81bSScott Long GetSpareDisk(_VBUS_ARG PVDevice pArray)
10231713e81bSScott Long {
1024d2bd3ab9SScott Long 	IAL_ADAPTER_T *pAdapter = (IAL_ADAPTER_T *)pArray->pVBus->OsExt;
102564470755SXin LI 	LBA_T capacity = LongDiv(pArray->VDeviceCapacity, pArray->u.array.bArnMember-1);
102664470755SXin LI 	LBA_T thiscap, maxcap = MAX_LBA_T;
10271713e81bSScott Long 	PVDevice pVDevice, pFind = NULL;
10281713e81bSScott Long 	int i;
10291713e81bSScott Long 
1030d2bd3ab9SScott Long 	for(i=0;i<MV_SATA_CHANNELS_NUM;i++)
1031d2bd3ab9SScott Long 	{
10321713e81bSScott Long 		pVDevice = &pAdapter->VDevices[i];
10331713e81bSScott Long 		if(!pVDevice)
10341713e81bSScott Long 			continue;
1035d2bd3ab9SScott Long 		thiscap = pArray->vf_format_v2? pVDevice->u.disk.dDeRealCapacity : pVDevice->VDeviceCapacity;
10361713e81bSScott Long 		/* find the smallest usable spare disk */
10371713e81bSScott Long 		if (pVDevice->VDeviceType==VD_SPARE &&
1038d2bd3ab9SScott Long 			pVDevice->u.disk.df_on_line &&
1039d2bd3ab9SScott Long 			thiscap < maxcap &&
1040d2bd3ab9SScott Long 			thiscap >= capacity)
1041d2bd3ab9SScott Long 		{
10421713e81bSScott Long 				maxcap = pVDevice->VDeviceCapacity;
10431713e81bSScott Long 				pFind = pVDevice;
10441713e81bSScott Long 		}
10451713e81bSScott Long 	}
10461713e81bSScott Long 	return pFind;
10471713e81bSScott Long }
10481713e81bSScott Long 
10491713e81bSScott Long /******************************************************************
10501713e81bSScott Long  * IO ATA Command
10511713e81bSScott Long  *******************************************************************/
10521713e81bSScott Long int HPTLIBAPI
10531713e81bSScott Long fDeReadWrite(PDevice pDev, ULONG Lba, UCHAR Cmd, void *tmpBuffer)
10541713e81bSScott Long {
10551713e81bSScott Long 	return mvReadWrite(pDev->mv, Lba, Cmd, tmpBuffer);
10561713e81bSScott Long }
10571713e81bSScott Long 
10581713e81bSScott Long void HPTLIBAPI fDeSelectMode(PDevice pDev, UCHAR NewMode)
10591713e81bSScott Long {
1060d2bd3ab9SScott Long 	MV_SATA_CHANNEL *pSataChannel = pDev->mv;
1061d2bd3ab9SScott Long 	MV_SATA_ADAPTER *pSataAdapter = pSataChannel->mvSataAdapter;
1062d2bd3ab9SScott Long 	MV_U8 channelIndex = pSataChannel->channelNumber;
10631713e81bSScott Long 	UCHAR mvMode;
10641713e81bSScott Long 	/* 508x don't use MW-DMA? */
10651713e81bSScott Long 	if (NewMode>4 && NewMode<8) NewMode = 4;
10661713e81bSScott Long 	pDev->bDeModeSetting = NewMode;
10671713e81bSScott Long 	if (NewMode<=4)
10681713e81bSScott Long 		mvMode = MV_ATA_TRANSFER_PIO_0 + NewMode;
10691713e81bSScott Long 	else
10701713e81bSScott Long 		mvMode = MV_ATA_TRANSFER_UDMA_0 + (NewMode-8);
10711713e81bSScott Long 
10721713e81bSScott Long 	/*To fix 88i8030 bug*/
10731713e81bSScott Long 	if (mvMode > MV_ATA_TRANSFER_UDMA_0 && mvMode < MV_ATA_TRANSFER_UDMA_4)
10741713e81bSScott Long 		mvMode = MV_ATA_TRANSFER_UDMA_0;
10751713e81bSScott Long 
10761713e81bSScott Long 	mvSataDisableChannelDma(pSataAdapter, channelIndex);
10771713e81bSScott Long 	/* Flush pending commands */
10781713e81bSScott Long 	mvSataFlushDmaQueue (pSataAdapter, channelIndex, MV_FLUSH_TYPE_NONE);
10791713e81bSScott Long 
10801713e81bSScott Long 	if (mvStorageDevATASetFeatures(pSataAdapter, channelIndex,
1081d2bd3ab9SScott Long 								   MV_ATA_SET_FEATURES_TRANSFER,
1082d2bd3ab9SScott Long 								   mvMode, 0, 0, 0) == MV_FALSE)
1083d2bd3ab9SScott Long 	{
10841713e81bSScott Long 		KdPrint(("channel %d: Set Features failed\n", channelIndex));
10851713e81bSScott Long 	}
10861713e81bSScott Long 	/* Enable EDMA */
10871713e81bSScott Long 	if (mvSataEnableChannelDma(pSataAdapter, channelIndex) == MV_FALSE)
10881713e81bSScott Long 		KdPrint(("Failed to enable DMA, channel=%d", channelIndex));
10891713e81bSScott Long }
10901713e81bSScott Long 
109164470755SXin LI int HPTLIBAPI fDeSetTCQ(PDevice pDev, int enable, int depth)
109264470755SXin LI {
109364470755SXin LI 	MV_SATA_CHANNEL *pSataChannel = pDev->mv;
109464470755SXin LI 	MV_SATA_ADAPTER *pSataAdapter = pSataChannel->mvSataAdapter;
109564470755SXin LI 	MV_U8 channelIndex = pSataChannel->channelNumber;
109664470755SXin LI 	IAL_ADAPTER_T *pAdapter = pSataAdapter->IALData;
109764470755SXin LI 	MV_CHANNEL		*channelInfo = &(pAdapter->mvChannel[channelIndex]);
109864470755SXin LI 	int dmaActive = pSataChannel->queueCommandsEnabled;
109964470755SXin LI 	int ret = 0;
110064470755SXin LI 
110164470755SXin LI 	if (dmaActive) {
110264470755SXin LI 		mvSataDisableChannelDma(pSataAdapter, channelIndex);
110364470755SXin LI 		mvSataFlushDmaQueue(pSataAdapter,channelIndex,MV_FLUSH_TYPE_CALLBACK);
110464470755SXin LI 	}
110564470755SXin LI 
110664470755SXin LI 	if (enable) {
110764470755SXin LI 		if (pSataChannel->queuedDMA == MV_EDMA_MODE_NOT_QUEUED &&
110864470755SXin LI 			(pSataChannel->identifyDevice[IDEN_SUPPORTED_COMMANDS2] & (0x2))) {
110964470755SXin LI 			UCHAR depth = ((pSataChannel->identifyDevice[IDEN_QUEUE_DEPTH]) & 0x1f) + 1;
111064470755SXin LI 			channelInfo->queueDepth = (depth==32)? 31 : depth;
111164470755SXin LI 			mvSataConfigEdmaMode(pSataAdapter, channelIndex, MV_EDMA_MODE_QUEUED, depth);
111264470755SXin LI 			ret = 1;
111364470755SXin LI 		}
111464470755SXin LI 	}
111564470755SXin LI 	else
111664470755SXin LI 	{
111764470755SXin LI 		if (pSataChannel->queuedDMA != MV_EDMA_MODE_NOT_QUEUED) {
111864470755SXin LI 			channelInfo->queueDepth = 2;
111964470755SXin LI 			mvSataConfigEdmaMode(pSataAdapter, channelIndex, MV_EDMA_MODE_NOT_QUEUED, 0);
112064470755SXin LI 			ret = 1;
112164470755SXin LI 		}
112264470755SXin LI 	}
112364470755SXin LI 
112464470755SXin LI 	if (dmaActive)
112564470755SXin LI 		mvSataEnableChannelDma(pSataAdapter,channelIndex);
112664470755SXin LI 	return ret;
112764470755SXin LI }
112864470755SXin LI 
112964470755SXin LI int HPTLIBAPI fDeSetNCQ(PDevice pDev, int enable, int depth)
113064470755SXin LI {
113164470755SXin LI 	return 0;
113264470755SXin LI }
113364470755SXin LI 
113464470755SXin LI int HPTLIBAPI fDeSetWriteCache(PDevice pDev, int enable)
113564470755SXin LI {
113664470755SXin LI 	MV_SATA_CHANNEL *pSataChannel = pDev->mv;
113764470755SXin LI 	MV_SATA_ADAPTER *pSataAdapter = pSataChannel->mvSataAdapter;
113864470755SXin LI 	MV_U8 channelIndex = pSataChannel->channelNumber;
113964470755SXin LI 	IAL_ADAPTER_T *pAdapter = pSataAdapter->IALData;
114064470755SXin LI 	MV_CHANNEL		*channelInfo = &(pAdapter->mvChannel[channelIndex]);
114164470755SXin LI 	int dmaActive = pSataChannel->queueCommandsEnabled;
114264470755SXin LI 	int ret = 0;
114364470755SXin LI 
114464470755SXin LI 	if (dmaActive) {
114564470755SXin LI 		mvSataDisableChannelDma(pSataAdapter, channelIndex);
114664470755SXin LI 		mvSataFlushDmaQueue(pSataAdapter,channelIndex,MV_FLUSH_TYPE_CALLBACK);
114764470755SXin LI 	}
114864470755SXin LI 
114964470755SXin LI 	if ((pSataChannel->identifyDevice[82] & (0x20))) {
115064470755SXin LI 		if (enable) {
115164470755SXin LI 			if (mvStorageDevATASetFeatures(pSataAdapter, channelIndex,
115264470755SXin LI 				MV_ATA_SET_FEATURES_ENABLE_WCACHE, 0, 0, 0, 0))
115364470755SXin LI 			{
115464470755SXin LI 				channelInfo->writeCacheEnabled = MV_TRUE;
115564470755SXin LI 				ret = 1;
115664470755SXin LI 			}
115764470755SXin LI 		}
115864470755SXin LI 		else {
115964470755SXin LI 			if (mvStorageDevATASetFeatures(pSataAdapter, channelIndex,
116064470755SXin LI 				MV_ATA_SET_FEATURES_DISABLE_WCACHE, 0, 0, 0, 0))
116164470755SXin LI 			{
116264470755SXin LI 				channelInfo->writeCacheEnabled = MV_FALSE;
116364470755SXin LI 				ret = 1;
116464470755SXin LI 			}
116564470755SXin LI 		}
116664470755SXin LI 	}
116764470755SXin LI 
116864470755SXin LI 	if (dmaActive)
116964470755SXin LI 		mvSataEnableChannelDma(pSataAdapter,channelIndex);
117064470755SXin LI 	return ret;
117164470755SXin LI }
117264470755SXin LI 
117364470755SXin LI int HPTLIBAPI fDeSetReadAhead(PDevice pDev, int enable)
117464470755SXin LI {
117564470755SXin LI 	MV_SATA_CHANNEL *pSataChannel = pDev->mv;
117664470755SXin LI 	MV_SATA_ADAPTER *pSataAdapter = pSataChannel->mvSataAdapter;
117764470755SXin LI 	MV_U8 channelIndex = pSataChannel->channelNumber;
117864470755SXin LI 	IAL_ADAPTER_T *pAdapter = pSataAdapter->IALData;
117964470755SXin LI 	MV_CHANNEL		*channelInfo = &(pAdapter->mvChannel[channelIndex]);
118064470755SXin LI 	int dmaActive = pSataChannel->queueCommandsEnabled;
118164470755SXin LI 	int ret = 0;
118264470755SXin LI 
118364470755SXin LI 	if (dmaActive) {
118464470755SXin LI 		mvSataDisableChannelDma(pSataAdapter, channelIndex);
118564470755SXin LI 		mvSataFlushDmaQueue(pSataAdapter,channelIndex,MV_FLUSH_TYPE_CALLBACK);
118664470755SXin LI 	}
118764470755SXin LI 
118864470755SXin LI 	if ((pSataChannel->identifyDevice[82] & (0x40))) {
118964470755SXin LI 		if (enable) {
119064470755SXin LI 			if (mvStorageDevATASetFeatures(pSataAdapter, channelIndex,
119164470755SXin LI 				MV_ATA_SET_FEATURES_ENABLE_RLA, 0, 0, 0, 0))
119264470755SXin LI 			{
119364470755SXin LI 				channelInfo->readAheadEnabled = MV_TRUE;
119464470755SXin LI 				ret = 1;
119564470755SXin LI 			}
119664470755SXin LI 		}
119764470755SXin LI 		else {
119864470755SXin LI 			if (mvStorageDevATASetFeatures(pSataAdapter, channelIndex,
119964470755SXin LI 				MV_ATA_SET_FEATURES_DISABLE_RLA, 0, 0, 0, 0))
120064470755SXin LI 			{
120164470755SXin LI 				channelInfo->readAheadEnabled = MV_FALSE;
120264470755SXin LI 				ret = 1;
120364470755SXin LI 			}
120464470755SXin LI 		}
120564470755SXin LI 	}
120664470755SXin LI 
120764470755SXin LI 	if (dmaActive)
120864470755SXin LI 		mvSataEnableChannelDma(pSataAdapter,channelIndex);
120964470755SXin LI 	return ret;
121064470755SXin LI }
121164470755SXin LI 
12121713e81bSScott Long #ifdef SUPPORT_ARRAY
12131713e81bSScott Long #define IdeRegisterVDevice  fCheckArray
12141713e81bSScott Long #else
12151713e81bSScott Long void
12161713e81bSScott Long IdeRegisterVDevice(PDevice pDev)
12171713e81bSScott Long {
12181713e81bSScott Long 	PVDevice pVDev = Map2pVDevice(pDev);
12191713e81bSScott Long 
12201713e81bSScott Long 	pVDev->VDeviceType = pDev->df_atapi? VD_ATAPI :
12211713e81bSScott Long 						 pDev->df_removable_drive? VD_REMOVABLE : VD_SINGLE_DISK;
12221713e81bSScott Long 	pVDev->vf_online = 1;
12231713e81bSScott Long 	pVDev->VDeviceCapacity = pDev->dDeRealCapacity;
12241713e81bSScott Long 	pVDev->pfnSendCommand = pfnSendCommand[pVDev->VDeviceType];
12251713e81bSScott Long 	pVDev->pfnDeviceFailed = pfnDeviceFailed[pVDev->VDeviceType];
12261713e81bSScott Long }
12271713e81bSScott Long #endif
12281713e81bSScott Long 
1229d2bd3ab9SScott Long static __inline PBUS_DMAMAP
1230d2bd3ab9SScott Long dmamap_get(struct IALAdapter * pAdapter)
1231d2bd3ab9SScott Long {
1232d2bd3ab9SScott Long 	PBUS_DMAMAP	p = pAdapter->pbus_dmamap_list;
1233d2bd3ab9SScott Long 	if (p)
1234d2bd3ab9SScott Long 		pAdapter->pbus_dmamap_list = p-> next;
1235d2bd3ab9SScott Long 	return p;
1236d2bd3ab9SScott Long }
1237d2bd3ab9SScott Long 
1238d2bd3ab9SScott Long static __inline void
1239d2bd3ab9SScott Long dmamap_put(PBUS_DMAMAP p)
1240d2bd3ab9SScott Long {
1241d2bd3ab9SScott Long 	p->next = p->pAdapter->pbus_dmamap_list;
1242d2bd3ab9SScott Long 	p->pAdapter->pbus_dmamap_list = p;
1243d2bd3ab9SScott Long }
1244d2bd3ab9SScott Long 
12451713e81bSScott Long static int num_adapters = 0;
12461713e81bSScott Long static int
12471713e81bSScott Long init_adapter(IAL_ADAPTER_T *pAdapter)
12481713e81bSScott Long {
12491713e81bSScott Long 	PVBus _vbus_p = &pAdapter->VBus;
12501713e81bSScott Long 	MV_SATA_ADAPTER *pMvSataAdapter;
1251d2bd3ab9SScott Long 	int i, channel, rid;
12521713e81bSScott Long 
12531713e81bSScott Long 	PVDevice pVDev;
12541713e81bSScott Long 
125549b3fc40SJohn Baldwin 	mtx_init(&pAdapter->lock, "hptsleeplock", NULL, MTX_DEF);
125649b3fc40SJohn Baldwin 	callout_init_mtx(&pAdapter->event_timer_connect, &pAdapter->lock, 0);
125749b3fc40SJohn Baldwin 	callout_init_mtx(&pAdapter->event_timer_disconnect, &pAdapter->lock, 0);
125864470755SXin LI 
125949b3fc40SJohn Baldwin 	sx_xlock(&hptmv_list_lock);
12601713e81bSScott Long 	pAdapter->next = 0;
12611713e81bSScott Long 
12624d24901aSPedro F. Giffuni 	if(gIal_Adapter == NULL){
12631713e81bSScott Long 		gIal_Adapter = pAdapter;
12641713e81bSScott Long 		pCurAdapter = gIal_Adapter;
1265d2bd3ab9SScott Long 	}
1266d2bd3ab9SScott Long 	else {
12671713e81bSScott Long 		pCurAdapter->next = pAdapter;
12681713e81bSScott Long 		pCurAdapter = pAdapter;
12691713e81bSScott Long 	}
127049b3fc40SJohn Baldwin 	sx_xunlock(&hptmv_list_lock);
12711713e81bSScott Long 
12721713e81bSScott Long 	pAdapter->outstandingCommands = 0;
12731713e81bSScott Long 
12741713e81bSScott Long 	pMvSataAdapter = &(pAdapter->mvSataAdapter);
12751713e81bSScott Long 	_vbus_p->OsExt = (void *)pAdapter;
12761713e81bSScott Long 	pMvSataAdapter->IALData = pAdapter;
12771713e81bSScott Long 
1278b6f97155SScott Long 	if (bus_dma_tag_create(bus_get_dma_tag(pAdapter->hpt_dev),/* parent */
1279d2bd3ab9SScott Long 			4,	/* alignment */
1280d2bd3ab9SScott Long 			BUS_SPACE_MAXADDR_32BIT+1, /* boundary */
1281d2bd3ab9SScott Long 			BUS_SPACE_MAXADDR,	/* lowaddr */
1282d2bd3ab9SScott Long 			BUS_SPACE_MAXADDR,	/* highaddr */
1283d2bd3ab9SScott Long 			NULL, NULL, 		/* filter, filterarg */
1284d2bd3ab9SScott Long 			PAGE_SIZE * (MAX_SG_DESCRIPTORS-1), /* maxsize */
1285d2bd3ab9SScott Long 			MAX_SG_DESCRIPTORS, /* nsegments */
1286d2bd3ab9SScott Long 			0x10000,	/* maxsegsize */
1287d2bd3ab9SScott Long 			BUS_DMA_WAITOK, 	/* flags */
1288d2bd3ab9SScott Long 			busdma_lock_mutex,	/* lockfunc */
128949b3fc40SJohn Baldwin 			&pAdapter->lock,	/* lockfuncarg */
1290d2bd3ab9SScott Long 			&pAdapter->io_dma_parent /* tag */))
1291d2bd3ab9SScott Long 		{
1292c2ede4b3SMartin Blapp 			return ENXIO;
12931713e81bSScott Long 	}
12941713e81bSScott Long 
12951713e81bSScott Long 
1296d2bd3ab9SScott Long 	if (hptmv_allocate_edma_queues(pAdapter))
1297d2bd3ab9SScott Long 	{
129864470755SXin LI 		MV_ERROR("RR18xx: Failed to allocate memory for EDMA queues\n");
1299d2bd3ab9SScott Long 		return ENOMEM;
13001713e81bSScott Long 	}
13011713e81bSScott Long 
13021713e81bSScott Long 	/* also map EPROM address */
13031713e81bSScott Long 	rid = 0x10;
1304eff83876SJustin Hibbits 	if (!(pAdapter->mem_res = bus_alloc_resource_any(pAdapter->hpt_dev,
1305eff83876SJustin Hibbits 			SYS_RES_MEMORY, &rid, RF_ACTIVE))
1306d2bd3ab9SScott Long 		||
1307d2bd3ab9SScott Long 		!(pMvSataAdapter->adapterIoBaseAddress = rman_get_virtual(pAdapter->mem_res)))
1308d2bd3ab9SScott Long 	{
130964470755SXin LI 		MV_ERROR("RR18xx: Failed to remap memory space\n");
1310d2bd3ab9SScott Long 		hptmv_free_edma_queues(pAdapter);
1311d2bd3ab9SScott Long 		return ENXIO;
13121713e81bSScott Long 	}
1313d2bd3ab9SScott Long 	else
1314d2bd3ab9SScott Long 	{
131564470755SXin LI 		KdPrint(("RR18xx: io base address 0x%p\n", pMvSataAdapter->adapterIoBaseAddress));
1316d2bd3ab9SScott Long 	}
13171713e81bSScott Long 
13181713e81bSScott Long 	pMvSataAdapter->adapterId = num_adapters++;
13191713e81bSScott Long 	/* get the revision ID */
1320d2bd3ab9SScott Long 	pMvSataAdapter->pciConfigRevisionId = pci_read_config(pAdapter->hpt_dev, PCIR_REVID, 1);
13211713e81bSScott Long 	pMvSataAdapter->pciConfigDeviceId = pci_get_device(pAdapter->hpt_dev);
13221713e81bSScott Long 
132364470755SXin LI 	/* init RR18xx */
13241713e81bSScott Long 	pMvSataAdapter->intCoalThre[0]= 1;
13251713e81bSScott Long 	pMvSataAdapter->intCoalThre[1]= 1;
13261713e81bSScott Long 	pMvSataAdapter->intTimeThre[0] = 1;
13271713e81bSScott Long 	pMvSataAdapter->intTimeThre[1] = 1;
13281713e81bSScott Long 	pMvSataAdapter->pciCommand = 0x0107E371;
13291713e81bSScott Long 	pMvSataAdapter->pciSerrMask = 0xd77fe6ul;
13301713e81bSScott Long 	pMvSataAdapter->pciInterruptMask = 0xd77fe6ul;
13311713e81bSScott Long 	pMvSataAdapter->mvSataEventNotify = hptmv_event_notify;
13321713e81bSScott Long 
1333d2bd3ab9SScott Long 	if (mvSataInitAdapter(pMvSataAdapter) == MV_FALSE)
1334d2bd3ab9SScott Long 	{
133564470755SXin LI 		MV_ERROR("RR18xx[%d]: core failed to initialize the adapter\n",
13361713e81bSScott Long 				 pMvSataAdapter->adapterId);
1337d2bd3ab9SScott Long unregister:
1338d2bd3ab9SScott Long 		bus_release_resource(pAdapter->hpt_dev, SYS_RES_MEMORY, rid, pAdapter->mem_res);
1339d2bd3ab9SScott Long 		hptmv_free_edma_queues(pAdapter);
1340d2bd3ab9SScott Long 		return ENXIO;
13411713e81bSScott Long 	}
13421713e81bSScott Long 	pAdapter->ver_601 = pMvSataAdapter->pcbVersion;
13431713e81bSScott Long 
13441713e81bSScott Long #ifndef FOR_DEMO
13451713e81bSScott Long 	set_fail_leds(pMvSataAdapter, 0);
13461713e81bSScott Long #endif
13471713e81bSScott Long 
13481713e81bSScott Long 	/* setup command blocks */
13491713e81bSScott Long 	KdPrint(("Allocate command blocks\n"));
13501713e81bSScott Long 	_vbus_(pFreeCommands) = 0;
1351d2bd3ab9SScott Long 	pAdapter->pCommandBlocks =
1352d2bd3ab9SScott Long 		malloc(sizeof(struct _Command) * MAX_COMMAND_BLOCKS_FOR_EACH_VBUS, M_DEVBUF, M_NOWAIT);
13537d9aed9cSScott Long 	KdPrint(("pCommandBlocks:%p\n",pAdapter->pCommandBlocks));
1354d2bd3ab9SScott Long 	if (!pAdapter->pCommandBlocks) {
1355d2bd3ab9SScott Long 		MV_ERROR("insufficient memory\n");
1356d2bd3ab9SScott Long 		goto unregister;
13571713e81bSScott Long 	}
13581713e81bSScott Long 
1359d2bd3ab9SScott Long 	for (i=0; i<MAX_COMMAND_BLOCKS_FOR_EACH_VBUS; i++) {
1360d2bd3ab9SScott Long 		FreeCommand(_VBUS_P &(pAdapter->pCommandBlocks[i]));
1361d2bd3ab9SScott Long 	}
1362d2bd3ab9SScott Long 
1363d2bd3ab9SScott Long 	/*Set up the bus_dmamap*/
1364d2bd3ab9SScott Long 	pAdapter->pbus_dmamap = (PBUS_DMAMAP)malloc (sizeof(struct _BUS_DMAMAP) * MAX_QUEUE_COMM, M_DEVBUF, M_NOWAIT);
1365d2bd3ab9SScott Long 	if(!pAdapter->pbus_dmamap) {
1366d2bd3ab9SScott Long 		MV_ERROR("insufficient memory\n");
1367d2bd3ab9SScott Long 		free(pAdapter->pCommandBlocks, M_DEVBUF);
1368d2bd3ab9SScott Long 		goto unregister;
1369d2bd3ab9SScott Long 	}
1370d2bd3ab9SScott Long 
1371d2bd3ab9SScott Long 	memset((void *)pAdapter->pbus_dmamap, 0, sizeof(struct _BUS_DMAMAP) * MAX_QUEUE_COMM);
1372d2bd3ab9SScott Long 	pAdapter->pbus_dmamap_list = 0;
1373d2bd3ab9SScott Long 	for (i=0; i < MAX_QUEUE_COMM; i++) {
1374d2bd3ab9SScott Long 		PBUS_DMAMAP  pmap = &(pAdapter->pbus_dmamap[i]);
1375d2bd3ab9SScott Long 		pmap->pAdapter = pAdapter;
1376d2bd3ab9SScott Long 		dmamap_put(pmap);
1377d2bd3ab9SScott Long 
1378d2bd3ab9SScott Long 		if(bus_dmamap_create(pAdapter->io_dma_parent, 0, &pmap->dma_map)) {
1379d2bd3ab9SScott Long 			MV_ERROR("Can not allocate dma map\n");
1380d2bd3ab9SScott Long 			free(pAdapter->pCommandBlocks, M_DEVBUF);
1381d2bd3ab9SScott Long 			free(pAdapter->pbus_dmamap, M_DEVBUF);
1382d2bd3ab9SScott Long 			goto unregister;
1383d2bd3ab9SScott Long 		}
138449b3fc40SJohn Baldwin 		callout_init_mtx(&pmap->timeout, &pAdapter->lock, 0);
1385d2bd3ab9SScott Long 	}
13861713e81bSScott Long 	/* setup PRD Tables */
13871713e81bSScott Long 	KdPrint(("Allocate PRD Tables\n"));
13881713e81bSScott Long 	pAdapter->pFreePRDLink = 0;
13891713e81bSScott Long 
1390d2bd3ab9SScott Long 	pAdapter->prdTableAddr = (PUCHAR)contigmalloc(
1391d2bd3ab9SScott Long 		(PRD_ENTRIES_SIZE*PRD_TABLES_FOR_VBUS + 32), M_DEVBUF, M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0ul);
13921713e81bSScott Long 
13937d9aed9cSScott Long 	KdPrint(("prdTableAddr:%p\n",pAdapter->prdTableAddr));
13941713e81bSScott Long 	if (!pAdapter->prdTableAddr) {
13951713e81bSScott Long 		MV_ERROR("insufficient PRD Tables\n");
13961713e81bSScott Long 		goto unregister;
13971713e81bSScott Long 	}
1398d2bd3ab9SScott Long 	pAdapter->prdTableAlignedAddr = (PUCHAR)(((ULONG_PTR)pAdapter->prdTableAddr + 0x1f) & ~(ULONG_PTR)0x1fL);
1399d2bd3ab9SScott Long 	{
1400d2bd3ab9SScott Long 		PUCHAR PRDTable = pAdapter->prdTableAlignedAddr;
1401d2bd3ab9SScott Long 		for (i=0; i<PRD_TABLES_FOR_VBUS; i++)
1402d2bd3ab9SScott Long 		{
1403d2bd3ab9SScott Long /*			KdPrint(("i=%d,pAdapter->pFreePRDLink=%p\n",i,pAdapter->pFreePRDLink)); */
14041713e81bSScott Long 			FreePRDTable(pAdapter, PRDTable);
14051713e81bSScott Long 			PRDTable += PRD_ENTRIES_SIZE;
14061713e81bSScott Long 		}
1407d2bd3ab9SScott Long 	}
14081713e81bSScott Long 
14091713e81bSScott Long 	/* enable the adapter interrupts */
14101713e81bSScott Long 
14111713e81bSScott Long 	/* configure and start the connected channels*/
1412d2bd3ab9SScott Long 	for (channel = 0; channel < MV_SATA_CHANNELS_NUM; channel++)
1413d2bd3ab9SScott Long 	{
14141713e81bSScott Long 		pAdapter->mvChannel[channel].online = MV_FALSE;
14151713e81bSScott Long 		if (mvSataIsStorageDeviceConnected(pMvSataAdapter, channel)
1416d2bd3ab9SScott Long 			== MV_TRUE)
1417d2bd3ab9SScott Long 		{
141864470755SXin LI 			KdPrint(("RR18xx[%d]: channel %d is connected\n",
14191713e81bSScott Long 					  pMvSataAdapter->adapterId, channel));
14201713e81bSScott Long 
1421d2bd3ab9SScott Long 			if (hptmv_init_channel(pAdapter, channel) == 0)
1422d2bd3ab9SScott Long 			{
1423d2bd3ab9SScott Long 				if (mvSataConfigureChannel(pMvSataAdapter, channel) == MV_FALSE)
1424d2bd3ab9SScott Long 				{
142564470755SXin LI 					MV_ERROR("RR18xx[%d]: Failed to configure channel"
1426d2bd3ab9SScott Long 							 " %d\n",pMvSataAdapter->adapterId, channel);
14271713e81bSScott Long 					hptmv_free_channel(pAdapter, channel);
14281713e81bSScott Long 				}
1429d2bd3ab9SScott Long 				else
1430d2bd3ab9SScott Long 				{
1431d2bd3ab9SScott Long 					if (start_channel(pAdapter, channel))
1432d2bd3ab9SScott Long 					{
143364470755SXin LI 						MV_ERROR("RR18xx[%d]: Failed to start channel,"
14341713e81bSScott Long 								 " channel=%d\n",pMvSataAdapter->adapterId,
14351713e81bSScott Long 								 channel);
14361713e81bSScott Long 						hptmv_free_channel(pAdapter, channel);
14371713e81bSScott Long 					}
14381713e81bSScott Long 					pAdapter->mvChannel[channel].online = MV_TRUE;
1439d2bd3ab9SScott Long 					/*  mvSataChannelSetEdmaLoopBackMode(pMvSataAdapter,
1440d2bd3ab9SScott Long 													   channel,
1441d2bd3ab9SScott Long 													   MV_TRUE);*/
1442d2bd3ab9SScott Long 				}
1443d2bd3ab9SScott Long 			}
14441713e81bSScott Long 		}
14451713e81bSScott Long 		KdPrint(("pAdapter->mvChannel[channel].online:%x, channel:%d\n",
14461713e81bSScott Long 			pAdapter->mvChannel[channel].online, channel));
14471713e81bSScott Long 	}
14481713e81bSScott Long 
14491713e81bSScott Long #ifdef SUPPORT_ARRAY
14501713e81bSScott Long 	for(i = MAX_ARRAY_DEVICE - 1; i >= 0; i--) {
14511713e81bSScott Long 		pVDev = ArrayTables(i);
14521713e81bSScott Long 		mArFreeArrayTable(pVDev);
14531713e81bSScott Long 	}
14541713e81bSScott Long #endif
14551713e81bSScott Long 
14561713e81bSScott Long 	KdPrint(("Initialize Devices\n"));
14571713e81bSScott Long 	for (channel = 0; channel < MV_SATA_CHANNELS_NUM; channel++) {
1458d2bd3ab9SScott Long 		MV_SATA_CHANNEL *pMvSataChannel = pMvSataAdapter->sataChannel[channel];
14591713e81bSScott Long 		if (pMvSataChannel) {
14601713e81bSScott Long 			init_vdev_params(pAdapter, channel);
14611713e81bSScott Long 			IdeRegisterVDevice(&pAdapter->VDevices[channel].u.disk);
14621713e81bSScott Long 		}
14631713e81bSScott Long 	}
14641713e81bSScott Long #ifdef SUPPORT_ARRAY
14651713e81bSScott Long 	CheckArrayCritical(_VBUS_P0);
14661713e81bSScott Long #endif
14671713e81bSScott Long 	_vbus_p->nInstances = 1;
14681713e81bSScott Long 	fRegisterVdevice(pAdapter);
14691713e81bSScott Long 
14701713e81bSScott Long 	for (channel=0;channel<MV_SATA_CHANNELS_NUM;channel++) {
14711713e81bSScott Long 		pVDev = _vbus_p->pVDevice[channel];
14721713e81bSScott Long 		if (pVDev && pVDev->vf_online)
14731713e81bSScott Long 			fCheckBootable(pVDev);
14741713e81bSScott Long 	}
14751713e81bSScott Long 
14761713e81bSScott Long #if defined(SUPPORT_ARRAY) && defined(_RAID5N_)
14771713e81bSScott Long 	init_raid5_memory(_VBUS_P0);
14781713e81bSScott Long 	_vbus_(r5).enable_write_back = 1;
147964470755SXin LI 	printf("RR18xx: RAID5 write-back %s\n", _vbus_(r5).enable_write_back? "enabled" : "disabled");
14801713e81bSScott Long #endif
14811713e81bSScott Long 
14821713e81bSScott Long 	mvSataUnmaskAdapterInterrupt(pMvSataAdapter);
14831713e81bSScott Long 	return 0;
14841713e81bSScott Long }
14851713e81bSScott Long 
14861713e81bSScott Long int
14871713e81bSScott Long MvSataResetChannel(MV_SATA_ADAPTER *pMvSataAdapter, MV_U8 channel)
14881713e81bSScott Long {
14891713e81bSScott Long 	IAL_ADAPTER_T   *pAdapter = (IAL_ADAPTER_T *)pMvSataAdapter->IALData;
14901713e81bSScott Long 
14911713e81bSScott Long 	mvSataDisableChannelDma(pMvSataAdapter, channel);
14921713e81bSScott Long 	/* Flush pending commands */
14931713e81bSScott Long 	mvSataFlushDmaQueue (pMvSataAdapter, channel, MV_FLUSH_TYPE_CALLBACK);
14941713e81bSScott Long 
14951713e81bSScott Long 	/* Software reset channel */
1496d2bd3ab9SScott Long 	if (mvStorageDevATASoftResetDevice(pMvSataAdapter, channel) == MV_FALSE)
1497d2bd3ab9SScott Long 	{
149864470755SXin LI 		MV_ERROR("RR18xx [%d,%d]: failed to perform Software reset\n",
14991713e81bSScott Long 				 pMvSataAdapter->adapterId, channel);
1500d2bd3ab9SScott Long 		hptmv_free_channel(pAdapter, channel);
15011713e81bSScott Long 		return -1;
15021713e81bSScott Long 	}
15031713e81bSScott Long 
15041713e81bSScott Long 	/* Hardware reset channel */
1505d2bd3ab9SScott Long 	if (mvSataChannelHardReset(pMvSataAdapter, channel)== MV_FALSE)
1506d2bd3ab9SScott Long 	{
150764470755SXin LI 		MV_ERROR("RR18xx [%d,%d] Failed to Hard reser the SATA channel\n",
1508d2bd3ab9SScott Long 				 pMvSataAdapter->adapterId, channel);
15091713e81bSScott Long 		hptmv_free_channel(pAdapter, channel);
15101713e81bSScott Long 		return -1;
15111713e81bSScott Long 	}
15121713e81bSScott Long 
1513d2bd3ab9SScott Long 	if (mvSataIsStorageDeviceConnected(pMvSataAdapter, channel) == MV_FALSE)
1514d2bd3ab9SScott Long 	{
151564470755SXin LI 		 MV_ERROR("RR18xx [%d,%d] Failed to Connect Device\n",
15161713e81bSScott Long 				 pMvSataAdapter->adapterId, channel);
15171713e81bSScott Long 		hptmv_free_channel(pAdapter, channel);
15181713e81bSScott Long 		return -1;
1519d2bd3ab9SScott Long 	}else
1520d2bd3ab9SScott Long 	{
1521d2bd3ab9SScott Long 		MV_ERROR("channel %d: perform recalibrate command", channel);
1522d2bd3ab9SScott Long 		if (!mvStorageDevATAExecuteNonUDMACommand(pMvSataAdapter, channel,
1523d2bd3ab9SScott Long 								MV_NON_UDMA_PROTOCOL_NON_DATA,
1524d2bd3ab9SScott Long 								MV_FALSE,
1525d2bd3ab9SScott Long 								NULL,	 /* pBuffer*/
1526d2bd3ab9SScott Long 								0,		 /* count  */
1527d2bd3ab9SScott Long 								0,		/*features*/
1528d2bd3ab9SScott Long 										/* sectorCount */
1529d2bd3ab9SScott Long 								0,
1530d2bd3ab9SScott Long 								0,	/* lbaLow */
1531d2bd3ab9SScott Long 								0,	/* lbaMid */
1532d2bd3ab9SScott Long 									/* lbaHigh */
1533d2bd3ab9SScott Long 								0,
1534d2bd3ab9SScott Long 								0,		/* device */
1535d2bd3ab9SScott Long 										/* command */
1536d2bd3ab9SScott Long 								0x10))
1537d2bd3ab9SScott Long 			MV_ERROR("channel %d: recalibrate failed", channel);
1538d2bd3ab9SScott Long 
15391713e81bSScott Long 		/* Set transfer mode */
15401713e81bSScott Long 		if((mvStorageDevATASetFeatures(pMvSataAdapter, channel,
1541d2bd3ab9SScott Long 						MV_ATA_SET_FEATURES_TRANSFER,
1542d2bd3ab9SScott Long 						MV_ATA_TRANSFER_PIO_SLOW, 0, 0, 0) == MV_FALSE) ||
15431713e81bSScott Long 			(mvStorageDevATASetFeatures(pMvSataAdapter, channel,
15441713e81bSScott Long 						MV_ATA_SET_FEATURES_TRANSFER,
1545d2bd3ab9SScott Long 						pAdapter->mvChannel[channel].maxPioModeSupported, 0, 0, 0) == MV_FALSE) ||
1546d2bd3ab9SScott Long 			(mvStorageDevATASetFeatures(pMvSataAdapter, channel,
1547d2bd3ab9SScott Long 						MV_ATA_SET_FEATURES_TRANSFER,
1548d2bd3ab9SScott Long 						pAdapter->mvChannel[channel].maxUltraDmaModeSupported, 0, 0, 0) == MV_FALSE) )
1549d2bd3ab9SScott Long 		{
15501713e81bSScott Long 			MV_ERROR("channel %d: Set Features failed", channel);
15511713e81bSScott Long 			hptmv_free_channel(pAdapter, channel);
15521713e81bSScott Long 			return -1;
15531713e81bSScott Long 		}
15541713e81bSScott Long 		/* Enable EDMA */
1555d2bd3ab9SScott Long 		if (mvSataEnableChannelDma(pMvSataAdapter, channel) == MV_FALSE)
1556d2bd3ab9SScott Long 		{
15571713e81bSScott Long 			MV_ERROR("Failed to enable DMA, channel=%d", channel);
15581713e81bSScott Long 			hptmv_free_channel(pAdapter, channel);
15591713e81bSScott Long 			return -1;
15601713e81bSScott Long 		}
15611713e81bSScott Long 	}
15621713e81bSScott Long 	return 0;
15631713e81bSScott Long }
15641713e81bSScott Long 
15651713e81bSScott Long static int
15661713e81bSScott Long fResetActiveCommands(PVBus _vbus_p)
15671713e81bSScott Long {
1568d2bd3ab9SScott Long 	MV_SATA_ADAPTER *pMvSataAdapter = &((IAL_ADAPTER_T *)_vbus_p->OsExt)->mvSataAdapter;
15691713e81bSScott Long 	MV_U8 channel;
15701713e81bSScott Long 	for (channel=0;channel< MV_SATA_CHANNELS_NUM;channel++) {
1571d2bd3ab9SScott Long 		if (pMvSataAdapter->sataChannel[channel] && pMvSataAdapter->sataChannel[channel]->outstandingCommands)
1572d2bd3ab9SScott Long 			MvSataResetChannel(pMvSataAdapter,channel);
15731713e81bSScott Long 	}
15741713e81bSScott Long 	return 0;
15751713e81bSScott Long }
15761713e81bSScott Long 
1577d2bd3ab9SScott Long void fCompleteAllCommandsSynchronously(PVBus _vbus_p)
15781713e81bSScott Long {
15791713e81bSScott Long 	UINT cont;
15801713e81bSScott Long 	ULONG ticks = 0;
15811713e81bSScott Long 	MV_U8 channel;
1582d2bd3ab9SScott Long 	MV_SATA_ADAPTER *pMvSataAdapter = &((IAL_ADAPTER_T *)_vbus_p->OsExt)->mvSataAdapter;
15831713e81bSScott Long 	MV_SATA_CHANNEL *pMvSataChannel;
15841713e81bSScott Long 
15851713e81bSScott Long 	do {
15861713e81bSScott Long check_cmds:
15871713e81bSScott Long 		cont = 0;
15881713e81bSScott Long 		CheckPendingCall(_VBUS_P0);
15891713e81bSScott Long #ifdef _RAID5N_
15901713e81bSScott Long 		dataxfer_poll();
15911713e81bSScott Long 		xor_poll();
15921713e81bSScott Long #endif
15931713e81bSScott Long 		for (channel=0;channel< MV_SATA_CHANNELS_NUM;channel++) {
15941713e81bSScott Long 			pMvSataChannel = pMvSataAdapter->sataChannel[channel];
1595d2bd3ab9SScott Long 			if (pMvSataChannel && pMvSataChannel->outstandingCommands)
1596d2bd3ab9SScott Long 			{
15971713e81bSScott Long 				while (pMvSataChannel->outstandingCommands) {
1598d2bd3ab9SScott Long 					if (!mvSataInterruptServiceRoutine(pMvSataAdapter)) {
15991713e81bSScott Long 						StallExec(1000);
16001713e81bSScott Long 						if (ticks++ > 3000) {
1601d2bd3ab9SScott Long 							MvSataResetChannel(pMvSataAdapter,channel);
16021713e81bSScott Long 							goto check_cmds;
16031713e81bSScott Long 						}
1604d2bd3ab9SScott Long 					}
1605d2bd3ab9SScott Long 					else
16061713e81bSScott Long 						ticks = 0;
16071713e81bSScott Long 				}
16081713e81bSScott Long 				cont = 1;
16091713e81bSScott Long 			}
16101713e81bSScott Long 		}
16111713e81bSScott Long 	} while (cont);
16121713e81bSScott Long }
16131713e81bSScott Long 
16141713e81bSScott Long void
16151713e81bSScott Long fResetVBus(_VBUS_ARG0)
16161713e81bSScott Long {
16177d9aed9cSScott Long 	KdPrint(("fMvResetBus(%p)", _vbus_p));
16181713e81bSScott Long 
16191713e81bSScott Long 	/* some commands may already finished. */
16201713e81bSScott Long 	CheckPendingCall(_VBUS_P0);
16211713e81bSScott Long 
16221713e81bSScott Long 	fResetActiveCommands(_vbus_p);
16231713e81bSScott Long 	/*
16241713e81bSScott Long 	 * the other pending commands may still be finished successfully.
16251713e81bSScott Long 	 */
16261713e81bSScott Long 	fCompleteAllCommandsSynchronously(_vbus_p);
16271713e81bSScott Long 
16281713e81bSScott Long 	/* Now there should be no pending commands. No more action needed. */
16291713e81bSScott Long 	CheckIdleCall(_VBUS_P0);
16301713e81bSScott Long 
16311713e81bSScott Long 	KdPrint(("fMvResetBus() done"));
16321713e81bSScott Long }
16331713e81bSScott Long 
1634d2bd3ab9SScott Long /*No rescan function*/
16351713e81bSScott Long void
16361713e81bSScott Long fRescanAllDevice(_VBUS_ARG0)
16371713e81bSScott Long {
16381713e81bSScott Long }
16391713e81bSScott Long 
16401713e81bSScott Long static MV_BOOLEAN
1641d2bd3ab9SScott Long CommandCompletionCB(MV_SATA_ADAPTER *pMvSataAdapter,
1642d2bd3ab9SScott Long 					MV_U8 channelNum,
1643d2bd3ab9SScott Long 					MV_COMPLETION_TYPE comp_type,
1644d2bd3ab9SScott Long 					MV_VOID_PTR commandId,
1645d2bd3ab9SScott Long 					MV_U16 responseFlags,
1646d2bd3ab9SScott Long 					MV_U32 timeStamp,
1647d2bd3ab9SScott Long 					MV_STORAGE_DEVICE_REGISTERS *registerStruct)
16481713e81bSScott Long {
16491713e81bSScott Long 	PCommand pCmd = (PCommand) commandId;
16501713e81bSScott Long 	_VBUS_INST(pCmd->pVDevice->pVBus)
16511713e81bSScott Long 
16521713e81bSScott Long 	if (pCmd->uScratch.sata_param.prdAddr)
1653d2bd3ab9SScott Long 		FreePRDTable(pMvSataAdapter->IALData,pCmd->uScratch.sata_param.prdAddr);
16541713e81bSScott Long 
1655d2bd3ab9SScott Long 	switch (comp_type)
1656d2bd3ab9SScott Long 	{
16571713e81bSScott Long 	case MV_COMPLETION_TYPE_NORMAL:
16581713e81bSScott Long 		pCmd->Result = RETURN_SUCCESS;
16591713e81bSScott Long 		break;
16601713e81bSScott Long 	case MV_COMPLETION_TYPE_ABORT:
16611713e81bSScott Long 		pCmd->Result = RETURN_BUS_RESET;
16621713e81bSScott Long 		break;
16631713e81bSScott Long 	case MV_COMPLETION_TYPE_ERROR:
1664d2bd3ab9SScott Long 		 MV_ERROR("IAL: COMPLETION ERROR, adapter %d, channel %d, flags=%x\n",
1665d2bd3ab9SScott Long 				 pMvSataAdapter->adapterId, channelNum, responseFlags);
16661713e81bSScott Long 
16671713e81bSScott Long 		if (responseFlags & 4) {
1668d2bd3ab9SScott Long 			MV_ERROR("ATA regs: error %x, sector count %x, LBA low %x, LBA mid %x,"
1669d2bd3ab9SScott Long 				" LBA high %x, device %x, status %x\n",
1670d2bd3ab9SScott Long 				registerStruct->errorRegister,
16711713e81bSScott Long 				registerStruct->sectorCountRegister,
16721713e81bSScott Long 				registerStruct->lbaLowRegister,
16731713e81bSScott Long 				registerStruct->lbaMidRegister,
16741713e81bSScott Long 				registerStruct->lbaHighRegister,
16751713e81bSScott Long 				registerStruct->deviceRegister,
16761713e81bSScott Long 				registerStruct->statusRegister);
16771713e81bSScott Long 		}
1678d2bd3ab9SScott Long 		/*We can't do handleEdmaError directly here, because CommandCompletionCB is called by
1679d2bd3ab9SScott Long 		 * mv's ISR, if we retry the command, than the internel data structure may be destroyed*/
16801713e81bSScott Long 		pCmd->uScratch.sata_param.responseFlags = responseFlags;
1681d2bd3ab9SScott Long 		pCmd->uScratch.sata_param.bIdeStatus = registerStruct->statusRegister;
1682d2bd3ab9SScott Long 		pCmd->uScratch.sata_param.errorRegister = registerStruct->errorRegister;
16831713e81bSScott Long 		pCmd->pVDevice->u.disk.QueueLength--;
16841713e81bSScott Long 		CallAfterReturn(_VBUS_P (DPC_PROC)handleEdmaError,pCmd);
16851713e81bSScott Long 		return TRUE;
16861713e81bSScott Long 
16871713e81bSScott Long 	default:
16881713e81bSScott Long 		MV_ERROR(" Unknown completion type (%d)\n", comp_type);
16891713e81bSScott Long 		return MV_FALSE;
16901713e81bSScott Long 	}
16911713e81bSScott Long 
1692d2bd3ab9SScott Long 	if (pCmd->uCmd.Ide.Command == IDE_COMMAND_VERIFY && pCmd->uScratch.sata_param.cmd_priv > 1) {
16931713e81bSScott Long 		pCmd->uScratch.sata_param.cmd_priv --;
16941713e81bSScott Long 		return TRUE;
16951713e81bSScott Long 	}
16961713e81bSScott Long 	pCmd->pVDevice->u.disk.QueueLength--;
16971713e81bSScott Long 	CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
16981713e81bSScott Long 	return TRUE;
16991713e81bSScott Long }
17001713e81bSScott Long 
17011713e81bSScott Long void
17021713e81bSScott Long fDeviceSendCommand(_VBUS_ARG PCommand pCmd)
17031713e81bSScott Long {
17041713e81bSScott Long 	MV_SATA_EDMA_PRD_ENTRY  *pPRDTable = 0;
17051713e81bSScott Long 	MV_SATA_ADAPTER *pMvSataAdapter;
17061713e81bSScott Long 	MV_SATA_CHANNEL *pMvSataChannel;
1707d2bd3ab9SScott Long 	PVDevice pVDevice = pCmd->pVDevice;
1708d2bd3ab9SScott Long 	PDevice  pDevice = &pVDevice->u.disk;
170964470755SXin LI 	LBA_T    Lba = pCmd->uCmd.Ide.Lba;
1710d2bd3ab9SScott Long 	USHORT   nSector = pCmd->uCmd.Ide.nSectors;
1711d2bd3ab9SScott Long 
17121713e81bSScott Long 	MV_QUEUE_COMMAND_RESULT result;
17131713e81bSScott Long 	MV_QUEUE_COMMAND_INFO commandInfo;
1714d2bd3ab9SScott Long 	MV_UDMA_COMMAND_PARAMS  *pUdmaParams = &commandInfo.commandParams.udmaCommand;
1715d2bd3ab9SScott Long 	MV_NONE_UDMA_COMMAND_PARAMS *pNoUdmaParams = &commandInfo.commandParams.NoneUdmaCommand;
1716d2bd3ab9SScott Long 
171764470755SXin LI 	MV_BOOLEAN is48bit;
17181713e81bSScott Long 	MV_U8      channel;
17191713e81bSScott Long 	int        i=0;
17201713e81bSScott Long 
17211713e81bSScott Long 	DECLARE_BUFFER(FPSCAT_GATH, tmpSg);
17221713e81bSScott Long 
17231713e81bSScott Long 	if (!pDevice->df_on_line) {
17241713e81bSScott Long 		MV_ERROR("Device is offline");
17251713e81bSScott Long 		pCmd->Result = RETURN_BAD_DEVICE;
17261713e81bSScott Long 		CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
17271713e81bSScott Long 		return;
17281713e81bSScott Long 	}
17291713e81bSScott Long 
17301713e81bSScott Long 	pDevice->HeadPosition = pCmd->uCmd.Ide.Lba + pCmd->uCmd.Ide.nSectors;
17311713e81bSScott Long 	pMvSataChannel = pDevice->mv;
17321713e81bSScott Long 	pMvSataAdapter = pMvSataChannel->mvSataAdapter;
17331713e81bSScott Long 	channel = pMvSataChannel->channelNumber;
17341713e81bSScott Long 
1735d2bd3ab9SScott Long 	/* old RAID0 has hidden lba. Remember to clear dDeHiddenLba when delete array! */
17361713e81bSScott Long 	Lba += pDevice->dDeHiddenLba;
17371713e81bSScott Long 	/* check LBA */
17381713e81bSScott Long 	if (Lba+nSector-1 > pDevice->dDeRealCapacity) {
17391713e81bSScott Long 		pCmd->Result = RETURN_INVALID_REQUEST;
17401713e81bSScott Long 		CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
17411713e81bSScott Long 		return;
17421713e81bSScott Long 	}
17431713e81bSScott Long 
174464470755SXin LI 	/*
174564470755SXin LI 	 * always use 48bit LBA if drive supports it.
174664470755SXin LI 	 * Some Seagate drives report error if you use a 28-bit command
174764470755SXin LI 	 * to access sector 0xfffffff.
174864470755SXin LI 	 */
174964470755SXin LI 	is48bit = pMvSataChannel->lba48Address;
17501713e81bSScott Long 
1751d2bd3ab9SScott Long 	switch (pCmd->uCmd.Ide.Command)
1752d2bd3ab9SScott Long 	{
17531713e81bSScott Long 	case IDE_COMMAND_READ:
17541713e81bSScott Long 	case IDE_COMMAND_WRITE:
17551713e81bSScott Long 		if (pDevice->bDeModeSetting<8) goto pio;
17561713e81bSScott Long 
17571713e81bSScott Long 		commandInfo.type = MV_QUEUED_COMMAND_TYPE_UDMA;
17581713e81bSScott Long 		pUdmaParams->isEXT = is48bit;
17591713e81bSScott Long 		pUdmaParams->numOfSectors = nSector;
17601713e81bSScott Long 		pUdmaParams->lowLBAAddress = Lba;
17611713e81bSScott Long 		pUdmaParams->highLBAAddress = 0;
17621713e81bSScott Long 		pUdmaParams->prdHighAddr = 0;
17631713e81bSScott Long 		pUdmaParams->callBack = CommandCompletionCB;
17641713e81bSScott Long 		pUdmaParams->commandId = (MV_VOID_PTR )pCmd;
17651713e81bSScott Long 		if(pCmd->uCmd.Ide.Command == IDE_COMMAND_READ)
17661713e81bSScott Long 			pUdmaParams->readWrite = MV_UDMA_TYPE_READ;
17671713e81bSScott Long 		else
17681713e81bSScott Long 			pUdmaParams->readWrite = MV_UDMA_TYPE_WRITE;
17691713e81bSScott Long 
17701713e81bSScott Long 		if (pCmd->pSgTable && pCmd->cf_physical_sg) {
17711713e81bSScott Long 			FPSCAT_GATH sg1=tmpSg, sg2=pCmd->pSgTable;
1772d2bd3ab9SScott Long 			do { *sg1++=*sg2; } while ((sg2++->wSgFlag & SG_FLAG_EOT)==0);
1773d2bd3ab9SScott Long 		}
1774d2bd3ab9SScott Long 		else {
1775d2bd3ab9SScott Long 			if (!pCmd->pfnBuildSgl || !pCmd->pfnBuildSgl(_VBUS_P pCmd, tmpSg, 0)) {
17761713e81bSScott Long pio:
17771713e81bSScott Long 				mvSataDisableChannelDma(pMvSataAdapter, channel);
1778d2bd3ab9SScott Long 				mvSataFlushDmaQueue(pMvSataAdapter, channel, MV_FLUSH_TYPE_CALLBACK);
17791713e81bSScott Long 
17801713e81bSScott Long 				if (pCmd->pSgTable && pCmd->cf_physical_sg==0) {
17811713e81bSScott Long 					FPSCAT_GATH sg1=tmpSg, sg2=pCmd->pSgTable;
1782d2bd3ab9SScott Long 					do { *sg1++=*sg2; } while ((sg2++->wSgFlag & SG_FLAG_EOT)==0);
1783d2bd3ab9SScott Long 				}
1784d2bd3ab9SScott Long 				else {
1785d2bd3ab9SScott Long 					if (!pCmd->pfnBuildSgl || !pCmd->pfnBuildSgl(_VBUS_P pCmd, tmpSg, 1)) {
17861713e81bSScott Long 						pCmd->Result = RETURN_NEED_LOGICAL_SG;
17871713e81bSScott Long 						goto finish_cmd;
17881713e81bSScott Long 					}
1789d2bd3ab9SScott Long 				}
17901713e81bSScott Long 
17911713e81bSScott Long 				do {
1792d2bd3ab9SScott Long 					ULONG size = tmpSg->wSgSize? tmpSg->wSgSize : 0x10000;
17931713e81bSScott Long 					ULONG_PTR addr = tmpSg->dSgAddress;
17941713e81bSScott Long 					if (size & 0x1ff) {
17951713e81bSScott Long 						pCmd->Result = RETURN_INVALID_REQUEST;
17961713e81bSScott Long 						goto finish_cmd;
17971713e81bSScott Long 					}
1798d2bd3ab9SScott Long 					if (mvStorageDevATAExecuteNonUDMACommand(pMvSataAdapter, channel,
1799d2bd3ab9SScott Long 						(pCmd->cf_data_out)?MV_NON_UDMA_PROTOCOL_PIO_DATA_OUT:MV_NON_UDMA_PROTOCOL_PIO_DATA_IN,
1800d2bd3ab9SScott Long 						is48bit,
1801d2bd3ab9SScott Long 						(MV_U16_PTR)addr,
18021713e81bSScott Long 						size >> 1,	/* count       */
18031713e81bSScott Long 						0,		/* features  N/A  */
18041713e81bSScott Long 						(MV_U16)(size>>9),	/*sector count*/
1805d2bd3ab9SScott Long 						(MV_U16)(  (is48bit? (MV_U16)((Lba >> 16) & 0xFF00) : 0 )  | (UCHAR)(Lba & 0xFF) ), /*lbalow*/
18061713e81bSScott Long 						(MV_U16)((Lba >> 8) & 0xFF), /* lbaMid      */
1807d2bd3ab9SScott Long 						(MV_U16)((Lba >> 16) & 0xFF),/* lbaHigh     */
1808d2bd3ab9SScott Long 						(MV_U8)(0x40 | (is48bit ? 0 : (UCHAR)(Lba >> 24) & 0xFF )),/* device      */
1809d2bd3ab9SScott Long 						(MV_U8)(is48bit ? (pCmd->cf_data_in?IDE_COMMAND_READ_EXT:IDE_COMMAND_WRITE_EXT):pCmd->uCmd.Ide.Command)
1810d2bd3ab9SScott Long 					)==MV_FALSE)
1811d2bd3ab9SScott Long 					{
18121713e81bSScott Long 						pCmd->Result = RETURN_IDE_ERROR;
18131713e81bSScott Long 						goto finish_cmd;
18141713e81bSScott Long 					}
18151713e81bSScott Long 					Lba += size>>9;
18161713e81bSScott Long 					if(Lba & 0xF0000000) is48bit = MV_TRUE;
18171713e81bSScott Long 				}
18181713e81bSScott Long 				while ((tmpSg++->wSgFlag & SG_FLAG_EOT)==0);
18191713e81bSScott Long 				pCmd->Result = RETURN_SUCCESS;
18201713e81bSScott Long finish_cmd:
18211713e81bSScott Long 				mvSataEnableChannelDma(pMvSataAdapter,channel);
1822d2bd3ab9SScott Long 				CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
18231713e81bSScott Long 				return;
18241713e81bSScott Long 			}
1825d2bd3ab9SScott Long 		}
18261713e81bSScott Long 
1827d2bd3ab9SScott Long 		pPRDTable = (MV_SATA_EDMA_PRD_ENTRY *) AllocatePRDTable(pMvSataAdapter->IALData);
18287d9aed9cSScott Long 		KdPrint(("pPRDTable:%p\n",pPRDTable));
18291713e81bSScott Long 		if (!pPRDTable) {
18301713e81bSScott Long 			pCmd->Result = RETURN_DEVICE_BUSY;
1831d2bd3ab9SScott Long 			CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
18321713e81bSScott Long 			HPT_ASSERT(0);
18331713e81bSScott Long 			return;
18341713e81bSScott Long 		}
18351713e81bSScott Long 
18361713e81bSScott Long 		do{
1837d2bd3ab9SScott Long 			pPRDTable[i].highBaseAddr = (sizeof(tmpSg->dSgAddress)>4 ? (MV_U32)(tmpSg->dSgAddress>>32) : 0);
18381713e81bSScott Long 			pPRDTable[i].flags = (MV_U16)tmpSg->wSgFlag;
18391713e81bSScott Long 			pPRDTable[i].byteCount = (MV_U16)tmpSg->wSgSize;
18401713e81bSScott Long 			pPRDTable[i].lowBaseAddr = (MV_U32)tmpSg->dSgAddress;
18411713e81bSScott Long 			pPRDTable[i].reserved = 0;
18421713e81bSScott Long 			i++;
18431713e81bSScott Long 		}while((tmpSg++->wSgFlag & SG_FLAG_EOT)==0);
18441713e81bSScott Long 
1845d2bd3ab9SScott Long 		pUdmaParams->prdLowAddr = (ULONG)fOsPhysicalAddress(pPRDTable);
1846d2bd3ab9SScott Long 		if ((pUdmaParams->numOfSectors == 256) && (pMvSataChannel->lba48Address == MV_FALSE)) {
18471713e81bSScott Long 			pUdmaParams->numOfSectors = 0;
18481713e81bSScott Long 		}
18491713e81bSScott Long 
18501713e81bSScott Long 		pCmd->uScratch.sata_param.prdAddr = (PVOID)pPRDTable;
18511713e81bSScott Long 
1852d2bd3ab9SScott Long 		result = mvSataQueueCommand(pMvSataAdapter, channel, &commandInfo);
18531713e81bSScott Long 
1854d2bd3ab9SScott Long 		if (result != MV_QUEUE_COMMAND_RESULT_OK)
1855d2bd3ab9SScott Long 		{
18561713e81bSScott Long queue_failed:
1857d2bd3ab9SScott Long 			switch (result)
1858d2bd3ab9SScott Long 			{
18591713e81bSScott Long 			case MV_QUEUE_COMMAND_RESULT_BAD_LBA_ADDRESS:
1860d2bd3ab9SScott Long 				MV_ERROR("IAL Error: Edma Queue command failed. Bad LBA "
1861d2bd3ab9SScott Long 						 "LBA[31:0](0x%08x)\n", pUdmaParams->lowLBAAddress);
18621713e81bSScott Long 				pCmd->Result = RETURN_IDE_ERROR;
18631713e81bSScott Long 				break;
18641713e81bSScott Long 			case MV_QUEUE_COMMAND_RESULT_QUEUED_MODE_DISABLED:
1865d2bd3ab9SScott Long 				MV_ERROR("IAL Error: Edma Queue command failed. EDMA"
1866d2bd3ab9SScott Long 						 " disabled adapter %d channel %d\n",
18671713e81bSScott Long 						 pMvSataAdapter->adapterId, channel);
18681713e81bSScott Long 				mvSataEnableChannelDma(pMvSataAdapter,channel);
18691713e81bSScott Long 				pCmd->Result = RETURN_IDE_ERROR;
18701713e81bSScott Long 				break;
18711713e81bSScott Long 			case MV_QUEUE_COMMAND_RESULT_FULL:
1872d2bd3ab9SScott Long 				MV_ERROR("IAL Error: Edma Queue command failed. Queue is"
1873d2bd3ab9SScott Long 						 " Full adapter %d channel %d\n",
18741713e81bSScott Long 						 pMvSataAdapter->adapterId, channel);
18751713e81bSScott Long 				pCmd->Result = RETURN_DEVICE_BUSY;
18761713e81bSScott Long 				break;
18771713e81bSScott Long 			case MV_QUEUE_COMMAND_RESULT_BAD_PARAMS:
1878d2bd3ab9SScott Long 				MV_ERROR("IAL Error: Edma Queue command failed. (Bad "
1879d2bd3ab9SScott Long 						 "Params), pMvSataAdapter: %p,  pSataChannel: %p.\n",
1880d2bd3ab9SScott Long 						 pMvSataAdapter, pMvSataAdapter->sataChannel[channel]);
18811713e81bSScott Long 				pCmd->Result = RETURN_IDE_ERROR;
18821713e81bSScott Long 				break;
18831713e81bSScott Long 			default:
1884d2bd3ab9SScott Long 				MV_ERROR("IAL Error: Bad result value (%d) from queue"
1885d2bd3ab9SScott Long 						 " command\n", result);
18861713e81bSScott Long 				pCmd->Result = RETURN_IDE_ERROR;
18871713e81bSScott Long 			}
18881713e81bSScott Long 			if(pPRDTable)
1889d2bd3ab9SScott Long 				FreePRDTable(pMvSataAdapter->IALData,pPRDTable);
1890d2bd3ab9SScott Long 			CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
18911713e81bSScott Long 		}
18921713e81bSScott Long 		pDevice->QueueLength++;
18931713e81bSScott Long 		return;
18941713e81bSScott Long 
18951713e81bSScott Long 	case IDE_COMMAND_VERIFY:
18961713e81bSScott Long 		commandInfo.type = MV_QUEUED_COMMAND_TYPE_NONE_UDMA;
18971713e81bSScott Long 		pNoUdmaParams->bufPtr = NULL;
18981713e81bSScott Long 		pNoUdmaParams->callBack = CommandCompletionCB;
18991713e81bSScott Long 		pNoUdmaParams->commandId = (MV_VOID_PTR)pCmd;
19001713e81bSScott Long 		pNoUdmaParams->count = 0;
19011713e81bSScott Long 		pNoUdmaParams->features = 0;
19021713e81bSScott Long 		pNoUdmaParams->protocolType = MV_NON_UDMA_PROTOCOL_NON_DATA;
19031713e81bSScott Long 
19041713e81bSScott Long 		pCmd->uScratch.sata_param.cmd_priv = 1;
19051713e81bSScott Long 		if (pMvSataChannel->lba48Address == MV_TRUE){
1906d2bd3ab9SScott Long 			pNoUdmaParams->command = MV_ATA_COMMAND_READ_VERIFY_SECTORS_EXT;
19071713e81bSScott Long 			pNoUdmaParams->isEXT = MV_TRUE;
1908d2bd3ab9SScott Long 			pNoUdmaParams->lbaHigh = (MV_U16)((Lba & 0xff0000) >> 16);
19091713e81bSScott Long 			pNoUdmaParams->lbaMid = (MV_U16)((Lba & 0xff00) >> 8);
19101713e81bSScott Long 			pNoUdmaParams->lbaLow =
19111713e81bSScott Long 				(MV_U16)(((Lba & 0xff000000) >> 16)| (Lba & 0xff));
19121713e81bSScott Long 			pNoUdmaParams->sectorCount = nSector;
19131713e81bSScott Long 			pNoUdmaParams->device = 0x40;
1914d2bd3ab9SScott Long 			result = mvSataQueueCommand(pMvSataAdapter, channel, &commandInfo);
19151713e81bSScott Long 			if (result != MV_QUEUE_COMMAND_RESULT_OK){
19161713e81bSScott Long 				goto queue_failed;
19171713e81bSScott Long 			}
19181713e81bSScott Long 			return;
19191713e81bSScott Long 		}
1920d2bd3ab9SScott Long 		else{
19211713e81bSScott Long 			pNoUdmaParams->command = MV_ATA_COMMAND_READ_VERIFY_SECTORS;
19221713e81bSScott Long 			pNoUdmaParams->isEXT = MV_FALSE;
19231713e81bSScott Long 			pNoUdmaParams->lbaHigh = (MV_U16)((Lba & 0xff0000) >> 16);
19241713e81bSScott Long 			pNoUdmaParams->lbaMid = (MV_U16)((Lba & 0xff00) >> 8);
19251713e81bSScott Long 			pNoUdmaParams->lbaLow = (MV_U16)(Lba & 0xff);
19261713e81bSScott Long 			pNoUdmaParams->sectorCount = 0xff & nSector;
19271713e81bSScott Long 			pNoUdmaParams->device = (MV_U8)(0x40 |
19281713e81bSScott Long 				((Lba & 0xf000000) >> 24));
19291713e81bSScott Long 			pNoUdmaParams->callBack = CommandCompletionCB;
1930d2bd3ab9SScott Long 			result = mvSataQueueCommand(pMvSataAdapter, channel, &commandInfo);
1931d2bd3ab9SScott Long 			/*FIXME: how about the commands already queued? but marvel also forgets to consider this*/
19321713e81bSScott Long 			if (result != MV_QUEUE_COMMAND_RESULT_OK){
19331713e81bSScott Long 				goto queue_failed;
19341713e81bSScott Long 			}
1935d2bd3ab9SScott Long 		}
19361713e81bSScott Long 		break;
19371713e81bSScott Long 	default:
19381713e81bSScott Long 		pCmd->Result = RETURN_INVALID_REQUEST;
19391713e81bSScott Long 		CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
19401713e81bSScott Long 		break;
19411713e81bSScott Long 	}
19421713e81bSScott Long }
19431713e81bSScott Long 
19441713e81bSScott Long /**********************************************************
19451713e81bSScott Long  *
19461713e81bSScott Long  *	Probe the hostadapter.
19471713e81bSScott Long  *
19481713e81bSScott Long  **********************************************************/
19491713e81bSScott Long static int
19501713e81bSScott Long hpt_probe(device_t dev)
19511713e81bSScott Long {
19521713e81bSScott Long 	if ((pci_get_vendor(dev) == MV_SATA_VENDOR_ID) &&
19531713e81bSScott Long 		(pci_get_device(dev) == MV_SATA_DEVICE_ID_5081
19541713e81bSScott Long #ifdef FOR_DEMO
19551713e81bSScott Long 		|| pci_get_device(dev) == MV_SATA_DEVICE_ID_5080
19561713e81bSScott Long #endif
1957d2bd3ab9SScott Long 		))
1958d2bd3ab9SScott Long 	{
19591713e81bSScott Long 		KdPrintI((CONTROLLER_NAME " found\n"));
19601713e81bSScott Long 		device_set_desc(dev, CONTROLLER_NAME);
19617634a04bSXin LI 		return (BUS_PROBE_DEFAULT);
19621713e81bSScott Long 	}
19631713e81bSScott Long 	else
19641713e81bSScott Long 		return(ENXIO);
19651713e81bSScott Long }
19661713e81bSScott Long 
19671713e81bSScott Long /***********************************************************
19681713e81bSScott Long  *
19691713e81bSScott Long  *      Auto configuration:  attach and init a host adapter.
19701713e81bSScott Long  *
19711713e81bSScott Long  ***********************************************************/
19721713e81bSScott Long static int
19731713e81bSScott Long hpt_attach(device_t dev)
19741713e81bSScott Long {
1975d2bd3ab9SScott Long 	IAL_ADAPTER_T * pAdapter = device_get_softc(dev);
19761713e81bSScott Long 	int rid;
19771713e81bSScott Long 	union ccb *ccb;
19781713e81bSScott Long 	struct cam_devq *devq;
19791713e81bSScott Long 	struct cam_sim *hpt_vsim;
19801713e81bSScott Long 
198149b3fc40SJohn Baldwin 	device_printf(dev, "%s Version %s \n", DRIVER_NAME, DRIVER_VERSION);
1982d2bd3ab9SScott Long 
19831713e81bSScott Long 	pAdapter->hpt_dev = dev;
19841713e81bSScott Long 
19851713e81bSScott Long 	rid = init_adapter(pAdapter);
19861713e81bSScott Long 	if (rid)
19871713e81bSScott Long 		return rid;
19881713e81bSScott Long 
19891713e81bSScott Long 	rid = 0;
199043cd6160SJustin Hibbits 	if ((pAdapter->hpt_irq = bus_alloc_resource_any(pAdapter->hpt_dev, SYS_RES_IRQ, &rid, RF_SHAREABLE | RF_ACTIVE)) == NULL)
1991d2bd3ab9SScott Long 	{
19921713e81bSScott Long 		hpt_printk(("can't allocate interrupt\n"));
19931713e81bSScott Long 		return(ENXIO);
19941713e81bSScott Long 	}
19951713e81bSScott Long 
199649b3fc40SJohn Baldwin 	if (bus_setup_intr(pAdapter->hpt_dev, pAdapter->hpt_irq,
199749b3fc40SJohn Baldwin 				INTR_TYPE_CAM | INTR_MPSAFE,
199864470755SXin LI 				NULL, hpt_intr, pAdapter, &pAdapter->hpt_intr))
1999d2bd3ab9SScott Long 	{
20001713e81bSScott Long 		hpt_printk(("can't set up interrupt\n"));
20011713e81bSScott Long 		free(pAdapter, M_DEVBUF);
20021713e81bSScott Long 		return(ENXIO);
20031713e81bSScott Long 	}
20041713e81bSScott Long 
2005d2bd3ab9SScott Long 
20063bd46ad7SEdward Tomasz Napierala 	ccb = xpt_alloc_ccb();
20071713e81bSScott Long 	ccb->ccb_h.pinfo.priority = 1;
20081713e81bSScott Long 	ccb->ccb_h.pinfo.index = CAM_UNQUEUED_INDEX;
20093bd46ad7SEdward Tomasz Napierala 
20101713e81bSScott Long 	/*
20111713e81bSScott Long 	 * Create the device queue for our SIM(s).
20121713e81bSScott Long 	 */
2013d2bd3ab9SScott Long 	if((devq = cam_simq_alloc(8/*MAX_QUEUE_COMM*/)) == NULL)
2014d2bd3ab9SScott Long 	{
20151713e81bSScott Long 		KdPrint(("ENXIO\n"));
20161713e81bSScott Long 		return ENOMEM;
20171713e81bSScott Long 	}
20181713e81bSScott Long 
20191713e81bSScott Long 	/*
20201713e81bSScott Long 	 * Construct our SIM entry
20211713e81bSScott Long 	 */
202264470755SXin LI 	hpt_vsim = cam_sim_alloc(hpt_action, hpt_poll, __str(PROC_DIR_NAME),
202349b3fc40SJohn Baldwin 			pAdapter, device_get_unit(pAdapter->hpt_dev),
202449b3fc40SJohn Baldwin 			&pAdapter->lock, 1, 8, devq);
202564470755SXin LI 	if (hpt_vsim == NULL) {
20261713e81bSScott Long 		cam_simq_free(devq);
20271713e81bSScott Long 		return ENOMEM;
20281713e81bSScott Long 	}
20291713e81bSScott Long 
203049b3fc40SJohn Baldwin 	mtx_lock(&pAdapter->lock);
2031b50569b7SScott Long 	if (xpt_bus_register(hpt_vsim, dev, 0) != CAM_SUCCESS)
2032d2bd3ab9SScott Long 	{
20331713e81bSScott Long 		cam_sim_free(hpt_vsim, /*free devq*/ TRUE);
203449b3fc40SJohn Baldwin 		mtx_unlock(&pAdapter->lock);
20351713e81bSScott Long 		hpt_vsim = NULL;
20361713e81bSScott Long 		return ENXIO;
20371713e81bSScott Long 	}
20381713e81bSScott Long 
20391713e81bSScott Long 	if(xpt_create_path(&pAdapter->path, /*periph */ NULL,
2040d2bd3ab9SScott Long 			cam_sim_path(hpt_vsim), CAM_TARGET_WILDCARD,
2041d2bd3ab9SScott Long 			CAM_LUN_WILDCARD) != CAM_REQ_CMP)
2042d2bd3ab9SScott Long 	{
20431713e81bSScott Long 		xpt_bus_deregister(cam_sim_path(hpt_vsim));
20441713e81bSScott Long 		cam_sim_free(hpt_vsim, /*free_devq*/TRUE);
204549b3fc40SJohn Baldwin 		mtx_unlock(&pAdapter->lock);
20461713e81bSScott Long 		hpt_vsim = NULL;
20471713e81bSScott Long 		return ENXIO;
20481713e81bSScott Long 	}
204949b3fc40SJohn Baldwin 	mtx_unlock(&pAdapter->lock);
20501713e81bSScott Long 
20511713e81bSScott Long 	xpt_setup_ccb(&(ccb->ccb_h), pAdapter->path, /*priority*/5);
20521713e81bSScott Long 	ccb->ccb_h.func_code = XPT_SASYNC_CB;
20531713e81bSScott Long 	ccb->csa.event_enable = AC_LOST_DEVICE;
20541713e81bSScott Long 	ccb->csa.callback = hpt_async;
20551713e81bSScott Long 	ccb->csa.callback_arg = hpt_vsim;
20561713e81bSScott Long 	xpt_action((union ccb *)ccb);
20572ef735f4SEdward Tomasz Napierala 	xpt_free_ccb(ccb);
20581713e81bSScott Long 
2059cd3ef666SXin LI 	if (device_get_unit(dev) == 0) {
20608eadd1b2SXin LI 		/* Start the work thread.  XXX */
20616e0e5d36SNate Lawson 		launch_worker_thread();
20626e0e5d36SNate Lawson 	}
20631713e81bSScott Long 
20641713e81bSScott Long 	return 0;
20651713e81bSScott Long }
20661713e81bSScott Long 
20671713e81bSScott Long static int
20681713e81bSScott Long hpt_detach(device_t dev)
20691713e81bSScott Long {
20701713e81bSScott Long 	return (EBUSY);
20711713e81bSScott Long }
20721713e81bSScott Long 
2073d2bd3ab9SScott Long 
20741713e81bSScott Long /***************************************************************
20751713e81bSScott Long  * The poll function is used to simulate the interrupt when
20761713e81bSScott Long  * the interrupt subsystem is not functioning.
20771713e81bSScott Long  *
20781713e81bSScott Long  ***************************************************************/
20791713e81bSScott Long static void
20801713e81bSScott Long hpt_poll(struct cam_sim *sim)
20811713e81bSScott Long {
208249b3fc40SJohn Baldwin 
208349b3fc40SJohn Baldwin 	hpt_intr_locked((void *)cam_sim_softc(sim));
20841713e81bSScott Long }
20851713e81bSScott Long 
20861713e81bSScott Long /****************************************************************
20871713e81bSScott Long  *	Name:	hpt_intr
20881713e81bSScott Long  *	Description:	Interrupt handler.
20891713e81bSScott Long  ****************************************************************/
20901713e81bSScott Long static void
20911713e81bSScott Long hpt_intr(void *arg)
20921713e81bSScott Long {
209349b3fc40SJohn Baldwin 	IAL_ADAPTER_T *pAdapter;
20941713e81bSScott Long 
209549b3fc40SJohn Baldwin 	pAdapter = arg;
209649b3fc40SJohn Baldwin 	mtx_lock(&pAdapter->lock);
209749b3fc40SJohn Baldwin 	hpt_intr_locked(pAdapter);
209849b3fc40SJohn Baldwin 	mtx_unlock(&pAdapter->lock);
209949b3fc40SJohn Baldwin }
210049b3fc40SJohn Baldwin 
210149b3fc40SJohn Baldwin static void
210249b3fc40SJohn Baldwin hpt_intr_locked(IAL_ADAPTER_T *pAdapter)
210349b3fc40SJohn Baldwin {
210449b3fc40SJohn Baldwin 
210549b3fc40SJohn Baldwin 	mtx_assert(&pAdapter->lock, MA_OWNED);
21061713e81bSScott Long 	/* KdPrintI(("----- Entering Isr() -----\n")); */
2107d2bd3ab9SScott Long 	if (mvSataInterruptServiceRoutine(&pAdapter->mvSataAdapter) == MV_TRUE)
2108d2bd3ab9SScott Long 	{
21091713e81bSScott Long 		_VBUS_INST(&pAdapter->VBus)
21101713e81bSScott Long 		CheckPendingCall(_VBUS_P0);
21111713e81bSScott Long 	}
21121713e81bSScott Long 
21131713e81bSScott Long 	/* KdPrintI(("----- Leaving Isr() -----\n")); */
21141713e81bSScott Long }
21151713e81bSScott Long 
21161713e81bSScott Long /**********************************************************
21171713e81bSScott Long  * 			Asynchronous Events
21181713e81bSScott Long  *********************************************************/
21191713e81bSScott Long #if (!defined(UNREFERENCED_PARAMETER))
21201713e81bSScott Long #define UNREFERENCED_PARAMETER(x) (void)(x)
21211713e81bSScott Long #endif
21221713e81bSScott Long 
21231713e81bSScott Long static void
21241713e81bSScott Long hpt_async(void * callback_arg, u_int32_t code, struct cam_path * path,
21251713e81bSScott Long     void * arg)
21261713e81bSScott Long {
21271713e81bSScott Long 	/* debug XXXX */
21281713e81bSScott Long 	panic("Here");
21291713e81bSScott Long 	UNREFERENCED_PARAMETER(callback_arg);
21301713e81bSScott Long 	UNREFERENCED_PARAMETER(code);
21311713e81bSScott Long 	UNREFERENCED_PARAMETER(path);
21321713e81bSScott Long 	UNREFERENCED_PARAMETER(arg);
21331713e81bSScott Long 
21341713e81bSScott Long }
21351713e81bSScott Long 
21361713e81bSScott Long static void
21371713e81bSScott Long FlushAdapter(IAL_ADAPTER_T *pAdapter)
21381713e81bSScott Long {
21391713e81bSScott Long 	int i;
21401713e81bSScott Long 
21411713e81bSScott Long 	hpt_printk(("flush all devices\n"));
21421713e81bSScott Long 
21431713e81bSScott Long 	/* flush all devices */
21441713e81bSScott Long 	for (i=0; i<MAX_VDEVICE_PER_VBUS; i++) {
21451713e81bSScott Long 		PVDevice pVDev = pAdapter->VBus.pVDevice[i];
2146d2bd3ab9SScott Long 		if(pVDev) fFlushVDev(pVDev);
21471713e81bSScott Long 	}
21481713e81bSScott Long }
21491713e81bSScott Long 
21501713e81bSScott Long static int
21511713e81bSScott Long hpt_shutdown(device_t dev)
21521713e81bSScott Long {
21531713e81bSScott Long 		IAL_ADAPTER_T *pAdapter;
21541713e81bSScott Long 
21551713e81bSScott Long 		pAdapter = device_get_softc(dev);
21561713e81bSScott Long 
215749b3fc40SJohn Baldwin 		mtx_lock(&pAdapter->lock);
21581713e81bSScott Long 		FlushAdapter(pAdapter);
215949b3fc40SJohn Baldwin 		mtx_unlock(&pAdapter->lock);
2160d2bd3ab9SScott Long 		  /* give the flush some time to happen,
2161d2bd3ab9SScott Long 		    *otherwise "shutdown -p now" will make file system corrupted */
2162d2bd3ab9SScott Long 		DELAY(1000 * 1000 * 5);
21631713e81bSScott Long 		return 0;
21641713e81bSScott Long }
21651713e81bSScott Long 
21661713e81bSScott Long void
21671713e81bSScott Long Check_Idle_Call(IAL_ADAPTER_T *pAdapter)
21681713e81bSScott Long {
21691713e81bSScott Long 	_VBUS_INST(&pAdapter->VBus)
21701713e81bSScott Long 
21711713e81bSScott Long 	if (mWaitingForIdle(_VBUS_P0)) {
21721713e81bSScott Long 		CheckIdleCall(_VBUS_P0);
21731713e81bSScott Long #ifdef SUPPORT_ARRAY
2174d2bd3ab9SScott Long 		{
2175d2bd3ab9SScott Long 			int i;
21761713e81bSScott Long 			PVDevice pArray;
2177d2bd3ab9SScott Long 			for(i = 0; i < MAX_ARRAY_PER_VBUS; i++){
21781713e81bSScott Long 				if ((pArray=ArrayTables(i))->u.array.dArStamp==0)
21791713e81bSScott Long 					continue;
21801713e81bSScott Long 				else if (pArray->u.array.rf_auto_rebuild) {
21811713e81bSScott Long 						KdPrint(("auto rebuild.\n"));
21821713e81bSScott Long 						pArray->u.array.rf_auto_rebuild = 0;
2183d2bd3ab9SScott Long 						hpt_queue_dpc((HPT_DPC)hpt_rebuild_data_block, pAdapter, pArray, DUPLICATE);
2184d2bd3ab9SScott Long 				}
21851713e81bSScott Long 			}
21861713e81bSScott Long 		}
21871713e81bSScott Long #endif
21881713e81bSScott Long 	}
21891713e81bSScott Long 	/* launch the awaiting commands blocked by mWaitingForIdle */
2190d2bd3ab9SScott Long 	while(pAdapter->pending_Q!= NULL)
2191d2bd3ab9SScott Long 	{
21921713e81bSScott Long 		_VBUS_INST(&pAdapter->VBus)
2193d2bd3ab9SScott Long 		union ccb *ccb = (union ccb *)pAdapter->pending_Q->ccb_h.ccb_ccb_ptr;
21941713e81bSScott Long 		hpt_free_ccb(&pAdapter->pending_Q, ccb);
21951713e81bSScott Long 		CallAfterReturn(_VBUS_P (DPC_PROC)OsSendCommand, ccb);
21961713e81bSScott Long 	}
21971713e81bSScott Long }
21981713e81bSScott Long 
21991713e81bSScott Long static void
22001713e81bSScott Long ccb_done(union ccb *ccb)
22011713e81bSScott Long {
2202d2bd3ab9SScott Long 	PBUS_DMAMAP pmap = (PBUS_DMAMAP)ccb->ccb_adapter;
2203d2bd3ab9SScott Long 	IAL_ADAPTER_T * pAdapter = pmap->pAdapter;
2204d2bd3ab9SScott Long 	KdPrintI(("ccb_done: ccb %p status %x\n", ccb, ccb->ccb_h.status));
22051713e81bSScott Long 
2206d2bd3ab9SScott Long 	dmamap_put(pmap);
22071713e81bSScott Long 	xpt_done(ccb);
22081713e81bSScott Long 
22091713e81bSScott Long 	pAdapter->outstandingCommands--;
22101713e81bSScott Long 
2211d2bd3ab9SScott Long 	if (pAdapter->outstandingCommands == 0)
2212d2bd3ab9SScott Long 	{
22131713e81bSScott Long 		if(DPC_Request_Nums == 0)
22141713e81bSScott Long 			Check_Idle_Call(pAdapter);
221549b3fc40SJohn Baldwin 		wakeup(pAdapter);
22161713e81bSScott Long 	}
22171713e81bSScott Long }
22181713e81bSScott Long 
22191713e81bSScott Long /****************************************************************
22201713e81bSScott Long  *	Name:	hpt_action
22211713e81bSScott Long  *	Description:	Process a queued command from the CAM layer.
22221713e81bSScott Long  *	Parameters:		sim - Pointer to SIM object
22231713e81bSScott Long  *					ccb - Pointer to SCSI command structure.
22241713e81bSScott Long  ****************************************************************/
22251713e81bSScott Long 
22261713e81bSScott Long void
22271713e81bSScott Long hpt_action(struct cam_sim *sim, union ccb *ccb)
22281713e81bSScott Long {
22291713e81bSScott Long 	IAL_ADAPTER_T * pAdapter = (IAL_ADAPTER_T *) cam_sim_softc(sim);
2230d2bd3ab9SScott Long 	PBUS_DMAMAP  pmap;
22311713e81bSScott Long 	_VBUS_INST(&pAdapter->VBus)
22321713e81bSScott Long 
223349b3fc40SJohn Baldwin 	mtx_assert(&pAdapter->lock, MA_OWNED);
2234e1ab829aSScott Long 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("hpt_action\n"));
2235d2bd3ab9SScott Long 	KdPrint(("hpt_action(%lx,%lx{%x})\n", (u_long)sim, (u_long)ccb, ccb->ccb_h.func_code));
22361713e81bSScott Long 
2237d2bd3ab9SScott Long 	switch (ccb->ccb_h.func_code)
2238d2bd3ab9SScott Long 	{
22391713e81bSScott Long 		case XPT_SCSI_IO:	/* Execute the requested I/O operation */
2240d2bd3ab9SScott Long 		{
22411713e81bSScott Long 			/* ccb->ccb_h.path_id is not our bus id - don't check it */
22421713e81bSScott Long 
22431713e81bSScott Long 			if (ccb->ccb_h.target_lun)	{
22441713e81bSScott Long 				ccb->ccb_h.status = CAM_LUN_INVALID;
22451713e81bSScott Long 				xpt_done(ccb);
22461713e81bSScott Long 				return;
22471713e81bSScott Long 			}
22481713e81bSScott Long 			if (ccb->ccb_h.target_id >= MAX_VDEVICE_PER_VBUS ||
22491713e81bSScott Long 				pAdapter->VBus.pVDevice[ccb->ccb_h.target_id]==0) {
22501713e81bSScott Long 				ccb->ccb_h.status = CAM_TID_INVALID;
22511713e81bSScott Long 				xpt_done(ccb);
22521713e81bSScott Long 				return;
22531713e81bSScott Long 			}
22541713e81bSScott Long 
22551713e81bSScott Long 			if (pAdapter->outstandingCommands==0 && DPC_Request_Nums==0)
22561713e81bSScott Long 				Check_Idle_Call(pAdapter);
22571713e81bSScott Long 
2258d2bd3ab9SScott Long 			pmap = dmamap_get(pAdapter);
2259d2bd3ab9SScott Long 			HPT_ASSERT(pmap);
2260d2bd3ab9SScott Long 			ccb->ccb_adapter = pmap;
2261d2bd3ab9SScott Long 			memset((void *)pmap->psg, 0,  sizeof(pmap->psg));
2262d2bd3ab9SScott Long 
22631713e81bSScott Long 			if (mWaitingForIdle(_VBUS_P0))
22641713e81bSScott Long 				hpt_queue_ccb(&pAdapter->pending_Q, ccb);
22651713e81bSScott Long 			else
22661713e81bSScott Long 				OsSendCommand(_VBUS_P ccb);
22671713e81bSScott Long 
22681713e81bSScott Long 			/* KdPrint(("leave scsiio\n")); */
22691713e81bSScott Long 			break;
2270d2bd3ab9SScott Long 		}
22711713e81bSScott Long 
22721713e81bSScott Long 		case XPT_RESET_BUS:
22731713e81bSScott Long 			KdPrint(("reset bus\n"));
22741713e81bSScott Long 			fResetVBus(_VBUS_P0);
22751713e81bSScott Long 			xpt_done(ccb);
22761713e81bSScott Long 			break;
22771713e81bSScott Long 
22781713e81bSScott Long 		case XPT_RESET_DEV:	/* Bus Device Reset the specified SCSI device */
22791713e81bSScott Long 		case XPT_ABORT:			/* Abort the specified CCB */
22801713e81bSScott Long 		case XPT_TERM_IO:		/* Terminate the I/O process */
22811713e81bSScott Long 			/* XXX Implement */
22821713e81bSScott Long 			ccb->ccb_h.status = CAM_REQ_INVALID;
22831713e81bSScott Long 			xpt_done(ccb);
22841713e81bSScott Long 			break;
22851713e81bSScott Long 
22861713e81bSScott Long 		case XPT_GET_TRAN_SETTINGS:
22871713e81bSScott Long 		case XPT_SET_TRAN_SETTINGS:
22881713e81bSScott Long 			/* XXX Implement */
22891713e81bSScott Long 			ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
22901713e81bSScott Long 			xpt_done(ccb);
22911713e81bSScott Long 			break;
22921713e81bSScott Long 
22931713e81bSScott Long 		case XPT_CALC_GEOMETRY:
2294f3b080e6SMarius Strobl 			cam_calc_geometry(&ccb->ccg, 1);
22951713e81bSScott Long 			xpt_done(ccb);
22961713e81bSScott Long 			break;
22971713e81bSScott Long 
22981713e81bSScott Long 		case XPT_PATH_INQ:		/* Path routing inquiry */
22991713e81bSScott Long 		{
23001713e81bSScott Long 			struct ccb_pathinq *cpi = &ccb->cpi;
23011713e81bSScott Long 
23021713e81bSScott Long 			cpi->version_num = 1; /* XXX??? */
23031713e81bSScott Long 			cpi->hba_inquiry = PI_SDTR_ABLE;
23041713e81bSScott Long 			cpi->target_sprt = 0;
23051713e81bSScott Long 			/* Not necessary to reset bus */
23061713e81bSScott Long 			cpi->hba_misc = PIM_NOBUSRESET;
23071713e81bSScott Long 			cpi->hba_eng_cnt = 0;
23081713e81bSScott Long 
23091713e81bSScott Long 			cpi->max_target = MAX_VDEVICE_PER_VBUS;
23101713e81bSScott Long 			cpi->max_lun = 0;
23111713e81bSScott Long 			cpi->initiator_id = MAX_VDEVICE_PER_VBUS;
23121713e81bSScott Long 
23131713e81bSScott Long 			cpi->bus_id = cam_sim_bus(sim);
23141713e81bSScott Long 			cpi->base_transfer_speed = 3300;
23154195c7deSAlan Somers 			strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
23164195c7deSAlan Somers 			strlcpy(cpi->hba_vid, "HPT   ", HBA_IDLEN);
23174195c7deSAlan Somers 			strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
23181713e81bSScott Long 			cpi->unit_number = cam_sim_unit(sim);
23199085ea50SAlexander Motin 			cpi->transport = XPORT_SPI;
23209085ea50SAlexander Motin 			cpi->transport_version = 2;
23219085ea50SAlexander Motin 			cpi->protocol = PROTO_SCSI;
23229085ea50SAlexander Motin 			cpi->protocol_version = SCSI_REV_2;
23231713e81bSScott Long 			cpi->ccb_h.status = CAM_REQ_CMP;
23241713e81bSScott Long 			xpt_done(ccb);
23251713e81bSScott Long 			break;
23261713e81bSScott Long 		}
23271713e81bSScott Long 
23281713e81bSScott Long 		default:
23291713e81bSScott Long 			KdPrint(("invalid cmd\n"));
23301713e81bSScott Long 			ccb->ccb_h.status = CAM_REQ_INVALID;
23311713e81bSScott Long 			xpt_done(ccb);
23321713e81bSScott Long 			break;
23331713e81bSScott Long 	}
23341713e81bSScott Long 	/* KdPrint(("leave hpt_action..............\n")); */
23351713e81bSScott Long }
23361713e81bSScott Long 
23371713e81bSScott Long /* shall be called at lock_driver() */
23381713e81bSScott Long static void
23391713e81bSScott Long hpt_queue_ccb(union ccb **ccb_Q, union ccb *ccb)
23401713e81bSScott Long {
23411713e81bSScott Long 	if(*ccb_Q == NULL)
23421713e81bSScott Long 		ccb->ccb_h.ccb_ccb_ptr = ccb;
23431713e81bSScott Long 	else {
23441713e81bSScott Long 		ccb->ccb_h.ccb_ccb_ptr = (*ccb_Q)->ccb_h.ccb_ccb_ptr;
23451713e81bSScott Long 		(*ccb_Q)->ccb_h.ccb_ccb_ptr = (char *)ccb;
23461713e81bSScott Long 	}
23471713e81bSScott Long 
23481713e81bSScott Long 	*ccb_Q = ccb;
23491713e81bSScott Long }
23501713e81bSScott Long 
23511713e81bSScott Long /* shall be called at lock_driver() */
23521713e81bSScott Long static void
23531713e81bSScott Long hpt_free_ccb(union ccb **ccb_Q, union ccb *ccb)
23541713e81bSScott Long {
23551713e81bSScott Long 	union ccb *TempCCB;
23561713e81bSScott Long 
23571713e81bSScott Long 	TempCCB = *ccb_Q;
23581713e81bSScott Long 
2359d2bd3ab9SScott Long 	if(ccb->ccb_h.ccb_ccb_ptr == ccb) /*it means SCpnt is the last one in CURRCMDs*/
23601713e81bSScott Long 		*ccb_Q = NULL;
23611713e81bSScott Long 	else {
23621713e81bSScott Long 		while(TempCCB->ccb_h.ccb_ccb_ptr != (char *)ccb)
23631713e81bSScott Long 			TempCCB = (union ccb *)TempCCB->ccb_h.ccb_ccb_ptr;
23641713e81bSScott Long 
23651713e81bSScott Long 		TempCCB->ccb_h.ccb_ccb_ptr = ccb->ccb_h.ccb_ccb_ptr;
23661713e81bSScott Long 
23671713e81bSScott Long 		if(*ccb_Q == ccb)
23681713e81bSScott Long 			*ccb_Q = TempCCB;
23691713e81bSScott Long 	}
23701713e81bSScott Long }
23711713e81bSScott Long 
23721713e81bSScott Long #ifdef SUPPORT_ARRAY
23731713e81bSScott Long /***************************************************************************
23741713e81bSScott Long  * Function:     hpt_worker_thread
23751713e81bSScott Long  * Description:  Do background rebuilding. Execute in kernel thread context.
23761713e81bSScott Long  * Returns:      None
23771713e81bSScott Long  ***************************************************************************/
23781713e81bSScott Long static void hpt_worker_thread(void)
23791713e81bSScott Long {
23801713e81bSScott Long 
23811713e81bSScott Long 	for(;;)	{
238249b3fc40SJohn Baldwin 		mtx_lock(&DpcQueue_Lock);
23831713e81bSScott Long 		while (DpcQueue_First!=DpcQueue_Last) {
23841713e81bSScott Long 			ST_HPT_DPC p;
23851713e81bSScott Long 			p = DpcQueue[DpcQueue_First];
23861713e81bSScott Long 			DpcQueue_First++;
23871713e81bSScott Long 			DpcQueue_First %= MAX_DPC;
23881713e81bSScott Long 			DPC_Request_Nums++;
238949b3fc40SJohn Baldwin 			mtx_unlock(&DpcQueue_Lock);
23901713e81bSScott Long 			p.dpc(p.pAdapter, p.arg, p.flags);
23911713e81bSScott Long 
239249b3fc40SJohn Baldwin 			mtx_lock(&p.pAdapter->lock);
239349b3fc40SJohn Baldwin 			mtx_lock(&DpcQueue_Lock);
23941713e81bSScott Long 			DPC_Request_Nums--;
2395d2bd3ab9SScott Long 			/* since we may have prevented Check_Idle_Call, do it here */
23961713e81bSScott Long 			if (DPC_Request_Nums==0) {
23971713e81bSScott Long 				if (p.pAdapter->outstandingCommands == 0) {
23981713e81bSScott Long 					_VBUS_INST(&p.pAdapter->VBus);
23991713e81bSScott Long 					Check_Idle_Call(p.pAdapter);
24001713e81bSScott Long 					CheckPendingCall(_VBUS_P0);
24011713e81bSScott Long 				}
24021713e81bSScott Long 			}
240349b3fc40SJohn Baldwin 			mtx_unlock(&p.pAdapter->lock);
240449b3fc40SJohn Baldwin 			mtx_unlock(&DpcQueue_Lock);
24051713e81bSScott Long 
2406d2bd3ab9SScott Long 			/*Schedule out*/
2407d2bd3ab9SScott Long 			if (SIGISMEMBER(curproc->p_siglist, SIGSTOP)) {
24081713e81bSScott Long 				/* abort rebuilding process. */
2409d2bd3ab9SScott Long 				IAL_ADAPTER_T *pAdapter;
2410d2bd3ab9SScott Long 				PVDevice      pArray;
2411d2bd3ab9SScott Long 				PVBus         _vbus_p;
2412d2bd3ab9SScott Long 				int i;
241349b3fc40SJohn Baldwin 
241449b3fc40SJohn Baldwin 				sx_slock(&hptmv_list_lock);
24151713e81bSScott Long 				pAdapter = gIal_Adapter;
2416d2bd3ab9SScott Long 
24174d24901aSPedro F. Giffuni 				while(pAdapter != NULL){
241849b3fc40SJohn Baldwin 					mtx_lock(&pAdapter->lock);
24191713e81bSScott Long 					_vbus_p = &pAdapter->VBus;
2420d2bd3ab9SScott Long 
2421d2bd3ab9SScott Long 					for (i=0;i<MAX_ARRAY_PER_VBUS;i++)
2422d2bd3ab9SScott Long 					{
24231713e81bSScott Long 						if ((pArray=ArrayTables(i))->u.array.dArStamp==0)
24241713e81bSScott Long 							continue;
2425d2bd3ab9SScott Long 						else if (pArray->u.array.rf_rebuilding ||
24261713e81bSScott Long 								pArray->u.array.rf_verifying ||
2427d2bd3ab9SScott Long 								pArray->u.array.rf_initializing)
2428d2bd3ab9SScott Long 							{
24291713e81bSScott Long 								pArray->u.array.rf_abort_rebuild = 1;
24301713e81bSScott Long 							}
24311713e81bSScott Long 					}
243249b3fc40SJohn Baldwin 					mtx_unlock(&pAdapter->lock);
24331713e81bSScott Long 					pAdapter = pAdapter->next;
24341713e81bSScott Long 				}
243549b3fc40SJohn Baldwin 				sx_sunlock(&hptmv_list_lock);
24361713e81bSScott Long 			}
243749b3fc40SJohn Baldwin 			mtx_lock(&DpcQueue_Lock);
2438d2bd3ab9SScott Long 		}
243949b3fc40SJohn Baldwin 		mtx_unlock(&DpcQueue_Lock);
24401713e81bSScott Long 
2441d2bd3ab9SScott Long /*Remove this debug option*/
2442d2bd3ab9SScott Long /*
24431713e81bSScott Long #ifdef DEBUG
24441713e81bSScott Long 		if (SIGISMEMBER(curproc->p_siglist, SIGSTOP))
24454d70511aSJohn Baldwin 			pause("hptrdy", 2*hz);
24461713e81bSScott Long #endif
2447d2bd3ab9SScott Long */
24483745c395SJulian Elischer 		kproc_suspend_check(curproc);
244949b3fc40SJohn Baldwin 		pause("-", 2*hz);  /* wait for something to do */
24501713e81bSScott Long 	}
24511713e81bSScott Long }
24521713e81bSScott Long 
24531713e81bSScott Long static struct proc *hptdaemonproc;
24541713e81bSScott Long static struct kproc_desc hpt_kp = {
24551713e81bSScott Long 	"hpt_wt",
24561713e81bSScott Long 	hpt_worker_thread,
24571713e81bSScott Long 	&hptdaemonproc
24581713e81bSScott Long };
24591713e81bSScott Long 
2460d2bd3ab9SScott Long /*Start this thread in the hpt_attach, to prevent kernel from loading it without our controller.*/
24611713e81bSScott Long static void
24621713e81bSScott Long launch_worker_thread(void)
24631713e81bSScott Long {
24641713e81bSScott Long 	IAL_ADAPTER_T *pAdapTemp;
24651713e81bSScott Long 
24661713e81bSScott Long 	kproc_start(&hpt_kp);
24671713e81bSScott Long 
246849b3fc40SJohn Baldwin 	sx_slock(&hptmv_list_lock);
24691713e81bSScott Long 	for (pAdapTemp = gIal_Adapter; pAdapTemp; pAdapTemp = pAdapTemp->next) {
24701713e81bSScott Long 
24711713e81bSScott Long 		_VBUS_INST(&pAdapTemp->VBus)
24721713e81bSScott Long 		int i;
24731713e81bSScott Long 		PVDevice pVDev;
24741713e81bSScott Long 
24751713e81bSScott Long 		for(i = 0; i < MAX_ARRAY_PER_VBUS; i++)
24761713e81bSScott Long 			if ((pVDev=ArrayTables(i))->u.array.dArStamp==0)
24771713e81bSScott Long 				continue;
2478d2bd3ab9SScott Long 			else{
2479d2bd3ab9SScott Long 				if (pVDev->u.array.rf_need_rebuild && !pVDev->u.array.rf_rebuilding)
2480d2bd3ab9SScott Long 					hpt_queue_dpc((HPT_DPC)hpt_rebuild_data_block, pAdapTemp, pVDev,
2481d2bd3ab9SScott Long 					(UCHAR)((pVDev->u.array.CriticalMembers || pVDev->VDeviceType == VD_RAID_1)? DUPLICATE : REBUILD_PARITY));
24821713e81bSScott Long 			}
24831713e81bSScott Long 	}
248449b3fc40SJohn Baldwin 	sx_sunlock(&hptmv_list_lock);
24851713e81bSScott Long 
24861713e81bSScott Long 	/*
2487d2bd3ab9SScott Long 	 * hpt_worker_thread needs to be suspended after shutdown sync, when fs sync finished.
24881713e81bSScott Long 	 */
2489c33c9789SAlexander Motin 	EVENTHANDLER_REGISTER(shutdown_post_sync, kproc_shutdown, hptdaemonproc,
2490c33c9789SAlexander Motin 	    SHUTDOWN_PRI_LAST);
24911713e81bSScott Long }
2492d2bd3ab9SScott Long /*
2493d2bd3ab9SScott Long  *SYSINIT(hptwt, SI_SUB_KTHREAD_IDLE, SI_ORDER_FIRST, launch_worker_thread, NULL);
2494d2bd3ab9SScott Long */
24951713e81bSScott Long 
2496d2bd3ab9SScott Long #endif
24971713e81bSScott Long 
24981713e81bSScott Long /********************************************************************************/
24991713e81bSScott Long 
2500d2bd3ab9SScott Long int HPTLIBAPI fOsBuildSgl(_VBUS_ARG PCommand pCmd, FPSCAT_GATH pSg, int logical)
25011713e81bSScott Long {
25027634a04bSXin LI 	union ccb *ccb = (union ccb *)pCmd->pOrgCommand;
25031713e81bSScott Long 
25047634a04bSXin LI 	if (logical) {
25057634a04bSXin LI 		pSg->dSgAddress = (ULONG_PTR)(UCHAR *)ccb->csio.data_ptr;
25067634a04bSXin LI 		pSg->wSgSize = ccb->csio.dxfer_len;
25077634a04bSXin LI 		pSg->wSgFlag = SG_FLAG_EOT;
25087634a04bSXin LI 		return TRUE;
25097634a04bSXin LI 	}
2510d2bd3ab9SScott Long 	/* since we have provided physical sg, nobody will ask us to build physical sg */
2511d2bd3ab9SScott Long 	HPT_ASSERT(0);
25121713e81bSScott Long 	return FALSE;
25131713e81bSScott Long }
25141713e81bSScott Long 
25151713e81bSScott Long /*******************************************************************************/
25161713e81bSScott Long ULONG HPTLIBAPI
25171713e81bSScott Long GetStamp(void)
25181713e81bSScott Long {
25191713e81bSScott Long 	/*
25201713e81bSScott Long 	 * the system variable, ticks, can't be used since it hasn't yet been active
25211713e81bSScott Long 	 * when our driver starts (ticks==0, it's a invalid stamp value)
25221713e81bSScott Long 	 */
2523d2bd3ab9SScott Long 	ULONG stamp;
2524d2bd3ab9SScott Long 	do { stamp = random(); } while (stamp==0);
25251713e81bSScott Long 	return stamp;
25261713e81bSScott Long }
25271713e81bSScott Long 
25281713e81bSScott Long 
25291713e81bSScott Long static void
25301713e81bSScott Long SetInquiryData(PINQUIRYDATA inquiryData, PVDevice pVDev)
25311713e81bSScott Long {
25321713e81bSScott Long 	int i;
2533d2bd3ab9SScott Long 	IDENTIFY_DATA2 *pIdentify = (IDENTIFY_DATA2*)pVDev->u.disk.mv->identifyDevice;
25341713e81bSScott Long 
25351713e81bSScott Long 	inquiryData->DeviceType = T_DIRECT; /*DIRECT_ACCESS_DEVICE*/
25361713e81bSScott Long 	inquiryData->AdditionalLength = (UCHAR)(sizeof(INQUIRYDATA) - 5);
25371713e81bSScott Long #ifndef SERIAL_CMDS
25381713e81bSScott Long 	inquiryData->CommandQueue = 1;
25391713e81bSScott Long #endif
25401713e81bSScott Long 
25411713e81bSScott Long 	switch(pVDev->VDeviceType) {
25421713e81bSScott Long 	case VD_SINGLE_DISK:
25431713e81bSScott Long 	case VD_ATAPI:
25441713e81bSScott Long 	case VD_REMOVABLE:
25451713e81bSScott Long 		/* Set the removable bit, if applicable. */
2546d2bd3ab9SScott Long 		if ((pVDev->u.disk.df_removable_drive) || (pIdentify->GeneralConfiguration & 0x80))
25471713e81bSScott Long 			inquiryData->RemovableMedia = 1;
25481713e81bSScott Long 
25491713e81bSScott Long 		/* Fill in vendor identification fields. */
2550d2bd3ab9SScott Long 		for (i = 0; i < 20; i += 2) {
2551d2bd3ab9SScott Long 			inquiryData->VendorId[i] 	= ((PUCHAR)pIdentify->ModelNumber)[i + 1];
2552d2bd3ab9SScott Long 			inquiryData->VendorId[i+1] 	= ((PUCHAR)pIdentify->ModelNumber)[i];
25531713e81bSScott Long 
25541713e81bSScott Long 		}
25551713e81bSScott Long 
25561713e81bSScott Long 		/* Initialize unused portion of product id. */
25571713e81bSScott Long 		for (i = 0; i < 4; i++) inquiryData->ProductId[12+i] = ' ';
25581713e81bSScott Long 
25591713e81bSScott Long 		/* firmware revision */
2560d2bd3ab9SScott Long 		for (i = 0; i < 4; i += 2)
2561d2bd3ab9SScott Long 		{
2562d2bd3ab9SScott Long 			inquiryData->ProductRevisionLevel[i] 	= ((PUCHAR)pIdentify->FirmwareRevision)[i+1];
2563d2bd3ab9SScott Long 			inquiryData->ProductRevisionLevel[i+1] 	= ((PUCHAR)pIdentify->FirmwareRevision)[i];
25641713e81bSScott Long 		}
25651713e81bSScott Long 		break;
25661713e81bSScott Long 	default:
256764470755SXin LI 		memcpy(&inquiryData->VendorId, "RR18xx  ", 8);
25681713e81bSScott Long #ifdef SUPPORT_ARRAY
25691713e81bSScott Long 		switch(pVDev->VDeviceType){
25701713e81bSScott Long 		case VD_RAID_0:
2571d2bd3ab9SScott Long 			if ((pVDev->u.array.pMember[0] && mIsArray(pVDev->u.array.pMember[0])) ||
2572d2bd3ab9SScott Long 				(pVDev->u.array.pMember[1] && mIsArray(pVDev->u.array.pMember[1])))
2573d2bd3ab9SScott Long 				memcpy(&inquiryData->ProductId, "RAID 1/0 Array  ", 16);
25741713e81bSScott Long 			else
2575d2bd3ab9SScott Long 				memcpy(&inquiryData->ProductId, "RAID 0 Array    ", 16);
25761713e81bSScott Long 			break;
25771713e81bSScott Long 		case VD_RAID_1:
2578d2bd3ab9SScott Long 			if ((pVDev->u.array.pMember[0] && mIsArray(pVDev->u.array.pMember[0])) ||
2579d2bd3ab9SScott Long 				(pVDev->u.array.pMember[1] && mIsArray(pVDev->u.array.pMember[1])))
2580d2bd3ab9SScott Long 				memcpy(&inquiryData->ProductId, "RAID 0/1 Array  ", 16);
25811713e81bSScott Long 			else
2582d2bd3ab9SScott Long 				memcpy(&inquiryData->ProductId, "RAID 1 Array    ", 16);
25831713e81bSScott Long 			break;
25841713e81bSScott Long 		case VD_RAID_5:
25851713e81bSScott Long 			memcpy(&inquiryData->ProductId, "RAID 5 Array    ", 16);
25861713e81bSScott Long 			break;
25871713e81bSScott Long 		case VD_JBOD:
25881713e81bSScott Long 			memcpy(&inquiryData->ProductId, "JBOD Array      ", 16);
25891713e81bSScott Long 			break;
25901713e81bSScott Long 		}
25911713e81bSScott Long #endif
25921713e81bSScott Long 		memcpy(&inquiryData->ProductRevisionLevel, "3.00", 4);
25931713e81bSScott Long 		break;
25941713e81bSScott Long 	}
25951713e81bSScott Long }
25961713e81bSScott Long 
25971713e81bSScott Long static void
25981713e81bSScott Long hpt_timeout(void *arg)
25991713e81bSScott Long {
260049b3fc40SJohn Baldwin 	PBUS_DMAMAP pmap = (PBUS_DMAMAP)((union ccb *)arg)->ccb_adapter;
260149b3fc40SJohn Baldwin 	IAL_ADAPTER_T *pAdapter = pmap->pAdapter;
260249b3fc40SJohn Baldwin 	_VBUS_INST(&pAdapter->VBus)
260349b3fc40SJohn Baldwin 
260449b3fc40SJohn Baldwin 	mtx_assert(&pAdapter->lock, MA_OWNED);
26051713e81bSScott Long 	fResetVBus(_VBUS_P0);
26061713e81bSScott Long }
26071713e81bSScott Long 
2608d2bd3ab9SScott Long static void
2609d2bd3ab9SScott Long hpt_io_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
2610d2bd3ab9SScott Long {
2611d2bd3ab9SScott Long 	PCommand pCmd = (PCommand)arg;
2612d2bd3ab9SScott Long 	union ccb *ccb = pCmd->pOrgCommand;
2613d2bd3ab9SScott Long 	struct ccb_hdr *ccb_h = &ccb->ccb_h;
2614d2bd3ab9SScott Long 	PBUS_DMAMAP pmap = (PBUS_DMAMAP) ccb->ccb_adapter;
2615d2bd3ab9SScott Long 	IAL_ADAPTER_T *pAdapter = pmap->pAdapter;
2616d2bd3ab9SScott Long 	PVDevice	pVDev = pAdapter->VBus.pVDevice[ccb_h->target_id];
2617d2bd3ab9SScott Long 	FPSCAT_GATH psg = pCmd->pSgTable;
2618d2bd3ab9SScott Long 	int idx;
2619d2bd3ab9SScott Long 	_VBUS_INST(pVDev->pVBus)
2620d2bd3ab9SScott Long 
2621d2bd3ab9SScott Long 	HPT_ASSERT(pCmd->cf_physical_sg);
2622d2bd3ab9SScott Long 
2623dd0b4fb6SKonstantin Belousov 	if (error)
2624d2bd3ab9SScott Long 		panic("busdma error");
2625d2bd3ab9SScott Long 
2626d2bd3ab9SScott Long 	HPT_ASSERT(nsegs<= MAX_SG_DESCRIPTORS);
2627d2bd3ab9SScott Long 
2628dd0b4fb6SKonstantin Belousov 	if (nsegs != 0) {
2629d2bd3ab9SScott Long 		for (idx = 0; idx < nsegs; idx++, psg++) {
2630d2bd3ab9SScott Long 			psg->dSgAddress = (ULONG_PTR)(UCHAR *)segs[idx].ds_addr;
2631d2bd3ab9SScott Long 			psg->wSgSize = segs[idx].ds_len;
2632d2bd3ab9SScott Long 			psg->wSgFlag = (idx == nsegs-1)? SG_FLAG_EOT: 0;
2633d2bd3ab9SScott Long 	/*		KdPrint(("psg[%d]:add=%p,size=%x,flag=%x\n", idx, psg->dSgAddress,psg->wSgSize,psg->wSgFlag)); */
2634d2bd3ab9SScott Long 		}
2635d2bd3ab9SScott Long 		/*	psg[-1].wSgFlag = SG_FLAG_EOT; */
2636d2bd3ab9SScott Long 
2637d2bd3ab9SScott Long 		if (pCmd->cf_data_in) {
2638dd0b4fb6SKonstantin Belousov 			bus_dmamap_sync(pAdapter->io_dma_parent, pmap->dma_map,
2639dd0b4fb6SKonstantin Belousov 			    BUS_DMASYNC_PREREAD);
2640d2bd3ab9SScott Long 		}
2641d2bd3ab9SScott Long 		else if (pCmd->cf_data_out) {
2642dd0b4fb6SKonstantin Belousov 			bus_dmamap_sync(pAdapter->io_dma_parent, pmap->dma_map,
2643dd0b4fb6SKonstantin Belousov 			    BUS_DMASYNC_PREWRITE);
2644dd0b4fb6SKonstantin Belousov 		}
2645d2bd3ab9SScott Long 	}
2646d2bd3ab9SScott Long 
264749b3fc40SJohn Baldwin 	callout_reset(&pmap->timeout, 20 * hz, hpt_timeout, ccb);
2648d2bd3ab9SScott Long 	pVDev->pfnSendCommand(_VBUS_P pCmd);
2649d2bd3ab9SScott Long 	CheckPendingCall(_VBUS_P0);
2650d2bd3ab9SScott Long }
2651d2bd3ab9SScott Long 
2652d2bd3ab9SScott Long 
2653d2bd3ab9SScott Long 
26541713e81bSScott Long static void HPTLIBAPI
26551713e81bSScott Long OsSendCommand(_VBUS_ARG union ccb *ccb)
26561713e81bSScott Long {
2657d2bd3ab9SScott Long 	PBUS_DMAMAP pmap = (PBUS_DMAMAP)ccb->ccb_adapter;
2658d2bd3ab9SScott Long 	IAL_ADAPTER_T *pAdapter = pmap->pAdapter;
2659d2bd3ab9SScott Long 	struct ccb_hdr *ccb_h = &ccb->ccb_h;
2660d2bd3ab9SScott Long 	struct ccb_scsiio *csio = &ccb->csio;
2661d2bd3ab9SScott Long 	PVDevice	pVDev = pAdapter->VBus.pVDevice[ccb_h->target_id];
26621713e81bSScott Long 
26637d9aed9cSScott Long 	KdPrintI(("OsSendCommand: ccb %p  cdb %x-%x-%x\n",
26641713e81bSScott Long 		ccb,
26651713e81bSScott Long 		*(ULONG *)&ccb->csio.cdb_io.cdb_bytes[0],
26661713e81bSScott Long 		*(ULONG *)&ccb->csio.cdb_io.cdb_bytes[4],
26671713e81bSScott Long 		*(ULONG *)&ccb->csio.cdb_io.cdb_bytes[8]
26681713e81bSScott Long 	));
26691713e81bSScott Long 
26701713e81bSScott Long 	pAdapter->outstandingCommands++;
26711713e81bSScott Long 
26721713e81bSScott Long 	if (pVDev == NULL || pVDev->vf_online == 0) {
26731713e81bSScott Long 		ccb->ccb_h.status = CAM_REQ_INVALID;
26741713e81bSScott Long 		ccb_done(ccb);
26751713e81bSScott Long 		goto Command_Complished;
26761713e81bSScott Long 	}
26771713e81bSScott Long 
26781713e81bSScott Long 	switch(ccb->csio.cdb_io.cdb_bytes[0])
26791713e81bSScott Long 	{
26801713e81bSScott Long 		case TEST_UNIT_READY:
26811713e81bSScott Long 		case START_STOP_UNIT:
26821713e81bSScott Long 		case SYNCHRONIZE_CACHE:
26831713e81bSScott Long 			/* FALLTHROUGH */
26841713e81bSScott Long 			ccb->ccb_h.status = CAM_REQ_CMP;
26851713e81bSScott Long 			break;
26861713e81bSScott Long 
26871713e81bSScott Long 		case INQUIRY:
26881713e81bSScott Long 			ZeroMemory(ccb->csio.data_ptr, ccb->csio.dxfer_len);
26891713e81bSScott Long 			SetInquiryData((PINQUIRYDATA)ccb->csio.data_ptr, pVDev);
26901713e81bSScott Long 			ccb_h->status = CAM_REQ_CMP;
26911713e81bSScott Long 			break;
26921713e81bSScott Long 
26931713e81bSScott Long 		case READ_CAPACITY:
26941713e81bSScott Long 		{
269564470755SXin LI 			UCHAR *rbuf=csio->data_ptr;
269664470755SXin LI 			unsigned int cap;
269764470755SXin LI 
269864470755SXin LI 			if (pVDev->VDeviceCapacity > 0xfffffffful) {
269964470755SXin LI 				cap = 0xfffffffful;
270064470755SXin LI 			} else {
270164470755SXin LI 				cap = pVDev->VDeviceCapacity - 1;
270264470755SXin LI 			}
270364470755SXin LI 
270464470755SXin LI 			rbuf[0] = (UCHAR)(cap>>24);
270564470755SXin LI 			rbuf[1] = (UCHAR)(cap>>16);
270664470755SXin LI 			rbuf[2] = (UCHAR)(cap>>8);
270764470755SXin LI 			rbuf[3] = (UCHAR)cap;
27081713e81bSScott Long 			/* Claim 512 byte blocks (big-endian). */
270964470755SXin LI 			rbuf[4] = 0;
271064470755SXin LI 			rbuf[5] = 0;
271164470755SXin LI 			rbuf[6] = 2;
271264470755SXin LI 			rbuf[7] = 0;
271364470755SXin LI 
271464470755SXin LI 			ccb_h->status = CAM_REQ_CMP;
271564470755SXin LI 			break;
271664470755SXin LI 		}
271764470755SXin LI 
271864470755SXin LI 		case 0x9e: /*SERVICE_ACTION_IN*/
271964470755SXin LI 		{
272064470755SXin LI 			UCHAR *rbuf = csio->data_ptr;
272164470755SXin LI 			LBA_T cap = pVDev->VDeviceCapacity - 1;
272264470755SXin LI 
272364470755SXin LI 			rbuf[0] = (UCHAR)(cap>>56);
272464470755SXin LI 			rbuf[1] = (UCHAR)(cap>>48);
272564470755SXin LI 			rbuf[2] = (UCHAR)(cap>>40);
272664470755SXin LI 			rbuf[3] = (UCHAR)(cap>>32);
272764470755SXin LI 			rbuf[4] = (UCHAR)(cap>>24);
272864470755SXin LI 			rbuf[5] = (UCHAR)(cap>>16);
272964470755SXin LI 			rbuf[6] = (UCHAR)(cap>>8);
273064470755SXin LI 			rbuf[7] = (UCHAR)cap;
273164470755SXin LI 			rbuf[8] = 0;
273264470755SXin LI 			rbuf[9] = 0;
273364470755SXin LI 			rbuf[10] = 2;
273464470755SXin LI 			rbuf[11] = 0;
273564470755SXin LI 
27361713e81bSScott Long 			ccb_h->status = CAM_REQ_CMP;
27371713e81bSScott Long 			break;
27381713e81bSScott Long 		}
27391713e81bSScott Long 
27401713e81bSScott Long 		case READ_6:
27411713e81bSScott Long 		case WRITE_6:
27421713e81bSScott Long 		case READ_10:
27431713e81bSScott Long 		case WRITE_10:
274464470755SXin LI 		case 0x88: /* READ_16 */
274564470755SXin LI 		case 0x8a: /* WRITE_16 */
27461713e81bSScott Long 		case 0x13:
27471713e81bSScott Long 		case 0x2f:
27481713e81bSScott Long 		{
27491713e81bSScott Long 			UCHAR Cdb[16];
27501713e81bSScott Long 			UCHAR CdbLength;
27511713e81bSScott Long 			_VBUS_INST(pVDev->pVBus)
2752d2bd3ab9SScott Long 			PCommand pCmd = AllocateCommand(_VBUS_P0);
2753dd0b4fb6SKonstantin Belousov 			int error;
27541713e81bSScott Long 			HPT_ASSERT(pCmd);
27551713e81bSScott Long 
27561713e81bSScott Long 			CdbLength = csio->cdb_len;
2757d2bd3ab9SScott Long 			if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0)
2758d2bd3ab9SScott Long 			{
2759d2bd3ab9SScott Long 				if ((ccb->ccb_h.flags & CAM_CDB_PHYS) == 0)
2760d2bd3ab9SScott Long 				{
27611713e81bSScott Long 					bcopy(csio->cdb_io.cdb_ptr, Cdb, CdbLength);
2762d2bd3ab9SScott Long 				}
2763d2bd3ab9SScott Long 				else
2764d2bd3ab9SScott Long 				{
27651713e81bSScott Long 					KdPrintE(("ERROR!!!\n"));
27661713e81bSScott Long 					ccb->ccb_h.status = CAM_REQ_INVALID;
27671713e81bSScott Long 					break;
27681713e81bSScott Long 				}
2769d2bd3ab9SScott Long 			}
2770d2bd3ab9SScott Long 			else
2771d2bd3ab9SScott Long 			{
27721713e81bSScott Long 				bcopy(csio->cdb_io.cdb_bytes, Cdb, CdbLength);
27731713e81bSScott Long 			}
27741713e81bSScott Long 
2775d2bd3ab9SScott Long 			pCmd->pOrgCommand = ccb;
27761713e81bSScott Long 			pCmd->pVDevice = pVDev;
27771713e81bSScott Long 			pCmd->pfnCompletion = fOsCommandDone;
27781713e81bSScott Long 			pCmd->pfnBuildSgl = fOsBuildSgl;
2779d2bd3ab9SScott Long 			pCmd->pSgTable = pmap->psg;
27801713e81bSScott Long 
2781d2bd3ab9SScott Long 			switch (Cdb[0])
2782d2bd3ab9SScott Long 			{
27831713e81bSScott Long 				case READ_6:
27841713e81bSScott Long 				case WRITE_6:
27851713e81bSScott Long 				case 0x13:
2786d2bd3ab9SScott Long 					pCmd->uCmd.Ide.Lba =  ((ULONG)Cdb[1] << 16) | ((ULONG)Cdb[2] << 8) | (ULONG)Cdb[3];
27871713e81bSScott Long 					pCmd->uCmd.Ide.nSectors = (USHORT) Cdb[4];
27881713e81bSScott Long 					break;
2789d2bd3ab9SScott Long 
279064470755SXin LI 				case 0x88: /* READ_16 */
279164470755SXin LI 				case 0x8a: /* WRITE_16 */
279264470755SXin LI 					pCmd->uCmd.Ide.Lba =
279364470755SXin LI 						(HPT_U64)Cdb[2] << 56 |
279464470755SXin LI 						(HPT_U64)Cdb[3] << 48 |
279564470755SXin LI 						(HPT_U64)Cdb[4] << 40 |
279664470755SXin LI 						(HPT_U64)Cdb[5] << 32 |
279764470755SXin LI 						(HPT_U64)Cdb[6] << 24 |
279864470755SXin LI 						(HPT_U64)Cdb[7] << 16 |
279964470755SXin LI 						(HPT_U64)Cdb[8] << 8 |
280064470755SXin LI 						(HPT_U64)Cdb[9];
280164470755SXin LI 					pCmd->uCmd.Ide.nSectors = (USHORT)Cdb[12] << 8 | (USHORT)Cdb[13];
280264470755SXin LI 					break;
280364470755SXin LI 
28041713e81bSScott Long 				default:
2805d2bd3ab9SScott Long 					pCmd->uCmd.Ide.Lba = (ULONG)Cdb[5] | ((ULONG)Cdb[4] << 8) | ((ULONG)Cdb[3] << 16) | ((ULONG)Cdb[2] << 24);
2806d2bd3ab9SScott Long 					pCmd->uCmd.Ide.nSectors = (USHORT) Cdb[8] | ((USHORT)Cdb[7]<<8);
28071713e81bSScott Long 					break;
28081713e81bSScott Long 			}
28091713e81bSScott Long 
2810d2bd3ab9SScott Long 			switch (Cdb[0])
2811d2bd3ab9SScott Long 			{
28121713e81bSScott Long 				case READ_6:
28131713e81bSScott Long 				case READ_10:
281464470755SXin LI 				case 0x88: /* READ_16 */
28151713e81bSScott Long 					pCmd->uCmd.Ide.Command = IDE_COMMAND_READ;
28161713e81bSScott Long 					pCmd->cf_data_in = 1;
28171713e81bSScott Long 					break;
28181713e81bSScott Long 
28191713e81bSScott Long 				case WRITE_6:
28201713e81bSScott Long 				case WRITE_10:
282164470755SXin LI 				case 0x8a: /* WRITE_16 */
28221713e81bSScott Long 					pCmd->uCmd.Ide.Command = IDE_COMMAND_WRITE;
28231713e81bSScott Long 					pCmd->cf_data_out = 1;
28241713e81bSScott Long 					break;
28251713e81bSScott Long 				case 0x13:
28261713e81bSScott Long 				case 0x2f:
28271713e81bSScott Long 					pCmd->uCmd.Ide.Command = IDE_COMMAND_VERIFY;
28281713e81bSScott Long 					break;
28291713e81bSScott Long 			}
2830d2bd3ab9SScott Long /*///////////////////////// */
2831d2bd3ab9SScott Long 			pCmd->cf_physical_sg = 1;
2832dd0b4fb6SKonstantin Belousov 			error = bus_dmamap_load_ccb(pAdapter->io_dma_parent,
2833d2bd3ab9SScott Long 						    pmap->dma_map,
2834dd0b4fb6SKonstantin Belousov 						    ccb,
2835dd0b4fb6SKonstantin Belousov 						    hpt_io_dmamap_callback,
2836dd0b4fb6SKonstantin Belousov 						    pCmd, BUS_DMA_WAITOK
2837d2bd3ab9SScott Long 						    );
2838d2bd3ab9SScott Long 			KdPrint(("bus_dmamap_load return %d\n", error));
2839d2bd3ab9SScott Long 			if (error && error!=EINPROGRESS) {
2840d2bd3ab9SScott Long 				hpt_printk(("bus_dmamap_load error %d\n", error));
2841d2bd3ab9SScott Long 				FreeCommand(_VBUS_P pCmd);
2842d2bd3ab9SScott Long 				ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2843d2bd3ab9SScott Long 				dmamap_put(pmap);
2844d2bd3ab9SScott Long 				pAdapter->outstandingCommands--;
284549b3fc40SJohn Baldwin 				if (pAdapter->outstandingCommands == 0)
284649b3fc40SJohn Baldwin 					wakeup(pAdapter);
2847d2bd3ab9SScott Long 				xpt_done(ccb);
2848d2bd3ab9SScott Long 			}
28491713e81bSScott Long 			goto Command_Complished;
28501713e81bSScott Long 		}
28511713e81bSScott Long 
28521713e81bSScott Long 		default:
28531713e81bSScott Long 			ccb->ccb_h.status = CAM_REQ_INVALID;
28541713e81bSScott Long 			break;
28551713e81bSScott Long 	}
28561713e81bSScott Long 	ccb_done(ccb);
28571713e81bSScott Long Command_Complished:
28581713e81bSScott Long 	CheckPendingCall(_VBUS_P0);
28591713e81bSScott Long 	return;
28601713e81bSScott Long }
28611713e81bSScott Long 
28621713e81bSScott Long static void HPTLIBAPI
28631713e81bSScott Long fOsCommandDone(_VBUS_ARG PCommand pCmd)
28641713e81bSScott Long {
2865d2bd3ab9SScott Long 	union ccb *ccb = pCmd->pOrgCommand;
2866d2bd3ab9SScott Long 	PBUS_DMAMAP pmap = (PBUS_DMAMAP)ccb->ccb_adapter;
2867d2bd3ab9SScott Long 	IAL_ADAPTER_T *pAdapter = pmap->pAdapter;
28681713e81bSScott Long 
2869d2bd3ab9SScott Long 	KdPrint(("fOsCommandDone(pcmd=%p, result=%d)\n", pCmd, pCmd->Result));
28701713e81bSScott Long 
287149b3fc40SJohn Baldwin 	callout_stop(&pmap->timeout);
28721713e81bSScott Long 
28731713e81bSScott Long 	switch(pCmd->Result) {
28741713e81bSScott Long 	case RETURN_SUCCESS:
28751713e81bSScott Long 		ccb->ccb_h.status = CAM_REQ_CMP;
28761713e81bSScott Long 		break;
28771713e81bSScott Long 	case RETURN_BAD_DEVICE:
28781713e81bSScott Long 		ccb->ccb_h.status = CAM_DEV_NOT_THERE;
28791713e81bSScott Long 		break;
28801713e81bSScott Long 	case RETURN_DEVICE_BUSY:
28811713e81bSScott Long 		ccb->ccb_h.status = CAM_BUSY;
28821713e81bSScott Long 		break;
28831713e81bSScott Long 	case RETURN_INVALID_REQUEST:
28841713e81bSScott Long 		ccb->ccb_h.status = CAM_REQ_INVALID;
28851713e81bSScott Long 		break;
28861713e81bSScott Long 	case RETURN_SELECTION_TIMEOUT:
28871713e81bSScott Long 		ccb->ccb_h.status = CAM_SEL_TIMEOUT;
28881713e81bSScott Long 		break;
28891713e81bSScott Long 	case RETURN_RETRY:
28901713e81bSScott Long 		ccb->ccb_h.status = CAM_BUSY;
28911713e81bSScott Long 		break;
28921713e81bSScott Long 	default:
28931713e81bSScott Long 		ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
28941713e81bSScott Long 		break;
28951713e81bSScott Long 	}
28961713e81bSScott Long 
2897d2bd3ab9SScott Long 	if (pCmd->cf_data_in) {
2898d2bd3ab9SScott Long 		bus_dmamap_sync(pAdapter->io_dma_parent, pmap->dma_map, BUS_DMASYNC_POSTREAD);
28991713e81bSScott Long 	}
2900a8bc7437SXin LI 	else if (pCmd->cf_data_out) {
2901d2bd3ab9SScott Long 		bus_dmamap_sync(pAdapter->io_dma_parent, pmap->dma_map, BUS_DMASYNC_POSTWRITE);
2902d2bd3ab9SScott Long 	}
29031713e81bSScott Long 
2904d2bd3ab9SScott Long 	bus_dmamap_unload(pAdapter->io_dma_parent, pmap->dma_map);
2905d2bd3ab9SScott Long 
29061713e81bSScott Long 	FreeCommand(_VBUS_P pCmd);
29071713e81bSScott Long 	ccb_done(ccb);
29081713e81bSScott Long }
29091713e81bSScott Long 
29101713e81bSScott Long int
29111713e81bSScott Long hpt_queue_dpc(HPT_DPC dpc, IAL_ADAPTER_T * pAdapter, void *arg, UCHAR flags)
29121713e81bSScott Long {
29131713e81bSScott Long 	int p;
29141713e81bSScott Long 
291549b3fc40SJohn Baldwin 	mtx_lock(&DpcQueue_Lock);
29161713e81bSScott Long 	p = (DpcQueue_Last + 1) % MAX_DPC;
29171713e81bSScott Long 	if (p==DpcQueue_First) {
29181713e81bSScott Long 		KdPrint(("DPC Queue full!\n"));
291949b3fc40SJohn Baldwin 		mtx_unlock(&DpcQueue_Lock);
29201713e81bSScott Long 		return -1;
29211713e81bSScott Long 	}
29221713e81bSScott Long 
29231713e81bSScott Long 	DpcQueue[DpcQueue_Last].dpc = dpc;
29241713e81bSScott Long 	DpcQueue[DpcQueue_Last].pAdapter = pAdapter;
29251713e81bSScott Long 	DpcQueue[DpcQueue_Last].arg = arg;
29261713e81bSScott Long 	DpcQueue[DpcQueue_Last].flags = flags;
29271713e81bSScott Long 	DpcQueue_Last = p;
292849b3fc40SJohn Baldwin 	mtx_unlock(&DpcQueue_Lock);
29291713e81bSScott Long 
29301713e81bSScott Long 	return 0;
29311713e81bSScott Long }
29321713e81bSScott Long 
29331713e81bSScott Long #ifdef _RAID5N_
29341713e81bSScott Long /*
2935d2bd3ab9SScott Long  * Allocate memory above 16M, otherwise we may eat all low memory for ISA devices.
2936d2bd3ab9SScott Long  * How about the memory for 5081 request/response array and PRD table?
29371713e81bSScott Long  */
29381713e81bSScott Long void
29391713e81bSScott Long *os_alloc_page(_VBUS_ARG0)
29401713e81bSScott Long {
2941d2bd3ab9SScott Long 	return (void *)contigmalloc(0x1000, M_DEVBUF, M_NOWAIT, 0x1000000, 0xffffffff, PAGE_SIZE, 0ul);
29421713e81bSScott Long }
2943d2bd3ab9SScott Long 
29441713e81bSScott Long void
29451713e81bSScott Long *os_alloc_dma_page(_VBUS_ARG0)
29461713e81bSScott Long {
2947d2bd3ab9SScott Long 	return (void *)contigmalloc(0x1000, M_DEVBUF, M_NOWAIT, 0x1000000, 0xffffffff, PAGE_SIZE, 0ul);
29481713e81bSScott Long }
29491713e81bSScott Long 
29501713e81bSScott Long void
29511713e81bSScott Long os_free_page(_VBUS_ARG void *p)
29521713e81bSScott Long {
2953*d1bdc282SBjoern A. Zeeb 	free(p, M_DEVBUF);
29541713e81bSScott Long }
29551713e81bSScott Long 
29561713e81bSScott Long void
29571713e81bSScott Long os_free_dma_page(_VBUS_ARG void *p)
29581713e81bSScott Long {
2959*d1bdc282SBjoern A. Zeeb 	free(p, M_DEVBUF);
29601713e81bSScott Long }
29611713e81bSScott Long 
29621713e81bSScott Long void
29631713e81bSScott Long DoXor1(ULONG *p0, ULONG *p1, ULONG *p2, UINT nBytes)
29641713e81bSScott Long {
29651713e81bSScott Long 	UINT i;
2966d2bd3ab9SScott Long 	for (i = 0; i < nBytes / 4; i++) *p0++ = *p1++ ^ *p2++;
29671713e81bSScott Long }
29681713e81bSScott Long 
29691713e81bSScott Long void
29701713e81bSScott Long DoXor2(ULONG *p0, ULONG *p2, UINT nBytes)
29711713e81bSScott Long {
29721713e81bSScott Long 	UINT i;
2973d2bd3ab9SScott Long 	for (i = 0; i < nBytes / 4; i++) *p0++ ^= *p2++;
29741713e81bSScott Long }
29751713e81bSScott Long #endif
2976