]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/dev/isp/isp_sbus.c
Copy head to stable/8 as part of 8.0 Release cycle.
[FreeBSD/stable/8.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 #include <sys/linker.h>
37 #include <sys/firmware.h>
38 #include <sys/bus.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/resource.h>
42
43 #include <dev/ofw/ofw_bus.h>
44
45 #include <machine/bus.h>
46 #include <machine/resource.h>
47 #include <sys/rman.h>
48 #include <sparc64/sbus/sbusvar.h>
49
50 #include <dev/isp/isp_freebsd.h>
51
52 static uint32_t isp_sbus_rd_reg(ispsoftc_t *, int);
53 static void isp_sbus_wr_reg(ispsoftc_t *, int, uint32_t);
54 static int isp_sbus_rd_isr(ispsoftc_t *, uint32_t *, uint16_t *, uint16_t *);
55 static int isp_sbus_mbxdma(ispsoftc_t *);
56 static int isp_sbus_dmasetup(ispsoftc_t *, XS_T *, void *);
57
58
59 static void isp_sbus_reset0(ispsoftc_t *);
60 static void isp_sbus_reset1(ispsoftc_t *);
61 static void isp_sbus_dumpregs(ispsoftc_t *, const char *);
62
63 static struct ispmdvec mdvec = {
64         isp_sbus_rd_isr,
65         isp_sbus_rd_reg,
66         isp_sbus_wr_reg,
67         isp_sbus_mbxdma,
68         isp_sbus_dmasetup,
69         isp_common_dmateardown,
70         isp_sbus_reset0,
71         isp_sbus_reset1,
72         isp_sbus_dumpregs,
73         NULL,
74         BIU_BURST_ENABLE|BIU_PCI_CONF1_FIFO_64
75 };
76
77 static int isp_sbus_probe (device_t);
78 static int isp_sbus_attach (device_t);
79
80
81 #define ISP_SBD(isp)    ((struct isp_sbussoftc *)isp)->sbus_dev
82 struct isp_sbussoftc {
83         ispsoftc_t                      sbus_isp;
84         device_t                        sbus_dev;
85         struct resource *               sbus_reg;
86         void *                          ih;
87         int16_t                         sbus_poff[_NREG_BLKS];
88         sdparam                         sbus_param;
89         struct isp_spi                  sbus_spi;
90         struct ispmdvec                 sbus_mdvec;
91         struct resource *               sbus_ires;
92 };
93
94
95 static device_method_t isp_sbus_methods[] = {
96         /* Device interface */
97         DEVMETHOD(device_probe,         isp_sbus_probe),
98         DEVMETHOD(device_attach,        isp_sbus_attach),
99         { 0, 0 }
100 };
101
102 static driver_t isp_sbus_driver = {
103         "isp", isp_sbus_methods, sizeof (struct isp_sbussoftc)
104 };
105 static devclass_t isp_devclass;
106 DRIVER_MODULE(isp, sbus, isp_sbus_driver, isp_devclass, 0, 0);
107
108 static int
109 isp_sbus_probe(device_t dev)
110 {
111         int found = 0;
112         const char *name = ofw_bus_get_name(dev);
113         if (strcmp(name, "SUNW,isp") == 0 ||
114             strcmp(name, "QLGC,isp") == 0 ||
115             strcmp(name, "ptisp") == 0 ||
116             strcmp(name, "PTI,ptisp") == 0) {
117                 found++;
118         }
119         if (!found)
120                 return (ENXIO);
121         
122         if (isp_announced == 0 && bootverbose) {
123                 printf("Qlogic ISP Driver, FreeBSD Version %d.%d, "
124                     "Core Version %d.%d\n",
125                     ISP_PLATFORM_VERSION_MAJOR, ISP_PLATFORM_VERSION_MINOR,
126                     ISP_CORE_VERSION_MAJOR, ISP_CORE_VERSION_MINOR);
127                 isp_announced++;
128         }
129         return (0);
130 }
131
132 static int
133 isp_sbus_attach(device_t dev)
134 {
135         struct resource *regs;
136         int tval, iqd, isp_debug, role, rid, ispburst, default_id;
137         struct isp_sbussoftc *sbs;
138         ispsoftc_t *isp = NULL;
139         int locksetup = 0;
140         int ints_setup = 0;
141
142         /*
143          * Figure out if we're supposed to skip this one.
144          * If we are, we actually go to ISP_ROLE_NONE.
145          */
146
147         tval = 0;
148         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
149             "disable", &tval) == 0 && tval) {
150                 device_printf(dev, "device is disabled\n");
151                 /* but return 0 so the !$)$)*!$*) unit isn't reused */
152                 return (0);
153         }
154         
155         role = 0;
156         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
157             "role", &role) == 0 &&
158             ((role & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) == 0)) {
159                 device_printf(dev, "setting role to 0x%x\n", role);
160         } else {
161                 role = ISP_DEFAULT_ROLES;
162         }
163
164         sbs = malloc(sizeof (*sbs), M_DEVBUF, M_NOWAIT | M_ZERO);
165         if (sbs == NULL) {
166                 device_printf(dev, "cannot allocate softc\n");
167                 return (ENOMEM);
168         }
169
170         regs = NULL;
171         iqd = 0;
172         rid = 0;
173         regs = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
174         if (regs == 0) {
175                 device_printf(dev, "unable to map registers\n");
176                 goto bad;
177         }
178         sbs->sbus_dev = dev;
179         sbs->sbus_reg = regs;
180         sbs->sbus_mdvec = mdvec;
181
182         sbs->sbus_poff[BIU_BLOCK >> _BLK_REG_SHFT] = BIU_REGS_OFF;
183         sbs->sbus_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = SBUS_MBOX_REGS_OFF;
184         sbs->sbus_poff[SXP_BLOCK >> _BLK_REG_SHFT] = SBUS_SXP_REGS_OFF;
185         sbs->sbus_poff[RISC_BLOCK >> _BLK_REG_SHFT] = SBUS_RISC_REGS_OFF;
186         sbs->sbus_poff[DMA_BLOCK >> _BLK_REG_SHFT] = DMA_REGS_OFF;
187         isp = &sbs->sbus_isp;
188         isp->isp_bus_tag = rman_get_bustag(regs);
189         isp->isp_bus_handle = rman_get_bushandle(regs);
190         isp->isp_mdvec = &sbs->sbus_mdvec;
191         isp->isp_bustype = ISP_BT_SBUS;
192         isp->isp_type = ISP_HA_SCSI_UNKNOWN;
193         isp->isp_param = &sbs->sbus_param;
194         isp->isp_osinfo.pc.ptr = &sbs->sbus_spi;
195         isp->isp_revision = 0;  /* XXX */
196         ISP_SET_PC(isp, 0, role, role);
197
198         /*
199          * Get the clock frequency and convert it from HZ to MHz,
200          * rounding up. This defaults to 25MHz if there isn't a
201          * device specific one in the OFW device tree.
202          */
203         sbs->sbus_mdvec.dv_clock = (sbus_get_clockfreq(dev) + 500000)/1000000;
204
205         /*
206          * Now figure out what the proper burst sizes, etc., to use.
207          * Unfortunately, there is no ddi_dma_burstsizes here which
208          * walks up the tree finding the limiting burst size node (if
209          * any). We just use what's here for isp.
210          */
211         ispburst = sbus_get_burstsz(dev);
212         if (ispburst == 0) {
213                 ispburst = SBUS_BURST_32 - 1;
214         }
215         sbs->sbus_mdvec.dv_conf1 =  0;
216         if (ispburst & (1 << 5)) {
217                 sbs->sbus_mdvec.dv_conf1 = BIU_SBUS_CONF1_FIFO_32;
218         } else if (ispburst & (1 << 4)) {
219                 sbs->sbus_mdvec.dv_conf1 = BIU_SBUS_CONF1_FIFO_16;
220         } else if (ispburst & (1 << 3)) {
221                 sbs->sbus_mdvec.dv_conf1 =
222                     BIU_SBUS_CONF1_BURST8 | BIU_SBUS_CONF1_FIFO_8;
223         }
224         if (sbs->sbus_mdvec.dv_conf1) {
225                 sbs->sbus_mdvec.dv_conf1 |= BIU_BURST_ENABLE;
226         }
227
228         /*
229          * We don't trust NVRAM on SBus cards
230          */
231         isp->isp_confopts |= ISP_CFG_NONVRAM;
232
233         /*
234          * Mark things if we're a PTI SBus adapter.
235          */
236         if (strcmp("PTI,ptisp", ofw_bus_get_name(dev)) == 0 ||
237             strcmp("ptisp", ofw_bus_get_name(dev)) == 0) {
238                 SDPARAM(isp, 0)->isp_ptisp = 1;
239         }
240
241
242         isp->isp_osinfo.fw = firmware_get("isp_1000");
243         if (isp->isp_osinfo.fw) {
244                 union {
245                         const void *cp;
246                         uint16_t *sp;
247                 } stupid;
248                 stupid.cp = isp->isp_osinfo.fw->data;
249                 isp->isp_mdvec->dv_ispfw = stupid.sp;
250         }
251
252         tval = 0;
253         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
254             "fwload_disable", &tval) == 0 && tval != 0) {
255                 isp->isp_confopts |= ISP_CFG_NORELOAD;
256         }
257
258         default_id = -1;
259         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
260             "iid", &tval) == 0) {
261                 default_id = tval;
262                 isp->isp_confopts |= ISP_CFG_OWNLOOPID;
263         }
264         if (default_id == -1) {
265                 /*
266                  * XXX: should be a way to get properties w/o having
267                  * XXX: to call OF_xxx functions
268                  */
269                 default_id = 7;
270         }
271         ISP_SPI_PC(isp, 0)->iid = default_id;
272
273         isp_debug = 0;
274         (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
275             "debug", &isp_debug);
276
277         /* Make sure the lock is set up. */
278         mtx_init(&isp->isp_osinfo.lock, "isp", NULL, MTX_DEF);
279         locksetup++;
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         if (isp_setup_intr(dev, sbs->sbus_ires, ISP_IFLAGS, NULL,
290             isp_platform_intr, isp, &sbs->ih)) {
291                 device_printf(dev, "could not setup interrupt\n");
292                 goto bad;
293         }
294         ints_setup++;
295
296         /*
297          * Set up logging levels.
298          */
299         if (isp_debug) {
300                 isp->isp_dblev = isp_debug;
301         } else {
302                 isp->isp_dblev = ISP_LOGWARN|ISP_LOGERR;
303         }
304         if (bootverbose) {
305                 isp->isp_dblev |= ISP_LOGCONFIG|ISP_LOGINFO;
306         }
307
308         /*
309          * Make sure we're in reset state.
310          */
311         ISP_LOCK(isp);
312         isp_reset(isp, 1);
313         if (isp->isp_state != ISP_RESETSTATE) {
314                 isp_uninit(isp);
315                 ISP_UNLOCK(isp);
316                 goto bad;
317         }
318         isp_init(isp);
319         if (role != ISP_ROLE_NONE && isp->isp_state != ISP_INITSTATE) {
320                 isp_uninit(isp);
321                 ISP_UNLOCK(isp);
322                 goto bad;
323         }
324         isp_attach(isp);
325         if (role != ISP_ROLE_NONE && isp->isp_state != ISP_RUNSTATE) {
326                 isp_uninit(isp);
327                 ISP_UNLOCK(isp);
328                 goto bad;
329         }
330         ISP_UNLOCK(isp);
331         return (0);
332
333 bad:
334
335         if (sbs && ints_setup) {
336                 (void) bus_teardown_intr(dev, sbs->sbus_ires, sbs->ih);
337         }
338
339         if (sbs && sbs->sbus_ires) {
340                 bus_release_resource(dev, SYS_RES_IRQ, iqd, sbs->sbus_ires);
341         }
342
343         if (locksetup && isp) {
344                 mtx_destroy(&isp->isp_osinfo.lock);
345         }
346
347         if (regs) {
348                 (void) bus_release_resource(dev, 0, 0, regs);
349         }
350
351         if (sbs) {
352                 if (sbs->sbus_isp.isp_param) {
353                         free(sbs->sbus_isp.isp_param, M_DEVBUF);
354                 }
355                 free(sbs, M_DEVBUF);
356         }
357         return (ENXIO);
358 }
359
360 #define IspVirt2Off(a, x)       \
361         (((struct isp_sbussoftc *)a)->sbus_poff[((x) & _BLK_REG_MASK) >> \
362         _BLK_REG_SHFT] + ((x) & 0xff))
363
364 #define BXR2(sbc, off)          \
365         bus_space_read_2(isp->isp_bus_tag, isp->isp_bus_handle, off)
366
367 static int
368 isp_sbus_rd_isr(ispsoftc_t *isp, uint32_t *isrp, uint16_t *semap, uint16_t *mbp)
369 {
370         uint16_t isr, sema;
371
372         isr = BXR2(sbc, IspVirt2Off(isp, BIU_ISR));
373         sema = BXR2(sbc, IspVirt2Off(isp, BIU_SEMA));
374         isp_prt(isp, ISP_LOGDEBUG3, "ISR 0x%x SEMA 0x%x", isr, sema);
375         isr &= INT_PENDING_MASK(isp);
376         sema &= BIU_SEMA_LOCK;
377         if (isr == 0 && sema == 0) {
378                 return (0);
379         }
380         *isrp = isr;
381         if ((*semap = sema) != 0) {
382                 *mbp = BXR2(sbc, IspVirt2Off(isp, OUTMAILBOX0));
383         }
384         return (1);
385 }
386
387 static uint32_t
388 isp_sbus_rd_reg(ispsoftc_t *isp, int regoff)
389 {
390         uint16_t rval;
391         struct isp_sbussoftc *sbs = (struct isp_sbussoftc *) isp;
392         int offset = sbs->sbus_poff[(regoff & _BLK_REG_MASK) >> _BLK_REG_SHFT];
393         offset += (regoff & 0xff);
394         rval = bus_space_read_2(isp->isp_bus_tag, isp->isp_bus_handle, offset);
395         isp_prt(isp, ISP_LOGDEBUG3,
396             "isp_sbus_rd_reg(off %x) = %x", regoff, rval);
397         return (rval);
398 }
399
400 static void
401 isp_sbus_wr_reg(ispsoftc_t *isp, int regoff, uint32_t val)
402 {
403         struct isp_sbussoftc *sbs = (struct isp_sbussoftc *) isp;
404         int offset = sbs->sbus_poff[(regoff & _BLK_REG_MASK) >> _BLK_REG_SHFT];
405         offset += (regoff & 0xff);
406         isp_prt(isp, ISP_LOGDEBUG3,
407             "isp_sbus_wr_reg(off %x) = %x", regoff, val);
408         bus_space_write_2(isp->isp_bus_tag, isp->isp_bus_handle, offset, val);
409         MEMORYBARRIER(isp, SYNC_REG, offset, 2);
410 }
411
412 struct imush {
413         ispsoftc_t *isp;
414         int error;
415 };
416
417 static void imc(void *, bus_dma_segment_t *, int, int);
418
419 static void
420 imc(void *arg, bus_dma_segment_t *segs, int nseg, int error)
421 {
422         struct imush *imushp = (struct imush *) arg;
423         if (error) {
424                 imushp->error = error;
425         } else {
426                 ispsoftc_t *isp =imushp->isp;
427                 bus_addr_t addr = segs->ds_addr;
428
429                 isp->isp_rquest_dma = addr;
430                 addr += ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
431                 isp->isp_result_dma = addr;
432         }
433 }
434
435 static int
436 isp_sbus_mbxdma(ispsoftc_t *isp)
437 {
438         caddr_t base;
439         uint32_t len;
440         int i, error, ns;
441         struct imush im;
442
443         /*
444          * Already been here? If so, leave...
445          */
446         if (isp->isp_rquest) {
447                 return (0);
448         }
449
450         ISP_UNLOCK(isp);
451
452         len = sizeof (struct isp_pcmd) * isp->isp_maxcmds;
453         isp->isp_osinfo.pcmd_pool = (struct isp_pcmd *)
454             malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
455         if (isp->isp_osinfo.pcmd_pool == NULL) {
456                 isp_prt(isp, ISP_LOGERR, "cannot alloc pcmd pool");
457                 ISP_LOCK(isp);
458                 return (1);
459         }
460
461         len = sizeof (XS_T **) * isp->isp_maxcmds;
462         isp->isp_xflist = (XS_T **) malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
463         if (isp->isp_xflist == NULL) {
464                 isp_prt(isp, ISP_LOGERR, "cannot alloc xflist array");
465                 ISP_LOCK(isp);
466                 return (1);
467         }
468         len = sizeof (bus_dmamap_t) * isp->isp_maxcmds;
469
470         if (isp_dma_tag_create(BUS_DMA_ROOTARG(ISP_SBD(isp)), 1,
471             BUS_SPACE_MAXADDR_24BIT+1, BUS_SPACE_MAXADDR_32BIT,
472             BUS_SPACE_MAXADDR_32BIT, NULL, NULL, BUS_SPACE_MAXSIZE_32BIT,
473             ISP_NSEGS, BUS_SPACE_MAXADDR_24BIT, 0, &isp->isp_osinfo.dmat)) {
474                 isp_prt(isp, ISP_LOGERR, "could not create master dma tag");
475                 free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
476                 free(isp->isp_xflist, M_DEVBUF);
477                 ISP_LOCK(isp);
478                 return(1);
479         }
480
481         /*
482          * Allocate and map the request, result queues, plus FC scratch area.
483          */
484         len = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
485         len += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
486
487         ns = (len / PAGE_SIZE) + 1;
488         if (isp_dma_tag_create(isp->isp_osinfo.dmat, QENTRY_LEN,
489             BUS_SPACE_MAXADDR_24BIT+1, BUS_SPACE_MAXADDR_32BIT,
490             BUS_SPACE_MAXADDR_32BIT, NULL, NULL, len, ns,
491             BUS_SPACE_MAXADDR_24BIT, 0, &isp->isp_osinfo.cdmat)) {
492                 isp_prt(isp, ISP_LOGERR,
493                     "cannot create a dma tag for control spaces");
494                 free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
495                 free(isp->isp_xflist, M_DEVBUF);
496                 ISP_LOCK(isp);
497                 return (1);
498         }
499
500         if (bus_dmamem_alloc(isp->isp_osinfo.cdmat, (void **)&base, BUS_DMA_NOWAIT,
501             &isp->isp_osinfo.cdmap) != 0) {
502                 isp_prt(isp, ISP_LOGERR,
503                     "cannot allocate %d bytes of CCB memory", len);
504                 bus_dma_tag_destroy(isp->isp_osinfo.cdmat);
505                 free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
506                 free(isp->isp_xflist, M_DEVBUF);
507                 ISP_LOCK(isp);
508                 return (1);
509         }
510
511         for (i = 0; i < isp->isp_maxcmds; i++) {
512                 struct isp_pcmd *pcmd = &isp->isp_osinfo.pcmd_pool[i];
513                 error = bus_dmamap_create(isp->isp_osinfo.dmat, 0, &pcmd->dmap);
514                 if (error) {
515                         isp_prt(isp, ISP_LOGERR,
516                             "error %d creating per-cmd DMA maps", error);
517                         while (--i >= 0) {
518                                 bus_dmamap_destroy(isp->isp_osinfo.dmat,
519                                     isp->isp_osinfo.pcmd_pool[i].dmap);
520                         }
521                         goto bad;
522                 }
523                 callout_init_mtx(&pcmd->wdog, &isp->isp_osinfo.lock, 0);
524                 if (i == isp->isp_maxcmds-1) {
525                         pcmd->next = NULL;
526                 } else {
527                         pcmd->next = &isp->isp_osinfo.pcmd_pool[i+1];
528                 }
529         }
530         isp->isp_osinfo.pcmd_free = &isp->isp_osinfo.pcmd_pool[0];
531
532         im.isp = isp;
533         im.error = 0;
534         bus_dmamap_load(isp->isp_osinfo.cdmat, isp->isp_osinfo.cdmap, base, len, imc, &im, 0);
535         if (im.error) {
536                 isp_prt(isp, ISP_LOGERR,
537                     "error %d loading dma map for control areas", im.error);
538                 goto bad;
539         }
540
541         isp->isp_rquest = base;
542         base += ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
543         isp->isp_result = base;
544         ISP_LOCK(isp);
545         return (0);
546
547 bad:
548         bus_dmamem_free(isp->isp_osinfo.cdmat, base, isp->isp_osinfo.cdmap);
549         bus_dma_tag_destroy(isp->isp_osinfo.cdmat);
550         free(isp->isp_xflist, M_DEVBUF);
551         free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
552         isp->isp_rquest = NULL;
553         ISP_LOCK(isp);
554         return (1);
555 }
556
557 typedef struct {
558         ispsoftc_t *isp;
559         void *cmd_token;
560         void *rq;       /* original request */
561         int error;
562         bus_size_t mapsize;
563 } mush_t;
564
565 #define MUSHERR_NOQENTRIES      -2
566
567 static void dma2(void *, bus_dma_segment_t *, int, int);
568
569 static void
570 dma2(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
571 {
572         mush_t *mp;
573         ispsoftc_t *isp;
574         struct ccb_scsiio *csio;
575         isp_ddir_t ddir;
576         ispreq_t *rq;
577
578         mp = (mush_t *) arg;
579         if (error) {
580                 mp->error = error;
581                 return;
582         }
583         csio = mp->cmd_token;
584         isp = mp->isp;
585         rq = mp->rq;
586         if (nseg) {
587                 if (sizeof (bus_addr_t) > 4) {
588                         if (rq->req_header.rqs_entry_type == RQSTYPE_T2RQS) {
589                                 rq->req_header.rqs_entry_type = RQSTYPE_T3RQS;
590                         } else if (rq->req_header.rqs_entry_type == RQSTYPE_REQUEST) {
591                                 rq->req_header.rqs_entry_type = RQSTYPE_A64;
592                         }
593                 }
594                 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
595                         bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_PREREAD);
596                         ddir = ISP_FROM_DEVICE;
597                 } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
598                         bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_PREWRITE);
599                         ddir = ISP_TO_DEVICE;
600                 } else {
601                         ddir = ISP_NOXFR;
602                 }
603         } else {
604                 dm_segs = NULL;
605                 nseg = 0;
606                 ddir = ISP_NOXFR;
607         }
608
609         if (isp_send_cmd(isp, rq, dm_segs, nseg, XS_XFRLEN(csio), ddir) != CMD_QUEUED) {
610                 mp->error = MUSHERR_NOQENTRIES;
611         }
612 }
613
614 static int
615 isp_sbus_dmasetup(ispsoftc_t *isp, struct ccb_scsiio *csio, void *ff)
616 {
617         mush_t mush, *mp;
618         void (*eptr)(void *, bus_dma_segment_t *, int, int);
619
620         mp = &mush;
621         mp->isp = isp;
622         mp->cmd_token = csio;
623         mp->rq = ff;
624         mp->error = 0;
625         mp->mapsize = 0;
626
627         eptr = dma2;
628
629         if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE || (csio->dxfer_len == 0)) {
630                 (*eptr)(mp, NULL, 0, 0);
631         } else if ((csio->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
632                 if ((csio->ccb_h.flags & CAM_DATA_PHYS) == 0) {
633                         int error;
634                         error = bus_dmamap_load(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, csio->data_ptr, csio->dxfer_len, eptr, mp, 0);
635 #if 0
636                         xpt_print(csio->ccb_h.path, "%s: bus_dmamap_load " "ptr %p len %d returned %d\n", __func__, csio->data_ptr, csio->dxfer_len, error);
637 #endif
638
639                         if (error == EINPROGRESS) {
640                                 bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
641                                 mp->error = EINVAL;
642                                 isp_prt(isp, ISP_LOGERR, "deferred dma allocation not supported");
643                         } else if (error && mp->error == 0) {
644 #ifdef  DIAGNOSTIC
645                                 isp_prt(isp, ISP_LOGERR, "error %d in dma mapping code", error);
646 #endif
647                                 mp->error = error;
648                         }
649                 } else {
650                         /* Pointer to physical buffer */
651                         struct bus_dma_segment seg;
652                         seg.ds_addr = (bus_addr_t)(vm_offset_t)csio->data_ptr;
653                         seg.ds_len = csio->dxfer_len;
654                         (*eptr)(mp, &seg, 1, 0);
655                 }
656         } else {
657                 struct bus_dma_segment *segs;
658
659                 if ((csio->ccb_h.flags & CAM_DATA_PHYS) != 0) {
660                         isp_prt(isp, ISP_LOGERR, "Physical segment pointers unsupported");
661                         mp->error = EINVAL;
662                 } else if ((csio->ccb_h.flags & CAM_SG_LIST_PHYS) == 0) {
663                         isp_prt(isp, ISP_LOGERR, "Physical SG/LIST Phys segment pointers unsupported");
664                         mp->error = EINVAL;
665                 } else {
666                         /* Just use the segments provided */
667                         segs = (struct bus_dma_segment *) csio->data_ptr;
668                         (*eptr)(mp, segs, csio->sglist_cnt, 0);
669                 }
670         }
671         if (mp->error) {
672                 int retval = CMD_COMPLETE;
673                 if (mp->error == MUSHERR_NOQENTRIES) {
674                         retval = CMD_EAGAIN;
675                 } else if (mp->error == EFBIG) {
676                         XS_SETERR(csio, CAM_REQ_TOO_BIG);
677                 } else if (mp->error == EINVAL) {
678                         XS_SETERR(csio, CAM_REQ_INVALID);
679                 } else {
680                         XS_SETERR(csio, CAM_UNREC_HBA_ERROR);
681                 }
682                 return (retval);
683         }
684         return (CMD_QUEUED);
685 }
686
687 static void
688 isp_sbus_reset0(ispsoftc_t *isp)
689 {
690         ISP_DISABLE_INTS(isp);
691 }
692
693 static void
694 isp_sbus_reset1(ispsoftc_t *isp)
695 {
696         ISP_ENABLE_INTS(isp);
697 }
698
699 static void
700 isp_sbus_dumpregs(ispsoftc_t *isp, const char *msg)
701 {
702         if (msg)
703                 printf("%s: %s\n", device_get_nameunit(isp->isp_dev), msg);
704         else
705                 printf("%s:\n", device_get_nameunit(isp->isp_dev));
706         printf("    biu_conf1=%x", ISP_READ(isp, BIU_CONF1));
707         printf(" biu_icr=%x biu_isr=%x biu_sema=%x ", ISP_READ(isp, BIU_ICR),
708             ISP_READ(isp, BIU_ISR), ISP_READ(isp, BIU_SEMA));
709         printf("risc_hccr=%x\n", ISP_READ(isp, HCCR));
710
711
712         ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
713         printf("    cdma_conf=%x cdma_sts=%x cdma_fifostat=%x\n",
714                 ISP_READ(isp, CDMA_CONF), ISP_READ(isp, CDMA_STATUS),
715                 ISP_READ(isp, CDMA_FIFO_STS));
716         printf("    ddma_conf=%x ddma_sts=%x ddma_fifostat=%x\n",
717                 ISP_READ(isp, DDMA_CONF), ISP_READ(isp, DDMA_STATUS),
718                 ISP_READ(isp, DDMA_FIFO_STS));
719         printf("    sxp_int=%x sxp_gross=%x sxp(scsi_ctrl)=%x\n",
720                 ISP_READ(isp, SXP_INTERRUPT),
721                 ISP_READ(isp, SXP_GROSS_ERR),
722                 ISP_READ(isp, SXP_PINS_CTRL));
723         ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
724         printf("    mbox regs: %x %x %x %x %x\n",
725             ISP_READ(isp, OUTMAILBOX0), ISP_READ(isp, OUTMAILBOX1),
726             ISP_READ(isp, OUTMAILBOX2), ISP_READ(isp, OUTMAILBOX3),
727             ISP_READ(isp, OUTMAILBOX4));
728 }