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