FreeBSD/Linux Kernel Cross Reference
sys/dev/ic/uha.c
1 /* $NetBSD: uha.c,v 1.35 2005/02/21 00:29:07 thorpej 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@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@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.35 2005/02/21 00:29:07 thorpej 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 <machine/bus.h>
86 #include <machine/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, (caddr_t *)&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(sc, mscp)
212 struct uha_softc *sc;
213 struct uha_mscp *mscp;
214 {
215
216 mscp->flags = 0;
217 }
218
219 /*
220 * A mscp (and hence a mbx-out) is put onto the free list.
221 */
222 void
223 uha_free_mscp(sc, mscp)
224 struct uha_softc *sc;
225 struct uha_mscp *mscp;
226 {
227 int s;
228
229 s = splbio();
230 uha_reset_mscp(sc, mscp);
231 TAILQ_INSERT_HEAD(&sc->sc_free_mscp, mscp, chain);
232 splx(s);
233 }
234
235 integrate int
236 uha_init_mscp(sc, mscp)
237 struct uha_softc *sc;
238 struct uha_mscp *mscp;
239 {
240 bus_dma_tag_t dmat = sc->sc_dmat;
241 int hashnum, error;
242
243 /*
244 * Create the DMA map for this MSCP.
245 */
246 error = 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 if (error) {
250 printf("%s: can't create mscp DMA map, error = %d\n",
251 sc->sc_dev.dv_xname, error);
252 return (error);
253 }
254
255 /*
256 * put in the phystokv hash table
257 * Never gets taken out.
258 */
259 mscp->hashkey = sc->sc_dmamap_mscp->dm_segs[0].ds_addr +
260 UHA_MSCP_OFF(mscp);
261 hashnum = MSCP_HASH(mscp->hashkey);
262 mscp->nexthash = sc->sc_mscphash[hashnum];
263 sc->sc_mscphash[hashnum] = mscp;
264 uha_reset_mscp(sc, mscp);
265 return (0);
266 }
267
268 /*
269 * Create a set of MSCPs and add them to the free list.
270 */
271 int
272 uha_create_mscps(sc, mscpstore, count)
273 struct uha_softc *sc;
274 struct uha_mscp *mscpstore;
275 int count;
276 {
277 struct uha_mscp *mscp;
278 int i, error;
279
280 memset(mscpstore, 0, sizeof(struct uha_mscp) * count);
281 for (i = 0; i < count; i++) {
282 mscp = &mscpstore[i];
283 if ((error = uha_init_mscp(sc, mscp)) != 0) {
284 printf("%s: unable to initialize mscp, error = %d\n",
285 sc->sc_dev.dv_xname, error);
286 goto out;
287 }
288 TAILQ_INSERT_TAIL(&sc->sc_free_mscp, mscp, chain);
289 }
290 out:
291 return (i);
292 }
293
294 /*
295 * Get a free mscp
296 *
297 * If there are none, see if we can allocate a new one. If so, put it in the
298 * hash table too otherwise either return an error or sleep.
299 */
300 struct uha_mscp *
301 uha_get_mscp(sc)
302 struct uha_softc *sc;
303 {
304 struct uha_mscp *mscp;
305 int s;
306
307 s = splbio();
308 mscp = TAILQ_FIRST(&sc->sc_free_mscp);
309 if (mscp != NULL) {
310 TAILQ_REMOVE(&sc->sc_free_mscp, mscp, chain);
311 mscp->flags |= MSCP_ALLOC;
312 }
313 splx(s);
314 return (mscp);
315 }
316
317 /*
318 * given a physical address, find the mscp that it corresponds to.
319 */
320 struct uha_mscp *
321 uha_mscp_phys_kv(sc, mscp_phys)
322 struct uha_softc *sc;
323 u_long mscp_phys;
324 {
325 int hashnum = MSCP_HASH(mscp_phys);
326 struct uha_mscp *mscp = sc->sc_mscphash[hashnum];
327
328 while (mscp) {
329 if (mscp->hashkey == mscp_phys)
330 break;
331 mscp = mscp->nexthash;
332 }
333 return (mscp);
334 }
335
336 /*
337 * We have a mscp which has been processed by the adaptor, now we look to see
338 * how the operation went.
339 */
340 void
341 uha_done(sc, mscp)
342 struct uha_softc *sc;
343 struct uha_mscp *mscp;
344 {
345 bus_dma_tag_t dmat = sc->sc_dmat;
346 struct scsi_sense_data *s1, *s2;
347 struct scsipi_xfer *xs = mscp->xs;
348
349 SC_DEBUG(xs->xs_periph, SCSIPI_DB2, ("uha_done\n"));
350
351 bus_dmamap_sync(dmat, sc->sc_dmamap_mscp,
352 UHA_MSCP_OFF(mscp), sizeof(struct uha_mscp),
353 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
354
355 /*
356 * If we were a data transfer, unload the map that described
357 * the data buffer.
358 */
359 if (xs->datalen) {
360 bus_dmamap_sync(dmat, mscp->dmamap_xfer, 0,
361 mscp->dmamap_xfer->dm_mapsize,
362 (xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_POSTREAD :
363 BUS_DMASYNC_POSTWRITE);
364 bus_dmamap_unload(dmat, mscp->dmamap_xfer);
365 }
366
367 /*
368 * Otherwise, put the results of the operation
369 * into the xfer and call whoever started it
370 */
371 if ((mscp->flags & MSCP_ALLOC) == 0) {
372 printf("%s: exiting ccb not allocated!\n", sc->sc_dev.dv_xname);
373 Debugger();
374 return;
375 }
376 if (xs->error == XS_NOERROR) {
377 if (mscp->host_stat != UHA_NO_ERR) {
378 switch (mscp->host_stat) {
379 case UHA_SBUS_TIMEOUT: /* No response */
380 xs->error = XS_SELTIMEOUT;
381 break;
382 default: /* Other scsi protocol messes */
383 printf("%s: host_stat %x\n",
384 sc->sc_dev.dv_xname, mscp->host_stat);
385 xs->error = XS_DRIVER_STUFFUP;
386 }
387 } else if (mscp->target_stat != SCSI_OK) {
388 switch (mscp->target_stat) {
389 case SCSI_CHECK:
390 s1 = &mscp->mscp_sense;
391 s2 = &xs->sense.scsi_sense;
392 *s2 = *s1;
393 xs->error = XS_SENSE;
394 break;
395 case SCSI_BUSY:
396 xs->error = XS_BUSY;
397 break;
398 default:
399 printf("%s: target_stat %x\n",
400 sc->sc_dev.dv_xname, mscp->target_stat);
401 xs->error = XS_DRIVER_STUFFUP;
402 }
403 } else
404 xs->resid = 0;
405 }
406 uha_free_mscp(sc, mscp);
407 scsipi_done(xs);
408 }
409
410 void
411 uhaminphys(bp)
412 struct buf *bp;
413 {
414
415 if (bp->b_bcount > UHA_MAXXFER)
416 bp->b_bcount = UHA_MAXXFER;
417 minphys(bp);
418 }
419
420 /*
421 * start a scsi operation given the command and the data address. Also
422 * needs the unit, target and lu.
423 */
424
425 void
426 uha_scsipi_request(chan, req, arg)
427 struct scsipi_channel *chan;
428 scsipi_adapter_req_t req;
429 void *arg;
430 {
431 struct scsipi_xfer *xs;
432 struct scsipi_periph *periph;
433 struct uha_softc *sc = (void *)chan->chan_adapter->adapt_dev;
434 bus_dma_tag_t dmat = sc->sc_dmat;
435 struct uha_mscp *mscp;
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 if (xs->cmdlen > sizeof(mscp->scsi_cmd)) {
472 printf("%s: cmdlen %d too large for MSCP\n",
473 sc->sc_dev.dv_xname, xs->cmdlen);
474 xs->error = XS_DRIVER_STUFFUP;
475 goto out_bad;
476 }
477 mscp->opcode = UHA_TSP;
478 /* XXX Not for tapes. */
479 mscp->ca = 0x01;
480 memcpy(&mscp->scsi_cmd, xs->cmd, mscp->scsi_cmd_length);
481 }
482 mscp->xdir = UHA_SDET;
483 mscp->dcn = 0x00;
484 mscp->chan = 0x00;
485 mscp->target = periph->periph_target;
486 mscp->lun = periph->periph_lun;
487 mscp->scsi_cmd_length = xs->cmdlen;
488 mscp->sense_ptr = sc->sc_dmamap_mscp->dm_segs[0].ds_addr +
489 UHA_MSCP_OFF(mscp) + offsetof(struct uha_mscp, mscp_sense);
490 mscp->req_sense_length = sizeof(mscp->mscp_sense);
491 mscp->host_stat = 0x00;
492 mscp->target_stat = 0x00;
493
494 if (xs->datalen) {
495 seg = 0;
496 #ifdef TFS
497 if (flags & SCSI_DATA_UIO) {
498 error = bus_dmamap_load_uio(dmat,
499 mscp->dmamap_xfer, (struct uio *)xs->data,
500 ((flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT :
501 BUS_DMA_WAITOK) | BUS_DMA_STREAMING |
502 ((flags & XS_CTL_DATA_IN) ? BUS_DMA_READ :
503 BUS_DMA_WRITE));
504 } else
505 #endif /*TFS */
506 {
507 error = bus_dmamap_load(dmat,
508 mscp->dmamap_xfer, xs->data, xs->datalen,
509 NULL,
510 ((flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT :
511 BUS_DMA_WAITOK) | BUS_DMA_STREAMING |
512 ((flags & XS_CTL_DATA_IN) ? BUS_DMA_READ :
513 BUS_DMA_WRITE));
514 }
515
516 switch (error) {
517 case 0:
518 break;
519
520 case ENOMEM:
521 case EAGAIN:
522 xs->error = XS_RESOURCE_SHORTAGE;
523 goto out_bad;
524
525 default:
526 xs->error = XS_DRIVER_STUFFUP;
527 printf("%s: error %d loading DMA map\n",
528 sc->sc_dev.dv_xname, error);
529 out_bad:
530 uha_free_mscp(sc, mscp);
531 scsipi_done(xs);
532 return;
533 }
534
535 bus_dmamap_sync(dmat, mscp->dmamap_xfer, 0,
536 mscp->dmamap_xfer->dm_mapsize,
537 (flags & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD :
538 BUS_DMASYNC_PREWRITE);
539
540 /*
541 * Load the hardware scatter/gather map with the
542 * contents of the DMA map.
543 */
544 for (seg = 0;
545 seg < mscp->dmamap_xfer->dm_nsegs; seg++) {
546 mscp->uha_dma[seg].seg_addr =
547 mscp->dmamap_xfer->dm_segs[seg].ds_addr;
548 mscp->uha_dma[seg].seg_len =
549 mscp->dmamap_xfer->dm_segs[seg].ds_len;
550 }
551
552 mscp->data_addr =
553 sc->sc_dmamap_mscp->dm_segs[0].ds_addr +
554 UHA_MSCP_OFF(mscp) + offsetof(struct uha_mscp,
555 uha_dma);
556 mscp->data_length = xs->datalen;
557 mscp->sgth = 0x01;
558 mscp->sg_num = seg;
559 } else { /* No data xfer, use non S/G values */
560 mscp->data_addr = (physaddr)0;
561 mscp->data_length = 0;
562 mscp->sgth = 0x00;
563 mscp->sg_num = 0;
564 }
565 mscp->link_id = 0;
566 mscp->link_addr = (physaddr)0;
567
568 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_mscp,
569 UHA_MSCP_OFF(mscp), sizeof(struct uha_mscp),
570 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
571
572 s = splbio();
573 (sc->start_mbox)(sc, mscp);
574 splx(s);
575
576 if ((flags & XS_CTL_POLL) == 0)
577 return;
578
579 /*
580 * If we can't use interrupts, poll on completion
581 */
582 if ((sc->poll)(sc, xs, mscp->timeout)) {
583 uha_timeout(mscp);
584 if ((sc->poll)(sc, xs, mscp->timeout))
585 uha_timeout(mscp);
586 }
587 return;
588
589 case ADAPTER_REQ_GROW_RESOURCES:
590 /* XXX Not supported. */
591 return;
592
593 case ADAPTER_REQ_SET_XFER_MODE:
594 /*
595 * We can't really do this (the UltraStor controllers
596 * have their own config).
597 *
598 * XXX How do we query the config?
599 */
600 return;
601 }
602 }
603 void
604 uha_timeout(arg)
605 void *arg;
606 {
607 struct uha_mscp *mscp = arg;
608 struct scsipi_xfer *xs = mscp->xs;
609 struct scsipi_periph *periph = xs->xs_periph;
610 struct uha_softc *sc =
611 (void *)periph->periph_channel->chan_adapter->adapt_dev;
612 int s;
613
614 scsipi_printaddr(periph);
615 printf("timed out");
616
617 s = splbio();
618
619 if (mscp->flags & MSCP_ABORT) {
620 /* abort timed out */
621 printf(" AGAIN\n");
622 /* XXX Must reset! */
623 } else {
624 /* abort the operation that has timed out */
625 printf("\n");
626 mscp->xs->error = XS_TIMEOUT;
627 mscp->timeout = UHA_ABORT_TIMEOUT;
628 mscp->flags |= MSCP_ABORT;
629 (sc->start_mbox)(sc, mscp);
630 }
631
632 splx(s);
633 }
Cache object: bf33942eb3f3d58fc281b893e3ac322e
|