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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/types.h>
33 #if (__FreeBSD_version >= 500000)
35 #include <sys/systm.h>
37 #include <machine/clock.h>
41 #include <sys/malloc.h>
43 #include <sys/libkern.h>
44 #include <sys/kernel.h>
46 #if (__FreeBSD_version >= 500000)
47 #include <sys/kthread.h>
48 #include <sys/mutex.h>
49 #include <sys/module.h>
52 #include <sys/eventhandler.h>
54 #include <sys/taskqueue.h>
55 #include <sys/ioccom.h>
57 #include <machine/resource.h>
58 #include <machine/bus.h>
59 #include <machine/stdarg.h>
65 #if (__FreeBSD_version >= 500000)
66 #include <dev/pci/pcireg.h>
67 #include <dev/pci/pcivar.h>
69 #include <pci/pcivar.h>
70 #include <pci/pcireg.h>
73 #if (__FreeBSD_version <= 500043)
74 #include <sys/devicestat.h>
78 #include <cam/cam_ccb.h>
79 #include <cam/cam_sim.h>
80 #include <cam/cam_xpt_sim.h>
81 #include <cam/cam_debug.h>
82 #include <cam/cam_periph.h>
83 #include <cam/scsi/scsi_all.h>
84 #include <cam/scsi/scsi_message.h>
86 #if (__FreeBSD_version < 500043)
87 #include <sys/bus_private.h>
90 #include <dev/hptiop/hptiop.h>
92 static const char driver_name[] = "hptiop";
93 static const char driver_version[] = "v1.9";
95 static devclass_t hptiop_devclass;
97 static int hptiop_send_sync_msg(struct hpt_iop_hba *hba,
98 u_int32_t msg, u_int32_t millisec);
99 static void hptiop_request_callback_itl(struct hpt_iop_hba *hba,
101 static void hptiop_request_callback_mv(struct hpt_iop_hba *hba, u_int64_t req);
102 static void hptiop_request_callback_mvfrey(struct hpt_iop_hba *hba,
104 static void hptiop_os_message_callback(struct hpt_iop_hba *hba, u_int32_t msg);
105 static int hptiop_do_ioctl_itl(struct hpt_iop_hba *hba,
106 struct hpt_iop_ioctl_param *pParams);
107 static int hptiop_do_ioctl_mv(struct hpt_iop_hba *hba,
108 struct hpt_iop_ioctl_param *pParams);
109 static int hptiop_do_ioctl_mvfrey(struct hpt_iop_hba *hba,
110 struct hpt_iop_ioctl_param *pParams);
111 static int hptiop_rescan_bus(struct hpt_iop_hba *hba);
112 static int hptiop_alloc_pci_res_itl(struct hpt_iop_hba *hba);
113 static int hptiop_alloc_pci_res_mv(struct hpt_iop_hba *hba);
114 static int hptiop_alloc_pci_res_mvfrey(struct hpt_iop_hba *hba);
115 static int hptiop_get_config_itl(struct hpt_iop_hba *hba,
116 struct hpt_iop_request_get_config *config);
117 static int hptiop_get_config_mv(struct hpt_iop_hba *hba,
118 struct hpt_iop_request_get_config *config);
119 static int hptiop_get_config_mvfrey(struct hpt_iop_hba *hba,
120 struct hpt_iop_request_get_config *config);
121 static int hptiop_set_config_itl(struct hpt_iop_hba *hba,
122 struct hpt_iop_request_set_config *config);
123 static int hptiop_set_config_mv(struct hpt_iop_hba *hba,
124 struct hpt_iop_request_set_config *config);
125 static int hptiop_set_config_mvfrey(struct hpt_iop_hba *hba,
126 struct hpt_iop_request_set_config *config);
127 static int hptiop_internal_memalloc_mv(struct hpt_iop_hba *hba);
128 static int hptiop_internal_memalloc_mvfrey(struct hpt_iop_hba *hba);
129 static int hptiop_internal_memfree_itl(struct hpt_iop_hba *hba);
130 static int hptiop_internal_memfree_mv(struct hpt_iop_hba *hba);
131 static int hptiop_internal_memfree_mvfrey(struct hpt_iop_hba *hba);
132 static int hptiop_post_ioctl_command_itl(struct hpt_iop_hba *hba,
133 u_int32_t req32, struct hpt_iop_ioctl_param *pParams);
134 static int hptiop_post_ioctl_command_mv(struct hpt_iop_hba *hba,
135 struct hpt_iop_request_ioctl_command *req,
136 struct hpt_iop_ioctl_param *pParams);
137 static int hptiop_post_ioctl_command_mvfrey(struct hpt_iop_hba *hba,
138 struct hpt_iop_request_ioctl_command *req,
139 struct hpt_iop_ioctl_param *pParams);
140 static void hptiop_post_req_itl(struct hpt_iop_hba *hba,
141 struct hpt_iop_srb *srb,
142 bus_dma_segment_t *segs, int nsegs);
143 static void hptiop_post_req_mv(struct hpt_iop_hba *hba,
144 struct hpt_iop_srb *srb,
145 bus_dma_segment_t *segs, int nsegs);
146 static void hptiop_post_req_mvfrey(struct hpt_iop_hba *hba,
147 struct hpt_iop_srb *srb,
148 bus_dma_segment_t *segs, int nsegs);
149 static void hptiop_post_msg_itl(struct hpt_iop_hba *hba, u_int32_t msg);
150 static void hptiop_post_msg_mv(struct hpt_iop_hba *hba, u_int32_t msg);
151 static void hptiop_post_msg_mvfrey(struct hpt_iop_hba *hba, u_int32_t msg);
152 static void hptiop_enable_intr_itl(struct hpt_iop_hba *hba);
153 static void hptiop_enable_intr_mv(struct hpt_iop_hba *hba);
154 static void hptiop_enable_intr_mvfrey(struct hpt_iop_hba *hba);
155 static void hptiop_disable_intr_itl(struct hpt_iop_hba *hba);
156 static void hptiop_disable_intr_mv(struct hpt_iop_hba *hba);
157 static void hptiop_disable_intr_mvfrey(struct hpt_iop_hba *hba);
158 static void hptiop_free_srb(struct hpt_iop_hba *hba, struct hpt_iop_srb *srb);
159 static int hptiop_os_query_remove_device(struct hpt_iop_hba *hba, int tid);
160 static int hptiop_probe(device_t dev);
161 static int hptiop_attach(device_t dev);
162 static int hptiop_detach(device_t dev);
163 static int hptiop_shutdown(device_t dev);
164 static void hptiop_action(struct cam_sim *sim, union ccb *ccb);
165 static void hptiop_poll(struct cam_sim *sim);
166 static void hptiop_async(void *callback_arg, u_int32_t code,
167 struct cam_path *path, void *arg);
168 static void hptiop_pci_intr(void *arg);
169 static void hptiop_release_resource(struct hpt_iop_hba *hba);
170 static void hptiop_reset_adapter(void *argv);
171 static d_open_t hptiop_open;
172 static d_close_t hptiop_close;
173 static d_ioctl_t hptiop_ioctl;
175 static struct cdevsw hptiop_cdevsw = {
176 .d_open = hptiop_open,
177 .d_close = hptiop_close,
178 .d_ioctl = hptiop_ioctl,
179 .d_name = driver_name,
180 #if __FreeBSD_version>=503000
181 .d_version = D_VERSION,
183 #if (__FreeBSD_version>=503000 && __FreeBSD_version<600034)
184 .d_flags = D_NEEDGIANT,
186 #if __FreeBSD_version<600034
187 #if __FreeBSD_version>=501000
190 .d_maj = HPT_DEV_MAJOR,
195 #if __FreeBSD_version < 503000
196 #define hba_from_dev(dev) ((struct hpt_iop_hba *)(dev)->si_drv1)
198 #define hba_from_dev(dev) \
199 ((struct hpt_iop_hba *)devclass_get_softc(hptiop_devclass, dev2unit(dev)))
202 #define BUS_SPACE_WRT4_ITL(offset, value) bus_space_write_4(hba->bar0t,\
203 hba->bar0h, offsetof(struct hpt_iopmu_itl, offset), (value))
204 #define BUS_SPACE_RD4_ITL(offset) bus_space_read_4(hba->bar0t,\
205 hba->bar0h, offsetof(struct hpt_iopmu_itl, offset))
207 #define BUS_SPACE_WRT4_MV0(offset, value) bus_space_write_4(hba->bar0t,\
208 hba->bar0h, offsetof(struct hpt_iopmv_regs, offset), value)
209 #define BUS_SPACE_RD4_MV0(offset) bus_space_read_4(hba->bar0t,\
210 hba->bar0h, offsetof(struct hpt_iopmv_regs, offset))
211 #define BUS_SPACE_WRT4_MV2(offset, value) bus_space_write_4(hba->bar2t,\
212 hba->bar2h, offsetof(struct hpt_iopmu_mv, offset), value)
213 #define BUS_SPACE_RD4_MV2(offset) bus_space_read_4(hba->bar2t,\
214 hba->bar2h, offsetof(struct hpt_iopmu_mv, offset))
216 #define BUS_SPACE_WRT4_MVFREY2(offset, value) bus_space_write_4(hba->bar2t,\
217 hba->bar2h, offsetof(struct hpt_iopmu_mvfrey, offset), value)
218 #define BUS_SPACE_RD4_MVFREY2(offset) bus_space_read_4(hba->bar2t,\
219 hba->bar2h, offsetof(struct hpt_iopmu_mvfrey, offset))
221 static int hptiop_open(ioctl_dev_t dev, int flags,
222 int devtype, ioctl_thread_t proc)
224 struct hpt_iop_hba *hba = hba_from_dev(dev);
228 if (hba->flag & HPT_IOCTL_FLAG_OPEN)
230 hba->flag |= HPT_IOCTL_FLAG_OPEN;
234 static int hptiop_close(ioctl_dev_t dev, int flags,
235 int devtype, ioctl_thread_t proc)
237 struct hpt_iop_hba *hba = hba_from_dev(dev);
238 hba->flag &= ~(u_int32_t)HPT_IOCTL_FLAG_OPEN;
242 static int hptiop_ioctl(ioctl_dev_t dev, u_long cmd, caddr_t data,
243 int flags, ioctl_thread_t proc)
246 struct hpt_iop_hba *hba = hba_from_dev(dev);
248 #if (__FreeBSD_version >= 500000)
253 case HPT_DO_IOCONTROL:
254 ret = hba->ops->do_ioctl(hba,
255 (struct hpt_iop_ioctl_param *)data);
258 ret = hptiop_rescan_bus(hba);
262 #if (__FreeBSD_version >= 500000)
269 static u_int64_t hptiop_mv_outbound_read(struct hpt_iop_hba *hba)
272 u_int32_t outbound_tail = BUS_SPACE_RD4_MV2(outbound_tail);
273 u_int32_t outbound_head = BUS_SPACE_RD4_MV2(outbound_head);
275 if (outbound_tail != outbound_head) {
276 bus_space_read_region_4(hba->bar2t, hba->bar2h,
277 offsetof(struct hpt_iopmu_mv,
278 outbound_q[outbound_tail]),
283 if (outbound_tail == MVIOP_QUEUE_LEN)
286 BUS_SPACE_WRT4_MV2(outbound_tail, outbound_tail);
292 static void hptiop_mv_inbound_write(u_int64_t p, struct hpt_iop_hba *hba)
294 u_int32_t inbound_head = BUS_SPACE_RD4_MV2(inbound_head);
295 u_int32_t head = inbound_head + 1;
297 if (head == MVIOP_QUEUE_LEN)
300 bus_space_write_region_4(hba->bar2t, hba->bar2h,
301 offsetof(struct hpt_iopmu_mv, inbound_q[inbound_head]),
303 BUS_SPACE_WRT4_MV2(inbound_head, head);
304 BUS_SPACE_WRT4_MV0(inbound_doorbell, MVIOP_MU_INBOUND_INT_POSTQUEUE);
307 static void hptiop_post_msg_itl(struct hpt_iop_hba *hba, u_int32_t msg)
309 BUS_SPACE_WRT4_ITL(inbound_msgaddr0, msg);
310 BUS_SPACE_RD4_ITL(outbound_intstatus);
313 static void hptiop_post_msg_mv(struct hpt_iop_hba *hba, u_int32_t msg)
316 BUS_SPACE_WRT4_MV2(inbound_msg, msg);
317 BUS_SPACE_WRT4_MV0(inbound_doorbell, MVIOP_MU_INBOUND_INT_MSG);
319 BUS_SPACE_RD4_MV0(outbound_intmask);
322 static void hptiop_post_msg_mvfrey(struct hpt_iop_hba *hba, u_int32_t msg)
324 BUS_SPACE_WRT4_MVFREY2(f0_to_cpu_msg_a, msg);
325 BUS_SPACE_RD4_MVFREY2(f0_to_cpu_msg_a);
328 static int hptiop_wait_ready_itl(struct hpt_iop_hba * hba, u_int32_t millisec)
333 for (i = 0; i < millisec; i++) {
334 req = BUS_SPACE_RD4_ITL(inbound_queue);
335 if (req != IOPMU_QUEUE_EMPTY)
340 if (req!=IOPMU_QUEUE_EMPTY) {
341 BUS_SPACE_WRT4_ITL(outbound_queue, req);
342 BUS_SPACE_RD4_ITL(outbound_intstatus);
349 static int hptiop_wait_ready_mv(struct hpt_iop_hba * hba, u_int32_t millisec)
351 if (hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_NOP, millisec))
357 static int hptiop_wait_ready_mvfrey(struct hpt_iop_hba * hba,
360 if (hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_NOP, millisec))
366 static void hptiop_request_callback_itl(struct hpt_iop_hba * hba,
369 struct hpt_iop_srb *srb;
370 struct hpt_iop_request_scsi_command *req=0;
373 u_int32_t result, temp, dxfer;
376 if (index & IOPMU_QUEUE_MASK_HOST_BITS) { /*host req*/
377 if (hba->firmware_version > 0x01020000 ||
378 hba->interface_version > 0x01020000) {
379 srb = hba->srb[index & ~(u_int32_t)
380 (IOPMU_QUEUE_ADDR_HOST_BIT
381 | IOPMU_QUEUE_REQUEST_RESULT_BIT)];
382 req = (struct hpt_iop_request_scsi_command *)srb;
383 if (index & IOPMU_QUEUE_REQUEST_RESULT_BIT)
384 result = IOP_RESULT_SUCCESS;
386 result = req->header.result;
388 srb = hba->srb[index &
389 ~(u_int32_t)IOPMU_QUEUE_ADDR_HOST_BIT];
390 req = (struct hpt_iop_request_scsi_command *)srb;
391 result = req->header.result;
393 dxfer = req->dataxfer_length;
398 temp = bus_space_read_4(hba->bar0t, hba->bar0h, index +
399 offsetof(struct hpt_iop_request_header, type));
400 result = bus_space_read_4(hba->bar0t, hba->bar0h, index +
401 offsetof(struct hpt_iop_request_header, result));
403 case IOP_REQUEST_TYPE_IOCTL_COMMAND:
406 bus_space_write_region_4(hba->bar0t, hba->bar0h, index +
407 offsetof(struct hpt_iop_request_header, context),
408 (u_int32_t *)&temp64, 2);
409 wakeup((void *)((unsigned long)hba->u.itl.mu + index));
413 case IOP_REQUEST_TYPE_SCSI_COMMAND:
414 bus_space_read_region_4(hba->bar0t, hba->bar0h, index +
415 offsetof(struct hpt_iop_request_header, context),
416 (u_int32_t *)&temp64, 2);
417 srb = (struct hpt_iop_srb *)(unsigned long)temp64;
418 dxfer = bus_space_read_4(hba->bar0t, hba->bar0h,
419 index + offsetof(struct hpt_iop_request_scsi_command,
422 ccb = (union ccb *)srb->ccb;
423 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
424 cdb = ccb->csio.cdb_io.cdb_ptr;
426 cdb = ccb->csio.cdb_io.cdb_bytes;
428 if (cdb[0] == SYNCHRONIZE_CACHE) { /* ??? */
429 ccb->ccb_h.status = CAM_REQ_CMP;
434 case IOP_RESULT_SUCCESS:
435 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
437 bus_dmamap_sync(hba->io_dmat,
438 srb->dma_map, BUS_DMASYNC_POSTREAD);
439 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
442 bus_dmamap_sync(hba->io_dmat,
443 srb->dma_map, BUS_DMASYNC_POSTWRITE);
444 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
448 ccb->ccb_h.status = CAM_REQ_CMP;
451 case IOP_RESULT_BAD_TARGET:
452 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
454 case IOP_RESULT_BUSY:
455 ccb->ccb_h.status = CAM_BUSY;
457 case IOP_RESULT_INVALID_REQUEST:
458 ccb->ccb_h.status = CAM_REQ_INVALID;
460 case IOP_RESULT_FAIL:
461 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
463 case IOP_RESULT_RESET:
464 ccb->ccb_h.status = CAM_BUSY;
466 case IOP_RESULT_CHECK_CONDITION:
467 memset(&ccb->csio.sense_data, 0,
468 sizeof(ccb->csio.sense_data));
469 if (dxfer < ccb->csio.sense_len)
470 ccb->csio.sense_resid = ccb->csio.sense_len -
473 ccb->csio.sense_resid = 0;
474 if (srb->srb_flag & HPT_SRB_FLAG_HIGH_MEM_ACESS) {/*iop*/
475 bus_space_read_region_1(hba->bar0t, hba->bar0h,
476 index + offsetof(struct hpt_iop_request_scsi_command,
477 sg_list), (u_int8_t *)&ccb->csio.sense_data,
478 MIN(dxfer, sizeof(ccb->csio.sense_data)));
480 memcpy(&ccb->csio.sense_data, &req->sg_list,
481 MIN(dxfer, sizeof(ccb->csio.sense_data)));
483 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
484 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
485 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
488 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
492 if (srb->srb_flag & HPT_SRB_FLAG_HIGH_MEM_ACESS)
493 BUS_SPACE_WRT4_ITL(outbound_queue, index);
495 ccb->csio.resid = ccb->csio.dxfer_len - dxfer;
497 hptiop_free_srb(hba, srb);
503 static void hptiop_drain_outbound_queue_itl(struct hpt_iop_hba *hba)
507 while ((req = BUS_SPACE_RD4_ITL(outbound_queue)) !=IOPMU_QUEUE_EMPTY) {
508 if (req & IOPMU_QUEUE_MASK_HOST_BITS)
509 hptiop_request_callback_itl(hba, req);
511 struct hpt_iop_request_header *p;
513 p = (struct hpt_iop_request_header *)
514 ((char *)hba->u.itl.mu + req);
515 temp = bus_space_read_4(hba->bar0t,
517 offsetof(struct hpt_iop_request_header,
519 if (temp & IOP_REQUEST_FLAG_SYNC_REQUEST) {
521 bus_space_read_region_4(hba->bar0t,
523 offsetof(struct hpt_iop_request_header,
525 (u_int32_t *)&temp64, 2);
527 hptiop_request_callback_itl(hba, req);
530 bus_space_write_region_4(hba->bar0t,
532 offsetof(struct hpt_iop_request_header,
534 (u_int32_t *)&temp64, 2);
537 hptiop_request_callback_itl(hba, req);
542 static int hptiop_intr_itl(struct hpt_iop_hba * hba)
547 status = BUS_SPACE_RD4_ITL(outbound_intstatus);
549 if (status & IOPMU_OUTBOUND_INT_MSG0) {
550 u_int32_t msg = BUS_SPACE_RD4_ITL(outbound_msgaddr0);
551 KdPrint(("hptiop: received outbound msg %x\n", msg));
552 BUS_SPACE_WRT4_ITL(outbound_intstatus, IOPMU_OUTBOUND_INT_MSG0);
553 hptiop_os_message_callback(hba, msg);
557 if (status & IOPMU_OUTBOUND_INT_POSTQUEUE) {
558 hptiop_drain_outbound_queue_itl(hba);
565 static void hptiop_request_callback_mv(struct hpt_iop_hba * hba,
568 u_int32_t context = (u_int32_t)_tag;
570 if (context & MVIOP_CMD_TYPE_SCSI) {
571 struct hpt_iop_srb *srb;
572 struct hpt_iop_request_scsi_command *req;
576 srb = hba->srb[context >> MVIOP_REQUEST_NUMBER_START_BIT];
577 req = (struct hpt_iop_request_scsi_command *)srb;
578 ccb = (union ccb *)srb->ccb;
579 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
580 cdb = ccb->csio.cdb_io.cdb_ptr;
582 cdb = ccb->csio.cdb_io.cdb_bytes;
584 if (cdb[0] == SYNCHRONIZE_CACHE) { /* ??? */
585 ccb->ccb_h.status = CAM_REQ_CMP;
588 if (context & MVIOP_MU_QUEUE_REQUEST_RESULT_BIT)
589 req->header.result = IOP_RESULT_SUCCESS;
591 switch (req->header.result) {
592 case IOP_RESULT_SUCCESS:
593 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
595 bus_dmamap_sync(hba->io_dmat,
596 srb->dma_map, BUS_DMASYNC_POSTREAD);
597 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
600 bus_dmamap_sync(hba->io_dmat,
601 srb->dma_map, BUS_DMASYNC_POSTWRITE);
602 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
605 ccb->ccb_h.status = CAM_REQ_CMP;
607 case IOP_RESULT_BAD_TARGET:
608 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
610 case IOP_RESULT_BUSY:
611 ccb->ccb_h.status = CAM_BUSY;
613 case IOP_RESULT_INVALID_REQUEST:
614 ccb->ccb_h.status = CAM_REQ_INVALID;
616 case IOP_RESULT_FAIL:
617 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
619 case IOP_RESULT_RESET:
620 ccb->ccb_h.status = CAM_BUSY;
622 case IOP_RESULT_CHECK_CONDITION:
623 memset(&ccb->csio.sense_data, 0,
624 sizeof(ccb->csio.sense_data));
625 if (req->dataxfer_length < ccb->csio.sense_len)
626 ccb->csio.sense_resid = ccb->csio.sense_len -
627 req->dataxfer_length;
629 ccb->csio.sense_resid = 0;
630 memcpy(&ccb->csio.sense_data, &req->sg_list,
631 MIN(req->dataxfer_length, sizeof(ccb->csio.sense_data)));
632 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
633 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
634 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
637 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
641 ccb->csio.resid = ccb->csio.dxfer_len - req->dataxfer_length;
643 hptiop_free_srb(hba, srb);
645 } else if (context & MVIOP_CMD_TYPE_IOCTL) {
646 struct hpt_iop_request_ioctl_command *req = hba->ctlcfg_ptr;
647 if (context & MVIOP_MU_QUEUE_REQUEST_RESULT_BIT)
648 hba->config_done = 1;
650 hba->config_done = -1;
653 (MVIOP_CMD_TYPE_SET_CONFIG |
654 MVIOP_CMD_TYPE_GET_CONFIG))
655 hba->config_done = 1;
657 device_printf(hba->pcidev, "wrong callback type\n");
661 static void hptiop_request_callback_mvfrey(struct hpt_iop_hba * hba,
664 u_int32_t req_type = _tag & 0xf;
666 struct hpt_iop_srb *srb;
667 struct hpt_iop_request_scsi_command *req;
672 case IOP_REQUEST_TYPE_GET_CONFIG:
673 case IOP_REQUEST_TYPE_SET_CONFIG:
674 hba->config_done = 1;
677 case IOP_REQUEST_TYPE_SCSI_COMMAND:
678 srb = hba->srb[(_tag >> 4) & 0xff];
679 req = (struct hpt_iop_request_scsi_command *)srb;
681 ccb = (union ccb *)srb->ccb;
683 untimeout(hptiop_reset_adapter, hba, ccb->ccb_h.timeout_ch);
685 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
686 cdb = ccb->csio.cdb_io.cdb_ptr;
688 cdb = ccb->csio.cdb_io.cdb_bytes;
690 if (cdb[0] == SYNCHRONIZE_CACHE) { /* ??? */
691 ccb->ccb_h.status = CAM_REQ_CMP;
695 if (_tag & MVFREYIOPMU_QUEUE_REQUEST_RESULT_BIT)
696 req->header.result = IOP_RESULT_SUCCESS;
698 switch (req->header.result) {
699 case IOP_RESULT_SUCCESS:
700 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
702 bus_dmamap_sync(hba->io_dmat,
703 srb->dma_map, BUS_DMASYNC_POSTREAD);
704 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
707 bus_dmamap_sync(hba->io_dmat,
708 srb->dma_map, BUS_DMASYNC_POSTWRITE);
709 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
712 ccb->ccb_h.status = CAM_REQ_CMP;
714 case IOP_RESULT_BAD_TARGET:
715 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
717 case IOP_RESULT_BUSY:
718 ccb->ccb_h.status = CAM_BUSY;
720 case IOP_RESULT_INVALID_REQUEST:
721 ccb->ccb_h.status = CAM_REQ_INVALID;
723 case IOP_RESULT_FAIL:
724 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
726 case IOP_RESULT_RESET:
727 ccb->ccb_h.status = CAM_BUSY;
729 case IOP_RESULT_CHECK_CONDITION:
730 memset(&ccb->csio.sense_data, 0,
731 sizeof(ccb->csio.sense_data));
732 if (req->dataxfer_length < ccb->csio.sense_len)
733 ccb->csio.sense_resid = ccb->csio.sense_len -
734 req->dataxfer_length;
736 ccb->csio.sense_resid = 0;
737 memcpy(&ccb->csio.sense_data, &req->sg_list,
738 MIN(req->dataxfer_length, sizeof(ccb->csio.sense_data)));
739 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
740 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
741 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
744 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
748 ccb->csio.resid = ccb->csio.dxfer_len - req->dataxfer_length;
750 hptiop_free_srb(hba, srb);
753 case IOP_REQUEST_TYPE_IOCTL_COMMAND:
754 if (_tag & MVFREYIOPMU_QUEUE_REQUEST_RESULT_BIT)
755 hba->config_done = 1;
757 hba->config_done = -1;
758 wakeup((struct hpt_iop_request_ioctl_command *)hba->ctlcfg_ptr);
761 device_printf(hba->pcidev, "wrong callback type\n");
766 static void hptiop_drain_outbound_queue_mv(struct hpt_iop_hba * hba)
770 while ((req = hptiop_mv_outbound_read(hba))) {
771 if (req & MVIOP_MU_QUEUE_ADDR_HOST_BIT) {
772 if (req & MVIOP_MU_QUEUE_REQUEST_RETURN_CONTEXT) {
773 hptiop_request_callback_mv(hba, req);
779 static int hptiop_intr_mv(struct hpt_iop_hba * hba)
784 status = BUS_SPACE_RD4_MV0(outbound_doorbell);
787 BUS_SPACE_WRT4_MV0(outbound_doorbell, ~status);
789 if (status & MVIOP_MU_OUTBOUND_INT_MSG) {
790 u_int32_t msg = BUS_SPACE_RD4_MV2(outbound_msg);
791 KdPrint(("hptiop: received outbound msg %x\n", msg));
792 hptiop_os_message_callback(hba, msg);
796 if (status & MVIOP_MU_OUTBOUND_INT_POSTQUEUE) {
797 hptiop_drain_outbound_queue_mv(hba);
804 static int hptiop_intr_mvfrey(struct hpt_iop_hba * hba)
806 u_int32_t status, _tag, cptr;
809 if (hba->initialized) {
810 BUS_SPACE_WRT4_MVFREY2(pcie_f0_int_enable, 0);
813 status = BUS_SPACE_RD4_MVFREY2(f0_doorbell);
815 BUS_SPACE_WRT4_MVFREY2(f0_doorbell, status);
816 if (status & CPU_TO_F0_DRBL_MSG_A_BIT) {
817 u_int32_t msg = BUS_SPACE_RD4_MVFREY2(cpu_to_f0_msg_a);
818 hptiop_os_message_callback(hba, msg);
823 status = BUS_SPACE_RD4_MVFREY2(isr_cause);
825 BUS_SPACE_WRT4_MVFREY2(isr_cause, status);
827 cptr = *hba->u.mvfrey.outlist_cptr & 0xff;
828 while (hba->u.mvfrey.outlist_rptr != cptr) {
829 hba->u.mvfrey.outlist_rptr++;
830 if (hba->u.mvfrey.outlist_rptr == hba->u.mvfrey.list_count) {
831 hba->u.mvfrey.outlist_rptr = 0;
834 _tag = hba->u.mvfrey.outlist[hba->u.mvfrey.outlist_rptr].val;
835 hptiop_request_callback_mvfrey(hba, _tag);
838 } while (cptr != (*hba->u.mvfrey.outlist_cptr & 0xff));
841 if (hba->initialized) {
842 BUS_SPACE_WRT4_MVFREY2(pcie_f0_int_enable, 0x1010);
848 static int hptiop_send_sync_request_itl(struct hpt_iop_hba * hba,
849 u_int32_t req32, u_int32_t millisec)
854 BUS_SPACE_WRT4_ITL(inbound_queue, req32);
855 BUS_SPACE_RD4_ITL(outbound_intstatus);
857 for (i = 0; i < millisec; i++) {
858 hptiop_intr_itl(hba);
859 bus_space_read_region_4(hba->bar0t, hba->bar0h, req32 +
860 offsetof(struct hpt_iop_request_header, context),
861 (u_int32_t *)&temp64, 2);
870 static int hptiop_send_sync_request_mv(struct hpt_iop_hba *hba,
871 void *req, u_int32_t millisec)
875 hba->config_done = 0;
877 phy_addr = hba->ctlcfgcmd_phy |
878 (u_int64_t)MVIOP_MU_QUEUE_ADDR_HOST_BIT;
879 ((struct hpt_iop_request_get_config *)req)->header.flags |=
880 IOP_REQUEST_FLAG_SYNC_REQUEST |
881 IOP_REQUEST_FLAG_OUTPUT_CONTEXT;
882 hptiop_mv_inbound_write(phy_addr, hba);
883 BUS_SPACE_RD4_MV0(outbound_intmask);
885 for (i = 0; i < millisec; i++) {
887 if (hba->config_done)
894 static int hptiop_send_sync_request_mvfrey(struct hpt_iop_hba *hba,
895 void *req, u_int32_t millisec)
899 struct hpt_iop_request_header *reqhdr =
900 (struct hpt_iop_request_header *)req;
902 hba->config_done = 0;
904 phy_addr = hba->ctlcfgcmd_phy;
905 reqhdr->flags = IOP_REQUEST_FLAG_SYNC_REQUEST
906 | IOP_REQUEST_FLAG_OUTPUT_CONTEXT
907 | IOP_REQUEST_FLAG_ADDR_BITS
908 | ((phy_addr >> 16) & 0xffff0000);
909 reqhdr->context = ((phy_addr & 0xffffffff) << 32 )
910 | IOPMU_QUEUE_ADDR_HOST_BIT | reqhdr->type;
912 hba->u.mvfrey.inlist_wptr++;
913 index = hba->u.mvfrey.inlist_wptr & 0x3fff;
915 if (index == hba->u.mvfrey.list_count) {
917 hba->u.mvfrey.inlist_wptr &= ~0x3fff;
918 hba->u.mvfrey.inlist_wptr ^= CL_POINTER_TOGGLE;
921 hba->u.mvfrey.inlist[index].addr = phy_addr;
922 hba->u.mvfrey.inlist[index].intrfc_len = (reqhdr->size + 3) / 4;
924 BUS_SPACE_WRT4_MVFREY2(inbound_write_ptr, hba->u.mvfrey.inlist_wptr);
925 BUS_SPACE_RD4_MVFREY2(inbound_write_ptr);
927 for (i = 0; i < millisec; i++) {
928 hptiop_intr_mvfrey(hba);
929 if (hba->config_done)
936 static int hptiop_send_sync_msg(struct hpt_iop_hba *hba,
937 u_int32_t msg, u_int32_t millisec)
942 hba->ops->post_msg(hba, msg);
944 for (i=0; i<millisec; i++) {
945 hba->ops->iop_intr(hba);
951 return hba->msg_done? 0 : -1;
954 static int hptiop_get_config_itl(struct hpt_iop_hba * hba,
955 struct hpt_iop_request_get_config * config)
959 config->header.size = sizeof(struct hpt_iop_request_get_config);
960 config->header.type = IOP_REQUEST_TYPE_GET_CONFIG;
961 config->header.flags = IOP_REQUEST_FLAG_SYNC_REQUEST;
962 config->header.result = IOP_RESULT_PENDING;
963 config->header.context = 0;
965 req32 = BUS_SPACE_RD4_ITL(inbound_queue);
966 if (req32 == IOPMU_QUEUE_EMPTY)
969 bus_space_write_region_4(hba->bar0t, hba->bar0h,
970 req32, (u_int32_t *)config,
971 sizeof(struct hpt_iop_request_header) >> 2);
973 if (hptiop_send_sync_request_itl(hba, req32, 20000)) {
974 KdPrint(("hptiop: get config send cmd failed"));
978 bus_space_read_region_4(hba->bar0t, hba->bar0h,
979 req32, (u_int32_t *)config,
980 sizeof(struct hpt_iop_request_get_config) >> 2);
982 BUS_SPACE_WRT4_ITL(outbound_queue, req32);
987 static int hptiop_get_config_mv(struct hpt_iop_hba * hba,
988 struct hpt_iop_request_get_config * config)
990 struct hpt_iop_request_get_config *req;
992 if (!(req = hba->ctlcfg_ptr))
995 req->header.flags = 0;
996 req->header.type = IOP_REQUEST_TYPE_GET_CONFIG;
997 req->header.size = sizeof(struct hpt_iop_request_get_config);
998 req->header.result = IOP_RESULT_PENDING;
999 req->header.context = MVIOP_CMD_TYPE_GET_CONFIG;
1001 if (hptiop_send_sync_request_mv(hba, req, 20000)) {
1002 KdPrint(("hptiop: get config send cmd failed"));
1010 static int hptiop_get_config_mvfrey(struct hpt_iop_hba * hba,
1011 struct hpt_iop_request_get_config * config)
1013 struct hpt_iop_request_get_config *info = hba->u.mvfrey.config;
1015 if (info->header.size != sizeof(struct hpt_iop_request_get_config) ||
1016 info->header.type != IOP_REQUEST_TYPE_GET_CONFIG) {
1017 KdPrint(("hptiop: header size %x/%x type %x/%x",
1018 info->header.size, (int)sizeof(struct hpt_iop_request_get_config),
1019 info->header.type, IOP_REQUEST_TYPE_GET_CONFIG));
1023 config->interface_version = info->interface_version;
1024 config->firmware_version = info->firmware_version;
1025 config->max_requests = info->max_requests;
1026 config->request_size = info->request_size;
1027 config->max_sg_count = info->max_sg_count;
1028 config->data_transfer_length = info->data_transfer_length;
1029 config->alignment_mask = info->alignment_mask;
1030 config->max_devices = info->max_devices;
1031 config->sdram_size = info->sdram_size;
1033 KdPrint(("hptiop: maxreq %x reqsz %x datalen %x maxdev %x sdram %x",
1034 config->max_requests, config->request_size,
1035 config->data_transfer_length, config->max_devices,
1036 config->sdram_size));
1041 static int hptiop_set_config_itl(struct hpt_iop_hba *hba,
1042 struct hpt_iop_request_set_config *config)
1046 req32 = BUS_SPACE_RD4_ITL(inbound_queue);
1048 if (req32 == IOPMU_QUEUE_EMPTY)
1051 config->header.size = sizeof(struct hpt_iop_request_set_config);
1052 config->header.type = IOP_REQUEST_TYPE_SET_CONFIG;
1053 config->header.flags = IOP_REQUEST_FLAG_SYNC_REQUEST;
1054 config->header.result = IOP_RESULT_PENDING;
1055 config->header.context = 0;
1057 bus_space_write_region_4(hba->bar0t, hba->bar0h, req32,
1058 (u_int32_t *)config,
1059 sizeof(struct hpt_iop_request_set_config) >> 2);
1061 if (hptiop_send_sync_request_itl(hba, req32, 20000)) {
1062 KdPrint(("hptiop: set config send cmd failed"));
1066 BUS_SPACE_WRT4_ITL(outbound_queue, req32);
1071 static int hptiop_set_config_mv(struct hpt_iop_hba *hba,
1072 struct hpt_iop_request_set_config *config)
1074 struct hpt_iop_request_set_config *req;
1076 if (!(req = hba->ctlcfg_ptr))
1079 memcpy((u_int8_t *)req + sizeof(struct hpt_iop_request_header),
1080 (u_int8_t *)config + sizeof(struct hpt_iop_request_header),
1081 sizeof(struct hpt_iop_request_set_config) -
1082 sizeof(struct hpt_iop_request_header));
1084 req->header.flags = 0;
1085 req->header.type = IOP_REQUEST_TYPE_SET_CONFIG;
1086 req->header.size = sizeof(struct hpt_iop_request_set_config);
1087 req->header.result = IOP_RESULT_PENDING;
1088 req->header.context = MVIOP_CMD_TYPE_SET_CONFIG;
1090 if (hptiop_send_sync_request_mv(hba, req, 20000)) {
1091 KdPrint(("hptiop: set config send cmd failed"));
1098 static int hptiop_set_config_mvfrey(struct hpt_iop_hba *hba,
1099 struct hpt_iop_request_set_config *config)
1101 struct hpt_iop_request_set_config *req;
1103 if (!(req = hba->ctlcfg_ptr))
1106 memcpy((u_int8_t *)req + sizeof(struct hpt_iop_request_header),
1107 (u_int8_t *)config + sizeof(struct hpt_iop_request_header),
1108 sizeof(struct hpt_iop_request_set_config) -
1109 sizeof(struct hpt_iop_request_header));
1111 req->header.type = IOP_REQUEST_TYPE_SET_CONFIG;
1112 req->header.size = sizeof(struct hpt_iop_request_set_config);
1113 req->header.result = IOP_RESULT_PENDING;
1115 if (hptiop_send_sync_request_mvfrey(hba, req, 20000)) {
1116 KdPrint(("hptiop: set config send cmd failed"));
1123 static int hptiop_post_ioctl_command_itl(struct hpt_iop_hba *hba,
1125 struct hpt_iop_ioctl_param *pParams)
1128 struct hpt_iop_request_ioctl_command req;
1130 if ((((pParams->nInBufferSize + 3) & ~3) + pParams->nOutBufferSize) >
1131 (hba->max_request_size -
1132 offsetof(struct hpt_iop_request_ioctl_command, buf))) {
1133 device_printf(hba->pcidev, "request size beyond max value");
1137 req.header.size = offsetof(struct hpt_iop_request_ioctl_command, buf)
1138 + pParams->nInBufferSize;
1139 req.header.type = IOP_REQUEST_TYPE_IOCTL_COMMAND;
1140 req.header.flags = IOP_REQUEST_FLAG_SYNC_REQUEST;
1141 req.header.result = IOP_RESULT_PENDING;
1142 req.header.context = req32 + (u_int64_t)(unsigned long)hba->u.itl.mu;
1143 req.ioctl_code = HPT_CTL_CODE_BSD_TO_IOP(pParams->dwIoControlCode);
1144 req.inbuf_size = pParams->nInBufferSize;
1145 req.outbuf_size = pParams->nOutBufferSize;
1146 req.bytes_returned = 0;
1148 bus_space_write_region_4(hba->bar0t, hba->bar0h, req32, (u_int32_t *)&req,
1149 offsetof(struct hpt_iop_request_ioctl_command, buf)>>2);
1151 hptiop_lock_adapter(hba);
1153 BUS_SPACE_WRT4_ITL(inbound_queue, req32);
1154 BUS_SPACE_RD4_ITL(outbound_intstatus);
1156 bus_space_read_region_4(hba->bar0t, hba->bar0h, req32 +
1157 offsetof(struct hpt_iop_request_ioctl_command, header.context),
1158 (u_int32_t *)&temp64, 2);
1160 if (hptiop_sleep(hba, (void *)((unsigned long)hba->u.itl.mu + req32),
1161 PPAUSE, "hptctl", HPT_OSM_TIMEOUT)==0)
1163 hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET, 60000);
1164 bus_space_read_region_4(hba->bar0t, hba->bar0h,req32 +
1165 offsetof(struct hpt_iop_request_ioctl_command,
1167 (u_int32_t *)&temp64, 2);
1170 hptiop_unlock_adapter(hba);
1174 static int hptiop_bus_space_copyin(struct hpt_iop_hba *hba, u_int32_t bus,
1175 void *user, int size)
1180 for (i=0; i<size; i++) {
1181 if (copyin((u_int8_t *)user + i, &byte, 1))
1183 bus_space_write_1(hba->bar0t, hba->bar0h, bus + i, byte);
1189 static int hptiop_bus_space_copyout(struct hpt_iop_hba *hba, u_int32_t bus,
1190 void *user, int size)
1195 for (i=0; i<size; i++) {
1196 byte = bus_space_read_1(hba->bar0t, hba->bar0h, bus + i);
1197 if (copyout(&byte, (u_int8_t *)user + i, 1))
1204 static int hptiop_do_ioctl_itl(struct hpt_iop_hba *hba,
1205 struct hpt_iop_ioctl_param * pParams)
1210 if ((pParams->Magic != HPT_IOCTL_MAGIC) &&
1211 (pParams->Magic != HPT_IOCTL_MAGIC32))
1214 req32 = BUS_SPACE_RD4_ITL(inbound_queue);
1215 if (req32 == IOPMU_QUEUE_EMPTY)
1218 if (pParams->nInBufferSize)
1219 if (hptiop_bus_space_copyin(hba, req32 +
1220 offsetof(struct hpt_iop_request_ioctl_command, buf),
1221 (void *)pParams->lpInBuffer, pParams->nInBufferSize))
1224 if (hptiop_post_ioctl_command_itl(hba, req32, pParams))
1227 result = bus_space_read_4(hba->bar0t, hba->bar0h, req32 +
1228 offsetof(struct hpt_iop_request_ioctl_command,
1231 if (result == IOP_RESULT_SUCCESS) {
1232 if (pParams->nOutBufferSize)
1233 if (hptiop_bus_space_copyout(hba, req32 +
1234 offsetof(struct hpt_iop_request_ioctl_command, buf) +
1235 ((pParams->nInBufferSize + 3) & ~3),
1236 (void *)pParams->lpOutBuffer, pParams->nOutBufferSize))
1239 if (pParams->lpBytesReturned) {
1240 if (hptiop_bus_space_copyout(hba, req32 +
1241 offsetof(struct hpt_iop_request_ioctl_command, bytes_returned),
1242 (void *)pParams->lpBytesReturned, sizeof(unsigned long)))
1246 BUS_SPACE_WRT4_ITL(outbound_queue, req32);
1251 BUS_SPACE_WRT4_ITL(outbound_queue, req32);
1257 static int hptiop_post_ioctl_command_mv(struct hpt_iop_hba *hba,
1258 struct hpt_iop_request_ioctl_command *req,
1259 struct hpt_iop_ioctl_param *pParams)
1264 if ((((pParams->nInBufferSize + 3) & ~3) + pParams->nOutBufferSize) >
1265 (hba->max_request_size -
1266 offsetof(struct hpt_iop_request_ioctl_command, buf))) {
1267 device_printf(hba->pcidev, "request size beyond max value");
1271 req->ioctl_code = HPT_CTL_CODE_BSD_TO_IOP(pParams->dwIoControlCode);
1272 req->inbuf_size = pParams->nInBufferSize;
1273 req->outbuf_size = pParams->nOutBufferSize;
1274 req->header.size = offsetof(struct hpt_iop_request_ioctl_command, buf)
1275 + pParams->nInBufferSize;
1276 req->header.context = (u_int64_t)MVIOP_CMD_TYPE_IOCTL;
1277 req->header.type = IOP_REQUEST_TYPE_IOCTL_COMMAND;
1278 req->header.result = IOP_RESULT_PENDING;
1279 req->header.flags = IOP_REQUEST_FLAG_OUTPUT_CONTEXT;
1280 size = req->header.size >> 8;
1281 size = size > 3 ? 3 : size;
1282 req_phy = hba->ctlcfgcmd_phy | MVIOP_MU_QUEUE_ADDR_HOST_BIT | size;
1283 hptiop_mv_inbound_write(req_phy, hba);
1285 BUS_SPACE_RD4_MV0(outbound_intmask);
1287 while (hba->config_done == 0) {
1288 if (hptiop_sleep(hba, req, PPAUSE,
1289 "hptctl", HPT_OSM_TIMEOUT)==0)
1291 hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET, 60000);
1296 static int hptiop_do_ioctl_mv(struct hpt_iop_hba *hba,
1297 struct hpt_iop_ioctl_param *pParams)
1299 struct hpt_iop_request_ioctl_command *req;
1301 if ((pParams->Magic != HPT_IOCTL_MAGIC) &&
1302 (pParams->Magic != HPT_IOCTL_MAGIC32))
1305 req = (struct hpt_iop_request_ioctl_command *)(hba->ctlcfg_ptr);
1306 hba->config_done = 0;
1307 hptiop_lock_adapter(hba);
1308 if (pParams->nInBufferSize)
1309 if (copyin((void *)pParams->lpInBuffer,
1310 req->buf, pParams->nInBufferSize))
1312 if (hptiop_post_ioctl_command_mv(hba, req, pParams))
1315 if (hba->config_done == 1) {
1316 if (pParams->nOutBufferSize)
1317 if (copyout(req->buf +
1318 ((pParams->nInBufferSize + 3) & ~3),
1319 (void *)pParams->lpOutBuffer,
1320 pParams->nOutBufferSize))
1323 if (pParams->lpBytesReturned)
1324 if (copyout(&req->bytes_returned,
1325 (void*)pParams->lpBytesReturned,
1328 hptiop_unlock_adapter(hba);
1332 hptiop_unlock_adapter(hba);
1337 static int hptiop_post_ioctl_command_mvfrey(struct hpt_iop_hba *hba,
1338 struct hpt_iop_request_ioctl_command *req,
1339 struct hpt_iop_ioctl_param *pParams)
1344 phy_addr = hba->ctlcfgcmd_phy;
1346 if ((((pParams->nInBufferSize + 3) & ~3) + pParams->nOutBufferSize) >
1347 (hba->max_request_size -
1348 offsetof(struct hpt_iop_request_ioctl_command, buf))) {
1349 device_printf(hba->pcidev, "request size beyond max value");
1353 req->ioctl_code = HPT_CTL_CODE_BSD_TO_IOP(pParams->dwIoControlCode);
1354 req->inbuf_size = pParams->nInBufferSize;
1355 req->outbuf_size = pParams->nOutBufferSize;
1356 req->header.size = offsetof(struct hpt_iop_request_ioctl_command, buf)
1357 + pParams->nInBufferSize;
1359 req->header.type = IOP_REQUEST_TYPE_IOCTL_COMMAND;
1360 req->header.result = IOP_RESULT_PENDING;
1362 req->header.flags = IOP_REQUEST_FLAG_SYNC_REQUEST
1363 | IOP_REQUEST_FLAG_OUTPUT_CONTEXT
1364 | IOP_REQUEST_FLAG_ADDR_BITS
1365 | ((phy_addr >> 16) & 0xffff0000);
1366 req->header.context = ((phy_addr & 0xffffffff) << 32 )
1367 | IOPMU_QUEUE_ADDR_HOST_BIT | req->header.type;
1369 hba->u.mvfrey.inlist_wptr++;
1370 index = hba->u.mvfrey.inlist_wptr & 0x3fff;
1372 if (index == hba->u.mvfrey.list_count) {
1374 hba->u.mvfrey.inlist_wptr &= ~0x3fff;
1375 hba->u.mvfrey.inlist_wptr ^= CL_POINTER_TOGGLE;
1378 hba->u.mvfrey.inlist[index].addr = phy_addr;
1379 hba->u.mvfrey.inlist[index].intrfc_len = (req->header.size + 3) / 4;
1381 BUS_SPACE_WRT4_MVFREY2(inbound_write_ptr, hba->u.mvfrey.inlist_wptr);
1382 BUS_SPACE_RD4_MVFREY2(inbound_write_ptr);
1384 while (hba->config_done == 0) {
1385 if (hptiop_sleep(hba, req, PPAUSE,
1386 "hptctl", HPT_OSM_TIMEOUT)==0)
1388 hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET, 60000);
1393 static int hptiop_do_ioctl_mvfrey(struct hpt_iop_hba *hba,
1394 struct hpt_iop_ioctl_param *pParams)
1396 struct hpt_iop_request_ioctl_command *req;
1398 if ((pParams->Magic != HPT_IOCTL_MAGIC) &&
1399 (pParams->Magic != HPT_IOCTL_MAGIC32))
1402 req = (struct hpt_iop_request_ioctl_command *)(hba->ctlcfg_ptr);
1403 hba->config_done = 0;
1404 hptiop_lock_adapter(hba);
1405 if (pParams->nInBufferSize)
1406 if (copyin((void *)pParams->lpInBuffer,
1407 req->buf, pParams->nInBufferSize))
1409 if (hptiop_post_ioctl_command_mvfrey(hba, req, pParams))
1412 if (hba->config_done == 1) {
1413 if (pParams->nOutBufferSize)
1414 if (copyout(req->buf +
1415 ((pParams->nInBufferSize + 3) & ~3),
1416 (void *)pParams->lpOutBuffer,
1417 pParams->nOutBufferSize))
1420 if (pParams->lpBytesReturned)
1421 if (copyout(&req->bytes_returned,
1422 (void*)pParams->lpBytesReturned,
1425 hptiop_unlock_adapter(hba);
1429 hptiop_unlock_adapter(hba);
1434 static int hptiop_rescan_bus(struct hpt_iop_hba * hba)
1438 if ((ccb = xpt_alloc_ccb()) == NULL)
1440 if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(hba->sim),
1441 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1449 static bus_dmamap_callback_t hptiop_map_srb;
1450 static bus_dmamap_callback_t hptiop_post_scsi_command;
1451 static bus_dmamap_callback_t hptiop_mv_map_ctlcfg;
1452 static bus_dmamap_callback_t hptiop_mvfrey_map_ctlcfg;
1454 static int hptiop_alloc_pci_res_itl(struct hpt_iop_hba *hba)
1456 hba->bar0_rid = 0x10;
1457 hba->bar0_res = bus_alloc_resource_any(hba->pcidev,
1458 SYS_RES_MEMORY, &hba->bar0_rid, RF_ACTIVE);
1460 if (hba->bar0_res == NULL) {
1461 device_printf(hba->pcidev,
1462 "failed to get iop base adrress.\n");
1465 hba->bar0t = rman_get_bustag(hba->bar0_res);
1466 hba->bar0h = rman_get_bushandle(hba->bar0_res);
1467 hba->u.itl.mu = (struct hpt_iopmu_itl *)
1468 rman_get_virtual(hba->bar0_res);
1470 if (!hba->u.itl.mu) {
1471 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1472 hba->bar0_rid, hba->bar0_res);
1473 device_printf(hba->pcidev, "alloc mem res failed\n");
1480 static int hptiop_alloc_pci_res_mv(struct hpt_iop_hba *hba)
1482 hba->bar0_rid = 0x10;
1483 hba->bar0_res = bus_alloc_resource_any(hba->pcidev,
1484 SYS_RES_MEMORY, &hba->bar0_rid, RF_ACTIVE);
1486 if (hba->bar0_res == NULL) {
1487 device_printf(hba->pcidev, "failed to get iop bar0.\n");
1490 hba->bar0t = rman_get_bustag(hba->bar0_res);
1491 hba->bar0h = rman_get_bushandle(hba->bar0_res);
1492 hba->u.mv.regs = (struct hpt_iopmv_regs *)
1493 rman_get_virtual(hba->bar0_res);
1495 if (!hba->u.mv.regs) {
1496 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1497 hba->bar0_rid, hba->bar0_res);
1498 device_printf(hba->pcidev, "alloc bar0 mem res failed\n");
1502 hba->bar2_rid = 0x18;
1503 hba->bar2_res = bus_alloc_resource_any(hba->pcidev,
1504 SYS_RES_MEMORY, &hba->bar2_rid, RF_ACTIVE);
1506 if (hba->bar2_res == NULL) {
1507 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1508 hba->bar0_rid, hba->bar0_res);
1509 device_printf(hba->pcidev, "failed to get iop bar2.\n");
1513 hba->bar2t = rman_get_bustag(hba->bar2_res);
1514 hba->bar2h = rman_get_bushandle(hba->bar2_res);
1515 hba->u.mv.mu = (struct hpt_iopmu_mv *)rman_get_virtual(hba->bar2_res);
1517 if (!hba->u.mv.mu) {
1518 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1519 hba->bar0_rid, hba->bar0_res);
1520 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1521 hba->bar2_rid, hba->bar2_res);
1522 device_printf(hba->pcidev, "alloc mem bar2 res failed\n");
1529 static int hptiop_alloc_pci_res_mvfrey(struct hpt_iop_hba *hba)
1531 hba->bar0_rid = 0x10;
1532 hba->bar0_res = bus_alloc_resource_any(hba->pcidev,
1533 SYS_RES_MEMORY, &hba->bar0_rid, RF_ACTIVE);
1535 if (hba->bar0_res == NULL) {
1536 device_printf(hba->pcidev, "failed to get iop bar0.\n");
1539 hba->bar0t = rman_get_bustag(hba->bar0_res);
1540 hba->bar0h = rman_get_bushandle(hba->bar0_res);
1541 hba->u.mvfrey.config = (struct hpt_iop_request_get_config *)
1542 rman_get_virtual(hba->bar0_res);
1544 if (!hba->u.mvfrey.config) {
1545 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1546 hba->bar0_rid, hba->bar0_res);
1547 device_printf(hba->pcidev, "alloc bar0 mem res failed\n");
1551 hba->bar2_rid = 0x18;
1552 hba->bar2_res = bus_alloc_resource_any(hba->pcidev,
1553 SYS_RES_MEMORY, &hba->bar2_rid, RF_ACTIVE);
1555 if (hba->bar2_res == NULL) {
1556 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1557 hba->bar0_rid, hba->bar0_res);
1558 device_printf(hba->pcidev, "failed to get iop bar2.\n");
1562 hba->bar2t = rman_get_bustag(hba->bar2_res);
1563 hba->bar2h = rman_get_bushandle(hba->bar2_res);
1565 (struct hpt_iopmu_mvfrey *)rman_get_virtual(hba->bar2_res);
1567 if (!hba->u.mvfrey.mu) {
1568 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1569 hba->bar0_rid, hba->bar0_res);
1570 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1571 hba->bar2_rid, hba->bar2_res);
1572 device_printf(hba->pcidev, "alloc mem bar2 res failed\n");
1579 static void hptiop_release_pci_res_itl(struct hpt_iop_hba *hba)
1582 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1583 hba->bar0_rid, hba->bar0_res);
1586 static void hptiop_release_pci_res_mv(struct hpt_iop_hba *hba)
1589 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1590 hba->bar0_rid, hba->bar0_res);
1592 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1593 hba->bar2_rid, hba->bar2_res);
1596 static void hptiop_release_pci_res_mvfrey(struct hpt_iop_hba *hba)
1599 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1600 hba->bar0_rid, hba->bar0_res);
1602 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1603 hba->bar2_rid, hba->bar2_res);
1606 static int hptiop_internal_memalloc_mv(struct hpt_iop_hba *hba)
1608 if (bus_dma_tag_create(hba->parent_dmat,
1611 BUS_SPACE_MAXADDR_32BIT,
1616 BUS_SPACE_MAXSIZE_32BIT,
1618 #if __FreeBSD_version > 502000
1622 &hba->ctlcfg_dmat)) {
1623 device_printf(hba->pcidev, "alloc ctlcfg_dmat failed\n");
1627 if (bus_dmamem_alloc(hba->ctlcfg_dmat, (void **)&hba->ctlcfg_ptr,
1628 #if __FreeBSD_version>501000
1629 BUS_DMA_WAITOK | BUS_DMA_COHERENT,
1633 &hba->ctlcfg_dmamap) != 0) {
1634 device_printf(hba->pcidev,
1635 "bus_dmamem_alloc failed!\n");
1636 bus_dma_tag_destroy(hba->ctlcfg_dmat);
1640 if (bus_dmamap_load(hba->ctlcfg_dmat,
1641 hba->ctlcfg_dmamap, hba->ctlcfg_ptr,
1642 MVIOP_IOCTLCFG_SIZE,
1643 hptiop_mv_map_ctlcfg, hba, 0)) {
1644 device_printf(hba->pcidev, "bus_dmamap_load failed!\n");
1645 if (hba->ctlcfg_dmat)
1646 bus_dmamem_free(hba->ctlcfg_dmat,
1647 hba->ctlcfg_ptr, hba->ctlcfg_dmamap);
1648 bus_dma_tag_destroy(hba->ctlcfg_dmat);
1655 static int hptiop_internal_memalloc_mvfrey(struct hpt_iop_hba *hba)
1657 u_int32_t list_count = BUS_SPACE_RD4_MVFREY2(inbound_conf_ctl);
1661 if (list_count == 0) {
1665 hba->u.mvfrey.list_count = list_count;
1666 hba->u.mvfrey.internal_mem_size = 0x800
1667 + list_count * sizeof(struct mvfrey_inlist_entry)
1668 + list_count * sizeof(struct mvfrey_outlist_entry)
1670 if (bus_dma_tag_create(hba->parent_dmat,
1673 BUS_SPACE_MAXADDR_32BIT,
1676 hba->u.mvfrey.internal_mem_size,
1678 BUS_SPACE_MAXSIZE_32BIT,
1680 #if __FreeBSD_version > 502000
1684 &hba->ctlcfg_dmat)) {
1685 device_printf(hba->pcidev, "alloc ctlcfg_dmat failed\n");
1689 if (bus_dmamem_alloc(hba->ctlcfg_dmat, (void **)&hba->ctlcfg_ptr,
1690 #if __FreeBSD_version>501000
1691 BUS_DMA_WAITOK | BUS_DMA_COHERENT,
1695 &hba->ctlcfg_dmamap) != 0) {
1696 device_printf(hba->pcidev,
1697 "bus_dmamem_alloc failed!\n");
1698 bus_dma_tag_destroy(hba->ctlcfg_dmat);
1702 if (bus_dmamap_load(hba->ctlcfg_dmat,
1703 hba->ctlcfg_dmamap, hba->ctlcfg_ptr,
1704 hba->u.mvfrey.internal_mem_size,
1705 hptiop_mvfrey_map_ctlcfg, hba, 0)) {
1706 device_printf(hba->pcidev, "bus_dmamap_load failed!\n");
1707 if (hba->ctlcfg_dmat) {
1708 bus_dmamem_free(hba->ctlcfg_dmat,
1709 hba->ctlcfg_ptr, hba->ctlcfg_dmamap);
1710 bus_dma_tag_destroy(hba->ctlcfg_dmat);
1718 static int hptiop_internal_memfree_itl(struct hpt_iop_hba *hba) {
1722 static int hptiop_internal_memfree_mv(struct hpt_iop_hba *hba)
1724 if (hba->ctlcfg_dmat) {
1725 bus_dmamap_unload(hba->ctlcfg_dmat, hba->ctlcfg_dmamap);
1726 bus_dmamem_free(hba->ctlcfg_dmat,
1727 hba->ctlcfg_ptr, hba->ctlcfg_dmamap);
1728 bus_dma_tag_destroy(hba->ctlcfg_dmat);
1734 static int hptiop_internal_memfree_mvfrey(struct hpt_iop_hba *hba)
1736 if (hba->ctlcfg_dmat) {
1737 bus_dmamap_unload(hba->ctlcfg_dmat, hba->ctlcfg_dmamap);
1738 bus_dmamem_free(hba->ctlcfg_dmat,
1739 hba->ctlcfg_ptr, hba->ctlcfg_dmamap);
1740 bus_dma_tag_destroy(hba->ctlcfg_dmat);
1746 static int hptiop_reset_comm_mvfrey(struct hpt_iop_hba *hba)
1750 if (hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET_COMM, 3000))
1753 /* wait 100ms for MCU ready */
1758 BUS_SPACE_WRT4_MVFREY2(inbound_base,
1759 hba->u.mvfrey.inlist_phy & 0xffffffff);
1760 BUS_SPACE_WRT4_MVFREY2(inbound_base_high,
1761 (hba->u.mvfrey.inlist_phy >> 16) >> 16);
1763 BUS_SPACE_WRT4_MVFREY2(outbound_base,
1764 hba->u.mvfrey.outlist_phy & 0xffffffff);
1765 BUS_SPACE_WRT4_MVFREY2(outbound_base_high,
1766 (hba->u.mvfrey.outlist_phy >> 16) >> 16);
1768 BUS_SPACE_WRT4_MVFREY2(outbound_shadow_base,
1769 hba->u.mvfrey.outlist_cptr_phy & 0xffffffff);
1770 BUS_SPACE_WRT4_MVFREY2(outbound_shadow_base_high,
1771 (hba->u.mvfrey.outlist_cptr_phy >> 16) >> 16);
1773 hba->u.mvfrey.inlist_wptr = (hba->u.mvfrey.list_count - 1)
1774 | CL_POINTER_TOGGLE;
1775 *hba->u.mvfrey.outlist_cptr = (hba->u.mvfrey.list_count - 1)
1776 | CL_POINTER_TOGGLE;
1777 hba->u.mvfrey.outlist_rptr = hba->u.mvfrey.list_count - 1;
1783 * CAM driver interface
1785 static device_method_t driver_methods[] = {
1786 /* Device interface */
1787 DEVMETHOD(device_probe, hptiop_probe),
1788 DEVMETHOD(device_attach, hptiop_attach),
1789 DEVMETHOD(device_detach, hptiop_detach),
1790 DEVMETHOD(device_shutdown, hptiop_shutdown),
1794 static struct hptiop_adapter_ops hptiop_itl_ops = {
1795 .family = INTEL_BASED_IOP,
1796 .iop_wait_ready = hptiop_wait_ready_itl,
1797 .internal_memalloc = 0,
1798 .internal_memfree = hptiop_internal_memfree_itl,
1799 .alloc_pci_res = hptiop_alloc_pci_res_itl,
1800 .release_pci_res = hptiop_release_pci_res_itl,
1801 .enable_intr = hptiop_enable_intr_itl,
1802 .disable_intr = hptiop_disable_intr_itl,
1803 .get_config = hptiop_get_config_itl,
1804 .set_config = hptiop_set_config_itl,
1805 .iop_intr = hptiop_intr_itl,
1806 .post_msg = hptiop_post_msg_itl,
1807 .post_req = hptiop_post_req_itl,
1808 .do_ioctl = hptiop_do_ioctl_itl,
1812 static struct hptiop_adapter_ops hptiop_mv_ops = {
1813 .family = MV_BASED_IOP,
1814 .iop_wait_ready = hptiop_wait_ready_mv,
1815 .internal_memalloc = hptiop_internal_memalloc_mv,
1816 .internal_memfree = hptiop_internal_memfree_mv,
1817 .alloc_pci_res = hptiop_alloc_pci_res_mv,
1818 .release_pci_res = hptiop_release_pci_res_mv,
1819 .enable_intr = hptiop_enable_intr_mv,
1820 .disable_intr = hptiop_disable_intr_mv,
1821 .get_config = hptiop_get_config_mv,
1822 .set_config = hptiop_set_config_mv,
1823 .iop_intr = hptiop_intr_mv,
1824 .post_msg = hptiop_post_msg_mv,
1825 .post_req = hptiop_post_req_mv,
1826 .do_ioctl = hptiop_do_ioctl_mv,
1830 static struct hptiop_adapter_ops hptiop_mvfrey_ops = {
1831 .family = MVFREY_BASED_IOP,
1832 .iop_wait_ready = hptiop_wait_ready_mvfrey,
1833 .internal_memalloc = hptiop_internal_memalloc_mvfrey,
1834 .internal_memfree = hptiop_internal_memfree_mvfrey,
1835 .alloc_pci_res = hptiop_alloc_pci_res_mvfrey,
1836 .release_pci_res = hptiop_release_pci_res_mvfrey,
1837 .enable_intr = hptiop_enable_intr_mvfrey,
1838 .disable_intr = hptiop_disable_intr_mvfrey,
1839 .get_config = hptiop_get_config_mvfrey,
1840 .set_config = hptiop_set_config_mvfrey,
1841 .iop_intr = hptiop_intr_mvfrey,
1842 .post_msg = hptiop_post_msg_mvfrey,
1843 .post_req = hptiop_post_req_mvfrey,
1844 .do_ioctl = hptiop_do_ioctl_mvfrey,
1845 .reset_comm = hptiop_reset_comm_mvfrey,
1848 static driver_t hptiop_pci_driver = {
1851 sizeof(struct hpt_iop_hba)
1854 DRIVER_MODULE(hptiop, pci, hptiop_pci_driver, hptiop_devclass, 0, 0);
1855 MODULE_DEPEND(hptiop, cam, 1, 1, 1);
1857 static int hptiop_probe(device_t dev)
1859 struct hpt_iop_hba *hba;
1861 static char buf[256];
1863 struct hptiop_adapter_ops *ops;
1865 if (pci_get_vendor(dev) != 0x1103)
1868 id = pci_get_device(dev);
1878 ops = &hptiop_mvfrey_ops;
1899 ops = &hptiop_itl_ops;
1904 ops = &hptiop_mv_ops;
1910 device_printf(dev, "adapter at PCI %d:%d:%d, IRQ %d\n",
1911 pci_get_bus(dev), pci_get_slot(dev),
1912 pci_get_function(dev), pci_get_irq(dev));
1914 sprintf(buf, "RocketRAID %x %s Controller\n",
1915 id, sas ? "SAS" : "SATA");
1916 device_set_desc_copy(dev, buf);
1918 hba = (struct hpt_iop_hba *)device_get_softc(dev);
1919 bzero(hba, sizeof(struct hpt_iop_hba));
1922 KdPrint(("hba->ops=%p\n", hba->ops));
1926 static int hptiop_attach(device_t dev)
1928 struct hpt_iop_hba *hba = (struct hpt_iop_hba *)device_get_softc(dev);
1929 struct hpt_iop_request_get_config iop_config;
1930 struct hpt_iop_request_set_config set_config;
1932 struct cam_devq *devq;
1933 struct ccb_setasync ccb;
1934 u_int32_t unit = device_get_unit(dev);
1936 device_printf(dev, "%d RocketRAID 3xxx/4xxx controller driver %s\n",
1937 unit, driver_version);
1939 KdPrint(("hptiop: attach(%d, %d/%d/%d) ops=%p\n", unit,
1940 pci_get_bus(dev), pci_get_slot(dev),
1941 pci_get_function(dev), hba->ops));
1943 #if __FreeBSD_version >=440000
1944 pci_enable_busmaster(dev);
1947 hba->pciunit = unit;
1949 if (hba->ops->alloc_pci_res(hba))
1952 if (hba->ops->iop_wait_ready(hba, 2000)) {
1953 device_printf(dev, "adapter is not ready\n");
1954 goto release_pci_res;
1957 #if (__FreeBSD_version >= 500000)
1958 mtx_init(&hba->lock, "hptioplock", NULL, MTX_DEF);
1961 if (bus_dma_tag_create(bus_get_dma_tag(dev),/* PCI parent */
1964 BUS_SPACE_MAXADDR, /* lowaddr */
1965 BUS_SPACE_MAXADDR, /* highaddr */
1966 NULL, NULL, /* filter, filterarg */
1967 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
1968 BUS_SPACE_UNRESTRICTED, /* nsegments */
1969 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1971 #if __FreeBSD_version>502000
1972 NULL, /* lockfunc */
1973 NULL, /* lockfuncarg */
1975 &hba->parent_dmat /* tag */))
1977 device_printf(dev, "alloc parent_dmat failed\n");
1978 goto release_pci_res;
1981 if (hba->ops->family == MV_BASED_IOP) {
1982 if (hba->ops->internal_memalloc(hba)) {
1983 device_printf(dev, "alloc srb_dmat failed\n");
1984 goto destroy_parent_tag;
1988 if (hba->ops->get_config(hba, &iop_config)) {
1989 device_printf(dev, "get iop config failed.\n");
1990 goto get_config_failed;
1993 hba->firmware_version = iop_config.firmware_version;
1994 hba->interface_version = iop_config.interface_version;
1995 hba->max_requests = iop_config.max_requests;
1996 hba->max_devices = iop_config.max_devices;
1997 hba->max_request_size = iop_config.request_size;
1998 hba->max_sg_count = iop_config.max_sg_count;
2000 if (hba->ops->family == MVFREY_BASED_IOP) {
2001 if (hba->ops->internal_memalloc(hba)) {
2002 device_printf(dev, "alloc srb_dmat failed\n");
2003 goto destroy_parent_tag;
2005 if (hba->ops->reset_comm(hba)) {
2006 device_printf(dev, "reset comm failed\n");
2007 goto get_config_failed;
2011 if (bus_dma_tag_create(hba->parent_dmat,/* parent */
2013 BUS_SPACE_MAXADDR_32BIT+1, /* boundary */
2014 BUS_SPACE_MAXADDR, /* lowaddr */
2015 BUS_SPACE_MAXADDR, /* highaddr */
2016 NULL, NULL, /* filter, filterarg */
2017 PAGE_SIZE * (hba->max_sg_count-1), /* maxsize */
2018 hba->max_sg_count, /* nsegments */
2019 0x20000, /* maxsegsize */
2020 BUS_DMA_ALLOCNOW, /* flags */
2021 #if __FreeBSD_version>502000
2022 busdma_lock_mutex, /* lockfunc */
2023 &hba->lock, /* lockfuncarg */
2025 &hba->io_dmat /* tag */))
2027 device_printf(dev, "alloc io_dmat failed\n");
2028 goto get_config_failed;
2031 if (bus_dma_tag_create(hba->parent_dmat,/* parent */
2034 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
2035 BUS_SPACE_MAXADDR, /* highaddr */
2036 NULL, NULL, /* filter, filterarg */
2037 HPT_SRB_MAX_SIZE * HPT_SRB_MAX_QUEUE_SIZE + 0x20,
2039 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
2041 #if __FreeBSD_version>502000
2042 NULL, /* lockfunc */
2043 NULL, /* lockfuncarg */
2045 &hba->srb_dmat /* tag */))
2047 device_printf(dev, "alloc srb_dmat failed\n");
2048 goto destroy_io_dmat;
2051 if (bus_dmamem_alloc(hba->srb_dmat, (void **)&hba->uncached_ptr,
2052 #if __FreeBSD_version>501000
2053 BUS_DMA_WAITOK | BUS_DMA_COHERENT,
2057 &hba->srb_dmamap) != 0)
2059 device_printf(dev, "srb bus_dmamem_alloc failed!\n");
2060 goto destroy_srb_dmat;
2063 if (bus_dmamap_load(hba->srb_dmat,
2064 hba->srb_dmamap, hba->uncached_ptr,
2065 (HPT_SRB_MAX_SIZE * HPT_SRB_MAX_QUEUE_SIZE) + 0x20,
2066 hptiop_map_srb, hba, 0))
2068 device_printf(dev, "bus_dmamap_load failed!\n");
2069 goto srb_dmamem_free;
2072 if ((devq = cam_simq_alloc(hba->max_requests - 1 )) == NULL) {
2073 device_printf(dev, "cam_simq_alloc failed\n");
2074 goto srb_dmamap_unload;
2077 #if __FreeBSD_version <700000
2078 hba->sim = cam_sim_alloc(hptiop_action, hptiop_poll, driver_name,
2079 hba, unit, hba->max_requests - 1, 1, devq);
2081 hba->sim = cam_sim_alloc(hptiop_action, hptiop_poll, driver_name,
2082 hba, unit, &Giant, hba->max_requests - 1, 1, devq);
2085 device_printf(dev, "cam_sim_alloc failed\n");
2086 cam_simq_free(devq);
2087 goto srb_dmamap_unload;
2089 #if __FreeBSD_version <700000
2090 if (xpt_bus_register(hba->sim, 0) != CAM_SUCCESS)
2092 if (xpt_bus_register(hba->sim, dev, 0) != CAM_SUCCESS)
2095 device_printf(dev, "xpt_bus_register failed\n");
2099 if (xpt_create_path(&hba->path, /*periph */ NULL,
2100 cam_sim_path(hba->sim), CAM_TARGET_WILDCARD,
2101 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2102 device_printf(dev, "xpt_create_path failed\n");
2103 goto deregister_xpt_bus;
2106 bzero(&set_config, sizeof(set_config));
2107 set_config.iop_id = unit;
2108 set_config.vbus_id = cam_sim_path(hba->sim);
2109 set_config.max_host_request_size = HPT_SRB_MAX_REQ_SIZE;
2111 if (hba->ops->set_config(hba, &set_config)) {
2112 device_printf(dev, "set iop config failed.\n");
2116 xpt_setup_ccb(&ccb.ccb_h, hba->path, /*priority*/5);
2117 ccb.ccb_h.func_code = XPT_SASYNC_CB;
2118 ccb.event_enable = (AC_FOUND_DEVICE | AC_LOST_DEVICE);
2119 ccb.callback = hptiop_async;
2120 ccb.callback_arg = hba->sim;
2121 xpt_action((union ccb *)&ccb);
2124 if ((hba->irq_res = bus_alloc_resource(hba->pcidev, SYS_RES_IRQ,
2125 &rid, 0, ~0ul, 1, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
2126 device_printf(dev, "allocate irq failed!\n");
2130 #if __FreeBSD_version <700000
2131 if (bus_setup_intr(hba->pcidev, hba->irq_res, INTR_TYPE_CAM,
2132 hptiop_pci_intr, hba, &hba->irq_handle))
2134 if (bus_setup_intr(hba->pcidev, hba->irq_res, INTR_TYPE_CAM,
2135 NULL, hptiop_pci_intr, hba, &hba->irq_handle))
2138 device_printf(dev, "allocate intr function failed!\n");
2139 goto free_irq_resource;
2142 if (hptiop_send_sync_msg(hba,
2143 IOPMU_INBOUND_MSG0_START_BACKGROUND_TASK, 5000)) {
2144 device_printf(dev, "fail to start background task\n");
2145 goto teartown_irq_resource;
2148 hba->ops->enable_intr(hba);
2149 hba->initialized = 1;
2151 hba->ioctl_dev = make_dev(&hptiop_cdevsw, unit,
2152 UID_ROOT, GID_WHEEL /*GID_OPERATOR*/,
2153 S_IRUSR | S_IWUSR, "%s%d", driver_name, unit);
2155 #if __FreeBSD_version < 503000
2156 hba->ioctl_dev->si_drv1 = hba;
2162 teartown_irq_resource:
2163 bus_teardown_intr(dev, hba->irq_res, hba->irq_handle);
2166 bus_release_resource(dev, SYS_RES_IRQ, 0, hba->irq_res);
2169 xpt_free_path(hba->path);
2172 xpt_bus_deregister(cam_sim_path(hba->sim));
2175 cam_sim_free(hba->sim, /*free devq*/ TRUE);
2178 if (hba->uncached_ptr)
2179 bus_dmamap_unload(hba->srb_dmat, hba->srb_dmamap);
2182 if (hba->uncached_ptr)
2183 bus_dmamem_free(hba->srb_dmat,
2184 hba->uncached_ptr, hba->srb_dmamap);
2188 bus_dma_tag_destroy(hba->srb_dmat);
2192 bus_dma_tag_destroy(hba->io_dmat);
2195 hba->ops->internal_memfree(hba);
2198 if (hba->parent_dmat)
2199 bus_dma_tag_destroy(hba->parent_dmat);
2202 if (hba->ops->release_pci_res)
2203 hba->ops->release_pci_res(hba);
2208 static int hptiop_detach(device_t dev)
2210 struct hpt_iop_hba * hba = (struct hpt_iop_hba *)device_get_softc(dev);
2214 hptiop_lock_adapter(hba);
2215 for (i = 0; i < hba->max_devices; i++)
2216 if (hptiop_os_query_remove_device(hba, i)) {
2217 device_printf(dev, "%d file system is busy. id=%d",
2222 if ((error = hptiop_shutdown(dev)) != 0)
2224 if (hptiop_send_sync_msg(hba,
2225 IOPMU_INBOUND_MSG0_STOP_BACKGROUND_TASK, 60000))
2228 hptiop_release_resource(hba);
2231 hptiop_unlock_adapter(hba);
2235 static int hptiop_shutdown(device_t dev)
2237 struct hpt_iop_hba * hba = (struct hpt_iop_hba *)device_get_softc(dev);
2241 if (hba->flag & HPT_IOCTL_FLAG_OPEN) {
2242 device_printf(dev, "%d device is busy", hba->pciunit);
2246 hba->ops->disable_intr(hba);
2248 if (hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_SHUTDOWN, 60000))
2254 static void hptiop_pci_intr(void *arg)
2256 struct hpt_iop_hba * hba = (struct hpt_iop_hba *)arg;
2257 hptiop_lock_adapter(hba);
2258 hba->ops->iop_intr(hba);
2259 hptiop_unlock_adapter(hba);
2262 static void hptiop_poll(struct cam_sim *sim)
2264 hptiop_pci_intr(cam_sim_softc(sim));
2267 static void hptiop_async(void * callback_arg, u_int32_t code,
2268 struct cam_path * path, void * arg)
2272 static void hptiop_enable_intr_itl(struct hpt_iop_hba *hba)
2274 BUS_SPACE_WRT4_ITL(outbound_intmask,
2275 ~(IOPMU_OUTBOUND_INT_POSTQUEUE | IOPMU_OUTBOUND_INT_MSG0));
2278 static void hptiop_enable_intr_mv(struct hpt_iop_hba *hba)
2282 int_mask = BUS_SPACE_RD4_MV0(outbound_intmask);
2284 int_mask |= MVIOP_MU_OUTBOUND_INT_POSTQUEUE
2285 | MVIOP_MU_OUTBOUND_INT_MSG;
2286 BUS_SPACE_WRT4_MV0(outbound_intmask,int_mask);
2289 static void hptiop_enable_intr_mvfrey(struct hpt_iop_hba *hba)
2291 BUS_SPACE_WRT4_MVFREY2(f0_doorbell_enable, CPU_TO_F0_DRBL_MSG_A_BIT);
2292 BUS_SPACE_RD4_MVFREY2(f0_doorbell_enable);
2294 BUS_SPACE_WRT4_MVFREY2(isr_enable, 0x1);
2295 BUS_SPACE_RD4_MVFREY2(isr_enable);
2297 BUS_SPACE_WRT4_MVFREY2(pcie_f0_int_enable, 0x1010);
2298 BUS_SPACE_RD4_MVFREY2(pcie_f0_int_enable);
2301 static void hptiop_disable_intr_itl(struct hpt_iop_hba *hba)
2305 int_mask = BUS_SPACE_RD4_ITL(outbound_intmask);
2307 int_mask |= IOPMU_OUTBOUND_INT_POSTQUEUE | IOPMU_OUTBOUND_INT_MSG0;
2308 BUS_SPACE_WRT4_ITL(outbound_intmask, int_mask);
2309 BUS_SPACE_RD4_ITL(outbound_intstatus);
2312 static void hptiop_disable_intr_mv(struct hpt_iop_hba *hba)
2315 int_mask = BUS_SPACE_RD4_MV0(outbound_intmask);
2317 int_mask &= ~(MVIOP_MU_OUTBOUND_INT_MSG
2318 | MVIOP_MU_OUTBOUND_INT_POSTQUEUE);
2319 BUS_SPACE_WRT4_MV0(outbound_intmask,int_mask);
2320 BUS_SPACE_RD4_MV0(outbound_intmask);
2323 static void hptiop_disable_intr_mvfrey(struct hpt_iop_hba *hba)
2325 BUS_SPACE_WRT4_MVFREY2(f0_doorbell_enable, 0);
2326 BUS_SPACE_RD4_MVFREY2(f0_doorbell_enable);
2328 BUS_SPACE_WRT4_MVFREY2(isr_enable, 0);
2329 BUS_SPACE_RD4_MVFREY2(isr_enable);
2331 BUS_SPACE_WRT4_MVFREY2(pcie_f0_int_enable, 0);
2332 BUS_SPACE_RD4_MVFREY2(pcie_f0_int_enable);
2335 static void hptiop_reset_adapter(void *argv)
2337 struct hpt_iop_hba * hba = (struct hpt_iop_hba *)argv;
2338 if (hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET, 60000))
2340 hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_START_BACKGROUND_TASK, 5000);
2343 static void *hptiop_get_srb(struct hpt_iop_hba * hba)
2345 struct hpt_iop_srb * srb;
2347 if (hba->srb_list) {
2348 srb = hba->srb_list;
2349 hba->srb_list = srb->next;
2356 static void hptiop_free_srb(struct hpt_iop_hba *hba, struct hpt_iop_srb *srb)
2358 srb->next = hba->srb_list;
2359 hba->srb_list = srb;
2362 static void hptiop_action(struct cam_sim *sim, union ccb *ccb)
2364 struct hpt_iop_hba * hba = (struct hpt_iop_hba *)cam_sim_softc(sim);
2365 struct hpt_iop_srb * srb;
2368 switch (ccb->ccb_h.func_code) {
2371 hptiop_lock_adapter(hba);
2372 if (ccb->ccb_h.target_lun != 0 ||
2373 ccb->ccb_h.target_id >= hba->max_devices ||
2374 (ccb->ccb_h.flags & CAM_CDB_PHYS))
2376 ccb->ccb_h.status = CAM_TID_INVALID;
2381 if ((srb = hptiop_get_srb(hba)) == NULL) {
2382 device_printf(hba->pcidev, "srb allocated failed");
2383 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2389 error = bus_dmamap_load_ccb(hba->io_dmat,
2392 hptiop_post_scsi_command,
2396 if (error && error != EINPROGRESS) {
2397 device_printf(hba->pcidev,
2398 "%d bus_dmamap_load error %d",
2399 hba->pciunit, error);
2400 xpt_freeze_simq(hba->sim, 1);
2401 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2402 hptiop_free_srb(hba, srb);
2408 hptiop_unlock_adapter(hba);
2412 device_printf(hba->pcidev, "reset adapter");
2413 hptiop_lock_adapter(hba);
2415 hptiop_reset_adapter(hba);
2416 hptiop_unlock_adapter(hba);
2419 case XPT_GET_TRAN_SETTINGS:
2420 case XPT_SET_TRAN_SETTINGS:
2421 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2424 case XPT_CALC_GEOMETRY:
2425 #if __FreeBSD_version >= 500000
2426 cam_calc_geometry(&ccb->ccg, 1);
2428 ccb->ccg.heads = 255;
2429 ccb->ccg.secs_per_track = 63;
2430 ccb->ccg.cylinders = ccb->ccg.volume_size /
2431 (ccb->ccg.heads * ccb->ccg.secs_per_track);
2432 ccb->ccb_h.status = CAM_REQ_CMP;
2438 struct ccb_pathinq *cpi = &ccb->cpi;
2440 cpi->version_num = 1;
2441 cpi->hba_inquiry = PI_SDTR_ABLE;
2442 cpi->target_sprt = 0;
2443 cpi->hba_misc = PIM_NOBUSRESET;
2444 cpi->hba_eng_cnt = 0;
2445 cpi->max_target = hba->max_devices;
2447 cpi->unit_number = cam_sim_unit(sim);
2448 cpi->bus_id = cam_sim_bus(sim);
2449 cpi->initiator_id = hba->max_devices;
2450 cpi->base_transfer_speed = 3300;
2452 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2453 strncpy(cpi->hba_vid, "HPT ", HBA_IDLEN);
2454 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2455 cpi->transport = XPORT_SPI;
2456 cpi->transport_version = 2;
2457 cpi->protocol = PROTO_SCSI;
2458 cpi->protocol_version = SCSI_REV_2;
2459 cpi->ccb_h.status = CAM_REQ_CMP;
2464 ccb->ccb_h.status = CAM_REQ_INVALID;
2472 static void hptiop_post_req_itl(struct hpt_iop_hba *hba,
2473 struct hpt_iop_srb *srb,
2474 bus_dma_segment_t *segs, int nsegs)
2477 union ccb *ccb = srb->ccb;
2480 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
2481 cdb = ccb->csio.cdb_io.cdb_ptr;
2483 cdb = ccb->csio.cdb_io.cdb_bytes;
2485 KdPrint(("ccb=%p %x-%x-%x\n",
2486 ccb, *(u_int32_t *)cdb, *((u_int32_t *)cdb+1), *((u_int32_t *)cdb+2)));
2488 if (srb->srb_flag & HPT_SRB_FLAG_HIGH_MEM_ACESS) {
2489 u_int32_t iop_req32;
2490 struct hpt_iop_request_scsi_command req;
2492 iop_req32 = BUS_SPACE_RD4_ITL(inbound_queue);
2494 if (iop_req32 == IOPMU_QUEUE_EMPTY) {
2495 device_printf(hba->pcidev, "invaild req offset\n");
2496 ccb->ccb_h.status = CAM_BUSY;
2497 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
2498 hptiop_free_srb(hba, srb);
2503 if (ccb->csio.dxfer_len && nsegs > 0) {
2504 struct hpt_iopsg *psg = req.sg_list;
2505 for (idx = 0; idx < nsegs; idx++, psg++) {
2506 psg->pci_address = (u_int64_t)segs[idx].ds_addr;
2507 psg->size = segs[idx].ds_len;
2513 bcopy(cdb, req.cdb, ccb->csio.cdb_len);
2516 offsetof(struct hpt_iop_request_scsi_command, sg_list)
2517 + nsegs*sizeof(struct hpt_iopsg);
2518 req.header.type = IOP_REQUEST_TYPE_SCSI_COMMAND;
2519 req.header.flags = 0;
2520 req.header.result = IOP_RESULT_PENDING;
2521 req.header.context = (u_int64_t)(unsigned long)srb;
2522 req.dataxfer_length = ccb->csio.dxfer_len;
2524 req.target = ccb->ccb_h.target_id;
2525 req.lun = ccb->ccb_h.target_lun;
2527 bus_space_write_region_1(hba->bar0t, hba->bar0h, iop_req32,
2528 (u_int8_t *)&req, req.header.size);
2530 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2531 bus_dmamap_sync(hba->io_dmat,
2532 srb->dma_map, BUS_DMASYNC_PREREAD);
2534 else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
2535 bus_dmamap_sync(hba->io_dmat,
2536 srb->dma_map, BUS_DMASYNC_PREWRITE);
2538 BUS_SPACE_WRT4_ITL(inbound_queue,iop_req32);
2540 struct hpt_iop_request_scsi_command *req;
2542 req = (struct hpt_iop_request_scsi_command *)srb;
2543 if (ccb->csio.dxfer_len && nsegs > 0) {
2544 struct hpt_iopsg *psg = req->sg_list;
2545 for (idx = 0; idx < nsegs; idx++, psg++) {
2547 (u_int64_t)segs[idx].ds_addr;
2548 psg->size = segs[idx].ds_len;
2554 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;
2563 offsetof(struct hpt_iop_request_scsi_command, sg_list)
2564 + nsegs*sizeof(struct hpt_iopsg);
2565 req->header.context = (u_int64_t)srb->index |
2566 IOPMU_QUEUE_ADDR_HOST_BIT;
2567 req->header.flags = IOP_REQUEST_FLAG_OUTPUT_CONTEXT;
2569 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2570 bus_dmamap_sync(hba->io_dmat,
2571 srb->dma_map, BUS_DMASYNC_PREREAD);
2572 }else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
2573 bus_dmamap_sync(hba->io_dmat,
2574 srb->dma_map, BUS_DMASYNC_PREWRITE);
2577 if (hba->firmware_version > 0x01020000
2578 || hba->interface_version > 0x01020000) {
2579 u_int32_t size_bits;
2581 if (req->header.size < 256)
2582 size_bits = IOPMU_QUEUE_REQUEST_SIZE_BIT;
2583 else if (req->header.size < 512)
2584 size_bits = IOPMU_QUEUE_ADDR_HOST_BIT;
2586 size_bits = IOPMU_QUEUE_REQUEST_SIZE_BIT
2587 | IOPMU_QUEUE_ADDR_HOST_BIT;
2589 BUS_SPACE_WRT4_ITL(inbound_queue,
2590 (u_int32_t)srb->phy_addr | size_bits);
2592 BUS_SPACE_WRT4_ITL(inbound_queue, (u_int32_t)srb->phy_addr
2593 |IOPMU_QUEUE_ADDR_HOST_BIT);
2597 static void hptiop_post_req_mv(struct hpt_iop_hba *hba,
2598 struct hpt_iop_srb *srb,
2599 bus_dma_segment_t *segs, int nsegs)
2602 union ccb *ccb = srb->ccb;
2604 struct hpt_iop_request_scsi_command *req;
2607 req = (struct hpt_iop_request_scsi_command *)srb;
2608 req_phy = srb->phy_addr;
2610 if (ccb->csio.dxfer_len && nsegs > 0) {
2611 struct hpt_iopsg *psg = req->sg_list;
2612 for (idx = 0; idx < nsegs; idx++, psg++) {
2613 psg->pci_address = (u_int64_t)segs[idx].ds_addr;
2614 psg->size = segs[idx].ds_len;
2619 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
2620 cdb = ccb->csio.cdb_io.cdb_ptr;
2622 cdb = ccb->csio.cdb_io.cdb_bytes;
2624 bcopy(cdb, req->cdb, ccb->csio.cdb_len);
2625 req->header.type = IOP_REQUEST_TYPE_SCSI_COMMAND;
2626 req->header.result = IOP_RESULT_PENDING;
2627 req->dataxfer_length = ccb->csio.dxfer_len;
2629 req->target = ccb->ccb_h.target_id;
2630 req->lun = ccb->ccb_h.target_lun;
2631 req->header.size = sizeof(struct hpt_iop_request_scsi_command)
2632 - sizeof(struct hpt_iopsg)
2633 + nsegs * sizeof(struct hpt_iopsg);
2634 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2635 bus_dmamap_sync(hba->io_dmat,
2636 srb->dma_map, BUS_DMASYNC_PREREAD);
2638 else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
2639 bus_dmamap_sync(hba->io_dmat,
2640 srb->dma_map, BUS_DMASYNC_PREWRITE);
2641 req->header.context = (u_int64_t)srb->index
2642 << MVIOP_REQUEST_NUMBER_START_BIT
2643 | MVIOP_CMD_TYPE_SCSI;
2644 req->header.flags = IOP_REQUEST_FLAG_OUTPUT_CONTEXT;
2645 size = req->header.size >> 8;
2646 hptiop_mv_inbound_write(req_phy
2647 | MVIOP_MU_QUEUE_ADDR_HOST_BIT
2648 | (size > 3 ? 3 : size), hba);
2651 static void hptiop_post_req_mvfrey(struct hpt_iop_hba *hba,
2652 struct hpt_iop_srb *srb,
2653 bus_dma_segment_t *segs, int nsegs)
2656 union ccb *ccb = srb->ccb;
2658 struct hpt_iop_request_scsi_command *req;
2661 req = (struct hpt_iop_request_scsi_command *)srb;
2662 req_phy = srb->phy_addr;
2664 if (ccb->csio.dxfer_len && nsegs > 0) {
2665 struct hpt_iopsg *psg = req->sg_list;
2666 for (idx = 0; idx < nsegs; idx++, psg++) {
2667 psg->pci_address = (u_int64_t)segs[idx].ds_addr | 1;
2668 psg->size = segs[idx].ds_len;
2673 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
2674 cdb = ccb->csio.cdb_io.cdb_ptr;
2676 cdb = ccb->csio.cdb_io.cdb_bytes;
2678 bcopy(cdb, req->cdb, ccb->csio.cdb_len);
2679 req->header.type = IOP_REQUEST_TYPE_SCSI_COMMAND;
2680 req->header.result = IOP_RESULT_PENDING;
2681 req->dataxfer_length = ccb->csio.dxfer_len;
2683 req->target = ccb->ccb_h.target_id;
2684 req->lun = ccb->ccb_h.target_lun;
2685 req->header.size = sizeof(struct hpt_iop_request_scsi_command)
2686 - sizeof(struct hpt_iopsg)
2687 + nsegs * sizeof(struct hpt_iopsg);
2688 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2689 bus_dmamap_sync(hba->io_dmat,
2690 srb->dma_map, BUS_DMASYNC_PREREAD);
2692 else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
2693 bus_dmamap_sync(hba->io_dmat,
2694 srb->dma_map, BUS_DMASYNC_PREWRITE);
2696 req->header.flags = IOP_REQUEST_FLAG_OUTPUT_CONTEXT
2697 | IOP_REQUEST_FLAG_ADDR_BITS
2698 | ((req_phy >> 16) & 0xffff0000);
2699 req->header.context = ((req_phy & 0xffffffff) << 32 )
2701 | IOPMU_QUEUE_ADDR_HOST_BIT | req->header.type;
2703 hba->u.mvfrey.inlist_wptr++;
2704 index = hba->u.mvfrey.inlist_wptr & 0x3fff;
2706 if (index == hba->u.mvfrey.list_count) {
2708 hba->u.mvfrey.inlist_wptr &= ~0x3fff;
2709 hba->u.mvfrey.inlist_wptr ^= CL_POINTER_TOGGLE;
2712 hba->u.mvfrey.inlist[index].addr = req_phy;
2713 hba->u.mvfrey.inlist[index].intrfc_len = (req->header.size + 3) / 4;
2715 BUS_SPACE_WRT4_MVFREY2(inbound_write_ptr, hba->u.mvfrey.inlist_wptr);
2716 BUS_SPACE_RD4_MVFREY2(inbound_write_ptr);
2718 if (req->header.type == IOP_REQUEST_TYPE_SCSI_COMMAND) {
2719 ccb->ccb_h.timeout_ch = timeout(hptiop_reset_adapter, hba, 20*hz);
2723 static void hptiop_post_scsi_command(void *arg, bus_dma_segment_t *segs,
2724 int nsegs, int error)
2726 struct hpt_iop_srb *srb = (struct hpt_iop_srb *)arg;
2727 union ccb *ccb = srb->ccb;
2728 struct hpt_iop_hba *hba = srb->hba;
2730 if (error || nsegs > hba->max_sg_count) {
2731 KdPrint(("hptiop: func_code=%x tid=%x lun=%x nsegs=%d\n",
2732 ccb->ccb_h.func_code,
2733 ccb->ccb_h.target_id,
2734 ccb->ccb_h.target_lun, nsegs));
2735 ccb->ccb_h.status = CAM_BUSY;
2736 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
2737 hptiop_free_srb(hba, srb);
2742 hba->ops->post_req(hba, srb, segs, nsegs);
2745 static void hptiop_mv_map_ctlcfg(void *arg, bus_dma_segment_t *segs,
2746 int nsegs, int error)
2748 struct hpt_iop_hba *hba = (struct hpt_iop_hba *)arg;
2749 hba->ctlcfgcmd_phy = ((u_int64_t)segs->ds_addr + 0x1F)
2751 hba->ctlcfg_ptr = (u_int8_t *)(((unsigned long)hba->ctlcfg_ptr + 0x1F)
2755 static void hptiop_mvfrey_map_ctlcfg(void *arg, bus_dma_segment_t *segs,
2756 int nsegs, int error)
2758 struct hpt_iop_hba *hba = (struct hpt_iop_hba *)arg;
2761 u_int32_t list_count = hba->u.mvfrey.list_count;
2763 phy = ((u_int64_t)segs->ds_addr + 0x1F)
2765 p = (u_int8_t *)(((unsigned long)hba->ctlcfg_ptr + 0x1F)
2768 hba->ctlcfgcmd_phy = phy;
2769 hba->ctlcfg_ptr = p;
2774 hba->u.mvfrey.inlist = (struct mvfrey_inlist_entry *)p;
2775 hba->u.mvfrey.inlist_phy = phy;
2777 p += list_count * sizeof(struct mvfrey_inlist_entry);
2778 phy += list_count * sizeof(struct mvfrey_inlist_entry);
2780 hba->u.mvfrey.outlist = (struct mvfrey_outlist_entry *)p;
2781 hba->u.mvfrey.outlist_phy = phy;
2783 p += list_count * sizeof(struct mvfrey_outlist_entry);
2784 phy += list_count * sizeof(struct mvfrey_outlist_entry);
2786 hba->u.mvfrey.outlist_cptr = (u_int32_t *)p;
2787 hba->u.mvfrey.outlist_cptr_phy = phy;
2790 static void hptiop_map_srb(void *arg, bus_dma_segment_t *segs,
2791 int nsegs, int error)
2793 struct hpt_iop_hba * hba = (struct hpt_iop_hba *)arg;
2794 bus_addr_t phy_addr = (segs->ds_addr + 0x1F) & ~(bus_addr_t)0x1F;
2795 struct hpt_iop_srb *srb, *tmp_srb;
2798 if (error || nsegs == 0) {
2799 device_printf(hba->pcidev, "hptiop_map_srb error");
2804 srb = (struct hpt_iop_srb *)
2805 (((unsigned long)hba->uncached_ptr + 0x1F)
2806 & ~(unsigned long)0x1F);
2808 for (i = 0; i < HPT_SRB_MAX_QUEUE_SIZE; i++) {
2809 tmp_srb = (struct hpt_iop_srb *)
2810 ((char *)srb + i * HPT_SRB_MAX_SIZE);
2811 if (((unsigned long)tmp_srb & 0x1F) == 0) {
2812 if (bus_dmamap_create(hba->io_dmat,
2813 0, &tmp_srb->dma_map)) {
2814 device_printf(hba->pcidev, "dmamap create failed");
2818 bzero(tmp_srb, sizeof(struct hpt_iop_srb));
2821 if (hba->ctlcfg_ptr == 0) {/*itl iop*/
2822 tmp_srb->phy_addr = (u_int64_t)(u_int32_t)
2824 if (phy_addr & IOPMU_MAX_MEM_SUPPORT_MASK_32G)
2826 HPT_SRB_FLAG_HIGH_MEM_ACESS;
2828 tmp_srb->phy_addr = phy_addr;
2831 hptiop_free_srb(hba, tmp_srb);
2832 hba->srb[i] = tmp_srb;
2833 phy_addr += HPT_SRB_MAX_SIZE;
2836 device_printf(hba->pcidev, "invalid alignment");
2842 static void hptiop_os_message_callback(struct hpt_iop_hba * hba, u_int32_t msg)
2847 static int hptiop_os_query_remove_device(struct hpt_iop_hba * hba,
2850 struct cam_periph *periph = NULL;
2851 struct cam_path *path;
2852 int status, retval = 0;
2854 status = xpt_create_path(&path, NULL, hba->sim->path_id, target_id, 0);
2856 if (status == CAM_REQ_CMP) {
2857 if ((periph = cam_periph_find(path, "da")) != NULL) {
2858 if (periph->refcount >= 1) {
2859 device_printf(hba->pcidev, "%d ,"
2862 hba->pciunit, target_id, periph->refcount);
2866 xpt_free_path(path);
2871 static void hptiop_release_resource(struct hpt_iop_hba *hba)
2875 struct ccb_setasync ccb;
2877 xpt_setup_ccb(&ccb.ccb_h, hba->path, /*priority*/5);
2878 ccb.ccb_h.func_code = XPT_SASYNC_CB;
2879 ccb.event_enable = 0;
2880 ccb.callback = hptiop_async;
2881 ccb.callback_arg = hba->sim;
2882 xpt_action((union ccb *)&ccb);
2883 xpt_free_path(hba->path);
2887 xpt_bus_deregister(cam_sim_path(hba->sim));
2888 cam_sim_free(hba->sim, TRUE);
2891 if (hba->ctlcfg_dmat) {
2892 bus_dmamap_unload(hba->ctlcfg_dmat, hba->ctlcfg_dmamap);
2893 bus_dmamem_free(hba->ctlcfg_dmat,
2894 hba->ctlcfg_ptr, hba->ctlcfg_dmamap);
2895 bus_dma_tag_destroy(hba->ctlcfg_dmat);
2898 for (i = 0; i < HPT_SRB_MAX_QUEUE_SIZE; i++) {
2899 struct hpt_iop_srb *srb = hba->srb[i];
2901 bus_dmamap_destroy(hba->io_dmat, srb->dma_map);
2904 if (hba->srb_dmat) {
2905 bus_dmamap_unload(hba->srb_dmat, hba->srb_dmamap);
2906 bus_dmamap_destroy(hba->srb_dmat, hba->srb_dmamap);
2907 bus_dma_tag_destroy(hba->srb_dmat);
2911 bus_dma_tag_destroy(hba->io_dmat);
2913 if (hba->parent_dmat)
2914 bus_dma_tag_destroy(hba->parent_dmat);
2916 if (hba->irq_handle)
2917 bus_teardown_intr(hba->pcidev, hba->irq_res, hba->irq_handle);
2920 bus_release_resource(hba->pcidev, SYS_RES_IRQ,
2924 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
2925 hba->bar0_rid, hba->bar0_res);
2927 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
2928 hba->bar2_rid, hba->bar2_res);
2930 destroy_dev(hba->ioctl_dev);