2 * HighPoint RR3xxx/4xxx RAID Driver for FreeBSD
3 * Copyright (C) 2007-2008 HighPoint Technologies, Inc. All Rights Reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/types.h>
33 #if (__FreeBSD_version >= 500000)
35 #include <sys/systm.h>
37 #include <machine/clock.h>
41 #include <sys/malloc.h>
43 #include <sys/libkern.h>
44 #include <sys/kernel.h>
46 #if (__FreeBSD_version >= 500000)
47 #include <sys/kthread.h>
48 #include <sys/mutex.h>
49 #include <sys/module.h>
52 #include <sys/eventhandler.h>
54 #include <sys/taskqueue.h>
55 #include <sys/ioccom.h>
57 #include <machine/resource.h>
58 #include <machine/bus.h>
59 #include <machine/stdarg.h>
65 #if (__FreeBSD_version >= 500000)
66 #include <dev/pci/pcireg.h>
67 #include <dev/pci/pcivar.h>
69 #include <pci/pcivar.h>
70 #include <pci/pcireg.h>
73 #if (__FreeBSD_version <= 500043)
74 #include <sys/devicestat.h>
78 #include <cam/cam_ccb.h>
79 #include <cam/cam_sim.h>
80 #include <cam/cam_xpt_sim.h>
81 #include <cam/cam_debug.h>
82 #include <cam/cam_periph.h>
83 #include <cam/scsi/scsi_all.h>
84 #include <cam/scsi/scsi_message.h>
86 #if (__FreeBSD_version < 500043)
87 #include <sys/bus_private.h>
90 #include <dev/hptiop/hptiop.h>
92 static char driver_name[] = "hptiop";
93 static char driver_version[] = "v1.3 (010208)";
95 static devclass_t hptiop_devclass;
97 static int hptiop_send_sync_msg(struct hpt_iop_hba *hba,
98 u_int32_t msg, u_int32_t millisec);
99 static void hptiop_request_callback_itl(struct hpt_iop_hba *hba,
101 static void hptiop_request_callback_mv(struct hpt_iop_hba *hba, u_int64_t req);
102 static void hptiop_os_message_callback(struct hpt_iop_hba *hba, u_int32_t msg);
103 static int hptiop_do_ioctl_itl(struct hpt_iop_hba *hba,
104 struct hpt_iop_ioctl_param *pParams);
105 static int hptiop_do_ioctl_mv(struct hpt_iop_hba *hba,
106 struct hpt_iop_ioctl_param *pParams);
107 static int hptiop_rescan_bus(struct hpt_iop_hba *hba);
108 static int hptiop_alloc_pci_res_itl(struct hpt_iop_hba *hba);
109 static int hptiop_alloc_pci_res_mv(struct hpt_iop_hba *hba);
110 static int hptiop_get_config_itl(struct hpt_iop_hba *hba,
111 struct hpt_iop_request_get_config *config);
112 static int hptiop_get_config_mv(struct hpt_iop_hba *hba,
113 struct hpt_iop_request_get_config *config);
114 static int hptiop_set_config_itl(struct hpt_iop_hba *hba,
115 struct hpt_iop_request_set_config *config);
116 static int hptiop_set_config_mv(struct hpt_iop_hba *hba,
117 struct hpt_iop_request_set_config *config);
118 static int hptiop_internal_memalloc_mv(struct hpt_iop_hba *hba);
119 static int hptiop_internal_memfree_mv(struct hpt_iop_hba *hba);
120 static int hptiop_post_ioctl_command_itl(struct hpt_iop_hba *hba,
121 u_int32_t req32, struct hpt_iop_ioctl_param *pParams);
122 static int hptiop_post_ioctl_command_mv(struct hpt_iop_hba *hba,
123 struct hpt_iop_request_ioctl_command *req,
124 struct hpt_iop_ioctl_param *pParams);
125 static void hptiop_post_req_itl(struct hpt_iop_hba *hba,
126 struct hpt_iop_srb *srb,
127 bus_dma_segment_t *segs, int nsegs);
128 static void hptiop_post_req_mv(struct hpt_iop_hba *hba,
129 struct hpt_iop_srb *srb,
130 bus_dma_segment_t *segs, int nsegs);
131 static void hptiop_post_msg_itl(struct hpt_iop_hba *hba, u_int32_t msg);
132 static void hptiop_post_msg_mv(struct hpt_iop_hba *hba, u_int32_t msg);
133 static void hptiop_enable_intr_itl(struct hpt_iop_hba *hba);
134 static void hptiop_enable_intr_mv(struct hpt_iop_hba *hba);
135 static void hptiop_disable_intr_itl(struct hpt_iop_hba *hba);
136 static void hptiop_disable_intr_mv(struct hpt_iop_hba *hba);
137 static void hptiop_free_srb(struct hpt_iop_hba *hba, struct hpt_iop_srb *srb);
138 static int hptiop_os_query_remove_device(struct hpt_iop_hba *hba, int tid);
139 static int hptiop_probe(device_t dev);
140 static int hptiop_attach(device_t dev);
141 static int hptiop_detach(device_t dev);
142 static int hptiop_shutdown(device_t dev);
143 static void hptiop_action(struct cam_sim *sim, union ccb *ccb);
144 static void hptiop_poll(struct cam_sim *sim);
145 static void hptiop_async(void *callback_arg, u_int32_t code,
146 struct cam_path *path, void *arg);
147 static void hptiop_pci_intr(void *arg);
148 static void hptiop_release_resource(struct hpt_iop_hba *hba);
149 static int hptiop_reset_adapter(struct hpt_iop_hba *hba);
151 static d_open_t hptiop_open;
152 static d_close_t hptiop_close;
153 static d_ioctl_t hptiop_ioctl;
155 static struct cdevsw hptiop_cdevsw = {
156 .d_open = hptiop_open,
157 .d_close = hptiop_close,
158 .d_ioctl = hptiop_ioctl,
159 .d_name = driver_name,
160 #if __FreeBSD_version>=503000
161 .d_version = D_VERSION,
163 #if (__FreeBSD_version>=503000 && __FreeBSD_version<600034)
164 .d_flags = D_NEEDGIANT,
166 #if __FreeBSD_version<600034
167 #if __FreeBSD_version>=501000
170 .d_maj = HPT_DEV_MAJOR,
175 #if __FreeBSD_version < 503000
176 #define hba_from_dev(dev) ((struct hpt_iop_hba *)(dev)->si_drv1)
178 #define hba_from_dev(dev) \
179 ((struct hpt_iop_hba *)devclass_get_softc(hptiop_devclass, dev2unit(dev)))
182 #define BUS_SPACE_WRT4_ITL(offset, value) bus_space_write_4(hba->bar0t,\
183 hba->bar0h, offsetof(struct hpt_iopmu_itl, offset), (value))
184 #define BUS_SPACE_RD4_ITL(offset) bus_space_read_4(hba->bar0t,\
185 hba->bar0h, offsetof(struct hpt_iopmu_itl, offset))
187 #define BUS_SPACE_WRT4_MV0(offset, value) bus_space_write_4(hba->bar0t,\
188 hba->bar0h, offsetof(struct hpt_iopmv_regs, offset), value)
189 #define BUS_SPACE_RD4_MV0(offset) bus_space_read_4(hba->bar0t,\
190 hba->bar0h, offsetof(struct hpt_iopmv_regs, offset))
191 #define BUS_SPACE_WRT4_MV2(offset, value) bus_space_write_4(hba->bar2t,\
192 hba->bar2h, offsetof(struct hpt_iopmu_mv, offset), value)
193 #define BUS_SPACE_RD4_MV2(offset) bus_space_read_4(hba->bar2t,\
194 hba->bar2h, offsetof(struct hpt_iopmu_mv, offset))
196 static int hptiop_open(ioctl_dev_t dev, int flags,
197 int devtype, ioctl_thread_t proc)
199 struct hpt_iop_hba *hba = hba_from_dev(dev);
203 if (hba->flag & HPT_IOCTL_FLAG_OPEN)
205 hba->flag |= HPT_IOCTL_FLAG_OPEN;
209 static int hptiop_close(ioctl_dev_t dev, int flags,
210 int devtype, ioctl_thread_t proc)
212 struct hpt_iop_hba *hba = hba_from_dev(dev);
213 hba->flag &= ~(u_int32_t)HPT_IOCTL_FLAG_OPEN;
217 static int hptiop_ioctl(ioctl_dev_t dev, u_long cmd, caddr_t data,
218 int flags, ioctl_thread_t proc)
221 struct hpt_iop_hba *hba = hba_from_dev(dev);
223 #if (__FreeBSD_version >= 500000)
228 case HPT_DO_IOCONTROL:
229 ret = hba->ops->do_ioctl(hba,
230 (struct hpt_iop_ioctl_param *)data);
233 ret = hptiop_rescan_bus(hba);
237 #if (__FreeBSD_version >= 500000)
244 static u_int64_t hptiop_mv_outbound_read(struct hpt_iop_hba *hba)
247 u_int32_t outbound_tail = BUS_SPACE_RD4_MV2(outbound_tail);
248 u_int32_t outbound_head = BUS_SPACE_RD4_MV2(outbound_head);
250 if (outbound_tail != outbound_head) {
251 bus_space_read_region_4(hba->bar2t, hba->bar2h,
252 offsetof(struct hpt_iopmu_mv,
253 outbound_q[outbound_tail]),
258 if (outbound_tail == MVIOP_QUEUE_LEN)
261 BUS_SPACE_WRT4_MV2(outbound_tail, outbound_tail);
267 static void hptiop_mv_inbound_write(u_int64_t p, struct hpt_iop_hba *hba)
269 u_int32_t inbound_head = BUS_SPACE_RD4_MV2(inbound_head);
270 u_int32_t head = inbound_head + 1;
272 if (head == MVIOP_QUEUE_LEN)
275 bus_space_write_region_4(hba->bar2t, hba->bar2h,
276 offsetof(struct hpt_iopmu_mv, inbound_q[inbound_head]),
278 BUS_SPACE_WRT4_MV2(inbound_head, head);
279 BUS_SPACE_WRT4_MV0(inbound_doorbell, MVIOP_MU_INBOUND_INT_POSTQUEUE);
282 static void hptiop_post_msg_itl(struct hpt_iop_hba *hba, u_int32_t msg)
284 BUS_SPACE_WRT4_ITL(inbound_msgaddr0, msg);
285 BUS_SPACE_RD4_ITL(outbound_intstatus);
288 static void hptiop_post_msg_mv(struct hpt_iop_hba *hba, u_int32_t msg)
291 BUS_SPACE_WRT4_MV2(inbound_msg, msg);
292 BUS_SPACE_WRT4_MV0(inbound_doorbell, MVIOP_MU_INBOUND_INT_MSG);
294 BUS_SPACE_RD4_MV0(outbound_intmask);
297 static int hptiop_wait_ready_itl(struct hpt_iop_hba * hba, u_int32_t millisec)
302 for (i = 0; i < millisec; i++) {
303 req = BUS_SPACE_RD4_ITL(inbound_queue);
304 if (req != IOPMU_QUEUE_EMPTY)
309 if (req!=IOPMU_QUEUE_EMPTY) {
310 BUS_SPACE_WRT4_ITL(outbound_queue, req);
311 BUS_SPACE_RD4_ITL(outbound_intstatus);
318 static int hptiop_wait_ready_mv(struct hpt_iop_hba * hba, u_int32_t millisec)
320 if (hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_NOP, millisec))
326 static void hptiop_request_callback_itl(struct hpt_iop_hba * hba,
329 struct hpt_iop_srb *srb;
330 struct hpt_iop_request_scsi_command *req=0;
333 u_int32_t result, temp, dxfer;
336 if (index & IOPMU_QUEUE_MASK_HOST_BITS) { /*host req*/
337 if (hba->firmware_version > 0x01020000 ||
338 hba->interface_version > 0x01020000) {
339 srb = hba->srb[index & ~(u_int32_t)
340 (IOPMU_QUEUE_ADDR_HOST_BIT
341 | IOPMU_QUEUE_REQUEST_RESULT_BIT)];
342 req = (struct hpt_iop_request_scsi_command *)srb;
343 if (index & IOPMU_QUEUE_REQUEST_RESULT_BIT)
344 result = IOP_RESULT_SUCCESS;
346 result = req->header.result;
348 srb = hba->srb[index &
349 ~(u_int32_t)IOPMU_QUEUE_ADDR_HOST_BIT];
350 req = (struct hpt_iop_request_scsi_command *)srb;
351 result = req->header.result;
353 dxfer = req->dataxfer_length;
358 temp = bus_space_read_4(hba->bar0t, hba->bar0h, index +
359 offsetof(struct hpt_iop_request_header, type));
360 result = bus_space_read_4(hba->bar0t, hba->bar0h, index +
361 offsetof(struct hpt_iop_request_header, result));
363 case IOP_REQUEST_TYPE_IOCTL_COMMAND:
366 bus_space_write_region_4(hba->bar0t, hba->bar0h, index +
367 offsetof(struct hpt_iop_request_header, context),
368 (u_int32_t *)&temp64, 2);
369 wakeup((void *)((unsigned long)hba->u.itl.mu + index));
373 case IOP_REQUEST_TYPE_SCSI_COMMAND:
374 bus_space_read_region_4(hba->bar0t, hba->bar0h, index +
375 offsetof(struct hpt_iop_request_header, context),
376 (u_int32_t *)&temp64, 2);
377 srb = (struct hpt_iop_srb *)(unsigned long)temp64;
378 dxfer = bus_space_read_4(hba->bar0t, hba->bar0h,
379 index + offsetof(struct hpt_iop_request_scsi_command,
382 ccb = (union ccb *)srb->ccb;
383 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
384 cdb = ccb->csio.cdb_io.cdb_ptr;
386 cdb = ccb->csio.cdb_io.cdb_bytes;
388 if (cdb[0] == SYNCHRONIZE_CACHE) { /* ??? */
389 ccb->ccb_h.status = CAM_REQ_CMP;
394 case IOP_RESULT_SUCCESS:
395 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
397 bus_dmamap_sync(hba->io_dmat,
398 srb->dma_map, BUS_DMASYNC_POSTREAD);
399 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
402 bus_dmamap_sync(hba->io_dmat,
403 srb->dma_map, BUS_DMASYNC_POSTWRITE);
404 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
408 ccb->ccb_h.status = CAM_REQ_CMP;
411 case IOP_RESULT_BAD_TARGET:
412 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
414 case IOP_RESULT_BUSY:
415 ccb->ccb_h.status = CAM_BUSY;
417 case IOP_RESULT_INVALID_REQUEST:
418 ccb->ccb_h.status = CAM_REQ_INVALID;
420 case IOP_RESULT_FAIL:
421 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
423 case IOP_RESULT_RESET:
424 ccb->ccb_h.status = CAM_BUSY;
426 case IOP_RESULT_CHECK_CONDITION:
427 if (srb->srb_flag & HPT_SRB_FLAG_HIGH_MEM_ACESS) {/*iop*/
428 bus_space_read_region_1(hba->bar0t, hba->bar0h,
429 index + offsetof(struct hpt_iop_request_scsi_command,
430 sg_list), (u_int8_t *)&ccb->csio.sense_data,
431 MIN(dxfer, sizeof(ccb->csio.sense_data)));
433 memcpy(&ccb->csio.sense_data, &req->sg_list,
434 MIN(dxfer, sizeof(ccb->csio.sense_data)));
436 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
437 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
438 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
441 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
445 if (srb->srb_flag & HPT_SRB_FLAG_HIGH_MEM_ACESS)
446 BUS_SPACE_WRT4_ITL(outbound_queue, index);
448 ccb->csio.resid = ccb->csio.dxfer_len - dxfer;
450 hptiop_free_srb(hba, srb);
456 static void hptiop_drain_outbound_queue_itl(struct hpt_iop_hba *hba)
460 while ((req = BUS_SPACE_RD4_ITL(outbound_queue)) !=IOPMU_QUEUE_EMPTY) {
461 if (req & IOPMU_QUEUE_MASK_HOST_BITS)
462 hptiop_request_callback_itl(hba, req);
464 struct hpt_iop_request_header *p;
466 p = (struct hpt_iop_request_header *)
467 ((char *)hba->u.itl.mu + req);
468 temp = bus_space_read_4(hba->bar0t,
470 offsetof(struct hpt_iop_request_header,
472 if (temp & IOP_REQUEST_FLAG_SYNC_REQUEST) {
474 bus_space_read_region_4(hba->bar0t,
476 offsetof(struct hpt_iop_request_header,
478 (u_int32_t *)&temp64, 2);
480 hptiop_request_callback_itl(hba, req);
483 bus_space_write_region_4(hba->bar0t,
485 offsetof(struct hpt_iop_request_header,
487 (u_int32_t *)&temp64, 2);
490 hptiop_request_callback_itl(hba, req);
495 static int hptiop_intr_itl(struct hpt_iop_hba * hba)
500 status = BUS_SPACE_RD4_ITL(outbound_intstatus);
502 if (status & IOPMU_OUTBOUND_INT_MSG0) {
503 u_int32_t msg = BUS_SPACE_RD4_ITL(outbound_msgaddr0);
504 KdPrint(("hptiop: received outbound msg %x\n", msg));
505 BUS_SPACE_WRT4_ITL(outbound_intstatus, IOPMU_OUTBOUND_INT_MSG0);
506 hptiop_os_message_callback(hba, msg);
510 if (status & IOPMU_OUTBOUND_INT_POSTQUEUE) {
511 hptiop_drain_outbound_queue_itl(hba);
518 static void hptiop_request_callback_mv(struct hpt_iop_hba * hba,
521 u_int32_t context = (u_int32_t)_tag;
523 if (context & MVIOP_CMD_TYPE_SCSI) {
524 struct hpt_iop_srb *srb;
525 struct hpt_iop_request_scsi_command *req;
529 srb = hba->srb[context >> MVIOP_REQUEST_NUMBER_START_BIT];
530 req = (struct hpt_iop_request_scsi_command *)srb;
531 ccb = (union ccb *)srb->ccb;
532 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
533 cdb = ccb->csio.cdb_io.cdb_ptr;
535 cdb = ccb->csio.cdb_io.cdb_bytes;
537 if (cdb[0] == SYNCHRONIZE_CACHE) { /* ??? */
538 ccb->ccb_h.status = CAM_REQ_CMP;
541 if (context & MVIOP_MU_QUEUE_REQUEST_RESULT_BIT)
542 req->header.result = IOP_RESULT_SUCCESS;
544 switch (req->header.result) {
545 case IOP_RESULT_SUCCESS:
546 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
548 bus_dmamap_sync(hba->io_dmat,
549 srb->dma_map, BUS_DMASYNC_POSTREAD);
550 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
553 bus_dmamap_sync(hba->io_dmat,
554 srb->dma_map, BUS_DMASYNC_POSTWRITE);
555 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
558 ccb->ccb_h.status = CAM_REQ_CMP;
560 case IOP_RESULT_BAD_TARGET:
561 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
563 case IOP_RESULT_BUSY:
564 ccb->ccb_h.status = CAM_BUSY;
566 case IOP_RESULT_INVALID_REQUEST:
567 ccb->ccb_h.status = CAM_REQ_INVALID;
569 case IOP_RESULT_FAIL:
570 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
572 case IOP_RESULT_RESET:
573 ccb->ccb_h.status = CAM_BUSY;
575 case IOP_RESULT_CHECK_CONDITION:
576 memcpy(&ccb->csio.sense_data, &req->sg_list,
577 MIN(req->dataxfer_length, sizeof(ccb->csio.sense_data)));
578 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
579 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
580 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
583 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
587 ccb->csio.resid = ccb->csio.dxfer_len - req->dataxfer_length;
589 hptiop_free_srb(hba, srb);
591 } else if (context & MVIOP_CMD_TYPE_IOCTL) {
592 struct hpt_iop_request_ioctl_command *req = hba->ctlcfg_ptr;
593 if (context & MVIOP_MU_QUEUE_REQUEST_RESULT_BIT)
594 hba->config_done = 1;
596 hba->config_done = -1;
599 (MVIOP_CMD_TYPE_SET_CONFIG |
600 MVIOP_CMD_TYPE_GET_CONFIG))
601 hba->config_done = 1;
603 device_printf(hba->pcidev, "wrong callback type\n");
607 static void hptiop_drain_outbound_queue_mv(struct hpt_iop_hba * hba)
611 while ((req = hptiop_mv_outbound_read(hba))) {
612 if (req & MVIOP_MU_QUEUE_ADDR_HOST_BIT) {
613 if (req & MVIOP_MU_QUEUE_REQUEST_RETURN_CONTEXT) {
614 hptiop_request_callback_mv(hba, req);
620 static int hptiop_intr_mv(struct hpt_iop_hba * hba)
625 status = BUS_SPACE_RD4_MV0(outbound_doorbell);
628 BUS_SPACE_WRT4_MV0(outbound_doorbell, ~status);
630 if (status & MVIOP_MU_OUTBOUND_INT_MSG) {
631 u_int32_t msg = BUS_SPACE_RD4_MV2(outbound_msg);
632 KdPrint(("hptiop: received outbound msg %x\n", msg));
633 hptiop_os_message_callback(hba, msg);
637 if (status & MVIOP_MU_OUTBOUND_INT_POSTQUEUE) {
638 hptiop_drain_outbound_queue_mv(hba);
645 static int hptiop_send_sync_request_itl(struct hpt_iop_hba * hba,
646 u_int32_t req32, u_int32_t millisec)
651 BUS_SPACE_WRT4_ITL(inbound_queue, req32);
652 BUS_SPACE_RD4_ITL(outbound_intstatus);
654 for (i = 0; i < millisec; i++) {
655 hptiop_intr_itl(hba);
656 bus_space_read_region_4(hba->bar0t, hba->bar0h, req32 +
657 offsetof(struct hpt_iop_request_header, context),
658 (u_int32_t *)&temp64, 2);
667 static int hptiop_send_sync_request_mv(struct hpt_iop_hba *hba,
668 void *req, u_int32_t millisec)
672 hba->config_done = 0;
674 phy_addr = hba->ctlcfgcmd_phy |
675 (u_int64_t)MVIOP_MU_QUEUE_ADDR_HOST_BIT;
676 ((struct hpt_iop_request_get_config *)req)->header.flags |=
677 IOP_REQUEST_FLAG_SYNC_REQUEST |
678 IOP_REQUEST_FLAG_OUTPUT_CONTEXT;
679 hptiop_mv_inbound_write(phy_addr, hba);
680 BUS_SPACE_RD4_MV0(outbound_intmask);
682 for (i = 0; i < millisec; i++) {
684 if (hba->config_done)
691 static int hptiop_send_sync_msg(struct hpt_iop_hba *hba,
692 u_int32_t msg, u_int32_t millisec)
697 hba->ops->post_msg(hba, msg);
699 for (i=0; i<millisec; i++) {
700 hba->ops->iop_intr(hba);
706 return hba->msg_done? 0 : -1;
709 static int hptiop_get_config_itl(struct hpt_iop_hba * hba,
710 struct hpt_iop_request_get_config * config)
714 config->header.size = sizeof(struct hpt_iop_request_get_config);
715 config->header.type = IOP_REQUEST_TYPE_GET_CONFIG;
716 config->header.flags = IOP_REQUEST_FLAG_SYNC_REQUEST;
717 config->header.result = IOP_RESULT_PENDING;
718 config->header.context = 0;
720 req32 = BUS_SPACE_RD4_ITL(inbound_queue);
721 if (req32 == IOPMU_QUEUE_EMPTY)
724 bus_space_write_region_4(hba->bar0t, hba->bar0h,
725 req32, (u_int32_t *)config,
726 sizeof(struct hpt_iop_request_header) >> 2);
728 if (hptiop_send_sync_request_itl(hba, req32, 20000)) {
729 KdPrint(("hptiop: get config send cmd failed"));
733 bus_space_read_region_4(hba->bar0t, hba->bar0h,
734 req32, (u_int32_t *)config,
735 sizeof(struct hpt_iop_request_get_config) >> 2);
737 BUS_SPACE_WRT4_ITL(outbound_queue, req32);
742 static int hptiop_get_config_mv(struct hpt_iop_hba * hba,
743 struct hpt_iop_request_get_config * config)
745 struct hpt_iop_request_get_config *req;
747 if (!(req = hba->ctlcfg_ptr))
750 req->header.flags = 0;
751 req->header.type = IOP_REQUEST_TYPE_GET_CONFIG;
752 req->header.size = sizeof(struct hpt_iop_request_get_config);
753 req->header.result = IOP_RESULT_PENDING;
754 req->header.context = MVIOP_CMD_TYPE_GET_CONFIG;
756 if (hptiop_send_sync_request_mv(hba, req, 20000)) {
757 KdPrint(("hptiop: get config send cmd failed"));
765 static int hptiop_set_config_itl(struct hpt_iop_hba *hba,
766 struct hpt_iop_request_set_config *config)
770 req32 = BUS_SPACE_RD4_ITL(inbound_queue);
772 if (req32 == IOPMU_QUEUE_EMPTY)
775 config->header.size = sizeof(struct hpt_iop_request_set_config);
776 config->header.type = IOP_REQUEST_TYPE_SET_CONFIG;
777 config->header.flags = IOP_REQUEST_FLAG_SYNC_REQUEST;
778 config->header.result = IOP_RESULT_PENDING;
779 config->header.context = 0;
781 bus_space_write_region_4(hba->bar0t, hba->bar0h, req32,
783 sizeof(struct hpt_iop_request_set_config) >> 2);
785 if (hptiop_send_sync_request_itl(hba, req32, 20000)) {
786 KdPrint(("hptiop: set config send cmd failed"));
790 BUS_SPACE_WRT4_ITL(outbound_queue, req32);
795 static int hptiop_set_config_mv(struct hpt_iop_hba *hba,
796 struct hpt_iop_request_set_config *config)
798 struct hpt_iop_request_set_config *req;
800 if (!(req = hba->ctlcfg_ptr))
803 memcpy((u_int8_t *)req + sizeof(struct hpt_iop_request_header),
804 (u_int8_t *)config + sizeof(struct hpt_iop_request_header),
805 sizeof(struct hpt_iop_request_set_config) -
806 sizeof(struct hpt_iop_request_header));
808 req->header.flags = 0;
809 req->header.type = IOP_REQUEST_TYPE_SET_CONFIG;
810 req->header.size = sizeof(struct hpt_iop_request_set_config);
811 req->header.result = IOP_RESULT_PENDING;
812 req->header.context = MVIOP_CMD_TYPE_SET_CONFIG;
814 if (hptiop_send_sync_request_mv(hba, req, 20000)) {
815 KdPrint(("hptiop: set config send cmd failed"));
822 static int hptiop_post_ioctl_command_itl(struct hpt_iop_hba *hba,
824 struct hpt_iop_ioctl_param *pParams)
827 struct hpt_iop_request_ioctl_command req;
829 if ((((pParams->nInBufferSize + 3) & ~3) + pParams->nOutBufferSize) >
830 (hba->max_request_size -
831 offsetof(struct hpt_iop_request_ioctl_command, buf))) {
832 device_printf(hba->pcidev, "request size beyond max value");
836 req.header.size = offsetof(struct hpt_iop_request_ioctl_command, buf)
837 + pParams->nInBufferSize;
838 req.header.type = IOP_REQUEST_TYPE_IOCTL_COMMAND;
839 req.header.flags = IOP_REQUEST_FLAG_SYNC_REQUEST;
840 req.header.result = IOP_RESULT_PENDING;
841 req.header.context = req32 + (u_int64_t)(unsigned long)hba->u.itl.mu;
842 req.ioctl_code = HPT_CTL_CODE_BSD_TO_IOP(pParams->dwIoControlCode);
843 req.inbuf_size = pParams->nInBufferSize;
844 req.outbuf_size = pParams->nOutBufferSize;
845 req.bytes_returned = 0;
847 bus_space_write_region_4(hba->bar0t, hba->bar0h, req32, (u_int32_t *)&req,
848 offsetof(struct hpt_iop_request_ioctl_command, buf)>>2);
850 hptiop_lock_adapter(hba);
852 BUS_SPACE_WRT4_ITL(inbound_queue, req32);
853 BUS_SPACE_RD4_ITL(outbound_intstatus);
855 bus_space_read_region_4(hba->bar0t, hba->bar0h, req32 +
856 offsetof(struct hpt_iop_request_ioctl_command, header.context),
857 (u_int32_t *)&temp64, 2);
859 if (hptiop_sleep(hba, (void *)((unsigned long)hba->u.itl.mu + req32),
860 PPAUSE, "hptctl", HPT_OSM_TIMEOUT)==0)
862 hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET, 60000);
863 bus_space_read_region_4(hba->bar0t, hba->bar0h,req32 +
864 offsetof(struct hpt_iop_request_ioctl_command,
866 (u_int32_t *)&temp64, 2);
869 hptiop_unlock_adapter(hba);
873 static int hptiop_bus_space_copyin(struct hpt_iop_hba *hba, u_int32_t bus, void *user, int size)
878 for (i=0; i<size; i++) {
879 if (copyin((u_int8_t *)user + i, &byte, 1))
881 bus_space_write_1(hba->bar0t, hba->bar0h, bus + i, byte);
887 static int hptiop_bus_space_copyout(struct hpt_iop_hba *hba, u_int32_t bus, void *user, int size)
892 for (i=0; i<size; i++) {
893 byte = bus_space_read_1(hba->bar0t, hba->bar0h, bus + i);
894 if (copyout(&byte, (u_int8_t *)user + i, 1))
901 static int hptiop_do_ioctl_itl(struct hpt_iop_hba *hba,
902 struct hpt_iop_ioctl_param * pParams)
907 if ((pParams->Magic != HPT_IOCTL_MAGIC) &&
908 (pParams->Magic != HPT_IOCTL_MAGIC32))
911 req32 = BUS_SPACE_RD4_ITL(inbound_queue);
912 if (req32 == IOPMU_QUEUE_EMPTY)
915 if (pParams->nInBufferSize)
916 if (hptiop_bus_space_copyin(hba, req32 +
917 offsetof(struct hpt_iop_request_ioctl_command, buf),
918 (void *)pParams->lpInBuffer, pParams->nInBufferSize))
921 if (hptiop_post_ioctl_command_itl(hba, req32, pParams))
924 result = bus_space_read_4(hba->bar0t, hba->bar0h, req32 +
925 offsetof(struct hpt_iop_request_ioctl_command,
928 if (result == IOP_RESULT_SUCCESS) {
929 if (pParams->nOutBufferSize)
930 if (hptiop_bus_space_copyout(hba, req32 +
931 offsetof(struct hpt_iop_request_ioctl_command, buf) +
932 ((pParams->nInBufferSize + 3) & ~3),
933 (void *)pParams->lpOutBuffer, pParams->nOutBufferSize))
936 if (pParams->lpBytesReturned) {
937 if (hptiop_bus_space_copyout(hba, req32 +
938 offsetof(struct hpt_iop_request_ioctl_command, bytes_returned),
939 (void *)pParams->lpBytesReturned, sizeof(unsigned long)))
943 BUS_SPACE_WRT4_ITL(outbound_queue, req32);
948 BUS_SPACE_WRT4_ITL(outbound_queue, req32);
954 static int hptiop_post_ioctl_command_mv(struct hpt_iop_hba *hba,
955 struct hpt_iop_request_ioctl_command *req,
956 struct hpt_iop_ioctl_param *pParams)
961 if ((((pParams->nInBufferSize + 3) & ~3) + pParams->nOutBufferSize) >
962 (hba->max_request_size -
963 offsetof(struct hpt_iop_request_ioctl_command, buf))) {
964 device_printf(hba->pcidev, "request size beyond max value");
968 req->ioctl_code = HPT_CTL_CODE_BSD_TO_IOP(pParams->dwIoControlCode);
969 req->inbuf_size = pParams->nInBufferSize;
970 req->outbuf_size = pParams->nOutBufferSize;
971 req->header.size = offsetof(struct hpt_iop_request_ioctl_command, buf)
972 + pParams->nInBufferSize;
973 req->header.context = (u_int64_t)MVIOP_CMD_TYPE_IOCTL;
974 req->header.type = IOP_REQUEST_TYPE_IOCTL_COMMAND;
975 req->header.result = IOP_RESULT_PENDING;
976 req->header.flags = IOP_REQUEST_FLAG_OUTPUT_CONTEXT;
977 size = req->header.size >> 8;
978 size = size > 3 ? 3 : size;
979 req_phy = hba->ctlcfgcmd_phy | MVIOP_MU_QUEUE_ADDR_HOST_BIT | size;
980 hptiop_mv_inbound_write(req_phy, hba);
982 BUS_SPACE_RD4_MV0(outbound_intmask);
984 while (hba->config_done == 0) {
985 if (hptiop_sleep(hba, req, PPAUSE,
986 "hptctl", HPT_OSM_TIMEOUT)==0)
988 hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET, 60000);
993 static int hptiop_do_ioctl_mv(struct hpt_iop_hba *hba,
994 struct hpt_iop_ioctl_param *pParams)
996 struct hpt_iop_request_ioctl_command *req;
998 if ((pParams->Magic != HPT_IOCTL_MAGIC) &&
999 (pParams->Magic != HPT_IOCTL_MAGIC32))
1002 req = (struct hpt_iop_request_ioctl_command *)(hba->ctlcfg_ptr);
1003 hba->config_done = 0;
1004 hptiop_lock_adapter(hba);
1005 if (pParams->nInBufferSize)
1006 if (copyin((void *)pParams->lpInBuffer,
1007 req->buf, pParams->nInBufferSize))
1009 if (hptiop_post_ioctl_command_mv(hba, req, pParams))
1012 if (hba->config_done == 1) {
1013 if (pParams->nOutBufferSize)
1014 if (copyout(req->buf +
1015 ((pParams->nInBufferSize + 3) & ~3),
1016 (void *)pParams->lpOutBuffer,
1017 pParams->nOutBufferSize))
1020 if (pParams->lpBytesReturned)
1021 if (copyout(&req->bytes_returned,
1022 (void*)pParams->lpBytesReturned,
1025 hptiop_unlock_adapter(hba);
1029 hptiop_unlock_adapter(hba);
1034 static int hptiop_rescan_bus(struct hpt_iop_hba * hba)
1038 if ((ccb = xpt_alloc_ccb()) == NULL)
1040 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, cam_sim_path(hba->sim),
1041 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1049 static bus_dmamap_callback_t hptiop_map_srb;
1050 static bus_dmamap_callback_t hptiop_post_scsi_command;
1051 static bus_dmamap_callback_t hptiop_mv_map_ctlcfg;
1053 static int hptiop_alloc_pci_res_itl(struct hpt_iop_hba *hba)
1055 hba->bar0_rid = 0x10;
1056 hba->bar0_res = bus_alloc_resource_any(hba->pcidev,
1057 SYS_RES_MEMORY, &hba->bar0_rid, RF_ACTIVE);
1059 if (hba->bar0_res == NULL) {
1060 device_printf(hba->pcidev,
1061 "failed to get iop base adrress.\n");
1064 hba->bar0t = rman_get_bustag(hba->bar0_res);
1065 hba->bar0h = rman_get_bushandle(hba->bar0_res);
1066 hba->u.itl.mu = (struct hpt_iopmu_itl *)
1067 rman_get_virtual(hba->bar0_res);
1069 if (!hba->u.itl.mu) {
1070 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1071 hba->bar0_rid, hba->bar0_res);
1072 device_printf(hba->pcidev, "alloc mem res failed\n");
1079 static int hptiop_alloc_pci_res_mv(struct hpt_iop_hba *hba)
1081 hba->bar0_rid = 0x10;
1082 hba->bar0_res = bus_alloc_resource_any(hba->pcidev,
1083 SYS_RES_MEMORY, &hba->bar0_rid, RF_ACTIVE);
1085 if (hba->bar0_res == NULL) {
1086 device_printf(hba->pcidev, "failed to get iop bar0.\n");
1089 hba->bar0t = rman_get_bustag(hba->bar0_res);
1090 hba->bar0h = rman_get_bushandle(hba->bar0_res);
1091 hba->u.mv.regs = (struct hpt_iopmv_regs *)
1092 rman_get_virtual(hba->bar0_res);
1094 if (!hba->u.mv.regs) {
1095 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1096 hba->bar0_rid, hba->bar0_res);
1097 device_printf(hba->pcidev, "alloc bar0 mem res failed\n");
1101 hba->bar2_rid = 0x18;
1102 hba->bar2_res = bus_alloc_resource_any(hba->pcidev,
1103 SYS_RES_MEMORY, &hba->bar2_rid, RF_ACTIVE);
1105 if (hba->bar2_res == NULL) {
1106 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1107 hba->bar0_rid, hba->bar0_res);
1108 device_printf(hba->pcidev, "failed to get iop bar2.\n");
1112 hba->bar2t = rman_get_bustag(hba->bar2_res);
1113 hba->bar2h = rman_get_bushandle(hba->bar2_res);
1114 hba->u.mv.mu = (struct hpt_iopmu_mv *)rman_get_virtual(hba->bar2_res);
1116 if (!hba->u.mv.mu) {
1117 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1118 hba->bar0_rid, hba->bar0_res);
1119 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1120 hba->bar2_rid, hba->bar2_res);
1121 device_printf(hba->pcidev, "alloc mem bar2 res failed\n");
1128 static void hptiop_release_pci_res_itl(struct hpt_iop_hba *hba)
1131 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1132 hba->bar0_rid, hba->bar0_res);
1135 static void hptiop_release_pci_res_mv(struct hpt_iop_hba *hba)
1138 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1139 hba->bar0_rid, hba->bar0_res);
1141 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1142 hba->bar2_rid, hba->bar2_res);
1145 static int hptiop_internal_memalloc_mv(struct hpt_iop_hba *hba)
1147 if (bus_dma_tag_create(hba->parent_dmat,
1150 BUS_SPACE_MAXADDR_32BIT,
1155 BUS_SPACE_MAXSIZE_32BIT,
1157 #if __FreeBSD_version > 502000
1161 &hba->ctlcfg_dmat)) {
1162 device_printf(hba->pcidev, "alloc ctlcfg_dmat failed\n");
1166 if (bus_dmamem_alloc(hba->ctlcfg_dmat, (void **)&hba->ctlcfg_ptr,
1167 #if __FreeBSD_version>501000
1168 BUS_DMA_WAITOK | BUS_DMA_COHERENT,
1172 &hba->ctlcfg_dmamap) != 0) {
1173 device_printf(hba->pcidev,
1174 "bus_dmamem_alloc failed!\n");
1175 bus_dma_tag_destroy(hba->ctlcfg_dmat);
1179 if (bus_dmamap_load(hba->ctlcfg_dmat,
1180 hba->ctlcfg_dmamap, hba->ctlcfg_ptr,
1181 MVIOP_IOCTLCFG_SIZE,
1182 hptiop_mv_map_ctlcfg, hba, 0)) {
1183 device_printf(hba->pcidev, "bus_dmamap_load failed!\n");
1184 if (hba->ctlcfg_dmat)
1185 bus_dmamem_free(hba->ctlcfg_dmat,
1186 hba->ctlcfg_ptr, hba->ctlcfg_dmamap);
1187 bus_dma_tag_destroy(hba->ctlcfg_dmat);
1194 static int hptiop_internal_memfree_mv(struct hpt_iop_hba *hba)
1196 if (hba->ctlcfg_dmat) {
1197 bus_dmamap_unload(hba->ctlcfg_dmat, hba->ctlcfg_dmamap);
1198 bus_dmamem_free(hba->ctlcfg_dmat,
1199 hba->ctlcfg_ptr, hba->ctlcfg_dmamap);
1200 bus_dma_tag_destroy(hba->ctlcfg_dmat);
1207 * CAM driver interface
1209 static device_method_t driver_methods[] = {
1210 /* Device interface */
1211 DEVMETHOD(device_probe, hptiop_probe),
1212 DEVMETHOD(device_attach, hptiop_attach),
1213 DEVMETHOD(device_detach, hptiop_detach),
1214 DEVMETHOD(device_shutdown, hptiop_shutdown),
1218 static struct hptiop_adapter_ops hptiop_itl_ops = {
1219 .iop_wait_ready = hptiop_wait_ready_itl,
1220 .internal_memalloc = 0,
1221 .internal_memfree = 0,
1222 .alloc_pci_res = hptiop_alloc_pci_res_itl,
1223 .release_pci_res = hptiop_release_pci_res_itl,
1224 .enable_intr = hptiop_enable_intr_itl,
1225 .disable_intr = hptiop_disable_intr_itl,
1226 .get_config = hptiop_get_config_itl,
1227 .set_config = hptiop_set_config_itl,
1228 .iop_intr = hptiop_intr_itl,
1229 .post_msg = hptiop_post_msg_itl,
1230 .post_req = hptiop_post_req_itl,
1231 .do_ioctl = hptiop_do_ioctl_itl,
1234 static struct hptiop_adapter_ops hptiop_mv_ops = {
1235 .iop_wait_ready = hptiop_wait_ready_mv,
1236 .internal_memalloc = hptiop_internal_memalloc_mv,
1237 .internal_memfree = hptiop_internal_memfree_mv,
1238 .alloc_pci_res = hptiop_alloc_pci_res_mv,
1239 .release_pci_res = hptiop_release_pci_res_mv,
1240 .enable_intr = hptiop_enable_intr_mv,
1241 .disable_intr = hptiop_disable_intr_mv,
1242 .get_config = hptiop_get_config_mv,
1243 .set_config = hptiop_set_config_mv,
1244 .iop_intr = hptiop_intr_mv,
1245 .post_msg = hptiop_post_msg_mv,
1246 .post_req = hptiop_post_req_mv,
1247 .do_ioctl = hptiop_do_ioctl_mv,
1250 static driver_t hptiop_pci_driver = {
1253 sizeof(struct hpt_iop_hba)
1256 DRIVER_MODULE(hptiop, pci, hptiop_pci_driver, hptiop_devclass, 0, 0);
1258 static int hptiop_probe(device_t dev)
1260 struct hpt_iop_hba *hba;
1262 static char buf[256];
1264 struct hptiop_adapter_ops *ops;
1266 if (pci_get_vendor(dev) != 0x1103)
1269 id = pci_get_device(dev);
1285 ops = &hptiop_itl_ops;
1290 ops = &hptiop_mv_ops;
1296 device_printf(dev, "adapter at PCI %d:%d:%d, IRQ %d\n",
1297 pci_get_bus(dev), pci_get_slot(dev),
1298 pci_get_function(dev), pci_get_irq(dev));
1300 sprintf(buf, "RocketRAID %x %s Controller\n",
1301 id, sas ? "SAS" : "SATA");
1302 device_set_desc_copy(dev, buf);
1304 hba = (struct hpt_iop_hba *)device_get_softc(dev);
1305 bzero(hba, sizeof(struct hpt_iop_hba));
1308 KdPrint(("hba->ops=%p\n", hba->ops));
1312 static int hptiop_attach(device_t dev)
1314 struct hpt_iop_hba *hba = (struct hpt_iop_hba *)device_get_softc(dev);
1315 struct hpt_iop_request_get_config iop_config;
1316 struct hpt_iop_request_set_config set_config;
1318 struct cam_devq *devq;
1319 struct ccb_setasync ccb;
1320 u_int32_t unit = device_get_unit(dev);
1322 device_printf(dev, "%d RocketRAID 3xxx/4xxx controller driver %s\n",
1323 unit, driver_version);
1325 KdPrint(("hptiop: attach(%d, %d/%d/%d) ops=%p\n", unit,
1326 pci_get_bus(dev), pci_get_slot(dev),
1327 pci_get_function(dev), hba->ops));
1329 #if __FreeBSD_version >=440000
1330 pci_enable_busmaster(dev);
1333 hba->pciunit = unit;
1335 if (hba->ops->alloc_pci_res(hba))
1338 if (hba->ops->iop_wait_ready(hba, 2000)) {
1339 device_printf(dev, "adapter is not ready\n");
1340 goto release_pci_res;
1343 #if (__FreeBSD_version >= 500000)
1344 mtx_init(&hba->lock, "hptioplock", NULL, MTX_DEF);
1347 if (bus_dma_tag_create(NULL,/* parent */
1350 BUS_SPACE_MAXADDR, /* lowaddr */
1351 BUS_SPACE_MAXADDR, /* highaddr */
1352 NULL, NULL, /* filter, filterarg */
1353 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
1354 BUS_SPACE_UNRESTRICTED, /* nsegments */
1355 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1357 #if __FreeBSD_version>502000
1358 NULL, /* lockfunc */
1359 NULL, /* lockfuncarg */
1361 &hba->parent_dmat /* tag */))
1363 device_printf(dev, "alloc parent_dmat failed\n");
1364 goto release_pci_res;
1367 if (hba->ops->internal_memalloc) {
1368 if (hba->ops->internal_memalloc(hba)) {
1369 device_printf(dev, "alloc srb_dmat failed\n");
1370 goto destroy_parent_tag;
1374 if (hba->ops->get_config(hba, &iop_config)) {
1375 device_printf(dev, "get iop config failed.\n");
1376 goto get_config_failed;
1379 hba->firmware_version = iop_config.firmware_version;
1380 hba->interface_version = iop_config.interface_version;
1381 hba->max_requests = iop_config.max_requests;
1382 hba->max_devices = iop_config.max_devices;
1383 hba->max_request_size = iop_config.request_size;
1384 hba->max_sg_count = iop_config.max_sg_count;
1386 if (bus_dma_tag_create(hba->parent_dmat,/* parent */
1388 BUS_SPACE_MAXADDR_32BIT+1, /* boundary */
1389 BUS_SPACE_MAXADDR, /* lowaddr */
1390 BUS_SPACE_MAXADDR, /* highaddr */
1391 NULL, NULL, /* filter, filterarg */
1392 PAGE_SIZE * (hba->max_sg_count-1), /* maxsize */
1393 hba->max_sg_count, /* nsegments */
1394 0x20000, /* maxsegsize */
1395 BUS_DMA_ALLOCNOW, /* flags */
1396 #if __FreeBSD_version>502000
1397 busdma_lock_mutex, /* lockfunc */
1398 &hba->lock, /* lockfuncarg */
1400 &hba->io_dmat /* tag */))
1402 device_printf(dev, "alloc io_dmat failed\n");
1403 goto get_config_failed;
1406 if (bus_dma_tag_create(hba->parent_dmat,/* parent */
1409 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1410 BUS_SPACE_MAXADDR, /* highaddr */
1411 NULL, NULL, /* filter, filterarg */
1412 HPT_SRB_MAX_SIZE * HPT_SRB_MAX_QUEUE_SIZE + 0x20,
1414 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1416 #if __FreeBSD_version>502000
1417 NULL, /* lockfunc */
1418 NULL, /* lockfuncarg */
1420 &hba->srb_dmat /* tag */))
1422 device_printf(dev, "alloc srb_dmat failed\n");
1423 goto destroy_io_dmat;
1426 if (bus_dmamem_alloc(hba->srb_dmat, (void **)&hba->uncached_ptr,
1427 #if __FreeBSD_version>501000
1428 BUS_DMA_WAITOK | BUS_DMA_COHERENT,
1432 &hba->srb_dmamap) != 0)
1434 device_printf(dev, "srb bus_dmamem_alloc failed!\n");
1435 goto destroy_srb_dmat;
1438 if (bus_dmamap_load(hba->srb_dmat,
1439 hba->srb_dmamap, hba->uncached_ptr,
1440 (HPT_SRB_MAX_SIZE * HPT_SRB_MAX_QUEUE_SIZE) + 0x20,
1441 hptiop_map_srb, hba, 0))
1443 device_printf(dev, "bus_dmamap_load failed!\n");
1444 goto srb_dmamem_free;
1447 if ((devq = cam_simq_alloc(hba->max_requests - 1 )) == NULL) {
1448 device_printf(dev, "cam_simq_alloc failed\n");
1449 goto srb_dmamap_unload;
1452 #if __FreeBSD_version <700000
1453 hba->sim = cam_sim_alloc(hptiop_action, hptiop_poll, driver_name,
1454 hba, unit, hba->max_requests - 1, 1, devq);
1456 hba->sim = cam_sim_alloc(hptiop_action, hptiop_poll, driver_name,
1457 hba, unit, &Giant, hba->max_requests - 1, 1, devq);
1460 device_printf(dev, "cam_sim_alloc failed\n");
1461 cam_simq_free(devq);
1462 goto srb_dmamap_unload;
1464 #if __FreeBSD_version <700000
1465 if (xpt_bus_register(hba->sim, 0) != CAM_SUCCESS)
1467 if (xpt_bus_register(hba->sim, dev, 0) != CAM_SUCCESS)
1470 device_printf(dev, "xpt_bus_register failed\n");
1474 if (xpt_create_path(&hba->path, /*periph */ NULL,
1475 cam_sim_path(hba->sim), CAM_TARGET_WILDCARD,
1476 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1477 device_printf(dev, "xpt_create_path failed\n");
1478 goto deregister_xpt_bus;
1481 bzero(&set_config, sizeof(set_config));
1482 set_config.iop_id = unit;
1483 set_config.vbus_id = cam_sim_path(hba->sim);
1484 set_config.max_host_request_size = HPT_SRB_MAX_REQ_SIZE;
1486 if (hba->ops->set_config(hba, &set_config)) {
1487 device_printf(dev, "set iop config failed.\n");
1491 xpt_setup_ccb(&ccb.ccb_h, hba->path, /*priority*/5);
1492 ccb.ccb_h.func_code = XPT_SASYNC_CB;
1493 ccb.event_enable = (AC_FOUND_DEVICE | AC_LOST_DEVICE);
1494 ccb.callback = hptiop_async;
1495 ccb.callback_arg = hba->sim;
1496 xpt_action((union ccb *)&ccb);
1499 if ((hba->irq_res = bus_alloc_resource(hba->pcidev, SYS_RES_IRQ,
1500 &rid, 0, ~0ul, 1, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
1501 device_printf(dev, "allocate irq failed!\n");
1505 #if __FreeBSD_version <700000
1506 if (bus_setup_intr(hba->pcidev, hba->irq_res, INTR_TYPE_CAM,
1507 hptiop_pci_intr, hba, &hba->irq_handle))
1509 if (bus_setup_intr(hba->pcidev, hba->irq_res, INTR_TYPE_CAM,
1510 NULL, hptiop_pci_intr, hba, &hba->irq_handle))
1513 device_printf(dev, "allocate intr function failed!\n");
1514 goto free_irq_resource;
1517 if (hptiop_send_sync_msg(hba,
1518 IOPMU_INBOUND_MSG0_START_BACKGROUND_TASK, 5000)) {
1519 device_printf(dev, "fail to start background task\n");
1520 goto teartown_irq_resource;
1523 hba->ops->enable_intr(hba);
1525 hba->ioctl_dev = make_dev(&hptiop_cdevsw, unit,
1526 UID_ROOT, GID_WHEEL /*GID_OPERATOR*/,
1527 S_IRUSR | S_IWUSR, "%s%d", driver_name, unit);
1529 #if __FreeBSD_version < 503000
1530 hba->ioctl_dev->si_drv1 = hba;
1536 teartown_irq_resource:
1537 bus_teardown_intr(dev, hba->irq_res, hba->irq_handle);
1540 bus_release_resource(dev, SYS_RES_IRQ, 0, hba->irq_res);
1543 xpt_free_path(hba->path);
1546 xpt_bus_deregister(cam_sim_path(hba->sim));
1549 cam_sim_free(hba->sim, /*free devq*/ TRUE);
1552 if (hba->uncached_ptr)
1553 bus_dmamap_unload(hba->srb_dmat, hba->srb_dmamap);
1556 if (hba->uncached_ptr)
1557 bus_dmamem_free(hba->srb_dmat,
1558 hba->uncached_ptr, hba->srb_dmamap);
1562 bus_dma_tag_destroy(hba->srb_dmat);
1566 bus_dma_tag_destroy(hba->io_dmat);
1569 if (hba->ops->internal_memfree)
1570 hba->ops->internal_memfree(hba);
1573 if (hba->parent_dmat)
1574 bus_dma_tag_destroy(hba->parent_dmat);
1577 if (hba->ops->release_pci_res)
1578 hba->ops->release_pci_res(hba);
1583 static int hptiop_detach(device_t dev)
1585 struct hpt_iop_hba * hba = (struct hpt_iop_hba *)device_get_softc(dev);
1589 hptiop_lock_adapter(hba);
1590 for (i = 0; i < hba->max_devices; i++)
1591 if (hptiop_os_query_remove_device(hba, i)) {
1592 device_printf(dev, "%d file system is busy. id=%d",
1597 if ((error = hptiop_shutdown(dev)) != 0)
1599 if (hptiop_send_sync_msg(hba,
1600 IOPMU_INBOUND_MSG0_STOP_BACKGROUND_TASK, 60000))
1603 hptiop_release_resource(hba);
1606 hptiop_unlock_adapter(hba);
1610 static int hptiop_shutdown(device_t dev)
1612 struct hpt_iop_hba * hba = (struct hpt_iop_hba *)device_get_softc(dev);
1616 if (hba->flag & HPT_IOCTL_FLAG_OPEN) {
1617 device_printf(dev, "%d device is busy", hba->pciunit);
1621 hba->ops->disable_intr(hba);
1623 if (hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_SHUTDOWN, 60000))
1629 static void hptiop_pci_intr(void *arg)
1631 struct hpt_iop_hba * hba = (struct hpt_iop_hba *)arg;
1632 hptiop_lock_adapter(hba);
1633 hba->ops->iop_intr(hba);
1634 hptiop_unlock_adapter(hba);
1637 static void hptiop_poll(struct cam_sim *sim)
1639 hptiop_pci_intr(cam_sim_softc(sim));
1642 static void hptiop_async(void * callback_arg, u_int32_t code,
1643 struct cam_path * path, void * arg)
1647 static void hptiop_enable_intr_itl(struct hpt_iop_hba *hba)
1649 BUS_SPACE_WRT4_ITL(outbound_intmask,
1650 ~(IOPMU_OUTBOUND_INT_POSTQUEUE | IOPMU_OUTBOUND_INT_MSG0));
1653 static void hptiop_enable_intr_mv(struct hpt_iop_hba *hba)
1657 int_mask = BUS_SPACE_RD4_MV0(outbound_intmask);
1659 int_mask |= MVIOP_MU_OUTBOUND_INT_POSTQUEUE
1660 | MVIOP_MU_OUTBOUND_INT_MSG;
1661 BUS_SPACE_WRT4_MV0(outbound_intmask,int_mask);
1664 static void hptiop_disable_intr_itl(struct hpt_iop_hba *hba)
1668 int_mask = BUS_SPACE_RD4_ITL(outbound_intmask);
1670 int_mask |= IOPMU_OUTBOUND_INT_POSTQUEUE | IOPMU_OUTBOUND_INT_MSG0;
1671 BUS_SPACE_WRT4_ITL(outbound_intmask, int_mask);
1672 BUS_SPACE_RD4_ITL(outbound_intstatus);
1675 static void hptiop_disable_intr_mv(struct hpt_iop_hba *hba)
1678 int_mask = BUS_SPACE_RD4_MV0(outbound_intmask);
1680 int_mask &= ~(MVIOP_MU_OUTBOUND_INT_MSG
1681 | MVIOP_MU_OUTBOUND_INT_POSTQUEUE);
1682 BUS_SPACE_WRT4_MV0(outbound_intmask,int_mask);
1683 BUS_SPACE_RD4_MV0(outbound_intmask);
1686 static int hptiop_reset_adapter(struct hpt_iop_hba * hba)
1688 return hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET, 60000);
1691 static void *hptiop_get_srb(struct hpt_iop_hba * hba)
1693 struct hpt_iop_srb * srb;
1695 if (hba->srb_list) {
1696 srb = hba->srb_list;
1697 hba->srb_list = srb->next;
1704 static void hptiop_free_srb(struct hpt_iop_hba *hba, struct hpt_iop_srb *srb)
1706 srb->next = hba->srb_list;
1707 hba->srb_list = srb;
1710 static void hptiop_action(struct cam_sim *sim, union ccb *ccb)
1712 struct hpt_iop_hba * hba = (struct hpt_iop_hba *)cam_sim_softc(sim);
1713 struct hpt_iop_srb * srb;
1715 switch (ccb->ccb_h.func_code) {
1718 hptiop_lock_adapter(hba);
1719 if (ccb->ccb_h.target_lun != 0 ||
1720 ccb->ccb_h.target_id >= hba->max_devices ||
1721 (ccb->ccb_h.flags & CAM_CDB_PHYS))
1723 ccb->ccb_h.status = CAM_TID_INVALID;
1728 if ((srb = hptiop_get_srb(hba)) == NULL) {
1729 device_printf(hba->pcidev, "srb allocated failed");
1730 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1737 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
1738 hptiop_post_scsi_command(srb, NULL, 0, 0);
1739 else if ((ccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
1740 if ((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0) {
1743 error = bus_dmamap_load(hba->io_dmat,
1746 ccb->csio.dxfer_len,
1747 hptiop_post_scsi_command,
1750 if (error && error != EINPROGRESS) {
1751 device_printf(hba->pcidev,
1752 "%d bus_dmamap_load error %d",
1753 hba->pciunit, error);
1754 xpt_freeze_simq(hba->sim, 1);
1755 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1757 hptiop_free_srb(hba, srb);
1763 device_printf(hba->pcidev,
1764 "CAM_DATA_PHYS not supported");
1765 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1770 struct bus_dma_segment *segs;
1772 if ((ccb->ccb_h.flags & CAM_SG_LIST_PHYS) == 0 ||
1773 (ccb->ccb_h.flags & CAM_DATA_PHYS) != 0) {
1774 device_printf(hba->pcidev, "SCSI cmd failed");
1775 ccb->ccb_h.status=CAM_PROVIDE_FAIL;
1779 segs = (struct bus_dma_segment *)ccb->csio.data_ptr;
1780 hptiop_post_scsi_command(srb, segs,
1781 ccb->csio.sglist_cnt, 0);
1785 hptiop_unlock_adapter(hba);
1789 device_printf(hba->pcidev, "reset adapter");
1790 hptiop_lock_adapter(hba);
1792 hptiop_reset_adapter(hba);
1793 hptiop_unlock_adapter(hba);
1796 case XPT_GET_TRAN_SETTINGS:
1797 case XPT_SET_TRAN_SETTINGS:
1798 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1801 case XPT_CALC_GEOMETRY:
1802 ccb->ccg.heads = 255;
1803 ccb->ccg.secs_per_track = 63;
1804 ccb->ccg.cylinders = ccb->ccg.volume_size /
1805 (ccb->ccg.heads * ccb->ccg.secs_per_track);
1806 ccb->ccb_h.status = CAM_REQ_CMP;
1811 struct ccb_pathinq *cpi = &ccb->cpi;
1813 cpi->version_num = 1;
1814 cpi->hba_inquiry = PI_SDTR_ABLE;
1815 cpi->target_sprt = 0;
1816 cpi->hba_misc = PIM_NOBUSRESET;
1817 cpi->hba_eng_cnt = 0;
1818 cpi->max_target = hba->max_devices;
1820 cpi->unit_number = cam_sim_unit(sim);
1821 cpi->bus_id = cam_sim_bus(sim);
1822 cpi->initiator_id = hba->max_devices;
1823 cpi->base_transfer_speed = 3300;
1825 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1826 strncpy(cpi->hba_vid, "HPT ", HBA_IDLEN);
1827 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1828 cpi->transport = XPORT_SPI;
1829 cpi->transport_version = 2;
1830 cpi->protocol = PROTO_SCSI;
1831 cpi->protocol_version = SCSI_REV_2;
1832 cpi->ccb_h.status = CAM_REQ_CMP;
1837 ccb->ccb_h.status = CAM_REQ_INVALID;
1845 static void hptiop_post_req_itl(struct hpt_iop_hba *hba,
1846 struct hpt_iop_srb *srb,
1847 bus_dma_segment_t *segs, int nsegs)
1850 union ccb *ccb = srb->ccb;
1853 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
1854 cdb = ccb->csio.cdb_io.cdb_ptr;
1856 cdb = ccb->csio.cdb_io.cdb_bytes;
1858 KdPrint(("ccb=%p %x-%x-%x\n",
1859 ccb, *(u_int32_t *)cdb, *((u_int32_t *)cdb+1), *((u_int32_t *)cdb+2)));
1861 if (srb->srb_flag & HPT_SRB_FLAG_HIGH_MEM_ACESS) {
1862 u_int32_t iop_req32;
1863 struct hpt_iop_request_scsi_command req;
1865 iop_req32 = BUS_SPACE_RD4_ITL(inbound_queue);
1867 if (iop_req32 == IOPMU_QUEUE_EMPTY) {
1868 device_printf(hba->pcidev, "invaild req offset\n");
1869 ccb->ccb_h.status = CAM_BUSY;
1870 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
1871 hptiop_free_srb(hba, srb);
1876 if (ccb->csio.dxfer_len && nsegs > 0) {
1877 struct hpt_iopsg *psg = req.sg_list;
1878 for (idx = 0; idx < nsegs; idx++, psg++) {
1879 psg->pci_address = (u_int64_t)segs[idx].ds_addr;
1880 psg->size = segs[idx].ds_len;
1886 bcopy(cdb, req.cdb, ccb->csio.cdb_len);
1888 req.header.size = offsetof(struct hpt_iop_request_scsi_command, sg_list)
1889 + nsegs*sizeof(struct hpt_iopsg);
1890 req.header.type = IOP_REQUEST_TYPE_SCSI_COMMAND;
1891 req.header.flags = 0;
1892 req.header.result = IOP_RESULT_PENDING;
1893 req.header.context = (u_int64_t)(unsigned long)srb;
1894 req.dataxfer_length = ccb->csio.dxfer_len;
1896 req.target = ccb->ccb_h.target_id;
1897 req.lun = ccb->ccb_h.target_lun;
1899 bus_space_write_region_1(hba->bar0t, hba->bar0h, iop_req32,
1900 (u_int8_t *)&req, req.header.size);
1902 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1903 bus_dmamap_sync(hba->io_dmat,
1904 srb->dma_map, BUS_DMASYNC_PREREAD);
1906 else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1907 bus_dmamap_sync(hba->io_dmat,
1908 srb->dma_map, BUS_DMASYNC_PREWRITE);
1910 BUS_SPACE_WRT4_ITL(inbound_queue,iop_req32);
1912 struct hpt_iop_request_scsi_command *req;
1914 req = (struct hpt_iop_request_scsi_command *)srb;
1915 if (ccb->csio.dxfer_len && nsegs > 0) {
1916 struct hpt_iopsg *psg = req->sg_list;
1917 for (idx = 0; idx < nsegs; idx++, psg++) {
1919 (u_int64_t)segs[idx].ds_addr;
1920 psg->size = segs[idx].ds_len;
1926 bcopy(cdb, req->cdb, ccb->csio.cdb_len);
1928 req->header.type = IOP_REQUEST_TYPE_SCSI_COMMAND;
1929 req->header.result = IOP_RESULT_PENDING;
1930 req->dataxfer_length = ccb->csio.dxfer_len;
1932 req->target = ccb->ccb_h.target_id;
1933 req->lun = ccb->ccb_h.target_lun;
1934 req->header.size = offsetof(struct hpt_iop_request_scsi_command, sg_list)
1935 + nsegs*sizeof(struct hpt_iopsg);
1936 req->header.context = (u_int64_t)srb->index |
1937 IOPMU_QUEUE_ADDR_HOST_BIT;
1938 req->header.flags = IOP_REQUEST_FLAG_OUTPUT_CONTEXT;
1940 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1941 bus_dmamap_sync(hba->io_dmat,
1942 srb->dma_map, BUS_DMASYNC_PREREAD);
1943 }else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
1944 bus_dmamap_sync(hba->io_dmat,
1945 srb->dma_map, BUS_DMASYNC_PREWRITE);
1948 if (hba->firmware_version > 0x01020000
1949 || hba->interface_version > 0x01020000) {
1950 u_int32_t size_bits;
1952 if (req->header.size < 256)
1953 size_bits = IOPMU_QUEUE_REQUEST_SIZE_BIT;
1954 else if (req->header.size < 512)
1955 size_bits = IOPMU_QUEUE_ADDR_HOST_BIT;
1957 size_bits = IOPMU_QUEUE_REQUEST_SIZE_BIT
1958 | IOPMU_QUEUE_ADDR_HOST_BIT;
1960 BUS_SPACE_WRT4_ITL(inbound_queue,
1961 (u_int32_t)srb->phy_addr | size_bits);
1963 BUS_SPACE_WRT4_ITL(inbound_queue, (u_int32_t)srb->phy_addr
1964 |IOPMU_QUEUE_ADDR_HOST_BIT);
1968 static void hptiop_post_req_mv(struct hpt_iop_hba *hba,
1969 struct hpt_iop_srb *srb,
1970 bus_dma_segment_t *segs, int nsegs)
1973 union ccb *ccb = srb->ccb;
1975 struct hpt_iop_request_scsi_command *req;
1978 req = (struct hpt_iop_request_scsi_command *)srb;
1979 req_phy = srb->phy_addr;
1981 if (ccb->csio.dxfer_len && nsegs > 0) {
1982 struct hpt_iopsg *psg = req->sg_list;
1983 for (idx = 0; idx < nsegs; idx++, psg++) {
1984 psg->pci_address = (u_int64_t)segs[idx].ds_addr;
1985 psg->size = segs[idx].ds_len;
1990 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
1991 cdb = ccb->csio.cdb_io.cdb_ptr;
1993 cdb = ccb->csio.cdb_io.cdb_bytes;
1995 bcopy(cdb, req->cdb, ccb->csio.cdb_len);
1996 req->header.type = IOP_REQUEST_TYPE_SCSI_COMMAND;
1997 req->header.result = IOP_RESULT_PENDING;
1998 req->dataxfer_length = ccb->csio.dxfer_len;
2000 req->target = ccb->ccb_h.target_id;
2001 req->lun = ccb->ccb_h.target_lun;
2002 req->header.size = sizeof(struct hpt_iop_request_scsi_command)
2003 - sizeof(struct hpt_iopsg)
2004 + nsegs * sizeof(struct hpt_iopsg);
2005 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2006 bus_dmamap_sync(hba->io_dmat,
2007 srb->dma_map, BUS_DMASYNC_PREREAD);
2009 else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
2010 bus_dmamap_sync(hba->io_dmat,
2011 srb->dma_map, BUS_DMASYNC_PREWRITE);
2012 req->header.context = (u_int64_t)srb->index
2013 << MVIOP_REQUEST_NUMBER_START_BIT
2014 | MVIOP_CMD_TYPE_SCSI;
2015 req->header.flags = IOP_REQUEST_FLAG_OUTPUT_CONTEXT;
2016 size = req->header.size >> 8;
2017 hptiop_mv_inbound_write(req_phy
2018 | MVIOP_MU_QUEUE_ADDR_HOST_BIT
2019 | (size > 3 ? 3 : size), hba);
2022 static void hptiop_post_scsi_command(void *arg, bus_dma_segment_t *segs,
2023 int nsegs, int error)
2025 struct hpt_iop_srb *srb = (struct hpt_iop_srb *)arg;
2026 union ccb *ccb = srb->ccb;
2027 struct hpt_iop_hba *hba = srb->hba;
2029 if (error || nsegs > hba->max_sg_count) {
2030 KdPrint(("hptiop: func_code=%x tid=%x lun=%x nsegs=%d\n",
2031 ccb->ccb_h.func_code,
2032 ccb->ccb_h.target_id,
2033 ccb->ccb_h.target_lun, nsegs));
2034 ccb->ccb_h.status = CAM_BUSY;
2035 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
2036 hptiop_free_srb(hba, srb);
2041 hba->ops->post_req(hba, srb, segs, nsegs);
2044 static void hptiop_mv_map_ctlcfg(void *arg, bus_dma_segment_t *segs,
2045 int nsegs, int error)
2047 struct hpt_iop_hba *hba = (struct hpt_iop_hba *)arg;
2048 hba->ctlcfgcmd_phy = ((u_int64_t)segs->ds_addr + 0x1F)
2050 hba->ctlcfg_ptr = (u_int8_t *)(((unsigned long)hba->ctlcfg_ptr + 0x1F)
2054 static void hptiop_map_srb(void *arg, bus_dma_segment_t *segs,
2055 int nsegs, int error)
2057 struct hpt_iop_hba * hba = (struct hpt_iop_hba *)arg;
2058 bus_addr_t phy_addr = (segs->ds_addr + 0x1F) & ~(bus_addr_t)0x1F;
2059 struct hpt_iop_srb *srb, *tmp_srb;
2062 if (error || nsegs == 0) {
2063 device_printf(hba->pcidev, "hptiop_map_srb error");
2068 srb = (struct hpt_iop_srb *)
2069 (((unsigned long)hba->uncached_ptr + 0x1F)
2070 & ~(unsigned long)0x1F);
2072 for (i = 0; i < HPT_SRB_MAX_QUEUE_SIZE; i++) {
2073 tmp_srb = (struct hpt_iop_srb *)
2074 ((char *)srb + i * HPT_SRB_MAX_SIZE);
2075 if (((unsigned long)tmp_srb & 0x1F) == 0) {
2076 if (bus_dmamap_create(hba->io_dmat,
2077 0, &tmp_srb->dma_map)) {
2078 device_printf(hba->pcidev, "dmamap create failed");
2082 bzero(tmp_srb, sizeof(struct hpt_iop_srb));
2085 if (hba->ctlcfg_ptr == 0) {/*itl iop*/
2086 tmp_srb->phy_addr = (u_int64_t)(u_int32_t)
2088 if (phy_addr & IOPMU_MAX_MEM_SUPPORT_MASK_32G)
2090 HPT_SRB_FLAG_HIGH_MEM_ACESS;
2092 tmp_srb->phy_addr = phy_addr;
2095 hptiop_free_srb(hba, tmp_srb);
2096 hba->srb[i] = tmp_srb;
2097 phy_addr += HPT_SRB_MAX_SIZE;
2100 device_printf(hba->pcidev, "invalid alignment");
2106 static void hptiop_os_message_callback(struct hpt_iop_hba * hba, u_int32_t msg)
2111 static int hptiop_os_query_remove_device(struct hpt_iop_hba * hba,
2114 struct cam_periph *periph = NULL;
2115 struct cam_path *path;
2116 int status, retval = 0;
2118 status = xpt_create_path(&path, NULL, hba->sim->path_id, target_id, 0);
2120 if (status == CAM_REQ_CMP) {
2121 if ((periph = cam_periph_find(path, "da")) != NULL) {
2122 if (periph->refcount >= 1) {
2123 device_printf(hba->pcidev, "%d ,"
2126 hba->pciunit, target_id, periph->refcount);
2130 xpt_free_path(path);
2135 static void hptiop_release_resource(struct hpt_iop_hba *hba)
2139 struct ccb_setasync ccb;
2141 xpt_setup_ccb(&ccb.ccb_h, hba->path, /*priority*/5);
2142 ccb.ccb_h.func_code = XPT_SASYNC_CB;
2143 ccb.event_enable = 0;
2144 ccb.callback = hptiop_async;
2145 ccb.callback_arg = hba->sim;
2146 xpt_action((union ccb *)&ccb);
2147 xpt_free_path(hba->path);
2151 xpt_bus_deregister(cam_sim_path(hba->sim));
2152 cam_sim_free(hba->sim, TRUE);
2155 if (hba->ctlcfg_dmat) {
2156 bus_dmamap_unload(hba->ctlcfg_dmat, hba->ctlcfg_dmamap);
2157 bus_dmamem_free(hba->ctlcfg_dmat,
2158 hba->ctlcfg_ptr, hba->ctlcfg_dmamap);
2159 bus_dma_tag_destroy(hba->ctlcfg_dmat);
2162 for (i = 0; i < HPT_SRB_MAX_QUEUE_SIZE; i++) {
2163 struct hpt_iop_srb *srb = hba->srb[i];
2165 bus_dmamap_destroy(hba->io_dmat, srb->dma_map);
2168 if (hba->srb_dmat) {
2169 bus_dmamap_unload(hba->srb_dmat, hba->srb_dmamap);
2170 bus_dmamap_destroy(hba->srb_dmat, hba->srb_dmamap);
2171 bus_dma_tag_destroy(hba->srb_dmat);
2175 bus_dma_tag_destroy(hba->io_dmat);
2177 if (hba->parent_dmat)
2178 bus_dma_tag_destroy(hba->parent_dmat);
2180 if (hba->irq_handle)
2181 bus_teardown_intr(hba->pcidev, hba->irq_res, hba->irq_handle);
2184 bus_release_resource(hba->pcidev, SYS_RES_IRQ,
2188 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
2189 hba->bar0_rid, hba->bar0_res);
2191 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
2192 hba->bar2_rid, hba->bar2_res);
2194 destroy_dev(hba->ioctl_dev);