1 /* 2 * Copyright (c) 2004-2005 HighPoint Technologies, Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: src/sys/dev/hptmv/entry.c,v 1.26 2012/06/01 04:34:49 eadler Exp $ 27 */ 28 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/kernel.h> 32 #include <sys/bus.h> 33 #include <sys/malloc.h> 34 #include <sys/resource.h> 35 #include <sys/time.h> 36 #include <sys/callout.h> 37 #include <sys/signalvar.h> 38 #include <sys/eventhandler.h> 39 #include <sys/proc.h> 40 #include <sys/kthread.h> 41 42 #include <sys/lock.h> 43 #include <sys/module.h> 44 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> 51 52 #ifndef __KERNEL__ 53 #define __KERNEL__ 54 #endif 55 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> 60 61 62 #ifdef DEBUG 63 #ifdef DEBUG_LEVEL 64 int hpt_dbg_level = DEBUG_LEVEL; 65 #else 66 int hpt_dbg_level = 0; 67 #endif 68 #endif 69 70 #define MV_ERROR kprintf 71 72 /* 73 * CAM SIM entry points 74 */ 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); 84 85 static struct thread *hptdaemonproc; 86 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), 92 93 DEVMETHOD(device_shutdown, hpt_shutdown), 94 DEVMETHOD_END 95 }; 96 97 static driver_t hpt_pci_driver = { 98 __str(PROC_DIR_NAME), 99 driver_methods, 100 sizeof(IAL_ADAPTER_T) 101 }; 102 103 static devclass_t hpt_devclass; 104 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); 108 109 #define ccb_ccb_ptr spriv_ptr0 110 #define ccb_adapter ccb_h.spriv_ptr1 111 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, 131 int logical); 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); 138 139 #define ccb_ccb_ptr spriv_ptr0 140 #define ccb_adapter ccb_h.spriv_ptr1 141 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]; 145 146 typedef struct st_HPT_DPC { 147 IAL_ADAPTER_T *pAdapter; 148 void (*dpc)(IAL_ADAPTER_T *, void *, UCHAR); 149 void *arg; 150 UCHAR flags; 151 } ST_HPT_DPC; 152 153 #define MAX_DPC 16 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; 158 159 char DRIVER_VERSION[] = "v1.16"; 160 161 static struct lock driver_lock; 162 void lock_driver(void) 163 { 164 lockmgr(&driver_lock, LK_EXCLUSIVE); 165 } 166 void unlock_driver(void) 167 { 168 lockmgr(&driver_lock, LK_RELEASE); 169 } 170 171 /******************************************************************************* 172 * Name: hptmv_free_channel 173 * 174 * Description: free allocated queues for the given channel 175 * 176 * Parameters: pMvSataAdapter - pointer to the RR18xx controler this 177 * channel connected to. 178 * channelNum - channel number. 179 * 180 ******************************************************************************/ 181 static void 182 hptmv_free_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum) 183 { 184 HPT_ASSERT(channelNum < MV_SATA_CHANNELS_NUM); 185 pAdapter->mvSataAdapter.sataChannel[channelNum] = NULL; 186 } 187 188 static void failDevice(PVDevice pVDev) 189 { 190 PVBus _vbus_p = pVDev->pVBus; 191 IAL_ADAPTER_T *pAdapter = (IAL_ADAPTER_T *)_vbus_p->OsExt; 192 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); 197 198 fNotifyGUI(ET_DEVICE_REMOVED, pVDev); 199 200 #ifndef FOR_DEMO 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); 205 } 206 #endif 207 } 208 209 int MvSataResetChannel(MV_SATA_ADAPTER *pMvSataAdapter, MV_U8 channel); 210 211 static void 212 handleEdmaError(_VBUS_ARG PCommand pCmd) 213 { 214 PDevice pDevice = &pCmd->pVDevice->u.disk; 215 MV_SATA_ADAPTER * pSataAdapter = pDevice->mv->mvSataAdapter; 216 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); 221 return; 222 } 223 224 if (pCmd->RetryCount++>5) { 225 hpt_printk(("too many retries on channel(%d)\n", pDevice->mv->channelNumber)); 226 failed: 227 failDevice(pCmd->pVDevice); 228 pCmd->Result = RETURN_IDE_ERROR; 229 CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd); 230 return; 231 } 232 233 /* reset the channel and retry the command */ 234 if (MvSataResetChannel(pSataAdapter, pDevice->mv->channelNumber)) 235 goto failed; 236 237 fNotifyGUI(ET_DEVICE_ERROR, Map2pVDevice(pDevice)); 238 239 hpt_printk(("Retry on channel(%d)\n", pDevice->mv->channelNumber)); 240 fDeviceSendCommand(_VBUS_P pCmd); 241 } 242 243 /**************************************************************** 244 * Name: hptmv_init_channel 245 * 246 * Description: allocate request and response queues for the EDMA of the 247 * given channel and sets other fields. 248 * 249 * Parameters: 250 * pAdapter - pointer to the emulated adapter data structure 251 * channelNum - channel number. 252 * Return: 0 on success, otherwise on failure 253 ****************************************************************/ 254 static int 255 hptmv_init_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum) 256 { 257 MV_SATA_CHANNEL *pMvSataChannel; 258 dma_addr_t req_dma_addr; 259 dma_addr_t rsp_dma_addr; 260 261 if (channelNum >= MV_SATA_CHANNELS_NUM) 262 { 263 MV_ERROR("RR18xx[%d]: Bad channelNum=%d", 264 pAdapter->mvSataAdapter.adapterId, channelNum); 265 return -1; 266 } 267 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; 273 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); 277 278 279 KdPrint(("requestQueue addr is 0x%llX", (HPT_U64)(ULONG_PTR)req_dma_addr)); 280 281 /* check the 1K alignment of the request queue*/ 282 if (req_dma_addr & 0x3ff) 283 { 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); 287 return -1; 288 } 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); 297 298 /* check the 256 alignment of the response queue*/ 299 if (rsp_dma_addr & 0xff) 300 { 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); 304 return -1; 305 } 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)); 311 312 pAdapter->mvChannel[channelNum].online = MV_TRUE; 313 return 0; 314 } 315 316 /****************************************************************************** 317 * Name: hptmv_parse_identify_results 318 * 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. 322 * 323 * Parameters: pMvSataChannel, pointer to the channel data structure. 324 * 325 * Returns: =0 ->success, < 0 ->failure. 326 * 327 ******************************************************************************/ 328 static int 329 hptmv_parse_identify_results(MV_SATA_CHANNEL *pMvSataChannel) 330 { 331 MV_U16 *iden = pMvSataChannel->identifyDevice; 332 333 /*LBA addressing*/ 334 if (! (iden[IDEN_CAPACITY_1_OFFSET] & 0x200)) 335 { 336 KdPrint(("IAL Error in IDENTIFY info: LBA not supported\n")); 337 return -1; 338 } 339 else 340 { 341 KdPrint(("%25s - %s\n", "Capabilities", "LBA supported")); 342 } 343 /*DMA support*/ 344 if (! (iden[IDEN_CAPACITY_1_OFFSET] & 0x100)) 345 { 346 KdPrint(("IAL Error in IDENTIFY info: DMA not supported\n")); 347 return -1; 348 } 349 else 350 { 351 KdPrint(("%25s - %s\n", "Capabilities", "DMA supported")); 352 } 353 /* PIO */ 354 if ((iden[IDEN_VALID] & 2) == 0) 355 { 356 KdPrint(("IAL Error in IDENTIFY info: not able to find PIO mode\n")); 357 return -1; 358 } 359 KdPrint(("%25s - 0x%02x\n", "PIO modes supported", 360 iden[IDEN_PIO_MODE_SPPORTED] & 0xff)); 361 362 /*UDMA*/ 363 if ((iden[IDEN_VALID] & 4) == 0) 364 { 365 KdPrint(("IAL Error in IDENTIFY info: not able to find UDMA mode\n")); 366 return -1; 367 } 368 369 /* 48 bit address */ 370 if ((iden[IDEN_SUPPORTED_COMMANDS2] & 0x400)) 371 { 372 KdPrint(("%25s - %s\n", "LBA48 addressing", "supported")); 373 pMvSataChannel->lba48Address = MV_TRUE; 374 } 375 else 376 { 377 KdPrint(("%25s - %s\n", "LBA48 addressing", "Not supported")); 378 pMvSataChannel->lba48Address = MV_FALSE; 379 } 380 return 0; 381 } 382 383 static void 384 init_vdev_params(IAL_ADAPTER_T *pAdapter, MV_U8 channel) 385 { 386 PVDevice pVDev = &pAdapter->VDevices[channel]; 387 MV_SATA_CHANNEL *pMvSataChannel = pAdapter->mvSataAdapter.sataChannel[channel]; 388 MV_U16_PTR IdentifyData = pMvSataChannel->identifyDevice; 389 390 pMvSataChannel->outstandingCommands = 0; 391 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; 396 397 #ifdef SUPPORT_48BIT_LBA 398 if (pMvSataChannel->lba48Address == MV_TRUE) 399 pVDev->u.disk.dDeRealCapacity = ((IdentifyData[101]<<16) | IdentifyData[100]) - 1; 400 else 401 #endif 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; 407 } else 408 pVDev->u.disk.dDeRealCapacity = 409 (IdentifyData[61]<<16 | IdentifyData[60]) - 1; 410 411 pVDev->u.disk.bDeUsable_Mode = pVDev->u.disk.bDeModeSetting = 412 pAdapter->mvChannel[channel].maxPioModeSupported - MV_ATA_TRANSFER_PIO_0; 413 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; 417 } 418 } 419 420 static void device_change(IAL_ADAPTER_T *pAdapter , MV_U8 channelIndex, int plugged) 421 { 422 PVDevice pVDev; 423 MV_SATA_ADAPTER *pMvSataAdapter = &pAdapter->mvSataAdapter; 424 MV_SATA_CHANNEL *pMvSataChannel = pMvSataAdapter->sataChannel[channelIndex]; 425 426 if (!pMvSataChannel) return; 427 428 if (plugged) 429 { 430 pVDev = &(pAdapter->VDevices[channelIndex]); 431 init_vdev_params(pAdapter, channelIndex); 432 433 pVDev->VDeviceType = pVDev->u.disk.df_atapi? VD_ATAPI : 434 pVDev->u.disk.df_removable_drive? VD_REMOVABLE : VD_SINGLE_DISK; 435 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; 440 441 #ifdef SUPPORT_ARRAY 442 if(pVDev->pParent) 443 { 444 int iMember; 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; 449 } 450 #endif 451 fNotifyGUI(ET_DEVICE_PLUGGED,pVDev); 452 fCheckBootable(pVDev); 453 RegisterVDevice(pVDev); 454 455 #ifndef FOR_DEMO 456 if (pAdapter->beeping) { 457 pAdapter->beeping = 0; 458 BeepOff(pAdapter->mvSataAdapter.adapterIoBaseAddress); 459 } 460 #endif 461 462 } 463 else 464 { 465 pVDev = &(pAdapter->VDevices[channelIndex]); 466 failDevice(pVDev); 467 } 468 } 469 470 static int 471 start_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum) 472 { 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; 477 478 KdPrint(("RR18xx [%d]: start channel (%d)", pMvSataAdapter->adapterId, 479 channelNum)); 480 481 482 /* Software reset channel */ 483 if (mvStorageDevATASoftResetDevice(pMvSataAdapter, channelNum) == MV_FALSE) 484 { 485 MV_ERROR("RR18xx [%d,%d]: failed to perform Software reset\n", 486 pMvSataAdapter->adapterId, channelNum); 487 return -1; 488 } 489 490 /* Hardware reset channel */ 491 if (mvSataChannelHardReset(pMvSataAdapter, channelNum) == MV_FALSE) 492 { 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) 497 { 498 MV_ERROR("RR18xx [%d,%d]: failed to perform Hard reset\n", 499 pMvSataAdapter->adapterId, channelNum); 500 return -1; 501 } 502 } 503 504 /* identify device*/ 505 if (mvStorageDevATAIdentifyDevice(pMvSataAdapter, channelNum) == MV_FALSE) 506 { 507 MV_ERROR("RR18xx [%d,%d]: failed to perform ATA Identify command\n" 508 , pMvSataAdapter->adapterId, channelNum); 509 return -1; 510 } 511 if (hptmv_parse_identify_results(pMvSataChannel)) 512 { 513 MV_ERROR("RR18xx [%d,%d]: Error in parsing ATA Identify message\n" 514 , pMvSataAdapter->adapterId, channelNum); 515 return -1; 516 } 517 518 /* mvStorageDevATASetFeatures */ 519 /* Disable 8 bit PIO in case CFA enabled */ 520 if (pMvSataChannel->identifyDevice[86] & 4) 521 { 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) 527 { 528 MV_ERROR("RR18xx [%d]: channel %d: mvStorageDevATASetFeatures" 529 " failed\n", pMvSataAdapter->adapterId, channelNum); 530 return -1; 531 } 532 } 533 /* Write cache */ 534 #ifdef ENABLE_WRITE_CACHE 535 if (pMvSataChannel->identifyDevice[82] & 0x20) 536 { 537 if (!(pMvSataChannel->identifyDevice[85] & 0x20)) /* if not enabled by default */ 538 { 539 if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum, 540 MV_ATA_SET_FEATURES_ENABLE_WCACHE, 0, 541 0, 0, 0) == MV_FALSE) 542 { 543 MV_ERROR("RR18xx [%d]: channel %d: mvStorageDevATASetFeatures failed\n", 544 pMvSataAdapter->adapterId, channelNum); 545 return -1; 546 } 547 } 548 KdPrint(("RR18xx [%d]: channel %d, write cache enabled\n", 549 pMvSataAdapter->adapterId, channelNum)); 550 } 551 else 552 { 553 KdPrint(("RR18xx [%d]: channel %d, write cache not supported\n", 554 pMvSataAdapter->adapterId, channelNum)); 555 } 556 #else /* disable write cache */ 557 { 558 if (pMvSataChannel->identifyDevice[85] & 0x20) 559 { 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) 565 { 566 MV_ERROR("RR18xx [%d]: channel %d: mvStorageDevATASetFeatures failed\n", 567 pMvSataAdapter->adapterId, channelNum); 568 return -1; 569 } 570 } 571 KdPrint(("RR18xx [%d]: channel=%d, write cache disabled\n", 572 pMvSataAdapter->adapterId, channelNum)); 573 } 574 #endif 575 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) == 582 MV_FALSE) 583 { 584 MV_ERROR("RR18xx [%d] channel %d: Set Features failed\n", 585 pMvSataAdapter->adapterId, channelNum); 586 return -1; 587 } 588 589 if (pMvSataChannel->identifyDevice[IDEN_PIO_MODE_SPPORTED] & 1) 590 { 591 pioMode = MV_ATA_TRANSFER_PIO_4; 592 } 593 else if (pMvSataChannel->identifyDevice[IDEN_PIO_MODE_SPPORTED] & 2) 594 { 595 pioMode = MV_ATA_TRANSFER_PIO_3; 596 } 597 else 598 { 599 MV_ERROR("IAL Error in IDENTIFY info: PIO modes 3 and 4 not supported\n"); 600 pioMode = MV_ATA_TRANSFER_PIO_SLOW; 601 } 602 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) 609 { 610 MV_ERROR("RR18xx [%d] channel %d: Set Features failed\n", 611 pMvSataAdapter->adapterId, channelNum); 612 return -1; 613 } 614 615 udmaMode = MV_ATA_TRANSFER_UDMA_0; 616 if (pMvSataChannel->identifyDevice[IDEN_UDMA_MODE] & 0x40) 617 { 618 udmaMode = MV_ATA_TRANSFER_UDMA_6; 619 } 620 else if (pMvSataChannel->identifyDevice[IDEN_UDMA_MODE] & 0x20) 621 { 622 udmaMode = MV_ATA_TRANSFER_UDMA_5; 623 } 624 else if (pMvSataChannel->identifyDevice[IDEN_UDMA_MODE] & 0x10) 625 { 626 udmaMode = MV_ATA_TRANSFER_UDMA_4; 627 } 628 else if (pMvSataChannel->identifyDevice[IDEN_UDMA_MODE] & 8) 629 { 630 udmaMode = MV_ATA_TRANSFER_UDMA_3; 631 } 632 else if (pMvSataChannel->identifyDevice[IDEN_UDMA_MODE] & 4) 633 { 634 udmaMode = MV_ATA_TRANSFER_UDMA_2; 635 } 636 637 KdPrint(("RR18xx [%d] Set transfer mode XFER_UDMA_%d\n", 638 pMvSataAdapter->adapterId, udmaMode & 0xf)); 639 pChannelInfo->maxUltraDmaModeSupported = udmaMode; 640 641 /*if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum, 642 MV_ATA_SET_FEATURES_TRANSFER, udmaMode, 643 0, 0, 0) == MV_FALSE) 644 { 645 MV_ERROR("RR18xx [%d] channel %d: Set Features failed\n", 646 pMvSataAdapter->adapterId, channelNum); 647 return -1; 648 }*/ 649 if (pChannelInfo->maxUltraDmaModeSupported == 0xFF) 650 return TRUE; 651 else 652 do 653 { 654 if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum, 655 MV_ATA_SET_FEATURES_TRANSFER, 656 pChannelInfo->maxUltraDmaModeSupported, 657 0, 0, 0) == MV_FALSE) 658 { 659 if (pChannelInfo->maxUltraDmaModeSupported > MV_ATA_TRANSFER_UDMA_0) 660 { 661 if (mvStorageDevATASoftResetDevice(pMvSataAdapter, channelNum) == MV_FALSE) 662 { 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) 670 return FALSE; 671 } 672 if (mvSataChannelHardReset(pMvSataAdapter, channelNum) == MV_FALSE) 673 return FALSE; 674 pChannelInfo->maxUltraDmaModeSupported--; 675 continue; 676 } 677 else return FALSE; 678 } 679 break; 680 }while (1); 681 682 /* Read look ahead */ 683 #ifdef ENABLE_READ_AHEAD 684 if (pMvSataChannel->identifyDevice[82] & 0x40) 685 { 686 if (!(pMvSataChannel->identifyDevice[85] & 0x40)) /* if not enabled by default */ 687 { 688 if (mvStorageDevATASetFeatures(pMvSataAdapter, channelNum, 689 MV_ATA_SET_FEATURES_ENABLE_RLA, 0, 0, 690 0, 0) == MV_FALSE) 691 { 692 MV_ERROR("RR18xx [%d] channel %d: Set Features failed\n", 693 pMvSataAdapter->adapterId, channelNum); 694 return -1; 695 } 696 } 697 KdPrint(("RR18xx [%d]: channel=%d, read look ahead enabled\n", 698 pMvSataAdapter->adapterId, channelNum)); 699 } 700 else 701 { 702 KdPrint(("RR18xx [%d]: channel %d, Read Look Ahead not supported\n", 703 pMvSataAdapter->adapterId, channelNum)); 704 } 705 #else 706 { 707 if (pMvSataChannel->identifyDevice[86] & 0x20) 708 { 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, 713 0, 0) == MV_FALSE) 714 { 715 MV_ERROR("RR18xx [%d]:channel %d: ATA Set Features failed\n", 716 pMvSataAdapter->adapterId, channelNum); 717 return -1; 718 } 719 } 720 KdPrint(("RR18xx [%d]:channel %d, read look ahead disabled\n", 721 pMvSataAdapter->adapterId, channelNum)); 722 } 723 #endif 724 725 726 { 727 KdPrint(("RR18xx [%d]: channel %d config EDMA, Non Queued Mode\n", 728 pMvSataAdapter->adapterId, 729 channelNum)); 730 if (mvSataConfigEdmaMode(pMvSataAdapter, channelNum, 731 MV_EDMA_MODE_NOT_QUEUED, 0) == MV_FALSE) 732 { 733 MV_ERROR("RR18xx [%d] channel %d Error: mvSataConfigEdmaMode failed\n", 734 pMvSataAdapter->adapterId, channelNum); 735 return -1; 736 } 737 } 738 /* Enable EDMA */ 739 if (mvSataEnableChannelDma(pMvSataAdapter, channelNum) == MV_FALSE) 740 { 741 MV_ERROR("RR18xx [%d] Failed to enable DMA, channel=%d\n", 742 pMvSataAdapter->adapterId, channelNum); 743 return -1; 744 } 745 MV_ERROR("RR18xx [%d,%d]: channel started successfully\n", 746 pMvSataAdapter->adapterId, channelNum); 747 748 #ifndef FOR_DEMO 749 set_fail_led(pMvSataAdapter, channelNum, 0); 750 #endif 751 return 0; 752 } 753 754 static void 755 hptmv_handle_event(void * data, int flag) 756 { 757 IAL_ADAPTER_T *pAdapter = (IAL_ADAPTER_T *)data; 758 MV_SATA_ADAPTER *pMvSataAdapter = &pAdapter->mvSataAdapter; 759 MV_U8 channelIndex; 760 761 /* mvOsSemTake(&pMvSataAdapter->semaphore); */ 762 for (channelIndex = 0; channelIndex < MV_SATA_CHANNELS_NUM; channelIndex++) 763 { 764 switch(pAdapter->sataEvents[channelIndex]) 765 { 766 case SATA_EVENT_CHANNEL_CONNECTED: 767 /* Handle only connects */ 768 if (flag == 1) 769 break; 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) 774 { 775 MV_ERROR("RR18xx [%d,%d] Failed to configure\n", 776 pMvSataAdapter->adapterId, channelIndex); 777 hptmv_free_channel(pAdapter, channelIndex); 778 } 779 else 780 { 781 /*mvSataChannelHardReset(pMvSataAdapter, channel);*/ 782 if (start_channel( pAdapter, channelIndex)) 783 { 784 MV_ERROR("RR18xx [%d,%d]Failed to start channel\n", 785 pMvSataAdapter->adapterId, channelIndex); 786 hptmv_free_channel(pAdapter, channelIndex); 787 } 788 else 789 { 790 device_change(pAdapter, channelIndex, TRUE); 791 } 792 } 793 pAdapter->sataEvents[channelIndex] = SATA_EVENT_NO_CHANGE; 794 break; 795 796 case SATA_EVENT_CHANNEL_DISCONNECTED: 797 /* Handle only disconnects */ 798 if (flag == 0) 799 break; 800 KdPrint(("RR18xx [%d,%d]: device disconnected\n", 801 pMvSataAdapter->adapterId, channelIndex)); 802 /* Flush pending commands */ 803 if(pMvSataAdapter->sataChannel[channelIndex]) 804 { 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); 816 } 817 else 818 { 819 KdPrint(("RR18xx [%d,%d]: channel already removed!!\n", 820 pMvSataAdapter->adapterId, channelIndex)); 821 } 822 pAdapter->sataEvents[channelIndex] = SATA_EVENT_NO_CHANGE; 823 break; 824 825 case SATA_EVENT_NO_CHANGE: 826 break; 827 828 default: 829 break; 830 } 831 } 832 /* mvOsSemRelease(&pMvSataAdapter->semaphore); */ 833 } 834 835 #define EVENT_CONNECT 1 836 #define EVENT_DISCONNECT 0 837 838 static void 839 hptmv_handle_event_connect(void *data) 840 { 841 hptmv_handle_event (data, 0); 842 } 843 844 static void 845 hptmv_handle_event_disconnect(void *data) 846 { 847 hptmv_handle_event (data, 1); 848 } 849 850 static MV_BOOLEAN 851 hptmv_event_notify(MV_SATA_ADAPTER *pMvSataAdapter, MV_EVENT_TYPE eventType, 852 MV_U32 param1, MV_U32 param2) 853 { 854 IAL_ADAPTER_T *pAdapter = pMvSataAdapter->IALData; 855 856 switch (eventType) 857 { 858 case MV_EVENT_TYPE_SATA_CABLE: 859 { 860 MV_U8 channel = param2; 861 862 if (param1 == EVENT_CONNECT) 863 { 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); 869 } 870 else if (param1 == EVENT_DISCONNECT) 871 { 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); 880 } 881 else 882 { 883 884 MV_ERROR("RR18xx: illigal value for param1(%d) at " 885 "connect/disconect event, host=%d\n", param1, 886 pMvSataAdapter->adapterId ); 887 888 } 889 } 890 break; 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)); 894 return MV_TRUE; 895 default: 896 MV_ERROR("RR18xx[%d]: unknown event type (%d)\n", 897 pMvSataAdapter->adapterId, eventType); 898 return MV_FALSE; 899 } 900 return MV_TRUE; 901 } 902 903 static int 904 hptmv_allocate_edma_queues(IAL_ADAPTER_T *pAdapter) 905 { 906 pAdapter->requestsArrayBaseAddr = (MV_U8 *)contigmalloc(REQUESTS_ARRAY_SIZE, 907 M_DEVBUF, M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0ul); 908 if (pAdapter->requestsArrayBaseAddr == NULL) 909 { 910 MV_ERROR("RR18xx[%d]: Failed to allocate memory for EDMA request" 911 " queues\n", pAdapter->mvSataAdapter.adapterId); 912 return -1; 913 } 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); 922 923 if ((pAdapter->requestsArrayBaseDmaAlignedAddr - pAdapter->requestsArrayBaseDmaAddr) != 924 (pAdapter->requestsArrayBaseAlignedAddr - pAdapter->requestsArrayBaseAddr)) 925 { 926 MV_ERROR("RR18xx[%d]: Error in Request Quueues Alignment\n", 927 pAdapter->mvSataAdapter.adapterId); 928 contigfree(pAdapter->requestsArrayBaseAddr, REQUESTS_ARRAY_SIZE, M_DEVBUF); 929 return -1; 930 } 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) 935 { 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); 939 return -1; 940 } 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); 949 950 if ((pAdapter->responsesArrayBaseDmaAlignedAddr - pAdapter->responsesArrayBaseDmaAddr) != 951 (pAdapter->responsesArrayBaseAlignedAddr - pAdapter->responsesArrayBaseAddr)) 952 { 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); 957 return -1; 958 } 959 return 0; 960 } 961 962 static void 963 hptmv_free_edma_queues(IAL_ADAPTER_T *pAdapter) 964 { 965 contigfree(pAdapter->requestsArrayBaseAddr, REQUESTS_ARRAY_SIZE, M_DEVBUF); 966 contigfree(pAdapter->responsesArrayBaseAddr, RESPONSES_ARRAY_SIZE, M_DEVBUF); 967 } 968 969 static PVOID 970 AllocatePRDTable(IAL_ADAPTER_T *pAdapter) 971 { 972 PVOID ret; 973 if (pAdapter->pFreePRDLink) { 974 KdPrint(("pAdapter->pFreePRDLink:%p\n",pAdapter->pFreePRDLink)); 975 ret = pAdapter->pFreePRDLink; 976 pAdapter->pFreePRDLink = *(void**)ret; 977 return ret; 978 } 979 return NULL; 980 } 981 982 static void 983 FreePRDTable(IAL_ADAPTER_T *pAdapter, PVOID PRDTable) 984 { 985 *(void**)PRDTable = pAdapter->pFreePRDLink; 986 pAdapter->pFreePRDLink = PRDTable; 987 } 988 989 extern PVDevice fGetFirstChild(PVDevice pLogical); 990 extern void fResetBootMark(PVDevice pLogical); 991 static void 992 fRegisterVdevice(IAL_ADAPTER_T *pAdapter) 993 { 994 PVDevice pPhysical, pLogical; 995 PVBus pVBus; 996 int i,j; 997 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; 1004 continue; 1005 } 1006 if (pLogical->VDeviceType==VD_SPARE || pPhysical!=fGetFirstChild(pLogical)) 1007 continue; 1008 1009 pVBus = &pAdapter->VBus; 1010 if(pVBus) 1011 { 1012 j=0; 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; 1017 1018 if (j>0 && pLogical->vf_bootmark) { 1019 if (pVBus->pVDevice[0]->vf_bootmark) { 1020 fResetBootMark(pLogical); 1021 } 1022 else { 1023 do { pVBus->pVDevice[j] = pVBus->pVDevice[j-1]; } while (--j); 1024 pVBus->pVDevice[0] = pLogical; 1025 } 1026 } 1027 } 1028 } 1029 } 1030 } 1031 1032 PVDevice 1033 GetSpareDisk(_VBUS_ARG PVDevice pArray) 1034 { 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; 1039 int i; 1040 1041 for(i=0;i<MV_SATA_CHANNELS_NUM;i++) 1042 { 1043 pVDevice = &pAdapter->VDevices[i]; 1044 if(!pVDevice) 1045 continue; 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 && 1050 thiscap < maxcap && 1051 thiscap >= capacity) 1052 { 1053 maxcap = pVDevice->VDeviceCapacity; 1054 pFind = pVDevice; 1055 } 1056 } 1057 return pFind; 1058 } 1059 1060 /****************************************************************** 1061 * IO ATA Command 1062 *******************************************************************/ 1063 int HPTLIBAPI 1064 fDeReadWrite(PDevice pDev, ULONG Lba, UCHAR Cmd, void *tmpBuffer) 1065 { 1066 return mvReadWrite(pDev->mv, Lba, Cmd, tmpBuffer); 1067 } 1068 1069 void HPTLIBAPI fDeSelectMode(PDevice pDev, UCHAR NewMode) 1070 { 1071 MV_SATA_CHANNEL *pSataChannel = pDev->mv; 1072 MV_SATA_ADAPTER *pSataAdapter = pSataChannel->mvSataAdapter; 1073 MV_U8 channelIndex = pSataChannel->channelNumber; 1074 UCHAR mvMode; 1075 /* 508x don't use MW-DMA? */ 1076 if (NewMode>4 && NewMode<8) NewMode = 4; 1077 pDev->bDeModeSetting = NewMode; 1078 if (NewMode<=4) 1079 mvMode = MV_ATA_TRANSFER_PIO_0 + NewMode; 1080 else 1081 mvMode = MV_ATA_TRANSFER_UDMA_0 + (NewMode-8); 1082 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; 1086 1087 mvSataDisableChannelDma(pSataAdapter, channelIndex); 1088 /* Flush pending commands */ 1089 mvSataFlushDmaQueue (pSataAdapter, channelIndex, MV_FLUSH_TYPE_NONE); 1090 1091 if (mvStorageDevATASetFeatures(pSataAdapter, channelIndex, 1092 MV_ATA_SET_FEATURES_TRANSFER, 1093 mvMode, 0, 0, 0) == MV_FALSE) 1094 { 1095 KdPrint(("channel %d: Set Features failed\n", channelIndex)); 1096 } 1097 /* Enable EDMA */ 1098 if (mvSataEnableChannelDma(pSataAdapter, channelIndex) == MV_FALSE) 1099 KdPrint(("Failed to enable DMA, channel=%d", channelIndex)); 1100 } 1101 1102 int HPTLIBAPI fDeSetTCQ(PDevice pDev, int enable, int depth) 1103 { 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; 1110 int ret = 0; 1111 1112 if (dmaActive) { 1113 mvSataDisableChannelDma(pSataAdapter, channelIndex); 1114 mvSataFlushDmaQueue(pSataAdapter,channelIndex,MV_FLUSH_TYPE_CALLBACK); 1115 } 1116 1117 if (enable) { 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); 1123 ret = 1; 1124 } 1125 } 1126 else 1127 { 1128 if (pSataChannel->queuedDMA != MV_EDMA_MODE_NOT_QUEUED) { 1129 channelInfo->queueDepth = 2; 1130 mvSataConfigEdmaMode(pSataAdapter, channelIndex, MV_EDMA_MODE_NOT_QUEUED, 0); 1131 ret = 1; 1132 } 1133 } 1134 1135 if (dmaActive) 1136 mvSataEnableChannelDma(pSataAdapter,channelIndex); 1137 return ret; 1138 } 1139 1140 int HPTLIBAPI fDeSetNCQ(PDevice pDev, int enable, int depth) 1141 { 1142 return 0; 1143 } 1144 1145 int HPTLIBAPI fDeSetWriteCache(PDevice pDev, int enable) 1146 { 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; 1153 int ret = 0; 1154 1155 if (dmaActive) { 1156 mvSataDisableChannelDma(pSataAdapter, channelIndex); 1157 mvSataFlushDmaQueue(pSataAdapter,channelIndex,MV_FLUSH_TYPE_CALLBACK); 1158 } 1159 1160 if ((pSataChannel->identifyDevice[82] & (0x20))) { 1161 if (enable) { 1162 if (mvStorageDevATASetFeatures(pSataAdapter, channelIndex, 1163 MV_ATA_SET_FEATURES_ENABLE_WCACHE, 0, 0, 0, 0)) 1164 { 1165 channelInfo->writeCacheEnabled = MV_TRUE; 1166 ret = 1; 1167 } 1168 } 1169 else { 1170 if (mvStorageDevATASetFeatures(pSataAdapter, channelIndex, 1171 MV_ATA_SET_FEATURES_DISABLE_WCACHE, 0, 0, 0, 0)) 1172 { 1173 channelInfo->writeCacheEnabled = MV_FALSE; 1174 ret = 1; 1175 } 1176 } 1177 } 1178 1179 if (dmaActive) 1180 mvSataEnableChannelDma(pSataAdapter,channelIndex); 1181 return ret; 1182 } 1183 1184 int HPTLIBAPI fDeSetReadAhead(PDevice pDev, int enable) 1185 { 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; 1192 int ret = 0; 1193 1194 if (dmaActive) { 1195 mvSataDisableChannelDma(pSataAdapter, channelIndex); 1196 mvSataFlushDmaQueue(pSataAdapter,channelIndex,MV_FLUSH_TYPE_CALLBACK); 1197 } 1198 1199 if ((pSataChannel->identifyDevice[82] & (0x40))) { 1200 if (enable) { 1201 if (mvStorageDevATASetFeatures(pSataAdapter, channelIndex, 1202 MV_ATA_SET_FEATURES_ENABLE_RLA, 0, 0, 0, 0)) 1203 { 1204 channelInfo->readAheadEnabled = MV_TRUE; 1205 ret = 1; 1206 } 1207 } 1208 else { 1209 if (mvStorageDevATASetFeatures(pSataAdapter, channelIndex, 1210 MV_ATA_SET_FEATURES_DISABLE_RLA, 0, 0, 0, 0)) 1211 { 1212 channelInfo->readAheadEnabled = MV_FALSE; 1213 ret = 1; 1214 } 1215 } 1216 } 1217 1218 if (dmaActive) 1219 mvSataEnableChannelDma(pSataAdapter,channelIndex); 1220 return ret; 1221 } 1222 1223 #ifdef SUPPORT_ARRAY 1224 #define IdeRegisterVDevice fCheckArray 1225 #else 1226 void 1227 IdeRegisterVDevice(PDevice pDev) 1228 { 1229 PVDevice pVDev = Map2pVDevice(pDev); 1230 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]; 1237 } 1238 #endif 1239 1240 static __inline PBUS_DMAMAP 1241 dmamap_get(struct IALAdapter * pAdapter) 1242 { 1243 PBUS_DMAMAP p = pAdapter->pbus_dmamap_list; 1244 if (p) 1245 pAdapter->pbus_dmamap_list = p-> next; 1246 return p; 1247 } 1248 1249 static __inline void 1250 dmamap_put(PBUS_DMAMAP p) 1251 { 1252 p->next = p->pAdapter->pbus_dmamap_list; 1253 p->pAdapter->pbus_dmamap_list = p; 1254 } 1255 1256 /*Since mtx not provide the initialize when declare, so we Final init here to initialize the global mtx*/ 1257 #define override_kernel_driver() 1258 1259 static void hpt_init(void *dummy) 1260 { 1261 override_kernel_driver(); 1262 lockinit(&driver_lock, "hptsleeplock", 0, LK_CANRECURSE); 1263 } 1264 SYSINIT(hptinit, SI_SUB_CONFIGURE, SI_ORDER_FIRST, hpt_init, NULL); 1265 1266 static int num_adapters = 0; 1267 static int 1268 init_adapter(IAL_ADAPTER_T *pAdapter) 1269 { 1270 PVBus _vbus_p = &pAdapter->VBus; 1271 MV_SATA_ADAPTER *pMvSataAdapter; 1272 int i, channel, rid; 1273 1274 PVDevice pVDev; 1275 1276 lock_driver(); 1277 1278 pAdapter->next = 0; 1279 1280 if(gIal_Adapter == NULL){ 1281 gIal_Adapter = pAdapter; 1282 pCurAdapter = gIal_Adapter; 1283 } 1284 else { 1285 pCurAdapter->next = pAdapter; 1286 pCurAdapter = pAdapter; 1287 } 1288 1289 pAdapter->outstandingCommands = 0; 1290 1291 pMvSataAdapter = &(pAdapter->mvSataAdapter); 1292 _vbus_p->OsExt = (void *)pAdapter; 1293 pMvSataAdapter->IALData = pAdapter; 1294 1295 if (bus_dma_tag_create(NULL,/* parent */ 1296 4, /* alignment */ 1297 BUS_SPACE_MAXADDR_32BIT+1, /* boundary */ 1298 BUS_SPACE_MAXADDR, /* lowaddr */ 1299 BUS_SPACE_MAXADDR, /* highaddr */ 1300 NULL, NULL, /* filter, filterarg */ 1301 PAGE_SIZE * (MAX_SG_DESCRIPTORS-1), /* maxsize */ 1302 MAX_SG_DESCRIPTORS, /* nsegments */ 1303 0x10000, /* maxsegsize */ 1304 BUS_DMA_WAITOK, /* flags */ 1305 &pAdapter->io_dma_parent /* tag */)) 1306 { 1307 return ENXIO; 1308 } 1309 1310 1311 if (hptmv_allocate_edma_queues(pAdapter)) 1312 { 1313 MV_ERROR("RR18xx: Failed to allocate memory for EDMA queues\n"); 1314 unlock_driver(); 1315 return ENOMEM; 1316 } 1317 1318 /* also map EPROM address */ 1319 rid = 0x10; 1320 if (!(pAdapter->mem_res = bus_alloc_resource(pAdapter->hpt_dev, SYS_RES_MEMORY, &rid, 1321 0, ~0, MV_SATA_PCI_BAR0_SPACE_SIZE+0x40000, RF_ACTIVE)) 1322 || 1323 !(pMvSataAdapter->adapterIoBaseAddress = rman_get_virtual(pAdapter->mem_res))) 1324 { 1325 MV_ERROR("RR18xx: Failed to remap memory space\n"); 1326 hptmv_free_edma_queues(pAdapter); 1327 unlock_driver(); 1328 return ENXIO; 1329 } 1330 else 1331 { 1332 KdPrint(("RR18xx: io base address 0x%p\n", pMvSataAdapter->adapterIoBaseAddress)); 1333 } 1334 1335 pMvSataAdapter->adapterId = num_adapters++; 1336 /* get the revision ID */ 1337 pMvSataAdapter->pciConfigRevisionId = pci_read_config(pAdapter->hpt_dev, PCIR_REVID, 1); 1338 pMvSataAdapter->pciConfigDeviceId = pci_get_device(pAdapter->hpt_dev); 1339 1340 /* init RR18xx */ 1341 pMvSataAdapter->intCoalThre[0]= 1; 1342 pMvSataAdapter->intCoalThre[1]= 1; 1343 pMvSataAdapter->intTimeThre[0] = 1; 1344 pMvSataAdapter->intTimeThre[1] = 1; 1345 pMvSataAdapter->pciCommand = 0x0107E371; 1346 pMvSataAdapter->pciSerrMask = 0xd77fe6ul; 1347 pMvSataAdapter->pciInterruptMask = 0xd77fe6ul; 1348 pMvSataAdapter->mvSataEventNotify = hptmv_event_notify; 1349 1350 if (mvSataInitAdapter(pMvSataAdapter) == MV_FALSE) 1351 { 1352 MV_ERROR("RR18xx[%d]: core failed to initialize the adapter\n", 1353 pMvSataAdapter->adapterId); 1354 unregister: 1355 bus_release_resource(pAdapter->hpt_dev, SYS_RES_MEMORY, rid, pAdapter->mem_res); 1356 hptmv_free_edma_queues(pAdapter); 1357 unlock_driver(); 1358 return ENXIO; 1359 } 1360 pAdapter->ver_601 = pMvSataAdapter->pcbVersion; 1361 1362 #ifndef FOR_DEMO 1363 set_fail_leds(pMvSataAdapter, 0); 1364 #endif 1365 1366 /* setup command blocks */ 1367 KdPrint(("Allocate command blocks\n")); 1368 _vbus_(pFreeCommands) = NULL; 1369 pAdapter->pCommandBlocks = 1370 kmalloc(sizeof(struct _Command) * MAX_COMMAND_BLOCKS_FOR_EACH_VBUS, M_DEVBUF, M_NOWAIT); 1371 KdPrint(("pCommandBlocks:%p\n",pAdapter->pCommandBlocks)); 1372 if (!pAdapter->pCommandBlocks) { 1373 MV_ERROR("insufficient memory\n"); 1374 goto unregister; 1375 } 1376 1377 for (i=0; i<MAX_COMMAND_BLOCKS_FOR_EACH_VBUS; i++) { 1378 FreeCommand(_VBUS_P &(pAdapter->pCommandBlocks[i])); 1379 } 1380 1381 /*Set up the bus_dmamap*/ 1382 pAdapter->pbus_dmamap = (PBUS_DMAMAP)kmalloc (sizeof(struct _BUS_DMAMAP) * MAX_QUEUE_COMM, M_DEVBUF, M_NOWAIT); 1383 if(!pAdapter->pbus_dmamap) { 1384 MV_ERROR("insufficient memory\n"); 1385 kfree(pAdapter->pCommandBlocks, M_DEVBUF); 1386 goto unregister; 1387 } 1388 1389 memset((void *)pAdapter->pbus_dmamap, 0, sizeof(struct _BUS_DMAMAP) * MAX_QUEUE_COMM); 1390 pAdapter->pbus_dmamap_list = 0; 1391 for (i=0; i < MAX_QUEUE_COMM; i++) { 1392 PBUS_DMAMAP pmap = &(pAdapter->pbus_dmamap[i]); 1393 pmap->pAdapter = pAdapter; 1394 dmamap_put(pmap); 1395 1396 if(bus_dmamap_create(pAdapter->io_dma_parent, 0, &pmap->dma_map)) { 1397 MV_ERROR("Can not allocate dma map\n"); 1398 kfree(pAdapter->pCommandBlocks, M_DEVBUF); 1399 kfree(pAdapter->pbus_dmamap, M_DEVBUF); 1400 goto unregister; 1401 } 1402 } 1403 /* setup PRD Tables */ 1404 KdPrint(("Allocate PRD Tables\n")); 1405 pAdapter->pFreePRDLink = 0; 1406 1407 pAdapter->prdTableAddr = (PUCHAR)contigmalloc( 1408 (PRD_ENTRIES_SIZE*PRD_TABLES_FOR_VBUS + 32), M_DEVBUF, M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0ul); 1409 1410 KdPrint(("prdTableAddr:%p\n",pAdapter->prdTableAddr)); 1411 if (!pAdapter->prdTableAddr) { 1412 MV_ERROR("insufficient PRD Tables\n"); 1413 goto unregister; 1414 } 1415 pAdapter->prdTableAlignedAddr = (PUCHAR)(((ULONG_PTR)pAdapter->prdTableAddr + 0x1f) & ~(ULONG_PTR)0x1fL); 1416 { 1417 PUCHAR PRDTable = pAdapter->prdTableAlignedAddr; 1418 for (i=0; i<PRD_TABLES_FOR_VBUS; i++) 1419 { 1420 /* KdPrint(("i=%d,pAdapter->pFreePRDLink=%p\n",i,pAdapter->pFreePRDLink)); */ 1421 FreePRDTable(pAdapter, PRDTable); 1422 PRDTable += PRD_ENTRIES_SIZE; 1423 } 1424 } 1425 1426 /* enable the adapter interrupts */ 1427 1428 /* configure and start the connected channels*/ 1429 for (channel = 0; channel < MV_SATA_CHANNELS_NUM; channel++) 1430 { 1431 pAdapter->mvChannel[channel].online = MV_FALSE; 1432 if (mvSataIsStorageDeviceConnected(pMvSataAdapter, channel) 1433 == MV_TRUE) 1434 { 1435 KdPrint(("RR18xx[%d]: channel %d is connected\n", 1436 pMvSataAdapter->adapterId, channel)); 1437 1438 if (hptmv_init_channel(pAdapter, channel) == 0) 1439 { 1440 if (mvSataConfigureChannel(pMvSataAdapter, channel) == MV_FALSE) 1441 { 1442 MV_ERROR("RR18xx[%d]: Failed to configure channel" 1443 " %d\n",pMvSataAdapter->adapterId, channel); 1444 hptmv_free_channel(pAdapter, channel); 1445 } 1446 else 1447 { 1448 if (start_channel(pAdapter, channel)) 1449 { 1450 MV_ERROR("RR18xx[%d]: Failed to start channel," 1451 " channel=%d\n",pMvSataAdapter->adapterId, 1452 channel); 1453 hptmv_free_channel(pAdapter, channel); 1454 } 1455 pAdapter->mvChannel[channel].online = MV_TRUE; 1456 /* mvSataChannelSetEdmaLoopBackMode(pMvSataAdapter, 1457 channel, 1458 MV_TRUE);*/ 1459 } 1460 } 1461 } 1462 KdPrint(("pAdapter->mvChannel[channel].online:%x, channel:%d\n", 1463 pAdapter->mvChannel[channel].online, channel)); 1464 } 1465 1466 #ifdef SUPPORT_ARRAY 1467 for(i = MAX_ARRAY_DEVICE - 1; i >= 0; i--) { 1468 pVDev = ArrayTables(i); 1469 mArFreeArrayTable(pVDev); 1470 } 1471 #endif 1472 1473 KdPrint(("Initialize Devices\n")); 1474 for (channel = 0; channel < MV_SATA_CHANNELS_NUM; channel++) { 1475 MV_SATA_CHANNEL *pMvSataChannel = pMvSataAdapter->sataChannel[channel]; 1476 if (pMvSataChannel) { 1477 init_vdev_params(pAdapter, channel); 1478 IdeRegisterVDevice(&pAdapter->VDevices[channel].u.disk); 1479 } 1480 } 1481 #ifdef SUPPORT_ARRAY 1482 CheckArrayCritical(_VBUS_P0); 1483 #endif 1484 _vbus_p->nInstances = 1; 1485 fRegisterVdevice(pAdapter); 1486 1487 for (channel=0;channel<MV_SATA_CHANNELS_NUM;channel++) { 1488 pVDev = _vbus_p->pVDevice[channel]; 1489 if (pVDev && pVDev->vf_online) 1490 fCheckBootable(pVDev); 1491 } 1492 1493 #if defined(SUPPORT_ARRAY) && defined(_RAID5N_) 1494 init_raid5_memory(_VBUS_P0); 1495 _vbus_(r5).enable_write_back = 1; 1496 kprintf("RR18xx: RAID5 write-back %s\n", _vbus_(r5).enable_write_back? "enabled" : "disabled"); 1497 #endif 1498 1499 mvSataUnmaskAdapterInterrupt(pMvSataAdapter); 1500 unlock_driver(); 1501 return 0; 1502 } 1503 1504 int 1505 MvSataResetChannel(MV_SATA_ADAPTER *pMvSataAdapter, MV_U8 channel) 1506 { 1507 IAL_ADAPTER_T *pAdapter = (IAL_ADAPTER_T *)pMvSataAdapter->IALData; 1508 1509 mvSataDisableChannelDma(pMvSataAdapter, channel); 1510 /* Flush pending commands */ 1511 mvSataFlushDmaQueue (pMvSataAdapter, channel, MV_FLUSH_TYPE_CALLBACK); 1512 1513 /* Software reset channel */ 1514 if (mvStorageDevATASoftResetDevice(pMvSataAdapter, channel) == MV_FALSE) 1515 { 1516 MV_ERROR("RR18xx [%d,%d]: failed to perform Software reset\n", 1517 pMvSataAdapter->adapterId, channel); 1518 hptmv_free_channel(pAdapter, channel); 1519 return -1; 1520 } 1521 1522 /* Hardware reset channel */ 1523 if (mvSataChannelHardReset(pMvSataAdapter, channel)== MV_FALSE) 1524 { 1525 MV_ERROR("RR18xx [%d,%d] Failed to Hard reser the SATA channel\n", 1526 pMvSataAdapter->adapterId, channel); 1527 hptmv_free_channel(pAdapter, channel); 1528 return -1; 1529 } 1530 1531 if (mvSataIsStorageDeviceConnected(pMvSataAdapter, channel) == MV_FALSE) 1532 { 1533 MV_ERROR("RR18xx [%d,%d] Failed to Connect Device\n", 1534 pMvSataAdapter->adapterId, channel); 1535 hptmv_free_channel(pAdapter, channel); 1536 return -1; 1537 }else 1538 { 1539 MV_ERROR("channel %d: perform recalibrate command", channel); 1540 if (!mvStorageDevATAExecuteNonUDMACommand(pMvSataAdapter, channel, 1541 MV_NON_UDMA_PROTOCOL_NON_DATA, 1542 MV_FALSE, 1543 NULL, /* pBuffer*/ 1544 0, /* count */ 1545 0, /*features*/ 1546 /* sectorCount */ 1547 0, 1548 0, /* lbaLow */ 1549 0, /* lbaMid */ 1550 /* lbaHigh */ 1551 0, 1552 0, /* device */ 1553 /* command */ 1554 0x10)) 1555 MV_ERROR("channel %d: recalibrate failed", channel); 1556 1557 /* Set transfer mode */ 1558 if((mvStorageDevATASetFeatures(pMvSataAdapter, channel, 1559 MV_ATA_SET_FEATURES_TRANSFER, 1560 MV_ATA_TRANSFER_PIO_SLOW, 0, 0, 0) == MV_FALSE) || 1561 (mvStorageDevATASetFeatures(pMvSataAdapter, channel, 1562 MV_ATA_SET_FEATURES_TRANSFER, 1563 pAdapter->mvChannel[channel].maxPioModeSupported, 0, 0, 0) == MV_FALSE) || 1564 (mvStorageDevATASetFeatures(pMvSataAdapter, channel, 1565 MV_ATA_SET_FEATURES_TRANSFER, 1566 pAdapter->mvChannel[channel].maxUltraDmaModeSupported, 0, 0, 0) == MV_FALSE) ) 1567 { 1568 MV_ERROR("channel %d: Set Features failed", channel); 1569 hptmv_free_channel(pAdapter, channel); 1570 return -1; 1571 } 1572 /* Enable EDMA */ 1573 if (mvSataEnableChannelDma(pMvSataAdapter, channel) == MV_FALSE) 1574 { 1575 MV_ERROR("Failed to enable DMA, channel=%d", channel); 1576 hptmv_free_channel(pAdapter, channel); 1577 return -1; 1578 } 1579 } 1580 return 0; 1581 } 1582 1583 static int 1584 fResetActiveCommands(PVBus _vbus_p) 1585 { 1586 MV_SATA_ADAPTER *pMvSataAdapter = &((IAL_ADAPTER_T *)_vbus_p->OsExt)->mvSataAdapter; 1587 MV_U8 channel; 1588 for (channel=0;channel< MV_SATA_CHANNELS_NUM;channel++) { 1589 if (pMvSataAdapter->sataChannel[channel] && pMvSataAdapter->sataChannel[channel]->outstandingCommands) 1590 MvSataResetChannel(pMvSataAdapter,channel); 1591 } 1592 return 0; 1593 } 1594 1595 void fCompleteAllCommandsSynchronously(PVBus _vbus_p) 1596 { 1597 UINT cont; 1598 ULONG ticks = 0; 1599 MV_U8 channel; 1600 MV_SATA_ADAPTER *pMvSataAdapter = &((IAL_ADAPTER_T *)_vbus_p->OsExt)->mvSataAdapter; 1601 MV_SATA_CHANNEL *pMvSataChannel; 1602 1603 do { 1604 check_cmds: 1605 cont = 0; 1606 CheckPendingCall(_VBUS_P0); 1607 #ifdef _RAID5N_ 1608 dataxfer_poll(); 1609 xor_poll(); 1610 #endif 1611 for (channel=0;channel< MV_SATA_CHANNELS_NUM;channel++) { 1612 pMvSataChannel = pMvSataAdapter->sataChannel[channel]; 1613 if (pMvSataChannel && pMvSataChannel->outstandingCommands) 1614 { 1615 while (pMvSataChannel->outstandingCommands) { 1616 if (!mvSataInterruptServiceRoutine(pMvSataAdapter)) { 1617 StallExec(1000); 1618 if (ticks++ > 3000) { 1619 MvSataResetChannel(pMvSataAdapter,channel); 1620 goto check_cmds; 1621 } 1622 } 1623 else 1624 ticks = 0; 1625 } 1626 cont = 1; 1627 } 1628 } 1629 } while (cont); 1630 } 1631 1632 void 1633 fResetVBus(_VBUS_ARG0) 1634 { 1635 KdPrint(("fMvResetBus(%p)", _vbus_p)); 1636 1637 /* some commands may already finished. */ 1638 CheckPendingCall(_VBUS_P0); 1639 1640 fResetActiveCommands(_vbus_p); 1641 /* 1642 * the other pending commands may still be finished successfully. 1643 */ 1644 fCompleteAllCommandsSynchronously(_vbus_p); 1645 1646 /* Now there should be no pending commands. No more action needed. */ 1647 CheckIdleCall(_VBUS_P0); 1648 1649 KdPrint(("fMvResetBus() done")); 1650 } 1651 1652 /*No rescan function*/ 1653 void 1654 fRescanAllDevice(_VBUS_ARG0) 1655 { 1656 } 1657 1658 static MV_BOOLEAN 1659 CommandCompletionCB(MV_SATA_ADAPTER *pMvSataAdapter, 1660 MV_U8 channelNum, 1661 MV_COMPLETION_TYPE comp_type, 1662 MV_VOID_PTR commandId, 1663 MV_U16 responseFlags, 1664 MV_U32 timeStamp, 1665 MV_STORAGE_DEVICE_REGISTERS *registerStruct) 1666 { 1667 PCommand pCmd = (PCommand) commandId; 1668 _VBUS_INST(pCmd->pVDevice->pVBus) 1669 1670 if (pCmd->uScratch.sata_param.prdAddr) 1671 FreePRDTable(pMvSataAdapter->IALData,pCmd->uScratch.sata_param.prdAddr); 1672 1673 switch (comp_type) 1674 { 1675 case MV_COMPLETION_TYPE_NORMAL: 1676 pCmd->Result = RETURN_SUCCESS; 1677 break; 1678 case MV_COMPLETION_TYPE_ABORT: 1679 pCmd->Result = RETURN_BUS_RESET; 1680 break; 1681 case MV_COMPLETION_TYPE_ERROR: 1682 MV_ERROR("IAL: COMPLETION ERROR, adapter %d, channel %d, flags=%x\n", 1683 pMvSataAdapter->adapterId, channelNum, responseFlags); 1684 1685 if (responseFlags & 4) { 1686 MV_ERROR("ATA regs: error %x, sector count %x, LBA low %x, LBA mid %x," 1687 " LBA high %x, device %x, status %x\n", 1688 registerStruct->errorRegister, 1689 registerStruct->sectorCountRegister, 1690 registerStruct->lbaLowRegister, 1691 registerStruct->lbaMidRegister, 1692 registerStruct->lbaHighRegister, 1693 registerStruct->deviceRegister, 1694 registerStruct->statusRegister); 1695 } 1696 /*We can't do handleEdmaError directly here, because CommandCompletionCB is called by 1697 * mv's ISR, if we retry the command, than the internel data structure may be destroyed*/ 1698 pCmd->uScratch.sata_param.responseFlags = responseFlags; 1699 pCmd->uScratch.sata_param.bIdeStatus = registerStruct->statusRegister; 1700 pCmd->uScratch.sata_param.errorRegister = registerStruct->errorRegister; 1701 pCmd->pVDevice->u.disk.QueueLength--; 1702 CallAfterReturn(_VBUS_P (DPC_PROC)handleEdmaError,pCmd); 1703 return TRUE; 1704 1705 default: 1706 MV_ERROR(" Unknown completion type (%d)\n", comp_type); 1707 return MV_FALSE; 1708 } 1709 1710 if (pCmd->uCmd.Ide.Command == IDE_COMMAND_VERIFY && pCmd->uScratch.sata_param.cmd_priv > 1) { 1711 pCmd->uScratch.sata_param.cmd_priv --; 1712 return TRUE; 1713 } 1714 pCmd->pVDevice->u.disk.QueueLength--; 1715 CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd); 1716 return TRUE; 1717 } 1718 1719 void 1720 fDeviceSendCommand(_VBUS_ARG PCommand pCmd) 1721 { 1722 MV_SATA_EDMA_PRD_ENTRY *pPRDTable = NULL; 1723 MV_SATA_ADAPTER *pMvSataAdapter; 1724 MV_SATA_CHANNEL *pMvSataChannel; 1725 PVDevice pVDevice = pCmd->pVDevice; 1726 PDevice pDevice = &pVDevice->u.disk; 1727 LBA_T Lba = pCmd->uCmd.Ide.Lba; 1728 USHORT nSector = pCmd->uCmd.Ide.nSectors; 1729 1730 MV_QUEUE_COMMAND_RESULT result; 1731 MV_QUEUE_COMMAND_INFO commandInfo; 1732 MV_UDMA_COMMAND_PARAMS *pUdmaParams = &commandInfo.commandParams.udmaCommand; 1733 MV_NONE_UDMA_COMMAND_PARAMS *pNoUdmaParams = &commandInfo.commandParams.NoneUdmaCommand; 1734 1735 MV_BOOLEAN is48bit; 1736 MV_U8 channel; 1737 int i=0; 1738 1739 DECLARE_BUFFER(FPSCAT_GATH, tmpSg); 1740 1741 if (!pDevice->df_on_line) { 1742 MV_ERROR("Device is offline"); 1743 pCmd->Result = RETURN_BAD_DEVICE; 1744 CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd); 1745 return; 1746 } 1747 1748 pDevice->HeadPosition = pCmd->uCmd.Ide.Lba + pCmd->uCmd.Ide.nSectors; 1749 pMvSataChannel = pDevice->mv; 1750 pMvSataAdapter = pMvSataChannel->mvSataAdapter; 1751 channel = pMvSataChannel->channelNumber; 1752 1753 /* old RAID0 has hidden lba. Remember to clear dDeHiddenLba when delete array! */ 1754 Lba += pDevice->dDeHiddenLba; 1755 /* check LBA */ 1756 if (Lba+nSector-1 > pDevice->dDeRealCapacity) { 1757 pCmd->Result = RETURN_INVALID_REQUEST; 1758 CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd); 1759 return; 1760 } 1761 1762 /* 1763 * always use 48bit LBA if drive supports it. 1764 * Some Seagate drives report error if you use a 28-bit command 1765 * to access sector 0xfffffff. 1766 */ 1767 is48bit = pMvSataChannel->lba48Address; 1768 1769 switch (pCmd->uCmd.Ide.Command) 1770 { 1771 case IDE_COMMAND_READ: 1772 case IDE_COMMAND_WRITE: 1773 if (pDevice->bDeModeSetting<8) goto pio; 1774 1775 commandInfo.type = MV_QUEUED_COMMAND_TYPE_UDMA; 1776 pUdmaParams->isEXT = is48bit; 1777 pUdmaParams->numOfSectors = nSector; 1778 pUdmaParams->lowLBAAddress = Lba; 1779 pUdmaParams->highLBAAddress = 0; 1780 pUdmaParams->prdHighAddr = 0; 1781 pUdmaParams->callBack = CommandCompletionCB; 1782 pUdmaParams->commandId = (MV_VOID_PTR )pCmd; 1783 if(pCmd->uCmd.Ide.Command == IDE_COMMAND_READ) 1784 pUdmaParams->readWrite = MV_UDMA_TYPE_READ; 1785 else 1786 pUdmaParams->readWrite = MV_UDMA_TYPE_WRITE; 1787 1788 if (pCmd->pSgTable && pCmd->cf_physical_sg) { 1789 FPSCAT_GATH sg1=tmpSg, sg2=pCmd->pSgTable; 1790 do { *sg1++=*sg2; } while ((sg2++->wSgFlag & SG_FLAG_EOT)==0); 1791 } 1792 else { 1793 if (!pCmd->pfnBuildSgl || !pCmd->pfnBuildSgl(_VBUS_P pCmd, tmpSg, 0)) { 1794 pio: 1795 mvSataDisableChannelDma(pMvSataAdapter, channel); 1796 mvSataFlushDmaQueue(pMvSataAdapter, channel, MV_FLUSH_TYPE_CALLBACK); 1797 1798 if (pCmd->pSgTable && pCmd->cf_physical_sg==0) { 1799 FPSCAT_GATH sg1=tmpSg, sg2=pCmd->pSgTable; 1800 do { *sg1++=*sg2; } while ((sg2++->wSgFlag & SG_FLAG_EOT)==0); 1801 } 1802 else { 1803 if (!pCmd->pfnBuildSgl || !pCmd->pfnBuildSgl(_VBUS_P pCmd, tmpSg, 1)) { 1804 pCmd->Result = RETURN_NEED_LOGICAL_SG; 1805 goto finish_cmd; 1806 } 1807 } 1808 1809 do { 1810 ULONG size = tmpSg->wSgSize? tmpSg->wSgSize : 0x10000; 1811 ULONG_PTR addr = tmpSg->dSgAddress; 1812 if (size & 0x1ff) { 1813 pCmd->Result = RETURN_INVALID_REQUEST; 1814 goto finish_cmd; 1815 } 1816 if (mvStorageDevATAExecuteNonUDMACommand(pMvSataAdapter, channel, 1817 (pCmd->cf_data_out)?MV_NON_UDMA_PROTOCOL_PIO_DATA_OUT:MV_NON_UDMA_PROTOCOL_PIO_DATA_IN, 1818 is48bit, 1819 (MV_U16_PTR)addr, 1820 size >> 1, /* count */ 1821 0, /* features N/A */ 1822 (MV_U16)(size>>9), /*sector count*/ 1823 (MV_U16)( (is48bit? (MV_U16)((Lba >> 16) & 0xFF00) : 0 ) | (UCHAR)(Lba & 0xFF) ), /*lbalow*/ 1824 (MV_U16)((Lba >> 8) & 0xFF), /* lbaMid */ 1825 (MV_U16)((Lba >> 16) & 0xFF),/* lbaHigh */ 1826 (MV_U8)(0x40 | (is48bit ? 0 : (UCHAR)(Lba >> 24) & 0xFF )),/* device */ 1827 (MV_U8)(is48bit ? (pCmd->cf_data_in?IDE_COMMAND_READ_EXT:IDE_COMMAND_WRITE_EXT):pCmd->uCmd.Ide.Command) 1828 )==MV_FALSE) 1829 { 1830 pCmd->Result = RETURN_IDE_ERROR; 1831 goto finish_cmd; 1832 } 1833 Lba += size>>9; 1834 if(Lba & 0xF0000000) is48bit = MV_TRUE; 1835 } 1836 while ((tmpSg++->wSgFlag & SG_FLAG_EOT)==0); 1837 pCmd->Result = RETURN_SUCCESS; 1838 finish_cmd: 1839 mvSataEnableChannelDma(pMvSataAdapter,channel); 1840 CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd); 1841 return; 1842 } 1843 } 1844 1845 pPRDTable = (MV_SATA_EDMA_PRD_ENTRY *) AllocatePRDTable(pMvSataAdapter->IALData); 1846 KdPrint(("pPRDTable:%p\n",pPRDTable)); 1847 if (!pPRDTable) { 1848 pCmd->Result = RETURN_DEVICE_BUSY; 1849 CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd); 1850 HPT_ASSERT(0); 1851 return; 1852 } 1853 1854 do{ 1855 pPRDTable[i].highBaseAddr = (sizeof(tmpSg->dSgAddress)>4 ? (MV_U32)(tmpSg->dSgAddress>>32) : 0); 1856 pPRDTable[i].flags = (MV_U16)tmpSg->wSgFlag; 1857 pPRDTable[i].byteCount = (MV_U16)tmpSg->wSgSize; 1858 pPRDTable[i].lowBaseAddr = (MV_U32)tmpSg->dSgAddress; 1859 pPRDTable[i].reserved = 0; 1860 i++; 1861 }while((tmpSg++->wSgFlag & SG_FLAG_EOT)==0); 1862 1863 pUdmaParams->prdLowAddr = (ULONG)fOsPhysicalAddress(pPRDTable); 1864 if ((pUdmaParams->numOfSectors == 256) && (pMvSataChannel->lba48Address == MV_FALSE)) { 1865 pUdmaParams->numOfSectors = 0; 1866 } 1867 1868 pCmd->uScratch.sata_param.prdAddr = (PVOID)pPRDTable; 1869 1870 result = mvSataQueueCommand(pMvSataAdapter, channel, &commandInfo); 1871 1872 if (result != MV_QUEUE_COMMAND_RESULT_OK) 1873 { 1874 queue_failed: 1875 switch (result) 1876 { 1877 case MV_QUEUE_COMMAND_RESULT_BAD_LBA_ADDRESS: 1878 MV_ERROR("IAL Error: Edma Queue command failed. Bad LBA " 1879 "LBA[31:0](0x%08x)\n", pUdmaParams->lowLBAAddress); 1880 pCmd->Result = RETURN_IDE_ERROR; 1881 break; 1882 case MV_QUEUE_COMMAND_RESULT_QUEUED_MODE_DISABLED: 1883 MV_ERROR("IAL Error: Edma Queue command failed. EDMA" 1884 " disabled adapter %d channel %d\n", 1885 pMvSataAdapter->adapterId, channel); 1886 mvSataEnableChannelDma(pMvSataAdapter,channel); 1887 pCmd->Result = RETURN_IDE_ERROR; 1888 break; 1889 case MV_QUEUE_COMMAND_RESULT_FULL: 1890 MV_ERROR("IAL Error: Edma Queue command failed. Queue is" 1891 " Full adapter %d channel %d\n", 1892 pMvSataAdapter->adapterId, channel); 1893 pCmd->Result = RETURN_DEVICE_BUSY; 1894 break; 1895 case MV_QUEUE_COMMAND_RESULT_BAD_PARAMS: 1896 MV_ERROR("IAL Error: Edma Queue command failed. (Bad " 1897 "Params), pMvSataAdapter: %p, pSataChannel: %p.\n", 1898 pMvSataAdapter, pMvSataAdapter->sataChannel[channel]); 1899 pCmd->Result = RETURN_IDE_ERROR; 1900 break; 1901 default: 1902 MV_ERROR("IAL Error: Bad result value (%d) from queue" 1903 " command\n", result); 1904 pCmd->Result = RETURN_IDE_ERROR; 1905 } 1906 if(pPRDTable) 1907 FreePRDTable(pMvSataAdapter->IALData,pPRDTable); 1908 CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd); 1909 } 1910 pDevice->QueueLength++; 1911 return; 1912 1913 case IDE_COMMAND_VERIFY: 1914 commandInfo.type = MV_QUEUED_COMMAND_TYPE_NONE_UDMA; 1915 pNoUdmaParams->bufPtr = NULL; 1916 pNoUdmaParams->callBack = CommandCompletionCB; 1917 pNoUdmaParams->commandId = (MV_VOID_PTR)pCmd; 1918 pNoUdmaParams->count = 0; 1919 pNoUdmaParams->features = 0; 1920 pNoUdmaParams->protocolType = MV_NON_UDMA_PROTOCOL_NON_DATA; 1921 1922 pCmd->uScratch.sata_param.cmd_priv = 1; 1923 if (pMvSataChannel->lba48Address == MV_TRUE){ 1924 pNoUdmaParams->command = MV_ATA_COMMAND_READ_VERIFY_SECTORS_EXT; 1925 pNoUdmaParams->isEXT = MV_TRUE; 1926 pNoUdmaParams->lbaHigh = (MV_U16)((Lba & 0xff0000) >> 16); 1927 pNoUdmaParams->lbaMid = (MV_U16)((Lba & 0xff00) >> 8); 1928 pNoUdmaParams->lbaLow = 1929 (MV_U16)(((Lba & 0xff000000) >> 16)| (Lba & 0xff)); 1930 pNoUdmaParams->sectorCount = nSector; 1931 pNoUdmaParams->device = 0x40; 1932 result = mvSataQueueCommand(pMvSataAdapter, channel, &commandInfo); 1933 if (result != MV_QUEUE_COMMAND_RESULT_OK){ 1934 goto queue_failed; 1935 } 1936 return; 1937 } 1938 else{ 1939 pNoUdmaParams->command = MV_ATA_COMMAND_READ_VERIFY_SECTORS; 1940 pNoUdmaParams->isEXT = MV_FALSE; 1941 pNoUdmaParams->lbaHigh = (MV_U16)((Lba & 0xff0000) >> 16); 1942 pNoUdmaParams->lbaMid = (MV_U16)((Lba & 0xff00) >> 8); 1943 pNoUdmaParams->lbaLow = (MV_U16)(Lba & 0xff); 1944 pNoUdmaParams->sectorCount = 0xff & nSector; 1945 pNoUdmaParams->device = (MV_U8)(0x40 | 1946 ((Lba & 0xf000000) >> 24)); 1947 pNoUdmaParams->callBack = CommandCompletionCB; 1948 result = mvSataQueueCommand(pMvSataAdapter, channel, &commandInfo); 1949 /*FIXME: how about the commands already queued? but marvel also forgets to consider this*/ 1950 if (result != MV_QUEUE_COMMAND_RESULT_OK){ 1951 goto queue_failed; 1952 } 1953 } 1954 break; 1955 default: 1956 pCmd->Result = RETURN_INVALID_REQUEST; 1957 CallAfterReturn(_VBUS_P (DPC_PROC)pCmd->pfnCompletion, pCmd); 1958 break; 1959 } 1960 } 1961 1962 /********************************************************** 1963 * 1964 * Probe the hostadapter. 1965 * 1966 **********************************************************/ 1967 static int 1968 hpt_probe(device_t dev) 1969 { 1970 if ((pci_get_vendor(dev) == MV_SATA_VENDOR_ID) && 1971 (pci_get_device(dev) == MV_SATA_DEVICE_ID_5081 1972 #ifdef FOR_DEMO 1973 || pci_get_device(dev) == MV_SATA_DEVICE_ID_5080 1974 #endif 1975 )) 1976 { 1977 KdPrintI((CONTROLLER_NAME " found\n")); 1978 device_set_desc(dev, CONTROLLER_NAME); 1979 return 0; 1980 } 1981 else 1982 return(ENXIO); 1983 } 1984 1985 /*********************************************************** 1986 * 1987 * Auto configuration: attach and init a host adapter. 1988 * 1989 ***********************************************************/ 1990 static int 1991 hpt_attach(device_t dev) 1992 { 1993 IAL_ADAPTER_T * pAdapter = device_get_softc(dev); 1994 int rid; 1995 union ccb *ccb; 1996 struct cam_devq *devq; 1997 struct cam_sim *hpt_vsim; 1998 1999 kprintf("%s Version %s \n", DRIVER_NAME, DRIVER_VERSION); 2000 2001 if (!pAdapter) 2002 { 2003 pAdapter = (IAL_ADAPTER_T *)kmalloc(sizeof (IAL_ADAPTER_T), M_DEVBUF, M_NOWAIT); 2004 device_set_softc(dev, (void *)pAdapter); 2005 } 2006 2007 if (!pAdapter) return (ENOMEM); 2008 bzero(pAdapter, sizeof(IAL_ADAPTER_T)); 2009 2010 pAdapter->hpt_dev = dev; 2011 2012 rid = init_adapter(pAdapter); 2013 if (rid) 2014 return rid; 2015 2016 rid = 0; 2017 if ((pAdapter->hpt_irq = bus_alloc_resource(pAdapter->hpt_dev, SYS_RES_IRQ, &rid, 0, ~0ul, 1, RF_SHAREABLE | RF_ACTIVE)) == NULL) 2018 { 2019 hpt_printk(("can't allocate interrupt\n")); 2020 return(ENXIO); 2021 } 2022 2023 if (bus_setup_intr(pAdapter->hpt_dev, pAdapter->hpt_irq, 0, 2024 hpt_intr, pAdapter, &pAdapter->hpt_intr, NULL)) 2025 { 2026 hpt_printk(("can't set up interrupt\n")); 2027 kfree(pAdapter, M_DEVBUF); 2028 return(ENXIO); 2029 } 2030 2031 ccb = xpt_alloc_ccb(); 2032 ccb->ccb_h.pinfo.priority = 1; 2033 ccb->ccb_h.pinfo.index = CAM_UNQUEUED_INDEX; 2034 2035 /* 2036 * Create the device queue for our SIM(s). 2037 */ 2038 if((devq = cam_simq_alloc(8/*MAX_QUEUE_COMM*/)) == NULL) 2039 { 2040 KdPrint(("ENXIO\n")); 2041 return ENOMEM; 2042 } 2043 2044 /* 2045 * Construct our SIM entry 2046 */ 2047 hpt_vsim = cam_sim_alloc(hpt_action, hpt_poll, __str(PROC_DIR_NAME), 2048 pAdapter, device_get_unit(pAdapter->hpt_dev), &sim_mplock, 1, 8, devq); 2049 cam_simq_release(devq); 2050 if (hpt_vsim == NULL) { 2051 return ENOMEM; 2052 } 2053 2054 if (xpt_bus_register(hpt_vsim, 0) != CAM_SUCCESS) 2055 { 2056 cam_sim_free(hpt_vsim); 2057 hpt_vsim = NULL; 2058 return ENXIO; 2059 } 2060 2061 if(xpt_create_path(&pAdapter->path, /*periph */ NULL, 2062 cam_sim_path(hpt_vsim), CAM_TARGET_WILDCARD, 2063 CAM_LUN_WILDCARD) != CAM_REQ_CMP) 2064 { 2065 xpt_bus_deregister(cam_sim_path(hpt_vsim)); 2066 cam_sim_free(hpt_vsim); 2067 hpt_vsim = NULL; 2068 return ENXIO; 2069 } 2070 2071 xpt_setup_ccb(&ccb->ccb_h, pAdapter->path, /*priority*/5); 2072 ccb->ccb_h.func_code = XPT_SASYNC_CB; 2073 ccb->csa.event_enable = AC_LOST_DEVICE; 2074 ccb->csa.callback = hpt_async; 2075 ccb->csa.callback_arg = hpt_vsim; 2076 xpt_action(ccb); 2077 xpt_free_ccb(&ccb->ccb_h); 2078 2079 callout_init(&pAdapter->event_timer_connect); 2080 callout_init(&pAdapter->event_timer_disconnect); 2081 2082 if (device_get_unit(dev) == 0) { 2083 /* Start the work thread. XXX */ 2084 launch_worker_thread(); 2085 2086 /* 2087 * hpt_worker_thread needs to be suspended after shutdown 2088 * sync, when fs sync finished. 2089 */ 2090 pAdapter->eh = EVENTHANDLER_REGISTER(shutdown_post_sync, 2091 shutdown_kproc, hptdaemonproc, SHUTDOWN_PRI_FIRST); 2092 } 2093 2094 return 0; 2095 } 2096 2097 static int 2098 hpt_detach(device_t dev) 2099 { 2100 return (EBUSY); 2101 } 2102 2103 2104 /*************************************************************** 2105 * The poll function is used to simulate the interrupt when 2106 * the interrupt subsystem is not functioning. 2107 * 2108 ***************************************************************/ 2109 static void 2110 hpt_poll(struct cam_sim *sim) 2111 { 2112 hpt_intr((void *)cam_sim_softc(sim)); 2113 } 2114 2115 /**************************************************************** 2116 * Name: hpt_intr 2117 * Description: Interrupt handler. 2118 ****************************************************************/ 2119 static void 2120 hpt_intr(void *arg) 2121 { 2122 IAL_ADAPTER_T *pAdapter = (IAL_ADAPTER_T *)arg; 2123 2124 lock_driver(); 2125 /* KdPrintI(("----- Entering Isr() -----\n")); */ 2126 if (mvSataInterruptServiceRoutine(&pAdapter->mvSataAdapter) == MV_TRUE) 2127 { 2128 _VBUS_INST(&pAdapter->VBus) 2129 CheckPendingCall(_VBUS_P0); 2130 } 2131 2132 /* KdPrintI(("----- Leaving Isr() -----\n")); */ 2133 unlock_driver(); 2134 } 2135 2136 /********************************************************** 2137 * Asynchronous Events 2138 *********************************************************/ 2139 #if (!defined(UNREFERENCED_PARAMETER)) 2140 #define UNREFERENCED_PARAMETER(x) (void)(x) 2141 #endif 2142 2143 static void 2144 hpt_async(void * callback_arg, u_int32_t code, struct cam_path * path, 2145 void * arg) 2146 { 2147 /* debug XXXX */ 2148 panic("Here"); 2149 UNREFERENCED_PARAMETER(callback_arg); 2150 UNREFERENCED_PARAMETER(code); 2151 UNREFERENCED_PARAMETER(path); 2152 UNREFERENCED_PARAMETER(arg); 2153 2154 } 2155 2156 static void 2157 FlushAdapter(IAL_ADAPTER_T *pAdapter) 2158 { 2159 int i; 2160 2161 hpt_printk(("flush all devices\n")); 2162 2163 /* flush all devices */ 2164 for (i=0; i<MAX_VDEVICE_PER_VBUS; i++) { 2165 PVDevice pVDev = pAdapter->VBus.pVDevice[i]; 2166 if(pVDev) fFlushVDev(pVDev); 2167 } 2168 } 2169 2170 static int 2171 hpt_shutdown(device_t dev) 2172 { 2173 IAL_ADAPTER_T *pAdapter; 2174 2175 pAdapter = device_get_softc(dev); 2176 if (pAdapter == NULL) 2177 return (EINVAL); 2178 2179 EVENTHANDLER_DEREGISTER(shutdown_post_sync, pAdapter->eh); 2180 FlushAdapter(pAdapter); 2181 /* give the flush some time to happen, 2182 *otherwise "shutdown -p now" will make file system corrupted */ 2183 DELAY(1000 * 1000 * 5); 2184 return 0; 2185 } 2186 2187 void 2188 Check_Idle_Call(IAL_ADAPTER_T *pAdapter) 2189 { 2190 _VBUS_INST(&pAdapter->VBus) 2191 2192 if (mWaitingForIdle(_VBUS_P0)) { 2193 CheckIdleCall(_VBUS_P0); 2194 #ifdef SUPPORT_ARRAY 2195 { 2196 int i; 2197 PVDevice pArray; 2198 for(i = 0; i < MAX_ARRAY_PER_VBUS; i++){ 2199 if ((pArray=ArrayTables(i))->u.array.dArStamp==0) 2200 continue; 2201 else if (pArray->u.array.rf_auto_rebuild) { 2202 KdPrint(("auto rebuild.\n")); 2203 pArray->u.array.rf_auto_rebuild = 0; 2204 hpt_queue_dpc((HPT_DPC)hpt_rebuild_data_block, pAdapter, pArray, DUPLICATE); 2205 } 2206 } 2207 } 2208 #endif 2209 } 2210 /* launch the awaiting commands blocked by mWaitingForIdle */ 2211 while(pAdapter->pending_Q!= NULL) 2212 { 2213 _VBUS_INST(&pAdapter->VBus) 2214 union ccb *ccb = (union ccb *)pAdapter->pending_Q->ccb_h.ccb_ccb_ptr; 2215 hpt_free_ccb(&pAdapter->pending_Q, ccb); 2216 CallAfterReturn(_VBUS_P (DPC_PROC)OsSendCommand, ccb); 2217 } 2218 } 2219 2220 static void 2221 ccb_done(union ccb *ccb) 2222 { 2223 PBUS_DMAMAP pmap = (PBUS_DMAMAP)ccb->ccb_adapter; 2224 IAL_ADAPTER_T * pAdapter = pmap->pAdapter; 2225 KdPrintI(("ccb_done: ccb %p status %x\n", ccb, ccb->ccb_h.status)); 2226 2227 dmamap_put(pmap); 2228 xpt_done(ccb); 2229 2230 pAdapter->outstandingCommands--; 2231 2232 if (pAdapter->outstandingCommands == 0) 2233 { 2234 if(DPC_Request_Nums == 0) 2235 Check_Idle_Call(pAdapter); 2236 } 2237 } 2238 2239 /**************************************************************** 2240 * Name: hpt_action 2241 * Description: Process a queued command from the CAM layer. 2242 * Parameters: sim - Pointer to SIM object 2243 * ccb - Pointer to SCSI command structure. 2244 ****************************************************************/ 2245 2246 static void 2247 hpt_action(struct cam_sim *sim, union ccb *ccb) 2248 { 2249 IAL_ADAPTER_T * pAdapter = (IAL_ADAPTER_T *) cam_sim_softc(sim); 2250 PBUS_DMAMAP pmap; 2251 _VBUS_INST(&pAdapter->VBus) 2252 2253 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("hpt_action\n")); 2254 KdPrint(("hpt_action(%lx,%lx{%x})\n", (u_long)sim, (u_long)ccb, ccb->ccb_h.func_code)); 2255 2256 switch (ccb->ccb_h.func_code) 2257 { 2258 case XPT_SCSI_IO: /* Execute the requested I/O operation */ 2259 { 2260 /* ccb->ccb_h.path_id is not our bus id - don't check it */ 2261 2262 if (ccb->ccb_h.target_lun) { 2263 ccb->ccb_h.status = CAM_LUN_INVALID; 2264 xpt_done(ccb); 2265 return; 2266 } 2267 if (ccb->ccb_h.target_id >= MAX_VDEVICE_PER_VBUS || 2268 pAdapter->VBus.pVDevice[ccb->ccb_h.target_id]==0) { 2269 ccb->ccb_h.status = CAM_TID_INVALID; 2270 xpt_done(ccb); 2271 return; 2272 } 2273 2274 lock_driver(); 2275 if (pAdapter->outstandingCommands==0 && DPC_Request_Nums==0) 2276 Check_Idle_Call(pAdapter); 2277 2278 pmap = dmamap_get(pAdapter); 2279 HPT_ASSERT(pmap); 2280 ccb->ccb_adapter = pmap; 2281 memset((void *)pmap->psg, 0, sizeof(pmap->psg)); 2282 2283 if (mWaitingForIdle(_VBUS_P0)) 2284 hpt_queue_ccb(&pAdapter->pending_Q, ccb); 2285 else 2286 OsSendCommand(_VBUS_P ccb); 2287 unlock_driver(); 2288 2289 /* KdPrint(("leave scsiio\n")); */ 2290 break; 2291 } 2292 2293 case XPT_RESET_BUS: 2294 KdPrint(("reset bus\n")); 2295 lock_driver(); 2296 fResetVBus(_VBUS_P0); 2297 unlock_driver(); 2298 xpt_done(ccb); 2299 break; 2300 2301 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */ 2302 case XPT_EN_LUN: /* Enable LUN as a target */ 2303 case XPT_TARGET_IO: /* Execute target I/O request */ 2304 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */ 2305 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/ 2306 case XPT_ABORT: /* Abort the specified CCB */ 2307 case XPT_TERM_IO: /* Terminate the I/O process */ 2308 /* XXX Implement */ 2309 ccb->ccb_h.status = CAM_REQ_INVALID; 2310 xpt_done(ccb); 2311 break; 2312 2313 case XPT_GET_TRAN_SETTINGS: 2314 case XPT_SET_TRAN_SETTINGS: 2315 /* XXX Implement */ 2316 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; 2317 xpt_done(ccb); 2318 break; 2319 2320 case XPT_CALC_GEOMETRY: 2321 cam_calc_geometry(&ccb->ccg, 1); 2322 xpt_done(ccb); 2323 break; 2324 2325 case XPT_PATH_INQ: /* Path routing inquiry */ 2326 { 2327 struct ccb_pathinq *cpi = &ccb->cpi; 2328 2329 cpi->version_num = 1; /* XXX??? */ 2330 cpi->hba_inquiry = PI_SDTR_ABLE; 2331 cpi->target_sprt = 0; 2332 /* Not necessary to reset bus */ 2333 cpi->hba_misc = PIM_NOBUSRESET; 2334 cpi->hba_eng_cnt = 0; 2335 2336 cpi->max_target = MAX_VDEVICE_PER_VBUS; 2337 cpi->max_lun = 0; 2338 cpi->initiator_id = MAX_VDEVICE_PER_VBUS; 2339 2340 cpi->bus_id = cam_sim_bus(sim); 2341 cpi->base_transfer_speed = 3300; 2342 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 2343 strncpy(cpi->hba_vid, "HPT ", HBA_IDLEN); 2344 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 2345 cpi->unit_number = cam_sim_unit(sim); 2346 cpi->transport = XPORT_SPI; 2347 cpi->transport_version = 2; 2348 cpi->protocol = PROTO_SCSI; 2349 cpi->protocol_version = SCSI_REV_2; 2350 cpi->maxio = HPTMV_DFLTPHYS; 2351 cpi->ccb_h.status = CAM_REQ_CMP; 2352 xpt_done(ccb); 2353 break; 2354 } 2355 2356 default: 2357 KdPrint(("invalid cmd\n")); 2358 ccb->ccb_h.status = CAM_REQ_INVALID; 2359 xpt_done(ccb); 2360 break; 2361 } 2362 /* KdPrint(("leave hpt_action..............\n")); */ 2363 } 2364 2365 /* shall be called at lock_driver() */ 2366 static void 2367 hpt_queue_ccb(union ccb **ccb_Q, union ccb *ccb) 2368 { 2369 if(*ccb_Q == NULL) 2370 ccb->ccb_h.ccb_ccb_ptr = ccb; 2371 else { 2372 ccb->ccb_h.ccb_ccb_ptr = (*ccb_Q)->ccb_h.ccb_ccb_ptr; 2373 (*ccb_Q)->ccb_h.ccb_ccb_ptr = (char *)ccb; 2374 } 2375 2376 *ccb_Q = ccb; 2377 } 2378 2379 /* shall be called at lock_driver() */ 2380 static void 2381 hpt_free_ccb(union ccb **ccb_Q, union ccb *ccb) 2382 { 2383 union ccb *TempCCB; 2384 2385 TempCCB = *ccb_Q; 2386 2387 if(ccb->ccb_h.ccb_ccb_ptr == ccb) /*it means SCpnt is the last one in CURRCMDs*/ 2388 *ccb_Q = NULL; 2389 else { 2390 while(TempCCB->ccb_h.ccb_ccb_ptr != (char *)ccb) 2391 TempCCB = (union ccb *)TempCCB->ccb_h.ccb_ccb_ptr; 2392 2393 TempCCB->ccb_h.ccb_ccb_ptr = ccb->ccb_h.ccb_ccb_ptr; 2394 2395 if(*ccb_Q == ccb) 2396 *ccb_Q = TempCCB; 2397 } 2398 } 2399 2400 #ifdef SUPPORT_ARRAY 2401 /*************************************************************************** 2402 * Function: hpt_worker_thread 2403 * Description: Do background rebuilding. Execute in kernel thread context. 2404 * Returns: None 2405 ***************************************************************************/ 2406 static void hpt_worker_thread(void) 2407 { 2408 for(;;) { 2409 while (DpcQueue_First!=DpcQueue_Last) { 2410 ST_HPT_DPC p; 2411 lock_driver(); 2412 p = DpcQueue[DpcQueue_First]; 2413 DpcQueue_First++; 2414 DpcQueue_First %= MAX_DPC; 2415 DPC_Request_Nums++; 2416 unlock_driver(); 2417 p.dpc(p.pAdapter, p.arg, p.flags); 2418 2419 lock_driver(); 2420 DPC_Request_Nums--; 2421 /* since we may have prevented Check_Idle_Call, do it here */ 2422 if (DPC_Request_Nums==0) { 2423 if (p.pAdapter->outstandingCommands == 0) { 2424 _VBUS_INST(&p.pAdapter->VBus); 2425 Check_Idle_Call(p.pAdapter); 2426 CheckPendingCall(_VBUS_P0); 2427 } 2428 } 2429 unlock_driver(); 2430 2431 /*Schedule out*/ 2432 tsleep((caddr_t)hpt_worker_thread, 0, "sched", 1); 2433 if (SIGISMEMBER(curproc->p_siglist, SIGSTOP)) { 2434 /* abort rebuilding process. */ 2435 IAL_ADAPTER_T *pAdapter; 2436 PVDevice pArray; 2437 PVBus _vbus_p; 2438 int i; 2439 pAdapter = gIal_Adapter; 2440 2441 while(pAdapter != NULL){ 2442 2443 _vbus_p = &pAdapter->VBus; 2444 2445 for (i=0;i<MAX_ARRAY_PER_VBUS;i++) 2446 { 2447 if ((pArray=ArrayTables(i))->u.array.dArStamp==0) 2448 continue; 2449 else if (pArray->u.array.rf_rebuilding || 2450 pArray->u.array.rf_verifying || 2451 pArray->u.array.rf_initializing) 2452 { 2453 pArray->u.array.rf_abort_rebuild = 1; 2454 } 2455 } 2456 pAdapter = pAdapter->next; 2457 } 2458 } 2459 } 2460 2461 /*Remove this debug option*/ 2462 /* 2463 #ifdef DEBUG 2464 if (SIGISMEMBER(curproc->p_siglist, SIGSTOP)) 2465 tsleep((caddr_t)hpt_worker_thread, 0, "hptrdy", 2*hz); 2466 #endif 2467 */ 2468 kproc_suspend_loop(); 2469 tsleep((caddr_t)hpt_worker_thread, 0, "hptrdy", 2*hz); /* wait for something to do */ 2470 } 2471 } 2472 2473 static struct kproc_desc hpt_kp = { 2474 "hpt_wt", 2475 hpt_worker_thread, 2476 &hptdaemonproc 2477 }; 2478 2479 /*Start this thread in the hpt_attach, to prevent kernel from loading it without our controller.*/ 2480 static void 2481 launch_worker_thread(void) 2482 { 2483 IAL_ADAPTER_T *pAdapTemp; 2484 2485 kproc_start(&hpt_kp); 2486 2487 for (pAdapTemp = gIal_Adapter; pAdapTemp; pAdapTemp = pAdapTemp->next) { 2488 2489 _VBUS_INST(&pAdapTemp->VBus) 2490 int i; 2491 PVDevice pVDev; 2492 2493 for(i = 0; i < MAX_ARRAY_PER_VBUS; i++) 2494 if ((pVDev=ArrayTables(i))->u.array.dArStamp==0) 2495 continue; 2496 else{ 2497 if (pVDev->u.array.rf_need_rebuild && !pVDev->u.array.rf_rebuilding) 2498 hpt_queue_dpc((HPT_DPC)hpt_rebuild_data_block, pAdapTemp, pVDev, 2499 (UCHAR)((pVDev->u.array.CriticalMembers || pVDev->VDeviceType == VD_RAID_1)? DUPLICATE : REBUILD_PARITY)); 2500 } 2501 } 2502 } 2503 /* 2504 *SYSINIT(hptwt, SI_SUB_KTHREAD_IDLE, SI_ORDER_FIRST, launch_worker_thread, NULL); 2505 */ 2506 2507 #endif 2508 2509 /********************************************************************************/ 2510 2511 static int HPTLIBAPI fOsBuildSgl(_VBUS_ARG PCommand pCmd, FPSCAT_GATH pSg, int logical) 2512 { 2513 union ccb *ccb = (union ccb *)pCmd->pOrgCommand; 2514 bus_dma_segment_t *sgList = (bus_dma_segment_t *)ccb->csio.data_ptr; 2515 int idx; 2516 2517 if(logical) { 2518 if (ccb->ccb_h.flags & CAM_DATA_PHYS) 2519 panic("physical address unsupported"); 2520 2521 if (ccb->ccb_h.flags & CAM_SCATTER_VALID) { 2522 if (ccb->ccb_h.flags & CAM_SG_LIST_PHYS) 2523 panic("physical address unsupported"); 2524 2525 for (idx = 0; idx < ccb->csio.sglist_cnt; idx++) { 2526 pSg[idx].dSgAddress = (ULONG_PTR)(UCHAR *)sgList[idx].ds_addr; 2527 pSg[idx].wSgSize = sgList[idx].ds_len; 2528 pSg[idx].wSgFlag = (idx==ccb->csio.sglist_cnt-1)? SG_FLAG_EOT : 0; 2529 } 2530 } 2531 else { 2532 pSg->dSgAddress = (ULONG_PTR)(UCHAR *)ccb->csio.data_ptr; 2533 pSg->wSgSize = ccb->csio.dxfer_len; 2534 pSg->wSgFlag = SG_FLAG_EOT; 2535 } 2536 return TRUE; 2537 } 2538 2539 /* since we have provided physical sg, nobody will ask us to build physical sg */ 2540 HPT_ASSERT(0); 2541 return FALSE; 2542 } 2543 2544 /*******************************************************************************/ 2545 ULONG HPTLIBAPI 2546 GetStamp(void) 2547 { 2548 /* 2549 * the system variable, ticks, can't be used since it hasn't yet been active 2550 * when our driver starts (ticks==0, it's a invalid stamp value) 2551 */ 2552 ULONG stamp; 2553 do { stamp = krandom(); } while (stamp==0); 2554 return stamp; 2555 } 2556 2557 2558 static void 2559 SetInquiryData(PINQUIRYDATA inquiryData, PVDevice pVDev) 2560 { 2561 int i; 2562 IDENTIFY_DATA2 *pIdentify = (IDENTIFY_DATA2*)pVDev->u.disk.mv->identifyDevice; 2563 2564 inquiryData->DeviceType = T_DIRECT; /*DIRECT_ACCESS_DEVICE*/ 2565 inquiryData->AdditionalLength = (UCHAR)(sizeof(INQUIRYDATA) - 5); 2566 #ifndef SERIAL_CMDS 2567 inquiryData->CommandQueue = 1; 2568 #endif 2569 2570 switch(pVDev->VDeviceType) { 2571 case VD_SINGLE_DISK: 2572 case VD_ATAPI: 2573 case VD_REMOVABLE: 2574 /* Set the removable bit, if applicable. */ 2575 if ((pVDev->u.disk.df_removable_drive) || (pIdentify->GeneralConfiguration & 0x80)) 2576 inquiryData->RemovableMedia = 1; 2577 2578 /* Fill in vendor identification fields. */ 2579 for (i = 0; i < 8; i += 2) { 2580 inquiryData->VendorId[i] = ((PUCHAR)pIdentify->ModelNumber)[i + 1]; 2581 inquiryData->VendorId[i+1] = ((PUCHAR)pIdentify->ModelNumber)[i]; 2582 2583 } 2584 2585 /* Initialize unused portion of product id. */ 2586 for (i = 0; i < 4; i++) inquiryData->ProductId[12+i] = ' '; 2587 2588 /* firmware revision */ 2589 for (i = 0; i < 4; i += 2) 2590 { 2591 inquiryData->ProductRevisionLevel[i] = ((PUCHAR)pIdentify->FirmwareRevision)[i+1]; 2592 inquiryData->ProductRevisionLevel[i+1] = ((PUCHAR)pIdentify->FirmwareRevision)[i]; 2593 } 2594 break; 2595 default: 2596 memcpy(&inquiryData->VendorId, "RR18xx ", 8); 2597 #ifdef SUPPORT_ARRAY 2598 switch(pVDev->VDeviceType){ 2599 case VD_RAID_0: 2600 if ((pVDev->u.array.pMember[0] && mIsArray(pVDev->u.array.pMember[0])) || 2601 (pVDev->u.array.pMember[1] && mIsArray(pVDev->u.array.pMember[1]))) 2602 memcpy(&inquiryData->ProductId, "RAID 1/0 Array ", 16); 2603 else 2604 memcpy(&inquiryData->ProductId, "RAID 0 Array ", 16); 2605 break; 2606 case VD_RAID_1: 2607 if ((pVDev->u.array.pMember[0] && mIsArray(pVDev->u.array.pMember[0])) || 2608 (pVDev->u.array.pMember[1] && mIsArray(pVDev->u.array.pMember[1]))) 2609 memcpy(&inquiryData->ProductId, "RAID 0/1 Array ", 16); 2610 else 2611 memcpy(&inquiryData->ProductId, "RAID 1 Array ", 16); 2612 break; 2613 case VD_RAID_5: 2614 memcpy(&inquiryData->ProductId, "RAID 5 Array ", 16); 2615 break; 2616 case VD_JBOD: 2617 memcpy(&inquiryData->ProductId, "JBOD Array ", 16); 2618 break; 2619 } 2620 #endif 2621 memcpy(&inquiryData->ProductRevisionLevel, "3.00", 4); 2622 break; 2623 } 2624 } 2625 2626 static void 2627 hpt_timeout(void *arg) 2628 { 2629 _VBUS_INST(&((PBUS_DMAMAP)((union ccb *)arg)->ccb_adapter)->pAdapter->VBus) 2630 lock_driver(); 2631 fResetVBus(_VBUS_P0); 2632 unlock_driver(); 2633 } 2634 2635 static void 2636 hpt_io_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 2637 { 2638 PCommand pCmd = (PCommand)arg; 2639 union ccb *ccb = pCmd->pOrgCommand; 2640 struct ccb_hdr *ccb_h = &ccb->ccb_h; 2641 PBUS_DMAMAP pmap = (PBUS_DMAMAP) ccb->ccb_adapter; 2642 IAL_ADAPTER_T *pAdapter = pmap->pAdapter; 2643 PVDevice pVDev = pAdapter->VBus.pVDevice[ccb_h->target_id]; 2644 FPSCAT_GATH psg = pCmd->pSgTable; 2645 int idx; 2646 _VBUS_INST(pVDev->pVBus) 2647 2648 HPT_ASSERT(pCmd->cf_physical_sg); 2649 2650 if (error || nsegs == 0) 2651 panic("busdma error"); 2652 2653 HPT_ASSERT(nsegs<= MAX_SG_DESCRIPTORS); 2654 2655 for (idx = 0; idx < nsegs; idx++, psg++) { 2656 psg->dSgAddress = (ULONG_PTR)(UCHAR *)segs[idx].ds_addr; 2657 psg->wSgSize = segs[idx].ds_len; 2658 psg->wSgFlag = (idx == nsegs-1)? SG_FLAG_EOT: 0; 2659 /* KdPrint(("psg[%d]:add=%p,size=%x,flag=%x\n", idx, psg->dSgAddress,psg->wSgSize,psg->wSgFlag)); */ 2660 } 2661 /* psg[-1].wSgFlag = SG_FLAG_EOT; */ 2662 2663 if (pCmd->cf_data_in) { 2664 bus_dmamap_sync(pAdapter->io_dma_parent, pmap->dma_map, BUS_DMASYNC_PREREAD); 2665 } 2666 else if (pCmd->cf_data_out) { 2667 bus_dmamap_sync(pAdapter->io_dma_parent, pmap->dma_map, BUS_DMASYNC_PREWRITE); 2668 } 2669 2670 callout_reset(ccb->ccb_h.timeout_ch, 20*hz, hpt_timeout, ccb); 2671 pVDev->pfnSendCommand(_VBUS_P pCmd); 2672 CheckPendingCall(_VBUS_P0); 2673 } 2674 2675 2676 2677 static void HPTLIBAPI 2678 OsSendCommand(_VBUS_ARG union ccb *ccb) 2679 { 2680 PBUS_DMAMAP pmap = (PBUS_DMAMAP)ccb->ccb_adapter; 2681 IAL_ADAPTER_T *pAdapter = pmap->pAdapter; 2682 struct ccb_hdr *ccb_h = &ccb->ccb_h; 2683 struct ccb_scsiio *csio = &ccb->csio; 2684 PVDevice pVDev = pAdapter->VBus.pVDevice[ccb_h->target_id]; 2685 2686 KdPrintI(("OsSendCommand: ccb %p cdb %x-%x-%x\n", 2687 ccb, 2688 *(ULONG *)&ccb->csio.cdb_io.cdb_bytes[0], 2689 *(ULONG *)&ccb->csio.cdb_io.cdb_bytes[4], 2690 *(ULONG *)&ccb->csio.cdb_io.cdb_bytes[8] 2691 )); 2692 2693 pAdapter->outstandingCommands++; 2694 2695 if (pVDev == NULL || pVDev->vf_online == 0) { 2696 ccb->ccb_h.status = CAM_REQ_INVALID; 2697 ccb_done(ccb); 2698 goto Command_Complished; 2699 } 2700 2701 switch(ccb->csio.cdb_io.cdb_bytes[0]) 2702 { 2703 case TEST_UNIT_READY: 2704 case START_STOP_UNIT: 2705 case SYNCHRONIZE_CACHE: 2706 /* FALLTHROUGH */ 2707 ccb->ccb_h.status = CAM_REQ_CMP; 2708 break; 2709 2710 case INQUIRY: 2711 ZeroMemory(ccb->csio.data_ptr, ccb->csio.dxfer_len); 2712 SetInquiryData((PINQUIRYDATA)ccb->csio.data_ptr, pVDev); 2713 ccb_h->status = CAM_REQ_CMP; 2714 break; 2715 2716 case READ_CAPACITY: 2717 { 2718 UCHAR *rbuf=csio->data_ptr; 2719 unsigned int cap; 2720 2721 if (pVDev->VDeviceCapacity > 0xfffffffful) { 2722 cap = 0xfffffffful; 2723 } else { 2724 cap = pVDev->VDeviceCapacity - 1; 2725 } 2726 2727 rbuf[0] = (UCHAR)(cap>>24); 2728 rbuf[1] = (UCHAR)(cap>>16); 2729 rbuf[2] = (UCHAR)(cap>>8); 2730 rbuf[3] = (UCHAR)cap; 2731 /* Claim 512 byte blocks (big-endian). */ 2732 rbuf[4] = 0; 2733 rbuf[5] = 0; 2734 rbuf[6] = 2; 2735 rbuf[7] = 0; 2736 2737 ccb_h->status = CAM_REQ_CMP; 2738 break; 2739 } 2740 2741 case 0x9e: /*SERVICE_ACTION_IN*/ 2742 { 2743 UCHAR *rbuf = csio->data_ptr; 2744 LBA_T cap = pVDev->VDeviceCapacity - 1; 2745 2746 rbuf[0] = (UCHAR)(cap>>56); 2747 rbuf[1] = (UCHAR)(cap>>48); 2748 rbuf[2] = (UCHAR)(cap>>40); 2749 rbuf[3] = (UCHAR)(cap>>32); 2750 rbuf[4] = (UCHAR)(cap>>24); 2751 rbuf[5] = (UCHAR)(cap>>16); 2752 rbuf[6] = (UCHAR)(cap>>8); 2753 rbuf[7] = (UCHAR)cap; 2754 rbuf[8] = 0; 2755 rbuf[9] = 0; 2756 rbuf[10] = 2; 2757 rbuf[11] = 0; 2758 2759 ccb_h->status = CAM_REQ_CMP; 2760 break; 2761 } 2762 2763 case READ_6: 2764 case WRITE_6: 2765 case READ_10: 2766 case WRITE_10: 2767 case 0x88: /* READ_16 */ 2768 case 0x8a: /* WRITE_16 */ 2769 case 0x13: 2770 case 0x2f: 2771 { 2772 UCHAR Cdb[16]; 2773 UCHAR CdbLength; 2774 _VBUS_INST(pVDev->pVBus) 2775 PCommand pCmd = AllocateCommand(_VBUS_P0); 2776 HPT_ASSERT(pCmd); 2777 2778 CdbLength = csio->cdb_len; 2779 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) 2780 { 2781 if ((ccb->ccb_h.flags & CAM_CDB_PHYS) == 0) 2782 { 2783 bcopy(csio->cdb_io.cdb_ptr, Cdb, CdbLength); 2784 } 2785 else 2786 { 2787 KdPrintE(("ERROR!!!\n")); 2788 ccb->ccb_h.status = CAM_REQ_INVALID; 2789 break; 2790 } 2791 } 2792 else 2793 { 2794 bcopy(csio->cdb_io.cdb_bytes, Cdb, CdbLength); 2795 } 2796 2797 pCmd->pOrgCommand = ccb; 2798 pCmd->pVDevice = pVDev; 2799 pCmd->pfnCompletion = fOsCommandDone; 2800 pCmd->pfnBuildSgl = fOsBuildSgl; 2801 pCmd->pSgTable = pmap->psg; 2802 2803 switch (Cdb[0]) 2804 { 2805 case READ_6: 2806 case WRITE_6: 2807 case 0x13: 2808 pCmd->uCmd.Ide.Lba = ((ULONG)Cdb[1] << 16) | ((ULONG)Cdb[2] << 8) | (ULONG)Cdb[3]; 2809 pCmd->uCmd.Ide.nSectors = (USHORT) Cdb[4]; 2810 break; 2811 2812 case 0x88: /* READ_16 */ 2813 case 0x8a: /* WRITE_16 */ 2814 pCmd->uCmd.Ide.Lba = 2815 (HPT_U64)Cdb[2] << 56 | 2816 (HPT_U64)Cdb[3] << 48 | 2817 (HPT_U64)Cdb[4] << 40 | 2818 (HPT_U64)Cdb[5] << 32 | 2819 (HPT_U64)Cdb[6] << 24 | 2820 (HPT_U64)Cdb[7] << 16 | 2821 (HPT_U64)Cdb[8] << 8 | 2822 (HPT_U64)Cdb[9]; 2823 pCmd->uCmd.Ide.nSectors = (USHORT)Cdb[12] << 8 | (USHORT)Cdb[13]; 2824 break; 2825 2826 default: 2827 pCmd->uCmd.Ide.Lba = (ULONG)Cdb[5] | ((ULONG)Cdb[4] << 8) | ((ULONG)Cdb[3] << 16) | ((ULONG)Cdb[2] << 24); 2828 pCmd->uCmd.Ide.nSectors = (USHORT) Cdb[8] | ((USHORT)Cdb[7]<<8); 2829 break; 2830 } 2831 2832 switch (Cdb[0]) 2833 { 2834 case READ_6: 2835 case READ_10: 2836 case 0x88: /* READ_16 */ 2837 pCmd->uCmd.Ide.Command = IDE_COMMAND_READ; 2838 pCmd->cf_data_in = 1; 2839 break; 2840 2841 case WRITE_6: 2842 case WRITE_10: 2843 case 0x8a: /* WRITE_16 */ 2844 pCmd->uCmd.Ide.Command = IDE_COMMAND_WRITE; 2845 pCmd->cf_data_out = 1; 2846 break; 2847 case 0x13: 2848 case 0x2f: 2849 pCmd->uCmd.Ide.Command = IDE_COMMAND_VERIFY; 2850 break; 2851 } 2852 /*///////////////////////// */ 2853 if (ccb->ccb_h.flags & CAM_SCATTER_VALID) { 2854 int idx; 2855 bus_dma_segment_t *sgList = (bus_dma_segment_t *)ccb->csio.data_ptr; 2856 2857 if (ccb->ccb_h.flags & CAM_SG_LIST_PHYS) 2858 pCmd->cf_physical_sg = 1; 2859 2860 for (idx = 0; idx < ccb->csio.sglist_cnt; idx++) { 2861 pCmd->pSgTable[idx].dSgAddress = (ULONG_PTR)(UCHAR *)sgList[idx].ds_addr; 2862 pCmd->pSgTable[idx].wSgSize = sgList[idx].ds_len; 2863 pCmd->pSgTable[idx].wSgFlag= (idx==ccb->csio.sglist_cnt-1)?SG_FLAG_EOT: 0; 2864 } 2865 2866 callout_reset(ccb->ccb_h.timeout_ch, 20 * hz, 2867 hpt_timeout, ccb); 2868 pVDev->pfnSendCommand(_VBUS_P pCmd); 2869 } 2870 else { 2871 int error; 2872 pCmd->cf_physical_sg = 1; 2873 error = bus_dmamap_load(pAdapter->io_dma_parent, 2874 pmap->dma_map, 2875 ccb->csio.data_ptr, ccb->csio.dxfer_len, 2876 hpt_io_dmamap_callback, pCmd, 2877 BUS_DMA_WAITOK 2878 ); 2879 KdPrint(("bus_dmamap_load return %d\n", error)); 2880 if (error && error!=EINPROGRESS) { 2881 hpt_printk(("bus_dmamap_load error %d\n", error)); 2882 FreeCommand(_VBUS_P pCmd); 2883 ccb->ccb_h.status = CAM_REQ_CMP_ERR; 2884 dmamap_put(pmap); 2885 pAdapter->outstandingCommands--; 2886 xpt_done(ccb); 2887 } 2888 } 2889 goto Command_Complished; 2890 } 2891 2892 default: 2893 ccb->ccb_h.status = CAM_REQ_INVALID; 2894 break; 2895 } 2896 ccb_done(ccb); 2897 Command_Complished: 2898 CheckPendingCall(_VBUS_P0); 2899 return; 2900 } 2901 2902 static void HPTLIBAPI 2903 fOsCommandDone(_VBUS_ARG PCommand pCmd) 2904 { 2905 union ccb *ccb = pCmd->pOrgCommand; 2906 PBUS_DMAMAP pmap = (PBUS_DMAMAP)ccb->ccb_adapter; 2907 IAL_ADAPTER_T *pAdapter = pmap->pAdapter; 2908 2909 KdPrint(("fOsCommandDone(pcmd=%p, result=%d)\n", pCmd, pCmd->Result)); 2910 2911 callout_stop(ccb->ccb_h.timeout_ch); 2912 2913 switch(pCmd->Result) { 2914 case RETURN_SUCCESS: 2915 ccb->ccb_h.status = CAM_REQ_CMP; 2916 break; 2917 case RETURN_BAD_DEVICE: 2918 ccb->ccb_h.status = CAM_DEV_NOT_THERE; 2919 break; 2920 case RETURN_DEVICE_BUSY: 2921 ccb->ccb_h.status = CAM_BUSY; 2922 break; 2923 case RETURN_INVALID_REQUEST: 2924 ccb->ccb_h.status = CAM_REQ_INVALID; 2925 break; 2926 case RETURN_SELECTION_TIMEOUT: 2927 ccb->ccb_h.status = CAM_SEL_TIMEOUT; 2928 break; 2929 case RETURN_RETRY: 2930 ccb->ccb_h.status = CAM_BUSY; 2931 break; 2932 default: 2933 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR; 2934 break; 2935 } 2936 2937 if (pCmd->cf_data_in) { 2938 bus_dmamap_sync(pAdapter->io_dma_parent, pmap->dma_map, BUS_DMASYNC_POSTREAD); 2939 } 2940 else if (pCmd->cf_data_out) { 2941 bus_dmamap_sync(pAdapter->io_dma_parent, pmap->dma_map, BUS_DMASYNC_POSTWRITE); 2942 } 2943 2944 bus_dmamap_unload(pAdapter->io_dma_parent, pmap->dma_map); 2945 2946 FreeCommand(_VBUS_P pCmd); 2947 ccb_done(ccb); 2948 } 2949 2950 int 2951 hpt_queue_dpc(HPT_DPC dpc, IAL_ADAPTER_T * pAdapter, void *arg, UCHAR flags) 2952 { 2953 int p; 2954 2955 p = (DpcQueue_Last + 1) % MAX_DPC; 2956 if (p==DpcQueue_First) { 2957 KdPrint(("DPC Queue full!\n")); 2958 return -1; 2959 } 2960 2961 DpcQueue[DpcQueue_Last].dpc = dpc; 2962 DpcQueue[DpcQueue_Last].pAdapter = pAdapter; 2963 DpcQueue[DpcQueue_Last].arg = arg; 2964 DpcQueue[DpcQueue_Last].flags = flags; 2965 DpcQueue_Last = p; 2966 2967 return 0; 2968 } 2969 2970 #ifdef _RAID5N_ 2971 /* 2972 * Allocate memory above 16M, otherwise we may eat all low memory for ISA devices. 2973 * How about the memory for 5081 request/response array and PRD table? 2974 */ 2975 void 2976 *os_alloc_page(_VBUS_ARG0) 2977 { 2978 return (void *)contigmalloc(0x1000, M_DEVBUF, M_NOWAIT, 0x1000000, 0xffffffff, PAGE_SIZE, 0ul); 2979 } 2980 2981 void 2982 *os_alloc_dma_page(_VBUS_ARG0) 2983 { 2984 return (void *)contigmalloc(0x1000, M_DEVBUF, M_NOWAIT, 0x1000000, 0xffffffff, PAGE_SIZE, 0ul); 2985 } 2986 2987 void 2988 os_free_page(_VBUS_ARG void *p) 2989 { 2990 contigfree(p, 0x1000, M_DEVBUF); 2991 } 2992 2993 void 2994 os_free_dma_page(_VBUS_ARG void *p) 2995 { 2996 contigfree(p, 0x1000, M_DEVBUF); 2997 } 2998 2999 void 3000 DoXor1(ULONG *p0, ULONG *p1, ULONG *p2, UINT nBytes) 3001 { 3002 UINT i; 3003 for (i = 0; i < nBytes / 4; i++) *p0++ = *p1++ ^ *p2++; 3004 } 3005 3006 void 3007 DoXor2(ULONG *p0, ULONG *p2, UINT nBytes) 3008 { 3009 UINT i; 3010 for (i = 0; i < nBytes / 4; i++) *p0++ ^= *p2++; 3011 } 3012 #endif 3013