]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/isp/isp_sbus.c
This commit was generated by cvs2svn to compensate for changes in r158795,
[FreeBSD/FreeBSD.git] / sys / dev / isp / isp_sbus.c
1 /*-
2  * PCI specific probe and attach routines for Qlogic ISP SCSI adapters.
3  * FreeBSD Version.
4  *
5  * Copyright (c) 1997-2006 by Matthew Jacob
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice immediately at the beginning of the file, without modification,
13  *    this list of conditions, and the following disclaimer.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/resource.h>
39
40 #include <dev/ofw/ofw_bus.h>
41
42 #include <machine/bus.h>
43 #include <machine/resource.h>
44 #include <sys/rman.h>
45 #include <sparc64/sbus/sbusvar.h>
46
47 #include <dev/isp/isp_freebsd.h>
48
49 static uint16_t isp_sbus_rd_reg(ispsoftc_t *, int);
50 static void isp_sbus_wr_reg(ispsoftc_t *, int, uint16_t);
51 static int
52 isp_sbus_rd_isr(ispsoftc_t *, uint16_t *, uint16_t *, uint16_t *);
53 static int isp_sbus_mbxdma(ispsoftc_t *);
54 static int
55 isp_sbus_dmasetup(ispsoftc_t *, XS_T *, ispreq_t *, uint16_t *, uint16_t);
56 static void
57 isp_sbus_dmateardown(ispsoftc_t *, XS_T *, uint16_t);
58
59 static void isp_sbus_reset1(ispsoftc_t *);
60 static void isp_sbus_dumpregs(ispsoftc_t *, const char *);
61
62 static struct ispmdvec mdvec = {
63         isp_sbus_rd_isr,
64         isp_sbus_rd_reg,
65         isp_sbus_wr_reg,
66         isp_sbus_mbxdma,
67         isp_sbus_dmasetup,
68         isp_sbus_dmateardown,
69         NULL,
70         isp_sbus_reset1,
71         isp_sbus_dumpregs,
72         NULL,
73         BIU_BURST_ENABLE|BIU_PCI_CONF1_FIFO_64
74 };
75
76 static int isp_sbus_probe (device_t);
77 static int isp_sbus_attach (device_t);
78
79
80 struct isp_sbussoftc {
81         ispsoftc_t                      sbus_isp;
82         device_t                        sbus_dev;
83         struct resource *               sbus_reg;
84         bus_space_tag_t                 sbus_st;
85         bus_space_handle_t              sbus_sh;
86         void *                          ih;
87         int16_t                         sbus_poff[_NREG_BLKS];
88         bus_dma_tag_t                   dmat;
89         bus_dmamap_t                    *dmaps;
90         sdparam                         sbus_param;
91         struct ispmdvec                 sbus_mdvec;
92         struct resource *               sbus_ires;
93 };
94
95 extern ispfwfunc *isp_get_firmware_p;
96
97 static device_method_t isp_sbus_methods[] = {
98         /* Device interface */
99         DEVMETHOD(device_probe,         isp_sbus_probe),
100         DEVMETHOD(device_attach,        isp_sbus_attach),
101         { 0, 0 }
102 };
103 static void isp_sbus_intr(void *);
104
105 static driver_t isp_sbus_driver = {
106         "isp", isp_sbus_methods, sizeof (struct isp_sbussoftc)
107 };
108 static devclass_t isp_devclass;
109 DRIVER_MODULE(isp, sbus, isp_sbus_driver, isp_devclass, 0, 0);
110
111 static int
112 isp_sbus_probe(device_t dev)
113 {
114         int found = 0;
115         const char *name = ofw_bus_get_name(dev);
116         if (strcmp(name, "SUNW,isp") == 0 ||
117             strcmp(name, "QLGC,isp") == 0 ||
118             strcmp(name, "ptisp") == 0 ||
119             strcmp(name, "PTI,ptisp") == 0) {
120                 found++;
121         }
122         if (!found)
123                 return (ENXIO);
124         
125         if (isp_announced == 0 && bootverbose) {
126                 printf("Qlogic ISP Driver, FreeBSD Version %d.%d, "
127                     "Core Version %d.%d\n",
128                     ISP_PLATFORM_VERSION_MAJOR, ISP_PLATFORM_VERSION_MINOR,
129                     ISP_CORE_VERSION_MAJOR, ISP_CORE_VERSION_MINOR);
130                 isp_announced++;
131         }
132         return (0);
133 }
134
135 static int
136 isp_sbus_attach(device_t dev)
137 {
138         struct resource *regs;
139         int tval, iqd, isp_debug, role, rid, ispburst;
140         struct isp_sbussoftc *sbs;
141         ispsoftc_t *isp = NULL;
142         int locksetup = 0;
143
144         /*
145          * Figure out if we're supposed to skip this one.
146          * If we are, we actually go to ISP_ROLE_NONE.
147          */
148
149         tval = 0;
150         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
151             "disable", &tval) == 0 && tval) {
152                 device_printf(dev, "device is disabled\n");
153                 /* but return 0 so the !$)$)*!$*) unit isn't reused */
154                 return (0);
155         }
156         
157         role = 0;
158         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
159             "role", &role) == 0 &&
160             ((role & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) == 0)) {
161                 device_printf(dev, "setting role to 0x%x\n", role);
162         } else {
163 #ifdef  ISP_TARGET_MODE
164                 role = ISP_ROLE_INITIATOR|ISP_ROLE_TARGET;
165 #else
166                 role = ISP_DEFAULT_ROLES;
167 #endif
168         }
169
170         sbs = malloc(sizeof (*sbs), M_DEVBUF, M_NOWAIT | M_ZERO);
171         if (sbs == NULL) {
172                 device_printf(dev, "cannot allocate softc\n");
173                 return (ENOMEM);
174         }
175
176         regs = NULL;
177         iqd = 0;
178
179         rid = 0;
180         regs =
181             bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
182         if (regs == 0) {
183                 device_printf(dev, "unable to map registers\n");
184                 goto bad;
185         }
186         sbs->sbus_dev = dev;
187         sbs->sbus_reg = regs;
188         sbs->sbus_st = rman_get_bustag(regs);
189         sbs->sbus_sh = rman_get_bushandle(regs);
190         sbs->sbus_mdvec = mdvec;
191
192         sbs->sbus_poff[BIU_BLOCK >> _BLK_REG_SHFT] = BIU_REGS_OFF;
193         sbs->sbus_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = SBUS_MBOX_REGS_OFF;
194         sbs->sbus_poff[SXP_BLOCK >> _BLK_REG_SHFT] = SBUS_SXP_REGS_OFF;
195         sbs->sbus_poff[RISC_BLOCK >> _BLK_REG_SHFT] = SBUS_RISC_REGS_OFF;
196         sbs->sbus_poff[DMA_BLOCK >> _BLK_REG_SHFT] = DMA_REGS_OFF;
197         isp = &sbs->sbus_isp;
198         isp->isp_mdvec = &sbs->sbus_mdvec;
199         isp->isp_bustype = ISP_BT_SBUS;
200         isp->isp_type = ISP_HA_SCSI_UNKNOWN;
201         isp->isp_param = &sbs->sbus_param;
202         isp->isp_revision = 0;  /* XXX */
203         isp->isp_role = role;
204         isp->isp_dev = dev;
205
206         /*
207          * Get the clock frequency and convert it from HZ to MHz,
208          * rounding up. This defaults to 25MHz if there isn't a
209          * device specific one in the OFW device tree.
210          */
211         sbs->sbus_mdvec.dv_clock = (sbus_get_clockfreq(dev) + 500000)/1000000;
212
213         /*
214          * Now figure out what the proper burst sizes, etc., to use.
215          * Unfortunately, there is no ddi_dma_burstsizes here which
216          * walks up the tree finding the limiting burst size node (if
217          * any). We just use what's here for isp.
218          */
219         ispburst = sbus_get_burstsz(dev);
220         if (ispburst == 0) {
221                 ispburst = SBUS_BURST_32 - 1;
222         }
223         sbs->sbus_mdvec.dv_conf1 =  0;
224         if (ispburst & (1 << 5)) {
225                 sbs->sbus_mdvec.dv_conf1 = BIU_SBUS_CONF1_FIFO_32;
226         } else if (ispburst & (1 << 4)) {
227                 sbs->sbus_mdvec.dv_conf1 = BIU_SBUS_CONF1_FIFO_16;
228         } else if (ispburst & (1 << 3)) {
229                 sbs->sbus_mdvec.dv_conf1 =
230                     BIU_SBUS_CONF1_BURST8 | BIU_SBUS_CONF1_FIFO_8;
231         }
232         if (sbs->sbus_mdvec.dv_conf1) {
233                 sbs->sbus_mdvec.dv_conf1 |= BIU_BURST_ENABLE;
234         }
235
236         /*
237          * Some early versions of the PTI SBus adapter
238          * would fail in trying to download (via poking)
239          * FW. We give up on them.
240          */
241         if (strcmp("PTI,ptisp", ofw_bus_get_name(dev)) == 0 ||
242             strcmp("ptisp", ofw_bus_get_name(dev)) == 0) {
243                 isp->isp_confopts |= ISP_CFG_NORELOAD;
244         }
245
246         /*
247          * We don't trust NVRAM on SBus cards
248          */
249         isp->isp_confopts |= ISP_CFG_NONVRAM;
250
251
252         /*
253          * Try and find firmware for this device.
254          */
255
256         if (isp_get_firmware_p) {
257                 (*isp_get_firmware_p)(0, 0, 0x1000, &sbs->sbus_mdvec.dv_ispfw);
258         }
259
260         iqd = 0;
261         sbs->sbus_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, &iqd,
262             RF_ACTIVE | RF_SHAREABLE);
263         if (sbs->sbus_ires == NULL) {
264                 device_printf(dev, "could not allocate interrupt\n");
265                 goto bad;
266         }
267
268         tval = 0;
269         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
270             "fwload_disable", &tval) == 0 && tval != 0) {
271                 isp->isp_confopts |= ISP_CFG_NORELOAD;
272         }
273
274         isp->isp_osinfo.default_id = -1;
275         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
276             "iid", &tval) == 0) {
277                 isp->isp_osinfo.default_id = tval;
278                 isp->isp_confopts |= ISP_CFG_OWNLOOPID;
279         }
280         if (isp->isp_osinfo.default_id == -1) {
281                 /*
282                  * XXX: should be a way to get properties w/o having
283                  * XXX: to call OF_xxx functions
284                  */
285                 isp->isp_osinfo.default_id = 7;
286         }
287
288         isp_debug = 0;
289         (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
290             "debug", &isp_debug);
291
292         /* Make sure the lock is set up. */
293         mtx_init(&isp->isp_osinfo.lock, "isp", NULL, MTX_DEF);
294         locksetup++;
295
296         if (bus_setup_intr(dev, sbs->sbus_ires, ISP_IFLAGS,
297             isp_sbus_intr, isp, &sbs->ih)) {
298                 device_printf(dev, "could not setup interrupt\n");
299                 goto bad;
300         }
301
302         /*
303          * Set up logging levels.
304          */
305         if (isp_debug) {
306                 isp->isp_dblev = isp_debug;
307         } else {
308                 isp->isp_dblev = ISP_LOGWARN|ISP_LOGERR;
309         }
310         if (bootverbose)
311                 isp->isp_dblev |= ISP_LOGCONFIG|ISP_LOGINFO;
312
313         /*
314          * Make sure we're in reset state.
315          */
316         ISP_LOCK(isp);
317         isp_reset(isp);
318         if (isp->isp_state != ISP_RESETSTATE) {
319                 ISP_UNLOCK(isp);
320                 goto bad;
321         }
322         isp_init(isp);
323         if (isp->isp_role != ISP_ROLE_NONE && isp->isp_state != ISP_INITSTATE) {
324                 isp_uninit(isp);
325                 ISP_UNLOCK(isp);
326                 goto bad;
327         }
328         isp_attach(isp);
329         if (isp->isp_role != ISP_ROLE_NONE && isp->isp_state != ISP_RUNSTATE) {
330                 isp_uninit(isp);
331                 ISP_UNLOCK(isp);
332                 goto bad;
333         }
334         /*
335          * XXXX: Here is where we might unload the f/w module
336          * XXXX: (or decrease the reference count to it).
337          */
338         ISP_UNLOCK(isp);
339         return (0);
340
341 bad:
342
343         if (sbs && sbs->ih) {
344                 (void) bus_teardown_intr(dev, sbs->sbus_ires, sbs->ih);
345         }
346
347         if (locksetup && isp) {
348                 mtx_destroy(&isp->isp_osinfo.lock);
349         }
350
351         if (sbs && sbs->sbus_ires) {
352                 bus_release_resource(dev, SYS_RES_IRQ, iqd, sbs->sbus_ires);
353         }
354
355
356         if (regs) {
357                 (void) bus_release_resource(dev, 0, 0, regs);
358         }
359
360         if (sbs) {
361                 if (sbs->sbus_isp.isp_param)
362                         free(sbs->sbus_isp.isp_param, M_DEVBUF);
363                 free(sbs, M_DEVBUF);
364         }
365
366         /*
367          * XXXX: Here is where we might unload the f/w module
368          * XXXX: (or decrease the reference count to it).
369          */
370         return (ENXIO);
371 }
372
373 static void
374 isp_sbus_intr(void *arg)
375 {
376         ispsoftc_t *isp = arg;
377         uint16_t isr, sema, mbox;
378
379         ISP_LOCK(isp);
380         isp->isp_intcnt++;
381         if (ISP_READ_ISR(isp, &isr, &sema, &mbox) == 0) {
382                 isp->isp_intbogus++;
383         } else {
384                 int iok = isp->isp_osinfo.intsok;
385                 isp->isp_osinfo.intsok = 0;
386                 isp_intr(isp, isr, sema, mbox);
387                 isp->isp_osinfo.intsok = iok;
388         }
389         ISP_UNLOCK(isp);
390 }
391
392 #define IspVirt2Off(a, x)       \
393         (((struct isp_sbussoftc *)a)->sbus_poff[((x) & _BLK_REG_MASK) >> \
394         _BLK_REG_SHFT] + ((x) & 0xff))
395
396 #define BXR2(sbc, off)          \
397         bus_space_read_2(sbc->sbus_st, sbc->sbus_sh, off)
398
399 static int
400 isp_sbus_rd_isr(ispsoftc_t *isp, uint16_t *isrp,
401     uint16_t *semap, uint16_t *mbp)
402 {
403         struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) isp;
404         uint16_t isr, sema;
405
406         isr = BXR2(sbc, IspVirt2Off(isp, BIU_ISR));
407         sema = BXR2(sbc, IspVirt2Off(isp, BIU_SEMA));
408         isp_prt(isp, ISP_LOGDEBUG3, "ISR 0x%x SEMA 0x%x", isr, sema);
409         isr &= INT_PENDING_MASK(isp);
410         sema &= BIU_SEMA_LOCK;
411         if (isr == 0 && sema == 0) {
412                 return (0);
413         }
414         *isrp = isr;
415         if ((*semap = sema) != 0) {
416                 *mbp = BXR2(sbc, IspVirt2Off(isp, OUTMAILBOX0));
417         }
418         return (1);
419 }
420
421 static uint16_t
422 isp_sbus_rd_reg(ispsoftc_t *isp, int regoff)
423 {
424         uint16_t rval;
425         struct isp_sbussoftc *sbs = (struct isp_sbussoftc *) isp;
426         int offset = sbs->sbus_poff[(regoff & _BLK_REG_MASK) >> _BLK_REG_SHFT];
427         offset += (regoff & 0xff);
428         rval = bus_space_read_2(sbs->sbus_st, sbs->sbus_sh, offset);
429         isp_prt(isp, ISP_LOGDEBUG3,
430             "isp_sbus_rd_reg(off %x) = %x", regoff, rval);
431         return (rval);
432 }
433
434 static void
435 isp_sbus_wr_reg(ispsoftc_t *isp, int regoff, uint16_t val)
436 {
437         struct isp_sbussoftc *sbs = (struct isp_sbussoftc *) isp;
438         int offset = sbs->sbus_poff[(regoff & _BLK_REG_MASK) >> _BLK_REG_SHFT];
439         offset += (regoff & 0xff);
440         isp_prt(isp, ISP_LOGDEBUG3,
441             "isp_sbus_wr_reg(off %x) = %x", regoff, val);
442         bus_space_write_2(sbs->sbus_st, sbs->sbus_sh, offset, val);
443 }
444
445 struct imush {
446         ispsoftc_t *isp;
447         int error;
448 };
449
450 static void imc(void *, bus_dma_segment_t *, int, int);
451
452 static void
453 imc(void *arg, bus_dma_segment_t *segs, int nseg, int error)
454 {
455         struct imush *imushp = (struct imush *) arg;
456         if (error) {
457                 imushp->error = error;
458         } else {
459                 ispsoftc_t *isp =imushp->isp;
460                 bus_addr_t addr = segs->ds_addr;
461
462                 isp->isp_rquest_dma = addr;
463                 addr += ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
464                 isp->isp_result_dma = addr;
465         }
466 }
467
468 /*
469  * Should be BUS_SPACE_MAXSIZE, but MAXPHYS is larger than BUS_SPACE_MAXSIZE
470  */
471 #define ISP_NSEGS ((MAXPHYS / PAGE_SIZE) + 1)  
472
473 static int
474 isp_sbus_mbxdma(ispsoftc_t *isp)
475 {
476         struct isp_sbussoftc *sbs = (struct isp_sbussoftc *)isp;
477         caddr_t base;
478         uint32_t len;
479         int i, error, ns;
480         struct imush im;
481
482         /*
483          * Already been here? If so, leave...
484          */
485         if (isp->isp_rquest) {
486                 return (0);
487         }
488
489         ISP_UNLOCK(isp);
490
491         if (bus_dma_tag_create(NULL, 1, BUS_SPACE_MAXADDR_24BIT+1,
492             BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR_32BIT,
493             NULL, NULL, BUS_SPACE_MAXSIZE_32BIT, ISP_NSEGS,
494             BUS_SPACE_MAXADDR_24BIT, 0, busdma_lock_mutex, &Giant,
495             &sbs->dmat)) {
496                 isp_prt(isp, ISP_LOGERR, "could not create master dma tag");
497                 ISP_LOCK(isp);
498                 return(1);
499         }
500
501         len = sizeof (XS_T **) * isp->isp_maxcmds;
502         isp->isp_xflist = (XS_T **) malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
503         if (isp->isp_xflist == NULL) {
504                 isp_prt(isp, ISP_LOGERR, "cannot alloc xflist array");
505                 ISP_LOCK(isp);
506                 return (1);
507         }
508         len = sizeof (bus_dmamap_t) * isp->isp_maxcmds;
509         sbs->dmaps = (bus_dmamap_t *) malloc(len, M_DEVBUF,  M_WAITOK);
510         if (sbs->dmaps == NULL) {
511                 isp_prt(isp, ISP_LOGERR, "can't alloc dma map storage");
512                 free(isp->isp_xflist, M_DEVBUF);
513                 ISP_LOCK(isp);
514                 return (1);
515         }
516
517         /*
518          * Allocate and map the request, result queues, plus FC scratch area.
519          */
520         len = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
521         len += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
522
523         ns = (len / PAGE_SIZE) + 1;
524         if (bus_dma_tag_create(sbs->dmat, QENTRY_LEN, BUS_SPACE_MAXADDR_24BIT+1,
525             BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR_32BIT, NULL, NULL,
526             len, ns, BUS_SPACE_MAXADDR_24BIT, 0, busdma_lock_mutex, &Giant,
527             &isp->isp_cdmat)) {
528                 isp_prt(isp, ISP_LOGERR,
529                     "cannot create a dma tag for control spaces");
530                 free(sbs->dmaps, M_DEVBUF);
531                 free(isp->isp_xflist, M_DEVBUF);
532                 ISP_LOCK(isp);
533                 return (1);
534         }
535
536         if (bus_dmamem_alloc(isp->isp_cdmat, (void **)&base, BUS_DMA_NOWAIT,
537             &isp->isp_cdmap) != 0) {
538                 isp_prt(isp, ISP_LOGERR,
539                     "cannot allocate %d bytes of CCB memory", len);
540                 bus_dma_tag_destroy(isp->isp_cdmat);
541                 free(isp->isp_xflist, M_DEVBUF);
542                 free(sbs->dmaps, M_DEVBUF);
543                 ISP_LOCK(isp);
544                 return (1);
545         }
546
547         for (i = 0; i < isp->isp_maxcmds; i++) {
548                 error = bus_dmamap_create(sbs->dmat, 0, &sbs->dmaps[i]);
549                 if (error) {
550                         isp_prt(isp, ISP_LOGERR,
551                             "error %d creating per-cmd DMA maps", error);
552                         while (--i >= 0) {
553                                 bus_dmamap_destroy(sbs->dmat, sbs->dmaps[i]);
554                         }
555                         goto bad;
556                 }
557         }
558
559         im.isp = isp;
560         im.error = 0;
561         bus_dmamap_load(isp->isp_cdmat, isp->isp_cdmap, base, len, imc, &im, 0);
562         if (im.error) {
563                 isp_prt(isp, ISP_LOGERR,
564                     "error %d loading dma map for control areas", im.error);
565                 goto bad;
566         }
567
568         isp->isp_rquest = base;
569         base += ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
570         ISP_LOCK(isp);
571         isp->isp_result = base;
572         return (0);
573
574 bad:
575         bus_dmamem_free(isp->isp_cdmat, base, isp->isp_cdmap);
576         bus_dma_tag_destroy(isp->isp_cdmat);
577         free(isp->isp_xflist, M_DEVBUF);
578         free(sbs->dmaps, M_DEVBUF);
579         ISP_LOCK(isp);
580         isp->isp_rquest = NULL;
581         return (1);
582 }
583
584 typedef struct {
585         ispsoftc_t *isp;
586         void *cmd_token;
587         void *rq;
588         uint16_t *nxtip;
589         uint16_t optr;
590         int error;
591 } mush_t;
592
593 #define MUSHERR_NOQENTRIES      -2
594
595
596 static void dma2(void *, bus_dma_segment_t *, int, int);
597
598 static void
599 dma2(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
600 {
601         mush_t *mp;
602         ispsoftc_t *isp;
603         struct ccb_scsiio *csio;
604         struct isp_sbussoftc *sbs;
605         bus_dmamap_t *dp;
606         bus_dma_segment_t *eseg;
607         ispreq_t *rq;
608         int seglim, datalen;
609         uint16_t nxti;
610
611         mp = (mush_t *) arg;
612         if (error) {
613                 mp->error = error;
614                 return;
615         }
616
617         if (nseg < 1) {
618                 isp_prt(mp->isp, ISP_LOGERR, "bad segment count (%d)", nseg);
619                 mp->error = EFAULT;
620                 return;
621         }
622         csio = mp->cmd_token;
623         isp = mp->isp;
624         rq = mp->rq;
625         sbs = (struct isp_sbussoftc *)mp->isp;
626         dp = &sbs->dmaps[isp_handle_index(rq->req_handle)];
627         nxti = *mp->nxtip;
628
629         if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
630                 bus_dmamap_sync(sbs->dmat, *dp, BUS_DMASYNC_PREREAD);
631         } else {
632                 bus_dmamap_sync(sbs->dmat, *dp, BUS_DMASYNC_PREWRITE);
633         }
634
635         datalen = XS_XFRLEN(csio);
636
637         /*
638          * We're passed an initial partially filled in entry that
639          * has most fields filled in except for data transfer
640          * related values.
641          *
642          * Our job is to fill in the initial request queue entry and
643          * then to start allocating and filling in continuation entries
644          * until we've covered the entire transfer.
645          */
646
647         if (csio->cdb_len > 12) {
648                 seglim = 0;
649         } else {
650                 seglim = ISP_RQDSEG;
651         }
652         if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
653                 rq->req_flags |= REQFLAG_DATA_IN;
654         } else {
655                 rq->req_flags |= REQFLAG_DATA_OUT;
656         }
657
658         eseg = dm_segs + nseg;
659
660         while (datalen != 0 && rq->req_seg_count < seglim && dm_segs != eseg) {
661                 rq->req_dataseg[rq->req_seg_count].ds_base = dm_segs->ds_addr;
662                 rq->req_dataseg[rq->req_seg_count].ds_count = dm_segs->ds_len;
663                 datalen -= dm_segs->ds_len;
664                 rq->req_seg_count++;
665                 dm_segs++;
666         }
667
668         while (datalen > 0 && dm_segs != eseg) {
669                 uint16_t onxti;
670                 ispcontreq_t local, *crq = &local, *cqe;
671
672                 cqe = (ispcontreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, nxti);
673                 onxti = nxti;
674                 nxti = ISP_NXT_QENTRY(onxti, RQUEST_QUEUE_LEN(isp));
675                 if (nxti == mp->optr) {
676                         isp_prt(isp, ISP_LOGDEBUG0, "Request Queue Overflow++");
677                         mp->error = MUSHERR_NOQENTRIES;
678                         return;
679                 }
680                 rq->req_header.rqs_entry_count++;
681                 MEMZERO((void *)crq, sizeof (*crq));
682                 crq->req_header.rqs_entry_count = 1;
683                 crq->req_header.rqs_entry_type = RQSTYPE_DATASEG;
684
685                 seglim = 0;
686                 while (datalen > 0 && seglim < ISP_CDSEG && dm_segs != eseg) {
687                         crq->req_dataseg[seglim].ds_base =
688                             dm_segs->ds_addr;
689                         crq->req_dataseg[seglim].ds_count =
690                             dm_segs->ds_len;
691                         rq->req_seg_count++;
692                         dm_segs++;
693                         seglim++;
694                         datalen -= dm_segs->ds_len;
695                 }
696                 isp_put_cont_req(isp, crq, cqe);
697                 MEMORYBARRIER(isp, SYNC_REQUEST, onxti, QENTRY_LEN);
698         }
699         *mp->nxtip = nxti;
700 }
701
702 static int
703 isp_sbus_dmasetup(ispsoftc_t *isp, struct ccb_scsiio *csio, ispreq_t *rq,
704         uint16_t *nxtip, uint16_t optr)
705 {
706         struct isp_sbussoftc *sbs = (struct isp_sbussoftc *)isp;
707         ispreq_t *qep;
708         bus_dmamap_t *dp = NULL;
709         mush_t mush, *mp;
710         void (*eptr)(void *, bus_dma_segment_t *, int, int);
711
712         qep = (ispreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, isp->isp_reqidx);
713         eptr = dma2;
714
715
716         if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE ||
717             (csio->dxfer_len == 0)) {
718                 rq->req_seg_count = 1;
719                 goto mbxsync;
720         }
721
722         /*
723          * Do a virtual grapevine step to collect info for
724          * the callback dma allocation that we have to use...
725          */
726         mp = &mush;
727         mp->isp = isp;
728         mp->cmd_token = csio;
729         mp->rq = rq;
730         mp->nxtip = nxtip;
731         mp->optr = optr;
732         mp->error = 0;
733
734         if ((csio->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
735                 if ((csio->ccb_h.flags & CAM_DATA_PHYS) == 0) {
736                         int error, s;
737                         dp = &sbs->dmaps[isp_handle_index(rq->req_handle)];
738                         s = splsoftvm();
739                         error = bus_dmamap_load(sbs->dmat, *dp,
740                             csio->data_ptr, csio->dxfer_len, eptr, mp, 0);
741                         if (error == EINPROGRESS) {
742                                 bus_dmamap_unload(sbs->dmat, *dp);
743                                 mp->error = EINVAL;
744                                 isp_prt(isp, ISP_LOGERR,
745                                     "deferred dma allocation not supported");
746                         } else if (error && mp->error == 0) {
747 #ifdef  DIAGNOSTIC
748                                 isp_prt(isp, ISP_LOGERR,
749                                     "error %d in dma mapping code", error);
750 #endif
751                                 mp->error = error;
752                         }
753                         splx(s);
754                 } else {
755                         /* Pointer to physical buffer */
756                         struct bus_dma_segment seg;
757                         seg.ds_addr = (bus_addr_t)csio->data_ptr;
758                         seg.ds_len = csio->dxfer_len;
759                         (*eptr)(mp, &seg, 1, 0);
760                 }
761         } else {
762                 struct bus_dma_segment *segs;
763
764                 if ((csio->ccb_h.flags & CAM_DATA_PHYS) != 0) {
765                         isp_prt(isp, ISP_LOGERR,
766                             "Physical segment pointers unsupported");
767                         mp->error = EINVAL;
768                 } else if ((csio->ccb_h.flags & CAM_SG_LIST_PHYS) == 0) {
769                         isp_prt(isp, ISP_LOGERR,
770                             "Virtual segment addresses unsupported");
771                         mp->error = EINVAL;
772                 } else {
773                         /* Just use the segments provided */
774                         segs = (struct bus_dma_segment *) csio->data_ptr;
775                         (*eptr)(mp, segs, csio->sglist_cnt, 0);
776                 }
777         }
778         if (mp->error) {
779                 int retval = CMD_COMPLETE;
780                 if (mp->error == MUSHERR_NOQENTRIES) {
781                         retval = CMD_EAGAIN;
782                 } else if (mp->error == EFBIG) {
783                         XS_SETERR(csio, CAM_REQ_TOO_BIG);
784                 } else if (mp->error == EINVAL) {
785                         XS_SETERR(csio, CAM_REQ_INVALID);
786                 } else {
787                         XS_SETERR(csio, CAM_UNREC_HBA_ERROR);
788                 }
789                 return (retval);
790         }
791 mbxsync:
792         switch (rq->req_header.rqs_entry_type) {
793         case RQSTYPE_REQUEST:
794                 isp_put_request(isp, rq, qep);
795                 break;
796         case RQSTYPE_CMDONLY:
797                 isp_put_extended_request(isp, (ispextreq_t *)rq,
798                     (ispextreq_t *)qep);
799                 break;
800         }
801         return (CMD_QUEUED);
802 }
803
804 static void
805 isp_sbus_dmateardown(ispsoftc_t *isp, XS_T *xs, uint16_t handle)
806 {
807         struct isp_sbussoftc *sbs = (struct isp_sbussoftc *)isp;
808         bus_dmamap_t *dp = &sbs->dmaps[isp_handle_index(handle)];
809         if ((xs->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
810                 bus_dmamap_sync(sbs->dmat, *dp, BUS_DMASYNC_POSTREAD);
811         } else {
812                 bus_dmamap_sync(sbs->dmat, *dp, BUS_DMASYNC_POSTWRITE);
813         }
814         bus_dmamap_unload(sbs->dmat, *dp);
815 }
816
817
818 static void
819 isp_sbus_reset1(ispsoftc_t *isp)
820 {
821         /* enable interrupts */
822         ENABLE_INTS(isp);
823 }
824
825 static void
826 isp_sbus_dumpregs(ispsoftc_t *isp, const char *msg)
827 {
828         if (msg)
829                 printf("%s: %s\n", device_get_nameunit(isp->isp_dev), msg);
830         else
831                 printf("%s:\n", device_get_nameunit(isp->isp_dev));
832         printf("    biu_conf1=%x", ISP_READ(isp, BIU_CONF1));
833         printf(" biu_icr=%x biu_isr=%x biu_sema=%x ", ISP_READ(isp, BIU_ICR),
834             ISP_READ(isp, BIU_ISR), ISP_READ(isp, BIU_SEMA));
835         printf("risc_hccr=%x\n", ISP_READ(isp, HCCR));
836
837
838         ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
839         printf("    cdma_conf=%x cdma_sts=%x cdma_fifostat=%x\n",
840                 ISP_READ(isp, CDMA_CONF), ISP_READ(isp, CDMA_STATUS),
841                 ISP_READ(isp, CDMA_FIFO_STS));
842         printf("    ddma_conf=%x ddma_sts=%x ddma_fifostat=%x\n",
843                 ISP_READ(isp, DDMA_CONF), ISP_READ(isp, DDMA_STATUS),
844                 ISP_READ(isp, DDMA_FIFO_STS));
845         printf("    sxp_int=%x sxp_gross=%x sxp(scsi_ctrl)=%x\n",
846                 ISP_READ(isp, SXP_INTERRUPT),
847                 ISP_READ(isp, SXP_GROSS_ERR),
848                 ISP_READ(isp, SXP_PINS_CTRL));
849         ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
850         printf("    mbox regs: %x %x %x %x %x\n",
851             ISP_READ(isp, OUTMAILBOX0), ISP_READ(isp, OUTMAILBOX1),
852             ISP_READ(isp, OUTMAILBOX2), ISP_READ(isp, OUTMAILBOX3),
853             ISP_READ(isp, OUTMAILBOX4));
854 }