1718cf2ccSPedro F. Giffuni /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni *
42f6cdcc8SScott Long * HighPoint RR3xxx/4xxx RAID Driver for FreeBSD
5022a7377SXin LI * Copyright (C) 2007-2012 HighPoint Technologies, Inc. All Rights Reserved.
62f6cdcc8SScott Long *
74439f8b4SScott Long * Redistribution and use in source and binary forms, with or without
84439f8b4SScott Long * modification, are permitted provided that the following conditions
94439f8b4SScott Long * are met:
104439f8b4SScott Long * 1. Redistributions of source code must retain the above copyright
114439f8b4SScott Long * notice, this list of conditions and the following disclaimer.
124439f8b4SScott Long * 2. Redistributions in binary form must reproduce the above copyright
134439f8b4SScott Long * notice, this list of conditions and the following disclaimer in the
144439f8b4SScott Long * documentation and/or other materials provided with the distribution.
154439f8b4SScott Long *
164439f8b4SScott Long * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
174439f8b4SScott Long * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
184439f8b4SScott Long * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
194439f8b4SScott Long * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
204439f8b4SScott Long * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
214439f8b4SScott Long * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
224439f8b4SScott Long * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
234439f8b4SScott Long * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
244439f8b4SScott Long * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
254439f8b4SScott Long * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
264439f8b4SScott Long * SUCH DAMAGE.
274439f8b4SScott Long */
284439f8b4SScott Long
294439f8b4SScott Long #include <sys/param.h>
304439f8b4SScott Long #include <sys/types.h>
314439f8b4SScott Long #include <sys/cons.h>
324439f8b4SScott Long #include <sys/time.h>
334439f8b4SScott Long #include <sys/systm.h>
34*e453e498SBrooks Davis #include <sys/stdarg.h>
354439f8b4SScott Long
364439f8b4SScott Long #include <sys/stat.h>
374439f8b4SScott Long #include <sys/malloc.h>
384439f8b4SScott Long #include <sys/conf.h>
394439f8b4SScott Long #include <sys/libkern.h>
404439f8b4SScott Long #include <sys/kernel.h>
414439f8b4SScott Long
424439f8b4SScott Long #include <sys/kthread.h>
434439f8b4SScott Long #include <sys/mutex.h>
444439f8b4SScott Long #include <sys/module.h>
454439f8b4SScott Long
464439f8b4SScott Long #include <sys/eventhandler.h>
474439f8b4SScott Long #include <sys/bus.h>
484439f8b4SScott Long #include <sys/taskqueue.h>
494439f8b4SScott Long #include <sys/ioccom.h>
504439f8b4SScott Long
514439f8b4SScott Long #include <machine/resource.h>
524439f8b4SScott Long #include <machine/bus.h>
534439f8b4SScott Long #include <sys/rman.h>
544439f8b4SScott Long
554439f8b4SScott Long #include <vm/vm.h>
564439f8b4SScott Long #include <vm/pmap.h>
574439f8b4SScott Long
584439f8b4SScott Long #include <dev/pci/pcireg.h>
594439f8b4SScott Long #include <dev/pci/pcivar.h>
604439f8b4SScott Long
614439f8b4SScott Long
624439f8b4SScott Long #include <cam/cam.h>
634439f8b4SScott Long #include <cam/cam_ccb.h>
644439f8b4SScott Long #include <cam/cam_sim.h>
654439f8b4SScott Long #include <cam/cam_xpt_sim.h>
664439f8b4SScott Long #include <cam/cam_debug.h>
674439f8b4SScott Long #include <cam/cam_periph.h>
684439f8b4SScott Long #include <cam/scsi/scsi_all.h>
694439f8b4SScott Long #include <cam/scsi/scsi_message.h>
704439f8b4SScott Long
714439f8b4SScott Long
724439f8b4SScott Long #include <dev/hptiop/hptiop.h>
734439f8b4SScott Long
74022a7377SXin LI static const char driver_name[] = "hptiop";
75bba8d13eSXin LI static const char driver_version[] = "v1.9";
764439f8b4SScott Long
772f6cdcc8SScott Long static int hptiop_send_sync_msg(struct hpt_iop_hba *hba,
782f6cdcc8SScott Long u_int32_t msg, u_int32_t millisec);
792f6cdcc8SScott Long static void hptiop_request_callback_itl(struct hpt_iop_hba *hba,
802f6cdcc8SScott Long u_int32_t req);
812f6cdcc8SScott Long static void hptiop_request_callback_mv(struct hpt_iop_hba *hba, u_int64_t req);
82022a7377SXin LI static void hptiop_request_callback_mvfrey(struct hpt_iop_hba *hba,
83022a7377SXin LI u_int32_t req);
842f6cdcc8SScott Long static void hptiop_os_message_callback(struct hpt_iop_hba *hba, u_int32_t msg);
852f6cdcc8SScott Long static int hptiop_do_ioctl_itl(struct hpt_iop_hba *hba,
862f6cdcc8SScott Long struct hpt_iop_ioctl_param *pParams);
872f6cdcc8SScott Long static int hptiop_do_ioctl_mv(struct hpt_iop_hba *hba,
882f6cdcc8SScott Long struct hpt_iop_ioctl_param *pParams);
89022a7377SXin LI static int hptiop_do_ioctl_mvfrey(struct hpt_iop_hba *hba,
90022a7377SXin LI struct hpt_iop_ioctl_param *pParams);
914439f8b4SScott Long static int hptiop_rescan_bus(struct hpt_iop_hba *hba);
922f6cdcc8SScott Long static int hptiop_alloc_pci_res_itl(struct hpt_iop_hba *hba);
932f6cdcc8SScott Long static int hptiop_alloc_pci_res_mv(struct hpt_iop_hba *hba);
94022a7377SXin LI static int hptiop_alloc_pci_res_mvfrey(struct hpt_iop_hba *hba);
952f6cdcc8SScott Long static int hptiop_get_config_itl(struct hpt_iop_hba *hba,
962f6cdcc8SScott Long struct hpt_iop_request_get_config *config);
972f6cdcc8SScott Long static int hptiop_get_config_mv(struct hpt_iop_hba *hba,
982f6cdcc8SScott Long struct hpt_iop_request_get_config *config);
99022a7377SXin LI static int hptiop_get_config_mvfrey(struct hpt_iop_hba *hba,
100022a7377SXin LI struct hpt_iop_request_get_config *config);
1012f6cdcc8SScott Long static int hptiop_set_config_itl(struct hpt_iop_hba *hba,
1022f6cdcc8SScott Long struct hpt_iop_request_set_config *config);
1032f6cdcc8SScott Long static int hptiop_set_config_mv(struct hpt_iop_hba *hba,
1042f6cdcc8SScott Long struct hpt_iop_request_set_config *config);
105022a7377SXin LI static int hptiop_set_config_mvfrey(struct hpt_iop_hba *hba,
106022a7377SXin LI struct hpt_iop_request_set_config *config);
1072f6cdcc8SScott Long static int hptiop_internal_memalloc_mv(struct hpt_iop_hba *hba);
108022a7377SXin LI static int hptiop_internal_memalloc_mvfrey(struct hpt_iop_hba *hba);
109022a7377SXin LI static int hptiop_internal_memfree_itl(struct hpt_iop_hba *hba);
1102f6cdcc8SScott Long static int hptiop_internal_memfree_mv(struct hpt_iop_hba *hba);
111022a7377SXin LI static int hptiop_internal_memfree_mvfrey(struct hpt_iop_hba *hba);
1122f6cdcc8SScott Long static int hptiop_post_ioctl_command_itl(struct hpt_iop_hba *hba,
1132f6cdcc8SScott Long u_int32_t req32, struct hpt_iop_ioctl_param *pParams);
1142f6cdcc8SScott Long static int hptiop_post_ioctl_command_mv(struct hpt_iop_hba *hba,
1152f6cdcc8SScott Long struct hpt_iop_request_ioctl_command *req,
1162f6cdcc8SScott Long struct hpt_iop_ioctl_param *pParams);
117022a7377SXin LI static int hptiop_post_ioctl_command_mvfrey(struct hpt_iop_hba *hba,
118022a7377SXin LI struct hpt_iop_request_ioctl_command *req,
119022a7377SXin LI struct hpt_iop_ioctl_param *pParams);
1202f6cdcc8SScott Long static void hptiop_post_req_itl(struct hpt_iop_hba *hba,
1212f6cdcc8SScott Long struct hpt_iop_srb *srb,
1222f6cdcc8SScott Long bus_dma_segment_t *segs, int nsegs);
1232f6cdcc8SScott Long static void hptiop_post_req_mv(struct hpt_iop_hba *hba,
1242f6cdcc8SScott Long struct hpt_iop_srb *srb,
1252f6cdcc8SScott Long bus_dma_segment_t *segs, int nsegs);
126022a7377SXin LI static void hptiop_post_req_mvfrey(struct hpt_iop_hba *hba,
127022a7377SXin LI struct hpt_iop_srb *srb,
128022a7377SXin LI bus_dma_segment_t *segs, int nsegs);
1292f6cdcc8SScott Long static void hptiop_post_msg_itl(struct hpt_iop_hba *hba, u_int32_t msg);
1302f6cdcc8SScott Long static void hptiop_post_msg_mv(struct hpt_iop_hba *hba, u_int32_t msg);
131022a7377SXin LI static void hptiop_post_msg_mvfrey(struct hpt_iop_hba *hba, u_int32_t msg);
1322f6cdcc8SScott Long static void hptiop_enable_intr_itl(struct hpt_iop_hba *hba);
1332f6cdcc8SScott Long static void hptiop_enable_intr_mv(struct hpt_iop_hba *hba);
134022a7377SXin LI static void hptiop_enable_intr_mvfrey(struct hpt_iop_hba *hba);
1352f6cdcc8SScott Long static void hptiop_disable_intr_itl(struct hpt_iop_hba *hba);
1362f6cdcc8SScott Long static void hptiop_disable_intr_mv(struct hpt_iop_hba *hba);
137022a7377SXin LI static void hptiop_disable_intr_mvfrey(struct hpt_iop_hba *hba);
1382f6cdcc8SScott Long static void hptiop_free_srb(struct hpt_iop_hba *hba, struct hpt_iop_srb *srb);
1392f6cdcc8SScott Long static int hptiop_os_query_remove_device(struct hpt_iop_hba *hba, int tid);
1404439f8b4SScott Long static int hptiop_probe(device_t dev);
1414439f8b4SScott Long static int hptiop_attach(device_t dev);
1424439f8b4SScott Long static int hptiop_detach(device_t dev);
1434439f8b4SScott Long static int hptiop_shutdown(device_t dev);
1444439f8b4SScott Long static void hptiop_action(struct cam_sim *sim, union ccb *ccb);
1454439f8b4SScott Long static void hptiop_poll(struct cam_sim *sim);
1464439f8b4SScott Long static void hptiop_async(void *callback_arg, u_int32_t code,
1474439f8b4SScott Long struct cam_path *path, void *arg);
1484439f8b4SScott Long static void hptiop_pci_intr(void *arg);
1494439f8b4SScott Long static void hptiop_release_resource(struct hpt_iop_hba *hba);
150022a7377SXin LI static void hptiop_reset_adapter(void *argv);
1514439f8b4SScott Long static d_open_t hptiop_open;
1524439f8b4SScott Long static d_close_t hptiop_close;
1534439f8b4SScott Long static d_ioctl_t hptiop_ioctl;
1544439f8b4SScott Long
1554439f8b4SScott Long static struct cdevsw hptiop_cdevsw = {
1564439f8b4SScott Long .d_open = hptiop_open,
1574439f8b4SScott Long .d_close = hptiop_close,
1584439f8b4SScott Long .d_ioctl = hptiop_ioctl,
1594439f8b4SScott Long .d_name = driver_name,
1604439f8b4SScott Long .d_version = D_VERSION,
1614439f8b4SScott Long };
1624439f8b4SScott Long
1634439f8b4SScott Long #define hba_from_dev(dev) \
164517a8a71SJohn Baldwin ((struct hpt_iop_hba *)((dev)->si_drv1))
1654439f8b4SScott Long
1662f6cdcc8SScott Long #define BUS_SPACE_WRT4_ITL(offset, value) bus_space_write_4(hba->bar0t,\
1672f6cdcc8SScott Long hba->bar0h, offsetof(struct hpt_iopmu_itl, offset), (value))
1682f6cdcc8SScott Long #define BUS_SPACE_RD4_ITL(offset) bus_space_read_4(hba->bar0t,\
1692f6cdcc8SScott Long hba->bar0h, offsetof(struct hpt_iopmu_itl, offset))
1702f6cdcc8SScott Long
1712f6cdcc8SScott Long #define BUS_SPACE_WRT4_MV0(offset, value) bus_space_write_4(hba->bar0t,\
1722f6cdcc8SScott Long hba->bar0h, offsetof(struct hpt_iopmv_regs, offset), value)
1732f6cdcc8SScott Long #define BUS_SPACE_RD4_MV0(offset) bus_space_read_4(hba->bar0t,\
1742f6cdcc8SScott Long hba->bar0h, offsetof(struct hpt_iopmv_regs, offset))
1752f6cdcc8SScott Long #define BUS_SPACE_WRT4_MV2(offset, value) bus_space_write_4(hba->bar2t,\
1762f6cdcc8SScott Long hba->bar2h, offsetof(struct hpt_iopmu_mv, offset), value)
1772f6cdcc8SScott Long #define BUS_SPACE_RD4_MV2(offset) bus_space_read_4(hba->bar2t,\
1782f6cdcc8SScott Long hba->bar2h, offsetof(struct hpt_iopmu_mv, offset))
1792f6cdcc8SScott Long
180022a7377SXin LI #define BUS_SPACE_WRT4_MVFREY2(offset, value) bus_space_write_4(hba->bar2t,\
181022a7377SXin LI hba->bar2h, offsetof(struct hpt_iopmu_mvfrey, offset), value)
182022a7377SXin LI #define BUS_SPACE_RD4_MVFREY2(offset) bus_space_read_4(hba->bar2t,\
183022a7377SXin LI hba->bar2h, offsetof(struct hpt_iopmu_mvfrey, offset))
184022a7377SXin LI
hptiop_open(ioctl_dev_t dev,int flags,int devtype,ioctl_thread_t proc)1854439f8b4SScott Long static int hptiop_open(ioctl_dev_t dev, int flags,
1864439f8b4SScott Long int devtype, ioctl_thread_t proc)
1874439f8b4SScott Long {
1884439f8b4SScott Long struct hpt_iop_hba *hba = hba_from_dev(dev);
1894439f8b4SScott Long
1904439f8b4SScott Long if (hba==NULL)
1914439f8b4SScott Long return ENXIO;
1924439f8b4SScott Long if (hba->flag & HPT_IOCTL_FLAG_OPEN)
1934439f8b4SScott Long return EBUSY;
1944439f8b4SScott Long hba->flag |= HPT_IOCTL_FLAG_OPEN;
1954439f8b4SScott Long return 0;
1964439f8b4SScott Long }
1974439f8b4SScott Long
hptiop_close(ioctl_dev_t dev,int flags,int devtype,ioctl_thread_t proc)1984439f8b4SScott Long static int hptiop_close(ioctl_dev_t dev, int flags,
1994439f8b4SScott Long int devtype, ioctl_thread_t proc)
2004439f8b4SScott Long {
2014439f8b4SScott Long struct hpt_iop_hba *hba = hba_from_dev(dev);
2024439f8b4SScott Long hba->flag &= ~(u_int32_t)HPT_IOCTL_FLAG_OPEN;
2034439f8b4SScott Long return 0;
2044439f8b4SScott Long }
2054439f8b4SScott Long
hptiop_ioctl(ioctl_dev_t dev,u_long cmd,caddr_t data,int flags,ioctl_thread_t proc)2064439f8b4SScott Long static int hptiop_ioctl(ioctl_dev_t dev, u_long cmd, caddr_t data,
2074439f8b4SScott Long int flags, ioctl_thread_t proc)
2084439f8b4SScott Long {
2094439f8b4SScott Long int ret = EFAULT;
2104439f8b4SScott Long struct hpt_iop_hba *hba = hba_from_dev(dev);
2114439f8b4SScott Long
2124439f8b4SScott Long switch (cmd) {
2134439f8b4SScott Long case HPT_DO_IOCONTROL:
2142f6cdcc8SScott Long ret = hba->ops->do_ioctl(hba,
2152f6cdcc8SScott Long (struct hpt_iop_ioctl_param *)data);
2164439f8b4SScott Long break;
2174439f8b4SScott Long case HPT_SCAN_BUS:
2184439f8b4SScott Long ret = hptiop_rescan_bus(hba);
2194439f8b4SScott Long break;
2204439f8b4SScott Long }
2214439f8b4SScott Long return ret;
2224439f8b4SScott Long }
2234439f8b4SScott Long
hptiop_mv_outbound_read(struct hpt_iop_hba * hba)2242f6cdcc8SScott Long static u_int64_t hptiop_mv_outbound_read(struct hpt_iop_hba *hba)
2254439f8b4SScott Long {
2262f6cdcc8SScott Long u_int64_t p;
2272f6cdcc8SScott Long u_int32_t outbound_tail = BUS_SPACE_RD4_MV2(outbound_tail);
2282f6cdcc8SScott Long u_int32_t outbound_head = BUS_SPACE_RD4_MV2(outbound_head);
2292f6cdcc8SScott Long
2302f6cdcc8SScott Long if (outbound_tail != outbound_head) {
2312f6cdcc8SScott Long bus_space_read_region_4(hba->bar2t, hba->bar2h,
2322f6cdcc8SScott Long offsetof(struct hpt_iopmu_mv,
2332f6cdcc8SScott Long outbound_q[outbound_tail]),
2342f6cdcc8SScott Long (u_int32_t *)&p, 2);
2352f6cdcc8SScott Long
2362f6cdcc8SScott Long outbound_tail++;
2372f6cdcc8SScott Long
2382f6cdcc8SScott Long if (outbound_tail == MVIOP_QUEUE_LEN)
2392f6cdcc8SScott Long outbound_tail = 0;
2402f6cdcc8SScott Long
2412f6cdcc8SScott Long BUS_SPACE_WRT4_MV2(outbound_tail, outbound_tail);
2422f6cdcc8SScott Long return p;
2432f6cdcc8SScott Long } else
2442f6cdcc8SScott Long return 0;
2454439f8b4SScott Long }
2464439f8b4SScott Long
hptiop_mv_inbound_write(u_int64_t p,struct hpt_iop_hba * hba)2472f6cdcc8SScott Long static void hptiop_mv_inbound_write(u_int64_t p, struct hpt_iop_hba *hba)
2484439f8b4SScott Long {
2492f6cdcc8SScott Long u_int32_t inbound_head = BUS_SPACE_RD4_MV2(inbound_head);
2502f6cdcc8SScott Long u_int32_t head = inbound_head + 1;
2512f6cdcc8SScott Long
2522f6cdcc8SScott Long if (head == MVIOP_QUEUE_LEN)
2532f6cdcc8SScott Long head = 0;
2542f6cdcc8SScott Long
2552f6cdcc8SScott Long bus_space_write_region_4(hba->bar2t, hba->bar2h,
2562f6cdcc8SScott Long offsetof(struct hpt_iopmu_mv, inbound_q[inbound_head]),
2572f6cdcc8SScott Long (u_int32_t *)&p, 2);
2582f6cdcc8SScott Long BUS_SPACE_WRT4_MV2(inbound_head, head);
2592f6cdcc8SScott Long BUS_SPACE_WRT4_MV0(inbound_doorbell, MVIOP_MU_INBOUND_INT_POSTQUEUE);
2604439f8b4SScott Long }
2614439f8b4SScott Long
hptiop_post_msg_itl(struct hpt_iop_hba * hba,u_int32_t msg)2622f6cdcc8SScott Long static void hptiop_post_msg_itl(struct hpt_iop_hba *hba, u_int32_t msg)
2634439f8b4SScott Long {
2642f6cdcc8SScott Long BUS_SPACE_WRT4_ITL(inbound_msgaddr0, msg);
2652f6cdcc8SScott Long BUS_SPACE_RD4_ITL(outbound_intstatus);
2664439f8b4SScott Long }
2674439f8b4SScott Long
hptiop_post_msg_mv(struct hpt_iop_hba * hba,u_int32_t msg)2682f6cdcc8SScott Long static void hptiop_post_msg_mv(struct hpt_iop_hba *hba, u_int32_t msg)
2694439f8b4SScott Long {
2702f6cdcc8SScott Long
2712f6cdcc8SScott Long BUS_SPACE_WRT4_MV2(inbound_msg, msg);
2722f6cdcc8SScott Long BUS_SPACE_WRT4_MV0(inbound_doorbell, MVIOP_MU_INBOUND_INT_MSG);
2732f6cdcc8SScott Long
2742f6cdcc8SScott Long BUS_SPACE_RD4_MV0(outbound_intmask);
2754439f8b4SScott Long }
2764439f8b4SScott Long
hptiop_post_msg_mvfrey(struct hpt_iop_hba * hba,u_int32_t msg)277022a7377SXin LI static void hptiop_post_msg_mvfrey(struct hpt_iop_hba *hba, u_int32_t msg)
278022a7377SXin LI {
279022a7377SXin LI BUS_SPACE_WRT4_MVFREY2(f0_to_cpu_msg_a, msg);
280022a7377SXin LI BUS_SPACE_RD4_MVFREY2(f0_to_cpu_msg_a);
281022a7377SXin LI }
282022a7377SXin LI
hptiop_wait_ready_itl(struct hpt_iop_hba * hba,u_int32_t millisec)2832f6cdcc8SScott Long static int hptiop_wait_ready_itl(struct hpt_iop_hba * hba, u_int32_t millisec)
2844439f8b4SScott Long {
2854439f8b4SScott Long u_int32_t req=0;
2864439f8b4SScott Long int i;
2874439f8b4SScott Long
2884439f8b4SScott Long for (i = 0; i < millisec; i++) {
2892f6cdcc8SScott Long req = BUS_SPACE_RD4_ITL(inbound_queue);
2904439f8b4SScott Long if (req != IOPMU_QUEUE_EMPTY)
2914439f8b4SScott Long break;
2924439f8b4SScott Long DELAY(1000);
2934439f8b4SScott Long }
2944439f8b4SScott Long
2954439f8b4SScott Long if (req!=IOPMU_QUEUE_EMPTY) {
2962f6cdcc8SScott Long BUS_SPACE_WRT4_ITL(outbound_queue, req);
2972f6cdcc8SScott Long BUS_SPACE_RD4_ITL(outbound_intstatus);
2984439f8b4SScott Long return 0;
2994439f8b4SScott Long }
3004439f8b4SScott Long
3014439f8b4SScott Long return -1;
3024439f8b4SScott Long }
3034439f8b4SScott Long
hptiop_wait_ready_mv(struct hpt_iop_hba * hba,u_int32_t millisec)3042f6cdcc8SScott Long static int hptiop_wait_ready_mv(struct hpt_iop_hba * hba, u_int32_t millisec)
3054439f8b4SScott Long {
3062f6cdcc8SScott Long if (hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_NOP, millisec))
3074439f8b4SScott Long return -1;
3084439f8b4SScott Long
3094439f8b4SScott Long return 0;
3104439f8b4SScott Long }
3114439f8b4SScott Long
hptiop_wait_ready_mvfrey(struct hpt_iop_hba * hba,u_int32_t millisec)312022a7377SXin LI static int hptiop_wait_ready_mvfrey(struct hpt_iop_hba * hba,
313022a7377SXin LI u_int32_t millisec)
314022a7377SXin LI {
315022a7377SXin LI if (hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_NOP, millisec))
316022a7377SXin LI return -1;
317022a7377SXin LI
318022a7377SXin LI return 0;
319022a7377SXin LI }
320022a7377SXin LI
hptiop_request_callback_itl(struct hpt_iop_hba * hba,u_int32_t index)3212f6cdcc8SScott Long static void hptiop_request_callback_itl(struct hpt_iop_hba * hba,
3222f6cdcc8SScott Long u_int32_t index)
3234439f8b4SScott Long {
3244439f8b4SScott Long struct hpt_iop_srb *srb;
3254d24901aSPedro F. Giffuni struct hpt_iop_request_scsi_command *req=NULL;
3264439f8b4SScott Long union ccb *ccb;
3274439f8b4SScott Long u_int8_t *cdb;
3282f6cdcc8SScott Long u_int32_t result, temp, dxfer;
3292f6cdcc8SScott Long u_int64_t temp64;
3304439f8b4SScott Long
3312f6cdcc8SScott Long if (index & IOPMU_QUEUE_MASK_HOST_BITS) { /*host req*/
3322f6cdcc8SScott Long if (hba->firmware_version > 0x01020000 ||
3332f6cdcc8SScott Long hba->interface_version > 0x01020000) {
3344439f8b4SScott Long srb = hba->srb[index & ~(u_int32_t)
3352f6cdcc8SScott Long (IOPMU_QUEUE_ADDR_HOST_BIT
3362f6cdcc8SScott Long | IOPMU_QUEUE_REQUEST_RESULT_BIT)];
3374439f8b4SScott Long req = (struct hpt_iop_request_scsi_command *)srb;
3384439f8b4SScott Long if (index & IOPMU_QUEUE_REQUEST_RESULT_BIT)
3392f6cdcc8SScott Long result = IOP_RESULT_SUCCESS;
3402f6cdcc8SScott Long else
3412f6cdcc8SScott Long result = req->header.result;
3424439f8b4SScott Long } else {
3432f6cdcc8SScott Long srb = hba->srb[index &
3442f6cdcc8SScott Long ~(u_int32_t)IOPMU_QUEUE_ADDR_HOST_BIT];
3454439f8b4SScott Long req = (struct hpt_iop_request_scsi_command *)srb;
3462f6cdcc8SScott Long result = req->header.result;
3474439f8b4SScott Long }
3482f6cdcc8SScott Long dxfer = req->dataxfer_length;
3494439f8b4SScott Long goto srb_complete;
3504439f8b4SScott Long }
3514439f8b4SScott Long
3522f6cdcc8SScott Long /*iop req*/
3532f6cdcc8SScott Long temp = bus_space_read_4(hba->bar0t, hba->bar0h, index +
3542f6cdcc8SScott Long offsetof(struct hpt_iop_request_header, type));
3552f6cdcc8SScott Long result = bus_space_read_4(hba->bar0t, hba->bar0h, index +
3562f6cdcc8SScott Long offsetof(struct hpt_iop_request_header, result));
3572f6cdcc8SScott Long switch(temp) {
3584439f8b4SScott Long case IOP_REQUEST_TYPE_IOCTL_COMMAND:
3594439f8b4SScott Long {
3602f6cdcc8SScott Long temp64 = 0;
3612f6cdcc8SScott Long bus_space_write_region_4(hba->bar0t, hba->bar0h, index +
3622f6cdcc8SScott Long offsetof(struct hpt_iop_request_header, context),
3632f6cdcc8SScott Long (u_int32_t *)&temp64, 2);
3642f6cdcc8SScott Long wakeup((void *)((unsigned long)hba->u.itl.mu + index));
3654439f8b4SScott Long break;
3664439f8b4SScott Long }
3674439f8b4SScott Long
3684439f8b4SScott Long case IOP_REQUEST_TYPE_SCSI_COMMAND:
3692f6cdcc8SScott Long bus_space_read_region_4(hba->bar0t, hba->bar0h, index +
3702f6cdcc8SScott Long offsetof(struct hpt_iop_request_header, context),
3712f6cdcc8SScott Long (u_int32_t *)&temp64, 2);
3722f6cdcc8SScott Long srb = (struct hpt_iop_srb *)(unsigned long)temp64;
3732f6cdcc8SScott Long dxfer = bus_space_read_4(hba->bar0t, hba->bar0h,
3742f6cdcc8SScott Long index + offsetof(struct hpt_iop_request_scsi_command,
3752f6cdcc8SScott Long dataxfer_length));
3764439f8b4SScott Long srb_complete:
3774439f8b4SScott Long ccb = (union ccb *)srb->ccb;
3784439f8b4SScott Long if (ccb->ccb_h.flags & CAM_CDB_POINTER)
3794439f8b4SScott Long cdb = ccb->csio.cdb_io.cdb_ptr;
3804439f8b4SScott Long else
3814439f8b4SScott Long cdb = ccb->csio.cdb_io.cdb_bytes;
3824439f8b4SScott Long
3834439f8b4SScott Long if (cdb[0] == SYNCHRONIZE_CACHE) { /* ??? */
3844439f8b4SScott Long ccb->ccb_h.status = CAM_REQ_CMP;
3854439f8b4SScott Long goto scsi_done;
3864439f8b4SScott Long }
3874439f8b4SScott Long
3882f6cdcc8SScott Long switch (result) {
3894439f8b4SScott Long case IOP_RESULT_SUCCESS:
3904439f8b4SScott Long switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
3914439f8b4SScott Long case CAM_DIR_IN:
3924439f8b4SScott Long bus_dmamap_sync(hba->io_dmat,
3934439f8b4SScott Long srb->dma_map, BUS_DMASYNC_POSTREAD);
3944439f8b4SScott Long bus_dmamap_unload(hba->io_dmat, srb->dma_map);
3954439f8b4SScott Long break;
3964439f8b4SScott Long case CAM_DIR_OUT:
3974439f8b4SScott Long bus_dmamap_sync(hba->io_dmat,
3984439f8b4SScott Long srb->dma_map, BUS_DMASYNC_POSTWRITE);
3994439f8b4SScott Long bus_dmamap_unload(hba->io_dmat, srb->dma_map);
4004439f8b4SScott Long break;
4014439f8b4SScott Long }
4024439f8b4SScott Long
4034439f8b4SScott Long ccb->ccb_h.status = CAM_REQ_CMP;
4044439f8b4SScott Long break;
4054439f8b4SScott Long
4064439f8b4SScott Long case IOP_RESULT_BAD_TARGET:
4074439f8b4SScott Long ccb->ccb_h.status = CAM_DEV_NOT_THERE;
4084439f8b4SScott Long break;
4094439f8b4SScott Long case IOP_RESULT_BUSY:
4104439f8b4SScott Long ccb->ccb_h.status = CAM_BUSY;
4114439f8b4SScott Long break;
4124439f8b4SScott Long case IOP_RESULT_INVALID_REQUEST:
4134439f8b4SScott Long ccb->ccb_h.status = CAM_REQ_INVALID;
4144439f8b4SScott Long break;
4154439f8b4SScott Long case IOP_RESULT_FAIL:
4164439f8b4SScott Long ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
4174439f8b4SScott Long break;
4184439f8b4SScott Long case IOP_RESULT_RESET:
4194439f8b4SScott Long ccb->ccb_h.status = CAM_BUSY;
4204439f8b4SScott Long break;
4212f6cdcc8SScott Long case IOP_RESULT_CHECK_CONDITION:
4221e5addb7SMarius Strobl memset(&ccb->csio.sense_data, 0,
4231e5addb7SMarius Strobl sizeof(ccb->csio.sense_data));
4241e5addb7SMarius Strobl if (dxfer < ccb->csio.sense_len)
4251e5addb7SMarius Strobl ccb->csio.sense_resid = ccb->csio.sense_len -
4261e5addb7SMarius Strobl dxfer;
4271e5addb7SMarius Strobl else
4281e5addb7SMarius Strobl ccb->csio.sense_resid = 0;
4292f6cdcc8SScott Long if (srb->srb_flag & HPT_SRB_FLAG_HIGH_MEM_ACESS) {/*iop*/
4302f6cdcc8SScott Long bus_space_read_region_1(hba->bar0t, hba->bar0h,
4312f6cdcc8SScott Long index + offsetof(struct hpt_iop_request_scsi_command,
4322f6cdcc8SScott Long sg_list), (u_int8_t *)&ccb->csio.sense_data,
4332f6cdcc8SScott Long MIN(dxfer, sizeof(ccb->csio.sense_data)));
4342f6cdcc8SScott Long } else {
4352f6cdcc8SScott Long memcpy(&ccb->csio.sense_data, &req->sg_list,
4362f6cdcc8SScott Long MIN(dxfer, sizeof(ccb->csio.sense_data)));
4372f6cdcc8SScott Long }
4382f6cdcc8SScott Long ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
4392f6cdcc8SScott Long ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
4402f6cdcc8SScott Long ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
4412f6cdcc8SScott Long break;
4424439f8b4SScott Long default:
4434439f8b4SScott Long ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
4444439f8b4SScott Long break;
4454439f8b4SScott Long }
4464439f8b4SScott Long scsi_done:
4474439f8b4SScott Long if (srb->srb_flag & HPT_SRB_FLAG_HIGH_MEM_ACESS)
4482f6cdcc8SScott Long BUS_SPACE_WRT4_ITL(outbound_queue, index);
4492f6cdcc8SScott Long
4502f6cdcc8SScott Long ccb->csio.resid = ccb->csio.dxfer_len - dxfer;
4514439f8b4SScott Long
4524439f8b4SScott Long hptiop_free_srb(hba, srb);
4534439f8b4SScott Long xpt_done(ccb);
4544439f8b4SScott Long break;
4554439f8b4SScott Long }
4564439f8b4SScott Long }
4574439f8b4SScott Long
hptiop_drain_outbound_queue_itl(struct hpt_iop_hba * hba)4582f6cdcc8SScott Long static void hptiop_drain_outbound_queue_itl(struct hpt_iop_hba *hba)
4592f6cdcc8SScott Long {
4602f6cdcc8SScott Long u_int32_t req, temp;
4612f6cdcc8SScott Long
4622f6cdcc8SScott Long while ((req = BUS_SPACE_RD4_ITL(outbound_queue)) !=IOPMU_QUEUE_EMPTY) {
4632f6cdcc8SScott Long if (req & IOPMU_QUEUE_MASK_HOST_BITS)
4642f6cdcc8SScott Long hptiop_request_callback_itl(hba, req);
4652f6cdcc8SScott Long else {
4662f6cdcc8SScott Long temp = bus_space_read_4(hba->bar0t,
4672f6cdcc8SScott Long hba->bar0h,req +
4682f6cdcc8SScott Long offsetof(struct hpt_iop_request_header,
4692f6cdcc8SScott Long flags));
4702f6cdcc8SScott Long if (temp & IOP_REQUEST_FLAG_SYNC_REQUEST) {
4712f6cdcc8SScott Long u_int64_t temp64;
4722f6cdcc8SScott Long bus_space_read_region_4(hba->bar0t,
4732f6cdcc8SScott Long hba->bar0h,req +
4742f6cdcc8SScott Long offsetof(struct hpt_iop_request_header,
4752f6cdcc8SScott Long context),
4762f6cdcc8SScott Long (u_int32_t *)&temp64, 2);
4772f6cdcc8SScott Long if (temp64) {
4782f6cdcc8SScott Long hptiop_request_callback_itl(hba, req);
4792f6cdcc8SScott Long } else {
4802f6cdcc8SScott Long temp64 = 1;
4812f6cdcc8SScott Long bus_space_write_region_4(hba->bar0t,
4822f6cdcc8SScott Long hba->bar0h,req +
4832f6cdcc8SScott Long offsetof(struct hpt_iop_request_header,
4842f6cdcc8SScott Long context),
4852f6cdcc8SScott Long (u_int32_t *)&temp64, 2);
4862f6cdcc8SScott Long }
4872f6cdcc8SScott Long } else
4882f6cdcc8SScott Long hptiop_request_callback_itl(hba, req);
4892f6cdcc8SScott Long }
4902f6cdcc8SScott Long }
4912f6cdcc8SScott Long }
4922f6cdcc8SScott Long
hptiop_intr_itl(struct hpt_iop_hba * hba)4932f6cdcc8SScott Long static int hptiop_intr_itl(struct hpt_iop_hba * hba)
4942f6cdcc8SScott Long {
4952f6cdcc8SScott Long u_int32_t status;
4962f6cdcc8SScott Long int ret = 0;
4972f6cdcc8SScott Long
4982f6cdcc8SScott Long status = BUS_SPACE_RD4_ITL(outbound_intstatus);
4992f6cdcc8SScott Long
5002f6cdcc8SScott Long if (status & IOPMU_OUTBOUND_INT_MSG0) {
5012f6cdcc8SScott Long u_int32_t msg = BUS_SPACE_RD4_ITL(outbound_msgaddr0);
5022f6cdcc8SScott Long KdPrint(("hptiop: received outbound msg %x\n", msg));
5032f6cdcc8SScott Long BUS_SPACE_WRT4_ITL(outbound_intstatus, IOPMU_OUTBOUND_INT_MSG0);
5042f6cdcc8SScott Long hptiop_os_message_callback(hba, msg);
5052f6cdcc8SScott Long ret = 1;
5062f6cdcc8SScott Long }
5072f6cdcc8SScott Long
5082f6cdcc8SScott Long if (status & IOPMU_OUTBOUND_INT_POSTQUEUE) {
5092f6cdcc8SScott Long hptiop_drain_outbound_queue_itl(hba);
5102f6cdcc8SScott Long ret = 1;
5112f6cdcc8SScott Long }
5122f6cdcc8SScott Long
5132f6cdcc8SScott Long return ret;
5142f6cdcc8SScott Long }
5152f6cdcc8SScott Long
hptiop_request_callback_mv(struct hpt_iop_hba * hba,u_int64_t _tag)5162f6cdcc8SScott Long static void hptiop_request_callback_mv(struct hpt_iop_hba * hba,
5172f6cdcc8SScott Long u_int64_t _tag)
5182f6cdcc8SScott Long {
5192f6cdcc8SScott Long u_int32_t context = (u_int32_t)_tag;
5202f6cdcc8SScott Long
5212f6cdcc8SScott Long if (context & MVIOP_CMD_TYPE_SCSI) {
5222f6cdcc8SScott Long struct hpt_iop_srb *srb;
5232f6cdcc8SScott Long struct hpt_iop_request_scsi_command *req;
5242f6cdcc8SScott Long union ccb *ccb;
5252f6cdcc8SScott Long u_int8_t *cdb;
5262f6cdcc8SScott Long
5272f6cdcc8SScott Long srb = hba->srb[context >> MVIOP_REQUEST_NUMBER_START_BIT];
5282f6cdcc8SScott Long req = (struct hpt_iop_request_scsi_command *)srb;
5292f6cdcc8SScott Long ccb = (union ccb *)srb->ccb;
5302f6cdcc8SScott Long if (ccb->ccb_h.flags & CAM_CDB_POINTER)
5312f6cdcc8SScott Long cdb = ccb->csio.cdb_io.cdb_ptr;
5322f6cdcc8SScott Long else
5332f6cdcc8SScott Long cdb = ccb->csio.cdb_io.cdb_bytes;
5342f6cdcc8SScott Long
5352f6cdcc8SScott Long if (cdb[0] == SYNCHRONIZE_CACHE) { /* ??? */
5362f6cdcc8SScott Long ccb->ccb_h.status = CAM_REQ_CMP;
5372f6cdcc8SScott Long goto scsi_done;
5382f6cdcc8SScott Long }
5392f6cdcc8SScott Long if (context & MVIOP_MU_QUEUE_REQUEST_RESULT_BIT)
5402f6cdcc8SScott Long req->header.result = IOP_RESULT_SUCCESS;
5412f6cdcc8SScott Long
5422f6cdcc8SScott Long switch (req->header.result) {
5432f6cdcc8SScott Long case IOP_RESULT_SUCCESS:
5442f6cdcc8SScott Long switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
5452f6cdcc8SScott Long case CAM_DIR_IN:
5462f6cdcc8SScott Long bus_dmamap_sync(hba->io_dmat,
5472f6cdcc8SScott Long srb->dma_map, BUS_DMASYNC_POSTREAD);
5482f6cdcc8SScott Long bus_dmamap_unload(hba->io_dmat, srb->dma_map);
5492f6cdcc8SScott Long break;
5502f6cdcc8SScott Long case CAM_DIR_OUT:
5512f6cdcc8SScott Long bus_dmamap_sync(hba->io_dmat,
5522f6cdcc8SScott Long srb->dma_map, BUS_DMASYNC_POSTWRITE);
5532f6cdcc8SScott Long bus_dmamap_unload(hba->io_dmat, srb->dma_map);
5542f6cdcc8SScott Long break;
5552f6cdcc8SScott Long }
5562f6cdcc8SScott Long ccb->ccb_h.status = CAM_REQ_CMP;
5572f6cdcc8SScott Long break;
5582f6cdcc8SScott Long case IOP_RESULT_BAD_TARGET:
5592f6cdcc8SScott Long ccb->ccb_h.status = CAM_DEV_NOT_THERE;
5602f6cdcc8SScott Long break;
5612f6cdcc8SScott Long case IOP_RESULT_BUSY:
5622f6cdcc8SScott Long ccb->ccb_h.status = CAM_BUSY;
5632f6cdcc8SScott Long break;
5642f6cdcc8SScott Long case IOP_RESULT_INVALID_REQUEST:
5652f6cdcc8SScott Long ccb->ccb_h.status = CAM_REQ_INVALID;
5662f6cdcc8SScott Long break;
5672f6cdcc8SScott Long case IOP_RESULT_FAIL:
5682f6cdcc8SScott Long ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
5692f6cdcc8SScott Long break;
5702f6cdcc8SScott Long case IOP_RESULT_RESET:
5712f6cdcc8SScott Long ccb->ccb_h.status = CAM_BUSY;
5722f6cdcc8SScott Long break;
5732f6cdcc8SScott Long case IOP_RESULT_CHECK_CONDITION:
5741e5addb7SMarius Strobl memset(&ccb->csio.sense_data, 0,
5751e5addb7SMarius Strobl sizeof(ccb->csio.sense_data));
5761e5addb7SMarius Strobl if (req->dataxfer_length < ccb->csio.sense_len)
5771e5addb7SMarius Strobl ccb->csio.sense_resid = ccb->csio.sense_len -
5781e5addb7SMarius Strobl req->dataxfer_length;
5791e5addb7SMarius Strobl else
5801e5addb7SMarius Strobl ccb->csio.sense_resid = 0;
5812f6cdcc8SScott Long memcpy(&ccb->csio.sense_data, &req->sg_list,
5822f6cdcc8SScott Long MIN(req->dataxfer_length, sizeof(ccb->csio.sense_data)));
5832f6cdcc8SScott Long ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
5842f6cdcc8SScott Long ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
5852f6cdcc8SScott Long ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
5862f6cdcc8SScott Long break;
5872f6cdcc8SScott Long default:
5882f6cdcc8SScott Long ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
5892f6cdcc8SScott Long break;
5902f6cdcc8SScott Long }
5912f6cdcc8SScott Long scsi_done:
5922f6cdcc8SScott Long ccb->csio.resid = ccb->csio.dxfer_len - req->dataxfer_length;
5932f6cdcc8SScott Long
5942f6cdcc8SScott Long hptiop_free_srb(hba, srb);
5952f6cdcc8SScott Long xpt_done(ccb);
5962f6cdcc8SScott Long } else if (context & MVIOP_CMD_TYPE_IOCTL) {
5972f6cdcc8SScott Long struct hpt_iop_request_ioctl_command *req = hba->ctlcfg_ptr;
5982f6cdcc8SScott Long if (context & MVIOP_MU_QUEUE_REQUEST_RESULT_BIT)
5992f6cdcc8SScott Long hba->config_done = 1;
6002f6cdcc8SScott Long else
6012f6cdcc8SScott Long hba->config_done = -1;
6022f6cdcc8SScott Long wakeup(req);
6032f6cdcc8SScott Long } else if (context &
6042f6cdcc8SScott Long (MVIOP_CMD_TYPE_SET_CONFIG |
6052f6cdcc8SScott Long MVIOP_CMD_TYPE_GET_CONFIG))
6062f6cdcc8SScott Long hba->config_done = 1;
6072f6cdcc8SScott Long else {
6082f6cdcc8SScott Long device_printf(hba->pcidev, "wrong callback type\n");
6092f6cdcc8SScott Long }
6102f6cdcc8SScott Long }
6112f6cdcc8SScott Long
hptiop_request_callback_mvfrey(struct hpt_iop_hba * hba,u_int32_t _tag)612022a7377SXin LI static void hptiop_request_callback_mvfrey(struct hpt_iop_hba * hba,
613022a7377SXin LI u_int32_t _tag)
614022a7377SXin LI {
615022a7377SXin LI u_int32_t req_type = _tag & 0xf;
616022a7377SXin LI
617022a7377SXin LI struct hpt_iop_srb *srb;
618022a7377SXin LI struct hpt_iop_request_scsi_command *req;
619022a7377SXin LI union ccb *ccb;
620022a7377SXin LI u_int8_t *cdb;
621022a7377SXin LI
622022a7377SXin LI switch (req_type) {
623022a7377SXin LI case IOP_REQUEST_TYPE_GET_CONFIG:
624022a7377SXin LI case IOP_REQUEST_TYPE_SET_CONFIG:
625022a7377SXin LI hba->config_done = 1;
626022a7377SXin LI break;
627022a7377SXin LI
628022a7377SXin LI case IOP_REQUEST_TYPE_SCSI_COMMAND:
629022a7377SXin LI srb = hba->srb[(_tag >> 4) & 0xff];
630022a7377SXin LI req = (struct hpt_iop_request_scsi_command *)srb;
631022a7377SXin LI
632022a7377SXin LI ccb = (union ccb *)srb->ccb;
633022a7377SXin LI
634ef588050SJohn Baldwin callout_stop(&srb->timeout);
635022a7377SXin LI
636022a7377SXin LI if (ccb->ccb_h.flags & CAM_CDB_POINTER)
637022a7377SXin LI cdb = ccb->csio.cdb_io.cdb_ptr;
638022a7377SXin LI else
639022a7377SXin LI cdb = ccb->csio.cdb_io.cdb_bytes;
640022a7377SXin LI
641022a7377SXin LI if (cdb[0] == SYNCHRONIZE_CACHE) { /* ??? */
642022a7377SXin LI ccb->ccb_h.status = CAM_REQ_CMP;
643022a7377SXin LI goto scsi_done;
644022a7377SXin LI }
645022a7377SXin LI
646022a7377SXin LI if (_tag & MVFREYIOPMU_QUEUE_REQUEST_RESULT_BIT)
647022a7377SXin LI req->header.result = IOP_RESULT_SUCCESS;
648022a7377SXin LI
649022a7377SXin LI switch (req->header.result) {
650022a7377SXin LI case IOP_RESULT_SUCCESS:
651022a7377SXin LI switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
652022a7377SXin LI case CAM_DIR_IN:
653022a7377SXin LI bus_dmamap_sync(hba->io_dmat,
654022a7377SXin LI srb->dma_map, BUS_DMASYNC_POSTREAD);
655022a7377SXin LI bus_dmamap_unload(hba->io_dmat, srb->dma_map);
656022a7377SXin LI break;
657022a7377SXin LI case CAM_DIR_OUT:
658022a7377SXin LI bus_dmamap_sync(hba->io_dmat,
659022a7377SXin LI srb->dma_map, BUS_DMASYNC_POSTWRITE);
660022a7377SXin LI bus_dmamap_unload(hba->io_dmat, srb->dma_map);
661022a7377SXin LI break;
662022a7377SXin LI }
663022a7377SXin LI ccb->ccb_h.status = CAM_REQ_CMP;
664022a7377SXin LI break;
665022a7377SXin LI case IOP_RESULT_BAD_TARGET:
666022a7377SXin LI ccb->ccb_h.status = CAM_DEV_NOT_THERE;
667022a7377SXin LI break;
668022a7377SXin LI case IOP_RESULT_BUSY:
669022a7377SXin LI ccb->ccb_h.status = CAM_BUSY;
670022a7377SXin LI break;
671022a7377SXin LI case IOP_RESULT_INVALID_REQUEST:
672022a7377SXin LI ccb->ccb_h.status = CAM_REQ_INVALID;
673022a7377SXin LI break;
674022a7377SXin LI case IOP_RESULT_FAIL:
675022a7377SXin LI ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
676022a7377SXin LI break;
677022a7377SXin LI case IOP_RESULT_RESET:
678022a7377SXin LI ccb->ccb_h.status = CAM_BUSY;
679022a7377SXin LI break;
680022a7377SXin LI case IOP_RESULT_CHECK_CONDITION:
681022a7377SXin LI memset(&ccb->csio.sense_data, 0,
682022a7377SXin LI sizeof(ccb->csio.sense_data));
683022a7377SXin LI if (req->dataxfer_length < ccb->csio.sense_len)
684022a7377SXin LI ccb->csio.sense_resid = ccb->csio.sense_len -
685022a7377SXin LI req->dataxfer_length;
686022a7377SXin LI else
687022a7377SXin LI ccb->csio.sense_resid = 0;
688022a7377SXin LI memcpy(&ccb->csio.sense_data, &req->sg_list,
689022a7377SXin LI MIN(req->dataxfer_length, sizeof(ccb->csio.sense_data)));
690022a7377SXin LI ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
691022a7377SXin LI ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
692022a7377SXin LI ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
693022a7377SXin LI break;
694022a7377SXin LI default:
695022a7377SXin LI ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
696022a7377SXin LI break;
697022a7377SXin LI }
698022a7377SXin LI scsi_done:
699022a7377SXin LI ccb->csio.resid = ccb->csio.dxfer_len - req->dataxfer_length;
700022a7377SXin LI
701022a7377SXin LI hptiop_free_srb(hba, srb);
702022a7377SXin LI xpt_done(ccb);
703022a7377SXin LI break;
704022a7377SXin LI case IOP_REQUEST_TYPE_IOCTL_COMMAND:
705022a7377SXin LI if (_tag & MVFREYIOPMU_QUEUE_REQUEST_RESULT_BIT)
706022a7377SXin LI hba->config_done = 1;
707022a7377SXin LI else
708022a7377SXin LI hba->config_done = -1;
709022a7377SXin LI wakeup((struct hpt_iop_request_ioctl_command *)hba->ctlcfg_ptr);
710022a7377SXin LI break;
711022a7377SXin LI default:
712022a7377SXin LI device_printf(hba->pcidev, "wrong callback type\n");
713022a7377SXin LI break;
714022a7377SXin LI }
715022a7377SXin LI }
716022a7377SXin LI
hptiop_drain_outbound_queue_mv(struct hpt_iop_hba * hba)7172f6cdcc8SScott Long static void hptiop_drain_outbound_queue_mv(struct hpt_iop_hba * hba)
7182f6cdcc8SScott Long {
7192f6cdcc8SScott Long u_int64_t req;
7202f6cdcc8SScott Long
7212f6cdcc8SScott Long while ((req = hptiop_mv_outbound_read(hba))) {
7222f6cdcc8SScott Long if (req & MVIOP_MU_QUEUE_ADDR_HOST_BIT) {
7232f6cdcc8SScott Long if (req & MVIOP_MU_QUEUE_REQUEST_RETURN_CONTEXT) {
7242f6cdcc8SScott Long hptiop_request_callback_mv(hba, req);
7252f6cdcc8SScott Long }
7262f6cdcc8SScott Long }
7272f6cdcc8SScott Long }
7282f6cdcc8SScott Long }
7292f6cdcc8SScott Long
hptiop_intr_mv(struct hpt_iop_hba * hba)7302f6cdcc8SScott Long static int hptiop_intr_mv(struct hpt_iop_hba * hba)
7312f6cdcc8SScott Long {
7322f6cdcc8SScott Long u_int32_t status;
7332f6cdcc8SScott Long int ret = 0;
7342f6cdcc8SScott Long
7352f6cdcc8SScott Long status = BUS_SPACE_RD4_MV0(outbound_doorbell);
7362f6cdcc8SScott Long
7372f6cdcc8SScott Long if (status)
7382f6cdcc8SScott Long BUS_SPACE_WRT4_MV0(outbound_doorbell, ~status);
7392f6cdcc8SScott Long
7402f6cdcc8SScott Long if (status & MVIOP_MU_OUTBOUND_INT_MSG) {
7412f6cdcc8SScott Long u_int32_t msg = BUS_SPACE_RD4_MV2(outbound_msg);
7422f6cdcc8SScott Long KdPrint(("hptiop: received outbound msg %x\n", msg));
7432f6cdcc8SScott Long hptiop_os_message_callback(hba, msg);
7442f6cdcc8SScott Long ret = 1;
7452f6cdcc8SScott Long }
7462f6cdcc8SScott Long
7472f6cdcc8SScott Long if (status & MVIOP_MU_OUTBOUND_INT_POSTQUEUE) {
7482f6cdcc8SScott Long hptiop_drain_outbound_queue_mv(hba);
7492f6cdcc8SScott Long ret = 1;
7502f6cdcc8SScott Long }
7512f6cdcc8SScott Long
7522f6cdcc8SScott Long return ret;
7532f6cdcc8SScott Long }
7542f6cdcc8SScott Long
hptiop_intr_mvfrey(struct hpt_iop_hba * hba)755022a7377SXin LI static int hptiop_intr_mvfrey(struct hpt_iop_hba * hba)
756022a7377SXin LI {
757022a7377SXin LI u_int32_t status, _tag, cptr;
758022a7377SXin LI int ret = 0;
759022a7377SXin LI
760022a7377SXin LI if (hba->initialized) {
761022a7377SXin LI BUS_SPACE_WRT4_MVFREY2(pcie_f0_int_enable, 0);
762022a7377SXin LI }
763022a7377SXin LI
764022a7377SXin LI status = BUS_SPACE_RD4_MVFREY2(f0_doorbell);
765022a7377SXin LI if (status) {
766022a7377SXin LI BUS_SPACE_WRT4_MVFREY2(f0_doorbell, status);
767022a7377SXin LI if (status & CPU_TO_F0_DRBL_MSG_A_BIT) {
768022a7377SXin LI u_int32_t msg = BUS_SPACE_RD4_MVFREY2(cpu_to_f0_msg_a);
769022a7377SXin LI hptiop_os_message_callback(hba, msg);
770022a7377SXin LI }
771022a7377SXin LI ret = 1;
772022a7377SXin LI }
773022a7377SXin LI
774022a7377SXin LI status = BUS_SPACE_RD4_MVFREY2(isr_cause);
775022a7377SXin LI if (status) {
776022a7377SXin LI BUS_SPACE_WRT4_MVFREY2(isr_cause, status);
777022a7377SXin LI do {
778022a7377SXin LI cptr = *hba->u.mvfrey.outlist_cptr & 0xff;
779022a7377SXin LI while (hba->u.mvfrey.outlist_rptr != cptr) {
780022a7377SXin LI hba->u.mvfrey.outlist_rptr++;
781022a7377SXin LI if (hba->u.mvfrey.outlist_rptr == hba->u.mvfrey.list_count) {
782022a7377SXin LI hba->u.mvfrey.outlist_rptr = 0;
783022a7377SXin LI }
784022a7377SXin LI
785022a7377SXin LI _tag = hba->u.mvfrey.outlist[hba->u.mvfrey.outlist_rptr].val;
786022a7377SXin LI hptiop_request_callback_mvfrey(hba, _tag);
787022a7377SXin LI ret = 2;
788022a7377SXin LI }
789022a7377SXin LI } while (cptr != (*hba->u.mvfrey.outlist_cptr & 0xff));
790022a7377SXin LI }
791022a7377SXin LI
792022a7377SXin LI if (hba->initialized) {
793022a7377SXin LI BUS_SPACE_WRT4_MVFREY2(pcie_f0_int_enable, 0x1010);
794022a7377SXin LI }
795022a7377SXin LI
796022a7377SXin LI return ret;
797022a7377SXin LI }
798022a7377SXin LI
hptiop_send_sync_request_itl(struct hpt_iop_hba * hba,u_int32_t req32,u_int32_t millisec)7992f6cdcc8SScott Long static int hptiop_send_sync_request_itl(struct hpt_iop_hba * hba,
8002f6cdcc8SScott Long u_int32_t req32, u_int32_t millisec)
8012f6cdcc8SScott Long {
8022f6cdcc8SScott Long u_int32_t i;
8032f6cdcc8SScott Long u_int64_t temp64;
8042f6cdcc8SScott Long
8052f6cdcc8SScott Long BUS_SPACE_WRT4_ITL(inbound_queue, req32);
8062f6cdcc8SScott Long BUS_SPACE_RD4_ITL(outbound_intstatus);
8072f6cdcc8SScott Long
8082f6cdcc8SScott Long for (i = 0; i < millisec; i++) {
8092f6cdcc8SScott Long hptiop_intr_itl(hba);
8102f6cdcc8SScott Long bus_space_read_region_4(hba->bar0t, hba->bar0h, req32 +
8112f6cdcc8SScott Long offsetof(struct hpt_iop_request_header, context),
8122f6cdcc8SScott Long (u_int32_t *)&temp64, 2);
8132f6cdcc8SScott Long if (temp64)
8142f6cdcc8SScott Long return 0;
8152f6cdcc8SScott Long DELAY(1000);
8162f6cdcc8SScott Long }
8172f6cdcc8SScott Long
8182f6cdcc8SScott Long return -1;
8192f6cdcc8SScott Long }
8202f6cdcc8SScott Long
hptiop_send_sync_request_mv(struct hpt_iop_hba * hba,void * req,u_int32_t millisec)8212f6cdcc8SScott Long static int hptiop_send_sync_request_mv(struct hpt_iop_hba *hba,
8222f6cdcc8SScott Long void *req, u_int32_t millisec)
8232f6cdcc8SScott Long {
8242f6cdcc8SScott Long u_int32_t i;
8252f6cdcc8SScott Long u_int64_t phy_addr;
8262f6cdcc8SScott Long hba->config_done = 0;
8272f6cdcc8SScott Long
8282f6cdcc8SScott Long phy_addr = hba->ctlcfgcmd_phy |
8292f6cdcc8SScott Long (u_int64_t)MVIOP_MU_QUEUE_ADDR_HOST_BIT;
8302f6cdcc8SScott Long ((struct hpt_iop_request_get_config *)req)->header.flags |=
8312f6cdcc8SScott Long IOP_REQUEST_FLAG_SYNC_REQUEST |
8322f6cdcc8SScott Long IOP_REQUEST_FLAG_OUTPUT_CONTEXT;
8332f6cdcc8SScott Long hptiop_mv_inbound_write(phy_addr, hba);
8342f6cdcc8SScott Long BUS_SPACE_RD4_MV0(outbound_intmask);
8352f6cdcc8SScott Long
8362f6cdcc8SScott Long for (i = 0; i < millisec; i++) {
8372f6cdcc8SScott Long hptiop_intr_mv(hba);
8382f6cdcc8SScott Long if (hba->config_done)
8392f6cdcc8SScott Long return 0;
8402f6cdcc8SScott Long DELAY(1000);
8412f6cdcc8SScott Long }
8422f6cdcc8SScott Long return -1;
8432f6cdcc8SScott Long }
8442f6cdcc8SScott Long
hptiop_send_sync_request_mvfrey(struct hpt_iop_hba * hba,void * req,u_int32_t millisec)845022a7377SXin LI static int hptiop_send_sync_request_mvfrey(struct hpt_iop_hba *hba,
846022a7377SXin LI void *req, u_int32_t millisec)
847022a7377SXin LI {
848022a7377SXin LI u_int32_t i, index;
849022a7377SXin LI u_int64_t phy_addr;
850022a7377SXin LI struct hpt_iop_request_header *reqhdr =
851022a7377SXin LI (struct hpt_iop_request_header *)req;
852022a7377SXin LI
853022a7377SXin LI hba->config_done = 0;
854022a7377SXin LI
855022a7377SXin LI phy_addr = hba->ctlcfgcmd_phy;
856022a7377SXin LI reqhdr->flags = IOP_REQUEST_FLAG_SYNC_REQUEST
857022a7377SXin LI | IOP_REQUEST_FLAG_OUTPUT_CONTEXT
858022a7377SXin LI | IOP_REQUEST_FLAG_ADDR_BITS
859022a7377SXin LI | ((phy_addr >> 16) & 0xffff0000);
860022a7377SXin LI reqhdr->context = ((phy_addr & 0xffffffff) << 32 )
861022a7377SXin LI | IOPMU_QUEUE_ADDR_HOST_BIT | reqhdr->type;
862022a7377SXin LI
863022a7377SXin LI hba->u.mvfrey.inlist_wptr++;
864022a7377SXin LI index = hba->u.mvfrey.inlist_wptr & 0x3fff;
865022a7377SXin LI
866022a7377SXin LI if (index == hba->u.mvfrey.list_count) {
867022a7377SXin LI index = 0;
868022a7377SXin LI hba->u.mvfrey.inlist_wptr &= ~0x3fff;
869022a7377SXin LI hba->u.mvfrey.inlist_wptr ^= CL_POINTER_TOGGLE;
870022a7377SXin LI }
871022a7377SXin LI
872022a7377SXin LI hba->u.mvfrey.inlist[index].addr = phy_addr;
873022a7377SXin LI hba->u.mvfrey.inlist[index].intrfc_len = (reqhdr->size + 3) / 4;
874022a7377SXin LI
875022a7377SXin LI BUS_SPACE_WRT4_MVFREY2(inbound_write_ptr, hba->u.mvfrey.inlist_wptr);
876022a7377SXin LI BUS_SPACE_RD4_MVFREY2(inbound_write_ptr);
877022a7377SXin LI
878022a7377SXin LI for (i = 0; i < millisec; i++) {
879022a7377SXin LI hptiop_intr_mvfrey(hba);
880022a7377SXin LI if (hba->config_done)
881022a7377SXin LI return 0;
882022a7377SXin LI DELAY(1000);
883022a7377SXin LI }
884022a7377SXin LI return -1;
885022a7377SXin LI }
886022a7377SXin LI
hptiop_send_sync_msg(struct hpt_iop_hba * hba,u_int32_t msg,u_int32_t millisec)8872f6cdcc8SScott Long static int hptiop_send_sync_msg(struct hpt_iop_hba *hba,
8882f6cdcc8SScott Long u_int32_t msg, u_int32_t millisec)
8892f6cdcc8SScott Long {
8902f6cdcc8SScott Long u_int32_t i;
8912f6cdcc8SScott Long
8922f6cdcc8SScott Long hba->msg_done = 0;
8932f6cdcc8SScott Long hba->ops->post_msg(hba, msg);
8942f6cdcc8SScott Long
8952f6cdcc8SScott Long for (i=0; i<millisec; i++) {
8962f6cdcc8SScott Long hba->ops->iop_intr(hba);
8972f6cdcc8SScott Long if (hba->msg_done)
8982f6cdcc8SScott Long break;
8992f6cdcc8SScott Long DELAY(1000);
9002f6cdcc8SScott Long }
9012f6cdcc8SScott Long
9022f6cdcc8SScott Long return hba->msg_done? 0 : -1;
9032f6cdcc8SScott Long }
9042f6cdcc8SScott Long
hptiop_get_config_itl(struct hpt_iop_hba * hba,struct hpt_iop_request_get_config * config)9052f6cdcc8SScott Long static int hptiop_get_config_itl(struct hpt_iop_hba * hba,
9062f6cdcc8SScott Long struct hpt_iop_request_get_config * config)
9072f6cdcc8SScott Long {
9082f6cdcc8SScott Long u_int32_t req32;
9092f6cdcc8SScott Long
9102f6cdcc8SScott Long config->header.size = sizeof(struct hpt_iop_request_get_config);
9112f6cdcc8SScott Long config->header.type = IOP_REQUEST_TYPE_GET_CONFIG;
9122f6cdcc8SScott Long config->header.flags = IOP_REQUEST_FLAG_SYNC_REQUEST;
9132f6cdcc8SScott Long config->header.result = IOP_RESULT_PENDING;
9142f6cdcc8SScott Long config->header.context = 0;
9152f6cdcc8SScott Long
9162f6cdcc8SScott Long req32 = BUS_SPACE_RD4_ITL(inbound_queue);
9172f6cdcc8SScott Long if (req32 == IOPMU_QUEUE_EMPTY)
9182f6cdcc8SScott Long return -1;
9192f6cdcc8SScott Long
9202f6cdcc8SScott Long bus_space_write_region_4(hba->bar0t, hba->bar0h,
9212f6cdcc8SScott Long req32, (u_int32_t *)config,
9222f6cdcc8SScott Long sizeof(struct hpt_iop_request_header) >> 2);
9232f6cdcc8SScott Long
9242f6cdcc8SScott Long if (hptiop_send_sync_request_itl(hba, req32, 20000)) {
9252f6cdcc8SScott Long KdPrint(("hptiop: get config send cmd failed"));
9262f6cdcc8SScott Long return -1;
9272f6cdcc8SScott Long }
9282f6cdcc8SScott Long
9292f6cdcc8SScott Long bus_space_read_region_4(hba->bar0t, hba->bar0h,
9302f6cdcc8SScott Long req32, (u_int32_t *)config,
9312f6cdcc8SScott Long sizeof(struct hpt_iop_request_get_config) >> 2);
9322f6cdcc8SScott Long
9332f6cdcc8SScott Long BUS_SPACE_WRT4_ITL(outbound_queue, req32);
9342f6cdcc8SScott Long
9352f6cdcc8SScott Long return 0;
9362f6cdcc8SScott Long }
9372f6cdcc8SScott Long
hptiop_get_config_mv(struct hpt_iop_hba * hba,struct hpt_iop_request_get_config * config)9382f6cdcc8SScott Long static int hptiop_get_config_mv(struct hpt_iop_hba * hba,
9392f6cdcc8SScott Long struct hpt_iop_request_get_config * config)
9402f6cdcc8SScott Long {
9412f6cdcc8SScott Long struct hpt_iop_request_get_config *req;
9422f6cdcc8SScott Long
9432f6cdcc8SScott Long if (!(req = hba->ctlcfg_ptr))
9442f6cdcc8SScott Long return -1;
9452f6cdcc8SScott Long
9462f6cdcc8SScott Long req->header.flags = 0;
9472f6cdcc8SScott Long req->header.type = IOP_REQUEST_TYPE_GET_CONFIG;
9482f6cdcc8SScott Long req->header.size = sizeof(struct hpt_iop_request_get_config);
9492f6cdcc8SScott Long req->header.result = IOP_RESULT_PENDING;
9502f6cdcc8SScott Long req->header.context = MVIOP_CMD_TYPE_GET_CONFIG;
9512f6cdcc8SScott Long
9522f6cdcc8SScott Long if (hptiop_send_sync_request_mv(hba, req, 20000)) {
9532f6cdcc8SScott Long KdPrint(("hptiop: get config send cmd failed"));
9542f6cdcc8SScott Long return -1;
9552f6cdcc8SScott Long }
9562f6cdcc8SScott Long
9572f6cdcc8SScott Long *config = *req;
9582f6cdcc8SScott Long return 0;
9592f6cdcc8SScott Long }
9602f6cdcc8SScott Long
hptiop_get_config_mvfrey(struct hpt_iop_hba * hba,struct hpt_iop_request_get_config * config)961022a7377SXin LI static int hptiop_get_config_mvfrey(struct hpt_iop_hba * hba,
962022a7377SXin LI struct hpt_iop_request_get_config * config)
963022a7377SXin LI {
964022a7377SXin LI struct hpt_iop_request_get_config *info = hba->u.mvfrey.config;
965022a7377SXin LI
966022a7377SXin LI if (info->header.size != sizeof(struct hpt_iop_request_get_config) ||
967022a7377SXin LI info->header.type != IOP_REQUEST_TYPE_GET_CONFIG) {
968022a7377SXin LI KdPrint(("hptiop: header size %x/%x type %x/%x",
969022a7377SXin LI info->header.size, (int)sizeof(struct hpt_iop_request_get_config),
970022a7377SXin LI info->header.type, IOP_REQUEST_TYPE_GET_CONFIG));
971022a7377SXin LI return -1;
972022a7377SXin LI }
973022a7377SXin LI
974022a7377SXin LI config->interface_version = info->interface_version;
975022a7377SXin LI config->firmware_version = info->firmware_version;
976022a7377SXin LI config->max_requests = info->max_requests;
977022a7377SXin LI config->request_size = info->request_size;
978022a7377SXin LI config->max_sg_count = info->max_sg_count;
979022a7377SXin LI config->data_transfer_length = info->data_transfer_length;
980022a7377SXin LI config->alignment_mask = info->alignment_mask;
981022a7377SXin LI config->max_devices = info->max_devices;
982022a7377SXin LI config->sdram_size = info->sdram_size;
983022a7377SXin LI
984022a7377SXin LI KdPrint(("hptiop: maxreq %x reqsz %x datalen %x maxdev %x sdram %x",
985022a7377SXin LI config->max_requests, config->request_size,
986022a7377SXin LI config->data_transfer_length, config->max_devices,
987022a7377SXin LI config->sdram_size));
988022a7377SXin LI
989022a7377SXin LI return 0;
990022a7377SXin LI }
991022a7377SXin LI
hptiop_set_config_itl(struct hpt_iop_hba * hba,struct hpt_iop_request_set_config * config)9922f6cdcc8SScott Long static int hptiop_set_config_itl(struct hpt_iop_hba *hba,
9932f6cdcc8SScott Long struct hpt_iop_request_set_config *config)
9942f6cdcc8SScott Long {
9952f6cdcc8SScott Long u_int32_t req32;
9962f6cdcc8SScott Long
9972f6cdcc8SScott Long req32 = BUS_SPACE_RD4_ITL(inbound_queue);
9982f6cdcc8SScott Long
9992f6cdcc8SScott Long if (req32 == IOPMU_QUEUE_EMPTY)
10002f6cdcc8SScott Long return -1;
10012f6cdcc8SScott Long
10022f6cdcc8SScott Long config->header.size = sizeof(struct hpt_iop_request_set_config);
10032f6cdcc8SScott Long config->header.type = IOP_REQUEST_TYPE_SET_CONFIG;
10042f6cdcc8SScott Long config->header.flags = IOP_REQUEST_FLAG_SYNC_REQUEST;
10052f6cdcc8SScott Long config->header.result = IOP_RESULT_PENDING;
10062f6cdcc8SScott Long config->header.context = 0;
10072f6cdcc8SScott Long
10082f6cdcc8SScott Long bus_space_write_region_4(hba->bar0t, hba->bar0h, req32,
10092f6cdcc8SScott Long (u_int32_t *)config,
10102f6cdcc8SScott Long sizeof(struct hpt_iop_request_set_config) >> 2);
10112f6cdcc8SScott Long
10122f6cdcc8SScott Long if (hptiop_send_sync_request_itl(hba, req32, 20000)) {
10132f6cdcc8SScott Long KdPrint(("hptiop: set config send cmd failed"));
10142f6cdcc8SScott Long return -1;
10152f6cdcc8SScott Long }
10162f6cdcc8SScott Long
10172f6cdcc8SScott Long BUS_SPACE_WRT4_ITL(outbound_queue, req32);
10182f6cdcc8SScott Long
10192f6cdcc8SScott Long return 0;
10202f6cdcc8SScott Long }
10212f6cdcc8SScott Long
hptiop_set_config_mv(struct hpt_iop_hba * hba,struct hpt_iop_request_set_config * config)10222f6cdcc8SScott Long static int hptiop_set_config_mv(struct hpt_iop_hba *hba,
10232f6cdcc8SScott Long struct hpt_iop_request_set_config *config)
10242f6cdcc8SScott Long {
10252f6cdcc8SScott Long struct hpt_iop_request_set_config *req;
10262f6cdcc8SScott Long
10272f6cdcc8SScott Long if (!(req = hba->ctlcfg_ptr))
10282f6cdcc8SScott Long return -1;
10292f6cdcc8SScott Long
10302f6cdcc8SScott Long memcpy((u_int8_t *)req + sizeof(struct hpt_iop_request_header),
10312f6cdcc8SScott Long (u_int8_t *)config + sizeof(struct hpt_iop_request_header),
10322f6cdcc8SScott Long sizeof(struct hpt_iop_request_set_config) -
10332f6cdcc8SScott Long sizeof(struct hpt_iop_request_header));
10342f6cdcc8SScott Long
10352f6cdcc8SScott Long req->header.flags = 0;
10362f6cdcc8SScott Long req->header.type = IOP_REQUEST_TYPE_SET_CONFIG;
10372f6cdcc8SScott Long req->header.size = sizeof(struct hpt_iop_request_set_config);
10382f6cdcc8SScott Long req->header.result = IOP_RESULT_PENDING;
10392f6cdcc8SScott Long req->header.context = MVIOP_CMD_TYPE_SET_CONFIG;
10402f6cdcc8SScott Long
10412f6cdcc8SScott Long if (hptiop_send_sync_request_mv(hba, req, 20000)) {
10422f6cdcc8SScott Long KdPrint(("hptiop: set config send cmd failed"));
10432f6cdcc8SScott Long return -1;
10442f6cdcc8SScott Long }
10452f6cdcc8SScott Long
10462f6cdcc8SScott Long return 0;
10472f6cdcc8SScott Long }
10482f6cdcc8SScott Long
hptiop_set_config_mvfrey(struct hpt_iop_hba * hba,struct hpt_iop_request_set_config * config)1049022a7377SXin LI static int hptiop_set_config_mvfrey(struct hpt_iop_hba *hba,
1050022a7377SXin LI struct hpt_iop_request_set_config *config)
1051022a7377SXin LI {
1052022a7377SXin LI struct hpt_iop_request_set_config *req;
1053022a7377SXin LI
1054022a7377SXin LI if (!(req = hba->ctlcfg_ptr))
1055022a7377SXin LI return -1;
1056022a7377SXin LI
1057022a7377SXin LI memcpy((u_int8_t *)req + sizeof(struct hpt_iop_request_header),
1058022a7377SXin LI (u_int8_t *)config + sizeof(struct hpt_iop_request_header),
1059022a7377SXin LI sizeof(struct hpt_iop_request_set_config) -
1060022a7377SXin LI sizeof(struct hpt_iop_request_header));
1061022a7377SXin LI
1062022a7377SXin LI req->header.type = IOP_REQUEST_TYPE_SET_CONFIG;
1063022a7377SXin LI req->header.size = sizeof(struct hpt_iop_request_set_config);
1064022a7377SXin LI req->header.result = IOP_RESULT_PENDING;
1065022a7377SXin LI
1066022a7377SXin LI if (hptiop_send_sync_request_mvfrey(hba, req, 20000)) {
1067022a7377SXin LI KdPrint(("hptiop: set config send cmd failed"));
1068022a7377SXin LI return -1;
1069022a7377SXin LI }
1070022a7377SXin LI
1071022a7377SXin LI return 0;
1072022a7377SXin LI }
1073022a7377SXin LI
hptiop_post_ioctl_command_itl(struct hpt_iop_hba * hba,u_int32_t req32,struct hpt_iop_ioctl_param * pParams)10742f6cdcc8SScott Long static int hptiop_post_ioctl_command_itl(struct hpt_iop_hba *hba,
10752f6cdcc8SScott Long u_int32_t req32,
10762f6cdcc8SScott Long struct hpt_iop_ioctl_param *pParams)
10772f6cdcc8SScott Long {
10782f6cdcc8SScott Long u_int64_t temp64;
10792f6cdcc8SScott Long struct hpt_iop_request_ioctl_command req;
10802f6cdcc8SScott Long
10812f6cdcc8SScott Long if ((((pParams->nInBufferSize + 3) & ~3) + pParams->nOutBufferSize) >
10822f6cdcc8SScott Long (hba->max_request_size -
10832f6cdcc8SScott Long offsetof(struct hpt_iop_request_ioctl_command, buf))) {
10842f6cdcc8SScott Long device_printf(hba->pcidev, "request size beyond max value");
10852f6cdcc8SScott Long return -1;
10862f6cdcc8SScott Long }
10872f6cdcc8SScott Long
10882f6cdcc8SScott Long req.header.size = offsetof(struct hpt_iop_request_ioctl_command, buf)
10892f6cdcc8SScott Long + pParams->nInBufferSize;
10902f6cdcc8SScott Long req.header.type = IOP_REQUEST_TYPE_IOCTL_COMMAND;
10912f6cdcc8SScott Long req.header.flags = IOP_REQUEST_FLAG_SYNC_REQUEST;
10922f6cdcc8SScott Long req.header.result = IOP_RESULT_PENDING;
10932f6cdcc8SScott Long req.header.context = req32 + (u_int64_t)(unsigned long)hba->u.itl.mu;
10942f6cdcc8SScott Long req.ioctl_code = HPT_CTL_CODE_BSD_TO_IOP(pParams->dwIoControlCode);
10952f6cdcc8SScott Long req.inbuf_size = pParams->nInBufferSize;
10962f6cdcc8SScott Long req.outbuf_size = pParams->nOutBufferSize;
10972f6cdcc8SScott Long req.bytes_returned = 0;
10982f6cdcc8SScott Long
10992f6cdcc8SScott Long bus_space_write_region_4(hba->bar0t, hba->bar0h, req32, (u_int32_t *)&req,
11002f6cdcc8SScott Long offsetof(struct hpt_iop_request_ioctl_command, buf)>>2);
11012f6cdcc8SScott Long
11022f6cdcc8SScott Long hptiop_lock_adapter(hba);
11032f6cdcc8SScott Long
11042f6cdcc8SScott Long BUS_SPACE_WRT4_ITL(inbound_queue, req32);
11052f6cdcc8SScott Long BUS_SPACE_RD4_ITL(outbound_intstatus);
11062f6cdcc8SScott Long
11072f6cdcc8SScott Long bus_space_read_region_4(hba->bar0t, hba->bar0h, req32 +
11082f6cdcc8SScott Long offsetof(struct hpt_iop_request_ioctl_command, header.context),
11092f6cdcc8SScott Long (u_int32_t *)&temp64, 2);
11102f6cdcc8SScott Long while (temp64) {
11112f6cdcc8SScott Long if (hptiop_sleep(hba, (void *)((unsigned long)hba->u.itl.mu + req32),
11122f6cdcc8SScott Long PPAUSE, "hptctl", HPT_OSM_TIMEOUT)==0)
11132f6cdcc8SScott Long break;
11142f6cdcc8SScott Long hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET, 60000);
11152f6cdcc8SScott Long bus_space_read_region_4(hba->bar0t, hba->bar0h,req32 +
11162f6cdcc8SScott Long offsetof(struct hpt_iop_request_ioctl_command,
11172f6cdcc8SScott Long header.context),
11182f6cdcc8SScott Long (u_int32_t *)&temp64, 2);
11192f6cdcc8SScott Long }
11202f6cdcc8SScott Long
11212f6cdcc8SScott Long hptiop_unlock_adapter(hba);
11222f6cdcc8SScott Long return 0;
11232f6cdcc8SScott Long }
11242f6cdcc8SScott Long
hptiop_bus_space_copyin(struct hpt_iop_hba * hba,u_int32_t bus,void * user,int size)1125022a7377SXin LI static int hptiop_bus_space_copyin(struct hpt_iop_hba *hba, u_int32_t bus,
1126022a7377SXin LI void *user, int size)
11272f6cdcc8SScott Long {
11282f6cdcc8SScott Long unsigned char byte;
11292f6cdcc8SScott Long int i;
11302f6cdcc8SScott Long
11312f6cdcc8SScott Long for (i=0; i<size; i++) {
11322f6cdcc8SScott Long if (copyin((u_int8_t *)user + i, &byte, 1))
11332f6cdcc8SScott Long return -1;
11342f6cdcc8SScott Long bus_space_write_1(hba->bar0t, hba->bar0h, bus + i, byte);
11352f6cdcc8SScott Long }
11362f6cdcc8SScott Long
11372f6cdcc8SScott Long return 0;
11382f6cdcc8SScott Long }
11392f6cdcc8SScott Long
hptiop_bus_space_copyout(struct hpt_iop_hba * hba,u_int32_t bus,void * user,int size)1140022a7377SXin LI static int hptiop_bus_space_copyout(struct hpt_iop_hba *hba, u_int32_t bus,
1141022a7377SXin LI void *user, int size)
11422f6cdcc8SScott Long {
11432f6cdcc8SScott Long unsigned char byte;
11442f6cdcc8SScott Long int i;
11452f6cdcc8SScott Long
11462f6cdcc8SScott Long for (i=0; i<size; i++) {
11472f6cdcc8SScott Long byte = bus_space_read_1(hba->bar0t, hba->bar0h, bus + i);
11482f6cdcc8SScott Long if (copyout(&byte, (u_int8_t *)user + i, 1))
11492f6cdcc8SScott Long return -1;
11502f6cdcc8SScott Long }
11512f6cdcc8SScott Long
11522f6cdcc8SScott Long return 0;
11532f6cdcc8SScott Long }
11542f6cdcc8SScott Long
hptiop_do_ioctl_itl(struct hpt_iop_hba * hba,struct hpt_iop_ioctl_param * pParams)11552f6cdcc8SScott Long static int hptiop_do_ioctl_itl(struct hpt_iop_hba *hba,
11562f6cdcc8SScott Long struct hpt_iop_ioctl_param * pParams)
11572f6cdcc8SScott Long {
11582f6cdcc8SScott Long u_int32_t req32;
11592f6cdcc8SScott Long u_int32_t result;
11602f6cdcc8SScott Long
11612f6cdcc8SScott Long if ((pParams->Magic != HPT_IOCTL_MAGIC) &&
11622f6cdcc8SScott Long (pParams->Magic != HPT_IOCTL_MAGIC32))
11632f6cdcc8SScott Long return EFAULT;
11642f6cdcc8SScott Long
11652f6cdcc8SScott Long req32 = BUS_SPACE_RD4_ITL(inbound_queue);
11662f6cdcc8SScott Long if (req32 == IOPMU_QUEUE_EMPTY)
11672f6cdcc8SScott Long return EFAULT;
11682f6cdcc8SScott Long
11692f6cdcc8SScott Long if (pParams->nInBufferSize)
11702f6cdcc8SScott Long if (hptiop_bus_space_copyin(hba, req32 +
11712f6cdcc8SScott Long offsetof(struct hpt_iop_request_ioctl_command, buf),
11722f6cdcc8SScott Long (void *)pParams->lpInBuffer, pParams->nInBufferSize))
11732f6cdcc8SScott Long goto invalid;
11742f6cdcc8SScott Long
11752f6cdcc8SScott Long if (hptiop_post_ioctl_command_itl(hba, req32, pParams))
11762f6cdcc8SScott Long goto invalid;
11772f6cdcc8SScott Long
11782f6cdcc8SScott Long result = bus_space_read_4(hba->bar0t, hba->bar0h, req32 +
11792f6cdcc8SScott Long offsetof(struct hpt_iop_request_ioctl_command,
11802f6cdcc8SScott Long header.result));
11812f6cdcc8SScott Long
11822f6cdcc8SScott Long if (result == IOP_RESULT_SUCCESS) {
11832f6cdcc8SScott Long if (pParams->nOutBufferSize)
11842f6cdcc8SScott Long if (hptiop_bus_space_copyout(hba, req32 +
11852f6cdcc8SScott Long offsetof(struct hpt_iop_request_ioctl_command, buf) +
11862f6cdcc8SScott Long ((pParams->nInBufferSize + 3) & ~3),
11872f6cdcc8SScott Long (void *)pParams->lpOutBuffer, pParams->nOutBufferSize))
11882f6cdcc8SScott Long goto invalid;
11892f6cdcc8SScott Long
11902f6cdcc8SScott Long if (pParams->lpBytesReturned) {
11912f6cdcc8SScott Long if (hptiop_bus_space_copyout(hba, req32 +
11922f6cdcc8SScott Long offsetof(struct hpt_iop_request_ioctl_command, bytes_returned),
11932f6cdcc8SScott Long (void *)pParams->lpBytesReturned, sizeof(unsigned long)))
11942f6cdcc8SScott Long goto invalid;
11952f6cdcc8SScott Long }
11962f6cdcc8SScott Long
11972f6cdcc8SScott Long BUS_SPACE_WRT4_ITL(outbound_queue, req32);
11982f6cdcc8SScott Long
11992f6cdcc8SScott Long return 0;
12002f6cdcc8SScott Long } else{
12012f6cdcc8SScott Long invalid:
12022f6cdcc8SScott Long BUS_SPACE_WRT4_ITL(outbound_queue, req32);
12032f6cdcc8SScott Long
12042f6cdcc8SScott Long return EFAULT;
12052f6cdcc8SScott Long }
12062f6cdcc8SScott Long }
12072f6cdcc8SScott Long
hptiop_post_ioctl_command_mv(struct hpt_iop_hba * hba,struct hpt_iop_request_ioctl_command * req,struct hpt_iop_ioctl_param * pParams)12082f6cdcc8SScott Long static int hptiop_post_ioctl_command_mv(struct hpt_iop_hba *hba,
12092f6cdcc8SScott Long struct hpt_iop_request_ioctl_command *req,
12102f6cdcc8SScott Long struct hpt_iop_ioctl_param *pParams)
12112f6cdcc8SScott Long {
12122f6cdcc8SScott Long u_int64_t req_phy;
12132f6cdcc8SScott Long int size = 0;
12142f6cdcc8SScott Long
12152f6cdcc8SScott Long if ((((pParams->nInBufferSize + 3) & ~3) + pParams->nOutBufferSize) >
12162f6cdcc8SScott Long (hba->max_request_size -
12172f6cdcc8SScott Long offsetof(struct hpt_iop_request_ioctl_command, buf))) {
12182f6cdcc8SScott Long device_printf(hba->pcidev, "request size beyond max value");
12192f6cdcc8SScott Long return -1;
12202f6cdcc8SScott Long }
12212f6cdcc8SScott Long
12222f6cdcc8SScott Long req->ioctl_code = HPT_CTL_CODE_BSD_TO_IOP(pParams->dwIoControlCode);
12232f6cdcc8SScott Long req->inbuf_size = pParams->nInBufferSize;
12242f6cdcc8SScott Long req->outbuf_size = pParams->nOutBufferSize;
12252f6cdcc8SScott Long req->header.size = offsetof(struct hpt_iop_request_ioctl_command, buf)
12262f6cdcc8SScott Long + pParams->nInBufferSize;
12272f6cdcc8SScott Long req->header.context = (u_int64_t)MVIOP_CMD_TYPE_IOCTL;
12282f6cdcc8SScott Long req->header.type = IOP_REQUEST_TYPE_IOCTL_COMMAND;
12292f6cdcc8SScott Long req->header.result = IOP_RESULT_PENDING;
12302f6cdcc8SScott Long req->header.flags = IOP_REQUEST_FLAG_OUTPUT_CONTEXT;
12312f6cdcc8SScott Long size = req->header.size >> 8;
1232b3956646SMarcelo Araujo size = imin(3, size);
12332f6cdcc8SScott Long req_phy = hba->ctlcfgcmd_phy | MVIOP_MU_QUEUE_ADDR_HOST_BIT | size;
12342f6cdcc8SScott Long hptiop_mv_inbound_write(req_phy, hba);
12352f6cdcc8SScott Long
12362f6cdcc8SScott Long BUS_SPACE_RD4_MV0(outbound_intmask);
12372f6cdcc8SScott Long
12382f6cdcc8SScott Long while (hba->config_done == 0) {
12392f6cdcc8SScott Long if (hptiop_sleep(hba, req, PPAUSE,
12402f6cdcc8SScott Long "hptctl", HPT_OSM_TIMEOUT)==0)
12412f6cdcc8SScott Long continue;
12422f6cdcc8SScott Long hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET, 60000);
12432f6cdcc8SScott Long }
12442f6cdcc8SScott Long return 0;
12452f6cdcc8SScott Long }
12462f6cdcc8SScott Long
hptiop_do_ioctl_mv(struct hpt_iop_hba * hba,struct hpt_iop_ioctl_param * pParams)12472f6cdcc8SScott Long static int hptiop_do_ioctl_mv(struct hpt_iop_hba *hba,
12482f6cdcc8SScott Long struct hpt_iop_ioctl_param *pParams)
12492f6cdcc8SScott Long {
12502f6cdcc8SScott Long struct hpt_iop_request_ioctl_command *req;
12512f6cdcc8SScott Long
12522f6cdcc8SScott Long if ((pParams->Magic != HPT_IOCTL_MAGIC) &&
12532f6cdcc8SScott Long (pParams->Magic != HPT_IOCTL_MAGIC32))
12542f6cdcc8SScott Long return EFAULT;
12552f6cdcc8SScott Long
12562f6cdcc8SScott Long req = (struct hpt_iop_request_ioctl_command *)(hba->ctlcfg_ptr);
12572f6cdcc8SScott Long hba->config_done = 0;
12582f6cdcc8SScott Long hptiop_lock_adapter(hba);
12592f6cdcc8SScott Long if (pParams->nInBufferSize)
12602f6cdcc8SScott Long if (copyin((void *)pParams->lpInBuffer,
12612f6cdcc8SScott Long req->buf, pParams->nInBufferSize))
12622f6cdcc8SScott Long goto invalid;
12632f6cdcc8SScott Long if (hptiop_post_ioctl_command_mv(hba, req, pParams))
12642f6cdcc8SScott Long goto invalid;
12652f6cdcc8SScott Long
12662f6cdcc8SScott Long if (hba->config_done == 1) {
12672f6cdcc8SScott Long if (pParams->nOutBufferSize)
12682f6cdcc8SScott Long if (copyout(req->buf +
12692f6cdcc8SScott Long ((pParams->nInBufferSize + 3) & ~3),
12702f6cdcc8SScott Long (void *)pParams->lpOutBuffer,
12712f6cdcc8SScott Long pParams->nOutBufferSize))
12722f6cdcc8SScott Long goto invalid;
12732f6cdcc8SScott Long
12742f6cdcc8SScott Long if (pParams->lpBytesReturned)
12752f6cdcc8SScott Long if (copyout(&req->bytes_returned,
12762f6cdcc8SScott Long (void*)pParams->lpBytesReturned,
12772f6cdcc8SScott Long sizeof(u_int32_t)))
12782f6cdcc8SScott Long goto invalid;
12792f6cdcc8SScott Long hptiop_unlock_adapter(hba);
12802f6cdcc8SScott Long return 0;
12812f6cdcc8SScott Long } else{
12822f6cdcc8SScott Long invalid:
12832f6cdcc8SScott Long hptiop_unlock_adapter(hba);
12842f6cdcc8SScott Long return EFAULT;
12852f6cdcc8SScott Long }
12862f6cdcc8SScott Long }
12872f6cdcc8SScott Long
hptiop_post_ioctl_command_mvfrey(struct hpt_iop_hba * hba,struct hpt_iop_request_ioctl_command * req,struct hpt_iop_ioctl_param * pParams)1288022a7377SXin LI static int hptiop_post_ioctl_command_mvfrey(struct hpt_iop_hba *hba,
1289022a7377SXin LI struct hpt_iop_request_ioctl_command *req,
1290022a7377SXin LI struct hpt_iop_ioctl_param *pParams)
1291022a7377SXin LI {
1292022a7377SXin LI u_int64_t phy_addr;
1293022a7377SXin LI u_int32_t index;
1294022a7377SXin LI
1295022a7377SXin LI phy_addr = hba->ctlcfgcmd_phy;
1296022a7377SXin LI
1297022a7377SXin LI if ((((pParams->nInBufferSize + 3) & ~3) + pParams->nOutBufferSize) >
1298022a7377SXin LI (hba->max_request_size -
1299022a7377SXin LI offsetof(struct hpt_iop_request_ioctl_command, buf))) {
1300022a7377SXin LI device_printf(hba->pcidev, "request size beyond max value");
1301022a7377SXin LI return -1;
1302022a7377SXin LI }
1303022a7377SXin LI
1304022a7377SXin LI req->ioctl_code = HPT_CTL_CODE_BSD_TO_IOP(pParams->dwIoControlCode);
1305022a7377SXin LI req->inbuf_size = pParams->nInBufferSize;
1306022a7377SXin LI req->outbuf_size = pParams->nOutBufferSize;
1307022a7377SXin LI req->header.size = offsetof(struct hpt_iop_request_ioctl_command, buf)
1308022a7377SXin LI + pParams->nInBufferSize;
1309022a7377SXin LI
1310022a7377SXin LI req->header.type = IOP_REQUEST_TYPE_IOCTL_COMMAND;
1311022a7377SXin LI req->header.result = IOP_RESULT_PENDING;
1312022a7377SXin LI
1313022a7377SXin LI req->header.flags = IOP_REQUEST_FLAG_SYNC_REQUEST
1314022a7377SXin LI | IOP_REQUEST_FLAG_OUTPUT_CONTEXT
1315022a7377SXin LI | IOP_REQUEST_FLAG_ADDR_BITS
1316022a7377SXin LI | ((phy_addr >> 16) & 0xffff0000);
1317022a7377SXin LI req->header.context = ((phy_addr & 0xffffffff) << 32 )
1318022a7377SXin LI | IOPMU_QUEUE_ADDR_HOST_BIT | req->header.type;
1319022a7377SXin LI
1320022a7377SXin LI hba->u.mvfrey.inlist_wptr++;
1321022a7377SXin LI index = hba->u.mvfrey.inlist_wptr & 0x3fff;
1322022a7377SXin LI
1323022a7377SXin LI if (index == hba->u.mvfrey.list_count) {
1324022a7377SXin LI index = 0;
1325022a7377SXin LI hba->u.mvfrey.inlist_wptr &= ~0x3fff;
1326022a7377SXin LI hba->u.mvfrey.inlist_wptr ^= CL_POINTER_TOGGLE;
1327022a7377SXin LI }
1328022a7377SXin LI
1329022a7377SXin LI hba->u.mvfrey.inlist[index].addr = phy_addr;
1330022a7377SXin LI hba->u.mvfrey.inlist[index].intrfc_len = (req->header.size + 3) / 4;
1331022a7377SXin LI
1332022a7377SXin LI BUS_SPACE_WRT4_MVFREY2(inbound_write_ptr, hba->u.mvfrey.inlist_wptr);
1333022a7377SXin LI BUS_SPACE_RD4_MVFREY2(inbound_write_ptr);
1334022a7377SXin LI
1335022a7377SXin LI while (hba->config_done == 0) {
1336022a7377SXin LI if (hptiop_sleep(hba, req, PPAUSE,
1337022a7377SXin LI "hptctl", HPT_OSM_TIMEOUT)==0)
1338022a7377SXin LI continue;
1339022a7377SXin LI hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET, 60000);
1340022a7377SXin LI }
1341022a7377SXin LI return 0;
1342022a7377SXin LI }
1343022a7377SXin LI
hptiop_do_ioctl_mvfrey(struct hpt_iop_hba * hba,struct hpt_iop_ioctl_param * pParams)1344022a7377SXin LI static int hptiop_do_ioctl_mvfrey(struct hpt_iop_hba *hba,
1345022a7377SXin LI struct hpt_iop_ioctl_param *pParams)
1346022a7377SXin LI {
1347022a7377SXin LI struct hpt_iop_request_ioctl_command *req;
1348022a7377SXin LI
1349022a7377SXin LI if ((pParams->Magic != HPT_IOCTL_MAGIC) &&
1350022a7377SXin LI (pParams->Magic != HPT_IOCTL_MAGIC32))
1351022a7377SXin LI return EFAULT;
1352022a7377SXin LI
1353022a7377SXin LI req = (struct hpt_iop_request_ioctl_command *)(hba->ctlcfg_ptr);
1354022a7377SXin LI hba->config_done = 0;
1355022a7377SXin LI hptiop_lock_adapter(hba);
1356022a7377SXin LI if (pParams->nInBufferSize)
1357022a7377SXin LI if (copyin((void *)pParams->lpInBuffer,
1358022a7377SXin LI req->buf, pParams->nInBufferSize))
1359022a7377SXin LI goto invalid;
1360022a7377SXin LI if (hptiop_post_ioctl_command_mvfrey(hba, req, pParams))
1361022a7377SXin LI goto invalid;
1362022a7377SXin LI
1363022a7377SXin LI if (hba->config_done == 1) {
1364022a7377SXin LI if (pParams->nOutBufferSize)
1365022a7377SXin LI if (copyout(req->buf +
1366022a7377SXin LI ((pParams->nInBufferSize + 3) & ~3),
1367022a7377SXin LI (void *)pParams->lpOutBuffer,
1368022a7377SXin LI pParams->nOutBufferSize))
1369022a7377SXin LI goto invalid;
1370022a7377SXin LI
1371022a7377SXin LI if (pParams->lpBytesReturned)
1372022a7377SXin LI if (copyout(&req->bytes_returned,
1373022a7377SXin LI (void*)pParams->lpBytesReturned,
1374022a7377SXin LI sizeof(u_int32_t)))
1375022a7377SXin LI goto invalid;
1376022a7377SXin LI hptiop_unlock_adapter(hba);
1377022a7377SXin LI return 0;
1378022a7377SXin LI } else{
1379022a7377SXin LI invalid:
1380022a7377SXin LI hptiop_unlock_adapter(hba);
1381022a7377SXin LI return EFAULT;
1382022a7377SXin LI }
1383022a7377SXin LI }
1384022a7377SXin LI
hptiop_rescan_bus(struct hpt_iop_hba * hba)13852f6cdcc8SScott Long static int hptiop_rescan_bus(struct hpt_iop_hba * hba)
13862f6cdcc8SScott Long {
13872f6cdcc8SScott Long union ccb *ccb;
13882f6cdcc8SScott Long
138983c5d981SAlexander Motin if ((ccb = xpt_alloc_ccb()) == NULL)
139083c5d981SAlexander Motin return(ENOMEM);
1391e5dfa058SAlexander Motin if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(hba->sim),
139283c5d981SAlexander Motin CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
139383c5d981SAlexander Motin xpt_free_ccb(ccb);
139483c5d981SAlexander Motin return(EIO);
139583c5d981SAlexander Motin }
139683c5d981SAlexander Motin xpt_rescan(ccb);
139783c5d981SAlexander Motin return(0);
13982f6cdcc8SScott Long }
13992f6cdcc8SScott Long
14002f6cdcc8SScott Long static bus_dmamap_callback_t hptiop_map_srb;
14012f6cdcc8SScott Long static bus_dmamap_callback_t hptiop_post_scsi_command;
14022f6cdcc8SScott Long static bus_dmamap_callback_t hptiop_mv_map_ctlcfg;
1403022a7377SXin LI static bus_dmamap_callback_t hptiop_mvfrey_map_ctlcfg;
14042f6cdcc8SScott Long
hptiop_alloc_pci_res_itl(struct hpt_iop_hba * hba)14052f6cdcc8SScott Long static int hptiop_alloc_pci_res_itl(struct hpt_iop_hba *hba)
14062f6cdcc8SScott Long {
14072f6cdcc8SScott Long hba->bar0_rid = 0x10;
14082f6cdcc8SScott Long hba->bar0_res = bus_alloc_resource_any(hba->pcidev,
14092f6cdcc8SScott Long SYS_RES_MEMORY, &hba->bar0_rid, RF_ACTIVE);
14102f6cdcc8SScott Long
14112f6cdcc8SScott Long if (hba->bar0_res == NULL) {
14122f6cdcc8SScott Long device_printf(hba->pcidev,
14132f6cdcc8SScott Long "failed to get iop base adrress.\n");
14142f6cdcc8SScott Long return -1;
14152f6cdcc8SScott Long }
14162f6cdcc8SScott Long hba->bar0t = rman_get_bustag(hba->bar0_res);
14172f6cdcc8SScott Long hba->bar0h = rman_get_bushandle(hba->bar0_res);
14182f6cdcc8SScott Long hba->u.itl.mu = (struct hpt_iopmu_itl *)
14192f6cdcc8SScott Long rman_get_virtual(hba->bar0_res);
14202f6cdcc8SScott Long
14212f6cdcc8SScott Long if (!hba->u.itl.mu) {
14222f6cdcc8SScott Long bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
14232f6cdcc8SScott Long hba->bar0_rid, hba->bar0_res);
14242f6cdcc8SScott Long device_printf(hba->pcidev, "alloc mem res failed\n");
14252f6cdcc8SScott Long return -1;
14262f6cdcc8SScott Long }
14272f6cdcc8SScott Long
14282f6cdcc8SScott Long return 0;
14292f6cdcc8SScott Long }
14302f6cdcc8SScott Long
hptiop_alloc_pci_res_mv(struct hpt_iop_hba * hba)14312f6cdcc8SScott Long static int hptiop_alloc_pci_res_mv(struct hpt_iop_hba *hba)
14322f6cdcc8SScott Long {
14332f6cdcc8SScott Long hba->bar0_rid = 0x10;
14342f6cdcc8SScott Long hba->bar0_res = bus_alloc_resource_any(hba->pcidev,
14352f6cdcc8SScott Long SYS_RES_MEMORY, &hba->bar0_rid, RF_ACTIVE);
14362f6cdcc8SScott Long
14372f6cdcc8SScott Long if (hba->bar0_res == NULL) {
14382f6cdcc8SScott Long device_printf(hba->pcidev, "failed to get iop bar0.\n");
14392f6cdcc8SScott Long return -1;
14402f6cdcc8SScott Long }
14412f6cdcc8SScott Long hba->bar0t = rman_get_bustag(hba->bar0_res);
14422f6cdcc8SScott Long hba->bar0h = rman_get_bushandle(hba->bar0_res);
14432f6cdcc8SScott Long hba->u.mv.regs = (struct hpt_iopmv_regs *)
14442f6cdcc8SScott Long rman_get_virtual(hba->bar0_res);
14452f6cdcc8SScott Long
14462f6cdcc8SScott Long if (!hba->u.mv.regs) {
14472f6cdcc8SScott Long bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
14482f6cdcc8SScott Long hba->bar0_rid, hba->bar0_res);
14492f6cdcc8SScott Long device_printf(hba->pcidev, "alloc bar0 mem res failed\n");
14502f6cdcc8SScott Long return -1;
14512f6cdcc8SScott Long }
14522f6cdcc8SScott Long
14532f6cdcc8SScott Long hba->bar2_rid = 0x18;
14542f6cdcc8SScott Long hba->bar2_res = bus_alloc_resource_any(hba->pcidev,
14552f6cdcc8SScott Long SYS_RES_MEMORY, &hba->bar2_rid, RF_ACTIVE);
14562f6cdcc8SScott Long
14572f6cdcc8SScott Long if (hba->bar2_res == NULL) {
14582f6cdcc8SScott Long bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
14592f6cdcc8SScott Long hba->bar0_rid, hba->bar0_res);
14602f6cdcc8SScott Long device_printf(hba->pcidev, "failed to get iop bar2.\n");
14612f6cdcc8SScott Long return -1;
14622f6cdcc8SScott Long }
14632f6cdcc8SScott Long
14642f6cdcc8SScott Long hba->bar2t = rman_get_bustag(hba->bar2_res);
14652f6cdcc8SScott Long hba->bar2h = rman_get_bushandle(hba->bar2_res);
14662f6cdcc8SScott Long hba->u.mv.mu = (struct hpt_iopmu_mv *)rman_get_virtual(hba->bar2_res);
14672f6cdcc8SScott Long
14682f6cdcc8SScott Long if (!hba->u.mv.mu) {
14692f6cdcc8SScott Long bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
14702f6cdcc8SScott Long hba->bar0_rid, hba->bar0_res);
14712f6cdcc8SScott Long bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
14722f6cdcc8SScott Long hba->bar2_rid, hba->bar2_res);
14732f6cdcc8SScott Long device_printf(hba->pcidev, "alloc mem bar2 res failed\n");
14742f6cdcc8SScott Long return -1;
14752f6cdcc8SScott Long }
14762f6cdcc8SScott Long
14772f6cdcc8SScott Long return 0;
14782f6cdcc8SScott Long }
14792f6cdcc8SScott Long
hptiop_alloc_pci_res_mvfrey(struct hpt_iop_hba * hba)1480022a7377SXin LI static int hptiop_alloc_pci_res_mvfrey(struct hpt_iop_hba *hba)
1481022a7377SXin LI {
1482022a7377SXin LI hba->bar0_rid = 0x10;
1483022a7377SXin LI hba->bar0_res = bus_alloc_resource_any(hba->pcidev,
1484022a7377SXin LI SYS_RES_MEMORY, &hba->bar0_rid, RF_ACTIVE);
1485022a7377SXin LI
1486022a7377SXin LI if (hba->bar0_res == NULL) {
1487022a7377SXin LI device_printf(hba->pcidev, "failed to get iop bar0.\n");
1488022a7377SXin LI return -1;
1489022a7377SXin LI }
1490022a7377SXin LI hba->bar0t = rman_get_bustag(hba->bar0_res);
1491022a7377SXin LI hba->bar0h = rman_get_bushandle(hba->bar0_res);
1492022a7377SXin LI hba->u.mvfrey.config = (struct hpt_iop_request_get_config *)
1493022a7377SXin LI rman_get_virtual(hba->bar0_res);
1494022a7377SXin LI
1495022a7377SXin LI if (!hba->u.mvfrey.config) {
1496022a7377SXin LI bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1497022a7377SXin LI hba->bar0_rid, hba->bar0_res);
1498022a7377SXin LI device_printf(hba->pcidev, "alloc bar0 mem res failed\n");
1499022a7377SXin LI return -1;
1500022a7377SXin LI }
1501022a7377SXin LI
1502022a7377SXin LI hba->bar2_rid = 0x18;
1503022a7377SXin LI hba->bar2_res = bus_alloc_resource_any(hba->pcidev,
1504022a7377SXin LI SYS_RES_MEMORY, &hba->bar2_rid, RF_ACTIVE);
1505022a7377SXin LI
1506022a7377SXin LI if (hba->bar2_res == NULL) {
1507022a7377SXin LI bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1508022a7377SXin LI hba->bar0_rid, hba->bar0_res);
1509022a7377SXin LI device_printf(hba->pcidev, "failed to get iop bar2.\n");
1510022a7377SXin LI return -1;
1511022a7377SXin LI }
1512022a7377SXin LI
1513022a7377SXin LI hba->bar2t = rman_get_bustag(hba->bar2_res);
1514022a7377SXin LI hba->bar2h = rman_get_bushandle(hba->bar2_res);
1515022a7377SXin LI hba->u.mvfrey.mu =
1516022a7377SXin LI (struct hpt_iopmu_mvfrey *)rman_get_virtual(hba->bar2_res);
1517022a7377SXin LI
1518022a7377SXin LI if (!hba->u.mvfrey.mu) {
1519022a7377SXin LI bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1520022a7377SXin LI hba->bar0_rid, hba->bar0_res);
1521022a7377SXin LI bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1522022a7377SXin LI hba->bar2_rid, hba->bar2_res);
1523022a7377SXin LI device_printf(hba->pcidev, "alloc mem bar2 res failed\n");
1524022a7377SXin LI return -1;
1525022a7377SXin LI }
1526022a7377SXin LI
1527022a7377SXin LI return 0;
1528022a7377SXin LI }
1529022a7377SXin LI
hptiop_release_pci_res_itl(struct hpt_iop_hba * hba)15302f6cdcc8SScott Long static void hptiop_release_pci_res_itl(struct hpt_iop_hba *hba)
15312f6cdcc8SScott Long {
15322f6cdcc8SScott Long if (hba->bar0_res)
15332f6cdcc8SScott Long bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
15342f6cdcc8SScott Long hba->bar0_rid, hba->bar0_res);
15352f6cdcc8SScott Long }
15362f6cdcc8SScott Long
hptiop_release_pci_res_mv(struct hpt_iop_hba * hba)15372f6cdcc8SScott Long static void hptiop_release_pci_res_mv(struct hpt_iop_hba *hba)
15382f6cdcc8SScott Long {
15392f6cdcc8SScott Long if (hba->bar0_res)
15402f6cdcc8SScott Long bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
15412f6cdcc8SScott Long hba->bar0_rid, hba->bar0_res);
15422f6cdcc8SScott Long if (hba->bar2_res)
15432f6cdcc8SScott Long bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
15442f6cdcc8SScott Long hba->bar2_rid, hba->bar2_res);
15452f6cdcc8SScott Long }
15462f6cdcc8SScott Long
hptiop_release_pci_res_mvfrey(struct hpt_iop_hba * hba)1547022a7377SXin LI static void hptiop_release_pci_res_mvfrey(struct hpt_iop_hba *hba)
1548022a7377SXin LI {
1549022a7377SXin LI if (hba->bar0_res)
1550022a7377SXin LI bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1551022a7377SXin LI hba->bar0_rid, hba->bar0_res);
1552022a7377SXin LI if (hba->bar2_res)
1553022a7377SXin LI bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1554022a7377SXin LI hba->bar2_rid, hba->bar2_res);
1555022a7377SXin LI }
1556022a7377SXin LI
hptiop_internal_memalloc_mv(struct hpt_iop_hba * hba)15572f6cdcc8SScott Long static int hptiop_internal_memalloc_mv(struct hpt_iop_hba *hba)
15582f6cdcc8SScott Long {
15592f6cdcc8SScott Long if (bus_dma_tag_create(hba->parent_dmat,
15602f6cdcc8SScott Long 1,
15612f6cdcc8SScott Long 0,
15622f6cdcc8SScott Long BUS_SPACE_MAXADDR_32BIT,
15632f6cdcc8SScott Long BUS_SPACE_MAXADDR,
15642f6cdcc8SScott Long NULL, NULL,
15652f6cdcc8SScott Long 0x800 - 0x8,
15662f6cdcc8SScott Long 1,
15672f6cdcc8SScott Long BUS_SPACE_MAXSIZE_32BIT,
15682f6cdcc8SScott Long BUS_DMA_ALLOCNOW,
15692f6cdcc8SScott Long NULL,
15702f6cdcc8SScott Long NULL,
15712f6cdcc8SScott Long &hba->ctlcfg_dmat)) {
15722f6cdcc8SScott Long device_printf(hba->pcidev, "alloc ctlcfg_dmat failed\n");
15732f6cdcc8SScott Long return -1;
15742f6cdcc8SScott Long }
15752f6cdcc8SScott Long
15762f6cdcc8SScott Long if (bus_dmamem_alloc(hba->ctlcfg_dmat, (void **)&hba->ctlcfg_ptr,
15772f6cdcc8SScott Long BUS_DMA_WAITOK | BUS_DMA_COHERENT,
15782f6cdcc8SScott Long &hba->ctlcfg_dmamap) != 0) {
15792f6cdcc8SScott Long device_printf(hba->pcidev,
15802f6cdcc8SScott Long "bus_dmamem_alloc failed!\n");
15812f6cdcc8SScott Long bus_dma_tag_destroy(hba->ctlcfg_dmat);
15822f6cdcc8SScott Long return -1;
15832f6cdcc8SScott Long }
15842f6cdcc8SScott Long
15852f6cdcc8SScott Long if (bus_dmamap_load(hba->ctlcfg_dmat,
15862f6cdcc8SScott Long hba->ctlcfg_dmamap, hba->ctlcfg_ptr,
15872f6cdcc8SScott Long MVIOP_IOCTLCFG_SIZE,
15882f6cdcc8SScott Long hptiop_mv_map_ctlcfg, hba, 0)) {
15892f6cdcc8SScott Long device_printf(hba->pcidev, "bus_dmamap_load failed!\n");
1590772de245SEitan Adler if (hba->ctlcfg_dmat) {
15912f6cdcc8SScott Long bus_dmamem_free(hba->ctlcfg_dmat,
15922f6cdcc8SScott Long hba->ctlcfg_ptr, hba->ctlcfg_dmamap);
15932f6cdcc8SScott Long bus_dma_tag_destroy(hba->ctlcfg_dmat);
1594772de245SEitan Adler }
15952f6cdcc8SScott Long return -1;
15962f6cdcc8SScott Long }
15972f6cdcc8SScott Long
15982f6cdcc8SScott Long return 0;
15992f6cdcc8SScott Long }
16002f6cdcc8SScott Long
hptiop_internal_memalloc_mvfrey(struct hpt_iop_hba * hba)1601022a7377SXin LI static int hptiop_internal_memalloc_mvfrey(struct hpt_iop_hba *hba)
1602022a7377SXin LI {
1603022a7377SXin LI u_int32_t list_count = BUS_SPACE_RD4_MVFREY2(inbound_conf_ctl);
1604022a7377SXin LI
1605022a7377SXin LI list_count >>= 16;
1606022a7377SXin LI
1607022a7377SXin LI if (list_count == 0) {
1608022a7377SXin LI return -1;
1609022a7377SXin LI }
1610022a7377SXin LI
1611022a7377SXin LI hba->u.mvfrey.list_count = list_count;
1612022a7377SXin LI hba->u.mvfrey.internal_mem_size = 0x800
1613022a7377SXin LI + list_count * sizeof(struct mvfrey_inlist_entry)
1614022a7377SXin LI + list_count * sizeof(struct mvfrey_outlist_entry)
1615022a7377SXin LI + sizeof(int);
1616022a7377SXin LI if (bus_dma_tag_create(hba->parent_dmat,
1617022a7377SXin LI 1,
1618022a7377SXin LI 0,
1619022a7377SXin LI BUS_SPACE_MAXADDR_32BIT,
1620022a7377SXin LI BUS_SPACE_MAXADDR,
1621022a7377SXin LI NULL, NULL,
1622022a7377SXin LI hba->u.mvfrey.internal_mem_size,
1623022a7377SXin LI 1,
1624022a7377SXin LI BUS_SPACE_MAXSIZE_32BIT,
1625022a7377SXin LI BUS_DMA_ALLOCNOW,
1626022a7377SXin LI NULL,
1627022a7377SXin LI NULL,
1628022a7377SXin LI &hba->ctlcfg_dmat)) {
1629022a7377SXin LI device_printf(hba->pcidev, "alloc ctlcfg_dmat failed\n");
1630022a7377SXin LI return -1;
1631022a7377SXin LI }
1632022a7377SXin LI
1633022a7377SXin LI if (bus_dmamem_alloc(hba->ctlcfg_dmat, (void **)&hba->ctlcfg_ptr,
1634022a7377SXin LI BUS_DMA_WAITOK | BUS_DMA_COHERENT,
1635022a7377SXin LI &hba->ctlcfg_dmamap) != 0) {
1636022a7377SXin LI device_printf(hba->pcidev,
1637022a7377SXin LI "bus_dmamem_alloc failed!\n");
1638022a7377SXin LI bus_dma_tag_destroy(hba->ctlcfg_dmat);
1639022a7377SXin LI return -1;
1640022a7377SXin LI }
1641022a7377SXin LI
1642022a7377SXin LI if (bus_dmamap_load(hba->ctlcfg_dmat,
1643022a7377SXin LI hba->ctlcfg_dmamap, hba->ctlcfg_ptr,
1644022a7377SXin LI hba->u.mvfrey.internal_mem_size,
1645022a7377SXin LI hptiop_mvfrey_map_ctlcfg, hba, 0)) {
1646022a7377SXin LI device_printf(hba->pcidev, "bus_dmamap_load failed!\n");
164728cb773dSXin LI if (hba->ctlcfg_dmat) {
1648022a7377SXin LI bus_dmamem_free(hba->ctlcfg_dmat,
1649022a7377SXin LI hba->ctlcfg_ptr, hba->ctlcfg_dmamap);
1650022a7377SXin LI bus_dma_tag_destroy(hba->ctlcfg_dmat);
165128cb773dSXin LI }
1652022a7377SXin LI return -1;
1653022a7377SXin LI }
1654022a7377SXin LI
1655022a7377SXin LI return 0;
1656022a7377SXin LI }
1657022a7377SXin LI
hptiop_internal_memfree_itl(struct hpt_iop_hba * hba)1658022a7377SXin LI static int hptiop_internal_memfree_itl(struct hpt_iop_hba *hba) {
1659022a7377SXin LI return 0;
1660022a7377SXin LI }
1661022a7377SXin LI
hptiop_internal_memfree_mv(struct hpt_iop_hba * hba)16622f6cdcc8SScott Long static int hptiop_internal_memfree_mv(struct hpt_iop_hba *hba)
16632f6cdcc8SScott Long {
16642f6cdcc8SScott Long if (hba->ctlcfg_dmat) {
16652f6cdcc8SScott Long bus_dmamap_unload(hba->ctlcfg_dmat, hba->ctlcfg_dmamap);
16662f6cdcc8SScott Long bus_dmamem_free(hba->ctlcfg_dmat,
16672f6cdcc8SScott Long hba->ctlcfg_ptr, hba->ctlcfg_dmamap);
16682f6cdcc8SScott Long bus_dma_tag_destroy(hba->ctlcfg_dmat);
16692f6cdcc8SScott Long }
16702f6cdcc8SScott Long
16712f6cdcc8SScott Long return 0;
16722f6cdcc8SScott Long }
16732f6cdcc8SScott Long
hptiop_internal_memfree_mvfrey(struct hpt_iop_hba * hba)1674022a7377SXin LI static int hptiop_internal_memfree_mvfrey(struct hpt_iop_hba *hba)
1675022a7377SXin LI {
1676022a7377SXin LI if (hba->ctlcfg_dmat) {
1677022a7377SXin LI bus_dmamap_unload(hba->ctlcfg_dmat, hba->ctlcfg_dmamap);
1678022a7377SXin LI bus_dmamem_free(hba->ctlcfg_dmat,
1679022a7377SXin LI hba->ctlcfg_ptr, hba->ctlcfg_dmamap);
1680022a7377SXin LI bus_dma_tag_destroy(hba->ctlcfg_dmat);
1681022a7377SXin LI }
1682022a7377SXin LI
1683022a7377SXin LI return 0;
1684022a7377SXin LI }
1685022a7377SXin LI
hptiop_reset_comm_mvfrey(struct hpt_iop_hba * hba)1686022a7377SXin LI static int hptiop_reset_comm_mvfrey(struct hpt_iop_hba *hba)
1687022a7377SXin LI {
1688022a7377SXin LI u_int32_t i = 100;
1689022a7377SXin LI
1690022a7377SXin LI if (hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET_COMM, 3000))
1691022a7377SXin LI return -1;
1692022a7377SXin LI
1693022a7377SXin LI /* wait 100ms for MCU ready */
1694022a7377SXin LI while(i--) {
1695022a7377SXin LI DELAY(1000);
1696022a7377SXin LI }
1697022a7377SXin LI
1698022a7377SXin LI BUS_SPACE_WRT4_MVFREY2(inbound_base,
1699022a7377SXin LI hba->u.mvfrey.inlist_phy & 0xffffffff);
1700022a7377SXin LI BUS_SPACE_WRT4_MVFREY2(inbound_base_high,
1701022a7377SXin LI (hba->u.mvfrey.inlist_phy >> 16) >> 16);
1702022a7377SXin LI
1703022a7377SXin LI BUS_SPACE_WRT4_MVFREY2(outbound_base,
1704022a7377SXin LI hba->u.mvfrey.outlist_phy & 0xffffffff);
1705022a7377SXin LI BUS_SPACE_WRT4_MVFREY2(outbound_base_high,
1706022a7377SXin LI (hba->u.mvfrey.outlist_phy >> 16) >> 16);
1707022a7377SXin LI
1708022a7377SXin LI BUS_SPACE_WRT4_MVFREY2(outbound_shadow_base,
1709022a7377SXin LI hba->u.mvfrey.outlist_cptr_phy & 0xffffffff);
1710022a7377SXin LI BUS_SPACE_WRT4_MVFREY2(outbound_shadow_base_high,
1711022a7377SXin LI (hba->u.mvfrey.outlist_cptr_phy >> 16) >> 16);
1712022a7377SXin LI
1713022a7377SXin LI hba->u.mvfrey.inlist_wptr = (hba->u.mvfrey.list_count - 1)
1714022a7377SXin LI | CL_POINTER_TOGGLE;
1715022a7377SXin LI *hba->u.mvfrey.outlist_cptr = (hba->u.mvfrey.list_count - 1)
1716022a7377SXin LI | CL_POINTER_TOGGLE;
1717022a7377SXin LI hba->u.mvfrey.outlist_rptr = hba->u.mvfrey.list_count - 1;
1718022a7377SXin LI
1719022a7377SXin LI return 0;
1720022a7377SXin LI }
1721022a7377SXin LI
17222f6cdcc8SScott Long /*
17232f6cdcc8SScott Long * CAM driver interface
17242f6cdcc8SScott Long */
17252f6cdcc8SScott Long static device_method_t driver_methods[] = {
17262f6cdcc8SScott Long /* Device interface */
17272f6cdcc8SScott Long DEVMETHOD(device_probe, hptiop_probe),
17282f6cdcc8SScott Long DEVMETHOD(device_attach, hptiop_attach),
17292f6cdcc8SScott Long DEVMETHOD(device_detach, hptiop_detach),
17302f6cdcc8SScott Long DEVMETHOD(device_shutdown, hptiop_shutdown),
17312f6cdcc8SScott Long { 0, 0 }
17322f6cdcc8SScott Long };
17332f6cdcc8SScott Long
17342f6cdcc8SScott Long static struct hptiop_adapter_ops hptiop_itl_ops = {
1735022a7377SXin LI .family = INTEL_BASED_IOP,
17362f6cdcc8SScott Long .iop_wait_ready = hptiop_wait_ready_itl,
17372f6cdcc8SScott Long .internal_memalloc = 0,
1738022a7377SXin LI .internal_memfree = hptiop_internal_memfree_itl,
17392f6cdcc8SScott Long .alloc_pci_res = hptiop_alloc_pci_res_itl,
17402f6cdcc8SScott Long .release_pci_res = hptiop_release_pci_res_itl,
17412f6cdcc8SScott Long .enable_intr = hptiop_enable_intr_itl,
17422f6cdcc8SScott Long .disable_intr = hptiop_disable_intr_itl,
17432f6cdcc8SScott Long .get_config = hptiop_get_config_itl,
17442f6cdcc8SScott Long .set_config = hptiop_set_config_itl,
17452f6cdcc8SScott Long .iop_intr = hptiop_intr_itl,
17462f6cdcc8SScott Long .post_msg = hptiop_post_msg_itl,
17472f6cdcc8SScott Long .post_req = hptiop_post_req_itl,
17482f6cdcc8SScott Long .do_ioctl = hptiop_do_ioctl_itl,
1749022a7377SXin LI .reset_comm = 0,
17502f6cdcc8SScott Long };
17512f6cdcc8SScott Long
17522f6cdcc8SScott Long static struct hptiop_adapter_ops hptiop_mv_ops = {
1753022a7377SXin LI .family = MV_BASED_IOP,
17542f6cdcc8SScott Long .iop_wait_ready = hptiop_wait_ready_mv,
17552f6cdcc8SScott Long .internal_memalloc = hptiop_internal_memalloc_mv,
17562f6cdcc8SScott Long .internal_memfree = hptiop_internal_memfree_mv,
17572f6cdcc8SScott Long .alloc_pci_res = hptiop_alloc_pci_res_mv,
17582f6cdcc8SScott Long .release_pci_res = hptiop_release_pci_res_mv,
17592f6cdcc8SScott Long .enable_intr = hptiop_enable_intr_mv,
17602f6cdcc8SScott Long .disable_intr = hptiop_disable_intr_mv,
17612f6cdcc8SScott Long .get_config = hptiop_get_config_mv,
17622f6cdcc8SScott Long .set_config = hptiop_set_config_mv,
17632f6cdcc8SScott Long .iop_intr = hptiop_intr_mv,
17642f6cdcc8SScott Long .post_msg = hptiop_post_msg_mv,
17652f6cdcc8SScott Long .post_req = hptiop_post_req_mv,
17662f6cdcc8SScott Long .do_ioctl = hptiop_do_ioctl_mv,
1767022a7377SXin LI .reset_comm = 0,
1768022a7377SXin LI };
1769022a7377SXin LI
1770022a7377SXin LI static struct hptiop_adapter_ops hptiop_mvfrey_ops = {
1771022a7377SXin LI .family = MVFREY_BASED_IOP,
1772022a7377SXin LI .iop_wait_ready = hptiop_wait_ready_mvfrey,
1773022a7377SXin LI .internal_memalloc = hptiop_internal_memalloc_mvfrey,
1774022a7377SXin LI .internal_memfree = hptiop_internal_memfree_mvfrey,
1775022a7377SXin LI .alloc_pci_res = hptiop_alloc_pci_res_mvfrey,
1776022a7377SXin LI .release_pci_res = hptiop_release_pci_res_mvfrey,
1777022a7377SXin LI .enable_intr = hptiop_enable_intr_mvfrey,
1778022a7377SXin LI .disable_intr = hptiop_disable_intr_mvfrey,
1779022a7377SXin LI .get_config = hptiop_get_config_mvfrey,
1780022a7377SXin LI .set_config = hptiop_set_config_mvfrey,
1781022a7377SXin LI .iop_intr = hptiop_intr_mvfrey,
1782022a7377SXin LI .post_msg = hptiop_post_msg_mvfrey,
1783022a7377SXin LI .post_req = hptiop_post_req_mvfrey,
1784022a7377SXin LI .do_ioctl = hptiop_do_ioctl_mvfrey,
1785022a7377SXin LI .reset_comm = hptiop_reset_comm_mvfrey,
17862f6cdcc8SScott Long };
17872f6cdcc8SScott Long
17882f6cdcc8SScott Long static driver_t hptiop_pci_driver = {
17892f6cdcc8SScott Long driver_name,
17902f6cdcc8SScott Long driver_methods,
17912f6cdcc8SScott Long sizeof(struct hpt_iop_hba)
17922f6cdcc8SScott Long };
17932f6cdcc8SScott Long
17948272dd4bSJohn Baldwin DRIVER_MODULE(hptiop, pci, hptiop_pci_driver, 0, 0);
1795d45ce511SEitan Adler MODULE_DEPEND(hptiop, cam, 1, 1, 1);
17962f6cdcc8SScott Long
hptiop_probe(device_t dev)17972f6cdcc8SScott Long static int hptiop_probe(device_t dev)
17982f6cdcc8SScott Long {
17992f6cdcc8SScott Long struct hpt_iop_hba *hba;
18002f6cdcc8SScott Long u_int32_t id;
18012f6cdcc8SScott Long int sas = 0;
18022f6cdcc8SScott Long struct hptiop_adapter_ops *ops;
18032f6cdcc8SScott Long
18042f6cdcc8SScott Long if (pci_get_vendor(dev) != 0x1103)
18052f6cdcc8SScott Long return (ENXIO);
18062f6cdcc8SScott Long
18072f6cdcc8SScott Long id = pci_get_device(dev);
18082f6cdcc8SScott Long
18092f6cdcc8SScott Long switch (id) {
1810022a7377SXin LI case 0x4520:
1811bba8d13eSXin LI case 0x4521:
1812022a7377SXin LI case 0x4522:
1813022a7377SXin LI sas = 1;
1814bba8d13eSXin LI case 0x3620:
1815bba8d13eSXin LI case 0x3622:
1816bba8d13eSXin LI case 0x3640:
1817022a7377SXin LI ops = &hptiop_mvfrey_ops;
1818022a7377SXin LI break;
18190af67118SXin LI case 0x4210:
18200af67118SXin LI case 0x4211:
18210af67118SXin LI case 0x4310:
18220af67118SXin LI case 0x4311:
18232f6cdcc8SScott Long case 0x4320:
18240af67118SXin LI case 0x4321:
18250af67118SXin LI case 0x4322:
18262f6cdcc8SScott Long sas = 1;
18272f6cdcc8SScott Long case 0x3220:
18282f6cdcc8SScott Long case 0x3320:
18292f6cdcc8SScott Long case 0x3410:
18302f6cdcc8SScott Long case 0x3520:
18312f6cdcc8SScott Long case 0x3510:
18322f6cdcc8SScott Long case 0x3511:
18332f6cdcc8SScott Long case 0x3521:
18342f6cdcc8SScott Long case 0x3522:
18350af67118SXin LI case 0x3530:
18362f6cdcc8SScott Long case 0x3540:
18370af67118SXin LI case 0x3560:
18382f6cdcc8SScott Long ops = &hptiop_itl_ops;
18392f6cdcc8SScott Long break;
18400af67118SXin LI case 0x3020:
18412f6cdcc8SScott Long case 0x3120:
18422f6cdcc8SScott Long case 0x3122:
18432f6cdcc8SScott Long ops = &hptiop_mv_ops;
18442f6cdcc8SScott Long break;
18452f6cdcc8SScott Long default:
18462f6cdcc8SScott Long return (ENXIO);
18472f6cdcc8SScott Long }
18482f6cdcc8SScott Long
18492f6cdcc8SScott Long device_printf(dev, "adapter at PCI %d:%d:%d, IRQ %d\n",
18502f6cdcc8SScott Long pci_get_bus(dev), pci_get_slot(dev),
18512f6cdcc8SScott Long pci_get_function(dev), pci_get_irq(dev));
18522f6cdcc8SScott Long
18531c64b3abSMark Johnston device_set_descf(dev, "RocketRAID %x %s Controller",
18542f6cdcc8SScott Long id, sas ? "SAS" : "SATA");
18552f6cdcc8SScott Long
18562f6cdcc8SScott Long hba = (struct hpt_iop_hba *)device_get_softc(dev);
18572f6cdcc8SScott Long bzero(hba, sizeof(struct hpt_iop_hba));
18582f6cdcc8SScott Long hba->ops = ops;
18592f6cdcc8SScott Long
18602f6cdcc8SScott Long KdPrint(("hba->ops=%p\n", hba->ops));
18612f6cdcc8SScott Long return 0;
18622f6cdcc8SScott Long }
18632f6cdcc8SScott Long
hptiop_attach(device_t dev)18642f6cdcc8SScott Long static int hptiop_attach(device_t dev)
18652f6cdcc8SScott Long {
1866517a8a71SJohn Baldwin struct make_dev_args args;
18672f6cdcc8SScott Long struct hpt_iop_hba *hba = (struct hpt_iop_hba *)device_get_softc(dev);
18682f6cdcc8SScott Long struct hpt_iop_request_get_config iop_config;
18692f6cdcc8SScott Long struct hpt_iop_request_set_config set_config;
18702f6cdcc8SScott Long int rid = 0;
18712f6cdcc8SScott Long struct cam_devq *devq;
18722f6cdcc8SScott Long struct ccb_setasync ccb;
18732f6cdcc8SScott Long u_int32_t unit = device_get_unit(dev);
18742f6cdcc8SScott Long
18752f6cdcc8SScott Long device_printf(dev, "%d RocketRAID 3xxx/4xxx controller driver %s\n",
18762f6cdcc8SScott Long unit, driver_version);
18772f6cdcc8SScott Long
18782f6cdcc8SScott Long KdPrint(("hptiop: attach(%d, %d/%d/%d) ops=%p\n", unit,
18792f6cdcc8SScott Long pci_get_bus(dev), pci_get_slot(dev),
18802f6cdcc8SScott Long pci_get_function(dev), hba->ops));
18812f6cdcc8SScott Long
18822f6cdcc8SScott Long pci_enable_busmaster(dev);
18832f6cdcc8SScott Long hba->pcidev = dev;
18842f6cdcc8SScott Long hba->pciunit = unit;
18852f6cdcc8SScott Long
18862f6cdcc8SScott Long if (hba->ops->alloc_pci_res(hba))
18872f6cdcc8SScott Long return ENXIO;
18882f6cdcc8SScott Long
18892f6cdcc8SScott Long if (hba->ops->iop_wait_ready(hba, 2000)) {
18902f6cdcc8SScott Long device_printf(dev, "adapter is not ready\n");
18912f6cdcc8SScott Long goto release_pci_res;
18922f6cdcc8SScott Long }
18932f6cdcc8SScott Long
18942f6cdcc8SScott Long mtx_init(&hba->lock, "hptioplock", NULL, MTX_DEF);
18952f6cdcc8SScott Long
1896b6f97155SScott Long if (bus_dma_tag_create(bus_get_dma_tag(dev),/* PCI parent */
18972f6cdcc8SScott Long 1, /* alignment */
18982f6cdcc8SScott Long 0, /* boundary */
18992f6cdcc8SScott Long BUS_SPACE_MAXADDR, /* lowaddr */
19002f6cdcc8SScott Long BUS_SPACE_MAXADDR, /* highaddr */
19012f6cdcc8SScott Long NULL, NULL, /* filter, filterarg */
19022f6cdcc8SScott Long BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
19032f6cdcc8SScott Long BUS_SPACE_UNRESTRICTED, /* nsegments */
19042f6cdcc8SScott Long BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
19052f6cdcc8SScott Long 0, /* flags */
19062f6cdcc8SScott Long NULL, /* lockfunc */
19072f6cdcc8SScott Long NULL, /* lockfuncarg */
19082f6cdcc8SScott Long &hba->parent_dmat /* tag */))
19092f6cdcc8SScott Long {
19102f6cdcc8SScott Long device_printf(dev, "alloc parent_dmat failed\n");
19112f6cdcc8SScott Long goto release_pci_res;
19122f6cdcc8SScott Long }
19132f6cdcc8SScott Long
1914022a7377SXin LI if (hba->ops->family == MV_BASED_IOP) {
19152f6cdcc8SScott Long if (hba->ops->internal_memalloc(hba)) {
19162f6cdcc8SScott Long device_printf(dev, "alloc srb_dmat failed\n");
19172f6cdcc8SScott Long goto destroy_parent_tag;
19182f6cdcc8SScott Long }
19192f6cdcc8SScott Long }
19202f6cdcc8SScott Long
19212f6cdcc8SScott Long if (hba->ops->get_config(hba, &iop_config)) {
19222f6cdcc8SScott Long device_printf(dev, "get iop config failed.\n");
19232f6cdcc8SScott Long goto get_config_failed;
19242f6cdcc8SScott Long }
19252f6cdcc8SScott Long
19262f6cdcc8SScott Long hba->firmware_version = iop_config.firmware_version;
19272f6cdcc8SScott Long hba->interface_version = iop_config.interface_version;
19282f6cdcc8SScott Long hba->max_requests = iop_config.max_requests;
19292f6cdcc8SScott Long hba->max_devices = iop_config.max_devices;
19302f6cdcc8SScott Long hba->max_request_size = iop_config.request_size;
19312f6cdcc8SScott Long hba->max_sg_count = iop_config.max_sg_count;
19322f6cdcc8SScott Long
1933022a7377SXin LI if (hba->ops->family == MVFREY_BASED_IOP) {
1934022a7377SXin LI if (hba->ops->internal_memalloc(hba)) {
1935022a7377SXin LI device_printf(dev, "alloc srb_dmat failed\n");
1936022a7377SXin LI goto destroy_parent_tag;
1937022a7377SXin LI }
1938022a7377SXin LI if (hba->ops->reset_comm(hba)) {
1939022a7377SXin LI device_printf(dev, "reset comm failed\n");
1940022a7377SXin LI goto get_config_failed;
1941022a7377SXin LI }
1942022a7377SXin LI }
1943022a7377SXin LI
19442f6cdcc8SScott Long if (bus_dma_tag_create(hba->parent_dmat,/* parent */
19452f6cdcc8SScott Long 4, /* alignment */
19462f6cdcc8SScott Long BUS_SPACE_MAXADDR_32BIT+1, /* boundary */
19472f6cdcc8SScott Long BUS_SPACE_MAXADDR, /* lowaddr */
19482f6cdcc8SScott Long BUS_SPACE_MAXADDR, /* highaddr */
19492f6cdcc8SScott Long NULL, NULL, /* filter, filterarg */
19502f6cdcc8SScott Long PAGE_SIZE * (hba->max_sg_count-1), /* maxsize */
19512f6cdcc8SScott Long hba->max_sg_count, /* nsegments */
19522f6cdcc8SScott Long 0x20000, /* maxsegsize */
19532f6cdcc8SScott Long BUS_DMA_ALLOCNOW, /* flags */
19542f6cdcc8SScott Long busdma_lock_mutex, /* lockfunc */
19552f6cdcc8SScott Long &hba->lock, /* lockfuncarg */
19562f6cdcc8SScott Long &hba->io_dmat /* tag */))
19572f6cdcc8SScott Long {
19582f6cdcc8SScott Long device_printf(dev, "alloc io_dmat failed\n");
19592f6cdcc8SScott Long goto get_config_failed;
19602f6cdcc8SScott Long }
19612f6cdcc8SScott Long
19622f6cdcc8SScott Long if (bus_dma_tag_create(hba->parent_dmat,/* parent */
19632f6cdcc8SScott Long 1, /* alignment */
19642f6cdcc8SScott Long 0, /* boundary */
19652f6cdcc8SScott Long BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
19662f6cdcc8SScott Long BUS_SPACE_MAXADDR, /* highaddr */
19672f6cdcc8SScott Long NULL, NULL, /* filter, filterarg */
19682f6cdcc8SScott Long HPT_SRB_MAX_SIZE * HPT_SRB_MAX_QUEUE_SIZE + 0x20,
19692f6cdcc8SScott Long 1, /* nsegments */
19702f6cdcc8SScott Long BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
19712f6cdcc8SScott Long 0, /* flags */
19722f6cdcc8SScott Long NULL, /* lockfunc */
19732f6cdcc8SScott Long NULL, /* lockfuncarg */
19742f6cdcc8SScott Long &hba->srb_dmat /* tag */))
19752f6cdcc8SScott Long {
19762f6cdcc8SScott Long device_printf(dev, "alloc srb_dmat failed\n");
19772f6cdcc8SScott Long goto destroy_io_dmat;
19782f6cdcc8SScott Long }
19792f6cdcc8SScott Long
19802f6cdcc8SScott Long if (bus_dmamem_alloc(hba->srb_dmat, (void **)&hba->uncached_ptr,
19812f6cdcc8SScott Long BUS_DMA_WAITOK | BUS_DMA_COHERENT,
19822f6cdcc8SScott Long &hba->srb_dmamap) != 0)
19832f6cdcc8SScott Long {
19842f6cdcc8SScott Long device_printf(dev, "srb bus_dmamem_alloc failed!\n");
19852f6cdcc8SScott Long goto destroy_srb_dmat;
19862f6cdcc8SScott Long }
19872f6cdcc8SScott Long
19882f6cdcc8SScott Long if (bus_dmamap_load(hba->srb_dmat,
19892f6cdcc8SScott Long hba->srb_dmamap, hba->uncached_ptr,
19902f6cdcc8SScott Long (HPT_SRB_MAX_SIZE * HPT_SRB_MAX_QUEUE_SIZE) + 0x20,
19912f6cdcc8SScott Long hptiop_map_srb, hba, 0))
19922f6cdcc8SScott Long {
19932f6cdcc8SScott Long device_printf(dev, "bus_dmamap_load failed!\n");
19942f6cdcc8SScott Long goto srb_dmamem_free;
19952f6cdcc8SScott Long }
19962f6cdcc8SScott Long
19972f6cdcc8SScott Long if ((devq = cam_simq_alloc(hba->max_requests - 1 )) == NULL) {
19982f6cdcc8SScott Long device_printf(dev, "cam_simq_alloc failed\n");
19992f6cdcc8SScott Long goto srb_dmamap_unload;
20002f6cdcc8SScott Long }
20012f6cdcc8SScott Long
20022f6cdcc8SScott Long hba->sim = cam_sim_alloc(hptiop_action, hptiop_poll, driver_name,
2003ef588050SJohn Baldwin hba, unit, &hba->lock, hba->max_requests - 1, 1, devq);
20042f6cdcc8SScott Long if (!hba->sim) {
20052f6cdcc8SScott Long device_printf(dev, "cam_sim_alloc failed\n");
20062f6cdcc8SScott Long cam_simq_free(devq);
20072f6cdcc8SScott Long goto srb_dmamap_unload;
20082f6cdcc8SScott Long }
2009ef588050SJohn Baldwin hptiop_lock_adapter(hba);
20102f6cdcc8SScott Long if (xpt_bus_register(hba->sim, dev, 0) != CAM_SUCCESS)
20112f6cdcc8SScott Long {
20122f6cdcc8SScott Long device_printf(dev, "xpt_bus_register failed\n");
20132f6cdcc8SScott Long goto free_cam_sim;
20142f6cdcc8SScott Long }
20152f6cdcc8SScott Long
20162f6cdcc8SScott Long if (xpt_create_path(&hba->path, /*periph */ NULL,
20172f6cdcc8SScott Long cam_sim_path(hba->sim), CAM_TARGET_WILDCARD,
20182f6cdcc8SScott Long CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
20192f6cdcc8SScott Long device_printf(dev, "xpt_create_path failed\n");
20202f6cdcc8SScott Long goto deregister_xpt_bus;
20212f6cdcc8SScott Long }
2022ef588050SJohn Baldwin hptiop_unlock_adapter(hba);
20232f6cdcc8SScott Long
20242f6cdcc8SScott Long bzero(&set_config, sizeof(set_config));
20252f6cdcc8SScott Long set_config.iop_id = unit;
20262f6cdcc8SScott Long set_config.vbus_id = cam_sim_path(hba->sim);
20272f6cdcc8SScott Long set_config.max_host_request_size = HPT_SRB_MAX_REQ_SIZE;
20282f6cdcc8SScott Long
20292f6cdcc8SScott Long if (hba->ops->set_config(hba, &set_config)) {
20302f6cdcc8SScott Long device_printf(dev, "set iop config failed.\n");
20312f6cdcc8SScott Long goto free_hba_path;
20322f6cdcc8SScott Long }
20332f6cdcc8SScott Long
20348dc96b74SEdward Tomasz Napierala memset(&ccb, 0, sizeof(ccb));
20352f6cdcc8SScott Long xpt_setup_ccb(&ccb.ccb_h, hba->path, /*priority*/5);
20362f6cdcc8SScott Long ccb.ccb_h.func_code = XPT_SASYNC_CB;
20372f6cdcc8SScott Long ccb.event_enable = (AC_FOUND_DEVICE | AC_LOST_DEVICE);
20382f6cdcc8SScott Long ccb.callback = hptiop_async;
20392f6cdcc8SScott Long ccb.callback_arg = hba->sim;
20402f6cdcc8SScott Long xpt_action((union ccb *)&ccb);
20412f6cdcc8SScott Long
20422f6cdcc8SScott Long rid = 0;
204343cd6160SJustin Hibbits if ((hba->irq_res = bus_alloc_resource_any(hba->pcidev, SYS_RES_IRQ,
204443cd6160SJustin Hibbits &rid, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
20452f6cdcc8SScott Long device_printf(dev, "allocate irq failed!\n");
20462f6cdcc8SScott Long goto free_hba_path;
20472f6cdcc8SScott Long }
20482f6cdcc8SScott Long
2049ef588050SJohn Baldwin if (bus_setup_intr(hba->pcidev, hba->irq_res, INTR_TYPE_CAM | INTR_MPSAFE,
20502f6cdcc8SScott Long NULL, hptiop_pci_intr, hba, &hba->irq_handle))
20512f6cdcc8SScott Long {
20522f6cdcc8SScott Long device_printf(dev, "allocate intr function failed!\n");
20532f6cdcc8SScott Long goto free_irq_resource;
20542f6cdcc8SScott Long }
20552f6cdcc8SScott Long
20562f6cdcc8SScott Long if (hptiop_send_sync_msg(hba,
20572f6cdcc8SScott Long IOPMU_INBOUND_MSG0_START_BACKGROUND_TASK, 5000)) {
20582f6cdcc8SScott Long device_printf(dev, "fail to start background task\n");
20592f6cdcc8SScott Long goto teartown_irq_resource;
20602f6cdcc8SScott Long }
20612f6cdcc8SScott Long
20622f6cdcc8SScott Long hba->ops->enable_intr(hba);
2063022a7377SXin LI hba->initialized = 1;
20642f6cdcc8SScott Long
2065517a8a71SJohn Baldwin make_dev_args_init(&args);
2066517a8a71SJohn Baldwin args.mda_devsw = &hptiop_cdevsw;
2067517a8a71SJohn Baldwin args.mda_uid = UID_ROOT;
2068517a8a71SJohn Baldwin args.mda_gid = GID_WHEEL /*GID_OPERATOR*/;
2069517a8a71SJohn Baldwin args.mda_mode = S_IRUSR | S_IWUSR;
2070517a8a71SJohn Baldwin args.mda_si_drv1 = hba;
20712f6cdcc8SScott Long
2072517a8a71SJohn Baldwin make_dev_s(&args, &hba->ioctl_dev, "%s%d", driver_name, unit);
20732f6cdcc8SScott Long
20742f6cdcc8SScott Long return 0;
20752f6cdcc8SScott Long
20762f6cdcc8SScott Long
20772f6cdcc8SScott Long teartown_irq_resource:
20782f6cdcc8SScott Long bus_teardown_intr(dev, hba->irq_res, hba->irq_handle);
20792f6cdcc8SScott Long
20802f6cdcc8SScott Long free_irq_resource:
20812f6cdcc8SScott Long bus_release_resource(dev, SYS_RES_IRQ, 0, hba->irq_res);
20822f6cdcc8SScott Long
2083ef588050SJohn Baldwin hptiop_lock_adapter(hba);
20842f6cdcc8SScott Long free_hba_path:
20852f6cdcc8SScott Long xpt_free_path(hba->path);
20862f6cdcc8SScott Long
20872f6cdcc8SScott Long deregister_xpt_bus:
20882f6cdcc8SScott Long xpt_bus_deregister(cam_sim_path(hba->sim));
20892f6cdcc8SScott Long
20902f6cdcc8SScott Long free_cam_sim:
20912f6cdcc8SScott Long cam_sim_free(hba->sim, /*free devq*/ TRUE);
2092ef588050SJohn Baldwin hptiop_unlock_adapter(hba);
20932f6cdcc8SScott Long
20942f6cdcc8SScott Long srb_dmamap_unload:
20952f6cdcc8SScott Long if (hba->uncached_ptr)
20962f6cdcc8SScott Long bus_dmamap_unload(hba->srb_dmat, hba->srb_dmamap);
20972f6cdcc8SScott Long
20982f6cdcc8SScott Long srb_dmamem_free:
20992f6cdcc8SScott Long if (hba->uncached_ptr)
21002f6cdcc8SScott Long bus_dmamem_free(hba->srb_dmat,
21012f6cdcc8SScott Long hba->uncached_ptr, hba->srb_dmamap);
21022f6cdcc8SScott Long
21032f6cdcc8SScott Long destroy_srb_dmat:
21042f6cdcc8SScott Long if (hba->srb_dmat)
21052f6cdcc8SScott Long bus_dma_tag_destroy(hba->srb_dmat);
21062f6cdcc8SScott Long
21072f6cdcc8SScott Long destroy_io_dmat:
21082f6cdcc8SScott Long if (hba->io_dmat)
21092f6cdcc8SScott Long bus_dma_tag_destroy(hba->io_dmat);
21102f6cdcc8SScott Long
21112f6cdcc8SScott Long get_config_failed:
21122f6cdcc8SScott Long hba->ops->internal_memfree(hba);
21132f6cdcc8SScott Long
21142f6cdcc8SScott Long destroy_parent_tag:
21152f6cdcc8SScott Long if (hba->parent_dmat)
21162f6cdcc8SScott Long bus_dma_tag_destroy(hba->parent_dmat);
21172f6cdcc8SScott Long
21182f6cdcc8SScott Long release_pci_res:
21192f6cdcc8SScott Long if (hba->ops->release_pci_res)
21202f6cdcc8SScott Long hba->ops->release_pci_res(hba);
21212f6cdcc8SScott Long
21222f6cdcc8SScott Long return ENXIO;
21232f6cdcc8SScott Long }
21242f6cdcc8SScott Long
hptiop_detach(device_t dev)21252f6cdcc8SScott Long static int hptiop_detach(device_t dev)
21262f6cdcc8SScott Long {
21272f6cdcc8SScott Long struct hpt_iop_hba * hba = (struct hpt_iop_hba *)device_get_softc(dev);
21282f6cdcc8SScott Long int i;
21292f6cdcc8SScott Long int error = EBUSY;
21302f6cdcc8SScott Long
21312f6cdcc8SScott Long hptiop_lock_adapter(hba);
21322f6cdcc8SScott Long for (i = 0; i < hba->max_devices; i++)
21332f6cdcc8SScott Long if (hptiop_os_query_remove_device(hba, i)) {
21342f6cdcc8SScott Long device_printf(dev, "%d file system is busy. id=%d",
21352f6cdcc8SScott Long hba->pciunit, i);
21362f6cdcc8SScott Long goto out;
21372f6cdcc8SScott Long }
21382f6cdcc8SScott Long
21392f6cdcc8SScott Long if ((error = hptiop_shutdown(dev)) != 0)
21402f6cdcc8SScott Long goto out;
21412f6cdcc8SScott Long if (hptiop_send_sync_msg(hba,
21422f6cdcc8SScott Long IOPMU_INBOUND_MSG0_STOP_BACKGROUND_TASK, 60000))
21432f6cdcc8SScott Long goto out;
2144ef588050SJohn Baldwin hptiop_unlock_adapter(hba);
21452f6cdcc8SScott Long
21462f6cdcc8SScott Long hptiop_release_resource(hba);
2147ef588050SJohn Baldwin return (0);
21482f6cdcc8SScott Long out:
21492f6cdcc8SScott Long hptiop_unlock_adapter(hba);
21502f6cdcc8SScott Long return error;
21512f6cdcc8SScott Long }
21522f6cdcc8SScott Long
hptiop_shutdown(device_t dev)21532f6cdcc8SScott Long static int hptiop_shutdown(device_t dev)
21542f6cdcc8SScott Long {
21552f6cdcc8SScott Long struct hpt_iop_hba * hba = (struct hpt_iop_hba *)device_get_softc(dev);
21562f6cdcc8SScott Long
21572f6cdcc8SScott Long int error = 0;
21582f6cdcc8SScott Long
21592f6cdcc8SScott Long if (hba->flag & HPT_IOCTL_FLAG_OPEN) {
21602f6cdcc8SScott Long device_printf(dev, "%d device is busy", hba->pciunit);
21612f6cdcc8SScott Long return EBUSY;
21622f6cdcc8SScott Long }
21632f6cdcc8SScott Long
21642f6cdcc8SScott Long hba->ops->disable_intr(hba);
21652f6cdcc8SScott Long
21662f6cdcc8SScott Long if (hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_SHUTDOWN, 60000))
21672f6cdcc8SScott Long error = EBUSY;
21682f6cdcc8SScott Long
21692f6cdcc8SScott Long return error;
21702f6cdcc8SScott Long }
21712f6cdcc8SScott Long
hptiop_pci_intr(void * arg)21722f6cdcc8SScott Long static void hptiop_pci_intr(void *arg)
21732f6cdcc8SScott Long {
21742f6cdcc8SScott Long struct hpt_iop_hba * hba = (struct hpt_iop_hba *)arg;
21752f6cdcc8SScott Long hptiop_lock_adapter(hba);
21762f6cdcc8SScott Long hba->ops->iop_intr(hba);
21772f6cdcc8SScott Long hptiop_unlock_adapter(hba);
21782f6cdcc8SScott Long }
21792f6cdcc8SScott Long
hptiop_poll(struct cam_sim * sim)21802f6cdcc8SScott Long static void hptiop_poll(struct cam_sim *sim)
21812f6cdcc8SScott Long {
2182ef588050SJohn Baldwin struct hpt_iop_hba *hba;
2183ef588050SJohn Baldwin
2184ef588050SJohn Baldwin hba = cam_sim_softc(sim);
2185ef588050SJohn Baldwin hba->ops->iop_intr(hba);
21862f6cdcc8SScott Long }
21872f6cdcc8SScott Long
hptiop_async(void * callback_arg,u_int32_t code,struct cam_path * path,void * arg)21882f6cdcc8SScott Long static void hptiop_async(void * callback_arg, u_int32_t code,
21892f6cdcc8SScott Long struct cam_path * path, void * arg)
21902f6cdcc8SScott Long {
21912f6cdcc8SScott Long }
21922f6cdcc8SScott Long
hptiop_enable_intr_itl(struct hpt_iop_hba * hba)21932f6cdcc8SScott Long static void hptiop_enable_intr_itl(struct hpt_iop_hba *hba)
21942f6cdcc8SScott Long {
21952f6cdcc8SScott Long BUS_SPACE_WRT4_ITL(outbound_intmask,
21962f6cdcc8SScott Long ~(IOPMU_OUTBOUND_INT_POSTQUEUE | IOPMU_OUTBOUND_INT_MSG0));
21972f6cdcc8SScott Long }
21982f6cdcc8SScott Long
hptiop_enable_intr_mv(struct hpt_iop_hba * hba)21992f6cdcc8SScott Long static void hptiop_enable_intr_mv(struct hpt_iop_hba *hba)
22002f6cdcc8SScott Long {
22012f6cdcc8SScott Long u_int32_t int_mask;
22022f6cdcc8SScott Long
22032f6cdcc8SScott Long int_mask = BUS_SPACE_RD4_MV0(outbound_intmask);
22042f6cdcc8SScott Long
22052f6cdcc8SScott Long int_mask |= MVIOP_MU_OUTBOUND_INT_POSTQUEUE
22062f6cdcc8SScott Long | MVIOP_MU_OUTBOUND_INT_MSG;
22072f6cdcc8SScott Long BUS_SPACE_WRT4_MV0(outbound_intmask,int_mask);
22082f6cdcc8SScott Long }
22092f6cdcc8SScott Long
hptiop_enable_intr_mvfrey(struct hpt_iop_hba * hba)2210022a7377SXin LI static void hptiop_enable_intr_mvfrey(struct hpt_iop_hba *hba)
2211022a7377SXin LI {
2212022a7377SXin LI BUS_SPACE_WRT4_MVFREY2(f0_doorbell_enable, CPU_TO_F0_DRBL_MSG_A_BIT);
2213022a7377SXin LI BUS_SPACE_RD4_MVFREY2(f0_doorbell_enable);
2214022a7377SXin LI
2215022a7377SXin LI BUS_SPACE_WRT4_MVFREY2(isr_enable, 0x1);
2216022a7377SXin LI BUS_SPACE_RD4_MVFREY2(isr_enable);
2217022a7377SXin LI
2218022a7377SXin LI BUS_SPACE_WRT4_MVFREY2(pcie_f0_int_enable, 0x1010);
2219022a7377SXin LI BUS_SPACE_RD4_MVFREY2(pcie_f0_int_enable);
2220022a7377SXin LI }
2221022a7377SXin LI
hptiop_disable_intr_itl(struct hpt_iop_hba * hba)22222f6cdcc8SScott Long static void hptiop_disable_intr_itl(struct hpt_iop_hba *hba)
22232f6cdcc8SScott Long {
22242f6cdcc8SScott Long u_int32_t int_mask;
22252f6cdcc8SScott Long
22262f6cdcc8SScott Long int_mask = BUS_SPACE_RD4_ITL(outbound_intmask);
22272f6cdcc8SScott Long
22282f6cdcc8SScott Long int_mask |= IOPMU_OUTBOUND_INT_POSTQUEUE | IOPMU_OUTBOUND_INT_MSG0;
22292f6cdcc8SScott Long BUS_SPACE_WRT4_ITL(outbound_intmask, int_mask);
22302f6cdcc8SScott Long BUS_SPACE_RD4_ITL(outbound_intstatus);
22312f6cdcc8SScott Long }
22322f6cdcc8SScott Long
hptiop_disable_intr_mv(struct hpt_iop_hba * hba)22332f6cdcc8SScott Long static void hptiop_disable_intr_mv(struct hpt_iop_hba *hba)
22342f6cdcc8SScott Long {
22352f6cdcc8SScott Long u_int32_t int_mask;
22362f6cdcc8SScott Long int_mask = BUS_SPACE_RD4_MV0(outbound_intmask);
22372f6cdcc8SScott Long
22382f6cdcc8SScott Long int_mask &= ~(MVIOP_MU_OUTBOUND_INT_MSG
22392f6cdcc8SScott Long | MVIOP_MU_OUTBOUND_INT_POSTQUEUE);
22402f6cdcc8SScott Long BUS_SPACE_WRT4_MV0(outbound_intmask,int_mask);
22412f6cdcc8SScott Long BUS_SPACE_RD4_MV0(outbound_intmask);
22422f6cdcc8SScott Long }
22432f6cdcc8SScott Long
hptiop_disable_intr_mvfrey(struct hpt_iop_hba * hba)2244022a7377SXin LI static void hptiop_disable_intr_mvfrey(struct hpt_iop_hba *hba)
22452f6cdcc8SScott Long {
2246022a7377SXin LI BUS_SPACE_WRT4_MVFREY2(f0_doorbell_enable, 0);
2247022a7377SXin LI BUS_SPACE_RD4_MVFREY2(f0_doorbell_enable);
2248022a7377SXin LI
2249022a7377SXin LI BUS_SPACE_WRT4_MVFREY2(isr_enable, 0);
2250022a7377SXin LI BUS_SPACE_RD4_MVFREY2(isr_enable);
2251022a7377SXin LI
2252022a7377SXin LI BUS_SPACE_WRT4_MVFREY2(pcie_f0_int_enable, 0);
2253022a7377SXin LI BUS_SPACE_RD4_MVFREY2(pcie_f0_int_enable);
2254022a7377SXin LI }
2255022a7377SXin LI
hptiop_reset_adapter(void * argv)2256022a7377SXin LI static void hptiop_reset_adapter(void *argv)
2257022a7377SXin LI {
2258022a7377SXin LI struct hpt_iop_hba * hba = (struct hpt_iop_hba *)argv;
2259022a7377SXin LI if (hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET, 60000))
2260022a7377SXin LI return;
2261022a7377SXin LI hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_START_BACKGROUND_TASK, 5000);
22622f6cdcc8SScott Long }
22632f6cdcc8SScott Long
hptiop_get_srb(struct hpt_iop_hba * hba)22642f6cdcc8SScott Long static void *hptiop_get_srb(struct hpt_iop_hba * hba)
22652f6cdcc8SScott Long {
22662f6cdcc8SScott Long struct hpt_iop_srb * srb;
22672f6cdcc8SScott Long
22682f6cdcc8SScott Long if (hba->srb_list) {
22692f6cdcc8SScott Long srb = hba->srb_list;
22702f6cdcc8SScott Long hba->srb_list = srb->next;
22712f6cdcc8SScott Long return srb;
22722f6cdcc8SScott Long }
22732f6cdcc8SScott Long
22742f6cdcc8SScott Long return NULL;
22752f6cdcc8SScott Long }
22762f6cdcc8SScott Long
hptiop_free_srb(struct hpt_iop_hba * hba,struct hpt_iop_srb * srb)22772f6cdcc8SScott Long static void hptiop_free_srb(struct hpt_iop_hba *hba, struct hpt_iop_srb *srb)
22782f6cdcc8SScott Long {
22792f6cdcc8SScott Long srb->next = hba->srb_list;
22802f6cdcc8SScott Long hba->srb_list = srb;
22812f6cdcc8SScott Long }
22822f6cdcc8SScott Long
hptiop_action(struct cam_sim * sim,union ccb * ccb)22832f6cdcc8SScott Long static void hptiop_action(struct cam_sim *sim, union ccb *ccb)
22842f6cdcc8SScott Long {
22852f6cdcc8SScott Long struct hpt_iop_hba * hba = (struct hpt_iop_hba *)cam_sim_softc(sim);
22862f6cdcc8SScott Long struct hpt_iop_srb * srb;
2287dd0b4fb6SKonstantin Belousov int error;
22882f6cdcc8SScott Long
22892f6cdcc8SScott Long switch (ccb->ccb_h.func_code) {
22902f6cdcc8SScott Long
22912f6cdcc8SScott Long case XPT_SCSI_IO:
22922f6cdcc8SScott Long if (ccb->ccb_h.target_lun != 0 ||
22932f6cdcc8SScott Long ccb->ccb_h.target_id >= hba->max_devices ||
22942f6cdcc8SScott Long (ccb->ccb_h.flags & CAM_CDB_PHYS))
22952f6cdcc8SScott Long {
22962f6cdcc8SScott Long ccb->ccb_h.status = CAM_TID_INVALID;
22972f6cdcc8SScott Long xpt_done(ccb);
2298ef588050SJohn Baldwin return;
22992f6cdcc8SScott Long }
23002f6cdcc8SScott Long
23012f6cdcc8SScott Long if ((srb = hptiop_get_srb(hba)) == NULL) {
23022f6cdcc8SScott Long device_printf(hba->pcidev, "srb allocated failed");
23032f6cdcc8SScott Long ccb->ccb_h.status = CAM_REQ_CMP_ERR;
23042f6cdcc8SScott Long xpt_done(ccb);
2305ef588050SJohn Baldwin return;
23062f6cdcc8SScott Long }
23072f6cdcc8SScott Long
23082f6cdcc8SScott Long srb->ccb = ccb;
2309dd0b4fb6SKonstantin Belousov error = bus_dmamap_load_ccb(hba->io_dmat,
23102f6cdcc8SScott Long srb->dma_map,
2311dd0b4fb6SKonstantin Belousov ccb,
23122f6cdcc8SScott Long hptiop_post_scsi_command,
2313dd0b4fb6SKonstantin Belousov srb,
2314dd0b4fb6SKonstantin Belousov 0);
23152f6cdcc8SScott Long
23162f6cdcc8SScott Long if (error && error != EINPROGRESS) {
23172f6cdcc8SScott Long device_printf(hba->pcidev,
23182f6cdcc8SScott Long "%d bus_dmamap_load error %d",
23192f6cdcc8SScott Long hba->pciunit, error);
23202f6cdcc8SScott Long xpt_freeze_simq(hba->sim, 1);
23212f6cdcc8SScott Long ccb->ccb_h.status = CAM_REQ_CMP_ERR;
23222f6cdcc8SScott Long hptiop_free_srb(hba, srb);
23232f6cdcc8SScott Long xpt_done(ccb);
2324ef588050SJohn Baldwin return;
23252f6cdcc8SScott Long }
23262f6cdcc8SScott Long
23272f6cdcc8SScott Long return;
23282f6cdcc8SScott Long
23292f6cdcc8SScott Long case XPT_RESET_BUS:
23302f6cdcc8SScott Long device_printf(hba->pcidev, "reset adapter");
23312f6cdcc8SScott Long hba->msg_done = 0;
23322f6cdcc8SScott Long hptiop_reset_adapter(hba);
23332f6cdcc8SScott Long break;
23342f6cdcc8SScott Long
23352f6cdcc8SScott Long case XPT_GET_TRAN_SETTINGS:
23362f6cdcc8SScott Long case XPT_SET_TRAN_SETTINGS:
23372f6cdcc8SScott Long ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
23382f6cdcc8SScott Long break;
23392f6cdcc8SScott Long
23402f6cdcc8SScott Long case XPT_CALC_GEOMETRY:
2341f3b080e6SMarius Strobl cam_calc_geometry(&ccb->ccg, 1);
23422f6cdcc8SScott Long break;
23432f6cdcc8SScott Long
23442f6cdcc8SScott Long case XPT_PATH_INQ:
23452f6cdcc8SScott Long {
23462f6cdcc8SScott Long struct ccb_pathinq *cpi = &ccb->cpi;
23472f6cdcc8SScott Long
23482f6cdcc8SScott Long cpi->version_num = 1;
23492f6cdcc8SScott Long cpi->hba_inquiry = PI_SDTR_ABLE;
23502f6cdcc8SScott Long cpi->target_sprt = 0;
23512f6cdcc8SScott Long cpi->hba_misc = PIM_NOBUSRESET;
23522f6cdcc8SScott Long cpi->hba_eng_cnt = 0;
23532f6cdcc8SScott Long cpi->max_target = hba->max_devices;
23542f6cdcc8SScott Long cpi->max_lun = 0;
23552f6cdcc8SScott Long cpi->unit_number = cam_sim_unit(sim);
23562f6cdcc8SScott Long cpi->bus_id = cam_sim_bus(sim);
23572f6cdcc8SScott Long cpi->initiator_id = hba->max_devices;
23582f6cdcc8SScott Long cpi->base_transfer_speed = 3300;
23592f6cdcc8SScott Long
23604195c7deSAlan Somers strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
23614195c7deSAlan Somers strlcpy(cpi->hba_vid, "HPT ", HBA_IDLEN);
23624195c7deSAlan Somers strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
236333827b84SAlexander Motin cpi->transport = XPORT_SPI;
236433827b84SAlexander Motin cpi->transport_version = 2;
236533827b84SAlexander Motin cpi->protocol = PROTO_SCSI;
236633827b84SAlexander Motin cpi->protocol_version = SCSI_REV_2;
23672f6cdcc8SScott Long cpi->ccb_h.status = CAM_REQ_CMP;
23682f6cdcc8SScott Long break;
23692f6cdcc8SScott Long }
23702f6cdcc8SScott Long
23712f6cdcc8SScott Long default:
23722f6cdcc8SScott Long ccb->ccb_h.status = CAM_REQ_INVALID;
23732f6cdcc8SScott Long break;
23742f6cdcc8SScott Long }
23752f6cdcc8SScott Long
23762f6cdcc8SScott Long xpt_done(ccb);
23772f6cdcc8SScott Long return;
23782f6cdcc8SScott Long }
23792f6cdcc8SScott Long
hptiop_post_req_itl(struct hpt_iop_hba * hba,struct hpt_iop_srb * srb,bus_dma_segment_t * segs,int nsegs)23802f6cdcc8SScott Long static void hptiop_post_req_itl(struct hpt_iop_hba *hba,
23812f6cdcc8SScott Long struct hpt_iop_srb *srb,
23822f6cdcc8SScott Long bus_dma_segment_t *segs, int nsegs)
23832f6cdcc8SScott Long {
23842f6cdcc8SScott Long int idx;
23852f6cdcc8SScott Long union ccb *ccb = srb->ccb;
23862f6cdcc8SScott Long u_int8_t *cdb;
23872f6cdcc8SScott Long
23882f6cdcc8SScott Long if (ccb->ccb_h.flags & CAM_CDB_POINTER)
23892f6cdcc8SScott Long cdb = ccb->csio.cdb_io.cdb_ptr;
23902f6cdcc8SScott Long else
23912f6cdcc8SScott Long cdb = ccb->csio.cdb_io.cdb_bytes;
23922f6cdcc8SScott Long
23932f6cdcc8SScott Long KdPrint(("ccb=%p %x-%x-%x\n",
23942f6cdcc8SScott Long ccb, *(u_int32_t *)cdb, *((u_int32_t *)cdb+1), *((u_int32_t *)cdb+2)));
23952f6cdcc8SScott Long
23962f6cdcc8SScott Long if (srb->srb_flag & HPT_SRB_FLAG_HIGH_MEM_ACESS) {
23972f6cdcc8SScott Long u_int32_t iop_req32;
23982f6cdcc8SScott Long struct hpt_iop_request_scsi_command req;
23992f6cdcc8SScott Long
24002f6cdcc8SScott Long iop_req32 = BUS_SPACE_RD4_ITL(inbound_queue);
24012f6cdcc8SScott Long
24022f6cdcc8SScott Long if (iop_req32 == IOPMU_QUEUE_EMPTY) {
2403b790c193SPedro F. Giffuni device_printf(hba->pcidev, "invalid req offset\n");
24042f6cdcc8SScott Long ccb->ccb_h.status = CAM_BUSY;
24052f6cdcc8SScott Long bus_dmamap_unload(hba->io_dmat, srb->dma_map);
24062f6cdcc8SScott Long hptiop_free_srb(hba, srb);
24072f6cdcc8SScott Long xpt_done(ccb);
24082f6cdcc8SScott Long return;
24092f6cdcc8SScott Long }
24102f6cdcc8SScott Long
24112f6cdcc8SScott Long if (ccb->csio.dxfer_len && nsegs > 0) {
24122f6cdcc8SScott Long struct hpt_iopsg *psg = req.sg_list;
24132f6cdcc8SScott Long for (idx = 0; idx < nsegs; idx++, psg++) {
24142f6cdcc8SScott Long psg->pci_address = (u_int64_t)segs[idx].ds_addr;
24152f6cdcc8SScott Long psg->size = segs[idx].ds_len;
24162f6cdcc8SScott Long psg->eot = 0;
24172f6cdcc8SScott Long }
24182f6cdcc8SScott Long psg[-1].eot = 1;
24192f6cdcc8SScott Long }
24202f6cdcc8SScott Long
24212f6cdcc8SScott Long bcopy(cdb, req.cdb, ccb->csio.cdb_len);
24222f6cdcc8SScott Long
2423022a7377SXin LI req.header.size =
2424022a7377SXin LI offsetof(struct hpt_iop_request_scsi_command, sg_list)
24252f6cdcc8SScott Long + nsegs*sizeof(struct hpt_iopsg);
24262f6cdcc8SScott Long req.header.type = IOP_REQUEST_TYPE_SCSI_COMMAND;
24272f6cdcc8SScott Long req.header.flags = 0;
24282f6cdcc8SScott Long req.header.result = IOP_RESULT_PENDING;
24292f6cdcc8SScott Long req.header.context = (u_int64_t)(unsigned long)srb;
24302f6cdcc8SScott Long req.dataxfer_length = ccb->csio.dxfer_len;
24312f6cdcc8SScott Long req.channel = 0;
24322f6cdcc8SScott Long req.target = ccb->ccb_h.target_id;
24332f6cdcc8SScott Long req.lun = ccb->ccb_h.target_lun;
24342f6cdcc8SScott Long
24352f6cdcc8SScott Long bus_space_write_region_1(hba->bar0t, hba->bar0h, iop_req32,
24362f6cdcc8SScott Long (u_int8_t *)&req, req.header.size);
24372f6cdcc8SScott Long
24382f6cdcc8SScott Long if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
24392f6cdcc8SScott Long bus_dmamap_sync(hba->io_dmat,
24402f6cdcc8SScott Long srb->dma_map, BUS_DMASYNC_PREREAD);
24412f6cdcc8SScott Long }
24422f6cdcc8SScott Long else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
24432f6cdcc8SScott Long bus_dmamap_sync(hba->io_dmat,
24442f6cdcc8SScott Long srb->dma_map, BUS_DMASYNC_PREWRITE);
24452f6cdcc8SScott Long
24462f6cdcc8SScott Long BUS_SPACE_WRT4_ITL(inbound_queue,iop_req32);
24472f6cdcc8SScott Long } else {
24482f6cdcc8SScott Long struct hpt_iop_request_scsi_command *req;
24492f6cdcc8SScott Long
24502f6cdcc8SScott Long req = (struct hpt_iop_request_scsi_command *)srb;
24512f6cdcc8SScott Long if (ccb->csio.dxfer_len && nsegs > 0) {
24522f6cdcc8SScott Long struct hpt_iopsg *psg = req->sg_list;
24532f6cdcc8SScott Long for (idx = 0; idx < nsegs; idx++, psg++) {
24542f6cdcc8SScott Long psg->pci_address =
24552f6cdcc8SScott Long (u_int64_t)segs[idx].ds_addr;
24562f6cdcc8SScott Long psg->size = segs[idx].ds_len;
24572f6cdcc8SScott Long psg->eot = 0;
24582f6cdcc8SScott Long }
24592f6cdcc8SScott Long psg[-1].eot = 1;
24602f6cdcc8SScott Long }
24612f6cdcc8SScott Long
24622f6cdcc8SScott Long bcopy(cdb, req->cdb, ccb->csio.cdb_len);
24632f6cdcc8SScott Long
24642f6cdcc8SScott Long req->header.type = IOP_REQUEST_TYPE_SCSI_COMMAND;
24652f6cdcc8SScott Long req->header.result = IOP_RESULT_PENDING;
24662f6cdcc8SScott Long req->dataxfer_length = ccb->csio.dxfer_len;
24672f6cdcc8SScott Long req->channel = 0;
24682f6cdcc8SScott Long req->target = ccb->ccb_h.target_id;
24692f6cdcc8SScott Long req->lun = ccb->ccb_h.target_lun;
2470022a7377SXin LI req->header.size =
2471022a7377SXin LI offsetof(struct hpt_iop_request_scsi_command, sg_list)
24722f6cdcc8SScott Long + nsegs*sizeof(struct hpt_iopsg);
24732f6cdcc8SScott Long req->header.context = (u_int64_t)srb->index |
24742f6cdcc8SScott Long IOPMU_QUEUE_ADDR_HOST_BIT;
24752f6cdcc8SScott Long req->header.flags = IOP_REQUEST_FLAG_OUTPUT_CONTEXT;
24762f6cdcc8SScott Long
24772f6cdcc8SScott Long if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
24782f6cdcc8SScott Long bus_dmamap_sync(hba->io_dmat,
24792f6cdcc8SScott Long srb->dma_map, BUS_DMASYNC_PREREAD);
24802f6cdcc8SScott Long }else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
24812f6cdcc8SScott Long bus_dmamap_sync(hba->io_dmat,
24822f6cdcc8SScott Long srb->dma_map, BUS_DMASYNC_PREWRITE);
24832f6cdcc8SScott Long }
24842f6cdcc8SScott Long
24852f6cdcc8SScott Long if (hba->firmware_version > 0x01020000
24862f6cdcc8SScott Long || hba->interface_version > 0x01020000) {
24872f6cdcc8SScott Long u_int32_t size_bits;
24882f6cdcc8SScott Long
24892f6cdcc8SScott Long if (req->header.size < 256)
24902f6cdcc8SScott Long size_bits = IOPMU_QUEUE_REQUEST_SIZE_BIT;
24912f6cdcc8SScott Long else if (req->header.size < 512)
24922f6cdcc8SScott Long size_bits = IOPMU_QUEUE_ADDR_HOST_BIT;
24932f6cdcc8SScott Long else
24942f6cdcc8SScott Long size_bits = IOPMU_QUEUE_REQUEST_SIZE_BIT
24952f6cdcc8SScott Long | IOPMU_QUEUE_ADDR_HOST_BIT;
24962f6cdcc8SScott Long
24972f6cdcc8SScott Long BUS_SPACE_WRT4_ITL(inbound_queue,
24982f6cdcc8SScott Long (u_int32_t)srb->phy_addr | size_bits);
24992f6cdcc8SScott Long } else
25002f6cdcc8SScott Long BUS_SPACE_WRT4_ITL(inbound_queue, (u_int32_t)srb->phy_addr
25012f6cdcc8SScott Long |IOPMU_QUEUE_ADDR_HOST_BIT);
25022f6cdcc8SScott Long }
25032f6cdcc8SScott Long }
25042f6cdcc8SScott Long
hptiop_post_req_mv(struct hpt_iop_hba * hba,struct hpt_iop_srb * srb,bus_dma_segment_t * segs,int nsegs)25052f6cdcc8SScott Long static void hptiop_post_req_mv(struct hpt_iop_hba *hba,
25062f6cdcc8SScott Long struct hpt_iop_srb *srb,
25072f6cdcc8SScott Long bus_dma_segment_t *segs, int nsegs)
25082f6cdcc8SScott Long {
25092f6cdcc8SScott Long int idx, size;
25102f6cdcc8SScott Long union ccb *ccb = srb->ccb;
25112f6cdcc8SScott Long u_int8_t *cdb;
25122f6cdcc8SScott Long struct hpt_iop_request_scsi_command *req;
25132f6cdcc8SScott Long u_int64_t req_phy;
25142f6cdcc8SScott Long
25152f6cdcc8SScott Long req = (struct hpt_iop_request_scsi_command *)srb;
25162f6cdcc8SScott Long req_phy = srb->phy_addr;
25172f6cdcc8SScott Long
25182f6cdcc8SScott Long if (ccb->csio.dxfer_len && nsegs > 0) {
25192f6cdcc8SScott Long struct hpt_iopsg *psg = req->sg_list;
25202f6cdcc8SScott Long for (idx = 0; idx < nsegs; idx++, psg++) {
25212f6cdcc8SScott Long psg->pci_address = (u_int64_t)segs[idx].ds_addr;
25222f6cdcc8SScott Long psg->size = segs[idx].ds_len;
25232f6cdcc8SScott Long psg->eot = 0;
25242f6cdcc8SScott Long }
25252f6cdcc8SScott Long psg[-1].eot = 1;
25262f6cdcc8SScott Long }
25272f6cdcc8SScott Long if (ccb->ccb_h.flags & CAM_CDB_POINTER)
25282f6cdcc8SScott Long cdb = ccb->csio.cdb_io.cdb_ptr;
25292f6cdcc8SScott Long else
25302f6cdcc8SScott Long cdb = ccb->csio.cdb_io.cdb_bytes;
25312f6cdcc8SScott Long
25322f6cdcc8SScott Long bcopy(cdb, req->cdb, ccb->csio.cdb_len);
25332f6cdcc8SScott Long req->header.type = IOP_REQUEST_TYPE_SCSI_COMMAND;
25342f6cdcc8SScott Long req->header.result = IOP_RESULT_PENDING;
25352f6cdcc8SScott Long req->dataxfer_length = ccb->csio.dxfer_len;
25362f6cdcc8SScott Long req->channel = 0;
25372f6cdcc8SScott Long req->target = ccb->ccb_h.target_id;
25382f6cdcc8SScott Long req->lun = ccb->ccb_h.target_lun;
25392f6cdcc8SScott Long req->header.size = sizeof(struct hpt_iop_request_scsi_command)
25402f6cdcc8SScott Long - sizeof(struct hpt_iopsg)
25412f6cdcc8SScott Long + nsegs * sizeof(struct hpt_iopsg);
25422f6cdcc8SScott Long if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
25432f6cdcc8SScott Long bus_dmamap_sync(hba->io_dmat,
25442f6cdcc8SScott Long srb->dma_map, BUS_DMASYNC_PREREAD);
25452f6cdcc8SScott Long }
25462f6cdcc8SScott Long else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
25472f6cdcc8SScott Long bus_dmamap_sync(hba->io_dmat,
25482f6cdcc8SScott Long srb->dma_map, BUS_DMASYNC_PREWRITE);
25492f6cdcc8SScott Long req->header.context = (u_int64_t)srb->index
25502f6cdcc8SScott Long << MVIOP_REQUEST_NUMBER_START_BIT
25512f6cdcc8SScott Long | MVIOP_CMD_TYPE_SCSI;
25522f6cdcc8SScott Long req->header.flags = IOP_REQUEST_FLAG_OUTPUT_CONTEXT;
25532f6cdcc8SScott Long size = req->header.size >> 8;
25542f6cdcc8SScott Long hptiop_mv_inbound_write(req_phy
25552f6cdcc8SScott Long | MVIOP_MU_QUEUE_ADDR_HOST_BIT
2556b3956646SMarcelo Araujo | imin(3, size), hba);
25572f6cdcc8SScott Long }
25582f6cdcc8SScott Long
hptiop_post_req_mvfrey(struct hpt_iop_hba * hba,struct hpt_iop_srb * srb,bus_dma_segment_t * segs,int nsegs)2559022a7377SXin LI static void hptiop_post_req_mvfrey(struct hpt_iop_hba *hba,
2560022a7377SXin LI struct hpt_iop_srb *srb,
2561022a7377SXin LI bus_dma_segment_t *segs, int nsegs)
2562022a7377SXin LI {
2563022a7377SXin LI int idx, index;
2564022a7377SXin LI union ccb *ccb = srb->ccb;
2565022a7377SXin LI u_int8_t *cdb;
2566022a7377SXin LI struct hpt_iop_request_scsi_command *req;
2567022a7377SXin LI u_int64_t req_phy;
2568022a7377SXin LI
2569022a7377SXin LI req = (struct hpt_iop_request_scsi_command *)srb;
2570022a7377SXin LI req_phy = srb->phy_addr;
2571022a7377SXin LI
2572022a7377SXin LI if (ccb->csio.dxfer_len && nsegs > 0) {
2573022a7377SXin LI struct hpt_iopsg *psg = req->sg_list;
2574022a7377SXin LI for (idx = 0; idx < nsegs; idx++, psg++) {
2575022a7377SXin LI psg->pci_address = (u_int64_t)segs[idx].ds_addr | 1;
2576022a7377SXin LI psg->size = segs[idx].ds_len;
2577022a7377SXin LI psg->eot = 0;
2578022a7377SXin LI }
2579022a7377SXin LI psg[-1].eot = 1;
2580022a7377SXin LI }
2581022a7377SXin LI if (ccb->ccb_h.flags & CAM_CDB_POINTER)
2582022a7377SXin LI cdb = ccb->csio.cdb_io.cdb_ptr;
2583022a7377SXin LI else
2584022a7377SXin LI cdb = ccb->csio.cdb_io.cdb_bytes;
2585022a7377SXin LI
2586022a7377SXin LI bcopy(cdb, req->cdb, ccb->csio.cdb_len);
2587022a7377SXin LI req->header.type = IOP_REQUEST_TYPE_SCSI_COMMAND;
2588022a7377SXin LI req->header.result = IOP_RESULT_PENDING;
2589022a7377SXin LI req->dataxfer_length = ccb->csio.dxfer_len;
2590022a7377SXin LI req->channel = 0;
2591022a7377SXin LI req->target = ccb->ccb_h.target_id;
2592022a7377SXin LI req->lun = ccb->ccb_h.target_lun;
2593022a7377SXin LI req->header.size = sizeof(struct hpt_iop_request_scsi_command)
2594022a7377SXin LI - sizeof(struct hpt_iopsg)
2595022a7377SXin LI + nsegs * sizeof(struct hpt_iopsg);
2596022a7377SXin LI if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2597022a7377SXin LI bus_dmamap_sync(hba->io_dmat,
2598022a7377SXin LI srb->dma_map, BUS_DMASYNC_PREREAD);
2599022a7377SXin LI }
2600022a7377SXin LI else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
2601022a7377SXin LI bus_dmamap_sync(hba->io_dmat,
2602022a7377SXin LI srb->dma_map, BUS_DMASYNC_PREWRITE);
2603022a7377SXin LI
2604022a7377SXin LI req->header.flags = IOP_REQUEST_FLAG_OUTPUT_CONTEXT
2605022a7377SXin LI | IOP_REQUEST_FLAG_ADDR_BITS
2606022a7377SXin LI | ((req_phy >> 16) & 0xffff0000);
2607022a7377SXin LI req->header.context = ((req_phy & 0xffffffff) << 32 )
2608022a7377SXin LI | srb->index << 4
2609022a7377SXin LI | IOPMU_QUEUE_ADDR_HOST_BIT | req->header.type;
2610022a7377SXin LI
2611022a7377SXin LI hba->u.mvfrey.inlist_wptr++;
2612022a7377SXin LI index = hba->u.mvfrey.inlist_wptr & 0x3fff;
2613022a7377SXin LI
2614022a7377SXin LI if (index == hba->u.mvfrey.list_count) {
2615022a7377SXin LI index = 0;
2616022a7377SXin LI hba->u.mvfrey.inlist_wptr &= ~0x3fff;
2617022a7377SXin LI hba->u.mvfrey.inlist_wptr ^= CL_POINTER_TOGGLE;
2618022a7377SXin LI }
2619022a7377SXin LI
2620022a7377SXin LI hba->u.mvfrey.inlist[index].addr = req_phy;
2621022a7377SXin LI hba->u.mvfrey.inlist[index].intrfc_len = (req->header.size + 3) / 4;
2622022a7377SXin LI
2623022a7377SXin LI BUS_SPACE_WRT4_MVFREY2(inbound_write_ptr, hba->u.mvfrey.inlist_wptr);
2624022a7377SXin LI BUS_SPACE_RD4_MVFREY2(inbound_write_ptr);
2625022a7377SXin LI
2626022a7377SXin LI if (req->header.type == IOP_REQUEST_TYPE_SCSI_COMMAND) {
2627ef588050SJohn Baldwin callout_reset(&srb->timeout, 20 * hz, hptiop_reset_adapter, hba);
2628022a7377SXin LI }
2629022a7377SXin LI }
2630022a7377SXin LI
hptiop_post_scsi_command(void * arg,bus_dma_segment_t * segs,int nsegs,int error)26312f6cdcc8SScott Long static void hptiop_post_scsi_command(void *arg, bus_dma_segment_t *segs,
26322f6cdcc8SScott Long int nsegs, int error)
26332f6cdcc8SScott Long {
26342f6cdcc8SScott Long struct hpt_iop_srb *srb = (struct hpt_iop_srb *)arg;
26352f6cdcc8SScott Long union ccb *ccb = srb->ccb;
26362f6cdcc8SScott Long struct hpt_iop_hba *hba = srb->hba;
26372f6cdcc8SScott Long
26382f6cdcc8SScott Long if (error || nsegs > hba->max_sg_count) {
2639123055f0SNathan Whitehorn KdPrint(("hptiop: func_code=%x tid=%x lun=%jx nsegs=%d\n",
26402f6cdcc8SScott Long ccb->ccb_h.func_code,
26412f6cdcc8SScott Long ccb->ccb_h.target_id,
2642123055f0SNathan Whitehorn (uintmax_t)ccb->ccb_h.target_lun, nsegs));
26432f6cdcc8SScott Long ccb->ccb_h.status = CAM_BUSY;
26442f6cdcc8SScott Long bus_dmamap_unload(hba->io_dmat, srb->dma_map);
26452f6cdcc8SScott Long hptiop_free_srb(hba, srb);
26462f6cdcc8SScott Long xpt_done(ccb);
26472f6cdcc8SScott Long return;
26482f6cdcc8SScott Long }
26492f6cdcc8SScott Long
26502f6cdcc8SScott Long hba->ops->post_req(hba, srb, segs, nsegs);
26512f6cdcc8SScott Long }
26522f6cdcc8SScott Long
hptiop_mv_map_ctlcfg(void * arg,bus_dma_segment_t * segs,int nsegs,int error)26532f6cdcc8SScott Long static void hptiop_mv_map_ctlcfg(void *arg, bus_dma_segment_t *segs,
26542f6cdcc8SScott Long int nsegs, int error)
26552f6cdcc8SScott Long {
26562f6cdcc8SScott Long struct hpt_iop_hba *hba = (struct hpt_iop_hba *)arg;
26572f6cdcc8SScott Long hba->ctlcfgcmd_phy = ((u_int64_t)segs->ds_addr + 0x1F)
26582f6cdcc8SScott Long & ~(u_int64_t)0x1F;
26592f6cdcc8SScott Long hba->ctlcfg_ptr = (u_int8_t *)(((unsigned long)hba->ctlcfg_ptr + 0x1F)
26602f6cdcc8SScott Long & ~0x1F);
26612f6cdcc8SScott Long }
26622f6cdcc8SScott Long
hptiop_mvfrey_map_ctlcfg(void * arg,bus_dma_segment_t * segs,int nsegs,int error)2663022a7377SXin LI static void hptiop_mvfrey_map_ctlcfg(void *arg, bus_dma_segment_t *segs,
2664022a7377SXin LI int nsegs, int error)
2665022a7377SXin LI {
2666022a7377SXin LI struct hpt_iop_hba *hba = (struct hpt_iop_hba *)arg;
2667022a7377SXin LI char *p;
2668022a7377SXin LI u_int64_t phy;
2669022a7377SXin LI u_int32_t list_count = hba->u.mvfrey.list_count;
2670022a7377SXin LI
2671022a7377SXin LI phy = ((u_int64_t)segs->ds_addr + 0x1F)
2672022a7377SXin LI & ~(u_int64_t)0x1F;
2673022a7377SXin LI p = (u_int8_t *)(((unsigned long)hba->ctlcfg_ptr + 0x1F)
2674022a7377SXin LI & ~0x1F);
2675022a7377SXin LI
2676022a7377SXin LI hba->ctlcfgcmd_phy = phy;
2677022a7377SXin LI hba->ctlcfg_ptr = p;
2678022a7377SXin LI
2679022a7377SXin LI p += 0x800;
2680022a7377SXin LI phy += 0x800;
2681022a7377SXin LI
2682022a7377SXin LI hba->u.mvfrey.inlist = (struct mvfrey_inlist_entry *)p;
2683022a7377SXin LI hba->u.mvfrey.inlist_phy = phy;
2684022a7377SXin LI
2685022a7377SXin LI p += list_count * sizeof(struct mvfrey_inlist_entry);
2686022a7377SXin LI phy += list_count * sizeof(struct mvfrey_inlist_entry);
2687022a7377SXin LI
2688022a7377SXin LI hba->u.mvfrey.outlist = (struct mvfrey_outlist_entry *)p;
2689022a7377SXin LI hba->u.mvfrey.outlist_phy = phy;
2690022a7377SXin LI
2691022a7377SXin LI p += list_count * sizeof(struct mvfrey_outlist_entry);
2692022a7377SXin LI phy += list_count * sizeof(struct mvfrey_outlist_entry);
2693022a7377SXin LI
2694022a7377SXin LI hba->u.mvfrey.outlist_cptr = (u_int32_t *)p;
2695022a7377SXin LI hba->u.mvfrey.outlist_cptr_phy = phy;
2696022a7377SXin LI }
2697022a7377SXin LI
hptiop_map_srb(void * arg,bus_dma_segment_t * segs,int nsegs,int error)26984439f8b4SScott Long static void hptiop_map_srb(void *arg, bus_dma_segment_t *segs,
26994439f8b4SScott Long int nsegs, int error)
27004439f8b4SScott Long {
27014439f8b4SScott Long struct hpt_iop_hba * hba = (struct hpt_iop_hba *)arg;
27024439f8b4SScott Long bus_addr_t phy_addr = (segs->ds_addr + 0x1F) & ~(bus_addr_t)0x1F;
27034439f8b4SScott Long struct hpt_iop_srb *srb, *tmp_srb;
27044439f8b4SScott Long int i;
27054439f8b4SScott Long
27064439f8b4SScott Long if (error || nsegs == 0) {
27072f6cdcc8SScott Long device_printf(hba->pcidev, "hptiop_map_srb error");
27084439f8b4SScott Long return;
27094439f8b4SScott Long }
27104439f8b4SScott Long
27114439f8b4SScott Long /* map srb */
27124439f8b4SScott Long srb = (struct hpt_iop_srb *)
27132f6cdcc8SScott Long (((unsigned long)hba->uncached_ptr + 0x1F)
27142f6cdcc8SScott Long & ~(unsigned long)0x1F);
27154439f8b4SScott Long
27164439f8b4SScott Long for (i = 0; i < HPT_SRB_MAX_QUEUE_SIZE; i++) {
27174439f8b4SScott Long tmp_srb = (struct hpt_iop_srb *)
27184439f8b4SScott Long ((char *)srb + i * HPT_SRB_MAX_SIZE);
27194439f8b4SScott Long if (((unsigned long)tmp_srb & 0x1F) == 0) {
27204439f8b4SScott Long if (bus_dmamap_create(hba->io_dmat,
27214439f8b4SScott Long 0, &tmp_srb->dma_map)) {
27222f6cdcc8SScott Long device_printf(hba->pcidev, "dmamap create failed");
27234439f8b4SScott Long return;
27244439f8b4SScott Long }
27254439f8b4SScott Long
27264439f8b4SScott Long bzero(tmp_srb, sizeof(struct hpt_iop_srb));
27274439f8b4SScott Long tmp_srb->hba = hba;
27284439f8b4SScott Long tmp_srb->index = i;
27292f6cdcc8SScott Long if (hba->ctlcfg_ptr == 0) {/*itl iop*/
27302f6cdcc8SScott Long tmp_srb->phy_addr = (u_int64_t)(u_int32_t)
27312f6cdcc8SScott Long (phy_addr >> 5);
27324439f8b4SScott Long if (phy_addr & IOPMU_MAX_MEM_SUPPORT_MASK_32G)
27332f6cdcc8SScott Long tmp_srb->srb_flag =
27342f6cdcc8SScott Long HPT_SRB_FLAG_HIGH_MEM_ACESS;
27352f6cdcc8SScott Long } else {
27362f6cdcc8SScott Long tmp_srb->phy_addr = phy_addr;
27372f6cdcc8SScott Long }
27382f6cdcc8SScott Long
2739ef588050SJohn Baldwin callout_init_mtx(&tmp_srb->timeout, &hba->lock, 0);
27404439f8b4SScott Long hptiop_free_srb(hba, tmp_srb);
27414439f8b4SScott Long hba->srb[i] = tmp_srb;
27424439f8b4SScott Long phy_addr += HPT_SRB_MAX_SIZE;
27434439f8b4SScott Long }
27444439f8b4SScott Long else {
27452f6cdcc8SScott Long device_printf(hba->pcidev, "invalid alignment");
27464439f8b4SScott Long return;
27474439f8b4SScott Long }
27484439f8b4SScott Long }
27494439f8b4SScott Long }
27504439f8b4SScott Long
hptiop_os_message_callback(struct hpt_iop_hba * hba,u_int32_t msg)27512f6cdcc8SScott Long static void hptiop_os_message_callback(struct hpt_iop_hba * hba, u_int32_t msg)
27524439f8b4SScott Long {
27534439f8b4SScott Long hba->msg_done = 1;
27544439f8b4SScott Long }
27554439f8b4SScott Long
hptiop_os_query_remove_device(struct hpt_iop_hba * hba,int target_id)27562f6cdcc8SScott Long static int hptiop_os_query_remove_device(struct hpt_iop_hba * hba,
27572f6cdcc8SScott Long int target_id)
27584439f8b4SScott Long {
27594439f8b4SScott Long struct cam_periph *periph = NULL;
27604439f8b4SScott Long struct cam_path *path;
27614439f8b4SScott Long int status, retval = 0;
27624439f8b4SScott Long
27634439f8b4SScott Long status = xpt_create_path(&path, NULL, hba->sim->path_id, target_id, 0);
27644439f8b4SScott Long
27654439f8b4SScott Long if (status == CAM_REQ_CMP) {
27664439f8b4SScott Long if ((periph = cam_periph_find(path, "da")) != NULL) {
27674439f8b4SScott Long if (periph->refcount >= 1) {
27682f6cdcc8SScott Long device_printf(hba->pcidev, "%d ,"
27692f6cdcc8SScott Long "target_id=0x%x,"
27702f6cdcc8SScott Long "refcount=%d",
27714439f8b4SScott Long hba->pciunit, target_id, periph->refcount);
27724439f8b4SScott Long retval = -1;
27734439f8b4SScott Long }
27744439f8b4SScott Long }
27754439f8b4SScott Long xpt_free_path(path);
27764439f8b4SScott Long }
27774439f8b4SScott Long return retval;
27784439f8b4SScott Long }
27794439f8b4SScott Long
hptiop_release_resource(struct hpt_iop_hba * hba)27804439f8b4SScott Long static void hptiop_release_resource(struct hpt_iop_hba *hba)
27814439f8b4SScott Long {
27822f6cdcc8SScott Long int i;
2783ef588050SJohn Baldwin
2784ef588050SJohn Baldwin if (hba->ioctl_dev)
2785ef588050SJohn Baldwin destroy_dev(hba->ioctl_dev);
2786ef588050SJohn Baldwin
27872f6cdcc8SScott Long if (hba->path) {
27884439f8b4SScott Long struct ccb_setasync ccb;
27894439f8b4SScott Long
27908dc96b74SEdward Tomasz Napierala memset(&ccb, 0, sizeof(ccb));
27914439f8b4SScott Long xpt_setup_ccb(&ccb.ccb_h, hba->path, /*priority*/5);
27924439f8b4SScott Long ccb.ccb_h.func_code = XPT_SASYNC_CB;
27934439f8b4SScott Long ccb.event_enable = 0;
27944439f8b4SScott Long ccb.callback = hptiop_async;
27954439f8b4SScott Long ccb.callback_arg = hba->sim;
27964439f8b4SScott Long xpt_action((union ccb *)&ccb);
27974439f8b4SScott Long xpt_free_path(hba->path);
27984439f8b4SScott Long }
27994439f8b4SScott Long
2800ef588050SJohn Baldwin if (hba->irq_handle)
2801ef588050SJohn Baldwin bus_teardown_intr(hba->pcidev, hba->irq_res, hba->irq_handle);
2802ef588050SJohn Baldwin
28034439f8b4SScott Long if (hba->sim) {
2804ef588050SJohn Baldwin hptiop_lock_adapter(hba);
28054439f8b4SScott Long xpt_bus_deregister(cam_sim_path(hba->sim));
28064439f8b4SScott Long cam_sim_free(hba->sim, TRUE);
2807ef588050SJohn Baldwin hptiop_unlock_adapter(hba);
28084439f8b4SScott Long }
28094439f8b4SScott Long
28102f6cdcc8SScott Long if (hba->ctlcfg_dmat) {
28112f6cdcc8SScott Long bus_dmamap_unload(hba->ctlcfg_dmat, hba->ctlcfg_dmamap);
28122f6cdcc8SScott Long bus_dmamem_free(hba->ctlcfg_dmat,
28132f6cdcc8SScott Long hba->ctlcfg_ptr, hba->ctlcfg_dmamap);
28142f6cdcc8SScott Long bus_dma_tag_destroy(hba->ctlcfg_dmat);
28152f6cdcc8SScott Long }
28162f6cdcc8SScott Long
28172f6cdcc8SScott Long for (i = 0; i < HPT_SRB_MAX_QUEUE_SIZE; i++) {
28182f6cdcc8SScott Long struct hpt_iop_srb *srb = hba->srb[i];
28192f6cdcc8SScott Long if (srb->dma_map)
28202f6cdcc8SScott Long bus_dmamap_destroy(hba->io_dmat, srb->dma_map);
2821ef588050SJohn Baldwin callout_drain(&srb->timeout);
28222f6cdcc8SScott Long }
28232f6cdcc8SScott Long
28244439f8b4SScott Long if (hba->srb_dmat) {
28254439f8b4SScott Long bus_dmamap_unload(hba->srb_dmat, hba->srb_dmamap);
28262f6cdcc8SScott Long bus_dmamap_destroy(hba->srb_dmat, hba->srb_dmamap);
28274439f8b4SScott Long bus_dma_tag_destroy(hba->srb_dmat);
28284439f8b4SScott Long }
28294439f8b4SScott Long
28304439f8b4SScott Long if (hba->io_dmat)
28314439f8b4SScott Long bus_dma_tag_destroy(hba->io_dmat);
28324439f8b4SScott Long
28334439f8b4SScott Long if (hba->parent_dmat)
28344439f8b4SScott Long bus_dma_tag_destroy(hba->parent_dmat);
28354439f8b4SScott Long
28364439f8b4SScott Long if (hba->irq_res)
28372f6cdcc8SScott Long bus_release_resource(hba->pcidev, SYS_RES_IRQ,
28382f6cdcc8SScott Long 0, hba->irq_res);
28394439f8b4SScott Long
28404439f8b4SScott Long if (hba->bar0_res)
28414439f8b4SScott Long bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
28424439f8b4SScott Long hba->bar0_rid, hba->bar0_res);
28432f6cdcc8SScott Long if (hba->bar2_res)
28442f6cdcc8SScott Long bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
28452f6cdcc8SScott Long hba->bar2_rid, hba->bar2_res);
2846ef588050SJohn Baldwin mtx_destroy(&hba->lock);
28474439f8b4SScott Long }
2848