]> CyberLeo.Net >> Repos - FreeBSD/releng/9.1.git/blob - sys/dev/isp/isp_pci.c
MFC r241414:
[FreeBSD/releng/9.1.git] / sys / dev / isp / isp_pci.c
1 /*-
2  * Copyright (c) 1997-2008 by Matthew Jacob
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 /*
27  * PCI specific probe and attach routines for Qlogic ISP SCSI adapters.
28  * FreeBSD Version.
29  */
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
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>
39 #include <sys/bus.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>
45 #include <sys/rman.h>
46 #include <sys/malloc.h>
47 #include <sys/uio.h>
48
49 #ifdef __sparc64__
50 #include <dev/ofw/openfirm.h>
51 #include <machine/ofw_machdep.h>
52 #endif
53
54 #include <dev/isp/isp_freebsd.h>
55
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 *);
67
68
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 *);
72
73 static struct ispmdvec mdvec = {
74         isp_pci_rd_isr,
75         isp_pci_rd_reg,
76         isp_pci_wr_reg,
77         isp_pci_mbxdma,
78         isp_pci_dmasetup,
79         isp_common_dmateardown,
80         isp_pci_reset0,
81         isp_pci_reset1,
82         isp_pci_dumpregs,
83         NULL,
84         BIU_BURST_ENABLE|BIU_PCI_CONF1_FIFO_64
85 };
86
87 static struct ispmdvec mdvec_1080 = {
88         isp_pci_rd_isr,
89         isp_pci_rd_reg_1080,
90         isp_pci_wr_reg_1080,
91         isp_pci_mbxdma,
92         isp_pci_dmasetup,
93         isp_common_dmateardown,
94         isp_pci_reset0,
95         isp_pci_reset1,
96         isp_pci_dumpregs,
97         NULL,
98         BIU_BURST_ENABLE|BIU_PCI_CONF1_FIFO_64
99 };
100
101 static struct ispmdvec mdvec_12160 = {
102         isp_pci_rd_isr,
103         isp_pci_rd_reg_1080,
104         isp_pci_wr_reg_1080,
105         isp_pci_mbxdma,
106         isp_pci_dmasetup,
107         isp_common_dmateardown,
108         isp_pci_reset0,
109         isp_pci_reset1,
110         isp_pci_dumpregs,
111         NULL,
112         BIU_BURST_ENABLE|BIU_PCI_CONF1_FIFO_64
113 };
114
115 static struct ispmdvec mdvec_2100 = {
116         isp_pci_rd_isr,
117         isp_pci_rd_reg,
118         isp_pci_wr_reg,
119         isp_pci_mbxdma,
120         isp_pci_dmasetup,
121         isp_common_dmateardown,
122         isp_pci_reset0,
123         isp_pci_reset1,
124         isp_pci_dumpregs
125 };
126
127 static struct ispmdvec mdvec_2200 = {
128         isp_pci_rd_isr,
129         isp_pci_rd_reg,
130         isp_pci_wr_reg,
131         isp_pci_mbxdma,
132         isp_pci_dmasetup,
133         isp_common_dmateardown,
134         isp_pci_reset0,
135         isp_pci_reset1,
136         isp_pci_dumpregs
137 };
138
139 static struct ispmdvec mdvec_2300 = {
140         isp_pci_rd_isr_2300,
141         isp_pci_rd_reg,
142         isp_pci_wr_reg,
143         isp_pci_mbxdma,
144         isp_pci_dmasetup,
145         isp_common_dmateardown,
146         isp_pci_reset0,
147         isp_pci_reset1,
148         isp_pci_dumpregs
149 };
150
151 static struct ispmdvec mdvec_2400 = {
152         isp_pci_rd_isr_2400,
153         isp_pci_rd_reg_2400,
154         isp_pci_wr_reg_2400,
155         isp_pci_mbxdma,
156         isp_pci_dmasetup,
157         isp_common_dmateardown,
158         isp_pci_reset0,
159         isp_pci_reset1,
160         NULL
161 };
162
163 static struct ispmdvec mdvec_2500 = {
164         isp_pci_rd_isr_2400,
165         isp_pci_rd_reg_2400,
166         isp_pci_wr_reg_2400,
167         isp_pci_mbxdma,
168         isp_pci_dmasetup,
169         isp_common_dmateardown,
170         isp_pci_reset0,
171         isp_pci_reset1,
172         NULL
173 };
174
175 #ifndef PCIM_CMD_INVEN
176 #define PCIM_CMD_INVEN                  0x10
177 #endif
178 #ifndef PCIM_CMD_BUSMASTEREN
179 #define PCIM_CMD_BUSMASTEREN            0x0004
180 #endif
181 #ifndef PCIM_CMD_PERRESPEN
182 #define PCIM_CMD_PERRESPEN              0x0040
183 #endif
184 #ifndef PCIM_CMD_SEREN
185 #define PCIM_CMD_SEREN                  0x0100
186 #endif
187 #ifndef PCIM_CMD_INTX_DISABLE
188 #define PCIM_CMD_INTX_DISABLE           0x0400
189 #endif
190
191 #ifndef PCIR_COMMAND
192 #define PCIR_COMMAND                    0x04
193 #endif
194
195 #ifndef PCIR_CACHELNSZ
196 #define PCIR_CACHELNSZ                  0x0c
197 #endif
198
199 #ifndef PCIR_LATTIMER
200 #define PCIR_LATTIMER                   0x0d
201 #endif
202
203 #ifndef PCIR_ROMADDR
204 #define PCIR_ROMADDR                    0x30
205 #endif
206
207 #ifndef PCI_VENDOR_QLOGIC
208 #define PCI_VENDOR_QLOGIC               0x1077
209 #endif
210
211 #ifndef PCI_PRODUCT_QLOGIC_ISP1020
212 #define PCI_PRODUCT_QLOGIC_ISP1020      0x1020
213 #endif
214
215 #ifndef PCI_PRODUCT_QLOGIC_ISP1080
216 #define PCI_PRODUCT_QLOGIC_ISP1080      0x1080
217 #endif
218
219 #ifndef PCI_PRODUCT_QLOGIC_ISP10160
220 #define PCI_PRODUCT_QLOGIC_ISP10160     0x1016
221 #endif
222
223 #ifndef PCI_PRODUCT_QLOGIC_ISP12160
224 #define PCI_PRODUCT_QLOGIC_ISP12160     0x1216
225 #endif
226
227 #ifndef PCI_PRODUCT_QLOGIC_ISP1240
228 #define PCI_PRODUCT_QLOGIC_ISP1240      0x1240
229 #endif
230
231 #ifndef PCI_PRODUCT_QLOGIC_ISP1280
232 #define PCI_PRODUCT_QLOGIC_ISP1280      0x1280
233 #endif
234
235 #ifndef PCI_PRODUCT_QLOGIC_ISP2100
236 #define PCI_PRODUCT_QLOGIC_ISP2100      0x2100
237 #endif
238
239 #ifndef PCI_PRODUCT_QLOGIC_ISP2200
240 #define PCI_PRODUCT_QLOGIC_ISP2200      0x2200
241 #endif
242
243 #ifndef PCI_PRODUCT_QLOGIC_ISP2300
244 #define PCI_PRODUCT_QLOGIC_ISP2300      0x2300
245 #endif
246
247 #ifndef PCI_PRODUCT_QLOGIC_ISP2312
248 #define PCI_PRODUCT_QLOGIC_ISP2312      0x2312
249 #endif
250
251 #ifndef PCI_PRODUCT_QLOGIC_ISP2322
252 #define PCI_PRODUCT_QLOGIC_ISP2322      0x2322
253 #endif
254
255 #ifndef PCI_PRODUCT_QLOGIC_ISP2422
256 #define PCI_PRODUCT_QLOGIC_ISP2422      0x2422
257 #endif
258
259 #ifndef PCI_PRODUCT_QLOGIC_ISP2432
260 #define PCI_PRODUCT_QLOGIC_ISP2432      0x2432
261 #endif
262
263 #ifndef PCI_PRODUCT_QLOGIC_ISP2532
264 #define PCI_PRODUCT_QLOGIC_ISP2532      0x2532
265 #endif
266
267 #ifndef PCI_PRODUCT_QLOGIC_ISP6312
268 #define PCI_PRODUCT_QLOGIC_ISP6312      0x6312
269 #endif
270
271 #ifndef PCI_PRODUCT_QLOGIC_ISP6322
272 #define PCI_PRODUCT_QLOGIC_ISP6322      0x6322
273 #endif
274
275 #ifndef        PCI_PRODUCT_QLOGIC_ISP5432
276 #define        PCI_PRODUCT_QLOGIC_ISP5432      0x5432
277 #endif
278
279 #define        PCI_QLOGIC_ISP5432      \
280        ((PCI_PRODUCT_QLOGIC_ISP5432 << 16) | PCI_VENDOR_QLOGIC)
281
282 #define PCI_QLOGIC_ISP1020      \
283         ((PCI_PRODUCT_QLOGIC_ISP1020 << 16) | PCI_VENDOR_QLOGIC)
284
285 #define PCI_QLOGIC_ISP1080      \
286         ((PCI_PRODUCT_QLOGIC_ISP1080 << 16) | PCI_VENDOR_QLOGIC)
287
288 #define PCI_QLOGIC_ISP10160     \
289         ((PCI_PRODUCT_QLOGIC_ISP10160 << 16) | PCI_VENDOR_QLOGIC)
290
291 #define PCI_QLOGIC_ISP12160     \
292         ((PCI_PRODUCT_QLOGIC_ISP12160 << 16) | PCI_VENDOR_QLOGIC)
293
294 #define PCI_QLOGIC_ISP1240      \
295         ((PCI_PRODUCT_QLOGIC_ISP1240 << 16) | PCI_VENDOR_QLOGIC)
296
297 #define PCI_QLOGIC_ISP1280      \
298         ((PCI_PRODUCT_QLOGIC_ISP1280 << 16) | PCI_VENDOR_QLOGIC)
299
300 #define PCI_QLOGIC_ISP2100      \
301         ((PCI_PRODUCT_QLOGIC_ISP2100 << 16) | PCI_VENDOR_QLOGIC)
302
303 #define PCI_QLOGIC_ISP2200      \
304         ((PCI_PRODUCT_QLOGIC_ISP2200 << 16) | PCI_VENDOR_QLOGIC)
305
306 #define PCI_QLOGIC_ISP2300      \
307         ((PCI_PRODUCT_QLOGIC_ISP2300 << 16) | PCI_VENDOR_QLOGIC)
308
309 #define PCI_QLOGIC_ISP2312      \
310         ((PCI_PRODUCT_QLOGIC_ISP2312 << 16) | PCI_VENDOR_QLOGIC)
311
312 #define PCI_QLOGIC_ISP2322      \
313         ((PCI_PRODUCT_QLOGIC_ISP2322 << 16) | PCI_VENDOR_QLOGIC)
314
315 #define PCI_QLOGIC_ISP2422      \
316         ((PCI_PRODUCT_QLOGIC_ISP2422 << 16) | PCI_VENDOR_QLOGIC)
317
318 #define PCI_QLOGIC_ISP2432      \
319         ((PCI_PRODUCT_QLOGIC_ISP2432 << 16) | PCI_VENDOR_QLOGIC)
320
321 #define PCI_QLOGIC_ISP2532      \
322         ((PCI_PRODUCT_QLOGIC_ISP2532 << 16) | PCI_VENDOR_QLOGIC)
323
324 #define PCI_QLOGIC_ISP6312      \
325         ((PCI_PRODUCT_QLOGIC_ISP6312 << 16) | PCI_VENDOR_QLOGIC)
326
327 #define PCI_QLOGIC_ISP6322      \
328         ((PCI_PRODUCT_QLOGIC_ISP6322 << 16) | PCI_VENDOR_QLOGIC)
329
330 /*
331  * Odd case for some AMI raid cards... We need to *not* attach to this.
332  */
333 #define AMI_RAID_SUBVENDOR_ID   0x101e
334
335 #define IO_MAP_REG      0x10
336 #define MEM_MAP_REG     0x14
337
338 #define PCI_DFLT_LTNCY  0x40
339 #define PCI_DFLT_LNSZ   0x10
340
341 static int isp_pci_probe (device_t);
342 static int isp_pci_attach (device_t);
343 static int isp_pci_detach (device_t);
344
345
346 #define ISP_PCD(isp)    ((struct isp_pcisoftc *)isp)->pci_dev
347 struct isp_pcisoftc {
348         ispsoftc_t                      pci_isp;
349         device_t                        pci_dev;
350         struct resource *               regs;
351         void *                          irq;
352         int                             iqd;
353         int                             rtp;
354         int                             rgd;
355         void *                          ih;
356         int16_t                         pci_poff[_NREG_BLKS];
357         bus_dma_tag_t                   dmat;
358         int                             msicount;
359 };
360
361
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),
367         { 0, 0 }
368 };
369
370 static driver_t isp_pci_driver = {
371         "isp", isp_pci_methods, sizeof (struct isp_pcisoftc)
372 };
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
378 static int
379 isp_pci_probe(device_t dev)
380 {
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");
384                 break;
385         case PCI_QLOGIC_ISP1080:
386                 device_set_desc(dev, "Qlogic ISP 1080 PCI SCSI Adapter");
387                 break;
388         case PCI_QLOGIC_ISP1240:
389                 device_set_desc(dev, "Qlogic ISP 1240 PCI SCSI Adapter");
390                 break;
391         case PCI_QLOGIC_ISP1280:
392                 device_set_desc(dev, "Qlogic ISP 1280 PCI SCSI Adapter");
393                 break;
394         case PCI_QLOGIC_ISP10160:
395                 device_set_desc(dev, "Qlogic ISP 10160 PCI SCSI Adapter");
396                 break;
397         case PCI_QLOGIC_ISP12160:
398                 if (pci_get_subvendor(dev) == AMI_RAID_SUBVENDOR_ID) {
399                         return (ENXIO);
400                 }
401                 device_set_desc(dev, "Qlogic ISP 12160 PCI SCSI Adapter");
402                 break;
403         case PCI_QLOGIC_ISP2100:
404                 device_set_desc(dev, "Qlogic ISP 2100 PCI FC-AL Adapter");
405                 break;
406         case PCI_QLOGIC_ISP2200:
407                 device_set_desc(dev, "Qlogic ISP 2200 PCI FC-AL Adapter");
408                 break;
409         case PCI_QLOGIC_ISP2300:
410                 device_set_desc(dev, "Qlogic ISP 2300 PCI FC-AL Adapter");
411                 break;
412         case PCI_QLOGIC_ISP2312:
413                 device_set_desc(dev, "Qlogic ISP 2312 PCI FC-AL Adapter");
414                 break;
415         case PCI_QLOGIC_ISP2322:
416                 device_set_desc(dev, "Qlogic ISP 2322 PCI FC-AL Adapter");
417                 break;
418         case PCI_QLOGIC_ISP2422:
419                 device_set_desc(dev, "Qlogic ISP 2422 PCI FC-AL Adapter");
420                 break;
421         case PCI_QLOGIC_ISP2432:
422                 device_set_desc(dev, "Qlogic ISP 2432 PCI FC-AL Adapter");
423                 break;
424         case PCI_QLOGIC_ISP2532:
425                 device_set_desc(dev, "Qlogic ISP 2532 PCI FC-AL Adapter");
426                 break;
427         case PCI_QLOGIC_ISP5432:
428                 device_set_desc(dev, "Qlogic ISP 5432 PCI FC-AL Adapter");
429                 break;
430         case PCI_QLOGIC_ISP6312:
431                 device_set_desc(dev, "Qlogic ISP 6312 PCI FC-AL Adapter");
432                 break;
433         case PCI_QLOGIC_ISP6322:
434                 device_set_desc(dev, "Qlogic ISP 6322 PCI FC-AL Adapter");
435                 break;
436         default:
437                 return (ENXIO);
438         }
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);
444                 isp_announced++;
445         }
446         /*
447          * XXXX: Here is where we might load the f/w module
448          * XXXX: (or increase a reference count to it).
449          */
450         return (BUS_PROBE_DEFAULT);
451 }
452
453 static void
454 isp_get_generic_options(device_t dev, ispsoftc_t *isp, int *nvp)
455 {
456         int tval;
457
458         /*
459          * Figure out if we're supposed to skip this one.
460          */
461         tval = 0;
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;
465                 return;
466         }
467         
468         tval = 0;
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;
471         }
472         tval = 0;
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;
475         }
476         tval = 0;
477         (void) resource_int_value(device_get_name(dev), device_get_unit(dev), "debug", &tval);
478         if (tval) {
479                 isp->isp_dblev = tval;
480         } else {
481                 isp->isp_dblev = ISP_LOGWARN|ISP_LOGERR;
482         }
483         if (bootverbose) {
484                 isp->isp_dblev |= ISP_LOGCONFIG|ISP_LOGINFO;
485         }
486         tval = 0;
487         (void) resource_int_value(device_get_name(dev), device_get_unit(dev), "vports", &tval);
488         if (tval > 0 && tval < 127) {
489                 *nvp =  tval;
490         } else {
491                 *nvp = 0;
492         }
493         tval = 1;
494         (void) resource_int_value(device_get_name(dev), device_get_unit(dev), "autoconfig", &tval);
495         isp_autoconfig = tval;
496         tval = 7;
497         (void) resource_int_value(device_get_name(dev), device_get_unit(dev), "quickboot_time", &tval);
498         isp_quickboot_time = tval;
499
500         tval = 0;
501         if (resource_int_value(device_get_name(dev), device_get_unit(dev), "forcemulti", &tval) == 0 && tval != 0) {
502                 isp->isp_osinfo.forcemulti = 1;
503         }
504 }
505
506 static void
507 isp_get_pci_options(device_t dev, int *m1, int *m2)
508 {
509         int tval;
510         /*
511          * Which we should try first - memory mapping or i/o mapping?
512          *
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.
515          */
516         *m1 = PCIM_CMD_MEMEN;
517         *m2 = PCIM_CMD_PORTEN;
518
519         tval = 0;
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;
523         }
524         tval = 0;
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;
528         }
529 }
530
531 static void
532 isp_get_specific_options(device_t dev, int chan, ispsoftc_t *isp)
533 {
534         const char *sptr;
535         int tval;
536
537         if (resource_int_value(device_get_name(dev), device_get_unit(dev), "iid", &tval)) {
538                 if (IS_FC(isp)) {
539                         ISP_FC_PC(isp, chan)->default_id = 109 - chan;
540                 } else {
541 #ifdef __sparc64__
542                         ISP_SPI_PC(isp, chan)->iid = OF_getscsinitid(dev);
543 #else
544                         ISP_SPI_PC(isp, chan)->iid = 7;
545 #endif
546                 }
547         } else {
548                 if (IS_FC(isp)) {
549                         ISP_FC_PC(isp, chan)->default_id = tval - chan;
550                 } else {
551                         ISP_SPI_PC(isp, chan)->iid = tval;
552                 }
553                 isp->isp_confopts |= ISP_CFG_OWNLOOPID;
554         }
555
556         tval = -1;
557         if (resource_int_value(device_get_name(dev), device_get_unit(dev), "role", &tval) == 0) {
558                 switch (tval) {
559                 case ISP_ROLE_NONE:
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);
564                         break;
565                 default:
566                         tval = -1;
567                         break;
568                 }
569         }
570         if (tval == -1) {
571                 tval = ISP_DEFAULT_ROLES;
572         }
573
574         if (IS_SCSI(isp)) {
575                 ISP_SPI_PC(isp, chan)->def_role = tval;
576                 return;
577         }
578         ISP_FC_PC(isp, chan)->def_role = tval;
579
580         tval = 0;
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;
583         }
584         sptr = 0;
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;
594                 }
595         }
596
597         /*
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.
604          */
605         sptr = 0;
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') {
608                 char *eptr = 0;
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;
613                 }
614         }
615
616         sptr = 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') {
619                 char *eptr = 0;
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;
624                 }
625         }
626
627         tval = 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;
631         } else {
632                 ISP_FC_PC(isp, chan)->hysteresis = isp_fabric_hysteresis;
633         }
634
635         tval = -1;
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;
639         } else {
640                 ISP_FC_PC(isp, chan)->loop_down_limit = isp_loop_down_limit;
641         }
642
643         tval = -1;
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;
647         } else {
648                 ISP_FC_PC(isp, chan)->gone_device_time = isp_gone_device_time;
649         }
650 }
651
652 static int
653 isp_pci_attach(device_t dev)
654 {
655         int i, m1, m2, locksetup = 0;
656         int isp_nvports = 0;
657         uint32_t data, cmd, linesz, did;
658         struct isp_pcisoftc *pcs;
659         ispsoftc_t *isp;
660         size_t psize, xsize;
661         char fwname[32];
662
663         pcs = device_get_softc(dev);
664         if (pcs == NULL) {
665                 device_printf(dev, "cannot get softc\n");
666                 return (ENOMEM);
667         }
668         memset(pcs, 0, sizeof (*pcs));
669
670         pcs->pci_dev = dev;
671         isp = &pcs->pci_isp;
672         isp->isp_dev = dev;
673         isp->isp_nchan = 1;
674
675         /*
676          * Get Generic Options
677          */
678         isp_get_generic_options(dev, isp, &isp_nvports);
679
680         /*
681          * Check to see if options have us disabled
682          */
683         if (isp->isp_osinfo.disabled) {
684                 /*
685                  * But return zero to preserve unit numbering
686                  */
687                 return (0);
688         }
689
690         /*
691          * Get PCI options- which in this case are just mapping preferences.
692          */
693         isp_get_pci_options(dev, &m1, &m2);
694
695         linesz = PCI_DFLT_LNSZ;
696         pcs->irq = pcs->regs = NULL;
697         pcs->rgd = pcs->rtp = pcs->iqd = 0;
698
699         cmd = pci_read_config(dev, PCIR_COMMAND, 2);
700         if (cmd & m1) {
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);
704         }
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);
709         }
710         if (pcs->regs == NULL) {
711                 device_printf(dev, "unable to map any ports\n");
712                 goto bad;
713         }
714         if (bootverbose) {
715                 device_printf(dev, "using %s space register mapping\n", (pcs->rgd == IO_MAP_REG)? "I/O" : "Memory");
716         }
717         isp->isp_bus_tag = rman_get_bustag(pcs->regs);
718         isp->isp_bus_handle = rman_get_bushandle(pcs->regs);
719
720         pcs->pci_dev = dev;
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;
726
727         switch (pci_get_devid(dev)) {
728         case PCI_QLOGIC_ISP1020:
729                 did = 0x1040;
730                 isp->isp_mdvec = &mdvec;
731                 isp->isp_type = ISP_HA_SCSI_UNKNOWN;
732                 break;
733         case PCI_QLOGIC_ISP1080:
734                 did = 0x1080;
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;
738                 break;
739         case PCI_QLOGIC_ISP1240:
740                 did = 0x1080;
741                 isp->isp_mdvec = &mdvec_1080;
742                 isp->isp_type = ISP_HA_SCSI_1240;
743                 isp->isp_nchan = 2;
744                 pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] = ISP1080_DMA_REGS_OFF;
745                 break;
746         case PCI_QLOGIC_ISP1280:
747                 did = 0x1080;
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;
751                 break;
752         case PCI_QLOGIC_ISP10160:
753                 did = 0x12160;
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;
757                 break;
758         case PCI_QLOGIC_ISP12160:
759                 did = 0x12160;
760                 isp->isp_nchan = 2;
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;
764                 break;
765         case PCI_QLOGIC_ISP2100:
766                 did = 0x2100;
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) {
771                         /*
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
775                          * XXX; boards.
776                          */
777                         linesz = 1;
778                 }
779                 break;
780         case PCI_QLOGIC_ISP2200:
781                 did = 0x2200;
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;
785                 break;
786         case PCI_QLOGIC_ISP2300:
787                 did = 0x2300;
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;
791                 break;
792         case PCI_QLOGIC_ISP2312:
793         case PCI_QLOGIC_ISP6312:
794                 did = 0x2300;
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;
798                 break;
799         case PCI_QLOGIC_ISP2322:
800         case PCI_QLOGIC_ISP6322:
801                 did = 0x2322;
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;
805                 break;
806         case PCI_QLOGIC_ISP2422:
807         case PCI_QLOGIC_ISP2432:
808                 did = 0x2400;
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;
813                 break;
814         case PCI_QLOGIC_ISP2532:
815                 did = 0x2500;
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;
820                 break;
821         case PCI_QLOGIC_ISP5432:
822                 did = 0x2500;
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;
826                 break;
827         default:
828                 device_printf(dev, "unknown device type\n");
829                 goto bad;
830                 break;
831         }
832         isp->isp_revision = pci_get_revid(dev);
833
834         if (IS_FC(isp)) {
835                 psize = sizeof (fcparam);
836                 xsize = sizeof (struct isp_fc);
837         } else {
838                 psize = sizeof (sdparam);
839                 xsize = sizeof (struct isp_spi);
840         }
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");
846                 goto bad;
847         }
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");
851                 goto bad;
852         }
853
854         /*
855          * Now that we know who we are (roughly) get/set specific options
856          */
857         for (i = 0; i < isp->isp_nchan; i++) {
858                 isp_get_specific_options(dev, i, isp);
859         }
860
861         /*
862          * The 'it' suffix really only matters for SCSI cards in target mode.
863          */
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);
871         }
872         if (isp->isp_osinfo.fw == NULL) {
873                 snprintf(fwname, sizeof (fwname), "isp_%04x", did);
874                 isp->isp_osinfo.fw = firmware_get(fwname);
875         }
876         if (isp->isp_osinfo.fw != NULL) {
877                 isp->isp_mdvec->dv_ispfw = isp->isp_osinfo.fw->data;
878         }
879
880         /*
881          * Make sure that SERR, PERR, WRITE INVALIDATE and BUSMASTER
882          * are set.
883          */
884         cmd |= PCIM_CMD_SEREN | PCIM_CMD_PERRESPEN |
885                 PCIM_CMD_BUSMASTEREN | PCIM_CMD_INVEN;
886
887         if (IS_2300(isp)) {     /* per QLogic errata */
888                 cmd &= ~PCIM_CMD_INVEN;
889         }
890
891         if (IS_2322(isp) || pci_get_devid(dev) == PCI_QLOGIC_ISP6312) {
892                 cmd &= ~PCIM_CMD_INTX_DISABLE;
893         }
894
895         if (IS_24XX(isp)) {
896                 cmd &= ~PCIM_CMD_INTX_DISABLE;
897         }
898
899         pci_write_config(dev, PCIR_COMMAND, cmd, 2);
900
901         /*
902          * Make sure the Cache Line Size register is set sensibly.
903          */
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);
907                 data = linesz;
908                 pci_write_config(dev, PCIR_CACHELNSZ, data, 1);
909         }
910
911         /*
912          * Make sure the Latency Timer is sane.
913          */
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);
919         }
920
921         /*
922          * Make sure we've disabled the ROM.
923          */
924         data = pci_read_config(dev, PCIR_ROMADDR, 4);
925         data &= ~1;
926         pci_write_config(dev, PCIR_ROMADDR, data, 4);
927
928         /*
929          * Do MSI
930          *
931          * NB: MSI-X needs to be disabled for the 2432 (PCI-Express)
932          */
933         if (IS_24XX(isp) || IS_2322(isp)) {
934                 pcs->msicount = pci_msi_count(dev);
935                 if (pcs->msicount > 1) {
936                         pcs->msicount = 1;
937                 }
938                 if (pci_alloc_msi(dev, &pcs->msicount) == 0) {
939                         pcs->iqd = 1;
940                 } else {
941                         pcs->iqd = 0;
942                 }
943         }
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");
947                 goto bad;
948         }
949
950         /* Make sure the lock is set up. */
951         mtx_init(&isp->isp_osinfo.lock, "isp", NULL, MTX_DEF);
952         locksetup++;
953
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");
956                 goto bad;
957         }
958
959         /*
960          * Last minute checks...
961          */
962         if (IS_23XX(isp) || IS_24XX(isp)) {
963                 isp->isp_port = pci_get_function(dev);
964         }
965
966         /*
967          * Make sure we're in reset state.
968          */
969         ISP_LOCK(isp);
970         isp_reset(isp, 1);
971         if (isp->isp_state != ISP_RESETSTATE) {
972                 ISP_UNLOCK(isp);
973                 goto bad;
974         }
975         isp_init(isp);
976         if (isp->isp_state == ISP_INITSTATE) {
977                 isp->isp_state = ISP_RUNSTATE;
978         }
979         ISP_UNLOCK(isp);
980         if (isp_attach(isp)) {
981                 ISP_LOCK(isp);
982                 isp_uninit(isp);
983                 ISP_UNLOCK(isp);
984                 goto bad;
985         }
986         return (0);
987
988 bad:
989         if (pcs->ih) {
990                 (void) bus_teardown_intr(dev, pcs->irq, pcs->ih);
991         }
992         if (locksetup) {
993                 mtx_destroy(&isp->isp_osinfo.lock);
994         }
995         if (pcs->irq) {
996                 (void) bus_release_resource(dev, SYS_RES_IRQ, pcs->iqd, pcs->irq);
997         }
998         if (pcs->msicount) {
999                 pci_release_msi(dev);
1000         }
1001         if (pcs->regs) {
1002                 (void) bus_release_resource(dev, pcs->rtp, pcs->rgd, pcs->regs);
1003         }
1004         if (pcs->pci_isp.isp_param) {
1005                 free(pcs->pci_isp.isp_param, M_DEVBUF);
1006                 pcs->pci_isp.isp_param = NULL;
1007         }
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;
1011         }
1012         return (ENXIO);
1013 }
1014
1015 static int
1016 isp_pci_detach(device_t dev)
1017 {
1018         struct isp_pcisoftc *pcs;
1019         ispsoftc_t *isp;
1020         int status;
1021
1022         pcs = device_get_softc(dev);
1023         if (pcs == NULL) {
1024                 return (ENXIO);
1025         }
1026         isp = (ispsoftc_t *) pcs;
1027         status = isp_detach(isp);
1028         if (status)
1029                 return (status);
1030         ISP_LOCK(isp);
1031         isp_uninit(isp);
1032         if (pcs->ih) {
1033                 (void) bus_teardown_intr(dev, pcs->irq, pcs->ih);
1034         }
1035         ISP_UNLOCK(isp);
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);
1040         }
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;
1045         }
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;
1049         }
1050         return (0);
1051 }
1052
1053 #define IspVirt2Off(a, x)       \
1054         (((struct isp_pcisoftc *)a)->pci_poff[((x) & _BLK_REG_MASK) >> \
1055         _BLK_REG_SHFT] + ((x) & 0xfff))
1056
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)
1065
1066
1067 static ISP_INLINE int
1068 isp_pci_rd_debounced(ispsoftc_t *isp, int off, uint16_t *rp)
1069 {
1070         uint32_t val0, val1;
1071         int i = 0;
1072
1073         do {
1074                 val0 = BXR2(isp, IspVirt2Off(isp, off));
1075                 val1 = BXR2(isp, IspVirt2Off(isp, off));
1076         } while (val0 != val1 && ++i < 1000);
1077         if (val0 != val1) {
1078                 return (1);
1079         }
1080         *rp = val0;
1081         return (0);
1082 }
1083
1084 static int
1085 isp_pci_rd_isr(ispsoftc_t *isp, uint32_t *isrp, uint16_t *semap, uint16_t *mbp)
1086 {
1087         uint16_t isr, sema;
1088
1089         if (IS_2100(isp)) {
1090                 if (isp_pci_rd_debounced(isp, BIU_ISR, &isr)) {
1091                     return (0);
1092                 }
1093                 if (isp_pci_rd_debounced(isp, BIU_SEMA, &sema)) {
1094                     return (0);
1095                 }
1096         } else {
1097                 isr = BXR2(isp, IspVirt2Off(isp, BIU_ISR));
1098                 sema = BXR2(isp, IspVirt2Off(isp, BIU_SEMA));
1099         }
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) {
1104                 return (0);
1105         }
1106         *isrp = isr;
1107         if ((*semap = sema) != 0) {
1108                 if (IS_2100(isp)) {
1109                         if (isp_pci_rd_debounced(isp, OUTMAILBOX0, mbp)) {
1110                                 return (0);
1111                         }
1112                 } else {
1113                         *mbp = BXR2(isp, IspVirt2Off(isp, OUTMAILBOX0));
1114                 }
1115         }
1116         return (1);
1117 }
1118
1119 static int
1120 isp_pci_rd_isr_2300(ispsoftc_t *isp, uint32_t *isrp, uint16_t *semap, uint16_t *mbox0p)
1121 {
1122         uint32_t hccr;
1123         uint32_t r2hisr;
1124
1125         if (!(BXR2(isp, IspVirt2Off(isp, BIU_ISR) & BIU2100_ISR_RISC_INT))) {
1126                 *isrp = 0;
1127                 return (0);
1128         }
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) {
1132                 *isrp = 0;
1133                 return (0);
1134         }
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);
1143                 *semap = 1;
1144                 return (1);
1145         case ISPR2HST_RIO_16:
1146                 *isrp = r2hisr & 0xffff;
1147                 *mbox0p = ASYNC_RIO16_1;
1148                 *semap = 1;
1149                 return (1);
1150         case ISPR2HST_FPOST:
1151                 *isrp = r2hisr & 0xffff;
1152                 *mbox0p = ASYNC_CMD_CMPLT;
1153                 *semap = 1;
1154                 return (1);
1155         case ISPR2HST_FPOST_CTIO:
1156                 *isrp = r2hisr & 0xffff;
1157                 *mbox0p = ASYNC_CTIO_DONE;
1158                 *semap = 1;
1159                 return (1);
1160         case ISPR2HST_RSPQ_UPDATE:
1161                 *isrp = r2hisr & 0xffff;
1162                 *mbox0p = 0;
1163                 *semap = 0;
1164                 return (1);
1165         default:
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);
1171                 } else {
1172                         isp_prt(isp, ISP_LOGERR, "unknown interrupt 0x%x\n", r2hisr);
1173                 }
1174                 return (0);
1175         }
1176 }
1177
1178 static int
1179 isp_pci_rd_isr_2400(ispsoftc_t *isp, uint32_t *isrp, uint16_t *semap, uint16_t *mbox0p)
1180 {
1181         uint32_t r2hisr;
1182
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) {
1186                 *isrp = 0;
1187                 return (0);
1188         }
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);
1197                 *semap = 1;
1198                 return (1);
1199         case ISP2400R2HST_RSPQ_UPDATE:
1200         case ISP2400R2HST_ATIO_RSPQ_UPDATE:
1201         case ISP2400R2HST_ATIO_RQST_UPDATE:
1202                 *isrp = r2hisr & 0xffff;
1203                 *mbox0p = 0;
1204                 *semap = 0;
1205                 return (1);
1206         default:
1207                 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
1208                 isp_prt(isp, ISP_LOGERR, "unknown interrupt 0x%x\n", r2hisr);
1209                 return (0);
1210         }
1211 }
1212
1213 static uint32_t
1214 isp_pci_rd_reg(ispsoftc_t *isp, int regoff)
1215 {
1216         uint16_t rv;
1217         int oldconf = 0;
1218
1219         if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
1220                 /*
1221                  * We will assume that someone has paused the RISC processor.
1222                  */
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);
1226         }
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);
1231         }
1232         return (rv);
1233 }
1234
1235 static void
1236 isp_pci_wr_reg(ispsoftc_t *isp, int regoff, uint32_t val)
1237 {
1238         int oldconf = 0;
1239
1240         if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
1241                 /*
1242                  * We will assume that someone has paused the RISC processor.
1243                  */
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);
1248         }
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);
1254         }
1255
1256 }
1257
1258 static uint32_t
1259 isp_pci_rd_reg_1080(ispsoftc_t *isp, int regoff)
1260 {
1261         uint32_t rv, oc = 0;
1262
1263         if ((regoff & _BLK_REG_MASK) == SXP_BLOCK ||
1264             (regoff & _BLK_REG_MASK) == (SXP_BLOCK|SXP_BANK1_SELECT)) {
1265                 uint32_t tc;
1266                 /*
1267                  * We will assume that someone has paused the RISC processor.
1268                  */
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;
1273                 else
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);
1282         }
1283         rv = BXR2(isp, IspVirt2Off(isp, regoff));
1284         if (oc) {
1285                 BXW2(isp, IspVirt2Off(isp, BIU_CONF1), oc);
1286                 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1287         }
1288         return (rv);
1289 }
1290
1291 static void
1292 isp_pci_wr_reg_1080(ispsoftc_t *isp, int regoff, uint32_t val)
1293 {
1294         int oc = 0;
1295
1296         if ((regoff & _BLK_REG_MASK) == SXP_BLOCK ||
1297             (regoff & _BLK_REG_MASK) == (SXP_BLOCK|SXP_BANK1_SELECT)) {
1298                 uint32_t tc;
1299                 /*
1300                  * We will assume that someone has paused the RISC processor.
1301                  */
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;
1306                 else
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);
1315         }
1316         BXW2(isp, IspVirt2Off(isp, regoff), val);
1317         MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, regoff), 2, -1);
1318         if (oc) {
1319                 BXW2(isp, IspVirt2Off(isp, BIU_CONF1), oc);
1320                 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1321         }
1322 }
1323
1324 static uint32_t
1325 isp_pci_rd_reg_2400(ispsoftc_t *isp, int regoff)
1326 {
1327         uint32_t rv;
1328         int block = regoff & _BLK_REG_MASK;
1329
1330         switch (block) {
1331         case BIU_BLOCK:
1332                 break;
1333         case MBOX_BLOCK:
1334                 return (BXR2(isp, IspVirt2Off(isp, regoff)));
1335         case SXP_BLOCK:
1336                 isp_prt(isp, ISP_LOGWARN, "SXP_BLOCK read at 0x%x", regoff);
1337                 return (0xffffffff);
1338         case RISC_BLOCK:
1339                 isp_prt(isp, ISP_LOGWARN, "RISC_BLOCK read at 0x%x", regoff);
1340                 return (0xffffffff);
1341         case DMA_BLOCK:
1342                 isp_prt(isp, ISP_LOGWARN, "DMA_BLOCK read at 0x%x", regoff);
1343                 return (0xffffffff);
1344         default:
1345                 isp_prt(isp, ISP_LOGWARN, "unknown block read at 0x%x", regoff);
1346                 return (0xffffffff);
1347         }
1348
1349
1350         switch (regoff) {
1351         case BIU2400_FLASH_ADDR:
1352         case BIU2400_FLASH_DATA:
1353         case BIU2400_ICR:
1354         case BIU2400_ISR:
1355         case BIU2400_CSR:
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:
1364         case BIU2400_HCCR:
1365         case BIU2400_GPIOD:
1366         case BIU2400_GPIOE:
1367         case BIU2400_HSEMA:
1368                 rv = BXR4(isp, IspVirt2Off(isp, regoff));
1369                 break;
1370         case BIU2400_R2HSTSLO:
1371                 rv = BXR4(isp, IspVirt2Off(isp, regoff));
1372                 break;
1373         case BIU2400_R2HSTSHI:
1374                 rv = BXR4(isp, IspVirt2Off(isp, regoff)) >> 16;
1375                 break;
1376         default:
1377                 isp_prt(isp, ISP_LOGERR,
1378                     "isp_pci_rd_reg_2400: unknown offset %x", regoff);
1379                 rv = 0xffffffff;
1380                 break;
1381         }
1382         return (rv);
1383 }
1384
1385 static void
1386 isp_pci_wr_reg_2400(ispsoftc_t *isp, int regoff, uint32_t val)
1387 {
1388         int block = regoff & _BLK_REG_MASK;
1389
1390         switch (block) {
1391         case BIU_BLOCK:
1392                 break;
1393         case MBOX_BLOCK:
1394                 BXW2(isp, IspVirt2Off(isp, regoff), val);
1395                 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, regoff), 2, -1);
1396                 return;
1397         case SXP_BLOCK:
1398                 isp_prt(isp, ISP_LOGWARN, "SXP_BLOCK write at 0x%x", regoff);
1399                 return;
1400         case RISC_BLOCK:
1401                 isp_prt(isp, ISP_LOGWARN, "RISC_BLOCK write at 0x%x", regoff);
1402                 return;
1403         case DMA_BLOCK:
1404                 isp_prt(isp, ISP_LOGWARN, "DMA_BLOCK write at 0x%x", regoff);
1405                 return;
1406         default:
1407                 isp_prt(isp, ISP_LOGWARN, "unknown block write at 0x%x",
1408                     regoff);
1409                 break;
1410         }
1411
1412         switch (regoff) {
1413         case BIU2400_FLASH_ADDR:
1414         case BIU2400_FLASH_DATA:
1415         case BIU2400_ICR:
1416         case BIU2400_ISR:
1417         case BIU2400_CSR:
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:
1426         case BIU2400_HCCR:
1427         case BIU2400_GPIOD:
1428         case BIU2400_GPIOE:
1429         case BIU2400_HSEMA:
1430                 BXW4(isp, IspVirt2Off(isp, regoff), val);
1431                 MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, regoff), 4, -1);
1432                 break;
1433         default:
1434                 isp_prt(isp, ISP_LOGERR,
1435                     "isp_pci_wr_reg_2400: bad offset 0x%x", regoff);
1436                 break;
1437         }
1438 }
1439
1440
1441 struct imush {
1442         ispsoftc_t *isp;
1443         caddr_t vbase;
1444         int chan;
1445         int error;
1446 };
1447
1448 static void imc(void *, bus_dma_segment_t *, int, int);
1449 static void imc1(void *, bus_dma_segment_t *, int, int);
1450
1451 static void
1452 imc(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1453 {
1454         struct imush *imushp = (struct imush *) arg;
1455
1456         if (error) {
1457                 imushp->error = error;
1458                 return;
1459         }
1460         if (nseg != 1) {
1461                 imushp->error = EINVAL;
1462                 return;
1463         }
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;
1470
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;
1477         }
1478 #endif
1479 }
1480
1481 static void
1482 imc1(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1483 {
1484         struct imush *imushp = (struct imush *) arg;
1485         if (error) {
1486                 imushp->error = error;
1487                 return;
1488         }
1489         if (nseg != 1) {
1490                 imushp->error = EINVAL;
1491                 return;
1492         }
1493         FCPARAM(imushp->isp, imushp->chan)->isp_scdma = segs->ds_addr;
1494         FCPARAM(imushp->isp, imushp->chan)->isp_scratch = imushp->vbase;
1495 }
1496
1497 static int
1498 isp_pci_mbxdma(ispsoftc_t *isp)
1499 {
1500         caddr_t base;
1501         uint32_t len;
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 */
1506         struct imush im;
1507
1508         /*
1509          * Already been here? If so, leave...
1510          */
1511         if (isp->isp_rquest) {
1512                 return (0);
1513         }
1514         ISP_UNLOCK(isp);
1515
1516         if (isp->isp_maxcmds == 0) {
1517                 isp_prt(isp, ISP_LOGERR, "maxcmds not set");
1518                 ISP_LOCK(isp);
1519                 return (1);
1520         }
1521
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);
1526                 } else {
1527                         slim = (bus_size_t) (1UL << 31);
1528                 }
1529                 llim = BUS_SPACE_MAXADDR;
1530         } else {
1531                 llim = BUS_SPACE_MAXADDR_32BIT;
1532                 slim = (1UL << 24);
1533         }
1534
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");
1539                 ISP_LOCK(isp);
1540                 return (1);
1541         }
1542
1543         /*
1544          * XXX: We don't really support 64 bit target mode for parallel scsi yet
1545          */
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");
1550                 ISP_LOCK(isp);
1551                 return (1);
1552         }
1553 #endif
1554
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);
1557                 ISP_LOCK(isp);
1558                 isp_prt(isp, ISP_LOGERR, "could not create master dma tag");
1559                 return (1);
1560         }
1561
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);
1566                 ISP_LOCK(isp);
1567                 isp_prt(isp, ISP_LOGERR, "cannot alloc xflist array");
1568                 return (1);
1569         }
1570         for (len = 0; len < isp->isp_maxcmds - 1; len++) {
1571                 isp->isp_xflist[len].cmd = &isp->isp_xflist[len+1];
1572         }
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);
1580                 ISP_LOCK(isp);
1581                 isp_prt(isp, ISP_LOGERR, "cannot alloc tgtlist array");
1582                 return (1);
1583         }
1584         for (len = 0; len < isp->isp_maxcmds - 1; len++) {
1585                 isp->isp_tgtlist[len].cmd = &isp->isp_tgtlist[len+1];
1586         }
1587         isp->isp_tgtfree = isp->isp_tgtlist;
1588 #endif
1589
1590         /*
1591          * Allocate and map the request and result queues (and ATIO queue
1592          * if we're a 2400 supporting target mode).
1593          */
1594         len = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
1595         len += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
1596 #ifdef  ISP_TARGET_MODE
1597         if (IS_24XX(isp)) {
1598                 len += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
1599         }
1600 #endif
1601
1602         ns = (len / PAGE_SIZE) + 1;
1603
1604         /*
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.
1607          */
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);
1614 #endif
1615                 ISP_LOCK(isp);
1616                 return (1);
1617         }
1618
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);
1626 #endif
1627                 ISP_LOCK(isp);
1628                 return (1);
1629         }
1630
1631         im.isp = isp;
1632         im.chan = 0;
1633         im.vbase = base;
1634         im.error = 0;
1635
1636         bus_dmamap_load(isp->isp_osinfo.cdmat, isp->isp_osinfo.cdmap, base, len, imc, &im, 0);
1637         if (im.error) {
1638                 isp_prt(isp, ISP_LOGERR, "error %d loading dma map for control areas", im.error);
1639                 goto bad;
1640         }
1641
1642         if (IS_FC(isp)) {
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)) {
1646                                 goto bad;
1647                         }
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);
1650                                 goto bad;
1651                         }
1652                         im.isp = isp;
1653                         im.chan = cmap;
1654                         im.vbase = base;
1655                         im.error = 0;
1656                         bus_dmamap_load(fc->tdmat, fc->tdmap, base, ISP_FC_SCRLEN, imc1, &im, 0);
1657                         if (im.error) {
1658                                 bus_dmamem_free(fc->tdmat, base, fc->tdmap);
1659                                 bus_dma_tag_destroy(fc->tdmat);
1660                                 goto bad;
1661                         }
1662                 }
1663         }
1664
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);
1668                 if (error) {
1669                         isp_prt(isp, ISP_LOGERR, "error %d creating per-cmd DMA maps", error);
1670                         while (--i >= 0) {
1671                                 bus_dmamap_destroy(isp->isp_osinfo.dmat, isp->isp_osinfo.pcmd_pool[i].dmap);
1672                         }
1673                         goto bad;
1674                 }
1675                 callout_init_mtx(&pcmd->wdog, &isp->isp_osinfo.lock, 0);
1676                 if (i == isp->isp_maxcmds-1) {
1677                         pcmd->next = NULL;
1678                 } else {
1679                         pcmd->next = &isp->isp_osinfo.pcmd_pool[i+1];
1680                 }
1681         }
1682         isp->isp_osinfo.pcmd_free = &isp->isp_osinfo.pcmd_pool[0];
1683         ISP_LOCK(isp);
1684         return (0);
1685
1686 bad:
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);
1691         }
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);
1697 #endif
1698         free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
1699         isp->isp_rquest = NULL;
1700         ISP_LOCK(isp);
1701         return (1);
1702 }
1703
1704 typedef struct {
1705         ispsoftc_t *isp;
1706         void *cmd_token;
1707         void *rq;       /* original request */
1708         int error;
1709         bus_size_t mapsize;
1710 } mush_t;
1711
1712 #define MUSHERR_NOQENTRIES      -2
1713
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);
1717
1718 static void
1719 tdma2_2(void *arg, bus_dma_segment_t *dm_segs, int nseg, bus_size_t mapsize, int error)
1720 {
1721         mush_t *mp;
1722         mp = (mush_t *)arg;
1723         mp->mapsize = mapsize;
1724         tdma2(arg, dm_segs, nseg, error);
1725 }
1726
1727 static void
1728 tdma2(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1729 {
1730         mush_t *mp;
1731         ispsoftc_t *isp;
1732         struct ccb_scsiio *csio;
1733         isp_ddir_t ddir;
1734         ispreq_t *rq;
1735
1736         mp = (mush_t *) arg;
1737         if (error) {
1738                 mp->error = error;
1739                 return;
1740         }
1741         csio = mp->cmd_token;
1742         isp = mp->isp;
1743         rq = mp->rq;
1744         if (nseg) {
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);
1748                                 mp->error = EFAULT;
1749                                 return;
1750                         }
1751                         if (rq->req_header.rqs_entry_type == RQSTYPE_CTIO2) {
1752                                 rq->req_header.rqs_entry_type = RQSTYPE_CTIO3;
1753                         }
1754                 } else {
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);
1757                                 mp->error = EFAULT;
1758                                 return;
1759                         }
1760                 }
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;
1767                 } else {
1768                         dm_segs = NULL;
1769                         nseg = 0;
1770                         ddir = ISP_NOXFR;
1771                 }
1772         } else {
1773                 dm_segs = NULL;
1774                 nseg = 0;
1775                 ddir = ISP_NOXFR;
1776         }
1777
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;
1780         }
1781 }
1782 #endif
1783
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);
1786
1787 static void
1788 dma2_2(void *arg, bus_dma_segment_t *dm_segs, int nseg, bus_size_t mapsize, int error)
1789 {
1790         mush_t *mp;
1791         mp = (mush_t *)arg;
1792         mp->mapsize = mapsize;
1793         dma2(arg, dm_segs, nseg, error);
1794 }
1795
1796 static void
1797 dma2(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1798 {
1799         mush_t *mp;
1800         ispsoftc_t *isp;
1801         struct ccb_scsiio *csio;
1802         isp_ddir_t ddir;
1803         ispreq_t *rq;
1804
1805         mp = (mush_t *) arg;
1806         if (error) {
1807                 mp->error = error;
1808                 return;
1809         }
1810         csio = mp->cmd_token;
1811         isp = mp->isp;
1812         rq = mp->rq;
1813         if (nseg) {
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);
1817                                 mp->error = EFAULT;
1818                                 return;
1819                         }
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;
1824                         }
1825                 } else {
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);
1828                                 mp->error = EFAULT;
1829                                 return;
1830                         }
1831                 }
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;
1838                 } else {
1839                         ddir = ISP_NOXFR;
1840                 }
1841         } else {
1842                 dm_segs = NULL;
1843                 nseg = 0;
1844                 ddir = ISP_NOXFR;
1845         }
1846
1847         if (isp_send_cmd(isp, rq, dm_segs, nseg, XS_XFRLEN(csio), ddir) != CMD_QUEUED) {
1848                 mp->error = MUSHERR_NOQENTRIES;
1849         }
1850 }
1851
1852 static int
1853 isp_pci_dmasetup(ispsoftc_t *isp, struct ccb_scsiio *csio, void *ff)
1854 {
1855         mush_t mush, *mp;
1856         void (*eptr)(void *, bus_dma_segment_t *, int, int);
1857         void (*eptr2)(void *, bus_dma_segment_t *, int, bus_size_t, int);
1858
1859         mp = &mush;
1860         mp->isp = isp;
1861         mp->cmd_token = csio;
1862         mp->rq = ff;
1863         mp->error = 0;
1864         mp->mapsize = 0;
1865
1866 #ifdef  ISP_TARGET_MODE
1867         if (csio->ccb_h.func_code == XPT_CONT_TARGET_IO) {
1868                 eptr = tdma2;
1869                 eptr2 = tdma2_2;
1870         } else
1871 #endif
1872         {
1873                 eptr = dma2;
1874                 eptr2 = dma2_2;
1875         }
1876
1877
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) {
1882                         int error;
1883                         error = bus_dmamap_load(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, csio->data_ptr, csio->dxfer_len, eptr, mp, 0);
1884 #if 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);
1886 #endif
1887
1888                         if (error == EINPROGRESS) {
1889                                 bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
1890                                 mp->error = EINVAL;
1891                                 isp_prt(isp, ISP_LOGERR, "deferred dma allocation not supported");
1892                         } else if (error && mp->error == 0) {
1893 #ifdef  DIAGNOSTIC
1894                                 isp_prt(isp, ISP_LOGERR, "error %d in dma mapping code", error);
1895 #endif
1896                                 mp->error = error;
1897                         }
1898                 } else {
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);
1904                 }
1905         } else {
1906                 struct bus_dma_segment *segs;
1907
1908                 if ((csio->ccb_h.flags & CAM_DATA_PHYS) != 0) {
1909                         isp_prt(isp, ISP_LOGERR, "Physical segment pointers unsupported");
1910                         mp->error = EINVAL;
1911                 } else if ((csio->ccb_h.flags & CAM_SG_LIST_PHYS) == 0) {
1912                         struct uio sguio;
1913                         int error;
1914
1915                         /*
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
1921                          * seems unlikely.
1922                          */
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;
1930
1931                         error = bus_dmamap_load_uio(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, &sguio, eptr2, mp, 0);
1932
1933                         if (error != 0 && mp->error == 0) {
1934                                 isp_prt(isp, ISP_LOGERR, "error %d in dma mapping code", error);
1935                                 mp->error = error;
1936                         }
1937                 } else {
1938                         /* Just use the segments provided */
1939                         segs = (struct bus_dma_segment *) csio->data_ptr;
1940                         (*eptr)(mp, segs, csio->sglist_cnt, 0);
1941                 }
1942         }
1943         if (mp->error) {
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);
1951                 } else {
1952                         XS_SETERR(csio, CAM_UNREC_HBA_ERROR);
1953                 }
1954                 return (retval);
1955         }
1956         return (CMD_QUEUED);
1957 }
1958
1959 static void
1960 isp_pci_reset0(ispsoftc_t *isp)
1961 {
1962         ISP_DISABLE_INTS(isp);
1963 }
1964
1965 static void
1966 isp_pci_reset1(ispsoftc_t *isp)
1967 {
1968         if (!IS_24XX(isp)) {
1969                 /* Make sure the BIOS is disabled */
1970                 isp_pci_wr_reg(isp, HCCR, PCI_HCCR_CMD_BIOS);
1971         }
1972         /* and enable interrupts */
1973         ISP_ENABLE_INTS(isp);
1974 }
1975
1976 static void
1977 isp_pci_dumpregs(ispsoftc_t *isp, const char *msg)
1978 {
1979         struct isp_pcisoftc *pcs = (struct isp_pcisoftc *)isp;
1980         if (msg)
1981                 printf("%s: %s\n", device_get_nameunit(isp->isp_dev), msg);
1982         else
1983                 printf("%s:\n", device_get_nameunit(isp->isp_dev));
1984         if (IS_SCSI(isp))
1985                 printf("    biu_conf1=%x", ISP_READ(isp, BIU_CONF1));
1986         else
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));
1991
1992
1993         if (IS_SCSI(isp)) {
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);
2006         }
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));
2013 }