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