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