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