165adb54cSMatt Jacob /* $FreeBSD$ */ 265adb54cSMatt Jacob /* 365adb54cSMatt Jacob * PCI specific probe and attach routines for Qlogic ISP SCSI adapters. 465adb54cSMatt Jacob * FreeBSD Version. 565adb54cSMatt Jacob * 665adb54cSMatt Jacob *--------------------------------------- 765adb54cSMatt Jacob * Copyright (c) 1997, 1998 by Matthew Jacob 865adb54cSMatt Jacob * NASA/Ames Research Center 965adb54cSMatt Jacob * All rights reserved. 1065adb54cSMatt Jacob *--------------------------------------- 1165adb54cSMatt Jacob * 1265adb54cSMatt Jacob * Redistribution and use in source and binary forms, with or without 1365adb54cSMatt Jacob * modification, are permitted provided that the following conditions 1465adb54cSMatt Jacob * are met: 1565adb54cSMatt Jacob * 1. Redistributions of source code must retain the above copyright 1665adb54cSMatt Jacob * notice immediately at the beginning of the file, without modification, 1765adb54cSMatt Jacob * this list of conditions, and the following disclaimer. 1865adb54cSMatt Jacob * 2. Redistributions in binary form must reproduce the above copyright 1965adb54cSMatt Jacob * notice, this list of conditions and the following disclaimer in the 2065adb54cSMatt Jacob * documentation and/or other materials provided with the distribution. 2165adb54cSMatt Jacob * 3. The name of the author may not be used to endorse or promote products 2265adb54cSMatt Jacob * derived from this software without specific prior written permission. 2365adb54cSMatt Jacob * 2465adb54cSMatt Jacob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 2565adb54cSMatt Jacob * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2665adb54cSMatt Jacob * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2765adb54cSMatt Jacob * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 2865adb54cSMatt Jacob * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2965adb54cSMatt Jacob * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3065adb54cSMatt Jacob * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3165adb54cSMatt Jacob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3265adb54cSMatt Jacob * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3365adb54cSMatt Jacob * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3465adb54cSMatt Jacob * SUCH DAMAGE. 3565adb54cSMatt Jacob */ 3665adb54cSMatt Jacob #include <dev/isp/isp_freebsd.h> 3765adb54cSMatt Jacob #include <dev/isp/asm_pci.h> 38d720e6d5SJustin T. Gibbs #include <sys/malloc.h> 39d720e6d5SJustin T. Gibbs #include <vm/vm.h> 40d720e6d5SJustin T. Gibbs #include <vm/pmap.h> 41d720e6d5SJustin T. Gibbs #include <vm/vm_extern.h> 42d720e6d5SJustin T. Gibbs 4365adb54cSMatt Jacob 4465adb54cSMatt Jacob #include <pci/pcireg.h> 4565adb54cSMatt Jacob #include <pci/pcivar.h> 4665adb54cSMatt Jacob 473dd37e43SMatt Jacob #if __FreeBSD_version >= 300004 48d720e6d5SJustin T. Gibbs #include <machine/bus_memio.h> 49d720e6d5SJustin T. Gibbs #include <machine/bus_pio.h> 50d720e6d5SJustin T. Gibbs #include <machine/bus.h> 51d720e6d5SJustin T. Gibbs #endif 52d720e6d5SJustin T. Gibbs 5365adb54cSMatt Jacob static u_int16_t isp_pci_rd_reg __P((struct ispsoftc *, int)); 5465adb54cSMatt Jacob static void isp_pci_wr_reg __P((struct ispsoftc *, int, u_int16_t)); 5565adb54cSMatt Jacob static int isp_pci_mbxdma __P((struct ispsoftc *)); 5665adb54cSMatt Jacob static int isp_pci_dmasetup __P((struct ispsoftc *, ISP_SCSI_XFER_T *, 5765adb54cSMatt Jacob ispreq_t *, u_int8_t *, u_int8_t)); 583dd37e43SMatt Jacob #if __FreeBSD_version >= 300004 59d720e6d5SJustin T. Gibbs static void 60d720e6d5SJustin T. Gibbs isp_pci_dmateardown __P((struct ispsoftc *, ISP_SCSI_XFER_T *, u_int32_t)); 61d720e6d5SJustin T. Gibbs #else 62d720e6d5SJustin T. Gibbs #define isp_pci_dmateardown NULL 63d720e6d5SJustin T. Gibbs #endif 6465adb54cSMatt Jacob 6565adb54cSMatt Jacob static void isp_pci_reset1 __P((struct ispsoftc *)); 6665adb54cSMatt Jacob static void isp_pci_dumpregs __P((struct ispsoftc *)); 6765adb54cSMatt Jacob 6865adb54cSMatt Jacob static struct ispmdvec mdvec = { 6965adb54cSMatt Jacob isp_pci_rd_reg, 7065adb54cSMatt Jacob isp_pci_wr_reg, 7165adb54cSMatt Jacob isp_pci_mbxdma, 7265adb54cSMatt Jacob isp_pci_dmasetup, 73d720e6d5SJustin T. Gibbs isp_pci_dmateardown, 7465adb54cSMatt Jacob NULL, 7565adb54cSMatt Jacob isp_pci_reset1, 7665adb54cSMatt Jacob isp_pci_dumpregs, 7765adb54cSMatt Jacob ISP_RISC_CODE, 7865adb54cSMatt Jacob ISP_CODE_LENGTH, 7965adb54cSMatt Jacob ISP_CODE_ORG, 8065adb54cSMatt Jacob ISP_CODE_VERSION, 81d720e6d5SJustin T. Gibbs BIU_BURST_ENABLE, 82d720e6d5SJustin T. Gibbs 0 8365adb54cSMatt Jacob }; 8465adb54cSMatt Jacob 8565adb54cSMatt Jacob static struct ispmdvec mdvec_2100 = { 8665adb54cSMatt Jacob isp_pci_rd_reg, 8765adb54cSMatt Jacob isp_pci_wr_reg, 8865adb54cSMatt Jacob isp_pci_mbxdma, 8965adb54cSMatt Jacob isp_pci_dmasetup, 90d720e6d5SJustin T. Gibbs isp_pci_dmateardown, 9165adb54cSMatt Jacob NULL, 9265adb54cSMatt Jacob isp_pci_reset1, 9365adb54cSMatt Jacob isp_pci_dumpregs, 9465adb54cSMatt Jacob ISP2100_RISC_CODE, 9565adb54cSMatt Jacob ISP2100_CODE_LENGTH, 9665adb54cSMatt Jacob ISP2100_CODE_ORG, 9765adb54cSMatt Jacob ISP2100_CODE_VERSION, 98d720e6d5SJustin T. Gibbs BIU_BURST_ENABLE, 99d720e6d5SJustin T. Gibbs 0 10065adb54cSMatt Jacob }; 10165adb54cSMatt Jacob 10265adb54cSMatt Jacob #ifndef PCIM_CMD_INVEN 10365adb54cSMatt Jacob #define PCIM_CMD_INVEN 0x10 10465adb54cSMatt Jacob #endif 10565adb54cSMatt Jacob #ifndef PCIM_CMD_BUSMASTEREN 10665adb54cSMatt Jacob #define PCIM_CMD_BUSMASTEREN 0x0004 10765adb54cSMatt Jacob #endif 10865adb54cSMatt Jacob 10965adb54cSMatt Jacob #ifndef PCI_VENDOR_QLOGIC 11065adb54cSMatt Jacob #define PCI_VENDOR_QLOGIC 0x1077 11165adb54cSMatt Jacob #endif 11265adb54cSMatt Jacob 11365adb54cSMatt Jacob #ifndef PCI_PRODUCT_QLOGIC_ISP1020 11465adb54cSMatt Jacob #define PCI_PRODUCT_QLOGIC_ISP1020 0x1020 11565adb54cSMatt Jacob #endif 11665adb54cSMatt Jacob 11765adb54cSMatt Jacob #define PCI_QLOGIC_ISP \ 11865adb54cSMatt Jacob ((PCI_PRODUCT_QLOGIC_ISP1020 << 16) | PCI_VENDOR_QLOGIC) 11965adb54cSMatt Jacob 12065adb54cSMatt Jacob #ifndef PCI_PRODUCT_QLOGIC_ISP2100 12165adb54cSMatt Jacob #define PCI_PRODUCT_QLOGIC_ISP2100 0x2100 12265adb54cSMatt Jacob #endif 12365adb54cSMatt Jacob 12465adb54cSMatt Jacob #define PCI_QLOGIC_ISP2100 \ 12565adb54cSMatt Jacob ((PCI_PRODUCT_QLOGIC_ISP2100 << 16) | PCI_VENDOR_QLOGIC) 12665adb54cSMatt Jacob 12765adb54cSMatt Jacob #define IO_MAP_REG 0x10 12865adb54cSMatt Jacob #define MEM_MAP_REG 0x14 12965adb54cSMatt Jacob 13065adb54cSMatt Jacob 13165adb54cSMatt Jacob static char *isp_pci_probe __P((pcici_t tag, pcidi_t type)); 13265adb54cSMatt Jacob static void isp_pci_attach __P((pcici_t config_d, int unit)); 13365adb54cSMatt Jacob 134d720e6d5SJustin T. Gibbs /* This distinguishing define is not right, but it does work */ 13565adb54cSMatt Jacob 1363dd37e43SMatt Jacob #if __FreeBSD_version < 300004 137a185f9b1SMatt Jacob #define IO_SPACE_MAPPING 0 138a185f9b1SMatt Jacob #define MEM_SPACE_MAPPING 1 13965adb54cSMatt Jacob typedef int bus_space_tag_t; 14065adb54cSMatt Jacob typedef u_long bus_space_handle_t; 141a185f9b1SMatt Jacob #ifdef __alpha__ 14265adb54cSMatt Jacob #define bus_space_read_2(st, sh, offset) \ 143a185f9b1SMatt Jacob (st == IO_SPACE_MAPPING)? \ 144a185f9b1SMatt Jacob inw((pci_port_t)sh + offset) : readw((pci_port_t)sh + offset) 14565adb54cSMatt Jacob #define bus_space_write_2(st, sh, offset, val) \ 146a185f9b1SMatt Jacob if (st == IO_SPACE_MAPPING) outw((pci_port_t)sh + offset, val); else \ 147a185f9b1SMatt Jacob writew((pci_port_t)sh + offset, val) 148a185f9b1SMatt Jacob #else 149a185f9b1SMatt Jacob #define bus_space_read_2(st, sh, offset) \ 150a185f9b1SMatt Jacob (st == IO_SPACE_MAPPING)? \ 151a185f9b1SMatt Jacob inw((pci_port_t)sh + offset) : *((u_int16_t *)(uintptr_t)sh) 152a185f9b1SMatt Jacob #define bus_space_write_2(st, sh, offset, val) \ 153a185f9b1SMatt Jacob if (st == IO_SPACE_MAPPING) outw((pci_port_t)sh + offset, val); else \ 154b97fc948SBruce Evans *((u_int16_t *)(uintptr_t)sh) = val 155d720e6d5SJustin T. Gibbs #endif 156a185f9b1SMatt Jacob #else 157a185f9b1SMatt Jacob #ifdef __alpha__ 158a185f9b1SMatt Jacob #define IO_SPACE_MAPPING ALPHA_BUS_SPACE_IO 159a185f9b1SMatt Jacob #define MEM_SPACE_MAPPING ALPHA_BUS_SPACE_MEM 160a185f9b1SMatt Jacob #else 161a185f9b1SMatt Jacob #define IO_SPACE_MAPPING I386_BUS_SPACE_IO 162a185f9b1SMatt Jacob #define MEM_SPACE_MAPPING I386_BUS_SPACE_MEM 163a185f9b1SMatt Jacob #endif 164a185f9b1SMatt Jacob #endif 16565adb54cSMatt Jacob 16665adb54cSMatt Jacob struct isp_pcisoftc { 16765adb54cSMatt Jacob struct ispsoftc pci_isp; 16865adb54cSMatt Jacob pcici_t pci_id; 16965adb54cSMatt Jacob bus_space_tag_t pci_st; 17065adb54cSMatt Jacob bus_space_handle_t pci_sh; 1713dd37e43SMatt Jacob #if __FreeBSD_version >= 300004 172d720e6d5SJustin T. Gibbs bus_dma_tag_t parent_dmat; 173d720e6d5SJustin T. Gibbs bus_dma_tag_t cntrol_dmat; 174d720e6d5SJustin T. Gibbs bus_dmamap_t cntrol_dmap; 175d720e6d5SJustin T. Gibbs bus_dmamap_t dmaps[MAXISPREQUEST]; 176d720e6d5SJustin T. Gibbs #endif 17765adb54cSMatt Jacob union { 17865adb54cSMatt Jacob sdparam _x; 17965adb54cSMatt Jacob struct { 18065adb54cSMatt Jacob fcparam _a; 18165adb54cSMatt Jacob char _b[ISP2100_SCRLEN]; 18265adb54cSMatt Jacob } _y; 18365adb54cSMatt Jacob } _z; 18465adb54cSMatt Jacob }; 18565adb54cSMatt Jacob 186d720e6d5SJustin T. Gibbs static u_long ispunit; 18765adb54cSMatt Jacob 18865adb54cSMatt Jacob struct pci_device isp_pci_driver = { 18965adb54cSMatt Jacob "isp", 19065adb54cSMatt Jacob isp_pci_probe, 19165adb54cSMatt Jacob isp_pci_attach, 192d720e6d5SJustin T. Gibbs &ispunit, 19365adb54cSMatt Jacob NULL 19465adb54cSMatt Jacob }; 19565adb54cSMatt Jacob DATA_SET (pcidevice_set, isp_pci_driver); 19665adb54cSMatt Jacob 19765adb54cSMatt Jacob 19865adb54cSMatt Jacob static char * 19965adb54cSMatt Jacob isp_pci_probe(tag, type) 20065adb54cSMatt Jacob pcici_t tag; 20165adb54cSMatt Jacob pcidi_t type; 20265adb54cSMatt Jacob { 20365adb54cSMatt Jacob static int oneshot = 1; 20465adb54cSMatt Jacob char *x; 20565adb54cSMatt Jacob 20665adb54cSMatt Jacob switch (type) { 20765adb54cSMatt Jacob case PCI_QLOGIC_ISP: 20865adb54cSMatt Jacob x = "Qlogic ISP 10X0 PCI SCSI Adapter"; 20965adb54cSMatt Jacob break; 21065adb54cSMatt Jacob case PCI_QLOGIC_ISP2100: 21165adb54cSMatt Jacob x = "Qlogic ISP 2100 PCI FC-AL Adapter"; 21265adb54cSMatt Jacob break; 21365adb54cSMatt Jacob default: 21465adb54cSMatt Jacob return (NULL); 21565adb54cSMatt Jacob } 21665adb54cSMatt Jacob if (oneshot) { 21765adb54cSMatt Jacob oneshot = 0; 218d720e6d5SJustin T. Gibbs printf("%s Version %d.%d, Core Version %d.%d\n", PVS, 219d720e6d5SJustin T. Gibbs ISP_PLATFORM_VERSION_MAJOR, ISP_PLATFORM_VERSION_MINOR, 220d720e6d5SJustin T. Gibbs ISP_CORE_VERSION_MAJOR, ISP_CORE_VERSION_MINOR); 22165adb54cSMatt Jacob } 22265adb54cSMatt Jacob return (x); 22365adb54cSMatt Jacob } 22465adb54cSMatt Jacob 22565adb54cSMatt Jacob 22665adb54cSMatt Jacob static void 22765adb54cSMatt Jacob isp_pci_attach(config_id, unit) 22865adb54cSMatt Jacob pcici_t config_id; 22965adb54cSMatt Jacob int unit; 23065adb54cSMatt Jacob { 23165adb54cSMatt Jacob int mapped; 232a185f9b1SMatt Jacob pci_port_t io_port; 23365adb54cSMatt Jacob u_int32_t data; 23465adb54cSMatt Jacob struct isp_pcisoftc *pcs; 23565adb54cSMatt Jacob struct ispsoftc *isp; 23665adb54cSMatt Jacob vm_offset_t vaddr, paddr; 23765adb54cSMatt Jacob ISP_LOCKVAL_DECL; 23865adb54cSMatt Jacob 23965adb54cSMatt Jacob 24065adb54cSMatt Jacob pcs = malloc(sizeof (struct isp_pcisoftc), M_DEVBUF, M_NOWAIT); 24165adb54cSMatt Jacob if (pcs == NULL) { 2429bffbcd4SBruce Evans printf("isp%d: cannot allocate softc\n", unit); 24365adb54cSMatt Jacob return; 24465adb54cSMatt Jacob } 24565adb54cSMatt Jacob bzero(pcs, sizeof (struct isp_pcisoftc)); 24665adb54cSMatt Jacob 24765adb54cSMatt Jacob vaddr = paddr = NULL; 24865adb54cSMatt Jacob mapped = 0; 24965adb54cSMatt Jacob data = pci_conf_read(config_id, PCI_COMMAND_STATUS_REG); 250a185f9b1SMatt Jacob if (mapped == 0 && (data & PCI_COMMAND_MEM_ENABLE)) { 251a185f9b1SMatt Jacob if (pci_map_mem(config_id, MEM_MAP_REG, &vaddr, &paddr)) { 252a185f9b1SMatt Jacob pcs->pci_st = MEM_SPACE_MAPPING; 253a185f9b1SMatt Jacob pcs->pci_sh = vaddr; 25465adb54cSMatt Jacob mapped++; 25565adb54cSMatt Jacob } 25665adb54cSMatt Jacob } 257a185f9b1SMatt Jacob if (mapped == 0 && (data & PCI_COMMAND_IO_ENABLE)) { 258a185f9b1SMatt Jacob if (pci_map_port(config_id, PCI_MAP_REG_START, &io_port)) { 259a185f9b1SMatt Jacob pcs->pci_st = IO_SPACE_MAPPING; 260a185f9b1SMatt Jacob pcs->pci_sh = io_port; 26165adb54cSMatt Jacob mapped++; 26265adb54cSMatt Jacob } 26365adb54cSMatt Jacob } 26465adb54cSMatt Jacob if (mapped == 0) { 2659bffbcd4SBruce Evans printf("isp%d: unable to map any ports!\n", unit); 26665adb54cSMatt Jacob free(pcs, M_DEVBUF); 26765adb54cSMatt Jacob return; 26865adb54cSMatt Jacob } 26965adb54cSMatt Jacob printf("isp%d: using %s space register mapping\n", unit, 270a185f9b1SMatt Jacob pcs->pci_st == IO_SPACE_MAPPING? "I/O" : "Memory"); 27165adb54cSMatt Jacob 27265adb54cSMatt Jacob isp = &pcs->pci_isp; 27365adb54cSMatt Jacob (void) sprintf(isp->isp_name, "isp%d", unit); 27465adb54cSMatt Jacob isp->isp_osinfo.unit = unit; 27565adb54cSMatt Jacob 27665adb54cSMatt Jacob data = pci_conf_read(config_id, PCI_ID_REG); 27765adb54cSMatt Jacob if (data == PCI_QLOGIC_ISP) { 27865adb54cSMatt Jacob isp->isp_mdvec = &mdvec; 27965adb54cSMatt Jacob isp->isp_type = ISP_HA_SCSI_UNKNOWN; 28065adb54cSMatt Jacob isp->isp_param = &pcs->_z._x; 28165adb54cSMatt Jacob } else if (data == PCI_QLOGIC_ISP2100) { 28265adb54cSMatt Jacob isp->isp_mdvec = &mdvec_2100; 28365adb54cSMatt Jacob isp->isp_type = ISP_HA_FC_2100; 28465adb54cSMatt Jacob isp->isp_param = &pcs->_z._y._a; 28565adb54cSMatt Jacob 286d720e6d5SJustin T. Gibbs ISP_LOCK(isp); 28765adb54cSMatt Jacob data = pci_conf_read(config_id, PCI_COMMAND_STATUS_REG); 28865adb54cSMatt Jacob data |= PCIM_CMD_BUSMASTEREN | PCIM_CMD_INVEN; 28965adb54cSMatt Jacob pci_conf_write(config_id, PCI_COMMAND_STATUS_REG, data); 29065adb54cSMatt Jacob 29165adb54cSMatt Jacob /* 29265adb54cSMatt Jacob * Wierd- we need to clear the lsb in offset 0x30 to take the 29365adb54cSMatt Jacob * chip out of reset state. 29465adb54cSMatt Jacob */ 29565adb54cSMatt Jacob data = pci_conf_read(config_id, 0x30); 29665adb54cSMatt Jacob data &= ~1; 29765adb54cSMatt Jacob pci_conf_write(config_id, 0x30, data); 298d720e6d5SJustin T. Gibbs ISP_UNLOCK(isp); 29965adb54cSMatt Jacob } else { 300d720e6d5SJustin T. Gibbs printf("%s: unknown dev (%x)- punting\n", isp->isp_name, data); 30165adb54cSMatt Jacob free(pcs, M_DEVBUF); 30265adb54cSMatt Jacob return; 30365adb54cSMatt Jacob } 30465adb54cSMatt Jacob 3053dd37e43SMatt Jacob #if __FreeBSD_version >= 300004 306d720e6d5SJustin T. Gibbs if (bus_dma_tag_create(NULL, 0, 0, BUS_SPACE_MAXADDR_32BIT, 307d720e6d5SJustin T. Gibbs BUS_SPACE_MAXADDR, NULL, NULL, 1<<24, 308d720e6d5SJustin T. Gibbs 255, 1<<24, 0, &pcs->parent_dmat) != 0) { 309d720e6d5SJustin T. Gibbs printf("%s: could not create master dma tag\n", isp->isp_name); 310d720e6d5SJustin T. Gibbs free(pcs, M_DEVBUF); 311d720e6d5SJustin T. Gibbs return; 312d720e6d5SJustin T. Gibbs } 313d720e6d5SJustin T. Gibbs #endif 31465adb54cSMatt Jacob if (pci_map_int(config_id, (void (*)(void *))isp_intr, 31565adb54cSMatt Jacob (void *)isp, &IMASK) == 0) { 3169bffbcd4SBruce Evans printf("%s: could not map interrupt\n", isp->isp_name); 31765adb54cSMatt Jacob free(pcs, M_DEVBUF); 31865adb54cSMatt Jacob return; 31965adb54cSMatt Jacob } 32065adb54cSMatt Jacob 32165adb54cSMatt Jacob pcs->pci_id = config_id; 322d720e6d5SJustin T. Gibbs ISP_LOCK(isp); 32365adb54cSMatt Jacob isp_reset(isp); 32465adb54cSMatt Jacob if (isp->isp_state != ISP_RESETSTATE) { 325d720e6d5SJustin T. Gibbs ISP_UNLOCK(isp); 32665adb54cSMatt Jacob free(pcs, M_DEVBUF); 32765adb54cSMatt Jacob return; 32865adb54cSMatt Jacob } 32965adb54cSMatt Jacob isp_init(isp); 33065adb54cSMatt Jacob if (isp->isp_state != ISP_INITSTATE) { 33165adb54cSMatt Jacob isp_uninit(isp); 332d720e6d5SJustin T. Gibbs ISP_UNLOCK(isp); 33365adb54cSMatt Jacob free(pcs, M_DEVBUF); 33465adb54cSMatt Jacob return; 33565adb54cSMatt Jacob } 33665adb54cSMatt Jacob isp_attach(isp); 33765adb54cSMatt Jacob if (isp->isp_state != ISP_RUNSTATE) { 33865adb54cSMatt Jacob isp_uninit(isp); 33965adb54cSMatt Jacob free(pcs, M_DEVBUF); 34065adb54cSMatt Jacob } 341d720e6d5SJustin T. Gibbs ISP_UNLOCK(isp); 342e37c0455SDoug Rabson #ifdef __alpha__ 343e37c0455SDoug Rabson alpha_register_pci_scsi(config_id->bus, config_id->slot, isp->isp_sim); 344e37c0455SDoug Rabson #endif 34565adb54cSMatt Jacob } 34665adb54cSMatt Jacob 34765adb54cSMatt Jacob #define PCI_BIU_REGS_OFF BIU_REGS_OFF 34865adb54cSMatt Jacob 34965adb54cSMatt Jacob static u_int16_t 35065adb54cSMatt Jacob isp_pci_rd_reg(isp, regoff) 35165adb54cSMatt Jacob struct ispsoftc *isp; 35265adb54cSMatt Jacob int regoff; 35365adb54cSMatt Jacob { 35465adb54cSMatt Jacob u_int16_t rv; 35565adb54cSMatt Jacob struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp; 35665adb54cSMatt Jacob int offset, oldsxp = 0; 35765adb54cSMatt Jacob 35865adb54cSMatt Jacob if ((regoff & BIU_BLOCK) != 0) { 35965adb54cSMatt Jacob offset = PCI_BIU_REGS_OFF; 36065adb54cSMatt Jacob } else if ((regoff & MBOX_BLOCK) != 0) { 36165adb54cSMatt Jacob if (isp->isp_type & ISP_HA_SCSI) 36265adb54cSMatt Jacob offset = PCI_MBOX_REGS_OFF; 36365adb54cSMatt Jacob else 36465adb54cSMatt Jacob offset = PCI_MBOX_REGS2100_OFF; 36565adb54cSMatt Jacob } else if ((regoff & SXP_BLOCK) != 0) { 36665adb54cSMatt Jacob offset = PCI_SXP_REGS_OFF; 36765adb54cSMatt Jacob /* 36865adb54cSMatt Jacob * We will assume that someone has paused the RISC processor. 36965adb54cSMatt Jacob */ 37065adb54cSMatt Jacob oldsxp = isp_pci_rd_reg(isp, BIU_CONF1); 37165adb54cSMatt Jacob isp_pci_wr_reg(isp, BIU_CONF1, oldsxp & ~BIU_PCI_CONF1_SXP); 37265adb54cSMatt Jacob } else { 37365adb54cSMatt Jacob offset = PCI_RISC_REGS_OFF; 37465adb54cSMatt Jacob } 37565adb54cSMatt Jacob regoff &= 0xff; 37665adb54cSMatt Jacob offset += regoff; 37765adb54cSMatt Jacob rv = bus_space_read_2(pcs->pci_st, pcs->pci_sh, offset); 37865adb54cSMatt Jacob if ((regoff & SXP_BLOCK) != 0) { 37965adb54cSMatt Jacob isp_pci_wr_reg(isp, BIU_CONF1, oldsxp); 38065adb54cSMatt Jacob } 38165adb54cSMatt Jacob return (rv); 38265adb54cSMatt Jacob } 38365adb54cSMatt Jacob 38465adb54cSMatt Jacob static void 38565adb54cSMatt Jacob isp_pci_wr_reg(isp, regoff, val) 38665adb54cSMatt Jacob struct ispsoftc *isp; 38765adb54cSMatt Jacob int regoff; 38865adb54cSMatt Jacob u_int16_t val; 38965adb54cSMatt Jacob { 39065adb54cSMatt Jacob struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp; 39165adb54cSMatt Jacob int offset, oldsxp = 0; 39265adb54cSMatt Jacob if ((regoff & BIU_BLOCK) != 0) { 39365adb54cSMatt Jacob offset = PCI_BIU_REGS_OFF; 39465adb54cSMatt Jacob } else if ((regoff & MBOX_BLOCK) != 0) { 39565adb54cSMatt Jacob if (isp->isp_type & ISP_HA_SCSI) 39665adb54cSMatt Jacob offset = PCI_MBOX_REGS_OFF; 39765adb54cSMatt Jacob else 39865adb54cSMatt Jacob offset = PCI_MBOX_REGS2100_OFF; 39965adb54cSMatt Jacob } else if ((regoff & SXP_BLOCK) != 0) { 40065adb54cSMatt Jacob offset = PCI_SXP_REGS_OFF; 40165adb54cSMatt Jacob /* 40265adb54cSMatt Jacob * We will assume that someone has paused the RISC processor. 40365adb54cSMatt Jacob */ 40465adb54cSMatt Jacob oldsxp = isp_pci_rd_reg(isp, BIU_CONF1); 40565adb54cSMatt Jacob isp_pci_wr_reg(isp, BIU_CONF1, oldsxp & ~BIU_PCI_CONF1_SXP); 40665adb54cSMatt Jacob } else { 40765adb54cSMatt Jacob offset = PCI_RISC_REGS_OFF; 40865adb54cSMatt Jacob } 40965adb54cSMatt Jacob regoff &= 0xff; 41065adb54cSMatt Jacob offset += regoff; 41165adb54cSMatt Jacob bus_space_write_2(pcs->pci_st, pcs->pci_sh, offset, val); 41265adb54cSMatt Jacob if ((regoff & SXP_BLOCK) != 0) { 41365adb54cSMatt Jacob isp_pci_wr_reg(isp, BIU_CONF1, oldsxp); 41465adb54cSMatt Jacob } 41565adb54cSMatt Jacob } 41665adb54cSMatt Jacob 4173dd37e43SMatt Jacob #if __FreeBSD_version >= 300004 418d720e6d5SJustin T. Gibbs static void isp_map_rquest __P((void *, bus_dma_segment_t *, int, int)); 419d720e6d5SJustin T. Gibbs static void isp_map_result __P((void *, bus_dma_segment_t *, int, int)); 420d720e6d5SJustin T. Gibbs static void isp_map_fcscrt __P((void *, bus_dma_segment_t *, int, int)); 421d720e6d5SJustin T. Gibbs 422d720e6d5SJustin T. Gibbs static void 423d720e6d5SJustin T. Gibbs isp_map_rquest(arg, segs, nseg, error) 424d720e6d5SJustin T. Gibbs void *arg; 425d720e6d5SJustin T. Gibbs bus_dma_segment_t *segs; 426d720e6d5SJustin T. Gibbs int nseg; 427d720e6d5SJustin T. Gibbs int error; 428d720e6d5SJustin T. Gibbs { 429d720e6d5SJustin T. Gibbs struct ispsoftc *isp = (struct ispsoftc *) arg; 430d720e6d5SJustin T. Gibbs isp->isp_rquest_dma = segs->ds_addr; 431d720e6d5SJustin T. Gibbs } 432d720e6d5SJustin T. Gibbs 433d720e6d5SJustin T. Gibbs static void 434d720e6d5SJustin T. Gibbs isp_map_result(arg, segs, nseg, error) 435d720e6d5SJustin T. Gibbs void *arg; 436d720e6d5SJustin T. Gibbs bus_dma_segment_t *segs; 437d720e6d5SJustin T. Gibbs int nseg; 438d720e6d5SJustin T. Gibbs int error; 439d720e6d5SJustin T. Gibbs { 440d720e6d5SJustin T. Gibbs struct ispsoftc *isp = (struct ispsoftc *) arg; 441d720e6d5SJustin T. Gibbs isp->isp_result_dma = segs->ds_addr; 442d720e6d5SJustin T. Gibbs } 443d720e6d5SJustin T. Gibbs 444d720e6d5SJustin T. Gibbs static void 445d720e6d5SJustin T. Gibbs isp_map_fcscrt(arg, segs, nseg, error) 446d720e6d5SJustin T. Gibbs void *arg; 447d720e6d5SJustin T. Gibbs bus_dma_segment_t *segs; 448d720e6d5SJustin T. Gibbs int nseg; 449d720e6d5SJustin T. Gibbs int error; 450d720e6d5SJustin T. Gibbs { 451d720e6d5SJustin T. Gibbs struct ispsoftc *isp = (struct ispsoftc *) arg; 452d720e6d5SJustin T. Gibbs fcparam *fcp = isp->isp_param; 453d720e6d5SJustin T. Gibbs fcp->isp_scdma = segs->ds_addr; 454d720e6d5SJustin T. Gibbs } 455d720e6d5SJustin T. Gibbs 456d720e6d5SJustin T. Gibbs static int 457d720e6d5SJustin T. Gibbs isp_pci_mbxdma(isp) 458d720e6d5SJustin T. Gibbs struct ispsoftc *isp; 459d720e6d5SJustin T. Gibbs { 460d720e6d5SJustin T. Gibbs struct isp_pcisoftc *pci = (struct isp_pcisoftc *)isp; 461d720e6d5SJustin T. Gibbs caddr_t base; 462d720e6d5SJustin T. Gibbs u_int32_t len; 463d720e6d5SJustin T. Gibbs int i, error; 464d720e6d5SJustin T. Gibbs 465d720e6d5SJustin T. Gibbs /* 466d720e6d5SJustin T. Gibbs * Allocate and map the request, result queues, plus FC scratch area. 467d720e6d5SJustin T. Gibbs */ 468d720e6d5SJustin T. Gibbs len = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN); 469d720e6d5SJustin T. Gibbs len += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN); 470d720e6d5SJustin T. Gibbs if (isp->isp_type & ISP_HA_FC) { 471d720e6d5SJustin T. Gibbs len += ISP2100_SCRLEN; 472d720e6d5SJustin T. Gibbs } 473d720e6d5SJustin T. Gibbs if (bus_dma_tag_create(pci->parent_dmat, 0, 0, BUS_SPACE_MAXADDR, 474d720e6d5SJustin T. Gibbs BUS_SPACE_MAXADDR, NULL, NULL, len, 1, BUS_SPACE_MAXSIZE_32BIT, 475d720e6d5SJustin T. Gibbs 0, &pci->cntrol_dmat) != 0) { 476d720e6d5SJustin T. Gibbs printf("%s: cannot create a dma tag for control spaces\n", 477d720e6d5SJustin T. Gibbs isp->isp_name); 478d720e6d5SJustin T. Gibbs return (1); 479d720e6d5SJustin T. Gibbs } 480d720e6d5SJustin T. Gibbs if (bus_dmamem_alloc(pci->cntrol_dmat, (void **)&base, 481d720e6d5SJustin T. Gibbs BUS_DMA_NOWAIT, &pci->cntrol_dmap) != 0) { 4824873663cSMatt Jacob printf("%s: cannot allocate %d bytes of CCB memory\n", 4834873663cSMatt Jacob isp->isp_name, len); 484d720e6d5SJustin T. Gibbs return (1); 485d720e6d5SJustin T. Gibbs } 486d720e6d5SJustin T. Gibbs 487d720e6d5SJustin T. Gibbs isp->isp_rquest = base; 488d720e6d5SJustin T. Gibbs bus_dmamap_load(pci->cntrol_dmat, pci->cntrol_dmap, isp->isp_rquest, 489d720e6d5SJustin T. Gibbs ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN), isp_map_rquest, pci, 0); 490d720e6d5SJustin T. Gibbs 491d720e6d5SJustin T. Gibbs isp->isp_result = base + ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN); 492d720e6d5SJustin T. Gibbs bus_dmamap_load(pci->cntrol_dmat, pci->cntrol_dmap, isp->isp_result, 493d720e6d5SJustin T. Gibbs ISP_QUEUE_SIZE(RESULT_QUEUE_LEN), isp_map_result, pci, 0); 494d720e6d5SJustin T. Gibbs 495d720e6d5SJustin T. Gibbs if (isp->isp_type & ISP_HA_FC) { 496d720e6d5SJustin T. Gibbs fcparam *fcp = (fcparam *) isp->isp_param; 497d720e6d5SJustin T. Gibbs fcp->isp_scratch = isp->isp_result + 498d720e6d5SJustin T. Gibbs ISP_QUEUE_SIZE(RESULT_QUEUE_LEN); 499d720e6d5SJustin T. Gibbs bus_dmamap_load(pci->cntrol_dmat, pci->cntrol_dmap, 500d720e6d5SJustin T. Gibbs fcp->isp_scratch, ISP2100_SCRLEN, isp_map_fcscrt, pci, 0); 501d720e6d5SJustin T. Gibbs } 502d720e6d5SJustin T. Gibbs 503d720e6d5SJustin T. Gibbs /* 504d720e6d5SJustin T. Gibbs * Use this opportunity to initialize/create data DMA maps. 505d720e6d5SJustin T. Gibbs */ 506d720e6d5SJustin T. Gibbs for (i = 0; i < MAXISPREQUEST; i++) { 507d720e6d5SJustin T. Gibbs error = bus_dmamap_create(pci->parent_dmat, 0, &pci->dmaps[i]); 508d720e6d5SJustin T. Gibbs if (error) { 5094873663cSMatt Jacob printf("%s: error %d creating mailbox DMA maps\n", 510d720e6d5SJustin T. Gibbs isp->isp_name, error); 511d720e6d5SJustin T. Gibbs return (1); 512d720e6d5SJustin T. Gibbs } 513d720e6d5SJustin T. Gibbs } 514d720e6d5SJustin T. Gibbs return (0); 515d720e6d5SJustin T. Gibbs } 516d720e6d5SJustin T. Gibbs 517d720e6d5SJustin T. Gibbs static void dma2 __P((void *, bus_dma_segment_t *, int, int)); 518d720e6d5SJustin T. Gibbs typedef struct { 519d720e6d5SJustin T. Gibbs struct ispsoftc *isp; 520d720e6d5SJustin T. Gibbs ISP_SCSI_XFER_T *ccb; 521d720e6d5SJustin T. Gibbs ispreq_t *rq; 522d720e6d5SJustin T. Gibbs u_int8_t *iptrp; 523d720e6d5SJustin T. Gibbs u_int8_t optr; 524d720e6d5SJustin T. Gibbs u_int error; 525d720e6d5SJustin T. Gibbs } mush_t; 526d720e6d5SJustin T. Gibbs 5274873663cSMatt Jacob #define MUSHERR_NOQENTRIES -2 5284873663cSMatt Jacob 529d720e6d5SJustin T. Gibbs static void 530d720e6d5SJustin T. Gibbs dma2(arg, dm_segs, nseg, error) 531d720e6d5SJustin T. Gibbs void *arg; 532d720e6d5SJustin T. Gibbs bus_dma_segment_t *dm_segs; 533d720e6d5SJustin T. Gibbs int nseg; 534d720e6d5SJustin T. Gibbs int error; 535d720e6d5SJustin T. Gibbs { 536d720e6d5SJustin T. Gibbs mush_t *mp; 537d720e6d5SJustin T. Gibbs ISP_SCSI_XFER_T *ccb; 538d720e6d5SJustin T. Gibbs struct ispsoftc *isp; 539d720e6d5SJustin T. Gibbs struct isp_pcisoftc *pci; 540d720e6d5SJustin T. Gibbs bus_dmamap_t *dp; 541d720e6d5SJustin T. Gibbs bus_dma_segment_t *eseg; 542d720e6d5SJustin T. Gibbs ispreq_t *rq; 543d720e6d5SJustin T. Gibbs u_int8_t *iptrp; 544d720e6d5SJustin T. Gibbs u_int8_t optr; 545d720e6d5SJustin T. Gibbs ispcontreq_t *crq; 546d720e6d5SJustin T. Gibbs int drq, seglim, datalen; 547d720e6d5SJustin T. Gibbs 548d720e6d5SJustin T. Gibbs mp = (mush_t *) arg; 549d720e6d5SJustin T. Gibbs if (error) { 550d720e6d5SJustin T. Gibbs mp->error = error; 551d720e6d5SJustin T. Gibbs return; 552d720e6d5SJustin T. Gibbs } 553d720e6d5SJustin T. Gibbs 554d720e6d5SJustin T. Gibbs isp = mp->isp; 555d720e6d5SJustin T. Gibbs if (nseg < 1) { 556d720e6d5SJustin T. Gibbs printf("%s: zero or negative segment count\n", isp->isp_name); 557d720e6d5SJustin T. Gibbs mp->error = EFAULT; 558d720e6d5SJustin T. Gibbs return; 559d720e6d5SJustin T. Gibbs } 560d720e6d5SJustin T. Gibbs ccb = mp->ccb; 561d720e6d5SJustin T. Gibbs rq = mp->rq; 562d720e6d5SJustin T. Gibbs iptrp = mp->iptrp; 563d720e6d5SJustin T. Gibbs optr = mp->optr; 564d720e6d5SJustin T. Gibbs 565d720e6d5SJustin T. Gibbs pci = (struct isp_pcisoftc *)isp; 566d720e6d5SJustin T. Gibbs dp = &pci->dmaps[rq->req_handle - 1]; 567d720e6d5SJustin T. Gibbs if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) { 568d720e6d5SJustin T. Gibbs bus_dmamap_sync(pci->parent_dmat, *dp, BUS_DMASYNC_PREREAD); 569d720e6d5SJustin T. Gibbs drq = REQFLAG_DATA_IN; 570d720e6d5SJustin T. Gibbs } else { 571d720e6d5SJustin T. Gibbs bus_dmamap_sync(pci->parent_dmat, *dp, BUS_DMASYNC_PREWRITE); 572d720e6d5SJustin T. Gibbs drq = REQFLAG_DATA_OUT; 573d720e6d5SJustin T. Gibbs } 574d720e6d5SJustin T. Gibbs 575d720e6d5SJustin T. Gibbs datalen = XS_XFRLEN(ccb); 576d720e6d5SJustin T. Gibbs if (isp->isp_type & ISP_HA_FC) { 577d720e6d5SJustin T. Gibbs seglim = ISP_RQDSEG_T2; 578d720e6d5SJustin T. Gibbs ((ispreqt2_t *)rq)->req_totalcnt = datalen; 579d720e6d5SJustin T. Gibbs ((ispreqt2_t *)rq)->req_flags |= drq; 580d720e6d5SJustin T. Gibbs } else { 581d720e6d5SJustin T. Gibbs seglim = ISP_RQDSEG; 582d720e6d5SJustin T. Gibbs rq->req_flags |= drq; 583d720e6d5SJustin T. Gibbs } 584d720e6d5SJustin T. Gibbs 585d720e6d5SJustin T. Gibbs eseg = dm_segs + nseg; 586d720e6d5SJustin T. Gibbs 587d720e6d5SJustin T. Gibbs while (datalen != 0 && rq->req_seg_count < seglim && dm_segs != eseg) { 588d720e6d5SJustin T. Gibbs if (isp->isp_type & ISP_HA_FC) { 589d720e6d5SJustin T. Gibbs ispreqt2_t *rq2 = (ispreqt2_t *)rq; 590d720e6d5SJustin T. Gibbs rq2->req_dataseg[rq2->req_seg_count].ds_base = 591d720e6d5SJustin T. Gibbs dm_segs->ds_addr; 592d720e6d5SJustin T. Gibbs rq2->req_dataseg[rq2->req_seg_count].ds_count = 593d720e6d5SJustin T. Gibbs dm_segs->ds_len; 594d720e6d5SJustin T. Gibbs } else { 595d720e6d5SJustin T. Gibbs rq->req_dataseg[rq->req_seg_count].ds_base = 596d720e6d5SJustin T. Gibbs dm_segs->ds_addr; 597d720e6d5SJustin T. Gibbs rq->req_dataseg[rq->req_seg_count].ds_count = 598d720e6d5SJustin T. Gibbs dm_segs->ds_len; 599d720e6d5SJustin T. Gibbs } 600d720e6d5SJustin T. Gibbs datalen -= dm_segs->ds_len; 601d720e6d5SJustin T. Gibbs #if 0 602d720e6d5SJustin T. Gibbs if (isp->isp_type & ISP_HA_FC) { 603d720e6d5SJustin T. Gibbs ispreqt2_t *rq2 = (ispreqt2_t *)rq; 604d720e6d5SJustin T. Gibbs printf("%s: seg0[%d] cnt 0x%x paddr 0x%08x\n", 605d720e6d5SJustin T. Gibbs isp->isp_name, rq->req_seg_count, 606d720e6d5SJustin T. Gibbs rq2->req_dataseg[rq2->req_seg_count].ds_count, 607d720e6d5SJustin T. Gibbs rq2->req_dataseg[rq2->req_seg_count].ds_base); 608d720e6d5SJustin T. Gibbs } else { 609d720e6d5SJustin T. Gibbs printf("%s: seg0[%d] cnt 0x%x paddr 0x%08x\n", 610d720e6d5SJustin T. Gibbs isp->isp_name, rq->req_seg_count, 611d720e6d5SJustin T. Gibbs rq->req_dataseg[rq->req_seg_count].ds_count, 612d720e6d5SJustin T. Gibbs rq->req_dataseg[rq->req_seg_count].ds_base); 613d720e6d5SJustin T. Gibbs } 614d720e6d5SJustin T. Gibbs #endif 615d720e6d5SJustin T. Gibbs rq->req_seg_count++; 616d720e6d5SJustin T. Gibbs dm_segs++; 617d720e6d5SJustin T. Gibbs } 618d720e6d5SJustin T. Gibbs 619d720e6d5SJustin T. Gibbs while (datalen > 0 && dm_segs != eseg) { 620d720e6d5SJustin T. Gibbs crq = (ispcontreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, *iptrp); 6214873663cSMatt Jacob *iptrp = ISP_NXT_QENTRY(*iptrp, RQUEST_QUEUE_LEN); 622d720e6d5SJustin T. Gibbs if (*iptrp == optr) { 6234873663cSMatt Jacob #if 0 6244873663cSMatt Jacob printf("%s: Request Queue Overflow++\n", isp->isp_name); 6254873663cSMatt Jacob #endif 6264873663cSMatt Jacob mp->error = MUSHERR_NOQENTRIES; 627d720e6d5SJustin T. Gibbs return; 628d720e6d5SJustin T. Gibbs } 629d720e6d5SJustin T. Gibbs rq->req_header.rqs_entry_count++; 630d720e6d5SJustin T. Gibbs bzero((void *)crq, sizeof (*crq)); 631d720e6d5SJustin T. Gibbs crq->req_header.rqs_entry_count = 1; 632d720e6d5SJustin T. Gibbs crq->req_header.rqs_entry_type = RQSTYPE_DATASEG; 633d720e6d5SJustin T. Gibbs 634d720e6d5SJustin T. Gibbs seglim = 0; 635d720e6d5SJustin T. Gibbs while (datalen > 0 && seglim < ISP_CDSEG && dm_segs != eseg) { 636d720e6d5SJustin T. Gibbs crq->req_dataseg[seglim].ds_base = 637d720e6d5SJustin T. Gibbs dm_segs->ds_addr; 638d720e6d5SJustin T. Gibbs crq->req_dataseg[seglim].ds_count = 639d720e6d5SJustin T. Gibbs dm_segs->ds_len; 640d720e6d5SJustin T. Gibbs #if 0 641d720e6d5SJustin T. Gibbs printf("%s: seg%d[%d] cnt 0x%x paddr 0x%08x\n", 642d720e6d5SJustin T. Gibbs isp->isp_name, rq->req_header.rqs_entry_count-1, 643d720e6d5SJustin T. Gibbs seglim, crq->req_dataseg[seglim].ds_count, 644d720e6d5SJustin T. Gibbs crq->req_dataseg[seglim].ds_base); 645d720e6d5SJustin T. Gibbs #endif 646d720e6d5SJustin T. Gibbs rq->req_seg_count++; 647d720e6d5SJustin T. Gibbs dm_segs++; 648d720e6d5SJustin T. Gibbs seglim++; 649d720e6d5SJustin T. Gibbs datalen -= dm_segs->ds_len; 650d720e6d5SJustin T. Gibbs } 651d720e6d5SJustin T. Gibbs } 652d720e6d5SJustin T. Gibbs } 653d720e6d5SJustin T. Gibbs 654d720e6d5SJustin T. Gibbs static int 655d720e6d5SJustin T. Gibbs isp_pci_dmasetup(isp, ccb, rq, iptrp, optr) 656d720e6d5SJustin T. Gibbs struct ispsoftc *isp; 657d720e6d5SJustin T. Gibbs ISP_SCSI_XFER_T *ccb; 658d720e6d5SJustin T. Gibbs ispreq_t *rq; 659d720e6d5SJustin T. Gibbs u_int8_t *iptrp; 660d720e6d5SJustin T. Gibbs u_int8_t optr; 661d720e6d5SJustin T. Gibbs { 662d720e6d5SJustin T. Gibbs struct isp_pcisoftc *pci = (struct isp_pcisoftc *)isp; 663d720e6d5SJustin T. Gibbs struct ccb_hdr *ccb_h; 664d720e6d5SJustin T. Gibbs struct ccb_scsiio *csio; 665d720e6d5SJustin T. Gibbs bus_dmamap_t *dp; 666d720e6d5SJustin T. Gibbs mush_t mush, *mp; 667d720e6d5SJustin T. Gibbs 668d720e6d5SJustin T. Gibbs csio = (struct ccb_scsiio *) ccb; 669d720e6d5SJustin T. Gibbs ccb_h = &csio->ccb_h; 670d720e6d5SJustin T. Gibbs 671d720e6d5SJustin T. Gibbs if ((ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_NONE) { 672d720e6d5SJustin T. Gibbs rq->req_seg_count = 1; 6734873663cSMatt Jacob return (CMD_QUEUED); 674d720e6d5SJustin T. Gibbs } 675d720e6d5SJustin T. Gibbs dp = &pci->dmaps[rq->req_handle - 1]; 676d720e6d5SJustin T. Gibbs 677d720e6d5SJustin T. Gibbs /* 678d720e6d5SJustin T. Gibbs * Do a virtual grapevine step to collect info for 6794873663cSMatt Jacob * the callback dma allocation that we have to use... 680d720e6d5SJustin T. Gibbs */ 681d720e6d5SJustin T. Gibbs mp = &mush; 682d720e6d5SJustin T. Gibbs mp->isp = isp; 683d720e6d5SJustin T. Gibbs mp->ccb = ccb; 684d720e6d5SJustin T. Gibbs mp->rq = rq; 685d720e6d5SJustin T. Gibbs mp->iptrp = iptrp; 686d720e6d5SJustin T. Gibbs mp->optr = optr; 687d720e6d5SJustin T. Gibbs mp->error = 0; 688d720e6d5SJustin T. Gibbs 689d720e6d5SJustin T. Gibbs if ((ccb_h->flags & CAM_SCATTER_VALID) == 0) { 690d720e6d5SJustin T. Gibbs if ((ccb_h->flags & CAM_DATA_PHYS) == 0) { 6914873663cSMatt Jacob int error, s; 6924873663cSMatt Jacob 693d720e6d5SJustin T. Gibbs s = splsoftvm(); 694d720e6d5SJustin T. Gibbs error = bus_dmamap_load(pci->parent_dmat, *dp, 695d720e6d5SJustin T. Gibbs csio->data_ptr, csio->dxfer_len, dma2, mp, 0); 696d720e6d5SJustin T. Gibbs if (error == EINPROGRESS) { 697d720e6d5SJustin T. Gibbs bus_dmamap_unload(pci->parent_dmat, *dp); 698d720e6d5SJustin T. Gibbs mp->error = EINVAL; 6994873663cSMatt Jacob printf("%s: deferred dma allocation not " 7004873663cSMatt Jacob "supported\n", isp->isp_name); 701d720e6d5SJustin T. Gibbs } else if (error && mp->error == 0) { 702d720e6d5SJustin T. Gibbs mp->error = error; 703d720e6d5SJustin T. Gibbs } 7044873663cSMatt Jacob splx(s); 705d720e6d5SJustin T. Gibbs } else { 706d720e6d5SJustin T. Gibbs /* Pointer to physical buffer */ 707d720e6d5SJustin T. Gibbs struct bus_dma_segment seg; 708d720e6d5SJustin T. Gibbs seg.ds_addr = (bus_addr_t)csio->data_ptr; 709d720e6d5SJustin T. Gibbs seg.ds_len = csio->dxfer_len; 710d720e6d5SJustin T. Gibbs dma2(mp, &seg, 1, 0); 711d720e6d5SJustin T. Gibbs } 712d720e6d5SJustin T. Gibbs } else { 713d720e6d5SJustin T. Gibbs struct bus_dma_segment *segs; 714d720e6d5SJustin T. Gibbs 715d720e6d5SJustin T. Gibbs if ((ccb_h->flags & CAM_DATA_PHYS) != 0) { 716d720e6d5SJustin T. Gibbs printf("%s: Physical segment pointers unsupported", 717d720e6d5SJustin T. Gibbs isp->isp_name); 718d720e6d5SJustin T. Gibbs mp->error = EINVAL; 719d720e6d5SJustin T. Gibbs } else if ((ccb_h->flags & CAM_SG_LIST_PHYS) == 0) { 720d720e6d5SJustin T. Gibbs printf("%s: Virtual segment addresses unsupported", 721d720e6d5SJustin T. Gibbs isp->isp_name); 722d720e6d5SJustin T. Gibbs mp->error = EINVAL; 723d720e6d5SJustin T. Gibbs } else { 724d720e6d5SJustin T. Gibbs /* Just use the segments provided */ 725d720e6d5SJustin T. Gibbs segs = (struct bus_dma_segment *) csio->data_ptr; 726d720e6d5SJustin T. Gibbs dma2(mp, segs, csio->sglist_cnt, 0); 727d720e6d5SJustin T. Gibbs } 728d720e6d5SJustin T. Gibbs } 729d720e6d5SJustin T. Gibbs if (mp->error) { 7304873663cSMatt Jacob int retval = CMD_COMPLETE; 7314873663cSMatt Jacob if (mp->error == MUSHERR_NOQENTRIES) { 7324873663cSMatt Jacob retval = CMD_EAGAIN; 7334873663cSMatt Jacob ccb_h->status = CAM_UNREC_HBA_ERROR; 7344873663cSMatt Jacob } else if (mp->error == EFBIG) { 735d720e6d5SJustin T. Gibbs ccb_h->status = CAM_REQ_TOO_BIG; 736d720e6d5SJustin T. Gibbs } else if (mp->error == EINVAL) { 737d720e6d5SJustin T. Gibbs ccb_h->status = CAM_REQ_INVALID; 738d720e6d5SJustin T. Gibbs } else { 739d720e6d5SJustin T. Gibbs ccb_h->status = CAM_UNREC_HBA_ERROR; 740d720e6d5SJustin T. Gibbs } 7414873663cSMatt Jacob return (retval); 7424873663cSMatt Jacob } else { 7434873663cSMatt Jacob return (CMD_QUEUED); 744d720e6d5SJustin T. Gibbs } 745d720e6d5SJustin T. Gibbs } 746d720e6d5SJustin T. Gibbs 747d720e6d5SJustin T. Gibbs static void 748d720e6d5SJustin T. Gibbs isp_pci_dmateardown(isp, ccb, handle) 749d720e6d5SJustin T. Gibbs struct ispsoftc *isp; 750d720e6d5SJustin T. Gibbs ISP_SCSI_XFER_T *ccb; 751d720e6d5SJustin T. Gibbs u_int32_t handle; 752d720e6d5SJustin T. Gibbs { 753d720e6d5SJustin T. Gibbs struct isp_pcisoftc *pci = (struct isp_pcisoftc *)isp; 754d720e6d5SJustin T. Gibbs bus_dmamap_t *dp = &pci->dmaps[handle]; 755d720e6d5SJustin T. Gibbs 756d720e6d5SJustin T. Gibbs if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) { 757d720e6d5SJustin T. Gibbs bus_dmamap_sync(pci->parent_dmat, *dp, BUS_DMASYNC_POSTREAD); 758d720e6d5SJustin T. Gibbs } else { 759d720e6d5SJustin T. Gibbs bus_dmamap_sync(pci->parent_dmat, *dp, BUS_DMASYNC_POSTWRITE); 760d720e6d5SJustin T. Gibbs } 761d720e6d5SJustin T. Gibbs bus_dmamap_unload(pci->parent_dmat, *dp); 762d720e6d5SJustin T. Gibbs } 763d720e6d5SJustin T. Gibbs 7643dd37e43SMatt Jacob #else /* __FreeBSD_version >= 300004 */ 7653dd37e43SMatt Jacob 766d720e6d5SJustin T. Gibbs 76765adb54cSMatt Jacob static int 76865adb54cSMatt Jacob isp_pci_mbxdma(isp) 76965adb54cSMatt Jacob struct ispsoftc *isp; 77065adb54cSMatt Jacob { 77165adb54cSMatt Jacob struct isp_pcisoftc *pci = (struct isp_pcisoftc *)isp; 77265adb54cSMatt Jacob u_int32_t len; 77365adb54cSMatt Jacob int rseg; 77465adb54cSMatt Jacob 77565adb54cSMatt Jacob /* XXXX CHECK FOR ALIGNMENT */ 77665adb54cSMatt Jacob /* 77765adb54cSMatt Jacob * Allocate and map the request queue. 77865adb54cSMatt Jacob */ 779d720e6d5SJustin T. Gibbs len = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN); 78065adb54cSMatt Jacob isp->isp_rquest = malloc(len, M_DEVBUF, M_NOWAIT); 78165adb54cSMatt Jacob if (isp->isp_rquest == NULL) { 78265adb54cSMatt Jacob printf("%s: cannot malloc request queue\n", isp->isp_name); 78365adb54cSMatt Jacob return (1); 78465adb54cSMatt Jacob } 78565adb54cSMatt Jacob isp->isp_rquest_dma = vtophys(isp->isp_rquest); 78665adb54cSMatt Jacob 78765adb54cSMatt Jacob #if 0 78865adb54cSMatt Jacob printf("RQUEST=0x%x (0x%x)...", isp->isp_rquest, isp->isp_rquest_dma); 78965adb54cSMatt Jacob #endif 79065adb54cSMatt Jacob 79165adb54cSMatt Jacob /* 79265adb54cSMatt Jacob * Allocate and map the result queue. 79365adb54cSMatt Jacob */ 794d720e6d5SJustin T. Gibbs len = ISP_QUEUE_SIZE(RESULT_QUEUE_LEN); 79565adb54cSMatt Jacob isp->isp_result = malloc(len, M_DEVBUF, M_NOWAIT); 79665adb54cSMatt Jacob if (isp->isp_result == NULL) { 79765adb54cSMatt Jacob free(isp->isp_rquest, M_DEVBUF); 79865adb54cSMatt Jacob printf("%s: cannot malloc result queue\n", isp->isp_name); 79965adb54cSMatt Jacob return (1); 80065adb54cSMatt Jacob } 80165adb54cSMatt Jacob isp->isp_result_dma = vtophys(isp->isp_result); 80265adb54cSMatt Jacob #if 0 80365adb54cSMatt Jacob printf("RESULT=0x%x (0x%x)\n", isp->isp_result, isp->isp_result_dma); 80465adb54cSMatt Jacob #endif 80565adb54cSMatt Jacob if (isp->isp_type & ISP_HA_FC) { 80665adb54cSMatt Jacob fcparam *fcp = isp->isp_param; 80765adb54cSMatt Jacob len = ISP2100_SCRLEN; 80865adb54cSMatt Jacob fcp->isp_scratch = (volatile caddr_t) &pci->_z._y._b; 80965adb54cSMatt Jacob fcp->isp_scdma = vtophys(fcp->isp_scratch); 81065adb54cSMatt Jacob } 81165adb54cSMatt Jacob return (0); 81265adb54cSMatt Jacob } 81365adb54cSMatt Jacob 81465adb54cSMatt Jacob static int 81565adb54cSMatt Jacob isp_pci_dmasetup(isp, xs, rq, iptrp, optr) 81665adb54cSMatt Jacob struct ispsoftc *isp; 81765adb54cSMatt Jacob ISP_SCSI_XFER_T *xs; 81865adb54cSMatt Jacob ispreq_t *rq; 81965adb54cSMatt Jacob u_int8_t *iptrp; 82065adb54cSMatt Jacob u_int8_t optr; 82165adb54cSMatt Jacob { 82265adb54cSMatt Jacob struct isp_pcisoftc *pci = (struct isp_pcisoftc *)isp; 82365adb54cSMatt Jacob ispcontreq_t *crq; 82465adb54cSMatt Jacob vm_offset_t vaddr; 82565adb54cSMatt Jacob int drq, seglim; 82665adb54cSMatt Jacob u_int32_t paddr, nextpaddr, datalen, size, *ctrp; 82765adb54cSMatt Jacob 82865adb54cSMatt Jacob if (xs->datalen == 0) { 82965adb54cSMatt Jacob rq->req_seg_count = 1; 8304873663cSMatt Jacob return (CMD_QUEUED); 83165adb54cSMatt Jacob } 83265adb54cSMatt Jacob 83365adb54cSMatt Jacob if (xs->flags & SCSI_DATA_IN) { 83465adb54cSMatt Jacob drq = REQFLAG_DATA_IN; 83565adb54cSMatt Jacob } else { 83665adb54cSMatt Jacob drq = REQFLAG_DATA_OUT; 83765adb54cSMatt Jacob } 83865adb54cSMatt Jacob 83965adb54cSMatt Jacob if (isp->isp_type & ISP_HA_FC) { 84065adb54cSMatt Jacob seglim = ISP_RQDSEG_T2; 841d720e6d5SJustin T. Gibbs ((ispreqt2_t *)rq)->req_totalcnt = XS_XFRLEN(xs); 84265adb54cSMatt Jacob ((ispreqt2_t *)rq)->req_flags |= drq; 84365adb54cSMatt Jacob } else { 84465adb54cSMatt Jacob seglim = ISP_RQDSEG; 84565adb54cSMatt Jacob rq->req_flags |= drq; 84665adb54cSMatt Jacob } 84765adb54cSMatt Jacob 848d720e6d5SJustin T. Gibbs datalen = XS_XFRLEN(xs); 84965adb54cSMatt Jacob vaddr = (vm_offset_t) xs->data; 85065adb54cSMatt Jacob paddr = vtophys(vaddr); 85165adb54cSMatt Jacob 85265adb54cSMatt Jacob while (datalen != 0 && rq->req_seg_count < seglim) { 85365adb54cSMatt Jacob if (isp->isp_type & ISP_HA_FC) { 85465adb54cSMatt Jacob ispreqt2_t *rq2 = (ispreqt2_t *)rq; 85565adb54cSMatt Jacob rq2->req_dataseg[rq2->req_seg_count].ds_base = paddr; 85665adb54cSMatt Jacob ctrp = &rq2->req_dataseg[rq2->req_seg_count].ds_count; 85765adb54cSMatt Jacob } else { 85865adb54cSMatt Jacob rq->req_dataseg[rq->req_seg_count].ds_base = paddr; 85965adb54cSMatt Jacob ctrp = &rq->req_dataseg[rq->req_seg_count].ds_count; 86065adb54cSMatt Jacob } 86165adb54cSMatt Jacob nextpaddr = paddr; 86265adb54cSMatt Jacob *(ctrp) = 0; 86365adb54cSMatt Jacob 86465adb54cSMatt Jacob while (datalen != 0 && paddr == nextpaddr) { 86565adb54cSMatt Jacob nextpaddr = (paddr & (~PAGE_MASK)) + PAGE_SIZE; 86665adb54cSMatt Jacob size = nextpaddr - paddr; 86765adb54cSMatt Jacob if (size > datalen) 86865adb54cSMatt Jacob size = datalen; 86965adb54cSMatt Jacob 87065adb54cSMatt Jacob *(ctrp) += size; 87165adb54cSMatt Jacob vaddr += size; 87265adb54cSMatt Jacob datalen -= size; 87365adb54cSMatt Jacob if (datalen != 0) 87465adb54cSMatt Jacob paddr = vtophys(vaddr); 87565adb54cSMatt Jacob 87665adb54cSMatt Jacob } 87765adb54cSMatt Jacob #if 0 87865adb54cSMatt Jacob if (isp->isp_type & ISP_HA_FC) { 87965adb54cSMatt Jacob ispreqt2_t *rq2 = (ispreqt2_t *)rq; 88065adb54cSMatt Jacob printf("%s: seg0[%d] cnt 0x%x paddr 0x%08x\n", 88165adb54cSMatt Jacob isp->isp_name, rq->req_seg_count, 88265adb54cSMatt Jacob rq2->req_dataseg[rq2->req_seg_count].ds_count, 88365adb54cSMatt Jacob rq2->req_dataseg[rq2->req_seg_count].ds_base); 88465adb54cSMatt Jacob } else { 88565adb54cSMatt Jacob printf("%s: seg0[%d] cnt 0x%x paddr 0x%08x\n", 88665adb54cSMatt Jacob isp->isp_name, rq->req_seg_count, 88765adb54cSMatt Jacob rq->req_dataseg[rq->req_seg_count].ds_count, 88865adb54cSMatt Jacob rq->req_dataseg[rq->req_seg_count].ds_base); 88965adb54cSMatt Jacob } 89065adb54cSMatt Jacob #endif 89165adb54cSMatt Jacob rq->req_seg_count++; 89265adb54cSMatt Jacob } 89365adb54cSMatt Jacob 89465adb54cSMatt Jacob 89565adb54cSMatt Jacob 89665adb54cSMatt Jacob if (datalen == 0) 8974873663cSMatt Jacob return (CMD_QUEUED); 89865adb54cSMatt Jacob 89965adb54cSMatt Jacob paddr = vtophys(vaddr); 90065adb54cSMatt Jacob while (datalen > 0) { 90165adb54cSMatt Jacob crq = (ispcontreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, *iptrp); 9024873663cSMatt Jacob *iptrp = ISP_NXT_QENTRY(*iptrp, RQUEST_QUEUE_LEN); 90365adb54cSMatt Jacob if (*iptrp == optr) { 90465adb54cSMatt Jacob printf("%s: Request Queue Overflow\n", isp->isp_name); 905d720e6d5SJustin T. Gibbs XS_SETERR(xs, HBA_BOTCH); 9064873663cSMatt Jacob return (CMD_EAGAIN); 90765adb54cSMatt Jacob } 90865adb54cSMatt Jacob rq->req_header.rqs_entry_count++; 90965adb54cSMatt Jacob bzero((void *)crq, sizeof (*crq)); 91065adb54cSMatt Jacob crq->req_header.rqs_entry_count = 1; 91165adb54cSMatt Jacob crq->req_header.rqs_entry_type = RQSTYPE_DATASEG; 91265adb54cSMatt Jacob 91365adb54cSMatt Jacob for (seglim = 0; datalen != 0 && seglim < ISP_CDSEG; seglim++) { 91465adb54cSMatt Jacob crq->req_dataseg[seglim].ds_base = paddr; 91565adb54cSMatt Jacob ctrp = &crq->req_dataseg[seglim].ds_count; 91665adb54cSMatt Jacob *(ctrp) = 0; 91765adb54cSMatt Jacob nextpaddr = paddr; 91865adb54cSMatt Jacob while (datalen != 0 && paddr == nextpaddr) { 91965adb54cSMatt Jacob nextpaddr = (paddr & (~PAGE_MASK)) + PAGE_SIZE; 92065adb54cSMatt Jacob size = nextpaddr - paddr; 92165adb54cSMatt Jacob if (size > datalen) 92265adb54cSMatt Jacob size = datalen; 92365adb54cSMatt Jacob 92465adb54cSMatt Jacob *(ctrp) += size; 92565adb54cSMatt Jacob vaddr += size; 92665adb54cSMatt Jacob datalen -= size; 92765adb54cSMatt Jacob if (datalen != 0) 92865adb54cSMatt Jacob paddr = vtophys(vaddr); 92965adb54cSMatt Jacob } 93065adb54cSMatt Jacob #if 0 93165adb54cSMatt Jacob printf("%s: seg%d[%d] cnt 0x%x paddr 0x%08x\n", 93265adb54cSMatt Jacob isp->isp_name, rq->req_header.rqs_entry_count-1, 93365adb54cSMatt Jacob seglim, crq->req_dataseg[seglim].ds_count, 93465adb54cSMatt Jacob crq->req_dataseg[seglim].ds_base); 93565adb54cSMatt Jacob #endif 93665adb54cSMatt Jacob rq->req_seg_count++; 93765adb54cSMatt Jacob } 93865adb54cSMatt Jacob } 93965adb54cSMatt Jacob 9404873663cSMatt Jacob return (CMD_QUEUED); 94165adb54cSMatt Jacob } 942d720e6d5SJustin T. Gibbs #endif 94365adb54cSMatt Jacob 94465adb54cSMatt Jacob static void 94565adb54cSMatt Jacob isp_pci_reset1(isp) 94665adb54cSMatt Jacob struct ispsoftc *isp; 94765adb54cSMatt Jacob { 94865adb54cSMatt Jacob /* Make sure the BIOS is disabled */ 94965adb54cSMatt Jacob isp_pci_wr_reg(isp, HCCR, PCI_HCCR_CMD_BIOS); 95065adb54cSMatt Jacob } 95165adb54cSMatt Jacob 95265adb54cSMatt Jacob static void 95365adb54cSMatt Jacob isp_pci_dumpregs(isp) 95465adb54cSMatt Jacob struct ispsoftc *isp; 95565adb54cSMatt Jacob { 95665adb54cSMatt Jacob struct isp_pcisoftc *pci = (struct isp_pcisoftc *)isp; 9579bffbcd4SBruce Evans printf("%s: PCI Status Command/Status=%lx\n", pci->pci_isp.isp_name, 95865adb54cSMatt Jacob pci_conf_read(pci->pci_id, PCI_COMMAND_STATUS_REG)); 95965adb54cSMatt Jacob } 960