uha.c revision 1.29 1 /* $NetBSD: uha.c,v 1.29 2001/07/19 16:25:26 thorpej Exp $ */
2
3 #undef UHADEBUG
4 #ifdef DDB
5 #define integrate
6 #else
7 #define integrate static inline
8 #endif
9
10 /*-
11 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
12 * All rights reserved.
13 *
14 * This code is derived from software contributed to The NetBSD Foundation
15 * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
16 * Simulation Facility, NASA Ames Research Center.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. All advertising materials mentioning features or use of this software
27 * must display the following acknowledgement:
28 * This product includes software developed by the NetBSD
29 * Foundation, Inc. and its contributors.
30 * 4. Neither the name of The NetBSD Foundation nor the names of its
31 * contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
35 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
36 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
37 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
38 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
39 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
40 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
42 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
43 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44 * POSSIBILITY OF SUCH DAMAGE.
45 */
46
47 /*
48 * Ported for use with the UltraStor 14f by Gary Close (gclose (at) wvnvms.wvnet.edu)
49 * Slight fixes to timeouts to run with the 34F
50 * Thanks to Julian Elischer for advice and help with this port.
51 *
52 * Originally written by Julian Elischer (julian (at) tfs.com)
53 * for TRW Financial Systems for use under the MACH(2.5) operating system.
54 *
55 * TRW Financial Systems, in accordance with their agreement with Carnegie
56 * Mellon University, makes this software available to CMU to distribute
57 * or use in any manner that they see fit as long as this message is kept with
58 * the software. For this reason TFS also grants any other persons or
59 * organisations permission to use or modify this software.
60 *
61 * TFS supplies this software to be publicly redistributed
62 * on the understanding that TFS is not responsible for the correct
63 * functioning of this software in any circumstances.
64 *
65 * commenced: Sun Sep 27 18:14:01 PDT 1992
66 * slight mod to make work with 34F as well: Wed Jun 2 18:05:48 WST 1993
67 */
68
69 #include <sys/types.h>
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/kernel.h>
73 #include <sys/errno.h>
74 #include <sys/ioctl.h>
75 #include <sys/device.h>
76 #include <sys/malloc.h>
77 #include <sys/buf.h>
78 #include <sys/proc.h>
79 #include <sys/user.h>
80
81 #include <uvm/uvm_extern.h>
82
83 #include <machine/bus.h>
84 #include <machine/intr.h>
85
86 #include <dev/scsipi/scsi_all.h>
87 #include <dev/scsipi/scsipi_all.h>
88 #include <dev/scsipi/scsiconf.h>
89
90 #include <dev/ic/uhareg.h>
91 #include <dev/ic/uhavar.h>
92
93 #ifndef DDB
94 #define Debugger() panic("should call debugger here (uha.c)")
95 #endif /* ! DDB */
96
97 #define UHA_MAXXFER ((UHA_NSEG - 1) << PGSHIFT)
98
99 integrate void uha_reset_mscp __P((struct uha_softc *, struct uha_mscp *));
100 void uha_free_mscp __P((struct uha_softc *, struct uha_mscp *));
101 integrate int uha_init_mscp __P((struct uha_softc *, struct uha_mscp *));
102 struct uha_mscp *uha_get_mscp __P((struct uha_softc *));
103 void uhaminphys __P((struct buf *));
104 void uha_scsipi_request __P((struct scsipi_channel *,
105 scsipi_adapter_req_t, void *));
106 int uha_create_mscps __P((struct uha_softc *, struct uha_mscp *, int));
107
108 #define UHA_ABORT_TIMEOUT 2000 /* time to wait for abort (mSec) */
109
110 /*
111 * Attach all the sub-devices we can find
112 */
113 void
114 uha_attach(sc, upd)
115 struct uha_softc *sc;
116 struct uha_probe_data *upd;
117 {
118 struct scsipi_adapter *adapt = &sc->sc_adapter;
119 struct scsipi_channel *chan = &sc->sc_channel;
120 bus_dma_segment_t seg;
121 int i, error, rseg;
122
123 TAILQ_INIT(&sc->sc_free_mscp);
124
125 (sc->init)(sc);
126
127 /*
128 * Fill in the scsipi_adapter.
129 */
130 memset(adapt, 0, sizeof(*adapt));
131 adapt->adapt_dev = &sc->sc_dev;
132 adapt->adapt_nchannels = 1;
133 /* adapt_openings initialized below */
134 /* adapt_max_periph initialized below */
135 adapt->adapt_request = uha_scsipi_request;
136 adapt->adapt_minphys = uhaminphys;
137
138 /*
139 * Fill in the scsipi_channel.
140 */
141 memset(chan, 0, sizeof(*chan));
142 chan->chan_adapter = adapt;
143 chan->chan_bustype = &scsi_bustype;
144 chan->chan_channel = 0;
145 chan->chan_ntargets = 8;
146 chan->chan_nluns = 8;
147 chan->chan_id = upd->sc_scsi_dev;
148
149 #define MSCPSIZE (UHA_MSCP_MAX * sizeof(struct uha_mscp))
150
151 /*
152 * Allocate the MSCPs.
153 */
154 if ((error = bus_dmamem_alloc(sc->sc_dmat, MSCPSIZE,
155 PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
156 printf("%s: unable to allocate mscps, error = %d\n",
157 sc->sc_dev.dv_xname, error);
158 return;
159 }
160 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
161 MSCPSIZE, (caddr_t *)&sc->sc_mscps,
162 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
163 printf("%s: unable to map mscps, error = %d\n",
164 sc->sc_dev.dv_xname, error);
165 return;
166 }
167
168 /*
169 * Create and load the DMA map used for the mscps.
170 */
171 if ((error = bus_dmamap_create(sc->sc_dmat, MSCPSIZE,
172 1, MSCPSIZE, 0, BUS_DMA_NOWAIT | sc->sc_dmaflags,
173 &sc->sc_dmamap_mscp)) != 0) {
174 printf("%s: unable to create mscp DMA map, error = %d\n",
175 sc->sc_dev.dv_xname, error);
176 return;
177 }
178 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_mscp,
179 sc->sc_mscps, MSCPSIZE, NULL, BUS_DMA_NOWAIT)) != 0) {
180 printf("%s: unable to load mscp DMA map, error = %d\n",
181 sc->sc_dev.dv_xname, error);
182 return;
183 }
184
185 #undef MSCPSIZE
186
187 /*
188 * Initialize the mscps.
189 */
190 i = uha_create_mscps(sc, sc->sc_mscps, UHA_MSCP_MAX);
191 if (i == 0) {
192 printf("%s: unable to create mscps\n",
193 sc->sc_dev.dv_xname);
194 return;
195 } else if (i != UHA_MSCP_MAX) {
196 printf("%s: WARNING: only %d of %d mscps created\n",
197 sc->sc_dev.dv_xname, i, UHA_MSCP_MAX);
198 }
199
200 adapt->adapt_openings = i;
201 adapt->adapt_max_periph = adapt->adapt_openings;
202
203 /*
204 * ask the adapter what subunits are present
205 */
206 config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
207 }
208
209 integrate void
210 uha_reset_mscp(sc, mscp)
211 struct uha_softc *sc;
212 struct uha_mscp *mscp;
213 {
214
215 mscp->flags = 0;
216 }
217
218 /*
219 * A mscp (and hence a mbx-out) is put onto the free list.
220 */
221 void
222 uha_free_mscp(sc, mscp)
223 struct uha_softc *sc;
224 struct uha_mscp *mscp;
225 {
226 int s;
227
228 s = splbio();
229 uha_reset_mscp(sc, mscp);
230 TAILQ_INSERT_HEAD(&sc->sc_free_mscp, mscp, chain);
231 splx(s);
232 }
233
234 integrate int
235 uha_init_mscp(sc, mscp)
236 struct uha_softc *sc;
237 struct uha_mscp *mscp;
238 {
239 bus_dma_tag_t dmat = sc->sc_dmat;
240 int hashnum, error;
241
242 /*
243 * Create the DMA map for this MSCP.
244 */
245 error = bus_dmamap_create(dmat, UHA_MAXXFER, UHA_NSEG, UHA_MAXXFER,
246 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW | sc->sc_dmaflags,
247 &mscp->dmamap_xfer);
248 if (error) {
249 printf("%s: can't create mscp DMA map, error = %d\n",
250 sc->sc_dev.dv_xname, error);
251 return (error);
252 }
253
254 /*
255 * put in the phystokv hash table
256 * Never gets taken out.
257 */
258 mscp->hashkey = sc->sc_dmamap_mscp->dm_segs[0].ds_addr +
259 UHA_MSCP_OFF(mscp);
260 hashnum = MSCP_HASH(mscp->hashkey);
261 mscp->nexthash = sc->sc_mscphash[hashnum];
262 sc->sc_mscphash[hashnum] = mscp;
263 uha_reset_mscp(sc, mscp);
264 return (0);
265 }
266
267 /*
268 * Create a set of MSCPs and add them to the free list.
269 */
270 int
271 uha_create_mscps(sc, mscpstore, count)
272 struct uha_softc *sc;
273 struct uha_mscp *mscpstore;
274 int count;
275 {
276 struct uha_mscp *mscp;
277 int i, error;
278
279 memset(mscpstore, 0, sizeof(struct uha_mscp) * count);
280 for (i = 0; i < count; i++) {
281 mscp = &mscpstore[i];
282 if ((error = uha_init_mscp(sc, mscp)) != 0) {
283 printf("%s: unable to initialize mscp, error = %d\n",
284 sc->sc_dev.dv_xname, error);
285 goto out;
286 }
287 TAILQ_INSERT_TAIL(&sc->sc_free_mscp, mscp, chain);
288 }
289 out:
290 return (i);
291 }
292
293 /*
294 * Get a free mscp
295 *
296 * If there are none, see if we can allocate a new one. If so, put it in the
297 * hash table too otherwise either return an error or sleep.
298 */
299 struct uha_mscp *
300 uha_get_mscp(sc)
301 struct uha_softc *sc;
302 {
303 struct uha_mscp *mscp;
304 int s;
305
306 s = splbio();
307 mscp = TAILQ_FIRST(&sc->sc_free_mscp);
308 if (mscp != NULL) {
309 TAILQ_REMOVE(&sc->sc_free_mscp, mscp, chain);
310 mscp->flags |= MSCP_ALLOC;
311 }
312 splx(s);
313 return (mscp);
314 }
315
316 /*
317 * given a physical address, find the mscp that it corresponds to.
318 */
319 struct uha_mscp *
320 uha_mscp_phys_kv(sc, mscp_phys)
321 struct uha_softc *sc;
322 u_long mscp_phys;
323 {
324 int hashnum = MSCP_HASH(mscp_phys);
325 struct uha_mscp *mscp = sc->sc_mscphash[hashnum];
326
327 while (mscp) {
328 if (mscp->hashkey == mscp_phys)
329 break;
330 mscp = mscp->nexthash;
331 }
332 return (mscp);
333 }
334
335 /*
336 * We have a mscp which has been processed by the adaptor, now we look to see
337 * how the operation went.
338 */
339 void
340 uha_done(sc, mscp)
341 struct uha_softc *sc;
342 struct uha_mscp *mscp;
343 {
344 bus_dma_tag_t dmat = sc->sc_dmat;
345 struct scsipi_sense_data *s1, *s2;
346 struct scsipi_xfer *xs = mscp->xs;
347
348 SC_DEBUG(xs->xs_periph, SCSIPI_DB2, ("uha_done\n"));
349
350 bus_dmamap_sync(dmat, sc->sc_dmamap_mscp,
351 UHA_MSCP_OFF(mscp), sizeof(struct uha_mscp),
352 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
353
354 /*
355 * If we were a data transfer, unload the map that described
356 * the data buffer.
357 */
358 if (xs->datalen) {
359 bus_dmamap_sync(dmat, mscp->dmamap_xfer, 0,
360 mscp->dmamap_xfer->dm_mapsize,
361 (xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_POSTREAD :
362 BUS_DMASYNC_POSTWRITE);
363 bus_dmamap_unload(dmat, mscp->dmamap_xfer);
364 }
365
366 /*
367 * Otherwise, put the results of the operation
368 * into the xfer and call whoever started it
369 */
370 if ((mscp->flags & MSCP_ALLOC) == 0) {
371 printf("%s: exiting ccb not allocated!\n", sc->sc_dev.dv_xname);
372 Debugger();
373 return;
374 }
375 if (xs->error == XS_NOERROR) {
376 if (mscp->host_stat != UHA_NO_ERR) {
377 switch (mscp->host_stat) {
378 case UHA_SBUS_TIMEOUT: /* No response */
379 xs->error = XS_SELTIMEOUT;
380 break;
381 default: /* Other scsi protocol messes */
382 printf("%s: host_stat %x\n",
383 sc->sc_dev.dv_xname, mscp->host_stat);
384 xs->error = XS_DRIVER_STUFFUP;
385 }
386 } else if (mscp->target_stat != SCSI_OK) {
387 switch (mscp->target_stat) {
388 case SCSI_CHECK:
389 s1 = &mscp->mscp_sense;
390 s2 = &xs->sense.scsi_sense;
391 *s2 = *s1;
392 xs->error = XS_SENSE;
393 break;
394 case SCSI_BUSY:
395 xs->error = XS_BUSY;
396 break;
397 default:
398 printf("%s: target_stat %x\n",
399 sc->sc_dev.dv_xname, mscp->target_stat);
400 xs->error = XS_DRIVER_STUFFUP;
401 }
402 } else
403 xs->resid = 0;
404 }
405 uha_free_mscp(sc, mscp);
406 scsipi_done(xs);
407 }
408
409 void
410 uhaminphys(bp)
411 struct buf *bp;
412 {
413
414 if (bp->b_bcount > UHA_MAXXFER)
415 bp->b_bcount = UHA_MAXXFER;
416 minphys(bp);
417 }
418
419 /*
420 * start a scsi operation given the command and the data address. Also
421 * needs the unit, target and lu.
422 */
423
424 void
425 uha_scsipi_request(chan, req, arg)
426 struct scsipi_channel *chan;
427 scsipi_adapter_req_t req;
428 void *arg;
429 {
430 struct scsipi_xfer *xs;
431 struct scsipi_periph *periph;
432 struct uha_softc *sc = (void *)chan->chan_adapter->adapt_dev;
433 bus_dma_tag_t dmat = sc->sc_dmat;
434 struct uha_mscp *mscp;
435 struct uha_dma_seg *sg;
436 int error, seg, flags, s;
437
438
439 switch (req) {
440 case ADAPTER_REQ_RUN_XFER:
441 xs = arg;
442 periph = xs->xs_periph;
443 flags = xs->xs_control;
444
445 SC_DEBUG(periph, SCSIPI_DB2, ("uha_scsipi_request\n"));
446
447 /* Get an MSCP to use. */
448 mscp = uha_get_mscp(sc);
449 #ifdef DIAGNOSTIC
450 /*
451 * This should never happen as we track the resources
452 * in the mid-layer.
453 */
454 if (mscp == NULL) {
455 scsipi_printaddr(periph);
456 printf("unable to allocate mscp\n");
457 panic("uha_scsipi_request");
458 }
459 #endif
460
461 mscp->xs = xs;
462 mscp->timeout = xs->timeout;
463
464 /*
465 * Put all the arguments for the xfer in the mscp
466 */
467 if (flags & XS_CTL_RESET) {
468 mscp->opcode = UHA_SDR;
469 mscp->ca = 0x01;
470 } else {
471 mscp->opcode = UHA_TSP;
472 /* XXX Not for tapes. */
473 mscp->ca = 0x01;
474 memcpy(&mscp->scsi_cmd, xs->cmd, mscp->scsi_cmd_length);
475 }
476 mscp->xdir = UHA_SDET;
477 mscp->dcn = 0x00;
478 mscp->chan = 0x00;
479 mscp->target = periph->periph_target;
480 mscp->lun = periph->periph_lun;
481 mscp->scsi_cmd_length = xs->cmdlen;
482 mscp->sense_ptr = sc->sc_dmamap_mscp->dm_segs[0].ds_addr +
483 UHA_MSCP_OFF(mscp) + offsetof(struct uha_mscp, mscp_sense);
484 mscp->req_sense_length = sizeof(mscp->mscp_sense);
485 mscp->host_stat = 0x00;
486 mscp->target_stat = 0x00;
487
488 if (xs->datalen) {
489 sg = mscp->uha_dma;
490 seg = 0;
491 #ifdef TFS
492 if (flags & SCSI_DATA_UIO) {
493 error = bus_dmamap_load_uio(dmat,
494 mscp->dmamap_xfer, (struct uio *)xs->data,
495 ((flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT :
496 BUS_DMA_WAITOK) | BUS_DMA_STREAMING |
497 ((flags & XS_CTL_DATA_IN) ? BUS_DMA_READ :
498 BUS_DMA_WRITE));
499 } else
500 #endif /*TFS */
501 {
502 error = bus_dmamap_load(dmat,
503 mscp->dmamap_xfer, xs->data, xs->datalen,
504 NULL,
505 ((flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT :
506 BUS_DMA_WAITOK) | BUS_DMA_STREAMING |
507 ((flags & XS_CTL_DATA_IN) ? BUS_DMA_READ :
508 BUS_DMA_WRITE));
509 }
510
511 switch (error) {
512 case 0:
513 break;
514
515 case ENOMEM:
516 case EAGAIN:
517 xs->error = XS_RESOURCE_SHORTAGE;
518 goto out_bad;
519
520 default:
521 xs->error = XS_DRIVER_STUFFUP;
522 printf("%s: error %d loading DMA map\n",
523 sc->sc_dev.dv_xname, error);
524 out_bad:
525 uha_free_mscp(sc, mscp);
526 scsipi_done(xs);
527 return;
528 }
529
530 bus_dmamap_sync(dmat, mscp->dmamap_xfer, 0,
531 mscp->dmamap_xfer->dm_mapsize,
532 (flags & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD :
533 BUS_DMASYNC_PREWRITE);
534
535 /*
536 * Load the hardware scatter/gather map with the
537 * contents of the DMA map.
538 */
539 for (seg = 0;
540 seg < mscp->dmamap_xfer->dm_nsegs; seg++) {
541 mscp->uha_dma[seg].seg_addr =
542 mscp->dmamap_xfer->dm_segs[seg].ds_addr;
543 mscp->uha_dma[seg].seg_len =
544 mscp->dmamap_xfer->dm_segs[seg].ds_len;
545 }
546
547 mscp->data_addr =
548 sc->sc_dmamap_mscp->dm_segs[0].ds_addr +
549 UHA_MSCP_OFF(mscp) + offsetof(struct uha_mscp,
550 uha_dma);
551 mscp->data_length = xs->datalen;
552 mscp->sgth = 0x01;
553 mscp->sg_num = seg;
554 } else { /* No data xfer, use non S/G values */
555 mscp->data_addr = (physaddr)0;
556 mscp->data_length = 0;
557 mscp->sgth = 0x00;
558 mscp->sg_num = 0;
559 }
560 mscp->link_id = 0;
561 mscp->link_addr = (physaddr)0;
562
563 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_mscp,
564 UHA_MSCP_OFF(mscp), sizeof(struct uha_mscp),
565 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
566
567 s = splbio();
568 (sc->start_mbox)(sc, mscp);
569 splx(s);
570
571 if ((flags & XS_CTL_POLL) == 0)
572 return;
573
574 /*
575 * If we can't use interrupts, poll on completion
576 */
577 if ((sc->poll)(sc, xs, mscp->timeout)) {
578 uha_timeout(mscp);
579 if ((sc->poll)(sc, xs, mscp->timeout))
580 uha_timeout(mscp);
581 }
582 return;
583
584 case ADAPTER_REQ_GROW_RESOURCES:
585 /* XXX Not supported. */
586 return;
587
588 case ADAPTER_REQ_SET_XFER_MODE:
589 /*
590 * We can't really do this (the UltraStor controllers
591 * have their own config).
592 *
593 * XXX How do we query the config?
594 */
595 return;
596 }
597 }
598 void
599 uha_timeout(arg)
600 void *arg;
601 {
602 struct uha_mscp *mscp = arg;
603 struct scsipi_xfer *xs = mscp->xs;
604 struct scsipi_periph *periph = xs->xs_periph;
605 struct uha_softc *sc =
606 (void *)periph->periph_channel->chan_adapter->adapt_dev;
607 int s;
608
609 scsipi_printaddr(periph);
610 printf("timed out");
611
612 s = splbio();
613
614 if (mscp->flags & MSCP_ABORT) {
615 /* abort timed out */
616 printf(" AGAIN\n");
617 /* XXX Must reset! */
618 } else {
619 /* abort the operation that has timed out */
620 printf("\n");
621 mscp->xs->error = XS_TIMEOUT;
622 mscp->timeout = UHA_ABORT_TIMEOUT;
623 mscp->flags |= MSCP_ABORT;
624 (sc->start_mbox)(sc, mscp);
625 }
626
627 splx(s);
628 }
629