]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/isp/isp_pci.c
This commit was generated by cvs2svn to compensate for changes in r165254,
[FreeBSD/FreeBSD.git] / sys / dev / isp / isp_pci.c
1 /*-
2  *
3  * Copyright (c) 1997-2006 by Matthew Jacob
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice immediately at the beginning of the file, without modification,
11  *    this list of conditions, and the following disclaimer.
12  * 2. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  */
28 /*
29  * PCI specific probe and attach routines for Qlogic ISP SCSI adapters.
30  * FreeBSD Version.
31  */
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #if __FreeBSD_version >= 700000  
40 #include <sys/linker.h>
41 #include <sys/firmware.h>
42 #endif
43 #include <sys/bus.h>
44 #if __FreeBSD_version < 500000  
45 #include <pci/pcireg.h>
46 #include <pci/pcivar.h>
47 #include <machine/bus_memio.h>
48 #include <machine/bus_pio.h>
49 #else
50 #include <sys/stdint.h>
51 #include <dev/pci/pcireg.h>
52 #include <dev/pci/pcivar.h>
53 #endif
54 #include <machine/bus.h>
55 #include <machine/resource.h>
56 #include <sys/rman.h>
57 #include <sys/malloc.h>
58
59 #include <dev/isp/isp_freebsd.h>
60
61 #if __FreeBSD_version < 500000  
62 #define BUS_PROBE_DEFAULT       0
63 #endif
64
65 static uint32_t isp_pci_rd_reg(ispsoftc_t *, int);
66 static void isp_pci_wr_reg(ispsoftc_t *, int, uint32_t);
67 static uint32_t isp_pci_rd_reg_1080(ispsoftc_t *, int);
68 static void isp_pci_wr_reg_1080(ispsoftc_t *, int, uint32_t);
69 static uint32_t isp_pci_rd_reg_2400(ispsoftc_t *, int);
70 static void isp_pci_wr_reg_2400(ispsoftc_t *, int, uint32_t);
71 static int
72 isp_pci_rd_isr(ispsoftc_t *, uint32_t *, uint16_t *, uint16_t *);
73 static int
74 isp_pci_rd_isr_2300(ispsoftc_t *, uint32_t *, uint16_t *, uint16_t *);
75 static int
76 isp_pci_rd_isr_2400(ispsoftc_t *, uint32_t *, uint16_t *, uint16_t *);
77 static int isp_pci_mbxdma(ispsoftc_t *);
78 static int
79 isp_pci_dmasetup(ispsoftc_t *, XS_T *, ispreq_t *, uint32_t *, uint32_t);
80 static void
81 isp_pci_dmateardown(ispsoftc_t *, XS_T *, uint32_t);
82
83
84 static void isp_pci_reset1(ispsoftc_t *);
85 static void isp_pci_dumpregs(ispsoftc_t *, const char *);
86
87 static struct ispmdvec mdvec = {
88         isp_pci_rd_isr,
89         isp_pci_rd_reg,
90         isp_pci_wr_reg,
91         isp_pci_mbxdma,
92         isp_pci_dmasetup,
93         isp_pci_dmateardown,
94         NULL,
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_1080 = {
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_pci_dmateardown,
108         NULL,
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_12160 = {
116         isp_pci_rd_isr,
117         isp_pci_rd_reg_1080,
118         isp_pci_wr_reg_1080,
119         isp_pci_mbxdma,
120         isp_pci_dmasetup,
121         isp_pci_dmateardown,
122         NULL,
123         isp_pci_reset1,
124         isp_pci_dumpregs,
125         NULL,
126         BIU_BURST_ENABLE|BIU_PCI_CONF1_FIFO_64
127 };
128
129 static struct ispmdvec mdvec_2100 = {
130         isp_pci_rd_isr,
131         isp_pci_rd_reg,
132         isp_pci_wr_reg,
133         isp_pci_mbxdma,
134         isp_pci_dmasetup,
135         isp_pci_dmateardown,
136         NULL,
137         isp_pci_reset1,
138         isp_pci_dumpregs
139 };
140
141 static struct ispmdvec mdvec_2200 = {
142         isp_pci_rd_isr,
143         isp_pci_rd_reg,
144         isp_pci_wr_reg,
145         isp_pci_mbxdma,
146         isp_pci_dmasetup,
147         isp_pci_dmateardown,
148         NULL,
149         isp_pci_reset1,
150         isp_pci_dumpregs
151 };
152
153 static struct ispmdvec mdvec_2300 = {
154         isp_pci_rd_isr_2300,
155         isp_pci_rd_reg,
156         isp_pci_wr_reg,
157         isp_pci_mbxdma,
158         isp_pci_dmasetup,
159         isp_pci_dmateardown,
160         NULL,
161         isp_pci_reset1,
162         isp_pci_dumpregs
163 };
164
165 static struct ispmdvec mdvec_2400 = {
166         isp_pci_rd_isr_2400,
167         isp_pci_rd_reg_2400,
168         isp_pci_wr_reg_2400,
169         isp_pci_mbxdma,
170         isp_pci_dmasetup,
171         isp_pci_dmateardown,
172         NULL,
173         isp_pci_reset1,
174         NULL
175 };
176
177 #ifndef PCIM_CMD_INVEN
178 #define PCIM_CMD_INVEN                  0x10
179 #endif
180 #ifndef PCIM_CMD_BUSMASTEREN
181 #define PCIM_CMD_BUSMASTEREN            0x0004
182 #endif
183 #ifndef PCIM_CMD_PERRESPEN
184 #define PCIM_CMD_PERRESPEN              0x0040
185 #endif
186 #ifndef PCIM_CMD_SEREN
187 #define PCIM_CMD_SEREN                  0x0100
188 #endif
189 #ifndef PCIM_CMD_INTX_DISABLE
190 #define PCIM_CMD_INTX_DISABLE           0x0400
191 #endif
192
193 #ifndef PCIR_COMMAND
194 #define PCIR_COMMAND                    0x04
195 #endif
196
197 #ifndef PCIR_CACHELNSZ
198 #define PCIR_CACHELNSZ                  0x0c
199 #endif
200
201 #ifndef PCIR_LATTIMER
202 #define PCIR_LATTIMER                   0x0d
203 #endif
204
205 #ifndef PCIR_ROMADDR
206 #define PCIR_ROMADDR                    0x30
207 #endif
208
209 #ifndef PCI_VENDOR_QLOGIC
210 #define PCI_VENDOR_QLOGIC               0x1077
211 #endif
212
213 #ifndef PCI_PRODUCT_QLOGIC_ISP1020
214 #define PCI_PRODUCT_QLOGIC_ISP1020      0x1020
215 #endif
216
217 #ifndef PCI_PRODUCT_QLOGIC_ISP1080
218 #define PCI_PRODUCT_QLOGIC_ISP1080      0x1080
219 #endif
220
221 #ifndef PCI_PRODUCT_QLOGIC_ISP10160
222 #define PCI_PRODUCT_QLOGIC_ISP10160     0x1016
223 #endif
224
225 #ifndef PCI_PRODUCT_QLOGIC_ISP12160
226 #define PCI_PRODUCT_QLOGIC_ISP12160     0x1216
227 #endif
228
229 #ifndef PCI_PRODUCT_QLOGIC_ISP1240
230 #define PCI_PRODUCT_QLOGIC_ISP1240      0x1240
231 #endif
232
233 #ifndef PCI_PRODUCT_QLOGIC_ISP1280
234 #define PCI_PRODUCT_QLOGIC_ISP1280      0x1280
235 #endif
236
237 #ifndef PCI_PRODUCT_QLOGIC_ISP2100
238 #define PCI_PRODUCT_QLOGIC_ISP2100      0x2100
239 #endif
240
241 #ifndef PCI_PRODUCT_QLOGIC_ISP2200
242 #define PCI_PRODUCT_QLOGIC_ISP2200      0x2200
243 #endif
244
245 #ifndef PCI_PRODUCT_QLOGIC_ISP2300
246 #define PCI_PRODUCT_QLOGIC_ISP2300      0x2300
247 #endif
248
249 #ifndef PCI_PRODUCT_QLOGIC_ISP2312
250 #define PCI_PRODUCT_QLOGIC_ISP2312      0x2312
251 #endif
252
253 #ifndef PCI_PRODUCT_QLOGIC_ISP2322
254 #define PCI_PRODUCT_QLOGIC_ISP2322      0x2322
255 #endif
256
257 #ifndef PCI_PRODUCT_QLOGIC_ISP2422
258 #define PCI_PRODUCT_QLOGIC_ISP2422      0x2422
259 #endif
260
261 #ifndef PCI_PRODUCT_QLOGIC_ISP6312
262 #define PCI_PRODUCT_QLOGIC_ISP6312      0x6312
263 #endif
264
265 #ifndef PCI_PRODUCT_QLOGIC_ISP6322
266 #define PCI_PRODUCT_QLOGIC_ISP6322      0x6322
267 #endif
268
269
270 #define PCI_QLOGIC_ISP1020      \
271         ((PCI_PRODUCT_QLOGIC_ISP1020 << 16) | PCI_VENDOR_QLOGIC)
272
273 #define PCI_QLOGIC_ISP1080      \
274         ((PCI_PRODUCT_QLOGIC_ISP1080 << 16) | PCI_VENDOR_QLOGIC)
275
276 #define PCI_QLOGIC_ISP10160     \
277         ((PCI_PRODUCT_QLOGIC_ISP10160 << 16) | PCI_VENDOR_QLOGIC)
278
279 #define PCI_QLOGIC_ISP12160     \
280         ((PCI_PRODUCT_QLOGIC_ISP12160 << 16) | PCI_VENDOR_QLOGIC)
281
282 #define PCI_QLOGIC_ISP1240      \
283         ((PCI_PRODUCT_QLOGIC_ISP1240 << 16) | PCI_VENDOR_QLOGIC)
284
285 #define PCI_QLOGIC_ISP1280      \
286         ((PCI_PRODUCT_QLOGIC_ISP1280 << 16) | PCI_VENDOR_QLOGIC)
287
288 #define PCI_QLOGIC_ISP2100      \
289         ((PCI_PRODUCT_QLOGIC_ISP2100 << 16) | PCI_VENDOR_QLOGIC)
290
291 #define PCI_QLOGIC_ISP2200      \
292         ((PCI_PRODUCT_QLOGIC_ISP2200 << 16) | PCI_VENDOR_QLOGIC)
293
294 #define PCI_QLOGIC_ISP2300      \
295         ((PCI_PRODUCT_QLOGIC_ISP2300 << 16) | PCI_VENDOR_QLOGIC)
296
297 #define PCI_QLOGIC_ISP2312      \
298         ((PCI_PRODUCT_QLOGIC_ISP2312 << 16) | PCI_VENDOR_QLOGIC)
299
300 #define PCI_QLOGIC_ISP2322      \
301         ((PCI_PRODUCT_QLOGIC_ISP2322 << 16) | PCI_VENDOR_QLOGIC)
302
303 #define PCI_QLOGIC_ISP2422      \
304         ((PCI_PRODUCT_QLOGIC_ISP2422 << 16) | PCI_VENDOR_QLOGIC)
305
306 #define PCI_QLOGIC_ISP6312      \
307         ((PCI_PRODUCT_QLOGIC_ISP6312 << 16) | PCI_VENDOR_QLOGIC)
308
309 #define PCI_QLOGIC_ISP6322      \
310         ((PCI_PRODUCT_QLOGIC_ISP6322 << 16) | PCI_VENDOR_QLOGIC)
311
312 /*
313  * Odd case for some AMI raid cards... We need to *not* attach to this.
314  */
315 #define AMI_RAID_SUBVENDOR_ID   0x101e
316
317 #define IO_MAP_REG      0x10
318 #define MEM_MAP_REG     0x14
319
320 #define PCI_DFLT_LTNCY  0x40
321 #define PCI_DFLT_LNSZ   0x10
322
323 static int isp_pci_probe (device_t);
324 static int isp_pci_attach (device_t);
325 static int isp_pci_detach (device_t);
326
327
328 struct isp_pcisoftc {
329         ispsoftc_t                      pci_isp;
330         device_t                        pci_dev;
331         struct resource *               pci_reg;
332         bus_space_tag_t                 pci_st;
333         bus_space_handle_t              pci_sh;
334         void *                          ih;
335         int16_t                         pci_poff[_NREG_BLKS];
336         bus_dma_tag_t                   dmat;
337         bus_dmamap_t                    *dmaps;
338 };
339
340
341 static device_method_t isp_pci_methods[] = {
342         /* Device interface */
343         DEVMETHOD(device_probe,         isp_pci_probe),
344         DEVMETHOD(device_attach,        isp_pci_attach),
345         DEVMETHOD(device_detach,        isp_pci_detach),
346         { 0, 0 }
347 };
348 static void isp_pci_intr(void *);
349
350 static driver_t isp_pci_driver = {
351         "isp", isp_pci_methods, sizeof (struct isp_pcisoftc)
352 };
353 static devclass_t isp_devclass;
354 DRIVER_MODULE(isp, pci, isp_pci_driver, isp_devclass, 0, 0);
355 #if __FreeBSD_version < 700000  
356 extern ispfwfunc *isp_get_firmware_p;
357 #endif
358
359 static int
360 isp_pci_probe(device_t dev)
361 {
362         switch ((pci_get_device(dev) << 16) | (pci_get_vendor(dev))) {
363         case PCI_QLOGIC_ISP1020:
364                 device_set_desc(dev, "Qlogic ISP 1020/1040 PCI SCSI Adapter");
365                 break;
366         case PCI_QLOGIC_ISP1080:
367                 device_set_desc(dev, "Qlogic ISP 1080 PCI SCSI Adapter");
368                 break;
369         case PCI_QLOGIC_ISP1240:
370                 device_set_desc(dev, "Qlogic ISP 1240 PCI SCSI Adapter");
371                 break;
372         case PCI_QLOGIC_ISP1280:
373                 device_set_desc(dev, "Qlogic ISP 1280 PCI SCSI Adapter");
374                 break;
375         case PCI_QLOGIC_ISP10160:
376                 device_set_desc(dev, "Qlogic ISP 10160 PCI SCSI Adapter");
377                 break;
378         case PCI_QLOGIC_ISP12160:
379                 if (pci_get_subvendor(dev) == AMI_RAID_SUBVENDOR_ID) {
380                         return (ENXIO);
381                 }
382                 device_set_desc(dev, "Qlogic ISP 12160 PCI SCSI Adapter");
383                 break;
384         case PCI_QLOGIC_ISP2100:
385                 device_set_desc(dev, "Qlogic ISP 2100 PCI FC-AL Adapter");
386                 break;
387         case PCI_QLOGIC_ISP2200:
388                 device_set_desc(dev, "Qlogic ISP 2200 PCI FC-AL Adapter");
389                 break;
390         case PCI_QLOGIC_ISP2300:
391                 device_set_desc(dev, "Qlogic ISP 2300 PCI FC-AL Adapter");
392                 break;
393         case PCI_QLOGIC_ISP2312:
394                 device_set_desc(dev, "Qlogic ISP 2312 PCI FC-AL Adapter");
395                 break;
396         case PCI_QLOGIC_ISP2322:
397                 device_set_desc(dev, "Qlogic ISP 2322 PCI FC-AL Adapter");
398                 break;
399         case PCI_QLOGIC_ISP2422:
400                 device_set_desc(dev, "Qlogic ISP 2422 PCI FC-AL Adapter");
401                 break;
402         case PCI_QLOGIC_ISP6312:
403                 device_set_desc(dev, "Qlogic ISP 6312 PCI FC-AL Adapter");
404                 break;
405         case PCI_QLOGIC_ISP6322:
406                 device_set_desc(dev, "Qlogic ISP 6322 PCI FC-AL Adapter");
407                 break;
408         default:
409                 return (ENXIO);
410         }
411         if (isp_announced == 0 && bootverbose) {
412                 printf("Qlogic ISP Driver, FreeBSD Version %d.%d, "
413                     "Core Version %d.%d\n",
414                     ISP_PLATFORM_VERSION_MAJOR, ISP_PLATFORM_VERSION_MINOR,
415                     ISP_CORE_VERSION_MAJOR, ISP_CORE_VERSION_MINOR);
416                 isp_announced++;
417         }
418         /*
419          * XXXX: Here is where we might load the f/w module
420          * XXXX: (or increase a reference count to it).
421          */
422         return (BUS_PROBE_DEFAULT);
423 }
424
425 #if __FreeBSD_version < 500000  
426 static void
427 isp_get_options(device_t dev, ispsoftc_t *isp)
428 {
429         uint64_t wwn;
430         int bitmap, unit;
431
432         callout_handle_init(&isp->isp_osinfo.ldt);
433         callout_handle_init(&isp->isp_osinfo.gdt);
434
435         unit = device_get_unit(dev);
436         if (getenv_int("isp_disable", &bitmap)) {
437                 if (bitmap & (1 << unit)) {
438                         isp->isp_osinfo.disabled = 1;
439                         return;
440                 }
441         }
442
443         if (getenv_int("isp_no_fwload", &bitmap)) {
444                 if (bitmap & (1 << unit))
445                         isp->isp_confopts |= ISP_CFG_NORELOAD;
446         }
447         if (getenv_int("isp_fwload", &bitmap)) {
448                 if (bitmap & (1 << unit))
449                         isp->isp_confopts &= ~ISP_CFG_NORELOAD;
450         }
451         if (getenv_int("isp_no_nvram", &bitmap)) {
452                 if (bitmap & (1 << unit))
453                         isp->isp_confopts |= ISP_CFG_NONVRAM;
454         }
455         if (getenv_int("isp_nvram", &bitmap)) {
456                 if (bitmap & (1 << unit))
457                         isp->isp_confopts &= ~ISP_CFG_NONVRAM;
458         }
459         if (getenv_int("isp_fcduplex", &bitmap)) {
460                 if (bitmap & (1 << unit))
461                         isp->isp_confopts |= ISP_CFG_FULL_DUPLEX;
462         }
463         if (getenv_int("isp_no_fcduplex", &bitmap)) {
464                 if (bitmap & (1 << unit))
465                         isp->isp_confopts &= ~ISP_CFG_FULL_DUPLEX;
466         }
467         if (getenv_int("isp_nport", &bitmap)) {
468                 if (bitmap & (1 << unit))
469                         isp->isp_confopts |= ISP_CFG_NPORT;
470         }
471
472         /*
473          * Because the resource_*_value functions can neither return
474          * 64 bit integer values, nor can they be directly coerced
475          * to interpret the right hand side of the assignment as
476          * you want them to interpret it, we have to force WWN
477          * hint replacement to specify WWN strings with a leading
478          * 'w' (e..g w50000000aaaa0001). Sigh.
479          */
480         if (getenv_quad("isp_portwwn", &wwn)) {
481                 isp->isp_osinfo.default_port_wwn = wwn;
482                 isp->isp_confopts |= ISP_CFG_OWNWWPN;
483         }
484         if (isp->isp_osinfo.default_port_wwn == 0) {
485                 isp->isp_osinfo.default_port_wwn = 0x400000007F000009ull;
486         }
487
488         if (getenv_quad("isp_nodewwn", &wwn)) {
489                 isp->isp_osinfo.default_node_wwn = wwn;
490                 isp->isp_confopts |= ISP_CFG_OWNWWNN;
491         }
492         if (isp->isp_osinfo.default_node_wwn == 0) {
493                 isp->isp_osinfo.default_node_wwn = 0x400000007F000009ull;
494         }
495
496         bitmap = 0;
497         (void) getenv_int("isp_debug", &bitmap);
498         if (bitmap) {
499                 isp->isp_dblev = bitmap;
500         } else {
501                 isp->isp_dblev = ISP_LOGWARN|ISP_LOGERR;
502         }
503         if (bootverbose) {
504                 isp->isp_dblev |= ISP_LOGCONFIG|ISP_LOGINFO;
505         }
506
507         bitmap = 0;
508         (void) getenv_int("isp_fabric_hysteresis", &bitmap);
509         if (bitmap >= 0 && bitmap < 256) {
510                 isp->isp_osinfo.hysteresis = bitmap;
511         } else {
512                 isp->isp_osinfo.hysteresis = isp_fabric_hysteresis;
513         }
514
515         bitmap = 0;
516         (void) getenv_int("isp_loop_down_limit", &bitmap);
517         if (bitmap >= 0 && bitmap < 0xffff) {
518                 isp->isp_osinfo.loop_down_limit = bitmap;
519         } else {
520                 isp->isp_osinfo.loop_down_limit = isp_loop_down_limit;
521         }
522
523         bitmap = 0;
524         (void) getenv_int("isp_gone_device_time", &bitmap);
525         if (bitmap >= 0 && bitmap < 0xffff) {
526                 isp->isp_osinfo.gone_device_time = bitmap;
527         } else {
528                 isp->isp_osinfo.gone_device_time = isp_gone_device_time;
529         }
530
531
532 #ifdef  ISP_FW_CRASH_DUMP
533         bitmap = 0;
534         if (getenv_int("isp_fw_dump_enable", &bitmap)) {
535                 if (bitmap & (1 << unit) {
536                         size_t amt = 0;
537                         if (IS_2200(isp)) {
538                                 amt = QLA2200_RISC_IMAGE_DUMP_SIZE;
539                         } else if (IS_23XX(isp)) {
540                                 amt = QLA2300_RISC_IMAGE_DUMP_SIZE;
541                         }
542                         if (amt) {
543                                 FCPARAM(isp)->isp_dump_data =
544                                     malloc(amt, M_DEVBUF, M_WAITOK);
545                                 memset(FCPARAM(isp)->isp_dump_data, 0, amt);
546                         } else {
547                                 device_printf(dev,
548                                     "f/w crash dumps not supported for card\n");
549                         }
550                 }
551         }
552 #endif
553         bitmap = 0;
554         if (getenv_int("role", &bitmap)) {
555                 isp->isp_role = bitmap;
556         } else {
557                 isp->isp_role = ISP_DEFAULT_ROLES;
558         }
559 }
560
561 static void
562 isp_get_pci_options(device_t dev, int *m1, int *m2)
563 {
564         int bitmap;
565         int unit = device_get_unit(dev);
566
567         *m1 = PCIM_CMD_MEMEN;
568         *m2 = PCIM_CMD_PORTEN;
569         if (getenv_int("isp_mem_map", &bitmap)) {
570                 if (bitmap & (1 << unit)) {
571                         *m1 = PCIM_CMD_MEMEN;
572                         *m2 = PCIM_CMD_PORTEN;
573                 }
574         }
575         bitmap = 0;
576         if (getenv_int("isp_io_map", &bitmap)) {
577                 if (bitmap & (1 << unit)) {
578                         *m1 = PCIM_CMD_PORTEN;
579                         *m2 = PCIM_CMD_MEMEN;
580                 }
581         }
582 }
583 #else
584 static void
585 isp_get_options(device_t dev, ispsoftc_t *isp)
586 {
587         int tval;
588         const char *sptr;
589
590         callout_handle_init(&isp->isp_osinfo.ldt);
591         callout_handle_init(&isp->isp_osinfo.gdt);
592
593         /*
594          * Figure out if we're supposed to skip this one.
595          */
596
597         tval = 0;
598         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
599             "disable", &tval) == 0 && tval) {
600                 device_printf(dev, "disabled at user request\n");
601                 isp->isp_osinfo.disabled = 1;
602                 return;
603         }
604         
605         tval = -1;
606         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
607             "role", &tval) == 0 && tval != -1) {
608                 tval &= (ISP_ROLE_INITIATOR|ISP_ROLE_TARGET);
609                 isp->isp_role = tval;
610                 device_printf(dev, "setting role to 0x%x\n", isp->isp_role);
611         } else {
612 #ifdef  ISP_TARGET_MODE
613                 isp->isp_role = ISP_ROLE_TARGET;
614 #else
615                 isp->isp_role = ISP_DEFAULT_ROLES;
616 #endif
617         }
618
619         tval = 0;
620         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
621             "fwload_disable", &tval) == 0 && tval != 0) {
622                 isp->isp_confopts |= ISP_CFG_NORELOAD;
623         }
624         tval = 0;
625         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
626             "ignore_nvram", &tval) == 0 && tval != 0) {
627                 isp->isp_confopts |= ISP_CFG_NONVRAM;
628         }
629         tval = 0;
630         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
631             "fullduplex", &tval) == 0 && tval != 0) {
632                 isp->isp_confopts |= ISP_CFG_FULL_DUPLEX;
633         }
634 #ifdef  ISP_FW_CRASH_DUMP
635         tval = 0;
636         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
637             "fw_dump_enable", &tval) == 0 && tval != 0) {
638                 size_t amt = 0;
639                 if (IS_2200(isp)) {
640                         amt = QLA2200_RISC_IMAGE_DUMP_SIZE;
641                 } else if (IS_23XX(isp)) {
642                         amt = QLA2300_RISC_IMAGE_DUMP_SIZE;
643                 }
644                 if (amt) {
645                         FCPARAM(isp)->isp_dump_data =
646                             malloc(amt, M_DEVBUF, M_WAITOK | M_ZERO);
647                 } else {
648                         device_printf(dev,
649                             "f/w crash dumps not supported for this model\n");
650                 }
651         }
652 #endif
653
654         sptr = 0;
655         if (resource_string_value(device_get_name(dev), device_get_unit(dev),
656             "topology", (const char **) &sptr) == 0 && sptr != 0) {
657                 if (strcmp(sptr, "lport") == 0) {
658                         isp->isp_confopts |= ISP_CFG_LPORT;
659                 } else if (strcmp(sptr, "nport") == 0) {
660                         isp->isp_confopts |= ISP_CFG_NPORT;
661                 } else if (strcmp(sptr, "lport-only") == 0) {
662                         isp->isp_confopts |= ISP_CFG_LPORT_ONLY;
663                 } else if (strcmp(sptr, "nport-only") == 0) {
664                         isp->isp_confopts |= ISP_CFG_NPORT_ONLY;
665                 }
666         }
667
668         /*
669          * Because the resource_*_value functions can neither return
670          * 64 bit integer values, nor can they be directly coerced
671          * to interpret the right hand side of the assignment as
672          * you want them to interpret it, we have to force WWN
673          * hint replacement to specify WWN strings with a leading
674          * 'w' (e..g w50000000aaaa0001). Sigh.
675          */
676         sptr = 0;
677         tval = resource_string_value(device_get_name(dev), device_get_unit(dev),
678             "portwwn", (const char **) &sptr);
679         if (tval == 0 && sptr != 0 && *sptr++ == 'w') {
680                 char *eptr = 0;
681                 isp->isp_osinfo.default_port_wwn = strtouq(sptr, &eptr, 16);
682                 if (eptr < sptr + 16 || isp->isp_osinfo.default_port_wwn == 0) {
683                         device_printf(dev, "mangled portwwn hint '%s'\n", sptr);
684                         isp->isp_osinfo.default_port_wwn = 0;
685                 } else {
686                         isp->isp_confopts |= ISP_CFG_OWNWWPN;
687                 }
688         }
689         if (isp->isp_osinfo.default_port_wwn == 0) {
690                 isp->isp_osinfo.default_port_wwn = 0x400000007F000009ull;
691         }
692
693         sptr = 0;
694         tval = resource_string_value(device_get_name(dev), device_get_unit(dev),
695             "nodewwn", (const char **) &sptr);
696         if (tval == 0 && sptr != 0 && *sptr++ == 'w') {
697                 char *eptr = 0;
698                 isp->isp_osinfo.default_node_wwn = strtouq(sptr, &eptr, 16);
699                 if (eptr < sptr + 16 || isp->isp_osinfo.default_node_wwn == 0) {
700                         device_printf(dev, "mangled nodewwn hint '%s'\n", sptr);
701                         isp->isp_osinfo.default_node_wwn = 0;
702                 } else {
703                         isp->isp_confopts |= ISP_CFG_OWNWWNN;
704                 }
705         }
706         if (isp->isp_osinfo.default_node_wwn == 0) {
707                 isp->isp_osinfo.default_node_wwn = 0x400000007F000009ull;
708         }
709
710         isp->isp_osinfo.default_id = -1;
711         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
712             "iid", &tval) == 0) {
713                 isp->isp_osinfo.default_id = tval;
714                 isp->isp_confopts |= ISP_CFG_OWNLOOPID;
715         }
716         if (isp->isp_osinfo.default_id == -1) {
717                 if (IS_FC(isp)) {
718                         isp->isp_osinfo.default_id = 109;
719                 } else {
720                         isp->isp_osinfo.default_id = 7;
721                 }
722         }
723
724         /*
725          * Set up logging levels.
726          */
727         tval = 0;
728         (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
729             "debug", &tval);
730         if (tval) {
731                 isp->isp_dblev = tval;
732         } else {
733                 isp->isp_dblev = ISP_LOGWARN|ISP_LOGERR;
734         }
735         if (bootverbose) {
736                 isp->isp_dblev |= ISP_LOGCONFIG|ISP_LOGINFO;
737         }
738
739         tval = 0;
740         (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
741             "hysteresis", &tval);
742         if (tval >= 0 && tval < 256) {
743                 isp->isp_osinfo.hysteresis = tval;
744         } else {
745                 isp->isp_osinfo.hysteresis = isp_fabric_hysteresis;
746         }
747
748         tval = -1;
749         (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
750             "loop_down_limit", &tval);
751         if (tval >= 0 && tval < 0xffff) {
752                 isp->isp_osinfo.loop_down_limit = tval;
753         } else {
754                 isp->isp_osinfo.loop_down_limit = isp_loop_down_limit;
755         }
756
757         tval = -1;
758         (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
759             "gone_device_time", &tval);
760         if (tval >= 0 && tval < 0xffff) {
761                 isp->isp_osinfo.gone_device_time = tval;
762         } else {
763                 isp->isp_osinfo.gone_device_time = isp_gone_device_time;
764         }
765 }
766
767 static void
768 isp_get_pci_options(device_t dev, int *m1, int *m2)
769 {
770         int tval;
771         /*
772          * Which we should try first - memory mapping or i/o mapping?
773          *
774          * We used to try memory first followed by i/o on alpha, otherwise
775          * the reverse, but we should just try memory first all the time now.
776          */
777         *m1 = PCIM_CMD_MEMEN;
778         *m2 = PCIM_CMD_PORTEN;
779
780         tval = 0;
781         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
782             "prefer_iomap", &tval) == 0 && tval != 0) {
783                 *m1 = PCIM_CMD_PORTEN;
784                 *m2 = PCIM_CMD_MEMEN;
785         }
786         tval = 0;
787         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
788             "prefer_memmap", &tval) == 0 && tval != 0) {
789                 *m1 = PCIM_CMD_MEMEN;
790                 *m2 = PCIM_CMD_PORTEN;
791         }
792 }
793 #endif
794
795 static int
796 isp_pci_attach(device_t dev)
797 {
798         struct resource *regs, *irq;
799         int rtp, rgd, iqd, m1, m2;
800         uint32_t data, cmd, linesz, psize, basetype;
801         struct isp_pcisoftc *pcs;
802         ispsoftc_t *isp = NULL;
803         struct ispmdvec *mdvp;
804 #if __FreeBSD_version >= 500000  
805         int locksetup = 0;
806 #endif
807
808         pcs = device_get_softc(dev);
809         if (pcs == NULL) {
810                 device_printf(dev, "cannot get softc\n");
811                 return (ENOMEM);
812         }
813         memset(pcs, 0, sizeof (*pcs));
814         pcs->pci_dev = dev;
815         isp = &pcs->pci_isp;
816
817         /*
818          * Set and Get Generic Options
819          */
820         isp_get_options(dev, isp);
821
822         /*
823          * Check to see if options have us disabled
824          */
825         if (isp->isp_osinfo.disabled) {
826                 /*
827                  * But return zero to preserve unit numbering
828                  */
829                 return (0);
830         }
831
832         /*
833          * Get PCI options- which in this case are just mapping preferences.
834          */
835         isp_get_pci_options(dev, &m1, &m2);
836
837         linesz = PCI_DFLT_LNSZ;
838         irq = regs = NULL;
839         rgd = rtp = iqd = 0;
840
841         cmd = pci_read_config(dev, PCIR_COMMAND, 2);
842         if (cmd & m1) {
843                 rtp = (m1 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
844                 rgd = (m1 == PCIM_CMD_MEMEN)? MEM_MAP_REG : IO_MAP_REG;
845                 regs = bus_alloc_resource_any(dev, rtp, &rgd, RF_ACTIVE);
846         }
847         if (regs == NULL && (cmd & m2)) {
848                 rtp = (m2 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
849                 rgd = (m2 == PCIM_CMD_MEMEN)? MEM_MAP_REG : IO_MAP_REG;
850                 regs = bus_alloc_resource_any(dev, rtp, &rgd, RF_ACTIVE);
851         }
852         if (regs == NULL) {
853                 device_printf(dev, "unable to map any ports\n");
854                 goto bad;
855         }
856         if (bootverbose) {
857                 device_printf(dev, "using %s space register mapping\n",
858                     (rgd == IO_MAP_REG)? "I/O" : "Memory");
859         }
860         pcs->pci_dev = dev;
861         pcs->pci_reg = regs;
862         pcs->pci_st = rman_get_bustag(regs);
863         pcs->pci_sh = rman_get_bushandle(regs);
864
865         pcs->pci_poff[BIU_BLOCK >> _BLK_REG_SHFT] = BIU_REGS_OFF;
866         pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS_OFF;
867         pcs->pci_poff[SXP_BLOCK >> _BLK_REG_SHFT] = PCI_SXP_REGS_OFF;
868         pcs->pci_poff[RISC_BLOCK >> _BLK_REG_SHFT] = PCI_RISC_REGS_OFF;
869         pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] = DMA_REGS_OFF;
870         mdvp = &mdvec;
871         basetype = ISP_HA_SCSI_UNKNOWN;
872         psize = sizeof (sdparam);
873         if (pci_get_devid(dev) == PCI_QLOGIC_ISP1020) {
874                 mdvp = &mdvec;
875                 basetype = ISP_HA_SCSI_UNKNOWN;
876                 psize = sizeof (sdparam);
877         }
878         if (pci_get_devid(dev) == PCI_QLOGIC_ISP1080) {
879                 mdvp = &mdvec_1080;
880                 basetype = ISP_HA_SCSI_1080;
881                 psize = sizeof (sdparam);
882                 pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] =
883                     ISP1080_DMA_REGS_OFF;
884         }
885         if (pci_get_devid(dev) == PCI_QLOGIC_ISP1240) {
886                 mdvp = &mdvec_1080;
887                 basetype = ISP_HA_SCSI_1240;
888                 psize = 2 * sizeof (sdparam);
889                 pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] =
890                     ISP1080_DMA_REGS_OFF;
891         }
892         if (pci_get_devid(dev) == PCI_QLOGIC_ISP1280) {
893                 mdvp = &mdvec_1080;
894                 basetype = ISP_HA_SCSI_1280;
895                 psize = 2 * sizeof (sdparam);
896                 pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] =
897                     ISP1080_DMA_REGS_OFF;
898         }
899         if (pci_get_devid(dev) == PCI_QLOGIC_ISP10160) {
900                 mdvp = &mdvec_12160;
901                 basetype = ISP_HA_SCSI_10160;
902                 psize = sizeof (sdparam);
903                 pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] =
904                     ISP1080_DMA_REGS_OFF;
905         }
906         if (pci_get_devid(dev) == PCI_QLOGIC_ISP12160) {
907                 mdvp = &mdvec_12160;
908                 basetype = ISP_HA_SCSI_12160;
909                 psize = 2 * sizeof (sdparam);
910                 pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] =
911                     ISP1080_DMA_REGS_OFF;
912         }
913         if (pci_get_devid(dev) == PCI_QLOGIC_ISP2100) {
914                 mdvp = &mdvec_2100;
915                 basetype = ISP_HA_FC_2100;
916                 psize = sizeof (fcparam);
917                 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] =
918                     PCI_MBOX_REGS2100_OFF;
919                 if (pci_get_revid(dev) < 3) {
920                         /*
921                          * XXX: Need to get the actual revision
922                          * XXX: number of the 2100 FB. At any rate,
923                          * XXX: lower cache line size for early revision
924                          * XXX; boards.
925                          */
926                         linesz = 1;
927                 }
928         }
929         if (pci_get_devid(dev) == PCI_QLOGIC_ISP2200) {
930                 mdvp = &mdvec_2200;
931                 basetype = ISP_HA_FC_2200;
932                 psize = sizeof (fcparam);
933                 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] =
934                     PCI_MBOX_REGS2100_OFF;
935         }
936         if (pci_get_devid(dev) == PCI_QLOGIC_ISP2300) {
937                 mdvp = &mdvec_2300;
938                 basetype = ISP_HA_FC_2300;
939                 psize = sizeof (fcparam);
940                 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] =
941                     PCI_MBOX_REGS2300_OFF;
942         }
943         if (pci_get_devid(dev) == PCI_QLOGIC_ISP2312 ||
944             pci_get_devid(dev) == PCI_QLOGIC_ISP6312) {
945                 mdvp = &mdvec_2300;
946                 basetype = ISP_HA_FC_2312;
947                 psize = sizeof (fcparam);
948                 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] =
949                     PCI_MBOX_REGS2300_OFF;
950         }
951         if (pci_get_devid(dev) == PCI_QLOGIC_ISP2322 ||
952             pci_get_devid(dev) == PCI_QLOGIC_ISP6322) {
953                 mdvp = &mdvec_2300;
954                 basetype = ISP_HA_FC_2322;
955                 psize = sizeof (fcparam);
956                 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] =
957                     PCI_MBOX_REGS2300_OFF;
958         }
959         if (pci_get_devid(dev) == PCI_QLOGIC_ISP2422) {
960                 mdvp = &mdvec_2400;
961                 basetype = ISP_HA_FC_2400;
962                 psize = sizeof (fcparam);
963                 pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] =
964                     PCI_MBOX_REGS2400_OFF;
965         }
966         isp = &pcs->pci_isp;
967         isp->isp_param = malloc(psize, M_DEVBUF, M_NOWAIT | M_ZERO);
968         if (isp->isp_param == NULL) {
969                 device_printf(dev, "cannot allocate parameter data\n");
970                 goto bad;
971         }
972         isp->isp_mdvec = mdvp;
973         isp->isp_type = basetype;
974         isp->isp_revision = pci_get_revid(dev);
975         isp->isp_dev = dev;
976
977 #if __FreeBSD_version >= 700000  
978         /*
979          * Try and find firmware for this device.
980          */
981         {
982                 char fwname[32];
983                 unsigned int did = pci_get_device(dev);
984
985                 /*
986                  * Map a few pci ids to fw names
987                  */
988                 switch (did) {
989                 case PCI_PRODUCT_QLOGIC_ISP1020:
990                         did = 0x1040;
991                         break;
992                 case PCI_PRODUCT_QLOGIC_ISP1240:
993                         did = 0x1080;
994                         break;
995                 case PCI_PRODUCT_QLOGIC_ISP10160:
996                 case PCI_PRODUCT_QLOGIC_ISP12160:
997                         did = 0x12160;
998                         break;
999                 case PCI_PRODUCT_QLOGIC_ISP6312:
1000                 case PCI_PRODUCT_QLOGIC_ISP2312:
1001                         did = 0x2300;
1002                         break;
1003                 case PCI_PRODUCT_QLOGIC_ISP6322:
1004                         did = 0x2322;
1005                         break;
1006                 case PCI_PRODUCT_QLOGIC_ISP2422:
1007                         did = 0x2400;
1008                         break;
1009                 default:
1010                         break;
1011                 }
1012
1013                 isp->isp_osinfo.fw = NULL;
1014                 if (isp->isp_role & ISP_ROLE_TARGET) {
1015                         snprintf(fwname, sizeof (fwname), "isp_%04x_it", did);
1016                         isp->isp_osinfo.fw = firmware_get(fwname);
1017                 }
1018                 if (isp->isp_osinfo.fw == NULL) {
1019                         snprintf(fwname, sizeof (fwname), "isp_%04x", did);
1020                         isp->isp_osinfo.fw = firmware_get(fwname);
1021                 }
1022                 if (isp->isp_osinfo.fw != NULL) {
1023                         union {
1024                                 const void *fred;
1025                                 uint16_t *bob;
1026                         } u;
1027                         u.fred = isp->isp_osinfo.fw->data;
1028                         isp->isp_mdvec->dv_ispfw = u.bob;
1029                 }
1030         }
1031 #else
1032         if (isp_get_firmware_p) {
1033                 int device = (int) pci_get_device(dev);
1034 #ifdef  ISP_TARGET_MODE
1035                 (*isp_get_firmware_p)(0, 1, device, &mdvp->dv_ispfw);
1036 #else
1037                 (*isp_get_firmware_p)(0, 0, device, &mdvp->dv_ispfw);
1038 #endif
1039         }
1040 #endif
1041
1042         /*
1043          * Make sure that SERR, PERR, WRITE INVALIDATE and BUSMASTER
1044          * are set.
1045          */
1046         cmd |= PCIM_CMD_SEREN | PCIM_CMD_PERRESPEN |
1047                 PCIM_CMD_BUSMASTEREN | PCIM_CMD_INVEN;
1048
1049         if (IS_2300(isp)) {     /* per QLogic errata */
1050                 cmd &= ~PCIM_CMD_INVEN;
1051         }
1052
1053         if (IS_2322(isp) || pci_get_devid(dev) == PCI_QLOGIC_ISP6312) {
1054                 cmd &= ~PCIM_CMD_INTX_DISABLE;
1055         }
1056
1057         if (IS_24XX(isp)) {
1058                 int reg;
1059
1060                 cmd &= ~PCIM_CMD_INTX_DISABLE;
1061
1062                 /*
1063                  * Is this a PCI-X card? If so, set max read byte count.
1064                  */
1065                 if (pci_find_extcap(dev, PCIY_PCIX, &reg) == 0) {
1066                         uint16_t pxcmd;
1067                         reg += 2;
1068
1069                         pxcmd = pci_read_config(dev, reg, 2);
1070                         pxcmd &= ~0xc;
1071                         pxcmd |= 0x8;
1072                         pci_write_config(dev, reg, 2, pxcmd);
1073                 }
1074
1075                 /*
1076                  * Is this a PCI Express card? If so, set max read byte count.
1077                  */
1078                 if (pci_find_extcap(dev, PCIY_EXPRESS, &reg) == 0) {
1079                         uint16_t pectl;
1080
1081                         reg += 0x8;
1082                         pectl = pci_read_config(dev, reg, 2);
1083                         pectl &= ~0x7000;
1084                         pectl |= 0x4000;
1085                         pci_write_config(dev, reg, 2, pectl);
1086                 }
1087         }
1088
1089         pci_write_config(dev, PCIR_COMMAND, cmd, 2);
1090
1091         /*
1092          * Make sure the Cache Line Size register is set sensibly.
1093          */
1094         data = pci_read_config(dev, PCIR_CACHELNSZ, 1);
1095         if (data != linesz) {
1096                 data = PCI_DFLT_LNSZ;
1097                 isp_prt(isp, ISP_LOGCONFIG, "set PCI line size to %d", data);
1098                 pci_write_config(dev, PCIR_CACHELNSZ, data, 1);
1099         }
1100
1101         /*
1102          * Make sure the Latency Timer is sane.
1103          */
1104         data = pci_read_config(dev, PCIR_LATTIMER, 1);
1105         if (data < PCI_DFLT_LTNCY) {
1106                 data = PCI_DFLT_LTNCY;
1107                 isp_prt(isp, ISP_LOGCONFIG, "set PCI latency to %d", data);
1108                 pci_write_config(dev, PCIR_LATTIMER, data, 1);
1109         }
1110
1111         /*
1112          * Make sure we've disabled the ROM.
1113          */
1114         data = pci_read_config(dev, PCIR_ROMADDR, 4);
1115         data &= ~1;
1116         pci_write_config(dev, PCIR_ROMADDR, data, 4);
1117
1118         iqd = 0;
1119         irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &iqd,
1120             RF_ACTIVE | RF_SHAREABLE);
1121         if (irq == NULL) {
1122                 device_printf(dev, "could not allocate interrupt\n");
1123                 goto bad;
1124         }
1125
1126 #if __FreeBSD_version >= 500000  
1127         /* Make sure the lock is set up. */
1128         mtx_init(&isp->isp_osinfo.lock, "isp", NULL, MTX_DEF);
1129         locksetup++;
1130 #endif
1131
1132         if (bus_setup_intr(dev, irq, ISP_IFLAGS, isp_pci_intr, isp, &pcs->ih)) {
1133                 device_printf(dev, "could not setup interrupt\n");
1134                 goto bad;
1135         }
1136
1137         /*
1138          * Last minute checks...
1139          */
1140         if (IS_23XX(isp) || IS_24XX(isp)) {
1141                 isp->isp_port = pci_get_function(dev);
1142         }
1143
1144         if (IS_23XX(isp)) {
1145                 /*
1146                  * Can't tell if ROM will hang on 'ABOUT FIRMWARE' command.
1147                  */
1148                 isp->isp_touched = 1;
1149         }
1150
1151         /*
1152          * Make sure we're in reset state.
1153          */
1154         ISP_LOCK(isp);
1155         isp_reset(isp);
1156         if (isp->isp_state != ISP_RESETSTATE) {
1157                 ISP_UNLOCK(isp);
1158                 goto bad;
1159         }
1160         isp_init(isp);
1161         if (isp->isp_role != ISP_ROLE_NONE && isp->isp_state != ISP_INITSTATE) {
1162                 isp_uninit(isp);
1163                 ISP_UNLOCK(isp);
1164                 goto bad;
1165         }
1166         isp_attach(isp);
1167         if (isp->isp_role != ISP_ROLE_NONE && isp->isp_state != ISP_RUNSTATE) {
1168                 isp_uninit(isp);
1169                 ISP_UNLOCK(isp);
1170                 goto bad;
1171         }
1172         /*
1173          * XXXX: Here is where we might unload the f/w module
1174          * XXXX: (or decrease the reference count to it).
1175          */
1176         ISP_UNLOCK(isp);
1177
1178         return (0);
1179
1180 bad:
1181
1182         if (pcs && pcs->ih) {
1183                 (void) bus_teardown_intr(dev, irq, pcs->ih);
1184         }
1185
1186 #if __FreeBSD_version >= 500000  
1187         if (locksetup && isp) {
1188                 mtx_destroy(&isp->isp_osinfo.lock);
1189         }
1190 #endif
1191
1192         if (irq) {
1193                 (void) bus_release_resource(dev, SYS_RES_IRQ, iqd, irq);
1194         }
1195
1196
1197         if (regs) {
1198                 (void) bus_release_resource(dev, rtp, rgd, regs);
1199         }
1200
1201         if (pcs) {
1202                 if (pcs->pci_isp.isp_param) {
1203 #ifdef  ISP_FW_CRASH_DUMP
1204                         if (IS_FC(isp) && FCPARAM(isp)->isp_dump_data) {
1205                                 free(FCPARAM(isp)->isp_dump_data, M_DEVBUF);
1206                         }
1207 #endif
1208                         free(pcs->pci_isp.isp_param, M_DEVBUF);
1209                 }
1210         }
1211
1212         /*
1213          * XXXX: Here is where we might unload the f/w module
1214          * XXXX: (or decrease the reference count to it).
1215          */
1216         return (ENXIO);
1217 }
1218
1219 static int
1220 isp_pci_detach(device_t dev)
1221 {
1222         struct isp_pcisoftc *pcs;
1223         ispsoftc_t *isp;
1224
1225         pcs = device_get_softc(dev);
1226         if (pcs == NULL) {
1227                 return (ENXIO);
1228         }
1229         isp = (ispsoftc_t *) pcs;
1230         ISP_DISABLE_INTS(isp);
1231         return (0);
1232 }
1233
1234 static void
1235 isp_pci_intr(void *arg)
1236 {
1237         ispsoftc_t *isp = arg;
1238         uint32_t isr;
1239         uint16_t sema, mbox;
1240
1241         ISP_LOCK(isp);
1242         isp->isp_intcnt++;
1243         if (ISP_READ_ISR(isp, &isr, &sema, &mbox) == 0) {
1244                 isp->isp_intbogus++;
1245         } else {
1246                 isp_intr(isp, isr, sema, mbox);
1247         }
1248         ISP_UNLOCK(isp);
1249 }
1250
1251
1252 #define IspVirt2Off(a, x)       \
1253         (((struct isp_pcisoftc *)a)->pci_poff[((x) & _BLK_REG_MASK) >> \
1254         _BLK_REG_SHFT] + ((x) & 0xfff))
1255
1256 #define BXR2(pcs, off)          \
1257         bus_space_read_2(pcs->pci_st, pcs->pci_sh, off)
1258 #define BXW2(pcs, off, v)       \
1259         bus_space_write_2(pcs->pci_st, pcs->pci_sh, off, v)
1260 #define BXR4(pcs, off)          \
1261         bus_space_read_4(pcs->pci_st, pcs->pci_sh, off)
1262 #define BXW4(pcs, off, v)       \
1263         bus_space_write_4(pcs->pci_st, pcs->pci_sh, off, v)
1264
1265
1266 static __inline int
1267 isp_pci_rd_debounced(ispsoftc_t *isp, int off, uint16_t *rp)
1268 {
1269         struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
1270         uint32_t val0, val1;
1271         int i = 0;
1272
1273         do {
1274                 val0 = BXR2(pcs, IspVirt2Off(isp, off));
1275                 val1 = BXR2(pcs, IspVirt2Off(isp, off));
1276         } while (val0 != val1 && ++i < 1000);
1277         if (val0 != val1) {
1278                 return (1);
1279         }
1280         *rp = val0;
1281         return (0);
1282 }
1283
1284 static int
1285 isp_pci_rd_isr(ispsoftc_t *isp, uint32_t *isrp,
1286     uint16_t *semap, uint16_t *mbp)
1287 {
1288         struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
1289         uint16_t isr, sema;
1290
1291         if (IS_2100(isp)) {
1292                 if (isp_pci_rd_debounced(isp, BIU_ISR, &isr)) {
1293                     return (0);
1294                 }
1295                 if (isp_pci_rd_debounced(isp, BIU_SEMA, &sema)) {
1296                     return (0);
1297                 }
1298         } else {
1299                 isr = BXR2(pcs, IspVirt2Off(isp, BIU_ISR));
1300                 sema = BXR2(pcs, IspVirt2Off(isp, BIU_SEMA));
1301         }
1302         isp_prt(isp, ISP_LOGDEBUG3, "ISR 0x%x SEMA 0x%x", isr, sema);
1303         isr &= INT_PENDING_MASK(isp);
1304         sema &= BIU_SEMA_LOCK;
1305         if (isr == 0 && sema == 0) {
1306                 return (0);
1307         }
1308         *isrp = isr;
1309         if ((*semap = sema) != 0) {
1310                 if (IS_2100(isp)) {
1311                         if (isp_pci_rd_debounced(isp, OUTMAILBOX0, mbp)) {
1312                                 return (0);
1313                         }
1314                 } else {
1315                         *mbp = BXR2(pcs, IspVirt2Off(isp, OUTMAILBOX0));
1316                 }
1317         }
1318         return (1);
1319 }
1320
1321 static int
1322 isp_pci_rd_isr_2300(ispsoftc_t *isp, uint32_t *isrp,
1323     uint16_t *semap, uint16_t *mbox0p)
1324 {
1325         struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
1326         uint32_t hccr;
1327         uint32_t r2hisr;
1328
1329         if (!(BXR2(pcs, IspVirt2Off(isp, BIU_ISR) & BIU2100_ISR_RISC_INT))) {
1330                 *isrp = 0;
1331                 return (0);
1332         }
1333         r2hisr = BXR4(pcs, IspVirt2Off(pcs, BIU_R2HSTSLO));
1334         isp_prt(isp, ISP_LOGDEBUG3, "RISC2HOST ISR 0x%x", r2hisr);
1335         if ((r2hisr & BIU_R2HST_INTR) == 0) {
1336                 *isrp = 0;
1337                 return (0);
1338         }
1339         switch (r2hisr & BIU_R2HST_ISTAT_MASK) {
1340         case ISPR2HST_ROM_MBX_OK:
1341         case ISPR2HST_ROM_MBX_FAIL:
1342         case ISPR2HST_MBX_OK:
1343         case ISPR2HST_MBX_FAIL:
1344         case ISPR2HST_ASYNC_EVENT:
1345                 *isrp = r2hisr & 0xffff;
1346                 *mbox0p = (r2hisr >> 16);
1347                 *semap = 1;
1348                 return (1);
1349         case ISPR2HST_RIO_16:
1350                 *isrp = r2hisr & 0xffff;
1351                 *mbox0p = ASYNC_RIO1;
1352                 *semap = 1;
1353                 return (1);
1354         case ISPR2HST_FPOST:
1355                 *isrp = r2hisr & 0xffff;
1356                 *mbox0p = ASYNC_CMD_CMPLT;
1357                 *semap = 1;
1358                 return (1);
1359         case ISPR2HST_FPOST_CTIO:
1360                 *isrp = r2hisr & 0xffff;
1361                 *mbox0p = ASYNC_CTIO_DONE;
1362                 *semap = 1;
1363                 return (1);
1364         case ISPR2HST_RSPQ_UPDATE:
1365                 *isrp = r2hisr & 0xffff;
1366                 *mbox0p = 0;
1367                 *semap = 0;
1368                 return (1);
1369         default:
1370                 hccr = ISP_READ(isp, HCCR);
1371                 if (hccr & HCCR_PAUSE) {
1372                         ISP_WRITE(isp, HCCR, HCCR_RESET);
1373                         isp_prt(isp, ISP_LOGERR,
1374                             "RISC paused at interrupt (%x->%x\n", hccr,
1375                             ISP_READ(isp, HCCR));
1376                 } else {
1377                         isp_prt(isp, ISP_LOGERR, "unknown interrupt 0x%x\n",
1378                             r2hisr);
1379                 }
1380                 return (0);
1381         }
1382 }
1383
1384 static int
1385 isp_pci_rd_isr_2400(ispsoftc_t *isp, uint32_t *isrp,
1386     uint16_t *semap, uint16_t *mbox0p)
1387 {
1388         struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
1389         uint32_t r2hisr;
1390
1391         r2hisr = BXR4(pcs, IspVirt2Off(pcs, BIU2400_R2HSTSLO));
1392         isp_prt(isp, ISP_LOGDEBUG3, "RISC2HOST ISR 0x%x", r2hisr);
1393         if ((r2hisr & BIU2400_R2HST_INTR) == 0) {
1394                 *isrp = 0;
1395                 return (0);
1396         }
1397         switch (r2hisr & BIU2400_R2HST_ISTAT_MASK) {
1398         case ISP2400R2HST_ROM_MBX_OK:
1399         case ISP2400R2HST_ROM_MBX_FAIL:
1400         case ISP2400R2HST_MBX_OK:
1401         case ISP2400R2HST_MBX_FAIL:
1402         case ISP2400R2HST_ASYNC_EVENT:
1403                 *isrp = r2hisr & 0xffff;
1404                 *mbox0p = (r2hisr >> 16);
1405                 *semap = 1;
1406                 return (1);
1407         case ISP2400R2HST_RSPQ_UPDATE:
1408         case ISP2400R2HST_ATIO_RSPQ_UPDATE:
1409         case ISP2400R2HST_ATIO_RQST_UPDATE:
1410                 *isrp = r2hisr & 0xffff;
1411                 *mbox0p = 0;
1412                 *semap = 0;
1413                 return (1);
1414         default:
1415                 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
1416                 isp_prt(isp, ISP_LOGERR, "unknown interrupt 0x%x\n", r2hisr);
1417                 return (0);
1418         }
1419 }
1420
1421 static uint32_t
1422 isp_pci_rd_reg(ispsoftc_t *isp, int regoff)
1423 {
1424         uint32_t rv;
1425         struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
1426         int oldconf = 0;
1427
1428         if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
1429                 /*
1430                  * We will assume that someone has paused the RISC processor.
1431                  */
1432                 oldconf = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
1433                 BXW2(pcs, IspVirt2Off(isp, BIU_CONF1),
1434                     oldconf | BIU_PCI_CONF1_SXP);
1435         }
1436         rv = BXR2(pcs, IspVirt2Off(isp, regoff));
1437         if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
1438                 BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), oldconf);
1439         }
1440         return (rv);
1441 }
1442
1443 static void
1444 isp_pci_wr_reg(ispsoftc_t *isp, int regoff, uint32_t val)
1445 {
1446         struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
1447         int oldconf = 0;
1448         volatile int junk;
1449
1450         if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
1451                 /*
1452                  * We will assume that someone has paused the RISC processor.
1453                  */
1454                 oldconf = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
1455                 BXW2(pcs, IspVirt2Off(isp, BIU_CONF1),
1456                     oldconf | BIU_PCI_CONF1_SXP);
1457                 junk = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
1458         }
1459         BXW2(pcs, IspVirt2Off(isp, regoff), val);
1460         junk = BXR2(pcs, IspVirt2Off(isp, regoff));
1461         if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
1462                 BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), oldconf);
1463                 junk = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
1464         }
1465 }
1466
1467 static uint32_t
1468 isp_pci_rd_reg_1080(ispsoftc_t *isp, int regoff)
1469 {
1470         uint32_t rv, oc = 0;
1471         struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
1472
1473         if ((regoff & _BLK_REG_MASK) == SXP_BLOCK ||
1474             (regoff & _BLK_REG_MASK) == (SXP_BLOCK|SXP_BANK1_SELECT)) {
1475                 uint32_t tc;
1476                 /*
1477                  * We will assume that someone has paused the RISC processor.
1478                  */
1479                 oc = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
1480                 tc = oc & ~BIU_PCI1080_CONF1_DMA;
1481                 if (regoff & SXP_BANK1_SELECT)
1482                         tc |= BIU_PCI1080_CONF1_SXP1;
1483                 else
1484                         tc |= BIU_PCI1080_CONF1_SXP0;
1485                 BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), tc);
1486         } else if ((regoff & _BLK_REG_MASK) == DMA_BLOCK) {
1487                 oc = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
1488                 BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), 
1489                     oc | BIU_PCI1080_CONF1_DMA);
1490         }
1491         rv = BXR2(pcs, IspVirt2Off(isp, regoff));
1492         if (oc) {
1493                 BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), oc);
1494         }
1495         return (rv);
1496 }
1497
1498 static void
1499 isp_pci_wr_reg_1080(ispsoftc_t *isp, int regoff, uint32_t val)
1500 {
1501         struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
1502         int oc = 0;
1503         volatile int junk;
1504
1505         if ((regoff & _BLK_REG_MASK) == SXP_BLOCK ||
1506             (regoff & _BLK_REG_MASK) == (SXP_BLOCK|SXP_BANK1_SELECT)) {
1507                 uint32_t tc;
1508                 /*
1509                  * We will assume that someone has paused the RISC processor.
1510                  */
1511                 oc = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
1512                 tc = oc & ~BIU_PCI1080_CONF1_DMA;
1513                 if (regoff & SXP_BANK1_SELECT)
1514                         tc |= BIU_PCI1080_CONF1_SXP1;
1515                 else
1516                         tc |= BIU_PCI1080_CONF1_SXP0;
1517                 BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), tc);
1518                 junk = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
1519         } else if ((regoff & _BLK_REG_MASK) == DMA_BLOCK) {
1520                 oc = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
1521                 BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), 
1522                     oc | BIU_PCI1080_CONF1_DMA);
1523                 junk = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
1524         }
1525         BXW2(pcs, IspVirt2Off(isp, regoff), val);
1526         junk = BXR2(pcs, IspVirt2Off(isp, regoff));
1527         if (oc) {
1528                 BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), oc);
1529                 junk = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
1530         }
1531 }
1532
1533 static uint32_t
1534 isp_pci_rd_reg_2400(ispsoftc_t *isp, int regoff)
1535 {
1536         struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
1537         uint32_t rv;
1538         int block = regoff & _BLK_REG_MASK;
1539
1540         switch (block) {
1541         case BIU_BLOCK:
1542                 break;
1543         case MBOX_BLOCK:
1544                 return (BXR2(pcs, IspVirt2Off(pcs, regoff)));
1545         case SXP_BLOCK:
1546                 isp_prt(isp, ISP_LOGWARN, "SXP_BLOCK read at 0x%x", regoff);
1547                 return (0xffffffff);
1548         case RISC_BLOCK:
1549                 isp_prt(isp, ISP_LOGWARN, "RISC_BLOCK read at 0x%x", regoff);
1550                 return (0xffffffff);
1551         case DMA_BLOCK:
1552                 isp_prt(isp, ISP_LOGWARN, "DMA_BLOCK read at 0x%x", regoff);
1553                 return (0xffffffff);
1554         default:
1555                 isp_prt(isp, ISP_LOGWARN, "unknown block read at 0x%x", regoff);
1556                 return (0xffffffff);
1557         }
1558
1559
1560         switch (regoff) {
1561         case BIU2400_FLASH_ADDR:
1562         case BIU2400_FLASH_DATA:
1563         case BIU2400_ICR:
1564         case BIU2400_ISR:
1565         case BIU2400_CSR:
1566         case BIU2400_REQINP:
1567         case BIU2400_REQOUTP:
1568         case BIU2400_RSPINP:
1569         case BIU2400_RSPOUTP:
1570         case BIU2400_PRI_RQINP:
1571         case BIU2400_PRI_RSPINP:
1572         case BIU2400_ATIO_RSPINP:
1573         case BIU2400_ATIO_REQINP:
1574         case BIU2400_HCCR:
1575         case BIU2400_GPIOD:
1576         case BIU2400_GPIOE:
1577         case BIU2400_HSEMA:
1578                 rv = BXR4(pcs, IspVirt2Off(pcs, regoff));
1579                 break;
1580         case BIU2400_R2HSTSLO:
1581                 rv = BXR4(pcs, IspVirt2Off(pcs, regoff));
1582                 break;
1583         case BIU2400_R2HSTSHI:
1584                 rv = BXR4(pcs, IspVirt2Off(pcs, regoff)) >> 16;
1585                 break;
1586         default:
1587                 isp_prt(isp, ISP_LOGERR,
1588                     "isp_pci_rd_reg_2400: unknown offset %x", regoff);
1589                 rv = 0xffffffff;
1590                 break;
1591         }
1592         return (rv);
1593 }
1594
1595 static void
1596 isp_pci_wr_reg_2400(ispsoftc_t *isp, int regoff, uint32_t val)
1597 {
1598         struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
1599         int block = regoff & _BLK_REG_MASK;
1600         volatile int junk;
1601
1602         switch (block) {
1603         case BIU_BLOCK:
1604                 break;
1605         case MBOX_BLOCK:
1606                 BXW2(pcs, IspVirt2Off(pcs, regoff), val);
1607                 junk = BXR2(pcs, IspVirt2Off(pcs, regoff));
1608                 return;
1609         case SXP_BLOCK:
1610                 isp_prt(isp, ISP_LOGWARN, "SXP_BLOCK write at 0x%x", regoff);
1611                 return;
1612         case RISC_BLOCK:
1613                 isp_prt(isp, ISP_LOGWARN, "RISC_BLOCK write at 0x%x", regoff);
1614                 return;
1615         case DMA_BLOCK:
1616                 isp_prt(isp, ISP_LOGWARN, "DMA_BLOCK write at 0x%x", regoff);
1617                 return;
1618         default:
1619                 isp_prt(isp, ISP_LOGWARN, "unknown block write at 0x%x",
1620                     regoff);
1621                 break;
1622         }
1623
1624         switch (regoff) {
1625         case BIU2400_FLASH_ADDR:
1626         case BIU2400_FLASH_DATA:
1627         case BIU2400_ICR:
1628         case BIU2400_ISR:
1629         case BIU2400_CSR:
1630         case BIU2400_REQINP:
1631         case BIU2400_REQOUTP:
1632         case BIU2400_RSPINP:
1633         case BIU2400_RSPOUTP:
1634         case BIU2400_PRI_RQINP:
1635         case BIU2400_PRI_RSPINP:
1636         case BIU2400_ATIO_RSPINP:
1637         case BIU2400_ATIO_REQINP:
1638         case BIU2400_HCCR:
1639         case BIU2400_GPIOD:
1640         case BIU2400_GPIOE:
1641         case BIU2400_HSEMA:
1642                 BXW4(pcs, IspVirt2Off(pcs, regoff), val);
1643                 junk = BXR4(pcs, IspVirt2Off(pcs, regoff));
1644                 break;
1645         default:
1646                 isp_prt(isp, ISP_LOGERR,
1647                     "isp_pci_wr_reg_2400: bad offset 0x%x", regoff);
1648                 break;
1649         }
1650 }
1651
1652
1653 struct imush {
1654         ispsoftc_t *isp;
1655         int error;
1656 };
1657
1658 static void imc(void *, bus_dma_segment_t *, int, int);
1659
1660 static void
1661 imc(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1662 {
1663         struct imush *imushp = (struct imush *) arg;
1664         if (error) {
1665                 imushp->error = error;
1666         } else {
1667                 ispsoftc_t *isp =imushp->isp;
1668                 bus_addr_t addr = segs->ds_addr;
1669
1670                 isp->isp_rquest_dma = addr;
1671                 addr += ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
1672                 isp->isp_result_dma = addr;
1673                 if (IS_FC(isp)) {
1674                         addr += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
1675                         FCPARAM(isp)->isp_scdma = addr;
1676                 }
1677         }
1678 }
1679
1680 /*
1681  * Should be BUS_SPACE_MAXSIZE, but MAXPHYS is larger than BUS_SPACE_MAXSIZE
1682  */
1683 #define ISP_NSEGS ((MAXPHYS / PAGE_SIZE) + 1)  
1684
1685 #if __FreeBSD_version < 500000  
1686 #define BUS_DMA_ROOTARG NULL
1687 #define isp_dma_tag_create(a, b, c, d, e, f, g, h, i, j, k, z)  \
1688         bus_dma_tag_create(a, b, c, d, e, f, g, h, i, j, k, z)
1689 #elif   __FreeBSD_version < 700020
1690 #define BUS_DMA_ROOTARG NULL
1691 #define isp_dma_tag_create(a, b, c, d, e, f, g, h, i, j, k, z)  \
1692         bus_dma_tag_create(a, b, c, d, e, f, g, h, i, j, k, \
1693         busdma_lock_mutex, &Giant, z)
1694 #else
1695 #define BUS_DMA_ROOTARG bus_get_dma_tag(pcs->pci_dev)
1696 #define isp_dma_tag_create(a, b, c, d, e, f, g, h, i, j, k, z)  \
1697         bus_dma_tag_create(a, b, c, d, e, f, g, h, i, j, k, \
1698         busdma_lock_mutex, &Giant, z)
1699 #endif
1700
1701 static int
1702 isp_pci_mbxdma(ispsoftc_t *isp)
1703 {
1704         struct isp_pcisoftc *pcs = (struct isp_pcisoftc *)isp;
1705         caddr_t base;
1706         uint32_t len;
1707         int i, error, ns;
1708         bus_size_t slim;        /* segment size */
1709         bus_addr_t llim;        /* low limit of unavailable dma */
1710         bus_addr_t hlim;        /* high limit of unavailable dma */
1711         struct imush im;
1712
1713         /*
1714          * Already been here? If so, leave...
1715          */
1716         if (isp->isp_rquest) {
1717                 return (0);
1718         }
1719
1720         if (isp->isp_maxcmds == 0) {
1721                 isp_prt(isp, ISP_LOGERR, "maxcmds not set");
1722                 return (1);
1723         }
1724
1725         hlim = BUS_SPACE_MAXADDR;
1726         if (IS_ULTRA2(isp) || IS_FC(isp) || IS_1240(isp)) {
1727                 slim = (bus_size_t) (1ULL << 32);
1728                 llim = BUS_SPACE_MAXADDR;
1729         } else {
1730                 llim = BUS_SPACE_MAXADDR_32BIT;
1731                 slim = (1 << 24);
1732         }
1733
1734         /*
1735          * XXX: We don't really support 64 bit target mode for parallel scsi yet
1736          */
1737 #ifdef  ISP_TARGET_MODE
1738         if (IS_SCSI(isp) && sizeof (bus_addr_t) > 4) {
1739                 isp_prt(isp, ISP_LOGERR, "we cannot do DAC for SPI cards yet");
1740                 return (1);
1741         }
1742 #endif
1743
1744         ISP_UNLOCK(isp);
1745         if (isp_dma_tag_create(BUS_DMA_ROOTARG, 1, slim, llim,
1746             hlim, NULL, NULL, BUS_SPACE_MAXSIZE, ISP_NSEGS, slim, 0,
1747             &pcs->dmat)) {
1748                 isp_prt(isp, ISP_LOGERR, "could not create master dma tag");
1749                 ISP_LOCK(isp);
1750                 return (1);
1751         }
1752
1753
1754         len = sizeof (XS_T **) * isp->isp_maxcmds;
1755         isp->isp_xflist = (XS_T **) malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
1756         if (isp->isp_xflist == NULL) {
1757                 isp_prt(isp, ISP_LOGERR, "cannot alloc xflist array");
1758                 ISP_LOCK(isp);
1759                 return (1);
1760         }
1761 #ifdef  ISP_TARGET_MODE
1762         len = sizeof (void **) * isp->isp_maxcmds;
1763         isp->isp_tgtlist = (void **) malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
1764         if (isp->isp_tgtlist == NULL) {
1765                 isp_prt(isp, ISP_LOGERR, "cannot alloc tgtlist array");
1766                 ISP_LOCK(isp);
1767                 return (1);
1768         }
1769 #endif
1770         len = sizeof (bus_dmamap_t) * isp->isp_maxcmds;
1771         pcs->dmaps = (bus_dmamap_t *) malloc(len, M_DEVBUF,  M_WAITOK);
1772         if (pcs->dmaps == NULL) {
1773                 isp_prt(isp, ISP_LOGERR, "can't alloc dma map storage");
1774                 free(isp->isp_xflist, M_DEVBUF);
1775 #ifdef  ISP_TARGET_MODE
1776                 free(isp->isp_tgtlist, M_DEVBUF);
1777 #endif
1778                 ISP_LOCK(isp);
1779                 return (1);
1780         }
1781
1782         /*
1783          * Allocate and map the request, result queues, plus FC scratch area.
1784          */
1785         len = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
1786         len += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
1787         if (IS_FC(isp)) {
1788                 len += ISP2100_SCRLEN;
1789         }
1790
1791         ns = (len / PAGE_SIZE) + 1;
1792         /*
1793          * Create a tag for the control spaces- force it to within 32 bits.
1794          */
1795         if (isp_dma_tag_create(pcs->dmat, QENTRY_LEN, slim,
1796             BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
1797             NULL, NULL, len, ns, slim, 0, &isp->isp_cdmat)) {
1798                 isp_prt(isp, ISP_LOGERR,
1799                     "cannot create a dma tag for control spaces");
1800                 free(pcs->dmaps, M_DEVBUF);
1801                 free(isp->isp_xflist, M_DEVBUF);
1802 #ifdef  ISP_TARGET_MODE
1803                 free(isp->isp_tgtlist, M_DEVBUF);
1804 #endif
1805                 ISP_LOCK(isp);
1806                 return (1);
1807         }
1808
1809         if (bus_dmamem_alloc(isp->isp_cdmat, (void **)&base, BUS_DMA_NOWAIT,
1810             &isp->isp_cdmap) != 0) {
1811                 isp_prt(isp, ISP_LOGERR,
1812                     "cannot allocate %d bytes of CCB memory", len);
1813                 bus_dma_tag_destroy(isp->isp_cdmat);
1814                 free(isp->isp_xflist, M_DEVBUF);
1815 #ifdef  ISP_TARGET_MODE
1816                 free(isp->isp_tgtlist, M_DEVBUF);
1817 #endif
1818                 free(pcs->dmaps, M_DEVBUF);
1819                 ISP_LOCK(isp);
1820                 return (1);
1821         }
1822
1823         for (i = 0; i < isp->isp_maxcmds; i++) {
1824                 error = bus_dmamap_create(pcs->dmat, 0, &pcs->dmaps[i]);
1825                 if (error) {
1826                         isp_prt(isp, ISP_LOGERR,
1827                             "error %d creating per-cmd DMA maps", error);
1828                         while (--i >= 0) {
1829                                 bus_dmamap_destroy(pcs->dmat, pcs->dmaps[i]);
1830                         }
1831                         goto bad;
1832                 }
1833         }
1834
1835         im.isp = isp;
1836         im.error = 0;
1837         bus_dmamap_load(isp->isp_cdmat, isp->isp_cdmap, base, len, imc, &im, 0);
1838         if (im.error) {
1839                 isp_prt(isp, ISP_LOGERR,
1840                     "error %d loading dma map for control areas", im.error);
1841                 goto bad;
1842         }
1843
1844         isp->isp_rquest = base;
1845         base += ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
1846         isp->isp_result = base;
1847         if (IS_FC(isp)) {
1848                 base += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
1849                 FCPARAM(isp)->isp_scratch = base;
1850         }
1851         ISP_LOCK(isp);
1852         return (0);
1853
1854 bad:
1855         bus_dmamem_free(isp->isp_cdmat, base, isp->isp_cdmap);
1856         bus_dma_tag_destroy(isp->isp_cdmat);
1857         free(isp->isp_xflist, M_DEVBUF);
1858 #ifdef  ISP_TARGET_MODE
1859         free(isp->isp_tgtlist, M_DEVBUF);
1860 #endif
1861         free(pcs->dmaps, M_DEVBUF);
1862         ISP_LOCK(isp);
1863         isp->isp_rquest = NULL;
1864         return (1);
1865 }
1866
1867 typedef struct {
1868         ispsoftc_t *isp;
1869         void *cmd_token;
1870         void *rq;
1871         uint32_t *nxtip;
1872         uint32_t optr;
1873         int error;
1874 } mush_t;
1875
1876 #define MUSHERR_NOQENTRIES      -2
1877
1878 #ifdef  ISP_TARGET_MODE
1879 /*
1880  * We need to handle DMA for target mode differently from initiator mode.
1881  * 
1882  * DMA mapping and construction and submission of CTIO Request Entries
1883  * and rendevous for completion are very tightly coupled because we start
1884  * out by knowing (per platform) how much data we have to move, but we
1885  * don't know, up front, how many DMA mapping segments will have to be used
1886  * cover that data, so we don't know how many CTIO Request Entries we
1887  * will end up using. Further, for performance reasons we may want to
1888  * (on the last CTIO for Fibre Channel), send status too (if all went well).
1889  *
1890  * The standard vector still goes through isp_pci_dmasetup, but the callback
1891  * for the DMA mapping routines comes here instead with the whole transfer
1892  * mapped and a pointer to a partially filled in already allocated request
1893  * queue entry. We finish the job.
1894  */
1895 static void tdma_mk(void *, bus_dma_segment_t *, int, int);
1896 static void tdma_mkfc(void *, bus_dma_segment_t *, int, int);
1897
1898 #define STATUS_WITH_DATA        1
1899
1900 static void
1901 tdma_mk(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1902 {
1903         mush_t *mp;
1904         struct ccb_scsiio *csio;
1905         ispsoftc_t *isp;
1906         struct isp_pcisoftc *pcs;
1907         bus_dmamap_t *dp;
1908         ct_entry_t *cto, *qe;
1909         uint8_t scsi_status;
1910         uint32_t curi, nxti, handle;
1911         uint32_t sflags;
1912         int32_t resid;
1913         int nth_ctio, nctios, send_status;
1914
1915         mp = (mush_t *) arg;
1916         if (error) {
1917                 mp->error = error;
1918                 return;
1919         }
1920
1921         isp = mp->isp;
1922         csio = mp->cmd_token;
1923         cto = mp->rq;
1924         curi = isp->isp_reqidx;
1925         qe = (ct_entry_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, curi);
1926
1927         cto->ct_xfrlen = 0;
1928         cto->ct_seg_count = 0;
1929         cto->ct_header.rqs_entry_count = 1;
1930         MEMZERO(cto->ct_dataseg, sizeof(cto->ct_dataseg));
1931
1932         if (nseg == 0) {
1933                 cto->ct_header.rqs_seqno = 1;
1934                 isp_prt(isp, ISP_LOGTDEBUG1,
1935                     "CTIO[%x] lun%d iid%d tag %x flgs %x sts %x ssts %x res %d",
1936                     cto->ct_fwhandle, csio->ccb_h.target_lun, cto->ct_iid,
1937                     cto->ct_tag_val, cto->ct_flags, cto->ct_status,
1938                     cto->ct_scsi_status, cto->ct_resid);
1939                 ISP_TDQE(isp, "tdma_mk[no data]", curi, cto);
1940                 isp_put_ctio(isp, cto, qe);
1941                 return;
1942         }
1943
1944         nctios = nseg / ISP_RQDSEG;
1945         if (nseg % ISP_RQDSEG) {
1946                 nctios++;
1947         }
1948
1949         /*
1950          * Save syshandle, and potentially any SCSI status, which we'll
1951          * reinsert on the last CTIO we're going to send.
1952          */
1953
1954         handle = cto->ct_syshandle;
1955         cto->ct_syshandle = 0;
1956         cto->ct_header.rqs_seqno = 0;
1957         send_status = (cto->ct_flags & CT_SENDSTATUS) != 0;
1958
1959         if (send_status) {
1960                 sflags = cto->ct_flags & (CT_SENDSTATUS | CT_CCINCR);
1961                 cto->ct_flags &= ~(CT_SENDSTATUS | CT_CCINCR);
1962                 /*
1963                  * Preserve residual.
1964                  */
1965                 resid = cto->ct_resid;
1966
1967                 /*
1968                  * Save actual SCSI status.
1969                  */
1970                 scsi_status = cto->ct_scsi_status;
1971
1972 #ifndef STATUS_WITH_DATA
1973                 sflags |= CT_NO_DATA;
1974                 /*
1975                  * We can't do a status at the same time as a data CTIO, so
1976                  * we need to synthesize an extra CTIO at this level.
1977                  */
1978                 nctios++;
1979 #endif
1980         } else {
1981                 sflags = scsi_status = resid = 0;
1982         }
1983
1984         cto->ct_resid = 0;
1985         cto->ct_scsi_status = 0;
1986
1987         pcs = (struct isp_pcisoftc *)isp;
1988         dp = &pcs->dmaps[isp_handle_index(handle)];
1989         if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1990                 bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_PREREAD);
1991         } else {
1992                 bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_PREWRITE);
1993         }
1994
1995         nxti = *mp->nxtip;
1996
1997         for (nth_ctio = 0; nth_ctio < nctios; nth_ctio++) {
1998                 int seglim;
1999
2000                 seglim = nseg;
2001                 if (seglim) {
2002                         int seg;
2003
2004                         if (seglim > ISP_RQDSEG)
2005                                 seglim = ISP_RQDSEG;
2006
2007                         for (seg = 0; seg < seglim; seg++, nseg--) {
2008                                 /*
2009                                  * Unlike normal initiator commands, we don't
2010                                  * do any swizzling here.
2011                                  */
2012                                 cto->ct_dataseg[seg].ds_count = dm_segs->ds_len;
2013                                 cto->ct_dataseg[seg].ds_base = dm_segs->ds_addr;
2014                                 cto->ct_xfrlen += dm_segs->ds_len;
2015                                 dm_segs++;
2016                         }
2017                         cto->ct_seg_count = seg;
2018                 } else {
2019                         /*
2020                          * This case should only happen when we're sending an
2021                          * extra CTIO with final status.
2022                          */
2023                         if (send_status == 0) {
2024                                 isp_prt(isp, ISP_LOGWARN,
2025                                     "tdma_mk ran out of segments");
2026                                 mp->error = EINVAL;
2027                                 return;
2028                         }
2029                 }
2030
2031                 /*
2032                  * At this point, the fields ct_lun, ct_iid, ct_tagval,
2033                  * ct_tagtype, and ct_timeout have been carried over
2034                  * unchanged from what our caller had set.
2035                  * 
2036                  * The dataseg fields and the seg_count fields we just got
2037                  * through setting. The data direction we've preserved all
2038                  * along and only clear it if we're now sending status.
2039                  */
2040
2041                 if (nth_ctio == nctios - 1) {
2042                         /*
2043                          * We're the last in a sequence of CTIOs, so mark
2044                          * this CTIO and save the handle to the CCB such that
2045                          * when this CTIO completes we can free dma resources
2046                          * and do whatever else we need to do to finish the
2047                          * rest of the command. We *don't* give this to the
2048                          * firmware to work on- the caller will do that.
2049                          */
2050
2051                         cto->ct_syshandle = handle;
2052                         cto->ct_header.rqs_seqno = 1;
2053
2054                         if (send_status) {
2055                                 cto->ct_scsi_status = scsi_status;
2056                                 cto->ct_flags |= sflags;
2057                                 cto->ct_resid = resid;
2058                         }
2059                         if (send_status) {
2060                                 isp_prt(isp, ISP_LOGTDEBUG1,
2061                                     "CTIO[%x] lun%d iid %d tag %x ct_flags %x "
2062                                     "scsi status %x resid %d",
2063                                     cto->ct_fwhandle, csio->ccb_h.target_lun,
2064                                     cto->ct_iid, cto->ct_tag_val, cto->ct_flags,
2065                                     cto->ct_scsi_status, cto->ct_resid);
2066                         } else {
2067                                 isp_prt(isp, ISP_LOGTDEBUG1,
2068                                     "CTIO[%x] lun%d iid%d tag %x ct_flags 0x%x",
2069                                     cto->ct_fwhandle, csio->ccb_h.target_lun,
2070                                     cto->ct_iid, cto->ct_tag_val,
2071                                     cto->ct_flags);
2072                         }
2073                         isp_put_ctio(isp, cto, qe);
2074                         ISP_TDQE(isp, "last tdma_mk", curi, cto);
2075                         if (nctios > 1) {
2076                                 MEMORYBARRIER(isp, SYNC_REQUEST,
2077                                     curi, QENTRY_LEN);
2078                         }
2079                 } else {
2080                         ct_entry_t *oqe = qe;
2081
2082                         /*
2083                          * Make sure syshandle fields are clean
2084                          */
2085                         cto->ct_syshandle = 0;
2086                         cto->ct_header.rqs_seqno = 0;
2087
2088                         isp_prt(isp, ISP_LOGTDEBUG1,
2089                             "CTIO[%x] lun%d for ID%d ct_flags 0x%x",
2090                             cto->ct_fwhandle, csio->ccb_h.target_lun,
2091                             cto->ct_iid, cto->ct_flags);
2092
2093                         /*
2094                          * Get a new CTIO
2095                          */
2096                         qe = (ct_entry_t *)
2097                             ISP_QUEUE_ENTRY(isp->isp_rquest, nxti);
2098                         nxti = ISP_NXT_QENTRY(nxti, RQUEST_QUEUE_LEN(isp));
2099                         if (nxti == mp->optr) {
2100                                 isp_prt(isp, ISP_LOGTDEBUG0,
2101                                     "Queue Overflow in tdma_mk");
2102                                 mp->error = MUSHERR_NOQENTRIES;
2103                                 return;
2104                         }
2105
2106                         /*
2107                          * Now that we're done with the old CTIO,
2108                          * flush it out to the request queue.
2109                          */
2110                         ISP_TDQE(isp, "dma_tgt_fc", curi, cto);
2111                         isp_put_ctio(isp, cto, oqe);
2112                         if (nth_ctio != 0) {
2113                                 MEMORYBARRIER(isp, SYNC_REQUEST, curi,
2114                                     QENTRY_LEN);
2115                         }
2116                         curi = ISP_NXT_QENTRY(curi, RQUEST_QUEUE_LEN(isp));
2117
2118                         /*
2119                          * Reset some fields in the CTIO so we can reuse
2120                          * for the next one we'll flush to the request
2121                          * queue.
2122                          */
2123                         cto->ct_header.rqs_entry_type = RQSTYPE_CTIO;
2124                         cto->ct_header.rqs_entry_count = 1;
2125                         cto->ct_header.rqs_flags = 0;
2126                         cto->ct_status = 0;
2127                         cto->ct_scsi_status = 0;
2128                         cto->ct_xfrlen = 0;
2129                         cto->ct_resid = 0;
2130                         cto->ct_seg_count = 0;
2131                         MEMZERO(cto->ct_dataseg, sizeof(cto->ct_dataseg));
2132                 }
2133         }
2134         *mp->nxtip = nxti;
2135 }
2136
2137 /*
2138  * We don't have to do multiple CTIOs here. Instead, we can just do
2139  * continuation segments as needed. This greatly simplifies the code
2140  * improves performance.
2141  */
2142
2143 static void
2144 tdma_mkfc(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
2145 {
2146         mush_t *mp;
2147         struct ccb_scsiio *csio;
2148         ispsoftc_t *isp;
2149         ct2_entry_t *cto, *qe;
2150         uint32_t curi, nxti;
2151         ispds_t *ds;
2152         ispds64_t *ds64;
2153         int segcnt, seglim;
2154
2155         mp = (mush_t *) arg;
2156         if (error) {
2157                 mp->error = error;
2158                 return;
2159         }
2160
2161         isp = mp->isp;
2162         csio = mp->cmd_token;
2163         cto = mp->rq;
2164
2165         curi = isp->isp_reqidx;
2166         qe = (ct2_entry_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, curi);
2167
2168         if (nseg == 0) {
2169                 if ((cto->ct_flags & CT2_FLAG_MMASK) != CT2_FLAG_MODE1) {
2170                         isp_prt(isp, ISP_LOGWARN,
2171                             "dma2_tgt_fc, a status CTIO2 without MODE1 "
2172                             "set (0x%x)", cto->ct_flags);
2173                         mp->error = EINVAL;
2174                         return;
2175                 }
2176                 /*
2177                  * We preserve ct_lun, ct_iid, ct_rxid. We set the data
2178                  * flags to NO DATA and clear relative offset flags.
2179                  * We preserve the ct_resid and the response area.
2180                  */
2181                 cto->ct_header.rqs_seqno = 1;
2182                 cto->ct_seg_count = 0;
2183                 cto->ct_reloff = 0;
2184                 isp_prt(isp, ISP_LOGTDEBUG1,
2185                     "CTIO2[%x] lun %d->iid%d flgs 0x%x sts 0x%x ssts "
2186                     "0x%x res %d", cto->ct_rxid, csio->ccb_h.target_lun,
2187                     cto->ct_iid, cto->ct_flags, cto->ct_status,
2188                     cto->rsp.m1.ct_scsi_status, cto->ct_resid);
2189                 if (FCPARAM(isp)->isp_2klogin) {
2190                         isp_put_ctio2e(isp,
2191                             (ct2e_entry_t *)cto, (ct2e_entry_t *)qe);
2192                 } else {
2193                         isp_put_ctio2(isp, cto, qe);
2194                 }
2195                 ISP_TDQE(isp, "dma2_tgt_fc[no data]", curi, qe);
2196                 return;
2197         }
2198
2199         if ((cto->ct_flags & CT2_FLAG_MMASK) != CT2_FLAG_MODE0) {
2200                 isp_prt(isp, ISP_LOGERR,
2201                     "dma2_tgt_fc, a data CTIO2 without MODE0 set "
2202                     "(0x%x)", cto->ct_flags);
2203                 mp->error = EINVAL;
2204                 return;
2205         }
2206
2207
2208         nxti = *mp->nxtip;
2209
2210         /*
2211          * Check to see if we need to DAC addressing or not.
2212          *
2213          * Any address that's over the 4GB boundary causes this
2214          * to happen.
2215          */
2216         segcnt = nseg;
2217         if (sizeof (bus_addr_t) > 4) {
2218                 for (segcnt = 0; segcnt < nseg; segcnt++) {
2219                         uint64_t addr = dm_segs[segcnt].ds_addr;
2220                         if (addr >= 0x100000000LL) {
2221                                 break;
2222                         }
2223                 }
2224         }
2225         if (segcnt != nseg) {
2226                 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO3;
2227                 seglim = ISP_RQDSEG_T3;
2228                 ds64 = &cto->rsp.m0.u.ct_dataseg64[0];
2229                 ds = NULL;
2230         } else {
2231                 seglim = ISP_RQDSEG_T2;
2232                 ds64 = NULL;
2233                 ds = &cto->rsp.m0.u.ct_dataseg[0];
2234         }
2235         cto->ct_seg_count = 0;
2236
2237         /*
2238          * Set up the CTIO2 data segments.
2239          */
2240         for (segcnt = 0; cto->ct_seg_count < seglim && segcnt < nseg;
2241             cto->ct_seg_count++, segcnt++) {
2242                 if (ds64) {
2243                         ds64->ds_basehi =
2244                             ((uint64_t) (dm_segs[segcnt].ds_addr) >> 32);
2245                         ds64->ds_base = dm_segs[segcnt].ds_addr;
2246                         ds64->ds_count = dm_segs[segcnt].ds_len;
2247                         ds64++;
2248                 } else {
2249                         ds->ds_base = dm_segs[segcnt].ds_addr;
2250                         ds->ds_count = dm_segs[segcnt].ds_len;
2251                         ds++;
2252                 }
2253                 cto->rsp.m0.ct_xfrlen += dm_segs[segcnt].ds_len;
2254 #if __FreeBSD_version < 500000  
2255                 isp_prt(isp, ISP_LOGTDEBUG1,
2256                     "isp_send_ctio2: ent0[%d]0x%llx:%llu",
2257                     cto->ct_seg_count, (uint64_t)dm_segs[segcnt].ds_addr,
2258                     (uint64_t)dm_segs[segcnt].ds_len);
2259 #else
2260                 isp_prt(isp, ISP_LOGTDEBUG1,
2261                     "isp_send_ctio2: ent0[%d]0x%jx:%ju",
2262                     cto->ct_seg_count, (uintmax_t)dm_segs[segcnt].ds_addr,
2263                     (uintmax_t)dm_segs[segcnt].ds_len);
2264 #endif
2265         }
2266
2267         while (segcnt < nseg) {
2268                 uint32_t curip;
2269                 int seg;
2270                 ispcontreq_t local, *crq = &local, *qep;
2271
2272                 qep = (ispcontreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, nxti);
2273                 curip = nxti;
2274                 nxti = ISP_NXT_QENTRY(curip, RQUEST_QUEUE_LEN(isp));
2275                 if (nxti == mp->optr) {
2276                         ISP_UNLOCK(isp);
2277                         isp_prt(isp, ISP_LOGTDEBUG0,
2278                             "tdma_mkfc: request queue overflow");
2279                         mp->error = MUSHERR_NOQENTRIES;
2280                         return;
2281                 }
2282                 cto->ct_header.rqs_entry_count++;
2283                 MEMZERO((void *)crq, sizeof (*crq));
2284                 crq->req_header.rqs_entry_count = 1;
2285                 if (cto->ct_header.rqs_entry_type == RQSTYPE_CTIO3) {
2286                         seglim = ISP_CDSEG64;
2287                         ds = NULL;
2288                         ds64 = &((ispcontreq64_t *)crq)->req_dataseg[0];
2289                         crq->req_header.rqs_entry_type = RQSTYPE_A64_CONT;
2290                 } else {
2291                         seglim = ISP_CDSEG;
2292                         ds = &crq->req_dataseg[0];
2293                         ds64 = NULL;
2294                         crq->req_header.rqs_entry_type = RQSTYPE_DATASEG;
2295                 }
2296                 for (seg = 0; segcnt < nseg && seg < seglim;
2297                     segcnt++, seg++) {
2298                         if (ds64) {
2299                                 ds64->ds_basehi =
2300                                   ((uint64_t) (dm_segs[segcnt].ds_addr) >> 32);
2301                                 ds64->ds_base = dm_segs[segcnt].ds_addr;
2302                                 ds64->ds_count = dm_segs[segcnt].ds_len;
2303                                 ds64++;
2304                         } else {
2305                                 ds->ds_base = dm_segs[segcnt].ds_addr;
2306                                 ds->ds_count = dm_segs[segcnt].ds_len;
2307                                 ds++;
2308                         }
2309 #if __FreeBSD_version < 500000  
2310                         isp_prt(isp, ISP_LOGTDEBUG1,
2311                             "isp_send_ctio2: ent%d[%d]%llx:%llu",
2312                             cto->ct_header.rqs_entry_count-1, seg,
2313                             (uint64_t)dm_segs[segcnt].ds_addr,
2314                             (uint64_t)dm_segs[segcnt].ds_len);
2315 #else
2316                         isp_prt(isp, ISP_LOGTDEBUG1,
2317                             "isp_send_ctio2: ent%d[%d]%jx:%ju",
2318                             cto->ct_header.rqs_entry_count-1, seg,
2319                             (uintmax_t)dm_segs[segcnt].ds_addr,
2320                             (uintmax_t)dm_segs[segcnt].ds_len);
2321 #endif
2322                         cto->rsp.m0.ct_xfrlen += dm_segs[segcnt].ds_len;
2323                         cto->ct_seg_count++;
2324                 }
2325                 MEMORYBARRIER(isp, SYNC_REQUEST, curip, QENTRY_LEN);
2326                 isp_put_cont_req(isp, crq, qep);
2327                 ISP_TDQE(isp, "cont entry", curi, qep);
2328         }
2329
2330         /*
2331          * No do final twiddling for the CTIO itself.
2332          */
2333         cto->ct_header.rqs_seqno = 1;
2334         isp_prt(isp, ISP_LOGTDEBUG1,
2335             "CTIO2[%x] lun %d->iid%d flgs 0x%x sts 0x%x ssts 0x%x resid %d",
2336             cto->ct_rxid, csio->ccb_h.target_lun, (int) cto->ct_iid,
2337             cto->ct_flags, cto->ct_status, cto->rsp.m1.ct_scsi_status,
2338             cto->ct_resid);
2339         if (FCPARAM(isp)->isp_2klogin) {
2340                 isp_put_ctio2e(isp, (ct2e_entry_t *)cto, (ct2e_entry_t *)qe);
2341         } else {
2342                 isp_put_ctio2(isp, cto, qe);
2343         }
2344         ISP_TDQE(isp, "last dma2_tgt_fc", curi, qe);
2345         *mp->nxtip = nxti;
2346 }
2347 #endif
2348
2349 static void dma_2400(void *, bus_dma_segment_t *, int, int);
2350 static void dma2_a64(void *, bus_dma_segment_t *, int, int);
2351 static void dma2(void *, bus_dma_segment_t *, int, int);
2352
2353 static void
2354 dma_2400(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
2355 {
2356         mush_t *mp;
2357         ispsoftc_t *isp;
2358         struct ccb_scsiio *csio;
2359         struct isp_pcisoftc *pcs;
2360         bus_dmamap_t *dp;
2361         bus_dma_segment_t *eseg;
2362         ispreqt7_t *rq;
2363         int seglim, datalen;
2364         uint32_t nxti;
2365
2366         mp = (mush_t *) arg;
2367         if (error) {
2368                 mp->error = error;
2369                 return;
2370         }
2371
2372         if (nseg < 1) {
2373                 isp_prt(mp->isp, ISP_LOGERR, "bad segment count (%d)", nseg);
2374                 mp->error = EFAULT;
2375                 return;
2376         }
2377
2378         csio = mp->cmd_token;
2379         isp = mp->isp;
2380         rq = mp->rq;
2381         pcs = (struct isp_pcisoftc *)mp->isp;
2382         dp = &pcs->dmaps[isp_handle_index(rq->req_handle)];
2383         nxti = *mp->nxtip;
2384
2385         if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2386                 bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_PREREAD);
2387         } else {
2388                 bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_PREWRITE);
2389         }
2390         datalen = XS_XFRLEN(csio);
2391
2392         /*
2393          * We're passed an initial partially filled in entry that
2394          * has most fields filled in except for data transfer
2395          * related values.
2396          *
2397          * Our job is to fill in the initial request queue entry and
2398          * then to start allocating and filling in continuation entries
2399          * until we've covered the entire transfer.
2400          */
2401
2402         rq->req_header.rqs_entry_type = RQSTYPE_T7RQS;
2403         rq->req_dl = datalen;
2404         if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2405                 rq->req_alen_datadir = 0x2;
2406         } else {
2407                 rq->req_alen_datadir = 0x1;
2408         }
2409
2410         eseg = dm_segs + nseg;
2411
2412         rq->req_dataseg.ds_base = DMA_LO32(dm_segs->ds_addr);
2413         rq->req_dataseg.ds_basehi = DMA_HI32(dm_segs->ds_addr);
2414         rq->req_dataseg.ds_count = dm_segs->ds_len;
2415
2416         datalen -= dm_segs->ds_len;
2417
2418         dm_segs++;
2419         rq->req_seg_count++;
2420
2421         while (datalen > 0 && dm_segs != eseg) {
2422                 uint32_t onxti;
2423                 ispcontreq64_t local, *crq = &local, *cqe;
2424
2425                 cqe = (ispcontreq64_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, nxti);
2426                 onxti = nxti;
2427                 nxti = ISP_NXT_QENTRY(onxti, RQUEST_QUEUE_LEN(isp));
2428                 if (nxti == mp->optr) {
2429                         isp_prt(isp, ISP_LOGDEBUG0, "Request Queue Overflow++");
2430                         mp->error = MUSHERR_NOQENTRIES;
2431                         return;
2432                 }
2433                 rq->req_header.rqs_entry_count++;
2434                 MEMZERO((void *)crq, sizeof (*crq));
2435                 crq->req_header.rqs_entry_count = 1;
2436                 crq->req_header.rqs_entry_type = RQSTYPE_A64_CONT;
2437
2438                 seglim = 0;
2439                 while (datalen > 0 && seglim < ISP_CDSEG64 && dm_segs != eseg) {
2440                         crq->req_dataseg[seglim].ds_base =
2441                             DMA_LO32(dm_segs->ds_addr);
2442                         crq->req_dataseg[seglim].ds_basehi =
2443                             DMA_HI32(dm_segs->ds_addr);
2444                         crq->req_dataseg[seglim].ds_count =
2445                             dm_segs->ds_len;
2446                         rq->req_seg_count++;
2447                         dm_segs++;
2448                         seglim++;
2449                         datalen -= dm_segs->ds_len;
2450                 }
2451                 if (isp->isp_dblev & ISP_LOGDEBUG1) {
2452                         isp_print_bytes(isp, "Continuation", QENTRY_LEN, crq);
2453                 }
2454                 isp_put_cont64_req(isp, crq, cqe);
2455                 MEMORYBARRIER(isp, SYNC_REQUEST, onxti, QENTRY_LEN);
2456         }
2457         *mp->nxtip = nxti;
2458 }
2459
2460 static void
2461 dma2_a64(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
2462 {
2463         mush_t *mp;
2464         ispsoftc_t *isp;
2465         struct ccb_scsiio *csio;
2466         struct isp_pcisoftc *pcs;
2467         bus_dmamap_t *dp;
2468         bus_dma_segment_t *eseg;
2469         ispreq64_t *rq;
2470         int seglim, datalen;
2471         uint32_t nxti;
2472
2473         mp = (mush_t *) arg;
2474         if (error) {
2475                 mp->error = error;
2476                 return;
2477         }
2478
2479         if (nseg < 1) {
2480                 isp_prt(mp->isp, ISP_LOGERR, "bad segment count (%d)", nseg);
2481                 mp->error = EFAULT;
2482                 return;
2483         }
2484         csio = mp->cmd_token;
2485         isp = mp->isp;
2486         rq = mp->rq;
2487         pcs = (struct isp_pcisoftc *)mp->isp;
2488         dp = &pcs->dmaps[isp_handle_index(rq->req_handle)];
2489         nxti = *mp->nxtip;
2490
2491         if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2492                 bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_PREREAD);
2493         } else {
2494                 bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_PREWRITE);
2495         }
2496         datalen = XS_XFRLEN(csio);
2497
2498         /*
2499          * We're passed an initial partially filled in entry that
2500          * has most fields filled in except for data transfer
2501          * related values.
2502          *
2503          * Our job is to fill in the initial request queue entry and
2504          * then to start allocating and filling in continuation entries
2505          * until we've covered the entire transfer.
2506          */
2507
2508         if (IS_FC(isp)) {
2509                 rq->req_header.rqs_entry_type = RQSTYPE_T3RQS;
2510                 seglim = ISP_RQDSEG_T3;
2511                 ((ispreqt3_t *)rq)->req_totalcnt = datalen;
2512                 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2513                         ((ispreqt3_t *)rq)->req_flags |= REQFLAG_DATA_IN;
2514                 } else {
2515                         ((ispreqt3_t *)rq)->req_flags |= REQFLAG_DATA_OUT;
2516                 }
2517         } else {
2518                 rq->req_header.rqs_entry_type = RQSTYPE_A64;
2519                 if (csio->cdb_len > 12) {
2520                         seglim = 0;
2521                 } else {
2522                         seglim = ISP_RQDSEG_A64;
2523                 }
2524                 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2525                         rq->req_flags |= REQFLAG_DATA_IN;
2526                 } else {
2527                         rq->req_flags |= REQFLAG_DATA_OUT;
2528                 }
2529         }
2530
2531         eseg = dm_segs + nseg;
2532
2533         while (datalen != 0 && rq->req_seg_count < seglim && dm_segs != eseg) {
2534                 if (IS_FC(isp)) {
2535                         ispreqt3_t *rq3 = (ispreqt3_t *)rq;
2536                         rq3->req_dataseg[rq3->req_seg_count].ds_base =
2537                             DMA_LO32(dm_segs->ds_addr);
2538                         rq3->req_dataseg[rq3->req_seg_count].ds_basehi =
2539                             DMA_HI32(dm_segs->ds_addr);
2540                         rq3->req_dataseg[rq3->req_seg_count].ds_count =
2541                             dm_segs->ds_len;
2542                 } else {
2543                         rq->req_dataseg[rq->req_seg_count].ds_base =
2544                             DMA_LO32(dm_segs->ds_addr);
2545                         rq->req_dataseg[rq->req_seg_count].ds_basehi =
2546                             DMA_HI32(dm_segs->ds_addr);
2547                         rq->req_dataseg[rq->req_seg_count].ds_count =
2548                             dm_segs->ds_len;
2549                 }
2550                 datalen -= dm_segs->ds_len;
2551                 rq->req_seg_count++;
2552                 dm_segs++;
2553         }
2554
2555         while (datalen > 0 && dm_segs != eseg) {
2556                 uint32_t onxti;
2557                 ispcontreq64_t local, *crq = &local, *cqe;
2558
2559                 cqe = (ispcontreq64_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, nxti);
2560                 onxti = nxti;
2561                 nxti = ISP_NXT_QENTRY(onxti, RQUEST_QUEUE_LEN(isp));
2562                 if (nxti == mp->optr) {
2563                         isp_prt(isp, ISP_LOGDEBUG0, "Request Queue Overflow++");
2564                         mp->error = MUSHERR_NOQENTRIES;
2565                         return;
2566                 }
2567                 rq->req_header.rqs_entry_count++;
2568                 MEMZERO((void *)crq, sizeof (*crq));
2569                 crq->req_header.rqs_entry_count = 1;
2570                 crq->req_header.rqs_entry_type = RQSTYPE_A64_CONT;
2571
2572                 seglim = 0;
2573                 while (datalen > 0 && seglim < ISP_CDSEG64 && dm_segs != eseg) {
2574                         crq->req_dataseg[seglim].ds_base =
2575                             DMA_LO32(dm_segs->ds_addr);
2576                         crq->req_dataseg[seglim].ds_basehi =
2577                             DMA_HI32(dm_segs->ds_addr);
2578                         crq->req_dataseg[seglim].ds_count =
2579                             dm_segs->ds_len;
2580                         rq->req_seg_count++;
2581                         dm_segs++;
2582                         seglim++;
2583                         datalen -= dm_segs->ds_len;
2584                 }
2585                 if (isp->isp_dblev & ISP_LOGDEBUG1) {
2586                         isp_print_bytes(isp, "Continuation", QENTRY_LEN, crq);
2587                 }
2588                 isp_put_cont64_req(isp, crq, cqe);
2589                 MEMORYBARRIER(isp, SYNC_REQUEST, onxti, QENTRY_LEN);
2590         }
2591         *mp->nxtip = nxti;
2592 }
2593
2594 static void
2595 dma2(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
2596 {
2597         mush_t *mp;
2598         ispsoftc_t *isp;
2599         struct ccb_scsiio *csio;
2600         struct isp_pcisoftc *pcs;
2601         bus_dmamap_t *dp;
2602         bus_dma_segment_t *eseg;
2603         ispreq_t *rq;
2604         int seglim, datalen;
2605         uint32_t nxti;
2606
2607         mp = (mush_t *) arg;
2608         if (error) {
2609                 mp->error = error;
2610                 return;
2611         }
2612
2613         if (nseg < 1) {
2614                 isp_prt(mp->isp, ISP_LOGERR, "bad segment count (%d)", nseg);
2615                 mp->error = EFAULT;
2616                 return;
2617         }
2618         csio = mp->cmd_token;
2619         isp = mp->isp;
2620         rq = mp->rq;
2621         pcs = (struct isp_pcisoftc *)mp->isp;
2622         dp = &pcs->dmaps[isp_handle_index(rq->req_handle)];
2623         nxti = *mp->nxtip;
2624
2625         if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2626                 bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_PREREAD);
2627         } else {
2628                 bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_PREWRITE);
2629         }
2630
2631         datalen = XS_XFRLEN(csio);
2632
2633         /*
2634          * We're passed an initial partially filled in entry that
2635          * has most fields filled in except for data transfer
2636          * related values.
2637          *
2638          * Our job is to fill in the initial request queue entry and
2639          * then to start allocating and filling in continuation entries
2640          * until we've covered the entire transfer.
2641          */
2642
2643         if (IS_FC(isp)) {
2644                 seglim = ISP_RQDSEG_T2;
2645                 ((ispreqt2_t *)rq)->req_totalcnt = datalen;
2646                 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2647                         ((ispreqt2_t *)rq)->req_flags |= REQFLAG_DATA_IN;
2648                 } else {
2649                         ((ispreqt2_t *)rq)->req_flags |= REQFLAG_DATA_OUT;
2650                 }
2651         } else {
2652                 if (csio->cdb_len > 12) {
2653                         seglim = 0;
2654                 } else {
2655                         seglim = ISP_RQDSEG;
2656                 }
2657                 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2658                         rq->req_flags |= REQFLAG_DATA_IN;
2659                 } else {
2660                         rq->req_flags |= REQFLAG_DATA_OUT;
2661                 }
2662         }
2663
2664         eseg = dm_segs + nseg;
2665
2666         while (datalen != 0 && rq->req_seg_count < seglim && dm_segs != eseg) {
2667                 if (IS_FC(isp)) {
2668                         ispreqt2_t *rq2 = (ispreqt2_t *)rq;
2669                         rq2->req_dataseg[rq2->req_seg_count].ds_base =
2670                             DMA_LO32(dm_segs->ds_addr);
2671                         rq2->req_dataseg[rq2->req_seg_count].ds_count =
2672                             dm_segs->ds_len;
2673                 } else {
2674                         rq->req_dataseg[rq->req_seg_count].ds_base =
2675                                 DMA_LO32(dm_segs->ds_addr);
2676                         rq->req_dataseg[rq->req_seg_count].ds_count =
2677                                 dm_segs->ds_len;
2678                 }
2679                 datalen -= dm_segs->ds_len;
2680                 rq->req_seg_count++;
2681                 dm_segs++;
2682         }
2683
2684         while (datalen > 0 && dm_segs != eseg) {
2685                 uint32_t onxti;
2686                 ispcontreq_t local, *crq = &local, *cqe;
2687
2688                 cqe = (ispcontreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, nxti);
2689                 onxti = nxti;
2690                 nxti = ISP_NXT_QENTRY(onxti, RQUEST_QUEUE_LEN(isp));
2691                 if (nxti == mp->optr) {
2692                         isp_prt(isp, ISP_LOGDEBUG0, "Request Queue Overflow++");
2693                         mp->error = MUSHERR_NOQENTRIES;
2694                         return;
2695                 }
2696                 rq->req_header.rqs_entry_count++;
2697                 MEMZERO((void *)crq, sizeof (*crq));
2698                 crq->req_header.rqs_entry_count = 1;
2699                 crq->req_header.rqs_entry_type = RQSTYPE_DATASEG;
2700
2701                 seglim = 0;
2702                 while (datalen > 0 && seglim < ISP_CDSEG && dm_segs != eseg) {
2703                         crq->req_dataseg[seglim].ds_base =
2704                             DMA_LO32(dm_segs->ds_addr);
2705                         crq->req_dataseg[seglim].ds_count =
2706                             dm_segs->ds_len;
2707                         rq->req_seg_count++;
2708                         dm_segs++;
2709                         seglim++;
2710                         datalen -= dm_segs->ds_len;
2711                 }
2712                 if (isp->isp_dblev & ISP_LOGDEBUG1) {
2713                         isp_print_bytes(isp, "Continuation", QENTRY_LEN, crq);
2714                 }
2715                 isp_put_cont_req(isp, crq, cqe);
2716                 MEMORYBARRIER(isp, SYNC_REQUEST, onxti, QENTRY_LEN);
2717         }
2718         *mp->nxtip = nxti;
2719 }
2720
2721 /*
2722  * We enter with ISP_LOCK held
2723  */
2724 static int
2725 isp_pci_dmasetup(ispsoftc_t *isp, struct ccb_scsiio *csio, ispreq_t *rq,
2726         uint32_t *nxtip, uint32_t optr)
2727 {
2728         struct isp_pcisoftc *pcs = (struct isp_pcisoftc *)isp;
2729         ispreq_t *qep;
2730         bus_dmamap_t *dp = NULL;
2731         mush_t mush, *mp;
2732         void (*eptr)(void *, bus_dma_segment_t *, int, int);
2733
2734         qep = (ispreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, isp->isp_reqidx);
2735 #ifdef  ISP_TARGET_MODE
2736         if (csio->ccb_h.func_code == XPT_CONT_TARGET_IO) {
2737                 if (IS_FC(isp)) {
2738                         eptr = tdma_mkfc;
2739                 } else {
2740                         eptr = tdma_mk;
2741                 }
2742                 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE ||
2743                     (csio->dxfer_len == 0)) {
2744                         mp = &mush;
2745                         mp->isp = isp;
2746                         mp->cmd_token = csio;
2747                         mp->rq = rq;    /* really a ct_entry_t or ct2_entry_t */
2748                         mp->nxtip = nxtip;
2749                         mp->optr = optr;
2750                         mp->error = 0;
2751                         ISPLOCK_2_CAMLOCK(isp);
2752                         (*eptr)(mp, NULL, 0, 0);
2753                         CAMLOCK_2_ISPLOCK(isp);
2754                         goto mbxsync;
2755                 }
2756         } else
2757 #endif
2758         if (IS_24XX(isp)) {
2759                 eptr = dma_2400;
2760         } else if (sizeof (bus_addr_t) > 4) {
2761                 eptr = dma2_a64;
2762         } else {
2763                 eptr = dma2;
2764         }
2765
2766
2767         if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE ||
2768             (csio->dxfer_len == 0)) {
2769                 rq->req_seg_count = 1;
2770                 goto mbxsync;
2771         }
2772
2773         /*
2774          * Do a virtual grapevine step to collect info for
2775          * the callback dma allocation that we have to use...
2776          */
2777         mp = &mush;
2778         mp->isp = isp;
2779         mp->cmd_token = csio;
2780         mp->rq = rq;
2781         mp->nxtip = nxtip;
2782         mp->optr = optr;
2783         mp->error = 0;
2784
2785         ISPLOCK_2_CAMLOCK(isp);
2786         if ((csio->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
2787                 if ((csio->ccb_h.flags & CAM_DATA_PHYS) == 0) {
2788                         int error, s;
2789                         dp = &pcs->dmaps[isp_handle_index(rq->req_handle)];
2790                         s = splsoftvm();
2791                         error = bus_dmamap_load(pcs->dmat, *dp,
2792                             csio->data_ptr, csio->dxfer_len, eptr, mp, 0);
2793                         if (error == EINPROGRESS) {
2794                                 bus_dmamap_unload(pcs->dmat, *dp);
2795                                 mp->error = EINVAL;
2796                                 isp_prt(isp, ISP_LOGERR,
2797                                     "deferred dma allocation not supported");
2798                         } else if (error && mp->error == 0) {
2799 #ifdef  DIAGNOSTIC
2800                                 isp_prt(isp, ISP_LOGERR,
2801                                     "error %d in dma mapping code", error);
2802 #endif
2803                                 mp->error = error;
2804                         }
2805                         splx(s);
2806                 } else {
2807                         /* Pointer to physical buffer */
2808                         struct bus_dma_segment seg;
2809                         seg.ds_addr = (bus_addr_t)(vm_offset_t)csio->data_ptr;
2810                         seg.ds_len = csio->dxfer_len;
2811                         (*eptr)(mp, &seg, 1, 0);
2812                 }
2813         } else {
2814                 struct bus_dma_segment *segs;
2815
2816                 if ((csio->ccb_h.flags & CAM_DATA_PHYS) != 0) {
2817                         isp_prt(isp, ISP_LOGERR,
2818                             "Physical segment pointers unsupported");
2819                         mp->error = EINVAL;
2820                 } else if ((csio->ccb_h.flags & CAM_SG_LIST_PHYS) == 0) {
2821                         isp_prt(isp, ISP_LOGERR,
2822                             "Virtual segment addresses unsupported");
2823                         mp->error = EINVAL;
2824                 } else {
2825                         /* Just use the segments provided */
2826                         segs = (struct bus_dma_segment *) csio->data_ptr;
2827                         (*eptr)(mp, segs, csio->sglist_cnt, 0);
2828                 }
2829         }
2830         CAMLOCK_2_ISPLOCK(isp);
2831         if (mp->error) {
2832                 int retval = CMD_COMPLETE;
2833                 if (mp->error == MUSHERR_NOQENTRIES) {
2834                         retval = CMD_EAGAIN;
2835                 } else if (mp->error == EFBIG) {
2836                         XS_SETERR(csio, CAM_REQ_TOO_BIG);
2837                 } else if (mp->error == EINVAL) {
2838                         XS_SETERR(csio, CAM_REQ_INVALID);
2839                 } else {
2840                         XS_SETERR(csio, CAM_UNREC_HBA_ERROR);
2841                 }
2842                 return (retval);
2843         }
2844 mbxsync:
2845         if (isp->isp_dblev & ISP_LOGDEBUG1) {
2846                 isp_print_bytes(isp, "Request Queue Entry", QENTRY_LEN, rq);
2847         }
2848         switch (rq->req_header.rqs_entry_type) {
2849         case RQSTYPE_REQUEST:
2850                 isp_put_request(isp, rq, qep);
2851                 break;
2852         case RQSTYPE_CMDONLY:
2853                 isp_put_extended_request(isp, (ispextreq_t *)rq,
2854                     (ispextreq_t *)qep);
2855                 break;
2856         case RQSTYPE_T2RQS:
2857                 isp_put_request_t2(isp, (ispreqt2_t *) rq, (ispreqt2_t *) qep);
2858                 break;
2859         case RQSTYPE_A64:
2860         case RQSTYPE_T3RQS:
2861                 isp_put_request_t3(isp, (ispreqt3_t *) rq, (ispreqt3_t *) qep);
2862                 break;
2863         case RQSTYPE_T7RQS:
2864                 isp_put_request_t7(isp, (ispreqt7_t *) rq, (ispreqt7_t *) qep);
2865                 break;
2866         }
2867         return (CMD_QUEUED);
2868 }
2869
2870 static void
2871 isp_pci_dmateardown(ispsoftc_t *isp, XS_T *xs, uint32_t handle)
2872 {
2873         struct isp_pcisoftc *pcs = (struct isp_pcisoftc *)isp;
2874         bus_dmamap_t *dp = &pcs->dmaps[isp_handle_index(handle)];
2875         if ((xs->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2876                 bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_POSTREAD);
2877         } else {
2878                 bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_POSTWRITE);
2879         }
2880         bus_dmamap_unload(pcs->dmat, *dp);
2881 }
2882
2883
2884 static void
2885 isp_pci_reset1(ispsoftc_t *isp)
2886 {
2887         if (!IS_24XX(isp)) {
2888                 /* Make sure the BIOS is disabled */
2889                 isp_pci_wr_reg(isp, HCCR, PCI_HCCR_CMD_BIOS);
2890         }
2891         /* and enable interrupts */
2892         ISP_ENABLE_INTS(isp);
2893 }
2894
2895 static void
2896 isp_pci_dumpregs(ispsoftc_t *isp, const char *msg)
2897 {
2898         struct isp_pcisoftc *pcs = (struct isp_pcisoftc *)isp;
2899         if (msg)
2900                 printf("%s: %s\n", device_get_nameunit(isp->isp_dev), msg);
2901         else
2902                 printf("%s:\n", device_get_nameunit(isp->isp_dev));
2903         if (IS_SCSI(isp))
2904                 printf("    biu_conf1=%x", ISP_READ(isp, BIU_CONF1));
2905         else
2906                 printf("    biu_csr=%x", ISP_READ(isp, BIU2100_CSR));
2907         printf(" biu_icr=%x biu_isr=%x biu_sema=%x ", ISP_READ(isp, BIU_ICR),
2908             ISP_READ(isp, BIU_ISR), ISP_READ(isp, BIU_SEMA));
2909         printf("risc_hccr=%x\n", ISP_READ(isp, HCCR));
2910
2911
2912         if (IS_SCSI(isp)) {
2913                 ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
2914                 printf("    cdma_conf=%x cdma_sts=%x cdma_fifostat=%x\n",
2915                         ISP_READ(isp, CDMA_CONF), ISP_READ(isp, CDMA_STATUS),
2916                         ISP_READ(isp, CDMA_FIFO_STS));
2917                 printf("    ddma_conf=%x ddma_sts=%x ddma_fifostat=%x\n",
2918                         ISP_READ(isp, DDMA_CONF), ISP_READ(isp, DDMA_STATUS),
2919                         ISP_READ(isp, DDMA_FIFO_STS));
2920                 printf("    sxp_int=%x sxp_gross=%x sxp(scsi_ctrl)=%x\n",
2921                         ISP_READ(isp, SXP_INTERRUPT),
2922                         ISP_READ(isp, SXP_GROSS_ERR),
2923                         ISP_READ(isp, SXP_PINS_CTRL));
2924                 ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
2925         }
2926         printf("    mbox regs: %x %x %x %x %x\n",
2927             ISP_READ(isp, OUTMAILBOX0), ISP_READ(isp, OUTMAILBOX1),
2928             ISP_READ(isp, OUTMAILBOX2), ISP_READ(isp, OUTMAILBOX3),
2929             ISP_READ(isp, OUTMAILBOX4));
2930         printf("    PCI Status Command/Status=%x\n",
2931             pci_read_config(pcs->pci_dev, PCIR_COMMAND, 1));
2932 }