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);
375 MODULE_DEPEND(isp, cam, 1, 1, 1);
376 MODULE_DEPEND(isp, firmware, 1, 1, 1);
377 static int isp_nvports = 0;
380 isp_pci_probe(device_t dev)
382 switch ((pci_get_device(dev) << 16) | (pci_get_vendor(dev))) {
383 case PCI_QLOGIC_ISP1020:
384 device_set_desc(dev, "Qlogic ISP 1020/1040 PCI SCSI Adapter");
386 case PCI_QLOGIC_ISP1080:
387 device_set_desc(dev, "Qlogic ISP 1080 PCI SCSI Adapter");
389 case PCI_QLOGIC_ISP1240:
390 device_set_desc(dev, "Qlogic ISP 1240 PCI SCSI Adapter");
392 case PCI_QLOGIC_ISP1280:
393 device_set_desc(dev, "Qlogic ISP 1280 PCI SCSI Adapter");
395 case PCI_QLOGIC_ISP10160:
396 device_set_desc(dev, "Qlogic ISP 10160 PCI SCSI Adapter");
398 case PCI_QLOGIC_ISP12160:
399 if (pci_get_subvendor(dev) == AMI_RAID_SUBVENDOR_ID) {
402 device_set_desc(dev, "Qlogic ISP 12160 PCI SCSI Adapter");
404 case PCI_QLOGIC_ISP2100:
405 device_set_desc(dev, "Qlogic ISP 2100 PCI FC-AL Adapter");
407 case PCI_QLOGIC_ISP2200:
408 device_set_desc(dev, "Qlogic ISP 2200 PCI FC-AL Adapter");
410 case PCI_QLOGIC_ISP2300:
411 device_set_desc(dev, "Qlogic ISP 2300 PCI FC-AL Adapter");
413 case PCI_QLOGIC_ISP2312:
414 device_set_desc(dev, "Qlogic ISP 2312 PCI FC-AL Adapter");
416 case PCI_QLOGIC_ISP2322:
417 device_set_desc(dev, "Qlogic ISP 2322 PCI FC-AL Adapter");
419 case PCI_QLOGIC_ISP2422:
420 device_set_desc(dev, "Qlogic ISP 2422 PCI FC-AL Adapter");
422 case PCI_QLOGIC_ISP2432:
423 device_set_desc(dev, "Qlogic ISP 2432 PCI FC-AL Adapter");
425 case PCI_QLOGIC_ISP2532:
426 device_set_desc(dev, "Qlogic ISP 2532 PCI FC-AL Adapter");
428 case PCI_QLOGIC_ISP5432:
429 device_set_desc(dev, "Qlogic ISP 5432 PCI FC-AL Adapter");
431 case PCI_QLOGIC_ISP6312:
432 device_set_desc(dev, "Qlogic ISP 6312 PCI FC-AL Adapter");
434 case PCI_QLOGIC_ISP6322:
435 device_set_desc(dev, "Qlogic ISP 6322 PCI FC-AL Adapter");
440 if (isp_announced == 0 && bootverbose) {
441 printf("Qlogic ISP Driver, FreeBSD Version %d.%d, "
442 "Core Version %d.%d\n",
443 ISP_PLATFORM_VERSION_MAJOR, ISP_PLATFORM_VERSION_MINOR,
444 ISP_CORE_VERSION_MAJOR, ISP_CORE_VERSION_MINOR);
448 * XXXX: Here is where we might load the f/w module
449 * XXXX: (or increase a reference count to it).
451 return (BUS_PROBE_DEFAULT);
455 isp_get_generic_options(device_t dev, ispsoftc_t *isp)
460 * Figure out if we're supposed to skip this one.
463 if (resource_int_value(device_get_name(dev), device_get_unit(dev), "disable", &tval) == 0 && tval) {
464 device_printf(dev, "disabled at user request\n");
465 isp->isp_osinfo.disabled = 1;
470 if (resource_int_value(device_get_name(dev), device_get_unit(dev), "fwload_disable", &tval) == 0 && tval != 0) {
471 isp->isp_confopts |= ISP_CFG_NORELOAD;
474 if (resource_int_value(device_get_name(dev), device_get_unit(dev), "ignore_nvram", &tval) == 0 && tval != 0) {
475 isp->isp_confopts |= ISP_CFG_NONVRAM;
478 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), "debug", &tval);
480 isp->isp_dblev = tval;
482 isp->isp_dblev = ISP_LOGWARN|ISP_LOGERR;
485 isp->isp_dblev |= ISP_LOGCONFIG|ISP_LOGINFO;
488 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), "vports", &tval);
489 if (tval > 0 && tval < 127) {
493 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), "autoconfig", &tval);
494 isp_autoconfig = tval;
496 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), "quickboot_time", &tval);
497 isp_quickboot_time = tval;
501 isp_get_pci_options(device_t dev, int *m1, int *m2)
505 * Which we should try first - memory mapping or i/o mapping?
507 * We used to try memory first followed by i/o on alpha, otherwise
508 * the reverse, but we should just try memory first all the time now.
510 *m1 = PCIM_CMD_MEMEN;
511 *m2 = PCIM_CMD_PORTEN;
514 if (resource_int_value(device_get_name(dev), device_get_unit(dev), "prefer_iomap", &tval) == 0 && tval != 0) {
515 *m1 = PCIM_CMD_PORTEN;
516 *m2 = PCIM_CMD_MEMEN;
519 if (resource_int_value(device_get_name(dev), device_get_unit(dev), "prefer_memmap", &tval) == 0 && tval != 0) {
520 *m1 = PCIM_CMD_MEMEN;
521 *m2 = PCIM_CMD_PORTEN;
526 isp_get_specific_options(device_t dev, int chan, ispsoftc_t *isp)
531 if (resource_int_value(device_get_name(dev), device_get_unit(dev), "iid", &tval)) {
533 ISP_FC_PC(isp, chan)->default_id = 109 - chan;
536 ISP_SPI_PC(isp, chan)->iid = OF_getscsinitid(dev);
538 ISP_SPI_PC(isp, chan)->iid = 7;
543 ISP_FC_PC(isp, chan)->default_id = tval - chan;
545 ISP_SPI_PC(isp, chan)->iid = tval;
547 isp->isp_confopts |= ISP_CFG_OWNLOOPID;
551 if (resource_int_value(device_get_name(dev), device_get_unit(dev), "role", &tval) == 0) {
554 case ISP_ROLE_INITIATOR:
555 case ISP_ROLE_TARGET:
556 case ISP_ROLE_INITIATOR|ISP_ROLE_TARGET:
557 device_printf(dev, "setting role to 0x%x\n", tval);
565 tval = ISP_DEFAULT_ROLES;
569 ISP_SPI_PC(isp, chan)->def_role = tval;
572 ISP_FC_PC(isp, chan)->def_role = tval;
575 if (resource_int_value(device_get_name(dev), device_get_unit(dev), "fullduplex", &tval) == 0 && tval != 0) {
576 isp->isp_confopts |= ISP_CFG_FULL_DUPLEX;
579 if (resource_string_value(device_get_name(dev), device_get_unit(dev), "topology", (const char **) &sptr) == 0 && sptr != 0) {
580 if (strcmp(sptr, "lport") == 0) {
581 isp->isp_confopts |= ISP_CFG_LPORT;
582 } else if (strcmp(sptr, "nport") == 0) {
583 isp->isp_confopts |= ISP_CFG_NPORT;
584 } else if (strcmp(sptr, "lport-only") == 0) {
585 isp->isp_confopts |= ISP_CFG_LPORT_ONLY;
586 } else if (strcmp(sptr, "nport-only") == 0) {
587 isp->isp_confopts |= ISP_CFG_NPORT_ONLY;
592 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), "nofctape", &tval);
594 isp->isp_confopts |= ISP_CFG_NOFCTAPE;
598 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), "fctape", &tval);
600 isp->isp_confopts &= ~ISP_CFG_NOFCTAPE;
601 isp->isp_confopts |= ISP_CFG_FCTAPE;
606 * Because the resource_*_value functions can neither return
607 * 64 bit integer values, nor can they be directly coerced
608 * to interpret the right hand side of the assignment as
609 * you want them to interpret it, we have to force WWN
610 * hint replacement to specify WWN strings with a leading
611 * 'w' (e..g w50000000aaaa0001). Sigh.
614 tval = resource_string_value(device_get_name(dev), device_get_unit(dev), "portwwn", (const char **) &sptr);
615 if (tval == 0 && sptr != 0 && *sptr++ == 'w') {
617 ISP_FC_PC(isp, chan)->def_wwpn = strtouq(sptr, &eptr, 16);
618 if (eptr < sptr + 16 || ISP_FC_PC(isp, chan)->def_wwpn == -1) {
619 device_printf(dev, "mangled portwwn hint '%s'\n", sptr);
620 ISP_FC_PC(isp, chan)->def_wwpn = 0;
625 tval = resource_string_value(device_get_name(dev), device_get_unit(dev), "nodewwn", (const char **) &sptr);
626 if (tval == 0 && sptr != 0 && *sptr++ == 'w') {
628 ISP_FC_PC(isp, chan)->def_wwnn = strtouq(sptr, &eptr, 16);
629 if (eptr < sptr + 16 || ISP_FC_PC(isp, chan)->def_wwnn == 0) {
630 device_printf(dev, "mangled nodewwn hint '%s'\n", sptr);
631 ISP_FC_PC(isp, chan)->def_wwnn = 0;
636 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), "hysteresis", &tval);
637 if (tval >= 0 && tval < 256) {
638 ISP_FC_PC(isp, chan)->hysteresis = tval;
640 ISP_FC_PC(isp, chan)->hysteresis = isp_fabric_hysteresis;
644 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), "loop_down_limit", &tval);
645 if (tval >= 0 && tval < 0xffff) {
646 ISP_FC_PC(isp, chan)->loop_down_limit = tval;
648 ISP_FC_PC(isp, chan)->loop_down_limit = isp_loop_down_limit;
652 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), "gone_device_time", &tval);
653 if (tval >= 0 && tval < 0xffff) {
654 ISP_FC_PC(isp, chan)->gone_device_time = tval;
656 ISP_FC_PC(isp, chan)->gone_device_time = isp_gone_device_time;
661 isp_pci_attach(device_t dev)
663 int i, m1, m2, locksetup = 0;
664 uint32_t data, cmd, linesz, did;
665 struct isp_pcisoftc *pcs;
670 pcs = device_get_softc(dev);
672 device_printf(dev, "cannot get softc\n");
675 memset(pcs, 0, sizeof (*pcs));
681 if (sizeof (bus_addr_t) > 4)
682 isp->isp_osinfo.sixtyfourbit = 1;
685 * Get Generic Options
688 isp_get_generic_options(dev, isp);
691 * Check to see if options have us disabled
693 if (isp->isp_osinfo.disabled) {
695 * But return zero to preserve unit numbering
701 * Get PCI options- which in this case are just mapping preferences.
703 isp_get_pci_options(dev, &m1, &m2);
705 linesz = PCI_DFLT_LNSZ;
706 pcs->irq = pcs->regs = NULL;
707 pcs->rgd = pcs->rtp = pcs->iqd = 0;
709 cmd = pci_read_config(dev, PCIR_COMMAND, 2);
711 pcs->rtp = (m1 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
712 pcs->rgd = (m1 == PCIM_CMD_MEMEN)? MEM_MAP_REG : IO_MAP_REG;
713 pcs->regs = bus_alloc_resource_any(dev, pcs->rtp, &pcs->rgd, RF_ACTIVE);
715 if (pcs->regs == NULL && (cmd & m2)) {
716 pcs->rtp = (m2 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
717 pcs->rgd = (m2 == PCIM_CMD_MEMEN)? MEM_MAP_REG : IO_MAP_REG;
718 pcs->regs = bus_alloc_resource_any(dev, pcs->rtp, &pcs->rgd, RF_ACTIVE);
720 if (pcs->regs == NULL) {
721 device_printf(dev, "unable to map any ports\n");
725 device_printf(dev, "using %s space register mapping\n", (pcs->rgd == IO_MAP_REG)? "I/O" : "Memory");
727 isp->isp_bus_tag = rman_get_bustag(pcs->regs);
728 isp->isp_bus_handle = rman_get_bushandle(pcs->regs);
731 pcs->pci_poff[BIU_BLOCK >> _BLK_REG_SHFT] = BIU_REGS_OFF;
732 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS_OFF;
733 pcs->pci_poff[SXP_BLOCK >> _BLK_REG_SHFT] = PCI_SXP_REGS_OFF;
734 pcs->pci_poff[RISC_BLOCK >> _BLK_REG_SHFT] = PCI_RISC_REGS_OFF;
735 pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] = DMA_REGS_OFF;
737 switch (pci_get_devid(dev)) {
738 case PCI_QLOGIC_ISP1020:
740 isp->isp_mdvec = &mdvec;
741 isp->isp_type = ISP_HA_SCSI_UNKNOWN;
743 case PCI_QLOGIC_ISP1080:
745 isp->isp_mdvec = &mdvec_1080;
746 isp->isp_type = ISP_HA_SCSI_1080;
747 pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] = ISP1080_DMA_REGS_OFF;
749 case PCI_QLOGIC_ISP1240:
751 isp->isp_mdvec = &mdvec_1080;
752 isp->isp_type = ISP_HA_SCSI_1240;
754 pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] = ISP1080_DMA_REGS_OFF;
756 case PCI_QLOGIC_ISP1280:
758 isp->isp_mdvec = &mdvec_1080;
759 isp->isp_type = ISP_HA_SCSI_1280;
760 pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] = ISP1080_DMA_REGS_OFF;
762 case PCI_QLOGIC_ISP10160:
764 isp->isp_mdvec = &mdvec_12160;
765 isp->isp_type = ISP_HA_SCSI_10160;
766 pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] = ISP1080_DMA_REGS_OFF;
768 case PCI_QLOGIC_ISP12160:
771 isp->isp_mdvec = &mdvec_12160;
772 isp->isp_type = ISP_HA_SCSI_12160;
773 pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] = ISP1080_DMA_REGS_OFF;
775 case PCI_QLOGIC_ISP2100:
777 isp->isp_mdvec = &mdvec_2100;
778 isp->isp_type = ISP_HA_FC_2100;
779 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2100_OFF;
780 if (pci_get_revid(dev) < 3) {
782 * XXX: Need to get the actual revision
783 * XXX: number of the 2100 FB. At any rate,
784 * XXX: lower cache line size for early revision
790 case PCI_QLOGIC_ISP2200:
792 isp->isp_mdvec = &mdvec_2200;
793 isp->isp_type = ISP_HA_FC_2200;
794 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2100_OFF;
796 case PCI_QLOGIC_ISP2300:
798 isp->isp_mdvec = &mdvec_2300;
799 isp->isp_type = ISP_HA_FC_2300;
800 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2300_OFF;
802 case PCI_QLOGIC_ISP2312:
803 case PCI_QLOGIC_ISP6312:
805 isp->isp_mdvec = &mdvec_2300;
806 isp->isp_type = ISP_HA_FC_2312;
807 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2300_OFF;
809 case PCI_QLOGIC_ISP2322:
810 case PCI_QLOGIC_ISP6322:
812 isp->isp_mdvec = &mdvec_2300;
813 isp->isp_type = ISP_HA_FC_2322;
814 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2300_OFF;
816 case PCI_QLOGIC_ISP2422:
817 case PCI_QLOGIC_ISP2432:
819 isp->isp_nchan += isp_nvports;
820 isp->isp_mdvec = &mdvec_2400;
821 isp->isp_type = ISP_HA_FC_2400;
822 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2400_OFF;
824 case PCI_QLOGIC_ISP2532:
826 isp->isp_nchan += isp_nvports;
827 isp->isp_mdvec = &mdvec_2500;
828 isp->isp_type = ISP_HA_FC_2500;
829 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2400_OFF;
831 case PCI_QLOGIC_ISP5432:
833 isp->isp_mdvec = &mdvec_2500;
834 isp->isp_type = ISP_HA_FC_2500;
835 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2400_OFF;
838 device_printf(dev, "unknown device type\n");
842 isp->isp_revision = pci_get_revid(dev);
845 psize = sizeof (fcparam);
846 xsize = sizeof (struct isp_fc);
848 psize = sizeof (sdparam);
849 xsize = sizeof (struct isp_spi);
851 psize *= isp->isp_nchan;
852 xsize *= isp->isp_nchan;
853 isp->isp_param = malloc(psize, M_DEVBUF, M_NOWAIT | M_ZERO);
854 if (isp->isp_param == NULL) {
855 device_printf(dev, "cannot allocate parameter data\n");
858 isp->isp_osinfo.pc.ptr = malloc(xsize, M_DEVBUF, M_NOWAIT | M_ZERO);
859 if (isp->isp_osinfo.pc.ptr == NULL) {
860 device_printf(dev, "cannot allocate parameter data\n");
865 * Now that we know who we are (roughly) get/set specific options
867 for (i = 0; i < isp->isp_nchan; i++) {
868 isp_get_specific_options(dev, i, isp);
872 * The 'it' suffix really only matters for SCSI cards in target mode.
874 isp->isp_osinfo.fw = NULL;
875 if (IS_SCSI(isp) && (ISP_SPI_PC(isp, 0)->def_role & ISP_ROLE_TARGET)) {
876 snprintf(fwname, sizeof (fwname), "isp_%04x_it", did);
877 isp->isp_osinfo.fw = firmware_get(fwname);
878 } else if (IS_24XX(isp)) {
879 snprintf(fwname, sizeof (fwname), "isp_%04x_multi", did);
880 isp->isp_osinfo.fw = firmware_get(fwname);
882 if (isp->isp_osinfo.fw == NULL) {
883 snprintf(fwname, sizeof (fwname), "isp_%04x", did);
884 isp->isp_osinfo.fw = firmware_get(fwname);
886 if (isp->isp_osinfo.fw != NULL) {
887 isp_prt(isp, ISP_LOGCONFIG, "loaded firmware %s", fwname);
888 isp->isp_mdvec->dv_ispfw = isp->isp_osinfo.fw->data;
892 * Make sure that SERR, PERR, WRITE INVALIDATE and BUSMASTER are set.
894 cmd |= PCIM_CMD_SEREN | PCIM_CMD_PERRESPEN | PCIM_CMD_BUSMASTEREN | PCIM_CMD_INVEN;
895 if (IS_2300(isp)) { /* per QLogic errata */
896 cmd &= ~PCIM_CMD_INVEN;
898 if (IS_2322(isp) || pci_get_devid(dev) == PCI_QLOGIC_ISP6312) {
899 cmd &= ~PCIM_CMD_INTX_DISABLE;
902 cmd &= ~PCIM_CMD_INTX_DISABLE;
904 pci_write_config(dev, PCIR_COMMAND, cmd, 2);
907 * Make sure the Cache Line Size register is set sensibly.
909 data = pci_read_config(dev, PCIR_CACHELNSZ, 1);
910 if (data == 0 || (linesz != PCI_DFLT_LNSZ && data != linesz)) {
911 isp_prt(isp, ISP_LOGDEBUG0, "set PCI line size to %d from %d", linesz, data);
913 pci_write_config(dev, PCIR_CACHELNSZ, data, 1);
917 * Make sure the Latency Timer is sane.
919 data = pci_read_config(dev, PCIR_LATTIMER, 1);
920 if (data < PCI_DFLT_LTNCY) {
921 data = PCI_DFLT_LTNCY;
922 isp_prt(isp, ISP_LOGDEBUG0, "set PCI latency to %d", data);
923 pci_write_config(dev, PCIR_LATTIMER, data, 1);
927 * Make sure we've disabled the ROM.
929 data = pci_read_config(dev, PCIR_ROMADDR, 4);
931 pci_write_config(dev, PCIR_ROMADDR, data, 4);
936 * NB: MSI-X needs to be disabled for the 2432 (PCI-Express)
938 if (IS_24XX(isp) || IS_2322(isp)) {
939 pcs->msicount = pci_msi_count(dev);
940 if (pcs->msicount > 1) {
943 if (pci_alloc_msi(dev, &pcs->msicount) == 0) {
949 pcs->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &pcs->iqd, RF_ACTIVE | RF_SHAREABLE);
950 if (pcs->irq == NULL) {
951 device_printf(dev, "could not allocate interrupt\n");
955 /* Make sure the lock is set up. */
956 mtx_init(&isp->isp_osinfo.lock, "isp", NULL, MTX_DEF);
959 if (isp_setup_intr(dev, pcs->irq, ISP_IFLAGS, NULL, isp_platform_intr, isp, &pcs->ih)) {
960 device_printf(dev, "could not setup interrupt\n");
965 * Last minute checks...
967 if (IS_23XX(isp) || IS_24XX(isp)) {
968 isp->isp_port = pci_get_function(dev);
972 * Make sure we're in reset state.
976 if (isp->isp_state != ISP_RESETSTATE) {
981 if (isp->isp_state == ISP_INITSTATE) {
982 isp->isp_state = ISP_RUNSTATE;
985 if (isp_attach(isp)) {
995 (void) bus_teardown_intr(dev, pcs->irq, pcs->ih);
998 mtx_destroy(&isp->isp_osinfo.lock);
1001 (void) bus_release_resource(dev, SYS_RES_IRQ, pcs->iqd, pcs->irq);
1003 if (pcs->msicount) {
1004 pci_release_msi(dev);
1007 (void) bus_release_resource(dev, pcs->rtp, pcs->rgd, pcs->regs);
1009 if (pcs->pci_isp.isp_param) {
1010 free(pcs->pci_isp.isp_param, M_DEVBUF);
1011 pcs->pci_isp.isp_param = NULL;
1013 if (pcs->pci_isp.isp_osinfo.pc.ptr) {
1014 free(pcs->pci_isp.isp_osinfo.pc.ptr, M_DEVBUF);
1015 pcs->pci_isp.isp_osinfo.pc.ptr = NULL;
1021 isp_pci_detach(device_t dev)
1023 struct isp_pcisoftc *pcs;
1027 pcs = device_get_softc(dev);
1031 isp = (ispsoftc_t *) pcs;
1032 status = isp_detach(isp);
1038 (void) bus_teardown_intr(dev, pcs->irq, pcs->ih);
1041 mtx_destroy(&isp->isp_osinfo.lock);
1042 (void) bus_release_resource(dev, SYS_RES_IRQ, pcs->iqd, pcs->irq);
1043 if (pcs->msicount) {
1044 pci_release_msi(dev);
1046 (void) bus_release_resource(dev, pcs->rtp, pcs->rgd, pcs->regs);
1048 * XXX: THERE IS A LOT OF LEAKAGE HERE
1050 if (pcs->pci_isp.isp_param) {
1051 free(pcs->pci_isp.isp_param, M_DEVBUF);
1052 pcs->pci_isp.isp_param = NULL;
1054 if (pcs->pci_isp.isp_osinfo.pc.ptr) {
1055 free(pcs->pci_isp.isp_osinfo.pc.ptr, M_DEVBUF);
1056 pcs->pci_isp.isp_osinfo.pc.ptr = NULL;
1061 #define IspVirt2Off(a, x) \
1062 (((struct isp_pcisoftc *)a)->pci_poff[((x) & _BLK_REG_MASK) >> \
1063 _BLK_REG_SHFT] + ((x) & 0xfff))
1065 #define BXR2(isp, off) \
1066 bus_space_read_2(isp->isp_bus_tag, isp->isp_bus_handle, off)
1067 #define BXW2(isp, off, v) \
1068 bus_space_write_2(isp->isp_bus_tag, isp->isp_bus_handle, off, v)
1069 #define BXR4(isp, off) \
1070 bus_space_read_4(isp->isp_bus_tag, isp->isp_bus_handle, off)
1071 #define BXW4(isp, off, v) \
1072 bus_space_write_4(isp->isp_bus_tag, isp->isp_bus_handle, off, v)
1075 static ISP_INLINE int
1076 isp_pci_rd_debounced(ispsoftc_t *isp, int off, uint16_t *rp)
1078 uint32_t val0, val1;
1082 val0 = BXR2(isp, IspVirt2Off(isp, off));
1083 val1 = BXR2(isp, IspVirt2Off(isp, off));
1084 } while (val0 != val1 && ++i < 1000);
1093 isp_pci_rd_isr(ispsoftc_t *isp, uint32_t *isrp, uint16_t *semap, uint16_t *mbp)
1098 if (isp_pci_rd_debounced(isp, BIU_ISR, &isr)) {
1101 if (isp_pci_rd_debounced(isp, BIU_SEMA, &sema)) {
1105 isr = BXR2(isp, IspVirt2Off(isp, BIU_ISR));
1106 sema = BXR2(isp, IspVirt2Off(isp, BIU_SEMA));
1108 isp_prt(isp, ISP_LOGDEBUG3, "ISR 0x%x SEMA 0x%x", isr, sema);
1109 isr &= INT_PENDING_MASK(isp);
1110 sema &= BIU_SEMA_LOCK;
1111 if (isr == 0 && sema == 0) {
1115 if ((*semap = sema) != 0) {
1117 if (isp_pci_rd_debounced(isp, OUTMAILBOX0, mbp)) {
1121 *mbp = BXR2(isp, IspVirt2Off(isp, OUTMAILBOX0));
1128 isp_pci_rd_isr_2300(ispsoftc_t *isp, uint32_t *isrp, uint16_t *semap, uint16_t *mbox0p)
1133 if (!(BXR2(isp, IspVirt2Off(isp, BIU_ISR) & BIU2100_ISR_RISC_INT))) {
1137 r2hisr = BXR4(isp, IspVirt2Off(isp, BIU_R2HSTSLO));
1138 isp_prt(isp, ISP_LOGDEBUG3, "RISC2HOST ISR 0x%x", r2hisr);
1139 if ((r2hisr & BIU_R2HST_INTR) == 0) {
1143 switch (r2hisr & BIU_R2HST_ISTAT_MASK) {
1144 case ISPR2HST_ROM_MBX_OK:
1145 case ISPR2HST_ROM_MBX_FAIL:
1146 case ISPR2HST_MBX_OK:
1147 case ISPR2HST_MBX_FAIL:
1148 case ISPR2HST_ASYNC_EVENT:
1149 *isrp = r2hisr & 0xffff;
1150 *mbox0p = (r2hisr >> 16);
1153 case ISPR2HST_RIO_16:
1154 *isrp = r2hisr & 0xffff;
1155 *mbox0p = ASYNC_RIO16_1;
1158 case ISPR2HST_FPOST:
1159 *isrp = r2hisr & 0xffff;
1160 *mbox0p = ASYNC_CMD_CMPLT;
1163 case ISPR2HST_FPOST_CTIO:
1164 *isrp = r2hisr & 0xffff;
1165 *mbox0p = ASYNC_CTIO_DONE;
1168 case ISPR2HST_RSPQ_UPDATE:
1169 *isrp = r2hisr & 0xffff;
1174 hccr = ISP_READ(isp, HCCR);
1175 if (hccr & HCCR_PAUSE) {
1176 ISP_WRITE(isp, HCCR, HCCR_RESET);
1177 isp_prt(isp, ISP_LOGERR, "RISC paused at interrupt (%x->%x)", hccr, ISP_READ(isp, HCCR));
1178 ISP_WRITE(isp, BIU_ICR, 0);
1180 isp_prt(isp, ISP_LOGERR, "unknown interrupt 0x%x\n", r2hisr);
1187 isp_pci_rd_isr_2400(ispsoftc_t *isp, uint32_t *isrp, uint16_t *semap, uint16_t *mbox0p)
1191 r2hisr = BXR4(isp, IspVirt2Off(isp, BIU2400_R2HSTSLO));
1192 isp_prt(isp, ISP_LOGDEBUG3, "RISC2HOST ISR 0x%x", r2hisr);
1193 if ((r2hisr & BIU2400_R2HST_INTR) == 0) {
1197 switch (r2hisr & BIU2400_R2HST_ISTAT_MASK) {
1198 case ISP2400R2HST_ROM_MBX_OK:
1199 case ISP2400R2HST_ROM_MBX_FAIL:
1200 case ISP2400R2HST_MBX_OK:
1201 case ISP2400R2HST_MBX_FAIL:
1202 case ISP2400R2HST_ASYNC_EVENT:
1203 *isrp = r2hisr & 0xffff;
1204 *mbox0p = (r2hisr >> 16);
1207 case ISP2400R2HST_RSPQ_UPDATE:
1208 case ISP2400R2HST_ATIO_RSPQ_UPDATE:
1209 case ISP2400R2HST_ATIO_RQST_UPDATE:
1210 *isrp = r2hisr & 0xffff;
1215 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
1216 isp_prt(isp, ISP_LOGERR, "unknown interrupt 0x%x\n", r2hisr);
1222 isp_pci_rd_reg(ispsoftc_t *isp, int regoff)
1227 if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
1229 * We will assume that someone has paused the RISC processor.
1231 oldconf = BXR2(isp, IspVirt2Off(isp, BIU_CONF1));
1232 BXW2(isp, IspVirt2Off(isp, BIU_CONF1), oldconf | BIU_PCI_CONF1_SXP);
1233 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1235 rv = BXR2(isp, IspVirt2Off(isp, regoff));
1236 if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
1237 BXW2(isp, IspVirt2Off(isp, BIU_CONF1), oldconf);
1238 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1244 isp_pci_wr_reg(ispsoftc_t *isp, int regoff, uint32_t val)
1248 if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
1250 * We will assume that someone has paused the RISC processor.
1252 oldconf = BXR2(isp, IspVirt2Off(isp, BIU_CONF1));
1253 BXW2(isp, IspVirt2Off(isp, BIU_CONF1),
1254 oldconf | BIU_PCI_CONF1_SXP);
1255 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1257 BXW2(isp, IspVirt2Off(isp, regoff), val);
1258 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, regoff), 2, -1);
1259 if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
1260 BXW2(isp, IspVirt2Off(isp, BIU_CONF1), oldconf);
1261 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1267 isp_pci_rd_reg_1080(ispsoftc_t *isp, int regoff)
1269 uint32_t rv, oc = 0;
1271 if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
1274 * We will assume that someone has paused the RISC processor.
1276 oc = BXR2(isp, IspVirt2Off(isp, BIU_CONF1));
1277 tc = oc & ~BIU_PCI1080_CONF1_DMA;
1278 if (regoff & SXP_BANK1_SELECT)
1279 tc |= BIU_PCI1080_CONF1_SXP1;
1281 tc |= BIU_PCI1080_CONF1_SXP0;
1282 BXW2(isp, IspVirt2Off(isp, BIU_CONF1), tc);
1283 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1284 } else if ((regoff & _BLK_REG_MASK) == DMA_BLOCK) {
1285 oc = BXR2(isp, IspVirt2Off(isp, BIU_CONF1));
1286 BXW2(isp, IspVirt2Off(isp, BIU_CONF1),
1287 oc | BIU_PCI1080_CONF1_DMA);
1288 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1290 rv = BXR2(isp, IspVirt2Off(isp, regoff));
1292 BXW2(isp, IspVirt2Off(isp, BIU_CONF1), oc);
1293 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1299 isp_pci_wr_reg_1080(ispsoftc_t *isp, int regoff, uint32_t val)
1303 if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
1306 * We will assume that someone has paused the RISC processor.
1308 oc = BXR2(isp, IspVirt2Off(isp, BIU_CONF1));
1309 tc = oc & ~BIU_PCI1080_CONF1_DMA;
1310 if (regoff & SXP_BANK1_SELECT)
1311 tc |= BIU_PCI1080_CONF1_SXP1;
1313 tc |= BIU_PCI1080_CONF1_SXP0;
1314 BXW2(isp, IspVirt2Off(isp, BIU_CONF1), tc);
1315 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1316 } else if ((regoff & _BLK_REG_MASK) == DMA_BLOCK) {
1317 oc = BXR2(isp, IspVirt2Off(isp, BIU_CONF1));
1318 BXW2(isp, IspVirt2Off(isp, BIU_CONF1),
1319 oc | BIU_PCI1080_CONF1_DMA);
1320 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1322 BXW2(isp, IspVirt2Off(isp, regoff), val);
1323 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, regoff), 2, -1);
1325 BXW2(isp, IspVirt2Off(isp, BIU_CONF1), oc);
1326 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1331 isp_pci_rd_reg_2400(ispsoftc_t *isp, int regoff)
1334 int block = regoff & _BLK_REG_MASK;
1340 return (BXR2(isp, IspVirt2Off(isp, regoff)));
1342 isp_prt(isp, ISP_LOGWARN, "SXP_BLOCK read at 0x%x", regoff);
1343 return (0xffffffff);
1345 isp_prt(isp, ISP_LOGWARN, "RISC_BLOCK read at 0x%x", regoff);
1346 return (0xffffffff);
1348 isp_prt(isp, ISP_LOGWARN, "DMA_BLOCK read at 0x%x", regoff);
1349 return (0xffffffff);
1351 isp_prt(isp, ISP_LOGWARN, "unknown block read at 0x%x", regoff);
1352 return (0xffffffff);
1357 case BIU2400_FLASH_ADDR:
1358 case BIU2400_FLASH_DATA:
1362 case BIU2400_REQINP:
1363 case BIU2400_REQOUTP:
1364 case BIU2400_RSPINP:
1365 case BIU2400_RSPOUTP:
1366 case BIU2400_PRI_REQINP:
1367 case BIU2400_PRI_REQOUTP:
1368 case BIU2400_ATIO_RSPINP:
1369 case BIU2400_ATIO_RSPOUTP:
1374 rv = BXR4(isp, IspVirt2Off(isp, regoff));
1376 case BIU2400_R2HSTSLO:
1377 rv = BXR4(isp, IspVirt2Off(isp, regoff));
1379 case BIU2400_R2HSTSHI:
1380 rv = BXR4(isp, IspVirt2Off(isp, regoff)) >> 16;
1383 isp_prt(isp, ISP_LOGERR,
1384 "isp_pci_rd_reg_2400: unknown offset %x", regoff);
1392 isp_pci_wr_reg_2400(ispsoftc_t *isp, int regoff, uint32_t val)
1394 int block = regoff & _BLK_REG_MASK;
1400 BXW2(isp, IspVirt2Off(isp, regoff), val);
1401 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, regoff), 2, -1);
1404 isp_prt(isp, ISP_LOGWARN, "SXP_BLOCK write at 0x%x", regoff);
1407 isp_prt(isp, ISP_LOGWARN, "RISC_BLOCK write at 0x%x", regoff);
1410 isp_prt(isp, ISP_LOGWARN, "DMA_BLOCK write at 0x%x", regoff);
1413 isp_prt(isp, ISP_LOGWARN, "unknown block write at 0x%x",
1419 case BIU2400_FLASH_ADDR:
1420 case BIU2400_FLASH_DATA:
1424 case BIU2400_REQINP:
1425 case BIU2400_REQOUTP:
1426 case BIU2400_RSPINP:
1427 case BIU2400_RSPOUTP:
1428 case BIU2400_PRI_REQINP:
1429 case BIU2400_PRI_REQOUTP:
1430 case BIU2400_ATIO_RSPINP:
1431 case BIU2400_ATIO_RSPOUTP:
1436 BXW4(isp, IspVirt2Off(isp, regoff), val);
1437 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, regoff), 4, -1);
1440 isp_prt(isp, ISP_LOGERR,
1441 "isp_pci_wr_reg_2400: bad offset 0x%x", regoff);
1454 static void imc(void *, bus_dma_segment_t *, int, int);
1455 static void imc1(void *, bus_dma_segment_t *, int, int);
1458 imc(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1460 struct imush *imushp = (struct imush *) arg;
1464 imushp->error = error;
1468 imushp->error = EINVAL;
1471 isp_prt(imushp->isp, ISP_LOGDEBUG0, "request/result area @ 0x%jx/0x%jx", (uintmax_t) segs->ds_addr, (uintmax_t) segs->ds_len);
1473 imushp->isp->isp_rquest = imushp->vbase;
1474 imushp->isp->isp_rquest_dma = segs->ds_addr;
1475 segs->ds_addr += ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(imushp->isp));
1476 imushp->vbase += ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(imushp->isp));
1478 imushp->isp->isp_result_dma = segs->ds_addr;
1479 imushp->isp->isp_result = imushp->vbase;
1480 segs->ds_addr += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(imushp->isp));
1481 imushp->vbase += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(imushp->isp));
1483 if (imushp->isp->isp_type >= ISP_HA_FC_2300) {
1484 imushp->isp->isp_osinfo.ecmd_dma = segs->ds_addr;
1485 imushp->isp->isp_osinfo.ecmd_free = (isp_ecmd_t *)imushp->vbase;
1486 imushp->isp->isp_osinfo.ecmd_base = imushp->isp->isp_osinfo.ecmd_free;
1487 for (ecmd = imushp->isp->isp_osinfo.ecmd_free; ecmd < &imushp->isp->isp_osinfo.ecmd_free[N_XCMDS]; ecmd++) {
1488 if (ecmd == &imushp->isp->isp_osinfo.ecmd_free[N_XCMDS - 1]) {
1491 ecmd->next = ecmd + 1;
1495 #ifdef ISP_TARGET_MODE
1496 segs->ds_addr += (N_XCMDS * XCMD_SIZE);
1497 imushp->vbase += (N_XCMDS * XCMD_SIZE);
1498 if (IS_24XX(imushp->isp)) {
1499 imushp->isp->isp_atioq_dma = segs->ds_addr;
1500 imushp->isp->isp_atioq = imushp->vbase;
1506 imc1(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1508 struct imush *imushp = (struct imush *) arg;
1510 imushp->error = error;
1514 imushp->error = EINVAL;
1517 isp_prt(imushp->isp, ISP_LOGDEBUG0, "scdma @ 0x%jx/0x%jx", (uintmax_t) segs->ds_addr, (uintmax_t) segs->ds_len);
1518 FCPARAM(imushp->isp, imushp->chan)->isp_scdma = segs->ds_addr;
1519 FCPARAM(imushp->isp, imushp->chan)->isp_scratch = imushp->vbase;
1523 isp_pci_mbxdma(ispsoftc_t *isp)
1526 uint32_t len, nsegs;
1527 int i, error, cmap = 0;
1528 bus_size_t slim; /* segment size */
1529 bus_addr_t llim; /* low limit of unavailable dma */
1530 bus_addr_t hlim; /* high limit of unavailable dma */
1534 * Already been here? If so, leave...
1536 if (isp->isp_rquest) {
1541 if (isp->isp_maxcmds == 0) {
1542 isp_prt(isp, ISP_LOGERR, "maxcmds not set");
1547 hlim = BUS_SPACE_MAXADDR;
1548 if (IS_ULTRA2(isp) || IS_FC(isp) || IS_1240(isp)) {
1549 if (sizeof (bus_size_t) > 4) {
1550 slim = (bus_size_t) (1ULL << 32);
1552 slim = (bus_size_t) (1UL << 31);
1554 llim = BUS_SPACE_MAXADDR;
1556 llim = BUS_SPACE_MAXADDR_32BIT;
1560 len = isp->isp_maxcmds * sizeof (struct isp_pcmd);
1561 isp->isp_osinfo.pcmd_pool = (struct isp_pcmd *) malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
1562 if (isp->isp_osinfo.pcmd_pool == NULL) {
1563 isp_prt(isp, ISP_LOGERR, "cannot allocate pcmds");
1568 if (isp->isp_osinfo.sixtyfourbit) {
1569 nsegs = ISP_NSEG64_MAX;
1571 nsegs = ISP_NSEG_MAX;
1573 #ifdef ISP_TARGET_MODE
1575 * XXX: We don't really support 64 bit target mode for parallel scsi yet
1577 if (IS_SCSI(isp) && isp->isp_osinfo.sixtyfourbit) {
1578 free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
1579 isp_prt(isp, ISP_LOGERR, "we cannot do DAC for SPI cards yet");
1585 if (isp_dma_tag_create(BUS_DMA_ROOTARG(ISP_PCD(isp)), 1, slim, llim, hlim, NULL, NULL, BUS_SPACE_MAXSIZE, nsegs, slim, 0, &isp->isp_osinfo.dmat)) {
1586 free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
1588 isp_prt(isp, ISP_LOGERR, "could not create master dma tag");
1592 len = sizeof (isp_hdl_t) * isp->isp_maxcmds;
1593 isp->isp_xflist = (isp_hdl_t *) malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
1594 if (isp->isp_xflist == NULL) {
1595 free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
1597 isp_prt(isp, ISP_LOGERR, "cannot alloc xflist array");
1600 for (len = 0; len < isp->isp_maxcmds - 1; len++) {
1601 isp->isp_xflist[len].cmd = &isp->isp_xflist[len+1];
1603 isp->isp_xffree = isp->isp_xflist;
1604 #ifdef ISP_TARGET_MODE
1605 len = sizeof (isp_hdl_t) * isp->isp_maxcmds;
1606 isp->isp_tgtlist = (isp_hdl_t *) malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
1607 if (isp->isp_tgtlist == NULL) {
1608 free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
1609 free(isp->isp_xflist, M_DEVBUF);
1611 isp_prt(isp, ISP_LOGERR, "cannot alloc tgtlist array");
1614 for (len = 0; len < isp->isp_maxcmds - 1; len++) {
1615 isp->isp_tgtlist[len].cmd = &isp->isp_tgtlist[len+1];
1617 isp->isp_tgtfree = isp->isp_tgtlist;
1621 * Allocate and map the request and result queues (and ATIO queue
1622 * if we're a 2400 supporting target mode), and a region for
1623 * external dma addressable command/status structures (23XX and
1626 len = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
1627 len += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
1628 #ifdef ISP_TARGET_MODE
1630 len += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
1633 if (isp->isp_type >= ISP_HA_FC_2300) {
1634 len += (N_XCMDS * XCMD_SIZE);
1638 * Create a tag for the control spaces. We don't always need this
1639 * to be 32 bits, but we do this for simplicity and speed's sake.
1641 if (isp_dma_tag_create(isp->isp_osinfo.dmat, QENTRY_LEN, slim, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, len, 1, slim, 0, &isp->isp_osinfo.cdmat)) {
1642 isp_prt(isp, ISP_LOGERR, "cannot create a dma tag for control spaces");
1643 free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
1644 free(isp->isp_xflist, M_DEVBUF);
1645 #ifdef ISP_TARGET_MODE
1646 free(isp->isp_tgtlist, M_DEVBUF);
1652 if (bus_dmamem_alloc(isp->isp_osinfo.cdmat, (void **)&base, BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &isp->isp_osinfo.cdmap) != 0) {
1653 isp_prt(isp, ISP_LOGERR, "cannot allocate %d bytes of CCB memory", len);
1654 bus_dma_tag_destroy(isp->isp_osinfo.cdmat);
1655 free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
1656 free(isp->isp_xflist, M_DEVBUF);
1657 #ifdef ISP_TARGET_MODE
1658 free(isp->isp_tgtlist, M_DEVBUF);
1669 bus_dmamap_load(isp->isp_osinfo.cdmat, isp->isp_osinfo.cdmap, base, len, imc, &im, 0);
1671 isp_prt(isp, ISP_LOGERR, "error %d loading dma map for control areas", im.error);
1676 for (cmap = 0; cmap < isp->isp_nchan; cmap++) {
1677 struct isp_fc *fc = ISP_FC_PC(isp, cmap);
1678 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)) {
1681 if (bus_dmamem_alloc(fc->tdmat, (void **)&base, BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &fc->tdmap) != 0) {
1682 bus_dma_tag_destroy(fc->tdmat);
1689 bus_dmamap_load(fc->tdmat, fc->tdmap, base, ISP_FC_SCRLEN, imc1, &im, 0);
1691 bus_dmamem_free(fc->tdmat, base, fc->tdmap);
1692 bus_dma_tag_destroy(fc->tdmat);
1695 if (isp->isp_type >= ISP_HA_FC_2300) {
1696 for (i = 0; i < INITIAL_NEXUS_COUNT; i++) {
1697 struct isp_nexus *n = malloc(sizeof (struct isp_nexus), M_DEVBUF, M_NOWAIT | M_ZERO);
1699 while (fc->nexus_free_list) {
1700 n = fc->nexus_free_list;
1701 fc->nexus_free_list = n->next;
1706 n->next = fc->nexus_free_list;
1707 fc->nexus_free_list = n;
1713 for (i = 0; i < isp->isp_maxcmds; i++) {
1714 struct isp_pcmd *pcmd = &isp->isp_osinfo.pcmd_pool[i];
1715 error = bus_dmamap_create(isp->isp_osinfo.dmat, 0, &pcmd->dmap);
1717 isp_prt(isp, ISP_LOGERR, "error %d creating per-cmd DMA maps", error);
1719 bus_dmamap_destroy(isp->isp_osinfo.dmat, isp->isp_osinfo.pcmd_pool[i].dmap);
1723 callout_init_mtx(&pcmd->wdog, &isp->isp_osinfo.lock, 0);
1724 if (i == isp->isp_maxcmds-1) {
1727 pcmd->next = &isp->isp_osinfo.pcmd_pool[i+1];
1730 isp->isp_osinfo.pcmd_free = &isp->isp_osinfo.pcmd_pool[0];
1735 while (--cmap >= 0) {
1736 struct isp_fc *fc = ISP_FC_PC(isp, cmap);
1737 bus_dmamem_free(fc->tdmat, base, fc->tdmap);
1738 bus_dma_tag_destroy(fc->tdmat);
1739 while (fc->nexus_free_list) {
1740 struct isp_nexus *n = fc->nexus_free_list;
1741 fc->nexus_free_list = n->next;
1745 bus_dmamem_free(isp->isp_osinfo.cdmat, base, isp->isp_osinfo.cdmap);
1746 bus_dma_tag_destroy(isp->isp_osinfo.cdmat);
1747 free(isp->isp_xflist, M_DEVBUF);
1748 #ifdef ISP_TARGET_MODE
1749 free(isp->isp_tgtlist, M_DEVBUF);
1751 free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
1752 isp->isp_rquest = NULL;
1760 void *rq; /* original request */
1765 #define MUSHERR_NOQENTRIES -2
1767 #ifdef ISP_TARGET_MODE
1768 static void tdma2_2(void *, bus_dma_segment_t *, int, bus_size_t, int);
1769 static void tdma2(void *, bus_dma_segment_t *, int, int);
1772 tdma2_2(void *arg, bus_dma_segment_t *dm_segs, int nseg, bus_size_t mapsize, int error)
1776 mp->mapsize = mapsize;
1777 tdma2(arg, dm_segs, nseg, error);
1781 tdma2(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1785 struct ccb_scsiio *csio;
1789 mp = (mush_t *) arg;
1794 csio = mp->cmd_token;
1798 if (isp->isp_osinfo.sixtyfourbit) {
1799 if (nseg >= ISP_NSEG64_MAX) {
1800 isp_prt(isp, ISP_LOGERR, "number of segments (%d) exceed maximum we can support (%d)", nseg, ISP_NSEG64_MAX);
1804 if (rq->req_header.rqs_entry_type == RQSTYPE_CTIO2) {
1805 rq->req_header.rqs_entry_type = RQSTYPE_CTIO3;
1808 if (nseg >= ISP_NSEG_MAX) {
1809 isp_prt(isp, ISP_LOGERR, "number of segments (%d) exceed maximum we can support (%d)", nseg, ISP_NSEG_MAX);
1814 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1815 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_PREWRITE);
1816 ddir = ISP_TO_DEVICE;
1817 } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
1818 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_PREREAD);
1819 ddir = ISP_FROM_DEVICE;
1831 error = isp_send_tgt_cmd(isp, rq, dm_segs, nseg, XS_XFRLEN(csio), ddir, &csio->sense_data, csio->sense_len);
1834 mp->error = MUSHERR_NOQENTRIES;
1843 static void dma2_2(void *, bus_dma_segment_t *, int, bus_size_t, int);
1844 static void dma2(void *, bus_dma_segment_t *, int, int);
1847 dma2_2(void *arg, bus_dma_segment_t *dm_segs, int nseg, bus_size_t mapsize, int error)
1851 mp->mapsize = mapsize;
1852 dma2(arg, dm_segs, nseg, error);
1856 dma2(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1860 struct ccb_scsiio *csio;
1864 mp = (mush_t *) arg;
1869 csio = mp->cmd_token;
1873 if (isp->isp_osinfo.sixtyfourbit) {
1874 if (nseg >= ISP_NSEG64_MAX) {
1875 isp_prt(isp, ISP_LOGERR, "number of segments (%d) exceed maximum we can support (%d)", nseg, ISP_NSEG64_MAX);
1879 if (rq->req_header.rqs_entry_type == RQSTYPE_T2RQS) {
1880 rq->req_header.rqs_entry_type = RQSTYPE_T3RQS;
1881 } else if (rq->req_header.rqs_entry_type == RQSTYPE_REQUEST) {
1882 rq->req_header.rqs_entry_type = RQSTYPE_A64;
1885 if (nseg >= ISP_NSEG_MAX) {
1886 isp_prt(isp, ISP_LOGERR, "number of segments (%d) exceed maximum we can support (%d)", nseg, ISP_NSEG_MAX);
1891 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1892 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_PREREAD);
1893 ddir = ISP_FROM_DEVICE;
1894 } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
1895 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_PREWRITE);
1896 ddir = ISP_TO_DEVICE;
1906 error = isp_send_cmd(isp, rq, dm_segs, nseg, XS_XFRLEN(csio), ddir, (ispds64_t *)csio->req_map);
1909 mp->error = MUSHERR_NOQENTRIES;
1920 isp_pci_dmasetup(ispsoftc_t *isp, struct ccb_scsiio *csio, void *ff)
1923 void (*eptr)(void *, bus_dma_segment_t *, int, int);
1924 void (*eptr2)(void *, bus_dma_segment_t *, int, bus_size_t, int);
1929 mp->cmd_token = csio;
1934 #ifdef ISP_TARGET_MODE
1935 if (csio->ccb_h.func_code == XPT_CONT_TARGET_IO) {
1946 error = bus_dmamap_load_ccb(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap,
1947 (union ccb *)csio, eptr, mp, 0);
1948 if (error == EINPROGRESS) {
1949 bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
1951 isp_prt(isp, ISP_LOGERR, "deferred dma allocation not supported");
1952 } else if (error && mp->error == 0) {
1954 isp_prt(isp, ISP_LOGERR, "error %d in dma mapping code", error);
1959 int retval = CMD_COMPLETE;
1960 if (mp->error == MUSHERR_NOQENTRIES) {
1961 retval = CMD_EAGAIN;
1962 } else if (mp->error == EFBIG) {
1963 csio->ccb_h.status = CAM_REQ_TOO_BIG;
1964 } else if (mp->error == EINVAL) {
1965 csio->ccb_h.status = CAM_REQ_INVALID;
1967 csio->ccb_h.status = CAM_UNREC_HBA_ERROR;
1971 return (CMD_QUEUED);
1975 isp_pci_reset0(ispsoftc_t *isp)
1977 ISP_DISABLE_INTS(isp);
1981 isp_pci_reset1(ispsoftc_t *isp)
1983 if (!IS_24XX(isp)) {
1984 /* Make sure the BIOS is disabled */
1985 isp_pci_wr_reg(isp, HCCR, PCI_HCCR_CMD_BIOS);
1987 /* and enable interrupts */
1988 ISP_ENABLE_INTS(isp);
1992 isp_pci_dumpregs(ispsoftc_t *isp, const char *msg)
1994 struct isp_pcisoftc *pcs = (struct isp_pcisoftc *)isp;
1996 printf("%s: %s\n", device_get_nameunit(isp->isp_dev), msg);
1998 printf("%s:\n", device_get_nameunit(isp->isp_dev));
2000 printf(" biu_conf1=%x", ISP_READ(isp, BIU_CONF1));
2002 printf(" biu_csr=%x", ISP_READ(isp, BIU2100_CSR));
2003 printf(" biu_icr=%x biu_isr=%x biu_sema=%x ", ISP_READ(isp, BIU_ICR),
2004 ISP_READ(isp, BIU_ISR), ISP_READ(isp, BIU_SEMA));
2005 printf("risc_hccr=%x\n", ISP_READ(isp, HCCR));
2009 ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
2010 printf(" cdma_conf=%x cdma_sts=%x cdma_fifostat=%x\n",
2011 ISP_READ(isp, CDMA_CONF), ISP_READ(isp, CDMA_STATUS),
2012 ISP_READ(isp, CDMA_FIFO_STS));
2013 printf(" ddma_conf=%x ddma_sts=%x ddma_fifostat=%x\n",
2014 ISP_READ(isp, DDMA_CONF), ISP_READ(isp, DDMA_STATUS),
2015 ISP_READ(isp, DDMA_FIFO_STS));
2016 printf(" sxp_int=%x sxp_gross=%x sxp(scsi_ctrl)=%x\n",
2017 ISP_READ(isp, SXP_INTERRUPT),
2018 ISP_READ(isp, SXP_GROSS_ERR),
2019 ISP_READ(isp, SXP_PINS_CTRL));
2020 ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
2022 printf(" mbox regs: %x %x %x %x %x\n",
2023 ISP_READ(isp, OUTMAILBOX0), ISP_READ(isp, OUTMAILBOX1),
2024 ISP_READ(isp, OUTMAILBOX2), ISP_READ(isp, OUTMAILBOX3),
2025 ISP_READ(isp, OUTMAILBOX4));
2026 printf(" PCI Status Command/Status=%x\n",
2027 pci_read_config(pcs->pci_dev, PCIR_COMMAND, 1));