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