2 * Copyright (c) 2004-2005 HighPoint Technologies, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/sys/dev/hptmv/entry.c,v 1.26 2012/06/01 04:34:49 eadler Exp $
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/resource.h>
36 #include <sys/callout.h>
37 #include <sys/signalvar.h>
38 #include <sys/eventhandler.h>
40 #include <sys/kthread.h>
43 #include <sys/module.h>
45 #include <bus/pci/pcireg.h>
46 #include <bus/pci/pcivar.h>
47 #include <bus/cam/cam.h>
48 #include <bus/cam/cam_ccb.h>
49 #include <bus/cam/cam_xpt.h>
50 #include <bus/cam/cam_xpt_periph.h>
56 #include <dev/raid/hptmv/global.h>
57 #include <dev/raid/hptmv/hptintf.h>
58 #include <dev/raid/hptmv/osbsd.h>
59 #include <dev/raid/hptmv/access601.h>
64 int hpt_dbg_level = DEBUG_LEVEL;
66 int hpt_dbg_level = 0;
70 #define MV_ERROR kprintf
73 * CAM SIM entry points
75 static int hpt_probe (device_t dev);
76 static void launch_worker_thread(void);
77 static int hpt_attach(device_t dev);
78 static int hpt_detach(device_t dev);
79 static int hpt_shutdown(device_t dev);
80 static void hpt_poll(struct cam_sim *sim);
81 static void hpt_intr(void *arg);
82 static void hpt_async(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg);
83 static void hpt_action(struct cam_sim *sim, union ccb *ccb);
85 static struct thread *hptdaemonproc;
87 static device_method_t driver_methods[] = {
88 /* Device interface */
89 DEVMETHOD(device_probe, hpt_probe),
90 DEVMETHOD(device_attach, hpt_attach),
91 DEVMETHOD(device_detach, hpt_detach),
93 DEVMETHOD(device_shutdown, hpt_shutdown),
97 static driver_t hpt_pci_driver = {
100 sizeof(IAL_ADAPTER_T)
103 static devclass_t hpt_devclass;
105 #define __DRIVER_MODULE(p1, p2, p3, p4, p5, p6) DRIVER_MODULE(p1, p2, p3, p4, p5, p6)
106 __DRIVER_MODULE(PROC_DIR_NAME, pci, hpt_pci_driver, hpt_devclass, NULL, NULL);
107 MODULE_DEPEND(PROC_DIR_NAME, cam, 1, 1, 1);
109 #define ccb_ccb_ptr spriv_ptr0
110 #define ccb_adapter ccb_h.spriv_ptr1
112 static void SetInquiryData(PINQUIRYDATA inquiryData, PVDevice pVDev);
113 static void HPTLIBAPI OsSendCommand (_VBUS_ARG union ccb * ccb);
114 static void HPTLIBAPI fOsCommandDone(_VBUS_ARG PCommand pCmd);
115 static void ccb_done(union ccb *ccb);
116 static void hpt_queue_ccb(union ccb **ccb_Q, union ccb *ccb);
117 static void hpt_free_ccb(union ccb **ccb_Q, union ccb *ccb);
118 static void hptmv_free_edma_queues(IAL_ADAPTER_T *pAdapter);
119 static void hptmv_free_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum);
120 static void handleEdmaError(_VBUS_ARG PCommand pCmd);
121 static int hptmv_init_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum);
122 static int fResetActiveCommands(PVBus _vbus_p);
123 static void fRegisterVdevice(IAL_ADAPTER_T *pAdapter);
124 static int hptmv_allocate_edma_queues(IAL_ADAPTER_T *pAdapter);
125 static void hptmv_handle_event_disconnect(void *data);
126 static void hptmv_handle_event_connect(void *data);
127 static int start_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum);
128 static void init_vdev_params(IAL_ADAPTER_T *pAdapter, MV_U8 channel);
129 static int hptmv_parse_identify_results(MV_SATA_CHANNEL *pMvSataChannel);
130 static int HPTLIBAPI fOsBuildSgl(_VBUS_ARG PCommand pCmd, FPSCAT_GATH pSg,
132 static MV_BOOLEAN CommandCompletionCB(MV_SATA_ADAPTER *pMvSataAdapter,
133 MV_U8 channelNum, MV_COMPLETION_TYPE comp_type, MV_VOID_PTR commandId,
134 MV_U16 responseFlags, MV_U32 timeStamp,
135 MV_STORAGE_DEVICE_REGISTERS *registerStruct);
136 static MV_BOOLEAN hptmv_event_notify(MV_SATA_ADAPTER *pMvSataAdapter,
137 MV_EVENT_TYPE eventType, MV_U32 param1, MV_U32 param2);
139 #define ccb_ccb_ptr spriv_ptr0
140 #define ccb_adapter ccb_h.spriv_ptr1
142 IAL_ADAPTER_T *gIal_Adapter = NULL;
143 IAL_ADAPTER_T *pCurAdapter = NULL;
144 static MV_SATA_CHANNEL gMvSataChannels[MAX_VBUS][MV_SATA_CHANNELS_NUM];
146 typedef struct st_HPT_DPC {
147 IAL_ADAPTER_T *pAdapter;
148 void (*dpc)(IAL_ADAPTER_T *, void *, UCHAR);
154 UCHAR DPC_Request_Nums = 0;
155 static ST_HPT_DPC DpcQueue[MAX_DPC];
156 static int DpcQueue_First=0;
157 static int DpcQueue_Last = 0;
159 char DRIVER_VERSION[] = "v1.16";
161 static struct lock driver_lock;
162 void lock_driver(void)
164 lockmgr(&driver_lock, LK_EXCLUSIVE);
166 void unlock_driver(void)
168 lockmgr(&driver_lock, LK_RELEASE);
171 /*******************************************************************************
172 * Name: hptmv_free_channel
174 * Description: free allocated queues for the given channel
176 * Parameters: pMvSataAdapter - pointer to the RR18xx controler this
177 * channel connected to.
178 * channelNum - channel number.
180 ******************************************************************************/
182 hptmv_free_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum)
184 HPT_ASSERT(channelNum < MV_SATA_CHANNELS_NUM);
185 pAdapter->mvSataAdapter.sataChannel[channelNum] = NULL;
188 static void failDevice(PVDevice pVDev)
190 PVBus _vbus_p = pVDev->pVBus;
191 IAL_ADAPTER_T *pAdapter = (IAL_ADAPTER_T *)_vbus_p->OsExt;
193 pVDev->u.disk.df_on_line = 0;
194 pVDev->vf_online = 0;
195 if (pVDev->pfnDeviceFailed)
196 CallWhenIdle(_VBUS_P (DPC_PROC)pVDev->pfnDeviceFailed, pVDev);
198 fNotifyGUI(ET_DEVICE_REMOVED, pVDev);
201 if (pAdapter->ver_601==2 && !pAdapter->beeping) {
202 pAdapter->beeping = 1;
203 BeepOn(pAdapter->mvSataAdapter.adapterIoBaseAddress);
204 set_fail_led(&pAdapter->mvSataAdapter, pVDev->u.disk.mv->channelNumber, 1);
209 int MvSataResetChannel(MV_SATA_ADAPTER *pMvSataAdapter, MV_U8 channel);
212 handleEdmaError(_VBUS_ARG PCommand pCmd)
214 PDevice pDevice = &pCmd->pVDevice->u.disk;
215 MV_SATA_ADAPTER * pSataAdapter = pDevice->mv->mvSataAdapter;
217 if (!pDevice->df_on_line) {
218 KdPrint(("Device is offline"));
219 pCmd->Result = RETURN_BAD_DEVICE;
220 CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
224 if (pCmd->RetryCount++>5) {
225 hpt_printk(("too many retries on channel(%d)\n", pDevice->mv->channelNumber));
227 failDevice(pCmd->pVDevice);
228 pCmd->Result = RETURN_IDE_ERROR;
229 CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
233 /* reset the channel and retry the command */
234 if (MvSataResetChannel(pSataAdapter, pDevice->mv->channelNumber))
237 fNotifyGUI(ET_DEVICE_ERROR, Map2pVDevice(pDevice));
239 hpt_printk(("Retry on channel(%d)\n", pDevice->mv->channelNumber));
240 fDeviceSendCommand(_VBUS_P pCmd);
243 /****************************************************************
244 * Name: hptmv_init_channel
246 * Description: allocate request and response queues for the EDMA of the
247 * given channel and sets other fields.
250 * pAdapter - pointer to the emulated adapter data structure
251 * channelNum - channel number.
252 * Return: 0 on success, otherwise on failure
253 ****************************************************************/
255 hptmv_init_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum)
257 MV_SATA_CHANNEL *pMvSataChannel;
258 dma_addr_t req_dma_addr;
259 dma_addr_t rsp_dma_addr;
261 if (channelNum >= MV_SATA_CHANNELS_NUM)
263 MV_ERROR("RR18xx[%d]: Bad channelNum=%d",
264 pAdapter->mvSataAdapter.adapterId, channelNum);
268 pMvSataChannel = &gMvSataChannels[pAdapter->mvSataAdapter.adapterId][channelNum];
269 pAdapter->mvSataAdapter.sataChannel[channelNum] = pMvSataChannel;
270 pMvSataChannel->channelNumber = channelNum;
271 pMvSataChannel->lba48Address = MV_FALSE;
272 pMvSataChannel->maxReadTransfer = MV_FALSE;
274 pMvSataChannel->requestQueue = (struct mvDmaRequestQueueEntry *)
275 (pAdapter->requestsArrayBaseAlignedAddr + (channelNum * MV_EDMA_REQUEST_QUEUE_SIZE));
276 req_dma_addr = pAdapter->requestsArrayBaseDmaAlignedAddr + (channelNum * MV_EDMA_REQUEST_QUEUE_SIZE);
279 KdPrint(("requestQueue addr is 0x%llX", (HPT_U64)(ULONG_PTR)req_dma_addr));
281 /* check the 1K alignment of the request queue*/
282 if (req_dma_addr & 0x3ff)
284 MV_ERROR("RR18xx[%d]: request queue allocated isn't 1 K aligned,"
285 " dma_addr=%llx channel=%d\n", pAdapter->mvSataAdapter.adapterId,
286 (HPT_U64)(ULONG_PTR)req_dma_addr, channelNum);
289 pMvSataChannel->requestQueuePciLowAddress = req_dma_addr;
290 pMvSataChannel->requestQueuePciHiAddress = 0;
291 KdPrint(("RR18xx[%d,%d]: request queue allocated: 0x%p",
292 pAdapter->mvSataAdapter.adapterId, channelNum,
293 pMvSataChannel->requestQueue));
294 pMvSataChannel->responseQueue = (struct mvDmaResponseQueueEntry *)
295 (pAdapter->responsesArrayBaseAlignedAddr + (channelNum * MV_EDMA_RESPONSE_QUEUE_SIZE));
296 rsp_dma_addr = pAdapter->responsesArrayBaseDmaAlignedAddr + (channelNum * MV_EDMA_RESPONSE_QUEUE_SIZE);
298 /* check the 256 alignment of the response queue*/
299 if (rsp_dma_addr & 0xff)
301 MV_ERROR("RR18xx[%d,%d]: response queue allocated isn't 256 byte "
302 "aligned, dma_addr=%llx\n",
303 pAdapter->mvSataAdapter.adapterId, channelNum, (HPT_U64)(ULONG_PTR)rsp_dma_addr);
306 pMvSataChannel->responseQueuePciLowAddress = rsp_dma_addr;
307 pMvSataChannel->responseQueuePciHiAddress = 0;
308 KdPrint(("RR18xx[%d,%d]: response queue allocated: 0x%p",
309 pAdapter->mvSataAdapter.adapterId, channelNum,
310 pMvSataChannel->responseQueue));
312 pAdapter->mvChannel[channelNum].online = MV_TRUE;
316 /******************************************************************************
317 * Name: hptmv_parse_identify_results
319 * Description: this functions parses the identify command results, checks
320 * that the connected deives can be accesed by RR18xx EDMA,
321 * and updates the channel stucture accordingly.
323 * Parameters: pMvSataChannel, pointer to the channel data structure.
325 * Returns: =0 ->success, < 0 ->failure.
327 ******************************************************************************/
329 hptmv_parse_identify_results(MV_SATA_CHANNEL *pMvSataChannel)
331 MV_U16 *iden = pMvSataChannel->identifyDevice;
334 if (! (iden[IDEN_CAPACITY_1_OFFSET] & 0x200))
336 KdPrint(("IAL Error in IDENTIFY info: LBA not supported\n"));
341 KdPrint(("%25s - %s\n", "Capabilities", "LBA supported"));
344 if (! (iden[IDEN_CAPACITY_1_OFFSET] & 0x100))
346 KdPrint(("IAL Error in IDENTIFY info: DMA not supported\n"));
351 KdPrint(("%25s - %s\n", "Capabilities", "DMA supported"));
354 if ((iden[IDEN_VALID] & 2) == 0)
356 KdPrint(("IAL Error in IDENTIFY info: not able to find PIO mode\n"));
359 KdPrint(("%25s - 0x%02x\n", "PIO modes supported",
360 iden[IDEN_PIO_MODE_SPPORTED] & 0xff));
363 if ((iden[IDEN_VALID] & 4) == 0)
365 KdPrint(("IAL Error in IDENTIFY info: not able to find UDMA mode\n"));
370 if ((iden[IDEN_SUPPORTED_COMMANDS2] & 0x400))
372 KdPrint(("%25s - %s\n", "LBA48 addressing", "supported"));
373 pMvSataChannel->lba48Address = MV_TRUE;
377 KdPrint(("%25s - %s\n", "LBA48 addressing", "Not supported"));
378 pMvSataChannel->lba48Address = MV_FALSE;
384 init_vdev_params(IAL_ADAPTER_T *pAdapter, MV_U8 channel)
386 PVDevice pVDev = &pAdapter->VDevices[channel];
387 MV_SATA_CHANNEL *pMvSataChannel = pAdapter->mvSataAdapter.sataChannel[channel];
388 MV_U16_PTR IdentifyData = pMvSataChannel->identifyDevice;
390 pMvSataChannel->outstandingCommands = 0;
392 pVDev->u.disk.mv = pMvSataChannel;
393 pVDev->u.disk.df_on_line = 1;
394 pVDev->u.disk.pVBus = &pAdapter->VBus;
395 pVDev->pVBus = &pAdapter->VBus;
397 #ifdef SUPPORT_48BIT_LBA
398 if (pMvSataChannel->lba48Address == MV_TRUE)
399 pVDev->u.disk.dDeRealCapacity = ((IdentifyData[101]<<16) | IdentifyData[100]) - 1;
402 if(IdentifyData[53] & 1) {
403 pVDev->u.disk.dDeRealCapacity =
404 (((IdentifyData[58]<<16 | IdentifyData[57]) < (IdentifyData[61]<<16 | IdentifyData[60])) ?
405 (IdentifyData[61]<<16 | IdentifyData[60]) :
406 (IdentifyData[58]<<16 | IdentifyData[57])) - 1;
408 pVDev->u.disk.dDeRealCapacity =
409 (IdentifyData[61]<<16 | IdentifyData[60]) - 1;
411 pVDev->u.disk.bDeUsable_Mode = pVDev->u.disk.bDeModeSetting =
412 pAdapter->mvChannel[channel].maxPioModeSupported - MV_ATA_TRANSFER_PIO_0;
414 if (pAdapter->mvChannel[channel].maxUltraDmaModeSupported!=0xFF) {
415 pVDev->u.disk.bDeUsable_Mode = pVDev->u.disk.bDeModeSetting =
416 pAdapter->mvChannel[channel].maxUltraDmaModeSupported - MV_ATA_TRANSFER_UDMA_0 + 8;
420 static void device_change(IAL_ADAPTER_T *pAdapter , MV_U8 channelIndex, int plugged)
423 MV_SATA_ADAPTER *pMvSataAdapter = &pAdapter->mvSataAdapter;
424 MV_SATA_CHANNEL *pMvSataChannel = pMvSataAdapter->sataChannel[channelIndex];
426 if (!pMvSataChannel) return;
430 pVDev = &(pAdapter->VDevices[channelIndex]);
431 init_vdev_params(pAdapter, channelIndex);
433 pVDev->VDeviceType = pVDev->u.disk.df_atapi? VD_ATAPI :
434 pVDev->u.disk.df_removable_drive? VD_REMOVABLE : VD_SINGLE_DISK;
436 pVDev->VDeviceCapacity = pVDev->u.disk.dDeRealCapacity-SAVE_FOR_RAID_INFO;
437 pVDev->pfnSendCommand = pfnSendCommand[pVDev->VDeviceType];
438 pVDev->pfnDeviceFailed = pfnDeviceFailed[pVDev->VDeviceType];
439 pVDev->vf_online = 1;
445 for(iMember = 0; iMember < pVDev->pParent->u.array.bArnMember; iMember++)
446 if((PVDevice)pVDev->pParent->u.array.pMember[iMember] == pVDev)
447 pVDev->pParent->u.array.pMember[iMember] = NULL;
448 pVDev->pParent = NULL;
451 fNotifyGUI(ET_DEVICE_PLUGGED,pVDev);
452 fCheckBootable(pVDev);
453 RegisterVDevice(pVDev);
456 if (pAdapter->beeping) {
457 pAdapter->beeping = 0;
458 BeepOff(pAdapter->mvSataAdapter.adapterIoBaseAddress);
465 pVDev = &(pAdapter->VDevices[channelIndex]);
471 start_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum)
473 MV_SATA_ADAPTER *pMvSataAdapter = &pAdapter->mvSataAdapter;
474 MV_SATA_CHANNEL *pMvSataChannel = pMvSataAdapter->sataChannel[channelNum];
475 MV_CHANNEL *pChannelInfo = &(pAdapter->mvChannel[channelNum]);
476 MV_U32 udmaMode,pioMode;
478 KdPrint(("RR18xx [%d]: start channel (%d)", pMvSataAdapter->adapterId,
482 /* Software reset channel */
483 if (mvStorageDevATASoftResetDevice(pMvSataAdapter, channelNum) == MV_FALSE)
485 MV_ERROR("RR18xx [%d,%d]: failed to perform Software reset\n",
486 pMvSataAdapter->adapterId, channelNum);
490 /* Hardware reset channel */
491 if (mvSataChannelHardReset(pMvSataAdapter, channelNum) == MV_FALSE)
493 /* If failed, try again - this is when trying to hardreset a channel */
494 /* when drive is just spinning up */
495 StallExec(5000000); /* wait 5 sec before trying again */
496 if (mvSataChannelHardReset(pMvSataAdapter, channelNum) == MV_FALSE)
498 MV_ERROR("RR18xx [%d,%d]: failed to perform Hard reset\n",
499 pMvSataAdapter->adapterId, channelNum);
505 if (mvStorageDevATAIdentifyDevice(pMvSataAdapter, channelNum) == MV_FALSE)
507 MV_ERROR("RR18xx [%d,%d]: failed to perform ATA Identify command\n"
508 , pMvSataAdapter->adapterId, channelNum);
511 if (hptmv_parse_identify_results(pMvSataChannel))
513 MV_ERROR("RR18xx [%d,%d]: Error in parsing ATA Identify message\n"
514 , pMvSataAdapter->adapterId, channelNum);
518 /* mvStorageDevATASetFeatures */
519 /* Disable 8 bit PIO in case CFA enabled */
520 if (pMvSataChannel->identifyDevice[86] & 4)
522 KdPrint(("RR18xx [%d]: Disable 8 bit PIO (CFA enabled) \n",
523 pMvSataAdapter->adapterId));
524 if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
525 MV_ATA_SET_FEATURES_DISABLE_8_BIT_PIO, 0,
526 0, 0, 0) == MV_FALSE)
528 MV_ERROR("RR18xx [%d]: channel %d: mvStorageDevATASetFeatures"
529 " failed\n", pMvSataAdapter->adapterId, channelNum);
534 #ifdef ENABLE_WRITE_CACHE
535 if (pMvSataChannel->identifyDevice[82] & 0x20)
537 if (!(pMvSataChannel->identifyDevice[85] & 0x20)) /* if not enabled by default */
539 if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
540 MV_ATA_SET_FEATURES_ENABLE_WCACHE, 0,
541 0, 0, 0) == MV_FALSE)
543 MV_ERROR("RR18xx [%d]: channel %d: mvStorageDevATASetFeatures failed\n",
544 pMvSataAdapter->adapterId, channelNum);
548 KdPrint(("RR18xx [%d]: channel %d, write cache enabled\n",
549 pMvSataAdapter->adapterId, channelNum));
553 KdPrint(("RR18xx [%d]: channel %d, write cache not supported\n",
554 pMvSataAdapter->adapterId, channelNum));
556 #else /* disable write cache */
558 if (pMvSataChannel->identifyDevice[85] & 0x20)
560 KdPrint(("RR18xx [%d]: channel =%d, disable write cache\n",
561 pMvSataAdapter->adapterId, channelNum));
562 if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
563 MV_ATA_SET_FEATURES_DISABLE_WCACHE, 0,
564 0, 0, 0) == MV_FALSE)
566 MV_ERROR("RR18xx [%d]: channel %d: mvStorageDevATASetFeatures failed\n",
567 pMvSataAdapter->adapterId, channelNum);
571 KdPrint(("RR18xx [%d]: channel=%d, write cache disabled\n",
572 pMvSataAdapter->adapterId, channelNum));
576 /* Set transfer mode */
577 KdPrint(("RR18xx [%d] Set transfer mode XFER_PIO_SLOW\n",
578 pMvSataAdapter->adapterId));
579 if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
580 MV_ATA_SET_FEATURES_TRANSFER,
581 MV_ATA_TRANSFER_PIO_SLOW, 0, 0, 0) ==
584 MV_ERROR("RR18xx [%d] channel %d: Set Features failed\n",
585 pMvSataAdapter->adapterId, channelNum);
589 if (pMvSataChannel->identifyDevice[IDEN_PIO_MODE_SPPORTED] & 1)
591 pioMode = MV_ATA_TRANSFER_PIO_4;
593 else if (pMvSataChannel->identifyDevice[IDEN_PIO_MODE_SPPORTED] & 2)
595 pioMode = MV_ATA_TRANSFER_PIO_3;
599 MV_ERROR("IAL Error in IDENTIFY info: PIO modes 3 and 4 not supported\n");
600 pioMode = MV_ATA_TRANSFER_PIO_SLOW;
603 KdPrint(("RR18xx [%d] Set transfer mode XFER_PIO_4\n",
604 pMvSataAdapter->adapterId));
605 pAdapter->mvChannel[channelNum].maxPioModeSupported = pioMode;
606 if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
607 MV_ATA_SET_FEATURES_TRANSFER,
608 pioMode, 0, 0, 0) == MV_FALSE)
610 MV_ERROR("RR18xx [%d] channel %d: Set Features failed\n",
611 pMvSataAdapter->adapterId, channelNum);
615 udmaMode = MV_ATA_TRANSFER_UDMA_0;
616 if (pMvSataChannel->identifyDevice[IDEN_UDMA_MODE] & 0x40)
618 udmaMode = MV_ATA_TRANSFER_UDMA_6;
620 else if (pMvSataChannel->identifyDevice[IDEN_UDMA_MODE] & 0x20)
622 udmaMode = MV_ATA_TRANSFER_UDMA_5;
624 else if (pMvSataChannel->identifyDevice[IDEN_UDMA_MODE] & 0x10)
626 udmaMode = MV_ATA_TRANSFER_UDMA_4;
628 else if (pMvSataChannel->identifyDevice[IDEN_UDMA_MODE] & 8)
630 udmaMode = MV_ATA_TRANSFER_UDMA_3;
632 else if (pMvSataChannel->identifyDevice[IDEN_UDMA_MODE] & 4)
634 udmaMode = MV_ATA_TRANSFER_UDMA_2;
637 KdPrint(("RR18xx [%d] Set transfer mode XFER_UDMA_%d\n",
638 pMvSataAdapter->adapterId, udmaMode & 0xf));
639 pChannelInfo->maxUltraDmaModeSupported = udmaMode;
641 /*if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
642 MV_ATA_SET_FEATURES_TRANSFER, udmaMode,
643 0, 0, 0) == MV_FALSE)
645 MV_ERROR("RR18xx [%d] channel %d: Set Features failed\n",
646 pMvSataAdapter->adapterId, channelNum);
649 if (pChannelInfo->maxUltraDmaModeSupported == 0xFF)
654 if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
655 MV_ATA_SET_FEATURES_TRANSFER,
656 pChannelInfo->maxUltraDmaModeSupported,
657 0, 0, 0) == MV_FALSE)
659 if (pChannelInfo->maxUltraDmaModeSupported > MV_ATA_TRANSFER_UDMA_0)
661 if (mvStorageDevATASoftResetDevice(pMvSataAdapter, channelNum) == MV_FALSE)
663 MV_REG_WRITE_BYTE(pMvSataAdapter->adapterIoBaseAddress,
664 pMvSataChannel->eDmaRegsOffset +
665 0x11c, /* command reg */
666 MV_ATA_COMMAND_IDLE_IMMEDIATE);
667 mvMicroSecondsDelay(10000);
668 mvSataChannelHardReset(pMvSataAdapter, channelNum);
669 if (mvStorageDevATASoftResetDevice(pMvSataAdapter, channelNum) == MV_FALSE)
672 if (mvSataChannelHardReset(pMvSataAdapter, channelNum) == MV_FALSE)
674 pChannelInfo->maxUltraDmaModeSupported--;
682 /* Read look ahead */
683 #ifdef ENABLE_READ_AHEAD
684 if (pMvSataChannel->identifyDevice[82] & 0x40)
686 if (!(pMvSataChannel->identifyDevice[85] & 0x40)) /* if not enabled by default */
688 if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
689 MV_ATA_SET_FEATURES_ENABLE_RLA, 0, 0,
692 MV_ERROR("RR18xx [%d] channel %d: Set Features failed\n",
693 pMvSataAdapter->adapterId, channelNum);
697 KdPrint(("RR18xx [%d]: channel=%d, read look ahead enabled\n",
698 pMvSataAdapter->adapterId, channelNum));
702 KdPrint(("RR18xx [%d]: channel %d, Read Look Ahead not supported\n",
703 pMvSataAdapter->adapterId, channelNum));
707 if (pMvSataChannel->identifyDevice[86] & 0x20)
709 KdPrint(("RR18xx [%d]:channel %d, disable read look ahead\n",
710 pMvSataAdapter->adapterId, channelNum));
711 if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
712 MV_ATA_SET_FEATURES_DISABLE_RLA, 0, 0,
715 MV_ERROR("RR18xx [%d]:channel %d: ATA Set Features failed\n",
716 pMvSataAdapter->adapterId, channelNum);
720 KdPrint(("RR18xx [%d]:channel %d, read look ahead disabled\n",
721 pMvSataAdapter->adapterId, channelNum));
727 KdPrint(("RR18xx [%d]: channel %d config EDMA, Non Queued Mode\n",
728 pMvSataAdapter->adapterId,
730 if (mvSataConfigEdmaMode(pMvSataAdapter, channelNum,
731 MV_EDMA_MODE_NOT_QUEUED, 0) == MV_FALSE)
733 MV_ERROR("RR18xx [%d] channel %d Error: mvSataConfigEdmaMode failed\n",
734 pMvSataAdapter->adapterId, channelNum);
739 if (mvSataEnableChannelDma(pMvSataAdapter, channelNum) == MV_FALSE)
741 MV_ERROR("RR18xx [%d] Failed to enable DMA, channel=%d\n",
742 pMvSataAdapter->adapterId, channelNum);
745 MV_ERROR("RR18xx [%d,%d]: channel started successfully\n",
746 pMvSataAdapter->adapterId, channelNum);
749 set_fail_led(pMvSataAdapter, channelNum, 0);
755 hptmv_handle_event(void * data, int flag)
757 IAL_ADAPTER_T *pAdapter = (IAL_ADAPTER_T *)data;
758 MV_SATA_ADAPTER *pMvSataAdapter = &pAdapter->mvSataAdapter;
761 /* mvOsSemTake(&pMvSataAdapter->semaphore); */
762 for (channelIndex = 0; channelIndex < MV_SATA_CHANNELS_NUM; channelIndex++)
764 switch(pAdapter->sataEvents[channelIndex])
766 case SATA_EVENT_CHANNEL_CONNECTED:
767 /* Handle only connects */
770 KdPrint(("RR18xx [%d,%d]: new device connected\n",
771 pMvSataAdapter->adapterId, channelIndex));
772 hptmv_init_channel(pAdapter, channelIndex);
773 if (mvSataConfigureChannel( pMvSataAdapter, channelIndex) == MV_FALSE)
775 MV_ERROR("RR18xx [%d,%d] Failed to configure\n",
776 pMvSataAdapter->adapterId, channelIndex);
777 hptmv_free_channel(pAdapter, channelIndex);
781 /*mvSataChannelHardReset(pMvSataAdapter, channel);*/
782 if (start_channel( pAdapter, channelIndex))
784 MV_ERROR("RR18xx [%d,%d]Failed to start channel\n",
785 pMvSataAdapter->adapterId, channelIndex);
786 hptmv_free_channel(pAdapter, channelIndex);
790 device_change(pAdapter, channelIndex, TRUE);
793 pAdapter->sataEvents[channelIndex] = SATA_EVENT_NO_CHANGE;
796 case SATA_EVENT_CHANNEL_DISCONNECTED:
797 /* Handle only disconnects */
800 KdPrint(("RR18xx [%d,%d]: device disconnected\n",
801 pMvSataAdapter->adapterId, channelIndex));
802 /* Flush pending commands */
803 if(pMvSataAdapter->sataChannel[channelIndex])
805 _VBUS_INST(&pAdapter->VBus)
806 mvSataFlushDmaQueue (pMvSataAdapter, channelIndex,
807 MV_FLUSH_TYPE_CALLBACK);
808 CheckPendingCall(_VBUS_P0);
809 mvSataRemoveChannel(pMvSataAdapter,channelIndex);
810 hptmv_free_channel(pAdapter, channelIndex);
811 pMvSataAdapter->sataChannel[channelIndex] = NULL;
812 KdPrint(("RR18xx [%d,%d]: channel removed\n",
813 pMvSataAdapter->adapterId, channelIndex));
814 if (pAdapter->outstandingCommands==0 && DPC_Request_Nums==0)
815 Check_Idle_Call(pAdapter);
819 KdPrint(("RR18xx [%d,%d]: channel already removed!!\n",
820 pMvSataAdapter->adapterId, channelIndex));
822 pAdapter->sataEvents[channelIndex] = SATA_EVENT_NO_CHANGE;
825 case SATA_EVENT_NO_CHANGE:
832 /* mvOsSemRelease(&pMvSataAdapter->semaphore); */
835 #define EVENT_CONNECT 1
836 #define EVENT_DISCONNECT 0
839 hptmv_handle_event_connect(void *data)
841 hptmv_handle_event (data, 0);
845 hptmv_handle_event_disconnect(void *data)
847 hptmv_handle_event (data, 1);
851 hptmv_event_notify(MV_SATA_ADAPTER *pMvSataAdapter, MV_EVENT_TYPE eventType,
852 MV_U32 param1, MV_U32 param2)
854 IAL_ADAPTER_T *pAdapter = pMvSataAdapter->IALData;
858 case MV_EVENT_TYPE_SATA_CABLE:
860 MV_U8 channel = param2;
862 if (param1 == EVENT_CONNECT)
864 pAdapter->sataEvents[channel] = SATA_EVENT_CHANNEL_CONNECTED;
865 KdPrint(("RR18xx [%d,%d]: device connected event received\n",
866 pMvSataAdapter->adapterId, channel));
867 /* Delete previous timers (if multiple drives connected in the same time */
868 callout_reset(&pAdapter->event_timer_connect, 10*hz, hptmv_handle_event_connect, pAdapter);
870 else if (param1 == EVENT_DISCONNECT)
872 pAdapter->sataEvents[channel] = SATA_EVENT_CHANNEL_DISCONNECTED;
873 KdPrint(("RR18xx [%d,%d]: device disconnected event received \n",
874 pMvSataAdapter->adapterId, channel));
875 device_change(pAdapter, channel, FALSE);
876 /* Delete previous timers (if multiple drives disconnected in the same time */
877 /* callout_reset(&pAdapter->event_timer_disconnect, 10*hz, hptmv_handle_event_disconnect, pAdapter); */
878 /*It is not necessary to wait, handle it directly*/
879 hptmv_handle_event_disconnect(pAdapter);
884 MV_ERROR("RR18xx: illigal value for param1(%d) at "
885 "connect/disconect event, host=%d\n", param1,
886 pMvSataAdapter->adapterId );
891 case MV_EVENT_TYPE_ADAPTER_ERROR:
892 KdPrint(("RR18xx: DEVICE error event received, pci cause "
893 "reg=%x, don't how to handle this\n", param1));
896 MV_ERROR("RR18xx[%d]: unknown event type (%d)\n",
897 pMvSataAdapter->adapterId, eventType);
904 hptmv_allocate_edma_queues(IAL_ADAPTER_T *pAdapter)
906 pAdapter->requestsArrayBaseAddr = (MV_U8 *)contigmalloc(REQUESTS_ARRAY_SIZE,
907 M_DEVBUF, M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0ul);
908 if (pAdapter->requestsArrayBaseAddr == NULL)
910 MV_ERROR("RR18xx[%d]: Failed to allocate memory for EDMA request"
911 " queues\n", pAdapter->mvSataAdapter.adapterId);
914 pAdapter->requestsArrayBaseDmaAddr = fOsPhysicalAddress(pAdapter->requestsArrayBaseAddr);
915 pAdapter->requestsArrayBaseAlignedAddr = pAdapter->requestsArrayBaseAddr;
916 pAdapter->requestsArrayBaseAlignedAddr += MV_EDMA_REQUEST_QUEUE_SIZE;
917 pAdapter->requestsArrayBaseAlignedAddr = (MV_U8 *)
918 (((ULONG_PTR)pAdapter->requestsArrayBaseAlignedAddr) & ~(ULONG_PTR)(MV_EDMA_REQUEST_QUEUE_SIZE - 1));
919 pAdapter->requestsArrayBaseDmaAlignedAddr = pAdapter->requestsArrayBaseDmaAddr;
920 pAdapter->requestsArrayBaseDmaAlignedAddr += MV_EDMA_REQUEST_QUEUE_SIZE;
921 pAdapter->requestsArrayBaseDmaAlignedAddr &= ~(ULONG_PTR)(MV_EDMA_REQUEST_QUEUE_SIZE - 1);
923 if ((pAdapter->requestsArrayBaseDmaAlignedAddr - pAdapter->requestsArrayBaseDmaAddr) !=
924 (pAdapter->requestsArrayBaseAlignedAddr - pAdapter->requestsArrayBaseAddr))
926 MV_ERROR("RR18xx[%d]: Error in Request Quueues Alignment\n",
927 pAdapter->mvSataAdapter.adapterId);
928 contigfree(pAdapter->requestsArrayBaseAddr, REQUESTS_ARRAY_SIZE, M_DEVBUF);
931 /* response queues */
932 pAdapter->responsesArrayBaseAddr = (MV_U8 *)contigmalloc(RESPONSES_ARRAY_SIZE,
933 M_DEVBUF, M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0ul);
934 if (pAdapter->responsesArrayBaseAddr == NULL)
936 MV_ERROR("RR18xx[%d]: Failed to allocate memory for EDMA response"
937 " queues\n", pAdapter->mvSataAdapter.adapterId);
938 contigfree(pAdapter->requestsArrayBaseAddr, RESPONSES_ARRAY_SIZE, M_DEVBUF);
941 pAdapter->responsesArrayBaseDmaAddr = fOsPhysicalAddress(pAdapter->responsesArrayBaseAddr);
942 pAdapter->responsesArrayBaseAlignedAddr = pAdapter->responsesArrayBaseAddr;
943 pAdapter->responsesArrayBaseAlignedAddr += MV_EDMA_RESPONSE_QUEUE_SIZE;
944 pAdapter->responsesArrayBaseAlignedAddr = (MV_U8 *)
945 (((ULONG_PTR)pAdapter->responsesArrayBaseAlignedAddr) & ~(ULONG_PTR)(MV_EDMA_RESPONSE_QUEUE_SIZE - 1));
946 pAdapter->responsesArrayBaseDmaAlignedAddr = pAdapter->responsesArrayBaseDmaAddr;
947 pAdapter->responsesArrayBaseDmaAlignedAddr += MV_EDMA_RESPONSE_QUEUE_SIZE;
948 pAdapter->responsesArrayBaseDmaAlignedAddr &= ~(ULONG_PTR)(MV_EDMA_RESPONSE_QUEUE_SIZE - 1);
950 if ((pAdapter->responsesArrayBaseDmaAlignedAddr - pAdapter->responsesArrayBaseDmaAddr) !=
951 (pAdapter->responsesArrayBaseAlignedAddr - pAdapter->responsesArrayBaseAddr))
953 MV_ERROR("RR18xx[%d]: Error in Response Quueues Alignment\n",
954 pAdapter->mvSataAdapter.adapterId);
955 contigfree(pAdapter->requestsArrayBaseAddr, REQUESTS_ARRAY_SIZE, M_DEVBUF);
956 contigfree(pAdapter->responsesArrayBaseAddr, RESPONSES_ARRAY_SIZE, M_DEVBUF);
963 hptmv_free_edma_queues(IAL_ADAPTER_T *pAdapter)
965 contigfree(pAdapter->requestsArrayBaseAddr, REQUESTS_ARRAY_SIZE, M_DEVBUF);
966 contigfree(pAdapter->responsesArrayBaseAddr, RESPONSES_ARRAY_SIZE, M_DEVBUF);
970 AllocatePRDTable(IAL_ADAPTER_T *pAdapter)
973 if (pAdapter->pFreePRDLink) {
974 KdPrint(("pAdapter->pFreePRDLink:%p\n",pAdapter->pFreePRDLink));
975 ret = pAdapter->pFreePRDLink;
976 pAdapter->pFreePRDLink = *(void**)ret;
983 FreePRDTable(IAL_ADAPTER_T *pAdapter, PVOID PRDTable)
985 *(void**)PRDTable = pAdapter->pFreePRDLink;
986 pAdapter->pFreePRDLink = PRDTable;
989 extern PVDevice fGetFirstChild(PVDevice pLogical);
990 extern void fResetBootMark(PVDevice pLogical);
992 fRegisterVdevice(IAL_ADAPTER_T *pAdapter)
994 PVDevice pPhysical, pLogical;
998 for(i=0;i<MV_SATA_CHANNELS_NUM;i++) {
999 pPhysical = &(pAdapter->VDevices[i]);
1000 pLogical = pPhysical;
1001 while (pLogical->pParent) pLogical = pLogical->pParent;
1002 if (pLogical->vf_online==0) {
1003 pPhysical->vf_bootmark = pLogical->vf_bootmark = 0;
1006 if (pLogical->VDeviceType==VD_SPARE || pPhysical!=fGetFirstChild(pLogical))
1009 pVBus = &pAdapter->VBus;
1013 while(j<MAX_VDEVICE_PER_VBUS && pVBus->pVDevice[j]) j++;
1014 if(j<MAX_VDEVICE_PER_VBUS){
1015 pVBus->pVDevice[j] = pLogical;
1016 pLogical->pVBus = pVBus;
1018 if (j>0 && pLogical->vf_bootmark) {
1019 if (pVBus->pVDevice[0]->vf_bootmark) {
1020 fResetBootMark(pLogical);
1023 do { pVBus->pVDevice[j] = pVBus->pVDevice[j-1]; } while (--j);
1024 pVBus->pVDevice[0] = pLogical;
1033 GetSpareDisk(_VBUS_ARG PVDevice pArray)
1035 IAL_ADAPTER_T *pAdapter = (IAL_ADAPTER_T *)pArray->pVBus->OsExt;
1036 LBA_T capacity = LongDiv(pArray->VDeviceCapacity, pArray->u.array.bArnMember-1);
1037 LBA_T thiscap, maxcap = MAX_LBA_T;
1038 PVDevice pVDevice, pFind = NULL;
1041 for(i=0;i<MV_SATA_CHANNELS_NUM;i++)
1043 pVDevice = &pAdapter->VDevices[i];
1046 thiscap = pArray->vf_format_v2? pVDevice->u.disk.dDeRealCapacity : pVDevice->VDeviceCapacity;
1047 /* find the smallest usable spare disk */
1048 if (pVDevice->VDeviceType==VD_SPARE &&
1049 pVDevice->u.disk.df_on_line &&
1051 thiscap >= capacity)
1053 maxcap = pVDevice->VDeviceCapacity;
1060 /******************************************************************
1062 *******************************************************************/
1064 fDeReadWrite(PDevice pDev, ULONG Lba, UCHAR Cmd, void *tmpBuffer)
1066 return mvReadWrite(pDev->mv, Lba, Cmd, tmpBuffer);
1069 void HPTLIBAPI fDeSelectMode(PDevice pDev, UCHAR NewMode)
1071 MV_SATA_CHANNEL *pSataChannel = pDev->mv;
1072 MV_SATA_ADAPTER *pSataAdapter = pSataChannel->mvSataAdapter;
1073 MV_U8 channelIndex = pSataChannel->channelNumber;
1075 /* 508x don't use MW-DMA? */
1076 if (NewMode>4 && NewMode<8) NewMode = 4;
1077 pDev->bDeModeSetting = NewMode;
1079 mvMode = MV_ATA_TRANSFER_PIO_0 + NewMode;
1081 mvMode = MV_ATA_TRANSFER_UDMA_0 + (NewMode-8);
1083 /*To fix 88i8030 bug*/
1084 if (mvMode > MV_ATA_TRANSFER_UDMA_0 && mvMode < MV_ATA_TRANSFER_UDMA_4)
1085 mvMode = MV_ATA_TRANSFER_UDMA_0;
1087 mvSataDisableChannelDma(pSataAdapter, channelIndex);
1088 /* Flush pending commands */
1089 mvSataFlushDmaQueue (pSataAdapter, channelIndex, MV_FLUSH_TYPE_NONE);
1091 if (mvStorageDevATASetFeatures(pSataAdapter, channelIndex,
1092 MV_ATA_SET_FEATURES_TRANSFER,
1093 mvMode, 0, 0, 0) == MV_FALSE)
1095 KdPrint(("channel %d: Set Features failed\n", channelIndex));
1098 if (mvSataEnableChannelDma(pSataAdapter, channelIndex) == MV_FALSE)
1099 KdPrint(("Failed to enable DMA, channel=%d", channelIndex));
1102 int HPTLIBAPI fDeSetTCQ(PDevice pDev, int enable, int depth)
1104 MV_SATA_CHANNEL *pSataChannel = pDev->mv;
1105 MV_SATA_ADAPTER *pSataAdapter = pSataChannel->mvSataAdapter;
1106 MV_U8 channelIndex = pSataChannel->channelNumber;
1107 IAL_ADAPTER_T *pAdapter = pSataAdapter->IALData;
1108 MV_CHANNEL *channelInfo = &(pAdapter->mvChannel[channelIndex]);
1109 int dmaActive = pSataChannel->queueCommandsEnabled;
1113 mvSataDisableChannelDma(pSataAdapter, channelIndex);
1114 mvSataFlushDmaQueue(pSataAdapter,channelIndex,MV_FLUSH_TYPE_CALLBACK);
1118 if (pSataChannel->queuedDMA == MV_EDMA_MODE_NOT_QUEUED &&
1119 (pSataChannel->identifyDevice[IDEN_SUPPORTED_COMMANDS2] & (0x2))) {
1120 UCHAR depth = ((pSataChannel->identifyDevice[IDEN_QUEUE_DEPTH]) & 0x1f) + 1;
1121 channelInfo->queueDepth = (depth==32)? 31 : depth;
1122 mvSataConfigEdmaMode(pSataAdapter, channelIndex, MV_EDMA_MODE_QUEUED, depth);
1128 if (pSataChannel->queuedDMA != MV_EDMA_MODE_NOT_QUEUED) {
1129 channelInfo->queueDepth = 2;
1130 mvSataConfigEdmaMode(pSataAdapter, channelIndex, MV_EDMA_MODE_NOT_QUEUED, 0);
1136 mvSataEnableChannelDma(pSataAdapter,channelIndex);
1140 int HPTLIBAPI fDeSetNCQ(PDevice pDev, int enable, int depth)
1145 int HPTLIBAPI fDeSetWriteCache(PDevice pDev, int enable)
1147 MV_SATA_CHANNEL *pSataChannel = pDev->mv;
1148 MV_SATA_ADAPTER *pSataAdapter = pSataChannel->mvSataAdapter;
1149 MV_U8 channelIndex = pSataChannel->channelNumber;
1150 IAL_ADAPTER_T *pAdapter = pSataAdapter->IALData;
1151 MV_CHANNEL *channelInfo = &(pAdapter->mvChannel[channelIndex]);
1152 int dmaActive = pSataChannel->queueCommandsEnabled;
1156 mvSataDisableChannelDma(pSataAdapter, channelIndex);
1157 mvSataFlushDmaQueue(pSataAdapter,channelIndex,MV_FLUSH_TYPE_CALLBACK);
1160 if ((pSataChannel->identifyDevice[82] & (0x20))) {
1162 if (mvStorageDevATASetFeatures(pSataAdapter, channelIndex,
1163 MV_ATA_SET_FEATURES_ENABLE_WCACHE, 0, 0, 0, 0))
1165 channelInfo->writeCacheEnabled = MV_TRUE;
1170 if (mvStorageDevATASetFeatures(pSataAdapter, channelIndex,
1171 MV_ATA_SET_FEATURES_DISABLE_WCACHE, 0, 0, 0, 0))
1173 channelInfo->writeCacheEnabled = MV_FALSE;
1180 mvSataEnableChannelDma(pSataAdapter,channelIndex);
1184 int HPTLIBAPI fDeSetReadAhead(PDevice pDev, int enable)
1186 MV_SATA_CHANNEL *pSataChannel = pDev->mv;
1187 MV_SATA_ADAPTER *pSataAdapter = pSataChannel->mvSataAdapter;
1188 MV_U8 channelIndex = pSataChannel->channelNumber;
1189 IAL_ADAPTER_T *pAdapter = pSataAdapter->IALData;
1190 MV_CHANNEL *channelInfo = &(pAdapter->mvChannel[channelIndex]);
1191 int dmaActive = pSataChannel->queueCommandsEnabled;
1195 mvSataDisableChannelDma(pSataAdapter, channelIndex);
1196 mvSataFlushDmaQueue(pSataAdapter,channelIndex,MV_FLUSH_TYPE_CALLBACK);
1199 if ((pSataChannel->identifyDevice[82] & (0x40))) {
1201 if (mvStorageDevATASetFeatures(pSataAdapter, channelIndex,
1202 MV_ATA_SET_FEATURES_ENABLE_RLA, 0, 0, 0, 0))
1204 channelInfo->readAheadEnabled = MV_TRUE;
1209 if (mvStorageDevATASetFeatures(pSataAdapter, channelIndex,
1210 MV_ATA_SET_FEATURES_DISABLE_RLA, 0, 0, 0, 0))
1212 channelInfo->readAheadEnabled = MV_FALSE;
1219 mvSataEnableChannelDma(pSataAdapter,channelIndex);
1223 #ifdef SUPPORT_ARRAY
1224 #define IdeRegisterVDevice fCheckArray
1227 IdeRegisterVDevice(PDevice pDev)
1229 PVDevice pVDev = Map2pVDevice(pDev);
1231 pVDev->VDeviceType = pDev->df_atapi? VD_ATAPI :
1232 pDev->df_removable_drive? VD_REMOVABLE : VD_SINGLE_DISK;
1233 pVDev->vf_online = 1;
1234 pVDev->VDeviceCapacity = pDev->dDeRealCapacity;
1235 pVDev->pfnSendCommand = pfnSendCommand[pVDev->VDeviceType];
1236 pVDev->pfnDeviceFailed = pfnDeviceFailed[pVDev->VDeviceType];
1240 static __inline PBUS_DMAMAP
1241 dmamap_get(struct IALAdapter * pAdapter)
1243 PBUS_DMAMAP p = pAdapter->pbus_dmamap_list;
1245 pAdapter->pbus_dmamap_list = p-> next;
1249 static __inline void
1250 dmamap_put(PBUS_DMAMAP p)
1252 p->next = p->pAdapter->pbus_dmamap_list;
1253 p->pAdapter->pbus_dmamap_list = p;
1256 /*Since mtx not provide the initialize when declare, so we Final init here to initialize the global mtx*/
1257 #define override_kernel_driver()
1259 static void hpt_init(void *dummy)
1261 override_kernel_driver();
1262 lockinit(&driver_lock, "hptsleeplock", 0, LK_CANRECURSE);
1264 SYSINIT(hptinit, SI_SUB_CONFIGURE, SI_ORDER_FIRST, hpt_init, NULL);
1266 static int num_adapters = 0;
1268 init_adapter(IAL_ADAPTER_T *pAdapter)
1270 PVBus _vbus_p = &pAdapter->VBus;
1271 MV_SATA_ADAPTER *pMvSataAdapter;
1272 int i, channel, rid;
1280 if(gIal_Adapter == NULL){
1281 gIal_Adapter = pAdapter;
1282 pCurAdapter = gIal_Adapter;
1285 pCurAdapter->next = pAdapter;
1286 pCurAdapter = pAdapter;
1289 pAdapter->outstandingCommands = 0;
1291 pMvSataAdapter = &(pAdapter->mvSataAdapter);
1292 _vbus_p->OsExt = (void *)pAdapter;
1293 pMvSataAdapter->IALData = pAdapter;
1295 if (bus_dma_tag_create(NULL,/* parent */
1297 BUS_SPACE_MAXADDR_32BIT+1, /* boundary */
1298 BUS_SPACE_MAXADDR, /* lowaddr */
1299 BUS_SPACE_MAXADDR, /* highaddr */
1300 PAGE_SIZE * (MAX_SG_DESCRIPTORS-1), /* maxsize */
1301 MAX_SG_DESCRIPTORS, /* nsegments */
1302 0x10000, /* maxsegsize */
1303 BUS_DMA_WAITOK, /* flags */
1304 &pAdapter->io_dma_parent /* tag */))
1310 if (hptmv_allocate_edma_queues(pAdapter))
1312 MV_ERROR("RR18xx: Failed to allocate memory for EDMA queues\n");
1317 /* also map EPROM address */
1319 if (!(pAdapter->mem_res = bus_alloc_resource(pAdapter->hpt_dev, SYS_RES_MEMORY, &rid,
1320 0, ~0, MV_SATA_PCI_BAR0_SPACE_SIZE+0x40000, RF_ACTIVE))
1322 !(pMvSataAdapter->adapterIoBaseAddress = rman_get_virtual(pAdapter->mem_res)))
1324 MV_ERROR("RR18xx: Failed to remap memory space\n");
1325 hptmv_free_edma_queues(pAdapter);
1331 KdPrint(("RR18xx: io base address 0x%p\n", pMvSataAdapter->adapterIoBaseAddress));
1334 pMvSataAdapter->adapterId = num_adapters++;
1335 /* get the revision ID */
1336 pMvSataAdapter->pciConfigRevisionId = pci_read_config(pAdapter->hpt_dev, PCIR_REVID, 1);
1337 pMvSataAdapter->pciConfigDeviceId = pci_get_device(pAdapter->hpt_dev);
1340 pMvSataAdapter->intCoalThre[0]= 1;
1341 pMvSataAdapter->intCoalThre[1]= 1;
1342 pMvSataAdapter->intTimeThre[0] = 1;
1343 pMvSataAdapter->intTimeThre[1] = 1;
1344 pMvSataAdapter->pciCommand = 0x0107E371;
1345 pMvSataAdapter->pciSerrMask = 0xd77fe6ul;
1346 pMvSataAdapter->pciInterruptMask = 0xd77fe6ul;
1347 pMvSataAdapter->mvSataEventNotify = hptmv_event_notify;
1349 if (mvSataInitAdapter(pMvSataAdapter) == MV_FALSE)
1351 MV_ERROR("RR18xx[%d]: core failed to initialize the adapter\n",
1352 pMvSataAdapter->adapterId);
1354 bus_release_resource(pAdapter->hpt_dev, SYS_RES_MEMORY, rid, pAdapter->mem_res);
1355 hptmv_free_edma_queues(pAdapter);
1359 pAdapter->ver_601 = pMvSataAdapter->pcbVersion;
1362 set_fail_leds(pMvSataAdapter, 0);
1365 /* setup command blocks */
1366 KdPrint(("Allocate command blocks\n"));
1367 _vbus_(pFreeCommands) = NULL;
1368 pAdapter->pCommandBlocks =
1369 kmalloc(sizeof(struct _Command) * MAX_COMMAND_BLOCKS_FOR_EACH_VBUS, M_DEVBUF, M_NOWAIT);
1370 KdPrint(("pCommandBlocks:%p\n",pAdapter->pCommandBlocks));
1371 if (!pAdapter->pCommandBlocks) {
1372 MV_ERROR("insufficient memory\n");
1376 for (i=0; i<MAX_COMMAND_BLOCKS_FOR_EACH_VBUS; i++) {
1377 FreeCommand(_VBUS_P &(pAdapter->pCommandBlocks[i]));
1380 /*Set up the bus_dmamap*/
1381 pAdapter->pbus_dmamap = (PBUS_DMAMAP)kmalloc (sizeof(struct _BUS_DMAMAP) * MAX_QUEUE_COMM, M_DEVBUF, M_NOWAIT);
1382 if(!pAdapter->pbus_dmamap) {
1383 MV_ERROR("insufficient memory\n");
1384 kfree(pAdapter->pCommandBlocks, M_DEVBUF);
1388 memset((void *)pAdapter->pbus_dmamap, 0, sizeof(struct _BUS_DMAMAP) * MAX_QUEUE_COMM);
1389 pAdapter->pbus_dmamap_list = 0;
1390 for (i=0; i < MAX_QUEUE_COMM; i++) {
1391 PBUS_DMAMAP pmap = &(pAdapter->pbus_dmamap[i]);
1392 pmap->pAdapter = pAdapter;
1395 if(bus_dmamap_create(pAdapter->io_dma_parent, 0, &pmap->dma_map)) {
1396 MV_ERROR("Can not allocate dma map\n");
1397 kfree(pAdapter->pCommandBlocks, M_DEVBUF);
1398 kfree(pAdapter->pbus_dmamap, M_DEVBUF);
1402 /* setup PRD Tables */
1403 KdPrint(("Allocate PRD Tables\n"));
1404 pAdapter->pFreePRDLink = 0;
1406 pAdapter->prdTableAddr = (PUCHAR)contigmalloc(
1407 (PRD_ENTRIES_SIZE*PRD_TABLES_FOR_VBUS + 32), M_DEVBUF, M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0ul);
1409 KdPrint(("prdTableAddr:%p\n",pAdapter->prdTableAddr));
1410 if (!pAdapter->prdTableAddr) {
1411 MV_ERROR("insufficient PRD Tables\n");
1414 pAdapter->prdTableAlignedAddr = (PUCHAR)(((ULONG_PTR)pAdapter->prdTableAddr + 0x1f) & ~(ULONG_PTR)0x1fL);
1416 PUCHAR PRDTable = pAdapter->prdTableAlignedAddr;
1417 for (i=0; i<PRD_TABLES_FOR_VBUS; i++)
1419 /* KdPrint(("i=%d,pAdapter->pFreePRDLink=%p\n",i,pAdapter->pFreePRDLink)); */
1420 FreePRDTable(pAdapter, PRDTable);
1421 PRDTable += PRD_ENTRIES_SIZE;
1425 /* enable the adapter interrupts */
1427 /* configure and start the connected channels*/
1428 for (channel = 0; channel < MV_SATA_CHANNELS_NUM; channel++)
1430 pAdapter->mvChannel[channel].online = MV_FALSE;
1431 if (mvSataIsStorageDeviceConnected(pMvSataAdapter, channel)
1434 KdPrint(("RR18xx[%d]: channel %d is connected\n",
1435 pMvSataAdapter->adapterId, channel));
1437 if (hptmv_init_channel(pAdapter, channel) == 0)
1439 if (mvSataConfigureChannel(pMvSataAdapter, channel) == MV_FALSE)
1441 MV_ERROR("RR18xx[%d]: Failed to configure channel"
1442 " %d\n",pMvSataAdapter->adapterId, channel);
1443 hptmv_free_channel(pAdapter, channel);
1447 if (start_channel(pAdapter, channel))
1449 MV_ERROR("RR18xx[%d]: Failed to start channel,"
1450 " channel=%d\n",pMvSataAdapter->adapterId,
1452 hptmv_free_channel(pAdapter, channel);
1454 pAdapter->mvChannel[channel].online = MV_TRUE;
1455 /* mvSataChannelSetEdmaLoopBackMode(pMvSataAdapter,
1461 KdPrint(("pAdapter->mvChannel[channel].online:%x, channel:%d\n",
1462 pAdapter->mvChannel[channel].online, channel));
1465 #ifdef SUPPORT_ARRAY
1466 for(i = MAX_ARRAY_DEVICE - 1; i >= 0; i--) {
1467 pVDev = ArrayTables(i);
1468 mArFreeArrayTable(pVDev);
1472 KdPrint(("Initialize Devices\n"));
1473 for (channel = 0; channel < MV_SATA_CHANNELS_NUM; channel++) {
1474 MV_SATA_CHANNEL *pMvSataChannel = pMvSataAdapter->sataChannel[channel];
1475 if (pMvSataChannel) {
1476 init_vdev_params(pAdapter, channel);
1477 IdeRegisterVDevice(&pAdapter->VDevices[channel].u.disk);
1480 #ifdef SUPPORT_ARRAY
1481 CheckArrayCritical(_VBUS_P0);
1483 _vbus_p->nInstances = 1;
1484 fRegisterVdevice(pAdapter);
1486 for (channel=0;channel<MV_SATA_CHANNELS_NUM;channel++) {
1487 pVDev = _vbus_p->pVDevice[channel];
1488 if (pVDev && pVDev->vf_online)
1489 fCheckBootable(pVDev);
1492 #if defined(SUPPORT_ARRAY) && defined(_RAID5N_)
1493 init_raid5_memory(_VBUS_P0);
1494 _vbus_(r5).enable_write_back = 1;
1495 kprintf("RR18xx: RAID5 write-back %s\n", _vbus_(r5).enable_write_back? "enabled" : "disabled");
1498 mvSataUnmaskAdapterInterrupt(pMvSataAdapter);
1504 MvSataResetChannel(MV_SATA_ADAPTER *pMvSataAdapter, MV_U8 channel)
1506 IAL_ADAPTER_T *pAdapter = (IAL_ADAPTER_T *)pMvSataAdapter->IALData;
1508 mvSataDisableChannelDma(pMvSataAdapter, channel);
1509 /* Flush pending commands */
1510 mvSataFlushDmaQueue (pMvSataAdapter, channel, MV_FLUSH_TYPE_CALLBACK);
1512 /* Software reset channel */
1513 if (mvStorageDevATASoftResetDevice(pMvSataAdapter, channel) == MV_FALSE)
1515 MV_ERROR("RR18xx [%d,%d]: failed to perform Software reset\n",
1516 pMvSataAdapter->adapterId, channel);
1517 hptmv_free_channel(pAdapter, channel);
1521 /* Hardware reset channel */
1522 if (mvSataChannelHardReset(pMvSataAdapter, channel)== MV_FALSE)
1524 MV_ERROR("RR18xx [%d,%d] Failed to Hard reser the SATA channel\n",
1525 pMvSataAdapter->adapterId, channel);
1526 hptmv_free_channel(pAdapter, channel);
1530 if (mvSataIsStorageDeviceConnected(pMvSataAdapter, channel) == MV_FALSE)
1532 MV_ERROR("RR18xx [%d,%d] Failed to Connect Device\n",
1533 pMvSataAdapter->adapterId, channel);
1534 hptmv_free_channel(pAdapter, channel);
1538 MV_ERROR("channel %d: perform recalibrate command", channel);
1539 if (!mvStorageDevATAExecuteNonUDMACommand(pMvSataAdapter, channel,
1540 MV_NON_UDMA_PROTOCOL_NON_DATA,
1554 MV_ERROR("channel %d: recalibrate failed", channel);
1556 /* Set transfer mode */
1557 if((mvStorageDevATASetFeatures(pMvSataAdapter, channel,
1558 MV_ATA_SET_FEATURES_TRANSFER,
1559 MV_ATA_TRANSFER_PIO_SLOW, 0, 0, 0) == MV_FALSE) ||
1560 (mvStorageDevATASetFeatures(pMvSataAdapter, channel,
1561 MV_ATA_SET_FEATURES_TRANSFER,
1562 pAdapter->mvChannel[channel].maxPioModeSupported, 0, 0, 0) == MV_FALSE) ||
1563 (mvStorageDevATASetFeatures(pMvSataAdapter, channel,
1564 MV_ATA_SET_FEATURES_TRANSFER,
1565 pAdapter->mvChannel[channel].maxUltraDmaModeSupported, 0, 0, 0) == MV_FALSE) )
1567 MV_ERROR("channel %d: Set Features failed", channel);
1568 hptmv_free_channel(pAdapter, channel);
1572 if (mvSataEnableChannelDma(pMvSataAdapter, channel) == MV_FALSE)
1574 MV_ERROR("Failed to enable DMA, channel=%d", channel);
1575 hptmv_free_channel(pAdapter, channel);
1583 fResetActiveCommands(PVBus _vbus_p)
1585 MV_SATA_ADAPTER *pMvSataAdapter = &((IAL_ADAPTER_T *)_vbus_p->OsExt)->mvSataAdapter;
1587 for (channel=0;channel< MV_SATA_CHANNELS_NUM;channel++) {
1588 if (pMvSataAdapter->sataChannel[channel] && pMvSataAdapter->sataChannel[channel]->outstandingCommands)
1589 MvSataResetChannel(pMvSataAdapter,channel);
1594 void fCompleteAllCommandsSynchronously(PVBus _vbus_p)
1599 MV_SATA_ADAPTER *pMvSataAdapter = &((IAL_ADAPTER_T *)_vbus_p->OsExt)->mvSataAdapter;
1600 MV_SATA_CHANNEL *pMvSataChannel;
1605 CheckPendingCall(_VBUS_P0);
1610 for (channel=0;channel< MV_SATA_CHANNELS_NUM;channel++) {
1611 pMvSataChannel = pMvSataAdapter->sataChannel[channel];
1612 if (pMvSataChannel && pMvSataChannel->outstandingCommands)
1614 while (pMvSataChannel->outstandingCommands) {
1615 if (!mvSataInterruptServiceRoutine(pMvSataAdapter)) {
1617 if (ticks++ > 3000) {
1618 MvSataResetChannel(pMvSataAdapter,channel);
1632 fResetVBus(_VBUS_ARG0)
1634 KdPrint(("fMvResetBus(%p)", _vbus_p));
1636 /* some commands may already finished. */
1637 CheckPendingCall(_VBUS_P0);
1639 fResetActiveCommands(_vbus_p);
1641 * the other pending commands may still be finished successfully.
1643 fCompleteAllCommandsSynchronously(_vbus_p);
1645 /* Now there should be no pending commands. No more action needed. */
1646 CheckIdleCall(_VBUS_P0);
1648 KdPrint(("fMvResetBus() done"));
1651 /*No rescan function*/
1653 fRescanAllDevice(_VBUS_ARG0)
1658 CommandCompletionCB(MV_SATA_ADAPTER *pMvSataAdapter,
1660 MV_COMPLETION_TYPE comp_type,
1661 MV_VOID_PTR commandId,
1662 MV_U16 responseFlags,
1664 MV_STORAGE_DEVICE_REGISTERS *registerStruct)
1666 PCommand pCmd = (PCommand) commandId;
1667 _VBUS_INST(pCmd->pVDevice->pVBus)
1669 if (pCmd->uScratch.sata_param.prdAddr)
1670 FreePRDTable(pMvSataAdapter->IALData,pCmd->uScratch.sata_param.prdAddr);
1674 case MV_COMPLETION_TYPE_NORMAL:
1675 pCmd->Result = RETURN_SUCCESS;
1677 case MV_COMPLETION_TYPE_ABORT:
1678 pCmd->Result = RETURN_BUS_RESET;
1680 case MV_COMPLETION_TYPE_ERROR:
1681 MV_ERROR("IAL: COMPLETION ERROR, adapter %d, channel %d, flags=%x\n",
1682 pMvSataAdapter->adapterId, channelNum, responseFlags);
1684 if (responseFlags & 4) {
1685 MV_ERROR("ATA regs: error %x, sector count %x, LBA low %x, LBA mid %x,"
1686 " LBA high %x, device %x, status %x\n",
1687 registerStruct->errorRegister,
1688 registerStruct->sectorCountRegister,
1689 registerStruct->lbaLowRegister,
1690 registerStruct->lbaMidRegister,
1691 registerStruct->lbaHighRegister,
1692 registerStruct->deviceRegister,
1693 registerStruct->statusRegister);
1695 /*We can't do handleEdmaError directly here, because CommandCompletionCB is called by
1696 * mv's ISR, if we retry the command, than the internel data structure may be destroyed*/
1697 pCmd->uScratch.sata_param.responseFlags = responseFlags;
1698 pCmd->uScratch.sata_param.bIdeStatus = registerStruct->statusRegister;
1699 pCmd->uScratch.sata_param.errorRegister = registerStruct->errorRegister;
1700 pCmd->pVDevice->u.disk.QueueLength--;
1701 CallAfterReturn(_VBUS_P (DPC_PROC)handleEdmaError,pCmd);
1705 MV_ERROR(" Unknown completion type (%d)\n", comp_type);
1709 if (pCmd->uCmd.Ide.Command == IDE_COMMAND_VERIFY && pCmd->uScratch.sata_param.cmd_priv > 1) {
1710 pCmd->uScratch.sata_param.cmd_priv --;
1713 pCmd->pVDevice->u.disk.QueueLength--;
1714 CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
1719 fDeviceSendCommand(_VBUS_ARG PCommand pCmd)
1721 MV_SATA_EDMA_PRD_ENTRY *pPRDTable = NULL;
1722 MV_SATA_ADAPTER *pMvSataAdapter;
1723 MV_SATA_CHANNEL *pMvSataChannel;
1724 PVDevice pVDevice = pCmd->pVDevice;
1725 PDevice pDevice = &pVDevice->u.disk;
1726 LBA_T Lba = pCmd->uCmd.Ide.Lba;
1727 USHORT nSector = pCmd->uCmd.Ide.nSectors;
1729 MV_QUEUE_COMMAND_RESULT result;
1730 MV_QUEUE_COMMAND_INFO commandInfo;
1731 MV_UDMA_COMMAND_PARAMS *pUdmaParams = &commandInfo.commandParams.udmaCommand;
1732 MV_NONE_UDMA_COMMAND_PARAMS *pNoUdmaParams = &commandInfo.commandParams.NoneUdmaCommand;
1738 DECLARE_BUFFER(FPSCAT_GATH, tmpSg);
1740 if (!pDevice->df_on_line) {
1741 MV_ERROR("Device is offline");
1742 pCmd->Result = RETURN_BAD_DEVICE;
1743 CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
1747 pDevice->HeadPosition = pCmd->uCmd.Ide.Lba + pCmd->uCmd.Ide.nSectors;
1748 pMvSataChannel = pDevice->mv;
1749 pMvSataAdapter = pMvSataChannel->mvSataAdapter;
1750 channel = pMvSataChannel->channelNumber;
1752 /* old RAID0 has hidden lba. Remember to clear dDeHiddenLba when delete array! */
1753 Lba += pDevice->dDeHiddenLba;
1755 if (Lba+nSector-1 > pDevice->dDeRealCapacity) {
1756 pCmd->Result = RETURN_INVALID_REQUEST;
1757 CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
1762 * always use 48bit LBA if drive supports it.
1763 * Some Seagate drives report error if you use a 28-bit command
1764 * to access sector 0xfffffff.
1766 is48bit = pMvSataChannel->lba48Address;
1768 switch (pCmd->uCmd.Ide.Command)
1770 case IDE_COMMAND_READ:
1771 case IDE_COMMAND_WRITE:
1772 if (pDevice->bDeModeSetting<8) goto pio;
1774 commandInfo.type = MV_QUEUED_COMMAND_TYPE_UDMA;
1775 pUdmaParams->isEXT = is48bit;
1776 pUdmaParams->numOfSectors = nSector;
1777 pUdmaParams->lowLBAAddress = Lba;
1778 pUdmaParams->highLBAAddress = 0;
1779 pUdmaParams->prdHighAddr = 0;
1780 pUdmaParams->callBack = CommandCompletionCB;
1781 pUdmaParams->commandId = (MV_VOID_PTR )pCmd;
1782 if(pCmd->uCmd.Ide.Command == IDE_COMMAND_READ)
1783 pUdmaParams->readWrite = MV_UDMA_TYPE_READ;
1785 pUdmaParams->readWrite = MV_UDMA_TYPE_WRITE;
1787 if (pCmd->pSgTable && pCmd->cf_physical_sg) {
1788 FPSCAT_GATH sg1=tmpSg, sg2=pCmd->pSgTable;
1789 do { *sg1++=*sg2; } while ((sg2++->wSgFlag & SG_FLAG_EOT)==0);
1792 if (!pCmd->pfnBuildSgl || !pCmd->pfnBuildSgl(_VBUS_P pCmd, tmpSg, 0)) {
1794 mvSataDisableChannelDma(pMvSataAdapter, channel);
1795 mvSataFlushDmaQueue(pMvSataAdapter, channel, MV_FLUSH_TYPE_CALLBACK);
1797 if (pCmd->pSgTable && pCmd->cf_physical_sg==0) {
1798 FPSCAT_GATH sg1=tmpSg, sg2=pCmd->pSgTable;
1799 do { *sg1++=*sg2; } while ((sg2++->wSgFlag & SG_FLAG_EOT)==0);
1802 if (!pCmd->pfnBuildSgl || !pCmd->pfnBuildSgl(_VBUS_P pCmd, tmpSg, 1)) {
1803 pCmd->Result = RETURN_NEED_LOGICAL_SG;
1809 ULONG size = tmpSg->wSgSize? tmpSg->wSgSize : 0x10000;
1810 ULONG_PTR addr = tmpSg->dSgAddress;
1812 pCmd->Result = RETURN_INVALID_REQUEST;
1815 if (mvStorageDevATAExecuteNonUDMACommand(pMvSataAdapter, channel,
1816 (pCmd->cf_data_out)?MV_NON_UDMA_PROTOCOL_PIO_DATA_OUT:MV_NON_UDMA_PROTOCOL_PIO_DATA_IN,
1819 size >> 1, /* count */
1820 0, /* features N/A */
1821 (MV_U16)(size>>9), /*sector count*/
1822 (MV_U16)( (is48bit? (MV_U16)((Lba >> 16) & 0xFF00) : 0 ) | (UCHAR)(Lba & 0xFF) ), /*lbalow*/
1823 (MV_U16)((Lba >> 8) & 0xFF), /* lbaMid */
1824 (MV_U16)((Lba >> 16) & 0xFF),/* lbaHigh */
1825 (MV_U8)(0x40 | (is48bit ? 0 : (UCHAR)(Lba >> 24) & 0xFF )),/* device */
1826 (MV_U8)(is48bit ? (pCmd->cf_data_in?IDE_COMMAND_READ_EXT:IDE_COMMAND_WRITE_EXT):pCmd->uCmd.Ide.Command)
1829 pCmd->Result = RETURN_IDE_ERROR;
1833 if(Lba & 0xF0000000) is48bit = MV_TRUE;
1835 while ((tmpSg++->wSgFlag & SG_FLAG_EOT)==0);
1836 pCmd->Result = RETURN_SUCCESS;
1838 mvSataEnableChannelDma(pMvSataAdapter,channel);
1839 CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
1844 pPRDTable = (MV_SATA_EDMA_PRD_ENTRY *) AllocatePRDTable(pMvSataAdapter->IALData);
1845 KdPrint(("pPRDTable:%p\n",pPRDTable));
1847 pCmd->Result = RETURN_DEVICE_BUSY;
1848 CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
1854 pPRDTable[i].highBaseAddr = (sizeof(tmpSg->dSgAddress)>4 ? (MV_U32)(tmpSg->dSgAddress>>32) : 0);
1855 pPRDTable[i].flags = (MV_U16)tmpSg->wSgFlag;
1856 pPRDTable[i].byteCount = (MV_U16)tmpSg->wSgSize;
1857 pPRDTable[i].lowBaseAddr = (MV_U32)tmpSg->dSgAddress;
1858 pPRDTable[i].reserved = 0;
1860 }while((tmpSg++->wSgFlag & SG_FLAG_EOT)==0);
1862 pUdmaParams->prdLowAddr = (ULONG)fOsPhysicalAddress(pPRDTable);
1863 if ((pUdmaParams->numOfSectors == 256) && (pMvSataChannel->lba48Address == MV_FALSE)) {
1864 pUdmaParams->numOfSectors = 0;
1867 pCmd->uScratch.sata_param.prdAddr = (PVOID)pPRDTable;
1869 result = mvSataQueueCommand(pMvSataAdapter, channel, &commandInfo);
1871 if (result != MV_QUEUE_COMMAND_RESULT_OK)
1876 case MV_QUEUE_COMMAND_RESULT_BAD_LBA_ADDRESS:
1877 MV_ERROR("IAL Error: Edma Queue command failed. Bad LBA "
1878 "LBA[31:0](0x%08x)\n", pUdmaParams->lowLBAAddress);
1879 pCmd->Result = RETURN_IDE_ERROR;
1881 case MV_QUEUE_COMMAND_RESULT_QUEUED_MODE_DISABLED:
1882 MV_ERROR("IAL Error: Edma Queue command failed. EDMA"
1883 " disabled adapter %d channel %d\n",
1884 pMvSataAdapter->adapterId, channel);
1885 mvSataEnableChannelDma(pMvSataAdapter,channel);
1886 pCmd->Result = RETURN_IDE_ERROR;
1888 case MV_QUEUE_COMMAND_RESULT_FULL:
1889 MV_ERROR("IAL Error: Edma Queue command failed. Queue is"
1890 " Full adapter %d channel %d\n",
1891 pMvSataAdapter->adapterId, channel);
1892 pCmd->Result = RETURN_DEVICE_BUSY;
1894 case MV_QUEUE_COMMAND_RESULT_BAD_PARAMS:
1895 MV_ERROR("IAL Error: Edma Queue command failed. (Bad "
1896 "Params), pMvSataAdapter: %p, pSataChannel: %p.\n",
1897 pMvSataAdapter, pMvSataAdapter->sataChannel[channel]);
1898 pCmd->Result = RETURN_IDE_ERROR;
1901 MV_ERROR("IAL Error: Bad result value (%d) from queue"
1902 " command\n", result);
1903 pCmd->Result = RETURN_IDE_ERROR;
1906 FreePRDTable(pMvSataAdapter->IALData,pPRDTable);
1907 CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
1909 pDevice->QueueLength++;
1912 case IDE_COMMAND_VERIFY:
1913 commandInfo.type = MV_QUEUED_COMMAND_TYPE_NONE_UDMA;
1914 pNoUdmaParams->bufPtr = NULL;
1915 pNoUdmaParams->callBack = CommandCompletionCB;
1916 pNoUdmaParams->commandId = (MV_VOID_PTR)pCmd;
1917 pNoUdmaParams->count = 0;
1918 pNoUdmaParams->features = 0;
1919 pNoUdmaParams->protocolType = MV_NON_UDMA_PROTOCOL_NON_DATA;
1921 pCmd->uScratch.sata_param.cmd_priv = 1;
1922 if (pMvSataChannel->lba48Address == MV_TRUE){
1923 pNoUdmaParams->command = MV_ATA_COMMAND_READ_VERIFY_SECTORS_EXT;
1924 pNoUdmaParams->isEXT = MV_TRUE;
1925 pNoUdmaParams->lbaHigh = (MV_U16)((Lba & 0xff0000) >> 16);
1926 pNoUdmaParams->lbaMid = (MV_U16)((Lba & 0xff00) >> 8);
1927 pNoUdmaParams->lbaLow =
1928 (MV_U16)(((Lba & 0xff000000) >> 16)| (Lba & 0xff));
1929 pNoUdmaParams->sectorCount = nSector;
1930 pNoUdmaParams->device = 0x40;
1931 result = mvSataQueueCommand(pMvSataAdapter, channel, &commandInfo);
1932 if (result != MV_QUEUE_COMMAND_RESULT_OK){
1938 pNoUdmaParams->command = MV_ATA_COMMAND_READ_VERIFY_SECTORS;
1939 pNoUdmaParams->isEXT = MV_FALSE;
1940 pNoUdmaParams->lbaHigh = (MV_U16)((Lba & 0xff0000) >> 16);
1941 pNoUdmaParams->lbaMid = (MV_U16)((Lba & 0xff00) >> 8);
1942 pNoUdmaParams->lbaLow = (MV_U16)(Lba & 0xff);
1943 pNoUdmaParams->sectorCount = 0xff & nSector;
1944 pNoUdmaParams->device = (MV_U8)(0x40 |
1945 ((Lba & 0xf000000) >> 24));
1946 pNoUdmaParams->callBack = CommandCompletionCB;
1947 result = mvSataQueueCommand(pMvSataAdapter, channel, &commandInfo);
1948 /*FIXME: how about the commands already queued? but marvel also forgets to consider this*/
1949 if (result != MV_QUEUE_COMMAND_RESULT_OK){
1955 pCmd->Result = RETURN_INVALID_REQUEST;
1956 CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
1961 /**********************************************************
1963 * Probe the hostadapter.
1965 **********************************************************/
1967 hpt_probe(device_t dev)
1969 if ((pci_get_vendor(dev) == MV_SATA_VENDOR_ID) &&
1970 (pci_get_device(dev) == MV_SATA_DEVICE_ID_5081
1972 || pci_get_device(dev) == MV_SATA_DEVICE_ID_5080
1976 KdPrintI((CONTROLLER_NAME " found\n"));
1977 device_set_desc(dev, CONTROLLER_NAME);
1984 /***********************************************************
1986 * Auto configuration: attach and init a host adapter.
1988 ***********************************************************/
1990 hpt_attach(device_t dev)
1992 IAL_ADAPTER_T * pAdapter = device_get_softc(dev);
1995 struct cam_devq *devq;
1996 struct cam_sim *hpt_vsim;
1998 kprintf("%s Version %s \n", DRIVER_NAME, DRIVER_VERSION);
2002 pAdapter = (IAL_ADAPTER_T *)kmalloc(sizeof (IAL_ADAPTER_T), M_DEVBUF, M_NOWAIT);
2003 device_set_softc(dev, (void *)pAdapter);
2006 if (!pAdapter) return (ENOMEM);
2007 bzero(pAdapter, sizeof(IAL_ADAPTER_T));
2009 pAdapter->hpt_dev = dev;
2011 rid = init_adapter(pAdapter);
2016 if ((pAdapter->hpt_irq = bus_alloc_resource(pAdapter->hpt_dev, SYS_RES_IRQ, &rid, 0, ~0ul, 1, RF_SHAREABLE | RF_ACTIVE)) == NULL)
2018 hpt_printk(("can't allocate interrupt\n"));
2022 if (bus_setup_intr(pAdapter->hpt_dev, pAdapter->hpt_irq, 0,
2023 hpt_intr, pAdapter, &pAdapter->hpt_intr, NULL))
2025 hpt_printk(("can't set up interrupt\n"));
2026 kfree(pAdapter, M_DEVBUF);
2030 ccb = xpt_alloc_ccb();
2031 ccb->ccb_h.pinfo.priority = 1;
2032 ccb->ccb_h.pinfo.index = CAM_UNQUEUED_INDEX;
2035 * Create the device queue for our SIM(s).
2037 if((devq = cam_simq_alloc(8/*MAX_QUEUE_COMM*/)) == NULL)
2039 KdPrint(("ENXIO\n"));
2044 * Construct our SIM entry
2046 hpt_vsim = cam_sim_alloc(hpt_action, hpt_poll, __str(PROC_DIR_NAME),
2047 pAdapter, device_get_unit(pAdapter->hpt_dev), &sim_mplock, 1, 8, devq);
2048 cam_simq_release(devq);
2049 if (hpt_vsim == NULL) {
2053 if (xpt_bus_register(hpt_vsim, 0) != CAM_SUCCESS)
2055 cam_sim_free(hpt_vsim);
2060 if(xpt_create_path(&pAdapter->path, /*periph */ NULL,
2061 cam_sim_path(hpt_vsim), CAM_TARGET_WILDCARD,
2062 CAM_LUN_WILDCARD) != CAM_REQ_CMP)
2064 xpt_bus_deregister(cam_sim_path(hpt_vsim));
2065 cam_sim_free(hpt_vsim);
2070 xpt_setup_ccb(&ccb->ccb_h, pAdapter->path, /*priority*/5);
2071 ccb->ccb_h.func_code = XPT_SASYNC_CB;
2072 ccb->csa.event_enable = AC_LOST_DEVICE;
2073 ccb->csa.callback = hpt_async;
2074 ccb->csa.callback_arg = hpt_vsim;
2076 xpt_free_ccb(&ccb->ccb_h);
2078 callout_init(&pAdapter->event_timer_connect);
2079 callout_init(&pAdapter->event_timer_disconnect);
2081 if (device_get_unit(dev) == 0) {
2082 /* Start the work thread. XXX */
2083 launch_worker_thread();
2086 * hpt_worker_thread needs to be suspended after shutdown
2087 * sync, when fs sync finished.
2089 pAdapter->eh = EVENTHANDLER_REGISTER(shutdown_post_sync,
2090 shutdown_kproc, hptdaemonproc, SHUTDOWN_PRI_FIRST);
2097 hpt_detach(device_t dev)
2103 /***************************************************************
2104 * The poll function is used to simulate the interrupt when
2105 * the interrupt subsystem is not functioning.
2107 ***************************************************************/
2109 hpt_poll(struct cam_sim *sim)
2111 hpt_intr((void *)cam_sim_softc(sim));
2114 /****************************************************************
2116 * Description: Interrupt handler.
2117 ****************************************************************/
2121 IAL_ADAPTER_T *pAdapter = (IAL_ADAPTER_T *)arg;
2124 /* KdPrintI(("----- Entering Isr() -----\n")); */
2125 if (mvSataInterruptServiceRoutine(&pAdapter->mvSataAdapter) == MV_TRUE)
2127 _VBUS_INST(&pAdapter->VBus)
2128 CheckPendingCall(_VBUS_P0);
2131 /* KdPrintI(("----- Leaving Isr() -----\n")); */
2135 /**********************************************************
2136 * Asynchronous Events
2137 *********************************************************/
2138 #if (!defined(UNREFERENCED_PARAMETER))
2139 #define UNREFERENCED_PARAMETER(x) (void)(x)
2143 hpt_async(void * callback_arg, u_int32_t code, struct cam_path * path,
2148 UNREFERENCED_PARAMETER(callback_arg);
2149 UNREFERENCED_PARAMETER(code);
2150 UNREFERENCED_PARAMETER(path);
2151 UNREFERENCED_PARAMETER(arg);
2156 FlushAdapter(IAL_ADAPTER_T *pAdapter)
2160 hpt_printk(("flush all devices\n"));
2162 /* flush all devices */
2163 for (i=0; i<MAX_VDEVICE_PER_VBUS; i++) {
2164 PVDevice pVDev = pAdapter->VBus.pVDevice[i];
2165 if(pVDev) fFlushVDev(pVDev);
2170 hpt_shutdown(device_t dev)
2172 IAL_ADAPTER_T *pAdapter;
2174 pAdapter = device_get_softc(dev);
2175 if (pAdapter == NULL)
2178 EVENTHANDLER_DEREGISTER(shutdown_post_sync, pAdapter->eh);
2179 FlushAdapter(pAdapter);
2180 /* give the flush some time to happen,
2181 *otherwise "shutdown -p now" will make file system corrupted */
2182 DELAY(1000 * 1000 * 5);
2187 Check_Idle_Call(IAL_ADAPTER_T *pAdapter)
2189 _VBUS_INST(&pAdapter->VBus)
2191 if (mWaitingForIdle(_VBUS_P0)) {
2192 CheckIdleCall(_VBUS_P0);
2193 #ifdef SUPPORT_ARRAY
2197 for(i = 0; i < MAX_ARRAY_PER_VBUS; i++){
2198 if ((pArray=ArrayTables(i))->u.array.dArStamp==0)
2200 else if (pArray->u.array.rf_auto_rebuild) {
2201 KdPrint(("auto rebuild.\n"));
2202 pArray->u.array.rf_auto_rebuild = 0;
2203 hpt_queue_dpc((HPT_DPC)hpt_rebuild_data_block, pAdapter, pArray, DUPLICATE);
2209 /* launch the awaiting commands blocked by mWaitingForIdle */
2210 while(pAdapter->pending_Q!= NULL)
2212 _VBUS_INST(&pAdapter->VBus)
2213 union ccb *ccb = (union ccb *)pAdapter->pending_Q->ccb_h.ccb_ccb_ptr;
2214 hpt_free_ccb(&pAdapter->pending_Q, ccb);
2215 CallAfterReturn(_VBUS_P (DPC_PROC)OsSendCommand, ccb);
2220 ccb_done(union ccb *ccb)
2222 PBUS_DMAMAP pmap = (PBUS_DMAMAP)ccb->ccb_adapter;
2223 IAL_ADAPTER_T * pAdapter = pmap->pAdapter;
2224 KdPrintI(("ccb_done: ccb %p status %x\n", ccb, ccb->ccb_h.status));
2229 pAdapter->outstandingCommands--;
2231 if (pAdapter->outstandingCommands == 0)
2233 if(DPC_Request_Nums == 0)
2234 Check_Idle_Call(pAdapter);
2238 /****************************************************************
2240 * Description: Process a queued command from the CAM layer.
2241 * Parameters: sim - Pointer to SIM object
2242 * ccb - Pointer to SCSI command structure.
2243 ****************************************************************/
2246 hpt_action(struct cam_sim *sim, union ccb *ccb)
2248 IAL_ADAPTER_T * pAdapter = (IAL_ADAPTER_T *) cam_sim_softc(sim);
2250 _VBUS_INST(&pAdapter->VBus)
2252 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("hpt_action\n"));
2253 KdPrint(("hpt_action(%lx,%lx{%x})\n", (u_long)sim, (u_long)ccb, ccb->ccb_h.func_code));
2255 switch (ccb->ccb_h.func_code)
2257 case XPT_SCSI_IO: /* Execute the requested I/O operation */
2259 /* ccb->ccb_h.path_id is not our bus id - don't check it */
2261 if (ccb->ccb_h.target_lun) {
2262 ccb->ccb_h.status = CAM_LUN_INVALID;
2266 if (ccb->ccb_h.target_id >= MAX_VDEVICE_PER_VBUS ||
2267 pAdapter->VBus.pVDevice[ccb->ccb_h.target_id]==0) {
2268 ccb->ccb_h.status = CAM_TID_INVALID;
2274 if (pAdapter->outstandingCommands==0 && DPC_Request_Nums==0)
2275 Check_Idle_Call(pAdapter);
2277 pmap = dmamap_get(pAdapter);
2279 ccb->ccb_adapter = pmap;
2280 memset((void *)pmap->psg, 0, sizeof(pmap->psg));
2282 if (mWaitingForIdle(_VBUS_P0))
2283 hpt_queue_ccb(&pAdapter->pending_Q, ccb);
2285 OsSendCommand(_VBUS_P ccb);
2288 /* KdPrint(("leave scsiio\n")); */
2293 KdPrint(("reset bus\n"));
2295 fResetVBus(_VBUS_P0);
2300 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
2301 case XPT_EN_LUN: /* Enable LUN as a target */
2302 case XPT_TARGET_IO: /* Execute target I/O request */
2303 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
2304 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/
2305 case XPT_ABORT: /* Abort the specified CCB */
2306 case XPT_TERM_IO: /* Terminate the I/O process */
2308 ccb->ccb_h.status = CAM_REQ_INVALID;
2312 case XPT_GET_TRAN_SETTINGS:
2313 case XPT_SET_TRAN_SETTINGS:
2315 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2319 case XPT_CALC_GEOMETRY:
2320 cam_calc_geometry(&ccb->ccg, 1);
2324 case XPT_PATH_INQ: /* Path routing inquiry */
2326 struct ccb_pathinq *cpi = &ccb->cpi;
2328 cpi->version_num = 1; /* XXX??? */
2329 cpi->hba_inquiry = PI_SDTR_ABLE;
2330 cpi->target_sprt = 0;
2331 /* Not necessary to reset bus */
2332 cpi->hba_misc = PIM_NOBUSRESET;
2333 cpi->hba_eng_cnt = 0;
2335 cpi->max_target = MAX_VDEVICE_PER_VBUS;
2337 cpi->initiator_id = MAX_VDEVICE_PER_VBUS;
2339 cpi->bus_id = cam_sim_bus(sim);
2340 cpi->base_transfer_speed = 3300;
2341 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2342 strncpy(cpi->hba_vid, "HPT ", HBA_IDLEN);
2343 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2344 cpi->unit_number = cam_sim_unit(sim);
2345 cpi->transport = XPORT_SPI;
2346 cpi->transport_version = 2;
2347 cpi->protocol = PROTO_SCSI;
2348 cpi->protocol_version = SCSI_REV_2;
2349 cpi->maxio = HPTMV_DFLTPHYS;
2350 cpi->ccb_h.status = CAM_REQ_CMP;
2356 KdPrint(("invalid cmd\n"));
2357 ccb->ccb_h.status = CAM_REQ_INVALID;
2361 /* KdPrint(("leave hpt_action..............\n")); */
2364 /* shall be called at lock_driver() */
2366 hpt_queue_ccb(union ccb **ccb_Q, union ccb *ccb)
2369 ccb->ccb_h.ccb_ccb_ptr = ccb;
2371 ccb->ccb_h.ccb_ccb_ptr = (*ccb_Q)->ccb_h.ccb_ccb_ptr;
2372 (*ccb_Q)->ccb_h.ccb_ccb_ptr = (char *)ccb;
2378 /* shall be called at lock_driver() */
2380 hpt_free_ccb(union ccb **ccb_Q, union ccb *ccb)
2386 if(ccb->ccb_h.ccb_ccb_ptr == ccb) /*it means SCpnt is the last one in CURRCMDs*/
2389 while(TempCCB->ccb_h.ccb_ccb_ptr != (char *)ccb)
2390 TempCCB = (union ccb *)TempCCB->ccb_h.ccb_ccb_ptr;
2392 TempCCB->ccb_h.ccb_ccb_ptr = ccb->ccb_h.ccb_ccb_ptr;
2399 #ifdef SUPPORT_ARRAY
2400 /***************************************************************************
2401 * Function: hpt_worker_thread
2402 * Description: Do background rebuilding. Execute in kernel thread context.
2404 ***************************************************************************/
2405 static void hpt_worker_thread(void)
2408 while (DpcQueue_First!=DpcQueue_Last) {
2411 p = DpcQueue[DpcQueue_First];
2413 DpcQueue_First %= MAX_DPC;
2416 p.dpc(p.pAdapter, p.arg, p.flags);
2420 /* since we may have prevented Check_Idle_Call, do it here */
2421 if (DPC_Request_Nums==0) {
2422 if (p.pAdapter->outstandingCommands == 0) {
2423 _VBUS_INST(&p.pAdapter->VBus);
2424 Check_Idle_Call(p.pAdapter);
2425 CheckPendingCall(_VBUS_P0);
2431 tsleep((caddr_t)hpt_worker_thread, 0, "sched", 1);
2432 if (SIGISMEMBER(curproc->p_siglist, SIGSTOP)) {
2433 /* abort rebuilding process. */
2434 IAL_ADAPTER_T *pAdapter;
2438 pAdapter = gIal_Adapter;
2440 while(pAdapter != NULL){
2442 _vbus_p = &pAdapter->VBus;
2444 for (i=0;i<MAX_ARRAY_PER_VBUS;i++)
2446 if ((pArray=ArrayTables(i))->u.array.dArStamp==0)
2448 else if (pArray->u.array.rf_rebuilding ||
2449 pArray->u.array.rf_verifying ||
2450 pArray->u.array.rf_initializing)
2452 pArray->u.array.rf_abort_rebuild = 1;
2455 pAdapter = pAdapter->next;
2460 /*Remove this debug option*/
2463 if (SIGISMEMBER(curproc->p_siglist, SIGSTOP))
2464 tsleep((caddr_t)hpt_worker_thread, 0, "hptrdy", 2*hz);
2467 kproc_suspend_loop();
2468 tsleep((caddr_t)hpt_worker_thread, 0, "hptrdy", 2*hz); /* wait for something to do */
2472 static struct kproc_desc hpt_kp = {
2478 /*Start this thread in the hpt_attach, to prevent kernel from loading it without our controller.*/
2480 launch_worker_thread(void)
2482 IAL_ADAPTER_T *pAdapTemp;
2484 kproc_start(&hpt_kp);
2486 for (pAdapTemp = gIal_Adapter; pAdapTemp; pAdapTemp = pAdapTemp->next) {
2488 _VBUS_INST(&pAdapTemp->VBus)
2492 for(i = 0; i < MAX_ARRAY_PER_VBUS; i++)
2493 if ((pVDev=ArrayTables(i))->u.array.dArStamp==0)
2496 if (pVDev->u.array.rf_need_rebuild && !pVDev->u.array.rf_rebuilding)
2497 hpt_queue_dpc((HPT_DPC)hpt_rebuild_data_block, pAdapTemp, pVDev,
2498 (UCHAR)((pVDev->u.array.CriticalMembers || pVDev->VDeviceType == VD_RAID_1)? DUPLICATE : REBUILD_PARITY));
2503 *SYSINIT(hptwt, SI_SUB_KTHREAD_IDLE, SI_ORDER_FIRST, launch_worker_thread, NULL);
2508 /********************************************************************************/
2510 static int HPTLIBAPI fOsBuildSgl(_VBUS_ARG PCommand pCmd, FPSCAT_GATH pSg, int logical)
2512 union ccb *ccb = (union ccb *)pCmd->pOrgCommand;
2513 bus_dma_segment_t *sgList = (bus_dma_segment_t *)ccb->csio.data_ptr;
2517 if (ccb->ccb_h.flags & CAM_DATA_PHYS)
2518 panic("physical address unsupported");
2520 if (ccb->ccb_h.flags & CAM_SCATTER_VALID) {
2521 if (ccb->ccb_h.flags & CAM_SG_LIST_PHYS)
2522 panic("physical address unsupported");
2524 for (idx = 0; idx < ccb->csio.sglist_cnt; idx++) {
2525 pSg[idx].dSgAddress = (ULONG_PTR)(UCHAR *)sgList[idx].ds_addr;
2526 pSg[idx].wSgSize = sgList[idx].ds_len;
2527 pSg[idx].wSgFlag = (idx==ccb->csio.sglist_cnt-1)? SG_FLAG_EOT : 0;
2531 pSg->dSgAddress = (ULONG_PTR)(UCHAR *)ccb->csio.data_ptr;
2532 pSg->wSgSize = ccb->csio.dxfer_len;
2533 pSg->wSgFlag = SG_FLAG_EOT;
2538 /* since we have provided physical sg, nobody will ask us to build physical sg */
2543 /*******************************************************************************/
2548 * the system variable, ticks, can't be used since it hasn't yet been active
2549 * when our driver starts (ticks==0, it's a invalid stamp value)
2552 do { stamp = krandom(); } while (stamp==0);
2558 SetInquiryData(PINQUIRYDATA inquiryData, PVDevice pVDev)
2561 IDENTIFY_DATA2 *pIdentify = (IDENTIFY_DATA2*)pVDev->u.disk.mv->identifyDevice;
2563 inquiryData->DeviceType = T_DIRECT; /*DIRECT_ACCESS_DEVICE*/
2564 inquiryData->AdditionalLength = (UCHAR)(sizeof(INQUIRYDATA) - 5);
2566 inquiryData->CommandQueue = 1;
2569 switch(pVDev->VDeviceType) {
2570 case VD_SINGLE_DISK:
2573 /* Set the removable bit, if applicable. */
2574 if ((pVDev->u.disk.df_removable_drive) || (pIdentify->GeneralConfiguration & 0x80))
2575 inquiryData->RemovableMedia = 1;
2577 /* Fill in vendor identification fields. */
2578 for (i = 0; i < 8; i += 2) {
2579 inquiryData->VendorId[i] = ((PUCHAR)pIdentify->ModelNumber)[i + 1];
2580 inquiryData->VendorId[i+1] = ((PUCHAR)pIdentify->ModelNumber)[i];
2584 /* Initialize unused portion of product id. */
2585 for (i = 0; i < 4; i++) inquiryData->ProductId[12+i] = ' ';
2587 /* firmware revision */
2588 for (i = 0; i < 4; i += 2)
2590 inquiryData->ProductRevisionLevel[i] = ((PUCHAR)pIdentify->FirmwareRevision)[i+1];
2591 inquiryData->ProductRevisionLevel[i+1] = ((PUCHAR)pIdentify->FirmwareRevision)[i];
2595 memcpy(&inquiryData->VendorId, "RR18xx ", 8);
2596 #ifdef SUPPORT_ARRAY
2597 switch(pVDev->VDeviceType){
2599 if ((pVDev->u.array.pMember[0] && mIsArray(pVDev->u.array.pMember[0])) ||
2600 (pVDev->u.array.pMember[1] && mIsArray(pVDev->u.array.pMember[1])))
2601 memcpy(&inquiryData->ProductId, "RAID 1/0 Array ", 16);
2603 memcpy(&inquiryData->ProductId, "RAID 0 Array ", 16);
2606 if ((pVDev->u.array.pMember[0] && mIsArray(pVDev->u.array.pMember[0])) ||
2607 (pVDev->u.array.pMember[1] && mIsArray(pVDev->u.array.pMember[1])))
2608 memcpy(&inquiryData->ProductId, "RAID 0/1 Array ", 16);
2610 memcpy(&inquiryData->ProductId, "RAID 1 Array ", 16);
2613 memcpy(&inquiryData->ProductId, "RAID 5 Array ", 16);
2616 memcpy(&inquiryData->ProductId, "JBOD Array ", 16);
2620 memcpy(&inquiryData->ProductRevisionLevel, "3.00", 4);
2626 hpt_timeout(void *arg)
2628 _VBUS_INST(&((PBUS_DMAMAP)((union ccb *)arg)->ccb_adapter)->pAdapter->VBus)
2630 fResetVBus(_VBUS_P0);
2635 hpt_io_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
2637 PCommand pCmd = (PCommand)arg;
2638 union ccb *ccb = pCmd->pOrgCommand;
2639 struct ccb_hdr *ccb_h = &ccb->ccb_h;
2640 PBUS_DMAMAP pmap = (PBUS_DMAMAP) ccb->ccb_adapter;
2641 IAL_ADAPTER_T *pAdapter = pmap->pAdapter;
2642 PVDevice pVDev = pAdapter->VBus.pVDevice[ccb_h->target_id];
2643 FPSCAT_GATH psg = pCmd->pSgTable;
2645 _VBUS_INST(pVDev->pVBus)
2647 HPT_ASSERT(pCmd->cf_physical_sg);
2649 if (error || nsegs == 0)
2650 panic("busdma error");
2652 HPT_ASSERT(nsegs<= MAX_SG_DESCRIPTORS);
2654 for (idx = 0; idx < nsegs; idx++, psg++) {
2655 psg->dSgAddress = (ULONG_PTR)(UCHAR *)segs[idx].ds_addr;
2656 psg->wSgSize = segs[idx].ds_len;
2657 psg->wSgFlag = (idx == nsegs-1)? SG_FLAG_EOT: 0;
2658 /* KdPrint(("psg[%d]:add=%p,size=%x,flag=%x\n", idx, psg->dSgAddress,psg->wSgSize,psg->wSgFlag)); */
2660 /* psg[-1].wSgFlag = SG_FLAG_EOT; */
2662 if (pCmd->cf_data_in) {
2663 bus_dmamap_sync(pAdapter->io_dma_parent, pmap->dma_map, BUS_DMASYNC_PREREAD);
2665 else if (pCmd->cf_data_out) {
2666 bus_dmamap_sync(pAdapter->io_dma_parent, pmap->dma_map, BUS_DMASYNC_PREWRITE);
2669 callout_reset(ccb->ccb_h.timeout_ch, 20*hz, hpt_timeout, ccb);
2670 pVDev->pfnSendCommand(_VBUS_P pCmd);
2671 CheckPendingCall(_VBUS_P0);
2676 static void HPTLIBAPI
2677 OsSendCommand(_VBUS_ARG union ccb *ccb)
2679 PBUS_DMAMAP pmap = (PBUS_DMAMAP)ccb->ccb_adapter;
2680 IAL_ADAPTER_T *pAdapter = pmap->pAdapter;
2681 struct ccb_hdr *ccb_h = &ccb->ccb_h;
2682 struct ccb_scsiio *csio = &ccb->csio;
2683 PVDevice pVDev = pAdapter->VBus.pVDevice[ccb_h->target_id];
2685 KdPrintI(("OsSendCommand: ccb %p cdb %x-%x-%x\n",
2687 *(ULONG *)&ccb->csio.cdb_io.cdb_bytes[0],
2688 *(ULONG *)&ccb->csio.cdb_io.cdb_bytes[4],
2689 *(ULONG *)&ccb->csio.cdb_io.cdb_bytes[8]
2692 pAdapter->outstandingCommands++;
2694 if (pVDev == NULL || pVDev->vf_online == 0) {
2695 ccb->ccb_h.status = CAM_REQ_INVALID;
2697 goto Command_Complished;
2700 switch(ccb->csio.cdb_io.cdb_bytes[0])
2702 case TEST_UNIT_READY:
2703 case START_STOP_UNIT:
2704 case SYNCHRONIZE_CACHE:
2706 ccb->ccb_h.status = CAM_REQ_CMP;
2710 ZeroMemory(ccb->csio.data_ptr, ccb->csio.dxfer_len);
2711 SetInquiryData((PINQUIRYDATA)ccb->csio.data_ptr, pVDev);
2712 ccb_h->status = CAM_REQ_CMP;
2717 UCHAR *rbuf=csio->data_ptr;
2720 if (pVDev->VDeviceCapacity > 0xfffffffful) {
2723 cap = pVDev->VDeviceCapacity - 1;
2726 rbuf[0] = (UCHAR)(cap>>24);
2727 rbuf[1] = (UCHAR)(cap>>16);
2728 rbuf[2] = (UCHAR)(cap>>8);
2729 rbuf[3] = (UCHAR)cap;
2730 /* Claim 512 byte blocks (big-endian). */
2736 ccb_h->status = CAM_REQ_CMP;
2740 case 0x9e: /*SERVICE_ACTION_IN*/
2742 UCHAR *rbuf = csio->data_ptr;
2743 LBA_T cap = pVDev->VDeviceCapacity - 1;
2745 rbuf[0] = (UCHAR)(cap>>56);
2746 rbuf[1] = (UCHAR)(cap>>48);
2747 rbuf[2] = (UCHAR)(cap>>40);
2748 rbuf[3] = (UCHAR)(cap>>32);
2749 rbuf[4] = (UCHAR)(cap>>24);
2750 rbuf[5] = (UCHAR)(cap>>16);
2751 rbuf[6] = (UCHAR)(cap>>8);
2752 rbuf[7] = (UCHAR)cap;
2758 ccb_h->status = CAM_REQ_CMP;
2766 case 0x88: /* READ_16 */
2767 case 0x8a: /* WRITE_16 */
2773 _VBUS_INST(pVDev->pVBus)
2774 PCommand pCmd = AllocateCommand(_VBUS_P0);
2777 CdbLength = csio->cdb_len;
2778 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0)
2780 if ((ccb->ccb_h.flags & CAM_CDB_PHYS) == 0)
2782 bcopy(csio->cdb_io.cdb_ptr, Cdb, CdbLength);
2786 KdPrintE(("ERROR!!!\n"));
2787 ccb->ccb_h.status = CAM_REQ_INVALID;
2793 bcopy(csio->cdb_io.cdb_bytes, Cdb, CdbLength);
2796 pCmd->pOrgCommand = ccb;
2797 pCmd->pVDevice = pVDev;
2798 pCmd->pfnCompletion = fOsCommandDone;
2799 pCmd->pfnBuildSgl = fOsBuildSgl;
2800 pCmd->pSgTable = pmap->psg;
2807 pCmd->uCmd.Ide.Lba = ((ULONG)Cdb[1] << 16) | ((ULONG)Cdb[2] << 8) | (ULONG)Cdb[3];
2808 pCmd->uCmd.Ide.nSectors = (USHORT) Cdb[4];
2811 case 0x88: /* READ_16 */
2812 case 0x8a: /* WRITE_16 */
2813 pCmd->uCmd.Ide.Lba =
2814 (HPT_U64)Cdb[2] << 56 |
2815 (HPT_U64)Cdb[3] << 48 |
2816 (HPT_U64)Cdb[4] << 40 |
2817 (HPT_U64)Cdb[5] << 32 |
2818 (HPT_U64)Cdb[6] << 24 |
2819 (HPT_U64)Cdb[7] << 16 |
2820 (HPT_U64)Cdb[8] << 8 |
2822 pCmd->uCmd.Ide.nSectors = (USHORT)Cdb[12] << 8 | (USHORT)Cdb[13];
2826 pCmd->uCmd.Ide.Lba = (ULONG)Cdb[5] | ((ULONG)Cdb[4] << 8) | ((ULONG)Cdb[3] << 16) | ((ULONG)Cdb[2] << 24);
2827 pCmd->uCmd.Ide.nSectors = (USHORT) Cdb[8] | ((USHORT)Cdb[7]<<8);
2835 case 0x88: /* READ_16 */
2836 pCmd->uCmd.Ide.Command = IDE_COMMAND_READ;
2837 pCmd->cf_data_in = 1;
2842 case 0x8a: /* WRITE_16 */
2843 pCmd->uCmd.Ide.Command = IDE_COMMAND_WRITE;
2844 pCmd->cf_data_out = 1;
2848 pCmd->uCmd.Ide.Command = IDE_COMMAND_VERIFY;
2851 /*///////////////////////// */
2852 if (ccb->ccb_h.flags & CAM_SCATTER_VALID) {
2854 bus_dma_segment_t *sgList = (bus_dma_segment_t *)ccb->csio.data_ptr;
2856 if (ccb->ccb_h.flags & CAM_SG_LIST_PHYS)
2857 pCmd->cf_physical_sg = 1;
2859 for (idx = 0; idx < ccb->csio.sglist_cnt; idx++) {
2860 pCmd->pSgTable[idx].dSgAddress = (ULONG_PTR)(UCHAR *)sgList[idx].ds_addr;
2861 pCmd->pSgTable[idx].wSgSize = sgList[idx].ds_len;
2862 pCmd->pSgTable[idx].wSgFlag= (idx==ccb->csio.sglist_cnt-1)?SG_FLAG_EOT: 0;
2865 callout_reset(ccb->ccb_h.timeout_ch, 20 * hz,
2867 pVDev->pfnSendCommand(_VBUS_P pCmd);
2871 pCmd->cf_physical_sg = 1;
2872 error = bus_dmamap_load(pAdapter->io_dma_parent,
2874 ccb->csio.data_ptr, ccb->csio.dxfer_len,
2875 hpt_io_dmamap_callback, pCmd,
2878 KdPrint(("bus_dmamap_load return %d\n", error));
2879 if (error && error!=EINPROGRESS) {
2880 hpt_printk(("bus_dmamap_load error %d\n", error));
2881 FreeCommand(_VBUS_P pCmd);
2882 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2884 pAdapter->outstandingCommands--;
2888 goto Command_Complished;
2892 ccb->ccb_h.status = CAM_REQ_INVALID;
2897 CheckPendingCall(_VBUS_P0);
2901 static void HPTLIBAPI
2902 fOsCommandDone(_VBUS_ARG PCommand pCmd)
2904 union ccb *ccb = pCmd->pOrgCommand;
2905 PBUS_DMAMAP pmap = (PBUS_DMAMAP)ccb->ccb_adapter;
2906 IAL_ADAPTER_T *pAdapter = pmap->pAdapter;
2908 KdPrint(("fOsCommandDone(pcmd=%p, result=%d)\n", pCmd, pCmd->Result));
2910 callout_stop(ccb->ccb_h.timeout_ch);
2912 switch(pCmd->Result) {
2913 case RETURN_SUCCESS:
2914 ccb->ccb_h.status = CAM_REQ_CMP;
2916 case RETURN_BAD_DEVICE:
2917 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2919 case RETURN_DEVICE_BUSY:
2920 ccb->ccb_h.status = CAM_BUSY;
2922 case RETURN_INVALID_REQUEST:
2923 ccb->ccb_h.status = CAM_REQ_INVALID;
2925 case RETURN_SELECTION_TIMEOUT:
2926 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
2929 ccb->ccb_h.status = CAM_BUSY;
2932 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
2936 if (pCmd->cf_data_in) {
2937 bus_dmamap_sync(pAdapter->io_dma_parent, pmap->dma_map, BUS_DMASYNC_POSTREAD);
2939 else if (pCmd->cf_data_out) {
2940 bus_dmamap_sync(pAdapter->io_dma_parent, pmap->dma_map, BUS_DMASYNC_POSTWRITE);
2943 bus_dmamap_unload(pAdapter->io_dma_parent, pmap->dma_map);
2945 FreeCommand(_VBUS_P pCmd);
2950 hpt_queue_dpc(HPT_DPC dpc, IAL_ADAPTER_T * pAdapter, void *arg, UCHAR flags)
2954 p = (DpcQueue_Last + 1) % MAX_DPC;
2955 if (p==DpcQueue_First) {
2956 KdPrint(("DPC Queue full!\n"));
2960 DpcQueue[DpcQueue_Last].dpc = dpc;
2961 DpcQueue[DpcQueue_Last].pAdapter = pAdapter;
2962 DpcQueue[DpcQueue_Last].arg = arg;
2963 DpcQueue[DpcQueue_Last].flags = flags;
2971 * Allocate memory above 16M, otherwise we may eat all low memory for ISA devices.
2972 * How about the memory for 5081 request/response array and PRD table?
2975 *os_alloc_page(_VBUS_ARG0)
2977 return (void *)contigmalloc(0x1000, M_DEVBUF, M_NOWAIT, 0x1000000, 0xffffffff, PAGE_SIZE, 0ul);
2981 *os_alloc_dma_page(_VBUS_ARG0)
2983 return (void *)contigmalloc(0x1000, M_DEVBUF, M_NOWAIT, 0x1000000, 0xffffffff, PAGE_SIZE, 0ul);
2987 os_free_page(_VBUS_ARG void *p)
2989 contigfree(p, 0x1000, M_DEVBUF);
2993 os_free_dma_page(_VBUS_ARG void *p)
2995 contigfree(p, 0x1000, M_DEVBUF);
2999 DoXor1(ULONG *p0, ULONG *p1, ULONG *p2, UINT nBytes)
3002 for (i = 0; i < nBytes / 4; i++) *p0++ = *p1++ ^ *p2++;
3006 DoXor2(ULONG *p0, ULONG *p2, UINT nBytes)
3009 for (i = 0; i < nBytes / 4; i++) *p0++ ^= *p2++;