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