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);
379 isp_pci_probe(device_t dev)
381 switch ((pci_get_device(dev) << 16) | (pci_get_vendor(dev))) {
382 case PCI_QLOGIC_ISP1020:
383 device_set_desc(dev, "Qlogic ISP 1020/1040 PCI SCSI Adapter");
385 case PCI_QLOGIC_ISP1080:
386 device_set_desc(dev, "Qlogic ISP 1080 PCI SCSI Adapter");
388 case PCI_QLOGIC_ISP1240:
389 device_set_desc(dev, "Qlogic ISP 1240 PCI SCSI Adapter");
391 case PCI_QLOGIC_ISP1280:
392 device_set_desc(dev, "Qlogic ISP 1280 PCI SCSI Adapter");
394 case PCI_QLOGIC_ISP10160:
395 device_set_desc(dev, "Qlogic ISP 10160 PCI SCSI Adapter");
397 case PCI_QLOGIC_ISP12160:
398 if (pci_get_subvendor(dev) == AMI_RAID_SUBVENDOR_ID) {
401 device_set_desc(dev, "Qlogic ISP 12160 PCI SCSI Adapter");
403 case PCI_QLOGIC_ISP2100:
404 device_set_desc(dev, "Qlogic ISP 2100 PCI FC-AL Adapter");
406 case PCI_QLOGIC_ISP2200:
407 device_set_desc(dev, "Qlogic ISP 2200 PCI FC-AL Adapter");
409 case PCI_QLOGIC_ISP2300:
410 device_set_desc(dev, "Qlogic ISP 2300 PCI FC-AL Adapter");
412 case PCI_QLOGIC_ISP2312:
413 device_set_desc(dev, "Qlogic ISP 2312 PCI FC-AL Adapter");
415 case PCI_QLOGIC_ISP2322:
416 device_set_desc(dev, "Qlogic ISP 2322 PCI FC-AL Adapter");
418 case PCI_QLOGIC_ISP2422:
419 device_set_desc(dev, "Qlogic ISP 2422 PCI FC-AL Adapter");
421 case PCI_QLOGIC_ISP2432:
422 device_set_desc(dev, "Qlogic ISP 2432 PCI FC-AL Adapter");
424 case PCI_QLOGIC_ISP2532:
425 device_set_desc(dev, "Qlogic ISP 2532 PCI FC-AL Adapter");
427 case PCI_QLOGIC_ISP5432:
428 device_set_desc(dev, "Qlogic ISP 5432 PCI FC-AL Adapter");
430 case PCI_QLOGIC_ISP6312:
431 device_set_desc(dev, "Qlogic ISP 6312 PCI FC-AL Adapter");
433 case PCI_QLOGIC_ISP6322:
434 device_set_desc(dev, "Qlogic ISP 6322 PCI FC-AL Adapter");
439 if (isp_announced == 0 && bootverbose) {
440 printf("Qlogic ISP Driver, FreeBSD Version %d.%d, "
441 "Core Version %d.%d\n",
442 ISP_PLATFORM_VERSION_MAJOR, ISP_PLATFORM_VERSION_MINOR,
443 ISP_CORE_VERSION_MAJOR, ISP_CORE_VERSION_MINOR);
447 * XXXX: Here is where we might load the f/w module
448 * XXXX: (or increase a reference count to it).
450 return (BUS_PROBE_DEFAULT);
454 isp_get_generic_options(device_t dev, ispsoftc_t *isp, int *nvp)
459 * Figure out if we're supposed to skip this one.
462 if (resource_int_value(device_get_name(dev), device_get_unit(dev), "disable", &tval) == 0 && tval) {
463 device_printf(dev, "disabled at user request\n");
464 isp->isp_osinfo.disabled = 1;
469 if (resource_int_value(device_get_name(dev), device_get_unit(dev), "fwload_disable", &tval) == 0 && tval != 0) {
470 isp->isp_confopts |= ISP_CFG_NORELOAD;
473 if (resource_int_value(device_get_name(dev), device_get_unit(dev), "ignore_nvram", &tval) == 0 && tval != 0) {
474 isp->isp_confopts |= ISP_CFG_NONVRAM;
477 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), "debug", &tval);
479 isp->isp_dblev = tval;
481 isp->isp_dblev = ISP_LOGWARN|ISP_LOGERR;
484 isp->isp_dblev |= ISP_LOGCONFIG|ISP_LOGINFO;
487 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), "vports", &tval);
488 if (tval > 0 && tval < 127) {
494 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), "autoconfig", &tval);
495 isp_autoconfig = tval;
497 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), "quickboot_time", &tval);
498 isp_quickboot_time = tval;
501 if (resource_int_value(device_get_name(dev), device_get_unit(dev), "forcemulti", &tval) == 0 && tval != 0) {
502 isp->isp_osinfo.forcemulti = 1;
507 isp_get_pci_options(device_t dev, int *m1, int *m2)
511 * Which we should try first - memory mapping or i/o mapping?
513 * We used to try memory first followed by i/o on alpha, otherwise
514 * the reverse, but we should just try memory first all the time now.
516 *m1 = PCIM_CMD_MEMEN;
517 *m2 = PCIM_CMD_PORTEN;
520 if (resource_int_value(device_get_name(dev), device_get_unit(dev), "prefer_iomap", &tval) == 0 && tval != 0) {
521 *m1 = PCIM_CMD_PORTEN;
522 *m2 = PCIM_CMD_MEMEN;
525 if (resource_int_value(device_get_name(dev), device_get_unit(dev), "prefer_memmap", &tval) == 0 && tval != 0) {
526 *m1 = PCIM_CMD_MEMEN;
527 *m2 = PCIM_CMD_PORTEN;
532 isp_get_specific_options(device_t dev, int chan, ispsoftc_t *isp)
537 if (resource_int_value(device_get_name(dev), device_get_unit(dev), "iid", &tval)) {
539 ISP_FC_PC(isp, chan)->default_id = 109 - chan;
542 ISP_SPI_PC(isp, chan)->iid = OF_getscsinitid(dev);
544 ISP_SPI_PC(isp, chan)->iid = 7;
549 ISP_FC_PC(isp, chan)->default_id = tval - chan;
551 ISP_SPI_PC(isp, chan)->iid = tval;
553 isp->isp_confopts |= ISP_CFG_OWNLOOPID;
557 if (resource_int_value(device_get_name(dev), device_get_unit(dev), "role", &tval) == 0) {
560 case ISP_ROLE_INITIATOR:
561 case ISP_ROLE_TARGET:
562 case ISP_ROLE_INITIATOR|ISP_ROLE_TARGET:
563 device_printf(dev, "setting role to 0x%x\n", tval);
571 tval = ISP_DEFAULT_ROLES;
575 ISP_SPI_PC(isp, chan)->def_role = tval;
578 ISP_FC_PC(isp, chan)->def_role = tval;
581 if (resource_int_value(device_get_name(dev), device_get_unit(dev), "fullduplex", &tval) == 0 && tval != 0) {
582 isp->isp_confopts |= ISP_CFG_FULL_DUPLEX;
585 if (resource_string_value(device_get_name(dev), device_get_unit(dev), "topology", (const char **) &sptr) == 0 && sptr != 0) {
586 if (strcmp(sptr, "lport") == 0) {
587 isp->isp_confopts |= ISP_CFG_LPORT;
588 } else if (strcmp(sptr, "nport") == 0) {
589 isp->isp_confopts |= ISP_CFG_NPORT;
590 } else if (strcmp(sptr, "lport-only") == 0) {
591 isp->isp_confopts |= ISP_CFG_LPORT_ONLY;
592 } else if (strcmp(sptr, "nport-only") == 0) {
593 isp->isp_confopts |= ISP_CFG_NPORT_ONLY;
598 * Because the resource_*_value functions can neither return
599 * 64 bit integer values, nor can they be directly coerced
600 * to interpret the right hand side of the assignment as
601 * you want them to interpret it, we have to force WWN
602 * hint replacement to specify WWN strings with a leading
603 * 'w' (e..g w50000000aaaa0001). Sigh.
606 tval = resource_string_value(device_get_name(dev), device_get_unit(dev), "portwwn", (const char **) &sptr);
607 if (tval == 0 && sptr != 0 && *sptr++ == 'w') {
609 ISP_FC_PC(isp, chan)->def_wwpn = strtouq(sptr, &eptr, 16);
610 if (eptr < sptr + 16 || ISP_FC_PC(isp, chan)->def_wwpn == -1) {
611 device_printf(dev, "mangled portwwn hint '%s'\n", sptr);
612 ISP_FC_PC(isp, chan)->def_wwpn = 0;
617 tval = resource_string_value(device_get_name(dev), device_get_unit(dev), "nodewwn", (const char **) &sptr);
618 if (tval == 0 && sptr != 0 && *sptr++ == 'w') {
620 ISP_FC_PC(isp, chan)->def_wwnn = strtouq(sptr, &eptr, 16);
621 if (eptr < sptr + 16 || ISP_FC_PC(isp, chan)->def_wwnn == 0) {
622 device_printf(dev, "mangled nodewwn hint '%s'\n", sptr);
623 ISP_FC_PC(isp, chan)->def_wwnn = 0;
628 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), "hysteresis", &tval);
629 if (tval >= 0 && tval < 256) {
630 ISP_FC_PC(isp, chan)->hysteresis = tval;
632 ISP_FC_PC(isp, chan)->hysteresis = isp_fabric_hysteresis;
636 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), "loop_down_limit", &tval);
637 if (tval >= 0 && tval < 0xffff) {
638 ISP_FC_PC(isp, chan)->loop_down_limit = tval;
640 ISP_FC_PC(isp, chan)->loop_down_limit = isp_loop_down_limit;
644 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), "gone_device_time", &tval);
645 if (tval >= 0 && tval < 0xffff) {
646 ISP_FC_PC(isp, chan)->gone_device_time = tval;
648 ISP_FC_PC(isp, chan)->gone_device_time = isp_gone_device_time;
653 isp_pci_attach(device_t dev)
655 int i, m1, m2, locksetup = 0;
657 uint32_t data, cmd, linesz, did;
658 struct isp_pcisoftc *pcs;
663 pcs = device_get_softc(dev);
665 device_printf(dev, "cannot get softc\n");
668 memset(pcs, 0, sizeof (*pcs));
676 * Get Generic Options
678 isp_get_generic_options(dev, isp, &isp_nvports);
681 * Check to see if options have us disabled
683 if (isp->isp_osinfo.disabled) {
685 * But return zero to preserve unit numbering
691 * Get PCI options- which in this case are just mapping preferences.
693 isp_get_pci_options(dev, &m1, &m2);
695 linesz = PCI_DFLT_LNSZ;
696 pcs->irq = pcs->regs = NULL;
697 pcs->rgd = pcs->rtp = pcs->iqd = 0;
699 cmd = pci_read_config(dev, PCIR_COMMAND, 2);
701 pcs->rtp = (m1 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
702 pcs->rgd = (m1 == PCIM_CMD_MEMEN)? MEM_MAP_REG : IO_MAP_REG;
703 pcs->regs = bus_alloc_resource_any(dev, pcs->rtp, &pcs->rgd, RF_ACTIVE);
705 if (pcs->regs == NULL && (cmd & m2)) {
706 pcs->rtp = (m2 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
707 pcs->rgd = (m2 == PCIM_CMD_MEMEN)? MEM_MAP_REG : IO_MAP_REG;
708 pcs->regs = bus_alloc_resource_any(dev, pcs->rtp, &pcs->rgd, RF_ACTIVE);
710 if (pcs->regs == NULL) {
711 device_printf(dev, "unable to map any ports\n");
715 device_printf(dev, "using %s space register mapping\n", (pcs->rgd == IO_MAP_REG)? "I/O" : "Memory");
717 isp->isp_bus_tag = rman_get_bustag(pcs->regs);
718 isp->isp_bus_handle = rman_get_bushandle(pcs->regs);
721 pcs->pci_poff[BIU_BLOCK >> _BLK_REG_SHFT] = BIU_REGS_OFF;
722 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS_OFF;
723 pcs->pci_poff[SXP_BLOCK >> _BLK_REG_SHFT] = PCI_SXP_REGS_OFF;
724 pcs->pci_poff[RISC_BLOCK >> _BLK_REG_SHFT] = PCI_RISC_REGS_OFF;
725 pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] = DMA_REGS_OFF;
727 switch (pci_get_devid(dev)) {
728 case PCI_QLOGIC_ISP1020:
730 isp->isp_mdvec = &mdvec;
731 isp->isp_type = ISP_HA_SCSI_UNKNOWN;
733 case PCI_QLOGIC_ISP1080:
735 isp->isp_mdvec = &mdvec_1080;
736 isp->isp_type = ISP_HA_SCSI_1080;
737 pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] = ISP1080_DMA_REGS_OFF;
739 case PCI_QLOGIC_ISP1240:
741 isp->isp_mdvec = &mdvec_1080;
742 isp->isp_type = ISP_HA_SCSI_1240;
744 pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] = ISP1080_DMA_REGS_OFF;
746 case PCI_QLOGIC_ISP1280:
748 isp->isp_mdvec = &mdvec_1080;
749 isp->isp_type = ISP_HA_SCSI_1280;
750 pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] = ISP1080_DMA_REGS_OFF;
752 case PCI_QLOGIC_ISP10160:
754 isp->isp_mdvec = &mdvec_12160;
755 isp->isp_type = ISP_HA_SCSI_10160;
756 pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] = ISP1080_DMA_REGS_OFF;
758 case PCI_QLOGIC_ISP12160:
761 isp->isp_mdvec = &mdvec_12160;
762 isp->isp_type = ISP_HA_SCSI_12160;
763 pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] = ISP1080_DMA_REGS_OFF;
765 case PCI_QLOGIC_ISP2100:
767 isp->isp_mdvec = &mdvec_2100;
768 isp->isp_type = ISP_HA_FC_2100;
769 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2100_OFF;
770 if (pci_get_revid(dev) < 3) {
772 * XXX: Need to get the actual revision
773 * XXX: number of the 2100 FB. At any rate,
774 * XXX: lower cache line size for early revision
780 case PCI_QLOGIC_ISP2200:
782 isp->isp_mdvec = &mdvec_2200;
783 isp->isp_type = ISP_HA_FC_2200;
784 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2100_OFF;
786 case PCI_QLOGIC_ISP2300:
788 isp->isp_mdvec = &mdvec_2300;
789 isp->isp_type = ISP_HA_FC_2300;
790 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2300_OFF;
792 case PCI_QLOGIC_ISP2312:
793 case PCI_QLOGIC_ISP6312:
795 isp->isp_mdvec = &mdvec_2300;
796 isp->isp_type = ISP_HA_FC_2312;
797 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2300_OFF;
799 case PCI_QLOGIC_ISP2322:
800 case PCI_QLOGIC_ISP6322:
802 isp->isp_mdvec = &mdvec_2300;
803 isp->isp_type = ISP_HA_FC_2322;
804 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2300_OFF;
806 case PCI_QLOGIC_ISP2422:
807 case PCI_QLOGIC_ISP2432:
809 isp->isp_nchan += isp_nvports;
810 isp->isp_mdvec = &mdvec_2400;
811 isp->isp_type = ISP_HA_FC_2400;
812 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2400_OFF;
814 case PCI_QLOGIC_ISP2532:
816 isp->isp_nchan += isp_nvports;
817 isp->isp_mdvec = &mdvec_2500;
818 isp->isp_type = ISP_HA_FC_2500;
819 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2400_OFF;
821 case PCI_QLOGIC_ISP5432:
823 isp->isp_mdvec = &mdvec_2500;
824 isp->isp_type = ISP_HA_FC_2500;
825 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2400_OFF;
828 device_printf(dev, "unknown device type\n");
832 isp->isp_revision = pci_get_revid(dev);
835 psize = sizeof (fcparam);
836 xsize = sizeof (struct isp_fc);
838 psize = sizeof (sdparam);
839 xsize = sizeof (struct isp_spi);
841 psize *= isp->isp_nchan;
842 xsize *= isp->isp_nchan;
843 isp->isp_param = malloc(psize, M_DEVBUF, M_NOWAIT | M_ZERO);
844 if (isp->isp_param == NULL) {
845 device_printf(dev, "cannot allocate parameter data\n");
848 isp->isp_osinfo.pc.ptr = malloc(xsize, M_DEVBUF, M_NOWAIT | M_ZERO);
849 if (isp->isp_osinfo.pc.ptr == NULL) {
850 device_printf(dev, "cannot allocate parameter data\n");
855 * Now that we know who we are (roughly) get/set specific options
857 for (i = 0; i < isp->isp_nchan; i++) {
858 isp_get_specific_options(dev, i, isp);
862 * The 'it' suffix really only matters for SCSI cards in target mode.
864 isp->isp_osinfo.fw = NULL;
865 if (IS_SCSI(isp) && (ISP_SPI_PC(isp, 0)->def_role & ISP_ROLE_TARGET)) {
866 snprintf(fwname, sizeof (fwname), "isp_%04x_it", did);
867 isp->isp_osinfo.fw = firmware_get(fwname);
868 } else if (IS_24XX(isp) && (isp->isp_nchan > 1 || isp->isp_osinfo.forcemulti)) {
869 snprintf(fwname, sizeof (fwname), "isp_%04x_multi", did);
870 isp->isp_osinfo.fw = firmware_get(fwname);
872 if (isp->isp_osinfo.fw == NULL) {
873 snprintf(fwname, sizeof (fwname), "isp_%04x", did);
874 isp->isp_osinfo.fw = firmware_get(fwname);
876 if (isp->isp_osinfo.fw != NULL) {
877 isp->isp_mdvec->dv_ispfw = isp->isp_osinfo.fw->data;
881 * Make sure that SERR, PERR, WRITE INVALIDATE and BUSMASTER
884 cmd |= PCIM_CMD_SEREN | PCIM_CMD_PERRESPEN |
885 PCIM_CMD_BUSMASTEREN | PCIM_CMD_INVEN;
887 if (IS_2300(isp)) { /* per QLogic errata */
888 cmd &= ~PCIM_CMD_INVEN;
891 if (IS_2322(isp) || pci_get_devid(dev) == PCI_QLOGIC_ISP6312) {
892 cmd &= ~PCIM_CMD_INTX_DISABLE;
896 cmd &= ~PCIM_CMD_INTX_DISABLE;
899 pci_write_config(dev, PCIR_COMMAND, cmd, 2);
902 * Make sure the Cache Line Size register is set sensibly.
904 data = pci_read_config(dev, PCIR_CACHELNSZ, 1);
905 if (data == 0 || (linesz != PCI_DFLT_LNSZ && data != linesz)) {
906 isp_prt(isp, ISP_LOGCONFIG, "set PCI line size to %d from %d", linesz, data);
908 pci_write_config(dev, PCIR_CACHELNSZ, data, 1);
912 * Make sure the Latency Timer is sane.
914 data = pci_read_config(dev, PCIR_LATTIMER, 1);
915 if (data < PCI_DFLT_LTNCY) {
916 data = PCI_DFLT_LTNCY;
917 isp_prt(isp, ISP_LOGCONFIG, "set PCI latency to %d", data);
918 pci_write_config(dev, PCIR_LATTIMER, data, 1);
922 * Make sure we've disabled the ROM.
924 data = pci_read_config(dev, PCIR_ROMADDR, 4);
926 pci_write_config(dev, PCIR_ROMADDR, data, 4);
931 * NB: MSI-X needs to be disabled for the 2432 (PCI-Express)
933 if (IS_24XX(isp) || IS_2322(isp)) {
934 pcs->msicount = pci_msi_count(dev);
935 if (pcs->msicount > 1) {
938 if (pci_alloc_msi(dev, &pcs->msicount) == 0) {
944 pcs->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &pcs->iqd, RF_ACTIVE | RF_SHAREABLE);
945 if (pcs->irq == NULL) {
946 device_printf(dev, "could not allocate interrupt\n");
950 /* Make sure the lock is set up. */
951 mtx_init(&isp->isp_osinfo.lock, "isp", NULL, MTX_DEF);
954 if (isp_setup_intr(dev, pcs->irq, ISP_IFLAGS, NULL, isp_platform_intr, isp, &pcs->ih)) {
955 device_printf(dev, "could not setup interrupt\n");
960 * Last minute checks...
962 if (IS_23XX(isp) || IS_24XX(isp)) {
963 isp->isp_port = pci_get_function(dev);
967 * Make sure we're in reset state.
971 if (isp->isp_state != ISP_RESETSTATE) {
976 if (isp->isp_state == ISP_INITSTATE) {
977 isp->isp_state = ISP_RUNSTATE;
980 if (isp_attach(isp)) {
990 (void) bus_teardown_intr(dev, pcs->irq, pcs->ih);
993 mtx_destroy(&isp->isp_osinfo.lock);
996 (void) bus_release_resource(dev, SYS_RES_IRQ, pcs->iqd, pcs->irq);
999 pci_release_msi(dev);
1002 (void) bus_release_resource(dev, pcs->rtp, pcs->rgd, pcs->regs);
1004 if (pcs->pci_isp.isp_param) {
1005 free(pcs->pci_isp.isp_param, M_DEVBUF);
1006 pcs->pci_isp.isp_param = NULL;
1008 if (pcs->pci_isp.isp_osinfo.pc.ptr) {
1009 free(pcs->pci_isp.isp_osinfo.pc.ptr, M_DEVBUF);
1010 pcs->pci_isp.isp_osinfo.pc.ptr = NULL;
1016 isp_pci_detach(device_t dev)
1018 struct isp_pcisoftc *pcs;
1022 pcs = device_get_softc(dev);
1026 isp = (ispsoftc_t *) pcs;
1027 status = isp_detach(isp);
1033 (void) bus_teardown_intr(dev, pcs->irq, pcs->ih);
1036 mtx_destroy(&isp->isp_osinfo.lock);
1037 (void) bus_release_resource(dev, SYS_RES_IRQ, pcs->iqd, pcs->irq);
1038 if (pcs->msicount) {
1039 pci_release_msi(dev);
1041 (void) bus_release_resource(dev, pcs->rtp, pcs->rgd, pcs->regs);
1042 if (pcs->pci_isp.isp_param) {
1043 free(pcs->pci_isp.isp_param, M_DEVBUF);
1044 pcs->pci_isp.isp_param = NULL;
1046 if (pcs->pci_isp.isp_osinfo.pc.ptr) {
1047 free(pcs->pci_isp.isp_osinfo.pc.ptr, M_DEVBUF);
1048 pcs->pci_isp.isp_osinfo.pc.ptr = NULL;
1053 #define IspVirt2Off(a, x) \
1054 (((struct isp_pcisoftc *)a)->pci_poff[((x) & _BLK_REG_MASK) >> \
1055 _BLK_REG_SHFT] + ((x) & 0xfff))
1057 #define BXR2(isp, off) \
1058 bus_space_read_2(isp->isp_bus_tag, isp->isp_bus_handle, off)
1059 #define BXW2(isp, off, v) \
1060 bus_space_write_2(isp->isp_bus_tag, isp->isp_bus_handle, off, v)
1061 #define BXR4(isp, off) \
1062 bus_space_read_4(isp->isp_bus_tag, isp->isp_bus_handle, off)
1063 #define BXW4(isp, off, v) \
1064 bus_space_write_4(isp->isp_bus_tag, isp->isp_bus_handle, off, v)
1067 static ISP_INLINE int
1068 isp_pci_rd_debounced(ispsoftc_t *isp, int off, uint16_t *rp)
1070 uint32_t val0, val1;
1074 val0 = BXR2(isp, IspVirt2Off(isp, off));
1075 val1 = BXR2(isp, IspVirt2Off(isp, off));
1076 } while (val0 != val1 && ++i < 1000);
1085 isp_pci_rd_isr(ispsoftc_t *isp, uint32_t *isrp, uint16_t *semap, uint16_t *mbp)
1090 if (isp_pci_rd_debounced(isp, BIU_ISR, &isr)) {
1093 if (isp_pci_rd_debounced(isp, BIU_SEMA, &sema)) {
1097 isr = BXR2(isp, IspVirt2Off(isp, BIU_ISR));
1098 sema = BXR2(isp, IspVirt2Off(isp, BIU_SEMA));
1100 isp_prt(isp, ISP_LOGDEBUG3, "ISR 0x%x SEMA 0x%x", isr, sema);
1101 isr &= INT_PENDING_MASK(isp);
1102 sema &= BIU_SEMA_LOCK;
1103 if (isr == 0 && sema == 0) {
1107 if ((*semap = sema) != 0) {
1109 if (isp_pci_rd_debounced(isp, OUTMAILBOX0, mbp)) {
1113 *mbp = BXR2(isp, IspVirt2Off(isp, OUTMAILBOX0));
1120 isp_pci_rd_isr_2300(ispsoftc_t *isp, uint32_t *isrp, uint16_t *semap, uint16_t *mbox0p)
1125 if (!(BXR2(isp, IspVirt2Off(isp, BIU_ISR) & BIU2100_ISR_RISC_INT))) {
1129 r2hisr = BXR4(isp, IspVirt2Off(isp, BIU_R2HSTSLO));
1130 isp_prt(isp, ISP_LOGDEBUG3, "RISC2HOST ISR 0x%x", r2hisr);
1131 if ((r2hisr & BIU_R2HST_INTR) == 0) {
1135 switch (r2hisr & BIU_R2HST_ISTAT_MASK) {
1136 case ISPR2HST_ROM_MBX_OK:
1137 case ISPR2HST_ROM_MBX_FAIL:
1138 case ISPR2HST_MBX_OK:
1139 case ISPR2HST_MBX_FAIL:
1140 case ISPR2HST_ASYNC_EVENT:
1141 *isrp = r2hisr & 0xffff;
1142 *mbox0p = (r2hisr >> 16);
1145 case ISPR2HST_RIO_16:
1146 *isrp = r2hisr & 0xffff;
1147 *mbox0p = ASYNC_RIO16_1;
1150 case ISPR2HST_FPOST:
1151 *isrp = r2hisr & 0xffff;
1152 *mbox0p = ASYNC_CMD_CMPLT;
1155 case ISPR2HST_FPOST_CTIO:
1156 *isrp = r2hisr & 0xffff;
1157 *mbox0p = ASYNC_CTIO_DONE;
1160 case ISPR2HST_RSPQ_UPDATE:
1161 *isrp = r2hisr & 0xffff;
1166 hccr = ISP_READ(isp, HCCR);
1167 if (hccr & HCCR_PAUSE) {
1168 ISP_WRITE(isp, HCCR, HCCR_RESET);
1169 isp_prt(isp, ISP_LOGERR, "RISC paused at interrupt (%x->%x)", hccr, ISP_READ(isp, HCCR));
1170 ISP_WRITE(isp, BIU_ICR, 0);
1172 isp_prt(isp, ISP_LOGERR, "unknown interrupt 0x%x\n", r2hisr);
1179 isp_pci_rd_isr_2400(ispsoftc_t *isp, uint32_t *isrp, uint16_t *semap, uint16_t *mbox0p)
1183 r2hisr = BXR4(isp, IspVirt2Off(isp, BIU2400_R2HSTSLO));
1184 isp_prt(isp, ISP_LOGDEBUG3, "RISC2HOST ISR 0x%x", r2hisr);
1185 if ((r2hisr & BIU2400_R2HST_INTR) == 0) {
1189 switch (r2hisr & BIU2400_R2HST_ISTAT_MASK) {
1190 case ISP2400R2HST_ROM_MBX_OK:
1191 case ISP2400R2HST_ROM_MBX_FAIL:
1192 case ISP2400R2HST_MBX_OK:
1193 case ISP2400R2HST_MBX_FAIL:
1194 case ISP2400R2HST_ASYNC_EVENT:
1195 *isrp = r2hisr & 0xffff;
1196 *mbox0p = (r2hisr >> 16);
1199 case ISP2400R2HST_RSPQ_UPDATE:
1200 case ISP2400R2HST_ATIO_RSPQ_UPDATE:
1201 case ISP2400R2HST_ATIO_RQST_UPDATE:
1202 *isrp = r2hisr & 0xffff;
1207 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
1208 isp_prt(isp, ISP_LOGERR, "unknown interrupt 0x%x\n", r2hisr);
1214 isp_pci_rd_reg(ispsoftc_t *isp, int regoff)
1219 if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
1221 * We will assume that someone has paused the RISC processor.
1223 oldconf = BXR2(isp, IspVirt2Off(isp, BIU_CONF1));
1224 BXW2(isp, IspVirt2Off(isp, BIU_CONF1), oldconf | BIU_PCI_CONF1_SXP);
1225 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1227 rv = BXR2(isp, IspVirt2Off(isp, regoff));
1228 if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
1229 BXW2(isp, IspVirt2Off(isp, BIU_CONF1), oldconf);
1230 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1236 isp_pci_wr_reg(ispsoftc_t *isp, int regoff, uint32_t val)
1240 if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
1242 * We will assume that someone has paused the RISC processor.
1244 oldconf = BXR2(isp, IspVirt2Off(isp, BIU_CONF1));
1245 BXW2(isp, IspVirt2Off(isp, BIU_CONF1),
1246 oldconf | BIU_PCI_CONF1_SXP);
1247 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1249 BXW2(isp, IspVirt2Off(isp, regoff), val);
1250 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, regoff), 2, -1);
1251 if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
1252 BXW2(isp, IspVirt2Off(isp, BIU_CONF1), oldconf);
1253 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1259 isp_pci_rd_reg_1080(ispsoftc_t *isp, int regoff)
1261 uint32_t rv, oc = 0;
1263 if ((regoff & _BLK_REG_MASK) == SXP_BLOCK ||
1264 (regoff & _BLK_REG_MASK) == (SXP_BLOCK|SXP_BANK1_SELECT)) {
1267 * We will assume that someone has paused the RISC processor.
1269 oc = BXR2(isp, IspVirt2Off(isp, BIU_CONF1));
1270 tc = oc & ~BIU_PCI1080_CONF1_DMA;
1271 if (regoff & SXP_BANK1_SELECT)
1272 tc |= BIU_PCI1080_CONF1_SXP1;
1274 tc |= BIU_PCI1080_CONF1_SXP0;
1275 BXW2(isp, IspVirt2Off(isp, BIU_CONF1), tc);
1276 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1277 } else if ((regoff & _BLK_REG_MASK) == DMA_BLOCK) {
1278 oc = BXR2(isp, IspVirt2Off(isp, BIU_CONF1));
1279 BXW2(isp, IspVirt2Off(isp, BIU_CONF1),
1280 oc | BIU_PCI1080_CONF1_DMA);
1281 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1283 rv = BXR2(isp, IspVirt2Off(isp, regoff));
1285 BXW2(isp, IspVirt2Off(isp, BIU_CONF1), oc);
1286 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1292 isp_pci_wr_reg_1080(ispsoftc_t *isp, int regoff, uint32_t val)
1296 if ((regoff & _BLK_REG_MASK) == SXP_BLOCK ||
1297 (regoff & _BLK_REG_MASK) == (SXP_BLOCK|SXP_BANK1_SELECT)) {
1300 * We will assume that someone has paused the RISC processor.
1302 oc = BXR2(isp, IspVirt2Off(isp, BIU_CONF1));
1303 tc = oc & ~BIU_PCI1080_CONF1_DMA;
1304 if (regoff & SXP_BANK1_SELECT)
1305 tc |= BIU_PCI1080_CONF1_SXP1;
1307 tc |= BIU_PCI1080_CONF1_SXP0;
1308 BXW2(isp, IspVirt2Off(isp, BIU_CONF1), tc);
1309 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1310 } else if ((regoff & _BLK_REG_MASK) == DMA_BLOCK) {
1311 oc = BXR2(isp, IspVirt2Off(isp, BIU_CONF1));
1312 BXW2(isp, IspVirt2Off(isp, BIU_CONF1),
1313 oc | BIU_PCI1080_CONF1_DMA);
1314 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1316 BXW2(isp, IspVirt2Off(isp, regoff), val);
1317 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, regoff), 2, -1);
1319 BXW2(isp, IspVirt2Off(isp, BIU_CONF1), oc);
1320 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1325 isp_pci_rd_reg_2400(ispsoftc_t *isp, int regoff)
1328 int block = regoff & _BLK_REG_MASK;
1334 return (BXR2(isp, IspVirt2Off(isp, regoff)));
1336 isp_prt(isp, ISP_LOGWARN, "SXP_BLOCK read at 0x%x", regoff);
1337 return (0xffffffff);
1339 isp_prt(isp, ISP_LOGWARN, "RISC_BLOCK read at 0x%x", regoff);
1340 return (0xffffffff);
1342 isp_prt(isp, ISP_LOGWARN, "DMA_BLOCK read at 0x%x", regoff);
1343 return (0xffffffff);
1345 isp_prt(isp, ISP_LOGWARN, "unknown block read at 0x%x", regoff);
1346 return (0xffffffff);
1351 case BIU2400_FLASH_ADDR:
1352 case BIU2400_FLASH_DATA:
1356 case BIU2400_REQINP:
1357 case BIU2400_REQOUTP:
1358 case BIU2400_RSPINP:
1359 case BIU2400_RSPOUTP:
1360 case BIU2400_PRI_REQINP:
1361 case BIU2400_PRI_REQOUTP:
1362 case BIU2400_ATIO_RSPINP:
1363 case BIU2400_ATIO_RSPOUTP:
1368 rv = BXR4(isp, IspVirt2Off(isp, regoff));
1370 case BIU2400_R2HSTSLO:
1371 rv = BXR4(isp, IspVirt2Off(isp, regoff));
1373 case BIU2400_R2HSTSHI:
1374 rv = BXR4(isp, IspVirt2Off(isp, regoff)) >> 16;
1377 isp_prt(isp, ISP_LOGERR,
1378 "isp_pci_rd_reg_2400: unknown offset %x", regoff);
1386 isp_pci_wr_reg_2400(ispsoftc_t *isp, int regoff, uint32_t val)
1388 int block = regoff & _BLK_REG_MASK;
1394 BXW2(isp, IspVirt2Off(isp, regoff), val);
1395 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, regoff), 2, -1);
1398 isp_prt(isp, ISP_LOGWARN, "SXP_BLOCK write at 0x%x", regoff);
1401 isp_prt(isp, ISP_LOGWARN, "RISC_BLOCK write at 0x%x", regoff);
1404 isp_prt(isp, ISP_LOGWARN, "DMA_BLOCK write at 0x%x", regoff);
1407 isp_prt(isp, ISP_LOGWARN, "unknown block write at 0x%x",
1413 case BIU2400_FLASH_ADDR:
1414 case BIU2400_FLASH_DATA:
1418 case BIU2400_REQINP:
1419 case BIU2400_REQOUTP:
1420 case BIU2400_RSPINP:
1421 case BIU2400_RSPOUTP:
1422 case BIU2400_PRI_REQINP:
1423 case BIU2400_PRI_REQOUTP:
1424 case BIU2400_ATIO_RSPINP:
1425 case BIU2400_ATIO_RSPOUTP:
1430 BXW4(isp, IspVirt2Off(isp, regoff), val);
1431 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, regoff), 4, -1);
1434 isp_prt(isp, ISP_LOGERR,
1435 "isp_pci_wr_reg_2400: bad offset 0x%x", regoff);
1448 static void imc(void *, bus_dma_segment_t *, int, int);
1449 static void imc1(void *, bus_dma_segment_t *, int, int);
1452 imc(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1454 struct imush *imushp = (struct imush *) arg;
1457 imushp->error = error;
1461 imushp->error = EINVAL;
1464 imushp->isp->isp_rquest = imushp->vbase;
1465 imushp->isp->isp_rquest_dma = segs->ds_addr;
1466 segs->ds_addr += ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(imushp->isp));
1467 imushp->vbase += ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(imushp->isp));
1468 imushp->isp->isp_result_dma = segs->ds_addr;
1469 imushp->isp->isp_result = imushp->vbase;
1471 #ifdef ISP_TARGET_MODE
1472 if (IS_24XX(imushp->isp)) {
1473 segs->ds_addr += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(imushp->isp));
1474 imushp->vbase += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(imushp->isp));
1475 imushp->isp->isp_atioq_dma = segs->ds_addr;
1476 imushp->isp->isp_atioq = imushp->vbase;
1482 imc1(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1484 struct imush *imushp = (struct imush *) arg;
1486 imushp->error = error;
1490 imushp->error = EINVAL;
1493 FCPARAM(imushp->isp, imushp->chan)->isp_scdma = segs->ds_addr;
1494 FCPARAM(imushp->isp, imushp->chan)->isp_scratch = imushp->vbase;
1498 isp_pci_mbxdma(ispsoftc_t *isp)
1502 int i, error, ns, cmap = 0;
1503 bus_size_t slim; /* segment size */
1504 bus_addr_t llim; /* low limit of unavailable dma */
1505 bus_addr_t hlim; /* high limit of unavailable dma */
1509 * Already been here? If so, leave...
1511 if (isp->isp_rquest) {
1516 if (isp->isp_maxcmds == 0) {
1517 isp_prt(isp, ISP_LOGERR, "maxcmds not set");
1522 hlim = BUS_SPACE_MAXADDR;
1523 if (IS_ULTRA2(isp) || IS_FC(isp) || IS_1240(isp)) {
1524 if (sizeof (bus_size_t) > 4) {
1525 slim = (bus_size_t) (1ULL << 32);
1527 slim = (bus_size_t) (1UL << 31);
1529 llim = BUS_SPACE_MAXADDR;
1531 llim = BUS_SPACE_MAXADDR_32BIT;
1535 len = isp->isp_maxcmds * sizeof (struct isp_pcmd);
1536 isp->isp_osinfo.pcmd_pool = (struct isp_pcmd *) malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
1537 if (isp->isp_osinfo.pcmd_pool == NULL) {
1538 isp_prt(isp, ISP_LOGERR, "cannot allocate pcmds");
1544 * XXX: We don't really support 64 bit target mode for parallel scsi yet
1546 #ifdef ISP_TARGET_MODE
1547 if (IS_SCSI(isp) && sizeof (bus_addr_t) > 4) {
1548 free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
1549 isp_prt(isp, ISP_LOGERR, "we cannot do DAC for SPI cards yet");
1555 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)) {
1556 free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
1558 isp_prt(isp, ISP_LOGERR, "could not create master dma tag");
1562 len = sizeof (isp_hdl_t) * isp->isp_maxcmds;
1563 isp->isp_xflist = (isp_hdl_t *) malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
1564 if (isp->isp_xflist == NULL) {
1565 free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
1567 isp_prt(isp, ISP_LOGERR, "cannot alloc xflist array");
1570 for (len = 0; len < isp->isp_maxcmds - 1; len++) {
1571 isp->isp_xflist[len].cmd = &isp->isp_xflist[len+1];
1573 isp->isp_xffree = isp->isp_xflist;
1574 #ifdef ISP_TARGET_MODE
1575 len = sizeof (isp_hdl_t) * isp->isp_maxcmds;
1576 isp->isp_tgtlist = (isp_hdl_t *) malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
1577 if (isp->isp_tgtlist == NULL) {
1578 free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
1579 free(isp->isp_xflist, M_DEVBUF);
1581 isp_prt(isp, ISP_LOGERR, "cannot alloc tgtlist array");
1584 for (len = 0; len < isp->isp_maxcmds - 1; len++) {
1585 isp->isp_tgtlist[len].cmd = &isp->isp_tgtlist[len+1];
1587 isp->isp_tgtfree = isp->isp_tgtlist;
1591 * Allocate and map the request and result queues (and ATIO queue
1592 * if we're a 2400 supporting target mode).
1594 len = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
1595 len += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
1596 #ifdef ISP_TARGET_MODE
1598 len += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
1602 ns = (len / PAGE_SIZE) + 1;
1605 * Create a tag for the control spaces. We don't always need this
1606 * to be 32 bits, but we do this for simplicity and speed's sake.
1608 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)) {
1609 isp_prt(isp, ISP_LOGERR, "cannot create a dma tag for control spaces");
1610 free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
1611 free(isp->isp_xflist, M_DEVBUF);
1612 #ifdef ISP_TARGET_MODE
1613 free(isp->isp_tgtlist, M_DEVBUF);
1619 if (bus_dmamem_alloc(isp->isp_osinfo.cdmat, (void **)&base, BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &isp->isp_osinfo.cdmap) != 0) {
1620 isp_prt(isp, ISP_LOGERR, "cannot allocate %d bytes of CCB memory", len);
1621 bus_dma_tag_destroy(isp->isp_osinfo.cdmat);
1622 free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
1623 free(isp->isp_xflist, M_DEVBUF);
1624 #ifdef ISP_TARGET_MODE
1625 free(isp->isp_tgtlist, M_DEVBUF);
1636 bus_dmamap_load(isp->isp_osinfo.cdmat, isp->isp_osinfo.cdmap, base, len, imc, &im, 0);
1638 isp_prt(isp, ISP_LOGERR, "error %d loading dma map for control areas", im.error);
1643 for (cmap = 0; cmap < isp->isp_nchan; cmap++) {
1644 struct isp_fc *fc = ISP_FC_PC(isp, cmap);
1645 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)) {
1648 if (bus_dmamem_alloc(fc->tdmat, (void **)&base, BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &fc->tdmap) != 0) {
1649 bus_dma_tag_destroy(fc->tdmat);
1656 bus_dmamap_load(fc->tdmat, fc->tdmap, base, ISP_FC_SCRLEN, imc1, &im, 0);
1658 bus_dmamem_free(fc->tdmat, base, fc->tdmap);
1659 bus_dma_tag_destroy(fc->tdmat);
1665 for (i = 0; i < isp->isp_maxcmds; i++) {
1666 struct isp_pcmd *pcmd = &isp->isp_osinfo.pcmd_pool[i];
1667 error = bus_dmamap_create(isp->isp_osinfo.dmat, 0, &pcmd->dmap);
1669 isp_prt(isp, ISP_LOGERR, "error %d creating per-cmd DMA maps", error);
1671 bus_dmamap_destroy(isp->isp_osinfo.dmat, isp->isp_osinfo.pcmd_pool[i].dmap);
1675 callout_init_mtx(&pcmd->wdog, &isp->isp_osinfo.lock, 0);
1676 if (i == isp->isp_maxcmds-1) {
1679 pcmd->next = &isp->isp_osinfo.pcmd_pool[i+1];
1682 isp->isp_osinfo.pcmd_free = &isp->isp_osinfo.pcmd_pool[0];
1687 while (--cmap >= 0) {
1688 struct isp_fc *fc = ISP_FC_PC(isp, cmap);
1689 bus_dmamem_free(fc->tdmat, base, fc->tdmap);
1690 bus_dma_tag_destroy(fc->tdmat);
1692 bus_dmamem_free(isp->isp_osinfo.cdmat, base, isp->isp_osinfo.cdmap);
1693 bus_dma_tag_destroy(isp->isp_osinfo.cdmat);
1694 free(isp->isp_xflist, M_DEVBUF);
1695 #ifdef ISP_TARGET_MODE
1696 free(isp->isp_tgtlist, M_DEVBUF);
1698 free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
1699 isp->isp_rquest = NULL;
1707 void *rq; /* original request */
1712 #define MUSHERR_NOQENTRIES -2
1714 #ifdef ISP_TARGET_MODE
1715 static void tdma2_2(void *, bus_dma_segment_t *, int, bus_size_t, int);
1716 static void tdma2(void *, bus_dma_segment_t *, int, int);
1719 tdma2_2(void *arg, bus_dma_segment_t *dm_segs, int nseg, bus_size_t mapsize, int error)
1723 mp->mapsize = mapsize;
1724 tdma2(arg, dm_segs, nseg, error);
1728 tdma2(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1732 struct ccb_scsiio *csio;
1736 mp = (mush_t *) arg;
1741 csio = mp->cmd_token;
1745 if (sizeof (bus_addr_t) > 4) {
1746 if (nseg >= ISP_NSEG64_MAX) {
1747 isp_prt(isp, ISP_LOGERR, "number of segments (%d) exceed maximum we can support (%d)", nseg, ISP_NSEG64_MAX);
1751 if (rq->req_header.rqs_entry_type == RQSTYPE_CTIO2) {
1752 rq->req_header.rqs_entry_type = RQSTYPE_CTIO3;
1755 if (nseg >= ISP_NSEG_MAX) {
1756 isp_prt(isp, ISP_LOGERR, "number of segments (%d) exceed maximum we can support (%d)", nseg, ISP_NSEG_MAX);
1761 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1762 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_PREWRITE);
1763 ddir = ISP_TO_DEVICE;
1764 } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
1765 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_PREREAD);
1766 ddir = ISP_FROM_DEVICE;
1778 if (isp_send_tgt_cmd(isp, rq, dm_segs, nseg, XS_XFRLEN(csio), ddir, &csio->sense_data, csio->sense_len) != CMD_QUEUED) {
1779 mp->error = MUSHERR_NOQENTRIES;
1784 static void dma2_2(void *, bus_dma_segment_t *, int, bus_size_t, int);
1785 static void dma2(void *, bus_dma_segment_t *, int, int);
1788 dma2_2(void *arg, bus_dma_segment_t *dm_segs, int nseg, bus_size_t mapsize, int error)
1792 mp->mapsize = mapsize;
1793 dma2(arg, dm_segs, nseg, error);
1797 dma2(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1801 struct ccb_scsiio *csio;
1805 mp = (mush_t *) arg;
1810 csio = mp->cmd_token;
1814 if (sizeof (bus_addr_t) > 4) {
1815 if (nseg >= ISP_NSEG64_MAX) {
1816 isp_prt(isp, ISP_LOGERR, "number of segments (%d) exceed maximum we can support (%d)", nseg, ISP_NSEG64_MAX);
1820 if (rq->req_header.rqs_entry_type == RQSTYPE_T2RQS) {
1821 rq->req_header.rqs_entry_type = RQSTYPE_T3RQS;
1822 } else if (rq->req_header.rqs_entry_type == RQSTYPE_REQUEST) {
1823 rq->req_header.rqs_entry_type = RQSTYPE_A64;
1826 if (nseg >= ISP_NSEG_MAX) {
1827 isp_prt(isp, ISP_LOGERR, "number of segments (%d) exceed maximum we can support (%d)", nseg, ISP_NSEG_MAX);
1832 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1833 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_PREREAD);
1834 ddir = ISP_FROM_DEVICE;
1835 } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
1836 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_PREWRITE);
1837 ddir = ISP_TO_DEVICE;
1847 if (isp_send_cmd(isp, rq, dm_segs, nseg, XS_XFRLEN(csio), ddir) != CMD_QUEUED) {
1848 mp->error = MUSHERR_NOQENTRIES;
1853 isp_pci_dmasetup(ispsoftc_t *isp, struct ccb_scsiio *csio, void *ff)
1856 void (*eptr)(void *, bus_dma_segment_t *, int, int);
1857 void (*eptr2)(void *, bus_dma_segment_t *, int, bus_size_t, int);
1861 mp->cmd_token = csio;
1866 #ifdef ISP_TARGET_MODE
1867 if (csio->ccb_h.func_code == XPT_CONT_TARGET_IO) {
1878 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE || (csio->dxfer_len == 0)) {
1879 (*eptr)(mp, NULL, 0, 0);
1880 } else if ((csio->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
1881 if ((csio->ccb_h.flags & CAM_DATA_PHYS) == 0) {
1883 error = bus_dmamap_load(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, csio->data_ptr, csio->dxfer_len, eptr, mp, 0);
1885 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);
1888 if (error == EINPROGRESS) {
1889 bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
1891 isp_prt(isp, ISP_LOGERR, "deferred dma allocation not supported");
1892 } else if (error && mp->error == 0) {
1894 isp_prt(isp, ISP_LOGERR, "error %d in dma mapping code", error);
1899 /* Pointer to physical buffer */
1900 struct bus_dma_segment seg;
1901 seg.ds_addr = (bus_addr_t)(vm_offset_t)csio->data_ptr;
1902 seg.ds_len = csio->dxfer_len;
1903 (*eptr)(mp, &seg, 1, 0);
1906 struct bus_dma_segment *segs;
1908 if ((csio->ccb_h.flags & CAM_DATA_PHYS) != 0) {
1909 isp_prt(isp, ISP_LOGERR, "Physical segment pointers unsupported");
1911 } else if ((csio->ccb_h.flags & CAM_SG_LIST_PHYS) == 0) {
1916 * We're taking advantage of the fact that
1917 * the pointer/length sizes and layout of the iovec
1918 * structure are the same as the bus_dma_segment
1919 * structure. This might be a little dangerous,
1920 * but only if they change the structures, which
1923 KASSERT((sizeof (sguio.uio_iov) == sizeof (csio->data_ptr) &&
1924 sizeof (sguio.uio_iovcnt) >= sizeof (csio->sglist_cnt) &&
1925 sizeof (sguio.uio_resid) >= sizeof (csio->dxfer_len)), ("Ken's assumption failed"));
1926 sguio.uio_iov = (struct iovec *)csio->data_ptr;
1927 sguio.uio_iovcnt = csio->sglist_cnt;
1928 sguio.uio_resid = csio->dxfer_len;
1929 sguio.uio_segflg = UIO_SYSSPACE;
1931 error = bus_dmamap_load_uio(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, &sguio, eptr2, mp, 0);
1933 if (error != 0 && mp->error == 0) {
1934 isp_prt(isp, ISP_LOGERR, "error %d in dma mapping code", error);
1938 /* Just use the segments provided */
1939 segs = (struct bus_dma_segment *) csio->data_ptr;
1940 (*eptr)(mp, segs, csio->sglist_cnt, 0);
1944 int retval = CMD_COMPLETE;
1945 if (mp->error == MUSHERR_NOQENTRIES) {
1946 retval = CMD_EAGAIN;
1947 } else if (mp->error == EFBIG) {
1948 XS_SETERR(csio, CAM_REQ_TOO_BIG);
1949 } else if (mp->error == EINVAL) {
1950 XS_SETERR(csio, CAM_REQ_INVALID);
1952 XS_SETERR(csio, CAM_UNREC_HBA_ERROR);
1956 return (CMD_QUEUED);
1960 isp_pci_reset0(ispsoftc_t *isp)
1962 ISP_DISABLE_INTS(isp);
1966 isp_pci_reset1(ispsoftc_t *isp)
1968 if (!IS_24XX(isp)) {
1969 /* Make sure the BIOS is disabled */
1970 isp_pci_wr_reg(isp, HCCR, PCI_HCCR_CMD_BIOS);
1972 /* and enable interrupts */
1973 ISP_ENABLE_INTS(isp);
1977 isp_pci_dumpregs(ispsoftc_t *isp, const char *msg)
1979 struct isp_pcisoftc *pcs = (struct isp_pcisoftc *)isp;
1981 printf("%s: %s\n", device_get_nameunit(isp->isp_dev), msg);
1983 printf("%s:\n", device_get_nameunit(isp->isp_dev));
1985 printf(" biu_conf1=%x", ISP_READ(isp, BIU_CONF1));
1987 printf(" biu_csr=%x", ISP_READ(isp, BIU2100_CSR));
1988 printf(" biu_icr=%x biu_isr=%x biu_sema=%x ", ISP_READ(isp, BIU_ICR),
1989 ISP_READ(isp, BIU_ISR), ISP_READ(isp, BIU_SEMA));
1990 printf("risc_hccr=%x\n", ISP_READ(isp, HCCR));
1994 ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
1995 printf(" cdma_conf=%x cdma_sts=%x cdma_fifostat=%x\n",
1996 ISP_READ(isp, CDMA_CONF), ISP_READ(isp, CDMA_STATUS),
1997 ISP_READ(isp, CDMA_FIFO_STS));
1998 printf(" ddma_conf=%x ddma_sts=%x ddma_fifostat=%x\n",
1999 ISP_READ(isp, DDMA_CONF), ISP_READ(isp, DDMA_STATUS),
2000 ISP_READ(isp, DDMA_FIFO_STS));
2001 printf(" sxp_int=%x sxp_gross=%x sxp(scsi_ctrl)=%x\n",
2002 ISP_READ(isp, SXP_INTERRUPT),
2003 ISP_READ(isp, SXP_GROSS_ERR),
2004 ISP_READ(isp, SXP_PINS_CTRL));
2005 ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
2007 printf(" mbox regs: %x %x %x %x %x\n",
2008 ISP_READ(isp, OUTMAILBOX0), ISP_READ(isp, OUTMAILBOX1),
2009 ISP_READ(isp, OUTMAILBOX2), ISP_READ(isp, OUTMAILBOX3),
2010 ISP_READ(isp, OUTMAILBOX4));
2011 printf(" PCI Status Command/Status=%x\n",
2012 pci_read_config(pcs->pci_dev, PCIR_COMMAND, 1));