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