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