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