isp_sbus.c revision 1.67.2.1 1 1.67.2.1 ad /* $NetBSD: isp_sbus.c,v 1.67.2.1 2007/05/27 14:30:28 ad Exp $ */
2 1.1 mrg /*
3 1.1 mrg * SBus specific probe and attach routines for Qlogic ISP SCSI adapters.
4 1.1 mrg *
5 1.67.2.1 ad * Copyright (C) 1997, 1998, 1999 National Aeronautics & Space Administration
6 1.67.2.1 ad * All rights reserved.
7 1.67.2.1 ad *
8 1.67.2.1 ad * Additional Copyright (C) 2000-2007 by Matthew Jacob
9 1.1 mrg * All rights reserved.
10 1.1 mrg *
11 1.1 mrg * Redistribution and use in source and binary forms, with or without
12 1.1 mrg * modification, are permitted provided that the following conditions
13 1.1 mrg * are met:
14 1.1 mrg * 1. Redistributions of source code must retain the above copyright
15 1.67.2.1 ad * notice, this list of conditions and the following disclaimer.
16 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 mrg * notice, this list of conditions and the following disclaimer in the
18 1.1 mrg * documentation and/or other materials provided with the distribution.
19 1.67.2.1 ad * 3. The name of the author may not be used to endorse or promote products
20 1.67.2.1 ad * derived from this software without specific prior written permission
21 1.1 mrg *
22 1.67.2.1 ad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.67.2.1 ad * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.67.2.1 ad * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.67.2.1 ad * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.67.2.1 ad * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.67.2.1 ad * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.67.2.1 ad * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.67.2.1 ad * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.67.2.1 ad * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.67.2.1 ad * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 mrg *
33 1.1 mrg */
34 1.47 lukem
35 1.47 lukem #include <sys/cdefs.h>
36 1.67.2.1 ad __KERNEL_RCSID(0, "$NetBSD: isp_sbus.c,v 1.67.2.1 2007/05/27 14:30:28 ad Exp $");
37 1.1 mrg
38 1.1 mrg #include <sys/param.h>
39 1.1 mrg #include <sys/systm.h>
40 1.1 mrg #include <sys/device.h>
41 1.1 mrg #include <sys/kernel.h>
42 1.1 mrg #include <sys/malloc.h>
43 1.1 mrg #include <sys/queue.h>
44 1.48 mjacob #include <dev/ic/isp_netbsd.h>
45 1.28 pk #include <machine/intr.h>
46 1.1 mrg #include <machine/autoconf.h>
47 1.1 mrg #include <dev/sbus/sbusvar.h>
48 1.35 mjacob #include <sys/reboot.h>
49 1.1 mrg
50 1.67.2.1 ad static void isp_sbus_reset0(struct ispsoftc *);
51 1.49 mjacob static void isp_sbus_reset1(struct ispsoftc *);
52 1.42 mjacob static int isp_sbus_intr(void *);
53 1.45 mjacob static int
54 1.67.2.1 ad isp_sbus_rd_isr(struct ispsoftc *, uint32_t *, uint16_t *, uint16_t *);
55 1.67.2.1 ad static uint32_t isp_sbus_rd_reg(struct ispsoftc *, int);
56 1.67.2.1 ad static void isp_sbus_wr_reg (struct ispsoftc *, int, uint32_t);
57 1.42 mjacob static int isp_sbus_mbxdma(struct ispsoftc *);
58 1.67.2.1 ad static int isp_sbus_dmasetup(struct ispsoftc *, XS_T *, ispreq_t *, uint32_t *,
59 1.67.2.1 ad uint32_t);
60 1.67.2.1 ad static void isp_sbus_dmateardown(struct ispsoftc *, XS_T *, uint32_t);
61 1.1 mrg
62 1.67.2.1 ad #ifndef ISP_DISABLE_FW
63 1.67.2.1 ad #include <dev/microcode/isp/asm_sbus.h>
64 1.67.2.1 ad #else
65 1.17 mjacob #define ISP_1000_RISC_CODE NULL
66 1.17 mjacob #endif
67 1.17 mjacob
68 1.67.2.1 ad static const struct ispmdvec mdvec = {
69 1.45 mjacob isp_sbus_rd_isr,
70 1.1 mrg isp_sbus_rd_reg,
71 1.1 mrg isp_sbus_wr_reg,
72 1.1 mrg isp_sbus_mbxdma,
73 1.1 mrg isp_sbus_dmasetup,
74 1.1 mrg isp_sbus_dmateardown,
75 1.67.2.1 ad isp_sbus_reset0,
76 1.49 mjacob isp_sbus_reset1,
77 1.1 mrg NULL,
78 1.66 martin ISP_1000_RISC_CODE,
79 1.66 martin 0,
80 1.66 martin 0
81 1.1 mrg };
82 1.1 mrg
83 1.1 mrg struct isp_sbussoftc {
84 1.1 mrg struct ispsoftc sbus_isp;
85 1.33 mjacob struct sbusdev sbus_sd;
86 1.1 mrg sdparam sbus_dev;
87 1.1 mrg bus_space_tag_t sbus_bustag;
88 1.6 mjacob bus_space_handle_t sbus_reg;
89 1.1 mrg int sbus_node;
90 1.1 mrg int sbus_pri;
91 1.1 mrg struct ispmdvec sbus_mdvec;
92 1.14 mjacob bus_dmamap_t *sbus_dmamap;
93 1.8 mjacob int16_t sbus_poff[_NREG_BLKS];
94 1.1 mrg };
95 1.1 mrg
96 1.1 mrg
97 1.42 mjacob static int isp_match(struct device *, struct cfdata *, void *);
98 1.42 mjacob static void isp_sbus_attach(struct device *, struct device *, void *);
99 1.57 thorpej CFATTACH_DECL(isp_sbus, sizeof (struct isp_sbussoftc),
100 1.58 thorpej isp_match, isp_sbus_attach, NULL, NULL);
101 1.1 mrg
102 1.1 mrg static int
103 1.42 mjacob isp_match(struct device *parent, struct cfdata *cf, void *aux)
104 1.1 mrg {
105 1.1 mrg int rv;
106 1.1 mrg #ifdef DEBUG
107 1.1 mrg static int oneshot = 1;
108 1.1 mrg #endif
109 1.1 mrg struct sbus_attach_args *sa = aux;
110 1.1 mrg
111 1.54 thorpej rv = (strcmp(cf->cf_name, sa->sa_name) == 0 ||
112 1.1 mrg strcmp("PTI,ptisp", sa->sa_name) == 0 ||
113 1.1 mrg strcmp("ptisp", sa->sa_name) == 0 ||
114 1.1 mrg strcmp("SUNW,isp", sa->sa_name) == 0 ||
115 1.1 mrg strcmp("QLGC,isp", sa->sa_name) == 0);
116 1.1 mrg #ifdef DEBUG
117 1.1 mrg if (rv && oneshot) {
118 1.1 mrg oneshot = 0;
119 1.1 mrg printf("Qlogic ISP Driver, NetBSD (sbus) Platform Version "
120 1.1 mrg "%d.%d Core Version %d.%d\n",
121 1.1 mrg ISP_PLATFORM_VERSION_MAJOR, ISP_PLATFORM_VERSION_MINOR,
122 1.1 mrg ISP_CORE_VERSION_MAJOR, ISP_CORE_VERSION_MINOR);
123 1.1 mrg }
124 1.1 mrg #endif
125 1.1 mrg return (rv);
126 1.1 mrg }
127 1.1 mrg
128 1.20 mjacob
129 1.1 mrg static void
130 1.42 mjacob isp_sbus_attach(struct device *parent, struct device *self, void *aux)
131 1.1 mrg {
132 1.33 mjacob int freq, ispburst, sbusburst;
133 1.1 mrg struct sbus_attach_args *sa = aux;
134 1.1 mrg struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) self;
135 1.1 mrg struct ispsoftc *isp = &sbc->sbus_isp;
136 1.1 mrg
137 1.1 mrg printf(" for %s\n", sa->sa_name);
138 1.1 mrg
139 1.1 mrg sbc->sbus_bustag = sa->sa_bustag;
140 1.16 pk if (sa->sa_nintr != 0)
141 1.16 pk sbc->sbus_pri = sa->sa_pri;
142 1.1 mrg sbc->sbus_mdvec = mdvec;
143 1.1 mrg
144 1.51 eeh if (sa->sa_npromvaddrs) {
145 1.51 eeh sbus_promaddr_to_handle(sa->sa_bustag,
146 1.51 eeh sa->sa_promvaddrs[0], &sbc->sbus_reg);
147 1.1 mrg } else {
148 1.51 eeh if (sbus_bus_map(sa->sa_bustag, sa->sa_slot, sa->sa_offset,
149 1.51 eeh sa->sa_size, 0, &sbc->sbus_reg) != 0) {
150 1.1 mrg printf("%s: cannot map registers\n", self->dv_xname);
151 1.1 mrg return;
152 1.1 mrg }
153 1.1 mrg }
154 1.1 mrg sbc->sbus_node = sa->sa_node;
155 1.1 mrg
156 1.62 pk freq = prom_getpropint(sa->sa_node, "clock-frequency", 0);
157 1.1 mrg if (freq) {
158 1.1 mrg /*
159 1.1 mrg * Convert from HZ to MHz, rounding up.
160 1.1 mrg */
161 1.1 mrg freq = (freq + 500000)/1000000;
162 1.1 mrg #if 0
163 1.1 mrg printf("%s: %d MHz\n", self->dv_xname, freq);
164 1.1 mrg #endif
165 1.1 mrg }
166 1.1 mrg sbc->sbus_mdvec.dv_clock = freq;
167 1.5 mjacob
168 1.5 mjacob /*
169 1.33 mjacob * Now figure out what the proper burst sizes, etc., to use.
170 1.33 mjacob * Unfortunately, there is no ddi_dma_burstsizes here which
171 1.33 mjacob * walks up the tree finding the limiting burst size node (if
172 1.33 mjacob * any).
173 1.5 mjacob */
174 1.33 mjacob sbusburst = ((struct sbus_softc *)parent)->sc_burst;
175 1.33 mjacob if (sbusburst == 0)
176 1.33 mjacob sbusburst = SBUS_BURST_32 - 1;
177 1.62 pk ispburst = prom_getpropint(sa->sa_node, "burst-sizes", -1);
178 1.33 mjacob if (ispburst == -1) {
179 1.33 mjacob ispburst = sbusburst;
180 1.33 mjacob }
181 1.33 mjacob ispburst &= sbusburst;
182 1.33 mjacob ispburst &= ~(1 << 7);
183 1.33 mjacob ispburst &= ~(1 << 6);
184 1.33 mjacob sbc->sbus_mdvec.dv_conf1 = 0;
185 1.33 mjacob if (ispburst & (1 << 5)) {
186 1.33 mjacob sbc->sbus_mdvec.dv_conf1 = BIU_SBUS_CONF1_FIFO_32;
187 1.33 mjacob } else if (ispburst & (1 << 4)) {
188 1.33 mjacob sbc->sbus_mdvec.dv_conf1 = BIU_SBUS_CONF1_FIFO_16;
189 1.33 mjacob } else if (ispburst & (1 << 3)) {
190 1.33 mjacob sbc->sbus_mdvec.dv_conf1 =
191 1.33 mjacob BIU_SBUS_CONF1_BURST8 | BIU_SBUS_CONF1_FIFO_8;
192 1.33 mjacob }
193 1.33 mjacob if (sbc->sbus_mdvec.dv_conf1) {
194 1.33 mjacob sbc->sbus_mdvec.dv_conf1 |= BIU_BURST_ENABLE;
195 1.33 mjacob }
196 1.1 mrg
197 1.1 mrg isp->isp_mdvec = &sbc->sbus_mdvec;
198 1.2 mjacob isp->isp_bustype = ISP_BT_SBUS;
199 1.1 mrg isp->isp_type = ISP_HA_SCSI_UNKNOWN;
200 1.1 mrg isp->isp_param = &sbc->sbus_dev;
201 1.48 mjacob isp->isp_dmatag = sa->sa_dmatag;
202 1.48 mjacob MEMZERO(isp->isp_param, sizeof (sdparam));
203 1.1 mrg
204 1.8 mjacob sbc->sbus_poff[BIU_BLOCK >> _BLK_REG_SHFT] = BIU_REGS_OFF;
205 1.8 mjacob sbc->sbus_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = SBUS_MBOX_REGS_OFF;
206 1.8 mjacob sbc->sbus_poff[SXP_BLOCK >> _BLK_REG_SHFT] = SBUS_SXP_REGS_OFF;
207 1.8 mjacob sbc->sbus_poff[RISC_BLOCK >> _BLK_REG_SHFT] = SBUS_RISC_REGS_OFF;
208 1.8 mjacob sbc->sbus_poff[DMA_BLOCK >> _BLK_REG_SHFT] = DMA_REGS_OFF;
209 1.1 mrg
210 1.35 mjacob /* Establish interrupt channel */
211 1.59 pk bus_intr_establish(sbc->sbus_bustag, sbc->sbus_pri, IPL_BIO,
212 1.35 mjacob isp_sbus_intr, sbc);
213 1.35 mjacob sbus_establish(&sbc->sbus_sd, &sbc->sbus_isp.isp_osinfo._dev);
214 1.35 mjacob
215 1.29 mjacob /*
216 1.29 mjacob * Set up logging levels.
217 1.29 mjacob */
218 1.29 mjacob #ifdef ISP_LOGDEFAULT
219 1.29 mjacob isp->isp_dblev = ISP_LOGDEFAULT;
220 1.29 mjacob #else
221 1.35 mjacob isp->isp_dblev = ISP_LOGWARN|ISP_LOGERR;
222 1.35 mjacob if (bootverbose)
223 1.35 mjacob isp->isp_dblev |= ISP_LOGCONFIG|ISP_LOGINFO;
224 1.29 mjacob #ifdef SCSIDEBUG
225 1.29 mjacob isp->isp_dblev |= ISP_LOGDEBUG1|ISP_LOGDEBUG2;
226 1.29 mjacob #endif
227 1.29 mjacob #ifdef DEBUG
228 1.35 mjacob isp->isp_dblev |= ISP_LOGDEBUG0;
229 1.29 mjacob #endif
230 1.29 mjacob #endif
231 1.35 mjacob
232 1.65 thorpej isp->isp_confopts = device_cfdata(self)->cf_flags;
233 1.35 mjacob isp->isp_role = ISP_DEFAULT_ROLES;
234 1.35 mjacob
235 1.21 mjacob /*
236 1.21 mjacob * There's no tool on sparc to set NVRAM for ISPs, so ignore it.
237 1.21 mjacob */
238 1.21 mjacob isp->isp_confopts |= ISP_CFG_NONVRAM;
239 1.67.2.1 ad
240 1.67.2.1 ad /*
241 1.67.2.1 ad * Mark things if we're a PTI SBus adapter.
242 1.67.2.1 ad */
243 1.67.2.1 ad if (strcmp("PTI,ptisp", sa->sa_name) == 0 ||
244 1.67.2.1 ad strcmp("ptisp", sa->sa_name) == 0) {
245 1.67.2.1 ad SDPARAM(isp)->isp_ptisp = 1;
246 1.67.2.1 ad }
247 1.1 mrg ISP_LOCK(isp);
248 1.1 mrg isp_reset(isp);
249 1.1 mrg if (isp->isp_state != ISP_RESETSTATE) {
250 1.1 mrg ISP_UNLOCK(isp);
251 1.1 mrg return;
252 1.1 mrg }
253 1.67.2.1 ad ISP_ENABLE_INTS(isp);
254 1.1 mrg isp_init(isp);
255 1.1 mrg if (isp->isp_state != ISP_INITSTATE) {
256 1.1 mrg isp_uninit(isp);
257 1.1 mrg ISP_UNLOCK(isp);
258 1.1 mrg return;
259 1.1 mrg }
260 1.1 mrg
261 1.1 mrg /*
262 1.1 mrg * do generic attach.
263 1.1 mrg */
264 1.35 mjacob ISP_UNLOCK(isp);
265 1.1 mrg isp_attach(isp);
266 1.1 mrg if (isp->isp_state != ISP_RUNSTATE) {
267 1.35 mjacob ISP_LOCK(isp);
268 1.1 mrg isp_uninit(isp);
269 1.35 mjacob ISP_UNLOCK(isp);
270 1.1 mrg }
271 1.49 mjacob }
272 1.49 mjacob
273 1.49 mjacob
274 1.49 mjacob static void
275 1.67.2.1 ad isp_sbus_reset0(struct ispsoftc *isp)
276 1.49 mjacob {
277 1.67.2.1 ad ISP_DISABLE_INTS(isp);
278 1.67.2.1 ad }
279 1.49 mjacob
280 1.67.2.1 ad static void
281 1.67.2.1 ad isp_sbus_reset1(struct ispsoftc *isp)
282 1.67.2.1 ad {
283 1.67.2.1 ad ISP_ENABLE_INTS(isp);
284 1.30 mjacob }
285 1.30 mjacob
286 1.30 mjacob static int
287 1.42 mjacob isp_sbus_intr(void *arg)
288 1.30 mjacob {
289 1.67.2.1 ad uint32_t isr;
290 1.67.2.1 ad uint16_t sema, mbox;
291 1.45 mjacob struct ispsoftc *isp = arg;
292 1.45 mjacob
293 1.48 mjacob if (ISP_READ_ISR(isp, &isr, &sema, &mbox) == 0) {
294 1.48 mjacob isp->isp_intbogus++;
295 1.48 mjacob return (0);
296 1.48 mjacob } else {
297 1.48 mjacob struct isp_sbussoftc *sbc = arg;
298 1.45 mjacob sbc->sbus_isp.isp_osinfo.onintstack = 1;
299 1.45 mjacob isp_intr(isp, isr, sema, mbox);
300 1.45 mjacob sbc->sbus_isp.isp_osinfo.onintstack = 0;
301 1.45 mjacob return (1);
302 1.45 mjacob }
303 1.45 mjacob }
304 1.45 mjacob
305 1.45 mjacob #define IspVirt2Off(a, x) \
306 1.45 mjacob (((struct isp_sbussoftc *)a)->sbus_poff[((x) & _BLK_REG_MASK) >> \
307 1.45 mjacob _BLK_REG_SHFT] + ((x) & 0xff))
308 1.45 mjacob
309 1.45 mjacob #define BXR2(sbc, off) \
310 1.45 mjacob bus_space_read_2(sbc->sbus_bustag, sbc->sbus_reg, off)
311 1.45 mjacob
312 1.45 mjacob static int
313 1.67.2.1 ad isp_sbus_rd_isr(struct ispsoftc *isp, uint32_t *isrp,
314 1.67.2.1 ad uint16_t *semap, uint16_t *mbp)
315 1.45 mjacob {
316 1.45 mjacob struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) isp;
317 1.67.2.1 ad uint32_t isr;
318 1.67.2.1 ad uint16_t sema;
319 1.45 mjacob
320 1.45 mjacob isr = BXR2(sbc, IspVirt2Off(isp, BIU_ISR));
321 1.45 mjacob sema = BXR2(sbc, IspVirt2Off(isp, BIU_SEMA));
322 1.45 mjacob isp_prt(isp, ISP_LOGDEBUG3, "ISR 0x%x SEMA 0x%x", isr, sema);
323 1.45 mjacob isr &= INT_PENDING_MASK(isp);
324 1.45 mjacob sema &= BIU_SEMA_LOCK;
325 1.45 mjacob if (isr == 0 && sema == 0) {
326 1.45 mjacob return (0);
327 1.45 mjacob }
328 1.45 mjacob *isrp = isr;
329 1.45 mjacob if ((*semap = sema) != 0) {
330 1.45 mjacob *mbp = BXR2(sbc, IspVirt2Off(isp, OUTMAILBOX0));
331 1.45 mjacob }
332 1.45 mjacob return (1);
333 1.1 mrg }
334 1.1 mrg
335 1.67.2.1 ad static uint32_t
336 1.42 mjacob isp_sbus_rd_reg(struct ispsoftc *isp, int regoff)
337 1.1 mrg {
338 1.1 mrg struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) isp;
339 1.8 mjacob int offset = sbc->sbus_poff[(regoff & _BLK_REG_MASK) >> _BLK_REG_SHFT];
340 1.8 mjacob offset += (regoff & 0xff);
341 1.6 mjacob return (bus_space_read_2(sbc->sbus_bustag, sbc->sbus_reg, offset));
342 1.1 mrg }
343 1.1 mrg
344 1.1 mrg static void
345 1.67.2.1 ad isp_sbus_wr_reg(struct ispsoftc *isp, int regoff, uint32_t val)
346 1.1 mrg {
347 1.1 mrg struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) isp;
348 1.8 mjacob int offset = sbc->sbus_poff[(regoff & _BLK_REG_MASK) >> _BLK_REG_SHFT];
349 1.8 mjacob offset += (regoff & 0xff);
350 1.6 mjacob bus_space_write_2(sbc->sbus_bustag, sbc->sbus_reg, offset, val);
351 1.1 mrg }
352 1.1 mrg
353 1.1 mrg static int
354 1.42 mjacob isp_sbus_mbxdma(struct ispsoftc *isp)
355 1.1 mrg {
356 1.1 mrg struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) isp;
357 1.40 mjacob bus_dma_segment_t reqseg, rspseg;
358 1.40 mjacob int reqrs, rsprs, i, progress;
359 1.14 mjacob size_t n;
360 1.12 pk bus_size_t len;
361 1.1 mrg
362 1.14 mjacob if (isp->isp_rquest_dma)
363 1.14 mjacob return (0);
364 1.1 mrg
365 1.44 mjacob n = isp->isp_maxcmds * sizeof (XS_T *);
366 1.29 mjacob isp->isp_xflist = (XS_T **) malloc(n, M_DEVBUF, M_WAITOK);
367 1.14 mjacob if (isp->isp_xflist == NULL) {
368 1.29 mjacob isp_prt(isp, ISP_LOGERR, "cannot alloc xflist array");
369 1.14 mjacob return (1);
370 1.14 mjacob }
371 1.48 mjacob MEMZERO(isp->isp_xflist, n);
372 1.14 mjacob n = sizeof (bus_dmamap_t) * isp->isp_maxcmds;
373 1.14 mjacob sbc->sbus_dmamap = (bus_dmamap_t *) malloc(n, M_DEVBUF, M_WAITOK);
374 1.14 mjacob if (sbc->sbus_dmamap == NULL) {
375 1.14 mjacob free(isp->isp_xflist, M_DEVBUF);
376 1.29 mjacob isp->isp_xflist = NULL;
377 1.29 mjacob isp_prt(isp, ISP_LOGERR, "cannot alloc dmamap array");
378 1.29 mjacob return (1);
379 1.29 mjacob }
380 1.29 mjacob for (i = 0; i < isp->isp_maxcmds; i++) {
381 1.29 mjacob /* Allocate a DMA handle */
382 1.48 mjacob if (bus_dmamap_create(isp->isp_dmatag, MAXPHYS, 1, MAXPHYS, 0,
383 1.29 mjacob BUS_DMA_NOWAIT, &sbc->sbus_dmamap[i]) != 0) {
384 1.29 mjacob isp_prt(isp, ISP_LOGERR, "cmd DMA maps create error");
385 1.29 mjacob break;
386 1.29 mjacob }
387 1.29 mjacob }
388 1.29 mjacob if (i < isp->isp_maxcmds) {
389 1.29 mjacob while (--i >= 0) {
390 1.48 mjacob bus_dmamap_destroy(isp->isp_dmatag,
391 1.48 mjacob sbc->sbus_dmamap[i]);
392 1.29 mjacob }
393 1.29 mjacob free(isp->isp_xflist, M_DEVBUF);
394 1.29 mjacob free(sbc->sbus_dmamap, M_DEVBUF);
395 1.29 mjacob isp->isp_xflist = NULL;
396 1.29 mjacob sbc->sbus_dmamap = NULL;
397 1.14 mjacob return (1);
398 1.14 mjacob }
399 1.29 mjacob
400 1.1 mrg /*
401 1.40 mjacob * Allocate and map the request and response queues
402 1.1 mrg */
403 1.40 mjacob progress = 0;
404 1.29 mjacob len = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
405 1.48 mjacob if (bus_dmamem_alloc(isp->isp_dmatag, len, 0, 0, &reqseg, 1, &reqrs,
406 1.40 mjacob BUS_DMA_NOWAIT)) {
407 1.24 pk goto dmafail;
408 1.24 pk }
409 1.40 mjacob progress++;
410 1.48 mjacob if (bus_dmamem_map(isp->isp_dmatag, &reqseg, reqrs, len,
411 1.67.2.1 ad (void *)&isp->isp_rquest, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
412 1.24 pk goto dmafail;
413 1.24 pk }
414 1.40 mjacob progress++;
415 1.48 mjacob if (bus_dmamap_create(isp->isp_dmatag, len, 1, len, 0, BUS_DMA_NOWAIT,
416 1.48 mjacob &isp->isp_rqdmap) != 0) {
417 1.43 mjacob goto dmafail;
418 1.43 mjacob }
419 1.43 mjacob progress++;
420 1.48 mjacob if (bus_dmamap_load(isp->isp_dmatag, isp->isp_rqdmap,
421 1.43 mjacob isp->isp_rquest, len, NULL, BUS_DMA_NOWAIT) != 0) {
422 1.24 pk goto dmafail;
423 1.14 mjacob }
424 1.40 mjacob progress++;
425 1.48 mjacob isp->isp_rquest_dma = isp->isp_rqdmap->dm_segs[0].ds_addr;
426 1.1 mrg
427 1.29 mjacob len = ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
428 1.48 mjacob if (bus_dmamem_alloc(isp->isp_dmatag, len, 0, 0, &rspseg, 1, &rsprs,
429 1.40 mjacob BUS_DMA_NOWAIT)) {
430 1.24 pk goto dmafail;
431 1.24 pk }
432 1.40 mjacob progress++;
433 1.48 mjacob if (bus_dmamem_map(isp->isp_dmatag, &rspseg, rsprs, len,
434 1.67.2.1 ad (void *)&isp->isp_result, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
435 1.24 pk goto dmafail;
436 1.24 pk }
437 1.40 mjacob progress++;
438 1.48 mjacob if (bus_dmamap_create(isp->isp_dmatag, len, 1, len, 0, BUS_DMA_NOWAIT,
439 1.48 mjacob &isp->isp_rsdmap) != 0) {
440 1.43 mjacob goto dmafail;
441 1.43 mjacob }
442 1.43 mjacob progress++;
443 1.48 mjacob if (bus_dmamap_load(isp->isp_dmatag, isp->isp_rsdmap,
444 1.43 mjacob isp->isp_result, len, NULL, BUS_DMA_NOWAIT) != 0) {
445 1.24 pk goto dmafail;
446 1.14 mjacob }
447 1.48 mjacob isp->isp_result_dma = isp->isp_rsdmap->dm_segs[0].ds_addr;
448 1.43 mjacob
449 1.1 mrg return (0);
450 1.24 pk
451 1.24 pk dmafail:
452 1.40 mjacob isp_prt(isp, ISP_LOGERR, "Mailbox DMA Setup Failure");
453 1.40 mjacob
454 1.40 mjacob if (progress >= 8) {
455 1.48 mjacob bus_dmamap_unload(isp->isp_dmatag, isp->isp_rsdmap);
456 1.41 thorpej }
457 1.41 thorpej if (progress >= 7) {
458 1.48 mjacob bus_dmamap_destroy(isp->isp_dmatag, isp->isp_rsdmap);
459 1.43 mjacob }
460 1.43 mjacob if (progress >= 6) {
461 1.48 mjacob bus_dmamem_unmap(isp->isp_dmatag,
462 1.40 mjacob isp->isp_result, ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp)));
463 1.40 mjacob }
464 1.43 mjacob if (progress >= 5) {
465 1.48 mjacob bus_dmamem_free(isp->isp_dmatag, &rspseg, rsprs);
466 1.40 mjacob }
467 1.40 mjacob
468 1.40 mjacob if (progress >= 4) {
469 1.48 mjacob bus_dmamap_unload(isp->isp_dmatag, isp->isp_rqdmap);
470 1.41 thorpej }
471 1.41 thorpej if (progress >= 3) {
472 1.48 mjacob bus_dmamap_destroy(isp->isp_dmatag, isp->isp_rqdmap);
473 1.43 mjacob }
474 1.43 mjacob if (progress >= 2) {
475 1.48 mjacob bus_dmamem_unmap(isp->isp_dmatag,
476 1.40 mjacob isp->isp_rquest, ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp)));
477 1.40 mjacob }
478 1.43 mjacob if (progress >= 1) {
479 1.48 mjacob bus_dmamem_free(isp->isp_dmatag, &reqseg, reqrs);
480 1.40 mjacob }
481 1.40 mjacob
482 1.29 mjacob for (i = 0; i < isp->isp_maxcmds; i++) {
483 1.48 mjacob bus_dmamap_destroy(isp->isp_dmatag, sbc->sbus_dmamap[i]);
484 1.29 mjacob }
485 1.24 pk free(sbc->sbus_dmamap, M_DEVBUF);
486 1.24 pk free(isp->isp_xflist, M_DEVBUF);
487 1.29 mjacob isp->isp_xflist = NULL;
488 1.29 mjacob sbc->sbus_dmamap = NULL;
489 1.24 pk return (1);
490 1.1 mrg }
491 1.1 mrg
492 1.1 mrg /*
493 1.14 mjacob * Map a DMA request.
494 1.14 mjacob * We're guaranteed that rq->req_handle is a value from 1 to isp->isp_maxcmds.
495 1.1 mrg */
496 1.1 mrg
497 1.1 mrg static int
498 1.42 mjacob isp_sbus_dmasetup(struct ispsoftc *isp, XS_T *xs, ispreq_t *rq,
499 1.67.2.1 ad uint32_t *nxtip, uint32_t optr)
500 1.1 mrg {
501 1.1 mrg struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) isp;
502 1.27 mjacob bus_dmamap_t dmap;
503 1.48 mjacob ispreq_t *qep;
504 1.53 mjacob int error, cansleep = (xs->xs_control & XS_CTL_NOSLEEP) == 0;
505 1.14 mjacob int in = (xs->xs_control & XS_CTL_DATA_IN) != 0;
506 1.1 mrg
507 1.48 mjacob qep = (ispreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, isp->isp_reqidx);
508 1.1 mrg if (xs->datalen == 0) {
509 1.1 mrg rq->req_seg_count = 1;
510 1.14 mjacob goto mbxsync;
511 1.1 mrg }
512 1.27 mjacob
513 1.27 mjacob dmap = sbc->sbus_dmamap[isp_handle_index(rq->req_handle)];
514 1.27 mjacob if (dmap->dm_nsegs != 0) {
515 1.60 wiz panic("%s: DMA map already allocated", isp->isp_name);
516 1.1 mrg /* NOTREACHED */
517 1.1 mrg }
518 1.53 mjacob error = bus_dmamap_load(isp->isp_dmatag, dmap, xs->data, xs->datalen,
519 1.41 thorpej NULL, (cansleep ? BUS_DMA_WAITOK : BUS_DMA_NOWAIT) |
520 1.53 mjacob BUS_DMA_STREAMING);
521 1.53 mjacob if (error != 0) {
522 1.1 mrg XS_SETERR(xs, HBA_BOTCH);
523 1.52 mjacob if (error == EAGAIN || error == ENOMEM)
524 1.52 mjacob return (CMD_EAGAIN);
525 1.52 mjacob else
526 1.52 mjacob return (CMD_COMPLETE);
527 1.1 mrg }
528 1.27 mjacob
529 1.48 mjacob bus_dmamap_sync(isp->isp_dmatag, dmap, 0, xs->datalen,
530 1.37 mjacob in? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
531 1.27 mjacob
532 1.14 mjacob if (in) {
533 1.1 mrg rq->req_flags |= REQFLAG_DATA_IN;
534 1.1 mrg } else {
535 1.1 mrg rq->req_flags |= REQFLAG_DATA_OUT;
536 1.1 mrg }
537 1.22 mjacob
538 1.27 mjacob if (XS_CDBLEN(xs) > 12) {
539 1.67.2.1 ad uint32_t onxti;
540 1.48 mjacob ispcontreq_t local, *crq = &local, *cqe;
541 1.48 mjacob
542 1.48 mjacob onxti = *nxtip;
543 1.48 mjacob cqe = (ispcontreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, onxti);
544 1.48 mjacob *nxtip = ISP_NXT_QENTRY(onxti, RQUEST_QUEUE_LEN(isp));
545 1.48 mjacob if (*nxtip == optr) {
546 1.29 mjacob isp_prt(isp, ISP_LOGDEBUG0, "Request Queue Overflow++");
547 1.48 mjacob bus_dmamap_unload(isp->isp_dmatag, dmap);
548 1.27 mjacob XS_SETERR(xs, HBA_BOTCH);
549 1.27 mjacob return (CMD_EAGAIN);
550 1.27 mjacob }
551 1.22 mjacob rq->req_seg_count = 2;
552 1.48 mjacob MEMZERO((void *)crq, sizeof (*crq));
553 1.22 mjacob crq->req_header.rqs_entry_count = 1;
554 1.63 perry crq->req_header.rqs_entry_type = RQSTYPE_DATASEG;
555 1.22 mjacob crq->req_dataseg[0].ds_count = xs->datalen;
556 1.48 mjacob crq->req_dataseg[0].ds_base = dmap->dm_segs[0].ds_addr;
557 1.48 mjacob isp_put_cont_req(isp, crq, cqe);
558 1.48 mjacob MEMORYBARRIER(isp, SYNC_REQUEST, onxti, QENTRY_LEN);
559 1.22 mjacob } else {
560 1.48 mjacob rq->req_seg_count = 1;
561 1.22 mjacob rq->req_dataseg[0].ds_count = xs->datalen;
562 1.27 mjacob rq->req_dataseg[0].ds_base = dmap->dm_segs[0].ds_addr;
563 1.22 mjacob }
564 1.22 mjacob
565 1.14 mjacob mbxsync:
566 1.48 mjacob if (XS_CDBLEN(xs) > 12) {
567 1.48 mjacob isp_put_extended_request(isp,
568 1.48 mjacob (ispextreq_t *)rq, (ispextreq_t *) qep);
569 1.48 mjacob } else {
570 1.48 mjacob isp_put_request(isp, rq, qep);
571 1.48 mjacob }
572 1.3 mjacob return (CMD_QUEUED);
573 1.1 mrg }
574 1.1 mrg
575 1.1 mrg static void
576 1.67.2.1 ad isp_sbus_dmateardown(struct ispsoftc *isp, XS_T *xs, uint32_t handle)
577 1.1 mrg {
578 1.1 mrg struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) isp;
579 1.27 mjacob bus_dmamap_t dmap;
580 1.27 mjacob
581 1.27 mjacob dmap = sbc->sbus_dmamap[isp_handle_index(handle)];
582 1.27 mjacob
583 1.27 mjacob if (dmap->dm_nsegs == 0) {
584 1.60 wiz panic("%s: DMA map not already allocated", isp->isp_name);
585 1.1 mrg /* NOTREACHED */
586 1.1 mrg }
587 1.48 mjacob bus_dmamap_sync(isp->isp_dmatag, dmap, 0,
588 1.14 mjacob xs->datalen, (xs->xs_control & XS_CTL_DATA_IN)?
589 1.14 mjacob BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
590 1.48 mjacob bus_dmamap_unload(isp->isp_dmatag, dmap);
591 1.1 mrg }
592