2 * HighPoint RR3xxx/4xxx RAID Driver for FreeBSD
3 * Copyright (C) 2007-2012 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
26 * $FreeBSD: src/sys/dev/hptiop/hptiop.c,v 1.15 2012/10/25 17:29:11 delphij Exp $
29 #include <sys/param.h>
30 #include <sys/types.h>
33 #include <sys/systm.h>
36 #include <sys/malloc.h>
38 #include <sys/libkern.h>
39 #include <sys/kernel.h>
41 #include <sys/kthread.h>
43 #include <sys/module.h>
45 #include <sys/eventhandler.h>
47 #include <sys/taskqueue.h>
48 #include <sys/device.h>
49 #include <sys/mplock2.h>
51 #include <machine/stdarg.h>
57 #include <bus/pci/pcireg.h>
58 #include <bus/pci/pcivar.h>
60 #include <bus/cam/cam.h>
61 #include <bus/cam/cam_ccb.h>
62 #include <bus/cam/cam_sim.h>
63 #include <bus/cam/cam_xpt_periph.h>
64 #include <bus/cam/cam_xpt_sim.h>
65 #include <bus/cam/cam_debug.h>
66 #include <bus/cam/cam_periph.h>
67 #include <bus/cam/scsi/scsi_all.h>
68 #include <bus/cam/scsi/scsi_message.h>
70 #include <dev/raid/hptiop/hptiop.h>
72 static const char driver_name[] = "hptiop";
73 static const char driver_version[] = "v1.8";
75 static devclass_t hptiop_devclass;
77 static int hptiop_send_sync_msg(struct hpt_iop_hba *hba,
78 u_int32_t msg, u_int32_t millisec);
79 static void hptiop_request_callback_itl(struct hpt_iop_hba *hba,
81 static void hptiop_request_callback_mv(struct hpt_iop_hba *hba, u_int64_t req);
82 static void hptiop_request_callback_mvfrey(struct hpt_iop_hba *hba,
84 static void hptiop_os_message_callback(struct hpt_iop_hba *hba, u_int32_t msg);
85 static int hptiop_do_ioctl_itl(struct hpt_iop_hba *hba,
86 struct hpt_iop_ioctl_param *pParams);
87 static int hptiop_do_ioctl_mv(struct hpt_iop_hba *hba,
88 struct hpt_iop_ioctl_param *pParams);
89 static int hptiop_do_ioctl_mvfrey(struct hpt_iop_hba *hba,
90 struct hpt_iop_ioctl_param *pParams);
91 static void hptiop_bus_scan_cb(struct cam_periph *periph, union ccb *ccb);
92 static int hptiop_rescan_bus(struct hpt_iop_hba *hba);
93 static int hptiop_alloc_pci_res_itl(struct hpt_iop_hba *hba);
94 static int hptiop_alloc_pci_res_mv(struct hpt_iop_hba *hba);
95 static int hptiop_alloc_pci_res_mvfrey(struct hpt_iop_hba *hba);
96 static int hptiop_get_config_itl(struct hpt_iop_hba *hba,
97 struct hpt_iop_request_get_config *config);
98 static int hptiop_get_config_mv(struct hpt_iop_hba *hba,
99 struct hpt_iop_request_get_config *config);
100 static int hptiop_get_config_mvfrey(struct hpt_iop_hba *hba,
101 struct hpt_iop_request_get_config *config);
102 static int hptiop_set_config_itl(struct hpt_iop_hba *hba,
103 struct hpt_iop_request_set_config *config);
104 static int hptiop_set_config_mv(struct hpt_iop_hba *hba,
105 struct hpt_iop_request_set_config *config);
106 static int hptiop_set_config_mvfrey(struct hpt_iop_hba *hba,
107 struct hpt_iop_request_set_config *config);
108 static int hptiop_internal_memalloc_mv(struct hpt_iop_hba *hba);
109 static int hptiop_internal_memalloc_mvfrey(struct hpt_iop_hba *hba);
110 static int hptiop_internal_memfree_itl(struct hpt_iop_hba *hba);
111 static int hptiop_internal_memfree_mv(struct hpt_iop_hba *hba);
112 static int hptiop_internal_memfree_mvfrey(struct hpt_iop_hba *hba);
113 static int hptiop_post_ioctl_command_itl(struct hpt_iop_hba *hba,
114 u_int32_t req32, struct hpt_iop_ioctl_param *pParams);
115 static int hptiop_post_ioctl_command_mv(struct hpt_iop_hba *hba,
116 struct hpt_iop_request_ioctl_command *req,
117 struct hpt_iop_ioctl_param *pParams);
118 static int hptiop_post_ioctl_command_mvfrey(struct hpt_iop_hba *hba,
119 struct hpt_iop_request_ioctl_command *req,
120 struct hpt_iop_ioctl_param *pParams);
121 static void hptiop_post_req_itl(struct hpt_iop_hba *hba,
122 struct hpt_iop_srb *srb,
123 bus_dma_segment_t *segs, int nsegs);
124 static void hptiop_post_req_mv(struct hpt_iop_hba *hba,
125 struct hpt_iop_srb *srb,
126 bus_dma_segment_t *segs, int nsegs);
127 static void hptiop_post_req_mvfrey(struct hpt_iop_hba *hba,
128 struct hpt_iop_srb *srb,
129 bus_dma_segment_t *segs, int nsegs);
130 static void hptiop_post_msg_itl(struct hpt_iop_hba *hba, u_int32_t msg);
131 static void hptiop_post_msg_mv(struct hpt_iop_hba *hba, u_int32_t msg);
132 static void hptiop_post_msg_mvfrey(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_enable_intr_mvfrey(struct hpt_iop_hba *hba);
136 static void hptiop_disable_intr_itl(struct hpt_iop_hba *hba);
137 static void hptiop_disable_intr_mv(struct hpt_iop_hba *hba);
138 static void hptiop_disable_intr_mvfrey(struct hpt_iop_hba *hba);
139 static void hptiop_free_srb(struct hpt_iop_hba *hba, struct hpt_iop_srb *srb);
140 static int hptiop_os_query_remove_device(struct hpt_iop_hba *hba, int tid);
141 static int hptiop_probe(device_t dev);
142 static int hptiop_attach(device_t dev);
143 static int hptiop_detach(device_t dev);
144 static int hptiop_shutdown(device_t dev);
145 static void hptiop_action(struct cam_sim *sim, union ccb *ccb);
146 static void hptiop_poll(struct cam_sim *sim);
147 static void hptiop_async(void *callback_arg, u_int32_t code,
148 struct cam_path *path, void *arg);
149 static void hptiop_pci_intr(void *arg);
150 static void hptiop_release_resource(struct hpt_iop_hba *hba);
151 static void hptiop_reset_adapter(void *argv);
152 static d_open_t hptiop_open;
153 static d_close_t hptiop_close;
154 static d_ioctl_t hptiop_ioctl;
156 static struct dev_ops hptiop_ops = {
157 { driver_name, 0, 0 },
158 .d_open = hptiop_open,
159 .d_close = hptiop_close,
160 .d_ioctl = hptiop_ioctl,
163 #define hba_from_dev(dev) ((struct hpt_iop_hba *)(dev)->si_drv1)
165 #define BUS_SPACE_WRT4_ITL(offset, value) bus_space_write_4(hba->bar0t,\
166 hba->bar0h, offsetof(struct hpt_iopmu_itl, offset), (value))
167 #define BUS_SPACE_RD4_ITL(offset) bus_space_read_4(hba->bar0t,\
168 hba->bar0h, offsetof(struct hpt_iopmu_itl, offset))
170 #define BUS_SPACE_WRT4_MV0(offset, value) bus_space_write_4(hba->bar0t,\
171 hba->bar0h, offsetof(struct hpt_iopmv_regs, offset), value)
172 #define BUS_SPACE_RD4_MV0(offset) bus_space_read_4(hba->bar0t,\
173 hba->bar0h, offsetof(struct hpt_iopmv_regs, offset))
174 #define BUS_SPACE_WRT4_MV2(offset, value) bus_space_write_4(hba->bar2t,\
175 hba->bar2h, offsetof(struct hpt_iopmu_mv, offset), value)
176 #define BUS_SPACE_RD4_MV2(offset) bus_space_read_4(hba->bar2t,\
177 hba->bar2h, offsetof(struct hpt_iopmu_mv, offset))
179 #define BUS_SPACE_WRT4_MVFREY2(offset, value) bus_space_write_4(hba->bar2t,\
180 hba->bar2h, offsetof(struct hpt_iopmu_mvfrey, offset), value)
181 #define BUS_SPACE_RD4_MVFREY2(offset) bus_space_read_4(hba->bar2t,\
182 hba->bar2h, offsetof(struct hpt_iopmu_mvfrey, offset))
184 static int hptiop_open(struct dev_open_args *ap)
186 cdev_t dev = ap->a_head.a_dev;
187 struct hpt_iop_hba *hba = hba_from_dev(dev);
191 if (hba->flag & HPT_IOCTL_FLAG_OPEN)
193 hba->flag |= HPT_IOCTL_FLAG_OPEN;
197 static int hptiop_close(struct dev_close_args *ap)
199 cdev_t dev = ap->a_head.a_dev;
200 struct hpt_iop_hba *hba = hba_from_dev(dev);
201 hba->flag &= ~(u_int32_t)HPT_IOCTL_FLAG_OPEN;
205 static int hptiop_ioctl(struct dev_ioctl_args *ap)
207 cdev_t dev = ap->a_head.a_dev;
208 u_long cmd = ap->a_cmd;
209 caddr_t data = ap->a_data;
211 struct hpt_iop_hba *hba = hba_from_dev(dev);
216 case HPT_DO_IOCONTROL:
217 ret = hba->ops->do_ioctl(hba,
218 (struct hpt_iop_ioctl_param *)data);
221 ret = hptiop_rescan_bus(hba);
230 static u_int64_t hptiop_mv_outbound_read(struct hpt_iop_hba *hba)
233 u_int32_t outbound_tail = BUS_SPACE_RD4_MV2(outbound_tail);
234 u_int32_t outbound_head = BUS_SPACE_RD4_MV2(outbound_head);
236 if (outbound_tail != outbound_head) {
237 bus_space_read_region_4(hba->bar2t, hba->bar2h,
238 offsetof(struct hpt_iopmu_mv,
239 outbound_q[outbound_tail]),
244 if (outbound_tail == MVIOP_QUEUE_LEN)
247 BUS_SPACE_WRT4_MV2(outbound_tail, outbound_tail);
253 static void hptiop_mv_inbound_write(u_int64_t p, struct hpt_iop_hba *hba)
255 u_int32_t inbound_head = BUS_SPACE_RD4_MV2(inbound_head);
256 u_int32_t head = inbound_head + 1;
258 if (head == MVIOP_QUEUE_LEN)
261 bus_space_write_region_4(hba->bar2t, hba->bar2h,
262 offsetof(struct hpt_iopmu_mv, inbound_q[inbound_head]),
264 BUS_SPACE_WRT4_MV2(inbound_head, head);
265 BUS_SPACE_WRT4_MV0(inbound_doorbell, MVIOP_MU_INBOUND_INT_POSTQUEUE);
268 static void hptiop_post_msg_itl(struct hpt_iop_hba *hba, u_int32_t msg)
270 BUS_SPACE_WRT4_ITL(inbound_msgaddr0, msg);
271 BUS_SPACE_RD4_ITL(outbound_intstatus);
274 static void hptiop_post_msg_mv(struct hpt_iop_hba *hba, u_int32_t msg)
277 BUS_SPACE_WRT4_MV2(inbound_msg, msg);
278 BUS_SPACE_WRT4_MV0(inbound_doorbell, MVIOP_MU_INBOUND_INT_MSG);
280 BUS_SPACE_RD4_MV0(outbound_intmask);
283 static void hptiop_post_msg_mvfrey(struct hpt_iop_hba *hba, u_int32_t msg)
285 BUS_SPACE_WRT4_MVFREY2(f0_to_cpu_msg_a, msg);
286 BUS_SPACE_RD4_MVFREY2(f0_to_cpu_msg_a);
289 static int hptiop_wait_ready_itl(struct hpt_iop_hba * hba, u_int32_t millisec)
294 for (i = 0; i < millisec; i++) {
295 req = BUS_SPACE_RD4_ITL(inbound_queue);
296 if (req != IOPMU_QUEUE_EMPTY)
301 if (req!=IOPMU_QUEUE_EMPTY) {
302 BUS_SPACE_WRT4_ITL(outbound_queue, req);
303 BUS_SPACE_RD4_ITL(outbound_intstatus);
310 static int hptiop_wait_ready_mv(struct hpt_iop_hba * hba, u_int32_t millisec)
312 if (hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_NOP, millisec))
318 static int hptiop_wait_ready_mvfrey(struct hpt_iop_hba * hba,
321 if (hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_NOP, millisec))
327 static void hptiop_request_callback_itl(struct hpt_iop_hba * hba,
330 struct hpt_iop_srb *srb;
331 struct hpt_iop_request_scsi_command *req=NULL;
334 u_int32_t result, temp, dxfer;
337 if (index & IOPMU_QUEUE_MASK_HOST_BITS) { /*host req*/
338 if (hba->firmware_version > 0x01020000 ||
339 hba->interface_version > 0x01020000) {
340 srb = hba->srb[index & ~(u_int32_t)
341 (IOPMU_QUEUE_ADDR_HOST_BIT
342 | IOPMU_QUEUE_REQUEST_RESULT_BIT)];
343 req = (struct hpt_iop_request_scsi_command *)srb;
344 if (index & IOPMU_QUEUE_REQUEST_RESULT_BIT)
345 result = IOP_RESULT_SUCCESS;
347 result = req->header.result;
349 srb = hba->srb[index &
350 ~(u_int32_t)IOPMU_QUEUE_ADDR_HOST_BIT];
351 req = (struct hpt_iop_request_scsi_command *)srb;
352 result = req->header.result;
354 dxfer = req->dataxfer_length;
359 temp = bus_space_read_4(hba->bar0t, hba->bar0h, index +
360 offsetof(struct hpt_iop_request_header, type));
361 result = bus_space_read_4(hba->bar0t, hba->bar0h, index +
362 offsetof(struct hpt_iop_request_header, result));
364 case IOP_REQUEST_TYPE_IOCTL_COMMAND:
367 bus_space_write_region_4(hba->bar0t, hba->bar0h, index +
368 offsetof(struct hpt_iop_request_header, context),
369 (u_int32_t *)&temp64, 2);
370 wakeup((void *)((unsigned long)hba->u.itl.mu + index));
374 case IOP_REQUEST_TYPE_SCSI_COMMAND:
375 bus_space_read_region_4(hba->bar0t, hba->bar0h, index +
376 offsetof(struct hpt_iop_request_header, context),
377 (u_int32_t *)&temp64, 2);
378 srb = (struct hpt_iop_srb *)(unsigned long)temp64;
379 dxfer = bus_space_read_4(hba->bar0t, hba->bar0h,
380 index + offsetof(struct hpt_iop_request_scsi_command,
383 ccb = (union ccb *)srb->ccb;
384 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
385 cdb = ccb->csio.cdb_io.cdb_ptr;
387 cdb = ccb->csio.cdb_io.cdb_bytes;
389 if (cdb[0] == SYNCHRONIZE_CACHE) { /* ??? */
390 ccb->ccb_h.status = CAM_REQ_CMP;
395 case IOP_RESULT_SUCCESS:
396 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
398 bus_dmamap_sync(hba->io_dmat,
399 srb->dma_map, BUS_DMASYNC_POSTREAD);
400 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
403 bus_dmamap_sync(hba->io_dmat,
404 srb->dma_map, BUS_DMASYNC_POSTWRITE);
405 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
409 ccb->ccb_h.status = CAM_REQ_CMP;
412 case IOP_RESULT_BAD_TARGET:
413 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
415 case IOP_RESULT_BUSY:
416 ccb->ccb_h.status = CAM_BUSY;
418 case IOP_RESULT_INVALID_REQUEST:
419 ccb->ccb_h.status = CAM_REQ_INVALID;
421 case IOP_RESULT_FAIL:
422 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
424 case IOP_RESULT_RESET:
425 ccb->ccb_h.status = CAM_BUSY;
427 case IOP_RESULT_CHECK_CONDITION:
428 memset(&ccb->csio.sense_data, 0,
429 sizeof(ccb->csio.sense_data));
430 if (dxfer < ccb->csio.sense_len)
431 ccb->csio.sense_resid = ccb->csio.sense_len -
434 ccb->csio.sense_resid = 0;
435 if (srb->srb_flag & HPT_SRB_FLAG_HIGH_MEM_ACESS) {/*iop*/
436 bus_space_read_region_1(hba->bar0t, hba->bar0h,
437 index + offsetof(struct hpt_iop_request_scsi_command,
438 sg_list), (u_int8_t *)&ccb->csio.sense_data,
439 MIN(dxfer, sizeof(ccb->csio.sense_data)));
441 memcpy(&ccb->csio.sense_data, &req->sg_list,
442 MIN(dxfer, sizeof(ccb->csio.sense_data)));
444 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
445 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
446 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
449 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
453 if (srb->srb_flag & HPT_SRB_FLAG_HIGH_MEM_ACESS)
454 BUS_SPACE_WRT4_ITL(outbound_queue, index);
456 ccb->csio.resid = ccb->csio.dxfer_len - dxfer;
458 hptiop_free_srb(hba, srb);
464 static void hptiop_drain_outbound_queue_itl(struct hpt_iop_hba *hba)
468 while ((req = BUS_SPACE_RD4_ITL(outbound_queue)) !=IOPMU_QUEUE_EMPTY) {
469 if (req & IOPMU_QUEUE_MASK_HOST_BITS)
470 hptiop_request_callback_itl(hba, req);
472 temp = bus_space_read_4(hba->bar0t,
474 offsetof(struct hpt_iop_request_header,
476 if (temp & IOP_REQUEST_FLAG_SYNC_REQUEST) {
478 bus_space_read_region_4(hba->bar0t,
480 offsetof(struct hpt_iop_request_header,
482 (u_int32_t *)&temp64, 2);
484 hptiop_request_callback_itl(hba, req);
487 bus_space_write_region_4(hba->bar0t,
489 offsetof(struct hpt_iop_request_header,
491 (u_int32_t *)&temp64, 2);
494 hptiop_request_callback_itl(hba, req);
499 static int hptiop_intr_itl(struct hpt_iop_hba * hba)
504 status = BUS_SPACE_RD4_ITL(outbound_intstatus);
506 if (status & IOPMU_OUTBOUND_INT_MSG0) {
507 u_int32_t msg = BUS_SPACE_RD4_ITL(outbound_msgaddr0);
508 KdPrint(("hptiop: received outbound msg %x\n", msg));
509 BUS_SPACE_WRT4_ITL(outbound_intstatus, IOPMU_OUTBOUND_INT_MSG0);
510 hptiop_os_message_callback(hba, msg);
514 if (status & IOPMU_OUTBOUND_INT_POSTQUEUE) {
515 hptiop_drain_outbound_queue_itl(hba);
522 static void hptiop_request_callback_mv(struct hpt_iop_hba * hba,
525 u_int32_t context = (u_int32_t)_tag;
527 if (context & MVIOP_CMD_TYPE_SCSI) {
528 struct hpt_iop_srb *srb;
529 struct hpt_iop_request_scsi_command *req;
533 srb = hba->srb[context >> MVIOP_REQUEST_NUMBER_START_BIT];
534 req = (struct hpt_iop_request_scsi_command *)srb;
535 ccb = (union ccb *)srb->ccb;
536 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
537 cdb = ccb->csio.cdb_io.cdb_ptr;
539 cdb = ccb->csio.cdb_io.cdb_bytes;
541 if (cdb[0] == SYNCHRONIZE_CACHE) { /* ??? */
542 ccb->ccb_h.status = CAM_REQ_CMP;
545 if (context & MVIOP_MU_QUEUE_REQUEST_RESULT_BIT)
546 req->header.result = IOP_RESULT_SUCCESS;
548 switch (req->header.result) {
549 case IOP_RESULT_SUCCESS:
550 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
552 bus_dmamap_sync(hba->io_dmat,
553 srb->dma_map, BUS_DMASYNC_POSTREAD);
554 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
557 bus_dmamap_sync(hba->io_dmat,
558 srb->dma_map, BUS_DMASYNC_POSTWRITE);
559 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
562 ccb->ccb_h.status = CAM_REQ_CMP;
564 case IOP_RESULT_BAD_TARGET:
565 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
567 case IOP_RESULT_BUSY:
568 ccb->ccb_h.status = CAM_BUSY;
570 case IOP_RESULT_INVALID_REQUEST:
571 ccb->ccb_h.status = CAM_REQ_INVALID;
573 case IOP_RESULT_FAIL:
574 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
576 case IOP_RESULT_RESET:
577 ccb->ccb_h.status = CAM_BUSY;
579 case IOP_RESULT_CHECK_CONDITION:
580 memset(&ccb->csio.sense_data, 0,
581 sizeof(ccb->csio.sense_data));
582 if (req->dataxfer_length < ccb->csio.sense_len)
583 ccb->csio.sense_resid = ccb->csio.sense_len -
584 req->dataxfer_length;
586 ccb->csio.sense_resid = 0;
587 memcpy(&ccb->csio.sense_data, &req->sg_list,
588 MIN(req->dataxfer_length, sizeof(ccb->csio.sense_data)));
589 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
590 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
591 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
594 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
598 ccb->csio.resid = ccb->csio.dxfer_len - req->dataxfer_length;
600 hptiop_free_srb(hba, srb);
602 } else if (context & MVIOP_CMD_TYPE_IOCTL) {
603 struct hpt_iop_request_ioctl_command *req = hba->ctlcfg_ptr;
604 if (context & MVIOP_MU_QUEUE_REQUEST_RESULT_BIT)
605 hba->config_done = 1;
607 hba->config_done = -1;
610 (MVIOP_CMD_TYPE_SET_CONFIG |
611 MVIOP_CMD_TYPE_GET_CONFIG))
612 hba->config_done = 1;
614 device_printf(hba->pcidev, "wrong callback type\n");
618 static void hptiop_request_callback_mvfrey(struct hpt_iop_hba * hba,
621 u_int32_t req_type = _tag & 0xf;
623 struct hpt_iop_srb *srb;
624 struct hpt_iop_request_scsi_command *req;
629 case IOP_REQUEST_TYPE_GET_CONFIG:
630 case IOP_REQUEST_TYPE_SET_CONFIG:
631 hba->config_done = 1;
634 case IOP_REQUEST_TYPE_SCSI_COMMAND:
635 srb = hba->srb[(_tag >> 4) & 0xff];
636 req = (struct hpt_iop_request_scsi_command *)srb;
638 ccb = (union ccb *)srb->ccb;
640 callout_stop(&ccb->ccb_h.timeout_ch);
642 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
643 cdb = ccb->csio.cdb_io.cdb_ptr;
645 cdb = ccb->csio.cdb_io.cdb_bytes;
647 if (cdb[0] == SYNCHRONIZE_CACHE) { /* ??? */
648 ccb->ccb_h.status = CAM_REQ_CMP;
652 if (_tag & MVFREYIOPMU_QUEUE_REQUEST_RESULT_BIT)
653 req->header.result = IOP_RESULT_SUCCESS;
655 switch (req->header.result) {
656 case IOP_RESULT_SUCCESS:
657 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
659 bus_dmamap_sync(hba->io_dmat,
660 srb->dma_map, BUS_DMASYNC_POSTREAD);
661 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
664 bus_dmamap_sync(hba->io_dmat,
665 srb->dma_map, BUS_DMASYNC_POSTWRITE);
666 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
669 ccb->ccb_h.status = CAM_REQ_CMP;
671 case IOP_RESULT_BAD_TARGET:
672 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
674 case IOP_RESULT_BUSY:
675 ccb->ccb_h.status = CAM_BUSY;
677 case IOP_RESULT_INVALID_REQUEST:
678 ccb->ccb_h.status = CAM_REQ_INVALID;
680 case IOP_RESULT_FAIL:
681 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
683 case IOP_RESULT_RESET:
684 ccb->ccb_h.status = CAM_BUSY;
686 case IOP_RESULT_CHECK_CONDITION:
687 memset(&ccb->csio.sense_data, 0,
688 sizeof(ccb->csio.sense_data));
689 if (req->dataxfer_length < ccb->csio.sense_len)
690 ccb->csio.sense_resid = ccb->csio.sense_len -
691 req->dataxfer_length;
693 ccb->csio.sense_resid = 0;
694 memcpy(&ccb->csio.sense_data, &req->sg_list,
695 MIN(req->dataxfer_length, sizeof(ccb->csio.sense_data)));
696 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
697 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
698 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
701 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
705 ccb->csio.resid = ccb->csio.dxfer_len - req->dataxfer_length;
707 hptiop_free_srb(hba, srb);
710 case IOP_REQUEST_TYPE_IOCTL_COMMAND:
711 if (_tag & MVFREYIOPMU_QUEUE_REQUEST_RESULT_BIT)
712 hba->config_done = 1;
714 hba->config_done = -1;
715 wakeup((struct hpt_iop_request_ioctl_command *)hba->ctlcfg_ptr);
718 device_printf(hba->pcidev, "wrong callback type\n");
723 static void hptiop_drain_outbound_queue_mv(struct hpt_iop_hba * hba)
727 while ((req = hptiop_mv_outbound_read(hba))) {
728 if (req & MVIOP_MU_QUEUE_ADDR_HOST_BIT) {
729 if (req & MVIOP_MU_QUEUE_REQUEST_RETURN_CONTEXT) {
730 hptiop_request_callback_mv(hba, req);
736 static int hptiop_intr_mv(struct hpt_iop_hba * hba)
741 status = BUS_SPACE_RD4_MV0(outbound_doorbell);
744 BUS_SPACE_WRT4_MV0(outbound_doorbell, ~status);
746 if (status & MVIOP_MU_OUTBOUND_INT_MSG) {
747 u_int32_t msg = BUS_SPACE_RD4_MV2(outbound_msg);
748 KdPrint(("hptiop: received outbound msg %x\n", msg));
749 hptiop_os_message_callback(hba, msg);
753 if (status & MVIOP_MU_OUTBOUND_INT_POSTQUEUE) {
754 hptiop_drain_outbound_queue_mv(hba);
761 static int hptiop_intr_mvfrey(struct hpt_iop_hba * hba)
763 u_int32_t status, _tag, cptr;
766 if (hba->initialized) {
767 BUS_SPACE_WRT4_MVFREY2(pcie_f0_int_enable, 0);
770 status = BUS_SPACE_RD4_MVFREY2(f0_doorbell);
772 BUS_SPACE_WRT4_MVFREY2(f0_doorbell, status);
773 if (status & CPU_TO_F0_DRBL_MSG_A_BIT) {
774 u_int32_t msg = BUS_SPACE_RD4_MVFREY2(cpu_to_f0_msg_a);
775 hptiop_os_message_callback(hba, msg);
780 status = BUS_SPACE_RD4_MVFREY2(isr_cause);
782 BUS_SPACE_WRT4_MVFREY2(isr_cause, status);
784 cptr = *hba->u.mvfrey.outlist_cptr & 0xff;
785 while (hba->u.mvfrey.outlist_rptr != cptr) {
786 hba->u.mvfrey.outlist_rptr++;
787 if (hba->u.mvfrey.outlist_rptr == hba->u.mvfrey.list_count) {
788 hba->u.mvfrey.outlist_rptr = 0;
791 _tag = hba->u.mvfrey.outlist[hba->u.mvfrey.outlist_rptr].val;
792 hptiop_request_callback_mvfrey(hba, _tag);
795 } while (cptr != (*hba->u.mvfrey.outlist_cptr & 0xff));
798 if (hba->initialized) {
799 BUS_SPACE_WRT4_MVFREY2(pcie_f0_int_enable, 0x1010);
805 static int hptiop_send_sync_request_itl(struct hpt_iop_hba * hba,
806 u_int32_t req32, u_int32_t millisec)
811 BUS_SPACE_WRT4_ITL(inbound_queue, req32);
812 BUS_SPACE_RD4_ITL(outbound_intstatus);
814 for (i = 0; i < millisec; i++) {
815 hptiop_intr_itl(hba);
816 bus_space_read_region_4(hba->bar0t, hba->bar0h, req32 +
817 offsetof(struct hpt_iop_request_header, context),
818 (u_int32_t *)&temp64, 2);
827 static int hptiop_send_sync_request_mv(struct hpt_iop_hba *hba,
828 void *req, u_int32_t millisec)
832 hba->config_done = 0;
834 phy_addr = hba->ctlcfgcmd_phy |
835 (u_int64_t)MVIOP_MU_QUEUE_ADDR_HOST_BIT;
836 ((struct hpt_iop_request_get_config *)req)->header.flags |=
837 IOP_REQUEST_FLAG_SYNC_REQUEST |
838 IOP_REQUEST_FLAG_OUTPUT_CONTEXT;
839 hptiop_mv_inbound_write(phy_addr, hba);
840 BUS_SPACE_RD4_MV0(outbound_intmask);
842 for (i = 0; i < millisec; i++) {
844 if (hba->config_done)
851 static int hptiop_send_sync_request_mvfrey(struct hpt_iop_hba *hba,
852 void *req, u_int32_t millisec)
856 struct hpt_iop_request_header *reqhdr = (struct hpt_iop_request_header *)req;
858 hba->config_done = 0;
860 phy_addr = hba->ctlcfgcmd_phy;
861 reqhdr->flags = IOP_REQUEST_FLAG_SYNC_REQUEST
862 | IOP_REQUEST_FLAG_OUTPUT_CONTEXT
863 | IOP_REQUEST_FLAG_ADDR_BITS
864 | ((phy_addr >> 16) & 0xffff0000);
865 reqhdr->context = ((phy_addr & 0xffffffff) << 32 )
866 | IOPMU_QUEUE_ADDR_HOST_BIT | reqhdr->type;
868 hba->u.mvfrey.inlist_wptr++;
869 index = hba->u.mvfrey.inlist_wptr & 0x3fff;
871 if (index == hba->u.mvfrey.list_count) {
873 hba->u.mvfrey.inlist_wptr &= ~0x3fff;
874 hba->u.mvfrey.inlist_wptr ^= CL_POINTER_TOGGLE;
877 hba->u.mvfrey.inlist[index].addr = phy_addr;
878 hba->u.mvfrey.inlist[index].intrfc_len = (reqhdr->size + 3) / 4;
880 BUS_SPACE_WRT4_MVFREY2(inbound_write_ptr, hba->u.mvfrey.inlist_wptr);
881 BUS_SPACE_RD4_MVFREY2(inbound_write_ptr);
883 for (i = 0; i < millisec; i++) {
884 hptiop_intr_mvfrey(hba);
885 if (hba->config_done)
892 static int hptiop_send_sync_msg(struct hpt_iop_hba *hba,
893 u_int32_t msg, u_int32_t millisec)
898 hba->ops->post_msg(hba, msg);
900 for (i=0; i<millisec; i++) {
901 hba->ops->iop_intr(hba);
907 return hba->msg_done? 0 : -1;
910 static int hptiop_get_config_itl(struct hpt_iop_hba * hba,
911 struct hpt_iop_request_get_config * config)
915 config->header.size = sizeof(struct hpt_iop_request_get_config);
916 config->header.type = IOP_REQUEST_TYPE_GET_CONFIG;
917 config->header.flags = IOP_REQUEST_FLAG_SYNC_REQUEST;
918 config->header.result = IOP_RESULT_PENDING;
919 config->header.context = 0;
921 req32 = BUS_SPACE_RD4_ITL(inbound_queue);
922 if (req32 == IOPMU_QUEUE_EMPTY)
925 bus_space_write_region_4(hba->bar0t, hba->bar0h,
926 req32, (u_int32_t *)config,
927 sizeof(struct hpt_iop_request_header) >> 2);
929 if (hptiop_send_sync_request_itl(hba, req32, 20000)) {
930 KdPrint(("hptiop: get config send cmd failed"));
934 bus_space_read_region_4(hba->bar0t, hba->bar0h,
935 req32, (u_int32_t *)config,
936 sizeof(struct hpt_iop_request_get_config) >> 2);
938 BUS_SPACE_WRT4_ITL(outbound_queue, req32);
943 static int hptiop_get_config_mv(struct hpt_iop_hba * hba,
944 struct hpt_iop_request_get_config * config)
946 struct hpt_iop_request_get_config *req;
948 if (!(req = hba->ctlcfg_ptr))
951 req->header.flags = 0;
952 req->header.type = IOP_REQUEST_TYPE_GET_CONFIG;
953 req->header.size = sizeof(struct hpt_iop_request_get_config);
954 req->header.result = IOP_RESULT_PENDING;
955 req->header.context = MVIOP_CMD_TYPE_GET_CONFIG;
957 if (hptiop_send_sync_request_mv(hba, req, 20000)) {
958 KdPrint(("hptiop: get config send cmd failed"));
966 static int hptiop_get_config_mvfrey(struct hpt_iop_hba * hba,
967 struct hpt_iop_request_get_config * config)
969 struct hpt_iop_request_get_config *info = hba->u.mvfrey.config;
971 if (info->header.size != sizeof(struct hpt_iop_request_get_config) ||
972 info->header.type != IOP_REQUEST_TYPE_GET_CONFIG) {
973 KdPrint(("hptiop: header size %x/%x type %x/%x",
974 info->header.size, (int)sizeof(struct hpt_iop_request_get_config),
975 info->header.type, IOP_REQUEST_TYPE_GET_CONFIG));
979 config->interface_version = info->interface_version;
980 config->firmware_version = info->firmware_version;
981 config->max_requests = info->max_requests;
982 config->request_size = info->request_size;
983 config->max_sg_count = info->max_sg_count;
984 config->data_transfer_length = info->data_transfer_length;
985 config->alignment_mask = info->alignment_mask;
986 config->max_devices = info->max_devices;
987 config->sdram_size = info->sdram_size;
989 KdPrint(("hptiop: maxreq %x reqsz %x datalen %x maxdev %x sdram %x",
990 config->max_requests, config->request_size,
991 config->data_transfer_length, config->max_devices,
992 config->sdram_size));
997 static int hptiop_set_config_itl(struct hpt_iop_hba *hba,
998 struct hpt_iop_request_set_config *config)
1002 req32 = BUS_SPACE_RD4_ITL(inbound_queue);
1004 if (req32 == IOPMU_QUEUE_EMPTY)
1007 config->header.size = sizeof(struct hpt_iop_request_set_config);
1008 config->header.type = IOP_REQUEST_TYPE_SET_CONFIG;
1009 config->header.flags = IOP_REQUEST_FLAG_SYNC_REQUEST;
1010 config->header.result = IOP_RESULT_PENDING;
1011 config->header.context = 0;
1013 bus_space_write_region_4(hba->bar0t, hba->bar0h, req32,
1014 (u_int32_t *)config,
1015 sizeof(struct hpt_iop_request_set_config) >> 2);
1017 if (hptiop_send_sync_request_itl(hba, req32, 20000)) {
1018 KdPrint(("hptiop: set config send cmd failed"));
1022 BUS_SPACE_WRT4_ITL(outbound_queue, req32);
1027 static int hptiop_set_config_mv(struct hpt_iop_hba *hba,
1028 struct hpt_iop_request_set_config *config)
1030 struct hpt_iop_request_set_config *req;
1032 if (!(req = hba->ctlcfg_ptr))
1035 memcpy((u_int8_t *)req + sizeof(struct hpt_iop_request_header),
1036 (u_int8_t *)config + sizeof(struct hpt_iop_request_header),
1037 sizeof(struct hpt_iop_request_set_config) -
1038 sizeof(struct hpt_iop_request_header));
1040 req->header.flags = 0;
1041 req->header.type = IOP_REQUEST_TYPE_SET_CONFIG;
1042 req->header.size = sizeof(struct hpt_iop_request_set_config);
1043 req->header.result = IOP_RESULT_PENDING;
1044 req->header.context = MVIOP_CMD_TYPE_SET_CONFIG;
1046 if (hptiop_send_sync_request_mv(hba, req, 20000)) {
1047 KdPrint(("hptiop: set config send cmd failed"));
1054 static int hptiop_set_config_mvfrey(struct hpt_iop_hba *hba,
1055 struct hpt_iop_request_set_config *config)
1057 struct hpt_iop_request_set_config *req;
1059 if (!(req = hba->ctlcfg_ptr))
1062 memcpy((u_int8_t *)req + sizeof(struct hpt_iop_request_header),
1063 (u_int8_t *)config + sizeof(struct hpt_iop_request_header),
1064 sizeof(struct hpt_iop_request_set_config) -
1065 sizeof(struct hpt_iop_request_header));
1067 req->header.type = IOP_REQUEST_TYPE_SET_CONFIG;
1068 req->header.size = sizeof(struct hpt_iop_request_set_config);
1069 req->header.result = IOP_RESULT_PENDING;
1071 if (hptiop_send_sync_request_mvfrey(hba, req, 20000)) {
1072 KdPrint(("hptiop: set config send cmd failed"));
1079 static int hptiop_post_ioctl_command_itl(struct hpt_iop_hba *hba,
1081 struct hpt_iop_ioctl_param *pParams)
1084 struct hpt_iop_request_ioctl_command req;
1086 if ((((pParams->nInBufferSize + 3) & ~3) + pParams->nOutBufferSize) >
1087 (hba->max_request_size -
1088 offsetof(struct hpt_iop_request_ioctl_command, buf))) {
1089 device_printf(hba->pcidev, "request size beyond max value");
1093 req.header.size = offsetof(struct hpt_iop_request_ioctl_command, buf)
1094 + pParams->nInBufferSize;
1095 req.header.type = IOP_REQUEST_TYPE_IOCTL_COMMAND;
1096 req.header.flags = IOP_REQUEST_FLAG_SYNC_REQUEST;
1097 req.header.result = IOP_RESULT_PENDING;
1098 req.header.context = req32 + (u_int64_t)(unsigned long)hba->u.itl.mu;
1099 req.ioctl_code = HPT_CTL_CODE_BSD_TO_IOP(pParams->dwIoControlCode);
1100 req.inbuf_size = pParams->nInBufferSize;
1101 req.outbuf_size = pParams->nOutBufferSize;
1102 req.bytes_returned = 0;
1104 bus_space_write_region_4(hba->bar0t, hba->bar0h, req32, (u_int32_t *)&req,
1105 offsetof(struct hpt_iop_request_ioctl_command, buf)>>2);
1107 hptiop_lock_adapter(hba);
1109 BUS_SPACE_WRT4_ITL(inbound_queue, req32);
1110 BUS_SPACE_RD4_ITL(outbound_intstatus);
1112 bus_space_read_region_4(hba->bar0t, hba->bar0h, req32 +
1113 offsetof(struct hpt_iop_request_ioctl_command, header.context),
1114 (u_int32_t *)&temp64, 2);
1116 if (hptiop_sleep(hba, (void *)((unsigned long)hba->u.itl.mu + req32),
1117 0, "hptctl", HPT_OSM_TIMEOUT)==0)
1119 hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET, 60000);
1120 bus_space_read_region_4(hba->bar0t, hba->bar0h,req32 +
1121 offsetof(struct hpt_iop_request_ioctl_command,
1123 (u_int32_t *)&temp64, 2);
1126 hptiop_unlock_adapter(hba);
1130 static int hptiop_bus_space_copyin(struct hpt_iop_hba *hba, u_int32_t bus,
1131 void *user, int size)
1136 for (i=0; i<size; i++) {
1137 if (copyin((u_int8_t *)user + i, &byte, 1))
1139 bus_space_write_1(hba->bar0t, hba->bar0h, bus + i, byte);
1145 static int hptiop_bus_space_copyout(struct hpt_iop_hba *hba, u_int32_t bus,
1146 void *user, int size)
1151 for (i=0; i<size; i++) {
1152 byte = bus_space_read_1(hba->bar0t, hba->bar0h, bus + i);
1153 if (copyout(&byte, (u_int8_t *)user + i, 1))
1160 static int hptiop_do_ioctl_itl(struct hpt_iop_hba *hba,
1161 struct hpt_iop_ioctl_param * pParams)
1166 if ((pParams->Magic != HPT_IOCTL_MAGIC) &&
1167 (pParams->Magic != HPT_IOCTL_MAGIC32))
1170 req32 = BUS_SPACE_RD4_ITL(inbound_queue);
1171 if (req32 == IOPMU_QUEUE_EMPTY)
1174 if (pParams->nInBufferSize)
1175 if (hptiop_bus_space_copyin(hba, req32 +
1176 offsetof(struct hpt_iop_request_ioctl_command, buf),
1177 (void *)pParams->lpInBuffer, pParams->nInBufferSize))
1180 if (hptiop_post_ioctl_command_itl(hba, req32, pParams))
1183 result = bus_space_read_4(hba->bar0t, hba->bar0h, req32 +
1184 offsetof(struct hpt_iop_request_ioctl_command,
1187 if (result == IOP_RESULT_SUCCESS) {
1188 if (pParams->nOutBufferSize)
1189 if (hptiop_bus_space_copyout(hba, req32 +
1190 offsetof(struct hpt_iop_request_ioctl_command, buf) +
1191 ((pParams->nInBufferSize + 3) & ~3),
1192 (void *)pParams->lpOutBuffer, pParams->nOutBufferSize))
1195 if (pParams->lpBytesReturned) {
1196 if (hptiop_bus_space_copyout(hba, req32 +
1197 offsetof(struct hpt_iop_request_ioctl_command, bytes_returned),
1198 (void *)pParams->lpBytesReturned, sizeof(unsigned long)))
1202 BUS_SPACE_WRT4_ITL(outbound_queue, req32);
1207 BUS_SPACE_WRT4_ITL(outbound_queue, req32);
1213 static int hptiop_post_ioctl_command_mv(struct hpt_iop_hba *hba,
1214 struct hpt_iop_request_ioctl_command *req,
1215 struct hpt_iop_ioctl_param *pParams)
1220 if ((((pParams->nInBufferSize + 3) & ~3) + pParams->nOutBufferSize) >
1221 (hba->max_request_size -
1222 offsetof(struct hpt_iop_request_ioctl_command, buf))) {
1223 device_printf(hba->pcidev, "request size beyond max value");
1227 req->ioctl_code = HPT_CTL_CODE_BSD_TO_IOP(pParams->dwIoControlCode);
1228 req->inbuf_size = pParams->nInBufferSize;
1229 req->outbuf_size = pParams->nOutBufferSize;
1230 req->header.size = offsetof(struct hpt_iop_request_ioctl_command, buf)
1231 + pParams->nInBufferSize;
1232 req->header.context = (u_int64_t)MVIOP_CMD_TYPE_IOCTL;
1233 req->header.type = IOP_REQUEST_TYPE_IOCTL_COMMAND;
1234 req->header.result = IOP_RESULT_PENDING;
1235 req->header.flags = IOP_REQUEST_FLAG_OUTPUT_CONTEXT;
1236 size = req->header.size >> 8;
1237 size = size > 3 ? 3 : size;
1238 req_phy = hba->ctlcfgcmd_phy | MVIOP_MU_QUEUE_ADDR_HOST_BIT | size;
1239 hptiop_mv_inbound_write(req_phy, hba);
1241 BUS_SPACE_RD4_MV0(outbound_intmask);
1243 while (hba->config_done == 0) {
1244 if (hptiop_sleep(hba, req, 0,
1245 "hptctl", HPT_OSM_TIMEOUT)==0)
1247 hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET, 60000);
1252 static int hptiop_do_ioctl_mv(struct hpt_iop_hba *hba,
1253 struct hpt_iop_ioctl_param *pParams)
1255 struct hpt_iop_request_ioctl_command *req;
1257 if ((pParams->Magic != HPT_IOCTL_MAGIC) &&
1258 (pParams->Magic != HPT_IOCTL_MAGIC32))
1261 req = (struct hpt_iop_request_ioctl_command *)(hba->ctlcfg_ptr);
1262 hba->config_done = 0;
1263 hptiop_lock_adapter(hba);
1264 if (pParams->nInBufferSize)
1265 if (copyin((void *)pParams->lpInBuffer,
1266 req->buf, pParams->nInBufferSize))
1268 if (hptiop_post_ioctl_command_mv(hba, req, pParams))
1271 if (hba->config_done == 1) {
1272 if (pParams->nOutBufferSize)
1273 if (copyout(req->buf +
1274 ((pParams->nInBufferSize + 3) & ~3),
1275 (void *)pParams->lpOutBuffer,
1276 pParams->nOutBufferSize))
1279 if (pParams->lpBytesReturned)
1280 if (copyout(&req->bytes_returned,
1281 (void*)pParams->lpBytesReturned,
1284 hptiop_unlock_adapter(hba);
1288 hptiop_unlock_adapter(hba);
1293 static int hptiop_post_ioctl_command_mvfrey(struct hpt_iop_hba *hba,
1294 struct hpt_iop_request_ioctl_command *req,
1295 struct hpt_iop_ioctl_param *pParams)
1300 phy_addr = hba->ctlcfgcmd_phy;
1302 if ((((pParams->nInBufferSize + 3) & ~3) + pParams->nOutBufferSize) >
1303 (hba->max_request_size -
1304 offsetof(struct hpt_iop_request_ioctl_command, buf))) {
1305 device_printf(hba->pcidev, "request size beyond max value");
1309 req->ioctl_code = HPT_CTL_CODE_BSD_TO_IOP(pParams->dwIoControlCode);
1310 req->inbuf_size = pParams->nInBufferSize;
1311 req->outbuf_size = pParams->nOutBufferSize;
1312 req->header.size = offsetof(struct hpt_iop_request_ioctl_command, buf)
1313 + pParams->nInBufferSize;
1315 req->header.type = IOP_REQUEST_TYPE_IOCTL_COMMAND;
1316 req->header.result = IOP_RESULT_PENDING;
1318 req->header.flags = IOP_REQUEST_FLAG_SYNC_REQUEST
1319 | IOP_REQUEST_FLAG_OUTPUT_CONTEXT
1320 | IOP_REQUEST_FLAG_ADDR_BITS
1321 | ((phy_addr >> 16) & 0xffff0000);
1322 req->header.context = ((phy_addr & 0xffffffff) << 32 )
1323 | IOPMU_QUEUE_ADDR_HOST_BIT | req->header.type;
1325 hba->u.mvfrey.inlist_wptr++;
1326 index = hba->u.mvfrey.inlist_wptr & 0x3fff;
1328 if (index == hba->u.mvfrey.list_count) {
1330 hba->u.mvfrey.inlist_wptr &= ~0x3fff;
1331 hba->u.mvfrey.inlist_wptr ^= CL_POINTER_TOGGLE;
1334 hba->u.mvfrey.inlist[index].addr = phy_addr;
1335 hba->u.mvfrey.inlist[index].intrfc_len = (req->header.size + 3) / 4;
1337 BUS_SPACE_WRT4_MVFREY2(inbound_write_ptr, hba->u.mvfrey.inlist_wptr);
1338 BUS_SPACE_RD4_MVFREY2(inbound_write_ptr);
1340 while (hba->config_done == 0) {
1341 if (hptiop_sleep(hba, req, 0, "hptctl", HPT_OSM_TIMEOUT) == 0)
1343 hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET, 60000);
1348 static int hptiop_do_ioctl_mvfrey(struct hpt_iop_hba *hba,
1349 struct hpt_iop_ioctl_param *pParams)
1351 struct hpt_iop_request_ioctl_command *req;
1353 if ((pParams->Magic != HPT_IOCTL_MAGIC) &&
1354 (pParams->Magic != HPT_IOCTL_MAGIC32))
1357 req = (struct hpt_iop_request_ioctl_command *)(hba->ctlcfg_ptr);
1358 hba->config_done = 0;
1359 hptiop_lock_adapter(hba);
1360 if (pParams->nInBufferSize)
1361 if (copyin((void *)pParams->lpInBuffer,
1362 req->buf, pParams->nInBufferSize))
1364 if (hptiop_post_ioctl_command_mvfrey(hba, req, pParams))
1367 if (hba->config_done == 1) {
1368 if (pParams->nOutBufferSize)
1369 if (copyout(req->buf +
1370 ((pParams->nInBufferSize + 3) & ~3),
1371 (void *)pParams->lpOutBuffer,
1372 pParams->nOutBufferSize))
1375 if (pParams->lpBytesReturned)
1376 if (copyout(&req->bytes_returned,
1377 (void*)pParams->lpBytesReturned,
1380 hptiop_unlock_adapter(hba);
1384 hptiop_unlock_adapter(hba);
1389 static int hptiop_rescan_bus(struct hpt_iop_hba * hba)
1393 if ((ccb = xpt_alloc_ccb()) == NULL)
1395 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, cam_sim_path(hba->sim),
1396 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1401 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, 5/*priority (low)*/);
1402 ccb->ccb_h.func_code = XPT_SCAN_BUS;
1403 ccb->ccb_h.cbfcnp = hptiop_bus_scan_cb;
1404 ccb->crcn.flags = CAM_FLAG_NONE;
1409 static void hptiop_bus_scan_cb(struct cam_periph *periph, union ccb *ccb)
1411 xpt_free_path(ccb->ccb_h.path);
1415 static bus_dmamap_callback_t hptiop_map_srb;
1416 static bus_dmamap_callback_t hptiop_post_scsi_command;
1417 static bus_dmamap_callback_t hptiop_mv_map_ctlcfg;
1418 static bus_dmamap_callback_t hptiop_mvfrey_map_ctlcfg;
1420 static int hptiop_alloc_pci_res_itl(struct hpt_iop_hba *hba)
1422 hba->bar0_rid = 0x10;
1423 hba->bar0_res = bus_alloc_resource_any(hba->pcidev,
1424 SYS_RES_MEMORY, &hba->bar0_rid, RF_ACTIVE);
1426 if (hba->bar0_res == NULL) {
1427 device_printf(hba->pcidev,
1428 "failed to get iop base adrress.\n");
1431 hba->bar0t = rman_get_bustag(hba->bar0_res);
1432 hba->bar0h = rman_get_bushandle(hba->bar0_res);
1433 hba->u.itl.mu = (struct hpt_iopmu_itl *)
1434 rman_get_virtual(hba->bar0_res);
1436 if (!hba->u.itl.mu) {
1437 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1438 hba->bar0_rid, hba->bar0_res);
1439 device_printf(hba->pcidev, "alloc mem res failed\n");
1446 static int hptiop_alloc_pci_res_mv(struct hpt_iop_hba *hba)
1448 hba->bar0_rid = 0x10;
1449 hba->bar0_res = bus_alloc_resource_any(hba->pcidev,
1450 SYS_RES_MEMORY, &hba->bar0_rid, RF_ACTIVE);
1452 if (hba->bar0_res == NULL) {
1453 device_printf(hba->pcidev, "failed to get iop bar0.\n");
1456 hba->bar0t = rman_get_bustag(hba->bar0_res);
1457 hba->bar0h = rman_get_bushandle(hba->bar0_res);
1458 hba->u.mv.regs = (struct hpt_iopmv_regs *)
1459 rman_get_virtual(hba->bar0_res);
1461 if (!hba->u.mv.regs) {
1462 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1463 hba->bar0_rid, hba->bar0_res);
1464 device_printf(hba->pcidev, "alloc bar0 mem res failed\n");
1468 hba->bar2_rid = 0x18;
1469 hba->bar2_res = bus_alloc_resource_any(hba->pcidev,
1470 SYS_RES_MEMORY, &hba->bar2_rid, RF_ACTIVE);
1472 if (hba->bar2_res == NULL) {
1473 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1474 hba->bar0_rid, hba->bar0_res);
1475 device_printf(hba->pcidev, "failed to get iop bar2.\n");
1479 hba->bar2t = rman_get_bustag(hba->bar2_res);
1480 hba->bar2h = rman_get_bushandle(hba->bar2_res);
1481 hba->u.mv.mu = (struct hpt_iopmu_mv *)rman_get_virtual(hba->bar2_res);
1483 if (!hba->u.mv.mu) {
1484 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1485 hba->bar0_rid, hba->bar0_res);
1486 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1487 hba->bar2_rid, hba->bar2_res);
1488 device_printf(hba->pcidev, "alloc mem bar2 res failed\n");
1495 static int hptiop_alloc_pci_res_mvfrey(struct hpt_iop_hba *hba)
1497 hba->bar0_rid = 0x10;
1498 hba->bar0_res = bus_alloc_resource_any(hba->pcidev,
1499 SYS_RES_MEMORY, &hba->bar0_rid, RF_ACTIVE);
1501 if (hba->bar0_res == NULL) {
1502 device_printf(hba->pcidev, "failed to get iop bar0.\n");
1505 hba->bar0t = rman_get_bustag(hba->bar0_res);
1506 hba->bar0h = rman_get_bushandle(hba->bar0_res);
1507 hba->u.mvfrey.config = (struct hpt_iop_request_get_config *)
1508 rman_get_virtual(hba->bar0_res);
1510 if (!hba->u.mvfrey.config) {
1511 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1512 hba->bar0_rid, hba->bar0_res);
1513 device_printf(hba->pcidev, "alloc bar0 mem res failed\n");
1517 hba->bar2_rid = 0x18;
1518 hba->bar2_res = bus_alloc_resource_any(hba->pcidev,
1519 SYS_RES_MEMORY, &hba->bar2_rid, RF_ACTIVE);
1521 if (hba->bar2_res == NULL) {
1522 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1523 hba->bar0_rid, hba->bar0_res);
1524 device_printf(hba->pcidev, "failed to get iop bar2.\n");
1528 hba->bar2t = rman_get_bustag(hba->bar2_res);
1529 hba->bar2h = rman_get_bushandle(hba->bar2_res);
1531 (struct hpt_iopmu_mvfrey *)rman_get_virtual(hba->bar2_res);
1533 if (!hba->u.mvfrey.mu) {
1534 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1535 hba->bar0_rid, hba->bar0_res);
1536 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1537 hba->bar2_rid, hba->bar2_res);
1538 device_printf(hba->pcidev, "alloc mem bar2 res failed\n");
1545 static void hptiop_release_pci_res_itl(struct hpt_iop_hba *hba)
1548 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1549 hba->bar0_rid, hba->bar0_res);
1552 static void hptiop_release_pci_res_mv(struct hpt_iop_hba *hba)
1555 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1556 hba->bar0_rid, hba->bar0_res);
1558 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1559 hba->bar2_rid, hba->bar2_res);
1562 static void hptiop_release_pci_res_mvfrey(struct hpt_iop_hba *hba)
1565 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1566 hba->bar0_rid, hba->bar0_res);
1568 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1569 hba->bar2_rid, hba->bar2_res);
1572 static int hptiop_internal_memalloc_mv(struct hpt_iop_hba *hba)
1574 if (bus_dma_tag_create(hba->parent_dmat,
1577 BUS_SPACE_MAXADDR_32BIT,
1582 BUS_SPACE_MAXSIZE_32BIT,
1584 &hba->ctlcfg_dmat)) {
1585 device_printf(hba->pcidev, "alloc ctlcfg_dmat failed\n");
1589 if (bus_dmamem_alloc(hba->ctlcfg_dmat, (void **)&hba->ctlcfg_ptr,
1590 BUS_DMA_WAITOK | BUS_DMA_COHERENT,
1591 &hba->ctlcfg_dmamap) != 0) {
1592 device_printf(hba->pcidev,
1593 "bus_dmamem_alloc failed!\n");
1594 bus_dma_tag_destroy(hba->ctlcfg_dmat);
1598 if (bus_dmamap_load(hba->ctlcfg_dmat,
1599 hba->ctlcfg_dmamap, hba->ctlcfg_ptr,
1600 MVIOP_IOCTLCFG_SIZE,
1601 hptiop_mv_map_ctlcfg, hba, 0)) {
1602 device_printf(hba->pcidev, "bus_dmamap_load failed!\n");
1603 if (hba->ctlcfg_dmat) {
1604 bus_dmamem_free(hba->ctlcfg_dmat,
1605 hba->ctlcfg_ptr, hba->ctlcfg_dmamap);
1606 bus_dma_tag_destroy(hba->ctlcfg_dmat);
1614 static int hptiop_internal_memalloc_mvfrey(struct hpt_iop_hba *hba)
1616 u_int32_t list_count = BUS_SPACE_RD4_MVFREY2(inbound_conf_ctl);
1620 if (list_count == 0) {
1624 hba->u.mvfrey.list_count = list_count;
1625 hba->u.mvfrey.internal_mem_size = 0x800
1626 + list_count * sizeof(struct mvfrey_inlist_entry)
1627 + list_count * sizeof(struct mvfrey_outlist_entry)
1629 if (bus_dma_tag_create(hba->parent_dmat,
1632 BUS_SPACE_MAXADDR_32BIT,
1635 hba->u.mvfrey.internal_mem_size,
1637 BUS_SPACE_MAXSIZE_32BIT,
1639 &hba->ctlcfg_dmat)) {
1640 device_printf(hba->pcidev, "alloc ctlcfg_dmat failed\n");
1644 if (bus_dmamem_alloc(hba->ctlcfg_dmat, (void **)&hba->ctlcfg_ptr,
1645 BUS_DMA_WAITOK | BUS_DMA_COHERENT,
1646 &hba->ctlcfg_dmamap) != 0) {
1647 device_printf(hba->pcidev,
1648 "bus_dmamem_alloc failed!\n");
1649 bus_dma_tag_destroy(hba->ctlcfg_dmat);
1653 if (bus_dmamap_load(hba->ctlcfg_dmat,
1654 hba->ctlcfg_dmamap, hba->ctlcfg_ptr,
1655 hba->u.mvfrey.internal_mem_size,
1656 hptiop_mvfrey_map_ctlcfg, hba, 0)) {
1657 device_printf(hba->pcidev, "bus_dmamap_load failed!\n");
1658 if (hba->ctlcfg_dmat)
1659 bus_dmamem_free(hba->ctlcfg_dmat,
1660 hba->ctlcfg_ptr, hba->ctlcfg_dmamap);
1661 bus_dma_tag_destroy(hba->ctlcfg_dmat);
1668 static int hptiop_internal_memfree_itl(struct hpt_iop_hba *hba) {
1672 static int hptiop_internal_memfree_mv(struct hpt_iop_hba *hba)
1674 if (hba->ctlcfg_dmat) {
1675 bus_dmamap_unload(hba->ctlcfg_dmat, hba->ctlcfg_dmamap);
1676 bus_dmamem_free(hba->ctlcfg_dmat,
1677 hba->ctlcfg_ptr, hba->ctlcfg_dmamap);
1678 bus_dma_tag_destroy(hba->ctlcfg_dmat);
1684 static int hptiop_internal_memfree_mvfrey(struct hpt_iop_hba *hba)
1686 if (hba->ctlcfg_dmat) {
1687 bus_dmamap_unload(hba->ctlcfg_dmat, hba->ctlcfg_dmamap);
1688 bus_dmamem_free(hba->ctlcfg_dmat,
1689 hba->ctlcfg_ptr, hba->ctlcfg_dmamap);
1690 bus_dma_tag_destroy(hba->ctlcfg_dmat);
1696 static int hptiop_reset_comm_mvfrey(struct hpt_iop_hba *hba)
1700 if (hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET_COMM, 3000))
1703 /* wait 100ms for MCU ready */
1708 BUS_SPACE_WRT4_MVFREY2(inbound_base,
1709 hba->u.mvfrey.inlist_phy & 0xffffffff);
1710 BUS_SPACE_WRT4_MVFREY2(inbound_base_high,
1711 (hba->u.mvfrey.inlist_phy >> 16) >> 16);
1713 BUS_SPACE_WRT4_MVFREY2(outbound_base,
1714 hba->u.mvfrey.outlist_phy & 0xffffffff);
1715 BUS_SPACE_WRT4_MVFREY2(outbound_base_high,
1716 (hba->u.mvfrey.outlist_phy >> 16) >> 16);
1718 BUS_SPACE_WRT4_MVFREY2(outbound_shadow_base,
1719 hba->u.mvfrey.outlist_cptr_phy & 0xffffffff);
1720 BUS_SPACE_WRT4_MVFREY2(outbound_shadow_base_high,
1721 (hba->u.mvfrey.outlist_cptr_phy >> 16) >> 16);
1723 hba->u.mvfrey.inlist_wptr = (hba->u.mvfrey.list_count - 1)
1724 | CL_POINTER_TOGGLE;
1725 *hba->u.mvfrey.outlist_cptr = (hba->u.mvfrey.list_count - 1)
1726 | CL_POINTER_TOGGLE;
1727 hba->u.mvfrey.outlist_rptr = hba->u.mvfrey.list_count - 1;
1733 * CAM driver interface
1735 static device_method_t driver_methods[] = {
1736 /* Device interface */
1737 DEVMETHOD(device_probe, hptiop_probe),
1738 DEVMETHOD(device_attach, hptiop_attach),
1739 DEVMETHOD(device_detach, hptiop_detach),
1740 DEVMETHOD(device_shutdown, hptiop_shutdown),
1744 static struct hptiop_adapter_ops hptiop_itl_ops = {
1745 .family = INTEL_BASED_IOP,
1746 .iop_wait_ready = hptiop_wait_ready_itl,
1747 .internal_memalloc = 0,
1748 .internal_memfree = hptiop_internal_memfree_itl,
1749 .alloc_pci_res = hptiop_alloc_pci_res_itl,
1750 .release_pci_res = hptiop_release_pci_res_itl,
1751 .enable_intr = hptiop_enable_intr_itl,
1752 .disable_intr = hptiop_disable_intr_itl,
1753 .get_config = hptiop_get_config_itl,
1754 .set_config = hptiop_set_config_itl,
1755 .iop_intr = hptiop_intr_itl,
1756 .post_msg = hptiop_post_msg_itl,
1757 .post_req = hptiop_post_req_itl,
1758 .do_ioctl = hptiop_do_ioctl_itl,
1762 static struct hptiop_adapter_ops hptiop_mv_ops = {
1763 .family = MV_BASED_IOP,
1764 .iop_wait_ready = hptiop_wait_ready_mv,
1765 .internal_memalloc = hptiop_internal_memalloc_mv,
1766 .internal_memfree = hptiop_internal_memfree_mv,
1767 .alloc_pci_res = hptiop_alloc_pci_res_mv,
1768 .release_pci_res = hptiop_release_pci_res_mv,
1769 .enable_intr = hptiop_enable_intr_mv,
1770 .disable_intr = hptiop_disable_intr_mv,
1771 .get_config = hptiop_get_config_mv,
1772 .set_config = hptiop_set_config_mv,
1773 .iop_intr = hptiop_intr_mv,
1774 .post_msg = hptiop_post_msg_mv,
1775 .post_req = hptiop_post_req_mv,
1776 .do_ioctl = hptiop_do_ioctl_mv,
1780 static struct hptiop_adapter_ops hptiop_mvfrey_ops = {
1781 .family = MVFREY_BASED_IOP,
1782 .iop_wait_ready = hptiop_wait_ready_mvfrey,
1783 .internal_memalloc = hptiop_internal_memalloc_mvfrey,
1784 .internal_memfree = hptiop_internal_memfree_mvfrey,
1785 .alloc_pci_res = hptiop_alloc_pci_res_mvfrey,
1786 .release_pci_res = hptiop_release_pci_res_mvfrey,
1787 .enable_intr = hptiop_enable_intr_mvfrey,
1788 .disable_intr = hptiop_disable_intr_mvfrey,
1789 .get_config = hptiop_get_config_mvfrey,
1790 .set_config = hptiop_set_config_mvfrey,
1791 .iop_intr = hptiop_intr_mvfrey,
1792 .post_msg = hptiop_post_msg_mvfrey,
1793 .post_req = hptiop_post_req_mvfrey,
1794 .do_ioctl = hptiop_do_ioctl_mvfrey,
1795 .reset_comm = hptiop_reset_comm_mvfrey,
1798 static driver_t hptiop_pci_driver = {
1801 sizeof(struct hpt_iop_hba)
1804 DRIVER_MODULE(hptiop, pci, hptiop_pci_driver, hptiop_devclass, NULL, NULL);
1805 MODULE_DEPEND(hptiop, cam, 1, 1, 1);
1806 MODULE_VERSION(hptiop, 1);
1808 static int hptiop_probe(device_t dev)
1810 struct hpt_iop_hba *hba;
1812 static char buf[256];
1814 struct hptiop_adapter_ops *ops;
1816 if (pci_get_vendor(dev) != 0x1103)
1819 id = pci_get_device(dev);
1825 ops = &hptiop_mvfrey_ops;
1846 ops = &hptiop_itl_ops;
1851 ops = &hptiop_mv_ops;
1857 device_printf(dev, "adapter at PCI %d:%d:%d, IRQ %d\n",
1858 pci_get_bus(dev), pci_get_slot(dev),
1859 pci_get_function(dev), pci_get_irq(dev));
1861 ksprintf(buf, "RocketRAID %x %s Controller",
1862 id, sas ? "SAS" : "SATA");
1863 device_set_desc_copy(dev, buf);
1865 hba = (struct hpt_iop_hba *)device_get_softc(dev);
1866 bzero(hba, sizeof(struct hpt_iop_hba));
1869 KdPrint(("hba->ops=%p\n", hba->ops));
1873 static int hptiop_attach(device_t dev)
1875 struct hpt_iop_hba *hba = (struct hpt_iop_hba *)device_get_softc(dev);
1876 struct hpt_iop_request_get_config iop_config;
1877 struct hpt_iop_request_set_config set_config;
1879 struct cam_devq *devq;
1880 struct ccb_setasync ccb;
1881 u_int32_t unit = device_get_unit(dev);
1883 device_printf(dev, "RocketRAID 3xxx/4xxx controller driver %s\n",
1886 KdPrint(("hptiop: attach(%d, %d/%d/%d) ops=%p\n", unit,
1887 pci_get_bus(dev), pci_get_slot(dev),
1888 pci_get_function(dev), hba->ops));
1890 pci_enable_busmaster(dev);
1893 if (hba->ops->alloc_pci_res(hba))
1896 if (hba->ops->iop_wait_ready(hba, 2000)) {
1897 device_printf(dev, "adapter is not ready\n");
1898 goto release_pci_res;
1901 lockinit(&hba->lock, "hptioplock", 0, LK_CANRECURSE);
1903 if (bus_dma_tag_create(NULL,/* parent */
1906 BUS_SPACE_MAXADDR, /* lowaddr */
1907 BUS_SPACE_MAXADDR, /* highaddr */
1908 NULL, NULL, /* filter, filterarg */
1909 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
1910 BUS_SPACE_UNRESTRICTED, /* nsegments */
1911 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1913 &hba->parent_dmat /* tag */))
1915 device_printf(dev, "alloc parent_dmat failed\n");
1916 goto release_pci_res;
1919 if (hba->ops->family == MV_BASED_IOP) {
1920 if (hba->ops->internal_memalloc(hba)) {
1921 device_printf(dev, "alloc srb_dmat failed\n");
1922 goto destroy_parent_tag;
1926 if (hba->ops->get_config(hba, &iop_config)) {
1927 device_printf(dev, "get iop config failed.\n");
1928 goto get_config_failed;
1931 hba->firmware_version = iop_config.firmware_version;
1932 hba->interface_version = iop_config.interface_version;
1933 hba->max_requests = iop_config.max_requests;
1934 hba->max_devices = iop_config.max_devices;
1935 hba->max_request_size = iop_config.request_size;
1936 hba->max_sg_count = iop_config.max_sg_count;
1938 if (hba->ops->family == MVFREY_BASED_IOP) {
1939 if (hba->ops->internal_memalloc(hba)) {
1940 device_printf(dev, "alloc srb_dmat failed\n");
1941 goto destroy_parent_tag;
1943 if (hba->ops->reset_comm(hba)) {
1944 device_printf(dev, "reset comm failed\n");
1945 goto get_config_failed;
1949 if (bus_dma_tag_create(hba->parent_dmat,/* parent */
1951 BUS_SPACE_MAXADDR_32BIT+1, /* boundary */
1952 BUS_SPACE_MAXADDR, /* lowaddr */
1953 BUS_SPACE_MAXADDR, /* highaddr */
1954 NULL, NULL, /* filter, filterarg */
1955 PAGE_SIZE * (hba->max_sg_count-1), /* maxsize */
1956 hba->max_sg_count, /* nsegments */
1957 0x20000, /* maxsegsize */
1958 BUS_DMA_ALLOCNOW, /* flags */
1959 &hba->io_dmat /* tag */))
1961 device_printf(dev, "alloc io_dmat failed\n");
1962 goto get_config_failed;
1965 if (bus_dma_tag_create(hba->parent_dmat,/* parent */
1968 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1969 BUS_SPACE_MAXADDR, /* highaddr */
1970 NULL, NULL, /* filter, filterarg */
1971 HPT_SRB_MAX_SIZE * HPT_SRB_MAX_QUEUE_SIZE + 0x20,
1973 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1975 &hba->srb_dmat /* tag */))
1977 device_printf(dev, "alloc srb_dmat failed\n");
1978 goto destroy_io_dmat;
1981 if (bus_dmamem_alloc(hba->srb_dmat, (void **)&hba->uncached_ptr,
1982 BUS_DMA_WAITOK | BUS_DMA_COHERENT,
1983 &hba->srb_dmamap) != 0)
1985 device_printf(dev, "srb bus_dmamem_alloc failed!\n");
1986 goto destroy_srb_dmat;
1989 if (bus_dmamap_load(hba->srb_dmat,
1990 hba->srb_dmamap, hba->uncached_ptr,
1991 (HPT_SRB_MAX_SIZE * HPT_SRB_MAX_QUEUE_SIZE) + 0x20,
1992 hptiop_map_srb, hba, 0))
1994 device_printf(dev, "bus_dmamap_load failed!\n");
1995 goto srb_dmamem_free;
1998 if ((devq = cam_simq_alloc(hba->max_requests - 1 )) == NULL) {
1999 device_printf(dev, "cam_simq_alloc failed\n");
2000 goto srb_dmamap_unload;
2003 hba->sim = cam_sim_alloc(hptiop_action, hptiop_poll, driver_name,
2004 hba, unit, &sim_mplock, hba->max_requests - 1, 1, devq);
2005 cam_simq_release(devq);
2007 device_printf(dev, "cam_sim_alloc failed\n");
2008 goto srb_dmamap_unload;
2010 if (xpt_bus_register(hba->sim, 0) != CAM_SUCCESS)
2012 device_printf(dev, "xpt_bus_register failed\n");
2016 if (xpt_create_path(&hba->path, /*periph */ NULL,
2017 cam_sim_path(hba->sim), CAM_TARGET_WILDCARD,
2018 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2019 device_printf(dev, "xpt_create_path failed\n");
2020 goto deregister_xpt_bus;
2023 bzero(&set_config, sizeof(set_config));
2024 set_config.iop_id = unit;
2025 set_config.vbus_id = cam_sim_path(hba->sim);
2026 set_config.max_host_request_size = HPT_SRB_MAX_REQ_SIZE;
2028 if (hba->ops->set_config(hba, &set_config)) {
2029 device_printf(dev, "set iop config failed.\n");
2033 xpt_setup_ccb(&ccb.ccb_h, hba->path, /*priority*/5);
2034 ccb.ccb_h.func_code = XPT_SASYNC_CB;
2035 ccb.event_enable = (AC_FOUND_DEVICE | AC_LOST_DEVICE);
2036 ccb.callback = hptiop_async;
2037 ccb.callback_arg = hba->sim;
2038 xpt_action((union ccb *)&ccb);
2041 if ((hba->irq_res = bus_alloc_resource(hba->pcidev, SYS_RES_IRQ,
2042 &rid, 0, ~0ul, 1, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
2043 device_printf(dev, "allocate irq failed!\n");
2047 if (bus_setup_intr(hba->pcidev, hba->irq_res, 0,
2048 hptiop_pci_intr, hba, &hba->irq_handle, NULL))
2050 device_printf(dev, "allocate intr function failed!\n");
2051 goto free_irq_resource;
2054 if (hptiop_send_sync_msg(hba,
2055 IOPMU_INBOUND_MSG0_START_BACKGROUND_TASK, 5000)) {
2056 device_printf(dev, "fail to start background task\n");
2057 goto teartown_irq_resource;
2060 hba->ops->enable_intr(hba);
2061 hba->initialized = 1;
2063 hba->ioctl_dev = make_dev(&hptiop_ops, unit,
2064 UID_ROOT, GID_WHEEL /*GID_OPERATOR*/,
2065 S_IRUSR | S_IWUSR, "%s%d", driver_name, unit);
2067 hba->ioctl_dev->si_drv1 = hba;
2069 hptiop_rescan_bus(hba);
2074 teartown_irq_resource:
2075 bus_teardown_intr(dev, hba->irq_res, hba->irq_handle);
2078 bus_release_resource(dev, SYS_RES_IRQ, 0, hba->irq_res);
2081 xpt_free_path(hba->path);
2084 xpt_bus_deregister(cam_sim_path(hba->sim));
2087 cam_sim_free(hba->sim);
2090 if (hba->uncached_ptr)
2091 bus_dmamap_unload(hba->srb_dmat, hba->srb_dmamap);
2094 if (hba->uncached_ptr)
2095 bus_dmamem_free(hba->srb_dmat,
2096 hba->uncached_ptr, hba->srb_dmamap);
2100 bus_dma_tag_destroy(hba->srb_dmat);
2104 bus_dma_tag_destroy(hba->io_dmat);
2107 hba->ops->internal_memfree(hba);
2110 if (hba->parent_dmat)
2111 bus_dma_tag_destroy(hba->parent_dmat);
2114 if (hba->ops->release_pci_res)
2115 hba->ops->release_pci_res(hba);
2120 static int hptiop_detach(device_t dev)
2122 struct hpt_iop_hba * hba = (struct hpt_iop_hba *)device_get_softc(dev);
2126 hptiop_lock_adapter(hba);
2127 for (i = 0; i < hba->max_devices; i++)
2128 if (hptiop_os_query_remove_device(hba, i)) {
2129 device_printf(dev, "file system is busy. id=%d", i);
2133 if ((error = hptiop_shutdown(dev)) != 0)
2135 if (hptiop_send_sync_msg(hba,
2136 IOPMU_INBOUND_MSG0_STOP_BACKGROUND_TASK, 60000))
2139 hptiop_release_resource(hba);
2142 hptiop_unlock_adapter(hba);
2146 static int hptiop_shutdown(device_t dev)
2148 struct hpt_iop_hba * hba = (struct hpt_iop_hba *)device_get_softc(dev);
2152 if (hba->flag & HPT_IOCTL_FLAG_OPEN) {
2153 device_printf(dev, "device is busy");
2157 hba->ops->disable_intr(hba);
2159 if (hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_SHUTDOWN, 60000))
2165 static void hptiop_pci_intr(void *arg)
2167 struct hpt_iop_hba * hba = (struct hpt_iop_hba *)arg;
2168 hptiop_lock_adapter(hba);
2169 hba->ops->iop_intr(hba);
2170 hptiop_unlock_adapter(hba);
2173 static void hptiop_poll(struct cam_sim *sim)
2175 hptiop_pci_intr(cam_sim_softc(sim));
2178 static void hptiop_async(void * callback_arg, u_int32_t code,
2179 struct cam_path * path, void * arg)
2183 static void hptiop_enable_intr_itl(struct hpt_iop_hba *hba)
2185 BUS_SPACE_WRT4_ITL(outbound_intmask,
2186 ~(IOPMU_OUTBOUND_INT_POSTQUEUE | IOPMU_OUTBOUND_INT_MSG0));
2189 static void hptiop_enable_intr_mv(struct hpt_iop_hba *hba)
2193 int_mask = BUS_SPACE_RD4_MV0(outbound_intmask);
2195 int_mask |= MVIOP_MU_OUTBOUND_INT_POSTQUEUE
2196 | MVIOP_MU_OUTBOUND_INT_MSG;
2197 BUS_SPACE_WRT4_MV0(outbound_intmask,int_mask);
2200 static void hptiop_enable_intr_mvfrey(struct hpt_iop_hba *hba)
2202 BUS_SPACE_WRT4_MVFREY2(f0_doorbell_enable, CPU_TO_F0_DRBL_MSG_A_BIT);
2203 BUS_SPACE_RD4_MVFREY2(f0_doorbell_enable);
2205 BUS_SPACE_WRT4_MVFREY2(isr_enable, 0x1);
2206 BUS_SPACE_RD4_MVFREY2(isr_enable);
2208 BUS_SPACE_WRT4_MVFREY2(pcie_f0_int_enable, 0x1010);
2209 BUS_SPACE_RD4_MVFREY2(pcie_f0_int_enable);
2212 static void hptiop_disable_intr_itl(struct hpt_iop_hba *hba)
2216 int_mask = BUS_SPACE_RD4_ITL(outbound_intmask);
2218 int_mask |= IOPMU_OUTBOUND_INT_POSTQUEUE | IOPMU_OUTBOUND_INT_MSG0;
2219 BUS_SPACE_WRT4_ITL(outbound_intmask, int_mask);
2220 BUS_SPACE_RD4_ITL(outbound_intstatus);
2223 static void hptiop_disable_intr_mv(struct hpt_iop_hba *hba)
2226 int_mask = BUS_SPACE_RD4_MV0(outbound_intmask);
2228 int_mask &= ~(MVIOP_MU_OUTBOUND_INT_MSG
2229 | MVIOP_MU_OUTBOUND_INT_POSTQUEUE);
2230 BUS_SPACE_WRT4_MV0(outbound_intmask,int_mask);
2231 BUS_SPACE_RD4_MV0(outbound_intmask);
2234 static void hptiop_disable_intr_mvfrey(struct hpt_iop_hba *hba)
2236 BUS_SPACE_WRT4_MVFREY2(f0_doorbell_enable, 0);
2237 BUS_SPACE_RD4_MVFREY2(f0_doorbell_enable);
2239 BUS_SPACE_WRT4_MVFREY2(isr_enable, 0);
2240 BUS_SPACE_RD4_MVFREY2(isr_enable);
2242 BUS_SPACE_WRT4_MVFREY2(pcie_f0_int_enable, 0);
2243 BUS_SPACE_RD4_MVFREY2(pcie_f0_int_enable);
2246 static void hptiop_reset_adapter(void *argv)
2248 struct hpt_iop_hba * hba = (struct hpt_iop_hba *)argv;
2249 if (hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET, 60000))
2251 hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_START_BACKGROUND_TASK, 5000);
2254 static void *hptiop_get_srb(struct hpt_iop_hba * hba)
2256 struct hpt_iop_srb * srb;
2258 if (hba->srb_list) {
2259 srb = hba->srb_list;
2260 hba->srb_list = srb->next;
2267 static void hptiop_free_srb(struct hpt_iop_hba *hba, struct hpt_iop_srb *srb)
2269 srb->next = hba->srb_list;
2270 hba->srb_list = srb;
2273 static void hptiop_action(struct cam_sim *sim, union ccb *ccb)
2275 struct hpt_iop_hba * hba = (struct hpt_iop_hba *)cam_sim_softc(sim);
2276 struct hpt_iop_srb * srb;
2278 switch (ccb->ccb_h.func_code) {
2281 hptiop_lock_adapter(hba);
2282 if (ccb->ccb_h.target_lun != 0 ||
2283 ccb->ccb_h.target_id >= hba->max_devices ||
2284 (ccb->ccb_h.flags & CAM_CDB_PHYS))
2286 ccb->ccb_h.status = CAM_TID_INVALID;
2291 if ((srb = hptiop_get_srb(hba)) == NULL) {
2292 device_printf(hba->pcidev, "srb allocated failed");
2293 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2300 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
2301 hptiop_post_scsi_command(srb, NULL, 0, 0);
2302 else if ((ccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
2303 if ((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0) {
2306 error = bus_dmamap_load(hba->io_dmat,
2309 ccb->csio.dxfer_len,
2310 hptiop_post_scsi_command,
2313 if (error && error != EINPROGRESS) {
2314 device_printf(hba->pcidev,
2315 "bus_dmamap_load error %d", error);
2316 xpt_freeze_simq(hba->sim, 1);
2317 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2319 hptiop_free_srb(hba, srb);
2325 device_printf(hba->pcidev,
2326 "CAM_DATA_PHYS not supported");
2327 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2332 struct bus_dma_segment *segs;
2334 if ((ccb->ccb_h.flags & CAM_SG_LIST_PHYS) == 0 ||
2335 (ccb->ccb_h.flags & CAM_DATA_PHYS) != 0) {
2336 device_printf(hba->pcidev, "SCSI cmd failed");
2337 ccb->ccb_h.status=CAM_PROVIDE_FAIL;
2341 segs = (struct bus_dma_segment *)ccb->csio.data_ptr;
2342 hptiop_post_scsi_command(srb, segs,
2343 ccb->csio.sglist_cnt, 0);
2347 hptiop_unlock_adapter(hba);
2351 device_printf(hba->pcidev, "reset adapter");
2352 hptiop_lock_adapter(hba);
2354 hptiop_reset_adapter(hba);
2355 hptiop_unlock_adapter(hba);
2358 case XPT_GET_TRAN_SETTINGS:
2359 case XPT_SET_TRAN_SETTINGS:
2360 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2363 case XPT_CALC_GEOMETRY:
2364 cam_calc_geometry(&ccb->ccg, 1);
2369 struct ccb_pathinq *cpi = &ccb->cpi;
2371 cpi->version_num = 1;
2372 cpi->hba_inquiry = PI_SDTR_ABLE;
2373 cpi->target_sprt = 0;
2374 cpi->hba_misc = PIM_NOBUSRESET;
2375 cpi->hba_eng_cnt = 0;
2376 cpi->max_target = hba->max_devices;
2378 cpi->unit_number = cam_sim_unit(sim);
2379 cpi->bus_id = cam_sim_bus(sim);
2380 cpi->initiator_id = hba->max_devices;
2381 cpi->base_transfer_speed = 3300;
2383 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2384 strncpy(cpi->hba_vid, "HPT ", HBA_IDLEN);
2385 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2386 cpi->transport = XPORT_SPI;
2387 cpi->transport_version = 2;
2388 cpi->protocol = PROTO_SCSI;
2389 cpi->protocol_version = SCSI_REV_2;
2390 cpi->ccb_h.status = CAM_REQ_CMP;
2395 ccb->ccb_h.status = CAM_REQ_INVALID;
2403 static void hptiop_post_req_itl(struct hpt_iop_hba *hba,
2404 struct hpt_iop_srb *srb,
2405 bus_dma_segment_t *segs, int nsegs)
2408 union ccb *ccb = srb->ccb;
2411 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
2412 cdb = ccb->csio.cdb_io.cdb_ptr;
2414 cdb = ccb->csio.cdb_io.cdb_bytes;
2416 KdPrint(("ccb=%p %x-%x-%x\n",
2417 ccb, *(u_int32_t *)cdb, *((u_int32_t *)cdb+1), *((u_int32_t *)cdb+2)));
2419 if (srb->srb_flag & HPT_SRB_FLAG_HIGH_MEM_ACESS) {
2420 u_int32_t iop_req32;
2421 struct hpt_iop_request_scsi_command req;
2423 iop_req32 = BUS_SPACE_RD4_ITL(inbound_queue);
2425 if (iop_req32 == IOPMU_QUEUE_EMPTY) {
2426 device_printf(hba->pcidev, "invaild req offset\n");
2427 ccb->ccb_h.status = CAM_BUSY;
2428 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
2429 hptiop_free_srb(hba, srb);
2434 if (ccb->csio.dxfer_len && nsegs > 0) {
2435 struct hpt_iopsg *psg = req.sg_list;
2436 for (idx = 0; idx < nsegs; idx++, psg++) {
2437 psg->pci_address = (u_int64_t)segs[idx].ds_addr;
2438 psg->size = segs[idx].ds_len;
2444 bcopy(cdb, req.cdb, ccb->csio.cdb_len);
2447 offsetof(struct hpt_iop_request_scsi_command, sg_list)
2448 + nsegs*sizeof(struct hpt_iopsg);
2449 req.header.type = IOP_REQUEST_TYPE_SCSI_COMMAND;
2450 req.header.flags = 0;
2451 req.header.result = IOP_RESULT_PENDING;
2452 req.header.context = (u_int64_t)(unsigned long)srb;
2453 req.dataxfer_length = ccb->csio.dxfer_len;
2455 req.target = ccb->ccb_h.target_id;
2456 req.lun = ccb->ccb_h.target_lun;
2458 bus_space_write_region_1(hba->bar0t, hba->bar0h, iop_req32,
2459 (u_int8_t *)&req, req.header.size);
2461 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2462 bus_dmamap_sync(hba->io_dmat,
2463 srb->dma_map, BUS_DMASYNC_PREREAD);
2465 else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
2466 bus_dmamap_sync(hba->io_dmat,
2467 srb->dma_map, BUS_DMASYNC_PREWRITE);
2469 BUS_SPACE_WRT4_ITL(inbound_queue,iop_req32);
2471 struct hpt_iop_request_scsi_command *req;
2473 req = (struct hpt_iop_request_scsi_command *)srb;
2474 if (ccb->csio.dxfer_len && nsegs > 0) {
2475 struct hpt_iopsg *psg = req->sg_list;
2476 for (idx = 0; idx < nsegs; idx++, psg++) {
2478 (u_int64_t)segs[idx].ds_addr;
2479 psg->size = segs[idx].ds_len;
2485 bcopy(cdb, req->cdb, ccb->csio.cdb_len);
2487 req->header.type = IOP_REQUEST_TYPE_SCSI_COMMAND;
2488 req->header.result = IOP_RESULT_PENDING;
2489 req->dataxfer_length = ccb->csio.dxfer_len;
2491 req->target = ccb->ccb_h.target_id;
2492 req->lun = ccb->ccb_h.target_lun;
2494 offsetof(struct hpt_iop_request_scsi_command, sg_list)
2495 + nsegs*sizeof(struct hpt_iopsg);
2496 req->header.context = (u_int64_t)srb->index |
2497 IOPMU_QUEUE_ADDR_HOST_BIT;
2498 req->header.flags = IOP_REQUEST_FLAG_OUTPUT_CONTEXT;
2500 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2501 bus_dmamap_sync(hba->io_dmat,
2502 srb->dma_map, BUS_DMASYNC_PREREAD);
2503 }else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
2504 bus_dmamap_sync(hba->io_dmat,
2505 srb->dma_map, BUS_DMASYNC_PREWRITE);
2508 if (hba->firmware_version > 0x01020000
2509 || hba->interface_version > 0x01020000) {
2510 u_int32_t size_bits;
2512 if (req->header.size < 256)
2513 size_bits = IOPMU_QUEUE_REQUEST_SIZE_BIT;
2514 else if (req->header.size < 512)
2515 size_bits = IOPMU_QUEUE_ADDR_HOST_BIT;
2517 size_bits = IOPMU_QUEUE_REQUEST_SIZE_BIT
2518 | IOPMU_QUEUE_ADDR_HOST_BIT;
2520 BUS_SPACE_WRT4_ITL(inbound_queue,
2521 (u_int32_t)srb->phy_addr | size_bits);
2523 BUS_SPACE_WRT4_ITL(inbound_queue, (u_int32_t)srb->phy_addr
2524 |IOPMU_QUEUE_ADDR_HOST_BIT);
2528 static void hptiop_post_req_mv(struct hpt_iop_hba *hba,
2529 struct hpt_iop_srb *srb,
2530 bus_dma_segment_t *segs, int nsegs)
2533 union ccb *ccb = srb->ccb;
2535 struct hpt_iop_request_scsi_command *req;
2538 req = (struct hpt_iop_request_scsi_command *)srb;
2539 req_phy = srb->phy_addr;
2541 if (ccb->csio.dxfer_len && nsegs > 0) {
2542 struct hpt_iopsg *psg = req->sg_list;
2543 for (idx = 0; idx < nsegs; idx++, psg++) {
2544 psg->pci_address = (u_int64_t)segs[idx].ds_addr;
2545 psg->size = segs[idx].ds_len;
2550 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
2551 cdb = ccb->csio.cdb_io.cdb_ptr;
2553 cdb = ccb->csio.cdb_io.cdb_bytes;
2555 bcopy(cdb, req->cdb, ccb->csio.cdb_len);
2556 req->header.type = IOP_REQUEST_TYPE_SCSI_COMMAND;
2557 req->header.result = IOP_RESULT_PENDING;
2558 req->dataxfer_length = ccb->csio.dxfer_len;
2560 req->target = ccb->ccb_h.target_id;
2561 req->lun = ccb->ccb_h.target_lun;
2562 req->header.size = sizeof(struct hpt_iop_request_scsi_command)
2563 - sizeof(struct hpt_iopsg)
2564 + nsegs * sizeof(struct hpt_iopsg);
2565 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2566 bus_dmamap_sync(hba->io_dmat,
2567 srb->dma_map, BUS_DMASYNC_PREREAD);
2569 else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
2570 bus_dmamap_sync(hba->io_dmat,
2571 srb->dma_map, BUS_DMASYNC_PREWRITE);
2572 req->header.context = (u_int64_t)srb->index
2573 << MVIOP_REQUEST_NUMBER_START_BIT
2574 | MVIOP_CMD_TYPE_SCSI;
2575 req->header.flags = IOP_REQUEST_FLAG_OUTPUT_CONTEXT;
2576 size = req->header.size >> 8;
2577 hptiop_mv_inbound_write(req_phy
2578 | MVIOP_MU_QUEUE_ADDR_HOST_BIT
2579 | (size > 3 ? 3 : size), hba);
2582 static void hptiop_post_req_mvfrey(struct hpt_iop_hba *hba,
2583 struct hpt_iop_srb *srb,
2584 bus_dma_segment_t *segs, int nsegs)
2587 union ccb *ccb = srb->ccb;
2589 struct hpt_iop_request_scsi_command *req;
2592 req = (struct hpt_iop_request_scsi_command *)srb;
2593 req_phy = srb->phy_addr;
2595 if (ccb->csio.dxfer_len && nsegs > 0) {
2596 struct hpt_iopsg *psg = req->sg_list;
2597 for (idx = 0; idx < nsegs; idx++, psg++) {
2598 psg->pci_address = (u_int64_t)segs[idx].ds_addr | 1;
2599 psg->size = segs[idx].ds_len;
2604 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
2605 cdb = ccb->csio.cdb_io.cdb_ptr;
2607 cdb = ccb->csio.cdb_io.cdb_bytes;
2609 bcopy(cdb, req->cdb, ccb->csio.cdb_len);
2610 req->header.type = IOP_REQUEST_TYPE_SCSI_COMMAND;
2611 req->header.result = IOP_RESULT_PENDING;
2612 req->dataxfer_length = ccb->csio.dxfer_len;
2614 req->target = ccb->ccb_h.target_id;
2615 req->lun = ccb->ccb_h.target_lun;
2616 req->header.size = sizeof(struct hpt_iop_request_scsi_command)
2617 - sizeof(struct hpt_iopsg)
2618 + nsegs * sizeof(struct hpt_iopsg);
2619 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2620 bus_dmamap_sync(hba->io_dmat,
2621 srb->dma_map, BUS_DMASYNC_PREREAD);
2623 else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
2624 bus_dmamap_sync(hba->io_dmat,
2625 srb->dma_map, BUS_DMASYNC_PREWRITE);
2627 req->header.flags = IOP_REQUEST_FLAG_OUTPUT_CONTEXT
2628 | IOP_REQUEST_FLAG_ADDR_BITS
2629 | ((req_phy >> 16) & 0xffff0000);
2630 req->header.context = ((req_phy & 0xffffffff) << 32 )
2632 | IOPMU_QUEUE_ADDR_HOST_BIT | req->header.type;
2634 hba->u.mvfrey.inlist_wptr++;
2635 index = hba->u.mvfrey.inlist_wptr & 0x3fff;
2637 if (index == hba->u.mvfrey.list_count) {
2639 hba->u.mvfrey.inlist_wptr &= ~0x3fff;
2640 hba->u.mvfrey.inlist_wptr ^= CL_POINTER_TOGGLE;
2643 hba->u.mvfrey.inlist[index].addr = req_phy;
2644 hba->u.mvfrey.inlist[index].intrfc_len = (req->header.size + 3) / 4;
2646 BUS_SPACE_WRT4_MVFREY2(inbound_write_ptr, hba->u.mvfrey.inlist_wptr);
2647 BUS_SPACE_RD4_MVFREY2(inbound_write_ptr);
2649 if (req->header.type == IOP_REQUEST_TYPE_SCSI_COMMAND) {
2650 callout_reset(&ccb->ccb_h.timeout_ch, 20*hz,
2651 hptiop_reset_adapter, hba);
2655 static void hptiop_post_scsi_command(void *arg, bus_dma_segment_t *segs,
2656 int nsegs, int error)
2658 struct hpt_iop_srb *srb = (struct hpt_iop_srb *)arg;
2659 union ccb *ccb = srb->ccb;
2660 struct hpt_iop_hba *hba = srb->hba;
2662 if (error || nsegs > hba->max_sg_count) {
2663 KdPrint(("hptiop: func_code=%x tid=%x lun=%x nsegs=%d\n",
2664 ccb->ccb_h.func_code,
2665 ccb->ccb_h.target_id,
2666 ccb->ccb_h.target_lun, nsegs));
2667 ccb->ccb_h.status = CAM_BUSY;
2668 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
2669 hptiop_free_srb(hba, srb);
2674 hba->ops->post_req(hba, srb, segs, nsegs);
2677 static void hptiop_mv_map_ctlcfg(void *arg, bus_dma_segment_t *segs,
2678 int nsegs, int error)
2680 struct hpt_iop_hba *hba = (struct hpt_iop_hba *)arg;
2681 hba->ctlcfgcmd_phy = ((u_int64_t)segs->ds_addr + 0x1F)
2683 hba->ctlcfg_ptr = (u_int8_t *)(((unsigned long)hba->ctlcfg_ptr + 0x1F)
2687 static void hptiop_mvfrey_map_ctlcfg(void *arg, bus_dma_segment_t *segs,
2688 int nsegs, int error)
2690 struct hpt_iop_hba *hba = (struct hpt_iop_hba *)arg;
2693 u_int32_t list_count = hba->u.mvfrey.list_count;
2695 phy = ((u_int64_t)segs->ds_addr + 0x1F)
2697 p = (u_int8_t *)(((unsigned long)hba->ctlcfg_ptr + 0x1F)
2700 hba->ctlcfgcmd_phy = phy;
2701 hba->ctlcfg_ptr = p;
2706 hba->u.mvfrey.inlist = (struct mvfrey_inlist_entry *)p;
2707 hba->u.mvfrey.inlist_phy = phy;
2709 p += list_count * sizeof(struct mvfrey_inlist_entry);
2710 phy += list_count * sizeof(struct mvfrey_inlist_entry);
2712 hba->u.mvfrey.outlist = (struct mvfrey_outlist_entry *)p;
2713 hba->u.mvfrey.outlist_phy = phy;
2715 p += list_count * sizeof(struct mvfrey_outlist_entry);
2716 phy += list_count * sizeof(struct mvfrey_outlist_entry);
2718 hba->u.mvfrey.outlist_cptr = (u_int32_t *)p;
2719 hba->u.mvfrey.outlist_cptr_phy = phy;
2722 static void hptiop_map_srb(void *arg, bus_dma_segment_t *segs,
2723 int nsegs, int error)
2725 struct hpt_iop_hba * hba = (struct hpt_iop_hba *)arg;
2726 bus_addr_t phy_addr = (segs->ds_addr + 0x1F) & ~(bus_addr_t)0x1F;
2727 struct hpt_iop_srb *srb, *tmp_srb;
2730 if (error || nsegs == 0) {
2731 device_printf(hba->pcidev, "hptiop_map_srb error");
2736 srb = (struct hpt_iop_srb *)
2737 (((unsigned long)hba->uncached_ptr + 0x1F)
2738 & ~(unsigned long)0x1F);
2740 for (i = 0; i < HPT_SRB_MAX_QUEUE_SIZE; i++) {
2741 tmp_srb = (struct hpt_iop_srb *)
2742 ((char *)srb + i * HPT_SRB_MAX_SIZE);
2743 if (((unsigned long)tmp_srb & 0x1F) == 0) {
2744 if (bus_dmamap_create(hba->io_dmat,
2745 0, &tmp_srb->dma_map)) {
2746 device_printf(hba->pcidev, "dmamap create failed");
2750 bzero(tmp_srb, sizeof(struct hpt_iop_srb));
2753 if (hba->ctlcfg_ptr == 0) {/*itl iop*/
2754 tmp_srb->phy_addr = (u_int64_t)(u_int32_t)
2756 if (phy_addr & IOPMU_MAX_MEM_SUPPORT_MASK_32G)
2758 HPT_SRB_FLAG_HIGH_MEM_ACESS;
2760 tmp_srb->phy_addr = phy_addr;
2763 hptiop_free_srb(hba, tmp_srb);
2764 hba->srb[i] = tmp_srb;
2765 phy_addr += HPT_SRB_MAX_SIZE;
2768 device_printf(hba->pcidev, "invalid alignment");
2774 static void hptiop_os_message_callback(struct hpt_iop_hba * hba, u_int32_t msg)
2779 static int hptiop_os_query_remove_device(struct hpt_iop_hba * hba,
2782 struct cam_periph *periph = NULL;
2783 struct cam_path *path;
2784 int status, retval = 0;
2786 status = xpt_create_path(&path, NULL, hba->sim->path_id, target_id, 0);
2788 if (status == CAM_REQ_CMP) {
2789 if ((periph = cam_periph_find(path, "da")) != NULL) {
2790 if (periph->refcount >= 1) {
2791 device_printf(hba->pcidev, "target_id=0x%x,"
2792 "refcount=%d", target_id, periph->refcount);
2796 xpt_free_path(path);
2801 static void hptiop_release_resource(struct hpt_iop_hba *hba)
2805 struct ccb_setasync ccb;
2807 xpt_setup_ccb(&ccb.ccb_h, hba->path, /*priority*/5);
2808 ccb.ccb_h.func_code = XPT_SASYNC_CB;
2809 ccb.event_enable = 0;
2810 ccb.callback = hptiop_async;
2811 ccb.callback_arg = hba->sim;
2812 xpt_action((union ccb *)&ccb);
2813 xpt_free_path(hba->path);
2817 xpt_bus_deregister(cam_sim_path(hba->sim));
2818 cam_sim_free(hba->sim);
2821 if (hba->ctlcfg_dmat) {
2822 bus_dmamap_unload(hba->ctlcfg_dmat, hba->ctlcfg_dmamap);
2823 bus_dmamem_free(hba->ctlcfg_dmat,
2824 hba->ctlcfg_ptr, hba->ctlcfg_dmamap);
2825 bus_dma_tag_destroy(hba->ctlcfg_dmat);
2828 for (i = 0; i < HPT_SRB_MAX_QUEUE_SIZE; i++) {
2829 struct hpt_iop_srb *srb = hba->srb[i];
2831 bus_dmamap_destroy(hba->io_dmat, srb->dma_map);
2834 if (hba->srb_dmat) {
2835 bus_dmamap_unload(hba->srb_dmat, hba->srb_dmamap);
2836 bus_dmamap_destroy(hba->srb_dmat, hba->srb_dmamap);
2837 bus_dma_tag_destroy(hba->srb_dmat);
2841 bus_dma_tag_destroy(hba->io_dmat);
2843 if (hba->parent_dmat)
2844 bus_dma_tag_destroy(hba->parent_dmat);
2846 if (hba->irq_handle)
2847 bus_teardown_intr(hba->pcidev, hba->irq_res, hba->irq_handle);
2850 bus_release_resource(hba->pcidev, SYS_RES_IRQ,
2854 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
2855 hba->bar0_rid, hba->bar0_res);
2857 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
2858 hba->bar2_rid, hba->bar2_res);
2860 destroy_dev(hba->ioctl_dev);
2861 dev_ops_remove_minor(&hptiop_ops, device_get_unit(hba->pcidev));