entry.c revision 136849
1136849Sscottl/*
2136849Sscottl * Copyright (c) 2003-2004 HighPoint Technologies, Inc.
3136849Sscottl * All rights reserved.
4136849Sscottl *
5136849Sscottl * Redistribution and use in source and binary forms, with or without
6136849Sscottl * modification, are permitted provided that the following conditions
7136849Sscottl * are met:
8136849Sscottl * 1. Redistributions of source code must retain the above copyright
9136849Sscottl *    notice, this list of conditions and the following disclaimer.
10136849Sscottl * 2. Redistributions in binary form must reproduce the above copyright
11136849Sscottl *    notice, this list of conditions and the following disclaimer in the
12136849Sscottl *    documentation and/or other materials provided with the distribution.
13136849Sscottl *
14136849Sscottl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15136849Sscottl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16136849Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17136849Sscottl * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18136849Sscottl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19136849Sscottl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20136849Sscottl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21136849Sscottl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22136849Sscottl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23136849Sscottl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24136849Sscottl * SUCH DAMAGE.
25136849Sscottl *
26136849Sscottl * $FreeBSD: head/sys/dev/hptmv/entry.c 136849 2004-10-24 05:37:23Z scottl $
27136849Sscottl */
28136849Sscottl#include <sys/param.h>
29136849Sscottl#include <sys/systm.h>
30136849Sscottl#include <sys/kernel.h>
31136849Sscottl#include <sys/bus.h>
32136849Sscottl#include <sys/malloc.h>
33136849Sscottl#include <sys/resource.h>
34136849Sscottl#include <sys/time.h>
35136849Sscottl#include <sys/callout.h>
36136849Sscottl#include <sys/signalvar.h>
37136849Sscottl#include <sys/eventhandler.h>
38136849Sscottl#include <sys/proc.h>
39136849Sscottl#include <sys/kthread.h>
40136849Sscottl
41136849Sscottl#include <dev/pci/pcireg.h>
42136849Sscottl#include <dev/pci/pcivar.h>
43136849Sscottl
44136849Sscottl#include <dev/hptmv/global.h>
45136849Sscottl#include <dev/hptmv/hptintf.h>
46136849Sscottl#include <dev/hptmv/osbsd.h>
47136849Sscottl#include <contrib/dev/hptmv/access601.h>
48136849Sscottl
49136849Sscottl#ifdef DEBUG
50136849Sscottl#ifdef DEBUG_LEVEL
51136849Sscottlint hpt_dbg_level = DEBUG_LEVEL;
52136849Sscottl#else
53136849Sscottlint hpt_dbg_level = 0;
54136849Sscottl#endif
55136849Sscottl#endif
56136849Sscottl
57136849Sscottl#define MV_ERROR printf
58136849Sscottl/*
59136849Sscottl * CAM SIM entry points
60136849Sscottl */
61136849Sscottlstatic int	hpt_probe(device_t dev);
62136849Sscottlstatic int	hpt_attach(device_t dev);
63136849Sscottlstatic int	hpt_detach(device_t dev);
64136849Sscottlstatic int	hpt_shutdown(device_t dev);
65136849Sscottlstatic void	hpt_poll(struct cam_sim *sim);
66136849Sscottlstatic void	hpt_intr(void *arg);
67136849Sscottlstatic void	hpt_action(struct cam_sim *sim, union ccb *ccb);
68136849Sscottlstatic void	SetInquiryData(PINQUIRYDATA inquiryData, PVDevice pVDev);
69136849Sscottlstatic void	HPTLIBAPI OsSendCommand (_VBUS_ARG union ccb * ccb);
70136849Sscottlstatic void	HPTLIBAPI fOsCommandDone(_VBUS_ARG PCommand pCmd);
71136849Sscottlstatic void	ccb_done(union ccb *ccb);
72136849Sscottlstatic void	hpt_queue_ccb(union ccb **ccb_Q, union ccb *ccb);
73136849Sscottlstatic void	hpt_free_ccb(union ccb **ccb_Q, union ccb *ccb);
74136849Sscottlstatic MV_SATA_CHANNEL gMvSataChannels[MAX_VBUS][MV_SATA_CHANNELS_NUM];
75136849Sscottlstatic void	hptmv_free_edma_queues(IAL_ADAPTER_T *pAdapter);
76136849Sscottlstatic void	hptmv_free_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum);
77136849Sscottlstatic void	handleEdmaError(_VBUS_ARG PCommand pCmd);
78136849Sscottlstatic int	hptmv_init_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum);
79136849Sscottlstatic int	fResetActiveCommands(PVBus _vbus_p);
80136849Sscottlstatic void	fRegisterVdevice(IAL_ADAPTER_T *pAdapter);
81136849Sscottlstatic int	hptmv_allocate_edma_queues(IAL_ADAPTER_T *pAdapter);
82136849Sscottlstatic void	hptmv_handle_event_disconnect(void *data);
83136849Sscottlstatic void	hptmv_handle_event_connect(void *data);
84136849Sscottlstatic int	start_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum);
85136849Sscottlstatic void	init_vdev_params(IAL_ADAPTER_T *pAdapter, MV_U8 channel);
86136849Sscottlstatic int	hptmv_parse_identify_results(MV_SATA_CHANNEL *pMvSataChannel);
87136849Sscottlstatic void	hpt_async(void *callback_arg, u_int32_t code,
88136849Sscottl    struct cam_path *path, void *arg);
89136849Sscottlstatic int HPTLIBAPI fOsBuildSgl(_VBUS_ARG PCommand pCmd, FPSCAT_GATH pSg,
90136849Sscottl    int logical);
91136849Sscottlstatic MV_BOOLEAN CommandCompletionCB(MV_SATA_ADAPTER *pMvSataAdapter,
92136849Sscottl    MV_U8 channelNum, MV_COMPLETION_TYPE comp_type, MV_VOID_PTR commandId,
93136849Sscottl    MV_U16 responseFlags, MV_U32 timeStamp,
94136849Sscottl    MV_STORAGE_DEVICE_REGISTERS *registerStruct);
95136849Sscottlstatic MV_BOOLEAN hptmv_event_notify(MV_SATA_ADAPTER *pMvSataAdapter,
96136849Sscottl    MV_EVENT_TYPE eventType, MV_U32 param1, MV_U32 param2);
97136849Sscottl
98136849Sscottl#define ccb_ccb_ptr spriv_ptr0
99136849Sscottl#define ccb_adapter ccb_h.spriv_ptr1
100136849Sscottl
101136849SscottlIAL_ADAPTER_T *gIal_Adapter = 0;
102136849SscottlIAL_ADAPTER_T *pCurAdapter = 0;
103136849Sscottl
104136849Sscottltypedef struct st_HPT_DPC {
105136849Sscottl	IAL_ADAPTER_T *pAdapter;
106136849Sscottl	void (*dpc)(IAL_ADAPTER_T *, void *, UCHAR);
107136849Sscottl	void *arg;
108136849Sscottl	UCHAR flags;
109136849Sscottl} ST_HPT_DPC;
110136849Sscottl
111136849Sscottl#define MAX_DPC 16
112136849SscottlUCHAR DPC_Request_Nums = 0;
113136849Sscottlstatic ST_HPT_DPC DpcQueue[MAX_DPC];
114136849Sscottlstatic int DpcQueue_First=0;
115136849Sscottlstatic int DpcQueue_Last = 0;
116136849Sscottl
117136849Sscottlstatic device_method_t driver_methods[] = {
118136849Sscottl	/* Device interface */
119136849Sscottl	DEVMETHOD(device_probe,		hpt_probe),
120136849Sscottl	DEVMETHOD(device_attach,	hpt_attach),
121136849Sscottl	DEVMETHOD(device_detach,	hpt_detach),
122136849Sscottl	{ 0, 0 }
123136849Sscottl};
124136849Sscottl
125136849Sscottlstatic driver_t hpt_pci_driver = {
126136849Sscottl	__str(PROC_DIR_NAME),
127136849Sscottl	driver_methods,
128136849Sscottl	sizeof(IAL_ADAPTER_T)
129136849Sscottl};
130136849Sscottl
131136849Sscottlstatic devclass_t	hpt_devclass;
132136849Sscottl
133136849SscottlDRIVER_MODULE(PROC_DIR_NAME, pci, hpt_pci_driver, hpt_devclass, 0, 0);
134136849SscottlMODULE_DEPEND(PROC_DIR_NAME, cam, 1, 1, 1);
135136849Sscottl
136136849Sscottlintrmask_t
137136849Sscottllock_driver()
138136849Sscottl{
139136849Sscottl	intrmask_t spl = splcam();
140136849Sscottl	return spl;
141136849Sscottl}
142136849Sscottl
143136849Sscottlvoid
144136849Sscottlunlock_driver(intrmask_t spl)
145136849Sscottl{
146136849Sscottl	splx(spl);
147136849Sscottl}
148136849Sscottl
149136849Sscottl/*******************************************************************************
150136849Sscottl *	Name:	hptmv_free_channel
151136849Sscottl *
152136849Sscottl *	Description:	free allocated queues for the given channel
153136849Sscottl *
154136849Sscottl *	Parameters:    	pMvSataAdapter - pointer to the RR182x controler this
155136849Sscottl * 					channel connected to.
156136849Sscottl *			channelNum - channel number.
157136849Sscottl *
158136849Sscottl ******************************************************************************/
159136849Sscottlstatic void
160136849Sscottlhptmv_free_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum)
161136849Sscottl{
162136849Sscottl	PVDevice pVDev = &(pAdapter->VDevices[channelNum]);
163136849Sscottl	_VBUS_INST(&pAdapter->VBus);
164136849Sscottl
165136849Sscottl	HPT_ASSERT(channelNum < MV_SATA_CHANNELS_NUM);
166136849Sscottl
167136849Sscottl	pAdapter->mvSataAdapter.sataChannel[channelNum] = NULL;
168136849Sscottl
169136849Sscottl	if(pVDev->vf_online)
170136849Sscottl	{
171136849Sscottl		pVDev->u.disk.df_on_line = 0;
172136849Sscottl		pVDev->vf_online = 0;
173136849Sscottl		if (pVDev->pfnDeviceFailed) {
174136849Sscottl			CallWhenIdle(_VBUS_P (DPC_PROC)pVDev->pfnDeviceFailed,
175136849Sscottl				     pVDev);
176136849Sscottl		}
177136849Sscottl	}
178136849Sscottl}
179136849Sscottl
180136849Sscottlint MvSataResetChannel(MV_SATA_ADAPTER *pMvSataAdapter, MV_U8 channel);
181136849Sscottl
182136849Sscottlstatic void
183136849SscottlhandleEdmaError(_VBUS_ARG PCommand pCmd)
184136849Sscottl{
185136849Sscottl	PDevice pDevice = &pCmd->pVDevice->u.disk;
186136849Sscottl	MV_SATA_ADAPTER * pSataAdapter = pDevice->mv->mvSataAdapter;
187136849Sscottl
188136849Sscottl	MV_ERROR("Reset channel\n");
189136849Sscottl
190136849Sscottl	MvSataResetChannel(pSataAdapter, pDevice->mv->channelNumber);
191136849Sscottl	/*now no other cmds on this channel*/
192136849Sscottl	if (!pDevice->df_on_line) {
193136849Sscottl		KdPrint(("Device is offline"));
194136849Sscottl		pCmd->Result = RETURN_BAD_DEVICE;
195136849Sscottl		CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
196136849Sscottl		return;
197136849Sscottl	}
198136849Sscottl
199136849Sscottl	if (pCmd->RetryCount++>5) {
200136849Sscottl		pDevice->df_on_line = 0;
201136849Sscottl		pCmd->pVDevice->vf_online = 0;
202136849Sscottl		if (pCmd->pVDevice->pfnDeviceFailed)
203136849Sscottl			CallWhenIdle(_VBUS_P
204136849Sscottl			    (DPC_PROC)pCmd->pVDevice->pfnDeviceFailed,
205136849Sscottl			    pCmd->pVDevice);
206136849Sscottl		fNotifyGUI(ET_DEVICE_REMOVED, Map2pVDevice(pDevice));
207136849Sscottl		pCmd->Result = RETURN_IDE_ERROR;
208136849Sscottl		CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
209136849Sscottl		return;
210136849Sscottl	}
211136849Sscottl	/* retry the command */
212136849Sscottl	fDeviceSendCommand(_VBUS_P pCmd);
213136849Sscottl}
214136849Sscottl
215136849Sscottl/****************************************************************
216136849Sscottl *	Name:	hptmv_init_channel
217136849Sscottl *
218136849Sscottl *	Description:	allocate request and response queues for the EDMA of
219136849Sscottl *                      the given channel and sets other fields.
220136849Sscottl *	Parameters:
221136849Sscottl *		pAdapter - pointer to the emulated adapter data structure
222136849Sscottl *		channelNum - channel number.
223136849Sscottl *	Return:	0 on success, otherwise on failure
224136849Sscottl ****************************************************************/
225136849Sscottlstatic int
226136849Sscottlhptmv_init_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum)
227136849Sscottl{
228136849Sscottl	MV_SATA_CHANNEL *pMvSataChannel;
229136849Sscottl	dma_addr_t    req_dma_addr;
230136849Sscottl	dma_addr_t    rsp_dma_addr;
231136849Sscottl
232136849Sscottl	if (channelNum >= MV_SATA_CHANNELS_NUM)
233136849Sscottl	{
234136849Sscottl		MV_ERROR("RR182x[%d]: Bad channelNum=%d",
235136849Sscottl				 pAdapter->mvSataAdapter.adapterId, channelNum);
236136849Sscottl		return -1;
237136849Sscottl	}
238136849Sscottl
239136849Sscottl	pMvSataChannel =
240136849Sscottl	    &gMvSataChannels[pAdapter->mvSataAdapter.adapterId][channelNum];
241136849Sscottl	pAdapter->mvSataAdapter.sataChannel[channelNum] = pMvSataChannel;
242136849Sscottl	pMvSataChannel->channelNumber = channelNum;
243136849Sscottl	pMvSataChannel->lba48Address = MV_FALSE;
244136849Sscottl	pMvSataChannel->maxReadTransfer = MV_FALSE;
245136849Sscottl
246136849Sscottl	pMvSataChannel->requestQueue =
247136849Sscottl	    (struct mvDmaRequestQueueEntry *)
248136849Sscottl	    (pAdapter->requestsArrayBaseAlignedAddr +
249136849Sscottl	    (channelNum * MV_EDMA_REQUEST_QUEUE_SIZE));
250136849Sscottl	req_dma_addr = pAdapter->requestsArrayBaseDmaAlignedAddr +
251136849Sscottl	    (channelNum * MV_EDMA_REQUEST_QUEUE_SIZE);
252136849Sscottl
253136849Sscottl
254136849Sscottl	KdPrint(("requestQueue addr is 0x%X", req_dma_addr));
255136849Sscottl
256136849Sscottl	/* check the 1K alignment of the request queue*/
257136849Sscottl	if (req_dma_addr & 0x3ff)
258136849Sscottl	{
259136849Sscottl		MV_ERROR("RR182x[%d]: request queue allocated not 1 K aligned,"
260136849Sscottl			 " dma_addr=%lx channel=%d\n",
261136849Sscottl			 pAdapter->mvSataAdapter.adapterId,(u_long)req_dma_addr,
262136849Sscottl			 channelNum);
263136849Sscottl		return -1;
264136849Sscottl	}
265136849Sscottl	pMvSataChannel->requestQueuePciLowAddress = req_dma_addr;
266136849Sscottl	pMvSataChannel->requestQueuePciHiAddress = 0;
267136849Sscottl	KdPrint(("RR182x[%d,%d]: request queue allocated: 0x%p",
268136849Sscottl		pAdapter->mvSataAdapter.adapterId, channelNum,
269136849Sscottl		pMvSataChannel->requestQueue));
270136849Sscottl	pMvSataChannel->responseQueue =
271136849Sscottl	    (struct mvDmaResponseQueueEntry *)
272136849Sscottl	    (pAdapter->responsesArrayBaseAlignedAddr +
273136849Sscottl	    (channelNum * MV_EDMA_RESPONSE_QUEUE_SIZE));
274136849Sscottl	rsp_dma_addr = pAdapter->responsesArrayBaseDmaAlignedAddr +
275136849Sscottl	    (channelNum * MV_EDMA_RESPONSE_QUEUE_SIZE);
276136849Sscottl
277136849Sscottl	/* check the 256 alignment of the response queue*/
278136849Sscottl	if (rsp_dma_addr & 0xff)
279136849Sscottl	{
280136849Sscottl		MV_ERROR("RR182x[%d,%d]: response queue allocated not 256 byte"
281136849Sscottl			 " aligned, dma_addr=%lx\n",
282136849Sscottl			 pAdapter->mvSataAdapter.adapterId, channelNum,
283136849Sscottl			 (u_long)rsp_dma_addr);
284136849Sscottl		return -1;
285136849Sscottl	}
286136849Sscottl	pMvSataChannel->responseQueuePciLowAddress = rsp_dma_addr;
287136849Sscottl	pMvSataChannel->responseQueuePciHiAddress = 0;
288136849Sscottl	KdPrint(("RR182x[%d,%d]: response queue allocated: 0x%p",
289136849Sscottl		pAdapter->mvSataAdapter.adapterId, channelNum,
290136849Sscottl		pMvSataChannel->responseQueue));
291136849Sscottl
292136849Sscottl	pAdapter->mvChannel[channelNum].online = MV_TRUE;
293136849Sscottl	return 0;
294136849Sscottl}
295136849Sscottl
296136849Sscottl/******************************************************************************
297136849Sscottl *	Name: hptmv_parse_identify_results
298136849Sscottl *
299136849Sscottl *	Description:	this functions parses the identify command results,
300136849Sscottl *			checks that the connected deives can be accesed by
301136849Sscottl *			RR182x EDMA, and updates the channel stucture
302136849Sscottl *			accordingly.
303136849Sscottl *	Parameters:     pMvSataChannel, pointer to the channel data structure.
304136849Sscottl *
305136849Sscottl *	Returns:       	=0 ->success, < 0 ->failure.
306136849Sscottl *
307136849Sscottl ******************************************************************************/
308136849Sscottlstatic int
309136849Sscottlhptmv_parse_identify_results(MV_SATA_CHANNEL *pMvSataChannel)
310136849Sscottl{
311136849Sscottl	MV_U16  *iden = pMvSataChannel->identifyDevice;
312136849Sscottl
313136849Sscottl	/*LBA addressing*/
314136849Sscottl	if (! (iden[IDEN_CAPACITY_1_OFFSET] & 0x200)) {
315136849Sscottl		KdPrint(("IAL Error in IDENTIFY info: LBA not supported\n"));
316136849Sscottl		return -1;
317136849Sscottl	} else {
318136849Sscottl		KdPrint(("%25s - %s\n", "Capabilities", "LBA supported"));
319136849Sscottl	}
320136849Sscottl	/*DMA support*/
321136849Sscottl	if (! (iden[IDEN_CAPACITY_1_OFFSET] & 0x100)) {
322136849Sscottl		KdPrint(("IAL Error in IDENTIFY info: DMA not supported\n"));
323136849Sscottl		return -1;
324136849Sscottl	} else {
325136849Sscottl		KdPrint(("%25s - %s\n", "Capabilities", "DMA supported"));
326136849Sscottl	}
327136849Sscottl	/* PIO */
328136849Sscottl	if ((iden[IDEN_VALID] & 2) == 0) {
329136849Sscottl		KdPrint(("IAL Error in IDENTIFY info: not able to find PIO "
330136849Sscottl			"mode\n"));
331136849Sscottl		return -1;
332136849Sscottl	}
333136849Sscottl	KdPrint(("%25s - 0x%02x\n", "PIO modes supported",
334136849Sscottl			  iden[IDEN_PIO_MODE_SPPORTED] & 0xff));
335136849Sscottl
336136849Sscottl	/*UDMA*/
337136849Sscottl	if ((iden[IDEN_VALID] & 4) == 0) {
338136849Sscottl		KdPrint(("IAL Error in IDENTIFY info: not able to find UDMA "
339136849Sscottl			"mode\n"));
340136849Sscottl		return -1;
341136849Sscottl	}
342136849Sscottl
343136849Sscottl	/* 48 bit address */
344136849Sscottl	if ((iden[IDEN_SUPPORTED_COMMANDS2] & 0x400)) {
345136849Sscottl		KdPrint(("%25s - %s\n", "LBA48 addressing", "supported"));
346136849Sscottl		pMvSataChannel->lba48Address = MV_TRUE;
347136849Sscottl	} else {
348136849Sscottl		KdPrint(("%25s - %s\n", "LBA48 addressing", "Not supported"));
349136849Sscottl		pMvSataChannel->lba48Address = MV_FALSE;
350136849Sscottl	}
351136849Sscottl	return 0;
352136849Sscottl}
353136849Sscottl
354136849Sscottlstatic void
355136849Sscottlinit_vdev_params(IAL_ADAPTER_T *pAdapter, MV_U8 channel)
356136849Sscottl{
357136849Sscottl	PVDevice pVDev;
358136849Sscottl	MV_SATA_CHANNEL *pMvSataChannel;
359136849Sscottl	MV_U16_PTR IdentifyData;
360136849Sscottl
361136849Sscottl	pVDev = &pAdapter->VDevices[channel];
362136849Sscottl	pMvSataChannel = pAdapter->mvSataAdapter.sataChannel[channel];
363136849Sscottl	pMvSataChannel->outstandingCommands = 0;
364136849Sscottl	IdentifyData = pMvSataChannel->identifyDevice;
365136849Sscottl
366136849Sscottl	pVDev->u.disk.mv         = pMvSataChannel;
367136849Sscottl	pVDev->u.disk.df_on_line = 1;
368136849Sscottl	pVDev->u.disk.pVBus      = &pAdapter->VBus;
369136849Sscottl	pVDev->pVBus             = &pAdapter->VBus;
370136849Sscottl
371136849Sscottl#ifdef SUPPORT_48BIT_LBA
372136849Sscottl	if (pMvSataChannel->lba48Address == MV_TRUE)
373136849Sscottl		pVDev->u.disk.dDeRealCapacity =
374136849Sscottl		    ((IdentifyData[101]<<16) | IdentifyData[100]) - 1;
375136849Sscottl	else
376136849Sscottl#endif
377136849Sscottl	if(IdentifyData[53] & 1) {
378136849Sscottl		pVDev->u.disk.dDeRealCapacity =
379136849Sscottl		    (((IdentifyData[58]<<16 | IdentifyData[57]) <
380136849Sscottl		    (IdentifyData[61]<<16 | IdentifyData[60])) ?
381136849Sscottl		    (IdentifyData[61]<<16 | IdentifyData[60]) :
382136849Sscottl		    (IdentifyData[58]<<16 | IdentifyData[57])) - 1;
383136849Sscottl	} else
384136849Sscottl		pVDev->u.disk.dDeRealCapacity =
385136849Sscottl		    (IdentifyData[61]<<16 | IdentifyData[60]) - 1;
386136849Sscottl
387136849Sscottl	pVDev->u.disk.bDeUsable_Mode = pVDev->u.disk.bDeModeSetting =
388136849Sscottl	    pAdapter->mvChannel[channel].maxPioModeSupported -
389136849Sscottl	    MV_ATA_TRANSFER_PIO_0;
390136849Sscottl
391136849Sscottl	if (pAdapter->mvChannel[channel].maxUltraDmaModeSupported!=0xFF) {
392136849Sscottl		pVDev->u.disk.bDeUsable_Mode = pVDev->u.disk.bDeModeSetting =
393136849Sscottl		    pAdapter->mvChannel[channel].maxUltraDmaModeSupported -
394136849Sscottl		    MV_ATA_TRANSFER_UDMA_0 + 8;
395136849Sscottl	}
396136849Sscottl}
397136849Sscottl
398136849Sscottlstatic void
399136849Sscottldevice_change(IAL_ADAPTER_T *pAdapter , MV_U8 channelIndex, int plugged)
400136849Sscottl{
401136849Sscottl	PVDevice pVDev;
402136849Sscottl	MV_SATA_ADAPTER *pMvSataAdapter;
403136849Sscottl	MV_SATA_CHANNEL *pMvSataChannel;
404136849Sscottl	PVBus _vbus_p;
405136849Sscottl
406136849Sscottl	pMvSataAdapter = &pAdapter->mvSataAdapter;
407136849Sscottl	pMvSataChannel = pMvSataAdapter->sataChannel[channelIndex];
408136849Sscottl	_vbus_p  = &pAdapter->VBus;
409136849Sscottl
410136849Sscottl	if (!pMvSataChannel)
411136849Sscottl		return;
412136849Sscottl
413136849Sscottl	if (plugged) {
414136849Sscottl		pVDev = &(pAdapter->VDevices[channelIndex]);
415136849Sscottl		init_vdev_params(pAdapter, channelIndex);
416136849Sscottl
417136849Sscottl		pVDev->VDeviceType = pVDev->u.disk.df_atapi ? VD_ATAPI :
418136849Sscottl		    pVDev->u.disk.df_removable_drive ? VD_REMOVABLE :
419136849Sscottl		    VD_SINGLE_DISK;
420136849Sscottl
421136849Sscottl		pVDev->VDeviceCapacity = pVDev->u.disk.dDeRealCapacity;
422136849Sscottl		pVDev->pfnSendCommand = pfnSendCommand[pVDev->VDeviceType];
423136849Sscottl		pVDev->pfnDeviceFailed = pfnDeviceFailed[pVDev->VDeviceType];
424136849Sscottl		pVDev->vf_online = 1;
425136849Sscottl
426136849Sscottl#ifdef SUPPORT_ARRAY
427136849Sscottl		if(pVDev->pParent) {
428136849Sscottl			int iMember;
429136849Sscottl
430136849Sscottl			for (iMember = 0;
431136849Sscottl			     iMember < pVDev->pParent->u.array.bArnMember;
432136849Sscottl			     iMember++)
433136849Sscottl				if ((PVDevice)pVDev->pParent->u.array.pMember[iMember] == pVDev)
434136849Sscottl					pVDev->pParent->u.array.pMember[iMember] = NULL;
435136849Sscottl			pVDev->pParent = NULL;
436136849Sscottl		}
437136849Sscottl#endif
438136849Sscottl		fNotifyGUI(ET_DEVICE_PLUGGED,pVDev);
439136849Sscottl		fCheckBootable(pVDev);
440136849Sscottl		RegisterVDevice(pVDev);
441136849Sscottl
442136849Sscottl#ifndef FOR_DEMO
443136849Sscottl		if (pAdapter->beeping) {
444136849Sscottl			pAdapter->beeping = 0;
445136849Sscottl			BeepOff(pAdapter->mvSataAdapter.adapterIoBaseAddress);
446136849Sscottl		}
447136849Sscottl#endif
448136849Sscottl
449136849Sscottl	} else {
450136849Sscottl		pVDev  = &(pAdapter->VDevices[channelIndex]);
451136849Sscottl		pVDev->u.disk.df_on_line = 0;
452136849Sscottl		pVDev->vf_online = 0;
453136849Sscottl		if (pVDev->pfnDeviceFailed) {
454136849Sscottl			_VBUS_INST(&pAdapter->VBus)
455136849Sscottl			CallWhenIdle(_VBUS_P (DPC_PROC)pVDev->pfnDeviceFailed,
456136849Sscottl				     pVDev);
457136849Sscottl		}
458136849Sscottl		fNotifyGUI(ET_DEVICE_REMOVED,pVDev);
459136849Sscottl
460136849Sscottl#ifndef FOR_DEMO
461136849Sscottl		if (pAdapter->ver_601==2 && !pAdapter->beeping) {
462136849Sscottl			pAdapter->beeping = 1;
463136849Sscottl			BeepOn(pAdapter->mvSataAdapter.adapterIoBaseAddress);
464136849Sscottl			set_fail_led(&pAdapter->mvSataAdapter, channelIndex, 1);
465136849Sscottl		}
466136849Sscottl#endif
467136849Sscottl
468136849Sscottl	}
469136849Sscottl}
470136849Sscottl
471136849Sscottlstatic int
472136849Sscottlstart_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum)
473136849Sscottl{
474136849Sscottl	MV_SATA_ADAPTER *pMvSataAdapter;
475136849Sscottl	MV_SATA_CHANNEL *pMvSataChannel;
476136849Sscottl	MV_CHANNEL *pChannelInfo;
477136849Sscottl	MV_U32 udmaMode,pioMode;
478136849Sscottl
479136849Sscottl	pMvSataAdapter = &pAdapter->mvSataAdapter;
480136849Sscottl	pMvSataChannel = pMvSataAdapter->sataChannel[channelNum];
481136849Sscottl	pChannelInfo = &(pAdapter->mvChannel[channelNum]);
482136849Sscottl
483136849Sscottl	KdPrint(("RR182x [%d]: start channel (%d)", pMvSataAdapter->adapterId,
484136849Sscottl		channelNum));
485136849Sscottl
486136849Sscottl
487136849Sscottl	/* Software reset channel */
488136849Sscottl	if (mvStorageDevATASoftResetDevice(pMvSataAdapter, channelNum) ==
489136849Sscottl	    MV_FALSE) {
490136849Sscottl		MV_ERROR("RR182x [%d,%d]: failed to perform Software reset\n",
491136849Sscottl			 pMvSataAdapter->adapterId, channelNum);
492136849Sscottl		return -1;
493136849Sscottl	}
494136849Sscottl
495136849Sscottl	/* Hardware reset channel */
496136849Sscottl	if (mvSataChannelHardReset(pMvSataAdapter, channelNum) == MV_FALSE) {
497136849Sscottl		/*
498136849Sscottl		 * If failed, try again - this is when trying to hardreset a
499136849Sscottl		 * channel when drive is just spinning up
500136849Sscottl		 */
501136849Sscottl		StallExec(5000000); /* wait 5 sec before trying again */
502136849Sscottl		if (mvSataChannelHardReset(pMvSataAdapter, channelNum) ==
503136849Sscottl		    MV_FALSE) {
504136849Sscottl			MV_ERROR("RR182x [%d,%d]: failed to perform Hard "
505136849Sscottl				 "reset\n", pMvSataAdapter->adapterId,
506136849Sscottl				 channelNum);
507136849Sscottl			return -1;
508136849Sscottl		}
509136849Sscottl	}
510136849Sscottl
511136849Sscottl	/* identify device*/
512136849Sscottl	if (mvStorageDevATAIdentifyDevice(pMvSataAdapter, channelNum) ==
513136849Sscottl	    MV_FALSE) {
514136849Sscottl		MV_ERROR("RR182x [%d,%d]: failed to perform ATA Identify "
515136849Sscottl			 "command\n", pMvSataAdapter->adapterId, channelNum);
516136849Sscottl		return -1;
517136849Sscottl	}
518136849Sscottl	if (hptmv_parse_identify_results(pMvSataChannel)) {
519136849Sscottl		MV_ERROR("RR182x [%d,%d]: Error in parsing ATA Identify "
520136849Sscottl			 "message\n", pMvSataAdapter->adapterId, channelNum);
521136849Sscottl		return -1;
522136849Sscottl	}
523136849Sscottl
524136849Sscottl	/* mvStorageDevATASetFeatures */
525136849Sscottl	/* Disable 8 bit PIO in case CFA enabled */
526136849Sscottl	if (pMvSataChannel->identifyDevice[86] & 4) {
527136849Sscottl		KdPrint(("RR182x [%d]: Disable 8 bit PIO (CFA enabled) \n",
528136849Sscottl			pMvSataAdapter->adapterId));
529136849Sscottl		if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
530136849Sscottl		    MV_ATA_SET_FEATURES_DISABLE_8_BIT_PIO, 0, 0, 0, 0) ==
531136849Sscottl		    MV_FALSE) {
532136849Sscottl			MV_ERROR("RR182x [%d]: channel %d: "
533136849Sscottl				 "mvStorageDevATASetFeatures failed\n",
534136849Sscottl				 pMvSataAdapter->adapterId, channelNum);
535136849Sscottl			return -1;
536136849Sscottl		}
537136849Sscottl	}
538136849Sscottl
539136849Sscottl#ifdef ENABLE_WRITE_CACHE
540136849Sscottl	/* Write cache */
541136849Sscottl	if (pMvSataChannel->identifyDevice[82] & 0x20) {
542136849Sscottl		if (!(pMvSataChannel->identifyDevice[85] & 0x20)) {
543136849Sscottl			/* if not enabled by default */
544136849Sscottl			if (mvStorageDevATASetFeatures(pMvSataAdapter,
545136849Sscottl			    channelNum, MV_ATA_SET_FEATURES_ENABLE_WCACHE, 0,
546136849Sscottl			    0, 0, 0) == MV_FALSE) {
547136849Sscottl				MV_ERROR("RR182x [%d]: channel %d: "
548136849Sscottl					 "mvStorageDevATASetFeatures failed\n",
549136849Sscottl					 pMvSataAdapter->adapterId, channelNum);
550136849Sscottl				return -1;
551136849Sscottl			}
552136849Sscottl		}
553136849Sscottl		KdPrint(("RR182x [%d]: channel %d, write cache enabled\n",
554136849Sscottl			pMvSataAdapter->adapterId, channelNum));
555136849Sscottl	} else {
556136849Sscottl		KdPrint(("RR182x [%d]: channel %d, write cache not supported\n",
557136849Sscottl			pMvSataAdapter->adapterId, channelNum));
558136849Sscottl	}
559136849Sscottl#else
560136849Sscottl	/* disable write cache */
561136849Sscottl	if (pMvSataChannel->identifyDevice[85] & 0x20) {
562136849Sscottl		KdPrint(("RR182x [%d]: channel =%d, disable write cache\n",
563136849Sscottl			pMvSataAdapter->adapterId, channelNum));
564136849Sscottl		if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
565136849Sscottl		    MV_ATA_SET_FEATURES_DISABLE_WCACHE, 0, 0, 0, 0) ==
566136849Sscottl		    MV_FALSE) {
567136849Sscottl			MV_ERROR("RR182x [%d]: channel %d: "
568136849Sscottl				 "mvStorageDevATASetFeatures failed\n",
569136849Sscottl				 pMvSataAdapter->adapterId, channelNum);
570136849Sscottl			return -1;
571136849Sscottl		}
572136849Sscottl	}
573136849Sscottl	KdPrint(("RR182x [%d]: channel=%d, write cache disabled\n",
574136849Sscottl		pMvSataAdapter->adapterId, channelNum));
575136849Sscottl#endif
576136849Sscottl
577136849Sscottl	/* Set transfer mode */
578136849Sscottl	KdPrint(("RR182x [%d] Set transfer mode XFER_PIO_SLOW\n",
579136849Sscottl		pMvSataAdapter->adapterId));
580136849Sscottl	if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
581136849Sscottl	    MV_ATA_SET_FEATURES_TRANSFER, MV_ATA_TRANSFER_PIO_SLOW, 0, 0, 0) ==
582136849Sscottl	    MV_FALSE) {
583136849Sscottl		MV_ERROR("RR182x [%d] channel %d: Set Features failed\n",
584136849Sscottl			 pMvSataAdapter->adapterId, channelNum);
585136849Sscottl		return -1;
586136849Sscottl	}
587136849Sscottl
588136849Sscottl	if (pMvSataChannel->identifyDevice[IDEN_PIO_MODE_SPPORTED] & 1) {
589136849Sscottl		pioMode = MV_ATA_TRANSFER_PIO_4;
590136849Sscottl	} else if (pMvSataChannel->identifyDevice[IDEN_PIO_MODE_SPPORTED] & 2) {
591136849Sscottl		pioMode = MV_ATA_TRANSFER_PIO_3;
592136849Sscottl	} else {
593136849Sscottl		MV_ERROR("IAL Error in IDENTIFY info: PIO modes 3 and 4 not "
594136849Sscottl			 "supported\n");
595136849Sscottl		pioMode = MV_ATA_TRANSFER_PIO_SLOW;
596136849Sscottl	}
597136849Sscottl
598136849Sscottl	KdPrint(("RR182x [%d] Set transfer mode XFER_PIO_4\n",
599136849Sscottl		pMvSataAdapter->adapterId));
600136849Sscottl	pAdapter->mvChannel[channelNum].maxPioModeSupported = pioMode;
601136849Sscottl	if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
602136849Sscottl	    MV_ATA_SET_FEATURES_TRANSFER, pioMode, 0, 0, 0) == MV_FALSE) {
603136849Sscottl		MV_ERROR("RR182x [%d] channel %d: Set Features failed\n",
604136849Sscottl			 pMvSataAdapter->adapterId, channelNum);
605136849Sscottl		return -1;
606136849Sscottl	}
607136849Sscottl
608136849Sscottl	udmaMode = MV_ATA_TRANSFER_UDMA_0;
609136849Sscottl	if (pMvSataChannel->identifyDevice[IDEN_UDMA_MODE] & 0x40) {
610136849Sscottl		udmaMode =  MV_ATA_TRANSFER_UDMA_6;
611136849Sscottl	} else if (pMvSataChannel->identifyDevice[IDEN_UDMA_MODE] & 0x20) {
612136849Sscottl		udmaMode =  MV_ATA_TRANSFER_UDMA_5;
613136849Sscottl	} else if (pMvSataChannel->identifyDevice[IDEN_UDMA_MODE] & 0x10) {
614136849Sscottl		udmaMode =  MV_ATA_TRANSFER_UDMA_4;
615136849Sscottl	} else if (pMvSataChannel->identifyDevice[IDEN_UDMA_MODE] & 8) {
616136849Sscottl		udmaMode =  MV_ATA_TRANSFER_UDMA_3;
617136849Sscottl	} else if (pMvSataChannel->identifyDevice[IDEN_UDMA_MODE] & 4) {
618136849Sscottl		udmaMode =  MV_ATA_TRANSFER_UDMA_2;
619136849Sscottl	}
620136849Sscottl
621136849Sscottl	KdPrint(("RR182x [%d] Set transfer mode XFER_UDMA_%d\n",
622136849Sscottl		pMvSataAdapter->adapterId, udmaMode & 0xf));
623136849Sscottl	pChannelInfo->maxUltraDmaModeSupported = udmaMode;
624136849Sscottl
625136849Sscottl#if 0
626136849Sscottl	if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
627136849Sscottl	    MV_ATA_SET_FEATURES_TRANSFER, udmaMode, 0, 0, 0) == MV_FALSE) {
628136849Sscottl		MV_ERROR("RR182x [%d] channel %d: Set Features failed\n",
629136849Sscottl			 pMvSataAdapter->adapterId, channelNum);
630136849Sscottl		return -1;
631136849Sscottl	}
632136849Sscottl#endif
633136849Sscottl	if (pChannelInfo->maxUltraDmaModeSupported == 0xFF)
634136849Sscottl		return TRUE;
635136849Sscottl
636136849Sscottl	do {
637136849Sscottl		if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
638136849Sscottl		    MV_ATA_SET_FEATURES_TRANSFER,
639136849Sscottl		    pChannelInfo->maxUltraDmaModeSupported, 0, 0, 0) !=
640136849Sscottl		    MV_FALSE) {
641136849Sscottl			break;
642136849Sscottl		}
643136849Sscottl
644136849Sscottl		if (pChannelInfo->maxUltraDmaModeSupported <=
645136849Sscottl		    MV_ATA_TRANSFER_UDMA_0) {
646136849Sscottl			return FALSE;
647136849Sscottl		}
648136849Sscottl		if (mvStorageDevATASoftResetDevice(pMvSataAdapter,
649136849Sscottl		    channelNum) == MV_FALSE) {
650136849Sscottl			mv_reg_write_byte(pMvSataAdapter->adapterIoBaseAddress,
651136849Sscottl			    pMvSataChannel->eDmaRegsOffset + 0x11c,
652136849Sscottl			    /* command reg */ MV_ATA_COMMAND_IDLE_IMMEDIATE);
653136849Sscottl			mvMicroSecondsDelay(10000);
654136849Sscottl			mvSataChannelHardReset(pMvSataAdapter, channelNum);
655136849Sscottl			if (mvStorageDevATASoftResetDevice(pMvSataAdapter,
656136849Sscottl			    channelNum) == MV_FALSE)
657136849Sscottl				return FALSE;
658136849Sscottl		}
659136849Sscottl		if (mvSataChannelHardReset(pMvSataAdapter, channelNum) ==
660136849Sscottl		    MV_FALSE)
661136849Sscottl			return FALSE;
662136849Sscottl		pChannelInfo->maxUltraDmaModeSupported--;
663136849Sscottl	} while (1);
664136849Sscottl
665136849Sscottl#ifdef ENABLE_READ_AHEAD
666136849Sscottl	/* Read look ahead */
667136849Sscottl	if (pMvSataChannel->identifyDevice[82] & 0x40) {
668136849Sscottl		if (!(pMvSataChannel->identifyDevice[85] & 0x40)) {
669136849Sscottl			/* if not enabled by default */
670136849Sscottl			if (mvStorageDevATASetFeatures(pMvSataAdapter,
671136849Sscottl			    channelNum, MV_ATA_SET_FEATURES_ENABLE_RLA, 0, 0,
672136849Sscottl			    0, 0) == MV_FALSE) {
673136849Sscottl				MV_ERROR("RR182x [%d] channel %d: Set Features "
674136849Sscottl					 "failed\n", pMvSataAdapter->adapterId,
675136849Sscottl					 channelNum);
676136849Sscottl				return -1;
677136849Sscottl			}
678136849Sscottl		}
679136849Sscottl		KdPrint(("RR182x [%d]: channel=%d, read look ahead enabled\n",
680136849Sscottl			pMvSataAdapter->adapterId, channelNum));
681136849Sscottl	} else {
682136849Sscottl		KdPrint(("RR182x [%d]: channel %d, Read Look Ahead not "
683136849Sscottl			"supported\n", pMvSataAdapter->adapterId, channelNum));
684136849Sscottl	}
685136849Sscottl#else
686136849Sscottl	if (pMvSataChannel->identifyDevice[86] & 0x20) {
687136849Sscottl		KdPrint(("RR182x [%d]:channel %d, disable read look ahead\n",
688136849Sscottl			pMvSataAdapter->adapterId, channelNum));
689136849Sscottl		if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
690136849Sscottl		    MV_ATA_SET_FEATURES_DISABLE_RLA, 0, 0, 0, 0) == MV_FALSE) {
691136849Sscottl			MV_ERROR("RR182x [%d]:channel %d:  ATA Set Features "
692136849Sscottl				 "failed\n", pMvSataAdapter->adapterId,
693136849Sscottl				 channelNum);
694136849Sscottl			return -1;
695136849Sscottl		}
696136849Sscottl	}
697136849Sscottl	KdPrint(("RR182x [%d]:channel %d, read look ahead disabled\n",
698136849Sscottl		  pMvSataAdapter->adapterId, channelNum));
699136849Sscottl#endif
700136849Sscottl
701136849Sscottl#if 0
702136849Sscottl	KdPrint(("RR182x [%d]:channel %d, Set standby timer to 200 seconds\n",
703136849Sscottl			  pMvSataAdapter->adapterId, channelNum));
704136849Sscottl	if (mvStorageDevATAExecuteNonUDMACommand(pMvSataAdapter, channelNum,
705136849Sscottl						 MV_NON_UDMA_PROTOCOL_NON_DATA,
706136849Sscottl						 MV_FALSE,	/* isEXT*/
707136849Sscottl						 NULL, 0, 0,	/* features*/
708136849Sscottl						 40,		/*sectorCount*/
709136849Sscottl						 0,		/*lbaLow*/
710136849Sscottl						 0,		/*lbaMid*/
711136849Sscottl						 0,		/*lbaHigh*/
712136849Sscottl	 					 0,		/*device*/
713136849Sscottl						 MV_ATA_COMMAND_IDLE) ==
714136849Sscottl						 MV_FALSE) {
715136849Sscottl		MV_ERROR("RR182x [%d]:channel %d:  ATA Idle command failed\n",
716136849Sscottl			 pMvSataAdapter->adapterId, channelNum);
717136849Sscottl		return -1;
718136849Sscottl	}
719136849Sscottl#endif
720136849Sscottl
721136849Sscottl#if 0
722136849Sscottl	/* 2003-9-16 disable TCQ until we have better solution */
723136849Sscottl	if ((pMvSataChannel->identifyDevice[IDEN_SUPPORTED_COMMANDS2] & 2)) {
724136849Sscottl		MV_U8       depth;
725136849Sscottl		MV_BOOLEAN  result;
726136849Sscottl
727136849Sscottl		depth = (pMvSataChannel->identifyDevice[IDEN_QUEUE_DEPTH] &
728136849Sscottl		    0x1f) + 1;
729136849Sscottl		KdPrint(("RR182x [%d]: channel %d config EDMA, Queued Mode, "
730136849Sscottl			"queue depth %d\n", pMvSataAdapter->adapterId,
731136849Sscottl			channelNum, depth));
732136849Sscottl		result = mvSataConfigEdmaMode(pMvSataAdapter, channelNum,
733136849Sscottl		    MV_EDMA_MODE_QUEUED, depth);
734136849Sscottl		if (result == MV_FALSE) {
735136849Sscottl			MV_ERROR("RR182x [%d] Error: mvSataConfigEdmaMode "
736136849Sscottl				 "failed\n", pMvSataAdapter->adapterId);
737136849Sscottl			return -1;
738136849Sscottl		}
739136849Sscottl	} else {
740136849Sscottl#endif
741136849Sscottl	KdPrint(("RR182x [%d]: channel %d config EDMA, Non Queued Mode\n",
742136849Sscottl		pMvSataAdapter->adapterId, channelNum));
743136849Sscottl	if (mvSataConfigEdmaMode(pMvSataAdapter, channelNum,
744136849Sscottl	    MV_EDMA_MODE_NOT_QUEUED, 0) == MV_FALSE) {
745136849Sscottl		MV_ERROR("RR182x [%d] channel %d Error: mvSataConfigEdmaMode "
746136849Sscottl			 "failed\n", pMvSataAdapter->adapterId, channelNum);
747136849Sscottl		return -1;
748136849Sscottl	}
749136849Sscottl
750136849Sscottl	/* Enable EDMA */
751136849Sscottl	if (mvSataEnableChannelDma(pMvSataAdapter, channelNum) == MV_FALSE) {
752136849Sscottl		MV_ERROR("RR182x [%d] Failed to enable DMA, channel=%d\n",
753136849Sscottl			 pMvSataAdapter->adapterId, channelNum);
754136849Sscottl		return -1;
755136849Sscottl	}
756136849Sscottl	MV_ERROR("RR182x [%d,%d]: channel started successfully\n",
757136849Sscottl		 pMvSataAdapter->adapterId, channelNum);
758136849Sscottl
759136849Sscottl#ifndef FOR_DEMO
760136849Sscottl	set_fail_led(pMvSataAdapter, channelNum, 0);
761136849Sscottl#endif
762136849Sscottl	return 0;
763136849Sscottl}
764136849Sscottl
765136849Sscottlstatic void
766136849Sscottlhptmv_handle_event(void * data, int flag)
767136849Sscottl{
768136849Sscottl	IAL_ADAPTER_T *pAdapter;
769136849Sscottl	MV_SATA_ADAPTER *pMvSataAdapter;
770136849Sscottl	MV_U8 channelIndex;
771136849Sscottl
772136849Sscottl	pAdapter = (IAL_ADAPTER_T *)data;
773136849Sscottl	pMvSataAdapter = &pAdapter->mvSataAdapter;
774136849Sscottl
775136849Sscottl	mvOsSemTake(&pMvSataAdapter->semaphore);
776136849Sscottl	for (channelIndex = 0; channelIndex < MV_SATA_CHANNELS_NUM;
777136849Sscottl	     channelIndex++) {
778136849Sscottl		switch(pAdapter->sataEvents[channelIndex]) {
779136849Sscottl		case SATA_EVENT_CHANNEL_CONNECTED:
780136849Sscottl			/* Handle only connects */
781136849Sscottl			if (flag == 1)
782136849Sscottl				break;
783136849Sscottl			KdPrint(("RR182x [%d,%d]: new device connected\n",
784136849Sscottl				pMvSataAdapter->adapterId, channelIndex));
785136849Sscottl			hptmv_init_channel(pAdapter, channelIndex);
786136849Sscottl			if (mvSataConfigureChannel( pMvSataAdapter,
787136849Sscottl			    channelIndex) == MV_FALSE) {
788136849Sscottl				MV_ERROR("RR182x [%d,%d] Failed to configure\n",
789136849Sscottl					 pMvSataAdapter->adapterId,
790136849Sscottl					 channelIndex);
791136849Sscottl				hptmv_free_channel(pAdapter, channelIndex);
792136849Sscottl			} else {
793136849Sscottl#if 0
794136849Sscottl				mvSataChannelHardReset(pMvSataAdapter, channel);
795136849Sscottl#endif
796136849Sscottl				if (start_channel( pAdapter, channelIndex)) {
797136849Sscottl					MV_ERROR("RR182x [%d,%d]Failed to start"
798136849Sscottl						 " channel\n",
799136849Sscottl						 pMvSataAdapter->adapterId,
800136849Sscottl						 channelIndex);
801136849Sscottl					hptmv_free_channel(pAdapter,
802136849Sscottl					    channelIndex);
803136849Sscottl				} else {
804136849Sscottl					device_change(pAdapter, channelIndex,
805136849Sscottl					    TRUE);
806136849Sscottl				}
807136849Sscottl			}
808136849Sscottl			pAdapter->sataEvents[channelIndex] =
809136849Sscottl			    SATA_EVENT_NO_CHANGE;
810136849Sscottl			break;
811136849Sscottl
812136849Sscottl		case SATA_EVENT_CHANNEL_DISCONNECTED:
813136849Sscottl			/* Handle only disconnects */
814136849Sscottl			if (flag == 0)
815136849Sscottl				break;
816136849Sscottl			KdPrint(("RR182x [%d,%d]: device disconnected\n",
817136849Sscottl				 pMvSataAdapter->adapterId, channelIndex));
818136849Sscottl				/* Flush pending commands */
819136849Sscottl			if(pMvSataAdapter->sataChannel[channelIndex]) {
820136849Sscottl				_VBUS_INST(&pAdapter->VBus)
821136849Sscottl				mvSataFlushDmaQueue (pMvSataAdapter,
822136849Sscottl				    channelIndex, MV_FLUSH_TYPE_CALLBACK);
823136849Sscottl				CheckPendingCall(_VBUS_P0);
824136849Sscottl				mvSataRemoveChannel(pMvSataAdapter,
825136849Sscottl				    channelIndex);
826136849Sscottl				hptmv_free_channel(pAdapter, channelIndex);
827136849Sscottl				pMvSataAdapter->sataChannel[channelIndex] =
828136849Sscottl				    NULL;
829136849Sscottl				KdPrint(("RR182x [%d,%d]: channel removed\n",
830136849Sscottl					pMvSataAdapter->adapterId,
831136849Sscottl					channelIndex));
832136849Sscottl				if (pAdapter->outstandingCommands==0 &&
833136849Sscottl				    DPC_Request_Nums==0)
834136849Sscottl					Check_Idle_Call(pAdapter);
835136849Sscottl				} else {
836136849Sscottl				KdPrint(("RR182x [%d,%d]: channel already "
837136849Sscottl					"removed!!\n",
838136849Sscottl					pMvSataAdapter->adapterId,
839136849Sscottl					channelIndex));
840136849Sscottl			}
841136849Sscottl			pAdapter->sataEvents[channelIndex] =
842136849Sscottl			    SATA_EVENT_NO_CHANGE;
843136849Sscottl			break;
844136849Sscottl
845136849Sscottl		case SATA_EVENT_NO_CHANGE:
846136849Sscottl			break;
847136849Sscottl
848136849Sscottl		default:
849136849Sscottl			break;
850136849Sscottl		}
851136849Sscottl	}
852136849Sscottl	mvOsSemRelease(&pMvSataAdapter->semaphore);
853136849Sscottl}
854136849Sscottl
855136849Sscottl#define EVENT_CONNECT					1
856136849Sscottl#define EVENT_DISCONNECT				0
857136849Sscottl
858136849Sscottlstatic void
859136849Sscottlhptmv_handle_event_connect(void *data)
860136849Sscottl{
861136849Sscottl	hptmv_handle_event (data, 0);
862136849Sscottl}
863136849Sscottl
864136849Sscottlstatic void
865136849Sscottlhptmv_handle_event_disconnect(void *data)
866136849Sscottl{
867136849Sscottl	hptmv_handle_event (data, 1);
868136849Sscottl}
869136849Sscottl
870136849Sscottlstatic MV_BOOLEAN
871136849Sscottlhptmv_event_notify(MV_SATA_ADAPTER *pMvSataAdapter, MV_EVENT_TYPE eventType,
872136849Sscottl		   MV_U32 param1, MV_U32 param2)
873136849Sscottl{
874136849Sscottl	IAL_ADAPTER_T *pAdapter = pMvSataAdapter->IALData;
875136849Sscottl
876136849Sscottl	switch (eventType) {
877136849Sscottl	case MV_EVENT_TYPE_SATA_CABLE:
878136849Sscottl	{
879136849Sscottl		MV_U8   channel = param2;
880136849Sscottl
881136849Sscottl		if (param1 == EVENT_CONNECT) {
882136849Sscottl			pAdapter->sataEvents[channel] =
883136849Sscottl			    SATA_EVENT_CHANNEL_CONNECTED;
884136849Sscottl			KdPrint(("RR182x [%d,%d]: device connected event "
885136849Sscottl				"received\n", pMvSataAdapter->adapterId,
886136849Sscottl				channel));
887136849Sscottl			/*
888136849Sscottl			 * Delete previous timers (if multiple drives connected
889136849Sscottl			 * in the same time
890136849Sscottl			 */
891136849Sscottl			pAdapter->event_timer_connect =
892136849Sscottl			    timeout(hptmv_handle_event_connect, pAdapter,10*hz);
893136849Sscottl		} else if (param1 == EVENT_DISCONNECT) {
894136849Sscottl			pAdapter->sataEvents[channel] =
895136849Sscottl			    SATA_EVENT_CHANNEL_DISCONNECTED;
896136849Sscottl			KdPrint(("RR182x [%d,%d]: device disconnected event "
897136849Sscottl				"received \n", pMvSataAdapter->adapterId,
898136849Sscottl				channel));
899136849Sscottl			device_change(pAdapter, channel, FALSE);
900136849Sscottl			/*
901136849Sscottl			 * Delete previous timers (if multiple drives
902136849Sscottl			 * disconnected in the same time
903136849Sscottl			 */
904136849Sscottl			pAdapter->event_timer_disconnect =
905136849Sscottl			    timeout(hptmv_handle_event_disconnect, pAdapter,
906136849Sscottl			    10*hz);
907136849Sscottl		} else {
908136849Sscottl			MV_ERROR("RR182x: illigal value for param1(%d) at "
909136849Sscottl				 "connect/disconect event, host=%d\n", param1,
910136849Sscottl				 pMvSataAdapter->adapterId );
911136849Sscottl
912136849Sscottl		}
913136849Sscottl		break;
914136849Sscottl	}
915136849Sscottl	case MV_EVENT_TYPE_ADAPTER_ERROR:
916136849Sscottl		KdPrint(("RR182x: DEVICE error event received, pci cause "
917136849Sscottl			  "reg=%x,  don't how to handle this\n", param1));
918136849Sscottl		return MV_TRUE;
919136849Sscottl	default:
920136849Sscottl		MV_ERROR("RR182x[%d]: unknown event type (%d)\n",
921136849Sscottl			 pMvSataAdapter->adapterId, eventType);
922136849Sscottl		return MV_FALSE;
923136849Sscottl	}
924136849Sscottl	return MV_TRUE;
925136849Sscottl}
926136849Sscottl
927136849Sscottlstatic void
928136849Sscottlhptmv_map_req(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
929136849Sscottl{
930136849Sscottl	dma_addr_t *addr;
931136849Sscottl
932136849Sscottl	addr = (dma_addr_t *)arg;
933136849Sscottl
934136849Sscottl	if (error || nsegs != 1)
935136849Sscottl		return;
936136849Sscottl
937136849Sscottl	*addr = segs[0].ds_addr;
938136849Sscottl
939136849Sscottl	return;
940136849Sscottl}
941136849Sscottl
942136849Sscottlstatic int
943136849Sscottlhptmv_allocate_edma_queues(IAL_ADAPTER_T *pAdapter)
944136849Sscottl{
945136849Sscottl	if (bus_dmamem_alloc(pAdapter->req_dmat,
946136849Sscottl	    (void **)&pAdapter->requestsArrayBaseAddr, BUS_DMA_WAITOK,
947136849Sscottl	    &pAdapter->req_map) != 0) {
948136849Sscottl		MV_ERROR("RR182x[%d]: Failed to allocate memory for EDMA "
949136849Sscottl		    "request queues\n", pAdapter->mvSataAdapter.adapterId);
950136849Sscottl		return -1;
951136849Sscottl	}
952136849Sscottl
953136849Sscottl	(void)bus_dmamap_load(pAdapter->req_dmat, pAdapter->req_map,
954136849Sscottl	    pAdapter->requestsArrayBaseAddr, REQUESTS_ARRAY_SIZE, hptmv_map_req,
955136849Sscottl	    &pAdapter->requestsArrayBaseDmaAddr, 0);
956136849Sscottl
957136849Sscottl	pAdapter->requestsArrayBaseAlignedAddr =
958136849Sscottl	    pAdapter->requestsArrayBaseAddr;
959136849Sscottl	pAdapter->requestsArrayBaseAlignedAddr += MV_EDMA_REQUEST_QUEUE_SIZE;
960136849Sscottl	pAdapter->requestsArrayBaseAlignedAddr =
961136849Sscottl	    (MV_U8 *)(((ULONG_PTR)pAdapter->requestsArrayBaseAlignedAddr) &
962136849Sscottl	    ~(ULONG_PTR)(MV_EDMA_REQUEST_QUEUE_SIZE - 1));
963136849Sscottl	pAdapter->requestsArrayBaseDmaAlignedAddr =
964136849Sscottl	    pAdapter->requestsArrayBaseDmaAddr;
965136849Sscottl	pAdapter->requestsArrayBaseDmaAlignedAddr += MV_EDMA_REQUEST_QUEUE_SIZE;
966136849Sscottl	pAdapter->requestsArrayBaseDmaAlignedAddr &=
967136849Sscottl	    ~(ULONG_PTR)(MV_EDMA_REQUEST_QUEUE_SIZE - 1);
968136849Sscottl
969136849Sscottl	if ((pAdapter->requestsArrayBaseDmaAlignedAddr -
970136849Sscottl	    pAdapter->requestsArrayBaseDmaAddr) !=
971136849Sscottl	    (pAdapter->requestsArrayBaseAlignedAddr -
972136849Sscottl	    pAdapter->requestsArrayBaseAddr)) {
973136849Sscottl		MV_ERROR("RR182x[%d]: Error in Request Quueues Alignment\n",
974136849Sscottl			 pAdapter->mvSataAdapter.adapterId);
975136849Sscottl		bus_dmamap_unload(pAdapter->req_dmat, pAdapter->req_map);
976136849Sscottl		bus_dmamem_free(pAdapter->req_dmat,
977136849Sscottl		    pAdapter->requestsArrayBaseAddr, pAdapter->req_map);
978136849Sscottl		return -1;
979136849Sscottl	}
980136849Sscottl	/* response queues */
981136849Sscottl	if (bus_dmamem_alloc(pAdapter->resp_dmat,
982136849Sscottl	    (void **)&pAdapter->responsesArrayBaseAddr, BUS_DMA_WAITOK,
983136849Sscottl	    &pAdapter->resp_map) != 0) {
984136849Sscottl		MV_ERROR("RR182x[%d]: Failed to allocate memory for EDMA "
985136849Sscottl		    "response queues\n", pAdapter->mvSataAdapter.adapterId);
986136849Sscottl		bus_dmamap_unload(pAdapter->req_dmat, pAdapter->req_map);
987136849Sscottl		bus_dmamem_free(pAdapter->req_dmat,
988136849Sscottl		    pAdapter->requestsArrayBaseAddr, pAdapter->req_map);
989136849Sscottl		return -1;
990136849Sscottl	}
991136849Sscottl
992136849Sscottl	(void)bus_dmamap_load(pAdapter->resp_dmat, pAdapter->resp_map,
993136849Sscottl	    pAdapter->responsesArrayBaseAddr, RESPONSES_ARRAY_SIZE,
994136849Sscottl	    hptmv_map_req, &pAdapter->responsesArrayBaseDmaAddr, 0);
995136849Sscottl
996136849Sscottl	pAdapter->responsesArrayBaseAlignedAddr =
997136849Sscottl	    pAdapter->responsesArrayBaseAddr;
998136849Sscottl	pAdapter->responsesArrayBaseAlignedAddr += MV_EDMA_RESPONSE_QUEUE_SIZE;
999136849Sscottl	pAdapter->responsesArrayBaseAlignedAddr =
1000136849Sscottl	    (MV_U8 *)(((ULONG_PTR)pAdapter->responsesArrayBaseAlignedAddr) &
1001136849Sscottl	    ~(ULONG_PTR)(MV_EDMA_RESPONSE_QUEUE_SIZE - 1));
1002136849Sscottl	pAdapter->responsesArrayBaseDmaAlignedAddr =
1003136849Sscottl	    pAdapter->responsesArrayBaseDmaAddr;
1004136849Sscottl	pAdapter->responsesArrayBaseDmaAlignedAddr +=
1005136849Sscottl	    MV_EDMA_RESPONSE_QUEUE_SIZE;
1006136849Sscottl	pAdapter->responsesArrayBaseDmaAlignedAddr &=
1007136849Sscottl	    ~(ULONG_PTR)(MV_EDMA_RESPONSE_QUEUE_SIZE - 1);
1008136849Sscottl
1009136849Sscottl	if ((pAdapter->responsesArrayBaseDmaAlignedAddr -
1010136849Sscottl	    pAdapter->responsesArrayBaseDmaAddr) !=
1011136849Sscottl	    (pAdapter->responsesArrayBaseAlignedAddr -
1012136849Sscottl	    pAdapter->responsesArrayBaseAddr)) {
1013136849Sscottl		MV_ERROR("RR182x[%d]: Error in Response Quueues Alignment\n",
1014136849Sscottl			 pAdapter->mvSataAdapter.adapterId);
1015136849Sscottl		hptmv_free_edma_queues(pAdapter);
1016136849Sscottl		return -1;
1017136849Sscottl	}
1018136849Sscottl	return 0;
1019136849Sscottl}
1020136849Sscottl
1021136849Sscottlstatic void
1022136849Sscottlhptmv_free_edma_queues(IAL_ADAPTER_T *pAdapter)
1023136849Sscottl{
1024136849Sscottl	bus_dmamap_unload(pAdapter->req_dmat, pAdapter->req_map);
1025136849Sscottl	bus_dmamem_free(pAdapter->req_dmat, pAdapter->requestsArrayBaseAddr,
1026136849Sscottl	    pAdapter->req_map);
1027136849Sscottl	bus_dmamap_unload(pAdapter->resp_dmat, pAdapter->resp_map);
1028136849Sscottl	bus_dmamem_free(pAdapter->resp_dmat, pAdapter->responsesArrayBaseAddr,
1029136849Sscottl	    pAdapter->resp_map);
1030136849Sscottl}
1031136849Sscottl
1032136849Sscottlstatic PVOID
1033136849SscottlAllocatePRDTable(IAL_ADAPTER_T *pAdapter)
1034136849Sscottl{
1035136849Sscottl	PVOID ret;
1036136849Sscottl	if (pAdapter->pFreePRDLink) {
1037136849Sscottl		KdPrint(("pAdapter->pFreePRDLink:%lx\n",
1038136849Sscottl		    pAdapter->pFreePRDLink));
1039136849Sscottl		ret = pAdapter->pFreePRDLink;
1040136849Sscottl		pAdapter->pFreePRDLink = *(void**)ret;
1041136849Sscottl		return ret;
1042136849Sscottl	}
1043136849Sscottl	return NULL;
1044136849Sscottl}
1045136849Sscottl
1046136849Sscottlstatic void
1047136849SscottlFreePRDTable(IAL_ADAPTER_T *pAdapter, PVOID PRDTable)
1048136849Sscottl{
1049136849Sscottl	*(void**)PRDTable = pAdapter->pFreePRDLink;
1050136849Sscottl	pAdapter->pFreePRDLink = PRDTable;
1051136849Sscottl}
1052136849Sscottl
1053136849Sscottlextern PVDevice fGetFirstChild(PVDevice pLogical);
1054136849Sscottlextern void fResetBootMark(PVDevice pLogical);
1055136849Sscottlstatic void
1056136849SscottlfRegisterVdevice(IAL_ADAPTER_T *pAdapter)
1057136849Sscottl{
1058136849Sscottl	PVDevice pPhysical, pLogical;
1059136849Sscottl	PVBus  pVBus;
1060136849Sscottl	int i,j;
1061136849Sscottl
1062136849Sscottl	for(i = 0; i < MV_SATA_CHANNELS_NUM; i++) {
1063136849Sscottl		pPhysical = &(pAdapter->VDevices[i]);
1064136849Sscottl		pLogical = pPhysical;
1065136849Sscottl		while (pLogical->pParent) pLogical = pLogical->pParent;
1066136849Sscottl		if (pLogical->vf_online==0) {
1067136849Sscottl			pPhysical->vf_bootmark = pLogical->vf_bootmark = 0;
1068136849Sscottl			continue;
1069136849Sscottl		}
1070136849Sscottl		if (pLogical->VDeviceType == VD_SPARE ||
1071136849Sscottl		    pPhysical != fGetFirstChild(pLogical))
1072136849Sscottl			continue;
1073136849Sscottl
1074136849Sscottl		pVBus = &pAdapter->VBus;
1075136849Sscottl		if(pVBus) {
1076136849Sscottl			j=0;
1077136849Sscottl			while(j < MAX_VDEVICE_PER_VBUS && pVBus->pVDevice[j])
1078136849Sscottl				j++;
1079136849Sscottl			if (j < MAX_VDEVICE_PER_VBUS) {
1080136849Sscottl				pVBus->pVDevice[j] = pLogical;
1081136849Sscottl				pLogical->pVBus = pVBus;
1082136849Sscottl
1083136849Sscottl				if (j>0 && pLogical->vf_bootmark) {
1084136849Sscottl					if (pVBus->pVDevice[0]->vf_bootmark) {
1085136849Sscottl						fResetBootMark(pLogical);
1086136849Sscottl					} else {
1087136849Sscottl						do {
1088136849Sscottl							pVBus->pVDevice[j] =
1089136849Sscottl							   pVBus->pVDevice[j-1];
1090136849Sscottl						} while (--j);
1091136849Sscottl						pVBus->pVDevice[0] = pLogical;
1092136849Sscottl					}
1093136849Sscottl				}
1094136849Sscottl			}
1095136849Sscottl		}
1096136849Sscottl	}
1097136849Sscottl}
1098136849Sscottl
1099136849SscottlPVDevice
1100136849SscottlGetSpareDisk(_VBUS_ARG PVDevice pArray)
1101136849Sscottl{
1102136849Sscottl	IAL_ADAPTER_T *pAdapter;
1103136849Sscottl	ULONG capacity;
1104136849Sscottl	ULONG thiscap, maxcap = MAX_LBA_T;
1105136849Sscottl	PVDevice pVDevice, pFind = NULL;
1106136849Sscottl	int i;
1107136849Sscottl
1108136849Sscottl	pAdapter = (IAL_ADAPTER_T *)pArray->pVBus->OsExt;
1109136849Sscottl	capacity =
1110136849Sscottl	    LongDiv(pArray->VDeviceCapacity, pArray->u.array.bArnMember-1);
1111136849Sscottl	for (i = 0;i < MV_SATA_CHANNELS_NUM; i++) {
1112136849Sscottl		pVDevice = &pAdapter->VDevices[i];
1113136849Sscottl		if(!pVDevice)
1114136849Sscottl			continue;
1115136849Sscottl		thiscap = pArray->vf_format_v2 ?
1116136849Sscottl		   pVDevice->u.disk.dDeRealCapacity : pVDevice->VDeviceCapacity;
1117136849Sscottl		/* find the smallest usable spare disk */
1118136849Sscottl		if (pVDevice->VDeviceType==VD_SPARE &&
1119136849Sscottl		    pVDevice->u.disk.df_on_line && thiscap < maxcap &&
1120136849Sscottl		    thiscap >= capacity) {
1121136849Sscottl			maxcap = pVDevice->VDeviceCapacity;
1122136849Sscottl			pFind = pVDevice;
1123136849Sscottl		}
1124136849Sscottl	}
1125136849Sscottl	return pFind;
1126136849Sscottl}
1127136849Sscottl
1128136849Sscottl/******************************************************************
1129136849Sscottl * IO ATA Command
1130136849Sscottl *******************************************************************/
1131136849Sscottlint HPTLIBAPI
1132136849SscottlfDeReadWrite(PDevice pDev, ULONG Lba, UCHAR Cmd, void *tmpBuffer)
1133136849Sscottl{
1134136849Sscottl	return mvReadWrite(pDev->mv, Lba, Cmd, tmpBuffer);
1135136849Sscottl}
1136136849Sscottl
1137136849Sscottlvoid HPTLIBAPI fDeSelectMode(PDevice pDev, UCHAR NewMode)
1138136849Sscottl{
1139136849Sscottl#ifndef SIMULATE
1140136849Sscottl	MV_SATA_CHANNEL *pSataChannel;
1141136849Sscottl	MV_SATA_ADAPTER *pSataAdapter;
1142136849Sscottl	MV_U8 channelIndex;
1143136849Sscottl	UCHAR mvMode;
1144136849Sscottl
1145136849Sscottl	pSataChannel = pDev->mv;
1146136849Sscottl	pSataAdapter = pSataChannel->mvSataAdapter;
1147136849Sscottl	channelIndex = pSataChannel->channelNumber;
1148136849Sscottl
1149136849Sscottl	/* 508x don't use MW-DMA? */
1150136849Sscottl	if (NewMode>4 && NewMode<8) NewMode = 4;
1151136849Sscottl	pDev->bDeModeSetting = NewMode;
1152136849Sscottl	if (NewMode<=4)
1153136849Sscottl		mvMode = MV_ATA_TRANSFER_PIO_0 + NewMode;
1154136849Sscottl	else
1155136849Sscottl		mvMode = MV_ATA_TRANSFER_UDMA_0 + (NewMode-8);
1156136849Sscottl
1157136849Sscottl	/*To fix 88i8030 bug*/
1158136849Sscottl	if (mvMode > MV_ATA_TRANSFER_UDMA_0 && mvMode < MV_ATA_TRANSFER_UDMA_4)
1159136849Sscottl		mvMode = MV_ATA_TRANSFER_UDMA_0;
1160136849Sscottl
1161136849Sscottl	mvSataDisableChannelDma(pSataAdapter, channelIndex);
1162136849Sscottl	/* Flush pending commands */
1163136849Sscottl	mvSataFlushDmaQueue (pSataAdapter, channelIndex, MV_FLUSH_TYPE_NONE);
1164136849Sscottl
1165136849Sscottl	if (mvStorageDevATASetFeatures(pSataAdapter, channelIndex,
1166136849Sscottl	    MV_ATA_SET_FEATURES_TRANSFER, mvMode, 0, 0, 0) == MV_FALSE) {
1167136849Sscottl		KdPrint(("channel %d: Set Features failed\n", channelIndex));
1168136849Sscottl	}
1169136849Sscottl	/* Enable EDMA */
1170136849Sscottl	if (mvSataEnableChannelDma(pSataAdapter, channelIndex) == MV_FALSE)
1171136849Sscottl		KdPrint(("Failed to enable DMA, channel=%d", channelIndex));
1172136849Sscottl#endif
1173136849Sscottl}
1174136849Sscottl
1175136849Sscottl#ifdef SUPPORT_ARRAY
1176136849Sscottl#define IdeRegisterVDevice  fCheckArray
1177136849Sscottl#else
1178136849Sscottlvoid
1179136849SscottlIdeRegisterVDevice(PDevice pDev)
1180136849Sscottl{
1181136849Sscottl	PVDevice pVDev = Map2pVDevice(pDev);
1182136849Sscottl
1183136849Sscottl	pVDev->VDeviceType = pDev->df_atapi? VD_ATAPI :
1184136849Sscottl	    pDev->df_removable_drive ? VD_REMOVABLE : VD_SINGLE_DISK;
1185136849Sscottl	pVDev->vf_online = 1;
1186136849Sscottl	pVDev->VDeviceCapacity = pDev->dDeRealCapacity;
1187136849Sscottl	pVDev->pfnSendCommand = pfnSendCommand[pVDev->VDeviceType];
1188136849Sscottl	pVDev->pfnDeviceFailed = pfnDeviceFailed[pVDev->VDeviceType];
1189136849Sscottl}
1190136849Sscottl#endif
1191136849Sscottl
1192136849Sscottlstatic int num_adapters = 0;
1193136849Sscottlstatic int
1194136849Sscottlinit_adapter(IAL_ADAPTER_T *pAdapter)
1195136849Sscottl{
1196136849Sscottl	PCommand pCmd;
1197136849Sscottl	pPrivCommand prvCmd;
1198136849Sscottl	PVBus _vbus_p = &pAdapter->VBus;
1199136849Sscottl	MV_SATA_ADAPTER *pMvSataAdapter;
1200136849Sscottl	PUCHAR PRDTable;
1201136849Sscottl	int i, channel, rid, error;
1202136849Sscottl
1203136849Sscottl	PVDevice pVDev;
1204136849Sscottl
1205136849Sscottl	intrmask_t oldspl = lock_driver();
1206136849Sscottl
1207136849Sscottl	pAdapter->next = 0;
1208136849Sscottl
1209136849Sscottl	if(gIal_Adapter == 0) {
1210136849Sscottl		gIal_Adapter = pAdapter;
1211136849Sscottl		pCurAdapter = gIal_Adapter;
1212136849Sscottl	} else {
1213136849Sscottl		pCurAdapter->next = pAdapter;
1214136849Sscottl		pCurAdapter = pAdapter;
1215136849Sscottl	}
1216136849Sscottl
1217136849Sscottl	pAdapter->outstandingCommands = 0;
1218136849Sscottl
1219136849Sscottl	pMvSataAdapter = &(pAdapter->mvSataAdapter);
1220136849Sscottl	_vbus_p->OsExt = (void *)pAdapter;
1221136849Sscottl	pMvSataAdapter->IALData = pAdapter;
1222136849Sscottl
1223136849Sscottl	if (bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
1224136849Sscottl	    BUS_SPACE_MAXADDR, NULL, NULL, BUS_SPACE_MAXSIZE_32BIT,
1225136849Sscottl	    MV_MAX_SEGMENTS, BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL,
1226136849Sscottl	    &pAdapter->parent_dmat) != 0) {
1227136849Sscottl		MV_ERROR("RR182x: Failed to create busdma resources\n");
1228136849Sscottl		unlock_driver(oldspl);
1229136849Sscottl		return (ENOMEM);
1230136849Sscottl	}
1231136849Sscottl
1232136849Sscottl	if (bus_dma_tag_create(pAdapter->parent_dmat, PAGE_SIZE, 0,
1233136849Sscottl	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
1234136849Sscottl	    REQUESTS_ARRAY_SIZE, 1, REQUESTS_ARRAY_SIZE, 0, NULL, NULL,
1235136849Sscottl	    &pAdapter->req_dmat) != 0) {
1236136849Sscottl		MV_ERROR("RR182x: Failed to create busdma resources\n");
1237136849Sscottl		error = ENOMEM;
1238136849Sscottl		goto unregister;
1239136849Sscottl	}
1240136849Sscottl
1241136849Sscottl	if (bus_dma_tag_create(pAdapter->parent_dmat, PAGE_SIZE, 0,
1242136849Sscottl	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
1243136849Sscottl	    RESPONSES_ARRAY_SIZE, 1, RESPONSES_ARRAY_SIZE, 0, NULL, NULL,
1244136849Sscottl	    &pAdapter->resp_dmat) != 0) {
1245136849Sscottl		MV_ERROR("RR182x: Failed to create busdma resources\n");
1246136849Sscottl		error = ENOMEM;
1247136849Sscottl		goto unregister;
1248136849Sscottl	}
1249136849Sscottl
1250136849Sscottl	if (bus_dma_tag_create(pAdapter->parent_dmat, 1, 0,
1251136849Sscottl	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
1252136849Sscottl	    MAXBSIZE, MV_MAX_SEGMENTS, MAXBSIZE, 0, busdma_lock_mutex, &Giant,
1253136849Sscottl	    &pAdapter->buf_dmat) != 0) {
1254136849Sscottl		MV_ERROR("RR182x: Failed to create busdma resources\n");
1255136849Sscottl		error = ENOMEM;
1256136849Sscottl		goto unregister;
1257136849Sscottl	}
1258136849Sscottl
1259136849Sscottl	if (hptmv_allocate_edma_queues(pAdapter)) {
1260136849Sscottl		MV_ERROR("RR182x: Failed to allocate memory for EDMA queues\n");
1261136849Sscottl		error = ENOMEM;
1262136849Sscottl		goto unregister;
1263136849Sscottl	}
1264136849Sscottl
1265136849Sscottl	/* also map EPROM address */
1266136849Sscottl	rid = 0x10;
1267136849Sscottl	if ((pAdapter->mem_res = bus_alloc_resource(pAdapter->hpt_dev,
1268136849Sscottl	    SYS_RES_MEMORY, &rid, 0, ~0, MV_SATA_PCI_BAR0_SPACE_SIZE+0x40000,
1269136849Sscottl	    RF_ACTIVE)) == 0) {
1270136849Sscottl		MV_ERROR("RR182x: Failed to remap memory space\n");
1271136849Sscottl		error = ENXIO;
1272136849Sscottl		goto unregister;
1273136849Sscottl	}
1274136849Sscottl
1275136849Sscottl	/*
1276136849Sscottl	 * This field is opaque.  Abuse it so that the bus_space functions
1277136849Sscottl	 * can get the info that they need when called.
1278136849Sscottl	 */
1279136849Sscottl	pMvSataAdapter->adapterIoBaseAddress = pAdapter;
1280136849Sscottl	pAdapter->mem_bsh = rman_get_bushandle(pAdapter->mem_res);
1281136849Sscottl	pAdapter->mem_btag = rman_get_bustag(pAdapter->mem_res);
1282136849Sscottl
1283136849Sscottl	pMvSataAdapter->adapterId = num_adapters++;
1284136849Sscottl	/* get the revision ID */
1285136849Sscottl	pMvSataAdapter->pciConfigRevisionId =
1286136849Sscottl	    pci_read_config(pAdapter->hpt_dev, PCIR_REVID, 1);
1287136849Sscottl	pMvSataAdapter->pciConfigDeviceId = pci_get_device(pAdapter->hpt_dev);
1288136849Sscottl
1289136849Sscottl	/* init RR182x */
1290136849Sscottl	pMvSataAdapter->intCoalThre[0]= 1;
1291136849Sscottl	pMvSataAdapter->intCoalThre[1]= 1;
1292136849Sscottl	pMvSataAdapter->intTimeThre[0] = 1;
1293136849Sscottl	pMvSataAdapter->intTimeThre[1] = 1;
1294136849Sscottl	pMvSataAdapter->pciCommand = 0x0107E371;
1295136849Sscottl	pMvSataAdapter->pciSerrMask = 0xd77fe6ul;
1296136849Sscottl	pMvSataAdapter->pciInterruptMask = 0xd77fe6ul;
1297136849Sscottl	pMvSataAdapter->mvSataEventNotify = hptmv_event_notify;
1298136849Sscottl
1299136849Sscottl	if (mvSataInitAdapter(pMvSataAdapter) == MV_FALSE) {
1300136849Sscottl		MV_ERROR("RR182x[%d]: core failed to initialize the adapter\n",
1301136849Sscottl			 pMvSataAdapter->adapterId);
1302136849Sscottl		error = ENXIO;
1303136849Sscottl		goto unregister;
1304136849Sscottl	}
1305136849Sscottl	pAdapter->ver_601 = pMvSataAdapter->pcbVersion;
1306136849Sscottl
1307136849Sscottl#ifndef FOR_DEMO
1308136849Sscottl	set_fail_leds(pMvSataAdapter, 0);
1309136849Sscottl#endif
1310136849Sscottl
1311136849Sscottl	/* setup command blocks */
1312136849Sscottl	KdPrint(("Allocate command blocks\n"));
1313136849Sscottl	_vbus_(pFreeCommands) = 0;
1314136849Sscottl	pAdapter->pCommandBlocks = malloc(sizeof(struct _Command) *
1315136849Sscottl	    MAX_COMMAND_BLOCKS_FOR_EACH_VBUS, M_DEVBUF, M_ZERO | M_WAITOK);
1316136849Sscottl	KdPrint(("pCommandBlocks:%x\n", pAdapter->pCommandBlocks));
1317136849Sscottl
1318136849Sscottl	/*
1319136849Sscottl	 * Gotta cheat here.  The _Command struct only gives us a single
1320136849Sscottl	 * pointer for private data, but we need to store more than that.
1321136849Sscottl	 * Of course the pCommand retains no type stability, and FreeCommand
1322136849Sscottl	 * is hidden in the binary object, so gotta track these on our own
1323136849Sscottl	 * list.
1324136849Sscottl	 */
1325136849Sscottl	pAdapter->pPrivateBlocks = malloc(sizeof(struct _privCommand) *
1326136849Sscottl	    MAX_COMMAND_BLOCKS_FOR_EACH_VBUS, M_DEVBUF, M_ZERO | M_WAITOK);
1327136849Sscottl	TAILQ_INIT(&pAdapter->PrivCmdTQH);
1328136849Sscottl
1329136849Sscottl	for (i = 0; i < MAX_COMMAND_BLOCKS_FOR_EACH_VBUS; i++) {
1330136849Sscottl		pCmd = &pAdapter->pCommandBlocks[i];
1331136849Sscottl		prvCmd = &pAdapter->pPrivateBlocks[i];
1332136849Sscottl		prvCmd->pAdapter = pAdapter;
1333136849Sscottl		if ((error = bus_dmamap_create(pAdapter->buf_dmat, 0,
1334136849Sscottl		    &prvCmd->buf_map)) == 0) {
1335136849Sscottl			FreeCommand(_VBUS_P (pCmd));
1336136849Sscottl			FreePrivCommand(pAdapter, prvCmd);
1337136849Sscottl		} else
1338136849Sscottl			break;
1339136849Sscottl	}
1340136849Sscottl
1341136849Sscottl	/* setup PRD Tables */
1342136849Sscottl	KdPrint(("Allocate PRD Tables\n"));
1343136849Sscottl	pAdapter->pFreePRDLink = 0;
1344136849Sscottl
1345136849Sscottl	if (bus_dma_tag_create(pAdapter->parent_dmat, PAGE_SIZE, 0,
1346136849Sscottl	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
1347136849Sscottl	    PRD_ENTRIES_SIZE * PRD_TABLES_FOR_VBUS, 1,
1348136849Sscottl	    PRD_ENTRIES_SIZE * PRD_TABLES_FOR_VBUS, 0, NULL, NULL,
1349136849Sscottl	    &pAdapter->prd_dmat) != 0) {
1350136849Sscottl		MV_ERROR("RR182x: Failed to create busdma resources\n");
1351136849Sscottl		error = ENOMEM;
1352136849Sscottl		goto unregister;
1353136849Sscottl	}
1354136849Sscottl
1355136849Sscottl	if (bus_dmamem_alloc(pAdapter->prd_dmat,
1356136849Sscottl	    (void **)&pAdapter->prdTableAddr, BUS_DMA_WAITOK,
1357136849Sscottl	    &pAdapter->prd_map) != 0)
1358136849Sscottl		goto unregister;
1359136849Sscottl
1360136849Sscottl	(void)bus_dmamap_load(pAdapter->prd_dmat, pAdapter->prd_map,
1361136849Sscottl	    pAdapter->prdTableAddr, PRD_ENTRIES_SIZE * PRD_TABLES_FOR_VBUS,
1362136849Sscottl	    hptmv_map_req, &pAdapter->prdTableDmaAddr, 0);
1363136849Sscottl
1364136849Sscottl	KdPrint(("prdTableAddr:%x\n",pAdapter->prdTableAddr));
1365136849Sscottl	if (!pAdapter->prdTableAddr) {
1366136849Sscottl		MV_ERROR("insufficient PRD Tables\n");
1367136849Sscottl		error = ENOMEM;
1368136849Sscottl		goto unregister;
1369136849Sscottl	}
1370136849Sscottl
1371136849Sscottl	PRDTable = pAdapter->prdTableAddr;
1372136849Sscottl	for (i = 0; i < PRD_TABLES_FOR_VBUS; i++) {
1373136849Sscottl		KdPrint(("i= %d, pAdapter->pFreePRDLink= %p\n", i,
1374136849Sscottl		    pAdapter->pFreePRDLink));
1375136849Sscottl		FreePRDTable(pAdapter, PRDTable);
1376136849Sscottl		PRDTable += PRD_ENTRIES_SIZE;
1377136849Sscottl	}
1378136849Sscottl
1379136849Sscottl	/* enable the adapter interrupts */
1380136849Sscottl
1381136849Sscottl	/* configure and start the connected channels*/
1382136849Sscottl	for (channel = 0; channel < MV_SATA_CHANNELS_NUM; channel++) {
1383136849Sscottl		pAdapter->mvChannel[channel].online = MV_FALSE;
1384136849Sscottl		if (mvSataIsStorageDeviceConnected(pMvSataAdapter, channel)
1385136849Sscottl		    != MV_TRUE)
1386136849Sscottl			continue;
1387136849Sscottl
1388136849Sscottl		KdPrint(("RR182x[%d]: channel %d is connected\n",
1389136849Sscottl			pMvSataAdapter->adapterId, channel));
1390136849Sscottl
1391136849Sscottl		if (hptmv_init_channel(pAdapter, channel) == 0) {
1392136849Sscottl			if (mvSataConfigureChannel(pMvSataAdapter, channel)
1393136849Sscottl			    == MV_FALSE) {
1394136849Sscottl				MV_ERROR("RR182x[%d]: Failed to configure "
1395136849Sscottl				    "channel %d\n", pMvSataAdapter->adapterId,
1396136849Sscottl				    channel);
1397136849Sscottl				hptmv_free_channel(pAdapter, channel);
1398136849Sscottl				continue;
1399136849Sscottl			}
1400136849Sscottl			if (start_channel(pAdapter, channel)) {
1401136849Sscottl				MV_ERROR("RR182x[%d]: Failed to start channel, "
1402136849Sscottl				   "channel=%d\n", pMvSataAdapter->adapterId,
1403136849Sscottl				    channel);
1404136849Sscottl				hptmv_free_channel(pAdapter, channel);
1405136849Sscottl			}
1406136849Sscottl			pAdapter->mvChannel[channel].online = MV_TRUE;
1407136849Sscottl#if 0
1408136849Sscottl			mvSataChannelSetEdmaLoopBackMode(
1409136849Sscottl			    pMvSataAdapter, channel, MV_TRUE);
1410136849Sscottl#endif
1411136849Sscottl		}
1412136849Sscottl		KdPrint(("pAdapter->mvChannel[channel].online:%x, channel:%d\n",
1413136849Sscottl			pAdapter->mvChannel[channel].online, channel));
1414136849Sscottl	}
1415136849Sscottl
1416136849Sscottl#ifdef SUPPORT_ARRAY
1417136849Sscottl	for(i = MAX_ARRAY_DEVICE - 1; i >= 0; i--) {
1418136849Sscottl		pVDev = ArrayTables(i);
1419136849Sscottl		mArFreeArrayTable(pVDev);
1420136849Sscottl	}
1421136849Sscottl#endif
1422136849Sscottl
1423136849Sscottl	KdPrint(("Initialize Devices\n"));
1424136849Sscottl	for (channel = 0; channel < MV_SATA_CHANNELS_NUM; channel++) {
1425136849Sscottl		MV_SATA_CHANNEL *pMvSataChannel;
1426136849Sscottl
1427136849Sscottl		pMvSataChannel = pMvSataAdapter->sataChannel[channel];
1428136849Sscottl		if (pMvSataChannel) {
1429136849Sscottl			init_vdev_params(pAdapter, channel);
1430136849Sscottl			IdeRegisterVDevice(&pAdapter->VDevices[channel].u.disk);
1431136849Sscottl		}
1432136849Sscottl	}
1433136849Sscottl#ifdef SUPPORT_ARRAY
1434136849Sscottl	CheckArrayCritical(_VBUS_P0);
1435136849Sscottl#endif
1436136849Sscottl	_vbus_p->nInstances = 1;
1437136849Sscottl	fRegisterVdevice(pAdapter);
1438136849Sscottl
1439136849Sscottl	for (channel=0;channel<MV_SATA_CHANNELS_NUM;channel++) {
1440136849Sscottl		pVDev = _vbus_p->pVDevice[channel];
1441136849Sscottl		if (pVDev && pVDev->vf_online)
1442136849Sscottl			fCheckBootable(pVDev);
1443136849Sscottl	}
1444136849Sscottl
1445136849Sscottl#if defined(SUPPORT_ARRAY) && defined(_RAID5N_)
1446136849Sscottl	init_raid5_memory(_VBUS_P0);
1447136849Sscottl	_vbus_(r5).enable_write_back = 1;
1448136849Sscottl	printf("RR182x: RAID5 write-back %s\n",
1449136849Sscottl	    _vbus_(r5).enable_write_back? "enabled" : "disabled");
1450136849Sscottl#endif
1451136849Sscottl
1452136849Sscottl	mvSataUnmaskAdapterInterrupt(pMvSataAdapter);
1453136849Sscottl	unlock_driver(oldspl);
1454136849Sscottl	return 0;
1455136849Sscottl
1456136849Sscottlunregister:
1457136849Sscottl	if (pAdapter->mem_res != 0)
1458136849Sscottl		bus_release_resource(pAdapter->hpt_dev, SYS_RES_MEMORY, rid,
1459136849Sscottl		    pAdapter->mem_res);
1460136849Sscottl
1461136849Sscottl	hptmv_free_edma_queues(pAdapter);
1462136849Sscottl
1463136849Sscottl	if (pAdapter->resp_dmat != NULL)
1464136849Sscottl		bus_dma_tag_destroy(pAdapter->resp_dmat);
1465136849Sscottl	if (pAdapter->req_dmat != NULL)
1466136849Sscottl		bus_dma_tag_destroy(pAdapter->req_dmat);
1467136849Sscottl	if (pAdapter->buf_dmat != NULL)
1468136849Sscottl		bus_dma_tag_destroy(pAdapter->buf_dmat);
1469136849Sscottl	if (pAdapter->parent_dmat != NULL)
1470136849Sscottl		bus_dma_tag_destroy(pAdapter->parent_dmat);
1471136849Sscottl
1472136849Sscottl	unlock_driver(oldspl);
1473136849Sscottl	return error;
1474136849Sscottl}
1475136849Sscottl
1476136849Sscottlint
1477136849SscottlMvSataResetChannel(MV_SATA_ADAPTER *pMvSataAdapter, MV_U8 channel)
1478136849Sscottl{
1479136849Sscottl	IAL_ADAPTER_T *pAdapter = (IAL_ADAPTER_T *)pMvSataAdapter->IALData;
1480136849Sscottl
1481136849Sscottl	mvSataDisableChannelDma(pMvSataAdapter, channel);
1482136849Sscottl
1483136849Sscottl	/* Flush pending commands */
1484136849Sscottl	mvSataFlushDmaQueue (pMvSataAdapter, channel, MV_FLUSH_TYPE_CALLBACK);
1485136849Sscottl
1486136849Sscottl	/* Software reset channel */
1487136849Sscottl	if (mvStorageDevATASoftResetDevice(pMvSataAdapter, channel) ==
1488136849Sscottl	    MV_FALSE) {
1489136849Sscottl		MV_ERROR("RR182x [%d,%d]: failed to perform Software reset\n",
1490136849Sscottl			 pMvSataAdapter->adapterId, channel);
1491136849Sscottl		return -1;
1492136849Sscottl	}
1493136849Sscottl
1494136849Sscottl	/* Hardware reset channel */
1495136849Sscottl	if (mvSataChannelHardReset(pMvSataAdapter, channel)== MV_FALSE) {
1496136849Sscottl		MV_ERROR("RR182x [%d,%d] Failed to Hard reser the SATA "
1497136849Sscottl			 "channel\n", pMvSataAdapter->adapterId, channel);
1498136849Sscottl		hptmv_free_channel(pAdapter, channel);
1499136849Sscottl		return -1;
1500136849Sscottl	}
1501136849Sscottl
1502136849Sscottl	if (mvSataIsStorageDeviceConnected(pMvSataAdapter, channel) ==
1503136849Sscottl	    MV_FALSE) {
1504136849Sscottl		MV_ERROR("RR182x [%d,%d] Failed to Connect Device\n",
1505136849Sscottl			 pMvSataAdapter->adapterId, channel);
1506136849Sscottl		hptmv_free_channel(pAdapter, channel);
1507136849Sscottl		return -1;
1508136849Sscottl	} else {
1509136849Sscottl		/* Set transfer mode */
1510136849Sscottl		if((mvStorageDevATASetFeatures(pMvSataAdapter, channel,
1511136849Sscottl		    MV_ATA_SET_FEATURES_TRANSFER, MV_ATA_TRANSFER_PIO_SLOW, 0,
1512136849Sscottl		    0, 0) == MV_FALSE) ||
1513136849Sscottl		    (mvStorageDevATASetFeatures(pMvSataAdapter, channel,
1514136849Sscottl		    MV_ATA_SET_FEATURES_TRANSFER,
1515136849Sscottl		    pAdapter->mvChannel[channel].maxPioModeSupported, 0, 0, 0)
1516136849Sscottl		    == MV_FALSE) || (mvStorageDevATASetFeatures(pMvSataAdapter,
1517136849Sscottl		    channel, MV_ATA_SET_FEATURES_TRANSFER,
1518136849Sscottl		    pAdapter->mvChannel[channel].maxUltraDmaModeSupported, 0,
1519136849Sscottl		    0, 0) == MV_FALSE)) {
1520136849Sscottl			MV_ERROR("channel %d: Set Features failed", channel);
1521136849Sscottl			hptmv_free_channel(pAdapter, channel);
1522136849Sscottl			return -1;
1523136849Sscottl		}
1524136849Sscottl		/* Enable EDMA */
1525136849Sscottl		if (mvSataEnableChannelDma(pMvSataAdapter, channel)==MV_FALSE) {
1526136849Sscottl			MV_ERROR("Failed to enable DMA, channel=%d", channel);
1527136849Sscottl			hptmv_free_channel(pAdapter, channel);
1528136849Sscottl			return -1;
1529136849Sscottl		}
1530136849Sscottl	}
1531136849Sscottl	return 0;
1532136849Sscottl}
1533136849Sscottl
1534136849Sscottlstatic int
1535136849SscottlfResetActiveCommands(PVBus _vbus_p)
1536136849Sscottl{
1537136849Sscottl	MV_SATA_ADAPTER *pMvSataAdapter;
1538136849Sscottl	MV_U8 channel;
1539136849Sscottl	int   rtn = 0;
1540136849Sscottl
1541136849Sscottl	pMvSataAdapter = &((IAL_ADAPTER_T *)_vbus_p->OsExt)->mvSataAdapter;
1542136849Sscottl	for (channel=0;channel< MV_SATA_CHANNELS_NUM;channel++) {
1543136849Sscottl		if (pMvSataAdapter->sataChannel[channel] &&
1544136849Sscottl		    pMvSataAdapter->sataChannel[channel]->outstandingCommands)
1545136849Sscottl			if (MvSataResetChannel(pMvSataAdapter,channel) == -1)
1546136849Sscottl				 rtn = -1;
1547136849Sscottl	}
1548136849Sscottl	HPT_ASSERT(rtn==0);
1549136849Sscottl	return 0;
1550136849Sscottl}
1551136849Sscottl
1552136849Sscottlvoid
1553136849SscottlfCompleteAllCommandsSynchronously(PVBus _vbus_p)
1554136849Sscottl{
1555136849Sscottl	UINT cont;
1556136849Sscottl	ULONG ticks = 0;
1557136849Sscottl	MV_U8 channel;
1558136849Sscottl	MV_SATA_ADAPTER *pMvSataAdapter;
1559136849Sscottl	MV_SATA_CHANNEL *pMvSataChannel;
1560136849Sscottl
1561136849Sscottl	pMvSataAdapter = &((IAL_ADAPTER_T *)_vbus_p->OsExt)->mvSataAdapter;
1562136849Sscottl	do {
1563136849Sscottlcheck_cmds:
1564136849Sscottl		cont = 0;
1565136849Sscottl		CheckPendingCall(_VBUS_P0);
1566136849Sscottl#ifdef _RAID5N_
1567136849Sscottl		dataxfer_poll();
1568136849Sscottl		xor_poll();
1569136849Sscottl#endif
1570136849Sscottl		for (channel = 0; channel < MV_SATA_CHANNELS_NUM; channel++) {
1571136849Sscottl			pMvSataChannel = pMvSataAdapter->sataChannel[channel];
1572136849Sscottl			if (pMvSataChannel &&
1573136849Sscottl			    pMvSataChannel->outstandingCommands) {
1574136849Sscottl				while (pMvSataChannel->outstandingCommands) {
1575136849Sscottl					if (!mvSataInterruptServiceRoutine(
1576136849Sscottl					    pMvSataAdapter)) {
1577136849Sscottl						StallExec(1000);
1578136849Sscottl						if (ticks++ > 3000) {
1579136849Sscottl							MvSataResetChannel(
1580136849Sscottl							    pMvSataAdapter,
1581136849Sscottl							    channel);
1582136849Sscottl							goto check_cmds;
1583136849Sscottl						}
1584136849Sscottl					} else
1585136849Sscottl						ticks = 0;
1586136849Sscottl				}
1587136849Sscottl				cont = 1;
1588136849Sscottl			}
1589136849Sscottl		}
1590136849Sscottl	} while (cont);
1591136849Sscottl}
1592136849Sscottl
1593136849Sscottlvoid
1594136849SscottlfResetVBus(_VBUS_ARG0)
1595136849Sscottl{
1596136849Sscottl	KdPrint(("fMvResetBus(%x)", _vbus_p));
1597136849Sscottl
1598136849Sscottl	/* some commands may already finished. */
1599136849Sscottl	CheckPendingCall(_VBUS_P0);
1600136849Sscottl
1601136849Sscottl	fResetActiveCommands(_vbus_p);
1602136849Sscottl	/*
1603136849Sscottl	 * the other pending commands may still be finished successfully.
1604136849Sscottl	 */
1605136849Sscottl	fCompleteAllCommandsSynchronously(_vbus_p);
1606136849Sscottl
1607136849Sscottl	/* Now there should be no pending commands. No more action needed. */
1608136849Sscottl	CheckIdleCall(_VBUS_P0);
1609136849Sscottl
1610136849Sscottl	KdPrint(("fMvResetBus() done"));
1611136849Sscottl}
1612136849Sscottl
1613136849Sscottlvoid
1614136849SscottlfRescanAllDevice(_VBUS_ARG0)
1615136849Sscottl{
1616136849Sscottl}
1617136849Sscottl
1618136849Sscottlstatic MV_BOOLEAN
1619136849SscottlCommandCompletionCB(MV_SATA_ADAPTER *pMvSataAdapter, MV_U8 channelNum,
1620136849Sscottl    MV_COMPLETION_TYPE comp_type, MV_VOID_PTR commandId, MV_U16 responseFlags,
1621136849Sscottl    MV_U32 timeStamp, MV_STORAGE_DEVICE_REGISTERS *registerStruct)
1622136849Sscottl{
1623136849Sscottl	PCommand pCmd = (PCommand) commandId;
1624136849Sscottl	_VBUS_INST(pCmd->pVDevice->pVBus)
1625136849Sscottl
1626136849Sscottl	if (pCmd->uScratch.sata_param.prdAddr)
1627136849Sscottl		FreePRDTable(pMvSataAdapter->IALData,
1628136849Sscottl		    pCmd->uScratch.sata_param.prdAddr);
1629136849Sscottl
1630136849Sscottl	switch (comp_type) {
1631136849Sscottl	case MV_COMPLETION_TYPE_NORMAL:
1632136849Sscottl		pCmd->Result = RETURN_SUCCESS;
1633136849Sscottl		break;
1634136849Sscottl	case MV_COMPLETION_TYPE_ABORT:
1635136849Sscottl		pCmd->Result = RETURN_BUS_RESET;
1636136849Sscottl		break;
1637136849Sscottl	case MV_COMPLETION_TYPE_ERROR:
1638136849Sscottl		 MV_ERROR("IAL: COMPLETION ERROR, adapter %d, channel %d, "
1639136849Sscottl			  "flags=%x\n", pMvSataAdapter->adapterId, channelNum,
1640136849Sscottl			  responseFlags);
1641136849Sscottl
1642136849Sscottl		if (responseFlags & 4) {
1643136849Sscottl			MV_ERROR("ATA regs: error %x, sector count %x, LBA low "
1644136849Sscottl				 "%x, LBA mid %x, LBA high %x, device %x, "
1645136849Sscottl				 "status %x\n", registerStruct->errorRegister,
1646136849Sscottl				 registerStruct->sectorCountRegister,
1647136849Sscottl				 registerStruct->lbaLowRegister,
1648136849Sscottl				 registerStruct->lbaMidRegister,
1649136849Sscottl				 registerStruct->lbaHighRegister,
1650136849Sscottl				 registerStruct->deviceRegister,
1651136849Sscottl				 registerStruct->statusRegister);
1652136849Sscottl		}
1653136849Sscottl		/*
1654136849Sscottl		 * We can't do handleEdmaError directly here, because
1655136849Sscottl		 * CommandCompletionCB is called by mv's ISR, if we retry the
1656136849Sscottl		 * command, than the internel data structure may be destroyed
1657136849Sscottl		 */
1658136849Sscottl		pCmd->uScratch.sata_param.responseFlags = responseFlags;
1659136849Sscottl		pCmd->uScratch.sata_param.bIdeStatus =
1660136849Sscottl		    registerStruct->statusRegister;
1661136849Sscottl		pCmd->uScratch.sata_param.errorRegister =
1662136849Sscottl		    registerStruct->errorRegister;
1663136849Sscottl		pCmd->pVDevice->u.disk.QueueLength--;
1664136849Sscottl		CallAfterReturn(_VBUS_P (DPC_PROC)handleEdmaError,pCmd);
1665136849Sscottl		return TRUE;
1666136849Sscottl
1667136849Sscottl	default:
1668136849Sscottl		MV_ERROR(" Unknown completion type (%d)\n", comp_type);
1669136849Sscottl		return MV_FALSE;
1670136849Sscottl	}
1671136849Sscottl
1672136849Sscottl	if (pCmd->uCmd.Ide.Command == IDE_COMMAND_VERIFY &&
1673136849Sscottl	    pCmd->uScratch.sata_param.cmd_priv > 1) {
1674136849Sscottl		pCmd->uScratch.sata_param.cmd_priv --;
1675136849Sscottl		return TRUE;
1676136849Sscottl	}
1677136849Sscottl	pCmd->pVDevice->u.disk.QueueLength--;
1678136849Sscottl	CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
1679136849Sscottl	return TRUE;
1680136849Sscottl}
1681136849Sscottl
1682136849Sscottlvoid
1683136849SscottlfDeviceSendCommand(_VBUS_ARG PCommand pCmd)
1684136849Sscottl{
1685136849Sscottl	MV_SATA_EDMA_PRD_ENTRY *pPRDTable = 0;
1686136849Sscottl	MV_SATA_ADAPTER *pMvSataAdapter;
1687136849Sscottl	MV_SATA_CHANNEL *pMvSataChannel;
1688136849Sscottl	IAL_ADAPTER_T *pAdapter;
1689136849Sscottl	MV_QUEUE_COMMAND_RESULT result;
1690136849Sscottl	MV_QUEUE_COMMAND_INFO commandInfo;
1691136849Sscottl	MV_UDMA_COMMAND_PARAMS *pUdmaParams;
1692136849Sscottl	MV_NONE_UDMA_COMMAND_PARAMS *pNoUdmaParams;
1693136849Sscottl	MV_BOOLEAN is48bit = MV_FALSE;
1694136849Sscottl	PVDevice pVDevice;
1695136849Sscottl	PDevice pDevice;
1696136849Sscottl	ULONG Lba;
1697136849Sscottl	USHORT nSector;
1698136849Sscottl	MV_U8 channel;
1699136849Sscottl	int  i=0;
1700136849Sscottl
1701136849Sscottl	pVDevice = pCmd->pVDevice;
1702136849Sscottl	pDevice = &pVDevice->u.disk;
1703136849Sscottl	Lba = pCmd->uCmd.Ide.Lba;
1704136849Sscottl	nSector = pCmd->uCmd.Ide.nSectors;
1705136849Sscottl	pUdmaParams = &commandInfo.commandParams.udmaCommand;
1706136849Sscottl	pNoUdmaParams = &commandInfo.commandParams.NoneUdmaCommand;
1707136849Sscottl
1708136849Sscottl	DECLARE_BUFFER(FPSCAT_GATH, tmpSg);
1709136849Sscottl
1710136849Sscottl	if (!pDevice->df_on_line) {
1711136849Sscottl		MV_ERROR("Device is offline");
1712136849Sscottl		pCmd->Result = RETURN_BAD_DEVICE;
1713136849Sscottl		CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
1714136849Sscottl		return;
1715136849Sscottl	}
1716136849Sscottl
1717136849Sscottl	pDevice->HeadPosition = pCmd->uCmd.Ide.Lba + pCmd->uCmd.Ide.nSectors;
1718136849Sscottl	pMvSataChannel = pDevice->mv;
1719136849Sscottl	pMvSataAdapter = pMvSataChannel->mvSataAdapter;
1720136849Sscottl	channel = pMvSataChannel->channelNumber;
1721136849Sscottl	pAdapter = pMvSataAdapter->IALData;
1722136849Sscottl
1723136849Sscottl	/*
1724136849Sscottl	 * Old RAID0 has hidden lba. Remember to clear dDeHiddenLba when
1725136849Sscottl	 * deleting array!
1726136849Sscottl	 */
1727136849Sscottl	Lba += pDevice->dDeHiddenLba;
1728136849Sscottl	/* check LBA */
1729136849Sscottl	if (Lba+nSector-1 > pDevice->dDeRealCapacity) {
1730136849Sscottl		pCmd->Result = RETURN_INVALID_REQUEST;
1731136849Sscottl		CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
1732136849Sscottl		return;
1733136849Sscottl	}
1734136849Sscottl
1735136849Sscottl	if(Lba & 0xF0000000){
1736136849Sscottl		is48bit = MV_TRUE;
1737136849Sscottl	}
1738136849Sscottl
1739136849Sscottl	switch (pCmd->uCmd.Ide.Command)	{
1740136849Sscottl	case IDE_COMMAND_READ:
1741136849Sscottl	case IDE_COMMAND_WRITE:
1742136849Sscottl		if (pDevice->bDeModeSetting<8) goto pio;
1743136849Sscottl
1744136849Sscottl		commandInfo.type = MV_QUEUED_COMMAND_TYPE_UDMA;
1745136849Sscottl		pUdmaParams->isEXT = is48bit;
1746136849Sscottl		pUdmaParams->numOfSectors = nSector;
1747136849Sscottl		pUdmaParams->lowLBAAddress = Lba;
1748136849Sscottl		pUdmaParams->highLBAAddress = 0;
1749136849Sscottl		pUdmaParams->prdHighAddr = 0;
1750136849Sscottl		pUdmaParams->callBack = CommandCompletionCB;
1751136849Sscottl		pUdmaParams->commandId = (MV_VOID_PTR )pCmd;
1752136849Sscottl		if(pCmd->uCmd.Ide.Command == IDE_COMMAND_READ)
1753136849Sscottl			pUdmaParams->readWrite = MV_UDMA_TYPE_READ;
1754136849Sscottl		else
1755136849Sscottl			pUdmaParams->readWrite = MV_UDMA_TYPE_WRITE;
1756136849Sscottl
1757136849Sscottl		if (pCmd->pSgTable && pCmd->cf_physical_sg) {
1758136849Sscottl			FPSCAT_GATH sg1=tmpSg, sg2=pCmd->pSgTable;
1759136849Sscottl			do {
1760136849Sscottl				*sg1++=*sg2;
1761136849Sscottl			} while ((sg2++->wSgFlag & SG_FLAG_EOT)==0);
1762136849Sscottl		} else if (!pCmd->pfnBuildSgl ||
1763136849Sscottl			    !pCmd->pfnBuildSgl(_VBUS_P pCmd, tmpSg, 0)) {
1764136849Sscottlpio:
1765136849Sscottl			mvSataDisableChannelDma(pMvSataAdapter, channel);
1766136849Sscottl			mvSataFlushDmaQueue(pMvSataAdapter, channel,
1767136849Sscottl			    MV_FLUSH_TYPE_CALLBACK);
1768136849Sscottl
1769136849Sscottl			if (pCmd->pSgTable && pCmd->cf_physical_sg==0) {
1770136849Sscottl				FPSCAT_GATH sg1=tmpSg, sg2=pCmd->pSgTable;
1771136849Sscottl				do {
1772136849Sscottl					*sg1++=*sg2;
1773136849Sscottl				} while ((sg2++->wSgFlag & SG_FLAG_EOT)==0);
1774136849Sscottl			} else if (!pCmd->pfnBuildSgl ||
1775136849Sscottl				    !pCmd->pfnBuildSgl(_VBUS_P pCmd, tmpSg, 1)){
1776136849Sscottl				pCmd->Result = RETURN_NEED_LOGICAL_SG;
1777136849Sscottl				goto finish_cmd;
1778136849Sscottl			}
1779136849Sscottl
1780136849Sscottl			do {
1781136849Sscottl				ULONG size;
1782136849Sscottl				ULONG_PTR addr = tmpSg->dSgAddress;
1783136849Sscottl
1784136849Sscottl				size = tmpSg->wSgSize? tmpSg->wSgSize : 0x10000;
1785136849Sscottl				if (size & 0x1ff) {
1786136849Sscottl					pCmd->Result = RETURN_INVALID_REQUEST;
1787136849Sscottl					goto finish_cmd;
1788136849Sscottl				}
1789136849Sscottl				if (mvStorageDevATAExecuteNonUDMACommand(
1790136849Sscottl				    pMvSataAdapter, channel,
1791136849Sscottl				    (pCmd->cf_data_out) ?
1792136849Sscottl				    MV_NON_UDMA_PROTOCOL_PIO_DATA_OUT :
1793136849Sscottl				    MV_NON_UDMA_PROTOCOL_PIO_DATA_IN,
1794136849Sscottl				    is48bit, (MV_U16_PTR)addr,
1795136849Sscottl				    size >> 1,	/* count */
1796136849Sscottl				    0,		/* features N/A */
1797136849Sscottl				    (MV_U16)(size>>9),	/*sector count*/
1798136849Sscottl				    (MV_U16)((is48bit ?
1799136849Sscottl				    (MV_U16)((Lba >> 16) & 0xFF00) : 0 ) |
1800136849Sscottl				    (UCHAR)(Lba & 0xFF) ), /*lbalow*/
1801136849Sscottl				    (MV_U16)((Lba >> 8) & 0xFF), /* lbaMid */
1802136849Sscottl				    (MV_U16)((Lba >> 16) & 0xFF),/* lbaHig */
1803136849Sscottl				    (MV_U8)(0x40 | (is48bit ? 0 :
1804136849Sscottl				    (UCHAR)(Lba >> 24) & 0xFF )),/* device */
1805136849Sscottl				    (MV_U8)(is48bit ? (pCmd->cf_data_in ?
1806136849Sscottl				    IDE_COMMAND_READ_EXT :
1807136849Sscottl				    IDE_COMMAND_WRITE_EXT) :
1808136849Sscottl				    pCmd->uCmd.Ide.Command))==MV_FALSE) {
1809136849Sscottl					pCmd->Result = RETURN_IDE_ERROR;
1810136849Sscottl					goto finish_cmd;
1811136849Sscottl				}
1812136849Sscottl				Lba += size>>9;
1813136849Sscottl				if(Lba & 0xF0000000) is48bit = MV_TRUE;
1814136849Sscottl			}
1815136849Sscottl			while ((tmpSg++->wSgFlag & SG_FLAG_EOT)==0);
1816136849Sscottl			pCmd->Result = RETURN_SUCCESS;
1817136849Sscottlfinish_cmd:
1818136849Sscottl			mvSataEnableChannelDma(pMvSataAdapter,channel);
1819136849Sscottl			CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion,
1820136849Sscottl			    pCmd);
1821136849Sscottl			return;
1822136849Sscottl		}
1823136849Sscottl
1824136849Sscottl		pPRDTable = AllocatePRDTable(pAdapter);
1825136849Sscottl		KdPrint(("pPRDTable:%lx\n",pPRDTable));
1826136849Sscottl		if (!pPRDTable) {
1827136849Sscottl			pCmd->Result = RETURN_DEVICE_BUSY;
1828136849Sscottl			CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion,
1829136849Sscottl			    pCmd);
1830136849Sscottl			HPT_ASSERT(0);
1831136849Sscottl			return;
1832136849Sscottl		}
1833136849Sscottl
1834136849Sscottl		do {
1835136849Sscottl			pPRDTable[i].highBaseAddr = 0;
1836136849Sscottl			pPRDTable[i].flags = (MV_U16)tmpSg->wSgFlag;
1837136849Sscottl			pPRDTable[i].byteCount = (MV_U16)tmpSg->wSgSize;
1838136849Sscottl			pPRDTable[i].lowBaseAddr = (MV_U32)tmpSg->dSgAddress;
1839136849Sscottl			pPRDTable[i].reserved = 0;
1840136849Sscottl			i++;
1841136849Sscottl		} while((tmpSg++->wSgFlag & SG_FLAG_EOT)==0);
1842136849Sscottl
1843136849Sscottl		pUdmaParams->prdLowAddr = pAdapter->prdTableDmaAddr +
1844136849Sscottl		    ((ULONG)pPRDTable - (ULONG)pAdapter->prdTableAddr);
1845136849Sscottl
1846136849Sscottl		if ((pUdmaParams->numOfSectors == 256) &&
1847136849Sscottl		    (pMvSataChannel->lba48Address == MV_FALSE)) {
1848136849Sscottl			pUdmaParams->numOfSectors = 0;
1849136849Sscottl		}
1850136849Sscottl
1851136849Sscottl		pCmd->uScratch.sata_param.prdAddr = (PVOID)pPRDTable;
1852136849Sscottl
1853136849Sscottl		result = mvSataQueueCommand(pMvSataAdapter, channel,
1854136849Sscottl		    &commandInfo);
1855136849Sscottl
1856136849Sscottl		if (result != MV_QUEUE_COMMAND_RESULT_OK) {
1857136849Sscottlqueue_failed:
1858136849Sscottl			switch (result) {
1859136849Sscottl			case MV_QUEUE_COMMAND_RESULT_BAD_LBA_ADDRESS:
1860136849Sscottl				MV_ERROR("IAL Error: Edma Queue command "
1861136849Sscottl					 "failed. Bad LBA LBA[31:0](0x%08x)\n",
1862136849Sscottl					 pUdmaParams->lowLBAAddress);
1863136849Sscottl				pCmd->Result = RETURN_IDE_ERROR;
1864136849Sscottl				break;
1865136849Sscottl			case MV_QUEUE_COMMAND_RESULT_QUEUED_MODE_DISABLED:
1866136849Sscottl				MV_ERROR("IAL Error: Edma Queue command "
1867136849Sscottl					 "failed. EDMA disabled adapter %d "
1868136849Sscottl					 "channel %d\n",
1869136849Sscottl					 pMvSataAdapter->adapterId, channel);
1870136849Sscottl				mvSataEnableChannelDma(pMvSataAdapter,channel);
1871136849Sscottl				pCmd->Result = RETURN_IDE_ERROR;
1872136849Sscottl				break;
1873136849Sscottl			case MV_QUEUE_COMMAND_RESULT_FULL:
1874136849Sscottl				MV_ERROR("IAL Error: Edma Queue command "
1875136849Sscottl					 "failed. Queue is Full adapter %d "
1876136849Sscottl					 "channel %d\n",
1877136849Sscottl					 pMvSataAdapter->adapterId, channel);
1878136849Sscottl				pCmd->Result = RETURN_DEVICE_BUSY;
1879136849Sscottl				break;
1880136849Sscottl			case MV_QUEUE_COMMAND_RESULT_BAD_PARAMS:
1881136849Sscottl				MV_ERROR("IAL Error: Edma Queue command "
1882136849Sscottl					 "failed. (Bad Params), pMvSataAdapter:"
1883136849Sscottl					 " %p,  pSataChannel: %p.\n",
1884136849Sscottl					 pMvSataAdapter,
1885136849Sscottl					 pMvSataAdapter->sataChannel[channel]);
1886136849Sscottl				pCmd->Result = RETURN_IDE_ERROR;
1887136849Sscottl				break;
1888136849Sscottl			default:
1889136849Sscottl				MV_ERROR("IAL Error: Bad result value (%d) "
1890136849Sscottl					 "from queue command\n", result);
1891136849Sscottl				pCmd->Result = RETURN_IDE_ERROR;
1892136849Sscottl			}
1893136849Sscottl			if(pPRDTable)
1894136849Sscottl				FreePRDTable(pAdapter, pPRDTable);
1895136849Sscottl			CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion,
1896136849Sscottl			    pCmd);
1897136849Sscottl		}
1898136849Sscottl		pDevice->QueueLength++;
1899136849Sscottl		return;
1900136849Sscottl
1901136849Sscottl	case IDE_COMMAND_VERIFY:
1902136849Sscottl		commandInfo.type = MV_QUEUED_COMMAND_TYPE_NONE_UDMA;
1903136849Sscottl		pNoUdmaParams->bufPtr = NULL;
1904136849Sscottl		pNoUdmaParams->callBack = CommandCompletionCB;
1905136849Sscottl		pNoUdmaParams->commandId = (MV_VOID_PTR)pCmd;
1906136849Sscottl		pNoUdmaParams->count = 0;
1907136849Sscottl		pNoUdmaParams->features = 0;
1908136849Sscottl		pNoUdmaParams->protocolType = MV_NON_UDMA_PROTOCOL_NON_DATA;
1909136849Sscottl
1910136849Sscottl		pCmd->uScratch.sata_param.cmd_priv = 1;
1911136849Sscottl		if (pMvSataChannel->lba48Address == MV_TRUE){
1912136849Sscottl			pNoUdmaParams->command =
1913136849Sscottl			    MV_ATA_COMMAND_READ_VERIFY_SECTORS_EXT;
1914136849Sscottl			pNoUdmaParams->isEXT = MV_TRUE;
1915136849Sscottl			pNoUdmaParams->lbaHigh =
1916136849Sscottl			    (MV_U16)((Lba & 0xff0000) >> 16);
1917136849Sscottl			pNoUdmaParams->lbaMid = (MV_U16)((Lba & 0xff00) >> 8);
1918136849Sscottl			pNoUdmaParams->lbaLow =
1919136849Sscottl			    (MV_U16)(((Lba & 0xff000000) >> 16)| (Lba & 0xff));
1920136849Sscottl			pNoUdmaParams->sectorCount = nSector;
1921136849Sscottl			pNoUdmaParams->device = 0x40;
1922136849Sscottl			result = mvSataQueueCommand(pMvSataAdapter, channel,
1923136849Sscottl			     &commandInfo);
1924136849Sscottl			if (result != MV_QUEUE_COMMAND_RESULT_OK) {
1925136849Sscottl				goto queue_failed;
1926136849Sscottl			}
1927136849Sscottl			return;
1928136849Sscottl		}
1929136849Sscottl		pNoUdmaParams->command = MV_ATA_COMMAND_READ_VERIFY_SECTORS;
1930136849Sscottl		pNoUdmaParams->isEXT = MV_FALSE;
1931136849Sscottl		pNoUdmaParams->lbaHigh = (MV_U16)((Lba & 0xff0000) >> 16);
1932136849Sscottl		pNoUdmaParams->lbaMid = (MV_U16)((Lba & 0xff00) >> 8);
1933136849Sscottl		pNoUdmaParams->lbaLow = (MV_U16)(Lba & 0xff);
1934136849Sscottl		pNoUdmaParams->sectorCount = 0xff & nSector;
1935136849Sscottl		pNoUdmaParams->device = (MV_U8)(0x40 |
1936136849Sscottl			((Lba & 0xf000000) >> 24));
1937136849Sscottl		pNoUdmaParams->callBack = CommandCompletionCB;
1938136849Sscottl		result = mvSataQueueCommand(pMvSataAdapter, channel,
1939136849Sscottl		     &commandInfo);
1940136849Sscottl		/*
1941136849Sscottl		 * FIXME: how about the commands already queued? but marvel
1942136849Sscottl		 * also forgets to consider this
1943136849Sscottl		 */
1944136849Sscottl		if (result != MV_QUEUE_COMMAND_RESULT_OK){
1945136849Sscottl			goto queue_failed;
1946136849Sscottl		}
1947136849Sscottl		break;
1948136849Sscottl	default:
1949136849Sscottl		pCmd->Result = RETURN_INVALID_REQUEST;
1950136849Sscottl		CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
1951136849Sscottl		break;
1952136849Sscottl	}
1953136849Sscottl}
1954136849Sscottl
1955136849Sscottl/**********************************************************
1956136849Sscottl *
1957136849Sscottl *	Probe the hostadapter.
1958136849Sscottl *
1959136849Sscottl **********************************************************/
1960136849Sscottlstatic int
1961136849Sscottlhpt_probe(device_t dev)
1962136849Sscottl{
1963136849Sscottl	if ((pci_get_vendor(dev) == MV_SATA_VENDOR_ID) &&
1964136849Sscottl		(pci_get_device(dev) == MV_SATA_DEVICE_ID_5081
1965136849Sscottl#ifdef FOR_DEMO
1966136849Sscottl		|| pci_get_device(dev) == MV_SATA_DEVICE_ID_5080
1967136849Sscottl#endif
1968136849Sscottl		)) {
1969136849Sscottl		KdPrintI((CONTROLLER_NAME " found\n"));
1970136849Sscottl		device_set_desc(dev, CONTROLLER_NAME);
1971136849Sscottl		return 0;
1972136849Sscottl	}
1973136849Sscottl	else
1974136849Sscottl		return(ENXIO);
1975136849Sscottl}
1976136849Sscottl
1977136849Sscottl/***********************************************************
1978136849Sscottl *
1979136849Sscottl *      Auto configuration:  attach and init a host adapter.
1980136849Sscottl *
1981136849Sscottl ***********************************************************/
1982136849Sscottlstatic int
1983136849Sscottlhpt_attach(device_t dev)
1984136849Sscottl{
1985136849Sscottl	IAL_ADAPTER_T * pAdapter;
1986136849Sscottl	int rid;
1987136849Sscottl	union ccb *ccb;
1988136849Sscottl	struct cam_devq *devq;
1989136849Sscottl	struct cam_sim *hpt_vsim;
1990136849Sscottl
1991136849Sscottl	printf("%s Version %s\n", DRIVER_NAME, DRIVER_VERSION);
1992136849Sscottl
1993136849Sscottl	pAdapter = device_get_softc(dev);
1994136849Sscottl	pAdapter->hpt_dev = dev;
1995136849Sscottl
1996136849Sscottl	rid = init_adapter(pAdapter);
1997136849Sscottl	if (rid)
1998136849Sscottl		return rid;
1999136849Sscottl
2000136849Sscottl	rid = 0;
2001136849Sscottl	if ((pAdapter->hpt_irq = bus_alloc_resource(pAdapter->hpt_dev,
2002136849Sscottl	    SYS_RES_IRQ, &rid, 0, ~0ul, 1, RF_SHAREABLE | RF_ACTIVE)) == NULL){
2003136849Sscottl		hpt_printk(("can't allocate interrupt\n"));
2004136849Sscottl		return(ENXIO);
2005136849Sscottl	}
2006136849Sscottl
2007136849Sscottl	if(bus_setup_intr(pAdapter->hpt_dev, pAdapter->hpt_irq, INTR_TYPE_CAM,
2008136849Sscottl	    hpt_intr, pAdapter, &pAdapter->hpt_intr)) {
2009136849Sscottl		hpt_printk(("can't set up interrupt\n"));
2010136849Sscottl		free(pAdapter, M_DEVBUF);
2011136849Sscottl		return(ENXIO);
2012136849Sscottl	}
2013136849Sscottl
2014136849Sscottl#if 1
2015136849Sscottl	if ((ccb = malloc(sizeof(*ccb), M_DEVBUF, M_WAITOK | M_ZERO)) != NULL) {
2016136849Sscottl		ccb->ccb_h.pinfo.priority = 1;
2017136849Sscottl		ccb->ccb_h.pinfo.index = CAM_UNQUEUED_INDEX;
2018136849Sscottl	} else {
2019136849Sscottl		return ENOMEM;
2020136849Sscottl	}
2021136849Sscottl#endif
2022136849Sscottl	/*
2023136849Sscottl	 * Create the device queue for our SIM(s).
2024136849Sscottl	 */
2025136849Sscottl	if((devq = cam_simq_alloc(8/*MAX_QUEUE_COMM*/)) == NULL) {
2026136849Sscottl		KdPrint(("ENXIO\n"));
2027136849Sscottl		return ENOMEM;
2028136849Sscottl	}
2029136849Sscottl
2030136849Sscottl	/*
2031136849Sscottl	 * Construct our SIM entry
2032136849Sscottl	 */
2033136849Sscottl	if ((hpt_vsim = cam_sim_alloc(hpt_action, hpt_poll,__str(PROC_DIR_NAME),
2034136849Sscottl	    pAdapter, device_get_unit(pAdapter->hpt_dev), /*untagged*/1,
2035136849Sscottl	    /*tagged*/8,  devq)) == NULL) {
2036136849Sscottl		cam_simq_free(devq);
2037136849Sscottl		return ENOMEM;
2038136849Sscottl	}
2039136849Sscottl
2040136849Sscottl	if(xpt_bus_register(hpt_vsim, 0) != CAM_SUCCESS) {
2041136849Sscottl		cam_sim_free(hpt_vsim, /*free devq*/ TRUE);
2042136849Sscottl		hpt_vsim = NULL;
2043136849Sscottl		return ENXIO;
2044136849Sscottl	}
2045136849Sscottl
2046136849Sscottl	if(xpt_create_path(&pAdapter->path, /*periph */ NULL,
2047136849Sscottl	    cam_sim_path(hpt_vsim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD)
2048136849Sscottl	    != CAM_REQ_CMP) {
2049136849Sscottl		xpt_bus_deregister(cam_sim_path(hpt_vsim));
2050136849Sscottl		cam_sim_free(hpt_vsim, /*free_devq*/TRUE);
2051136849Sscottl		hpt_vsim = NULL;
2052136849Sscottl		return ENXIO;
2053136849Sscottl	}
2054136849Sscottl
2055136849Sscottl	xpt_setup_ccb(&(ccb->ccb_h), pAdapter->path, /*priority*/5);
2056136849Sscottl	ccb->ccb_h.func_code = XPT_SASYNC_CB;
2057136849Sscottl	ccb->csa.event_enable = AC_LOST_DEVICE;
2058136849Sscottl	ccb->csa.callback = hpt_async;
2059136849Sscottl	ccb->csa.callback_arg = hpt_vsim;
2060136849Sscottl	xpt_action((union ccb *)ccb);
2061136849Sscottl	free(ccb, M_DEVBUF);
2062136849Sscottl
2063136849Sscottl	if ((pAdapter->eh = EVENTHANDLER_REGISTER(shutdown_final, hpt_shutdown,
2064136849Sscottl	    dev, SHUTDOWN_PRI_DEFAULT)) == NULL)
2065136849Sscottl		device_printf(dev, "Shutdown event registrration failed\n");
2066136849Sscottl
2067136849Sscottl	return 0;
2068136849Sscottl}
2069136849Sscottl
2070136849Sscottlstatic int
2071136849Sscottlhpt_detach(device_t dev)
2072136849Sscottl{
2073136849Sscottl	return (EBUSY);
2074136849Sscottl}
2075136849Sscottl
2076136849Sscottl/***************************************************************
2077136849Sscottl * The poll function is used to simulate the interrupt when
2078136849Sscottl * the interrupt subsystem is not functioning.
2079136849Sscottl *
2080136849Sscottl ***************************************************************/
2081136849Sscottlstatic void
2082136849Sscottlhpt_poll(struct cam_sim *sim)
2083136849Sscottl{
2084136849Sscottl	hpt_intr((void *)cam_sim_softc(sim));
2085136849Sscottl}
2086136849Sscottl
2087136849Sscottl/****************************************************************
2088136849Sscottl *	Name:	hpt_intr
2089136849Sscottl *	Description:	Interrupt handler.
2090136849Sscottl ****************************************************************/
2091136849Sscottlstatic void
2092136849Sscottlhpt_intr(void *arg)
2093136849Sscottl{
2094136849Sscottl	IAL_ADAPTER_T *pAdapter = (IAL_ADAPTER_T *)arg;
2095136849Sscottl	intrmask_t oldspl;
2096136849Sscottl
2097136849Sscottl	 oldspl = lock_driver();
2098136849Sscottl	/* KdPrintI(("----- Entering Isr() -----\n")); */
2099136849Sscottl	if (mvSataInterruptServiceRoutine(&pAdapter->mvSataAdapter) == MV_TRUE){
2100136849Sscottl		_VBUS_INST(&pAdapter->VBus)
2101136849Sscottl		CheckPendingCall(_VBUS_P0);
2102136849Sscottl	}
2103136849Sscottl
2104136849Sscottl	/* KdPrintI(("----- Leaving Isr() -----\n")); */
2105136849Sscottl	unlock_driver(oldspl);
2106136849Sscottl}
2107136849Sscottl
2108136849Sscottl/**********************************************************
2109136849Sscottl * 			Asynchronous Events
2110136849Sscottl *********************************************************/
2111136849Sscottl#if (!defined(UNREFERENCED_PARAMETER))
2112136849Sscottl#define UNREFERENCED_PARAMETER(x) (void)(x)
2113136849Sscottl#endif
2114136849Sscottl
2115136849Sscottlstatic void
2116136849Sscottlhpt_async(void * callback_arg, u_int32_t code, struct cam_path * path,
2117136849Sscottl    void * arg)
2118136849Sscottl{
2119136849Sscottl	/* debug XXXX */
2120136849Sscottl	panic("Here");
2121136849Sscottl	UNREFERENCED_PARAMETER(callback_arg);
2122136849Sscottl	UNREFERENCED_PARAMETER(code);
2123136849Sscottl	UNREFERENCED_PARAMETER(path);
2124136849Sscottl	UNREFERENCED_PARAMETER(arg);
2125136849Sscottl
2126136849Sscottl}
2127136849Sscottl
2128136849Sscottlstatic void
2129136849SscottlFlushAdapter(IAL_ADAPTER_T *pAdapter)
2130136849Sscottl{
2131136849Sscottl	int i;
2132136849Sscottl
2133136849Sscottl	hpt_printk(("flush all devices\n"));
2134136849Sscottl
2135136849Sscottl	/* flush all devices */
2136136849Sscottl	for (i=0; i<MAX_VDEVICE_PER_VBUS; i++) {
2137136849Sscottl		PVDevice pVDev = pAdapter->VBus.pVDevice[i];
2138136849Sscottl		if (pVDev)
2139136849Sscottl			fFlushVDev(pVDev);
2140136849Sscottl	}
2141136849Sscottl}
2142136849Sscottl
2143136849Sscottlstatic int
2144136849Sscottlhpt_shutdown(device_t dev)
2145136849Sscottl{
2146136849Sscottl	IAL_ADAPTER_T *pAdapter;
2147136849Sscottl
2148136849Sscottl	pAdapter = device_get_softc(dev);
2149136849Sscottl	if (pAdapter == NULL)
2150136849Sscottl		return (EINVAL);
2151136849Sscottl
2152136849Sscottl	EVENTHANDLER_DEREGISTER(shutdown_final, pAdapter->eh);
2153136849Sscottl	FlushAdapter(pAdapter);
2154136849Sscottl
2155136849Sscottl	return 0;
2156136849Sscottl}
2157136849Sscottl
2158136849Sscottlvoid
2159136849SscottlCheck_Idle_Call(IAL_ADAPTER_T *pAdapter)
2160136849Sscottl{
2161136849Sscottl	int i = 0;
2162136849Sscottl
2163136849Sscottl	_VBUS_INST(&pAdapter->VBus)
2164136849Sscottl
2165136849Sscottl	if (mWaitingForIdle(_VBUS_P0)) {
2166136849Sscottl		CheckIdleCall(_VBUS_P0);
2167136849Sscottl#ifdef SUPPORT_ARRAY
2168136849Sscottl		for(i = 0; i < MAX_ARRAY_PER_VBUS; i++){
2169136849Sscottl			PVDevice pArray;
2170136849Sscottl
2171136849Sscottl			if ((pArray = ArrayTables(i))->u.array.dArStamp == 0)
2172136849Sscottl				continue;
2173136849Sscottl			else if (pArray->u.array.rf_auto_rebuild) {
2174136849Sscottl				KdPrint(("auto rebuild.\n"));
2175136849Sscottl				pArray->u.array.rf_auto_rebuild = 0;
2176136849Sscottl				hpt_queue_dpc((HPT_DPC)hpt_rebuild_data_block,
2177136849Sscottl				    pAdapter, pArray, DUPLICATE);
2178136849Sscottl			}
2179136849Sscottl		}
2180136849Sscottl#endif
2181136849Sscottl	}
2182136849Sscottl	/* launch the awaiting commands blocked by mWaitingForIdle */
2183136849Sscottl	while(pAdapter->pending_Q!= NULL) {
2184136849Sscottl		_VBUS_INST(&pAdapter->VBus)
2185136849Sscottl		union ccb *ccb =
2186136849Sscottl		    (union ccb *)pAdapter->pending_Q->ccb_h.ccb_ccb_ptr;
2187136849Sscottl
2188136849Sscottl		hpt_free_ccb(&pAdapter->pending_Q, ccb);
2189136849Sscottl		CallAfterReturn(_VBUS_P (DPC_PROC)OsSendCommand, ccb);
2190136849Sscottl	}
2191136849Sscottl}
2192136849Sscottl
2193136849Sscottlstatic void
2194136849Sscottlccb_done(union ccb *ccb)
2195136849Sscottl{
2196136849Sscottl	IAL_ADAPTER_T * pAdapter = (IAL_ADAPTER_T *)ccb->ccb_adapter;
2197136849Sscottl	KdPrintI(("ccb_done: ccb %x status %x", ccb, ccb->ccb_h.status));
2198136849Sscottl
2199136849Sscottl	xpt_done(ccb);
2200136849Sscottl
2201136849Sscottl	pAdapter->outstandingCommands--;
2202136849Sscottl
2203136849Sscottl	if (pAdapter->outstandingCommands == 0) {
2204136849Sscottl		if(DPC_Request_Nums == 0)
2205136849Sscottl			Check_Idle_Call(pAdapter);
2206136849Sscottl	}
2207136849Sscottl}
2208136849Sscottl
2209136849Sscottl/****************************************************************
2210136849Sscottl *	Name:	hpt_action
2211136849Sscottl *	Description:	Process a queued command from the CAM layer.
2212136849Sscottl *	Parameters:		sim - Pointer to SIM object
2213136849Sscottl *					ccb - Pointer to SCSI command structure.
2214136849Sscottl ****************************************************************/
2215136849Sscottl
2216136849Sscottlvoid
2217136849Sscottlhpt_action(struct cam_sim *sim, union ccb *ccb)
2218136849Sscottl{
2219136849Sscottl	intrmask_t oldspl;
2220136849Sscottl	IAL_ADAPTER_T * pAdapter = (IAL_ADAPTER_T *) cam_sim_softc(sim);
2221136849Sscottl	_VBUS_INST(&pAdapter->VBus)
2222136849Sscottl
2223136849Sscottl	ccb->ccb_adapter = pAdapter;
2224136849Sscottl
2225136849Sscottl	CAM_DEBUG(ccb->ccb_h->path, CAM_DEBUG_TRACE, ("hpt_action\n"));
2226136849Sscottl	KdPrint(("hpt_action(%lx,%lx{%x})\n", (u_long)sim, (u_long)ccb,
2227136849Sscottl	    ccb->ccb_h.func_code));
2228136849Sscottl
2229136849Sscottl	switch (ccb->ccb_h.func_code) {
2230136849Sscottl	case XPT_SCSI_IO:	/* Execute the requested I/O operation */
2231136849Sscottl		/* ccb->ccb_h.path_id is not our bus id - don't check it */
2232136849Sscottl
2233136849Sscottl		if (ccb->ccb_h.target_lun)	{
2234136849Sscottl			ccb->ccb_h.status = CAM_LUN_INVALID;
2235136849Sscottl			xpt_done(ccb);
2236136849Sscottl			return;
2237136849Sscottl		}
2238136849Sscottl		if (ccb->ccb_h.target_id >= MAX_VDEVICE_PER_VBUS ||
2239136849Sscottl			pAdapter->VBus.pVDevice[ccb->ccb_h.target_id]==0) {
2240136849Sscottl			ccb->ccb_h.status = CAM_TID_INVALID;
2241136849Sscottl			xpt_done(ccb);
2242136849Sscottl			return;
2243136849Sscottl		}
2244136849Sscottl
2245136849Sscottl		oldspl = lock_driver();
2246136849Sscottl		if (pAdapter->outstandingCommands==0 && DPC_Request_Nums==0)
2247136849Sscottl			Check_Idle_Call(pAdapter);
2248136849Sscottl
2249136849Sscottl		if (mWaitingForIdle(_VBUS_P0))
2250136849Sscottl			hpt_queue_ccb(&pAdapter->pending_Q, ccb);
2251136849Sscottl		else
2252136849Sscottl			OsSendCommand(_VBUS_P ccb);
2253136849Sscottl		unlock_driver(oldspl);
2254136849Sscottl
2255136849Sscottl		/* KdPrint(("leave scsiio\n")); */
2256136849Sscottl		break;
2257136849Sscottl
2258136849Sscottl	case XPT_RESET_BUS:
2259136849Sscottl		KdPrint(("reset bus\n"));
2260136849Sscottl		oldspl = lock_driver();
2261136849Sscottl		fResetVBus(_VBUS_P0);
2262136849Sscottl		unlock_driver(oldspl);
2263136849Sscottl		xpt_done(ccb);
2264136849Sscottl		break;
2265136849Sscottl
2266136849Sscottl	case XPT_RESET_DEV:	/* Bus Device Reset the specified SCSI device */
2267136849Sscottl	case XPT_EN_LUN:		/* Enable LUN as a target */
2268136849Sscottl	case XPT_TARGET_IO:		/* Execute target I/O request */
2269136849Sscottl	case XPT_ACCEPT_TARGET_IO:	/* Accept Host Target Mode CDB */
2270136849Sscottl	case XPT_CONT_TARGET_IO:	/* Continue Host Target I/O Connection*/
2271136849Sscottl	case XPT_ABORT:			/* Abort the specified CCB */
2272136849Sscottl	case XPT_TERM_IO:		/* Terminate the I/O process */
2273136849Sscottl		/* XXX Implement */
2274136849Sscottl		ccb->ccb_h.status = CAM_REQ_INVALID;
2275136849Sscottl		xpt_done(ccb);
2276136849Sscottl		break;
2277136849Sscottl
2278136849Sscottl	case XPT_GET_TRAN_SETTINGS:
2279136849Sscottl	case XPT_SET_TRAN_SETTINGS:
2280136849Sscottl		/* XXX Implement */
2281136849Sscottl		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2282136849Sscottl		xpt_done(ccb);
2283136849Sscottl		break;
2284136849Sscottl
2285136849Sscottl	case XPT_CALC_GEOMETRY:
2286136849Sscottl	{
2287136849Sscottl		struct	  ccb_calc_geometry *ccg;
2288136849Sscottl		u_int32_t size_mb;
2289136849Sscottl		u_int32_t secs_per_cylinder;
2290136849Sscottl
2291136849Sscottl		ccg = &ccb->ccg;
2292136849Sscottl		size_mb = ccg->volume_size / ((1024L*1024L) / ccg->block_size);
2293136849Sscottl
2294136849Sscottl		if (size_mb > 1024 ) {
2295136849Sscottl			ccg->heads = 255;
2296136849Sscottl			ccg->secs_per_track = 63;
2297136849Sscottl		} else {
2298136849Sscottl			ccg->heads = 64;
2299136849Sscottl			ccg->secs_per_track = 32;
2300136849Sscottl		}
2301136849Sscottl		secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2302136849Sscottl		ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2303136849Sscottl		ccb->ccb_h.status = CAM_REQ_CMP;
2304136849Sscottl		xpt_done(ccb);
2305136849Sscottl		break;
2306136849Sscottl	}
2307136849Sscottl
2308136849Sscottl	case XPT_PATH_INQ:		/* Path routing inquiry */
2309136849Sscottl	{
2310136849Sscottl		struct ccb_pathinq *cpi = &ccb->cpi;
2311136849Sscottl
2312136849Sscottl		cpi->version_num = 1; /* XXX??? */
2313136849Sscottl		cpi->hba_inquiry = PI_SDTR_ABLE;
2314136849Sscottl		cpi->target_sprt = 0;
2315136849Sscottl		/* Not necessary to reset bus */
2316136849Sscottl		cpi->hba_misc = PIM_NOBUSRESET;
2317136849Sscottl		cpi->hba_eng_cnt = 0;
2318136849Sscottl
2319136849Sscottl		cpi->max_target = MAX_VDEVICE_PER_VBUS;
2320136849Sscottl		cpi->max_lun = 0;
2321136849Sscottl		cpi->initiator_id = MAX_VDEVICE_PER_VBUS;
2322136849Sscottl
2323136849Sscottl		cpi->bus_id = cam_sim_bus(sim);
2324136849Sscottl		cpi->base_transfer_speed = 3300;
2325136849Sscottl		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2326136849Sscottl		strncpy(cpi->hba_vid, "HPT   ", HBA_IDLEN);
2327136849Sscottl		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2328136849Sscottl		cpi->unit_number = cam_sim_unit(sim);
2329136849Sscottl		cpi->ccb_h.status = CAM_REQ_CMP;
2330136849Sscottl		xpt_done(ccb);
2331136849Sscottl		break;
2332136849Sscottl	}
2333136849Sscottl
2334136849Sscottl	default:
2335136849Sscottl		KdPrint(("invalid cmd\n"));
2336136849Sscottl		ccb->ccb_h.status = CAM_REQ_INVALID;
2337136849Sscottl		xpt_done(ccb);
2338136849Sscottl		break;
2339136849Sscottl	}
2340136849Sscottl	/* KdPrint(("leave hpt_action..............\n")); */
2341136849Sscottl}
2342136849Sscottl
2343136849Sscottl/* shall be called at lock_driver() */
2344136849Sscottlstatic void
2345136849Sscottlhpt_queue_ccb(union ccb **ccb_Q, union ccb *ccb)
2346136849Sscottl{
2347136849Sscottl	if(*ccb_Q == NULL)
2348136849Sscottl		ccb->ccb_h.ccb_ccb_ptr = ccb;
2349136849Sscottl	else {
2350136849Sscottl		ccb->ccb_h.ccb_ccb_ptr = (*ccb_Q)->ccb_h.ccb_ccb_ptr;
2351136849Sscottl		(*ccb_Q)->ccb_h.ccb_ccb_ptr = (char *)ccb;
2352136849Sscottl	}
2353136849Sscottl
2354136849Sscottl	*ccb_Q = ccb;
2355136849Sscottl}
2356136849Sscottl
2357136849Sscottl/* shall be called at lock_driver() */
2358136849Sscottlstatic void
2359136849Sscottlhpt_free_ccb(union ccb **ccb_Q, union ccb *ccb)
2360136849Sscottl{
2361136849Sscottl	union ccb *TempCCB;
2362136849Sscottl
2363136849Sscottl	TempCCB = *ccb_Q;
2364136849Sscottl
2365136849Sscottl	if(ccb->ccb_h.ccb_ccb_ptr == ccb)
2366136849Sscottl		/*it means SCpnt is the last one in CURRCMDs*/
2367136849Sscottl		*ccb_Q = NULL;
2368136849Sscottl	else {
2369136849Sscottl		while(TempCCB->ccb_h.ccb_ccb_ptr != (char *)ccb)
2370136849Sscottl			TempCCB = (union ccb *)TempCCB->ccb_h.ccb_ccb_ptr;
2371136849Sscottl
2372136849Sscottl		TempCCB->ccb_h.ccb_ccb_ptr = ccb->ccb_h.ccb_ccb_ptr;
2373136849Sscottl
2374136849Sscottl		if(*ccb_Q == ccb)
2375136849Sscottl			*ccb_Q = TempCCB;
2376136849Sscottl	}
2377136849Sscottl}
2378136849Sscottl
2379136849Sscottl#ifdef SUPPORT_ARRAY
2380136849Sscottl/***************************************************************************
2381136849Sscottl * Function:     hpt_worker_thread
2382136849Sscottl * Description:  Do background rebuilding. Execute in kernel thread context.
2383136849Sscottl * Returns:      None
2384136849Sscottl ***************************************************************************/
2385136849Sscottlstatic void hpt_worker_thread(void)
2386136849Sscottl{
2387136849Sscottl	intrmask_t oldspl;
2388136849Sscottl
2389136849Sscottl	for(;;)	{
2390136849Sscottl		while (DpcQueue_First!=DpcQueue_Last) {
2391136849Sscottl			ST_HPT_DPC p;
2392136849Sscottl			IAL_ADAPTER_T *pAdapter;
2393136849Sscottl			PVDevice      pArray;
2394136849Sscottl			PVBus         _vbus_p;
2395136849Sscottl			int i;
2396136849Sscottl
2397136849Sscottl			oldspl = lock_driver();
2398136849Sscottl			p = DpcQueue[DpcQueue_First];
2399136849Sscottl			DpcQueue_First++;
2400136849Sscottl			DpcQueue_First %= MAX_DPC;
2401136849Sscottl			DPC_Request_Nums++;
2402136849Sscottl			unlock_driver(oldspl);
2403136849Sscottl			p.dpc(p.pAdapter, p.arg, p.flags);
2404136849Sscottl
2405136849Sscottl			oldspl = lock_driver();
2406136849Sscottl			DPC_Request_Nums--;
2407136849Sscottl			/*
2408136849Sscottl			 * since we may have prevented Check_Idle_Call, do it
2409136849Sscottl			 * here
2410136849Sscottl			 */
2411136849Sscottl			if (DPC_Request_Nums==0) {
2412136849Sscottl				if (p.pAdapter->outstandingCommands == 0) {
2413136849Sscottl					_VBUS_INST(&p.pAdapter->VBus);
2414136849Sscottl					Check_Idle_Call(p.pAdapter);
2415136849Sscottl					CheckPendingCall(_VBUS_P0);
2416136849Sscottl				}
2417136849Sscottl			}
2418136849Sscottl			unlock_driver(oldspl);
2419136849Sscottl			if (SIGISMEMBER(curproc->p_siglist, SIGSTOP) == 0)
2420136849Sscottl				continue;
2421136849Sscottl
2422136849Sscottl			/* abort rebuilding process. */
2423136849Sscottl			pAdapter = gIal_Adapter;
2424136849Sscottl			while (pAdapter != 0) {
2425136849Sscottl				_vbus_p = &pAdapter->VBus;
2426136849Sscottl				for (i = 0; i < MAX_ARRAY_PER_VBUS;i++){
2427136849Sscottl					if ((pArray=ArrayTables(i))->u.array.dArStamp==0)
2428136849Sscottl						continue;
2429136849Sscottl					if (pArray->u.array.rf_rebuilding ||
2430136849Sscottl					    pArray->u.array.rf_verifying ||
2431136849Sscottl					    pArray->u.array.rf_initializing) {
2432136849Sscottl						pArray->u.array.rf_abort_rebuild = 1;
2433136849Sscottl					}
2434136849Sscottl				}
2435136849Sscottl				pAdapter = pAdapter->next;
2436136849Sscottl			}
2437136849Sscottl		}
2438136849Sscottl
2439136849Sscottl#ifdef DEBUG
2440136849Sscottl		if (SIGISMEMBER(curproc->p_siglist, SIGSTOP))
2441136849Sscottl			tsleep(hpt_worker_thread, PPAUSE, "hptrdy", 2 * hz);
2442136849Sscottl#endif
2443136849Sscottl#if (__FreeBSD_version >= 500043)
2444136849Sscottl		kthread_suspend_check(curproc);
2445136849Sscottl#else
2446136849Sscottl		kproc_suspend_loop(curproc);
2447136849Sscottl#endif
2448136849Sscottl		/* wait for something to do */
2449136849Sscottl		tsleep(hpt_worker_thread, PPAUSE, "hptrdy", 2 * hz);
2450136849Sscottl	}
2451136849Sscottl}
2452136849Sscottl
2453136849Sscottlstatic struct proc *hptdaemonproc;
2454136849Sscottlstatic struct kproc_desc hpt_kp = {
2455136849Sscottl	"hpt_wt",
2456136849Sscottl	hpt_worker_thread,
2457136849Sscottl	&hptdaemonproc
2458136849Sscottl};
2459136849Sscottl
2460136849Sscottlstatic void
2461136849Sscottllaunch_worker_thread(void)
2462136849Sscottl{
2463136849Sscottl	IAL_ADAPTER_T *pAdapTemp;
2464136849Sscottl
2465136849Sscottl	kproc_start(&hpt_kp);
2466136849Sscottl
2467136849Sscottl	for (pAdapTemp = gIal_Adapter; pAdapTemp; pAdapTemp = pAdapTemp->next) {
2468136849Sscottl
2469136849Sscottl		_VBUS_INST(&pAdapTemp->VBus)
2470136849Sscottl		int i;
2471136849Sscottl		PVDevice pVDev;
2472136849Sscottl
2473136849Sscottl		for(i = 0; i < MAX_ARRAY_PER_VBUS; i++)
2474136849Sscottl			if ((pVDev=ArrayTables(i))->u.array.dArStamp==0)
2475136849Sscottl				continue;
2476136849Sscottl			if (pVDev->u.array.rf_need_rebuild &&
2477136849Sscottl			    !pVDev->u.array.rf_rebuilding) {
2478136849Sscottl				hpt_queue_dpc((HPT_DPC)hpt_rebuild_data_block,
2479136849Sscottl				    pAdapTemp, pVDev,
2480136849Sscottl				    (UCHAR)((pVDev->u.array.CriticalMembers ||
2481136849Sscottl				    pVDev->VDeviceType == VD_RAID_1) ?
2482136849Sscottl				    DUPLICATE : REBUILD_PARITY));
2483136849Sscottl			    }
2484136849Sscottl	}
2485136849Sscottl
2486136849Sscottl	/*
2487136849Sscottl	 * hpt_worker_thread needs to be suspended after shutdown sync, when fs
2488136849Sscottl	 * sync finished.
2489136849Sscottl	 */
2490136849Sscottl#if (__FreeBSD_version < 500043)
2491136849Sscottl	EVENTHANDLER_REGISTER(shutdown_post_sync, shutdown_kproc,
2492136849Sscottl	    hptdaemonproc, SHUTDOWN_PRI_FIRST);
2493136849Sscottl#else
2494136849Sscottl	EVENTHANDLER_REGISTER(shutdown_post_sync, kproc_shutdown,
2495136849Sscottl	    hptdaemonproc, SHUTDOWN_PRI_FIRST);
2496136849Sscottl#endif
2497136849Sscottl}
2498136849SscottlSYSINIT(hptwt, SI_SUB_KTHREAD_IDLE, SI_ORDER_FIRST, launch_worker_thread, NULL);
2499136849Sscottl
2500136849Sscottl#endif /* SUPPORT_ARRAY */
2501136849Sscottl
2502136849Sscottl/* build sgl with merge function */
2503136849Sscottl#define ON64KBOUNDARY(x) (((ULONG_PTR)(x) & 0xFFFF) == 0)
2504136849Sscottl
2505136849Sscottl/* XXX */
2506136849Sscottl/* #define NOTNEIGHBORPAGE(x, y) (max(x, y) - min(x, y) > PAGE_SIZE) */
2507136849Sscottl#define NOTNEIGHBORPAGE(highvaddr, lowvaddr)	\
2508136849Sscottl    ((ULONG_PTR)(highvaddr) - (ULONG_PTR)(lowvaddr) != PAGE_SIZE)
2509136849Sscottl
2510136849Sscottl
2511136849Sscottl/********************************************************************************/
2512136849Sscottl
2513136849Sscottlstatic void
2514136849Sscottlhptmv_buffer_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
2515136849Sscottl{
2516136849Sscottl	FPSCAT_GATH pSg;
2517136849Sscottl	int idx;
2518136849Sscottl
2519136849Sscottl	if (error || nsegs == 0) {
2520136849Sscottl		panic("busdma bewm");
2521136849Sscottl		return;
2522136849Sscottl	}
2523136849Sscottl
2524136849Sscottl	pSg = *(FPSCAT_GATH *)arg;
2525136849Sscottl
2526136849Sscottl	for (idx = 0; idx < nsegs; idx++) {
2527136849Sscottl		pSg[idx].dSgAddress = (ULONG_PTR)segs[idx].ds_addr;
2528136849Sscottl		pSg[idx].wSgSize = segs[idx].ds_len;
2529136849Sscottl		pSg[idx].wSgFlag = 0;
2530136849Sscottl	}
2531136849Sscottl	pSg[idx - 1].wSgFlag = SG_FLAG_EOT;
2532136849Sscottl
2533136849Sscottl	return;
2534136849Sscottl}
2535136849Sscottl
2536136849Sscottlstatic int HPTLIBAPI
2537136849SscottlfOsBuildSgl(_VBUS_ARG PCommand pCmd, FPSCAT_GATH pSg, int logical)
2538136849Sscottl{
2539136849Sscottl	IAL_ADAPTER_T *pAdapter;
2540136849Sscottl	pPrivCommand prvCmd;
2541136849Sscottl	union ccb *ccb;
2542136849Sscottl	struct ccb_hdr *ccb_h;
2543136849Sscottl	struct ccb_scsiio *csio;
2544136849Sscottl	bus_dma_segment_t *sgList;
2545136849Sscottl	int error;
2546136849Sscottl
2547136849Sscottl	prvCmd = pCmd->pOrgCommand;
2548136849Sscottl	pAdapter = prvCmd->pAdapter;
2549136849Sscottl	ccb = prvCmd->ccb;
2550136849Sscottl	ccb_h = &ccb->ccb_h;
2551136849Sscottl	csio = &ccb->csio;
2552136849Sscottl	sgList = (bus_dma_segment_t *)(csio->data_ptr);
2553136849Sscottl
2554136849Sscottl	if ((ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_NONE)
2555136849Sscottl		return TRUE;
2556136849Sscottl
2557136849Sscottl	if ((ccb_h->flags & CAM_SCATTER_VALID) != 0) {
2558136849Sscottl		if((ccb_h->flags & CAM_DATA_PHYS) != 0)
2559136849Sscottl			panic(KMSG_LEADING "physical address unsupported!");
2560136849Sscottl
2561136849Sscottl		hptmv_buffer_callback(&pSg, sgList, csio->sglist_cnt, 0);
2562136849Sscottl		return TRUE;
2563136849Sscottl	}
2564136849Sscottl
2565136849Sscottl	if (logical) {
2566136849Sscottl		if ((ccb_h->flags & CAM_DATA_PHYS) != 0)
2567136849Sscottl			panic(KMSG_LEADING "physical address unsupported\n");
2568136849Sscottl
2569136849Sscottl		pSg->dSgAddress = (ULONG_PTR)csio->data_ptr;
2570136849Sscottl		pSg->wSgSize = (USHORT)csio->dxfer_len;
2571136849Sscottl		pSg->wSgFlag = SG_FLAG_EOT;
2572136849Sscottl		return TRUE;
2573136849Sscottl	}
2574136849Sscottl
2575136849Sscottl	KdPrint(("use sgl (physical) ...........\n"));
2576136849Sscottl
2577136849Sscottl	/*
2578136849Sscottl	 * XXX Hack to make this work with PAE.  It will fail under
2579136849Sscottl	 * heavy load.
2580136849Sscottl	 */
2581136849Sscottl	error = bus_dmamap_load(pAdapter->buf_dmat, prvCmd->buf_map,
2582136849Sscottl	    csio->data_ptr, csio->dxfer_len, hptmv_buffer_callback, &pSg,
2583136849Sscottl	    BUS_DMA_NOWAIT);
2584136849Sscottl
2585136849Sscottl	if (error) {
2586136849Sscottl		printf("bus_dmamap_load failed error= %d\n", error);
2587136849Sscottl		return FALSE;
2588136849Sscottl	}
2589136849Sscottl
2590136849Sscottl/*#ifdef DEBUG
2591136849Sscottl	do {
2592136849Sscottl		int size, i = 0;
2593136849Sscottl		KdPrintI(("sg[%d]:0x%lx %d\n", i++, pSg[i].dSgAddress,
2594136849Sscottl		    pSg[i].wSgSize));
2595136849Sscottl		size = pSg->wSgSize;
2596136849Sscottl		if (pSg[i].wSgFlag & SG_FLAG_EOT)
2597136849Sscottl			break;
2598136849Sscottl	} while (i<17);
2599136849Sscottl#endif*/
2600136849Sscottl
2601136849Sscottl	if ((ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2602136849Sscottl		bus_dmamap_sync(pAdapter->buf_dmat, prvCmd->buf_map,
2603136849Sscottl		    BUS_DMASYNC_PREREAD);
2604136849Sscottl	} else {
2605136849Sscottl		bus_dmamap_sync(pAdapter->buf_dmat, prvCmd->buf_map,
2606136849Sscottl		    BUS_DMASYNC_PREWRITE);
2607136849Sscottl	}
2608136849Sscottl
2609136849Sscottl	return TRUE;
2610136849Sscottl}
2611136849Sscottl
2612136849Sscottl/*******************************************************************************/
2613136849SscottlULONG HPTLIBAPI
2614136849SscottlGetStamp(void)
2615136849Sscottl{
2616136849Sscottl	ULONG stamp;
2617136849Sscottl
2618136849Sscottl	/*
2619136849Sscottl	 * the system variable, ticks, can't be used since it hasn't yet been active
2620136849Sscottl	 * when our driver starts (ticks==0, it's a invalid stamp value)
2621136849Sscottl	 */
2622136849Sscottl	do {
2623136849Sscottl		stamp = random();
2624136849Sscottl	} while (stamp==0);
2625136849Sscottl
2626136849Sscottl	return stamp;
2627136849Sscottl}
2628136849Sscottl
2629136849Sscottl
2630136849Sscottlstatic void
2631136849SscottlSetInquiryData(PINQUIRYDATA inquiryData, PVDevice pVDev)
2632136849Sscottl{
2633136849Sscottl	int i;
2634136849Sscottl	IDENTIFY_DATA2 *pIdentify;
2635136849Sscottl
2636136849Sscottl	pIdentify = (IDENTIFY_DATA2*)pVDev->u.disk.mv->identifyDevice;
2637136849Sscottl	inquiryData->DeviceType = T_DIRECT; /*DIRECT_ACCESS_DEVICE*/
2638136849Sscottl	inquiryData->AdditionalLength = (UCHAR)(sizeof(INQUIRYDATA) - 5);
2639136849Sscottl#ifndef SERIAL_CMDS
2640136849Sscottl	inquiryData->CommandQueue = 1;
2641136849Sscottl#endif
2642136849Sscottl
2643136849Sscottl	switch(pVDev->VDeviceType) {
2644136849Sscottl	case VD_SINGLE_DISK:
2645136849Sscottl	case VD_ATAPI:
2646136849Sscottl	case VD_REMOVABLE:
2647136849Sscottl		/* Set the removable bit, if applicable. */
2648136849Sscottl		if ((pVDev->u.disk.df_removable_drive) ||
2649136849Sscottl		    (pIdentify->GeneralConfiguration & 0x80))
2650136849Sscottl			inquiryData->RemovableMedia = 1;
2651136849Sscottl
2652136849Sscottl		/* Fill in vendor identification fields. */
2653136849Sscottl		for (i = 0; i < 20; i += 2) {
2654136849Sscottl			inquiryData->VendorId[i] =
2655136849Sscottl			    ((PUCHAR)pIdentify->ModelNumber)[i + 1];
2656136849Sscottl			inquiryData->VendorId[i+1] =
2657136849Sscottl			    ((PUCHAR)pIdentify->ModelNumber)[i];
2658136849Sscottl
2659136849Sscottl		}
2660136849Sscottl
2661136849Sscottl		/* Initialize unused portion of product id. */
2662136849Sscottl		for (i = 0; i < 4; i++) inquiryData->ProductId[12+i] = ' ';
2663136849Sscottl
2664136849Sscottl		/* firmware revision */
2665136849Sscottl		for (i = 0; i < 4; i += 2) {
2666136849Sscottl			inquiryData->ProductRevisionLevel[i] =
2667136849Sscottl			    ((PUCHAR)pIdentify->FirmwareRevision)[i+1];
2668136849Sscottl			inquiryData->ProductRevisionLevel[i+1] =
2669136849Sscottl			    ((PUCHAR)pIdentify->FirmwareRevision)[i];
2670136849Sscottl		}
2671136849Sscottl		break;
2672136849Sscottl	default:
2673136849Sscottl		memcpy(&inquiryData->VendorId, "RR182x  ", 8);
2674136849Sscottl#ifdef SUPPORT_ARRAY
2675136849Sscottl		switch(pVDev->VDeviceType) {
2676136849Sscottl		case VD_RAID_0:
2677136849Sscottl			if ((pVDev->u.array.pMember[0] &&
2678136849Sscottl			    mIsArray(pVDev->u.array.pMember[0])) ||
2679136849Sscottl			    (pVDev->u.array.pMember[1] &&
2680136849Sscottl			    mIsArray(pVDev->u.array.pMember[1])))
2681136849Sscottl				memcpy(&inquiryData->ProductId,
2682136849Sscottl				    "RAID 1/0 Array  ", 16);
2683136849Sscottl			else
2684136849Sscottl				memcpy(&inquiryData->ProductId,
2685136849Sscottl				    "RAID 0 Array    ", 16);
2686136849Sscottl			break;
2687136849Sscottl		case VD_RAID_1:
2688136849Sscottl			if ((pVDev->u.array.pMember[0] &&
2689136849Sscottl			    mIsArray(pVDev->u.array.pMember[0])) ||
2690136849Sscottl			    (pVDev->u.array.pMember[1] &&
2691136849Sscottl			    mIsArray(pVDev->u.array.pMember[1])))
2692136849Sscottl				memcpy(&inquiryData->ProductId,
2693136849Sscottl				    "RAID 0/1 Array  ", 16);
2694136849Sscottl			else
2695136849Sscottl				memcpy(&inquiryData->ProductId,
2696136849Sscottl				    "RAID 1 Array    ", 16);
2697136849Sscottl			break;
2698136849Sscottl		case VD_RAID_5:
2699136849Sscottl			memcpy(&inquiryData->ProductId, "RAID 5 Array    ", 16);
2700136849Sscottl			break;
2701136849Sscottl		case VD_JBOD:
2702136849Sscottl			memcpy(&inquiryData->ProductId, "JBOD Array      ", 16);
2703136849Sscottl			break;
2704136849Sscottl		}
2705136849Sscottl#endif
2706136849Sscottl		memcpy(&inquiryData->ProductRevisionLevel, "3.00", 4);
2707136849Sscottl		break;
2708136849Sscottl	}
2709136849Sscottl}
2710136849Sscottl
2711136849Sscottlstatic void
2712136849Sscottlhpt_timeout(void *arg)
2713136849Sscottl{
2714136849Sscottl	_VBUS_INST(&((IAL_ADAPTER_T*)((union ccb *)arg)->ccb_adapter)->VBus)
2715136849Sscottl	intrmask_t oldspl;
2716136849Sscottl
2717136849Sscottl	oldspl = lock_driver();
2718136849Sscottl	fResetVBus(_VBUS_P0);
2719136849Sscottl	unlock_driver(oldspl);
2720136849Sscottl}
2721136849Sscottl
2722136849Sscottlstatic void HPTLIBAPI
2723136849SscottlOsSendCommand(_VBUS_ARG union ccb *ccb)
2724136849Sscottl{
2725136849Sscottl	IAL_ADAPTER_T *pAdapter;
2726136849Sscottl	struct ccb_hdr *ccb_h;
2727136849Sscottl	struct ccb_scsiio *csio;
2728136849Sscottl	PVDevice pVDev;
2729136849Sscottl
2730136849Sscottl	pAdapter = (IAL_ADAPTER_T *)ccb->ccb_adapter;
2731136849Sscottl	ccb_h = &ccb->ccb_h;
2732136849Sscottl	csio = &ccb->csio;
2733136849Sscottl	pVDev = pAdapter->VBus.pVDevice[ccb_h->target_id];
2734136849Sscottl
2735136849Sscottl	KdPrintI(("OsSendCommand: ccb %x cdb %x-%x-%x\n",
2736136849Sscottl		ccb,
2737136849Sscottl		*(ULONG *)&ccb->csio.cdb_io.cdb_bytes[0],
2738136849Sscottl		*(ULONG *)&ccb->csio.cdb_io.cdb_bytes[4],
2739136849Sscottl		*(ULONG *)&ccb->csio.cdb_io.cdb_bytes[8]
2740136849Sscottl	));
2741136849Sscottl
2742136849Sscottl	pAdapter->outstandingCommands++;
2743136849Sscottl
2744136849Sscottl	if (pVDev == NULL || pVDev->vf_online == 0) {
2745136849Sscottl		ccb->ccb_h.status = CAM_REQ_INVALID;
2746136849Sscottl		ccb_done(ccb);
2747136849Sscottl		goto Command_Complished;
2748136849Sscottl	}
2749136849Sscottl
2750136849Sscottl	switch(ccb->csio.cdb_io.cdb_bytes[0])
2751136849Sscottl	{
2752136849Sscottl	case TEST_UNIT_READY:
2753136849Sscottl	case START_STOP_UNIT:
2754136849Sscottl	case SYNCHRONIZE_CACHE:
2755136849Sscottl		/* FALLTHROUGH */
2756136849Sscottl		ccb->ccb_h.status = CAM_REQ_CMP;
2757136849Sscottl		break;
2758136849Sscottl
2759136849Sscottl	case INQUIRY:
2760136849Sscottl		ZeroMemory(ccb->csio.data_ptr, ccb->csio.dxfer_len);
2761136849Sscottl		SetInquiryData((PINQUIRYDATA)ccb->csio.data_ptr, pVDev);
2762136849Sscottl		ccb_h->status = CAM_REQ_CMP;
2763136849Sscottl		break;
2764136849Sscottl
2765136849Sscottl	case READ_CAPACITY:
2766136849Sscottl	{
2767136849Sscottl		UCHAR swip[4];
2768136849Sscottl		/* Claim 512 byte blocks (big-endian). */
2769136849Sscottl		((PREAD_CAPACITY_DATA)csio->data_ptr)->BytesPerBlock = 0x20000;
2770136849Sscottl		*(ULONG*)swip = pVDev->VDeviceCapacity - 1;
2771136849Sscottl		((PREAD_CAPACITY_DATA)csio->data_ptr)->LogicalBlockAddress =
2772136849Sscottl			(swip[0] << 24) |  (swip[1] << 16) | (swip[2] << 8) | swip[3];
2773136849Sscottl		ccb_h->status = CAM_REQ_CMP;
2774136849Sscottl		break;
2775136849Sscottl	}
2776136849Sscottl
2777136849Sscottl	case READ_6:
2778136849Sscottl	case WRITE_6:
2779136849Sscottl	case READ_10:
2780136849Sscottl	case WRITE_10:
2781136849Sscottl	case 0x13:
2782136849Sscottl	case 0x2f:
2783136849Sscottl	{
2784136849Sscottl		UCHAR Cdb[16];
2785136849Sscottl		UCHAR CdbLength;
2786136849Sscottl		_VBUS_INST(pVDev->pVBus)
2787136849Sscottl		PCommand pCmd;
2788136849Sscottl
2789136849Sscottl		pCmd = AllocateCommand(_VBUS_P0);
2790136849Sscottl		HPT_ASSERT(pCmd);
2791136849Sscottl
2792136849Sscottl		CdbLength = csio->cdb_len;
2793136849Sscottl		if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
2794136849Sscottl			if ((ccb->ccb_h.flags & CAM_CDB_PHYS) == 0) {
2795136849Sscottl				bcopy(csio->cdb_io.cdb_ptr, Cdb, CdbLength);
2796136849Sscottl			} else {
2797136849Sscottl				KdPrintE(("ERROR!!!\n"));
2798136849Sscottl				ccb->ccb_h.status = CAM_REQ_INVALID;
2799136849Sscottl				break;
2800136849Sscottl			}
2801136849Sscottl		} else {
2802136849Sscottl			bcopy(csio->cdb_io.cdb_bytes, Cdb, CdbLength);
2803136849Sscottl		}
2804136849Sscottl
2805136849Sscottl		pCmd->pOrgCommand = AllocPrivCommand(pAdapter);
2806136849Sscottl		if (pCmd->pOrgCommand == NULL)
2807136849Sscottl			panic("command leak!");
2808136849Sscottl		((pPrivCommand)(pCmd->pOrgCommand))->ccb = ccb;
2809136849Sscottl		pCmd->pVDevice = pVDev;
2810136849Sscottl		pCmd->pfnCompletion = fOsCommandDone;
2811136849Sscottl		pCmd->pfnBuildSgl = fOsBuildSgl;
2812136849Sscottl
2813136849Sscottl		switch (Cdb[0]) {
2814136849Sscottl		case READ_6:
2815136849Sscottl		case WRITE_6:
2816136849Sscottl		case 0x13:
2817136849Sscottl			pCmd->uCmd.Ide.Lba =  ((ULONG)Cdb[1] << 16) |
2818136849Sscottl			    ((ULONG)Cdb[2] << 8) | (ULONG)Cdb[3];
2819136849Sscottl			pCmd->uCmd.Ide.nSectors = (USHORT) Cdb[4];
2820136849Sscottl			break;
2821136849Sscottl		default:
2822136849Sscottl			pCmd->uCmd.Ide.Lba = (ULONG)Cdb[5] |
2823136849Sscottl			    ((ULONG)Cdb[4] << 8) | ((ULONG)Cdb[3] << 16) |
2824136849Sscottl			    ((ULONG)Cdb[2] << 24);
2825136849Sscottl			pCmd->uCmd.Ide.nSectors = (USHORT) Cdb[8] |
2826136849Sscottl			    ((USHORT)Cdb[7]<<8);
2827136849Sscottl			break;
2828136849Sscottl		}
2829136849Sscottl
2830136849Sscottl		switch (Cdb[0]) {
2831136849Sscottl		case READ_6:
2832136849Sscottl		case READ_10:
2833136849Sscottl			pCmd->uCmd.Ide.Command = IDE_COMMAND_READ;
2834136849Sscottl			pCmd->cf_data_in = 1;
2835136849Sscottl			break;
2836136849Sscottl
2837136849Sscottl		case WRITE_6:
2838136849Sscottl		case WRITE_10:
2839136849Sscottl			pCmd->uCmd.Ide.Command = IDE_COMMAND_WRITE;
2840136849Sscottl			pCmd->cf_data_out = 1;
2841136849Sscottl			break;
2842136849Sscottl		case 0x13:
2843136849Sscottl		case 0x2f:
2844136849Sscottl			pCmd->uCmd.Ide.Command = IDE_COMMAND_VERIFY;
2845136849Sscottl			break;
2846136849Sscottl		}
2847136849Sscottl
2848136849Sscottl		ccb->ccb_h.timeout_ch = timeout(hpt_timeout, (caddr_t)ccb, 20*hz);
2849136849Sscottl
2850136849Sscottl		pVDev->pfnSendCommand(_VBUS_P pCmd);
2851136849Sscottl		goto Command_Complished;
2852136849Sscottl	}
2853136849Sscottl
2854136849Sscottl	default:
2855136849Sscottl		ccb->ccb_h.status = CAM_REQ_INVALID;
2856136849Sscottl		break;
2857136849Sscottl	}
2858136849Sscottl	ccb_done(ccb);
2859136849SscottlCommand_Complished:
2860136849Sscottl	CheckPendingCall(_VBUS_P0);
2861136849Sscottl	return;
2862136849Sscottl}
2863136849Sscottl
2864136849Sscottlstatic void HPTLIBAPI
2865136849SscottlfOsCommandDone(_VBUS_ARG PCommand pCmd)
2866136849Sscottl{
2867136849Sscottl	IAL_ADAPTER_T *pAdapter;
2868136849Sscottl	pPrivCommand prvCmd;
2869136849Sscottl	union ccb *ccb;
2870136849Sscottl
2871136849Sscottl	prvCmd = pCmd->pOrgCommand;
2872136849Sscottl	pAdapter = prvCmd->pAdapter;
2873136849Sscottl	ccb = prvCmd->ccb;
2874136849Sscottl
2875136849Sscottl	KdPrint(("fOsCommandDone(%p, %d)", pCmd, pCmd->Result));
2876136849Sscottl
2877136849Sscottl	untimeout(hpt_timeout, (caddr_t)ccb, ccb->ccb_h.timeout_ch);
2878136849Sscottl
2879136849Sscottl	switch(pCmd->Result) {
2880136849Sscottl	case RETURN_SUCCESS:
2881136849Sscottl		ccb->ccb_h.status = CAM_REQ_CMP;
2882136849Sscottl		break;
2883136849Sscottl	case RETURN_BAD_DEVICE:
2884136849Sscottl		ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2885136849Sscottl		break;
2886136849Sscottl	case RETURN_DEVICE_BUSY:
2887136849Sscottl		ccb->ccb_h.status = CAM_BUSY;
2888136849Sscottl		break;
2889136849Sscottl	case RETURN_INVALID_REQUEST:
2890136849Sscottl		ccb->ccb_h.status = CAM_REQ_INVALID;
2891136849Sscottl		break;
2892136849Sscottl	case RETURN_SELECTION_TIMEOUT:
2893136849Sscottl		ccb->ccb_h.status = CAM_SEL_TIMEOUT;
2894136849Sscottl		break;
2895136849Sscottl	case RETURN_RETRY:
2896136849Sscottl		ccb->ccb_h.status = CAM_BUSY;
2897136849Sscottl		break;
2898136849Sscottl	default:
2899136849Sscottl		ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
2900136849Sscottl		break;
2901136849Sscottl	}
2902136849Sscottl
2903136849Sscottl	if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2904136849Sscottl		bus_dmamap_sync(pAdapter->buf_dmat, prvCmd->buf_map,
2905136849Sscottl		    BUS_DMASYNC_POSTREAD);
2906136849Sscottl	} else {
2907136849Sscottl		bus_dmamap_sync(pAdapter->buf_dmat, prvCmd->buf_map,
2908136849Sscottl		    BUS_DMASYNC_POSTWRITE);
2909136849Sscottl	}
2910136849Sscottl	bus_dmamap_unload(pAdapter->buf_dmat, prvCmd->buf_map);
2911136849Sscottl
2912136849Sscottl	FreePrivCommand(pAdapter, prvCmd);
2913136849Sscottl	FreeCommand(_VBUS_P pCmd);
2914136849Sscottl	ccb_done(ccb);
2915136849Sscottl}
2916136849Sscottl
2917136849Sscottlint
2918136849Sscottlhpt_queue_dpc(HPT_DPC dpc, IAL_ADAPTER_T * pAdapter, void *arg, UCHAR flags)
2919136849Sscottl{
2920136849Sscottl	int p;
2921136849Sscottl
2922136849Sscottl	p = (DpcQueue_Last + 1) % MAX_DPC;
2923136849Sscottl	if (p==DpcQueue_First) {
2924136849Sscottl		KdPrint(("DPC Queue full!\n"));
2925136849Sscottl		return -1;
2926136849Sscottl	}
2927136849Sscottl
2928136849Sscottl	DpcQueue[DpcQueue_Last].dpc = dpc;
2929136849Sscottl	DpcQueue[DpcQueue_Last].pAdapter = pAdapter;
2930136849Sscottl	DpcQueue[DpcQueue_Last].arg = arg;
2931136849Sscottl	DpcQueue[DpcQueue_Last].flags = flags;
2932136849Sscottl	DpcQueue_Last = p;
2933136849Sscottl
2934136849Sscottl	return 0;
2935136849Sscottl}
2936136849Sscottl
2937136849Sscottl#ifdef _RAID5N_
2938136849Sscottl/*
2939136849Sscottl * Allocate memory above 16M, otherwise we may eat all low memory for ISA
2940136849Sscottl * devices.
2941136849Sscottl *
2942136849Sscottl * Busdma should be used here, not contigmalloc/free.  However, this API
2943136849Sscottl * will need to be changed to use it effective.
2944136849Sscottl */
2945136849Sscottlvoid
2946136849Sscottl*os_alloc_page(_VBUS_ARG0)
2947136849Sscottl{
2948136849Sscottl	return (void *)contigmalloc(0x1000, M_DEVBUF, M_NOWAIT, 0x1000000,
2949136849Sscottl	    0xffffffff, PAGE_SIZE, 0);
2950136849Sscottl}
2951136849Sscottlvoid
2952136849Sscottl*os_alloc_dma_page(_VBUS_ARG0)
2953136849Sscottl{
2954136849Sscottl	return (void *)contigmalloc(0x1000, M_DEVBUF, M_NOWAIT, 0x1000000,
2955136849Sscottl	    0xffffffff, PAGE_SIZE, 0);
2956136849Sscottl}
2957136849Sscottl
2958136849Sscottl/*
2959136849Sscottl * The next two are not used right now.
2960136849Sscottl */
2961136849Sscottlvoid
2962136849Sscottlos_free_page(_VBUS_ARG void *p)
2963136849Sscottl{
2964136849Sscottl	contigfree(p, 0x1000, M_DEVBUF);
2965136849Sscottl}
2966136849Sscottl
2967136849Sscottlvoid
2968136849Sscottlos_free_dma_page(_VBUS_ARG void *p)
2969136849Sscottl{
2970136849Sscottl	contigfree(p, 0x1000, M_DEVBUF);
2971136849Sscottl}
2972136849Sscottl
2973136849Sscottlvoid
2974136849SscottlDoXor1(ULONG *p0, ULONG *p1, ULONG *p2, UINT nBytes)
2975136849Sscottl{
2976136849Sscottl	UINT i;
2977136849Sscottl	for (i = 0; i < nBytes / 4; i++)
2978136849Sscottl		*p0++ = *p1++ ^ *p2++;
2979136849Sscottl}
2980136849Sscottl
2981136849Sscottlvoid
2982136849SscottlDoXor2(ULONG *p0, ULONG *p2, UINT nBytes)
2983136849Sscottl{
2984136849Sscottl	UINT i;
2985136849Sscottl	for (i = 0; i < nBytes / 4; i++)
2986136849Sscottl		*p0++ ^= *p2++;
2987136849Sscottl}
2988136849Sscottl#endif
2989