1 /* 2 * HighPoint RR3xxx/4xxx RAID Driver for FreeBSD 3 * Copyright (C) 2007-2008 HighPoint Technologies, Inc. All Rights Reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: src/sys/dev/hptiop/hptiop.c,v 1.10 2011/10/13 20:06:19 marius Exp $ 27 */ 28 29 #include <sys/param.h> 30 #include <sys/types.h> 31 #include <sys/cons.h> 32 #include <sys/time.h> 33 #include <sys/systm.h> 34 35 #include <sys/stat.h> 36 #include <sys/malloc.h> 37 #include <sys/conf.h> 38 #include <sys/libkern.h> 39 #include <sys/kernel.h> 40 41 #include <sys/kthread.h> 42 #include <sys/lock.h> 43 #include <sys/module.h> 44 45 #include <sys/eventhandler.h> 46 #include <sys/bus.h> 47 #include <sys/taskqueue.h> 48 #include <sys/ioccom.h> 49 #include <sys/device.h> 50 #include <sys/mplock2.h> 51 52 #include <machine/stdarg.h> 53 #include <sys/rman.h> 54 55 #include <vm/vm.h> 56 #include <vm/pmap.h> 57 58 #include <bus/pci/pcireg.h> 59 #include <bus/pci/pcivar.h> 60 61 #include <bus/cam/cam.h> 62 #include <bus/cam/cam_ccb.h> 63 #include <bus/cam/cam_sim.h> 64 #include <bus/cam/cam_xpt_periph.h> 65 #include <bus/cam/cam_xpt_sim.h> 66 #include <bus/cam/cam_debug.h> 67 #include <bus/cam/cam_periph.h> 68 #include <bus/cam/scsi/scsi_all.h> 69 #include <bus/cam/scsi/scsi_message.h> 70 71 #include <dev/raid/hptiop/hptiop.h> 72 73 static char driver_name[] = "hptiop"; 74 static char driver_version[] = "v1.3 (010208)"; 75 76 static devclass_t hptiop_devclass; 77 78 static int hptiop_send_sync_msg(struct hpt_iop_hba *hba, 79 u_int32_t msg, u_int32_t millisec); 80 static void hptiop_request_callback_itl(struct hpt_iop_hba *hba, 81 u_int32_t req); 82 static void hptiop_request_callback_mv(struct hpt_iop_hba *hba, u_int64_t req); 83 static void hptiop_os_message_callback(struct hpt_iop_hba *hba, u_int32_t msg); 84 static int hptiop_do_ioctl_itl(struct hpt_iop_hba *hba, 85 struct hpt_iop_ioctl_param *pParams); 86 static int hptiop_do_ioctl_mv(struct hpt_iop_hba *hba, 87 struct hpt_iop_ioctl_param *pParams); 88 static void hptiop_bus_scan_cb(struct cam_periph *periph, union ccb *ccb); 89 static int hptiop_rescan_bus(struct hpt_iop_hba *hba); 90 static int hptiop_alloc_pci_res_itl(struct hpt_iop_hba *hba); 91 static int hptiop_alloc_pci_res_mv(struct hpt_iop_hba *hba); 92 static int hptiop_get_config_itl(struct hpt_iop_hba *hba, 93 struct hpt_iop_request_get_config *config); 94 static int hptiop_get_config_mv(struct hpt_iop_hba *hba, 95 struct hpt_iop_request_get_config *config); 96 static int hptiop_set_config_itl(struct hpt_iop_hba *hba, 97 struct hpt_iop_request_set_config *config); 98 static int hptiop_set_config_mv(struct hpt_iop_hba *hba, 99 struct hpt_iop_request_set_config *config); 100 static int hptiop_internal_memalloc_mv(struct hpt_iop_hba *hba); 101 static int hptiop_internal_memfree_mv(struct hpt_iop_hba *hba); 102 static int hptiop_post_ioctl_command_itl(struct hpt_iop_hba *hba, 103 u_int32_t req32, struct hpt_iop_ioctl_param *pParams); 104 static int hptiop_post_ioctl_command_mv(struct hpt_iop_hba *hba, 105 struct hpt_iop_request_ioctl_command *req, 106 struct hpt_iop_ioctl_param *pParams); 107 static void hptiop_post_req_itl(struct hpt_iop_hba *hba, 108 struct hpt_iop_srb *srb, 109 bus_dma_segment_t *segs, int nsegs); 110 static void hptiop_post_req_mv(struct hpt_iop_hba *hba, 111 struct hpt_iop_srb *srb, 112 bus_dma_segment_t *segs, int nsegs); 113 static void hptiop_post_msg_itl(struct hpt_iop_hba *hba, u_int32_t msg); 114 static void hptiop_post_msg_mv(struct hpt_iop_hba *hba, u_int32_t msg); 115 static void hptiop_enable_intr_itl(struct hpt_iop_hba *hba); 116 static void hptiop_enable_intr_mv(struct hpt_iop_hba *hba); 117 static void hptiop_disable_intr_itl(struct hpt_iop_hba *hba); 118 static void hptiop_disable_intr_mv(struct hpt_iop_hba *hba); 119 static void hptiop_free_srb(struct hpt_iop_hba *hba, struct hpt_iop_srb *srb); 120 static int hptiop_os_query_remove_device(struct hpt_iop_hba *hba, int tid); 121 static int hptiop_probe(device_t dev); 122 static int hptiop_attach(device_t dev); 123 static int hptiop_detach(device_t dev); 124 static int hptiop_shutdown(device_t dev); 125 static void hptiop_action(struct cam_sim *sim, union ccb *ccb); 126 static void hptiop_poll(struct cam_sim *sim); 127 static void hptiop_async(void *callback_arg, u_int32_t code, 128 struct cam_path *path, void *arg); 129 static void hptiop_pci_intr(void *arg); 130 static void hptiop_release_resource(struct hpt_iop_hba *hba); 131 static int hptiop_reset_adapter(struct hpt_iop_hba *hba); 132 133 static d_open_t hptiop_open; 134 static d_close_t hptiop_close; 135 static d_ioctl_t hptiop_ioctl; 136 137 static struct dev_ops hptiop_ops = { 138 { driver_name, 0, 0 }, 139 .d_open = hptiop_open, 140 .d_close = hptiop_close, 141 .d_ioctl = hptiop_ioctl, 142 }; 143 144 #define hba_from_dev(dev) ((struct hpt_iop_hba *)(dev)->si_drv1) 145 146 #define BUS_SPACE_WRT4_ITL(offset, value) bus_space_write_4(hba->bar0t,\ 147 hba->bar0h, offsetof(struct hpt_iopmu_itl, offset), (value)) 148 #define BUS_SPACE_RD4_ITL(offset) bus_space_read_4(hba->bar0t,\ 149 hba->bar0h, offsetof(struct hpt_iopmu_itl, offset)) 150 151 #define BUS_SPACE_WRT4_MV0(offset, value) bus_space_write_4(hba->bar0t,\ 152 hba->bar0h, offsetof(struct hpt_iopmv_regs, offset), value) 153 #define BUS_SPACE_RD4_MV0(offset) bus_space_read_4(hba->bar0t,\ 154 hba->bar0h, offsetof(struct hpt_iopmv_regs, offset)) 155 #define BUS_SPACE_WRT4_MV2(offset, value) bus_space_write_4(hba->bar2t,\ 156 hba->bar2h, offsetof(struct hpt_iopmu_mv, offset), value) 157 #define BUS_SPACE_RD4_MV2(offset) bus_space_read_4(hba->bar2t,\ 158 hba->bar2h, offsetof(struct hpt_iopmu_mv, offset)) 159 160 static int hptiop_open(struct dev_open_args *ap) 161 { 162 cdev_t dev = ap->a_head.a_dev; 163 struct hpt_iop_hba *hba = hba_from_dev(dev); 164 165 if (hba==NULL) 166 return ENXIO; 167 if (hba->flag & HPT_IOCTL_FLAG_OPEN) 168 return EBUSY; 169 hba->flag |= HPT_IOCTL_FLAG_OPEN; 170 return 0; 171 } 172 173 static int hptiop_close(struct dev_close_args *ap) 174 { 175 cdev_t dev = ap->a_head.a_dev; 176 struct hpt_iop_hba *hba = hba_from_dev(dev); 177 hba->flag &= ~(u_int32_t)HPT_IOCTL_FLAG_OPEN; 178 return 0; 179 } 180 181 static int hptiop_ioctl(struct dev_ioctl_args *ap) 182 { 183 cdev_t dev = ap->a_head.a_dev; 184 u_long cmd = ap->a_cmd; 185 caddr_t data = ap->a_data; 186 int ret = EFAULT; 187 struct hpt_iop_hba *hba = hba_from_dev(dev); 188 189 get_mplock(); 190 191 switch (cmd) { 192 case HPT_DO_IOCONTROL: 193 ret = hba->ops->do_ioctl(hba, 194 (struct hpt_iop_ioctl_param *)data); 195 break; 196 case HPT_SCAN_BUS: 197 ret = hptiop_rescan_bus(hba); 198 break; 199 } 200 201 rel_mplock(); 202 203 return ret; 204 } 205 206 static u_int64_t hptiop_mv_outbound_read(struct hpt_iop_hba *hba) 207 { 208 u_int64_t p; 209 u_int32_t outbound_tail = BUS_SPACE_RD4_MV2(outbound_tail); 210 u_int32_t outbound_head = BUS_SPACE_RD4_MV2(outbound_head); 211 212 if (outbound_tail != outbound_head) { 213 bus_space_read_region_4(hba->bar2t, hba->bar2h, 214 offsetof(struct hpt_iopmu_mv, 215 outbound_q[outbound_tail]), 216 (u_int32_t *)&p, 2); 217 218 outbound_tail++; 219 220 if (outbound_tail == MVIOP_QUEUE_LEN) 221 outbound_tail = 0; 222 223 BUS_SPACE_WRT4_MV2(outbound_tail, outbound_tail); 224 return p; 225 } else 226 return 0; 227 } 228 229 static void hptiop_mv_inbound_write(u_int64_t p, struct hpt_iop_hba *hba) 230 { 231 u_int32_t inbound_head = BUS_SPACE_RD4_MV2(inbound_head); 232 u_int32_t head = inbound_head + 1; 233 234 if (head == MVIOP_QUEUE_LEN) 235 head = 0; 236 237 bus_space_write_region_4(hba->bar2t, hba->bar2h, 238 offsetof(struct hpt_iopmu_mv, inbound_q[inbound_head]), 239 (u_int32_t *)&p, 2); 240 BUS_SPACE_WRT4_MV2(inbound_head, head); 241 BUS_SPACE_WRT4_MV0(inbound_doorbell, MVIOP_MU_INBOUND_INT_POSTQUEUE); 242 } 243 244 static void hptiop_post_msg_itl(struct hpt_iop_hba *hba, u_int32_t msg) 245 { 246 BUS_SPACE_WRT4_ITL(inbound_msgaddr0, msg); 247 BUS_SPACE_RD4_ITL(outbound_intstatus); 248 } 249 250 static void hptiop_post_msg_mv(struct hpt_iop_hba *hba, u_int32_t msg) 251 { 252 253 BUS_SPACE_WRT4_MV2(inbound_msg, msg); 254 BUS_SPACE_WRT4_MV0(inbound_doorbell, MVIOP_MU_INBOUND_INT_MSG); 255 256 BUS_SPACE_RD4_MV0(outbound_intmask); 257 } 258 259 static int hptiop_wait_ready_itl(struct hpt_iop_hba * hba, u_int32_t millisec) 260 { 261 u_int32_t req=0; 262 int i; 263 264 for (i = 0; i < millisec; i++) { 265 req = BUS_SPACE_RD4_ITL(inbound_queue); 266 if (req != IOPMU_QUEUE_EMPTY) 267 break; 268 DELAY(1000); 269 } 270 271 if (req!=IOPMU_QUEUE_EMPTY) { 272 BUS_SPACE_WRT4_ITL(outbound_queue, req); 273 BUS_SPACE_RD4_ITL(outbound_intstatus); 274 return 0; 275 } 276 277 return -1; 278 } 279 280 static int hptiop_wait_ready_mv(struct hpt_iop_hba * hba, u_int32_t millisec) 281 { 282 if (hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_NOP, millisec)) 283 return -1; 284 285 return 0; 286 } 287 288 static void hptiop_request_callback_itl(struct hpt_iop_hba * hba, 289 u_int32_t index) 290 { 291 struct hpt_iop_srb *srb; 292 struct hpt_iop_request_scsi_command *req=NULL; 293 union ccb *ccb; 294 u_int8_t *cdb; 295 u_int32_t result, temp, dxfer; 296 u_int64_t temp64; 297 298 if (index & IOPMU_QUEUE_MASK_HOST_BITS) { /*host req*/ 299 if (hba->firmware_version > 0x01020000 || 300 hba->interface_version > 0x01020000) { 301 srb = hba->srb[index & ~(u_int32_t) 302 (IOPMU_QUEUE_ADDR_HOST_BIT 303 | IOPMU_QUEUE_REQUEST_RESULT_BIT)]; 304 req = (struct hpt_iop_request_scsi_command *)srb; 305 if (index & IOPMU_QUEUE_REQUEST_RESULT_BIT) 306 result = IOP_RESULT_SUCCESS; 307 else 308 result = req->header.result; 309 } else { 310 srb = hba->srb[index & 311 ~(u_int32_t)IOPMU_QUEUE_ADDR_HOST_BIT]; 312 req = (struct hpt_iop_request_scsi_command *)srb; 313 result = req->header.result; 314 } 315 dxfer = req->dataxfer_length; 316 goto srb_complete; 317 } 318 319 /*iop req*/ 320 temp = bus_space_read_4(hba->bar0t, hba->bar0h, index + 321 offsetof(struct hpt_iop_request_header, type)); 322 result = bus_space_read_4(hba->bar0t, hba->bar0h, index + 323 offsetof(struct hpt_iop_request_header, result)); 324 switch(temp) { 325 case IOP_REQUEST_TYPE_IOCTL_COMMAND: 326 { 327 temp64 = 0; 328 bus_space_write_region_4(hba->bar0t, hba->bar0h, index + 329 offsetof(struct hpt_iop_request_header, context), 330 (u_int32_t *)&temp64, 2); 331 wakeup((void *)((unsigned long)hba->u.itl.mu + index)); 332 break; 333 } 334 335 case IOP_REQUEST_TYPE_SCSI_COMMAND: 336 bus_space_read_region_4(hba->bar0t, hba->bar0h, index + 337 offsetof(struct hpt_iop_request_header, context), 338 (u_int32_t *)&temp64, 2); 339 srb = (struct hpt_iop_srb *)(unsigned long)temp64; 340 dxfer = bus_space_read_4(hba->bar0t, hba->bar0h, 341 index + offsetof(struct hpt_iop_request_scsi_command, 342 dataxfer_length)); 343 srb_complete: 344 ccb = (union ccb *)srb->ccb; 345 if (ccb->ccb_h.flags & CAM_CDB_POINTER) 346 cdb = ccb->csio.cdb_io.cdb_ptr; 347 else 348 cdb = ccb->csio.cdb_io.cdb_bytes; 349 350 if (cdb[0] == SYNCHRONIZE_CACHE) { /* ??? */ 351 ccb->ccb_h.status = CAM_REQ_CMP; 352 goto scsi_done; 353 } 354 355 switch (result) { 356 case IOP_RESULT_SUCCESS: 357 switch (ccb->ccb_h.flags & CAM_DIR_MASK) { 358 case CAM_DIR_IN: 359 bus_dmamap_sync(hba->io_dmat, 360 srb->dma_map, BUS_DMASYNC_POSTREAD); 361 bus_dmamap_unload(hba->io_dmat, srb->dma_map); 362 break; 363 case CAM_DIR_OUT: 364 bus_dmamap_sync(hba->io_dmat, 365 srb->dma_map, BUS_DMASYNC_POSTWRITE); 366 bus_dmamap_unload(hba->io_dmat, srb->dma_map); 367 break; 368 } 369 370 ccb->ccb_h.status = CAM_REQ_CMP; 371 break; 372 373 case IOP_RESULT_BAD_TARGET: 374 ccb->ccb_h.status = CAM_DEV_NOT_THERE; 375 break; 376 case IOP_RESULT_BUSY: 377 ccb->ccb_h.status = CAM_BUSY; 378 break; 379 case IOP_RESULT_INVALID_REQUEST: 380 ccb->ccb_h.status = CAM_REQ_INVALID; 381 break; 382 case IOP_RESULT_FAIL: 383 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR; 384 break; 385 case IOP_RESULT_RESET: 386 ccb->ccb_h.status = CAM_BUSY; 387 break; 388 case IOP_RESULT_CHECK_CONDITION: 389 memset(&ccb->csio.sense_data, 0, 390 sizeof(ccb->csio.sense_data)); 391 if (dxfer < ccb->csio.sense_len) 392 ccb->csio.sense_resid = ccb->csio.sense_len - 393 dxfer; 394 else 395 ccb->csio.sense_resid = 0; 396 if (srb->srb_flag & HPT_SRB_FLAG_HIGH_MEM_ACESS) {/*iop*/ 397 bus_space_read_region_1(hba->bar0t, hba->bar0h, 398 index + offsetof(struct hpt_iop_request_scsi_command, 399 sg_list), (u_int8_t *)&ccb->csio.sense_data, 400 MIN(dxfer, sizeof(ccb->csio.sense_data))); 401 } else { 402 memcpy(&ccb->csio.sense_data, &req->sg_list, 403 MIN(dxfer, sizeof(ccb->csio.sense_data))); 404 } 405 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR; 406 ccb->ccb_h.status |= CAM_AUTOSNS_VALID; 407 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; 408 break; 409 default: 410 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR; 411 break; 412 } 413 scsi_done: 414 if (srb->srb_flag & HPT_SRB_FLAG_HIGH_MEM_ACESS) 415 BUS_SPACE_WRT4_ITL(outbound_queue, index); 416 417 ccb->csio.resid = ccb->csio.dxfer_len - dxfer; 418 419 hptiop_free_srb(hba, srb); 420 xpt_done(ccb); 421 break; 422 } 423 } 424 425 static void hptiop_drain_outbound_queue_itl(struct hpt_iop_hba *hba) 426 { 427 u_int32_t req, temp; 428 429 while ((req = BUS_SPACE_RD4_ITL(outbound_queue)) !=IOPMU_QUEUE_EMPTY) { 430 if (req & IOPMU_QUEUE_MASK_HOST_BITS) 431 hptiop_request_callback_itl(hba, req); 432 else { 433 struct hpt_iop_request_header *p; 434 435 p = (struct hpt_iop_request_header *) 436 ((char *)hba->u.itl.mu + req); 437 temp = bus_space_read_4(hba->bar0t, 438 hba->bar0h,req + 439 offsetof(struct hpt_iop_request_header, 440 flags)); 441 if (temp & IOP_REQUEST_FLAG_SYNC_REQUEST) { 442 u_int64_t temp64; 443 bus_space_read_region_4(hba->bar0t, 444 hba->bar0h,req + 445 offsetof(struct hpt_iop_request_header, 446 context), 447 (u_int32_t *)&temp64, 2); 448 if (temp64) { 449 hptiop_request_callback_itl(hba, req); 450 } else { 451 temp64 = 1; 452 bus_space_write_region_4(hba->bar0t, 453 hba->bar0h,req + 454 offsetof(struct hpt_iop_request_header, 455 context), 456 (u_int32_t *)&temp64, 2); 457 } 458 } else 459 hptiop_request_callback_itl(hba, req); 460 } 461 } 462 } 463 464 static int hptiop_intr_itl(struct hpt_iop_hba * hba) 465 { 466 u_int32_t status; 467 int ret = 0; 468 469 status = BUS_SPACE_RD4_ITL(outbound_intstatus); 470 471 if (status & IOPMU_OUTBOUND_INT_MSG0) { 472 u_int32_t msg = BUS_SPACE_RD4_ITL(outbound_msgaddr0); 473 KdPrint(("hptiop: received outbound msg %x\n", msg)); 474 BUS_SPACE_WRT4_ITL(outbound_intstatus, IOPMU_OUTBOUND_INT_MSG0); 475 hptiop_os_message_callback(hba, msg); 476 ret = 1; 477 } 478 479 if (status & IOPMU_OUTBOUND_INT_POSTQUEUE) { 480 hptiop_drain_outbound_queue_itl(hba); 481 ret = 1; 482 } 483 484 return ret; 485 } 486 487 static void hptiop_request_callback_mv(struct hpt_iop_hba * hba, 488 u_int64_t _tag) 489 { 490 u_int32_t context = (u_int32_t)_tag; 491 492 if (context & MVIOP_CMD_TYPE_SCSI) { 493 struct hpt_iop_srb *srb; 494 struct hpt_iop_request_scsi_command *req; 495 union ccb *ccb; 496 u_int8_t *cdb; 497 498 srb = hba->srb[context >> MVIOP_REQUEST_NUMBER_START_BIT]; 499 req = (struct hpt_iop_request_scsi_command *)srb; 500 ccb = (union ccb *)srb->ccb; 501 if (ccb->ccb_h.flags & CAM_CDB_POINTER) 502 cdb = ccb->csio.cdb_io.cdb_ptr; 503 else 504 cdb = ccb->csio.cdb_io.cdb_bytes; 505 506 if (cdb[0] == SYNCHRONIZE_CACHE) { /* ??? */ 507 ccb->ccb_h.status = CAM_REQ_CMP; 508 goto scsi_done; 509 } 510 if (context & MVIOP_MU_QUEUE_REQUEST_RESULT_BIT) 511 req->header.result = IOP_RESULT_SUCCESS; 512 513 switch (req->header.result) { 514 case IOP_RESULT_SUCCESS: 515 switch (ccb->ccb_h.flags & CAM_DIR_MASK) { 516 case CAM_DIR_IN: 517 bus_dmamap_sync(hba->io_dmat, 518 srb->dma_map, BUS_DMASYNC_POSTREAD); 519 bus_dmamap_unload(hba->io_dmat, srb->dma_map); 520 break; 521 case CAM_DIR_OUT: 522 bus_dmamap_sync(hba->io_dmat, 523 srb->dma_map, BUS_DMASYNC_POSTWRITE); 524 bus_dmamap_unload(hba->io_dmat, srb->dma_map); 525 break; 526 } 527 ccb->ccb_h.status = CAM_REQ_CMP; 528 break; 529 case IOP_RESULT_BAD_TARGET: 530 ccb->ccb_h.status = CAM_DEV_NOT_THERE; 531 break; 532 case IOP_RESULT_BUSY: 533 ccb->ccb_h.status = CAM_BUSY; 534 break; 535 case IOP_RESULT_INVALID_REQUEST: 536 ccb->ccb_h.status = CAM_REQ_INVALID; 537 break; 538 case IOP_RESULT_FAIL: 539 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR; 540 break; 541 case IOP_RESULT_RESET: 542 ccb->ccb_h.status = CAM_BUSY; 543 break; 544 case IOP_RESULT_CHECK_CONDITION: 545 memset(&ccb->csio.sense_data, 0, 546 sizeof(ccb->csio.sense_data)); 547 if (req->dataxfer_length < ccb->csio.sense_len) 548 ccb->csio.sense_resid = ccb->csio.sense_len - 549 req->dataxfer_length; 550 else 551 ccb->csio.sense_resid = 0; 552 memcpy(&ccb->csio.sense_data, &req->sg_list, 553 MIN(req->dataxfer_length, sizeof(ccb->csio.sense_data))); 554 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR; 555 ccb->ccb_h.status |= CAM_AUTOSNS_VALID; 556 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; 557 break; 558 default: 559 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR; 560 break; 561 } 562 scsi_done: 563 ccb->csio.resid = ccb->csio.dxfer_len - req->dataxfer_length; 564 565 hptiop_free_srb(hba, srb); 566 xpt_done(ccb); 567 } else if (context & MVIOP_CMD_TYPE_IOCTL) { 568 struct hpt_iop_request_ioctl_command *req = hba->ctlcfg_ptr; 569 if (context & MVIOP_MU_QUEUE_REQUEST_RESULT_BIT) 570 hba->config_done = 1; 571 else 572 hba->config_done = -1; 573 wakeup(req); 574 } else if (context & 575 (MVIOP_CMD_TYPE_SET_CONFIG | 576 MVIOP_CMD_TYPE_GET_CONFIG)) 577 hba->config_done = 1; 578 else { 579 device_printf(hba->pcidev, "wrong callback type\n"); 580 } 581 } 582 583 static void hptiop_drain_outbound_queue_mv(struct hpt_iop_hba * hba) 584 { 585 u_int64_t req; 586 587 while ((req = hptiop_mv_outbound_read(hba))) { 588 if (req & MVIOP_MU_QUEUE_ADDR_HOST_BIT) { 589 if (req & MVIOP_MU_QUEUE_REQUEST_RETURN_CONTEXT) { 590 hptiop_request_callback_mv(hba, req); 591 } 592 } 593 } 594 } 595 596 static int hptiop_intr_mv(struct hpt_iop_hba * hba) 597 { 598 u_int32_t status; 599 int ret = 0; 600 601 status = BUS_SPACE_RD4_MV0(outbound_doorbell); 602 603 if (status) 604 BUS_SPACE_WRT4_MV0(outbound_doorbell, ~status); 605 606 if (status & MVIOP_MU_OUTBOUND_INT_MSG) { 607 u_int32_t msg = BUS_SPACE_RD4_MV2(outbound_msg); 608 KdPrint(("hptiop: received outbound msg %x\n", msg)); 609 hptiop_os_message_callback(hba, msg); 610 ret = 1; 611 } 612 613 if (status & MVIOP_MU_OUTBOUND_INT_POSTQUEUE) { 614 hptiop_drain_outbound_queue_mv(hba); 615 ret = 1; 616 } 617 618 return ret; 619 } 620 621 static int hptiop_send_sync_request_itl(struct hpt_iop_hba * hba, 622 u_int32_t req32, u_int32_t millisec) 623 { 624 u_int32_t i; 625 u_int64_t temp64; 626 627 BUS_SPACE_WRT4_ITL(inbound_queue, req32); 628 BUS_SPACE_RD4_ITL(outbound_intstatus); 629 630 for (i = 0; i < millisec; i++) { 631 hptiop_intr_itl(hba); 632 bus_space_read_region_4(hba->bar0t, hba->bar0h, req32 + 633 offsetof(struct hpt_iop_request_header, context), 634 (u_int32_t *)&temp64, 2); 635 if (temp64) 636 return 0; 637 DELAY(1000); 638 } 639 640 return -1; 641 } 642 643 static int hptiop_send_sync_request_mv(struct hpt_iop_hba *hba, 644 void *req, u_int32_t millisec) 645 { 646 u_int32_t i; 647 u_int64_t phy_addr; 648 hba->config_done = 0; 649 650 phy_addr = hba->ctlcfgcmd_phy | 651 (u_int64_t)MVIOP_MU_QUEUE_ADDR_HOST_BIT; 652 ((struct hpt_iop_request_get_config *)req)->header.flags |= 653 IOP_REQUEST_FLAG_SYNC_REQUEST | 654 IOP_REQUEST_FLAG_OUTPUT_CONTEXT; 655 hptiop_mv_inbound_write(phy_addr, hba); 656 BUS_SPACE_RD4_MV0(outbound_intmask); 657 658 for (i = 0; i < millisec; i++) { 659 hptiop_intr_mv(hba); 660 if (hba->config_done) 661 return 0; 662 DELAY(1000); 663 } 664 return -1; 665 } 666 667 static int hptiop_send_sync_msg(struct hpt_iop_hba *hba, 668 u_int32_t msg, u_int32_t millisec) 669 { 670 u_int32_t i; 671 672 hba->msg_done = 0; 673 hba->ops->post_msg(hba, msg); 674 675 for (i=0; i<millisec; i++) { 676 hba->ops->iop_intr(hba); 677 if (hba->msg_done) 678 break; 679 DELAY(1000); 680 } 681 682 return hba->msg_done? 0 : -1; 683 } 684 685 static int hptiop_get_config_itl(struct hpt_iop_hba * hba, 686 struct hpt_iop_request_get_config * config) 687 { 688 u_int32_t req32; 689 690 config->header.size = sizeof(struct hpt_iop_request_get_config); 691 config->header.type = IOP_REQUEST_TYPE_GET_CONFIG; 692 config->header.flags = IOP_REQUEST_FLAG_SYNC_REQUEST; 693 config->header.result = IOP_RESULT_PENDING; 694 config->header.context = 0; 695 696 req32 = BUS_SPACE_RD4_ITL(inbound_queue); 697 if (req32 == IOPMU_QUEUE_EMPTY) 698 return -1; 699 700 bus_space_write_region_4(hba->bar0t, hba->bar0h, 701 req32, (u_int32_t *)config, 702 sizeof(struct hpt_iop_request_header) >> 2); 703 704 if (hptiop_send_sync_request_itl(hba, req32, 20000)) { 705 KdPrint(("hptiop: get config send cmd failed")); 706 return -1; 707 } 708 709 bus_space_read_region_4(hba->bar0t, hba->bar0h, 710 req32, (u_int32_t *)config, 711 sizeof(struct hpt_iop_request_get_config) >> 2); 712 713 BUS_SPACE_WRT4_ITL(outbound_queue, req32); 714 715 return 0; 716 } 717 718 static int hptiop_get_config_mv(struct hpt_iop_hba * hba, 719 struct hpt_iop_request_get_config * config) 720 { 721 struct hpt_iop_request_get_config *req; 722 723 if (!(req = hba->ctlcfg_ptr)) 724 return -1; 725 726 req->header.flags = 0; 727 req->header.type = IOP_REQUEST_TYPE_GET_CONFIG; 728 req->header.size = sizeof(struct hpt_iop_request_get_config); 729 req->header.result = IOP_RESULT_PENDING; 730 req->header.context = MVIOP_CMD_TYPE_GET_CONFIG; 731 732 if (hptiop_send_sync_request_mv(hba, req, 20000)) { 733 KdPrint(("hptiop: get config send cmd failed")); 734 return -1; 735 } 736 737 *config = *req; 738 return 0; 739 } 740 741 static int hptiop_set_config_itl(struct hpt_iop_hba *hba, 742 struct hpt_iop_request_set_config *config) 743 { 744 u_int32_t req32; 745 746 req32 = BUS_SPACE_RD4_ITL(inbound_queue); 747 748 if (req32 == IOPMU_QUEUE_EMPTY) 749 return -1; 750 751 config->header.size = sizeof(struct hpt_iop_request_set_config); 752 config->header.type = IOP_REQUEST_TYPE_SET_CONFIG; 753 config->header.flags = IOP_REQUEST_FLAG_SYNC_REQUEST; 754 config->header.result = IOP_RESULT_PENDING; 755 config->header.context = 0; 756 757 bus_space_write_region_4(hba->bar0t, hba->bar0h, req32, 758 (u_int32_t *)config, 759 sizeof(struct hpt_iop_request_set_config) >> 2); 760 761 if (hptiop_send_sync_request_itl(hba, req32, 20000)) { 762 KdPrint(("hptiop: set config send cmd failed")); 763 return -1; 764 } 765 766 BUS_SPACE_WRT4_ITL(outbound_queue, req32); 767 768 return 0; 769 } 770 771 static int hptiop_set_config_mv(struct hpt_iop_hba *hba, 772 struct hpt_iop_request_set_config *config) 773 { 774 struct hpt_iop_request_set_config *req; 775 776 if (!(req = hba->ctlcfg_ptr)) 777 return -1; 778 779 memcpy((u_int8_t *)req + sizeof(struct hpt_iop_request_header), 780 (u_int8_t *)config + sizeof(struct hpt_iop_request_header), 781 sizeof(struct hpt_iop_request_set_config) - 782 sizeof(struct hpt_iop_request_header)); 783 784 req->header.flags = 0; 785 req->header.type = IOP_REQUEST_TYPE_SET_CONFIG; 786 req->header.size = sizeof(struct hpt_iop_request_set_config); 787 req->header.result = IOP_RESULT_PENDING; 788 req->header.context = MVIOP_CMD_TYPE_SET_CONFIG; 789 790 if (hptiop_send_sync_request_mv(hba, req, 20000)) { 791 KdPrint(("hptiop: set config send cmd failed")); 792 return -1; 793 } 794 795 return 0; 796 } 797 798 static int hptiop_post_ioctl_command_itl(struct hpt_iop_hba *hba, 799 u_int32_t req32, 800 struct hpt_iop_ioctl_param *pParams) 801 { 802 u_int64_t temp64; 803 struct hpt_iop_request_ioctl_command req; 804 805 if ((((pParams->nInBufferSize + 3) & ~3) + pParams->nOutBufferSize) > 806 (hba->max_request_size - 807 offsetof(struct hpt_iop_request_ioctl_command, buf))) { 808 device_printf(hba->pcidev, "request size beyond max value"); 809 return -1; 810 } 811 812 req.header.size = offsetof(struct hpt_iop_request_ioctl_command, buf) 813 + pParams->nInBufferSize; 814 req.header.type = IOP_REQUEST_TYPE_IOCTL_COMMAND; 815 req.header.flags = IOP_REQUEST_FLAG_SYNC_REQUEST; 816 req.header.result = IOP_RESULT_PENDING; 817 req.header.context = req32 + (u_int64_t)(unsigned long)hba->u.itl.mu; 818 req.ioctl_code = HPT_CTL_CODE_BSD_TO_IOP(pParams->dwIoControlCode); 819 req.inbuf_size = pParams->nInBufferSize; 820 req.outbuf_size = pParams->nOutBufferSize; 821 req.bytes_returned = 0; 822 823 bus_space_write_region_4(hba->bar0t, hba->bar0h, req32, (u_int32_t *)&req, 824 offsetof(struct hpt_iop_request_ioctl_command, buf)>>2); 825 826 hptiop_lock_adapter(hba); 827 828 BUS_SPACE_WRT4_ITL(inbound_queue, req32); 829 BUS_SPACE_RD4_ITL(outbound_intstatus); 830 831 bus_space_read_region_4(hba->bar0t, hba->bar0h, req32 + 832 offsetof(struct hpt_iop_request_ioctl_command, header.context), 833 (u_int32_t *)&temp64, 2); 834 while (temp64) { 835 if (hptiop_sleep(hba, (void *)((unsigned long)hba->u.itl.mu + req32), 836 0, "hptctl", HPT_OSM_TIMEOUT)==0) 837 break; 838 hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET, 60000); 839 bus_space_read_region_4(hba->bar0t, hba->bar0h,req32 + 840 offsetof(struct hpt_iop_request_ioctl_command, 841 header.context), 842 (u_int32_t *)&temp64, 2); 843 } 844 845 hptiop_unlock_adapter(hba); 846 return 0; 847 } 848 849 static int hptiop_bus_space_copyin(struct hpt_iop_hba *hba, u_int32_t bus, void *user, int size) 850 { 851 unsigned char byte; 852 int i; 853 854 for (i=0; i<size; i++) { 855 if (copyin((u_int8_t *)user + i, &byte, 1)) 856 return -1; 857 bus_space_write_1(hba->bar0t, hba->bar0h, bus + i, byte); 858 } 859 860 return 0; 861 } 862 863 static int hptiop_bus_space_copyout(struct hpt_iop_hba *hba, u_int32_t bus, void *user, int size) 864 { 865 unsigned char byte; 866 int i; 867 868 for (i=0; i<size; i++) { 869 byte = bus_space_read_1(hba->bar0t, hba->bar0h, bus + i); 870 if (copyout(&byte, (u_int8_t *)user + i, 1)) 871 return -1; 872 } 873 874 return 0; 875 } 876 877 static int hptiop_do_ioctl_itl(struct hpt_iop_hba *hba, 878 struct hpt_iop_ioctl_param * pParams) 879 { 880 u_int32_t req32; 881 u_int32_t result; 882 883 if ((pParams->Magic != HPT_IOCTL_MAGIC) && 884 (pParams->Magic != HPT_IOCTL_MAGIC32)) 885 return EFAULT; 886 887 req32 = BUS_SPACE_RD4_ITL(inbound_queue); 888 if (req32 == IOPMU_QUEUE_EMPTY) 889 return EFAULT; 890 891 if (pParams->nInBufferSize) 892 if (hptiop_bus_space_copyin(hba, req32 + 893 offsetof(struct hpt_iop_request_ioctl_command, buf), 894 (void *)pParams->lpInBuffer, pParams->nInBufferSize)) 895 goto invalid; 896 897 if (hptiop_post_ioctl_command_itl(hba, req32, pParams)) 898 goto invalid; 899 900 result = bus_space_read_4(hba->bar0t, hba->bar0h, req32 + 901 offsetof(struct hpt_iop_request_ioctl_command, 902 header.result)); 903 904 if (result == IOP_RESULT_SUCCESS) { 905 if (pParams->nOutBufferSize) 906 if (hptiop_bus_space_copyout(hba, req32 + 907 offsetof(struct hpt_iop_request_ioctl_command, buf) + 908 ((pParams->nInBufferSize + 3) & ~3), 909 (void *)pParams->lpOutBuffer, pParams->nOutBufferSize)) 910 goto invalid; 911 912 if (pParams->lpBytesReturned) { 913 if (hptiop_bus_space_copyout(hba, req32 + 914 offsetof(struct hpt_iop_request_ioctl_command, bytes_returned), 915 (void *)pParams->lpBytesReturned, sizeof(unsigned long))) 916 goto invalid; 917 } 918 919 BUS_SPACE_WRT4_ITL(outbound_queue, req32); 920 921 return 0; 922 } else{ 923 invalid: 924 BUS_SPACE_WRT4_ITL(outbound_queue, req32); 925 926 return EFAULT; 927 } 928 } 929 930 static int hptiop_post_ioctl_command_mv(struct hpt_iop_hba *hba, 931 struct hpt_iop_request_ioctl_command *req, 932 struct hpt_iop_ioctl_param *pParams) 933 { 934 u_int64_t req_phy; 935 int size = 0; 936 937 if ((((pParams->nInBufferSize + 3) & ~3) + pParams->nOutBufferSize) > 938 (hba->max_request_size - 939 offsetof(struct hpt_iop_request_ioctl_command, buf))) { 940 device_printf(hba->pcidev, "request size beyond max value"); 941 return -1; 942 } 943 944 req->ioctl_code = HPT_CTL_CODE_BSD_TO_IOP(pParams->dwIoControlCode); 945 req->inbuf_size = pParams->nInBufferSize; 946 req->outbuf_size = pParams->nOutBufferSize; 947 req->header.size = offsetof(struct hpt_iop_request_ioctl_command, buf) 948 + pParams->nInBufferSize; 949 req->header.context = (u_int64_t)MVIOP_CMD_TYPE_IOCTL; 950 req->header.type = IOP_REQUEST_TYPE_IOCTL_COMMAND; 951 req->header.result = IOP_RESULT_PENDING; 952 req->header.flags = IOP_REQUEST_FLAG_OUTPUT_CONTEXT; 953 size = req->header.size >> 8; 954 size = size > 3 ? 3 : size; 955 req_phy = hba->ctlcfgcmd_phy | MVIOP_MU_QUEUE_ADDR_HOST_BIT | size; 956 hptiop_mv_inbound_write(req_phy, hba); 957 958 BUS_SPACE_RD4_MV0(outbound_intmask); 959 960 while (hba->config_done == 0) { 961 if (hptiop_sleep(hba, req, 0, 962 "hptctl", HPT_OSM_TIMEOUT)==0) 963 continue; 964 hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET, 60000); 965 } 966 return 0; 967 } 968 969 static int hptiop_do_ioctl_mv(struct hpt_iop_hba *hba, 970 struct hpt_iop_ioctl_param *pParams) 971 { 972 struct hpt_iop_request_ioctl_command *req; 973 974 if ((pParams->Magic != HPT_IOCTL_MAGIC) && 975 (pParams->Magic != HPT_IOCTL_MAGIC32)) 976 return EFAULT; 977 978 req = (struct hpt_iop_request_ioctl_command *)(hba->ctlcfg_ptr); 979 hba->config_done = 0; 980 hptiop_lock_adapter(hba); 981 if (pParams->nInBufferSize) 982 if (copyin((void *)pParams->lpInBuffer, 983 req->buf, pParams->nInBufferSize)) 984 goto invalid; 985 if (hptiop_post_ioctl_command_mv(hba, req, pParams)) 986 goto invalid; 987 988 if (hba->config_done == 1) { 989 if (pParams->nOutBufferSize) 990 if (copyout(req->buf + 991 ((pParams->nInBufferSize + 3) & ~3), 992 (void *)pParams->lpOutBuffer, 993 pParams->nOutBufferSize)) 994 goto invalid; 995 996 if (pParams->lpBytesReturned) 997 if (copyout(&req->bytes_returned, 998 (void*)pParams->lpBytesReturned, 999 sizeof(u_int32_t))) 1000 goto invalid; 1001 hptiop_unlock_adapter(hba); 1002 return 0; 1003 } else{ 1004 invalid: 1005 hptiop_unlock_adapter(hba); 1006 return EFAULT; 1007 } 1008 } 1009 1010 static int hptiop_rescan_bus(struct hpt_iop_hba * hba) 1011 { 1012 union ccb *ccb; 1013 1014 if ((ccb = xpt_alloc_ccb()) == NULL) 1015 return(ENOMEM); 1016 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, cam_sim_path(hba->sim), 1017 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 1018 xpt_free_ccb(ccb); 1019 return(EIO); 1020 } 1021 1022 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, 5/*priority (low)*/); 1023 ccb->ccb_h.func_code = XPT_SCAN_BUS; 1024 ccb->ccb_h.cbfcnp = hptiop_bus_scan_cb; 1025 ccb->crcn.flags = CAM_FLAG_NONE; 1026 xpt_action(ccb); 1027 return(0); 1028 } 1029 1030 static void hptiop_bus_scan_cb(struct cam_periph *periph, union ccb *ccb) 1031 { 1032 xpt_free_path(ccb->ccb_h.path); 1033 kfree(ccb, M_TEMP); 1034 } 1035 1036 static bus_dmamap_callback_t hptiop_map_srb; 1037 static bus_dmamap_callback_t hptiop_post_scsi_command; 1038 static bus_dmamap_callback_t hptiop_mv_map_ctlcfg; 1039 1040 static int hptiop_alloc_pci_res_itl(struct hpt_iop_hba *hba) 1041 { 1042 hba->bar0_rid = 0x10; 1043 hba->bar0_res = bus_alloc_resource_any(hba->pcidev, 1044 SYS_RES_MEMORY, &hba->bar0_rid, RF_ACTIVE); 1045 1046 if (hba->bar0_res == NULL) { 1047 device_printf(hba->pcidev, 1048 "failed to get iop base adrress.\n"); 1049 return -1; 1050 } 1051 hba->bar0t = rman_get_bustag(hba->bar0_res); 1052 hba->bar0h = rman_get_bushandle(hba->bar0_res); 1053 hba->u.itl.mu = (struct hpt_iopmu_itl *) 1054 rman_get_virtual(hba->bar0_res); 1055 1056 if (!hba->u.itl.mu) { 1057 bus_release_resource(hba->pcidev, SYS_RES_MEMORY, 1058 hba->bar0_rid, hba->bar0_res); 1059 device_printf(hba->pcidev, "alloc mem res failed\n"); 1060 return -1; 1061 } 1062 1063 return 0; 1064 } 1065 1066 static int hptiop_alloc_pci_res_mv(struct hpt_iop_hba *hba) 1067 { 1068 hba->bar0_rid = 0x10; 1069 hba->bar0_res = bus_alloc_resource_any(hba->pcidev, 1070 SYS_RES_MEMORY, &hba->bar0_rid, RF_ACTIVE); 1071 1072 if (hba->bar0_res == NULL) { 1073 device_printf(hba->pcidev, "failed to get iop bar0.\n"); 1074 return -1; 1075 } 1076 hba->bar0t = rman_get_bustag(hba->bar0_res); 1077 hba->bar0h = rman_get_bushandle(hba->bar0_res); 1078 hba->u.mv.regs = (struct hpt_iopmv_regs *) 1079 rman_get_virtual(hba->bar0_res); 1080 1081 if (!hba->u.mv.regs) { 1082 bus_release_resource(hba->pcidev, SYS_RES_MEMORY, 1083 hba->bar0_rid, hba->bar0_res); 1084 device_printf(hba->pcidev, "alloc bar0 mem res failed\n"); 1085 return -1; 1086 } 1087 1088 hba->bar2_rid = 0x18; 1089 hba->bar2_res = bus_alloc_resource_any(hba->pcidev, 1090 SYS_RES_MEMORY, &hba->bar2_rid, RF_ACTIVE); 1091 1092 if (hba->bar2_res == NULL) { 1093 bus_release_resource(hba->pcidev, SYS_RES_MEMORY, 1094 hba->bar0_rid, hba->bar0_res); 1095 device_printf(hba->pcidev, "failed to get iop bar2.\n"); 1096 return -1; 1097 } 1098 1099 hba->bar2t = rman_get_bustag(hba->bar2_res); 1100 hba->bar2h = rman_get_bushandle(hba->bar2_res); 1101 hba->u.mv.mu = (struct hpt_iopmu_mv *)rman_get_virtual(hba->bar2_res); 1102 1103 if (!hba->u.mv.mu) { 1104 bus_release_resource(hba->pcidev, SYS_RES_MEMORY, 1105 hba->bar0_rid, hba->bar0_res); 1106 bus_release_resource(hba->pcidev, SYS_RES_MEMORY, 1107 hba->bar2_rid, hba->bar2_res); 1108 device_printf(hba->pcidev, "alloc mem bar2 res failed\n"); 1109 return -1; 1110 } 1111 1112 return 0; 1113 } 1114 1115 static void hptiop_release_pci_res_itl(struct hpt_iop_hba *hba) 1116 { 1117 if (hba->bar0_res) 1118 bus_release_resource(hba->pcidev, SYS_RES_MEMORY, 1119 hba->bar0_rid, hba->bar0_res); 1120 } 1121 1122 static void hptiop_release_pci_res_mv(struct hpt_iop_hba *hba) 1123 { 1124 if (hba->bar0_res) 1125 bus_release_resource(hba->pcidev, SYS_RES_MEMORY, 1126 hba->bar0_rid, hba->bar0_res); 1127 if (hba->bar2_res) 1128 bus_release_resource(hba->pcidev, SYS_RES_MEMORY, 1129 hba->bar2_rid, hba->bar2_res); 1130 } 1131 1132 static int hptiop_internal_memalloc_mv(struct hpt_iop_hba *hba) 1133 { 1134 if (bus_dma_tag_create(hba->parent_dmat, 1135 1, 1136 0, 1137 BUS_SPACE_MAXADDR_32BIT, 1138 BUS_SPACE_MAXADDR, 1139 NULL, NULL, 1140 0x800 - 0x8, 1141 1, 1142 BUS_SPACE_MAXSIZE_32BIT, 1143 BUS_DMA_ALLOCNOW, 1144 &hba->ctlcfg_dmat)) { 1145 device_printf(hba->pcidev, "alloc ctlcfg_dmat failed\n"); 1146 return -1; 1147 } 1148 1149 if (bus_dmamem_alloc(hba->ctlcfg_dmat, (void **)&hba->ctlcfg_ptr, 1150 BUS_DMA_WAITOK | BUS_DMA_COHERENT, 1151 &hba->ctlcfg_dmamap) != 0) { 1152 device_printf(hba->pcidev, 1153 "bus_dmamem_alloc failed!\n"); 1154 bus_dma_tag_destroy(hba->ctlcfg_dmat); 1155 return -1; 1156 } 1157 1158 if (bus_dmamap_load(hba->ctlcfg_dmat, 1159 hba->ctlcfg_dmamap, hba->ctlcfg_ptr, 1160 MVIOP_IOCTLCFG_SIZE, 1161 hptiop_mv_map_ctlcfg, hba, 0)) { 1162 device_printf(hba->pcidev, "bus_dmamap_load failed!\n"); 1163 if (hba->ctlcfg_dmat) 1164 bus_dmamem_free(hba->ctlcfg_dmat, 1165 hba->ctlcfg_ptr, hba->ctlcfg_dmamap); 1166 bus_dma_tag_destroy(hba->ctlcfg_dmat); 1167 return -1; 1168 } 1169 1170 return 0; 1171 } 1172 1173 static int hptiop_internal_memfree_mv(struct hpt_iop_hba *hba) 1174 { 1175 if (hba->ctlcfg_dmat) { 1176 bus_dmamap_unload(hba->ctlcfg_dmat, hba->ctlcfg_dmamap); 1177 bus_dmamem_free(hba->ctlcfg_dmat, 1178 hba->ctlcfg_ptr, hba->ctlcfg_dmamap); 1179 bus_dma_tag_destroy(hba->ctlcfg_dmat); 1180 } 1181 1182 return 0; 1183 } 1184 1185 /* 1186 * CAM driver interface 1187 */ 1188 static device_method_t driver_methods[] = { 1189 /* Device interface */ 1190 DEVMETHOD(device_probe, hptiop_probe), 1191 DEVMETHOD(device_attach, hptiop_attach), 1192 DEVMETHOD(device_detach, hptiop_detach), 1193 DEVMETHOD(device_shutdown, hptiop_shutdown), 1194 { 0, 0 } 1195 }; 1196 1197 static struct hptiop_adapter_ops hptiop_itl_ops = { 1198 .iop_wait_ready = hptiop_wait_ready_itl, 1199 .internal_memalloc = 0, 1200 .internal_memfree = 0, 1201 .alloc_pci_res = hptiop_alloc_pci_res_itl, 1202 .release_pci_res = hptiop_release_pci_res_itl, 1203 .enable_intr = hptiop_enable_intr_itl, 1204 .disable_intr = hptiop_disable_intr_itl, 1205 .get_config = hptiop_get_config_itl, 1206 .set_config = hptiop_set_config_itl, 1207 .iop_intr = hptiop_intr_itl, 1208 .post_msg = hptiop_post_msg_itl, 1209 .post_req = hptiop_post_req_itl, 1210 .do_ioctl = hptiop_do_ioctl_itl, 1211 }; 1212 1213 static struct hptiop_adapter_ops hptiop_mv_ops = { 1214 .iop_wait_ready = hptiop_wait_ready_mv, 1215 .internal_memalloc = hptiop_internal_memalloc_mv, 1216 .internal_memfree = hptiop_internal_memfree_mv, 1217 .alloc_pci_res = hptiop_alloc_pci_res_mv, 1218 .release_pci_res = hptiop_release_pci_res_mv, 1219 .enable_intr = hptiop_enable_intr_mv, 1220 .disable_intr = hptiop_disable_intr_mv, 1221 .get_config = hptiop_get_config_mv, 1222 .set_config = hptiop_set_config_mv, 1223 .iop_intr = hptiop_intr_mv, 1224 .post_msg = hptiop_post_msg_mv, 1225 .post_req = hptiop_post_req_mv, 1226 .do_ioctl = hptiop_do_ioctl_mv, 1227 }; 1228 1229 static driver_t hptiop_pci_driver = { 1230 driver_name, 1231 driver_methods, 1232 sizeof(struct hpt_iop_hba) 1233 }; 1234 1235 DRIVER_MODULE(hptiop, pci, hptiop_pci_driver, hptiop_devclass, NULL, NULL); 1236 MODULE_VERSION(hptiop, 1); 1237 1238 static int hptiop_probe(device_t dev) 1239 { 1240 struct hpt_iop_hba *hba; 1241 u_int32_t id; 1242 static char buf[256]; 1243 int sas = 0; 1244 struct hptiop_adapter_ops *ops; 1245 1246 if (pci_get_vendor(dev) != 0x1103) 1247 return (ENXIO); 1248 1249 id = pci_get_device(dev); 1250 1251 switch (id) { 1252 case 0x4322: 1253 case 0x4321: 1254 case 0x4320: 1255 sas = 1; 1256 case 0x3220: 1257 case 0x3320: 1258 case 0x3410: 1259 case 0x3520: 1260 case 0x3510: 1261 case 0x3511: 1262 case 0x3521: 1263 case 0x3522: 1264 case 0x3540: 1265 ops = &hptiop_itl_ops; 1266 break; 1267 case 0x3120: 1268 case 0x3122: 1269 case 0x3020: 1270 ops = &hptiop_mv_ops; 1271 break; 1272 default: 1273 return (ENXIO); 1274 } 1275 1276 device_printf(dev, "adapter at PCI %d:%d:%d, IRQ %d\n", 1277 pci_get_bus(dev), pci_get_slot(dev), 1278 pci_get_function(dev), pci_get_irq(dev)); 1279 1280 ksprintf(buf, "RocketRAID %x %s Controller", 1281 id, sas ? "SAS" : "SATA"); 1282 device_set_desc_copy(dev, buf); 1283 1284 hba = (struct hpt_iop_hba *)device_get_softc(dev); 1285 bzero(hba, sizeof(struct hpt_iop_hba)); 1286 hba->ops = ops; 1287 1288 KdPrint(("hba->ops=%p\n", hba->ops)); 1289 return 0; 1290 } 1291 1292 static int hptiop_attach(device_t dev) 1293 { 1294 struct hpt_iop_hba *hba = (struct hpt_iop_hba *)device_get_softc(dev); 1295 struct hpt_iop_request_get_config iop_config; 1296 struct hpt_iop_request_set_config set_config; 1297 int rid = 0; 1298 struct cam_devq *devq; 1299 struct ccb_setasync ccb; 1300 u_int32_t unit = device_get_unit(dev); 1301 1302 device_printf(dev, "RocketRAID 3xxx/4xxx controller driver %s\n", 1303 driver_version); 1304 1305 KdPrint(("hptiop: attach(%d, %d/%d/%d) ops=%p\n", unit, 1306 pci_get_bus(dev), pci_get_slot(dev), 1307 pci_get_function(dev), hba->ops)); 1308 1309 pci_enable_busmaster(dev); 1310 hba->pcidev = dev; 1311 1312 if (hba->ops->alloc_pci_res(hba)) 1313 return ENXIO; 1314 1315 if (hba->ops->iop_wait_ready(hba, 2000)) { 1316 device_printf(dev, "adapter is not ready\n"); 1317 goto release_pci_res; 1318 } 1319 1320 lockinit(&hba->lock, "hptioplock", 0, LK_CANRECURSE); 1321 1322 if (bus_dma_tag_create(NULL,/* parent */ 1323 1, /* alignment */ 1324 0, /* boundary */ 1325 BUS_SPACE_MAXADDR, /* lowaddr */ 1326 BUS_SPACE_MAXADDR, /* highaddr */ 1327 NULL, NULL, /* filter, filterarg */ 1328 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */ 1329 BUS_SPACE_UNRESTRICTED, /* nsegments */ 1330 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */ 1331 0, /* flags */ 1332 &hba->parent_dmat /* tag */)) 1333 { 1334 device_printf(dev, "alloc parent_dmat failed\n"); 1335 goto release_pci_res; 1336 } 1337 1338 if (hba->ops->internal_memalloc) { 1339 if (hba->ops->internal_memalloc(hba)) { 1340 device_printf(dev, "alloc srb_dmat failed\n"); 1341 goto destroy_parent_tag; 1342 } 1343 } 1344 1345 if (hba->ops->get_config(hba, &iop_config)) { 1346 device_printf(dev, "get iop config failed.\n"); 1347 goto get_config_failed; 1348 } 1349 1350 hba->firmware_version = iop_config.firmware_version; 1351 hba->interface_version = iop_config.interface_version; 1352 hba->max_requests = iop_config.max_requests; 1353 hba->max_devices = iop_config.max_devices; 1354 hba->max_request_size = iop_config.request_size; 1355 hba->max_sg_count = iop_config.max_sg_count; 1356 1357 if (bus_dma_tag_create(hba->parent_dmat,/* parent */ 1358 4, /* alignment */ 1359 BUS_SPACE_MAXADDR_32BIT+1, /* boundary */ 1360 BUS_SPACE_MAXADDR, /* lowaddr */ 1361 BUS_SPACE_MAXADDR, /* highaddr */ 1362 NULL, NULL, /* filter, filterarg */ 1363 PAGE_SIZE * (hba->max_sg_count-1), /* maxsize */ 1364 hba->max_sg_count, /* nsegments */ 1365 0x20000, /* maxsegsize */ 1366 BUS_DMA_ALLOCNOW, /* flags */ 1367 &hba->io_dmat /* tag */)) 1368 { 1369 device_printf(dev, "alloc io_dmat failed\n"); 1370 goto get_config_failed; 1371 } 1372 1373 if (bus_dma_tag_create(hba->parent_dmat,/* parent */ 1374 1, /* alignment */ 1375 0, /* boundary */ 1376 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 1377 BUS_SPACE_MAXADDR, /* highaddr */ 1378 NULL, NULL, /* filter, filterarg */ 1379 HPT_SRB_MAX_SIZE * HPT_SRB_MAX_QUEUE_SIZE + 0x20, 1380 1, /* nsegments */ 1381 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */ 1382 0, /* flags */ 1383 &hba->srb_dmat /* tag */)) 1384 { 1385 device_printf(dev, "alloc srb_dmat failed\n"); 1386 goto destroy_io_dmat; 1387 } 1388 1389 if (bus_dmamem_alloc(hba->srb_dmat, (void **)&hba->uncached_ptr, 1390 BUS_DMA_WAITOK | BUS_DMA_COHERENT, 1391 &hba->srb_dmamap) != 0) 1392 { 1393 device_printf(dev, "srb bus_dmamem_alloc failed!\n"); 1394 goto destroy_srb_dmat; 1395 } 1396 1397 if (bus_dmamap_load(hba->srb_dmat, 1398 hba->srb_dmamap, hba->uncached_ptr, 1399 (HPT_SRB_MAX_SIZE * HPT_SRB_MAX_QUEUE_SIZE) + 0x20, 1400 hptiop_map_srb, hba, 0)) 1401 { 1402 device_printf(dev, "bus_dmamap_load failed!\n"); 1403 goto srb_dmamem_free; 1404 } 1405 1406 if ((devq = cam_simq_alloc(hba->max_requests - 1 )) == NULL) { 1407 device_printf(dev, "cam_simq_alloc failed\n"); 1408 goto srb_dmamap_unload; 1409 } 1410 1411 hba->sim = cam_sim_alloc(hptiop_action, hptiop_poll, driver_name, 1412 hba, unit, &sim_mplock, hba->max_requests - 1, 1, devq); 1413 if (!hba->sim) { 1414 device_printf(dev, "cam_sim_alloc failed\n"); 1415 cam_simq_release(devq); 1416 goto srb_dmamap_unload; 1417 } 1418 if (xpt_bus_register(hba->sim, 0) != CAM_SUCCESS) 1419 { 1420 device_printf(dev, "xpt_bus_register failed\n"); 1421 goto free_cam_sim; 1422 } 1423 1424 if (xpt_create_path(&hba->path, /*periph */ NULL, 1425 cam_sim_path(hba->sim), CAM_TARGET_WILDCARD, 1426 CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 1427 device_printf(dev, "xpt_create_path failed\n"); 1428 goto deregister_xpt_bus; 1429 } 1430 1431 bzero(&set_config, sizeof(set_config)); 1432 set_config.iop_id = unit; 1433 set_config.vbus_id = cam_sim_path(hba->sim); 1434 set_config.max_host_request_size = HPT_SRB_MAX_REQ_SIZE; 1435 1436 if (hba->ops->set_config(hba, &set_config)) { 1437 device_printf(dev, "set iop config failed.\n"); 1438 goto free_hba_path; 1439 } 1440 1441 xpt_setup_ccb(&ccb.ccb_h, hba->path, /*priority*/5); 1442 ccb.ccb_h.func_code = XPT_SASYNC_CB; 1443 ccb.event_enable = (AC_FOUND_DEVICE | AC_LOST_DEVICE); 1444 ccb.callback = hptiop_async; 1445 ccb.callback_arg = hba->sim; 1446 xpt_action((union ccb *)&ccb); 1447 1448 rid = 0; 1449 if ((hba->irq_res = bus_alloc_resource(hba->pcidev, SYS_RES_IRQ, 1450 &rid, 0, ~0ul, 1, RF_SHAREABLE | RF_ACTIVE)) == NULL) { 1451 device_printf(dev, "allocate irq failed!\n"); 1452 goto free_hba_path; 1453 } 1454 1455 if (bus_setup_intr(hba->pcidev, hba->irq_res, 0, 1456 hptiop_pci_intr, hba, &hba->irq_handle, NULL)) 1457 { 1458 device_printf(dev, "allocate intr function failed!\n"); 1459 goto free_irq_resource; 1460 } 1461 1462 if (hptiop_send_sync_msg(hba, 1463 IOPMU_INBOUND_MSG0_START_BACKGROUND_TASK, 5000)) { 1464 device_printf(dev, "fail to start background task\n"); 1465 goto teartown_irq_resource; 1466 } 1467 1468 hba->ops->enable_intr(hba); 1469 1470 hba->ioctl_dev = make_dev(&hptiop_ops, unit, 1471 UID_ROOT, GID_WHEEL /*GID_OPERATOR*/, 1472 S_IRUSR | S_IWUSR, "%s%d", driver_name, unit); 1473 1474 hba->ioctl_dev->si_drv1 = hba; 1475 1476 hptiop_rescan_bus(hba); 1477 1478 return 0; 1479 1480 1481 teartown_irq_resource: 1482 bus_teardown_intr(dev, hba->irq_res, hba->irq_handle); 1483 1484 free_irq_resource: 1485 bus_release_resource(dev, SYS_RES_IRQ, 0, hba->irq_res); 1486 1487 free_hba_path: 1488 xpt_free_path(hba->path); 1489 1490 deregister_xpt_bus: 1491 xpt_bus_deregister(cam_sim_path(hba->sim)); 1492 1493 free_cam_sim: 1494 cam_sim_free(hba->sim); 1495 1496 srb_dmamap_unload: 1497 if (hba->uncached_ptr) 1498 bus_dmamap_unload(hba->srb_dmat, hba->srb_dmamap); 1499 1500 srb_dmamem_free: 1501 if (hba->uncached_ptr) 1502 bus_dmamem_free(hba->srb_dmat, 1503 hba->uncached_ptr, hba->srb_dmamap); 1504 1505 destroy_srb_dmat: 1506 if (hba->srb_dmat) 1507 bus_dma_tag_destroy(hba->srb_dmat); 1508 1509 destroy_io_dmat: 1510 if (hba->io_dmat) 1511 bus_dma_tag_destroy(hba->io_dmat); 1512 1513 get_config_failed: 1514 if (hba->ops->internal_memfree) 1515 hba->ops->internal_memfree(hba); 1516 1517 destroy_parent_tag: 1518 if (hba->parent_dmat) 1519 bus_dma_tag_destroy(hba->parent_dmat); 1520 1521 release_pci_res: 1522 if (hba->ops->release_pci_res) 1523 hba->ops->release_pci_res(hba); 1524 1525 return ENXIO; 1526 } 1527 1528 static int hptiop_detach(device_t dev) 1529 { 1530 struct hpt_iop_hba * hba = (struct hpt_iop_hba *)device_get_softc(dev); 1531 int i; 1532 int error = EBUSY; 1533 1534 hptiop_lock_adapter(hba); 1535 for (i = 0; i < hba->max_devices; i++) 1536 if (hptiop_os_query_remove_device(hba, i)) { 1537 device_printf(dev, "file system is busy. id=%d", i); 1538 goto out; 1539 } 1540 1541 if ((error = hptiop_shutdown(dev)) != 0) 1542 goto out; 1543 if (hptiop_send_sync_msg(hba, 1544 IOPMU_INBOUND_MSG0_STOP_BACKGROUND_TASK, 60000)) 1545 goto out; 1546 1547 hptiop_release_resource(hba); 1548 error = 0; 1549 out: 1550 hptiop_unlock_adapter(hba); 1551 return error; 1552 } 1553 1554 static int hptiop_shutdown(device_t dev) 1555 { 1556 struct hpt_iop_hba * hba = (struct hpt_iop_hba *)device_get_softc(dev); 1557 1558 int error = 0; 1559 1560 if (hba->flag & HPT_IOCTL_FLAG_OPEN) { 1561 device_printf(dev, "device is busy"); 1562 return EBUSY; 1563 } 1564 1565 hba->ops->disable_intr(hba); 1566 1567 if (hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_SHUTDOWN, 60000)) 1568 error = EBUSY; 1569 1570 return error; 1571 } 1572 1573 static void hptiop_pci_intr(void *arg) 1574 { 1575 struct hpt_iop_hba * hba = (struct hpt_iop_hba *)arg; 1576 hptiop_lock_adapter(hba); 1577 hba->ops->iop_intr(hba); 1578 hptiop_unlock_adapter(hba); 1579 } 1580 1581 static void hptiop_poll(struct cam_sim *sim) 1582 { 1583 hptiop_pci_intr(cam_sim_softc(sim)); 1584 } 1585 1586 static void hptiop_async(void * callback_arg, u_int32_t code, 1587 struct cam_path * path, void * arg) 1588 { 1589 } 1590 1591 static void hptiop_enable_intr_itl(struct hpt_iop_hba *hba) 1592 { 1593 BUS_SPACE_WRT4_ITL(outbound_intmask, 1594 ~(IOPMU_OUTBOUND_INT_POSTQUEUE | IOPMU_OUTBOUND_INT_MSG0)); 1595 } 1596 1597 static void hptiop_enable_intr_mv(struct hpt_iop_hba *hba) 1598 { 1599 u_int32_t int_mask; 1600 1601 int_mask = BUS_SPACE_RD4_MV0(outbound_intmask); 1602 1603 int_mask |= MVIOP_MU_OUTBOUND_INT_POSTQUEUE 1604 | MVIOP_MU_OUTBOUND_INT_MSG; 1605 BUS_SPACE_WRT4_MV0(outbound_intmask,int_mask); 1606 } 1607 1608 static void hptiop_disable_intr_itl(struct hpt_iop_hba *hba) 1609 { 1610 u_int32_t int_mask; 1611 1612 int_mask = BUS_SPACE_RD4_ITL(outbound_intmask); 1613 1614 int_mask |= IOPMU_OUTBOUND_INT_POSTQUEUE | IOPMU_OUTBOUND_INT_MSG0; 1615 BUS_SPACE_WRT4_ITL(outbound_intmask, int_mask); 1616 BUS_SPACE_RD4_ITL(outbound_intstatus); 1617 } 1618 1619 static void hptiop_disable_intr_mv(struct hpt_iop_hba *hba) 1620 { 1621 u_int32_t int_mask; 1622 int_mask = BUS_SPACE_RD4_MV0(outbound_intmask); 1623 1624 int_mask &= ~(MVIOP_MU_OUTBOUND_INT_MSG 1625 | MVIOP_MU_OUTBOUND_INT_POSTQUEUE); 1626 BUS_SPACE_WRT4_MV0(outbound_intmask,int_mask); 1627 BUS_SPACE_RD4_MV0(outbound_intmask); 1628 } 1629 1630 static int hptiop_reset_adapter(struct hpt_iop_hba * hba) 1631 { 1632 return hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET, 60000); 1633 } 1634 1635 static void *hptiop_get_srb(struct hpt_iop_hba * hba) 1636 { 1637 struct hpt_iop_srb * srb; 1638 1639 if (hba->srb_list) { 1640 srb = hba->srb_list; 1641 hba->srb_list = srb->next; 1642 return srb; 1643 } 1644 1645 return NULL; 1646 } 1647 1648 static void hptiop_free_srb(struct hpt_iop_hba *hba, struct hpt_iop_srb *srb) 1649 { 1650 srb->next = hba->srb_list; 1651 hba->srb_list = srb; 1652 } 1653 1654 static void hptiop_action(struct cam_sim *sim, union ccb *ccb) 1655 { 1656 struct hpt_iop_hba * hba = (struct hpt_iop_hba *)cam_sim_softc(sim); 1657 struct hpt_iop_srb * srb; 1658 1659 switch (ccb->ccb_h.func_code) { 1660 1661 case XPT_SCSI_IO: 1662 hptiop_lock_adapter(hba); 1663 if (ccb->ccb_h.target_lun != 0 || 1664 ccb->ccb_h.target_id >= hba->max_devices || 1665 (ccb->ccb_h.flags & CAM_CDB_PHYS)) 1666 { 1667 ccb->ccb_h.status = CAM_TID_INVALID; 1668 xpt_done(ccb); 1669 goto scsi_done; 1670 } 1671 1672 if ((srb = hptiop_get_srb(hba)) == NULL) { 1673 device_printf(hba->pcidev, "srb allocated failed"); 1674 ccb->ccb_h.status = CAM_REQ_CMP_ERR; 1675 xpt_done(ccb); 1676 goto scsi_done; 1677 } 1678 1679 srb->ccb = ccb; 1680 1681 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE) 1682 hptiop_post_scsi_command(srb, NULL, 0, 0); 1683 else if ((ccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) { 1684 if ((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0) { 1685 int error; 1686 1687 error = bus_dmamap_load(hba->io_dmat, 1688 srb->dma_map, 1689 ccb->csio.data_ptr, 1690 ccb->csio.dxfer_len, 1691 hptiop_post_scsi_command, 1692 srb, 0); 1693 1694 if (error && error != EINPROGRESS) { 1695 device_printf(hba->pcidev, 1696 "bus_dmamap_load error %d", error); 1697 xpt_freeze_simq(hba->sim, 1); 1698 ccb->ccb_h.status = CAM_REQ_CMP_ERR; 1699 invalid: 1700 hptiop_free_srb(hba, srb); 1701 xpt_done(ccb); 1702 goto scsi_done; 1703 } 1704 } 1705 else { 1706 device_printf(hba->pcidev, 1707 "CAM_DATA_PHYS not supported"); 1708 ccb->ccb_h.status = CAM_REQ_CMP_ERR; 1709 goto invalid; 1710 } 1711 } 1712 else { 1713 struct bus_dma_segment *segs; 1714 1715 if ((ccb->ccb_h.flags & CAM_SG_LIST_PHYS) == 0 || 1716 (ccb->ccb_h.flags & CAM_DATA_PHYS) != 0) { 1717 device_printf(hba->pcidev, "SCSI cmd failed"); 1718 ccb->ccb_h.status=CAM_PROVIDE_FAIL; 1719 goto invalid; 1720 } 1721 1722 segs = (struct bus_dma_segment *)ccb->csio.data_ptr; 1723 hptiop_post_scsi_command(srb, segs, 1724 ccb->csio.sglist_cnt, 0); 1725 } 1726 1727 scsi_done: 1728 hptiop_unlock_adapter(hba); 1729 return; 1730 1731 case XPT_RESET_BUS: 1732 device_printf(hba->pcidev, "reset adapter"); 1733 hptiop_lock_adapter(hba); 1734 hba->msg_done = 0; 1735 hptiop_reset_adapter(hba); 1736 hptiop_unlock_adapter(hba); 1737 break; 1738 1739 case XPT_GET_TRAN_SETTINGS: 1740 case XPT_SET_TRAN_SETTINGS: 1741 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; 1742 break; 1743 1744 case XPT_CALC_GEOMETRY: 1745 ccb->ccg.heads = 255; 1746 ccb->ccg.secs_per_track = 63; 1747 ccb->ccg.cylinders = ccb->ccg.volume_size / 1748 (ccb->ccg.heads * ccb->ccg.secs_per_track); 1749 ccb->ccb_h.status = CAM_REQ_CMP; 1750 break; 1751 1752 case XPT_PATH_INQ: 1753 { 1754 struct ccb_pathinq *cpi = &ccb->cpi; 1755 1756 cpi->version_num = 1; 1757 cpi->hba_inquiry = PI_SDTR_ABLE; 1758 cpi->target_sprt = 0; 1759 cpi->hba_misc = PIM_NOBUSRESET; 1760 cpi->hba_eng_cnt = 0; 1761 cpi->max_target = hba->max_devices; 1762 cpi->max_lun = 0; 1763 cpi->unit_number = cam_sim_unit(sim); 1764 cpi->bus_id = cam_sim_bus(sim); 1765 cpi->initiator_id = hba->max_devices; 1766 cpi->base_transfer_speed = 3300; 1767 1768 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 1769 strncpy(cpi->hba_vid, "HPT ", HBA_IDLEN); 1770 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 1771 cpi->transport = XPORT_SPI; 1772 cpi->transport_version = 2; 1773 cpi->protocol = PROTO_SCSI; 1774 cpi->protocol_version = SCSI_REV_2; 1775 cpi->ccb_h.status = CAM_REQ_CMP; 1776 break; 1777 } 1778 1779 default: 1780 ccb->ccb_h.status = CAM_REQ_INVALID; 1781 break; 1782 } 1783 1784 xpt_done(ccb); 1785 return; 1786 } 1787 1788 static void hptiop_post_req_itl(struct hpt_iop_hba *hba, 1789 struct hpt_iop_srb *srb, 1790 bus_dma_segment_t *segs, int nsegs) 1791 { 1792 int idx; 1793 union ccb *ccb = srb->ccb; 1794 u_int8_t *cdb; 1795 1796 if (ccb->ccb_h.flags & CAM_CDB_POINTER) 1797 cdb = ccb->csio.cdb_io.cdb_ptr; 1798 else 1799 cdb = ccb->csio.cdb_io.cdb_bytes; 1800 1801 KdPrint(("ccb=%p %x-%x-%x\n", 1802 ccb, *(u_int32_t *)cdb, *((u_int32_t *)cdb+1), *((u_int32_t *)cdb+2))); 1803 1804 if (srb->srb_flag & HPT_SRB_FLAG_HIGH_MEM_ACESS) { 1805 u_int32_t iop_req32; 1806 struct hpt_iop_request_scsi_command req; 1807 1808 iop_req32 = BUS_SPACE_RD4_ITL(inbound_queue); 1809 1810 if (iop_req32 == IOPMU_QUEUE_EMPTY) { 1811 device_printf(hba->pcidev, "invaild req offset\n"); 1812 ccb->ccb_h.status = CAM_BUSY; 1813 bus_dmamap_unload(hba->io_dmat, srb->dma_map); 1814 hptiop_free_srb(hba, srb); 1815 xpt_done(ccb); 1816 return; 1817 } 1818 1819 if (ccb->csio.dxfer_len && nsegs > 0) { 1820 struct hpt_iopsg *psg = req.sg_list; 1821 for (idx = 0; idx < nsegs; idx++, psg++) { 1822 psg->pci_address = (u_int64_t)segs[idx].ds_addr; 1823 psg->size = segs[idx].ds_len; 1824 psg->eot = 0; 1825 } 1826 psg[-1].eot = 1; 1827 } 1828 1829 bcopy(cdb, req.cdb, ccb->csio.cdb_len); 1830 1831 req.header.size = offsetof(struct hpt_iop_request_scsi_command, sg_list) 1832 + nsegs*sizeof(struct hpt_iopsg); 1833 req.header.type = IOP_REQUEST_TYPE_SCSI_COMMAND; 1834 req.header.flags = 0; 1835 req.header.result = IOP_RESULT_PENDING; 1836 req.header.context = (u_int64_t)(unsigned long)srb; 1837 req.dataxfer_length = ccb->csio.dxfer_len; 1838 req.channel = 0; 1839 req.target = ccb->ccb_h.target_id; 1840 req.lun = ccb->ccb_h.target_lun; 1841 1842 bus_space_write_region_1(hba->bar0t, hba->bar0h, iop_req32, 1843 (u_int8_t *)&req, req.header.size); 1844 1845 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) { 1846 bus_dmamap_sync(hba->io_dmat, 1847 srb->dma_map, BUS_DMASYNC_PREREAD); 1848 } 1849 else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) 1850 bus_dmamap_sync(hba->io_dmat, 1851 srb->dma_map, BUS_DMASYNC_PREWRITE); 1852 1853 BUS_SPACE_WRT4_ITL(inbound_queue,iop_req32); 1854 } else { 1855 struct hpt_iop_request_scsi_command *req; 1856 1857 req = (struct hpt_iop_request_scsi_command *)srb; 1858 if (ccb->csio.dxfer_len && nsegs > 0) { 1859 struct hpt_iopsg *psg = req->sg_list; 1860 for (idx = 0; idx < nsegs; idx++, psg++) { 1861 psg->pci_address = 1862 (u_int64_t)segs[idx].ds_addr; 1863 psg->size = segs[idx].ds_len; 1864 psg->eot = 0; 1865 } 1866 psg[-1].eot = 1; 1867 } 1868 1869 bcopy(cdb, req->cdb, ccb->csio.cdb_len); 1870 1871 req->header.type = IOP_REQUEST_TYPE_SCSI_COMMAND; 1872 req->header.result = IOP_RESULT_PENDING; 1873 req->dataxfer_length = ccb->csio.dxfer_len; 1874 req->channel = 0; 1875 req->target = ccb->ccb_h.target_id; 1876 req->lun = ccb->ccb_h.target_lun; 1877 req->header.size = offsetof(struct hpt_iop_request_scsi_command, sg_list) 1878 + nsegs*sizeof(struct hpt_iopsg); 1879 req->header.context = (u_int64_t)srb->index | 1880 IOPMU_QUEUE_ADDR_HOST_BIT; 1881 req->header.flags = IOP_REQUEST_FLAG_OUTPUT_CONTEXT; 1882 1883 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) { 1884 bus_dmamap_sync(hba->io_dmat, 1885 srb->dma_map, BUS_DMASYNC_PREREAD); 1886 }else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) { 1887 bus_dmamap_sync(hba->io_dmat, 1888 srb->dma_map, BUS_DMASYNC_PREWRITE); 1889 } 1890 1891 if (hba->firmware_version > 0x01020000 1892 || hba->interface_version > 0x01020000) { 1893 u_int32_t size_bits; 1894 1895 if (req->header.size < 256) 1896 size_bits = IOPMU_QUEUE_REQUEST_SIZE_BIT; 1897 else if (req->header.size < 512) 1898 size_bits = IOPMU_QUEUE_ADDR_HOST_BIT; 1899 else 1900 size_bits = IOPMU_QUEUE_REQUEST_SIZE_BIT 1901 | IOPMU_QUEUE_ADDR_HOST_BIT; 1902 1903 BUS_SPACE_WRT4_ITL(inbound_queue, 1904 (u_int32_t)srb->phy_addr | size_bits); 1905 } else 1906 BUS_SPACE_WRT4_ITL(inbound_queue, (u_int32_t)srb->phy_addr 1907 |IOPMU_QUEUE_ADDR_HOST_BIT); 1908 } 1909 } 1910 1911 static void hptiop_post_req_mv(struct hpt_iop_hba *hba, 1912 struct hpt_iop_srb *srb, 1913 bus_dma_segment_t *segs, int nsegs) 1914 { 1915 int idx, size; 1916 union ccb *ccb = srb->ccb; 1917 u_int8_t *cdb; 1918 struct hpt_iop_request_scsi_command *req; 1919 u_int64_t req_phy; 1920 1921 req = (struct hpt_iop_request_scsi_command *)srb; 1922 req_phy = srb->phy_addr; 1923 1924 if (ccb->csio.dxfer_len && nsegs > 0) { 1925 struct hpt_iopsg *psg = req->sg_list; 1926 for (idx = 0; idx < nsegs; idx++, psg++) { 1927 psg->pci_address = (u_int64_t)segs[idx].ds_addr; 1928 psg->size = segs[idx].ds_len; 1929 psg->eot = 0; 1930 } 1931 psg[-1].eot = 1; 1932 } 1933 if (ccb->ccb_h.flags & CAM_CDB_POINTER) 1934 cdb = ccb->csio.cdb_io.cdb_ptr; 1935 else 1936 cdb = ccb->csio.cdb_io.cdb_bytes; 1937 1938 bcopy(cdb, req->cdb, ccb->csio.cdb_len); 1939 req->header.type = IOP_REQUEST_TYPE_SCSI_COMMAND; 1940 req->header.result = IOP_RESULT_PENDING; 1941 req->dataxfer_length = ccb->csio.dxfer_len; 1942 req->channel = 0; 1943 req->target = ccb->ccb_h.target_id; 1944 req->lun = ccb->ccb_h.target_lun; 1945 req->header.size = sizeof(struct hpt_iop_request_scsi_command) 1946 - sizeof(struct hpt_iopsg) 1947 + nsegs * sizeof(struct hpt_iopsg); 1948 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) { 1949 bus_dmamap_sync(hba->io_dmat, 1950 srb->dma_map, BUS_DMASYNC_PREREAD); 1951 } 1952 else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) 1953 bus_dmamap_sync(hba->io_dmat, 1954 srb->dma_map, BUS_DMASYNC_PREWRITE); 1955 req->header.context = (u_int64_t)srb->index 1956 << MVIOP_REQUEST_NUMBER_START_BIT 1957 | MVIOP_CMD_TYPE_SCSI; 1958 req->header.flags = IOP_REQUEST_FLAG_OUTPUT_CONTEXT; 1959 size = req->header.size >> 8; 1960 hptiop_mv_inbound_write(req_phy 1961 | MVIOP_MU_QUEUE_ADDR_HOST_BIT 1962 | (size > 3 ? 3 : size), hba); 1963 } 1964 1965 static void hptiop_post_scsi_command(void *arg, bus_dma_segment_t *segs, 1966 int nsegs, int error) 1967 { 1968 struct hpt_iop_srb *srb = (struct hpt_iop_srb *)arg; 1969 union ccb *ccb = srb->ccb; 1970 struct hpt_iop_hba *hba = srb->hba; 1971 1972 if (error || nsegs > hba->max_sg_count) { 1973 KdPrint(("hptiop: func_code=%x tid=%x lun=%x nsegs=%d\n", 1974 ccb->ccb_h.func_code, 1975 ccb->ccb_h.target_id, 1976 ccb->ccb_h.target_lun, nsegs)); 1977 ccb->ccb_h.status = CAM_BUSY; 1978 bus_dmamap_unload(hba->io_dmat, srb->dma_map); 1979 hptiop_free_srb(hba, srb); 1980 xpt_done(ccb); 1981 return; 1982 } 1983 1984 hba->ops->post_req(hba, srb, segs, nsegs); 1985 } 1986 1987 static void hptiop_mv_map_ctlcfg(void *arg, bus_dma_segment_t *segs, 1988 int nsegs, int error) 1989 { 1990 struct hpt_iop_hba *hba = (struct hpt_iop_hba *)arg; 1991 hba->ctlcfgcmd_phy = ((u_int64_t)segs->ds_addr + 0x1F) 1992 & ~(u_int64_t)0x1F; 1993 hba->ctlcfg_ptr = (u_int8_t *)(((unsigned long)hba->ctlcfg_ptr + 0x1F) 1994 & ~0x1F); 1995 } 1996 1997 static void hptiop_map_srb(void *arg, bus_dma_segment_t *segs, 1998 int nsegs, int error) 1999 { 2000 struct hpt_iop_hba * hba = (struct hpt_iop_hba *)arg; 2001 bus_addr_t phy_addr = (segs->ds_addr + 0x1F) & ~(bus_addr_t)0x1F; 2002 struct hpt_iop_srb *srb, *tmp_srb; 2003 int i; 2004 2005 if (error || nsegs == 0) { 2006 device_printf(hba->pcidev, "hptiop_map_srb error"); 2007 return; 2008 } 2009 2010 /* map srb */ 2011 srb = (struct hpt_iop_srb *) 2012 (((unsigned long)hba->uncached_ptr + 0x1F) 2013 & ~(unsigned long)0x1F); 2014 2015 for (i = 0; i < HPT_SRB_MAX_QUEUE_SIZE; i++) { 2016 tmp_srb = (struct hpt_iop_srb *) 2017 ((char *)srb + i * HPT_SRB_MAX_SIZE); 2018 if (((unsigned long)tmp_srb & 0x1F) == 0) { 2019 if (bus_dmamap_create(hba->io_dmat, 2020 0, &tmp_srb->dma_map)) { 2021 device_printf(hba->pcidev, "dmamap create failed"); 2022 return; 2023 } 2024 2025 bzero(tmp_srb, sizeof(struct hpt_iop_srb)); 2026 tmp_srb->hba = hba; 2027 tmp_srb->index = i; 2028 if (hba->ctlcfg_ptr == 0) {/*itl iop*/ 2029 tmp_srb->phy_addr = (u_int64_t)(u_int32_t) 2030 (phy_addr >> 5); 2031 if (phy_addr & IOPMU_MAX_MEM_SUPPORT_MASK_32G) 2032 tmp_srb->srb_flag = 2033 HPT_SRB_FLAG_HIGH_MEM_ACESS; 2034 } else { 2035 tmp_srb->phy_addr = phy_addr; 2036 } 2037 2038 hptiop_free_srb(hba, tmp_srb); 2039 hba->srb[i] = tmp_srb; 2040 phy_addr += HPT_SRB_MAX_SIZE; 2041 } 2042 else { 2043 device_printf(hba->pcidev, "invalid alignment"); 2044 return; 2045 } 2046 } 2047 } 2048 2049 static void hptiop_os_message_callback(struct hpt_iop_hba * hba, u_int32_t msg) 2050 { 2051 hba->msg_done = 1; 2052 } 2053 2054 static int hptiop_os_query_remove_device(struct hpt_iop_hba * hba, 2055 int target_id) 2056 { 2057 struct cam_periph *periph = NULL; 2058 struct cam_path *path; 2059 int status, retval = 0; 2060 2061 status = xpt_create_path(&path, NULL, hba->sim->path_id, target_id, 0); 2062 2063 if (status == CAM_REQ_CMP) { 2064 if ((periph = cam_periph_find(path, "da")) != NULL) { 2065 if (periph->refcount >= 1) { 2066 device_printf(hba->pcidev, "target_id=0x%x," 2067 "refcount=%d", target_id, periph->refcount); 2068 retval = -1; 2069 } 2070 } 2071 xpt_free_path(path); 2072 } 2073 return retval; 2074 } 2075 2076 static void hptiop_release_resource(struct hpt_iop_hba *hba) 2077 { 2078 int i; 2079 if (hba->path) { 2080 struct ccb_setasync ccb; 2081 2082 xpt_setup_ccb(&ccb.ccb_h, hba->path, /*priority*/5); 2083 ccb.ccb_h.func_code = XPT_SASYNC_CB; 2084 ccb.event_enable = 0; 2085 ccb.callback = hptiop_async; 2086 ccb.callback_arg = hba->sim; 2087 xpt_action((union ccb *)&ccb); 2088 xpt_free_path(hba->path); 2089 } 2090 2091 if (hba->sim) { 2092 xpt_bus_deregister(cam_sim_path(hba->sim)); 2093 cam_sim_free(hba->sim); 2094 } 2095 2096 if (hba->ctlcfg_dmat) { 2097 bus_dmamap_unload(hba->ctlcfg_dmat, hba->ctlcfg_dmamap); 2098 bus_dmamem_free(hba->ctlcfg_dmat, 2099 hba->ctlcfg_ptr, hba->ctlcfg_dmamap); 2100 bus_dma_tag_destroy(hba->ctlcfg_dmat); 2101 } 2102 2103 for (i = 0; i < HPT_SRB_MAX_QUEUE_SIZE; i++) { 2104 struct hpt_iop_srb *srb = hba->srb[i]; 2105 if (srb->dma_map) 2106 bus_dmamap_destroy(hba->io_dmat, srb->dma_map); 2107 } 2108 2109 if (hba->srb_dmat) { 2110 bus_dmamap_unload(hba->srb_dmat, hba->srb_dmamap); 2111 bus_dmamap_destroy(hba->srb_dmat, hba->srb_dmamap); 2112 bus_dma_tag_destroy(hba->srb_dmat); 2113 } 2114 2115 if (hba->io_dmat) 2116 bus_dma_tag_destroy(hba->io_dmat); 2117 2118 if (hba->parent_dmat) 2119 bus_dma_tag_destroy(hba->parent_dmat); 2120 2121 if (hba->irq_handle) 2122 bus_teardown_intr(hba->pcidev, hba->irq_res, hba->irq_handle); 2123 2124 if (hba->irq_res) 2125 bus_release_resource(hba->pcidev, SYS_RES_IRQ, 2126 0, hba->irq_res); 2127 2128 if (hba->bar0_res) 2129 bus_release_resource(hba->pcidev, SYS_RES_MEMORY, 2130 hba->bar0_rid, hba->bar0_res); 2131 if (hba->bar2_res) 2132 bus_release_resource(hba->pcidev, SYS_RES_MEMORY, 2133 hba->bar2_rid, hba->bar2_res); 2134 if (hba->ioctl_dev) 2135 destroy_dev(hba->ioctl_dev); 2136 dev_ops_remove_minor(&hptiop_ops, device_get_unit(hba->pcidev)); 2137 } 2138