xref: /openbsd/sys/dev/ic/aic79xx_openbsd.c (revision a454aff3)
1 /*	$OpenBSD: aic79xx_openbsd.c,v 1.60 2022/04/16 19:19:58 naddy Exp $	*/
2 
3 /*
4  * Copyright (c) 2004 Milos Urbanek, Kenneth R. Westerback & Marco Peereboom
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  */
29 
30 /*
31  * Bus independent OpenBSD shim for the aic79xx based Adaptec SCSI controllers
32  *
33  * Copyright (c) 1994-2002 Justin T. Gibbs.
34  * Copyright (c) 2001-2002 Adaptec Inc.
35  * All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions, and the following disclaimer,
42  *    without modification.
43  * 2. The name of the author may not be used to endorse or promote products
44  *    derived from this software without specific prior written permission.
45  *
46  * Alternatively, this software may be distributed under the terms of the
47  * GNU Public License ("GPL").
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
53  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  */
62 
63 #include <dev/ic/aic79xx_openbsd.h>
64 #include <dev/ic/aic79xx_inline.h>
65 #include <dev/ic/aic79xx.h>
66 
67 #ifndef AHD_TMODE_ENABLE
68 #define AHD_TMODE_ENABLE 0
69 #endif
70 
71 /* XXX milos add ahd_ioctl */
72 void	ahd_action(struct scsi_xfer *);
73 void	ahd_execute_scb(void *, bus_dma_segment_t *, int);
74 int	ahd_poll(struct ahd_softc *, int);
75 void	ahd_setup_data(struct ahd_softc *, struct scsi_xfer *,
76 		    struct scb *);
77 
78 void	ahd_adapter_req_set_xfer_mode(struct ahd_softc *, struct scb *);
79 
80 struct cfdriver ahd_cd = {
81 	NULL, "ahd", DV_DULL
82 };
83 
84 static const struct scsi_adapter ahd_switch = {
85 	ahd_action, NULL, NULL, NULL, NULL
86 };
87 
88 /*
89  * Attach all the sub-devices we can find
90  */
91 int
ahd_attach(struct ahd_softc * ahd)92 ahd_attach(struct ahd_softc *ahd)
93 {
94 	struct scsibus_attach_args saa;
95 	char   ahd_info[256];
96 	int	s;
97 
98 	ahd_controller_info(ahd, ahd_info, sizeof ahd_info);
99 	printf("%s\n", ahd_info);
100 	ahd_lock(ahd, &s);
101 
102 	if (bootverbose) {
103 		ahd_controller_info(ahd, ahd_info, sizeof ahd_info);
104 		printf("%s: %s\n", ahd->sc_dev.dv_xname, ahd_info);
105 	}
106 
107 	ahd_intr_enable(ahd, TRUE);
108 
109 	if (ahd->flags & AHD_RESET_BUS_A)
110 		ahd_reset_channel(ahd, 'A', TRUE);
111 
112 	saa.saa_adapter_target = ahd->our_id;
113 	saa.saa_adapter_buswidth = (ahd->features & AHD_WIDE) ? 16 : 8;
114 	saa.saa_adapter_softc = ahd;
115 	saa.saa_adapter = &ahd_switch;
116 	saa.saa_luns = 8;
117 	saa.saa_openings = 16; /* Must ALWAYS be < 256!! */
118 	saa.saa_pool = &ahd->sc_iopool;
119 	saa.saa_quirks = saa.saa_flags = 0;
120 	saa.saa_wwpn = saa.saa_wwnn = 0;
121 
122 	ahd->sc_child = config_found((void *)&ahd->sc_dev, &saa, scsiprint);
123 
124 	ahd_unlock(ahd, &s);
125 
126 	return (1);
127 
128 }
129 
130 /*
131  * Catch an interrupt from the adapter
132  */
133 int
ahd_platform_intr(void * arg)134 ahd_platform_intr(void *arg)
135 {
136 	struct	ahd_softc *ahd;
137 
138 	/* XXX in ahc there is some bus_dmamap_sync(PREREAD|PREWRITE); */
139 
140 	ahd = (struct ahd_softc *)arg;
141 	return ahd_intr(ahd);
142 }
143 
144 /*
145  * We have an scb which has been processed by the
146  * adaptor, now we look to see how the operation
147  * went.
148  */
149 void
ahd_done(struct ahd_softc * ahd,struct scb * scb)150 ahd_done(struct ahd_softc *ahd, struct scb *scb)
151 {
152 	struct scsi_xfer *xs = scb->xs;
153 
154 	/* XXX in ahc there is some bus_dmamap_sync(PREREAD|PREWRITE); */
155 
156 	TAILQ_REMOVE(&ahd->pending_scbs, scb, next);
157 
158 	timeout_del(&xs->stimeout);
159 
160 	if (xs->datalen) {
161 		int op;
162 
163 		if ((xs->flags & SCSI_DATA_IN) != 0)
164 			op = BUS_DMASYNC_POSTREAD;
165 		else
166 			op = BUS_DMASYNC_POSTWRITE;
167 		bus_dmamap_sync(ahd->parent_dmat, scb->dmamap, 0,
168 		    scb->dmamap->dm_mapsize, op);
169 		bus_dmamap_unload(ahd->parent_dmat, scb->dmamap);
170 	}
171 
172 	/* Translate the CAM status code to a SCSI error code. */
173 	switch (xs->error) {
174 	case CAM_SCSI_STATUS_ERROR:
175 	case CAM_REQ_INPROG:
176 	case CAM_REQ_CMP:
177 		switch (xs->status) {
178 		case SCSI_TASKSET_FULL:
179 		case SCSI_BUSY:
180 			xs->error = XS_BUSY;
181 			break;
182 		case SCSI_CHECK:
183 		case SCSI_TERMINATED:
184 			if ((scb->flags & SCB_SENSE) == 0) {
185 				/* CHECK on CHECK? */
186 				xs->error = XS_DRIVER_STUFFUP;
187 			} else
188 				xs->error = XS_NOERROR;
189 			break;
190 		default:
191 			xs->error = XS_NOERROR;
192 			break;
193 		}
194 		break;
195 	case CAM_BUSY:
196 	case CAM_REQUEUE_REQ:
197 		xs->error = XS_BUSY;
198 		break;
199 	case CAM_CMD_TIMEOUT:
200 		xs->error = XS_TIMEOUT;
201 		break;
202 	case CAM_BDR_SENT:
203 	case CAM_SCSI_BUS_RESET:
204 		xs->error = XS_RESET;
205 		break;
206 	case CAM_SEL_TIMEOUT:
207 		xs->error = XS_SELTIMEOUT;
208 		break;
209 	default:
210 		xs->error = XS_DRIVER_STUFFUP;
211 		break;
212 	}
213 
214 	if (xs->error != XS_NOERROR) {
215 		/* Don't clobber any existing error state */
216 	} else if ((scb->flags & SCB_SENSE) != 0) {
217 		/*
218 		 * We performed autosense retrieval.
219 		 *
220 		 * Zero any sense not transferred by the
221 		 * device.  The SCSI spec mandates that any
222 		 * untransferred data should be assumed to be
223 		 * zero.  Complete the 'bounce' of sense information
224 		 * through buffers accessible via bus-space by
225 		 * copying it into the clients csio.
226 		 */
227 		memset(&xs->sense, 0, sizeof(struct scsi_sense_data));
228 		memcpy(&xs->sense, ahd_get_sense_buf(ahd, scb),
229 		    sizeof(struct scsi_sense_data));
230 		xs->error = XS_SENSE;
231 	} else if ((scb->flags & SCB_PKT_SENSE) != 0) {
232 		struct scsi_status_iu_header *siu;
233 		u_int32_t len;
234 
235 		siu = (struct scsi_status_iu_header *)scb->sense_data;
236 		len = SIU_SENSE_LENGTH(siu);
237 		memset(&xs->sense, 0, sizeof(xs->sense));
238 		memcpy(&xs->sense, SIU_SENSE_DATA(siu),
239 		    ulmin(len, sizeof(xs->sense)));
240 		xs->error = XS_SENSE;
241 	}
242 
243 	scsi_done(xs);
244 }
245 
246 void
ahd_action(struct scsi_xfer * xs)247 ahd_action(struct scsi_xfer *xs)
248 {
249 	struct	ahd_softc *ahd;
250 	struct scb *scb;
251 	struct hardware_scb *hscb;
252 	u_int	target_id;
253 	u_int	our_id;
254 	int	s;
255 	struct	ahd_initiator_tinfo *tinfo;
256 	struct	ahd_tmode_tstate *tstate;
257 	u_int16_t quirks;
258 
259 #ifdef AHD_DEBUG
260 	printf("%s: ahd_action\n", ahd_name(ahd));
261 #endif
262 	ahd = xs->sc_link->bus->sb_adapter_softc;
263 
264 	target_id = xs->sc_link->target;
265 	our_id = SCSI_SCSI_ID(ahd, xs->sc_link);
266 
267 	ahd_lock(ahd, &s);
268 	if ((ahd->flags & AHD_INITIATORROLE) == 0) {
269 		xs->error = XS_DRIVER_STUFFUP;
270 		scsi_done(xs);
271 		ahd_unlock(ahd, &s);
272 		return;
273 	}
274 	/*
275 	 * get an scb to use.
276 	 */
277 	tinfo = ahd_fetch_transinfo(ahd, 'A', our_id, target_id, &tstate);
278 
279 	quirks = xs->sc_link->quirks;
280 
281 	ahd_unlock(ahd, &s);
282 
283 	scb = xs->io;
284 	hscb = scb->hscb;
285 	scb->flags = SCB_FLAG_NONE;
286 	scb->hscb->control = 0;
287 	ahd->scb_data.scbindex[SCB_GET_TAG(scb)] = NULL;
288 
289 #ifdef AHD_DEBUG
290 	printf("%s: start scb(%p)\n", ahd_name(ahd), scb);
291 #endif
292 
293 	scb->xs = xs;
294 	timeout_set(&xs->stimeout, ahd_timeout, scb);
295 
296 	/*
297 	 * Put all the arguments for the xfer in the scb
298 	 */
299 	hscb->control = 0;
300 	hscb->scsiid = BUILD_SCSIID(ahd, xs->sc_link, target_id, our_id);
301 	hscb->lun = xs->sc_link->lun;
302 	if (xs->xs_control & XS_CTL_RESET) {
303 		hscb->cdb_len = 0;
304 		scb->flags |= SCB_DEVICE_RESET;
305 		hscb->control |= MK_MESSAGE;
306 		hscb->task_management = SIU_TASKMGMT_LUN_RESET;
307 		ahd_execute_scb(scb, NULL, 0);
308 	} else {
309 		hscb->task_management = 0;
310 		ahd_setup_data(ahd, xs, scb);
311 	}
312 }
313 
314 void
ahd_execute_scb(void * arg,bus_dma_segment_t * dm_segs,int nsegments)315 ahd_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nsegments)
316 {
317 	struct	scb *scb;
318 	struct	scsi_xfer *xs;
319 	struct	ahd_softc *ahd;
320 	struct	ahd_initiator_tinfo *tinfo;
321 	struct	ahd_tmode_tstate *tstate;
322 	u_int	mask;
323 	int	s;
324 
325 	scb = (struct scb *)arg;
326 	xs = scb->xs;
327 	xs->error = CAM_REQ_INPROG;
328 	xs->status = 0;
329 	ahd = xs->sc_link->bus->sb_adapter_softc;
330 
331 	if (nsegments != 0) {
332 		void *sg;
333 		int op;
334 		u_int i;
335 
336 		ahd_setup_data_scb(ahd, scb);
337 
338 		/* Copy the segments into our SG list */
339 		for (i = nsegments, sg = scb->sg_list; i > 0; i--) {
340 
341 			sg = ahd_sg_setup(ahd, scb, sg, dm_segs->ds_addr,
342 					  dm_segs->ds_len,
343 					  /*last*/i == 1);
344 			dm_segs++;
345 		}
346 
347 		if ((xs->flags & SCSI_DATA_IN) != 0)
348 			op = BUS_DMASYNC_PREREAD;
349 		else
350 			op = BUS_DMASYNC_PREWRITE;
351 
352 		bus_dmamap_sync(ahd->parent_dmat, scb->dmamap, 0,
353 				scb->dmamap->dm_mapsize, op);
354 
355 	}
356 
357 	ahd_lock(ahd, &s);
358 
359 	/*
360 	 * Last time we need to check if this SCB needs to
361 	 * be aborted.
362 	 */
363 	if (xs->flags & ITSDONE) {
364 		if (nsegments != 0)
365 			bus_dmamap_unload(ahd->parent_dmat,
366 					  scb->dmamap);
367 		ahd_unlock(ahd, &s);
368 		return;
369 	}
370 
371 	tinfo = ahd_fetch_transinfo(ahd, SCSIID_CHANNEL(ahd, scb->hscb->scsiid),
372 				    SCSIID_OUR_ID(scb->hscb->scsiid),
373 				    SCSIID_TARGET(ahd, scb->hscb->scsiid),
374 				    &tstate);
375 
376 	mask = SCB_GET_TARGET_MASK(ahd, scb);
377 
378 	if ((tstate->discenable & mask) != 0)
379 		scb->hscb->control |= DISCENB;
380 
381 	if ((tstate->tagenable & mask) != 0)
382 		scb->hscb->control |= TAG_ENB;
383 
384 	if ((tinfo->curr.ppr_options & MSG_EXT_PPR_PROT_IUS) != 0) {
385 		scb->flags |= SCB_PACKETIZED;
386 		if (scb->hscb->task_management != 0)
387 			scb->hscb->control &= ~MK_MESSAGE;
388 	}
389 
390 	if ((tstate->auto_negotiate & mask) != 0) {
391 		scb->flags |= SCB_AUTO_NEGOTIATE;
392 		scb->hscb->control |= MK_MESSAGE;
393 	}
394 
395 	/* XXX with ahc there was some bus_dmamap_sync(PREREAD|PREWRITE); */
396 
397 	TAILQ_INSERT_HEAD(&ahd->pending_scbs, scb, next);
398 
399 	if (!(xs->flags & SCSI_POLL))
400 		timeout_add_msec(&xs->stimeout, xs->timeout);
401 
402 	scb->flags |= SCB_ACTIVE;
403 
404 	if ((scb->flags & SCB_TARGET_IMMEDIATE) != 0) {
405 		/* Define a mapping from our tag to the SCB. */
406 		ahd->scb_data.scbindex[SCB_GET_TAG(scb)] = scb;
407 		ahd_pause(ahd);
408 		ahd_set_scbptr(ahd, SCB_GET_TAG(scb));
409 		ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_TARG);
410 		ahd_unpause(ahd);
411 	} else {
412 		ahd_queue_scb(ahd, scb);
413 	}
414 
415 	if (!(xs->flags & SCSI_POLL)) {
416 		int target = xs->sc_link->target;
417 		int lun = SCB_GET_LUN(scb);
418 
419 		if (ahd->inited_target[target] == 0) {
420 			struct  ahd_devinfo devinfo;
421 
422 			ahd_adapter_req_set_xfer_mode(ahd, scb);
423 			ahd_compile_devinfo(&devinfo, ahd->our_id, target, lun,
424 			    'A', /*XXX milos*/ROLE_UNKNOWN);
425 			ahd_scb_devinfo(ahd, &devinfo, scb);
426 			ahd_update_neg_request(ahd, &devinfo, tstate, tinfo,
427 				AHD_NEG_IF_NON_ASYNC);
428 			ahd->inited_target[target] = 1;
429 		}
430 
431 		ahd_unlock(ahd, &s);
432 		return;
433 	}
434 
435 	/*
436 	 * If we can't use interrupts, poll for completion
437 	 */
438 #ifdef AHD_DEBUG
439 	printf("%s: cmd_poll\n", ahd_name(ahd));
440 #endif
441 
442 	do {
443 		if (ahd_poll(ahd, xs->timeout)) {
444 			if (!(xs->flags & SCSI_SILENT))
445 				printf("cmd fail\n");
446 			ahd_timeout(scb);
447 			break;
448 		}
449 	} while (!(xs->flags & ITSDONE));
450 
451 	ahd_unlock(ahd, &s);
452 }
453 
454 int
ahd_poll(struct ahd_softc * ahd,int wait)455 ahd_poll(struct ahd_softc *ahd, int wait)
456 {
457 	while (--wait) {
458 		DELAY(1000);
459 		if (ahd_inb(ahd, INTSTAT) & INT_PEND)
460 			break;
461 	}
462 
463 	if (wait == 0) {
464 		printf("%s: board is not responding\n", ahd_name(ahd));
465 		return (EIO);
466 	}
467 
468 	ahd_intr((void *)ahd);
469 	return (0);
470 }
471 
472 void
ahd_setup_data(struct ahd_softc * ahd,struct scsi_xfer * xs,struct scb * scb)473 ahd_setup_data(struct ahd_softc *ahd, struct scsi_xfer *xs,
474 	       struct scb *scb)
475 {
476 	struct hardware_scb *hscb;
477 
478 	hscb = scb->hscb;
479 	xs->resid = xs->status = 0;
480 	xs->error = CAM_REQ_INPROG;
481 
482 	hscb->cdb_len = xs->cmdlen;
483 	if (hscb->cdb_len > MAX_CDB_LEN) {
484 		xs->error = XS_DRIVER_STUFFUP;
485 		scsi_done(xs);
486 		return;
487 	}
488 
489 	memcpy(hscb->shared_data.idata.cdb, &xs->cmd, hscb->cdb_len);
490 
491 	/* Only use S/G if there is a transfer */
492 	if (xs->datalen) {
493 		int error;
494 
495 		error = bus_dmamap_load(ahd->parent_dmat,
496 					scb->dmamap, xs->data,
497 					xs->datalen, NULL,
498 					((xs->flags & SCSI_NOSLEEP) ?
499 					BUS_DMA_NOWAIT : BUS_DMA_WAITOK) |
500 					BUS_DMA_STREAMING |
501 					((xs->flags & XS_CTL_DATA_IN) ?
502 					BUS_DMA_READ : BUS_DMA_WRITE));
503 		if (error) {
504 #ifdef AHD_DEBUG
505 			printf("%s: in ahd_setup_data(): bus_dmamap_load() "
506 			    "= %d\n", ahd_name(ahd), error);
507 #endif
508 			xs->error = XS_DRIVER_STUFFUP;
509 			scsi_done(xs);
510 			return;
511 		}
512 		ahd_execute_scb(scb, scb->dmamap->dm_segs,
513 		    scb->dmamap->dm_nsegs);
514 	} else {
515 		ahd_execute_scb(scb, NULL, 0);
516 	}
517 }
518 
519 void
ahd_platform_set_tags(struct ahd_softc * ahd,struct ahd_devinfo * devinfo,ahd_queue_alg alg)520 ahd_platform_set_tags(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
521     ahd_queue_alg alg)
522 {
523 	struct ahd_tmode_tstate *tstate;
524 
525 	ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid,
526 	    devinfo->target, &tstate);
527 
528 	if (alg != AHD_QUEUE_NONE)
529 		tstate->tagenable |= devinfo->target_mask;
530 	else
531 		tstate->tagenable &= ~devinfo->target_mask;
532 }
533 
534 int
ahd_softc_comp(struct ahd_softc * lahd,struct ahd_softc * rahd)535 ahd_softc_comp(struct ahd_softc *lahd, struct ahd_softc *rahd)
536 {
537 	/* We don't sort softcs under OpenBSD so report equal always */
538 	return (0);
539 }
540 
541 int
ahd_detach(struct device * self,int flags)542 ahd_detach(struct device *self, int flags)
543 {
544 	int rv = 0;
545 
546 	struct ahd_softc *ahd = (struct ahd_softc*)self;
547 
548 	if (ahd->sc_child != NULL)
549 		rv = config_detach((void *)ahd->sc_child, flags);
550 
551 	ahd_free(ahd);
552 
553 	return rv;
554 }
555 
556 void
ahd_adapter_req_set_xfer_mode(struct ahd_softc * ahd,struct scb * scb)557 ahd_adapter_req_set_xfer_mode(struct ahd_softc *ahd, struct scb *scb)
558 {
559 	struct ahd_initiator_tinfo *tinfo;
560 	struct ahd_tmode_tstate *tstate;
561 	int target_id, our_id;
562 	struct ahd_devinfo devinfo;
563 	u_int16_t quirks;
564 	u_int width, ppr_options, period, offset;
565 	int s;
566 
567 	target_id = scb->xs->sc_link->target;
568 	our_id = SCSI_SCSI_ID(ahd, scb->xs->sc_link);
569 
570 	s = splbio();
571 
572 	quirks = scb->xs->sc_link->quirks;
573 	tinfo = ahd_fetch_transinfo(ahd, 'A', our_id, target_id, &tstate);
574 	ahd_compile_devinfo(&devinfo, our_id, target_id, 0, 'A',
575 	    ROLE_INITIATOR);
576 
577 	tstate->discenable |= (ahd->user_discenable & devinfo.target_mask);
578 
579 	if (quirks & SDEV_NOTAGS)
580 		tstate->tagenable &= ~devinfo.target_mask;
581 	else if (ahd->user_tagenable & devinfo.target_mask)
582 		tstate->tagenable |= devinfo.target_mask;
583 
584 	if (quirks & SDEV_NOWIDE)
585 		width = MSG_EXT_WDTR_BUS_8_BIT;
586 	else
587 		width = MSG_EXT_WDTR_BUS_16_BIT;
588 
589 	ahd_validate_width(ahd, NULL, &width, ROLE_UNKNOWN);
590 	if (width > tinfo->user.width)
591 		width = tinfo->user.width;
592 	ahd_set_width(ahd, &devinfo, width, AHD_TRANS_GOAL, FALSE);
593 
594 	if (quirks & SDEV_NOSYNC) {
595 		period = 0;
596 		offset = 0;
597 	} else {
598 		period = tinfo->user.period;
599 		offset = tinfo->user.offset;
600 	}
601 
602 	/* XXX Look at saved INQUIRY flags for PPR capabilities XXX */
603 	ppr_options = tinfo->user.ppr_options;
604 	/* XXX Other reasons to avoid ppr? XXX */
605 	if (width < MSG_EXT_WDTR_BUS_16_BIT)
606 		ppr_options = 0;
607 
608 	if ((tstate->discenable & devinfo.target_mask) == 0 ||
609 	    (tstate->tagenable & devinfo.target_mask) == 0)
610 		ppr_options &= ~MSG_EXT_PPR_PROT_IUS;
611 
612 	ahd_find_syncrate(ahd, &period, &ppr_options, AHD_SYNCRATE_MAX);
613 	ahd_validate_offset(ahd, NULL, period, &offset, width, ROLE_UNKNOWN);
614 
615 	if (offset == 0) {
616 		period = 0;
617 		ppr_options = 0;
618 	}
619 
620 	if (ppr_options != 0 && tinfo->user.transport_version >= 3) {
621 		tinfo->goal.transport_version = tinfo->user.transport_version;
622 		tinfo->curr.transport_version = tinfo->user.transport_version;
623 	}
624 
625 	ahd_set_syncrate(ahd, &devinfo, period, offset, ppr_options,
626 		AHD_TRANS_GOAL, FALSE);
627 
628 	splx(s);
629 }
630 
631 void
aic_timer_reset(aic_timer_t * timer,u_int msec,ahd_callback_t * func,void * arg)632 aic_timer_reset(aic_timer_t *timer, u_int msec, ahd_callback_t *func,
633     void *arg)
634 {
635 	uint64_t nticks;
636 
637 	nticks = msec;
638 	nticks *= hz;
639 	nticks /= 1000;
640 	callout_reset(timer, nticks, func, arg);
641 }
642 
643 void
aic_scb_timer_reset(struct scb * scb,u_int msec)644 aic_scb_timer_reset(struct scb *scb, u_int msec)
645 {
646 	uint64_t nticks;
647 
648 	nticks = msec;
649 	nticks *= hz;
650 	nticks /= 1000;
651 	if (!(scb->xs->xs_control & XS_CTL_POLL))
652 		callout_reset(&scb->xs->xs_callout, nticks, ahd_timeout, scb);
653 }
654 
655 void
ahd_flush_device_writes(struct ahd_softc * ahd)656 ahd_flush_device_writes(struct ahd_softc *ahd)
657 {
658 	/* XXX Is this sufficient for all architectures??? */
659 	ahd_inb(ahd, INTSTAT);
660 }
661 
662 void
aic_platform_scb_free(struct ahd_softc * ahd,struct scb * scb)663 aic_platform_scb_free(struct ahd_softc *ahd, struct scb *scb)
664 {
665 	int s;
666 
667 	ahd_lock(ahd, &s);
668 
669 	if ((ahd->flags & AHD_RESOURCE_SHORTAGE) != 0) {
670 		ahd->flags &= ~AHD_RESOURCE_SHORTAGE;
671 	}
672 
673 	if (!cold) {
674 		/* we are no longer in autoconf */
675 		timeout_del(&scb->xs->stimeout);
676 	}
677 
678 	ahd_unlock(ahd, &s);
679 }
680 
681 void
ahd_print_path(struct ahd_softc * ahd,struct scb * scb)682 ahd_print_path(struct ahd_softc *ahd, struct scb *scb)
683 {
684 	sc_print_addr(scb->xs->sc_link);
685 }
686 
687 void
ahd_platform_dump_card_state(struct ahd_softc * ahd)688 ahd_platform_dump_card_state(struct ahd_softc *ahd)
689 {
690 	/* Nothing to do here for OpenBSD */
691 	printf("FEATURES = 0x%x, FLAGS = 0x%x, CHIP = 0x%x BUGS =0x%x\n",
692 		ahd->features, ahd->flags, ahd->chip, ahd->bugs);
693 }
694 
695