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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/resource.h>
37 #include <sys/callout.h>
38 #include <sys/signalvar.h>
39 #include <sys/eventhandler.h>
41 #include <sys/kthread.h>
43 #include <sys/mutex.h>
44 #include <sys/module.h>
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/pcivar.h>
54 #include <dev/hptmv/global.h>
55 #include <dev/hptmv/hptintf.h>
56 #include <dev/hptmv/osbsd.h>
57 #include <dev/hptmv/access601.h>
62 int hpt_dbg_level = DEBUG_LEVEL;
64 int hpt_dbg_level = 0;
68 #define MV_ERROR printf
71 * CAM SIM entry points
73 static int hpt_probe (device_t dev);
74 static void launch_worker_thread(void);
75 static int hpt_attach(device_t dev);
76 static int hpt_detach(device_t dev);
77 static int hpt_shutdown(device_t dev);
78 static void hpt_poll(struct cam_sim *sim);
79 static void hpt_intr(void *arg);
80 static void hpt_async(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg);
81 static void hpt_action(struct cam_sim *sim, union ccb *ccb);
83 static device_method_t driver_methods[] = {
84 /* Device interface */
85 DEVMETHOD(device_probe, hpt_probe),
86 DEVMETHOD(device_attach, hpt_attach),
87 DEVMETHOD(device_detach, hpt_detach),
89 DEVMETHOD(device_shutdown, hpt_shutdown),
93 static driver_t hpt_pci_driver = {
99 static devclass_t hpt_devclass;
101 #define __DRIVER_MODULE(p1, p2, p3, p4, p5, p6) DRIVER_MODULE(p1, p2, p3, p4, p5, p6)
102 __DRIVER_MODULE(PROC_DIR_NAME, pci, hpt_pci_driver, hpt_devclass, 0, 0);
103 MODULE_DEPEND(PROC_DIR_NAME, cam, 1, 1, 1);
105 #define ccb_ccb_ptr spriv_ptr0
106 #define ccb_adapter ccb_h.spriv_ptr1
108 static void SetInquiryData(PINQUIRYDATA inquiryData, PVDevice pVDev);
109 static void HPTLIBAPI OsSendCommand (_VBUS_ARG union ccb * ccb);
110 static void HPTLIBAPI fOsCommandDone(_VBUS_ARG PCommand pCmd);
111 static void ccb_done(union ccb *ccb);
112 static void hpt_queue_ccb(union ccb **ccb_Q, union ccb *ccb);
113 static void hpt_free_ccb(union ccb **ccb_Q, union ccb *ccb);
114 static void hpt_intr_locked(IAL_ADAPTER_T *pAdapter);
115 static void hptmv_free_edma_queues(IAL_ADAPTER_T *pAdapter);
116 static void hptmv_free_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum);
117 static void handleEdmaError(_VBUS_ARG PCommand pCmd);
118 static int hptmv_init_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum);
119 static int fResetActiveCommands(PVBus _vbus_p);
120 static void fRegisterVdevice(IAL_ADAPTER_T *pAdapter);
121 static int hptmv_allocate_edma_queues(IAL_ADAPTER_T *pAdapter);
122 static void hptmv_handle_event_disconnect(void *data);
123 static void hptmv_handle_event_connect(void *data);
124 static int start_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum);
125 static void init_vdev_params(IAL_ADAPTER_T *pAdapter, MV_U8 channel);
126 static int hptmv_parse_identify_results(MV_SATA_CHANNEL *pMvSataChannel);
127 static int HPTLIBAPI fOsBuildSgl(_VBUS_ARG PCommand pCmd, FPSCAT_GATH pSg,
129 static MV_BOOLEAN CommandCompletionCB(MV_SATA_ADAPTER *pMvSataAdapter,
130 MV_U8 channelNum, MV_COMPLETION_TYPE comp_type, MV_VOID_PTR commandId,
131 MV_U16 responseFlags, MV_U32 timeStamp,
132 MV_STORAGE_DEVICE_REGISTERS *registerStruct);
133 static MV_BOOLEAN hptmv_event_notify(MV_SATA_ADAPTER *pMvSataAdapter,
134 MV_EVENT_TYPE eventType, MV_U32 param1, MV_U32 param2);
136 #define ccb_ccb_ptr spriv_ptr0
137 #define ccb_adapter ccb_h.spriv_ptr1
139 static struct sx hptmv_list_lock;
140 SX_SYSINIT(hptmv_list_lock, &hptmv_list_lock, "hptmv list");
141 IAL_ADAPTER_T *gIal_Adapter = 0;
142 IAL_ADAPTER_T *pCurAdapter = 0;
143 static MV_SATA_CHANNEL gMvSataChannels[MAX_VBUS][MV_SATA_CHANNELS_NUM];
145 typedef struct st_HPT_DPC {
146 IAL_ADAPTER_T *pAdapter;
147 void (*dpc)(IAL_ADAPTER_T *, void *, UCHAR);
153 UCHAR DPC_Request_Nums = 0;
154 static ST_HPT_DPC DpcQueue[MAX_DPC];
155 static int DpcQueue_First=0;
156 static int DpcQueue_Last = 0;
157 static struct mtx DpcQueue_Lock;
158 MTX_SYSINIT(hpmtv_dpc_lock, &DpcQueue_Lock, "hptmv dpc", MTX_DEF);
160 char DRIVER_VERSION[] = "v1.16";
162 /*******************************************************************************
163 * Name: hptmv_free_channel
165 * Description: free allocated queues for the given channel
167 * Parameters: pMvSataAdapter - pointer to the RR18xx controller this
168 * channel connected to.
169 * channelNum - channel number.
171 ******************************************************************************/
173 hptmv_free_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum)
175 HPT_ASSERT(channelNum < MV_SATA_CHANNELS_NUM);
176 pAdapter->mvSataAdapter.sataChannel[channelNum] = NULL;
179 static void failDevice(PVDevice pVDev)
181 PVBus _vbus_p = pVDev->pVBus;
182 IAL_ADAPTER_T *pAdapter = (IAL_ADAPTER_T *)_vbus_p->OsExt;
184 pVDev->u.disk.df_on_line = 0;
185 pVDev->vf_online = 0;
186 if (pVDev->pfnDeviceFailed)
187 CallWhenIdle(_VBUS_P (DPC_PROC)pVDev->pfnDeviceFailed, pVDev);
189 fNotifyGUI(ET_DEVICE_REMOVED, pVDev);
192 if (pAdapter->ver_601==2 && !pAdapter->beeping) {
193 pAdapter->beeping = 1;
194 BeepOn(pAdapter->mvSataAdapter.adapterIoBaseAddress);
195 set_fail_led(&pAdapter->mvSataAdapter, pVDev->u.disk.mv->channelNumber, 1);
200 int MvSataResetChannel(MV_SATA_ADAPTER *pMvSataAdapter, MV_U8 channel);
203 handleEdmaError(_VBUS_ARG PCommand pCmd)
205 PDevice pDevice = &pCmd->pVDevice->u.disk;
206 MV_SATA_ADAPTER * pSataAdapter = pDevice->mv->mvSataAdapter;
208 if (!pDevice->df_on_line) {
209 KdPrint(("Device is offline"));
210 pCmd->Result = RETURN_BAD_DEVICE;
211 CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
215 if (pCmd->RetryCount++>5) {
216 hpt_printk(("too many retries on channel(%d)\n", pDevice->mv->channelNumber));
218 failDevice(pCmd->pVDevice);
219 pCmd->Result = RETURN_IDE_ERROR;
220 CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
224 /* reset the channel and retry the command */
225 if (MvSataResetChannel(pSataAdapter, pDevice->mv->channelNumber))
228 fNotifyGUI(ET_DEVICE_ERROR, Map2pVDevice(pDevice));
230 hpt_printk(("Retry on channel(%d)\n", pDevice->mv->channelNumber));
231 fDeviceSendCommand(_VBUS_P pCmd);
234 /****************************************************************
235 * Name: hptmv_init_channel
237 * Description: allocate request and response queues for the EDMA of the
238 * given channel and sets other fields.
241 * pAdapter - pointer to the emulated adapter data structure
242 * channelNum - channel number.
243 * Return: 0 on success, otherwise on failure
244 ****************************************************************/
246 hptmv_init_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum)
248 MV_SATA_CHANNEL *pMvSataChannel;
249 dma_addr_t req_dma_addr;
250 dma_addr_t rsp_dma_addr;
252 if (channelNum >= MV_SATA_CHANNELS_NUM)
254 MV_ERROR("RR18xx[%d]: Bad channelNum=%d",
255 pAdapter->mvSataAdapter.adapterId, channelNum);
259 pMvSataChannel = &gMvSataChannels[pAdapter->mvSataAdapter.adapterId][channelNum];
260 pAdapter->mvSataAdapter.sataChannel[channelNum] = pMvSataChannel;
261 pMvSataChannel->channelNumber = channelNum;
262 pMvSataChannel->lba48Address = MV_FALSE;
263 pMvSataChannel->maxReadTransfer = MV_FALSE;
265 pMvSataChannel->requestQueue = (struct mvDmaRequestQueueEntry *)
266 (pAdapter->requestsArrayBaseAlignedAddr + (channelNum * MV_EDMA_REQUEST_QUEUE_SIZE));
267 req_dma_addr = pAdapter->requestsArrayBaseDmaAlignedAddr + (channelNum * MV_EDMA_REQUEST_QUEUE_SIZE);
270 KdPrint(("requestQueue addr is 0x%llX", (HPT_U64)(ULONG_PTR)req_dma_addr));
272 /* check the 1K alignment of the request queue*/
273 if (req_dma_addr & 0x3ff)
275 MV_ERROR("RR18xx[%d]: request queue allocated isn't 1 K aligned,"
276 " dma_addr=%llx channel=%d\n", pAdapter->mvSataAdapter.adapterId,
277 (HPT_U64)(ULONG_PTR)req_dma_addr, channelNum);
280 pMvSataChannel->requestQueuePciLowAddress = req_dma_addr;
281 pMvSataChannel->requestQueuePciHiAddress = 0;
282 KdPrint(("RR18xx[%d,%d]: request queue allocated: 0x%p",
283 pAdapter->mvSataAdapter.adapterId, channelNum,
284 pMvSataChannel->requestQueue));
285 pMvSataChannel->responseQueue = (struct mvDmaResponseQueueEntry *)
286 (pAdapter->responsesArrayBaseAlignedAddr + (channelNum * MV_EDMA_RESPONSE_QUEUE_SIZE));
287 rsp_dma_addr = pAdapter->responsesArrayBaseDmaAlignedAddr + (channelNum * MV_EDMA_RESPONSE_QUEUE_SIZE);
289 /* check the 256 alignment of the response queue*/
290 if (rsp_dma_addr & 0xff)
292 MV_ERROR("RR18xx[%d,%d]: response queue allocated isn't 256 byte "
293 "aligned, dma_addr=%llx\n",
294 pAdapter->mvSataAdapter.adapterId, channelNum, (HPT_U64)(ULONG_PTR)rsp_dma_addr);
297 pMvSataChannel->responseQueuePciLowAddress = rsp_dma_addr;
298 pMvSataChannel->responseQueuePciHiAddress = 0;
299 KdPrint(("RR18xx[%d,%d]: response queue allocated: 0x%p",
300 pAdapter->mvSataAdapter.adapterId, channelNum,
301 pMvSataChannel->responseQueue));
303 pAdapter->mvChannel[channelNum].online = MV_TRUE;
307 /******************************************************************************
308 * Name: hptmv_parse_identify_results
310 * Description: this functions parses the identify command results, checks
311 * that the connected deives can be accesed by RR18xx EDMA,
312 * and updates the channel structure accordingly.
314 * Parameters: pMvSataChannel, pointer to the channel data structure.
316 * Returns: =0 ->success, < 0 ->failure.
318 ******************************************************************************/
320 hptmv_parse_identify_results(MV_SATA_CHANNEL *pMvSataChannel)
322 MV_U16 *iden = pMvSataChannel->identifyDevice;
325 if (! (iden[IDEN_CAPACITY_1_OFFSET] & 0x200))
327 KdPrint(("IAL Error in IDENTIFY info: LBA not supported\n"));
332 KdPrint(("%25s - %s\n", "Capabilities", "LBA supported"));
335 if (! (iden[IDEN_CAPACITY_1_OFFSET] & 0x100))
337 KdPrint(("IAL Error in IDENTIFY info: DMA not supported\n"));
342 KdPrint(("%25s - %s\n", "Capabilities", "DMA supported"));
345 if ((iden[IDEN_VALID] & 2) == 0)
347 KdPrint(("IAL Error in IDENTIFY info: not able to find PIO mode\n"));
350 KdPrint(("%25s - 0x%02x\n", "PIO modes supported",
351 iden[IDEN_PIO_MODE_SPPORTED] & 0xff));
354 if ((iden[IDEN_VALID] & 4) == 0)
356 KdPrint(("IAL Error in IDENTIFY info: not able to find UDMA mode\n"));
361 if ((iden[IDEN_SUPPORTED_COMMANDS2] & 0x400))
363 KdPrint(("%25s - %s\n", "LBA48 addressing", "supported"));
364 pMvSataChannel->lba48Address = MV_TRUE;
368 KdPrint(("%25s - %s\n", "LBA48 addressing", "Not supported"));
369 pMvSataChannel->lba48Address = MV_FALSE;
375 init_vdev_params(IAL_ADAPTER_T *pAdapter, MV_U8 channel)
377 PVDevice pVDev = &pAdapter->VDevices[channel];
378 MV_SATA_CHANNEL *pMvSataChannel = pAdapter->mvSataAdapter.sataChannel[channel];
379 MV_U16_PTR IdentifyData = pMvSataChannel->identifyDevice;
381 pMvSataChannel->outstandingCommands = 0;
383 pVDev->u.disk.mv = pMvSataChannel;
384 pVDev->u.disk.df_on_line = 1;
385 pVDev->u.disk.pVBus = &pAdapter->VBus;
386 pVDev->pVBus = &pAdapter->VBus;
388 #ifdef SUPPORT_48BIT_LBA
389 if (pMvSataChannel->lba48Address == MV_TRUE)
390 pVDev->u.disk.dDeRealCapacity = ((IdentifyData[101]<<16) | IdentifyData[100]) - 1;
393 if(IdentifyData[53] & 1) {
394 pVDev->u.disk.dDeRealCapacity =
395 (((IdentifyData[58]<<16 | IdentifyData[57]) < (IdentifyData[61]<<16 | IdentifyData[60])) ?
396 (IdentifyData[61]<<16 | IdentifyData[60]) :
397 (IdentifyData[58]<<16 | IdentifyData[57])) - 1;
399 pVDev->u.disk.dDeRealCapacity =
400 (IdentifyData[61]<<16 | IdentifyData[60]) - 1;
402 pVDev->u.disk.bDeUsable_Mode = pVDev->u.disk.bDeModeSetting =
403 pAdapter->mvChannel[channel].maxPioModeSupported - MV_ATA_TRANSFER_PIO_0;
405 if (pAdapter->mvChannel[channel].maxUltraDmaModeSupported!=0xFF) {
406 pVDev->u.disk.bDeUsable_Mode = pVDev->u.disk.bDeModeSetting =
407 pAdapter->mvChannel[channel].maxUltraDmaModeSupported - MV_ATA_TRANSFER_UDMA_0 + 8;
411 static void device_change(IAL_ADAPTER_T *pAdapter , MV_U8 channelIndex, int plugged)
414 MV_SATA_ADAPTER *pMvSataAdapter = &pAdapter->mvSataAdapter;
415 MV_SATA_CHANNEL *pMvSataChannel = pMvSataAdapter->sataChannel[channelIndex];
417 if (!pMvSataChannel) return;
421 pVDev = &(pAdapter->VDevices[channelIndex]);
422 init_vdev_params(pAdapter, channelIndex);
424 pVDev->VDeviceType = pVDev->u.disk.df_atapi? VD_ATAPI :
425 pVDev->u.disk.df_removable_drive? VD_REMOVABLE : VD_SINGLE_DISK;
427 pVDev->VDeviceCapacity = pVDev->u.disk.dDeRealCapacity-SAVE_FOR_RAID_INFO;
428 pVDev->pfnSendCommand = pfnSendCommand[pVDev->VDeviceType];
429 pVDev->pfnDeviceFailed = pfnDeviceFailed[pVDev->VDeviceType];
430 pVDev->vf_online = 1;
436 for(iMember = 0; iMember < pVDev->pParent->u.array.bArnMember; iMember++)
437 if((PVDevice)pVDev->pParent->u.array.pMember[iMember] == pVDev)
438 pVDev->pParent->u.array.pMember[iMember] = NULL;
439 pVDev->pParent = NULL;
442 fNotifyGUI(ET_DEVICE_PLUGGED,pVDev);
443 fCheckBootable(pVDev);
444 RegisterVDevice(pVDev);
447 if (pAdapter->beeping) {
448 pAdapter->beeping = 0;
449 BeepOff(pAdapter->mvSataAdapter.adapterIoBaseAddress);
456 pVDev = &(pAdapter->VDevices[channelIndex]);
462 start_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum)
464 MV_SATA_ADAPTER *pMvSataAdapter = &pAdapter->mvSataAdapter;
465 MV_SATA_CHANNEL *pMvSataChannel = pMvSataAdapter->sataChannel[channelNum];
466 MV_CHANNEL *pChannelInfo = &(pAdapter->mvChannel[channelNum]);
467 MV_U32 udmaMode,pioMode;
469 KdPrint(("RR18xx [%d]: start channel (%d)", pMvSataAdapter->adapterId,
473 /* Software reset channel */
474 if (mvStorageDevATASoftResetDevice(pMvSataAdapter, channelNum) == MV_FALSE)
476 MV_ERROR("RR18xx [%d,%d]: failed to perform Software reset\n",
477 pMvSataAdapter->adapterId, channelNum);
481 /* Hardware reset channel */
482 if (mvSataChannelHardReset(pMvSataAdapter, channelNum) == MV_FALSE)
484 /* If failed, try again - this is when trying to hardreset a channel */
485 /* when drive is just spinning up */
486 StallExec(5000000); /* wait 5 sec before trying again */
487 if (mvSataChannelHardReset(pMvSataAdapter, channelNum) == MV_FALSE)
489 MV_ERROR("RR18xx [%d,%d]: failed to perform Hard reset\n",
490 pMvSataAdapter->adapterId, channelNum);
496 if (mvStorageDevATAIdentifyDevice(pMvSataAdapter, channelNum) == MV_FALSE)
498 MV_ERROR("RR18xx [%d,%d]: failed to perform ATA Identify command\n"
499 , pMvSataAdapter->adapterId, channelNum);
502 if (hptmv_parse_identify_results(pMvSataChannel))
504 MV_ERROR("RR18xx [%d,%d]: Error in parsing ATA Identify message\n"
505 , pMvSataAdapter->adapterId, channelNum);
509 /* mvStorageDevATASetFeatures */
510 /* Disable 8 bit PIO in case CFA enabled */
511 if (pMvSataChannel->identifyDevice[86] & 4)
513 KdPrint(("RR18xx [%d]: Disable 8 bit PIO (CFA enabled) \n",
514 pMvSataAdapter->adapterId));
515 if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
516 MV_ATA_SET_FEATURES_DISABLE_8_BIT_PIO, 0,
517 0, 0, 0) == MV_FALSE)
519 MV_ERROR("RR18xx [%d]: channel %d: mvStorageDevATASetFeatures"
520 " failed\n", pMvSataAdapter->adapterId, channelNum);
525 #ifdef ENABLE_WRITE_CACHE
526 if (pMvSataChannel->identifyDevice[82] & 0x20)
528 if (!(pMvSataChannel->identifyDevice[85] & 0x20)) /* if not enabled by default */
530 if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
531 MV_ATA_SET_FEATURES_ENABLE_WCACHE, 0,
532 0, 0, 0) == MV_FALSE)
534 MV_ERROR("RR18xx [%d]: channel %d: mvStorageDevATASetFeatures failed\n",
535 pMvSataAdapter->adapterId, channelNum);
539 KdPrint(("RR18xx [%d]: channel %d, write cache enabled\n",
540 pMvSataAdapter->adapterId, channelNum));
544 KdPrint(("RR18xx [%d]: channel %d, write cache not supported\n",
545 pMvSataAdapter->adapterId, channelNum));
547 #else /* disable write cache */
549 if (pMvSataChannel->identifyDevice[85] & 0x20)
551 KdPrint(("RR18xx [%d]: channel =%d, disable write cache\n",
552 pMvSataAdapter->adapterId, channelNum));
553 if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
554 MV_ATA_SET_FEATURES_DISABLE_WCACHE, 0,
555 0, 0, 0) == MV_FALSE)
557 MV_ERROR("RR18xx [%d]: channel %d: mvStorageDevATASetFeatures failed\n",
558 pMvSataAdapter->adapterId, channelNum);
562 KdPrint(("RR18xx [%d]: channel=%d, write cache disabled\n",
563 pMvSataAdapter->adapterId, channelNum));
567 /* Set transfer mode */
568 KdPrint(("RR18xx [%d] Set transfer mode XFER_PIO_SLOW\n",
569 pMvSataAdapter->adapterId));
570 if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
571 MV_ATA_SET_FEATURES_TRANSFER,
572 MV_ATA_TRANSFER_PIO_SLOW, 0, 0, 0) ==
575 MV_ERROR("RR18xx [%d] channel %d: Set Features failed\n",
576 pMvSataAdapter->adapterId, channelNum);
580 if (pMvSataChannel->identifyDevice[IDEN_PIO_MODE_SPPORTED] & 1)
582 pioMode = MV_ATA_TRANSFER_PIO_4;
584 else if (pMvSataChannel->identifyDevice[IDEN_PIO_MODE_SPPORTED] & 2)
586 pioMode = MV_ATA_TRANSFER_PIO_3;
590 MV_ERROR("IAL Error in IDENTIFY info: PIO modes 3 and 4 not supported\n");
591 pioMode = MV_ATA_TRANSFER_PIO_SLOW;
594 KdPrint(("RR18xx [%d] Set transfer mode XFER_PIO_4\n",
595 pMvSataAdapter->adapterId));
596 pAdapter->mvChannel[channelNum].maxPioModeSupported = pioMode;
597 if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
598 MV_ATA_SET_FEATURES_TRANSFER,
599 pioMode, 0, 0, 0) == MV_FALSE)
601 MV_ERROR("RR18xx [%d] channel %d: Set Features failed\n",
602 pMvSataAdapter->adapterId, channelNum);
606 udmaMode = MV_ATA_TRANSFER_UDMA_0;
607 if (pMvSataChannel->identifyDevice[IDEN_UDMA_MODE] & 0x40)
609 udmaMode = MV_ATA_TRANSFER_UDMA_6;
611 else if (pMvSataChannel->identifyDevice[IDEN_UDMA_MODE] & 0x20)
613 udmaMode = MV_ATA_TRANSFER_UDMA_5;
615 else if (pMvSataChannel->identifyDevice[IDEN_UDMA_MODE] & 0x10)
617 udmaMode = MV_ATA_TRANSFER_UDMA_4;
619 else if (pMvSataChannel->identifyDevice[IDEN_UDMA_MODE] & 8)
621 udmaMode = MV_ATA_TRANSFER_UDMA_3;
623 else if (pMvSataChannel->identifyDevice[IDEN_UDMA_MODE] & 4)
625 udmaMode = MV_ATA_TRANSFER_UDMA_2;
628 KdPrint(("RR18xx [%d] Set transfer mode XFER_UDMA_%d\n",
629 pMvSataAdapter->adapterId, udmaMode & 0xf));
630 pChannelInfo->maxUltraDmaModeSupported = udmaMode;
632 /*if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
633 MV_ATA_SET_FEATURES_TRANSFER, udmaMode,
634 0, 0, 0) == MV_FALSE)
636 MV_ERROR("RR18xx [%d] channel %d: Set Features failed\n",
637 pMvSataAdapter->adapterId, channelNum);
640 if (pChannelInfo->maxUltraDmaModeSupported == 0xFF)
645 if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
646 MV_ATA_SET_FEATURES_TRANSFER,
647 pChannelInfo->maxUltraDmaModeSupported,
648 0, 0, 0) == MV_FALSE)
650 if (pChannelInfo->maxUltraDmaModeSupported > MV_ATA_TRANSFER_UDMA_0)
652 if (mvStorageDevATASoftResetDevice(pMvSataAdapter, channelNum) == MV_FALSE)
654 MV_REG_WRITE_BYTE(pMvSataAdapter->adapterIoBaseAddress,
655 pMvSataChannel->eDmaRegsOffset +
656 0x11c, /* command reg */
657 MV_ATA_COMMAND_IDLE_IMMEDIATE);
658 mvMicroSecondsDelay(10000);
659 mvSataChannelHardReset(pMvSataAdapter, channelNum);
660 if (mvStorageDevATASoftResetDevice(pMvSataAdapter, channelNum) == MV_FALSE)
663 if (mvSataChannelHardReset(pMvSataAdapter, channelNum) == MV_FALSE)
665 pChannelInfo->maxUltraDmaModeSupported--;
673 /* Read look ahead */
674 #ifdef ENABLE_READ_AHEAD
675 if (pMvSataChannel->identifyDevice[82] & 0x40)
677 if (!(pMvSataChannel->identifyDevice[85] & 0x40)) /* if not enabled by default */
679 if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
680 MV_ATA_SET_FEATURES_ENABLE_RLA, 0, 0,
683 MV_ERROR("RR18xx [%d] channel %d: Set Features failed\n",
684 pMvSataAdapter->adapterId, channelNum);
688 KdPrint(("RR18xx [%d]: channel=%d, read look ahead enabled\n",
689 pMvSataAdapter->adapterId, channelNum));
693 KdPrint(("RR18xx [%d]: channel %d, Read Look Ahead not supported\n",
694 pMvSataAdapter->adapterId, channelNum));
698 if (pMvSataChannel->identifyDevice[86] & 0x20)
700 KdPrint(("RR18xx [%d]:channel %d, disable read look ahead\n",
701 pMvSataAdapter->adapterId, channelNum));
702 if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum,
703 MV_ATA_SET_FEATURES_DISABLE_RLA, 0, 0,
706 MV_ERROR("RR18xx [%d]:channel %d: ATA Set Features failed\n",
707 pMvSataAdapter->adapterId, channelNum);
711 KdPrint(("RR18xx [%d]:channel %d, read look ahead disabled\n",
712 pMvSataAdapter->adapterId, channelNum));
718 KdPrint(("RR18xx [%d]: channel %d config EDMA, Non Queued Mode\n",
719 pMvSataAdapter->adapterId,
721 if (mvSataConfigEdmaMode(pMvSataAdapter, channelNum,
722 MV_EDMA_MODE_NOT_QUEUED, 0) == MV_FALSE)
724 MV_ERROR("RR18xx [%d] channel %d Error: mvSataConfigEdmaMode failed\n",
725 pMvSataAdapter->adapterId, channelNum);
730 if (mvSataEnableChannelDma(pMvSataAdapter, channelNum) == MV_FALSE)
732 MV_ERROR("RR18xx [%d] Failed to enable DMA, channel=%d\n",
733 pMvSataAdapter->adapterId, channelNum);
736 MV_ERROR("RR18xx [%d,%d]: channel started successfully\n",
737 pMvSataAdapter->adapterId, channelNum);
740 set_fail_led(pMvSataAdapter, channelNum, 0);
746 hptmv_handle_event(void * data, int flag)
748 IAL_ADAPTER_T *pAdapter = (IAL_ADAPTER_T *)data;
749 MV_SATA_ADAPTER *pMvSataAdapter = &pAdapter->mvSataAdapter;
752 mtx_assert(&pAdapter->lock, MA_OWNED);
753 /* mvOsSemTake(&pMvSataAdapter->semaphore); */
754 for (channelIndex = 0; channelIndex < MV_SATA_CHANNELS_NUM; channelIndex++)
756 switch(pAdapter->sataEvents[channelIndex])
758 case SATA_EVENT_CHANNEL_CONNECTED:
759 /* Handle only connects */
762 KdPrint(("RR18xx [%d,%d]: new device connected\n",
763 pMvSataAdapter->adapterId, channelIndex));
764 hptmv_init_channel(pAdapter, channelIndex);
765 if (mvSataConfigureChannel( pMvSataAdapter, channelIndex) == MV_FALSE)
767 MV_ERROR("RR18xx [%d,%d] Failed to configure\n",
768 pMvSataAdapter->adapterId, channelIndex);
769 hptmv_free_channel(pAdapter, channelIndex);
773 /*mvSataChannelHardReset(pMvSataAdapter, channel);*/
774 if (start_channel( pAdapter, channelIndex))
776 MV_ERROR("RR18xx [%d,%d]Failed to start channel\n",
777 pMvSataAdapter->adapterId, channelIndex);
778 hptmv_free_channel(pAdapter, channelIndex);
782 device_change(pAdapter, channelIndex, TRUE);
785 pAdapter->sataEvents[channelIndex] = SATA_EVENT_NO_CHANGE;
788 case SATA_EVENT_CHANNEL_DISCONNECTED:
789 /* Handle only disconnects */
792 KdPrint(("RR18xx [%d,%d]: device disconnected\n",
793 pMvSataAdapter->adapterId, channelIndex));
794 /* Flush pending commands */
795 if(pMvSataAdapter->sataChannel[channelIndex])
797 _VBUS_INST(&pAdapter->VBus)
798 mvSataFlushDmaQueue (pMvSataAdapter, channelIndex,
799 MV_FLUSH_TYPE_CALLBACK);
800 CheckPendingCall(_VBUS_P0);
801 mvSataRemoveChannel(pMvSataAdapter,channelIndex);
802 hptmv_free_channel(pAdapter, channelIndex);
803 pMvSataAdapter->sataChannel[channelIndex] = NULL;
804 KdPrint(("RR18xx [%d,%d]: channel removed\n",
805 pMvSataAdapter->adapterId, channelIndex));
806 if (pAdapter->outstandingCommands==0 && DPC_Request_Nums==0)
807 Check_Idle_Call(pAdapter);
811 KdPrint(("RR18xx [%d,%d]: channel already removed!!\n",
812 pMvSataAdapter->adapterId, channelIndex));
814 pAdapter->sataEvents[channelIndex] = SATA_EVENT_NO_CHANGE;
817 case SATA_EVENT_NO_CHANGE:
824 /* mvOsSemRelease(&pMvSataAdapter->semaphore); */
827 #define EVENT_CONNECT 1
828 #define EVENT_DISCONNECT 0
831 hptmv_handle_event_connect(void *data)
833 hptmv_handle_event (data, 0);
837 hptmv_handle_event_disconnect(void *data)
839 hptmv_handle_event (data, 1);
843 hptmv_event_notify(MV_SATA_ADAPTER *pMvSataAdapter, MV_EVENT_TYPE eventType,
844 MV_U32 param1, MV_U32 param2)
846 IAL_ADAPTER_T *pAdapter = pMvSataAdapter->IALData;
850 case MV_EVENT_TYPE_SATA_CABLE:
852 MV_U8 channel = param2;
854 if (param1 == EVENT_CONNECT)
856 pAdapter->sataEvents[channel] = SATA_EVENT_CHANNEL_CONNECTED;
857 KdPrint(("RR18xx [%d,%d]: device connected event received\n",
858 pMvSataAdapter->adapterId, channel));
859 /* Delete previous timers (if multiple drives connected in the same time */
860 callout_reset(&pAdapter->event_timer_connect, 10 * hz, hptmv_handle_event_connect, pAdapter);
862 else if (param1 == EVENT_DISCONNECT)
864 pAdapter->sataEvents[channel] = SATA_EVENT_CHANNEL_DISCONNECTED;
865 KdPrint(("RR18xx [%d,%d]: device disconnected event received \n",
866 pMvSataAdapter->adapterId, channel));
867 device_change(pAdapter, channel, FALSE);
868 /* Delete previous timers (if multiple drives disconnected in the same time */
869 /*callout_reset(&pAdapter->event_timer_disconnect, 10 * hz, hptmv_handle_event_disconnect, pAdapter); */
870 /*It is not necessary to wait, handle it directly*/
871 hptmv_handle_event_disconnect(pAdapter);
876 MV_ERROR("RR18xx: illegal value for param1(%d) at "
877 "connect/disconnect event, host=%d\n", param1,
878 pMvSataAdapter->adapterId );
883 case MV_EVENT_TYPE_ADAPTER_ERROR:
884 KdPrint(("RR18xx: DEVICE error event received, pci cause "
885 "reg=%x, don't how to handle this\n", param1));
888 MV_ERROR("RR18xx[%d]: unknown event type (%d)\n",
889 pMvSataAdapter->adapterId, eventType);
896 hptmv_allocate_edma_queues(IAL_ADAPTER_T *pAdapter)
898 pAdapter->requestsArrayBaseAddr = (MV_U8 *)contigmalloc(REQUESTS_ARRAY_SIZE,
899 M_DEVBUF, M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0ul);
900 if (pAdapter->requestsArrayBaseAddr == NULL)
902 MV_ERROR("RR18xx[%d]: Failed to allocate memory for EDMA request"
903 " queues\n", pAdapter->mvSataAdapter.adapterId);
906 pAdapter->requestsArrayBaseDmaAddr = fOsPhysicalAddress(pAdapter->requestsArrayBaseAddr);
907 pAdapter->requestsArrayBaseAlignedAddr = pAdapter->requestsArrayBaseAddr;
908 pAdapter->requestsArrayBaseAlignedAddr += MV_EDMA_REQUEST_QUEUE_SIZE;
909 pAdapter->requestsArrayBaseAlignedAddr = (MV_U8 *)
910 (((ULONG_PTR)pAdapter->requestsArrayBaseAlignedAddr) & ~(ULONG_PTR)(MV_EDMA_REQUEST_QUEUE_SIZE - 1));
911 pAdapter->requestsArrayBaseDmaAlignedAddr = pAdapter->requestsArrayBaseDmaAddr;
912 pAdapter->requestsArrayBaseDmaAlignedAddr += MV_EDMA_REQUEST_QUEUE_SIZE;
913 pAdapter->requestsArrayBaseDmaAlignedAddr &= ~(ULONG_PTR)(MV_EDMA_REQUEST_QUEUE_SIZE - 1);
915 if ((pAdapter->requestsArrayBaseDmaAlignedAddr - pAdapter->requestsArrayBaseDmaAddr) !=
916 (pAdapter->requestsArrayBaseAlignedAddr - pAdapter->requestsArrayBaseAddr))
918 MV_ERROR("RR18xx[%d]: Error in Request Quueues Alignment\n",
919 pAdapter->mvSataAdapter.adapterId);
920 contigfree(pAdapter->requestsArrayBaseAddr, REQUESTS_ARRAY_SIZE, M_DEVBUF);
923 /* response queues */
924 pAdapter->responsesArrayBaseAddr = (MV_U8 *)contigmalloc(RESPONSES_ARRAY_SIZE,
925 M_DEVBUF, M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0ul);
926 if (pAdapter->responsesArrayBaseAddr == NULL)
928 MV_ERROR("RR18xx[%d]: Failed to allocate memory for EDMA response"
929 " queues\n", pAdapter->mvSataAdapter.adapterId);
930 contigfree(pAdapter->requestsArrayBaseAddr, RESPONSES_ARRAY_SIZE, M_DEVBUF);
933 pAdapter->responsesArrayBaseDmaAddr = fOsPhysicalAddress(pAdapter->responsesArrayBaseAddr);
934 pAdapter->responsesArrayBaseAlignedAddr = pAdapter->responsesArrayBaseAddr;
935 pAdapter->responsesArrayBaseAlignedAddr += MV_EDMA_RESPONSE_QUEUE_SIZE;
936 pAdapter->responsesArrayBaseAlignedAddr = (MV_U8 *)
937 (((ULONG_PTR)pAdapter->responsesArrayBaseAlignedAddr) & ~(ULONG_PTR)(MV_EDMA_RESPONSE_QUEUE_SIZE - 1));
938 pAdapter->responsesArrayBaseDmaAlignedAddr = pAdapter->responsesArrayBaseDmaAddr;
939 pAdapter->responsesArrayBaseDmaAlignedAddr += MV_EDMA_RESPONSE_QUEUE_SIZE;
940 pAdapter->responsesArrayBaseDmaAlignedAddr &= ~(ULONG_PTR)(MV_EDMA_RESPONSE_QUEUE_SIZE - 1);
942 if ((pAdapter->responsesArrayBaseDmaAlignedAddr - pAdapter->responsesArrayBaseDmaAddr) !=
943 (pAdapter->responsesArrayBaseAlignedAddr - pAdapter->responsesArrayBaseAddr))
945 MV_ERROR("RR18xx[%d]: Error in Response Queues Alignment\n",
946 pAdapter->mvSataAdapter.adapterId);
947 contigfree(pAdapter->requestsArrayBaseAddr, REQUESTS_ARRAY_SIZE, M_DEVBUF);
948 contigfree(pAdapter->responsesArrayBaseAddr, RESPONSES_ARRAY_SIZE, M_DEVBUF);
955 hptmv_free_edma_queues(IAL_ADAPTER_T *pAdapter)
957 contigfree(pAdapter->requestsArrayBaseAddr, REQUESTS_ARRAY_SIZE, M_DEVBUF);
958 contigfree(pAdapter->responsesArrayBaseAddr, RESPONSES_ARRAY_SIZE, M_DEVBUF);
962 AllocatePRDTable(IAL_ADAPTER_T *pAdapter)
965 if (pAdapter->pFreePRDLink) {
966 KdPrint(("pAdapter->pFreePRDLink:%p\n",pAdapter->pFreePRDLink));
967 ret = pAdapter->pFreePRDLink;
968 pAdapter->pFreePRDLink = *(void**)ret;
975 FreePRDTable(IAL_ADAPTER_T *pAdapter, PVOID PRDTable)
977 *(void**)PRDTable = pAdapter->pFreePRDLink;
978 pAdapter->pFreePRDLink = PRDTable;
981 extern PVDevice fGetFirstChild(PVDevice pLogical);
982 extern void fResetBootMark(PVDevice pLogical);
984 fRegisterVdevice(IAL_ADAPTER_T *pAdapter)
986 PVDevice pPhysical, pLogical;
990 for(i=0;i<MV_SATA_CHANNELS_NUM;i++) {
991 pPhysical = &(pAdapter->VDevices[i]);
992 pLogical = pPhysical;
993 while (pLogical->pParent) pLogical = pLogical->pParent;
994 if (pLogical->vf_online==0) {
995 pPhysical->vf_bootmark = pLogical->vf_bootmark = 0;
998 if (pLogical->VDeviceType==VD_SPARE || pPhysical!=fGetFirstChild(pLogical))
1001 pVBus = &pAdapter->VBus;
1005 while(j<MAX_VDEVICE_PER_VBUS && pVBus->pVDevice[j]) j++;
1006 if(j<MAX_VDEVICE_PER_VBUS){
1007 pVBus->pVDevice[j] = pLogical;
1008 pLogical->pVBus = pVBus;
1010 if (j>0 && pLogical->vf_bootmark) {
1011 if (pVBus->pVDevice[0]->vf_bootmark) {
1012 fResetBootMark(pLogical);
1015 do { pVBus->pVDevice[j] = pVBus->pVDevice[j-1]; } while (--j);
1016 pVBus->pVDevice[0] = pLogical;
1025 GetSpareDisk(_VBUS_ARG PVDevice pArray)
1027 IAL_ADAPTER_T *pAdapter = (IAL_ADAPTER_T *)pArray->pVBus->OsExt;
1028 LBA_T capacity = LongDiv(pArray->VDeviceCapacity, pArray->u.array.bArnMember-1);
1029 LBA_T thiscap, maxcap = MAX_LBA_T;
1030 PVDevice pVDevice, pFind = NULL;
1033 for(i=0;i<MV_SATA_CHANNELS_NUM;i++)
1035 pVDevice = &pAdapter->VDevices[i];
1038 thiscap = pArray->vf_format_v2? pVDevice->u.disk.dDeRealCapacity : pVDevice->VDeviceCapacity;
1039 /* find the smallest usable spare disk */
1040 if (pVDevice->VDeviceType==VD_SPARE &&
1041 pVDevice->u.disk.df_on_line &&
1043 thiscap >= capacity)
1045 maxcap = pVDevice->VDeviceCapacity;
1052 /******************************************************************
1054 *******************************************************************/
1056 fDeReadWrite(PDevice pDev, ULONG Lba, UCHAR Cmd, void *tmpBuffer)
1058 return mvReadWrite(pDev->mv, Lba, Cmd, tmpBuffer);
1061 void HPTLIBAPI fDeSelectMode(PDevice pDev, UCHAR NewMode)
1063 MV_SATA_CHANNEL *pSataChannel = pDev->mv;
1064 MV_SATA_ADAPTER *pSataAdapter = pSataChannel->mvSataAdapter;
1065 MV_U8 channelIndex = pSataChannel->channelNumber;
1067 /* 508x don't use MW-DMA? */
1068 if (NewMode>4 && NewMode<8) NewMode = 4;
1069 pDev->bDeModeSetting = NewMode;
1071 mvMode = MV_ATA_TRANSFER_PIO_0 + NewMode;
1073 mvMode = MV_ATA_TRANSFER_UDMA_0 + (NewMode-8);
1075 /*To fix 88i8030 bug*/
1076 if (mvMode > MV_ATA_TRANSFER_UDMA_0 && mvMode < MV_ATA_TRANSFER_UDMA_4)
1077 mvMode = MV_ATA_TRANSFER_UDMA_0;
1079 mvSataDisableChannelDma(pSataAdapter, channelIndex);
1080 /* Flush pending commands */
1081 mvSataFlushDmaQueue (pSataAdapter, channelIndex, MV_FLUSH_TYPE_NONE);
1083 if (mvStorageDevATASetFeatures(pSataAdapter, channelIndex,
1084 MV_ATA_SET_FEATURES_TRANSFER,
1085 mvMode, 0, 0, 0) == MV_FALSE)
1087 KdPrint(("channel %d: Set Features failed\n", channelIndex));
1090 if (mvSataEnableChannelDma(pSataAdapter, channelIndex) == MV_FALSE)
1091 KdPrint(("Failed to enable DMA, channel=%d", channelIndex));
1094 int HPTLIBAPI fDeSetTCQ(PDevice pDev, int enable, int depth)
1096 MV_SATA_CHANNEL *pSataChannel = pDev->mv;
1097 MV_SATA_ADAPTER *pSataAdapter = pSataChannel->mvSataAdapter;
1098 MV_U8 channelIndex = pSataChannel->channelNumber;
1099 IAL_ADAPTER_T *pAdapter = pSataAdapter->IALData;
1100 MV_CHANNEL *channelInfo = &(pAdapter->mvChannel[channelIndex]);
1101 int dmaActive = pSataChannel->queueCommandsEnabled;
1105 mvSataDisableChannelDma(pSataAdapter, channelIndex);
1106 mvSataFlushDmaQueue(pSataAdapter,channelIndex,MV_FLUSH_TYPE_CALLBACK);
1110 if (pSataChannel->queuedDMA == MV_EDMA_MODE_NOT_QUEUED &&
1111 (pSataChannel->identifyDevice[IDEN_SUPPORTED_COMMANDS2] & (0x2))) {
1112 UCHAR depth = ((pSataChannel->identifyDevice[IDEN_QUEUE_DEPTH]) & 0x1f) + 1;
1113 channelInfo->queueDepth = (depth==32)? 31 : depth;
1114 mvSataConfigEdmaMode(pSataAdapter, channelIndex, MV_EDMA_MODE_QUEUED, depth);
1120 if (pSataChannel->queuedDMA != MV_EDMA_MODE_NOT_QUEUED) {
1121 channelInfo->queueDepth = 2;
1122 mvSataConfigEdmaMode(pSataAdapter, channelIndex, MV_EDMA_MODE_NOT_QUEUED, 0);
1128 mvSataEnableChannelDma(pSataAdapter,channelIndex);
1132 int HPTLIBAPI fDeSetNCQ(PDevice pDev, int enable, int depth)
1137 int HPTLIBAPI fDeSetWriteCache(PDevice pDev, int enable)
1139 MV_SATA_CHANNEL *pSataChannel = pDev->mv;
1140 MV_SATA_ADAPTER *pSataAdapter = pSataChannel->mvSataAdapter;
1141 MV_U8 channelIndex = pSataChannel->channelNumber;
1142 IAL_ADAPTER_T *pAdapter = pSataAdapter->IALData;
1143 MV_CHANNEL *channelInfo = &(pAdapter->mvChannel[channelIndex]);
1144 int dmaActive = pSataChannel->queueCommandsEnabled;
1148 mvSataDisableChannelDma(pSataAdapter, channelIndex);
1149 mvSataFlushDmaQueue(pSataAdapter,channelIndex,MV_FLUSH_TYPE_CALLBACK);
1152 if ((pSataChannel->identifyDevice[82] & (0x20))) {
1154 if (mvStorageDevATASetFeatures(pSataAdapter, channelIndex,
1155 MV_ATA_SET_FEATURES_ENABLE_WCACHE, 0, 0, 0, 0))
1157 channelInfo->writeCacheEnabled = MV_TRUE;
1162 if (mvStorageDevATASetFeatures(pSataAdapter, channelIndex,
1163 MV_ATA_SET_FEATURES_DISABLE_WCACHE, 0, 0, 0, 0))
1165 channelInfo->writeCacheEnabled = MV_FALSE;
1172 mvSataEnableChannelDma(pSataAdapter,channelIndex);
1176 int HPTLIBAPI fDeSetReadAhead(PDevice pDev, int enable)
1178 MV_SATA_CHANNEL *pSataChannel = pDev->mv;
1179 MV_SATA_ADAPTER *pSataAdapter = pSataChannel->mvSataAdapter;
1180 MV_U8 channelIndex = pSataChannel->channelNumber;
1181 IAL_ADAPTER_T *pAdapter = pSataAdapter->IALData;
1182 MV_CHANNEL *channelInfo = &(pAdapter->mvChannel[channelIndex]);
1183 int dmaActive = pSataChannel->queueCommandsEnabled;
1187 mvSataDisableChannelDma(pSataAdapter, channelIndex);
1188 mvSataFlushDmaQueue(pSataAdapter,channelIndex,MV_FLUSH_TYPE_CALLBACK);
1191 if ((pSataChannel->identifyDevice[82] & (0x40))) {
1193 if (mvStorageDevATASetFeatures(pSataAdapter, channelIndex,
1194 MV_ATA_SET_FEATURES_ENABLE_RLA, 0, 0, 0, 0))
1196 channelInfo->readAheadEnabled = MV_TRUE;
1201 if (mvStorageDevATASetFeatures(pSataAdapter, channelIndex,
1202 MV_ATA_SET_FEATURES_DISABLE_RLA, 0, 0, 0, 0))
1204 channelInfo->readAheadEnabled = MV_FALSE;
1211 mvSataEnableChannelDma(pSataAdapter,channelIndex);
1215 #ifdef SUPPORT_ARRAY
1216 #define IdeRegisterVDevice fCheckArray
1219 IdeRegisterVDevice(PDevice pDev)
1221 PVDevice pVDev = Map2pVDevice(pDev);
1223 pVDev->VDeviceType = pDev->df_atapi? VD_ATAPI :
1224 pDev->df_removable_drive? VD_REMOVABLE : VD_SINGLE_DISK;
1225 pVDev->vf_online = 1;
1226 pVDev->VDeviceCapacity = pDev->dDeRealCapacity;
1227 pVDev->pfnSendCommand = pfnSendCommand[pVDev->VDeviceType];
1228 pVDev->pfnDeviceFailed = pfnDeviceFailed[pVDev->VDeviceType];
1232 static __inline PBUS_DMAMAP
1233 dmamap_get(struct IALAdapter * pAdapter)
1235 PBUS_DMAMAP p = pAdapter->pbus_dmamap_list;
1237 pAdapter->pbus_dmamap_list = p-> next;
1241 static __inline void
1242 dmamap_put(PBUS_DMAMAP p)
1244 p->next = p->pAdapter->pbus_dmamap_list;
1245 p->pAdapter->pbus_dmamap_list = p;
1248 static int num_adapters = 0;
1250 init_adapter(IAL_ADAPTER_T *pAdapter)
1252 PVBus _vbus_p = &pAdapter->VBus;
1253 MV_SATA_ADAPTER *pMvSataAdapter;
1254 int i, channel, rid;
1258 mtx_init(&pAdapter->lock, "hptsleeplock", NULL, MTX_DEF);
1259 callout_init_mtx(&pAdapter->event_timer_connect, &pAdapter->lock, 0);
1260 callout_init_mtx(&pAdapter->event_timer_disconnect, &pAdapter->lock, 0);
1262 sx_xlock(&hptmv_list_lock);
1265 if(gIal_Adapter == 0){
1266 gIal_Adapter = pAdapter;
1267 pCurAdapter = gIal_Adapter;
1270 pCurAdapter->next = pAdapter;
1271 pCurAdapter = pAdapter;
1273 sx_xunlock(&hptmv_list_lock);
1275 pAdapter->outstandingCommands = 0;
1277 pMvSataAdapter = &(pAdapter->mvSataAdapter);
1278 _vbus_p->OsExt = (void *)pAdapter;
1279 pMvSataAdapter->IALData = pAdapter;
1281 if (bus_dma_tag_create(bus_get_dma_tag(pAdapter->hpt_dev),/* parent */
1283 BUS_SPACE_MAXADDR_32BIT+1, /* boundary */
1284 BUS_SPACE_MAXADDR, /* lowaddr */
1285 BUS_SPACE_MAXADDR, /* highaddr */
1286 NULL, NULL, /* filter, filterarg */
1287 PAGE_SIZE * (MAX_SG_DESCRIPTORS-1), /* maxsize */
1288 MAX_SG_DESCRIPTORS, /* nsegments */
1289 0x10000, /* maxsegsize */
1290 BUS_DMA_WAITOK, /* flags */
1291 busdma_lock_mutex, /* lockfunc */
1292 &pAdapter->lock, /* lockfuncarg */
1293 &pAdapter->io_dma_parent /* tag */))
1299 if (hptmv_allocate_edma_queues(pAdapter))
1301 MV_ERROR("RR18xx: Failed to allocate memory for EDMA queues\n");
1305 /* also map EPROM address */
1307 if (!(pAdapter->mem_res = bus_alloc_resource_any(pAdapter->hpt_dev,
1308 SYS_RES_MEMORY, &rid, RF_ACTIVE))
1310 !(pMvSataAdapter->adapterIoBaseAddress = rman_get_virtual(pAdapter->mem_res)))
1312 MV_ERROR("RR18xx: Failed to remap memory space\n");
1313 hptmv_free_edma_queues(pAdapter);
1318 KdPrint(("RR18xx: io base address 0x%p\n", pMvSataAdapter->adapterIoBaseAddress));
1321 pMvSataAdapter->adapterId = num_adapters++;
1322 /* get the revision ID */
1323 pMvSataAdapter->pciConfigRevisionId = pci_read_config(pAdapter->hpt_dev, PCIR_REVID, 1);
1324 pMvSataAdapter->pciConfigDeviceId = pci_get_device(pAdapter->hpt_dev);
1327 pMvSataAdapter->intCoalThre[0]= 1;
1328 pMvSataAdapter->intCoalThre[1]= 1;
1329 pMvSataAdapter->intTimeThre[0] = 1;
1330 pMvSataAdapter->intTimeThre[1] = 1;
1331 pMvSataAdapter->pciCommand = 0x0107E371;
1332 pMvSataAdapter->pciSerrMask = 0xd77fe6ul;
1333 pMvSataAdapter->pciInterruptMask = 0xd77fe6ul;
1334 pMvSataAdapter->mvSataEventNotify = hptmv_event_notify;
1336 if (mvSataInitAdapter(pMvSataAdapter) == MV_FALSE)
1338 MV_ERROR("RR18xx[%d]: core failed to initialize the adapter\n",
1339 pMvSataAdapter->adapterId);
1341 bus_release_resource(pAdapter->hpt_dev, SYS_RES_MEMORY, rid, pAdapter->mem_res);
1342 hptmv_free_edma_queues(pAdapter);
1345 pAdapter->ver_601 = pMvSataAdapter->pcbVersion;
1348 set_fail_leds(pMvSataAdapter, 0);
1351 /* setup command blocks */
1352 KdPrint(("Allocate command blocks\n"));
1353 _vbus_(pFreeCommands) = 0;
1354 pAdapter->pCommandBlocks =
1355 malloc(sizeof(struct _Command) * MAX_COMMAND_BLOCKS_FOR_EACH_VBUS, M_DEVBUF, M_NOWAIT);
1356 KdPrint(("pCommandBlocks:%p\n",pAdapter->pCommandBlocks));
1357 if (!pAdapter->pCommandBlocks) {
1358 MV_ERROR("insufficient memory\n");
1362 for (i=0; i<MAX_COMMAND_BLOCKS_FOR_EACH_VBUS; i++) {
1363 FreeCommand(_VBUS_P &(pAdapter->pCommandBlocks[i]));
1366 /*Set up the bus_dmamap*/
1367 pAdapter->pbus_dmamap = (PBUS_DMAMAP)malloc (sizeof(struct _BUS_DMAMAP) * MAX_QUEUE_COMM, M_DEVBUF, M_NOWAIT);
1368 if(!pAdapter->pbus_dmamap) {
1369 MV_ERROR("insufficient memory\n");
1370 free(pAdapter->pCommandBlocks, M_DEVBUF);
1374 memset((void *)pAdapter->pbus_dmamap, 0, sizeof(struct _BUS_DMAMAP) * MAX_QUEUE_COMM);
1375 pAdapter->pbus_dmamap_list = 0;
1376 for (i=0; i < MAX_QUEUE_COMM; i++) {
1377 PBUS_DMAMAP pmap = &(pAdapter->pbus_dmamap[i]);
1378 pmap->pAdapter = pAdapter;
1381 if(bus_dmamap_create(pAdapter->io_dma_parent, 0, &pmap->dma_map)) {
1382 MV_ERROR("Can not allocate dma map\n");
1383 free(pAdapter->pCommandBlocks, M_DEVBUF);
1384 free(pAdapter->pbus_dmamap, M_DEVBUF);
1387 callout_init_mtx(&pmap->timeout, &pAdapter->lock, 0);
1389 /* setup PRD Tables */
1390 KdPrint(("Allocate PRD Tables\n"));
1391 pAdapter->pFreePRDLink = 0;
1393 pAdapter->prdTableAddr = (PUCHAR)contigmalloc(
1394 (PRD_ENTRIES_SIZE*PRD_TABLES_FOR_VBUS + 32), M_DEVBUF, M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0ul);
1396 KdPrint(("prdTableAddr:%p\n",pAdapter->prdTableAddr));
1397 if (!pAdapter->prdTableAddr) {
1398 MV_ERROR("insufficient PRD Tables\n");
1401 pAdapter->prdTableAlignedAddr = (PUCHAR)(((ULONG_PTR)pAdapter->prdTableAddr + 0x1f) & ~(ULONG_PTR)0x1fL);
1403 PUCHAR PRDTable = pAdapter->prdTableAlignedAddr;
1404 for (i=0; i<PRD_TABLES_FOR_VBUS; i++)
1406 /* KdPrint(("i=%d,pAdapter->pFreePRDLink=%p\n",i,pAdapter->pFreePRDLink)); */
1407 FreePRDTable(pAdapter, PRDTable);
1408 PRDTable += PRD_ENTRIES_SIZE;
1412 /* enable the adapter interrupts */
1414 /* configure and start the connected channels*/
1415 for (channel = 0; channel < MV_SATA_CHANNELS_NUM; channel++)
1417 pAdapter->mvChannel[channel].online = MV_FALSE;
1418 if (mvSataIsStorageDeviceConnected(pMvSataAdapter, channel)
1421 KdPrint(("RR18xx[%d]: channel %d is connected\n",
1422 pMvSataAdapter->adapterId, channel));
1424 if (hptmv_init_channel(pAdapter, channel) == 0)
1426 if (mvSataConfigureChannel(pMvSataAdapter, channel) == MV_FALSE)
1428 MV_ERROR("RR18xx[%d]: Failed to configure channel"
1429 " %d\n",pMvSataAdapter->adapterId, channel);
1430 hptmv_free_channel(pAdapter, channel);
1434 if (start_channel(pAdapter, channel))
1436 MV_ERROR("RR18xx[%d]: Failed to start channel,"
1437 " channel=%d\n",pMvSataAdapter->adapterId,
1439 hptmv_free_channel(pAdapter, channel);
1441 pAdapter->mvChannel[channel].online = MV_TRUE;
1442 /* mvSataChannelSetEdmaLoopBackMode(pMvSataAdapter,
1448 KdPrint(("pAdapter->mvChannel[channel].online:%x, channel:%d\n",
1449 pAdapter->mvChannel[channel].online, channel));
1452 #ifdef SUPPORT_ARRAY
1453 for(i = MAX_ARRAY_DEVICE - 1; i >= 0; i--) {
1454 pVDev = ArrayTables(i);
1455 mArFreeArrayTable(pVDev);
1459 KdPrint(("Initialize Devices\n"));
1460 for (channel = 0; channel < MV_SATA_CHANNELS_NUM; channel++) {
1461 MV_SATA_CHANNEL *pMvSataChannel = pMvSataAdapter->sataChannel[channel];
1462 if (pMvSataChannel) {
1463 init_vdev_params(pAdapter, channel);
1464 IdeRegisterVDevice(&pAdapter->VDevices[channel].u.disk);
1467 #ifdef SUPPORT_ARRAY
1468 CheckArrayCritical(_VBUS_P0);
1470 _vbus_p->nInstances = 1;
1471 fRegisterVdevice(pAdapter);
1473 for (channel=0;channel<MV_SATA_CHANNELS_NUM;channel++) {
1474 pVDev = _vbus_p->pVDevice[channel];
1475 if (pVDev && pVDev->vf_online)
1476 fCheckBootable(pVDev);
1479 #if defined(SUPPORT_ARRAY) && defined(_RAID5N_)
1480 init_raid5_memory(_VBUS_P0);
1481 _vbus_(r5).enable_write_back = 1;
1482 printf("RR18xx: RAID5 write-back %s\n", _vbus_(r5).enable_write_back? "enabled" : "disabled");
1485 mvSataUnmaskAdapterInterrupt(pMvSataAdapter);
1490 MvSataResetChannel(MV_SATA_ADAPTER *pMvSataAdapter, MV_U8 channel)
1492 IAL_ADAPTER_T *pAdapter = (IAL_ADAPTER_T *)pMvSataAdapter->IALData;
1494 mvSataDisableChannelDma(pMvSataAdapter, channel);
1495 /* Flush pending commands */
1496 mvSataFlushDmaQueue (pMvSataAdapter, channel, MV_FLUSH_TYPE_CALLBACK);
1498 /* Software reset channel */
1499 if (mvStorageDevATASoftResetDevice(pMvSataAdapter, channel) == MV_FALSE)
1501 MV_ERROR("RR18xx [%d,%d]: failed to perform Software reset\n",
1502 pMvSataAdapter->adapterId, channel);
1503 hptmv_free_channel(pAdapter, channel);
1507 /* Hardware reset channel */
1508 if (mvSataChannelHardReset(pMvSataAdapter, channel)== MV_FALSE)
1510 MV_ERROR("RR18xx [%d,%d] Failed to Hard reser the SATA channel\n",
1511 pMvSataAdapter->adapterId, channel);
1512 hptmv_free_channel(pAdapter, channel);
1516 if (mvSataIsStorageDeviceConnected(pMvSataAdapter, channel) == MV_FALSE)
1518 MV_ERROR("RR18xx [%d,%d] Failed to Connect Device\n",
1519 pMvSataAdapter->adapterId, channel);
1520 hptmv_free_channel(pAdapter, channel);
1524 MV_ERROR("channel %d: perform recalibrate command", channel);
1525 if (!mvStorageDevATAExecuteNonUDMACommand(pMvSataAdapter, channel,
1526 MV_NON_UDMA_PROTOCOL_NON_DATA,
1540 MV_ERROR("channel %d: recalibrate failed", channel);
1542 /* Set transfer mode */
1543 if((mvStorageDevATASetFeatures(pMvSataAdapter, channel,
1544 MV_ATA_SET_FEATURES_TRANSFER,
1545 MV_ATA_TRANSFER_PIO_SLOW, 0, 0, 0) == MV_FALSE) ||
1546 (mvStorageDevATASetFeatures(pMvSataAdapter, channel,
1547 MV_ATA_SET_FEATURES_TRANSFER,
1548 pAdapter->mvChannel[channel].maxPioModeSupported, 0, 0, 0) == MV_FALSE) ||
1549 (mvStorageDevATASetFeatures(pMvSataAdapter, channel,
1550 MV_ATA_SET_FEATURES_TRANSFER,
1551 pAdapter->mvChannel[channel].maxUltraDmaModeSupported, 0, 0, 0) == MV_FALSE) )
1553 MV_ERROR("channel %d: Set Features failed", channel);
1554 hptmv_free_channel(pAdapter, channel);
1558 if (mvSataEnableChannelDma(pMvSataAdapter, channel) == MV_FALSE)
1560 MV_ERROR("Failed to enable DMA, channel=%d", channel);
1561 hptmv_free_channel(pAdapter, channel);
1569 fResetActiveCommands(PVBus _vbus_p)
1571 MV_SATA_ADAPTER *pMvSataAdapter = &((IAL_ADAPTER_T *)_vbus_p->OsExt)->mvSataAdapter;
1573 for (channel=0;channel< MV_SATA_CHANNELS_NUM;channel++) {
1574 if (pMvSataAdapter->sataChannel[channel] && pMvSataAdapter->sataChannel[channel]->outstandingCommands)
1575 MvSataResetChannel(pMvSataAdapter,channel);
1580 void fCompleteAllCommandsSynchronously(PVBus _vbus_p)
1585 MV_SATA_ADAPTER *pMvSataAdapter = &((IAL_ADAPTER_T *)_vbus_p->OsExt)->mvSataAdapter;
1586 MV_SATA_CHANNEL *pMvSataChannel;
1591 CheckPendingCall(_VBUS_P0);
1596 for (channel=0;channel< MV_SATA_CHANNELS_NUM;channel++) {
1597 pMvSataChannel = pMvSataAdapter->sataChannel[channel];
1598 if (pMvSataChannel && pMvSataChannel->outstandingCommands)
1600 while (pMvSataChannel->outstandingCommands) {
1601 if (!mvSataInterruptServiceRoutine(pMvSataAdapter)) {
1603 if (ticks++ > 3000) {
1604 MvSataResetChannel(pMvSataAdapter,channel);
1618 fResetVBus(_VBUS_ARG0)
1620 KdPrint(("fMvResetBus(%p)", _vbus_p));
1622 /* some commands may already finished. */
1623 CheckPendingCall(_VBUS_P0);
1625 fResetActiveCommands(_vbus_p);
1627 * the other pending commands may still be finished successfully.
1629 fCompleteAllCommandsSynchronously(_vbus_p);
1631 /* Now there should be no pending commands. No more action needed. */
1632 CheckIdleCall(_VBUS_P0);
1634 KdPrint(("fMvResetBus() done"));
1637 /*No rescan function*/
1639 fRescanAllDevice(_VBUS_ARG0)
1644 CommandCompletionCB(MV_SATA_ADAPTER *pMvSataAdapter,
1646 MV_COMPLETION_TYPE comp_type,
1647 MV_VOID_PTR commandId,
1648 MV_U16 responseFlags,
1650 MV_STORAGE_DEVICE_REGISTERS *registerStruct)
1652 PCommand pCmd = (PCommand) commandId;
1653 _VBUS_INST(pCmd->pVDevice->pVBus)
1655 if (pCmd->uScratch.sata_param.prdAddr)
1656 FreePRDTable(pMvSataAdapter->IALData,pCmd->uScratch.sata_param.prdAddr);
1660 case MV_COMPLETION_TYPE_NORMAL:
1661 pCmd->Result = RETURN_SUCCESS;
1663 case MV_COMPLETION_TYPE_ABORT:
1664 pCmd->Result = RETURN_BUS_RESET;
1666 case MV_COMPLETION_TYPE_ERROR:
1667 MV_ERROR("IAL: COMPLETION ERROR, adapter %d, channel %d, flags=%x\n",
1668 pMvSataAdapter->adapterId, channelNum, responseFlags);
1670 if (responseFlags & 4) {
1671 MV_ERROR("ATA regs: error %x, sector count %x, LBA low %x, LBA mid %x,"
1672 " LBA high %x, device %x, status %x\n",
1673 registerStruct->errorRegister,
1674 registerStruct->sectorCountRegister,
1675 registerStruct->lbaLowRegister,
1676 registerStruct->lbaMidRegister,
1677 registerStruct->lbaHighRegister,
1678 registerStruct->deviceRegister,
1679 registerStruct->statusRegister);
1681 /*We can't do handleEdmaError directly here, because CommandCompletionCB is called by
1682 * mv's ISR, if we retry the command, than the internel data structure may be destroyed*/
1683 pCmd->uScratch.sata_param.responseFlags = responseFlags;
1684 pCmd->uScratch.sata_param.bIdeStatus = registerStruct->statusRegister;
1685 pCmd->uScratch.sata_param.errorRegister = registerStruct->errorRegister;
1686 pCmd->pVDevice->u.disk.QueueLength--;
1687 CallAfterReturn(_VBUS_P (DPC_PROC)handleEdmaError,pCmd);
1691 MV_ERROR(" Unknown completion type (%d)\n", comp_type);
1695 if (pCmd->uCmd.Ide.Command == IDE_COMMAND_VERIFY && pCmd->uScratch.sata_param.cmd_priv > 1) {
1696 pCmd->uScratch.sata_param.cmd_priv --;
1699 pCmd->pVDevice->u.disk.QueueLength--;
1700 CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
1705 fDeviceSendCommand(_VBUS_ARG PCommand pCmd)
1707 MV_SATA_EDMA_PRD_ENTRY *pPRDTable = 0;
1708 MV_SATA_ADAPTER *pMvSataAdapter;
1709 MV_SATA_CHANNEL *pMvSataChannel;
1710 PVDevice pVDevice = pCmd->pVDevice;
1711 PDevice pDevice = &pVDevice->u.disk;
1712 LBA_T Lba = pCmd->uCmd.Ide.Lba;
1713 USHORT nSector = pCmd->uCmd.Ide.nSectors;
1715 MV_QUEUE_COMMAND_RESULT result;
1716 MV_QUEUE_COMMAND_INFO commandInfo;
1717 MV_UDMA_COMMAND_PARAMS *pUdmaParams = &commandInfo.commandParams.udmaCommand;
1718 MV_NONE_UDMA_COMMAND_PARAMS *pNoUdmaParams = &commandInfo.commandParams.NoneUdmaCommand;
1724 DECLARE_BUFFER(FPSCAT_GATH, tmpSg);
1726 if (!pDevice->df_on_line) {
1727 MV_ERROR("Device is offline");
1728 pCmd->Result = RETURN_BAD_DEVICE;
1729 CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
1733 pDevice->HeadPosition = pCmd->uCmd.Ide.Lba + pCmd->uCmd.Ide.nSectors;
1734 pMvSataChannel = pDevice->mv;
1735 pMvSataAdapter = pMvSataChannel->mvSataAdapter;
1736 channel = pMvSataChannel->channelNumber;
1738 /* old RAID0 has hidden lba. Remember to clear dDeHiddenLba when delete array! */
1739 Lba += pDevice->dDeHiddenLba;
1741 if (Lba+nSector-1 > pDevice->dDeRealCapacity) {
1742 pCmd->Result = RETURN_INVALID_REQUEST;
1743 CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
1748 * always use 48bit LBA if drive supports it.
1749 * Some Seagate drives report error if you use a 28-bit command
1750 * to access sector 0xfffffff.
1752 is48bit = pMvSataChannel->lba48Address;
1754 switch (pCmd->uCmd.Ide.Command)
1756 case IDE_COMMAND_READ:
1757 case IDE_COMMAND_WRITE:
1758 if (pDevice->bDeModeSetting<8) goto pio;
1760 commandInfo.type = MV_QUEUED_COMMAND_TYPE_UDMA;
1761 pUdmaParams->isEXT = is48bit;
1762 pUdmaParams->numOfSectors = nSector;
1763 pUdmaParams->lowLBAAddress = Lba;
1764 pUdmaParams->highLBAAddress = 0;
1765 pUdmaParams->prdHighAddr = 0;
1766 pUdmaParams->callBack = CommandCompletionCB;
1767 pUdmaParams->commandId = (MV_VOID_PTR )pCmd;
1768 if(pCmd->uCmd.Ide.Command == IDE_COMMAND_READ)
1769 pUdmaParams->readWrite = MV_UDMA_TYPE_READ;
1771 pUdmaParams->readWrite = MV_UDMA_TYPE_WRITE;
1773 if (pCmd->pSgTable && pCmd->cf_physical_sg) {
1774 FPSCAT_GATH sg1=tmpSg, sg2=pCmd->pSgTable;
1775 do { *sg1++=*sg2; } while ((sg2++->wSgFlag & SG_FLAG_EOT)==0);
1778 if (!pCmd->pfnBuildSgl || !pCmd->pfnBuildSgl(_VBUS_P pCmd, tmpSg, 0)) {
1780 mvSataDisableChannelDma(pMvSataAdapter, channel);
1781 mvSataFlushDmaQueue(pMvSataAdapter, channel, MV_FLUSH_TYPE_CALLBACK);
1783 if (pCmd->pSgTable && pCmd->cf_physical_sg==0) {
1784 FPSCAT_GATH sg1=tmpSg, sg2=pCmd->pSgTable;
1785 do { *sg1++=*sg2; } while ((sg2++->wSgFlag & SG_FLAG_EOT)==0);
1788 if (!pCmd->pfnBuildSgl || !pCmd->pfnBuildSgl(_VBUS_P pCmd, tmpSg, 1)) {
1789 pCmd->Result = RETURN_NEED_LOGICAL_SG;
1795 ULONG size = tmpSg->wSgSize? tmpSg->wSgSize : 0x10000;
1796 ULONG_PTR addr = tmpSg->dSgAddress;
1798 pCmd->Result = RETURN_INVALID_REQUEST;
1801 if (mvStorageDevATAExecuteNonUDMACommand(pMvSataAdapter, channel,
1802 (pCmd->cf_data_out)?MV_NON_UDMA_PROTOCOL_PIO_DATA_OUT:MV_NON_UDMA_PROTOCOL_PIO_DATA_IN,
1805 size >> 1, /* count */
1806 0, /* features N/A */
1807 (MV_U16)(size>>9), /*sector count*/
1808 (MV_U16)( (is48bit? (MV_U16)((Lba >> 16) & 0xFF00) : 0 ) | (UCHAR)(Lba & 0xFF) ), /*lbalow*/
1809 (MV_U16)((Lba >> 8) & 0xFF), /* lbaMid */
1810 (MV_U16)((Lba >> 16) & 0xFF),/* lbaHigh */
1811 (MV_U8)(0x40 | (is48bit ? 0 : (UCHAR)(Lba >> 24) & 0xFF )),/* device */
1812 (MV_U8)(is48bit ? (pCmd->cf_data_in?IDE_COMMAND_READ_EXT:IDE_COMMAND_WRITE_EXT):pCmd->uCmd.Ide.Command)
1815 pCmd->Result = RETURN_IDE_ERROR;
1819 if(Lba & 0xF0000000) is48bit = MV_TRUE;
1821 while ((tmpSg++->wSgFlag & SG_FLAG_EOT)==0);
1822 pCmd->Result = RETURN_SUCCESS;
1824 mvSataEnableChannelDma(pMvSataAdapter,channel);
1825 CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
1830 pPRDTable = (MV_SATA_EDMA_PRD_ENTRY *) AllocatePRDTable(pMvSataAdapter->IALData);
1831 KdPrint(("pPRDTable:%p\n",pPRDTable));
1833 pCmd->Result = RETURN_DEVICE_BUSY;
1834 CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
1840 pPRDTable[i].highBaseAddr = (sizeof(tmpSg->dSgAddress)>4 ? (MV_U32)(tmpSg->dSgAddress>>32) : 0);
1841 pPRDTable[i].flags = (MV_U16)tmpSg->wSgFlag;
1842 pPRDTable[i].byteCount = (MV_U16)tmpSg->wSgSize;
1843 pPRDTable[i].lowBaseAddr = (MV_U32)tmpSg->dSgAddress;
1844 pPRDTable[i].reserved = 0;
1846 }while((tmpSg++->wSgFlag & SG_FLAG_EOT)==0);
1848 pUdmaParams->prdLowAddr = (ULONG)fOsPhysicalAddress(pPRDTable);
1849 if ((pUdmaParams->numOfSectors == 256) && (pMvSataChannel->lba48Address == MV_FALSE)) {
1850 pUdmaParams->numOfSectors = 0;
1853 pCmd->uScratch.sata_param.prdAddr = (PVOID)pPRDTable;
1855 result = mvSataQueueCommand(pMvSataAdapter, channel, &commandInfo);
1857 if (result != MV_QUEUE_COMMAND_RESULT_OK)
1862 case MV_QUEUE_COMMAND_RESULT_BAD_LBA_ADDRESS:
1863 MV_ERROR("IAL Error: Edma Queue command failed. Bad LBA "
1864 "LBA[31:0](0x%08x)\n", pUdmaParams->lowLBAAddress);
1865 pCmd->Result = RETURN_IDE_ERROR;
1867 case MV_QUEUE_COMMAND_RESULT_QUEUED_MODE_DISABLED:
1868 MV_ERROR("IAL Error: Edma Queue command failed. EDMA"
1869 " disabled adapter %d channel %d\n",
1870 pMvSataAdapter->adapterId, channel);
1871 mvSataEnableChannelDma(pMvSataAdapter,channel);
1872 pCmd->Result = RETURN_IDE_ERROR;
1874 case MV_QUEUE_COMMAND_RESULT_FULL:
1875 MV_ERROR("IAL Error: Edma Queue command failed. Queue is"
1876 " Full adapter %d channel %d\n",
1877 pMvSataAdapter->adapterId, channel);
1878 pCmd->Result = RETURN_DEVICE_BUSY;
1880 case MV_QUEUE_COMMAND_RESULT_BAD_PARAMS:
1881 MV_ERROR("IAL Error: Edma Queue command failed. (Bad "
1882 "Params), pMvSataAdapter: %p, pSataChannel: %p.\n",
1883 pMvSataAdapter, pMvSataAdapter->sataChannel[channel]);
1884 pCmd->Result = RETURN_IDE_ERROR;
1887 MV_ERROR("IAL Error: Bad result value (%d) from queue"
1888 " command\n", result);
1889 pCmd->Result = RETURN_IDE_ERROR;
1892 FreePRDTable(pMvSataAdapter->IALData,pPRDTable);
1893 CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
1895 pDevice->QueueLength++;
1898 case IDE_COMMAND_VERIFY:
1899 commandInfo.type = MV_QUEUED_COMMAND_TYPE_NONE_UDMA;
1900 pNoUdmaParams->bufPtr = NULL;
1901 pNoUdmaParams->callBack = CommandCompletionCB;
1902 pNoUdmaParams->commandId = (MV_VOID_PTR)pCmd;
1903 pNoUdmaParams->count = 0;
1904 pNoUdmaParams->features = 0;
1905 pNoUdmaParams->protocolType = MV_NON_UDMA_PROTOCOL_NON_DATA;
1907 pCmd->uScratch.sata_param.cmd_priv = 1;
1908 if (pMvSataChannel->lba48Address == MV_TRUE){
1909 pNoUdmaParams->command = MV_ATA_COMMAND_READ_VERIFY_SECTORS_EXT;
1910 pNoUdmaParams->isEXT = MV_TRUE;
1911 pNoUdmaParams->lbaHigh = (MV_U16)((Lba & 0xff0000) >> 16);
1912 pNoUdmaParams->lbaMid = (MV_U16)((Lba & 0xff00) >> 8);
1913 pNoUdmaParams->lbaLow =
1914 (MV_U16)(((Lba & 0xff000000) >> 16)| (Lba & 0xff));
1915 pNoUdmaParams->sectorCount = nSector;
1916 pNoUdmaParams->device = 0x40;
1917 result = mvSataQueueCommand(pMvSataAdapter, channel, &commandInfo);
1918 if (result != MV_QUEUE_COMMAND_RESULT_OK){
1924 pNoUdmaParams->command = MV_ATA_COMMAND_READ_VERIFY_SECTORS;
1925 pNoUdmaParams->isEXT = MV_FALSE;
1926 pNoUdmaParams->lbaHigh = (MV_U16)((Lba & 0xff0000) >> 16);
1927 pNoUdmaParams->lbaMid = (MV_U16)((Lba & 0xff00) >> 8);
1928 pNoUdmaParams->lbaLow = (MV_U16)(Lba & 0xff);
1929 pNoUdmaParams->sectorCount = 0xff & nSector;
1930 pNoUdmaParams->device = (MV_U8)(0x40 |
1931 ((Lba & 0xf000000) >> 24));
1932 pNoUdmaParams->callBack = CommandCompletionCB;
1933 result = mvSataQueueCommand(pMvSataAdapter, channel, &commandInfo);
1934 /*FIXME: how about the commands already queued? but marvel also forgets to consider this*/
1935 if (result != MV_QUEUE_COMMAND_RESULT_OK){
1941 pCmd->Result = RETURN_INVALID_REQUEST;
1942 CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd);
1947 /**********************************************************
1949 * Probe the hostadapter.
1951 **********************************************************/
1953 hpt_probe(device_t dev)
1955 if ((pci_get_vendor(dev) == MV_SATA_VENDOR_ID) &&
1956 (pci_get_device(dev) == MV_SATA_DEVICE_ID_5081
1958 || pci_get_device(dev) == MV_SATA_DEVICE_ID_5080
1962 KdPrintI((CONTROLLER_NAME " found\n"));
1963 device_set_desc(dev, CONTROLLER_NAME);
1964 return (BUS_PROBE_DEFAULT);
1970 /***********************************************************
1972 * Auto configuration: attach and init a host adapter.
1974 ***********************************************************/
1976 hpt_attach(device_t dev)
1978 IAL_ADAPTER_T * pAdapter = device_get_softc(dev);
1981 struct cam_devq *devq;
1982 struct cam_sim *hpt_vsim;
1984 device_printf(dev, "%s Version %s \n", DRIVER_NAME, DRIVER_VERSION);
1986 pAdapter->hpt_dev = dev;
1988 rid = init_adapter(pAdapter);
1993 if ((pAdapter->hpt_irq = bus_alloc_resource_any(pAdapter->hpt_dev, SYS_RES_IRQ, &rid, RF_SHAREABLE | RF_ACTIVE)) == NULL)
1995 hpt_printk(("can't allocate interrupt\n"));
1999 if (bus_setup_intr(pAdapter->hpt_dev, pAdapter->hpt_irq,
2000 INTR_TYPE_CAM | INTR_MPSAFE,
2001 NULL, hpt_intr, pAdapter, &pAdapter->hpt_intr))
2003 hpt_printk(("can't set up interrupt\n"));
2004 free(pAdapter, M_DEVBUF);
2009 if((ccb = (union ccb *)malloc(sizeof(*ccb), M_DEVBUF, M_WAITOK)) != (union ccb*)NULL)
2011 bzero(ccb, sizeof(*ccb));
2012 ccb->ccb_h.pinfo.priority = 1;
2013 ccb->ccb_h.pinfo.index = CAM_UNQUEUED_INDEX;
2020 * Create the device queue for our SIM(s).
2022 if((devq = cam_simq_alloc(8/*MAX_QUEUE_COMM*/)) == NULL)
2024 KdPrint(("ENXIO\n"));
2029 * Construct our SIM entry
2031 hpt_vsim = cam_sim_alloc(hpt_action, hpt_poll, __str(PROC_DIR_NAME),
2032 pAdapter, device_get_unit(pAdapter->hpt_dev),
2033 &pAdapter->lock, 1, 8, devq);
2034 if (hpt_vsim == NULL) {
2035 cam_simq_free(devq);
2039 mtx_lock(&pAdapter->lock);
2040 if (xpt_bus_register(hpt_vsim, dev, 0) != CAM_SUCCESS)
2042 cam_sim_free(hpt_vsim, /*free devq*/ TRUE);
2043 mtx_unlock(&pAdapter->lock);
2048 if(xpt_create_path(&pAdapter->path, /*periph */ NULL,
2049 cam_sim_path(hpt_vsim), CAM_TARGET_WILDCARD,
2050 CAM_LUN_WILDCARD) != CAM_REQ_CMP)
2052 xpt_bus_deregister(cam_sim_path(hpt_vsim));
2053 cam_sim_free(hpt_vsim, /*free_devq*/TRUE);
2054 mtx_unlock(&pAdapter->lock);
2058 mtx_unlock(&pAdapter->lock);
2060 xpt_setup_ccb(&(ccb->ccb_h), pAdapter->path, /*priority*/5);
2061 ccb->ccb_h.func_code = XPT_SASYNC_CB;
2062 ccb->csa.event_enable = AC_LOST_DEVICE;
2063 ccb->csa.callback = hpt_async;
2064 ccb->csa.callback_arg = hpt_vsim;
2065 xpt_action((union ccb *)ccb);
2066 free(ccb, M_DEVBUF);
2068 if (device_get_unit(dev) == 0) {
2069 /* Start the work thread. XXX */
2070 launch_worker_thread();
2077 hpt_detach(device_t dev)
2083 /***************************************************************
2084 * The poll function is used to simulate the interrupt when
2085 * the interrupt subsystem is not functioning.
2087 ***************************************************************/
2089 hpt_poll(struct cam_sim *sim)
2091 IAL_ADAPTER_T *pAdapter;
2093 pAdapter = cam_sim_softc(sim);
2095 hpt_intr_locked((void *)cam_sim_softc(sim));
2098 /****************************************************************
2100 * Description: Interrupt handler.
2101 ****************************************************************/
2105 IAL_ADAPTER_T *pAdapter;
2108 mtx_lock(&pAdapter->lock);
2109 hpt_intr_locked(pAdapter);
2110 mtx_unlock(&pAdapter->lock);
2114 hpt_intr_locked(IAL_ADAPTER_T *pAdapter)
2117 mtx_assert(&pAdapter->lock, MA_OWNED);
2118 /* KdPrintI(("----- Entering Isr() -----\n")); */
2119 if (mvSataInterruptServiceRoutine(&pAdapter->mvSataAdapter) == MV_TRUE)
2121 _VBUS_INST(&pAdapter->VBus)
2122 CheckPendingCall(_VBUS_P0);
2125 /* KdPrintI(("----- Leaving Isr() -----\n")); */
2128 /**********************************************************
2129 * Asynchronous Events
2130 *********************************************************/
2131 #if (!defined(UNREFERENCED_PARAMETER))
2132 #define UNREFERENCED_PARAMETER(x) (void)(x)
2136 hpt_async(void * callback_arg, u_int32_t code, struct cam_path * path,
2141 UNREFERENCED_PARAMETER(callback_arg);
2142 UNREFERENCED_PARAMETER(code);
2143 UNREFERENCED_PARAMETER(path);
2144 UNREFERENCED_PARAMETER(arg);
2149 FlushAdapter(IAL_ADAPTER_T *pAdapter)
2153 hpt_printk(("flush all devices\n"));
2155 /* flush all devices */
2156 for (i=0; i<MAX_VDEVICE_PER_VBUS; i++) {
2157 PVDevice pVDev = pAdapter->VBus.pVDevice[i];
2158 if(pVDev) fFlushVDev(pVDev);
2163 hpt_shutdown(device_t dev)
2165 IAL_ADAPTER_T *pAdapter;
2167 pAdapter = device_get_softc(dev);
2169 EVENTHANDLER_DEREGISTER(shutdown_final, pAdapter->eh);
2170 mtx_lock(&pAdapter->lock);
2171 FlushAdapter(pAdapter);
2172 mtx_unlock(&pAdapter->lock);
2173 /* give the flush some time to happen,
2174 *otherwise "shutdown -p now" will make file system corrupted */
2175 DELAY(1000 * 1000 * 5);
2180 Check_Idle_Call(IAL_ADAPTER_T *pAdapter)
2182 _VBUS_INST(&pAdapter->VBus)
2184 if (mWaitingForIdle(_VBUS_P0)) {
2185 CheckIdleCall(_VBUS_P0);
2186 #ifdef SUPPORT_ARRAY
2190 for(i = 0; i < MAX_ARRAY_PER_VBUS; i++){
2191 if ((pArray=ArrayTables(i))->u.array.dArStamp==0)
2193 else if (pArray->u.array.rf_auto_rebuild) {
2194 KdPrint(("auto rebuild.\n"));
2195 pArray->u.array.rf_auto_rebuild = 0;
2196 hpt_queue_dpc((HPT_DPC)hpt_rebuild_data_block, pAdapter, pArray, DUPLICATE);
2202 /* launch the awaiting commands blocked by mWaitingForIdle */
2203 while(pAdapter->pending_Q!= NULL)
2205 _VBUS_INST(&pAdapter->VBus)
2206 union ccb *ccb = (union ccb *)pAdapter->pending_Q->ccb_h.ccb_ccb_ptr;
2207 hpt_free_ccb(&pAdapter->pending_Q, ccb);
2208 CallAfterReturn(_VBUS_P (DPC_PROC)OsSendCommand, ccb);
2213 ccb_done(union ccb *ccb)
2215 PBUS_DMAMAP pmap = (PBUS_DMAMAP)ccb->ccb_adapter;
2216 IAL_ADAPTER_T * pAdapter = pmap->pAdapter;
2217 KdPrintI(("ccb_done: ccb %p status %x\n", ccb, ccb->ccb_h.status));
2222 pAdapter->outstandingCommands--;
2224 if (pAdapter->outstandingCommands == 0)
2226 if(DPC_Request_Nums == 0)
2227 Check_Idle_Call(pAdapter);
2232 /****************************************************************
2234 * Description: Process a queued command from the CAM layer.
2235 * Parameters: sim - Pointer to SIM object
2236 * ccb - Pointer to SCSI command structure.
2237 ****************************************************************/
2240 hpt_action(struct cam_sim *sim, union ccb *ccb)
2242 IAL_ADAPTER_T * pAdapter = (IAL_ADAPTER_T *) cam_sim_softc(sim);
2244 _VBUS_INST(&pAdapter->VBus)
2246 mtx_assert(&pAdapter->lock, MA_OWNED);
2247 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("hpt_action\n"));
2248 KdPrint(("hpt_action(%lx,%lx{%x})\n", (u_long)sim, (u_long)ccb, ccb->ccb_h.func_code));
2250 switch (ccb->ccb_h.func_code)
2252 case XPT_SCSI_IO: /* Execute the requested I/O operation */
2254 /* ccb->ccb_h.path_id is not our bus id - don't check it */
2256 if (ccb->ccb_h.target_lun) {
2257 ccb->ccb_h.status = CAM_LUN_INVALID;
2261 if (ccb->ccb_h.target_id >= MAX_VDEVICE_PER_VBUS ||
2262 pAdapter->VBus.pVDevice[ccb->ccb_h.target_id]==0) {
2263 ccb->ccb_h.status = CAM_TID_INVALID;
2268 if (pAdapter->outstandingCommands==0 && DPC_Request_Nums==0)
2269 Check_Idle_Call(pAdapter);
2271 pmap = dmamap_get(pAdapter);
2273 ccb->ccb_adapter = pmap;
2274 memset((void *)pmap->psg, 0, sizeof(pmap->psg));
2276 if (mWaitingForIdle(_VBUS_P0))
2277 hpt_queue_ccb(&pAdapter->pending_Q, ccb);
2279 OsSendCommand(_VBUS_P ccb);
2281 /* KdPrint(("leave scsiio\n")); */
2286 KdPrint(("reset bus\n"));
2287 fResetVBus(_VBUS_P0);
2291 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
2292 case XPT_EN_LUN: /* Enable LUN as a target */
2293 case XPT_TARGET_IO: /* Execute target I/O request */
2294 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
2295 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/
2296 case XPT_ABORT: /* Abort the specified CCB */
2297 case XPT_TERM_IO: /* Terminate the I/O process */
2299 ccb->ccb_h.status = CAM_REQ_INVALID;
2303 case XPT_GET_TRAN_SETTINGS:
2304 case XPT_SET_TRAN_SETTINGS:
2306 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2310 case XPT_CALC_GEOMETRY:
2311 cam_calc_geometry(&ccb->ccg, 1);
2315 case XPT_PATH_INQ: /* Path routing inquiry */
2317 struct ccb_pathinq *cpi = &ccb->cpi;
2319 cpi->version_num = 1; /* XXX??? */
2320 cpi->hba_inquiry = PI_SDTR_ABLE;
2321 cpi->target_sprt = 0;
2322 /* Not necessary to reset bus */
2323 cpi->hba_misc = PIM_NOBUSRESET;
2324 cpi->hba_eng_cnt = 0;
2326 cpi->max_target = MAX_VDEVICE_PER_VBUS;
2328 cpi->initiator_id = MAX_VDEVICE_PER_VBUS;
2330 cpi->bus_id = cam_sim_bus(sim);
2331 cpi->base_transfer_speed = 3300;
2332 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2333 strncpy(cpi->hba_vid, "HPT ", HBA_IDLEN);
2334 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2335 cpi->unit_number = cam_sim_unit(sim);
2336 cpi->transport = XPORT_SPI;
2337 cpi->transport_version = 2;
2338 cpi->protocol = PROTO_SCSI;
2339 cpi->protocol_version = SCSI_REV_2;
2340 cpi->ccb_h.status = CAM_REQ_CMP;
2346 KdPrint(("invalid cmd\n"));
2347 ccb->ccb_h.status = CAM_REQ_INVALID;
2351 /* KdPrint(("leave hpt_action..............\n")); */
2354 /* shall be called at lock_driver() */
2356 hpt_queue_ccb(union ccb **ccb_Q, union ccb *ccb)
2359 ccb->ccb_h.ccb_ccb_ptr = ccb;
2361 ccb->ccb_h.ccb_ccb_ptr = (*ccb_Q)->ccb_h.ccb_ccb_ptr;
2362 (*ccb_Q)->ccb_h.ccb_ccb_ptr = (char *)ccb;
2368 /* shall be called at lock_driver() */
2370 hpt_free_ccb(union ccb **ccb_Q, union ccb *ccb)
2376 if(ccb->ccb_h.ccb_ccb_ptr == ccb) /*it means SCpnt is the last one in CURRCMDs*/
2379 while(TempCCB->ccb_h.ccb_ccb_ptr != (char *)ccb)
2380 TempCCB = (union ccb *)TempCCB->ccb_h.ccb_ccb_ptr;
2382 TempCCB->ccb_h.ccb_ccb_ptr = ccb->ccb_h.ccb_ccb_ptr;
2389 #ifdef SUPPORT_ARRAY
2390 /***************************************************************************
2391 * Function: hpt_worker_thread
2392 * Description: Do background rebuilding. Execute in kernel thread context.
2394 ***************************************************************************/
2395 static void hpt_worker_thread(void)
2399 mtx_lock(&DpcQueue_Lock);
2400 while (DpcQueue_First!=DpcQueue_Last) {
2402 p = DpcQueue[DpcQueue_First];
2404 DpcQueue_First %= MAX_DPC;
2406 mtx_unlock(&DpcQueue_Lock);
2407 p.dpc(p.pAdapter, p.arg, p.flags);
2409 mtx_lock(&p.pAdapter->lock);
2410 mtx_lock(&DpcQueue_Lock);
2412 /* since we may have prevented Check_Idle_Call, do it here */
2413 if (DPC_Request_Nums==0) {
2414 if (p.pAdapter->outstandingCommands == 0) {
2415 _VBUS_INST(&p.pAdapter->VBus);
2416 Check_Idle_Call(p.pAdapter);
2417 CheckPendingCall(_VBUS_P0);
2420 mtx_unlock(&p.pAdapter->lock);
2421 mtx_unlock(&DpcQueue_Lock);
2424 if (SIGISMEMBER(curproc->p_siglist, SIGSTOP)) {
2425 /* abort rebuilding process. */
2426 IAL_ADAPTER_T *pAdapter;
2431 sx_slock(&hptmv_list_lock);
2432 pAdapter = gIal_Adapter;
2434 while(pAdapter != 0){
2435 mtx_lock(&pAdapter->lock);
2436 _vbus_p = &pAdapter->VBus;
2438 for (i=0;i<MAX_ARRAY_PER_VBUS;i++)
2440 if ((pArray=ArrayTables(i))->u.array.dArStamp==0)
2442 else if (pArray->u.array.rf_rebuilding ||
2443 pArray->u.array.rf_verifying ||
2444 pArray->u.array.rf_initializing)
2446 pArray->u.array.rf_abort_rebuild = 1;
2449 mtx_unlock(&pAdapter->lock);
2450 pAdapter = pAdapter->next;
2452 sx_sunlock(&hptmv_list_lock);
2454 mtx_lock(&DpcQueue_Lock);
2456 mtx_unlock(&DpcQueue_Lock);
2458 /*Remove this debug option*/
2461 if (SIGISMEMBER(curproc->p_siglist, SIGSTOP))
2462 pause("hptrdy", 2*hz);
2465 kproc_suspend_check(curproc);
2466 pause("-", 2*hz); /* wait for something to do */
2470 static struct proc *hptdaemonproc;
2471 static struct kproc_desc hpt_kp = {
2477 /*Start this thread in the hpt_attach, to prevent kernel from loading it without our controller.*/
2479 launch_worker_thread(void)
2481 IAL_ADAPTER_T *pAdapTemp;
2483 kproc_start(&hpt_kp);
2485 sx_slock(&hptmv_list_lock);
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));
2501 sx_sunlock(&hptmv_list_lock);
2504 * hpt_worker_thread needs to be suspended after shutdown sync, when fs sync finished.
2506 EVENTHANDLER_REGISTER(shutdown_post_sync, kproc_shutdown, hptdaemonproc,
2510 *SYSINIT(hptwt, SI_SUB_KTHREAD_IDLE, SI_ORDER_FIRST, launch_worker_thread, NULL);
2515 /********************************************************************************/
2517 int HPTLIBAPI fOsBuildSgl(_VBUS_ARG PCommand pCmd, FPSCAT_GATH pSg, int logical)
2519 union ccb *ccb = (union ccb *)pCmd->pOrgCommand;
2522 pSg->dSgAddress = (ULONG_PTR)(UCHAR *)ccb->csio.data_ptr;
2523 pSg->wSgSize = ccb->csio.dxfer_len;
2524 pSg->wSgFlag = SG_FLAG_EOT;
2527 /* since we have provided physical sg, nobody will ask us to build physical sg */
2532 /*******************************************************************************/
2537 * the system variable, ticks, can't be used since it hasn't yet been active
2538 * when our driver starts (ticks==0, it's a invalid stamp value)
2541 do { stamp = random(); } while (stamp==0);
2547 SetInquiryData(PINQUIRYDATA inquiryData, PVDevice pVDev)
2550 IDENTIFY_DATA2 *pIdentify = (IDENTIFY_DATA2*)pVDev->u.disk.mv->identifyDevice;
2552 inquiryData->DeviceType = T_DIRECT; /*DIRECT_ACCESS_DEVICE*/
2553 inquiryData->AdditionalLength = (UCHAR)(sizeof(INQUIRYDATA) - 5);
2555 inquiryData->CommandQueue = 1;
2558 switch(pVDev->VDeviceType) {
2559 case VD_SINGLE_DISK:
2562 /* Set the removable bit, if applicable. */
2563 if ((pVDev->u.disk.df_removable_drive) || (pIdentify->GeneralConfiguration & 0x80))
2564 inquiryData->RemovableMedia = 1;
2566 /* Fill in vendor identification fields. */
2567 for (i = 0; i < 20; i += 2) {
2568 inquiryData->VendorId[i] = ((PUCHAR)pIdentify->ModelNumber)[i + 1];
2569 inquiryData->VendorId[i+1] = ((PUCHAR)pIdentify->ModelNumber)[i];
2573 /* Initialize unused portion of product id. */
2574 for (i = 0; i < 4; i++) inquiryData->ProductId[12+i] = ' ';
2576 /* firmware revision */
2577 for (i = 0; i < 4; i += 2)
2579 inquiryData->ProductRevisionLevel[i] = ((PUCHAR)pIdentify->FirmwareRevision)[i+1];
2580 inquiryData->ProductRevisionLevel[i+1] = ((PUCHAR)pIdentify->FirmwareRevision)[i];
2584 memcpy(&inquiryData->VendorId, "RR18xx ", 8);
2585 #ifdef SUPPORT_ARRAY
2586 switch(pVDev->VDeviceType){
2588 if ((pVDev->u.array.pMember[0] && mIsArray(pVDev->u.array.pMember[0])) ||
2589 (pVDev->u.array.pMember[1] && mIsArray(pVDev->u.array.pMember[1])))
2590 memcpy(&inquiryData->ProductId, "RAID 1/0 Array ", 16);
2592 memcpy(&inquiryData->ProductId, "RAID 0 Array ", 16);
2595 if ((pVDev->u.array.pMember[0] && mIsArray(pVDev->u.array.pMember[0])) ||
2596 (pVDev->u.array.pMember[1] && mIsArray(pVDev->u.array.pMember[1])))
2597 memcpy(&inquiryData->ProductId, "RAID 0/1 Array ", 16);
2599 memcpy(&inquiryData->ProductId, "RAID 1 Array ", 16);
2602 memcpy(&inquiryData->ProductId, "RAID 5 Array ", 16);
2605 memcpy(&inquiryData->ProductId, "JBOD Array ", 16);
2609 memcpy(&inquiryData->ProductRevisionLevel, "3.00", 4);
2615 hpt_timeout(void *arg)
2617 PBUS_DMAMAP pmap = (PBUS_DMAMAP)((union ccb *)arg)->ccb_adapter;
2618 IAL_ADAPTER_T *pAdapter = pmap->pAdapter;
2619 _VBUS_INST(&pAdapter->VBus)
2621 mtx_assert(&pAdapter->lock, MA_OWNED);
2622 fResetVBus(_VBUS_P0);
2626 hpt_io_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
2628 PCommand pCmd = (PCommand)arg;
2629 union ccb *ccb = pCmd->pOrgCommand;
2630 struct ccb_hdr *ccb_h = &ccb->ccb_h;
2631 PBUS_DMAMAP pmap = (PBUS_DMAMAP) ccb->ccb_adapter;
2632 IAL_ADAPTER_T *pAdapter = pmap->pAdapter;
2633 PVDevice pVDev = pAdapter->VBus.pVDevice[ccb_h->target_id];
2634 FPSCAT_GATH psg = pCmd->pSgTable;
2636 _VBUS_INST(pVDev->pVBus)
2638 HPT_ASSERT(pCmd->cf_physical_sg);
2641 panic("busdma error");
2643 HPT_ASSERT(nsegs<= MAX_SG_DESCRIPTORS);
2646 for (idx = 0; idx < nsegs; idx++, psg++) {
2647 psg->dSgAddress = (ULONG_PTR)(UCHAR *)segs[idx].ds_addr;
2648 psg->wSgSize = segs[idx].ds_len;
2649 psg->wSgFlag = (idx == nsegs-1)? SG_FLAG_EOT: 0;
2650 /* KdPrint(("psg[%d]:add=%p,size=%x,flag=%x\n", idx, psg->dSgAddress,psg->wSgSize,psg->wSgFlag)); */
2652 /* psg[-1].wSgFlag = SG_FLAG_EOT; */
2654 if (pCmd->cf_data_in) {
2655 bus_dmamap_sync(pAdapter->io_dma_parent, pmap->dma_map,
2656 BUS_DMASYNC_PREREAD);
2658 else if (pCmd->cf_data_out) {
2659 bus_dmamap_sync(pAdapter->io_dma_parent, pmap->dma_map,
2660 BUS_DMASYNC_PREWRITE);
2664 callout_reset(&pmap->timeout, 20 * hz, hpt_timeout, ccb);
2665 pVDev->pfnSendCommand(_VBUS_P pCmd);
2666 CheckPendingCall(_VBUS_P0);
2671 static void HPTLIBAPI
2672 OsSendCommand(_VBUS_ARG union ccb *ccb)
2674 PBUS_DMAMAP pmap = (PBUS_DMAMAP)ccb->ccb_adapter;
2675 IAL_ADAPTER_T *pAdapter = pmap->pAdapter;
2676 struct ccb_hdr *ccb_h = &ccb->ccb_h;
2677 struct ccb_scsiio *csio = &ccb->csio;
2678 PVDevice pVDev = pAdapter->VBus.pVDevice[ccb_h->target_id];
2680 KdPrintI(("OsSendCommand: ccb %p cdb %x-%x-%x\n",
2682 *(ULONG *)&ccb->csio.cdb_io.cdb_bytes[0],
2683 *(ULONG *)&ccb->csio.cdb_io.cdb_bytes[4],
2684 *(ULONG *)&ccb->csio.cdb_io.cdb_bytes[8]
2687 pAdapter->outstandingCommands++;
2689 if (pVDev == NULL || pVDev->vf_online == 0) {
2690 ccb->ccb_h.status = CAM_REQ_INVALID;
2692 goto Command_Complished;
2695 switch(ccb->csio.cdb_io.cdb_bytes[0])
2697 case TEST_UNIT_READY:
2698 case START_STOP_UNIT:
2699 case SYNCHRONIZE_CACHE:
2701 ccb->ccb_h.status = CAM_REQ_CMP;
2705 ZeroMemory(ccb->csio.data_ptr, ccb->csio.dxfer_len);
2706 SetInquiryData((PINQUIRYDATA)ccb->csio.data_ptr, pVDev);
2707 ccb_h->status = CAM_REQ_CMP;
2712 UCHAR *rbuf=csio->data_ptr;
2715 if (pVDev->VDeviceCapacity > 0xfffffffful) {
2718 cap = pVDev->VDeviceCapacity - 1;
2721 rbuf[0] = (UCHAR)(cap>>24);
2722 rbuf[1] = (UCHAR)(cap>>16);
2723 rbuf[2] = (UCHAR)(cap>>8);
2724 rbuf[3] = (UCHAR)cap;
2725 /* Claim 512 byte blocks (big-endian). */
2731 ccb_h->status = CAM_REQ_CMP;
2735 case 0x9e: /*SERVICE_ACTION_IN*/
2737 UCHAR *rbuf = csio->data_ptr;
2738 LBA_T cap = pVDev->VDeviceCapacity - 1;
2740 rbuf[0] = (UCHAR)(cap>>56);
2741 rbuf[1] = (UCHAR)(cap>>48);
2742 rbuf[2] = (UCHAR)(cap>>40);
2743 rbuf[3] = (UCHAR)(cap>>32);
2744 rbuf[4] = (UCHAR)(cap>>24);
2745 rbuf[5] = (UCHAR)(cap>>16);
2746 rbuf[6] = (UCHAR)(cap>>8);
2747 rbuf[7] = (UCHAR)cap;
2753 ccb_h->status = CAM_REQ_CMP;
2761 case 0x88: /* READ_16 */
2762 case 0x8a: /* WRITE_16 */
2768 _VBUS_INST(pVDev->pVBus)
2769 PCommand pCmd = AllocateCommand(_VBUS_P0);
2773 CdbLength = csio->cdb_len;
2774 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0)
2776 if ((ccb->ccb_h.flags & CAM_CDB_PHYS) == 0)
2778 bcopy(csio->cdb_io.cdb_ptr, Cdb, CdbLength);
2782 KdPrintE(("ERROR!!!\n"));
2783 ccb->ccb_h.status = CAM_REQ_INVALID;
2789 bcopy(csio->cdb_io.cdb_bytes, Cdb, CdbLength);
2792 pCmd->pOrgCommand = ccb;
2793 pCmd->pVDevice = pVDev;
2794 pCmd->pfnCompletion = fOsCommandDone;
2795 pCmd->pfnBuildSgl = fOsBuildSgl;
2796 pCmd->pSgTable = pmap->psg;
2803 pCmd->uCmd.Ide.Lba = ((ULONG)Cdb[1] << 16) | ((ULONG)Cdb[2] << 8) | (ULONG)Cdb[3];
2804 pCmd->uCmd.Ide.nSectors = (USHORT) Cdb[4];
2807 case 0x88: /* READ_16 */
2808 case 0x8a: /* WRITE_16 */
2809 pCmd->uCmd.Ide.Lba =
2810 (HPT_U64)Cdb[2] << 56 |
2811 (HPT_U64)Cdb[3] << 48 |
2812 (HPT_U64)Cdb[4] << 40 |
2813 (HPT_U64)Cdb[5] << 32 |
2814 (HPT_U64)Cdb[6] << 24 |
2815 (HPT_U64)Cdb[7] << 16 |
2816 (HPT_U64)Cdb[8] << 8 |
2818 pCmd->uCmd.Ide.nSectors = (USHORT)Cdb[12] << 8 | (USHORT)Cdb[13];
2822 pCmd->uCmd.Ide.Lba = (ULONG)Cdb[5] | ((ULONG)Cdb[4] << 8) | ((ULONG)Cdb[3] << 16) | ((ULONG)Cdb[2] << 24);
2823 pCmd->uCmd.Ide.nSectors = (USHORT) Cdb[8] | ((USHORT)Cdb[7]<<8);
2831 case 0x88: /* READ_16 */
2832 pCmd->uCmd.Ide.Command = IDE_COMMAND_READ;
2833 pCmd->cf_data_in = 1;
2838 case 0x8a: /* WRITE_16 */
2839 pCmd->uCmd.Ide.Command = IDE_COMMAND_WRITE;
2840 pCmd->cf_data_out = 1;
2844 pCmd->uCmd.Ide.Command = IDE_COMMAND_VERIFY;
2847 /*///////////////////////// */
2848 pCmd->cf_physical_sg = 1;
2849 error = bus_dmamap_load_ccb(pAdapter->io_dma_parent,
2852 hpt_io_dmamap_callback,
2853 pCmd, BUS_DMA_WAITOK
2855 KdPrint(("bus_dmamap_load return %d\n", error));
2856 if (error && error!=EINPROGRESS) {
2857 hpt_printk(("bus_dmamap_load error %d\n", error));
2858 FreeCommand(_VBUS_P pCmd);
2859 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2861 pAdapter->outstandingCommands--;
2862 if (pAdapter->outstandingCommands == 0)
2866 goto Command_Complished;
2870 ccb->ccb_h.status = CAM_REQ_INVALID;
2875 CheckPendingCall(_VBUS_P0);
2879 static void HPTLIBAPI
2880 fOsCommandDone(_VBUS_ARG PCommand pCmd)
2882 union ccb *ccb = pCmd->pOrgCommand;
2883 PBUS_DMAMAP pmap = (PBUS_DMAMAP)ccb->ccb_adapter;
2884 IAL_ADAPTER_T *pAdapter = pmap->pAdapter;
2886 KdPrint(("fOsCommandDone(pcmd=%p, result=%d)\n", pCmd, pCmd->Result));
2888 callout_stop(&pmap->timeout);
2890 switch(pCmd->Result) {
2891 case RETURN_SUCCESS:
2892 ccb->ccb_h.status = CAM_REQ_CMP;
2894 case RETURN_BAD_DEVICE:
2895 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2897 case RETURN_DEVICE_BUSY:
2898 ccb->ccb_h.status = CAM_BUSY;
2900 case RETURN_INVALID_REQUEST:
2901 ccb->ccb_h.status = CAM_REQ_INVALID;
2903 case RETURN_SELECTION_TIMEOUT:
2904 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
2907 ccb->ccb_h.status = CAM_BUSY;
2910 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
2914 if (pCmd->cf_data_in) {
2915 bus_dmamap_sync(pAdapter->io_dma_parent, pmap->dma_map, BUS_DMASYNC_POSTREAD);
2917 else if (pCmd->cf_data_out) {
2918 bus_dmamap_sync(pAdapter->io_dma_parent, pmap->dma_map, BUS_DMASYNC_POSTWRITE);
2921 bus_dmamap_unload(pAdapter->io_dma_parent, pmap->dma_map);
2923 FreeCommand(_VBUS_P pCmd);
2928 hpt_queue_dpc(HPT_DPC dpc, IAL_ADAPTER_T * pAdapter, void *arg, UCHAR flags)
2932 mtx_lock(&DpcQueue_Lock);
2933 p = (DpcQueue_Last + 1) % MAX_DPC;
2934 if (p==DpcQueue_First) {
2935 KdPrint(("DPC Queue full!\n"));
2936 mtx_unlock(&DpcQueue_Lock);
2940 DpcQueue[DpcQueue_Last].dpc = dpc;
2941 DpcQueue[DpcQueue_Last].pAdapter = pAdapter;
2942 DpcQueue[DpcQueue_Last].arg = arg;
2943 DpcQueue[DpcQueue_Last].flags = flags;
2945 mtx_unlock(&DpcQueue_Lock);
2952 * Allocate memory above 16M, otherwise we may eat all low memory for ISA devices.
2953 * How about the memory for 5081 request/response array and PRD table?
2956 *os_alloc_page(_VBUS_ARG0)
2958 return (void *)contigmalloc(0x1000, M_DEVBUF, M_NOWAIT, 0x1000000, 0xffffffff, PAGE_SIZE, 0ul);
2962 *os_alloc_dma_page(_VBUS_ARG0)
2964 return (void *)contigmalloc(0x1000, M_DEVBUF, M_NOWAIT, 0x1000000, 0xffffffff, PAGE_SIZE, 0ul);
2968 os_free_page(_VBUS_ARG void *p)
2970 contigfree(p, 0x1000, M_DEVBUF);
2974 os_free_dma_page(_VBUS_ARG void *p)
2976 contigfree(p, 0x1000, M_DEVBUF);
2980 DoXor1(ULONG *p0, ULONG *p1, ULONG *p2, UINT nBytes)
2983 for (i = 0; i < nBytes / 4; i++) *p0++ = *p1++ ^ *p2++;
2987 DoXor2(ULONG *p0, ULONG *p2, UINT nBytes)
2990 for (i = 0; i < nBytes / 4; i++) *p0++ ^= *p2++;