2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * HighPoint RR3xxx/4xxx RAID Driver for FreeBSD
5 * Copyright (C) 2007-2012 HighPoint Technologies, Inc. All Rights Reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/types.h>
36 #include <sys/systm.h>
39 #include <sys/malloc.h>
41 #include <sys/libkern.h>
42 #include <sys/kernel.h>
44 #include <sys/kthread.h>
45 #include <sys/mutex.h>
46 #include <sys/module.h>
48 #include <sys/eventhandler.h>
50 #include <sys/taskqueue.h>
51 #include <sys/ioccom.h>
53 #include <machine/resource.h>
54 #include <machine/bus.h>
55 #include <machine/stdarg.h>
61 #include <dev/pci/pcireg.h>
62 #include <dev/pci/pcivar.h>
66 #include <cam/cam_ccb.h>
67 #include <cam/cam_sim.h>
68 #include <cam/cam_xpt_sim.h>
69 #include <cam/cam_debug.h>
70 #include <cam/cam_periph.h>
71 #include <cam/scsi/scsi_all.h>
72 #include <cam/scsi/scsi_message.h>
75 #include <dev/hptiop/hptiop.h>
77 static const char driver_name[] = "hptiop";
78 static const char driver_version[] = "v1.9";
80 static devclass_t hptiop_devclass;
82 static int hptiop_send_sync_msg(struct hpt_iop_hba *hba,
83 u_int32_t msg, u_int32_t millisec);
84 static void hptiop_request_callback_itl(struct hpt_iop_hba *hba,
86 static void hptiop_request_callback_mv(struct hpt_iop_hba *hba, u_int64_t req);
87 static void hptiop_request_callback_mvfrey(struct hpt_iop_hba *hba,
89 static void hptiop_os_message_callback(struct hpt_iop_hba *hba, u_int32_t msg);
90 static int hptiop_do_ioctl_itl(struct hpt_iop_hba *hba,
91 struct hpt_iop_ioctl_param *pParams);
92 static int hptiop_do_ioctl_mv(struct hpt_iop_hba *hba,
93 struct hpt_iop_ioctl_param *pParams);
94 static int hptiop_do_ioctl_mvfrey(struct hpt_iop_hba *hba,
95 struct hpt_iop_ioctl_param *pParams);
96 static int hptiop_rescan_bus(struct hpt_iop_hba *hba);
97 static int hptiop_alloc_pci_res_itl(struct hpt_iop_hba *hba);
98 static int hptiop_alloc_pci_res_mv(struct hpt_iop_hba *hba);
99 static int hptiop_alloc_pci_res_mvfrey(struct hpt_iop_hba *hba);
100 static int hptiop_get_config_itl(struct hpt_iop_hba *hba,
101 struct hpt_iop_request_get_config *config);
102 static int hptiop_get_config_mv(struct hpt_iop_hba *hba,
103 struct hpt_iop_request_get_config *config);
104 static int hptiop_get_config_mvfrey(struct hpt_iop_hba *hba,
105 struct hpt_iop_request_get_config *config);
106 static int hptiop_set_config_itl(struct hpt_iop_hba *hba,
107 struct hpt_iop_request_set_config *config);
108 static int hptiop_set_config_mv(struct hpt_iop_hba *hba,
109 struct hpt_iop_request_set_config *config);
110 static int hptiop_set_config_mvfrey(struct hpt_iop_hba *hba,
111 struct hpt_iop_request_set_config *config);
112 static int hptiop_internal_memalloc_mv(struct hpt_iop_hba *hba);
113 static int hptiop_internal_memalloc_mvfrey(struct hpt_iop_hba *hba);
114 static int hptiop_internal_memfree_itl(struct hpt_iop_hba *hba);
115 static int hptiop_internal_memfree_mv(struct hpt_iop_hba *hba);
116 static int hptiop_internal_memfree_mvfrey(struct hpt_iop_hba *hba);
117 static int hptiop_post_ioctl_command_itl(struct hpt_iop_hba *hba,
118 u_int32_t req32, struct hpt_iop_ioctl_param *pParams);
119 static int hptiop_post_ioctl_command_mv(struct hpt_iop_hba *hba,
120 struct hpt_iop_request_ioctl_command *req,
121 struct hpt_iop_ioctl_param *pParams);
122 static int hptiop_post_ioctl_command_mvfrey(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_req_mvfrey(struct hpt_iop_hba *hba,
132 struct hpt_iop_srb *srb,
133 bus_dma_segment_t *segs, int nsegs);
134 static void hptiop_post_msg_itl(struct hpt_iop_hba *hba, u_int32_t msg);
135 static void hptiop_post_msg_mv(struct hpt_iop_hba *hba, u_int32_t msg);
136 static void hptiop_post_msg_mvfrey(struct hpt_iop_hba *hba, u_int32_t msg);
137 static void hptiop_enable_intr_itl(struct hpt_iop_hba *hba);
138 static void hptiop_enable_intr_mv(struct hpt_iop_hba *hba);
139 static void hptiop_enable_intr_mvfrey(struct hpt_iop_hba *hba);
140 static void hptiop_disable_intr_itl(struct hpt_iop_hba *hba);
141 static void hptiop_disable_intr_mv(struct hpt_iop_hba *hba);
142 static void hptiop_disable_intr_mvfrey(struct hpt_iop_hba *hba);
143 static void hptiop_free_srb(struct hpt_iop_hba *hba, struct hpt_iop_srb *srb);
144 static int hptiop_os_query_remove_device(struct hpt_iop_hba *hba, int tid);
145 static int hptiop_probe(device_t dev);
146 static int hptiop_attach(device_t dev);
147 static int hptiop_detach(device_t dev);
148 static int hptiop_shutdown(device_t dev);
149 static void hptiop_action(struct cam_sim *sim, union ccb *ccb);
150 static void hptiop_poll(struct cam_sim *sim);
151 static void hptiop_async(void *callback_arg, u_int32_t code,
152 struct cam_path *path, void *arg);
153 static void hptiop_pci_intr(void *arg);
154 static void hptiop_release_resource(struct hpt_iop_hba *hba);
155 static void hptiop_reset_adapter(void *argv);
156 static d_open_t hptiop_open;
157 static d_close_t hptiop_close;
158 static d_ioctl_t hptiop_ioctl;
160 static struct cdevsw hptiop_cdevsw = {
161 .d_open = hptiop_open,
162 .d_close = hptiop_close,
163 .d_ioctl = hptiop_ioctl,
164 .d_name = driver_name,
165 .d_version = D_VERSION,
168 #define hba_from_dev(dev) \
169 ((struct hpt_iop_hba *)devclass_get_softc(hptiop_devclass, dev2unit(dev)))
171 #define BUS_SPACE_WRT4_ITL(offset, value) bus_space_write_4(hba->bar0t,\
172 hba->bar0h, offsetof(struct hpt_iopmu_itl, offset), (value))
173 #define BUS_SPACE_RD4_ITL(offset) bus_space_read_4(hba->bar0t,\
174 hba->bar0h, offsetof(struct hpt_iopmu_itl, offset))
176 #define BUS_SPACE_WRT4_MV0(offset, value) bus_space_write_4(hba->bar0t,\
177 hba->bar0h, offsetof(struct hpt_iopmv_regs, offset), value)
178 #define BUS_SPACE_RD4_MV0(offset) bus_space_read_4(hba->bar0t,\
179 hba->bar0h, offsetof(struct hpt_iopmv_regs, offset))
180 #define BUS_SPACE_WRT4_MV2(offset, value) bus_space_write_4(hba->bar2t,\
181 hba->bar2h, offsetof(struct hpt_iopmu_mv, offset), value)
182 #define BUS_SPACE_RD4_MV2(offset) bus_space_read_4(hba->bar2t,\
183 hba->bar2h, offsetof(struct hpt_iopmu_mv, offset))
185 #define BUS_SPACE_WRT4_MVFREY2(offset, value) bus_space_write_4(hba->bar2t,\
186 hba->bar2h, offsetof(struct hpt_iopmu_mvfrey, offset), value)
187 #define BUS_SPACE_RD4_MVFREY2(offset) bus_space_read_4(hba->bar2t,\
188 hba->bar2h, offsetof(struct hpt_iopmu_mvfrey, offset))
190 static int hptiop_open(ioctl_dev_t dev, int flags,
191 int devtype, ioctl_thread_t proc)
193 struct hpt_iop_hba *hba = hba_from_dev(dev);
197 if (hba->flag & HPT_IOCTL_FLAG_OPEN)
199 hba->flag |= HPT_IOCTL_FLAG_OPEN;
203 static int hptiop_close(ioctl_dev_t dev, int flags,
204 int devtype, ioctl_thread_t proc)
206 struct hpt_iop_hba *hba = hba_from_dev(dev);
207 hba->flag &= ~(u_int32_t)HPT_IOCTL_FLAG_OPEN;
211 static int hptiop_ioctl(ioctl_dev_t dev, u_long cmd, caddr_t data,
212 int flags, ioctl_thread_t proc)
215 struct hpt_iop_hba *hba = hba_from_dev(dev);
220 case HPT_DO_IOCONTROL:
221 ret = hba->ops->do_ioctl(hba,
222 (struct hpt_iop_ioctl_param *)data);
225 ret = hptiop_rescan_bus(hba);
234 static u_int64_t hptiop_mv_outbound_read(struct hpt_iop_hba *hba)
237 u_int32_t outbound_tail = BUS_SPACE_RD4_MV2(outbound_tail);
238 u_int32_t outbound_head = BUS_SPACE_RD4_MV2(outbound_head);
240 if (outbound_tail != outbound_head) {
241 bus_space_read_region_4(hba->bar2t, hba->bar2h,
242 offsetof(struct hpt_iopmu_mv,
243 outbound_q[outbound_tail]),
248 if (outbound_tail == MVIOP_QUEUE_LEN)
251 BUS_SPACE_WRT4_MV2(outbound_tail, outbound_tail);
257 static void hptiop_mv_inbound_write(u_int64_t p, struct hpt_iop_hba *hba)
259 u_int32_t inbound_head = BUS_SPACE_RD4_MV2(inbound_head);
260 u_int32_t head = inbound_head + 1;
262 if (head == MVIOP_QUEUE_LEN)
265 bus_space_write_region_4(hba->bar2t, hba->bar2h,
266 offsetof(struct hpt_iopmu_mv, inbound_q[inbound_head]),
268 BUS_SPACE_WRT4_MV2(inbound_head, head);
269 BUS_SPACE_WRT4_MV0(inbound_doorbell, MVIOP_MU_INBOUND_INT_POSTQUEUE);
272 static void hptiop_post_msg_itl(struct hpt_iop_hba *hba, u_int32_t msg)
274 BUS_SPACE_WRT4_ITL(inbound_msgaddr0, msg);
275 BUS_SPACE_RD4_ITL(outbound_intstatus);
278 static void hptiop_post_msg_mv(struct hpt_iop_hba *hba, u_int32_t msg)
281 BUS_SPACE_WRT4_MV2(inbound_msg, msg);
282 BUS_SPACE_WRT4_MV0(inbound_doorbell, MVIOP_MU_INBOUND_INT_MSG);
284 BUS_SPACE_RD4_MV0(outbound_intmask);
287 static void hptiop_post_msg_mvfrey(struct hpt_iop_hba *hba, u_int32_t msg)
289 BUS_SPACE_WRT4_MVFREY2(f0_to_cpu_msg_a, msg);
290 BUS_SPACE_RD4_MVFREY2(f0_to_cpu_msg_a);
293 static int hptiop_wait_ready_itl(struct hpt_iop_hba * hba, u_int32_t millisec)
298 for (i = 0; i < millisec; i++) {
299 req = BUS_SPACE_RD4_ITL(inbound_queue);
300 if (req != IOPMU_QUEUE_EMPTY)
305 if (req!=IOPMU_QUEUE_EMPTY) {
306 BUS_SPACE_WRT4_ITL(outbound_queue, req);
307 BUS_SPACE_RD4_ITL(outbound_intstatus);
314 static int hptiop_wait_ready_mv(struct hpt_iop_hba * hba, u_int32_t millisec)
316 if (hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_NOP, millisec))
322 static int hptiop_wait_ready_mvfrey(struct hpt_iop_hba * hba,
325 if (hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_NOP, millisec))
331 static void hptiop_request_callback_itl(struct hpt_iop_hba * hba,
334 struct hpt_iop_srb *srb;
335 struct hpt_iop_request_scsi_command *req=NULL;
338 u_int32_t result, temp, dxfer;
341 if (index & IOPMU_QUEUE_MASK_HOST_BITS) { /*host req*/
342 if (hba->firmware_version > 0x01020000 ||
343 hba->interface_version > 0x01020000) {
344 srb = hba->srb[index & ~(u_int32_t)
345 (IOPMU_QUEUE_ADDR_HOST_BIT
346 | IOPMU_QUEUE_REQUEST_RESULT_BIT)];
347 req = (struct hpt_iop_request_scsi_command *)srb;
348 if (index & IOPMU_QUEUE_REQUEST_RESULT_BIT)
349 result = IOP_RESULT_SUCCESS;
351 result = req->header.result;
353 srb = hba->srb[index &
354 ~(u_int32_t)IOPMU_QUEUE_ADDR_HOST_BIT];
355 req = (struct hpt_iop_request_scsi_command *)srb;
356 result = req->header.result;
358 dxfer = req->dataxfer_length;
363 temp = bus_space_read_4(hba->bar0t, hba->bar0h, index +
364 offsetof(struct hpt_iop_request_header, type));
365 result = bus_space_read_4(hba->bar0t, hba->bar0h, index +
366 offsetof(struct hpt_iop_request_header, result));
368 case IOP_REQUEST_TYPE_IOCTL_COMMAND:
371 bus_space_write_region_4(hba->bar0t, hba->bar0h, index +
372 offsetof(struct hpt_iop_request_header, context),
373 (u_int32_t *)&temp64, 2);
374 wakeup((void *)((unsigned long)hba->u.itl.mu + index));
378 case IOP_REQUEST_TYPE_SCSI_COMMAND:
379 bus_space_read_region_4(hba->bar0t, hba->bar0h, index +
380 offsetof(struct hpt_iop_request_header, context),
381 (u_int32_t *)&temp64, 2);
382 srb = (struct hpt_iop_srb *)(unsigned long)temp64;
383 dxfer = bus_space_read_4(hba->bar0t, hba->bar0h,
384 index + offsetof(struct hpt_iop_request_scsi_command,
387 ccb = (union ccb *)srb->ccb;
388 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
389 cdb = ccb->csio.cdb_io.cdb_ptr;
391 cdb = ccb->csio.cdb_io.cdb_bytes;
393 if (cdb[0] == SYNCHRONIZE_CACHE) { /* ??? */
394 ccb->ccb_h.status = CAM_REQ_CMP;
399 case IOP_RESULT_SUCCESS:
400 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
402 bus_dmamap_sync(hba->io_dmat,
403 srb->dma_map, BUS_DMASYNC_POSTREAD);
404 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
407 bus_dmamap_sync(hba->io_dmat,
408 srb->dma_map, BUS_DMASYNC_POSTWRITE);
409 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
413 ccb->ccb_h.status = CAM_REQ_CMP;
416 case IOP_RESULT_BAD_TARGET:
417 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
419 case IOP_RESULT_BUSY:
420 ccb->ccb_h.status = CAM_BUSY;
422 case IOP_RESULT_INVALID_REQUEST:
423 ccb->ccb_h.status = CAM_REQ_INVALID;
425 case IOP_RESULT_FAIL:
426 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
428 case IOP_RESULT_RESET:
429 ccb->ccb_h.status = CAM_BUSY;
431 case IOP_RESULT_CHECK_CONDITION:
432 memset(&ccb->csio.sense_data, 0,
433 sizeof(ccb->csio.sense_data));
434 if (dxfer < ccb->csio.sense_len)
435 ccb->csio.sense_resid = ccb->csio.sense_len -
438 ccb->csio.sense_resid = 0;
439 if (srb->srb_flag & HPT_SRB_FLAG_HIGH_MEM_ACESS) {/*iop*/
440 bus_space_read_region_1(hba->bar0t, hba->bar0h,
441 index + offsetof(struct hpt_iop_request_scsi_command,
442 sg_list), (u_int8_t *)&ccb->csio.sense_data,
443 MIN(dxfer, sizeof(ccb->csio.sense_data)));
445 memcpy(&ccb->csio.sense_data, &req->sg_list,
446 MIN(dxfer, sizeof(ccb->csio.sense_data)));
448 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
449 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
450 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
453 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
457 if (srb->srb_flag & HPT_SRB_FLAG_HIGH_MEM_ACESS)
458 BUS_SPACE_WRT4_ITL(outbound_queue, index);
460 ccb->csio.resid = ccb->csio.dxfer_len - dxfer;
462 hptiop_free_srb(hba, srb);
468 static void hptiop_drain_outbound_queue_itl(struct hpt_iop_hba *hba)
472 while ((req = BUS_SPACE_RD4_ITL(outbound_queue)) !=IOPMU_QUEUE_EMPTY) {
473 if (req & IOPMU_QUEUE_MASK_HOST_BITS)
474 hptiop_request_callback_itl(hba, req);
476 struct hpt_iop_request_header *p;
478 p = (struct hpt_iop_request_header *)
479 ((char *)hba->u.itl.mu + req);
480 temp = bus_space_read_4(hba->bar0t,
482 offsetof(struct hpt_iop_request_header,
484 if (temp & IOP_REQUEST_FLAG_SYNC_REQUEST) {
486 bus_space_read_region_4(hba->bar0t,
488 offsetof(struct hpt_iop_request_header,
490 (u_int32_t *)&temp64, 2);
492 hptiop_request_callback_itl(hba, req);
495 bus_space_write_region_4(hba->bar0t,
497 offsetof(struct hpt_iop_request_header,
499 (u_int32_t *)&temp64, 2);
502 hptiop_request_callback_itl(hba, req);
507 static int hptiop_intr_itl(struct hpt_iop_hba * hba)
512 status = BUS_SPACE_RD4_ITL(outbound_intstatus);
514 if (status & IOPMU_OUTBOUND_INT_MSG0) {
515 u_int32_t msg = BUS_SPACE_RD4_ITL(outbound_msgaddr0);
516 KdPrint(("hptiop: received outbound msg %x\n", msg));
517 BUS_SPACE_WRT4_ITL(outbound_intstatus, IOPMU_OUTBOUND_INT_MSG0);
518 hptiop_os_message_callback(hba, msg);
522 if (status & IOPMU_OUTBOUND_INT_POSTQUEUE) {
523 hptiop_drain_outbound_queue_itl(hba);
530 static void hptiop_request_callback_mv(struct hpt_iop_hba * hba,
533 u_int32_t context = (u_int32_t)_tag;
535 if (context & MVIOP_CMD_TYPE_SCSI) {
536 struct hpt_iop_srb *srb;
537 struct hpt_iop_request_scsi_command *req;
541 srb = hba->srb[context >> MVIOP_REQUEST_NUMBER_START_BIT];
542 req = (struct hpt_iop_request_scsi_command *)srb;
543 ccb = (union ccb *)srb->ccb;
544 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
545 cdb = ccb->csio.cdb_io.cdb_ptr;
547 cdb = ccb->csio.cdb_io.cdb_bytes;
549 if (cdb[0] == SYNCHRONIZE_CACHE) { /* ??? */
550 ccb->ccb_h.status = CAM_REQ_CMP;
553 if (context & MVIOP_MU_QUEUE_REQUEST_RESULT_BIT)
554 req->header.result = IOP_RESULT_SUCCESS;
556 switch (req->header.result) {
557 case IOP_RESULT_SUCCESS:
558 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
560 bus_dmamap_sync(hba->io_dmat,
561 srb->dma_map, BUS_DMASYNC_POSTREAD);
562 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
565 bus_dmamap_sync(hba->io_dmat,
566 srb->dma_map, BUS_DMASYNC_POSTWRITE);
567 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
570 ccb->ccb_h.status = CAM_REQ_CMP;
572 case IOP_RESULT_BAD_TARGET:
573 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
575 case IOP_RESULT_BUSY:
576 ccb->ccb_h.status = CAM_BUSY;
578 case IOP_RESULT_INVALID_REQUEST:
579 ccb->ccb_h.status = CAM_REQ_INVALID;
581 case IOP_RESULT_FAIL:
582 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
584 case IOP_RESULT_RESET:
585 ccb->ccb_h.status = CAM_BUSY;
587 case IOP_RESULT_CHECK_CONDITION:
588 memset(&ccb->csio.sense_data, 0,
589 sizeof(ccb->csio.sense_data));
590 if (req->dataxfer_length < ccb->csio.sense_len)
591 ccb->csio.sense_resid = ccb->csio.sense_len -
592 req->dataxfer_length;
594 ccb->csio.sense_resid = 0;
595 memcpy(&ccb->csio.sense_data, &req->sg_list,
596 MIN(req->dataxfer_length, sizeof(ccb->csio.sense_data)));
597 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
598 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
599 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
602 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
606 ccb->csio.resid = ccb->csio.dxfer_len - req->dataxfer_length;
608 hptiop_free_srb(hba, srb);
610 } else if (context & MVIOP_CMD_TYPE_IOCTL) {
611 struct hpt_iop_request_ioctl_command *req = hba->ctlcfg_ptr;
612 if (context & MVIOP_MU_QUEUE_REQUEST_RESULT_BIT)
613 hba->config_done = 1;
615 hba->config_done = -1;
618 (MVIOP_CMD_TYPE_SET_CONFIG |
619 MVIOP_CMD_TYPE_GET_CONFIG))
620 hba->config_done = 1;
622 device_printf(hba->pcidev, "wrong callback type\n");
626 static void hptiop_request_callback_mvfrey(struct hpt_iop_hba * hba,
629 u_int32_t req_type = _tag & 0xf;
631 struct hpt_iop_srb *srb;
632 struct hpt_iop_request_scsi_command *req;
637 case IOP_REQUEST_TYPE_GET_CONFIG:
638 case IOP_REQUEST_TYPE_SET_CONFIG:
639 hba->config_done = 1;
642 case IOP_REQUEST_TYPE_SCSI_COMMAND:
643 srb = hba->srb[(_tag >> 4) & 0xff];
644 req = (struct hpt_iop_request_scsi_command *)srb;
646 ccb = (union ccb *)srb->ccb;
648 callout_stop(&srb->timeout);
650 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
651 cdb = ccb->csio.cdb_io.cdb_ptr;
653 cdb = ccb->csio.cdb_io.cdb_bytes;
655 if (cdb[0] == SYNCHRONIZE_CACHE) { /* ??? */
656 ccb->ccb_h.status = CAM_REQ_CMP;
660 if (_tag & MVFREYIOPMU_QUEUE_REQUEST_RESULT_BIT)
661 req->header.result = IOP_RESULT_SUCCESS;
663 switch (req->header.result) {
664 case IOP_RESULT_SUCCESS:
665 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
667 bus_dmamap_sync(hba->io_dmat,
668 srb->dma_map, BUS_DMASYNC_POSTREAD);
669 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
672 bus_dmamap_sync(hba->io_dmat,
673 srb->dma_map, BUS_DMASYNC_POSTWRITE);
674 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
677 ccb->ccb_h.status = CAM_REQ_CMP;
679 case IOP_RESULT_BAD_TARGET:
680 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
682 case IOP_RESULT_BUSY:
683 ccb->ccb_h.status = CAM_BUSY;
685 case IOP_RESULT_INVALID_REQUEST:
686 ccb->ccb_h.status = CAM_REQ_INVALID;
688 case IOP_RESULT_FAIL:
689 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
691 case IOP_RESULT_RESET:
692 ccb->ccb_h.status = CAM_BUSY;
694 case IOP_RESULT_CHECK_CONDITION:
695 memset(&ccb->csio.sense_data, 0,
696 sizeof(ccb->csio.sense_data));
697 if (req->dataxfer_length < ccb->csio.sense_len)
698 ccb->csio.sense_resid = ccb->csio.sense_len -
699 req->dataxfer_length;
701 ccb->csio.sense_resid = 0;
702 memcpy(&ccb->csio.sense_data, &req->sg_list,
703 MIN(req->dataxfer_length, sizeof(ccb->csio.sense_data)));
704 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
705 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
706 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
709 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
713 ccb->csio.resid = ccb->csio.dxfer_len - req->dataxfer_length;
715 hptiop_free_srb(hba, srb);
718 case IOP_REQUEST_TYPE_IOCTL_COMMAND:
719 if (_tag & MVFREYIOPMU_QUEUE_REQUEST_RESULT_BIT)
720 hba->config_done = 1;
722 hba->config_done = -1;
723 wakeup((struct hpt_iop_request_ioctl_command *)hba->ctlcfg_ptr);
726 device_printf(hba->pcidev, "wrong callback type\n");
731 static void hptiop_drain_outbound_queue_mv(struct hpt_iop_hba * hba)
735 while ((req = hptiop_mv_outbound_read(hba))) {
736 if (req & MVIOP_MU_QUEUE_ADDR_HOST_BIT) {
737 if (req & MVIOP_MU_QUEUE_REQUEST_RETURN_CONTEXT) {
738 hptiop_request_callback_mv(hba, req);
744 static int hptiop_intr_mv(struct hpt_iop_hba * hba)
749 status = BUS_SPACE_RD4_MV0(outbound_doorbell);
752 BUS_SPACE_WRT4_MV0(outbound_doorbell, ~status);
754 if (status & MVIOP_MU_OUTBOUND_INT_MSG) {
755 u_int32_t msg = BUS_SPACE_RD4_MV2(outbound_msg);
756 KdPrint(("hptiop: received outbound msg %x\n", msg));
757 hptiop_os_message_callback(hba, msg);
761 if (status & MVIOP_MU_OUTBOUND_INT_POSTQUEUE) {
762 hptiop_drain_outbound_queue_mv(hba);
769 static int hptiop_intr_mvfrey(struct hpt_iop_hba * hba)
771 u_int32_t status, _tag, cptr;
774 if (hba->initialized) {
775 BUS_SPACE_WRT4_MVFREY2(pcie_f0_int_enable, 0);
778 status = BUS_SPACE_RD4_MVFREY2(f0_doorbell);
780 BUS_SPACE_WRT4_MVFREY2(f0_doorbell, status);
781 if (status & CPU_TO_F0_DRBL_MSG_A_BIT) {
782 u_int32_t msg = BUS_SPACE_RD4_MVFREY2(cpu_to_f0_msg_a);
783 hptiop_os_message_callback(hba, msg);
788 status = BUS_SPACE_RD4_MVFREY2(isr_cause);
790 BUS_SPACE_WRT4_MVFREY2(isr_cause, status);
792 cptr = *hba->u.mvfrey.outlist_cptr & 0xff;
793 while (hba->u.mvfrey.outlist_rptr != cptr) {
794 hba->u.mvfrey.outlist_rptr++;
795 if (hba->u.mvfrey.outlist_rptr == hba->u.mvfrey.list_count) {
796 hba->u.mvfrey.outlist_rptr = 0;
799 _tag = hba->u.mvfrey.outlist[hba->u.mvfrey.outlist_rptr].val;
800 hptiop_request_callback_mvfrey(hba, _tag);
803 } while (cptr != (*hba->u.mvfrey.outlist_cptr & 0xff));
806 if (hba->initialized) {
807 BUS_SPACE_WRT4_MVFREY2(pcie_f0_int_enable, 0x1010);
813 static int hptiop_send_sync_request_itl(struct hpt_iop_hba * hba,
814 u_int32_t req32, u_int32_t millisec)
819 BUS_SPACE_WRT4_ITL(inbound_queue, req32);
820 BUS_SPACE_RD4_ITL(outbound_intstatus);
822 for (i = 0; i < millisec; i++) {
823 hptiop_intr_itl(hba);
824 bus_space_read_region_4(hba->bar0t, hba->bar0h, req32 +
825 offsetof(struct hpt_iop_request_header, context),
826 (u_int32_t *)&temp64, 2);
835 static int hptiop_send_sync_request_mv(struct hpt_iop_hba *hba,
836 void *req, u_int32_t millisec)
840 hba->config_done = 0;
842 phy_addr = hba->ctlcfgcmd_phy |
843 (u_int64_t)MVIOP_MU_QUEUE_ADDR_HOST_BIT;
844 ((struct hpt_iop_request_get_config *)req)->header.flags |=
845 IOP_REQUEST_FLAG_SYNC_REQUEST |
846 IOP_REQUEST_FLAG_OUTPUT_CONTEXT;
847 hptiop_mv_inbound_write(phy_addr, hba);
848 BUS_SPACE_RD4_MV0(outbound_intmask);
850 for (i = 0; i < millisec; i++) {
852 if (hba->config_done)
859 static int hptiop_send_sync_request_mvfrey(struct hpt_iop_hba *hba,
860 void *req, u_int32_t millisec)
864 struct hpt_iop_request_header *reqhdr =
865 (struct hpt_iop_request_header *)req;
867 hba->config_done = 0;
869 phy_addr = hba->ctlcfgcmd_phy;
870 reqhdr->flags = IOP_REQUEST_FLAG_SYNC_REQUEST
871 | IOP_REQUEST_FLAG_OUTPUT_CONTEXT
872 | IOP_REQUEST_FLAG_ADDR_BITS
873 | ((phy_addr >> 16) & 0xffff0000);
874 reqhdr->context = ((phy_addr & 0xffffffff) << 32 )
875 | IOPMU_QUEUE_ADDR_HOST_BIT | reqhdr->type;
877 hba->u.mvfrey.inlist_wptr++;
878 index = hba->u.mvfrey.inlist_wptr & 0x3fff;
880 if (index == hba->u.mvfrey.list_count) {
882 hba->u.mvfrey.inlist_wptr &= ~0x3fff;
883 hba->u.mvfrey.inlist_wptr ^= CL_POINTER_TOGGLE;
886 hba->u.mvfrey.inlist[index].addr = phy_addr;
887 hba->u.mvfrey.inlist[index].intrfc_len = (reqhdr->size + 3) / 4;
889 BUS_SPACE_WRT4_MVFREY2(inbound_write_ptr, hba->u.mvfrey.inlist_wptr);
890 BUS_SPACE_RD4_MVFREY2(inbound_write_ptr);
892 for (i = 0; i < millisec; i++) {
893 hptiop_intr_mvfrey(hba);
894 if (hba->config_done)
901 static int hptiop_send_sync_msg(struct hpt_iop_hba *hba,
902 u_int32_t msg, u_int32_t millisec)
907 hba->ops->post_msg(hba, msg);
909 for (i=0; i<millisec; i++) {
910 hba->ops->iop_intr(hba);
916 return hba->msg_done? 0 : -1;
919 static int hptiop_get_config_itl(struct hpt_iop_hba * hba,
920 struct hpt_iop_request_get_config * config)
924 config->header.size = sizeof(struct hpt_iop_request_get_config);
925 config->header.type = IOP_REQUEST_TYPE_GET_CONFIG;
926 config->header.flags = IOP_REQUEST_FLAG_SYNC_REQUEST;
927 config->header.result = IOP_RESULT_PENDING;
928 config->header.context = 0;
930 req32 = BUS_SPACE_RD4_ITL(inbound_queue);
931 if (req32 == IOPMU_QUEUE_EMPTY)
934 bus_space_write_region_4(hba->bar0t, hba->bar0h,
935 req32, (u_int32_t *)config,
936 sizeof(struct hpt_iop_request_header) >> 2);
938 if (hptiop_send_sync_request_itl(hba, req32, 20000)) {
939 KdPrint(("hptiop: get config send cmd failed"));
943 bus_space_read_region_4(hba->bar0t, hba->bar0h,
944 req32, (u_int32_t *)config,
945 sizeof(struct hpt_iop_request_get_config) >> 2);
947 BUS_SPACE_WRT4_ITL(outbound_queue, req32);
952 static int hptiop_get_config_mv(struct hpt_iop_hba * hba,
953 struct hpt_iop_request_get_config * config)
955 struct hpt_iop_request_get_config *req;
957 if (!(req = hba->ctlcfg_ptr))
960 req->header.flags = 0;
961 req->header.type = IOP_REQUEST_TYPE_GET_CONFIG;
962 req->header.size = sizeof(struct hpt_iop_request_get_config);
963 req->header.result = IOP_RESULT_PENDING;
964 req->header.context = MVIOP_CMD_TYPE_GET_CONFIG;
966 if (hptiop_send_sync_request_mv(hba, req, 20000)) {
967 KdPrint(("hptiop: get config send cmd failed"));
975 static int hptiop_get_config_mvfrey(struct hpt_iop_hba * hba,
976 struct hpt_iop_request_get_config * config)
978 struct hpt_iop_request_get_config *info = hba->u.mvfrey.config;
980 if (info->header.size != sizeof(struct hpt_iop_request_get_config) ||
981 info->header.type != IOP_REQUEST_TYPE_GET_CONFIG) {
982 KdPrint(("hptiop: header size %x/%x type %x/%x",
983 info->header.size, (int)sizeof(struct hpt_iop_request_get_config),
984 info->header.type, IOP_REQUEST_TYPE_GET_CONFIG));
988 config->interface_version = info->interface_version;
989 config->firmware_version = info->firmware_version;
990 config->max_requests = info->max_requests;
991 config->request_size = info->request_size;
992 config->max_sg_count = info->max_sg_count;
993 config->data_transfer_length = info->data_transfer_length;
994 config->alignment_mask = info->alignment_mask;
995 config->max_devices = info->max_devices;
996 config->sdram_size = info->sdram_size;
998 KdPrint(("hptiop: maxreq %x reqsz %x datalen %x maxdev %x sdram %x",
999 config->max_requests, config->request_size,
1000 config->data_transfer_length, config->max_devices,
1001 config->sdram_size));
1006 static int hptiop_set_config_itl(struct hpt_iop_hba *hba,
1007 struct hpt_iop_request_set_config *config)
1011 req32 = BUS_SPACE_RD4_ITL(inbound_queue);
1013 if (req32 == IOPMU_QUEUE_EMPTY)
1016 config->header.size = sizeof(struct hpt_iop_request_set_config);
1017 config->header.type = IOP_REQUEST_TYPE_SET_CONFIG;
1018 config->header.flags = IOP_REQUEST_FLAG_SYNC_REQUEST;
1019 config->header.result = IOP_RESULT_PENDING;
1020 config->header.context = 0;
1022 bus_space_write_region_4(hba->bar0t, hba->bar0h, req32,
1023 (u_int32_t *)config,
1024 sizeof(struct hpt_iop_request_set_config) >> 2);
1026 if (hptiop_send_sync_request_itl(hba, req32, 20000)) {
1027 KdPrint(("hptiop: set config send cmd failed"));
1031 BUS_SPACE_WRT4_ITL(outbound_queue, req32);
1036 static int hptiop_set_config_mv(struct hpt_iop_hba *hba,
1037 struct hpt_iop_request_set_config *config)
1039 struct hpt_iop_request_set_config *req;
1041 if (!(req = hba->ctlcfg_ptr))
1044 memcpy((u_int8_t *)req + sizeof(struct hpt_iop_request_header),
1045 (u_int8_t *)config + sizeof(struct hpt_iop_request_header),
1046 sizeof(struct hpt_iop_request_set_config) -
1047 sizeof(struct hpt_iop_request_header));
1049 req->header.flags = 0;
1050 req->header.type = IOP_REQUEST_TYPE_SET_CONFIG;
1051 req->header.size = sizeof(struct hpt_iop_request_set_config);
1052 req->header.result = IOP_RESULT_PENDING;
1053 req->header.context = MVIOP_CMD_TYPE_SET_CONFIG;
1055 if (hptiop_send_sync_request_mv(hba, req, 20000)) {
1056 KdPrint(("hptiop: set config send cmd failed"));
1063 static int hptiop_set_config_mvfrey(struct hpt_iop_hba *hba,
1064 struct hpt_iop_request_set_config *config)
1066 struct hpt_iop_request_set_config *req;
1068 if (!(req = hba->ctlcfg_ptr))
1071 memcpy((u_int8_t *)req + sizeof(struct hpt_iop_request_header),
1072 (u_int8_t *)config + sizeof(struct hpt_iop_request_header),
1073 sizeof(struct hpt_iop_request_set_config) -
1074 sizeof(struct hpt_iop_request_header));
1076 req->header.type = IOP_REQUEST_TYPE_SET_CONFIG;
1077 req->header.size = sizeof(struct hpt_iop_request_set_config);
1078 req->header.result = IOP_RESULT_PENDING;
1080 if (hptiop_send_sync_request_mvfrey(hba, req, 20000)) {
1081 KdPrint(("hptiop: set config send cmd failed"));
1088 static int hptiop_post_ioctl_command_itl(struct hpt_iop_hba *hba,
1090 struct hpt_iop_ioctl_param *pParams)
1093 struct hpt_iop_request_ioctl_command req;
1095 if ((((pParams->nInBufferSize + 3) & ~3) + pParams->nOutBufferSize) >
1096 (hba->max_request_size -
1097 offsetof(struct hpt_iop_request_ioctl_command, buf))) {
1098 device_printf(hba->pcidev, "request size beyond max value");
1102 req.header.size = offsetof(struct hpt_iop_request_ioctl_command, buf)
1103 + pParams->nInBufferSize;
1104 req.header.type = IOP_REQUEST_TYPE_IOCTL_COMMAND;
1105 req.header.flags = IOP_REQUEST_FLAG_SYNC_REQUEST;
1106 req.header.result = IOP_RESULT_PENDING;
1107 req.header.context = req32 + (u_int64_t)(unsigned long)hba->u.itl.mu;
1108 req.ioctl_code = HPT_CTL_CODE_BSD_TO_IOP(pParams->dwIoControlCode);
1109 req.inbuf_size = pParams->nInBufferSize;
1110 req.outbuf_size = pParams->nOutBufferSize;
1111 req.bytes_returned = 0;
1113 bus_space_write_region_4(hba->bar0t, hba->bar0h, req32, (u_int32_t *)&req,
1114 offsetof(struct hpt_iop_request_ioctl_command, buf)>>2);
1116 hptiop_lock_adapter(hba);
1118 BUS_SPACE_WRT4_ITL(inbound_queue, req32);
1119 BUS_SPACE_RD4_ITL(outbound_intstatus);
1121 bus_space_read_region_4(hba->bar0t, hba->bar0h, req32 +
1122 offsetof(struct hpt_iop_request_ioctl_command, header.context),
1123 (u_int32_t *)&temp64, 2);
1125 if (hptiop_sleep(hba, (void *)((unsigned long)hba->u.itl.mu + req32),
1126 PPAUSE, "hptctl", HPT_OSM_TIMEOUT)==0)
1128 hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET, 60000);
1129 bus_space_read_region_4(hba->bar0t, hba->bar0h,req32 +
1130 offsetof(struct hpt_iop_request_ioctl_command,
1132 (u_int32_t *)&temp64, 2);
1135 hptiop_unlock_adapter(hba);
1139 static int hptiop_bus_space_copyin(struct hpt_iop_hba *hba, u_int32_t bus,
1140 void *user, int size)
1145 for (i=0; i<size; i++) {
1146 if (copyin((u_int8_t *)user + i, &byte, 1))
1148 bus_space_write_1(hba->bar0t, hba->bar0h, bus + i, byte);
1154 static int hptiop_bus_space_copyout(struct hpt_iop_hba *hba, u_int32_t bus,
1155 void *user, int size)
1160 for (i=0; i<size; i++) {
1161 byte = bus_space_read_1(hba->bar0t, hba->bar0h, bus + i);
1162 if (copyout(&byte, (u_int8_t *)user + i, 1))
1169 static int hptiop_do_ioctl_itl(struct hpt_iop_hba *hba,
1170 struct hpt_iop_ioctl_param * pParams)
1175 if ((pParams->Magic != HPT_IOCTL_MAGIC) &&
1176 (pParams->Magic != HPT_IOCTL_MAGIC32))
1179 req32 = BUS_SPACE_RD4_ITL(inbound_queue);
1180 if (req32 == IOPMU_QUEUE_EMPTY)
1183 if (pParams->nInBufferSize)
1184 if (hptiop_bus_space_copyin(hba, req32 +
1185 offsetof(struct hpt_iop_request_ioctl_command, buf),
1186 (void *)pParams->lpInBuffer, pParams->nInBufferSize))
1189 if (hptiop_post_ioctl_command_itl(hba, req32, pParams))
1192 result = bus_space_read_4(hba->bar0t, hba->bar0h, req32 +
1193 offsetof(struct hpt_iop_request_ioctl_command,
1196 if (result == IOP_RESULT_SUCCESS) {
1197 if (pParams->nOutBufferSize)
1198 if (hptiop_bus_space_copyout(hba, req32 +
1199 offsetof(struct hpt_iop_request_ioctl_command, buf) +
1200 ((pParams->nInBufferSize + 3) & ~3),
1201 (void *)pParams->lpOutBuffer, pParams->nOutBufferSize))
1204 if (pParams->lpBytesReturned) {
1205 if (hptiop_bus_space_copyout(hba, req32 +
1206 offsetof(struct hpt_iop_request_ioctl_command, bytes_returned),
1207 (void *)pParams->lpBytesReturned, sizeof(unsigned long)))
1211 BUS_SPACE_WRT4_ITL(outbound_queue, req32);
1216 BUS_SPACE_WRT4_ITL(outbound_queue, req32);
1222 static int hptiop_post_ioctl_command_mv(struct hpt_iop_hba *hba,
1223 struct hpt_iop_request_ioctl_command *req,
1224 struct hpt_iop_ioctl_param *pParams)
1229 if ((((pParams->nInBufferSize + 3) & ~3) + pParams->nOutBufferSize) >
1230 (hba->max_request_size -
1231 offsetof(struct hpt_iop_request_ioctl_command, buf))) {
1232 device_printf(hba->pcidev, "request size beyond max value");
1236 req->ioctl_code = HPT_CTL_CODE_BSD_TO_IOP(pParams->dwIoControlCode);
1237 req->inbuf_size = pParams->nInBufferSize;
1238 req->outbuf_size = pParams->nOutBufferSize;
1239 req->header.size = offsetof(struct hpt_iop_request_ioctl_command, buf)
1240 + pParams->nInBufferSize;
1241 req->header.context = (u_int64_t)MVIOP_CMD_TYPE_IOCTL;
1242 req->header.type = IOP_REQUEST_TYPE_IOCTL_COMMAND;
1243 req->header.result = IOP_RESULT_PENDING;
1244 req->header.flags = IOP_REQUEST_FLAG_OUTPUT_CONTEXT;
1245 size = req->header.size >> 8;
1246 size = imin(3, size);
1247 req_phy = hba->ctlcfgcmd_phy | MVIOP_MU_QUEUE_ADDR_HOST_BIT | size;
1248 hptiop_mv_inbound_write(req_phy, hba);
1250 BUS_SPACE_RD4_MV0(outbound_intmask);
1252 while (hba->config_done == 0) {
1253 if (hptiop_sleep(hba, req, PPAUSE,
1254 "hptctl", HPT_OSM_TIMEOUT)==0)
1256 hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET, 60000);
1261 static int hptiop_do_ioctl_mv(struct hpt_iop_hba *hba,
1262 struct hpt_iop_ioctl_param *pParams)
1264 struct hpt_iop_request_ioctl_command *req;
1266 if ((pParams->Magic != HPT_IOCTL_MAGIC) &&
1267 (pParams->Magic != HPT_IOCTL_MAGIC32))
1270 req = (struct hpt_iop_request_ioctl_command *)(hba->ctlcfg_ptr);
1271 hba->config_done = 0;
1272 hptiop_lock_adapter(hba);
1273 if (pParams->nInBufferSize)
1274 if (copyin((void *)pParams->lpInBuffer,
1275 req->buf, pParams->nInBufferSize))
1277 if (hptiop_post_ioctl_command_mv(hba, req, pParams))
1280 if (hba->config_done == 1) {
1281 if (pParams->nOutBufferSize)
1282 if (copyout(req->buf +
1283 ((pParams->nInBufferSize + 3) & ~3),
1284 (void *)pParams->lpOutBuffer,
1285 pParams->nOutBufferSize))
1288 if (pParams->lpBytesReturned)
1289 if (copyout(&req->bytes_returned,
1290 (void*)pParams->lpBytesReturned,
1293 hptiop_unlock_adapter(hba);
1297 hptiop_unlock_adapter(hba);
1302 static int hptiop_post_ioctl_command_mvfrey(struct hpt_iop_hba *hba,
1303 struct hpt_iop_request_ioctl_command *req,
1304 struct hpt_iop_ioctl_param *pParams)
1309 phy_addr = hba->ctlcfgcmd_phy;
1311 if ((((pParams->nInBufferSize + 3) & ~3) + pParams->nOutBufferSize) >
1312 (hba->max_request_size -
1313 offsetof(struct hpt_iop_request_ioctl_command, buf))) {
1314 device_printf(hba->pcidev, "request size beyond max value");
1318 req->ioctl_code = HPT_CTL_CODE_BSD_TO_IOP(pParams->dwIoControlCode);
1319 req->inbuf_size = pParams->nInBufferSize;
1320 req->outbuf_size = pParams->nOutBufferSize;
1321 req->header.size = offsetof(struct hpt_iop_request_ioctl_command, buf)
1322 + pParams->nInBufferSize;
1324 req->header.type = IOP_REQUEST_TYPE_IOCTL_COMMAND;
1325 req->header.result = IOP_RESULT_PENDING;
1327 req->header.flags = IOP_REQUEST_FLAG_SYNC_REQUEST
1328 | IOP_REQUEST_FLAG_OUTPUT_CONTEXT
1329 | IOP_REQUEST_FLAG_ADDR_BITS
1330 | ((phy_addr >> 16) & 0xffff0000);
1331 req->header.context = ((phy_addr & 0xffffffff) << 32 )
1332 | IOPMU_QUEUE_ADDR_HOST_BIT | req->header.type;
1334 hba->u.mvfrey.inlist_wptr++;
1335 index = hba->u.mvfrey.inlist_wptr & 0x3fff;
1337 if (index == hba->u.mvfrey.list_count) {
1339 hba->u.mvfrey.inlist_wptr &= ~0x3fff;
1340 hba->u.mvfrey.inlist_wptr ^= CL_POINTER_TOGGLE;
1343 hba->u.mvfrey.inlist[index].addr = phy_addr;
1344 hba->u.mvfrey.inlist[index].intrfc_len = (req->header.size + 3) / 4;
1346 BUS_SPACE_WRT4_MVFREY2(inbound_write_ptr, hba->u.mvfrey.inlist_wptr);
1347 BUS_SPACE_RD4_MVFREY2(inbound_write_ptr);
1349 while (hba->config_done == 0) {
1350 if (hptiop_sleep(hba, req, PPAUSE,
1351 "hptctl", HPT_OSM_TIMEOUT)==0)
1353 hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET, 60000);
1358 static int hptiop_do_ioctl_mvfrey(struct hpt_iop_hba *hba,
1359 struct hpt_iop_ioctl_param *pParams)
1361 struct hpt_iop_request_ioctl_command *req;
1363 if ((pParams->Magic != HPT_IOCTL_MAGIC) &&
1364 (pParams->Magic != HPT_IOCTL_MAGIC32))
1367 req = (struct hpt_iop_request_ioctl_command *)(hba->ctlcfg_ptr);
1368 hba->config_done = 0;
1369 hptiop_lock_adapter(hba);
1370 if (pParams->nInBufferSize)
1371 if (copyin((void *)pParams->lpInBuffer,
1372 req->buf, pParams->nInBufferSize))
1374 if (hptiop_post_ioctl_command_mvfrey(hba, req, pParams))
1377 if (hba->config_done == 1) {
1378 if (pParams->nOutBufferSize)
1379 if (copyout(req->buf +
1380 ((pParams->nInBufferSize + 3) & ~3),
1381 (void *)pParams->lpOutBuffer,
1382 pParams->nOutBufferSize))
1385 if (pParams->lpBytesReturned)
1386 if (copyout(&req->bytes_returned,
1387 (void*)pParams->lpBytesReturned,
1390 hptiop_unlock_adapter(hba);
1394 hptiop_unlock_adapter(hba);
1399 static int hptiop_rescan_bus(struct hpt_iop_hba * hba)
1403 if ((ccb = xpt_alloc_ccb()) == NULL)
1405 if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(hba->sim),
1406 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1414 static bus_dmamap_callback_t hptiop_map_srb;
1415 static bus_dmamap_callback_t hptiop_post_scsi_command;
1416 static bus_dmamap_callback_t hptiop_mv_map_ctlcfg;
1417 static bus_dmamap_callback_t hptiop_mvfrey_map_ctlcfg;
1419 static int hptiop_alloc_pci_res_itl(struct hpt_iop_hba *hba)
1421 hba->bar0_rid = 0x10;
1422 hba->bar0_res = bus_alloc_resource_any(hba->pcidev,
1423 SYS_RES_MEMORY, &hba->bar0_rid, RF_ACTIVE);
1425 if (hba->bar0_res == NULL) {
1426 device_printf(hba->pcidev,
1427 "failed to get iop base adrress.\n");
1430 hba->bar0t = rman_get_bustag(hba->bar0_res);
1431 hba->bar0h = rman_get_bushandle(hba->bar0_res);
1432 hba->u.itl.mu = (struct hpt_iopmu_itl *)
1433 rman_get_virtual(hba->bar0_res);
1435 if (!hba->u.itl.mu) {
1436 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1437 hba->bar0_rid, hba->bar0_res);
1438 device_printf(hba->pcidev, "alloc mem res failed\n");
1445 static int hptiop_alloc_pci_res_mv(struct hpt_iop_hba *hba)
1447 hba->bar0_rid = 0x10;
1448 hba->bar0_res = bus_alloc_resource_any(hba->pcidev,
1449 SYS_RES_MEMORY, &hba->bar0_rid, RF_ACTIVE);
1451 if (hba->bar0_res == NULL) {
1452 device_printf(hba->pcidev, "failed to get iop bar0.\n");
1455 hba->bar0t = rman_get_bustag(hba->bar0_res);
1456 hba->bar0h = rman_get_bushandle(hba->bar0_res);
1457 hba->u.mv.regs = (struct hpt_iopmv_regs *)
1458 rman_get_virtual(hba->bar0_res);
1460 if (!hba->u.mv.regs) {
1461 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1462 hba->bar0_rid, hba->bar0_res);
1463 device_printf(hba->pcidev, "alloc bar0 mem res failed\n");
1467 hba->bar2_rid = 0x18;
1468 hba->bar2_res = bus_alloc_resource_any(hba->pcidev,
1469 SYS_RES_MEMORY, &hba->bar2_rid, RF_ACTIVE);
1471 if (hba->bar2_res == NULL) {
1472 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1473 hba->bar0_rid, hba->bar0_res);
1474 device_printf(hba->pcidev, "failed to get iop bar2.\n");
1478 hba->bar2t = rman_get_bustag(hba->bar2_res);
1479 hba->bar2h = rman_get_bushandle(hba->bar2_res);
1480 hba->u.mv.mu = (struct hpt_iopmu_mv *)rman_get_virtual(hba->bar2_res);
1482 if (!hba->u.mv.mu) {
1483 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1484 hba->bar0_rid, hba->bar0_res);
1485 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1486 hba->bar2_rid, hba->bar2_res);
1487 device_printf(hba->pcidev, "alloc mem bar2 res failed\n");
1494 static int hptiop_alloc_pci_res_mvfrey(struct hpt_iop_hba *hba)
1496 hba->bar0_rid = 0x10;
1497 hba->bar0_res = bus_alloc_resource_any(hba->pcidev,
1498 SYS_RES_MEMORY, &hba->bar0_rid, RF_ACTIVE);
1500 if (hba->bar0_res == NULL) {
1501 device_printf(hba->pcidev, "failed to get iop bar0.\n");
1504 hba->bar0t = rman_get_bustag(hba->bar0_res);
1505 hba->bar0h = rman_get_bushandle(hba->bar0_res);
1506 hba->u.mvfrey.config = (struct hpt_iop_request_get_config *)
1507 rman_get_virtual(hba->bar0_res);
1509 if (!hba->u.mvfrey.config) {
1510 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1511 hba->bar0_rid, hba->bar0_res);
1512 device_printf(hba->pcidev, "alloc bar0 mem res failed\n");
1516 hba->bar2_rid = 0x18;
1517 hba->bar2_res = bus_alloc_resource_any(hba->pcidev,
1518 SYS_RES_MEMORY, &hba->bar2_rid, RF_ACTIVE);
1520 if (hba->bar2_res == NULL) {
1521 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1522 hba->bar0_rid, hba->bar0_res);
1523 device_printf(hba->pcidev, "failed to get iop bar2.\n");
1527 hba->bar2t = rman_get_bustag(hba->bar2_res);
1528 hba->bar2h = rman_get_bushandle(hba->bar2_res);
1530 (struct hpt_iopmu_mvfrey *)rman_get_virtual(hba->bar2_res);
1532 if (!hba->u.mvfrey.mu) {
1533 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1534 hba->bar0_rid, hba->bar0_res);
1535 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1536 hba->bar2_rid, hba->bar2_res);
1537 device_printf(hba->pcidev, "alloc mem bar2 res failed\n");
1544 static void hptiop_release_pci_res_itl(struct hpt_iop_hba *hba)
1547 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1548 hba->bar0_rid, hba->bar0_res);
1551 static void hptiop_release_pci_res_mv(struct hpt_iop_hba *hba)
1554 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1555 hba->bar0_rid, hba->bar0_res);
1557 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1558 hba->bar2_rid, hba->bar2_res);
1561 static void hptiop_release_pci_res_mvfrey(struct hpt_iop_hba *hba)
1564 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1565 hba->bar0_rid, hba->bar0_res);
1567 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1568 hba->bar2_rid, hba->bar2_res);
1571 static int hptiop_internal_memalloc_mv(struct hpt_iop_hba *hba)
1573 if (bus_dma_tag_create(hba->parent_dmat,
1576 BUS_SPACE_MAXADDR_32BIT,
1581 BUS_SPACE_MAXSIZE_32BIT,
1585 &hba->ctlcfg_dmat)) {
1586 device_printf(hba->pcidev, "alloc ctlcfg_dmat failed\n");
1590 if (bus_dmamem_alloc(hba->ctlcfg_dmat, (void **)&hba->ctlcfg_ptr,
1591 BUS_DMA_WAITOK | BUS_DMA_COHERENT,
1592 &hba->ctlcfg_dmamap) != 0) {
1593 device_printf(hba->pcidev,
1594 "bus_dmamem_alloc failed!\n");
1595 bus_dma_tag_destroy(hba->ctlcfg_dmat);
1599 if (bus_dmamap_load(hba->ctlcfg_dmat,
1600 hba->ctlcfg_dmamap, hba->ctlcfg_ptr,
1601 MVIOP_IOCTLCFG_SIZE,
1602 hptiop_mv_map_ctlcfg, hba, 0)) {
1603 device_printf(hba->pcidev, "bus_dmamap_load failed!\n");
1604 if (hba->ctlcfg_dmat) {
1605 bus_dmamem_free(hba->ctlcfg_dmat,
1606 hba->ctlcfg_ptr, hba->ctlcfg_dmamap);
1607 bus_dma_tag_destroy(hba->ctlcfg_dmat);
1615 static int hptiop_internal_memalloc_mvfrey(struct hpt_iop_hba *hba)
1617 u_int32_t list_count = BUS_SPACE_RD4_MVFREY2(inbound_conf_ctl);
1621 if (list_count == 0) {
1625 hba->u.mvfrey.list_count = list_count;
1626 hba->u.mvfrey.internal_mem_size = 0x800
1627 + list_count * sizeof(struct mvfrey_inlist_entry)
1628 + list_count * sizeof(struct mvfrey_outlist_entry)
1630 if (bus_dma_tag_create(hba->parent_dmat,
1633 BUS_SPACE_MAXADDR_32BIT,
1636 hba->u.mvfrey.internal_mem_size,
1638 BUS_SPACE_MAXSIZE_32BIT,
1642 &hba->ctlcfg_dmat)) {
1643 device_printf(hba->pcidev, "alloc ctlcfg_dmat failed\n");
1647 if (bus_dmamem_alloc(hba->ctlcfg_dmat, (void **)&hba->ctlcfg_ptr,
1648 BUS_DMA_WAITOK | BUS_DMA_COHERENT,
1649 &hba->ctlcfg_dmamap) != 0) {
1650 device_printf(hba->pcidev,
1651 "bus_dmamem_alloc failed!\n");
1652 bus_dma_tag_destroy(hba->ctlcfg_dmat);
1656 if (bus_dmamap_load(hba->ctlcfg_dmat,
1657 hba->ctlcfg_dmamap, hba->ctlcfg_ptr,
1658 hba->u.mvfrey.internal_mem_size,
1659 hptiop_mvfrey_map_ctlcfg, hba, 0)) {
1660 device_printf(hba->pcidev, "bus_dmamap_load failed!\n");
1661 if (hba->ctlcfg_dmat) {
1662 bus_dmamem_free(hba->ctlcfg_dmat,
1663 hba->ctlcfg_ptr, hba->ctlcfg_dmamap);
1664 bus_dma_tag_destroy(hba->ctlcfg_dmat);
1672 static int hptiop_internal_memfree_itl(struct hpt_iop_hba *hba) {
1676 static int hptiop_internal_memfree_mv(struct hpt_iop_hba *hba)
1678 if (hba->ctlcfg_dmat) {
1679 bus_dmamap_unload(hba->ctlcfg_dmat, hba->ctlcfg_dmamap);
1680 bus_dmamem_free(hba->ctlcfg_dmat,
1681 hba->ctlcfg_ptr, hba->ctlcfg_dmamap);
1682 bus_dma_tag_destroy(hba->ctlcfg_dmat);
1688 static int hptiop_internal_memfree_mvfrey(struct hpt_iop_hba *hba)
1690 if (hba->ctlcfg_dmat) {
1691 bus_dmamap_unload(hba->ctlcfg_dmat, hba->ctlcfg_dmamap);
1692 bus_dmamem_free(hba->ctlcfg_dmat,
1693 hba->ctlcfg_ptr, hba->ctlcfg_dmamap);
1694 bus_dma_tag_destroy(hba->ctlcfg_dmat);
1700 static int hptiop_reset_comm_mvfrey(struct hpt_iop_hba *hba)
1704 if (hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET_COMM, 3000))
1707 /* wait 100ms for MCU ready */
1712 BUS_SPACE_WRT4_MVFREY2(inbound_base,
1713 hba->u.mvfrey.inlist_phy & 0xffffffff);
1714 BUS_SPACE_WRT4_MVFREY2(inbound_base_high,
1715 (hba->u.mvfrey.inlist_phy >> 16) >> 16);
1717 BUS_SPACE_WRT4_MVFREY2(outbound_base,
1718 hba->u.mvfrey.outlist_phy & 0xffffffff);
1719 BUS_SPACE_WRT4_MVFREY2(outbound_base_high,
1720 (hba->u.mvfrey.outlist_phy >> 16) >> 16);
1722 BUS_SPACE_WRT4_MVFREY2(outbound_shadow_base,
1723 hba->u.mvfrey.outlist_cptr_phy & 0xffffffff);
1724 BUS_SPACE_WRT4_MVFREY2(outbound_shadow_base_high,
1725 (hba->u.mvfrey.outlist_cptr_phy >> 16) >> 16);
1727 hba->u.mvfrey.inlist_wptr = (hba->u.mvfrey.list_count - 1)
1728 | CL_POINTER_TOGGLE;
1729 *hba->u.mvfrey.outlist_cptr = (hba->u.mvfrey.list_count - 1)
1730 | CL_POINTER_TOGGLE;
1731 hba->u.mvfrey.outlist_rptr = hba->u.mvfrey.list_count - 1;
1737 * CAM driver interface
1739 static device_method_t driver_methods[] = {
1740 /* Device interface */
1741 DEVMETHOD(device_probe, hptiop_probe),
1742 DEVMETHOD(device_attach, hptiop_attach),
1743 DEVMETHOD(device_detach, hptiop_detach),
1744 DEVMETHOD(device_shutdown, hptiop_shutdown),
1748 static struct hptiop_adapter_ops hptiop_itl_ops = {
1749 .family = INTEL_BASED_IOP,
1750 .iop_wait_ready = hptiop_wait_ready_itl,
1751 .internal_memalloc = 0,
1752 .internal_memfree = hptiop_internal_memfree_itl,
1753 .alloc_pci_res = hptiop_alloc_pci_res_itl,
1754 .release_pci_res = hptiop_release_pci_res_itl,
1755 .enable_intr = hptiop_enable_intr_itl,
1756 .disable_intr = hptiop_disable_intr_itl,
1757 .get_config = hptiop_get_config_itl,
1758 .set_config = hptiop_set_config_itl,
1759 .iop_intr = hptiop_intr_itl,
1760 .post_msg = hptiop_post_msg_itl,
1761 .post_req = hptiop_post_req_itl,
1762 .do_ioctl = hptiop_do_ioctl_itl,
1766 static struct hptiop_adapter_ops hptiop_mv_ops = {
1767 .family = MV_BASED_IOP,
1768 .iop_wait_ready = hptiop_wait_ready_mv,
1769 .internal_memalloc = hptiop_internal_memalloc_mv,
1770 .internal_memfree = hptiop_internal_memfree_mv,
1771 .alloc_pci_res = hptiop_alloc_pci_res_mv,
1772 .release_pci_res = hptiop_release_pci_res_mv,
1773 .enable_intr = hptiop_enable_intr_mv,
1774 .disable_intr = hptiop_disable_intr_mv,
1775 .get_config = hptiop_get_config_mv,
1776 .set_config = hptiop_set_config_mv,
1777 .iop_intr = hptiop_intr_mv,
1778 .post_msg = hptiop_post_msg_mv,
1779 .post_req = hptiop_post_req_mv,
1780 .do_ioctl = hptiop_do_ioctl_mv,
1784 static struct hptiop_adapter_ops hptiop_mvfrey_ops = {
1785 .family = MVFREY_BASED_IOP,
1786 .iop_wait_ready = hptiop_wait_ready_mvfrey,
1787 .internal_memalloc = hptiop_internal_memalloc_mvfrey,
1788 .internal_memfree = hptiop_internal_memfree_mvfrey,
1789 .alloc_pci_res = hptiop_alloc_pci_res_mvfrey,
1790 .release_pci_res = hptiop_release_pci_res_mvfrey,
1791 .enable_intr = hptiop_enable_intr_mvfrey,
1792 .disable_intr = hptiop_disable_intr_mvfrey,
1793 .get_config = hptiop_get_config_mvfrey,
1794 .set_config = hptiop_set_config_mvfrey,
1795 .iop_intr = hptiop_intr_mvfrey,
1796 .post_msg = hptiop_post_msg_mvfrey,
1797 .post_req = hptiop_post_req_mvfrey,
1798 .do_ioctl = hptiop_do_ioctl_mvfrey,
1799 .reset_comm = hptiop_reset_comm_mvfrey,
1802 static driver_t hptiop_pci_driver = {
1805 sizeof(struct hpt_iop_hba)
1808 DRIVER_MODULE(hptiop, pci, hptiop_pci_driver, hptiop_devclass, 0, 0);
1809 MODULE_DEPEND(hptiop, cam, 1, 1, 1);
1811 static int hptiop_probe(device_t dev)
1813 struct hpt_iop_hba *hba;
1815 static char buf[256];
1817 struct hptiop_adapter_ops *ops;
1819 if (pci_get_vendor(dev) != 0x1103)
1822 id = pci_get_device(dev);
1832 ops = &hptiop_mvfrey_ops;
1853 ops = &hptiop_itl_ops;
1858 ops = &hptiop_mv_ops;
1864 device_printf(dev, "adapter at PCI %d:%d:%d, IRQ %d\n",
1865 pci_get_bus(dev), pci_get_slot(dev),
1866 pci_get_function(dev), pci_get_irq(dev));
1868 sprintf(buf, "RocketRAID %x %s Controller\n",
1869 id, sas ? "SAS" : "SATA");
1870 device_set_desc_copy(dev, buf);
1872 hba = (struct hpt_iop_hba *)device_get_softc(dev);
1873 bzero(hba, sizeof(struct hpt_iop_hba));
1876 KdPrint(("hba->ops=%p\n", hba->ops));
1880 static int hptiop_attach(device_t dev)
1882 struct hpt_iop_hba *hba = (struct hpt_iop_hba *)device_get_softc(dev);
1883 struct hpt_iop_request_get_config iop_config;
1884 struct hpt_iop_request_set_config set_config;
1886 struct cam_devq *devq;
1887 struct ccb_setasync ccb;
1888 u_int32_t unit = device_get_unit(dev);
1890 device_printf(dev, "%d RocketRAID 3xxx/4xxx controller driver %s\n",
1891 unit, driver_version);
1893 KdPrint(("hptiop: attach(%d, %d/%d/%d) ops=%p\n", unit,
1894 pci_get_bus(dev), pci_get_slot(dev),
1895 pci_get_function(dev), hba->ops));
1897 pci_enable_busmaster(dev);
1899 hba->pciunit = unit;
1901 if (hba->ops->alloc_pci_res(hba))
1904 if (hba->ops->iop_wait_ready(hba, 2000)) {
1905 device_printf(dev, "adapter is not ready\n");
1906 goto release_pci_res;
1909 mtx_init(&hba->lock, "hptioplock", NULL, MTX_DEF);
1911 if (bus_dma_tag_create(bus_get_dma_tag(dev),/* PCI parent */
1914 BUS_SPACE_MAXADDR, /* lowaddr */
1915 BUS_SPACE_MAXADDR, /* highaddr */
1916 NULL, NULL, /* filter, filterarg */
1917 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
1918 BUS_SPACE_UNRESTRICTED, /* nsegments */
1919 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1921 NULL, /* lockfunc */
1922 NULL, /* lockfuncarg */
1923 &hba->parent_dmat /* tag */))
1925 device_printf(dev, "alloc parent_dmat failed\n");
1926 goto release_pci_res;
1929 if (hba->ops->family == MV_BASED_IOP) {
1930 if (hba->ops->internal_memalloc(hba)) {
1931 device_printf(dev, "alloc srb_dmat failed\n");
1932 goto destroy_parent_tag;
1936 if (hba->ops->get_config(hba, &iop_config)) {
1937 device_printf(dev, "get iop config failed.\n");
1938 goto get_config_failed;
1941 hba->firmware_version = iop_config.firmware_version;
1942 hba->interface_version = iop_config.interface_version;
1943 hba->max_requests = iop_config.max_requests;
1944 hba->max_devices = iop_config.max_devices;
1945 hba->max_request_size = iop_config.request_size;
1946 hba->max_sg_count = iop_config.max_sg_count;
1948 if (hba->ops->family == MVFREY_BASED_IOP) {
1949 if (hba->ops->internal_memalloc(hba)) {
1950 device_printf(dev, "alloc srb_dmat failed\n");
1951 goto destroy_parent_tag;
1953 if (hba->ops->reset_comm(hba)) {
1954 device_printf(dev, "reset comm failed\n");
1955 goto get_config_failed;
1959 if (bus_dma_tag_create(hba->parent_dmat,/* parent */
1961 BUS_SPACE_MAXADDR_32BIT+1, /* boundary */
1962 BUS_SPACE_MAXADDR, /* lowaddr */
1963 BUS_SPACE_MAXADDR, /* highaddr */
1964 NULL, NULL, /* filter, filterarg */
1965 PAGE_SIZE * (hba->max_sg_count-1), /* maxsize */
1966 hba->max_sg_count, /* nsegments */
1967 0x20000, /* maxsegsize */
1968 BUS_DMA_ALLOCNOW, /* flags */
1969 busdma_lock_mutex, /* lockfunc */
1970 &hba->lock, /* lockfuncarg */
1971 &hba->io_dmat /* tag */))
1973 device_printf(dev, "alloc io_dmat failed\n");
1974 goto get_config_failed;
1977 if (bus_dma_tag_create(hba->parent_dmat,/* parent */
1980 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1981 BUS_SPACE_MAXADDR, /* highaddr */
1982 NULL, NULL, /* filter, filterarg */
1983 HPT_SRB_MAX_SIZE * HPT_SRB_MAX_QUEUE_SIZE + 0x20,
1985 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1987 NULL, /* lockfunc */
1988 NULL, /* lockfuncarg */
1989 &hba->srb_dmat /* tag */))
1991 device_printf(dev, "alloc srb_dmat failed\n");
1992 goto destroy_io_dmat;
1995 if (bus_dmamem_alloc(hba->srb_dmat, (void **)&hba->uncached_ptr,
1996 BUS_DMA_WAITOK | BUS_DMA_COHERENT,
1997 &hba->srb_dmamap) != 0)
1999 device_printf(dev, "srb bus_dmamem_alloc failed!\n");
2000 goto destroy_srb_dmat;
2003 if (bus_dmamap_load(hba->srb_dmat,
2004 hba->srb_dmamap, hba->uncached_ptr,
2005 (HPT_SRB_MAX_SIZE * HPT_SRB_MAX_QUEUE_SIZE) + 0x20,
2006 hptiop_map_srb, hba, 0))
2008 device_printf(dev, "bus_dmamap_load failed!\n");
2009 goto srb_dmamem_free;
2012 if ((devq = cam_simq_alloc(hba->max_requests - 1 )) == NULL) {
2013 device_printf(dev, "cam_simq_alloc failed\n");
2014 goto srb_dmamap_unload;
2017 hba->sim = cam_sim_alloc(hptiop_action, hptiop_poll, driver_name,
2018 hba, unit, &hba->lock, hba->max_requests - 1, 1, devq);
2020 device_printf(dev, "cam_sim_alloc failed\n");
2021 cam_simq_free(devq);
2022 goto srb_dmamap_unload;
2024 hptiop_lock_adapter(hba);
2025 if (xpt_bus_register(hba->sim, dev, 0) != CAM_SUCCESS)
2027 device_printf(dev, "xpt_bus_register failed\n");
2031 if (xpt_create_path(&hba->path, /*periph */ NULL,
2032 cam_sim_path(hba->sim), CAM_TARGET_WILDCARD,
2033 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2034 device_printf(dev, "xpt_create_path failed\n");
2035 goto deregister_xpt_bus;
2037 hptiop_unlock_adapter(hba);
2039 bzero(&set_config, sizeof(set_config));
2040 set_config.iop_id = unit;
2041 set_config.vbus_id = cam_sim_path(hba->sim);
2042 set_config.max_host_request_size = HPT_SRB_MAX_REQ_SIZE;
2044 if (hba->ops->set_config(hba, &set_config)) {
2045 device_printf(dev, "set iop config failed.\n");
2049 xpt_setup_ccb(&ccb.ccb_h, hba->path, /*priority*/5);
2050 ccb.ccb_h.func_code = XPT_SASYNC_CB;
2051 ccb.event_enable = (AC_FOUND_DEVICE | AC_LOST_DEVICE);
2052 ccb.callback = hptiop_async;
2053 ccb.callback_arg = hba->sim;
2054 xpt_action((union ccb *)&ccb);
2057 if ((hba->irq_res = bus_alloc_resource_any(hba->pcidev, SYS_RES_IRQ,
2058 &rid, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
2059 device_printf(dev, "allocate irq failed!\n");
2063 if (bus_setup_intr(hba->pcidev, hba->irq_res, INTR_TYPE_CAM | INTR_MPSAFE,
2064 NULL, hptiop_pci_intr, hba, &hba->irq_handle))
2066 device_printf(dev, "allocate intr function failed!\n");
2067 goto free_irq_resource;
2070 if (hptiop_send_sync_msg(hba,
2071 IOPMU_INBOUND_MSG0_START_BACKGROUND_TASK, 5000)) {
2072 device_printf(dev, "fail to start background task\n");
2073 goto teartown_irq_resource;
2076 hba->ops->enable_intr(hba);
2077 hba->initialized = 1;
2079 hba->ioctl_dev = make_dev(&hptiop_cdevsw, unit,
2080 UID_ROOT, GID_WHEEL /*GID_OPERATOR*/,
2081 S_IRUSR | S_IWUSR, "%s%d", driver_name, unit);
2087 teartown_irq_resource:
2088 bus_teardown_intr(dev, hba->irq_res, hba->irq_handle);
2091 bus_release_resource(dev, SYS_RES_IRQ, 0, hba->irq_res);
2093 hptiop_lock_adapter(hba);
2095 xpt_free_path(hba->path);
2098 xpt_bus_deregister(cam_sim_path(hba->sim));
2101 cam_sim_free(hba->sim, /*free devq*/ TRUE);
2102 hptiop_unlock_adapter(hba);
2105 if (hba->uncached_ptr)
2106 bus_dmamap_unload(hba->srb_dmat, hba->srb_dmamap);
2109 if (hba->uncached_ptr)
2110 bus_dmamem_free(hba->srb_dmat,
2111 hba->uncached_ptr, hba->srb_dmamap);
2115 bus_dma_tag_destroy(hba->srb_dmat);
2119 bus_dma_tag_destroy(hba->io_dmat);
2122 hba->ops->internal_memfree(hba);
2125 if (hba->parent_dmat)
2126 bus_dma_tag_destroy(hba->parent_dmat);
2129 if (hba->ops->release_pci_res)
2130 hba->ops->release_pci_res(hba);
2135 static int hptiop_detach(device_t dev)
2137 struct hpt_iop_hba * hba = (struct hpt_iop_hba *)device_get_softc(dev);
2141 hptiop_lock_adapter(hba);
2142 for (i = 0; i < hba->max_devices; i++)
2143 if (hptiop_os_query_remove_device(hba, i)) {
2144 device_printf(dev, "%d file system is busy. id=%d",
2149 if ((error = hptiop_shutdown(dev)) != 0)
2151 if (hptiop_send_sync_msg(hba,
2152 IOPMU_INBOUND_MSG0_STOP_BACKGROUND_TASK, 60000))
2154 hptiop_unlock_adapter(hba);
2156 hptiop_release_resource(hba);
2159 hptiop_unlock_adapter(hba);
2163 static int hptiop_shutdown(device_t dev)
2165 struct hpt_iop_hba * hba = (struct hpt_iop_hba *)device_get_softc(dev);
2169 if (hba->flag & HPT_IOCTL_FLAG_OPEN) {
2170 device_printf(dev, "%d device is busy", hba->pciunit);
2174 hba->ops->disable_intr(hba);
2176 if (hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_SHUTDOWN, 60000))
2182 static void hptiop_pci_intr(void *arg)
2184 struct hpt_iop_hba * hba = (struct hpt_iop_hba *)arg;
2185 hptiop_lock_adapter(hba);
2186 hba->ops->iop_intr(hba);
2187 hptiop_unlock_adapter(hba);
2190 static void hptiop_poll(struct cam_sim *sim)
2192 struct hpt_iop_hba *hba;
2194 hba = cam_sim_softc(sim);
2195 hba->ops->iop_intr(hba);
2198 static void hptiop_async(void * callback_arg, u_int32_t code,
2199 struct cam_path * path, void * arg)
2203 static void hptiop_enable_intr_itl(struct hpt_iop_hba *hba)
2205 BUS_SPACE_WRT4_ITL(outbound_intmask,
2206 ~(IOPMU_OUTBOUND_INT_POSTQUEUE | IOPMU_OUTBOUND_INT_MSG0));
2209 static void hptiop_enable_intr_mv(struct hpt_iop_hba *hba)
2213 int_mask = BUS_SPACE_RD4_MV0(outbound_intmask);
2215 int_mask |= MVIOP_MU_OUTBOUND_INT_POSTQUEUE
2216 | MVIOP_MU_OUTBOUND_INT_MSG;
2217 BUS_SPACE_WRT4_MV0(outbound_intmask,int_mask);
2220 static void hptiop_enable_intr_mvfrey(struct hpt_iop_hba *hba)
2222 BUS_SPACE_WRT4_MVFREY2(f0_doorbell_enable, CPU_TO_F0_DRBL_MSG_A_BIT);
2223 BUS_SPACE_RD4_MVFREY2(f0_doorbell_enable);
2225 BUS_SPACE_WRT4_MVFREY2(isr_enable, 0x1);
2226 BUS_SPACE_RD4_MVFREY2(isr_enable);
2228 BUS_SPACE_WRT4_MVFREY2(pcie_f0_int_enable, 0x1010);
2229 BUS_SPACE_RD4_MVFREY2(pcie_f0_int_enable);
2232 static void hptiop_disable_intr_itl(struct hpt_iop_hba *hba)
2236 int_mask = BUS_SPACE_RD4_ITL(outbound_intmask);
2238 int_mask |= IOPMU_OUTBOUND_INT_POSTQUEUE | IOPMU_OUTBOUND_INT_MSG0;
2239 BUS_SPACE_WRT4_ITL(outbound_intmask, int_mask);
2240 BUS_SPACE_RD4_ITL(outbound_intstatus);
2243 static void hptiop_disable_intr_mv(struct hpt_iop_hba *hba)
2246 int_mask = BUS_SPACE_RD4_MV0(outbound_intmask);
2248 int_mask &= ~(MVIOP_MU_OUTBOUND_INT_MSG
2249 | MVIOP_MU_OUTBOUND_INT_POSTQUEUE);
2250 BUS_SPACE_WRT4_MV0(outbound_intmask,int_mask);
2251 BUS_SPACE_RD4_MV0(outbound_intmask);
2254 static void hptiop_disable_intr_mvfrey(struct hpt_iop_hba *hba)
2256 BUS_SPACE_WRT4_MVFREY2(f0_doorbell_enable, 0);
2257 BUS_SPACE_RD4_MVFREY2(f0_doorbell_enable);
2259 BUS_SPACE_WRT4_MVFREY2(isr_enable, 0);
2260 BUS_SPACE_RD4_MVFREY2(isr_enable);
2262 BUS_SPACE_WRT4_MVFREY2(pcie_f0_int_enable, 0);
2263 BUS_SPACE_RD4_MVFREY2(pcie_f0_int_enable);
2266 static void hptiop_reset_adapter(void *argv)
2268 struct hpt_iop_hba * hba = (struct hpt_iop_hba *)argv;
2269 if (hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET, 60000))
2271 hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_START_BACKGROUND_TASK, 5000);
2274 static void *hptiop_get_srb(struct hpt_iop_hba * hba)
2276 struct hpt_iop_srb * srb;
2278 if (hba->srb_list) {
2279 srb = hba->srb_list;
2280 hba->srb_list = srb->next;
2287 static void hptiop_free_srb(struct hpt_iop_hba *hba, struct hpt_iop_srb *srb)
2289 srb->next = hba->srb_list;
2290 hba->srb_list = srb;
2293 static void hptiop_action(struct cam_sim *sim, union ccb *ccb)
2295 struct hpt_iop_hba * hba = (struct hpt_iop_hba *)cam_sim_softc(sim);
2296 struct hpt_iop_srb * srb;
2299 switch (ccb->ccb_h.func_code) {
2302 if (ccb->ccb_h.target_lun != 0 ||
2303 ccb->ccb_h.target_id >= hba->max_devices ||
2304 (ccb->ccb_h.flags & CAM_CDB_PHYS))
2306 ccb->ccb_h.status = CAM_TID_INVALID;
2311 if ((srb = hptiop_get_srb(hba)) == NULL) {
2312 device_printf(hba->pcidev, "srb allocated failed");
2313 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2319 error = bus_dmamap_load_ccb(hba->io_dmat,
2322 hptiop_post_scsi_command,
2326 if (error && error != EINPROGRESS) {
2327 device_printf(hba->pcidev,
2328 "%d bus_dmamap_load error %d",
2329 hba->pciunit, error);
2330 xpt_freeze_simq(hba->sim, 1);
2331 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2332 hptiop_free_srb(hba, srb);
2340 device_printf(hba->pcidev, "reset adapter");
2342 hptiop_reset_adapter(hba);
2345 case XPT_GET_TRAN_SETTINGS:
2346 case XPT_SET_TRAN_SETTINGS:
2347 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2350 case XPT_CALC_GEOMETRY:
2351 cam_calc_geometry(&ccb->ccg, 1);
2356 struct ccb_pathinq *cpi = &ccb->cpi;
2358 cpi->version_num = 1;
2359 cpi->hba_inquiry = PI_SDTR_ABLE;
2360 cpi->target_sprt = 0;
2361 cpi->hba_misc = PIM_NOBUSRESET;
2362 cpi->hba_eng_cnt = 0;
2363 cpi->max_target = hba->max_devices;
2365 cpi->unit_number = cam_sim_unit(sim);
2366 cpi->bus_id = cam_sim_bus(sim);
2367 cpi->initiator_id = hba->max_devices;
2368 cpi->base_transfer_speed = 3300;
2370 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2371 strlcpy(cpi->hba_vid, "HPT ", HBA_IDLEN);
2372 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2373 cpi->transport = XPORT_SPI;
2374 cpi->transport_version = 2;
2375 cpi->protocol = PROTO_SCSI;
2376 cpi->protocol_version = SCSI_REV_2;
2377 cpi->ccb_h.status = CAM_REQ_CMP;
2382 ccb->ccb_h.status = CAM_REQ_INVALID;
2390 static void hptiop_post_req_itl(struct hpt_iop_hba *hba,
2391 struct hpt_iop_srb *srb,
2392 bus_dma_segment_t *segs, int nsegs)
2395 union ccb *ccb = srb->ccb;
2398 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
2399 cdb = ccb->csio.cdb_io.cdb_ptr;
2401 cdb = ccb->csio.cdb_io.cdb_bytes;
2403 KdPrint(("ccb=%p %x-%x-%x\n",
2404 ccb, *(u_int32_t *)cdb, *((u_int32_t *)cdb+1), *((u_int32_t *)cdb+2)));
2406 if (srb->srb_flag & HPT_SRB_FLAG_HIGH_MEM_ACESS) {
2407 u_int32_t iop_req32;
2408 struct hpt_iop_request_scsi_command req;
2410 iop_req32 = BUS_SPACE_RD4_ITL(inbound_queue);
2412 if (iop_req32 == IOPMU_QUEUE_EMPTY) {
2413 device_printf(hba->pcidev, "invalid req offset\n");
2414 ccb->ccb_h.status = CAM_BUSY;
2415 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
2416 hptiop_free_srb(hba, srb);
2421 if (ccb->csio.dxfer_len && nsegs > 0) {
2422 struct hpt_iopsg *psg = req.sg_list;
2423 for (idx = 0; idx < nsegs; idx++, psg++) {
2424 psg->pci_address = (u_int64_t)segs[idx].ds_addr;
2425 psg->size = segs[idx].ds_len;
2431 bcopy(cdb, req.cdb, ccb->csio.cdb_len);
2434 offsetof(struct hpt_iop_request_scsi_command, sg_list)
2435 + nsegs*sizeof(struct hpt_iopsg);
2436 req.header.type = IOP_REQUEST_TYPE_SCSI_COMMAND;
2437 req.header.flags = 0;
2438 req.header.result = IOP_RESULT_PENDING;
2439 req.header.context = (u_int64_t)(unsigned long)srb;
2440 req.dataxfer_length = ccb->csio.dxfer_len;
2442 req.target = ccb->ccb_h.target_id;
2443 req.lun = ccb->ccb_h.target_lun;
2445 bus_space_write_region_1(hba->bar0t, hba->bar0h, iop_req32,
2446 (u_int8_t *)&req, req.header.size);
2448 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2449 bus_dmamap_sync(hba->io_dmat,
2450 srb->dma_map, BUS_DMASYNC_PREREAD);
2452 else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
2453 bus_dmamap_sync(hba->io_dmat,
2454 srb->dma_map, BUS_DMASYNC_PREWRITE);
2456 BUS_SPACE_WRT4_ITL(inbound_queue,iop_req32);
2458 struct hpt_iop_request_scsi_command *req;
2460 req = (struct hpt_iop_request_scsi_command *)srb;
2461 if (ccb->csio.dxfer_len && nsegs > 0) {
2462 struct hpt_iopsg *psg = req->sg_list;
2463 for (idx = 0; idx < nsegs; idx++, psg++) {
2465 (u_int64_t)segs[idx].ds_addr;
2466 psg->size = segs[idx].ds_len;
2472 bcopy(cdb, req->cdb, ccb->csio.cdb_len);
2474 req->header.type = IOP_REQUEST_TYPE_SCSI_COMMAND;
2475 req->header.result = IOP_RESULT_PENDING;
2476 req->dataxfer_length = ccb->csio.dxfer_len;
2478 req->target = ccb->ccb_h.target_id;
2479 req->lun = ccb->ccb_h.target_lun;
2481 offsetof(struct hpt_iop_request_scsi_command, sg_list)
2482 + nsegs*sizeof(struct hpt_iopsg);
2483 req->header.context = (u_int64_t)srb->index |
2484 IOPMU_QUEUE_ADDR_HOST_BIT;
2485 req->header.flags = IOP_REQUEST_FLAG_OUTPUT_CONTEXT;
2487 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2488 bus_dmamap_sync(hba->io_dmat,
2489 srb->dma_map, BUS_DMASYNC_PREREAD);
2490 }else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
2491 bus_dmamap_sync(hba->io_dmat,
2492 srb->dma_map, BUS_DMASYNC_PREWRITE);
2495 if (hba->firmware_version > 0x01020000
2496 || hba->interface_version > 0x01020000) {
2497 u_int32_t size_bits;
2499 if (req->header.size < 256)
2500 size_bits = IOPMU_QUEUE_REQUEST_SIZE_BIT;
2501 else if (req->header.size < 512)
2502 size_bits = IOPMU_QUEUE_ADDR_HOST_BIT;
2504 size_bits = IOPMU_QUEUE_REQUEST_SIZE_BIT
2505 | IOPMU_QUEUE_ADDR_HOST_BIT;
2507 BUS_SPACE_WRT4_ITL(inbound_queue,
2508 (u_int32_t)srb->phy_addr | size_bits);
2510 BUS_SPACE_WRT4_ITL(inbound_queue, (u_int32_t)srb->phy_addr
2511 |IOPMU_QUEUE_ADDR_HOST_BIT);
2515 static void hptiop_post_req_mv(struct hpt_iop_hba *hba,
2516 struct hpt_iop_srb *srb,
2517 bus_dma_segment_t *segs, int nsegs)
2520 union ccb *ccb = srb->ccb;
2522 struct hpt_iop_request_scsi_command *req;
2525 req = (struct hpt_iop_request_scsi_command *)srb;
2526 req_phy = srb->phy_addr;
2528 if (ccb->csio.dxfer_len && nsegs > 0) {
2529 struct hpt_iopsg *psg = req->sg_list;
2530 for (idx = 0; idx < nsegs; idx++, psg++) {
2531 psg->pci_address = (u_int64_t)segs[idx].ds_addr;
2532 psg->size = segs[idx].ds_len;
2537 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
2538 cdb = ccb->csio.cdb_io.cdb_ptr;
2540 cdb = ccb->csio.cdb_io.cdb_bytes;
2542 bcopy(cdb, req->cdb, ccb->csio.cdb_len);
2543 req->header.type = IOP_REQUEST_TYPE_SCSI_COMMAND;
2544 req->header.result = IOP_RESULT_PENDING;
2545 req->dataxfer_length = ccb->csio.dxfer_len;
2547 req->target = ccb->ccb_h.target_id;
2548 req->lun = ccb->ccb_h.target_lun;
2549 req->header.size = sizeof(struct hpt_iop_request_scsi_command)
2550 - sizeof(struct hpt_iopsg)
2551 + nsegs * sizeof(struct hpt_iopsg);
2552 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2553 bus_dmamap_sync(hba->io_dmat,
2554 srb->dma_map, BUS_DMASYNC_PREREAD);
2556 else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
2557 bus_dmamap_sync(hba->io_dmat,
2558 srb->dma_map, BUS_DMASYNC_PREWRITE);
2559 req->header.context = (u_int64_t)srb->index
2560 << MVIOP_REQUEST_NUMBER_START_BIT
2561 | MVIOP_CMD_TYPE_SCSI;
2562 req->header.flags = IOP_REQUEST_FLAG_OUTPUT_CONTEXT;
2563 size = req->header.size >> 8;
2564 hptiop_mv_inbound_write(req_phy
2565 | MVIOP_MU_QUEUE_ADDR_HOST_BIT
2566 | imin(3, size), hba);
2569 static void hptiop_post_req_mvfrey(struct hpt_iop_hba *hba,
2570 struct hpt_iop_srb *srb,
2571 bus_dma_segment_t *segs, int nsegs)
2574 union ccb *ccb = srb->ccb;
2576 struct hpt_iop_request_scsi_command *req;
2579 req = (struct hpt_iop_request_scsi_command *)srb;
2580 req_phy = srb->phy_addr;
2582 if (ccb->csio.dxfer_len && nsegs > 0) {
2583 struct hpt_iopsg *psg = req->sg_list;
2584 for (idx = 0; idx < nsegs; idx++, psg++) {
2585 psg->pci_address = (u_int64_t)segs[idx].ds_addr | 1;
2586 psg->size = segs[idx].ds_len;
2591 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
2592 cdb = ccb->csio.cdb_io.cdb_ptr;
2594 cdb = ccb->csio.cdb_io.cdb_bytes;
2596 bcopy(cdb, req->cdb, ccb->csio.cdb_len);
2597 req->header.type = IOP_REQUEST_TYPE_SCSI_COMMAND;
2598 req->header.result = IOP_RESULT_PENDING;
2599 req->dataxfer_length = ccb->csio.dxfer_len;
2601 req->target = ccb->ccb_h.target_id;
2602 req->lun = ccb->ccb_h.target_lun;
2603 req->header.size = sizeof(struct hpt_iop_request_scsi_command)
2604 - sizeof(struct hpt_iopsg)
2605 + nsegs * sizeof(struct hpt_iopsg);
2606 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2607 bus_dmamap_sync(hba->io_dmat,
2608 srb->dma_map, BUS_DMASYNC_PREREAD);
2610 else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
2611 bus_dmamap_sync(hba->io_dmat,
2612 srb->dma_map, BUS_DMASYNC_PREWRITE);
2614 req->header.flags = IOP_REQUEST_FLAG_OUTPUT_CONTEXT
2615 | IOP_REQUEST_FLAG_ADDR_BITS
2616 | ((req_phy >> 16) & 0xffff0000);
2617 req->header.context = ((req_phy & 0xffffffff) << 32 )
2619 | IOPMU_QUEUE_ADDR_HOST_BIT | req->header.type;
2621 hba->u.mvfrey.inlist_wptr++;
2622 index = hba->u.mvfrey.inlist_wptr & 0x3fff;
2624 if (index == hba->u.mvfrey.list_count) {
2626 hba->u.mvfrey.inlist_wptr &= ~0x3fff;
2627 hba->u.mvfrey.inlist_wptr ^= CL_POINTER_TOGGLE;
2630 hba->u.mvfrey.inlist[index].addr = req_phy;
2631 hba->u.mvfrey.inlist[index].intrfc_len = (req->header.size + 3) / 4;
2633 BUS_SPACE_WRT4_MVFREY2(inbound_write_ptr, hba->u.mvfrey.inlist_wptr);
2634 BUS_SPACE_RD4_MVFREY2(inbound_write_ptr);
2636 if (req->header.type == IOP_REQUEST_TYPE_SCSI_COMMAND) {
2637 callout_reset(&srb->timeout, 20 * hz, hptiop_reset_adapter, hba);
2641 static void hptiop_post_scsi_command(void *arg, bus_dma_segment_t *segs,
2642 int nsegs, int error)
2644 struct hpt_iop_srb *srb = (struct hpt_iop_srb *)arg;
2645 union ccb *ccb = srb->ccb;
2646 struct hpt_iop_hba *hba = srb->hba;
2648 if (error || nsegs > hba->max_sg_count) {
2649 KdPrint(("hptiop: func_code=%x tid=%x lun=%jx nsegs=%d\n",
2650 ccb->ccb_h.func_code,
2651 ccb->ccb_h.target_id,
2652 (uintmax_t)ccb->ccb_h.target_lun, nsegs));
2653 ccb->ccb_h.status = CAM_BUSY;
2654 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
2655 hptiop_free_srb(hba, srb);
2660 hba->ops->post_req(hba, srb, segs, nsegs);
2663 static void hptiop_mv_map_ctlcfg(void *arg, bus_dma_segment_t *segs,
2664 int nsegs, int error)
2666 struct hpt_iop_hba *hba = (struct hpt_iop_hba *)arg;
2667 hba->ctlcfgcmd_phy = ((u_int64_t)segs->ds_addr + 0x1F)
2669 hba->ctlcfg_ptr = (u_int8_t *)(((unsigned long)hba->ctlcfg_ptr + 0x1F)
2673 static void hptiop_mvfrey_map_ctlcfg(void *arg, bus_dma_segment_t *segs,
2674 int nsegs, int error)
2676 struct hpt_iop_hba *hba = (struct hpt_iop_hba *)arg;
2679 u_int32_t list_count = hba->u.mvfrey.list_count;
2681 phy = ((u_int64_t)segs->ds_addr + 0x1F)
2683 p = (u_int8_t *)(((unsigned long)hba->ctlcfg_ptr + 0x1F)
2686 hba->ctlcfgcmd_phy = phy;
2687 hba->ctlcfg_ptr = p;
2692 hba->u.mvfrey.inlist = (struct mvfrey_inlist_entry *)p;
2693 hba->u.mvfrey.inlist_phy = phy;
2695 p += list_count * sizeof(struct mvfrey_inlist_entry);
2696 phy += list_count * sizeof(struct mvfrey_inlist_entry);
2698 hba->u.mvfrey.outlist = (struct mvfrey_outlist_entry *)p;
2699 hba->u.mvfrey.outlist_phy = phy;
2701 p += list_count * sizeof(struct mvfrey_outlist_entry);
2702 phy += list_count * sizeof(struct mvfrey_outlist_entry);
2704 hba->u.mvfrey.outlist_cptr = (u_int32_t *)p;
2705 hba->u.mvfrey.outlist_cptr_phy = phy;
2708 static void hptiop_map_srb(void *arg, bus_dma_segment_t *segs,
2709 int nsegs, int error)
2711 struct hpt_iop_hba * hba = (struct hpt_iop_hba *)arg;
2712 bus_addr_t phy_addr = (segs->ds_addr + 0x1F) & ~(bus_addr_t)0x1F;
2713 struct hpt_iop_srb *srb, *tmp_srb;
2716 if (error || nsegs == 0) {
2717 device_printf(hba->pcidev, "hptiop_map_srb error");
2722 srb = (struct hpt_iop_srb *)
2723 (((unsigned long)hba->uncached_ptr + 0x1F)
2724 & ~(unsigned long)0x1F);
2726 for (i = 0; i < HPT_SRB_MAX_QUEUE_SIZE; i++) {
2727 tmp_srb = (struct hpt_iop_srb *)
2728 ((char *)srb + i * HPT_SRB_MAX_SIZE);
2729 if (((unsigned long)tmp_srb & 0x1F) == 0) {
2730 if (bus_dmamap_create(hba->io_dmat,
2731 0, &tmp_srb->dma_map)) {
2732 device_printf(hba->pcidev, "dmamap create failed");
2736 bzero(tmp_srb, sizeof(struct hpt_iop_srb));
2739 if (hba->ctlcfg_ptr == 0) {/*itl iop*/
2740 tmp_srb->phy_addr = (u_int64_t)(u_int32_t)
2742 if (phy_addr & IOPMU_MAX_MEM_SUPPORT_MASK_32G)
2744 HPT_SRB_FLAG_HIGH_MEM_ACESS;
2746 tmp_srb->phy_addr = phy_addr;
2749 callout_init_mtx(&tmp_srb->timeout, &hba->lock, 0);
2750 hptiop_free_srb(hba, tmp_srb);
2751 hba->srb[i] = tmp_srb;
2752 phy_addr += HPT_SRB_MAX_SIZE;
2755 device_printf(hba->pcidev, "invalid alignment");
2761 static void hptiop_os_message_callback(struct hpt_iop_hba * hba, u_int32_t msg)
2766 static int hptiop_os_query_remove_device(struct hpt_iop_hba * hba,
2769 struct cam_periph *periph = NULL;
2770 struct cam_path *path;
2771 int status, retval = 0;
2773 status = xpt_create_path(&path, NULL, hba->sim->path_id, target_id, 0);
2775 if (status == CAM_REQ_CMP) {
2776 if ((periph = cam_periph_find(path, "da")) != NULL) {
2777 if (periph->refcount >= 1) {
2778 device_printf(hba->pcidev, "%d ,"
2781 hba->pciunit, target_id, periph->refcount);
2785 xpt_free_path(path);
2790 static void hptiop_release_resource(struct hpt_iop_hba *hba)
2795 destroy_dev(hba->ioctl_dev);
2798 struct ccb_setasync ccb;
2800 xpt_setup_ccb(&ccb.ccb_h, hba->path, /*priority*/5);
2801 ccb.ccb_h.func_code = XPT_SASYNC_CB;
2802 ccb.event_enable = 0;
2803 ccb.callback = hptiop_async;
2804 ccb.callback_arg = hba->sim;
2805 xpt_action((union ccb *)&ccb);
2806 xpt_free_path(hba->path);
2809 if (hba->irq_handle)
2810 bus_teardown_intr(hba->pcidev, hba->irq_res, hba->irq_handle);
2813 hptiop_lock_adapter(hba);
2814 xpt_bus_deregister(cam_sim_path(hba->sim));
2815 cam_sim_free(hba->sim, TRUE);
2816 hptiop_unlock_adapter(hba);
2819 if (hba->ctlcfg_dmat) {
2820 bus_dmamap_unload(hba->ctlcfg_dmat, hba->ctlcfg_dmamap);
2821 bus_dmamem_free(hba->ctlcfg_dmat,
2822 hba->ctlcfg_ptr, hba->ctlcfg_dmamap);
2823 bus_dma_tag_destroy(hba->ctlcfg_dmat);
2826 for (i = 0; i < HPT_SRB_MAX_QUEUE_SIZE; i++) {
2827 struct hpt_iop_srb *srb = hba->srb[i];
2829 bus_dmamap_destroy(hba->io_dmat, srb->dma_map);
2830 callout_drain(&srb->timeout);
2833 if (hba->srb_dmat) {
2834 bus_dmamap_unload(hba->srb_dmat, hba->srb_dmamap);
2835 bus_dmamap_destroy(hba->srb_dmat, hba->srb_dmamap);
2836 bus_dma_tag_destroy(hba->srb_dmat);
2840 bus_dma_tag_destroy(hba->io_dmat);
2842 if (hba->parent_dmat)
2843 bus_dma_tag_destroy(hba->parent_dmat);
2846 bus_release_resource(hba->pcidev, SYS_RES_IRQ,
2850 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
2851 hba->bar0_rid, hba->bar0_res);
2853 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
2854 hba->bar2_rid, hba->bar2_res);
2855 mtx_destroy(&hba->lock);