2 * Copyright (c) 1997-2008 by Matthew Jacob
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice immediately at the beginning of the file, without modification,
10 * this list of conditions, and the following disclaimer.
11 * 2. The name of the author may not be used to endorse or promote products
12 * derived from this software without specific prior written permission.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * PCI specific probe and attach routines for Qlogic ISP SCSI adapters.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/linker.h>
38 #include <sys/firmware.h>
40 #include <sys/stdint.h>
41 #include <dev/pci/pcireg.h>
42 #include <dev/pci/pcivar.h>
43 #include <machine/bus.h>
44 #include <machine/resource.h>
46 #include <sys/malloc.h>
50 #include <dev/ofw/openfirm.h>
51 #include <machine/ofw_machdep.h>
54 #include <dev/isp/isp_freebsd.h>
56 static uint32_t isp_pci_rd_reg(ispsoftc_t *, int);
57 static void isp_pci_wr_reg(ispsoftc_t *, int, uint32_t);
58 static uint32_t isp_pci_rd_reg_1080(ispsoftc_t *, int);
59 static void isp_pci_wr_reg_1080(ispsoftc_t *, int, uint32_t);
60 static uint32_t isp_pci_rd_reg_2400(ispsoftc_t *, int);
61 static void isp_pci_wr_reg_2400(ispsoftc_t *, int, uint32_t);
62 static int isp_pci_rd_isr(ispsoftc_t *, uint32_t *, uint16_t *, uint16_t *);
63 static int isp_pci_rd_isr_2300(ispsoftc_t *, uint32_t *, uint16_t *, uint16_t *);
64 static int isp_pci_rd_isr_2400(ispsoftc_t *, uint32_t *, uint16_t *, uint16_t *);
65 static int isp_pci_mbxdma(ispsoftc_t *);
66 static int isp_pci_dmasetup(ispsoftc_t *, XS_T *, void *);
69 static void isp_pci_reset0(ispsoftc_t *);
70 static void isp_pci_reset1(ispsoftc_t *);
71 static void isp_pci_dumpregs(ispsoftc_t *, const char *);
73 static struct ispmdvec mdvec = {
79 isp_common_dmateardown,
84 BIU_BURST_ENABLE|BIU_PCI_CONF1_FIFO_64
87 static struct ispmdvec mdvec_1080 = {
93 isp_common_dmateardown,
98 BIU_BURST_ENABLE|BIU_PCI_CONF1_FIFO_64
101 static struct ispmdvec mdvec_12160 = {
107 isp_common_dmateardown,
112 BIU_BURST_ENABLE|BIU_PCI_CONF1_FIFO_64
115 static struct ispmdvec mdvec_2100 = {
121 isp_common_dmateardown,
127 static struct ispmdvec mdvec_2200 = {
133 isp_common_dmateardown,
139 static struct ispmdvec mdvec_2300 = {
145 isp_common_dmateardown,
151 static struct ispmdvec mdvec_2400 = {
157 isp_common_dmateardown,
163 static struct ispmdvec mdvec_2500 = {
169 isp_common_dmateardown,
175 #ifndef PCIM_CMD_INVEN
176 #define PCIM_CMD_INVEN 0x10
178 #ifndef PCIM_CMD_BUSMASTEREN
179 #define PCIM_CMD_BUSMASTEREN 0x0004
181 #ifndef PCIM_CMD_PERRESPEN
182 #define PCIM_CMD_PERRESPEN 0x0040
184 #ifndef PCIM_CMD_SEREN
185 #define PCIM_CMD_SEREN 0x0100
187 #ifndef PCIM_CMD_INTX_DISABLE
188 #define PCIM_CMD_INTX_DISABLE 0x0400
192 #define PCIR_COMMAND 0x04
195 #ifndef PCIR_CACHELNSZ
196 #define PCIR_CACHELNSZ 0x0c
199 #ifndef PCIR_LATTIMER
200 #define PCIR_LATTIMER 0x0d
204 #define PCIR_ROMADDR 0x30
207 #ifndef PCI_VENDOR_QLOGIC
208 #define PCI_VENDOR_QLOGIC 0x1077
211 #ifndef PCI_PRODUCT_QLOGIC_ISP1020
212 #define PCI_PRODUCT_QLOGIC_ISP1020 0x1020
215 #ifndef PCI_PRODUCT_QLOGIC_ISP1080
216 #define PCI_PRODUCT_QLOGIC_ISP1080 0x1080
219 #ifndef PCI_PRODUCT_QLOGIC_ISP10160
220 #define PCI_PRODUCT_QLOGIC_ISP10160 0x1016
223 #ifndef PCI_PRODUCT_QLOGIC_ISP12160
224 #define PCI_PRODUCT_QLOGIC_ISP12160 0x1216
227 #ifndef PCI_PRODUCT_QLOGIC_ISP1240
228 #define PCI_PRODUCT_QLOGIC_ISP1240 0x1240
231 #ifndef PCI_PRODUCT_QLOGIC_ISP1280
232 #define PCI_PRODUCT_QLOGIC_ISP1280 0x1280
235 #ifndef PCI_PRODUCT_QLOGIC_ISP2100
236 #define PCI_PRODUCT_QLOGIC_ISP2100 0x2100
239 #ifndef PCI_PRODUCT_QLOGIC_ISP2200
240 #define PCI_PRODUCT_QLOGIC_ISP2200 0x2200
243 #ifndef PCI_PRODUCT_QLOGIC_ISP2300
244 #define PCI_PRODUCT_QLOGIC_ISP2300 0x2300
247 #ifndef PCI_PRODUCT_QLOGIC_ISP2312
248 #define PCI_PRODUCT_QLOGIC_ISP2312 0x2312
251 #ifndef PCI_PRODUCT_QLOGIC_ISP2322
252 #define PCI_PRODUCT_QLOGIC_ISP2322 0x2322
255 #ifndef PCI_PRODUCT_QLOGIC_ISP2422
256 #define PCI_PRODUCT_QLOGIC_ISP2422 0x2422
259 #ifndef PCI_PRODUCT_QLOGIC_ISP2432
260 #define PCI_PRODUCT_QLOGIC_ISP2432 0x2432
263 #ifndef PCI_PRODUCT_QLOGIC_ISP2532
264 #define PCI_PRODUCT_QLOGIC_ISP2532 0x2532
267 #ifndef PCI_PRODUCT_QLOGIC_ISP6312
268 #define PCI_PRODUCT_QLOGIC_ISP6312 0x6312
271 #ifndef PCI_PRODUCT_QLOGIC_ISP6322
272 #define PCI_PRODUCT_QLOGIC_ISP6322 0x6322
275 #ifndef PCI_PRODUCT_QLOGIC_ISP5432
276 #define PCI_PRODUCT_QLOGIC_ISP5432 0x5432
279 #define PCI_QLOGIC_ISP5432 \
280 ((PCI_PRODUCT_QLOGIC_ISP5432 << 16) | PCI_VENDOR_QLOGIC)
282 #define PCI_QLOGIC_ISP1020 \
283 ((PCI_PRODUCT_QLOGIC_ISP1020 << 16) | PCI_VENDOR_QLOGIC)
285 #define PCI_QLOGIC_ISP1080 \
286 ((PCI_PRODUCT_QLOGIC_ISP1080 << 16) | PCI_VENDOR_QLOGIC)
288 #define PCI_QLOGIC_ISP10160 \
289 ((PCI_PRODUCT_QLOGIC_ISP10160 << 16) | PCI_VENDOR_QLOGIC)
291 #define PCI_QLOGIC_ISP12160 \
292 ((PCI_PRODUCT_QLOGIC_ISP12160 << 16) | PCI_VENDOR_QLOGIC)
294 #define PCI_QLOGIC_ISP1240 \
295 ((PCI_PRODUCT_QLOGIC_ISP1240 << 16) | PCI_VENDOR_QLOGIC)
297 #define PCI_QLOGIC_ISP1280 \
298 ((PCI_PRODUCT_QLOGIC_ISP1280 << 16) | PCI_VENDOR_QLOGIC)
300 #define PCI_QLOGIC_ISP2100 \
301 ((PCI_PRODUCT_QLOGIC_ISP2100 << 16) | PCI_VENDOR_QLOGIC)
303 #define PCI_QLOGIC_ISP2200 \
304 ((PCI_PRODUCT_QLOGIC_ISP2200 << 16) | PCI_VENDOR_QLOGIC)
306 #define PCI_QLOGIC_ISP2300 \
307 ((PCI_PRODUCT_QLOGIC_ISP2300 << 16) | PCI_VENDOR_QLOGIC)
309 #define PCI_QLOGIC_ISP2312 \
310 ((PCI_PRODUCT_QLOGIC_ISP2312 << 16) | PCI_VENDOR_QLOGIC)
312 #define PCI_QLOGIC_ISP2322 \
313 ((PCI_PRODUCT_QLOGIC_ISP2322 << 16) | PCI_VENDOR_QLOGIC)
315 #define PCI_QLOGIC_ISP2422 \
316 ((PCI_PRODUCT_QLOGIC_ISP2422 << 16) | PCI_VENDOR_QLOGIC)
318 #define PCI_QLOGIC_ISP2432 \
319 ((PCI_PRODUCT_QLOGIC_ISP2432 << 16) | PCI_VENDOR_QLOGIC)
321 #define PCI_QLOGIC_ISP2532 \
322 ((PCI_PRODUCT_QLOGIC_ISP2532 << 16) | PCI_VENDOR_QLOGIC)
324 #define PCI_QLOGIC_ISP6312 \
325 ((PCI_PRODUCT_QLOGIC_ISP6312 << 16) | PCI_VENDOR_QLOGIC)
327 #define PCI_QLOGIC_ISP6322 \
328 ((PCI_PRODUCT_QLOGIC_ISP6322 << 16) | PCI_VENDOR_QLOGIC)
331 * Odd case for some AMI raid cards... We need to *not* attach to this.
333 #define AMI_RAID_SUBVENDOR_ID 0x101e
335 #define IO_MAP_REG 0x10
336 #define MEM_MAP_REG 0x14
338 #define PCI_DFLT_LTNCY 0x40
339 #define PCI_DFLT_LNSZ 0x10
341 static int isp_pci_probe (device_t);
342 static int isp_pci_attach (device_t);
343 static int isp_pci_detach (device_t);
346 #define ISP_PCD(isp) ((struct isp_pcisoftc *)isp)->pci_dev
347 struct isp_pcisoftc {
350 struct resource * regs;
356 int16_t pci_poff[_NREG_BLKS];
362 static device_method_t isp_pci_methods[] = {
363 /* Device interface */
364 DEVMETHOD(device_probe, isp_pci_probe),
365 DEVMETHOD(device_attach, isp_pci_attach),
366 DEVMETHOD(device_detach, isp_pci_detach),
370 static driver_t isp_pci_driver = {
371 "isp", isp_pci_methods, sizeof (struct isp_pcisoftc)
373 static devclass_t isp_devclass;
374 DRIVER_MODULE(isp, pci, isp_pci_driver, isp_devclass, 0, 0);
377 isp_pci_probe(device_t dev)
379 switch ((pci_get_device(dev) << 16) | (pci_get_vendor(dev))) {
380 case PCI_QLOGIC_ISP1020:
381 device_set_desc(dev, "Qlogic ISP 1020/1040 PCI SCSI Adapter");
383 case PCI_QLOGIC_ISP1080:
384 device_set_desc(dev, "Qlogic ISP 1080 PCI SCSI Adapter");
386 case PCI_QLOGIC_ISP1240:
387 device_set_desc(dev, "Qlogic ISP 1240 PCI SCSI Adapter");
389 case PCI_QLOGIC_ISP1280:
390 device_set_desc(dev, "Qlogic ISP 1280 PCI SCSI Adapter");
392 case PCI_QLOGIC_ISP10160:
393 device_set_desc(dev, "Qlogic ISP 10160 PCI SCSI Adapter");
395 case PCI_QLOGIC_ISP12160:
396 if (pci_get_subvendor(dev) == AMI_RAID_SUBVENDOR_ID) {
399 device_set_desc(dev, "Qlogic ISP 12160 PCI SCSI Adapter");
401 case PCI_QLOGIC_ISP2100:
402 device_set_desc(dev, "Qlogic ISP 2100 PCI FC-AL Adapter");
404 case PCI_QLOGIC_ISP2200:
405 device_set_desc(dev, "Qlogic ISP 2200 PCI FC-AL Adapter");
407 case PCI_QLOGIC_ISP2300:
408 device_set_desc(dev, "Qlogic ISP 2300 PCI FC-AL Adapter");
410 case PCI_QLOGIC_ISP2312:
411 device_set_desc(dev, "Qlogic ISP 2312 PCI FC-AL Adapter");
413 case PCI_QLOGIC_ISP2322:
414 device_set_desc(dev, "Qlogic ISP 2322 PCI FC-AL Adapter");
416 case PCI_QLOGIC_ISP2422:
417 device_set_desc(dev, "Qlogic ISP 2422 PCI FC-AL Adapter");
419 case PCI_QLOGIC_ISP2432:
420 device_set_desc(dev, "Qlogic ISP 2432 PCI FC-AL Adapter");
422 case PCI_QLOGIC_ISP2532:
423 device_set_desc(dev, "Qlogic ISP 2532 PCI FC-AL Adapter");
425 case PCI_QLOGIC_ISP5432:
426 device_set_desc(dev, "Qlogic ISP 5432 PCI FC-AL Adapter");
428 case PCI_QLOGIC_ISP6312:
429 device_set_desc(dev, "Qlogic ISP 6312 PCI FC-AL Adapter");
431 case PCI_QLOGIC_ISP6322:
432 device_set_desc(dev, "Qlogic ISP 6322 PCI FC-AL Adapter");
437 if (isp_announced == 0 && bootverbose) {
438 printf("Qlogic ISP Driver, FreeBSD Version %d.%d, "
439 "Core Version %d.%d\n",
440 ISP_PLATFORM_VERSION_MAJOR, ISP_PLATFORM_VERSION_MINOR,
441 ISP_CORE_VERSION_MAJOR, ISP_CORE_VERSION_MINOR);
445 * XXXX: Here is where we might load the f/w module
446 * XXXX: (or increase a reference count to it).
448 return (BUS_PROBE_DEFAULT);
452 isp_get_generic_options(device_t dev, ispsoftc_t *isp, int *nvp)
457 * Figure out if we're supposed to skip this one.
460 if (resource_int_value(device_get_name(dev), device_get_unit(dev), "disable", &tval) == 0 && tval) {
461 device_printf(dev, "disabled at user request\n");
462 isp->isp_osinfo.disabled = 1;
467 if (resource_int_value(device_get_name(dev), device_get_unit(dev), "fwload_disable", &tval) == 0 && tval != 0) {
468 isp->isp_confopts |= ISP_CFG_NORELOAD;
471 if (resource_int_value(device_get_name(dev), device_get_unit(dev), "ignore_nvram", &tval) == 0 && tval != 0) {
472 isp->isp_confopts |= ISP_CFG_NONVRAM;
475 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), "debug", &tval);
477 isp->isp_dblev = tval;
479 isp->isp_dblev = ISP_LOGWARN|ISP_LOGERR;
482 isp->isp_dblev |= ISP_LOGCONFIG|ISP_LOGINFO;
484 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), "vports", &tval);
485 if (tval > 0 && tval < 127) {
491 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), "autoconfig", &tval);
492 isp_autoconfig = tval;
494 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), "quickboot_time", &tval);
495 isp_quickboot_time = tval;
498 if (resource_int_value(device_get_name(dev), device_get_unit(dev), "forcemulti", &tval) == 0 && tval != 0) {
499 isp->isp_osinfo.forcemulti = 1;
504 isp_get_pci_options(device_t dev, int *m1, int *m2)
508 * Which we should try first - memory mapping or i/o mapping?
510 * We used to try memory first followed by i/o on alpha, otherwise
511 * the reverse, but we should just try memory first all the time now.
513 *m1 = PCIM_CMD_MEMEN;
514 *m2 = PCIM_CMD_PORTEN;
517 if (resource_int_value(device_get_name(dev), device_get_unit(dev), "prefer_iomap", &tval) == 0 && tval != 0) {
518 *m1 = PCIM_CMD_PORTEN;
519 *m2 = PCIM_CMD_MEMEN;
522 if (resource_int_value(device_get_name(dev), device_get_unit(dev), "prefer_memmap", &tval) == 0 && tval != 0) {
523 *m1 = PCIM_CMD_MEMEN;
524 *m2 = PCIM_CMD_PORTEN;
529 isp_get_specific_options(device_t dev, int chan, ispsoftc_t *isp)
534 if (resource_int_value(device_get_name(dev), device_get_unit(dev), "iid", &tval)) {
536 ISP_FC_PC(isp, chan)->default_id = 109 - chan;
539 ISP_SPI_PC(isp, chan)->iid = OF_getscsinitid(dev);
541 ISP_SPI_PC(isp, chan)->iid = 7;
546 ISP_FC_PC(isp, chan)->default_id = tval - chan;
548 ISP_SPI_PC(isp, chan)->iid = tval;
550 isp->isp_confopts |= ISP_CFG_OWNLOOPID;
554 if (resource_int_value(device_get_name(dev), device_get_unit(dev), "role", &tval) == 0) {
557 case ISP_ROLE_INITIATOR:
558 case ISP_ROLE_TARGET:
559 case ISP_ROLE_INITIATOR|ISP_ROLE_TARGET:
560 device_printf(dev, "setting role to 0x%x\n", tval);
568 tval = ISP_DEFAULT_ROLES;
572 ISP_SPI_PC(isp, chan)->def_role = tval;
575 ISP_FC_PC(isp, chan)->def_role = tval;
578 if (resource_int_value(device_get_name(dev), device_get_unit(dev), "fullduplex", &tval) == 0 && tval != 0) {
579 isp->isp_confopts |= ISP_CFG_FULL_DUPLEX;
582 if (resource_string_value(device_get_name(dev), device_get_unit(dev), "topology", (const char **) &sptr) == 0 && sptr != 0) {
583 if (strcmp(sptr, "lport") == 0) {
584 isp->isp_confopts |= ISP_CFG_LPORT;
585 } else if (strcmp(sptr, "nport") == 0) {
586 isp->isp_confopts |= ISP_CFG_NPORT;
587 } else if (strcmp(sptr, "lport-only") == 0) {
588 isp->isp_confopts |= ISP_CFG_LPORT_ONLY;
589 } else if (strcmp(sptr, "nport-only") == 0) {
590 isp->isp_confopts |= ISP_CFG_NPORT_ONLY;
595 * Because the resource_*_value functions can neither return
596 * 64 bit integer values, nor can they be directly coerced
597 * to interpret the right hand side of the assignment as
598 * you want them to interpret it, we have to force WWN
599 * hint replacement to specify WWN strings with a leading
600 * 'w' (e..g w50000000aaaa0001). Sigh.
603 tval = resource_string_value(device_get_name(dev), device_get_unit(dev), "portwwn", (const char **) &sptr);
604 if (tval == 0 && sptr != 0 && *sptr++ == 'w') {
606 ISP_FC_PC(isp, chan)->def_wwpn = strtouq(sptr, &eptr, 16);
607 if (eptr < sptr + 16 || ISP_FC_PC(isp, chan)->def_wwpn == -1) {
608 device_printf(dev, "mangled portwwn hint '%s'\n", sptr);
609 ISP_FC_PC(isp, chan)->def_wwpn = 0;
614 tval = resource_string_value(device_get_name(dev), device_get_unit(dev), "nodewwn", (const char **) &sptr);
615 if (tval == 0 && sptr != 0 && *sptr++ == 'w') {
617 ISP_FC_PC(isp, chan)->def_wwnn = strtouq(sptr, &eptr, 16);
618 if (eptr < sptr + 16 || ISP_FC_PC(isp, chan)->def_wwnn == 0) {
619 device_printf(dev, "mangled nodewwn hint '%s'\n", sptr);
620 ISP_FC_PC(isp, chan)->def_wwnn = 0;
625 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), "hysteresis", &tval);
626 if (tval >= 0 && tval < 256) {
627 ISP_FC_PC(isp, chan)->hysteresis = tval;
629 ISP_FC_PC(isp, chan)->hysteresis = isp_fabric_hysteresis;
633 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), "loop_down_limit", &tval);
634 if (tval >= 0 && tval < 0xffff) {
635 ISP_FC_PC(isp, chan)->loop_down_limit = tval;
637 ISP_FC_PC(isp, chan)->loop_down_limit = isp_loop_down_limit;
641 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), "gone_device_time", &tval);
642 if (tval >= 0 && tval < 0xffff) {
643 ISP_FC_PC(isp, chan)->gone_device_time = tval;
645 ISP_FC_PC(isp, chan)->gone_device_time = isp_gone_device_time;
650 isp_pci_attach(device_t dev)
652 int i, m1, m2, locksetup = 0;
654 uint32_t data, cmd, linesz, did;
655 struct isp_pcisoftc *pcs;
660 pcs = device_get_softc(dev);
662 device_printf(dev, "cannot get softc\n");
665 memset(pcs, 0, sizeof (*pcs));
673 * Get Generic Options
675 isp_get_generic_options(dev, isp, &isp_nvports);
678 * Check to see if options have us disabled
680 if (isp->isp_osinfo.disabled) {
682 * But return zero to preserve unit numbering
688 * Get PCI options- which in this case are just mapping preferences.
690 isp_get_pci_options(dev, &m1, &m2);
692 linesz = PCI_DFLT_LNSZ;
693 pcs->irq = pcs->regs = NULL;
694 pcs->rgd = pcs->rtp = pcs->iqd = 0;
696 cmd = pci_read_config(dev, PCIR_COMMAND, 2);
698 pcs->rtp = (m1 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
699 pcs->rgd = (m1 == PCIM_CMD_MEMEN)? MEM_MAP_REG : IO_MAP_REG;
700 pcs->regs = bus_alloc_resource_any(dev, pcs->rtp, &pcs->rgd, RF_ACTIVE);
702 if (pcs->regs == NULL && (cmd & m2)) {
703 pcs->rtp = (m2 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
704 pcs->rgd = (m2 == PCIM_CMD_MEMEN)? MEM_MAP_REG : IO_MAP_REG;
705 pcs->regs = bus_alloc_resource_any(dev, pcs->rtp, &pcs->rgd, RF_ACTIVE);
707 if (pcs->regs == NULL) {
708 device_printf(dev, "unable to map any ports\n");
712 device_printf(dev, "using %s space register mapping\n", (pcs->rgd == IO_MAP_REG)? "I/O" : "Memory");
714 isp->isp_bus_tag = rman_get_bustag(pcs->regs);
715 isp->isp_bus_handle = rman_get_bushandle(pcs->regs);
718 pcs->pci_poff[BIU_BLOCK >> _BLK_REG_SHFT] = BIU_REGS_OFF;
719 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS_OFF;
720 pcs->pci_poff[SXP_BLOCK >> _BLK_REG_SHFT] = PCI_SXP_REGS_OFF;
721 pcs->pci_poff[RISC_BLOCK >> _BLK_REG_SHFT] = PCI_RISC_REGS_OFF;
722 pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] = DMA_REGS_OFF;
724 switch (pci_get_devid(dev)) {
725 case PCI_QLOGIC_ISP1020:
727 isp->isp_mdvec = &mdvec;
728 isp->isp_type = ISP_HA_SCSI_UNKNOWN;
730 case PCI_QLOGIC_ISP1080:
732 isp->isp_mdvec = &mdvec_1080;
733 isp->isp_type = ISP_HA_SCSI_1080;
734 pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] = ISP1080_DMA_REGS_OFF;
736 case PCI_QLOGIC_ISP1240:
738 isp->isp_mdvec = &mdvec_1080;
739 isp->isp_type = ISP_HA_SCSI_1240;
741 pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] = ISP1080_DMA_REGS_OFF;
743 case PCI_QLOGIC_ISP1280:
745 isp->isp_mdvec = &mdvec_1080;
746 isp->isp_type = ISP_HA_SCSI_1280;
747 pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] = ISP1080_DMA_REGS_OFF;
749 case PCI_QLOGIC_ISP10160:
751 isp->isp_mdvec = &mdvec_12160;
752 isp->isp_type = ISP_HA_SCSI_10160;
753 pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] = ISP1080_DMA_REGS_OFF;
755 case PCI_QLOGIC_ISP12160:
758 isp->isp_mdvec = &mdvec_12160;
759 isp->isp_type = ISP_HA_SCSI_12160;
760 pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] = ISP1080_DMA_REGS_OFF;
762 case PCI_QLOGIC_ISP2100:
764 isp->isp_mdvec = &mdvec_2100;
765 isp->isp_type = ISP_HA_FC_2100;
766 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2100_OFF;
767 if (pci_get_revid(dev) < 3) {
769 * XXX: Need to get the actual revision
770 * XXX: number of the 2100 FB. At any rate,
771 * XXX: lower cache line size for early revision
777 case PCI_QLOGIC_ISP2200:
779 isp->isp_mdvec = &mdvec_2200;
780 isp->isp_type = ISP_HA_FC_2200;
781 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2100_OFF;
783 case PCI_QLOGIC_ISP2300:
785 isp->isp_mdvec = &mdvec_2300;
786 isp->isp_type = ISP_HA_FC_2300;
787 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2300_OFF;
789 case PCI_QLOGIC_ISP2312:
790 case PCI_QLOGIC_ISP6312:
792 isp->isp_mdvec = &mdvec_2300;
793 isp->isp_type = ISP_HA_FC_2312;
794 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2300_OFF;
796 case PCI_QLOGIC_ISP2322:
797 case PCI_QLOGIC_ISP6322:
799 isp->isp_mdvec = &mdvec_2300;
800 isp->isp_type = ISP_HA_FC_2322;
801 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2300_OFF;
803 case PCI_QLOGIC_ISP2422:
804 case PCI_QLOGIC_ISP2432:
806 isp->isp_nchan += isp_nvports;
807 isp->isp_mdvec = &mdvec_2400;
808 isp->isp_type = ISP_HA_FC_2400;
809 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2400_OFF;
811 case PCI_QLOGIC_ISP2532:
813 isp->isp_nchan += isp_nvports;
814 isp->isp_mdvec = &mdvec_2500;
815 isp->isp_type = ISP_HA_FC_2500;
816 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2400_OFF;
818 case PCI_QLOGIC_ISP5432:
820 isp->isp_mdvec = &mdvec_2500;
821 isp->isp_type = ISP_HA_FC_2500;
822 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2400_OFF;
825 device_printf(dev, "unknown device type\n");
829 isp->isp_revision = pci_get_revid(dev);
832 psize = sizeof (fcparam);
833 xsize = sizeof (struct isp_fc);
835 psize = sizeof (sdparam);
836 xsize = sizeof (struct isp_spi);
838 psize *= isp->isp_nchan;
839 xsize *= isp->isp_nchan;
840 isp->isp_param = malloc(psize, M_DEVBUF, M_NOWAIT | M_ZERO);
841 if (isp->isp_param == NULL) {
842 device_printf(dev, "cannot allocate parameter data\n");
845 isp->isp_osinfo.pc.ptr = malloc(xsize, M_DEVBUF, M_NOWAIT | M_ZERO);
846 if (isp->isp_osinfo.pc.ptr == NULL) {
847 device_printf(dev, "cannot allocate parameter data\n");
852 * Now that we know who we are (roughly) get/set specific options
854 for (i = 0; i < isp->isp_nchan; i++) {
855 isp_get_specific_options(dev, i, isp);
859 * The 'it' suffix really only matters for SCSI cards in target mode.
861 isp->isp_osinfo.fw = NULL;
862 if (IS_SCSI(isp) && (ISP_SPI_PC(isp, 0)->def_role & ISP_ROLE_TARGET)) {
863 snprintf(fwname, sizeof (fwname), "isp_%04x_it", did);
864 isp->isp_osinfo.fw = firmware_get(fwname);
865 } else if (IS_24XX(isp) && (isp->isp_nchan > 1 || isp->isp_osinfo.forcemulti)) {
866 snprintf(fwname, sizeof (fwname), "isp_%04x_multi", did);
867 isp->isp_osinfo.fw = firmware_get(fwname);
869 if (isp->isp_osinfo.fw == NULL) {
870 snprintf(fwname, sizeof (fwname), "isp_%04x", did);
871 isp->isp_osinfo.fw = firmware_get(fwname);
873 if (isp->isp_osinfo.fw != NULL) {
874 isp->isp_mdvec->dv_ispfw = isp->isp_osinfo.fw->data;
878 * Make sure that SERR, PERR, WRITE INVALIDATE and BUSMASTER
881 cmd |= PCIM_CMD_SEREN | PCIM_CMD_PERRESPEN |
882 PCIM_CMD_BUSMASTEREN | PCIM_CMD_INVEN;
884 if (IS_2300(isp)) { /* per QLogic errata */
885 cmd &= ~PCIM_CMD_INVEN;
888 if (IS_2322(isp) || pci_get_devid(dev) == PCI_QLOGIC_ISP6312) {
889 cmd &= ~PCIM_CMD_INTX_DISABLE;
893 cmd &= ~PCIM_CMD_INTX_DISABLE;
896 pci_write_config(dev, PCIR_COMMAND, cmd, 2);
899 * Make sure the Cache Line Size register is set sensibly.
901 data = pci_read_config(dev, PCIR_CACHELNSZ, 1);
902 if (data == 0 || (linesz != PCI_DFLT_LNSZ && data != linesz)) {
903 isp_prt(isp, ISP_LOGCONFIG, "set PCI line size to %d from %d", linesz, data);
905 pci_write_config(dev, PCIR_CACHELNSZ, data, 1);
909 * Make sure the Latency Timer is sane.
911 data = pci_read_config(dev, PCIR_LATTIMER, 1);
912 if (data < PCI_DFLT_LTNCY) {
913 data = PCI_DFLT_LTNCY;
914 isp_prt(isp, ISP_LOGCONFIG, "set PCI latency to %d", data);
915 pci_write_config(dev, PCIR_LATTIMER, data, 1);
919 * Make sure we've disabled the ROM.
921 data = pci_read_config(dev, PCIR_ROMADDR, 4);
923 pci_write_config(dev, PCIR_ROMADDR, data, 4);
928 * NB: MSI-X needs to be disabled for the 2432 (PCI-Express)
930 if (IS_24XX(isp) || IS_2322(isp)) {
931 pcs->msicount = pci_msi_count(dev);
932 if (pcs->msicount > 1) {
935 if (pci_alloc_msi(dev, &pcs->msicount) == 0) {
941 pcs->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &pcs->iqd, RF_ACTIVE | RF_SHAREABLE);
942 if (pcs->irq == NULL) {
943 device_printf(dev, "could not allocate interrupt\n");
947 /* Make sure the lock is set up. */
948 mtx_init(&isp->isp_osinfo.lock, "isp", NULL, MTX_DEF);
951 if (isp_setup_intr(dev, pcs->irq, ISP_IFLAGS, NULL, isp_platform_intr, isp, &pcs->ih)) {
952 device_printf(dev, "could not setup interrupt\n");
957 * Last minute checks...
959 if (IS_23XX(isp) || IS_24XX(isp)) {
960 isp->isp_port = pci_get_function(dev);
964 * Make sure we're in reset state.
968 if (isp->isp_state != ISP_RESETSTATE) {
973 if (isp->isp_state == ISP_INITSTATE) {
974 isp->isp_state = ISP_RUNSTATE;
977 if (isp_attach(isp)) {
987 (void) bus_teardown_intr(dev, pcs->irq, pcs->ih);
990 mtx_destroy(&isp->isp_osinfo.lock);
993 (void) bus_release_resource(dev, SYS_RES_IRQ, pcs->iqd, pcs->irq);
996 pci_release_msi(dev);
999 (void) bus_release_resource(dev, pcs->rtp, pcs->rgd, pcs->regs);
1001 if (pcs->pci_isp.isp_param) {
1002 free(pcs->pci_isp.isp_param, M_DEVBUF);
1003 pcs->pci_isp.isp_param = NULL;
1005 if (pcs->pci_isp.isp_osinfo.pc.ptr) {
1006 free(pcs->pci_isp.isp_osinfo.pc.ptr, M_DEVBUF);
1007 pcs->pci_isp.isp_osinfo.pc.ptr = NULL;
1013 isp_pci_detach(device_t dev)
1015 struct isp_pcisoftc *pcs;
1019 pcs = device_get_softc(dev);
1023 isp = (ispsoftc_t *) pcs;
1024 status = isp_detach(isp);
1030 (void) bus_teardown_intr(dev, pcs->irq, pcs->ih);
1033 mtx_destroy(&isp->isp_osinfo.lock);
1034 (void) bus_release_resource(dev, SYS_RES_IRQ, pcs->iqd, pcs->irq);
1035 if (pcs->msicount) {
1036 pci_release_msi(dev);
1038 (void) bus_release_resource(dev, pcs->rtp, pcs->rgd, pcs->regs);
1039 if (pcs->pci_isp.isp_param) {
1040 free(pcs->pci_isp.isp_param, M_DEVBUF);
1041 pcs->pci_isp.isp_param = NULL;
1043 if (pcs->pci_isp.isp_osinfo.pc.ptr) {
1044 free(pcs->pci_isp.isp_osinfo.pc.ptr, M_DEVBUF);
1045 pcs->pci_isp.isp_osinfo.pc.ptr = NULL;
1050 #define IspVirt2Off(a, x) \
1051 (((struct isp_pcisoftc *)a)->pci_poff[((x) & _BLK_REG_MASK) >> \
1052 _BLK_REG_SHFT] + ((x) & 0xfff))
1054 #define BXR2(isp, off) \
1055 bus_space_read_2(isp->isp_bus_tag, isp->isp_bus_handle, off)
1056 #define BXW2(isp, off, v) \
1057 bus_space_write_2(isp->isp_bus_tag, isp->isp_bus_handle, off, v)
1058 #define BXR4(isp, off) \
1059 bus_space_read_4(isp->isp_bus_tag, isp->isp_bus_handle, off)
1060 #define BXW4(isp, off, v) \
1061 bus_space_write_4(isp->isp_bus_tag, isp->isp_bus_handle, off, v)
1064 static ISP_INLINE int
1065 isp_pci_rd_debounced(ispsoftc_t *isp, int off, uint16_t *rp)
1067 uint32_t val0, val1;
1071 val0 = BXR2(isp, IspVirt2Off(isp, off));
1072 val1 = BXR2(isp, IspVirt2Off(isp, off));
1073 } while (val0 != val1 && ++i < 1000);
1082 isp_pci_rd_isr(ispsoftc_t *isp, uint32_t *isrp, uint16_t *semap, uint16_t *mbp)
1087 if (isp_pci_rd_debounced(isp, BIU_ISR, &isr)) {
1090 if (isp_pci_rd_debounced(isp, BIU_SEMA, &sema)) {
1094 isr = BXR2(isp, IspVirt2Off(isp, BIU_ISR));
1095 sema = BXR2(isp, IspVirt2Off(isp, BIU_SEMA));
1097 isp_prt(isp, ISP_LOGDEBUG3, "ISR 0x%x SEMA 0x%x", isr, sema);
1098 isr &= INT_PENDING_MASK(isp);
1099 sema &= BIU_SEMA_LOCK;
1100 if (isr == 0 && sema == 0) {
1104 if ((*semap = sema) != 0) {
1106 if (isp_pci_rd_debounced(isp, OUTMAILBOX0, mbp)) {
1110 *mbp = BXR2(isp, IspVirt2Off(isp, OUTMAILBOX0));
1117 isp_pci_rd_isr_2300(ispsoftc_t *isp, uint32_t *isrp, uint16_t *semap, uint16_t *mbox0p)
1122 if (!(BXR2(isp, IspVirt2Off(isp, BIU_ISR) & BIU2100_ISR_RISC_INT))) {
1126 r2hisr = BXR4(isp, IspVirt2Off(isp, BIU_R2HSTSLO));
1127 isp_prt(isp, ISP_LOGDEBUG3, "RISC2HOST ISR 0x%x", r2hisr);
1128 if ((r2hisr & BIU_R2HST_INTR) == 0) {
1132 switch (r2hisr & BIU_R2HST_ISTAT_MASK) {
1133 case ISPR2HST_ROM_MBX_OK:
1134 case ISPR2HST_ROM_MBX_FAIL:
1135 case ISPR2HST_MBX_OK:
1136 case ISPR2HST_MBX_FAIL:
1137 case ISPR2HST_ASYNC_EVENT:
1138 *isrp = r2hisr & 0xffff;
1139 *mbox0p = (r2hisr >> 16);
1142 case ISPR2HST_RIO_16:
1143 *isrp = r2hisr & 0xffff;
1144 *mbox0p = ASYNC_RIO16_1;
1147 case ISPR2HST_FPOST:
1148 *isrp = r2hisr & 0xffff;
1149 *mbox0p = ASYNC_CMD_CMPLT;
1152 case ISPR2HST_FPOST_CTIO:
1153 *isrp = r2hisr & 0xffff;
1154 *mbox0p = ASYNC_CTIO_DONE;
1157 case ISPR2HST_RSPQ_UPDATE:
1158 *isrp = r2hisr & 0xffff;
1163 hccr = ISP_READ(isp, HCCR);
1164 if (hccr & HCCR_PAUSE) {
1165 ISP_WRITE(isp, HCCR, HCCR_RESET);
1166 isp_prt(isp, ISP_LOGERR, "RISC paused at interrupt (%x->%x)", hccr, ISP_READ(isp, HCCR));
1167 ISP_WRITE(isp, BIU_ICR, 0);
1169 isp_prt(isp, ISP_LOGERR, "unknown interrupt 0x%x\n", r2hisr);
1176 isp_pci_rd_isr_2400(ispsoftc_t *isp, uint32_t *isrp, uint16_t *semap, uint16_t *mbox0p)
1180 r2hisr = BXR4(isp, IspVirt2Off(isp, BIU2400_R2HSTSLO));
1181 isp_prt(isp, ISP_LOGDEBUG3, "RISC2HOST ISR 0x%x", r2hisr);
1182 if ((r2hisr & BIU2400_R2HST_INTR) == 0) {
1186 switch (r2hisr & BIU2400_R2HST_ISTAT_MASK) {
1187 case ISP2400R2HST_ROM_MBX_OK:
1188 case ISP2400R2HST_ROM_MBX_FAIL:
1189 case ISP2400R2HST_MBX_OK:
1190 case ISP2400R2HST_MBX_FAIL:
1191 case ISP2400R2HST_ASYNC_EVENT:
1192 *isrp = r2hisr & 0xffff;
1193 *mbox0p = (r2hisr >> 16);
1196 case ISP2400R2HST_RSPQ_UPDATE:
1197 case ISP2400R2HST_ATIO_RSPQ_UPDATE:
1198 case ISP2400R2HST_ATIO_RQST_UPDATE:
1199 *isrp = r2hisr & 0xffff;
1204 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
1205 isp_prt(isp, ISP_LOGERR, "unknown interrupt 0x%x\n", r2hisr);
1211 isp_pci_rd_reg(ispsoftc_t *isp, int regoff)
1216 if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
1218 * We will assume that someone has paused the RISC processor.
1220 oldconf = BXR2(isp, IspVirt2Off(isp, BIU_CONF1));
1221 BXW2(isp, IspVirt2Off(isp, BIU_CONF1), oldconf | BIU_PCI_CONF1_SXP);
1222 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1224 rv = BXR2(isp, IspVirt2Off(isp, regoff));
1225 if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
1226 BXW2(isp, IspVirt2Off(isp, BIU_CONF1), oldconf);
1227 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1233 isp_pci_wr_reg(ispsoftc_t *isp, int regoff, uint32_t val)
1237 if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
1239 * We will assume that someone has paused the RISC processor.
1241 oldconf = BXR2(isp, IspVirt2Off(isp, BIU_CONF1));
1242 BXW2(isp, IspVirt2Off(isp, BIU_CONF1),
1243 oldconf | BIU_PCI_CONF1_SXP);
1244 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1246 BXW2(isp, IspVirt2Off(isp, regoff), val);
1247 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, regoff), 2, -1);
1248 if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
1249 BXW2(isp, IspVirt2Off(isp, BIU_CONF1), oldconf);
1250 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1256 isp_pci_rd_reg_1080(ispsoftc_t *isp, int regoff)
1258 uint32_t rv, oc = 0;
1260 if ((regoff & _BLK_REG_MASK) == SXP_BLOCK ||
1261 (regoff & _BLK_REG_MASK) == (SXP_BLOCK|SXP_BANK1_SELECT)) {
1264 * We will assume that someone has paused the RISC processor.
1266 oc = BXR2(isp, IspVirt2Off(isp, BIU_CONF1));
1267 tc = oc & ~BIU_PCI1080_CONF1_DMA;
1268 if (regoff & SXP_BANK1_SELECT)
1269 tc |= BIU_PCI1080_CONF1_SXP1;
1271 tc |= BIU_PCI1080_CONF1_SXP0;
1272 BXW2(isp, IspVirt2Off(isp, BIU_CONF1), tc);
1273 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1274 } else if ((regoff & _BLK_REG_MASK) == DMA_BLOCK) {
1275 oc = BXR2(isp, IspVirt2Off(isp, BIU_CONF1));
1276 BXW2(isp, IspVirt2Off(isp, BIU_CONF1),
1277 oc | BIU_PCI1080_CONF1_DMA);
1278 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1280 rv = BXR2(isp, IspVirt2Off(isp, regoff));
1282 BXW2(isp, IspVirt2Off(isp, BIU_CONF1), oc);
1283 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1289 isp_pci_wr_reg_1080(ispsoftc_t *isp, int regoff, uint32_t val)
1293 if ((regoff & _BLK_REG_MASK) == SXP_BLOCK ||
1294 (regoff & _BLK_REG_MASK) == (SXP_BLOCK|SXP_BANK1_SELECT)) {
1297 * We will assume that someone has paused the RISC processor.
1299 oc = BXR2(isp, IspVirt2Off(isp, BIU_CONF1));
1300 tc = oc & ~BIU_PCI1080_CONF1_DMA;
1301 if (regoff & SXP_BANK1_SELECT)
1302 tc |= BIU_PCI1080_CONF1_SXP1;
1304 tc |= BIU_PCI1080_CONF1_SXP0;
1305 BXW2(isp, IspVirt2Off(isp, BIU_CONF1), tc);
1306 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1307 } else if ((regoff & _BLK_REG_MASK) == DMA_BLOCK) {
1308 oc = BXR2(isp, IspVirt2Off(isp, BIU_CONF1));
1309 BXW2(isp, IspVirt2Off(isp, BIU_CONF1),
1310 oc | BIU_PCI1080_CONF1_DMA);
1311 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1313 BXW2(isp, IspVirt2Off(isp, regoff), val);
1314 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, regoff), 2, -1);
1316 BXW2(isp, IspVirt2Off(isp, BIU_CONF1), oc);
1317 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1322 isp_pci_rd_reg_2400(ispsoftc_t *isp, int regoff)
1325 int block = regoff & _BLK_REG_MASK;
1331 return (BXR2(isp, IspVirt2Off(isp, regoff)));
1333 isp_prt(isp, ISP_LOGWARN, "SXP_BLOCK read at 0x%x", regoff);
1334 return (0xffffffff);
1336 isp_prt(isp, ISP_LOGWARN, "RISC_BLOCK read at 0x%x", regoff);
1337 return (0xffffffff);
1339 isp_prt(isp, ISP_LOGWARN, "DMA_BLOCK read at 0x%x", regoff);
1340 return (0xffffffff);
1342 isp_prt(isp, ISP_LOGWARN, "unknown block read at 0x%x", regoff);
1343 return (0xffffffff);
1348 case BIU2400_FLASH_ADDR:
1349 case BIU2400_FLASH_DATA:
1353 case BIU2400_REQINP:
1354 case BIU2400_REQOUTP:
1355 case BIU2400_RSPINP:
1356 case BIU2400_RSPOUTP:
1357 case BIU2400_PRI_REQINP:
1358 case BIU2400_PRI_REQOUTP:
1359 case BIU2400_ATIO_RSPINP:
1360 case BIU2400_ATIO_RSPOUTP:
1365 rv = BXR4(isp, IspVirt2Off(isp, regoff));
1367 case BIU2400_R2HSTSLO:
1368 rv = BXR4(isp, IspVirt2Off(isp, regoff));
1370 case BIU2400_R2HSTSHI:
1371 rv = BXR4(isp, IspVirt2Off(isp, regoff)) >> 16;
1374 isp_prt(isp, ISP_LOGERR,
1375 "isp_pci_rd_reg_2400: unknown offset %x", regoff);
1383 isp_pci_wr_reg_2400(ispsoftc_t *isp, int regoff, uint32_t val)
1385 int block = regoff & _BLK_REG_MASK;
1391 BXW2(isp, IspVirt2Off(isp, regoff), val);
1392 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, regoff), 2, -1);
1395 isp_prt(isp, ISP_LOGWARN, "SXP_BLOCK write at 0x%x", regoff);
1398 isp_prt(isp, ISP_LOGWARN, "RISC_BLOCK write at 0x%x", regoff);
1401 isp_prt(isp, ISP_LOGWARN, "DMA_BLOCK write at 0x%x", regoff);
1404 isp_prt(isp, ISP_LOGWARN, "unknown block write at 0x%x",
1410 case BIU2400_FLASH_ADDR:
1411 case BIU2400_FLASH_DATA:
1415 case BIU2400_REQINP:
1416 case BIU2400_REQOUTP:
1417 case BIU2400_RSPINP:
1418 case BIU2400_RSPOUTP:
1419 case BIU2400_PRI_REQINP:
1420 case BIU2400_PRI_REQOUTP:
1421 case BIU2400_ATIO_RSPINP:
1422 case BIU2400_ATIO_RSPOUTP:
1427 BXW4(isp, IspVirt2Off(isp, regoff), val);
1428 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, regoff), 4, -1);
1431 isp_prt(isp, ISP_LOGERR,
1432 "isp_pci_wr_reg_2400: bad offset 0x%x", regoff);
1445 static void imc(void *, bus_dma_segment_t *, int, int);
1446 static void imc1(void *, bus_dma_segment_t *, int, int);
1449 imc(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1451 struct imush *imushp = (struct imush *) arg;
1454 imushp->error = error;
1458 imushp->error = EINVAL;
1461 imushp->isp->isp_rquest = imushp->vbase;
1462 imushp->isp->isp_rquest_dma = segs->ds_addr;
1463 segs->ds_addr += ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(imushp->isp));
1464 imushp->vbase += ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(imushp->isp));
1465 imushp->isp->isp_result_dma = segs->ds_addr;
1466 imushp->isp->isp_result = imushp->vbase;
1468 #ifdef ISP_TARGET_MODE
1469 if (IS_24XX(imushp->isp)) {
1470 segs->ds_addr += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(imushp->isp));
1471 imushp->vbase += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(imushp->isp));
1472 imushp->isp->isp_atioq_dma = segs->ds_addr;
1473 imushp->isp->isp_atioq = imushp->vbase;
1479 imc1(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1481 struct imush *imushp = (struct imush *) arg;
1483 imushp->error = error;
1487 imushp->error = EINVAL;
1490 FCPARAM(imushp->isp, imushp->chan)->isp_scdma = segs->ds_addr;
1491 FCPARAM(imushp->isp, imushp->chan)->isp_scratch = imushp->vbase;
1495 isp_pci_mbxdma(ispsoftc_t *isp)
1499 int i, error, ns, cmap = 0;
1500 bus_size_t slim; /* segment size */
1501 bus_addr_t llim; /* low limit of unavailable dma */
1502 bus_addr_t hlim; /* high limit of unavailable dma */
1506 * Already been here? If so, leave...
1508 if (isp->isp_rquest) {
1513 if (isp->isp_maxcmds == 0) {
1514 isp_prt(isp, ISP_LOGERR, "maxcmds not set");
1519 hlim = BUS_SPACE_MAXADDR;
1520 if (IS_ULTRA2(isp) || IS_FC(isp) || IS_1240(isp)) {
1521 if (sizeof (bus_size_t) > 4) {
1522 slim = (bus_size_t) (1ULL << 32);
1524 slim = (bus_size_t) (1UL << 31);
1526 llim = BUS_SPACE_MAXADDR;
1528 llim = BUS_SPACE_MAXADDR_32BIT;
1532 len = isp->isp_maxcmds * sizeof (struct isp_pcmd);
1533 isp->isp_osinfo.pcmd_pool = (struct isp_pcmd *) malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
1534 if (isp->isp_osinfo.pcmd_pool == NULL) {
1535 isp_prt(isp, ISP_LOGERR, "cannot allocate pcmds");
1541 * XXX: We don't really support 64 bit target mode for parallel scsi yet
1543 #ifdef ISP_TARGET_MODE
1544 if (IS_SCSI(isp) && sizeof (bus_addr_t) > 4) {
1545 free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
1546 isp_prt(isp, ISP_LOGERR, "we cannot do DAC for SPI cards yet");
1552 if (isp_dma_tag_create(BUS_DMA_ROOTARG(ISP_PCD(isp)), 1, slim, llim, hlim, NULL, NULL, BUS_SPACE_MAXSIZE, ISP_NSEGS, slim, 0, &isp->isp_osinfo.dmat)) {
1553 free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
1555 isp_prt(isp, ISP_LOGERR, "could not create master dma tag");
1559 len = sizeof (isp_hdl_t) * isp->isp_maxcmds;
1560 isp->isp_xflist = (isp_hdl_t *) malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
1561 if (isp->isp_xflist == NULL) {
1562 free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
1564 isp_prt(isp, ISP_LOGERR, "cannot alloc xflist array");
1567 for (len = 0; len < isp->isp_maxcmds - 1; len++) {
1568 isp->isp_xflist[len].cmd = &isp->isp_xflist[len+1];
1570 isp->isp_xffree = isp->isp_xflist;
1571 #ifdef ISP_TARGET_MODE
1572 len = sizeof (isp_hdl_t) * isp->isp_maxcmds;
1573 isp->isp_tgtlist = (isp_hdl_t *) malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
1574 if (isp->isp_tgtlist == NULL) {
1575 free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
1576 free(isp->isp_xflist, M_DEVBUF);
1578 isp_prt(isp, ISP_LOGERR, "cannot alloc tgtlist array");
1581 for (len = 0; len < isp->isp_maxcmds - 1; len++) {
1582 isp->isp_tgtlist[len].cmd = &isp->isp_tgtlist[len+1];
1584 isp->isp_tgtfree = isp->isp_tgtlist;
1588 * Allocate and map the request and result queues (and ATIO queue
1589 * if we're a 2400 supporting target mode).
1591 len = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
1592 len += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
1593 #ifdef ISP_TARGET_MODE
1595 len += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
1599 ns = (len / PAGE_SIZE) + 1;
1602 * Create a tag for the control spaces. We don't always need this
1603 * to be 32 bits, but we do this for simplicity and speed's sake.
1605 if (isp_dma_tag_create(isp->isp_osinfo.dmat, QENTRY_LEN, slim, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, len, ns, slim, 0, &isp->isp_osinfo.cdmat)) {
1606 isp_prt(isp, ISP_LOGERR, "cannot create a dma tag for control spaces");
1607 free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
1608 free(isp->isp_xflist, M_DEVBUF);
1609 #ifdef ISP_TARGET_MODE
1610 free(isp->isp_tgtlist, M_DEVBUF);
1616 if (bus_dmamem_alloc(isp->isp_osinfo.cdmat, (void **)&base, BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &isp->isp_osinfo.cdmap) != 0) {
1617 isp_prt(isp, ISP_LOGERR, "cannot allocate %d bytes of CCB memory", len);
1618 bus_dma_tag_destroy(isp->isp_osinfo.cdmat);
1619 free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
1620 free(isp->isp_xflist, M_DEVBUF);
1621 #ifdef ISP_TARGET_MODE
1622 free(isp->isp_tgtlist, M_DEVBUF);
1633 bus_dmamap_load(isp->isp_osinfo.cdmat, isp->isp_osinfo.cdmap, base, len, imc, &im, 0);
1635 isp_prt(isp, ISP_LOGERR, "error %d loading dma map for control areas", im.error);
1640 for (cmap = 0; cmap < isp->isp_nchan; cmap++) {
1641 struct isp_fc *fc = ISP_FC_PC(isp, cmap);
1642 if (isp_dma_tag_create(isp->isp_osinfo.dmat, 64, slim, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, ISP_FC_SCRLEN, 1, slim, 0, &fc->tdmat)) {
1645 if (bus_dmamem_alloc(fc->tdmat, (void **)&base, BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &fc->tdmap) != 0) {
1646 bus_dma_tag_destroy(fc->tdmat);
1653 bus_dmamap_load(fc->tdmat, fc->tdmap, base, ISP_FC_SCRLEN, imc1, &im, 0);
1655 bus_dmamem_free(fc->tdmat, base, fc->tdmap);
1656 bus_dma_tag_destroy(fc->tdmat);
1662 for (i = 0; i < isp->isp_maxcmds; i++) {
1663 struct isp_pcmd *pcmd = &isp->isp_osinfo.pcmd_pool[i];
1664 error = bus_dmamap_create(isp->isp_osinfo.dmat, 0, &pcmd->dmap);
1666 isp_prt(isp, ISP_LOGERR, "error %d creating per-cmd DMA maps", error);
1668 bus_dmamap_destroy(isp->isp_osinfo.dmat, isp->isp_osinfo.pcmd_pool[i].dmap);
1672 callout_init_mtx(&pcmd->wdog, &isp->isp_osinfo.lock, 0);
1673 if (i == isp->isp_maxcmds-1) {
1676 pcmd->next = &isp->isp_osinfo.pcmd_pool[i+1];
1679 isp->isp_osinfo.pcmd_free = &isp->isp_osinfo.pcmd_pool[0];
1684 while (--cmap >= 0) {
1685 struct isp_fc *fc = ISP_FC_PC(isp, cmap);
1686 bus_dmamem_free(fc->tdmat, base, fc->tdmap);
1687 bus_dma_tag_destroy(fc->tdmat);
1689 bus_dmamem_free(isp->isp_osinfo.cdmat, base, isp->isp_osinfo.cdmap);
1690 bus_dma_tag_destroy(isp->isp_osinfo.cdmat);
1691 free(isp->isp_xflist, M_DEVBUF);
1692 #ifdef ISP_TARGET_MODE
1693 free(isp->isp_tgtlist, M_DEVBUF);
1695 free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
1696 isp->isp_rquest = NULL;
1704 void *rq; /* original request */
1709 #define MUSHERR_NOQENTRIES -2
1711 #ifdef ISP_TARGET_MODE
1712 static void tdma2_2(void *, bus_dma_segment_t *, int, bus_size_t, int);
1713 static void tdma2(void *, bus_dma_segment_t *, int, int);
1716 tdma2_2(void *arg, bus_dma_segment_t *dm_segs, int nseg, bus_size_t mapsize, int error)
1720 mp->mapsize = mapsize;
1721 tdma2(arg, dm_segs, nseg, error);
1725 tdma2(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1729 struct ccb_scsiio *csio;
1733 mp = (mush_t *) arg;
1738 csio = mp->cmd_token;
1742 if (sizeof (bus_addr_t) > 4) {
1743 if (nseg >= ISP_NSEG64_MAX) {
1744 isp_prt(isp, ISP_LOGERR, "number of segments (%d) exceed maximum we can support (%d)", nseg, ISP_NSEG64_MAX);
1748 if (rq->req_header.rqs_entry_type == RQSTYPE_CTIO2) {
1749 rq->req_header.rqs_entry_type = RQSTYPE_CTIO3;
1752 if (nseg >= ISP_NSEG_MAX) {
1753 isp_prt(isp, ISP_LOGERR, "number of segments (%d) exceed maximum we can support (%d)", nseg, ISP_NSEG_MAX);
1758 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1759 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_PREWRITE);
1760 ddir = ISP_TO_DEVICE;
1761 } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
1762 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_PREREAD);
1763 ddir = ISP_FROM_DEVICE;
1775 if (isp_send_tgt_cmd(isp, rq, dm_segs, nseg, XS_XFRLEN(csio), ddir, &csio->sense_data, csio->sense_len) != CMD_QUEUED) {
1776 mp->error = MUSHERR_NOQENTRIES;
1781 static void dma2_2(void *, bus_dma_segment_t *, int, bus_size_t, int);
1782 static void dma2(void *, bus_dma_segment_t *, int, int);
1785 dma2_2(void *arg, bus_dma_segment_t *dm_segs, int nseg, bus_size_t mapsize, int error)
1789 mp->mapsize = mapsize;
1790 dma2(arg, dm_segs, nseg, error);
1794 dma2(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1798 struct ccb_scsiio *csio;
1802 mp = (mush_t *) arg;
1807 csio = mp->cmd_token;
1811 if (sizeof (bus_addr_t) > 4) {
1812 if (nseg >= ISP_NSEG64_MAX) {
1813 isp_prt(isp, ISP_LOGERR, "number of segments (%d) exceed maximum we can support (%d)", nseg, ISP_NSEG64_MAX);
1817 if (rq->req_header.rqs_entry_type == RQSTYPE_T2RQS) {
1818 rq->req_header.rqs_entry_type = RQSTYPE_T3RQS;
1819 } else if (rq->req_header.rqs_entry_type == RQSTYPE_REQUEST) {
1820 rq->req_header.rqs_entry_type = RQSTYPE_A64;
1823 if (nseg >= ISP_NSEG_MAX) {
1824 isp_prt(isp, ISP_LOGERR, "number of segments (%d) exceed maximum we can support (%d)", nseg, ISP_NSEG_MAX);
1829 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1830 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_PREREAD);
1831 ddir = ISP_FROM_DEVICE;
1832 } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
1833 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_PREWRITE);
1834 ddir = ISP_TO_DEVICE;
1844 if (isp_send_cmd(isp, rq, dm_segs, nseg, XS_XFRLEN(csio), ddir) != CMD_QUEUED) {
1845 mp->error = MUSHERR_NOQENTRIES;
1850 isp_pci_dmasetup(ispsoftc_t *isp, struct ccb_scsiio *csio, void *ff)
1853 void (*eptr)(void *, bus_dma_segment_t *, int, int);
1854 void (*eptr2)(void *, bus_dma_segment_t *, int, bus_size_t, int);
1858 mp->cmd_token = csio;
1863 #ifdef ISP_TARGET_MODE
1864 if (csio->ccb_h.func_code == XPT_CONT_TARGET_IO) {
1875 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE || (csio->dxfer_len == 0)) {
1876 (*eptr)(mp, NULL, 0, 0);
1877 } else if ((csio->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
1878 if ((csio->ccb_h.flags & CAM_DATA_PHYS) == 0) {
1880 error = bus_dmamap_load(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, csio->data_ptr, csio->dxfer_len, eptr, mp, 0);
1882 xpt_print(csio->ccb_h.path, "%s: bus_dmamap_load " "ptr %p len %d returned %d\n", __func__, csio->data_ptr, csio->dxfer_len, error);
1885 if (error == EINPROGRESS) {
1886 bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
1888 isp_prt(isp, ISP_LOGERR, "deferred dma allocation not supported");
1889 } else if (error && mp->error == 0) {
1891 isp_prt(isp, ISP_LOGERR, "error %d in dma mapping code", error);
1896 /* Pointer to physical buffer */
1897 struct bus_dma_segment seg;
1898 seg.ds_addr = (bus_addr_t)(vm_offset_t)csio->data_ptr;
1899 seg.ds_len = csio->dxfer_len;
1900 (*eptr)(mp, &seg, 1, 0);
1903 struct bus_dma_segment *segs;
1905 if ((csio->ccb_h.flags & CAM_DATA_PHYS) != 0) {
1906 isp_prt(isp, ISP_LOGERR, "Physical segment pointers unsupported");
1908 } else if ((csio->ccb_h.flags & CAM_SG_LIST_PHYS) == 0) {
1913 * We're taking advantage of the fact that
1914 * the pointer/length sizes and layout of the iovec
1915 * structure are the same as the bus_dma_segment
1916 * structure. This might be a little dangerous,
1917 * but only if they change the structures, which
1920 KASSERT((sizeof (sguio.uio_iov) == sizeof (csio->data_ptr) &&
1921 sizeof (sguio.uio_iovcnt) >= sizeof (csio->sglist_cnt) &&
1922 sizeof (sguio.uio_resid) >= sizeof (csio->dxfer_len)), ("Ken's assumption failed"));
1923 sguio.uio_iov = (struct iovec *)csio->data_ptr;
1924 sguio.uio_iovcnt = csio->sglist_cnt;
1925 sguio.uio_resid = csio->dxfer_len;
1926 sguio.uio_segflg = UIO_SYSSPACE;
1928 error = bus_dmamap_load_uio(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, &sguio, eptr2, mp, 0);
1930 if (error != 0 && mp->error == 0) {
1931 isp_prt(isp, ISP_LOGERR, "error %d in dma mapping code", error);
1935 /* Just use the segments provided */
1936 segs = (struct bus_dma_segment *) csio->data_ptr;
1937 (*eptr)(mp, segs, csio->sglist_cnt, 0);
1941 int retval = CMD_COMPLETE;
1942 if (mp->error == MUSHERR_NOQENTRIES) {
1943 retval = CMD_EAGAIN;
1944 } else if (mp->error == EFBIG) {
1945 XS_SETERR(csio, CAM_REQ_TOO_BIG);
1946 } else if (mp->error == EINVAL) {
1947 XS_SETERR(csio, CAM_REQ_INVALID);
1949 XS_SETERR(csio, CAM_UNREC_HBA_ERROR);
1953 return (CMD_QUEUED);
1957 isp_pci_reset0(ispsoftc_t *isp)
1959 ISP_DISABLE_INTS(isp);
1963 isp_pci_reset1(ispsoftc_t *isp)
1965 if (!IS_24XX(isp)) {
1966 /* Make sure the BIOS is disabled */
1967 isp_pci_wr_reg(isp, HCCR, PCI_HCCR_CMD_BIOS);
1969 /* and enable interrupts */
1970 ISP_ENABLE_INTS(isp);
1974 isp_pci_dumpregs(ispsoftc_t *isp, const char *msg)
1976 struct isp_pcisoftc *pcs = (struct isp_pcisoftc *)isp;
1978 printf("%s: %s\n", device_get_nameunit(isp->isp_dev), msg);
1980 printf("%s:\n", device_get_nameunit(isp->isp_dev));
1982 printf(" biu_conf1=%x", ISP_READ(isp, BIU_CONF1));
1984 printf(" biu_csr=%x", ISP_READ(isp, BIU2100_CSR));
1985 printf(" biu_icr=%x biu_isr=%x biu_sema=%x ", ISP_READ(isp, BIU_ICR),
1986 ISP_READ(isp, BIU_ISR), ISP_READ(isp, BIU_SEMA));
1987 printf("risc_hccr=%x\n", ISP_READ(isp, HCCR));
1991 ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
1992 printf(" cdma_conf=%x cdma_sts=%x cdma_fifostat=%x\n",
1993 ISP_READ(isp, CDMA_CONF), ISP_READ(isp, CDMA_STATUS),
1994 ISP_READ(isp, CDMA_FIFO_STS));
1995 printf(" ddma_conf=%x ddma_sts=%x ddma_fifostat=%x\n",
1996 ISP_READ(isp, DDMA_CONF), ISP_READ(isp, DDMA_STATUS),
1997 ISP_READ(isp, DDMA_FIFO_STS));
1998 printf(" sxp_int=%x sxp_gross=%x sxp(scsi_ctrl)=%x\n",
1999 ISP_READ(isp, SXP_INTERRUPT),
2000 ISP_READ(isp, SXP_GROSS_ERR),
2001 ISP_READ(isp, SXP_PINS_CTRL));
2002 ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
2004 printf(" mbox regs: %x %x %x %x %x\n",
2005 ISP_READ(isp, OUTMAILBOX0), ISP_READ(isp, OUTMAILBOX1),
2006 ISP_READ(isp, OUTMAILBOX2), ISP_READ(isp, OUTMAILBOX3),
2007 ISP_READ(isp, OUTMAILBOX4));
2008 printf(" PCI Status Command/Status=%x\n",
2009 pci_read_config(pcs->pci_dev, PCIR_COMMAND, 1));